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