henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "p2p/base/stun.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
| 14 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | #include <algorithm> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 16 | #include <memory> |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 17 | #include <utility> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 18 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 19 | #include "absl/memory/memory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/byte_order.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/crc32.h" |
| 23 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/message_digest.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 25 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 26 | using rtc::ByteBufferReader; |
| 27 | using rtc::ByteBufferWriter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 28 | |
Zach Stein | 92c4289 | 2018-11-28 11:38:52 -0800 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | uint32_t ReduceTransactionId(const std::string& transaction_id) { |
| 32 | RTC_DCHECK(transaction_id.length() == cricket::kStunTransactionIdLength || |
| 33 | transaction_id.length() == |
| 34 | cricket::kStunLegacyTransactionIdLength); |
Zach Stein | ff71a49 | 2018-12-07 11:25:12 -0800 | [diff] [blame] | 35 | ByteBufferReader reader(transaction_id.c_str(), transaction_id.length(), |
| 36 | rtc::ByteBuffer::ORDER_NETWORK); |
Zach Stein | 92c4289 | 2018-11-28 11:38:52 -0800 | [diff] [blame] | 37 | uint32_t result = 0; |
Zach Stein | ff71a49 | 2018-12-07 11:25:12 -0800 | [diff] [blame] | 38 | uint32_t next; |
| 39 | while (reader.ReadUInt32(&next)) { |
| 40 | result ^= next; |
Zach Stein | 92c4289 | 2018-11-28 11:38:52 -0800 | [diff] [blame] | 41 | } |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | } // namespace |
| 46 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 47 | namespace cricket { |
| 48 | |
| 49 | const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server"; |
| 50 | const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request"; |
| 51 | const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized"; |
| 52 | const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden"; |
| 53 | const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials"; |
| 54 | const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch"; |
| 55 | const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce"; |
| 56 | const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials"; |
| 57 | const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol"; |
| 58 | const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict"; |
| 59 | const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error"; |
| 60 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 61 | const char TURN_MAGIC_COOKIE_VALUE[] = {'\x72', '\xC6', '\x4B', '\xC6'}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | const char EMPTY_TRANSACTION_ID[] = "0000000000000000"; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 63 | const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E; |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 64 | const int SERVER_NOT_REACHABLE_ERROR = 701; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | |
| 66 | // StunMessage |
| 67 | |
| 68 | StunMessage::StunMessage() |
| 69 | : type_(0), |
| 70 | length_(0), |
Jonas Oreland | 7ca6311 | 2018-02-27 08:45:13 +0100 | [diff] [blame] | 71 | transaction_id_(EMPTY_TRANSACTION_ID), |
| 72 | stun_magic_cookie_(kStunMagicCookie) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 73 | RTC_DCHECK(IsValidTransactionId(transaction_id_)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 76 | StunMessage::~StunMessage() = default; |
| 77 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 78 | bool StunMessage::IsLegacy() const { |
| 79 | if (transaction_id_.size() == kStunLegacyTransactionIdLength) |
| 80 | return true; |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 81 | RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
| 85 | bool StunMessage::SetTransactionID(const std::string& str) { |
| 86 | if (!IsValidTransactionId(str)) { |
| 87 | return false; |
| 88 | } |
| 89 | transaction_id_ = str; |
Zach Stein | 92c4289 | 2018-11-28 11:38:52 -0800 | [diff] [blame] | 90 | reduced_transaction_id_ = ReduceTransactionId(transaction_id_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
Jonas Oreland | 16ccef7 | 2018-03-27 09:02:43 +0200 | [diff] [blame] | 94 | static bool DesignatedExpertRange(int attr_type) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 95 | return (attr_type >= 0x4000 && attr_type <= 0x7FFF) || |
| 96 | (attr_type >= 0xC000 && attr_type <= 0xFFFF); |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 97 | } |
| 98 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 99 | void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) { |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 100 | // Fail any attributes that aren't valid for this type of message, |
Jonas Oreland | 16ccef7 | 2018-03-27 09:02:43 +0200 | [diff] [blame] | 101 | // but allow any type for the range that in the RFC is reserved for |
| 102 | // the "designated experts". |
| 103 | if (!DesignatedExpertRange(attr->type())) { |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 104 | RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type())); |
| 105 | } |
nisse | cc99bc2 | 2017-02-02 01:31:30 -0800 | [diff] [blame] | 106 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | attr->SetOwner(this); |
| 108 | size_t attr_length = attr->length(); |
| 109 | if (attr_length % 4 != 0) { |
| 110 | attr_length += (4 - (attr_length % 4)); |
| 111 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 112 | length_ += static_cast<uint16_t>(attr_length + 4); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 113 | |
| 114 | attrs_.push_back(std::move(attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 117 | std::unique_ptr<StunAttribute> StunMessage::RemoveAttribute(int type) { |
| 118 | std::unique_ptr<StunAttribute> attribute; |
| 119 | for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | if ((*it)->type() == type) { |
| 121 | attribute = std::move(*it); |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 122 | attrs_.erase(std::next(it).base()); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | if (attribute) { |
| 127 | attribute->SetOwner(nullptr); |
| 128 | size_t attr_length = attribute->length(); |
| 129 | if (attr_length % 4 != 0) { |
| 130 | attr_length += (4 - (attr_length % 4)); |
| 131 | } |
| 132 | length_ -= static_cast<uint16_t>(attr_length + 4); |
| 133 | } |
| 134 | return attribute; |
| 135 | } |
| 136 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 137 | const StunAddressAttribute* StunMessage::GetAddress(int type) const { |
| 138 | switch (type) { |
| 139 | case STUN_ATTR_MAPPED_ADDRESS: { |
| 140 | // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is |
| 141 | // missing. |
| 142 | const StunAttribute* mapped_address = |
| 143 | GetAttribute(STUN_ATTR_MAPPED_ADDRESS); |
| 144 | if (!mapped_address) |
| 145 | mapped_address = GetAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 146 | return reinterpret_cast<const StunAddressAttribute*>(mapped_address); |
| 147 | } |
| 148 | |
| 149 | default: |
| 150 | return static_cast<const StunAddressAttribute*>(GetAttribute(type)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | const StunUInt32Attribute* StunMessage::GetUInt32(int type) const { |
| 155 | return static_cast<const StunUInt32Attribute*>(GetAttribute(type)); |
| 156 | } |
| 157 | |
| 158 | const StunUInt64Attribute* StunMessage::GetUInt64(int type) const { |
| 159 | return static_cast<const StunUInt64Attribute*>(GetAttribute(type)); |
| 160 | } |
| 161 | |
| 162 | const StunByteStringAttribute* StunMessage::GetByteString(int type) const { |
| 163 | return static_cast<const StunByteStringAttribute*>(GetAttribute(type)); |
| 164 | } |
| 165 | |
| 166 | const StunErrorCodeAttribute* StunMessage::GetErrorCode() const { |
| 167 | return static_cast<const StunErrorCodeAttribute*>( |
| 168 | GetAttribute(STUN_ATTR_ERROR_CODE)); |
| 169 | } |
| 170 | |
deadbeef | 996fc6b | 2017-04-26 09:21:22 -0700 | [diff] [blame] | 171 | int StunMessage::GetErrorCodeValue() const { |
| 172 | const StunErrorCodeAttribute* error_attribute = GetErrorCode(); |
| 173 | return error_attribute ? error_attribute->code() : STUN_ERROR_GLOBAL_FAILURE; |
| 174 | } |
| 175 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | const StunUInt16ListAttribute* StunMessage::GetUnknownAttributes() const { |
| 177 | return static_cast<const StunUInt16ListAttribute*>( |
| 178 | GetAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES)); |
| 179 | } |
| 180 | |
| 181 | // Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the |
| 182 | // procedure outlined in RFC 5389, section 15.4. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 183 | bool StunMessage::ValidateMessageIntegrity(const char* data, |
| 184 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | const std::string& password) { |
| 186 | // Verifying the size of the message. |
katrielc | e4bda24 | 2016-06-09 08:45:45 -0700 | [diff] [blame] | 187 | if ((size % 4) != 0 || size < kStunHeaderSize) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // Getting the message length from the STUN header. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 192 | uint16_t msg_length = rtc::GetBE16(&data[2]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 193 | if (size != (msg_length + kStunHeaderSize)) { |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | // Finding Message Integrity attribute in stun message. |
| 198 | size_t current_pos = kStunHeaderSize; |
| 199 | bool has_message_integrity_attr = false; |
katrielc | 1a20610 | 2016-06-20 05:13:16 -0700 | [diff] [blame] | 200 | while (current_pos + 4 <= size) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 201 | uint16_t attr_type, attr_length; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 202 | // Getting attribute type and length. |
| 203 | attr_type = rtc::GetBE16(&data[current_pos]); |
| 204 | attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]); |
| 205 | |
| 206 | // If M-I, sanity check it, and break out. |
| 207 | if (attr_type == STUN_ATTR_MESSAGE_INTEGRITY) { |
| 208 | if (attr_length != kStunMessageIntegritySize || |
katrielc | 1a20610 | 2016-06-20 05:13:16 -0700 | [diff] [blame] | 209 | current_pos + sizeof(attr_type) + sizeof(attr_length) + attr_length > |
| 210 | size) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | has_message_integrity_attr = true; |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | // Otherwise, skip to the next attribute. |
| 218 | current_pos += sizeof(attr_type) + sizeof(attr_length) + attr_length; |
| 219 | if ((attr_length % 4) != 0) { |
| 220 | current_pos += (4 - (attr_length % 4)); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (!has_message_integrity_attr) { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | // Getting length of the message to calculate Message Integrity. |
| 229 | size_t mi_pos = current_pos; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 230 | std::unique_ptr<char[]> temp_data(new char[current_pos]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 231 | memcpy(temp_data.get(), data, current_pos); |
| 232 | if (size > mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize) { |
| 233 | // Stun message has other attributes after message integrity. |
| 234 | // Adjust the length parameter in stun message to calculate HMAC. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 235 | size_t extra_offset = |
| 236 | size - (mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 237 | size_t new_adjusted_len = size - extra_offset - kStunHeaderSize; |
| 238 | |
| 239 | // Writing new length of the STUN message @ Message Length in temp buffer. |
| 240 | // 0 1 2 3 |
| 241 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 242 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 243 | // |0 0| STUN Message Type | Message Length | |
| 244 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 245 | rtc::SetBE16(temp_data.get() + 2, static_cast<uint16_t>(new_adjusted_len)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | char hmac[kStunMessageIntegritySize]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 249 | size_t ret = |
| 250 | rtc::ComputeHmac(rtc::DIGEST_SHA_1, password.c_str(), password.size(), |
| 251 | temp_data.get(), mi_pos, hmac, sizeof(hmac)); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 252 | RTC_DCHECK(ret == sizeof(hmac)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 253 | if (ret != sizeof(hmac)) |
| 254 | return false; |
| 255 | |
| 256 | // Comparing the calculated HMAC with the one present in the message. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 257 | return memcmp(data + current_pos + kStunAttributeHeaderSize, hmac, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 258 | sizeof(hmac)) == 0; |
| 259 | } |
| 260 | |
| 261 | bool StunMessage::AddMessageIntegrity(const std::string& password) { |
| 262 | return AddMessageIntegrity(password.c_str(), password.size()); |
| 263 | } |
| 264 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 265 | bool StunMessage::AddMessageIntegrity(const char* key, size_t keylen) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 266 | // Add the attribute with a dummy value. Since this is a known attribute, it |
| 267 | // can't fail. |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 268 | auto msg_integrity_attr_ptr = absl::make_unique<StunByteStringAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 269 | STUN_ATTR_MESSAGE_INTEGRITY, std::string(kStunMessageIntegritySize, '0')); |
| 270 | auto* msg_integrity_attr = msg_integrity_attr_ptr.get(); |
| 271 | AddAttribute(std::move(msg_integrity_attr_ptr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 272 | |
| 273 | // Calculate the HMAC for the message. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 274 | ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 275 | if (!Write(&buf)) |
| 276 | return false; |
| 277 | |
| 278 | int msg_len_for_hmac = static_cast<int>( |
| 279 | buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length()); |
| 280 | char hmac[kStunMessageIntegritySize]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 281 | size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, key, keylen, buf.Data(), |
| 282 | msg_len_for_hmac, hmac, sizeof(hmac)); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 283 | RTC_DCHECK(ret == sizeof(hmac)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 284 | if (ret != sizeof(hmac)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 285 | RTC_LOG(LS_ERROR) << "HMAC computation failed. Message-Integrity " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 286 | "has dummy value."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
| 290 | // Insert correct HMAC into the attribute. |
| 291 | msg_integrity_attr->CopyBytes(hmac, sizeof(hmac)); |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | // Verifies a message is in fact a STUN message, by performing the checks |
| 296 | // outlined in RFC 5389, section 7.3, including the FINGERPRINT check detailed |
| 297 | // in section 15.5. |
| 298 | bool StunMessage::ValidateFingerprint(const char* data, size_t size) { |
| 299 | // Check the message length. |
| 300 | size_t fingerprint_attr_size = |
| 301 | kStunAttributeHeaderSize + StunUInt32Attribute::SIZE; |
| 302 | if (size % 4 != 0 || size < kStunHeaderSize + fingerprint_attr_size) |
| 303 | return false; |
| 304 | |
| 305 | // Skip the rest if the magic cookie isn't present. |
| 306 | const char* magic_cookie = |
| 307 | data + kStunTransactionIdOffset - kStunMagicCookieLength; |
| 308 | if (rtc::GetBE32(magic_cookie) != kStunMagicCookie) |
| 309 | return false; |
| 310 | |
| 311 | // Check the fingerprint type and length. |
| 312 | const char* fingerprint_attr_data = data + size - fingerprint_attr_size; |
| 313 | if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT || |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 314 | rtc::GetBE16(fingerprint_attr_data + sizeof(uint16_t)) != |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | StunUInt32Attribute::SIZE) |
| 316 | return false; |
| 317 | |
| 318 | // Check the fingerprint value. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 319 | uint32_t fingerprint = |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 320 | rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize); |
| 321 | return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) == |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 322 | rtc::ComputeCrc32(data, size - fingerprint_attr_size)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | bool StunMessage::AddFingerprint() { |
| 326 | // Add the attribute with a dummy value. Since this is a known attribute, |
| 327 | // it can't fail. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 328 | auto fingerprint_attr_ptr = |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 329 | absl::make_unique<StunUInt32Attribute>(STUN_ATTR_FINGERPRINT, 0); |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 330 | auto* fingerprint_attr = fingerprint_attr_ptr.get(); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 331 | AddAttribute(std::move(fingerprint_attr_ptr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 332 | |
| 333 | // Calculate the CRC-32 for the message and insert it. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 334 | ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 335 | if (!Write(&buf)) |
| 336 | return false; |
| 337 | |
| 338 | int msg_len_for_crc32 = static_cast<int>( |
| 339 | buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 340 | uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 341 | |
| 342 | // Insert the correct CRC-32, XORed with a constant, into the attribute. |
| 343 | fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE); |
| 344 | return true; |
| 345 | } |
| 346 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 347 | bool StunMessage::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 348 | if (!buf->ReadUInt16(&type_)) |
| 349 | return false; |
| 350 | |
| 351 | if (type_ & 0x8000) { |
| 352 | // RTP and RTCP set the MSB of first byte, since first two bits are version, |
| 353 | // and version is always 2 (10). If set, this is not a STUN packet. |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | if (!buf->ReadUInt16(&length_)) |
| 358 | return false; |
| 359 | |
| 360 | std::string magic_cookie; |
| 361 | if (!buf->ReadString(&magic_cookie, kStunMagicCookieLength)) |
| 362 | return false; |
| 363 | |
| 364 | std::string transaction_id; |
| 365 | if (!buf->ReadString(&transaction_id, kStunTransactionIdLength)) |
| 366 | return false; |
| 367 | |
Andrew Royes | 154d839 | 2019-03-14 10:38:31 -0700 | [diff] [blame] | 368 | uint32_t magic_cookie_int; |
| 369 | static_assert(sizeof(magic_cookie_int) == kStunMagicCookieLength, |
| 370 | "Integer size mismatch: magic_cookie_int and kStunMagicCookie"); |
| 371 | std::memcpy(&magic_cookie_int, magic_cookie.data(), sizeof(magic_cookie_int)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 372 | if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) { |
| 373 | // If magic cookie is invalid it means that the peer implements |
| 374 | // RFC3489 instead of RFC5389. |
| 375 | transaction_id.insert(0, magic_cookie); |
| 376 | } |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 377 | RTC_DCHECK(IsValidTransactionId(transaction_id)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 378 | transaction_id_ = transaction_id; |
Zach Stein | 92c4289 | 2018-11-28 11:38:52 -0800 | [diff] [blame] | 379 | reduced_transaction_id_ = ReduceTransactionId(transaction_id_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 380 | |
| 381 | if (length_ != buf->Length()) |
| 382 | return false; |
| 383 | |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 384 | attrs_.resize(0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 385 | |
| 386 | size_t rest = buf->Length() - length_; |
| 387 | while (buf->Length() > rest) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 388 | uint16_t attr_type, attr_length; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 389 | if (!buf->ReadUInt16(&attr_type)) |
| 390 | return false; |
| 391 | if (!buf->ReadUInt16(&attr_length)) |
| 392 | return false; |
| 393 | |
Honghai Zhang | 3e02430 | 2016-09-22 09:52:16 -0700 | [diff] [blame] | 394 | std::unique_ptr<StunAttribute> attr( |
| 395 | CreateAttribute(attr_type, attr_length)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 396 | if (!attr) { |
| 397 | // Skip any unknown or malformed attributes. |
| 398 | if ((attr_length % 4) != 0) { |
| 399 | attr_length += (4 - (attr_length % 4)); |
| 400 | } |
| 401 | if (!buf->Consume(attr_length)) |
| 402 | return false; |
| 403 | } else { |
| 404 | if (!attr->Read(buf)) |
| 405 | return false; |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 406 | attrs_.push_back(std::move(attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 410 | RTC_DCHECK(buf->Length() == rest); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 414 | bool StunMessage::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 415 | buf->WriteUInt16(type_); |
| 416 | buf->WriteUInt16(length_); |
| 417 | if (!IsLegacy()) |
Jonas Oreland | 7ca6311 | 2018-02-27 08:45:13 +0100 | [diff] [blame] | 418 | buf->WriteUInt32(stun_magic_cookie_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 419 | buf->WriteString(transaction_id_); |
| 420 | |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 421 | for (const auto& attr : attrs_) { |
| 422 | buf->WriteUInt16(attr->type()); |
| 423 | buf->WriteUInt16(static_cast<uint16_t>(attr->length())); |
| 424 | if (!attr->Write(buf)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 425 | return false; |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 426 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | return true; |
| 430 | } |
| 431 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 432 | StunMessage* StunMessage::CreateNew() const { |
| 433 | return new StunMessage(); |
| 434 | } |
| 435 | |
Jonas Oreland | 7ca6311 | 2018-02-27 08:45:13 +0100 | [diff] [blame] | 436 | void StunMessage::SetStunMagicCookie(uint32_t val) { |
| 437 | stun_magic_cookie_ = val; |
| 438 | } |
| 439 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 440 | StunAttributeValueType StunMessage::GetAttributeValueType(int type) const { |
| 441 | switch (type) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 442 | case STUN_ATTR_MAPPED_ADDRESS: |
| 443 | return STUN_VALUE_ADDRESS; |
| 444 | case STUN_ATTR_USERNAME: |
| 445 | return STUN_VALUE_BYTE_STRING; |
| 446 | case STUN_ATTR_MESSAGE_INTEGRITY: |
| 447 | return STUN_VALUE_BYTE_STRING; |
| 448 | case STUN_ATTR_ERROR_CODE: |
| 449 | return STUN_VALUE_ERROR_CODE; |
| 450 | case STUN_ATTR_UNKNOWN_ATTRIBUTES: |
| 451 | return STUN_VALUE_UINT16_LIST; |
| 452 | case STUN_ATTR_REALM: |
| 453 | return STUN_VALUE_BYTE_STRING; |
| 454 | case STUN_ATTR_NONCE: |
| 455 | return STUN_VALUE_BYTE_STRING; |
| 456 | case STUN_ATTR_XOR_MAPPED_ADDRESS: |
| 457 | return STUN_VALUE_XOR_ADDRESS; |
| 458 | case STUN_ATTR_SOFTWARE: |
| 459 | return STUN_VALUE_BYTE_STRING; |
| 460 | case STUN_ATTR_ALTERNATE_SERVER: |
| 461 | return STUN_VALUE_ADDRESS; |
| 462 | case STUN_ATTR_FINGERPRINT: |
| 463 | return STUN_VALUE_UINT32; |
| 464 | case STUN_ATTR_ORIGIN: |
| 465 | return STUN_VALUE_BYTE_STRING; |
| 466 | case STUN_ATTR_RETRANSMIT_COUNT: |
| 467 | return STUN_VALUE_UINT32; |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame^] | 468 | case STUN_ATTR_LAST_ICE_CHECK_RECEIVED: |
| 469 | return STUN_VALUE_BYTE_STRING; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 470 | default: |
| 471 | return STUN_VALUE_UNKNOWN; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ { |
| 476 | StunAttributeValueType value_type = GetAttributeValueType(type); |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 477 | if (value_type != STUN_VALUE_UNKNOWN) { |
| 478 | return StunAttribute::Create(value_type, type, |
| 479 | static_cast<uint16_t>(length), this); |
Jonas Oreland | 16ccef7 | 2018-03-27 09:02:43 +0200 | [diff] [blame] | 480 | } else if (DesignatedExpertRange(type)) { |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 481 | // Read unknown attributes as STUN_VALUE_BYTE_STRING |
| 482 | return StunAttribute::Create(STUN_VALUE_BYTE_STRING, type, |
| 483 | static_cast<uint16_t>(length), this); |
| 484 | } else { |
| 485 | return NULL; |
| 486 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | const StunAttribute* StunMessage::GetAttribute(int type) const { |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 490 | for (const auto& attr : attrs_) { |
| 491 | if (attr->type() == type) { |
| 492 | return attr.get(); |
| 493 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 494 | } |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | bool StunMessage::IsValidTransactionId(const std::string& transaction_id) { |
| 499 | return transaction_id.size() == kStunTransactionIdLength || |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 500 | transaction_id.size() == kStunLegacyTransactionIdLength; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // StunAttribute |
| 504 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 505 | StunAttribute::StunAttribute(uint16_t type, uint16_t length) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 506 | : type_(type), length_(length) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 507 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 508 | void StunAttribute::ConsumePadding(ByteBufferReader* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 509 | int remainder = length_ % 4; |
| 510 | if (remainder > 0) { |
| 511 | buf->Consume(4 - remainder); |
| 512 | } |
| 513 | } |
| 514 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 515 | void StunAttribute::WritePadding(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 516 | int remainder = length_ % 4; |
| 517 | if (remainder > 0) { |
| 518 | char zeroes[4] = {0}; |
| 519 | buf->WriteBytes(zeroes, 4 - remainder); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | StunAttribute* StunAttribute::Create(StunAttributeValueType value_type, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 524 | uint16_t type, |
| 525 | uint16_t length, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 526 | StunMessage* owner) { |
| 527 | switch (value_type) { |
| 528 | case STUN_VALUE_ADDRESS: |
| 529 | return new StunAddressAttribute(type, length); |
| 530 | case STUN_VALUE_XOR_ADDRESS: |
| 531 | return new StunXorAddressAttribute(type, length, owner); |
| 532 | case STUN_VALUE_UINT32: |
| 533 | return new StunUInt32Attribute(type); |
| 534 | case STUN_VALUE_UINT64: |
| 535 | return new StunUInt64Attribute(type); |
| 536 | case STUN_VALUE_BYTE_STRING: |
| 537 | return new StunByteStringAttribute(type, length); |
| 538 | case STUN_VALUE_ERROR_CODE: |
| 539 | return new StunErrorCodeAttribute(type, length); |
| 540 | case STUN_VALUE_UINT16_LIST: |
| 541 | return new StunUInt16ListAttribute(type, length); |
| 542 | default: |
| 543 | return NULL; |
| 544 | } |
| 545 | } |
| 546 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 547 | std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress( |
| 548 | uint16_t type) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 549 | return absl::make_unique<StunAddressAttribute>(type, 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 550 | } |
| 551 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 552 | std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress( |
| 553 | uint16_t type) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 554 | return absl::make_unique<StunXorAddressAttribute>(type, 0, nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 555 | } |
| 556 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 557 | std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64( |
| 558 | uint16_t type) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 559 | return absl::make_unique<StunUInt64Attribute>(type); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 560 | } |
| 561 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 562 | std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32( |
| 563 | uint16_t type) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 564 | return absl::make_unique<StunUInt32Attribute>(type); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 565 | } |
| 566 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 567 | std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString( |
| 568 | uint16_t type) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 569 | return absl::make_unique<StunByteStringAttribute>(type, 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 570 | } |
| 571 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 572 | std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 573 | return absl::make_unique<StunErrorCodeAttribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 574 | STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE); |
| 575 | } |
| 576 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 577 | std::unique_ptr<StunUInt16ListAttribute> |
| 578 | StunAttribute::CreateUnknownAttributes() { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 579 | return absl::make_unique<StunUInt16ListAttribute>( |
| 580 | STUN_ATTR_UNKNOWN_ATTRIBUTES, 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 583 | StunAddressAttribute::StunAddressAttribute(uint16_t type, |
| 584 | const rtc::SocketAddress& addr) |
| 585 | : StunAttribute(type, 0) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 586 | SetAddress(addr); |
| 587 | } |
| 588 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 589 | StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 590 | : StunAttribute(type, length) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 591 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 592 | StunAttributeValueType StunAddressAttribute::value_type() const { |
| 593 | return STUN_VALUE_ADDRESS; |
| 594 | } |
| 595 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 596 | bool StunAddressAttribute::Read(ByteBufferReader* buf) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 597 | uint8_t dummy; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 598 | if (!buf->ReadUInt8(&dummy)) |
| 599 | return false; |
| 600 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 601 | uint8_t stun_family; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 602 | if (!buf->ReadUInt8(&stun_family)) { |
| 603 | return false; |
| 604 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 605 | uint16_t port; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 606 | if (!buf->ReadUInt16(&port)) |
| 607 | return false; |
| 608 | if (stun_family == STUN_ADDRESS_IPV4) { |
| 609 | in_addr v4addr; |
| 610 | if (length() != SIZE_IP4) { |
| 611 | return false; |
| 612 | } |
| 613 | if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) { |
| 614 | return false; |
| 615 | } |
| 616 | rtc::IPAddress ipaddr(v4addr); |
| 617 | SetAddress(rtc::SocketAddress(ipaddr, port)); |
| 618 | } else if (stun_family == STUN_ADDRESS_IPV6) { |
| 619 | in6_addr v6addr; |
| 620 | if (length() != SIZE_IP6) { |
| 621 | return false; |
| 622 | } |
| 623 | if (!buf->ReadBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr))) { |
| 624 | return false; |
| 625 | } |
| 626 | rtc::IPAddress ipaddr(v6addr); |
| 627 | SetAddress(rtc::SocketAddress(ipaddr, port)); |
| 628 | } else { |
| 629 | return false; |
| 630 | } |
| 631 | return true; |
| 632 | } |
| 633 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 634 | bool StunAddressAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 635 | StunAddressFamily address_family = family(); |
| 636 | if (address_family == STUN_ADDRESS_UNDEF) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 637 | RTC_LOG(LS_ERROR) << "Error writing address attribute: unknown family."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | buf->WriteUInt8(0); |
| 641 | buf->WriteUInt8(address_family); |
| 642 | buf->WriteUInt16(address_.port()); |
| 643 | switch (address_.family()) { |
| 644 | case AF_INET: { |
| 645 | in_addr v4addr = address_.ipaddr().ipv4_address(); |
| 646 | buf->WriteBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr)); |
| 647 | break; |
| 648 | } |
| 649 | case AF_INET6: { |
| 650 | in6_addr v6addr = address_.ipaddr().ipv6_address(); |
| 651 | buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr)); |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | return true; |
| 656 | } |
| 657 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 658 | StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type, |
| 659 | const rtc::SocketAddress& addr) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 660 | : StunAddressAttribute(type, addr), owner_(NULL) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 661 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 662 | StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type, |
| 663 | uint16_t length, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 664 | StunMessage* owner) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 665 | : StunAddressAttribute(type, length), owner_(owner) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 666 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 667 | StunAttributeValueType StunXorAddressAttribute::value_type() const { |
| 668 | return STUN_VALUE_XOR_ADDRESS; |
| 669 | } |
| 670 | |
| 671 | void StunXorAddressAttribute::SetOwner(StunMessage* owner) { |
| 672 | owner_ = owner; |
| 673 | } |
| 674 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 675 | rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const { |
| 676 | if (owner_) { |
| 677 | rtc::IPAddress ip = ipaddr(); |
| 678 | switch (ip.family()) { |
| 679 | case AF_INET: { |
| 680 | in_addr v4addr = ip.ipv4_address(); |
| 681 | v4addr.s_addr = |
| 682 | (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie)); |
| 683 | return rtc::IPAddress(v4addr); |
| 684 | } |
| 685 | case AF_INET6: { |
| 686 | in6_addr v6addr = ip.ipv6_address(); |
| 687 | const std::string& transaction_id = owner_->transaction_id(); |
| 688 | if (transaction_id.length() == kStunTransactionIdLength) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 689 | uint32_t transactionid_as_ints[3]; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 690 | memcpy(&transactionid_as_ints[0], transaction_id.c_str(), |
| 691 | transaction_id.length()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 692 | uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 693 | // Transaction ID is in network byte order, but magic cookie |
| 694 | // is stored in host byte order. |
| 695 | ip_as_ints[0] = |
| 696 | (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie)); |
| 697 | ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]); |
| 698 | ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]); |
| 699 | ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]); |
| 700 | return rtc::IPAddress(v6addr); |
| 701 | } |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | // Invalid ip family or transaction ID, or missing owner. |
| 707 | // Return an AF_UNSPEC address. |
| 708 | return rtc::IPAddress(); |
| 709 | } |
| 710 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 711 | bool StunXorAddressAttribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 712 | if (!StunAddressAttribute::Read(buf)) |
| 713 | return false; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 714 | uint16_t xoredport = port() ^ (kStunMagicCookie >> 16); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 715 | rtc::IPAddress xored_ip = GetXoredIP(); |
| 716 | SetAddress(rtc::SocketAddress(xored_ip, xoredport)); |
| 717 | return true; |
| 718 | } |
| 719 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 720 | bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 721 | StunAddressFamily address_family = family(); |
| 722 | if (address_family == STUN_ADDRESS_UNDEF) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 723 | RTC_LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | rtc::IPAddress xored_ip = GetXoredIP(); |
| 727 | if (xored_ip.family() == AF_UNSPEC) { |
| 728 | return false; |
| 729 | } |
| 730 | buf->WriteUInt8(0); |
| 731 | buf->WriteUInt8(family()); |
| 732 | buf->WriteUInt16(port() ^ (kStunMagicCookie >> 16)); |
| 733 | switch (xored_ip.family()) { |
| 734 | case AF_INET: { |
| 735 | in_addr v4addr = xored_ip.ipv4_address(); |
| 736 | buf->WriteBytes(reinterpret_cast<const char*>(&v4addr), sizeof(v4addr)); |
| 737 | break; |
| 738 | } |
| 739 | case AF_INET6: { |
| 740 | in6_addr v6addr = xored_ip.ipv6_address(); |
| 741 | buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr)); |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | return true; |
| 746 | } |
| 747 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 748 | StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 749 | : StunAttribute(type, SIZE), bits_(value) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 750 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 751 | StunUInt32Attribute::StunUInt32Attribute(uint16_t type) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 752 | : StunAttribute(type, SIZE), bits_(0) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 753 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 754 | StunAttributeValueType StunUInt32Attribute::value_type() const { |
| 755 | return STUN_VALUE_UINT32; |
| 756 | } |
| 757 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 758 | bool StunUInt32Attribute::GetBit(size_t index) const { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 759 | RTC_DCHECK(index < 32); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 760 | return static_cast<bool>((bits_ >> index) & 0x1); |
| 761 | } |
| 762 | |
| 763 | void StunUInt32Attribute::SetBit(size_t index, bool value) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 764 | RTC_DCHECK(index < 32); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 765 | bits_ &= ~(1 << index); |
| 766 | bits_ |= value ? (1 << index) : 0; |
| 767 | } |
| 768 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 769 | bool StunUInt32Attribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 770 | if (length() != SIZE || !buf->ReadUInt32(&bits_)) |
| 771 | return false; |
| 772 | return true; |
| 773 | } |
| 774 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 775 | bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 776 | buf->WriteUInt32(bits_); |
| 777 | return true; |
| 778 | } |
| 779 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 780 | StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 781 | : StunAttribute(type, SIZE), bits_(value) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 782 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 783 | StunUInt64Attribute::StunUInt64Attribute(uint16_t type) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 784 | : StunAttribute(type, SIZE), bits_(0) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 785 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 786 | StunAttributeValueType StunUInt64Attribute::value_type() const { |
| 787 | return STUN_VALUE_UINT64; |
| 788 | } |
| 789 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 790 | bool StunUInt64Attribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 791 | if (length() != SIZE || !buf->ReadUInt64(&bits_)) |
| 792 | return false; |
| 793 | return true; |
| 794 | } |
| 795 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 796 | bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 797 | buf->WriteUInt64(bits_); |
| 798 | return true; |
| 799 | } |
| 800 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 801 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 802 | : StunAttribute(type, 0), bytes_(NULL) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 803 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 804 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 805 | const std::string& str) |
| 806 | : StunAttribute(type, 0), bytes_(NULL) { |
| 807 | CopyBytes(str.c_str(), str.size()); |
| 808 | } |
| 809 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 810 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 811 | const void* bytes, |
| 812 | size_t length) |
| 813 | : StunAttribute(type, 0), bytes_(NULL) { |
| 814 | CopyBytes(bytes, length); |
| 815 | } |
| 816 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 817 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 818 | : StunAttribute(type, length), bytes_(NULL) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 819 | |
| 820 | StunByteStringAttribute::~StunByteStringAttribute() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 821 | delete[] bytes_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 824 | StunAttributeValueType StunByteStringAttribute::value_type() const { |
| 825 | return STUN_VALUE_BYTE_STRING; |
| 826 | } |
| 827 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 828 | void StunByteStringAttribute::CopyBytes(const char* bytes) { |
| 829 | CopyBytes(bytes, strlen(bytes)); |
| 830 | } |
| 831 | |
| 832 | void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) { |
| 833 | char* new_bytes = new char[length]; |
| 834 | memcpy(new_bytes, bytes, length); |
| 835 | SetBytes(new_bytes, length); |
| 836 | } |
| 837 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 838 | uint8_t StunByteStringAttribute::GetByte(size_t index) const { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 839 | RTC_DCHECK(bytes_ != NULL); |
| 840 | RTC_DCHECK(index < length()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 841 | return static_cast<uint8_t>(bytes_[index]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 844 | void StunByteStringAttribute::SetByte(size_t index, uint8_t value) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 845 | RTC_DCHECK(bytes_ != NULL); |
| 846 | RTC_DCHECK(index < length()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 847 | bytes_[index] = value; |
| 848 | } |
| 849 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 850 | bool StunByteStringAttribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 851 | bytes_ = new char[length()]; |
| 852 | if (!buf->ReadBytes(bytes_, length())) { |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | ConsumePadding(buf); |
| 857 | return true; |
| 858 | } |
| 859 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 860 | bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 861 | buf->WriteBytes(bytes_, length()); |
| 862 | WritePadding(buf); |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | void StunByteStringAttribute::SetBytes(char* bytes, size_t length) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 867 | delete[] bytes_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 868 | bytes_ = bytes; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 869 | SetLength(static_cast<uint16_t>(length)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 870 | } |
| 871 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 872 | const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4; |
| 873 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 874 | StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, |
| 875 | int code, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 876 | const std::string& reason) |
| 877 | : StunAttribute(type, 0) { |
| 878 | SetCode(code); |
| 879 | SetReason(reason); |
| 880 | } |
| 881 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 882 | StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 883 | : StunAttribute(type, length), class_(0), number_(0) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 884 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 885 | StunErrorCodeAttribute::~StunErrorCodeAttribute() {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 886 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 887 | StunAttributeValueType StunErrorCodeAttribute::value_type() const { |
| 888 | return STUN_VALUE_ERROR_CODE; |
| 889 | } |
| 890 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 891 | int StunErrorCodeAttribute::code() const { |
| 892 | return class_ * 100 + number_; |
| 893 | } |
| 894 | |
| 895 | void StunErrorCodeAttribute::SetCode(int code) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 896 | class_ = static_cast<uint8_t>(code / 100); |
| 897 | number_ = static_cast<uint8_t>(code % 100); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void StunErrorCodeAttribute::SetReason(const std::string& reason) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 901 | SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 902 | reason_ = reason; |
| 903 | } |
| 904 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 905 | bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 906 | uint32_t val; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 907 | if (length() < MIN_SIZE || !buf->ReadUInt32(&val)) |
| 908 | return false; |
| 909 | |
| 910 | if ((val >> 11) != 0) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 911 | RTC_LOG(LS_ERROR) << "error-code bits not zero"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 912 | |
| 913 | class_ = ((val >> 8) & 0x7); |
| 914 | number_ = (val & 0xff); |
| 915 | |
| 916 | if (!buf->ReadString(&reason_, length() - 4)) |
| 917 | return false; |
| 918 | |
| 919 | ConsumePadding(buf); |
| 920 | return true; |
| 921 | } |
| 922 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 923 | bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 924 | buf->WriteUInt32(class_ << 8 | number_); |
| 925 | buf->WriteString(reason_); |
| 926 | WritePadding(buf); |
| 927 | return true; |
| 928 | } |
| 929 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 930 | StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 931 | : StunAttribute(type, length) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 932 | attr_types_ = new std::vector<uint16_t>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | StunUInt16ListAttribute::~StunUInt16ListAttribute() { |
| 936 | delete attr_types_; |
| 937 | } |
| 938 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 939 | StunAttributeValueType StunUInt16ListAttribute::value_type() const { |
| 940 | return STUN_VALUE_UINT16_LIST; |
| 941 | } |
| 942 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 943 | size_t StunUInt16ListAttribute::Size() const { |
| 944 | return attr_types_->size(); |
| 945 | } |
| 946 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 947 | uint16_t StunUInt16ListAttribute::GetType(int index) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 948 | return (*attr_types_)[index]; |
| 949 | } |
| 950 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 951 | void StunUInt16ListAttribute::SetType(int index, uint16_t value) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 952 | (*attr_types_)[index] = value; |
| 953 | } |
| 954 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 955 | void StunUInt16ListAttribute::AddType(uint16_t value) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 956 | attr_types_->push_back(value); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 957 | SetLength(static_cast<uint16_t>(attr_types_->size() * 2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 958 | } |
| 959 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 960 | bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 961 | if (length() % 2) |
| 962 | return false; |
| 963 | |
| 964 | for (size_t i = 0; i < length() / 2; i++) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 965 | uint16_t attr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 966 | if (!buf->ReadUInt16(&attr)) |
| 967 | return false; |
| 968 | attr_types_->push_back(attr); |
| 969 | } |
| 970 | // Padding of these attributes is done in RFC 5389 style. This is |
| 971 | // slightly different from RFC3489, but it shouldn't be important. |
| 972 | // RFC3489 pads out to a 32 bit boundary by duplicating one of the |
| 973 | // entries in the list (not necessarily the last one - it's unspecified). |
| 974 | // RFC5389 pads on the end, and the bytes are always ignored. |
| 975 | ConsumePadding(buf); |
| 976 | return true; |
| 977 | } |
| 978 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 979 | bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 980 | for (size_t i = 0; i < attr_types_->size(); ++i) { |
| 981 | buf->WriteUInt16((*attr_types_)[i]); |
| 982 | } |
| 983 | WritePadding(buf); |
| 984 | return true; |
| 985 | } |
| 986 | |
| 987 | int GetStunSuccessResponseType(int req_type) { |
| 988 | return IsStunRequestType(req_type) ? (req_type | 0x100) : -1; |
| 989 | } |
| 990 | |
| 991 | int GetStunErrorResponseType(int req_type) { |
| 992 | return IsStunRequestType(req_type) ? (req_type | 0x110) : -1; |
| 993 | } |
| 994 | |
| 995 | bool IsStunRequestType(int msg_type) { |
| 996 | return ((msg_type & kStunTypeMask) == 0x000); |
| 997 | } |
| 998 | |
| 999 | bool IsStunIndicationType(int msg_type) { |
| 1000 | return ((msg_type & kStunTypeMask) == 0x010); |
| 1001 | } |
| 1002 | |
| 1003 | bool IsStunSuccessResponseType(int msg_type) { |
| 1004 | return ((msg_type & kStunTypeMask) == 0x100); |
| 1005 | } |
| 1006 | |
| 1007 | bool IsStunErrorResponseType(int msg_type) { |
| 1008 | return ((msg_type & kStunTypeMask) == 0x110); |
| 1009 | } |
| 1010 | |
| 1011 | bool ComputeStunCredentialHash(const std::string& username, |
| 1012 | const std::string& realm, |
| 1013 | const std::string& password, |
| 1014 | std::string* hash) { |
| 1015 | // http://tools.ietf.org/html/rfc5389#section-15.4 |
| 1016 | // long-term credentials will be calculated using the key and key is |
| 1017 | // key = MD5(username ":" realm ":" SASLprep(password)) |
| 1018 | std::string input = username; |
| 1019 | input += ':'; |
| 1020 | input += realm; |
| 1021 | input += ':'; |
| 1022 | input += password; |
| 1023 | |
| 1024 | char digest[rtc::MessageDigest::kMaxSize]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1025 | size_t size = rtc::ComputeDigest(rtc::DIGEST_MD5, input.c_str(), input.size(), |
| 1026 | digest, sizeof(digest)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1027 | if (size == 0) { |
| 1028 | return false; |
| 1029 | } |
| 1030 | |
| 1031 | *hash = std::string(digest, size); |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1035 | std::unique_ptr<StunAttribute> CopyStunAttribute( |
| 1036 | const StunAttribute& attribute, |
| 1037 | rtc::ByteBufferWriter* tmp_buffer_ptr) { |
| 1038 | ByteBufferWriter tmpBuffer; |
| 1039 | if (tmp_buffer_ptr == nullptr) { |
| 1040 | tmp_buffer_ptr = &tmpBuffer; |
| 1041 | } |
| 1042 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1043 | std::unique_ptr<StunAttribute> copy(StunAttribute::Create( |
| 1044 | attribute.value_type(), attribute.type(), |
| 1045 | static_cast<uint16_t>(attribute.length()), nullptr)); |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1046 | |
| 1047 | if (!copy) { |
| 1048 | return nullptr; |
| 1049 | } |
| 1050 | tmp_buffer_ptr->Clear(); |
| 1051 | if (!attribute.Write(tmp_buffer_ptr)) { |
| 1052 | return nullptr; |
| 1053 | } |
| 1054 | rtc::ByteBufferReader reader(*tmp_buffer_ptr); |
| 1055 | if (!copy->Read(&reader)) { |
| 1056 | return nullptr; |
| 1057 | } |
| 1058 | |
| 1059 | return copy; |
| 1060 | } |
| 1061 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame] | 1062 | StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const { |
| 1063 | switch (type) { |
| 1064 | case STUN_ATTR_LIFETIME: |
| 1065 | return STUN_VALUE_UINT32; |
| 1066 | case STUN_ATTR_MAGIC_COOKIE: |
| 1067 | return STUN_VALUE_BYTE_STRING; |
| 1068 | case STUN_ATTR_BANDWIDTH: |
| 1069 | return STUN_VALUE_UINT32; |
| 1070 | case STUN_ATTR_DESTINATION_ADDRESS: |
| 1071 | return STUN_VALUE_ADDRESS; |
| 1072 | case STUN_ATTR_SOURCE_ADDRESS2: |
| 1073 | return STUN_VALUE_ADDRESS; |
| 1074 | case STUN_ATTR_DATA: |
| 1075 | return STUN_VALUE_BYTE_STRING; |
| 1076 | case STUN_ATTR_OPTIONS: |
| 1077 | return STUN_VALUE_UINT32; |
| 1078 | default: |
| 1079 | return StunMessage::GetAttributeValueType(type); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | StunMessage* RelayMessage::CreateNew() const { |
| 1084 | return new RelayMessage(); |
| 1085 | } |
| 1086 | |
| 1087 | StunAttributeValueType TurnMessage::GetAttributeValueType(int type) const { |
| 1088 | switch (type) { |
| 1089 | case STUN_ATTR_CHANNEL_NUMBER: |
| 1090 | return STUN_VALUE_UINT32; |
| 1091 | case STUN_ATTR_TURN_LIFETIME: |
| 1092 | return STUN_VALUE_UINT32; |
| 1093 | case STUN_ATTR_XOR_PEER_ADDRESS: |
| 1094 | return STUN_VALUE_XOR_ADDRESS; |
| 1095 | case STUN_ATTR_DATA: |
| 1096 | return STUN_VALUE_BYTE_STRING; |
| 1097 | case STUN_ATTR_XOR_RELAYED_ADDRESS: |
| 1098 | return STUN_VALUE_XOR_ADDRESS; |
| 1099 | case STUN_ATTR_EVEN_PORT: |
| 1100 | return STUN_VALUE_BYTE_STRING; |
| 1101 | case STUN_ATTR_REQUESTED_TRANSPORT: |
| 1102 | return STUN_VALUE_UINT32; |
| 1103 | case STUN_ATTR_DONT_FRAGMENT: |
| 1104 | return STUN_VALUE_BYTE_STRING; |
| 1105 | case STUN_ATTR_RESERVATION_TOKEN: |
| 1106 | return STUN_VALUE_BYTE_STRING; |
| 1107 | default: |
| 1108 | return StunMessage::GetAttributeValueType(type); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | StunMessage* TurnMessage::CreateNew() const { |
| 1113 | return new TurnMessage(); |
| 1114 | } |
| 1115 | |
| 1116 | StunAttributeValueType IceMessage::GetAttributeValueType(int type) const { |
| 1117 | switch (type) { |
| 1118 | case STUN_ATTR_PRIORITY: |
| 1119 | case STUN_ATTR_NETWORK_INFO: |
| 1120 | case STUN_ATTR_NOMINATION: |
| 1121 | return STUN_VALUE_UINT32; |
| 1122 | case STUN_ATTR_USE_CANDIDATE: |
| 1123 | return STUN_VALUE_BYTE_STRING; |
| 1124 | case STUN_ATTR_ICE_CONTROLLED: |
| 1125 | return STUN_VALUE_UINT64; |
| 1126 | case STUN_ATTR_ICE_CONTROLLING: |
| 1127 | return STUN_VALUE_UINT64; |
| 1128 | default: |
| 1129 | return StunMessage::GetAttributeValueType(type); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | StunMessage* IceMessage::CreateNew() const { |
| 1134 | return new IceMessage(); |
| 1135 | } |
| 1136 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1137 | } // namespace cricket |