/src/boringssl/ssl/internal.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
3 | | // Copyright 2005 Nokia. All rights reserved. |
4 | | // |
5 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | // you may not use this file except in compliance with the License. |
7 | | // You may obtain a copy of the License at |
8 | | // |
9 | | // https://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, software |
12 | | // distributed under the License is distributed on an "AS IS" BASIS, |
13 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | // See the License for the specific language governing permissions and |
15 | | // limitations under the License. |
16 | | |
17 | | #ifndef OPENSSL_HEADER_SSL_INTERNAL_H |
18 | | #define OPENSSL_HEADER_SSL_INTERNAL_H |
19 | | |
20 | | #include <openssl/base.h> |
21 | | |
22 | | #include <stdlib.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <atomic> |
26 | | #include <bitset> |
27 | | #include <initializer_list> |
28 | | #include <limits> |
29 | | #include <new> |
30 | | #include <optional> |
31 | | #include <string_view> |
32 | | #include <type_traits> |
33 | | #include <utility> |
34 | | |
35 | | #include <openssl/aead.h> |
36 | | #include <openssl/curve25519.h> |
37 | | #include <openssl/err.h> |
38 | | #include <openssl/hpke.h> |
39 | | #include <openssl/mem.h> |
40 | | #include <openssl/span.h> |
41 | | #include <openssl/ssl.h> |
42 | | #include <openssl/stack.h> |
43 | | |
44 | | #include "../crypto/err/internal.h" |
45 | | #include "../crypto/internal.h" |
46 | | #include "../crypto/lhash/internal.h" |
47 | | #include "../crypto/mem_internal.h" |
48 | | #include "../crypto/spake2plus/internal.h" |
49 | | |
50 | | |
51 | | #if defined(OPENSSL_WINDOWS) |
52 | | // Windows defines struct timeval in winsock2.h. |
53 | | #include <winsock2.h> |
54 | | #else |
55 | | #include <sys/time.h> |
56 | | #endif |
57 | | |
58 | | |
59 | | BSSL_NAMESPACE_BEGIN |
60 | | |
61 | | struct SSL_CONFIG; |
62 | | struct SSL_HANDSHAKE; |
63 | | struct SSL_PROTOCOL_METHOD; |
64 | | struct SSL_X509_METHOD; |
65 | | |
66 | | // C++ utilities. |
67 | | |
68 | | // An MRUQueue maintains a queue of up to |N| objects of type |T|. If the queue |
69 | | // is at capacity, adding to the queue pops the least recently added element. |
70 | | template <typename T, size_t N> |
71 | | class MRUQueue { |
72 | | public: |
73 | | static constexpr bool kAllowUniquePtr = true; |
74 | | |
75 | 50.8k | MRUQueue() = default; bssl::MRUQueue<bssl::DTLSSentRecord, 32ul>::MRUQueue() Line | Count | Source | 75 | 10.5k | MRUQueue() = default; |
bssl::MRUQueue<bssl::DTLSRecordNumber, 32ul>::MRUQueue() Line | Count | Source | 75 | 40.2k | MRUQueue() = default; |
|
76 | | |
77 | | // If we ever need to make this type movable, we could. (The defaults almost |
78 | | // work except we need |start_| to be reset when moved-from.) |
79 | | MRUQueue(const MRUQueue &other) = delete; |
80 | | MRUQueue &operator=(const MRUQueue &other) = delete; |
81 | | |
82 | 2.29k | bool empty() const { return size() == 0; } |
83 | 98.0k | size_t size() const { return storage_.size(); } bssl::MRUQueue<bssl::DTLSRecordNumber, 32ul>::size() const Line | Count | Source | 83 | 90.7k | size_t size() const { return storage_.size(); } |
bssl::MRUQueue<bssl::DTLSSentRecord, 32ul>::size() const Line | Count | Source | 83 | 7.30k | size_t size() const { return storage_.size(); } |
|
84 | | |
85 | 61.4k | T &operator[](size_t i) { |
86 | 61.4k | BSSL_CHECK(i < size()); |
87 | 61.4k | return storage_[(start_ + i) % N]; |
88 | 61.4k | } bssl::MRUQueue<bssl::DTLSRecordNumber, 32ul>::operator[](unsigned long) Line | Count | Source | 85 | 58.4k | T &operator[](size_t i) { | 86 | 58.4k | BSSL_CHECK(i < size()); | 87 | 58.4k | return storage_[(start_ + i) % N]; | 88 | 58.4k | } |
bssl::MRUQueue<bssl::DTLSSentRecord, 32ul>::operator[](unsigned long) Line | Count | Source | 85 | 3.01k | T &operator[](size_t i) { | 86 | 3.01k | BSSL_CHECK(i < size()); | 87 | 3.01k | return storage_[(start_ + i) % N]; | 88 | 3.01k | } |
|
89 | | const T &operator[](size_t i) const { |
90 | | return (*const_cast<MRUQueue *>(this))[i]; |
91 | | } |
92 | | |
93 | 12.3k | void Clear() { |
94 | 12.3k | storage_.clear(); |
95 | 12.3k | start_ = 0; |
96 | 12.3k | } |
97 | | |
98 | 114k | void PushBack(T t) { |
99 | 114k | if (storage_.size() < N) { |
100 | 79.0k | assert(start_ == 0); |
101 | 79.0k | storage_.PushBack(std::move(t)); |
102 | 79.0k | } else { |
103 | 35.2k | (*this)[0] = std::move(t); |
104 | 35.2k | start_ = (start_ + 1) % N; |
105 | 35.2k | } |
106 | 114k | } bssl::MRUQueue<bssl::DTLSSentRecord, 32ul>::PushBack(bssl::DTLSSentRecord) Line | Count | Source | 98 | 19.6k | void PushBack(T t) { | 99 | 19.6k | if (storage_.size() < N) { | 100 | 19.6k | assert(start_ == 0); | 101 | 19.6k | storage_.PushBack(std::move(t)); | 102 | 19.6k | } else { | 103 | 0 | (*this)[0] = std::move(t); | 104 | 0 | start_ = (start_ + 1) % N; | 105 | 0 | } | 106 | 19.6k | } |
bssl::MRUQueue<bssl::DTLSRecordNumber, 32ul>::PushBack(bssl::DTLSRecordNumber) Line | Count | Source | 98 | 94.5k | void PushBack(T t) { | 99 | 94.5k | if (storage_.size() < N) { | 100 | 59.3k | assert(start_ == 0); | 101 | 59.3k | storage_.PushBack(std::move(t)); | 102 | 59.3k | } else { | 103 | 35.2k | (*this)[0] = std::move(t); | 104 | 35.2k | start_ = (start_ + 1) % N; | 105 | 35.2k | } | 106 | 94.5k | } |
|
107 | | |
108 | | private: |
109 | | InplaceVector<T, N> storage_; |
110 | | PackedSize<N> start_ = 0; |
111 | | }; |
112 | | |
113 | | // CBBFinishArray behaves like |CBB_finish| but stores the result in an Array. |
114 | | OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out); |
115 | | |
116 | | // GetAllNames helps to implement |*_get_all_*_names| style functions. It |
117 | | // writes at most |max_out| string pointers to |out| and returns the number that |
118 | | // it would have liked to have written. The strings written consist of |
119 | | // |fixed_names_len| strings from |fixed_names| followed by |objects_len| |
120 | | // strings taken by projecting |objects| through |name|. |
121 | | template <typename T, typename Name> |
122 | | inline size_t GetAllNames(const char **out, size_t max_out, |
123 | | Span<const char *const> fixed_names, Name(T::*name), |
124 | 0 | Span<const T> objects) { |
125 | 0 | auto span = bssl::Span(out, max_out); |
126 | 0 | for (size_t i = 0; !span.empty() && i < fixed_names.size(); i++) { |
127 | 0 | span[0] = fixed_names[i]; |
128 | 0 | span = span.subspan(1); |
129 | 0 | } |
130 | 0 | span = span.subspan(0, objects.size()); |
131 | 0 | for (size_t i = 0; i < span.size(); i++) { |
132 | 0 | span[i] = objects[i].*name; |
133 | 0 | } |
134 | 0 | return fixed_names.size() + objects.size(); |
135 | 0 | } Unexecuted instantiation: unsigned long bssl::GetAllNames<ssl_cipher_st, char const*>(char const**, unsigned long, bssl::Span<char const* const>, char const* ssl_cipher_st::*, bssl::Span<ssl_cipher_st const>) Unexecuted instantiation: unsigned long bssl::GetAllNames<bssl::NamedGroup, char const [32]>(char const**, unsigned long, bssl::Span<char const* const>, char const (bssl::NamedGroup::*) [32], bssl::Span<bssl::NamedGroup const>) Unexecuted instantiation: unsigned long bssl::GetAllNames<SignatureAlgorithmName, char const [24]>(char const**, unsigned long, bssl::Span<char const* const>, char const (SignatureAlgorithmName::*) [24], bssl::Span<SignatureAlgorithmName const>) Unexecuted instantiation: unsigned long bssl::GetAllNames<bssl::VersionInfo, char const*>(char const**, unsigned long, bssl::Span<char const* const>, char const* bssl::VersionInfo::*, bssl::Span<bssl::VersionInfo const>) |
136 | | |
137 | | // RefCounted is a common base for ref-counted types. This is an instance of the |
138 | | // C++ curiously-recurring template pattern, so a type Foo must subclass |
139 | | // RefCounted<Foo>. It additionally must friend RefCounted<Foo> to allow calling |
140 | | // the destructor. |
141 | | template <typename Derived> |
142 | | class RefCounted { |
143 | | public: |
144 | | RefCounted(const RefCounted &) = delete; |
145 | | RefCounted &operator=(const RefCounted &) = delete; |
146 | | |
147 | | // These methods are intentionally named differently from `bssl::UpRef` to |
148 | | // avoid a collision. Only the implementations of `FOO_up_ref` and `FOO_free` |
149 | | // should call these. |
150 | 419k | void UpRefInternal() { CRYPTO_refcount_inc(&references_); } bssl::RefCounted<ssl_ech_keys_st>::UpRefInternal() Line | Count | Source | 150 | 461 | void UpRefInternal() { CRYPTO_refcount_inc(&references_); } |
bssl::RefCounted<ssl_credential_st>::UpRefInternal() Line | Count | Source | 150 | 44.1k | void UpRefInternal() { CRYPTO_refcount_inc(&references_); } |
bssl::RefCounted<ssl_ctx_st>::UpRefInternal() Line | Count | Source | 150 | 236k | void UpRefInternal() { CRYPTO_refcount_inc(&references_); } |
bssl::RefCounted<ssl_session_st>::UpRefInternal() Line | Count | Source | 150 | 138k | void UpRefInternal() { CRYPTO_refcount_inc(&references_); } |
|
151 | 968k | void DecRefInternal() { |
152 | 968k | if (CRYPTO_refcount_dec_and_test_zero(&references_)) { |
153 | 548k | Derived *d = static_cast<Derived *>(this); |
154 | 548k | d->~Derived(); |
155 | 548k | OPENSSL_free(d); |
156 | 548k | } |
157 | 968k | } bssl::RefCounted<ssl_ech_keys_st>::DecRefInternal() Line | Count | Source | 151 | 13.7k | void DecRefInternal() { | 152 | 13.7k | if (CRYPTO_refcount_dec_and_test_zero(&references_)) { | 153 | 13.3k | Derived *d = static_cast<Derived *>(this); | 154 | 13.3k | d->~Derived(); | 155 | 13.3k | OPENSSL_free(d); | 156 | 13.3k | } | 157 | 13.7k | } |
bssl::RefCounted<ssl_credential_st>::DecRefInternal() Line | Count | Source | 151 | 285k | void DecRefInternal() { | 152 | 285k | if (CRYPTO_refcount_dec_and_test_zero(&references_)) { | 153 | 241k | Derived *d = static_cast<Derived *>(this); | 154 | 241k | d->~Derived(); | 155 | 241k | OPENSSL_free(d); | 156 | 241k | } | 157 | 285k | } |
bssl::RefCounted<ssl_ctx_st>::DecRefInternal() Line | Count | Source | 151 | 241k | void DecRefInternal() { | 152 | 241k | if (CRYPTO_refcount_dec_and_test_zero(&references_)) { | 153 | 4.80k | Derived *d = static_cast<Derived *>(this); | 154 | 4.80k | d->~Derived(); | 155 | 4.80k | OPENSSL_free(d); | 156 | 4.80k | } | 157 | 241k | } |
bssl::RefCounted<ssl_session_st>::DecRefInternal() Line | Count | Source | 151 | 427k | void DecRefInternal() { | 152 | 427k | if (CRYPTO_refcount_dec_and_test_zero(&references_)) { | 153 | 288k | Derived *d = static_cast<Derived *>(this); | 154 | 288k | d->~Derived(); | 155 | 288k | OPENSSL_free(d); | 156 | 288k | } | 157 | 427k | } |
|
158 | | |
159 | | protected: |
160 | | // Ensure that only `Derived`, which must inherit from `RefCounted<Derived>`, |
161 | | // can call the constructor. This catches bugs where someone inherited from |
162 | | // the wrong base. |
163 | | class CheckSubClass { |
164 | | private: |
165 | | friend Derived; |
166 | | CheckSubClass() = default; |
167 | | }; |
168 | 548k | RefCounted(CheckSubClass) { |
169 | 548k | static_assert(std::is_base_of<RefCounted, Derived>::value, |
170 | 548k | "Derived must subclass RefCounted<Derived>"); |
171 | 548k | } bssl::RefCounted<ssl_ech_keys_st>::RefCounted(bssl::RefCounted<ssl_ech_keys_st>::CheckSubClass) Line | Count | Source | 168 | 13.3k | RefCounted(CheckSubClass) { | 169 | 13.3k | static_assert(std::is_base_of<RefCounted, Derived>::value, | 170 | 13.3k | "Derived must subclass RefCounted<Derived>"); | 171 | 13.3k | } |
bssl::RefCounted<ssl_credential_st>::RefCounted(bssl::RefCounted<ssl_credential_st>::CheckSubClass) Line | Count | Source | 168 | 241k | RefCounted(CheckSubClass) { | 169 | 241k | static_assert(std::is_base_of<RefCounted, Derived>::value, | 170 | 241k | "Derived must subclass RefCounted<Derived>"); | 171 | 241k | } |
bssl::RefCounted<ssl_ctx_st>::RefCounted(bssl::RefCounted<ssl_ctx_st>::CheckSubClass) Line | Count | Source | 168 | 4.82k | RefCounted(CheckSubClass) { | 169 | 4.82k | static_assert(std::is_base_of<RefCounted, Derived>::value, | 170 | 4.82k | "Derived must subclass RefCounted<Derived>"); | 171 | 4.82k | } |
bssl::RefCounted<ssl_session_st>::RefCounted(bssl::RefCounted<ssl_session_st>::CheckSubClass) Line | Count | Source | 168 | 288k | RefCounted(CheckSubClass) { | 169 | 288k | static_assert(std::is_base_of<RefCounted, Derived>::value, | 170 | 288k | "Derived must subclass RefCounted<Derived>"); | 171 | 288k | } |
|
172 | | |
173 | | ~RefCounted() = default; |
174 | | |
175 | | private: |
176 | | CRYPTO_refcount_t references_ = 1; |
177 | | }; |
178 | | |
179 | | |
180 | | // Protocol versions. |
181 | | // |
182 | | // Due to DTLS's historical wire version differences, we maintain two notions of |
183 | | // version. |
184 | | // |
185 | | // The "version" or "wire version" is the actual 16-bit value that appears on |
186 | | // the wire. It uniquely identifies a version and is also used at API |
187 | | // boundaries. The set of supported versions differs between TLS and DTLS. Wire |
188 | | // versions are opaque values and may not be compared numerically. |
189 | | // |
190 | | // The "protocol version" identifies the high-level handshake variant being |
191 | | // used. DTLS versions map to the corresponding TLS versions. Protocol versions |
192 | | // are sequential and may be compared numerically. |
193 | | |
194 | | // ssl_protocol_version_from_wire sets |*out| to the protocol version |
195 | | // corresponding to wire version |version| and returns true. If |version| is not |
196 | | // a valid TLS or DTLS version, it returns false. |
197 | | // |
198 | | // Note this simultaneously handles both DTLS and TLS. Use one of the |
199 | | // higher-level functions below for most operations. |
200 | | bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version); |
201 | | |
202 | | // ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the |
203 | | // minimum and maximum enabled protocol versions, respectively. |
204 | | bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version, |
205 | | uint16_t *out_max_version); |
206 | | |
207 | | // ssl_supports_version returns whether |hs| supports |version|. |
208 | | bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version); |
209 | | |
210 | | // ssl_method_supports_version returns whether |method| supports |version|. |
211 | | bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method, |
212 | | uint16_t version); |
213 | | |
214 | | // ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in |
215 | | // decreasing preference order. The version list is filtered to those whose |
216 | | // protocol version is at least |extra_min_version|. |
217 | | bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb, |
218 | | uint16_t extra_min_version); |
219 | | |
220 | | // ssl_negotiate_version negotiates a common version based on |hs|'s preferences |
221 | | // and the peer preference list in |peer_versions|. On success, it returns true |
222 | | // and sets |*out_version| to the selected version. Otherwise, it returns false |
223 | | // and sets |*out_alert| to an alert to send. |
224 | | bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
225 | | uint16_t *out_version, const CBS *peer_versions); |
226 | | |
227 | | // ssl_has_final_version returns whether |ssl| has determined the final version. |
228 | | // This may be used to distinguish the predictive 0-RTT version from the final |
229 | | // one. |
230 | | bool ssl_has_final_version(const SSL *ssl); |
231 | | |
232 | | // ssl_protocol_version returns |ssl|'s protocol version. It is an error to |
233 | | // call this function before the version is determined. |
234 | | uint16_t ssl_protocol_version(const SSL *ssl); |
235 | | |
236 | | // Cipher suites. |
237 | | |
238 | | BSSL_NAMESPACE_END |
239 | | |
240 | | struct ssl_cipher_st { |
241 | | // name is the OpenSSL name for the cipher. |
242 | | const char *name; |
243 | | // standard_name is the IETF name for the cipher. |
244 | | const char *standard_name; |
245 | | // id is the cipher suite value bitwise OR-d with 0x03000000. |
246 | | uint32_t id; |
247 | | |
248 | | // algorithm_* determine the cipher suite. See constants below for the values. |
249 | | uint32_t algorithm_mkey; |
250 | | uint32_t algorithm_auth; |
251 | | uint32_t algorithm_enc; |
252 | | uint32_t algorithm_mac; |
253 | | uint32_t algorithm_prf; |
254 | | }; |
255 | | |
256 | | BSSL_NAMESPACE_BEGIN |
257 | | |
258 | | // Bits for |algorithm_mkey| (key exchange algorithm). |
259 | 122k | #define SSL_kRSA 0x00000001u |
260 | 151k | #define SSL_kECDHE 0x00000002u |
261 | | // SSL_kPSK is only set for plain PSK, not ECDHE_PSK. |
262 | 131k | #define SSL_kPSK 0x00000004u |
263 | 4.76M | #define SSL_kGENERIC 0x00000008u |
264 | | |
265 | | // Bits for |algorithm_auth| (server authentication). |
266 | 573k | #define SSL_aRSA_SIGN 0x00000001u |
267 | 573k | #define SSL_aRSA_DECRYPT 0x00000002u |
268 | 535k | #define SSL_aECDSA 0x00000004u |
269 | | // SSL_aPSK is set for both PSK and ECDHE_PSK. |
270 | 311k | #define SSL_aPSK 0x00000008u |
271 | 2.35M | #define SSL_aGENERIC 0x00000010u |
272 | | |
273 | 521k | #define SSL_aCERT (SSL_aRSA_SIGN | SSL_aRSA_DECRYPT | SSL_aECDSA) |
274 | | |
275 | | // Bits for |algorithm_enc| (symmetric encryption). |
276 | 371k | #define SSL_3DES 0x00000001u |
277 | 217k | #define SSL_AES128 0x00000002u |
278 | 209k | #define SSL_AES256 0x00000004u |
279 | 301k | #define SSL_AES128GCM 0x00000008u |
280 | 215k | #define SSL_AES256GCM 0x00000010u |
281 | 247k | #define SSL_CHACHA20POLY1305 0x00000020u |
282 | | |
283 | | #define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM) |
284 | | |
285 | | // Bits for |algorithm_mac| (symmetric authentication). |
286 | 146k | #define SSL_SHA1 0x00000001u |
287 | 0 | #define SSL_SHA256 0x00000002u |
288 | | // SSL_AEAD is set for all AEADs. |
289 | 329k | #define SSL_AEAD 0x00000004u |
290 | | |
291 | | // Bits for |algorithm_prf| (handshake digest). |
292 | 1.27M | #define SSL_HANDSHAKE_MAC_DEFAULT 0x1 |
293 | 152k | #define SSL_HANDSHAKE_MAC_SHA256 0x2 |
294 | 48.3k | #define SSL_HANDSHAKE_MAC_SHA384 0x4 |
295 | | |
296 | | // SSL_MAX_MD_SIZE is size of the largest hash function used in TLS, SHA-384. |
297 | | #define SSL_MAX_MD_SIZE 48 |
298 | | |
299 | | // An SSLCipherPreferenceList contains a list of SSL_CIPHERs with equal- |
300 | | // preference groups. For TLS clients, the groups are moot because the server |
301 | | // picks the cipher and groups cannot be expressed on the wire. However, for |
302 | | // servers, the equal-preference groups allow the client's preferences to be |
303 | | // partially respected. (This only has an effect with |
304 | | // SSL_OP_CIPHER_SERVER_PREFERENCE). |
305 | | // |
306 | | // The equal-preference groups are expressed by grouping SSL_CIPHERs together. |
307 | | // All elements of a group have the same priority: no ordering is expressed |
308 | | // within a group. |
309 | | // |
310 | | // The values in |ciphers| are in one-to-one correspondence with |
311 | | // |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of |
312 | | // bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to |
313 | | // indicate that the corresponding SSL_CIPHER is not the last element of a |
314 | | // group, or 0 to indicate that it is. |
315 | | // |
316 | | // For example, if |in_group_flags| contains all zeros then that indicates a |
317 | | // traditional, fully-ordered preference. Every SSL_CIPHER is the last element |
318 | | // of the group (i.e. they are all in a one-element group). |
319 | | // |
320 | | // For a more complex example, consider: |
321 | | // ciphers: A B C D E F |
322 | | // in_group_flags: 1 1 0 0 1 0 |
323 | | // |
324 | | // That would express the following, order: |
325 | | // |
326 | | // A E |
327 | | // B -> D -> F |
328 | | // C |
329 | | struct SSLCipherPreferenceList { |
330 | | static constexpr bool kAllowUniquePtr = true; |
331 | | |
332 | 16.4k | SSLCipherPreferenceList() = default; |
333 | | ~SSLCipherPreferenceList(); |
334 | | |
335 | | bool Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers, |
336 | | Span<const bool> in_group_flags); |
337 | | bool Init(const SSLCipherPreferenceList &); |
338 | | |
339 | | void Remove(const SSL_CIPHER *cipher); |
340 | | |
341 | | UniquePtr<STACK_OF(SSL_CIPHER)> ciphers; |
342 | | bool *in_group_flags = nullptr; |
343 | | }; |
344 | | |
345 | | // AllCiphers returns an array of all supported ciphers, sorted by id. |
346 | | Span<const SSL_CIPHER> AllCiphers(); |
347 | | |
348 | | // ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD |
349 | | // object for |cipher| protocol version |version|. It sets |*out_mac_secret_len| |
350 | | // and |*out_fixed_iv_len| to the MAC key length and fixed IV length, |
351 | | // respectively. The MAC key length is zero except for legacy block and stream |
352 | | // ciphers. It returns true on success and false on error. |
353 | | bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead, |
354 | | size_t *out_mac_secret_len, |
355 | | size_t *out_fixed_iv_len, const SSL_CIPHER *cipher, |
356 | | uint16_t version); |
357 | | |
358 | | // ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and |
359 | | // |cipher|. |
360 | | const EVP_MD *ssl_get_handshake_digest(uint16_t version, |
361 | | const SSL_CIPHER *cipher); |
362 | | |
363 | | // ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a |
364 | | // newly-allocated |SSLCipherPreferenceList| containing the result. It returns |
365 | | // true on success and false on failure. If |strict| is true, nonsense will be |
366 | | // rejected. If false, nonsense will be silently ignored. An empty result is |
367 | | // considered an error regardless of |strict|. |has_aes_hw| indicates if the |
368 | | // list should be ordered based on having support for AES in hardware or not. |
369 | | bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list, |
370 | | const bool has_aes_hw, const char *rule_str, |
371 | | bool strict); |
372 | | |
373 | | // ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth| |
374 | | // values suitable for use with |key| in TLS 1.2 and below. |sign_ok| indicates |
375 | | // whether |key| may be used for signing. |
376 | | uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key, bool sign_ok); |
377 | | |
378 | | // ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the |
379 | | // server and, optionally, the client with a certificate. |
380 | | bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher); |
381 | | |
382 | | // ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a |
383 | | // ServerKeyExchange message. |
384 | | // |
385 | | // This function may return false while still allowing |cipher| an optional |
386 | | // ServerKeyExchange. This is the case for plain PSK ciphers. |
387 | | bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher); |
388 | | |
389 | | // ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the |
390 | | // length of an encrypted 1-byte record, for use in record-splitting. Otherwise |
391 | | // it returns zero. |
392 | | size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher); |
393 | | |
394 | | // ssl_choose_tls13_cipher returns an |SSL_CIPHER| corresponding with the best |
395 | | // available from |cipher_suites| compatible with |version| and |policy|. It |
396 | | // returns NULL if there isn't a compatible cipher. |has_aes_hw| indicates if |
397 | | // the choice should be made as if support for AES in hardware is available. |
398 | | const SSL_CIPHER *ssl_choose_tls13_cipher(CBS cipher_suites, bool has_aes_hw, |
399 | | uint16_t version, |
400 | | enum ssl_compliance_policy_t policy); |
401 | | |
402 | | // ssl_tls13_cipher_meets_policy returns true if |cipher_id| is acceptable given |
403 | | // |policy|. |
404 | | bool ssl_tls13_cipher_meets_policy(uint16_t cipher_id, |
405 | | enum ssl_compliance_policy_t policy); |
406 | | |
407 | | // ssl_cipher_is_deprecated returns true if |cipher| is deprecated. |
408 | | OPENSSL_EXPORT bool ssl_cipher_is_deprecated(const SSL_CIPHER *cipher); |
409 | | |
410 | | |
411 | | // Transcript layer. |
412 | | |
413 | | // SSLTranscript maintains the handshake transcript as a combination of a |
414 | | // buffer and running hash. |
415 | | class SSLTranscript { |
416 | | public: |
417 | | explicit SSLTranscript(bool is_dtls); |
418 | | ~SSLTranscript(); |
419 | | |
420 | | SSLTranscript(SSLTranscript &&other) = default; |
421 | 0 | SSLTranscript &operator=(SSLTranscript &&other) = default; |
422 | | |
423 | | // Init initializes the handshake transcript. If called on an existing |
424 | | // transcript, it resets the transcript and hash. It returns true on success |
425 | | // and false on failure. |
426 | | bool Init(); |
427 | | |
428 | | // InitHash initializes the handshake hash based on the PRF and contents of |
429 | | // the handshake transcript. Subsequent calls to |Update| will update the |
430 | | // rolling hash. It returns one on success and zero on failure. It is an error |
431 | | // to call this function after the handshake buffer is released. This may be |
432 | | // called multiple times to change the hash function. |
433 | | bool InitHash(uint16_t version, const SSL_CIPHER *cipher); |
434 | | |
435 | | // UpdateForHelloRetryRequest resets the rolling hash with the |
436 | | // HelloRetryRequest construction. It returns true on success and false on |
437 | | // failure. It is an error to call this function before the handshake buffer |
438 | | // is released. |
439 | | bool UpdateForHelloRetryRequest(); |
440 | | |
441 | | // CopyToHashContext initializes |ctx| with |digest| and the data thus far in |
442 | | // the transcript. It returns true on success and false on failure. If the |
443 | | // handshake buffer is still present, |digest| may be any supported digest. |
444 | | // Otherwise, |digest| must match the transcript hash. |
445 | | bool CopyToHashContext(EVP_MD_CTX *ctx, const EVP_MD *digest) const; |
446 | | |
447 | 34.2k | Span<const uint8_t> buffer() const { |
448 | 34.2k | return Span(reinterpret_cast<const uint8_t *>(buffer_->data), |
449 | 34.2k | buffer_->length); |
450 | 34.2k | } |
451 | | |
452 | | // FreeBuffer releases the handshake buffer. Subsequent calls to |
453 | | // |Update| will not update the handshake buffer. |
454 | | void FreeBuffer(); |
455 | | |
456 | | // DigestLen returns the length of the PRF hash. |
457 | | size_t DigestLen() const; |
458 | | |
459 | | // Digest returns the PRF hash. For TLS 1.1 and below, this is |
460 | | // |EVP_md5_sha1|. |
461 | | const EVP_MD *Digest() const; |
462 | | |
463 | | // Update adds |in| to the handshake buffer and handshake hash, whichever is |
464 | | // enabled. It returns true on success and false on failure. |
465 | | bool Update(Span<const uint8_t> in); |
466 | | |
467 | | // GetHash writes the handshake hash to |out| which must have room for at |
468 | | // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to |
469 | | // the number of bytes written. Otherwise, it returns false. |
470 | | bool GetHash(uint8_t *out, size_t *out_len) const; |
471 | | |
472 | | // GetFinishedMAC computes the MAC for the Finished message into the bytes |
473 | | // pointed by |out| and writes the number of bytes to |*out_len|. |out| must |
474 | | // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false |
475 | | // on failure. |
476 | | bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session, |
477 | | bool from_server) const; |
478 | | |
479 | | private: |
480 | | // HashBuffer initializes |ctx| to use |digest| and writes the contents of |
481 | | // |buffer_| to |ctx|. If this SSLTranscript is for DTLS 1.3, the appropriate |
482 | | // bytes in |buffer_| will be skipped when hashing the buffer. |
483 | | bool HashBuffer(EVP_MD_CTX *ctx, const EVP_MD *digest) const; |
484 | | |
485 | | // AddToBufferOrHash directly adds the contents of |in| to |buffer_| and/or |
486 | | // |hash_|. |
487 | | bool AddToBufferOrHash(Span<const uint8_t> in); |
488 | | |
489 | | // buffer_, if non-null, contains the handshake transcript. |
490 | | UniquePtr<BUF_MEM> buffer_; |
491 | | // hash, if initialized with an |EVP_MD|, maintains the handshake hash. |
492 | | ScopedEVP_MD_CTX hash_; |
493 | | // is_dtls_ indicates whether this is a transcript for a DTLS connection. |
494 | | bool is_dtls_ : 1; |
495 | | // version_ contains the version for the connection (if known). |
496 | | uint16_t version_ = 0; |
497 | | }; |
498 | | |
499 | | // tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret| |
500 | | // as the secret and |label| as the label. |seed1| and |seed2| are concatenated |
501 | | // to form the seed parameter. It returns true on success and false on failure. |
502 | | bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out, |
503 | | Span<const uint8_t> secret, std::string_view label, |
504 | | Span<const uint8_t> seed1, Span<const uint8_t> seed2); |
505 | | |
506 | | |
507 | | // Encryption layer. |
508 | | |
509 | | // SSLAEADContext contains information about an AEAD that is being used to |
510 | | // encrypt an SSL connection. |
511 | | class SSLAEADContext { |
512 | | public: |
513 | | explicit SSLAEADContext(const SSL_CIPHER *cipher); |
514 | | ~SSLAEADContext(); |
515 | | static constexpr bool kAllowUniquePtr = true; |
516 | | |
517 | | SSLAEADContext(const SSLAEADContext &&) = delete; |
518 | | SSLAEADContext &operator=(const SSLAEADContext &&) = delete; |
519 | | |
520 | | // CreateNullCipher creates an |SSLAEADContext| for the null cipher. |
521 | | static UniquePtr<SSLAEADContext> CreateNullCipher(); |
522 | | |
523 | | // Create creates an |SSLAEADContext| using the supplied key material. It |
524 | | // returns nullptr on error. Only one of |Open| or |Seal| may be used with the |
525 | | // resulting object, depending on |direction|. |version| is the wire version. |
526 | | static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction, |
527 | | uint16_t version, |
528 | | const SSL_CIPHER *cipher, |
529 | | Span<const uint8_t> enc_key, |
530 | | Span<const uint8_t> mac_key, |
531 | | Span<const uint8_t> fixed_iv); |
532 | | |
533 | | // CreatePlaceholderForQUIC creates a placeholder |SSLAEADContext| for the |
534 | | // given cipher. The resulting object can be queried for various properties |
535 | | // but cannot encrypt or decrypt data. |
536 | | static UniquePtr<SSLAEADContext> CreatePlaceholderForQUIC( |
537 | | const SSL_CIPHER *cipher); |
538 | | |
539 | 42.1k | const SSL_CIPHER *cipher() const { return cipher_; } |
540 | | |
541 | | // is_null_cipher returns true if this is the null cipher. |
542 | 5.89M | bool is_null_cipher() const { return !cipher_; } |
543 | | |
544 | | // ExplicitNonceLen returns the length of the explicit nonce. |
545 | | size_t ExplicitNonceLen() const; |
546 | | |
547 | | // MaxOverhead returns the maximum overhead of calling |Seal|. |
548 | | size_t MaxOverhead() const; |
549 | | |
550 | | // MaxSealInputLen returns the maximum length for |Seal| that can fit in |
551 | | // |max_out| output bytes, or zero if no input may fit. |
552 | | size_t MaxSealInputLen(size_t max_out) const; |
553 | | |
554 | | // SuffixLen calculates the suffix length written by |SealScatter| and writes |
555 | | // it to |*out_suffix_len|. It returns true on success and false on error. |
556 | | // |in_len| and |extra_in_len| should equal the argument of the same names |
557 | | // passed to |SealScatter|. |
558 | | bool SuffixLen(size_t *out_suffix_len, size_t in_len, |
559 | | size_t extra_in_len) const; |
560 | | |
561 | | // CiphertextLen calculates the total ciphertext length written by |
562 | | // |SealScatter| and writes it to |*out_len|. It returns true on success and |
563 | | // false on error. |in_len| and |extra_in_len| should equal the argument of |
564 | | // the same names passed to |SealScatter|. |
565 | | bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const; |
566 | | |
567 | | // Open authenticates and decrypts |in| in-place. On success, it sets |*out| |
568 | | // to the plaintext in |in| and returns true. Otherwise, it returns |
569 | | // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|. |
570 | | bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version, |
571 | | uint64_t seqnum, Span<const uint8_t> header, Span<uint8_t> in); |
572 | | |
573 | | // Seal encrypts and authenticates |in_len| bytes from |in| and writes the |
574 | | // result to |out|. It returns true on success and false on error. |
575 | | // |
576 | | // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|. |
577 | | bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type, |
578 | | uint16_t record_version, uint64_t seqnum, |
579 | | Span<const uint8_t> header, const uint8_t *in, size_t in_len); |
580 | | |
581 | | // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits |
582 | | // the result between |out_prefix|, |out| and |out_suffix|. It returns one on |
583 | | // success and zero on error. |
584 | | // |
585 | | // On successful return, exactly |ExplicitNonceLen| bytes are written to |
586 | | // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to |
587 | | // |out_suffix|. |
588 | | // |
589 | | // |extra_in| may point to an additional plaintext buffer. If present, |
590 | | // |extra_in_len| additional bytes are encrypted and authenticated, and the |
591 | | // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should |
592 | | // be used to size |out_suffix| accordingly. |
593 | | // |
594 | | // If |in| and |out| alias then |out| must be == |in|. Other arguments may not |
595 | | // alias anything. |
596 | | bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix, |
597 | | uint8_t type, uint16_t record_version, uint64_t seqnum, |
598 | | Span<const uint8_t> header, const uint8_t *in, size_t in_len, |
599 | | const uint8_t *extra_in, size_t extra_in_len); |
600 | | |
601 | | bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const; |
602 | | |
603 | | private: |
604 | | // GetAdditionalData returns the additional data, writing into |storage| if |
605 | | // necessary. |
606 | | Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type, |
607 | | uint16_t record_version, |
608 | | uint64_t seqnum, size_t plaintext_len, |
609 | | Span<const uint8_t> header); |
610 | | |
611 | | const SSL_CIPHER *cipher_; |
612 | | ScopedEVP_AEAD_CTX ctx_; |
613 | | // fixed_nonce_ contains any bytes of the nonce that are fixed for all |
614 | | // records. |
615 | | InplaceVector<uint8_t, 12> fixed_nonce_; |
616 | | uint8_t variable_nonce_len_ = 0; |
617 | | // variable_nonce_included_in_record_ is true if the variable nonce |
618 | | // for a record is included as a prefix before the ciphertext. |
619 | | bool variable_nonce_included_in_record_ : 1; |
620 | | // random_variable_nonce_ is true if the variable nonce is |
621 | | // randomly generated, rather than derived from the sequence |
622 | | // number. |
623 | | bool random_variable_nonce_ : 1; |
624 | | // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the |
625 | | // variable nonce rather than prepended. |
626 | | bool xor_fixed_nonce_ : 1; |
627 | | // omit_length_in_ad_ is true if the length should be omitted in the |
628 | | // AEAD's ad parameter. |
629 | | bool omit_length_in_ad_ : 1; |
630 | | // ad_is_header_ is true if the AEAD's ad parameter is the record header. |
631 | | bool ad_is_header_ : 1; |
632 | | }; |
633 | | |
634 | | |
635 | | // DTLS replay bitmap. |
636 | | |
637 | | // DTLSReplayBitmap maintains a sliding window of sequence numbers to detect |
638 | | // replayed packets. |
639 | | class DTLSReplayBitmap { |
640 | | public: |
641 | | // ShouldDiscard returns true if |seq_num| has been seen in |
642 | | // |bitmap| or is stale. Otherwise it returns false. |
643 | | bool ShouldDiscard(uint64_t seqnum) const; |
644 | | |
645 | | // Record updates the bitmap to record receipt of sequence number |
646 | | // |seq_num|. It slides the window forward if needed. It is an error to call |
647 | | // this function on a stale sequence number. |
648 | | void Record(uint64_t seqnum); |
649 | | |
650 | 79.5k | uint64_t max_seq_num() const { return max_seq_num_; } |
651 | | |
652 | | private: |
653 | | // map is a bitset of sequence numbers that have been seen. Bit i corresponds |
654 | | // to |max_seq_num_ - i|. |
655 | | std::bitset<256> map_; |
656 | | // max_seq_num_ is the largest sequence number seen so far as a 64-bit |
657 | | // integer, or zero if none have been seen. |
658 | | uint64_t max_seq_num_ = 0; |
659 | | }; |
660 | | |
661 | | // reconstruct_seqnum takes the low order bits of a record sequence number from |
662 | | // the wire and reconstructs the full sequence number. It does so using the |
663 | | // algorithm described in section 4.2.2 of RFC 9147, where |wire_seq| is the |
664 | | // low bits of the sequence number as seen on the wire, |seq_mask| is a bitmask |
665 | | // of 8 or 16 1 bits corresponding to the length of the sequence number on the |
666 | | // wire, and |max_valid_seqnum| is the largest sequence number of a record |
667 | | // successfully deprotected in this epoch. This function returns the sequence |
668 | | // number that is numerically closest to one plus |max_valid_seqnum| that when |
669 | | // bitwise and-ed with |seq_mask| equals |wire_seq|. |
670 | | // |
671 | | // |max_valid_seqnum| must be most 2^48-1, in which case the output will also be |
672 | | // at most 2^48-1. |
673 | | OPENSSL_EXPORT uint64_t reconstruct_seqnum(uint16_t wire_seq, uint64_t seq_mask, |
674 | | uint64_t max_valid_seqnum); |
675 | | |
676 | | |
677 | | // Record layer. |
678 | | |
679 | | class DTLSRecordNumber { |
680 | | public: |
681 | | static constexpr uint64_t kMaxSequence = (uint64_t{1} << 48) - 1; |
682 | | |
683 | 570k | DTLSRecordNumber() = default; |
684 | 89.9k | DTLSRecordNumber(uint16_t epoch, uint64_t sequence) { |
685 | 89.9k | BSSL_CHECK(sequence <= kMaxSequence); |
686 | 89.9k | combined_ = (uint64_t{epoch} << 48) | sequence; |
687 | 89.9k | } |
688 | | |
689 | 93.5k | static DTLSRecordNumber FromCombined(uint64_t combined) { |
690 | 93.5k | return DTLSRecordNumber(combined); |
691 | 93.5k | } |
692 | | |
693 | 2.74k | bool operator==(DTLSRecordNumber r) const { |
694 | 2.74k | return combined() == r.combined(); |
695 | 2.74k | } |
696 | 0 | bool operator!=(DTLSRecordNumber r) const { return !((*this) == r); } |
697 | 110k | bool operator<(DTLSRecordNumber r) const { return combined() < r.combined(); } |
698 | | |
699 | 310k | uint64_t combined() const { return combined_; } |
700 | 465k | uint16_t epoch() const { return combined_ >> 48; } |
701 | 476k | uint64_t sequence() const { return combined_ & kMaxSequence; } |
702 | | |
703 | 72.7k | bool HasNext() const { return sequence() < kMaxSequence; } |
704 | 36.3k | DTLSRecordNumber Next() const { |
705 | 36.3k | BSSL_CHECK(HasNext()); |
706 | | // This will not overflow into the epoch. |
707 | 36.3k | return DTLSRecordNumber::FromCombined(combined_ + 1); |
708 | 36.3k | } |
709 | | |
710 | | private: |
711 | 93.5k | explicit DTLSRecordNumber(uint64_t combined) : combined_(combined) {} |
712 | | |
713 | | uint64_t combined_ = 0; |
714 | | }; |
715 | | |
716 | | class RecordNumberEncrypter { |
717 | | public: |
718 | | static constexpr bool kAllowUniquePtr = true; |
719 | | static constexpr size_t kMaxKeySize = 32; |
720 | | |
721 | | // Create returns a DTLS 1.3 record number encrypter for |traffic_secret|, or |
722 | | // nullptr on error. |
723 | | static UniquePtr<RecordNumberEncrypter> Create( |
724 | | const SSL_CIPHER *cipher, Span<const uint8_t> traffic_secret); |
725 | | |
726 | 42.1k | virtual ~RecordNumberEncrypter() = default; |
727 | | virtual size_t KeySize() = 0; |
728 | | virtual bool SetKey(Span<const uint8_t> key) = 0; |
729 | | virtual bool GenerateMask(Span<uint8_t> out, Span<const uint8_t> sample) = 0; |
730 | | }; |
731 | | |
732 | | struct DTLSReadEpoch { |
733 | | static constexpr bool kAllowUniquePtr = true; |
734 | | |
735 | | // TODO(davidben): This could be made slightly more compact if |bitmap| stored |
736 | | // a DTLSRecordNumber. |
737 | | uint16_t epoch = 0; |
738 | | UniquePtr<SSLAEADContext> aead; |
739 | | UniquePtr<RecordNumberEncrypter> rn_encrypter; |
740 | | DTLSReplayBitmap bitmap; |
741 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> traffic_secret; |
742 | | }; |
743 | | |
744 | | struct DTLSWriteEpoch { |
745 | | static constexpr bool kAllowUniquePtr = true; |
746 | | |
747 | 250k | uint16_t epoch() const { return next_record.epoch(); } |
748 | | |
749 | | DTLSRecordNumber next_record; |
750 | | UniquePtr<SSLAEADContext> aead; |
751 | | UniquePtr<RecordNumberEncrypter> rn_encrypter; |
752 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> traffic_secret; |
753 | | }; |
754 | | |
755 | | // ssl_record_prefix_len returns the length of the prefix before the ciphertext |
756 | | // of a record for |ssl|. |
757 | | // |
758 | | // TODO(davidben): Expose this as part of public API once the high-level |
759 | | // buffer-free APIs are available. |
760 | | size_t ssl_record_prefix_len(const SSL *ssl); |
761 | | |
762 | | enum ssl_open_record_t { |
763 | | ssl_open_record_success, |
764 | | ssl_open_record_discard, |
765 | | ssl_open_record_partial, |
766 | | ssl_open_record_close_notify, |
767 | | ssl_open_record_error, |
768 | | }; |
769 | | |
770 | | // tls_open_record decrypts a record from |in| in-place. |
771 | | // |
772 | | // If the input did not contain a complete record, it returns |
773 | | // |ssl_open_record_partial|. It sets |*out_consumed| to the total number of |
774 | | // bytes necessary. It is guaranteed that a successful call to |tls_open_record| |
775 | | // will consume at least that many bytes. |
776 | | // |
777 | | // Otherwise, it sets |*out_consumed| to the number of bytes of input |
778 | | // consumed. Note that input may be consumed on all return codes if a record was |
779 | | // decrypted. |
780 | | // |
781 | | // On success, it returns |ssl_open_record_success|. It sets |*out_type| to the |
782 | | // record type and |*out| to the record body in |in|. Note that |*out| may be |
783 | | // empty. |
784 | | // |
785 | | // If a record was successfully processed but should be discarded, it returns |
786 | | // |ssl_open_record_discard|. |
787 | | // |
788 | | // If a record was successfully processed but is a close_notify, it returns |
789 | | // |ssl_open_record_close_notify|. |
790 | | // |
791 | | // On failure or fatal alert, it returns |ssl_open_record_error| and sets |
792 | | // |*out_alert| to an alert to emit, or zero if no alert should be emitted. |
793 | | enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, |
794 | | Span<uint8_t> *out, size_t *out_consumed, |
795 | | uint8_t *out_alert, Span<uint8_t> in); |
796 | | |
797 | | // dtls_open_record implements |tls_open_record| for DTLS. It only returns |
798 | | // |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to |
799 | | // zero. The caller should read one packet and try again. On success, |
800 | | // |*out_number| is set to the record number of the record. |
801 | | enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, |
802 | | DTLSRecordNumber *out_number, |
803 | | Span<uint8_t> *out, |
804 | | size_t *out_consumed, |
805 | | uint8_t *out_alert, Span<uint8_t> in); |
806 | | |
807 | | // ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher |
808 | | // state needs record-splitting and zero otherwise. |
809 | | bool ssl_needs_record_splitting(const SSL *ssl); |
810 | | |
811 | | // tls_seal_record seals a new record of type |type| and body |in| and writes it |
812 | | // to |out|. At most |max_out| bytes will be written. It returns true on success |
813 | | // and false on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC |
814 | | // 1/n-1 record splitting and may write two records concatenated. |
815 | | // |
816 | | // For a large record, the bulk of the ciphertext will begin |
817 | | // |tls_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may |
818 | | // improve performance. It writes at most |in_len| + |SSL_max_seal_overhead| |
819 | | // bytes to |out|. |
820 | | // |
821 | | // |in| and |out| may not alias. |
822 | | bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
823 | | uint8_t type, const uint8_t *in, size_t in_len); |
824 | | |
825 | | // dtls_record_header_write_len returns the length of the record header that |
826 | | // will be written at |epoch|. |
827 | | size_t dtls_record_header_write_len(const SSL *ssl, uint16_t epoch); |
828 | | |
829 | | // dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a |
830 | | // record. |
831 | | size_t dtls_max_seal_overhead(const SSL *ssl, uint16_t epoch); |
832 | | |
833 | | // dtls_seal_prefix_len returns the number of bytes of prefix to reserve in |
834 | | // front of the plaintext when sealing a record in-place. |
835 | | size_t dtls_seal_prefix_len(const SSL *ssl, uint16_t epoch); |
836 | | |
837 | | // dtls_seal_max_input_len returns the maximum number of input bytes that can |
838 | | // fit in a record of up to |max_out| bytes, or zero if none may fit. |
839 | | size_t dtls_seal_max_input_len(const SSL *ssl, uint16_t epoch, size_t max_out); |
840 | | |
841 | | // dtls_get_read_epoch and dtls_get_write_epoch return the epoch corresponding |
842 | | // to |epoch| or nullptr if there is none. |
843 | | DTLSReadEpoch *dtls_get_read_epoch(const SSL *ssl, uint16_t epoch); |
844 | | DTLSWriteEpoch *dtls_get_write_epoch(const SSL *ssl, uint16_t epoch); |
845 | | |
846 | | // dtls_seal_record implements |tls_seal_record| for DTLS. |epoch| selects which |
847 | | // epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out| may |
848 | | // alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes |
849 | | // ahead of |out|. On success, |*out_number| is set to the record number of the |
850 | | // record. |
851 | | bool dtls_seal_record(SSL *ssl, DTLSRecordNumber *out_number, uint8_t *out, |
852 | | size_t *out_len, size_t max_out, uint8_t type, |
853 | | const uint8_t *in, size_t in_len, uint16_t epoch); |
854 | | |
855 | | // ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown |
856 | | // state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|, |
857 | | // |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as |
858 | | // appropriate. |
859 | | enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert, |
860 | | Span<const uint8_t> in); |
861 | | |
862 | | |
863 | | // Private key operations. |
864 | | |
865 | | // ssl_private_key_* perform the corresponding operation on |
866 | | // |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they |
867 | | // call the corresponding function or |complete| depending on whether there is a |
868 | | // pending operation. Otherwise, they implement the operation with |
869 | | // |EVP_PKEY|. |
870 | | |
871 | | enum ssl_private_key_result_t ssl_private_key_sign( |
872 | | SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
873 | | uint16_t sigalg, Span<const uint8_t> in); |
874 | | |
875 | | enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs, |
876 | | uint8_t *out, |
877 | | size_t *out_len, |
878 | | size_t max_out, |
879 | | Span<const uint8_t> in); |
880 | | |
881 | | // ssl_pkey_supports_algorithm returns whether |pkey| may be used to sign |
882 | | // |sigalg|. |
883 | | bool ssl_pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey, |
884 | | uint16_t sigalg, bool is_verify); |
885 | | |
886 | | // ssl_public_key_verify verifies that the |signature| is valid for the public |
887 | | // key |pkey| and input |in|, using the signature algorithm |sigalg|. |
888 | | bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature, |
889 | | uint16_t sigalg, EVP_PKEY *pkey, |
890 | | Span<const uint8_t> in); |
891 | | |
892 | | |
893 | | // Key shares. |
894 | | |
895 | | // SSLKeyShare abstracts over KEM-like constructions, for use with TLS 1.2 ECDHE |
896 | | // cipher suites and the TLS 1.3 key_share extension. |
897 | | // |
898 | | // TODO(davidben): This class is named SSLKeyShare after the TLS 1.3 key_share |
899 | | // extension, but it really implements a KEM abstraction. Additionally, we use |
900 | | // the same type for Encap, which is a one-off, stateless operation, as Generate |
901 | | // and Decap. Slightly tidier would be for Generate to return a new SSLKEMKey |
902 | | // (or we introduce EVP_KEM and EVP_KEM_KEY), with a Decap method, and for Encap |
903 | | // to be static function. |
904 | | class SSLKeyShare { |
905 | | public: |
906 | 197k | virtual ~SSLKeyShare() {} |
907 | | static constexpr bool kAllowUniquePtr = true; |
908 | | |
909 | | // Create returns a SSLKeyShare instance for use with group |group_id| or |
910 | | // nullptr on error. |
911 | | static UniquePtr<SSLKeyShare> Create(uint16_t group_id); |
912 | | |
913 | | // GroupID returns the group ID. |
914 | | virtual uint16_t GroupID() const = 0; |
915 | | |
916 | | // Generate generates a keypair and writes the public key to |out_public_key|. |
917 | | // It returns true on success and false on error. |
918 | | virtual bool Generate(CBB *out_public_key) = 0; |
919 | | |
920 | | // Encap generates an ephemeral, symmetric secret and encapsulates it with |
921 | | // |peer_key|. On success, it returns true, writes the encapsulated secret to |
922 | | // |out_ciphertext|, and sets |*out_secret| to the shared secret. On failure, |
923 | | // it returns false and sets |*out_alert| to an alert to send to the peer. |
924 | | virtual bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret, |
925 | | uint8_t *out_alert, Span<const uint8_t> peer_key) = 0; |
926 | | |
927 | | // Decap decapsulates the symmetric secret in |ciphertext|. On success, it |
928 | | // returns true and sets |*out_secret| to the shared secret. On failure, it |
929 | | // returns false and sets |*out_alert| to an alert to send to the peer. |
930 | | virtual bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert, |
931 | | Span<const uint8_t> ciphertext) = 0; |
932 | | |
933 | | // SerializePrivateKey writes the private key to |out|, returning true if |
934 | | // successful and false otherwise. It should be called after |Generate|. |
935 | 0 | virtual bool SerializePrivateKey(CBB *out) { return false; } |
936 | | |
937 | | // DeserializePrivateKey initializes the state of the key exchange from |in|, |
938 | | // returning true if successful and false otherwise. |
939 | 0 | virtual bool DeserializePrivateKey(CBS *in) { return false; } |
940 | | }; |
941 | | |
942 | | struct NamedGroup { |
943 | | int nid; |
944 | | uint16_t group_id; |
945 | | const char name[32], alias[32]; |
946 | | }; |
947 | | |
948 | | // NamedGroups returns all supported groups. |
949 | | Span<const NamedGroup> NamedGroups(); |
950 | | |
951 | | // ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it |
952 | | // sets |*out_group_id| to the group ID and returns true. Otherwise, it returns |
953 | | // false. |
954 | | bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid); |
955 | | |
956 | | // ssl_name_to_group_id looks up the group corresponding to the |name| string of |
957 | | // length |len|. On success, it sets |*out_group_id| to the group ID and returns |
958 | | // true. Otherwise, it returns false. |
959 | | bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len); |
960 | | |
961 | | // ssl_group_id_to_nid returns the NID corresponding to |group_id| or |
962 | | // |NID_undef| if unknown. |
963 | | int ssl_group_id_to_nid(uint16_t group_id); |
964 | | |
965 | | |
966 | | // Handshake messages. |
967 | | |
968 | | struct SSLMessage { |
969 | | bool is_v2_hello; |
970 | | uint8_t type; |
971 | | CBS body; |
972 | | // raw is the entire serialized handshake message, including the TLS or DTLS |
973 | | // message header. |
974 | | CBS raw; |
975 | | }; |
976 | | |
977 | | // SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including |
978 | | // ChangeCipherSpec, in the longest handshake flight. Currently this is the |
979 | | // client's second leg in a full handshake when client certificates, NPN, and |
980 | | // Channel ID, are all enabled. |
981 | 1.08M | #define SSL_MAX_HANDSHAKE_FLIGHT 7 |
982 | | |
983 | | extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE]; |
984 | | extern const uint8_t kTLS12DowngradeRandom[8]; |
985 | | extern const uint8_t kTLS13DowngradeRandom[8]; |
986 | | extern const uint8_t kJDK11DowngradeRandom[8]; |
987 | | |
988 | | // ssl_max_handshake_message_len returns the maximum number of bytes permitted |
989 | | // in a handshake message for |ssl|. |
990 | | size_t ssl_max_handshake_message_len(const SSL *ssl); |
991 | | |
992 | | // tls_can_accept_handshake_data returns whether |ssl| is able to accept more |
993 | | // data into handshake buffer. |
994 | | bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert); |
995 | | |
996 | | // tls_has_unprocessed_handshake_data returns whether there is buffered |
997 | | // handshake data that has not been consumed by |get_message|. |
998 | | bool tls_has_unprocessed_handshake_data(const SSL *ssl); |
999 | | |
1000 | | // tls_append_handshake_data appends |data| to the handshake buffer. It returns |
1001 | | // true on success and false on allocation failure. |
1002 | | bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data); |
1003 | | |
1004 | | // dtls_has_unprocessed_handshake_data behaves like |
1005 | | // |tls_has_unprocessed_handshake_data| for DTLS. |
1006 | | bool dtls_has_unprocessed_handshake_data(const SSL *ssl); |
1007 | | |
1008 | | // tls_flush_pending_hs_data flushes any handshake plaintext data. |
1009 | | bool tls_flush_pending_hs_data(SSL *ssl); |
1010 | | |
1011 | | // dtls_clear_outgoing_messages releases all buffered outgoing messages. |
1012 | | void dtls_clear_outgoing_messages(SSL *ssl); |
1013 | | |
1014 | | // dtls_clear_unused_write_epochs releases any write epochs that are no longer |
1015 | | // needed. |
1016 | | void dtls_clear_unused_write_epochs(SSL *ssl); |
1017 | | |
1018 | | |
1019 | | // Callbacks. |
1020 | | |
1021 | | // ssl_do_info_callback calls |ssl|'s info callback, if set. |
1022 | | void ssl_do_info_callback(const SSL *ssl, int type, int value); |
1023 | | |
1024 | | // ssl_do_msg_callback calls |ssl|'s message callback, if set. |
1025 | | void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type, |
1026 | | Span<const uint8_t> in); |
1027 | | |
1028 | | |
1029 | | // Transport buffers. |
1030 | | |
1031 | | class SSLBuffer { |
1032 | | public: |
1033 | 236k | SSLBuffer() {} |
1034 | 236k | ~SSLBuffer() { Clear(); } |
1035 | | |
1036 | | SSLBuffer(const SSLBuffer &) = delete; |
1037 | | SSLBuffer &operator=(const SSLBuffer &) = delete; |
1038 | | |
1039 | 3.67M | uint8_t *data() { return buf_ + offset_; } |
1040 | 8.75M | size_t size() const { return size_; } |
1041 | 392k | bool empty() const { return size_ == 0; } |
1042 | 2.86M | size_t cap() const { return cap_; } |
1043 | | |
1044 | 2.21M | Span<uint8_t> span() { return Span(data(), size()); } |
1045 | | |
1046 | 31.3k | Span<uint8_t> remaining() { return Span(data() + size(), cap() - size()); } |
1047 | | |
1048 | | // Clear releases the buffer. |
1049 | | void Clear(); |
1050 | | |
1051 | | // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such |
1052 | | // that data written after |header_len| is aligned to a |
1053 | | // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false |
1054 | | // on error. |
1055 | | bool EnsureCap(size_t header_len, size_t new_cap); |
1056 | | |
1057 | | // DidWrite extends the buffer by |len|. The caller must have filled in to |
1058 | | // this point. |
1059 | | void DidWrite(size_t len); |
1060 | | |
1061 | | // Consume consumes |len| bytes from the front of the buffer. The memory |
1062 | | // consumed will remain valid until the next call to |DiscardConsumed| or |
1063 | | // |Clear|. |
1064 | | void Consume(size_t len); |
1065 | | |
1066 | | // DiscardConsumed discards the consumed bytes from the buffer. If the buffer |
1067 | | // is now empty, it releases memory used by it. |
1068 | | void DiscardConsumed(); |
1069 | | |
1070 | | private: |
1071 | | // buf_ is the memory allocated for this buffer. |
1072 | | uint8_t *buf_ = nullptr; |
1073 | | // offset_ is the offset into |buf_| which the buffer contents start at. |
1074 | | uint16_t offset_ = 0; |
1075 | | // size_ is the size of the buffer contents from |buf_| + |offset_|. |
1076 | | uint16_t size_ = 0; |
1077 | | // cap_ is how much memory beyond |buf_| + |offset_| is available. |
1078 | | uint16_t cap_ = 0; |
1079 | | // inline_buf_ is a static buffer for short reads. |
1080 | | uint8_t inline_buf_[SSL3_RT_HEADER_LENGTH]; |
1081 | | }; |
1082 | | |
1083 | | // ssl_read_buffer_extend_to extends the read buffer to the desired length. For |
1084 | | // TLS, it reads to the end of the buffer until the buffer is |len| bytes |
1085 | | // long. For DTLS, it reads a new packet and ignores |len|. It returns one on |
1086 | | // success, zero on EOF, and a negative number on error. |
1087 | | // |
1088 | | // It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is |
1089 | | // non-empty. |
1090 | | int ssl_read_buffer_extend_to(SSL *ssl, size_t len); |
1091 | | |
1092 | | // ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer| |
1093 | | // to a record-processing function. If |ret| is a success or if the caller |
1094 | | // should retry, it returns one and sets |*out_retry|. Otherwise, it returns <= |
1095 | | // 0. |
1096 | | int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret, |
1097 | | size_t consumed, uint8_t alert); |
1098 | | |
1099 | | // ssl_write_buffer_flush flushes the write buffer to the transport. It returns |
1100 | | // one on success and <= 0 on error. For DTLS, whether or not the write |
1101 | | // succeeds, the write buffer will be cleared. |
1102 | | int ssl_write_buffer_flush(SSL *ssl); |
1103 | | |
1104 | | |
1105 | | // Certificate functions. |
1106 | | |
1107 | | // ssl_parse_cert_chain parses a certificate list from |cbs| in the format used |
1108 | | // by a TLS Certificate message. On success, it advances |cbs| and returns |
1109 | | // true. Otherwise, it returns false and sets |*out_alert| to an alert to send |
1110 | | // to the peer. |
1111 | | // |
1112 | | // If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to |
1113 | | // the certificate chain and the leaf certificate's public key |
1114 | | // respectively. Otherwise, both will be set to nullptr. |
1115 | | // |
1116 | | // If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the |
1117 | | // SHA-256 hash of the leaf to |out_leaf_sha256|. |
1118 | | bool ssl_parse_cert_chain(uint8_t *out_alert, |
1119 | | UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain, |
1120 | | UniquePtr<EVP_PKEY> *out_pubkey, |
1121 | | uint8_t *out_leaf_sha256, CBS *cbs, |
1122 | | CRYPTO_BUFFER_POOL *pool); |
1123 | | |
1124 | | enum ssl_key_usage_t { |
1125 | | key_usage_digital_signature = 0, |
1126 | | key_usage_encipherment = 2, |
1127 | | }; |
1128 | | |
1129 | | // ssl_cert_check_key_usage parses the DER-encoded, X.509 certificate in |in| |
1130 | | // and returns true if doesn't specify a key usage or, if it does, if it |
1131 | | // includes |bit|. Otherwise it pushes to the error queue and returns false. |
1132 | | OPENSSL_EXPORT bool ssl_cert_check_key_usage(const CBS *in, |
1133 | | enum ssl_key_usage_t bit); |
1134 | | |
1135 | | // ssl_cert_extract_issuer parses the DER-encoded, X.509 certificate in |in| |
1136 | | // and extracts the issuer. On success it returns true and the DER encoded |
1137 | | // issuer is in |out_dn|, otherwise it returns false. |
1138 | | OPENSSL_EXPORT bool ssl_cert_extract_issuer(const CBS *in, CBS *out_dn); |
1139 | | |
1140 | | // ssl_cert_matches_issuer parses the DER-encoded, X.509 certificate in |in| |
1141 | | // and returns true if its issuer is an exact match for the DER encoded |
1142 | | // distinguished name in |dn| |
1143 | | bool ssl_cert_matches_issuer(const CBS *in, const CBS *dn); |
1144 | | |
1145 | | // ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509 |
1146 | | // certificate in |in|. It returns an allocated |EVP_PKEY| or else returns |
1147 | | // nullptr and pushes to the error queue. |
1148 | | UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in); |
1149 | | |
1150 | | // SSL_parse_CA_list parses a CA list from |cbs| in the format used by a TLS |
1151 | | // CertificateRequest message and Certificate Authorities extension. On success, |
1152 | | // it returns a newly-allocated |CRYPTO_BUFFER| list and advances |
1153 | | // |cbs|. Otherwise, it returns nullptr and sets |*out_alert| to an alert to |
1154 | | // send to the peer. |
1155 | | UniquePtr<STACK_OF(CRYPTO_BUFFER)> SSL_parse_CA_list(SSL *ssl, |
1156 | | uint8_t *out_alert, |
1157 | | CBS *cbs); |
1158 | | |
1159 | | // ssl_has_client_CAs returns whether there are configured CAs. |
1160 | | bool ssl_has_client_CAs(const SSL_CONFIG *cfg); |
1161 | | |
1162 | | // ssl_add_client_CA_list adds the configured CA list to |cbb| in the format |
1163 | | // used by a TLS CertificateRequest message. It returns true on success and |
1164 | | // false on error. |
1165 | | bool ssl_add_client_CA_list(const SSL_HANDSHAKE *hs, CBB *cbb); |
1166 | | |
1167 | | // ssl_has_CA_names returns whether there are configured CA names. |
1168 | | bool ssl_has_CA_names(const SSL_CONFIG *cfg); |
1169 | | |
1170 | | // ssl_add_CA_names adds the configured CA_names list to |cbb| in the format |
1171 | | // used by a TLS Certificate Authorities extension. It returns true on success |
1172 | | // and false on error. |
1173 | | bool ssl_add_CA_names(const SSL_HANDSHAKE *hs, CBB *cbb); |
1174 | | |
1175 | | // ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as |
1176 | | // a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes |
1177 | | // an error on the error queue. |
1178 | | bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey, |
1179 | | const CRYPTO_BUFFER *leaf); |
1180 | | |
1181 | | |
1182 | | // TLS 1.3 key derivation. |
1183 | | |
1184 | | // tls13_init_key_schedule initializes the handshake hash and key derivation |
1185 | | // state, and incorporates the PSK. The cipher suite and PRF hash must have been |
1186 | | // selected at this point. It returns true on success and false on error. |
1187 | | bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk); |
1188 | | |
1189 | | // tls13_init_early_key_schedule initializes the handshake hash and key |
1190 | | // derivation state from |session| for use with 0-RTT. It returns one on success |
1191 | | // and zero on error. |
1192 | | bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, |
1193 | | const SSL_SESSION *session); |
1194 | | |
1195 | | // tls13_advance_key_schedule incorporates |in| into the key schedule with |
1196 | | // HKDF-Extract. It returns true on success and false on error. |
1197 | | bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in); |
1198 | | |
1199 | | // tls13_set_traffic_key sets the read or write traffic keys to |
1200 | | // |traffic_secret|. The version and cipher suite are determined from |session|. |
1201 | | // It returns true on success and false on error. |
1202 | | bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level, |
1203 | | enum evp_aead_direction_t direction, |
1204 | | const SSL_SESSION *session, |
1205 | | Span<const uint8_t> traffic_secret); |
1206 | | |
1207 | | // tls13_derive_early_secret derives the early traffic secret. It returns true |
1208 | | // on success and false on error. |
1209 | | bool tls13_derive_early_secret(SSL_HANDSHAKE *hs); |
1210 | | |
1211 | | // tls13_derive_handshake_secrets derives the handshake traffic secret. It |
1212 | | // returns true on success and false on error. |
1213 | | bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs); |
1214 | | |
1215 | | // tls13_rotate_traffic_key derives the next read or write traffic secret. It |
1216 | | // returns true on success and false on error. |
1217 | | bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction); |
1218 | | |
1219 | | // tls13_derive_application_secrets derives the initial application data traffic |
1220 | | // and exporter secrets based on the handshake transcripts and |master_secret|. |
1221 | | // It returns true on success and false on error. |
1222 | | bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs); |
1223 | | |
1224 | | // tls13_derive_resumption_secret derives the |resumption_secret|. |
1225 | | bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs); |
1226 | | |
1227 | | // tls13_export_keying_material provides an exporter interface to use the |
1228 | | // |exporter_secret|. |
1229 | | bool tls13_export_keying_material(const SSL *ssl, Span<uint8_t> out, |
1230 | | Span<const uint8_t> secret, |
1231 | | std::string_view label, |
1232 | | Span<const uint8_t> context); |
1233 | | |
1234 | | // tls13_finished_mac calculates the MAC of the handshake transcript to verify |
1235 | | // the integrity of the Finished message, and stores the result in |out| and |
1236 | | // length in |out_len|. |is_server| is true if this is for the Server Finished |
1237 | | // and false for the Client Finished. |
1238 | | bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, |
1239 | | bool is_server); |
1240 | | |
1241 | | // tls13_derive_session_psk calculates the PSK for this session based on the |
1242 | | // resumption master secret and |nonce|. It returns true on success, and false |
1243 | | // on failure. |
1244 | | bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce, |
1245 | | bool is_dtls); |
1246 | | |
1247 | | // tls13_write_psk_binder calculates the PSK binder value over |transcript| and |
1248 | | // |msg|, and replaces the last bytes of |msg| with the resulting value. It |
1249 | | // returns true on success, and false on failure. If |out_binder_len| is |
1250 | | // non-NULL, it sets |*out_binder_len| to the length of the value computed. |
1251 | | bool tls13_write_psk_binder(const SSL_HANDSHAKE *hs, |
1252 | | const SSLTranscript &transcript, Span<uint8_t> msg, |
1253 | | size_t *out_binder_len); |
1254 | | |
1255 | | // tls13_verify_psk_binder verifies that the handshake transcript, truncated up |
1256 | | // to the binders has a valid signature using the value of |session|'s |
1257 | | // resumption secret. It returns true on success, and false on failure. |
1258 | | bool tls13_verify_psk_binder(const SSL_HANDSHAKE *hs, |
1259 | | const SSL_SESSION *session, const SSLMessage &msg, |
1260 | | CBS *binders); |
1261 | | |
1262 | | |
1263 | | // Encrypted ClientHello. |
1264 | | |
1265 | | struct ECHConfig { |
1266 | | static constexpr bool kAllowUniquePtr = true; |
1267 | | // raw contains the serialized ECHConfig. |
1268 | | Array<uint8_t> raw; |
1269 | | // The following fields alias into |raw|. |
1270 | | Span<const uint8_t> public_key; |
1271 | | Span<const uint8_t> public_name; |
1272 | | Span<const uint8_t> cipher_suites; |
1273 | | uint16_t kem_id = 0; |
1274 | | uint8_t maximum_name_length = 0; |
1275 | | uint8_t config_id = 0; |
1276 | | }; |
1277 | | |
1278 | | class ECHServerConfig { |
1279 | | public: |
1280 | | static constexpr bool kAllowUniquePtr = true; |
1281 | 11.2k | ECHServerConfig() = default; |
1282 | | ECHServerConfig(const ECHServerConfig &other) = delete; |
1283 | | ECHServerConfig &operator=(ECHServerConfig &&) = delete; |
1284 | | |
1285 | | // Init parses |ech_config| as an ECHConfig and saves a copy of |key|. |
1286 | | // It returns true on success and false on error. |
1287 | | bool Init(Span<const uint8_t> ech_config, const EVP_HPKE_KEY *key, |
1288 | | bool is_retry_config); |
1289 | | |
1290 | | // SetupContext sets up |ctx| for a new connection, given the specified |
1291 | | // HPKE ciphersuite and encapsulated KEM key. It returns true on success and |
1292 | | // false on error. This function may only be called on an initialized object. |
1293 | | bool SetupContext(EVP_HPKE_CTX *ctx, uint16_t kdf_id, uint16_t aead_id, |
1294 | | Span<const uint8_t> enc) const; |
1295 | | |
1296 | 497 | const ECHConfig &ech_config() const { return ech_config_; } |
1297 | 27 | bool is_retry_config() const { return is_retry_config_; } |
1298 | | |
1299 | | private: |
1300 | | ECHConfig ech_config_; |
1301 | | ScopedEVP_HPKE_KEY key_; |
1302 | | bool is_retry_config_ = false; |
1303 | | }; |
1304 | | |
1305 | | enum ssl_client_hello_type_t { |
1306 | | ssl_client_hello_unencrypted, |
1307 | | ssl_client_hello_inner, |
1308 | | ssl_client_hello_outer, |
1309 | | }; |
1310 | | |
1311 | | // ECH_CLIENT_* are types for the ClientHello encrypted_client_hello extension. |
1312 | 1.22k | #define ECH_CLIENT_OUTER 0 |
1313 | 1.31k | #define ECH_CLIENT_INNER 1 |
1314 | | |
1315 | | // ssl_decode_client_hello_inner recovers the full ClientHelloInner from the |
1316 | | // EncodedClientHelloInner |encoded_client_hello_inner| by replacing its |
1317 | | // outer_extensions extension with the referenced extensions from the |
1318 | | // ClientHelloOuter |client_hello_outer|. If successful, it writes the recovered |
1319 | | // ClientHelloInner to |out_client_hello_inner|. It returns true on success and |
1320 | | // false on failure. |
1321 | | // |
1322 | | // This function is exported for fuzzing. |
1323 | | OPENSSL_EXPORT bool ssl_decode_client_hello_inner( |
1324 | | SSL *ssl, uint8_t *out_alert, Array<uint8_t> *out_client_hello_inner, |
1325 | | Span<const uint8_t> encoded_client_hello_inner, |
1326 | | const SSL_CLIENT_HELLO *client_hello_outer); |
1327 | | |
1328 | | // ssl_client_hello_decrypt attempts to decrypt and decode the |payload|. It |
1329 | | // writes the result to |*out|. |payload| must point into |client_hello_outer|. |
1330 | | // It returns true on success and false on error. On error, it sets |
1331 | | // |*out_is_decrypt_error| to whether the failure was due to a bad ciphertext. |
1332 | | bool ssl_client_hello_decrypt(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
1333 | | bool *out_is_decrypt_error, Array<uint8_t> *out, |
1334 | | const SSL_CLIENT_HELLO *client_hello_outer, |
1335 | | Span<const uint8_t> payload); |
1336 | | |
1337 | 3.55k | #define ECH_CONFIRMATION_SIGNAL_LEN 8 |
1338 | | |
1339 | | // ssl_ech_confirmation_signal_hello_offset returns the offset of the ECH |
1340 | | // confirmation signal in a ServerHello message, including the handshake header. |
1341 | | size_t ssl_ech_confirmation_signal_hello_offset(const SSL *ssl); |
1342 | | |
1343 | | // ssl_ech_accept_confirmation computes the server's ECH acceptance signal, |
1344 | | // writing it to |out|. The transcript portion is the concatenation of |
1345 | | // |transcript| with |msg|. The |ECH_CONFIRMATION_SIGNAL_LEN| bytes from |
1346 | | // |offset| in |msg| are replaced with zeros before hashing. This function |
1347 | | // returns true on success, and false on failure. |
1348 | | bool ssl_ech_accept_confirmation(const SSL_HANDSHAKE *hs, Span<uint8_t> out, |
1349 | | Span<const uint8_t> client_random, |
1350 | | const SSLTranscript &transcript, bool is_hrr, |
1351 | | Span<const uint8_t> msg, size_t offset); |
1352 | | |
1353 | | // ssl_is_valid_ech_public_name returns true if |public_name| is a valid ECH |
1354 | | // public name and false otherwise. It is exported for testing. |
1355 | | OPENSSL_EXPORT bool ssl_is_valid_ech_public_name( |
1356 | | Span<const uint8_t> public_name); |
1357 | | |
1358 | | // ssl_is_valid_ech_config_list returns true if |ech_config_list| is a valid |
1359 | | // ECHConfigList structure and false otherwise. |
1360 | | bool ssl_is_valid_ech_config_list(Span<const uint8_t> ech_config_list); |
1361 | | |
1362 | | // ssl_select_ech_config selects an ECHConfig and associated parameters to offer |
1363 | | // on the client and updates |hs|. It returns true on success, whether an |
1364 | | // ECHConfig was found or not, and false on internal error. On success, the |
1365 | | // encapsulated key is written to |out_enc| and |*out_enc_len| is set to the |
1366 | | // number of bytes written. If the function did not select an ECHConfig, the |
1367 | | // encapsulated key is the empty string. |
1368 | | bool ssl_select_ech_config(SSL_HANDSHAKE *hs, Span<uint8_t> out_enc, |
1369 | | size_t *out_enc_len); |
1370 | | |
1371 | | // ssl_ech_extension_body_length returns the length of the body of a ClientHello |
1372 | | // ECH extension that encrypts |in_len| bytes with |aead| and an 'enc' value of |
1373 | | // length |enc_len|. The result does not include the four-byte extension header. |
1374 | | size_t ssl_ech_extension_body_length(const EVP_HPKE_AEAD *aead, size_t enc_len, |
1375 | | size_t in_len); |
1376 | | |
1377 | | // ssl_encrypt_client_hello constructs a new ClientHelloInner, adds it to the |
1378 | | // inner transcript, and encrypts for inclusion in the ClientHelloOuter. |enc| |
1379 | | // is the encapsulated key to include in the extension. It returns true on |
1380 | | // success and false on error. If not offering ECH, |enc| is ignored and the |
1381 | | // function will compute a GREASE ECH extension if necessary, and otherwise |
1382 | | // return success while doing nothing. |
1383 | | // |
1384 | | // Encrypting the ClientHelloInner incorporates all extensions in the |
1385 | | // ClientHelloOuter, so all other state necessary for |ssl_add_client_hello| |
1386 | | // must already be computed. |
1387 | | bool ssl_encrypt_client_hello(SSL_HANDSHAKE *hs, Span<const uint8_t> enc); |
1388 | | |
1389 | | |
1390 | | // Credentials. |
1391 | | |
1392 | | enum class SSLCredentialType { |
1393 | | kX509, |
1394 | | kDelegated, |
1395 | | kSPAKE2PlusV1Client, |
1396 | | kSPAKE2PlusV1Server, |
1397 | | }; |
1398 | | |
1399 | | BSSL_NAMESPACE_END |
1400 | | |
1401 | | // SSL_CREDENTIAL is exported to C, so it must be defined outside the namespace. |
1402 | | struct ssl_credential_st : public bssl::RefCounted<ssl_credential_st> { |
1403 | | explicit ssl_credential_st(bssl::SSLCredentialType type); |
1404 | | ssl_credential_st(const ssl_credential_st &) = delete; |
1405 | | ssl_credential_st &operator=(const ssl_credential_st &) = delete; |
1406 | | |
1407 | | // Dup returns a copy of the credential, or nullptr on error. The |ex_data| |
1408 | | // values are not copied. This is only used on the legacy credential, whose |
1409 | | // |ex_data| is inaccessible. |
1410 | | bssl::UniquePtr<SSL_CREDENTIAL> Dup() const; |
1411 | | |
1412 | | // ClearCertAndKey erases any certificate and private key on the credential. |
1413 | | void ClearCertAndKey(); |
1414 | | |
1415 | | // UsesX509 returns true if the credential type uses an X.509 certificate. |
1416 | | bool UsesX509() const; |
1417 | | |
1418 | | // UsesPrivateKey returns true if the credential type uses an asymmetric |
1419 | | // private key. |
1420 | | bool UsesPrivateKey() const; |
1421 | | |
1422 | | // IsComplete returns whether all required fields in the credential have been |
1423 | | // filled in. |
1424 | | bool IsComplete() const; |
1425 | | |
1426 | | // SetLeafCert sets the leaf certificate to |leaf|, leaving the remaining |
1427 | | // certificates unmodified. It returns true on success and false on error. If |
1428 | | // |discard_key_on_mismatch| is true and the private key is inconsistent with |
1429 | | // the new leaf certificate, it is silently discarded. |
1430 | | bool SetLeafCert(bssl::UniquePtr<CRYPTO_BUFFER> leaf, |
1431 | | bool discard_key_on_mismatch); |
1432 | | |
1433 | | // ClearIntermediateCerts clears intermediate certificates in the certificate |
1434 | | // chain, while preserving the leaf. |
1435 | | void ClearIntermediateCerts(); |
1436 | | |
1437 | | // AppendIntermediateCert appends |cert| to the certificate chain. If there is |
1438 | | // no leaf certificate configured, it leaves a placeholder null in |chain|. It |
1439 | | // returns one on success and zero on error. |
1440 | | bool AppendIntermediateCert(bssl::UniquePtr<CRYPTO_BUFFER> cert); |
1441 | | |
1442 | | // ChainContainsIssuer returns true if |dn| is a byte for byte match with the |
1443 | | // issuer of any certificate in |chain|, false otherwise. |
1444 | | bool ChainContainsIssuer(bssl::Span<const uint8_t> dn) const; |
1445 | | |
1446 | | // type is the credential type and determines which other fields apply. |
1447 | | bssl::SSLCredentialType type; |
1448 | | |
1449 | | // pubkey is the cached public key of the credential. Unlike |privkey|, it is |
1450 | | // always present and is extracted from the certificate, delegated credential, |
1451 | | // etc. |
1452 | | bssl::UniquePtr<EVP_PKEY> pubkey; |
1453 | | |
1454 | | // privkey is the private key of the credential. It may be omitted in favor of |
1455 | | // |key_method|. |
1456 | | bssl::UniquePtr<EVP_PKEY> privkey; |
1457 | | |
1458 | | // key_method, if non-null, is a set of callbacks to call for private key |
1459 | | // operations. |
1460 | | const SSL_PRIVATE_KEY_METHOD *key_method = nullptr; |
1461 | | |
1462 | | // sigalgs, if non-empty, is the set of signature algorithms supported by the |
1463 | | // private key in decreasing order of preference. If empty, the default list |
1464 | | // is used. |
1465 | | // |
1466 | | // In delegated credentials, this field is not configurable and is instead |
1467 | | // computed from the dc_cert_verify_algorithm field. |
1468 | | bssl::Array<uint16_t> sigalgs; |
1469 | | |
1470 | | // chain contains the certificate chain, with the leaf at the beginning. The |
1471 | | // first element of |chain| may be nullptr to indicate that the leaf |
1472 | | // certificate has not yet been set. |
1473 | | // If |chain| != nullptr -> len(chain) >= 1 |
1474 | | // If |chain[0]| == nullptr -> len(chain) >= 2. |
1475 | | // |chain[1..]| != nullptr |
1476 | | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain; |
1477 | | |
1478 | | // dc is the DelegatedCredential structure, if this is a delegated credential. |
1479 | | bssl::UniquePtr<CRYPTO_BUFFER> dc; |
1480 | | |
1481 | | // dc_algorithm is the signature scheme of the signature over the delegated |
1482 | | // credential itself, made by the end-entity certificate's public key. |
1483 | | uint16_t dc_algorithm = 0; |
1484 | | |
1485 | | // Signed certificate timestamp list to be sent to the client, if requested |
1486 | | bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list; |
1487 | | |
1488 | | // OCSP response to be sent to the client, if requested. |
1489 | | bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response; |
1490 | | |
1491 | | // SPAKE2+-specific information. |
1492 | | bssl::Array<uint8_t> pake_context; |
1493 | | bssl::Array<uint8_t> client_identity; |
1494 | | bssl::Array<uint8_t> server_identity; |
1495 | | bssl::Array<uint8_t> password_verifier_w0; |
1496 | | bssl::Array<uint8_t> password_verifier_w1; // server-only |
1497 | | bssl::Array<uint8_t> registration_record; // client-only |
1498 | | mutable std::atomic<uint32_t> pake_limit; |
1499 | | |
1500 | | // Checks whether there are still permitted PAKE attempts remaining, without |
1501 | | // changing the counter. |
1502 | | bool HasPAKEAttempts() const; |
1503 | | |
1504 | | // Atomically decrement |pake_limit|. Return true if successful and false if |
1505 | | // |pake_limit| is already zero. |
1506 | | bool ClaimPAKEAttempt() const; |
1507 | | |
1508 | | // Atomically increment |pake_limit|. This must be paired with a |
1509 | | // |ClaimPAKEAttempt| call. |
1510 | | void RestorePAKEAttempt() const; |
1511 | | |
1512 | | // trust_anchor_id, if non-empty, is the trust anchor ID for the root of the |
1513 | | // chain in |chain|. |
1514 | | bssl::Array<uint8_t> trust_anchor_id; |
1515 | | |
1516 | | CRYPTO_EX_DATA ex_data; |
1517 | | |
1518 | | // must_match_issuer is a flag indicating that this credential should be |
1519 | | // considered only when it matches a peer request for a particular issuer via |
1520 | | // a negotiation mechanism (such as the certificate_authorities extension). |
1521 | | // This also implies that chain is a certificate path ending in a certificate |
1522 | | // issued by the certificate with that trust anchor identifier. |
1523 | | bool must_match_issuer = false; |
1524 | | |
1525 | | private: |
1526 | | friend RefCounted; |
1527 | | ~ssl_credential_st(); |
1528 | | }; |
1529 | | |
1530 | | BSSL_NAMESPACE_BEGIN |
1531 | | |
1532 | | // ssl_get_full_credential_list computes |hs|'s full credential list, including |
1533 | | // the legacy credential. On success, it writes it to |*out| and returns true. |
1534 | | // Otherwise, it returns false. The credential list may be empty, in which case |
1535 | | // this function will successfully output an empty array. |
1536 | | // |
1537 | | // This function should be called at most once during the handshake and is |
1538 | | // intended to be used for certificate-based credentials. It runs the |
1539 | | // auto-chaining logic as part of finishing the legacy credential. Other uses of |
1540 | | // the credential list (e.g. PAKE credentials) should iterate over |
1541 | | // |hs->config->cert->credentials|. |
1542 | | // |
1543 | | // The pointers in the result are only valid until |hs| is next mutated. |
1544 | | bool ssl_get_full_credential_list(SSL_HANDSHAKE *hs, |
1545 | | Array<SSL_CREDENTIAL *> *out); |
1546 | | |
1547 | | // ssl_credential_matches_requested_issuers returns true if |cred| is a |
1548 | | // usable match for any requested issuers in |hs|, and false with an error |
1549 | | // otherwise. |
1550 | | bool ssl_credential_matches_requested_issuers(SSL_HANDSHAKE *hs, |
1551 | | const SSL_CREDENTIAL *cred); |
1552 | | |
1553 | | // ssl_check_tls13_credential_ignoring_issuer returns true if |cred| is usable |
1554 | | // as the certificate in a TLS 1.3 handshake, ignoring the issuer check. |
1555 | | // |out_sigalg| will be set to a matching signature algorithm if true is |
1556 | | // returned. |
1557 | | bool ssl_check_tls13_credential_ignoring_issuer(SSL_HANDSHAKE *hs, |
1558 | | const SSL_CREDENTIAL *cred, |
1559 | | uint16_t *out_sigalg); |
1560 | | |
1561 | | |
1562 | | // Handshake functions. |
1563 | | |
1564 | | enum ssl_hs_wait_t { |
1565 | | ssl_hs_error, |
1566 | | ssl_hs_ok, |
1567 | | ssl_hs_read_server_hello, |
1568 | | ssl_hs_read_message, |
1569 | | ssl_hs_flush, |
1570 | | ssl_hs_certificate_selection_pending, |
1571 | | ssl_hs_handoff, |
1572 | | ssl_hs_handback, |
1573 | | ssl_hs_x509_lookup, |
1574 | | ssl_hs_private_key_operation, |
1575 | | ssl_hs_pending_session, |
1576 | | ssl_hs_pending_ticket, |
1577 | | ssl_hs_early_return, |
1578 | | ssl_hs_early_data_rejected, |
1579 | | ssl_hs_read_end_of_early_data, |
1580 | | ssl_hs_read_change_cipher_spec, |
1581 | | ssl_hs_certificate_verify, |
1582 | | ssl_hs_hints_ready, |
1583 | | }; |
1584 | | |
1585 | | enum ssl_grease_index_t { |
1586 | | ssl_grease_cipher = 0, |
1587 | | ssl_grease_group, |
1588 | | ssl_grease_extension1, |
1589 | | ssl_grease_extension2, |
1590 | | ssl_grease_version, |
1591 | | ssl_grease_ticket_extension, |
1592 | | ssl_grease_ech_config_id, |
1593 | | ssl_grease_last_index = ssl_grease_ech_config_id, |
1594 | | }; |
1595 | | |
1596 | | enum tls12_server_hs_state_t { |
1597 | | state12_start_accept = 0, |
1598 | | state12_read_client_hello, |
1599 | | state12_read_client_hello_after_ech, |
1600 | | state12_cert_callback, |
1601 | | state12_tls13, |
1602 | | state12_select_parameters, |
1603 | | state12_send_server_hello, |
1604 | | state12_send_server_certificate, |
1605 | | state12_send_server_key_exchange, |
1606 | | state12_send_server_hello_done, |
1607 | | state12_read_client_certificate, |
1608 | | state12_verify_client_certificate, |
1609 | | state12_read_client_key_exchange, |
1610 | | state12_read_client_certificate_verify, |
1611 | | state12_read_change_cipher_spec, |
1612 | | state12_process_change_cipher_spec, |
1613 | | state12_read_next_proto, |
1614 | | state12_read_channel_id, |
1615 | | state12_read_client_finished, |
1616 | | state12_send_server_finished, |
1617 | | state12_finish_server_handshake, |
1618 | | state12_done, |
1619 | | }; |
1620 | | |
1621 | | enum tls13_server_hs_state_t { |
1622 | | state13_select_parameters = 0, |
1623 | | state13_select_session, |
1624 | | state13_send_hello_retry_request, |
1625 | | state13_read_second_client_hello, |
1626 | | state13_send_server_hello, |
1627 | | state13_send_server_certificate_verify, |
1628 | | state13_send_server_finished, |
1629 | | state13_send_half_rtt_ticket, |
1630 | | state13_read_second_client_flight, |
1631 | | state13_process_end_of_early_data, |
1632 | | state13_read_client_encrypted_extensions, |
1633 | | state13_read_client_certificate, |
1634 | | state13_read_client_certificate_verify, |
1635 | | state13_read_channel_id, |
1636 | | state13_read_client_finished, |
1637 | | state13_send_new_session_ticket, |
1638 | | state13_done, |
1639 | | }; |
1640 | | |
1641 | | // handback_t lists the points in the state machine where a handback can occur. |
1642 | | // These are the different points at which key material is no longer needed. |
1643 | | enum handback_t { |
1644 | | handback_after_session_resumption = 0, |
1645 | | handback_after_ecdhe = 1, |
1646 | | handback_after_handshake = 2, |
1647 | | handback_tls13 = 3, |
1648 | | handback_max_value = handback_tls13, |
1649 | | }; |
1650 | | |
1651 | | // SSL_HANDSHAKE_HINTS contains handshake hints for a connection. See |
1652 | | // |SSL_request_handshake_hints| and related functions. |
1653 | | struct SSL_HANDSHAKE_HINTS { |
1654 | | static constexpr bool kAllowUniquePtr = true; |
1655 | | |
1656 | | Array<uint8_t> server_random_tls12; |
1657 | | Array<uint8_t> server_random_tls13; |
1658 | | |
1659 | | uint16_t key_share_group_id = 0; |
1660 | | Array<uint8_t> key_share_ciphertext; |
1661 | | Array<uint8_t> key_share_secret; |
1662 | | |
1663 | | uint16_t signature_algorithm = 0; |
1664 | | Array<uint8_t> signature_input; |
1665 | | Array<uint8_t> signature_spki; |
1666 | | Array<uint8_t> signature; |
1667 | | |
1668 | | Array<uint8_t> decrypted_psk; |
1669 | | bool ignore_psk = false; |
1670 | | |
1671 | | uint16_t cert_compression_alg_id = 0; |
1672 | | Array<uint8_t> cert_compression_input; |
1673 | | Array<uint8_t> cert_compression_output; |
1674 | | |
1675 | | uint16_t ecdhe_group_id = 0; |
1676 | | Array<uint8_t> ecdhe_public_key; |
1677 | | Array<uint8_t> ecdhe_private_key; |
1678 | | |
1679 | | Array<uint8_t> decrypted_ticket; |
1680 | | bool renew_ticket = false; |
1681 | | bool ignore_ticket = false; |
1682 | | }; |
1683 | | |
1684 | | struct SSLPAKEShare { |
1685 | | static constexpr bool kAllowUniquePtr = true; |
1686 | | uint16_t named_pake; |
1687 | | Array<uint8_t> client_identity; |
1688 | | Array<uint8_t> server_identity; |
1689 | | Array<uint8_t> pake_message; |
1690 | | }; |
1691 | | |
1692 | | struct SSL_HANDSHAKE { |
1693 | | explicit SSL_HANDSHAKE(SSL *ssl); |
1694 | | ~SSL_HANDSHAKE(); |
1695 | | static constexpr bool kAllowUniquePtr = true; |
1696 | | |
1697 | | // ssl is a non-owning pointer to the parent |SSL| object. |
1698 | | SSL *ssl; |
1699 | | |
1700 | | // config is a non-owning pointer to the handshake configuration. |
1701 | | SSL_CONFIG *config; |
1702 | | |
1703 | | // wait contains the operation the handshake is currently blocking on or |
1704 | | // |ssl_hs_ok| if none. |
1705 | | enum ssl_hs_wait_t wait = ssl_hs_ok; |
1706 | | |
1707 | | // state is the internal state for the TLS 1.2 and below handshake. Its |
1708 | | // values depend on |do_handshake| but the starting state is always zero. |
1709 | | int state = 0; |
1710 | | |
1711 | | // tls13_state is the internal state for the TLS 1.3 handshake. Its values |
1712 | | // depend on |do_handshake| but the starting state is always zero. |
1713 | | int tls13_state = 0; |
1714 | | |
1715 | | // min_version is the minimum accepted protocol version, taking account both |
1716 | | // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs. |
1717 | | uint16_t min_version = 0; |
1718 | | |
1719 | | // max_version is the maximum accepted protocol version, taking account both |
1720 | | // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs. |
1721 | | uint16_t max_version = 0; |
1722 | | |
1723 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> secret; |
1724 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> early_traffic_secret; |
1725 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> client_handshake_secret; |
1726 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> server_handshake_secret; |
1727 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> client_traffic_secret_0; |
1728 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> server_traffic_secret_0; |
1729 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> expected_client_finished; |
1730 | | |
1731 | | // GetClientHello, on the server, returns either the normal ClientHello |
1732 | | // message or the ClientHelloInner if it has been serialized to |
1733 | | // |ech_client_hello_buf|. This function should only be called when the |
1734 | | // current message is a ClientHello. It returns true on success and false on |
1735 | | // error. |
1736 | | // |
1737 | | // Note that fields of the returned |out_msg| and |out_client_hello| point |
1738 | | // into a handshake-owned buffer, so their lifetimes should not exceed this |
1739 | | // SSL_HANDSHAKE. |
1740 | | bool GetClientHello(SSLMessage *out_msg, SSL_CLIENT_HELLO *out_client_hello); |
1741 | | |
1742 | | union { |
1743 | | // sent is a bitset where the bits correspond to elements of kExtensions |
1744 | | // in extensions.cc. Each bit is set if that extension was sent in a |
1745 | | // ClientHello. It's not used by servers. |
1746 | | uint32_t sent = 0; |
1747 | | // received is a bitset, like |sent|, but is used by servers to record |
1748 | | // which extensions were received from a client. |
1749 | | uint32_t received; |
1750 | | } extensions; |
1751 | | |
1752 | | // inner_extensions_sent, on clients that offer ECH, is |extensions.sent| for |
1753 | | // the ClientHelloInner. |
1754 | | uint32_t inner_extensions_sent = 0; |
1755 | | |
1756 | | // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on. |
1757 | | UniquePtr<ERR_SAVE_STATE> error; |
1758 | | |
1759 | | // key_shares are the current key exchange instances. The second is only used |
1760 | | // as a client if we believe that we should offer two key shares in a |
1761 | | // ClientHello. |
1762 | | UniquePtr<SSLKeyShare> key_shares[2]; |
1763 | | |
1764 | | // transcript is the current handshake transcript. |
1765 | | SSLTranscript transcript; |
1766 | | |
1767 | | // inner_transcript, on the client, is the handshake transcript for the |
1768 | | // ClientHelloInner handshake. It is moved to |transcript| if the server |
1769 | | // accepts ECH. |
1770 | | SSLTranscript inner_transcript; |
1771 | | |
1772 | | // inner_client_random is the ClientHello random value used with |
1773 | | // ClientHelloInner. |
1774 | | uint8_t inner_client_random[SSL3_RANDOM_SIZE] = {0}; |
1775 | | |
1776 | | // cookie is the value of the cookie in HelloRetryRequest, or empty if none |
1777 | | // was received. |
1778 | | Array<uint8_t> cookie; |
1779 | | |
1780 | | // dtls_cookie is the value of the cookie in DTLS HelloVerifyRequest. If |
1781 | | // empty, either none was received or HelloVerifyRequest contained an empty |
1782 | | // cookie. Check the received_hello_verify_request field to distinguish an |
1783 | | // empty cookie from no HelloVerifyRequest message being received. |
1784 | | Array<uint8_t> dtls_cookie; |
1785 | | |
1786 | | // ech_client_outer contains the outer ECH extension to send in the |
1787 | | // ClientHello, excluding the header and type byte. |
1788 | | Array<uint8_t> ech_client_outer; |
1789 | | |
1790 | | // ech_retry_configs, on the client, contains the retry configs from the |
1791 | | // server as a serialized ECHConfigList. |
1792 | | Array<uint8_t> ech_retry_configs; |
1793 | | |
1794 | | // ech_client_hello_buf, on the server, contains the bytes of the |
1795 | | // reconstructed ClientHelloInner message. |
1796 | | Array<uint8_t> ech_client_hello_buf; |
1797 | | |
1798 | | // key_share_bytes is the key_share extension that the client should send. |
1799 | | Array<uint8_t> key_share_bytes; |
1800 | | |
1801 | | // key_share_ciphertext, for servers, is encapsulated shared secret to be sent |
1802 | | // to the client in the TLS 1.3 key_share extension. |
1803 | | Array<uint8_t> key_share_ciphertext; |
1804 | | |
1805 | | // peer_sigalgs are the signature algorithms that the peer supports. These are |
1806 | | // taken from the contents of the signature algorithms extension for a server |
1807 | | // or from the CertificateRequest for a client. |
1808 | | Array<uint16_t> peer_sigalgs; |
1809 | | |
1810 | | // peer_supported_group_list contains the supported group IDs advertised by |
1811 | | // the peer. This is only set on the server's end. The server does not |
1812 | | // advertise this extension to the client. |
1813 | | Array<uint16_t> peer_supported_group_list; |
1814 | | |
1815 | | // peer_delegated_credential_sigalgs are the signature algorithms the peer |
1816 | | // supports with delegated credentials, or empty if the peer does not support |
1817 | | // delegated credentials. |
1818 | | Array<uint16_t> peer_delegated_credential_sigalgs; |
1819 | | |
1820 | | // peer_key is the peer's ECDH key for a TLS 1.2 client. |
1821 | | Array<uint8_t> peer_key; |
1822 | | |
1823 | | // extension_permutation is the permutation to apply to ClientHello |
1824 | | // extensions. It maps indices into the |kExtensions| table into other |
1825 | | // indices. |
1826 | | Array<uint8_t> extension_permutation; |
1827 | | |
1828 | | // cert_compression_alg_id, for a server, contains the negotiated certificate |
1829 | | // compression algorithm for this client. It is only valid if |
1830 | | // |cert_compression_negotiated| is true. |
1831 | | uint16_t cert_compression_alg_id; |
1832 | | |
1833 | | // ech_hpke_ctx is the HPKE context used in ECH. On the server, it is |
1834 | | // initialized if |ech_status| is |ssl_ech_accepted|. On the client, it is |
1835 | | // initialized if |selected_ech_config| is not nullptr. |
1836 | | ScopedEVP_HPKE_CTX ech_hpke_ctx; |
1837 | | |
1838 | | // server_params, in a TLS 1.2 server, stores the ServerKeyExchange |
1839 | | // parameters. It has client and server randoms prepended for signing |
1840 | | // convenience. |
1841 | | Array<uint8_t> server_params; |
1842 | | |
1843 | | // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the |
1844 | | // server when using a TLS 1.2 PSK key exchange. |
1845 | | UniquePtr<char> peer_psk_identity_hint; |
1846 | | |
1847 | | // ca_names contains the list of CAs received via the Certificate Authorities |
1848 | | // extension in our peer's CertificateRequest or ClientHello message |
1849 | | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names; |
1850 | | |
1851 | | // peer_requested_trust_anchors, if not nullopt, contains the trust anchor IDs |
1852 | | // (possibly none) the peer requested in ClientHello or CertificateRequest. If |
1853 | | // nullopt, the peer did not send the extension. |
1854 | | std::optional<Array<uint8_t>> peer_requested_trust_anchors; |
1855 | | |
1856 | | // peer_available_trust_anchors, if not empty, is the list of trust anchor IDs |
1857 | | // the peer reported as available in EncryptedExtensions. This is only sent by |
1858 | | // servers to clients. |
1859 | | Array<uint8_t> peer_available_trust_anchors; |
1860 | | |
1861 | | // cached_x509_ca_names contains a cache of parsed versions of the elements of |
1862 | | // |ca_names|. This pointer is left non-owning so only |
1863 | | // |ssl_crypto_x509_method| needs to link against crypto/x509. |
1864 | | STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr; |
1865 | | |
1866 | | // certificate_types, on the client, contains the set of certificate types |
1867 | | // received in a CertificateRequest message. |
1868 | | Array<uint8_t> certificate_types; |
1869 | | |
1870 | | // credential is the credential we are using for the handshake. |
1871 | | UniquePtr<SSL_CREDENTIAL> credential; |
1872 | | |
1873 | | // peer_pubkey is the public key parsed from the peer's leaf certificate. |
1874 | | UniquePtr<EVP_PKEY> peer_pubkey; |
1875 | | |
1876 | | // new_session is the new mutable session being established by the current |
1877 | | // handshake. It should not be cached. |
1878 | | UniquePtr<SSL_SESSION> new_session; |
1879 | | |
1880 | | // early_session is the session corresponding to the current 0-RTT state on |
1881 | | // the client if |in_early_data| is true. |
1882 | | UniquePtr<SSL_SESSION> early_session; |
1883 | | |
1884 | | // ssl_ech_keys, for servers, is the set of ECH keys to use with this |
1885 | | // handshake. This is copied from |SSL_CTX| to ensure consistent behavior as |
1886 | | // |SSL_CTX| rotates keys. |
1887 | | UniquePtr<SSL_ECH_KEYS> ech_keys; |
1888 | | |
1889 | | // selected_ech_config, for clients, is the ECHConfig the client uses to offer |
1890 | | // ECH, or nullptr if ECH is not being offered. If non-NULL, |ech_hpke_ctx| |
1891 | | // will be initialized. |
1892 | | UniquePtr<ECHConfig> selected_ech_config; |
1893 | | |
1894 | | // new_cipher is the cipher being negotiated in this handshake. |
1895 | | const SSL_CIPHER *new_cipher = nullptr; |
1896 | | |
1897 | | // key_block is the record-layer key block for TLS 1.2 and earlier. |
1898 | | Array<uint8_t> key_block; |
1899 | | |
1900 | | // hints contains the handshake hints for this connection. If |
1901 | | // |hints_requested| is true, this field is non-null and contains the pending |
1902 | | // hints to filled as the predicted handshake progresses. Otherwise, this |
1903 | | // field, if non-null, contains hints configured by the caller and will |
1904 | | // influence the handshake on match. |
1905 | | UniquePtr<SSL_HANDSHAKE_HINTS> hints; |
1906 | | |
1907 | | // ech_is_inner, on the server, indicates whether the ClientHello contained an |
1908 | | // inner ECH extension. |
1909 | | bool ech_is_inner : 1; |
1910 | | |
1911 | | // ech_authenticated_reject, on the client, indicates whether an ECH rejection |
1912 | | // handshake has been authenticated. |
1913 | | bool ech_authenticated_reject : 1; |
1914 | | |
1915 | | // scts_requested is true if the SCT extension is in the ClientHello. |
1916 | | bool scts_requested : 1; |
1917 | | |
1918 | | // handshake_finalized is true once the handshake has completed, at which |
1919 | | // point accessors should use the established state. |
1920 | | bool handshake_finalized : 1; |
1921 | | |
1922 | | // accept_psk_mode stores whether the client's PSK mode is compatible with our |
1923 | | // preferences. |
1924 | | bool accept_psk_mode : 1; |
1925 | | |
1926 | | // cert_request is true if a client certificate was requested. |
1927 | | bool cert_request : 1; |
1928 | | |
1929 | | // certificate_status_expected is true if OCSP stapling was negotiated and the |
1930 | | // server is expected to send a CertificateStatus message. (This is used on |
1931 | | // both the client and server sides.) |
1932 | | bool certificate_status_expected : 1; |
1933 | | |
1934 | | // ocsp_stapling_requested is true if a client requested OCSP stapling. |
1935 | | bool ocsp_stapling_requested : 1; |
1936 | | |
1937 | | // should_ack_sni is used by a server and indicates that the SNI extension |
1938 | | // should be echoed in the ServerHello. |
1939 | | bool should_ack_sni : 1; |
1940 | | |
1941 | | // in_false_start is true if there is a pending client handshake in False |
1942 | | // Start. The client may write data at this point. |
1943 | | bool in_false_start : 1; |
1944 | | |
1945 | | // in_early_data is true if there is a pending handshake that has progressed |
1946 | | // enough to send and receive early data. |
1947 | | bool in_early_data : 1; |
1948 | | |
1949 | | // early_data_offered is true if the client sent the early_data extension. |
1950 | | bool early_data_offered : 1; |
1951 | | |
1952 | | // can_early_read is true if application data may be read at this point in the |
1953 | | // handshake. |
1954 | | bool can_early_read : 1; |
1955 | | |
1956 | | // can_early_write is true if application data may be written at this point in |
1957 | | // the handshake. |
1958 | | bool can_early_write : 1; |
1959 | | |
1960 | | // is_early_version is true if the protocol version configured is not |
1961 | | // necessarily the final version and is just the predicted 0-RTT version. |
1962 | | bool is_early_version : 1; |
1963 | | |
1964 | | // next_proto_neg_seen is one of NPN was negotiated. |
1965 | | bool next_proto_neg_seen : 1; |
1966 | | |
1967 | | // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent |
1968 | | // or received. |
1969 | | bool ticket_expected : 1; |
1970 | | |
1971 | | // extended_master_secret is true if the extended master secret extension is |
1972 | | // negotiated in this handshake. |
1973 | | bool extended_master_secret : 1; |
1974 | | |
1975 | | // pending_private_key_op is true if there is a pending private key operation |
1976 | | // in progress. |
1977 | | bool pending_private_key_op : 1; |
1978 | | |
1979 | | // handback indicates that a server should pause the handshake after |
1980 | | // finishing operations that require private key material, in such a way that |
1981 | | // |SSL_get_error| returns |SSL_ERROR_HANDBACK|. It is set by |
1982 | | // |SSL_apply_handoff|. |
1983 | | bool handback : 1; |
1984 | | |
1985 | | // hints_requested indicates the caller has requested handshake hints. Only |
1986 | | // the first round-trip of the handshake will complete, after which the |
1987 | | // |hints| structure can be serialized. |
1988 | | bool hints_requested : 1; |
1989 | | |
1990 | | // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid. |
1991 | | bool cert_compression_negotiated : 1; |
1992 | | |
1993 | | // apply_jdk11_workaround is true if the peer is probably a JDK 11 client |
1994 | | // which implemented TLS 1.3 incorrectly. |
1995 | | bool apply_jdk11_workaround : 1; |
1996 | | |
1997 | | // can_release_private_key is true if the private key will no longer be used |
1998 | | // in this handshake. |
1999 | | bool can_release_private_key : 1; |
2000 | | |
2001 | | // channel_id_negotiated is true if Channel ID should be used in this |
2002 | | // handshake. |
2003 | | bool channel_id_negotiated : 1; |
2004 | | |
2005 | | // received_hello_verify_request is true if we received a HelloVerifyRequest |
2006 | | // message from the server. |
2007 | | bool received_hello_verify_request : 1; |
2008 | | |
2009 | | // matched_peer_trust_anchor indicates that we have matched a trust anchor |
2010 | | // the peer requested in the trust anchors extension. |
2011 | | bool matched_peer_trust_anchor : 1; |
2012 | | |
2013 | | // peer_matched_trust_anchor is true if the peer indicated a match with one of |
2014 | | // our requested trust anchors. |
2015 | | bool peer_matched_trust_anchor : 1; |
2016 | | |
2017 | | // client_version is the value sent or received in the ClientHello version. |
2018 | | uint16_t client_version = 0; |
2019 | | |
2020 | | // early_data_read is the amount of early data that has been read by the |
2021 | | // record layer. |
2022 | | uint16_t early_data_read = 0; |
2023 | | |
2024 | | // early_data_written is the amount of early data that has been written by the |
2025 | | // record layer. |
2026 | | uint16_t early_data_written = 0; |
2027 | | |
2028 | | // signature_algorithm is the signature algorithm to be used in signing with |
2029 | | // the selected credential, or zero if not applicable or not yet selected. |
2030 | | uint16_t signature_algorithm = 0; |
2031 | | |
2032 | | // ech_config_id is the ECH config sent by the client. |
2033 | | uint8_t ech_config_id = 0; |
2034 | | |
2035 | | // session_id is the session ID in the ClientHello. |
2036 | | InplaceVector<uint8_t, SSL_MAX_SSL_SESSION_ID_LENGTH> session_id; |
2037 | | |
2038 | | // grease_seed is the entropy for GREASE values. |
2039 | | uint8_t grease_seed[ssl_grease_last_index + 1] = {0}; |
2040 | | |
2041 | | // pake_share is the PAKE message received over the wire, if any. |
2042 | | UniquePtr<SSLPAKEShare> pake_share; |
2043 | | |
2044 | | // pake_share_bytes are the bytes of the PAKEShare to send, if any. |
2045 | | Array<uint8_t> pake_share_bytes; |
2046 | | |
2047 | | // pake_prover is the PAKE context for a client. |
2048 | | UniquePtr<spake2plus::Prover> pake_prover; |
2049 | | |
2050 | | // pake_verifier is the PAKE context for a server. |
2051 | | UniquePtr<spake2plus::Verifier> pake_verifier; |
2052 | | }; |
2053 | | |
2054 | | // kMaxTickets is the maximum number of tickets to send immediately after the |
2055 | | // handshake. We use a one-byte ticket nonce, and there is no point in sending |
2056 | | // so many tickets. |
2057 | | constexpr size_t kMaxTickets = 16; |
2058 | | |
2059 | | UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl); |
2060 | | |
2061 | | // ssl_check_message_type checks if |msg| has type |type|. If so it returns |
2062 | | // one. Otherwise, it sends an alert and returns zero. |
2063 | | bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type); |
2064 | | |
2065 | | // ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0 |
2066 | | // on error. It sets |out_early_return| to one if we've completed the handshake |
2067 | | // early. |
2068 | | int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return); |
2069 | | |
2070 | | // The following are implementations of |do_handshake| for the client and |
2071 | | // server. |
2072 | | enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs); |
2073 | | enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs); |
2074 | | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs); |
2075 | | enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs); |
2076 | | |
2077 | | // The following functions return human-readable representations of the TLS |
2078 | | // handshake states for debugging. |
2079 | | const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs); |
2080 | | const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs); |
2081 | | const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs); |
2082 | | const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs); |
2083 | | |
2084 | | // tls13_add_key_update queues a KeyUpdate message on |ssl|. |request_type| must |
2085 | | // be one of |SSL_KEY_UPDATE_REQUESTED| or |SSL_KEY_UPDATE_NOT_REQUESTED|. |
2086 | | bool tls13_add_key_update(SSL *ssl, int request_type); |
2087 | | |
2088 | | // tls13_post_handshake processes a post-handshake message. It returns true on |
2089 | | // success and false on failure. |
2090 | | bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg); |
2091 | | |
2092 | | bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
2093 | | bool allow_anonymous); |
2094 | | bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
2095 | | |
2096 | | // tls13_process_finished processes |msg| as a Finished message from the |
2097 | | // peer. If |use_saved_value| is true, the verify_data is compared against |
2098 | | // |hs->expected_client_finished| rather than computed fresh. |
2099 | | bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
2100 | | bool use_saved_value); |
2101 | | |
2102 | | bool tls13_add_certificate(SSL_HANDSHAKE *hs); |
2103 | | |
2104 | | // tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the |
2105 | | // handshake. If it returns |ssl_private_key_retry|, it should be called again |
2106 | | // to retry when the signing operation is completed. |
2107 | | enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs); |
2108 | | |
2109 | | bool tls13_add_finished(SSL_HANDSHAKE *hs); |
2110 | | bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg); |
2111 | | bssl::UniquePtr<SSL_SESSION> tls13_create_session_with_ticket(SSL *ssl, |
2112 | | CBS *body); |
2113 | | |
2114 | | // ssl_setup_extension_permutation computes a ClientHello extension permutation |
2115 | | // for |hs|, if applicable. It returns true on success and false on error. |
2116 | | bool ssl_setup_extension_permutation(SSL_HANDSHAKE *hs); |
2117 | | |
2118 | | // ssl_setup_key_shares computes client key shares and saves them in |hs|. It |
2119 | | // returns true on success and false on failure. If |override_group_id| is zero, |
2120 | | // it offers the default groups, including GREASE. If it is non-zero, it offers |
2121 | | // a single key share of the specified group. |
2122 | | bool ssl_setup_key_shares(SSL_HANDSHAKE *hs, uint16_t override_group_id); |
2123 | | |
2124 | | // ssl_setup_pake_shares computes the client PAKE shares and saves them in |hs|. |
2125 | | // It returns true on success and false on failure. |
2126 | | bool ssl_setup_pake_shares(SSL_HANDSHAKE *hs); |
2127 | | |
2128 | | bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
2129 | | Array<uint8_t> *out_secret, |
2130 | | uint8_t *out_alert, CBS *contents); |
2131 | | bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found, |
2132 | | Span<const uint8_t> *out_peer_key, |
2133 | | uint8_t *out_alert, |
2134 | | const SSL_CLIENT_HELLO *client_hello); |
2135 | | bool ssl_ext_pake_add_serverhello(SSL_HANDSHAKE *hs, CBB *out); |
2136 | | bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out); |
2137 | | |
2138 | | bool ssl_ext_pake_parse_serverhello(SSL_HANDSHAKE *hs, |
2139 | | Array<uint8_t> *out_secret, |
2140 | | uint8_t *out_alert, CBS *contents); |
2141 | | |
2142 | | bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
2143 | | uint8_t *out_alert, |
2144 | | CBS *contents); |
2145 | | bool ssl_ext_pre_shared_key_parse_clienthello( |
2146 | | SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
2147 | | uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, |
2148 | | const SSL_CLIENT_HELLO *client_hello, CBS *contents); |
2149 | | bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out); |
2150 | | |
2151 | | // ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and |
2152 | | // returns whether it's valid. |
2153 | | bool ssl_is_sct_list_valid(const CBS *contents); |
2154 | | |
2155 | | // ssl_write_client_hello_without_extensions writes a ClientHello to |out|, |
2156 | | // up to the extensions field. |type| determines the type of ClientHello to |
2157 | | // write. If |omit_session_id| is true, the session ID is empty. |
2158 | | bool ssl_write_client_hello_without_extensions(const SSL_HANDSHAKE *hs, |
2159 | | CBB *cbb, |
2160 | | ssl_client_hello_type_t type, |
2161 | | bool empty_session_id); |
2162 | | |
2163 | | // ssl_add_client_hello constructs a ClientHello and adds it to the outgoing |
2164 | | // flight. It returns true on success and false on error. |
2165 | | bool ssl_add_client_hello(SSL_HANDSHAKE *hs); |
2166 | | |
2167 | | struct ParsedServerHello { |
2168 | | CBS raw; |
2169 | | uint16_t legacy_version = 0; |
2170 | | CBS random; |
2171 | | CBS session_id; |
2172 | | uint16_t cipher_suite = 0; |
2173 | | uint8_t compression_method = 0; |
2174 | | CBS extensions; |
2175 | | }; |
2176 | | |
2177 | | // ssl_parse_server_hello parses |msg| as a ServerHello. On success, it writes |
2178 | | // the result to |*out| and returns true. Otherwise, it returns false and sets |
2179 | | // |*out_alert| to an alert to send to the peer. |
2180 | | bool ssl_parse_server_hello(ParsedServerHello *out, uint8_t *out_alert, |
2181 | | const SSLMessage &msg); |
2182 | | |
2183 | | enum ssl_cert_verify_context_t { |
2184 | | ssl_cert_verify_server, |
2185 | | ssl_cert_verify_client, |
2186 | | ssl_cert_verify_channel_id, |
2187 | | }; |
2188 | | |
2189 | | // tls13_get_cert_verify_signature_input generates the message to be signed for |
2190 | | // TLS 1.3's CertificateVerify message. |cert_verify_context| determines the |
2191 | | // type of signature. It sets |*out| to a newly allocated buffer containing the |
2192 | | // result. This function returns true on success and false on failure. |
2193 | | bool tls13_get_cert_verify_signature_input( |
2194 | | SSL_HANDSHAKE *hs, Array<uint8_t> *out, |
2195 | | enum ssl_cert_verify_context_t cert_verify_context); |
2196 | | |
2197 | | // ssl_is_valid_alpn_list returns whether |in| is a valid ALPN protocol list. |
2198 | | bool ssl_is_valid_alpn_list(Span<const uint8_t> in); |
2199 | | |
2200 | | // ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server |
2201 | | // selection for |hs->ssl|'s client preferences. |
2202 | | bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs, |
2203 | | Span<const uint8_t> protocol); |
2204 | | |
2205 | | // ssl_alpn_list_contains_protocol returns whether |list|, a serialized ALPN |
2206 | | // protocol list, contains |protocol|. |
2207 | | bool ssl_alpn_list_contains_protocol(Span<const uint8_t> list, |
2208 | | Span<const uint8_t> protocol); |
2209 | | |
2210 | | // ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns |
2211 | | // true on successful negotiation or if nothing was negotiated. It returns false |
2212 | | // and sets |*out_alert| to an alert on error. |
2213 | | bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
2214 | | const SSL_CLIENT_HELLO *client_hello); |
2215 | | |
2216 | | // ssl_get_local_application_settings looks up the configured ALPS value for |
2217 | | // |protocol|. If found, it sets |*out_settings| to the value and returns true. |
2218 | | // Otherwise, it returns false. |
2219 | | bool ssl_get_local_application_settings(const SSL_HANDSHAKE *hs, |
2220 | | Span<const uint8_t> *out_settings, |
2221 | | Span<const uint8_t> protocol); |
2222 | | |
2223 | | // ssl_negotiate_alps negotiates the ALPS extension, if applicable. It returns |
2224 | | // true on successful negotiation or if nothing was negotiated. It returns false |
2225 | | // and sets |*out_alert| to an alert on error. |
2226 | | bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
2227 | | const SSL_CLIENT_HELLO *client_hello); |
2228 | | |
2229 | | // ssl_is_valid_trust_anchor_list returns whether |in| is a valid trust anchor |
2230 | | // identifiers list. |
2231 | | bool ssl_is_valid_trust_anchor_list(Span<const uint8_t> in); |
2232 | | |
2233 | | struct SSLExtension { |
2234 | | SSLExtension(uint16_t type_arg, bool allowed_arg = true) |
2235 | 84.1k | : type(type_arg), allowed(allowed_arg), present(false) { |
2236 | 84.1k | CBS_init(&data, nullptr, 0); |
2237 | 84.1k | } |
2238 | | |
2239 | | uint16_t type; |
2240 | | bool allowed; |
2241 | | bool present; |
2242 | | CBS data; |
2243 | | }; |
2244 | | |
2245 | | // ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances |
2246 | | // it. It writes the parsed extensions to pointers in |extensions|. On success, |
2247 | | // it fills in the |present| and |data| fields and returns true. Otherwise, it |
2248 | | // sets |*out_alert| to an alert to send and returns false. Unknown extensions |
2249 | | // are rejected unless |ignore_unknown| is true. |
2250 | | bool ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert, |
2251 | | std::initializer_list<SSLExtension *> extensions, |
2252 | | bool ignore_unknown); |
2253 | | |
2254 | | // ssl_verify_peer_cert verifies the peer certificate for |hs|. |
2255 | | enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs); |
2256 | | // ssl_reverify_peer_cert verifies the peer certificate for |hs| when resuming a |
2257 | | // session. |
2258 | | enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs, |
2259 | | bool send_alert); |
2260 | | |
2261 | | enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs); |
2262 | | |
2263 | | // ssl_send_finished adds a Finished message to the current flight of messages. |
2264 | | // It returns true on success and false on error. |
2265 | | bool ssl_send_finished(SSL_HANDSHAKE *hs); |
2266 | | |
2267 | | // ssl_send_tls12_certificate adds a TLS 1.2 Certificate message to the current |
2268 | | // flight of messages. It returns true on success and false on error. |
2269 | | bool ssl_send_tls12_certificate(SSL_HANDSHAKE *hs); |
2270 | | |
2271 | | // ssl_handshake_session returns the |SSL_SESSION| corresponding to the current |
2272 | | // handshake. Note, in TLS 1.2 resumptions, this session is immutable. |
2273 | | const SSL_SESSION *ssl_handshake_session(const SSL_HANDSHAKE *hs); |
2274 | | |
2275 | | // ssl_done_writing_client_hello is called after the last ClientHello is written |
2276 | | // by |hs|. It releases some memory that is no longer needed. |
2277 | | void ssl_done_writing_client_hello(SSL_HANDSHAKE *hs); |
2278 | | |
2279 | | |
2280 | | // Flags. |
2281 | | |
2282 | | // SSLFlags is a bitmask of flags that can be encoded with the TLS flags |
2283 | | // extension, draft-ietf-tls-tlsflags-14. For now, our in-memory representation |
2284 | | // matches the wire representation, and we only support flags up to 32. If |
2285 | | // higher values are needed, we can increase the size of the bitmask, or only |
2286 | | // store the flags we implement in the bitmask. |
2287 | | using SSLFlags = uint32_t; |
2288 | | inline constexpr SSLFlags kSSLFlagResumptionAcrossNames = 1 << 8; |
2289 | | |
2290 | | // ssl_add_flags_extension encodes a tls_flags extension (including the header) |
2291 | | // containing the flags in |flags|. It returns true on success and false on |
2292 | | // error. If |flags| is zero (no flags set), it returns true without adding |
2293 | | // anything to |cbb|. |
2294 | | bool ssl_add_flags_extension(CBB *cbb, SSLFlags flags); |
2295 | | |
2296 | | // ssl_parse_flags_extension_request parses tls_flags extension value (excluding |
2297 | | // the header) from |cbs|, for a request message (ClientHello, |
2298 | | // CertificateRequest, or NewSessionTicket). Unrecognized flags will be ignored. |
2299 | | // |
2300 | | // On success, it sets |*out| to the parsed flags and returns true. On error, it |
2301 | | // sets |*out_alert| to a TLS alert and returns false. |
2302 | | bool ssl_parse_flags_extension_request(const CBS *cbs, SSLFlags *out, |
2303 | | uint8_t *out_alert); |
2304 | | |
2305 | | // ssl_parse_flags_extension_response parses tls_flags extension value |
2306 | | // (excluding the header) from |cbs|, for a response message (HelloRetryRequest, |
2307 | | // ServerHello, EncryptedExtensions, or Certificate). Only the flags in |
2308 | | // |allowed_flags| may be present. |
2309 | | // |
2310 | | // On success, it sets |*out| to the parsed flags and returns true. On error, it |
2311 | | // sets |*out_alert| to a TLS alert and returns false. |
2312 | | bool ssl_parse_flags_extension_response(const CBS *cbs, SSLFlags *out, |
2313 | | uint8_t *out_alert, |
2314 | | SSLFlags allowed_flags); |
2315 | | |
2316 | | |
2317 | | // SSLKEYLOGFILE functions. |
2318 | | |
2319 | | // ssl_log_secret logs |secret| with label |label|, if logging is enabled for |
2320 | | // |ssl|. It returns true on success and false on failure. |
2321 | | bool ssl_log_secret(const SSL *ssl, const char *label, |
2322 | | Span<const uint8_t> secret); |
2323 | | |
2324 | | |
2325 | | // ClientHello functions. |
2326 | | |
2327 | | bool ssl_parse_client_hello_with_trailing_data(const SSL *ssl, CBS *cbs, |
2328 | | SSL_CLIENT_HELLO *out); |
2329 | | |
2330 | | bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello, |
2331 | | CBS *out, uint16_t extension_type); |
2332 | | |
2333 | | bool ssl_client_cipher_list_contains_cipher( |
2334 | | const SSL_CLIENT_HELLO *client_hello, uint16_t id); |
2335 | | |
2336 | | |
2337 | | // GREASE. |
2338 | | |
2339 | | // ssl_get_grease_value returns a GREASE value for |hs|. For a given |
2340 | | // connection, the values for each index will be deterministic. This allows the |
2341 | | // same ClientHello be sent twice for a HelloRetryRequest or the same group be |
2342 | | // advertised in both supported_groups and key_shares. |
2343 | | uint16_t ssl_get_grease_value(const SSL_HANDSHAKE *hs, |
2344 | | enum ssl_grease_index_t index); |
2345 | | |
2346 | | |
2347 | | // Signature algorithms. |
2348 | | |
2349 | | // tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature |
2350 | | // algorithms and saves them on |hs|. It returns true on success and false on |
2351 | | // error. |
2352 | | bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs); |
2353 | | |
2354 | | // tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm |
2355 | | // that should be used with |pkey| in TLS 1.1 and earlier. It returns true on |
2356 | | // success and false if |pkey| may not be used at those versions. |
2357 | | bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey); |
2358 | | |
2359 | | // tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use |
2360 | | // with |cred| based on the peer's preferences and the algorithms supported. It |
2361 | | // returns true on success and false on error. |
2362 | | bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, |
2363 | | const SSL_CREDENTIAL *cred, uint16_t *out); |
2364 | | |
2365 | | // tls12_add_verify_sigalgs adds the signature algorithms acceptable for the |
2366 | | // peer signature to |out|. It returns true on success and false on error. |
2367 | | bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out); |
2368 | | |
2369 | | // tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer |
2370 | | // signature from |pkey|. It returns true on success and false on error, setting |
2371 | | // |*out_alert| to an alert to send. |
2372 | | bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert, |
2373 | | uint16_t sigalg, EVP_PKEY *pkey); |
2374 | | |
2375 | | |
2376 | | // Underdocumented functions. |
2377 | | // |
2378 | | // Functions below here haven't been touched up and may be underdocumented. |
2379 | | |
2380 | 159 | #define TLSEXT_CHANNEL_ID_SIZE 128 |
2381 | | |
2382 | | // From RFC 4492, used in encoding the curve type in ECParameters |
2383 | 115k | #define NAMED_CURVE_TYPE 3 |
2384 | | |
2385 | | struct CERT { |
2386 | | static constexpr bool kAllowUniquePtr = true; |
2387 | | |
2388 | | explicit CERT(const SSL_X509_METHOD *x509_method); |
2389 | | ~CERT(); |
2390 | | |
2391 | 4.82k | bool is_valid() const { return legacy_credential != nullptr; } |
2392 | | |
2393 | | // credentials is the list of credentials to select between. Elements of this |
2394 | | // array immutable. |
2395 | | Vector<UniquePtr<SSL_CREDENTIAL>> credentials; |
2396 | | |
2397 | | // legacy_credential is the credential configured by the legacy |
2398 | | // non-credential-based APIs. If IsComplete() returns true, it is appended to |
2399 | | // the list of credentials. |
2400 | | UniquePtr<SSL_CREDENTIAL> legacy_credential; |
2401 | | |
2402 | | // x509_method contains pointers to functions that might deal with |X509| |
2403 | | // compatibility, or might be a no-op, depending on the application. |
2404 | | const SSL_X509_METHOD *x509_method = nullptr; |
2405 | | |
2406 | | // x509_chain may contain a parsed copy of |chain[1..]| from the legacy |
2407 | | // credential. This is only used as a cache in order to implement “get0” |
2408 | | // functions that return a non-owning pointer to the certificate chain. |
2409 | | STACK_OF(X509) *x509_chain = nullptr; |
2410 | | |
2411 | | // x509_leaf may contain a parsed copy of the first element of |chain| from |
2412 | | // the legacy credential. This is only used as a cache in order to implement |
2413 | | // “get0” functions that return a non-owning pointer to the certificate chain. |
2414 | | X509 *x509_leaf = nullptr; |
2415 | | |
2416 | | // x509_stash contains the last |X509| object append to the legacy |
2417 | | // credential's chain. This is a workaround for some third-party code that |
2418 | | // continue to use an |X509| object even after passing ownership with an |
2419 | | // “add0” function. |
2420 | | X509 *x509_stash = nullptr; |
2421 | | |
2422 | | // Certificate setup callback: if set is called whenever a |
2423 | | // certificate may be required (client or server). the callback |
2424 | | // can then examine any appropriate parameters and setup any |
2425 | | // certificates required. This allows advanced applications |
2426 | | // to select certificates on the fly: for example based on |
2427 | | // supported signature algorithms or curves. |
2428 | | int (*cert_cb)(SSL *ssl, void *arg) = nullptr; |
2429 | | void *cert_cb_arg = nullptr; |
2430 | | |
2431 | | // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX |
2432 | | // store is used instead. |
2433 | | X509_STORE *verify_store = nullptr; |
2434 | | |
2435 | | // sid_ctx partitions the session space within a shared session cache or |
2436 | | // ticket key. Only sessions with a matching value will be accepted. |
2437 | | InplaceVector<uint8_t, SSL_MAX_SID_CTX_LENGTH> sid_ctx; |
2438 | | }; |
2439 | | |
2440 | | // |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS. |
2441 | | struct SSL_PROTOCOL_METHOD { |
2442 | | bool is_dtls; |
2443 | | bool (*ssl_new)(SSL *ssl); |
2444 | | void (*ssl_free)(SSL *ssl); |
2445 | | // get_message sets |*out| to the current handshake message and returns true |
2446 | | // if one has been received. It returns false if more input is needed. |
2447 | | bool (*get_message)(const SSL *ssl, SSLMessage *out); |
2448 | | // next_message is called to release the current handshake message. |
2449 | | void (*next_message)(SSL *ssl); |
2450 | | // has_unprocessed_handshake_data returns whether there is buffered |
2451 | | // handshake data that has not been consumed by |get_message|. |
2452 | | bool (*has_unprocessed_handshake_data)(const SSL *ssl); |
2453 | | // Use the |ssl_open_handshake| wrapper. |
2454 | | ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed, |
2455 | | uint8_t *out_alert, Span<uint8_t> in); |
2456 | | // Use the |ssl_open_change_cipher_spec| wrapper. |
2457 | | ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed, |
2458 | | uint8_t *out_alert, |
2459 | | Span<uint8_t> in); |
2460 | | // Use the |ssl_open_app_data| wrapper. |
2461 | | ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out, |
2462 | | size_t *out_consumed, uint8_t *out_alert, |
2463 | | Span<uint8_t> in); |
2464 | | // write_app_data encrypts and writes |in| as application data. On success, it |
2465 | | // returns one and sets |*out_bytes_written| to the number of bytes of |in| |
2466 | | // written. Otherwise, it returns <= 0 and sets |*out_needs_handshake| to |
2467 | | // whether the operation failed because the caller needs to drive the |
2468 | | // handshake. |
2469 | | int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, |
2470 | | size_t *out_bytes_written, Span<const uint8_t> in); |
2471 | | int (*dispatch_alert)(SSL *ssl); |
2472 | | // init_message begins a new handshake message of type |type|. |cbb| is the |
2473 | | // root CBB to be passed into |finish_message|. |*body| is set to a child CBB |
2474 | | // the caller should write to. It returns true on success and false on error. |
2475 | | bool (*init_message)(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
2476 | | // finish_message finishes a handshake message. It sets |*out_msg| to the |
2477 | | // serialized message. It returns true on success and false on error. |
2478 | | bool (*finish_message)(const SSL *ssl, CBB *cbb, |
2479 | | bssl::Array<uint8_t> *out_msg); |
2480 | | // add_message adds a handshake message to the pending flight. It returns |
2481 | | // true on success and false on error. |
2482 | | bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg); |
2483 | | // add_change_cipher_spec adds a ChangeCipherSpec record to the pending |
2484 | | // flight. It returns true on success and false on error. |
2485 | | bool (*add_change_cipher_spec)(SSL *ssl); |
2486 | | // finish_flight marks the pending flight as finished and ready to send. |
2487 | | // |flush| must be called to write it. |
2488 | | void (*finish_flight)(SSL *ssl); |
2489 | | // schedule_ack schedules a DTLS 1.3 ACK to be sent, without an ACK delay. |
2490 | | // |flush| must be called to write it. |
2491 | | void (*schedule_ack)(SSL *ssl); |
2492 | | // flush writes any scheduled data to the transport. It returns one on success |
2493 | | // and <= 0 on error. |
2494 | | int (*flush)(SSL *ssl); |
2495 | | // on_handshake_complete is called when the handshake is complete. |
2496 | | void (*on_handshake_complete)(SSL *ssl); |
2497 | | // set_read_state sets |ssl|'s read cipher state and level to |aead_ctx| and |
2498 | | // |level|. In QUIC, |aead_ctx| is a placeholder object. In TLS 1.3, |
2499 | | // |traffic_secret| is the original traffic secret. This function returns true |
2500 | | // on success and false on error. |
2501 | | // |
2502 | | // TODO(crbug.com/371998381): Take the traffic secrets as input and let the |
2503 | | // function create the SSLAEADContext. |
2504 | | bool (*set_read_state)(SSL *ssl, ssl_encryption_level_t level, |
2505 | | UniquePtr<SSLAEADContext> aead_ctx, |
2506 | | Span<const uint8_t> traffic_secret); |
2507 | | // set_write_state sets |ssl|'s write cipher state and level to |aead_ctx| and |
2508 | | // |level|. In QUIC, |aead_ctx| is a placeholder object In TLS 1.3, |
2509 | | // |traffic_secret| is the original traffic secret. This function returns true |
2510 | | // on success and false on error. |
2511 | | // |
2512 | | // TODO(crbug.com/371998381): Take the traffic secrets as input and let the |
2513 | | // function create the SSLAEADContext. |
2514 | | bool (*set_write_state)(SSL *ssl, ssl_encryption_level_t level, |
2515 | | UniquePtr<SSLAEADContext> aead_ctx, |
2516 | | Span<const uint8_t> traffic_secret); |
2517 | | }; |
2518 | | |
2519 | | // The following wrappers call |open_*| but handle |read_shutdown| correctly. |
2520 | | |
2521 | | // ssl_open_handshake processes a record from |in| for reading a handshake |
2522 | | // message. |
2523 | | ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed, |
2524 | | uint8_t *out_alert, Span<uint8_t> in); |
2525 | | |
2526 | | // ssl_open_change_cipher_spec processes a record from |in| for reading a |
2527 | | // ChangeCipherSpec. |
2528 | | ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
2529 | | uint8_t *out_alert, |
2530 | | Span<uint8_t> in); |
2531 | | |
2532 | | // ssl_open_app_data processes a record from |in| for reading application data. |
2533 | | // On success, it returns |ssl_open_record_success| and sets |*out| to the |
2534 | | // input. If it encounters a post-handshake message, it returns |
2535 | | // |ssl_open_record_discard|. The caller should then retry, after processing any |
2536 | | // messages received with |get_message|. |
2537 | | ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out, |
2538 | | size_t *out_consumed, uint8_t *out_alert, |
2539 | | Span<uint8_t> in); |
2540 | | |
2541 | | struct SSL_X509_METHOD { |
2542 | | // check_CA_list returns one if |names| is a good list of X.509 distinguished |
2543 | | // names and zero otherwise. This is used to ensure that we can reject |
2544 | | // unparsable values at handshake time when using crypto/x509. |
2545 | | bool (*check_CA_list)(STACK_OF(CRYPTO_BUFFER) *names); |
2546 | | |
2547 | | // cert_clear frees and NULLs all X509 certificate-related state. |
2548 | | void (*cert_clear)(CERT *cert); |
2549 | | // cert_free frees all X509-related state. |
2550 | | void (*cert_free)(CERT *cert); |
2551 | | // cert_flush_cached_chain drops any cached |X509|-based certificate chain |
2552 | | // from |cert|. |
2553 | | // cert_dup duplicates any needed fields from |cert| to |new_cert|. |
2554 | | void (*cert_dup)(CERT *new_cert, const CERT *cert); |
2555 | | void (*cert_flush_cached_chain)(CERT *cert); |
2556 | | // cert_flush_cached_chain drops any cached |X509|-based leaf certificate |
2557 | | // from |cert|. |
2558 | | void (*cert_flush_cached_leaf)(CERT *cert); |
2559 | | |
2560 | | // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain| |
2561 | | // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns |
2562 | | // true on success or false on error. |
2563 | | bool (*session_cache_objects)(SSL_SESSION *session); |
2564 | | // session_dup duplicates any needed fields from |session| to |new_session|. |
2565 | | // It returns true on success or false on error. |
2566 | | bool (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session); |
2567 | | // session_clear frees any X509-related state from |session|. |
2568 | | void (*session_clear)(SSL_SESSION *session); |
2569 | | // session_verify_cert_chain verifies the certificate chain in |session|, |
2570 | | // sets |session->verify_result| and returns true on success or false on |
2571 | | // error. |
2572 | | bool (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl, |
2573 | | uint8_t *out_alert); |
2574 | | |
2575 | | // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|. |
2576 | | void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs); |
2577 | | // ssl_new does any necessary initialisation of |hs|. It returns true on |
2578 | | // success or false on error. |
2579 | | bool (*ssl_new)(SSL_HANDSHAKE *hs); |
2580 | | // ssl_free frees anything created by |ssl_new|. |
2581 | | void (*ssl_config_free)(SSL_CONFIG *cfg); |
2582 | | // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|. |
2583 | | void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg); |
2584 | | // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if |
2585 | | // necessary. On success, it updates |ssl|'s certificate configuration as |
2586 | | // needed and returns true. Otherwise, it returns false. |
2587 | | bool (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs); |
2588 | | // ssl_ctx_new does any necessary initialisation of |ctx|. It returns true on |
2589 | | // success or false on error. |
2590 | | bool (*ssl_ctx_new)(SSL_CTX *ctx); |
2591 | | // ssl_ctx_free frees anything created by |ssl_ctx_new|. |
2592 | | void (*ssl_ctx_free)(SSL_CTX *ctx); |
2593 | | // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|. |
2594 | | void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl); |
2595 | | }; |
2596 | | |
2597 | | // ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using |
2598 | | // crypto/x509. |
2599 | | extern const SSL_X509_METHOD ssl_crypto_x509_method; |
2600 | | |
2601 | | // ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid |
2602 | | // crypto/x509. |
2603 | | extern const SSL_X509_METHOD ssl_noop_x509_method; |
2604 | | |
2605 | | struct TicketKey { |
2606 | | static constexpr bool kAllowUniquePtr = true; |
2607 | | |
2608 | | uint8_t name[SSL_TICKET_KEY_NAME_LEN] = {0}; |
2609 | | uint8_t hmac_key[16] = {0}; |
2610 | | uint8_t aes_key[16] = {0}; |
2611 | | // next_rotation_tv_sec is the time (in seconds from the epoch) when the |
2612 | | // current key should be superseded by a new key, or the time when a previous |
2613 | | // key should be dropped. If zero, then the key should not be automatically |
2614 | | // rotated. |
2615 | | uint64_t next_rotation_tv_sec = 0; |
2616 | | }; |
2617 | | |
2618 | | struct CertCompressionAlg { |
2619 | | static constexpr bool kAllowUniquePtr = true; |
2620 | | |
2621 | | ssl_cert_compression_func_t compress = nullptr; |
2622 | | ssl_cert_decompression_func_t decompress = nullptr; |
2623 | | uint16_t alg_id = 0; |
2624 | | }; |
2625 | | |
2626 | | BSSL_NAMESPACE_END |
2627 | | |
2628 | | DEFINE_LHASH_OF(SSL_SESSION) |
2629 | | |
2630 | | BSSL_NAMESPACE_BEGIN |
2631 | | |
2632 | | // An ssl_shutdown_t describes the shutdown state of one end of the connection, |
2633 | | // whether it is alive or has been shutdown via close_notify or fatal alert. |
2634 | | enum ssl_shutdown_t { |
2635 | | ssl_shutdown_none = 0, |
2636 | | ssl_shutdown_close_notify = 1, |
2637 | | ssl_shutdown_error = 2, |
2638 | | }; |
2639 | | |
2640 | | enum ssl_ech_status_t { |
2641 | | // ssl_ech_none indicates ECH was not offered, or we have not gotten far |
2642 | | // enough in the handshake to determine the status. |
2643 | | ssl_ech_none, |
2644 | | // ssl_ech_accepted indicates the server accepted ECH. |
2645 | | ssl_ech_accepted, |
2646 | | // ssl_ech_rejected indicates the server was offered ECH but rejected it. |
2647 | | ssl_ech_rejected, |
2648 | | }; |
2649 | | |
2650 | | struct SSL3_STATE { |
2651 | | static constexpr bool kAllowUniquePtr = true; |
2652 | | |
2653 | | SSL3_STATE(); |
2654 | | ~SSL3_STATE(); |
2655 | | |
2656 | | uint64_t read_sequence = 0; |
2657 | | uint64_t write_sequence = 0; |
2658 | | |
2659 | | uint8_t server_random[SSL3_RANDOM_SIZE] = {0}; |
2660 | | uint8_t client_random[SSL3_RANDOM_SIZE] = {0}; |
2661 | | |
2662 | | // read_buffer holds data from the transport to be processed. |
2663 | | SSLBuffer read_buffer; |
2664 | | // write_buffer holds data to be written to the transport. |
2665 | | SSLBuffer write_buffer; |
2666 | | |
2667 | | // pending_app_data is the unconsumed application data. It points into |
2668 | | // |read_buffer|. |
2669 | | Span<uint8_t> pending_app_data; |
2670 | | |
2671 | | // unreported_bytes_written is the number of bytes successfully written to the |
2672 | | // transport, but not yet reported to the caller. The next |SSL_write| will |
2673 | | // skip this many bytes from the input. This is used if |
2674 | | // |SSL_MODE_ENABLE_PARTIAL_WRITE| is disabled, in which case |SSL_write| only |
2675 | | // reports bytes written when the full caller input is written. |
2676 | | size_t unreported_bytes_written = 0; |
2677 | | |
2678 | | // pending_write, if |has_pending_write| is true, is the caller-supplied data |
2679 | | // corresponding to the current pending write. This is used to check the |
2680 | | // caller retried with a compatible buffer. |
2681 | | Span<const uint8_t> pending_write; |
2682 | | |
2683 | | // pending_write_type, if |has_pending_write| is true, is the record type |
2684 | | // for the current pending write. |
2685 | | // |
2686 | | // TODO(davidben): Remove this when alerts are moved out of this write path. |
2687 | | uint8_t pending_write_type = 0; |
2688 | | |
2689 | | // read_shutdown is the shutdown state for the read half of the connection. |
2690 | | enum ssl_shutdown_t read_shutdown = ssl_shutdown_none; |
2691 | | |
2692 | | // write_shutdown is the shutdown state for the write half of the connection. |
2693 | | enum ssl_shutdown_t write_shutdown = ssl_shutdown_none; |
2694 | | |
2695 | | // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for |
2696 | | // the receive half of the connection. |
2697 | | UniquePtr<ERR_SAVE_STATE> read_error; |
2698 | | |
2699 | | int total_renegotiations = 0; |
2700 | | |
2701 | | // This holds a variable that indicates what we were doing when a 0 or -1 is |
2702 | | // returned. This is needed for non-blocking IO so we know what request |
2703 | | // needs re-doing when in SSL_accept or SSL_connect |
2704 | | int rwstate = SSL_ERROR_NONE; |
2705 | | |
2706 | | enum ssl_encryption_level_t quic_read_level = ssl_encryption_initial; |
2707 | | enum ssl_encryption_level_t quic_write_level = ssl_encryption_initial; |
2708 | | |
2709 | | // version is the protocol version, or zero if the version has not yet been |
2710 | | // set. In clients offering 0-RTT, this version will initially be set to the |
2711 | | // early version, then switched to the final version. To distinguish these |
2712 | | // cases, use |ssl_has_final_version|. |
2713 | | uint16_t version = 0; |
2714 | | |
2715 | | // early_data_skipped is the amount of early data that has been skipped by the |
2716 | | // record layer. |
2717 | | uint16_t early_data_skipped = 0; |
2718 | | |
2719 | | // empty_record_count is the number of consecutive empty records received. |
2720 | | uint8_t empty_record_count = 0; |
2721 | | |
2722 | | // warning_alert_count is the number of consecutive warning alerts |
2723 | | // received. |
2724 | | uint8_t warning_alert_count = 0; |
2725 | | |
2726 | | // key_update_count is the number of consecutive KeyUpdates received. |
2727 | | uint8_t key_update_count = 0; |
2728 | | |
2729 | | // ech_status indicates whether ECH was accepted by the server. |
2730 | | ssl_ech_status_t ech_status = ssl_ech_none; |
2731 | | |
2732 | | // skip_early_data instructs the record layer to skip unexpected early data |
2733 | | // messages when 0RTT is rejected. |
2734 | | bool skip_early_data : 1; |
2735 | | |
2736 | | // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled |
2737 | | // and future messages should use the record layer. |
2738 | | bool v2_hello_done : 1; |
2739 | | |
2740 | | // is_v2_hello is true if the current handshake message was derived from a |
2741 | | // V2ClientHello rather than received from the peer directly. |
2742 | | bool is_v2_hello : 1; |
2743 | | |
2744 | | // has_message is true if the current handshake message has been returned |
2745 | | // at least once by |get_message| and false otherwise. |
2746 | | bool has_message : 1; |
2747 | | |
2748 | | // initial_handshake_complete is true if the initial handshake has |
2749 | | // completed. |
2750 | | bool initial_handshake_complete : 1; |
2751 | | |
2752 | | // session_reused indicates whether a session was resumed. |
2753 | | bool session_reused : 1; |
2754 | | |
2755 | | bool send_connection_binding : 1; |
2756 | | |
2757 | | // channel_id_valid is true if, on the server, the client has negotiated a |
2758 | | // Channel ID and the |channel_id| field is filled in. |
2759 | | bool channel_id_valid : 1; |
2760 | | |
2761 | | // key_update_pending is true if we are in the process of sending a KeyUpdate |
2762 | | // message. As a DoS mitigation (and a requirement in DTLS), we never send |
2763 | | // more than one KeyUpdate at once. In DTLS, this tracks whether there is an |
2764 | | // unACKed KeyUpdate. |
2765 | | bool key_update_pending : 1; |
2766 | | |
2767 | | // early_data_accepted is true if early data was accepted by the server. |
2768 | | bool early_data_accepted : 1; |
2769 | | |
2770 | | // alert_dispatch is true there is an alert in |send_alert| to be sent. |
2771 | | bool alert_dispatch : 1; |
2772 | | |
2773 | | // renegotiate_pending is whether the read half of the channel is blocked on a |
2774 | | // HelloRequest. |
2775 | | bool renegotiate_pending : 1; |
2776 | | |
2777 | | // used_hello_retry_request is whether the handshake used a TLS 1.3 |
2778 | | // HelloRetryRequest message. |
2779 | | bool used_hello_retry_request : 1; |
2780 | | |
2781 | | // was_key_usage_invalid is whether the handshake succeeded despite using a |
2782 | | // TLS mode which was incompatible with the leaf certificate's keyUsage |
2783 | | // extension. |
2784 | | bool was_key_usage_invalid : 1; |
2785 | | |
2786 | | // hs_buf is the buffer of handshake data to process. |
2787 | | UniquePtr<BUF_MEM> hs_buf; |
2788 | | |
2789 | | // pending_hs_data contains the pending handshake data that has not yet |
2790 | | // been encrypted to |pending_flight|. This allows packing the handshake into |
2791 | | // fewer records. |
2792 | | UniquePtr<BUF_MEM> pending_hs_data; |
2793 | | |
2794 | | // pending_flight is the pending outgoing flight. This is used to flush each |
2795 | | // handshake flight in a single write. |write_buffer| must be written out |
2796 | | // before this data. |
2797 | | UniquePtr<BUF_MEM> pending_flight; |
2798 | | |
2799 | | // pending_flight_offset is the number of bytes of |pending_flight| which have |
2800 | | // been successfully written. |
2801 | | uint32_t pending_flight_offset = 0; |
2802 | | |
2803 | | // ticket_age_skew is the difference, in seconds, between the client-sent |
2804 | | // ticket age and the server-computed value in TLS 1.3 server connections |
2805 | | // which resumed a session. |
2806 | | int32_t ticket_age_skew = 0; |
2807 | | |
2808 | | // ssl_early_data_reason stores details on why 0-RTT was accepted or rejected. |
2809 | | enum ssl_early_data_reason_t early_data_reason = ssl_early_data_unknown; |
2810 | | |
2811 | | // aead_read_ctx is the current read cipher state. |
2812 | | UniquePtr<SSLAEADContext> aead_read_ctx; |
2813 | | |
2814 | | // aead_write_ctx is the current write cipher state. |
2815 | | UniquePtr<SSLAEADContext> aead_write_ctx; |
2816 | | |
2817 | | // hs is the handshake state for the current handshake or NULL if there isn't |
2818 | | // one. |
2819 | | UniquePtr<SSL_HANDSHAKE> hs; |
2820 | | |
2821 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> write_traffic_secret; |
2822 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> read_traffic_secret; |
2823 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> exporter_secret; |
2824 | | |
2825 | | // Connection binding to prevent renegotiation attacks |
2826 | | InplaceVector<uint8_t, 12> previous_client_finished; |
2827 | | InplaceVector<uint8_t, 12> previous_server_finished; |
2828 | | |
2829 | | uint8_t send_alert[2] = {0}; |
2830 | | |
2831 | | // established_session is the session established by the connection. This |
2832 | | // session is only filled upon the completion of the handshake and is |
2833 | | // immutable. |
2834 | | UniquePtr<SSL_SESSION> established_session; |
2835 | | |
2836 | | // Next protocol negotiation. For the client, this is the protocol that we |
2837 | | // sent in NextProtocol and is set when handling ServerHello extensions. |
2838 | | // |
2839 | | // For a server, this is the client's selected_protocol from NextProtocol and |
2840 | | // is set when handling the NextProtocol message, before the Finished |
2841 | | // message. |
2842 | | Array<uint8_t> next_proto_negotiated; |
2843 | | |
2844 | | // ALPN information |
2845 | | // (we are in the process of transitioning from NPN to ALPN.) |
2846 | | |
2847 | | // In a server these point to the selected ALPN protocol after the |
2848 | | // ClientHello has been processed. In a client these contain the protocol |
2849 | | // that the server selected once the ServerHello has been processed. |
2850 | | Array<uint8_t> alpn_selected; |
2851 | | |
2852 | | // hostname, on the server, is the value of the SNI extension. |
2853 | | UniquePtr<char> hostname; |
2854 | | |
2855 | | // For a server: |
2856 | | // If |channel_id_valid| is true, then this contains the |
2857 | | // verified Channel ID from the client: a P256 point, (x,y), where |
2858 | | // each are big-endian values. |
2859 | | uint8_t channel_id[64] = {0}; |
2860 | | |
2861 | | // Contains the QUIC transport params received by the peer. |
2862 | | Array<uint8_t> peer_quic_transport_params; |
2863 | | |
2864 | | // srtp_profile is the selected SRTP protection profile for |
2865 | | // DTLS-SRTP. |
2866 | | const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr; |
2867 | | }; |
2868 | | |
2869 | | // lengths of messages |
2870 | 1.57M | #define DTLS1_RT_MAX_HEADER_LENGTH 13 |
2871 | | |
2872 | | // DTLS_PLAINTEXT_RECORD_HEADER_LENGTH is the length of the DTLS record header |
2873 | | // for plaintext records (in DTLS 1.3) or DTLS versions <= 1.2. |
2874 | 117k | #define DTLS_PLAINTEXT_RECORD_HEADER_LENGTH 13 |
2875 | | |
2876 | | // DTLS1_3_RECORD_HEADER_LENGTH is the length of the DTLS 1.3 record header |
2877 | | // sent by BoringSSL for encrypted records. Note that received encrypted DTLS |
2878 | | // 1.3 records might have a different length header. |
2879 | 25.7k | #define DTLS1_3_RECORD_HEADER_WRITE_LENGTH 5 |
2880 | | |
2881 | | static_assert(DTLS1_RT_MAX_HEADER_LENGTH >= DTLS_PLAINTEXT_RECORD_HEADER_LENGTH, |
2882 | | "DTLS1_RT_MAX_HEADER_LENGTH must not be smaller than defined " |
2883 | | "record header lengths"); |
2884 | | static_assert(DTLS1_RT_MAX_HEADER_LENGTH >= DTLS1_3_RECORD_HEADER_WRITE_LENGTH, |
2885 | | "DTLS1_RT_MAX_HEADER_LENGTH must not be smaller than defined " |
2886 | | "record header lengths"); |
2887 | | |
2888 | 526k | #define DTLS1_HM_HEADER_LENGTH 12 |
2889 | | |
2890 | | // A DTLSMessageBitmap maintains a list of bits which may be marked to indicate |
2891 | | // a portion of a message was received or ACKed. |
2892 | | class DTLSMessageBitmap { |
2893 | | public: |
2894 | | // A Range represents a range of bits from |start|, inclusive, to |end|, |
2895 | | // exclusive. |
2896 | | struct Range { |
2897 | | size_t start = 0; |
2898 | | size_t end = 0; |
2899 | | |
2900 | 104k | bool empty() const { return start == end; } |
2901 | 73.8k | size_t size() const { return end - start; } |
2902 | 0 | bool operator==(const Range &r) const { |
2903 | 0 | return start == r.start && end == r.end; |
2904 | 0 | } |
2905 | 0 | bool operator!=(const Range &r) const { return !(*this == r); } |
2906 | | }; |
2907 | | |
2908 | | // Init initializes the structure with |num_bits| unmarked bits, from zero |
2909 | | // to |num_bits - 1|. |
2910 | | bool Init(size_t num_bits); |
2911 | | |
2912 | | // MarkRange marks the bits from |start|, inclusive, to |end|, exclusive. |
2913 | | void MarkRange(size_t start, size_t end); |
2914 | | |
2915 | | // NextUnmarkedRange returns the next range of unmarked bits, starting from |
2916 | | // |start|, inclusive. If all bits after |start| are marked, it returns an |
2917 | | // empty range. |
2918 | | Range NextUnmarkedRange(size_t start) const; |
2919 | | |
2920 | | // IsComplete returns whether every bit in the bitmask has been marked. |
2921 | 288k | bool IsComplete() const { return bytes_.empty(); } |
2922 | | |
2923 | | private: |
2924 | | // bytes_ contains the unmarked bits. We maintain an invariant: if |bytes_| is |
2925 | | // not empty, some bit is unset. |
2926 | | Array<uint8_t> bytes_; |
2927 | | // first_unmarked_byte_ is the index of first byte in |bytes_| that is not |
2928 | | // 0xff. This is maintained to amortize checking if the message is complete. |
2929 | | size_t first_unmarked_byte_ = 0; |
2930 | | }; |
2931 | | |
2932 | | struct hm_header_st { |
2933 | | uint8_t type; |
2934 | | uint32_t msg_len; |
2935 | | uint16_t seq; |
2936 | | uint32_t frag_off; |
2937 | | uint32_t frag_len; |
2938 | | }; |
2939 | | |
2940 | | // An DTLSIncomingMessage is an incoming DTLS message, possibly not yet |
2941 | | // assembled. |
2942 | | struct DTLSIncomingMessage { |
2943 | | static constexpr bool kAllowUniquePtr = true; |
2944 | | |
2945 | 69.0k | Span<uint8_t> msg() { return Span(data).subspan(DTLS1_HM_HEADER_LENGTH); } |
2946 | 163k | Span<const uint8_t> msg() const { |
2947 | 163k | return Span(data).subspan(DTLS1_HM_HEADER_LENGTH); |
2948 | 163k | } |
2949 | 89.8k | size_t msg_len() const { return msg().size(); } |
2950 | | |
2951 | | // type is the type of the message. |
2952 | | uint8_t type = 0; |
2953 | | // seq is the sequence number of this message. |
2954 | | uint16_t seq = 0; |
2955 | | // data contains the message, including the message header of length |
2956 | | // |DTLS1_HM_HEADER_LENGTH|. |
2957 | | Array<uint8_t> data; |
2958 | | // reassembly tracks which parts of the message have been received. |
2959 | | DTLSMessageBitmap reassembly; |
2960 | | }; |
2961 | | |
2962 | | struct DTLSOutgoingMessage { |
2963 | 29.8k | size_t msg_len() const { |
2964 | 29.8k | assert(!is_ccs); |
2965 | 29.8k | assert(data.size() >= DTLS1_HM_HEADER_LENGTH); |
2966 | 29.8k | return data.size() - DTLS1_HM_HEADER_LENGTH; |
2967 | 29.8k | } |
2968 | | |
2969 | 42.5k | bool IsFullyAcked() const { |
2970 | | // ACKs only exist in DTLS 1.3, which does not send ChangeCipherSpec. |
2971 | 42.5k | return !is_ccs && acked.IsComplete(); |
2972 | 42.5k | } |
2973 | | |
2974 | | Array<uint8_t> data; |
2975 | | uint16_t epoch = 0; |
2976 | | bool is_ccs = false; |
2977 | | // acked tracks which bits of the message have been ACKed by the peer. If |
2978 | | // |msg_len| is zero, it tracks one bit for whether the header has been |
2979 | | // received. |
2980 | | DTLSMessageBitmap acked; |
2981 | | }; |
2982 | | |
2983 | | struct OPENSSL_timeval { |
2984 | | uint64_t tv_sec; |
2985 | | uint32_t tv_usec; |
2986 | | }; |
2987 | | |
2988 | | struct DTLSTimer { |
2989 | | public: |
2990 | | static constexpr uint64_t kNever = UINT64_MAX; |
2991 | | |
2992 | | // StartMicroseconds schedules the timer to expire the specified number of |
2993 | | // microseconds from |now|. |
2994 | | void StartMicroseconds(OPENSSL_timeval now, uint64_t microseconds); |
2995 | | |
2996 | | // Stop disables the timer. |
2997 | | void Stop(); |
2998 | | |
2999 | | // IsExpired returns true if the timer was set and is expired at time |now|. |
3000 | | bool IsExpired(OPENSSL_timeval now) const; |
3001 | | |
3002 | | // IsSet returns true if the timer is scheduled or expired, and false if it is |
3003 | | // stopped. |
3004 | | bool IsSet() const; |
3005 | | |
3006 | | // MicrosecondsRemaining returns the time remaining, in microseconds, at |
3007 | | // |now|, or |kNever| if the timer is unset. |
3008 | | uint64_t MicrosecondsRemaining(OPENSSL_timeval now) const; |
3009 | | |
3010 | | private: |
3011 | | // expire_time_ is the time when the timer expires, or zero if the timer is |
3012 | | // unset. |
3013 | | // |
3014 | | // TODO(crbug.com/366284846): This is an extremely inconvenient time |
3015 | | // representation. Switch libssl to something like a 64-bit count of |
3016 | | // microseconds. While it's decidedly past 1970 now, zero is a less obviously |
3017 | | // sound distinguished value for the monotonic clock, so maybe we should use a |
3018 | | // different distinguished time, like |INT64_MAX| in the microseconds |
3019 | | // representation. |
3020 | | OPENSSL_timeval expire_time_ = {0, 0}; |
3021 | | }; |
3022 | | |
3023 | | // DTLS_MAX_EXTRA_WRITE_EPOCHS is the maximum number of additional write epochs |
3024 | | // that DTLS may need to retain. |
3025 | | // |
3026 | | // The maximum is, as a DTLS 1.3 server, immediately after sending Finished. At |
3027 | | // this point, the current epoch is the application write keys (epoch 3), but we |
3028 | | // may have ServerHello (epoch 0) and EncryptedExtensions (epoch 1) to |
3029 | | // retransmit. KeyUpdate does not increase this count. If the server were to |
3030 | | // initiate KeyUpdate from this state, it would not apply the new epoch until |
3031 | | // the client's ACKs have caught up. At that point, epochs 0 and 1 can be |
3032 | | // discarded. |
3033 | | #define DTLS_MAX_EXTRA_WRITE_EPOCHS 2 |
3034 | | |
3035 | | // DTLS_MAX_ACK_BUFFER is the maximum number of records worth of data we'll keep |
3036 | | // track of with DTLS 1.3 ACKs. When we exceed this value, information about |
3037 | | // stale records will be dropped. This will not break the connection but may |
3038 | | // cause ACKs to perform worse and retransmit unnecessary information. |
3039 | | #define DTLS_MAX_ACK_BUFFER 32 |
3040 | | |
3041 | | // A DTLSSentRecord records information about a record we sent. Each record |
3042 | | // covers all bytes from |first_msg_start| (inclusive) of |first_msg| to |
3043 | | // |last_msg_end| (exclusive) of |last_msg|. Messages are referenced by index |
3044 | | // into |outgoing_messages|. |last_msg_end| may be |outgoing_messages.size()| if |
3045 | | // |last_msg_end| is zero. |
3046 | | // |
3047 | | // When the message is empty, |first_msg_start| and |last_msg_end| are |
3048 | | // maintained as if there is a single bit in the message representing the |
3049 | | // header. See |acked| in DTLSOutgoingMessage. |
3050 | | struct DTLSSentRecord { |
3051 | | DTLSRecordNumber number; |
3052 | | PackedSize<SSL_MAX_HANDSHAKE_FLIGHT> first_msg = 0; |
3053 | | PackedSize<SSL_MAX_HANDSHAKE_FLIGHT> last_msg = 0; |
3054 | | uint32_t first_msg_start = 0; |
3055 | | uint32_t last_msg_end = 0; |
3056 | | }; |
3057 | | |
3058 | | enum class QueuedKeyUpdate { |
3059 | | kNone, |
3060 | | kUpdateNotRequested, |
3061 | | kUpdateRequested, |
3062 | | }; |
3063 | | |
3064 | | // DTLS_PREV_READ_EPOCH_EXPIRE_SECONDS is how long to retain the previous read |
3065 | | // epoch in DTLS 1.3. This value is set based on the following: |
3066 | | // |
3067 | | // - Section 4.2.1 of RFC 9147 recommends retaining past read epochs for the |
3068 | | // default TCP MSL. This accommodates packet reordering with KeyUpdate. |
3069 | | // |
3070 | | // - Section 5.8.1 of RFC 9147 requires being capable of ACKing the client's |
3071 | | // final flight for at least twice the default MSL. That requires retaining |
3072 | | // epoch 2 after the handshake. |
3073 | | // |
3074 | | // - Section 4 of RFC 9293 defines the MSL to be two minutes. |
3075 | 65.2k | #define DTLS_PREV_READ_EPOCH_EXPIRE_SECONDS (4 * 60) |
3076 | | |
3077 | | struct DTLSPrevReadEpoch { |
3078 | | static constexpr bool kAllowUniquePtr = true; |
3079 | | DTLSReadEpoch epoch; |
3080 | | // expire is the expiration time of the read epoch, expressed as a POSIX |
3081 | | // timestamp in seconds. |
3082 | | uint64_t expire; |
3083 | | }; |
3084 | | |
3085 | | struct DTLS1_STATE { |
3086 | | static constexpr bool kAllowUniquePtr = true; |
3087 | | |
3088 | | DTLS1_STATE(); |
3089 | | ~DTLS1_STATE(); |
3090 | | |
3091 | | bool Init(); |
3092 | | |
3093 | | // has_change_cipher_spec is true if we have received a ChangeCipherSpec from |
3094 | | // the peer in this epoch. |
3095 | | bool has_change_cipher_spec : 1; |
3096 | | |
3097 | | // outgoing_messages_complete is true if |outgoing_messages| has been |
3098 | | // completed by an attempt to flush it. Future calls to |add_message| and |
3099 | | // |add_change_cipher_spec| will start a new flight. |
3100 | | bool outgoing_messages_complete : 1; |
3101 | | |
3102 | | // flight_has_reply is true if the current outgoing flight is complete and has |
3103 | | // processed at least one message. This is used to detect whether we or the |
3104 | | // peer sent the final flight. |
3105 | | bool flight_has_reply : 1; |
3106 | | |
3107 | | // handshake_write_overflow and handshake_read_overflow are true if |
3108 | | // handshake_write_seq and handshake_read_seq, respectively have overflowed. |
3109 | | bool handshake_write_overflow : 1; |
3110 | | bool handshake_read_overflow : 1; |
3111 | | |
3112 | | // sending_flight and sending_ack are true if we are in the process of sending |
3113 | | // a handshake flight and ACK, respectively. |
3114 | | bool sending_flight : 1; |
3115 | | bool sending_ack : 1; |
3116 | | // pending_flush is whether we have a pending flush on the transport. |
3117 | | bool pending_flush : 1; |
3118 | | |
3119 | | // queued_key_update, if not kNone, indicates we've queued a KeyUpdate message |
3120 | | // to send after the current flight is ACKed. |
3121 | | QueuedKeyUpdate queued_key_update : 2; |
3122 | | |
3123 | | uint16_t handshake_write_seq = 0; |
3124 | | uint16_t handshake_read_seq = 0; |
3125 | | |
3126 | | // read_epoch is the current read epoch. |
3127 | | DTLSReadEpoch read_epoch; |
3128 | | |
3129 | | // next_read_epoch is the next read epoch in DTLS 1.3. It will become |
3130 | | // current once a record is received from it. |
3131 | | UniquePtr<DTLSReadEpoch> next_read_epoch; |
3132 | | |
3133 | | // prev_read_epoch is the previous read epoch in DTLS 1.3. |
3134 | | UniquePtr<DTLSPrevReadEpoch> prev_read_epoch; |
3135 | | |
3136 | | // write_epoch is the current DTLS write epoch. Non-retransmit records will |
3137 | | // generally use this epoch. |
3138 | | // TODO(crbug.com/381113363): 0-RTT will be the exception, when implemented. |
3139 | | DTLSWriteEpoch write_epoch; |
3140 | | |
3141 | | // extra_write_epochs is the collection available write epochs. |
3142 | | InplaceVector<UniquePtr<DTLSWriteEpoch>, DTLS_MAX_EXTRA_WRITE_EPOCHS> |
3143 | | extra_write_epochs; |
3144 | | |
3145 | | // incoming_messages is a ring buffer of incoming handshake messages that have |
3146 | | // yet to be processed. The front of the ring buffer is message number |
3147 | | // |handshake_read_seq|, at position |handshake_read_seq| % |
3148 | | // |SSL_MAX_HANDSHAKE_FLIGHT|. |
3149 | | UniquePtr<DTLSIncomingMessage> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT]; |
3150 | | |
3151 | | // outgoing_messages is the queue of outgoing messages from the last handshake |
3152 | | // flight. |
3153 | | InplaceVector<DTLSOutgoingMessage, SSL_MAX_HANDSHAKE_FLIGHT> |
3154 | | outgoing_messages; |
3155 | | |
3156 | | // sent_records is a queue of records we sent, for processing ACKs. To save |
3157 | | // memory in the steady state, the structure is stored on the heap and dropped |
3158 | | // when empty. |
3159 | | UniquePtr<MRUQueue<DTLSSentRecord, DTLS_MAX_ACK_BUFFER>> sent_records; |
3160 | | |
3161 | | // records_to_ack is a queue of received records that we should ACK. This is |
3162 | | // not stored on the heap because, in the steady state, DTLS 1.3 does not |
3163 | | // necessarily empty this list. (We probably could drop records from here once |
3164 | | // they are sufficiently old.) |
3165 | | MRUQueue<DTLSRecordNumber, DTLS_MAX_ACK_BUFFER> records_to_ack; |
3166 | | |
3167 | | // outgoing_written is the number of outgoing messages that have been |
3168 | | // written. |
3169 | | uint8_t outgoing_written = 0; |
3170 | | // outgoing_offset is the number of bytes of the next outgoing message have |
3171 | | // been written. |
3172 | | uint32_t outgoing_offset = 0; |
3173 | | |
3174 | | unsigned mtu = 0; // max DTLS packet size |
3175 | | |
3176 | | // num_timeouts is the number of times the retransmit timer has fired since |
3177 | | // the last time it was reset. |
3178 | | unsigned num_timeouts = 0; |
3179 | | |
3180 | | // retransmit_timer tracks when to schedule the next DTLS retransmit if we do |
3181 | | // not hear from the peer. |
3182 | | DTLSTimer retransmit_timer; |
3183 | | |
3184 | | // ack_timer tracks when to send an ACK. |
3185 | | DTLSTimer ack_timer; |
3186 | | |
3187 | | // timeout_duration_ms is the timeout duration in milliseconds. |
3188 | | uint32_t timeout_duration_ms = 0; |
3189 | | }; |
3190 | | |
3191 | | // An ALPSConfig is a pair of ALPN protocol and settings value to use with ALPS. |
3192 | | struct ALPSConfig { |
3193 | | Array<uint8_t> protocol; |
3194 | | Array<uint8_t> settings; |
3195 | | }; |
3196 | | |
3197 | | // SSL_CONFIG contains configuration bits that can be shed after the handshake |
3198 | | // completes. Objects of this type are not shared; they are unique to a |
3199 | | // particular |SSL|. |
3200 | | // |
3201 | | // See SSL_shed_handshake_config() for more about the conditions under which |
3202 | | // configuration can be shed. |
3203 | | struct SSL_CONFIG { |
3204 | | static constexpr bool kAllowUniquePtr = true; |
3205 | | |
3206 | | explicit SSL_CONFIG(SSL *ssl_arg); |
3207 | | ~SSL_CONFIG(); |
3208 | | |
3209 | | // ssl is a non-owning pointer to the parent |SSL| object. |
3210 | | SSL *const ssl = nullptr; |
3211 | | |
3212 | | // conf_max_version is the maximum acceptable version configured by |
3213 | | // |SSL_set_max_proto_version|. Note this version is not normalized in DTLS |
3214 | | // and is further constrained by |SSL_OP_NO_*|. |
3215 | | uint16_t conf_max_version = 0; |
3216 | | |
3217 | | // conf_min_version is the minimum acceptable version configured by |
3218 | | // |SSL_set_min_proto_version|. Note this version is not normalized in DTLS |
3219 | | // and is further constrained by |SSL_OP_NO_*|. |
3220 | | uint16_t conf_min_version = 0; |
3221 | | |
3222 | | X509_VERIFY_PARAM *param = nullptr; |
3223 | | |
3224 | | // crypto |
3225 | | UniquePtr<SSLCipherPreferenceList> cipher_list; |
3226 | | |
3227 | | // This is used to hold the local certificate used (i.e. the server |
3228 | | // certificate for a server or the client certificate for a client). |
3229 | | UniquePtr<CERT> cert; |
3230 | | |
3231 | | int (*verify_callback)(int ok, |
3232 | | X509_STORE_CTX *ctx) = |
3233 | | nullptr; // fail if callback returns 0 |
3234 | | |
3235 | | enum ssl_verify_result_t (*custom_verify_callback)( |
3236 | | SSL *ssl, uint8_t *out_alert) = nullptr; |
3237 | | // Server-only: psk_identity_hint is the identity hint to send in |
3238 | | // PSK-based key exchanges. |
3239 | | UniquePtr<char> psk_identity_hint; |
3240 | | |
3241 | | unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity, |
3242 | | unsigned max_identity_len, uint8_t *psk, |
3243 | | unsigned max_psk_len) = nullptr; |
3244 | | unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk, |
3245 | | unsigned max_psk_len) = nullptr; |
3246 | | |
3247 | | // for server side, keep the list of CA_dn we can use |
3248 | | UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA; |
3249 | | |
3250 | | // cached_x509_client_CA is a cache of parsed versions of the elements of |
3251 | | // |client_CA|. |
3252 | | STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr; |
3253 | | |
3254 | | // For client side, keep the list of CA distinguished names we can use |
3255 | | // for the Certificate Authorities extension. |
3256 | | // TODO(bbe) having this separate from the client side (above) is mildly |
3257 | | // silly, but OpenSSL has *_client_CA API's for this exposed, and for the |
3258 | | // moment we are not crossing those streams. |
3259 | | UniquePtr<STACK_OF(CRYPTO_BUFFER)> CA_names; |
3260 | | |
3261 | | // Trust anchor IDs to be requested in the trust_anchors extension. |
3262 | | std::optional<Array<uint8_t>> requested_trust_anchors; |
3263 | | |
3264 | | Array<uint16_t> supported_group_list; // our list |
3265 | | |
3266 | | // channel_id_private is the client's Channel ID private key, or null if |
3267 | | // Channel ID should not be offered on this connection. |
3268 | | UniquePtr<EVP_PKEY> channel_id_private; |
3269 | | |
3270 | | // For a client, this contains the list of supported protocols in wire |
3271 | | // format. |
3272 | | Array<uint8_t> alpn_client_proto_list; |
3273 | | |
3274 | | // alps_configs contains the list of supported protocols to use with ALPS, |
3275 | | // along with their corresponding ALPS values. |
3276 | | Vector<ALPSConfig> alps_configs; |
3277 | | |
3278 | | // Contains the QUIC transport params that this endpoint will send. |
3279 | | Array<uint8_t> quic_transport_params; |
3280 | | |
3281 | | // Contains the context used to decide whether to accept early data in QUIC. |
3282 | | Array<uint8_t> quic_early_data_context; |
3283 | | |
3284 | | // verify_sigalgs, if not empty, is the set of signature algorithms |
3285 | | // accepted from the peer in decreasing order of preference. |
3286 | | Array<uint16_t> verify_sigalgs; |
3287 | | |
3288 | | // srtp_profiles is the list of configured SRTP protection profiles for |
3289 | | // DTLS-SRTP. |
3290 | | UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles; |
3291 | | |
3292 | | // client_ech_config_list, if not empty, is a serialized ECHConfigList |
3293 | | // structure for the client to use when negotiating ECH. |
3294 | | Array<uint8_t> client_ech_config_list; |
3295 | | |
3296 | | // compliance_policy limits the set of ciphers that can be selected when |
3297 | | // negotiating a TLS 1.3 connection. |
3298 | | enum ssl_compliance_policy_t compliance_policy = ssl_compliance_policy_none; |
3299 | | |
3300 | | // verify_mode is a bitmask of |SSL_VERIFY_*| values. |
3301 | | uint8_t verify_mode = SSL_VERIFY_NONE; |
3302 | | |
3303 | | // ech_grease_enabled controls whether ECH GREASE may be sent in the |
3304 | | // ClientHello. |
3305 | | bool ech_grease_enabled : 1; |
3306 | | |
3307 | | // Enable signed certificate time stamps. Currently client only. |
3308 | | bool signed_cert_timestamps_enabled : 1; |
3309 | | |
3310 | | // ocsp_stapling_enabled is only used by client connections and indicates |
3311 | | // whether OCSP stapling will be requested. |
3312 | | bool ocsp_stapling_enabled : 1; |
3313 | | |
3314 | | // channel_id_enabled is copied from the |SSL_CTX|. For a server, it means |
3315 | | // that we'll accept Channel IDs from clients. It is ignored on the client. |
3316 | | bool channel_id_enabled : 1; |
3317 | | |
3318 | | // If enforce_rsa_key_usage is true, the handshake will fail if the |
3319 | | // keyUsage extension is present and incompatible with the TLS usage. |
3320 | | // This field is not read until after certificate verification. |
3321 | | bool enforce_rsa_key_usage : 1; |
3322 | | |
3323 | | // retain_only_sha256_of_client_certs is true if we should compute the SHA256 |
3324 | | // hash of the peer's certificate and then discard it to save memory and |
3325 | | // session space. Only effective on the server side. |
3326 | | bool retain_only_sha256_of_client_certs : 1; |
3327 | | |
3328 | | // handoff indicates that a server should stop after receiving the |
3329 | | // ClientHello and pause the handshake in such a way that |SSL_get_error| |
3330 | | // returns |SSL_ERROR_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX| |
3331 | | // element of the same name and may be cleared if the handoff is declined. |
3332 | | bool handoff : 1; |
3333 | | |
3334 | | // shed_handshake_config indicates that the handshake config (this object!) |
3335 | | // should be freed after the handshake completes. |
3336 | | bool shed_handshake_config : 1; |
3337 | | |
3338 | | // jdk11_workaround is whether to disable TLS 1.3 for JDK 11 clients, as a |
3339 | | // workaround for https://bugs.openjdk.java.net/browse/JDK-8211806. |
3340 | | bool jdk11_workaround : 1; |
3341 | | |
3342 | | // QUIC drafts up to and including 32 used a different TLS extension |
3343 | | // codepoint to convey QUIC's transport parameters. |
3344 | | bool quic_use_legacy_codepoint : 1; |
3345 | | |
3346 | | // permute_extensions is whether to permute extensions when sending messages. |
3347 | | bool permute_extensions : 1; |
3348 | | |
3349 | | // aes_hw_override if set indicates we should override checking for aes |
3350 | | // hardware support, and use the value in aes_hw_override_value instead. |
3351 | | bool aes_hw_override : 1; |
3352 | | |
3353 | | // aes_hw_override_value is used for testing to indicate the support or lack |
3354 | | // of support for AES hw. The value is only considered if |aes_hw_override| is |
3355 | | // true. |
3356 | | bool aes_hw_override_value : 1; |
3357 | | |
3358 | | // alps_use_new_codepoint if set indicates we use new ALPS extension codepoint |
3359 | | // to negotiate and convey application settings. |
3360 | | bool alps_use_new_codepoint : 1; |
3361 | | }; |
3362 | | |
3363 | | // From RFC 8446, used in determining PSK modes. |
3364 | 71.1k | #define SSL_PSK_DHE_KE 0x1 |
3365 | | |
3366 | | // kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early |
3367 | | // data that will be accepted. This value should be slightly below |
3368 | | // kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext. |
3369 | | static const size_t kMaxEarlyDataAccepted = 14336; |
3370 | | |
3371 | | UniquePtr<CERT> ssl_cert_dup(CERT *cert); |
3372 | | bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer); |
3373 | | bool ssl_is_key_type_supported(int key_type); |
3374 | | // ssl_compare_public_and_private_key returns true if |pubkey| is the public |
3375 | | // counterpart to |privkey|. Otherwise it returns false and pushes a helpful |
3376 | | // message on the error queue. |
3377 | | bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, |
3378 | | const EVP_PKEY *privkey); |
3379 | | bool ssl_get_new_session(SSL_HANDSHAKE *hs); |
3380 | | |
3381 | | // ssl_encrypt_ticket encrypt a ticket for |session| and writes the result to |
3382 | | // |out|. It returns true on success and false on error. If, on success, nothing |
3383 | | // was written to |out|, the caller should skip sending a ticket. |
3384 | | bool ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, |
3385 | | const SSL_SESSION *session); |
3386 | | |
3387 | | bool ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx); |
3388 | | |
3389 | | // ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on |
3390 | | // error. |
3391 | | UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method); |
3392 | | |
3393 | | // ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table |
3394 | | // keyed on session IDs. |
3395 | | uint32_t ssl_hash_session_id(Span<const uint8_t> session_id); |
3396 | | |
3397 | | // SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over |
3398 | | // the parsed data. |
3399 | | OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_parse( |
3400 | | CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool); |
3401 | | |
3402 | | // ssl_session_serialize writes |in| to |cbb| as if it were serialising a |
3403 | | // session for Session-ID resumption. It returns true on success and false on |
3404 | | // error. |
3405 | | OPENSSL_EXPORT bool ssl_session_serialize(const SSL_SESSION *in, CBB *cbb); |
3406 | | |
3407 | | enum class SSLSessionType { |
3408 | | // The session is not resumable. |
3409 | | kNotResumable, |
3410 | | // The session uses a TLS 1.2 session ID. |
3411 | | kID, |
3412 | | // The session uses a TLS 1.2 ticket. |
3413 | | kTicket, |
3414 | | // The session uses a TLS 1.3 pre-shared key. |
3415 | | kPreSharedKey, |
3416 | | }; |
3417 | | |
3418 | | // ssl_session_get_type returns the type of |session|. |
3419 | | SSLSessionType ssl_session_get_type(const SSL_SESSION *session); |
3420 | | |
3421 | | // ssl_session_is_context_valid returns whether |session|'s session ID context |
3422 | | // matches the one set on |hs|. |
3423 | | bool ssl_session_is_context_valid(const SSL_HANDSHAKE *hs, |
3424 | | const SSL_SESSION *session); |
3425 | | |
3426 | | // ssl_session_is_time_valid returns true if |session| is still valid and false |
3427 | | // if it has expired. |
3428 | | bool ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session); |
3429 | | |
3430 | | // ssl_session_is_resumable returns whether |session| is resumable for |hs|. |
3431 | | bool ssl_session_is_resumable(const SSL_HANDSHAKE *hs, |
3432 | | const SSL_SESSION *session); |
3433 | | |
3434 | | // ssl_session_protocol_version returns the protocol version associated with |
3435 | | // |session|. Note that despite the name, this is not the same as |
3436 | | // |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name. |
3437 | | uint16_t ssl_session_protocol_version(const SSL_SESSION *session); |
3438 | | |
3439 | | // ssl_session_get_digest returns the digest used in |session|. |
3440 | | const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session); |
3441 | | |
3442 | | void ssl_set_session(SSL *ssl, SSL_SESSION *session); |
3443 | | |
3444 | | // ssl_get_prev_session looks up the previous session based on |client_hello|. |
3445 | | // On success, it sets |*out_session| to the session or nullptr if none was |
3446 | | // found. If the session could not be looked up synchronously, it returns |
3447 | | // |ssl_hs_pending_session| and should be called again. If a ticket could not be |
3448 | | // decrypted immediately it returns |ssl_hs_pending_ticket| and should also |
3449 | | // be called again. Otherwise, it returns |ssl_hs_error|. |
3450 | | enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs, |
3451 | | UniquePtr<SSL_SESSION> *out_session, |
3452 | | bool *out_tickets_supported, |
3453 | | bool *out_renew_ticket, |
3454 | | const SSL_CLIENT_HELLO *client_hello); |
3455 | | |
3456 | | // The following flags determine which parts of the session are duplicated. |
3457 | 323 | #define SSL_SESSION_DUP_AUTH_ONLY 0x0 |
3458 | 169k | #define SSL_SESSION_INCLUDE_TICKET 0x1 |
3459 | 115k | #define SSL_SESSION_INCLUDE_NONAUTH 0x2 |
3460 | | #define SSL_SESSION_DUP_ALL \ |
3461 | 54.1k | (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH) |
3462 | | |
3463 | | // SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the |
3464 | | // fields in |session| or nullptr on error. The new session is non-resumable and |
3465 | | // must be explicitly marked resumable once it has been filled in. |
3466 | | OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, |
3467 | | int dup_flags); |
3468 | | |
3469 | | // ssl_session_rebase_time updates |session|'s start time to the current time, |
3470 | | // adjusting the timeout so the expiration time is unchanged. |
3471 | | void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session); |
3472 | | |
3473 | | // ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews |
3474 | | // |session|'s timeout to |timeout| (measured from the current time). The |
3475 | | // renewal is clamped to the session's auth_timeout. |
3476 | | void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session, |
3477 | | uint32_t timeout); |
3478 | | |
3479 | | void ssl_update_cache(SSL *ssl); |
3480 | | |
3481 | | void ssl_send_alert(SSL *ssl, int level, int desc); |
3482 | | int ssl_send_alert_impl(SSL *ssl, int level, int desc); |
3483 | | bool tls_get_message(const SSL *ssl, SSLMessage *out); |
3484 | | ssl_open_record_t tls_open_handshake(SSL *ssl, size_t *out_consumed, |
3485 | | uint8_t *out_alert, Span<uint8_t> in); |
3486 | | void tls_next_message(SSL *ssl); |
3487 | | |
3488 | | int tls_dispatch_alert(SSL *ssl); |
3489 | | ssl_open_record_t tls_open_app_data(SSL *ssl, Span<uint8_t> *out, |
3490 | | size_t *out_consumed, uint8_t *out_alert, |
3491 | | Span<uint8_t> in); |
3492 | | ssl_open_record_t tls_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
3493 | | uint8_t *out_alert, |
3494 | | Span<uint8_t> in); |
3495 | | int tls_write_app_data(SSL *ssl, bool *out_needs_handshake, |
3496 | | size_t *out_bytes_written, Span<const uint8_t> in); |
3497 | | |
3498 | | bool tls_new(SSL *ssl); |
3499 | | void tls_free(SSL *ssl); |
3500 | | |
3501 | | bool tls_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
3502 | | bool tls_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg); |
3503 | | bool tls_add_message(SSL *ssl, Array<uint8_t> msg); |
3504 | | bool tls_add_change_cipher_spec(SSL *ssl); |
3505 | | int tls_flush(SSL *ssl); |
3506 | | |
3507 | | bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
3508 | | bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg); |
3509 | | bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg); |
3510 | | bool dtls1_add_change_cipher_spec(SSL *ssl); |
3511 | | void dtls1_finish_flight(SSL *ssl); |
3512 | | void dtls1_schedule_ack(SSL *ssl); |
3513 | | int dtls1_flush(SSL *ssl); |
3514 | | |
3515 | | // ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to |
3516 | | // the pending flight. It returns true on success and false on error. |
3517 | | bool ssl_add_message_cbb(SSL *ssl, CBB *cbb); |
3518 | | |
3519 | | // ssl_hash_message incorporates |msg| into the handshake hash. It returns true |
3520 | | // on success and false on allocation failure. |
3521 | | bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
3522 | | |
3523 | | ssl_open_record_t dtls1_process_ack(SSL *ssl, uint8_t *out_alert, |
3524 | | DTLSRecordNumber ack_record_number, |
3525 | | Span<const uint8_t> data); |
3526 | | ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out, |
3527 | | size_t *out_consumed, uint8_t *out_alert, |
3528 | | Span<uint8_t> in); |
3529 | | ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
3530 | | uint8_t *out_alert, |
3531 | | Span<uint8_t> in); |
3532 | | |
3533 | | int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake, |
3534 | | size_t *out_bytes_written, Span<const uint8_t> in); |
3535 | | |
3536 | | // dtls1_write_record sends a record. It returns one on success and <= 0 on |
3537 | | // error. |
3538 | | int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in, |
3539 | | uint16_t epoch); |
3540 | | |
3541 | | bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
3542 | | CBS *out_body); |
3543 | | |
3544 | | // DTLS1_MTU_TIMEOUTS is the maximum number of retransmit timeouts to expire |
3545 | | // before starting to decrease the MTU. |
3546 | 0 | #define DTLS1_MTU_TIMEOUTS 2 |
3547 | | |
3548 | | // DTLS1_MAX_TIMEOUTS is the maximum number of retransmit timeouts to expire |
3549 | | // before failing the DTLS handshake. |
3550 | 13.0k | #define DTLS1_MAX_TIMEOUTS 12 |
3551 | | |
3552 | | void dtls1_stop_timer(SSL *ssl); |
3553 | | |
3554 | | unsigned int dtls1_min_mtu(void); |
3555 | | |
3556 | | bool dtls1_new(SSL *ssl); |
3557 | | void dtls1_free(SSL *ssl); |
3558 | | |
3559 | | bool dtls1_process_handshake_fragments(SSL *ssl, uint8_t *out_alert, |
3560 | | DTLSRecordNumber record_number, |
3561 | | Span<const uint8_t> record); |
3562 | | bool dtls1_get_message(const SSL *ssl, SSLMessage *out); |
3563 | | ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed, |
3564 | | uint8_t *out_alert, Span<uint8_t> in); |
3565 | | void dtls1_next_message(SSL *ssl); |
3566 | | int dtls1_dispatch_alert(SSL *ssl); |
3567 | | |
3568 | | // tls1_configure_aead configures either the read or write direction AEAD (as |
3569 | | // determined by |direction|) using the keys generated by the TLS KDF. The |
3570 | | // |key_block_cache| argument is used to store the generated key block, if |
3571 | | // empty. Otherwise it's assumed that the key block is already contained within |
3572 | | // it. It returns true on success or false on error. |
3573 | | bool tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction, |
3574 | | Array<uint8_t> *key_block_cache, |
3575 | | const SSL_SESSION *session, |
3576 | | Span<const uint8_t> iv_override); |
3577 | | |
3578 | | bool tls1_change_cipher_state(SSL_HANDSHAKE *hs, |
3579 | | evp_aead_direction_t direction); |
3580 | | |
3581 | | // tls1_generate_master_secret computes the master secret from |premaster| and |
3582 | | // writes it to |out|. |out| must have size |SSL3_MASTER_SECRET_SIZE|. |
3583 | | bool tls1_generate_master_secret(SSL_HANDSHAKE *hs, Span<uint8_t> out, |
3584 | | Span<const uint8_t> premaster); |
3585 | | |
3586 | | // tls1_get_grouplist returns the locally-configured group preference list. |
3587 | | Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl); |
3588 | | |
3589 | | // tls1_check_group_id returns whether |group_id| is consistent with locally- |
3590 | | // configured group preferences. |
3591 | | bool tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id); |
3592 | | |
3593 | | // tls1_get_shared_group sets |*out_group_id| to the first preferred shared |
3594 | | // group between client and server preferences and returns true. If none may be |
3595 | | // found, it returns false. |
3596 | | bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id); |
3597 | | |
3598 | | // ssl_add_clienthello_tlsext writes ClientHello extensions to |out| for |type|. |
3599 | | // It returns true on success and false on failure. The |header_len| argument is |
3600 | | // the length of the ClientHello written so far and is used to compute the |
3601 | | // padding length. (It does not include the record header or handshake headers.) |
3602 | | // |
3603 | | // If |type| is |ssl_client_hello_inner|, this function also writes the |
3604 | | // compressed extensions to |out_encoded|. Otherwise, |out_encoded| should be |
3605 | | // nullptr. |
3606 | | // |
3607 | | // On success, the function sets |*out_needs_psk_binder| to whether the last |
3608 | | // ClientHello extension was the pre_shared_key extension and needs a PSK binder |
3609 | | // filled in. The caller should then update |out| and, if applicable, |
3610 | | // |out_encoded| with the binder after completing the whole message. |
3611 | | bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, CBB *out_encoded, |
3612 | | bool *out_needs_psk_binder, |
3613 | | ssl_client_hello_type_t type, |
3614 | | size_t header_len); |
3615 | | |
3616 | | bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out); |
3617 | | bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
3618 | | const SSL_CLIENT_HELLO *client_hello); |
3619 | | bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *extensions); |
3620 | | |
3621 | 1.22k | #define tlsext_tick_md EVP_sha256 |
3622 | | |
3623 | | // ssl_process_ticket processes a session ticket from the client. It returns |
3624 | | // one of: |
3625 | | // |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and |
3626 | | // |*out_renew_ticket| is set to whether the ticket should be renewed. |
3627 | | // |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a |
3628 | | // fresh ticket should be sent, but the given ticket cannot be used. |
3629 | | // |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted. |
3630 | | // Retry later. |
3631 | | // |ssl_ticket_aead_error|: an error occured that is fatal to the connection. |
3632 | | enum ssl_ticket_aead_result_t ssl_process_ticket( |
3633 | | SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
3634 | | bool *out_renew_ticket, Span<const uint8_t> ticket, |
3635 | | Span<const uint8_t> session_id); |
3636 | | |
3637 | | // tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies |
3638 | | // the signature. If the key is valid, it saves the Channel ID and returns true. |
3639 | | // Otherwise, it returns false. |
3640 | | bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
3641 | | |
3642 | | // tls1_write_channel_id generates a Channel ID message and puts the output in |
3643 | | // |cbb|. |ssl->channel_id_private| must already be set before calling. This |
3644 | | // function returns true on success and false on error. |
3645 | | bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb); |
3646 | | |
3647 | | // tls1_channel_id_hash computes the hash to be signed by Channel ID and writes |
3648 | | // it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns |
3649 | | // true on success and false on failure. |
3650 | | bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len); |
3651 | | |
3652 | | // tls1_record_handshake_hashes_for_channel_id records the current handshake |
3653 | | // hashes in |hs->new_session| so that Channel ID resumptions can sign that |
3654 | | // data. |
3655 | | bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs); |
3656 | | |
3657 | | // ssl_can_write returns whether |ssl| is allowed to write. |
3658 | | bool ssl_can_write(const SSL *ssl); |
3659 | | |
3660 | | // ssl_can_read returns wheter |ssl| is allowed to read. |
3661 | | bool ssl_can_read(const SSL *ssl); |
3662 | | |
3663 | | OPENSSL_timeval ssl_ctx_get_current_time(const SSL_CTX *ctx); |
3664 | | |
3665 | | // ssl_reset_error_state resets state for |SSL_get_error|. |
3666 | | void ssl_reset_error_state(SSL *ssl); |
3667 | | |
3668 | | // ssl_set_read_error sets |ssl|'s read half into an error state, saving the |
3669 | | // current state of the error queue. |
3670 | | void ssl_set_read_error(SSL *ssl); |
3671 | | |
3672 | | BSSL_NAMESPACE_END |
3673 | | |
3674 | | |
3675 | | // Opaque C types. |
3676 | | // |
3677 | | // The following types are exported to C code as public typedefs, so they must |
3678 | | // be defined outside of the namespace. |
3679 | | |
3680 | | // ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility |
3681 | | // structure to support the legacy version-locked methods. |
3682 | | struct ssl_method_st { |
3683 | | // version, if non-zero, is the only protocol version acceptable to an |
3684 | | // SSL_CTX initialized from this method. |
3685 | | uint16_t version; |
3686 | | // method is the underlying SSL_PROTOCOL_METHOD that initializes the |
3687 | | // SSL_CTX. |
3688 | | const bssl::SSL_PROTOCOL_METHOD *method; |
3689 | | // x509_method contains pointers to functions that might deal with |X509| |
3690 | | // compatibility, or might be a no-op, depending on the application. |
3691 | | const bssl::SSL_X509_METHOD *x509_method; |
3692 | | }; |
3693 | | |
3694 | | struct ssl_ctx_st : public bssl::RefCounted<ssl_ctx_st> { |
3695 | | explicit ssl_ctx_st(const SSL_METHOD *ssl_method); |
3696 | | ssl_ctx_st(const ssl_ctx_st &) = delete; |
3697 | | ssl_ctx_st &operator=(const ssl_ctx_st &) = delete; |
3698 | | |
3699 | | const bssl::SSL_PROTOCOL_METHOD *method = nullptr; |
3700 | | const bssl::SSL_X509_METHOD *x509_method = nullptr; |
3701 | | |
3702 | | // lock is used to protect various operations on this object. |
3703 | | CRYPTO_MUTEX lock; |
3704 | | |
3705 | | // conf_max_version is the maximum acceptable protocol version configured by |
3706 | | // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS |
3707 | | // and is further constrainted by |SSL_OP_NO_*|. |
3708 | | uint16_t conf_max_version = 0; |
3709 | | |
3710 | | // conf_min_version is the minimum acceptable protocol version configured by |
3711 | | // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS |
3712 | | // and is further constrainted by |SSL_OP_NO_*|. |
3713 | | uint16_t conf_min_version = 0; |
3714 | | |
3715 | | // num_tickets is the number of tickets to send immediately after the TLS 1.3 |
3716 | | // handshake. TLS 1.3 recommends single-use tickets so, by default, issue two |
3717 | | /// in case the client makes several connections before getting a renewal. |
3718 | | uint8_t num_tickets = 2; |
3719 | | |
3720 | | // quic_method is the method table corresponding to the QUIC hooks. |
3721 | | const SSL_QUIC_METHOD *quic_method = nullptr; |
3722 | | |
3723 | | bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list; |
3724 | | |
3725 | | X509_STORE *cert_store = nullptr; |
3726 | | LHASH_OF(SSL_SESSION) *sessions = nullptr; |
3727 | | // Most session-ids that will be cached, default is |
3728 | | // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited. |
3729 | | unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
3730 | | SSL_SESSION *session_cache_head = nullptr; |
3731 | | SSL_SESSION *session_cache_tail = nullptr; |
3732 | | |
3733 | | // handshakes_since_cache_flush is the number of successful handshakes since |
3734 | | // the last cache flush. |
3735 | | int handshakes_since_cache_flush = 0; |
3736 | | |
3737 | | // This can have one of 2 values, ored together, |
3738 | | // SSL_SESS_CACHE_CLIENT, |
3739 | | // SSL_SESS_CACHE_SERVER, |
3740 | | // Default is SSL_SESSION_CACHE_SERVER, which means only |
3741 | | // SSL_accept which cache SSL_SESSIONS. |
3742 | | int session_cache_mode = SSL_SESS_CACHE_SERVER; |
3743 | | |
3744 | | // session_timeout is the default lifetime for new sessions in TLS 1.2 and |
3745 | | // earlier, in seconds. |
3746 | | uint32_t session_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
3747 | | |
3748 | | // session_psk_dhe_timeout is the default lifetime for new sessions in TLS |
3749 | | // 1.3, in seconds. |
3750 | | uint32_t session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT; |
3751 | | |
3752 | | // If this callback is not null, it will be called each time a session id is |
3753 | | // added to the cache. If this function returns 1, it means that the |
3754 | | // callback will do a SSL_SESSION_free() when it has finished using it. |
3755 | | // Otherwise, on 0, it means the callback has finished with it. If |
3756 | | // remove_session_cb is not null, it will be called when a session-id is |
3757 | | // removed from the cache. After the call, OpenSSL will SSL_SESSION_free() |
3758 | | // it. |
3759 | | int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess) = nullptr; |
3760 | | void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess) = nullptr; |
3761 | | SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len, |
3762 | | int *copy) = nullptr; |
3763 | | |
3764 | | // if defined, these override the X509_verify_cert() calls |
3765 | | int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg) = nullptr; |
3766 | | void *app_verify_arg = nullptr; |
3767 | | |
3768 | | ssl_verify_result_t (*custom_verify_callback)(SSL *ssl, |
3769 | | uint8_t *out_alert) = nullptr; |
3770 | | |
3771 | | // Default password callback. |
3772 | | pem_password_cb *default_passwd_callback = nullptr; |
3773 | | |
3774 | | // Default password callback user data. |
3775 | | void *default_passwd_callback_userdata = nullptr; |
3776 | | |
3777 | | // get client cert callback |
3778 | | int (*client_cert_cb)(SSL *ssl, X509 **out_x509, |
3779 | | EVP_PKEY **out_pkey) = nullptr; |
3780 | | |
3781 | | CRYPTO_EX_DATA ex_data; |
3782 | | |
3783 | | // Default values used when no per-SSL value is defined follow |
3784 | | |
3785 | | void (*info_callback)(const SSL *ssl, int type, int value) = nullptr; |
3786 | | |
3787 | | // what we put in client cert requests |
3788 | | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA; |
3789 | | |
3790 | | // cached_x509_client_CA is a cache of parsed versions of the elements of |
3791 | | // |client_CA|. |
3792 | | STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr; |
3793 | | |
3794 | | // What we put in client hello in the CA extension. |
3795 | | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> CA_names; |
3796 | | |
3797 | | // What we request in the trust_anchors extension. |
3798 | | std::optional<bssl::Array<uint8_t>> requested_trust_anchors; |
3799 | | |
3800 | | // Default values to use in SSL structures follow (these are copied by |
3801 | | // SSL_new) |
3802 | | |
3803 | | uint32_t options = 0; |
3804 | | // Disable the auto-chaining feature by default. wpa_supplicant relies on this |
3805 | | // feature, but require callers opt into it. |
3806 | | uint32_t mode = SSL_MODE_NO_AUTO_CHAIN; |
3807 | | uint32_t max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
3808 | | |
3809 | | bssl::UniquePtr<bssl::CERT> cert; |
3810 | | |
3811 | | // callback that allows applications to peek at protocol messages |
3812 | | void (*msg_callback)(int is_write, int version, int content_type, |
3813 | | const void *buf, size_t len, SSL *ssl, |
3814 | | void *arg) = nullptr; |
3815 | | void *msg_callback_arg = nullptr; |
3816 | | |
3817 | | int verify_mode = SSL_VERIFY_NONE; |
3818 | | int (*default_verify_callback)(int ok, X509_STORE_CTX *ctx) = |
3819 | | nullptr; // called 'verify_callback' in the SSL |
3820 | | |
3821 | | X509_VERIFY_PARAM *param = nullptr; |
3822 | | |
3823 | | // select_certificate_cb is called before most ClientHello processing and |
3824 | | // before the decision whether to resume a session is made. See |
3825 | | // |ssl_select_cert_result_t| for details of the return values. |
3826 | | ssl_select_cert_result_t (*select_certificate_cb)(const SSL_CLIENT_HELLO *) = |
3827 | | nullptr; |
3828 | | |
3829 | | // dos_protection_cb is called once the resumption decision for a ClientHello |
3830 | | // has been made. It returns one to continue the handshake or zero to |
3831 | | // abort. |
3832 | | int (*dos_protection_cb)(const SSL_CLIENT_HELLO *) = nullptr; |
3833 | | |
3834 | | // Controls whether to verify certificates when resuming connections. They |
3835 | | // were already verified when the connection was first made, so the default is |
3836 | | // false. For now, this is only respected on clients, not servers. |
3837 | | bool reverify_on_resume = false; |
3838 | | |
3839 | | // Maximum amount of data to send in one fragment. actual record size can be |
3840 | | // more than this due to padding and MAC overheads. |
3841 | | uint16_t max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
3842 | | |
3843 | | // TLS extensions servername callback |
3844 | | int (*servername_callback)(SSL *, int *, void *) = nullptr; |
3845 | | void *servername_arg = nullptr; |
3846 | | |
3847 | | // RFC 4507 session ticket keys. |ticket_key_current| may be NULL before the |
3848 | | // first handshake and |ticket_key_prev| may be NULL at any time. |
3849 | | // Automatically generated ticket keys are rotated as needed at handshake |
3850 | | // time. Hence, all access must be synchronized through |lock|. |
3851 | | bssl::UniquePtr<bssl::TicketKey> ticket_key_current; |
3852 | | bssl::UniquePtr<bssl::TicketKey> ticket_key_prev; |
3853 | | |
3854 | | // Callback to support customisation of ticket key setting |
3855 | | int (*ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv, |
3856 | | EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) = nullptr; |
3857 | | |
3858 | | // Server-only: psk_identity_hint is the default identity hint to send in |
3859 | | // PSK-based key exchanges. |
3860 | | bssl::UniquePtr<char> psk_identity_hint; |
3861 | | |
3862 | | unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity, |
3863 | | unsigned max_identity_len, uint8_t *psk, |
3864 | | unsigned max_psk_len) = nullptr; |
3865 | | unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk, |
3866 | | unsigned max_psk_len) = nullptr; |
3867 | | |
3868 | | |
3869 | | // Next protocol negotiation information |
3870 | | // (for experimental NPN extension). |
3871 | | |
3872 | | // For a server, this contains a callback function by which the set of |
3873 | | // advertised protocols can be provided. |
3874 | | int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out, |
3875 | | unsigned *out_len, void *arg) = nullptr; |
3876 | | void *next_protos_advertised_cb_arg = nullptr; |
3877 | | // For a client, this contains a callback function that selects the |
3878 | | // next protocol from the list provided by the server. |
3879 | | int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len, |
3880 | | const uint8_t *in, unsigned in_len, |
3881 | | void *arg) = nullptr; |
3882 | | void *next_proto_select_cb_arg = nullptr; |
3883 | | |
3884 | | // ALPN information |
3885 | | // (we are in the process of transitioning from NPN to ALPN.) |
3886 | | |
3887 | | // For a server, this contains a callback function that allows the |
3888 | | // server to select the protocol for the connection. |
3889 | | // out: on successful return, this must point to the raw protocol |
3890 | | // name (without the length prefix). |
3891 | | // outlen: on successful return, this contains the length of |*out|. |
3892 | | // in: points to the client's list of supported protocols in |
3893 | | // wire-format. |
3894 | | // inlen: the length of |in|. |
3895 | | int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len, |
3896 | | const uint8_t *in, unsigned in_len, |
3897 | | void *arg) = nullptr; |
3898 | | void *alpn_select_cb_arg = nullptr; |
3899 | | |
3900 | | // For a client, this contains the list of supported protocols in wire |
3901 | | // format. |
3902 | | bssl::Array<uint8_t> alpn_client_proto_list; |
3903 | | |
3904 | | // SRTP profiles we are willing to do from RFC 5764 |
3905 | | bssl::UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles; |
3906 | | |
3907 | | // Defined compression algorithms for certificates. |
3908 | | bssl::Vector<bssl::CertCompressionAlg> cert_compression_algs; |
3909 | | |
3910 | | // Supported group values inherited by SSL structure |
3911 | | bssl::Array<uint16_t> supported_group_list; |
3912 | | |
3913 | | // channel_id_private is the client's Channel ID private key, or null if |
3914 | | // Channel ID should not be offered on this connection. |
3915 | | bssl::UniquePtr<EVP_PKEY> channel_id_private; |
3916 | | |
3917 | | // ech_keys contains the server's list of ECHConfig values and associated |
3918 | | // private keys. This list may be swapped out at any time, so all access must |
3919 | | // be synchronized through |lock|. |
3920 | | bssl::UniquePtr<SSL_ECH_KEYS> ech_keys; |
3921 | | |
3922 | | // keylog_callback, if not NULL, is the key logging callback. See |
3923 | | // |SSL_CTX_set_keylog_callback|. |
3924 | | void (*keylog_callback)(const SSL *ssl, const char *line) = nullptr; |
3925 | | |
3926 | | // current_time_cb, if not NULL, is the function to use to get the current |
3927 | | // time. It sets |*out_clock| to the current time. The |ssl| argument is |
3928 | | // always NULL. See |SSL_CTX_set_current_time_cb|. |
3929 | | void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock) = nullptr; |
3930 | | |
3931 | | // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate |
3932 | | // memory. |
3933 | | CRYPTO_BUFFER_POOL *pool = nullptr; |
3934 | | |
3935 | | // ticket_aead_method contains function pointers for opening and sealing |
3936 | | // session tickets. |
3937 | | const SSL_TICKET_AEAD_METHOD *ticket_aead_method = nullptr; |
3938 | | |
3939 | | // legacy_ocsp_callback implements an OCSP-related callback for OpenSSL |
3940 | | // compatibility. |
3941 | | int (*legacy_ocsp_callback)(SSL *ssl, void *arg) = nullptr; |
3942 | | void *legacy_ocsp_callback_arg = nullptr; |
3943 | | |
3944 | | // compliance_policy limits the set of ciphers that can be selected when |
3945 | | // negotiating a TLS 1.3 connection. |
3946 | | enum ssl_compliance_policy_t compliance_policy = ssl_compliance_policy_none; |
3947 | | |
3948 | | // verify_sigalgs, if not empty, is the set of signature algorithms |
3949 | | // accepted from the peer in decreasing order of preference. |
3950 | | bssl::Array<uint16_t> verify_sigalgs; |
3951 | | |
3952 | | // retain_only_sha256_of_client_certs is true if we should compute the SHA256 |
3953 | | // hash of the peer's certificate and then discard it to save memory and |
3954 | | // session space. Only effective on the server side. |
3955 | | bool retain_only_sha256_of_client_certs : 1; |
3956 | | |
3957 | | // quiet_shutdown is true if the connection should not send a close_notify on |
3958 | | // shutdown. |
3959 | | bool quiet_shutdown : 1; |
3960 | | |
3961 | | // ocsp_stapling_enabled is only used by client connections and indicates |
3962 | | // whether OCSP stapling will be requested. |
3963 | | bool ocsp_stapling_enabled : 1; |
3964 | | |
3965 | | // If true, a client will request certificate timestamps. |
3966 | | bool signed_cert_timestamps_enabled : 1; |
3967 | | |
3968 | | // channel_id_enabled is whether Channel ID is enabled. For a server, means |
3969 | | // that we'll accept Channel IDs from clients. For a client, means that we'll |
3970 | | // advertise support. |
3971 | | bool channel_id_enabled : 1; |
3972 | | |
3973 | | // grease_enabled is whether GREASE (RFC 8701) is enabled. |
3974 | | bool grease_enabled : 1; |
3975 | | |
3976 | | // permute_extensions is whether to permute extensions when sending messages. |
3977 | | bool permute_extensions : 1; |
3978 | | |
3979 | | // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN |
3980 | | // protocols from the peer. |
3981 | | bool allow_unknown_alpn_protos : 1; |
3982 | | |
3983 | | // false_start_allowed_without_alpn is whether False Start (if |
3984 | | // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN. |
3985 | | bool false_start_allowed_without_alpn : 1; |
3986 | | |
3987 | | // handoff indicates that a server should stop after receiving the |
3988 | | // ClientHello and pause the handshake in such a way that |SSL_get_error| |
3989 | | // returns |SSL_ERROR_HANDOFF|. |
3990 | | bool handoff : 1; |
3991 | | |
3992 | | // If enable_early_data is true, early data can be sent and accepted. |
3993 | | bool enable_early_data : 1; |
3994 | | |
3995 | | // aes_hw_override if set indicates we should override checking for AES |
3996 | | // hardware support, and use the value in aes_hw_override_value instead. |
3997 | | bool aes_hw_override : 1; |
3998 | | |
3999 | | // aes_hw_override_value is used for testing to indicate the support or lack |
4000 | | // of support for AES hardware. The value is only considered if |
4001 | | // |aes_hw_override| is true. |
4002 | | bool aes_hw_override_value : 1; |
4003 | | |
4004 | | // resumption_across_names_enabled indicates whether a TLS 1.3 server should |
4005 | | // signal its sessions may be resumed across names in the server certificate. |
4006 | | bool resumption_across_names_enabled : 1; |
4007 | | |
4008 | | private: |
4009 | | friend RefCounted; |
4010 | | ~ssl_ctx_st(); |
4011 | | }; |
4012 | | |
4013 | | struct ssl_st { |
4014 | | explicit ssl_st(SSL_CTX *ctx_arg); |
4015 | | ssl_st(const ssl_st &) = delete; |
4016 | | ssl_st &operator=(const ssl_st &) = delete; |
4017 | | ~ssl_st(); |
4018 | | |
4019 | | // method is the method table corresponding to the current protocol (DTLS or |
4020 | | // TLS). |
4021 | | const bssl::SSL_PROTOCOL_METHOD *method = nullptr; |
4022 | | |
4023 | | // config is a container for handshake configuration. Accesses to this field |
4024 | | // should check for nullptr, since configuration may be shed after the |
4025 | | // handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use |
4026 | | // that instead, and skip the null check.) |
4027 | | bssl::UniquePtr<bssl::SSL_CONFIG> config; |
4028 | | |
4029 | | uint16_t max_send_fragment = 0; |
4030 | | |
4031 | | // There are 2 BIO's even though they are normally both the same. This is so |
4032 | | // data can be read and written to different handlers |
4033 | | |
4034 | | bssl::UniquePtr<BIO> rbio; // used by SSL_read |
4035 | | bssl::UniquePtr<BIO> wbio; // used by SSL_write |
4036 | | |
4037 | | // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|. |
4038 | | // Otherwise, it returns a value corresponding to what operation is needed to |
4039 | | // progress. |
4040 | | bssl::ssl_hs_wait_t (*do_handshake)(bssl::SSL_HANDSHAKE *hs) = nullptr; |
4041 | | |
4042 | | bssl::SSL3_STATE *s3 = nullptr; // TLS variables |
4043 | | bssl::DTLS1_STATE *d1 = nullptr; // DTLS variables |
4044 | | |
4045 | | // callback that allows applications to peek at protocol messages |
4046 | | void (*msg_callback)(int write_p, int version, int content_type, |
4047 | | const void *buf, size_t len, SSL *ssl, |
4048 | | void *arg) = nullptr; |
4049 | | void *msg_callback_arg = nullptr; |
4050 | | |
4051 | | // session info |
4052 | | |
4053 | | // initial_timeout_duration_ms is the default DTLS timeout duration in |
4054 | | // milliseconds. It's used to initialize the timer any time it's restarted. We |
4055 | | // default to RFC 9147's recommendation for real-time applications, 400ms. |
4056 | | uint32_t initial_timeout_duration_ms = 400; |
4057 | | |
4058 | | // session is the configured session to be offered by the client. This session |
4059 | | // is immutable. |
4060 | | bssl::UniquePtr<SSL_SESSION> session; |
4061 | | |
4062 | | void (*info_callback)(const SSL *ssl, int type, int value) = nullptr; |
4063 | | |
4064 | | bssl::UniquePtr<SSL_CTX> ctx; |
4065 | | |
4066 | | // session_ctx is the |SSL_CTX| used for the session cache and related |
4067 | | // settings. |
4068 | | bssl::UniquePtr<SSL_CTX> session_ctx; |
4069 | | |
4070 | | // extra application data |
4071 | | CRYPTO_EX_DATA ex_data; |
4072 | | |
4073 | | uint32_t options = 0; // protocol behaviour |
4074 | | uint32_t mode = 0; // API behaviour |
4075 | | uint32_t max_cert_list = 0; |
4076 | | bssl::UniquePtr<char> hostname; |
4077 | | |
4078 | | // quic_method is the method table corresponding to the QUIC hooks. |
4079 | | const SSL_QUIC_METHOD *quic_method = nullptr; |
4080 | | |
4081 | | // renegotiate_mode controls how peer renegotiation attempts are handled. |
4082 | | ssl_renegotiate_mode_t renegotiate_mode = ssl_renegotiate_never; |
4083 | | |
4084 | | // server is true iff the this SSL* is the server half. Note: before the SSL* |
4085 | | // is initialized by either SSL_set_accept_state or SSL_set_connect_state, |
4086 | | // the side is not determined. In this state, server is always false. |
4087 | | bool server : 1; |
4088 | | |
4089 | | // quiet_shutdown is true if the connection should not send a close_notify on |
4090 | | // shutdown. |
4091 | | bool quiet_shutdown : 1; |
4092 | | |
4093 | | // If enable_early_data is true, early data can be sent and accepted. |
4094 | | bool enable_early_data : 1; |
4095 | | |
4096 | | // resumption_across_names_enabled indicates whether a TLS 1.3 server should |
4097 | | // signal its sessions may be resumed across names in the server certificate. |
4098 | | bool resumption_across_names_enabled : 1; |
4099 | | }; |
4100 | | |
4101 | | struct ssl_session_st : public bssl::RefCounted<ssl_session_st> { |
4102 | | explicit ssl_session_st(const bssl::SSL_X509_METHOD *method); |
4103 | | ssl_session_st(const ssl_session_st &) = delete; |
4104 | | ssl_session_st &operator=(const ssl_session_st &) = delete; |
4105 | | |
4106 | | // ssl_version is the (D)TLS version that established the session. |
4107 | | uint16_t ssl_version = 0; |
4108 | | |
4109 | | // group_id is the ID of the ECDH group used to establish this session or zero |
4110 | | // if not applicable or unknown. |
4111 | | uint16_t group_id = 0; |
4112 | | |
4113 | | // peer_signature_algorithm is the signature algorithm used to authenticate |
4114 | | // the peer, or zero if not applicable or unknown. |
4115 | | uint16_t peer_signature_algorithm = 0; |
4116 | | |
4117 | | // secret, in TLS 1.2 and below, is the master secret associated with the |
4118 | | // session. In TLS 1.3 and up, it is the resumption PSK for sessions handed to |
4119 | | // the caller, but it stores the resumption secret when stored on |SSL| |
4120 | | // objects. |
4121 | | bssl::InplaceVector<uint8_t, SSL_MAX_MASTER_KEY_LENGTH> secret; |
4122 | | |
4123 | | bssl::InplaceVector<uint8_t, SSL_MAX_SSL_SESSION_ID_LENGTH> session_id; |
4124 | | |
4125 | | // this is used to determine whether the session is being reused in |
4126 | | // the appropriate context. It is up to the application to set this, |
4127 | | // via SSL_new |
4128 | | bssl::InplaceVector<uint8_t, SSL_MAX_SID_CTX_LENGTH> sid_ctx; |
4129 | | |
4130 | | bssl::UniquePtr<char> psk_identity; |
4131 | | |
4132 | | // certs contains the certificate chain from the peer, starting with the leaf |
4133 | | // certificate. |
4134 | | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs; |
4135 | | |
4136 | | const bssl::SSL_X509_METHOD *x509_method = nullptr; |
4137 | | |
4138 | | // x509_peer is the peer's certificate. |
4139 | | X509 *x509_peer = nullptr; |
4140 | | |
4141 | | // x509_chain is the certificate chain sent by the peer. NOTE: for historical |
4142 | | // reasons, when a client (so the peer is a server), the chain includes |
4143 | | // |peer|, but when a server it does not. |
4144 | | STACK_OF(X509) *x509_chain = nullptr; |
4145 | | |
4146 | | // x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that |
4147 | | // omits the leaf certificate. This exists because OpenSSL, historically, |
4148 | | // didn't include the leaf certificate in the chain for a server, but did for |
4149 | | // a client. The |x509_chain| always includes it and, if an API call requires |
4150 | | // a chain without, it is stored here. |
4151 | | STACK_OF(X509) *x509_chain_without_leaf = nullptr; |
4152 | | |
4153 | | // verify_result is the result of certificate verification in the case of |
4154 | | // non-fatal certificate errors. |
4155 | | long verify_result = X509_V_ERR_INVALID_CALL; |
4156 | | |
4157 | | // timeout is the lifetime of the session in seconds, measured from |time|. |
4158 | | // This is renewable up to |auth_timeout|. |
4159 | | uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
4160 | | |
4161 | | // auth_timeout is the non-renewable lifetime of the session in seconds, |
4162 | | // measured from |time|. |
4163 | | uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
4164 | | |
4165 | | // time is the time the session was issued, measured in seconds from the UNIX |
4166 | | // epoch. |
4167 | | uint64_t time = 0; |
4168 | | |
4169 | | const SSL_CIPHER *cipher = nullptr; |
4170 | | |
4171 | | CRYPTO_EX_DATA ex_data; // application specific data |
4172 | | |
4173 | | // These are used to make removal of session-ids more efficient and to |
4174 | | // implement a maximum cache size. |
4175 | | SSL_SESSION *prev = nullptr, *next = nullptr; |
4176 | | |
4177 | | bssl::Array<uint8_t> ticket; |
4178 | | |
4179 | | bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list; |
4180 | | |
4181 | | // The OCSP response that came with the session. |
4182 | | bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response; |
4183 | | |
4184 | | // peer_sha256 contains the SHA-256 hash of the peer's certificate if |
4185 | | // |peer_sha256_valid| is true. |
4186 | | uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0}; |
4187 | | |
4188 | | // original_handshake_hash contains the handshake hash (either SHA-1+MD5 or |
4189 | | // SHA-2, depending on TLS version) for the original, full handshake that |
4190 | | // created a session. This is used by Channel IDs during resumption. |
4191 | | bssl::InplaceVector<uint8_t, SSL_MAX_MD_SIZE> original_handshake_hash; |
4192 | | |
4193 | | uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds |
4194 | | |
4195 | | uint32_t ticket_age_add = 0; |
4196 | | |
4197 | | // ticket_max_early_data is the maximum amount of data allowed to be sent as |
4198 | | // early data. If zero, 0-RTT is disallowed. |
4199 | | uint32_t ticket_max_early_data = 0; |
4200 | | |
4201 | | // early_alpn is the ALPN protocol from the initial handshake. This is only |
4202 | | // stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT |
4203 | | // resumptions. For the current connection's ALPN protocol, see |
4204 | | // |alpn_selected| on |SSL3_STATE|. |
4205 | | bssl::Array<uint8_t> early_alpn; |
4206 | | |
4207 | | // local_application_settings, if |has_application_settings| is true, is the |
4208 | | // local ALPS value for this connection. |
4209 | | bssl::Array<uint8_t> local_application_settings; |
4210 | | |
4211 | | // peer_application_settings, if |has_application_settings| is true, is the |
4212 | | // peer ALPS value for this connection. |
4213 | | bssl::Array<uint8_t> peer_application_settings; |
4214 | | |
4215 | | // extended_master_secret is whether the master secret in this session was |
4216 | | // generated using EMS and thus isn't vulnerable to the Triple Handshake |
4217 | | // attack. |
4218 | | bool extended_master_secret : 1; |
4219 | | |
4220 | | // peer_sha256_valid is whether |peer_sha256| is valid. |
4221 | | bool peer_sha256_valid : 1; // Non-zero if peer_sha256 is valid |
4222 | | |
4223 | | // not_resumable is used to indicate that session resumption is disallowed. |
4224 | | bool not_resumable : 1; |
4225 | | |
4226 | | // ticket_age_add_valid is whether |ticket_age_add| is valid. |
4227 | | bool ticket_age_add_valid : 1; |
4228 | | |
4229 | | // is_server is whether this session was created by a server. |
4230 | | bool is_server : 1; |
4231 | | |
4232 | | // is_quic indicates whether this session was created using QUIC. |
4233 | | bool is_quic : 1; |
4234 | | |
4235 | | // has_application_settings indicates whether ALPS was negotiated in this |
4236 | | // session. |
4237 | | bool has_application_settings : 1; |
4238 | | |
4239 | | // is_resumable_across_names indicates whether the session may be resumed for |
4240 | | // any of the identities presented in the certificate. |
4241 | | bool is_resumable_across_names : 1; |
4242 | | |
4243 | | // quic_early_data_context is used to determine whether early data must be |
4244 | | // rejected when performing a QUIC handshake. |
4245 | | bssl::Array<uint8_t> quic_early_data_context; |
4246 | | |
4247 | | private: |
4248 | | friend RefCounted; |
4249 | | ~ssl_session_st(); |
4250 | | }; |
4251 | | |
4252 | | struct ssl_ech_keys_st : public bssl::RefCounted<ssl_ech_keys_st> { |
4253 | 13.3k | ssl_ech_keys_st() : RefCounted(CheckSubClass()) {} |
4254 | | |
4255 | | bssl::Vector<bssl::UniquePtr<bssl::ECHServerConfig>> configs; |
4256 | | |
4257 | | private: |
4258 | | friend RefCounted; |
4259 | 13.3k | ~ssl_ech_keys_st() = default; |
4260 | | }; |
4261 | | |
4262 | | #endif // OPENSSL_HEADER_SSL_INTERNAL_H |