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

Side by Side Diff: content/browser/renderer_host/p2p/socket_host.cc

Issue 159353002: This CL adds methods to manipulate RTP header extension, particularly (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/p2p/socket_host.h" 5 #include "content/browser/renderer_host/p2p/socket_host.h"
6 6
7 #include "base/sys_byteorder.h" 7 #include "base/sys_byteorder.h"
8 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" 8 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
9 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" 9 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h"
10 #include "content/browser/renderer_host/p2p/socket_host_udp.h" 10 #include "content/browser/renderer_host/p2p/socket_host_udp.h"
11 #include "crypto/hmac.h"
12 #include "third_party/libjingle/source/talk/base/asyncpacketsocket.h"
13 #include "third_party/libjingle/source/talk/base/byteorder.h"
14 #include "third_party/libjingle/source/talk/base/messagedigest.h"
15 #include "third_party/libjingle/source/talk/p2p/base/stun.h"
11 16
12 namespace { 17 namespace {
18
13 const uint32 kStunMagicCookie = 0x2112A442; 19 const uint32 kStunMagicCookie = 0x2112A442;
20 const int kMinRtpHdrLen = 12;
21 const int kRtpExtnHdrLen = 4;
22 const int kDtlsRecordHeaderLen = 13;
23 const int kTurnChannelHdrLen = 4;
24 const int kAbsSendTimeExtnLen = 3;
25 const int kOneByteHdrLen = 1;
26
27 // Fake auth tag written by the render process if external authentication is
28 // enabled. HMAC in packet will be compared against this value before updating
29 // packet with actual HMAC value.
30 static const unsigned char kFakeAuthTag[10] = {
31 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
32 };
33
34 bool IsTurnChannelData(const char* data) {
35 return ((*data & 0xC0) == 0x40);
36 }
37
38 bool IsDtlsPacket(const char* data, int len) {
39 const uint8* u = reinterpret_cast<const uint8*>(data);
40 return (len >= kDtlsRecordHeaderLen && (u[0] > 19 && u[0] < 64));
41 }
42
43 bool IsRtcpPacket(const char* data) {
44 int type = (static_cast<uint8>(data[1]) & 0x7F);
45 return (type >= 64 && type < 96);
46 }
47
48 bool IsStunPacket(const char* data) {
49 return ((*data & 0xC0) == 0);
50 }
51
52 bool IsTurnSendIndicationPacket(const char* data) {
53 uint16 type = talk_base::GetBE16(data);
54 return (type == cricket::TURN_SEND_INDICATION);
55 }
56
57 bool IsRtpPacket(const char* data, int len) {
58 return ((*data & 0xC0) == 0x80);
59 }
60
14 } // namespace 61 } // namespace
15 62
16 namespace content { 63 namespace content {
17 64
18 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, 65 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender,
19 int id) 66 int id)
20 : message_sender_(message_sender), 67 : message_sender_(message_sender),
21 id_(id), 68 id_(id),
22 state_(STATE_UNINITIALIZED) { 69 state_(STATE_UNINITIALIZED) {
23 } 70 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 case P2P_SOCKET_STUN_TCP_CLIENT: 142 case P2P_SOCKET_STUN_TCP_CLIENT:
96 case P2P_SOCKET_STUN_SSLTCP_CLIENT: 143 case P2P_SOCKET_STUN_SSLTCP_CLIENT:
97 case P2P_SOCKET_STUN_TLS_CLIENT: 144 case P2P_SOCKET_STUN_TLS_CLIENT:
98 return new P2PSocketHostStunTcp(message_sender, id, type, url_context); 145 return new P2PSocketHostStunTcp(message_sender, id, type, url_context);
99 } 146 }
100 147
101 NOTREACHED(); 148 NOTREACHED();
102 return NULL; 149 return NULL;
103 } 150 }
104 151
152 void MaybeUpdatePacketAbsSendTimeExtn(
Solis 2014/02/27 09:36:10 Use same order as in .h. Makes it easier to find t
Mallinath (Gone from Chromium) 2014/02/27 19:43:10 Done.
153 char* data, int length, const talk_base::PacketOptions& options) {
154 if (length <= 0) {
Solis 2014/02/27 09:36:10 No need to DCHECK(data != NULL); ?
Mallinath (Gone from Chromium) 2014/02/27 19:43:10 Done.
155 return;
156 }
157
158 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
159 // PacketOptions, nothing to be updated in this packet.
160 if (options.packet_time_params.rtp_sendtime_extension_id == -1 &&
161 options.packet_time_params.srtp_auth_key.empty()) {
162 return;
163 }
164
165 DCHECK(!IsDtlsPacket(data, length));
166 DCHECK(!IsRtcpPacket(data));
167
168 // If there is a srtp auth key present then packet must be a RTP packet.
169 // RTP packet may have been wrapped in a TURN Channel Data or
170 // TURN send indication.
171 int rtp_start_pos;
172 int rtp_length;
173 if (!GetRtpPacketStartPositionAndLength(
174 data, length, &rtp_start_pos, &rtp_length)) {
175 // This method should never return false.
176 NOTREACHED();
177 return;
178 }
179
180 // Skip to rtp packet.
181 char* start = data + rtp_start_pos;
182 // If there a non negetive sendtime extension id present in packet options,
183 // then we should parse the rtp packet to update the timestamp. Otherwise
184 // just calculate HMAC and update packet with it.
185 if (options.packet_time_params.rtp_sendtime_extension_id != -1) {
186 if (!MaybeUpdateRtpAbsSendTimeExtn(
187 start, rtp_length,
188 options.packet_time_params.rtp_sendtime_extension_id)) {
189 // We should find an extension id in the rtp packet.
190 NOTREACHED();
191 return;
192 }
193 }
194
195 MaybeUpdateRtpAuthTag(start, rtp_length, options);
196 }
197
198 bool GetRtpPacketStartPositionAndLength(
199 char* packet, int length, int* rtp_start_pos, int* rtp_packet_length) {
200 int rtp_begin, rtp_length;
201 if (IsTurnChannelData(packet)) {
202 // Turn Channel Message header format.
203 // 0 1 2 3
204 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
205 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206 // | Channel Number | Length |
207 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208 // | |
209 // / Application Data /
210 // / /
211 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212 if (length < kTurnChannelHdrLen) {
213 return false;
214 }
215
216 rtp_begin = kTurnChannelHdrLen;
217 rtp_length = talk_base::GetBE16(&packet[2]);
218 if (length < rtp_length + kTurnChannelHdrLen) {
219 return false;
220 }
221 } else if (IsTurnSendIndicationPacket(packet)) {
222 if (length <= P2PSocketHost::kStunHeaderSize) {
223 // Message must be greater than 20 bytes, if it's carrying any payload.
224 return false;
225 }
226 // Validate STUN message length.
227 int stun_msg_len = talk_base::GetBE16(&packet[2]);
228 if (stun_msg_len + P2PSocketHost::kStunHeaderSize != length) {
229 return false;
230 }
231
232 // First skip mandatory stun header which is of 20 bytes.
233 rtp_begin = P2PSocketHost::kStunHeaderSize;
234 // Loop through STUN attributes until we find STUN DATA attribute.
235 char* start = packet + *rtp_start_pos;
Solis 2014/02/27 09:36:10 rtp_begin + add test case to catch.
Mallinath (Gone from Chromium) 2014/02/27 19:43:10 Done.
236 bool data_attr_present = false;
237 while ((packet + rtp_begin) - start < length) {
238 // Keep reading STUN attributes until we hit DATA attribute.
239 // Attribute will be a TLV structure.
240 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
241 // | Type | Length |
242 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
243 // | Value (variable) ....
244 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
245 // The value in the length field MUST contain the length of the Value
246 // part of the attribute, prior to padding, measured in bytes. Since
247 // STUN aligns attributes on 32-bit boundaries, attributes whose content
248 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
249 // padding so that its value contains a multiple of 4 bytes. The
250 // padding bits are ignored, and may be any value.
251 uint16 attr_type, attr_length;
252 // Getting attribute type and length.
253 attr_type = talk_base::GetBE16(&packet[rtp_begin]);
254 attr_length = talk_base::GetBE16(
255 &packet[rtp_begin + sizeof(attr_type)]);
256 // Checking for bogus attribute length.
257 if (length < attr_length + *rtp_start_pos) {
Solis 2014/02/27 09:36:10 should be rtp_begin? add test case for this.
Mallinath (Gone from Chromium) 2014/02/27 19:43:10 Done.
258 return false;
259 }
260 if (attr_type != cricket::STUN_ATTR_DATA) {
261 rtp_begin += sizeof(attr_type) + sizeof(attr_length) + attr_length;
262 if ((attr_length % 4) != 0) {
263 rtp_begin += (4 - (attr_length % 4));
264 }
265 continue;
266 }
267 data_attr_present = true;
268 rtp_begin += 4; // Skip STUN_DATA_ATTR header.
269 rtp_length = attr_length;
270 // One final check of length before exiting.
271 if (length < rtp_length + rtp_begin)
Solis 2014/02/27 09:36:10 Be consistent with braces around single statements
Mallinath (Gone from Chromium) 2014/02/27 19:43:10 Done.
272 return false;
273
274 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet.
275 break;
276 }
277
278 if (!data_attr_present) {
279 // There is no data attribute present in the message. We can't do anything
280 // with the message.
281 return false;
282 }
283
284 } else {
285 // This is a raw RTP packet.
286 rtp_begin = 0;
287 rtp_length = length;
288 }
289
290 // Making sure we have a valid RTP packet at the end.
291 if (IsRtpPacket(packet + rtp_begin, rtp_length) &&
292 ValidateRtpHeader(packet + rtp_begin, rtp_length)) {
293 *rtp_start_pos = rtp_begin;
294 *rtp_packet_length = rtp_length;
295 return true;
296 }
297 return false;
298 }
299
300 // Verifies rtp header and message length.
301 bool ValidateRtpHeader(char* rtp, int length) {
302 int cc_count = rtp[0] & 0x0F;
303 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
304 if (rtp_hdr_len_without_extn > length) {
305 return false;
306 }
307
308 // If extension bit is not set, we are done with header processing, as input
309 // length is verified above.
310 bool X = (rtp[0] & 0x10);
311 if (!X)
312 return true;
313
314 rtp += rtp_hdr_len_without_extn;
315
316 // Getting extension profile length.
317 // Length is in 32 bit words.
318 uint16 extn_length = talk_base::GetBE16(rtp + 2) * 4;
319
320 // Verify input length against total header size.
321 if (rtp_hdr_len_without_extn + kRtpExtnHdrLen + extn_length > length) {
322 return false;
323 }
324 return true;
325 }
326
327 // ValidateRtpHeader must be called before this method to make sure, we have
328 // a sane rtp packet.
329 bool MaybeUpdateRtpAbsSendTimeExtn(char* rtp, int length,
330 int extension_id) {
331 // 0 1 2 3
332 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
333 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
334 // |V=2|P|X| CC |M| PT | sequence number |
335 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
336 // | timestamp |
337 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
338 // | synchronization source (SSRC) identifier |
339 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
340 // | contributing source (CSRC) identifiers |
341 // | .... |
342 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
343
344 bool X = (rtp[0] & 0x10);
345 if (!X) // Return if extension bit is not set.
346 return true;
347
348 int cc_count = rtp[0] & 0x0F;
349 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
350
351 rtp += rtp_hdr_len_without_extn;
352
353 // Getting extension profile ID and length.
354 uint16 profile_id = talk_base::GetBE16(rtp);
355 // Length is in 32 bit words.
356 uint16 extn_length = talk_base::GetBE16(rtp + 2) * 4;
357
358 rtp += kRtpExtnHdrLen; // Moving past extn header.
359
360 bool found = false;
361 // WebRTC is using one byte header extension.
362 // TODO(mallinath) - Handle two byte header extension.
363 if (profile_id == 0xBEDE) { // OneByte extension header
364 // 0
365 // 0 1 2 3 4 5 6 7
366 // +-+-+-+-+-+-+-+-+
367 // | ID | len |
368 // +-+-+-+-+-+-+-+-+
369
370 // 0 1 2 3
371 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
372 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
373 // | 0xBE | 0xDE | length=3 |
374 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
375 // | ID | L=0 | data | ID | L=1 | data...
376 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
378 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 // | data |
380 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381 char* extn_start = rtp;
382 while (rtp - extn_start < extn_length) {
383 const int id = (*rtp & 0xF0) >> 4;
384 const int len = (*rtp & 0x0F) + 1;
385 // The 4-bit length is the number minus one of data bytes of this header
386 // extension element following the one-byte header.
387 if (id == extension_id) {
388 UpdateAbsSendTimeExtnValue(rtp + kOneByteHdrLen, len);
389 found = true;
390 break;
391 }
392 rtp += kOneByteHdrLen + len;
393 // Counting padding bytes
394 while ((*rtp != 0) && (rtp - extn_start < extn_length)) {
395 ++rtp;
396 }
397 }
398 }
399 return found;
400 }
401
402 void UpdateAbsSendTimeExtnValue(char* extn_data, int len) {
403 // Absolute send time in RTP streams.
404 //
405 // The absolute send time is signaled to the receiver in-band using the
406 // general mechanism for RTP header extensions [RFC5285]. The payload
407 // of this extension (the transmitted value) is a 24-bit unsigned integer
408 // containing the sender's current time in seconds as a fixed point number
409 // with 18 bits fractional part.
410 //
411 // The form of the absolute send time extension block:
412 //
413 // 0 1 2 3
414 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
415 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
416 // | ID | len=2 | absolute send time |
417 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
418 DCHECK_EQ(len, kAbsSendTimeExtnLen);
419 // Now() has resolution ~1-15ms, using HighResNow(). But it is warned not to
420 // use it unless necessary, as it is expensive than Now().
421 uint64 now_us =
422 (base::TimeTicks::HighResNow() - base::TimeTicks()).InMicroseconds();
423 // Convert second to 24-bit unsigned with 18 bit fractional part
424 uint32 now_second = ((now_us << 18) / base::Time::kMicrosecondsPerSecond) &
425 0x00FFFFFF;
426 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle.
427 extn_data[0] = static_cast<uint8>(now_second >> 16);
428 extn_data[1] = static_cast<uint8>(now_second >> 8);
429 extn_data[2] = static_cast<uint8>(now_second);
430 }
431
432 // Assumes |len| is actual packet length + tag length.
433 void MaybeUpdateRtpAuthTag(char* rtp, int len,
434 const talk_base::PacketOptions& options) {
435 size_t tag_length = options.packet_time_params.srtp_auth_tag_len;
436 char* auth_tag = rtp + (len - tag_length);
437
438 // We should have a fake HMAC value @ auth_tag.
439 DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
440
441 crypto::HMAC hmac(crypto::HMAC::SHA1);
442 if (!hmac.Init(reinterpret_cast<const unsigned char*>(
443 &options.packet_time_params.srtp_auth_key[0]),
444 options.packet_time_params.srtp_auth_key.size())) {
445 NOTREACHED();
446 return;
447 }
448
449 if (hmac.DigestLength() < tag_length) {
450 NOTREACHED();
451 return;
452 }
453
454 // Copy ROC after end of rtp packet.
455 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, 4);
456
457 unsigned char output[64];
458 size_t out_len = sizeof(output);
459 DCHECK(!hmac.Sign(rtp, output, out_len));
460
461 // Copy HMAC from output to packet. This is required as auth tag length
462 // may not be equal to the actual HMAC length.
463 memcpy(auth_tag, output, tag_length);
464 }
465
105 } // namespace content 466 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698