blob: 347b099a964e4c0be2ff1c59f20671758e216410 [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.
Jonas Olsson45cc8902018-02-13 10:37:07 +0100251 RTC_NOTREACHED();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100252 RTC_LOG(LS_WARNING)
zstein4dde3df2017-07-07 14:26:25 -0700253 << "Failed to " << (session_ ? "update" : "create")
254 << " SRTP session: unsupported cipher_suite without length information"
255 << cs;
256 return false;
257 }
258
259 if (!key ||
260 len != static_cast<size_t>(expected_key_len + expected_salt_len)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100261 RTC_LOG(LS_WARNING) << "Failed to " << (session_ ? "update" : "create")
262 << " SRTP session: invalid key";
zstein4dde3df2017-07-07 14:26:25 -0700263 return false;
264 }
265
266 policy.ssrc.type = static_cast<srtp_ssrc_type_t>(type);
267 policy.ssrc.value = 0;
268 policy.key = const_cast<uint8_t*>(key);
269 // TODO(astor) parse window size from WSH session-param
270 policy.window_size = 1024;
271 policy.allow_repeat_tx = 1;
272 // If external authentication option is enabled, supply custom auth module
273 // id EXTERNAL_HMAC_SHA1 in the policy structure.
274 // We want to set this option only for rtp packets.
275 // By default policy structure is initialized to HMAC_SHA1.
276 // Enable external HMAC authentication only for outgoing streams and only
277 // for cipher suites that support it (i.e. only non-GCM cipher suites).
278 if (type == ssrc_any_outbound && IsExternalAuthEnabled() &&
279 !rtc::IsGcmCryptoSuite(cs)) {
280 policy.rtp.auth_type = EXTERNAL_HMAC_SHA1;
281 }
Zhi Huangc99b6c72017-11-10 16:44:46 -0800282 if (!extension_ids.empty()) {
283 policy.enc_xtn_hdr = const_cast<int*>(&extension_ids[0]);
284 policy.enc_xtn_hdr_count = static_cast<int>(extension_ids.size());
zstein4dde3df2017-07-07 14:26:25 -0700285 }
286 policy.next = nullptr;
287
288 if (!session_) {
289 int err = srtp_create(&session_, &policy);
290 if (err != srtp_err_status_ok) {
291 session_ = nullptr;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100292 RTC_LOG(LS_ERROR) << "Failed to create SRTP session, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700293 return false;
294 }
295 srtp_set_user_data(session_, this);
296 } else {
297 int err = srtp_update(session_, &policy);
298 if (err != srtp_err_status_ok) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100299 RTC_LOG(LS_ERROR) << "Failed to update SRTP session, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700300 return false;
301 }
302 }
303
304 rtp_auth_tag_len_ = policy.rtp.auth_tag_len;
305 rtcp_auth_tag_len_ = policy.rtcp.auth_tag_len;
306 external_auth_active_ = (policy.rtp.auth_type == EXTERNAL_HMAC_SHA1);
307 return true;
308}
309
Zhi Huangc99b6c72017-11-10 16:44:46 -0800310bool SrtpSession::SetKey(int type,
311 int cs,
312 const uint8_t* key,
313 size_t len,
314 const std::vector<int>& extension_ids) {
zstein4dde3df2017-07-07 14:26:25 -0700315 RTC_DCHECK(thread_checker_.CalledOnValidThread());
316 if (session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100317 RTC_LOG(LS_ERROR) << "Failed to create SRTP session: "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100318 "SRTP session already created";
zstein4dde3df2017-07-07 14:26:25 -0700319 return false;
320 }
321
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700322 // This is the first time we need to actually interact with libsrtp, so
323 // initialize it if needed.
324 if (IncrementLibsrtpUsageCountAndMaybeInit()) {
325 inited_ = true;
326 } else {
zstein4dde3df2017-07-07 14:26:25 -0700327 return false;
328 }
329
Zhi Huangc99b6c72017-11-10 16:44:46 -0800330 return DoSetKey(type, cs, key, len, extension_ids);
zstein4dde3df2017-07-07 14:26:25 -0700331}
332
Zhi Huangc99b6c72017-11-10 16:44:46 -0800333bool SrtpSession::UpdateKey(int type,
334 int cs,
335 const uint8_t* key,
336 size_t len,
337 const std::vector<int>& extension_ids) {
zstein4dde3df2017-07-07 14:26:25 -0700338 RTC_DCHECK(thread_checker_.CalledOnValidThread());
339 if (!session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100340 RTC_LOG(LS_ERROR) << "Failed to update non-existing SRTP session";
zstein4dde3df2017-07-07 14:26:25 -0700341 return false;
342 }
343
Zhi Huangc99b6c72017-11-10 16:44:46 -0800344 return DoSetKey(type, cs, key, len, extension_ids);
zstein4dde3df2017-07-07 14:26:25 -0700345}
346
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700347int g_libsrtp_usage_count = 0;
348rtc::GlobalLockPod g_libsrtp_lock;
zstein4dde3df2017-07-07 14:26:25 -0700349
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700350// static
351bool SrtpSession::IncrementLibsrtpUsageCountAndMaybeInit() {
352 rtc::GlobalLockScope ls(&g_libsrtp_lock);
353
354 RTC_DCHECK_GE(g_libsrtp_usage_count, 0);
355 if (g_libsrtp_usage_count == 0) {
zstein4dde3df2017-07-07 14:26:25 -0700356 int err;
357 err = srtp_init();
358 if (err != srtp_err_status_ok) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100359 RTC_LOG(LS_ERROR) << "Failed to init SRTP, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700360 return false;
361 }
362
363 err = srtp_install_event_handler(&SrtpSession::HandleEventThunk);
364 if (err != srtp_err_status_ok) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100365 RTC_LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700366 return false;
367 }
368
369 err = external_crypto_init();
370 if (err != srtp_err_status_ok) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100371 RTC_LOG(LS_ERROR) << "Failed to initialize fake auth, err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700372 return false;
373 }
zstein4dde3df2017-07-07 14:26:25 -0700374 }
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700375 ++g_libsrtp_usage_count;
zstein4dde3df2017-07-07 14:26:25 -0700376 return true;
377}
378
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700379// static
380void SrtpSession::DecrementLibsrtpUsageCountAndMaybeDeinit() {
381 rtc::GlobalLockScope ls(&g_libsrtp_lock);
zstein4dde3df2017-07-07 14:26:25 -0700382
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700383 RTC_DCHECK_GE(g_libsrtp_usage_count, 1);
384 if (--g_libsrtp_usage_count == 0) {
zstein4dde3df2017-07-07 14:26:25 -0700385 int err = srtp_shutdown();
386 if (err) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100387 RTC_LOG(LS_ERROR) << "srtp_shutdown failed. err=" << err;
zstein4dde3df2017-07-07 14:26:25 -0700388 }
zstein4dde3df2017-07-07 14:26:25 -0700389 }
390}
391
392void SrtpSession::HandleEvent(const srtp_event_data_t* ev) {
393 RTC_DCHECK(thread_checker_.CalledOnValidThread());
394 switch (ev->event) {
395 case event_ssrc_collision:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100396 RTC_LOG(LS_INFO) << "SRTP event: SSRC collision";
zstein4dde3df2017-07-07 14:26:25 -0700397 break;
398 case event_key_soft_limit:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100399 RTC_LOG(LS_INFO) << "SRTP event: reached soft key usage limit";
zstein4dde3df2017-07-07 14:26:25 -0700400 break;
401 case event_key_hard_limit:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100402 RTC_LOG(LS_INFO) << "SRTP event: reached hard key usage limit";
zstein4dde3df2017-07-07 14:26:25 -0700403 break;
404 case event_packet_index_limit:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100405 RTC_LOG(LS_INFO)
406 << "SRTP event: reached hard packet limit (2^48 packets)";
zstein4dde3df2017-07-07 14:26:25 -0700407 break;
408 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100409 RTC_LOG(LS_INFO) << "SRTP event: unknown " << ev->event;
zstein4dde3df2017-07-07 14:26:25 -0700410 break;
411 }
412}
413
414void SrtpSession::HandleEventThunk(srtp_event_data_t* ev) {
415 // Callback will be executed from same thread that calls the "srtp_protect"
416 // and "srtp_unprotect" functions.
417 SrtpSession* session =
418 static_cast<SrtpSession*>(srtp_get_user_data(ev->session));
419 if (session) {
420 session->HandleEvent(ev);
421 }
422}
423
424} // namespace cricket