You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
17 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "media/base/rtp_utils.h"
  11. #include <string.h>
  12. #include <vector>
  13. // PacketTimeUpdateParams is defined in asyncpacketsocket.h.
  14. // TODO(sergeyu): Find more appropriate place for PacketTimeUpdateParams.
  15. #include "media/base/turn_utils.h"
  16. #include "rtc_base/async_packet_socket.h"
  17. #include "rtc_base/byte_order.h"
  18. #include "rtc_base/checks.h"
  19. #include "rtc_base/message_digest.h"
  20. namespace cricket {
  21. static const uint8_t kRtpVersion = 2;
  22. static const size_t kRtpFlagsOffset = 0;
  23. static const size_t kRtpPayloadTypeOffset = 1;
  24. static const size_t kRtpSeqNumOffset = 2;
  25. static const size_t kRtpTimestampOffset = 4;
  26. static const size_t kRtpSsrcOffset = 8;
  27. static const size_t kRtcpPayloadTypeOffset = 1;
  28. static const size_t kRtpExtensionHeaderLen = 4;
  29. static const size_t kAbsSendTimeExtensionLen = 3;
  30. static const size_t kOneByteExtensionHeaderLen = 1;
  31. namespace {
  32. // Fake auth tag written by the sender when external authentication is enabled.
  33. // HMAC in packet will be compared against this value before updating packet
  34. // with actual HMAC value.
  35. static const uint8_t kFakeAuthTag[10] = {0xba, 0xdd, 0xba, 0xdd, 0xba,
  36. 0xdd, 0xba, 0xdd, 0xba, 0xdd};
  37. void UpdateAbsSendTimeExtensionValue(uint8_t* extension_data,
  38. size_t length,
  39. uint64_t time_us) {
  40. // Absolute send time in RTP streams.
  41. //
  42. // The absolute send time is signaled to the receiver in-band using the
  43. // general mechanism for RTP header extensions [RFC5285]. The payload
  44. // of this extension (the transmitted value) is a 24-bit unsigned integer
  45. // containing the sender's current time in seconds as a fixed point number
  46. // with 18 bits fractional part.
  47. //
  48. // The form of the absolute send time extension block:
  49. //
  50. // 0 1 2 3
  51. // 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
  52. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. // | ID | len=2 | absolute send time |
  54. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  55. if (length != kAbsSendTimeExtensionLen) {
  56. RTC_NOTREACHED();
  57. return;
  58. }
  59. // Convert microseconds to a 6.18 fixed point value in seconds.
  60. uint32_t send_time = ((time_us << 18) / 1000000) & 0x00FFFFFF;
  61. extension_data[0] = static_cast<uint8_t>(send_time >> 16);
  62. extension_data[1] = static_cast<uint8_t>(send_time >> 8);
  63. extension_data[2] = static_cast<uint8_t>(send_time);
  64. }
  65. // Assumes |length| is actual packet length + tag length. Updates HMAC at end of
  66. // the RTP packet.
  67. void UpdateRtpAuthTag(uint8_t* rtp,
  68. size_t length,
  69. const rtc::PacketTimeUpdateParams& packet_time_params) {
  70. // If there is no key, return.
  71. if (packet_time_params.srtp_auth_key.empty()) {
  72. return;
  73. }
  74. size_t tag_length = packet_time_params.srtp_auth_tag_len;
  75. // ROC (rollover counter) is at the beginning of the auth tag.
  76. const size_t kRocLength = 4;
  77. if (tag_length < kRocLength || tag_length > length) {
  78. RTC_NOTREACHED();
  79. return;
  80. }
  81. uint8_t* auth_tag = rtp + (length - tag_length);
  82. // We should have a fake HMAC value @ auth_tag.
  83. RTC_DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
  84. // Copy ROC after end of rtp packet.
  85. memcpy(auth_tag, &packet_time_params.srtp_packet_index, kRocLength);
  86. // Authentication of a RTP packet will have RTP packet + ROC size.
  87. size_t auth_required_length = length - tag_length + kRocLength;
  88. uint8_t output[64];
  89. size_t result =
  90. rtc::ComputeHmac(rtc::DIGEST_SHA_1, &packet_time_params.srtp_auth_key[0],
  91. packet_time_params.srtp_auth_key.size(), rtp,
  92. auth_required_length, output, sizeof(output));
  93. if (result < tag_length) {
  94. RTC_NOTREACHED();
  95. return;
  96. }
  97. // Copy HMAC from output to packet. This is required as auth tag length
  98. // may not be equal to the actual HMAC length.
  99. memcpy(auth_tag, output, tag_length);
  100. }
  101. } // namespace
  102. bool GetUint8(const void* data, size_t offset, int* value) {
  103. if (!data || !value) {
  104. return false;
  105. }
  106. *value = *(static_cast<const uint8_t*>(data) + offset);
  107. return true;
  108. }
  109. bool GetUint16(const void* data, size_t offset, int* value) {
  110. if (!data || !value) {
  111. return false;
  112. }
  113. *value = static_cast<int>(
  114. rtc::GetBE16(static_cast<const uint8_t*>(data) + offset));
  115. return true;
  116. }
  117. bool GetUint32(const void* data, size_t offset, uint32_t* value) {
  118. if (!data || !value) {
  119. return false;
  120. }
  121. *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + offset);
  122. return true;
  123. }
  124. bool SetUint8(void* data, size_t offset, uint8_t value) {
  125. if (!data) {
  126. return false;
  127. }
  128. rtc::Set8(data, offset, value);
  129. return true;
  130. }
  131. bool SetUint16(void* data, size_t offset, uint16_t value) {
  132. if (!data) {
  133. return false;
  134. }
  135. rtc::SetBE16(static_cast<uint8_t*>(data) + offset, value);
  136. return true;
  137. }
  138. bool SetUint32(void* data, size_t offset, uint32_t value) {
  139. if (!data) {
  140. return false;
  141. }
  142. rtc::SetBE32(static_cast<uint8_t*>(data) + offset, value);
  143. return true;
  144. }
  145. bool GetRtpFlags(const void* data, size_t len, int* value) {
  146. if (len < kMinRtpPacketLen) {
  147. return false;
  148. }
  149. return GetUint8(data, kRtpFlagsOffset, value);
  150. }
  151. bool GetRtpPayloadType(const void* data, size_t len, int* value) {
  152. if (len < kMinRtpPacketLen) {
  153. return false;
  154. }
  155. if (!GetUint8(data, kRtpPayloadTypeOffset, value)) {
  156. return false;
  157. }
  158. *value &= 0x7F;
  159. return true;
  160. }
  161. bool GetRtpSeqNum(const void* data, size_t len, int* value) {
  162. if (len < kMinRtpPacketLen) {
  163. return false;
  164. }
  165. return GetUint16(data, kRtpSeqNumOffset, value);
  166. }
  167. bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value) {
  168. if (len < kMinRtpPacketLen) {
  169. return false;
  170. }
  171. return GetUint32(data, kRtpTimestampOffset, value);
  172. }
  173. bool GetRtpSsrc(const void* data, size_t len, uint32_t* value) {
  174. if (len < kMinRtpPacketLen) {
  175. return false;
  176. }
  177. return GetUint32(data, kRtpSsrcOffset, value);
  178. }
  179. bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) {
  180. if (!data || len < kMinRtpPacketLen || !value)
  181. return false;
  182. const uint8_t* header = static_cast<const uint8_t*>(data);
  183. // Get base header size + length of CSRCs (not counting extension yet).
  184. size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32_t);
  185. if (len < header_size)
  186. return false;
  187. // If there's an extension, read and add in the extension size.
  188. if (header[0] & 0x10) {
  189. if (len < header_size + sizeof(uint32_t))
  190. return false;
  191. header_size +=
  192. ((rtc::GetBE16(header + header_size + 2) + 1) * sizeof(uint32_t));
  193. if (len < header_size)
  194. return false;
  195. }
  196. *value = header_size;
  197. return true;
  198. }
  199. bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
  200. return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
  201. GetRtpSeqNum(data, len, &(header->seq_num)) &&
  202. GetRtpTimestamp(data, len, &(header->timestamp)) &&
  203. GetRtpSsrc(data, len, &(header->ssrc)));
  204. }
  205. bool GetRtcpType(const void* data, size_t len, int* value) {
  206. if (len < kMinRtcpPacketLen) {
  207. return false;
  208. }
  209. return GetUint8(data, kRtcpPayloadTypeOffset, value);
  210. }
  211. // This method returns SSRC first of RTCP packet, except if packet is SDES.
  212. // TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
  213. // to send non-compound packets only to feedback messages.
  214. bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value) {
  215. // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
  216. if (!data || len < kMinRtcpPacketLen + 4 || !value)
  217. return false;
  218. int pl_type;
  219. if (!GetRtcpType(data, len, &pl_type))
  220. return false;
  221. // SDES packet parsing is not supported.
  222. if (pl_type == kRtcpTypeSDES)
  223. return false;
  224. *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + 4);
  225. return true;
  226. }
  227. bool SetRtpSsrc(void* data, size_t len, uint32_t value) {
  228. return SetUint32(data, kRtpSsrcOffset, value);
  229. }
  230. // Assumes version 2, no padding, no extensions, no csrcs.
  231. bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
  232. if (!IsValidRtpPayloadType(header.payload_type) || header.seq_num < 0 ||
  233. header.seq_num > static_cast<int>(UINT16_MAX)) {
  234. return false;
  235. }
  236. return (SetUint8(data, kRtpFlagsOffset, kRtpVersion << 6) &&
  237. SetUint8(data, kRtpPayloadTypeOffset, header.payload_type & 0x7F) &&
  238. SetUint16(data, kRtpSeqNumOffset,
  239. static_cast<uint16_t>(header.seq_num)) &&
  240. SetUint32(data, kRtpTimestampOffset, header.timestamp) &&
  241. SetRtpSsrc(data, len, header.ssrc));
  242. }
  243. static bool HasCorrectRtpVersion(rtc::ArrayView<const uint8_t> packet) {
  244. return packet.data()[0] >> 6 == kRtpVersion;
  245. }
  246. bool IsRtpPacket(rtc::ArrayView<const char> packet) {
  247. return packet.size() >= kMinRtpPacketLen &&
  248. HasCorrectRtpVersion(
  249. rtc::reinterpret_array_view<const uint8_t>(packet));
  250. }
  251. // Check the RTP payload type. If 63 < payload type < 96, it's RTCP.
  252. // For additional details, see http://tools.ietf.org/html/rfc5761.
  253. bool IsRtcpPacket(rtc::ArrayView<const char> packet) {
  254. if (packet.size() < kMinRtcpPacketLen ||
  255. !HasCorrectRtpVersion(
  256. rtc::reinterpret_array_view<const uint8_t>(packet))) {
  257. return false;
  258. }
  259. char pt = packet[1] & 0x7F;
  260. return (63 < pt) && (pt < 96);
  261. }
  262. bool IsValidRtpPayloadType(int payload_type) {
  263. return payload_type >= 0 && payload_type <= 127;
  264. }
  265. bool IsValidRtpPacketSize(RtpPacketType packet_type, size_t size) {
  266. RTC_DCHECK_NE(RtpPacketType::kUnknown, packet_type);
  267. size_t min_packet_length = packet_type == RtpPacketType::kRtcp
  268. ? kMinRtcpPacketLen
  269. : kMinRtpPacketLen;
  270. return size >= min_packet_length && size <= kMaxRtpPacketLen;
  271. }
  272. absl::string_view RtpPacketTypeToString(RtpPacketType packet_type) {
  273. switch (packet_type) {
  274. case RtpPacketType::kRtp:
  275. return "RTP";
  276. case RtpPacketType::kRtcp:
  277. return "RTCP";
  278. case RtpPacketType::kUnknown:
  279. return "Unknown";
  280. }
  281. }
  282. RtpPacketType InferRtpPacketType(rtc::ArrayView<const char> packet) {
  283. // RTCP packets are RTP packets so must check that first.
  284. if (IsRtcpPacket(packet)) {
  285. return RtpPacketType::kRtcp;
  286. }
  287. if (IsRtpPacket(packet)) {
  288. return RtpPacketType::kRtp;
  289. }
  290. return RtpPacketType::kUnknown;
  291. }
  292. bool ValidateRtpHeader(const uint8_t* rtp,
  293. size_t length,
  294. size_t* header_length) {
  295. if (header_length) {
  296. *header_length = 0;
  297. }
  298. if (length < kMinRtpPacketLen) {
  299. return false;
  300. }
  301. size_t cc_count = rtp[0] & 0x0F;
  302. size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
  303. if (header_length_without_extension > length) {
  304. return false;
  305. }
  306. // If extension bit is not set, we are done with header processing, as input
  307. // length is verified above.
  308. if (!(rtp[0] & 0x10)) {
  309. if (header_length)
  310. *header_length = header_length_without_extension;
  311. return true;
  312. }
  313. rtp += header_length_without_extension;
  314. if (header_length_without_extension + kRtpExtensionHeaderLen > length) {
  315. return false;
  316. }
  317. // Getting extension profile length.
  318. // Length is in 32 bit words.
  319. uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
  320. size_t extension_length = extension_length_in_32bits * 4;
  321. size_t rtp_header_length = extension_length +
  322. header_length_without_extension +
  323. kRtpExtensionHeaderLen;
  324. // Verify input length against total header size.
  325. if (rtp_header_length > length) {
  326. return false;
  327. }
  328. if (header_length) {
  329. *header_length = rtp_header_length;
  330. }
  331. return true;
  332. }
  333. // ValidateRtpHeader() must be called before this method to make sure, we have
  334. // a sane rtp packet.
  335. bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp,
  336. size_t length,
  337. int extension_id,
  338. uint64_t time_us) {
  339. // 0 1 2 3
  340. // 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
  341. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  342. // |V=2|P|X| CC |M| PT | sequence number |
  343. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  344. // | timestamp |
  345. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  346. // | synchronization source (SSRC) identifier |
  347. // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  348. // | contributing source (CSRC) identifiers |
  349. // | .... |
  350. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  351. // Return if extension bit is not set.
  352. if (!(rtp[0] & 0x10)) {
  353. return true;
  354. }
  355. size_t cc_count = rtp[0] & 0x0F;
  356. size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
  357. rtp += header_length_without_extension;
  358. // Getting extension profile ID and length.
  359. uint16_t profile_id = rtc::GetBE16(rtp);
  360. // Length is in 32 bit words.
  361. uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
  362. size_t extension_length = extension_length_in_32bits * 4;
  363. rtp += kRtpExtensionHeaderLen; // Moving past extension header.
  364. bool found = false;
  365. // WebRTC is using one byte header extension.
  366. // TODO(mallinath) - Handle two byte header extension.
  367. if (profile_id == 0xBEDE) { // OneByte extension header
  368. // 0
  369. // 0 1 2 3 4 5 6 7
  370. // +-+-+-+-+-+-+-+-+
  371. // | ID |length |
  372. // +-+-+-+-+-+-+-+-+
  373. // 0 1 2 3
  374. // 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
  375. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  376. // | 0xBE | 0xDE | length=3 |
  377. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  378. // | ID | L=0 | data | ID | L=1 | data...
  379. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  380. // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
  381. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  382. // | data |
  383. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  384. const uint8_t* extension_start = rtp;
  385. const uint8_t* extension_end = extension_start + extension_length;
  386. while (rtp < extension_end) {
  387. const int id = (*rtp & 0xF0) >> 4;
  388. const size_t length = (*rtp & 0x0F) + 1;
  389. if (rtp + kOneByteExtensionHeaderLen + length > extension_end) {
  390. return false;
  391. }
  392. // The 4-bit length is the number minus one of data bytes of this header
  393. // extension element following the one-byte header.
  394. if (id == extension_id) {
  395. UpdateAbsSendTimeExtensionValue(rtp + kOneByteExtensionHeaderLen,
  396. length, time_us);
  397. found = true;
  398. break;
  399. }
  400. rtp += kOneByteExtensionHeaderLen + length;
  401. // Counting padding bytes.
  402. while ((rtp < extension_end) && (*rtp == 0)) {
  403. ++rtp;
  404. }
  405. }
  406. }
  407. return found;
  408. }
  409. bool ApplyPacketOptions(uint8_t* data,
  410. size_t length,
  411. const rtc::PacketTimeUpdateParams& packet_time_params,
  412. uint64_t time_us) {
  413. RTC_DCHECK(data);
  414. RTC_DCHECK(length);
  415. // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
  416. // PacketOptions, nothing to be updated in this packet.
  417. if (packet_time_params.rtp_sendtime_extension_id == -1 &&
  418. packet_time_params.srtp_auth_key.empty()) {
  419. return true;
  420. }
  421. // If there is a srtp auth key present then the packet must be an RTP packet.
  422. // RTP packet may have been wrapped in a TURN Channel Data or TURN send
  423. // indication.
  424. size_t rtp_start_pos;
  425. size_t rtp_length;
  426. if (!UnwrapTurnPacket(data, length, &rtp_start_pos, &rtp_length)) {
  427. RTC_NOTREACHED();
  428. return false;
  429. }
  430. // Making sure we have a valid RTP packet at the end.
  431. auto packet = rtc::MakeArrayView(data + rtp_start_pos, rtp_length);
  432. if (!IsRtpPacket(rtc::reinterpret_array_view<const char>(packet)) ||
  433. !ValidateRtpHeader(data + rtp_start_pos, rtp_length, nullptr)) {
  434. RTC_NOTREACHED();
  435. return false;
  436. }
  437. uint8_t* start = data + rtp_start_pos;
  438. // If packet option has non default value (-1) for sendtime extension id,
  439. // then we should parse the rtp packet to update the timestamp. Otherwise
  440. // just calculate HMAC and update packet with it.
  441. if (packet_time_params.rtp_sendtime_extension_id != -1) {
  442. UpdateRtpAbsSendTimeExtension(start, rtp_length,
  443. packet_time_params.rtp_sendtime_extension_id,
  444. time_us);
  445. }
  446. UpdateRtpAuthTag(start, rtp_length, packet_time_params);
  447. return true;
  448. }
  449. } // namespace cricket