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