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