blob: d0ba1cf72b02d8e14b96413b13e290bcc69a1df0 [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
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000278bool IsValidRtpPayloadType(int payload_type) {
279 return payload_type >= 0 && payload_type <= 127;
280}
281
zstein3dcf0e92017-06-01 13:22:42 -0700282bool IsValidRtpRtcpPacketSize(bool rtcp, size_t size) {
283 return (rtcp ? size >= kMinRtcpPacketLen : size >= kMinRtpPacketLen) &&
284 size <= kMaxRtpPacketLen;
285}
286
287const char* RtpRtcpStringLiteral(bool rtcp) {
288 return rtcp ? "RTCP" : "RTP";
289}
290
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800291bool ValidateRtpHeader(const uint8_t* rtp,
292 size_t length,
293 size_t* header_length) {
294 if (header_length) {
295 *header_length = 0;
296 }
297
298 if (length < kMinRtpPacketLen) {
299 return false;
300 }
301
302 size_t cc_count = rtp[0] & 0x0F;
303 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
304 if (header_length_without_extension > length) {
305 return false;
306 }
307
308 // If extension bit is not set, we are done with header processing, as input
309 // length is verified above.
310 if (!(rtp[0] & 0x10)) {
311 if (header_length)
312 *header_length = header_length_without_extension;
313
314 return true;
315 }
316
317 rtp += header_length_without_extension;
318
319 if (header_length_without_extension + kRtpExtensionHeaderLen > length) {
320 return false;
321 }
322
323 // Getting extension profile length.
324 // Length is in 32 bit words.
325 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
326 size_t extension_length = extension_length_in_32bits * 4;
327
328 size_t rtp_header_length = extension_length +
329 header_length_without_extension +
330 kRtpExtensionHeaderLen;
331
332 // Verify input length against total header size.
333 if (rtp_header_length > length) {
334 return false;
335 }
336
337 if (header_length) {
338 *header_length = rtp_header_length;
339 }
340 return true;
341}
342
343// ValidateRtpHeader() must be called before this method to make sure, we have
344// a sane rtp packet.
345bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp,
346 size_t length,
347 int extension_id,
348 uint64_t time_us) {
349 // 0 1 2 3
350 // 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
351 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
352 // |V=2|P|X| CC |M| PT | sequence number |
353 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
354 // | timestamp |
355 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
356 // | synchronization source (SSRC) identifier |
357 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
358 // | contributing source (CSRC) identifiers |
359 // | .... |
360 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
361
362 // Return if extension bit is not set.
363 if (!(rtp[0] & 0x10)) {
364 return true;
365 }
366
367 size_t cc_count = rtp[0] & 0x0F;
368 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
369
370 rtp += header_length_without_extension;
371
372 // Getting extension profile ID and length.
373 uint16_t profile_id = rtc::GetBE16(rtp);
374 // Length is in 32 bit words.
375 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
376 size_t extension_length = extension_length_in_32bits * 4;
377
378 rtp += kRtpExtensionHeaderLen; // Moving past extension header.
379
380 bool found = false;
381 // WebRTC is using one byte header extension.
382 // TODO(mallinath) - Handle two byte header extension.
383 if (profile_id == 0xBEDE) { // OneByte extension header
384 // 0
385 // 0 1 2 3 4 5 6 7
386 // +-+-+-+-+-+-+-+-+
387 // | ID |length |
388 // +-+-+-+-+-+-+-+-+
389
390 // 0 1 2 3
391 // 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
392 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
393 // | 0xBE | 0xDE | length=3 |
394 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
395 // | ID | L=0 | data | ID | L=1 | data...
396 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
397 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
398 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399 // | data |
400 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
401 const uint8_t* extension_start = rtp;
402 const uint8_t* extension_end = extension_start + extension_length;
403
404 while (rtp < extension_end) {
405 const int id = (*rtp & 0xF0) >> 4;
406 const size_t length = (*rtp & 0x0F) + 1;
407 if (rtp + kOneByteExtensionHeaderLen + length > extension_end) {
408 return false;
409 }
410 // The 4-bit length is the number minus one of data bytes of this header
411 // extension element following the one-byte header.
412 if (id == extension_id) {
413 UpdateAbsSendTimeExtensionValue(rtp + kOneByteExtensionHeaderLen,
414 length, time_us);
415 found = true;
416 break;
417 }
418 rtp += kOneByteExtensionHeaderLen + length;
419 // Counting padding bytes.
420 while ((rtp < extension_end) && (*rtp == 0)) {
421 ++rtp;
422 }
423 }
424 }
425 return found;
426}
427
428bool ApplyPacketOptions(uint8_t* data,
429 size_t length,
430 const rtc::PacketTimeUpdateParams& packet_time_params,
431 uint64_t time_us) {
432 RTC_DCHECK(data);
433 RTC_DCHECK(length);
434
435 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
436 // PacketOptions, nothing to be updated in this packet.
437 if (packet_time_params.rtp_sendtime_extension_id == -1 &&
438 packet_time_params.srtp_auth_key.empty()) {
439 return true;
440 }
441
442 // If there is a srtp auth key present then the packet must be an RTP packet.
443 // RTP packet may have been wrapped in a TURN Channel Data or TURN send
444 // indication.
445 size_t rtp_start_pos;
446 size_t rtp_length;
447 if (!UnwrapTurnPacket(data, length, &rtp_start_pos, &rtp_length)) {
448 RTC_NOTREACHED();
449 return false;
450 }
451
452 // Making sure we have a valid RTP packet at the end.
453 if (!IsRtpPacket(data + rtp_start_pos, rtp_length) ||
454 !ValidateRtpHeader(data + rtp_start_pos, rtp_length, nullptr)) {
455 RTC_NOTREACHED();
456 return false;
457 }
458
459 uint8_t* start = data + rtp_start_pos;
460 // If packet option has non default value (-1) for sendtime extension id,
461 // then we should parse the rtp packet to update the timestamp. Otherwise
462 // just calculate HMAC and update packet with it.
463 if (packet_time_params.rtp_sendtime_extension_id != -1) {
464 UpdateRtpAbsSendTimeExtension(start, rtp_length,
465 packet_time_params.rtp_sendtime_extension_id,
466 time_us);
467 }
468
469 UpdateRtpAuthTag(start, rtp_length, packet_time_params);
470 return true;
471}
472
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473} // namespace cricket