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

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
65 namespace packet_processing_helpers {
66
67 bool ValidateRtpHeader(char* rtp, int length);
Solis 2014/03/03 10:22:29 These can go in the anonymous namespace?
Mallinath (Gone from Chromium) 2014/03/04 00:15:46 yea, they can. The reason I kept in the same names
Solis 2014/03/04 09:53:24 But putting them in the anonymous namespace signal
Mallinath (Gone from Chromium) 2014/03/06 07:39:43 Done.
68 void UpdateAbsSendTimeExtnValue(char* data, int len);
69 void MaybeUpdateRtpAuthTag(char* packet, int len,
70 const talk_base::PacketOptions& options);
71
72 void MaybeUpdatePacketAbsSendTimeExtn(
73 char* data, int length, const talk_base::PacketOptions& options) {
74 DCHECK(data != NULL);
75 DCHECK(length > 0);
76 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
77 // PacketOptions, nothing to be updated in this packet.
78 if (options.packet_time_params.rtp_sendtime_extension_id == -1 &&
79 options.packet_time_params.srtp_auth_key.empty()) {
80 return;
81 }
82
83 DCHECK(!IsDtlsPacket(data, length));
84 DCHECK(!IsRtcpPacket(data));
85
86 // If there is a srtp auth key present then packet must be a RTP packet.
87 // RTP packet may have been wrapped in a TURN Channel Data or
88 // TURN send indication.
89 int rtp_start_pos;
90 int rtp_length;
91 if (!GetRtpPacketStartPositionAndLength(
92 data, length, &rtp_start_pos, &rtp_length)) {
93 // This method should never return false.
94 NOTREACHED();
95 return;
96 }
97
98 // Skip to rtp packet.
99 char* start = data + rtp_start_pos;
100 // If there a non negetive sendtime extension id present in packet options,
Solis 2014/03/04 09:53:24 spelling
Mallinath (Gone from Chromium) 2014/03/06 07:39:43 Done.
101 // then we should parse the rtp packet to update the timestamp. Otherwise
102 // just calculate HMAC and update packet with it.
103 if (options.packet_time_params.rtp_sendtime_extension_id != -1) {
104 if (!MaybeUpdateRtpAbsSendTimeExtn(
105 start, rtp_length,
106 options.packet_time_params.rtp_sendtime_extension_id)) {
107 // We should find an extension id in the rtp packet.
108 NOTREACHED();
109 return;
110 }
111 }
112
113 MaybeUpdateRtpAuthTag(start, rtp_length, options);
Solis 2014/03/03 10:22:29 It seems MaybeUpdatePacketAbsSendTimeExtn() should
Mallinath (Gone from Chromium) 2014/03/04 00:15:46 I don't have better options than MaybeUpdatePacket
Solis 2014/03/04 09:53:24 It's a bit of swiss-army-knife type of function -
Mallinath (Gone from Chromium) 2014/03/06 07:39:43 You are talking about MaybeUpdatePacketAbsSendTime
Solis 2014/03/06 09:41:53 The comment was in response to a thread concerning
Mallinath (Gone from Chromium) 2014/03/06 16:18:13 I would like to get this CL in first before doing
114 }
115
116 bool GetRtpPacketStartPositionAndLength(
117 char* packet, int length, int* rtp_start_pos, int* rtp_packet_length) {
118 int rtp_begin, rtp_length;
119 if (IsTurnChannelData(packet)) {
120 // Turn Channel Message header format.
121 // 0 1 2 3
122 // 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
123 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124 // | Channel Number | Length |
125 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 // | |
127 // / Application Data /
128 // / /
129 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 if (length < kTurnChannelHdrLen) {
131 return false;
132 }
133
134 rtp_begin = kTurnChannelHdrLen;
135 rtp_length = talk_base::GetBE16(&packet[2]);
136 if (length < rtp_length + kTurnChannelHdrLen) {
137 return false;
138 }
139 } else if (IsTurnSendIndicationPacket(packet)) {
140 if (length <= P2PSocketHost::kStunHeaderSize) {
141 // Message must be greater than 20 bytes, if it's carrying any payload.
142 return false;
143 }
144 // Validate STUN message length.
145 int stun_msg_len = talk_base::GetBE16(&packet[2]);
146 if (stun_msg_len + P2PSocketHost::kStunHeaderSize != length) {
147 return false;
148 }
149
150 // First skip mandatory stun header which is of 20 bytes.
151 rtp_begin = P2PSocketHost::kStunHeaderSize;
152 // Loop through STUN attributes until we find STUN DATA attribute.
153 char* start = packet + rtp_begin;
154 bool data_attr_present = false;
155 while ((packet + rtp_begin) - start < length) {
156 // Keep reading STUN attributes until we hit DATA attribute.
157 // Attribute will be a TLV structure.
158 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 // | Type | Length |
160 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 // | Value (variable) ....
162 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 // The value in the length field MUST contain the length of the Value
164 // part of the attribute, prior to padding, measured in bytes. Since
165 // STUN aligns attributes on 32-bit boundaries, attributes whose content
166 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
167 // padding so that its value contains a multiple of 4 bytes. The
168 // padding bits are ignored, and may be any value.
169 uint16 attr_type, attr_length;
170 // Getting attribute type and length.
171 attr_type = talk_base::GetBE16(&packet[rtp_begin]);
172 attr_length = talk_base::GetBE16(
173 &packet[rtp_begin + sizeof(attr_type)]);
174 // Checking for bogus attribute length.
175 if (length < attr_length + rtp_begin) {
176 return false;
177 }
178
179 if (attr_type != cricket::STUN_ATTR_DATA) {
180 rtp_begin += sizeof(attr_type) + sizeof(attr_length) + attr_length;
181 if ((attr_length % 4) != 0) {
182 rtp_begin += (4 - (attr_length % 4));
183 }
184 continue;
185 }
186
187 data_attr_present = true;
188 rtp_begin += 4; // Skip STUN_DATA_ATTR header.
189 rtp_length = attr_length;
190 // One final check of length before exiting.
191 if (length < rtp_length + rtp_begin) {
192 return false;
193 }
194 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet.
195 break;
196 }
197
198 if (!data_attr_present) {
199 // There is no data attribute present in the message. We can't do anything
200 // with the message.
201 return false;
202 }
203
204 } else {
205 // This is a raw RTP packet.
206 rtp_begin = 0;
207 rtp_length = length;
208 }
209
210 // Making sure we have a valid RTP packet at the end.
211 if (IsRtpPacket(packet + rtp_begin, rtp_length) &&
212 ValidateRtpHeader(packet + rtp_begin, rtp_length)) {
213 *rtp_start_pos = rtp_begin;
214 *rtp_packet_length = rtp_length;
215 return true;
216 }
217 return false;
218 }
219
220 // ValidateRtpHeader must be called before this method to make sure, we have
221 // a sane rtp packet.
222 bool MaybeUpdateRtpAbsSendTimeExtn(char* rtp, int length, int extension_id) {
223 // 0 1 2 3
224 // 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
225 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
226 // |V=2|P|X| CC |M| PT | sequence number |
227 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
228 // | timestamp |
229 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
230 // | synchronization source (SSRC) identifier |
231 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
232 // | contributing source (CSRC) identifiers |
233 // | .... |
234 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235
236 bool X = (rtp[0] & 0x10);
237 if (!X) // Return if extension bit is not set.
238 return true;
239
240 int cc_count = rtp[0] & 0x0F;
241 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
242
243 rtp += rtp_hdr_len_without_extn;
244
245 // Getting extension profile ID and length.
246 uint16 profile_id = talk_base::GetBE16(rtp);
247 // Length is in 32 bit words.
248 uint16 extn_length = talk_base::GetBE16(rtp + 2) * 4;
249
250 rtp += kRtpExtnHdrLen; // Moving past extn header.
251
252 bool found = false;
253 // WebRTC is using one byte header extension.
254 // TODO(mallinath) - Handle two byte header extension.
255 if (profile_id == 0xBEDE) { // OneByte extension header
256 // 0
257 // 0 1 2 3 4 5 6 7
258 // +-+-+-+-+-+-+-+-+
259 // | ID | len |
260 // +-+-+-+-+-+-+-+-+
261
262 // 0 1 2 3
263 // 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
264 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
265 // | 0xBE | 0xDE | length=3 |
266 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
267 // | ID | L=0 | data | ID | L=1 | data...
268 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
269 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
270 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
271 // | data |
272 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
273 char* extn_start = rtp;
274 while (rtp - extn_start < extn_length) {
275 const int id = (*rtp & 0xF0) >> 4;
276 const int len = (*rtp & 0x0F) + 1;
277 // The 4-bit length is the number minus one of data bytes of this header
278 // extension element following the one-byte header.
279 if (id == extension_id) {
280 UpdateAbsSendTimeExtnValue(rtp + kOneByteHdrLen, len);
281 found = true;
282 break;
283 }
284 rtp += kOneByteHdrLen + len;
285 // Counting padding bytes
286 while ((*rtp != 0) && (rtp - extn_start < extn_length)) {
287 ++rtp;
288 }
289 }
290 }
291 return found;
292 }
293
294 // Verifies rtp header and message length.
295 bool ValidateRtpHeader(char* rtp, int length) {
296 int cc_count = rtp[0] & 0x0F;
297 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count;
298 if (rtp_hdr_len_without_extn > length) {
299 return false;
300 }
301
302 // If extension bit is not set, we are done with header processing, as input
303 // length is verified above.
304 bool X = (rtp[0] & 0x10);
305 if (!X)
306 return true;
307
308 rtp += rtp_hdr_len_without_extn;
309
310 // Getting extension profile length.
311 // Length is in 32 bit words.
312 uint16 extn_length = talk_base::GetBE16(rtp + 2) * 4;
313
314 // Verify input length against total header size.
315 if (rtp_hdr_len_without_extn + kRtpExtnHdrLen + extn_length > length) {
316 return false;
317 }
318 return true;
319 }
320
321 void UpdateAbsSendTimeExtnValue(char* extn_data, int len) {
322 // Absolute send time in RTP streams.
323 //
324 // The absolute send time is signaled to the receiver in-band using the
325 // general mechanism for RTP header extensions [RFC5285]. The payload
326 // of this extension (the transmitted value) is a 24-bit unsigned integer
327 // containing the sender's current time in seconds as a fixed point number
328 // with 18 bits fractional part.
329 //
330 // The form of the absolute send time extension block:
331 //
332 // 0 1 2 3
333 // 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
334 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
335 // | ID | len=2 | absolute send time |
336 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337 DCHECK_EQ(len, kAbsSendTimeExtnLen);
338 // Now() has resolution ~1-15ms, using HighResNow(). But it is warned not to
339 // use it unless necessary, as it is expensive than Now().
340 uint64 now_us =
341 (base::TimeTicks::HighResNow() - base::TimeTicks()).InMicroseconds();
342 // Convert second to 24-bit unsigned with 18 bit fractional part
343 uint32 now_second = ((now_us << 18) / base::Time::kMicrosecondsPerSecond) &
344 0x00FFFFFF;
345 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle.
346 extn_data[0] = static_cast<uint8>(now_second >> 16);
347 extn_data[1] = static_cast<uint8>(now_second >> 8);
348 extn_data[2] = static_cast<uint8>(now_second);
349 }
350
351 // Assumes |len| is actual packet length + tag length.
352 void MaybeUpdateRtpAuthTag(char* rtp, int len,
353 const talk_base::PacketOptions& options) {
354 // If there is no key, return.
355 if (options.packet_time_params.srtp_auth_key.empty())
356 return;
357
358 size_t tag_length = options.packet_time_params.srtp_auth_tag_len;
359 char* auth_tag = rtp + (len - tag_length);
360
361 // We should have a fake HMAC value @ auth_tag.
362 DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
363
364 crypto::HMAC hmac(crypto::HMAC::SHA1);
365 if (!hmac.Init(reinterpret_cast<const unsigned char*>(
366 &options.packet_time_params.srtp_auth_key[0]),
Solis 2014/03/04 09:53:24 nit: formatting
Mallinath (Gone from Chromium) 2014/03/06 07:39:43 Done.
367 options.packet_time_params.srtp_auth_key.size())) {
368 NOTREACHED();
369 return;
370 }
371
372 if (hmac.DigestLength() < tag_length) {
373 NOTREACHED();
374 return;
375 }
376
377 // Copy ROC after end of rtp packet.
378 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, 4);
379
380 unsigned char output[64];
381 size_t out_len = sizeof(output);
Solis 2014/03/04 09:53:24 nit: no need for this temp variable.
Mallinath (Gone from Chromium) 2014/03/06 07:39:43 Done.
382 DCHECK(!hmac.Sign(rtp, output, out_len));
383
384 // Copy HMAC from output to packet. This is required as auth tag length
385 // may not be equal to the actual HMAC length.
386 memcpy(auth_tag, output, tag_length);
387 }
388
389 } // packet_processing_helpers
390
18 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, 391 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender,
19 int id) 392 int id)
20 : message_sender_(message_sender), 393 : message_sender_(message_sender),
21 id_(id), 394 id_(id),
22 state_(STATE_UNINITIALIZED) { 395 state_(STATE_UNINITIALIZED) {
23 } 396 }
24 397
25 P2PSocketHost::~P2PSocketHost() { } 398 P2PSocketHost::~P2PSocketHost() { }
26 399
27 // Verifies that the packet |data| has a valid STUN header. 400 // Verifies that the packet |data| has a valid STUN header.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 case P2P_SOCKET_STUN_SSLTCP_CLIENT: 469 case P2P_SOCKET_STUN_SSLTCP_CLIENT:
97 case P2P_SOCKET_STUN_TLS_CLIENT: 470 case P2P_SOCKET_STUN_TLS_CLIENT:
98 return new P2PSocketHostStunTcp(message_sender, id, type, url_context); 471 return new P2PSocketHostStunTcp(message_sender, id, type, url_context);
99 } 472 }
100 473
101 NOTREACHED(); 474 NOTREACHED();
102 return NULL; 475 return NULL;
103 } 476 }
104 477
105 } // namespace content 478 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698