blob: e1bf03be62f8b4bb2101d6396cfa24d268af07c0 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 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
Patrik Höglund56d94522019-11-18 15:53:32 +010011#include "api/transport/stun.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012
13#include <string.h>
Yves Gerey665174f2018-06-19 15:03:05 +020014#include <algorithm>
Harald Alvestrandbee64082020-11-12 11:17:41 +000015#include <cstdint>
16#include <iterator>
kwiberg3ec46792016-04-27 07:22:53 -070017#include <memory>
Steve Anton6c38cc72017-11-29 10:25:58 -080018#include <utility>
kwiberg3ec46792016-04-27 07:22:53 -070019
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/byte_order.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
22#include "rtc_base/crc32.h"
23#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/message_digest.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
jbauchf1f87202016-03-30 06:43:37 -070026using rtc::ByteBufferReader;
27using rtc::ByteBufferWriter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028
Harald Alvestrandbee64082020-11-12 11:17:41 +000029namespace cricket {
30
Zach Stein92c42892018-11-28 11:38:52 -080031namespace {
32
Harald Alvestrandbee64082020-11-12 11:17:41 +000033const int k127Utf8CharactersLengthInBytes = 508;
34const int kDefaultMaxAttributeLength = 508;
35const int kMessageIntegrityAttributeLength = 20;
Harald Alvestrand837f13c2020-12-04 07:52:22 +000036const int kTheoreticalMaximumAttributeLength = 65535;
Harald Alvestrandbee64082020-11-12 11:17:41 +000037
Zach Stein92c42892018-11-28 11:38:52 -080038uint32_t ReduceTransactionId(const std::string& transaction_id) {
39 RTC_DCHECK(transaction_id.length() == cricket::kStunTransactionIdLength ||
40 transaction_id.length() ==
41 cricket::kStunLegacyTransactionIdLength);
Danil Chapovalov7b46e172019-11-14 17:40:23 +010042 ByteBufferReader reader(transaction_id.c_str(), transaction_id.length());
Zach Stein92c42892018-11-28 11:38:52 -080043 uint32_t result = 0;
Zach Steinff71a492018-12-07 11:25:12 -080044 uint32_t next;
45 while (reader.ReadUInt32(&next)) {
46 result ^= next;
Zach Stein92c42892018-11-28 11:38:52 -080047 }
48 return result;
49}
50
Harald Alvestrandbee64082020-11-12 11:17:41 +000051// Check the maximum length of a BYTE_STRING attribute against specifications.
52bool LengthValid(int type, int length) {
53 // "Less than 509 bytes" is intended to indicate a maximum of 127
54 // UTF-8 characters, which may take up to 4 bytes per character.
55 switch (type) {
56 case STUN_ATTR_USERNAME:
57 return length <=
58 k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.3
59 case STUN_ATTR_MESSAGE_INTEGRITY:
60 return length ==
61 kMessageIntegrityAttributeLength; // RFC 8489 section 14.5
62 case STUN_ATTR_REALM:
63 return length <=
64 k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.9
65 case STUN_ATTR_NONCE:
66 return length <=
67 k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.10
68 case STUN_ATTR_SOFTWARE:
69 return length <=
70 k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.14
71 case STUN_ATTR_ORIGIN:
72 // 0x802F is unassigned by IANA.
73 // RESPONSE-ORIGIN is defined in RFC 5780 section 7.3, but does not
74 // specify a maximum length. It's an URL, so return an arbitrary
75 // restriction.
76 return length <= kDefaultMaxAttributeLength;
77 case STUN_ATTR_DATA:
78 // No length restriction in RFC; it's the content of an UDP datagram,
79 // which in theory can be up to 65.535 bytes.
80 // TODO(bugs.webrtc.org/12179): Write a test to find the real limit.
Harald Alvestrand837f13c2020-12-04 07:52:22 +000081 return length <= kTheoreticalMaximumAttributeLength;
Harald Alvestrandbee64082020-11-12 11:17:41 +000082 default:
83 // Return an arbitrary restriction for all other types.
Harald Alvestrand837f13c2020-12-04 07:52:22 +000084 return length <= kTheoreticalMaximumAttributeLength;
Harald Alvestrandbee64082020-11-12 11:17:41 +000085 }
86 RTC_NOTREACHED();
87 return true;
88}
Zach Stein92c42892018-11-28 11:38:52 -080089
Harald Alvestrandbee64082020-11-12 11:17:41 +000090} // namespace
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091
92const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server";
93const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request";
94const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized";
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -070095const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[] = "Unknown Attribute";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden";
97const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials";
98const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch";
99const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce";
100const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials";
101const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol";
102const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict";
103const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error";
104
Yves Gerey665174f2018-06-19 15:03:05 +0200105const char TURN_MAGIC_COOKIE_VALUE[] = {'\x72', '\xC6', '\x4B', '\xC6'};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106const char EMPTY_TRANSACTION_ID[] = "0000000000000000";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200107const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E;
Eldar Relloda13ea22019-06-01 12:23:43 +0300108const int SERVER_NOT_REACHABLE_ERROR = 701;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000109
110// StunMessage
111
112StunMessage::StunMessage()
113 : type_(0),
114 length_(0),
Jonas Oreland7ca63112018-02-27 08:45:13 +0100115 transaction_id_(EMPTY_TRANSACTION_ID),
116 stun_magic_cookie_(kStunMagicCookie) {
nisseede5da42017-01-12 05:15:36 -0800117 RTC_DCHECK(IsValidTransactionId(transaction_id_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000118}
119
Steve Antonca7d54e2017-10-25 14:42:51 -0700120StunMessage::~StunMessage() = default;
121
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122bool StunMessage::IsLegacy() const {
123 if (transaction_id_.size() == kStunLegacyTransactionIdLength)
124 return true;
nisseede5da42017-01-12 05:15:36 -0800125 RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126 return false;
127}
128
129bool StunMessage::SetTransactionID(const std::string& str) {
130 if (!IsValidTransactionId(str)) {
131 return false;
132 }
133 transaction_id_ = str;
Zach Stein92c42892018-11-28 11:38:52 -0800134 reduced_transaction_id_ = ReduceTransactionId(transaction_id_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 return true;
136}
137
Jonas Oreland16ccef72018-03-27 09:02:43 +0200138static bool DesignatedExpertRange(int attr_type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200139 return (attr_type >= 0x4000 && attr_type <= 0x7FFF) ||
140 (attr_type >= 0xC000 && attr_type <= 0xFFFF);
Jonas Orelandbdcee282017-10-10 14:01:40 +0200141}
142
zsteinf42cc9d2017-03-27 16:17:19 -0700143void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200144 // Fail any attributes that aren't valid for this type of message,
Jonas Oreland16ccef72018-03-27 09:02:43 +0200145 // but allow any type for the range that in the RFC is reserved for
146 // the "designated experts".
147 if (!DesignatedExpertRange(attr->type())) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200148 RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type()));
149 }
nissecc99bc22017-02-02 01:31:30 -0800150
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 attr->SetOwner(this);
152 size_t attr_length = attr->length();
153 if (attr_length % 4 != 0) {
154 attr_length += (4 - (attr_length % 4));
155 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200156 length_ += static_cast<uint16_t>(attr_length + 4);
zsteinf42cc9d2017-03-27 16:17:19 -0700157
158 attrs_.push_back(std::move(attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159}
160
Jonas Oreland202994c2017-12-18 12:10:43 +0100161std::unique_ptr<StunAttribute> StunMessage::RemoveAttribute(int type) {
162 std::unique_ptr<StunAttribute> attribute;
163 for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) {
Yves Gerey665174f2018-06-19 15:03:05 +0200164 if ((*it)->type() == type) {
165 attribute = std::move(*it);
Jonas Oreland202994c2017-12-18 12:10:43 +0100166 attrs_.erase(std::next(it).base());
167 break;
168 }
169 }
170 if (attribute) {
171 attribute->SetOwner(nullptr);
172 size_t attr_length = attribute->length();
173 if (attr_length % 4 != 0) {
174 attr_length += (4 - (attr_length % 4));
175 }
176 length_ -= static_cast<uint16_t>(attr_length + 4);
177 }
178 return attribute;
179}
180
Jonas Oreland63737a92019-11-21 15:12:14 +0100181void StunMessage::ClearAttributes() {
182 for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) {
183 (*it)->SetOwner(nullptr);
184 }
185 attrs_.clear();
186 length_ = 0;
187}
188
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700189std::vector<uint16_t> StunMessage::GetNonComprehendedAttributes() const {
190 std::vector<uint16_t> unknown_attributes;
191 for (auto& attr : attrs_) {
192 // "comprehension-required" range is 0x0000-0x7FFF.
193 if (attr->type() >= 0x0000 && attr->type() <= 0x7FFF &&
194 GetAttributeValueType(attr->type()) == STUN_VALUE_UNKNOWN) {
195 unknown_attributes.push_back(attr->type());
196 }
197 }
198 return unknown_attributes;
199}
200
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201const StunAddressAttribute* StunMessage::GetAddress(int type) const {
202 switch (type) {
203 case STUN_ATTR_MAPPED_ADDRESS: {
204 // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is
205 // missing.
206 const StunAttribute* mapped_address =
207 GetAttribute(STUN_ATTR_MAPPED_ADDRESS);
208 if (!mapped_address)
209 mapped_address = GetAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS);
210 return reinterpret_cast<const StunAddressAttribute*>(mapped_address);
211 }
212
213 default:
214 return static_cast<const StunAddressAttribute*>(GetAttribute(type));
215 }
216}
217
218const StunUInt32Attribute* StunMessage::GetUInt32(int type) const {
219 return static_cast<const StunUInt32Attribute*>(GetAttribute(type));
220}
221
222const StunUInt64Attribute* StunMessage::GetUInt64(int type) const {
223 return static_cast<const StunUInt64Attribute*>(GetAttribute(type));
224}
225
226const StunByteStringAttribute* StunMessage::GetByteString(int type) const {
227 return static_cast<const StunByteStringAttribute*>(GetAttribute(type));
228}
229
Jonas Oreland1721de12019-11-20 12:10:39 +0100230const StunUInt16ListAttribute* StunMessage::GetUInt16List(int type) const {
231 return static_cast<const StunUInt16ListAttribute*>(GetAttribute(type));
232}
233
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234const StunErrorCodeAttribute* StunMessage::GetErrorCode() const {
235 return static_cast<const StunErrorCodeAttribute*>(
236 GetAttribute(STUN_ATTR_ERROR_CODE));
237}
238
deadbeef996fc6b2017-04-26 09:21:22 -0700239int StunMessage::GetErrorCodeValue() const {
240 const StunErrorCodeAttribute* error_attribute = GetErrorCode();
241 return error_attribute ? error_attribute->code() : STUN_ERROR_GLOBAL_FAILURE;
242}
243
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000244const StunUInt16ListAttribute* StunMessage::GetUnknownAttributes() const {
245 return static_cast<const StunUInt16ListAttribute*>(
246 GetAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES));
247}
248
Yves Gerey665174f2018-06-19 15:03:05 +0200249bool StunMessage::ValidateMessageIntegrity(const char* data,
250 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 const std::string& password) {
Jonas Oreland63737a92019-11-21 15:12:14 +0100252 return ValidateMessageIntegrityOfType(STUN_ATTR_MESSAGE_INTEGRITY,
253 kStunMessageIntegritySize, data, size,
254 password);
255}
256
257bool StunMessage::ValidateMessageIntegrity32(const char* data,
258 size_t size,
259 const std::string& password) {
260 return ValidateMessageIntegrityOfType(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32,
261 kStunMessageIntegrity32Size, data, size,
262 password);
263}
264
265// Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the
266// procedure outlined in RFC 5389, section 15.4.
267bool StunMessage::ValidateMessageIntegrityOfType(int mi_attr_type,
268 size_t mi_attr_size,
269 const char* data,
270 size_t size,
271 const std::string& password) {
272 RTC_DCHECK(mi_attr_size <= kStunMessageIntegritySize);
273
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274 // Verifying the size of the message.
katrielce4bda242016-06-09 08:45:45 -0700275 if ((size % 4) != 0 || size < kStunHeaderSize) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000276 return false;
277 }
278
279 // Getting the message length from the STUN header.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200280 uint16_t msg_length = rtc::GetBE16(&data[2]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000281 if (size != (msg_length + kStunHeaderSize)) {
282 return false;
283 }
284
285 // Finding Message Integrity attribute in stun message.
286 size_t current_pos = kStunHeaderSize;
287 bool has_message_integrity_attr = false;
katrielc1a206102016-06-20 05:13:16 -0700288 while (current_pos + 4 <= size) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200289 uint16_t attr_type, attr_length;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000290 // Getting attribute type and length.
291 attr_type = rtc::GetBE16(&data[current_pos]);
292 attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]);
293
294 // If M-I, sanity check it, and break out.
Jonas Oreland63737a92019-11-21 15:12:14 +0100295 if (attr_type == mi_attr_type) {
296 if (attr_length != mi_attr_size ||
katrielc1a206102016-06-20 05:13:16 -0700297 current_pos + sizeof(attr_type) + sizeof(attr_length) + attr_length >
298 size) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000299 return false;
300 }
301 has_message_integrity_attr = true;
302 break;
303 }
304
305 // Otherwise, skip to the next attribute.
306 current_pos += sizeof(attr_type) + sizeof(attr_length) + attr_length;
307 if ((attr_length % 4) != 0) {
308 current_pos += (4 - (attr_length % 4));
309 }
310 }
311
312 if (!has_message_integrity_attr) {
313 return false;
314 }
315
316 // Getting length of the message to calculate Message Integrity.
317 size_t mi_pos = current_pos;
kwiberg3ec46792016-04-27 07:22:53 -0700318 std::unique_ptr<char[]> temp_data(new char[current_pos]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000319 memcpy(temp_data.get(), data, current_pos);
Jonas Oreland63737a92019-11-21 15:12:14 +0100320 if (size > mi_pos + kStunAttributeHeaderSize + mi_attr_size) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000321 // Stun message has other attributes after message integrity.
322 // Adjust the length parameter in stun message to calculate HMAC.
Yves Gerey665174f2018-06-19 15:03:05 +0200323 size_t extra_offset =
Jonas Oreland63737a92019-11-21 15:12:14 +0100324 size - (mi_pos + kStunAttributeHeaderSize + mi_attr_size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000325 size_t new_adjusted_len = size - extra_offset - kStunHeaderSize;
326
327 // Writing new length of the STUN message @ Message Length in temp buffer.
328 // 0 1 2 3
329 // 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
330 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
331 // |0 0| STUN Message Type | Message Length |
332 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Peter Boström0c4e06b2015-10-07 12:23:21 +0200333 rtc::SetBE16(temp_data.get() + 2, static_cast<uint16_t>(new_adjusted_len));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000334 }
335
336 char hmac[kStunMessageIntegritySize];
Yves Gerey665174f2018-06-19 15:03:05 +0200337 size_t ret =
338 rtc::ComputeHmac(rtc::DIGEST_SHA_1, password.c_str(), password.size(),
339 temp_data.get(), mi_pos, hmac, sizeof(hmac));
nisseede5da42017-01-12 05:15:36 -0800340 RTC_DCHECK(ret == sizeof(hmac));
Jonas Oreland63737a92019-11-21 15:12:14 +0100341 if (ret != sizeof(hmac)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000342 return false;
Jonas Oreland63737a92019-11-21 15:12:14 +0100343 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344
345 // Comparing the calculated HMAC with the one present in the message.
Yves Gerey665174f2018-06-19 15:03:05 +0200346 return memcmp(data + current_pos + kStunAttributeHeaderSize, hmac,
Jonas Oreland63737a92019-11-21 15:12:14 +0100347 mi_attr_size) == 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000348}
349
350bool StunMessage::AddMessageIntegrity(const std::string& password) {
Jonas Oreland63737a92019-11-21 15:12:14 +0100351 return AddMessageIntegrityOfType(STUN_ATTR_MESSAGE_INTEGRITY,
352 kStunMessageIntegritySize, password.c_str(),
353 password.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000354}
355
Yves Gerey665174f2018-06-19 15:03:05 +0200356bool StunMessage::AddMessageIntegrity(const char* key, size_t keylen) {
Jonas Oreland63737a92019-11-21 15:12:14 +0100357 return AddMessageIntegrityOfType(STUN_ATTR_MESSAGE_INTEGRITY,
358 kStunMessageIntegritySize, key, keylen);
359}
360
361bool StunMessage::AddMessageIntegrity32(absl::string_view password) {
362 return AddMessageIntegrityOfType(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32,
363 kStunMessageIntegrity32Size, password.data(),
364 password.length());
365}
366
367bool StunMessage::AddMessageIntegrityOfType(int attr_type,
368 size_t attr_size,
369 const char* key,
370 size_t keylen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 // Add the attribute with a dummy value. Since this is a known attribute, it
372 // can't fail.
Jonas Oreland63737a92019-11-21 15:12:14 +0100373 RTC_DCHECK(attr_size <= kStunMessageIntegritySize);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200374 auto msg_integrity_attr_ptr = std::make_unique<StunByteStringAttribute>(
Jonas Oreland63737a92019-11-21 15:12:14 +0100375 attr_type, std::string(attr_size, '0'));
zsteinf42cc9d2017-03-27 16:17:19 -0700376 auto* msg_integrity_attr = msg_integrity_attr_ptr.get();
377 AddAttribute(std::move(msg_integrity_attr_ptr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378
379 // Calculate the HMAC for the message.
jbauchf1f87202016-03-30 06:43:37 -0700380 ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 if (!Write(&buf))
382 return false;
383
384 int msg_len_for_hmac = static_cast<int>(
385 buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length());
386 char hmac[kStunMessageIntegritySize];
Yves Gerey665174f2018-06-19 15:03:05 +0200387 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, key, keylen, buf.Data(),
388 msg_len_for_hmac, hmac, sizeof(hmac));
nisseede5da42017-01-12 05:15:36 -0800389 RTC_DCHECK(ret == sizeof(hmac));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000390 if (ret != sizeof(hmac)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100391 RTC_LOG(LS_ERROR) << "HMAC computation failed. Message-Integrity "
Jonas Olssond7d762d2018-03-28 09:47:51 +0200392 "has dummy value.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000393 return false;
394 }
395
396 // Insert correct HMAC into the attribute.
Jonas Oreland63737a92019-11-21 15:12:14 +0100397 msg_integrity_attr->CopyBytes(hmac, attr_size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000398 return true;
399}
400
401// Verifies a message is in fact a STUN message, by performing the checks
402// outlined in RFC 5389, section 7.3, including the FINGERPRINT check detailed
403// in section 15.5.
404bool StunMessage::ValidateFingerprint(const char* data, size_t size) {
405 // Check the message length.
406 size_t fingerprint_attr_size =
407 kStunAttributeHeaderSize + StunUInt32Attribute::SIZE;
408 if (size % 4 != 0 || size < kStunHeaderSize + fingerprint_attr_size)
409 return false;
410
411 // Skip the rest if the magic cookie isn't present.
412 const char* magic_cookie =
413 data + kStunTransactionIdOffset - kStunMagicCookieLength;
414 if (rtc::GetBE32(magic_cookie) != kStunMagicCookie)
415 return false;
416
417 // Check the fingerprint type and length.
418 const char* fingerprint_attr_data = data + size - fingerprint_attr_size;
419 if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT ||
Peter Boström0c4e06b2015-10-07 12:23:21 +0200420 rtc::GetBE16(fingerprint_attr_data + sizeof(uint16_t)) !=
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000421 StunUInt32Attribute::SIZE)
422 return false;
423
424 // Check the fingerprint value.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200425 uint32_t fingerprint =
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000426 rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize);
427 return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) ==
Yves Gerey665174f2018-06-19 15:03:05 +0200428 rtc::ComputeCrc32(data, size - fingerprint_attr_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000429}
430
Jonas Oreland253d50f2019-11-28 17:08:07 +0100431bool StunMessage::IsStunMethod(rtc::ArrayView<int> methods,
432 const char* data,
433 size_t size) {
434 // Check the message length.
435 if (size % 4 != 0 || size < kStunHeaderSize)
436 return false;
437
438 // Skip the rest if the magic cookie isn't present.
439 const char* magic_cookie =
440 data + kStunTransactionIdOffset - kStunMagicCookieLength;
441 if (rtc::GetBE32(magic_cookie) != kStunMagicCookie)
442 return false;
443
444 int method = rtc::GetBE16(data);
445 for (int m : methods) {
446 if (m == method) {
447 return true;
448 }
449 }
450 return false;
451}
452
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000453bool StunMessage::AddFingerprint() {
454 // Add the attribute with a dummy value. Since this is a known attribute,
455 // it can't fail.
zsteinf42cc9d2017-03-27 16:17:19 -0700456 auto fingerprint_attr_ptr =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200457 std::make_unique<StunUInt32Attribute>(STUN_ATTR_FINGERPRINT, 0);
Steve Antonca7d54e2017-10-25 14:42:51 -0700458 auto* fingerprint_attr = fingerprint_attr_ptr.get();
zsteinf42cc9d2017-03-27 16:17:19 -0700459 AddAttribute(std::move(fingerprint_attr_ptr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460
461 // Calculate the CRC-32 for the message and insert it.
jbauchf1f87202016-03-30 06:43:37 -0700462 ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000463 if (!Write(&buf))
464 return false;
465
466 int msg_len_for_crc32 = static_cast<int>(
467 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200468 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000469
470 // Insert the correct CRC-32, XORed with a constant, into the attribute.
471 fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE);
472 return true;
473}
474
jbauchf1f87202016-03-30 06:43:37 -0700475bool StunMessage::Read(ByteBufferReader* buf) {
Jonas Oreland1721de12019-11-20 12:10:39 +0100476 if (!buf->ReadUInt16(&type_)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000477 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +0100478 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000479
480 if (type_ & 0x8000) {
481 // RTP and RTCP set the MSB of first byte, since first two bits are version,
482 // and version is always 2 (10). If set, this is not a STUN packet.
483 return false;
484 }
485
Jonas Oreland1721de12019-11-20 12:10:39 +0100486 if (!buf->ReadUInt16(&length_)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000487 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +0100488 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000489
490 std::string magic_cookie;
Jonas Oreland1721de12019-11-20 12:10:39 +0100491 if (!buf->ReadString(&magic_cookie, kStunMagicCookieLength)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000492 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +0100493 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000494
495 std::string transaction_id;
Jonas Oreland1721de12019-11-20 12:10:39 +0100496 if (!buf->ReadString(&transaction_id, kStunTransactionIdLength)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000497 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +0100498 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000499
Andrew Royes154d8392019-03-14 10:38:31 -0700500 uint32_t magic_cookie_int;
501 static_assert(sizeof(magic_cookie_int) == kStunMagicCookieLength,
502 "Integer size mismatch: magic_cookie_int and kStunMagicCookie");
503 std::memcpy(&magic_cookie_int, magic_cookie.data(), sizeof(magic_cookie_int));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000504 if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) {
505 // If magic cookie is invalid it means that the peer implements
506 // RFC3489 instead of RFC5389.
507 transaction_id.insert(0, magic_cookie);
508 }
nisseede5da42017-01-12 05:15:36 -0800509 RTC_DCHECK(IsValidTransactionId(transaction_id));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000510 transaction_id_ = transaction_id;
Zach Stein92c42892018-11-28 11:38:52 -0800511 reduced_transaction_id_ = ReduceTransactionId(transaction_id_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000512
Jonas Oreland1721de12019-11-20 12:10:39 +0100513 if (length_ != buf->Length()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000514 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +0100515 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000516
zsteinad94c4c2017-03-06 13:36:05 -0800517 attrs_.resize(0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000518
519 size_t rest = buf->Length() - length_;
520 while (buf->Length() > rest) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200521 uint16_t attr_type, attr_length;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000522 if (!buf->ReadUInt16(&attr_type))
523 return false;
524 if (!buf->ReadUInt16(&attr_length))
525 return false;
526
Honghai Zhang3e024302016-09-22 09:52:16 -0700527 std::unique_ptr<StunAttribute> attr(
528 CreateAttribute(attr_type, attr_length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000529 if (!attr) {
530 // Skip any unknown or malformed attributes.
531 if ((attr_length % 4) != 0) {
532 attr_length += (4 - (attr_length % 4));
533 }
Jonas Oreland1721de12019-11-20 12:10:39 +0100534 if (!buf->Consume(attr_length)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000535 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +0100536 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000537 } else {
Jonas Oreland1721de12019-11-20 12:10:39 +0100538 if (!attr->Read(buf)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000539 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +0100540 }
zsteinad94c4c2017-03-06 13:36:05 -0800541 attrs_.push_back(std::move(attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000542 }
543 }
544
nisseede5da42017-01-12 05:15:36 -0800545 RTC_DCHECK(buf->Length() == rest);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000546 return true;
547}
548
jbauchf1f87202016-03-30 06:43:37 -0700549bool StunMessage::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000550 buf->WriteUInt16(type_);
551 buf->WriteUInt16(length_);
552 if (!IsLegacy())
Jonas Oreland7ca63112018-02-27 08:45:13 +0100553 buf->WriteUInt32(stun_magic_cookie_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000554 buf->WriteString(transaction_id_);
555
zsteinad94c4c2017-03-06 13:36:05 -0800556 for (const auto& attr : attrs_) {
557 buf->WriteUInt16(attr->type());
558 buf->WriteUInt16(static_cast<uint16_t>(attr->length()));
559 if (!attr->Write(buf)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000560 return false;
zsteinad94c4c2017-03-06 13:36:05 -0800561 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000562 }
563
564 return true;
565}
566
Steve Antonca7d54e2017-10-25 14:42:51 -0700567StunMessage* StunMessage::CreateNew() const {
568 return new StunMessage();
569}
570
Jonas Oreland7ca63112018-02-27 08:45:13 +0100571void StunMessage::SetStunMagicCookie(uint32_t val) {
572 stun_magic_cookie_ = val;
573}
574
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000575StunAttributeValueType StunMessage::GetAttributeValueType(int type) const {
576 switch (type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200577 case STUN_ATTR_MAPPED_ADDRESS:
578 return STUN_VALUE_ADDRESS;
579 case STUN_ATTR_USERNAME:
580 return STUN_VALUE_BYTE_STRING;
581 case STUN_ATTR_MESSAGE_INTEGRITY:
582 return STUN_VALUE_BYTE_STRING;
583 case STUN_ATTR_ERROR_CODE:
584 return STUN_VALUE_ERROR_CODE;
585 case STUN_ATTR_UNKNOWN_ATTRIBUTES:
586 return STUN_VALUE_UINT16_LIST;
587 case STUN_ATTR_REALM:
588 return STUN_VALUE_BYTE_STRING;
589 case STUN_ATTR_NONCE:
590 return STUN_VALUE_BYTE_STRING;
591 case STUN_ATTR_XOR_MAPPED_ADDRESS:
592 return STUN_VALUE_XOR_ADDRESS;
593 case STUN_ATTR_SOFTWARE:
594 return STUN_VALUE_BYTE_STRING;
595 case STUN_ATTR_ALTERNATE_SERVER:
596 return STUN_VALUE_ADDRESS;
597 case STUN_ATTR_FINGERPRINT:
598 return STUN_VALUE_UINT32;
599 case STUN_ATTR_ORIGIN:
600 return STUN_VALUE_BYTE_STRING;
601 case STUN_ATTR_RETRANSMIT_COUNT:
602 return STUN_VALUE_UINT32;
Jonas Orelandfa543642020-09-16 10:44:54 +0200603 case STUN_ATTR_GOOG_LAST_ICE_CHECK_RECEIVED:
Qingsi Wang0894f0f2019-06-18 14:11:36 -0700604 return STUN_VALUE_BYTE_STRING;
Jonas Oreland1721de12019-11-20 12:10:39 +0100605 case STUN_ATTR_GOOG_MISC_INFO:
606 return STUN_VALUE_UINT16_LIST;
Yves Gerey665174f2018-06-19 15:03:05 +0200607 default:
608 return STUN_VALUE_UNKNOWN;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000609 }
610}
611
612StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ {
613 StunAttributeValueType value_type = GetAttributeValueType(type);
Jonas Orelandbdcee282017-10-10 14:01:40 +0200614 if (value_type != STUN_VALUE_UNKNOWN) {
615 return StunAttribute::Create(value_type, type,
616 static_cast<uint16_t>(length), this);
Jonas Oreland16ccef72018-03-27 09:02:43 +0200617 } else if (DesignatedExpertRange(type)) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200618 // Read unknown attributes as STUN_VALUE_BYTE_STRING
619 return StunAttribute::Create(STUN_VALUE_BYTE_STRING, type,
620 static_cast<uint16_t>(length), this);
621 } else {
622 return NULL;
623 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000624}
625
626const StunAttribute* StunMessage::GetAttribute(int type) const {
zsteinad94c4c2017-03-06 13:36:05 -0800627 for (const auto& attr : attrs_) {
628 if (attr->type() == type) {
629 return attr.get();
630 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000631 }
632 return NULL;
633}
634
635bool StunMessage::IsValidTransactionId(const std::string& transaction_id) {
636 return transaction_id.size() == kStunTransactionIdLength ||
Yves Gerey665174f2018-06-19 15:03:05 +0200637 transaction_id.size() == kStunLegacyTransactionIdLength;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000638}
639
Jonas Oreland253d50f2019-11-28 17:08:07 +0100640bool StunMessage::EqualAttributes(
641 const StunMessage* other,
642 std::function<bool(int type)> attribute_type_mask) const {
643 RTC_DCHECK(other != nullptr);
644 rtc::ByteBufferWriter tmp_buffer_ptr1;
645 rtc::ByteBufferWriter tmp_buffer_ptr2;
646 for (const auto& attr : attrs_) {
647 if (attribute_type_mask(attr->type())) {
648 const StunAttribute* other_attr = other->GetAttribute(attr->type());
649 if (other_attr == nullptr) {
650 return false;
651 }
652 tmp_buffer_ptr1.Clear();
653 tmp_buffer_ptr2.Clear();
654 attr->Write(&tmp_buffer_ptr1);
655 other_attr->Write(&tmp_buffer_ptr2);
656 if (tmp_buffer_ptr1.Length() != tmp_buffer_ptr2.Length()) {
657 return false;
658 }
659 if (memcmp(tmp_buffer_ptr1.Data(), tmp_buffer_ptr2.Data(),
660 tmp_buffer_ptr1.Length()) != 0) {
661 return false;
662 }
663 }
664 }
665
666 for (const auto& attr : other->attrs_) {
667 if (attribute_type_mask(attr->type())) {
668 const StunAttribute* own_attr = GetAttribute(attr->type());
669 if (own_attr == nullptr) {
670 return false;
671 }
672 // we have already compared all values...
673 }
674 }
675 return true;
676}
677
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000678// StunAttribute
679
Peter Boström0c4e06b2015-10-07 12:23:21 +0200680StunAttribute::StunAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200681 : type_(type), length_(length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000682
jbauchf1f87202016-03-30 06:43:37 -0700683void StunAttribute::ConsumePadding(ByteBufferReader* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000684 int remainder = length_ % 4;
685 if (remainder > 0) {
686 buf->Consume(4 - remainder);
687 }
688}
689
jbauchf1f87202016-03-30 06:43:37 -0700690void StunAttribute::WritePadding(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000691 int remainder = length_ % 4;
692 if (remainder > 0) {
693 char zeroes[4] = {0};
694 buf->WriteBytes(zeroes, 4 - remainder);
695 }
696}
697
698StunAttribute* StunAttribute::Create(StunAttributeValueType value_type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200699 uint16_t type,
700 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000701 StunMessage* owner) {
702 switch (value_type) {
703 case STUN_VALUE_ADDRESS:
704 return new StunAddressAttribute(type, length);
705 case STUN_VALUE_XOR_ADDRESS:
706 return new StunXorAddressAttribute(type, length, owner);
707 case STUN_VALUE_UINT32:
708 return new StunUInt32Attribute(type);
709 case STUN_VALUE_UINT64:
710 return new StunUInt64Attribute(type);
711 case STUN_VALUE_BYTE_STRING:
712 return new StunByteStringAttribute(type, length);
713 case STUN_VALUE_ERROR_CODE:
714 return new StunErrorCodeAttribute(type, length);
715 case STUN_VALUE_UINT16_LIST:
716 return new StunUInt16ListAttribute(type, length);
717 default:
718 return NULL;
719 }
720}
721
zsteinf42cc9d2017-03-27 16:17:19 -0700722std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress(
723 uint16_t type) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200724 return std::make_unique<StunAddressAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000725}
726
zsteinf42cc9d2017-03-27 16:17:19 -0700727std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress(
728 uint16_t type) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200729 return std::make_unique<StunXorAddressAttribute>(type, 0, nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000730}
731
zsteinf42cc9d2017-03-27 16:17:19 -0700732std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64(
733 uint16_t type) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200734 return std::make_unique<StunUInt64Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000735}
736
zsteinf42cc9d2017-03-27 16:17:19 -0700737std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32(
738 uint16_t type) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200739 return std::make_unique<StunUInt32Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000740}
741
zsteinf42cc9d2017-03-27 16:17:19 -0700742std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString(
743 uint16_t type) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200744 return std::make_unique<StunByteStringAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000745}
746
zsteinf42cc9d2017-03-27 16:17:19 -0700747std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200748 return std::make_unique<StunErrorCodeAttribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000749 STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE);
750}
751
zsteinf42cc9d2017-03-27 16:17:19 -0700752std::unique_ptr<StunUInt16ListAttribute>
Jonas Oreland1721de12019-11-20 12:10:39 +0100753StunAttribute::CreateUInt16ListAttribute(uint16_t type) {
754 return std::make_unique<StunUInt16ListAttribute>(type, 0);
755}
756
757std::unique_ptr<StunUInt16ListAttribute>
zsteinf42cc9d2017-03-27 16:17:19 -0700758StunAttribute::CreateUnknownAttributes() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200759 return std::make_unique<StunUInt16ListAttribute>(STUN_ATTR_UNKNOWN_ATTRIBUTES,
760 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000761}
762
Peter Boström0c4e06b2015-10-07 12:23:21 +0200763StunAddressAttribute::StunAddressAttribute(uint16_t type,
764 const rtc::SocketAddress& addr)
765 : StunAttribute(type, 0) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000766 SetAddress(addr);
767}
768
Peter Boström0c4e06b2015-10-07 12:23:21 +0200769StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200770 : StunAttribute(type, length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000771
Steve Antonca7d54e2017-10-25 14:42:51 -0700772StunAttributeValueType StunAddressAttribute::value_type() const {
773 return STUN_VALUE_ADDRESS;
774}
775
jbauchf1f87202016-03-30 06:43:37 -0700776bool StunAddressAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200777 uint8_t dummy;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000778 if (!buf->ReadUInt8(&dummy))
779 return false;
780
Peter Boström0c4e06b2015-10-07 12:23:21 +0200781 uint8_t stun_family;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000782 if (!buf->ReadUInt8(&stun_family)) {
783 return false;
784 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200785 uint16_t port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000786 if (!buf->ReadUInt16(&port))
787 return false;
788 if (stun_family == STUN_ADDRESS_IPV4) {
789 in_addr v4addr;
790 if (length() != SIZE_IP4) {
791 return false;
792 }
793 if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) {
794 return false;
795 }
796 rtc::IPAddress ipaddr(v4addr);
797 SetAddress(rtc::SocketAddress(ipaddr, port));
798 } else if (stun_family == STUN_ADDRESS_IPV6) {
799 in6_addr v6addr;
800 if (length() != SIZE_IP6) {
801 return false;
802 }
803 if (!buf->ReadBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr))) {
804 return false;
805 }
806 rtc::IPAddress ipaddr(v6addr);
807 SetAddress(rtc::SocketAddress(ipaddr, port));
808 } else {
809 return false;
810 }
811 return true;
812}
813
jbauchf1f87202016-03-30 06:43:37 -0700814bool StunAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000815 StunAddressFamily address_family = family();
816 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100817 RTC_LOG(LS_ERROR) << "Error writing address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000818 return false;
819 }
820 buf->WriteUInt8(0);
821 buf->WriteUInt8(address_family);
822 buf->WriteUInt16(address_.port());
823 switch (address_.family()) {
824 case AF_INET: {
825 in_addr v4addr = address_.ipaddr().ipv4_address();
826 buf->WriteBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr));
827 break;
828 }
829 case AF_INET6: {
830 in6_addr v6addr = address_.ipaddr().ipv6_address();
831 buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr));
832 break;
833 }
834 }
835 return true;
836}
837
Peter Boström0c4e06b2015-10-07 12:23:21 +0200838StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
839 const rtc::SocketAddress& addr)
Yves Gerey665174f2018-06-19 15:03:05 +0200840 : StunAddressAttribute(type, addr), owner_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000841
Peter Boström0c4e06b2015-10-07 12:23:21 +0200842StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
843 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000844 StunMessage* owner)
Yves Gerey665174f2018-06-19 15:03:05 +0200845 : StunAddressAttribute(type, length), owner_(owner) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000846
Steve Antonca7d54e2017-10-25 14:42:51 -0700847StunAttributeValueType StunXorAddressAttribute::value_type() const {
848 return STUN_VALUE_XOR_ADDRESS;
849}
850
851void StunXorAddressAttribute::SetOwner(StunMessage* owner) {
852 owner_ = owner;
853}
854
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000855rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const {
856 if (owner_) {
857 rtc::IPAddress ip = ipaddr();
858 switch (ip.family()) {
859 case AF_INET: {
860 in_addr v4addr = ip.ipv4_address();
861 v4addr.s_addr =
862 (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie));
863 return rtc::IPAddress(v4addr);
864 }
865 case AF_INET6: {
866 in6_addr v6addr = ip.ipv6_address();
867 const std::string& transaction_id = owner_->transaction_id();
868 if (transaction_id.length() == kStunTransactionIdLength) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200869 uint32_t transactionid_as_ints[3];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000870 memcpy(&transactionid_as_ints[0], transaction_id.c_str(),
871 transaction_id.length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200872 uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873 // Transaction ID is in network byte order, but magic cookie
874 // is stored in host byte order.
875 ip_as_ints[0] =
876 (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie));
877 ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]);
878 ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]);
879 ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]);
880 return rtc::IPAddress(v6addr);
881 }
882 break;
883 }
884 }
885 }
886 // Invalid ip family or transaction ID, or missing owner.
887 // Return an AF_UNSPEC address.
888 return rtc::IPAddress();
889}
890
jbauchf1f87202016-03-30 06:43:37 -0700891bool StunXorAddressAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000892 if (!StunAddressAttribute::Read(buf))
893 return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200894 uint16_t xoredport = port() ^ (kStunMagicCookie >> 16);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895 rtc::IPAddress xored_ip = GetXoredIP();
896 SetAddress(rtc::SocketAddress(xored_ip, xoredport));
897 return true;
898}
899
jbauchf1f87202016-03-30 06:43:37 -0700900bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000901 StunAddressFamily address_family = family();
902 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100903 RTC_LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000904 return false;
905 }
906 rtc::IPAddress xored_ip = GetXoredIP();
907 if (xored_ip.family() == AF_UNSPEC) {
908 return false;
909 }
910 buf->WriteUInt8(0);
911 buf->WriteUInt8(family());
912 buf->WriteUInt16(port() ^ (kStunMagicCookie >> 16));
913 switch (xored_ip.family()) {
914 case AF_INET: {
915 in_addr v4addr = xored_ip.ipv4_address();
916 buf->WriteBytes(reinterpret_cast<const char*>(&v4addr), sizeof(v4addr));
917 break;
918 }
919 case AF_INET6: {
920 in6_addr v6addr = xored_ip.ipv6_address();
921 buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr));
922 break;
923 }
924 }
925 return true;
926}
927
Peter Boström0c4e06b2015-10-07 12:23:21 +0200928StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200929 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000930
Peter Boström0c4e06b2015-10-07 12:23:21 +0200931StunUInt32Attribute::StunUInt32Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200932 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933
Steve Antonca7d54e2017-10-25 14:42:51 -0700934StunAttributeValueType StunUInt32Attribute::value_type() const {
935 return STUN_VALUE_UINT32;
936}
937
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000938bool StunUInt32Attribute::GetBit(size_t index) const {
nisseede5da42017-01-12 05:15:36 -0800939 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000940 return static_cast<bool>((bits_ >> index) & 0x1);
941}
942
943void StunUInt32Attribute::SetBit(size_t index, bool value) {
nisseede5da42017-01-12 05:15:36 -0800944 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000945 bits_ &= ~(1 << index);
946 bits_ |= value ? (1 << index) : 0;
947}
948
jbauchf1f87202016-03-30 06:43:37 -0700949bool StunUInt32Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000950 if (length() != SIZE || !buf->ReadUInt32(&bits_))
951 return false;
952 return true;
953}
954
jbauchf1f87202016-03-30 06:43:37 -0700955bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000956 buf->WriteUInt32(bits_);
957 return true;
958}
959
Peter Boström0c4e06b2015-10-07 12:23:21 +0200960StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200961 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000962
Peter Boström0c4e06b2015-10-07 12:23:21 +0200963StunUInt64Attribute::StunUInt64Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200964 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000965
Steve Antonca7d54e2017-10-25 14:42:51 -0700966StunAttributeValueType StunUInt64Attribute::value_type() const {
967 return STUN_VALUE_UINT64;
968}
969
jbauchf1f87202016-03-30 06:43:37 -0700970bool StunUInt64Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000971 if (length() != SIZE || !buf->ReadUInt64(&bits_))
972 return false;
973 return true;
974}
975
jbauchf1f87202016-03-30 06:43:37 -0700976bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000977 buf->WriteUInt64(bits_);
978 return true;
979}
980
Peter Boström0c4e06b2015-10-07 12:23:21 +0200981StunByteStringAttribute::StunByteStringAttribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200982 : StunAttribute(type, 0), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000983
Peter Boström0c4e06b2015-10-07 12:23:21 +0200984StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000985 const std::string& str)
986 : StunAttribute(type, 0), bytes_(NULL) {
987 CopyBytes(str.c_str(), str.size());
988}
989
Peter Boström0c4e06b2015-10-07 12:23:21 +0200990StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 const void* bytes,
992 size_t length)
993 : StunAttribute(type, 0), bytes_(NULL) {
994 CopyBytes(bytes, length);
995}
996
Peter Boström0c4e06b2015-10-07 12:23:21 +0200997StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200998 : StunAttribute(type, length), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000999
1000StunByteStringAttribute::~StunByteStringAttribute() {
Yves Gerey665174f2018-06-19 15:03:05 +02001001 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001002}
1003
Steve Antonca7d54e2017-10-25 14:42:51 -07001004StunAttributeValueType StunByteStringAttribute::value_type() const {
1005 return STUN_VALUE_BYTE_STRING;
1006}
1007
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001008void StunByteStringAttribute::CopyBytes(const char* bytes) {
1009 CopyBytes(bytes, strlen(bytes));
1010}
1011
1012void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) {
1013 char* new_bytes = new char[length];
1014 memcpy(new_bytes, bytes, length);
1015 SetBytes(new_bytes, length);
1016}
1017
Peter Boström0c4e06b2015-10-07 12:23:21 +02001018uint8_t StunByteStringAttribute::GetByte(size_t index) const {
nisseede5da42017-01-12 05:15:36 -08001019 RTC_DCHECK(bytes_ != NULL);
1020 RTC_DCHECK(index < length());
Peter Boström0c4e06b2015-10-07 12:23:21 +02001021 return static_cast<uint8_t>(bytes_[index]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001022}
1023
Peter Boström0c4e06b2015-10-07 12:23:21 +02001024void StunByteStringAttribute::SetByte(size_t index, uint8_t value) {
nisseede5da42017-01-12 05:15:36 -08001025 RTC_DCHECK(bytes_ != NULL);
1026 RTC_DCHECK(index < length());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001027 bytes_[index] = value;
1028}
1029
jbauchf1f87202016-03-30 06:43:37 -07001030bool StunByteStringAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001031 bytes_ = new char[length()];
1032 if (!buf->ReadBytes(bytes_, length())) {
1033 return false;
1034 }
1035
1036 ConsumePadding(buf);
1037 return true;
1038}
1039
jbauchf1f87202016-03-30 06:43:37 -07001040bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const {
Harald Alvestrandbee64082020-11-12 11:17:41 +00001041 // Check that length is legal according to specs
1042 if (!LengthValid(type(), length())) {
1043 return false;
1044 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001045 buf->WriteBytes(bytes_, length());
1046 WritePadding(buf);
1047 return true;
1048}
1049
1050void StunByteStringAttribute::SetBytes(char* bytes, size_t length) {
Yves Gerey665174f2018-06-19 15:03:05 +02001051 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001052 bytes_ = bytes;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001053 SetLength(static_cast<uint16_t>(length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001054}
1055
zsteinf42cc9d2017-03-27 16:17:19 -07001056const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4;
1057
Peter Boström0c4e06b2015-10-07 12:23:21 +02001058StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type,
1059 int code,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001060 const std::string& reason)
1061 : StunAttribute(type, 0) {
1062 SetCode(code);
1063 SetReason(reason);
1064}
1065
Peter Boström0c4e06b2015-10-07 12:23:21 +02001066StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +02001067 : StunAttribute(type, length), class_(0), number_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001068
Yves Gerey665174f2018-06-19 15:03:05 +02001069StunErrorCodeAttribute::~StunErrorCodeAttribute() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001070
Steve Antonca7d54e2017-10-25 14:42:51 -07001071StunAttributeValueType StunErrorCodeAttribute::value_type() const {
1072 return STUN_VALUE_ERROR_CODE;
1073}
1074
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001075int StunErrorCodeAttribute::code() const {
1076 return class_ * 100 + number_;
1077}
1078
1079void StunErrorCodeAttribute::SetCode(int code) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001080 class_ = static_cast<uint8_t>(code / 100);
1081 number_ = static_cast<uint8_t>(code % 100);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001082}
1083
1084void StunErrorCodeAttribute::SetReason(const std::string& reason) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001085 SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001086 reason_ = reason;
1087}
1088
jbauchf1f87202016-03-30 06:43:37 -07001089bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001090 uint32_t val;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001091 if (length() < MIN_SIZE || !buf->ReadUInt32(&val))
1092 return false;
1093
1094 if ((val >> 11) != 0)
Mirko Bonadei675513b2017-11-09 11:09:25 +01001095 RTC_LOG(LS_ERROR) << "error-code bits not zero";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001096
1097 class_ = ((val >> 8) & 0x7);
1098 number_ = (val & 0xff);
1099
1100 if (!buf->ReadString(&reason_, length() - 4))
1101 return false;
1102
1103 ConsumePadding(buf);
1104 return true;
1105}
1106
jbauchf1f87202016-03-30 06:43:37 -07001107bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001108 buf->WriteUInt32(class_ << 8 | number_);
1109 buf->WriteString(reason_);
1110 WritePadding(buf);
1111 return true;
1112}
1113
Peter Boström0c4e06b2015-10-07 12:23:21 +02001114StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001115 : StunAttribute(type, length) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001116 attr_types_ = new std::vector<uint16_t>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001117}
1118
1119StunUInt16ListAttribute::~StunUInt16ListAttribute() {
1120 delete attr_types_;
1121}
1122
Steve Antonca7d54e2017-10-25 14:42:51 -07001123StunAttributeValueType StunUInt16ListAttribute::value_type() const {
1124 return STUN_VALUE_UINT16_LIST;
1125}
1126
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001127size_t StunUInt16ListAttribute::Size() const {
1128 return attr_types_->size();
1129}
1130
Peter Boström0c4e06b2015-10-07 12:23:21 +02001131uint16_t StunUInt16ListAttribute::GetType(int index) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001132 return (*attr_types_)[index];
1133}
1134
Peter Boström0c4e06b2015-10-07 12:23:21 +02001135void StunUInt16ListAttribute::SetType(int index, uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001136 (*attr_types_)[index] = value;
1137}
1138
Peter Boström0c4e06b2015-10-07 12:23:21 +02001139void StunUInt16ListAttribute::AddType(uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 attr_types_->push_back(value);
Peter Boström0c4e06b2015-10-07 12:23:21 +02001141 SetLength(static_cast<uint16_t>(attr_types_->size() * 2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001142}
1143
Jonas Oreland63737a92019-11-21 15:12:14 +01001144void StunUInt16ListAttribute::AddTypeAtIndex(uint16_t index, uint16_t value) {
1145 if (attr_types_->size() < static_cast<size_t>(index + 1)) {
1146 attr_types_->resize(index + 1);
1147 }
1148 (*attr_types_)[index] = value;
1149 SetLength(static_cast<uint16_t>(attr_types_->size() * 2));
1150}
1151
jbauchf1f87202016-03-30 06:43:37 -07001152bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) {
Jonas Oreland1721de12019-11-20 12:10:39 +01001153 if (length() % 2) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 return false;
Jonas Oreland1721de12019-11-20 12:10:39 +01001155 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156
1157 for (size_t i = 0; i < length() / 2; i++) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001158 uint16_t attr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 if (!buf->ReadUInt16(&attr))
1160 return false;
1161 attr_types_->push_back(attr);
1162 }
1163 // Padding of these attributes is done in RFC 5389 style. This is
1164 // slightly different from RFC3489, but it shouldn't be important.
1165 // RFC3489 pads out to a 32 bit boundary by duplicating one of the
1166 // entries in the list (not necessarily the last one - it's unspecified).
1167 // RFC5389 pads on the end, and the bytes are always ignored.
1168 ConsumePadding(buf);
1169 return true;
1170}
1171
jbauchf1f87202016-03-30 06:43:37 -07001172bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001173 for (size_t i = 0; i < attr_types_->size(); ++i) {
1174 buf->WriteUInt16((*attr_types_)[i]);
1175 }
1176 WritePadding(buf);
1177 return true;
1178}
1179
Jonas Oreland9a52bd72019-12-11 11:35:48 +01001180std::string StunMethodToString(int msg_type) {
1181 switch (msg_type) {
1182 case STUN_BINDING_REQUEST:
1183 return "STUN BINDING request";
1184 case STUN_BINDING_INDICATION:
1185 return "STUN BINDING indication";
1186 case STUN_BINDING_RESPONSE:
1187 return "STUN BINDING response";
1188 case STUN_BINDING_ERROR_RESPONSE:
1189 return "STUN BINDING error response";
1190 case GOOG_PING_REQUEST:
1191 return "GOOG PING request";
1192 case GOOG_PING_RESPONSE:
1193 return "GOOG PING response";
1194 case GOOG_PING_ERROR_RESPONSE:
1195 return "GOOG PING error response";
1196 case STUN_ALLOCATE_REQUEST:
1197 return "TURN ALLOCATE request";
1198 case STUN_ALLOCATE_RESPONSE:
1199 return "TURN ALLOCATE response";
1200 case STUN_ALLOCATE_ERROR_RESPONSE:
1201 return "TURN ALLOCATE error response";
1202 case TURN_REFRESH_REQUEST:
1203 return "TURN REFRESH request";
1204 case TURN_REFRESH_RESPONSE:
1205 return "TURN REFRESH response";
1206 case TURN_REFRESH_ERROR_RESPONSE:
1207 return "TURN REFRESH error response";
1208 case TURN_SEND_INDICATION:
1209 return "TURN SEND INDICATION";
1210 case TURN_DATA_INDICATION:
1211 return "TURN DATA INDICATION";
1212 case TURN_CREATE_PERMISSION_REQUEST:
1213 return "TURN CREATE PERMISSION request";
1214 case TURN_CREATE_PERMISSION_RESPONSE:
1215 return "TURN CREATE PERMISSION response";
1216 case TURN_CREATE_PERMISSION_ERROR_RESPONSE:
1217 return "TURN CREATE PERMISSION error response";
1218 case TURN_CHANNEL_BIND_REQUEST:
1219 return "TURN CHANNEL BIND request";
1220 case TURN_CHANNEL_BIND_RESPONSE:
1221 return "TURN CHANNEL BIND response";
1222 case TURN_CHANNEL_BIND_ERROR_RESPONSE:
1223 return "TURN CHANNEL BIND error response";
1224 default:
1225 return "UNKNOWN<" + std::to_string(msg_type) + ">";
1226 }
1227}
1228
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001229int GetStunSuccessResponseType(int req_type) {
1230 return IsStunRequestType(req_type) ? (req_type | 0x100) : -1;
1231}
1232
1233int GetStunErrorResponseType(int req_type) {
1234 return IsStunRequestType(req_type) ? (req_type | 0x110) : -1;
1235}
1236
1237bool IsStunRequestType(int msg_type) {
1238 return ((msg_type & kStunTypeMask) == 0x000);
1239}
1240
1241bool IsStunIndicationType(int msg_type) {
1242 return ((msg_type & kStunTypeMask) == 0x010);
1243}
1244
1245bool IsStunSuccessResponseType(int msg_type) {
1246 return ((msg_type & kStunTypeMask) == 0x100);
1247}
1248
1249bool IsStunErrorResponseType(int msg_type) {
1250 return ((msg_type & kStunTypeMask) == 0x110);
1251}
1252
1253bool ComputeStunCredentialHash(const std::string& username,
1254 const std::string& realm,
1255 const std::string& password,
1256 std::string* hash) {
1257 // http://tools.ietf.org/html/rfc5389#section-15.4
1258 // long-term credentials will be calculated using the key and key is
1259 // key = MD5(username ":" realm ":" SASLprep(password))
1260 std::string input = username;
1261 input += ':';
1262 input += realm;
1263 input += ':';
1264 input += password;
1265
1266 char digest[rtc::MessageDigest::kMaxSize];
Yves Gerey665174f2018-06-19 15:03:05 +02001267 size_t size = rtc::ComputeDigest(rtc::DIGEST_MD5, input.c_str(), input.size(),
1268 digest, sizeof(digest));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001269 if (size == 0) {
1270 return false;
1271 }
1272
1273 *hash = std::string(digest, size);
1274 return true;
1275}
1276
Jonas Oreland202994c2017-12-18 12:10:43 +01001277std::unique_ptr<StunAttribute> CopyStunAttribute(
1278 const StunAttribute& attribute,
1279 rtc::ByteBufferWriter* tmp_buffer_ptr) {
1280 ByteBufferWriter tmpBuffer;
1281 if (tmp_buffer_ptr == nullptr) {
1282 tmp_buffer_ptr = &tmpBuffer;
1283 }
1284
Yves Gerey665174f2018-06-19 15:03:05 +02001285 std::unique_ptr<StunAttribute> copy(StunAttribute::Create(
1286 attribute.value_type(), attribute.type(),
1287 static_cast<uint16_t>(attribute.length()), nullptr));
Jonas Oreland202994c2017-12-18 12:10:43 +01001288
1289 if (!copy) {
1290 return nullptr;
1291 }
1292 tmp_buffer_ptr->Clear();
1293 if (!attribute.Write(tmp_buffer_ptr)) {
1294 return nullptr;
1295 }
1296 rtc::ByteBufferReader reader(*tmp_buffer_ptr);
1297 if (!copy->Read(&reader)) {
1298 return nullptr;
1299 }
1300
1301 return copy;
1302}
1303
Steve Antonca7d54e2017-10-25 14:42:51 -07001304StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const {
1305 switch (type) {
1306 case STUN_ATTR_LIFETIME:
1307 return STUN_VALUE_UINT32;
1308 case STUN_ATTR_MAGIC_COOKIE:
1309 return STUN_VALUE_BYTE_STRING;
1310 case STUN_ATTR_BANDWIDTH:
1311 return STUN_VALUE_UINT32;
1312 case STUN_ATTR_DESTINATION_ADDRESS:
1313 return STUN_VALUE_ADDRESS;
1314 case STUN_ATTR_SOURCE_ADDRESS2:
1315 return STUN_VALUE_ADDRESS;
1316 case STUN_ATTR_DATA:
1317 return STUN_VALUE_BYTE_STRING;
1318 case STUN_ATTR_OPTIONS:
1319 return STUN_VALUE_UINT32;
1320 default:
1321 return StunMessage::GetAttributeValueType(type);
1322 }
1323}
1324
1325StunMessage* RelayMessage::CreateNew() const {
1326 return new RelayMessage();
1327}
1328
1329StunAttributeValueType TurnMessage::GetAttributeValueType(int type) const {
1330 switch (type) {
1331 case STUN_ATTR_CHANNEL_NUMBER:
1332 return STUN_VALUE_UINT32;
1333 case STUN_ATTR_TURN_LIFETIME:
1334 return STUN_VALUE_UINT32;
1335 case STUN_ATTR_XOR_PEER_ADDRESS:
1336 return STUN_VALUE_XOR_ADDRESS;
1337 case STUN_ATTR_DATA:
1338 return STUN_VALUE_BYTE_STRING;
1339 case STUN_ATTR_XOR_RELAYED_ADDRESS:
1340 return STUN_VALUE_XOR_ADDRESS;
1341 case STUN_ATTR_EVEN_PORT:
1342 return STUN_VALUE_BYTE_STRING;
1343 case STUN_ATTR_REQUESTED_TRANSPORT:
1344 return STUN_VALUE_UINT32;
1345 case STUN_ATTR_DONT_FRAGMENT:
1346 return STUN_VALUE_BYTE_STRING;
1347 case STUN_ATTR_RESERVATION_TOKEN:
1348 return STUN_VALUE_BYTE_STRING;
1349 default:
1350 return StunMessage::GetAttributeValueType(type);
1351 }
1352}
1353
1354StunMessage* TurnMessage::CreateNew() const {
1355 return new TurnMessage();
1356}
1357
1358StunAttributeValueType IceMessage::GetAttributeValueType(int type) const {
1359 switch (type) {
1360 case STUN_ATTR_PRIORITY:
Jonas Orelandfa543642020-09-16 10:44:54 +02001361 case STUN_ATTR_GOOG_NETWORK_INFO:
Steve Antonca7d54e2017-10-25 14:42:51 -07001362 case STUN_ATTR_NOMINATION:
1363 return STUN_VALUE_UINT32;
1364 case STUN_ATTR_USE_CANDIDATE:
1365 return STUN_VALUE_BYTE_STRING;
1366 case STUN_ATTR_ICE_CONTROLLED:
1367 return STUN_VALUE_UINT64;
1368 case STUN_ATTR_ICE_CONTROLLING:
1369 return STUN_VALUE_UINT64;
1370 default:
1371 return StunMessage::GetAttributeValueType(type);
1372 }
1373}
1374
1375StunMessage* IceMessage::CreateNew() const {
1376 return new IceMessage();
1377}
1378
Jonas Oreland253d50f2019-11-28 17:08:07 +01001379std::unique_ptr<StunMessage> StunMessage::Clone() const {
1380 std::unique_ptr<StunMessage> copy(CreateNew());
1381 if (!copy) {
1382 return nullptr;
1383 }
1384 rtc::ByteBufferWriter buf;
1385 if (!Write(&buf)) {
1386 return nullptr;
1387 }
1388 rtc::ByteBufferReader reader(buf);
1389 if (!copy->Read(&reader)) {
1390 return nullptr;
1391 }
1392 return copy;
1393}
1394
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001395} // namespace cricket