Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/p2p/base/stun.h" | 11 #include "webrtc/p2p/base/stun.h" |
| 12 | 12 |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include <memory> | 15 #include <memory> |
| 16 | 16 |
| 17 #include "webrtc/base/byteorder.h" | 17 #include "webrtc/base/byteorder.h" |
| 18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/common.h" | |
| 20 #include "webrtc/base/crc32.h" | 19 #include "webrtc/base/crc32.h" |
| 21 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/messagedigest.h" | 21 #include "webrtc/base/messagedigest.h" |
| 23 #include "webrtc/base/stringencode.h" | 22 #include "webrtc/base/stringencode.h" |
| 24 | 23 |
| 25 using rtc::ByteBufferReader; | 24 using rtc::ByteBufferReader; |
| 26 using rtc::ByteBufferWriter; | 25 using rtc::ByteBufferWriter; |
| 27 | 26 |
| 28 namespace cricket { | 27 namespace cricket { |
| 29 | 28 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 } | 66 } |
| 68 | 67 |
| 69 bool StunMessage::SetTransactionID(const std::string& str) { | 68 bool StunMessage::SetTransactionID(const std::string& str) { |
| 70 if (!IsValidTransactionId(str)) { | 69 if (!IsValidTransactionId(str)) { |
| 71 return false; | 70 return false; |
| 72 } | 71 } |
| 73 transaction_id_ = str; | 72 transaction_id_ = str; |
| 74 return true; | 73 return true; |
| 75 } | 74 } |
| 76 | 75 |
| 77 bool StunMessage::AddAttribute(StunAttribute* attr) { | 76 void StunMessage::AddAttribute(StunAttribute* attr) { |
| 78 // Fail any attributes that aren't valid for this type of message. | 77 // Fail any attributes that aren't valid for this type of message. |
| 79 if (attr->value_type() != GetAttributeValueType(attr->type())) { | 78 RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type())); |
| 80 return false; | 79 |
|
kwiberg-webrtc
2017/02/01 10:40:17
Old non-debug behavior for wrong attr type was to
pthatcher2
2017/02/01 22:15:06
I think a DCHECK is sufficient. I think there is
kwiberg-webrtc
2017/02/02 08:39:00
Well then. If no caller does the wrong thing, we'r
| |
| 81 } | |
| 82 attrs_->push_back(attr); | 80 attrs_->push_back(attr); |
| 83 attr->SetOwner(this); | 81 attr->SetOwner(this); |
| 84 size_t attr_length = attr->length(); | 82 size_t attr_length = attr->length(); |
| 85 if (attr_length % 4 != 0) { | 83 if (attr_length % 4 != 0) { |
| 86 attr_length += (4 - (attr_length % 4)); | 84 attr_length += (4 - (attr_length % 4)); |
| 87 } | 85 } |
| 88 length_ += static_cast<uint16_t>(attr_length + 4); | 86 length_ += static_cast<uint16_t>(attr_length + 4); |
| 89 return true; | |
| 90 } | 87 } |
| 91 | 88 |
| 92 const StunAddressAttribute* StunMessage::GetAddress(int type) const { | 89 const StunAddressAttribute* StunMessage::GetAddress(int type) const { |
| 93 switch (type) { | 90 switch (type) { |
| 94 case STUN_ATTR_MAPPED_ADDRESS: { | 91 case STUN_ATTR_MAPPED_ADDRESS: { |
| 95 // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is | 92 // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is |
| 96 // missing. | 93 // missing. |
| 97 const StunAttribute* mapped_address = | 94 const StunAttribute* mapped_address = |
| 98 GetAttribute(STUN_ATTR_MAPPED_ADDRESS); | 95 GetAttribute(STUN_ATTR_MAPPED_ADDRESS); |
| 99 if (!mapped_address) | 96 if (!mapped_address) |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 return AddMessageIntegrity(password.c_str(), password.size()); | 210 return AddMessageIntegrity(password.c_str(), password.size()); |
| 214 } | 211 } |
| 215 | 212 |
| 216 bool StunMessage::AddMessageIntegrity(const char* key, | 213 bool StunMessage::AddMessageIntegrity(const char* key, |
| 217 size_t keylen) { | 214 size_t keylen) { |
| 218 // Add the attribute with a dummy value. Since this is a known attribute, it | 215 // Add the attribute with a dummy value. Since this is a known attribute, it |
| 219 // can't fail. | 216 // can't fail. |
| 220 StunByteStringAttribute* msg_integrity_attr = | 217 StunByteStringAttribute* msg_integrity_attr = |
| 221 new StunByteStringAttribute(STUN_ATTR_MESSAGE_INTEGRITY, | 218 new StunByteStringAttribute(STUN_ATTR_MESSAGE_INTEGRITY, |
| 222 std::string(kStunMessageIntegritySize, '0')); | 219 std::string(kStunMessageIntegritySize, '0')); |
| 223 VERIFY(AddAttribute(msg_integrity_attr)); | 220 AddAttribute(msg_integrity_attr); |
| 224 | 221 |
| 225 // Calculate the HMAC for the message. | 222 // Calculate the HMAC for the message. |
| 226 ByteBufferWriter buf; | 223 ByteBufferWriter buf; |
| 227 if (!Write(&buf)) | 224 if (!Write(&buf)) |
| 228 return false; | 225 return false; |
| 229 | 226 |
| 230 int msg_len_for_hmac = static_cast<int>( | 227 int msg_len_for_hmac = static_cast<int>( |
| 231 buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length()); | 228 buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length()); |
| 232 char hmac[kStunMessageIntegritySize]; | 229 char hmac[kStunMessageIntegritySize]; |
| 233 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, | 230 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize); | 271 rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize); |
| 275 return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) == | 272 return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) == |
| 276 rtc::ComputeCrc32(data, size - fingerprint_attr_size)); | 273 rtc::ComputeCrc32(data, size - fingerprint_attr_size)); |
| 277 } | 274 } |
| 278 | 275 |
| 279 bool StunMessage::AddFingerprint() { | 276 bool StunMessage::AddFingerprint() { |
| 280 // Add the attribute with a dummy value. Since this is a known attribute, | 277 // Add the attribute with a dummy value. Since this is a known attribute, |
| 281 // it can't fail. | 278 // it can't fail. |
| 282 StunUInt32Attribute* fingerprint_attr = | 279 StunUInt32Attribute* fingerprint_attr = |
| 283 new StunUInt32Attribute(STUN_ATTR_FINGERPRINT, 0); | 280 new StunUInt32Attribute(STUN_ATTR_FINGERPRINT, 0); |
| 284 VERIFY(AddAttribute(fingerprint_attr)); | 281 AddAttribute(fingerprint_attr); |
| 285 | 282 |
| 286 // Calculate the CRC-32 for the message and insert it. | 283 // Calculate the CRC-32 for the message and insert it. |
| 287 ByteBufferWriter buf; | 284 ByteBufferWriter buf; |
| 288 if (!Write(&buf)) | 285 if (!Write(&buf)) |
| 289 return false; | 286 return false; |
| 290 | 287 |
| 291 int msg_len_for_crc32 = static_cast<int>( | 288 int msg_len_for_crc32 = static_cast<int>( |
| 292 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length()); | 289 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length()); |
| 293 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32); | 290 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32); |
| 294 | 291 |
| (...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 915 digest, sizeof(digest)); | 912 digest, sizeof(digest)); |
| 916 if (size == 0) { | 913 if (size == 0) { |
| 917 return false; | 914 return false; |
| 918 } | 915 } |
| 919 | 916 |
| 920 *hash = std::string(digest, size); | 917 *hash = std::string(digest, size); |
| 921 return true; | 918 return true; |
| 922 } | 919 } |
| 923 | 920 |
| 924 } // namespace cricket | 921 } // namespace cricket |
| OLD | NEW |