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