Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* 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 Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 142 | #ifndef OPENSSL_HEADER_SSL_INTERNAL_H |
| 143 | #define OPENSSL_HEADER_SSL_INTERNAL_H |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | |
| 145 | #include <openssl/base.h> |
| 146 | |
David Benjamin | c937699 | 2017-07-24 15:31:13 -0400 | [diff] [blame] | 147 | #include <stdlib.h> |
| 148 | |
David Benjamin | 6db6604 | 2024-02-20 13:57:21 -0500 | [diff] [blame] | 149 | #include <algorithm> |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 150 | #include <initializer_list> |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 151 | #include <limits> |
David Benjamin | d272dea | 2017-07-29 11:44:22 -0400 | [diff] [blame] | 152 | #include <new> |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 153 | #include <type_traits> |
| 154 | #include <utility> |
| 155 | |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | #include <openssl/aead.h> |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 157 | #include <openssl/curve25519.h> |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 158 | #include <openssl/err.h> |
David Benjamin | 070a6c3 | 2021-05-05 15:39:27 -0400 | [diff] [blame] | 159 | #include <openssl/hpke.h> |
David Benjamin | 4e84035 | 2017-10-11 18:41:11 -0400 | [diff] [blame] | 160 | #include <openssl/lhash.h> |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 161 | #include <openssl/mem.h> |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 162 | #include <openssl/span.h> |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 163 | #include <openssl/ssl.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | #include <openssl/stack.h> |
| 165 | |
David Benjamin | e52f4c4 | 2017-10-01 22:35:10 -0400 | [diff] [blame] | 166 | #include "../crypto/err/internal.h" |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 167 | #include "../crypto/internal.h" |
David Benjamin | 7f85116 | 2021-06-21 17:01:55 -0400 | [diff] [blame] | 168 | #include "../crypto/lhash/internal.h" |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 169 | |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 170 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 171 | #if defined(OPENSSL_WINDOWS) |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 172 | // Windows defines struct timeval in winsock2.h. |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 173 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 174 | #include <winsock2.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 175 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 176 | #else |
David Benjamin | 0abd6f2 | 2015-12-04 21:49:53 -0500 | [diff] [blame] | 177 | #include <sys/time.h> |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 178 | #endif |
| 179 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 180 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 181 | BSSL_NAMESPACE_BEGIN |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 182 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 183 | struct SSL_CONFIG; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 184 | struct SSL_HANDSHAKE; |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 185 | struct SSL_PROTOCOL_METHOD; |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 186 | struct SSL_X509_METHOD; |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 187 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 188 | // C++ utilities. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 189 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 190 | // 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 Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 195 | template <typename T, typename... Args> |
| 196 | T *New(Args &&... args) { |
David Benjamin | a4cb62f | 2017-07-31 13:52:31 -0400 | [diff] [blame] | 197 | void *t = OPENSSL_malloc(sizeof(T)); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 198 | if (t == nullptr) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 199 | return nullptr; |
| 200 | } |
David Benjamin | a4cb62f | 2017-07-31 13:52:31 -0400 | [diff] [blame] | 201 | return new (t) T(std::forward<Args>(args)...); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 202 | } |
| 203 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 204 | // Delete behaves like |delete| but uses |OPENSSL_free| to release memory. |
| 205 | // |
| 206 | // Note: unlike |delete| this does not support non-public destructors. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 207 | template <typename T> |
| 208 | void Delete(T *t) { |
| 209 | if (t != nullptr) { |
| 210 | t->~T(); |
| 211 | OPENSSL_free(t); |
| 212 | } |
| 213 | } |
| 214 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 215 | // 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 Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 217 | namespace internal { |
| 218 | template <typename T> |
David Benjamin | 493d5cb | 2022-04-18 17:20:27 -0400 | [diff] [blame] | 219 | struct DeleterImpl<T, std::enable_if_t<T::kAllowUniquePtr>> { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 220 | static void Free(T *t) { Delete(t); } |
| 221 | }; |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 222 | } // namespace internal |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 223 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 224 | // MakeUnique behaves like |std::make_unique| but returns nullptr on allocation |
| 225 | // error. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 226 | template <typename T, typename... Args> |
| 227 | UniquePtr<T> MakeUnique(Args &&... args) { |
| 228 | return UniquePtr<T>(New<T>(std::forward<Args>(args)...)); |
| 229 | } |
| 230 | |
David Benjamin | c528061 | 2024-02-15 00:05:41 -0500 | [diff] [blame] | 231 | // TODO(davidben): Remove these macros after April 2024, once the C++ runtime |
| 232 | // dependency has stuck. |
David Benjamin | 506be38 | 2017-07-25 15:18:18 -0400 | [diff] [blame] | 233 | #define HAS_VIRTUAL_DESTRUCTOR |
| 234 | #define PURE_VIRTUAL = 0 |
David Benjamin | c937699 | 2017-07-24 15:31:13 -0400 | [diff] [blame] | 235 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 236 | // Array<T> is an owning array of elements of |T|. |
| 237 | template <typename T> |
| 238 | class 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 Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 257 | bool empty() const { return size_ == 0; } |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 258 | |
| 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 Benjamin | 24545c5 | 2021-06-07 16:05:07 -0400 | [diff] [blame] | 263 | const T *begin() const { return data_; } |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 264 | T *end() { return data_ + size_; } |
David Benjamin | 24545c5 | 2021-06-07 16:05:07 -0400 | [diff] [blame] | 265 | const T *end() const { return data_ + size_; } |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 266 | |
| 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 Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 304 | data_ = reinterpret_cast<T *>(OPENSSL_malloc(new_size * sizeof(T))); |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 305 | if (data_ == nullptr) { |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 306 | 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 Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 315 | // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns |
| 316 | // true on success and false on error. |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 317 | bool CopyFrom(Span<const T> in) { |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 318 | if (!Init(in.size())) { |
| 319 | return false; |
| 320 | } |
David Benjamin | 6db6604 | 2024-02-20 13:57:21 -0500 | [diff] [blame] | 321 | std::copy(in.begin(), in.end(), data_); |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 325 | // 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 Benjamin | 662bfad | 2020-09-15 15:22:01 -0400 | [diff] [blame] | 331 | for (size_t i = new_size; i < size_; i++) { |
| 332 | data_[i].~T(); |
| 333 | } |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 334 | size_ = new_size; |
| 335 | } |
| 336 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 337 | private: |
| 338 | T *data_ = nullptr; |
| 339 | size_t size_ = 0; |
| 340 | }; |
| 341 | |
Daniel McArdle | ff746c1 | 2019-09-16 12:35:05 -0400 | [diff] [blame] | 342 | // 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. |
| 348 | template <typename T> |
| 349 | class 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 McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 364 | const T *data() const { return array_.data(); } |
| 365 | T *data() { return array_.data(); } |
Daniel McArdle | ff746c1 | 2019-09-16 12:35:05 -0400 | [diff] [blame] | 366 | 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 Benjamin | 24545c5 | 2021-06-07 16:05:07 -0400 | [diff] [blame] | 373 | const T *begin() const { return array_.data(); } |
Daniel McArdle | ff746c1 | 2019-09-16 12:35:05 -0400 | [diff] [blame] | 374 | T *end() { return array_.data() + size_; } |
David Benjamin | 24545c5 | 2021-06-07 16:05:07 -0400 | [diff] [blame] | 375 | const T *end() const { return array_.data() + size_; } |
Daniel McArdle | ff746c1 | 2019-09-16 12:35:05 -0400 | [diff] [blame] | 376 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 377 | void clear() { |
| 378 | size_ = 0; |
| 379 | array_.Reset(); |
| 380 | } |
| 381 | |
Daniel McArdle | ff746c1 | 2019-09-16 12:35:05 -0400 | [diff] [blame] | 382 | // 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 Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 440 | // CBBFinishArray behaves like |CBB_finish| but stores the result in an Array. |
Adam Langley | ddb57cf | 2018-01-26 09:17:53 -0800 | [diff] [blame] | 441 | OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out); |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 442 | |
Adam Langley | c215ce7 | 2023-05-08 11:35:14 -0700 | [diff] [blame] | 443 | // 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|. |
| 448 | template <typename T, typename Name> |
| 449 | inline 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 Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 464 | // 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. |
| 468 | template <typename Derived> |
| 469 | class 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 Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 506 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 507 | // Protocol versions. |
| 508 | // |
Steven Valdez | b84674b | 2018-08-28 10:14:07 -0400 | [diff] [blame] | 509 | // Due to DTLS's historical wire version differences, we maintain two notions of |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 510 | // 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 Valdez | b84674b | 2018-08-28 10:14:07 -0400 | [diff] [blame] | 518 | // used. DTLS versions map to the corresponding TLS versions. Protocol versions |
| 519 | // are sequential and may be compared numerically. |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 520 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 521 | // ssl_protocol_version_from_wire sets |*out| to the protocol version |
David Benjamin | ed9aed1 | 2017-09-27 19:24:09 -0400 | [diff] [blame] | 522 | // corresponding to wire version |version| and returns true. If |version| is not |
| 523 | // a valid TLS or DTLS version, it returns false. |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 524 | // |
| 525 | // Note this simultaneously handles both DTLS and TLS. Use one of the |
| 526 | // higher-level functions below for most operations. |
David Benjamin | ed9aed1 | 2017-09-27 19:24:09 -0400 | [diff] [blame] | 527 | bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 528 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 529 | // ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the |
| 530 | // minimum and maximum enabled protocol versions, respectively. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 531 | bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version, |
David Benjamin | ed9aed1 | 2017-09-27 19:24:09 -0400 | [diff] [blame] | 532 | uint16_t *out_max_version); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 533 | |
David Benjamin | ed9aed1 | 2017-09-27 19:24:09 -0400 | [diff] [blame] | 534 | // ssl_supports_version returns whether |hs| supports |version|. |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 535 | bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 536 | |
Matthew Braithwaite | 0e9e0ba | 2018-04-10 15:40:12 -0700 | [diff] [blame] | 537 | // ssl_method_supports_version returns whether |method| supports |version|. |
| 538 | bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method, |
| 539 | uint16_t version); |
| 540 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 541 | // ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 542 | // decreasing preference order. The version list is filtered to those whose |
| 543 | // protocol version is at least |extra_min_version|. |
| 544 | bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb, |
| 545 | uint16_t extra_min_version); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 546 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 547 | // ssl_negotiate_version negotiates a common version based on |hs|'s preferences |
David Benjamin | ed9aed1 | 2017-09-27 19:24:09 -0400 | [diff] [blame] | 548 | // 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 Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 550 | // and sets |*out_alert| to an alert to send. |
David Benjamin | ed9aed1 | 2017-09-27 19:24:09 -0400 | [diff] [blame] | 551 | bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 552 | uint16_t *out_version, const CBS *peer_versions); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 553 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 554 | // ssl_protocol_version returns |ssl|'s protocol version. It is an error to |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 555 | // call this function before the version is determined. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 556 | uint16_t ssl_protocol_version(const SSL *ssl); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 557 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 558 | // Cipher suites. |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 559 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 560 | BSSL_NAMESPACE_END |
David Benjamin | 33fc2ba | 2017-09-13 16:22:58 -0400 | [diff] [blame] | 561 | |
| 562 | struct 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-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 578 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 33fc2ba | 2017-09-13 16:22:58 -0400 | [diff] [blame] | 579 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 580 | // Bits for |algorithm_mkey| (key exchange algorithm). |
David Benjamin | e64d2c7 | 2017-07-12 16:31:08 -0400 | [diff] [blame] | 581 | #define SSL_kRSA 0x00000001u |
| 582 | #define SSL_kECDHE 0x00000002u |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 583 | // SSL_kPSK is only set for plain PSK, not ECDHE_PSK. |
David Benjamin | e64d2c7 | 2017-07-12 16:31:08 -0400 | [diff] [blame] | 584 | #define SSL_kPSK 0x00000004u |
| 585 | #define SSL_kGENERIC 0x00000008u |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 586 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 587 | // Bits for |algorithm_auth| (server authentication). |
David Benjamin | 90f0f05 | 2024-02-12 14:48:31 -0500 | [diff] [blame] | 588 | #define SSL_aRSA_SIGN 0x00000001u |
| 589 | #define SSL_aRSA_DECRYPT 0x00000002u |
| 590 | #define SSL_aECDSA 0x00000004u |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 591 | // SSL_aPSK is set for both PSK and ECDHE_PSK. |
David Benjamin | 90f0f05 | 2024-02-12 14:48:31 -0500 | [diff] [blame] | 592 | #define SSL_aPSK 0x00000008u |
| 593 | #define SSL_aGENERIC 0x00000010u |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 594 | |
David Benjamin | 90f0f05 | 2024-02-12 14:48:31 -0500 | [diff] [blame] | 595 | #define SSL_aCERT (SSL_aRSA_SIGN | SSL_aRSA_DECRYPT | SSL_aECDSA) |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 596 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 597 | // Bits for |algorithm_enc| (symmetric encryption). |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 598 | #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 Benjamin | de2d610 | 2023-04-21 17:32:21 -0400 | [diff] [blame] | 603 | #define SSL_CHACHA20POLY1305 0x00000020u |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 604 | |
| 605 | #define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM) |
| 606 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 607 | // Bits for |algorithm_mac| (symmetric authentication). |
David Benjamin | e64d2c7 | 2017-07-12 16:31:08 -0400 | [diff] [blame] | 608 | #define SSL_SHA1 0x00000001u |
Adam Langley | b1c6f45 | 2023-05-03 14:33:37 -0700 | [diff] [blame] | 609 | #define SSL_SHA256 0x00000002u |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 610 | // SSL_AEAD is set for all AEADs. |
Adam Langley | b1c6f45 | 2023-05-03 14:33:37 -0700 | [diff] [blame] | 611 | #define SSL_AEAD 0x00000004u |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 612 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 613 | // Bits for |algorithm_prf| (handshake digest). |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 614 | #define SSL_HANDSHAKE_MAC_DEFAULT 0x1 |
| 615 | #define SSL_HANDSHAKE_MAC_SHA256 0x2 |
| 616 | #define SSL_HANDSHAKE_MAC_SHA384 0x4 |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 617 | |
David Benjamin | bc42402 | 2019-07-21 12:05:20 -0400 | [diff] [blame] | 618 | // 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 Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 621 | // 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 |
| 651 | struct 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 Braithwaite | d2ed382 | 2018-07-10 16:27:22 -0700 | [diff] [blame] | 659 | bool Init(const SSLCipherPreferenceList &); |
| 660 | |
| 661 | void Remove(const SSL_CIPHER *cipher); |
David Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 662 | |
| 663 | UniquePtr<STACK_OF(SSL_CIPHER)> ciphers; |
| 664 | bool *in_group_flags = nullptr; |
| 665 | }; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 666 | |
Matthew Braithwaite | d2ed382 | 2018-07-10 16:27:22 -0700 | [diff] [blame] | 667 | // AllCiphers returns an array of all supported ciphers, sorted by id. |
| 668 | Span<const SSL_CIPHER> AllCiphers(); |
| 669 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 670 | // 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 Benjamin | f496249 | 2017-09-27 19:34:32 -0400 | [diff] [blame] | 674 | // ciphers. It returns true on success and false on error. |
| 675 | bool 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 Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 678 | uint16_t version, bool is_dtls); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 679 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 680 | // ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and |
| 681 | // |cipher|. |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 682 | const EVP_MD *ssl_get_handshake_digest(uint16_t version, |
| 683 | const SSL_CIPHER *cipher); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 684 | |
David Benjamin | 5be3a74 | 2017-10-28 16:08:58 -0400 | [diff] [blame] | 685 | // ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a |
David Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 686 | // 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 Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 689 | // 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 Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 691 | bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list, |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 692 | const bool has_aes_hw, const char *rule_str, |
| 693 | bool strict); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 694 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 695 | // ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth| |
David Benjamin | 90f0f05 | 2024-02-12 14:48:31 -0500 | [diff] [blame] | 696 | // values suitable for use with |key| in TLS 1.2 and below. |sign_ok| indicates |
| 697 | // whether |key| may be used for signing. |
| 698 | uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key, bool sign_ok); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 699 | |
David Benjamin | f496249 | 2017-09-27 19:34:32 -0400 | [diff] [blame] | 700 | // ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the |
| 701 | // server and, optionally, the client with a certificate. |
| 702 | bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 703 | |
David Benjamin | f496249 | 2017-09-27 19:34:32 -0400 | [diff] [blame] | 704 | // ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a |
| 705 | // ServerKeyExchange message. |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 706 | // |
David Benjamin | f496249 | 2017-09-27 19:34:32 -0400 | [diff] [blame] | 707 | // This function may return false while still allowing |cipher| an optional |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 708 | // ServerKeyExchange. This is the case for plain PSK ciphers. |
David Benjamin | f496249 | 2017-09-27 19:34:32 -0400 | [diff] [blame] | 709 | bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 710 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 711 | // 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 Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 714 | size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher); |
| 715 | |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 716 | // ssl_choose_tls13_cipher returns an |SSL_CIPHER| corresponding with the best |
David Benjamin | 860db9e | 2024-03-02 22:53:13 -0500 | [diff] [blame] | 717 | // available from |cipher_suites| compatible with |version| and |policy|. It |
| 718 | // returns NULL if there isn't a compatible cipher. |has_aes_hw| indicates if |
| 719 | // the choice should be made as if support for AES in hardware is available. |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 720 | const SSL_CIPHER *ssl_choose_tls13_cipher(CBS cipher_suites, bool has_aes_hw, |
David Benjamin | 860db9e | 2024-03-02 22:53:13 -0500 | [diff] [blame] | 721 | uint16_t version, |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 722 | enum ssl_compliance_policy_t policy); |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 723 | |
| 724 | // ssl_tls13_cipher_meets_policy returns true if |cipher_id| is acceptable given |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 725 | // |policy|. |
| 726 | bool ssl_tls13_cipher_meets_policy(uint16_t cipher_id, |
| 727 | enum ssl_compliance_policy_t policy); |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 728 | |
Adam Langley | b1c6f45 | 2023-05-03 14:33:37 -0700 | [diff] [blame] | 729 | // ssl_cipher_is_deprecated returns true if |cipher| is deprecated. |
| 730 | OPENSSL_EXPORT bool ssl_cipher_is_deprecated(const SSL_CIPHER *cipher); |
| 731 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 732 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 733 | // Transcript layer. |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 734 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 735 | // SSLTranscript maintains the handshake transcript as a combination of a |
| 736 | // buffer and running hash. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 737 | class SSLTranscript { |
| 738 | public: |
| 739 | SSLTranscript(); |
| 740 | ~SSLTranscript(); |
| 741 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 742 | SSLTranscript(SSLTranscript &&other) = default; |
| 743 | SSLTranscript &operator=(SSLTranscript &&other) = default; |
| 744 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 745 | // Init initializes the handshake transcript. If called on an existing |
| 746 | // transcript, it resets the transcript and hash. It returns true on success |
| 747 | // and false on failure. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 748 | bool Init(); |
| 749 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 750 | // InitHash initializes the handshake hash based on the PRF and contents of |
| 751 | // the handshake transcript. Subsequent calls to |Update| will update the |
| 752 | // rolling hash. It returns one on success and zero on failure. It is an error |
David Benjamin | d55f450 | 2021-08-11 13:19:19 -0400 | [diff] [blame] | 753 | // to call this function after the handshake buffer is released. This may be |
| 754 | // called multiple times to change the hash function. |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 755 | bool InitHash(uint16_t version, const SSL_CIPHER *cipher); |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 756 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 757 | // UpdateForHelloRetryRequest resets the rolling hash with the |
| 758 | // HelloRetryRequest construction. It returns true on success and false on |
| 759 | // failure. It is an error to call this function before the handshake buffer |
| 760 | // is released. |
| 761 | bool UpdateForHelloRetryRequest(); |
| 762 | |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 763 | // CopyToHashContext initializes |ctx| with |digest| and the data thus far in |
| 764 | // the transcript. It returns true on success and false on failure. If the |
| 765 | // handshake buffer is still present, |digest| may be any supported digest. |
| 766 | // Otherwise, |digest| must match the transcript hash. |
David Benjamin | b32aa05 | 2021-06-02 17:05:00 -0400 | [diff] [blame] | 767 | bool CopyToHashContext(EVP_MD_CTX *ctx, const EVP_MD *digest) const; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 768 | |
David Benjamin | b32aa05 | 2021-06-02 17:05:00 -0400 | [diff] [blame] | 769 | Span<const uint8_t> buffer() const { |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 770 | return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data), |
| 771 | buffer_->length); |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 772 | } |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 773 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 774 | // FreeBuffer releases the handshake buffer. Subsequent calls to |
| 775 | // |Update| will not update the handshake buffer. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 776 | void FreeBuffer(); |
| 777 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 778 | // DigestLen returns the length of the PRF hash. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 779 | size_t DigestLen() const; |
| 780 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 781 | // Digest returns the PRF hash. For TLS 1.1 and below, this is |
| 782 | // |EVP_md5_sha1|. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 783 | const EVP_MD *Digest() const; |
| 784 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 785 | // Update adds |in| to the handshake buffer and handshake hash, whichever is |
| 786 | // enabled. It returns true on success and false on failure. |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 787 | bool Update(Span<const uint8_t> in); |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 788 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 789 | // GetHash writes the handshake hash to |out| which must have room for at |
| 790 | // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to |
| 791 | // the number of bytes written. Otherwise, it returns false. |
David Benjamin | b32aa05 | 2021-06-02 17:05:00 -0400 | [diff] [blame] | 792 | bool GetHash(uint8_t *out, size_t *out_len) const; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 793 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 794 | // GetFinishedMAC computes the MAC for the Finished message into the bytes |
| 795 | // pointed by |out| and writes the number of bytes to |*out_len|. |out| must |
| 796 | // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false |
| 797 | // on failure. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 798 | bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session, |
David Benjamin | b32aa05 | 2021-06-02 17:05:00 -0400 | [diff] [blame] | 799 | bool from_server) const; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 800 | |
| 801 | private: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 802 | // buffer_, if non-null, contains the handshake transcript. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 803 | UniquePtr<BUF_MEM> buffer_; |
David Benjamin | a4e9f8d | 2018-06-26 00:12:52 -0400 | [diff] [blame] | 804 | // hash, if initialized with an |EVP_MD|, maintains the handshake hash. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 805 | ScopedEVP_MD_CTX hash_; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 806 | }; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 807 | |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 808 | // tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret| |
| 809 | // as the secret and |label| as the label. |seed1| and |seed2| are concatenated |
| 810 | // to form the seed parameter. It returns true on success and false on failure. |
| 811 | bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out, |
| 812 | Span<const uint8_t> secret, Span<const char> label, |
| 813 | Span<const uint8_t> seed1, Span<const uint8_t> seed2); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 814 | |
| 815 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 816 | // Encryption layer. |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 817 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 818 | // SSLAEADContext contains information about an AEAD that is being used to |
| 819 | // encrypt an SSL connection. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 820 | class SSLAEADContext { |
| 821 | public: |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 822 | SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 823 | ~SSLAEADContext(); |
David Benjamin | 9a89250 | 2017-07-25 22:26:44 -0400 | [diff] [blame] | 824 | static constexpr bool kAllowUniquePtr = true; |
| 825 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 826 | SSLAEADContext(const SSLAEADContext &&) = delete; |
| 827 | SSLAEADContext &operator=(const SSLAEADContext &&) = delete; |
| 828 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 829 | // CreateNullCipher creates an |SSLAEADContext| for the null cipher. |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 830 | static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 831 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 832 | // Create creates an |SSLAEADContext| using the supplied key material. It |
| 833 | // returns nullptr on error. Only one of |Open| or |Seal| may be used with the |
| 834 | // resulting object, depending on |direction|. |version| is the normalized |
| 835 | // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef. |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 836 | static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction, |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 837 | uint16_t version, bool is_dtls, |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 838 | const SSL_CIPHER *cipher, |
| 839 | Span<const uint8_t> enc_key, |
| 840 | Span<const uint8_t> mac_key, |
| 841 | Span<const uint8_t> fixed_iv); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 842 | |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 843 | // CreatePlaceholderForQUIC creates a placeholder |SSLAEADContext| for the |
| 844 | // given cipher and version. The resulting object can be queried for various |
| 845 | // properties but cannot encrypt or decrypt data. |
| 846 | static UniquePtr<SSLAEADContext> CreatePlaceholderForQUIC( |
| 847 | uint16_t version, const SSL_CIPHER *cipher); |
| 848 | |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 849 | // SetVersionIfNullCipher sets the version the SSLAEADContext for the null |
| 850 | // cipher, to make version-specific determinations in the record layer prior |
| 851 | // to a cipher being selected. |
| 852 | void SetVersionIfNullCipher(uint16_t version); |
| 853 | |
| 854 | // ProtocolVersion returns the protocol version associated with this |
| 855 | // SSLAEADContext. It can only be called once |version_| has been set to a |
| 856 | // valid value. |
| 857 | uint16_t ProtocolVersion() const; |
| 858 | |
| 859 | // RecordVersion returns the record version that should be used with this |
| 860 | // SSLAEADContext for record construction and crypto. |
| 861 | uint16_t RecordVersion() const; |
| 862 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 863 | const SSL_CIPHER *cipher() const { return cipher_; } |
| 864 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 865 | // is_null_cipher returns true if this is the null cipher. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 866 | bool is_null_cipher() const { return !cipher_; } |
| 867 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 868 | // ExplicitNonceLen returns the length of the explicit nonce. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 869 | size_t ExplicitNonceLen() const; |
| 870 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 871 | // MaxOverhead returns the maximum overhead of calling |Seal|. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 872 | size_t MaxOverhead() const; |
| 873 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 874 | // SuffixLen calculates the suffix length written by |SealScatter| and writes |
| 875 | // it to |*out_suffix_len|. It returns true on success and false on error. |
| 876 | // |in_len| and |extra_in_len| should equal the argument of the same names |
| 877 | // passed to |SealScatter|. |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 878 | bool SuffixLen(size_t *out_suffix_len, size_t in_len, |
| 879 | size_t extra_in_len) const; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 880 | |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 881 | // CiphertextLen calculates the total ciphertext length written by |
| 882 | // |SealScatter| and writes it to |*out_len|. It returns true on success and |
| 883 | // false on error. |in_len| and |extra_in_len| should equal the argument of |
| 884 | // the same names passed to |SealScatter|. |
| 885 | bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const; |
| 886 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 887 | // Open authenticates and decrypts |in| in-place. On success, it sets |*out| |
| 888 | // to the plaintext in |in| and returns true. Otherwise, it returns |
| 889 | // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|. |
| 890 | bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version, |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 891 | uint64_t seqnum, Span<const uint8_t> header, Span<uint8_t> in); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 892 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 893 | // Seal encrypts and authenticates |in_len| bytes from |in| and writes the |
| 894 | // result to |out|. It returns true on success and false on error. |
| 895 | // |
| 896 | // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 897 | bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type, |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 898 | uint16_t record_version, uint64_t seqnum, |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 899 | Span<const uint8_t> header, const uint8_t *in, size_t in_len); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 900 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 901 | // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits |
| 902 | // the result between |out_prefix|, |out| and |out_suffix|. It returns one on |
| 903 | // success and zero on error. |
| 904 | // |
| 905 | // On successful return, exactly |ExplicitNonceLen| bytes are written to |
| 906 | // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to |
| 907 | // |out_suffix|. |
| 908 | // |
| 909 | // |extra_in| may point to an additional plaintext buffer. If present, |
| 910 | // |extra_in_len| additional bytes are encrypted and authenticated, and the |
| 911 | // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should |
| 912 | // be used to size |out_suffix| accordingly. |
| 913 | // |
| 914 | // If |in| and |out| alias then |out| must be == |in|. Other arguments may not |
| 915 | // alias anything. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 916 | bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix, |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 917 | uint8_t type, uint16_t record_version, uint64_t seqnum, |
| 918 | Span<const uint8_t> header, const uint8_t *in, size_t in_len, |
| 919 | const uint8_t *extra_in, size_t extra_in_len); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 920 | |
| 921 | bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const; |
| 922 | |
| 923 | private: |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 924 | // GetAdditionalData returns the additional data, writing into |storage| if |
| 925 | // necessary. |
| 926 | Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type, |
| 927 | uint16_t record_version, |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 928 | uint64_t seqnum, size_t plaintext_len, |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 929 | Span<const uint8_t> header); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 930 | |
| 931 | const SSL_CIPHER *cipher_; |
| 932 | ScopedEVP_AEAD_CTX ctx_; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 933 | // fixed_nonce_ contains any bytes of the nonce that are fixed for all |
| 934 | // records. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 935 | uint8_t fixed_nonce_[12]; |
| 936 | uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0; |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 937 | // version_ is the wire version that should be used with this AEAD. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 938 | uint16_t version_; |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 939 | // is_dtls_ is whether DTLS is being used with this AEAD. |
| 940 | bool is_dtls_; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 941 | // variable_nonce_included_in_record_ is true if the variable nonce |
| 942 | // for a record is included as a prefix before the ciphertext. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 943 | bool variable_nonce_included_in_record_ : 1; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 944 | // random_variable_nonce_ is true if the variable nonce is |
| 945 | // randomly generated, rather than derived from the sequence |
| 946 | // number. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 947 | bool random_variable_nonce_ : 1; |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 948 | // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the |
| 949 | // variable nonce rather than prepended. |
| 950 | bool xor_fixed_nonce_ : 1; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 951 | // omit_length_in_ad_ is true if the length should be omitted in the |
| 952 | // AEAD's ad parameter. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 953 | bool omit_length_in_ad_ : 1; |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 954 | // ad_is_header_ is true if the AEAD's ad parameter is the record header. |
| 955 | bool ad_is_header_ : 1; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 956 | }; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 957 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 958 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 959 | // DTLS replay bitmap. |
David Benjamin | 7446a3b | 2015-07-25 17:53:57 -0400 | [diff] [blame] | 960 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 961 | // DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect |
| 962 | // replayed packets. It should be initialized by zeroing every field. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 963 | struct DTLS1_BITMAP { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 964 | // map is a bit mask of the last 64 sequence numbers. Bit |
| 965 | // |1<<i| corresponds to |max_seq_num - i|. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 966 | uint64_t map = 0; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 967 | // max_seq_num is the largest sequence number seen so far as a 64-bit |
| 968 | // integer. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 969 | uint64_t max_seq_num = 0; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 970 | }; |
David Benjamin | 7446a3b | 2015-07-25 17:53:57 -0400 | [diff] [blame] | 971 | |
| 972 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 973 | // Record layer. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 974 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 975 | // ssl_record_prefix_len returns the length of the prefix before the ciphertext |
| 976 | // of a record for |ssl|. |
| 977 | // |
| 978 | // TODO(davidben): Expose this as part of public API once the high-level |
| 979 | // buffer-free APIs are available. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 980 | size_t ssl_record_prefix_len(const SSL *ssl); |
| 981 | |
| 982 | enum ssl_open_record_t { |
| 983 | ssl_open_record_success, |
| 984 | ssl_open_record_discard, |
| 985 | ssl_open_record_partial, |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 986 | ssl_open_record_close_notify, |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 987 | ssl_open_record_error, |
| 988 | }; |
| 989 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 990 | // tls_open_record decrypts a record from |in| in-place. |
| 991 | // |
| 992 | // If the input did not contain a complete record, it returns |
| 993 | // |ssl_open_record_partial|. It sets |*out_consumed| to the total number of |
| 994 | // bytes necessary. It is guaranteed that a successful call to |tls_open_record| |
| 995 | // will consume at least that many bytes. |
| 996 | // |
| 997 | // Otherwise, it sets |*out_consumed| to the number of bytes of input |
| 998 | // consumed. Note that input may be consumed on all return codes if a record was |
| 999 | // decrypted. |
| 1000 | // |
| 1001 | // On success, it returns |ssl_open_record_success|. It sets |*out_type| to the |
| 1002 | // record type and |*out| to the record body in |in|. Note that |*out| may be |
| 1003 | // empty. |
| 1004 | // |
| 1005 | // If a record was successfully processed but should be discarded, it returns |
| 1006 | // |ssl_open_record_discard|. |
| 1007 | // |
David Benjamin | 751d1a1 | 2017-09-25 13:37:30 -0400 | [diff] [blame] | 1008 | // If a record was successfully processed but is a close_notify, it returns |
| 1009 | // |ssl_open_record_close_notify|. |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1010 | // |
David Benjamin | 751d1a1 | 2017-09-25 13:37:30 -0400 | [diff] [blame] | 1011 | // On failure or fatal alert, it returns |ssl_open_record_error| and sets |
| 1012 | // |*out_alert| to an alert to emit, or zero if no alert should be emitted. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 1013 | enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, |
| 1014 | Span<uint8_t> *out, size_t *out_consumed, |
| 1015 | uint8_t *out_alert, Span<uint8_t> in); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1016 | |
David Benjamin | a05d427 | 2017-10-06 19:34:55 -0400 | [diff] [blame] | 1017 | // dtls_open_record implements |tls_open_record| for DTLS. It only returns |
| 1018 | // |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to |
| 1019 | // zero. The caller should read one packet and try again. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 1020 | enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, |
| 1021 | Span<uint8_t> *out, |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 1022 | size_t *out_consumed, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 1023 | uint8_t *out_alert, Span<uint8_t> in); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1024 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1025 | // ssl_seal_align_prefix_len returns the length of the prefix before the start |
| 1026 | // of the bulk of the ciphertext when sealing a record with |ssl|. Callers may |
| 1027 | // use this to align buffers. |
| 1028 | // |
| 1029 | // Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte |
| 1030 | // record and is the offset into second record's ciphertext. Thus sealing a |
| 1031 | // small record may result in a smaller output than this value. |
| 1032 | // |
| 1033 | // TODO(davidben): Is this alignment valuable? Record-splitting makes this a |
| 1034 | // mess. |
David Benjamin | 1a01e1f | 2016-06-08 18:31:24 -0400 | [diff] [blame] | 1035 | size_t ssl_seal_align_prefix_len(const SSL *ssl); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1036 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1037 | // tls_seal_record seals a new record of type |type| and body |in| and writes it |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1038 | // to |out|. At most |max_out| bytes will be written. It returns true on success |
| 1039 | // and false on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC |
| 1040 | // 1/n-1 record splitting and may write two records concatenated. |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1041 | // |
| 1042 | // For a large record, the bulk of the ciphertext will begin |
| 1043 | // |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may |
| 1044 | // improve performance. It writes at most |in_len| + |SSL_max_seal_overhead| |
| 1045 | // bytes to |out|. |
| 1046 | // |
| 1047 | // |in| and |out| may not alias. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1048 | bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 1049 | uint8_t type, const uint8_t *in, size_t in_len); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1050 | |
| 1051 | enum dtls1_use_epoch_t { |
| 1052 | dtls1_use_previous_epoch, |
| 1053 | dtls1_use_current_epoch, |
| 1054 | }; |
| 1055 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1056 | // dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a |
| 1057 | // record. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 1058 | size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch); |
| 1059 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1060 | // dtls_seal_prefix_len returns the number of bytes of prefix to reserve in |
| 1061 | // front of the plaintext when sealing a record in-place. |
David Benjamin | a772b16 | 2017-01-24 17:51:33 -0500 | [diff] [blame] | 1062 | size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch); |
| 1063 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1064 | // dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects |
| 1065 | // which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out| |
| 1066 | // may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes |
| 1067 | // ahead of |out|. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1068 | bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 1069 | uint8_t type, const uint8_t *in, size_t in_len, |
| 1070 | enum dtls1_use_epoch_t use_epoch); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1071 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1072 | // ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown |
| 1073 | // state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|, |
| 1074 | // |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as |
| 1075 | // appropriate. |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 1076 | enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 1077 | Span<const uint8_t> in); |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 1078 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1079 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1080 | // Private key operations. |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1081 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1082 | // ssl_private_key_* perform the corresponding operation on |
| 1083 | // |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they |
| 1084 | // call the corresponding function or |complete| depending on whether there is a |
| 1085 | // pending operation. Otherwise, they implement the operation with |
| 1086 | // |EVP_PKEY|. |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1087 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1088 | enum ssl_private_key_result_t ssl_private_key_sign( |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1089 | SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 1090 | uint16_t sigalg, Span<const uint8_t> in); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1091 | |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 1092 | enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs, |
| 1093 | uint8_t *out, |
| 1094 | size_t *out_len, |
| 1095 | size_t max_out, |
| 1096 | Span<const uint8_t> in); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1097 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 1098 | // ssl_pkey_supports_algorithm returns whether |pkey| may be used to sign |
| 1099 | // |sigalg|. |
| 1100 | bool ssl_pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey, |
| 1101 | uint16_t sigalg); |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 1102 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1103 | // ssl_public_key_verify verifies that the |signature| is valid for the public |
| 1104 | // key |pkey| and input |in|, using the signature algorithm |sigalg|. |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 1105 | bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature, |
| 1106 | uint16_t sigalg, EVP_PKEY *pkey, |
| 1107 | Span<const uint8_t> in); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1108 | |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 1109 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1110 | // Key shares. |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1111 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 1112 | // SSLKeyShare abstracts over KEM-like constructions, for use with TLS 1.2 ECDHE |
| 1113 | // cipher suites and the TLS 1.3 key_share extension. |
| 1114 | // |
| 1115 | // TODO(davidben): This class is named SSLKeyShare after the TLS 1.3 key_share |
| 1116 | // extension, but it really implements a KEM abstraction. Additionally, we use |
| 1117 | // the same type for Encap, which is a one-off, stateless operation, as Generate |
| 1118 | // and Decap. Slightly tidier would be for Generate to return a new SSLKEMKey |
| 1119 | // (or we introduce EVP_KEM and EVP_KEM_KEY), with a Decap method, and for Encap |
| 1120 | // to be static function. |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 1121 | class SSLKeyShare { |
| 1122 | public: |
| 1123 | virtual ~SSLKeyShare() {} |
David Benjamin | 9a89250 | 2017-07-25 22:26:44 -0400 | [diff] [blame] | 1124 | static constexpr bool kAllowUniquePtr = true; |
David Benjamin | c937699 | 2017-07-24 15:31:13 -0400 | [diff] [blame] | 1125 | HAS_VIRTUAL_DESTRUCTOR |
David Benjamin | b917909 | 2016-10-26 13:47:33 -0400 | [diff] [blame] | 1126 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1127 | // Create returns a SSLKeyShare instance for use with group |group_id| or |
| 1128 | // nullptr on error. |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 1129 | static UniquePtr<SSLKeyShare> Create(uint16_t group_id); |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1130 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1131 | // GroupID returns the group ID. |
David Benjamin | c937699 | 2017-07-24 15:31:13 -0400 | [diff] [blame] | 1132 | virtual uint16_t GroupID() const PURE_VIRTUAL; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1133 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 1134 | // Generate generates a keypair and writes the public key to |out_public_key|. |
| 1135 | // It returns true on success and false on error. |
| 1136 | virtual bool Generate(CBB *out_public_key) PURE_VIRTUAL; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1137 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 1138 | // Encap generates an ephemeral, symmetric secret and encapsulates it with |
| 1139 | // |peer_key|. On success, it returns true, writes the encapsulated secret to |
| 1140 | // |out_ciphertext|, and sets |*out_secret| to the shared secret. On failure, |
| 1141 | // it returns false and sets |*out_alert| to an alert to send to the peer. |
| 1142 | virtual bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret, |
| 1143 | uint8_t *out_alert, |
| 1144 | Span<const uint8_t> peer_key) PURE_VIRTUAL; |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1145 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 1146 | // Decap decapsulates the symmetric secret in |ciphertext|. On success, it |
| 1147 | // returns true and sets |*out_secret| to the shared secret. On failure, it |
| 1148 | // returns false and sets |*out_alert| to an alert to send to the peer. |
| 1149 | virtual bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 1150 | Span<const uint8_t> ciphertext) PURE_VIRTUAL; |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 1151 | |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 1152 | // SerializePrivateKey writes the private key to |out|, returning true if |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 1153 | // successful and false otherwise. It should be called after |Generate|. |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 1154 | virtual bool SerializePrivateKey(CBB *out) { return false; } |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 1155 | |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 1156 | // DeserializePrivateKey initializes the state of the key exchange from |in|, |
| 1157 | // returning true if successful and false otherwise. |
| 1158 | virtual bool DeserializePrivateKey(CBS *in) { return false; } |
David Benjamin | b917909 | 2016-10-26 13:47:33 -0400 | [diff] [blame] | 1159 | }; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1160 | |
Matthew Braithwaite | c65eb2c | 2018-11-02 17:29:35 -0700 | [diff] [blame] | 1161 | struct NamedGroup { |
| 1162 | int nid; |
| 1163 | uint16_t group_id; |
David Benjamin | 1b724a6 | 2023-05-03 17:58:01 -0400 | [diff] [blame] | 1164 | const char name[32], alias[32]; |
Matthew Braithwaite | c65eb2c | 2018-11-02 17:29:35 -0700 | [diff] [blame] | 1165 | }; |
| 1166 | |
| 1167 | // NamedGroups returns all supported groups. |
| 1168 | Span<const NamedGroup> NamedGroups(); |
| 1169 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1170 | // ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1171 | // sets |*out_group_id| to the group ID and returns true. Otherwise, it returns |
| 1172 | // false. |
| 1173 | bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid); |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1174 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1175 | // ssl_name_to_group_id looks up the group corresponding to the |name| string of |
| 1176 | // length |len|. On success, it sets |*out_group_id| to the group ID and returns |
| 1177 | // true. Otherwise, it returns false. |
| 1178 | bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len); |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 1179 | |
David Benjamin | 6cf9820 | 2023-05-26 22:03:30 -0400 | [diff] [blame] | 1180 | // ssl_group_id_to_nid returns the NID corresponding to |group_id| or |
| 1181 | // |NID_undef| if unknown. |
| 1182 | int ssl_group_id_to_nid(uint16_t group_id); |
| 1183 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1184 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1185 | // Handshake messages. |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1186 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1187 | struct SSLMessage { |
| 1188 | bool is_v2_hello; |
| 1189 | uint8_t type; |
| 1190 | CBS body; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1191 | // raw is the entire serialized handshake message, including the TLS or DTLS |
| 1192 | // message header. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1193 | CBS raw; |
| 1194 | }; |
| 1195 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1196 | // SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including |
| 1197 | // ChangeCipherSpec, in the longest handshake flight. Currently this is the |
| 1198 | // client's second leg in a full handshake when client certificates, NPN, and |
| 1199 | // Channel ID, are all enabled. |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 1200 | #define SSL_MAX_HANDSHAKE_FLIGHT 7 |
| 1201 | |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 1202 | extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE]; |
Steven Valdez | f1af129 | 2018-08-13 10:54:48 -0400 | [diff] [blame] | 1203 | extern const uint8_t kTLS12DowngradeRandom[8]; |
| 1204 | extern const uint8_t kTLS13DowngradeRandom[8]; |
David Benjamin | 6965d25 | 2018-11-19 15:49:56 -0600 | [diff] [blame] | 1205 | extern const uint8_t kJDK11DowngradeRandom[8]; |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 1206 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1207 | // ssl_max_handshake_message_len returns the maximum number of bytes permitted |
| 1208 | // in a handshake message for |ssl|. |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1209 | size_t ssl_max_handshake_message_len(const SSL *ssl); |
| 1210 | |
David Benjamin | 33febf6 | 2017-10-07 16:52:50 -0400 | [diff] [blame] | 1211 | // tls_can_accept_handshake_data returns whether |ssl| is able to accept more |
| 1212 | // data into handshake buffer. |
| 1213 | bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert); |
| 1214 | |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1215 | // tls_has_unprocessed_handshake_data returns whether there is buffered |
| 1216 | // handshake data that has not been consumed by |get_message|. |
| 1217 | bool tls_has_unprocessed_handshake_data(const SSL *ssl); |
| 1218 | |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 1219 | // tls_append_handshake_data appends |data| to the handshake buffer. It returns |
| 1220 | // true on success and false on allocation failure. |
| 1221 | bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data); |
| 1222 | |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1223 | // dtls_has_unprocessed_handshake_data behaves like |
| 1224 | // |tls_has_unprocessed_handshake_data| for DTLS. |
| 1225 | bool dtls_has_unprocessed_handshake_data(const SSL *ssl); |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 1226 | |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 1227 | // tls_flush_pending_hs_data flushes any handshake plaintext data. |
| 1228 | bool tls_flush_pending_hs_data(SSL *ssl); |
| 1229 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1230 | struct DTLS_OUTGOING_MESSAGE { |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 1231 | DTLS_OUTGOING_MESSAGE() {} |
| 1232 | DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete; |
| 1233 | DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete; |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 1234 | |
| 1235 | void Clear(); |
| 1236 | |
David Benjamin | 46af243 | 2022-09-04 15:12:57 -0400 | [diff] [blame] | 1237 | Array<uint8_t> data; |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 1238 | uint16_t epoch = 0; |
| 1239 | bool is_ccs = false; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1240 | }; |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 1241 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1242 | // dtls_clear_outgoing_messages releases all buffered outgoing messages. |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 1243 | void dtls_clear_outgoing_messages(SSL *ssl); |
| 1244 | |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1245 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1246 | // Callbacks. |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 1247 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1248 | // ssl_do_info_callback calls |ssl|'s info callback, if set. |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 1249 | void ssl_do_info_callback(const SSL *ssl, int type, int value); |
| 1250 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1251 | // ssl_do_msg_callback calls |ssl|'s message callback, if set. |
Adam Langley | c9827e0 | 2019-04-12 14:46:50 -0700 | [diff] [blame] | 1252 | void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 1253 | Span<const uint8_t> in); |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 1254 | |
| 1255 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1256 | // Transport buffers. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1257 | |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 1258 | class SSLBuffer { |
| 1259 | public: |
| 1260 | SSLBuffer() {} |
| 1261 | ~SSLBuffer() { Clear(); } |
| 1262 | |
| 1263 | SSLBuffer(const SSLBuffer &) = delete; |
| 1264 | SSLBuffer &operator=(const SSLBuffer &) = delete; |
| 1265 | |
| 1266 | uint8_t *data() { return buf_ + offset_; } |
| 1267 | size_t size() const { return size_; } |
| 1268 | bool empty() const { return size_ == 0; } |
| 1269 | size_t cap() const { return cap_; } |
| 1270 | |
| 1271 | Span<uint8_t> span() { return MakeSpan(data(), size()); } |
| 1272 | |
| 1273 | Span<uint8_t> remaining() { |
| 1274 | return MakeSpan(data() + size(), cap() - size()); |
| 1275 | } |
| 1276 | |
| 1277 | // Clear releases the buffer. |
| 1278 | void Clear(); |
| 1279 | |
| 1280 | // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such |
| 1281 | // that data written after |header_len| is aligned to a |
| 1282 | // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false |
| 1283 | // on error. |
| 1284 | bool EnsureCap(size_t header_len, size_t new_cap); |
| 1285 | |
| 1286 | // DidWrite extends the buffer by |len|. The caller must have filled in to |
| 1287 | // this point. |
| 1288 | void DidWrite(size_t len); |
| 1289 | |
| 1290 | // Consume consumes |len| bytes from the front of the buffer. The memory |
| 1291 | // consumed will remain valid until the next call to |DiscardConsumed| or |
| 1292 | // |Clear|. |
| 1293 | void Consume(size_t len); |
| 1294 | |
| 1295 | // DiscardConsumed discards the consumed bytes from the buffer. If the buffer |
| 1296 | // is now empty, it releases memory used by it. |
| 1297 | void DiscardConsumed(); |
| 1298 | |
| 1299 | private: |
| 1300 | // buf_ is the memory allocated for this buffer. |
| 1301 | uint8_t *buf_ = nullptr; |
| 1302 | // offset_ is the offset into |buf_| which the buffer contents start at. |
| 1303 | uint16_t offset_ = 0; |
| 1304 | // size_ is the size of the buffer contents from |buf_| + |offset_|. |
| 1305 | uint16_t size_ = 0; |
| 1306 | // cap_ is how much memory beyond |buf_| + |offset_| is available. |
| 1307 | uint16_t cap_ = 0; |
David Benjamin | c733754 | 2019-11-28 08:32:16 -0500 | [diff] [blame] | 1308 | // inline_buf_ is a static buffer for short reads. |
| 1309 | uint8_t inline_buf_[SSL3_RT_HEADER_LENGTH]; |
| 1310 | // buf_allocated_ is true if |buf_| points to allocated data and must be freed |
| 1311 | // or false if it points into |inline_buf_|. |
| 1312 | bool buf_allocated_ = false; |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 1313 | }; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1314 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1315 | // ssl_read_buffer_extend_to extends the read buffer to the desired length. For |
| 1316 | // TLS, it reads to the end of the buffer until the buffer is |len| bytes |
| 1317 | // long. For DTLS, it reads a new packet and ignores |len|. It returns one on |
| 1318 | // success, zero on EOF, and a negative number on error. |
| 1319 | // |
| 1320 | // It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is |
| 1321 | // non-empty. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1322 | int ssl_read_buffer_extend_to(SSL *ssl, size_t len); |
| 1323 | |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 1324 | // ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer| |
| 1325 | // to a record-processing function. If |ret| is a success or if the caller |
| 1326 | // should retry, it returns one and sets |*out_retry|. Otherwise, it returns <= |
| 1327 | // 0. |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 1328 | int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret, |
| 1329 | size_t consumed, uint8_t alert); |
| 1330 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1331 | // ssl_write_buffer_flush flushes the write buffer to the transport. It returns |
| 1332 | // one on success and <= 0 on error. For DTLS, whether or not the write |
| 1333 | // succeeds, the write buffer will be cleared. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1334 | int ssl_write_buffer_flush(SSL *ssl); |
| 1335 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1336 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1337 | // Certificate functions. |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1338 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1339 | // ssl_parse_cert_chain parses a certificate list from |cbs| in the format used |
| 1340 | // by a TLS Certificate message. On success, it advances |cbs| and returns |
| 1341 | // true. Otherwise, it returns false and sets |*out_alert| to an alert to send |
| 1342 | // to the peer. |
| 1343 | // |
| 1344 | // If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to |
| 1345 | // the certificate chain and the leaf certificate's public key |
| 1346 | // respectively. Otherwise, both will be set to nullptr. |
| 1347 | // |
| 1348 | // If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the |
| 1349 | // SHA-256 hash of the leaf to |out_leaf_sha256|. |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 1350 | bool ssl_parse_cert_chain(uint8_t *out_alert, |
| 1351 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain, |
| 1352 | UniquePtr<EVP_PKEY> *out_pubkey, |
| 1353 | uint8_t *out_leaf_sha256, CBS *cbs, |
| 1354 | CRYPTO_BUFFER_POOL *pool); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1355 | |
Jesse Selover | d7266ec | 2019-01-30 16:06:10 -0500 | [diff] [blame] | 1356 | enum ssl_key_usage_t { |
| 1357 | key_usage_digital_signature = 0, |
| 1358 | key_usage_encipherment = 2, |
| 1359 | }; |
| 1360 | |
| 1361 | // ssl_cert_check_key_usage parses the DER-encoded, X.509 certificate in |in| |
| 1362 | // and returns true if doesn't specify a key usage or, if it does, if it |
| 1363 | // includes |bit|. Otherwise it pushes to the error queue and returns false. |
David Benjamin | a1dffbf | 2022-10-25 16:29:43 -0400 | [diff] [blame] | 1364 | OPENSSL_EXPORT bool ssl_cert_check_key_usage(const CBS *in, |
| 1365 | enum ssl_key_usage_t bit); |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 1366 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1367 | // ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509 |
| 1368 | // certificate in |in|. It returns an allocated |EVP_PKEY| or else returns |
| 1369 | // nullptr and pushes to the error queue. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1370 | UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in); |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1371 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1372 | // ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a |
| 1373 | // TLS CertificateRequest message. On success, it returns a newly-allocated |
| 1374 | // |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and |
| 1375 | // sets |*out_alert| to an alert to send to the peer. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1376 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl, |
| 1377 | uint8_t *out_alert, |
| 1378 | CBS *cbs); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1379 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1380 | // ssl_has_client_CAs returns there are configured CAs. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1381 | bool ssl_has_client_CAs(const SSL_CONFIG *cfg); |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1382 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1383 | // ssl_add_client_CA_list adds the configured CA list to |cbb| in the format |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1384 | // used by a TLS CertificateRequest message. It returns true on success and |
| 1385 | // false on error. |
| 1386 | bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb); |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 1387 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1388 | // ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as |
| 1389 | // a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes |
| 1390 | // an error on the error queue. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1391 | bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey, |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 1392 | const CRYPTO_BUFFER *leaf); |
Steven Valdez | bf5aa84 | 2016-07-15 07:07:40 -0400 | [diff] [blame] | 1393 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1394 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1395 | // TLS 1.3 key derivation. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1396 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1397 | // tls13_init_key_schedule initializes the handshake hash and key derivation |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1398 | // state, and incorporates the PSK. The cipher suite and PRF hash must have been |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1399 | // selected at this point. It returns true on success and false on error. |
David Benjamin | 79b8b3a | 2019-08-16 18:58:13 -0400 | [diff] [blame] | 1400 | bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1401 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1402 | // tls13_init_early_key_schedule initializes the handshake hash and key |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1403 | // derivation state from |session| for use with 0-RTT. It returns one on success |
| 1404 | // and zero on error. |
| 1405 | bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, |
| 1406 | const SSL_SESSION *session); |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1407 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1408 | // tls13_advance_key_schedule incorporates |in| into the key schedule with |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1409 | // HKDF-Extract. It returns true on success and false on error. |
David Benjamin | 79b8b3a | 2019-08-16 18:58:13 -0400 | [diff] [blame] | 1410 | bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1411 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1412 | // tls13_set_traffic_key sets the read or write traffic keys to |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 1413 | // |traffic_secret|. The version and cipher suite are determined from |session|. |
| 1414 | // It returns true on success and false on error. |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 1415 | bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level, |
| 1416 | enum evp_aead_direction_t direction, |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 1417 | const SSL_SESSION *session, |
David Benjamin | 79b8b3a | 2019-08-16 18:58:13 -0400 | [diff] [blame] | 1418 | Span<const uint8_t> traffic_secret); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1419 | |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 1420 | // tls13_derive_early_secret derives the early traffic secret. It returns true |
David Benjamin | 1e85905 | 2020-02-09 16:04:58 -0500 | [diff] [blame] | 1421 | // on success and false on error. |
David Benjamin | d634357 | 2019-08-15 17:29:02 -0400 | [diff] [blame] | 1422 | bool tls13_derive_early_secret(SSL_HANDSHAKE *hs); |
| 1423 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1424 | // tls13_derive_handshake_secrets derives the handshake traffic secret. It |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1425 | // returns true on success and false on error. |
| 1426 | bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1427 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1428 | // tls13_rotate_traffic_key derives the next read or write traffic secret. It |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1429 | // returns true on success and false on error. |
| 1430 | bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction); |
Steven Valdez | 1dc53d2 | 2016-07-26 12:27:38 -0400 | [diff] [blame] | 1431 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1432 | // tls13_derive_application_secrets derives the initial application data traffic |
| 1433 | // and exporter secrets based on the handshake transcripts and |master_secret|. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1434 | // It returns true on success and false on error. |
| 1435 | bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1436 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1437 | // tls13_derive_resumption_secret derives the |resumption_secret|. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1438 | bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1439 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1440 | // tls13_export_keying_material provides an exporter interface to use the |
| 1441 | // |exporter_secret|. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1442 | bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out, |
| 1443 | Span<const uint8_t> secret, |
| 1444 | Span<const char> label, |
| 1445 | Span<const uint8_t> context); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1446 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1447 | // tls13_finished_mac calculates the MAC of the handshake transcript to verify |
| 1448 | // the integrity of the Finished message, and stores the result in |out| and |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1449 | // length in |out_len|. |is_server| is true if this is for the Server Finished |
| 1450 | // and false for the Client Finished. |
| 1451 | bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, |
| 1452 | bool is_server); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1453 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1454 | // tls13_derive_session_psk calculates the PSK for this session based on the |
| 1455 | // resumption master secret and |nonce|. It returns true on success, and false |
| 1456 | // on failure. |
Alessandro Ghedini | 2cc6f44 | 2018-12-11 11:35:17 +0000 | [diff] [blame] | 1457 | bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce); |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1458 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1459 | // tls13_write_psk_binder calculates the PSK binder value over |transcript| and |
| 1460 | // |msg|, and replaces the last bytes of |msg| with the resulting value. It |
| 1461 | // returns true on success, and false on failure. If |out_binder_len| is |
| 1462 | // non-NULL, it sets |*out_binder_len| to the length of the value computed. |
| 1463 | bool tls13_write_psk_binder(const SSL_HANDSHAKE *hs, |
| 1464 | const SSLTranscript &transcript, Span<uint8_t> msg, |
| 1465 | size_t *out_binder_len); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1466 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 1467 | // tls13_verify_psk_binder verifies that the handshake transcript, truncated up |
| 1468 | // to the binders has a valid signature using the value of |session|'s |
| 1469 | // resumption secret. It returns true on success, and false on failure. |
David Benjamin | b32aa05 | 2021-06-02 17:05:00 -0400 | [diff] [blame] | 1470 | bool tls13_verify_psk_binder(const SSL_HANDSHAKE *hs, |
| 1471 | const SSL_SESSION *session, const SSLMessage &msg, |
| 1472 | CBS *binders); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1473 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1474 | |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1475 | // Encrypted ClientHello. |
| 1476 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1477 | struct ECHConfig { |
| 1478 | static constexpr bool kAllowUniquePtr = true; |
| 1479 | // raw contains the serialized ECHConfig. |
| 1480 | Array<uint8_t> raw; |
| 1481 | // The following fields alias into |raw|. |
| 1482 | Span<const uint8_t> public_key; |
| 1483 | Span<const uint8_t> public_name; |
| 1484 | Span<const uint8_t> cipher_suites; |
| 1485 | uint16_t kem_id = 0; |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 1486 | uint8_t maximum_name_length = 0; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1487 | uint8_t config_id = 0; |
| 1488 | }; |
| 1489 | |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1490 | class ECHServerConfig { |
| 1491 | public: |
David Benjamin | 1d58cd1 | 2021-05-04 15:24:24 -0400 | [diff] [blame] | 1492 | static constexpr bool kAllowUniquePtr = true; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1493 | ECHServerConfig() = default; |
David Benjamin | 1d58cd1 | 2021-05-04 15:24:24 -0400 | [diff] [blame] | 1494 | ECHServerConfig(const ECHServerConfig &other) = delete; |
David Benjamin | 1d58cd1 | 2021-05-04 15:24:24 -0400 | [diff] [blame] | 1495 | ECHServerConfig &operator=(ECHServerConfig &&) = delete; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1496 | |
David Benjamin | c890ae5 | 2021-06-06 13:32:29 -0400 | [diff] [blame] | 1497 | // Init parses |ech_config| as an ECHConfig and saves a copy of |key|. |
| 1498 | // It returns true on success and false on error. |
| 1499 | bool Init(Span<const uint8_t> ech_config, const EVP_HPKE_KEY *key, |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1500 | bool is_retry_config); |
| 1501 | |
David Benjamin | f39c81d | 2021-05-03 18:39:46 -0400 | [diff] [blame] | 1502 | // SetupContext sets up |ctx| for a new connection, given the specified |
| 1503 | // HPKE ciphersuite and encapsulated KEM key. It returns true on success and |
| 1504 | // false on error. This function may only be called on an initialized object. |
| 1505 | bool SetupContext(EVP_HPKE_CTX *ctx, uint16_t kdf_id, uint16_t aead_id, |
| 1506 | Span<const uint8_t> enc) const; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1507 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1508 | const ECHConfig &ech_config() const { return ech_config_; } |
| 1509 | bool is_retry_config() const { return is_retry_config_; } |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1510 | |
| 1511 | private: |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1512 | ECHConfig ech_config_; |
David Benjamin | 1d58cd1 | 2021-05-04 15:24:24 -0400 | [diff] [blame] | 1513 | ScopedEVP_HPKE_KEY key_; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1514 | bool is_retry_config_ = false; |
| 1515 | }; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1516 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1517 | enum ssl_client_hello_type_t { |
| 1518 | ssl_client_hello_unencrypted, |
| 1519 | ssl_client_hello_inner, |
| 1520 | ssl_client_hello_outer, |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1521 | }; |
| 1522 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 1523 | // ECH_CLIENT_* are types for the ClientHello encrypted_client_hello extension. |
| 1524 | #define ECH_CLIENT_OUTER 0 |
| 1525 | #define ECH_CLIENT_INNER 1 |
| 1526 | |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1527 | // ssl_decode_client_hello_inner recovers the full ClientHelloInner from the |
| 1528 | // EncodedClientHelloInner |encoded_client_hello_inner| by replacing its |
| 1529 | // outer_extensions extension with the referenced extensions from the |
| 1530 | // ClientHelloOuter |client_hello_outer|. If successful, it writes the recovered |
| 1531 | // ClientHelloInner to |out_client_hello_inner|. It returns true on success and |
| 1532 | // false on failure. |
David Benjamin | 44425dd | 2022-01-27 12:22:42 -0500 | [diff] [blame] | 1533 | // |
| 1534 | // This function is exported for fuzzing. |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1535 | OPENSSL_EXPORT bool ssl_decode_client_hello_inner( |
| 1536 | SSL *ssl, uint8_t *out_alert, Array<uint8_t> *out_client_hello_inner, |
| 1537 | Span<const uint8_t> encoded_client_hello_inner, |
| 1538 | const SSL_CLIENT_HELLO *client_hello_outer); |
| 1539 | |
David Benjamin | 44425dd | 2022-01-27 12:22:42 -0500 | [diff] [blame] | 1540 | // ssl_client_hello_decrypt attempts to decrypt and decode the |payload|. It |
| 1541 | // writes the result to |*out|. |payload| must point into |client_hello_outer|. |
| 1542 | // It returns true on success and false on error. On error, it sets |
| 1543 | // |*out_is_decrypt_error| to whether the failure was due to a bad ciphertext. |
| 1544 | bool ssl_client_hello_decrypt(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1545 | bool *out_is_decrypt_error, Array<uint8_t> *out, |
Steven Valdez | 94a63a5 | 2021-04-29 10:52:42 -0400 | [diff] [blame] | 1546 | const SSL_CLIENT_HELLO *client_hello_outer, |
Steven Valdez | 94a63a5 | 2021-04-29 10:52:42 -0400 | [diff] [blame] | 1547 | Span<const uint8_t> payload); |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 1548 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1549 | #define ECH_CONFIRMATION_SIGNAL_LEN 8 |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 1550 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1551 | // ssl_ech_confirmation_signal_hello_offset returns the offset of the ECH |
| 1552 | // confirmation signal in a ServerHello message, including the handshake header. |
| 1553 | size_t ssl_ech_confirmation_signal_hello_offset(const SSL *ssl); |
| 1554 | |
| 1555 | // ssl_ech_accept_confirmation computes the server's ECH acceptance signal, |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 1556 | // writing it to |out|. The transcript portion is the concatenation of |
| 1557 | // |transcript| with |msg|. The |ECH_CONFIRMATION_SIGNAL_LEN| bytes from |
| 1558 | // |offset| in |msg| are replaced with zeros before hashing. This function |
| 1559 | // returns true on success, and false on failure. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1560 | bool ssl_ech_accept_confirmation(const SSL_HANDSHAKE *hs, Span<uint8_t> out, |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 1561 | Span<const uint8_t> client_random, |
| 1562 | const SSLTranscript &transcript, bool is_hrr, |
| 1563 | Span<const uint8_t> msg, size_t offset); |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1564 | |
David Benjamin | 9cbe737 | 2021-06-15 18:09:10 -0400 | [diff] [blame] | 1565 | // ssl_is_valid_ech_public_name returns true if |public_name| is a valid ECH |
| 1566 | // public name and false otherwise. It is exported for testing. |
| 1567 | OPENSSL_EXPORT bool ssl_is_valid_ech_public_name( |
| 1568 | Span<const uint8_t> public_name); |
| 1569 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1570 | // ssl_is_valid_ech_config_list returns true if |ech_config_list| is a valid |
| 1571 | // ECHConfigList structure and false otherwise. |
| 1572 | bool ssl_is_valid_ech_config_list(Span<const uint8_t> ech_config_list); |
| 1573 | |
| 1574 | // ssl_select_ech_config selects an ECHConfig and associated parameters to offer |
| 1575 | // on the client and updates |hs|. It returns true on success, whether an |
| 1576 | // ECHConfig was found or not, and false on internal error. On success, the |
| 1577 | // encapsulated key is written to |out_enc| and |*out_enc_len| is set to the |
| 1578 | // number of bytes written. If the function did not select an ECHConfig, the |
| 1579 | // encapsulated key is the empty string. |
| 1580 | bool ssl_select_ech_config(SSL_HANDSHAKE *hs, Span<uint8_t> out_enc, |
| 1581 | size_t *out_enc_len); |
| 1582 | |
| 1583 | // ssl_ech_extension_body_length returns the length of the body of a ClientHello |
| 1584 | // ECH extension that encrypts |in_len| bytes with |aead| and an 'enc' value of |
| 1585 | // length |enc_len|. The result does not include the four-byte extension header. |
| 1586 | size_t ssl_ech_extension_body_length(const EVP_HPKE_AEAD *aead, size_t enc_len, |
| 1587 | size_t in_len); |
| 1588 | |
| 1589 | // ssl_encrypt_client_hello constructs a new ClientHelloInner, adds it to the |
| 1590 | // inner transcript, and encrypts for inclusion in the ClientHelloOuter. |enc| |
| 1591 | // is the encapsulated key to include in the extension. It returns true on |
| 1592 | // success and false on error. If not offering ECH, |enc| is ignored and the |
| 1593 | // function will compute a GREASE ECH extension if necessary, and otherwise |
| 1594 | // return success while doing nothing. |
| 1595 | // |
| 1596 | // Encrypting the ClientHelloInner incorporates all extensions in the |
| 1597 | // ClientHelloOuter, so all other state necessary for |ssl_add_client_hello| |
| 1598 | // must already be computed. |
| 1599 | bool ssl_encrypt_client_hello(SSL_HANDSHAKE *hs, Span<const uint8_t> enc); |
David Benjamin | 246c556 | 2021-05-20 13:42:25 -0400 | [diff] [blame] | 1600 | |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 1601 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 1602 | // Credentials. |
David Benjamin | 08b1729 | 2021-03-29 15:35:56 -0400 | [diff] [blame] | 1603 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 1604 | enum class SSLCredentialType { |
| 1605 | kX509, |
| 1606 | kDelegated, |
David Benjamin | 08b1729 | 2021-03-29 15:35:56 -0400 | [diff] [blame] | 1607 | }; |
| 1608 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 1609 | BSSL_NAMESPACE_END |
| 1610 | |
| 1611 | // SSL_CREDENTIAL is exported to C, so it must be defined outside the namespace. |
| 1612 | struct ssl_credential_st : public bssl::RefCounted<ssl_credential_st> { |
| 1613 | explicit ssl_credential_st(bssl::SSLCredentialType type); |
| 1614 | ssl_credential_st(const ssl_credential_st &) = delete; |
| 1615 | ssl_credential_st &operator=(const ssl_credential_st &) = delete; |
| 1616 | |
| 1617 | // Dup returns a copy of the credential, or nullptr on error. The |ex_data| |
| 1618 | // values are not copied. This is only used on the default credential, whose |
| 1619 | // |ex_data| is inaccessible. |
| 1620 | bssl::UniquePtr<SSL_CREDENTIAL> Dup() const; |
| 1621 | |
| 1622 | // ClearCertAndKey erases any certificate and private key on the credential. |
| 1623 | void ClearCertAndKey(); |
| 1624 | |
| 1625 | // UsesX509 returns true if the credential type uses an X.509 certificate. |
| 1626 | bool UsesX509() const; |
| 1627 | |
| 1628 | // UsesPrivateKey returns true if the credential type uses an asymmetric |
| 1629 | // private key. |
| 1630 | bool UsesPrivateKey() const; |
| 1631 | |
| 1632 | // IsComplete returns whether all required fields in the credential have been |
| 1633 | // filled in. |
| 1634 | bool IsComplete() const; |
| 1635 | |
| 1636 | // SetLeafCert sets the leaf certificate to |leaf|, leaving the remaining |
| 1637 | // certificates unmodified. It returns true on success and false on error. If |
| 1638 | // |discard_key_on_mismatch| is true and the private key is inconsistent with |
| 1639 | // the new leaf certificate, it is silently discarded. |
| 1640 | bool SetLeafCert(bssl::UniquePtr<CRYPTO_BUFFER> leaf, |
| 1641 | bool discard_key_on_mismatch); |
| 1642 | |
David Benjamin | a792f88 | 2024-03-11 15:59:01 -0400 | [diff] [blame^] | 1643 | // ClearIntermediateCerts clears intermediate certificates in the certificate |
| 1644 | // chain, while preserving the leaf. |
| 1645 | void ClearIntermediateCerts(); |
| 1646 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 1647 | // AppendIntermediateCert appends |cert| to the certificate chain. If there is |
| 1648 | // no leaf certificate configured, it leaves a placeholder null in |chain|. It |
| 1649 | // returns one on success and zero on error. |
| 1650 | bool AppendIntermediateCert(bssl::UniquePtr<CRYPTO_BUFFER> cert); |
| 1651 | |
| 1652 | // type is the credential type and determines which other fields apply. |
| 1653 | bssl::SSLCredentialType type; |
| 1654 | |
| 1655 | // pubkey is the cached public key of the credential. Unlike |privkey|, it is |
| 1656 | // always present and is extracted from the certificate, delegated credential, |
| 1657 | // etc. |
| 1658 | bssl::UniquePtr<EVP_PKEY> pubkey; |
| 1659 | |
| 1660 | // privkey is the private key of the credential. It may be omitted in favor of |
| 1661 | // |key_method|. |
| 1662 | bssl::UniquePtr<EVP_PKEY> privkey; |
| 1663 | |
| 1664 | // key_method, if non-null, is a set of callbacks to call for private key |
| 1665 | // operations. |
| 1666 | const SSL_PRIVATE_KEY_METHOD *key_method = nullptr; |
| 1667 | |
| 1668 | // sigalgs, if non-empty, is the set of signature algorithms supported by the |
| 1669 | // private key in decreasing order of preference. If empty, the default list |
| 1670 | // is used. |
| 1671 | // |
| 1672 | // In delegated credentials, this field is not configurable and is instead |
| 1673 | // computed from the dc_cert_verify_algorithm field. |
| 1674 | bssl::Array<uint16_t> sigalgs; |
| 1675 | |
| 1676 | // chain contains the certificate chain, with the leaf at the beginning. The |
| 1677 | // first element of |chain| may be nullptr to indicate that the leaf |
| 1678 | // certificate has not yet been set. |
| 1679 | // If |chain| != nullptr -> len(chain) >= 1 |
| 1680 | // If |chain[0]| == nullptr -> len(chain) >= 2. |
| 1681 | // |chain[1..]| != nullptr |
| 1682 | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain; |
| 1683 | |
| 1684 | // dc is the DelegatedCredential structure, if this is a delegated credential. |
| 1685 | bssl::UniquePtr<CRYPTO_BUFFER> dc; |
| 1686 | |
| 1687 | // dc_algorithm is the signature scheme of the signature over the delegated |
| 1688 | // credential itself, made by the end-entity certificate's public key. |
| 1689 | uint16_t dc_algorithm = 0; |
| 1690 | |
| 1691 | // Signed certificate timestamp list to be sent to the client, if requested |
| 1692 | bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list; |
| 1693 | |
| 1694 | // OCSP response to be sent to the client, if requested. |
| 1695 | bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response; |
| 1696 | |
| 1697 | CRYPTO_EX_DATA ex_data; |
| 1698 | |
| 1699 | private: |
| 1700 | friend RefCounted; |
| 1701 | ~ssl_credential_st(); |
| 1702 | }; |
| 1703 | |
| 1704 | BSSL_NAMESPACE_BEGIN |
| 1705 | |
| 1706 | // ssl_get_credential_list computes |hs|'s credential list. On success, it |
| 1707 | // writes it to |*out| and returns true. Otherwise, it returns false. The |
| 1708 | // credential list may be empty, in which case this function will successfully |
| 1709 | // return an empty array. |
| 1710 | // |
| 1711 | // The pointers in the result are only valid until |hs| is next mutated. |
| 1712 | bool ssl_get_credential_list(SSL_HANDSHAKE *hs, Array<SSL_CREDENTIAL *> *out); |
David Benjamin | 08b1729 | 2021-03-29 15:35:56 -0400 | [diff] [blame] | 1713 | |
| 1714 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1715 | // Handshake functions. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1716 | |
| 1717 | enum ssl_hs_wait_t { |
| 1718 | ssl_hs_error, |
| 1719 | ssl_hs_ok, |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1720 | ssl_hs_read_server_hello, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1721 | ssl_hs_read_message, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1722 | ssl_hs_flush, |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1723 | ssl_hs_certificate_selection_pending, |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 1724 | ssl_hs_handoff, |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 1725 | ssl_hs_handback, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1726 | ssl_hs_x509_lookup, |
| 1727 | ssl_hs_private_key_operation, |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1728 | ssl_hs_pending_session, |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1729 | ssl_hs_pending_ticket, |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1730 | ssl_hs_early_return, |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1731 | ssl_hs_early_data_rejected, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1732 | ssl_hs_read_end_of_early_data, |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 1733 | ssl_hs_read_change_cipher_spec, |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1734 | ssl_hs_certificate_verify, |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 1735 | ssl_hs_hints_ready, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1736 | }; |
| 1737 | |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 1738 | enum ssl_grease_index_t { |
| 1739 | ssl_grease_cipher = 0, |
| 1740 | ssl_grease_group, |
| 1741 | ssl_grease_extension1, |
| 1742 | ssl_grease_extension2, |
| 1743 | ssl_grease_version, |
| 1744 | ssl_grease_ticket_extension, |
David Benjamin | 43ab56c | 2021-05-20 10:25:01 -0400 | [diff] [blame] | 1745 | ssl_grease_ech_config_id, |
| 1746 | ssl_grease_last_index = ssl_grease_ech_config_id, |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 1747 | }; |
| 1748 | |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 1749 | enum tls12_server_hs_state_t { |
| 1750 | state12_start_accept = 0, |
| 1751 | state12_read_client_hello, |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1752 | state12_read_client_hello_after_ech, |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 1753 | state12_cert_callback, |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 1754 | state12_tls13, |
| 1755 | state12_select_parameters, |
| 1756 | state12_send_server_hello, |
| 1757 | state12_send_server_certificate, |
| 1758 | state12_send_server_key_exchange, |
| 1759 | state12_send_server_hello_done, |
| 1760 | state12_read_client_certificate, |
| 1761 | state12_verify_client_certificate, |
| 1762 | state12_read_client_key_exchange, |
| 1763 | state12_read_client_certificate_verify, |
| 1764 | state12_read_change_cipher_spec, |
| 1765 | state12_process_change_cipher_spec, |
| 1766 | state12_read_next_proto, |
| 1767 | state12_read_channel_id, |
| 1768 | state12_read_client_finished, |
| 1769 | state12_send_server_finished, |
| 1770 | state12_finish_server_handshake, |
| 1771 | state12_done, |
| 1772 | }; |
| 1773 | |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1774 | enum tls13_server_hs_state_t { |
| 1775 | state13_select_parameters = 0, |
| 1776 | state13_select_session, |
| 1777 | state13_send_hello_retry_request, |
| 1778 | state13_read_second_client_hello, |
| 1779 | state13_send_server_hello, |
| 1780 | state13_send_server_certificate_verify, |
| 1781 | state13_send_server_finished, |
Matthew Braithwaite | 093a823 | 2020-01-28 14:06:55 -0800 | [diff] [blame] | 1782 | state13_send_half_rtt_ticket, |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1783 | state13_read_second_client_flight, |
| 1784 | state13_process_end_of_early_data, |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 1785 | state13_read_client_encrypted_extensions, |
Matthew Braithwaite | 08e1fe0 | 2019-11-26 17:49:04 -0800 | [diff] [blame] | 1786 | state13_read_client_certificate, |
| 1787 | state13_read_client_certificate_verify, |
| 1788 | state13_read_channel_id, |
| 1789 | state13_read_client_finished, |
| 1790 | state13_send_new_session_ticket, |
| 1791 | state13_done, |
| 1792 | }; |
| 1793 | |
Matthew Braithwaite | 3e2b3ee | 2018-05-10 15:46:42 -0700 | [diff] [blame] | 1794 | // handback_t lists the points in the state machine where a handback can occur. |
| 1795 | // These are the different points at which key material is no longer needed. |
| 1796 | enum handback_t { |
David Benjamin | 0c30649 | 2020-02-09 16:28:52 -0500 | [diff] [blame] | 1797 | handback_after_session_resumption = 0, |
| 1798 | handback_after_ecdhe = 1, |
| 1799 | handback_after_handshake = 2, |
| 1800 | handback_tls13 = 3, |
| 1801 | handback_max_value = handback_tls13, |
Matthew Braithwaite | 3e2b3ee | 2018-05-10 15:46:42 -0700 | [diff] [blame] | 1802 | }; |
| 1803 | |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 1804 | // SSL_HANDSHAKE_HINTS contains handshake hints for a connection. See |
| 1805 | // |SSL_request_handshake_hints| and related functions. |
| 1806 | struct SSL_HANDSHAKE_HINTS { |
| 1807 | static constexpr bool kAllowUniquePtr = true; |
| 1808 | |
David Benjamin | 4a6c8fd | 2022-07-21 14:05:41 -0700 | [diff] [blame] | 1809 | Array<uint8_t> server_random_tls12; |
| 1810 | Array<uint8_t> server_random_tls13; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 1811 | |
| 1812 | uint16_t key_share_group_id = 0; |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 1813 | Array<uint8_t> key_share_ciphertext; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 1814 | Array<uint8_t> key_share_secret; |
| 1815 | |
| 1816 | uint16_t signature_algorithm = 0; |
| 1817 | Array<uint8_t> signature_input; |
| 1818 | Array<uint8_t> signature_spki; |
| 1819 | Array<uint8_t> signature; |
| 1820 | |
| 1821 | Array<uint8_t> decrypted_psk; |
| 1822 | bool ignore_psk = false; |
David Benjamin | 26f186b | 2021-06-08 19:17:58 -0400 | [diff] [blame] | 1823 | |
| 1824 | uint16_t cert_compression_alg_id = 0; |
| 1825 | Array<uint8_t> cert_compression_input; |
| 1826 | Array<uint8_t> cert_compression_output; |
David Benjamin | 4a6c8fd | 2022-07-21 14:05:41 -0700 | [diff] [blame] | 1827 | |
| 1828 | uint16_t ecdhe_group_id = 0; |
| 1829 | Array<uint8_t> ecdhe_public_key; |
| 1830 | Array<uint8_t> ecdhe_private_key; |
David Benjamin | adaa322 | 2022-08-02 17:10:53 -0700 | [diff] [blame] | 1831 | |
| 1832 | Array<uint8_t> decrypted_ticket; |
| 1833 | bool renew_ticket = false; |
| 1834 | bool ignore_ticket = false; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 1835 | }; |
| 1836 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1837 | struct SSL_HANDSHAKE { |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 1838 | explicit SSL_HANDSHAKE(SSL *ssl); |
| 1839 | ~SSL_HANDSHAKE(); |
David Benjamin | 9a89250 | 2017-07-25 22:26:44 -0400 | [diff] [blame] | 1840 | static constexpr bool kAllowUniquePtr = true; |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 1841 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1842 | // ssl is a non-owning pointer to the parent |SSL| object. |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 1843 | SSL *ssl; |
| 1844 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1845 | // config is a non-owning pointer to the handshake configuration. |
| 1846 | SSL_CONFIG *config; |
| 1847 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1848 | // wait contains the operation the handshake is currently blocking on or |
| 1849 | // |ssl_hs_ok| if none. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 1850 | enum ssl_hs_wait_t wait = ssl_hs_ok; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 1851 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1852 | // state is the internal state for the TLS 1.2 and below handshake. Its |
| 1853 | // values depend on |do_handshake| but the starting state is always zero. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1854 | int state = 0; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1855 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1856 | // tls13_state is the internal state for the TLS 1.3 handshake. Its values |
| 1857 | // depend on |do_handshake| but the starting state is always zero. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 1858 | int tls13_state = 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1859 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1860 | // min_version is the minimum accepted protocol version, taking account both |
| 1861 | // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 1862 | uint16_t min_version = 0; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1863 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1864 | // max_version is the maximum accepted protocol version, taking account both |
| 1865 | // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 1866 | uint16_t max_version = 0; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1867 | |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 1868 | private: |
| 1869 | size_t hash_len_ = 0; |
| 1870 | uint8_t secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1871 | uint8_t early_traffic_secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1872 | uint8_t client_handshake_secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1873 | uint8_t server_handshake_secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1874 | uint8_t client_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0}; |
| 1875 | uint8_t server_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0}; |
| 1876 | uint8_t expected_client_finished_[SSL_MAX_MD_SIZE] = {0}; |
| 1877 | |
| 1878 | public: |
| 1879 | void ResizeSecrets(size_t hash_len); |
| 1880 | |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1881 | // GetClientHello, on the server, returns either the normal ClientHello |
| 1882 | // message or the ClientHelloInner if it has been serialized to |
| 1883 | // |ech_client_hello_buf|. This function should only be called when the |
| 1884 | // current message is a ClientHello. It returns true on success and false on |
| 1885 | // error. |
| 1886 | // |
| 1887 | // Note that fields of the returned |out_msg| and |out_client_hello| point |
| 1888 | // into a handshake-owned buffer, so their lifetimes should not exceed this |
| 1889 | // SSL_HANDSHAKE. |
| 1890 | bool GetClientHello(SSLMessage *out_msg, SSL_CLIENT_HELLO *out_client_hello); |
| 1891 | |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 1892 | Span<uint8_t> secret() { return MakeSpan(secret_, hash_len_); } |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1893 | Span<const uint8_t> secret() const { |
| 1894 | return MakeConstSpan(secret_, hash_len_); |
| 1895 | } |
David Benjamin | e530ea3 | 2019-08-16 19:28:00 -0400 | [diff] [blame] | 1896 | Span<uint8_t> early_traffic_secret() { |
| 1897 | return MakeSpan(early_traffic_secret_, hash_len_); |
| 1898 | } |
| 1899 | Span<uint8_t> client_handshake_secret() { |
| 1900 | return MakeSpan(client_handshake_secret_, hash_len_); |
| 1901 | } |
| 1902 | Span<uint8_t> server_handshake_secret() { |
| 1903 | return MakeSpan(server_handshake_secret_, hash_len_); |
| 1904 | } |
| 1905 | Span<uint8_t> client_traffic_secret_0() { |
| 1906 | return MakeSpan(client_traffic_secret_0_, hash_len_); |
| 1907 | } |
| 1908 | Span<uint8_t> server_traffic_secret_0() { |
| 1909 | return MakeSpan(server_traffic_secret_0_, hash_len_); |
| 1910 | } |
| 1911 | Span<uint8_t> expected_client_finished() { |
| 1912 | return MakeSpan(expected_client_finished_, hash_len_); |
| 1913 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1914 | |
David Benjamin | f5d2cd0 | 2016-10-06 19:39:20 -0400 | [diff] [blame] | 1915 | union { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1916 | // sent is a bitset where the bits correspond to elements of kExtensions |
David Benjamin | ec552ca | 2021-06-16 10:47:36 -0400 | [diff] [blame] | 1917 | // in extensions.cc. Each bit is set if that extension was sent in a |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1918 | // ClientHello. It's not used by servers. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 1919 | uint32_t sent = 0; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1920 | // received is a bitset, like |sent|, but is used by servers to record |
| 1921 | // which extensions were received from a client. |
David Benjamin | f5d2cd0 | 2016-10-06 19:39:20 -0400 | [diff] [blame] | 1922 | uint32_t received; |
| 1923 | } extensions; |
| 1924 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1925 | // inner_extensions_sent, on clients that offer ECH, is |extensions.sent| for |
| 1926 | // the ClientHelloInner. |
| 1927 | uint32_t inner_extensions_sent = 0; |
| 1928 | |
David Benjamin | e52f4c4 | 2017-10-01 22:35:10 -0400 | [diff] [blame] | 1929 | // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on. |
| 1930 | UniquePtr<ERR_SAVE_STATE> error; |
| 1931 | |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 1932 | // key_shares are the current key exchange instances. The second is only used |
| 1933 | // as a client if we believe that we should offer two key shares in a |
| 1934 | // ClientHello. |
| 1935 | UniquePtr<SSLKeyShare> key_shares[2]; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 1936 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1937 | // transcript is the current handshake transcript. |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1938 | SSLTranscript transcript; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1939 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1940 | // inner_transcript, on the client, is the handshake transcript for the |
| 1941 | // ClientHelloInner handshake. It is moved to |transcript| if the server |
| 1942 | // accepts ECH. |
| 1943 | SSLTranscript inner_transcript; |
| 1944 | |
| 1945 | // inner_client_random is the ClientHello random value used with |
| 1946 | // ClientHelloInner. |
| 1947 | uint8_t inner_client_random[SSL3_RANDOM_SIZE] = {0}; |
| 1948 | |
David Benjamin | 361e3e0 | 2022-09-11 13:58:38 -0400 | [diff] [blame] | 1949 | // cookie is the value of the cookie in HelloRetryRequest, or empty if none |
| 1950 | // was received. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 1951 | Array<uint8_t> cookie; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 1952 | |
David Benjamin | 361e3e0 | 2022-09-11 13:58:38 -0400 | [diff] [blame] | 1953 | // dtls_cookie is the value of the cookie in DTLS HelloVerifyRequest. If |
| 1954 | // empty, either none was received or HelloVerifyRequest contained an empty |
| 1955 | // cookie. |
| 1956 | Array<uint8_t> dtls_cookie; |
| 1957 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 1958 | // ech_client_outer contains the outer ECH extension to send in the |
| 1959 | // ClientHello, excluding the header and type byte. |
| 1960 | Array<uint8_t> ech_client_outer; |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 1961 | |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 1962 | // ech_retry_configs, on the client, contains the retry configs from the |
| 1963 | // server as a serialized ECHConfigList. |
| 1964 | Array<uint8_t> ech_retry_configs; |
| 1965 | |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 1966 | // ech_client_hello_buf, on the server, contains the bytes of the |
| 1967 | // reconstructed ClientHelloInner message. |
| 1968 | Array<uint8_t> ech_client_hello_buf; |
| 1969 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 1970 | // key_share_bytes is the key_share extension that the client should send. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 1971 | Array<uint8_t> key_share_bytes; |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 1972 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 1973 | // key_share_ciphertext, for servers, is encapsulated shared secret to be sent |
| 1974 | // to the client in the TLS 1.3 key_share extension. |
| 1975 | Array<uint8_t> key_share_ciphertext; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1976 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1977 | // peer_sigalgs are the signature algorithms that the peer supports. These are |
| 1978 | // taken from the contents of the signature algorithms extension for a server |
| 1979 | // or from the CertificateRequest for a client. |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 1980 | Array<uint16_t> peer_sigalgs; |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1981 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1982 | // peer_supported_group_list contains the supported group IDs advertised by |
| 1983 | // the peer. This is only set on the server's end. The server does not |
| 1984 | // advertise this extension to the client. |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 1985 | Array<uint16_t> peer_supported_group_list; |
David Benjamin | 43612b6 | 2016-10-07 00:41:50 -0400 | [diff] [blame] | 1986 | |
Watson Ladd | dcd6e44 | 2020-08-10 15:12:45 -0400 | [diff] [blame] | 1987 | // peer_delegated_credential_sigalgs are the signature algorithms the peer |
David Benjamin | c06c4d5 | 2024-02-13 23:02:14 -0500 | [diff] [blame] | 1988 | // supports with delegated credentials, or empty if the peer does not support |
| 1989 | // delegated credentials. |
Watson Ladd | dcd6e44 | 2020-08-10 15:12:45 -0400 | [diff] [blame] | 1990 | Array<uint16_t> peer_delegated_credential_sigalgs; |
| 1991 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1992 | // peer_key is the peer's ECDH key for a TLS 1.2 client. |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 1993 | Array<uint8_t> peer_key; |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1994 | |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 1995 | // extension_permutation is the permutation to apply to ClientHello |
| 1996 | // extensions. It maps indices into the |kExtensions| table into other |
| 1997 | // indices. |
| 1998 | Array<uint8_t> extension_permutation; |
| 1999 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2000 | // cert_compression_alg_id, for a server, contains the negotiated certificate |
| 2001 | // compression algorithm for this client. It is only valid if |
| 2002 | // |cert_compression_negotiated| is true. |
| 2003 | uint16_t cert_compression_alg_id; |
| 2004 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2005 | // ech_hpke_ctx is the HPKE context used in ECH. On the server, it is |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 2006 | // initialized if |ech_status| is |ssl_ech_accepted|. On the client, it is |
| 2007 | // initialized if |selected_ech_config| is not nullptr. |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 2008 | ScopedEVP_HPKE_CTX ech_hpke_ctx; |
| 2009 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2010 | // server_params, in a TLS 1.2 server, stores the ServerKeyExchange |
| 2011 | // parameters. It has client and server randoms prepended for signing |
| 2012 | // convenience. |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 2013 | Array<uint8_t> server_params; |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 2014 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2015 | // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the |
| 2016 | // server when using a TLS 1.2 PSK key exchange. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 2017 | UniquePtr<char> peer_psk_identity_hint; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2018 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2019 | // ca_names, on the client, contains the list of CAs received in a |
| 2020 | // CertificateRequest message. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 2021 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names; |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 2022 | |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2023 | // cached_x509_ca_names contains a cache of parsed versions of the elements of |
| 2024 | // |ca_names|. This pointer is left non-owning so only |
| 2025 | // |ssl_crypto_x509_method| needs to link against crypto/x509. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 2026 | STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2027 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2028 | // certificate_types, on the client, contains the set of certificate types |
| 2029 | // received in a CertificateRequest message. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2030 | Array<uint8_t> certificate_types; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2031 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 2032 | // credential is the credential we are using for the handshake. |
| 2033 | UniquePtr<SSL_CREDENTIAL> credential; |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 2034 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2035 | // peer_pubkey is the public key parsed from the peer's leaf certificate. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 2036 | UniquePtr<EVP_PKEY> peer_pubkey; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 2037 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2038 | // new_session is the new mutable session being established by the current |
| 2039 | // handshake. It should not be cached. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 2040 | UniquePtr<SSL_SESSION> new_session; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2041 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2042 | // early_session is the session corresponding to the current 0-RTT state on |
| 2043 | // the client if |in_early_data| is true. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 2044 | UniquePtr<SSL_SESSION> early_session; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2045 | |
David Benjamin | c3b373b | 2021-06-06 13:04:26 -0400 | [diff] [blame] | 2046 | // ssl_ech_keys, for servers, is the set of ECH keys to use with this |
| 2047 | // handshake. This is copied from |SSL_CTX| to ensure consistent behavior as |
| 2048 | // |SSL_CTX| rotates keys. |
| 2049 | UniquePtr<SSL_ECH_KEYS> ech_keys; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 2050 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2051 | // selected_ech_config, for clients, is the ECHConfig the client uses to offer |
| 2052 | // ECH, or nullptr if ECH is not being offered. If non-NULL, |ech_hpke_ctx| |
| 2053 | // will be initialized. |
| 2054 | UniquePtr<ECHConfig> selected_ech_config; |
| 2055 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2056 | // new_cipher is the cipher being negotiated in this handshake. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 2057 | const SSL_CIPHER *new_cipher = nullptr; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2058 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2059 | // key_block is the record-layer key block for TLS 1.2 and earlier. |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2060 | Array<uint8_t> key_block; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2061 | |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 2062 | // hints contains the handshake hints for this connection. If |
| 2063 | // |hints_requested| is true, this field is non-null and contains the pending |
| 2064 | // hints to filled as the predicted handshake progresses. Otherwise, this |
| 2065 | // field, if non-null, contains hints configured by the caller and will |
| 2066 | // influence the handshake on match. |
| 2067 | UniquePtr<SSL_HANDSHAKE_HINTS> hints; |
| 2068 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 2069 | // ech_is_inner, on the server, indicates whether the ClientHello contained an |
| 2070 | // inner ECH extension. |
| 2071 | bool ech_is_inner : 1; |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 2072 | |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 2073 | // ech_authenticated_reject, on the client, indicates whether an ECH rejection |
| 2074 | // handshake has been authenticated. |
| 2075 | bool ech_authenticated_reject : 1; |
| 2076 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2077 | // scts_requested is true if the SCT extension is in the ClientHello. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2078 | bool scts_requested : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2079 | |
Steven Valdez | d816874 | 2017-08-31 10:15:48 -0400 | [diff] [blame] | 2080 | // handshake_finalized is true once the handshake has completed, at which |
| 2081 | // point accessors should use the established state. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2082 | bool handshake_finalized : 1; |
Steven Valdez | d816874 | 2017-08-31 10:15:48 -0400 | [diff] [blame] | 2083 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2084 | // accept_psk_mode stores whether the client's PSK mode is compatible with our |
| 2085 | // preferences. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2086 | bool accept_psk_mode : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2087 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2088 | // cert_request is true if a client certificate was requested. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2089 | bool cert_request : 1; |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2090 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2091 | // certificate_status_expected is true if OCSP stapling was negotiated and the |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2092 | // server is expected to send a CertificateStatus message. (This is used on |
| 2093 | // both the client and server sides.) |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2094 | bool certificate_status_expected : 1; |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2095 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2096 | // ocsp_stapling_requested is true if a client requested OCSP stapling. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2097 | bool ocsp_stapling_requested : 1; |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2098 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2099 | // should_ack_sni is used by a server and indicates that the SNI extension |
| 2100 | // should be echoed in the ServerHello. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2101 | bool should_ack_sni : 1; |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2102 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2103 | // in_false_start is true if there is a pending client handshake in False |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2104 | // Start. The client may write data at this point. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2105 | bool in_false_start : 1; |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2106 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2107 | // in_early_data is true if there is a pending handshake that has progressed |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2108 | // enough to send and receive early data. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2109 | bool in_early_data : 1; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2110 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2111 | // early_data_offered is true if the client sent the early_data extension. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2112 | bool early_data_offered : 1; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2113 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2114 | // can_early_read is true if application data may be read at this point in the |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2115 | // handshake. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2116 | bool can_early_read : 1; |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 2117 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2118 | // can_early_write is true if application data may be written at this point in |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2119 | // the handshake. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2120 | bool can_early_write : 1; |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 2121 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2122 | // next_proto_neg_seen is one of NPN was negotiated. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2123 | bool next_proto_neg_seen : 1; |
David Benjamin | b74b081 | 2016-10-06 19:43:48 -0400 | [diff] [blame] | 2124 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2125 | // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2126 | // or received. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2127 | bool ticket_expected : 1; |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 2128 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2129 | // extended_master_secret is true if the extended master secret extension is |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2130 | // negotiated in this handshake. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2131 | bool extended_master_secret : 1; |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 2132 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2133 | // pending_private_key_op is true if there is a pending private key operation |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2134 | // in progress. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2135 | bool pending_private_key_op : 1; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 2136 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2137 | // handback indicates that a server should pause the handshake after |
| 2138 | // finishing operations that require private key material, in such a way that |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 2139 | // |SSL_get_error| returns |SSL_ERROR_HANDBACK|. It is set by |
| 2140 | // |SSL_apply_handoff|. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2141 | bool handback : 1; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2142 | |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 2143 | // hints_requested indicates the caller has requested handshake hints. Only |
| 2144 | // the first round-trip of the handshake will complete, after which the |
| 2145 | // |hints| structure can be serialized. |
| 2146 | bool hints_requested : 1; |
| 2147 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2148 | // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2149 | bool cert_compression_negotiated : 1; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2150 | |
David Benjamin | 6965d25 | 2018-11-19 15:49:56 -0600 | [diff] [blame] | 2151 | // apply_jdk11_workaround is true if the peer is probably a JDK 11 client |
| 2152 | // which implemented TLS 1.3 incorrectly. |
| 2153 | bool apply_jdk11_workaround : 1; |
| 2154 | |
David Benjamin | 9b2cdb7 | 2021-04-01 23:21:53 -0400 | [diff] [blame] | 2155 | // can_release_private_key is true if the private key will no longer be used |
| 2156 | // in this handshake. |
| 2157 | bool can_release_private_key : 1; |
| 2158 | |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 2159 | // channel_id_negotiated is true if Channel ID should be used in this |
| 2160 | // handshake. |
| 2161 | bool channel_id_negotiated : 1; |
| 2162 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2163 | // client_version is the value sent or received in the ClientHello version. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 2164 | uint16_t client_version = 0; |
Alessandro Ghedini | de254b4 | 2017-04-17 19:12:33 +0100 | [diff] [blame] | 2165 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2166 | // early_data_read is the amount of early data that has been read by the |
| 2167 | // record layer. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 2168 | uint16_t early_data_read = 0; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2169 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2170 | // early_data_written is the amount of early data that has been written by the |
| 2171 | // record layer. |
David Benjamin | 8f28886 | 2017-07-20 14:01:44 -0400 | [diff] [blame] | 2172 | uint16_t early_data_written = 0; |
Adam Langley | 2f9b47f | 2017-12-29 14:15:55 -0800 | [diff] [blame] | 2173 | |
David Benjamin | 05c285d | 2024-03-04 13:23:06 -0500 | [diff] [blame] | 2174 | // signature_algorithm is the signature algorithm to be used in signing with |
| 2175 | // the selected credential, or zero if not applicable or not yet selected. |
| 2176 | uint16_t signature_algorithm = 0; |
| 2177 | |
Steven Valdez | 94a63a5 | 2021-04-29 10:52:42 -0400 | [diff] [blame] | 2178 | // ech_config_id is the ECH config sent by the client. |
| 2179 | uint8_t ech_config_id = 0; |
| 2180 | |
Steven Valdez | b84674b | 2018-08-28 10:14:07 -0400 | [diff] [blame] | 2181 | // session_id is the session ID in the ClientHello. |
Adam Langley | 2f9b47f | 2017-12-29 14:15:55 -0800 | [diff] [blame] | 2182 | uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0}; |
| 2183 | uint8_t session_id_len = 0; |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2184 | |
David Benjamin | 33e8c78 | 2021-05-19 17:07:18 -0400 | [diff] [blame] | 2185 | // grease_seed is the entropy for GREASE values. |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2186 | uint8_t grease_seed[ssl_grease_last_index + 1] = {0}; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2187 | }; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2188 | |
David Benjamin | 3f180b8 | 2022-05-09 17:45:18 -0400 | [diff] [blame] | 2189 | // kMaxTickets is the maximum number of tickets to send immediately after the |
| 2190 | // handshake. We use a one-byte ticket nonce, and there is no point in sending |
| 2191 | // so many tickets. |
| 2192 | constexpr size_t kMaxTickets = 16; |
| 2193 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2194 | UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2195 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2196 | // ssl_check_message_type checks if |msg| has type |type|. If so it returns |
| 2197 | // one. Otherwise, it sends an alert and returns zero. |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 2198 | bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type); |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 2199 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2200 | // ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0 |
| 2201 | // on error. It sets |out_early_return| to one if we've completed the handshake |
| 2202 | // early. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2203 | int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2204 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2205 | // The following are implementations of |do_handshake| for the client and |
| 2206 | // server. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 2207 | enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs); |
| 2208 | enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 2209 | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs); |
| 2210 | enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2211 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2212 | // The following functions return human-readable representations of the TLS |
| 2213 | // handshake states for debugging. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 2214 | const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs); |
| 2215 | const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs); |
David Benjamin | f60bcfb | 2017-08-18 15:23:44 -0400 | [diff] [blame] | 2216 | const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs); |
| 2217 | const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs); |
| 2218 | |
Adam Langley | ba9ad66 | 2018-12-17 13:59:38 -0800 | [diff] [blame] | 2219 | // tls13_add_key_update queues a KeyUpdate message on |ssl|. The |
| 2220 | // |update_requested| argument must be one of |SSL_KEY_UPDATE_REQUESTED| or |
| 2221 | // |SSL_KEY_UPDATE_NOT_REQUESTED|. |
| 2222 | bool tls13_add_key_update(SSL *ssl, int update_requested); |
| 2223 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 2224 | // tls13_post_handshake processes a post-handshake message. It returns true on |
| 2225 | // success and false on failure. |
| 2226 | bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg); |
Steven Valdez | 8e1c7be | 2016-07-26 12:39:22 -0400 | [diff] [blame] | 2227 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 2228 | bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 2229 | bool allow_anonymous); |
| 2230 | bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
David Benjamin | 794cc59 | 2017-03-25 22:24:23 -0500 | [diff] [blame] | 2231 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2232 | // tls13_process_finished processes |msg| as a Finished message from the |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 2233 | // peer. If |use_saved_value| is true, the verify_data is compared against |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2234 | // |hs->expected_client_finished| rather than computed fresh. |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 2235 | bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 2236 | bool use_saved_value); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2237 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 2238 | bool tls13_add_certificate(SSL_HANDSHAKE *hs); |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 2239 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2240 | // tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the |
| 2241 | // handshake. If it returns |ssl_private_key_retry|, it should be called again |
| 2242 | // to retry when the signing operation is completed. |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 2243 | enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs); |
| 2244 | |
David Benjamin | 12f5878 | 2018-08-28 17:06:31 -0500 | [diff] [blame] | 2245 | bool tls13_add_finished(SSL_HANDSHAKE *hs); |
| 2246 | bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg); |
Adam Langley | 53a17f5 | 2020-05-26 14:44:07 -0700 | [diff] [blame] | 2247 | bssl::UniquePtr<SSL_SESSION> tls13_create_session_with_ticket(SSL *ssl, |
| 2248 | CBS *body); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2249 | |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 2250 | // ssl_setup_extension_permutation computes a ClientHello extension permutation |
| 2251 | // for |hs|, if applicable. It returns true on success and false on error. |
| 2252 | bool ssl_setup_extension_permutation(SSL_HANDSHAKE *hs); |
| 2253 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2254 | // ssl_setup_key_shares computes client key shares and saves them in |hs|. It |
| 2255 | // returns true on success and false on failure. If |override_group_id| is zero, |
| 2256 | // it offers the default groups, including GREASE. If it is non-zero, it offers |
| 2257 | // a single key share of the specified group. |
| 2258 | bool ssl_setup_key_shares(SSL_HANDSHAKE *hs, uint16_t override_group_id); |
| 2259 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2260 | bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2261 | Array<uint8_t> *out_secret, |
| 2262 | uint8_t *out_alert, CBS *contents); |
| 2263 | bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found, |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2264 | Span<const uint8_t> *out_peer_key, |
| 2265 | uint8_t *out_alert, |
| 2266 | const SSL_CLIENT_HELLO *client_hello); |
| 2267 | bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2268 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2269 | bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2270 | uint8_t *out_alert, |
| 2271 | CBS *contents); |
| 2272 | bool ssl_ext_pre_shared_key_parse_clienthello( |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 2273 | SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 2274 | uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, |
| 2275 | const SSL_CLIENT_HELLO *client_hello, CBS *contents); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2276 | bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2277 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2278 | // ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 2279 | // returns whether it's valid. |
| 2280 | bool ssl_is_sct_list_valid(const CBS *contents); |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 2281 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2282 | // ssl_write_client_hello_without_extensions writes a ClientHello to |out|, |
| 2283 | // up to the extensions field. |type| determines the type of ClientHello to |
| 2284 | // write. If |omit_session_id| is true, the session ID is empty. |
| 2285 | bool ssl_write_client_hello_without_extensions(const SSL_HANDSHAKE *hs, |
| 2286 | CBB *cbb, |
| 2287 | ssl_client_hello_type_t type, |
| 2288 | bool empty_session_id); |
| 2289 | |
| 2290 | // ssl_add_client_hello constructs a ClientHello and adds it to the outgoing |
| 2291 | // flight. It returns true on success and false on error. |
| 2292 | bool ssl_add_client_hello(SSL_HANDSHAKE *hs); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2293 | |
David Benjamin | e2cb423 | 2021-06-23 18:14:22 -0400 | [diff] [blame] | 2294 | struct ParsedServerHello { |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 2295 | CBS raw; |
David Benjamin | e2cb423 | 2021-06-23 18:14:22 -0400 | [diff] [blame] | 2296 | uint16_t legacy_version = 0; |
| 2297 | CBS random; |
| 2298 | CBS session_id; |
| 2299 | uint16_t cipher_suite = 0; |
| 2300 | uint8_t compression_method = 0; |
| 2301 | CBS extensions; |
| 2302 | }; |
| 2303 | |
| 2304 | // ssl_parse_server_hello parses |msg| as a ServerHello. On success, it writes |
| 2305 | // the result to |*out| and returns true. Otherwise, it returns false and sets |
| 2306 | // |*out_alert| to an alert to send to the peer. |
| 2307 | bool ssl_parse_server_hello(ParsedServerHello *out, uint8_t *out_alert, |
| 2308 | const SSLMessage &msg); |
| 2309 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 2310 | enum ssl_cert_verify_context_t { |
| 2311 | ssl_cert_verify_server, |
| 2312 | ssl_cert_verify_client, |
| 2313 | ssl_cert_verify_channel_id, |
| 2314 | }; |
| 2315 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2316 | // tls13_get_cert_verify_signature_input generates the message to be signed for |
| 2317 | // TLS 1.3's CertificateVerify message. |cert_verify_context| determines the |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 2318 | // type of signature. It sets |*out| to a newly allocated buffer containing the |
| 2319 | // result. This function returns true on success and false on failure. |
| 2320 | bool tls13_get_cert_verify_signature_input( |
| 2321 | SSL_HANDSHAKE *hs, Array<uint8_t> *out, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 2322 | enum ssl_cert_verify_context_t cert_verify_context); |
| 2323 | |
David Benjamin | 12a3e7e | 2021-04-13 11:47:36 -0400 | [diff] [blame] | 2324 | // ssl_is_valid_alpn_list returns whether |in| is a valid ALPN protocol list. |
| 2325 | bool ssl_is_valid_alpn_list(Span<const uint8_t> in); |
| 2326 | |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 2327 | // ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2328 | // selection for |hs->ssl|'s client preferences. |
| 2329 | bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs, |
| 2330 | Span<const uint8_t> protocol); |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 2331 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2332 | // ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2333 | // true on successful negotiation or if nothing was negotiated. It returns false |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2334 | // and sets |*out_alert| to an alert on error. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2335 | bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 2336 | const SSL_CLIENT_HELLO *client_hello); |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 2337 | |
David Benjamin | 4e93cd4 | 2021-05-18 13:38:25 -0400 | [diff] [blame] | 2338 | // ssl_get_local_application_settings looks up the configured ALPS value for |
| 2339 | // |protocol|. If found, it sets |*out_settings| to the value and returns true. |
| 2340 | // Otherwise, it returns false. |
| 2341 | bool ssl_get_local_application_settings(const SSL_HANDSHAKE *hs, |
| 2342 | Span<const uint8_t> *out_settings, |
| 2343 | Span<const uint8_t> protocol); |
| 2344 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2345 | // ssl_negotiate_alps negotiates the ALPS extension, if applicable. It returns |
| 2346 | // true on successful negotiation or if nothing was negotiated. It returns false |
| 2347 | // and sets |*out_alert| to an alert on error. |
| 2348 | bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 2349 | const SSL_CLIENT_HELLO *client_hello); |
| 2350 | |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 2351 | struct SSLExtension { |
| 2352 | SSLExtension(uint16_t type_arg, bool allowed_arg = true) |
| 2353 | : type(type_arg), allowed(allowed_arg), present(false) { |
| 2354 | CBS_init(&data, nullptr, 0); |
| 2355 | } |
| 2356 | |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 2357 | uint16_t type; |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 2358 | bool allowed; |
| 2359 | bool present; |
| 2360 | CBS data; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2361 | }; |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 2362 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2363 | // ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 2364 | // it. It writes the parsed extensions to pointers in |extensions|. On success, |
| 2365 | // it fills in the |present| and |data| fields and returns true. Otherwise, it |
| 2366 | // sets |*out_alert| to an alert to send and returns false. Unknown extensions |
| 2367 | // are rejected unless |ignore_unknown| is true. |
David Benjamin | c4ec14c | 2020-09-21 18:42:52 -0400 | [diff] [blame] | 2368 | bool ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert, |
David Benjamin | a75027b | 2021-07-20 15:07:22 -0400 | [diff] [blame] | 2369 | std::initializer_list<SSLExtension *> extensions, |
David Benjamin | c4ec14c | 2020-09-21 18:42:52 -0400 | [diff] [blame] | 2370 | bool ignore_unknown); |
David Benjamin | ffb1107 | 2016-11-13 10:32:10 +0900 | [diff] [blame] | 2371 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2372 | // ssl_verify_peer_cert verifies the peer certificate for |hs|. |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 2373 | enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs); |
Jesse Selover | 1c337e5 | 2018-08-10 13:28:47 -0400 | [diff] [blame] | 2374 | // ssl_reverify_peer_cert verifies the peer certificate for |hs| when resuming a |
| 2375 | // session. |
David Benjamin | ee0716f | 2019-11-19 14:16:28 +0800 | [diff] [blame] | 2376 | enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs, |
| 2377 | bool send_alert); |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 2378 | |
David Benjamin | 00f48c8 | 2017-10-06 18:43:53 -0400 | [diff] [blame] | 2379 | enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs); |
David Benjamin | 88a537f | 2024-02-23 11:25:22 -0500 | [diff] [blame] | 2380 | |
| 2381 | // ssl_send_finished adds a Finished message to the current flight of messages. |
| 2382 | // It returns true on success and false on error. |
David Benjamin | 00f48c8 | 2017-10-06 18:43:53 -0400 | [diff] [blame] | 2383 | bool ssl_send_finished(SSL_HANDSHAKE *hs); |
David Benjamin | 88a537f | 2024-02-23 11:25:22 -0500 | [diff] [blame] | 2384 | |
| 2385 | // ssl_send_tls12_certificate adds a TLS 1.2 Certificate message to the current |
| 2386 | // flight of messages. It returns true on success and false on error. |
| 2387 | bool ssl_send_tls12_certificate(SSL_HANDSHAKE *hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2388 | |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 2389 | // ssl_handshake_session returns the |SSL_SESSION| corresponding to the current |
| 2390 | // handshake. Note, in TLS 1.2 resumptions, this session is immutable. |
| 2391 | const SSL_SESSION *ssl_handshake_session(const SSL_HANDSHAKE *hs); |
| 2392 | |
David Benjamin | 6c9758f | 2021-05-18 13:50:50 -0400 | [diff] [blame] | 2393 | // ssl_done_writing_client_hello is called after the last ClientHello is written |
| 2394 | // by |hs|. It releases some memory that is no longer needed. |
| 2395 | void ssl_done_writing_client_hello(SSL_HANDSHAKE *hs); |
| 2396 | |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 2397 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2398 | // SSLKEYLOGFILE functions. |
David Benjamin | e776cc2 | 2016-07-19 07:26:49 +0200 | [diff] [blame] | 2399 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2400 | // ssl_log_secret logs |secret| with label |label|, if logging is enabled for |
David Benjamin | b244e3a | 2019-08-16 19:33:15 -0400 | [diff] [blame] | 2401 | // |ssl|. It returns true on success and false on failure. |
| 2402 | bool ssl_log_secret(const SSL *ssl, const char *label, |
| 2403 | Span<const uint8_t> secret); |
David Benjamin | e776cc2 | 2016-07-19 07:26:49 +0200 | [diff] [blame] | 2404 | |
| 2405 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2406 | // ClientHello functions. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2407 | |
David Benjamin | 1596137 | 2021-04-14 21:46:25 -0400 | [diff] [blame] | 2408 | // ssl_client_hello_init parses |body| as a ClientHello message, excluding the |
| 2409 | // message header, and writes the result to |*out|. It returns true on success |
| 2410 | // and false on error. This function is exported for testing. |
| 2411 | OPENSSL_EXPORT bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out, |
| 2412 | Span<const uint8_t> body); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2413 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 2414 | bool ssl_parse_client_hello_with_trailing_data(const SSL *ssl, CBS *cbs, |
| 2415 | SSL_CLIENT_HELLO *out); |
| 2416 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 2417 | bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello, |
| 2418 | CBS *out, uint16_t extension_type); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2419 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 2420 | bool ssl_client_cipher_list_contains_cipher( |
| 2421 | const SSL_CLIENT_HELLO *client_hello, uint16_t id); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2422 | |
| 2423 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2424 | // GREASE. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2425 | |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2426 | // ssl_get_grease_value returns a GREASE value for |hs|. For a given |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2427 | // connection, the values for each index will be deterministic. This allows the |
| 2428 | // same ClientHello be sent twice for a HelloRetryRequest or the same group be |
| 2429 | // advertised in both supported_groups and key_shares. |
David Benjamin | 33e8c78 | 2021-05-19 17:07:18 -0400 | [diff] [blame] | 2430 | uint16_t ssl_get_grease_value(const SSL_HANDSHAKE *hs, |
| 2431 | enum ssl_grease_index_t index); |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2432 | |
| 2433 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2434 | // Signature algorithms. |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 2435 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2436 | // tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 2437 | // algorithms and saves them on |hs|. It returns true on success and false on |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2438 | // error. |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 2439 | bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs); |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 2440 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2441 | // tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 2442 | // that should be used with |pkey| in TLS 1.1 and earlier. It returns true on |
| 2443 | // success and false if |pkey| may not be used at those versions. |
| 2444 | bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey); |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 2445 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2446 | // tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 2447 | // with |cred| based on the peer's preferences and the algorithms supported. It |
| 2448 | // returns true on success and false on error. |
| 2449 | bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, |
| 2450 | const SSL_CREDENTIAL *cred, uint16_t *out); |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 2451 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2452 | // tls12_add_verify_sigalgs adds the signature algorithms acceptable for the |
David Benjamin | f249840 | 2020-01-15 20:15:36 -0500 | [diff] [blame] | 2453 | // peer signature to |out|. It returns true on success and false on error. |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 2454 | bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out); |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 2455 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2456 | // tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 2457 | // signature. It returns true on success and false on error, setting |
| 2458 | // |*out_alert| to an alert to send. |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 2459 | bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 2460 | uint16_t sigalg); |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 2461 | |
| 2462 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2463 | // Underdocumented functions. |
| 2464 | // |
| 2465 | // Functions below here haven't been touched up and may be underdocumented. |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 2466 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2467 | #define TLSEXT_CHANNEL_ID_SIZE 128 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2468 | |
David Benjamin | 8648c53 | 2021-08-19 18:02:37 -0400 | [diff] [blame] | 2469 | // From RFC 4492, used in encoding the curve type in ECParameters |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2470 | #define NAMED_CURVE_TYPE 3 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2471 | |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2472 | struct CERT { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 2473 | static constexpr bool kAllowUniquePtr = true; |
| 2474 | |
| 2475 | explicit CERT(const SSL_X509_METHOD *x509_method); |
| 2476 | ~CERT(); |
| 2477 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 2478 | bool is_valid() const { return default_credential != nullptr; } |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 2479 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 2480 | // credentials is the list of credentials to select between. Elements of this |
| 2481 | // array immutable. |
| 2482 | GrowableArray<UniquePtr<SSL_CREDENTIAL>> credentials; |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 2483 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 2484 | // default_credential is the credential configured by the legacy, |
| 2485 | // non-credential-based APIs. If IsComplete() returns true, it is appended to |
| 2486 | // the list of credentials. |
| 2487 | UniquePtr<SSL_CREDENTIAL> default_credential; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 2488 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2489 | // x509_method contains pointers to functions that might deal with |X509| |
| 2490 | // compatibility, or might be a no-op, depending on the application. |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 2491 | const SSL_X509_METHOD *x509_method = nullptr; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 2492 | |
David Benjamin | 91a3f26 | 2024-02-10 11:08:08 -0500 | [diff] [blame] | 2493 | // x509_chain may contain a parsed copy of |chain[1..]| from the default |
| 2494 | // credential. This is only used as a cache in order to implement “get0” |
| 2495 | // functions that return a non-owning pointer to the certificate chain. |
| 2496 | STACK_OF(X509) *x509_chain = nullptr; |
| 2497 | |
| 2498 | // x509_leaf may contain a parsed copy of the first element of |chain| from |
| 2499 | // the default credential. This is only used as a cache in order to implement |
| 2500 | // “get0” functions that return a non-owning pointer to the certificate chain. |
| 2501 | X509 *x509_leaf = nullptr; |
| 2502 | |
| 2503 | // x509_stash contains the last |X509| object append to the default |
| 2504 | // credential's chain. This is a workaround for some third-party code that |
| 2505 | // continue to use an |X509| object even after passing ownership with an |
| 2506 | // “add0” function. |
| 2507 | X509 *x509_stash = nullptr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2508 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2509 | // Certificate setup callback: if set is called whenever a |
| 2510 | // certificate may be required (client or server). the callback |
| 2511 | // can then examine any appropriate parameters and setup any |
| 2512 | // certificates required. This allows advanced applications |
| 2513 | // to select certificates on the fly: for example based on |
| 2514 | // supported signature algorithms or curves. |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 2515 | int (*cert_cb)(SSL *ssl, void *arg) = nullptr; |
| 2516 | void *cert_cb_arg = nullptr; |
Adam Langley | d323f4b | 2016-03-01 15:58:14 -0800 | [diff] [blame] | 2517 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2518 | // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX |
| 2519 | // store is used instead. |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 2520 | X509_STORE *verify_store = nullptr; |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 2521 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2522 | // sid_ctx partitions the session space within a shared session cache or |
| 2523 | // ticket key. Only sessions with a matching value will be accepted. |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 2524 | uint8_t sid_ctx_length = 0; |
| 2525 | uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0}; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2526 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2527 | |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2528 | // |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS. |
| 2529 | struct SSL_PROTOCOL_METHOD { |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 2530 | bool is_dtls; |
| 2531 | bool (*ssl_new)(SSL *ssl); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2532 | void (*ssl_free)(SSL *ssl); |
| 2533 | // get_message sets |*out| to the current handshake message and returns true |
| 2534 | // if one has been received. It returns false if more input is needed. |
Adam Langley | c9827e0 | 2019-04-12 14:46:50 -0700 | [diff] [blame] | 2535 | bool (*get_message)(const SSL *ssl, SSLMessage *out); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2536 | // next_message is called to release the current handshake message. |
| 2537 | void (*next_message)(SSL *ssl); |
David Benjamin | f9cc26f | 2020-02-09 16:49:31 -0500 | [diff] [blame] | 2538 | // has_unprocessed_handshake_data returns whether there is buffered |
| 2539 | // handshake data that has not been consumed by |get_message|. |
| 2540 | bool (*has_unprocessed_handshake_data)(const SSL *ssl); |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 2541 | // Use the |ssl_open_handshake| wrapper. |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 2542 | ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed, |
| 2543 | uint8_t *out_alert, Span<uint8_t> in); |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 2544 | // Use the |ssl_open_change_cipher_spec| wrapper. |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 2545 | ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed, |
| 2546 | uint8_t *out_alert, |
| 2547 | Span<uint8_t> in); |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 2548 | // Use the |ssl_open_app_data| wrapper. |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 2549 | ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out, |
| 2550 | size_t *out_consumed, uint8_t *out_alert, |
| 2551 | Span<uint8_t> in); |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 2552 | // write_app_data encrypts and writes |in| as application data. On success, it |
| 2553 | // returns one and sets |*out_bytes_written| to the number of bytes of |in| |
| 2554 | // written. Otherwise, it returns <= 0 and sets |*out_needs_handshake| to |
| 2555 | // whether the operation failed because the caller needs to drive the |
| 2556 | // handshake. |
| 2557 | int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, |
| 2558 | size_t *out_bytes_written, Span<const uint8_t> in); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2559 | int (*dispatch_alert)(SSL *ssl); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2560 | // init_message begins a new handshake message of type |type|. |cbb| is the |
| 2561 | // root CBB to be passed into |finish_message|. |*body| is set to a child CBB |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 2562 | // the caller should write to. It returns true on success and false on error. |
David Benjamin | bcef514 | 2021-06-02 11:24:26 -0400 | [diff] [blame] | 2563 | bool (*init_message)(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2564 | // finish_message finishes a handshake message. It sets |*out_msg| to the |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 2565 | // serialized message. It returns true on success and false on error. |
David Benjamin | bcef514 | 2021-06-02 11:24:26 -0400 | [diff] [blame] | 2566 | bool (*finish_message)(const SSL *ssl, CBB *cbb, |
| 2567 | bssl::Array<uint8_t> *out_msg); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 2568 | // add_message adds a handshake message to the pending flight. It returns |
| 2569 | // true on success and false on error. |
| 2570 | bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2571 | // add_change_cipher_spec adds a ChangeCipherSpec record to the pending |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 2572 | // flight. It returns true on success and false on error. |
| 2573 | bool (*add_change_cipher_spec)(SSL *ssl); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2574 | // flush_flight flushes the pending flight to the transport. It returns one on |
| 2575 | // success and <= 0 on error. |
| 2576 | int (*flush_flight)(SSL *ssl); |
| 2577 | // on_handshake_complete is called when the handshake is complete. |
| 2578 | void (*on_handshake_complete)(SSL *ssl); |
David Benjamin | b092192 | 2020-02-20 12:33:28 -0500 | [diff] [blame] | 2579 | // set_read_state sets |ssl|'s read cipher state and level to |aead_ctx| and |
David Benjamin | 5298ef9 | 2020-03-13 12:17:30 -0400 | [diff] [blame] | 2580 | // |level|. In QUIC, |aead_ctx| is a placeholder object and |secret_for_quic| |
| 2581 | // is the original secret. This function returns true on success and false on |
| 2582 | // error. |
David Benjamin | b092192 | 2020-02-20 12:33:28 -0500 | [diff] [blame] | 2583 | bool (*set_read_state)(SSL *ssl, ssl_encryption_level_t level, |
David Benjamin | 5298ef9 | 2020-03-13 12:17:30 -0400 | [diff] [blame] | 2584 | UniquePtr<SSLAEADContext> aead_ctx, |
| 2585 | Span<const uint8_t> secret_for_quic); |
David Benjamin | b092192 | 2020-02-20 12:33:28 -0500 | [diff] [blame] | 2586 | // set_write_state sets |ssl|'s write cipher state and level to |aead_ctx| and |
David Benjamin | 5298ef9 | 2020-03-13 12:17:30 -0400 | [diff] [blame] | 2587 | // |level|. In QUIC, |aead_ctx| is a placeholder object and |secret_for_quic| |
| 2588 | // is the original secret. This function returns true on success and false on |
| 2589 | // error. |
David Benjamin | b092192 | 2020-02-20 12:33:28 -0500 | [diff] [blame] | 2590 | bool (*set_write_state)(SSL *ssl, ssl_encryption_level_t level, |
David Benjamin | 5298ef9 | 2020-03-13 12:17:30 -0400 | [diff] [blame] | 2591 | UniquePtr<SSLAEADContext> aead_ctx, |
| 2592 | Span<const uint8_t> secret_for_quic); |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2593 | }; |
| 2594 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 2595 | // The following wrappers call |open_*| but handle |read_shutdown| correctly. |
| 2596 | |
| 2597 | // ssl_open_handshake processes a record from |in| for reading a handshake |
| 2598 | // message. |
| 2599 | ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed, |
| 2600 | uint8_t *out_alert, Span<uint8_t> in); |
| 2601 | |
| 2602 | // ssl_open_change_cipher_spec processes a record from |in| for reading a |
| 2603 | // ChangeCipherSpec. |
| 2604 | ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 2605 | uint8_t *out_alert, |
| 2606 | Span<uint8_t> in); |
| 2607 | |
| 2608 | // ssl_open_app_data processes a record from |in| for reading application data. |
| 2609 | // On success, it returns |ssl_open_record_success| and sets |*out| to the |
| 2610 | // input. If it encounters a post-handshake message, it returns |
| 2611 | // |ssl_open_record_discard|. The caller should then retry, after processing any |
| 2612 | // messages received with |get_message|. |
| 2613 | ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 2614 | size_t *out_consumed, uint8_t *out_alert, |
| 2615 | Span<uint8_t> in); |
| 2616 | |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2617 | struct SSL_X509_METHOD { |
| 2618 | // check_client_CA_list returns one if |names| is a good list of X.509 |
| 2619 | // distinguished names and zero otherwise. This is used to ensure that we can |
| 2620 | // reject unparsable values at handshake time when using crypto/x509. |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2621 | bool (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names); |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2622 | |
| 2623 | // cert_clear frees and NULLs all X509 certificate-related state. |
| 2624 | void (*cert_clear)(CERT *cert); |
| 2625 | // cert_free frees all X509-related state. |
| 2626 | void (*cert_free)(CERT *cert); |
| 2627 | // cert_flush_cached_chain drops any cached |X509|-based certificate chain |
| 2628 | // from |cert|. |
| 2629 | // cert_dup duplicates any needed fields from |cert| to |new_cert|. |
| 2630 | void (*cert_dup)(CERT *new_cert, const CERT *cert); |
| 2631 | void (*cert_flush_cached_chain)(CERT *cert); |
| 2632 | // cert_flush_cached_chain drops any cached |X509|-based leaf certificate |
| 2633 | // from |cert|. |
| 2634 | void (*cert_flush_cached_leaf)(CERT *cert); |
| 2635 | |
| 2636 | // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain| |
| 2637 | // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2638 | // true on success or false on error. |
| 2639 | bool (*session_cache_objects)(SSL_SESSION *session); |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2640 | // session_dup duplicates any needed fields from |session| to |new_session|. |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2641 | // It returns true on success or false on error. |
| 2642 | bool (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session); |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2643 | // session_clear frees any X509-related state from |session|. |
| 2644 | void (*session_clear)(SSL_SESSION *session); |
| 2645 | // session_verify_cert_chain verifies the certificate chain in |session|, |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2646 | // sets |session->verify_result| and returns true on success or false on |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2647 | // error. |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2648 | bool (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl, |
| 2649 | uint8_t *out_alert); |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2650 | |
| 2651 | // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|. |
| 2652 | void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs); |
Adam Langley | ffe384c | 2019-05-01 11:13:12 -0700 | [diff] [blame] | 2653 | // ssl_new does any necessary initialisation of |hs|. It returns true on |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2654 | // success or false on error. |
| 2655 | bool (*ssl_new)(SSL_HANDSHAKE *hs); |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2656 | // ssl_free frees anything created by |ssl_new|. |
| 2657 | void (*ssl_config_free)(SSL_CONFIG *cfg); |
| 2658 | // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|. |
| 2659 | void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg); |
| 2660 | // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if |
| 2661 | // necessary. On success, it updates |ssl|'s certificate configuration as |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2662 | // needed and returns true. Otherwise, it returns false. |
| 2663 | bool (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs); |
Adam Langley | ffe384c | 2019-05-01 11:13:12 -0700 | [diff] [blame] | 2664 | // ssl_ctx_new does any necessary initialisation of |ctx|. It returns true on |
David Benjamin | 1e77ef4 | 2019-03-30 01:03:50 -0500 | [diff] [blame] | 2665 | // success or false on error. |
| 2666 | bool (*ssl_ctx_new)(SSL_CTX *ctx); |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 2667 | // ssl_ctx_free frees anything created by |ssl_ctx_new|. |
| 2668 | void (*ssl_ctx_free)(SSL_CTX *ctx); |
| 2669 | // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|. |
| 2670 | void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl); |
| 2671 | }; |
| 2672 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2673 | // ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using |
| 2674 | // crypto/x509. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2675 | extern const SSL_X509_METHOD ssl_crypto_x509_method; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 2676 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2677 | // ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid |
| 2678 | // crypto/x509. |
David Benjamin | ba2d3df | 2017-08-04 13:59:24 -0400 | [diff] [blame] | 2679 | extern const SSL_X509_METHOD ssl_noop_x509_method; |
| 2680 | |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 2681 | struct TicketKey { |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2682 | static constexpr bool kAllowUniquePtr = true; |
| 2683 | |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 2684 | uint8_t name[SSL_TICKET_KEY_NAME_LEN] = {0}; |
| 2685 | uint8_t hmac_key[16] = {0}; |
| 2686 | uint8_t aes_key[16] = {0}; |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2687 | // next_rotation_tv_sec is the time (in seconds from the epoch) when the |
| 2688 | // current key should be superseded by a new key, or the time when a previous |
| 2689 | // key should be dropped. If zero, then the key should not be automatically |
| 2690 | // rotated. |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 2691 | uint64_t next_rotation_tv_sec = 0; |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 2692 | }; |
| 2693 | |
David Benjamin | 35b4a12 | 2018-07-14 17:04:41 -0400 | [diff] [blame] | 2694 | struct CertCompressionAlg { |
| 2695 | static constexpr bool kAllowUniquePtr = true; |
| 2696 | |
| 2697 | ssl_cert_compression_func_t compress = nullptr; |
| 2698 | ssl_cert_decompression_func_t decompress = nullptr; |
| 2699 | uint16_t alg_id = 0; |
| 2700 | }; |
| 2701 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 2702 | BSSL_NAMESPACE_END |
David Benjamin | 4e84035 | 2017-10-11 18:41:11 -0400 | [diff] [blame] | 2703 | |
David Benjamin | 1eff948 | 2018-09-24 14:10:29 -0500 | [diff] [blame] | 2704 | DEFINE_LHASH_OF(SSL_SESSION) |
David Benjamin | 4e84035 | 2017-10-11 18:41:11 -0400 | [diff] [blame] | 2705 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 2706 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 4e84035 | 2017-10-11 18:41:11 -0400 | [diff] [blame] | 2707 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2708 | // An ssl_shutdown_t describes the shutdown state of one end of the connection, |
| 2709 | // whether it is alive or has been shutdown via close_notify or fatal alert. |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2710 | enum ssl_shutdown_t { |
| 2711 | ssl_shutdown_none = 0, |
| 2712 | ssl_shutdown_close_notify = 1, |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 2713 | ssl_shutdown_error = 2, |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2714 | }; |
| 2715 | |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 2716 | enum ssl_ech_status_t { |
| 2717 | // ssl_ech_none indicates ECH was not offered, or we have not gotten far |
| 2718 | // enough in the handshake to determine the status. |
| 2719 | ssl_ech_none, |
| 2720 | // ssl_ech_accepted indicates the server accepted ECH. |
| 2721 | ssl_ech_accepted, |
| 2722 | // ssl_ech_rejected indicates the server was offered ECH but rejected it. |
| 2723 | ssl_ech_rejected, |
| 2724 | }; |
| 2725 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2726 | struct SSL3_STATE { |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2727 | static constexpr bool kAllowUniquePtr = true; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2728 | |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2729 | SSL3_STATE(); |
| 2730 | ~SSL3_STATE(); |
| 2731 | |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 2732 | uint64_t read_sequence = 0; |
| 2733 | uint64_t write_sequence = 0; |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2734 | |
| 2735 | uint8_t server_random[SSL3_RANDOM_SIZE] = {0}; |
| 2736 | uint8_t client_random[SSL3_RANDOM_SIZE] = {0}; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2737 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2738 | // read_buffer holds data from the transport to be processed. |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 2739 | SSLBuffer read_buffer; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2740 | // write_buffer holds data to be written to the transport. |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 2741 | SSLBuffer write_buffer; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2742 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 2743 | // pending_app_data is the unconsumed application data. It points into |
| 2744 | // |read_buffer|. |
| 2745 | Span<uint8_t> pending_app_data; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2746 | |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 2747 | // unreported_bytes_written is the number of bytes successfully written to the |
| 2748 | // transport, but not yet reported to the caller. The next |SSL_write| will |
| 2749 | // skip this many bytes from the input. This is used if |
| 2750 | // |SSL_MODE_ENABLE_PARTIAL_WRITE| is disabled, in which case |SSL_write| only |
| 2751 | // reports bytes written when the full caller input is written. |
| 2752 | size_t unreported_bytes_written = 0; |
| 2753 | |
| 2754 | // pending_write, if |has_pending_write| is true, is the caller-supplied data |
| 2755 | // corresponding to the current pending write. This is used to check the |
| 2756 | // caller retried with a compatible buffer. |
| 2757 | Span<const uint8_t> pending_write; |
| 2758 | |
| 2759 | // pending_write_type, if |has_pending_write| is true, is the record type |
| 2760 | // for the current pending write. |
| 2761 | // |
| 2762 | // TODO(davidben): Remove this when alerts are moved out of this write path. |
| 2763 | uint8_t pending_write_type = 0; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2764 | |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2765 | // read_shutdown is the shutdown state for the read half of the connection. |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2766 | enum ssl_shutdown_t read_shutdown = ssl_shutdown_none; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2767 | |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2768 | // write_shutdown is the shutdown state for the write half of the connection. |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2769 | enum ssl_shutdown_t write_shutdown = ssl_shutdown_none; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2770 | |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 2771 | // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for |
| 2772 | // the receive half of the connection. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2773 | UniquePtr<ERR_SAVE_STATE> read_error; |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 2774 | |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2775 | int total_renegotiations = 0; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2776 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 2777 | // This holds a variable that indicates what we were doing when a 0 or -1 is |
| 2778 | // returned. This is needed for non-blocking IO so we know what request |
| 2779 | // needs re-doing when in SSL_accept or SSL_connect |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 2780 | int rwstate = SSL_ERROR_NONE; |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 2781 | |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 2782 | enum ssl_encryption_level_t read_level = ssl_encryption_initial; |
| 2783 | enum ssl_encryption_level_t write_level = ssl_encryption_initial; |
| 2784 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2785 | // early_data_skipped is the amount of early data that has been skipped by the |
| 2786 | // record layer. |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2787 | uint16_t early_data_skipped = 0; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2788 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2789 | // empty_record_count is the number of consecutive empty records received. |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2790 | uint8_t empty_record_count = 0; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2791 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2792 | // warning_alert_count is the number of consecutive warning alerts |
| 2793 | // received. |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2794 | uint8_t warning_alert_count = 0; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2795 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2796 | // key_update_count is the number of consecutive KeyUpdates received. |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2797 | uint8_t key_update_count = 0; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2798 | |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 2799 | // ech_status indicates whether ECH was accepted by the server. |
| 2800 | ssl_ech_status_t ech_status = ssl_ech_none; |
David Benjamin | 3a036c7 | 2021-06-02 12:17:16 -0400 | [diff] [blame] | 2801 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2802 | // skip_early_data instructs the record layer to skip unexpected early data |
| 2803 | // messages when 0RTT is rejected. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2804 | bool skip_early_data : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2805 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2806 | // have_version is true if the connection's final version is known. Otherwise |
| 2807 | // the version has not been negotiated yet. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2808 | bool have_version : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2809 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2810 | // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled |
| 2811 | // and future messages should use the record layer. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2812 | bool v2_hello_done : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2813 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2814 | // is_v2_hello is true if the current handshake message was derived from a |
| 2815 | // V2ClientHello rather than received from the peer directly. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2816 | bool is_v2_hello : 1; |
David Benjamin | 1a444da | 2017-01-21 14:27:45 -0500 | [diff] [blame] | 2817 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2818 | // has_message is true if the current handshake message has been returned |
| 2819 | // at least once by |get_message| and false otherwise. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2820 | bool has_message : 1; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 2821 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2822 | // initial_handshake_complete is true if the initial handshake has |
| 2823 | // completed. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2824 | bool initial_handshake_complete : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2825 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2826 | // session_reused indicates whether a session was resumed. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2827 | bool session_reused : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2828 | |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2829 | bool send_connection_binding : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2830 | |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 2831 | // channel_id_valid is true if, on the server, the client has negotiated a |
| 2832 | // Channel ID and the |channel_id| field is filled in. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2833 | bool channel_id_valid : 1; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2834 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 2835 | // key_update_pending is true if we have a KeyUpdate acknowledgment |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2836 | // outstanding. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2837 | bool key_update_pending : 1; |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 2838 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 2839 | // early_data_accepted is true if early data was accepted by the server. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 2840 | bool early_data_accepted : 1; |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 2841 | |
David Benjamin | ee4888c | 2019-07-21 12:16:52 -0400 | [diff] [blame] | 2842 | // alert_dispatch is true there is an alert in |send_alert| to be sent. |
| 2843 | bool alert_dispatch : 1; |
| 2844 | |
David Benjamin | 0e7dbd5 | 2019-05-15 16:01:18 -0400 | [diff] [blame] | 2845 | // renegotiate_pending is whether the read half of the channel is blocked on a |
| 2846 | // HelloRequest. |
| 2847 | bool renegotiate_pending : 1; |
| 2848 | |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 2849 | // used_hello_retry_request is whether the handshake used a TLS 1.3 |
| 2850 | // HelloRetryRequest message. |
| 2851 | bool used_hello_retry_request : 1; |
| 2852 | |
David Benjamin | a614d46 | 2022-12-02 15:30:15 -0500 | [diff] [blame] | 2853 | // was_key_usage_invalid is whether the handshake succeeded despite using a |
| 2854 | // TLS mode which was incompatible with the leaf certificate's keyUsage |
| 2855 | // extension. |
| 2856 | bool was_key_usage_invalid : 1; |
| 2857 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 2858 | // hs_buf is the buffer of handshake data to process. |
| 2859 | UniquePtr<BUF_MEM> hs_buf; |
| 2860 | |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 2861 | // pending_hs_data contains the pending handshake data that has not yet |
| 2862 | // been encrypted to |pending_flight|. This allows packing the handshake into |
| 2863 | // fewer records. |
| 2864 | UniquePtr<BUF_MEM> pending_hs_data; |
| 2865 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2866 | // pending_flight is the pending outgoing flight. This is used to flush each |
| 2867 | // handshake flight in a single write. |write_buffer| must be written out |
| 2868 | // before this data. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2869 | UniquePtr<BUF_MEM> pending_flight; |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 2870 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2871 | // pending_flight_offset is the number of bytes of |pending_flight| which have |
| 2872 | // been successfully written. |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2873 | uint32_t pending_flight_offset = 0; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2874 | |
Adam Langley | 2f9b47f | 2017-12-29 14:15:55 -0800 | [diff] [blame] | 2875 | // ticket_age_skew is the difference, in seconds, between the client-sent |
| 2876 | // ticket age and the server-computed value in TLS 1.3 server connections |
| 2877 | // which resumed a session. |
| 2878 | int32_t ticket_age_skew = 0; |
| 2879 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2880 | // ssl_early_data_reason stores details on why 0-RTT was accepted or rejected. |
| 2881 | enum ssl_early_data_reason_t early_data_reason = ssl_early_data_unknown; |
| 2882 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2883 | // aead_read_ctx is the current read cipher state. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2884 | UniquePtr<SSLAEADContext> aead_read_ctx; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2885 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2886 | // aead_write_ctx is the current write cipher state. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2887 | UniquePtr<SSLAEADContext> aead_write_ctx; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2888 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2889 | // hs is the handshake state for the current handshake or NULL if there isn't |
| 2890 | // one. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2891 | UniquePtr<SSL_HANDSHAKE> hs; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2892 | |
David Benjamin | bc42402 | 2019-07-21 12:05:20 -0400 | [diff] [blame] | 2893 | uint8_t write_traffic_secret[SSL_MAX_MD_SIZE] = {0}; |
| 2894 | uint8_t read_traffic_secret[SSL_MAX_MD_SIZE] = {0}; |
| 2895 | uint8_t exporter_secret[SSL_MAX_MD_SIZE] = {0}; |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2896 | uint8_t write_traffic_secret_len = 0; |
| 2897 | uint8_t read_traffic_secret_len = 0; |
| 2898 | uint8_t exporter_secret_len = 0; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2899 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2900 | // Connection binding to prevent renegotiation attacks |
David Benjamin | 9417257 | 2017-10-13 16:53:21 -0400 | [diff] [blame] | 2901 | uint8_t previous_client_finished[12] = {0}; |
| 2902 | uint8_t previous_client_finished_len = 0; |
| 2903 | uint8_t previous_server_finished_len = 0; |
| 2904 | uint8_t previous_server_finished[12] = {0}; |
Adam Langley | 4ba6e19 | 2016-12-07 15:54:54 -0800 | [diff] [blame] | 2905 | |
Adam Langley | 2f9b47f | 2017-12-29 14:15:55 -0800 | [diff] [blame] | 2906 | uint8_t send_alert[2] = {0}; |
| 2907 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2908 | // established_session is the session established by the connection. This |
| 2909 | // session is only filled upon the completion of the handshake and is |
| 2910 | // immutable. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2911 | UniquePtr<SSL_SESSION> established_session; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2912 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2913 | // Next protocol negotiation. For the client, this is the protocol that we |
| 2914 | // sent in NextProtocol and is set when handling ServerHello extensions. |
| 2915 | // |
| 2916 | // For a server, this is the client's selected_protocol from NextProtocol and |
| 2917 | // is set when handling the NextProtocol message, before the Finished |
| 2918 | // message. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2919 | Array<uint8_t> next_proto_negotiated; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2920 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2921 | // ALPN information |
| 2922 | // (we are in the process of transitioning from NPN to ALPN.) |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2923 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2924 | // In a server these point to the selected ALPN protocol after the |
| 2925 | // ClientHello has been processed. In a client these contain the protocol |
| 2926 | // that the server selected once the ServerHello has been processed. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2927 | Array<uint8_t> alpn_selected; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2928 | |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 2929 | // hostname, on the server, is the value of the SNI extension. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2930 | UniquePtr<char> hostname; |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 2931 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2932 | // For a server: |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2933 | // If |channel_id_valid| is true, then this contains the |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2934 | // verified Channel ID from the client: a P256 point, (x,y), where |
| 2935 | // each are big-endian values. |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 2936 | uint8_t channel_id[64] = {0}; |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2937 | |
| 2938 | // Contains the QUIC transport params received by the peer. |
| 2939 | Array<uint8_t> peer_quic_transport_params; |
David Benjamin | fceca8e | 2018-04-12 16:37:19 -0400 | [diff] [blame] | 2940 | |
| 2941 | // srtp_profile is the selected SRTP protection profile for |
| 2942 | // DTLS-SRTP. |
| 2943 | const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2944 | }; |
David Benjamin | e228bd2 | 2016-10-17 20:41:08 -0400 | [diff] [blame] | 2945 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2946 | // lengths of messages |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 2947 | #define DTLS1_RT_HEADER_LENGTH 13 |
| 2948 | |
| 2949 | #define DTLS1_HM_HEADER_LENGTH 12 |
| 2950 | |
| 2951 | #define DTLS1_CCS_HEADER_LENGTH 1 |
| 2952 | |
| 2953 | #define DTLS1_AL_HEADER_LENGTH 2 |
| 2954 | |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 2955 | struct hm_header_st { |
| 2956 | uint8_t type; |
| 2957 | uint32_t msg_len; |
| 2958 | uint16_t seq; |
| 2959 | uint32_t frag_off; |
| 2960 | uint32_t frag_len; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 2961 | }; |
| 2962 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2963 | // An hm_fragment is an incoming DTLS message, possibly not yet assembled. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2964 | struct hm_fragment { |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 2965 | static constexpr bool kAllowUniquePtr = true; |
| 2966 | |
| 2967 | hm_fragment() {} |
| 2968 | hm_fragment(const hm_fragment &) = delete; |
| 2969 | hm_fragment &operator=(const hm_fragment &) = delete; |
| 2970 | |
| 2971 | ~hm_fragment(); |
| 2972 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2973 | // type is the type of the message. |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 2974 | uint8_t type = 0; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2975 | // seq is the sequence number of this message. |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 2976 | uint16_t seq = 0; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2977 | // msg_len is the length of the message body. |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 2978 | uint32_t msg_len = 0; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2979 | // data is a pointer to the message, including message header. It has length |
| 2980 | // |DTLS1_HM_HEADER_LENGTH| + |msg_len|. |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 2981 | uint8_t *data = nullptr; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2982 | // reassembly is a bitmask of |msg_len| bits corresponding to which parts of |
| 2983 | // the message have been received. It is NULL if the message is complete. |
David Benjamin | 049fdfc | 2017-10-17 01:12:53 -0400 | [diff] [blame] | 2984 | uint8_t *reassembly = nullptr; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2985 | }; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 2986 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 2987 | struct OPENSSL_timeval { |
| 2988 | uint64_t tv_sec; |
| 2989 | uint32_t tv_usec; |
| 2990 | }; |
| 2991 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2992 | struct DTLS1_STATE { |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 2993 | static constexpr bool kAllowUniquePtr = true; |
| 2994 | |
| 2995 | DTLS1_STATE(); |
| 2996 | ~DTLS1_STATE(); |
| 2997 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2998 | // has_change_cipher_spec is true if we have received a ChangeCipherSpec from |
| 2999 | // the peer in this epoch. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3000 | bool has_change_cipher_spec : 1; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3001 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3002 | // outgoing_messages_complete is true if |outgoing_messages| has been |
| 3003 | // completed by an attempt to flush it. Future calls to |add_message| and |
| 3004 | // |add_change_cipher_spec| will start a new flight. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3005 | bool outgoing_messages_complete : 1; |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 3006 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3007 | // flight_has_reply is true if the current outgoing flight is complete and has |
| 3008 | // processed at least one message. This is used to detect whether we or the |
| 3009 | // peer sent the final flight. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3010 | bool flight_has_reply : 1; |
David Benjamin | 302b818 | 2017-08-22 14:47:22 -0700 | [diff] [blame] | 3011 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3012 | // The current data and handshake epoch. This is initially undefined, and |
| 3013 | // starts at zero once the initial handshake is completed. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3014 | uint16_t r_epoch = 0; |
| 3015 | uint16_t w_epoch = 0; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3016 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3017 | // records being received in the current epoch |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3018 | DTLS1_BITMAP bitmap; |
| 3019 | |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3020 | uint16_t handshake_write_seq = 0; |
| 3021 | uint16_t handshake_read_seq = 0; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3022 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3023 | // save last sequence number for retransmissions |
David Benjamin | 32013e8 | 2022-09-22 16:55:34 -0400 | [diff] [blame] | 3024 | uint64_t last_write_sequence = 0; |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3025 | UniquePtr<SSLAEADContext> last_aead_write_ctx; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3026 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3027 | // incoming_messages is a ring buffer of incoming handshake messages that have |
| 3028 | // yet to be processed. The front of the ring buffer is message number |
| 3029 | // |handshake_read_seq|, at position |handshake_read_seq| % |
| 3030 | // |SSL_MAX_HANDSHAKE_FLIGHT|. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3031 | UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT]; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3032 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3033 | // outgoing_messages is the queue of outgoing messages from the last handshake |
| 3034 | // flight. |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 3035 | DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT]; |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3036 | uint8_t outgoing_messages_len = 0; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3037 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3038 | // outgoing_written is the number of outgoing messages that have been |
| 3039 | // written. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3040 | uint8_t outgoing_written = 0; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3041 | // outgoing_offset is the number of bytes of the next outgoing message have |
| 3042 | // been written. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3043 | uint32_t outgoing_offset = 0; |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 3044 | |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3045 | unsigned mtu = 0; // max DTLS packet size |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3046 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3047 | // num_timeouts is the number of times the retransmit timer has fired since |
| 3048 | // the last time it was reset. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3049 | unsigned num_timeouts = 0; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3050 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3051 | // Indicates when the last handshake msg or heartbeat sent will |
| 3052 | // timeout. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3053 | struct OPENSSL_timeval next_timeout = {0, 0}; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3054 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3055 | // timeout_duration_ms is the timeout duration in milliseconds. |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 3056 | unsigned timeout_duration_ms = 0; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3057 | }; |
David Benjamin | 593047f | 2015-05-08 13:08:52 -0400 | [diff] [blame] | 3058 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3059 | // An ALPSConfig is a pair of ALPN protocol and settings value to use with ALPS. |
| 3060 | struct ALPSConfig { |
| 3061 | Array<uint8_t> protocol; |
| 3062 | Array<uint8_t> settings; |
| 3063 | }; |
| 3064 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3065 | // SSL_CONFIG contains configuration bits that can be shed after the handshake |
| 3066 | // completes. Objects of this type are not shared; they are unique to a |
| 3067 | // particular |SSL|. |
| 3068 | // |
| 3069 | // See SSL_shed_handshake_config() for more about the conditions under which |
| 3070 | // configuration can be shed. |
| 3071 | struct SSL_CONFIG { |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 3072 | static constexpr bool kAllowUniquePtr = true; |
| 3073 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3074 | explicit SSL_CONFIG(SSL *ssl_arg); |
| 3075 | ~SSL_CONFIG(); |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3076 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3077 | // ssl is a non-owning pointer to the parent |SSL| object. |
| 3078 | SSL *const ssl = nullptr; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3079 | |
Nitish Sakhawalkar | a4af5f8 | 2019-03-25 17:26:59 -0700 | [diff] [blame] | 3080 | // conf_max_version is the maximum acceptable version configured by |
| 3081 | // |SSL_set_max_proto_version|. Note this version is not normalized in DTLS |
| 3082 | // and is further constrained by |SSL_OP_NO_*|. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3083 | uint16_t conf_max_version = 0; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3084 | |
Nitish Sakhawalkar | a4af5f8 | 2019-03-25 17:26:59 -0700 | [diff] [blame] | 3085 | // conf_min_version is the minimum acceptable version configured by |
| 3086 | // |SSL_set_min_proto_version|. Note this version is not normalized in DTLS |
| 3087 | // and is further constrained by |SSL_OP_NO_*|. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3088 | uint16_t conf_min_version = 0; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3089 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3090 | X509_VERIFY_PARAM *param = nullptr; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3091 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3092 | // crypto |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3093 | UniquePtr<SSLCipherPreferenceList> cipher_list; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3094 | |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 3095 | // This is used to hold the local certificate used (i.e. the server |
| 3096 | // certificate for a server or the client certificate for a client). |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3097 | UniquePtr<CERT> cert; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3098 | |
| 3099 | int (*verify_callback)(int ok, |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3100 | X509_STORE_CTX *ctx) = |
| 3101 | nullptr; // fail if callback returns 0 |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3102 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3103 | enum ssl_verify_result_t (*custom_verify_callback)( |
| 3104 | SSL *ssl, uint8_t *out_alert) = nullptr; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3105 | // Server-only: psk_identity_hint is the identity hint to send in |
| 3106 | // PSK-based key exchanges. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3107 | UniquePtr<char> psk_identity_hint; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3108 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3109 | unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity, |
| 3110 | unsigned max_identity_len, uint8_t *psk, |
| 3111 | unsigned max_psk_len) = nullptr; |
| 3112 | unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk, |
| 3113 | unsigned max_psk_len) = nullptr; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3114 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3115 | // for server side, keep the list of CA_dn we can use |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3116 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA; |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 3117 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3118 | // cached_x509_client_CA is a cache of parsed versions of the elements of |
| 3119 | // |client_CA|. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3120 | STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3121 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3122 | Array<uint16_t> supported_group_list; // our list |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3123 | |
David Benjamin | b587911 | 2021-05-18 17:10:18 -0400 | [diff] [blame] | 3124 | // channel_id_private is the client's Channel ID private key, or null if |
| 3125 | // Channel ID should not be offered on this connection. |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 3126 | UniquePtr<EVP_PKEY> channel_id_private; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3127 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3128 | // For a client, this contains the list of supported protocols in wire |
| 3129 | // format. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3130 | Array<uint8_t> alpn_client_proto_list; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3131 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3132 | // alps_configs contains the list of supported protocols to use with ALPS, |
| 3133 | // along with their corresponding ALPS values. |
| 3134 | GrowableArray<ALPSConfig> alps_configs; |
| 3135 | |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 3136 | // Contains the QUIC transport params that this endpoint will send. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3137 | Array<uint8_t> quic_transport_params; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3138 | |
Nick Harper | 7c52299 | 2020-04-30 14:15:49 -0700 | [diff] [blame] | 3139 | // Contains the context used to decide whether to accept early data in QUIC. |
| 3140 | Array<uint8_t> quic_early_data_context; |
| 3141 | |
Adam Langley | 826ce15 | 2018-08-03 10:31:21 -0700 | [diff] [blame] | 3142 | // verify_sigalgs, if not empty, is the set of signature algorithms |
| 3143 | // accepted from the peer in decreasing order of preference. |
| 3144 | Array<uint16_t> verify_sigalgs; |
| 3145 | |
David Benjamin | b95d4b4 | 2018-05-02 16:11:34 -0400 | [diff] [blame] | 3146 | // srtp_profiles is the list of configured SRTP protection profiles for |
| 3147 | // DTLS-SRTP. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3148 | UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles; |
David Benjamin | b95d4b4 | 2018-05-02 16:11:34 -0400 | [diff] [blame] | 3149 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3150 | // client_ech_config_list, if not empty, is a serialized ECHConfigList |
| 3151 | // structure for the client to use when negotiating ECH. |
| 3152 | Array<uint8_t> client_ech_config_list; |
| 3153 | |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3154 | // tls13_cipher_policy limits the set of ciphers that can be selected when |
| 3155 | // negotiating a TLS 1.3 connection. |
| 3156 | enum ssl_compliance_policy_t tls13_cipher_policy = ssl_compliance_policy_none; |
| 3157 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3158 | // verify_mode is a bitmask of |SSL_VERIFY_*| values. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3159 | uint8_t verify_mode = SSL_VERIFY_NONE; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3160 | |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 3161 | // ech_grease_enabled controls whether ECH GREASE may be sent in the |
| 3162 | // ClientHello. |
| 3163 | bool ech_grease_enabled : 1; |
| 3164 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3165 | // Enable signed certificate time stamps. Currently client only. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3166 | bool signed_cert_timestamps_enabled : 1; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3167 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3168 | // ocsp_stapling_enabled is only used by client connections and indicates |
| 3169 | // whether OCSP stapling will be requested. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3170 | bool ocsp_stapling_enabled : 1; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3171 | |
David Benjamin | b587911 | 2021-05-18 17:10:18 -0400 | [diff] [blame] | 3172 | // channel_id_enabled is copied from the |SSL_CTX|. For a server, it means |
| 3173 | // that we'll accept Channel IDs from clients. It is ignored on the client. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3174 | bool channel_id_enabled : 1; |
David Benjamin | 3f2611a | 2017-02-06 13:38:26 -0500 | [diff] [blame] | 3175 | |
Jesse Selover | d7266ec | 2019-01-30 16:06:10 -0500 | [diff] [blame] | 3176 | // If enforce_rsa_key_usage is true, the handshake will fail if the |
| 3177 | // keyUsage extension is present and incompatible with the TLS usage. |
| 3178 | // This field is not read until after certificate verification. |
| 3179 | bool enforce_rsa_key_usage : 1; |
| 3180 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3181 | // retain_only_sha256_of_client_certs is true if we should compute the SHA256 |
| 3182 | // hash of the peer's certificate and then discard it to save memory and |
| 3183 | // session space. Only effective on the server side. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3184 | bool retain_only_sha256_of_client_certs : 1; |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 3185 | |
| 3186 | // handoff indicates that a server should stop after receiving the |
| 3187 | // ClientHello and pause the handshake in such a way that |SSL_get_error| |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 3188 | // returns |SSL_ERROR_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX| |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 3189 | // element of the same name and may be cleared if the handoff is declined. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3190 | bool handoff : 1; |
Adam Langley | 8df8e64 | 2018-02-28 13:35:39 -0800 | [diff] [blame] | 3191 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3192 | // shed_handshake_config indicates that the handshake config (this object!) |
| 3193 | // should be freed after the handshake completes. |
| 3194 | bool shed_handshake_config : 1; |
David Benjamin | 2d98d49 | 2018-10-08 17:31:17 -0500 | [diff] [blame] | 3195 | |
David Benjamin | 6965d25 | 2018-11-19 15:49:56 -0600 | [diff] [blame] | 3196 | // jdk11_workaround is whether to disable TLS 1.3 for JDK 11 clients, as a |
| 3197 | // workaround for https://bugs.openjdk.java.net/browse/JDK-8211806. |
| 3198 | bool jdk11_workaround : 1; |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 3199 | |
| 3200 | // QUIC drafts up to and including 32 used a different TLS extension |
| 3201 | // codepoint to convey QUIC's transport parameters. |
| 3202 | bool quic_use_legacy_codepoint : 1; |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 3203 | |
| 3204 | // permute_extensions is whether to permute extensions when sending messages. |
| 3205 | bool permute_extensions : 1; |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 3206 | |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 3207 | // aes_hw_override if set indicates we should override checking for aes |
| 3208 | // hardware support, and use the value in aes_hw_override_value instead. |
| 3209 | bool aes_hw_override : 1; |
| 3210 | |
| 3211 | // aes_hw_override_value is used for testing to indicate the support or lack |
| 3212 | // of support for AES hw. The value is only considered if |aes_hw_override| is |
| 3213 | // true. |
| 3214 | bool aes_hw_override_value : 1; |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 3215 | |
| 3216 | // alps_use_new_codepoint if set indicates we use new ALPS extension codepoint |
| 3217 | // to negotiate and convey application settings. |
| 3218 | bool alps_use_new_codepoint : 1; |
David Benjamin | 60c2867 | 2024-03-01 12:37:47 -0500 | [diff] [blame] | 3219 | |
| 3220 | // check_client_certificate_type indicates whether the client, in TLS 1.2 and |
| 3221 | // below, will check its certificate against the server's requested |
| 3222 | // certificate types. |
| 3223 | bool check_client_certificate_type : 1; |
David Benjamin | 9280f15 | 2024-03-01 17:20:04 -0500 | [diff] [blame] | 3224 | |
| 3225 | // check_ecdsa_curve indicates whether the server, in TLS 1.2 and below, will |
| 3226 | // check its certificate against the client's supported ECDSA curves. |
| 3227 | bool check_ecdsa_curve : 1; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3228 | }; |
| 3229 | |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 3230 | // From RFC 8446, used in determining PSK modes. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 3231 | #define SSL_PSK_DHE_KE 0x1 |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 3232 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3233 | // kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early |
| 3234 | // data that will be accepted. This value should be slightly below |
| 3235 | // kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext. |
Alessandro Ghedini | de254b4 | 2017-04-17 19:12:33 +0100 | [diff] [blame] | 3236 | static const size_t kMaxEarlyDataAccepted = 14336; |
| 3237 | |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 3238 | UniquePtr<CERT> ssl_cert_dup(CERT *cert); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 3239 | bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer); |
| 3240 | bool ssl_is_key_type_supported(int key_type); |
| 3241 | // ssl_compare_public_and_private_key returns true if |pubkey| is the public |
| 3242 | // counterpart to |privkey|. Otherwise it returns false and pushes a helpful |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3243 | // message on the error queue. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 3244 | bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, |
Adam Langley | c26692c | 2017-01-25 09:34:42 -0800 | [diff] [blame] | 3245 | const EVP_PKEY *privkey); |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 3246 | bool ssl_get_new_session(SSL_HANDSHAKE *hs); |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 3247 | bool ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, |
| 3248 | const SSL_SESSION *session); |
| 3249 | bool ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3250 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3251 | // ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on |
| 3252 | // error. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 3253 | UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method); |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 3254 | |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 3255 | // ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table |
| 3256 | // keyed on session IDs. |
| 3257 | uint32_t ssl_hash_session_id(Span<const uint8_t> session_id); |
| 3258 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3259 | // SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over |
| 3260 | // the parsed data. |
Matthew Braithwaite | 6d597a3 | 2018-07-11 14:58:22 -0700 | [diff] [blame] | 3261 | OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_parse( |
| 3262 | CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool); |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 3263 | |
Adam Langley | e8d2439 | 2018-01-25 15:49:37 -0800 | [diff] [blame] | 3264 | // ssl_session_serialize writes |in| to |cbb| as if it were serialising a |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 3265 | // session for Session-ID resumption. It returns true on success and false on |
Adam Langley | e8d2439 | 2018-01-25 15:49:37 -0800 | [diff] [blame] | 3266 | // error. |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 3267 | OPENSSL_EXPORT bool ssl_session_serialize(const SSL_SESSION *in, CBB *cbb); |
Adam Langley | e8d2439 | 2018-01-25 15:49:37 -0800 | [diff] [blame] | 3268 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 3269 | // ssl_session_is_context_valid returns whether |session|'s session ID context |
| 3270 | // matches the one set on |hs|. |
| 3271 | bool ssl_session_is_context_valid(const SSL_HANDSHAKE *hs, |
| 3272 | const SSL_SESSION *session); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3273 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 3274 | // ssl_session_is_time_valid returns true if |session| is still valid and false |
| 3275 | // if it has expired. |
| 3276 | bool ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3277 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 3278 | // ssl_session_is_resumable returns whether |session| is resumable for |hs|. |
| 3279 | bool ssl_session_is_resumable(const SSL_HANDSHAKE *hs, |
| 3280 | const SSL_SESSION *session); |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 3281 | |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 3282 | // ssl_session_protocol_version returns the protocol version associated with |
| 3283 | // |session|. Note that despite the name, this is not the same as |
| 3284 | // |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name. |
| 3285 | uint16_t ssl_session_protocol_version(const SSL_SESSION *session); |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 3286 | |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 3287 | // ssl_session_get_digest returns the digest used in |session|. |
| 3288 | const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3289 | |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 3290 | void ssl_set_session(SSL *ssl, SSL_SESSION *session); |
| 3291 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3292 | // ssl_get_prev_session looks up the previous session based on |client_hello|. |
| 3293 | // On success, it sets |*out_session| to the session or nullptr if none was |
| 3294 | // found. If the session could not be looked up synchronously, it returns |
| 3295 | // |ssl_hs_pending_session| and should be called again. If a ticket could not be |
| 3296 | // decrypted immediately it returns |ssl_hs_pending_ticket| and should also |
| 3297 | // be called again. Otherwise, it returns |ssl_hs_error|. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3298 | enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs, |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 3299 | UniquePtr<SSL_SESSION> *out_session, |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3300 | bool *out_tickets_supported, |
| 3301 | bool *out_renew_ticket, |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 3302 | const SSL_CLIENT_HELLO *client_hello); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3303 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3304 | // The following flags determine which parts of the session are duplicated. |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3305 | #define SSL_SESSION_DUP_AUTH_ONLY 0x0 |
| 3306 | #define SSL_SESSION_INCLUDE_TICKET 0x1 |
| 3307 | #define SSL_SESSION_INCLUDE_NONAUTH 0x2 |
| 3308 | #define SSL_SESSION_DUP_ALL \ |
| 3309 | (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH) |
| 3310 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3311 | // SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the |
| 3312 | // fields in |session| or nullptr on error. The new session is non-resumable and |
| 3313 | // must be explicitly marked resumable once it has been filled in. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 3314 | OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, |
| 3315 | int dup_flags); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3316 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3317 | // ssl_session_rebase_time updates |session|'s start time to the current time, |
| 3318 | // adjusting the timeout so the expiration time is unchanged. |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 3319 | void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session); |
| 3320 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3321 | // ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews |
| 3322 | // |session|'s timeout to |timeout| (measured from the current time). The |
| 3323 | // renewal is clamped to the session's auth_timeout. |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 3324 | void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session, |
| 3325 | uint32_t timeout); |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 3326 | |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 3327 | void ssl_update_cache(SSL *ssl); |
David Benjamin | f31e681 | 2014-11-13 18:05:55 -0500 | [diff] [blame] | 3328 | |
David Benjamin | b29e1e1 | 2019-05-06 14:44:46 -0500 | [diff] [blame] | 3329 | void ssl_send_alert(SSL *ssl, int level, int desc); |
| 3330 | int ssl_send_alert_impl(SSL *ssl, int level, int desc); |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 3331 | bool tls_get_message(const SSL *ssl, SSLMessage *out); |
| 3332 | ssl_open_record_t tls_open_handshake(SSL *ssl, size_t *out_consumed, |
| 3333 | uint8_t *out_alert, Span<uint8_t> in); |
| 3334 | void tls_next_message(SSL *ssl); |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 3335 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 3336 | int tls_dispatch_alert(SSL *ssl); |
| 3337 | ssl_open_record_t tls_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 3338 | size_t *out_consumed, uint8_t *out_alert, |
| 3339 | Span<uint8_t> in); |
| 3340 | ssl_open_record_t tls_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 3341 | uint8_t *out_alert, |
| 3342 | Span<uint8_t> in); |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 3343 | int tls_write_app_data(SSL *ssl, bool *out_needs_handshake, |
| 3344 | size_t *out_bytes_written, Span<const uint8_t> in); |
Steven Valdez | b6b6ff3 | 2016-10-26 11:56:35 -0400 | [diff] [blame] | 3345 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 3346 | bool tls_new(SSL *ssl); |
| 3347 | void tls_free(SSL *ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3348 | |
David Benjamin | bcef514 | 2021-06-02 11:24:26 -0400 | [diff] [blame] | 3349 | bool tls_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
| 3350 | bool tls_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg); |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 3351 | bool tls_add_message(SSL *ssl, Array<uint8_t> msg); |
| 3352 | bool tls_add_change_cipher_spec(SSL *ssl); |
| 3353 | int tls_flush_flight(SSL *ssl); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 3354 | |
David Benjamin | bcef514 | 2021-06-02 11:24:26 -0400 | [diff] [blame] | 3355 | bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
| 3356 | bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 3357 | bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg); |
| 3358 | bool dtls1_add_change_cipher_spec(SSL *ssl); |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 3359 | int dtls1_flush_flight(SSL *ssl); |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 3360 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3361 | // ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 3362 | // the pending flight. It returns true on success and false on error. |
| 3363 | bool ssl_add_message_cbb(SSL *ssl, CBB *cbb); |
Steven Valdez | 5eead16 | 2016-11-11 22:23:25 -0500 | [diff] [blame] | 3364 | |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 3365 | // ssl_hash_message incorporates |msg| into the handshake hash. It returns true |
| 3366 | // on success and false on allocation failure. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 3367 | bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
David Benjamin | ced9479 | 2016-11-14 17:12:11 +0900 | [diff] [blame] | 3368 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 3369 | ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 3370 | size_t *out_consumed, uint8_t *out_alert, |
| 3371 | Span<uint8_t> in); |
| 3372 | ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 3373 | uint8_t *out_alert, |
| 3374 | Span<uint8_t> in); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3375 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 3376 | int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake, |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 3377 | size_t *out_bytes_written, Span<const uint8_t> in); |
David Benjamin | 45d45c1 | 2016-06-07 15:20:49 -0400 | [diff] [blame] | 3378 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3379 | // dtls1_write_record sends a record. It returns one on success and <= 0 on |
| 3380 | // error. |
David Benjamin | 4bd32a8 | 2021-05-14 17:06:29 -0400 | [diff] [blame] | 3381 | int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in, |
David Benjamin | 45d45c1 | 2016-06-07 15:20:49 -0400 | [diff] [blame] | 3382 | enum dtls1_use_epoch_t use_epoch); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3383 | |
David Benjamin | aad50db | 2016-06-23 17:49:12 -0400 | [diff] [blame] | 3384 | int dtls1_retransmit_outgoing_messages(SSL *ssl); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 3385 | bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3386 | CBS *out_body); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 3387 | bool dtls1_check_timeout_num(SSL *ssl); |
David Benjamin | 2fa83de | 2015-02-08 01:40:08 -0500 | [diff] [blame] | 3388 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3389 | void dtls1_start_timer(SSL *ssl); |
| 3390 | void dtls1_stop_timer(SSL *ssl); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 3391 | bool dtls1_is_timer_expired(SSL *ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3392 | unsigned int dtls1_min_mtu(void); |
| 3393 | |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 3394 | bool dtls1_new(SSL *ssl); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3395 | void dtls1_free(SSL *ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3396 | |
Adam Langley | c9827e0 | 2019-04-12 14:46:50 -0700 | [diff] [blame] | 3397 | bool dtls1_get_message(const SSL *ssl, SSLMessage *out); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 3398 | ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed, |
| 3399 | uint8_t *out_alert, Span<uint8_t> in); |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 3400 | void dtls1_next_message(SSL *ssl); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3401 | int dtls1_dispatch_alert(SSL *ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3402 | |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 3403 | // tls1_configure_aead configures either the read or write direction AEAD (as |
| 3404 | // determined by |direction|) using the keys generated by the TLS KDF. The |
| 3405 | // |key_block_cache| argument is used to store the generated key block, if |
| 3406 | // empty. Otherwise it's assumed that the key block is already contained within |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 3407 | // it. It returns true on success or false on error. |
| 3408 | bool tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction, |
| 3409 | Array<uint8_t> *key_block_cache, |
| 3410 | const SSL_SESSION *session, |
| 3411 | Span<const uint8_t> iv_override); |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 3412 | |
David Benjamin | 5fa22ed | 2020-06-22 17:08:11 -0400 | [diff] [blame] | 3413 | bool tls1_change_cipher_state(SSL_HANDSHAKE *hs, |
| 3414 | evp_aead_direction_t direction); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3415 | int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out, |
David Benjamin | 2637f3c | 2017-10-27 00:56:55 -0400 | [diff] [blame] | 3416 | Span<const uint8_t> premaster); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3417 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 3418 | // tls1_get_grouplist returns the locally-configured group preference list. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3419 | Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 3420 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3421 | // tls1_check_group_id returns whether |group_id| is consistent with locally- |
| 3422 | // configured group preferences. |
| 3423 | bool tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id); |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 3424 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3425 | // tls1_get_shared_group sets |*out_group_id| to the first preferred shared |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3426 | // group between client and server preferences and returns true. If none may be |
| 3427 | // found, it returns false. |
| 3428 | bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 3429 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3430 | // ssl_add_clienthello_tlsext writes ClientHello extensions to |out| for |type|. |
| 3431 | // It returns true on success and false on failure. The |header_len| argument is |
| 3432 | // the length of the ClientHello written so far and is used to compute the |
| 3433 | // padding length. (It does not include the record header or handshake headers.) |
| 3434 | // |
| 3435 | // If |type| is |ssl_client_hello_inner|, this function also writes the |
| 3436 | // compressed extensions to |out_encoded|. Otherwise, |out_encoded| should be |
| 3437 | // nullptr. |
| 3438 | // |
| 3439 | // On success, the function sets |*out_needs_psk_binder| to whether the last |
| 3440 | // ClientHello extension was the pre_shared_key extension and needs a PSK binder |
| 3441 | // filled in. The caller should then update |out| and, if applicable, |
| 3442 | // |out_encoded| with the binder after completing the whole message. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3443 | bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, CBB *out_encoded, |
| 3444 | bool *out_needs_psk_binder, |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 3445 | ssl_client_hello_type_t type, |
| 3446 | size_t header_len); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 3447 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3448 | bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out); |
| 3449 | bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3450 | const SSL_CLIENT_HELLO *client_hello); |
David Benjamin | e2cb423 | 2021-06-23 18:14:22 -0400 | [diff] [blame] | 3451 | bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *extensions); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3452 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3453 | #define tlsext_tick_md EVP_sha256 |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3454 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3455 | // ssl_process_ticket processes a session ticket from the client. It returns |
| 3456 | // one of: |
| 3457 | // |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and |
| 3458 | // |*out_renew_ticket| is set to whether the ticket should be renewed. |
| 3459 | // |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a |
| 3460 | // fresh ticket should be sent, but the given ticket cannot be used. |
| 3461 | // |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted. |
| 3462 | // Retry later. |
| 3463 | // |ssl_ticket_aead_error|: an error occured that is fatal to the connection. |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3464 | enum ssl_ticket_aead_result_t ssl_process_ticket( |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3465 | SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3466 | bool *out_renew_ticket, Span<const uint8_t> ticket, |
| 3467 | Span<const uint8_t> session_id); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3468 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3469 | // tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3470 | // the signature. If the key is valid, it saves the Channel ID and returns true. |
| 3471 | // Otherwise, it returns false. |
| 3472 | bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3473 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3474 | // tls1_write_channel_id generates a Channel ID message and puts the output in |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 3475 | // |cbb|. |ssl->channel_id_private| must already be set before calling. This |
| 3476 | // function returns true on success and false on error. |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3477 | bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3478 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3479 | // tls1_channel_id_hash computes the hash to be signed by Channel ID and writes |
| 3480 | // it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3481 | // true on success and false on failure. |
| 3482 | bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3483 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3484 | // tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 3485 | // hashes in |hs->new_session| so that Channel ID resumptions can sign that |
| 3486 | // data. |
| 3487 | bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3488 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3489 | // ssl_can_write returns whether |ssl| is allowed to write. |
| 3490 | bool ssl_can_write(const SSL *ssl); |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 3491 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3492 | // ssl_can_read returns wheter |ssl| is allowed to read. |
| 3493 | bool ssl_can_read(const SSL *ssl); |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 3494 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 3495 | void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3496 | void ssl_ctx_get_current_time(const SSL_CTX *ctx, |
| 3497 | struct OPENSSL_timeval *out_clock); |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 3498 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3499 | // ssl_reset_error_state resets state for |SSL_get_error|. |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 3500 | void ssl_reset_error_state(SSL *ssl); |
| 3501 | |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 3502 | // ssl_set_read_error sets |ssl|'s read half into an error state, saving the |
| 3503 | // current state of the error queue. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3504 | void ssl_set_read_error(SSL *ssl); |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 3505 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 3506 | BSSL_NAMESPACE_END |
Adam Langley | 11d11d6 | 2017-07-17 11:10:46 -0700 | [diff] [blame] | 3507 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3508 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3509 | // Opaque C types. |
| 3510 | // |
| 3511 | // The following types are exported to C code as public typedefs, so they must |
| 3512 | // be defined outside of the namespace. |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 3513 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3514 | // ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility |
| 3515 | // structure to support the legacy version-locked methods. |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 3516 | struct ssl_method_st { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3517 | // version, if non-zero, is the only protocol version acceptable to an |
| 3518 | // SSL_CTX initialized from this method. |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 3519 | uint16_t version; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3520 | // method is the underlying SSL_PROTOCOL_METHOD that initializes the |
| 3521 | // SSL_CTX. |
David Benjamin | 664e99a | 2017-10-10 19:23:21 -0400 | [diff] [blame] | 3522 | const bssl::SSL_PROTOCOL_METHOD *method; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3523 | // x509_method contains pointers to functions that might deal with |X509| |
| 3524 | // compatibility, or might be a no-op, depending on the application. |
David Benjamin | 0363de9 | 2018-06-29 13:57:38 -0400 | [diff] [blame] | 3525 | const bssl::SSL_X509_METHOD *x509_method; |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 3526 | }; |
| 3527 | |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 3528 | struct ssl_ctx_st : public bssl::RefCounted<ssl_ctx_st> { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3529 | explicit ssl_ctx_st(const SSL_METHOD *ssl_method); |
| 3530 | ssl_ctx_st(const ssl_ctx_st &) = delete; |
| 3531 | ssl_ctx_st &operator=(const ssl_ctx_st &) = delete; |
| 3532 | |
| 3533 | const bssl::SSL_PROTOCOL_METHOD *method = nullptr; |
| 3534 | const bssl::SSL_X509_METHOD *x509_method = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3535 | |
| 3536 | // lock is used to protect various operations on this object. |
| 3537 | CRYPTO_MUTEX lock; |
| 3538 | |
| 3539 | // conf_max_version is the maximum acceptable protocol version configured by |
| 3540 | // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS |
| 3541 | // and is further constrainted by |SSL_OP_NO_*|. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3542 | uint16_t conf_max_version = 0; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3543 | |
| 3544 | // conf_min_version is the minimum acceptable protocol version configured by |
| 3545 | // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS |
| 3546 | // and is further constrainted by |SSL_OP_NO_*|. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3547 | uint16_t conf_min_version = 0; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3548 | |
David Benjamin | 3f180b8 | 2022-05-09 17:45:18 -0400 | [diff] [blame] | 3549 | // num_tickets is the number of tickets to send immediately after the TLS 1.3 |
| 3550 | // handshake. TLS 1.3 recommends single-use tickets so, by default, issue two |
| 3551 | /// in case the client makes several connections before getting a renewal. |
| 3552 | uint8_t num_tickets = 2; |
| 3553 | |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 3554 | // quic_method is the method table corresponding to the QUIC hooks. |
| 3555 | const SSL_QUIC_METHOD *quic_method = nullptr; |
| 3556 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3557 | bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3558 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3559 | X509_STORE *cert_store = nullptr; |
| 3560 | LHASH_OF(SSL_SESSION) *sessions = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3561 | // Most session-ids that will be cached, default is |
| 3562 | // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3563 | unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
| 3564 | SSL_SESSION *session_cache_head = nullptr; |
| 3565 | SSL_SESSION *session_cache_tail = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3566 | |
| 3567 | // handshakes_since_cache_flush is the number of successful handshakes since |
| 3568 | // the last cache flush. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3569 | int handshakes_since_cache_flush = 0; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3570 | |
| 3571 | // This can have one of 2 values, ored together, |
| 3572 | // SSL_SESS_CACHE_CLIENT, |
| 3573 | // SSL_SESS_CACHE_SERVER, |
| 3574 | // Default is SSL_SESSION_CACHE_SERVER, which means only |
| 3575 | // SSL_accept which cache SSL_SESSIONS. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3576 | int session_cache_mode = SSL_SESS_CACHE_SERVER; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3577 | |
| 3578 | // session_timeout is the default lifetime for new sessions in TLS 1.2 and |
| 3579 | // earlier, in seconds. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3580 | uint32_t session_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3581 | |
| 3582 | // session_psk_dhe_timeout is the default lifetime for new sessions in TLS |
| 3583 | // 1.3, in seconds. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3584 | uint32_t session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3585 | |
| 3586 | // If this callback is not null, it will be called each time a session id is |
| 3587 | // added to the cache. If this function returns 1, it means that the |
| 3588 | // callback will do a SSL_SESSION_free() when it has finished using it. |
| 3589 | // Otherwise, on 0, it means the callback has finished with it. If |
| 3590 | // remove_session_cb is not null, it will be called when a session-id is |
| 3591 | // removed from the cache. After the call, OpenSSL will SSL_SESSION_free() |
| 3592 | // it. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3593 | int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess) = nullptr; |
| 3594 | void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3595 | SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len, |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3596 | int *copy) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3597 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3598 | // if defined, these override the X509_verify_cert() calls |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3599 | int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg) = nullptr; |
| 3600 | void *app_verify_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3601 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3602 | ssl_verify_result_t (*custom_verify_callback)(SSL *ssl, |
| 3603 | uint8_t *out_alert) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3604 | |
| 3605 | // Default password callback. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3606 | pem_password_cb *default_passwd_callback = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3607 | |
| 3608 | // Default password callback user data. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3609 | void *default_passwd_callback_userdata = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3610 | |
| 3611 | // get client cert callback |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3612 | int (*client_cert_cb)(SSL *ssl, X509 **out_x509, |
| 3613 | EVP_PKEY **out_pkey) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3614 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3615 | CRYPTO_EX_DATA ex_data; |
| 3616 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3617 | // Default values used when no per-SSL value is defined follow |
| 3618 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3619 | void (*info_callback)(const SSL *ssl, int type, int value) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3620 | |
| 3621 | // what we put in client cert requests |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3622 | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3623 | |
| 3624 | // cached_x509_client_CA is a cache of parsed versions of the elements of |
| 3625 | // |client_CA|. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3626 | STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3627 | |
| 3628 | |
| 3629 | // Default values to use in SSL structures follow (these are copied by |
| 3630 | // SSL_new) |
| 3631 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3632 | uint32_t options = 0; |
| 3633 | // Disable the auto-chaining feature by default. wpa_supplicant relies on this |
| 3634 | // feature, but require callers opt into it. |
| 3635 | uint32_t mode = SSL_MODE_NO_AUTO_CHAIN; |
| 3636 | uint32_t max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3637 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3638 | bssl::UniquePtr<bssl::CERT> cert; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3639 | |
| 3640 | // callback that allows applications to peek at protocol messages |
David Benjamin | 123eaae | 2022-01-27 12:25:11 -0500 | [diff] [blame] | 3641 | void (*msg_callback)(int is_write, int version, int content_type, |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3642 | const void *buf, size_t len, SSL *ssl, |
| 3643 | void *arg) = nullptr; |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3644 | void *msg_callback_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3645 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3646 | int verify_mode = SSL_VERIFY_NONE; |
| 3647 | int (*default_verify_callback)(int ok, X509_STORE_CTX *ctx) = |
| 3648 | nullptr; // called 'verify_callback' in the SSL |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3649 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3650 | X509_VERIFY_PARAM *param = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3651 | |
| 3652 | // select_certificate_cb is called before most ClientHello processing and |
| 3653 | // before the decision whether to resume a session is made. See |
| 3654 | // |ssl_select_cert_result_t| for details of the return values. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3655 | ssl_select_cert_result_t (*select_certificate_cb)(const SSL_CLIENT_HELLO *) = |
| 3656 | nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3657 | |
| 3658 | // dos_protection_cb is called once the resumption decision for a ClientHello |
| 3659 | // has been made. It returns one to continue the handshake or zero to |
| 3660 | // abort. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3661 | int (*dos_protection_cb)(const SSL_CLIENT_HELLO *) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3662 | |
Jesse Selover | 1c337e5 | 2018-08-10 13:28:47 -0400 | [diff] [blame] | 3663 | // Controls whether to verify certificates when resuming connections. They |
| 3664 | // were already verified when the connection was first made, so the default is |
| 3665 | // false. For now, this is only respected on clients, not servers. |
| 3666 | bool reverify_on_resume = false; |
| 3667 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3668 | // Maximum amount of data to send in one fragment. actual record size can be |
| 3669 | // more than this due to padding and MAC overheads. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3670 | uint16_t max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3671 | |
| 3672 | // TLS extensions servername callback |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 3673 | int (*servername_callback)(SSL *, int *, void *) = nullptr; |
| 3674 | void *servername_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3675 | |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 3676 | // RFC 4507 session ticket keys. |ticket_key_current| may be NULL before the |
| 3677 | // first handshake and |ticket_key_prev| may be NULL at any time. |
| 3678 | // Automatically generated ticket keys are rotated as needed at handshake |
| 3679 | // time. Hence, all access must be synchronized through |lock|. |
| 3680 | bssl::UniquePtr<bssl::TicketKey> ticket_key_current; |
| 3681 | bssl::UniquePtr<bssl::TicketKey> ticket_key_prev; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3682 | |
| 3683 | // Callback to support customisation of ticket key setting |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 3684 | int (*ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv, |
| 3685 | EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3686 | |
| 3687 | // Server-only: psk_identity_hint is the default identity hint to send in |
| 3688 | // PSK-based key exchanges. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3689 | bssl::UniquePtr<char> psk_identity_hint; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3690 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3691 | unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity, |
| 3692 | unsigned max_identity_len, uint8_t *psk, |
| 3693 | unsigned max_psk_len) = nullptr; |
| 3694 | unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk, |
| 3695 | unsigned max_psk_len) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3696 | |
| 3697 | |
| 3698 | // Next protocol negotiation information |
| 3699 | // (for experimental NPN extension). |
| 3700 | |
| 3701 | // For a server, this contains a callback function by which the set of |
| 3702 | // advertised protocols can be provided. |
| 3703 | int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out, |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3704 | unsigned *out_len, void *arg) = nullptr; |
| 3705 | void *next_protos_advertised_cb_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3706 | // For a client, this contains a callback function that selects the |
| 3707 | // next protocol from the list provided by the server. |
| 3708 | int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len, |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3709 | const uint8_t *in, unsigned in_len, |
| 3710 | void *arg) = nullptr; |
| 3711 | void *next_proto_select_cb_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3712 | |
| 3713 | // ALPN information |
| 3714 | // (we are in the process of transitioning from NPN to ALPN.) |
| 3715 | |
| 3716 | // For a server, this contains a callback function that allows the |
| 3717 | // server to select the protocol for the connection. |
| 3718 | // out: on successful return, this must point to the raw protocol |
| 3719 | // name (without the length prefix). |
| 3720 | // outlen: on successful return, this contains the length of |*out|. |
| 3721 | // in: points to the client's list of supported protocols in |
| 3722 | // wire-format. |
| 3723 | // inlen: the length of |in|. |
| 3724 | int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len, |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3725 | const uint8_t *in, unsigned in_len, |
| 3726 | void *arg) = nullptr; |
| 3727 | void *alpn_select_cb_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3728 | |
| 3729 | // For a client, this contains the list of supported protocols in wire |
| 3730 | // format. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3731 | bssl::Array<uint8_t> alpn_client_proto_list; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3732 | |
| 3733 | // SRTP profiles we are willing to do from RFC 5764 |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3734 | bssl::UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3735 | |
| 3736 | // Defined compression algorithms for certificates. |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 3737 | bssl::GrowableArray<bssl::CertCompressionAlg> cert_compression_algs; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3738 | |
| 3739 | // Supported group values inherited by SSL structure |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3740 | bssl::Array<uint16_t> supported_group_list; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3741 | |
David Benjamin | b587911 | 2021-05-18 17:10:18 -0400 | [diff] [blame] | 3742 | // channel_id_private is the client's Channel ID private key, or null if |
| 3743 | // Channel ID should not be offered on this connection. |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 3744 | bssl::UniquePtr<EVP_PKEY> channel_id_private; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3745 | |
David Benjamin | c3b373b | 2021-06-06 13:04:26 -0400 | [diff] [blame] | 3746 | // ech_keys contains the server's list of ECHConfig values and associated |
| 3747 | // private keys. This list may be swapped out at any time, so all access must |
| 3748 | // be synchronized through |lock|. |
| 3749 | bssl::UniquePtr<SSL_ECH_KEYS> ech_keys; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 3750 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3751 | // keylog_callback, if not NULL, is the key logging callback. See |
| 3752 | // |SSL_CTX_set_keylog_callback|. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3753 | void (*keylog_callback)(const SSL *ssl, const char *line) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3754 | |
| 3755 | // current_time_cb, if not NULL, is the function to use to get the current |
| 3756 | // time. It sets |*out_clock| to the current time. The |ssl| argument is |
| 3757 | // always NULL. See |SSL_CTX_set_current_time_cb|. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3758 | void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3759 | |
| 3760 | // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate |
| 3761 | // memory. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3762 | CRYPTO_BUFFER_POOL *pool = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3763 | |
| 3764 | // ticket_aead_method contains function pointers for opening and sealing |
| 3765 | // session tickets. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3766 | const SSL_TICKET_AEAD_METHOD *ticket_aead_method = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3767 | |
| 3768 | // legacy_ocsp_callback implements an OCSP-related callback for OpenSSL |
| 3769 | // compatibility. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3770 | int (*legacy_ocsp_callback)(SSL *ssl, void *arg) = nullptr; |
| 3771 | void *legacy_ocsp_callback_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3772 | |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 3773 | // tls13_cipher_policy limits the set of ciphers that can be selected when |
| 3774 | // negotiating a TLS 1.3 connection. |
| 3775 | enum ssl_compliance_policy_t tls13_cipher_policy = ssl_compliance_policy_none; |
| 3776 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3777 | // verify_sigalgs, if not empty, is the set of signature algorithms |
| 3778 | // accepted from the peer in decreasing order of preference. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 3779 | bssl::Array<uint16_t> verify_sigalgs; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3780 | |
| 3781 | // retain_only_sha256_of_client_certs is true if we should compute the SHA256 |
| 3782 | // hash of the peer's certificate and then discard it to save memory and |
| 3783 | // session space. Only effective on the server side. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3784 | bool retain_only_sha256_of_client_certs : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3785 | |
| 3786 | // quiet_shutdown is true if the connection should not send a close_notify on |
| 3787 | // shutdown. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3788 | bool quiet_shutdown : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3789 | |
| 3790 | // ocsp_stapling_enabled is only used by client connections and indicates |
| 3791 | // whether OCSP stapling will be requested. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3792 | bool ocsp_stapling_enabled : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3793 | |
| 3794 | // If true, a client will request certificate timestamps. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3795 | bool signed_cert_timestamps_enabled : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3796 | |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 3797 | // channel_id_enabled is whether Channel ID is enabled. For a server, means |
| 3798 | // that we'll accept Channel IDs from clients. For a client, means that we'll |
| 3799 | // advertise support. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3800 | bool channel_id_enabled : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3801 | |
David Benjamin | 3675eb3 | 2021-05-18 14:01:07 -0400 | [diff] [blame] | 3802 | // grease_enabled is whether GREASE (RFC 8701) is enabled. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3803 | bool grease_enabled : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3804 | |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 3805 | // permute_extensions is whether to permute extensions when sending messages. |
| 3806 | bool permute_extensions : 1; |
| 3807 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3808 | // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN |
| 3809 | // protocols from the peer. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3810 | bool allow_unknown_alpn_protos : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3811 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3812 | // false_start_allowed_without_alpn is whether False Start (if |
| 3813 | // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3814 | bool false_start_allowed_without_alpn : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3815 | |
| 3816 | // handoff indicates that a server should stop after receiving the |
| 3817 | // ClientHello and pause the handshake in such a way that |SSL_get_error| |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 3818 | // returns |SSL_ERROR_HANDOFF|. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3819 | bool handoff : 1; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3820 | |
| 3821 | // If enable_early_data is true, early data can be sent and accepted. |
| 3822 | bool enable_early_data : 1; |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3823 | |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 3824 | // aes_hw_override if set indicates we should override checking for AES |
| 3825 | // hardware support, and use the value in aes_hw_override_value instead. |
| 3826 | bool aes_hw_override : 1; |
| 3827 | |
| 3828 | // aes_hw_override_value is used for testing to indicate the support or lack |
| 3829 | // of support for AES hardware. The value is only considered if |
| 3830 | // |aes_hw_override| is true. |
| 3831 | bool aes_hw_override_value : 1; |
| 3832 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3833 | private: |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 3834 | friend RefCounted; |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3835 | ~ssl_ctx_st(); |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3836 | }; |
| 3837 | |
| 3838 | struct ssl_st { |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3839 | explicit ssl_st(SSL_CTX *ctx_arg); |
| 3840 | ssl_st(const ssl_st &) = delete; |
| 3841 | ssl_st &operator=(const ssl_st &) = delete; |
| 3842 | ~ssl_st(); |
| 3843 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3844 | // method is the method table corresponding to the current protocol (DTLS or |
| 3845 | // TLS). |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3846 | const bssl::SSL_PROTOCOL_METHOD *method = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3847 | |
| 3848 | // config is a container for handshake configuration. Accesses to this field |
| 3849 | // should check for nullptr, since configuration may be shed after the |
| 3850 | // handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use |
| 3851 | // that instead, and skip the null check.) |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 3852 | bssl::UniquePtr<bssl::SSL_CONFIG> config; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3853 | |
| 3854 | // version is the protocol version. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3855 | uint16_t version = 0; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3856 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3857 | uint16_t max_send_fragment = 0; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3858 | |
| 3859 | // There are 2 BIO's even though they are normally both the same. This is so |
| 3860 | // data can be read and written to different handlers |
| 3861 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 3862 | bssl::UniquePtr<BIO> rbio; // used by SSL_read |
| 3863 | bssl::UniquePtr<BIO> wbio; // used by SSL_write |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3864 | |
| 3865 | // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|. |
| 3866 | // Otherwise, it returns a value corresponding to what operation is needed to |
| 3867 | // progress. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3868 | bssl::ssl_hs_wait_t (*do_handshake)(bssl::SSL_HANDSHAKE *hs) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3869 | |
David Benjamin | c59b9aa | 2018-07-16 21:34:03 -0400 | [diff] [blame] | 3870 | bssl::SSL3_STATE *s3 = nullptr; // TLS variables |
| 3871 | bssl::DTLS1_STATE *d1 = nullptr; // DTLS variables |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3872 | |
| 3873 | // callback that allows applications to peek at protocol messages |
| 3874 | void (*msg_callback)(int write_p, int version, int content_type, |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3875 | const void *buf, size_t len, SSL *ssl, |
| 3876 | void *arg) = nullptr; |
| 3877 | void *msg_callback_arg = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3878 | |
| 3879 | // session info |
| 3880 | |
| 3881 | // initial_timeout_duration_ms is the default DTLS timeout duration in |
| 3882 | // milliseconds. It's used to initialize the timer any time it's restarted. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3883 | // |
| 3884 | // RFC 6347 states that implementations SHOULD use an initial timer value of 1 |
| 3885 | // second. |
| 3886 | unsigned initial_timeout_duration_ms = 1000; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3887 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3888 | // session is the configured session to be offered by the client. This session |
| 3889 | // is immutable. |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 3890 | bssl::UniquePtr<SSL_SESSION> session; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3891 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3892 | void (*info_callback)(const SSL *ssl, int type, int value) = nullptr; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3893 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 3894 | bssl::UniquePtr<SSL_CTX> ctx; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3895 | |
| 3896 | // session_ctx is the |SSL_CTX| used for the session cache and related |
| 3897 | // settings. |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 3898 | bssl::UniquePtr<SSL_CTX> session_ctx; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3899 | |
| 3900 | // extra application data |
| 3901 | CRYPTO_EX_DATA ex_data; |
| 3902 | |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3903 | uint32_t options = 0; // protocol behaviour |
| 3904 | uint32_t mode = 0; // API behaviour |
| 3905 | uint32_t max_cert_list = 0; |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 3906 | bssl::UniquePtr<char> hostname; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3907 | |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 3908 | // quic_method is the method table corresponding to the QUIC hooks. |
| 3909 | const SSL_QUIC_METHOD *quic_method = nullptr; |
| 3910 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3911 | // renegotiate_mode controls how peer renegotiation attempts are handled. |
David Benjamin | c1389f2 | 2018-07-02 19:13:42 -0400 | [diff] [blame] | 3912 | ssl_renegotiate_mode_t renegotiate_mode = ssl_renegotiate_never; |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3913 | |
| 3914 | // server is true iff the this SSL* is the server half. Note: before the SSL* |
| 3915 | // is initialized by either SSL_set_accept_state or SSL_set_connect_state, |
| 3916 | // the side is not determined. In this state, server is always false. |
| 3917 | bool server : 1; |
| 3918 | |
| 3919 | // quiet_shutdown is true if the connection should not send a close_notify on |
| 3920 | // shutdown. |
| 3921 | bool quiet_shutdown : 1; |
| 3922 | |
David Benjamin | 4979803 | 2018-07-02 16:23:54 -0400 | [diff] [blame] | 3923 | // If enable_early_data is true, early data can be sent and accepted. |
| 3924 | bool enable_early_data : 1; |
| 3925 | }; |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 3926 | |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 3927 | struct ssl_session_st : public bssl::RefCounted<ssl_session_st> { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3928 | explicit ssl_session_st(const bssl::SSL_X509_METHOD *method); |
| 3929 | ssl_session_st(const ssl_session_st &) = delete; |
| 3930 | ssl_session_st &operator=(const ssl_session_st &) = delete; |
| 3931 | |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 3932 | // ssl_version is the (D)TLS version that established the session. |
| 3933 | uint16_t ssl_version = 0; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3934 | |
| 3935 | // group_id is the ID of the ECDH group used to establish this session or zero |
| 3936 | // if not applicable or unknown. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3937 | uint16_t group_id = 0; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3938 | |
| 3939 | // peer_signature_algorithm is the signature algorithm used to authenticate |
| 3940 | // the peer, or zero if not applicable or unknown. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3941 | uint16_t peer_signature_algorithm = 0; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3942 | |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 3943 | // secret, in TLS 1.2 and below, is the master secret associated with the |
| 3944 | // session. In TLS 1.3 and up, it is the resumption PSK for sessions handed to |
| 3945 | // the caller, but it stores the resumption secret when stored on |SSL| |
| 3946 | // objects. |
David Benjamin | 44a389a | 2022-12-10 00:46:54 -0500 | [diff] [blame] | 3947 | uint8_t secret_length = 0; |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 3948 | uint8_t secret[SSL_MAX_MASTER_KEY_LENGTH] = {0}; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3949 | |
| 3950 | // session_id - valid? |
David Benjamin | 44a389a | 2022-12-10 00:46:54 -0500 | [diff] [blame] | 3951 | uint8_t session_id_length = 0; |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3952 | uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0}; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3953 | // this is used to determine whether the session is being reused in |
| 3954 | // the appropriate context. It is up to the application to set this, |
| 3955 | // via SSL_new |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3956 | uint8_t sid_ctx_length = 0; |
| 3957 | uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0}; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3958 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3959 | bssl::UniquePtr<char> psk_identity; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3960 | |
| 3961 | // certs contains the certificate chain from the peer, starting with the leaf |
| 3962 | // certificate. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3963 | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3964 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3965 | const bssl::SSL_X509_METHOD *x509_method = nullptr; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3966 | |
| 3967 | // x509_peer is the peer's certificate. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3968 | X509 *x509_peer = nullptr; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3969 | |
| 3970 | // x509_chain is the certificate chain sent by the peer. NOTE: for historical |
| 3971 | // reasons, when a client (so the peer is a server), the chain includes |
| 3972 | // |peer|, but when a server it does not. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3973 | STACK_OF(X509) *x509_chain = nullptr; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3974 | |
| 3975 | // x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that |
| 3976 | // omits the leaf certificate. This exists because OpenSSL, historically, |
| 3977 | // didn't include the leaf certificate in the chain for a server, but did for |
| 3978 | // a client. The |x509_chain| always includes it and, if an API call requires |
| 3979 | // a chain without, it is stored here. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3980 | STACK_OF(X509) *x509_chain_without_leaf = nullptr; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3981 | |
| 3982 | // verify_result is the result of certificate verification in the case of |
| 3983 | // non-fatal certificate errors. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3984 | long verify_result = X509_V_ERR_INVALID_CALL; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3985 | |
| 3986 | // timeout is the lifetime of the session in seconds, measured from |time|. |
| 3987 | // This is renewable up to |auth_timeout|. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3988 | uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3989 | |
| 3990 | // auth_timeout is the non-renewable lifetime of the session in seconds, |
| 3991 | // measured from |time|. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3992 | uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3993 | |
| 3994 | // time is the time the session was issued, measured in seconds from the UNIX |
| 3995 | // epoch. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3996 | uint64_t time = 0; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3997 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 3998 | const SSL_CIPHER *cipher = nullptr; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 3999 | |
| 4000 | CRYPTO_EX_DATA ex_data; // application specific data |
| 4001 | |
| 4002 | // These are used to make removal of session-ids more efficient and to |
| 4003 | // implement a maximum cache size. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4004 | SSL_SESSION *prev = nullptr, *next = nullptr; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4005 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4006 | bssl::Array<uint8_t> ticket; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4007 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4008 | bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4009 | |
| 4010 | // The OCSP response that came with the session. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4011 | bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4012 | |
| 4013 | // peer_sha256 contains the SHA-256 hash of the peer's certificate if |
| 4014 | // |peer_sha256_valid| is true. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4015 | uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0}; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4016 | |
| 4017 | // original_handshake_hash contains the handshake hash (either SHA-1+MD5 or |
| 4018 | // SHA-2, depending on TLS version) for the original, full handshake that |
| 4019 | // created a session. This is used by Channel IDs during resumption. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4020 | uint8_t original_handshake_hash[EVP_MAX_MD_SIZE] = {0}; |
| 4021 | uint8_t original_handshake_hash_len = 0; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4022 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4023 | uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4024 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4025 | uint32_t ticket_age_add = 0; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4026 | |
| 4027 | // ticket_max_early_data is the maximum amount of data allowed to be sent as |
| 4028 | // early data. If zero, 0-RTT is disallowed. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4029 | uint32_t ticket_max_early_data = 0; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4030 | |
| 4031 | // early_alpn is the ALPN protocol from the initial handshake. This is only |
| 4032 | // stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 4033 | // resumptions. For the current connection's ALPN protocol, see |
| 4034 | // |alpn_selected| on |SSL3_STATE|. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4035 | bssl::Array<uint8_t> early_alpn; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4036 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 4037 | // local_application_settings, if |has_application_settings| is true, is the |
| 4038 | // local ALPS value for this connection. |
| 4039 | bssl::Array<uint8_t> local_application_settings; |
| 4040 | |
| 4041 | // peer_application_settings, if |has_application_settings| is true, is the |
| 4042 | // peer ALPS value for this connection. |
| 4043 | bssl::Array<uint8_t> peer_application_settings; |
| 4044 | |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 4045 | // extended_master_secret is whether the master secret in this session was |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4046 | // generated using EMS and thus isn't vulnerable to the Triple Handshake |
| 4047 | // attack. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 4048 | bool extended_master_secret : 1; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4049 | |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 4050 | // peer_sha256_valid is whether |peer_sha256| is valid. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 4051 | bool peer_sha256_valid : 1; // Non-zero if peer_sha256 is valid |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4052 | |
| 4053 | // not_resumable is used to indicate that session resumption is disallowed. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 4054 | bool not_resumable : 1; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4055 | |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 4056 | // ticket_age_add_valid is whether |ticket_age_add| is valid. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 4057 | bool ticket_age_add_valid : 1; |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4058 | |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 4059 | // is_server is whether this session was created by a server. |
Jesse Selover | 6b0d822 | 2018-08-14 12:47:29 -0400 | [diff] [blame] | 4060 | bool is_server : 1; |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4061 | |
Nick Harper | 6bfd25c | 2020-03-30 17:15:19 -0700 | [diff] [blame] | 4062 | // is_quic indicates whether this session was created using QUIC. |
| 4063 | bool is_quic : 1; |
| 4064 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 4065 | // has_application_settings indicates whether ALPS was negotiated in this |
| 4066 | // session. |
| 4067 | bool has_application_settings : 1; |
| 4068 | |
Nick Harper | 8519432 | 2020-05-20 16:59:29 -0700 | [diff] [blame] | 4069 | // quic_early_data_context is used to determine whether early data must be |
Nick Harper | 7c52299 | 2020-04-30 14:15:49 -0700 | [diff] [blame] | 4070 | // rejected when performing a QUIC handshake. |
Nick Harper | 8519432 | 2020-05-20 16:59:29 -0700 | [diff] [blame] | 4071 | bssl::Array<uint8_t> quic_early_data_context; |
Nick Harper | 7c52299 | 2020-04-30 14:15:49 -0700 | [diff] [blame] | 4072 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4073 | private: |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 4074 | friend RefCounted; |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 4075 | ~ssl_session_st(); |
David Benjamin | 1c68fa2 | 2018-06-02 19:02:46 -0400 | [diff] [blame] | 4076 | }; |
| 4077 | |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 4078 | struct ssl_ech_keys_st : public bssl::RefCounted<ssl_ech_keys_st> { |
| 4079 | ssl_ech_keys_st() : RefCounted(CheckSubClass()) {} |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 4080 | |
David Benjamin | 1d58cd1 | 2021-05-04 15:24:24 -0400 | [diff] [blame] | 4081 | bssl::GrowableArray<bssl::UniquePtr<bssl::ECHServerConfig>> configs; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 4082 | |
| 4083 | private: |
David Benjamin | fbf10f0 | 2024-02-12 16:57:18 -0500 | [diff] [blame] | 4084 | friend RefCounted; |
David Benjamin | c3b373b | 2021-06-06 13:04:26 -0400 | [diff] [blame] | 4085 | ~ssl_ech_keys_st() = default; |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 4086 | }; |
David Benjamin | e39ac8f | 2017-07-20 12:22:21 -0400 | [diff] [blame] | 4087 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4088 | #endif // OPENSSL_HEADER_SSL_INTERNAL_H |