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