| 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 |
| 11 #include "webrtc/pc/srtpfilter.h" | 11 #include "webrtc/pc/srtpfilter.h" |
| 12 | 12 |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include <algorithm> | 15 #include <algorithm> |
| 16 | 16 |
| 17 #include "third_party/libsrtp/include/srtp.h" | 17 #include "third_party/libsrtp/include/srtp.h" |
| 18 #include "third_party/libsrtp/include/srtp_priv.h" | 18 #include "third_party/libsrtp/include/srtp_priv.h" |
| 19 #include "webrtc/base/base64.h" | 19 #include "webrtc/base/base64.h" |
| 20 #include "webrtc/base/buffer.h" | 20 #include "webrtc/base/buffer.h" |
| 21 #include "webrtc/base/byteorder.h" | 21 #include "webrtc/base/byteorder.h" |
| 22 #include "webrtc/base/checks.h" | 22 #include "webrtc/base/checks.h" |
| 23 #include "webrtc/base/logging.h" | 23 #include "webrtc/base/logging.h" |
| 24 #include "webrtc/base/sslstreamadapter.h" |
| 24 #include "webrtc/base/stringencode.h" | 25 #include "webrtc/base/stringencode.h" |
| 25 #include "webrtc/base/timeutils.h" | 26 #include "webrtc/base/timeutils.h" |
| 26 #include "webrtc/media/base/rtputils.h" | 27 #include "webrtc/media/base/rtputils.h" |
| 27 #include "webrtc/pc/externalhmac.h" | 28 #include "webrtc/pc/externalhmac.h" |
| 28 | 29 |
| 29 namespace cricket { | 30 namespace cricket { |
| 30 | 31 |
| 31 #ifndef HAVE_SRTP | 32 #ifndef HAVE_SRTP |
| 32 | 33 |
| 33 // This helper function is used on systems that don't (yet) have SRTP, | 34 // This helper function is used on systems that don't (yet) have SRTP, |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 if (!IsActive()) { | 219 if (!IsActive()) { |
| 219 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active"; | 220 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active"; |
| 220 return false; | 221 return false; |
| 221 } | 222 } |
| 222 | 223 |
| 223 RTC_CHECK(send_session_); | 224 RTC_CHECK(send_session_); |
| 224 *srtp_overhead = send_session_->GetSrtpOverhead(); | 225 *srtp_overhead = send_session_->GetSrtpOverhead(); |
| 225 return true; | 226 return true; |
| 226 } | 227 } |
| 227 | 228 |
| 229 #if defined(ENABLE_EXTERNAL_AUTH) |
| 230 bool SrtpFilter::IsExternalAuthActive() const { |
| 231 if (!IsActive()) { |
| 232 LOG(LS_WARNING) << "Failed to check IsExternalAuthActive: SRTP not active"; |
| 233 return false; |
| 234 } |
| 235 |
| 236 RTC_CHECK(send_session_); |
| 237 return send_session_->IsExternalAuthActive(); |
| 238 } |
| 239 #endif |
| 240 |
| 228 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) { |
| 229 signal_silent_time_in_ms_ = signal_silent_time_in_ms; | 242 signal_silent_time_in_ms_ = signal_silent_time_in_ms; |
| 230 if (IsActive()) { | 243 if (IsActive()) { |
| 231 RTC_CHECK(send_session_); | 244 RTC_CHECK(send_session_); |
| 232 send_session_->set_signal_silent_time(signal_silent_time_in_ms); | 245 send_session_->set_signal_silent_time(signal_silent_time_in_ms); |
| 233 RTC_CHECK(recv_session_); | 246 RTC_CHECK(recv_session_); |
| 234 recv_session_->set_signal_silent_time(signal_silent_time_in_ms); | 247 recv_session_->set_signal_silent_time(signal_silent_time_in_ms); |
| 235 if (send_rtcp_session_) | 248 if (send_rtcp_session_) |
| 236 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); |
| 237 if (recv_rtcp_session_) | 250 if (recv_rtcp_session_) |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 /////////////////////////////////////////////////////////////////////////////// | 468 /////////////////////////////////////////////////////////////////////////////// |
| 456 // SrtpSession | 469 // SrtpSession |
| 457 | 470 |
| 458 #ifdef HAVE_SRTP | 471 #ifdef HAVE_SRTP |
| 459 | 472 |
| 460 bool SrtpSession::inited_ = false; | 473 bool SrtpSession::inited_ = false; |
| 461 | 474 |
| 462 // This lock protects SrtpSession::inited_. | 475 // This lock protects SrtpSession::inited_. |
| 463 rtc::GlobalLockPod SrtpSession::lock_; | 476 rtc::GlobalLockPod SrtpSession::lock_; |
| 464 | 477 |
| 465 SrtpSession::SrtpSession() | 478 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); | 479 SignalSrtpError.repeat(srtp_stat_->SignalSrtpError); |
| 472 } | 480 } |
| 473 | 481 |
| 474 SrtpSession::~SrtpSession() { | 482 SrtpSession::~SrtpSession() { |
| 475 if (session_) { | 483 if (session_) { |
| 476 srtp_set_user_data(session_, nullptr); | 484 srtp_set_user_data(session_, nullptr); |
| 477 srtp_dealloc(session_); | 485 srtp_dealloc(session_); |
| 478 } | 486 } |
| 479 } | 487 } |
| 480 | 488 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 if (err != srtp_err_status_ok) { | 594 if (err != srtp_err_status_ok) { |
| 587 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err; | 595 LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err; |
| 588 return false; | 596 return false; |
| 589 } | 597 } |
| 590 return true; | 598 return true; |
| 591 } | 599 } |
| 592 | 600 |
| 593 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) { |
| 594 #if defined(ENABLE_EXTERNAL_AUTH) | 602 #if defined(ENABLE_EXTERNAL_AUTH) |
| 595 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 603 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 604 RTC_DCHECK(IsExternalAuthActive()); |
| 605 if (!IsExternalAuthActive()) { |
| 606 return false; |
| 607 } |
| 608 |
| 596 ExternalHmacContext* external_hmac = nullptr; | 609 ExternalHmacContext* external_hmac = nullptr; |
| 597 // stream_template will be the reference context for other streams. | 610 // stream_template will be the reference context for other streams. |
| 598 // Let's use it for getting the keys. | 611 // Let's use it for getting the keys. |
| 599 srtp_stream_ctx_t* srtp_context = session_->stream_template; | 612 srtp_stream_ctx_t* srtp_context = session_->stream_template; |
| 600 if (srtp_context && srtp_context->rtp_auth) { | 613 if (srtp_context && srtp_context->rtp_auth) { |
| 601 external_hmac = reinterpret_cast<ExternalHmacContext*>( | 614 external_hmac = reinterpret_cast<ExternalHmacContext*>( |
| 602 srtp_context->rtp_auth->state); | 615 srtp_context->rtp_auth->state); |
| 603 } | 616 } |
| 604 | 617 |
| 605 if (!external_hmac) { | 618 if (!external_hmac) { |
| 606 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!."; | 619 LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!."; |
| 607 return false; | 620 return false; |
| 608 } | 621 } |
| 609 | 622 |
| 610 *key = external_hmac->key; | 623 *key = external_hmac->key; |
| 611 *key_len = external_hmac->key_length; | 624 *key_len = external_hmac->key_length; |
| 612 *tag_len = rtp_auth_tag_len_; | 625 *tag_len = rtp_auth_tag_len_; |
| 613 return true; | 626 return true; |
| 614 #else | 627 #else |
| 615 return false; | 628 return false; |
| 616 #endif | 629 #endif |
| 617 } | 630 } |
| 618 | 631 |
| 619 int SrtpSession::GetSrtpOverhead() const { | 632 int SrtpSession::GetSrtpOverhead() const { |
| 620 return rtp_auth_tag_len_; | 633 return rtp_auth_tag_len_; |
| 621 } | 634 } |
| 622 | 635 |
| 636 #if defined(ENABLE_EXTERNAL_AUTH) |
| 637 bool SrtpSession::IsExternalAuthActive() const { |
| 638 return external_auth_active_; |
| 639 } |
| 640 #endif |
| 641 |
| 623 bool SrtpSession::GetSendStreamPacketIndex(void* p, | 642 bool SrtpSession::GetSendStreamPacketIndex(void* p, |
| 624 int in_len, | 643 int in_len, |
| 625 int64_t* index) { | 644 int64_t* index) { |
| 626 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 645 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 627 srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p); | 646 srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p); |
| 628 srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc); | 647 srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc); |
| 629 if (!stream) { | 648 if (!stream) { |
| 630 return false; | 649 return false; |
| 631 } | 650 } |
| 632 | 651 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 655 | 674 |
| 656 srtp_policy_t policy; | 675 srtp_policy_t policy; |
| 657 memset(&policy, 0, sizeof(policy)); | 676 memset(&policy, 0, sizeof(policy)); |
| 658 if (cs == rtc::SRTP_AES128_CM_SHA1_80) { | 677 if (cs == rtc::SRTP_AES128_CM_SHA1_80) { |
| 659 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp); | 678 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); | 679 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); |
| 661 } else if (cs == rtc::SRTP_AES128_CM_SHA1_32) { | 680 } else if (cs == rtc::SRTP_AES128_CM_SHA1_32) { |
| 662 // RTP HMAC is shortened to 32 bits, but RTCP remains 80 bits. | 681 // 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); | 682 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); | 683 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); |
| 665 #if !defined(ENABLE_EXTERNAL_AUTH) | |
| 666 // TODO(jbauch): Re-enable once https://crbug.com/628400 is resolved. | |
| 667 } else if (cs == rtc::SRTP_AEAD_AES_128_GCM) { | 684 } else if (cs == rtc::SRTP_AEAD_AES_128_GCM) { |
| 668 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtp); | 685 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtp); |
| 669 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtcp); | 686 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtcp); |
| 670 } else if (cs == rtc::SRTP_AEAD_AES_256_GCM) { | 687 } else if (cs == rtc::SRTP_AEAD_AES_256_GCM) { |
| 671 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtp); | 688 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtp); |
| 672 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtcp); | 689 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtcp); |
| 673 #endif // ENABLE_EXTERNAL_AUTH | |
| 674 } else { | 690 } else { |
| 675 LOG(LS_WARNING) << "Failed to create SRTP session: unsupported" | 691 LOG(LS_WARNING) << "Failed to create SRTP session: unsupported" |
| 676 << " cipher_suite " << cs; | 692 << " cipher_suite " << cs; |
| 677 return false; | 693 return false; |
| 678 } | 694 } |
| 679 | 695 |
| 680 int expected_key_len; | 696 int expected_key_len; |
| 681 int expected_salt_len; | 697 int expected_salt_len; |
| 682 if (!rtc::GetSrtpKeyAndSaltLengths(cs, &expected_key_len, | 698 if (!rtc::GetSrtpKeyAndSaltLengths(cs, &expected_key_len, |
| 683 &expected_salt_len)) { | 699 &expected_salt_len)) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 697 policy.ssrc.value = 0; | 713 policy.ssrc.value = 0; |
| 698 policy.key = const_cast<uint8_t*>(key); | 714 policy.key = const_cast<uint8_t*>(key); |
| 699 // TODO(astor) parse window size from WSH session-param | 715 // TODO(astor) parse window size from WSH session-param |
| 700 policy.window_size = 1024; | 716 policy.window_size = 1024; |
| 701 policy.allow_repeat_tx = 1; | 717 policy.allow_repeat_tx = 1; |
| 702 // If external authentication option is enabled, supply custom auth module | 718 // If external authentication option is enabled, supply custom auth module |
| 703 // id EXTERNAL_HMAC_SHA1 in the policy structure. | 719 // id EXTERNAL_HMAC_SHA1 in the policy structure. |
| 704 // We want to set this option only for rtp packets. | 720 // We want to set this option only for rtp packets. |
| 705 // By default policy structure is initialized to HMAC_SHA1. | 721 // By default policy structure is initialized to HMAC_SHA1. |
| 706 #if defined(ENABLE_EXTERNAL_AUTH) | 722 #if defined(ENABLE_EXTERNAL_AUTH) |
| 707 // Enable external HMAC authentication only for outgoing streams. | 723 // Enable external HMAC authentication only for outgoing streams and only |
| 708 if (type == ssrc_any_outbound) { | 724 // for cipher suites that support it (i.e. only non-GCM cipher suites). |
| 725 if (type == ssrc_any_outbound && !rtc::IsGcmCryptoSuite(cs)) { |
| 709 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1; | 726 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1; |
| 710 } | 727 } |
| 711 #endif | 728 #endif |
| 712 policy.next = nullptr; | 729 policy.next = nullptr; |
| 713 | 730 |
| 714 int err = srtp_create(&session_, &policy); | 731 int err = srtp_create(&session_, &policy); |
| 715 if (err != srtp_err_status_ok) { | 732 if (err != srtp_err_status_ok) { |
| 716 session_ = nullptr; | 733 session_ = nullptr; |
| 717 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err; | 734 LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err; |
| 718 return false; | 735 return false; |
| 719 } | 736 } |
| 720 | 737 |
| 721 srtp_set_user_data(session_, this); | 738 srtp_set_user_data(session_, this); |
| 722 rtp_auth_tag_len_ = policy.rtp.auth_tag_len; | 739 rtp_auth_tag_len_ = policy.rtp.auth_tag_len; |
| 723 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) |
| 742 external_auth_active_ = (policy.rtp.auth_type == EXTERNAL_HMAC_SHA1); |
| 743 #endif |
| 724 return true; | 744 return true; |
| 725 } | 745 } |
| 726 | 746 |
| 727 bool SrtpSession::Init() { | 747 bool SrtpSession::Init() { |
| 728 rtc::GlobalLockScope ls(&lock_); | 748 rtc::GlobalLockScope ls(&lock_); |
| 729 | 749 |
| 730 if (!inited_) { | 750 if (!inited_) { |
| 731 int err; | 751 int err; |
| 732 err = srtp_init(); | 752 err = srtp_init(); |
| 733 if (err != srtp_err_status_ok) { | 753 if (err != srtp_err_status_ok) { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 937 SrtpNotAvailable(__FUNCTION__); | 957 SrtpNotAvailable(__FUNCTION__); |
| 938 } | 958 } |
| 939 | 959 |
| 940 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) { | 960 void SrtpStat::HandleSrtpResult(const SrtpStat::FailureKey& key) { |
| 941 SrtpNotAvailable(__FUNCTION__); | 961 SrtpNotAvailable(__FUNCTION__); |
| 942 } | 962 } |
| 943 | 963 |
| 944 #endif // HAVE_SRTP | 964 #endif // HAVE_SRTP |
| 945 | 965 |
| 946 } // namespace cricket | 966 } // namespace cricket |
| OLD | NEW |