Chromium Code Reviews| 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 #include "webrtc/base/asyncpacketsocket.h" | |
|
pthatcher1
2016/01/14 19:34:02
Dependency on a socket is weird for an rtputils cl
Sergey Ulanov
2016/01/14 20:13:10
Done.
| |
| 32 #include "webrtc/base/checks.h" | |
| 33 #include "webrtc/base/messagedigest.h" | |
| 34 | |
| 30 namespace cricket { | 35 namespace cricket { |
| 31 | 36 |
| 32 static const uint8_t kRtpVersion = 2; | 37 static const uint8_t kRtpVersion = 2; |
| 33 static const size_t kRtpFlagsOffset = 0; | 38 static const size_t kRtpFlagsOffset = 0; |
| 34 static const size_t kRtpPayloadTypeOffset = 1; | 39 static const size_t kRtpPayloadTypeOffset = 1; |
| 35 static const size_t kRtpSeqNumOffset = 2; | 40 static const size_t kRtpSeqNumOffset = 2; |
| 36 static const size_t kRtpTimestampOffset = 4; | 41 static const size_t kRtpTimestampOffset = 4; |
| 37 static const size_t kRtpSsrcOffset = 8; | 42 static const size_t kRtpSsrcOffset = 8; |
| 38 static const size_t kRtcpPayloadTypeOffset = 1; | 43 static const size_t kRtcpPayloadTypeOffset = 1; |
| 44 static const size_t kRtpExtensionHeaderLen = 4; | |
| 45 static const size_t kAbsSendTimeExtensionLen = 3; | |
| 46 static const size_t kOneByteExtensionHeaderLen = 1; | |
| 47 | |
| 48 namespace { | |
| 49 | |
| 50 // Fake auth tag written by the sender when external authentication is enabled. | |
| 51 // HMAC in packet will be compared against this value before updating packet | |
| 52 // with actual HMAC value. | |
| 53 static const uint8_t kFakeAuthTag[10] = { | |
| 54 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd | |
| 55 }; | |
| 56 | |
| 57 void UpdateAbsSendTimeExtensionValue(uint8_t* extension_data, | |
| 58 size_t length, | |
| 59 uint64_t time_us) { | |
| 60 // Absolute send time in RTP streams. | |
| 61 // | |
| 62 // The absolute send time is signaled to the receiver in-band using the | |
| 63 // general mechanism for RTP header extensions [RFC5285]. The payload | |
| 64 // of this extension (the transmitted value) is a 24-bit unsigned integer | |
| 65 // containing the sender's current time in seconds as a fixed point number | |
| 66 // with 18 bits fractional part. | |
| 67 // | |
| 68 // The form of the absolute send time extension block: | |
| 69 // | |
| 70 // 0 1 2 3 | |
| 71 // 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 | |
| 72 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 73 // | ID | len=2 | absolute send time | | |
| 74 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 75 if (length != kAbsSendTimeExtensionLen) { | |
| 76 RTC_NOTREACHED(); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 // Convert microseconds to a 6.18 fixed point value in seconds. | |
| 81 uint32_t send_time = ((time_us << 18) / 1000000) & 0x00FFFFFF; | |
| 82 extension_data[0] = static_cast<uint8_t>(send_time >> 16); | |
| 83 extension_data[1] = static_cast<uint8_t>(send_time >> 8); | |
| 84 extension_data[2] = static_cast<uint8_t>(send_time); | |
| 85 } | |
| 86 | |
| 87 // Assumes |length| is actual packet length + tag length. Updates HMAC at end of | |
| 88 // the RTP packet. | |
| 89 void UpdateRtpAuthTag(uint8_t* rtp, | |
| 90 size_t length, | |
| 91 const rtc::PacketTimeUpdateParams& packet_time_params) { | |
| 92 // If there is no key, return. | |
| 93 if (packet_time_params.srtp_auth_key.empty()) { | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 size_t tag_length = packet_time_params.srtp_auth_tag_len; | |
| 98 | |
| 99 // ROC (rollover counter) is at the beginning of the auth tag. | |
| 100 const size_t kRocLength = 4; | |
| 101 if (tag_length < kRocLength || tag_length > length) { | |
| 102 RTC_NOTREACHED(); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 uint8_t* auth_tag = rtp + (length - tag_length); | |
| 107 | |
| 108 // We should have a fake HMAC value @ auth_tag. | |
| 109 RTC_DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length)); | |
| 110 | |
| 111 // Copy ROC after end of rtp packet. | |
| 112 memcpy(auth_tag, &packet_time_params.srtp_packet_index, kRocLength); | |
| 113 // Authentication of a RTP packet will have RTP packet + ROC size. | |
| 114 int auth_required_length = length - tag_length + kRocLength; | |
| 115 | |
| 116 uint8_t output[64]; | |
| 117 size_t result = rtc::ComputeHmac( | |
| 118 rtc::DIGEST_SHA_1, &packet_time_params.srtp_auth_key[0], | |
| 119 packet_time_params.srtp_auth_key.size(), rtp, | |
| 120 auth_required_length, output, sizeof(output)); | |
| 121 | |
| 122 if (result < tag_length) { | |
| 123 RTC_NOTREACHED(); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 // Copy HMAC from output to packet. This is required as auth tag length | |
| 128 // may not be equal to the actual HMAC length. | |
| 129 memcpy(auth_tag, output, tag_length); | |
| 130 } | |
| 131 | |
| 132 } | |
| 39 | 133 |
| 40 bool GetUint8(const void* data, size_t offset, int* value) { | 134 bool GetUint8(const void* data, size_t offset, int* value) { |
| 41 if (!data || !value) { | 135 if (!data || !value) { |
| 42 return false; | 136 return false; |
| 43 } | 137 } |
| 44 *value = *(static_cast<const uint8_t*>(data) + offset); | 138 *value = *(static_cast<const uint8_t*>(data) + offset); |
| 45 return true; | 139 return true; |
| 46 } | 140 } |
| 47 | 141 |
| 48 bool GetUint16(const void* data, size_t offset, int* value) { | 142 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) | 287 if (len < kMinRtpPacketLen) |
| 194 return false; | 288 return false; |
| 195 | 289 |
| 196 return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion; | 290 return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion; |
| 197 } | 291 } |
| 198 | 292 |
| 199 bool IsValidRtpPayloadType(int payload_type) { | 293 bool IsValidRtpPayloadType(int payload_type) { |
| 200 return payload_type >= 0 && payload_type <= 127; | 294 return payload_type >= 0 && payload_type <= 127; |
| 201 } | 295 } |
| 202 | 296 |
| 297 bool ValidateRtpHeader(const uint8_t* rtp, | |
| 298 size_t length, | |
| 299 size_t* header_length) { | |
| 300 if (header_length) { | |
| 301 *header_length = 0; | |
| 302 } | |
| 303 | |
| 304 if (length < kMinRtpPacketLen) { | |
| 305 return false; | |
| 306 } | |
| 307 | |
| 308 size_t cc_count = rtp[0] & 0x0F; | |
| 309 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count; | |
| 310 if (header_length_without_extension > length) { | |
| 311 return false; | |
| 312 } | |
| 313 | |
| 314 // If extension bit is not set, we are done with header processing, as input | |
| 315 // length is verified above. | |
| 316 if (!(rtp[0] & 0x10)) { | |
| 317 if (header_length) | |
| 318 *header_length = header_length_without_extension; | |
| 319 | |
| 320 return true; | |
| 321 } | |
| 322 | |
| 323 rtp += header_length_without_extension; | |
| 324 | |
| 325 if (header_length_without_extension + kRtpExtensionHeaderLen > length) { | |
| 326 return false; | |
| 327 } | |
| 328 | |
| 329 // Getting extension profile length. | |
| 330 // Length is in 32 bit words. | |
| 331 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); | |
| 332 size_t extension_length = extension_length_in_32bits * 4; | |
| 333 | |
| 334 size_t rtp_header_length = extension_length + | |
| 335 header_length_without_extension + | |
| 336 kRtpExtensionHeaderLen; | |
| 337 | |
| 338 // Verify input length against total header size. | |
| 339 if (rtp_header_length > length) { | |
| 340 return false; | |
| 341 } | |
| 342 | |
| 343 if (header_length) { | |
| 344 *header_length = rtp_header_length; | |
| 345 } | |
| 346 return true; | |
| 347 } | |
| 348 | |
| 349 // ValidateRtpHeader() must be called before this method to make sure, we have | |
| 350 // a sane rtp packet. | |
| 351 bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp, | |
| 352 size_t length, | |
| 353 int extension_id, | |
| 354 uint64_t time_us) { | |
| 355 // 0 1 2 3 | |
| 356 // 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 | |
| 357 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 358 // |V=2|P|X| CC |M| PT | sequence number | | |
| 359 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 360 // | timestamp | | |
| 361 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 362 // | synchronization source (SSRC) identifier | | |
| 363 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
| 364 // | contributing source (CSRC) identifiers | | |
| 365 // | .... | | |
| 366 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 367 | |
| 368 // Return if extension bit is not set. | |
| 369 if (!(rtp[0] & 0x10)) { | |
| 370 return true; | |
| 371 } | |
| 372 | |
| 373 size_t cc_count = rtp[0] & 0x0F; | |
| 374 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count; | |
| 375 | |
| 376 rtp += header_length_without_extension; | |
| 377 | |
| 378 // Getting extension profile ID and length. | |
| 379 uint16_t profile_id = rtc::GetBE16(rtp); | |
| 380 // Length is in 32 bit words. | |
| 381 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); | |
| 382 size_t extension_length = extension_length_in_32bits * 4; | |
| 383 | |
| 384 rtp += kRtpExtensionHeaderLen; // Moving past extension header. | |
| 385 | |
| 386 bool found = false; | |
| 387 // WebRTC is using one byte header extension. | |
| 388 // TODO(mallinath) - Handle two byte header extension. | |
| 389 if (profile_id == 0xBEDE) { // OneByte extension header | |
| 390 // 0 | |
| 391 // 0 1 2 3 4 5 6 7 | |
| 392 // +-+-+-+-+-+-+-+-+ | |
| 393 // | ID |length | | |
| 394 // +-+-+-+-+-+-+-+-+ | |
| 395 | |
| 396 // 0 1 2 3 | |
| 397 // 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 | |
| 398 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 399 // | 0xBE | 0xDE | length=3 | | |
| 400 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 401 // | ID | L=0 | data | ID | L=1 | data... | |
| 402 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 403 // ...data | 0 (pad) | 0 (pad) | ID | L=3 | | |
| 404 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 405 // | data | | |
| 406 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 407 const uint8_t* extension_start = rtp; | |
| 408 const uint8_t* extension_end = extension_start + extension_length; | |
| 409 | |
| 410 while (rtp < extension_end) { | |
| 411 const int id = (*rtp & 0xF0) >> 4; | |
| 412 const size_t length = (*rtp & 0x0F) + 1; | |
| 413 if (rtp + kOneByteExtensionHeaderLen + length > extension_end) { | |
| 414 return false; | |
| 415 } | |
| 416 // The 4-bit length is the number minus one of data bytes of this header | |
| 417 // extension element following the one-byte header. | |
| 418 if (id == extension_id) { | |
| 419 UpdateAbsSendTimeExtensionValue(rtp + kOneByteExtensionHeaderLen, | |
| 420 length, time_us); | |
| 421 found = true; | |
| 422 break; | |
| 423 } | |
| 424 rtp += kOneByteExtensionHeaderLen + length; | |
| 425 // Counting padding bytes. | |
| 426 while ((rtp < extension_end) && (*rtp == 0)) { | |
| 427 ++rtp; | |
| 428 } | |
| 429 } | |
| 430 } | |
| 431 return found; | |
| 432 } | |
| 433 | |
| 434 bool ApplyPacketOptions(uint8_t* data, | |
| 435 size_t length, | |
| 436 const rtc::PacketTimeUpdateParams& packet_time_params, | |
| 437 uint64_t time_us) { | |
| 438 RTC_DCHECK(data); | |
| 439 RTC_DCHECK(length); | |
| 440 | |
| 441 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in | |
| 442 // PacketOptions, nothing to be updated in this packet. | |
| 443 if (packet_time_params.rtp_sendtime_extension_id == -1 && | |
| 444 packet_time_params.srtp_auth_key.empty()) { | |
| 445 return true; | |
| 446 } | |
| 447 | |
| 448 // If there is a srtp auth key present then the packet must be an RTP packet. | |
| 449 // RTP packet may have been wrapped in a TURN Channel Data or TURN send | |
| 450 // indication. | |
| 451 size_t rtp_start_pos; | |
| 452 size_t rtp_length; | |
| 453 if (!UnwrapTurnPacket(data, length, &rtp_start_pos, &rtp_length)) { | |
| 454 RTC_NOTREACHED(); | |
| 455 return false; | |
| 456 } | |
| 457 | |
| 458 // Making sure we have a valid RTP packet at the end. | |
| 459 if (!IsRtpPacket(data + rtp_start_pos, rtp_length) || | |
| 460 !ValidateRtpHeader(data + rtp_start_pos, rtp_length, nullptr)) { | |
| 461 RTC_NOTREACHED(); | |
| 462 return false; | |
| 463 } | |
| 464 | |
| 465 uint8_t* start = data + rtp_start_pos; | |
| 466 // If packet option has non default value (-1) for sendtime extension id, | |
| 467 // then we should parse the rtp packet to update the timestamp. Otherwise | |
| 468 // just calculate HMAC and update packet with it. | |
| 469 if (packet_time_params.rtp_sendtime_extension_id != -1) { | |
| 470 UpdateRtpAbsSendTimeExtension(start, rtp_length, | |
| 471 packet_time_params.rtp_sendtime_extension_id, | |
| 472 time_us); | |
| 473 } | |
| 474 | |
| 475 UpdateRtpAuthTag(start, rtp_length, packet_time_params); | |
| 476 return true; | |
| 477 } | |
| 478 | |
| 203 } // namespace cricket | 479 } // namespace cricket |
| OLD | NEW |