OLD | NEW |
| (Empty) |
1 /* | |
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 | |
28 #include "talk/media/base/rtputils.h" | |
29 | |
30 #include "talk/media/base/turnutils.h" | |
31 // PacketTimeUpdateParams is defined in asyncpacketsocket.h. | |
32 // TODO(sergeyu): Find more appropriate place for PacketTimeUpdateParams. | |
33 #include "webrtc/base/asyncpacketsocket.h" | |
34 #include "webrtc/base/checks.h" | |
35 #include "webrtc/base/messagedigest.h" | |
36 | |
37 namespace cricket { | |
38 | |
39 static const uint8_t kRtpVersion = 2; | |
40 static const size_t kRtpFlagsOffset = 0; | |
41 static const size_t kRtpPayloadTypeOffset = 1; | |
42 static const size_t kRtpSeqNumOffset = 2; | |
43 static const size_t kRtpTimestampOffset = 4; | |
44 static const size_t kRtpSsrcOffset = 8; | |
45 static const size_t kRtcpPayloadTypeOffset = 1; | |
46 static const size_t kRtpExtensionHeaderLen = 4; | |
47 static const size_t kAbsSendTimeExtensionLen = 3; | |
48 static const size_t kOneByteExtensionHeaderLen = 1; | |
49 | |
50 namespace { | |
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. | |
55 static const uint8_t kFakeAuthTag[10] = { | |
56 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd | |
57 }; | |
58 | |
59 void 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. | |
91 void 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 } | |
135 | |
136 bool GetUint8(const void* data, size_t offset, int* value) { | |
137 if (!data || !value) { | |
138 return false; | |
139 } | |
140 *value = *(static_cast<const uint8_t*>(data) + offset); | |
141 return true; | |
142 } | |
143 | |
144 bool GetUint16(const void* data, size_t offset, int* value) { | |
145 if (!data || !value) { | |
146 return false; | |
147 } | |
148 *value = static_cast<int>( | |
149 rtc::GetBE16(static_cast<const uint8_t*>(data) + offset)); | |
150 return true; | |
151 } | |
152 | |
153 bool GetUint32(const void* data, size_t offset, uint32_t* value) { | |
154 if (!data || !value) { | |
155 return false; | |
156 } | |
157 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + offset); | |
158 return true; | |
159 } | |
160 | |
161 bool SetUint8(void* data, size_t offset, uint8_t value) { | |
162 if (!data) { | |
163 return false; | |
164 } | |
165 rtc::Set8(data, offset, value); | |
166 return true; | |
167 } | |
168 | |
169 bool SetUint16(void* data, size_t offset, uint16_t value) { | |
170 if (!data) { | |
171 return false; | |
172 } | |
173 rtc::SetBE16(static_cast<uint8_t*>(data) + offset, value); | |
174 return true; | |
175 } | |
176 | |
177 bool SetUint32(void* data, size_t offset, uint32_t value) { | |
178 if (!data) { | |
179 return false; | |
180 } | |
181 rtc::SetBE32(static_cast<uint8_t*>(data) + offset, value); | |
182 return true; | |
183 } | |
184 | |
185 bool 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 | |
192 bool 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 | |
203 bool 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 | |
210 bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value) { | |
211 if (len < kMinRtpPacketLen) { | |
212 return false; | |
213 } | |
214 return GetUint32(data, kRtpTimestampOffset, value); | |
215 } | |
216 | |
217 bool GetRtpSsrc(const void* data, size_t len, uint32_t* value) { | |
218 if (len < kMinRtpPacketLen) { | |
219 return false; | |
220 } | |
221 return GetUint32(data, kRtpSsrcOffset, value); | |
222 } | |
223 | |
224 bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) { | |
225 if (!data || len < kMinRtpPacketLen || !value) return false; | |
226 const uint8_t* header = static_cast<const uint8_t*>(data); | |
227 // Get base header size + length of CSRCs (not counting extension yet). | |
228 size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32_t); | |
229 if (len < header_size) return false; | |
230 // If there's an extension, read and add in the extension size. | |
231 if (header[0] & 0x10) { | |
232 if (len < header_size + sizeof(uint32_t)) | |
233 return false; | |
234 header_size += | |
235 ((rtc::GetBE16(header + header_size + 2) + 1) * sizeof(uint32_t)); | |
236 if (len < header_size) return false; | |
237 } | |
238 *value = header_size; | |
239 return true; | |
240 } | |
241 | |
242 bool 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 | |
249 bool 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. | |
259 bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value) { | |
260 // 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; | |
266 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + 4); | |
267 return true; | |
268 } | |
269 | |
270 bool SetRtpSsrc(void* data, size_t len, uint32_t value) { | |
271 return SetUint32(data, kRtpSsrcOffset, value); | |
272 } | |
273 | |
274 // Assumes version 2, no padding, no extensions, no csrcs. | |
275 bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) { | |
276 if (!IsValidRtpPayloadType(header.payload_type) || | |
277 header.seq_num < 0 || header.seq_num > UINT16_MAX) { | |
278 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) && | |
285 SetRtpSsrc(data, len, header.ssrc)); | |
286 } | |
287 | |
288 bool IsRtpPacket(const void* data, size_t len) { | |
289 if (len < kMinRtpPacketLen) | |
290 return false; | |
291 | |
292 return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion; | |
293 } | |
294 | |
295 bool IsValidRtpPayloadType(int payload_type) { | |
296 return payload_type >= 0 && payload_type <= 127; | |
297 } | |
298 | |
299 bool 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. | |
353 bool 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 | |
436 bool 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 | |
481 } // namespace cricket | |
OLD | NEW |