blob: a07848d475c5a33352858a1d4b64b23b3764cd53 [file] [log] [blame]
zstein4dde3df2017-07-07 14:26:25 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/srtpsession.h"
zstein4dde3df2017-07-07 14:26:25 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "media/base/rtputils.h"
14#include "pc/externalhmac.h"
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -070015#include "rtc_base/criticalsection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/logging.h"
17#include "rtc_base/sslstreamadapter.h"
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -070018#include "third_party/libsrtp/include/srtp.h"
19#include "third_party/libsrtp/include/srtp_priv.h"
zstein4dde3df2017-07-07 14:26:25 -070020
21namespace cricket {
22
zstein4dde3df2017-07-07 14:26:25 -070023SrtpSession::SrtpSession() {}
24
25SrtpSession::~SrtpSession() {
26 if (session_) {
27 srtp_set_user_data(session_, nullptr);
28 srtp_dealloc(session_);
29 }
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -070030 if (inited_) {
31 DecrementLibsrtpUsageCountAndMaybeDeinit();
32 }
zstein4dde3df2017-07-07 14:26:25 -070033}
34
Zhi Huangc99b6c72017-11-10 16:44:46 -080035bool 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);
zstein4dde3df2017-07-07 14:26:25 -070040}
41
Zhi Huangc99b6c72017-11-10 16:44:46 -080042bool 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);
zstein4dde3df2017-07-07 14:26:25 -070047}
48
Zhi Huangc99b6c72017-11-10 16:44:46 -080049bool 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);
zstein4dde3df2017-07-07 14:26:25 -070054}
55
Zhi Huangc99b6c72017-11-10 16:44:46 -080056bool 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);
zstein4dde3df2017-07-07 14:26:25 -070061}
62
63bool SrtpSession::ProtectRtp(void* p, int in_len, int max_len, int* out_len) {
64 RTC_DCHECK(thread_checker_.CalledOnValidThread());
65 if (!session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010066 RTC_LOG(LS_WARNING) << "Failed to protect SRTP packet: no SRTP Session";
zstein4dde3df2017-07-07 14:26:25 -070067 return false;
68 }
69
70 int need_len = in_len + rtp_auth_tag_len_; // NOLINT
71 if (max_len < need_len) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010072 RTC_LOG(LS_WARNING) << "Failed to protect SRTP packet: The buffer length "
73 << max_len << " is less than the needed " << need_len;
zstein4dde3df2017-07-07 14:26:25 -070074 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 Bonadei675513b2017-11-09 11:09:25 +010082 RTC_LOG(LS_WARNING) << "Failed to protect SRTP packet, seqnum=" << seq_num
83 << ", err=" << err
84 << ", last seqnum=" << last_send_seq_num_;
zstein4dde3df2017-07-07 14:26:25 -070085 return false;
86 }
87 last_send_seq_num_ = seq_num;
88 return true;
89}
90
91bool 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
102bool SrtpSession::ProtectRtcp(void* p, int in_len, int max_len, int* out_len) {
103 RTC_DCHECK(thread_checker_.CalledOnValidThread());
104 if (!session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100105 RTC_LOG(LS_WARNING) << "Failed to protect SRTCP packet: no SRTP Session";
zstein4dde3df2017-07-07 14:26:25 -0700106 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 Bonadei675513b2017-11-09 11:09:25 +0100111 RTC_LOG(LS_WARNING) << "Failed to protect SRTCP packet: The buffer length "
112 << max_len << " is less than the needed " << need_len;
zstein4dde3df2017-07-07 14:26:25 -0700113 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 Bonadei675513b2017-11-09 11:09:25 +0100119 RTC_LOG(LS_WARNING) << "Failed to protect SRTCP packet, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700120 return false;
121 }
122 return true;
123}
124
125bool SrtpSession::UnprotectRtp(void* p, int in_len, int* out_len) {
126 RTC_DCHECK(thread_checker_.CalledOnValidThread());
127 if (!session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100128 RTC_LOG(LS_WARNING) << "Failed to unprotect SRTP packet: no SRTP Session";
zstein4dde3df2017-07-07 14:26:25 -0700129 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 Bonadei675513b2017-11-09 11:09:25 +0100135 RTC_LOG(LS_WARNING) << "Failed to unprotect SRTP packet, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700136 return false;
137 }
138 return true;
139}
140
141bool SrtpSession::UnprotectRtcp(void* p, int in_len, int* out_len) {
142 RTC_DCHECK(thread_checker_.CalledOnValidThread());
143 if (!session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100144 RTC_LOG(LS_WARNING) << "Failed to unprotect SRTCP packet: no SRTP Session";
zstein4dde3df2017-07-07 14:26:25 -0700145 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 Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700152 return false;
153 }
154 return true;
155}
156
157bool 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;
zstein4dde3df2017-07-07 14:26:25 -0700168 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 }
zstein4dde3df2017-07-07 14:26:25 -0700173
174 if (!external_hmac) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100175 RTC_LOG(LS_ERROR) << "Failed to get auth keys from libsrtp!.";
zstein4dde3df2017-07-07 14:26:25 -0700176 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
185int SrtpSession::GetSrtpOverhead() const {
186 return rtp_auth_tag_len_;
187}
188
189void SrtpSession::EnableExternalAuth() {
190 RTC_DCHECK(!session_);
191 external_auth_enabled_ = true;
192}
193
194bool SrtpSession::IsExternalAuthEnabled() const {
195 return external_auth_enabled_;
196}
197
198bool SrtpSession::IsExternalAuthActive() const {
199 return external_auth_active_;
200}
201
202bool 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 Huangc99b6c72017-11-10 16:44:46 -0800218bool SrtpSession::DoSetKey(int type,
219 int cs,
220 const uint8_t* key,
221 size_t len,
222 const std::vector<int>& extension_ids) {
zstein4dde3df2017-07-07 14:26:25 -0700223 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 Bonadei675513b2017-11-09 11:09:25 +0100241 RTC_LOG(LS_WARNING) << "Failed to " << (session_ ? "update" : "create")
242 << " SRTP session: unsupported cipher_suite " << cs;
zstein4dde3df2017-07-07 14:26:25 -0700243 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 Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LS_WARNING)
zstein4dde3df2017-07-07 14:26:25 -0700252 << "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 Bonadei675513b2017-11-09 11:09:25 +0100260 RTC_LOG(LS_WARNING) << "Failed to " << (session_ ? "update" : "create")
261 << " SRTP session: invalid key";
zstein4dde3df2017-07-07 14:26:25 -0700262 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 Huangc99b6c72017-11-10 16:44:46 -0800281 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());
zstein4dde3df2017-07-07 14:26:25 -0700284 }
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 Bonadei675513b2017-11-09 11:09:25 +0100291 RTC_LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700292 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 Bonadei675513b2017-11-09 11:09:25 +0100298 RTC_LOG(LS_ERROR) << "Failed to update SRTP session, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700299 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 Huangc99b6c72017-11-10 16:44:46 -0800309bool SrtpSession::SetKey(int type,
310 int cs,
311 const uint8_t* key,
312 size_t len,
313 const std::vector<int>& extension_ids) {
zstein4dde3df2017-07-07 14:26:25 -0700314 RTC_DCHECK(thread_checker_.CalledOnValidThread());
315 if (session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100316 RTC_LOG(LS_ERROR) << "Failed to create SRTP session: "
317 << "SRTP session already created";
zstein4dde3df2017-07-07 14:26:25 -0700318 return false;
319 }
320
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700321 // 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 {
zstein4dde3df2017-07-07 14:26:25 -0700326 return false;
327 }
328
Zhi Huangc99b6c72017-11-10 16:44:46 -0800329 return DoSetKey(type, cs, key, len, extension_ids);
zstein4dde3df2017-07-07 14:26:25 -0700330}
331
Zhi Huangc99b6c72017-11-10 16:44:46 -0800332bool SrtpSession::UpdateKey(int type,
333 int cs,
334 const uint8_t* key,
335 size_t len,
336 const std::vector<int>& extension_ids) {
zstein4dde3df2017-07-07 14:26:25 -0700337 RTC_DCHECK(thread_checker_.CalledOnValidThread());
338 if (!session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100339 RTC_LOG(LS_ERROR) << "Failed to update non-existing SRTP session";
zstein4dde3df2017-07-07 14:26:25 -0700340 return false;
341 }
342
Zhi Huangc99b6c72017-11-10 16:44:46 -0800343 return DoSetKey(type, cs, key, len, extension_ids);
zstein4dde3df2017-07-07 14:26:25 -0700344}
345
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700346int g_libsrtp_usage_count = 0;
347rtc::GlobalLockPod g_libsrtp_lock;
zstein4dde3df2017-07-07 14:26:25 -0700348
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700349// static
350bool 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) {
zstein4dde3df2017-07-07 14:26:25 -0700355 int err;
356 err = srtp_init();
357 if (err != srtp_err_status_ok) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100358 RTC_LOG(LS_ERROR) << "Failed to init SRTP, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700359 return false;
360 }
361
362 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk);
363 if (err != srtp_err_status_ok) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100364 RTC_LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700365 return false;
366 }
367
368 err = external_crypto_init();
369 if (err != srtp_err_status_ok) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100370 RTC_LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700371 return false;
372 }
zstein4dde3df2017-07-07 14:26:25 -0700373 }
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700374 ++g_libsrtp_usage_count;
zstein4dde3df2017-07-07 14:26:25 -0700375 return true;
376}
377
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700378// static
379void SrtpSession::DecrementLibsrtpUsageCountAndMaybeDeinit() {
380 rtc::GlobalLockScope ls(&g_libsrtp_lock);
zstein4dde3df2017-07-07 14:26:25 -0700381
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700382 RTC_DCHECK_GE(g_libsrtp_usage_count, 1);
383 if (--g_libsrtp_usage_count == 0) {
zstein4dde3df2017-07-07 14:26:25 -0700384 int err = srtp_shutdown();
385 if (err) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100386 RTC_LOG(LS_ERROR) << "srtp_shutdown failed. err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700387 }
zstein4dde3df2017-07-07 14:26:25 -0700388 }
389}
390
391void SrtpSession::HandleEvent(const srtp_event_data_t* ev) {
392 RTC_DCHECK(thread_checker_.CalledOnValidThread());
393 switch (ev->event) {
394 case event_ssrc_collision:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100395 RTC_LOG(LS_INFO) << "SRTP event: SSRC collision";
zstein4dde3df2017-07-07 14:26:25 -0700396 break;
397 case event_key_soft_limit:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100398 RTC_LOG(LS_INFO) << "SRTP event: reached soft key usage limit";
zstein4dde3df2017-07-07 14:26:25 -0700399 break;
400 case event_key_hard_limit:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100401 RTC_LOG(LS_INFO) << "SRTP event: reached hard key usage limit";
zstein4dde3df2017-07-07 14:26:25 -0700402 break;
403 case event_packet_index_limit:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100404 RTC_LOG(LS_INFO)
405 << "SRTP event: reached hard packet limit (2^48 packets)";
zstein4dde3df2017-07-07 14:26:25 -0700406 break;
407 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100408 RTC_LOG(LS_INFO) << "SRTP event: unknown " << ev->event;
zstein4dde3df2017-07-07 14:26:25 -0700409 break;
410 }
411}
412
413void 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