OLD | NEW |
---|---|
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 Loading... | |
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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 bool srtp_required) | 152 bool srtp_required) |
156 : worker_thread_(worker_thread), | 153 : worker_thread_(worker_thread), |
157 network_thread_(network_thread), | 154 network_thread_(network_thread), |
158 signaling_thread_(signaling_thread), | 155 signaling_thread_(signaling_thread), |
159 content_name_(content_name), | 156 content_name_(content_name), |
160 rtcp_mux_required_(rtcp_mux_required), | 157 rtcp_mux_required_(rtcp_mux_required), |
161 srtp_required_(srtp_required), | 158 srtp_required_(srtp_required), |
162 media_channel_(media_channel), | 159 media_channel_(media_channel), |
163 selected_candidate_pair_(nullptr) { | 160 selected_candidate_pair_(nullptr) { |
164 RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); | 161 RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
165 if (srtp_required) { | 162 rtp_transport_ = rtc::MakeUnique<webrtc::RtpTransport>(rtcp_mux_required); |
166 auto transport = | 163 srtp_transport_ = nullptr; |
167 rtc::MakeUnique<webrtc::SrtpTransport>(rtcp_mux_required, content_name); | |
168 srtp_transport_ = transport.get(); | |
169 rtp_transport_ = std::move(transport); | |
170 #if defined(ENABLE_EXTERNAL_AUTH) | |
171 srtp_transport_->EnableExternalAuth(); | |
172 #endif | |
173 } else { | |
174 rtp_transport_ = rtc::MakeUnique<webrtc::RtpTransport>(rtcp_mux_required); | |
175 srtp_transport_ = nullptr; | |
176 } | |
177 rtp_transport_->SignalReadyToSend.connect( | 164 rtp_transport_->SignalReadyToSend.connect( |
178 this, &BaseChannel::OnTransportReadyToSend); | 165 this, &BaseChannel::OnTransportReadyToSend); |
179 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced | 166 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced |
180 // with a callback interface later so that the demuxer can select which | 167 // with a callback interface later so that the demuxer can select which |
181 // channel to signal. | 168 // channel to signal. |
182 rtp_transport_->SignalPacketReceived.connect(this, | 169 rtp_transport_->SignalPacketReceived.connect(this, |
183 &BaseChannel::OnPacketReceived); | 170 &BaseChannel::OnPacketReceived); |
184 LOG(LS_INFO) << "Created channel for " << content_name; | 171 LOG(LS_INFO) << "Created channel for " << content_name; |
185 } | 172 } |
186 | 173 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 debug_name = rtp_packet_transport->debug_name(); | 301 debug_name = rtp_packet_transport->debug_name(); |
315 } | 302 } |
316 if (rtp_packet_transport == rtp_transport_->rtp_packet_transport()) { | 303 if (rtp_packet_transport == rtp_transport_->rtp_packet_transport()) { |
317 // Nothing to do if transport isn't changing. | 304 // Nothing to do if transport isn't changing. |
318 return; | 305 return; |
319 } | 306 } |
320 | 307 |
321 // When using DTLS-SRTP, we must reset the SrtpTransport every time the | 308 // When using DTLS-SRTP, we must reset the SrtpTransport every time the |
322 // DtlsTransport changes and wait until the DTLS handshake is complete to set | 309 // DtlsTransport changes and wait until the DTLS handshake is complete to set |
323 // the newly negotiated parameters. | 310 // the newly negotiated parameters. |
324 if (ShouldSetupDtlsSrtp_n()) { | 311 if (rtp_dtls_transport && rtp_dtls_transport->IsDtlsActive()) { |
325 // Set |writable_| to false such that UpdateWritableState_w can set up | 312 // Set |writable_| to false such that UpdateWritableState_w can set up |
326 // DTLS-SRTP when |writable_| becomes true again. | 313 // DTLS-SRTP when |writable_| becomes true again. |
327 writable_ = false; | 314 writable_ = false; |
328 dtls_active_ = false; | 315 if (dtls_srtp_transport_) { |
329 if (srtp_transport_) { | 316 dtls_srtp_transport_->ResetParams(); |
330 srtp_transport_->ResetParams(); | |
331 } | 317 } |
332 } | 318 } |
333 | 319 |
334 // If this BaseChannel doesn't require RTCP mux and we haven't fully | 320 // If this BaseChannel doesn't require RTCP mux and we haven't fully |
335 // negotiated RTCP mux, we need an RTCP transport. | 321 // negotiated RTCP mux, we need an RTCP transport. |
336 if (rtcp_packet_transport) { | 322 if (rtcp_packet_transport) { |
337 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on " | 323 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on " |
338 << debug_name << " transport " << rtcp_packet_transport; | 324 << debug_name << " transport " << rtcp_packet_transport; |
339 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport); | 325 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport); |
340 } | 326 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 } | 370 } |
385 | 371 |
386 if (rtcp && new_dtls_transport) { | 372 if (rtcp && new_dtls_transport) { |
387 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_active())) | 373 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_active())) |
388 << "Setting RTCP for DTLS/SRTP after the DTLS is active " | 374 << "Setting RTCP for DTLS/SRTP after the DTLS is active " |
389 << "should never happen."; | 375 << "should never happen."; |
390 } | 376 } |
391 | 377 |
392 if (new_dtls_transport) { | 378 if (new_dtls_transport) { |
393 ConnectToDtlsTransport(new_dtls_transport); | 379 ConnectToDtlsTransport(new_dtls_transport); |
380 if (dtls_srtp_transport_) { | |
381 dtls_srtp_transport_->SetDtlsTransport(rtcp, new_dtls_transport); | |
382 } | |
394 } else { | 383 } else { |
395 ConnectToPacketTransport(new_packet_transport); | 384 ConnectToPacketTransport(new_packet_transport); |
396 } | 385 } |
397 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_; | 386 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_; |
398 for (const auto& pair : socket_options) { | 387 for (const auto& pair : socket_options) { |
399 new_packet_transport->SetOption(pair.first, pair.second); | 388 new_packet_transport->SetOption(pair.first, pair.second); |
400 } | 389 } |
401 } | 390 } |
402 | 391 |
403 void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) { | 392 void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) { |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
587 if (!ShouldSetupDtlsSrtp_n()) { | 576 if (!ShouldSetupDtlsSrtp_n()) { |
588 return; | 577 return; |
589 } | 578 } |
590 | 579 |
591 // Reset the SrtpTransport if it's not the CONNECTED state. For the CONNECTED | 580 // Reset the SrtpTransport if it's not the CONNECTED state. For the CONNECTED |
592 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to | 581 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to |
593 // cover other scenarios like the whole transport is writable (not just this | 582 // cover other scenarios like the whole transport is writable (not just this |
594 // TransportChannel) or when TransportChannel is attached after DTLS is | 583 // TransportChannel) or when TransportChannel is attached after DTLS is |
595 // negotiated. | 584 // negotiated. |
596 if (state != DTLS_TRANSPORT_CONNECTED) { | 585 if (state != DTLS_TRANSPORT_CONNECTED) { |
597 dtls_active_ = false; | 586 if (dtls_srtp_transport_) { |
598 if (srtp_transport_) { | 587 dtls_srtp_transport_->ResetParams(); |
599 srtp_transport_->ResetParams(); | |
600 } | 588 } |
601 } | 589 } |
602 } | 590 } |
603 | 591 |
604 void BaseChannel::OnSelectedCandidatePairChanged( | 592 void BaseChannel::OnSelectedCandidatePairChanged( |
605 IceTransportInternal* ice_transport, | 593 IceTransportInternal* ice_transport, |
606 CandidatePairInterface* selected_candidate_pair, | 594 CandidatePairInterface* selected_candidate_pair, |
607 int last_sent_packet_id, | 595 int last_sent_packet_id, |
608 bool ready_to_send) { | 596 bool ready_to_send) { |
609 RTC_DCHECK((rtp_dtls_transport_ && | 597 RTC_DCHECK((rtp_dtls_transport_ && |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
683 // (and SetSend(true) is called). | 671 // (and SetSend(true) is called). |
684 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive" | 672 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive" |
685 << " and crypto is required"; | 673 << " and crypto is required"; |
686 RTC_NOTREACHED(); | 674 RTC_NOTREACHED(); |
687 return false; | 675 return false; |
688 } | 676 } |
689 // Bon voyage. | 677 // Bon voyage. |
690 return rtcp ? rtp_transport_->SendRtcpPacket(packet, options, PF_NORMAL) | 678 return rtcp ? rtp_transport_->SendRtcpPacket(packet, options, PF_NORMAL) |
691 : rtp_transport_->SendRtpPacket(packet, options, PF_NORMAL); | 679 : rtp_transport_->SendRtpPacket(packet, options, PF_NORMAL); |
692 } | 680 } |
681 | |
682 if (dtls_srtp_transport_) { | |
683 RTC_DCHECK(!srtp_transport_); | |
684 RTC_DCHECK(dtls_srtp_transport_->IsActive()); | |
685 // Bon voyage. | |
686 return rtcp ? dtls_srtp_transport_->SendRtcpPacket(packet, options, | |
687 PF_SRTP_BYPASS) | |
688 : dtls_srtp_transport_->SendRtpPacket(packet, options, | |
689 PF_SRTP_BYPASS); | |
690 } | |
693 RTC_DCHECK(srtp_transport_); | 691 RTC_DCHECK(srtp_transport_); |
694 RTC_DCHECK(srtp_transport_->IsActive()); | 692 RTC_DCHECK(srtp_transport_->IsActive()); |
695 // Bon voyage. | 693 // Bon voyage. |
696 return rtcp ? srtp_transport_->SendRtcpPacket(packet, options, PF_SRTP_BYPASS) | 694 return rtcp ? srtp_transport_->SendRtcpPacket(packet, options, PF_SRTP_BYPASS) |
697 : srtp_transport_->SendRtpPacket(packet, options, PF_SRTP_BYPASS); | 695 : srtp_transport_->SendRtpPacket(packet, options, PF_SRTP_BYPASS); |
698 } | 696 } |
699 | 697 |
700 bool BaseChannel::HandlesPayloadType(int packet_type) const { | 698 bool BaseChannel::HandlesPayloadType(int packet_type) const { |
701 return rtp_transport_->HandlesPayloadType(packet_type); | 699 return rtp_transport_->HandlesPayloadType(packet_type); |
702 } | 700 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
839 void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) { | 837 void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) { |
840 RTC_DCHECK(signaling_thread() == rtc::Thread::Current()); | 838 RTC_DCHECK(signaling_thread() == rtc::Thread::Current()); |
841 SignalDtlsSrtpSetupFailure(this, rtcp); | 839 SignalDtlsSrtpSetupFailure(this, rtcp); |
842 } | 840 } |
843 | 841 |
844 bool BaseChannel::ShouldSetupDtlsSrtp_n() const { | 842 bool BaseChannel::ShouldSetupDtlsSrtp_n() const { |
845 // Since DTLS is applied to all transports, checking RTP should be enough. | 843 // Since DTLS is applied to all transports, checking RTP should be enough. |
846 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive(); | 844 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive(); |
847 } | 845 } |
848 | 846 |
849 // This function returns true if either DTLS-SRTP is not in use | |
850 // *or* DTLS-SRTP is successfully set up. | |
851 bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) { | |
852 RTC_DCHECK(network_thread_->IsCurrent()); | |
853 bool ret = false; | |
854 | |
855 DtlsTransportInternal* transport = | |
856 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_; | |
857 RTC_DCHECK(transport); | |
858 RTC_DCHECK(transport->IsDtlsActive()); | |
859 | |
860 int selected_crypto_suite; | |
861 | |
862 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) { | |
863 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite"; | |
864 return false; | |
865 } | |
866 | |
867 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " " | |
868 << RtpRtcpStringLiteral(rtcp); | |
869 | |
870 int key_len; | |
871 int salt_len; | |
872 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len, | |
873 &salt_len)) { | |
874 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite; | |
875 return false; | |
876 } | |
877 | |
878 // OK, we're now doing DTLS (RFC 5764) | |
879 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2); | |
880 | |
881 // RFC 5705 exporter using the RFC 5764 parameters | |
882 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false, | |
883 &dtls_buffer[0], dtls_buffer.size())) { | |
884 LOG(LS_WARNING) << "DTLS-SRTP key export failed"; | |
885 RTC_NOTREACHED(); // This should never happen | |
886 return false; | |
887 } | |
888 | |
889 // Sync up the keys with the DTLS-SRTP interface | |
890 std::vector<unsigned char> client_write_key(key_len + salt_len); | |
891 std::vector<unsigned char> server_write_key(key_len + salt_len); | |
892 size_t offset = 0; | |
893 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len); | |
894 offset += key_len; | |
895 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len); | |
896 offset += key_len; | |
897 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len); | |
898 offset += salt_len; | |
899 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len); | |
900 | |
901 std::vector<unsigned char> *send_key, *recv_key; | |
902 rtc::SSLRole role; | |
903 if (!transport->GetSslRole(&role)) { | |
904 LOG(LS_WARNING) << "GetSslRole failed"; | |
905 return false; | |
906 } | |
907 | |
908 if (role == rtc::SSL_SERVER) { | |
909 send_key = &server_write_key; | |
910 recv_key = &client_write_key; | |
911 } else { | |
912 send_key = &client_write_key; | |
913 recv_key = &server_write_key; | |
914 } | |
915 | |
916 if (rtcp) { | |
917 if (!dtls_active()) { | |
918 RTC_DCHECK(srtp_transport_); | |
919 ret = srtp_transport_->SetRtcpParams( | |
920 selected_crypto_suite, &(*send_key)[0], | |
921 static_cast<int>(send_key->size()), selected_crypto_suite, | |
922 &(*recv_key)[0], static_cast<int>(recv_key->size())); | |
923 } else { | |
924 // RTCP doesn't need to call SetRtpParam because it is only used | |
925 // to make the updated encrypted RTP header extension IDs take effect. | |
926 ret = true; | |
927 } | |
928 } else { | |
929 RTC_DCHECK(srtp_transport_); | |
930 ret = srtp_transport_->SetRtpParams(selected_crypto_suite, &(*send_key)[0], | |
931 static_cast<int>(send_key->size()), | |
932 selected_crypto_suite, &(*recv_key)[0], | |
933 static_cast<int>(recv_key->size())); | |
934 dtls_active_ = ret; | |
935 } | |
936 | |
937 if (!ret) { | |
938 LOG(LS_WARNING) << "DTLS-SRTP key installation failed"; | |
939 } else { | |
940 UpdateTransportOverhead(); | |
941 } | |
942 return ret; | |
943 } | |
944 | |
945 void BaseChannel::MaybeSetupDtlsSrtp_n() { | 847 void BaseChannel::MaybeSetupDtlsSrtp_n() { |
946 if (dtls_active()) { | 848 if (dtls_active()) { |
947 return; | 849 return; |
948 } | 850 } |
949 | 851 |
950 if (!ShouldSetupDtlsSrtp_n()) { | 852 if (!ShouldSetupDtlsSrtp_n()) { |
951 return; | 853 return; |
952 } | 854 } |
953 | 855 |
954 if (!srtp_transport_) { | 856 if (!dtls_srtp_transport_) { |
955 EnableSrtpTransport_n(); | 857 EnableDtlsSrtpTransport_n(); |
956 } | 858 } |
957 | 859 |
958 if (!SetupDtlsSrtp_n(false)) { | 860 if (!dtls_srtp_transport_->SetupDtlsSrtp(false)) { |
959 SignalDtlsSrtpSetupFailure_n(false); | 861 SignalDtlsSrtpSetupFailure_n(false); |
960 return; | 862 return; |
961 } | 863 } |
962 | 864 |
963 if (rtcp_dtls_transport_) { | 865 if (rtcp_dtls_transport_) { |
964 if (!SetupDtlsSrtp_n(true)) { | 866 if (!dtls_srtp_transport_->SetupDtlsSrtp(true)) { |
965 SignalDtlsSrtpSetupFailure_n(true); | 867 SignalDtlsSrtpSetupFailure_n(true); |
966 return; | 868 return; |
967 } | 869 } |
968 } | 870 } |
969 } | 871 } |
970 | 872 |
971 void BaseChannel::ChannelNotWritable_n() { | 873 void BaseChannel::ChannelNotWritable_n() { |
972 RTC_DCHECK(network_thread_->IsCurrent()); | 874 RTC_DCHECK(network_thread_->IsCurrent()); |
973 if (!writable_) | 875 if (!writable_) |
974 return; | 876 return; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1032 std::string* error_desc) { | 934 std::string* error_desc) { |
1033 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive(); | 935 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive(); |
1034 if (*dtls && !cryptos.empty()) { | 936 if (*dtls && !cryptos.empty()) { |
1035 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc); | 937 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc); |
1036 return false; | 938 return false; |
1037 } | 939 } |
1038 return true; | 940 return true; |
1039 } | 941 } |
1040 | 942 |
1041 void BaseChannel::EnableSrtpTransport_n() { | 943 void BaseChannel::EnableSrtpTransport_n() { |
1042 if (srtp_transport_ == nullptr) { | 944 if (!srtp_transport_) { |
pthatcher
2017/09/12 00:24:44
This could use an early return.
Zhi Huang
2017/09/27 00:46:32
Done.
| |
945 // DtlsSrtpTransport and SrtpTransport shouldn't be enabled at the same | |
946 // time. | |
pthatcher
2017/09/12 00:24:43
If SrtpTransport is really just for SDES, I think
Zhi Huang
2017/09/27 00:46:32
Done.
| |
947 RTC_DCHECK(!dtls_srtp_transport_); | |
1043 rtp_transport_->SignalReadyToSend.disconnect(this); | 948 rtp_transport_->SignalReadyToSend.disconnect(this); |
1044 rtp_transport_->SignalPacketReceived.disconnect(this); | 949 rtp_transport_->SignalPacketReceived.disconnect(this); |
1045 | 950 |
1046 auto transport = rtc::MakeUnique<webrtc::SrtpTransport>( | 951 auto transport = rtc::MakeUnique<webrtc::SrtpTransport>( |
1047 std::move(rtp_transport_), content_name_); | 952 std::move(rtp_transport_), content_name_); |
1048 srtp_transport_ = transport.get(); | 953 srtp_transport_ = transport.get(); |
954 #if defined(ENABLE_EXTERNAL_AUTH) | |
955 srtp_transport_->EnableExternalAuth(); | |
956 #endif | |
1049 rtp_transport_ = std::move(transport); | 957 rtp_transport_ = std::move(transport); |
1050 | 958 |
1051 rtp_transport_->SignalReadyToSend.connect( | 959 rtp_transport_->SignalReadyToSend.connect( |
1052 this, &BaseChannel::OnTransportReadyToSend); | 960 this, &BaseChannel::OnTransportReadyToSend); |
1053 rtp_transport_->SignalPacketReceived.connect( | 961 rtp_transport_->SignalPacketReceived.connect( |
1054 this, &BaseChannel::OnPacketReceived); | 962 this, &BaseChannel::OnPacketReceived); |
1055 LOG(LS_INFO) << "Wrapping RtpTransport in SrtpTransport."; | 963 LOG(LS_INFO) << "Wrapping RtpTransport in SrtpTransport."; |
1056 } | 964 } |
1057 } | 965 } |
1058 | 966 |
967 void BaseChannel::EnableDtlsSrtpTransport_n() { | |
pthatcher
2017/09/12 00:24:43
This could just be called EnableDtlsSrtp_n().
Zhi Huang
2017/09/27 00:46:32
Done.
| |
968 if (!dtls_srtp_transport_) { | |
pthatcher
2017/09/12 00:24:43
This could use an early return.
Zhi Huang
2017/09/27 00:46:32
Done.
| |
969 // DtlsSrtpTransport and SrtpTransport shouldn't be enabled at the same | |
970 // time. | |
971 RTC_DCHECK(!srtp_transport_); | |
972 rtp_transport_->SignalReadyToSend.disconnect(this); | |
973 rtp_transport_->SignalPacketReceived.disconnect(this); | |
974 | |
975 std::unique_ptr<webrtc::SrtpTransport> srtp_transport = | |
976 rtc::MakeUnique<webrtc::SrtpTransport>(std::move(rtp_transport_), | |
977 content_name_); | |
978 srtp_transport->SetEncryptedHeaderExtensionIds( | |
979 cricket::CS_LOCAL, recv_encrypted_header_extension_ids_); | |
980 srtp_transport->SetEncryptedHeaderExtensionIds( | |
981 cricket::CS_REMOTE, send_encrypted_header_extension_ids_); | |
982 #if defined(ENABLE_EXTERNAL_AUTH) | |
983 srtp_transport->EnableExternalAuth(); | |
984 #endif | |
985 std::unique_ptr<webrtc::DtlsSrtpTransport> transport = | |
986 rtc::MakeUnique<webrtc::DtlsSrtpTransport>(std::move(srtp_transport), | |
987 content_name_); | |
988 dtls_srtp_transport_ = transport.get(); | |
989 dtls_srtp_transport_->SetDtlsTransport(true, rtcp_dtls_transport_); | |
990 dtls_srtp_transport_->SetDtlsTransport(false, rtp_dtls_transport_); | |
991 | |
992 rtp_transport_ = std::move(transport); | |
993 rtp_transport_->SignalReadyToSend.connect( | |
994 this, &BaseChannel::OnTransportReadyToSend); | |
995 rtp_transport_->SignalPacketReceived.connect( | |
996 this, &BaseChannel::OnPacketReceived); | |
997 LOG(LS_INFO) << "Wrapping SrtpTransport in DtlsSrtpTransport."; | |
998 } | |
999 } | |
1000 | |
1001 void BaseChannel::CacheEncryptedHeaderExtensionIds( | |
1002 cricket::ContentSource source, | |
1003 const std::vector<int>& extension_ids) { | |
1004 if (source == cricket::CS_LOCAL) { | |
1005 recv_encrypted_header_extension_ids_ = extension_ids; | |
1006 } else { | |
1007 send_encrypted_header_extension_ids_ = extension_ids; | |
1008 } | |
1009 } | |
1010 | |
1059 bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos, | 1011 bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos, |
1060 ContentAction action, | 1012 ContentAction action, |
1061 ContentSource src, | 1013 ContentSource src, |
1062 const std::vector<int>& encrypted_extension_ids, | 1014 const std::vector<int>& encrypted_extension_ids, |
1063 std::string* error_desc) { | 1015 std::string* error_desc) { |
1064 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w"); | 1016 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w"); |
1065 if (action == CA_UPDATE) { | 1017 if (action == CA_UPDATE) { |
1066 // no crypto params. | 1018 // no crypto params. |
1067 return true; | 1019 return true; |
1068 } | 1020 } |
1069 bool ret = false; | 1021 bool ret = false; |
1070 bool dtls = false; | 1022 bool dtls = false; |
1071 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc); | 1023 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc); |
1072 if (!ret) { | 1024 if (!ret) { |
1073 return false; | 1025 return false; |
1074 } | 1026 } |
1027 // Cache the encrypted extension ids. This is needed because |SetSrtp_n| could | |
1028 // happen before |dtls| is true and DtlsSrtpTransport is enabled. | |
1029 CacheEncryptedHeaderExtensionIds(src, encrypted_extension_ids); | |
1075 | 1030 |
1076 // If SRTP was not required, but we're setting a description that uses SDES, | 1031 if (!dtls) { |
1077 // we need to upgrade to an SrtpTransport. | 1032 // If SRTP was not required, but we're setting a description that uses SDES, |
1078 if (!srtp_transport_ && !dtls && !cryptos.empty()) { | 1033 // we need to upgrade to an SrtpTransport. |
1079 EnableSrtpTransport_n(); | 1034 if (!srtp_transport_ && !cryptos.empty()) { |
1080 } | 1035 EnableSrtpTransport_n(); |
1081 if (srtp_transport_) { | 1036 } |
1082 srtp_transport_->SetEncryptedHeaderExtensionIds(src, | 1037 if (srtp_transport_) { |
1083 encrypted_extension_ids); | 1038 srtp_transport_->SetEncryptedHeaderExtensionIds(src, |
1084 } | 1039 encrypted_extension_ids); |
1085 switch (action) { | 1040 } |
1086 case CA_OFFER: | 1041 switch (action) { |
1087 // If DTLS is already active on the channel, we could be renegotiating | 1042 case CA_OFFER: |
1088 // here. We don't update the srtp filter. | |
1089 if (!dtls) { | |
1090 ret = sdes_negotiator_.SetOffer(cryptos, src); | 1043 ret = sdes_negotiator_.SetOffer(cryptos, src); |
1044 break; | |
1045 case CA_PRANSWER: | |
1046 ret = sdes_negotiator_.SetProvisionalAnswer(cryptos, src); | |
1047 break; | |
1048 case CA_ANSWER: | |
1049 ret = sdes_negotiator_.SetAnswer(cryptos, src); | |
1050 break; | |
1051 default: | |
1052 break; | |
1053 } | |
1054 // If setting an SDES answer succeeded, apply the negotiated parameters | |
1055 // to the SRTP transport. | |
1056 if ((action == CA_PRANSWER || action == CA_ANSWER) && ret) { | |
1057 if (sdes_negotiator_.send_cipher_suite() && | |
1058 sdes_negotiator_.recv_cipher_suite()) { | |
1059 ret = srtp_transport_->SetRtpParams( | |
1060 *(sdes_negotiator_.send_cipher_suite()), | |
1061 sdes_negotiator_.send_key().data(), | |
1062 static_cast<int>(sdes_negotiator_.send_key().size()), | |
1063 *(sdes_negotiator_.recv_cipher_suite()), | |
1064 sdes_negotiator_.recv_key().data(), | |
1065 static_cast<int>(sdes_negotiator_.recv_key().size())); | |
1066 } else { | |
1067 LOG(LS_INFO) << "No crypto keys are provided for SDES."; | |
1068 if (action == CA_ANSWER && srtp_transport_) { | |
1069 // Explicitly reset the |srtp_transport_| if no crypto param is | |
1070 // provided in the answer. No need to call |ResetParams()| for | |
1071 // |sdes_negotiator_| because it resets the params inside |SetAnswer|. | |
1072 srtp_transport_->ResetParams(); | |
1073 } | |
1091 } | 1074 } |
1092 break; | 1075 } |
1093 case CA_PRANSWER: | 1076 } else { |
1094 // If we're doing DTLS-SRTP, we don't want to update the filter | 1077 // Update the DtlsSrtpTransport if using DTLS. |
1095 // with an answer, because we already have SRTP parameters. | 1078 // TODO(jbauch): Only update if encrypted extension ids have changed. |
pthatcher
2017/09/12 00:24:44
Shouldn't this comment be moved down into the "els
Zhi Huang
2017/09/27 00:46:32
Done.
| |
1096 if (!dtls) { | 1079 if (!dtls_srtp_transport_) { |
1097 ret = sdes_negotiator_.SetProvisionalAnswer(cryptos, src); | 1080 EnableDtlsSrtpTransport_n(); |
1098 } | |
1099 break; | |
1100 case CA_ANSWER: | |
1101 // If we're doing DTLS-SRTP, we don't want to update the filter | |
1102 // with an answer, because we already have SRTP parameters. | |
1103 if (!dtls) { | |
1104 ret = sdes_negotiator_.SetAnswer(cryptos, src); | |
1105 } | |
1106 break; | |
1107 default: | |
1108 break; | |
1109 } | |
1110 | |
1111 // If setting an SDES answer succeeded, apply the negotiated parameters | |
1112 // to the SRTP transport. | |
1113 if ((action == CA_PRANSWER || action == CA_ANSWER) && !dtls && ret) { | |
1114 if (sdes_negotiator_.send_cipher_suite() && | |
1115 sdes_negotiator_.recv_cipher_suite()) { | |
1116 ret = srtp_transport_->SetRtpParams( | |
1117 *(sdes_negotiator_.send_cipher_suite()), | |
1118 sdes_negotiator_.send_key().data(), | |
1119 static_cast<int>(sdes_negotiator_.send_key().size()), | |
1120 *(sdes_negotiator_.recv_cipher_suite()), | |
1121 sdes_negotiator_.recv_key().data(), | |
1122 static_cast<int>(sdes_negotiator_.recv_key().size())); | |
1123 } else { | 1081 } else { |
1124 LOG(LS_INFO) << "No crypto keys are provided for SDES."; | 1082 dtls_srtp_transport_->SetEncryptedHeaderExtensionIds( |
1125 if (action == CA_ANSWER && srtp_transport_) { | 1083 src, encrypted_extension_ids); |
1126 // Explicitly reset the |srtp_transport_| if no crypto param is | 1084 } |
1127 // provided in the answer. No need to call |ResetParams()| for | 1085 if (dtls_active() && dtls_srtp_transport_->rtp_dtls_transport() && |
1128 // |sdes_negotiator_| because it resets the params inside |SetAnswer|. | 1086 dtls_srtp_transport_->rtp_dtls_transport()->dtls_state() == |
1129 srtp_transport_->ResetParams(); | 1087 DTLS_TRANSPORT_CONNECTED) { |
1130 } | 1088 bool rtcp = false; |
1089 ret = dtls_srtp_transport_->SetupDtlsSrtp(rtcp); | |
1131 } | 1090 } |
1132 } | 1091 } |
1133 | 1092 |
1134 // Only update SRTP filter if using DTLS. SDES is handled internally | |
1135 // by the SRTP filter. | |
1136 // TODO(jbauch): Only update if encrypted extension ids have changed. | |
1137 if (ret && dtls_active() && rtp_dtls_transport_ && | |
1138 rtp_dtls_transport_->dtls_state() == DTLS_TRANSPORT_CONNECTED) { | |
1139 bool rtcp = false; | |
1140 ret = SetupDtlsSrtp_n(rtcp); | |
1141 } | |
1142 if (!ret) { | 1093 if (!ret) { |
1143 SafeSetError("Failed to setup SRTP filter.", error_desc); | 1094 SafeSetError("Failed to setup SRTP transport.", error_desc); |
pthatcher
2017/09/12 00:24:44
You could just say "Failed to setup SRTP".
Zhi Huang
2017/09/27 00:46:32
Done.
| |
1144 return false; | 1095 return false; |
1145 } | 1096 } |
1146 return true; | 1097 return true; |
1147 } | 1098 } |
1148 | 1099 |
1149 bool BaseChannel::SetRtcpMux_n(bool enable, | 1100 bool BaseChannel::SetRtcpMux_n(bool enable, |
1150 ContentAction action, | 1101 ContentAction action, |
1151 ContentSource src, | 1102 ContentSource src, |
1152 std::string* error_desc) { | 1103 std::string* error_desc) { |
1153 // 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 |
(...skipping 1272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2426 | 2377 |
2427 void RtpDataChannel::OnDataChannelReadyToSend(bool writable) { | 2378 void RtpDataChannel::OnDataChannelReadyToSend(bool writable) { |
2428 // This is usded for congestion control to indicate that the stream is ready | 2379 // This is usded for congestion control to indicate that the stream is ready |
2429 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates | 2380 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates |
2430 // that the transport channel is ready. | 2381 // that the transport channel is ready. |
2431 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA, | 2382 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA, |
2432 new DataChannelReadyToSendMessageData(writable)); | 2383 new DataChannelReadyToSendMessageData(writable)); |
2433 } | 2384 } |
2434 | 2385 |
2435 } // namespace cricket | 2386 } // namespace cricket |
OLD | NEW |