OLD | NEW |
---|---|
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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 return false; | 197 return false; |
198 } | 198 } |
199 if (recv_rtcp_session_) { | 199 if (recv_rtcp_session_) { |
200 return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len); | 200 return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len); |
201 } else { | 201 } else { |
202 RTC_CHECK(recv_session_); | 202 RTC_CHECK(recv_session_); |
203 return recv_session_->UnprotectRtcp(p, in_len, out_len); | 203 return recv_session_->UnprotectRtcp(p, in_len, out_len); |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 bool SrtpFilter::AllowExternalAuth() { | |
208 if (!IsActive()) { | |
209 LOG(LS_WARNING) << "Failed to AllowExternalAuth: SRTP not active"; | |
210 return false; | |
Taylor Brandstetter
2017/02/28 22:53:40
Could DCHECK here.
joachim
2017/03/01 00:43:46
I didn't DCHECK to stay consistent with the existi
Taylor Brandstetter
2017/03/01 01:45:32
Acknowledged.
| |
211 } | |
212 | |
213 RTC_CHECK(send_session_); | |
214 return send_session_->AllowExternalAuth(); | |
215 } | |
216 | |
217 void SrtpFilter::DisableAllowExternalAuthForTests( | |
218 bool disable_allow_external_auth) { | |
219 force_disable_allow_external_auth_ = disable_allow_external_auth; | |
220 if (!IsActive()) { | |
221 return; | |
222 } | |
223 | |
224 RTC_CHECK(send_session_); | |
225 send_session_->DisableAllowExternalAuthForTests(disable_allow_external_auth); | |
226 } | |
227 | |
207 bool SrtpFilter::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) { | 228 bool SrtpFilter::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) { |
208 if (!IsActive()) { | 229 if (!IsActive()) { |
209 LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active"; | 230 LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active"; |
210 return false; | 231 return false; |
211 } | 232 } |
212 | 233 |
213 RTC_CHECK(send_session_); | 234 RTC_CHECK(send_session_); |
214 return send_session_->GetRtpAuthParams(key, key_len, tag_len); | 235 return send_session_->GetRtpAuthParams(key, key_len, tag_len); |
215 } | 236 } |
216 | 237 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 send_session_.reset(new SrtpSession()); | 339 send_session_.reset(new SrtpSession()); |
319 applied_send_params_ = CryptoParams(); | 340 applied_send_params_ = CryptoParams(); |
320 recv_session_.reset(new SrtpSession()); | 341 recv_session_.reset(new SrtpSession()); |
321 applied_recv_params_ = CryptoParams(); | 342 applied_recv_params_ = CryptoParams(); |
322 | 343 |
323 SignalSrtpError.repeat(send_session_->SignalSrtpError); | 344 SignalSrtpError.repeat(send_session_->SignalSrtpError); |
324 SignalSrtpError.repeat(recv_session_->SignalSrtpError); | 345 SignalSrtpError.repeat(recv_session_->SignalSrtpError); |
325 | 346 |
326 send_session_->set_signal_silent_time(signal_silent_time_in_ms_); | 347 send_session_->set_signal_silent_time(signal_silent_time_in_ms_); |
327 recv_session_->set_signal_silent_time(signal_silent_time_in_ms_); | 348 recv_session_->set_signal_silent_time(signal_silent_time_in_ms_); |
349 | |
350 send_session_->DisableAllowExternalAuthForTests( | |
351 force_disable_allow_external_auth_); | |
328 } | 352 } |
329 | 353 |
330 bool SrtpFilter::NegotiateParams(const std::vector<CryptoParams>& answer_params, | 354 bool SrtpFilter::NegotiateParams(const std::vector<CryptoParams>& answer_params, |
331 CryptoParams* selected_params) { | 355 CryptoParams* selected_params) { |
332 // We're processing an accept. We should have exactly one set of params, | 356 // We're processing an accept. We should have exactly one set of params, |
333 // unless the offer didn't mention crypto, in which case we shouldn't be here. | 357 // unless the offer didn't mention crypto, in which case we shouldn't be here. |
334 bool ret = (answer_params.size() == 1U && !offer_params_.empty()); | 358 bool ret = (answer_params.size() == 1U && !offer_params_.empty()); |
335 if (ret) { | 359 if (ret) { |
336 // We should find a match between the answer params and the offered params. | 360 // We should find a match between the answer params and the offered params. |
337 std::vector<CryptoParams>::const_iterator it; | 361 std::vector<CryptoParams>::const_iterator it; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
455 /////////////////////////////////////////////////////////////////////////////// | 479 /////////////////////////////////////////////////////////////////////////////// |
456 // SrtpSession | 480 // SrtpSession |
457 | 481 |
458 #ifdef HAVE_SRTP | 482 #ifdef HAVE_SRTP |
459 | 483 |
460 bool SrtpSession::inited_ = false; | 484 bool SrtpSession::inited_ = false; |
461 | 485 |
462 // This lock protects SrtpSession::inited_. | 486 // This lock protects SrtpSession::inited_. |
463 rtc::GlobalLockPod SrtpSession::lock_; | 487 rtc::GlobalLockPod SrtpSession::lock_; |
464 | 488 |
465 SrtpSession::SrtpSession() | 489 SrtpSession::SrtpSession() : srtp_stat_(new SrtpStat()) { |
466 : session_(nullptr), | |
467 rtp_auth_tag_len_(0), | |
468 rtcp_auth_tag_len_(0), | |
469 srtp_stat_(new SrtpStat()), | |
470 last_send_seq_num_(-1) { | |
471 SignalSrtpError.repeat(srtp_stat_->SignalSrtpError); | 490 SignalSrtpError.repeat(srtp_stat_->SignalSrtpError); |
472 } | 491 } |
473 | 492 |
474 SrtpSession::~SrtpSession() { | 493 SrtpSession::~SrtpSession() { |
475 if (session_) { | 494 if (session_) { |
476 srtp_set_user_data(session_, nullptr); | 495 srtp_set_user_data(session_, nullptr); |
477 srtp_dealloc(session_); | 496 srtp_dealloc(session_); |
478 } | 497 } |
479 } | 498 } |
480 | 499 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
583 *out_len = in_len; | 602 *out_len = in_len; |
584 int err = srtp_unprotect_rtcp(session_, p, out_len); | 603 int err = srtp_unprotect_rtcp(session_, p, out_len); |
585 srtp_stat_->AddUnprotectRtcpResult(err); | 604 srtp_stat_->AddUnprotectRtcpResult(err); |
586 if (err != srtp_err_status_ok) { | 605 if (err != srtp_err_status_ok) { |
587 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err; | 606 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err; |
588 return false; | 607 return false; |
589 } | 608 } |
590 return true; | 609 return true; |
591 } | 610 } |
592 | 611 |
612 bool SrtpSession::AllowExternalAuth() { | |
Taylor Brandstetter
2017/02/28 22:53:40
"AllowExternalAuth" implies some action, but this
joachim
2017/03/01 00:43:46
Renamed to "IsExternalAuthActive".
| |
613 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
614 return allow_external_auth_ && !force_disable_allow_external_auth_; | |
615 } | |
616 | |
617 void SrtpSession::DisableAllowExternalAuthForTests( | |
618 bool disable_allow_external_auth) { | |
619 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
620 force_disable_allow_external_auth_ = disable_allow_external_auth; | |
621 } | |
622 | |
593 bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) { | 623 bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) { |
594 #if defined(ENABLE_EXTERNAL_AUTH) | |
595 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 624 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
625 if (!AllowExternalAuth()) { | |
Taylor Brandstetter
2017/02/28 22:53:40
DCHECK?
joachim
2017/03/01 00:43:46
Done.
| |
626 return false; | |
627 } | |
628 | |
596 ExternalHmacContext* external_hmac = nullptr; | 629 ExternalHmacContext* external_hmac = nullptr; |
597 // stream_template will be the reference context for other streams. | 630 // stream_template will be the reference context for other streams. |
598 // Let's use it for getting the keys. | 631 // Let's use it for getting the keys. |
599 srtp_stream_ctx_t* srtp_context = session_->stream_template; | 632 srtp_stream_ctx_t* srtp_context = session_->stream_template; |
600 if (srtp_context && srtp_context->rtp_auth) { | 633 if (srtp_context && srtp_context->rtp_auth) { |
601 external_hmac = reinterpret_cast<ExternalHmacContext*>( | 634 external_hmac = reinterpret_cast<ExternalHmacContext*>( |
602 srtp_context->rtp_auth->state); | 635 srtp_context->rtp_auth->state); |
603 } | 636 } |
604 | 637 |
605 if (!external_hmac) { | 638 if (!external_hmac) { |
606 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!."; | 639 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!."; |
607 return false; | 640 return false; |
608 } | 641 } |
609 | 642 |
610 *key = external_hmac->key; | 643 *key = external_hmac->key; |
611 *key_len = external_hmac->key_length; | 644 *key_len = external_hmac->key_length; |
612 *tag_len = rtp_auth_tag_len_; | 645 *tag_len = rtp_auth_tag_len_; |
613 return true; | 646 return true; |
614 #else | |
615 return false; | |
616 #endif | |
617 } | 647 } |
618 | 648 |
619 int SrtpSession::GetSrtpOverhead() const { | 649 int SrtpSession::GetSrtpOverhead() const { |
620 return rtp_auth_tag_len_; | 650 return rtp_auth_tag_len_; |
621 } | 651 } |
622 | 652 |
623 bool SrtpSession::GetSendStreamPacketIndex(void* p, | 653 bool SrtpSession::GetSendStreamPacketIndex(void* p, |
624 int in_len, | 654 int in_len, |
625 int64_t* index) { | 655 int64_t* index) { |
626 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 656 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 24 matching lines...) Expand all Loading... | |
651 | 681 |
652 if (!Init()) { | 682 if (!Init()) { |
653 return false; | 683 return false; |
654 } | 684 } |
655 | 685 |
656 srtp_policy_t policy; | 686 srtp_policy_t policy; |
657 memset(&policy, 0, sizeof(policy)); | 687 memset(&policy, 0, sizeof(policy)); |
658 if (cs == rtc::SRTP_AES128_CM_SHA1_80) { | 688 if (cs == rtc::SRTP_AES128_CM_SHA1_80) { |
659 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp); | 689 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp); |
660 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); | 690 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); |
691 allow_external_auth_ = true; | |
661 } else if (cs == rtc::SRTP_AES128_CM_SHA1_32) { | 692 } else if (cs == rtc::SRTP_AES128_CM_SHA1_32) { |
662 // RTP HMAC is shortened to 32 bits, but RTCP remains 80 bits. | 693 // RTP HMAC is shortened to 32 bits, but RTCP remains 80 bits. |
663 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtp); | 694 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtp); |
664 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); | 695 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); |
665 #if !defined(ENABLE_EXTERNAL_AUTH) | 696 allow_external_auth_ = true; |
666 // TODO(jbauch): Re-enable once https://crbug.com/628400 is resolved. | |
667 } else if (cs == rtc::SRTP_AEAD_AES_128_GCM) { | 697 } else if (cs == rtc::SRTP_AEAD_AES_128_GCM) { |
668 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtp); | 698 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtp); |
669 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtcp); | 699 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtcp); |
700 allow_external_auth_ = false; | |
670 } else if (cs == rtc::SRTP_AEAD_AES_256_GCM) { | 701 } else if (cs == rtc::SRTP_AEAD_AES_256_GCM) { |
671 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtp); | 702 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtp); |
672 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtcp); | 703 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtcp); |
673 #endif // ENABLE_EXTERNAL_AUTH | 704 allow_external_auth_ = false; |
674 } else { | 705 } else { |
675 LOG(LS_WARNING) << "Failed to create SRTP session: unsupported" | 706 LOG(LS_WARNING) << "Failed to create SRTP session: unsupported" |
676 << " cipher_suite " << cs; | 707 << " cipher_suite " << cs; |
677 return false; | 708 return false; |
678 } | 709 } |
679 | 710 |
680 int expected_key_len; | 711 int expected_key_len; |
681 int expected_salt_len; | 712 int expected_salt_len; |
682 if (!rtc::GetSrtpKeyAndSaltLengths(cs, &expected_key_len, | 713 if (!rtc::GetSrtpKeyAndSaltLengths(cs, &expected_key_len, |
683 &expected_salt_len)) { | 714 &expected_salt_len)) { |
(...skipping 12 matching lines...) Expand all Loading... | |
696 policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type); | 727 policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type); |
697 policy.ssrc.value = 0; | 728 policy.ssrc.value = 0; |
698 policy.key = const_cast<uint8_t*>(key); | 729 policy.key = const_cast<uint8_t*>(key); |
699 // TODO(astor) parse window size from WSH session-param | 730 // TODO(astor) parse window size from WSH session-param |
700 policy.window_size = 1024; | 731 policy.window_size = 1024; |
701 policy.allow_repeat_tx = 1; | 732 policy.allow_repeat_tx = 1; |
702 // If external authentication option is enabled, supply custom auth module | 733 // If external authentication option is enabled, supply custom auth module |
703 // id EXTERNAL_HMAC_SHA1 in the policy structure. | 734 // id EXTERNAL_HMAC_SHA1 in the policy structure. |
704 // We want to set this option only for rtp packets. | 735 // We want to set this option only for rtp packets. |
705 // By default policy structure is initialized to HMAC_SHA1. | 736 // By default policy structure is initialized to HMAC_SHA1. |
706 #if defined(ENABLE_EXTERNAL_AUTH) | |
707 // Enable external HMAC authentication only for outgoing streams. | 737 // Enable external HMAC authentication only for outgoing streams. |
708 if (type == ssrc_any_outbound) { | 738 if (AllowExternalAuth() && type == ssrc_any_outbound) { |
709 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1; | 739 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1; |
710 } | 740 } |
711 #endif | |
712 policy.next = nullptr; | 741 policy.next = nullptr; |
713 | 742 |
714 int err = srtp_create(&session_, &policy); | 743 int err = srtp_create(&session_, &policy); |
715 if (err != srtp_err_status_ok) { | 744 if (err != srtp_err_status_ok) { |
716 session_ = nullptr; | 745 session_ = nullptr; |
717 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err; | 746 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err; |
718 return false; | 747 return false; |
719 } | 748 } |
720 | 749 |
721 srtp_set_user_data(session_, this); | 750 srtp_set_user_data(session_, this); |
(...skipping 11 matching lines...) Expand all Loading... | |
733 if (err != srtp_err_status_ok) { | 762 if (err != srtp_err_status_ok) { |
734 LOG(LS_ERROR) << "Failed to init SRTP, err=" << err; | 763 LOG(LS_ERROR) << "Failed to init SRTP, err=" << err; |
735 return false; | 764 return false; |
736 } | 765 } |
737 | 766 |
738 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk); | 767 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk); |
739 if (err != srtp_err_status_ok) { | 768 if (err != srtp_err_status_ok) { |
740 LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err; | 769 LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err; |
741 return false; | 770 return false; |
742 } | 771 } |
743 #if defined(ENABLE_EXTERNAL_AUTH) | 772 |
744 err = external_crypto_init(); | 773 err = external_crypto_init(); |
745 if (err != srtp_err_status_ok) { | 774 if (err != srtp_err_status_ok) { |
746 LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err; | 775 LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err; |
747 return false; | 776 return false; |
748 } | 777 } |
749 #endif | 778 |
750 inited_ = true; | 779 inited_ = true; |
751 } | 780 } |
752 | 781 |
753 return true; | 782 return true; |
754 } | 783 } |
755 | 784 |
756 void SrtpSession::Terminate() { | 785 void SrtpSession::Terminate() { |
757 rtc::GlobalLockScope ls(&lock_); | 786 rtc::GlobalLockScope ls(&lock_); |
758 | 787 |
759 if (inited_) { | 788 if (inited_) { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
937 SrtpNotAvailable(__FUNCTION__); | 966 SrtpNotAvailable(__FUNCTION__); |
938 } | 967 } |
939 | 968 |
940 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) { | 969 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) { |
941 SrtpNotAvailable(__FUNCTION__); | 970 SrtpNotAvailable(__FUNCTION__); |
942 } | 971 } |
943 | 972 |
944 #endif // HAVE_SRTP | 973 #endif // HAVE_SRTP |
945 | 974 |
946 } // namespace cricket | 975 } // namespace cricket |
OLD | NEW |