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

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 P2PSocketHost::MaybeUpdatePacketSendTimeExtn(
153 char* data, int length, const talk_base::PacketOptions& options) {
154 if (length <= 0) {
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) {
Solis 2014/02/26 15:49:34 You know this from the check at the beginning of t
Mallinath (Gone from Chromium) 2014/02/26 19:59:20 At the beginning i am checking both extension id a
186 if (!MaybeUpdateRtpSendTimeExtn(
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 P2PSocketHost::GetRtpPacketStartPositionAndLength(
199 char* packet, int length, int* rtp_start_pos, int* rtp_packet_length) {
200 if (IsTurnChannelData(packet)) {
201 // Turn Channel Message header format.
202 // 0 1 2 3
203 // 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
204 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205 // | Channel Number | Length |
206 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207 // | |
208 // / Application Data /
209 // / /
210 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211 if (length < kTurnChannelHdrLen) {
212 return false;
213 }
214
215 *rtp_start_pos = kTurnChannelHdrLen;
Solis 2014/02/26 15:49:34 I strongly advise against changing an output param
Mallinath (Gone from Chromium) 2014/02/26 19:59:20 Done.
216 *rtp_packet_length = talk_base::GetBE16(&packet[2]);
217 if (length < *rtp_packet_length + kTurnChannelHdrLen) {
218 return false;
219 }
220 } else if (IsTurnSendIndicationPacket(packet)) {
221 if (length <= kStunHeaderSize) {
222 // Message must be greater than 20 bytes, if it's carrying any payload.
223 return false;
224 }
225 // Validate STUN message length.
226 int stun_msg_len = talk_base::GetBE16(&packet[2]);
227 if (stun_msg_len + kStunHeaderSize != length) {
228 return false;
229 }
230
231 // First skip mandatory stun header which is of 20 bytes.
232 *rtp_start_pos = kStunHeaderSize;
233 // Loop through STUN attributes until we find STUN DATA attribute.
234 char* start = packet + *rtp_start_pos;
235 bool data_attr_present = false;
236 while ((packet + *rtp_start_pos) - start < length) {
237 // Keep reading STUN attributes until we hit DATA attribute.
238 // Attribute will be a TLV structure.
239 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240 // | Type | Length |
241 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
242 // | Value (variable) ....
243 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
244 // The value in the length field MUST contain the length of the Value
245 // part of the attribute, prior to padding, measured in bytes. Since
246 // STUN aligns attributes on 32-bit boundaries, attributes whose content
247 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
248 // padding so that its value contains a multiple of 4 bytes. The
249 // padding bits are ignored, and may be any value.
250 uint16 attr_type, attr_length;
251 // Getting attribute type and length.
252 attr_type = talk_base::GetBE16(&packet[*rtp_start_pos]);
253 attr_length = talk_base::GetBE16(
254 &packet[*rtp_start_pos + sizeof(attr_type)]);
255 // Checking for bogus attribute length.
256 if (length < attr_length + *rtp_start_pos) {
257 return false;
258 }
259 if (attr_type != cricket::STUN_ATTR_DATA) {
260 *rtp_start_pos += sizeof(attr_type) + sizeof(attr_length) + attr_length;
261 if ((attr_length % 4) != 0) {
262 *rtp_start_pos += (4 - (attr_length % 4));
263 }
264 continue;
265 }
266 data_attr_present = true;
267 *rtp_start_pos += 4; // Skip STUN_DATA_ATTR header.
268 *rtp_packet_length = attr_length;
269 // One final check of length before exiting.
270 if (length < *rtp_packet_length + *rtp_start_pos)
271 return false;
272
273 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet.
274 break;
275 }
276
277 if (!data_attr_present) {
278 // There is no data attribute present in the message. We can't do anything
279 // with the message.
280 return false;
281 }
282
283 } else {
284 // This is a raw RTP packet.
285 *rtp_start_pos = 0;
286 *rtp_packet_length = length;
287 }
288
289 return (IsRtpPacket(packet + *rtp_start_pos, *rtp_packet_length)) ?
290 ValidateRtpHeader(packet + *rtp_start_pos, *rtp_packet_length) : false;
291 }
292
293 // Verifies rtp header and message length.
294 bool P2PSocketHost::ValidateRtpHeader(char* rtp, int length) {
295 int cc_count = rtp[0] & 0x0F;
296 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
297 if (rtp_hdr_len_without_extn > length) {
298 return false;
299 }
300
301 bool X = (rtp[0] & 0x10);
302 if (!X) // Return if extension bit is not set.
303 return true;
304
305 rtp += rtp_hdr_len_without_extn;
306
307 // Getting extension profile length.
308 // Length is in 32 bit words.
309 uint16 extn_length = talk_base::GetBE16(rtp + 2) * 4;
310
311 // Verify input length against total header size.
312 if (rtp_hdr_len_without_extn + kRtpExtnHdrLen + extn_length > length) {
313 return false;
314 }
315 return true;
316 }
317
318 // ValidateRtpHeader must be called before this method to make sure, we have
319 // a sane rtp packet.
320 bool P2PSocketHost::MaybeUpdateRtpSendTimeExtn(char* rtp, int length,
321 int extension_id) {
322 // 0 1 2 3
323 // 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
324 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
325 // |V=2|P|X| CC |M| PT | sequence number |
326 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
327 // | timestamp |
328 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
329 // | synchronization source (SSRC) identifier |
330 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
331 // | contributing source (CSRC) identifiers |
332 // | .... |
333 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
334
335 bool X = (rtp[0] & 0x10);
336 if (!X) // Return if extension bit is not set.
337 return true;
338
339 int cc_count = rtp[0] & 0x0F;
340 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
341
342 rtp += rtp_hdr_len_without_extn;
343
344 // Getting extension profile ID and length.
345 uint16 profile_id = talk_base::GetBE16(rtp);
346 // Length is in 32 bit words.
347 uint16 extn_length = talk_base::GetBE16(rtp + 2) * 4;
348
349 rtp += kRtpExtnHdrLen; // Moving past extn header.
350
351 bool found = false;
352 // WebRTC is using one byte header extension.
353 // TODO(mallinath) - Handle two byte header extension.
354 if (profile_id == 0xBEDE) { // OneByte extension header
355 // 0
356 // 0 1 2 3 4 5 6 7
357 // +-+-+-+-+-+-+-+-+
358 // | ID | len |
359 // +-+-+-+-+-+-+-+-+
360
361 // 0 1 2 3
362 // 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
363 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
364 // | 0xBE | 0xDE | length=3 |
365 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366 // | ID | L=0 | data | ID | L=1 | data...
367 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
368 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
369 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
370 // | data |
371 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
372 char* extn_start = rtp;
373 while (rtp - extn_start < extn_length) {
374 const int id = (*rtp & 0xF0) >> 4;
375 const int len = (*rtp & 0x0F) + 1;
376 // The 4-bit length is the number minus one of data bytes of this header
377 // extension element following the one-byte header.
378 if (id == extension_id) {
379 UpdateSendTimeExtnValue(rtp + kOneByteHdrLen, len);
380 found = true;
381 break;
382 }
383 rtp += kOneByteHdrLen + len;
384 // Counting padding bytes
385 while ((*rtp != 0) && (rtp - extn_start < extn_length)) {
386 ++rtp;
387 }
388 }
389 }
390 return found;
391 }
392
393 void P2PSocketHost::UpdateSendTimeExtnValue(char* extn_data, int len) {
394 // Absolute send time in RTP streams.
395 //
396 // The absolute send time is signaled to the receiver in-band using the
397 // general mechanism for RTP header extensions [RFC5285]. The payload
398 // of this extension (the transmitted value) is a 24-bit unsigned integer
399 // containing the sender's current time in seconds as a fixed point number
400 // with 18 bits fractional part.
401 //
402 // The form of the absolute send time extension block:
403 //
404 // 0 1 2 3
405 // 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
406 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
407 // | ID | len=2 | absolute send time |
408 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
409 DCHECK_EQ(len, kAbsSendTimeExtnLen);
410 // Now() has resolution ~1-15ms, using HighResNow(). But it is warned not to
411 // use it unless necessary, as it is expensive than Now().
412 uint64 now_us =
413 (base::TimeTicks::HighResNow() - base::TimeTicks()).InMicroseconds();
414 // Convert second to 24-bit unsigned with 18 bit fractional part
415 uint32 now_second = ((now_us << 18) / base::Time::kMicrosecondsPerSecond) &
416 0x00FFFFFF;
417 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle.
418 extn_data[0] = static_cast<uint8>(now_second >> 16);
419 extn_data[1] = static_cast<uint8>(now_second >> 8);
420 extn_data[2] = static_cast<uint8>(now_second);
421 }
422
423 // Assumes |len| is actual packet length + tag length.
424 void P2PSocketHost::MaybeUpdateRtpAuthTag(
425 char* rtp, int len, const talk_base::PacketOptions& options) {
426 size_t tag_length = options.packet_time_params.srtp_auth_tag_len;
427 char* auth_tag = rtp + (len - tag_length);
428
429 // We should have a fake HMAC value @ auth_tag.
430 DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
431
432 crypto::HMAC hmac(crypto::HMAC::SHA1);
433 if (!hmac.Init(reinterpret_cast<const unsigned char*>(
434 &options.packet_time_params.srtp_auth_key[0]),
435 options.packet_time_params.srtp_auth_key.size())) {
436 NOTREACHED();
437 return;
438 }
439
440 if (hmac.DigestLength() < tag_length) {
441 NOTREACHED();
442 return;
443 }
444
445 // Copy ROC after end of rtp packet.
446 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, 4);
447
448 unsigned char output[64];
449 size_t out_len = sizeof(output);
450 DCHECK(!hmac.Sign(rtp, output, out_len));
451
452 // Copy HMAC from output to packet. This is required as auth tag length
453 // may not be equal to the actual HMAC length.
454 memcpy(auth_tag, output, tag_length);
455 }
456
105 } // namespace content 457 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698