blob: 149651cddaef4712346733da0acc967018338339 [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")
231 << " with negotiated parameters:"
232 << " send cipher_suite " << send_cs << " recv cipher_suite "
233 << recv_cs;
Zhi Huangcf990f52017-09-22 12:12:30 -0700234 return true;
235}
236
237bool SrtpTransport::SetRtcpParams(int send_cs,
238 const uint8_t* send_key,
239 int send_key_len,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800240 const std::vector<int>& send_extension_ids,
Zhi Huangcf990f52017-09-22 12:12:30 -0700241 int recv_cs,
242 const uint8_t* recv_key,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800243 int recv_key_len,
244 const std::vector<int>& recv_extension_ids) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700245 // This can only be called once, but can be safely called after
246 // SetRtpParams
247 if (send_rtcp_session_ || recv_rtcp_session_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100248 RTC_LOG(LS_ERROR) << "Tried to set SRTCP Params when filter already active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700249 return false;
250 }
251
252 send_rtcp_session_.reset(new cricket::SrtpSession());
Zhi Huangc99b6c72017-11-10 16:44:46 -0800253 if (!send_rtcp_session_->SetSend(send_cs, send_key, send_key_len,
254 send_extension_ids)) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700255 return false;
256 }
257
258 recv_rtcp_session_.reset(new cricket::SrtpSession());
Zhi Huangc99b6c72017-11-10 16:44:46 -0800259 if (!recv_rtcp_session_->SetRecv(recv_cs, recv_key, recv_key_len,
260 recv_extension_ids)) {
Zhi Huangcf990f52017-09-22 12:12:30 -0700261 return false;
262 }
263
Mirko Bonadei675513b2017-11-09 11:09:25 +0100264 RTC_LOG(LS_INFO) << "SRTCP activated with negotiated parameters:"
265 << " send cipher_suite " << send_cs << " recv cipher_suite "
266 << recv_cs;
Zhi Huangcf990f52017-09-22 12:12:30 -0700267
268 return true;
269}
270
271bool SrtpTransport::IsActive() const {
272 return send_session_ && recv_session_;
273}
274
275void SrtpTransport::ResetParams() {
276 send_session_ = nullptr;
277 recv_session_ = nullptr;
278 send_rtcp_session_ = nullptr;
279 recv_rtcp_session_ = nullptr;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100280 RTC_LOG(LS_INFO) << "The params in SRTP transport are reset.";
Zhi Huangcf990f52017-09-22 12:12:30 -0700281}
282
Zhi Huangcf990f52017-09-22 12:12:30 -0700283void SrtpTransport::CreateSrtpSessions() {
284 send_session_.reset(new cricket::SrtpSession());
285 recv_session_.reset(new cricket::SrtpSession());
286
287 if (external_auth_enabled_) {
288 send_session_->EnableExternalAuth();
289 }
290}
291
292bool SrtpTransport::ProtectRtp(void* p, int in_len, int max_len, int* out_len) {
293 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100294 RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700295 return false;
296 }
297 RTC_CHECK(send_session_);
298 return send_session_->ProtectRtp(p, in_len, max_len, out_len);
299}
300
301bool SrtpTransport::ProtectRtp(void* p,
302 int in_len,
303 int max_len,
304 int* out_len,
305 int64_t* index) {
306 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100307 RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700308 return false;
309 }
310 RTC_CHECK(send_session_);
311 return send_session_->ProtectRtp(p, in_len, max_len, out_len, index);
312}
313
314bool SrtpTransport::ProtectRtcp(void* p,
315 int in_len,
316 int max_len,
317 int* out_len) {
318 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100319 RTC_LOG(LS_WARNING) << "Failed to ProtectRtcp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700320 return false;
321 }
322 if (send_rtcp_session_) {
323 return send_rtcp_session_->ProtectRtcp(p, in_len, max_len, out_len);
324 } else {
325 RTC_CHECK(send_session_);
326 return send_session_->ProtectRtcp(p, in_len, max_len, out_len);
327 }
328}
329
330bool SrtpTransport::UnprotectRtp(void* p, int in_len, int* out_len) {
331 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100332 RTC_LOG(LS_WARNING) << "Failed to UnprotectRtp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700333 return false;
334 }
335 RTC_CHECK(recv_session_);
336 return recv_session_->UnprotectRtp(p, in_len, out_len);
337}
338
339bool SrtpTransport::UnprotectRtcp(void* p, int in_len, int* out_len) {
340 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100341 RTC_LOG(LS_WARNING) << "Failed to UnprotectRtcp: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700342 return false;
343 }
344 if (recv_rtcp_session_) {
345 return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len);
346 } else {
347 RTC_CHECK(recv_session_);
348 return recv_session_->UnprotectRtcp(p, in_len, out_len);
349 }
350}
351
352bool SrtpTransport::GetRtpAuthParams(uint8_t** key,
353 int* key_len,
354 int* tag_len) {
355 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100356 RTC_LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700357 return false;
358 }
359
360 RTC_CHECK(send_session_);
361 return send_session_->GetRtpAuthParams(key, key_len, tag_len);
362}
363
364bool SrtpTransport::GetSrtpOverhead(int* srtp_overhead) const {
365 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100366 RTC_LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700367 return false;
368 }
369
370 RTC_CHECK(send_session_);
371 *srtp_overhead = send_session_->GetSrtpOverhead();
372 return true;
373}
374
375void SrtpTransport::EnableExternalAuth() {
376 RTC_DCHECK(!IsActive());
377 external_auth_enabled_ = true;
378}
379
380bool SrtpTransport::IsExternalAuthEnabled() const {
381 return external_auth_enabled_;
382}
383
384bool SrtpTransport::IsExternalAuthActive() const {
385 if (!IsActive()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100386 RTC_LOG(LS_WARNING)
387 << "Failed to check IsExternalAuthActive: SRTP not active";
Zhi Huangcf990f52017-09-22 12:12:30 -0700388 return false;
389 }
390
391 RTC_CHECK(send_session_);
392 return send_session_->IsExternalAuthActive();
393}
394
zstein398c3fd2017-07-19 13:38:02 -0700395} // namespace webrtc