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