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 static const unsigned char kFakeHmac[10] = { | |
juberti2
2014/02/22 01:16:05
kFakeAuthTag. Add a comment describing this
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
28 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd | |
29 }; | |
30 | |
31 bool IsTurnChannelData(const char* data) { | |
juberti2
2014/02/22 01:16:05
can just look at first byte, like below: (*data &
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
32 uint16 type = talk_base::GetBE16(data); | |
33 return ((type & 0xC000) == 0x4000); // MSB are 0b01 | |
34 } | |
35 | |
36 bool IsDtlsPacket(const char* data, int len) { | |
37 const uint8* u = reinterpret_cast<const uint8*>(data); | |
38 return (len >= kDtlsRecordHeaderLen && (u[0] > 19 && u[0] < 64)); | |
39 } | |
40 | |
41 bool IsRtcpPacket(const char* data) { | |
42 int type = (static_cast<uint8>(data[1]) & 0x7F); | |
43 return (type >= 64 && type < 96); | |
44 } | |
45 | |
46 bool IsStunPacket(const char* data) { | |
47 uint16 type = talk_base::GetBE16(data); | |
juberti2
2014/02/22 01:16:05
can just look at first byte and do ((*data & 0xC0)
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
48 return !(type & 0xC000); | |
49 } | |
50 | |
51 bool IsTurnDataIndicationPacket(const char* data) { | |
juberti2
2014/02/22 01:16:05
SendIndication
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
52 uint16 type = talk_base::GetBE16(data); | |
53 return (type == cricket::TURN_SEND_INDICATION); | |
54 } | |
55 | |
56 bool IsRtpPacket(const char* data, int len) { | |
57 if (len < kMinRtpHdrLen) | |
juberti2
2014/02/22 01:16:05
probably not necessary to check this here, since y
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
58 return false; | |
59 // Version number must be 2. | |
60 return ((static_cast<int>((*data >> 6) & 0x3)) == 2) ? true : false; | |
juberti2
2014/02/22 01:16:05
To be consistent with STUN check, do (*data & 0xC0
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
61 } | |
62 | |
14 } // namespace | 63 } // namespace |
15 | 64 |
16 namespace content { | 65 namespace content { |
17 | 66 |
18 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, | 67 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, |
19 int id) | 68 int id) |
20 : message_sender_(message_sender), | 69 : message_sender_(message_sender), |
21 id_(id), | 70 id_(id), |
22 state_(STATE_UNINITIALIZED) { | 71 state_(STATE_UNINITIALIZED) { |
23 } | 72 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 case P2P_SOCKET_STUN_TCP_CLIENT: | 144 case P2P_SOCKET_STUN_TCP_CLIENT: |
96 case P2P_SOCKET_STUN_SSLTCP_CLIENT: | 145 case P2P_SOCKET_STUN_SSLTCP_CLIENT: |
97 case P2P_SOCKET_STUN_TLS_CLIENT: | 146 case P2P_SOCKET_STUN_TLS_CLIENT: |
98 return new P2PSocketHostStunTcp(message_sender, id, type, url_context); | 147 return new P2PSocketHostStunTcp(message_sender, id, type, url_context); |
99 } | 148 } |
100 | 149 |
101 NOTREACHED(); | 150 NOTREACHED(); |
102 return NULL; | 151 return NULL; |
103 } | 152 } |
104 | 153 |
154 void P2PSocketHost::MaybeUpdateRtpSendTimeExtn( | |
155 char* data, int length, const talk_base::PacketOptions& options) { | |
156 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in | |
157 // PacketOptions, nothing to be updated in this packet. | |
158 if (options.packet_time_params.rtp_sendtime_extension_id == -1 && | |
159 options.packet_time_params.srtp_auth_key.empty()) { | |
160 return; | |
161 } | |
162 | |
163 DCHECK(!IsDtlsPacket(data, length)); | |
164 DCHECK(!IsRtcpPacket(data)); | |
165 | |
166 // If there is a srtp auth key present then packet must be a RTP packet. | |
167 // RTP packet may have been wrapped in a TURN Channel Data or | |
168 // TURN send indication. | |
169 int rtp_start_pos = -1; | |
170 if (!GetRtpPacketStartPosition(data, length, &rtp_start_pos)) { | |
171 // This method should never return false. | |
juberti2
2014/02/22 01:16:05
Use NOTREACHED then?
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
172 return; | |
173 } | |
174 | |
175 // Skip to rtp packet. | |
176 char* start = data + rtp_start_pos; | |
177 int rtp_packet_length = length - rtp_start_pos; | |
178 // If there a non negetive sendtime extension id present in packet options, | |
179 // then we should parse the rtp packet to update the timestamp. Otherwise | |
180 // just calculate HMAC and update packet with it. | |
181 if (options.packet_time_params.rtp_sendtime_extension_id != -1) { | |
182 if (!UpdateRtpAbsSendTimeExtension( | |
183 start, rtp_packet_length, | |
184 options.packet_time_params.rtp_sendtime_extension_id)) { | |
185 // We should find an extension id in the rtp packet. | |
186 DCHECK(false); | |
juberti2
2014/02/22 01:16:05
NOTREACHED; return;
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
187 } | |
188 } | |
189 | |
190 UpdateHmac(start, rtp_packet_length, options); | |
191 } | |
192 | |
193 bool P2PSocketHost::GetRtpPacketStartPosition(char* packet, int length, | |
194 int* rtp_start_pos) { | |
195 | |
196 if (IsTurnChannelData(packet)) { | |
197 *rtp_start_pos = kTurnChannelHdrLen; | |
198 } else if (IsTurnDataIndicationPacket(packet)) { | |
199 // Parsing a TURN SEND INDICATION message is bit tricky as it can have | |
juberti2
2014/02/22 01:16:05
I think it would be better to iterate the STUN att
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
200 // variable length peer address attribute. So we can not directly skip to | |
201 // STUN_ATTR_DATA attribute structure and get payload length. We have to | |
202 // to run through cricket::TurnMessage. This can be bit expensive, but | |
203 // good news is that libjingle will send data using Data indication only | |
204 // for the first few packets, until it completes TURN channel binding. | |
205 talk_base::ByteBuffer buf(packet, length); | |
206 cricket::TurnMessage msg; | |
207 if (!msg.Read(&buf)) { | |
208 return false; | |
209 } | |
210 const cricket::StunByteStringAttribute* data_attr = | |
211 msg.GetByteString(cricket::STUN_ATTR_DATA); | |
212 if (!data_attr) { | |
213 return false; | |
214 } | |
215 int payload_len = data_attr->length(); | |
216 // Now we have actual payload length and also total packet length. | |
217 // PACKET = TURN_DATA_INDICATION + Pay load. We can get header length by | |
218 // substracting payload length from packet length. | |
219 *rtp_start_pos = length - payload_len; | |
juberti2
2014/02/22 01:16:05
This is fragile since there is no guarantee that t
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
220 } else { | |
221 // This is a raw RTP packet. | |
222 *rtp_start_pos = 0; | |
223 } | |
224 DCHECK(!IsRtpPacket(packet + *rtp_start_pos, length - *rtp_start_pos)); | |
juberti2
2014/02/22 01:16:05
shouldn't there be no !
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
225 return true; | |
226 } | |
227 | |
228 bool P2PSocketHost::UpdateRtpAbsSendTimeExtension(char* data, int length, | |
juberti2
2014/02/22 01:16:05
instead of |data|, perhaps call this |rtp| to make
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
229 const int extension_id) { | |
230 // 0 1 2 3 | |
231 // 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 | |
232 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
233 // |V=2|P|X| CC |M| PT | sequence number | | |
234 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
235 // | timestamp | | |
236 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
237 // | synchronization source (SSRC) identifier | | |
238 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
239 // | contributing source (CSRC) identifiers | | |
240 // | .... | | |
241 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
242 | |
243 int cc_count = data[0] & 0x0F; | |
244 int rtp_hdr_len_without_extn = kMinRtpHdrLen + 4 * cc_count; | |
245 if (rtp_hdr_len_without_extn > length) { | |
246 DCHECK(false); | |
juberti2
2014/02/22 01:16:05
NOTREACHED
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
247 return false; | |
248 } | |
249 | |
250 bool X = (data[0] & 0x10); | |
251 if (!X) // Return if extension bit is not set. | |
252 return true; | |
253 | |
254 data += rtp_hdr_len_without_extn; | |
255 | |
256 // Getting extension profile ID and length. | |
257 uint16 profile_id = talk_base::GetBE16(data); | |
258 // Length is in 32 bit words. | |
259 uint16 extn_length = talk_base::GetBE16(data + 2) * 4; | |
260 | |
261 // Verify input length against total header size. | |
262 if (rtp_hdr_len_without_extn + kRtpExtnHdrLen + extn_length > length) { | |
263 DCHECK(false); | |
juberti2
2014/02/22 01:16:05
NOTREACHED
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
264 return false; | |
265 } | |
266 | |
267 data += kRtpExtnHdrLen; // Moving past extn header. | |
268 | |
269 // WebRTC is using one byte header extension. | |
270 // TODO(mallinath) - Handle two byte header extension. | |
271 if (profile_id == 0xBEDE) { // OneByte extension header | |
272 // 0 | |
273 // 0 1 2 3 4 5 6 7 | |
274 // +-+-+-+-+-+-+-+-+ | |
275 // | ID | len | | |
276 // +-+-+-+-+-+-+-+-+ | |
277 | |
278 // 0 1 2 3 | |
279 // 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 | |
280 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
281 // | 0xBE | 0xDE | length=3 | | |
282 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
283 // | ID | L=0 | data | ID | L=1 | data... | |
284 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
285 // ...data | 0 (pad) | 0 (pad) | ID | L=3 | | |
286 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
287 // | data | | |
288 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
289 char* extn_start = data; | |
290 while(data - extn_start < extn_length) { | |
juberti2
2014/02/22 01:16:05
while (
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
291 const int id = (*data & 0xF0) >> 4; | |
292 const int len = (*data & 0x0F) + 1; | |
293 // The 4-bit length is the number minus one of data bytes of this header | |
294 // extension element following the one-byte header. | |
295 if (id == extension_id) { | |
296 UpdateAbsSendtime(data + kOneByteHdrLen, len); | |
297 break; | |
298 } | |
299 data += kOneByteHdrLen + len; | |
300 // Counting padding bytes | |
301 while (*data != 0) { | |
juberti2
2014/02/22 01:16:05
need to check that we don't go off the end here -
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
I am not sure we can calculate padding explicitly
juberti2
2014/02/25 00:48:24
I see. Should this while be checking *rtp == 0?
| |
302 ++data; | |
303 } | |
304 } | |
305 } | |
306 return true; | |
307 } | |
308 | |
309 void P2PSocketHost::UpdateAbsSendtime(char* data, int len) { | |
juberti2
2014/02/22 01:16:05
instead of |data|, call this |extn_data| or someth
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
310 // Absolute send time in RTP streams. | |
311 // | |
312 // The absolute send time is signaled to the receiver in-band using the | |
313 // general mechanism for RTP header extensions [RFC5285]. The payload | |
314 // of this extension (the transmitted value) is a 24-bit unsigned integer | |
315 // containing the sender's current time in seconds as a fixed point number | |
316 // with 18 bits fractional part. | |
317 // | |
318 // The form of the absolute send time extension block: | |
319 // | |
320 // 0 1 2 3 | |
321 // 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 | |
322 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
323 // | ID | len=2 | absolute send time | | |
324 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
325 DCHECK_EQ(len, kAbsSendTimeExtnLen); | |
326 // Now() has resolution ~1-15ms, using HighResNow(). But it is warned not to | |
327 // use it unless necessary, as it is expensive than Now(). | |
328 uint64 now_us = | |
329 (base::TimeTicks::HighResNow() - base::TimeTicks()).InMicroseconds(); | |
330 // Convert second to 24-bit unsigned with 18 bit fractional part | |
331 uint32 now_second = ((now_us << 18) / base::Time::kMicrosecondsPerSecond) & | |
332 0x00FFFFFF; | |
333 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle. | |
334 data[0] = static_cast<uint8>(now_second >> 16); | |
335 data[1] = static_cast<uint8>(now_second >> 8); | |
336 data[2] = static_cast<uint8>(now_second); | |
337 } | |
338 | |
339 // Assumes |len| is actual packet length + tag length. | |
340 void P2PSocketHost::UpdateHmac(char* packet, int len, | |
juberti2
2014/02/22 01:16:05
packet -> rtp
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
341 const talk_base::PacketOptions& options) { | |
342 int tag_length = options.packet_time_params.srtp_auth_tag_len; | |
343 char* auth_tag = packet + (len - tag_length); | |
344 | |
345 // Some checking before calculating HMAC. | |
346 DCHECK_LT(tag_length, len); | |
347 DCHECK_LT(tag_length, 64); // HMAC output. | |
juberti2
2014/02/22 01:16:05
make sure it is less than hmac.Size
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
348 // We should have a fake HMAC value @ auth_tag. | |
349 DCHECK_EQ(0, memcmp(auth_tag, kFakeHmac, tag_length)); | |
350 | |
351 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
352 if (!hmac.Init(reinterpret_cast<const unsigned char*>( | |
353 &options.packet_time_params.srtp_auth_key[0]), | |
354 options.packet_time_params.srtp_auth_key.size())) { | |
355 DCHECK(false); | |
juberti2
2014/02/22 01:16:05
NOTREACHED
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
356 return; | |
357 } | |
358 | |
359 // Copy ROC after end of rtp packet. | |
360 memcpy(auth_tag, reinterpret_cast<const void*>( | |
juberti2
2014/02/22 01:16:05
don't need to cast
Mallinath (Gone from Chromium)
2014/02/25 00:20:02
Done.
| |
361 &options.packet_time_params.srtp_packet_index), 4); | |
362 | |
363 unsigned char output[64]; | |
364 size_t out_len = sizeof(output); | |
365 DCHECK(!hmac.Sign(packet, output, out_len)); | |
366 | |
367 // Copy HMAC from output to packet. This is required as auth tag length | |
368 // may not be equal to the actual HMAC length. | |
369 memcpy(auth_tag, output, options.packet_time_params.srtp_auth_tag_len); | |
370 } | |
371 | |
105 } // namespace content | 372 } // namespace content |
OLD | NEW |