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

Side by Side Diff: webrtc/pc/srtpfilter.cc

Issue 2722423003: Improve testing of SRTP external auth code paths. (Closed)
Patch Set: Changes based on feedback from Taylor. Created 3 years, 9 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 | « webrtc/pc/srtpfilter.h ('k') | webrtc/pc/srtpfilter_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 2009 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2009 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 // NOTE: This is called from ChannelManager D'tor. 45 // NOTE: This is called from ChannelManager D'tor.
46 void ShutdownSrtp() { 46 void ShutdownSrtp() {
47 #ifdef HAVE_SRTP 47 #ifdef HAVE_SRTP
48 // If srtp_dealloc is not executed then this will clear all existing sessions. 48 // If srtp_dealloc is not executed then this will clear all existing sessions.
49 // This should be called when application is shutting down. 49 // This should be called when application is shutting down.
50 SrtpSession::Terminate(); 50 SrtpSession::Terminate();
51 #endif 51 #endif
52 } 52 }
53 53
54 SrtpFilter::SrtpFilter() 54 SrtpFilter::SrtpFilter() {
55 : state_(ST_INIT),
56 signal_silent_time_in_ms_(0) {
57 } 55 }
58 56
59 SrtpFilter::~SrtpFilter() { 57 SrtpFilter::~SrtpFilter() {
60 } 58 }
61 59
62 bool SrtpFilter::IsActive() const { 60 bool SrtpFilter::IsActive() const {
63 return state_ >= ST_ACTIVE; 61 return state_ >= ST_ACTIVE;
64 } 62 }
65 63
66 bool SrtpFilter::SetOffer(const std::vector<CryptoParams>& offer_params, 64 bool SrtpFilter::SetOffer(const std::vector<CryptoParams>& offer_params,
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 if (!IsActive()) { 217 if (!IsActive()) {
220 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active"; 218 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active";
221 return false; 219 return false;
222 } 220 }
223 221
224 RTC_CHECK(send_session_); 222 RTC_CHECK(send_session_);
225 *srtp_overhead = send_session_->GetSrtpOverhead(); 223 *srtp_overhead = send_session_->GetSrtpOverhead();
226 return true; 224 return true;
227 } 225 }
228 226
229 #if defined(ENABLE_EXTERNAL_AUTH) 227 void SrtpFilter::EnableExternalAuth() {
228 RTC_DCHECK(!IsActive());
229 external_auth_enabled_ = true;
230 }
231
232 bool SrtpFilter::IsExternalAuthEnabled() const {
233 return external_auth_enabled_;
234 }
235
230 bool SrtpFilter::IsExternalAuthActive() const { 236 bool SrtpFilter::IsExternalAuthActive() const {
231 if (!IsActive()) { 237 if (!IsActive()) {
232 LOG(LS_WARNING) << "Failed to check IsExternalAuthActive: SRTP not active"; 238 LOG(LS_WARNING) << "Failed to check IsExternalAuthActive: SRTP not active";
233 return false; 239 return false;
234 } 240 }
235 241
236 RTC_CHECK(send_session_); 242 RTC_CHECK(send_session_);
237 return send_session_->IsExternalAuthActive(); 243 return send_session_->IsExternalAuthActive();
238 } 244 }
239 #endif
240 245
241 void SrtpFilter::set_signal_silent_time(int signal_silent_time_in_ms) { 246 void SrtpFilter::set_signal_silent_time(int signal_silent_time_in_ms) {
242 signal_silent_time_in_ms_ = signal_silent_time_in_ms; 247 signal_silent_time_in_ms_ = signal_silent_time_in_ms;
243 if (IsActive()) { 248 if (IsActive()) {
244 RTC_CHECK(send_session_); 249 RTC_CHECK(send_session_);
245 send_session_->set_signal_silent_time(signal_silent_time_in_ms); 250 send_session_->set_signal_silent_time(signal_silent_time_in_ms);
246 RTC_CHECK(recv_session_); 251 RTC_CHECK(recv_session_);
247 recv_session_->set_signal_silent_time(signal_silent_time_in_ms); 252 recv_session_->set_signal_silent_time(signal_silent_time_in_ms);
248 if (send_rtcp_session_) 253 if (send_rtcp_session_)
249 send_rtcp_session_->set_signal_silent_time(signal_silent_time_in_ms); 254 send_rtcp_session_->set_signal_silent_time(signal_silent_time_in_ms);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 send_session_.reset(new SrtpSession()); 336 send_session_.reset(new SrtpSession());
332 applied_send_params_ = CryptoParams(); 337 applied_send_params_ = CryptoParams();
333 recv_session_.reset(new SrtpSession()); 338 recv_session_.reset(new SrtpSession());
334 applied_recv_params_ = CryptoParams(); 339 applied_recv_params_ = CryptoParams();
335 340
336 SignalSrtpError.repeat(send_session_->SignalSrtpError); 341 SignalSrtpError.repeat(send_session_->SignalSrtpError);
337 SignalSrtpError.repeat(recv_session_->SignalSrtpError); 342 SignalSrtpError.repeat(recv_session_->SignalSrtpError);
338 343
339 send_session_->set_signal_silent_time(signal_silent_time_in_ms_); 344 send_session_->set_signal_silent_time(signal_silent_time_in_ms_);
340 recv_session_->set_signal_silent_time(signal_silent_time_in_ms_); 345 recv_session_->set_signal_silent_time(signal_silent_time_in_ms_);
346 if (external_auth_enabled_) {
347 send_session_->EnableExternalAuth();
348 }
341 } 349 }
342 350
343 bool SrtpFilter::NegotiateParams(const std::vector<CryptoParams>& answer_params, 351 bool SrtpFilter::NegotiateParams(const std::vector<CryptoParams>& answer_params,
344 CryptoParams* selected_params) { 352 CryptoParams* selected_params) {
345 // We're processing an accept. We should have exactly one set of params, 353 // We're processing an accept. We should have exactly one set of params,
346 // unless the offer didn't mention crypto, in which case we shouldn't be here. 354 // unless the offer didn't mention crypto, in which case we shouldn't be here.
347 bool ret = (answer_params.size() == 1U && !offer_params_.empty()); 355 bool ret = (answer_params.size() == 1U && !offer_params_.empty());
348 if (ret) { 356 if (ret) {
349 // We should find a match between the answer params and the offered params. 357 // We should find a match between the answer params and the offered params.
350 std::vector<CryptoParams>::const_iterator it; 358 std::vector<CryptoParams>::const_iterator it;
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 int err = srtp_unprotect_rtcp(session_, p, out_len); 600 int err = srtp_unprotect_rtcp(session_, p, out_len);
593 srtp_stat_->AddUnprotectRtcpResult(err); 601 srtp_stat_->AddUnprotectRtcpResult(err);
594 if (err != srtp_err_status_ok) { 602 if (err != srtp_err_status_ok) {
595 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err; 603 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err;
596 return false; 604 return false;
597 } 605 }
598 return true; 606 return true;
599 } 607 }
600 608
601 bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) { 609 bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) {
602 #if defined(ENABLE_EXTERNAL_AUTH)
603 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 610 RTC_DCHECK(thread_checker_.CalledOnValidThread());
604 RTC_DCHECK(IsExternalAuthActive()); 611 RTC_DCHECK(IsExternalAuthActive());
605 if (!IsExternalAuthActive()) { 612 if (!IsExternalAuthActive()) {
606 return false; 613 return false;
607 } 614 }
608 615
609 ExternalHmacContext* external_hmac = nullptr; 616 ExternalHmacContext* external_hmac = nullptr;
610 // stream_template will be the reference context for other streams. 617 // stream_template will be the reference context for other streams.
611 // Let's use it for getting the keys. 618 // Let's use it for getting the keys.
612 srtp_stream_ctx_t* srtp_context = session_->stream_template; 619 srtp_stream_ctx_t* srtp_context = session_->stream_template;
613 if (srtp_context && srtp_context->rtp_auth) { 620 if (srtp_context && srtp_context->rtp_auth) {
614 external_hmac = reinterpret_cast<ExternalHmacContext*>( 621 external_hmac = reinterpret_cast<ExternalHmacContext*>(
615 srtp_context->rtp_auth->state); 622 srtp_context->rtp_auth->state);
616 } 623 }
617 624
618 if (!external_hmac) { 625 if (!external_hmac) {
619 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!."; 626 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!.";
620 return false; 627 return false;
621 } 628 }
622 629
623 *key = external_hmac->key; 630 *key = external_hmac->key;
624 *key_len = external_hmac->key_length; 631 *key_len = external_hmac->key_length;
625 *tag_len = rtp_auth_tag_len_; 632 *tag_len = rtp_auth_tag_len_;
626 return true; 633 return true;
627 #else
628 return false;
629 #endif
630 } 634 }
631 635
632 int SrtpSession::GetSrtpOverhead() const { 636 int SrtpSession::GetSrtpOverhead() const {
633 return rtp_auth_tag_len_; 637 return rtp_auth_tag_len_;
634 } 638 }
635 639
636 #if defined(ENABLE_EXTERNAL_AUTH) 640 void SrtpSession::EnableExternalAuth() {
641 RTC_DCHECK(!session_);
642 external_auth_enabled_ = true;
643 }
644
645 bool SrtpSession::IsExternalAuthEnabled() const {
646 return external_auth_enabled_;
647 }
648
637 bool SrtpSession::IsExternalAuthActive() const { 649 bool SrtpSession::IsExternalAuthActive() const {
638 return external_auth_active_; 650 return external_auth_active_;
639 } 651 }
640 #endif
641 652
642 bool SrtpSession::GetSendStreamPacketIndex(void* p, 653 bool SrtpSession::GetSendStreamPacketIndex(void* p,
643 int in_len, 654 int in_len,
644 int64_t* index) { 655 int64_t* index) {
645 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 656 RTC_DCHECK(thread_checker_.CalledOnValidThread());
646 srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p); 657 srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p);
647 srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc); 658 srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc);
648 if (!stream) { 659 if (!stream) {
649 return false; 660 return false;
650 } 661 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type); 723 policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type);
713 policy.ssrc.value = 0; 724 policy.ssrc.value = 0;
714 policy.key = const_cast<uint8_t*>(key); 725 policy.key = const_cast<uint8_t*>(key);
715 // TODO(astor) parse window size from WSH session-param 726 // TODO(astor) parse window size from WSH session-param
716 policy.window_size = 1024; 727 policy.window_size = 1024;
717 policy.allow_repeat_tx = 1; 728 policy.allow_repeat_tx = 1;
718 // If external authentication option is enabled, supply custom auth module 729 // If external authentication option is enabled, supply custom auth module
719 // id EXTERNAL_HMAC_SHA1 in the policy structure. 730 // id EXTERNAL_HMAC_SHA1 in the policy structure.
720 // We want to set this option only for rtp packets. 731 // We want to set this option only for rtp packets.
721 // By default policy structure is initialized to HMAC_SHA1. 732 // By default policy structure is initialized to HMAC_SHA1.
722 #if defined(ENABLE_EXTERNAL_AUTH)
723 // Enable external HMAC authentication only for outgoing streams and only 733 // Enable external HMAC authentication only for outgoing streams and only
724 // for cipher suites that support it (i.e. only non-GCM cipher suites). 734 // for cipher suites that support it (i.e. only non-GCM cipher suites).
725 if (type == ssrc_any_outbound && !rtc::IsGcmCryptoSuite(cs)) { 735 if (type == ssrc_any_outbound && IsExternalAuthEnabled() &&
736 !rtc::IsGcmCryptoSuite(cs)) {
726 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1; 737 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1;
727 } 738 }
728 #endif
729 policy.next = nullptr; 739 policy.next = nullptr;
730 740
731 int err = srtp_create(&session_, &policy); 741 int err = srtp_create(&session_, &policy);
732 if (err != srtp_err_status_ok) { 742 if (err != srtp_err_status_ok) {
733 session_ = nullptr; 743 session_ = nullptr;
734 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err; 744 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err;
735 return false; 745 return false;
736 } 746 }
737 747
738 srtp_set_user_data(session_, this); 748 srtp_set_user_data(session_, this);
739 rtp_auth_tag_len_ = policy.rtp.auth_tag_len; 749 rtp_auth_tag_len_ = policy.rtp.auth_tag_len;
740 rtcp_auth_tag_len_ = policy.rtcp.auth_tag_len; 750 rtcp_auth_tag_len_ = policy.rtcp.auth_tag_len;
741 #if defined(ENABLE_EXTERNAL_AUTH)
742 external_auth_active_ = (policy.rtp.auth_type == EXTERNAL_HMAC_SHA1); 751 external_auth_active_ = (policy.rtp.auth_type == EXTERNAL_HMAC_SHA1);
743 #endif
744 return true; 752 return true;
745 } 753 }
746 754
747 bool SrtpSession::Init() { 755 bool SrtpSession::Init() {
748 rtc::GlobalLockScope ls(&lock_); 756 rtc::GlobalLockScope ls(&lock_);
749 757
750 if (!inited_) { 758 if (!inited_) {
751 int err; 759 int err;
752 err = srtp_init(); 760 err = srtp_init();
753 if (err != srtp_err_status_ok) { 761 if (err != srtp_err_status_ok) {
754 LOG(LS_ERROR) << "Failed to init SRTP, err=" << err; 762 LOG(LS_ERROR) << "Failed to init SRTP, err=" << err;
755 return false; 763 return false;
756 } 764 }
757 765
758 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk); 766 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk);
759 if (err != srtp_err_status_ok) { 767 if (err != srtp_err_status_ok) {
760 LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err; 768 LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err;
761 return false; 769 return false;
762 } 770 }
763 #if defined(ENABLE_EXTERNAL_AUTH) 771
764 err = external_crypto_init(); 772 err = external_crypto_init();
765 if (err != srtp_err_status_ok) { 773 if (err != srtp_err_status_ok) {
766 LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err; 774 LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err;
767 return false; 775 return false;
768 } 776 }
769 #endif
770 inited_ = true; 777 inited_ = true;
771 } 778 }
772 779
773 return true; 780 return true;
774 } 781 }
775 782
776 void SrtpSession::Terminate() { 783 void SrtpSession::Terminate() {
777 rtc::GlobalLockScope ls(&lock_); 784 rtc::GlobalLockScope ls(&lock_);
778 785
779 if (inited_) { 786 if (inited_) {
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 SrtpNotAvailable(__FUNCTION__); 964 SrtpNotAvailable(__FUNCTION__);
958 } 965 }
959 966
960 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) { 967 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) {
961 SrtpNotAvailable(__FUNCTION__); 968 SrtpNotAvailable(__FUNCTION__);
962 } 969 }
963 970
964 #endif // HAVE_SRTP 971 #endif // HAVE_SRTP
965 972
966 } // namespace cricket 973 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/pc/srtpfilter.h ('k') | webrtc/pc/srtpfilter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698