blob: d2897747bb6253909f94371c2c7977a609f15ac6 [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"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/byte_order.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
22#include "rtc_base/crc32.h"
23#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/message_digest.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
jbauchf1f87202016-03-30 06:43:37 -070026using rtc::ByteBufferReader;
27using rtc::ByteBufferWriter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028
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
Andrew Royes154d8392019-03-14 10:38:31 -0700367 uint32_t magic_cookie_int;
368 static_assert(sizeof(magic_cookie_int) == kStunMagicCookieLength,
369 "Integer size mismatch: magic_cookie_int and kStunMagicCookie");
370 std::memcpy(&magic_cookie_int, magic_cookie.data(), sizeof(magic_cookie_int));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) {
372 // If magic cookie is invalid it means that the peer implements
373 // RFC3489 instead of RFC5389.
374 transaction_id.insert(0, magic_cookie);
375 }
nisseede5da42017-01-12 05:15:36 -0800376 RTC_DCHECK(IsValidTransactionId(transaction_id));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000377 transaction_id_ = transaction_id;
Zach Stein92c42892018-11-28 11:38:52 -0800378 reduced_transaction_id_ = ReduceTransactionId(transaction_id_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000379
380 if (length_ != buf->Length())
381 return false;
382
zsteinad94c4c2017-03-06 13:36:05 -0800383 attrs_.resize(0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000384
385 size_t rest = buf->Length() - length_;
386 while (buf->Length() > rest) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200387 uint16_t attr_type, attr_length;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 if (!buf->ReadUInt16(&attr_type))
389 return false;
390 if (!buf->ReadUInt16(&attr_length))
391 return false;
392
Honghai Zhang3e024302016-09-22 09:52:16 -0700393 std::unique_ptr<StunAttribute> attr(
394 CreateAttribute(attr_type, attr_length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000395 if (!attr) {
396 // Skip any unknown or malformed attributes.
397 if ((attr_length % 4) != 0) {
398 attr_length += (4 - (attr_length % 4));
399 }
400 if (!buf->Consume(attr_length))
401 return false;
402 } else {
403 if (!attr->Read(buf))
404 return false;
zsteinad94c4c2017-03-06 13:36:05 -0800405 attrs_.push_back(std::move(attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000406 }
407 }
408
nisseede5da42017-01-12 05:15:36 -0800409 RTC_DCHECK(buf->Length() == rest);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410 return true;
411}
412
jbauchf1f87202016-03-30 06:43:37 -0700413bool StunMessage::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000414 buf->WriteUInt16(type_);
415 buf->WriteUInt16(length_);
416 if (!IsLegacy())
Jonas Oreland7ca63112018-02-27 08:45:13 +0100417 buf->WriteUInt32(stun_magic_cookie_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 buf->WriteString(transaction_id_);
419
zsteinad94c4c2017-03-06 13:36:05 -0800420 for (const auto& attr : attrs_) {
421 buf->WriteUInt16(attr->type());
422 buf->WriteUInt16(static_cast<uint16_t>(attr->length()));
423 if (!attr->Write(buf)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000424 return false;
zsteinad94c4c2017-03-06 13:36:05 -0800425 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000426 }
427
428 return true;
429}
430
Steve Antonca7d54e2017-10-25 14:42:51 -0700431StunMessage* StunMessage::CreateNew() const {
432 return new StunMessage();
433}
434
Jonas Oreland7ca63112018-02-27 08:45:13 +0100435void StunMessage::SetStunMagicCookie(uint32_t val) {
436 stun_magic_cookie_ = val;
437}
438
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000439StunAttributeValueType StunMessage::GetAttributeValueType(int type) const {
440 switch (type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200441 case STUN_ATTR_MAPPED_ADDRESS:
442 return STUN_VALUE_ADDRESS;
443 case STUN_ATTR_USERNAME:
444 return STUN_VALUE_BYTE_STRING;
445 case STUN_ATTR_MESSAGE_INTEGRITY:
446 return STUN_VALUE_BYTE_STRING;
447 case STUN_ATTR_ERROR_CODE:
448 return STUN_VALUE_ERROR_CODE;
449 case STUN_ATTR_UNKNOWN_ATTRIBUTES:
450 return STUN_VALUE_UINT16_LIST;
451 case STUN_ATTR_REALM:
452 return STUN_VALUE_BYTE_STRING;
453 case STUN_ATTR_NONCE:
454 return STUN_VALUE_BYTE_STRING;
455 case STUN_ATTR_XOR_MAPPED_ADDRESS:
456 return STUN_VALUE_XOR_ADDRESS;
457 case STUN_ATTR_SOFTWARE:
458 return STUN_VALUE_BYTE_STRING;
459 case STUN_ATTR_ALTERNATE_SERVER:
460 return STUN_VALUE_ADDRESS;
461 case STUN_ATTR_FINGERPRINT:
462 return STUN_VALUE_UINT32;
463 case STUN_ATTR_ORIGIN:
464 return STUN_VALUE_BYTE_STRING;
465 case STUN_ATTR_RETRANSMIT_COUNT:
466 return STUN_VALUE_UINT32;
467 default:
468 return STUN_VALUE_UNKNOWN;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000469 }
470}
471
472StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ {
473 StunAttributeValueType value_type = GetAttributeValueType(type);
Jonas Orelandbdcee282017-10-10 14:01:40 +0200474 if (value_type != STUN_VALUE_UNKNOWN) {
475 return StunAttribute::Create(value_type, type,
476 static_cast<uint16_t>(length), this);
Jonas Oreland16ccef72018-03-27 09:02:43 +0200477 } else if (DesignatedExpertRange(type)) {
Jonas Orelandbdcee282017-10-10 14:01:40 +0200478 // Read unknown attributes as STUN_VALUE_BYTE_STRING
479 return StunAttribute::Create(STUN_VALUE_BYTE_STRING, type,
480 static_cast<uint16_t>(length), this);
481 } else {
482 return NULL;
483 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000484}
485
486const StunAttribute* StunMessage::GetAttribute(int type) const {
zsteinad94c4c2017-03-06 13:36:05 -0800487 for (const auto& attr : attrs_) {
488 if (attr->type() == type) {
489 return attr.get();
490 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000491 }
492 return NULL;
493}
494
495bool StunMessage::IsValidTransactionId(const std::string& transaction_id) {
496 return transaction_id.size() == kStunTransactionIdLength ||
Yves Gerey665174f2018-06-19 15:03:05 +0200497 transaction_id.size() == kStunLegacyTransactionIdLength;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000498}
499
500// StunAttribute
501
Peter Boström0c4e06b2015-10-07 12:23:21 +0200502StunAttribute::StunAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200503 : type_(type), length_(length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000504
jbauchf1f87202016-03-30 06:43:37 -0700505void StunAttribute::ConsumePadding(ByteBufferReader* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000506 int remainder = length_ % 4;
507 if (remainder > 0) {
508 buf->Consume(4 - remainder);
509 }
510}
511
jbauchf1f87202016-03-30 06:43:37 -0700512void StunAttribute::WritePadding(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000513 int remainder = length_ % 4;
514 if (remainder > 0) {
515 char zeroes[4] = {0};
516 buf->WriteBytes(zeroes, 4 - remainder);
517 }
518}
519
520StunAttribute* StunAttribute::Create(StunAttributeValueType value_type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200521 uint16_t type,
522 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000523 StunMessage* owner) {
524 switch (value_type) {
525 case STUN_VALUE_ADDRESS:
526 return new StunAddressAttribute(type, length);
527 case STUN_VALUE_XOR_ADDRESS:
528 return new StunXorAddressAttribute(type, length, owner);
529 case STUN_VALUE_UINT32:
530 return new StunUInt32Attribute(type);
531 case STUN_VALUE_UINT64:
532 return new StunUInt64Attribute(type);
533 case STUN_VALUE_BYTE_STRING:
534 return new StunByteStringAttribute(type, length);
535 case STUN_VALUE_ERROR_CODE:
536 return new StunErrorCodeAttribute(type, length);
537 case STUN_VALUE_UINT16_LIST:
538 return new StunUInt16ListAttribute(type, length);
539 default:
540 return NULL;
541 }
542}
543
zsteinf42cc9d2017-03-27 16:17:19 -0700544std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress(
545 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200546 return absl::make_unique<StunAddressAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000547}
548
zsteinf42cc9d2017-03-27 16:17:19 -0700549std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress(
550 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200551 return absl::make_unique<StunXorAddressAttribute>(type, 0, nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000552}
553
zsteinf42cc9d2017-03-27 16:17:19 -0700554std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64(
555 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200556 return absl::make_unique<StunUInt64Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000557}
558
zsteinf42cc9d2017-03-27 16:17:19 -0700559std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32(
560 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200561 return absl::make_unique<StunUInt32Attribute>(type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000562}
563
zsteinf42cc9d2017-03-27 16:17:19 -0700564std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString(
565 uint16_t type) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200566 return absl::make_unique<StunByteStringAttribute>(type, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000567}
568
zsteinf42cc9d2017-03-27 16:17:19 -0700569std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200570 return absl::make_unique<StunErrorCodeAttribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000571 STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE);
572}
573
zsteinf42cc9d2017-03-27 16:17:19 -0700574std::unique_ptr<StunUInt16ListAttribute>
575StunAttribute::CreateUnknownAttributes() {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200576 return absl::make_unique<StunUInt16ListAttribute>(
577 STUN_ATTR_UNKNOWN_ATTRIBUTES, 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000578}
579
Peter Boström0c4e06b2015-10-07 12:23:21 +0200580StunAddressAttribute::StunAddressAttribute(uint16_t type,
581 const rtc::SocketAddress& addr)
582 : StunAttribute(type, 0) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000583 SetAddress(addr);
584}
585
Peter Boström0c4e06b2015-10-07 12:23:21 +0200586StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200587 : StunAttribute(type, length) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000588
Steve Antonca7d54e2017-10-25 14:42:51 -0700589StunAttributeValueType StunAddressAttribute::value_type() const {
590 return STUN_VALUE_ADDRESS;
591}
592
jbauchf1f87202016-03-30 06:43:37 -0700593bool StunAddressAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200594 uint8_t dummy;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000595 if (!buf->ReadUInt8(&dummy))
596 return false;
597
Peter Boström0c4e06b2015-10-07 12:23:21 +0200598 uint8_t stun_family;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000599 if (!buf->ReadUInt8(&stun_family)) {
600 return false;
601 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200602 uint16_t port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000603 if (!buf->ReadUInt16(&port))
604 return false;
605 if (stun_family == STUN_ADDRESS_IPV4) {
606 in_addr v4addr;
607 if (length() != SIZE_IP4) {
608 return false;
609 }
610 if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) {
611 return false;
612 }
613 rtc::IPAddress ipaddr(v4addr);
614 SetAddress(rtc::SocketAddress(ipaddr, port));
615 } else if (stun_family == STUN_ADDRESS_IPV6) {
616 in6_addr v6addr;
617 if (length() != SIZE_IP6) {
618 return false;
619 }
620 if (!buf->ReadBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr))) {
621 return false;
622 }
623 rtc::IPAddress ipaddr(v6addr);
624 SetAddress(rtc::SocketAddress(ipaddr, port));
625 } else {
626 return false;
627 }
628 return true;
629}
630
jbauchf1f87202016-03-30 06:43:37 -0700631bool StunAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000632 StunAddressFamily address_family = family();
633 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100634 RTC_LOG(LS_ERROR) << "Error writing address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000635 return false;
636 }
637 buf->WriteUInt8(0);
638 buf->WriteUInt8(address_family);
639 buf->WriteUInt16(address_.port());
640 switch (address_.family()) {
641 case AF_INET: {
642 in_addr v4addr = address_.ipaddr().ipv4_address();
643 buf->WriteBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr));
644 break;
645 }
646 case AF_INET6: {
647 in6_addr v6addr = address_.ipaddr().ipv6_address();
648 buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr));
649 break;
650 }
651 }
652 return true;
653}
654
Peter Boström0c4e06b2015-10-07 12:23:21 +0200655StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
656 const rtc::SocketAddress& addr)
Yves Gerey665174f2018-06-19 15:03:05 +0200657 : StunAddressAttribute(type, addr), owner_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000658
Peter Boström0c4e06b2015-10-07 12:23:21 +0200659StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
660 uint16_t length,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000661 StunMessage* owner)
Yves Gerey665174f2018-06-19 15:03:05 +0200662 : StunAddressAttribute(type, length), owner_(owner) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000663
Steve Antonca7d54e2017-10-25 14:42:51 -0700664StunAttributeValueType StunXorAddressAttribute::value_type() const {
665 return STUN_VALUE_XOR_ADDRESS;
666}
667
668void StunXorAddressAttribute::SetOwner(StunMessage* owner) {
669 owner_ = owner;
670}
671
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000672rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const {
673 if (owner_) {
674 rtc::IPAddress ip = ipaddr();
675 switch (ip.family()) {
676 case AF_INET: {
677 in_addr v4addr = ip.ipv4_address();
678 v4addr.s_addr =
679 (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie));
680 return rtc::IPAddress(v4addr);
681 }
682 case AF_INET6: {
683 in6_addr v6addr = ip.ipv6_address();
684 const std::string& transaction_id = owner_->transaction_id();
685 if (transaction_id.length() == kStunTransactionIdLength) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200686 uint32_t transactionid_as_ints[3];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000687 memcpy(&transactionid_as_ints[0], transaction_id.c_str(),
688 transaction_id.length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200689 uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000690 // Transaction ID is in network byte order, but magic cookie
691 // is stored in host byte order.
692 ip_as_ints[0] =
693 (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie));
694 ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]);
695 ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]);
696 ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]);
697 return rtc::IPAddress(v6addr);
698 }
699 break;
700 }
701 }
702 }
703 // Invalid ip family or transaction ID, or missing owner.
704 // Return an AF_UNSPEC address.
705 return rtc::IPAddress();
706}
707
jbauchf1f87202016-03-30 06:43:37 -0700708bool StunXorAddressAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000709 if (!StunAddressAttribute::Read(buf))
710 return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200711 uint16_t xoredport = port() ^ (kStunMagicCookie >> 16);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000712 rtc::IPAddress xored_ip = GetXoredIP();
713 SetAddress(rtc::SocketAddress(xored_ip, xoredport));
714 return true;
715}
716
jbauchf1f87202016-03-30 06:43:37 -0700717bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000718 StunAddressFamily address_family = family();
719 if (address_family == STUN_ADDRESS_UNDEF) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100720 RTC_LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000721 return false;
722 }
723 rtc::IPAddress xored_ip = GetXoredIP();
724 if (xored_ip.family() == AF_UNSPEC) {
725 return false;
726 }
727 buf->WriteUInt8(0);
728 buf->WriteUInt8(family());
729 buf->WriteUInt16(port() ^ (kStunMagicCookie >> 16));
730 switch (xored_ip.family()) {
731 case AF_INET: {
732 in_addr v4addr = xored_ip.ipv4_address();
733 buf->WriteBytes(reinterpret_cast<const char*>(&v4addr), sizeof(v4addr));
734 break;
735 }
736 case AF_INET6: {
737 in6_addr v6addr = xored_ip.ipv6_address();
738 buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr));
739 break;
740 }
741 }
742 return true;
743}
744
Peter Boström0c4e06b2015-10-07 12:23:21 +0200745StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200746 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000747
Peter Boström0c4e06b2015-10-07 12:23:21 +0200748StunUInt32Attribute::StunUInt32Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200749 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000750
Steve Antonca7d54e2017-10-25 14:42:51 -0700751StunAttributeValueType StunUInt32Attribute::value_type() const {
752 return STUN_VALUE_UINT32;
753}
754
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000755bool StunUInt32Attribute::GetBit(size_t index) const {
nisseede5da42017-01-12 05:15:36 -0800756 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000757 return static_cast<bool>((bits_ >> index) & 0x1);
758}
759
760void StunUInt32Attribute::SetBit(size_t index, bool value) {
nisseede5da42017-01-12 05:15:36 -0800761 RTC_DCHECK(index < 32);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000762 bits_ &= ~(1 << index);
763 bits_ |= value ? (1 << index) : 0;
764}
765
jbauchf1f87202016-03-30 06:43:37 -0700766bool StunUInt32Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000767 if (length() != SIZE || !buf->ReadUInt32(&bits_))
768 return false;
769 return true;
770}
771
jbauchf1f87202016-03-30 06:43:37 -0700772bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000773 buf->WriteUInt32(bits_);
774 return true;
775}
776
Peter Boström0c4e06b2015-10-07 12:23:21 +0200777StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value)
Yves Gerey665174f2018-06-19 15:03:05 +0200778 : StunAttribute(type, SIZE), bits_(value) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000779
Peter Boström0c4e06b2015-10-07 12:23:21 +0200780StunUInt64Attribute::StunUInt64Attribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200781 : StunAttribute(type, SIZE), bits_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000782
Steve Antonca7d54e2017-10-25 14:42:51 -0700783StunAttributeValueType StunUInt64Attribute::value_type() const {
784 return STUN_VALUE_UINT64;
785}
786
jbauchf1f87202016-03-30 06:43:37 -0700787bool StunUInt64Attribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000788 if (length() != SIZE || !buf->ReadUInt64(&bits_))
789 return false;
790 return true;
791}
792
jbauchf1f87202016-03-30 06:43:37 -0700793bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000794 buf->WriteUInt64(bits_);
795 return true;
796}
797
Peter Boström0c4e06b2015-10-07 12:23:21 +0200798StunByteStringAttribute::StunByteStringAttribute(uint16_t type)
Yves Gerey665174f2018-06-19 15:03:05 +0200799 : StunAttribute(type, 0), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000800
Peter Boström0c4e06b2015-10-07 12:23:21 +0200801StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000802 const std::string& str)
803 : StunAttribute(type, 0), bytes_(NULL) {
804 CopyBytes(str.c_str(), str.size());
805}
806
Peter Boström0c4e06b2015-10-07 12:23:21 +0200807StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000808 const void* bytes,
809 size_t length)
810 : StunAttribute(type, 0), bytes_(NULL) {
811 CopyBytes(bytes, length);
812}
813
Peter Boström0c4e06b2015-10-07 12:23:21 +0200814StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200815 : StunAttribute(type, length), bytes_(NULL) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000816
817StunByteStringAttribute::~StunByteStringAttribute() {
Yves Gerey665174f2018-06-19 15:03:05 +0200818 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000819}
820
Steve Antonca7d54e2017-10-25 14:42:51 -0700821StunAttributeValueType StunByteStringAttribute::value_type() const {
822 return STUN_VALUE_BYTE_STRING;
823}
824
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000825void StunByteStringAttribute::CopyBytes(const char* bytes) {
826 CopyBytes(bytes, strlen(bytes));
827}
828
829void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) {
830 char* new_bytes = new char[length];
831 memcpy(new_bytes, bytes, length);
832 SetBytes(new_bytes, length);
833}
834
Peter Boström0c4e06b2015-10-07 12:23:21 +0200835uint8_t StunByteStringAttribute::GetByte(size_t index) const {
nisseede5da42017-01-12 05:15:36 -0800836 RTC_DCHECK(bytes_ != NULL);
837 RTC_DCHECK(index < length());
Peter Boström0c4e06b2015-10-07 12:23:21 +0200838 return static_cast<uint8_t>(bytes_[index]);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000839}
840
Peter Boström0c4e06b2015-10-07 12:23:21 +0200841void StunByteStringAttribute::SetByte(size_t index, uint8_t value) {
nisseede5da42017-01-12 05:15:36 -0800842 RTC_DCHECK(bytes_ != NULL);
843 RTC_DCHECK(index < length());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000844 bytes_[index] = value;
845}
846
jbauchf1f87202016-03-30 06:43:37 -0700847bool StunByteStringAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000848 bytes_ = new char[length()];
849 if (!buf->ReadBytes(bytes_, length())) {
850 return false;
851 }
852
853 ConsumePadding(buf);
854 return true;
855}
856
jbauchf1f87202016-03-30 06:43:37 -0700857bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000858 buf->WriteBytes(bytes_, length());
859 WritePadding(buf);
860 return true;
861}
862
863void StunByteStringAttribute::SetBytes(char* bytes, size_t length) {
Yves Gerey665174f2018-06-19 15:03:05 +0200864 delete[] bytes_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000865 bytes_ = bytes;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200866 SetLength(static_cast<uint16_t>(length));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000867}
868
zsteinf42cc9d2017-03-27 16:17:19 -0700869const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4;
870
Peter Boström0c4e06b2015-10-07 12:23:21 +0200871StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type,
872 int code,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873 const std::string& reason)
874 : StunAttribute(type, 0) {
875 SetCode(code);
876 SetReason(reason);
877}
878
Peter Boström0c4e06b2015-10-07 12:23:21 +0200879StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length)
Yves Gerey665174f2018-06-19 15:03:05 +0200880 : StunAttribute(type, length), class_(0), number_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000881
Yves Gerey665174f2018-06-19 15:03:05 +0200882StunErrorCodeAttribute::~StunErrorCodeAttribute() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000883
Steve Antonca7d54e2017-10-25 14:42:51 -0700884StunAttributeValueType StunErrorCodeAttribute::value_type() const {
885 return STUN_VALUE_ERROR_CODE;
886}
887
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000888int StunErrorCodeAttribute::code() const {
889 return class_ * 100 + number_;
890}
891
892void StunErrorCodeAttribute::SetCode(int code) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200893 class_ = static_cast<uint8_t>(code / 100);
894 number_ = static_cast<uint8_t>(code % 100);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895}
896
897void StunErrorCodeAttribute::SetReason(const std::string& reason) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200898 SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000899 reason_ = reason;
900}
901
jbauchf1f87202016-03-30 06:43:37 -0700902bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200903 uint32_t val;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000904 if (length() < MIN_SIZE || !buf->ReadUInt32(&val))
905 return false;
906
907 if ((val >> 11) != 0)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100908 RTC_LOG(LS_ERROR) << "error-code bits not zero";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000909
910 class_ = ((val >> 8) & 0x7);
911 number_ = (val & 0xff);
912
913 if (!buf->ReadString(&reason_, length() - 4))
914 return false;
915
916 ConsumePadding(buf);
917 return true;
918}
919
jbauchf1f87202016-03-30 06:43:37 -0700920bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000921 buf->WriteUInt32(class_ << 8 | number_);
922 buf->WriteString(reason_);
923 WritePadding(buf);
924 return true;
925}
926
Peter Boström0c4e06b2015-10-07 12:23:21 +0200927StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000928 : StunAttribute(type, length) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200929 attr_types_ = new std::vector<uint16_t>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000930}
931
932StunUInt16ListAttribute::~StunUInt16ListAttribute() {
933 delete attr_types_;
934}
935
Steve Antonca7d54e2017-10-25 14:42:51 -0700936StunAttributeValueType StunUInt16ListAttribute::value_type() const {
937 return STUN_VALUE_UINT16_LIST;
938}
939
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000940size_t StunUInt16ListAttribute::Size() const {
941 return attr_types_->size();
942}
943
Peter Boström0c4e06b2015-10-07 12:23:21 +0200944uint16_t StunUInt16ListAttribute::GetType(int index) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000945 return (*attr_types_)[index];
946}
947
Peter Boström0c4e06b2015-10-07 12:23:21 +0200948void StunUInt16ListAttribute::SetType(int index, uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000949 (*attr_types_)[index] = value;
950}
951
Peter Boström0c4e06b2015-10-07 12:23:21 +0200952void StunUInt16ListAttribute::AddType(uint16_t value) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000953 attr_types_->push_back(value);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200954 SetLength(static_cast<uint16_t>(attr_types_->size() * 2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000955}
956
jbauchf1f87202016-03-30 06:43:37 -0700957bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000958 if (length() % 2)
959 return false;
960
961 for (size_t i = 0; i < length() / 2; i++) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200962 uint16_t attr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000963 if (!buf->ReadUInt16(&attr))
964 return false;
965 attr_types_->push_back(attr);
966 }
967 // Padding of these attributes is done in RFC 5389 style. This is
968 // slightly different from RFC3489, but it shouldn't be important.
969 // RFC3489 pads out to a 32 bit boundary by duplicating one of the
970 // entries in the list (not necessarily the last one - it's unspecified).
971 // RFC5389 pads on the end, and the bytes are always ignored.
972 ConsumePadding(buf);
973 return true;
974}
975
jbauchf1f87202016-03-30 06:43:37 -0700976bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000977 for (size_t i = 0; i < attr_types_->size(); ++i) {
978 buf->WriteUInt16((*attr_types_)[i]);
979 }
980 WritePadding(buf);
981 return true;
982}
983
984int GetStunSuccessResponseType(int req_type) {
985 return IsStunRequestType(req_type) ? (req_type | 0x100) : -1;
986}
987
988int GetStunErrorResponseType(int req_type) {
989 return IsStunRequestType(req_type) ? (req_type | 0x110) : -1;
990}
991
992bool IsStunRequestType(int msg_type) {
993 return ((msg_type & kStunTypeMask) == 0x000);
994}
995
996bool IsStunIndicationType(int msg_type) {
997 return ((msg_type & kStunTypeMask) == 0x010);
998}
999
1000bool IsStunSuccessResponseType(int msg_type) {
1001 return ((msg_type & kStunTypeMask) == 0x100);
1002}
1003
1004bool IsStunErrorResponseType(int msg_type) {
1005 return ((msg_type & kStunTypeMask) == 0x110);
1006}
1007
1008bool ComputeStunCredentialHash(const std::string& username,
1009 const std::string& realm,
1010 const std::string& password,
1011 std::string* hash) {
1012 // http://tools.ietf.org/html/rfc5389#section-15.4
1013 // long-term credentials will be calculated using the key and key is
1014 // key = MD5(username ":" realm ":" SASLprep(password))
1015 std::string input = username;
1016 input += ':';
1017 input += realm;
1018 input += ':';
1019 input += password;
1020
1021 char digest[rtc::MessageDigest::kMaxSize];
Yves Gerey665174f2018-06-19 15:03:05 +02001022 size_t size = rtc::ComputeDigest(rtc::DIGEST_MD5, input.c_str(), input.size(),
1023 digest, sizeof(digest));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001024 if (size == 0) {
1025 return false;
1026 }
1027
1028 *hash = std::string(digest, size);
1029 return true;
1030}
1031
Jonas Oreland202994c2017-12-18 12:10:43 +01001032std::unique_ptr<StunAttribute> CopyStunAttribute(
1033 const StunAttribute& attribute,
1034 rtc::ByteBufferWriter* tmp_buffer_ptr) {
1035 ByteBufferWriter tmpBuffer;
1036 if (tmp_buffer_ptr == nullptr) {
1037 tmp_buffer_ptr = &tmpBuffer;
1038 }
1039
Yves Gerey665174f2018-06-19 15:03:05 +02001040 std::unique_ptr<StunAttribute> copy(StunAttribute::Create(
1041 attribute.value_type(), attribute.type(),
1042 static_cast<uint16_t>(attribute.length()), nullptr));
Jonas Oreland202994c2017-12-18 12:10:43 +01001043
1044 if (!copy) {
1045 return nullptr;
1046 }
1047 tmp_buffer_ptr->Clear();
1048 if (!attribute.Write(tmp_buffer_ptr)) {
1049 return nullptr;
1050 }
1051 rtc::ByteBufferReader reader(*tmp_buffer_ptr);
1052 if (!copy->Read(&reader)) {
1053 return nullptr;
1054 }
1055
1056 return copy;
1057}
1058
Steve Antonca7d54e2017-10-25 14:42:51 -07001059StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const {
1060 switch (type) {
1061 case STUN_ATTR_LIFETIME:
1062 return STUN_VALUE_UINT32;
1063 case STUN_ATTR_MAGIC_COOKIE:
1064 return STUN_VALUE_BYTE_STRING;
1065 case STUN_ATTR_BANDWIDTH:
1066 return STUN_VALUE_UINT32;
1067 case STUN_ATTR_DESTINATION_ADDRESS:
1068 return STUN_VALUE_ADDRESS;
1069 case STUN_ATTR_SOURCE_ADDRESS2:
1070 return STUN_VALUE_ADDRESS;
1071 case STUN_ATTR_DATA:
1072 return STUN_VALUE_BYTE_STRING;
1073 case STUN_ATTR_OPTIONS:
1074 return STUN_VALUE_UINT32;
1075 default:
1076 return StunMessage::GetAttributeValueType(type);
1077 }
1078}
1079
1080StunMessage* RelayMessage::CreateNew() const {
1081 return new RelayMessage();
1082}
1083
1084StunAttributeValueType TurnMessage::GetAttributeValueType(int type) const {
1085 switch (type) {
1086 case STUN_ATTR_CHANNEL_NUMBER:
1087 return STUN_VALUE_UINT32;
1088 case STUN_ATTR_TURN_LIFETIME:
1089 return STUN_VALUE_UINT32;
1090 case STUN_ATTR_XOR_PEER_ADDRESS:
1091 return STUN_VALUE_XOR_ADDRESS;
1092 case STUN_ATTR_DATA:
1093 return STUN_VALUE_BYTE_STRING;
1094 case STUN_ATTR_XOR_RELAYED_ADDRESS:
1095 return STUN_VALUE_XOR_ADDRESS;
1096 case STUN_ATTR_EVEN_PORT:
1097 return STUN_VALUE_BYTE_STRING;
1098 case STUN_ATTR_REQUESTED_TRANSPORT:
1099 return STUN_VALUE_UINT32;
1100 case STUN_ATTR_DONT_FRAGMENT:
1101 return STUN_VALUE_BYTE_STRING;
1102 case STUN_ATTR_RESERVATION_TOKEN:
1103 return STUN_VALUE_BYTE_STRING;
1104 default:
1105 return StunMessage::GetAttributeValueType(type);
1106 }
1107}
1108
1109StunMessage* TurnMessage::CreateNew() const {
1110 return new TurnMessage();
1111}
1112
1113StunAttributeValueType IceMessage::GetAttributeValueType(int type) const {
1114 switch (type) {
1115 case STUN_ATTR_PRIORITY:
1116 case STUN_ATTR_NETWORK_INFO:
1117 case STUN_ATTR_NOMINATION:
1118 return STUN_VALUE_UINT32;
1119 case STUN_ATTR_USE_CANDIDATE:
1120 return STUN_VALUE_BYTE_STRING;
1121 case STUN_ATTR_ICE_CONTROLLED:
1122 return STUN_VALUE_UINT64;
1123 case STUN_ATTR_ICE_CONTROLLING:
1124 return STUN_VALUE_UINT64;
1125 default:
1126 return StunMessage::GetAttributeValueType(type);
1127 }
1128}
1129
1130StunMessage* IceMessage::CreateNew() const {
1131 return new IceMessage();
1132}
1133
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001134} // namespace cricket