QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
| 6 | |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 7 | #include <cstddef> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | #include <cstdint> |
| 9 | #include <cstring> |
| 10 | #include <iomanip> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 11 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 13 | #include "third_party/boringssl/src/include/openssl/siphash.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_endian.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 22 | |
| 23 | namespace quic { |
| 24 | |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // QuicConnectionIdHasher can be used to generate a stable connection ID hash |
| 28 | // function that will return the same value for two equal connection IDs for |
| 29 | // the duration of process lifetime. It is meant to be used as input to data |
| 30 | // structures that do not outlast process lifetime. A new key is generated once |
| 31 | // per process to prevent attackers from crafting connection IDs in such a way |
| 32 | // that they always land in the same hash bucket. |
| 33 | class QuicConnectionIdHasher { |
| 34 | public: |
dschinazi | 2d01482 | 2019-07-18 15:28:13 -0700 | [diff] [blame] | 35 | inline QuicConnectionIdHasher() |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 36 | : QuicConnectionIdHasher(QuicRandom::GetInstance()) {} |
| 37 | |
| 38 | explicit inline QuicConnectionIdHasher(QuicRandom* random) { |
| 39 | random->RandBytes(&sip_hash_key_, sizeof(sip_hash_key_)); |
| 40 | } |
| 41 | |
| 42 | inline size_t Hash(const char* input, size_t input_len) const { |
| 43 | return static_cast<size_t>(SIPHASH_24( |
| 44 | sip_hash_key_, reinterpret_cast<const uint8_t*>(input), input_len)); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | uint64_t sip_hash_key_[2]; |
| 49 | }; |
| 50 | |
| 51 | } // namespace |
| 52 | |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 53 | QuicConnectionId::QuicConnectionId() : QuicConnectionId(nullptr, 0) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | |
| 55 | QuicConnectionId::QuicConnectionId(const char* data, uint8_t length) { |
dschinazi | b012d21 | 2019-08-01 18:07:26 -0700 | [diff] [blame] | 56 | static_assert(kQuicMaxConnectionIdAllVersionsLength <= |
| 57 | std::numeric_limits<uint8_t>::max(), |
| 58 | "kQuicMaxConnectionIdAllVersionsLength too high"); |
| 59 | if (length > kQuicMaxConnectionIdAllVersionsLength) { |
dschinazi | b953d02 | 2019-08-01 18:05:58 -0700 | [diff] [blame] | 60 | QUIC_BUG << "Attempted to create connection ID of length " |
| 61 | << static_cast<int>(length); |
dschinazi | b012d21 | 2019-08-01 18:07:26 -0700 | [diff] [blame] | 62 | length = kQuicMaxConnectionIdAllVersionsLength; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | } |
| 64 | length_ = length; |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 65 | if (length_ == 0) { |
| 66 | return; |
| 67 | } |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 68 | if (length_ <= sizeof(data_short_)) { |
| 69 | memcpy(data_short_, data, length_); |
| 70 | return; |
| 71 | } |
| 72 | data_long_ = reinterpret_cast<char*>(malloc(length_)); |
| 73 | CHECK_NE(nullptr, data_long_); |
| 74 | memcpy(data_long_, data, length_); |
| 75 | } |
| 76 | |
| 77 | QuicConnectionId::~QuicConnectionId() { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 78 | if (length_ > sizeof(data_short_)) { |
| 79 | free(data_long_); |
| 80 | data_long_ = nullptr; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 84 | QuicConnectionId::QuicConnectionId(const QuicConnectionId& other) |
| 85 | : QuicConnectionId(other.data(), other.length()) {} |
| 86 | |
| 87 | QuicConnectionId& QuicConnectionId::operator=(const QuicConnectionId& other) { |
| 88 | set_length(other.length()); |
| 89 | memcpy(mutable_data(), other.data(), length_); |
| 90 | return *this; |
| 91 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 92 | |
| 93 | const char* QuicConnectionId::data() const { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 94 | if (length_ <= sizeof(data_short_)) { |
| 95 | return data_short_; |
| 96 | } |
| 97 | return data_long_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | char* QuicConnectionId::mutable_data() { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 101 | if (length_ <= sizeof(data_short_)) { |
| 102 | return data_short_; |
| 103 | } |
| 104 | return data_long_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | uint8_t QuicConnectionId::length() const { |
| 108 | return length_; |
| 109 | } |
| 110 | |
| 111 | void QuicConnectionId::set_length(uint8_t length) { |
dschinazi | b012d21 | 2019-08-01 18:07:26 -0700 | [diff] [blame] | 112 | if (length > kQuicMaxConnectionIdAllVersionsLength) { |
dschinazi | b953d02 | 2019-08-01 18:05:58 -0700 | [diff] [blame] | 113 | QUIC_BUG << "Attempted to set connection ID length to " |
| 114 | << static_cast<int>(length); |
dschinazi | b012d21 | 2019-08-01 18:07:26 -0700 | [diff] [blame] | 115 | length = kQuicMaxConnectionIdAllVersionsLength; |
dschinazi | 6c84c14 | 2019-07-31 09:11:49 -0700 | [diff] [blame] | 116 | } |
dschinazi | 0d06d7b | 2019-08-19 14:33:07 -0700 | [diff] [blame] | 117 | char temporary_data[sizeof(data_short_)]; |
| 118 | if (length > sizeof(data_short_)) { |
| 119 | if (length_ <= sizeof(data_short_)) { |
| 120 | // Copy data from data_short_ to data_long_. |
| 121 | memcpy(temporary_data, data_short_, length_); |
| 122 | data_long_ = reinterpret_cast<char*>(malloc(length)); |
| 123 | CHECK_NE(nullptr, data_long_); |
| 124 | memcpy(data_long_, temporary_data, length_); |
| 125 | } else { |
| 126 | // Resize data_long_. |
| 127 | char* realloc_result = |
| 128 | reinterpret_cast<char*>(realloc(data_long_, length)); |
| 129 | CHECK_NE(nullptr, realloc_result); |
| 130 | data_long_ = realloc_result; |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 131 | } |
dschinazi | 0d06d7b | 2019-08-19 14:33:07 -0700 | [diff] [blame] | 132 | } else if (length_ > sizeof(data_short_)) { |
| 133 | // Copy data from data_long_ to data_short_. |
| 134 | memcpy(temporary_data, data_long_, length); |
| 135 | free(data_long_); |
| 136 | data_long_ = nullptr; |
| 137 | memcpy(data_short_, temporary_data, length); |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 138 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 139 | length_ = length; |
| 140 | } |
| 141 | |
| 142 | bool QuicConnectionId::IsEmpty() const { |
| 143 | return length_ == 0; |
| 144 | } |
| 145 | |
| 146 | size_t QuicConnectionId::Hash() const { |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 147 | static const QuicConnectionIdHasher hasher = QuicConnectionIdHasher(); |
| 148 | return hasher.Hash(data(), length_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | } |
| 150 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 151 | std::string QuicConnectionId::ToString() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | if (IsEmpty()) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 153 | return std::string("0"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | } |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 155 | return QuicTextUtils::HexEncode(data(), length_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | std::ostream& operator<<(std::ostream& os, const QuicConnectionId& v) { |
| 159 | os << v.ToString(); |
| 160 | return os; |
| 161 | } |
| 162 | |
| 163 | bool QuicConnectionId::operator==(const QuicConnectionId& v) const { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 164 | return length_ == v.length_ && memcmp(data(), v.data(), length_) == 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bool QuicConnectionId::operator!=(const QuicConnectionId& v) const { |
| 168 | return !(v == *this); |
| 169 | } |
| 170 | |
| 171 | bool QuicConnectionId::operator<(const QuicConnectionId& v) const { |
| 172 | if (length_ < v.length_) { |
| 173 | return true; |
| 174 | } |
| 175 | if (length_ > v.length_) { |
| 176 | return false; |
| 177 | } |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 178 | return memcmp(data(), v.data(), length_) < 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | QuicConnectionId EmptyQuicConnectionId() { |
| 182 | return QuicConnectionId(); |
| 183 | } |
| 184 | |
| 185 | static_assert(kQuicDefaultConnectionIdLength == sizeof(uint64_t), |
| 186 | "kQuicDefaultConnectionIdLength changed"); |
| 187 | static_assert(kQuicDefaultConnectionIdLength == PACKET_8BYTE_CONNECTION_ID, |
| 188 | "kQuicDefaultConnectionIdLength changed"); |
| 189 | |
| 190 | } // namespace quic |