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