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