blob: e8fd10d8bb61f325b90a29acef2421b2b9ddd730 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "media/base/rtputils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Sergey Ulanovdc305db2016-01-14 17:14:54 -080013// PacketTimeUpdateParams is defined in asyncpacketsocket.h.
14// TODO(sergeyu): Find more appropriate place for PacketTimeUpdateParams.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "media/base/turnutils.h"
16#include "rtc_base/asyncpacketsocket.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/messagedigest.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080019
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020namespace cricket {
21
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +000022static const uint8_t kRtpVersion = 2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023static const size_t kRtpFlagsOffset = 0;
24static const size_t kRtpPayloadTypeOffset = 1;
25static const size_t kRtpSeqNumOffset = 2;
26static const size_t kRtpTimestampOffset = 4;
27static const size_t kRtpSsrcOffset = 8;
28static const size_t kRtcpPayloadTypeOffset = 1;
Sergey Ulanovdc305db2016-01-14 17:14:54 -080029static const size_t kRtpExtensionHeaderLen = 4;
30static const size_t kAbsSendTimeExtensionLen = 3;
31static const size_t kOneByteExtensionHeaderLen = 1;
32
33namespace {
34
35// Fake auth tag written by the sender when external authentication is enabled.
36// HMAC in packet will be compared against this value before updating packet
37// with actual HMAC value.
38static const uint8_t kFakeAuthTag[10] = {
39 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
40};
41
42void UpdateAbsSendTimeExtensionValue(uint8_t* extension_data,
43 size_t length,
44 uint64_t time_us) {
45 // Absolute send time in RTP streams.
46 //
47 // The absolute send time is signaled to the receiver in-band using the
48 // general mechanism for RTP header extensions [RFC5285]. The payload
49 // of this extension (the transmitted value) is a 24-bit unsigned integer
50 // containing the sender's current time in seconds as a fixed point number
51 // with 18 bits fractional part.
52 //
53 // The form of the absolute send time extension block:
54 //
55 // 0 1 2 3
56 // 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
57 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 // | ID | len=2 | absolute send time |
59 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 if (length != kAbsSendTimeExtensionLen) {
61 RTC_NOTREACHED();
62 return;
63 }
64
65 // Convert microseconds to a 6.18 fixed point value in seconds.
66 uint32_t send_time = ((time_us << 18) / 1000000) & 0x00FFFFFF;
67 extension_data[0] = static_cast<uint8_t>(send_time >> 16);
68 extension_data[1] = static_cast<uint8_t>(send_time >> 8);
69 extension_data[2] = static_cast<uint8_t>(send_time);
70}
71
72// Assumes |length| is actual packet length + tag length. Updates HMAC at end of
73// the RTP packet.
74void UpdateRtpAuthTag(uint8_t* rtp,
75 size_t length,
76 const rtc::PacketTimeUpdateParams& packet_time_params) {
77 // If there is no key, return.
78 if (packet_time_params.srtp_auth_key.empty()) {
79 return;
80 }
81
82 size_t tag_length = packet_time_params.srtp_auth_tag_len;
83
84 // ROC (rollover counter) is at the beginning of the auth tag.
85 const size_t kRocLength = 4;
86 if (tag_length < kRocLength || tag_length > length) {
87 RTC_NOTREACHED();
88 return;
89 }
90
91 uint8_t* auth_tag = rtp + (length - tag_length);
92
93 // We should have a fake HMAC value @ auth_tag.
94 RTC_DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
95
96 // Copy ROC after end of rtp packet.
97 memcpy(auth_tag, &packet_time_params.srtp_packet_index, kRocLength);
98 // Authentication of a RTP packet will have RTP packet + ROC size.
99 size_t auth_required_length = length - tag_length + kRocLength;
100
101 uint8_t output[64];
102 size_t result = rtc::ComputeHmac(
103 rtc::DIGEST_SHA_1, &packet_time_params.srtp_auth_key[0],
104 packet_time_params.srtp_auth_key.size(), rtp,
105 auth_required_length, output, sizeof(output));
106
107 if (result < tag_length) {
108 RTC_NOTREACHED();
109 return;
110 }
111
112 // Copy HMAC from output to packet. This is required as auth tag length
113 // may not be equal to the actual HMAC length.
114 memcpy(auth_tag, output, tag_length);
115}
116
Steve Antone78bcb92017-10-31 09:53:08 -0700117} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119bool GetUint8(const void* data, size_t offset, int* value) {
120 if (!data || !value) {
121 return false;
122 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200123 *value = *(static_cast<const uint8_t*>(data) + offset);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 return true;
125}
126
127bool GetUint16(const void* data, size_t offset, int* value) {
128 if (!data || !value) {
129 return false;
130 }
131 *value = static_cast<int>(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200132 rtc::GetBE16(static_cast<const uint8_t*>(data) + offset));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 return true;
134}
135
Peter Boström0c4e06b2015-10-07 12:23:21 +0200136bool GetUint32(const void* data, size_t offset, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 if (!data || !value) {
138 return false;
139 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200140 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + offset);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 return true;
142}
143
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000144bool SetUint8(void* data, size_t offset, uint8_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 if (!data) {
146 return false;
147 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000148 rtc::Set8(data, offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 return true;
150}
151
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000152bool SetUint16(void* data, size_t offset, uint16_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 if (!data) {
154 return false;
155 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200156 rtc::SetBE16(static_cast<uint8_t*>(data) + offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 return true;
158}
159
Peter Boström0c4e06b2015-10-07 12:23:21 +0200160bool SetUint32(void* data, size_t offset, uint32_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 if (!data) {
162 return false;
163 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200164 rtc::SetBE32(static_cast<uint8_t*>(data) + offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 return true;
166}
167
168bool GetRtpFlags(const void* data, size_t len, int* value) {
169 if (len < kMinRtpPacketLen) {
170 return false;
171 }
172 return GetUint8(data, kRtpFlagsOffset, value);
173}
174
175bool GetRtpPayloadType(const void* data, size_t len, int* value) {
176 if (len < kMinRtpPacketLen) {
177 return false;
178 }
179 if (!GetUint8(data, kRtpPayloadTypeOffset, value)) {
180 return false;
181 }
182 *value &= 0x7F;
183 return true;
184}
185
186bool GetRtpSeqNum(const void* data, size_t len, int* value) {
187 if (len < kMinRtpPacketLen) {
188 return false;
189 }
190 return GetUint16(data, kRtpSeqNumOffset, value);
191}
192
Peter Boström0c4e06b2015-10-07 12:23:21 +0200193bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 if (len < kMinRtpPacketLen) {
195 return false;
196 }
197 return GetUint32(data, kRtpTimestampOffset, value);
198}
199
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200bool GetRtpSsrc(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 if (len < kMinRtpPacketLen) {
202 return false;
203 }
204 return GetUint32(data, kRtpSsrcOffset, value);
205}
206
207bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) {
208 if (!data || len < kMinRtpPacketLen || !value) return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200209 const uint8_t* header = static_cast<const uint8_t*>(data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 // Get base header size + length of CSRCs (not counting extension yet).
Peter Boström0c4e06b2015-10-07 12:23:21 +0200211 size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32_t);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 if (len < header_size) return false;
213 // If there's an extension, read and add in the extension size.
214 if (header[0] & 0x10) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200215 if (len < header_size + sizeof(uint32_t))
216 return false;
217 header_size +=
218 ((rtc::GetBE16(header + header_size + 2) + 1) * sizeof(uint32_t));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 if (len < header_size) return false;
220 }
221 *value = header_size;
222 return true;
223}
224
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
226 return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
227 GetRtpSeqNum(data, len, &(header->seq_num)) &&
228 GetRtpTimestamp(data, len, &(header->timestamp)) &&
229 GetRtpSsrc(data, len, &(header->ssrc)));
230}
231
232bool GetRtcpType(const void* data, size_t len, int* value) {
233 if (len < kMinRtcpPacketLen) {
234 return false;
235 }
236 return GetUint8(data, kRtcpPayloadTypeOffset, value);
237}
238
239// This method returns SSRC first of RTCP packet, except if packet is SDES.
240// TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
241// to send non-compound packets only to feedback messages.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200242bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
244 if (!data || len < kMinRtcpPacketLen + 4 || !value) return false;
245 int pl_type;
246 if (!GetRtcpType(data, len, &pl_type)) return false;
247 // SDES packet parsing is not supported.
248 if (pl_type == kRtcpTypeSDES) return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200249 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 return true;
251}
252
Peter Boström0c4e06b2015-10-07 12:23:21 +0200253bool SetRtpSsrc(void* data, size_t len, uint32_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 return SetUint32(data, kRtpSsrcOffset, value);
255}
256
257// Assumes version 2, no padding, no extensions, no csrcs.
258bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000259 if (!IsValidRtpPayloadType(header.payload_type) ||
260 header.seq_num < 0 || header.seq_num > UINT16_MAX) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000261 return false;
262 }
263 return (SetUint8(data, kRtpFlagsOffset, kRtpVersion << 6) &&
264 SetUint8(data, kRtpPayloadTypeOffset, header.payload_type & 0x7F) &&
265 SetUint16(data, kRtpSeqNumOffset,
266 static_cast<uint16_t>(header.seq_num)) &&
267 SetUint32(data, kRtpTimestampOffset, header.timestamp) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 SetRtpSsrc(data, len, header.ssrc));
269}
270
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +0000271bool IsRtpPacket(const void* data, size_t len) {
272 if (len < kMinRtpPacketLen)
273 return false;
274
Peter Boström0c4e06b2015-10-07 12:23:21 +0200275 return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion;
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +0000276}
277
Zhi Huang365381f2018-04-13 16:44:34 -0700278// Check the RTP payload type. If 63 < payload type < 96, it's RTCP.
279// For additional details, see http://tools.ietf.org/html/rfc5761.
280bool IsRtcpPacket(const char* data, size_t len) {
281 if (len < 2) {
282 return false;
283 }
284 char pt = data[1] & 0x7F;
285 return (63 < pt) && (pt < 96);
286}
287
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000288bool IsValidRtpPayloadType(int payload_type) {
289 return payload_type >= 0 && payload_type <= 127;
290}
291
zstein3dcf0e92017-06-01 13:22:42 -0700292bool IsValidRtpRtcpPacketSize(bool rtcp, size_t size) {
293 return (rtcp ? size >= kMinRtcpPacketLen : size >= kMinRtpPacketLen) &&
294 size <= kMaxRtpPacketLen;
295}
296
297const char* RtpRtcpStringLiteral(bool rtcp) {
298 return rtcp ? "RTCP" : "RTP";
299}
300
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800301bool ValidateRtpHeader(const uint8_t* rtp,
302 size_t length,
303 size_t* header_length) {
304 if (header_length) {
305 *header_length = 0;
306 }
307
308 if (length < kMinRtpPacketLen) {
309 return false;
310 }
311
312 size_t cc_count = rtp[0] & 0x0F;
313 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
314 if (header_length_without_extension > length) {
315 return false;
316 }
317
318 // If extension bit is not set, we are done with header processing, as input
319 // length is verified above.
320 if (!(rtp[0] & 0x10)) {
321 if (header_length)
322 *header_length = header_length_without_extension;
323
324 return true;
325 }
326
327 rtp += header_length_without_extension;
328
329 if (header_length_without_extension + kRtpExtensionHeaderLen > length) {
330 return false;
331 }
332
333 // Getting extension profile length.
334 // Length is in 32 bit words.
335 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
336 size_t extension_length = extension_length_in_32bits * 4;
337
338 size_t rtp_header_length = extension_length +
339 header_length_without_extension +
340 kRtpExtensionHeaderLen;
341
342 // Verify input length against total header size.
343 if (rtp_header_length > length) {
344 return false;
345 }
346
347 if (header_length) {
348 *header_length = rtp_header_length;
349 }
350 return true;
351}
352
353// ValidateRtpHeader() must be called before this method to make sure, we have
354// a sane rtp packet.
355bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp,
356 size_t length,
357 int extension_id,
358 uint64_t time_us) {
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 // |V=2|P|X| CC |M| PT | sequence number |
363 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
364 // | timestamp |
365 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366 // | synchronization source (SSRC) identifier |
367 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
368 // | contributing source (CSRC) identifiers |
369 // | .... |
370 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371
372 // Return if extension bit is not set.
373 if (!(rtp[0] & 0x10)) {
374 return true;
375 }
376
377 size_t cc_count = rtp[0] & 0x0F;
378 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
379
380 rtp += header_length_without_extension;
381
382 // Getting extension profile ID and length.
383 uint16_t profile_id = rtc::GetBE16(rtp);
384 // Length is in 32 bit words.
385 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
386 size_t extension_length = extension_length_in_32bits * 4;
387
388 rtp += kRtpExtensionHeaderLen; // Moving past extension header.
389
390 bool found = false;
391 // WebRTC is using one byte header extension.
392 // TODO(mallinath) - Handle two byte header extension.
393 if (profile_id == 0xBEDE) { // OneByte extension header
394 // 0
395 // 0 1 2 3 4 5 6 7
396 // +-+-+-+-+-+-+-+-+
397 // | ID |length |
398 // +-+-+-+-+-+-+-+-+
399
400 // 0 1 2 3
401 // 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
402 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
403 // | 0xBE | 0xDE | length=3 |
404 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
405 // | ID | L=0 | data | ID | L=1 | data...
406 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
407 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
408 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
409 // | data |
410 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
411 const uint8_t* extension_start = rtp;
412 const uint8_t* extension_end = extension_start + extension_length;
413
414 while (rtp < extension_end) {
415 const int id = (*rtp & 0xF0) >> 4;
416 const size_t length = (*rtp & 0x0F) + 1;
417 if (rtp + kOneByteExtensionHeaderLen + length > extension_end) {
418 return false;
419 }
420 // The 4-bit length is the number minus one of data bytes of this header
421 // extension element following the one-byte header.
422 if (id == extension_id) {
423 UpdateAbsSendTimeExtensionValue(rtp + kOneByteExtensionHeaderLen,
424 length, time_us);
425 found = true;
426 break;
427 }
428 rtp += kOneByteExtensionHeaderLen + length;
429 // Counting padding bytes.
430 while ((rtp < extension_end) && (*rtp == 0)) {
431 ++rtp;
432 }
433 }
434 }
435 return found;
436}
437
438bool ApplyPacketOptions(uint8_t* data,
439 size_t length,
440 const rtc::PacketTimeUpdateParams& packet_time_params,
441 uint64_t time_us) {
442 RTC_DCHECK(data);
443 RTC_DCHECK(length);
444
445 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
446 // PacketOptions, nothing to be updated in this packet.
447 if (packet_time_params.rtp_sendtime_extension_id == -1 &&
448 packet_time_params.srtp_auth_key.empty()) {
449 return true;
450 }
451
452 // If there is a srtp auth key present then the packet must be an RTP packet.
453 // RTP packet may have been wrapped in a TURN Channel Data or TURN send
454 // indication.
455 size_t rtp_start_pos;
456 size_t rtp_length;
457 if (!UnwrapTurnPacket(data, length, &rtp_start_pos, &rtp_length)) {
458 RTC_NOTREACHED();
459 return false;
460 }
461
462 // Making sure we have a valid RTP packet at the end.
463 if (!IsRtpPacket(data + rtp_start_pos, rtp_length) ||
464 !ValidateRtpHeader(data + rtp_start_pos, rtp_length, nullptr)) {
465 RTC_NOTREACHED();
466 return false;
467 }
468
469 uint8_t* start = data + rtp_start_pos;
470 // If packet option has non default value (-1) for sendtime extension id,
471 // then we should parse the rtp packet to update the timestamp. Otherwise
472 // just calculate HMAC and update packet with it.
473 if (packet_time_params.rtp_sendtime_extension_id != -1) {
474 UpdateRtpAbsSendTimeExtension(start, rtp_length,
475 packet_time_params.rtp_sendtime_extension_id,
476 time_us);
477 }
478
479 UpdateRtpAuthTag(start, rtp_length, packet_time_params);
480 return true;
481}
482
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483} // namespace cricket