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

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

Issue 2722423003: Improve testing of SRTP external auth code paths. (Closed)
Patch Set: 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
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), 55 #if defined(ENABLE_EXTERNAL_AUTH)
56 signal_silent_time_in_ms_(0) { 56 external_auth_allowed_ = true;
57 #endif
Taylor Brandstetter 2017/03/03 02:20:34 Another option, which I slightly prefer, would be
joachim 2017/03/03 20:42:57 Done (gets enabled in channel.cc). I used "IsExter
Taylor Brandstetter 2017/03/03 23:03:57 Yep, channel.cc is what I meant, sorry for the mix
57 } 58 }
58 59
59 SrtpFilter::~SrtpFilter() { 60 SrtpFilter::~SrtpFilter() {
60 } 61 }
61 62
62 bool SrtpFilter::IsActive() const { 63 bool SrtpFilter::IsActive() const {
63 return state_ >= ST_ACTIVE; 64 return state_ >= ST_ACTIVE;
64 } 65 }
65 66
66 bool SrtpFilter::SetOffer(const std::vector<CryptoParams>& offer_params, 67 bool SrtpFilter::SetOffer(const std::vector<CryptoParams>& offer_params,
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 if (!IsActive()) { 220 if (!IsActive()) {
220 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active"; 221 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active";
221 return false; 222 return false;
222 } 223 }
223 224
224 RTC_CHECK(send_session_); 225 RTC_CHECK(send_session_);
225 *srtp_overhead = send_session_->GetSrtpOverhead(); 226 *srtp_overhead = send_session_->GetSrtpOverhead();
226 return true; 227 return true;
227 } 228 }
228 229
229 #if defined(ENABLE_EXTERNAL_AUTH) 230 void SrtpFilter::AllowExternalAuthForTest(bool allow) {
231 external_auth_allowed_ = allow;
232 if (IsActive()) {
233 RTC_CHECK(send_session_);
234 send_session_->AllowExternalAuthForTest(allow);
235 }
236 }
237
238 bool SrtpFilter::IsExternalAuthAllowed() const {
239 return external_auth_allowed_;
240 }
241
230 bool SrtpFilter::IsExternalAuthActive() const { 242 bool SrtpFilter::IsExternalAuthActive() const {
231 if (!IsActive()) { 243 if (!IsActive()) {
232 LOG(LS_WARNING) << "Failed to check IsExternalAuthActive: SRTP not active"; 244 LOG(LS_WARNING) << "Failed to check IsExternalAuthActive: SRTP not active";
233 return false; 245 return false;
234 } 246 }
235 247
236 RTC_CHECK(send_session_); 248 RTC_CHECK(send_session_);
237 return send_session_->IsExternalAuthActive(); 249 return send_session_->IsExternalAuthActive();
238 } 250 }
239 #endif
240 251
241 void SrtpFilter::set_signal_silent_time(int signal_silent_time_in_ms) { 252 void SrtpFilter::set_signal_silent_time(int signal_silent_time_in_ms) {
242 signal_silent_time_in_ms_ = signal_silent_time_in_ms; 253 signal_silent_time_in_ms_ = signal_silent_time_in_ms;
243 if (IsActive()) { 254 if (IsActive()) {
244 RTC_CHECK(send_session_); 255 RTC_CHECK(send_session_);
245 send_session_->set_signal_silent_time(signal_silent_time_in_ms); 256 send_session_->set_signal_silent_time(signal_silent_time_in_ms);
246 RTC_CHECK(recv_session_); 257 RTC_CHECK(recv_session_);
247 recv_session_->set_signal_silent_time(signal_silent_time_in_ms); 258 recv_session_->set_signal_silent_time(signal_silent_time_in_ms);
248 if (send_rtcp_session_) 259 if (send_rtcp_session_)
249 send_rtcp_session_->set_signal_silent_time(signal_silent_time_in_ms); 260 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()); 342 send_session_.reset(new SrtpSession());
332 applied_send_params_ = CryptoParams(); 343 applied_send_params_ = CryptoParams();
333 recv_session_.reset(new SrtpSession()); 344 recv_session_.reset(new SrtpSession());
334 applied_recv_params_ = CryptoParams(); 345 applied_recv_params_ = CryptoParams();
335 346
336 SignalSrtpError.repeat(send_session_->SignalSrtpError); 347 SignalSrtpError.repeat(send_session_->SignalSrtpError);
337 SignalSrtpError.repeat(recv_session_->SignalSrtpError); 348 SignalSrtpError.repeat(recv_session_->SignalSrtpError);
338 349
339 send_session_->set_signal_silent_time(signal_silent_time_in_ms_); 350 send_session_->set_signal_silent_time(signal_silent_time_in_ms_);
340 recv_session_->set_signal_silent_time(signal_silent_time_in_ms_); 351 recv_session_->set_signal_silent_time(signal_silent_time_in_ms_);
352 send_session_->AllowExternalAuthForTest(external_auth_allowed_);
341 } 353 }
342 354
343 bool SrtpFilter::NegotiateParams(const std::vector<CryptoParams>& answer_params, 355 bool SrtpFilter::NegotiateParams(const std::vector<CryptoParams>& answer_params,
344 CryptoParams* selected_params) { 356 CryptoParams* selected_params) {
345 // We're processing an accept. We should have exactly one set of params, 357 // 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. 358 // 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()); 359 bool ret = (answer_params.size() == 1U && !offer_params_.empty());
348 if (ret) { 360 if (ret) {
349 // We should find a match between the answer params and the offered params. 361 // We should find a match between the answer params and the offered params.
350 std::vector<CryptoParams>::const_iterator it; 362 std::vector<CryptoParams>::const_iterator it;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 // SrtpSession 481 // SrtpSession
470 482
471 #ifdef HAVE_SRTP 483 #ifdef HAVE_SRTP
472 484
473 bool SrtpSession::inited_ = false; 485 bool SrtpSession::inited_ = false;
474 486
475 // This lock protects SrtpSession::inited_. 487 // This lock protects SrtpSession::inited_.
476 rtc::GlobalLockPod SrtpSession::lock_; 488 rtc::GlobalLockPod SrtpSession::lock_;
477 489
478 SrtpSession::SrtpSession() : srtp_stat_(new SrtpStat()) { 490 SrtpSession::SrtpSession() : srtp_stat_(new SrtpStat()) {
491 #if defined(ENABLE_EXTERNAL_AUTH)
492 external_auth_allowed_ = true;
493 #endif
479 SignalSrtpError.repeat(srtp_stat_->SignalSrtpError); 494 SignalSrtpError.repeat(srtp_stat_->SignalSrtpError);
480 } 495 }
481 496
482 SrtpSession::~SrtpSession() { 497 SrtpSession::~SrtpSession() {
483 if (session_) { 498 if (session_) {
484 srtp_set_user_data(session_, nullptr); 499 srtp_set_user_data(session_, nullptr);
485 srtp_dealloc(session_); 500 srtp_dealloc(session_);
486 } 501 }
487 } 502 }
488 503
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 int err = srtp_unprotect_rtcp(session_, p, out_len); 607 int err = srtp_unprotect_rtcp(session_, p, out_len);
593 srtp_stat_->AddUnprotectRtcpResult(err); 608 srtp_stat_->AddUnprotectRtcpResult(err);
594 if (err != srtp_err_status_ok) { 609 if (err != srtp_err_status_ok) {
595 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err; 610 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err;
596 return false; 611 return false;
597 } 612 }
598 return true; 613 return true;
599 } 614 }
600 615
601 bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) { 616 bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) {
602 #if defined(ENABLE_EXTERNAL_AUTH)
603 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 617 RTC_DCHECK(thread_checker_.CalledOnValidThread());
604 RTC_DCHECK(IsExternalAuthActive()); 618 RTC_DCHECK(IsExternalAuthActive());
605 if (!IsExternalAuthActive()) { 619 if (!IsExternalAuthActive()) {
606 return false; 620 return false;
607 } 621 }
608 622
609 ExternalHmacContext* external_hmac = nullptr; 623 ExternalHmacContext* external_hmac = nullptr;
610 // stream_template will be the reference context for other streams. 624 // stream_template will be the reference context for other streams.
611 // Let's use it for getting the keys. 625 // Let's use it for getting the keys.
612 srtp_stream_ctx_t* srtp_context = session_->stream_template; 626 srtp_stream_ctx_t* srtp_context = session_->stream_template;
613 if (srtp_context && srtp_context->rtp_auth) { 627 if (srtp_context && srtp_context->rtp_auth) {
614 external_hmac = reinterpret_cast<ExternalHmacContext*>( 628 external_hmac = reinterpret_cast<ExternalHmacContext*>(
615 srtp_context->rtp_auth->state); 629 srtp_context->rtp_auth->state);
616 } 630 }
617 631
618 if (!external_hmac) { 632 if (!external_hmac) {
619 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!."; 633 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!.";
620 return false; 634 return false;
621 } 635 }
622 636
623 *key = external_hmac->key; 637 *key = external_hmac->key;
624 *key_len = external_hmac->key_length; 638 *key_len = external_hmac->key_length;
625 *tag_len = rtp_auth_tag_len_; 639 *tag_len = rtp_auth_tag_len_;
626 return true; 640 return true;
627 #else
628 return false;
629 #endif
630 } 641 }
631 642
632 int SrtpSession::GetSrtpOverhead() const { 643 int SrtpSession::GetSrtpOverhead() const {
633 return rtp_auth_tag_len_; 644 return rtp_auth_tag_len_;
634 } 645 }
635 646
636 #if defined(ENABLE_EXTERNAL_AUTH) 647 void SrtpSession::AllowExternalAuthForTest(bool allow) {
648 external_auth_allowed_ = allow;
649 }
650
651 bool SrtpSession::IsExternalAuthAllowed() const {
652 return external_auth_allowed_;
653 }
654
637 bool SrtpSession::IsExternalAuthActive() const { 655 bool SrtpSession::IsExternalAuthActive() const {
638 return external_auth_active_; 656 return external_auth_active_;
639 } 657 }
640 #endif
641 658
642 bool SrtpSession::GetSendStreamPacketIndex(void* p, 659 bool SrtpSession::GetSendStreamPacketIndex(void* p,
643 int in_len, 660 int in_len,
644 int64_t* index) { 661 int64_t* index) {
645 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 662 RTC_DCHECK(thread_checker_.CalledOnValidThread());
646 srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p); 663 srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p);
647 srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc); 664 srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc);
648 if (!stream) { 665 if (!stream) {
649 return false; 666 return false;
650 } 667 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type); 729 policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type);
713 policy.ssrc.value = 0; 730 policy.ssrc.value = 0;
714 policy.key = const_cast<uint8_t*>(key); 731 policy.key = const_cast<uint8_t*>(key);
715 // TODO(astor) parse window size from WSH session-param 732 // TODO(astor) parse window size from WSH session-param
716 policy.window_size = 1024; 733 policy.window_size = 1024;
717 policy.allow_repeat_tx = 1; 734 policy.allow_repeat_tx = 1;
718 // If external authentication option is enabled, supply custom auth module 735 // If external authentication option is enabled, supply custom auth module
719 // id EXTERNAL_HMAC_SHA1 in the policy structure. 736 // id EXTERNAL_HMAC_SHA1 in the policy structure.
720 // We want to set this option only for rtp packets. 737 // We want to set this option only for rtp packets.
721 // By default policy structure is initialized to HMAC_SHA1. 738 // 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 739 // Enable external HMAC authentication only for outgoing streams and only
724 // for cipher suites that support it (i.e. only non-GCM cipher suites). 740 // for cipher suites that support it (i.e. only non-GCM cipher suites).
725 if (type == ssrc_any_outbound && !rtc::IsGcmCryptoSuite(cs)) { 741 if (type == ssrc_any_outbound && IsExternalAuthAllowed() &&
742 !rtc::IsGcmCryptoSuite(cs)) {
726 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1; 743 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1;
727 } 744 }
728 #endif
729 policy.next = nullptr; 745 policy.next = nullptr;
730 746
731 int err = srtp_create(&session_, &policy); 747 int err = srtp_create(&session_, &policy);
732 if (err != srtp_err_status_ok) { 748 if (err != srtp_err_status_ok) {
733 session_ = nullptr; 749 session_ = nullptr;
734 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err; 750 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err;
735 return false; 751 return false;
736 } 752 }
737 753
738 srtp_set_user_data(session_, this); 754 srtp_set_user_data(session_, this);
739 rtp_auth_tag_len_ = policy.rtp.auth_tag_len; 755 rtp_auth_tag_len_ = policy.rtp.auth_tag_len;
740 rtcp_auth_tag_len_ = policy.rtcp.auth_tag_len; 756 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); 757 external_auth_active_ = (policy.rtp.auth_type == EXTERNAL_HMAC_SHA1);
743 #endif
744 return true; 758 return true;
745 } 759 }
746 760
747 bool SrtpSession::Init() { 761 bool SrtpSession::Init() {
748 rtc::GlobalLockScope ls(&lock_); 762 rtc::GlobalLockScope ls(&lock_);
749 763
750 if (!inited_) { 764 if (!inited_) {
751 int err; 765 int err;
752 err = srtp_init(); 766 err = srtp_init();
753 if (err != srtp_err_status_ok) { 767 if (err != srtp_err_status_ok) {
754 LOG(LS_ERROR) << "Failed to init SRTP, err=" << err; 768 LOG(LS_ERROR) << "Failed to init SRTP, err=" << err;
755 return false; 769 return false;
756 } 770 }
757 771
758 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk); 772 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk);
759 if (err != srtp_err_status_ok) { 773 if (err != srtp_err_status_ok) {
760 LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err; 774 LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err;
761 return false; 775 return false;
762 } 776 }
763 #if defined(ENABLE_EXTERNAL_AUTH) 777
764 err = external_crypto_init(); 778 err = external_crypto_init();
765 if (err != srtp_err_status_ok) { 779 if (err != srtp_err_status_ok) {
766 LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err; 780 LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err;
767 return false; 781 return false;
768 } 782 }
769 #endif
770 inited_ = true; 783 inited_ = true;
771 } 784 }
772 785
773 return true; 786 return true;
774 } 787 }
775 788
776 void SrtpSession::Terminate() { 789 void SrtpSession::Terminate() {
777 rtc::GlobalLockScope ls(&lock_); 790 rtc::GlobalLockScope ls(&lock_);
778 791
779 if (inited_) { 792 if (inited_) {
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 SrtpNotAvailable(__FUNCTION__); 970 SrtpNotAvailable(__FUNCTION__);
958 } 971 }
959 972
960 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) { 973 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) {
961 SrtpNotAvailable(__FUNCTION__); 974 SrtpNotAvailable(__FUNCTION__);
962 } 975 }
963 976
964 #endif // HAVE_SRTP 977 #endif // HAVE_SRTP
965 978
966 } // namespace cricket 979 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698