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