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