blob: 625034dfabfbb10f73a615da107e4d3f61be3ea4 [file] [log] [blame]
zstein398c3fd2017-07-19 13:38:02 -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/srtptransport.h"
zstein398c3fd2017-07-19 13:38:02 -070012
13#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <vector>
zstein398c3fd2017-07-19 13:38:02 -070015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/base/rtputils.h"
17#include "pc/rtptransport.h"
18#include "pc/srtpsession.h"
19#include "rtc_base/asyncpacketsocket.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070020#include "rtc_base/base64.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/copyonwritebuffer.h"
22#include "rtc_base/ptr_util.h"
23#include "rtc_base/trace_event.h"
zstein398c3fd2017-07-19 13:38:02 -070024
25namespace webrtc {
26
Zhi Huang2dfc42d2017-12-04 13:38:48 -080027SrtpTransport::SrtpTransport(bool rtcp_mux_enabled)
28 : RtpTransportInternalAdapter(new RtpTransport(rtcp_mux_enabled)) {
Zhi Huangf2d7beb2017-11-20 14:35:11 -080029 // Own the raw pointer |transport| from the base class.
30 rtp_transport_.reset(transport_);
31 RTC_DCHECK(rtp_transport_);
zstein398c3fd2017-07-19 13:38:02 -070032 ConnectToRtpTransport();
33}
34
Zhi Huangf2d7beb2017-11-20 14:35:11 -080035SrtpTransport::SrtpTransport(
Zhi Huang2dfc42d2017-12-04 13:38:48 -080036 std::unique_ptr<RtpTransportInternal> rtp_transport)
Zhi Huangf2d7beb2017-11-20 14:35:11 -080037 : RtpTransportInternalAdapter(rtp_transport.get()),
Zhi Huangf2d7beb2017-11-20 14:35:11 -080038 rtp_transport_(std::move(rtp_transport)) {
39 RTC_DCHECK(rtp_transport_);
zstein398c3fd2017-07-19 13:38:02 -070040 ConnectToRtpTransport();
41}
42
43void SrtpTransport::ConnectToRtpTransport() {
44 rtp_transport_->SignalPacketReceived.connect(
45 this, &SrtpTransport::OnPacketReceived);
46 rtp_transport_->SignalReadyToSend.connect(this,
47 &SrtpTransport::OnReadyToSend);
Zhi Huang942bc2e2017-11-13 13:26:07 -080048 rtp_transport_->SignalNetworkRouteChanged.connect(
49 this, &SrtpTransport::OnNetworkRouteChanged);
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080050 rtp_transport_->SignalWritableState.connect(this,
51 &SrtpTransport::OnWritableState);
52 rtp_transport_->SignalSentPacket.connect(this, &SrtpTransport::OnSentPacket);
zstein398c3fd2017-07-19 13:38:02 -070053}
54
Zhi Huangcf990f52017-09-22 12:12:30 -070055bool SrtpTransport::SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
56 const rtc::PacketOptions& options,
57 int flags) {
58 return SendPacket(false, packet, options, flags);
59}
60
61bool SrtpTransport::SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
62 const rtc::PacketOptions& options,
63 int flags) {
64 return SendPacket(true, packet, options, flags);
65}
66
zstein398c3fd2017-07-19 13:38:02 -070067bool SrtpTransport::SendPacket(bool rtcp,
68 rtc::CopyOnWriteBuffer* packet,
69 const rtc::PacketOptions& options,
70 int flags) {
Zhi Huangcf990f52017-09-22 12:12:30 -070071 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010072 RTC_LOG(LS_ERROR)
Zhi Huangcf990f52017-09-22 12:12:30 -070073 << "Failed to send the packet because SRTP transport is inactive.";
74 return false;
75 }
zstein398c3fd2017-07-19 13:38:02 -070076
Zhi Huangcf990f52017-09-22 12:12:30 -070077 rtc::PacketOptions updated_options = options;
78 rtc::CopyOnWriteBuffer cp = *packet;
79 TRACE_EVENT0("webrtc", "SRTP Encode");
80 bool res;
81 uint8_t* data = packet->data();
82 int len = static_cast<int>(packet->size());
83 if (!rtcp) {
84// If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
85// inside libsrtp for a RTP packet. A external HMAC module will be writing
86// a fake HMAC value. This is ONLY done for a RTP packet.
87// Socket layer will update rtp sendtime extension header if present in
88// packet with current time before updating the HMAC.
89#if !defined(ENABLE_EXTERNAL_AUTH)
90 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len);
91#else
92 if (!IsExternalAuthActive()) {
93 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len);
94 } else {
95 updated_options.packet_time_params.rtp_sendtime_extension_id =
96 rtp_abs_sendtime_extn_id_;
97 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len,
98 &updated_options.packet_time_params.srtp_packet_index);
99 // If protection succeeds, let's get auth params from srtp.
100 if (res) {
101 uint8_t* auth_key = NULL;
102 int key_len;
103 res = GetRtpAuthParams(
104 &auth_key, &key_len,
105 &updated_options.packet_time_params.srtp_auth_tag_len);
106 if (res) {
107 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
108 updated_options.packet_time_params.srtp_auth_key.assign(
109 auth_key, auth_key + key_len);
110 }
111 }
112 }
113#endif
114 if (!res) {
115 int seq_num = -1;
116 uint32_t ssrc = 0;
117 cricket::GetRtpSeqNum(data, len, &seq_num);
118 cricket::GetRtpSsrc(data, len, &ssrc);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800119 RTC_LOG(LS_ERROR) << "Failed to protect RTP packet: size=" << len
120 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
Zhi Huangcf990f52017-09-22 12:12:30 -0700121 return false;
122 }
123 } else {
124 res = ProtectRtcp(data, len, static_cast<int>(packet->capacity()), &len);
125 if (!res) {
126 int type = -1;
127 cricket::GetRtcpType(data, len, &type);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800128 RTC_LOG(LS_ERROR) << "Failed to protect RTCP packet: size=" << len
129 << ", type=" << type;
Zhi Huangcf990f52017-09-22 12:12:30 -0700130 return false;
131 }
132 }
133
134 // Update the length of the packet now that we've added the auth tag.
135 packet->SetSize(len);
136 return rtcp ? rtp_transport_->SendRtcpPacket(packet, updated_options, flags)
137 : rtp_transport_->SendRtpPacket(packet, updated_options, flags);
zstein398c3fd2017-07-19 13:38:02 -0700138}
139
140void SrtpTransport::OnPacketReceived(bool rtcp,
141 rtc::CopyOnWriteBuffer* packet,
142 const rtc::PacketTime& packet_time) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700143 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100144 RTC_LOG(LS_WARNING)
145 << "Inactive SRTP transport received a packet. Drop it.";
Zhi Huangcf990f52017-09-22 12:12:30 -0700146 return;
147 }
zstein398c3fd2017-07-19 13:38:02 -0700148
Zhi Huangcf990f52017-09-22 12:12:30 -0700149 TRACE_EVENT0("webrtc", "SRTP Decode");
150 char* data = packet->data<char>();
151 int len = static_cast<int>(packet->size());
152 bool res;
153 if (!rtcp) {
154 res = UnprotectRtp(data, len, &len);
155 if (!res) {
156 int seq_num = -1;
157 uint32_t ssrc = 0;
158 cricket::GetRtpSeqNum(data, len, &seq_num);
159 cricket::GetRtpSsrc(data, len, &ssrc);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800160 RTC_LOG(LS_ERROR) << "Failed to unprotect RTP packet: size=" << len
161 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
Zhi Huangcf990f52017-09-22 12:12:30 -0700162 return;
163 }
164 } else {
165 res = UnprotectRtcp(data, len, &len);
166 if (!res) {
167 int type = -1;
168 cricket::GetRtcpType(data, len, &type);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800169 RTC_LOG(LS_ERROR) << "Failed to unprotect RTCP packet: size=" << len
170 << ", type=" << type;
Zhi Huangcf990f52017-09-22 12:12:30 -0700171 return;
172 }
173 }
174
175 packet->SetSize(len);
zstein398c3fd2017-07-19 13:38:02 -0700176 SignalPacketReceived(rtcp, packet, packet_time);
177}
178
Zhi Huang942bc2e2017-11-13 13:26:07 -0800179void SrtpTransport::OnNetworkRouteChanged(
180
181 rtc::Optional<rtc::NetworkRoute> network_route) {
182 // Only append the SRTP overhead when there is a selected network route.
183 if (network_route) {
184 int srtp_overhead = 0;
185 if (IsActive()) {
186 GetSrtpOverhead(&srtp_overhead);
187 }
188 network_route->packet_overhead += srtp_overhead;
189 }
190 SignalNetworkRouteChanged(network_route);
191}
192
Zhi Huangcf990f52017-09-22 12:12:30 -0700193bool SrtpTransport::SetRtpParams(int send_cs,
194 const uint8_t* send_key,
195 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800196 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -0700197 int recv_cs,
198 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800199 int recv_key_len,
200 const std::vector<int>& recv_extension_ids) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700201 // If parameters are being set for the first time, we should create new SRTP
202 // sessions and call "SetSend/SetRecv". Otherwise we should call
203 // "UpdateSend"/"UpdateRecv" on the existing sessions, which will internally
204 // call "srtp_update".
205 bool new_sessions = false;
206 if (!send_session_) {
207 RTC_DCHECK(!recv_session_);
208 CreateSrtpSessions();
209 new_sessions = true;
210 }
Zhi Huangcf990f52017-09-22 12:12:30 -0700211 bool ret = new_sessions
Zhi Huangc99b6c72017-11-10 16:44:46 -0800212 ? send_session_->SetSend(send_cs, send_key, send_key_len,
213 send_extension_ids)
214 : send_session_->UpdateSend(send_cs, send_key, send_key_len,
215 send_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -0700216 if (!ret) {
217 ResetParams();
218 return false;
219 }
220
Zhi Huangc99b6c72017-11-10 16:44:46 -0800221 ret = new_sessions ? recv_session_->SetRecv(recv_cs, recv_key, recv_key_len,
222 recv_extension_ids)
223 : recv_session_->UpdateRecv(
224 recv_cs, recv_key, recv_key_len, recv_extension_ids);
Zhi Huangcf990f52017-09-22 12:12:30 -0700225 if (!ret) {
226 ResetParams();
227 return false;
228 }
229
Mirko Bonadei675513b2017-11-09 11:09:25 +0100230 RTC_LOG(LS_INFO) << "SRTP " << (new_sessions ? "activated" : "updated")
Jonas Olsson45cc8902018-02-13 10:37:07 +0100231 << " with negotiated parameters: send cipher_suite "
232 << send_cs << " recv cipher_suite " << recv_cs;
Zhi Huangcf990f52017-09-22 12:12:30 -0700233 return true;
234}
235
236bool SrtpTransport::SetRtcpParams(int send_cs,
237 const uint8_t* send_key,
238 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800239 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -0700240 int recv_cs,
241 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800242 int recv_key_len,
243 const std::vector<int>& recv_extension_ids) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700244 // This can only be called once, but can be safely called after
245 // SetRtpParams
246 if (send_rtcp_session_ || recv_rtcp_session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100247 RTC_LOG(LS_ERROR) << "Tried to set SRTCP Params when filter already active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700248 return false;
249 }
250
251 send_rtcp_session_.reset(new cricket::SrtpSession());
Zhi Huangc99b6c72017-11-10 16:44:46 -0800252 if (!send_rtcp_session_->SetSend(send_cs, send_key, send_key_len,
253 send_extension_ids)) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700254 return false;
255 }
256
257 recv_rtcp_session_.reset(new cricket::SrtpSession());
Zhi Huangc99b6c72017-11-10 16:44:46 -0800258 if (!recv_rtcp_session_->SetRecv(recv_cs, recv_key, recv_key_len,
259 recv_extension_ids)) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700260 return false;
261 }
262
Mirko Bonadei675513b2017-11-09 11:09:25 +0100263 RTC_LOG(LS_INFO) << "SRTCP activated with negotiated parameters:"
Jonas Olsson45cc8902018-02-13 10:37:07 +0100264 " send cipher_suite "
265 << send_cs << " recv cipher_suite " << recv_cs;
Zhi Huangcf990f52017-09-22 12:12:30 -0700266
267 return true;
268}
269
270bool SrtpTransport::IsActive() const {
271 return send_session_ && recv_session_;
272}
273
274void SrtpTransport::ResetParams() {
275 send_session_ = nullptr;
276 recv_session_ = nullptr;
277 send_rtcp_session_ = nullptr;
278 recv_rtcp_session_ = nullptr;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100279 RTC_LOG(LS_INFO) << "The params in SRTP transport are reset.";
Zhi Huangcf990f52017-09-22 12:12:30 -0700280}
281
Zhi Huangcf990f52017-09-22 12:12:30 -0700282void SrtpTransport::CreateSrtpSessions() {
283 send_session_.reset(new cricket::SrtpSession());
284 recv_session_.reset(new cricket::SrtpSession());
285
286 if (external_auth_enabled_) {
287 send_session_->EnableExternalAuth();
288 }
289}
290
291bool SrtpTransport::ProtectRtp(void* p, int in_len, int max_len, int* out_len) {
292 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100293 RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700294 return false;
295 }
296 RTC_CHECK(send_session_);
297 return send_session_->ProtectRtp(p, in_len, max_len, out_len);
298}
299
300bool SrtpTransport::ProtectRtp(void* p,
301 int in_len,
302 int max_len,
303 int* out_len,
304 int64_t* index) {
305 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100306 RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700307 return false;
308 }
309 RTC_CHECK(send_session_);
310 return send_session_->ProtectRtp(p, in_len, max_len, out_len, index);
311}
312
313bool SrtpTransport::ProtectRtcp(void* p,
314 int in_len,
315 int max_len,
316 int* out_len) {
317 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100318 RTC_LOG(LS_WARNING) << "Failed to ProtectRtcp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700319 return false;
320 }
321 if (send_rtcp_session_) {
322 return send_rtcp_session_->ProtectRtcp(p, in_len, max_len, out_len);
323 } else {
324 RTC_CHECK(send_session_);
325 return send_session_->ProtectRtcp(p, in_len, max_len, out_len);
326 }
327}
328
329bool SrtpTransport::UnprotectRtp(void* p, int in_len, int* out_len) {
330 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100331 RTC_LOG(LS_WARNING) << "Failed to UnprotectRtp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700332 return false;
333 }
334 RTC_CHECK(recv_session_);
335 return recv_session_->UnprotectRtp(p, in_len, out_len);
336}
337
338bool SrtpTransport::UnprotectRtcp(void* p, int in_len, int* out_len) {
339 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100340 RTC_LOG(LS_WARNING) << "Failed to UnprotectRtcp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700341 return false;
342 }
343 if (recv_rtcp_session_) {
344 return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len);
345 } else {
346 RTC_CHECK(recv_session_);
347 return recv_session_->UnprotectRtcp(p, in_len, out_len);
348 }
349}
350
351bool SrtpTransport::GetRtpAuthParams(uint8_t** key,
352 int* key_len,
353 int* tag_len) {
354 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100355 RTC_LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700356 return false;
357 }
358
359 RTC_CHECK(send_session_);
360 return send_session_->GetRtpAuthParams(key, key_len, tag_len);
361}
362
363bool SrtpTransport::GetSrtpOverhead(int* srtp_overhead) const {
364 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100365 RTC_LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700366 return false;
367 }
368
369 RTC_CHECK(send_session_);
370 *srtp_overhead = send_session_->GetSrtpOverhead();
371 return true;
372}
373
374void SrtpTransport::EnableExternalAuth() {
375 RTC_DCHECK(!IsActive());
376 external_auth_enabled_ = true;
377}
378
379bool SrtpTransport::IsExternalAuthEnabled() const {
380 return external_auth_enabled_;
381}
382
383bool SrtpTransport::IsExternalAuthActive() const {
384 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100385 RTC_LOG(LS_WARNING)
386 << "Failed to check IsExternalAuthActive: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700387 return false;
388 }
389
390 RTC_CHECK(send_session_);
391 return send_session_->IsExternalAuthActive();
392}
393
zstein398c3fd2017-07-19 13:38:02 -0700394} // namespace webrtc