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