blob: c6ef07b6275049c5750d4bc7d6fe92252fe986f0 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
dschinazi0fdff8e2019-07-18 09:07:39 -07007#include <cstddef>
QUICHE teama6ef0a62019-03-07 20:34:33 -05008#include <cstdint>
9#include <cstring>
10#include <iomanip>
vasilvv872e7a32019-03-12 16:42:44 -070011#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050012
dschinazi0fdff8e2019-07-18 09:07:39 -070013#include "third_party/boringssl/src/include/openssl/siphash.h"
14#include "net/third_party/quiche/src/quic/core/crypto/quic_random.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#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 teama6ef0a62019-03-07 20:34:33 -050021#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
22
23namespace quic {
24
dschinazi0fdff8e2019-07-18 09:07:39 -070025namespace {
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.
33class QuicConnectionIdHasher {
34 public:
dschinazi2d014822019-07-18 15:28:13 -070035 inline QuicConnectionIdHasher()
dschinazi0fdff8e2019-07-18 09:07:39 -070036 : 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
dschinazib3241162019-06-10 17:59:37 -070053QuicConnectionId::QuicConnectionId() : QuicConnectionId(nullptr, 0) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050054
55QuicConnectionId::QuicConnectionId(const char* data, uint8_t length) {
dschinazib012d212019-08-01 18:07:26 -070056 static_assert(kQuicMaxConnectionIdAllVersionsLength <=
57 std::numeric_limits<uint8_t>::max(),
58 "kQuicMaxConnectionIdAllVersionsLength too high");
59 if (length > kQuicMaxConnectionIdAllVersionsLength) {
dschinazib953d022019-08-01 18:05:58 -070060 QUIC_BUG << "Attempted to create connection ID of length "
61 << static_cast<int>(length);
dschinazib012d212019-08-01 18:07:26 -070062 length = kQuicMaxConnectionIdAllVersionsLength;
QUICHE teama6ef0a62019-03-07 20:34:33 -050063 }
64 length_ = length;
dschinazib3241162019-06-10 17:59:37 -070065 if (length_ == 0) {
66 return;
67 }
68 if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050069 memcpy(data_, data, length_);
dschinazib3241162019-06-10 17:59:37 -070070 return;
71 }
dschinazi6abd56b2019-06-18 18:04:30 -070072 QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 1, 6);
dschinazib3241162019-06-10 17:59:37 -070073 if (length_ <= sizeof(data_short_)) {
74 memcpy(data_short_, data, length_);
75 return;
76 }
77 data_long_ = reinterpret_cast<char*>(malloc(length_));
78 CHECK_NE(nullptr, data_long_);
79 memcpy(data_long_, data, length_);
80}
81
82QuicConnectionId::~QuicConnectionId() {
83 if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) {
84 return;
85 }
dschinazi6abd56b2019-06-18 18:04:30 -070086 QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 2, 6);
dschinazib3241162019-06-10 17:59:37 -070087 if (length_ > sizeof(data_short_)) {
88 free(data_long_);
89 data_long_ = nullptr;
QUICHE teama6ef0a62019-03-07 20:34:33 -050090 }
91}
92
dschinazib3241162019-06-10 17:59:37 -070093QuicConnectionId::QuicConnectionId(const QuicConnectionId& other)
94 : QuicConnectionId(other.data(), other.length()) {}
95
96QuicConnectionId& QuicConnectionId::operator=(const QuicConnectionId& other) {
97 set_length(other.length());
98 memcpy(mutable_data(), other.data(), length_);
99 return *this;
100}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500101
102const char* QuicConnectionId::data() const {
dschinazib3241162019-06-10 17:59:37 -0700103 if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) {
104 return data_;
105 }
dschinazi6abd56b2019-06-18 18:04:30 -0700106 QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 3, 6);
dschinazib3241162019-06-10 17:59:37 -0700107 if (length_ <= sizeof(data_short_)) {
108 return data_short_;
109 }
110 return data_long_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500111}
112
113char* QuicConnectionId::mutable_data() {
dschinazib3241162019-06-10 17:59:37 -0700114 if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) {
115 return data_;
116 }
dschinazi6abd56b2019-06-18 18:04:30 -0700117 QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 4, 6);
dschinazib3241162019-06-10 17:59:37 -0700118 if (length_ <= sizeof(data_short_)) {
119 return data_short_;
120 }
121 return data_long_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500122}
123
124uint8_t QuicConnectionId::length() const {
125 return length_;
126}
127
128void QuicConnectionId::set_length(uint8_t length) {
dschinazib012d212019-08-01 18:07:26 -0700129 if (length > kQuicMaxConnectionIdAllVersionsLength) {
dschinazib953d022019-08-01 18:05:58 -0700130 QUIC_BUG << "Attempted to set connection ID length to "
131 << static_cast<int>(length);
dschinazib012d212019-08-01 18:07:26 -0700132 length = kQuicMaxConnectionIdAllVersionsLength;
dschinazi6c84c142019-07-31 09:11:49 -0700133 }
dschinazib3241162019-06-10 17:59:37 -0700134 if (GetQuicRestartFlag(quic_use_allocated_connection_ids)) {
dschinazi6abd56b2019-06-18 18:04:30 -0700135 QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 5, 6);
dschinazib3241162019-06-10 17:59:37 -0700136 char temporary_data[sizeof(data_short_)];
137 if (length > sizeof(data_short_)) {
138 if (length_ <= sizeof(data_short_)) {
139 // Copy data from data_short_ to data_long_.
140 memcpy(temporary_data, data_short_, length_);
141 data_long_ = reinterpret_cast<char*>(malloc(length));
142 CHECK_NE(nullptr, data_long_);
143 memcpy(data_long_, temporary_data, length_);
144 } else {
145 // Resize data_long_.
146 char* realloc_result =
147 reinterpret_cast<char*>(realloc(data_long_, length));
148 CHECK_NE(nullptr, realloc_result);
149 data_long_ = realloc_result;
150 }
151 } else if (length_ > sizeof(data_short_)) {
152 // Copy data from data_long_ to data_short_.
153 memcpy(temporary_data, data_long_, length);
154 free(data_long_);
155 data_long_ = nullptr;
156 memcpy(data_short_, temporary_data, length);
157 }
158 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500159 length_ = length;
160}
161
162bool QuicConnectionId::IsEmpty() const {
163 return length_ == 0;
164}
165
166size_t QuicConnectionId::Hash() const {
dschinazi0fdff8e2019-07-18 09:07:39 -0700167 if (!GetQuicRestartFlag(quic_connection_id_use_siphash)) {
168 uint64_t data_bytes[3] = {0, 0, 0};
dschinazib012d212019-08-01 18:07:26 -0700169 static_assert(sizeof(data_bytes) >= kQuicMaxConnectionIdAllVersionsLength,
170 "kQuicMaxConnectionIdAllVersionsLength changed");
dschinazi0fdff8e2019-07-18 09:07:39 -0700171 memcpy(data_bytes, data(), length_);
172 // This Hash function is designed to return the same value as the host byte
173 // order representation when the connection ID length is 64 bits.
174 return QuicEndian::NetToHost64(kQuicDefaultConnectionIdLength ^ length_ ^
175 data_bytes[0] ^ data_bytes[1] ^
176 data_bytes[2]);
177 }
178 QUIC_RESTART_FLAG_COUNT(quic_connection_id_use_siphash);
179 static const QuicConnectionIdHasher hasher = QuicConnectionIdHasher();
180 return hasher.Hash(data(), length_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500181}
182
vasilvvc48c8712019-03-11 13:38:16 -0700183std::string QuicConnectionId::ToString() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500184 if (IsEmpty()) {
vasilvvc48c8712019-03-11 13:38:16 -0700185 return std::string("0");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500186 }
dschinazib3241162019-06-10 17:59:37 -0700187 return QuicTextUtils::HexEncode(data(), length_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500188}
189
190std::ostream& operator<<(std::ostream& os, const QuicConnectionId& v) {
191 os << v.ToString();
192 return os;
193}
194
195bool QuicConnectionId::operator==(const QuicConnectionId& v) const {
dschinazib3241162019-06-10 17:59:37 -0700196 return length_ == v.length_ && memcmp(data(), v.data(), length_) == 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500197}
198
199bool QuicConnectionId::operator!=(const QuicConnectionId& v) const {
200 return !(v == *this);
201}
202
203bool QuicConnectionId::operator<(const QuicConnectionId& v) const {
204 if (length_ < v.length_) {
205 return true;
206 }
207 if (length_ > v.length_) {
208 return false;
209 }
dschinazib3241162019-06-10 17:59:37 -0700210 return memcmp(data(), v.data(), length_) < 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500211}
212
213QuicConnectionId EmptyQuicConnectionId() {
214 return QuicConnectionId();
215}
216
217static_assert(kQuicDefaultConnectionIdLength == sizeof(uint64_t),
218 "kQuicDefaultConnectionIdLength changed");
219static_assert(kQuicDefaultConnectionIdLength == PACKET_8BYTE_CONNECTION_ID,
220 "kQuicDefaultConnectionIdLength changed");
221
222} // namespace quic