blob: 33b444884d7ddad6b72f200595f580683fe2b5b1 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "p2p/base/stun.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012
13#include <string.h>
14
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070016#include <memory>
Steve Anton6c38cc72017-11-29 10:25:58 -080017#include <utility>
kwiberg3ec46792016-04-27 07:22:53 -070018
Karl Wiberg918f50c2018-07-05 11:40:33 +020019#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/byteorder.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/crc32.h"
23#include "rtc_base/logging.h"
24#include "rtc_base/messagedigest.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
Zach Stein92c42892018-11-28 11:38:52 -080029namespace {
30
31uint32_t ReduceTransactionId(const std::string& transaction_id) {
32 RTC_DCHECK(transaction_id.length() == cricket::kStunTransactionIdLength ||
33 transaction_id.length() ==
34 cricket::kStunLegacyTransactionIdLength);
35 uint32_t transaction_id_as_ints[4];
36 memcpy(transaction_id_as_ints, transaction_id.c_str(),
37 transaction_id.length());
38
39 uint32_t result = 0;
40 for (size_t i = 0; i < transaction_id.length() / 4; ++i) {
41 result ^= transaction_id_as_ints[i];
42 }
43 return result;
44}
45
46} // namespace
47
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048namespace cricket {
49
50const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server";
51const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request";
52const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized";
53const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden";
54const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials";
55const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch";
56const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce";
57const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials";
58const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol";
59const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict";
60const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error";
61
Yves Gerey665174f2018-06-19 15:03:05 +020062const char TURN_MAGIC_COOKIE_VALUE[] = {'\x72', '\xC6', '\x4B', '\xC6'};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063const char EMPTY_TRANSACTION_ID[] = "0000000000000000";
Peter Boström0c4e06b2015-10-07 12:23:21 +020064const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065
66// StunMessage
67
68StunMessage::StunMessage()
69 : type_(0),
70 length_(0),
Jonas Oreland7ca63112018-02-27 08:45:13 +010071 transaction_id_(EMPTY_TRANSACTION_ID),
72 stun_magic_cookie_(kStunMagicCookie) {
nisseede5da42017-01-12 05:15:36 -080073 RTC_DCHECK(IsValidTransactionId(transaction_id_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074}
75
Steve Antonca7d54e2017-10-25 14:42:51 -070076StunMessage::~StunMessage() = default;
77
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078bool StunMessage::IsLegacy() const {
79 if (transaction_id_.size() == kStunLegacyTransactionIdLength)
80 return true;
nisseede5da42017-01-12 05:15:36 -080081 RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 return false;
83}
84
85bool StunMessage::SetTransactionID(const std::string& str) {
86 if (!IsValidTransactionId(str)) {
87 return false;
88 }
89 transaction_id_ = str;
Zach Stein92c42892018-11-28 11:38:52 -080090 reduced_transaction_id_ = ReduceTransactionId(transaction_id_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091 return true;
92}
93
Jonas Oreland16ccef72018-03-27 09:02:43 +020094static bool DesignatedExpertRange(int attr_type) {
Yves Gerey665174f2018-06-19 15:03:05 +020095 return (attr_type >= 0x4000 && attr_type <= 0x7FFF) ||
96 (attr_type >= 0xC000 && attr_type <= 0xFFFF);
Jonas Orelandbdcee282017-10-10 14:01:40 +020097}
98
zsteinf42cc9d2017-03-27 16:17:19 -070099void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200100 // Fail any attributes that aren't valid for this type of message,
Jonas Oreland16ccef72018-03-27 09:02:43 +0200101 // but allow any type for the range that in the RFC is reserved for
102 // the "designated experts".
103 if (!DesignatedExpertRange(attr->type())) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200104 RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type()));
105 }
nissecc99bc22017-02-02 01:31:30 -0800106
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 attr->SetOwner(this);
108 size_t attr_length = attr->length();
109 if (attr_length % 4 != 0) {
110 attr_length += (4 - (attr_length % 4));
111 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200112 length_ += static_cast<uint16_t>(attr_length + 4);
zsteinf42cc9d2017-03-27 16:17:19 -0700113
114 attrs_.push_back(std::move(attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115}
116
Jonas Oreland202994c2017-12-18 12:10:43 +0100117std::unique_ptr<StunAttribute> StunMessage::RemoveAttribute(int type) {
118 std::unique_ptr<StunAttribute> attribute;
119 for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) {
Yves Gerey665174f2018-06-19 15:03:05 +0200120 if ((*it)->type() == type) {
121 attribute = std::move(*it);
Jonas Oreland202994c2017-12-18 12:10:43 +0100122 attrs_.erase(std::next(it).base());
123 break;
124 }
125 }
126 if (attribute) {
127 attribute->SetOwner(nullptr);
128 size_t attr_length = attribute->length();
129 if (attr_length % 4 != 0) {
130 attr_length += (4 - (attr_length % 4));
131 }
132 length_ -= static_cast<uint16_t>(attr_length + 4);
133 }
134 return attribute;
135}
136
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137const StunAddressAttribute* StunMessage::GetAddress(int type) const {
138 switch (type) {
139 case STUN_ATTR_MAPPED_ADDRESS: {
140 // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is
141 // missing.
142 const StunAttribute* mapped_address =
143 GetAttribute(STUN_ATTR_MAPPED_ADDRESS);
144 if (!mapped_address)
145 mapped_address = GetAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS);
146 return reinterpret_cast<const StunAddressAttribute*>(mapped_address);
147 }
148
149 default:
150 return static_cast<const StunAddressAttribute*>(GetAttribute(type));
151 }
152}
153
154const StunUInt32Attribute* StunMessage::GetUInt32(int type) const {
155 return static_cast<const StunUInt32Attribute*>(GetAttribute(type));
156}
157
158const StunUInt64Attribute* StunMessage::GetUInt64(int type) const {
159 return static_cast<const StunUInt64Attribute*>(GetAttribute(type));
160}
161
162const StunByteStringAttribute* StunMessage::GetByteString(int type) const {
163 return static_cast<const StunByteStringAttribute*>(GetAttribute(type));
164}
165
166const StunErrorCodeAttribute* StunMessage::GetErrorCode() const {
167 return static_cast<const StunErrorCodeAttribute*>(
168 GetAttribute(STUN_ATTR_ERROR_CODE));
169}
170
deadbeef996fc6b2017-04-26 09:21:22 -0700171int StunMessage::GetErrorCodeValue() const {
172 const StunErrorCodeAttribute* error_attribute = GetErrorCode();
173 return error_attribute ? error_attribute->code() : STUN_ERROR_GLOBAL_FAILURE;
174}
175
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176const StunUInt16ListAttribute* StunMessage::GetUnknownAttributes() const {
177 return static_cast<const StunUInt16ListAttribute*>(
178 GetAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES));
179}
180
181// Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the
182// procedure outlined in RFC 5389, section 15.4.
Yves Gerey665174f2018-06-19 15:03:05 +0200183bool StunMessage::ValidateMessageIntegrity(const char* data,
184 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 const std::string& password) {
186 // Verifying the size of the message.
katrielce4bda242016-06-09 08:45:45 -0700187 if ((size % 4) != 0 || size < kStunHeaderSize) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188 return false;
189 }
190
191 // Getting the message length from the STUN header.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200192 uint16_t msg_length = rtc::GetBE16(&data[2]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 if (size != (msg_length + kStunHeaderSize)) {
194 return false;
195 }
196
197 // Finding Message Integrity attribute in stun message.
198 size_t current_pos = kStunHeaderSize;
199 bool has_message_integrity_attr = false;
katrielc1a206102016-06-20 05:13:16 -0700200 while (current_pos + 4 <= size) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200201 uint16_t attr_type, attr_length;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 // Getting attribute type and length.
203 attr_type = rtc::GetBE16(&data[current_pos]);
204 attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]);
205
206 // If M-I, sanity check it, and break out.
207 if (attr_type == STUN_ATTR_MESSAGE_INTEGRITY) {
208 if (attr_length != kStunMessageIntegritySize ||
katrielc1a206102016-06-20 05:13:16 -0700209 current_pos + sizeof(attr_type) + sizeof(attr_length) + attr_length >
210 size) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211 return false;
212 }
213 has_message_integrity_attr = true;
214 break;
215 }
216
217 // Otherwise, skip to the next attribute.
218 current_pos += sizeof(attr_type) + sizeof(attr_length) + attr_length;
219 if ((attr_length % 4) != 0) {
220 current_pos += (4 - (attr_length % 4));
221 }
222 }
223
224 if (!has_message_integrity_attr) {
225 return false;
226 }
227
228 // Getting length of the message to calculate Message Integrity.
229 size_t mi_pos = current_pos;
kwiberg3ec46792016-04-27 07:22:53 -0700230 std::unique_ptr<char[]> temp_data(new char[current_pos]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231 memcpy(temp_data.get(), data, current_pos);
232 if (size > mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize) {
233 // Stun message has other attributes after message integrity.
234 // Adjust the length parameter in stun message to calculate HMAC.
Yves Gerey665174f2018-06-19 15:03:05 +0200235 size_t extra_offset =
236 size - (mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000237 size_t new_adjusted_len = size - extra_offset - kStunHeaderSize;
238
239 // Writing new length of the STUN message @ Message Length in temp buffer.
240 // 0 1 2 3
241 // 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
242 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
243 // |0 0| STUN Message Type | Message Length |
244 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Peter Boström0c4e06b2015-10-07 12:23:21 +0200245 rtc::SetBE16(temp_data.get() + 2, static_cast<uint16_t>(new_adjusted_len));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 }
247
248 char hmac[kStunMessageIntegritySize];
Yves Gerey665174f2018-06-19 15:03:05 +0200249 size_t ret =
250 rtc::ComputeHmac(rtc::DIGEST_SHA_1, password.c_str(), password.size(),
251 temp_data.get(), mi_pos, hmac, sizeof(hmac));
nisseede5da42017-01-12 05:15:36 -0800252 RTC_DCHECK(ret == sizeof(hmac));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253 if (ret != sizeof(hmac))
254 return false;
255
256 // Comparing the calculated HMAC with the one present in the message.
Yves Gerey665174f2018-06-19 15:03:05 +0200257 return memcmp(data + current_pos + kStunAttributeHeaderSize, hmac,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000258 sizeof(hmac)) == 0;
259}
260
261bool StunMessage::AddMessageIntegrity(const std::string& password) {
262 return AddMessageIntegrity(password.c_str(), password.size());
263}
264
Yves Gerey665174f2018-06-19 15:03:05 +0200265bool StunMessage::AddMessageIntegrity(const char* key, size_t keylen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000266 // Add the attribute with a dummy value. Since this is a known attribute, it
267 // can't fail.
Karl Wiberg918f50c2018-07-05 11:40:33 +0200268 auto msg_integrity_attr_ptr = absl::make_unique<StunByteStringAttribute>(
zsteinf42cc9d2017-03-27 16:17:19 -0700269 STUN_ATTR_MESSAGE_INTEGRITY, std::string(kStunMessageIntegritySize, '0'));
270 auto* msg_integrity_attr = msg_integrity_attr_ptr.get();
271 AddAttribute(std::move(msg_integrity_attr_ptr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272
273 // Calculate the HMAC for the message.
jbauchf1f87202016-03-30 06:43:37 -0700274 ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 if (!Write(&buf))
276 return false;
277
278 int msg_len_for_hmac = static_cast<int>(
279 buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length());
280 char hmac[kStunMessageIntegritySize];
Yves Gerey665174f2018-06-19 15:03:05 +0200281 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, key, keylen, buf.Data(),
282 msg_len_for_hmac, hmac, sizeof(hmac));
nisseede5da42017-01-12 05:15:36 -0800283 RTC_DCHECK(ret == sizeof(hmac));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000284 if (ret != sizeof(hmac)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100285 RTC_LOG(LS_ERROR) << "HMAC computation failed. Message-Integrity "
Jonas Olssond7d762d2018-03-28 09:47:51 +0200286 "has dummy value.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000287 return false;
288 }
289
290 // Insert correct HMAC into the attribute.
291 msg_integrity_attr->CopyBytes(hmac, sizeof(hmac));
292 return true;
293}
294
295// Verifies a message is in fact a STUN message, by performing the checks
296// outlined in RFC 5389, section 7.3, including the FINGERPRINT check detailed
297// in section 15.5.
298bool StunMessage::ValidateFingerprint(const char* data, size_t size) {
299 // Check the message length.
300 size_t fingerprint_attr_size =
301 kStunAttributeHeaderSize + StunUInt32Attribute::SIZE;
302 if (size % 4 != 0 || size < kStunHeaderSize + fingerprint_attr_size)
303 return false;
304
305 // Skip the rest if the magic cookie isn't present.
306 const char* magic_cookie =
307 data + kStunTransactionIdOffset - kStunMagicCookieLength;
308 if (rtc::GetBE32(magic_cookie) != kStunMagicCookie)
309 return false;
310
311 // Check the fingerprint type and length.
312 const char* fingerprint_attr_data = data + size - fingerprint_attr_size;
313 if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT ||
Peter Boström0c4e06b2015-10-07 12:23:21 +0200314 rtc::GetBE16(fingerprint_attr_data + sizeof(uint16_t)) !=
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000315 StunUInt32Attribute::SIZE)
316 return false;
317
318 // Check the fingerprint value.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200319 uint32_t fingerprint =
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000320 rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize);
321 return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) ==
Yves Gerey665174f2018-06-19 15:03:05 +0200322 rtc::ComputeCrc32(data, size - fingerprint_attr_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323}
324
325bool StunMessage::AddFingerprint() {
326 // Add the attribute with a dummy value. Since this is a known attribute,
327 // it can't fail.
zsteinf42cc9d2017-03-27 16:17:19 -0700328 auto fingerprint_attr_ptr =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200329 absl::make_unique<StunUInt32Attribute>(STUN_ATTR_FINGERPRINT, 0);
Steve Antonca7d54e2017-10-25 14:42:51 -0700330 auto* fingerprint_attr = fingerprint_attr_ptr.get();
zsteinf42cc9d2017-03-27 16:17:19 -0700331 AddAttribute(std::move(fingerprint_attr_ptr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000332
333 // Calculate the CRC-32 for the message and insert it.
jbauchf1f87202016-03-30 06:43:37 -0700334 ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000335 if (!Write(&buf))
336 return false;
337
338 int msg_len_for_crc32 = static_cast<int>(
339 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200340 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341
342 // Insert the correct CRC-32, XORed with a constant, into the attribute.
343 fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE);
344 return true;
345}
346
jbauchf1f87202016-03-30 06:43:37 -0700347bool StunMessage::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000348 if (!buf->ReadUInt16(&type_))
349 return false;
350
351 if (type_ & 0x8000) {
352 // RTP and RTCP set the MSB of first byte, since first two bits are version,
353 // and version is always 2 (10). If set, this is not a STUN packet.
354 return false;
355 }
356
357 if (!buf->ReadUInt16(&length_))
358 return false;
359
360 std::string magic_cookie;
361 if (!buf->ReadString(&magic_cookie, kStunMagicCookieLength))
362 return false;
363
364 std::string transaction_id;
365 if (!buf->ReadString(&transaction_id, kStunTransactionIdLength))
366 return false;
367
Peter Boström0c4e06b2015-10-07 12:23:21 +0200368 uint32_t magic_cookie_int =
369 *reinterpret_cast<const uint32_t*>(magic_cookie.data());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000370 if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) {
371 // If magic cookie is invalid it means that the peer implements
372 // RFC3489 instead of RFC5389.
373 transaction_id.insert(0, magic_cookie);
374 }
nisseede5da42017-01-12 05:15:36 -0800375 RTC_DCHECK(IsValidTransactionId(transaction_id));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000376 transaction_id_ = transaction_id;
Zach Stein92c42892018-11-28 11:38:52 -0800377 reduced_transaction_id_ = ReduceTransactionId(transaction_id_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378
379 if (length_ != buf->Length())
380 return false;
381
zsteinad94c4c2017-03-06 13:36:05 -0800382 attrs_.resize(0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000383
384 size_t rest = buf->Length() - length_;
385 while (buf->Length() > rest) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200386 uint16_t attr_type, attr_length;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000387 if (!buf->ReadUInt16(&attr_type))
388 return false;
389 if (!buf->ReadUInt16(&attr_length))
390 return false;
391
Honghai Zhang3e024302016-09-22 09:52:16 -0700392 std::unique_ptr<StunAttribute> attr(
393 CreateAttribute(attr_type, attr_length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 if (!attr) {
395 // Skip any unknown or malformed attributes.
396 if ((attr_length % 4) != 0) {
397 attr_length += (4 - (attr_length % 4));
398 }
399 if (!buf->Consume(attr_length))
400 return false;
401 } else {
402 if (!attr->Read(buf))
403 return false;
zsteinad94c4c2017-03-06 13:36:05 -0800404 attrs_.push_back(std::move(attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000405 }
406 }
407
nisseede5da42017-01-12 05:15:36 -0800408 RTC_DCHECK(buf->Length() == rest);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000409 return true;
410}
411
jbauchf1f87202016-03-30 06:43:37 -0700412bool StunMessage::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000413 buf->WriteUInt16(type_);
414 buf->WriteUInt16(length_);
415 if (!IsLegacy())
Jonas Oreland7ca63112018-02-27 08:45:13 +0100416 buf->WriteUInt32(stun_magic_cookie_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000417 buf->WriteString(transaction_id_);
418
zsteinad94c4c2017-03-06 13:36:05 -0800419 for (const auto& attr : attrs_) {
420 buf->WriteUInt16(attr->type());
421 buf->WriteUInt16(static_cast<uint16_t>(attr->length()));
422 if (!attr->Write(buf)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000423 return false;
zsteinad94c4c2017-03-06 13:36:05 -0800424 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000425 }
426
427 return true;
428}
429
Steve Antonca7d54e2017-10-25 14:42:51 -0700430StunMessage* StunMessage::CreateNew() const {
431 return new StunMessage();
432}
433
Jonas Oreland7ca63112018-02-27 08:45:13 +0100434void StunMessage::SetStunMagicCookie(uint32_t val) {
435 stun_magic_cookie_ = val;
436}
437
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000438StunAttributeValueType StunMessage::GetAttributeValueType(int type) const {
439 switch (type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200440 case STUN_ATTR_MAPPED_ADDRESS:
441 return STUN_VALUE_ADDRESS;
442 case STUN_ATTR_USERNAME:
443 return STUN_VALUE_BYTE_STRING;
444 case STUN_ATTR_MESSAGE_INTEGRITY:
445 return STUN_VALUE_BYTE_STRING;
446 case STUN_ATTR_ERROR_CODE:
447 return STUN_VALUE_ERROR_CODE;
448 case STUN_ATTR_UNKNOWN_ATTRIBUTES:
449 return STUN_VALUE_UINT16_LIST;
450 case STUN_ATTR_REALM:
451 return STUN_VALUE_BYTE_STRING;
452 case STUN_ATTR_NONCE:
453 return STUN_VALUE_BYTE_STRING;
454 case STUN_ATTR_XOR_MAPPED_ADDRESS:
455 return STUN_VALUE_XOR_ADDRESS;
456 case STUN_ATTR_SOFTWARE:
457 return STUN_VALUE_BYTE_STRING;
458 case STUN_ATTR_ALTERNATE_SERVER:
459 return STUN_VALUE_ADDRESS;
460 case STUN_ATTR_FINGERPRINT:
461 return STUN_VALUE_UINT32;
462 case STUN_ATTR_ORIGIN:
463 return STUN_VALUE_BYTE_STRING;
464 case STUN_ATTR_RETRANSMIT_COUNT:
465 return STUN_VALUE_UINT32;
466 default:
467 return STUN_VALUE_UNKNOWN;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000468 }
469}
470
471StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ {
472 StunAttributeValueType value_type = GetAttributeValueType(type);
Jonas Orelandbdcee282017-10-10 14:01:40 +0200473 if (value_type != STUN_VALUE_UNKNOWN) {
474 return StunAttribute::Create(value_type, type,
475 static_cast<uint16_t>(length), this);
Jonas Oreland16ccef72018-03-27 09:02:43 +0200476 } else if (DesignatedExpertRange(type)) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200477 // Read unknown attributes as STUN_VALUE_BYTE_STRING
478 return StunAttribute::Create(STUN_VALUE_BYTE_STRING, type,
479 static_cast<uint16_t>(length), this);
480 } else {
481 return NULL;
482 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000483}
484
485const StunAttribute* StunMessage::GetAttribute(int type) const {
zsteinad94c4c2017-03-06 13:36:05 -0800486 for (const auto& attr : attrs_) {
487 if (attr->type() == type) {
488 return attr.get();
489 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000490 }
491 return NULL;
492}
493
494bool StunMessage::IsValidTransactionId(const std::string& transaction_id) {
495 return transaction_id.size() == kStunTransactionIdLength ||
Yves Gerey665174f2018-06-19 15:03:05 +0200496 transaction_id.size() == kStunLegacyTransactionIdLength;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000497}
498
499// StunAttribute
500
Peter Boström0c4e06b2015-10-07 12:23:21 +0200501StunAttribute::StunAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200502 : type_(type), length_(length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000503
jbauchf1f87202016-03-30 06:43:37 -0700504void StunAttribute::ConsumePadding(ByteBufferReader* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000505 int remainder = length_ % 4;
506 if (remainder > 0) {
507 buf->Consume(4 - remainder);
508 }
509}
510
jbauchf1f87202016-03-30 06:43:37 -0700511void StunAttribute::WritePadding(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000512 int remainder = length_ % 4;
513 if (remainder > 0) {
514 char zeroes[4] = {0};
515 buf->WriteBytes(zeroes, 4 - remainder);
516 }
517}
518
519StunAttribute* StunAttribute::Create(StunAttributeValueType value_type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200520 uint16_t type,
521 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000522 StunMessage* owner) {
523 switch (value_type) {
524 case STUN_VALUE_ADDRESS:
525 return new StunAddressAttribute(type, length);
526 case STUN_VALUE_XOR_ADDRESS:
527 return new StunXorAddressAttribute(type, length, owner);
528 case STUN_VALUE_UINT32:
529 return new StunUInt32Attribute(type);
530 case STUN_VALUE_UINT64:
531 return new StunUInt64Attribute(type);
532 case STUN_VALUE_BYTE_STRING:
533 return new StunByteStringAttribute(type, length);
534 case STUN_VALUE_ERROR_CODE:
535 return new StunErrorCodeAttribute(type, length);
536 case STUN_VALUE_UINT16_LIST:
537 return new StunUInt16ListAttribute(type, length);
538 default:
539 return NULL;
540 }
541}
542
zsteinf42cc9d2017-03-27 16:17:19 -0700543std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress(
544 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200545 return absl::make_unique<StunAddressAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000546}
547
zsteinf42cc9d2017-03-27 16:17:19 -0700548std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress(
549 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200550 return absl::make_unique<StunXorAddressAttribute>(type, 0, nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000551}
552
zsteinf42cc9d2017-03-27 16:17:19 -0700553std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64(
554 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200555 return absl::make_unique<StunUInt64Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000556}
557
zsteinf42cc9d2017-03-27 16:17:19 -0700558std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32(
559 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200560 return absl::make_unique<StunUInt32Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000561}
562
zsteinf42cc9d2017-03-27 16:17:19 -0700563std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString(
564 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200565 return absl::make_unique<StunByteStringAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000566}
567
zsteinf42cc9d2017-03-27 16:17:19 -0700568std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200569 return absl::make_unique<StunErrorCodeAttribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000570 STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE);
571}
572
zsteinf42cc9d2017-03-27 16:17:19 -0700573std::unique_ptr<StunUInt16ListAttribute>
574StunAttribute::CreateUnknownAttributes() {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200575 return absl::make_unique<StunUInt16ListAttribute>(
576 STUN_ATTR_UNKNOWN_ATTRIBUTES, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000577}
578
Peter Boström0c4e06b2015-10-07 12:23:21 +0200579StunAddressAttribute::StunAddressAttribute(uint16_t type,
580 const rtc::SocketAddress& addr)
581 : StunAttribute(type, 0) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000582 SetAddress(addr);
583}
584
Peter Boström0c4e06b2015-10-07 12:23:21 +0200585StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200586 : StunAttribute(type, length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000587
Steve Antonca7d54e2017-10-25 14:42:51 -0700588StunAttributeValueType StunAddressAttribute::value_type() const {
589 return STUN_VALUE_ADDRESS;
590}
591
jbauchf1f87202016-03-30 06:43:37 -0700592bool StunAddressAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200593 uint8_t dummy;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000594 if (!buf->ReadUInt8(&dummy))
595 return false;
596
Peter Boström0c4e06b2015-10-07 12:23:21 +0200597 uint8_t stun_family;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000598 if (!buf->ReadUInt8(&stun_family)) {
599 return false;
600 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200601 uint16_t port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000602 if (!buf->ReadUInt16(&port))
603 return false;
604 if (stun_family == STUN_ADDRESS_IPV4) {
605 in_addr v4addr;
606 if (length() != SIZE_IP4) {
607 return false;
608 }
609 if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) {
610 return false;
611 }
612 rtc::IPAddress ipaddr(v4addr);
613 SetAddress(rtc::SocketAddress(ipaddr, port));
614 } else if (stun_family == STUN_ADDRESS_IPV6) {
615 in6_addr v6addr;
616 if (length() != SIZE_IP6) {
617 return false;
618 }
619 if (!buf->ReadBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr))) {
620 return false;
621 }
622 rtc::IPAddress ipaddr(v6addr);
623 SetAddress(rtc::SocketAddress(ipaddr, port));
624 } else {
625 return false;
626 }
627 return true;
628}
629
jbauchf1f87202016-03-30 06:43:37 -0700630bool StunAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000631 StunAddressFamily address_family = family();
632 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100633 RTC_LOG(LS_ERROR) << "Error writing address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000634 return false;
635 }
636 buf->WriteUInt8(0);
637 buf->WriteUInt8(address_family);
638 buf->WriteUInt16(address_.port());
639 switch (address_.family()) {
640 case AF_INET: {
641 in_addr v4addr = address_.ipaddr().ipv4_address();
642 buf->WriteBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr));
643 break;
644 }
645 case AF_INET6: {
646 in6_addr v6addr = address_.ipaddr().ipv6_address();
647 buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr));
648 break;
649 }
650 }
651 return true;
652}
653
Peter Boström0c4e06b2015-10-07 12:23:21 +0200654StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
655 const rtc::SocketAddress& addr)
Yves Gerey665174f2018-06-19 15:03:05 +0200656 : StunAddressAttribute(type, addr), owner_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000657
Peter Boström0c4e06b2015-10-07 12:23:21 +0200658StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
659 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000660 StunMessage* owner)
Yves Gerey665174f2018-06-19 15:03:05 +0200661 : StunAddressAttribute(type, length), owner_(owner) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000662
Steve Antonca7d54e2017-10-25 14:42:51 -0700663StunAttributeValueType StunXorAddressAttribute::value_type() const {
664 return STUN_VALUE_XOR_ADDRESS;
665}
666
667void StunXorAddressAttribute::SetOwner(StunMessage* owner) {
668 owner_ = owner;
669}
670
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000671rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const {
672 if (owner_) {
673 rtc::IPAddress ip = ipaddr();
674 switch (ip.family()) {
675 case AF_INET: {
676 in_addr v4addr = ip.ipv4_address();
677 v4addr.s_addr =
678 (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie));
679 return rtc::IPAddress(v4addr);
680 }
681 case AF_INET6: {
682 in6_addr v6addr = ip.ipv6_address();
683 const std::string& transaction_id = owner_->transaction_id();
684 if (transaction_id.length() == kStunTransactionIdLength) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200685 uint32_t transactionid_as_ints[3];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000686 memcpy(&transactionid_as_ints[0], transaction_id.c_str(),
687 transaction_id.length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200688 uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000689 // Transaction ID is in network byte order, but magic cookie
690 // is stored in host byte order.
691 ip_as_ints[0] =
692 (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie));
693 ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]);
694 ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]);
695 ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]);
696 return rtc::IPAddress(v6addr);
697 }
698 break;
699 }
700 }
701 }
702 // Invalid ip family or transaction ID, or missing owner.
703 // Return an AF_UNSPEC address.
704 return rtc::IPAddress();
705}
706
jbauchf1f87202016-03-30 06:43:37 -0700707bool StunXorAddressAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000708 if (!StunAddressAttribute::Read(buf))
709 return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200710 uint16_t xoredport = port() ^ (kStunMagicCookie >> 16);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000711 rtc::IPAddress xored_ip = GetXoredIP();
712 SetAddress(rtc::SocketAddress(xored_ip, xoredport));
713 return true;
714}
715
jbauchf1f87202016-03-30 06:43:37 -0700716bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000717 StunAddressFamily address_family = family();
718 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 RTC_LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000720 return false;
721 }
722 rtc::IPAddress xored_ip = GetXoredIP();
723 if (xored_ip.family() == AF_UNSPEC) {
724 return false;
725 }
726 buf->WriteUInt8(0);
727 buf->WriteUInt8(family());
728 buf->WriteUInt16(port() ^ (kStunMagicCookie >> 16));
729 switch (xored_ip.family()) {
730 case AF_INET: {
731 in_addr v4addr = xored_ip.ipv4_address();
732 buf->WriteBytes(reinterpret_cast<const char*>(&v4addr), sizeof(v4addr));
733 break;
734 }
735 case AF_INET6: {
736 in6_addr v6addr = xored_ip.ipv6_address();
737 buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr));
738 break;
739 }
740 }
741 return true;
742}
743
Peter Boström0c4e06b2015-10-07 12:23:21 +0200744StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200745 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000746
Peter Boström0c4e06b2015-10-07 12:23:21 +0200747StunUInt32Attribute::StunUInt32Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200748 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000749
Steve Antonca7d54e2017-10-25 14:42:51 -0700750StunAttributeValueType StunUInt32Attribute::value_type() const {
751 return STUN_VALUE_UINT32;
752}
753
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000754bool StunUInt32Attribute::GetBit(size_t index) const {
nisseede5da42017-01-12 05:15:36 -0800755 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000756 return static_cast<bool>((bits_ >> index) & 0x1);
757}
758
759void StunUInt32Attribute::SetBit(size_t index, bool value) {
nisseede5da42017-01-12 05:15:36 -0800760 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000761 bits_ &= ~(1 << index);
762 bits_ |= value ? (1 << index) : 0;
763}
764
jbauchf1f87202016-03-30 06:43:37 -0700765bool StunUInt32Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000766 if (length() != SIZE || !buf->ReadUInt32(&bits_))
767 return false;
768 return true;
769}
770
jbauchf1f87202016-03-30 06:43:37 -0700771bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000772 buf->WriteUInt32(bits_);
773 return true;
774}
775
Peter Boström0c4e06b2015-10-07 12:23:21 +0200776StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200777 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000778
Peter Boström0c4e06b2015-10-07 12:23:21 +0200779StunUInt64Attribute::StunUInt64Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200780 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000781
Steve Antonca7d54e2017-10-25 14:42:51 -0700782StunAttributeValueType StunUInt64Attribute::value_type() const {
783 return STUN_VALUE_UINT64;
784}
785
jbauchf1f87202016-03-30 06:43:37 -0700786bool StunUInt64Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000787 if (length() != SIZE || !buf->ReadUInt64(&bits_))
788 return false;
789 return true;
790}
791
jbauchf1f87202016-03-30 06:43:37 -0700792bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000793 buf->WriteUInt64(bits_);
794 return true;
795}
796
Peter Boström0c4e06b2015-10-07 12:23:21 +0200797StunByteStringAttribute::StunByteStringAttribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200798 : StunAttribute(type, 0), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000799
Peter Boström0c4e06b2015-10-07 12:23:21 +0200800StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000801 const std::string& str)
802 : StunAttribute(type, 0), bytes_(NULL) {
803 CopyBytes(str.c_str(), str.size());
804}
805
Peter Boström0c4e06b2015-10-07 12:23:21 +0200806StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000807 const void* bytes,
808 size_t length)
809 : StunAttribute(type, 0), bytes_(NULL) {
810 CopyBytes(bytes, length);
811}
812
Peter Boström0c4e06b2015-10-07 12:23:21 +0200813StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200814 : StunAttribute(type, length), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000815
816StunByteStringAttribute::~StunByteStringAttribute() {
Yves Gerey665174f2018-06-19 15:03:05 +0200817 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000818}
819
Steve Antonca7d54e2017-10-25 14:42:51 -0700820StunAttributeValueType StunByteStringAttribute::value_type() const {
821 return STUN_VALUE_BYTE_STRING;
822}
823
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000824void StunByteStringAttribute::CopyBytes(const char* bytes) {
825 CopyBytes(bytes, strlen(bytes));
826}
827
828void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) {
829 char* new_bytes = new char[length];
830 memcpy(new_bytes, bytes, length);
831 SetBytes(new_bytes, length);
832}
833
Peter Boström0c4e06b2015-10-07 12:23:21 +0200834uint8_t StunByteStringAttribute::GetByte(size_t index) const {
nisseede5da42017-01-12 05:15:36 -0800835 RTC_DCHECK(bytes_ != NULL);
836 RTC_DCHECK(index < length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200837 return static_cast<uint8_t>(bytes_[index]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000838}
839
Peter Boström0c4e06b2015-10-07 12:23:21 +0200840void StunByteStringAttribute::SetByte(size_t index, uint8_t value) {
nisseede5da42017-01-12 05:15:36 -0800841 RTC_DCHECK(bytes_ != NULL);
842 RTC_DCHECK(index < length());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000843 bytes_[index] = value;
844}
845
jbauchf1f87202016-03-30 06:43:37 -0700846bool StunByteStringAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000847 bytes_ = new char[length()];
848 if (!buf->ReadBytes(bytes_, length())) {
849 return false;
850 }
851
852 ConsumePadding(buf);
853 return true;
854}
855
jbauchf1f87202016-03-30 06:43:37 -0700856bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000857 buf->WriteBytes(bytes_, length());
858 WritePadding(buf);
859 return true;
860}
861
862void StunByteStringAttribute::SetBytes(char* bytes, size_t length) {
Yves Gerey665174f2018-06-19 15:03:05 +0200863 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000864 bytes_ = bytes;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200865 SetLength(static_cast<uint16_t>(length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000866}
867
zsteinf42cc9d2017-03-27 16:17:19 -0700868const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4;
869
Peter Boström0c4e06b2015-10-07 12:23:21 +0200870StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type,
871 int code,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000872 const std::string& reason)
873 : StunAttribute(type, 0) {
874 SetCode(code);
875 SetReason(reason);
876}
877
Peter Boström0c4e06b2015-10-07 12:23:21 +0200878StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200879 : StunAttribute(type, length), class_(0), number_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000880
Yves Gerey665174f2018-06-19 15:03:05 +0200881StunErrorCodeAttribute::~StunErrorCodeAttribute() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000882
Steve Antonca7d54e2017-10-25 14:42:51 -0700883StunAttributeValueType StunErrorCodeAttribute::value_type() const {
884 return STUN_VALUE_ERROR_CODE;
885}
886
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000887int StunErrorCodeAttribute::code() const {
888 return class_ * 100 + number_;
889}
890
891void StunErrorCodeAttribute::SetCode(int code) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200892 class_ = static_cast<uint8_t>(code / 100);
893 number_ = static_cast<uint8_t>(code % 100);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000894}
895
896void StunErrorCodeAttribute::SetReason(const std::string& reason) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200897 SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898 reason_ = reason;
899}
900
jbauchf1f87202016-03-30 06:43:37 -0700901bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200902 uint32_t val;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903 if (length() < MIN_SIZE || !buf->ReadUInt32(&val))
904 return false;
905
906 if ((val >> 11) != 0)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100907 RTC_LOG(LS_ERROR) << "error-code bits not zero";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000908
909 class_ = ((val >> 8) & 0x7);
910 number_ = (val & 0xff);
911
912 if (!buf->ReadString(&reason_, length() - 4))
913 return false;
914
915 ConsumePadding(buf);
916 return true;
917}
918
jbauchf1f87202016-03-30 06:43:37 -0700919bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920 buf->WriteUInt32(class_ << 8 | number_);
921 buf->WriteString(reason_);
922 WritePadding(buf);
923 return true;
924}
925
Peter Boström0c4e06b2015-10-07 12:23:21 +0200926StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000927 : StunAttribute(type, length) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200928 attr_types_ = new std::vector<uint16_t>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000929}
930
931StunUInt16ListAttribute::~StunUInt16ListAttribute() {
932 delete attr_types_;
933}
934
Steve Antonca7d54e2017-10-25 14:42:51 -0700935StunAttributeValueType StunUInt16ListAttribute::value_type() const {
936 return STUN_VALUE_UINT16_LIST;
937}
938
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000939size_t StunUInt16ListAttribute::Size() const {
940 return attr_types_->size();
941}
942
Peter Boström0c4e06b2015-10-07 12:23:21 +0200943uint16_t StunUInt16ListAttribute::GetType(int index) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000944 return (*attr_types_)[index];
945}
946
Peter Boström0c4e06b2015-10-07 12:23:21 +0200947void StunUInt16ListAttribute::SetType(int index, uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000948 (*attr_types_)[index] = value;
949}
950
Peter Boström0c4e06b2015-10-07 12:23:21 +0200951void StunUInt16ListAttribute::AddType(uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000952 attr_types_->push_back(value);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200953 SetLength(static_cast<uint16_t>(attr_types_->size() * 2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000954}
955
jbauchf1f87202016-03-30 06:43:37 -0700956bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000957 if (length() % 2)
958 return false;
959
960 for (size_t i = 0; i < length() / 2; i++) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200961 uint16_t attr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000962 if (!buf->ReadUInt16(&attr))
963 return false;
964 attr_types_->push_back(attr);
965 }
966 // Padding of these attributes is done in RFC 5389 style. This is
967 // slightly different from RFC3489, but it shouldn't be important.
968 // RFC3489 pads out to a 32 bit boundary by duplicating one of the
969 // entries in the list (not necessarily the last one - it's unspecified).
970 // RFC5389 pads on the end, and the bytes are always ignored.
971 ConsumePadding(buf);
972 return true;
973}
974
jbauchf1f87202016-03-30 06:43:37 -0700975bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000976 for (size_t i = 0; i < attr_types_->size(); ++i) {
977 buf->WriteUInt16((*attr_types_)[i]);
978 }
979 WritePadding(buf);
980 return true;
981}
982
983int GetStunSuccessResponseType(int req_type) {
984 return IsStunRequestType(req_type) ? (req_type | 0x100) : -1;
985}
986
987int GetStunErrorResponseType(int req_type) {
988 return IsStunRequestType(req_type) ? (req_type | 0x110) : -1;
989}
990
991bool IsStunRequestType(int msg_type) {
992 return ((msg_type & kStunTypeMask) == 0x000);
993}
994
995bool IsStunIndicationType(int msg_type) {
996 return ((msg_type & kStunTypeMask) == 0x010);
997}
998
999bool IsStunSuccessResponseType(int msg_type) {
1000 return ((msg_type & kStunTypeMask) == 0x100);
1001}
1002
1003bool IsStunErrorResponseType(int msg_type) {
1004 return ((msg_type & kStunTypeMask) == 0x110);
1005}
1006
1007bool ComputeStunCredentialHash(const std::string& username,
1008 const std::string& realm,
1009 const std::string& password,
1010 std::string* hash) {
1011 // http://tools.ietf.org/html/rfc5389#section-15.4
1012 // long-term credentials will be calculated using the key and key is
1013 // key = MD5(username ":" realm ":" SASLprep(password))
1014 std::string input = username;
1015 input += ':';
1016 input += realm;
1017 input += ':';
1018 input += password;
1019
1020 char digest[rtc::MessageDigest::kMaxSize];
Yves Gerey665174f2018-06-19 15:03:05 +02001021 size_t size = rtc::ComputeDigest(rtc::DIGEST_MD5, input.c_str(), input.size(),
1022 digest, sizeof(digest));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001023 if (size == 0) {
1024 return false;
1025 }
1026
1027 *hash = std::string(digest, size);
1028 return true;
1029}
1030
Jonas Oreland202994c2017-12-18 12:10:43 +01001031std::unique_ptr<StunAttribute> CopyStunAttribute(
1032 const StunAttribute& attribute,
1033 rtc::ByteBufferWriter* tmp_buffer_ptr) {
1034 ByteBufferWriter tmpBuffer;
1035 if (tmp_buffer_ptr == nullptr) {
1036 tmp_buffer_ptr = &tmpBuffer;
1037 }
1038
Yves Gerey665174f2018-06-19 15:03:05 +02001039 std::unique_ptr<StunAttribute> copy(StunAttribute::Create(
1040 attribute.value_type(), attribute.type(),
1041 static_cast<uint16_t>(attribute.length()), nullptr));
Jonas Oreland202994c2017-12-18 12:10:43 +01001042
1043 if (!copy) {
1044 return nullptr;
1045 }
1046 tmp_buffer_ptr->Clear();
1047 if (!attribute.Write(tmp_buffer_ptr)) {
1048 return nullptr;
1049 }
1050 rtc::ByteBufferReader reader(*tmp_buffer_ptr);
1051 if (!copy->Read(&reader)) {
1052 return nullptr;
1053 }
1054
1055 return copy;
1056}
1057
Steve Antonca7d54e2017-10-25 14:42:51 -07001058StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const {
1059 switch (type) {
1060 case STUN_ATTR_LIFETIME:
1061 return STUN_VALUE_UINT32;
1062 case STUN_ATTR_MAGIC_COOKIE:
1063 return STUN_VALUE_BYTE_STRING;
1064 case STUN_ATTR_BANDWIDTH:
1065 return STUN_VALUE_UINT32;
1066 case STUN_ATTR_DESTINATION_ADDRESS:
1067 return STUN_VALUE_ADDRESS;
1068 case STUN_ATTR_SOURCE_ADDRESS2:
1069 return STUN_VALUE_ADDRESS;
1070 case STUN_ATTR_DATA:
1071 return STUN_VALUE_BYTE_STRING;
1072 case STUN_ATTR_OPTIONS:
1073 return STUN_VALUE_UINT32;
1074 default:
1075 return StunMessage::GetAttributeValueType(type);
1076 }
1077}
1078
1079StunMessage* RelayMessage::CreateNew() const {
1080 return new RelayMessage();
1081}
1082
1083StunAttributeValueType TurnMessage::GetAttributeValueType(int type) const {
1084 switch (type) {
1085 case STUN_ATTR_CHANNEL_NUMBER:
1086 return STUN_VALUE_UINT32;
1087 case STUN_ATTR_TURN_LIFETIME:
1088 return STUN_VALUE_UINT32;
1089 case STUN_ATTR_XOR_PEER_ADDRESS:
1090 return STUN_VALUE_XOR_ADDRESS;
1091 case STUN_ATTR_DATA:
1092 return STUN_VALUE_BYTE_STRING;
1093 case STUN_ATTR_XOR_RELAYED_ADDRESS:
1094 return STUN_VALUE_XOR_ADDRESS;
1095 case STUN_ATTR_EVEN_PORT:
1096 return STUN_VALUE_BYTE_STRING;
1097 case STUN_ATTR_REQUESTED_TRANSPORT:
1098 return STUN_VALUE_UINT32;
1099 case STUN_ATTR_DONT_FRAGMENT:
1100 return STUN_VALUE_BYTE_STRING;
1101 case STUN_ATTR_RESERVATION_TOKEN:
1102 return STUN_VALUE_BYTE_STRING;
1103 default:
1104 return StunMessage::GetAttributeValueType(type);
1105 }
1106}
1107
1108StunMessage* TurnMessage::CreateNew() const {
1109 return new TurnMessage();
1110}
1111
1112StunAttributeValueType IceMessage::GetAttributeValueType(int type) const {
1113 switch (type) {
1114 case STUN_ATTR_PRIORITY:
1115 case STUN_ATTR_NETWORK_INFO:
1116 case STUN_ATTR_NOMINATION:
1117 return STUN_VALUE_UINT32;
1118 case STUN_ATTR_USE_CANDIDATE:
1119 return STUN_VALUE_BYTE_STRING;
1120 case STUN_ATTR_ICE_CONTROLLED:
1121 return STUN_VALUE_UINT64;
1122 case STUN_ATTR_ICE_CONTROLLING:
1123 return STUN_VALUE_UINT64;
1124 default:
1125 return StunMessage::GetAttributeValueType(type);
1126 }
1127}
1128
1129StunMessage* IceMessage::CreateNew() const {
1130 return new IceMessage();
1131}
1132
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001133} // namespace cricket