OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 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 | |
11 #include "webrtc/p2p/base/rtppacketutil.h" | |
12 | |
13 #include <string.h> | |
14 | |
15 #include "webrtc/base/asyncpacketsocket.h" | |
16 #include "webrtc/base/byteorder.h" | |
17 #include "webrtc/base/checks.h" | |
18 #include "webrtc/base/messagedigest.h" | |
19 #include "webrtc/base/timeutils.h" | |
20 #include "webrtc/p2p/base/stun.h" | |
21 | |
22 namespace rtc { | |
23 | |
24 namespace { | |
25 | |
26 const size_t kMinRtpHeaderLength = 12; | |
pthatcher1
2016/01/13 23:21:33
There's a lot of overlap between this and rtputils
Sergey Ulanov
2016/01/14 06:02:21
I didn't know about rtputils.cc. Move all the code
| |
27 const size_t kMinRtcpHeaderLength = 8; | |
28 const size_t kRtpExtensionHeaderLength = 4; | |
29 const size_t kDtlsRecordHeaderLength = 13; | |
30 const size_t kTurnChannelHeaderLength = 4; | |
31 const size_t kAbsSendTimeExtensionLength = 3; | |
32 const size_t kOneByteHeaderLength = 1; | |
33 const size_t kMaxRtpPacketLength = 2048; | |
34 | |
35 // Fake auth tag written by the render process if external authentication is | |
pthatcher1
2016/01/13 23:21:33
Does this still make sense being here? It doesn't
Sergey Ulanov
2016/01/14 06:02:21
Updated the comment
| |
36 // enabled. HMAC in packet will be compared against this value before updating | |
37 // packet with actual HMAC value. | |
38 static const unsigned char kFakeAuthTag[10] = { | |
39 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd | |
40 }; | |
41 | |
42 bool IsTurnChannelData(const char* data, size_t length) { | |
43 return length >= kTurnChannelHeaderLength && ((*data & 0xC0) == 0x40); | |
44 } | |
45 | |
46 bool IsTurnSendIndicationPacket(const char* data, size_t length) { | |
47 if (length < cricket::kStunHeaderSize) { | |
48 return false; | |
49 } | |
50 | |
51 uint16_t type = rtc::GetBE16(data); | |
52 return (type == cricket::TURN_SEND_INDICATION); | |
53 } | |
pthatcher1
2016/01/13 23:21:33
The above are not RTP, and so seem awkward being i
Sergey Ulanov
2016/01/14 06:02:21
Moved to turnutils.cc
| |
54 | |
55 bool IsRtpPacket(const char* data, size_t length) { | |
56 return (length >= kMinRtpHeaderLength) && ((*data & 0xC0) == 0x80); | |
57 } | |
58 | |
59 void UpdateAbsSendTimeExtensionValue(char* extension_data, | |
60 size_t length, | |
61 uint32_t abs_send_time) { | |
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 != kAbsSendTimeExtensionLength) { | |
78 RTC_NOTREACHED(); | |
79 return; | |
80 } | |
81 | |
82 // Now() has resolution ~1-15ms | |
83 uint32_t now_second = abs_send_time; | |
84 if (!now_second) { | |
85 uint64_t now_us = rtc::TimeMicros(); | |
86 // Convert second to 24-bit unsigned with 18 bit fractional part | |
87 now_second = ((now_us << 18) / rtc::kNumMicrosecsPerSec) & 0x00FFFFFF; | |
88 } | |
pthatcher1
2016/01/13 23:21:33
It seems like it would be better to pass in the ti
Sergey Ulanov
2016/01/14 06:02:21
Done.
| |
89 // TODO(mallinath): Add SetBE24 to byteorder.h. | |
90 extension_data[0] = static_cast<uint8_t>(now_second >> 16); | |
91 extension_data[1] = static_cast<uint8_t>(now_second >> 8); | |
92 extension_data[2] = static_cast<uint8_t>(now_second); | |
93 } | |
94 | |
95 // Assumes |length| is actual packet length + tag length. Updates HMAC at end of | |
96 // the RTP packet. | |
97 void UpdateRtpAuthTag(char* rtp, size_t length, const PacketOptions& options) { | |
pthatcher1
2016/01/13 23:21:33
It seems like the key and len should be passed in
Sergey Ulanov
2016/01/14 06:02:21
Updated it to pass rtc::PacketTimeUpdateParams. Th
| |
98 // If there is no key, return. | |
99 if (options.packet_time_params.srtp_auth_key.empty()) { | |
100 return; | |
101 } | |
102 | |
103 size_t tag_length = options.packet_time_params.srtp_auth_tag_len; | |
104 | |
105 // ROC (rollover counter) is at the beginning of the auth tag. | |
106 const size_t kRocLength = 4; | |
107 if (tag_length < kRocLength || tag_length > length) { | |
108 RTC_NOTREACHED(); | |
109 return; | |
110 } | |
111 | |
112 char* auth_tag = rtp + (length - tag_length); | |
113 | |
114 // We should have a fake HMAC value @ auth_tag. | |
115 RTC_DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length)); | |
116 | |
117 // Copy ROC after end of rtp packet. | |
118 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, kRocLength); | |
119 // Authentication of a RTP packet will have RTP packet + ROC size. | |
120 int auth_required_length = length - tag_length + kRocLength; | |
121 | |
122 unsigned char output[64]; | |
123 | |
124 size_t result = ComputeHmac( | |
125 rtc::DIGEST_SHA_1, &options.packet_time_params.srtp_auth_key[0], | |
126 options.packet_time_params.srtp_auth_key.size(), rtp, | |
127 auth_required_length, output, sizeof(output)); | |
128 | |
129 if (result < tag_length) { | |
130 RTC_NOTREACHED(); | |
131 return; | |
132 } | |
133 | |
134 // Copy HMAC from output to packet. This is required as auth tag length | |
135 // may not be equal to the actual HMAC length. | |
136 memcpy(auth_tag, output, tag_length); | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 bool IsDtlsPacket(const char* data, size_t length) { | |
142 const uint8_t* u = reinterpret_cast<const uint8_t*>(data); | |
143 return (length >= kDtlsRecordHeaderLength && (u[0] > 19 && u[0] < 64)); | |
144 } | |
pthatcher1
2016/01/13 23:21:33
This doesn't seem to belong in an rtputils file ei
Sergey Ulanov
2016/01/14 06:02:21
Done.
| |
145 | |
146 bool IsRtcpPacket(const char* data, size_t length) { | |
147 if (length < kMinRtcpHeaderLength) { | |
148 return false; | |
149 } | |
150 | |
151 int type = (static_cast<uint8_t>(data[1]) & 0x7F); | |
152 return (type >= 64 && type < 96); | |
153 } | |
154 | |
155 bool ValidateRtpHeader(const char* rtp, size_t length, size_t* header_length) { | |
156 if (header_length) { | |
157 *header_length = 0; | |
158 } | |
159 | |
160 if (length < kMinRtpHeaderLength) { | |
161 return false; | |
162 } | |
163 | |
164 size_t cc_count = rtp[0] & 0x0F; | |
165 size_t header_length_without_extension = kMinRtpHeaderLength + 4 * cc_count; | |
166 if (header_length_without_extension > length) { | |
167 return false; | |
168 } | |
169 | |
170 // If extension bit is not set, we are done with header processing, as input | |
171 // length is verified above. | |
172 if (!(rtp[0] & 0x10)) { | |
173 if (header_length) | |
174 *header_length = header_length_without_extension; | |
175 | |
176 return true; | |
177 } | |
178 | |
179 rtp += header_length_without_extension; | |
180 | |
181 if (header_length_without_extension + kRtpExtensionHeaderLength > length) { | |
182 return false; | |
183 } | |
184 | |
185 // Getting extension profile length. | |
186 // Length is in 32 bit words. | |
187 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); | |
188 size_t extension_length = extension_length_in_32bits * 4; | |
189 | |
190 size_t rtp_header_length = extension_length + | |
191 header_length_without_extension + | |
192 kRtpExtensionHeaderLength; | |
193 | |
194 // Verify input length against total header size. | |
195 if (rtp_header_length > length) { | |
196 return false; | |
197 } | |
198 | |
199 if (header_length) { | |
200 *header_length = rtp_header_length; | |
201 } | |
202 return true; | |
203 } | |
204 | |
205 bool ApplyPacketOptions(char* data, | |
206 size_t length, | |
207 const rtc::PacketOptions& options, | |
208 uint32_t abs_send_time) { | |
pthatcher1
2016/01/13 23:21:33
This seems like it should be UpdateRtpPacket, with
Sergey Ulanov
2016/01/14 06:02:21
Changed it to use rtc::PacketTimeUpdateParams
| |
209 RTC_DCHECK(data != NULL); | |
210 RTC_DCHECK(length > 0); | |
211 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in | |
212 // PacketOptions, nothing to be updated in this packet. | |
213 if (options.packet_time_params.rtp_sendtime_extension_id == -1 && | |
214 options.packet_time_params.srtp_auth_key.empty()) { | |
215 return true; | |
216 } | |
217 | |
218 RTC_DCHECK(!IsDtlsPacket(data, length)); | |
219 RTC_DCHECK(!IsRtcpPacket(data, length)); | |
220 | |
221 // If there is a srtp auth key present then packet must be a RTP packet. | |
222 // RTP packet may have been wrapped in a TURN Channel Data or | |
223 // TURN send indication. | |
224 size_t rtp_start_pos; | |
225 size_t rtp_length; | |
226 if (!GetRtpPacketStartPositionAndLength( | |
227 data, length, &rtp_start_pos, &rtp_length)) { | |
228 // This method should never return false. | |
229 RTC_NOTREACHED(); | |
230 return false; | |
231 } | |
232 | |
233 // Skip to rtp packet. | |
234 char* start = data + rtp_start_pos; | |
235 // If packet option has non default value (-1) for sendtime extension id, | |
236 // then we should parse the rtp packet to update the timestamp. Otherwise | |
237 // just calculate HMAC and update packet with it. | |
238 if (options.packet_time_params.rtp_sendtime_extension_id != -1) { | |
239 UpdateRtpAbsSendTimeExtension( | |
240 start, | |
241 rtp_length, | |
242 options.packet_time_params.rtp_sendtime_extension_id, | |
243 abs_send_time); | |
244 } | |
245 | |
246 UpdateRtpAuthTag(start, rtp_length, options); | |
247 return true; | |
248 } | |
249 | |
250 bool GetRtpPacketStartPositionAndLength(const char* packet, | |
251 size_t length, | |
252 size_t* rtp_start_pos, | |
253 size_t* rtp_packet_length) { | |
pthatcher1
2016/01/13 23:21:33
This is kind of a funny function. "Find an RTP pa
Sergey Ulanov
2016/01/14 06:02:21
Moved to turnutils.cc and renamed to UnwrapTurnPac
| |
254 if (length < kMinRtpHeaderLength || length > kMaxRtpPacketLength) { | |
255 return false; | |
256 } | |
257 | |
258 size_t rtp_begin; | |
259 size_t rtp_length = 0; | |
260 if (IsTurnChannelData(packet, length)) { | |
261 // Turn Channel Message header format. | |
262 // 0 1 2 3 | |
263 // 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 | |
264 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
265 // | Channel Number | Length | | |
266 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
267 // | | | |
268 // / Application Data / | |
269 // / / | |
270 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
271 rtp_begin = kTurnChannelHeaderLength; | |
272 rtp_length = rtc::GetBE16(&packet[2]); | |
273 if (length < rtp_length + kTurnChannelHeaderLength) { | |
274 return false; | |
275 } | |
276 } else if (IsTurnSendIndicationPacket(packet, length)) { | |
277 // Validate STUN message length. | |
278 const size_t stun_message_length = rtc::GetBE16(&packet[2]); | |
279 if (stun_message_length + cricket::kStunHeaderSize != length) { | |
280 return false; | |
281 } | |
282 | |
283 // First skip mandatory stun header which is of 20 bytes. | |
284 rtp_begin = cricket::kStunHeaderSize; | |
285 // Loop through STUN attributes until we find STUN DATA attribute. | |
286 bool data_attr_present = false; | |
287 while (rtp_begin < length) { | |
288 // Keep reading STUN attributes until we hit DATA attribute. | |
289 // Attribute will be a TLV structure. | |
290 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
291 // | Type | Length | | |
292 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
293 // | Value (variable) .... | |
294 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
295 // The value in the length field MUST contain the length of the Value | |
296 // part of the attribute, prior to padding, measured in bytes. Since | |
297 // STUN aligns attributes on 32-bit boundaries, attributes whose content | |
298 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of | |
299 // padding so that its value contains a multiple of 4 bytes. The | |
300 // padding bits are ignored, and may be any value. | |
301 uint16_t attr_type, attr_length; | |
302 const int kAttrHeaderLength = sizeof(attr_type) + sizeof(attr_length); | |
303 | |
304 if (length < rtp_begin + kAttrHeaderLength) { | |
305 return false; | |
306 } | |
307 | |
308 // Getting attribute type and length. | |
309 attr_type = rtc::GetBE16(&packet[rtp_begin]); | |
310 attr_length = rtc::GetBE16( | |
311 &packet[rtp_begin + sizeof(attr_type)]); | |
312 | |
313 rtp_begin += kAttrHeaderLength; // Skip STUN_DATA_ATTR header. | |
314 | |
315 // Checking for bogus attribute length. | |
316 if (length < rtp_begin + attr_length) { | |
317 return false; | |
318 } | |
319 | |
320 if (attr_type != cricket::STUN_ATTR_DATA) { | |
321 rtp_begin += attr_length; | |
322 if ((attr_length % 4) != 0) { | |
323 rtp_begin += (4 - (attr_length % 4)); | |
324 } | |
325 continue; | |
326 } | |
327 | |
328 data_attr_present = true; | |
329 rtp_length = attr_length; | |
330 | |
331 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet. | |
332 break; | |
333 } | |
334 | |
335 if (!data_attr_present) { | |
336 // There is no data attribute present in the message. We can't do anything | |
337 // with the message. | |
338 return false; | |
339 } | |
340 | |
341 } else { | |
342 // This is a raw RTP packet. | |
343 rtp_begin = 0; | |
344 rtp_length = length; | |
345 } | |
346 | |
347 // Making sure we have a valid RTP packet at the end. | |
348 if (IsRtpPacket(packet + rtp_begin, rtp_length) && | |
349 ValidateRtpHeader(packet + rtp_begin, rtp_length, NULL)) { | |
350 *rtp_start_pos = rtp_begin; | |
351 *rtp_packet_length = rtp_length; | |
352 return true; | |
353 } | |
354 return false; | |
355 } | |
356 | |
357 // ValidateRtpHeader must be called before this method to make sure, we have | |
358 // a sane rtp packet. | |
359 bool UpdateRtpAbsSendTimeExtension(char* rtp, | |
360 size_t length, | |
361 int extension_id, | |
362 uint32_t abs_send_time) { | |
363 // 0 1 2 3 | |
364 // 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 | |
365 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
366 // |V=2|P|X| CC |M| PT | sequence number | | |
367 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
368 // | timestamp | | |
369 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
370 // | synchronization source (SSRC) identifier | | |
371 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
372 // | contributing source (CSRC) identifiers | | |
373 // | .... | | |
374 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
375 | |
376 // Return if extension bit is not set. | |
377 if (!(rtp[0] & 0x10)) { | |
378 return true; | |
379 } | |
380 | |
381 size_t cc_count = rtp[0] & 0x0F; | |
382 size_t header_length_without_extension = kMinRtpHeaderLength + 4 * cc_count; | |
383 | |
384 rtp += header_length_without_extension; | |
385 | |
386 // Getting extension profile ID and length. | |
387 uint16_t profile_id = rtc::GetBE16(rtp); | |
388 // Length is in 32 bit words. | |
389 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); | |
390 size_t extension_length = extension_length_in_32bits * 4; | |
391 | |
392 rtp += kRtpExtensionHeaderLength; // Moving past extension header. | |
393 | |
394 bool found = false; | |
395 // WebRTC is using one byte header extension. | |
396 // TODO(mallinath) - Handle two byte header extension. | |
397 if (profile_id == 0xBEDE) { // OneByte extension header | |
398 // 0 | |
399 // 0 1 2 3 4 5 6 7 | |
400 // +-+-+-+-+-+-+-+-+ | |
401 // | ID |length | | |
402 // +-+-+-+-+-+-+-+-+ | |
403 | |
404 // 0 1 2 3 | |
405 // 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 | |
406 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
407 // | 0xBE | 0xDE | length=3 | | |
408 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
409 // | ID | L=0 | data | ID | L=1 | data... | |
410 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
411 // ...data | 0 (pad) | 0 (pad) | ID | L=3 | | |
412 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
413 // | data | | |
414 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
415 const char* extension_start = rtp; | |
416 const char* extension_end = extension_start + extension_length; | |
417 | |
418 while (rtp < extension_end) { | |
419 const int id = (*rtp & 0xF0) >> 4; | |
420 const size_t length = (*rtp & 0x0F) + 1; | |
421 if (rtp + kOneByteHeaderLength + length > extension_end) { | |
422 return false; | |
423 } | |
424 // The 4-bit length is the number minus one of data bytes of this header | |
425 // extension element following the one-byte header. | |
426 if (id == extension_id) { | |
427 UpdateAbsSendTimeExtensionValue( | |
428 rtp + kOneByteHeaderLength, length, abs_send_time); | |
429 found = true; | |
430 break; | |
431 } | |
432 rtp += kOneByteHeaderLength + length; | |
433 // Counting padding bytes. | |
434 while ((rtp < extension_end) && (*rtp == 0)) { | |
435 ++rtp; | |
436 } | |
437 } | |
438 } | |
439 return found; | |
440 } | |
441 | |
442 } // namespace rtc | |
OLD | NEW |