OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
12 * and/or other materials provided with the distribution. | 12 * and/or other materials provided with the distribution. |
13 * 3. The name of the author may not be used to endorse or promote products | 13 * 3. The name of the author may not be used to endorse or promote products |
14 * derived from this software without specific prior written permission. | 14 * derived from this software without specific prior written permission. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include "talk/media/base/rtputils.h" | 28 #include "talk/media/base/rtputils.h" |
29 | 29 |
| 30 #include "talk/media/base/turnutils.h" |
| 31 // PacketTimeUpdateParams is defined in asyncpacketsocket.h. |
| 32 // TODO(sergeyu): Find more appropriate place for PacketTimeUpdateParams. |
| 33 #include "webrtc/base/asyncpacketsocket.h" |
| 34 #include "webrtc/base/checks.h" |
| 35 #include "webrtc/base/messagedigest.h" |
| 36 |
30 namespace cricket { | 37 namespace cricket { |
31 | 38 |
32 static const uint8_t kRtpVersion = 2; | 39 static const uint8_t kRtpVersion = 2; |
33 static const size_t kRtpFlagsOffset = 0; | 40 static const size_t kRtpFlagsOffset = 0; |
34 static const size_t kRtpPayloadTypeOffset = 1; | 41 static const size_t kRtpPayloadTypeOffset = 1; |
35 static const size_t kRtpSeqNumOffset = 2; | 42 static const size_t kRtpSeqNumOffset = 2; |
36 static const size_t kRtpTimestampOffset = 4; | 43 static const size_t kRtpTimestampOffset = 4; |
37 static const size_t kRtpSsrcOffset = 8; | 44 static const size_t kRtpSsrcOffset = 8; |
38 static const size_t kRtcpPayloadTypeOffset = 1; | 45 static const size_t kRtcpPayloadTypeOffset = 1; |
| 46 static const size_t kRtpExtensionHeaderLen = 4; |
| 47 static const size_t kAbsSendTimeExtensionLen = 3; |
| 48 static const size_t kOneByteExtensionHeaderLen = 1; |
| 49 |
| 50 namespace { |
| 51 |
| 52 // Fake auth tag written by the sender when external authentication is enabled. |
| 53 // HMAC in packet will be compared against this value before updating packet |
| 54 // with actual HMAC value. |
| 55 static const uint8_t kFakeAuthTag[10] = { |
| 56 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd |
| 57 }; |
| 58 |
| 59 void UpdateAbsSendTimeExtensionValue(uint8_t* extension_data, |
| 60 size_t length, |
| 61 uint64_t time_us) { |
| 62 // Absolute send time in RTP streams. |
| 63 // |
| 64 // The absolute send time is signaled to the receiver in-band using the |
| 65 // general mechanism for RTP header extensions [RFC5285]. The payload |
| 66 // of this extension (the transmitted value) is a 24-bit unsigned integer |
| 67 // containing the sender's current time in seconds as a fixed point number |
| 68 // with 18 bits fractional part. |
| 69 // |
| 70 // The form of the absolute send time extension block: |
| 71 // |
| 72 // 0 1 2 3 |
| 73 // 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 |
| 74 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 75 // | ID | len=2 | absolute send time | |
| 76 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 77 if (length != kAbsSendTimeExtensionLen) { |
| 78 RTC_NOTREACHED(); |
| 79 return; |
| 80 } |
| 81 |
| 82 // Convert microseconds to a 6.18 fixed point value in seconds. |
| 83 uint32_t send_time = ((time_us << 18) / 1000000) & 0x00FFFFFF; |
| 84 extension_data[0] = static_cast<uint8_t>(send_time >> 16); |
| 85 extension_data[1] = static_cast<uint8_t>(send_time >> 8); |
| 86 extension_data[2] = static_cast<uint8_t>(send_time); |
| 87 } |
| 88 |
| 89 // Assumes |length| is actual packet length + tag length. Updates HMAC at end of |
| 90 // the RTP packet. |
| 91 void UpdateRtpAuthTag(uint8_t* rtp, |
| 92 size_t length, |
| 93 const rtc::PacketTimeUpdateParams& packet_time_params) { |
| 94 // If there is no key, return. |
| 95 if (packet_time_params.srtp_auth_key.empty()) { |
| 96 return; |
| 97 } |
| 98 |
| 99 size_t tag_length = packet_time_params.srtp_auth_tag_len; |
| 100 |
| 101 // ROC (rollover counter) is at the beginning of the auth tag. |
| 102 const size_t kRocLength = 4; |
| 103 if (tag_length < kRocLength || tag_length > length) { |
| 104 RTC_NOTREACHED(); |
| 105 return; |
| 106 } |
| 107 |
| 108 uint8_t* auth_tag = rtp + (length - tag_length); |
| 109 |
| 110 // We should have a fake HMAC value @ auth_tag. |
| 111 RTC_DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length)); |
| 112 |
| 113 // Copy ROC after end of rtp packet. |
| 114 memcpy(auth_tag, &packet_time_params.srtp_packet_index, kRocLength); |
| 115 // Authentication of a RTP packet will have RTP packet + ROC size. |
| 116 size_t auth_required_length = length - tag_length + kRocLength; |
| 117 |
| 118 uint8_t output[64]; |
| 119 size_t result = rtc::ComputeHmac( |
| 120 rtc::DIGEST_SHA_1, &packet_time_params.srtp_auth_key[0], |
| 121 packet_time_params.srtp_auth_key.size(), rtp, |
| 122 auth_required_length, output, sizeof(output)); |
| 123 |
| 124 if (result < tag_length) { |
| 125 RTC_NOTREACHED(); |
| 126 return; |
| 127 } |
| 128 |
| 129 // Copy HMAC from output to packet. This is required as auth tag length |
| 130 // may not be equal to the actual HMAC length. |
| 131 memcpy(auth_tag, output, tag_length); |
| 132 } |
| 133 |
| 134 } |
39 | 135 |
40 bool GetUint8(const void* data, size_t offset, int* value) { | 136 bool GetUint8(const void* data, size_t offset, int* value) { |
41 if (!data || !value) { | 137 if (!data || !value) { |
42 return false; | 138 return false; |
43 } | 139 } |
44 *value = *(static_cast<const uint8_t*>(data) + offset); | 140 *value = *(static_cast<const uint8_t*>(data) + offset); |
45 return true; | 141 return true; |
46 } | 142 } |
47 | 143 |
48 bool GetUint16(const void* data, size_t offset, int* value) { | 144 bool GetUint16(const void* data, size_t offset, int* value) { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (len < kMinRtpPacketLen) | 289 if (len < kMinRtpPacketLen) |
194 return false; | 290 return false; |
195 | 291 |
196 return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion; | 292 return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion; |
197 } | 293 } |
198 | 294 |
199 bool IsValidRtpPayloadType(int payload_type) { | 295 bool IsValidRtpPayloadType(int payload_type) { |
200 return payload_type >= 0 && payload_type <= 127; | 296 return payload_type >= 0 && payload_type <= 127; |
201 } | 297 } |
202 | 298 |
| 299 bool ValidateRtpHeader(const uint8_t* rtp, |
| 300 size_t length, |
| 301 size_t* header_length) { |
| 302 if (header_length) { |
| 303 *header_length = 0; |
| 304 } |
| 305 |
| 306 if (length < kMinRtpPacketLen) { |
| 307 return false; |
| 308 } |
| 309 |
| 310 size_t cc_count = rtp[0] & 0x0F; |
| 311 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count; |
| 312 if (header_length_without_extension > length) { |
| 313 return false; |
| 314 } |
| 315 |
| 316 // If extension bit is not set, we are done with header processing, as input |
| 317 // length is verified above. |
| 318 if (!(rtp[0] & 0x10)) { |
| 319 if (header_length) |
| 320 *header_length = header_length_without_extension; |
| 321 |
| 322 return true; |
| 323 } |
| 324 |
| 325 rtp += header_length_without_extension; |
| 326 |
| 327 if (header_length_without_extension + kRtpExtensionHeaderLen > length) { |
| 328 return false; |
| 329 } |
| 330 |
| 331 // Getting extension profile length. |
| 332 // Length is in 32 bit words. |
| 333 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); |
| 334 size_t extension_length = extension_length_in_32bits * 4; |
| 335 |
| 336 size_t rtp_header_length = extension_length + |
| 337 header_length_without_extension + |
| 338 kRtpExtensionHeaderLen; |
| 339 |
| 340 // Verify input length against total header size. |
| 341 if (rtp_header_length > length) { |
| 342 return false; |
| 343 } |
| 344 |
| 345 if (header_length) { |
| 346 *header_length = rtp_header_length; |
| 347 } |
| 348 return true; |
| 349 } |
| 350 |
| 351 // ValidateRtpHeader() must be called before this method to make sure, we have |
| 352 // a sane rtp packet. |
| 353 bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp, |
| 354 size_t length, |
| 355 int extension_id, |
| 356 uint64_t time_us) { |
| 357 // 0 1 2 3 |
| 358 // 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 |
| 359 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 360 // |V=2|P|X| CC |M| PT | sequence number | |
| 361 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 362 // | timestamp | |
| 363 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 364 // | synchronization source (SSRC) identifier | |
| 365 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 366 // | contributing source (CSRC) identifiers | |
| 367 // | .... | |
| 368 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 369 |
| 370 // Return if extension bit is not set. |
| 371 if (!(rtp[0] & 0x10)) { |
| 372 return true; |
| 373 } |
| 374 |
| 375 size_t cc_count = rtp[0] & 0x0F; |
| 376 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count; |
| 377 |
| 378 rtp += header_length_without_extension; |
| 379 |
| 380 // Getting extension profile ID and length. |
| 381 uint16_t profile_id = rtc::GetBE16(rtp); |
| 382 // Length is in 32 bit words. |
| 383 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); |
| 384 size_t extension_length = extension_length_in_32bits * 4; |
| 385 |
| 386 rtp += kRtpExtensionHeaderLen; // Moving past extension header. |
| 387 |
| 388 bool found = false; |
| 389 // WebRTC is using one byte header extension. |
| 390 // TODO(mallinath) - Handle two byte header extension. |
| 391 if (profile_id == 0xBEDE) { // OneByte extension header |
| 392 // 0 |
| 393 // 0 1 2 3 4 5 6 7 |
| 394 // +-+-+-+-+-+-+-+-+ |
| 395 // | ID |length | |
| 396 // +-+-+-+-+-+-+-+-+ |
| 397 |
| 398 // 0 1 2 3 |
| 399 // 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 |
| 400 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 401 // | 0xBE | 0xDE | length=3 | |
| 402 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 403 // | ID | L=0 | data | ID | L=1 | data... |
| 404 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 405 // ...data | 0 (pad) | 0 (pad) | ID | L=3 | |
| 406 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 407 // | data | |
| 408 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 409 const uint8_t* extension_start = rtp; |
| 410 const uint8_t* extension_end = extension_start + extension_length; |
| 411 |
| 412 while (rtp < extension_end) { |
| 413 const int id = (*rtp & 0xF0) >> 4; |
| 414 const size_t length = (*rtp & 0x0F) + 1; |
| 415 if (rtp + kOneByteExtensionHeaderLen + length > extension_end) { |
| 416 return false; |
| 417 } |
| 418 // The 4-bit length is the number minus one of data bytes of this header |
| 419 // extension element following the one-byte header. |
| 420 if (id == extension_id) { |
| 421 UpdateAbsSendTimeExtensionValue(rtp + kOneByteExtensionHeaderLen, |
| 422 length, time_us); |
| 423 found = true; |
| 424 break; |
| 425 } |
| 426 rtp += kOneByteExtensionHeaderLen + length; |
| 427 // Counting padding bytes. |
| 428 while ((rtp < extension_end) && (*rtp == 0)) { |
| 429 ++rtp; |
| 430 } |
| 431 } |
| 432 } |
| 433 return found; |
| 434 } |
| 435 |
| 436 bool ApplyPacketOptions(uint8_t* data, |
| 437 size_t length, |
| 438 const rtc::PacketTimeUpdateParams& packet_time_params, |
| 439 uint64_t time_us) { |
| 440 RTC_DCHECK(data); |
| 441 RTC_DCHECK(length); |
| 442 |
| 443 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in |
| 444 // PacketOptions, nothing to be updated in this packet. |
| 445 if (packet_time_params.rtp_sendtime_extension_id == -1 && |
| 446 packet_time_params.srtp_auth_key.empty()) { |
| 447 return true; |
| 448 } |
| 449 |
| 450 // If there is a srtp auth key present then the packet must be an RTP packet. |
| 451 // RTP packet may have been wrapped in a TURN Channel Data or TURN send |
| 452 // indication. |
| 453 size_t rtp_start_pos; |
| 454 size_t rtp_length; |
| 455 if (!UnwrapTurnPacket(data, length, &rtp_start_pos, &rtp_length)) { |
| 456 RTC_NOTREACHED(); |
| 457 return false; |
| 458 } |
| 459 |
| 460 // Making sure we have a valid RTP packet at the end. |
| 461 if (!IsRtpPacket(data + rtp_start_pos, rtp_length) || |
| 462 !ValidateRtpHeader(data + rtp_start_pos, rtp_length, nullptr)) { |
| 463 RTC_NOTREACHED(); |
| 464 return false; |
| 465 } |
| 466 |
| 467 uint8_t* start = data + rtp_start_pos; |
| 468 // If packet option has non default value (-1) for sendtime extension id, |
| 469 // then we should parse the rtp packet to update the timestamp. Otherwise |
| 470 // just calculate HMAC and update packet with it. |
| 471 if (packet_time_params.rtp_sendtime_extension_id != -1) { |
| 472 UpdateRtpAbsSendTimeExtension(start, rtp_length, |
| 473 packet_time_params.rtp_sendtime_extension_id, |
| 474 time_us); |
| 475 } |
| 476 |
| 477 UpdateRtpAuthTag(start, rtp_length, packet_time_params); |
| 478 return true; |
| 479 } |
| 480 |
203 } // namespace cricket | 481 } // namespace cricket |
OLD | NEW |