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