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

Side by Side Diff: pc/channel.cc

Issue 3018513002: Revert of Completed the functionalities of SrtpTransport. (Closed)
Patch Set: Created 3 years, 3 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
« no previous file with comments | « pc/channel.h ('k') | pc/channel_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 rtc::Thread* signaling_thread, 144 rtc::Thread* signaling_thread,
145 MediaChannel* media_channel, 145 MediaChannel* media_channel,
146 const std::string& content_name, 146 const std::string& content_name,
147 bool rtcp_mux_required, 147 bool rtcp_mux_required,
148 bool srtp_required) 148 bool srtp_required)
149 : worker_thread_(worker_thread), 149 : worker_thread_(worker_thread),
150 network_thread_(network_thread), 150 network_thread_(network_thread),
151 signaling_thread_(signaling_thread), 151 signaling_thread_(signaling_thread),
152 content_name_(content_name), 152 content_name_(content_name),
153 rtcp_mux_required_(rtcp_mux_required), 153 rtcp_mux_required_(rtcp_mux_required),
154 rtp_transport_(
155 srtp_required
156 ? rtc::WrapUnique<webrtc::RtpTransportInternal>(
157 new webrtc::SrtpTransport(rtcp_mux_required, content_name))
158 : rtc::MakeUnique<webrtc::RtpTransport>(rtcp_mux_required)),
154 srtp_required_(srtp_required), 159 srtp_required_(srtp_required),
155 media_channel_(media_channel), 160 media_channel_(media_channel),
156 selected_candidate_pair_(nullptr) { 161 selected_candidate_pair_(nullptr) {
157 RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); 162 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
158 if (srtp_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) 163 #if defined(ENABLE_EXTERNAL_AUTH)
164 srtp_transport_->EnableExternalAuth(); 164 srtp_filter_.EnableExternalAuth();
165 #endif 165 #endif
166 } else {
167 rtp_transport_ = rtc::MakeUnique<webrtc::RtpTransport>(rtcp_mux_required);
168 srtp_transport_ = nullptr;
169 }
170 rtp_transport_->SignalReadyToSend.connect( 166 rtp_transport_->SignalReadyToSend.connect(
171 this, &BaseChannel::OnTransportReadyToSend); 167 this, &BaseChannel::OnTransportReadyToSend);
172 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced 168 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced
173 // with a callback interface later so that the demuxer can select which 169 // with a callback interface later so that the demuxer can select which
174 // channel to signal. 170 // channel to signal.
175 rtp_transport_->SignalPacketReceived.connect(this, 171 rtp_transport_->SignalPacketReceived.connect(this,
176 &BaseChannel::OnPacketReceived); 172 &BaseChannel::OnPacketReceived);
177 LOG(LS_INFO) << "Created channel for " << content_name; 173 LOG(LS_INFO) << "Created channel for " << content_name;
178 } 174 }
179 175
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 transport_name_ = rtp_dtls_transport->transport_name(); 300 transport_name_ = rtp_dtls_transport->transport_name();
305 debug_name = transport_name_; 301 debug_name = transport_name_;
306 } else { 302 } else {
307 debug_name = rtp_packet_transport->debug_name(); 303 debug_name = rtp_packet_transport->debug_name();
308 } 304 }
309 if (rtp_packet_transport == rtp_transport_->rtp_packet_transport()) { 305 if (rtp_packet_transport == rtp_transport_->rtp_packet_transport()) {
310 // Nothing to do if transport isn't changing. 306 // Nothing to do if transport isn't changing.
311 return; 307 return;
312 } 308 }
313 309
314 // When using DTLS-SRTP, we must reset the SrtpTransport every time the 310 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
315 // DtlsTransport changes and wait until the DTLS handshake is complete to set 311 // changes and wait until the DTLS handshake is complete to set the newly
316 // the newly negotiated parameters. 312 // negotiated parameters.
317 if (ShouldSetupDtlsSrtp_n()) { 313 if (ShouldSetupDtlsSrtp_n()) {
318 // Set |writable_| to false such that UpdateWritableState_w can set up 314 // Set |writable_| to false such that UpdateWritableState_w can set up
319 // DTLS-SRTP when |writable_| becomes true again. 315 // DTLS-SRTP when |writable_| becomes true again.
320 writable_ = false; 316 writable_ = false;
321 dtls_active_ = false; 317 srtp_filter_.ResetParams();
322 if (srtp_transport_) {
323 srtp_transport_->ResetParams();
324 }
325 } 318 }
326 319
327 // 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
328 // negotiated RTCP mux, we need an RTCP transport. 321 // negotiated RTCP mux, we need an RTCP transport.
329 if (rtcp_packet_transport) { 322 if (rtcp_packet_transport) {
330 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on " 323 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
331 << debug_name << " transport " << rtcp_packet_transport; 324 << debug_name << " transport " << rtcp_packet_transport;
332 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport); 325 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport);
333 } 326 }
334 327
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 rtp_transport_->SetRtpPacketTransport(new_packet_transport); 363 rtp_transport_->SetRtpPacketTransport(new_packet_transport);
371 } 364 }
372 old_dtls_transport = new_dtls_transport; 365 old_dtls_transport = new_dtls_transport;
373 366
374 // If there's no new transport, we're done after disconnecting from old one. 367 // If there's no new transport, we're done after disconnecting from old one.
375 if (!new_packet_transport) { 368 if (!new_packet_transport) {
376 return; 369 return;
377 } 370 }
378 371
379 if (rtcp && new_dtls_transport) { 372 if (rtcp && new_dtls_transport) {
380 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_active())) 373 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
381 << "Setting RTCP for DTLS/SRTP after the DTLS is active " 374 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
382 << "should never happen."; 375 << "should never happen.";
383 } 376 }
384 377
385 if (new_dtls_transport) { 378 if (new_dtls_transport) {
386 ConnectToDtlsTransport(new_dtls_transport); 379 ConnectToDtlsTransport(new_dtls_transport);
387 } else { 380 } else {
388 ConnectToPacketTransport(new_packet_transport); 381 ConnectToPacketTransport(new_packet_transport);
389 } 382 }
390 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_; 383 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
391 for (const auto& pair : socket_options) { 384 for (const auto& pair : socket_options) {
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 // Need to access some state updated on the network thread. 515 // Need to access some state updated on the network thread.
523 return network_thread_->Invoke<bool>( 516 return network_thread_->Invoke<bool>(
524 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this)); 517 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
525 } 518 }
526 519
527 bool BaseChannel::IsReadyToSendMedia_n() const { 520 bool BaseChannel::IsReadyToSendMedia_n() const {
528 // Send outgoing data if we are enabled, have local and remote content, 521 // Send outgoing data if we are enabled, have local and remote content,
529 // and we have had some form of connectivity. 522 // and we have had some form of connectivity.
530 return enabled() && IsReceiveContentDirection(remote_content_direction_) && 523 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
531 IsSendContentDirection(local_content_direction_) && 524 IsSendContentDirection(local_content_direction_) &&
532 was_ever_writable() && (srtp_active() || !ShouldSetupDtlsSrtp_n()); 525 was_ever_writable() &&
526 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
533 } 527 }
534 528
535 bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet, 529 bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
536 const rtc::PacketOptions& options) { 530 const rtc::PacketOptions& options) {
537 return SendPacket(false, packet, options); 531 return SendPacket(false, packet, options);
538 } 532 }
539 533
540 bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet, 534 bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
541 const rtc::PacketOptions& options) { 535 const rtc::PacketOptions& options) {
542 return SendPacket(true, packet, options); 536 return SendPacket(true, packet, options);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 RTC_DCHECK(network_thread_->IsCurrent()); 568 RTC_DCHECK(network_thread_->IsCurrent());
575 UpdateWritableState_n(); 569 UpdateWritableState_n();
576 } 570 }
577 571
578 void BaseChannel::OnDtlsState(DtlsTransportInternal* transport, 572 void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
579 DtlsTransportState state) { 573 DtlsTransportState state) {
580 if (!ShouldSetupDtlsSrtp_n()) { 574 if (!ShouldSetupDtlsSrtp_n()) {
581 return; 575 return;
582 } 576 }
583 577
584 // Reset the SrtpTransport if it's not the CONNECTED state. For the CONNECTED 578 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
585 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to 579 // 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 580 // cover other scenarios like the whole transport is writable (not just this
587 // TransportChannel) or when TransportChannel is attached after DTLS is 581 // TransportChannel) or when TransportChannel is attached after DTLS is
588 // negotiated. 582 // negotiated.
589 if (state != DTLS_TRANSPORT_CONNECTED) { 583 if (state != DTLS_TRANSPORT_CONNECTED) {
590 dtls_active_ = false; 584 srtp_filter_.ResetParams();
591 if (srtp_transport_) {
592 srtp_transport_->ResetParams();
593 }
594 } 585 }
595 } 586 }
596 587
597 void BaseChannel::OnSelectedCandidatePairChanged( 588 void BaseChannel::OnSelectedCandidatePairChanged(
598 IceTransportInternal* ice_transport, 589 IceTransportInternal* ice_transport,
599 CandidatePairInterface* selected_candidate_pair, 590 CandidatePairInterface* selected_candidate_pair,
600 int last_sent_packet_id, 591 int last_sent_packet_id,
601 bool ready_to_send) { 592 bool ready_to_send) {
602 RTC_DCHECK((rtp_dtls_transport_ && 593 RTC_DCHECK((rtp_dtls_transport_ &&
603 ice_transport == rtp_dtls_transport_->ice_transport()) || 594 ice_transport == rtp_dtls_transport_->ice_transport()) ||
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 } 648 }
658 649
659 // Protect ourselves against crazy data. 650 // Protect ourselves against crazy data.
660 if (!ValidPacket(rtcp, packet)) { 651 if (!ValidPacket(rtcp, packet)) {
661 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " " 652 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
662 << RtpRtcpStringLiteral(rtcp) 653 << RtpRtcpStringLiteral(rtcp)
663 << " packet: wrong size=" << packet->size(); 654 << " packet: wrong size=" << packet->size();
664 return false; 655 return false;
665 } 656 }
666 657
667 if (!srtp_active()) { 658 rtc::PacketOptions updated_options;
668 if (srtp_required_) { 659 updated_options = options;
669 // The audio/video engines may attempt to send RTCP packets as soon as the 660 // Protect if needed.
670 // streams are created, so don't treat this as an error for RTCP. 661 if (srtp_filter_.IsActive()) {
671 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809 662 TRACE_EVENT0("webrtc", "SRTP Encode");
672 if (rtcp) { 663 bool res;
664 uint8_t* data = packet->data();
665 int len = static_cast<int>(packet->size());
666 if (!rtcp) {
667 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
668 // inside libsrtp for a RTP packet. A external HMAC module will be writing
669 // a fake HMAC value. This is ONLY done for a RTP packet.
670 // Socket layer will update rtp sendtime extension header if present in
671 // packet with current time before updating the HMAC.
672 #if !defined(ENABLE_EXTERNAL_AUTH)
673 res = srtp_filter_.ProtectRtp(data, len,
674 static_cast<int>(packet->capacity()), &len);
675 #else
676 if (!srtp_filter_.IsExternalAuthActive()) {
677 res = srtp_filter_.ProtectRtp(
678 data, len, static_cast<int>(packet->capacity()), &len);
679 } else {
680 updated_options.packet_time_params.rtp_sendtime_extension_id =
681 rtp_abs_sendtime_extn_id_;
682 res = srtp_filter_.ProtectRtp(
683 data, len, static_cast<int>(packet->capacity()), &len,
684 &updated_options.packet_time_params.srtp_packet_index);
685 // If protection succeeds, let's get auth params from srtp.
686 if (res) {
687 uint8_t* auth_key = NULL;
688 int key_len;
689 res = srtp_filter_.GetRtpAuthParams(
690 &auth_key, &key_len,
691 &updated_options.packet_time_params.srtp_auth_tag_len);
692 if (res) {
693 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
694 updated_options.packet_time_params.srtp_auth_key.assign(
695 auth_key, auth_key + key_len);
696 }
697 }
698 }
699 #endif
700 if (!res) {
701 int seq_num = -1;
702 uint32_t ssrc = 0;
703 GetRtpSeqNum(data, len, &seq_num);
704 GetRtpSsrc(data, len, &ssrc);
705 LOG(LS_ERROR) << "Failed to protect " << content_name_
706 << " RTP packet: size=" << len << ", seqnum=" << seq_num
707 << ", SSRC=" << ssrc;
673 return false; 708 return false;
674 } 709 }
675 // However, there shouldn't be any RTP packets sent before SRTP is set up 710 } else {
676 // (and SetSend(true) is called). 711 res = srtp_filter_.ProtectRtcp(
677 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive" 712 data, len, static_cast<int>(packet->capacity()), &len);
678 << " and crypto is required"; 713 if (!res) {
679 RTC_NOTREACHED(); 714 int type = -1;
715 GetRtcpType(data, len, &type);
716 LOG(LS_ERROR) << "Failed to protect " << content_name_
717 << " RTCP packet: size=" << len << ", type=" << type;
718 return false;
719 }
720 }
721
722 // Update the length of the packet now that we've added the auth tag.
723 packet->SetSize(len);
724 } else if (srtp_required_) {
725 // The audio/video engines may attempt to send RTCP packets as soon as the
726 // streams are created, so don't treat this as an error for RTCP.
727 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
728 if (rtcp) {
680 return false; 729 return false;
681 } 730 }
682 // Bon voyage. 731 // However, there shouldn't be any RTP packets sent before SRTP is set up
683 return rtcp ? rtp_transport_->SendRtcpPacket(packet, options, PF_NORMAL) 732 // (and SetSend(true) is called).
684 : rtp_transport_->SendRtpPacket(packet, options, PF_NORMAL); 733 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
734 << " and crypto is required";
735 RTC_NOTREACHED();
736 return false;
685 } 737 }
686 RTC_DCHECK(srtp_transport_); 738
687 RTC_DCHECK(srtp_transport_->IsActive());
688 // Bon voyage. 739 // Bon voyage.
689 return rtcp ? srtp_transport_->SendRtcpPacket(packet, options, PF_SRTP_BYPASS) 740 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
690 : srtp_transport_->SendRtpPacket(packet, options, PF_SRTP_BYPASS); 741 return rtp_transport_->SendPacket(rtcp, packet, updated_options, flags);
691 } 742 }
692 743
693 bool BaseChannel::HandlesPayloadType(int packet_type) const { 744 bool BaseChannel::HandlesPayloadType(int packet_type) const {
694 return rtp_transport_->HandlesPayloadType(packet_type); 745 return rtp_transport_->HandlesPayloadType(packet_type);
695 } 746 }
696 747
697 void BaseChannel::OnPacketReceived(bool rtcp, 748 void BaseChannel::OnPacketReceived(bool rtcp,
698 rtc::CopyOnWriteBuffer* packet, 749 rtc::CopyOnWriteBuffer* packet,
699 const rtc::PacketTime& packet_time) { 750 const rtc::PacketTime& packet_time) {
700 if (!has_received_packet_ && !rtcp) { 751 if (!has_received_packet_ && !rtcp) {
701 has_received_packet_ = true; 752 has_received_packet_ = true;
702 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED); 753 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
703 } 754 }
704 755
705 if (!srtp_active() && srtp_required_) { 756 // Unprotect the packet, if needed.
757 if (srtp_filter_.IsActive()) {
758 TRACE_EVENT0("webrtc", "SRTP Decode");
759 char* data = packet->data<char>();
760 int len = static_cast<int>(packet->size());
761 bool res;
762 if (!rtcp) {
763 res = srtp_filter_.UnprotectRtp(data, len, &len);
764 if (!res) {
765 int seq_num = -1;
766 uint32_t ssrc = 0;
767 GetRtpSeqNum(data, len, &seq_num);
768 GetRtpSsrc(data, len, &ssrc);
769 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
770 << " RTP packet: size=" << len << ", seqnum=" << seq_num
771 << ", SSRC=" << ssrc;
772 return;
773 }
774 } else {
775 res = srtp_filter_.UnprotectRtcp(data, len, &len);
776 if (!res) {
777 int type = -1;
778 GetRtcpType(data, len, &type);
779 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
780 << " RTCP packet: size=" << len << ", type=" << type;
781 return;
782 }
783 }
784
785 packet->SetSize(len);
786 } else if (srtp_required_) {
706 // Our session description indicates that SRTP is required, but we got a 787 // Our session description indicates that SRTP is required, but we got a
707 // packet before our SRTP filter is active. This means either that 788 // packet before our SRTP filter is active. This means either that
708 // a) we got SRTP packets before we received the SDES keys, in which case 789 // a) we got SRTP packets before we received the SDES keys, in which case
709 // we can't decrypt it anyway, or 790 // we can't decrypt it anyway, or
710 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP 791 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
711 // transports, so we haven't yet extracted keys, even if DTLS did 792 // transports, so we haven't yet extracted keys, even if DTLS did
712 // complete on the transport that the packets are being sent on. It's 793 // complete on the transport that the packets are being sent on. It's
713 // really good practice to wait for both RTP and RTCP to be good to go 794 // really good practice to wait for both RTP and RTCP to be good to go
714 // before sending media, to prevent weird failure modes, so it's fine 795 // before sending media, to prevent weird failure modes, so it's fine
715 // for us to just eat packets here. This is all sidestepped if RTCP mux 796 // for us to just eat packets here. This is all sidestepped if RTCP mux
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 } 952 }
872 953
873 if (role == rtc::SSL_SERVER) { 954 if (role == rtc::SSL_SERVER) {
874 send_key = &server_write_key; 955 send_key = &server_write_key;
875 recv_key = &client_write_key; 956 recv_key = &client_write_key;
876 } else { 957 } else {
877 send_key = &client_write_key; 958 send_key = &client_write_key;
878 recv_key = &server_write_key; 959 recv_key = &server_write_key;
879 } 960 }
880 961
881 if (rtcp) { 962 if (!srtp_filter_.IsActive()) {
882 if (!dtls_active()) { 963 if (rtcp) {
883 RTC_DCHECK(srtp_transport_); 964 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
884 ret = srtp_transport_->SetRtcpParams( 965 static_cast<int>(send_key->size()),
885 selected_crypto_suite, &(*send_key)[0], 966 selected_crypto_suite, &(*recv_key)[0],
886 static_cast<int>(send_key->size()), selected_crypto_suite, 967 static_cast<int>(recv_key->size()));
887 &(*recv_key)[0], static_cast<int>(recv_key->size()));
888 } else { 968 } else {
889 // RTCP doesn't need to call SetRtpParam because it is only used 969 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
890 // to make the updated encrypted RTP header extension IDs take effect. 970 static_cast<int>(send_key->size()),
891 ret = true; 971 selected_crypto_suite, &(*recv_key)[0],
972 static_cast<int>(recv_key->size()));
892 } 973 }
893 } else { 974 } else {
894 RTC_DCHECK(srtp_transport_); 975 if (rtcp) {
895 ret = srtp_transport_->SetRtpParams(selected_crypto_suite, &(*send_key)[0], 976 // RTCP doesn't need to be updated because UpdateRtpParams is only used
896 static_cast<int>(send_key->size()), 977 // to update the set of encrypted RTP header extension IDs.
897 selected_crypto_suite, &(*recv_key)[0], 978 ret = true;
898 static_cast<int>(recv_key->size())); 979 } else {
899 dtls_active_ = ret; 980 ret = srtp_filter_.UpdateRtpParams(selected_crypto_suite, &(*send_key)[0],
981 static_cast<int>(send_key->size()),
982 selected_crypto_suite, &(*recv_key)[0],
983 static_cast<int>(recv_key->size()));
984 }
900 } 985 }
901 986
902 if (!ret) { 987 if (!ret) {
903 LOG(LS_WARNING) << "DTLS-SRTP key installation failed"; 988 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
904 } else { 989 } else {
990 dtls_keyed_ = true;
905 UpdateTransportOverhead(); 991 UpdateTransportOverhead();
906 } 992 }
907 return ret; 993 return ret;
908 } 994 }
909 995
910 void BaseChannel::MaybeSetupDtlsSrtp_n() { 996 void BaseChannel::MaybeSetupDtlsSrtp_n() {
911 if (dtls_active()) { 997 if (srtp_filter_.IsActive()) {
912 return; 998 return;
913 } 999 }
914 1000
915 if (!ShouldSetupDtlsSrtp_n()) { 1001 if (!ShouldSetupDtlsSrtp_n()) {
916 return; 1002 return;
917 } 1003 }
918 1004
919 if (!srtp_transport_) {
920 EnableSrtpTransport_n();
921 }
922
923 if (!SetupDtlsSrtp_n(false)) { 1005 if (!SetupDtlsSrtp_n(false)) {
924 SignalDtlsSrtpSetupFailure_n(false); 1006 SignalDtlsSrtpSetupFailure_n(false);
925 return; 1007 return;
926 } 1008 }
927 1009
928 if (rtcp_dtls_transport_) { 1010 if (rtcp_dtls_transport_) {
929 if (!SetupDtlsSrtp_n(true)) { 1011 if (!SetupDtlsSrtp_n(true)) {
930 SignalDtlsSrtpSetupFailure_n(true); 1012 SignalDtlsSrtpSetupFailure_n(true);
931 return; 1013 return;
932 } 1014 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 bool* dtls, 1078 bool* dtls,
997 std::string* error_desc) { 1079 std::string* error_desc) {
998 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive(); 1080 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
999 if (*dtls && !cryptos.empty()) { 1081 if (*dtls && !cryptos.empty()) {
1000 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc); 1082 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
1001 return false; 1083 return false;
1002 } 1084 }
1003 return true; 1085 return true;
1004 } 1086 }
1005 1087
1006 void BaseChannel::EnableSrtpTransport_n() {
1007 if (srtp_transport_ == nullptr) {
1008 rtp_transport_->SignalReadyToSend.disconnect(this);
1009 rtp_transport_->SignalPacketReceived.disconnect(this);
1010
1011 auto transport = rtc::MakeUnique<webrtc::SrtpTransport>(
1012 std::move(rtp_transport_), content_name_);
1013 srtp_transport_ = transport.get();
1014 rtp_transport_ = std::move(transport);
1015
1016 rtp_transport_->SignalReadyToSend.connect(
1017 this, &BaseChannel::OnTransportReadyToSend);
1018 rtp_transport_->SignalPacketReceived.connect(
1019 this, &BaseChannel::OnPacketReceived);
1020 LOG(LS_INFO) << "Wrapping RtpTransport in SrtpTransport.";
1021 }
1022 }
1023
1024 bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos, 1088 bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
1025 ContentAction action, 1089 ContentAction action,
1026 ContentSource src, 1090 ContentSource src,
1027 const std::vector<int>& encrypted_extension_ids, 1091 const std::vector<int>& encrypted_extension_ids,
1028 std::string* error_desc) { 1092 std::string* error_desc) {
1029 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w"); 1093 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
1030 if (action == CA_UPDATE) { 1094 if (action == CA_UPDATE) {
1031 // no crypto params. 1095 // no crypto params.
1032 return true; 1096 return true;
1033 } 1097 }
1034 bool ret = false; 1098 bool ret = false;
1035 bool dtls = false; 1099 bool dtls = false;
1036 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc); 1100 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
1037 if (!ret) { 1101 if (!ret) {
1038 return false; 1102 return false;
1039 } 1103 }
1040 1104 srtp_filter_.SetEncryptedHeaderExtensionIds(src, encrypted_extension_ids);
1041 // If SRTP was not required, but we're setting a description that uses SDES,
1042 // we need to upgrade to an SrtpTransport.
1043 if (!srtp_transport_ && !dtls && !cryptos.empty()) {
1044 EnableSrtpTransport_n();
1045 }
1046 if (srtp_transport_) {
1047 srtp_transport_->SetEncryptedHeaderExtensionIds(src,
1048 encrypted_extension_ids);
1049 }
1050 switch (action) { 1105 switch (action) {
1051 case CA_OFFER: 1106 case CA_OFFER:
1052 // If DTLS is already active on the channel, we could be renegotiating 1107 // If DTLS is already active on the channel, we could be renegotiating
1053 // here. We don't update the srtp filter. 1108 // here. We don't update the srtp filter.
1054 if (!dtls) { 1109 if (!dtls) {
1055 ret = sdes_negotiator_.SetOffer(cryptos, src); 1110 ret = srtp_filter_.SetOffer(cryptos, src);
1056 } 1111 }
1057 break; 1112 break;
1058 case CA_PRANSWER: 1113 case CA_PRANSWER:
1059 // If we're doing DTLS-SRTP, we don't want to update the filter 1114 // If we're doing DTLS-SRTP, we don't want to update the filter
1060 // with an answer, because we already have SRTP parameters. 1115 // with an answer, because we already have SRTP parameters.
1061 if (!dtls) { 1116 if (!dtls) {
1062 ret = sdes_negotiator_.SetProvisionalAnswer(cryptos, src); 1117 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1063 } 1118 }
1064 break; 1119 break;
1065 case CA_ANSWER: 1120 case CA_ANSWER:
1066 // If we're doing DTLS-SRTP, we don't want to update the filter 1121 // If we're doing DTLS-SRTP, we don't want to update the filter
1067 // with an answer, because we already have SRTP parameters. 1122 // with an answer, because we already have SRTP parameters.
1068 if (!dtls) { 1123 if (!dtls) {
1069 ret = sdes_negotiator_.SetAnswer(cryptos, src); 1124 ret = srtp_filter_.SetAnswer(cryptos, src);
1070 } 1125 }
1071 break; 1126 break;
1072 default: 1127 default:
1073 break; 1128 break;
1074 } 1129 }
1075
1076 // If setting an SDES answer succeeded, apply the negotiated parameters
1077 // to the SRTP transport.
1078 if ((action == CA_PRANSWER || action == CA_ANSWER) && !dtls && ret) {
1079 if (sdes_negotiator_.send_cipher_suite() &&
1080 sdes_negotiator_.recv_cipher_suite()) {
1081 ret = srtp_transport_->SetRtpParams(
1082 *(sdes_negotiator_.send_cipher_suite()),
1083 sdes_negotiator_.send_key().data(),
1084 static_cast<int>(sdes_negotiator_.send_key().size()),
1085 *(sdes_negotiator_.recv_cipher_suite()),
1086 sdes_negotiator_.recv_key().data(),
1087 static_cast<int>(sdes_negotiator_.recv_key().size()));
1088 } else {
1089 LOG(LS_INFO) << "No crypto keys are provided for SDES.";
1090 if (action == CA_ANSWER && srtp_transport_) {
1091 // Explicitly reset the |srtp_transport_| if no crypto param is
1092 // provided in the answer. No need to call |ResetParams()| for
1093 // |sdes_negotiator_| because it resets the params inside |SetAnswer|.
1094 srtp_transport_->ResetParams();
1095 }
1096 }
1097 }
1098
1099 // Only update SRTP filter if using DTLS. SDES is handled internally 1130 // Only update SRTP filter if using DTLS. SDES is handled internally
1100 // by the SRTP filter. 1131 // by the SRTP filter.
1101 // TODO(jbauch): Only update if encrypted extension ids have changed. 1132 // TODO(jbauch): Only update if encrypted extension ids have changed.
1102 if (ret && dtls_active() && rtp_dtls_transport_ && 1133 if (ret && dtls_keyed_ && rtp_dtls_transport_ &&
1103 rtp_dtls_transport_->dtls_state() == DTLS_TRANSPORT_CONNECTED) { 1134 rtp_dtls_transport_->dtls_state() == DTLS_TRANSPORT_CONNECTED) {
1104 bool rtcp = false; 1135 bool rtcp = false;
1105 ret = SetupDtlsSrtp_n(rtcp); 1136 ret = SetupDtlsSrtp_n(rtcp);
1106 } 1137 }
1107 if (!ret) { 1138 if (!ret) {
1108 SafeSetError("Failed to setup SRTP filter.", error_desc); 1139 SafeSetError("Failed to setup SRTP filter.", error_desc);
1109 return false; 1140 return false;
1110 } 1141 }
1111 return true; 1142 return true;
1112 } 1143 }
(...skipping 23 matching lines...) Expand all
1136 break; 1167 break;
1137 case CA_ANSWER: 1168 case CA_ANSWER:
1138 ret = rtcp_mux_filter_.SetAnswer(enable, src); 1169 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1139 if (ret && rtcp_mux_filter_.IsActive()) { 1170 if (ret && rtcp_mux_filter_.IsActive()) {
1140 // We permanently activated RTCP muxing; signal that we no longer need 1171 // We permanently activated RTCP muxing; signal that we no longer need
1141 // the RTCP transport. 1172 // the RTCP transport.
1142 std::string debug_name = 1173 std::string debug_name =
1143 transport_name_.empty() 1174 transport_name_.empty()
1144 ? rtp_transport_->rtp_packet_transport()->debug_name() 1175 ? rtp_transport_->rtp_packet_transport()->debug_name()
1145 : transport_name_; 1176 : transport_name_;
1177 ;
1146 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name() 1178 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1147 << "; no longer need RTCP transport for " << debug_name; 1179 << "; no longer need RTCP transport for " << debug_name;
1148 if (rtp_transport_->rtcp_packet_transport()) { 1180 if (rtp_transport_->rtcp_packet_transport()) {
1149 SetTransport_n(true, nullptr, nullptr); 1181 SetTransport_n(true, nullptr, nullptr);
1150 SignalRtcpMuxFullyActive(transport_name_); 1182 SignalRtcpMuxFullyActive(transport_name_);
1151 } 1183 }
1152 UpdateWritableState_n(); 1184 UpdateWritableState_n();
1153 } 1185 }
1154 break; 1186 break;
1155 case CA_UPDATE: 1187 case CA_UPDATE:
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1364 send_time_extension ? send_time_extension->id : -1; 1396 send_time_extension ? send_time_extension->id : -1;
1365 invoker_.AsyncInvoke<void>( 1397 invoker_.AsyncInvoke<void>(
1366 RTC_FROM_HERE, network_thread_, 1398 RTC_FROM_HERE, network_thread_,
1367 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this, 1399 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1368 rtp_abs_sendtime_extn_id)); 1400 rtp_abs_sendtime_extn_id));
1369 #endif 1401 #endif
1370 } 1402 }
1371 1403
1372 void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n( 1404 void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1373 int rtp_abs_sendtime_extn_id) { 1405 int rtp_abs_sendtime_extn_id) {
1374 if (srtp_transport_) { 1406 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
1375 srtp_transport_->CacheRtpAbsSendTimeHeaderExtension(
1376 rtp_abs_sendtime_extn_id);
1377 } else {
1378 LOG(LS_WARNING) << "Trying to cache the Absolute Send Time extension id "
1379 "but the SRTP is not active.";
1380 }
1381 } 1407 }
1382 1408
1383 void BaseChannel::OnMessage(rtc::Message *pmsg) { 1409 void BaseChannel::OnMessage(rtc::Message *pmsg) {
1384 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage"); 1410 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
1385 switch (pmsg->message_id) { 1411 switch (pmsg->message_id) {
1386 case MSG_SEND_RTP_PACKET: 1412 case MSG_SEND_RTP_PACKET:
1387 case MSG_SEND_RTCP_PACKET: { 1413 case MSG_SEND_RTCP_PACKET: {
1388 RTC_DCHECK(network_thread_->IsCurrent()); 1414 RTC_DCHECK(network_thread_->IsCurrent());
1389 SendPacketMessageData* data = 1415 SendPacketMessageData* data =
1390 static_cast<SendPacketMessageData*>(pmsg->pdata); 1416 static_cast<SendPacketMessageData*>(pmsg->pdata);
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
1654 : kIpv6Overhaed; 1680 : kIpv6Overhaed;
1655 1681
1656 constexpr int kUdpOverhaed = 8; 1682 constexpr int kUdpOverhaed = 8;
1657 constexpr int kTcpOverhaed = 20; 1683 constexpr int kTcpOverhaed = 20;
1658 transport_overhead_per_packet += 1684 transport_overhead_per_packet +=
1659 selected_candidate_pair_->local_candidate().protocol() == 1685 selected_candidate_pair_->local_candidate().protocol() ==
1660 TCP_PROTOCOL_NAME 1686 TCP_PROTOCOL_NAME
1661 ? kTcpOverhaed 1687 ? kTcpOverhaed
1662 : kUdpOverhaed; 1688 : kUdpOverhaed;
1663 1689
1664 if (sdes_active()) { 1690 if (secure()) {
1665 int srtp_overhead = 0; 1691 int srtp_overhead = 0;
1666 if (srtp_transport_->GetSrtpOverhead(&srtp_overhead)) 1692 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1667 transport_overhead_per_packet += srtp_overhead; 1693 transport_overhead_per_packet += srtp_overhead;
1668 } 1694 }
1669 1695
1670 return transport_overhead_per_packet; 1696 return transport_overhead_per_packet;
1671 } 1697 }
1672 1698
1673 void BaseChannel::UpdateTransportOverhead() { 1699 void BaseChannel::UpdateTransportOverhead() {
1674 int transport_overhead_per_packet = GetTransportOverheadPerPacket(); 1700 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1675 if (transport_overhead_per_packet) 1701 if (transport_overhead_per_packet)
1676 invoker_.AsyncInvoke<void>( 1702 invoker_.AsyncInvoke<void>(
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
2376 2402
2377 void RtpDataChannel::OnDataChannelReadyToSend(bool writable) { 2403 void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
2378 // This is usded for congestion control to indicate that the stream is ready 2404 // 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 2405 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2380 // that the transport channel is ready. 2406 // that the transport channel is ready.
2381 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA, 2407 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
2382 new DataChannelReadyToSendMessageData(writable)); 2408 new DataChannelReadyToSendMessageData(writable));
2383 } 2409 }
2384 2410
2385 } // namespace cricket 2411 } // namespace cricket
OLDNEW
« no previous file with comments | « pc/channel.h ('k') | pc/channel_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698