blob: a3c2504d9ff8140c94158d834ee13c38da83f8cb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
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
14 * derived from this software without specific prior written permission.
15 *
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
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
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,
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
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
kjellandera96e2d72016-02-04 23:52:28 -080028#include "webrtc/media/base/rtputils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
Sergey Ulanovdc305db2016-01-14 17:14:54 -080030// PacketTimeUpdateParams is defined in asyncpacketsocket.h.
31// TODO(sergeyu): Find more appropriate place for PacketTimeUpdateParams.
32#include "webrtc/base/asyncpacketsocket.h"
33#include "webrtc/base/checks.h"
34#include "webrtc/base/messagedigest.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/turnutils.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080036
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037namespace cricket {
38
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +000039static const uint8_t kRtpVersion = 2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040static const size_t kRtpFlagsOffset = 0;
41static const size_t kRtpPayloadTypeOffset = 1;
42static const size_t kRtpSeqNumOffset = 2;
43static const size_t kRtpTimestampOffset = 4;
44static const size_t kRtpSsrcOffset = 8;
45static const size_t kRtcpPayloadTypeOffset = 1;
Sergey Ulanovdc305db2016-01-14 17:14:54 -080046static const size_t kRtpExtensionHeaderLen = 4;
47static const size_t kAbsSendTimeExtensionLen = 3;
48static const size_t kOneByteExtensionHeaderLen = 1;
49
50namespace {
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.
55static const uint8_t kFakeAuthTag[10] = {
56 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
57};
58
59void 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.
91void 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}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135
136bool GetUint8(const void* data, size_t offset, int* value) {
137 if (!data || !value) {
138 return false;
139 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200140 *value = *(static_cast<const uint8_t*>(data) + offset);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 return true;
142}
143
144bool GetUint16(const void* data, size_t offset, int* value) {
145 if (!data || !value) {
146 return false;
147 }
148 *value = static_cast<int>(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 rtc::GetBE16(static_cast<const uint8_t*>(data) + offset));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 return true;
151}
152
Peter Boström0c4e06b2015-10-07 12:23:21 +0200153bool GetUint32(const void* data, size_t offset, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 if (!data || !value) {
155 return false;
156 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200157 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + offset);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 return true;
159}
160
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000161bool SetUint8(void* data, size_t offset, uint8_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 if (!data) {
163 return false;
164 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165 rtc::Set8(data, offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 return true;
167}
168
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000169bool SetUint16(void* data, size_t offset, uint16_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 if (!data) {
171 return false;
172 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200173 rtc::SetBE16(static_cast<uint8_t*>(data) + offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 return true;
175}
176
Peter Boström0c4e06b2015-10-07 12:23:21 +0200177bool SetUint32(void* data, size_t offset, uint32_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 if (!data) {
179 return false;
180 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200181 rtc::SetBE32(static_cast<uint8_t*>(data) + offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 return true;
183}
184
185bool GetRtpFlags(const void* data, size_t len, int* value) {
186 if (len < kMinRtpPacketLen) {
187 return false;
188 }
189 return GetUint8(data, kRtpFlagsOffset, value);
190}
191
192bool GetRtpPayloadType(const void* data, size_t len, int* value) {
193 if (len < kMinRtpPacketLen) {
194 return false;
195 }
196 if (!GetUint8(data, kRtpPayloadTypeOffset, value)) {
197 return false;
198 }
199 *value &= 0x7F;
200 return true;
201}
202
203bool GetRtpSeqNum(const void* data, size_t len, int* value) {
204 if (len < kMinRtpPacketLen) {
205 return false;
206 }
207 return GetUint16(data, kRtpSeqNumOffset, value);
208}
209
Peter Boström0c4e06b2015-10-07 12:23:21 +0200210bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 if (len < kMinRtpPacketLen) {
212 return false;
213 }
214 return GetUint32(data, kRtpTimestampOffset, value);
215}
216
Peter Boström0c4e06b2015-10-07 12:23:21 +0200217bool GetRtpSsrc(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 if (len < kMinRtpPacketLen) {
219 return false;
220 }
221 return GetUint32(data, kRtpSsrcOffset, value);
222}
223
224bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) {
225 if (!data || len < kMinRtpPacketLen || !value) return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200226 const uint8_t* header = static_cast<const uint8_t*>(data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 // Get base header size + length of CSRCs (not counting extension yet).
Peter Boström0c4e06b2015-10-07 12:23:21 +0200228 size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32_t);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 if (len < header_size) return false;
230 // If there's an extension, read and add in the extension size.
231 if (header[0] & 0x10) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200232 if (len < header_size + sizeof(uint32_t))
233 return false;
234 header_size +=
235 ((rtc::GetBE16(header + header_size + 2) + 1) * sizeof(uint32_t));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 if (len < header_size) return false;
237 }
238 *value = header_size;
239 return true;
240}
241
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
243 return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
244 GetRtpSeqNum(data, len, &(header->seq_num)) &&
245 GetRtpTimestamp(data, len, &(header->timestamp)) &&
246 GetRtpSsrc(data, len, &(header->ssrc)));
247}
248
249bool GetRtcpType(const void* data, size_t len, int* value) {
250 if (len < kMinRtcpPacketLen) {
251 return false;
252 }
253 return GetUint8(data, kRtcpPayloadTypeOffset, value);
254}
255
256// This method returns SSRC first of RTCP packet, except if packet is SDES.
257// TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
258// to send non-compound packets only to feedback messages.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200259bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
261 if (!data || len < kMinRtcpPacketLen + 4 || !value) return false;
262 int pl_type;
263 if (!GetRtcpType(data, len, &pl_type)) return false;
264 // SDES packet parsing is not supported.
265 if (pl_type == kRtcpTypeSDES) return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200266 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 return true;
268}
269
Peter Boström0c4e06b2015-10-07 12:23:21 +0200270bool SetRtpSsrc(void* data, size_t len, uint32_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 return SetUint32(data, kRtpSsrcOffset, value);
272}
273
274// Assumes version 2, no padding, no extensions, no csrcs.
275bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000276 if (!IsValidRtpPayloadType(header.payload_type) ||
277 header.seq_num < 0 || header.seq_num > UINT16_MAX) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000278 return false;
279 }
280 return (SetUint8(data, kRtpFlagsOffset, kRtpVersion << 6) &&
281 SetUint8(data, kRtpPayloadTypeOffset, header.payload_type & 0x7F) &&
282 SetUint16(data, kRtpSeqNumOffset,
283 static_cast<uint16_t>(header.seq_num)) &&
284 SetUint32(data, kRtpTimestampOffset, header.timestamp) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 SetRtpSsrc(data, len, header.ssrc));
286}
287
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +0000288bool IsRtpPacket(const void* data, size_t len) {
289 if (len < kMinRtpPacketLen)
290 return false;
291
Peter Boström0c4e06b2015-10-07 12:23:21 +0200292 return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion;
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +0000293}
294
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000295bool IsValidRtpPayloadType(int payload_type) {
296 return payload_type >= 0 && payload_type <= 127;
297}
298
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800299bool 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.
353bool 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
436bool 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481} // namespace cricket