blob: d64f6c482f200d4af764a218d70f4a8da2909622 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE.
140 */
141
David Benjamin2ee94aa2015-04-07 22:38:30 -0400142#ifndef OPENSSL_HEADER_SSL_INTERNAL_H
143#define OPENSSL_HEADER_SSL_INTERNAL_H
Adam Langley95c29f32014-06-20 12:00:00 -0700144
145#include <openssl/base.h>
146
David Benjaminc9376992017-07-24 15:31:13 -0400147#include <stdlib.h>
148
David Benjamin6db66042024-02-20 13:57:21 -0500149#include <algorithm>
David Benjamina75027b2021-07-20 15:07:22 -0400150#include <initializer_list>
David Benjamin499742c2017-07-22 12:45:38 -0400151#include <limits>
David Benjamind272dea2017-07-29 11:44:22 -0400152#include <new>
David Benjamincfc11c22017-07-18 22:45:18 -0400153#include <type_traits>
154#include <utility>
155
Adam Langleyc9fb3752014-06-20 12:00:00 -0700156#include <openssl/aead.h>
Daniel McArdle00e434d2021-02-18 11:47:18 -0500157#include <openssl/curve25519.h>
David Benjamincfc11c22017-07-18 22:45:18 -0400158#include <openssl/err.h>
David Benjamin070a6c32021-05-05 15:39:27 -0400159#include <openssl/hpke.h>
David Benjamin4e840352017-10-11 18:41:11 -0400160#include <openssl/lhash.h>
David Benjamincfc11c22017-07-18 22:45:18 -0400161#include <openssl/mem.h>
David Benjamin499742c2017-07-22 12:45:38 -0400162#include <openssl/span.h>
Jesse Selover6b0d8222018-08-14 12:47:29 -0400163#include <openssl/ssl.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700164#include <openssl/stack.h>
165
David Benjamine52f4c42017-10-01 22:35:10 -0400166#include "../crypto/err/internal.h"
David Benjamin08f5c762017-09-21 02:43:05 -0400167#include "../crypto/internal.h"
David Benjamin7f851162021-06-21 17:01:55 -0400168#include "../crypto/lhash/internal.h"
David Benjamin08f5c762017-09-21 02:43:05 -0400169
Steven Valdezcb966542016-08-17 16:56:14 -0400170
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400171#if defined(OPENSSL_WINDOWS)
David Benjaminc11ea9422017-08-29 16:33:21 -0400172// Windows defines struct timeval in winsock2.h.
David Benjamina353cdb2016-06-09 16:48:33 -0400173OPENSSL_MSVC_PRAGMA(warning(push, 3))
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400174#include <winsock2.h>
David Benjamina353cdb2016-06-09 16:48:33 -0400175OPENSSL_MSVC_PRAGMA(warning(pop))
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400176#else
David Benjamin0abd6f22015-12-04 21:49:53 -0500177#include <sys/time.h>
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400178#endif
179
Steven Valdez87eab492016-06-27 16:34:59 -0400180
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700181BSSL_NAMESPACE_BEGIN
Adam Langleyfcf25832014-12-18 17:42:32 -0800182
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700183struct SSL_CONFIG;
David Benjamin86e95b82017-07-18 16:34:25 -0400184struct SSL_HANDSHAKE;
David Benjamin664e99a2017-10-10 19:23:21 -0400185struct SSL_PROTOCOL_METHOD;
David Benjamin0363de92018-06-29 13:57:38 -0400186struct SSL_X509_METHOD;
Steven Valdez8f36c512017-06-20 10:55:02 -0400187
David Benjaminc11ea9422017-08-29 16:33:21 -0400188// C++ utilities.
David Benjamincfc11c22017-07-18 22:45:18 -0400189
David Benjaminc11ea9422017-08-29 16:33:21 -0400190// New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It
191// returns nullptr on allocation error. It only implements single-object
192// allocation and not new T[n].
193//
194// Note: unlike |new|, this does not support non-public constructors.
David Benjamincfc11c22017-07-18 22:45:18 -0400195template <typename T, typename... Args>
196T *New(Args &&... args) {
David Benjamina4cb62f2017-07-31 13:52:31 -0400197 void *t = OPENSSL_malloc(sizeof(T));
David Benjamincfc11c22017-07-18 22:45:18 -0400198 if (t == nullptr) {
David Benjamincfc11c22017-07-18 22:45:18 -0400199 return nullptr;
200 }
David Benjamina4cb62f2017-07-31 13:52:31 -0400201 return new (t) T(std::forward<Args>(args)...);
David Benjamincfc11c22017-07-18 22:45:18 -0400202}
203
David Benjaminc11ea9422017-08-29 16:33:21 -0400204// Delete behaves like |delete| but uses |OPENSSL_free| to release memory.
205//
206// Note: unlike |delete| this does not support non-public destructors.
David Benjamincfc11c22017-07-18 22:45:18 -0400207template <typename T>
208void Delete(T *t) {
209 if (t != nullptr) {
210 t->~T();
211 OPENSSL_free(t);
212 }
213}
214
David Benjaminc11ea9422017-08-29 16:33:21 -0400215// All types with kAllowUniquePtr set may be used with UniquePtr. Other types
216// may be C structs which require a |BORINGSSL_MAKE_DELETER| registration.
David Benjamincfc11c22017-07-18 22:45:18 -0400217namespace internal {
218template <typename T>
David Benjamin493d5cb2022-04-18 17:20:27 -0400219struct DeleterImpl<T, std::enable_if_t<T::kAllowUniquePtr>> {
David Benjamincfc11c22017-07-18 22:45:18 -0400220 static void Free(T *t) { Delete(t); }
221};
Jesse Selover6b0d8222018-08-14 12:47:29 -0400222} // namespace internal
David Benjamincfc11c22017-07-18 22:45:18 -0400223
David Benjaminc11ea9422017-08-29 16:33:21 -0400224// MakeUnique behaves like |std::make_unique| but returns nullptr on allocation
225// error.
David Benjamincfc11c22017-07-18 22:45:18 -0400226template <typename T, typename... Args>
227UniquePtr<T> MakeUnique(Args &&... args) {
228 return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
229}
230
David Benjaminc5280612024-02-15 00:05:41 -0500231// TODO(davidben): Remove these macros after April 2024, once the C++ runtime
232// dependency has stuck.
David Benjamin506be382017-07-25 15:18:18 -0400233#define HAS_VIRTUAL_DESTRUCTOR
234#define PURE_VIRTUAL = 0
David Benjaminc9376992017-07-24 15:31:13 -0400235
David Benjamin499742c2017-07-22 12:45:38 -0400236// Array<T> is an owning array of elements of |T|.
237template <typename T>
238class Array {
239 public:
240 // Array's default constructor creates an empty array.
241 Array() {}
242 Array(const Array &) = delete;
243 Array(Array &&other) { *this = std::move(other); }
244
245 ~Array() { Reset(); }
246
247 Array &operator=(const Array &) = delete;
248 Array &operator=(Array &&other) {
249 Reset();
250 other.Release(&data_, &size_);
251 return *this;
252 }
253
254 const T *data() const { return data_; }
255 T *data() { return data_; }
256 size_t size() const { return size_; }
David Benjaminb9493552017-09-27 19:02:51 -0400257 bool empty() const { return size_ == 0; }
David Benjamin499742c2017-07-22 12:45:38 -0400258
259 const T &operator[](size_t i) const { return data_[i]; }
260 T &operator[](size_t i) { return data_[i]; }
261
262 T *begin() { return data_; }
David Benjamin24545c52021-06-07 16:05:07 -0400263 const T *begin() const { return data_; }
David Benjamin499742c2017-07-22 12:45:38 -0400264 T *end() { return data_ + size_; }
David Benjamin24545c52021-06-07 16:05:07 -0400265 const T *end() const { return data_ + size_; }
David Benjamin499742c2017-07-22 12:45:38 -0400266
267 void Reset() { Reset(nullptr, 0); }
268
269 // Reset releases the current contents of the array and takes ownership of the
270 // raw pointer supplied by the caller.
271 void Reset(T *new_data, size_t new_size) {
272 for (size_t i = 0; i < size_; i++) {
273 data_[i].~T();
274 }
275 OPENSSL_free(data_);
276 data_ = new_data;
277 size_ = new_size;
278 }
279
280 // Release releases ownership of the array to a raw pointer supplied by the
281 // caller.
282 void Release(T **out, size_t *out_size) {
283 *out = data_;
284 *out_size = size_;
285 data_ = nullptr;
286 size_ = 0;
287 }
288
289 // Init replaces the array with a newly-allocated array of |new_size|
290 // default-constructed copies of |T|. It returns true on success and false on
291 // error.
292 //
293 // Note that if |T| is a primitive type like |uint8_t|, it is uninitialized.
294 bool Init(size_t new_size) {
295 Reset();
296 if (new_size == 0) {
297 return true;
298 }
299
300 if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) {
301 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
302 return false;
303 }
Jesse Selover6b0d8222018-08-14 12:47:29 -0400304 data_ = reinterpret_cast<T *>(OPENSSL_malloc(new_size * sizeof(T)));
David Benjamin499742c2017-07-22 12:45:38 -0400305 if (data_ == nullptr) {
David Benjamin499742c2017-07-22 12:45:38 -0400306 return false;
307 }
308 size_ = new_size;
309 for (size_t i = 0; i < size_; i++) {
310 new (&data_[i]) T;
311 }
312 return true;
313 }
314
David Benjamin08f5c762017-09-21 02:43:05 -0400315 // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns
316 // true on success and false on error.
David Benjamine325c3f2018-04-12 16:11:15 -0400317 bool CopyFrom(Span<const T> in) {
David Benjamin08f5c762017-09-21 02:43:05 -0400318 if (!Init(in.size())) {
319 return false;
320 }
David Benjamin6db66042024-02-20 13:57:21 -0500321 std::copy(in.begin(), in.end(), data_);
David Benjamin08f5c762017-09-21 02:43:05 -0400322 return true;
323 }
324
David Benjamin28655672018-07-18 23:23:25 -0400325 // Shrink shrinks the stored size of the array to |new_size|. It crashes if
326 // the new size is larger. Note this does not shrink the allocation itself.
327 void Shrink(size_t new_size) {
328 if (new_size > size_) {
329 abort();
330 }
David Benjamin662bfad2020-09-15 15:22:01 -0400331 for (size_t i = new_size; i < size_; i++) {
332 data_[i].~T();
333 }
David Benjamin28655672018-07-18 23:23:25 -0400334 size_ = new_size;
335 }
336
David Benjamin499742c2017-07-22 12:45:38 -0400337 private:
338 T *data_ = nullptr;
339 size_t size_ = 0;
340};
341
Daniel McArdleff746c12019-09-16 12:35:05 -0400342// GrowableArray<T> is an array that owns elements of |T|, backed by an
343// Array<T>. When necessary, pushing will automatically trigger a resize.
344//
345// Note, for simplicity, this class currently differs from |std::vector| in that
346// |T| must be efficiently default-constructible. Allocated elements beyond the
347// end of the array are constructed and destructed.
348template <typename T>
349class GrowableArray {
350 public:
351 GrowableArray() = default;
352 GrowableArray(const GrowableArray &) = delete;
353 GrowableArray(GrowableArray &&other) { *this = std::move(other); }
354 ~GrowableArray() {}
355
356 GrowableArray &operator=(const GrowableArray &) = delete;
357 GrowableArray &operator=(GrowableArray &&other) {
358 size_ = other.size_;
359 other.size_ = 0;
360 array_ = std::move(other.array_);
361 return *this;
362 }
363
Daniel McArdle00e434d2021-02-18 11:47:18 -0500364 const T *data() const { return array_.data(); }
365 T *data() { return array_.data(); }
Daniel McArdleff746c12019-09-16 12:35:05 -0400366 size_t size() const { return size_; }
367 bool empty() const { return size_ == 0; }
368
369 const T &operator[](size_t i) const { return array_[i]; }
370 T &operator[](size_t i) { return array_[i]; }
371
372 T *begin() { return array_.data(); }
David Benjamin24545c52021-06-07 16:05:07 -0400373 const T *begin() const { return array_.data(); }
Daniel McArdleff746c12019-09-16 12:35:05 -0400374 T *end() { return array_.data() + size_; }
David Benjamin24545c52021-06-07 16:05:07 -0400375 const T *end() const { return array_.data() + size_; }
Daniel McArdleff746c12019-09-16 12:35:05 -0400376
Steven Valdez51607f12020-08-05 10:46:05 -0400377 void clear() {
378 size_ = 0;
379 array_.Reset();
380 }
381
Daniel McArdleff746c12019-09-16 12:35:05 -0400382 // Push adds |elem| at the end of the internal array, growing if necessary. It
383 // returns false when allocation fails.
384 bool Push(T elem) {
385 if (!MaybeGrow()) {
386 return false;
387 }
388 array_[size_] = std::move(elem);
389 size_++;
390 return true;
391 }
392
393 // CopyFrom replaces the contents of the array with a copy of |in|. It returns
394 // true on success and false on allocation error.
395 bool CopyFrom(Span<const T> in) {
396 if (!array_.CopyFrom(in)) {
397 return false;
398 }
399 size_ = in.size();
400 return true;
401 }
402
403 private:
404 // If there is no room for one more element, creates a new backing array with
405 // double the size of the old one and copies elements over.
406 bool MaybeGrow() {
407 if (array_.size() == 0) {
408 return array_.Init(kDefaultSize);
409 }
410 // No need to grow if we have room for one more T.
411 if (size_ < array_.size()) {
412 return true;
413 }
414 // Double the array's size if it's safe to do so.
415 if (array_.size() > std::numeric_limits<size_t>::max() / 2) {
416 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
417 return false;
418 }
419 Array<T> new_array;
420 if (!new_array.Init(array_.size() * 2)) {
421 return false;
422 }
423 for (size_t i = 0; i < array_.size(); i++) {
424 new_array[i] = std::move(array_[i]);
425 }
426 array_ = std::move(new_array);
427
428 return true;
429 }
430
431 // |size_| is the number of elements stored in this GrowableArray.
432 size_t size_ = 0;
433 // |array_| is the backing array. Note that |array_.size()| is this
434 // GrowableArray's current capacity and that |size_ <= array_.size()|.
435 Array<T> array_;
436 // |kDefaultSize| is the default initial size of the backing array.
437 static constexpr size_t kDefaultSize = 16;
438};
439
David Benjamin879efc32017-09-21 11:20:53 -0400440// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
Adam Langleyddb57cf2018-01-26 09:17:53 -0800441OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
David Benjamin879efc32017-09-21 11:20:53 -0400442
Adam Langleyc215ce72023-05-08 11:35:14 -0700443// GetAllNames helps to implement |*_get_all_*_names| style functions. It
444// writes at most |max_out| string pointers to |out| and returns the number that
445// it would have liked to have written. The strings written consist of
446// |fixed_names_len| strings from |fixed_names| followed by |objects_len|
447// strings taken by projecting |objects| through |name|.
448template <typename T, typename Name>
449inline size_t GetAllNames(const char **out, size_t max_out,
450 Span<const char *const> fixed_names, Name(T::*name),
451 Span<const T> objects) {
452 auto span = bssl::MakeSpan(out, max_out);
453 for (size_t i = 0; !span.empty() && i < fixed_names.size(); i++) {
454 span[0] = fixed_names[i];
455 span = span.subspan(1);
456 }
457 span = span.subspan(0, objects.size());
458 for (size_t i = 0; i < span.size(); i++) {
459 span[i] = objects[i].*name;
460 }
461 return fixed_names.size() + objects.size();
462}
463
David Benjaminfbf10f02024-02-12 16:57:18 -0500464// RefCounted is a common base for ref-counted types. This is an instance of the
465// C++ curiously-recurring template pattern, so a type Foo must subclass
466// RefCounted<Foo>. It additionally must friend RefCounted<Foo> to allow calling
467// the destructor.
468template <typename Derived>
469class RefCounted {
470 public:
471 RefCounted(const RefCounted &) = delete;
472 RefCounted &operator=(const RefCounted &) = delete;
473
474 // These methods are intentionally named differently from `bssl::UpRef` to
475 // avoid a collision. Only the implementations of `FOO_up_ref` and `FOO_free`
476 // should call these.
477 void UpRefInternal() { CRYPTO_refcount_inc(&references_); }
478 void DecRefInternal() {
479 if (CRYPTO_refcount_dec_and_test_zero(&references_)) {
480 Derived *d = static_cast<Derived *>(this);
481 d->~Derived();
482 OPENSSL_free(d);
483 }
484 }
485
486 protected:
487 // Ensure that only `Derived`, which must inherit from `RefCounted<Derived>`,
488 // can call the constructor. This catches bugs where someone inherited from
489 // the wrong base.
490 class CheckSubClass {
491 private:
492 friend Derived;
493 CheckSubClass() = default;
494 };
495 RefCounted(CheckSubClass) {
496 static_assert(std::is_base_of<RefCounted, Derived>::value,
497 "Derived must subclass RefCounted<Derived>");
498 }
499
500 ~RefCounted() = default;
501
502 private:
503 CRYPTO_refcount_t references_ = 1;
504};
505
David Benjamincfc11c22017-07-18 22:45:18 -0400506
David Benjaminc11ea9422017-08-29 16:33:21 -0400507// Protocol versions.
508//
Steven Valdezb84674b2018-08-28 10:14:07 -0400509// Due to DTLS's historical wire version differences, we maintain two notions of
David Benjaminc11ea9422017-08-29 16:33:21 -0400510// version.
511//
512// The "version" or "wire version" is the actual 16-bit value that appears on
513// the wire. It uniquely identifies a version and is also used at API
514// boundaries. The set of supported versions differs between TLS and DTLS. Wire
515// versions are opaque values and may not be compared numerically.
516//
517// The "protocol version" identifies the high-level handshake variant being
Steven Valdezb84674b2018-08-28 10:14:07 -0400518// used. DTLS versions map to the corresponding TLS versions. Protocol versions
519// are sequential and may be compared numerically.
Steven Valdez8f36c512017-06-20 10:55:02 -0400520
David Benjaminc11ea9422017-08-29 16:33:21 -0400521// ssl_protocol_version_from_wire sets |*out| to the protocol version
David Benjamined9aed12017-09-27 19:24:09 -0400522// corresponding to wire version |version| and returns true. If |version| is not
523// a valid TLS or DTLS version, it returns false.
David Benjaminc11ea9422017-08-29 16:33:21 -0400524//
525// Note this simultaneously handles both DTLS and TLS. Use one of the
526// higher-level functions below for most operations.
David Benjamined9aed12017-09-27 19:24:09 -0400527bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400528
David Benjaminc11ea9422017-08-29 16:33:21 -0400529// ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
530// minimum and maximum enabled protocol versions, respectively.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700531bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
David Benjamined9aed12017-09-27 19:24:09 -0400532 uint16_t *out_max_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400533
David Benjamined9aed12017-09-27 19:24:09 -0400534// ssl_supports_version returns whether |hs| supports |version|.
David Benjamin14e51ad2021-05-19 15:24:34 -0400535bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400536
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700537// ssl_method_supports_version returns whether |method| supports |version|.
538bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
539 uint16_t version);
540
David Benjaminc11ea9422017-08-29 16:33:21 -0400541// ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in
David Benjamin83a49932021-05-20 15:57:09 -0400542// decreasing preference order. The version list is filtered to those whose
543// protocol version is at least |extra_min_version|.
544bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb,
545 uint16_t extra_min_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400546
David Benjaminc11ea9422017-08-29 16:33:21 -0400547// ssl_negotiate_version negotiates a common version based on |hs|'s preferences
David Benjamined9aed12017-09-27 19:24:09 -0400548// and the peer preference list in |peer_versions|. On success, it returns true
549// and sets |*out_version| to the selected version. Otherwise, it returns false
David Benjaminc11ea9422017-08-29 16:33:21 -0400550// and sets |*out_alert| to an alert to send.
David Benjamined9aed12017-09-27 19:24:09 -0400551bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
552 uint16_t *out_version, const CBS *peer_versions);
Steven Valdez8f36c512017-06-20 10:55:02 -0400553
David Benjamind1e3ce12017-10-06 18:31:15 -0400554// ssl_protocol_version returns |ssl|'s protocol version. It is an error to
David Benjaminc11ea9422017-08-29 16:33:21 -0400555// call this function before the version is determined.
David Benjamind1e3ce12017-10-06 18:31:15 -0400556uint16_t ssl_protocol_version(const SSL *ssl);
Steven Valdez8f36c512017-06-20 10:55:02 -0400557
David Benjaminc11ea9422017-08-29 16:33:21 -0400558// Cipher suites.
David Benjamin71f07942015-04-08 02:36:59 -0400559
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700560BSSL_NAMESPACE_END
David Benjamin33fc2ba2017-09-13 16:22:58 -0400561
562struct ssl_cipher_st {
563 // name is the OpenSSL name for the cipher.
564 const char *name;
565 // standard_name is the IETF name for the cipher.
566 const char *standard_name;
567 // id is the cipher suite value bitwise OR-d with 0x03000000.
568 uint32_t id;
569
570 // algorithm_* determine the cipher suite. See constants below for the values.
571 uint32_t algorithm_mkey;
572 uint32_t algorithm_auth;
573 uint32_t algorithm_enc;
574 uint32_t algorithm_mac;
575 uint32_t algorithm_prf;
576};
577
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700578BSSL_NAMESPACE_BEGIN
David Benjamin33fc2ba2017-09-13 16:22:58 -0400579
David Benjaminc11ea9422017-08-29 16:33:21 -0400580// Bits for |algorithm_mkey| (key exchange algorithm).
David Benjamine64d2c72017-07-12 16:31:08 -0400581#define SSL_kRSA 0x00000001u
582#define SSL_kECDHE 0x00000002u
David Benjaminc11ea9422017-08-29 16:33:21 -0400583// SSL_kPSK is only set for plain PSK, not ECDHE_PSK.
David Benjamine64d2c72017-07-12 16:31:08 -0400584#define SSL_kPSK 0x00000004u
585#define SSL_kGENERIC 0x00000008u
David Benjamin71f07942015-04-08 02:36:59 -0400586
David Benjaminc11ea9422017-08-29 16:33:21 -0400587// Bits for |algorithm_auth| (server authentication).
David Benjamin90f0f052024-02-12 14:48:31 -0500588#define SSL_aRSA_SIGN 0x00000001u
589#define SSL_aRSA_DECRYPT 0x00000002u
590#define SSL_aECDSA 0x00000004u
David Benjaminc11ea9422017-08-29 16:33:21 -0400591// SSL_aPSK is set for both PSK and ECDHE_PSK.
David Benjamin90f0f052024-02-12 14:48:31 -0500592#define SSL_aPSK 0x00000008u
593#define SSL_aGENERIC 0x00000010u
David Benjamin71f07942015-04-08 02:36:59 -0400594
David Benjamin90f0f052024-02-12 14:48:31 -0500595#define SSL_aCERT (SSL_aRSA_SIGN | SSL_aRSA_DECRYPT | SSL_aECDSA)
David Benjaminc032dfa2016-05-12 14:54:57 -0400596
David Benjaminc11ea9422017-08-29 16:33:21 -0400597// Bits for |algorithm_enc| (symmetric encryption).
Jesse Selover6b0d8222018-08-14 12:47:29 -0400598#define SSL_3DES 0x00000001u
599#define SSL_AES128 0x00000002u
600#define SSL_AES256 0x00000004u
601#define SSL_AES128GCM 0x00000008u
602#define SSL_AES256GCM 0x00000010u
David Benjaminde2d6102023-04-21 17:32:21 -0400603#define SSL_CHACHA20POLY1305 0x00000020u
David Benjamin71f07942015-04-08 02:36:59 -0400604
605#define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
606
David Benjaminc11ea9422017-08-29 16:33:21 -0400607// Bits for |algorithm_mac| (symmetric authentication).
David Benjamine64d2c72017-07-12 16:31:08 -0400608#define SSL_SHA1 0x00000001u
Adam Langleyb1c6f452023-05-03 14:33:37 -0700609#define SSL_SHA256 0x00000002u
David Benjaminc11ea9422017-08-29 16:33:21 -0400610// SSL_AEAD is set for all AEADs.
Adam Langleyb1c6f452023-05-03 14:33:37 -0700611#define SSL_AEAD 0x00000004u
David Benjamin71f07942015-04-08 02:36:59 -0400612
David Benjaminc11ea9422017-08-29 16:33:21 -0400613// Bits for |algorithm_prf| (handshake digest).
David Benjaminb0883312015-08-06 09:54:13 -0400614#define SSL_HANDSHAKE_MAC_DEFAULT 0x1
615#define SSL_HANDSHAKE_MAC_SHA256 0x2
616#define SSL_HANDSHAKE_MAC_SHA384 0x4
David Benjamin71f07942015-04-08 02:36:59 -0400617
David Benjaminbc424022019-07-21 12:05:20 -0400618// SSL_MAX_MD_SIZE is size of the largest hash function used in TLS, SHA-384.
619#define SSL_MAX_MD_SIZE 48
620
David Benjamin48b276d2018-04-12 17:37:32 -0400621// An SSLCipherPreferenceList contains a list of SSL_CIPHERs with equal-
622// preference groups. For TLS clients, the groups are moot because the server
623// picks the cipher and groups cannot be expressed on the wire. However, for
624// servers, the equal-preference groups allow the client's preferences to be
625// partially respected. (This only has an effect with
626// SSL_OP_CIPHER_SERVER_PREFERENCE).
627//
628// The equal-preference groups are expressed by grouping SSL_CIPHERs together.
629// All elements of a group have the same priority: no ordering is expressed
630// within a group.
631//
632// The values in |ciphers| are in one-to-one correspondence with
633// |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of
634// bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to
635// indicate that the corresponding SSL_CIPHER is not the last element of a
636// group, or 0 to indicate that it is.
637//
638// For example, if |in_group_flags| contains all zeros then that indicates a
639// traditional, fully-ordered preference. Every SSL_CIPHER is the last element
640// of the group (i.e. they are all in a one-element group).
641//
642// For a more complex example, consider:
643// ciphers: A B C D E F
644// in_group_flags: 1 1 0 0 1 0
645//
646// That would express the following, order:
647//
648// A E
649// B -> D -> F
650// C
651struct SSLCipherPreferenceList {
652 static constexpr bool kAllowUniquePtr = true;
653
654 SSLCipherPreferenceList() = default;
655 ~SSLCipherPreferenceList();
656
657 bool Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers,
658 Span<const bool> in_group_flags);
Matthew Braithwaited2ed3822018-07-10 16:27:22 -0700659 bool Init(const SSLCipherPreferenceList &);
660
661 void Remove(const SSL_CIPHER *cipher);
David Benjamin48b276d2018-04-12 17:37:32 -0400662
663 UniquePtr<STACK_OF(SSL_CIPHER)> ciphers;
664 bool *in_group_flags = nullptr;
665};
David Benjamin71f07942015-04-08 02:36:59 -0400666
Matthew Braithwaited2ed3822018-07-10 16:27:22 -0700667// AllCiphers returns an array of all supported ciphers, sorted by id.
668Span<const SSL_CIPHER> AllCiphers();
669
David Benjaminc11ea9422017-08-29 16:33:21 -0400670// ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
671// object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
672// and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
673// respectively. The MAC key length is zero except for legacy block and stream
David Benjaminf4962492017-09-27 19:34:32 -0400674// ciphers. It returns true on success and false on error.
675bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
676 size_t *out_mac_secret_len,
677 size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
David Benjamin8525ff32018-09-05 18:44:15 -0500678 uint16_t version, bool is_dtls);
David Benjamin71f07942015-04-08 02:36:59 -0400679
David Benjaminc11ea9422017-08-29 16:33:21 -0400680// ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and
681// |cipher|.
David Benjaminca9e8f52017-08-09 15:02:34 -0400682const EVP_MD *ssl_get_handshake_digest(uint16_t version,
683 const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400684
David Benjamin5be3a742017-10-28 16:08:58 -0400685// ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a
David Benjamin48b276d2018-04-12 17:37:32 -0400686// newly-allocated |SSLCipherPreferenceList| containing the result. It returns
687// true on success and false on failure. If |strict| is true, nonsense will be
688// rejected. If false, nonsense will be silently ignored. An empty result is
Bob Beck8cacbd92023-04-05 14:38:38 -0600689// considered an error regardless of |strict|. |has_aes_hw| indicates if the
690// list should be ordered based on having support for AES in hardware or not.
David Benjamin0ce090a2018-07-02 20:24:40 -0400691bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list,
Bob Beck8cacbd92023-04-05 14:38:38 -0600692 const bool has_aes_hw, const char *rule_str,
693 bool strict);
David Benjamin71f07942015-04-08 02:36:59 -0400694
David Benjaminc11ea9422017-08-29 16:33:21 -0400695// ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth|
David Benjamin90f0f052024-02-12 14:48:31 -0500696// values suitable for use with |key| in TLS 1.2 and below. |sign_ok| indicates
697// whether |key| may be used for signing.
698uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key, bool sign_ok);
David Benjamin71f07942015-04-08 02:36:59 -0400699
David Benjaminf4962492017-09-27 19:34:32 -0400700// ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the
701// server and, optionally, the client with a certificate.
702bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400703
David Benjaminf4962492017-09-27 19:34:32 -0400704// ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a
705// ServerKeyExchange message.
David Benjaminc11ea9422017-08-29 16:33:21 -0400706//
David Benjaminf4962492017-09-27 19:34:32 -0400707// This function may return false while still allowing |cipher| an optional
David Benjaminc11ea9422017-08-29 16:33:21 -0400708// ServerKeyExchange. This is the case for plain PSK ciphers.
David Benjaminf4962492017-09-27 19:34:32 -0400709bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400710
David Benjaminc11ea9422017-08-29 16:33:21 -0400711// ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the
712// length of an encrypted 1-byte record, for use in record-splitting. Otherwise
713// it returns zero.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400714size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
715
Steven Valdezd6f9c352019-06-25 10:13:18 -0400716// ssl_choose_tls13_cipher returns an |SSL_CIPHER| corresponding with the best
Adam Langley451ea3c2022-05-18 16:15:49 -0700717// available from |cipher_suites| compatible with |version|, |group_id|, and
Adam Langley2f6409e2023-04-10 21:09:11 +0000718// |policy|. It returns NULL if there isn't a compatible cipher. |has_aes_hw|
Bob Beck8cacbd92023-04-05 14:38:38 -0600719// indicates if the choice should be made as if support for AES in hardware
720// is available.
721const SSL_CIPHER *ssl_choose_tls13_cipher(CBS cipher_suites, bool has_aes_hw,
722 uint16_t version, uint16_t group_id,
Adam Langley2f6409e2023-04-10 21:09:11 +0000723 enum ssl_compliance_policy_t policy);
Adam Langley451ea3c2022-05-18 16:15:49 -0700724
725// ssl_tls13_cipher_meets_policy returns true if |cipher_id| is acceptable given
Adam Langley2f6409e2023-04-10 21:09:11 +0000726// |policy|.
727bool ssl_tls13_cipher_meets_policy(uint16_t cipher_id,
728 enum ssl_compliance_policy_t policy);
Steven Valdezd6f9c352019-06-25 10:13:18 -0400729
Adam Langleyb1c6f452023-05-03 14:33:37 -0700730// ssl_cipher_is_deprecated returns true if |cipher| is deprecated.
731OPENSSL_EXPORT bool ssl_cipher_is_deprecated(const SSL_CIPHER *cipher);
732
David Benjamin71f07942015-04-08 02:36:59 -0400733
David Benjaminc11ea9422017-08-29 16:33:21 -0400734// Transcript layer.
Steven Valdez908ac192017-01-12 13:17:07 -0500735
David Benjaminc11ea9422017-08-29 16:33:21 -0400736// SSLTranscript maintains the handshake transcript as a combination of a
737// buffer and running hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400738class SSLTranscript {
739 public:
740 SSLTranscript();
741 ~SSLTranscript();
742
David Benjamin83a49932021-05-20 15:57:09 -0400743 SSLTranscript(SSLTranscript &&other) = default;
744 SSLTranscript &operator=(SSLTranscript &&other) = default;
745
David Benjaminc11ea9422017-08-29 16:33:21 -0400746 // Init initializes the handshake transcript. If called on an existing
747 // transcript, it resets the transcript and hash. It returns true on success
748 // and false on failure.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400749 bool Init();
750
David Benjaminc11ea9422017-08-29 16:33:21 -0400751 // InitHash initializes the handshake hash based on the PRF and contents of
752 // the handshake transcript. Subsequent calls to |Update| will update the
753 // rolling hash. It returns one on success and zero on failure. It is an error
David Benjamind55f4502021-08-11 13:19:19 -0400754 // to call this function after the handshake buffer is released. This may be
755 // called multiple times to change the hash function.
David Benjaminca9e8f52017-08-09 15:02:34 -0400756 bool InitHash(uint16_t version, const SSL_CIPHER *cipher);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400757
Steven Valdezcd8470f2017-10-11 12:29:36 -0400758 // UpdateForHelloRetryRequest resets the rolling hash with the
759 // HelloRetryRequest construction. It returns true on success and false on
760 // failure. It is an error to call this function before the handshake buffer
761 // is released.
762 bool UpdateForHelloRetryRequest();
763
David Benjamin9806ae02019-08-16 15:32:03 -0400764 // CopyToHashContext initializes |ctx| with |digest| and the data thus far in
765 // the transcript. It returns true on success and false on failure. If the
766 // handshake buffer is still present, |digest| may be any supported digest.
767 // Otherwise, |digest| must match the transcript hash.
David Benjaminb32aa052021-06-02 17:05:00 -0400768 bool CopyToHashContext(EVP_MD_CTX *ctx, const EVP_MD *digest) const;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400769
David Benjaminb32aa052021-06-02 17:05:00 -0400770 Span<const uint8_t> buffer() const {
David Benjamin75a1f232017-10-11 17:19:19 -0400771 return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
772 buffer_->length);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400773 }
David Benjamin6dc8bf62017-07-19 16:38:21 -0400774
David Benjaminc11ea9422017-08-29 16:33:21 -0400775 // FreeBuffer releases the handshake buffer. Subsequent calls to
776 // |Update| will not update the handshake buffer.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400777 void FreeBuffer();
778
David Benjaminc11ea9422017-08-29 16:33:21 -0400779 // DigestLen returns the length of the PRF hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400780 size_t DigestLen() const;
781
David Benjaminc11ea9422017-08-29 16:33:21 -0400782 // Digest returns the PRF hash. For TLS 1.1 and below, this is
783 // |EVP_md5_sha1|.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400784 const EVP_MD *Digest() const;
785
David Benjaminc11ea9422017-08-29 16:33:21 -0400786 // Update adds |in| to the handshake buffer and handshake hash, whichever is
787 // enabled. It returns true on success and false on failure.
David Benjamin75a1f232017-10-11 17:19:19 -0400788 bool Update(Span<const uint8_t> in);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400789
David Benjaminc11ea9422017-08-29 16:33:21 -0400790 // GetHash writes the handshake hash to |out| which must have room for at
791 // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
792 // the number of bytes written. Otherwise, it returns false.
David Benjaminb32aa052021-06-02 17:05:00 -0400793 bool GetHash(uint8_t *out, size_t *out_len) const;
David Benjamin6dc8bf62017-07-19 16:38:21 -0400794
David Benjaminc11ea9422017-08-29 16:33:21 -0400795 // GetFinishedMAC computes the MAC for the Finished message into the bytes
796 // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
797 // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
798 // on failure.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400799 bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
David Benjaminb32aa052021-06-02 17:05:00 -0400800 bool from_server) const;
David Benjamin6dc8bf62017-07-19 16:38:21 -0400801
802 private:
David Benjaminc11ea9422017-08-29 16:33:21 -0400803 // buffer_, if non-null, contains the handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400804 UniquePtr<BUF_MEM> buffer_;
David Benjamina4e9f8d2018-06-26 00:12:52 -0400805 // hash, if initialized with an |EVP_MD|, maintains the handshake hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400806 ScopedEVP_MD_CTX hash_;
David Benjamin86e95b82017-07-18 16:34:25 -0400807};
Steven Valdez908ac192017-01-12 13:17:07 -0500808
David Benjamin2637f3c2017-10-27 00:56:55 -0400809// tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
810// as the secret and |label| as the label. |seed1| and |seed2| are concatenated
811// to form the seed parameter. It returns true on success and false on failure.
812bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
813 Span<const uint8_t> secret, Span<const char> label,
814 Span<const uint8_t> seed1, Span<const uint8_t> seed2);
Steven Valdez908ac192017-01-12 13:17:07 -0500815
816
David Benjaminc11ea9422017-08-29 16:33:21 -0400817// Encryption layer.
David Benjamin31a07792015-03-03 14:20:26 -0500818
David Benjaminc11ea9422017-08-29 16:33:21 -0400819// SSLAEADContext contains information about an AEAD that is being used to
820// encrypt an SSL connection.
David Benjamincfc11c22017-07-18 22:45:18 -0400821class SSLAEADContext {
822 public:
Steven Valdezc7d4d212017-09-11 13:53:08 -0400823 SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
David Benjamincfc11c22017-07-18 22:45:18 -0400824 ~SSLAEADContext();
David Benjamin9a892502017-07-25 22:26:44 -0400825 static constexpr bool kAllowUniquePtr = true;
826
David Benjamincfc11c22017-07-18 22:45:18 -0400827 SSLAEADContext(const SSLAEADContext &&) = delete;
828 SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
829
David Benjaminc11ea9422017-08-29 16:33:21 -0400830 // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400831 static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
David Benjamincfc11c22017-07-18 22:45:18 -0400832
David Benjaminc11ea9422017-08-29 16:33:21 -0400833 // Create creates an |SSLAEADContext| using the supplied key material. It
834 // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
835 // resulting object, depending on |direction|. |version| is the normalized
836 // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
David Benjaminb9493552017-09-27 19:02:51 -0400837 static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
David Benjamin8525ff32018-09-05 18:44:15 -0500838 uint16_t version, bool is_dtls,
David Benjaminb9493552017-09-27 19:02:51 -0400839 const SSL_CIPHER *cipher,
840 Span<const uint8_t> enc_key,
841 Span<const uint8_t> mac_key,
842 Span<const uint8_t> fixed_iv);
David Benjamincfc11c22017-07-18 22:45:18 -0400843
Steven Valdezc8e0f902018-07-14 11:23:01 -0400844 // CreatePlaceholderForQUIC creates a placeholder |SSLAEADContext| for the
845 // given cipher and version. The resulting object can be queried for various
846 // properties but cannot encrypt or decrypt data.
847 static UniquePtr<SSLAEADContext> CreatePlaceholderForQUIC(
848 uint16_t version, const SSL_CIPHER *cipher);
849
Steven Valdezc7d4d212017-09-11 13:53:08 -0400850 // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
851 // cipher, to make version-specific determinations in the record layer prior
852 // to a cipher being selected.
853 void SetVersionIfNullCipher(uint16_t version);
854
855 // ProtocolVersion returns the protocol version associated with this
856 // SSLAEADContext. It can only be called once |version_| has been set to a
857 // valid value.
858 uint16_t ProtocolVersion() const;
859
860 // RecordVersion returns the record version that should be used with this
861 // SSLAEADContext for record construction and crypto.
862 uint16_t RecordVersion() const;
863
David Benjamincfc11c22017-07-18 22:45:18 -0400864 const SSL_CIPHER *cipher() const { return cipher_; }
865
David Benjaminc11ea9422017-08-29 16:33:21 -0400866 // is_null_cipher returns true if this is the null cipher.
David Benjamincfc11c22017-07-18 22:45:18 -0400867 bool is_null_cipher() const { return !cipher_; }
868
David Benjaminc11ea9422017-08-29 16:33:21 -0400869 // ExplicitNonceLen returns the length of the explicit nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400870 size_t ExplicitNonceLen() const;
871
David Benjaminc11ea9422017-08-29 16:33:21 -0400872 // MaxOverhead returns the maximum overhead of calling |Seal|.
David Benjamincfc11c22017-07-18 22:45:18 -0400873 size_t MaxOverhead() const;
874
David Benjaminc11ea9422017-08-29 16:33:21 -0400875 // SuffixLen calculates the suffix length written by |SealScatter| and writes
876 // it to |*out_suffix_len|. It returns true on success and false on error.
877 // |in_len| and |extra_in_len| should equal the argument of the same names
878 // passed to |SealScatter|.
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700879 bool SuffixLen(size_t *out_suffix_len, size_t in_len,
880 size_t extra_in_len) const;
David Benjamincfc11c22017-07-18 22:45:18 -0400881
David Benjamine2ab21d2018-04-04 23:55:06 -0400882 // CiphertextLen calculates the total ciphertext length written by
883 // |SealScatter| and writes it to |*out_len|. It returns true on success and
884 // false on error. |in_len| and |extra_in_len| should equal the argument of
885 // the same names passed to |SealScatter|.
886 bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const;
887
David Benjaminc64d1232017-10-04 18:14:28 -0400888 // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
889 // to the plaintext in |in| and returns true. Otherwise, it returns
890 // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
891 bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
David Benjamin32013e82022-09-22 16:55:34 -0400892 uint64_t seqnum, Span<const uint8_t> header, Span<uint8_t> in);
David Benjamincfc11c22017-07-18 22:45:18 -0400893
David Benjaminc11ea9422017-08-29 16:33:21 -0400894 // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
895 // result to |out|. It returns true on success and false on error.
896 //
897 // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
David Benjamincfc11c22017-07-18 22:45:18 -0400898 bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
David Benjamin32013e82022-09-22 16:55:34 -0400899 uint16_t record_version, uint64_t seqnum,
David Benjamine2ab21d2018-04-04 23:55:06 -0400900 Span<const uint8_t> header, const uint8_t *in, size_t in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400901
David Benjaminc11ea9422017-08-29 16:33:21 -0400902 // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
903 // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
904 // success and zero on error.
905 //
906 // On successful return, exactly |ExplicitNonceLen| bytes are written to
907 // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
908 // |out_suffix|.
909 //
910 // |extra_in| may point to an additional plaintext buffer. If present,
911 // |extra_in_len| additional bytes are encrypted and authenticated, and the
912 // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
913 // be used to size |out_suffix| accordingly.
914 //
915 // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
916 // alias anything.
David Benjamincfc11c22017-07-18 22:45:18 -0400917 bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
David Benjamin32013e82022-09-22 16:55:34 -0400918 uint8_t type, uint16_t record_version, uint64_t seqnum,
919 Span<const uint8_t> header, const uint8_t *in, size_t in_len,
920 const uint8_t *extra_in, size_t extra_in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400921
922 bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
923
924 private:
David Benjamine2ab21d2018-04-04 23:55:06 -0400925 // GetAdditionalData returns the additional data, writing into |storage| if
926 // necessary.
927 Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type,
928 uint16_t record_version,
David Benjamin32013e82022-09-22 16:55:34 -0400929 uint64_t seqnum, size_t plaintext_len,
David Benjamine2ab21d2018-04-04 23:55:06 -0400930 Span<const uint8_t> header);
David Benjamincfc11c22017-07-18 22:45:18 -0400931
932 const SSL_CIPHER *cipher_;
933 ScopedEVP_AEAD_CTX ctx_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400934 // fixed_nonce_ contains any bytes of the nonce that are fixed for all
935 // records.
David Benjamincfc11c22017-07-18 22:45:18 -0400936 uint8_t fixed_nonce_[12];
937 uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400938 // version_ is the wire version that should be used with this AEAD.
David Benjamincfc11c22017-07-18 22:45:18 -0400939 uint16_t version_;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400940 // is_dtls_ is whether DTLS is being used with this AEAD.
941 bool is_dtls_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400942 // variable_nonce_included_in_record_ is true if the variable nonce
943 // for a record is included as a prefix before the ciphertext.
David Benjamincfc11c22017-07-18 22:45:18 -0400944 bool variable_nonce_included_in_record_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400945 // random_variable_nonce_ is true if the variable nonce is
946 // randomly generated, rather than derived from the sequence
947 // number.
David Benjamincfc11c22017-07-18 22:45:18 -0400948 bool random_variable_nonce_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400949 // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
950 // variable nonce rather than prepended.
951 bool xor_fixed_nonce_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400952 // omit_length_in_ad_ is true if the length should be omitted in the
953 // AEAD's ad parameter.
David Benjamincfc11c22017-07-18 22:45:18 -0400954 bool omit_length_in_ad_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400955 // ad_is_header_ is true if the AEAD's ad parameter is the record header.
956 bool ad_is_header_ : 1;
David Benjamin86e95b82017-07-18 16:34:25 -0400957};
David Benjamin31a07792015-03-03 14:20:26 -0500958
David Benjamin31a07792015-03-03 14:20:26 -0500959
David Benjaminc11ea9422017-08-29 16:33:21 -0400960// DTLS replay bitmap.
David Benjamin7446a3b2015-07-25 17:53:57 -0400961
David Benjaminc11ea9422017-08-29 16:33:21 -0400962// DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
963// replayed packets. It should be initialized by zeroing every field.
David Benjamin86e95b82017-07-18 16:34:25 -0400964struct DTLS1_BITMAP {
David Benjaminc11ea9422017-08-29 16:33:21 -0400965 // map is a bit mask of the last 64 sequence numbers. Bit
966 // |1<<i| corresponds to |max_seq_num - i|.
David Benjamin800046f2017-10-17 01:37:51 -0400967 uint64_t map = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -0400968 // max_seq_num is the largest sequence number seen so far as a 64-bit
969 // integer.
David Benjamin800046f2017-10-17 01:37:51 -0400970 uint64_t max_seq_num = 0;
David Benjamin86e95b82017-07-18 16:34:25 -0400971};
David Benjamin7446a3b2015-07-25 17:53:57 -0400972
973
David Benjaminc11ea9422017-08-29 16:33:21 -0400974// Record layer.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400975
David Benjaminc11ea9422017-08-29 16:33:21 -0400976// ssl_record_prefix_len returns the length of the prefix before the ciphertext
977// of a record for |ssl|.
978//
979// TODO(davidben): Expose this as part of public API once the high-level
980// buffer-free APIs are available.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400981size_t ssl_record_prefix_len(const SSL *ssl);
982
983enum ssl_open_record_t {
984 ssl_open_record_success,
985 ssl_open_record_discard,
986 ssl_open_record_partial,
David Benjamin728f3542016-06-02 15:42:01 -0400987 ssl_open_record_close_notify,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400988 ssl_open_record_error,
989};
990
David Benjaminc11ea9422017-08-29 16:33:21 -0400991// tls_open_record decrypts a record from |in| in-place.
992//
993// If the input did not contain a complete record, it returns
994// |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
995// bytes necessary. It is guaranteed that a successful call to |tls_open_record|
996// will consume at least that many bytes.
997//
998// Otherwise, it sets |*out_consumed| to the number of bytes of input
999// consumed. Note that input may be consumed on all return codes if a record was
1000// decrypted.
1001//
1002// On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
1003// record type and |*out| to the record body in |in|. Note that |*out| may be
1004// empty.
1005//
1006// If a record was successfully processed but should be discarded, it returns
1007// |ssl_open_record_discard|.
1008//
David Benjamin751d1a12017-09-25 13:37:30 -04001009// If a record was successfully processed but is a close_notify, it returns
1010// |ssl_open_record_close_notify|.
David Benjaminc11ea9422017-08-29 16:33:21 -04001011//
David Benjamin751d1a12017-09-25 13:37:30 -04001012// On failure or fatal alert, it returns |ssl_open_record_error| and sets
1013// |*out_alert| to an alert to emit, or zero if no alert should be emitted.
David Benjaminc64d1232017-10-04 18:14:28 -04001014enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
1015 Span<uint8_t> *out, size_t *out_consumed,
1016 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -04001017
David Benjamina05d4272017-10-06 19:34:55 -04001018// dtls_open_record implements |tls_open_record| for DTLS. It only returns
1019// |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
1020// zero. The caller should read one packet and try again.
David Benjaminc64d1232017-10-04 18:14:28 -04001021enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
1022 Span<uint8_t> *out,
David Benjamina7810c12016-06-06 18:54:51 -04001023 size_t *out_consumed,
David Benjaminc64d1232017-10-04 18:14:28 -04001024 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -04001025
David Benjaminc11ea9422017-08-29 16:33:21 -04001026// ssl_seal_align_prefix_len returns the length of the prefix before the start
1027// of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
1028// use this to align buffers.
1029//
1030// Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
1031// record and is the offset into second record's ciphertext. Thus sealing a
1032// small record may result in a smaller output than this value.
1033//
1034// TODO(davidben): Is this alignment valuable? Record-splitting makes this a
1035// mess.
David Benjamin1a01e1f2016-06-08 18:31:24 -04001036size_t ssl_seal_align_prefix_len(const SSL *ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -04001037
David Benjaminc11ea9422017-08-29 16:33:21 -04001038// tls_seal_record seals a new record of type |type| and body |in| and writes it
David Benjamin8525ff32018-09-05 18:44:15 -05001039// to |out|. At most |max_out| bytes will be written. It returns true on success
1040// and false on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC
1041// 1/n-1 record splitting and may write two records concatenated.
David Benjaminc11ea9422017-08-29 16:33:21 -04001042//
1043// For a large record, the bulk of the ciphertext will begin
1044// |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
1045// improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
1046// bytes to |out|.
1047//
1048// |in| and |out| may not alias.
David Benjamin8525ff32018-09-05 18:44:15 -05001049bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
1050 uint8_t type, const uint8_t *in, size_t in_len);
David Benjaminb8d28cf2015-07-28 21:34:45 -04001051
1052enum dtls1_use_epoch_t {
1053 dtls1_use_previous_epoch,
1054 dtls1_use_current_epoch,
1055};
1056
David Benjaminc11ea9422017-08-29 16:33:21 -04001057// dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
1058// record.
David Benjamin1a999cf2017-01-03 10:30:35 -05001059size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
1060
David Benjaminc11ea9422017-08-29 16:33:21 -04001061// dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
1062// front of the plaintext when sealing a record in-place.
David Benjamina772b162017-01-24 17:51:33 -05001063size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
1064
David Benjaminc11ea9422017-08-29 16:33:21 -04001065// dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
1066// which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
1067// may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
1068// ahead of |out|.
David Benjamin8525ff32018-09-05 18:44:15 -05001069bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
1070 uint8_t type, const uint8_t *in, size_t in_len,
1071 enum dtls1_use_epoch_t use_epoch);
David Benjaminb8d28cf2015-07-28 21:34:45 -04001072
David Benjaminc11ea9422017-08-29 16:33:21 -04001073// ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
1074// state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
1075// |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
1076// appropriate.
David Benjamin728f3542016-06-02 15:42:01 -04001077enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
David Benjaminc64d1232017-10-04 18:14:28 -04001078 Span<const uint8_t> in);
David Benjamin728f3542016-06-02 15:42:01 -04001079
David Benjaminb8d28cf2015-07-28 21:34:45 -04001080
David Benjaminc11ea9422017-08-29 16:33:21 -04001081// Private key operations.
David Benjaminb4d65fd2015-05-29 17:11:21 -04001082
Christopher Patton9cde8482018-07-17 11:36:36 -07001083// ssl_has_private_key returns whether |hs| has a private key configured.
1084bool ssl_has_private_key(const SSL_HANDSHAKE *hs);
nagendra modadugu601448a2015-07-24 09:31:31 -07001085
David Benjaminc11ea9422017-08-29 16:33:21 -04001086// ssl_private_key_* perform the corresponding operation on
1087// |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
1088// call the corresponding function or |complete| depending on whether there is a
1089// pending operation. Otherwise, they implement the operation with
1090// |EVP_PKEY|.
David Benjaminb4d65fd2015-05-29 17:11:21 -04001091
David Benjaminb4d65fd2015-05-29 17:11:21 -04001092enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -04001093 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin75a1f232017-10-11 17:19:19 -04001094 uint16_t sigalg, Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -04001095
David Benjamin75a1f232017-10-11 17:19:19 -04001096enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
1097 uint8_t *out,
1098 size_t *out_len,
1099 size_t max_out,
1100 Span<const uint8_t> in);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001101
David Benjamin75a1f232017-10-11 17:19:19 -04001102// ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
1103// key supports |sigalg|.
1104bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
Jesse Selover6b0d8222018-08-14 12:47:29 -04001105 uint16_t sigalg);
David Benjamin1fb125c2016-07-08 18:52:12 -07001106
David Benjaminc11ea9422017-08-29 16:33:21 -04001107// ssl_public_key_verify verifies that the |signature| is valid for the public
1108// key |pkey| and input |in|, using the signature algorithm |sigalg|.
David Benjamin75a1f232017-10-11 17:19:19 -04001109bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
1110 uint16_t sigalg, EVP_PKEY *pkey,
1111 Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -04001112
David Benjamin1fb125c2016-07-08 18:52:12 -07001113
David Benjaminc11ea9422017-08-29 16:33:21 -04001114// Key shares.
David Benjamin4298d772015-12-19 00:18:25 -05001115
David Benjamin08b1f382023-02-28 17:22:23 -05001116// SSLKeyShare abstracts over KEM-like constructions, for use with TLS 1.2 ECDHE
1117// cipher suites and the TLS 1.3 key_share extension.
1118//
1119// TODO(davidben): This class is named SSLKeyShare after the TLS 1.3 key_share
1120// extension, but it really implements a KEM abstraction. Additionally, we use
1121// the same type for Encap, which is a one-off, stateless operation, as Generate
1122// and Decap. Slightly tidier would be for Generate to return a new SSLKEMKey
1123// (or we introduce EVP_KEM and EVP_KEM_KEY), with a Decap method, and for Encap
1124// to be static function.
David Benjaminc642aca2017-07-19 23:28:43 -04001125class SSLKeyShare {
1126 public:
1127 virtual ~SSLKeyShare() {}
David Benjamin9a892502017-07-25 22:26:44 -04001128 static constexpr bool kAllowUniquePtr = true;
David Benjaminc9376992017-07-24 15:31:13 -04001129 HAS_VIRTUAL_DESTRUCTOR
David Benjaminb9179092016-10-26 13:47:33 -04001130
David Benjaminc11ea9422017-08-29 16:33:21 -04001131 // Create returns a SSLKeyShare instance for use with group |group_id| or
1132 // nullptr on error.
David Benjaminc642aca2017-07-19 23:28:43 -04001133 static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
David Benjamin4298d772015-12-19 00:18:25 -05001134
David Benjaminc11ea9422017-08-29 16:33:21 -04001135 // GroupID returns the group ID.
David Benjaminc9376992017-07-24 15:31:13 -04001136 virtual uint16_t GroupID() const PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -05001137
David Benjamin08b1f382023-02-28 17:22:23 -05001138 // Generate generates a keypair and writes the public key to |out_public_key|.
1139 // It returns true on success and false on error.
1140 virtual bool Generate(CBB *out_public_key) PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -05001141
David Benjamin08b1f382023-02-28 17:22:23 -05001142 // Encap generates an ephemeral, symmetric secret and encapsulates it with
1143 // |peer_key|. On success, it returns true, writes the encapsulated secret to
1144 // |out_ciphertext|, and sets |*out_secret| to the shared secret. On failure,
1145 // it returns false and sets |*out_alert| to an alert to send to the peer.
1146 virtual bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret,
1147 uint8_t *out_alert,
1148 Span<const uint8_t> peer_key) PURE_VIRTUAL;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001149
David Benjamin08b1f382023-02-28 17:22:23 -05001150 // Decap decapsulates the symmetric secret in |ciphertext|. On success, it
1151 // returns true and sets |*out_secret| to the shared secret. On failure, it
1152 // returns false and sets |*out_alert| to an alert to send to the peer.
1153 virtual bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert,
1154 Span<const uint8_t> ciphertext) PURE_VIRTUAL;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001155
David Benjamin0a6c3fc2021-03-29 16:11:12 -04001156 // SerializePrivateKey writes the private key to |out|, returning true if
David Benjamin08b1f382023-02-28 17:22:23 -05001157 // successful and false otherwise. It should be called after |Generate|.
David Benjamin0a6c3fc2021-03-29 16:11:12 -04001158 virtual bool SerializePrivateKey(CBB *out) { return false; }
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001159
David Benjamin0a6c3fc2021-03-29 16:11:12 -04001160 // DeserializePrivateKey initializes the state of the key exchange from |in|,
1161 // returning true if successful and false otherwise.
1162 virtual bool DeserializePrivateKey(CBS *in) { return false; }
David Benjaminb9179092016-10-26 13:47:33 -04001163};
David Benjamin4298d772015-12-19 00:18:25 -05001164
Matthew Braithwaitec65eb2c2018-11-02 17:29:35 -07001165struct NamedGroup {
1166 int nid;
1167 uint16_t group_id;
David Benjamin1b724a62023-05-03 17:58:01 -04001168 const char name[32], alias[32];
Matthew Braithwaitec65eb2c2018-11-02 17:29:35 -07001169};
1170
1171// NamedGroups returns all supported groups.
1172Span<const NamedGroup> NamedGroups();
1173
David Benjaminc11ea9422017-08-29 16:33:21 -04001174// ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
David Benjamin8525ff32018-09-05 18:44:15 -05001175// sets |*out_group_id| to the group ID and returns true. Otherwise, it returns
1176// false.
1177bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
David Benjamin4298d772015-12-19 00:18:25 -05001178
David Benjamin8525ff32018-09-05 18:44:15 -05001179// ssl_name_to_group_id looks up the group corresponding to the |name| string of
1180// length |len|. On success, it sets |*out_group_id| to the group ID and returns
1181// true. Otherwise, it returns false.
1182bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001183
David Benjamin6cf98202023-05-26 22:03:30 -04001184// ssl_group_id_to_nid returns the NID corresponding to |group_id| or
1185// |NID_undef| if unknown.
1186int ssl_group_id_to_nid(uint16_t group_id);
1187
David Benjamin4298d772015-12-19 00:18:25 -05001188
David Benjaminc11ea9422017-08-29 16:33:21 -04001189// Handshake messages.
David Benjamin060cfb02016-05-12 00:43:05 -04001190
David Benjamin7934f082017-08-01 16:32:25 -04001191struct SSLMessage {
1192 bool is_v2_hello;
1193 uint8_t type;
1194 CBS body;
David Benjaminc11ea9422017-08-29 16:33:21 -04001195 // raw is the entire serialized handshake message, including the TLS or DTLS
1196 // message header.
David Benjamin7934f082017-08-01 16:32:25 -04001197 CBS raw;
1198};
1199
David Benjaminc11ea9422017-08-29 16:33:21 -04001200// SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
1201// ChangeCipherSpec, in the longest handshake flight. Currently this is the
1202// client's second leg in a full handshake when client certificates, NPN, and
1203// Channel ID, are all enabled.
David Benjamin29a83c52016-06-17 19:12:54 -04001204#define SSL_MAX_HANDSHAKE_FLIGHT 7
1205
Steven Valdez964b2372017-11-07 17:09:52 -05001206extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE];
Steven Valdezf1af1292018-08-13 10:54:48 -04001207extern const uint8_t kTLS12DowngradeRandom[8];
1208extern const uint8_t kTLS13DowngradeRandom[8];
David Benjamin6965d252018-11-19 15:49:56 -06001209extern const uint8_t kJDK11DowngradeRandom[8];
Steven Valdez964b2372017-11-07 17:09:52 -05001210
David Benjaminc11ea9422017-08-29 16:33:21 -04001211// ssl_max_handshake_message_len returns the maximum number of bytes permitted
1212// in a handshake message for |ssl|.
David Benjamin060cfb02016-05-12 00:43:05 -04001213size_t ssl_max_handshake_message_len(const SSL *ssl);
1214
David Benjamin33febf62017-10-07 16:52:50 -04001215// tls_can_accept_handshake_data returns whether |ssl| is able to accept more
1216// data into handshake buffer.
1217bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
1218
David Benjamin40e94702017-10-06 18:26:36 -04001219// tls_has_unprocessed_handshake_data returns whether there is buffered
1220// handshake data that has not been consumed by |get_message|.
1221bool tls_has_unprocessed_handshake_data(const SSL *ssl);
1222
David Benjamin7d10ab52018-11-02 18:31:17 -05001223// tls_append_handshake_data appends |data| to the handshake buffer. It returns
1224// true on success and false on allocation failure.
1225bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data);
1226
David Benjamin40e94702017-10-06 18:26:36 -04001227// dtls_has_unprocessed_handshake_data behaves like
1228// |tls_has_unprocessed_handshake_data| for DTLS.
1229bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
David Benjamin61672812016-07-14 23:10:43 -04001230
David Benjamin700631b2018-05-24 17:17:34 -04001231// tls_flush_pending_hs_data flushes any handshake plaintext data.
1232bool tls_flush_pending_hs_data(SSL *ssl);
1233
David Benjamin86e95b82017-07-18 16:34:25 -04001234struct DTLS_OUTGOING_MESSAGE {
David Benjamin049fdfc2017-10-17 01:12:53 -04001235 DTLS_OUTGOING_MESSAGE() {}
1236 DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
1237 DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
David Benjamin049fdfc2017-10-17 01:12:53 -04001238
1239 void Clear();
1240
David Benjamin46af2432022-09-04 15:12:57 -04001241 Array<uint8_t> data;
David Benjamin049fdfc2017-10-17 01:12:53 -04001242 uint16_t epoch = 0;
1243 bool is_ccs = false;
David Benjamin86e95b82017-07-18 16:34:25 -04001244};
David Benjamin29a83c52016-06-17 19:12:54 -04001245
David Benjaminc11ea9422017-08-29 16:33:21 -04001246// dtls_clear_outgoing_messages releases all buffered outgoing messages.
David Benjamin29a83c52016-06-17 19:12:54 -04001247void dtls_clear_outgoing_messages(SSL *ssl);
1248
David Benjamin060cfb02016-05-12 00:43:05 -04001249
David Benjaminc11ea9422017-08-29 16:33:21 -04001250// Callbacks.
David Benjamin4e9cc712016-06-01 20:16:03 -04001251
David Benjaminc11ea9422017-08-29 16:33:21 -04001252// ssl_do_info_callback calls |ssl|'s info callback, if set.
David Benjamin4e9cc712016-06-01 20:16:03 -04001253void ssl_do_info_callback(const SSL *ssl, int type, int value);
1254
David Benjaminc11ea9422017-08-29 16:33:21 -04001255// ssl_do_msg_callback calls |ssl|'s message callback, if set.
Adam Langleyc9827e02019-04-12 14:46:50 -07001256void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
David Benjaminc64d1232017-10-04 18:14:28 -04001257 Span<const uint8_t> in);
David Benjamin4e9cc712016-06-01 20:16:03 -04001258
1259
David Benjaminc11ea9422017-08-29 16:33:21 -04001260// Transport buffers.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001261
David Benjaminea712e32017-10-13 16:50:39 -04001262class SSLBuffer {
1263 public:
1264 SSLBuffer() {}
1265 ~SSLBuffer() { Clear(); }
1266
1267 SSLBuffer(const SSLBuffer &) = delete;
1268 SSLBuffer &operator=(const SSLBuffer &) = delete;
1269
1270 uint8_t *data() { return buf_ + offset_; }
1271 size_t size() const { return size_; }
1272 bool empty() const { return size_ == 0; }
1273 size_t cap() const { return cap_; }
1274
1275 Span<uint8_t> span() { return MakeSpan(data(), size()); }
1276
1277 Span<uint8_t> remaining() {
1278 return MakeSpan(data() + size(), cap() - size());
1279 }
1280
1281 // Clear releases the buffer.
1282 void Clear();
1283
1284 // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
1285 // that data written after |header_len| is aligned to a
1286 // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
1287 // on error.
1288 bool EnsureCap(size_t header_len, size_t new_cap);
1289
1290 // DidWrite extends the buffer by |len|. The caller must have filled in to
1291 // this point.
1292 void DidWrite(size_t len);
1293
1294 // Consume consumes |len| bytes from the front of the buffer. The memory
1295 // consumed will remain valid until the next call to |DiscardConsumed| or
1296 // |Clear|.
1297 void Consume(size_t len);
1298
1299 // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
1300 // is now empty, it releases memory used by it.
1301 void DiscardConsumed();
1302
1303 private:
1304 // buf_ is the memory allocated for this buffer.
1305 uint8_t *buf_ = nullptr;
1306 // offset_ is the offset into |buf_| which the buffer contents start at.
1307 uint16_t offset_ = 0;
1308 // size_ is the size of the buffer contents from |buf_| + |offset_|.
1309 uint16_t size_ = 0;
1310 // cap_ is how much memory beyond |buf_| + |offset_| is available.
1311 uint16_t cap_ = 0;
David Benjaminc7337542019-11-28 08:32:16 -05001312 // inline_buf_ is a static buffer for short reads.
1313 uint8_t inline_buf_[SSL3_RT_HEADER_LENGTH];
1314 // buf_allocated_ is true if |buf_| points to allocated data and must be freed
1315 // or false if it points into |inline_buf_|.
1316 bool buf_allocated_ = false;
David Benjaminea712e32017-10-13 16:50:39 -04001317};
David Benjaminb8d28cf2015-07-28 21:34:45 -04001318
David Benjaminc11ea9422017-08-29 16:33:21 -04001319// ssl_read_buffer_extend_to extends the read buffer to the desired length. For
1320// TLS, it reads to the end of the buffer until the buffer is |len| bytes
1321// long. For DTLS, it reads a new packet and ignores |len|. It returns one on
1322// success, zero on EOF, and a negative number on error.
1323//
1324// It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
1325// non-empty.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001326int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
1327
David Benjaminea712e32017-10-13 16:50:39 -04001328// ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
1329// to a record-processing function. If |ret| is a success or if the caller
1330// should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
1331// 0.
David Benjamind9229f92017-10-06 17:36:20 -04001332int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
1333 size_t consumed, uint8_t alert);
1334
David Benjaminc11ea9422017-08-29 16:33:21 -04001335// ssl_write_buffer_flush flushes the write buffer to the transport. It returns
1336// one on success and <= 0 on error. For DTLS, whether or not the write
1337// succeeds, the write buffer will be cleared.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001338int ssl_write_buffer_flush(SSL *ssl);
1339
David Benjaminb8d28cf2015-07-28 21:34:45 -04001340
David Benjaminc11ea9422017-08-29 16:33:21 -04001341// Certificate functions.
David Benjamin75836432016-06-17 18:48:29 -04001342
David Benjamin8525ff32018-09-05 18:44:15 -05001343// ssl_has_certificate returns whether a certificate and private key are
1344// configured.
Christopher Patton9cde8482018-07-17 11:36:36 -07001345bool ssl_has_certificate(const SSL_HANDSHAKE *hs);
David Benjamin32a66d52016-07-13 22:03:11 -04001346
David Benjaminc11ea9422017-08-29 16:33:21 -04001347// ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
1348// by a TLS Certificate message. On success, it advances |cbs| and returns
1349// true. Otherwise, it returns false and sets |*out_alert| to an alert to send
1350// to the peer.
1351//
1352// If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
1353// the certificate chain and the leaf certificate's public key
1354// respectively. Otherwise, both will be set to nullptr.
1355//
1356// If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
1357// SHA-256 hash of the leaf to |out_leaf_sha256|.
David Benjamine664a532017-07-20 20:19:36 -04001358bool ssl_parse_cert_chain(uint8_t *out_alert,
1359 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
1360 UniquePtr<EVP_PKEY> *out_pubkey,
1361 uint8_t *out_leaf_sha256, CBS *cbs,
1362 CRYPTO_BUFFER_POOL *pool);
David Benjamin5c900c82016-07-13 23:03:26 -04001363
Jesse Seloverd7266ec2019-01-30 16:06:10 -05001364enum ssl_key_usage_t {
1365 key_usage_digital_signature = 0,
1366 key_usage_encipherment = 2,
1367};
1368
1369// ssl_cert_check_key_usage parses the DER-encoded, X.509 certificate in |in|
1370// and returns true if doesn't specify a key usage or, if it does, if it
1371// includes |bit|. Otherwise it pushes to the error queue and returns false.
David Benjamina1dffbf2022-10-25 16:29:43 -04001372OPENSSL_EXPORT bool ssl_cert_check_key_usage(const CBS *in,
1373 enum ssl_key_usage_t bit);
Adam Langley05672202016-12-13 12:05:49 -08001374
David Benjaminc11ea9422017-08-29 16:33:21 -04001375// ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
1376// certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
1377// nullptr and pushes to the error queue.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001378UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
Adam Langleyd5157222016-12-12 11:37:43 -08001379
David Benjaminc11ea9422017-08-29 16:33:21 -04001380// ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
1381// TLS CertificateRequest message. On success, it returns a newly-allocated
1382// |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
1383// sets |*out_alert| to an alert to send to the peer.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001384UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
1385 uint8_t *out_alert,
1386 CBS *cbs);
David Benjamine0332e82016-07-13 22:40:36 -04001387
Steven Valdezcd8470f2017-10-11 12:29:36 -04001388// ssl_has_client_CAs returns there are configured CAs.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001389bool ssl_has_client_CAs(const SSL_CONFIG *cfg);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001390
David Benjaminc11ea9422017-08-29 16:33:21 -04001391// ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
David Benjamin8525ff32018-09-05 18:44:15 -05001392// used by a TLS CertificateRequest message. It returns true on success and
1393// false on error.
1394bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjamin32a66d52016-07-13 22:03:11 -04001395
David Benjaminc11ea9422017-08-29 16:33:21 -04001396// ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
1397// a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
1398// an error on the error queue.
David Benjamin8525ff32018-09-05 18:44:15 -05001399bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Adam Langley05672202016-12-13 12:05:49 -08001400 const CRYPTO_BUFFER *leaf);
Steven Valdezbf5aa842016-07-15 07:07:40 -04001401
David Benjaminc11ea9422017-08-29 16:33:21 -04001402// ssl_on_certificate_selected is called once the certificate has been selected.
1403// It finalizes the certificate and initializes |hs->local_pubkey|. It returns
David Benjamin8525ff32018-09-05 18:44:15 -05001404// true on success and false on error.
1405bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
David Benjamina232a712017-03-30 15:51:53 -05001406
David Benjamin75836432016-06-17 18:48:29 -04001407
David Benjaminc11ea9422017-08-29 16:33:21 -04001408// TLS 1.3 key derivation.
Steven Valdez143e8b32016-07-11 13:19:03 -04001409
David Benjaminc11ea9422017-08-29 16:33:21 -04001410// tls13_init_key_schedule initializes the handshake hash and key derivation
Steven Valdezcd8470f2017-10-11 12:29:36 -04001411// state, and incorporates the PSK. The cipher suite and PRF hash must have been
David Benjamin8525ff32018-09-05 18:44:15 -05001412// selected at this point. It returns true on success and false on error.
David Benjamin79b8b3a2019-08-16 18:58:13 -04001413bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk);
Steven Valdez143e8b32016-07-11 13:19:03 -04001414
David Benjaminc11ea9422017-08-29 16:33:21 -04001415// tls13_init_early_key_schedule initializes the handshake hash and key
David Benjamin83a49932021-05-20 15:57:09 -04001416// derivation state from |session| for use with 0-RTT. It returns one on success
1417// and zero on error.
1418bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs,
1419 const SSL_SESSION *session);
Steven Valdez2d850622017-01-11 11:34:52 -05001420
David Benjaminc11ea9422017-08-29 16:33:21 -04001421// tls13_advance_key_schedule incorporates |in| into the key schedule with
David Benjamin8525ff32018-09-05 18:44:15 -05001422// HKDF-Extract. It returns true on success and false on error.
David Benjamin79b8b3a2019-08-16 18:58:13 -04001423bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in);
Steven Valdez143e8b32016-07-11 13:19:03 -04001424
David Benjaminc11ea9422017-08-29 16:33:21 -04001425// tls13_set_traffic_key sets the read or write traffic keys to
David Benjamin754d4c92020-02-11 16:27:21 -05001426// |traffic_secret|. The version and cipher suite are determined from |session|.
1427// It returns true on success and false on error.
Steven Valdezc8e0f902018-07-14 11:23:01 -04001428bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level,
1429 enum evp_aead_direction_t direction,
David Benjamin754d4c92020-02-11 16:27:21 -05001430 const SSL_SESSION *session,
David Benjamin79b8b3a2019-08-16 18:58:13 -04001431 Span<const uint8_t> traffic_secret);
Steven Valdez143e8b32016-07-11 13:19:03 -04001432
David Benjamind6343572019-08-15 17:29:02 -04001433// tls13_derive_early_secret derives the early traffic secret. It returns true
David Benjamin1e859052020-02-09 16:04:58 -05001434// on success and false on error.
David Benjamind6343572019-08-15 17:29:02 -04001435bool tls13_derive_early_secret(SSL_HANDSHAKE *hs);
1436
David Benjaminc11ea9422017-08-29 16:33:21 -04001437// tls13_derive_handshake_secrets derives the handshake traffic secret. It
David Benjamin8525ff32018-09-05 18:44:15 -05001438// returns true on success and false on error.
1439bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001440
David Benjaminc11ea9422017-08-29 16:33:21 -04001441// tls13_rotate_traffic_key derives the next read or write traffic secret. It
David Benjamin8525ff32018-09-05 18:44:15 -05001442// returns true on success and false on error.
1443bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
Steven Valdez1dc53d22016-07-26 12:27:38 -04001444
David Benjaminc11ea9422017-08-29 16:33:21 -04001445// tls13_derive_application_secrets derives the initial application data traffic
1446// and exporter secrets based on the handshake transcripts and |master_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001447// It returns true on success and false on error.
1448bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001449
David Benjaminc11ea9422017-08-29 16:33:21 -04001450// tls13_derive_resumption_secret derives the |resumption_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001451bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001452
David Benjaminc11ea9422017-08-29 16:33:21 -04001453// tls13_export_keying_material provides an exporter interface to use the
1454// |exporter_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001455bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
1456 Span<const uint8_t> secret,
1457 Span<const char> label,
1458 Span<const uint8_t> context);
Steven Valdez143e8b32016-07-11 13:19:03 -04001459
David Benjaminc11ea9422017-08-29 16:33:21 -04001460// tls13_finished_mac calculates the MAC of the handshake transcript to verify
1461// the integrity of the Finished message, and stores the result in |out| and
David Benjamin8525ff32018-09-05 18:44:15 -05001462// length in |out_len|. |is_server| is true if this is for the Server Finished
1463// and false for the Client Finished.
1464bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
1465 bool is_server);
Steven Valdez143e8b32016-07-11 13:19:03 -04001466
Steven Valdezcd8470f2017-10-11 12:29:36 -04001467// tls13_derive_session_psk calculates the PSK for this session based on the
1468// resumption master secret and |nonce|. It returns true on success, and false
1469// on failure.
Alessandro Ghedini2cc6f442018-12-11 11:35:17 +00001470bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001471
David Benjamin83a49932021-05-20 15:57:09 -04001472// tls13_write_psk_binder calculates the PSK binder value over |transcript| and
1473// |msg|, and replaces the last bytes of |msg| with the resulting value. It
1474// returns true on success, and false on failure. If |out_binder_len| is
1475// non-NULL, it sets |*out_binder_len| to the length of the value computed.
1476bool tls13_write_psk_binder(const SSL_HANDSHAKE *hs,
1477 const SSLTranscript &transcript, Span<uint8_t> msg,
1478 size_t *out_binder_len);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001479
David Benjamin8525ff32018-09-05 18:44:15 -05001480// tls13_verify_psk_binder verifies that the handshake transcript, truncated up
1481// to the binders has a valid signature using the value of |session|'s
1482// resumption secret. It returns true on success, and false on failure.
David Benjaminb32aa052021-06-02 17:05:00 -04001483bool tls13_verify_psk_binder(const SSL_HANDSHAKE *hs,
1484 const SSL_SESSION *session, const SSLMessage &msg,
1485 CBS *binders);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001486
Steven Valdez143e8b32016-07-11 13:19:03 -04001487
Daniel McArdle00e434d2021-02-18 11:47:18 -05001488// Encrypted ClientHello.
1489
David Benjamin83a49932021-05-20 15:57:09 -04001490struct ECHConfig {
1491 static constexpr bool kAllowUniquePtr = true;
1492 // raw contains the serialized ECHConfig.
1493 Array<uint8_t> raw;
1494 // The following fields alias into |raw|.
1495 Span<const uint8_t> public_key;
1496 Span<const uint8_t> public_name;
1497 Span<const uint8_t> cipher_suites;
1498 uint16_t kem_id = 0;
David Benjamin18b68362021-06-18 23:13:46 -04001499 uint8_t maximum_name_length = 0;
David Benjamin83a49932021-05-20 15:57:09 -04001500 uint8_t config_id = 0;
1501};
1502
Daniel McArdle00e434d2021-02-18 11:47:18 -05001503class ECHServerConfig {
1504 public:
David Benjamin1d58cd12021-05-04 15:24:24 -04001505 static constexpr bool kAllowUniquePtr = true;
David Benjamin83a49932021-05-20 15:57:09 -04001506 ECHServerConfig() = default;
David Benjamin1d58cd12021-05-04 15:24:24 -04001507 ECHServerConfig(const ECHServerConfig &other) = delete;
David Benjamin1d58cd12021-05-04 15:24:24 -04001508 ECHServerConfig &operator=(ECHServerConfig &&) = delete;
Daniel McArdle00e434d2021-02-18 11:47:18 -05001509
David Benjaminc890ae52021-06-06 13:32:29 -04001510 // Init parses |ech_config| as an ECHConfig and saves a copy of |key|.
1511 // It returns true on success and false on error.
1512 bool Init(Span<const uint8_t> ech_config, const EVP_HPKE_KEY *key,
Daniel McArdle00e434d2021-02-18 11:47:18 -05001513 bool is_retry_config);
1514
David Benjaminf39c81d2021-05-03 18:39:46 -04001515 // SetupContext sets up |ctx| for a new connection, given the specified
1516 // HPKE ciphersuite and encapsulated KEM key. It returns true on success and
1517 // false on error. This function may only be called on an initialized object.
1518 bool SetupContext(EVP_HPKE_CTX *ctx, uint16_t kdf_id, uint16_t aead_id,
1519 Span<const uint8_t> enc) const;
Daniel McArdle00e434d2021-02-18 11:47:18 -05001520
David Benjamin83a49932021-05-20 15:57:09 -04001521 const ECHConfig &ech_config() const { return ech_config_; }
1522 bool is_retry_config() const { return is_retry_config_; }
Daniel McArdle00e434d2021-02-18 11:47:18 -05001523
1524 private:
David Benjamin83a49932021-05-20 15:57:09 -04001525 ECHConfig ech_config_;
David Benjamin1d58cd12021-05-04 15:24:24 -04001526 ScopedEVP_HPKE_KEY key_;
David Benjamin83a49932021-05-20 15:57:09 -04001527 bool is_retry_config_ = false;
1528};
Daniel McArdle00e434d2021-02-18 11:47:18 -05001529
David Benjamin83a49932021-05-20 15:57:09 -04001530enum ssl_client_hello_type_t {
1531 ssl_client_hello_unencrypted,
1532 ssl_client_hello_inner,
1533 ssl_client_hello_outer,
Daniel McArdle00e434d2021-02-18 11:47:18 -05001534};
1535
David Benjamin18b68362021-06-18 23:13:46 -04001536// ECH_CLIENT_* are types for the ClientHello encrypted_client_hello extension.
1537#define ECH_CLIENT_OUTER 0
1538#define ECH_CLIENT_INNER 1
1539
Daniel McArdle00e434d2021-02-18 11:47:18 -05001540// ssl_decode_client_hello_inner recovers the full ClientHelloInner from the
1541// EncodedClientHelloInner |encoded_client_hello_inner| by replacing its
1542// outer_extensions extension with the referenced extensions from the
1543// ClientHelloOuter |client_hello_outer|. If successful, it writes the recovered
1544// ClientHelloInner to |out_client_hello_inner|. It returns true on success and
1545// false on failure.
David Benjamin44425dd2022-01-27 12:22:42 -05001546//
1547// This function is exported for fuzzing.
Daniel McArdle00e434d2021-02-18 11:47:18 -05001548OPENSSL_EXPORT bool ssl_decode_client_hello_inner(
1549 SSL *ssl, uint8_t *out_alert, Array<uint8_t> *out_client_hello_inner,
1550 Span<const uint8_t> encoded_client_hello_inner,
1551 const SSL_CLIENT_HELLO *client_hello_outer);
1552
David Benjamin44425dd2022-01-27 12:22:42 -05001553// ssl_client_hello_decrypt attempts to decrypt and decode the |payload|. It
1554// writes the result to |*out|. |payload| must point into |client_hello_outer|.
1555// It returns true on success and false on error. On error, it sets
1556// |*out_is_decrypt_error| to whether the failure was due to a bad ciphertext.
1557bool ssl_client_hello_decrypt(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1558 bool *out_is_decrypt_error, Array<uint8_t> *out,
Steven Valdez94a63a52021-04-29 10:52:42 -04001559 const SSL_CLIENT_HELLO *client_hello_outer,
Steven Valdez94a63a52021-04-29 10:52:42 -04001560 Span<const uint8_t> payload);
Dan McArdlec2959352020-10-29 14:31:31 -04001561
David Benjamin83a49932021-05-20 15:57:09 -04001562#define ECH_CONFIRMATION_SIGNAL_LEN 8
Dan McArdlec2959352020-10-29 14:31:31 -04001563
David Benjamin83a49932021-05-20 15:57:09 -04001564// ssl_ech_confirmation_signal_hello_offset returns the offset of the ECH
1565// confirmation signal in a ServerHello message, including the handshake header.
1566size_t ssl_ech_confirmation_signal_hello_offset(const SSL *ssl);
1567
1568// ssl_ech_accept_confirmation computes the server's ECH acceptance signal,
David Benjamin18b68362021-06-18 23:13:46 -04001569// writing it to |out|. The transcript portion is the concatenation of
1570// |transcript| with |msg|. The |ECH_CONFIRMATION_SIGNAL_LEN| bytes from
1571// |offset| in |msg| are replaced with zeros before hashing. This function
1572// returns true on success, and false on failure.
David Benjamin83a49932021-05-20 15:57:09 -04001573bool ssl_ech_accept_confirmation(const SSL_HANDSHAKE *hs, Span<uint8_t> out,
David Benjamin18b68362021-06-18 23:13:46 -04001574 Span<const uint8_t> client_random,
1575 const SSLTranscript &transcript, bool is_hrr,
1576 Span<const uint8_t> msg, size_t offset);
David Benjamin83a49932021-05-20 15:57:09 -04001577
David Benjamin9cbe7372021-06-15 18:09:10 -04001578// ssl_is_valid_ech_public_name returns true if |public_name| is a valid ECH
1579// public name and false otherwise. It is exported for testing.
1580OPENSSL_EXPORT bool ssl_is_valid_ech_public_name(
1581 Span<const uint8_t> public_name);
1582
David Benjamin83a49932021-05-20 15:57:09 -04001583// ssl_is_valid_ech_config_list returns true if |ech_config_list| is a valid
1584// ECHConfigList structure and false otherwise.
1585bool ssl_is_valid_ech_config_list(Span<const uint8_t> ech_config_list);
1586
1587// ssl_select_ech_config selects an ECHConfig and associated parameters to offer
1588// on the client and updates |hs|. It returns true on success, whether an
1589// ECHConfig was found or not, and false on internal error. On success, the
1590// encapsulated key is written to |out_enc| and |*out_enc_len| is set to the
1591// number of bytes written. If the function did not select an ECHConfig, the
1592// encapsulated key is the empty string.
1593bool ssl_select_ech_config(SSL_HANDSHAKE *hs, Span<uint8_t> out_enc,
1594 size_t *out_enc_len);
1595
1596// ssl_ech_extension_body_length returns the length of the body of a ClientHello
1597// ECH extension that encrypts |in_len| bytes with |aead| and an 'enc' value of
1598// length |enc_len|. The result does not include the four-byte extension header.
1599size_t ssl_ech_extension_body_length(const EVP_HPKE_AEAD *aead, size_t enc_len,
1600 size_t in_len);
1601
1602// ssl_encrypt_client_hello constructs a new ClientHelloInner, adds it to the
1603// inner transcript, and encrypts for inclusion in the ClientHelloOuter. |enc|
1604// is the encapsulated key to include in the extension. It returns true on
1605// success and false on error. If not offering ECH, |enc| is ignored and the
1606// function will compute a GREASE ECH extension if necessary, and otherwise
1607// return success while doing nothing.
1608//
1609// Encrypting the ClientHelloInner incorporates all extensions in the
1610// ClientHelloOuter, so all other state necessary for |ssl_add_client_hello|
1611// must already be computed.
1612bool ssl_encrypt_client_hello(SSL_HANDSHAKE *hs, Span<const uint8_t> enc);
David Benjamin246c5562021-05-20 13:42:25 -04001613
Dan McArdlec2959352020-10-29 14:31:31 -04001614
David Benjamin08b17292021-03-29 15:35:56 -04001615// Delegated credentials.
1616
David Benjamin48b0edf2024-02-11 17:14:41 -05001617// This structure stores a delegated credential (DC) as defined by RFC 9345.
David Benjamin08b17292021-03-29 15:35:56 -04001618struct DC {
1619 static constexpr bool kAllowUniquePtr = true;
1620 ~DC();
1621
1622 // Dup returns a copy of this DC and takes references to |raw| and |pkey|.
1623 UniquePtr<DC> Dup();
1624
1625 // Parse parses the delegated credential stored in |in|. If successful it
1626 // returns the parsed structure, otherwise it returns |nullptr| and sets
1627 // |*out_alert|.
1628 static UniquePtr<DC> Parse(CRYPTO_BUFFER *in, uint8_t *out_alert);
1629
David Benjamin48b0edf2024-02-11 17:14:41 -05001630 // raw is the delegated credential encoded as specified in RFC 9345.
David Benjamin08b17292021-03-29 15:35:56 -04001631 UniquePtr<CRYPTO_BUFFER> raw;
1632
David Benjaminefad2bf2024-02-23 14:37:10 -05001633 // dc_cert_verify_algorithm is the signature scheme of the DC public key. This
1634 // is used for the CertificateVerify message.
David Benjamin48b0edf2024-02-11 17:14:41 -05001635 uint16_t dc_cert_verify_algorithm = 0;
David Benjamin08b17292021-03-29 15:35:56 -04001636
David Benjaminefad2bf2024-02-23 14:37:10 -05001637 // algorithm is the signature scheme of the signature over the delegated
1638 // credential itself, made by the end-entity certificate's public key.
1639 uint16_t algorithm = 0;
1640
David Benjamin08b17292021-03-29 15:35:56 -04001641 // pkey is the public key parsed from |public_key|.
1642 UniquePtr<EVP_PKEY> pkey;
1643
1644 private:
1645 friend DC* New<DC>();
1646 DC();
1647};
1648
1649// ssl_signing_with_dc returns true if the peer has indicated support for
1650// delegated credentials and this host has sent a delegated credential in
1651// response. If this is true then we've committed to using the DC in the
1652// handshake.
1653bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs);
1654
1655
David Benjaminc11ea9422017-08-29 16:33:21 -04001656// Handshake functions.
Steven Valdez143e8b32016-07-11 13:19:03 -04001657
1658enum ssl_hs_wait_t {
1659 ssl_hs_error,
1660 ssl_hs_ok,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001661 ssl_hs_read_server_hello,
Steven Valdez143e8b32016-07-11 13:19:03 -04001662 ssl_hs_read_message,
Steven Valdez143e8b32016-07-11 13:19:03 -04001663 ssl_hs_flush,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001664 ssl_hs_certificate_selection_pending,
Adam Langley3fe8fa72018-01-26 09:14:30 -08001665 ssl_hs_handoff,
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001666 ssl_hs_handback,
Steven Valdez143e8b32016-07-11 13:19:03 -04001667 ssl_hs_x509_lookup,
1668 ssl_hs_private_key_operation,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001669 ssl_hs_pending_session,
David Benjamin707af292017-03-10 17:47:18 -05001670 ssl_hs_pending_ticket,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001671 ssl_hs_early_return,
Steven Valdeze831a812017-03-09 14:56:07 -05001672 ssl_hs_early_data_rejected,
Steven Valdez2d850622017-01-11 11:34:52 -05001673 ssl_hs_read_end_of_early_data,
Steven Valdez520e1222017-06-13 12:45:25 -04001674 ssl_hs_read_change_cipher_spec,
David Benjamin3a1dd462017-07-11 16:13:10 -04001675 ssl_hs_certificate_verify,
David Benjaminb571e772021-03-25 19:42:16 -04001676 ssl_hs_hints_ready,
Steven Valdez143e8b32016-07-11 13:19:03 -04001677};
1678
David Benjamina7bc9442018-01-18 10:08:53 -05001679enum ssl_grease_index_t {
1680 ssl_grease_cipher = 0,
1681 ssl_grease_group,
1682 ssl_grease_extension1,
1683 ssl_grease_extension2,
1684 ssl_grease_version,
1685 ssl_grease_ticket_extension,
David Benjamin43ab56c2021-05-20 10:25:01 -04001686 ssl_grease_ech_config_id,
1687 ssl_grease_last_index = ssl_grease_ech_config_id,
David Benjamina7bc9442018-01-18 10:08:53 -05001688};
1689
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001690enum tls12_server_hs_state_t {
1691 state12_start_accept = 0,
1692 state12_read_client_hello,
Daniel McArdle00e434d2021-02-18 11:47:18 -05001693 state12_read_client_hello_after_ech,
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001694 state12_select_certificate,
1695 state12_tls13,
1696 state12_select_parameters,
1697 state12_send_server_hello,
1698 state12_send_server_certificate,
1699 state12_send_server_key_exchange,
1700 state12_send_server_hello_done,
1701 state12_read_client_certificate,
1702 state12_verify_client_certificate,
1703 state12_read_client_key_exchange,
1704 state12_read_client_certificate_verify,
1705 state12_read_change_cipher_spec,
1706 state12_process_change_cipher_spec,
1707 state12_read_next_proto,
1708 state12_read_channel_id,
1709 state12_read_client_finished,
1710 state12_send_server_finished,
1711 state12_finish_server_handshake,
1712 state12_done,
1713};
1714
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001715enum tls13_server_hs_state_t {
1716 state13_select_parameters = 0,
1717 state13_select_session,
1718 state13_send_hello_retry_request,
1719 state13_read_second_client_hello,
1720 state13_send_server_hello,
1721 state13_send_server_certificate_verify,
1722 state13_send_server_finished,
Matthew Braithwaite093a8232020-01-28 14:06:55 -08001723 state13_send_half_rtt_ticket,
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001724 state13_read_second_client_flight,
1725 state13_process_end_of_early_data,
Steven Valdez51607f12020-08-05 10:46:05 -04001726 state13_read_client_encrypted_extensions,
Matthew Braithwaite08e1fe02019-11-26 17:49:04 -08001727 state13_read_client_certificate,
1728 state13_read_client_certificate_verify,
1729 state13_read_channel_id,
1730 state13_read_client_finished,
1731 state13_send_new_session_ticket,
1732 state13_done,
1733};
1734
Matthew Braithwaite3e2b3ee2018-05-10 15:46:42 -07001735// handback_t lists the points in the state machine where a handback can occur.
1736// These are the different points at which key material is no longer needed.
1737enum handback_t {
David Benjamin0c306492020-02-09 16:28:52 -05001738 handback_after_session_resumption = 0,
1739 handback_after_ecdhe = 1,
1740 handback_after_handshake = 2,
1741 handback_tls13 = 3,
1742 handback_max_value = handback_tls13,
Matthew Braithwaite3e2b3ee2018-05-10 15:46:42 -07001743};
1744
David Benjaminb571e772021-03-25 19:42:16 -04001745// SSL_HANDSHAKE_HINTS contains handshake hints for a connection. See
1746// |SSL_request_handshake_hints| and related functions.
1747struct SSL_HANDSHAKE_HINTS {
1748 static constexpr bool kAllowUniquePtr = true;
1749
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001750 Array<uint8_t> server_random_tls12;
1751 Array<uint8_t> server_random_tls13;
David Benjaminb571e772021-03-25 19:42:16 -04001752
1753 uint16_t key_share_group_id = 0;
David Benjamin08b1f382023-02-28 17:22:23 -05001754 Array<uint8_t> key_share_ciphertext;
David Benjaminb571e772021-03-25 19:42:16 -04001755 Array<uint8_t> key_share_secret;
1756
1757 uint16_t signature_algorithm = 0;
1758 Array<uint8_t> signature_input;
1759 Array<uint8_t> signature_spki;
1760 Array<uint8_t> signature;
1761
1762 Array<uint8_t> decrypted_psk;
1763 bool ignore_psk = false;
David Benjamin26f186b2021-06-08 19:17:58 -04001764
1765 uint16_t cert_compression_alg_id = 0;
1766 Array<uint8_t> cert_compression_input;
1767 Array<uint8_t> cert_compression_output;
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001768
1769 uint16_t ecdhe_group_id = 0;
1770 Array<uint8_t> ecdhe_public_key;
1771 Array<uint8_t> ecdhe_private_key;
David Benjaminadaa3222022-08-02 17:10:53 -07001772
1773 Array<uint8_t> decrypted_ticket;
1774 bool renew_ticket = false;
1775 bool ignore_ticket = false;
David Benjaminb571e772021-03-25 19:42:16 -04001776};
1777
David Benjamin86e95b82017-07-18 16:34:25 -04001778struct SSL_HANDSHAKE {
David Benjamin8f288862017-07-20 14:01:44 -04001779 explicit SSL_HANDSHAKE(SSL *ssl);
1780 ~SSL_HANDSHAKE();
David Benjamin9a892502017-07-25 22:26:44 -04001781 static constexpr bool kAllowUniquePtr = true;
David Benjamin8f288862017-07-20 14:01:44 -04001782
David Benjaminc11ea9422017-08-29 16:33:21 -04001783 // ssl is a non-owning pointer to the parent |SSL| object.
David Benjamince8c9d22016-11-14 10:45:16 +09001784 SSL *ssl;
1785
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001786 // config is a non-owning pointer to the handshake configuration.
1787 SSL_CONFIG *config;
1788
David Benjaminc11ea9422017-08-29 16:33:21 -04001789 // wait contains the operation the handshake is currently blocking on or
1790 // |ssl_hs_ok| if none.
David Benjamin8f288862017-07-20 14:01:44 -04001791 enum ssl_hs_wait_t wait = ssl_hs_ok;
Adam Langley4ba6e192016-12-07 15:54:54 -08001792
David Benjaminc11ea9422017-08-29 16:33:21 -04001793 // state is the internal state for the TLS 1.2 and below handshake. Its
1794 // values depend on |do_handshake| but the starting state is always zero.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001795 int state = 0;
David Benjamincb0c29f2016-12-12 17:00:50 -05001796
David Benjaminc11ea9422017-08-29 16:33:21 -04001797 // tls13_state is the internal state for the TLS 1.3 handshake. Its values
1798 // depend on |do_handshake| but the starting state is always zero.
David Benjamin8f288862017-07-20 14:01:44 -04001799 int tls13_state = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04001800
David Benjaminc11ea9422017-08-29 16:33:21 -04001801 // min_version is the minimum accepted protocol version, taking account both
1802 // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001803 uint16_t min_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001804
David Benjaminc11ea9422017-08-29 16:33:21 -04001805 // max_version is the maximum accepted protocol version, taking account both
1806 // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001807 uint16_t max_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001808
David Benjamine530ea32019-08-16 19:28:00 -04001809 private:
1810 size_t hash_len_ = 0;
1811 uint8_t secret_[SSL_MAX_MD_SIZE] = {0};
1812 uint8_t early_traffic_secret_[SSL_MAX_MD_SIZE] = {0};
1813 uint8_t client_handshake_secret_[SSL_MAX_MD_SIZE] = {0};
1814 uint8_t server_handshake_secret_[SSL_MAX_MD_SIZE] = {0};
1815 uint8_t client_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0};
1816 uint8_t server_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0};
1817 uint8_t expected_client_finished_[SSL_MAX_MD_SIZE] = {0};
1818
1819 public:
1820 void ResizeSecrets(size_t hash_len);
1821
Daniel McArdle00e434d2021-02-18 11:47:18 -05001822 // GetClientHello, on the server, returns either the normal ClientHello
1823 // message or the ClientHelloInner if it has been serialized to
1824 // |ech_client_hello_buf|. This function should only be called when the
1825 // current message is a ClientHello. It returns true on success and false on
1826 // error.
1827 //
1828 // Note that fields of the returned |out_msg| and |out_client_hello| point
1829 // into a handshake-owned buffer, so their lifetimes should not exceed this
1830 // SSL_HANDSHAKE.
1831 bool GetClientHello(SSLMessage *out_msg, SSL_CLIENT_HELLO *out_client_hello);
1832
David Benjamine530ea32019-08-16 19:28:00 -04001833 Span<uint8_t> secret() { return MakeSpan(secret_, hash_len_); }
David Benjamin83a49932021-05-20 15:57:09 -04001834 Span<const uint8_t> secret() const {
1835 return MakeConstSpan(secret_, hash_len_);
1836 }
David Benjamine530ea32019-08-16 19:28:00 -04001837 Span<uint8_t> early_traffic_secret() {
1838 return MakeSpan(early_traffic_secret_, hash_len_);
1839 }
1840 Span<uint8_t> client_handshake_secret() {
1841 return MakeSpan(client_handshake_secret_, hash_len_);
1842 }
1843 Span<uint8_t> server_handshake_secret() {
1844 return MakeSpan(server_handshake_secret_, hash_len_);
1845 }
1846 Span<uint8_t> client_traffic_secret_0() {
1847 return MakeSpan(client_traffic_secret_0_, hash_len_);
1848 }
1849 Span<uint8_t> server_traffic_secret_0() {
1850 return MakeSpan(server_traffic_secret_0_, hash_len_);
1851 }
1852 Span<uint8_t> expected_client_finished() {
1853 return MakeSpan(expected_client_finished_, hash_len_);
1854 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001855
David Benjaminf5d2cd02016-10-06 19:39:20 -04001856 union {
David Benjaminc11ea9422017-08-29 16:33:21 -04001857 // sent is a bitset where the bits correspond to elements of kExtensions
David Benjaminec552ca2021-06-16 10:47:36 -04001858 // in extensions.cc. Each bit is set if that extension was sent in a
David Benjaminc11ea9422017-08-29 16:33:21 -04001859 // ClientHello. It's not used by servers.
David Benjamin8f288862017-07-20 14:01:44 -04001860 uint32_t sent = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04001861 // received is a bitset, like |sent|, but is used by servers to record
1862 // which extensions were received from a client.
David Benjaminf5d2cd02016-10-06 19:39:20 -04001863 uint32_t received;
1864 } extensions;
1865
David Benjamin83a49932021-05-20 15:57:09 -04001866 // inner_extensions_sent, on clients that offer ECH, is |extensions.sent| for
1867 // the ClientHelloInner.
1868 uint32_t inner_extensions_sent = 0;
1869
David Benjamine52f4c42017-10-01 22:35:10 -04001870 // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
1871 UniquePtr<ERR_SAVE_STATE> error;
1872
Adam Langley7b935932018-11-12 13:53:42 -08001873 // key_shares are the current key exchange instances. The second is only used
1874 // as a client if we believe that we should offer two key shares in a
1875 // ClientHello.
1876 UniquePtr<SSLKeyShare> key_shares[2];
Adam Langley4ba6e192016-12-07 15:54:54 -08001877
David Benjaminc11ea9422017-08-29 16:33:21 -04001878 // transcript is the current handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001879 SSLTranscript transcript;
Steven Valdez908ac192017-01-12 13:17:07 -05001880
David Benjamin83a49932021-05-20 15:57:09 -04001881 // inner_transcript, on the client, is the handshake transcript for the
1882 // ClientHelloInner handshake. It is moved to |transcript| if the server
1883 // accepts ECH.
1884 SSLTranscript inner_transcript;
1885
1886 // inner_client_random is the ClientHello random value used with
1887 // ClientHelloInner.
1888 uint8_t inner_client_random[SSL3_RANDOM_SIZE] = {0};
1889
David Benjamin361e3e02022-09-11 13:58:38 -04001890 // cookie is the value of the cookie in HelloRetryRequest, or empty if none
1891 // was received.
David Benjamin08f5c762017-09-21 02:43:05 -04001892 Array<uint8_t> cookie;
David Benjamin3baa6e12016-10-07 21:10:38 -04001893
David Benjamin361e3e02022-09-11 13:58:38 -04001894 // dtls_cookie is the value of the cookie in DTLS HelloVerifyRequest. If
1895 // empty, either none was received or HelloVerifyRequest contained an empty
1896 // cookie.
1897 Array<uint8_t> dtls_cookie;
1898
David Benjamin18b68362021-06-18 23:13:46 -04001899 // ech_client_outer contains the outer ECH extension to send in the
1900 // ClientHello, excluding the header and type byte.
1901 Array<uint8_t> ech_client_outer;
Dan McArdle1920c6f2020-03-11 17:29:40 -04001902
David Benjaminba423c92021-06-15 16:26:58 -04001903 // ech_retry_configs, on the client, contains the retry configs from the
1904 // server as a serialized ECHConfigList.
1905 Array<uint8_t> ech_retry_configs;
1906
Daniel McArdle00e434d2021-02-18 11:47:18 -05001907 // ech_client_hello_buf, on the server, contains the bytes of the
1908 // reconstructed ClientHelloInner message.
1909 Array<uint8_t> ech_client_hello_buf;
1910
David Benjamin97ede402021-05-18 14:17:52 -04001911 // key_share_bytes is the key_share extension that the client should send.
David Benjamin08f5c762017-09-21 02:43:05 -04001912 Array<uint8_t> key_share_bytes;
David Benjamin4fe3c902016-08-16 02:17:03 -04001913
David Benjamin08b1f382023-02-28 17:22:23 -05001914 // key_share_ciphertext, for servers, is encapsulated shared secret to be sent
1915 // to the client in the TLS 1.3 key_share extension.
1916 Array<uint8_t> key_share_ciphertext;
Steven Valdez143e8b32016-07-11 13:19:03 -04001917
David Benjaminc11ea9422017-08-29 16:33:21 -04001918 // peer_sigalgs are the signature algorithms that the peer supports. These are
1919 // taken from the contents of the signature algorithms extension for a server
1920 // or from the CertificateRequest for a client.
David Benjaminb1cf48e2017-09-21 11:37:46 -04001921 Array<uint16_t> peer_sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04001922
David Benjaminc11ea9422017-08-29 16:33:21 -04001923 // peer_supported_group_list contains the supported group IDs advertised by
1924 // the peer. This is only set on the server's end. The server does not
1925 // advertise this extension to the client.
David Benjamincf0ce672017-09-21 02:25:59 -04001926 Array<uint16_t> peer_supported_group_list;
David Benjamin43612b62016-10-07 00:41:50 -04001927
Watson Ladddcd6e442020-08-10 15:12:45 -04001928 // peer_delegated_credential_sigalgs are the signature algorithms the peer
David Benjaminc06c4d52024-02-13 23:02:14 -05001929 // supports with delegated credentials, or empty if the peer does not support
1930 // delegated credentials.
Watson Ladddcd6e442020-08-10 15:12:45 -04001931 Array<uint16_t> peer_delegated_credential_sigalgs;
1932
David Benjaminc11ea9422017-08-29 16:33:21 -04001933 // peer_key is the peer's ECDH key for a TLS 1.2 client.
David Benjamin499742c2017-07-22 12:45:38 -04001934 Array<uint8_t> peer_key;
David Benjamina4c8ff02016-10-08 02:49:01 -04001935
David Benjamine9c5d722021-06-09 17:43:16 -04001936 // extension_permutation is the permutation to apply to ClientHello
1937 // extensions. It maps indices into the |kExtensions| table into other
1938 // indices.
1939 Array<uint8_t> extension_permutation;
1940
Adam Langleya307cb72018-05-02 09:06:48 -07001941 // cert_compression_alg_id, for a server, contains the negotiated certificate
1942 // compression algorithm for this client. It is only valid if
1943 // |cert_compression_negotiated| is true.
1944 uint16_t cert_compression_alg_id;
1945
David Benjamin83a49932021-05-20 15:57:09 -04001946 // ech_hpke_ctx is the HPKE context used in ECH. On the server, it is
David Benjaminba423c92021-06-15 16:26:58 -04001947 // initialized if |ech_status| is |ssl_ech_accepted|. On the client, it is
1948 // initialized if |selected_ech_config| is not nullptr.
Daniel McArdle00e434d2021-02-18 11:47:18 -05001949 ScopedEVP_HPKE_CTX ech_hpke_ctx;
1950
David Benjaminc11ea9422017-08-29 16:33:21 -04001951 // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
1952 // parameters. It has client and server randoms prepended for signing
1953 // convenience.
David Benjamin879efc32017-09-21 11:20:53 -04001954 Array<uint8_t> server_params;
David Benjamina4c8ff02016-10-08 02:49:01 -04001955
David Benjaminc11ea9422017-08-29 16:33:21 -04001956 // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
1957 // server when using a TLS 1.2 PSK key exchange.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001958 UniquePtr<char> peer_psk_identity_hint;
Adam Langley4ba6e192016-12-07 15:54:54 -08001959
David Benjaminc11ea9422017-08-29 16:33:21 -04001960 // ca_names, on the client, contains the list of CAs received in a
1961 // CertificateRequest message.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001962 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
Adam Langley34b4c822017-02-02 10:57:17 -08001963
David Benjaminb9493552017-09-27 19:02:51 -04001964 // cached_x509_ca_names contains a cache of parsed versions of the elements of
1965 // |ca_names|. This pointer is left non-owning so only
1966 // |ssl_crypto_x509_method| needs to link against crypto/x509.
David Benjamin8f288862017-07-20 14:01:44 -04001967 STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
Adam Langley4ba6e192016-12-07 15:54:54 -08001968
David Benjaminc11ea9422017-08-29 16:33:21 -04001969 // certificate_types, on the client, contains the set of certificate types
1970 // received in a CertificateRequest message.
David Benjamin08f5c762017-09-21 02:43:05 -04001971 Array<uint8_t> certificate_types;
Adam Langley4ba6e192016-12-07 15:54:54 -08001972
David Benjaminc11ea9422017-08-29 16:33:21 -04001973 // local_pubkey is the public key we are authenticating as.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001974 UniquePtr<EVP_PKEY> local_pubkey;
David Benjamina232a712017-03-30 15:51:53 -05001975
David Benjaminc11ea9422017-08-29 16:33:21 -04001976 // peer_pubkey is the public key parsed from the peer's leaf certificate.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001977 UniquePtr<EVP_PKEY> peer_pubkey;
Adam Langleyd5157222016-12-12 11:37:43 -08001978
David Benjaminc11ea9422017-08-29 16:33:21 -04001979 // new_session is the new mutable session being established by the current
1980 // handshake. It should not be cached.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001981 UniquePtr<SSL_SESSION> new_session;
David Benjamin45738dd2017-02-09 20:01:26 -05001982
David Benjaminc11ea9422017-08-29 16:33:21 -04001983 // early_session is the session corresponding to the current 0-RTT state on
1984 // the client if |in_early_data| is true.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001985 UniquePtr<SSL_SESSION> early_session;
Steven Valdeze831a812017-03-09 14:56:07 -05001986
David Benjaminc3b373b2021-06-06 13:04:26 -04001987 // ssl_ech_keys, for servers, is the set of ECH keys to use with this
1988 // handshake. This is copied from |SSL_CTX| to ensure consistent behavior as
1989 // |SSL_CTX| rotates keys.
1990 UniquePtr<SSL_ECH_KEYS> ech_keys;
Daniel McArdle00e434d2021-02-18 11:47:18 -05001991
David Benjamin83a49932021-05-20 15:57:09 -04001992 // selected_ech_config, for clients, is the ECHConfig the client uses to offer
1993 // ECH, or nullptr if ECH is not being offered. If non-NULL, |ech_hpke_ctx|
1994 // will be initialized.
1995 UniquePtr<ECHConfig> selected_ech_config;
1996
David Benjaminc11ea9422017-08-29 16:33:21 -04001997 // new_cipher is the cipher being negotiated in this handshake.
David Benjamin8f288862017-07-20 14:01:44 -04001998 const SSL_CIPHER *new_cipher = nullptr;
David Benjamin45738dd2017-02-09 20:01:26 -05001999
David Benjaminc11ea9422017-08-29 16:33:21 -04002000 // key_block is the record-layer key block for TLS 1.2 and earlier.
David Benjaminb9493552017-09-27 19:02:51 -04002001 Array<uint8_t> key_block;
Adam Langley4ba6e192016-12-07 15:54:54 -08002002
David Benjaminb571e772021-03-25 19:42:16 -04002003 // hints contains the handshake hints for this connection. If
2004 // |hints_requested| is true, this field is non-null and contains the pending
2005 // hints to filled as the predicted handshake progresses. Otherwise, this
2006 // field, if non-null, contains hints configured by the caller and will
2007 // influence the handshake on match.
2008 UniquePtr<SSL_HANDSHAKE_HINTS> hints;
2009
David Benjamin18b68362021-06-18 23:13:46 -04002010 // ech_is_inner, on the server, indicates whether the ClientHello contained an
2011 // inner ECH extension.
2012 bool ech_is_inner : 1;
Dan McArdlec2959352020-10-29 14:31:31 -04002013
David Benjaminba423c92021-06-15 16:26:58 -04002014 // ech_authenticated_reject, on the client, indicates whether an ECH rejection
2015 // handshake has been authenticated.
2016 bool ech_authenticated_reject : 1;
2017
David Benjaminfd45ee72017-08-31 14:49:09 -04002018 // scts_requested is true if the SCT extension is in the ClientHello.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002019 bool scts_requested : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002020
Steven Valdezd8168742017-08-31 10:15:48 -04002021 // handshake_finalized is true once the handshake has completed, at which
2022 // point accessors should use the established state.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002023 bool handshake_finalized : 1;
Steven Valdezd8168742017-08-31 10:15:48 -04002024
David Benjaminc11ea9422017-08-29 16:33:21 -04002025 // accept_psk_mode stores whether the client's PSK mode is compatible with our
2026 // preferences.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002027 bool accept_psk_mode : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002028
David Benjaminfd45ee72017-08-31 14:49:09 -04002029 // cert_request is true if a client certificate was requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002030 bool cert_request : 1;
David Benjamina0486782016-10-06 19:11:32 -04002031
David Benjaminfd45ee72017-08-31 14:49:09 -04002032 // certificate_status_expected is true if OCSP stapling was negotiated and the
David Benjaminc11ea9422017-08-29 16:33:21 -04002033 // server is expected to send a CertificateStatus message. (This is used on
2034 // both the client and server sides.)
Jesse Selover6b0d8222018-08-14 12:47:29 -04002035 bool certificate_status_expected : 1;
David Benjamina0486782016-10-06 19:11:32 -04002036
David Benjaminfd45ee72017-08-31 14:49:09 -04002037 // ocsp_stapling_requested is true if a client requested OCSP stapling.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002038 bool ocsp_stapling_requested : 1;
David Benjamina0486782016-10-06 19:11:32 -04002039
David Benjaminc11ea9422017-08-29 16:33:21 -04002040 // should_ack_sni is used by a server and indicates that the SNI extension
2041 // should be echoed in the ServerHello.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002042 bool should_ack_sni : 1;
David Benjamina0486782016-10-06 19:11:32 -04002043
David Benjaminfd45ee72017-08-31 14:49:09 -04002044 // in_false_start is true if there is a pending client handshake in False
David Benjaminc11ea9422017-08-29 16:33:21 -04002045 // Start. The client may write data at this point.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002046 bool in_false_start : 1;
David Benjamina0486782016-10-06 19:11:32 -04002047
David Benjaminfd45ee72017-08-31 14:49:09 -04002048 // in_early_data is true if there is a pending handshake that has progressed
David Benjaminc11ea9422017-08-29 16:33:21 -04002049 // enough to send and receive early data.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002050 bool in_early_data : 1;
Steven Valdeze831a812017-03-09 14:56:07 -05002051
David Benjaminfd45ee72017-08-31 14:49:09 -04002052 // early_data_offered is true if the client sent the early_data extension.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002053 bool early_data_offered : 1;
Steven Valdez2d850622017-01-11 11:34:52 -05002054
David Benjaminfd45ee72017-08-31 14:49:09 -04002055 // can_early_read is true if application data may be read at this point in the
David Benjaminc11ea9422017-08-29 16:33:21 -04002056 // handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002057 bool can_early_read : 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05002058
David Benjaminfd45ee72017-08-31 14:49:09 -04002059 // can_early_write is true if application data may be written at this point in
David Benjaminc11ea9422017-08-29 16:33:21 -04002060 // the handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002061 bool can_early_write : 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05002062
David Benjaminc11ea9422017-08-29 16:33:21 -04002063 // next_proto_neg_seen is one of NPN was negotiated.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002064 bool next_proto_neg_seen : 1;
David Benjaminb74b0812016-10-06 19:43:48 -04002065
David Benjaminfd45ee72017-08-31 14:49:09 -04002066 // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
David Benjaminc11ea9422017-08-29 16:33:21 -04002067 // or received.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002068 bool ticket_expected : 1;
David Benjaminf04c2e92016-12-06 13:35:25 -05002069
David Benjaminfd45ee72017-08-31 14:49:09 -04002070 // extended_master_secret is true if the extended master secret extension is
David Benjaminc11ea9422017-08-29 16:33:21 -04002071 // negotiated in this handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002072 bool extended_master_secret : 1;
David Benjaminfc02b592017-02-17 16:26:01 -05002073
David Benjaminfd45ee72017-08-31 14:49:09 -04002074 // pending_private_key_op is true if there is a pending private key operation
David Benjaminc11ea9422017-08-29 16:33:21 -04002075 // in progress.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002076 bool pending_private_key_op : 1;
David Benjamin44148742017-06-17 13:20:59 -04002077
Adam Langleya307cb72018-05-02 09:06:48 -07002078 // handback indicates that a server should pause the handshake after
2079 // finishing operations that require private key material, in such a way that
David Benjaminf4928302019-08-21 16:04:53 -04002080 // |SSL_get_error| returns |SSL_ERROR_HANDBACK|. It is set by
2081 // |SSL_apply_handoff|.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002082 bool handback : 1;
Adam Langleya307cb72018-05-02 09:06:48 -07002083
David Benjaminb571e772021-03-25 19:42:16 -04002084 // hints_requested indicates the caller has requested handshake hints. Only
2085 // the first round-trip of the handshake will complete, after which the
2086 // |hints| structure can be serialized.
2087 bool hints_requested : 1;
2088
Adam Langleya307cb72018-05-02 09:06:48 -07002089 // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002090 bool cert_compression_negotiated : 1;
Adam Langleya307cb72018-05-02 09:06:48 -07002091
David Benjamin6965d252018-11-19 15:49:56 -06002092 // apply_jdk11_workaround is true if the peer is probably a JDK 11 client
2093 // which implemented TLS 1.3 incorrectly.
2094 bool apply_jdk11_workaround : 1;
2095
David Benjamin9b2cdb72021-04-01 23:21:53 -04002096 // can_release_private_key is true if the private key will no longer be used
2097 // in this handshake.
2098 bool can_release_private_key : 1;
2099
David Benjamin8acec002021-05-19 13:03:34 -04002100 // channel_id_negotiated is true if Channel ID should be used in this
2101 // handshake.
2102 bool channel_id_negotiated : 1;
2103
David Benjaminc11ea9422017-08-29 16:33:21 -04002104 // client_version is the value sent or received in the ClientHello version.
David Benjamin8f288862017-07-20 14:01:44 -04002105 uint16_t client_version = 0;
Alessandro Ghedinide254b42017-04-17 19:12:33 +01002106
David Benjaminc11ea9422017-08-29 16:33:21 -04002107 // early_data_read is the amount of early data that has been read by the
2108 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04002109 uint16_t early_data_read = 0;
Steven Valdeze831a812017-03-09 14:56:07 -05002110
David Benjaminc11ea9422017-08-29 16:33:21 -04002111 // early_data_written is the amount of early data that has been written by the
2112 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04002113 uint16_t early_data_written = 0;
Adam Langley2f9b47f2017-12-29 14:15:55 -08002114
Steven Valdez94a63a52021-04-29 10:52:42 -04002115 // ech_config_id is the ECH config sent by the client.
2116 uint8_t ech_config_id = 0;
2117
Steven Valdezb84674b2018-08-28 10:14:07 -04002118 // session_id is the session ID in the ClientHello.
Adam Langley2f9b47f2017-12-29 14:15:55 -08002119 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
2120 uint8_t session_id_len = 0;
David Benjamina7bc9442018-01-18 10:08:53 -05002121
David Benjamin33e8c782021-05-19 17:07:18 -04002122 // grease_seed is the entropy for GREASE values.
David Benjamina7bc9442018-01-18 10:08:53 -05002123 uint8_t grease_seed[ssl_grease_last_index + 1] = {0};
David Benjamin86e95b82017-07-18 16:34:25 -04002124};
Steven Valdez143e8b32016-07-11 13:19:03 -04002125
David Benjamin3f180b82022-05-09 17:45:18 -04002126// kMaxTickets is the maximum number of tickets to send immediately after the
2127// handshake. We use a one-byte ticket nonce, and there is no point in sending
2128// so many tickets.
2129constexpr size_t kMaxTickets = 16;
2130
David Benjamin8e7bbba2017-10-13 17:18:35 -04002131UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -04002132
David Benjaminc11ea9422017-08-29 16:33:21 -04002133// ssl_check_message_type checks if |msg| has type |type|. If so it returns
2134// one. Otherwise, it sends an alert and returns zero.
David Benjamin97250f42017-10-07 04:12:35 -04002135bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
David Benjamin276b7e82017-01-21 14:13:39 -05002136
David Benjaminc11ea9422017-08-29 16:33:21 -04002137// ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
2138// on error. It sets |out_early_return| to one if we've completed the handshake
2139// early.
David Benjamin74795b32017-08-31 15:13:12 -04002140int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -04002141
David Benjaminc11ea9422017-08-29 16:33:21 -04002142// The following are implementations of |do_handshake| for the client and
2143// server.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04002144enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
2145enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +09002146enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
2147enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002148
David Benjaminc11ea9422017-08-29 16:33:21 -04002149// The following functions return human-readable representations of the TLS
2150// handshake states for debugging.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04002151const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
2152const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
David Benjaminf60bcfb2017-08-18 15:23:44 -04002153const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
2154const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
2155
Adam Langleyba9ad662018-12-17 13:59:38 -08002156// tls13_add_key_update queues a KeyUpdate message on |ssl|. The
2157// |update_requested| argument must be one of |SSL_KEY_UPDATE_REQUESTED| or
2158// |SSL_KEY_UPDATE_NOT_REQUESTED|.
2159bool tls13_add_key_update(SSL *ssl, int update_requested);
2160
David Benjamin12f58782018-08-28 17:06:31 -05002161// tls13_post_handshake processes a post-handshake message. It returns true on
2162// success and false on failure.
2163bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
Steven Valdez8e1c7be2016-07-26 12:39:22 -04002164
David Benjamin12f58782018-08-28 17:06:31 -05002165bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
2166 bool allow_anonymous);
2167bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjamin794cc592017-03-25 22:24:23 -05002168
David Benjaminc11ea9422017-08-29 16:33:21 -04002169// tls13_process_finished processes |msg| as a Finished message from the
David Benjamin12f58782018-08-28 17:06:31 -05002170// peer. If |use_saved_value| is true, the verify_data is compared against
David Benjaminc11ea9422017-08-29 16:33:21 -04002171// |hs->expected_client_finished| rather than computed fresh.
David Benjamin12f58782018-08-28 17:06:31 -05002172bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
2173 bool use_saved_value);
Steven Valdez143e8b32016-07-11 13:19:03 -04002174
David Benjamin12f58782018-08-28 17:06:31 -05002175bool tls13_add_certificate(SSL_HANDSHAKE *hs);
David Benjamin44148742017-06-17 13:20:59 -04002176
David Benjaminc11ea9422017-08-29 16:33:21 -04002177// tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
2178// handshake. If it returns |ssl_private_key_retry|, it should be called again
2179// to retry when the signing operation is completed.
David Benjamin44148742017-06-17 13:20:59 -04002180enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
2181
David Benjamin12f58782018-08-28 17:06:31 -05002182bool tls13_add_finished(SSL_HANDSHAKE *hs);
2183bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
Adam Langley53a17f52020-05-26 14:44:07 -07002184bssl::UniquePtr<SSL_SESSION> tls13_create_session_with_ticket(SSL *ssl,
2185 CBS *body);
Steven Valdez143e8b32016-07-11 13:19:03 -04002186
David Benjamine9c5d722021-06-09 17:43:16 -04002187// ssl_setup_extension_permutation computes a ClientHello extension permutation
2188// for |hs|, if applicable. It returns true on success and false on error.
2189bool ssl_setup_extension_permutation(SSL_HANDSHAKE *hs);
2190
David Benjamin97ede402021-05-18 14:17:52 -04002191// ssl_setup_key_shares computes client key shares and saves them in |hs|. It
2192// returns true on success and false on failure. If |override_group_id| is zero,
2193// it offers the default groups, including GREASE. If it is non-zero, it offers
2194// a single key share of the specified group.
2195bool ssl_setup_key_shares(SSL_HANDSHAKE *hs, uint16_t override_group_id);
2196
David Benjamin31640932017-10-11 13:22:39 -04002197bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2198 Array<uint8_t> *out_secret,
2199 uint8_t *out_alert, CBS *contents);
2200bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002201 Span<const uint8_t> *out_peer_key,
2202 uint8_t *out_alert,
2203 const SSL_CLIENT_HELLO *client_hello);
2204bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez143e8b32016-07-11 13:19:03 -04002205
David Benjamin31640932017-10-11 13:22:39 -04002206bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
2207 uint8_t *out_alert,
2208 CBS *contents);
2209bool ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05002210 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin9806ae02019-08-16 15:32:03 -04002211 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert,
2212 const SSL_CLIENT_HELLO *client_hello, CBS *contents);
David Benjamin31640932017-10-11 13:22:39 -04002213bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002214
David Benjaminc11ea9422017-08-29 16:33:21 -04002215// ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
David Benjamin861abcc2018-07-14 17:40:26 -04002216// returns whether it's valid.
2217bool ssl_is_sct_list_valid(const CBS *contents);
Adam Langleycfa08c32016-11-17 13:21:27 -08002218
David Benjamin83a49932021-05-20 15:57:09 -04002219// ssl_write_client_hello_without_extensions writes a ClientHello to |out|,
2220// up to the extensions field. |type| determines the type of ClientHello to
2221// write. If |omit_session_id| is true, the session ID is empty.
2222bool ssl_write_client_hello_without_extensions(const SSL_HANDSHAKE *hs,
2223 CBB *cbb,
2224 ssl_client_hello_type_t type,
2225 bool empty_session_id);
2226
2227// ssl_add_client_hello constructs a ClientHello and adds it to the outgoing
2228// flight. It returns true on success and false on error.
2229bool ssl_add_client_hello(SSL_HANDSHAKE *hs);
Steven Valdez5440fe02016-07-18 12:40:30 -04002230
David Benjamine2cb4232021-06-23 18:14:22 -04002231struct ParsedServerHello {
David Benjamin18b68362021-06-18 23:13:46 -04002232 CBS raw;
David Benjamine2cb4232021-06-23 18:14:22 -04002233 uint16_t legacy_version = 0;
2234 CBS random;
2235 CBS session_id;
2236 uint16_t cipher_suite = 0;
2237 uint8_t compression_method = 0;
2238 CBS extensions;
2239};
2240
2241// ssl_parse_server_hello parses |msg| as a ServerHello. On success, it writes
2242// the result to |*out| and returns true. Otherwise, it returns false and sets
2243// |*out_alert| to an alert to send to the peer.
2244bool ssl_parse_server_hello(ParsedServerHello *out, uint8_t *out_alert,
2245 const SSLMessage &msg);
2246
Nick Harper60a85cb2016-09-23 16:25:11 -07002247enum ssl_cert_verify_context_t {
2248 ssl_cert_verify_server,
2249 ssl_cert_verify_client,
2250 ssl_cert_verify_channel_id,
2251};
2252
David Benjaminc11ea9422017-08-29 16:33:21 -04002253// tls13_get_cert_verify_signature_input generates the message to be signed for
2254// TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
David Benjamin75a1f232017-10-11 17:19:19 -04002255// type of signature. It sets |*out| to a newly allocated buffer containing the
2256// result. This function returns true on success and false on failure.
2257bool tls13_get_cert_verify_signature_input(
2258 SSL_HANDSHAKE *hs, Array<uint8_t> *out,
Nick Harper60a85cb2016-09-23 16:25:11 -07002259 enum ssl_cert_verify_context_t cert_verify_context);
2260
David Benjamin12a3e7e2021-04-13 11:47:36 -04002261// ssl_is_valid_alpn_list returns whether |in| is a valid ALPN protocol list.
2262bool ssl_is_valid_alpn_list(Span<const uint8_t> in);
2263
David Benjamindd6c2e82017-10-17 15:48:46 -04002264// ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002265// selection for |hs->ssl|'s client preferences.
2266bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
2267 Span<const uint8_t> protocol);
David Benjamindd6c2e82017-10-17 15:48:46 -04002268
David Benjaminc11ea9422017-08-29 16:33:21 -04002269// ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
David Benjamin31640932017-10-11 13:22:39 -04002270// true on successful negotiation or if nothing was negotiated. It returns false
David Benjaminc11ea9422017-08-29 16:33:21 -04002271// and sets |*out_alert| to an alert on error.
David Benjamin31640932017-10-11 13:22:39 -04002272bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
2273 const SSL_CLIENT_HELLO *client_hello);
David Benjamin9ef31f02016-10-31 18:01:13 -04002274
David Benjamin4e93cd42021-05-18 13:38:25 -04002275// ssl_get_local_application_settings looks up the configured ALPS value for
2276// |protocol|. If found, it sets |*out_settings| to the value and returns true.
2277// Otherwise, it returns false.
2278bool ssl_get_local_application_settings(const SSL_HANDSHAKE *hs,
2279 Span<const uint8_t> *out_settings,
2280 Span<const uint8_t> protocol);
2281
Steven Valdez51607f12020-08-05 10:46:05 -04002282// ssl_negotiate_alps negotiates the ALPS extension, if applicable. It returns
2283// true on successful negotiation or if nothing was negotiated. It returns false
2284// and sets |*out_alert| to an alert on error.
2285bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert,
2286 const SSL_CLIENT_HELLO *client_hello);
2287
David Benjamina75027b2021-07-20 15:07:22 -04002288struct SSLExtension {
2289 SSLExtension(uint16_t type_arg, bool allowed_arg = true)
2290 : type(type_arg), allowed(allowed_arg), present(false) {
2291 CBS_init(&data, nullptr, 0);
2292 }
2293
David Benjaminffb11072016-11-13 10:32:10 +09002294 uint16_t type;
David Benjamina75027b2021-07-20 15:07:22 -04002295 bool allowed;
2296 bool present;
2297 CBS data;
David Benjamin86e95b82017-07-18 16:34:25 -04002298};
David Benjaminffb11072016-11-13 10:32:10 +09002299
David Benjaminc11ea9422017-08-29 16:33:21 -04002300// ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
David Benjamina75027b2021-07-20 15:07:22 -04002301// it. It writes the parsed extensions to pointers in |extensions|. On success,
2302// it fills in the |present| and |data| fields and returns true. Otherwise, it
2303// sets |*out_alert| to an alert to send and returns false. Unknown extensions
2304// are rejected unless |ignore_unknown| is true.
David Benjaminc4ec14c2020-09-21 18:42:52 -04002305bool ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
David Benjamina75027b2021-07-20 15:07:22 -04002306 std::initializer_list<SSLExtension *> extensions,
David Benjaminc4ec14c2020-09-21 18:42:52 -04002307 bool ignore_unknown);
David Benjaminffb11072016-11-13 10:32:10 +09002308
David Benjaminc11ea9422017-08-29 16:33:21 -04002309// ssl_verify_peer_cert verifies the peer certificate for |hs|.
David Benjamin3a1dd462017-07-11 16:13:10 -04002310enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
Jesse Selover1c337e52018-08-10 13:28:47 -04002311// ssl_reverify_peer_cert verifies the peer certificate for |hs| when resuming a
2312// session.
David Benjaminee0716f2019-11-19 14:16:28 +08002313enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs,
2314 bool send_alert);
David Benjamin3a1dd462017-07-11 16:13:10 -04002315
David Benjamin00f48c82017-10-06 18:43:53 -04002316enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
David Benjamin88a537f2024-02-23 11:25:22 -05002317
2318// ssl_send_finished adds a Finished message to the current flight of messages.
2319// It returns true on success and false on error.
David Benjamin00f48c82017-10-06 18:43:53 -04002320bool ssl_send_finished(SSL_HANDSHAKE *hs);
David Benjamin88a537f2024-02-23 11:25:22 -05002321
2322// ssl_send_tls12_certificate adds a TLS 1.2 Certificate message to the current
2323// flight of messages. It returns true on success and false on error.
2324bool ssl_send_tls12_certificate(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002325
David Benjamin5fa22ed2020-06-22 17:08:11 -04002326// ssl_handshake_session returns the |SSL_SESSION| corresponding to the current
2327// handshake. Note, in TLS 1.2 resumptions, this session is immutable.
2328const SSL_SESSION *ssl_handshake_session(const SSL_HANDSHAKE *hs);
2329
David Benjamin6c9758f2021-05-18 13:50:50 -04002330// ssl_done_writing_client_hello is called after the last ClientHello is written
2331// by |hs|. It releases some memory that is no longer needed.
2332void ssl_done_writing_client_hello(SSL_HANDSHAKE *hs);
2333
David Benjamin5fa22ed2020-06-22 17:08:11 -04002334
David Benjaminc11ea9422017-08-29 16:33:21 -04002335// SSLKEYLOGFILE functions.
David Benjamine776cc22016-07-19 07:26:49 +02002336
David Benjaminc11ea9422017-08-29 16:33:21 -04002337// ssl_log_secret logs |secret| with label |label|, if logging is enabled for
David Benjaminb244e3a2019-08-16 19:33:15 -04002338// |ssl|. It returns true on success and false on failure.
2339bool ssl_log_secret(const SSL *ssl, const char *label,
2340 Span<const uint8_t> secret);
David Benjamine776cc22016-07-19 07:26:49 +02002341
2342
David Benjaminc11ea9422017-08-29 16:33:21 -04002343// ClientHello functions.
David Benjamine14ff062016-08-09 16:21:24 -04002344
David Benjamin15961372021-04-14 21:46:25 -04002345// ssl_client_hello_init parses |body| as a ClientHello message, excluding the
2346// message header, and writes the result to |*out|. It returns true on success
2347// and false on error. This function is exported for testing.
2348OPENSSL_EXPORT bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
2349 Span<const uint8_t> body);
David Benjamine14ff062016-08-09 16:21:24 -04002350
David Benjamin18b68362021-06-18 23:13:46 -04002351bool ssl_parse_client_hello_with_trailing_data(const SSL *ssl, CBS *cbs,
2352 SSL_CLIENT_HELLO *out);
2353
David Benjamin861abcc2018-07-14 17:40:26 -04002354bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
2355 CBS *out, uint16_t extension_type);
David Benjamine14ff062016-08-09 16:21:24 -04002356
David Benjamin861abcc2018-07-14 17:40:26 -04002357bool ssl_client_cipher_list_contains_cipher(
2358 const SSL_CLIENT_HELLO *client_hello, uint16_t id);
David Benjamine14ff062016-08-09 16:21:24 -04002359
2360
David Benjaminc11ea9422017-08-29 16:33:21 -04002361// GREASE.
David Benjamin65ac9972016-09-02 21:35:25 -04002362
David Benjamina7bc9442018-01-18 10:08:53 -05002363// ssl_get_grease_value returns a GREASE value for |hs|. For a given
David Benjaminc11ea9422017-08-29 16:33:21 -04002364// connection, the values for each index will be deterministic. This allows the
2365// same ClientHello be sent twice for a HelloRetryRequest or the same group be
2366// advertised in both supported_groups and key_shares.
David Benjamin33e8c782021-05-19 17:07:18 -04002367uint16_t ssl_get_grease_value(const SSL_HANDSHAKE *hs,
2368 enum ssl_grease_index_t index);
David Benjamin65ac9972016-09-02 21:35:25 -04002369
2370
David Benjaminc11ea9422017-08-29 16:33:21 -04002371// Signature algorithms.
David Benjamin3ef76972016-10-17 17:59:54 -04002372
David Benjaminc11ea9422017-08-29 16:33:21 -04002373// tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
David Benjamin610cdbb2018-01-22 19:08:38 -05002374// algorithms and saves them on |hs|. It returns true on success and false on
David Benjaminc11ea9422017-08-29 16:33:21 -04002375// error.
David Benjamin610cdbb2018-01-22 19:08:38 -05002376bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
David Benjamin3ef76972016-10-17 17:59:54 -04002377
David Benjaminc11ea9422017-08-29 16:33:21 -04002378// tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
David Benjamin610cdbb2018-01-22 19:08:38 -05002379// that should be used with |pkey| in TLS 1.1 and earlier. It returns true on
2380// success and false if |pkey| may not be used at those versions.
2381bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
David Benjamina3651382017-04-20 17:49:36 -04002382
David Benjaminc11ea9422017-08-29 16:33:21 -04002383// tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
2384// with |hs|'s private key based on the peer's preferences and the algorithms
David Benjamin610cdbb2018-01-22 19:08:38 -05002385// supported. It returns true on success and false on error.
2386bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
David Benjamin3ef76972016-10-17 17:59:54 -04002387
Christopher Patton6c1b3762018-07-17 12:49:41 -07002388// tls1_get_peer_verify_algorithms returns the signature schemes for which the
2389// peer indicated support.
2390//
2391// NOTE: The related function |SSL_get0_peer_verify_algorithms| only has
2392// well-defined behavior during the callbacks set by |SSL_CTX_set_cert_cb| and
2393// |SSL_CTX_set_client_cert_cb|, or when the handshake is paused because of
2394// them.
2395Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs);
2396
David Benjaminc11ea9422017-08-29 16:33:21 -04002397// tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
David Benjaminf2498402020-01-15 20:15:36 -05002398// peer signature to |out|. It returns true on success and false on error.
David Benjaminebad5082020-02-03 19:32:19 -05002399bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out);
David Benjamin3ef76972016-10-17 17:59:54 -04002400
David Benjaminc11ea9422017-08-29 16:33:21 -04002401// tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
David Benjamin610cdbb2018-01-22 19:08:38 -05002402// signature. It returns true on success and false on error, setting
2403// |*out_alert| to an alert to send.
David Benjaminebad5082020-02-03 19:32:19 -05002404bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin610cdbb2018-01-22 19:08:38 -05002405 uint16_t sigalg);
David Benjamin3ef76972016-10-17 17:59:54 -04002406
2407
David Benjaminc11ea9422017-08-29 16:33:21 -04002408// Underdocumented functions.
2409//
2410// Functions below here haven't been touched up and may be underdocumented.
David Benjamin71f07942015-04-08 02:36:59 -04002411
Adam Langley1258b6a2014-06-20 12:00:00 -07002412#define TLSEXT_CHANNEL_ID_SIZE 128
Adam Langley95c29f32014-06-20 12:00:00 -07002413
David Benjamin8648c532021-08-19 18:02:37 -04002414// From RFC 4492, used in encoding the curve type in ECParameters
Adam Langleyfcf25832014-12-18 17:42:32 -08002415#define NAMED_CURVE_TYPE 3
Adam Langley95c29f32014-06-20 12:00:00 -07002416
David Benjamin664e99a2017-10-10 19:23:21 -04002417struct CERT {
David Benjamine325c3f2018-04-12 16:11:15 -04002418 static constexpr bool kAllowUniquePtr = true;
2419
2420 explicit CERT(const SSL_X509_METHOD *x509_method);
2421 ~CERT();
2422
2423 UniquePtr<EVP_PKEY> privatekey;
Adam Langley3a2b47a2017-01-24 13:59:42 -08002424
David Benjaminc11ea9422017-08-29 16:33:21 -04002425 // chain contains the certificate chain, with the leaf at the beginning. The
2426 // first element of |chain| may be NULL to indicate that the leaf certificate
2427 // has not yet been set.
2428 // If |chain| != NULL -> len(chain) >= 1
2429 // If |chain[0]| == NULL -> len(chain) >= 2.
2430 // |chain[1..]| != NULL
David Benjamine325c3f2018-04-12 16:11:15 -04002431 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
Adam Langley3a2b47a2017-01-24 13:59:42 -08002432
David Benjaminc11ea9422017-08-29 16:33:21 -04002433 // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
2434 // a cache in order to implement “get0” functions that return a non-owning
2435 // pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04002436 STACK_OF(X509) *x509_chain = nullptr;
David Benjaminf31e6812014-11-13 18:05:55 -05002437
David Benjaminc11ea9422017-08-29 16:33:21 -04002438 // x509_leaf may contain a parsed copy of the first element of |chain|. This
2439 // is only used as a cache in order to implement “get0” functions that return
2440 // a non-owning pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04002441 X509 *x509_leaf = nullptr;
Adam Langley3a2b47a2017-01-24 13:59:42 -08002442
David Benjaminc11ea9422017-08-29 16:33:21 -04002443 // x509_stash contains the last |X509| object append to the chain. This is a
2444 // workaround for some third-party code that continue to use an |X509| object
2445 // even after passing ownership with an “add0” function.
David Benjamine325c3f2018-04-12 16:11:15 -04002446 X509 *x509_stash = nullptr;
Adam Langleye1e78132017-01-31 15:24:31 -08002447
David Benjaminc11ea9422017-08-29 16:33:21 -04002448 // key_method, if non-NULL, is a set of callbacks to call for private key
2449 // operations.
David Benjamine325c3f2018-04-12 16:11:15 -04002450 const SSL_PRIVATE_KEY_METHOD *key_method = nullptr;
David Benjaminb4d65fd2015-05-29 17:11:21 -04002451
David Benjaminc11ea9422017-08-29 16:33:21 -04002452 // x509_method contains pointers to functions that might deal with |X509|
2453 // compatibility, or might be a no-op, depending on the application.
David Benjamine325c3f2018-04-12 16:11:15 -04002454 const SSL_X509_METHOD *x509_method = nullptr;
Adam Langley3509dac2017-02-01 11:59:18 -08002455
David Benjamine325c3f2018-04-12 16:11:15 -04002456 // sigalgs, if non-empty, is the set of signature algorithms supported by
David Benjaminc11ea9422017-08-29 16:33:21 -04002457 // |privatekey| in decreasing order of preference.
David Benjamine325c3f2018-04-12 16:11:15 -04002458 Array<uint16_t> sigalgs;
Adam Langley95c29f32014-06-20 12:00:00 -07002459
David Benjaminc11ea9422017-08-29 16:33:21 -04002460 // Certificate setup callback: if set is called whenever a
2461 // certificate may be required (client or server). the callback
2462 // can then examine any appropriate parameters and setup any
2463 // certificates required. This allows advanced applications
2464 // to select certificates on the fly: for example based on
2465 // supported signature algorithms or curves.
David Benjamine325c3f2018-04-12 16:11:15 -04002466 int (*cert_cb)(SSL *ssl, void *arg) = nullptr;
2467 void *cert_cb_arg = nullptr;
Adam Langleyd323f4b2016-03-01 15:58:14 -08002468
David Benjaminc11ea9422017-08-29 16:33:21 -04002469 // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
2470 // store is used instead.
David Benjamine325c3f2018-04-12 16:11:15 -04002471 X509_STORE *verify_store = nullptr;
David Benjamin83a32122017-02-14 18:34:54 -05002472
David Benjaminc11ea9422017-08-29 16:33:21 -04002473 // Signed certificate timestamp list to be sent to the client, if requested
David Benjamine325c3f2018-04-12 16:11:15 -04002474 UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin83a32122017-02-14 18:34:54 -05002475
David Benjaminc11ea9422017-08-29 16:33:21 -04002476 // OCSP response to be sent to the client, if requested.
David Benjamine325c3f2018-04-12 16:11:15 -04002477 UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin5960a902017-02-14 20:07:11 -05002478
David Benjaminc11ea9422017-08-29 16:33:21 -04002479 // sid_ctx partitions the session space within a shared session cache or
2480 // ticket key. Only sessions with a matching value will be accepted.
David Benjamine325c3f2018-04-12 16:11:15 -04002481 uint8_t sid_ctx_length = 0;
2482 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
Christopher Patton6c1b3762018-07-17 12:49:41 -07002483
2484 // Delegated credentials.
2485
2486 // dc is the delegated credential to send to the peer (if requested).
2487 UniquePtr<DC> dc = nullptr;
2488
2489 // dc_privatekey is used instead of |privatekey| or |key_method| to
2490 // authenticate the host if a delegated credential is used in the handshake.
2491 UniquePtr<EVP_PKEY> dc_privatekey = nullptr;
2492
2493 // dc_key_method, if not NULL, is used instead of |dc_privatekey| to
2494 // authenticate the host.
2495 const SSL_PRIVATE_KEY_METHOD *dc_key_method = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002496};
Adam Langley95c29f32014-06-20 12:00:00 -07002497
David Benjamin664e99a2017-10-10 19:23:21 -04002498// |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
2499struct SSL_PROTOCOL_METHOD {
David Benjamin97250f42017-10-07 04:12:35 -04002500 bool is_dtls;
2501 bool (*ssl_new)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04002502 void (*ssl_free)(SSL *ssl);
2503 // get_message sets |*out| to the current handshake message and returns true
2504 // if one has been received. It returns false if more input is needed.
Adam Langleyc9827e02019-04-12 14:46:50 -07002505 bool (*get_message)(const SSL *ssl, SSLMessage *out);
David Benjamin664e99a2017-10-10 19:23:21 -04002506 // next_message is called to release the current handshake message.
2507 void (*next_message)(SSL *ssl);
David Benjaminf9cc26f2020-02-09 16:49:31 -05002508 // has_unprocessed_handshake_data returns whether there is buffered
2509 // handshake data that has not been consumed by |get_message|.
2510 bool (*has_unprocessed_handshake_data)(const SSL *ssl);
David Benjaminf6632da2017-10-12 19:11:47 -04002511 // Use the |ssl_open_handshake| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04002512 ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
2513 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04002514 // Use the |ssl_open_change_cipher_spec| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04002515 ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
2516 uint8_t *out_alert,
2517 Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04002518 // Use the |ssl_open_app_data| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04002519 ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
2520 size_t *out_consumed, uint8_t *out_alert,
2521 Span<uint8_t> in);
David Benjamin4bd32a82021-05-14 17:06:29 -04002522 // write_app_data encrypts and writes |in| as application data. On success, it
2523 // returns one and sets |*out_bytes_written| to the number of bytes of |in|
2524 // written. Otherwise, it returns <= 0 and sets |*out_needs_handshake| to
2525 // whether the operation failed because the caller needs to drive the
2526 // handshake.
2527 int (*write_app_data)(SSL *ssl, bool *out_needs_handshake,
2528 size_t *out_bytes_written, Span<const uint8_t> in);
David Benjamin664e99a2017-10-10 19:23:21 -04002529 int (*dispatch_alert)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04002530 // init_message begins a new handshake message of type |type|. |cbb| is the
2531 // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
David Benjamin97250f42017-10-07 04:12:35 -04002532 // the caller should write to. It returns true on success and false on error.
David Benjaminbcef5142021-06-02 11:24:26 -04002533 bool (*init_message)(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
David Benjamin664e99a2017-10-10 19:23:21 -04002534 // finish_message finishes a handshake message. It sets |*out_msg| to the
David Benjamin97250f42017-10-07 04:12:35 -04002535 // serialized message. It returns true on success and false on error.
David Benjaminbcef5142021-06-02 11:24:26 -04002536 bool (*finish_message)(const SSL *ssl, CBB *cbb,
2537 bssl::Array<uint8_t> *out_msg);
David Benjamin97250f42017-10-07 04:12:35 -04002538 // add_message adds a handshake message to the pending flight. It returns
2539 // true on success and false on error.
2540 bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
David Benjamin664e99a2017-10-10 19:23:21 -04002541 // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
David Benjamin97250f42017-10-07 04:12:35 -04002542 // flight. It returns true on success and false on error.
2543 bool (*add_change_cipher_spec)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04002544 // flush_flight flushes the pending flight to the transport. It returns one on
2545 // success and <= 0 on error.
2546 int (*flush_flight)(SSL *ssl);
2547 // on_handshake_complete is called when the handshake is complete.
2548 void (*on_handshake_complete)(SSL *ssl);
David Benjaminb0921922020-02-20 12:33:28 -05002549 // set_read_state sets |ssl|'s read cipher state and level to |aead_ctx| and
David Benjamin5298ef92020-03-13 12:17:30 -04002550 // |level|. In QUIC, |aead_ctx| is a placeholder object and |secret_for_quic|
2551 // is the original secret. This function returns true on success and false on
2552 // error.
David Benjaminb0921922020-02-20 12:33:28 -05002553 bool (*set_read_state)(SSL *ssl, ssl_encryption_level_t level,
David Benjamin5298ef92020-03-13 12:17:30 -04002554 UniquePtr<SSLAEADContext> aead_ctx,
2555 Span<const uint8_t> secret_for_quic);
David Benjaminb0921922020-02-20 12:33:28 -05002556 // set_write_state sets |ssl|'s write cipher state and level to |aead_ctx| and
David Benjamin5298ef92020-03-13 12:17:30 -04002557 // |level|. In QUIC, |aead_ctx| is a placeholder object and |secret_for_quic|
2558 // is the original secret. This function returns true on success and false on
2559 // error.
David Benjaminb0921922020-02-20 12:33:28 -05002560 bool (*set_write_state)(SSL *ssl, ssl_encryption_level_t level,
David Benjamin5298ef92020-03-13 12:17:30 -04002561 UniquePtr<SSLAEADContext> aead_ctx,
2562 Span<const uint8_t> secret_for_quic);
David Benjamin664e99a2017-10-10 19:23:21 -04002563};
2564
David Benjaminf6632da2017-10-12 19:11:47 -04002565// The following wrappers call |open_*| but handle |read_shutdown| correctly.
2566
2567// ssl_open_handshake processes a record from |in| for reading a handshake
2568// message.
2569ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
2570 uint8_t *out_alert, Span<uint8_t> in);
2571
2572// ssl_open_change_cipher_spec processes a record from |in| for reading a
2573// ChangeCipherSpec.
2574ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2575 uint8_t *out_alert,
2576 Span<uint8_t> in);
2577
2578// ssl_open_app_data processes a record from |in| for reading application data.
2579// On success, it returns |ssl_open_record_success| and sets |*out| to the
2580// input. If it encounters a post-handshake message, it returns
2581// |ssl_open_record_discard|. The caller should then retry, after processing any
2582// messages received with |get_message|.
2583ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
2584 size_t *out_consumed, uint8_t *out_alert,
2585 Span<uint8_t> in);
2586
David Benjamin0363de92018-06-29 13:57:38 -04002587struct SSL_X509_METHOD {
2588 // check_client_CA_list returns one if |names| is a good list of X.509
2589 // distinguished names and zero otherwise. This is used to ensure that we can
2590 // reject unparsable values at handshake time when using crypto/x509.
David Benjamin1e77ef42019-03-30 01:03:50 -05002591 bool (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
David Benjamin0363de92018-06-29 13:57:38 -04002592
2593 // cert_clear frees and NULLs all X509 certificate-related state.
2594 void (*cert_clear)(CERT *cert);
2595 // cert_free frees all X509-related state.
2596 void (*cert_free)(CERT *cert);
2597 // cert_flush_cached_chain drops any cached |X509|-based certificate chain
2598 // from |cert|.
2599 // cert_dup duplicates any needed fields from |cert| to |new_cert|.
2600 void (*cert_dup)(CERT *new_cert, const CERT *cert);
2601 void (*cert_flush_cached_chain)(CERT *cert);
2602 // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
2603 // from |cert|.
2604 void (*cert_flush_cached_leaf)(CERT *cert);
2605
2606 // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
2607 // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
David Benjamin1e77ef42019-03-30 01:03:50 -05002608 // true on success or false on error.
2609 bool (*session_cache_objects)(SSL_SESSION *session);
David Benjamin0363de92018-06-29 13:57:38 -04002610 // session_dup duplicates any needed fields from |session| to |new_session|.
David Benjamin1e77ef42019-03-30 01:03:50 -05002611 // It returns true on success or false on error.
2612 bool (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
David Benjamin0363de92018-06-29 13:57:38 -04002613 // session_clear frees any X509-related state from |session|.
2614 void (*session_clear)(SSL_SESSION *session);
2615 // session_verify_cert_chain verifies the certificate chain in |session|,
David Benjamin1e77ef42019-03-30 01:03:50 -05002616 // sets |session->verify_result| and returns true on success or false on
David Benjamin0363de92018-06-29 13:57:38 -04002617 // error.
David Benjamin1e77ef42019-03-30 01:03:50 -05002618 bool (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl,
2619 uint8_t *out_alert);
David Benjamin0363de92018-06-29 13:57:38 -04002620
2621 // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
2622 void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs);
Adam Langleyffe384c2019-05-01 11:13:12 -07002623 // ssl_new does any necessary initialisation of |hs|. It returns true on
David Benjamin1e77ef42019-03-30 01:03:50 -05002624 // success or false on error.
2625 bool (*ssl_new)(SSL_HANDSHAKE *hs);
David Benjamin0363de92018-06-29 13:57:38 -04002626 // ssl_free frees anything created by |ssl_new|.
2627 void (*ssl_config_free)(SSL_CONFIG *cfg);
2628 // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
2629 void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg);
2630 // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
2631 // necessary. On success, it updates |ssl|'s certificate configuration as
David Benjamin1e77ef42019-03-30 01:03:50 -05002632 // needed and returns true. Otherwise, it returns false.
2633 bool (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs);
Adam Langleyffe384c2019-05-01 11:13:12 -07002634 // ssl_ctx_new does any necessary initialisation of |ctx|. It returns true on
David Benjamin1e77ef42019-03-30 01:03:50 -05002635 // success or false on error.
2636 bool (*ssl_ctx_new)(SSL_CTX *ctx);
David Benjamin0363de92018-06-29 13:57:38 -04002637 // ssl_ctx_free frees anything created by |ssl_ctx_new|.
2638 void (*ssl_ctx_free)(SSL_CTX *ctx);
2639 // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
2640 void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
2641};
2642
David Benjaminc11ea9422017-08-29 16:33:21 -04002643// ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
2644// crypto/x509.
David Benjamin86e95b82017-07-18 16:34:25 -04002645extern const SSL_X509_METHOD ssl_crypto_x509_method;
Adam Langley3509dac2017-02-01 11:59:18 -08002646
David Benjaminc11ea9422017-08-29 16:33:21 -04002647// ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
2648// crypto/x509.
David Benjaminba2d3df2017-08-04 13:59:24 -04002649extern const SSL_X509_METHOD ssl_noop_x509_method;
2650
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002651struct TicketKey {
David Benjamin664e99a2017-10-10 19:23:21 -04002652 static constexpr bool kAllowUniquePtr = true;
2653
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002654 uint8_t name[SSL_TICKET_KEY_NAME_LEN] = {0};
2655 uint8_t hmac_key[16] = {0};
2656 uint8_t aes_key[16] = {0};
David Benjamin664e99a2017-10-10 19:23:21 -04002657 // next_rotation_tv_sec is the time (in seconds from the epoch) when the
2658 // current key should be superseded by a new key, or the time when a previous
2659 // key should be dropped. If zero, then the key should not be automatically
2660 // rotated.
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002661 uint64_t next_rotation_tv_sec = 0;
David Benjamin664e99a2017-10-10 19:23:21 -04002662};
2663
David Benjamin35b4a122018-07-14 17:04:41 -04002664struct CertCompressionAlg {
2665 static constexpr bool kAllowUniquePtr = true;
2666
2667 ssl_cert_compression_func_t compress = nullptr;
2668 ssl_cert_decompression_func_t decompress = nullptr;
2669 uint16_t alg_id = 0;
2670};
2671
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002672BSSL_NAMESPACE_END
David Benjamin4e840352017-10-11 18:41:11 -04002673
David Benjamin1eff9482018-09-24 14:10:29 -05002674DEFINE_LHASH_OF(SSL_SESSION)
David Benjamin4e840352017-10-11 18:41:11 -04002675
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002676BSSL_NAMESPACE_BEGIN
David Benjamin4e840352017-10-11 18:41:11 -04002677
David Benjaminc11ea9422017-08-29 16:33:21 -04002678// An ssl_shutdown_t describes the shutdown state of one end of the connection,
2679// whether it is alive or has been shutdown via close_notify or fatal alert.
David Benjamine228bd22016-10-17 20:41:08 -04002680enum ssl_shutdown_t {
2681 ssl_shutdown_none = 0,
2682 ssl_shutdown_close_notify = 1,
David Benjamin31aad2d2017-10-06 16:04:06 -04002683 ssl_shutdown_error = 2,
David Benjamine228bd22016-10-17 20:41:08 -04002684};
2685
David Benjaminba423c92021-06-15 16:26:58 -04002686enum ssl_ech_status_t {
2687 // ssl_ech_none indicates ECH was not offered, or we have not gotten far
2688 // enough in the handshake to determine the status.
2689 ssl_ech_none,
2690 // ssl_ech_accepted indicates the server accepted ECH.
2691 ssl_ech_accepted,
2692 // ssl_ech_rejected indicates the server was offered ECH but rejected it.
2693 ssl_ech_rejected,
2694};
2695
David Benjamin86e95b82017-07-18 16:34:25 -04002696struct SSL3_STATE {
David Benjamin94172572017-10-13 16:53:21 -04002697 static constexpr bool kAllowUniquePtr = true;
David Benjamine228bd22016-10-17 20:41:08 -04002698
David Benjamin94172572017-10-13 16:53:21 -04002699 SSL3_STATE();
2700 ~SSL3_STATE();
2701
David Benjamin32013e82022-09-22 16:55:34 -04002702 uint64_t read_sequence = 0;
2703 uint64_t write_sequence = 0;
David Benjamin94172572017-10-13 16:53:21 -04002704
2705 uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
2706 uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
David Benjamine228bd22016-10-17 20:41:08 -04002707
David Benjaminc11ea9422017-08-29 16:33:21 -04002708 // read_buffer holds data from the transport to be processed.
David Benjaminea712e32017-10-13 16:50:39 -04002709 SSLBuffer read_buffer;
David Benjaminc11ea9422017-08-29 16:33:21 -04002710 // write_buffer holds data to be written to the transport.
David Benjaminea712e32017-10-13 16:50:39 -04002711 SSLBuffer write_buffer;
David Benjamine228bd22016-10-17 20:41:08 -04002712
David Benjamind9229f92017-10-06 17:36:20 -04002713 // pending_app_data is the unconsumed application data. It points into
2714 // |read_buffer|.
2715 Span<uint8_t> pending_app_data;
David Benjamine228bd22016-10-17 20:41:08 -04002716
David Benjamin4bd32a82021-05-14 17:06:29 -04002717 // unreported_bytes_written is the number of bytes successfully written to the
2718 // transport, but not yet reported to the caller. The next |SSL_write| will
2719 // skip this many bytes from the input. This is used if
2720 // |SSL_MODE_ENABLE_PARTIAL_WRITE| is disabled, in which case |SSL_write| only
2721 // reports bytes written when the full caller input is written.
2722 size_t unreported_bytes_written = 0;
2723
2724 // pending_write, if |has_pending_write| is true, is the caller-supplied data
2725 // corresponding to the current pending write. This is used to check the
2726 // caller retried with a compatible buffer.
2727 Span<const uint8_t> pending_write;
2728
2729 // pending_write_type, if |has_pending_write| is true, is the record type
2730 // for the current pending write.
2731 //
2732 // TODO(davidben): Remove this when alerts are moved out of this write path.
2733 uint8_t pending_write_type = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002734
David Benjamin23c25d52017-10-06 16:02:47 -04002735 // read_shutdown is the shutdown state for the read half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002736 enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002737
David Benjamin23c25d52017-10-06 16:02:47 -04002738 // write_shutdown is the shutdown state for the write half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002739 enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002740
David Benjamin31aad2d2017-10-06 16:04:06 -04002741 // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
2742 // the receive half of the connection.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002743 UniquePtr<ERR_SAVE_STATE> read_error;
David Benjamin31aad2d2017-10-06 16:04:06 -04002744
David Benjamin94172572017-10-13 16:53:21 -04002745 int total_renegotiations = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002746
David Benjamin32ce0ac2017-10-13 19:17:22 -04002747 // This holds a variable that indicates what we were doing when a 0 or -1 is
2748 // returned. This is needed for non-blocking IO so we know what request
2749 // needs re-doing when in SSL_accept or SSL_connect
David Benjaminf4928302019-08-21 16:04:53 -04002750 int rwstate = SSL_ERROR_NONE;
David Benjamin32ce0ac2017-10-13 19:17:22 -04002751
Steven Valdezc8e0f902018-07-14 11:23:01 -04002752 enum ssl_encryption_level_t read_level = ssl_encryption_initial;
2753 enum ssl_encryption_level_t write_level = ssl_encryption_initial;
2754
David Benjaminc11ea9422017-08-29 16:33:21 -04002755 // early_data_skipped is the amount of early data that has been skipped by the
2756 // record layer.
David Benjamin94172572017-10-13 16:53:21 -04002757 uint16_t early_data_skipped = 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002758
David Benjaminc11ea9422017-08-29 16:33:21 -04002759 // empty_record_count is the number of consecutive empty records received.
David Benjamin94172572017-10-13 16:53:21 -04002760 uint8_t empty_record_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002761
David Benjaminc11ea9422017-08-29 16:33:21 -04002762 // warning_alert_count is the number of consecutive warning alerts
2763 // received.
David Benjamin94172572017-10-13 16:53:21 -04002764 uint8_t warning_alert_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002765
David Benjaminc11ea9422017-08-29 16:33:21 -04002766 // key_update_count is the number of consecutive KeyUpdates received.
David Benjamin94172572017-10-13 16:53:21 -04002767 uint8_t key_update_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002768
David Benjaminba423c92021-06-15 16:26:58 -04002769 // ech_status indicates whether ECH was accepted by the server.
2770 ssl_ech_status_t ech_status = ssl_ech_none;
David Benjamin3a036c72021-06-02 12:17:16 -04002771
David Benjaminc11ea9422017-08-29 16:33:21 -04002772 // skip_early_data instructs the record layer to skip unexpected early data
2773 // messages when 0RTT is rejected.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002774 bool skip_early_data : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002775
David Benjaminc11ea9422017-08-29 16:33:21 -04002776 // have_version is true if the connection's final version is known. Otherwise
2777 // the version has not been negotiated yet.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002778 bool have_version : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002779
David Benjaminc11ea9422017-08-29 16:33:21 -04002780 // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
2781 // and future messages should use the record layer.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002782 bool v2_hello_done : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002783
David Benjaminc11ea9422017-08-29 16:33:21 -04002784 // is_v2_hello is true if the current handshake message was derived from a
2785 // V2ClientHello rather than received from the peer directly.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002786 bool is_v2_hello : 1;
David Benjamin1a444da2017-01-21 14:27:45 -05002787
David Benjaminc11ea9422017-08-29 16:33:21 -04002788 // has_message is true if the current handshake message has been returned
2789 // at least once by |get_message| and false otherwise.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002790 bool has_message : 1;
David Benjamin7934f082017-08-01 16:32:25 -04002791
David Benjaminc11ea9422017-08-29 16:33:21 -04002792 // initial_handshake_complete is true if the initial handshake has
2793 // completed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002794 bool initial_handshake_complete : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002795
David Benjaminc11ea9422017-08-29 16:33:21 -04002796 // session_reused indicates whether a session was resumed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002797 bool session_reused : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002798
Watson Ladd629f3212019-06-11 17:46:52 -07002799 // delegated_credential_used is whether we presented a delegated credential to
2800 // the peer.
2801 bool delegated_credential_used : 1;
2802
Jesse Selover6b0d8222018-08-14 12:47:29 -04002803 bool send_connection_binding : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002804
David Benjamin8acec002021-05-19 13:03:34 -04002805 // channel_id_valid is true if, on the server, the client has negotiated a
2806 // Channel ID and the |channel_id| field is filled in.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002807 bool channel_id_valid : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002808
David Benjamin046bc1f2017-08-31 15:06:42 -04002809 // key_update_pending is true if we have a KeyUpdate acknowledgment
David Benjaminc11ea9422017-08-29 16:33:21 -04002810 // outstanding.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002811 bool key_update_pending : 1;
David Benjaminbbba9392017-04-06 12:54:12 -04002812
David Benjamin02e62562017-12-18 18:04:01 -05002813 // early_data_accepted is true if early data was accepted by the server.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002814 bool early_data_accepted : 1;
David Benjamin02e62562017-12-18 18:04:01 -05002815
David Benjaminee4888c2019-07-21 12:16:52 -04002816 // alert_dispatch is true there is an alert in |send_alert| to be sent.
2817 bool alert_dispatch : 1;
2818
David Benjamin0e7dbd52019-05-15 16:01:18 -04002819 // renegotiate_pending is whether the read half of the channel is blocked on a
2820 // HelloRequest.
2821 bool renegotiate_pending : 1;
2822
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002823 // used_hello_retry_request is whether the handshake used a TLS 1.3
2824 // HelloRetryRequest message.
2825 bool used_hello_retry_request : 1;
2826
David Benjamina614d462022-12-02 15:30:15 -05002827 // was_key_usage_invalid is whether the handshake succeeded despite using a
2828 // TLS mode which was incompatible with the leaf certificate's keyUsage
2829 // extension.
2830 bool was_key_usage_invalid : 1;
2831
David Benjamin32ce0ac2017-10-13 19:17:22 -04002832 // hs_buf is the buffer of handshake data to process.
2833 UniquePtr<BUF_MEM> hs_buf;
2834
David Benjamin700631b2018-05-24 17:17:34 -04002835 // pending_hs_data contains the pending handshake data that has not yet
2836 // been encrypted to |pending_flight|. This allows packing the handshake into
2837 // fewer records.
2838 UniquePtr<BUF_MEM> pending_hs_data;
2839
David Benjaminc11ea9422017-08-29 16:33:21 -04002840 // pending_flight is the pending outgoing flight. This is used to flush each
2841 // handshake flight in a single write. |write_buffer| must be written out
2842 // before this data.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002843 UniquePtr<BUF_MEM> pending_flight;
David Benjamindaf207a2017-01-03 18:37:41 -05002844
David Benjaminc11ea9422017-08-29 16:33:21 -04002845 // pending_flight_offset is the number of bytes of |pending_flight| which have
2846 // been successfully written.
David Benjamin94172572017-10-13 16:53:21 -04002847 uint32_t pending_flight_offset = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002848
Adam Langley2f9b47f2017-12-29 14:15:55 -08002849 // ticket_age_skew is the difference, in seconds, between the client-sent
2850 // ticket age and the server-computed value in TLS 1.3 server connections
2851 // which resumed a session.
2852 int32_t ticket_age_skew = 0;
2853
David Benjamin64770122019-05-04 11:00:04 -05002854 // ssl_early_data_reason stores details on why 0-RTT was accepted or rejected.
2855 enum ssl_early_data_reason_t early_data_reason = ssl_early_data_unknown;
2856
David Benjaminc11ea9422017-08-29 16:33:21 -04002857 // aead_read_ctx is the current read cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002858 UniquePtr<SSLAEADContext> aead_read_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002859
David Benjaminc11ea9422017-08-29 16:33:21 -04002860 // aead_write_ctx is the current write cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002861 UniquePtr<SSLAEADContext> aead_write_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002862
David Benjaminc11ea9422017-08-29 16:33:21 -04002863 // hs is the handshake state for the current handshake or NULL if there isn't
2864 // one.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002865 UniquePtr<SSL_HANDSHAKE> hs;
David Benjamine228bd22016-10-17 20:41:08 -04002866
David Benjaminbc424022019-07-21 12:05:20 -04002867 uint8_t write_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2868 uint8_t read_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2869 uint8_t exporter_secret[SSL_MAX_MD_SIZE] = {0};
David Benjamin94172572017-10-13 16:53:21 -04002870 uint8_t write_traffic_secret_len = 0;
2871 uint8_t read_traffic_secret_len = 0;
2872 uint8_t exporter_secret_len = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002873
David Benjaminc11ea9422017-08-29 16:33:21 -04002874 // Connection binding to prevent renegotiation attacks
David Benjamin94172572017-10-13 16:53:21 -04002875 uint8_t previous_client_finished[12] = {0};
2876 uint8_t previous_client_finished_len = 0;
2877 uint8_t previous_server_finished_len = 0;
2878 uint8_t previous_server_finished[12] = {0};
Adam Langley4ba6e192016-12-07 15:54:54 -08002879
Adam Langley2f9b47f2017-12-29 14:15:55 -08002880 uint8_t send_alert[2] = {0};
2881
David Benjaminc11ea9422017-08-29 16:33:21 -04002882 // established_session is the session established by the connection. This
2883 // session is only filled upon the completion of the handshake and is
2884 // immutable.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002885 UniquePtr<SSL_SESSION> established_session;
David Benjamine228bd22016-10-17 20:41:08 -04002886
David Benjaminc11ea9422017-08-29 16:33:21 -04002887 // Next protocol negotiation. For the client, this is the protocol that we
2888 // sent in NextProtocol and is set when handling ServerHello extensions.
2889 //
2890 // For a server, this is the client's selected_protocol from NextProtocol and
2891 // is set when handling the NextProtocol message, before the Finished
2892 // message.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002893 Array<uint8_t> next_proto_negotiated;
David Benjamine228bd22016-10-17 20:41:08 -04002894
David Benjaminc11ea9422017-08-29 16:33:21 -04002895 // ALPN information
2896 // (we are in the process of transitioning from NPN to ALPN.)
David Benjamine228bd22016-10-17 20:41:08 -04002897
David Benjaminc11ea9422017-08-29 16:33:21 -04002898 // In a server these point to the selected ALPN protocol after the
2899 // ClientHello has been processed. In a client these contain the protocol
2900 // that the server selected once the ServerHello has been processed.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002901 Array<uint8_t> alpn_selected;
David Benjamine228bd22016-10-17 20:41:08 -04002902
David Benjamina8614602017-09-06 15:40:19 -04002903 // hostname, on the server, is the value of the SNI extension.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002904 UniquePtr<char> hostname;
David Benjamina8614602017-09-06 15:40:19 -04002905
David Benjaminc11ea9422017-08-29 16:33:21 -04002906 // For a server:
David Benjamin46853762018-07-03 14:01:26 -04002907 // If |channel_id_valid| is true, then this contains the
David Benjaminc11ea9422017-08-29 16:33:21 -04002908 // verified Channel ID from the client: a P256 point, (x,y), where
2909 // each are big-endian values.
David Benjamin46853762018-07-03 14:01:26 -04002910 uint8_t channel_id[64] = {0};
Nick Harper3c034b22017-12-22 15:50:43 -08002911
2912 // Contains the QUIC transport params received by the peer.
2913 Array<uint8_t> peer_quic_transport_params;
David Benjaminfceca8e2018-04-12 16:37:19 -04002914
2915 // srtp_profile is the selected SRTP protection profile for
2916 // DTLS-SRTP.
2917 const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002918};
David Benjamine228bd22016-10-17 20:41:08 -04002919
David Benjaminc11ea9422017-08-29 16:33:21 -04002920// lengths of messages
David Benjamin593047f2015-05-08 13:08:52 -04002921#define DTLS1_RT_HEADER_LENGTH 13
2922
2923#define DTLS1_HM_HEADER_LENGTH 12
2924
2925#define DTLS1_CCS_HEADER_LENGTH 1
2926
2927#define DTLS1_AL_HEADER_LENGTH 2
2928
David Benjamin593047f2015-05-08 13:08:52 -04002929struct hm_header_st {
2930 uint8_t type;
2931 uint32_t msg_len;
2932 uint16_t seq;
2933 uint32_t frag_off;
2934 uint32_t frag_len;
David Benjamin593047f2015-05-08 13:08:52 -04002935};
2936
David Benjaminc11ea9422017-08-29 16:33:21 -04002937// An hm_fragment is an incoming DTLS message, possibly not yet assembled.
David Benjamin86e95b82017-07-18 16:34:25 -04002938struct hm_fragment {
David Benjamin049fdfc2017-10-17 01:12:53 -04002939 static constexpr bool kAllowUniquePtr = true;
2940
2941 hm_fragment() {}
2942 hm_fragment(const hm_fragment &) = delete;
2943 hm_fragment &operator=(const hm_fragment &) = delete;
2944
2945 ~hm_fragment();
2946
David Benjaminc11ea9422017-08-29 16:33:21 -04002947 // type is the type of the message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002948 uint8_t type = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002949 // seq is the sequence number of this message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002950 uint16_t seq = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002951 // msg_len is the length of the message body.
David Benjamin049fdfc2017-10-17 01:12:53 -04002952 uint32_t msg_len = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002953 // data is a pointer to the message, including message header. It has length
2954 // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
David Benjamin049fdfc2017-10-17 01:12:53 -04002955 uint8_t *data = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04002956 // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
2957 // the message have been received. It is NULL if the message is complete.
David Benjamin049fdfc2017-10-17 01:12:53 -04002958 uint8_t *reassembly = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002959};
David Benjamin593047f2015-05-08 13:08:52 -04002960
David Benjaminad8f5e12017-02-20 17:00:20 -05002961struct OPENSSL_timeval {
2962 uint64_t tv_sec;
2963 uint32_t tv_usec;
2964};
2965
David Benjamin86e95b82017-07-18 16:34:25 -04002966struct DTLS1_STATE {
David Benjamin800046f2017-10-17 01:37:51 -04002967 static constexpr bool kAllowUniquePtr = true;
2968
2969 DTLS1_STATE();
2970 ~DTLS1_STATE();
2971
David Benjaminc11ea9422017-08-29 16:33:21 -04002972 // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
2973 // the peer in this epoch.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002974 bool has_change_cipher_spec : 1;
David Benjamin593047f2015-05-08 13:08:52 -04002975
David Benjaminc11ea9422017-08-29 16:33:21 -04002976 // outgoing_messages_complete is true if |outgoing_messages| has been
2977 // completed by an attempt to flush it. Future calls to |add_message| and
2978 // |add_change_cipher_spec| will start a new flight.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002979 bool outgoing_messages_complete : 1;
David Benjamin9bbdf582017-08-02 19:46:29 -04002980
David Benjaminc11ea9422017-08-29 16:33:21 -04002981 // flight_has_reply is true if the current outgoing flight is complete and has
2982 // processed at least one message. This is used to detect whether we or the
2983 // peer sent the final flight.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002984 bool flight_has_reply : 1;
David Benjamin302b8182017-08-22 14:47:22 -07002985
David Benjaminc11ea9422017-08-29 16:33:21 -04002986 // The current data and handshake epoch. This is initially undefined, and
2987 // starts at zero once the initial handshake is completed.
David Benjamin800046f2017-10-17 01:37:51 -04002988 uint16_t r_epoch = 0;
2989 uint16_t w_epoch = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002990
David Benjaminc11ea9422017-08-29 16:33:21 -04002991 // records being received in the current epoch
David Benjamin593047f2015-05-08 13:08:52 -04002992 DTLS1_BITMAP bitmap;
2993
David Benjamin800046f2017-10-17 01:37:51 -04002994 uint16_t handshake_write_seq = 0;
2995 uint16_t handshake_read_seq = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002996
David Benjaminc11ea9422017-08-29 16:33:21 -04002997 // save last sequence number for retransmissions
David Benjamin32013e82022-09-22 16:55:34 -04002998 uint64_t last_write_sequence = 0;
David Benjamin800046f2017-10-17 01:37:51 -04002999 UniquePtr<SSLAEADContext> last_aead_write_ctx;
David Benjamin593047f2015-05-08 13:08:52 -04003000
David Benjaminc11ea9422017-08-29 16:33:21 -04003001 // incoming_messages is a ring buffer of incoming handshake messages that have
3002 // yet to be processed. The front of the ring buffer is message number
3003 // |handshake_read_seq|, at position |handshake_read_seq| %
3004 // |SSL_MAX_HANDSHAKE_FLIGHT|.
David Benjamin800046f2017-10-17 01:37:51 -04003005 UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin593047f2015-05-08 13:08:52 -04003006
David Benjaminc11ea9422017-08-29 16:33:21 -04003007 // outgoing_messages is the queue of outgoing messages from the last handshake
3008 // flight.
David Benjamin29a83c52016-06-17 19:12:54 -04003009 DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin800046f2017-10-17 01:37:51 -04003010 uint8_t outgoing_messages_len = 0;
David Benjamin593047f2015-05-08 13:08:52 -04003011
David Benjaminc11ea9422017-08-29 16:33:21 -04003012 // outgoing_written is the number of outgoing messages that have been
3013 // written.
David Benjamin800046f2017-10-17 01:37:51 -04003014 uint8_t outgoing_written = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04003015 // outgoing_offset is the number of bytes of the next outgoing message have
3016 // been written.
David Benjamin800046f2017-10-17 01:37:51 -04003017 uint32_t outgoing_offset = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -05003018
David Benjamin800046f2017-10-17 01:37:51 -04003019 unsigned mtu = 0; // max DTLS packet size
David Benjamin593047f2015-05-08 13:08:52 -04003020
David Benjaminc11ea9422017-08-29 16:33:21 -04003021 // num_timeouts is the number of times the retransmit timer has fired since
3022 // the last time it was reset.
David Benjamin800046f2017-10-17 01:37:51 -04003023 unsigned num_timeouts = 0;
David Benjamin593047f2015-05-08 13:08:52 -04003024
David Benjaminc11ea9422017-08-29 16:33:21 -04003025 // Indicates when the last handshake msg or heartbeat sent will
3026 // timeout.
David Benjamin800046f2017-10-17 01:37:51 -04003027 struct OPENSSL_timeval next_timeout = {0, 0};
David Benjamin593047f2015-05-08 13:08:52 -04003028
David Benjaminc11ea9422017-08-29 16:33:21 -04003029 // timeout_duration_ms is the timeout duration in milliseconds.
David Benjamin800046f2017-10-17 01:37:51 -04003030 unsigned timeout_duration_ms = 0;
David Benjamin86e95b82017-07-18 16:34:25 -04003031};
David Benjamin593047f2015-05-08 13:08:52 -04003032
Steven Valdez51607f12020-08-05 10:46:05 -04003033// An ALPSConfig is a pair of ALPN protocol and settings value to use with ALPS.
3034struct ALPSConfig {
3035 Array<uint8_t> protocol;
3036 Array<uint8_t> settings;
3037};
3038
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003039// SSL_CONFIG contains configuration bits that can be shed after the handshake
3040// completes. Objects of this type are not shared; they are unique to a
3041// particular |SSL|.
3042//
3043// See SSL_shed_handshake_config() for more about the conditions under which
3044// configuration can be shed.
3045struct SSL_CONFIG {
David Benjamin50596f82018-07-02 19:47:27 -04003046 static constexpr bool kAllowUniquePtr = true;
3047
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003048 explicit SSL_CONFIG(SSL *ssl_arg);
3049 ~SSL_CONFIG();
David Benjamin3f2611a2017-02-06 13:38:26 -05003050
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003051 // ssl is a non-owning pointer to the parent |SSL| object.
3052 SSL *const ssl = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05003053
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -07003054 // conf_max_version is the maximum acceptable version configured by
3055 // |SSL_set_max_proto_version|. Note this version is not normalized in DTLS
3056 // and is further constrained by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003057 uint16_t conf_max_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05003058
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -07003059 // conf_min_version is the minimum acceptable version configured by
3060 // |SSL_set_min_proto_version|. Note this version is not normalized in DTLS
3061 // and is further constrained by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003062 uint16_t conf_min_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05003063
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003064 X509_VERIFY_PARAM *param = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05003065
David Benjaminc11ea9422017-08-29 16:33:21 -04003066 // crypto
David Benjamin0ce090a2018-07-02 20:24:40 -04003067 UniquePtr<SSLCipherPreferenceList> cipher_list;
David Benjamin3f2611a2017-02-06 13:38:26 -05003068
Adam Langley3fe8fa72018-01-26 09:14:30 -08003069 // This is used to hold the local certificate used (i.e. the server
3070 // certificate for a server or the client certificate for a client).
David Benjamin0ce090a2018-07-02 20:24:40 -04003071 UniquePtr<CERT> cert;
David Benjamin3f2611a2017-02-06 13:38:26 -05003072
3073 int (*verify_callback)(int ok,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003074 X509_STORE_CTX *ctx) =
3075 nullptr; // fail if callback returns 0
David Benjamin3f2611a2017-02-06 13:38:26 -05003076
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003077 enum ssl_verify_result_t (*custom_verify_callback)(
3078 SSL *ssl, uint8_t *out_alert) = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04003079 // Server-only: psk_identity_hint is the identity hint to send in
3080 // PSK-based key exchanges.
David Benjamin0ce090a2018-07-02 20:24:40 -04003081 UniquePtr<char> psk_identity_hint;
David Benjamin3f2611a2017-02-06 13:38:26 -05003082
David Benjamin0ce090a2018-07-02 20:24:40 -04003083 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
3084 unsigned max_identity_len, uint8_t *psk,
3085 unsigned max_psk_len) = nullptr;
3086 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
3087 unsigned max_psk_len) = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05003088
David Benjaminc11ea9422017-08-29 16:33:21 -04003089 // for server side, keep the list of CA_dn we can use
David Benjamin0ce090a2018-07-02 20:24:40 -04003090 UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
Adam Langley34b4c822017-02-02 10:57:17 -08003091
David Benjaminc11ea9422017-08-29 16:33:21 -04003092 // cached_x509_client_CA is a cache of parsed versions of the elements of
3093 // |client_CA|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003094 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05003095
David Benjamin0ce090a2018-07-02 20:24:40 -04003096 Array<uint16_t> supported_group_list; // our list
David Benjamin3f2611a2017-02-06 13:38:26 -05003097
David Benjaminb5879112021-05-18 17:10:18 -04003098 // channel_id_private is the client's Channel ID private key, or null if
3099 // Channel ID should not be offered on this connection.
David Benjamin46853762018-07-03 14:01:26 -04003100 UniquePtr<EVP_PKEY> channel_id_private;
David Benjamin3f2611a2017-02-06 13:38:26 -05003101
David Benjaminc11ea9422017-08-29 16:33:21 -04003102 // For a client, this contains the list of supported protocols in wire
3103 // format.
David Benjamin0ce090a2018-07-02 20:24:40 -04003104 Array<uint8_t> alpn_client_proto_list;
David Benjamin3f2611a2017-02-06 13:38:26 -05003105
Steven Valdez51607f12020-08-05 10:46:05 -04003106 // alps_configs contains the list of supported protocols to use with ALPS,
3107 // along with their corresponding ALPS values.
3108 GrowableArray<ALPSConfig> alps_configs;
3109
Nick Harper3c034b22017-12-22 15:50:43 -08003110 // Contains the QUIC transport params that this endpoint will send.
David Benjamin0ce090a2018-07-02 20:24:40 -04003111 Array<uint8_t> quic_transport_params;
David Benjamin3f2611a2017-02-06 13:38:26 -05003112
Nick Harper7c522992020-04-30 14:15:49 -07003113 // Contains the context used to decide whether to accept early data in QUIC.
3114 Array<uint8_t> quic_early_data_context;
3115
Adam Langley826ce152018-08-03 10:31:21 -07003116 // verify_sigalgs, if not empty, is the set of signature algorithms
3117 // accepted from the peer in decreasing order of preference.
3118 Array<uint16_t> verify_sigalgs;
3119
David Benjaminb95d4b42018-05-02 16:11:34 -04003120 // srtp_profiles is the list of configured SRTP protection profiles for
3121 // DTLS-SRTP.
David Benjamin0ce090a2018-07-02 20:24:40 -04003122 UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
David Benjaminb95d4b42018-05-02 16:11:34 -04003123
David Benjamin83a49932021-05-20 15:57:09 -04003124 // client_ech_config_list, if not empty, is a serialized ECHConfigList
3125 // structure for the client to use when negotiating ECH.
3126 Array<uint8_t> client_ech_config_list;
3127
Adam Langley2f6409e2023-04-10 21:09:11 +00003128 // tls13_cipher_policy limits the set of ciphers that can be selected when
3129 // negotiating a TLS 1.3 connection.
3130 enum ssl_compliance_policy_t tls13_cipher_policy = ssl_compliance_policy_none;
3131
David Benjaminc11ea9422017-08-29 16:33:21 -04003132 // verify_mode is a bitmask of |SSL_VERIFY_*| values.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003133 uint8_t verify_mode = SSL_VERIFY_NONE;
David Benjamin3f2611a2017-02-06 13:38:26 -05003134
Dan McArdle1920c6f2020-03-11 17:29:40 -04003135 // ech_grease_enabled controls whether ECH GREASE may be sent in the
3136 // ClientHello.
3137 bool ech_grease_enabled : 1;
3138
David Benjaminc11ea9422017-08-29 16:33:21 -04003139 // Enable signed certificate time stamps. Currently client only.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003140 bool signed_cert_timestamps_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05003141
David Benjaminc11ea9422017-08-29 16:33:21 -04003142 // ocsp_stapling_enabled is only used by client connections and indicates
3143 // whether OCSP stapling will be requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003144 bool ocsp_stapling_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05003145
David Benjaminb5879112021-05-18 17:10:18 -04003146 // channel_id_enabled is copied from the |SSL_CTX|. For a server, it means
3147 // that we'll accept Channel IDs from clients. It is ignored on the client.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003148 bool channel_id_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05003149
Jesse Seloverd7266ec2019-01-30 16:06:10 -05003150 // If enforce_rsa_key_usage is true, the handshake will fail if the
3151 // keyUsage extension is present and incompatible with the TLS usage.
3152 // This field is not read until after certificate verification.
3153 bool enforce_rsa_key_usage : 1;
3154
David Benjaminc11ea9422017-08-29 16:33:21 -04003155 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
3156 // hash of the peer's certificate and then discard it to save memory and
3157 // session space. Only effective on the server side.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003158 bool retain_only_sha256_of_client_certs : 1;
Adam Langley3fe8fa72018-01-26 09:14:30 -08003159
3160 // handoff indicates that a server should stop after receiving the
3161 // ClientHello and pause the handshake in such a way that |SSL_get_error|
David Benjaminf4928302019-08-21 16:04:53 -04003162 // returns |SSL_ERROR_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX|
Adam Langley3fe8fa72018-01-26 09:14:30 -08003163 // element of the same name and may be cleared if the handoff is declined.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003164 bool handoff : 1;
Adam Langley8df8e642018-02-28 13:35:39 -08003165
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003166 // shed_handshake_config indicates that the handshake config (this object!)
3167 // should be freed after the handshake completes.
3168 bool shed_handshake_config : 1;
David Benjamin2d98d492018-10-08 17:31:17 -05003169
David Benjamin6965d252018-11-19 15:49:56 -06003170 // jdk11_workaround is whether to disable TLS 1.3 for JDK 11 clients, as a
3171 // workaround for https://bugs.openjdk.java.net/browse/JDK-8211806.
3172 bool jdk11_workaround : 1;
David Schinazi3d8b8c32021-01-14 11:25:49 -08003173
3174 // QUIC drafts up to and including 32 used a different TLS extension
3175 // codepoint to convey QUIC's transport parameters.
3176 bool quic_use_legacy_codepoint : 1;
David Benjamine9c5d722021-06-09 17:43:16 -04003177
3178 // permute_extensions is whether to permute extensions when sending messages.
3179 bool permute_extensions : 1;
Adam Langley451ea3c2022-05-18 16:15:49 -07003180
Bob Beck8cacbd92023-04-05 14:38:38 -06003181 // aes_hw_override if set indicates we should override checking for aes
3182 // hardware support, and use the value in aes_hw_override_value instead.
3183 bool aes_hw_override : 1;
3184
3185 // aes_hw_override_value is used for testing to indicate the support or lack
3186 // of support for AES hw. The value is only considered if |aes_hw_override| is
3187 // true.
3188 bool aes_hw_override_value : 1;
Victor Tan558960d2023-06-23 15:04:33 +00003189
3190 // alps_use_new_codepoint if set indicates we use new ALPS extension codepoint
3191 // to negotiate and convey application settings.
3192 bool alps_use_new_codepoint : 1;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003193};
3194
David Benjamina130ce02018-08-14 22:26:39 -05003195// From RFC 8446, used in determining PSK modes.
Steven Valdeza833c352016-11-01 13:39:36 -04003196#define SSL_PSK_DHE_KE 0x1
Steven Valdez1e6f11a2016-07-27 11:10:52 -04003197
David Benjaminc11ea9422017-08-29 16:33:21 -04003198// kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
3199// data that will be accepted. This value should be slightly below
3200// kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
Alessandro Ghedinide254b42017-04-17 19:12:33 +01003201static const size_t kMaxEarlyDataAccepted = 14336;
3202
David Benjamine325c3f2018-04-12 16:11:15 -04003203UniquePtr<CERT> ssl_cert_dup(CERT *cert);
David Benjamin8525ff32018-09-05 18:44:15 -05003204bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
3205bool ssl_is_key_type_supported(int key_type);
3206// ssl_compare_public_and_private_key returns true if |pubkey| is the public
3207// counterpart to |privkey|. Otherwise it returns false and pushes a helpful
David Benjaminc11ea9422017-08-29 16:33:21 -04003208// message on the error queue.
David Benjamin8525ff32018-09-05 18:44:15 -05003209bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
Adam Langleyc26692c2017-01-25 09:34:42 -08003210 const EVP_PKEY *privkey);
David Benjamin962b3752021-05-10 15:17:18 -04003211bool ssl_get_new_session(SSL_HANDSHAKE *hs);
David Benjamin6e723e52023-03-29 19:28:46 +09003212bool ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out,
3213 const SSL_SESSION *session);
3214bool ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04003215
David Benjaminc11ea9422017-08-29 16:33:21 -04003216// ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
3217// error.
David Benjamin31b0c9b2017-07-20 14:49:15 -04003218UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
Adam Langley46db7af2017-02-01 15:49:37 -08003219
David Benjamin58150ed2018-06-29 23:58:43 -04003220// ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table
3221// keyed on session IDs.
3222uint32_t ssl_hash_session_id(Span<const uint8_t> session_id);
3223
David Benjaminc11ea9422017-08-29 16:33:21 -04003224// SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
3225// the parsed data.
Matthew Braithwaite6d597a32018-07-11 14:58:22 -07003226OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_parse(
3227 CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool);
Adam Langley46db7af2017-02-01 15:49:37 -08003228
Adam Langleye8d24392018-01-25 15:49:37 -08003229// ssl_session_serialize writes |in| to |cbb| as if it were serialising a
David Benjamin6e723e52023-03-29 19:28:46 +09003230// session for Session-ID resumption. It returns true on success and false on
Adam Langleye8d24392018-01-25 15:49:37 -08003231// error.
David Benjamin6e723e52023-03-29 19:28:46 +09003232OPENSSL_EXPORT bool ssl_session_serialize(const SSL_SESSION *in, CBB *cbb);
Adam Langleye8d24392018-01-25 15:49:37 -08003233
David Benjamin6e723e52023-03-29 19:28:46 +09003234// ssl_session_is_context_valid returns whether |session|'s session ID context
3235// matches the one set on |hs|.
3236bool ssl_session_is_context_valid(const SSL_HANDSHAKE *hs,
3237 const SSL_SESSION *session);
Steven Valdez4aa154e2016-07-29 14:32:55 -04003238
David Benjamin6e723e52023-03-29 19:28:46 +09003239// ssl_session_is_time_valid returns true if |session| is still valid and false
3240// if it has expired.
3241bool ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
Steven Valdez4aa154e2016-07-29 14:32:55 -04003242
David Benjamin6e723e52023-03-29 19:28:46 +09003243// ssl_session_is_resumable returns whether |session| is resumable for |hs|.
3244bool ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
3245 const SSL_SESSION *session);
David Benjamin75f99142016-11-12 12:36:06 +09003246
David Benjamina4bafd32017-10-03 15:06:29 -04003247// ssl_session_protocol_version returns the protocol version associated with
3248// |session|. Note that despite the name, this is not the same as
3249// |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
3250uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
Steven Valdez8f36c512017-06-20 10:55:02 -04003251
David Benjamina4bafd32017-10-03 15:06:29 -04003252// ssl_session_get_digest returns the digest used in |session|.
3253const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
Steven Valdez908ac192017-01-12 13:17:07 -05003254
David Benjamin4d0be242016-09-01 01:10:07 -04003255void ssl_set_session(SSL *ssl, SSL_SESSION *session);
3256
David Benjaminc11ea9422017-08-29 16:33:21 -04003257// ssl_get_prev_session looks up the previous session based on |client_hello|.
3258// On success, it sets |*out_session| to the session or nullptr if none was
3259// found. If the session could not be looked up synchronously, it returns
3260// |ssl_hs_pending_session| and should be called again. If a ticket could not be
3261// decrypted immediately it returns |ssl_hs_pending_ticket| and should also
3262// be called again. Otherwise, it returns |ssl_hs_error|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003263enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs,
Steven Valdez398085b2017-08-29 13:25:56 -04003264 UniquePtr<SSL_SESSION> *out_session,
David Benjaminfd45ee72017-08-31 14:49:09 -04003265 bool *out_tickets_supported,
3266 bool *out_renew_ticket,
Steven Valdez398085b2017-08-29 13:25:56 -04003267 const SSL_CLIENT_HELLO *client_hello);
David Benjamine3aa1d92015-06-16 15:34:50 -04003268
David Benjaminc11ea9422017-08-29 16:33:21 -04003269// The following flags determine which parts of the session are duplicated.
Steven Valdez4aa154e2016-07-29 14:32:55 -04003270#define SSL_SESSION_DUP_AUTH_ONLY 0x0
3271#define SSL_SESSION_INCLUDE_TICKET 0x1
3272#define SSL_SESSION_INCLUDE_NONAUTH 0x2
3273#define SSL_SESSION_DUP_ALL \
3274 (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
3275
David Benjaminc11ea9422017-08-29 16:33:21 -04003276// SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
3277// fields in |session| or nullptr on error. The new session is non-resumable and
3278// must be explicitly marked resumable once it has been filled in.
David Benjamin31b0c9b2017-07-20 14:49:15 -04003279OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
3280 int dup_flags);
Steven Valdez87eab492016-06-27 16:34:59 -04003281
David Benjaminc11ea9422017-08-29 16:33:21 -04003282// ssl_session_rebase_time updates |session|'s start time to the current time,
3283// adjusting the timeout so the expiration time is unchanged.
David Benjamin17b30832017-01-28 14:00:32 -05003284void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
3285
David Benjaminc11ea9422017-08-29 16:33:21 -04003286// ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
3287// |session|'s timeout to |timeout| (measured from the current time). The
3288// renewal is clamped to the session's auth_timeout.
David Benjaminad8f5e12017-02-20 17:00:20 -05003289void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
3290 uint32_t timeout);
David Benjamin123db572016-11-03 16:59:25 -04003291
David Benjamina10017c2021-06-16 16:00:13 -04003292void ssl_update_cache(SSL *ssl);
David Benjaminf31e6812014-11-13 18:05:55 -05003293
David Benjaminb29e1e12019-05-06 14:44:46 -05003294void ssl_send_alert(SSL *ssl, int level, int desc);
3295int ssl_send_alert_impl(SSL *ssl, int level, int desc);
David Benjamin82a4b222020-02-09 17:51:45 -05003296bool tls_get_message(const SSL *ssl, SSLMessage *out);
3297ssl_open_record_t tls_open_handshake(SSL *ssl, size_t *out_consumed,
3298 uint8_t *out_alert, Span<uint8_t> in);
3299void tls_next_message(SSL *ssl);
David Benjamin854dd652014-08-26 00:32:30 -04003300
David Benjamin82a4b222020-02-09 17:51:45 -05003301int tls_dispatch_alert(SSL *ssl);
3302ssl_open_record_t tls_open_app_data(SSL *ssl, Span<uint8_t> *out,
3303 size_t *out_consumed, uint8_t *out_alert,
3304 Span<uint8_t> in);
3305ssl_open_record_t tls_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
3306 uint8_t *out_alert,
3307 Span<uint8_t> in);
David Benjamin4bd32a82021-05-14 17:06:29 -04003308int tls_write_app_data(SSL *ssl, bool *out_needs_handshake,
3309 size_t *out_bytes_written, Span<const uint8_t> in);
Steven Valdezb6b6ff32016-10-26 11:56:35 -04003310
David Benjamin82a4b222020-02-09 17:51:45 -05003311bool tls_new(SSL *ssl);
3312void tls_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003313
David Benjaminbcef5142021-06-02 11:24:26 -04003314bool tls_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
3315bool tls_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
David Benjamin82a4b222020-02-09 17:51:45 -05003316bool tls_add_message(SSL *ssl, Array<uint8_t> msg);
3317bool tls_add_change_cipher_spec(SSL *ssl);
3318int tls_flush_flight(SSL *ssl);
David Benjamin75836432016-06-17 18:48:29 -04003319
David Benjaminbcef5142021-06-02 11:24:26 -04003320bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
3321bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
David Benjamin97250f42017-10-07 04:12:35 -04003322bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
3323bool dtls1_add_change_cipher_spec(SSL *ssl);
David Benjamin1a999cf2017-01-03 10:30:35 -05003324int dtls1_flush_flight(SSL *ssl);
David Benjaminc6604172016-06-02 16:38:35 -04003325
David Benjaminc11ea9422017-08-29 16:33:21 -04003326// ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
David Benjamin97250f42017-10-07 04:12:35 -04003327// the pending flight. It returns true on success and false on error.
3328bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
Steven Valdez5eead162016-11-11 22:23:25 -05003329
David Benjamin97250f42017-10-07 04:12:35 -04003330// ssl_hash_message incorporates |msg| into the handshake hash. It returns true
3331// on success and false on allocation failure.
David Benjamin7934f082017-08-01 16:32:25 -04003332bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjaminced94792016-11-14 17:12:11 +09003333
David Benjamind9229f92017-10-06 17:36:20 -04003334ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
3335 size_t *out_consumed, uint8_t *out_alert,
3336 Span<uint8_t> in);
3337ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
3338 uint8_t *out_alert,
3339 Span<uint8_t> in);
Adam Langley95c29f32014-06-20 12:00:00 -07003340
David Benjamin74795b32017-08-31 15:13:12 -04003341int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
David Benjamin4bd32a82021-05-14 17:06:29 -04003342 size_t *out_bytes_written, Span<const uint8_t> in);
David Benjamin45d45c12016-06-07 15:20:49 -04003343
David Benjaminc11ea9422017-08-29 16:33:21 -04003344// dtls1_write_record sends a record. It returns one on success and <= 0 on
3345// error.
David Benjamin4bd32a82021-05-14 17:06:29 -04003346int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in,
David Benjamin45d45c12016-06-07 15:20:49 -04003347 enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -07003348
David Benjaminaad50db2016-06-23 17:49:12 -04003349int dtls1_retransmit_outgoing_messages(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04003350bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
Jesse Selover6b0d8222018-08-14 12:47:29 -04003351 CBS *out_body);
David Benjamin97250f42017-10-07 04:12:35 -04003352bool dtls1_check_timeout_num(SSL *ssl);
David Benjamin2fa83de2015-02-08 01:40:08 -05003353
David Benjamin0d56f882015-12-19 17:05:56 -05003354void dtls1_start_timer(SSL *ssl);
3355void dtls1_stop_timer(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04003356bool dtls1_is_timer_expired(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003357unsigned int dtls1_min_mtu(void);
3358
David Benjamin97250f42017-10-07 04:12:35 -04003359bool dtls1_new(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05003360void dtls1_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003361
Adam Langleyc9827e02019-04-12 14:46:50 -07003362bool dtls1_get_message(const SSL *ssl, SSLMessage *out);
David Benjamind9229f92017-10-06 17:36:20 -04003363ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
3364 uint8_t *out_alert, Span<uint8_t> in);
David Benjamin8f94c312017-08-01 17:35:55 -04003365void dtls1_next_message(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05003366int dtls1_dispatch_alert(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003367
Adam Langley3fe8fa72018-01-26 09:14:30 -08003368// tls1_configure_aead configures either the read or write direction AEAD (as
3369// determined by |direction|) using the keys generated by the TLS KDF. The
3370// |key_block_cache| argument is used to store the generated key block, if
3371// empty. Otherwise it's assumed that the key block is already contained within
David Benjamin5fa22ed2020-06-22 17:08:11 -04003372// it. It returns true on success or false on error.
3373bool tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction,
3374 Array<uint8_t> *key_block_cache,
3375 const SSL_SESSION *session,
3376 Span<const uint8_t> iv_override);
Adam Langley3fe8fa72018-01-26 09:14:30 -08003377
David Benjamin5fa22ed2020-06-22 17:08:11 -04003378bool tls1_change_cipher_state(SSL_HANDSHAKE *hs,
3379 evp_aead_direction_t direction);
Steven Valdez908ac192017-01-12 13:17:07 -05003380int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
David Benjamin2637f3c2017-10-27 00:56:55 -04003381 Span<const uint8_t> premaster);
Adam Langley95c29f32014-06-20 12:00:00 -07003382
David Benjamincf0ce672017-09-21 02:25:59 -04003383// tls1_get_grouplist returns the locally-configured group preference list.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003384Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl);
Steven Valdez5440fe02016-07-18 12:40:30 -04003385
David Benjamin861abcc2018-07-14 17:40:26 -04003386// tls1_check_group_id returns whether |group_id| is consistent with locally-
3387// configured group preferences.
3388bool tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id);
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02003389
David Benjaminc11ea9422017-08-29 16:33:21 -04003390// tls1_get_shared_group sets |*out_group_id| to the first preferred shared
David Benjamin861abcc2018-07-14 17:40:26 -04003391// group between client and server preferences and returns true. If none may be
3392// found, it returns false.
3393bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
David Benjamin072334d2014-07-13 16:24:27 -04003394
David Benjamin83a49932021-05-20 15:57:09 -04003395// ssl_add_clienthello_tlsext writes ClientHello extensions to |out| for |type|.
3396// It returns true on success and false on failure. The |header_len| argument is
3397// the length of the ClientHello written so far and is used to compute the
3398// padding length. (It does not include the record header or handshake headers.)
3399//
3400// If |type| is |ssl_client_hello_inner|, this function also writes the
3401// compressed extensions to |out_encoded|. Otherwise, |out_encoded| should be
3402// nullptr.
3403//
3404// On success, the function sets |*out_needs_psk_binder| to whether the last
3405// ClientHello extension was the pre_shared_key extension and needs a PSK binder
3406// filled in. The caller should then update |out| and, if applicable,
3407// |out_encoded| with the binder after completing the whole message.
David Benjamin83a49932021-05-20 15:57:09 -04003408bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, CBB *out_encoded,
3409 bool *out_needs_psk_binder,
David Benjamin18b68362021-06-18 23:13:46 -04003410 ssl_client_hello_type_t type,
3411 size_t header_len);
David Benjamine8d53502015-10-10 14:13:23 -04003412
David Benjamin861abcc2018-07-14 17:40:26 -04003413bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
3414bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3415 const SSL_CLIENT_HELLO *client_hello);
David Benjamine2cb4232021-06-23 18:14:22 -04003416bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *extensions);
Adam Langley95c29f32014-06-20 12:00:00 -07003417
Adam Langleyfcf25832014-12-18 17:42:32 -08003418#define tlsext_tick_md EVP_sha256
David Benjamine3aa1d92015-06-16 15:34:50 -04003419
David Benjaminc11ea9422017-08-29 16:33:21 -04003420// ssl_process_ticket processes a session ticket from the client. It returns
3421// one of:
3422// |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
3423// |*out_renew_ticket| is set to whether the ticket should be renewed.
3424// |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
3425// fresh ticket should be sent, but the given ticket cannot be used.
3426// |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
3427// Retry later.
3428// |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
Adam Langley4c341d02017-03-08 19:33:21 -08003429enum ssl_ticket_aead_result_t ssl_process_ticket(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003430 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
David Benjamin28655672018-07-18 23:23:25 -04003431 bool *out_renew_ticket, Span<const uint8_t> ticket,
3432 Span<const uint8_t> session_id);
Adam Langley95c29f32014-06-20 12:00:00 -07003433
David Benjaminc11ea9422017-08-29 16:33:21 -04003434// tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
David Benjamin861abcc2018-07-14 17:40:26 -04003435// the signature. If the key is valid, it saves the Channel ID and returns true.
3436// Otherwise, it returns false.
3437bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Nick Harper60a85cb2016-09-23 16:25:11 -07003438
David Benjaminc11ea9422017-08-29 16:33:21 -04003439// tls1_write_channel_id generates a Channel ID message and puts the output in
David Benjamin46853762018-07-03 14:01:26 -04003440// |cbb|. |ssl->channel_id_private| must already be set before calling. This
3441// function returns true on success and false on error.
David Benjaminf1db1a32017-10-27 01:12:34 -04003442bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07003443
David Benjaminc11ea9422017-08-29 16:33:21 -04003444// tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
3445// it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
David Benjamin861abcc2018-07-14 17:40:26 -04003446// true on success and false on failure.
3447bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003448
David Benjamin861abcc2018-07-14 17:40:26 -04003449// tls1_record_handshake_hashes_for_channel_id records the current handshake
3450// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3451// data.
3452bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
Adam Langley1258b6a2014-06-20 12:00:00 -07003453
David Benjamin861abcc2018-07-14 17:40:26 -04003454// ssl_can_write returns whether |ssl| is allowed to write.
3455bool ssl_can_write(const SSL *ssl);
Steven Valdez681eb6a2016-12-19 13:19:29 -05003456
David Benjamin861abcc2018-07-14 17:40:26 -04003457// ssl_can_read returns wheter |ssl| is allowed to read.
3458bool ssl_can_read(const SSL *ssl);
Steven Valdez681eb6a2016-12-19 13:19:29 -05003459
David Benjaminad8f5e12017-02-20 17:00:20 -05003460void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003461void ssl_ctx_get_current_time(const SSL_CTX *ctx,
3462 struct OPENSSL_timeval *out_clock);
David Benjamin721e8b72016-08-03 13:13:17 -04003463
David Benjaminc11ea9422017-08-29 16:33:21 -04003464// ssl_reset_error_state resets state for |SSL_get_error|.
David Benjamin2be4aa72017-01-02 07:56:51 -05003465void ssl_reset_error_state(SSL *ssl);
3466
David Benjamin31aad2d2017-10-06 16:04:06 -04003467// ssl_set_read_error sets |ssl|'s read half into an error state, saving the
3468// current state of the error queue.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003469void ssl_set_read_error(SSL *ssl);
David Benjamin31aad2d2017-10-06 16:04:06 -04003470
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07003471BSSL_NAMESPACE_END
Adam Langley11d11d62017-07-17 11:10:46 -07003472
Steven Valdez87eab492016-06-27 16:34:59 -04003473
David Benjaminc11ea9422017-08-29 16:33:21 -04003474// Opaque C types.
3475//
3476// The following types are exported to C code as public typedefs, so they must
3477// be defined outside of the namespace.
David Benjamine39ac8f2017-07-20 12:22:21 -04003478
David Benjaminc11ea9422017-08-29 16:33:21 -04003479// ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
3480// structure to support the legacy version-locked methods.
David Benjamine39ac8f2017-07-20 12:22:21 -04003481struct ssl_method_st {
David Benjaminc11ea9422017-08-29 16:33:21 -04003482 // version, if non-zero, is the only protocol version acceptable to an
3483 // SSL_CTX initialized from this method.
David Benjamine39ac8f2017-07-20 12:22:21 -04003484 uint16_t version;
David Benjaminc11ea9422017-08-29 16:33:21 -04003485 // method is the underlying SSL_PROTOCOL_METHOD that initializes the
3486 // SSL_CTX.
David Benjamin664e99a2017-10-10 19:23:21 -04003487 const bssl::SSL_PROTOCOL_METHOD *method;
David Benjaminc11ea9422017-08-29 16:33:21 -04003488 // x509_method contains pointers to functions that might deal with |X509|
3489 // compatibility, or might be a no-op, depending on the application.
David Benjamin0363de92018-06-29 13:57:38 -04003490 const bssl::SSL_X509_METHOD *x509_method;
David Benjamine39ac8f2017-07-20 12:22:21 -04003491};
3492
David Benjaminfbf10f02024-02-12 16:57:18 -05003493struct ssl_ctx_st : public bssl::RefCounted<ssl_ctx_st> {
David Benjaminc1389f22018-07-02 19:13:42 -04003494 explicit ssl_ctx_st(const SSL_METHOD *ssl_method);
3495 ssl_ctx_st(const ssl_ctx_st &) = delete;
3496 ssl_ctx_st &operator=(const ssl_ctx_st &) = delete;
3497
3498 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
3499 const bssl::SSL_X509_METHOD *x509_method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003500
3501 // lock is used to protect various operations on this object.
3502 CRYPTO_MUTEX lock;
3503
3504 // conf_max_version is the maximum acceptable protocol version configured by
3505 // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
3506 // and is further constrainted by |SSL_OP_NO_*|.
David Benjaminc1389f22018-07-02 19:13:42 -04003507 uint16_t conf_max_version = 0;
David Benjamin49798032018-07-02 16:23:54 -04003508
3509 // conf_min_version is the minimum acceptable protocol version configured by
3510 // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
3511 // and is further constrainted by |SSL_OP_NO_*|.
David Benjaminc1389f22018-07-02 19:13:42 -04003512 uint16_t conf_min_version = 0;
David Benjamin49798032018-07-02 16:23:54 -04003513
David Benjamin3f180b82022-05-09 17:45:18 -04003514 // num_tickets is the number of tickets to send immediately after the TLS 1.3
3515 // handshake. TLS 1.3 recommends single-use tickets so, by default, issue two
3516 /// in case the client makes several connections before getting a renewal.
3517 uint8_t num_tickets = 2;
3518
Steven Valdezc8e0f902018-07-14 11:23:01 -04003519 // quic_method is the method table corresponding to the QUIC hooks.
3520 const SSL_QUIC_METHOD *quic_method = nullptr;
3521
David Benjamin0ce090a2018-07-02 20:24:40 -04003522 bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list;
David Benjamin49798032018-07-02 16:23:54 -04003523
David Benjaminc1389f22018-07-02 19:13:42 -04003524 X509_STORE *cert_store = nullptr;
3525 LHASH_OF(SSL_SESSION) *sessions = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003526 // Most session-ids that will be cached, default is
3527 // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
David Benjaminc1389f22018-07-02 19:13:42 -04003528 unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
3529 SSL_SESSION *session_cache_head = nullptr;
3530 SSL_SESSION *session_cache_tail = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003531
3532 // handshakes_since_cache_flush is the number of successful handshakes since
3533 // the last cache flush.
David Benjaminc1389f22018-07-02 19:13:42 -04003534 int handshakes_since_cache_flush = 0;
David Benjamin49798032018-07-02 16:23:54 -04003535
3536 // This can have one of 2 values, ored together,
3537 // SSL_SESS_CACHE_CLIENT,
3538 // SSL_SESS_CACHE_SERVER,
3539 // Default is SSL_SESSION_CACHE_SERVER, which means only
3540 // SSL_accept which cache SSL_SESSIONS.
David Benjaminc1389f22018-07-02 19:13:42 -04003541 int session_cache_mode = SSL_SESS_CACHE_SERVER;
David Benjamin49798032018-07-02 16:23:54 -04003542
3543 // session_timeout is the default lifetime for new sessions in TLS 1.2 and
3544 // earlier, in seconds.
David Benjaminc1389f22018-07-02 19:13:42 -04003545 uint32_t session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin49798032018-07-02 16:23:54 -04003546
3547 // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
3548 // 1.3, in seconds.
David Benjaminc1389f22018-07-02 19:13:42 -04003549 uint32_t session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin49798032018-07-02 16:23:54 -04003550
3551 // If this callback is not null, it will be called each time a session id is
3552 // added to the cache. If this function returns 1, it means that the
3553 // callback will do a SSL_SESSION_free() when it has finished using it.
3554 // Otherwise, on 0, it means the callback has finished with it. If
3555 // remove_session_cb is not null, it will be called when a session-id is
3556 // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
3557 // it.
David Benjaminc1389f22018-07-02 19:13:42 -04003558 int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess) = nullptr;
3559 void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003560 SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
David Benjaminc1389f22018-07-02 19:13:42 -04003561 int *copy) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003562
David Benjamin49798032018-07-02 16:23:54 -04003563 // if defined, these override the X509_verify_cert() calls
David Benjaminc1389f22018-07-02 19:13:42 -04003564 int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg) = nullptr;
3565 void *app_verify_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003566
David Benjaminc1389f22018-07-02 19:13:42 -04003567 ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
3568 uint8_t *out_alert) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003569
3570 // Default password callback.
David Benjaminc1389f22018-07-02 19:13:42 -04003571 pem_password_cb *default_passwd_callback = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003572
3573 // Default password callback user data.
David Benjaminc1389f22018-07-02 19:13:42 -04003574 void *default_passwd_callback_userdata = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003575
3576 // get client cert callback
Jesse Selover6b0d8222018-08-14 12:47:29 -04003577 int (*client_cert_cb)(SSL *ssl, X509 **out_x509,
3578 EVP_PKEY **out_pkey) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003579
David Benjamin49798032018-07-02 16:23:54 -04003580 CRYPTO_EX_DATA ex_data;
3581
David Benjamin49798032018-07-02 16:23:54 -04003582 // Default values used when no per-SSL value is defined follow
3583
David Benjaminc1389f22018-07-02 19:13:42 -04003584 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003585
3586 // what we put in client cert requests
David Benjamin0ce090a2018-07-02 20:24:40 -04003587 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
David Benjamin49798032018-07-02 16:23:54 -04003588
3589 // cached_x509_client_CA is a cache of parsed versions of the elements of
3590 // |client_CA|.
David Benjaminc1389f22018-07-02 19:13:42 -04003591 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003592
3593
3594 // Default values to use in SSL structures follow (these are copied by
3595 // SSL_new)
3596
David Benjaminc1389f22018-07-02 19:13:42 -04003597 uint32_t options = 0;
3598 // Disable the auto-chaining feature by default. wpa_supplicant relies on this
3599 // feature, but require callers opt into it.
3600 uint32_t mode = SSL_MODE_NO_AUTO_CHAIN;
3601 uint32_t max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
David Benjamin49798032018-07-02 16:23:54 -04003602
David Benjamin0ce090a2018-07-02 20:24:40 -04003603 bssl::UniquePtr<bssl::CERT> cert;
David Benjamin49798032018-07-02 16:23:54 -04003604
3605 // callback that allows applications to peek at protocol messages
David Benjamin123eaae2022-01-27 12:25:11 -05003606 void (*msg_callback)(int is_write, int version, int content_type,
Jesse Selover6b0d8222018-08-14 12:47:29 -04003607 const void *buf, size_t len, SSL *ssl,
3608 void *arg) = nullptr;
David Benjaminc1389f22018-07-02 19:13:42 -04003609 void *msg_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003610
David Benjaminc1389f22018-07-02 19:13:42 -04003611 int verify_mode = SSL_VERIFY_NONE;
3612 int (*default_verify_callback)(int ok, X509_STORE_CTX *ctx) =
3613 nullptr; // called 'verify_callback' in the SSL
David Benjamin49798032018-07-02 16:23:54 -04003614
David Benjaminc1389f22018-07-02 19:13:42 -04003615 X509_VERIFY_PARAM *param = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003616
3617 // select_certificate_cb is called before most ClientHello processing and
3618 // before the decision whether to resume a session is made. See
3619 // |ssl_select_cert_result_t| for details of the return values.
David Benjaminc1389f22018-07-02 19:13:42 -04003620 ssl_select_cert_result_t (*select_certificate_cb)(const SSL_CLIENT_HELLO *) =
3621 nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003622
3623 // dos_protection_cb is called once the resumption decision for a ClientHello
3624 // has been made. It returns one to continue the handshake or zero to
3625 // abort.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003626 int (*dos_protection_cb)(const SSL_CLIENT_HELLO *) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003627
Jesse Selover1c337e52018-08-10 13:28:47 -04003628 // Controls whether to verify certificates when resuming connections. They
3629 // were already verified when the connection was first made, so the default is
3630 // false. For now, this is only respected on clients, not servers.
3631 bool reverify_on_resume = false;
3632
David Benjamin49798032018-07-02 16:23:54 -04003633 // Maximum amount of data to send in one fragment. actual record size can be
3634 // more than this due to padding and MAC overheads.
David Benjaminc1389f22018-07-02 19:13:42 -04003635 uint16_t max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin49798032018-07-02 16:23:54 -04003636
3637 // TLS extensions servername callback
David Benjamin46853762018-07-03 14:01:26 -04003638 int (*servername_callback)(SSL *, int *, void *) = nullptr;
3639 void *servername_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003640
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003641 // RFC 4507 session ticket keys. |ticket_key_current| may be NULL before the
3642 // first handshake and |ticket_key_prev| may be NULL at any time.
3643 // Automatically generated ticket keys are rotated as needed at handshake
3644 // time. Hence, all access must be synchronized through |lock|.
3645 bssl::UniquePtr<bssl::TicketKey> ticket_key_current;
3646 bssl::UniquePtr<bssl::TicketKey> ticket_key_prev;
David Benjamin49798032018-07-02 16:23:54 -04003647
3648 // Callback to support customisation of ticket key setting
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003649 int (*ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
3650 EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003651
3652 // Server-only: psk_identity_hint is the default identity hint to send in
3653 // PSK-based key exchanges.
David Benjamin0ce090a2018-07-02 20:24:40 -04003654 bssl::UniquePtr<char> psk_identity_hint;
David Benjamin49798032018-07-02 16:23:54 -04003655
David Benjaminc1389f22018-07-02 19:13:42 -04003656 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
3657 unsigned max_identity_len, uint8_t *psk,
3658 unsigned max_psk_len) = nullptr;
3659 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
3660 unsigned max_psk_len) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003661
3662
3663 // Next protocol negotiation information
3664 // (for experimental NPN extension).
3665
3666 // For a server, this contains a callback function by which the set of
3667 // advertised protocols can be provided.
3668 int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
David Benjaminc1389f22018-07-02 19:13:42 -04003669 unsigned *out_len, void *arg) = nullptr;
3670 void *next_protos_advertised_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003671 // For a client, this contains a callback function that selects the
3672 // next protocol from the list provided by the server.
3673 int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
David Benjaminc1389f22018-07-02 19:13:42 -04003674 const uint8_t *in, unsigned in_len,
3675 void *arg) = nullptr;
3676 void *next_proto_select_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003677
3678 // ALPN information
3679 // (we are in the process of transitioning from NPN to ALPN.)
3680
3681 // For a server, this contains a callback function that allows the
3682 // server to select the protocol for the connection.
3683 // out: on successful return, this must point to the raw protocol
3684 // name (without the length prefix).
3685 // outlen: on successful return, this contains the length of |*out|.
3686 // in: points to the client's list of supported protocols in
3687 // wire-format.
3688 // inlen: the length of |in|.
3689 int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
David Benjaminc1389f22018-07-02 19:13:42 -04003690 const uint8_t *in, unsigned in_len,
3691 void *arg) = nullptr;
3692 void *alpn_select_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003693
3694 // For a client, this contains the list of supported protocols in wire
3695 // format.
David Benjamin0ce090a2018-07-02 20:24:40 -04003696 bssl::Array<uint8_t> alpn_client_proto_list;
David Benjamin49798032018-07-02 16:23:54 -04003697
3698 // SRTP profiles we are willing to do from RFC 5764
David Benjamin0ce090a2018-07-02 20:24:40 -04003699 bssl::UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
David Benjamin49798032018-07-02 16:23:54 -04003700
3701 // Defined compression algorithms for certificates.
David Benjamin8fe15842019-10-08 16:57:38 -04003702 bssl::GrowableArray<bssl::CertCompressionAlg> cert_compression_algs;
David Benjamin49798032018-07-02 16:23:54 -04003703
3704 // Supported group values inherited by SSL structure
David Benjamin0ce090a2018-07-02 20:24:40 -04003705 bssl::Array<uint16_t> supported_group_list;
David Benjamin49798032018-07-02 16:23:54 -04003706
David Benjaminb5879112021-05-18 17:10:18 -04003707 // channel_id_private is the client's Channel ID private key, or null if
3708 // Channel ID should not be offered on this connection.
David Benjamin46853762018-07-03 14:01:26 -04003709 bssl::UniquePtr<EVP_PKEY> channel_id_private;
David Benjamin49798032018-07-02 16:23:54 -04003710
David Benjaminc3b373b2021-06-06 13:04:26 -04003711 // ech_keys contains the server's list of ECHConfig values and associated
3712 // private keys. This list may be swapped out at any time, so all access must
3713 // be synchronized through |lock|.
3714 bssl::UniquePtr<SSL_ECH_KEYS> ech_keys;
Daniel McArdle00e434d2021-02-18 11:47:18 -05003715
David Benjamin49798032018-07-02 16:23:54 -04003716 // keylog_callback, if not NULL, is the key logging callback. See
3717 // |SSL_CTX_set_keylog_callback|.
David Benjaminc1389f22018-07-02 19:13:42 -04003718 void (*keylog_callback)(const SSL *ssl, const char *line) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003719
3720 // current_time_cb, if not NULL, is the function to use to get the current
3721 // time. It sets |*out_clock| to the current time. The |ssl| argument is
3722 // always NULL. See |SSL_CTX_set_current_time_cb|.
David Benjaminc1389f22018-07-02 19:13:42 -04003723 void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003724
3725 // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
3726 // memory.
David Benjaminc1389f22018-07-02 19:13:42 -04003727 CRYPTO_BUFFER_POOL *pool = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003728
3729 // ticket_aead_method contains function pointers for opening and sealing
3730 // session tickets.
David Benjaminc1389f22018-07-02 19:13:42 -04003731 const SSL_TICKET_AEAD_METHOD *ticket_aead_method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003732
3733 // legacy_ocsp_callback implements an OCSP-related callback for OpenSSL
3734 // compatibility.
David Benjaminc1389f22018-07-02 19:13:42 -04003735 int (*legacy_ocsp_callback)(SSL *ssl, void *arg) = nullptr;
3736 void *legacy_ocsp_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003737
Adam Langley2f6409e2023-04-10 21:09:11 +00003738 // tls13_cipher_policy limits the set of ciphers that can be selected when
3739 // negotiating a TLS 1.3 connection.
3740 enum ssl_compliance_policy_t tls13_cipher_policy = ssl_compliance_policy_none;
3741
David Benjamin49798032018-07-02 16:23:54 -04003742 // verify_sigalgs, if not empty, is the set of signature algorithms
3743 // accepted from the peer in decreasing order of preference.
David Benjamin0ce090a2018-07-02 20:24:40 -04003744 bssl::Array<uint16_t> verify_sigalgs;
David Benjamin49798032018-07-02 16:23:54 -04003745
3746 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
3747 // hash of the peer's certificate and then discard it to save memory and
3748 // session space. Only effective on the server side.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003749 bool retain_only_sha256_of_client_certs : 1;
David Benjamin49798032018-07-02 16:23:54 -04003750
3751 // quiet_shutdown is true if the connection should not send a close_notify on
3752 // shutdown.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003753 bool quiet_shutdown : 1;
David Benjamin49798032018-07-02 16:23:54 -04003754
3755 // ocsp_stapling_enabled is only used by client connections and indicates
3756 // whether OCSP stapling will be requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003757 bool ocsp_stapling_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003758
3759 // If true, a client will request certificate timestamps.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003760 bool signed_cert_timestamps_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003761
David Benjamin46853762018-07-03 14:01:26 -04003762 // channel_id_enabled is whether Channel ID is enabled. For a server, means
3763 // that we'll accept Channel IDs from clients. For a client, means that we'll
3764 // advertise support.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003765 bool channel_id_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003766
David Benjamin3675eb32021-05-18 14:01:07 -04003767 // grease_enabled is whether GREASE (RFC 8701) is enabled.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003768 bool grease_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003769
David Benjamine9c5d722021-06-09 17:43:16 -04003770 // permute_extensions is whether to permute extensions when sending messages.
3771 bool permute_extensions : 1;
3772
David Benjamin49798032018-07-02 16:23:54 -04003773 // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN
3774 // protocols from the peer.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003775 bool allow_unknown_alpn_protos : 1;
David Benjamin49798032018-07-02 16:23:54 -04003776
David Benjamin49798032018-07-02 16:23:54 -04003777 // false_start_allowed_without_alpn is whether False Start (if
3778 // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003779 bool false_start_allowed_without_alpn : 1;
David Benjamin49798032018-07-02 16:23:54 -04003780
3781 // handoff indicates that a server should stop after receiving the
3782 // ClientHello and pause the handshake in such a way that |SSL_get_error|
David Benjaminf4928302019-08-21 16:04:53 -04003783 // returns |SSL_ERROR_HANDOFF|.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003784 bool handoff : 1;
David Benjamin49798032018-07-02 16:23:54 -04003785
3786 // If enable_early_data is true, early data can be sent and accepted.
3787 bool enable_early_data : 1;
David Benjaminc1389f22018-07-02 19:13:42 -04003788
Bob Beck8cacbd92023-04-05 14:38:38 -06003789 // aes_hw_override if set indicates we should override checking for AES
3790 // hardware support, and use the value in aes_hw_override_value instead.
3791 bool aes_hw_override : 1;
3792
3793 // aes_hw_override_value is used for testing to indicate the support or lack
3794 // of support for AES hardware. The value is only considered if
3795 // |aes_hw_override| is true.
3796 bool aes_hw_override_value : 1;
3797
David Benjaminc1389f22018-07-02 19:13:42 -04003798 private:
David Benjaminfbf10f02024-02-12 16:57:18 -05003799 friend RefCounted;
David Benjaminc1389f22018-07-02 19:13:42 -04003800 ~ssl_ctx_st();
David Benjamin49798032018-07-02 16:23:54 -04003801};
3802
3803struct ssl_st {
David Benjaminc1389f22018-07-02 19:13:42 -04003804 explicit ssl_st(SSL_CTX *ctx_arg);
3805 ssl_st(const ssl_st &) = delete;
3806 ssl_st &operator=(const ssl_st &) = delete;
3807 ~ssl_st();
3808
David Benjamin49798032018-07-02 16:23:54 -04003809 // method is the method table corresponding to the current protocol (DTLS or
3810 // TLS).
David Benjaminc1389f22018-07-02 19:13:42 -04003811 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003812
3813 // config is a container for handshake configuration. Accesses to this field
3814 // should check for nullptr, since configuration may be shed after the
3815 // handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use
3816 // that instead, and skip the null check.)
David Benjamin50596f82018-07-02 19:47:27 -04003817 bssl::UniquePtr<bssl::SSL_CONFIG> config;
David Benjamin49798032018-07-02 16:23:54 -04003818
3819 // version is the protocol version.
David Benjaminc1389f22018-07-02 19:13:42 -04003820 uint16_t version = 0;
David Benjamin49798032018-07-02 16:23:54 -04003821
David Benjaminc1389f22018-07-02 19:13:42 -04003822 uint16_t max_send_fragment = 0;
David Benjamin49798032018-07-02 16:23:54 -04003823
3824 // There are 2 BIO's even though they are normally both the same. This is so
3825 // data can be read and written to different handlers
3826
David Benjamin50596f82018-07-02 19:47:27 -04003827 bssl::UniquePtr<BIO> rbio; // used by SSL_read
3828 bssl::UniquePtr<BIO> wbio; // used by SSL_write
David Benjamin49798032018-07-02 16:23:54 -04003829
3830 // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
3831 // Otherwise, it returns a value corresponding to what operation is needed to
3832 // progress.
David Benjaminc1389f22018-07-02 19:13:42 -04003833 bssl::ssl_hs_wait_t (*do_handshake)(bssl::SSL_HANDSHAKE *hs) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003834
David Benjaminc59b9aa2018-07-16 21:34:03 -04003835 bssl::SSL3_STATE *s3 = nullptr; // TLS variables
3836 bssl::DTLS1_STATE *d1 = nullptr; // DTLS variables
David Benjamin49798032018-07-02 16:23:54 -04003837
3838 // callback that allows applications to peek at protocol messages
3839 void (*msg_callback)(int write_p, int version, int content_type,
David Benjaminc1389f22018-07-02 19:13:42 -04003840 const void *buf, size_t len, SSL *ssl,
3841 void *arg) = nullptr;
3842 void *msg_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003843
3844 // session info
3845
3846 // initial_timeout_duration_ms is the default DTLS timeout duration in
3847 // milliseconds. It's used to initialize the timer any time it's restarted.
David Benjaminc1389f22018-07-02 19:13:42 -04003848 //
3849 // RFC 6347 states that implementations SHOULD use an initial timer value of 1
3850 // second.
3851 unsigned initial_timeout_duration_ms = 1000;
David Benjamin49798032018-07-02 16:23:54 -04003852
David Benjamin49798032018-07-02 16:23:54 -04003853 // session is the configured session to be offered by the client. This session
3854 // is immutable.
David Benjamin50596f82018-07-02 19:47:27 -04003855 bssl::UniquePtr<SSL_SESSION> session;
David Benjamin49798032018-07-02 16:23:54 -04003856
David Benjaminc1389f22018-07-02 19:13:42 -04003857 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003858
David Benjamin50596f82018-07-02 19:47:27 -04003859 bssl::UniquePtr<SSL_CTX> ctx;
David Benjamin49798032018-07-02 16:23:54 -04003860
3861 // session_ctx is the |SSL_CTX| used for the session cache and related
3862 // settings.
David Benjamin50596f82018-07-02 19:47:27 -04003863 bssl::UniquePtr<SSL_CTX> session_ctx;
David Benjamin49798032018-07-02 16:23:54 -04003864
3865 // extra application data
3866 CRYPTO_EX_DATA ex_data;
3867
David Benjaminc1389f22018-07-02 19:13:42 -04003868 uint32_t options = 0; // protocol behaviour
3869 uint32_t mode = 0; // API behaviour
3870 uint32_t max_cert_list = 0;
David Benjamin46853762018-07-03 14:01:26 -04003871 bssl::UniquePtr<char> hostname;
David Benjamin49798032018-07-02 16:23:54 -04003872
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +00003873 // quic_method is the method table corresponding to the QUIC hooks.
3874 const SSL_QUIC_METHOD *quic_method = nullptr;
3875
David Benjamin49798032018-07-02 16:23:54 -04003876 // renegotiate_mode controls how peer renegotiation attempts are handled.
David Benjaminc1389f22018-07-02 19:13:42 -04003877 ssl_renegotiate_mode_t renegotiate_mode = ssl_renegotiate_never;
David Benjamin49798032018-07-02 16:23:54 -04003878
3879 // server is true iff the this SSL* is the server half. Note: before the SSL*
3880 // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
3881 // the side is not determined. In this state, server is always false.
3882 bool server : 1;
3883
3884 // quiet_shutdown is true if the connection should not send a close_notify on
3885 // shutdown.
3886 bool quiet_shutdown : 1;
3887
David Benjamin49798032018-07-02 16:23:54 -04003888 // If enable_early_data is true, early data can be sent and accepted.
3889 bool enable_early_data : 1;
3890};
David Benjamine39ac8f2017-07-20 12:22:21 -04003891
David Benjaminfbf10f02024-02-12 16:57:18 -05003892struct ssl_session_st : public bssl::RefCounted<ssl_session_st> {
David Benjaminbfdd1a92018-06-29 16:26:38 -04003893 explicit ssl_session_st(const bssl::SSL_X509_METHOD *method);
3894 ssl_session_st(const ssl_session_st &) = delete;
3895 ssl_session_st &operator=(const ssl_session_st &) = delete;
3896
Jesse Selover6b0d8222018-08-14 12:47:29 -04003897 // ssl_version is the (D)TLS version that established the session.
3898 uint16_t ssl_version = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003899
3900 // group_id is the ID of the ECDH group used to establish this session or zero
3901 // if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003902 uint16_t group_id = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003903
3904 // peer_signature_algorithm is the signature algorithm used to authenticate
3905 // the peer, or zero if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003906 uint16_t peer_signature_algorithm = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003907
David Benjamin5351c8b2020-11-19 00:25:29 -05003908 // secret, in TLS 1.2 and below, is the master secret associated with the
3909 // session. In TLS 1.3 and up, it is the resumption PSK for sessions handed to
3910 // the caller, but it stores the resumption secret when stored on |SSL|
3911 // objects.
David Benjamin44a389a2022-12-10 00:46:54 -05003912 uint8_t secret_length = 0;
David Benjamin5351c8b2020-11-19 00:25:29 -05003913 uint8_t secret[SSL_MAX_MASTER_KEY_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003914
3915 // session_id - valid?
David Benjamin44a389a2022-12-10 00:46:54 -05003916 uint8_t session_id_length = 0;
David Benjaminbfdd1a92018-06-29 16:26:38 -04003917 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003918 // this is used to determine whether the session is being reused in
3919 // the appropriate context. It is up to the application to set this,
3920 // via SSL_new
David Benjaminbfdd1a92018-06-29 16:26:38 -04003921 uint8_t sid_ctx_length = 0;
3922 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003923
David Benjaminbfdd1a92018-06-29 16:26:38 -04003924 bssl::UniquePtr<char> psk_identity;
David Benjamin1c68fa22018-06-02 19:02:46 -04003925
3926 // certs contains the certificate chain from the peer, starting with the leaf
3927 // certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003928 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs;
David Benjamin1c68fa22018-06-02 19:02:46 -04003929
David Benjaminbfdd1a92018-06-29 16:26:38 -04003930 const bssl::SSL_X509_METHOD *x509_method = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003931
3932 // x509_peer is the peer's certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003933 X509 *x509_peer = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003934
3935 // x509_chain is the certificate chain sent by the peer. NOTE: for historical
3936 // reasons, when a client (so the peer is a server), the chain includes
3937 // |peer|, but when a server it does not.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003938 STACK_OF(X509) *x509_chain = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003939
3940 // x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that
3941 // omits the leaf certificate. This exists because OpenSSL, historically,
3942 // didn't include the leaf certificate in the chain for a server, but did for
3943 // a client. The |x509_chain| always includes it and, if an API call requires
3944 // a chain without, it is stored here.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003945 STACK_OF(X509) *x509_chain_without_leaf = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003946
3947 // verify_result is the result of certificate verification in the case of
3948 // non-fatal certificate errors.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003949 long verify_result = X509_V_ERR_INVALID_CALL;
David Benjamin1c68fa22018-06-02 19:02:46 -04003950
3951 // timeout is the lifetime of the session in seconds, measured from |time|.
3952 // This is renewable up to |auth_timeout|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003953 uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003954
3955 // auth_timeout is the non-renewable lifetime of the session in seconds,
3956 // measured from |time|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003957 uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003958
3959 // time is the time the session was issued, measured in seconds from the UNIX
3960 // epoch.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003961 uint64_t time = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003962
David Benjaminbfdd1a92018-06-29 16:26:38 -04003963 const SSL_CIPHER *cipher = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003964
3965 CRYPTO_EX_DATA ex_data; // application specific data
3966
3967 // These are used to make removal of session-ids more efficient and to
3968 // implement a maximum cache size.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003969 SSL_SESSION *prev = nullptr, *next = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003970
David Benjaminbfdd1a92018-06-29 16:26:38 -04003971 bssl::Array<uint8_t> ticket;
David Benjamin1c68fa22018-06-02 19:02:46 -04003972
David Benjaminbfdd1a92018-06-29 16:26:38 -04003973 bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin1c68fa22018-06-02 19:02:46 -04003974
3975 // The OCSP response that came with the session.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003976 bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin1c68fa22018-06-02 19:02:46 -04003977
3978 // peer_sha256 contains the SHA-256 hash of the peer's certificate if
3979 // |peer_sha256_valid| is true.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003980 uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003981
3982 // original_handshake_hash contains the handshake hash (either SHA-1+MD5 or
3983 // SHA-2, depending on TLS version) for the original, full handshake that
3984 // created a session. This is used by Channel IDs during resumption.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003985 uint8_t original_handshake_hash[EVP_MAX_MD_SIZE] = {0};
3986 uint8_t original_handshake_hash_len = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003987
David Benjaminbfdd1a92018-06-29 16:26:38 -04003988 uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds
David Benjamin1c68fa22018-06-02 19:02:46 -04003989
David Benjaminbfdd1a92018-06-29 16:26:38 -04003990 uint32_t ticket_age_add = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003991
3992 // ticket_max_early_data is the maximum amount of data allowed to be sent as
3993 // early data. If zero, 0-RTT is disallowed.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003994 uint32_t ticket_max_early_data = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003995
3996 // early_alpn is the ALPN protocol from the initial handshake. This is only
3997 // stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT
Steven Valdez51607f12020-08-05 10:46:05 -04003998 // resumptions. For the current connection's ALPN protocol, see
3999 // |alpn_selected| on |SSL3_STATE|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04004000 bssl::Array<uint8_t> early_alpn;
David Benjamin1c68fa22018-06-02 19:02:46 -04004001
Steven Valdez51607f12020-08-05 10:46:05 -04004002 // local_application_settings, if |has_application_settings| is true, is the
4003 // local ALPS value for this connection.
4004 bssl::Array<uint8_t> local_application_settings;
4005
4006 // peer_application_settings, if |has_application_settings| is true, is the
4007 // peer ALPS value for this connection.
4008 bssl::Array<uint8_t> peer_application_settings;
4009
David Benjamina3a71e92018-06-29 13:24:45 -04004010 // extended_master_secret is whether the master secret in this session was
David Benjamin1c68fa22018-06-02 19:02:46 -04004011 // generated using EMS and thus isn't vulnerable to the Triple Handshake
4012 // attack.
Jesse Selover6b0d8222018-08-14 12:47:29 -04004013 bool extended_master_secret : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04004014
David Benjamina3a71e92018-06-29 13:24:45 -04004015 // peer_sha256_valid is whether |peer_sha256| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04004016 bool peer_sha256_valid : 1; // Non-zero if peer_sha256 is valid
David Benjamin1c68fa22018-06-02 19:02:46 -04004017
4018 // not_resumable is used to indicate that session resumption is disallowed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04004019 bool not_resumable : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04004020
David Benjamina3a71e92018-06-29 13:24:45 -04004021 // ticket_age_add_valid is whether |ticket_age_add| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04004022 bool ticket_age_add_valid : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04004023
David Benjamina3a71e92018-06-29 13:24:45 -04004024 // is_server is whether this session was created by a server.
Jesse Selover6b0d8222018-08-14 12:47:29 -04004025 bool is_server : 1;
David Benjaminbfdd1a92018-06-29 16:26:38 -04004026
Nick Harper6bfd25c2020-03-30 17:15:19 -07004027 // is_quic indicates whether this session was created using QUIC.
4028 bool is_quic : 1;
4029
Steven Valdez51607f12020-08-05 10:46:05 -04004030 // has_application_settings indicates whether ALPS was negotiated in this
4031 // session.
4032 bool has_application_settings : 1;
4033
Nick Harper85194322020-05-20 16:59:29 -07004034 // quic_early_data_context is used to determine whether early data must be
Nick Harper7c522992020-04-30 14:15:49 -07004035 // rejected when performing a QUIC handshake.
Nick Harper85194322020-05-20 16:59:29 -07004036 bssl::Array<uint8_t> quic_early_data_context;
Nick Harper7c522992020-04-30 14:15:49 -07004037
David Benjaminbfdd1a92018-06-29 16:26:38 -04004038 private:
David Benjaminfbf10f02024-02-12 16:57:18 -05004039 friend RefCounted;
David Benjaminbfdd1a92018-06-29 16:26:38 -04004040 ~ssl_session_st();
David Benjamin1c68fa22018-06-02 19:02:46 -04004041};
4042
David Benjaminfbf10f02024-02-12 16:57:18 -05004043struct ssl_ech_keys_st : public bssl::RefCounted<ssl_ech_keys_st> {
4044 ssl_ech_keys_st() : RefCounted(CheckSubClass()) {}
Daniel McArdle00e434d2021-02-18 11:47:18 -05004045
David Benjamin1d58cd12021-05-04 15:24:24 -04004046 bssl::GrowableArray<bssl::UniquePtr<bssl::ECHServerConfig>> configs;
Daniel McArdle00e434d2021-02-18 11:47:18 -05004047
4048 private:
David Benjaminfbf10f02024-02-12 16:57:18 -05004049 friend RefCounted;
David Benjaminc3b373b2021-06-06 13:04:26 -04004050 ~ssl_ech_keys_st() = default;
Daniel McArdle00e434d2021-02-18 11:47:18 -05004051};
David Benjamine39ac8f2017-07-20 12:22:21 -04004052
David Benjaminc11ea9422017-08-29 16:33:21 -04004053#endif // OPENSSL_HEADER_SSL_INTERNAL_H