blob: dd0fa627b71ba66e17ddcf885ac5f4c0e6ec2318 [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);
Zach Steinff71a492018-12-07 11:25:12 -080035 ByteBufferReader reader(transaction_id.c_str(), transaction_id.length(),
36 rtc::ByteBuffer::ORDER_NETWORK);
Zach Stein92c42892018-11-28 11:38:52 -080037 uint32_t result = 0;
Zach Steinff71a492018-12-07 11:25:12 -080038 uint32_t next;
39 while (reader.ReadUInt32(&next)) {
40 result ^= next;
Zach Stein92c42892018-11-28 11:38:52 -080041 }
42 return result;
43}
44
45} // namespace
46
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047namespace cricket {
48
49const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server";
50const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request";
51const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized";
52const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden";
53const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials";
54const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch";
55const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce";
56const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials";
57const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol";
58const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict";
59const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error";
60
Yves Gerey665174f2018-06-19 15:03:05 +020061const char TURN_MAGIC_COOKIE_VALUE[] = {'\x72', '\xC6', '\x4B', '\xC6'};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062const char EMPTY_TRANSACTION_ID[] = "0000000000000000";
Peter Boström0c4e06b2015-10-07 12:23:21 +020063const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064
65// StunMessage
66
67StunMessage::StunMessage()
68 : type_(0),
69 length_(0),
Jonas Oreland7ca63112018-02-27 08:45:13 +010070 transaction_id_(EMPTY_TRANSACTION_ID),
71 stun_magic_cookie_(kStunMagicCookie) {
nisseede5da42017-01-12 05:15:36 -080072 RTC_DCHECK(IsValidTransactionId(transaction_id_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073}
74
Steve Antonca7d54e2017-10-25 14:42:51 -070075StunMessage::~StunMessage() = default;
76
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077bool StunMessage::IsLegacy() const {
78 if (transaction_id_.size() == kStunLegacyTransactionIdLength)
79 return true;
nisseede5da42017-01-12 05:15:36 -080080 RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081 return false;
82}
83
84bool StunMessage::SetTransactionID(const std::string& str) {
85 if (!IsValidTransactionId(str)) {
86 return false;
87 }
88 transaction_id_ = str;
Zach Stein92c42892018-11-28 11:38:52 -080089 reduced_transaction_id_ = ReduceTransactionId(transaction_id_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090 return true;
91}
92
Jonas Oreland16ccef72018-03-27 09:02:43 +020093static bool DesignatedExpertRange(int attr_type) {
Yves Gerey665174f2018-06-19 15:03:05 +020094 return (attr_type >= 0x4000 && attr_type <= 0x7FFF) ||
95 (attr_type >= 0xC000 && attr_type <= 0xFFFF);
Jonas Orelandbdcee282017-10-10 14:01:40 +020096}
97
zsteinf42cc9d2017-03-27 16:17:19 -070098void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) {
Jonas Orelandbdcee282017-10-10 14:01:40 +020099 // Fail any attributes that aren't valid for this type of message,
Jonas Oreland16ccef72018-03-27 09:02:43 +0200100 // but allow any type for the range that in the RFC is reserved for
101 // the "designated experts".
102 if (!DesignatedExpertRange(attr->type())) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200103 RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type()));
104 }
nissecc99bc22017-02-02 01:31:30 -0800105
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 attr->SetOwner(this);
107 size_t attr_length = attr->length();
108 if (attr_length % 4 != 0) {
109 attr_length += (4 - (attr_length % 4));
110 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200111 length_ += static_cast<uint16_t>(attr_length + 4);
zsteinf42cc9d2017-03-27 16:17:19 -0700112
113 attrs_.push_back(std::move(attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114}
115
Jonas Oreland202994c2017-12-18 12:10:43 +0100116std::unique_ptr<StunAttribute> StunMessage::RemoveAttribute(int type) {
117 std::unique_ptr<StunAttribute> attribute;
118 for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) {
Yves Gerey665174f2018-06-19 15:03:05 +0200119 if ((*it)->type() == type) {
120 attribute = std::move(*it);
Jonas Oreland202994c2017-12-18 12:10:43 +0100121 attrs_.erase(std::next(it).base());
122 break;
123 }
124 }
125 if (attribute) {
126 attribute->SetOwner(nullptr);
127 size_t attr_length = attribute->length();
128 if (attr_length % 4 != 0) {
129 attr_length += (4 - (attr_length % 4));
130 }
131 length_ -= static_cast<uint16_t>(attr_length + 4);
132 }
133 return attribute;
134}
135
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136const StunAddressAttribute* StunMessage::GetAddress(int type) const {
137 switch (type) {
138 case STUN_ATTR_MAPPED_ADDRESS: {
139 // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is
140 // missing.
141 const StunAttribute* mapped_address =
142 GetAttribute(STUN_ATTR_MAPPED_ADDRESS);
143 if (!mapped_address)
144 mapped_address = GetAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS);
145 return reinterpret_cast<const StunAddressAttribute*>(mapped_address);
146 }
147
148 default:
149 return static_cast<const StunAddressAttribute*>(GetAttribute(type));
150 }
151}
152
153const StunUInt32Attribute* StunMessage::GetUInt32(int type) const {
154 return static_cast<const StunUInt32Attribute*>(GetAttribute(type));
155}
156
157const StunUInt64Attribute* StunMessage::GetUInt64(int type) const {
158 return static_cast<const StunUInt64Attribute*>(GetAttribute(type));
159}
160
161const StunByteStringAttribute* StunMessage::GetByteString(int type) const {
162 return static_cast<const StunByteStringAttribute*>(GetAttribute(type));
163}
164
165const StunErrorCodeAttribute* StunMessage::GetErrorCode() const {
166 return static_cast<const StunErrorCodeAttribute*>(
167 GetAttribute(STUN_ATTR_ERROR_CODE));
168}
169
deadbeef996fc6b2017-04-26 09:21:22 -0700170int StunMessage::GetErrorCodeValue() const {
171 const StunErrorCodeAttribute* error_attribute = GetErrorCode();
172 return error_attribute ? error_attribute->code() : STUN_ERROR_GLOBAL_FAILURE;
173}
174
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175const StunUInt16ListAttribute* StunMessage::GetUnknownAttributes() const {
176 return static_cast<const StunUInt16ListAttribute*>(
177 GetAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES));
178}
179
180// Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the
181// procedure outlined in RFC 5389, section 15.4.
Yves Gerey665174f2018-06-19 15:03:05 +0200182bool StunMessage::ValidateMessageIntegrity(const char* data,
183 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 const std::string& password) {
185 // Verifying the size of the message.
katrielce4bda242016-06-09 08:45:45 -0700186 if ((size % 4) != 0 || size < kStunHeaderSize) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187 return false;
188 }
189
190 // Getting the message length from the STUN header.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200191 uint16_t msg_length = rtc::GetBE16(&data[2]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 if (size != (msg_length + kStunHeaderSize)) {
193 return false;
194 }
195
196 // Finding Message Integrity attribute in stun message.
197 size_t current_pos = kStunHeaderSize;
198 bool has_message_integrity_attr = false;
katrielc1a206102016-06-20 05:13:16 -0700199 while (current_pos + 4 <= size) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200 uint16_t attr_type, attr_length;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 // Getting attribute type and length.
202 attr_type = rtc::GetBE16(&data[current_pos]);
203 attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]);
204
205 // If M-I, sanity check it, and break out.
206 if (attr_type == STUN_ATTR_MESSAGE_INTEGRITY) {
207 if (attr_length != kStunMessageIntegritySize ||
katrielc1a206102016-06-20 05:13:16 -0700208 current_pos + sizeof(attr_type) + sizeof(attr_length) + attr_length >
209 size) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210 return false;
211 }
212 has_message_integrity_attr = true;
213 break;
214 }
215
216 // Otherwise, skip to the next attribute.
217 current_pos += sizeof(attr_type) + sizeof(attr_length) + attr_length;
218 if ((attr_length % 4) != 0) {
219 current_pos += (4 - (attr_length % 4));
220 }
221 }
222
223 if (!has_message_integrity_attr) {
224 return false;
225 }
226
227 // Getting length of the message to calculate Message Integrity.
228 size_t mi_pos = current_pos;
kwiberg3ec46792016-04-27 07:22:53 -0700229 std::unique_ptr<char[]> temp_data(new char[current_pos]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000230 memcpy(temp_data.get(), data, current_pos);
231 if (size > mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize) {
232 // Stun message has other attributes after message integrity.
233 // Adjust the length parameter in stun message to calculate HMAC.
Yves Gerey665174f2018-06-19 15:03:05 +0200234 size_t extra_offset =
235 size - (mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 size_t new_adjusted_len = size - extra_offset - kStunHeaderSize;
237
238 // Writing new length of the STUN message @ Message Length in temp buffer.
239 // 0 1 2 3
240 // 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
241 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
242 // |0 0| STUN Message Type | Message Length |
243 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Peter Boström0c4e06b2015-10-07 12:23:21 +0200244 rtc::SetBE16(temp_data.get() + 2, static_cast<uint16_t>(new_adjusted_len));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 }
246
247 char hmac[kStunMessageIntegritySize];
Yves Gerey665174f2018-06-19 15:03:05 +0200248 size_t ret =
249 rtc::ComputeHmac(rtc::DIGEST_SHA_1, password.c_str(), password.size(),
250 temp_data.get(), mi_pos, hmac, sizeof(hmac));
nisseede5da42017-01-12 05:15:36 -0800251 RTC_DCHECK(ret == sizeof(hmac));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 if (ret != sizeof(hmac))
253 return false;
254
255 // Comparing the calculated HMAC with the one present in the message.
Yves Gerey665174f2018-06-19 15:03:05 +0200256 return memcmp(data + current_pos + kStunAttributeHeaderSize, hmac,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257 sizeof(hmac)) == 0;
258}
259
260bool StunMessage::AddMessageIntegrity(const std::string& password) {
261 return AddMessageIntegrity(password.c_str(), password.size());
262}
263
Yves Gerey665174f2018-06-19 15:03:05 +0200264bool StunMessage::AddMessageIntegrity(const char* key, size_t keylen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000265 // Add the attribute with a dummy value. Since this is a known attribute, it
266 // can't fail.
Karl Wiberg918f50c2018-07-05 11:40:33 +0200267 auto msg_integrity_attr_ptr = absl::make_unique<StunByteStringAttribute>(
zsteinf42cc9d2017-03-27 16:17:19 -0700268 STUN_ATTR_MESSAGE_INTEGRITY, std::string(kStunMessageIntegritySize, '0'));
269 auto* msg_integrity_attr = msg_integrity_attr_ptr.get();
270 AddAttribute(std::move(msg_integrity_attr_ptr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000271
272 // Calculate the HMAC for the message.
jbauchf1f87202016-03-30 06:43:37 -0700273 ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274 if (!Write(&buf))
275 return false;
276
277 int msg_len_for_hmac = static_cast<int>(
278 buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length());
279 char hmac[kStunMessageIntegritySize];
Yves Gerey665174f2018-06-19 15:03:05 +0200280 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, key, keylen, buf.Data(),
281 msg_len_for_hmac, hmac, sizeof(hmac));
nisseede5da42017-01-12 05:15:36 -0800282 RTC_DCHECK(ret == sizeof(hmac));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000283 if (ret != sizeof(hmac)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100284 RTC_LOG(LS_ERROR) << "HMAC computation failed. Message-Integrity "
Jonas Olssond7d762d2018-03-28 09:47:51 +0200285 "has dummy value.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000286 return false;
287 }
288
289 // Insert correct HMAC into the attribute.
290 msg_integrity_attr->CopyBytes(hmac, sizeof(hmac));
291 return true;
292}
293
294// Verifies a message is in fact a STUN message, by performing the checks
295// outlined in RFC 5389, section 7.3, including the FINGERPRINT check detailed
296// in section 15.5.
297bool StunMessage::ValidateFingerprint(const char* data, size_t size) {
298 // Check the message length.
299 size_t fingerprint_attr_size =
300 kStunAttributeHeaderSize + StunUInt32Attribute::SIZE;
301 if (size % 4 != 0 || size < kStunHeaderSize + fingerprint_attr_size)
302 return false;
303
304 // Skip the rest if the magic cookie isn't present.
305 const char* magic_cookie =
306 data + kStunTransactionIdOffset - kStunMagicCookieLength;
307 if (rtc::GetBE32(magic_cookie) != kStunMagicCookie)
308 return false;
309
310 // Check the fingerprint type and length.
311 const char* fingerprint_attr_data = data + size - fingerprint_attr_size;
312 if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT ||
Peter Boström0c4e06b2015-10-07 12:23:21 +0200313 rtc::GetBE16(fingerprint_attr_data + sizeof(uint16_t)) !=
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000314 StunUInt32Attribute::SIZE)
315 return false;
316
317 // Check the fingerprint value.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200318 uint32_t fingerprint =
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000319 rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize);
320 return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) ==
Yves Gerey665174f2018-06-19 15:03:05 +0200321 rtc::ComputeCrc32(data, size - fingerprint_attr_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000322}
323
324bool StunMessage::AddFingerprint() {
325 // Add the attribute with a dummy value. Since this is a known attribute,
326 // it can't fail.
zsteinf42cc9d2017-03-27 16:17:19 -0700327 auto fingerprint_attr_ptr =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200328 absl::make_unique<StunUInt32Attribute>(STUN_ATTR_FINGERPRINT, 0);
Steve Antonca7d54e2017-10-25 14:42:51 -0700329 auto* fingerprint_attr = fingerprint_attr_ptr.get();
zsteinf42cc9d2017-03-27 16:17:19 -0700330 AddAttribute(std::move(fingerprint_attr_ptr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000331
332 // Calculate the CRC-32 for the message and insert it.
jbauchf1f87202016-03-30 06:43:37 -0700333 ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000334 if (!Write(&buf))
335 return false;
336
337 int msg_len_for_crc32 = static_cast<int>(
338 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200339 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000340
341 // Insert the correct CRC-32, XORed with a constant, into the attribute.
342 fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE);
343 return true;
344}
345
jbauchf1f87202016-03-30 06:43:37 -0700346bool StunMessage::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000347 if (!buf->ReadUInt16(&type_))
348 return false;
349
350 if (type_ & 0x8000) {
351 // RTP and RTCP set the MSB of first byte, since first two bits are version,
352 // and version is always 2 (10). If set, this is not a STUN packet.
353 return false;
354 }
355
356 if (!buf->ReadUInt16(&length_))
357 return false;
358
359 std::string magic_cookie;
360 if (!buf->ReadString(&magic_cookie, kStunMagicCookieLength))
361 return false;
362
363 std::string transaction_id;
364 if (!buf->ReadString(&transaction_id, kStunTransactionIdLength))
365 return false;
366
Peter Boström0c4e06b2015-10-07 12:23:21 +0200367 uint32_t magic_cookie_int =
368 *reinterpret_cast<const uint32_t*>(magic_cookie.data());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) {
370 // If magic cookie is invalid it means that the peer implements
371 // RFC3489 instead of RFC5389.
372 transaction_id.insert(0, magic_cookie);
373 }
nisseede5da42017-01-12 05:15:36 -0800374 RTC_DCHECK(IsValidTransactionId(transaction_id));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375 transaction_id_ = transaction_id;
Zach Stein92c42892018-11-28 11:38:52 -0800376 reduced_transaction_id_ = ReduceTransactionId(transaction_id_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000377
378 if (length_ != buf->Length())
379 return false;
380
zsteinad94c4c2017-03-06 13:36:05 -0800381 attrs_.resize(0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000382
383 size_t rest = buf->Length() - length_;
384 while (buf->Length() > rest) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200385 uint16_t attr_type, attr_length;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 if (!buf->ReadUInt16(&attr_type))
387 return false;
388 if (!buf->ReadUInt16(&attr_length))
389 return false;
390
Honghai Zhang3e024302016-09-22 09:52:16 -0700391 std::unique_ptr<StunAttribute> attr(
392 CreateAttribute(attr_type, attr_length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000393 if (!attr) {
394 // Skip any unknown or malformed attributes.
395 if ((attr_length % 4) != 0) {
396 attr_length += (4 - (attr_length % 4));
397 }
398 if (!buf->Consume(attr_length))
399 return false;
400 } else {
401 if (!attr->Read(buf))
402 return false;
zsteinad94c4c2017-03-06 13:36:05 -0800403 attrs_.push_back(std::move(attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000404 }
405 }
406
nisseede5da42017-01-12 05:15:36 -0800407 RTC_DCHECK(buf->Length() == rest);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000408 return true;
409}
410
jbauchf1f87202016-03-30 06:43:37 -0700411bool StunMessage::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000412 buf->WriteUInt16(type_);
413 buf->WriteUInt16(length_);
414 if (!IsLegacy())
Jonas Oreland7ca63112018-02-27 08:45:13 +0100415 buf->WriteUInt32(stun_magic_cookie_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000416 buf->WriteString(transaction_id_);
417
zsteinad94c4c2017-03-06 13:36:05 -0800418 for (const auto& attr : attrs_) {
419 buf->WriteUInt16(attr->type());
420 buf->WriteUInt16(static_cast<uint16_t>(attr->length()));
421 if (!attr->Write(buf)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000422 return false;
zsteinad94c4c2017-03-06 13:36:05 -0800423 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000424 }
425
426 return true;
427}
428
Steve Antonca7d54e2017-10-25 14:42:51 -0700429StunMessage* StunMessage::CreateNew() const {
430 return new StunMessage();
431}
432
Jonas Oreland7ca63112018-02-27 08:45:13 +0100433void StunMessage::SetStunMagicCookie(uint32_t val) {
434 stun_magic_cookie_ = val;
435}
436
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000437StunAttributeValueType StunMessage::GetAttributeValueType(int type) const {
438 switch (type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200439 case STUN_ATTR_MAPPED_ADDRESS:
440 return STUN_VALUE_ADDRESS;
441 case STUN_ATTR_USERNAME:
442 return STUN_VALUE_BYTE_STRING;
443 case STUN_ATTR_MESSAGE_INTEGRITY:
444 return STUN_VALUE_BYTE_STRING;
445 case STUN_ATTR_ERROR_CODE:
446 return STUN_VALUE_ERROR_CODE;
447 case STUN_ATTR_UNKNOWN_ATTRIBUTES:
448 return STUN_VALUE_UINT16_LIST;
449 case STUN_ATTR_REALM:
450 return STUN_VALUE_BYTE_STRING;
451 case STUN_ATTR_NONCE:
452 return STUN_VALUE_BYTE_STRING;
453 case STUN_ATTR_XOR_MAPPED_ADDRESS:
454 return STUN_VALUE_XOR_ADDRESS;
455 case STUN_ATTR_SOFTWARE:
456 return STUN_VALUE_BYTE_STRING;
457 case STUN_ATTR_ALTERNATE_SERVER:
458 return STUN_VALUE_ADDRESS;
459 case STUN_ATTR_FINGERPRINT:
460 return STUN_VALUE_UINT32;
461 case STUN_ATTR_ORIGIN:
462 return STUN_VALUE_BYTE_STRING;
463 case STUN_ATTR_RETRANSMIT_COUNT:
464 return STUN_VALUE_UINT32;
465 default:
466 return STUN_VALUE_UNKNOWN;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000467 }
468}
469
470StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ {
471 StunAttributeValueType value_type = GetAttributeValueType(type);
Jonas Orelandbdcee282017-10-10 14:01:40 +0200472 if (value_type != STUN_VALUE_UNKNOWN) {
473 return StunAttribute::Create(value_type, type,
474 static_cast<uint16_t>(length), this);
Jonas Oreland16ccef72018-03-27 09:02:43 +0200475 } else if (DesignatedExpertRange(type)) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200476 // Read unknown attributes as STUN_VALUE_BYTE_STRING
477 return StunAttribute::Create(STUN_VALUE_BYTE_STRING, type,
478 static_cast<uint16_t>(length), this);
479 } else {
480 return NULL;
481 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000482}
483
484const StunAttribute* StunMessage::GetAttribute(int type) const {
zsteinad94c4c2017-03-06 13:36:05 -0800485 for (const auto& attr : attrs_) {
486 if (attr->type() == type) {
487 return attr.get();
488 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000489 }
490 return NULL;
491}
492
493bool StunMessage::IsValidTransactionId(const std::string& transaction_id) {
494 return transaction_id.size() == kStunTransactionIdLength ||
Yves Gerey665174f2018-06-19 15:03:05 +0200495 transaction_id.size() == kStunLegacyTransactionIdLength;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000496}
497
498// StunAttribute
499
Peter Boström0c4e06b2015-10-07 12:23:21 +0200500StunAttribute::StunAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200501 : type_(type), length_(length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000502
jbauchf1f87202016-03-30 06:43:37 -0700503void StunAttribute::ConsumePadding(ByteBufferReader* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000504 int remainder = length_ % 4;
505 if (remainder > 0) {
506 buf->Consume(4 - remainder);
507 }
508}
509
jbauchf1f87202016-03-30 06:43:37 -0700510void StunAttribute::WritePadding(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000511 int remainder = length_ % 4;
512 if (remainder > 0) {
513 char zeroes[4] = {0};
514 buf->WriteBytes(zeroes, 4 - remainder);
515 }
516}
517
518StunAttribute* StunAttribute::Create(StunAttributeValueType value_type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200519 uint16_t type,
520 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000521 StunMessage* owner) {
522 switch (value_type) {
523 case STUN_VALUE_ADDRESS:
524 return new StunAddressAttribute(type, length);
525 case STUN_VALUE_XOR_ADDRESS:
526 return new StunXorAddressAttribute(type, length, owner);
527 case STUN_VALUE_UINT32:
528 return new StunUInt32Attribute(type);
529 case STUN_VALUE_UINT64:
530 return new StunUInt64Attribute(type);
531 case STUN_VALUE_BYTE_STRING:
532 return new StunByteStringAttribute(type, length);
533 case STUN_VALUE_ERROR_CODE:
534 return new StunErrorCodeAttribute(type, length);
535 case STUN_VALUE_UINT16_LIST:
536 return new StunUInt16ListAttribute(type, length);
537 default:
538 return NULL;
539 }
540}
541
zsteinf42cc9d2017-03-27 16:17:19 -0700542std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress(
543 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200544 return absl::make_unique<StunAddressAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000545}
546
zsteinf42cc9d2017-03-27 16:17:19 -0700547std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress(
548 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200549 return absl::make_unique<StunXorAddressAttribute>(type, 0, nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000550}
551
zsteinf42cc9d2017-03-27 16:17:19 -0700552std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64(
553 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200554 return absl::make_unique<StunUInt64Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000555}
556
zsteinf42cc9d2017-03-27 16:17:19 -0700557std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32(
558 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200559 return absl::make_unique<StunUInt32Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000560}
561
zsteinf42cc9d2017-03-27 16:17:19 -0700562std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString(
563 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200564 return absl::make_unique<StunByteStringAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000565}
566
zsteinf42cc9d2017-03-27 16:17:19 -0700567std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200568 return absl::make_unique<StunErrorCodeAttribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000569 STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE);
570}
571
zsteinf42cc9d2017-03-27 16:17:19 -0700572std::unique_ptr<StunUInt16ListAttribute>
573StunAttribute::CreateUnknownAttributes() {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200574 return absl::make_unique<StunUInt16ListAttribute>(
575 STUN_ATTR_UNKNOWN_ATTRIBUTES, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000576}
577
Peter Boström0c4e06b2015-10-07 12:23:21 +0200578StunAddressAttribute::StunAddressAttribute(uint16_t type,
579 const rtc::SocketAddress& addr)
580 : StunAttribute(type, 0) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000581 SetAddress(addr);
582}
583
Peter Boström0c4e06b2015-10-07 12:23:21 +0200584StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200585 : StunAttribute(type, length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000586
Steve Antonca7d54e2017-10-25 14:42:51 -0700587StunAttributeValueType StunAddressAttribute::value_type() const {
588 return STUN_VALUE_ADDRESS;
589}
590
jbauchf1f87202016-03-30 06:43:37 -0700591bool StunAddressAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200592 uint8_t dummy;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000593 if (!buf->ReadUInt8(&dummy))
594 return false;
595
Peter Boström0c4e06b2015-10-07 12:23:21 +0200596 uint8_t stun_family;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000597 if (!buf->ReadUInt8(&stun_family)) {
598 return false;
599 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200600 uint16_t port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000601 if (!buf->ReadUInt16(&port))
602 return false;
603 if (stun_family == STUN_ADDRESS_IPV4) {
604 in_addr v4addr;
605 if (length() != SIZE_IP4) {
606 return false;
607 }
608 if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) {
609 return false;
610 }
611 rtc::IPAddress ipaddr(v4addr);
612 SetAddress(rtc::SocketAddress(ipaddr, port));
613 } else if (stun_family == STUN_ADDRESS_IPV6) {
614 in6_addr v6addr;
615 if (length() != SIZE_IP6) {
616 return false;
617 }
618 if (!buf->ReadBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr))) {
619 return false;
620 }
621 rtc::IPAddress ipaddr(v6addr);
622 SetAddress(rtc::SocketAddress(ipaddr, port));
623 } else {
624 return false;
625 }
626 return true;
627}
628
jbauchf1f87202016-03-30 06:43:37 -0700629bool StunAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000630 StunAddressFamily address_family = family();
631 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100632 RTC_LOG(LS_ERROR) << "Error writing address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000633 return false;
634 }
635 buf->WriteUInt8(0);
636 buf->WriteUInt8(address_family);
637 buf->WriteUInt16(address_.port());
638 switch (address_.family()) {
639 case AF_INET: {
640 in_addr v4addr = address_.ipaddr().ipv4_address();
641 buf->WriteBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr));
642 break;
643 }
644 case AF_INET6: {
645 in6_addr v6addr = address_.ipaddr().ipv6_address();
646 buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr));
647 break;
648 }
649 }
650 return true;
651}
652
Peter Boström0c4e06b2015-10-07 12:23:21 +0200653StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
654 const rtc::SocketAddress& addr)
Yves Gerey665174f2018-06-19 15:03:05 +0200655 : StunAddressAttribute(type, addr), owner_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000656
Peter Boström0c4e06b2015-10-07 12:23:21 +0200657StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
658 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000659 StunMessage* owner)
Yves Gerey665174f2018-06-19 15:03:05 +0200660 : StunAddressAttribute(type, length), owner_(owner) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000661
Steve Antonca7d54e2017-10-25 14:42:51 -0700662StunAttributeValueType StunXorAddressAttribute::value_type() const {
663 return STUN_VALUE_XOR_ADDRESS;
664}
665
666void StunXorAddressAttribute::SetOwner(StunMessage* owner) {
667 owner_ = owner;
668}
669
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000670rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const {
671 if (owner_) {
672 rtc::IPAddress ip = ipaddr();
673 switch (ip.family()) {
674 case AF_INET: {
675 in_addr v4addr = ip.ipv4_address();
676 v4addr.s_addr =
677 (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie));
678 return rtc::IPAddress(v4addr);
679 }
680 case AF_INET6: {
681 in6_addr v6addr = ip.ipv6_address();
682 const std::string& transaction_id = owner_->transaction_id();
683 if (transaction_id.length() == kStunTransactionIdLength) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200684 uint32_t transactionid_as_ints[3];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000685 memcpy(&transactionid_as_ints[0], transaction_id.c_str(),
686 transaction_id.length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200687 uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000688 // Transaction ID is in network byte order, but magic cookie
689 // is stored in host byte order.
690 ip_as_ints[0] =
691 (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie));
692 ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]);
693 ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]);
694 ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]);
695 return rtc::IPAddress(v6addr);
696 }
697 break;
698 }
699 }
700 }
701 // Invalid ip family or transaction ID, or missing owner.
702 // Return an AF_UNSPEC address.
703 return rtc::IPAddress();
704}
705
jbauchf1f87202016-03-30 06:43:37 -0700706bool StunXorAddressAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000707 if (!StunAddressAttribute::Read(buf))
708 return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200709 uint16_t xoredport = port() ^ (kStunMagicCookie >> 16);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000710 rtc::IPAddress xored_ip = GetXoredIP();
711 SetAddress(rtc::SocketAddress(xored_ip, xoredport));
712 return true;
713}
714
jbauchf1f87202016-03-30 06:43:37 -0700715bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000716 StunAddressFamily address_family = family();
717 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100718 RTC_LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000719 return false;
720 }
721 rtc::IPAddress xored_ip = GetXoredIP();
722 if (xored_ip.family() == AF_UNSPEC) {
723 return false;
724 }
725 buf->WriteUInt8(0);
726 buf->WriteUInt8(family());
727 buf->WriteUInt16(port() ^ (kStunMagicCookie >> 16));
728 switch (xored_ip.family()) {
729 case AF_INET: {
730 in_addr v4addr = xored_ip.ipv4_address();
731 buf->WriteBytes(reinterpret_cast<const char*>(&v4addr), sizeof(v4addr));
732 break;
733 }
734 case AF_INET6: {
735 in6_addr v6addr = xored_ip.ipv6_address();
736 buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr));
737 break;
738 }
739 }
740 return true;
741}
742
Peter Boström0c4e06b2015-10-07 12:23:21 +0200743StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200744 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000745
Peter Boström0c4e06b2015-10-07 12:23:21 +0200746StunUInt32Attribute::StunUInt32Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200747 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000748
Steve Antonca7d54e2017-10-25 14:42:51 -0700749StunAttributeValueType StunUInt32Attribute::value_type() const {
750 return STUN_VALUE_UINT32;
751}
752
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000753bool StunUInt32Attribute::GetBit(size_t index) const {
nisseede5da42017-01-12 05:15:36 -0800754 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000755 return static_cast<bool>((bits_ >> index) & 0x1);
756}
757
758void StunUInt32Attribute::SetBit(size_t index, bool value) {
nisseede5da42017-01-12 05:15:36 -0800759 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000760 bits_ &= ~(1 << index);
761 bits_ |= value ? (1 << index) : 0;
762}
763
jbauchf1f87202016-03-30 06:43:37 -0700764bool StunUInt32Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000765 if (length() != SIZE || !buf->ReadUInt32(&bits_))
766 return false;
767 return true;
768}
769
jbauchf1f87202016-03-30 06:43:37 -0700770bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000771 buf->WriteUInt32(bits_);
772 return true;
773}
774
Peter Boström0c4e06b2015-10-07 12:23:21 +0200775StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200776 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000777
Peter Boström0c4e06b2015-10-07 12:23:21 +0200778StunUInt64Attribute::StunUInt64Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200779 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000780
Steve Antonca7d54e2017-10-25 14:42:51 -0700781StunAttributeValueType StunUInt64Attribute::value_type() const {
782 return STUN_VALUE_UINT64;
783}
784
jbauchf1f87202016-03-30 06:43:37 -0700785bool StunUInt64Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000786 if (length() != SIZE || !buf->ReadUInt64(&bits_))
787 return false;
788 return true;
789}
790
jbauchf1f87202016-03-30 06:43:37 -0700791bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000792 buf->WriteUInt64(bits_);
793 return true;
794}
795
Peter Boström0c4e06b2015-10-07 12:23:21 +0200796StunByteStringAttribute::StunByteStringAttribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200797 : StunAttribute(type, 0), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000798
Peter Boström0c4e06b2015-10-07 12:23:21 +0200799StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000800 const std::string& str)
801 : StunAttribute(type, 0), bytes_(NULL) {
802 CopyBytes(str.c_str(), str.size());
803}
804
Peter Boström0c4e06b2015-10-07 12:23:21 +0200805StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000806 const void* bytes,
807 size_t length)
808 : StunAttribute(type, 0), bytes_(NULL) {
809 CopyBytes(bytes, length);
810}
811
Peter Boström0c4e06b2015-10-07 12:23:21 +0200812StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200813 : StunAttribute(type, length), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000814
815StunByteStringAttribute::~StunByteStringAttribute() {
Yves Gerey665174f2018-06-19 15:03:05 +0200816 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000817}
818
Steve Antonca7d54e2017-10-25 14:42:51 -0700819StunAttributeValueType StunByteStringAttribute::value_type() const {
820 return STUN_VALUE_BYTE_STRING;
821}
822
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000823void StunByteStringAttribute::CopyBytes(const char* bytes) {
824 CopyBytes(bytes, strlen(bytes));
825}
826
827void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) {
828 char* new_bytes = new char[length];
829 memcpy(new_bytes, bytes, length);
830 SetBytes(new_bytes, length);
831}
832
Peter Boström0c4e06b2015-10-07 12:23:21 +0200833uint8_t StunByteStringAttribute::GetByte(size_t index) const {
nisseede5da42017-01-12 05:15:36 -0800834 RTC_DCHECK(bytes_ != NULL);
835 RTC_DCHECK(index < length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200836 return static_cast<uint8_t>(bytes_[index]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000837}
838
Peter Boström0c4e06b2015-10-07 12:23:21 +0200839void StunByteStringAttribute::SetByte(size_t index, uint8_t value) {
nisseede5da42017-01-12 05:15:36 -0800840 RTC_DCHECK(bytes_ != NULL);
841 RTC_DCHECK(index < length());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000842 bytes_[index] = value;
843}
844
jbauchf1f87202016-03-30 06:43:37 -0700845bool StunByteStringAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000846 bytes_ = new char[length()];
847 if (!buf->ReadBytes(bytes_, length())) {
848 return false;
849 }
850
851 ConsumePadding(buf);
852 return true;
853}
854
jbauchf1f87202016-03-30 06:43:37 -0700855bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000856 buf->WriteBytes(bytes_, length());
857 WritePadding(buf);
858 return true;
859}
860
861void StunByteStringAttribute::SetBytes(char* bytes, size_t length) {
Yves Gerey665174f2018-06-19 15:03:05 +0200862 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000863 bytes_ = bytes;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200864 SetLength(static_cast<uint16_t>(length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000865}
866
zsteinf42cc9d2017-03-27 16:17:19 -0700867const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4;
868
Peter Boström0c4e06b2015-10-07 12:23:21 +0200869StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type,
870 int code,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000871 const std::string& reason)
872 : StunAttribute(type, 0) {
873 SetCode(code);
874 SetReason(reason);
875}
876
Peter Boström0c4e06b2015-10-07 12:23:21 +0200877StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200878 : StunAttribute(type, length), class_(0), number_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000879
Yves Gerey665174f2018-06-19 15:03:05 +0200880StunErrorCodeAttribute::~StunErrorCodeAttribute() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000881
Steve Antonca7d54e2017-10-25 14:42:51 -0700882StunAttributeValueType StunErrorCodeAttribute::value_type() const {
883 return STUN_VALUE_ERROR_CODE;
884}
885
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000886int StunErrorCodeAttribute::code() const {
887 return class_ * 100 + number_;
888}
889
890void StunErrorCodeAttribute::SetCode(int code) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200891 class_ = static_cast<uint8_t>(code / 100);
892 number_ = static_cast<uint8_t>(code % 100);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000893}
894
895void StunErrorCodeAttribute::SetReason(const std::string& reason) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200896 SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000897 reason_ = reason;
898}
899
jbauchf1f87202016-03-30 06:43:37 -0700900bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200901 uint32_t val;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000902 if (length() < MIN_SIZE || !buf->ReadUInt32(&val))
903 return false;
904
905 if ((val >> 11) != 0)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100906 RTC_LOG(LS_ERROR) << "error-code bits not zero";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000907
908 class_ = ((val >> 8) & 0x7);
909 number_ = (val & 0xff);
910
911 if (!buf->ReadString(&reason_, length() - 4))
912 return false;
913
914 ConsumePadding(buf);
915 return true;
916}
917
jbauchf1f87202016-03-30 06:43:37 -0700918bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000919 buf->WriteUInt32(class_ << 8 | number_);
920 buf->WriteString(reason_);
921 WritePadding(buf);
922 return true;
923}
924
Peter Boström0c4e06b2015-10-07 12:23:21 +0200925StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000926 : StunAttribute(type, length) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200927 attr_types_ = new std::vector<uint16_t>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000928}
929
930StunUInt16ListAttribute::~StunUInt16ListAttribute() {
931 delete attr_types_;
932}
933
Steve Antonca7d54e2017-10-25 14:42:51 -0700934StunAttributeValueType StunUInt16ListAttribute::value_type() const {
935 return STUN_VALUE_UINT16_LIST;
936}
937
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000938size_t StunUInt16ListAttribute::Size() const {
939 return attr_types_->size();
940}
941
Peter Boström0c4e06b2015-10-07 12:23:21 +0200942uint16_t StunUInt16ListAttribute::GetType(int index) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000943 return (*attr_types_)[index];
944}
945
Peter Boström0c4e06b2015-10-07 12:23:21 +0200946void StunUInt16ListAttribute::SetType(int index, uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000947 (*attr_types_)[index] = value;
948}
949
Peter Boström0c4e06b2015-10-07 12:23:21 +0200950void StunUInt16ListAttribute::AddType(uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000951 attr_types_->push_back(value);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200952 SetLength(static_cast<uint16_t>(attr_types_->size() * 2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000953}
954
jbauchf1f87202016-03-30 06:43:37 -0700955bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000956 if (length() % 2)
957 return false;
958
959 for (size_t i = 0; i < length() / 2; i++) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200960 uint16_t attr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000961 if (!buf->ReadUInt16(&attr))
962 return false;
963 attr_types_->push_back(attr);
964 }
965 // Padding of these attributes is done in RFC 5389 style. This is
966 // slightly different from RFC3489, but it shouldn't be important.
967 // RFC3489 pads out to a 32 bit boundary by duplicating one of the
968 // entries in the list (not necessarily the last one - it's unspecified).
969 // RFC5389 pads on the end, and the bytes are always ignored.
970 ConsumePadding(buf);
971 return true;
972}
973
jbauchf1f87202016-03-30 06:43:37 -0700974bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000975 for (size_t i = 0; i < attr_types_->size(); ++i) {
976 buf->WriteUInt16((*attr_types_)[i]);
977 }
978 WritePadding(buf);
979 return true;
980}
981
982int GetStunSuccessResponseType(int req_type) {
983 return IsStunRequestType(req_type) ? (req_type | 0x100) : -1;
984}
985
986int GetStunErrorResponseType(int req_type) {
987 return IsStunRequestType(req_type) ? (req_type | 0x110) : -1;
988}
989
990bool IsStunRequestType(int msg_type) {
991 return ((msg_type & kStunTypeMask) == 0x000);
992}
993
994bool IsStunIndicationType(int msg_type) {
995 return ((msg_type & kStunTypeMask) == 0x010);
996}
997
998bool IsStunSuccessResponseType(int msg_type) {
999 return ((msg_type & kStunTypeMask) == 0x100);
1000}
1001
1002bool IsStunErrorResponseType(int msg_type) {
1003 return ((msg_type & kStunTypeMask) == 0x110);
1004}
1005
1006bool ComputeStunCredentialHash(const std::string& username,
1007 const std::string& realm,
1008 const std::string& password,
1009 std::string* hash) {
1010 // http://tools.ietf.org/html/rfc5389#section-15.4
1011 // long-term credentials will be calculated using the key and key is
1012 // key = MD5(username ":" realm ":" SASLprep(password))
1013 std::string input = username;
1014 input += ':';
1015 input += realm;
1016 input += ':';
1017 input += password;
1018
1019 char digest[rtc::MessageDigest::kMaxSize];
Yves Gerey665174f2018-06-19 15:03:05 +02001020 size_t size = rtc::ComputeDigest(rtc::DIGEST_MD5, input.c_str(), input.size(),
1021 digest, sizeof(digest));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001022 if (size == 0) {
1023 return false;
1024 }
1025
1026 *hash = std::string(digest, size);
1027 return true;
1028}
1029
Jonas Oreland202994c2017-12-18 12:10:43 +01001030std::unique_ptr<StunAttribute> CopyStunAttribute(
1031 const StunAttribute& attribute,
1032 rtc::ByteBufferWriter* tmp_buffer_ptr) {
1033 ByteBufferWriter tmpBuffer;
1034 if (tmp_buffer_ptr == nullptr) {
1035 tmp_buffer_ptr = &tmpBuffer;
1036 }
1037
Yves Gerey665174f2018-06-19 15:03:05 +02001038 std::unique_ptr<StunAttribute> copy(StunAttribute::Create(
1039 attribute.value_type(), attribute.type(),
1040 static_cast<uint16_t>(attribute.length()), nullptr));
Jonas Oreland202994c2017-12-18 12:10:43 +01001041
1042 if (!copy) {
1043 return nullptr;
1044 }
1045 tmp_buffer_ptr->Clear();
1046 if (!attribute.Write(tmp_buffer_ptr)) {
1047 return nullptr;
1048 }
1049 rtc::ByteBufferReader reader(*tmp_buffer_ptr);
1050 if (!copy->Read(&reader)) {
1051 return nullptr;
1052 }
1053
1054 return copy;
1055}
1056
Steve Antonca7d54e2017-10-25 14:42:51 -07001057StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const {
1058 switch (type) {
1059 case STUN_ATTR_LIFETIME:
1060 return STUN_VALUE_UINT32;
1061 case STUN_ATTR_MAGIC_COOKIE:
1062 return STUN_VALUE_BYTE_STRING;
1063 case STUN_ATTR_BANDWIDTH:
1064 return STUN_VALUE_UINT32;
1065 case STUN_ATTR_DESTINATION_ADDRESS:
1066 return STUN_VALUE_ADDRESS;
1067 case STUN_ATTR_SOURCE_ADDRESS2:
1068 return STUN_VALUE_ADDRESS;
1069 case STUN_ATTR_DATA:
1070 return STUN_VALUE_BYTE_STRING;
1071 case STUN_ATTR_OPTIONS:
1072 return STUN_VALUE_UINT32;
1073 default:
1074 return StunMessage::GetAttributeValueType(type);
1075 }
1076}
1077
1078StunMessage* RelayMessage::CreateNew() const {
1079 return new RelayMessage();
1080}
1081
1082StunAttributeValueType TurnMessage::GetAttributeValueType(int type) const {
1083 switch (type) {
1084 case STUN_ATTR_CHANNEL_NUMBER:
1085 return STUN_VALUE_UINT32;
1086 case STUN_ATTR_TURN_LIFETIME:
1087 return STUN_VALUE_UINT32;
1088 case STUN_ATTR_XOR_PEER_ADDRESS:
1089 return STUN_VALUE_XOR_ADDRESS;
1090 case STUN_ATTR_DATA:
1091 return STUN_VALUE_BYTE_STRING;
1092 case STUN_ATTR_XOR_RELAYED_ADDRESS:
1093 return STUN_VALUE_XOR_ADDRESS;
1094 case STUN_ATTR_EVEN_PORT:
1095 return STUN_VALUE_BYTE_STRING;
1096 case STUN_ATTR_REQUESTED_TRANSPORT:
1097 return STUN_VALUE_UINT32;
1098 case STUN_ATTR_DONT_FRAGMENT:
1099 return STUN_VALUE_BYTE_STRING;
1100 case STUN_ATTR_RESERVATION_TOKEN:
1101 return STUN_VALUE_BYTE_STRING;
1102 default:
1103 return StunMessage::GetAttributeValueType(type);
1104 }
1105}
1106
1107StunMessage* TurnMessage::CreateNew() const {
1108 return new TurnMessage();
1109}
1110
1111StunAttributeValueType IceMessage::GetAttributeValueType(int type) const {
1112 switch (type) {
1113 case STUN_ATTR_PRIORITY:
1114 case STUN_ATTR_NETWORK_INFO:
1115 case STUN_ATTR_NOMINATION:
1116 return STUN_VALUE_UINT32;
1117 case STUN_ATTR_USE_CANDIDATE:
1118 return STUN_VALUE_BYTE_STRING;
1119 case STUN_ATTR_ICE_CONTROLLED:
1120 return STUN_VALUE_UINT64;
1121 case STUN_ATTR_ICE_CONTROLLING:
1122 return STUN_VALUE_UINT64;
1123 default:
1124 return StunMessage::GetAttributeValueType(type);
1125 }
1126}
1127
1128StunMessage* IceMessage::CreateNew() const {
1129 return new IceMessage();
1130}
1131
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001132} // namespace cricket