Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1876)

Side by Side Diff: pc/channel.cc

Issue 3012953002: Created the DtlsSrtpTransport.
Patch Set: Resolved the comments. Created 3 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 enum { 56 enum {
57 MSG_EARLYMEDIATIMEOUT = 1, 57 MSG_EARLYMEDIATIMEOUT = 1,
58 MSG_SEND_RTP_PACKET, 58 MSG_SEND_RTP_PACKET,
59 MSG_SEND_RTCP_PACKET, 59 MSG_SEND_RTCP_PACKET,
60 MSG_CHANNEL_ERROR, 60 MSG_CHANNEL_ERROR,
61 MSG_READYTOSENDDATA, 61 MSG_READYTOSENDDATA,
62 MSG_DATARECEIVED, 62 MSG_DATARECEIVED,
63 MSG_FIRSTPACKETRECEIVED, 63 MSG_FIRSTPACKETRECEIVED,
64 }; 64 };
65 65
66 // Value specified in RFC 5764.
67 static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
68
69 static const int kAgcMinus10db = -10; 66 static const int kAgcMinus10db = -10;
70 67
71 static void SafeSetError(const std::string& message, std::string* error_desc) { 68 static void SafeSetError(const std::string& message, std::string* error_desc) {
72 if (error_desc) { 69 if (error_desc) {
73 *error_desc = message; 70 *error_desc = message;
74 } 71 }
75 } 72 }
76 73
77 struct VoiceChannelErrorMessageData : public rtc::MessageData { 74 struct VoiceChannelErrorMessageData : public rtc::MessageData {
78 VoiceChannelErrorMessageData(uint32_t in_ssrc, 75 VoiceChannelErrorMessageData(uint32_t in_ssrc,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 bool srtp_required) 145 bool srtp_required)
149 : worker_thread_(worker_thread), 146 : worker_thread_(worker_thread),
150 network_thread_(network_thread), 147 network_thread_(network_thread),
151 signaling_thread_(signaling_thread), 148 signaling_thread_(signaling_thread),
152 content_name_(content_name), 149 content_name_(content_name),
153 rtcp_mux_required_(rtcp_mux_required), 150 rtcp_mux_required_(rtcp_mux_required),
154 srtp_required_(srtp_required), 151 srtp_required_(srtp_required),
155 media_channel_(media_channel), 152 media_channel_(media_channel),
156 selected_candidate_pair_(nullptr) { 153 selected_candidate_pair_(nullptr) {
157 RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); 154 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
158 if (srtp_required) { 155 rtp_transport_ = rtc::MakeUnique<webrtc::RtpTransport>(rtcp_mux_required);
159 auto transport =
160 rtc::MakeUnique<webrtc::SrtpTransport>(rtcp_mux_required, content_name);
161 srtp_transport_ = transport.get();
162 rtp_transport_ = std::move(transport);
163 #if defined(ENABLE_EXTERNAL_AUTH)
164 srtp_transport_->EnableExternalAuth();
165 #endif
166 } else {
167 rtp_transport_ = rtc::MakeUnique<webrtc::RtpTransport>(rtcp_mux_required);
168 srtp_transport_ = nullptr;
169 }
170 rtp_transport_->SignalReadyToSend.connect( 156 rtp_transport_->SignalReadyToSend.connect(
171 this, &BaseChannel::OnTransportReadyToSend); 157 this, &BaseChannel::OnTransportReadyToSend);
172 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced 158 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced
173 // with a callback interface later so that the demuxer can select which 159 // with a callback interface later so that the demuxer can select which
174 // channel to signal. 160 // channel to signal.
175 rtp_transport_->SignalPacketReceived.connect(this, 161 rtp_transport_->SignalPacketReceived.connect(this,
176 &BaseChannel::OnPacketReceived); 162 &BaseChannel::OnPacketReceived);
177 LOG(LS_INFO) << "Created channel for " << content_name; 163 LOG(LS_INFO) << "Created channel for " << content_name;
178 } 164 }
179 165
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 return; 297 return;
312 } 298 }
313 299
314 // When using DTLS-SRTP, we must reset the SrtpTransport every time the 300 // When using DTLS-SRTP, we must reset the SrtpTransport every time the
315 // DtlsTransport changes and wait until the DTLS handshake is complete to set 301 // DtlsTransport changes and wait until the DTLS handshake is complete to set
316 // the newly negotiated parameters. 302 // the newly negotiated parameters.
317 if (ShouldSetupDtlsSrtp_n()) { 303 if (ShouldSetupDtlsSrtp_n()) {
318 // Set |writable_| to false such that UpdateWritableState_w can set up 304 // Set |writable_| to false such that UpdateWritableState_w can set up
319 // DTLS-SRTP when |writable_| becomes true again. 305 // DTLS-SRTP when |writable_| becomes true again.
320 writable_ = false; 306 writable_ = false;
321 dtls_active_ = false; 307 if (dtls_srtp_transport_) {
322 if (srtp_transport_) { 308 dtls_srtp_transport_->ResetParams();
323 srtp_transport_->ResetParams();
324 } 309 }
325 } 310 }
326 311
327 // If this BaseChannel doesn't require RTCP mux and we haven't fully 312 // If this BaseChannel doesn't require RTCP mux and we haven't fully
328 // negotiated RTCP mux, we need an RTCP transport. 313 // negotiated RTCP mux, we need an RTCP transport.
329 if (rtcp_packet_transport) { 314 if (rtcp_packet_transport) {
330 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on " 315 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
331 << debug_name << " transport " << rtcp_packet_transport; 316 << debug_name << " transport " << rtcp_packet_transport;
332 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport); 317 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport);
333 } 318 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 } 362 }
378 363
379 if (rtcp && new_dtls_transport) { 364 if (rtcp && new_dtls_transport) {
380 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_active())) 365 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_active()))
381 << "Setting RTCP for DTLS/SRTP after the DTLS is active " 366 << "Setting RTCP for DTLS/SRTP after the DTLS is active "
382 << "should never happen."; 367 << "should never happen.";
383 } 368 }
384 369
385 if (new_dtls_transport) { 370 if (new_dtls_transport) {
386 ConnectToDtlsTransport(new_dtls_transport); 371 ConnectToDtlsTransport(new_dtls_transport);
372 if (dtls_srtp_transport_) {
373 rtcp ? dtls_srtp_transport_->SetRtcpDtlsTransport(new_dtls_transport)
374 : dtls_srtp_transport_->SetRtpDtlsTransport(new_dtls_transport);
375 }
387 } else { 376 } else {
388 ConnectToPacketTransport(new_packet_transport); 377 ConnectToPacketTransport(new_packet_transport);
389 } 378 }
390 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_; 379 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
391 for (const auto& pair : socket_options) { 380 for (const auto& pair : socket_options) {
392 new_packet_transport->SetOption(pair.first, pair.second); 381 new_packet_transport->SetOption(pair.first, pair.second);
393 } 382 }
394 } 383 }
395 384
396 void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) { 385 void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) {
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 if (!ShouldSetupDtlsSrtp_n()) { 569 if (!ShouldSetupDtlsSrtp_n()) {
581 return; 570 return;
582 } 571 }
583 572
584 // Reset the SrtpTransport if it's not the CONNECTED state. For the CONNECTED 573 // Reset the SrtpTransport if it's not the CONNECTED state. For the CONNECTED
585 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to 574 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
586 // cover other scenarios like the whole transport is writable (not just this 575 // cover other scenarios like the whole transport is writable (not just this
587 // TransportChannel) or when TransportChannel is attached after DTLS is 576 // TransportChannel) or when TransportChannel is attached after DTLS is
588 // negotiated. 577 // negotiated.
589 if (state != DTLS_TRANSPORT_CONNECTED) { 578 if (state != DTLS_TRANSPORT_CONNECTED) {
590 dtls_active_ = false; 579 if (dtls_srtp_transport_) {
591 if (srtp_transport_) { 580 dtls_srtp_transport_->ResetParams();
592 srtp_transport_->ResetParams();
593 } 581 }
594 } 582 }
595 } 583 }
596 584
597 void BaseChannel::OnSelectedCandidatePairChanged( 585 void BaseChannel::OnSelectedCandidatePairChanged(
598 IceTransportInternal* ice_transport, 586 IceTransportInternal* ice_transport,
599 CandidatePairInterface* selected_candidate_pair, 587 CandidatePairInterface* selected_candidate_pair,
600 int last_sent_packet_id, 588 int last_sent_packet_id,
601 bool ready_to_send) { 589 bool ready_to_send) {
602 RTC_DCHECK((rtp_dtls_transport_ && 590 RTC_DCHECK((rtp_dtls_transport_ &&
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 // (and SetSend(true) is called). 664 // (and SetSend(true) is called).
677 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive" 665 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
678 << " and crypto is required"; 666 << " and crypto is required";
679 RTC_NOTREACHED(); 667 RTC_NOTREACHED();
680 return false; 668 return false;
681 } 669 }
682 // Bon voyage. 670 // Bon voyage.
683 return rtcp ? rtp_transport_->SendRtcpPacket(packet, options, PF_NORMAL) 671 return rtcp ? rtp_transport_->SendRtcpPacket(packet, options, PF_NORMAL)
684 : rtp_transport_->SendRtpPacket(packet, options, PF_NORMAL); 672 : rtp_transport_->SendRtpPacket(packet, options, PF_NORMAL);
685 } 673 }
686 RTC_DCHECK(srtp_transport_); 674
687 RTC_DCHECK(srtp_transport_->IsActive()); 675 if (dtls_srtp_transport_) {
676 RTC_DCHECK(!sdes_transport_);
677 RTC_DCHECK(dtls_srtp_transport_->IsActive());
678 // Bon voyage.
679 return rtcp ? dtls_srtp_transport_->SendRtcpPacket(packet, options,
680 PF_SRTP_BYPASS)
681 : dtls_srtp_transport_->SendRtpPacket(packet, options,
682 PF_SRTP_BYPASS);
683 }
684
685 RTC_DCHECK(sdes_transport_);
686 RTC_DCHECK(sdes_transport_->IsActive());
688 // Bon voyage. 687 // Bon voyage.
689 return rtcp ? srtp_transport_->SendRtcpPacket(packet, options, PF_SRTP_BYPASS) 688 return rtcp ? sdes_transport_->SendRtcpPacket(packet, options, PF_SRTP_BYPASS)
690 : srtp_transport_->SendRtpPacket(packet, options, PF_SRTP_BYPASS); 689 : sdes_transport_->SendRtpPacket(packet, options, PF_SRTP_BYPASS);
691 } 690 }
692 691
693 bool BaseChannel::HandlesPayloadType(int packet_type) const { 692 bool BaseChannel::HandlesPayloadType(int packet_type) const {
694 return rtp_transport_->HandlesPayloadType(packet_type); 693 return rtp_transport_->HandlesPayloadType(packet_type);
695 } 694 }
696 695
697 void BaseChannel::OnPacketReceived(bool rtcp, 696 void BaseChannel::OnPacketReceived(bool rtcp,
698 rtc::CopyOnWriteBuffer* packet, 697 rtc::CopyOnWriteBuffer* packet,
699 const rtc::PacketTime& packet_time) { 698 const rtc::PacketTime& packet_time) {
700 if (!has_received_packet_ && !rtcp) { 699 if (!has_received_packet_ && !rtcp) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) { 803 void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
805 RTC_DCHECK(signaling_thread() == rtc::Thread::Current()); 804 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
806 SignalDtlsSrtpSetupFailure(this, rtcp); 805 SignalDtlsSrtpSetupFailure(this, rtcp);
807 } 806 }
808 807
809 bool BaseChannel::ShouldSetupDtlsSrtp_n() const { 808 bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
810 // Since DTLS is applied to all transports, checking RTP should be enough. 809 // Since DTLS is applied to all transports, checking RTP should be enough.
811 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive(); 810 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
812 } 811 }
813 812
814 // This function returns true if either DTLS-SRTP is not in use
815 // *or* DTLS-SRTP is successfully set up.
816 bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
817 RTC_DCHECK(network_thread_->IsCurrent());
818 bool ret = false;
819
820 DtlsTransportInternal* transport =
821 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
822 RTC_DCHECK(transport);
823 RTC_DCHECK(transport->IsDtlsActive());
824
825 int selected_crypto_suite;
826
827 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
828 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
829 return false;
830 }
831
832 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
833 << RtpRtcpStringLiteral(rtcp);
834
835 int key_len;
836 int salt_len;
837 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
838 &salt_len)) {
839 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
840 return false;
841 }
842
843 // OK, we're now doing DTLS (RFC 5764)
844 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
845
846 // RFC 5705 exporter using the RFC 5764 parameters
847 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
848 &dtls_buffer[0], dtls_buffer.size())) {
849 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
850 RTC_NOTREACHED(); // This should never happen
851 return false;
852 }
853
854 // Sync up the keys with the DTLS-SRTP interface
855 std::vector<unsigned char> client_write_key(key_len + salt_len);
856 std::vector<unsigned char> server_write_key(key_len + salt_len);
857 size_t offset = 0;
858 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
859 offset += key_len;
860 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
861 offset += key_len;
862 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
863 offset += salt_len;
864 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
865
866 std::vector<unsigned char> *send_key, *recv_key;
867 rtc::SSLRole role;
868 if (!transport->GetSslRole(&role)) {
869 LOG(LS_WARNING) << "GetSslRole failed";
870 return false;
871 }
872
873 if (role == rtc::SSL_SERVER) {
874 send_key = &server_write_key;
875 recv_key = &client_write_key;
876 } else {
877 send_key = &client_write_key;
878 recv_key = &server_write_key;
879 }
880
881 if (rtcp) {
882 if (!dtls_active()) {
883 RTC_DCHECK(srtp_transport_);
884 ret = srtp_transport_->SetRtcpParams(
885 selected_crypto_suite, &(*send_key)[0],
886 static_cast<int>(send_key->size()), selected_crypto_suite,
887 &(*recv_key)[0], static_cast<int>(recv_key->size()));
888 } else {
889 // RTCP doesn't need to call SetRtpParam because it is only used
890 // to make the updated encrypted RTP header extension IDs take effect.
891 ret = true;
892 }
893 } else {
894 RTC_DCHECK(srtp_transport_);
895 ret = srtp_transport_->SetRtpParams(selected_crypto_suite, &(*send_key)[0],
896 static_cast<int>(send_key->size()),
897 selected_crypto_suite, &(*recv_key)[0],
898 static_cast<int>(recv_key->size()));
899 dtls_active_ = ret;
900 }
901
902 if (!ret) {
903 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
904 } else {
905 UpdateTransportOverhead();
906 }
907 return ret;
908 }
909
910 void BaseChannel::MaybeSetupDtlsSrtp_n() { 813 void BaseChannel::MaybeSetupDtlsSrtp_n() {
911 if (dtls_active()) { 814 if (dtls_active()) {
912 return; 815 return;
913 } 816 }
914 817
915 if (!ShouldSetupDtlsSrtp_n()) { 818 if (!ShouldSetupDtlsSrtp_n()) {
916 return; 819 return;
917 } 820 }
918 821
919 if (!srtp_transport_) { 822 if (!dtls_srtp_transport_) {
920 EnableSrtpTransport_n(); 823 EnableDtlsSrtp_n();
921 } 824 }
922 825
923 if (!SetupDtlsSrtp_n(false)) { 826 if (!dtls_srtp_transport_->SetupDtlsSrtp(false)) {
924 SignalDtlsSrtpSetupFailure_n(false); 827 SignalDtlsSrtpSetupFailure_n(false);
925 return; 828 return;
926 } 829 }
927 830
928 if (rtcp_dtls_transport_) { 831 if (rtcp_dtls_transport_) {
929 if (!SetupDtlsSrtp_n(true)) { 832 if (!dtls_srtp_transport_->SetupDtlsSrtp(true)) {
930 SignalDtlsSrtpSetupFailure_n(true); 833 SignalDtlsSrtpSetupFailure_n(true);
931 return; 834 return;
932 } 835 }
933 } 836 }
934 } 837 }
935 838
936 void BaseChannel::ChannelNotWritable_n() { 839 void BaseChannel::ChannelNotWritable_n() {
937 RTC_DCHECK(network_thread_->IsCurrent()); 840 RTC_DCHECK(network_thread_->IsCurrent());
938 if (!writable_) 841 if (!writable_)
939 return; 842 return;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 bool* dtls, 899 bool* dtls,
997 std::string* error_desc) { 900 std::string* error_desc) {
998 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive(); 901 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
999 if (*dtls && !cryptos.empty()) { 902 if (*dtls && !cryptos.empty()) {
1000 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc); 903 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
1001 return false; 904 return false;
1002 } 905 }
1003 return true; 906 return true;
1004 } 907 }
1005 908
1006 void BaseChannel::EnableSrtpTransport_n() { 909 void BaseChannel::EnableSdes_n() {
1007 if (srtp_transport_ == nullptr) { 910 if (sdes_transport_) {
1008 rtp_transport_->SignalReadyToSend.disconnect(this); 911 return;
1009 rtp_transport_->SignalPacketReceived.disconnect(this); 912 }
913 // DtlsSrtpTransport and SrtpTransport shouldn't be enabled at the same
914 // time.
915 RTC_DCHECK(!dtls_srtp_transport_);
916 rtp_transport_->SignalReadyToSend.disconnect(this);
917 rtp_transport_->SignalPacketReceived.disconnect(this);
1010 918
1011 auto transport = rtc::MakeUnique<webrtc::SrtpTransport>( 919 auto transport = rtc::MakeUnique<webrtc::SrtpTransport>(
1012 std::move(rtp_transport_), content_name_); 920 std::move(rtp_transport_), content_name_);
1013 srtp_transport_ = transport.get(); 921 sdes_transport_ = transport.get();
1014 rtp_transport_ = std::move(transport); 922 #if defined(ENABLE_EXTERNAL_AUTH)
923 sdes_transport_->EnableExternalAuth();
924 #endif
925 rtp_transport_ = std::move(transport);
1015 926
1016 rtp_transport_->SignalReadyToSend.connect( 927 rtp_transport_->SignalReadyToSend.connect(
1017 this, &BaseChannel::OnTransportReadyToSend); 928 this, &BaseChannel::OnTransportReadyToSend);
1018 rtp_transport_->SignalPacketReceived.connect( 929 rtp_transport_->SignalPacketReceived.connect(this,
1019 this, &BaseChannel::OnPacketReceived); 930 &BaseChannel::OnPacketReceived);
1020 LOG(LS_INFO) << "Wrapping RtpTransport in SrtpTransport."; 931 LOG(LS_INFO) << "Wrapping RtpTransport in SrtpTransport.";
932 }
933
934 void BaseChannel::EnableDtlsSrtp_n() {
935 if (dtls_srtp_transport_) {
936 return;
937 }
938 // DtlsSrtpTransport and SrtpTransport shouldn't be enabled at the same
939 // time.
940 RTC_DCHECK(!sdes_transport_);
941 rtp_transport_->SignalReadyToSend.disconnect(this);
942 rtp_transport_->SignalPacketReceived.disconnect(this);
943
944 std::unique_ptr<webrtc::SrtpTransport> srtp_transport =
945 rtc::MakeUnique<webrtc::SrtpTransport>(std::move(rtp_transport_),
946 content_name_);
947 srtp_transport->SetSendEncryptedHeaderExtensionIds(
948 send_encrypted_header_extension_ids_);
949 srtp_transport->SetRecvEncryptedHeaderExtensionIds(
950 recv_encrypted_header_extension_ids_);
951 #if defined(ENABLE_EXTERNAL_AUTH)
952 srtp_transport->EnableExternalAuth();
953 #endif
954 std::unique_ptr<webrtc::DtlsSrtpTransport> transport =
955 rtc::MakeUnique<webrtc::DtlsSrtpTransport>(std::move(srtp_transport));
956 dtls_srtp_transport_ = transport.get();
957 dtls_srtp_transport_->SetRtcpDtlsTransport(rtcp_dtls_transport_);
958 dtls_srtp_transport_->SetRtpDtlsTransport(rtp_dtls_transport_);
959
960 rtp_transport_ = std::move(transport);
961 rtp_transport_->SignalReadyToSend.connect(
962 this, &BaseChannel::OnTransportReadyToSend);
963 rtp_transport_->SignalPacketReceived.connect(this,
964 &BaseChannel::OnPacketReceived);
965 LOG(LS_INFO) << "Wrapping SrtpTransport in DtlsSrtpTransport.";
966 }
967
968 void BaseChannel::CacheEncryptedHeaderExtensionIds(
969 cricket::ContentSource source,
970 const std::vector<int>& extension_ids) {
971 if (source == cricket::CS_LOCAL) {
972 recv_encrypted_header_extension_ids_ = extension_ids;
973 } else {
974 send_encrypted_header_extension_ids_ = extension_ids;
1021 } 975 }
1022 } 976 }
1023 977
1024 bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos, 978 bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
1025 ContentAction action, 979 ContentAction action,
1026 ContentSource src, 980 ContentSource src,
1027 const std::vector<int>& encrypted_extension_ids, 981 const std::vector<int>& encrypted_extension_ids,
1028 std::string* error_desc) { 982 std::string* error_desc) {
1029 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w"); 983 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
1030 if (action == CA_UPDATE) { 984 if (action == CA_UPDATE) {
1031 // no crypto params. 985 // no crypto params.
1032 return true; 986 return true;
1033 } 987 }
1034 bool ret = false; 988 bool ret = false;
1035 bool dtls = false; 989 bool dtls = false;
1036 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc); 990 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
1037 if (!ret) { 991 if (!ret) {
1038 return false; 992 return false;
1039 } 993 }
994 // Cache the encrypted extension ids. |SetSrtp_n| could happen before the
995 // certficate is ready and the |rtp_dtls_transport_| is active. The
Taylor Brandstetter 2017/09/27 23:54:48 certificate
996 // |dtls_srtp_transport_| is not enabled until the |rtp_dtls_transport_| is
997 // active.
998 CacheEncryptedHeaderExtensionIds(src, encrypted_extension_ids);
1040 999
1000 if (!dtls) {
1001 ret = SetSdesSrtp_n(cryptos, action, src, encrypted_extension_ids);
1002 } else {
1003 ret = SetDtlsSrtp_n(src, encrypted_extension_ids);
1004 }
1005
1006 if (!ret) {
1007 SafeSetError("Failed to setup SRTP.", error_desc);
1008 return false;
1009 }
1010 return true;
1011 }
1012
1013 bool BaseChannel::SetSdesSrtp_n(
1014 const std::vector<CryptoParams>& cryptos,
1015 ContentAction action,
1016 ContentSource src,
1017 const std::vector<int>& encrypted_extension_ids) {
1018 if (cryptos.empty()) {
1019 return true;
1020 }
1041 // If SRTP was not required, but we're setting a description that uses SDES, 1021 // If SRTP was not required, but we're setting a description that uses SDES,
1042 // we need to upgrade to an SrtpTransport. 1022 // we need to upgrade to an SrtpTransport.
1043 if (!srtp_transport_ && !dtls && !cryptos.empty()) { 1023 if (!sdes_transport_) {
1044 EnableSrtpTransport_n(); 1024 EnableSdes_n();
1045 } 1025 }
1046 if (srtp_transport_) { 1026 if (sdes_transport_) {
1047 srtp_transport_->SetEncryptedHeaderExtensionIds(src, 1027 src == cricket::CS_LOCAL
1048 encrypted_extension_ids); 1028 ? sdes_transport_->SetRecvEncryptedHeaderExtensionIds(
1029 encrypted_extension_ids)
1030 : sdes_transport_->SetSendEncryptedHeaderExtensionIds(
1031 encrypted_extension_ids);
1049 } 1032 }
1033
1034 bool ret = true;
1050 switch (action) { 1035 switch (action) {
1051 case CA_OFFER: 1036 case CA_OFFER:
1052 // If DTLS is already active on the channel, we could be renegotiating 1037 ret = sdes_negotiator_.SetOffer(cryptos, src);
1053 // here. We don't update the srtp filter.
1054 if (!dtls) {
1055 ret = sdes_negotiator_.SetOffer(cryptos, src);
1056 }
1057 break; 1038 break;
1058 case CA_PRANSWER: 1039 case CA_PRANSWER:
1059 // If we're doing DTLS-SRTP, we don't want to update the filter 1040 ret = sdes_negotiator_.SetProvisionalAnswer(cryptos, src);
1060 // with an answer, because we already have SRTP parameters.
1061 if (!dtls) {
1062 ret = sdes_negotiator_.SetProvisionalAnswer(cryptos, src);
1063 }
1064 break; 1041 break;
1065 case CA_ANSWER: 1042 case CA_ANSWER:
1066 // If we're doing DTLS-SRTP, we don't want to update the filter 1043 ret = sdes_negotiator_.SetAnswer(cryptos, src);
1067 // with an answer, because we already have SRTP parameters.
1068 if (!dtls) {
1069 ret = sdes_negotiator_.SetAnswer(cryptos, src);
1070 }
1071 break; 1044 break;
1072 default: 1045 default:
1073 break; 1046 break;
1074 } 1047 }
1075 1048
1076 // If setting an SDES answer succeeded, apply the negotiated parameters 1049 // If setting an SDES answer succeeded, apply the negotiated parameters
1077 // to the SRTP transport. 1050 // to the SRTP transport.
1078 if ((action == CA_PRANSWER || action == CA_ANSWER) && !dtls && ret) { 1051 if ((action == CA_PRANSWER || action == CA_ANSWER) && ret) {
1079 if (sdes_negotiator_.send_cipher_suite() && 1052 if (sdes_negotiator_.send_cipher_suite() &&
1080 sdes_negotiator_.recv_cipher_suite()) { 1053 sdes_negotiator_.recv_cipher_suite()) {
1081 ret = srtp_transport_->SetRtpParams( 1054 ret = sdes_transport_->SetRtpParams(
1082 *(sdes_negotiator_.send_cipher_suite()), 1055 *(sdes_negotiator_.send_cipher_suite()),
1083 sdes_negotiator_.send_key().data(), 1056 sdes_negotiator_.send_key().data(),
1084 static_cast<int>(sdes_negotiator_.send_key().size()), 1057 static_cast<int>(sdes_negotiator_.send_key().size()),
1085 *(sdes_negotiator_.recv_cipher_suite()), 1058 *(sdes_negotiator_.recv_cipher_suite()),
1086 sdes_negotiator_.recv_key().data(), 1059 sdes_negotiator_.recv_key().data(),
1087 static_cast<int>(sdes_negotiator_.recv_key().size())); 1060 static_cast<int>(sdes_negotiator_.recv_key().size()));
1088 } else { 1061 } else {
1089 LOG(LS_INFO) << "No crypto keys are provided for SDES."; 1062 LOG(LS_INFO) << "No crypto keys are provided for SDES.";
1090 if (action == CA_ANSWER && srtp_transport_) { 1063 if (action == CA_ANSWER && sdes_transport_) {
1091 // Explicitly reset the |srtp_transport_| if no crypto param is 1064 // Explicitly reset the |sdes_transport_| if no crypto param is
1092 // provided in the answer. No need to call |ResetParams()| for 1065 // provided in the answer. No need to call |ResetParams()| for
1093 // |sdes_negotiator_| because it resets the params inside |SetAnswer|. 1066 // |sdes_negotiator_| because it resets the params inside |SetAnswer|.
1094 srtp_transport_->ResetParams(); 1067 sdes_transport_->ResetParams();
1095 } 1068 }
1096 } 1069 }
1097 } 1070 }
1071 return ret;
1072 }
1098 1073
1099 // Only update SRTP filter if using DTLS. SDES is handled internally 1074 bool BaseChannel::SetDtlsSrtp_n(
1100 // by the SRTP filter. 1075 ContentSource src,
1101 // TODO(jbauch): Only update if encrypted extension ids have changed. 1076 const std::vector<int>& encrypted_extension_ids) {
1102 if (ret && dtls_active() && rtp_dtls_transport_ && 1077 bool ret = true;
1103 rtp_dtls_transport_->dtls_state() == DTLS_TRANSPORT_CONNECTED) { 1078 if (!dtls_srtp_transport_) {
1079 EnableDtlsSrtp_n();
1080 }
1081 // Update the DtlsSrtpTransport if using DTLS.
1082 // TODO(zhihuang): Only update if encrypted extension ids have changed.
1083 else {
1084 src == cricket::CS_LOCAL
1085 ? dtls_srtp_transport_->SetRecvEncryptedHeaderExtensionIds(
1086 encrypted_extension_ids)
1087 : dtls_srtp_transport_->SetSendEncryptedHeaderExtensionIds(
1088 encrypted_extension_ids);
1089 }
1090
1091 if (dtls_active() && dtls_srtp_transport_->rtp_dtls_transport() &&
1092 dtls_srtp_transport_->rtp_dtls_transport()->dtls_state() ==
1093 DTLS_TRANSPORT_CONNECTED) {
1104 bool rtcp = false; 1094 bool rtcp = false;
1105 ret = SetupDtlsSrtp_n(rtcp); 1095 ret = dtls_srtp_transport_->SetupDtlsSrtp(rtcp);
1106 } 1096 }
1107 if (!ret) { 1097 return ret;
1108 SafeSetError("Failed to setup SRTP filter.", error_desc);
1109 return false;
1110 }
1111 return true;
1112 } 1098 }
1113 1099
1114 bool BaseChannel::SetRtcpMux_n(bool enable, 1100 bool BaseChannel::SetRtcpMux_n(bool enable,
1115 ContentAction action, 1101 ContentAction action,
1116 ContentSource src, 1102 ContentSource src,
1117 std::string* error_desc) { 1103 std::string* error_desc) {
1118 // Provide a more specific error message for the RTCP mux "require" policy 1104 // Provide a more specific error message for the RTCP mux "require" policy
1119 // case. 1105 // case.
1120 if (rtcp_mux_required_ && !enable) { 1106 if (rtcp_mux_required_ && !enable) {
1121 SafeSetError( 1107 SafeSetError(
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
1364 send_time_extension ? send_time_extension->id : -1; 1350 send_time_extension ? send_time_extension->id : -1;
1365 invoker_.AsyncInvoke<void>( 1351 invoker_.AsyncInvoke<void>(
1366 RTC_FROM_HERE, network_thread_, 1352 RTC_FROM_HERE, network_thread_,
1367 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this, 1353 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1368 rtp_abs_sendtime_extn_id)); 1354 rtp_abs_sendtime_extn_id));
1369 #endif 1355 #endif
1370 } 1356 }
1371 1357
1372 void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n( 1358 void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1373 int rtp_abs_sendtime_extn_id) { 1359 int rtp_abs_sendtime_extn_id) {
1374 if (srtp_transport_) { 1360 if (sdes_transport_) {
1375 srtp_transport_->CacheRtpAbsSendTimeHeaderExtension( 1361 sdes_transport_->CacheRtpAbsSendTimeHeaderExtension(
1376 rtp_abs_sendtime_extn_id); 1362 rtp_abs_sendtime_extn_id);
1377 } else { 1363 } else {
1378 LOG(LS_WARNING) << "Trying to cache the Absolute Send Time extension id " 1364 LOG(LS_WARNING) << "Trying to cache the Absolute Send Time extension id "
1379 "but the SRTP is not active."; 1365 "but the SRTP is not active.";
1380 } 1366 }
1381 } 1367 }
1382 1368
1383 void BaseChannel::OnMessage(rtc::Message *pmsg) { 1369 void BaseChannel::OnMessage(rtc::Message *pmsg) {
1384 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage"); 1370 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
1385 switch (pmsg->message_id) { 1371 switch (pmsg->message_id) {
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
1656 constexpr int kUdpOverhaed = 8; 1642 constexpr int kUdpOverhaed = 8;
1657 constexpr int kTcpOverhaed = 20; 1643 constexpr int kTcpOverhaed = 20;
1658 transport_overhead_per_packet += 1644 transport_overhead_per_packet +=
1659 selected_candidate_pair_->local_candidate().protocol() == 1645 selected_candidate_pair_->local_candidate().protocol() ==
1660 TCP_PROTOCOL_NAME 1646 TCP_PROTOCOL_NAME
1661 ? kTcpOverhaed 1647 ? kTcpOverhaed
1662 : kUdpOverhaed; 1648 : kUdpOverhaed;
1663 1649
1664 if (sdes_active()) { 1650 if (sdes_active()) {
1665 int srtp_overhead = 0; 1651 int srtp_overhead = 0;
1666 if (srtp_transport_->GetSrtpOverhead(&srtp_overhead)) 1652 if (sdes_transport_->GetSrtpOverhead(&srtp_overhead))
1667 transport_overhead_per_packet += srtp_overhead; 1653 transport_overhead_per_packet += srtp_overhead;
1668 } 1654 }
1669 1655
1670 return transport_overhead_per_packet; 1656 return transport_overhead_per_packet;
1671 } 1657 }
1672 1658
1673 void BaseChannel::UpdateTransportOverhead() { 1659 void BaseChannel::UpdateTransportOverhead() {
1674 int transport_overhead_per_packet = GetTransportOverheadPerPacket(); 1660 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1675 if (transport_overhead_per_packet) 1661 if (transport_overhead_per_packet)
1676 invoker_.AsyncInvoke<void>( 1662 invoker_.AsyncInvoke<void>(
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
2376 2362
2377 void RtpDataChannel::OnDataChannelReadyToSend(bool writable) { 2363 void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
2378 // This is usded for congestion control to indicate that the stream is ready 2364 // This is usded for congestion control to indicate that the stream is ready
2379 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates 2365 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2380 // that the transport channel is ready. 2366 // that the transport channel is ready.
2381 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA, 2367 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
2382 new DataChannelReadyToSendMessageData(writable)); 2368 new DataChannelReadyToSendMessageData(writable));
2383 } 2369 }
2384 2370
2385 } // namespace cricket 2371 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698