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