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