zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/srtp_session.h" |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 12 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 13 | #include "media/base/rtp_utils.h" |
| 14 | #include "pc/external_hmac.h" |
| 15 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 17 | #include "rtc_base/ssl_stream_adapter.h" |
Zhi Huang | 0a5fdbb | 2018-06-14 17:40:19 | [diff] [blame] | 18 | #include "system_wrappers/include/metrics.h" |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 19 | #include "third_party/libsrtp/include/srtp.h" |
| 20 | #include "third_party/libsrtp/include/srtp_priv.h" |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 21 | |
| 22 | namespace cricket { |
| 23 | |
Zhi Huang | 0a5fdbb | 2018-06-14 17:40:19 | [diff] [blame] | 24 | // One more than the maximum libsrtp error code. Required by |
| 25 | // RTC_HISTOGRAM_ENUMERATION. Keep this in sync with srtp_error_status_t defined |
| 26 | // in srtp.h. |
| 27 | constexpr int kSrtpErrorCodeBoundary = 28; |
| 28 | |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 29 | SrtpSession::SrtpSession() {} |
| 30 | |
| 31 | SrtpSession::~SrtpSession() { |
| 32 | if (session_) { |
| 33 | srtp_set_user_data(session_, nullptr); |
| 34 | srtp_dealloc(session_); |
| 35 | } |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 36 | if (inited_) { |
| 37 | DecrementLibsrtpUsageCountAndMaybeDeinit(); |
| 38 | } |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 39 | } |
| 40 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 41 | bool SrtpSession::SetSend(int cs, |
| 42 | const uint8_t* key, |
| 43 | size_t len, |
| 44 | const std::vector<int>& extension_ids) { |
| 45 | return SetKey(ssrc_any_outbound, cs, key, len, extension_ids); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 46 | } |
| 47 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 48 | bool SrtpSession::UpdateSend(int cs, |
| 49 | const uint8_t* key, |
| 50 | size_t len, |
| 51 | const std::vector<int>& extension_ids) { |
| 52 | return UpdateKey(ssrc_any_outbound, cs, key, len, extension_ids); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 53 | } |
| 54 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 55 | bool SrtpSession::SetRecv(int cs, |
| 56 | const uint8_t* key, |
| 57 | size_t len, |
| 58 | const std::vector<int>& extension_ids) { |
| 59 | return SetKey(ssrc_any_inbound, cs, key, len, extension_ids); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 60 | } |
| 61 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 62 | bool SrtpSession::UpdateRecv(int cs, |
| 63 | const uint8_t* key, |
| 64 | size_t len, |
| 65 | const std::vector<int>& extension_ids) { |
| 66 | return UpdateKey(ssrc_any_inbound, cs, key, len, extension_ids); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool SrtpSession::ProtectRtp(void* p, int in_len, int max_len, int* out_len) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 70 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 71 | if (!session_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 72 | RTC_LOG(LS_WARNING) << "Failed to protect SRTP packet: no SRTP Session"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
| 76 | int need_len = in_len + rtp_auth_tag_len_; // NOLINT |
| 77 | if (max_len < need_len) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 78 | RTC_LOG(LS_WARNING) << "Failed to protect SRTP packet: The buffer length " |
| 79 | << max_len << " is less than the needed " << need_len; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | *out_len = in_len; |
| 84 | int err = srtp_protect(session_, p, out_len); |
| 85 | int seq_num; |
| 86 | GetRtpSeqNum(p, in_len, &seq_num); |
| 87 | if (err != srtp_err_status_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 88 | RTC_LOG(LS_WARNING) << "Failed to protect SRTP packet, seqnum=" << seq_num |
| 89 | << ", err=" << err |
| 90 | << ", last seqnum=" << last_send_seq_num_; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | last_send_seq_num_ = seq_num; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | bool SrtpSession::ProtectRtp(void* p, |
| 98 | int in_len, |
| 99 | int max_len, |
| 100 | int* out_len, |
| 101 | int64_t* index) { |
| 102 | if (!ProtectRtp(p, in_len, max_len, out_len)) { |
| 103 | return false; |
| 104 | } |
| 105 | return (index) ? GetSendStreamPacketIndex(p, in_len, index) : true; |
| 106 | } |
| 107 | |
| 108 | bool SrtpSession::ProtectRtcp(void* p, int in_len, int max_len, int* out_len) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 109 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 110 | if (!session_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 111 | RTC_LOG(LS_WARNING) << "Failed to protect SRTCP packet: no SRTP Session"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
| 115 | int need_len = in_len + sizeof(uint32_t) + rtcp_auth_tag_len_; // NOLINT |
| 116 | if (max_len < need_len) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 117 | RTC_LOG(LS_WARNING) << "Failed to protect SRTCP packet: The buffer length " |
| 118 | << max_len << " is less than the needed " << need_len; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
| 122 | *out_len = in_len; |
| 123 | int err = srtp_protect_rtcp(session_, p, out_len); |
| 124 | if (err != srtp_err_status_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 125 | RTC_LOG(LS_WARNING) << "Failed to protect SRTCP packet, err=" << err; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | bool SrtpSession::UnprotectRtp(void* p, int in_len, int* out_len) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 132 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 133 | if (!session_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 134 | RTC_LOG(LS_WARNING) << "Failed to unprotect SRTP packet: no SRTP Session"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | *out_len = in_len; |
| 139 | int err = srtp_unprotect(session_, p, out_len); |
| 140 | if (err != srtp_err_status_ok) { |
erikvarga@webrtc.org | d76a0fc | 2018-10-09 10:31:28 | [diff] [blame] | 141 | // Limit the error logging to avoid excessive logs when there are lots of |
| 142 | // bad packets. |
| 143 | const int kFailureLogThrottleCount = 100; |
| 144 | if (decryption_failure_count_ % kFailureLogThrottleCount == 0) { |
| 145 | RTC_LOG(LS_WARNING) << "Failed to unprotect SRTP packet, err=" << err |
| 146 | << ", previous failure count: " |
| 147 | << decryption_failure_count_; |
| 148 | } |
| 149 | ++decryption_failure_count_; |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 150 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SrtpUnprotectError", |
Zhi Huang | 0a5fdbb | 2018-06-14 17:40:19 | [diff] [blame] | 151 | static_cast<int>(err), kSrtpErrorCodeBoundary); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool SrtpSession::UnprotectRtcp(void* p, int in_len, int* out_len) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 158 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 159 | if (!session_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 160 | RTC_LOG(LS_WARNING) << "Failed to unprotect SRTCP packet: no SRTP Session"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 161 | return false; |
| 162 | } |
| 163 | |
| 164 | *out_len = in_len; |
| 165 | int err = srtp_unprotect_rtcp(session_, p, out_len); |
| 166 | if (err != srtp_err_status_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 167 | RTC_LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err; |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 168 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SrtcpUnprotectError", |
Zhi Huang | 0a5fdbb | 2018-06-14 17:40:19 | [diff] [blame] | 169 | static_cast<int>(err), kSrtpErrorCodeBoundary); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 176 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 177 | RTC_DCHECK(IsExternalAuthActive()); |
| 178 | if (!IsExternalAuthActive()) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | ExternalHmacContext* external_hmac = nullptr; |
| 183 | // stream_template will be the reference context for other streams. |
| 184 | // Let's use it for getting the keys. |
| 185 | srtp_stream_ctx_t* srtp_context = session_->stream_template; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 186 | if (srtp_context && srtp_context->session_keys && |
| 187 | srtp_context->session_keys->rtp_auth) { |
| 188 | external_hmac = reinterpret_cast<ExternalHmacContext*>( |
| 189 | srtp_context->session_keys->rtp_auth->state); |
| 190 | } |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 191 | |
| 192 | if (!external_hmac) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 193 | RTC_LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!."; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | |
| 197 | *key = external_hmac->key; |
| 198 | *key_len = external_hmac->key_length; |
| 199 | *tag_len = rtp_auth_tag_len_; |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | int SrtpSession::GetSrtpOverhead() const { |
| 204 | return rtp_auth_tag_len_; |
| 205 | } |
| 206 | |
| 207 | void SrtpSession::EnableExternalAuth() { |
| 208 | RTC_DCHECK(!session_); |
| 209 | external_auth_enabled_ = true; |
| 210 | } |
| 211 | |
| 212 | bool SrtpSession::IsExternalAuthEnabled() const { |
| 213 | return external_auth_enabled_; |
| 214 | } |
| 215 | |
| 216 | bool SrtpSession::IsExternalAuthActive() const { |
| 217 | return external_auth_active_; |
| 218 | } |
| 219 | |
| 220 | bool SrtpSession::GetSendStreamPacketIndex(void* p, |
| 221 | int in_len, |
| 222 | int64_t* index) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 223 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 224 | srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p); |
| 225 | srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc); |
| 226 | if (!stream) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | // Shift packet index, put into network byte order |
| 231 | *index = static_cast<int64_t>(rtc::NetworkToHost64( |
| 232 | srtp_rdbx_get_packet_index(&stream->rtp_rdbx) << 16)); |
| 233 | return true; |
| 234 | } |
| 235 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 236 | bool SrtpSession::DoSetKey(int type, |
| 237 | int cs, |
| 238 | const uint8_t* key, |
| 239 | size_t len, |
| 240 | const std::vector<int>& extension_ids) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 241 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 242 | |
| 243 | srtp_policy_t policy; |
| 244 | memset(&policy, 0, sizeof(policy)); |
| 245 | if (cs == rtc::SRTP_AES128_CM_SHA1_80) { |
| 246 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp); |
| 247 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); |
| 248 | } else if (cs == rtc::SRTP_AES128_CM_SHA1_32) { |
| 249 | // RTP HMAC is shortened to 32 bits, but RTCP remains 80 bits. |
| 250 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtp); |
| 251 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); |
| 252 | } else if (cs == rtc::SRTP_AEAD_AES_128_GCM) { |
| 253 | srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtp); |
| 254 | srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtcp); |
| 255 | } else if (cs == rtc::SRTP_AEAD_AES_256_GCM) { |
| 256 | srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtp); |
| 257 | srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtcp); |
| 258 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 259 | RTC_LOG(LS_WARNING) << "Failed to " << (session_ ? "update" : "create") |
| 260 | << " SRTP session: unsupported cipher_suite " << cs; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | int expected_key_len; |
| 265 | int expected_salt_len; |
| 266 | if (!rtc::GetSrtpKeyAndSaltLengths(cs, &expected_key_len, |
| 267 | &expected_salt_len)) { |
| 268 | // This should never happen. |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 269 | RTC_NOTREACHED(); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 270 | RTC_LOG(LS_WARNING) |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 271 | << "Failed to " << (session_ ? "update" : "create") |
| 272 | << " SRTP session: unsupported cipher_suite without length information" |
| 273 | << cs; |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | if (!key || |
| 278 | len != static_cast<size_t>(expected_key_len + expected_salt_len)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 279 | RTC_LOG(LS_WARNING) << "Failed to " << (session_ ? "update" : "create") |
| 280 | << " SRTP session: invalid key"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
| 284 | policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type); |
| 285 | policy.ssrc.value = 0; |
| 286 | policy.key = const_cast<uint8_t*>(key); |
| 287 | // TODO(astor) parse window size from WSH session-param |
| 288 | policy.window_size = 1024; |
| 289 | policy.allow_repeat_tx = 1; |
| 290 | // If external authentication option is enabled, supply custom auth module |
| 291 | // id EXTERNAL_HMAC_SHA1 in the policy structure. |
| 292 | // We want to set this option only for rtp packets. |
| 293 | // By default policy structure is initialized to HMAC_SHA1. |
| 294 | // Enable external HMAC authentication only for outgoing streams and only |
| 295 | // for cipher suites that support it (i.e. only non-GCM cipher suites). |
| 296 | if (type == ssrc_any_outbound && IsExternalAuthEnabled() && |
| 297 | !rtc::IsGcmCryptoSuite(cs)) { |
| 298 | policy.rtp.auth_type = EXTERNAL_HMAC_SHA1; |
| 299 | } |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 300 | if (!extension_ids.empty()) { |
| 301 | policy.enc_xtn_hdr = const_cast<int*>(&extension_ids[0]); |
| 302 | policy.enc_xtn_hdr_count = static_cast<int>(extension_ids.size()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 303 | } |
| 304 | policy.next = nullptr; |
| 305 | |
| 306 | if (!session_) { |
| 307 | int err = srtp_create(&session_, &policy); |
| 308 | if (err != srtp_err_status_ok) { |
| 309 | session_ = nullptr; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 310 | RTC_LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | srtp_set_user_data(session_, this); |
| 314 | } else { |
| 315 | int err = srtp_update(session_, &policy); |
| 316 | if (err != srtp_err_status_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 317 | RTC_LOG(LS_ERROR) << "Failed to update SRTP session, err=" << err; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | rtp_auth_tag_len_ = policy.rtp.auth_tag_len; |
| 323 | rtcp_auth_tag_len_ = policy.rtcp.auth_tag_len; |
| 324 | external_auth_active_ = (policy.rtp.auth_type == EXTERNAL_HMAC_SHA1); |
| 325 | return true; |
| 326 | } |
| 327 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 328 | bool SrtpSession::SetKey(int type, |
| 329 | int cs, |
| 330 | const uint8_t* key, |
| 331 | size_t len, |
| 332 | const std::vector<int>& extension_ids) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 333 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 334 | if (session_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 335 | RTC_LOG(LS_ERROR) << "Failed to create SRTP session: " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 336 | "SRTP session already created"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 340 | // This is the first time we need to actually interact with libsrtp, so |
| 341 | // initialize it if needed. |
| 342 | if (IncrementLibsrtpUsageCountAndMaybeInit()) { |
| 343 | inited_ = true; |
| 344 | } else { |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 345 | return false; |
| 346 | } |
| 347 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 348 | return DoSetKey(type, cs, key, len, extension_ids); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 349 | } |
| 350 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 351 | bool SrtpSession::UpdateKey(int type, |
| 352 | int cs, |
| 353 | const uint8_t* key, |
| 354 | size_t len, |
| 355 | const std::vector<int>& extension_ids) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 356 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 357 | if (!session_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 358 | RTC_LOG(LS_ERROR) << "Failed to update non-existing SRTP session"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 362 | return DoSetKey(type, cs, key, len, extension_ids); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 363 | } |
| 364 | |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 365 | int g_libsrtp_usage_count = 0; |
| 366 | rtc::GlobalLockPod g_libsrtp_lock; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 367 | |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 368 | // static |
| 369 | bool SrtpSession::IncrementLibsrtpUsageCountAndMaybeInit() { |
| 370 | rtc::GlobalLockScope ls(&g_libsrtp_lock); |
| 371 | |
| 372 | RTC_DCHECK_GE(g_libsrtp_usage_count, 0); |
| 373 | if (g_libsrtp_usage_count == 0) { |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 374 | int err; |
| 375 | err = srtp_init(); |
| 376 | if (err != srtp_err_status_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 377 | RTC_LOG(LS_ERROR) << "Failed to init SRTP, err=" << err; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | |
| 381 | err = srtp_install_event_handler(&SrtpSession::HandleEventThunk); |
| 382 | if (err != srtp_err_status_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 383 | RTC_LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
| 387 | err = external_crypto_init(); |
| 388 | if (err != srtp_err_status_ok) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 389 | RTC_LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 390 | return false; |
| 391 | } |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 392 | } |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 393 | ++g_libsrtp_usage_count; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 394 | return true; |
| 395 | } |
| 396 | |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 397 | // static |
| 398 | void SrtpSession::DecrementLibsrtpUsageCountAndMaybeDeinit() { |
| 399 | rtc::GlobalLockScope ls(&g_libsrtp_lock); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 400 | |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 401 | RTC_DCHECK_GE(g_libsrtp_usage_count, 1); |
| 402 | if (--g_libsrtp_usage_count == 0) { |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 403 | int err = srtp_shutdown(); |
| 404 | if (err) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 405 | RTC_LOG(LS_ERROR) << "srtp_shutdown failed. err=" << err; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 406 | } |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | void SrtpSession::HandleEvent(const srtp_event_data_t* ev) { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 411 | RTC_DCHECK(thread_checker_.IsCurrent()); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 412 | switch (ev->event) { |
| 413 | case event_ssrc_collision: |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 414 | RTC_LOG(LS_INFO) << "SRTP event: SSRC collision"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 415 | break; |
| 416 | case event_key_soft_limit: |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 417 | RTC_LOG(LS_INFO) << "SRTP event: reached soft key usage limit"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 418 | break; |
| 419 | case event_key_hard_limit: |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 420 | RTC_LOG(LS_INFO) << "SRTP event: reached hard key usage limit"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 421 | break; |
| 422 | case event_packet_index_limit: |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 423 | RTC_LOG(LS_INFO) |
| 424 | << "SRTP event: reached hard packet limit (2^48 packets)"; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 425 | break; |
| 426 | default: |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 427 | RTC_LOG(LS_INFO) << "SRTP event: unknown " << ev->event; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void SrtpSession::HandleEventThunk(srtp_event_data_t* ev) { |
| 433 | // Callback will be executed from same thread that calls the "srtp_protect" |
| 434 | // and "srtp_unprotect" functions. |
| 435 | SrtpSession* session = |
| 436 | static_cast<SrtpSession*>(srtp_get_user_data(ev->session)); |
| 437 | if (session) { |
| 438 | session->HandleEvent(ev); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | } // namespace cricket |