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