Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: webrtc/pc/srtptransport.cc

Issue 2997983002: Completed the functionalities of SrtpTransport. (Closed)
Patch Set: Remove the UpdateRtpParams method. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/pc/srtptransport.h" 11 #include "webrtc/pc/srtptransport.h"
12 12
13 #include <string> 13 #include <string>
14 14
15 #include "webrtc/media/base/rtputils.h" 15 #include "webrtc/media/base/rtputils.h"
16 // For cricket::PF_SRTP_BYPASS
17 #include "webrtc/p2p/base/dtlstransportinternal.h"
16 #include "webrtc/pc/rtptransport.h" 18 #include "webrtc/pc/rtptransport.h"
17 #include "webrtc/pc/srtpsession.h" 19 #include "webrtc/pc/srtpsession.h"
18 #include "webrtc/rtc_base/asyncpacketsocket.h" 20 #include "webrtc/rtc_base/asyncpacketsocket.h"
21 #include "webrtc/rtc_base/base64.h"
19 #include "webrtc/rtc_base/copyonwritebuffer.h" 22 #include "webrtc/rtc_base/copyonwritebuffer.h"
20 #include "webrtc/rtc_base/ptr_util.h" 23 #include "webrtc/rtc_base/ptr_util.h"
21 #include "webrtc/rtc_base/trace_event.h" 24 #include "webrtc/rtc_base/trace_event.h"
22 25
23 namespace webrtc { 26 namespace webrtc {
24 27
25 SrtpTransport::SrtpTransport(bool rtcp_mux_enabled, 28 SrtpTransport::SrtpTransport(bool rtcp_mux_enabled,
26 const std::string& content_name) 29 const std::string& content_name)
27 : content_name_(content_name), 30 : content_name_(content_name),
28 rtp_transport_(rtc::MakeUnique<RtpTransport>(rtcp_mux_enabled)) { 31 rtp_transport_(rtc::MakeUnique<RtpTransport>(rtcp_mux_enabled)) {
29 ConnectToRtpTransport(); 32 ConnectToRtpTransport();
30 } 33 }
31 34
32 SrtpTransport::SrtpTransport(std::unique_ptr<RtpTransportInternal> transport, 35 SrtpTransport::SrtpTransport(std::unique_ptr<RtpTransportInternal> transport,
33 const std::string& content_name) 36 const std::string& content_name)
34 : content_name_(content_name), rtp_transport_(std::move(transport)) { 37 : content_name_(content_name), rtp_transport_(std::move(transport)) {
35 ConnectToRtpTransport(); 38 ConnectToRtpTransport();
36 } 39 }
37 40
38 void SrtpTransport::ConnectToRtpTransport() { 41 void SrtpTransport::ConnectToRtpTransport() {
39 rtp_transport_->SignalPacketReceived.connect( 42 rtp_transport_->SignalPacketReceived.connect(
40 this, &SrtpTransport::OnPacketReceived); 43 this, &SrtpTransport::OnPacketReceived);
41 rtp_transport_->SignalReadyToSend.connect(this, 44 rtp_transport_->SignalReadyToSend.connect(this,
42 &SrtpTransport::OnReadyToSend); 45 &SrtpTransport::OnReadyToSend);
43 } 46 }
44 47
48 bool SrtpTransport::SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
49 const rtc::PacketOptions& options) {
50 return SendPacket(false, packet, options, cricket::PF_SRTP_BYPASS);
51 }
52
53 bool SrtpTransport::SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
54 const rtc::PacketOptions& options) {
55 return SendPacket(true, packet, options, cricket::PF_SRTP_BYPASS);
56 }
57
45 bool SrtpTransport::SendPacket(bool rtcp, 58 bool SrtpTransport::SendPacket(bool rtcp,
46 rtc::CopyOnWriteBuffer* packet, 59 rtc::CopyOnWriteBuffer* packet,
47 const rtc::PacketOptions& options, 60 const rtc::PacketOptions& options,
48 int flags) { 61 int flags) {
49 // TODO(zstein): Protect packet. 62 if (!IsActive()) {
50 63 LOG(LS_ERROR)
64 << "Failed to send the packet because SRTP transport is inactive.";
65 return false;
66 }
67
68 rtc::CopyOnWriteBuffer cp = *packet;
69 TRACE_EVENT0("webrtc", "SRTP Encode");
70 bool res;
71 uint8_t* data = packet->data();
72 int len = static_cast<int>(packet->size());
73 if (!rtcp) {
74 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
75 // inside libsrtp for a RTP packet. A external HMAC module will be writing
76 // a fake HMAC value. This is ONLY done for a RTP packet.
77 // Socket layer will update rtp sendtime extension header if present in
78 // packet with current time before updating the HMAC.
79 #if !defined(ENABLE_EXTERNAL_AUTH)
80 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len);
81 #else
82 rtc::PacketOptions updated_options = options;
83 if (!IsExternalAuthActive()) {
84 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len);
85 } else {
86 updated_options.packet_time_params.rtp_sendtime_extension_id =
87 rtp_abs_sendtime_extn_id_;
88 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len,
89 &updated_options.packet_time_params.srtp_packet_index);
90 // If protection succeeds, let's get auth params from srtp.
91 if (res) {
92 uint8_t* auth_key = NULL;
93 int key_len;
94 res = GetRtpAuthParams(
95 &auth_key, &key_len,
96 &updated_options.packet_time_params.srtp_auth_tag_len);
97 if (res) {
98 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
99 updated_options.packet_time_params.srtp_auth_key.assign(
100 auth_key, auth_key + key_len);
101 }
102 }
103 }
104 #endif
105 if (!res) {
106 int seq_num = -1;
107 uint32_t ssrc = 0;
108 cricket::GetRtpSeqNum(data, len, &seq_num);
109 cricket::GetRtpSsrc(data, len, &ssrc);
110 LOG(LS_ERROR) << "Failed to protect " << content_name_
111 << " RTP packet: size=" << len << ", seqnum=" << seq_num
112 << ", SSRC=" << ssrc;
113 return false;
114 }
115 } else {
116 res = ProtectRtcp(data, len, static_cast<int>(packet->capacity()), &len);
117 if (!res) {
118 int type = -1;
119 cricket::GetRtcpType(data, len, &type);
120 LOG(LS_ERROR) << "Failed to protect " << content_name_
121 << " RTCP packet: size=" << len << ", type=" << type;
122 return false;
123 }
124 }
125
126 // Update the length of the packet now that we've added the auth tag.
127 packet->SetSize(len);
51 return rtp_transport_->SendPacket(rtcp, packet, options, flags); 128 return rtp_transport_->SendPacket(rtcp, packet, options, flags);
52 } 129 }
53 130
54 void SrtpTransport::OnPacketReceived(bool rtcp, 131 void SrtpTransport::OnPacketReceived(bool rtcp,
55 rtc::CopyOnWriteBuffer* packet, 132 rtc::CopyOnWriteBuffer* packet,
56 const rtc::PacketTime& packet_time) { 133 const rtc::PacketTime& packet_time) {
57 // TODO(zstein): Unprotect packet. 134 if (!IsActive()) {
58 135 LOG(LS_WARNING) << "Inactive SRTP transport received a packet. Drop it.";
136 return;
137 }
138
139 TRACE_EVENT0("webrtc", "SRTP Decode");
140 char* data = packet->data<char>();
141 int len = static_cast<int>(packet->size());
142 bool res;
143 if (!rtcp) {
144 res = UnprotectRtp(data, len, &len);
145 if (!res) {
146 int seq_num = -1;
147 uint32_t ssrc = 0;
148 cricket::GetRtpSeqNum(data, len, &seq_num);
149 cricket::GetRtpSsrc(data, len, &ssrc);
150 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
151 << " RTP packet: size=" << len << ", seqnum=" << seq_num
152 << ", SSRC=" << ssrc;
153 return;
154 }
155 } else {
156 res = UnprotectRtcp(data, len, &len);
157 if (!res) {
158 int type = -1;
159 cricket::GetRtcpType(data, len, &type);
160 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
161 << " RTCP packet: size=" << len << ", type=" << type;
162 return;
163 }
164 }
165
166 packet->SetSize(len);
59 SignalPacketReceived(rtcp, packet, packet_time); 167 SignalPacketReceived(rtcp, packet, packet_time);
60 } 168 }
61 169
170 bool SrtpTransport::SetRtpParams(int send_cs,
171 const uint8_t* send_key,
172 int send_key_len,
173 int recv_cs,
174 const uint8_t* recv_key,
175 int recv_key_len) {
176 CreateSrtpSessions();
177 send_session_->SetEncryptedHeaderExtensionIds(
178 send_encrypted_header_extension_ids_);
179 if (external_auth_enabled_) {
180 send_session_->EnableExternalAuth();
181 }
182 if (!send_session_->SetSend(send_cs, send_key, send_key_len)) {
183 ResetParams();
184 return false;
185 }
186
187 recv_session_->SetEncryptedHeaderExtensionIds(
188 recv_encrypted_header_extension_ids_);
189 if (!recv_session_->SetRecv(recv_cs, recv_key, recv_key_len)) {
190 ResetParams();
191 return false;
192 }
193
194 LOG(LS_INFO) << "SRTP activated with negotiated parameters:"
195 << " send cipher_suite " << send_cs << " recv cipher_suite "
196 << recv_cs;
197 return true;
198 }
199
200 bool SrtpTransport::SetRtcpParams(int send_cs,
201 const uint8_t* send_key,
202 int send_key_len,
203 int recv_cs,
204 const uint8_t* recv_key,
205 int recv_key_len) {
206 // This can only be called once, but can be safely called after
207 // SetRtpParams
208 if (send_rtcp_session_ || recv_rtcp_session_) {
209 LOG(LS_ERROR) << "Tried to set SRTCP Params when filter already active";
210 return false;
211 }
212
213 send_rtcp_session_.reset(new cricket::SrtpSession());
214 if (!send_rtcp_session_->SetRecv(send_cs, send_key, send_key_len)) {
215 return false;
216 }
217
218 recv_rtcp_session_.reset(new cricket::SrtpSession());
219 if (!recv_rtcp_session_->SetRecv(recv_cs, recv_key, recv_key_len)) {
220 return false;
221 }
222
223 LOG(LS_INFO) << "SRTCP activated with negotiated parameters:"
224 << " send cipher_suite " << send_cs << " recv cipher_suite "
225 << recv_cs;
226
227 return true;
228 }
229
230 bool SrtpTransport::IsActive() const {
231 return send_session_ && recv_session_;
232 }
233
234 void SrtpTransport::ResetParams() {
235 send_session_ = nullptr;
236 recv_session_ = nullptr;
237 send_rtcp_session_ = nullptr;
238 recv_rtcp_session_ = nullptr;
239 LOG(LS_INFO) << "The params in SRTP transport are reset.";
240 }
241
242 void SrtpTransport::SetEncryptedHeaderExtensionIds(
243 cricket::ContentSource source,
244 const std::vector<int>& extension_ids) {
245 if (source == cricket::CS_LOCAL) {
246 recv_encrypted_header_extension_ids_ = extension_ids;
247 } else {
248 send_encrypted_header_extension_ids_ = extension_ids;
249 }
250 }
251
252 void SrtpTransport::CreateSrtpSessions() {
253 send_session_.reset(new cricket::SrtpSession());
254 recv_session_.reset(new cricket::SrtpSession());
255
256 if (external_auth_enabled_) {
257 send_session_->EnableExternalAuth();
258 }
259 }
260
261 bool SrtpTransport::ProtectRtp(void* p, int in_len, int max_len, int* out_len) {
262 if (!IsActive()) {
263 LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active";
264 return false;
265 }
266 RTC_CHECK(send_session_);
267 return send_session_->ProtectRtp(p, in_len, max_len, out_len);
268 }
269
270 bool SrtpTransport::ProtectRtp(void* p,
271 int in_len,
272 int max_len,
273 int* out_len,
274 int64_t* index) {
275 if (!IsActive()) {
276 LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active";
277 return false;
278 }
279 RTC_CHECK(send_session_);
280 return send_session_->ProtectRtp(p, in_len, max_len, out_len, index);
281 }
282
283 bool SrtpTransport::ProtectRtcp(void* p,
284 int in_len,
285 int max_len,
286 int* out_len) {
287 if (!IsActive()) {
288 LOG(LS_WARNING) << "Failed to ProtectRtcp: SRTP not active";
289 return false;
290 }
291 if (send_rtcp_session_) {
292 return send_rtcp_session_->ProtectRtcp(p, in_len, max_len, out_len);
293 } else {
294 RTC_CHECK(send_session_);
295 return send_session_->ProtectRtcp(p, in_len, max_len, out_len);
296 }
297 }
298
299 bool SrtpTransport::UnprotectRtp(void* p, int in_len, int* out_len) {
300 if (!IsActive()) {
301 LOG(LS_WARNING) << "Failed to UnprotectRtp: SRTP not active";
302 return false;
303 }
304 RTC_CHECK(recv_session_);
305 return recv_session_->UnprotectRtp(p, in_len, out_len);
306 }
307
308 bool SrtpTransport::UnprotectRtcp(void* p, int in_len, int* out_len) {
309 if (!IsActive()) {
310 LOG(LS_WARNING) << "Failed to UnprotectRtcp: SRTP not active";
311 return false;
312 }
313 if (recv_rtcp_session_) {
314 return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len);
315 } else {
316 RTC_CHECK(recv_session_);
317 return recv_session_->UnprotectRtcp(p, in_len, out_len);
318 }
319 }
320
321 bool SrtpTransport::GetRtpAuthParams(uint8_t** key,
322 int* key_len,
323 int* tag_len) {
324 if (!IsActive()) {
325 LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active";
326 return false;
327 }
328
329 RTC_CHECK(send_session_);
330 return send_session_->GetRtpAuthParams(key, key_len, tag_len);
331 }
332
333 bool SrtpTransport::GetSrtpOverhead(int* srtp_overhead) const {
334 if (!IsActive()) {
335 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active";
336 return false;
337 }
338
339 RTC_CHECK(send_session_);
340 *srtp_overhead = send_session_->GetSrtpOverhead();
341 return true;
342 }
343
344 void SrtpTransport::EnableExternalAuth() {
345 RTC_DCHECK(!IsActive());
346 external_auth_enabled_ = true;
347 }
348
349 bool SrtpTransport::IsExternalAuthEnabled() const {
350 return external_auth_enabled_;
351 }
352
353 bool SrtpTransport::IsExternalAuthActive() const {
354 if (!IsActive()) {
355 LOG(LS_WARNING) << "Failed to check IsExternalAuthActive: SRTP not active";
356 return false;
357 }
358
359 RTC_CHECK(send_session_);
360 return send_session_->IsExternalAuthActive();
361 }
362
62 } // namespace webrtc 363 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698