blob: 5a787b8e67ec40c9d57dfaddd969a47fcba76be5 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Benjamin2ee94aa2015-04-07 22:38:30 -0400142#ifndef OPENSSL_HEADER_SSL_INTERNAL_H
143#define OPENSSL_HEADER_SSL_INTERNAL_H
Adam Langley95c29f32014-06-20 12:00:00 -0700144
145#include <openssl/base.h>
146
David Benjaminc9376992017-07-24 15:31:13 -0400147#include <stdlib.h>
148
David Benjamin499742c2017-07-22 12:45:38 -0400149#include <limits>
David Benjamind272dea2017-07-29 11:44:22 -0400150#include <new>
David Benjamincfc11c22017-07-18 22:45:18 -0400151#include <type_traits>
152#include <utility>
153
Adam Langleyc9fb3752014-06-20 12:00:00 -0700154#include <openssl/aead.h>
David Benjamincfc11c22017-07-18 22:45:18 -0400155#include <openssl/err.h>
David Benjamin4e840352017-10-11 18:41:11 -0400156#include <openssl/lhash.h>
David Benjamincfc11c22017-07-18 22:45:18 -0400157#include <openssl/mem.h>
David Benjamin499742c2017-07-22 12:45:38 -0400158#include <openssl/span.h>
Jesse Selover6b0d8222018-08-14 12:47:29 -0400159#include <openssl/ssl.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/stack.h>
161
David Benjamine52f4c42017-10-01 22:35:10 -0400162#include "../crypto/err/internal.h"
David Benjamin08f5c762017-09-21 02:43:05 -0400163#include "../crypto/internal.h"
164
Steven Valdezcb966542016-08-17 16:56:14 -0400165
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400166#if defined(OPENSSL_WINDOWS)
David Benjaminc11ea9422017-08-29 16:33:21 -0400167// Windows defines struct timeval in winsock2.h.
David Benjamina353cdb2016-06-09 16:48:33 -0400168OPENSSL_MSVC_PRAGMA(warning(push, 3))
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400169#include <winsock2.h>
David Benjamina353cdb2016-06-09 16:48:33 -0400170OPENSSL_MSVC_PRAGMA(warning(pop))
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400171#else
David Benjamin0abd6f22015-12-04 21:49:53 -0500172#include <sys/time.h>
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400173#endif
174
Steven Valdez87eab492016-06-27 16:34:59 -0400175
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700176BSSL_NAMESPACE_BEGIN
Adam Langleyfcf25832014-12-18 17:42:32 -0800177
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700178struct SSL_CONFIG;
David Benjamin86e95b82017-07-18 16:34:25 -0400179struct SSL_HANDSHAKE;
David Benjamin664e99a2017-10-10 19:23:21 -0400180struct SSL_PROTOCOL_METHOD;
David Benjamin0363de92018-06-29 13:57:38 -0400181struct SSL_X509_METHOD;
Steven Valdez8f36c512017-06-20 10:55:02 -0400182
David Benjaminc11ea9422017-08-29 16:33:21 -0400183// C++ utilities.
David Benjamincfc11c22017-07-18 22:45:18 -0400184
David Benjaminc11ea9422017-08-29 16:33:21 -0400185// 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 Benjamincfc11c22017-07-18 22:45:18 -0400190template <typename T, typename... Args>
191T *New(Args &&... args) {
David Benjamina4cb62f2017-07-31 13:52:31 -0400192 void *t = OPENSSL_malloc(sizeof(T));
David Benjamincfc11c22017-07-18 22:45:18 -0400193 if (t == nullptr) {
194 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
195 return nullptr;
196 }
David Benjamina4cb62f2017-07-31 13:52:31 -0400197 return new (t) T(std::forward<Args>(args)...);
David Benjamincfc11c22017-07-18 22:45:18 -0400198}
199
David Benjaminc11ea9422017-08-29 16:33:21 -0400200// Delete behaves like |delete| but uses |OPENSSL_free| to release memory.
201//
202// Note: unlike |delete| this does not support non-public destructors.
David Benjamincfc11c22017-07-18 22:45:18 -0400203template <typename T>
204void Delete(T *t) {
205 if (t != nullptr) {
206 t->~T();
207 OPENSSL_free(t);
208 }
209}
210
David Benjaminc11ea9422017-08-29 16:33:21 -0400211// 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 Benjamincfc11c22017-07-18 22:45:18 -0400213namespace internal {
214template <typename T>
David Benjamin9a892502017-07-25 22:26:44 -0400215struct DeleterImpl<T, typename std::enable_if<T::kAllowUniquePtr>::type> {
David Benjamincfc11c22017-07-18 22:45:18 -0400216 static void Free(T *t) { Delete(t); }
217};
Jesse Selover6b0d8222018-08-14 12:47:29 -0400218} // namespace internal
David Benjamincfc11c22017-07-18 22:45:18 -0400219
David Benjaminc11ea9422017-08-29 16:33:21 -0400220// MakeUnique behaves like |std::make_unique| but returns nullptr on allocation
221// error.
David Benjamincfc11c22017-07-18 22:45:18 -0400222template <typename T, typename... Args>
223UniquePtr<T> MakeUnique(Args &&... args) {
224 return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
225}
226
David Benjamin506be382017-07-25 15:18:18 -0400227#if defined(BORINGSSL_ALLOW_CXX_RUNTIME)
228#define HAS_VIRTUAL_DESTRUCTOR
229#define PURE_VIRTUAL = 0
230#else
David Benjamin35368092017-08-29 16:55:10 -0400231// HAS_VIRTUAL_DESTRUCTOR should be declared in any base class which defines a
David Benjaminc11ea9422017-08-29 16:33:21 -0400232// virtual destructor. This avoids a dependency on |_ZdlPv| and prevents the
233// class from being used with |delete|.
David Benjaminc9376992017-07-24 15:31:13 -0400234#define HAS_VIRTUAL_DESTRUCTOR \
235 void operator delete(void *) { abort(); }
236
David Benjaminc11ea9422017-08-29 16:33:21 -0400237// PURE_VIRTUAL should be used instead of = 0 when defining pure-virtual
238// functions. This avoids a dependency on |__cxa_pure_virtual| but loses
239// compile-time checking.
Jesse Selover6b0d8222018-08-14 12:47:29 -0400240#define PURE_VIRTUAL \
241 { abort(); }
David Benjamin506be382017-07-25 15:18:18 -0400242#endif
David Benjaminc9376992017-07-24 15:31:13 -0400243
Bruce Dawsone05b72c2017-10-10 12:56:54 -0700244// CONSTEXPR_ARRAY works around a VS 2015 bug where ranged for loops don't work
245// on constexpr arrays.
246#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1910
247#define CONSTEXPR_ARRAY const
248#else
249#define CONSTEXPR_ARRAY constexpr
250#endif
251
David Benjamin499742c2017-07-22 12:45:38 -0400252// Array<T> is an owning array of elements of |T|.
253template <typename T>
254class Array {
255 public:
256 // Array's default constructor creates an empty array.
257 Array() {}
258 Array(const Array &) = delete;
259 Array(Array &&other) { *this = std::move(other); }
260
261 ~Array() { Reset(); }
262
263 Array &operator=(const Array &) = delete;
264 Array &operator=(Array &&other) {
265 Reset();
266 other.Release(&data_, &size_);
267 return *this;
268 }
269
270 const T *data() const { return data_; }
271 T *data() { return data_; }
272 size_t size() const { return size_; }
David Benjaminb9493552017-09-27 19:02:51 -0400273 bool empty() const { return size_ == 0; }
David Benjamin499742c2017-07-22 12:45:38 -0400274
275 const T &operator[](size_t i) const { return data_[i]; }
276 T &operator[](size_t i) { return data_[i]; }
277
278 T *begin() { return data_; }
279 const T *cbegin() const { return data_; }
280 T *end() { return data_ + size_; }
281 const T *cend() const { return data_ + size_; }
282
283 void Reset() { Reset(nullptr, 0); }
284
285 // Reset releases the current contents of the array and takes ownership of the
286 // raw pointer supplied by the caller.
287 void Reset(T *new_data, size_t new_size) {
288 for (size_t i = 0; i < size_; i++) {
289 data_[i].~T();
290 }
291 OPENSSL_free(data_);
292 data_ = new_data;
293 size_ = new_size;
294 }
295
296 // Release releases ownership of the array to a raw pointer supplied by the
297 // caller.
298 void Release(T **out, size_t *out_size) {
299 *out = data_;
300 *out_size = size_;
301 data_ = nullptr;
302 size_ = 0;
303 }
304
305 // Init replaces the array with a newly-allocated array of |new_size|
306 // default-constructed copies of |T|. It returns true on success and false on
307 // error.
308 //
309 // Note that if |T| is a primitive type like |uint8_t|, it is uninitialized.
310 bool Init(size_t new_size) {
311 Reset();
312 if (new_size == 0) {
313 return true;
314 }
315
316 if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) {
317 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
318 return false;
319 }
Jesse Selover6b0d8222018-08-14 12:47:29 -0400320 data_ = reinterpret_cast<T *>(OPENSSL_malloc(new_size * sizeof(T)));
David Benjamin499742c2017-07-22 12:45:38 -0400321 if (data_ == nullptr) {
322 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
323 return false;
324 }
325 size_ = new_size;
326 for (size_t i = 0; i < size_; i++) {
327 new (&data_[i]) T;
328 }
329 return true;
330 }
331
David Benjamin08f5c762017-09-21 02:43:05 -0400332 // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns
333 // true on success and false on error.
David Benjamine325c3f2018-04-12 16:11:15 -0400334 bool CopyFrom(Span<const T> in) {
David Benjamin08f5c762017-09-21 02:43:05 -0400335 if (!Init(in.size())) {
336 return false;
337 }
David Benjamine325c3f2018-04-12 16:11:15 -0400338 OPENSSL_memcpy(data_, in.data(), sizeof(T) * in.size());
David Benjamin08f5c762017-09-21 02:43:05 -0400339 return true;
340 }
341
David Benjamin28655672018-07-18 23:23:25 -0400342 // Shrink shrinks the stored size of the array to |new_size|. It crashes if
343 // the new size is larger. Note this does not shrink the allocation itself.
344 void Shrink(size_t new_size) {
345 if (new_size > size_) {
346 abort();
347 }
348 size_ = new_size;
349 }
350
David Benjamin499742c2017-07-22 12:45:38 -0400351 private:
352 T *data_ = nullptr;
353 size_t size_ = 0;
354};
355
David Benjamin879efc32017-09-21 11:20:53 -0400356// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
Adam Langleyddb57cf2018-01-26 09:17:53 -0800357OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
David Benjamin879efc32017-09-21 11:20:53 -0400358
David Benjamincfc11c22017-07-18 22:45:18 -0400359
David Benjaminc11ea9422017-08-29 16:33:21 -0400360// Protocol versions.
361//
Steven Valdezb84674b2018-08-28 10:14:07 -0400362// Due to DTLS's historical wire version differences, we maintain two notions of
David Benjaminc11ea9422017-08-29 16:33:21 -0400363// version.
364//
365// The "version" or "wire version" is the actual 16-bit value that appears on
366// the wire. It uniquely identifies a version and is also used at API
367// boundaries. The set of supported versions differs between TLS and DTLS. Wire
368// versions are opaque values and may not be compared numerically.
369//
370// The "protocol version" identifies the high-level handshake variant being
Steven Valdezb84674b2018-08-28 10:14:07 -0400371// used. DTLS versions map to the corresponding TLS versions. Protocol versions
372// are sequential and may be compared numerically.
Steven Valdez8f36c512017-06-20 10:55:02 -0400373
David Benjaminc11ea9422017-08-29 16:33:21 -0400374// ssl_protocol_version_from_wire sets |*out| to the protocol version
David Benjamined9aed12017-09-27 19:24:09 -0400375// corresponding to wire version |version| and returns true. If |version| is not
376// a valid TLS or DTLS version, it returns false.
David Benjaminc11ea9422017-08-29 16:33:21 -0400377//
378// Note this simultaneously handles both DTLS and TLS. Use one of the
379// higher-level functions below for most operations.
David Benjamined9aed12017-09-27 19:24:09 -0400380bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400381
David Benjaminc11ea9422017-08-29 16:33:21 -0400382// ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
383// minimum and maximum enabled protocol versions, respectively.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700384bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
David Benjamined9aed12017-09-27 19:24:09 -0400385 uint16_t *out_max_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400386
David Benjamined9aed12017-09-27 19:24:09 -0400387// ssl_supports_version returns whether |hs| supports |version|.
388bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400389
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700390// ssl_method_supports_version returns whether |method| supports |version|.
391bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
392 uint16_t version);
393
David Benjaminc11ea9422017-08-29 16:33:21 -0400394// ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in
395// decreasing preference order.
David Benjamined9aed12017-09-27 19:24:09 -0400396bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb);
Steven Valdez8f36c512017-06-20 10:55:02 -0400397
David Benjaminc11ea9422017-08-29 16:33:21 -0400398// ssl_negotiate_version negotiates a common version based on |hs|'s preferences
David Benjamined9aed12017-09-27 19:24:09 -0400399// and the peer preference list in |peer_versions|. On success, it returns true
400// and sets |*out_version| to the selected version. Otherwise, it returns false
David Benjaminc11ea9422017-08-29 16:33:21 -0400401// and sets |*out_alert| to an alert to send.
David Benjamined9aed12017-09-27 19:24:09 -0400402bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
403 uint16_t *out_version, const CBS *peer_versions);
Steven Valdez8f36c512017-06-20 10:55:02 -0400404
David Benjamind1e3ce12017-10-06 18:31:15 -0400405// ssl_protocol_version returns |ssl|'s protocol version. It is an error to
David Benjaminc11ea9422017-08-29 16:33:21 -0400406// call this function before the version is determined.
David Benjamind1e3ce12017-10-06 18:31:15 -0400407uint16_t ssl_protocol_version(const SSL *ssl);
Steven Valdez8f36c512017-06-20 10:55:02 -0400408
David Benjaminc11ea9422017-08-29 16:33:21 -0400409// Cipher suites.
David Benjamin71f07942015-04-08 02:36:59 -0400410
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700411BSSL_NAMESPACE_END
David Benjamin33fc2ba2017-09-13 16:22:58 -0400412
413struct ssl_cipher_st {
414 // name is the OpenSSL name for the cipher.
415 const char *name;
416 // standard_name is the IETF name for the cipher.
417 const char *standard_name;
418 // id is the cipher suite value bitwise OR-d with 0x03000000.
419 uint32_t id;
420
421 // algorithm_* determine the cipher suite. See constants below for the values.
422 uint32_t algorithm_mkey;
423 uint32_t algorithm_auth;
424 uint32_t algorithm_enc;
425 uint32_t algorithm_mac;
426 uint32_t algorithm_prf;
427};
428
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700429BSSL_NAMESPACE_BEGIN
David Benjamin33fc2ba2017-09-13 16:22:58 -0400430
David Benjaminc11ea9422017-08-29 16:33:21 -0400431// Bits for |algorithm_mkey| (key exchange algorithm).
David Benjamine64d2c72017-07-12 16:31:08 -0400432#define SSL_kRSA 0x00000001u
433#define SSL_kECDHE 0x00000002u
David Benjaminc11ea9422017-08-29 16:33:21 -0400434// SSL_kPSK is only set for plain PSK, not ECDHE_PSK.
David Benjamine64d2c72017-07-12 16:31:08 -0400435#define SSL_kPSK 0x00000004u
436#define SSL_kGENERIC 0x00000008u
David Benjamin71f07942015-04-08 02:36:59 -0400437
David Benjaminc11ea9422017-08-29 16:33:21 -0400438// Bits for |algorithm_auth| (server authentication).
David Benjamine64d2c72017-07-12 16:31:08 -0400439#define SSL_aRSA 0x00000001u
440#define SSL_aECDSA 0x00000002u
David Benjaminc11ea9422017-08-29 16:33:21 -0400441// SSL_aPSK is set for both PSK and ECDHE_PSK.
David Benjamine64d2c72017-07-12 16:31:08 -0400442#define SSL_aPSK 0x00000004u
443#define SSL_aGENERIC 0x00000008u
David Benjamin71f07942015-04-08 02:36:59 -0400444
David Benjaminc032dfa2016-05-12 14:54:57 -0400445#define SSL_aCERT (SSL_aRSA | SSL_aECDSA)
446
David Benjaminc11ea9422017-08-29 16:33:21 -0400447// Bits for |algorithm_enc| (symmetric encryption).
Jesse Selover6b0d8222018-08-14 12:47:29 -0400448#define SSL_3DES 0x00000001u
449#define SSL_AES128 0x00000002u
450#define SSL_AES256 0x00000004u
451#define SSL_AES128GCM 0x00000008u
452#define SSL_AES256GCM 0x00000010u
453#define SSL_eNULL 0x00000020u
454#define SSL_CHACHA20POLY1305 0x00000040u
David Benjamin71f07942015-04-08 02:36:59 -0400455
456#define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
457
David Benjaminc11ea9422017-08-29 16:33:21 -0400458// Bits for |algorithm_mac| (symmetric authentication).
David Benjamine64d2c72017-07-12 16:31:08 -0400459#define SSL_SHA1 0x00000001u
David Benjaminc11ea9422017-08-29 16:33:21 -0400460// SSL_AEAD is set for all AEADs.
David Benjamin6e678ee2018-04-16 19:54:42 -0400461#define SSL_AEAD 0x00000002u
David Benjamin71f07942015-04-08 02:36:59 -0400462
David Benjaminc11ea9422017-08-29 16:33:21 -0400463// Bits for |algorithm_prf| (handshake digest).
David Benjaminb0883312015-08-06 09:54:13 -0400464#define SSL_HANDSHAKE_MAC_DEFAULT 0x1
465#define SSL_HANDSHAKE_MAC_SHA256 0x2
466#define SSL_HANDSHAKE_MAC_SHA384 0x4
David Benjamin71f07942015-04-08 02:36:59 -0400467
David Benjaminbc424022019-07-21 12:05:20 -0400468// SSL_MAX_MD_SIZE is size of the largest hash function used in TLS, SHA-384.
469#define SSL_MAX_MD_SIZE 48
470
David Benjamin48b276d2018-04-12 17:37:32 -0400471// An SSLCipherPreferenceList contains a list of SSL_CIPHERs with equal-
472// preference groups. For TLS clients, the groups are moot because the server
473// picks the cipher and groups cannot be expressed on the wire. However, for
474// servers, the equal-preference groups allow the client's preferences to be
475// partially respected. (This only has an effect with
476// SSL_OP_CIPHER_SERVER_PREFERENCE).
477//
478// The equal-preference groups are expressed by grouping SSL_CIPHERs together.
479// All elements of a group have the same priority: no ordering is expressed
480// within a group.
481//
482// The values in |ciphers| are in one-to-one correspondence with
483// |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of
484// bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to
485// indicate that the corresponding SSL_CIPHER is not the last element of a
486// group, or 0 to indicate that it is.
487//
488// For example, if |in_group_flags| contains all zeros then that indicates a
489// traditional, fully-ordered preference. Every SSL_CIPHER is the last element
490// of the group (i.e. they are all in a one-element group).
491//
492// For a more complex example, consider:
493// ciphers: A B C D E F
494// in_group_flags: 1 1 0 0 1 0
495//
496// That would express the following, order:
497//
498// A E
499// B -> D -> F
500// C
501struct SSLCipherPreferenceList {
502 static constexpr bool kAllowUniquePtr = true;
503
504 SSLCipherPreferenceList() = default;
505 ~SSLCipherPreferenceList();
506
507 bool Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers,
508 Span<const bool> in_group_flags);
Matthew Braithwaited2ed3822018-07-10 16:27:22 -0700509 bool Init(const SSLCipherPreferenceList &);
510
511 void Remove(const SSL_CIPHER *cipher);
David Benjamin48b276d2018-04-12 17:37:32 -0400512
513 UniquePtr<STACK_OF(SSL_CIPHER)> ciphers;
514 bool *in_group_flags = nullptr;
515};
David Benjamin71f07942015-04-08 02:36:59 -0400516
Matthew Braithwaited2ed3822018-07-10 16:27:22 -0700517// AllCiphers returns an array of all supported ciphers, sorted by id.
518Span<const SSL_CIPHER> AllCiphers();
519
David Benjaminc11ea9422017-08-29 16:33:21 -0400520// ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
521// object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
522// and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
523// respectively. The MAC key length is zero except for legacy block and stream
David Benjaminf4962492017-09-27 19:34:32 -0400524// ciphers. It returns true on success and false on error.
525bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
526 size_t *out_mac_secret_len,
527 size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
David Benjamin8525ff32018-09-05 18:44:15 -0500528 uint16_t version, bool is_dtls);
David Benjamin71f07942015-04-08 02:36:59 -0400529
David Benjaminc11ea9422017-08-29 16:33:21 -0400530// ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and
531// |cipher|.
David Benjaminca9e8f52017-08-09 15:02:34 -0400532const EVP_MD *ssl_get_handshake_digest(uint16_t version,
533 const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400534
David Benjamin5be3a742017-10-28 16:08:58 -0400535// ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a
David Benjamin48b276d2018-04-12 17:37:32 -0400536// newly-allocated |SSLCipherPreferenceList| containing the result. It returns
537// true on success and false on failure. If |strict| is true, nonsense will be
538// rejected. If false, nonsense will be silently ignored. An empty result is
539// considered an error regardless of |strict|.
David Benjamin0ce090a2018-07-02 20:24:40 -0400540bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list,
David Benjamin48b276d2018-04-12 17:37:32 -0400541 const char *rule_str, bool strict);
David Benjamin71f07942015-04-08 02:36:59 -0400542
David Benjaminc11ea9422017-08-29 16:33:21 -0400543// ssl_cipher_get_value returns the cipher suite id of |cipher|.
David Benjamina1c90a52015-05-30 17:03:14 -0400544uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
545
David Benjaminc11ea9422017-08-29 16:33:21 -0400546// ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth|
547// values suitable for use with |key| in TLS 1.2 and below.
David Benjamin69522112017-03-28 15:38:29 -0500548uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key);
David Benjamin71f07942015-04-08 02:36:59 -0400549
David Benjaminf4962492017-09-27 19:34:32 -0400550// ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the
551// server and, optionally, the client with a certificate.
552bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400553
David Benjaminf4962492017-09-27 19:34:32 -0400554// ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a
555// ServerKeyExchange message.
David Benjaminc11ea9422017-08-29 16:33:21 -0400556//
David Benjaminf4962492017-09-27 19:34:32 -0400557// This function may return false while still allowing |cipher| an optional
David Benjaminc11ea9422017-08-29 16:33:21 -0400558// ServerKeyExchange. This is the case for plain PSK ciphers.
David Benjaminf4962492017-09-27 19:34:32 -0400559bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400560
David Benjaminc11ea9422017-08-29 16:33:21 -0400561// ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the
562// length of an encrypted 1-byte record, for use in record-splitting. Otherwise
563// it returns zero.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400564size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
565
Steven Valdezd6f9c352019-06-25 10:13:18 -0400566// ssl_choose_tls13_cipher returns an |SSL_CIPHER| corresponding with the best
567// available from |cipher_suites| compatible with |version| and |group_id|. It
568// returns NULL if there isn't a compatible cipher.
569const SSL_CIPHER *ssl_choose_tls13_cipher(CBS cipher_suites, uint16_t version,
570 uint16_t group_id);
571
David Benjamin71f07942015-04-08 02:36:59 -0400572
David Benjaminc11ea9422017-08-29 16:33:21 -0400573// Transcript layer.
Steven Valdez908ac192017-01-12 13:17:07 -0500574
David Benjaminc11ea9422017-08-29 16:33:21 -0400575// SSLTranscript maintains the handshake transcript as a combination of a
576// buffer and running hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400577class SSLTranscript {
578 public:
579 SSLTranscript();
580 ~SSLTranscript();
581
David Benjaminc11ea9422017-08-29 16:33:21 -0400582 // Init initializes the handshake transcript. If called on an existing
583 // transcript, it resets the transcript and hash. It returns true on success
584 // and false on failure.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400585 bool Init();
586
David Benjaminc11ea9422017-08-29 16:33:21 -0400587 // InitHash initializes the handshake hash based on the PRF and contents of
588 // the handshake transcript. Subsequent calls to |Update| will update the
589 // rolling hash. It returns one on success and zero on failure. It is an error
590 // to call this function after the handshake buffer is released.
David Benjaminca9e8f52017-08-09 15:02:34 -0400591 bool InitHash(uint16_t version, const SSL_CIPHER *cipher);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400592
Steven Valdezcd8470f2017-10-11 12:29:36 -0400593 // UpdateForHelloRetryRequest resets the rolling hash with the
594 // HelloRetryRequest construction. It returns true on success and false on
595 // failure. It is an error to call this function before the handshake buffer
596 // is released.
597 bool UpdateForHelloRetryRequest();
598
599 // CopyHashContext copies the hash context into |ctx| and returns true on
600 // success.
601 bool CopyHashContext(EVP_MD_CTX *ctx);
602
David Benjamin75a1f232017-10-11 17:19:19 -0400603 Span<const uint8_t> buffer() {
604 return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
605 buffer_->length);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400606 }
David Benjamin6dc8bf62017-07-19 16:38:21 -0400607
David Benjaminc11ea9422017-08-29 16:33:21 -0400608 // FreeBuffer releases the handshake buffer. Subsequent calls to
609 // |Update| will not update the handshake buffer.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400610 void FreeBuffer();
611
David Benjaminc11ea9422017-08-29 16:33:21 -0400612 // DigestLen returns the length of the PRF hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400613 size_t DigestLen() const;
614
David Benjaminc11ea9422017-08-29 16:33:21 -0400615 // Digest returns the PRF hash. For TLS 1.1 and below, this is
616 // |EVP_md5_sha1|.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400617 const EVP_MD *Digest() const;
618
David Benjaminc11ea9422017-08-29 16:33:21 -0400619 // Update adds |in| to the handshake buffer and handshake hash, whichever is
620 // enabled. It returns true on success and false on failure.
David Benjamin75a1f232017-10-11 17:19:19 -0400621 bool Update(Span<const uint8_t> in);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400622
David Benjaminc11ea9422017-08-29 16:33:21 -0400623 // GetHash writes the handshake hash to |out| which must have room for at
624 // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
625 // the number of bytes written. Otherwise, it returns false.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400626 bool GetHash(uint8_t *out, size_t *out_len);
627
David Benjaminc11ea9422017-08-29 16:33:21 -0400628 // GetFinishedMAC computes the MAC for the Finished message into the bytes
629 // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
630 // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
631 // on failure.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400632 bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
David Benjamin6abaa312017-08-31 01:12:28 -0400633 bool from_server);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400634
635 private:
David Benjaminc11ea9422017-08-29 16:33:21 -0400636 // buffer_, if non-null, contains the handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400637 UniquePtr<BUF_MEM> buffer_;
David Benjamina4e9f8d2018-06-26 00:12:52 -0400638 // hash, if initialized with an |EVP_MD|, maintains the handshake hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400639 ScopedEVP_MD_CTX hash_;
David Benjamin86e95b82017-07-18 16:34:25 -0400640};
Steven Valdez908ac192017-01-12 13:17:07 -0500641
David Benjamin2637f3c2017-10-27 00:56:55 -0400642// tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
643// as the secret and |label| as the label. |seed1| and |seed2| are concatenated
644// to form the seed parameter. It returns true on success and false on failure.
645bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
646 Span<const uint8_t> secret, Span<const char> label,
647 Span<const uint8_t> seed1, Span<const uint8_t> seed2);
Steven Valdez908ac192017-01-12 13:17:07 -0500648
649
David Benjaminc11ea9422017-08-29 16:33:21 -0400650// Encryption layer.
David Benjamin31a07792015-03-03 14:20:26 -0500651
David Benjaminc11ea9422017-08-29 16:33:21 -0400652// SSLAEADContext contains information about an AEAD that is being used to
653// encrypt an SSL connection.
David Benjamincfc11c22017-07-18 22:45:18 -0400654class SSLAEADContext {
655 public:
Steven Valdezc7d4d212017-09-11 13:53:08 -0400656 SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
David Benjamincfc11c22017-07-18 22:45:18 -0400657 ~SSLAEADContext();
David Benjamin9a892502017-07-25 22:26:44 -0400658 static constexpr bool kAllowUniquePtr = true;
659
David Benjamincfc11c22017-07-18 22:45:18 -0400660 SSLAEADContext(const SSLAEADContext &&) = delete;
661 SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
662
David Benjaminc11ea9422017-08-29 16:33:21 -0400663 // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400664 static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
David Benjamincfc11c22017-07-18 22:45:18 -0400665
David Benjaminc11ea9422017-08-29 16:33:21 -0400666 // Create creates an |SSLAEADContext| using the supplied key material. It
667 // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
668 // resulting object, depending on |direction|. |version| is the normalized
669 // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
David Benjaminb9493552017-09-27 19:02:51 -0400670 static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
David Benjamin8525ff32018-09-05 18:44:15 -0500671 uint16_t version, bool is_dtls,
David Benjaminb9493552017-09-27 19:02:51 -0400672 const SSL_CIPHER *cipher,
673 Span<const uint8_t> enc_key,
674 Span<const uint8_t> mac_key,
675 Span<const uint8_t> fixed_iv);
David Benjamincfc11c22017-07-18 22:45:18 -0400676
Steven Valdezc8e0f902018-07-14 11:23:01 -0400677 // CreatePlaceholderForQUIC creates a placeholder |SSLAEADContext| for the
678 // given cipher and version. The resulting object can be queried for various
679 // properties but cannot encrypt or decrypt data.
680 static UniquePtr<SSLAEADContext> CreatePlaceholderForQUIC(
681 uint16_t version, const SSL_CIPHER *cipher);
682
Steven Valdezc7d4d212017-09-11 13:53:08 -0400683 // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
684 // cipher, to make version-specific determinations in the record layer prior
685 // to a cipher being selected.
686 void SetVersionIfNullCipher(uint16_t version);
687
688 // ProtocolVersion returns the protocol version associated with this
689 // SSLAEADContext. It can only be called once |version_| has been set to a
690 // valid value.
691 uint16_t ProtocolVersion() const;
692
693 // RecordVersion returns the record version that should be used with this
694 // SSLAEADContext for record construction and crypto.
695 uint16_t RecordVersion() const;
696
David Benjamincfc11c22017-07-18 22:45:18 -0400697 const SSL_CIPHER *cipher() const { return cipher_; }
698
David Benjaminc11ea9422017-08-29 16:33:21 -0400699 // is_null_cipher returns true if this is the null cipher.
David Benjamincfc11c22017-07-18 22:45:18 -0400700 bool is_null_cipher() const { return !cipher_; }
701
David Benjaminc11ea9422017-08-29 16:33:21 -0400702 // ExplicitNonceLen returns the length of the explicit nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400703 size_t ExplicitNonceLen() const;
704
David Benjaminc11ea9422017-08-29 16:33:21 -0400705 // MaxOverhead returns the maximum overhead of calling |Seal|.
David Benjamincfc11c22017-07-18 22:45:18 -0400706 size_t MaxOverhead() const;
707
David Benjaminc11ea9422017-08-29 16:33:21 -0400708 // SuffixLen calculates the suffix length written by |SealScatter| and writes
709 // it to |*out_suffix_len|. It returns true on success and false on error.
710 // |in_len| and |extra_in_len| should equal the argument of the same names
711 // passed to |SealScatter|.
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700712 bool SuffixLen(size_t *out_suffix_len, size_t in_len,
713 size_t extra_in_len) const;
David Benjamincfc11c22017-07-18 22:45:18 -0400714
David Benjamine2ab21d2018-04-04 23:55:06 -0400715 // CiphertextLen calculates the total ciphertext length written by
716 // |SealScatter| and writes it to |*out_len|. It returns true on success and
717 // false on error. |in_len| and |extra_in_len| should equal the argument of
718 // the same names passed to |SealScatter|.
719 bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const;
720
David Benjaminc64d1232017-10-04 18:14:28 -0400721 // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
722 // to the plaintext in |in| and returns true. Otherwise, it returns
723 // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
724 bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
David Benjamine2ab21d2018-04-04 23:55:06 -0400725 const uint8_t seqnum[8], Span<const uint8_t> header,
726 Span<uint8_t> in);
David Benjamincfc11c22017-07-18 22:45:18 -0400727
David Benjaminc11ea9422017-08-29 16:33:21 -0400728 // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
729 // result to |out|. It returns true on success and false on error.
730 //
731 // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
David Benjamincfc11c22017-07-18 22:45:18 -0400732 bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
David Benjamine2ab21d2018-04-04 23:55:06 -0400733 uint16_t record_version, const uint8_t seqnum[8],
734 Span<const uint8_t> header, const uint8_t *in, size_t in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400735
David Benjaminc11ea9422017-08-29 16:33:21 -0400736 // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
737 // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
738 // success and zero on error.
739 //
740 // On successful return, exactly |ExplicitNonceLen| bytes are written to
741 // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
742 // |out_suffix|.
743 //
744 // |extra_in| may point to an additional plaintext buffer. If present,
745 // |extra_in_len| additional bytes are encrypted and authenticated, and the
746 // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
747 // be used to size |out_suffix| accordingly.
748 //
749 // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
750 // alias anything.
David Benjamincfc11c22017-07-18 22:45:18 -0400751 bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400752 uint8_t type, uint16_t record_version,
David Benjamine2ab21d2018-04-04 23:55:06 -0400753 const uint8_t seqnum[8], Span<const uint8_t> header,
754 const uint8_t *in, size_t in_len, const uint8_t *extra_in,
755 size_t extra_in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400756
757 bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
758
759 private:
David Benjamine2ab21d2018-04-04 23:55:06 -0400760 // GetAdditionalData returns the additional data, writing into |storage| if
761 // necessary.
762 Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type,
763 uint16_t record_version,
764 const uint8_t seqnum[8],
765 size_t plaintext_len,
766 Span<const uint8_t> header);
David Benjamincfc11c22017-07-18 22:45:18 -0400767
768 const SSL_CIPHER *cipher_;
769 ScopedEVP_AEAD_CTX ctx_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400770 // fixed_nonce_ contains any bytes of the nonce that are fixed for all
771 // records.
David Benjamincfc11c22017-07-18 22:45:18 -0400772 uint8_t fixed_nonce_[12];
773 uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400774 // version_ is the wire version that should be used with this AEAD.
David Benjamincfc11c22017-07-18 22:45:18 -0400775 uint16_t version_;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400776 // is_dtls_ is whether DTLS is being used with this AEAD.
777 bool is_dtls_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400778 // variable_nonce_included_in_record_ is true if the variable nonce
779 // for a record is included as a prefix before the ciphertext.
David Benjamincfc11c22017-07-18 22:45:18 -0400780 bool variable_nonce_included_in_record_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400781 // random_variable_nonce_ is true if the variable nonce is
782 // randomly generated, rather than derived from the sequence
783 // number.
David Benjamincfc11c22017-07-18 22:45:18 -0400784 bool random_variable_nonce_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400785 // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
786 // variable nonce rather than prepended.
787 bool xor_fixed_nonce_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400788 // omit_length_in_ad_ is true if the length should be omitted in the
789 // AEAD's ad parameter.
David Benjamincfc11c22017-07-18 22:45:18 -0400790 bool omit_length_in_ad_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400791 // ad_is_header_ is true if the AEAD's ad parameter is the record header.
792 bool ad_is_header_ : 1;
David Benjamin86e95b82017-07-18 16:34:25 -0400793};
David Benjamin31a07792015-03-03 14:20:26 -0500794
David Benjamin31a07792015-03-03 14:20:26 -0500795
David Benjaminc11ea9422017-08-29 16:33:21 -0400796// DTLS replay bitmap.
David Benjamin7446a3b2015-07-25 17:53:57 -0400797
David Benjaminc11ea9422017-08-29 16:33:21 -0400798// DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
799// replayed packets. It should be initialized by zeroing every field.
David Benjamin86e95b82017-07-18 16:34:25 -0400800struct DTLS1_BITMAP {
David Benjaminc11ea9422017-08-29 16:33:21 -0400801 // map is a bit mask of the last 64 sequence numbers. Bit
802 // |1<<i| corresponds to |max_seq_num - i|.
David Benjamin800046f2017-10-17 01:37:51 -0400803 uint64_t map = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -0400804 // max_seq_num is the largest sequence number seen so far as a 64-bit
805 // integer.
David Benjamin800046f2017-10-17 01:37:51 -0400806 uint64_t max_seq_num = 0;
David Benjamin86e95b82017-07-18 16:34:25 -0400807};
David Benjamin7446a3b2015-07-25 17:53:57 -0400808
809
David Benjaminc11ea9422017-08-29 16:33:21 -0400810// Record layer.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400811
David Benjaminc11ea9422017-08-29 16:33:21 -0400812// ssl_record_sequence_update increments the sequence number in |seq|. It
David Benjamin8525ff32018-09-05 18:44:15 -0500813// returns true on success and false on wraparound.
814bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
David Benjamin1db21562015-12-25 15:11:39 -0500815
David Benjaminc11ea9422017-08-29 16:33:21 -0400816// ssl_record_prefix_len returns the length of the prefix before the ciphertext
817// of a record for |ssl|.
818//
819// TODO(davidben): Expose this as part of public API once the high-level
820// buffer-free APIs are available.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400821size_t ssl_record_prefix_len(const SSL *ssl);
822
823enum ssl_open_record_t {
824 ssl_open_record_success,
825 ssl_open_record_discard,
826 ssl_open_record_partial,
David Benjamin728f3542016-06-02 15:42:01 -0400827 ssl_open_record_close_notify,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400828 ssl_open_record_error,
829};
830
David Benjaminc11ea9422017-08-29 16:33:21 -0400831// tls_open_record decrypts a record from |in| in-place.
832//
833// If the input did not contain a complete record, it returns
834// |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
835// bytes necessary. It is guaranteed that a successful call to |tls_open_record|
836// will consume at least that many bytes.
837//
838// Otherwise, it sets |*out_consumed| to the number of bytes of input
839// consumed. Note that input may be consumed on all return codes if a record was
840// decrypted.
841//
842// On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
843// record type and |*out| to the record body in |in|. Note that |*out| may be
844// empty.
845//
846// If a record was successfully processed but should be discarded, it returns
847// |ssl_open_record_discard|.
848//
David Benjamin751d1a12017-09-25 13:37:30 -0400849// If a record was successfully processed but is a close_notify, it returns
850// |ssl_open_record_close_notify|.
David Benjaminc11ea9422017-08-29 16:33:21 -0400851//
David Benjamin751d1a12017-09-25 13:37:30 -0400852// On failure or fatal alert, it returns |ssl_open_record_error| and sets
853// |*out_alert| to an alert to emit, or zero if no alert should be emitted.
David Benjaminc64d1232017-10-04 18:14:28 -0400854enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
855 Span<uint8_t> *out, size_t *out_consumed,
856 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400857
David Benjamina05d4272017-10-06 19:34:55 -0400858// dtls_open_record implements |tls_open_record| for DTLS. It only returns
859// |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
860// zero. The caller should read one packet and try again.
David Benjaminc64d1232017-10-04 18:14:28 -0400861enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
862 Span<uint8_t> *out,
David Benjamina7810c12016-06-06 18:54:51 -0400863 size_t *out_consumed,
David Benjaminc64d1232017-10-04 18:14:28 -0400864 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400865
David Benjaminc11ea9422017-08-29 16:33:21 -0400866// ssl_seal_align_prefix_len returns the length of the prefix before the start
867// of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
868// use this to align buffers.
869//
870// Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
871// record and is the offset into second record's ciphertext. Thus sealing a
872// small record may result in a smaller output than this value.
873//
874// TODO(davidben): Is this alignment valuable? Record-splitting makes this a
875// mess.
David Benjamin1a01e1f2016-06-08 18:31:24 -0400876size_t ssl_seal_align_prefix_len(const SSL *ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400877
David Benjaminc11ea9422017-08-29 16:33:21 -0400878// tls_seal_record seals a new record of type |type| and body |in| and writes it
David Benjamin8525ff32018-09-05 18:44:15 -0500879// to |out|. At most |max_out| bytes will be written. It returns true on success
880// and false on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC
881// 1/n-1 record splitting and may write two records concatenated.
David Benjaminc11ea9422017-08-29 16:33:21 -0400882//
883// For a large record, the bulk of the ciphertext will begin
884// |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
885// improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
886// bytes to |out|.
887//
888// |in| and |out| may not alias.
David Benjamin8525ff32018-09-05 18:44:15 -0500889bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
890 uint8_t type, const uint8_t *in, size_t in_len);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400891
892enum dtls1_use_epoch_t {
893 dtls1_use_previous_epoch,
894 dtls1_use_current_epoch,
895};
896
David Benjaminc11ea9422017-08-29 16:33:21 -0400897// dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
898// record.
David Benjamin1a999cf2017-01-03 10:30:35 -0500899size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
900
David Benjaminc11ea9422017-08-29 16:33:21 -0400901// dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
902// front of the plaintext when sealing a record in-place.
David Benjamina772b162017-01-24 17:51:33 -0500903size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
904
David Benjaminc11ea9422017-08-29 16:33:21 -0400905// dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
906// which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
907// may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
908// ahead of |out|.
David Benjamin8525ff32018-09-05 18:44:15 -0500909bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
910 uint8_t type, const uint8_t *in, size_t in_len,
911 enum dtls1_use_epoch_t use_epoch);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400912
David Benjaminc11ea9422017-08-29 16:33:21 -0400913// ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
914// state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
915// |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
916// appropriate.
David Benjamin728f3542016-06-02 15:42:01 -0400917enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
David Benjaminc64d1232017-10-04 18:14:28 -0400918 Span<const uint8_t> in);
David Benjamin728f3542016-06-02 15:42:01 -0400919
David Benjaminb8d28cf2015-07-28 21:34:45 -0400920
David Benjaminc11ea9422017-08-29 16:33:21 -0400921// Private key operations.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400922
Christopher Patton9cde8482018-07-17 11:36:36 -0700923// ssl_has_private_key returns whether |hs| has a private key configured.
924bool ssl_has_private_key(const SSL_HANDSHAKE *hs);
nagendra modadugu601448a2015-07-24 09:31:31 -0700925
David Benjaminc11ea9422017-08-29 16:33:21 -0400926// ssl_private_key_* perform the corresponding operation on
927// |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
928// call the corresponding function or |complete| depending on whether there is a
929// pending operation. Otherwise, they implement the operation with
930// |EVP_PKEY|.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400931
David Benjaminb4d65fd2015-05-29 17:11:21 -0400932enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -0400933 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin75a1f232017-10-11 17:19:19 -0400934 uint16_t sigalg, Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400935
David Benjamin75a1f232017-10-11 17:19:19 -0400936enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
937 uint8_t *out,
938 size_t *out_len,
939 size_t max_out,
940 Span<const uint8_t> in);
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700941
David Benjamin75a1f232017-10-11 17:19:19 -0400942// ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
943// key supports |sigalg|.
944bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
Jesse Selover6b0d8222018-08-14 12:47:29 -0400945 uint16_t sigalg);
David Benjamin1fb125c2016-07-08 18:52:12 -0700946
David Benjaminc11ea9422017-08-29 16:33:21 -0400947// ssl_public_key_verify verifies that the |signature| is valid for the public
948// key |pkey| and input |in|, using the signature algorithm |sigalg|.
David Benjamin75a1f232017-10-11 17:19:19 -0400949bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
950 uint16_t sigalg, EVP_PKEY *pkey,
951 Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400952
David Benjamin1fb125c2016-07-08 18:52:12 -0700953
David Benjaminc11ea9422017-08-29 16:33:21 -0400954// Key shares.
David Benjamin4298d772015-12-19 00:18:25 -0500955
David Benjaminc11ea9422017-08-29 16:33:21 -0400956// SSLKeyShare abstracts over Diffie-Hellman-like key exchanges.
David Benjaminc642aca2017-07-19 23:28:43 -0400957class SSLKeyShare {
958 public:
959 virtual ~SSLKeyShare() {}
David Benjamin9a892502017-07-25 22:26:44 -0400960 static constexpr bool kAllowUniquePtr = true;
David Benjaminc9376992017-07-24 15:31:13 -0400961 HAS_VIRTUAL_DESTRUCTOR
David Benjaminb9179092016-10-26 13:47:33 -0400962
David Benjaminc11ea9422017-08-29 16:33:21 -0400963 // Create returns a SSLKeyShare instance for use with group |group_id| or
964 // nullptr on error.
David Benjaminc642aca2017-07-19 23:28:43 -0400965 static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
David Benjamin4298d772015-12-19 00:18:25 -0500966
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700967 // Create deserializes an SSLKeyShare instance previously serialized by
968 // |Serialize|.
969 static UniquePtr<SSLKeyShare> Create(CBS *in);
970
David Benjaminc11ea9422017-08-29 16:33:21 -0400971 // GroupID returns the group ID.
David Benjaminc9376992017-07-24 15:31:13 -0400972 virtual uint16_t GroupID() const PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -0500973
David Benjaminc11ea9422017-08-29 16:33:21 -0400974 // Offer generates a keypair and writes the public value to
975 // |out_public_key|. It returns true on success and false on error.
David Benjaminc9376992017-07-24 15:31:13 -0400976 virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -0500977
Adam Langley7b935932018-11-12 13:53:42 -0800978 // Accept performs a key exchange against the |peer_key| generated by |Offer|.
David Benjaminc11ea9422017-08-29 16:33:21 -0400979 // On success, it returns true, writes the public value to |out_public_key|,
Adam Langley7b935932018-11-12 13:53:42 -0800980 // and sets |*out_secret| to the shared secret. On failure, it returns false
981 // and sets |*out_alert| to an alert to send to the peer.
David Benjaminc11ea9422017-08-29 16:33:21 -0400982 //
983 // The default implementation calls |Offer| and then |Finish|, assuming a key
984 // exchange protocol where the peers are symmetric.
David Benjamin499742c2017-07-22 12:45:38 -0400985 virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
986 uint8_t *out_alert, Span<const uint8_t> peer_key);
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -0700987
David Benjaminc11ea9422017-08-29 16:33:21 -0400988 // Finish performs a key exchange against the |peer_key| generated by
David Benjamin499742c2017-07-22 12:45:38 -0400989 // |Accept|. On success, it returns true and sets |*out_secret| to the shared
Adam Langley7b935932018-11-12 13:53:42 -0800990 // secret. On failure, it returns false and sets |*out_alert| to an alert to
David Benjamin499742c2017-07-22 12:45:38 -0400991 // send to the peer.
992 virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
993 Span<const uint8_t> peer_key) PURE_VIRTUAL;
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700994
995 // Serialize writes the state of the key exchange to |out|, returning true if
996 // successful and false otherwise.
997 virtual bool Serialize(CBB *out) { return false; }
998
999 // Deserialize initializes the state of the key exchange from |in|, returning
1000 // true if successful and false otherwise. It is called by |Create|.
1001 virtual bool Deserialize(CBS *in) { return false; }
David Benjaminb9179092016-10-26 13:47:33 -04001002};
David Benjamin4298d772015-12-19 00:18:25 -05001003
Matthew Braithwaitec65eb2c2018-11-02 17:29:35 -07001004struct NamedGroup {
1005 int nid;
1006 uint16_t group_id;
1007 const char name[8], alias[11];
1008};
1009
1010// NamedGroups returns all supported groups.
1011Span<const NamedGroup> NamedGroups();
1012
David Benjaminc11ea9422017-08-29 16:33:21 -04001013// ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
David Benjamin8525ff32018-09-05 18:44:15 -05001014// sets |*out_group_id| to the group ID and returns true. Otherwise, it returns
1015// false.
1016bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
David Benjamin4298d772015-12-19 00:18:25 -05001017
David Benjamin8525ff32018-09-05 18:44:15 -05001018// ssl_name_to_group_id looks up the group corresponding to the |name| string of
1019// length |len|. On success, it sets |*out_group_id| to the group ID and returns
1020// true. Otherwise, it returns false.
1021bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001022
David Benjamin4298d772015-12-19 00:18:25 -05001023
David Benjaminc11ea9422017-08-29 16:33:21 -04001024// Handshake messages.
David Benjamin060cfb02016-05-12 00:43:05 -04001025
David Benjamin7934f082017-08-01 16:32:25 -04001026struct SSLMessage {
1027 bool is_v2_hello;
1028 uint8_t type;
1029 CBS body;
David Benjaminc11ea9422017-08-29 16:33:21 -04001030 // raw is the entire serialized handshake message, including the TLS or DTLS
1031 // message header.
David Benjamin7934f082017-08-01 16:32:25 -04001032 CBS raw;
1033};
1034
David Benjaminc11ea9422017-08-29 16:33:21 -04001035// SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
1036// ChangeCipherSpec, in the longest handshake flight. Currently this is the
1037// client's second leg in a full handshake when client certificates, NPN, and
1038// Channel ID, are all enabled.
David Benjamin29a83c52016-06-17 19:12:54 -04001039#define SSL_MAX_HANDSHAKE_FLIGHT 7
1040
Steven Valdez964b2372017-11-07 17:09:52 -05001041extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE];
Steven Valdezf1af1292018-08-13 10:54:48 -04001042extern const uint8_t kTLS12DowngradeRandom[8];
1043extern const uint8_t kTLS13DowngradeRandom[8];
David Benjamin6965d252018-11-19 15:49:56 -06001044extern const uint8_t kJDK11DowngradeRandom[8];
Steven Valdez964b2372017-11-07 17:09:52 -05001045
David Benjaminc11ea9422017-08-29 16:33:21 -04001046// ssl_max_handshake_message_len returns the maximum number of bytes permitted
1047// in a handshake message for |ssl|.
David Benjamin060cfb02016-05-12 00:43:05 -04001048size_t ssl_max_handshake_message_len(const SSL *ssl);
1049
David Benjamin33febf62017-10-07 16:52:50 -04001050// tls_can_accept_handshake_data returns whether |ssl| is able to accept more
1051// data into handshake buffer.
1052bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
1053
David Benjamin40e94702017-10-06 18:26:36 -04001054// tls_has_unprocessed_handshake_data returns whether there is buffered
1055// handshake data that has not been consumed by |get_message|.
1056bool tls_has_unprocessed_handshake_data(const SSL *ssl);
1057
David Benjamin7d10ab52018-11-02 18:31:17 -05001058// tls_append_handshake_data appends |data| to the handshake buffer. It returns
1059// true on success and false on allocation failure.
1060bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data);
1061
David Benjamin40e94702017-10-06 18:26:36 -04001062// dtls_has_unprocessed_handshake_data behaves like
1063// |tls_has_unprocessed_handshake_data| for DTLS.
1064bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
David Benjamin61672812016-07-14 23:10:43 -04001065
David Benjamin700631b2018-05-24 17:17:34 -04001066// tls_flush_pending_hs_data flushes any handshake plaintext data.
1067bool tls_flush_pending_hs_data(SSL *ssl);
1068
David Benjamin86e95b82017-07-18 16:34:25 -04001069struct DTLS_OUTGOING_MESSAGE {
David Benjamin049fdfc2017-10-17 01:12:53 -04001070 DTLS_OUTGOING_MESSAGE() {}
1071 DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
1072 DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
1073 ~DTLS_OUTGOING_MESSAGE() { Clear(); }
1074
1075 void Clear();
1076
1077 uint8_t *data = nullptr;
1078 uint32_t len = 0;
1079 uint16_t epoch = 0;
1080 bool is_ccs = false;
David Benjamin86e95b82017-07-18 16:34:25 -04001081};
David Benjamin29a83c52016-06-17 19:12:54 -04001082
David Benjaminc11ea9422017-08-29 16:33:21 -04001083// dtls_clear_outgoing_messages releases all buffered outgoing messages.
David Benjamin29a83c52016-06-17 19:12:54 -04001084void dtls_clear_outgoing_messages(SSL *ssl);
1085
David Benjamin060cfb02016-05-12 00:43:05 -04001086
David Benjaminc11ea9422017-08-29 16:33:21 -04001087// Callbacks.
David Benjamin4e9cc712016-06-01 20:16:03 -04001088
David Benjaminc11ea9422017-08-29 16:33:21 -04001089// ssl_do_info_callback calls |ssl|'s info callback, if set.
David Benjamin4e9cc712016-06-01 20:16:03 -04001090void ssl_do_info_callback(const SSL *ssl, int type, int value);
1091
David Benjaminc11ea9422017-08-29 16:33:21 -04001092// ssl_do_msg_callback calls |ssl|'s message callback, if set.
Adam Langleyc9827e02019-04-12 14:46:50 -07001093void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
David Benjaminc64d1232017-10-04 18:14:28 -04001094 Span<const uint8_t> in);
David Benjamin4e9cc712016-06-01 20:16:03 -04001095
1096
David Benjaminc11ea9422017-08-29 16:33:21 -04001097// Transport buffers.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001098
David Benjaminea712e32017-10-13 16:50:39 -04001099class SSLBuffer {
1100 public:
1101 SSLBuffer() {}
1102 ~SSLBuffer() { Clear(); }
1103
1104 SSLBuffer(const SSLBuffer &) = delete;
1105 SSLBuffer &operator=(const SSLBuffer &) = delete;
1106
1107 uint8_t *data() { return buf_ + offset_; }
1108 size_t size() const { return size_; }
1109 bool empty() const { return size_ == 0; }
1110 size_t cap() const { return cap_; }
1111
1112 Span<uint8_t> span() { return MakeSpan(data(), size()); }
1113
1114 Span<uint8_t> remaining() {
1115 return MakeSpan(data() + size(), cap() - size());
1116 }
1117
1118 // Clear releases the buffer.
1119 void Clear();
1120
1121 // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
1122 // that data written after |header_len| is aligned to a
1123 // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
1124 // on error.
1125 bool EnsureCap(size_t header_len, size_t new_cap);
1126
1127 // DidWrite extends the buffer by |len|. The caller must have filled in to
1128 // this point.
1129 void DidWrite(size_t len);
1130
1131 // Consume consumes |len| bytes from the front of the buffer. The memory
1132 // consumed will remain valid until the next call to |DiscardConsumed| or
1133 // |Clear|.
1134 void Consume(size_t len);
1135
1136 // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
1137 // is now empty, it releases memory used by it.
1138 void DiscardConsumed();
1139
1140 private:
1141 // buf_ is the memory allocated for this buffer.
1142 uint8_t *buf_ = nullptr;
1143 // offset_ is the offset into |buf_| which the buffer contents start at.
1144 uint16_t offset_ = 0;
1145 // size_ is the size of the buffer contents from |buf_| + |offset_|.
1146 uint16_t size_ = 0;
1147 // cap_ is how much memory beyond |buf_| + |offset_| is available.
1148 uint16_t cap_ = 0;
1149};
David Benjaminb8d28cf2015-07-28 21:34:45 -04001150
David Benjaminc11ea9422017-08-29 16:33:21 -04001151// ssl_read_buffer_extend_to extends the read buffer to the desired length. For
1152// TLS, it reads to the end of the buffer until the buffer is |len| bytes
1153// long. For DTLS, it reads a new packet and ignores |len|. It returns one on
1154// success, zero on EOF, and a negative number on error.
1155//
1156// It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
1157// non-empty.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001158int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
1159
David Benjaminea712e32017-10-13 16:50:39 -04001160// ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
1161// to a record-processing function. If |ret| is a success or if the caller
1162// should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
1163// 0.
David Benjamind9229f92017-10-06 17:36:20 -04001164int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
1165 size_t consumed, uint8_t alert);
1166
David Benjaminc11ea9422017-08-29 16:33:21 -04001167// ssl_write_buffer_flush flushes the write buffer to the transport. It returns
1168// one on success and <= 0 on error. For DTLS, whether or not the write
1169// succeeds, the write buffer will be cleared.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001170int ssl_write_buffer_flush(SSL *ssl);
1171
David Benjaminb8d28cf2015-07-28 21:34:45 -04001172
David Benjaminc11ea9422017-08-29 16:33:21 -04001173// Certificate functions.
David Benjamin75836432016-06-17 18:48:29 -04001174
David Benjamin8525ff32018-09-05 18:44:15 -05001175// ssl_has_certificate returns whether a certificate and private key are
1176// configured.
Christopher Patton9cde8482018-07-17 11:36:36 -07001177bool ssl_has_certificate(const SSL_HANDSHAKE *hs);
David Benjamin32a66d52016-07-13 22:03:11 -04001178
David Benjaminc11ea9422017-08-29 16:33:21 -04001179// ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
1180// by a TLS Certificate message. On success, it advances |cbs| and returns
1181// true. Otherwise, it returns false and sets |*out_alert| to an alert to send
1182// to the peer.
1183//
1184// If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
1185// the certificate chain and the leaf certificate's public key
1186// respectively. Otherwise, both will be set to nullptr.
1187//
1188// If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
1189// SHA-256 hash of the leaf to |out_leaf_sha256|.
David Benjamine664a532017-07-20 20:19:36 -04001190bool ssl_parse_cert_chain(uint8_t *out_alert,
1191 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
1192 UniquePtr<EVP_PKEY> *out_pubkey,
1193 uint8_t *out_leaf_sha256, CBS *cbs,
1194 CRYPTO_BUFFER_POOL *pool);
David Benjamin5c900c82016-07-13 23:03:26 -04001195
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001196// ssl_add_cert_chain adds |hs->ssl|'s certificate chain to |cbb| in the format
1197// used by a TLS Certificate message. If there is no certificate chain, it emits
David Benjamin8525ff32018-09-05 18:44:15 -05001198// an empty certificate list. It returns true on success and false on error.
1199bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjamin32a66d52016-07-13 22:03:11 -04001200
Jesse Seloverd7266ec2019-01-30 16:06:10 -05001201enum ssl_key_usage_t {
1202 key_usage_digital_signature = 0,
1203 key_usage_encipherment = 2,
1204};
1205
1206// ssl_cert_check_key_usage parses the DER-encoded, X.509 certificate in |in|
1207// and returns true if doesn't specify a key usage or, if it does, if it
1208// includes |bit|. Otherwise it pushes to the error queue and returns false.
1209bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit);
Adam Langley05672202016-12-13 12:05:49 -08001210
David Benjaminc11ea9422017-08-29 16:33:21 -04001211// ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
1212// certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
1213// nullptr and pushes to the error queue.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001214UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
Adam Langleyd5157222016-12-12 11:37:43 -08001215
David Benjaminc11ea9422017-08-29 16:33:21 -04001216// ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
1217// TLS CertificateRequest message. On success, it returns a newly-allocated
1218// |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
1219// sets |*out_alert| to an alert to send to the peer.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001220UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
1221 uint8_t *out_alert,
1222 CBS *cbs);
David Benjamine0332e82016-07-13 22:40:36 -04001223
Steven Valdezcd8470f2017-10-11 12:29:36 -04001224// ssl_has_client_CAs returns there are configured CAs.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001225bool ssl_has_client_CAs(const SSL_CONFIG *cfg);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001226
David Benjaminc11ea9422017-08-29 16:33:21 -04001227// ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
David Benjamin8525ff32018-09-05 18:44:15 -05001228// used by a TLS CertificateRequest message. It returns true on success and
1229// false on error.
1230bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjamin32a66d52016-07-13 22:03:11 -04001231
David Benjaminc11ea9422017-08-29 16:33:21 -04001232// ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
1233// a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
1234// an error on the error queue.
David Benjamin8525ff32018-09-05 18:44:15 -05001235bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Adam Langley05672202016-12-13 12:05:49 -08001236 const CRYPTO_BUFFER *leaf);
Steven Valdezbf5aa842016-07-15 07:07:40 -04001237
David Benjaminc11ea9422017-08-29 16:33:21 -04001238// ssl_on_certificate_selected is called once the certificate has been selected.
1239// It finalizes the certificate and initializes |hs->local_pubkey|. It returns
David Benjamin8525ff32018-09-05 18:44:15 -05001240// true on success and false on error.
1241bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
David Benjamina232a712017-03-30 15:51:53 -05001242
David Benjamin75836432016-06-17 18:48:29 -04001243
David Benjaminc11ea9422017-08-29 16:33:21 -04001244// TLS 1.3 key derivation.
Steven Valdez143e8b32016-07-11 13:19:03 -04001245
David Benjaminc11ea9422017-08-29 16:33:21 -04001246// tls13_init_key_schedule initializes the handshake hash and key derivation
Steven Valdezcd8470f2017-10-11 12:29:36 -04001247// state, and incorporates the PSK. The cipher suite and PRF hash must have been
David Benjamin8525ff32018-09-05 18:44:15 -05001248// selected at this point. It returns true on success and false on error.
1249bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1250 size_t psk_len);
Steven Valdez143e8b32016-07-11 13:19:03 -04001251
David Benjaminc11ea9422017-08-29 16:33:21 -04001252// tls13_init_early_key_schedule initializes the handshake hash and key
Steven Valdezcd8470f2017-10-11 12:29:36 -04001253// derivation state from the resumption secret and incorporates the PSK to
1254// derive the early secrets. It returns one on success and zero on error.
David Benjamin8525ff32018-09-05 18:44:15 -05001255bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1256 size_t psk_len);
Steven Valdez2d850622017-01-11 11:34:52 -05001257
David Benjaminc11ea9422017-08-29 16:33:21 -04001258// tls13_advance_key_schedule incorporates |in| into the key schedule with
David Benjamin8525ff32018-09-05 18:44:15 -05001259// HKDF-Extract. It returns true on success and false on error.
1260bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
David Benjamin6e4fc332016-11-17 16:43:08 +09001261 size_t len);
Steven Valdez143e8b32016-07-11 13:19:03 -04001262
David Benjaminc11ea9422017-08-29 16:33:21 -04001263// tls13_set_traffic_key sets the read or write traffic keys to
David Benjamin8525ff32018-09-05 18:44:15 -05001264// |traffic_secret|. It returns true on success and false on error.
Steven Valdezc8e0f902018-07-14 11:23:01 -04001265bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level,
1266 enum evp_aead_direction_t direction,
David Benjamin8525ff32018-09-05 18:44:15 -05001267 const uint8_t *traffic_secret,
1268 size_t traffic_secret_len);
Steven Valdez143e8b32016-07-11 13:19:03 -04001269
David Benjamin8525ff32018-09-05 18:44:15 -05001270// tls13_derive_early_secrets derives the early traffic secret. It returns true
1271// on success and false on error.
1272bool tls13_derive_early_secrets(SSL_HANDSHAKE *hs);
Steven Valdez2d850622017-01-11 11:34:52 -05001273
David Benjaminc11ea9422017-08-29 16:33:21 -04001274// tls13_derive_handshake_secrets derives the handshake traffic secret. It
David Benjamin8525ff32018-09-05 18:44:15 -05001275// returns true on success and false on error.
1276bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001277
David Benjaminc11ea9422017-08-29 16:33:21 -04001278// tls13_rotate_traffic_key derives the next read or write traffic secret. It
David Benjamin8525ff32018-09-05 18:44:15 -05001279// returns true on success and false on error.
1280bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
Steven Valdez1dc53d22016-07-26 12:27:38 -04001281
David Benjaminc11ea9422017-08-29 16:33:21 -04001282// tls13_derive_application_secrets derives the initial application data traffic
1283// and exporter secrets based on the handshake transcripts and |master_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001284// It returns true on success and false on error.
1285bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001286
David Benjaminc11ea9422017-08-29 16:33:21 -04001287// tls13_derive_resumption_secret derives the |resumption_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001288bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001289
David Benjaminc11ea9422017-08-29 16:33:21 -04001290// tls13_export_keying_material provides an exporter interface to use the
1291// |exporter_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001292bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
1293 Span<const uint8_t> secret,
1294 Span<const char> label,
1295 Span<const uint8_t> context);
Steven Valdez143e8b32016-07-11 13:19:03 -04001296
David Benjaminc11ea9422017-08-29 16:33:21 -04001297// tls13_finished_mac calculates the MAC of the handshake transcript to verify
1298// the integrity of the Finished message, and stores the result in |out| and
David Benjamin8525ff32018-09-05 18:44:15 -05001299// length in |out_len|. |is_server| is true if this is for the Server Finished
1300// and false for the Client Finished.
1301bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
1302 bool is_server);
Steven Valdez143e8b32016-07-11 13:19:03 -04001303
Steven Valdezcd8470f2017-10-11 12:29:36 -04001304// tls13_derive_session_psk calculates the PSK for this session based on the
1305// resumption master secret and |nonce|. It returns true on success, and false
1306// on failure.
Alessandro Ghedini2cc6f442018-12-11 11:35:17 +00001307bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001308
David Benjaminc11ea9422017-08-29 16:33:21 -04001309// tls13_write_psk_binder calculates the PSK binder value and replaces the last
David Benjamin8525ff32018-09-05 18:44:15 -05001310// bytes of |msg| with the resulting value. It returns true on success, and
1311// false on failure.
1312bool tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001313
David Benjamin8525ff32018-09-05 18:44:15 -05001314// tls13_verify_psk_binder verifies that the handshake transcript, truncated up
1315// to the binders has a valid signature using the value of |session|'s
1316// resumption secret. It returns true on success, and false on failure.
1317bool tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
1318 const SSLMessage &msg, CBS *binders);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001319
Steven Valdez143e8b32016-07-11 13:19:03 -04001320
David Benjaminc11ea9422017-08-29 16:33:21 -04001321// Handshake functions.
Steven Valdez143e8b32016-07-11 13:19:03 -04001322
1323enum ssl_hs_wait_t {
1324 ssl_hs_error,
1325 ssl_hs_ok,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001326 ssl_hs_read_server_hello,
Steven Valdez143e8b32016-07-11 13:19:03 -04001327 ssl_hs_read_message,
Steven Valdez143e8b32016-07-11 13:19:03 -04001328 ssl_hs_flush,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001329 ssl_hs_certificate_selection_pending,
Adam Langley3fe8fa72018-01-26 09:14:30 -08001330 ssl_hs_handoff,
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001331 ssl_hs_handback,
Steven Valdez143e8b32016-07-11 13:19:03 -04001332 ssl_hs_x509_lookup,
Nick Harper60a85cb2016-09-23 16:25:11 -07001333 ssl_hs_channel_id_lookup,
Steven Valdez143e8b32016-07-11 13:19:03 -04001334 ssl_hs_private_key_operation,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001335 ssl_hs_pending_session,
David Benjamin707af292017-03-10 17:47:18 -05001336 ssl_hs_pending_ticket,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001337 ssl_hs_early_return,
Steven Valdeze831a812017-03-09 14:56:07 -05001338 ssl_hs_early_data_rejected,
Steven Valdez2d850622017-01-11 11:34:52 -05001339 ssl_hs_read_end_of_early_data,
Steven Valdez520e1222017-06-13 12:45:25 -04001340 ssl_hs_read_change_cipher_spec,
David Benjamin3a1dd462017-07-11 16:13:10 -04001341 ssl_hs_certificate_verify,
Steven Valdez143e8b32016-07-11 13:19:03 -04001342};
1343
David Benjamina7bc9442018-01-18 10:08:53 -05001344enum ssl_grease_index_t {
1345 ssl_grease_cipher = 0,
1346 ssl_grease_group,
1347 ssl_grease_extension1,
1348 ssl_grease_extension2,
1349 ssl_grease_version,
1350 ssl_grease_ticket_extension,
1351 ssl_grease_last_index = ssl_grease_ticket_extension,
1352};
1353
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001354enum tls12_server_hs_state_t {
1355 state12_start_accept = 0,
1356 state12_read_client_hello,
1357 state12_select_certificate,
1358 state12_tls13,
1359 state12_select_parameters,
1360 state12_send_server_hello,
1361 state12_send_server_certificate,
1362 state12_send_server_key_exchange,
1363 state12_send_server_hello_done,
1364 state12_read_client_certificate,
1365 state12_verify_client_certificate,
1366 state12_read_client_key_exchange,
1367 state12_read_client_certificate_verify,
1368 state12_read_change_cipher_spec,
1369 state12_process_change_cipher_spec,
1370 state12_read_next_proto,
1371 state12_read_channel_id,
1372 state12_read_client_finished,
1373 state12_send_server_finished,
1374 state12_finish_server_handshake,
1375 state12_done,
1376};
1377
Matthew Braithwaite3e2b3ee2018-05-10 15:46:42 -07001378// handback_t lists the points in the state machine where a handback can occur.
1379// These are the different points at which key material is no longer needed.
1380enum handback_t {
1381 handback_after_session_resumption,
1382 handback_after_ecdhe,
1383 handback_after_handshake,
1384};
1385
Christopher Patton6c1b3762018-07-17 12:49:41 -07001386
1387// Delegated credentials.
1388
1389// This structure stores a delegated credential (DC) as defined by
Watson Ladd2f213f62019-02-12 16:59:54 -08001390// draft-ietf-tls-subcerts-03.
Christopher Patton6c1b3762018-07-17 12:49:41 -07001391struct DC {
1392 static constexpr bool kAllowUniquePtr = true;
1393 ~DC();
1394
1395 // Dup returns a copy of this DC and takes references to |raw| and |pkey|.
1396 UniquePtr<DC> Dup();
1397
1398 // Parse parses the delegated credential stored in |in|. If successful it
1399 // returns the parsed structure, otherwise it returns |nullptr| and sets
1400 // |*out_alert|.
1401 static UniquePtr<DC> Parse(CRYPTO_BUFFER *in, uint8_t *out_alert);
1402
1403 // raw is the delegated credential encoded as specified in draft-ietf-tls-
Christopher Pattonbe9953a2019-04-01 15:39:04 -04001404 // subcerts-03.
Christopher Patton6c1b3762018-07-17 12:49:41 -07001405 UniquePtr<CRYPTO_BUFFER> raw;
1406
1407 // expected_cert_verify_algorithm is the signature scheme of the DC public
1408 // key.
1409 uint16_t expected_cert_verify_algorithm = 0;
1410
Christopher Patton6c1b3762018-07-17 12:49:41 -07001411 // pkey is the public key parsed from |public_key|.
1412 UniquePtr<EVP_PKEY> pkey;
1413
1414 private:
1415 friend DC* New<DC>();
1416 DC();
1417};
1418
1419// ssl_signing_with_dc returns true if the peer has indicated support for
1420// delegated credentials and this host has sent a delegated credential in
1421// response. If this is true then we've committed to using the DC in the
1422// handshake.
1423bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs);
1424
1425
David Benjamin86e95b82017-07-18 16:34:25 -04001426struct SSL_HANDSHAKE {
David Benjamin8f288862017-07-20 14:01:44 -04001427 explicit SSL_HANDSHAKE(SSL *ssl);
1428 ~SSL_HANDSHAKE();
David Benjamin9a892502017-07-25 22:26:44 -04001429 static constexpr bool kAllowUniquePtr = true;
David Benjamin8f288862017-07-20 14:01:44 -04001430
David Benjaminc11ea9422017-08-29 16:33:21 -04001431 // ssl is a non-owning pointer to the parent |SSL| object.
David Benjamince8c9d22016-11-14 10:45:16 +09001432 SSL *ssl;
1433
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001434 // config is a non-owning pointer to the handshake configuration.
1435 SSL_CONFIG *config;
1436
David Benjaminc11ea9422017-08-29 16:33:21 -04001437 // wait contains the operation the handshake is currently blocking on or
1438 // |ssl_hs_ok| if none.
David Benjamin8f288862017-07-20 14:01:44 -04001439 enum ssl_hs_wait_t wait = ssl_hs_ok;
Adam Langley4ba6e192016-12-07 15:54:54 -08001440
David Benjaminc11ea9422017-08-29 16:33:21 -04001441 // state is the internal state for the TLS 1.2 and below handshake. Its
1442 // values depend on |do_handshake| but the starting state is always zero.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001443 int state = 0;
David Benjamincb0c29f2016-12-12 17:00:50 -05001444
David Benjaminc11ea9422017-08-29 16:33:21 -04001445 // tls13_state is the internal state for the TLS 1.3 handshake. Its values
1446 // depend on |do_handshake| but the starting state is always zero.
David Benjamin8f288862017-07-20 14:01:44 -04001447 int tls13_state = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04001448
David Benjaminc11ea9422017-08-29 16:33:21 -04001449 // min_version is the minimum accepted protocol version, taking account both
1450 // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001451 uint16_t min_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001452
David Benjaminc11ea9422017-08-29 16:33:21 -04001453 // max_version is the maximum accepted protocol version, taking account both
1454 // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001455 uint16_t max_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001456
David Benjamin8f288862017-07-20 14:01:44 -04001457 size_t hash_len = 0;
David Benjaminbc424022019-07-21 12:05:20 -04001458 uint8_t secret[SSL_MAX_MD_SIZE] = {0};
1459 uint8_t early_traffic_secret[SSL_MAX_MD_SIZE] = {0};
1460 uint8_t client_handshake_secret[SSL_MAX_MD_SIZE] = {0};
1461 uint8_t server_handshake_secret[SSL_MAX_MD_SIZE] = {0};
1462 uint8_t client_traffic_secret_0[SSL_MAX_MD_SIZE] = {0};
1463 uint8_t server_traffic_secret_0[SSL_MAX_MD_SIZE] = {0};
1464 uint8_t expected_client_finished[SSL_MAX_MD_SIZE] = {0};
Steven Valdez143e8b32016-07-11 13:19:03 -04001465
David Benjaminf5d2cd02016-10-06 19:39:20 -04001466 union {
David Benjaminc11ea9422017-08-29 16:33:21 -04001467 // sent is a bitset where the bits correspond to elements of kExtensions
1468 // in t1_lib.c. Each bit is set if that extension was sent in a
1469 // ClientHello. It's not used by servers.
David Benjamin8f288862017-07-20 14:01:44 -04001470 uint32_t sent = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04001471 // received is a bitset, like |sent|, but is used by servers to record
1472 // which extensions were received from a client.
David Benjaminf5d2cd02016-10-06 19:39:20 -04001473 uint32_t received;
1474 } extensions;
1475
David Benjaminc11ea9422017-08-29 16:33:21 -04001476 // retry_group is the group ID selected by the server in HelloRetryRequest in
1477 // TLS 1.3.
David Benjamin8f288862017-07-20 14:01:44 -04001478 uint16_t retry_group = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04001479
David Benjamine52f4c42017-10-01 22:35:10 -04001480 // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
1481 UniquePtr<ERR_SAVE_STATE> error;
1482
Adam Langley7b935932018-11-12 13:53:42 -08001483 // key_shares are the current key exchange instances. The second is only used
1484 // as a client if we believe that we should offer two key shares in a
1485 // ClientHello.
1486 UniquePtr<SSLKeyShare> key_shares[2];
Adam Langley4ba6e192016-12-07 15:54:54 -08001487
David Benjaminc11ea9422017-08-29 16:33:21 -04001488 // transcript is the current handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001489 SSLTranscript transcript;
Steven Valdez908ac192017-01-12 13:17:07 -05001490
David Benjaminc11ea9422017-08-29 16:33:21 -04001491 // cookie is the value of the cookie received from the server, if any.
David Benjamin08f5c762017-09-21 02:43:05 -04001492 Array<uint8_t> cookie;
David Benjamin3baa6e12016-10-07 21:10:38 -04001493
David Benjaminc11ea9422017-08-29 16:33:21 -04001494 // key_share_bytes is the value of the previously sent KeyShare extension by
1495 // the client in TLS 1.3.
David Benjamin08f5c762017-09-21 02:43:05 -04001496 Array<uint8_t> key_share_bytes;
David Benjamin4fe3c902016-08-16 02:17:03 -04001497
David Benjaminc11ea9422017-08-29 16:33:21 -04001498 // ecdh_public_key, for servers, is the key share to be sent to the client in
1499 // TLS 1.3.
David Benjamin879efc32017-09-21 11:20:53 -04001500 Array<uint8_t> ecdh_public_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04001501
David Benjaminc11ea9422017-08-29 16:33:21 -04001502 // peer_sigalgs are the signature algorithms that the peer supports. These are
1503 // taken from the contents of the signature algorithms extension for a server
1504 // or from the CertificateRequest for a client.
David Benjaminb1cf48e2017-09-21 11:37:46 -04001505 Array<uint16_t> peer_sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04001506
David Benjaminc11ea9422017-08-29 16:33:21 -04001507 // peer_supported_group_list contains the supported group IDs advertised by
1508 // the peer. This is only set on the server's end. The server does not
1509 // advertise this extension to the client.
David Benjamincf0ce672017-09-21 02:25:59 -04001510 Array<uint16_t> peer_supported_group_list;
David Benjamin43612b62016-10-07 00:41:50 -04001511
David Benjaminc11ea9422017-08-29 16:33:21 -04001512 // peer_key is the peer's ECDH key for a TLS 1.2 client.
David Benjamin499742c2017-07-22 12:45:38 -04001513 Array<uint8_t> peer_key;
David Benjamina4c8ff02016-10-08 02:49:01 -04001514
Nick Harper36fcc4c2017-09-21 15:02:22 -07001515 // negotiated_token_binding_version is used by a server to store the
1516 // on-the-wire encoding of the Token Binding protocol version to advertise in
1517 // the ServerHello/EncryptedExtensions if the Token Binding extension is to be
1518 // sent.
1519 uint16_t negotiated_token_binding_version;
1520
Adam Langleya307cb72018-05-02 09:06:48 -07001521 // cert_compression_alg_id, for a server, contains the negotiated certificate
1522 // compression algorithm for this client. It is only valid if
1523 // |cert_compression_negotiated| is true.
1524 uint16_t cert_compression_alg_id;
1525
David Benjaminc11ea9422017-08-29 16:33:21 -04001526 // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
1527 // parameters. It has client and server randoms prepended for signing
1528 // convenience.
David Benjamin879efc32017-09-21 11:20:53 -04001529 Array<uint8_t> server_params;
David Benjamina4c8ff02016-10-08 02:49:01 -04001530
David Benjaminc11ea9422017-08-29 16:33:21 -04001531 // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
1532 // server when using a TLS 1.2 PSK key exchange.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001533 UniquePtr<char> peer_psk_identity_hint;
Adam Langley4ba6e192016-12-07 15:54:54 -08001534
David Benjaminc11ea9422017-08-29 16:33:21 -04001535 // ca_names, on the client, contains the list of CAs received in a
1536 // CertificateRequest message.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001537 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
Adam Langley34b4c822017-02-02 10:57:17 -08001538
David Benjaminb9493552017-09-27 19:02:51 -04001539 // cached_x509_ca_names contains a cache of parsed versions of the elements of
1540 // |ca_names|. This pointer is left non-owning so only
1541 // |ssl_crypto_x509_method| needs to link against crypto/x509.
David Benjamin8f288862017-07-20 14:01:44 -04001542 STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
Adam Langley4ba6e192016-12-07 15:54:54 -08001543
David Benjaminc11ea9422017-08-29 16:33:21 -04001544 // certificate_types, on the client, contains the set of certificate types
1545 // received in a CertificateRequest message.
David Benjamin08f5c762017-09-21 02:43:05 -04001546 Array<uint8_t> certificate_types;
Adam Langley4ba6e192016-12-07 15:54:54 -08001547
David Benjaminc11ea9422017-08-29 16:33:21 -04001548 // local_pubkey is the public key we are authenticating as.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001549 UniquePtr<EVP_PKEY> local_pubkey;
David Benjamina232a712017-03-30 15:51:53 -05001550
David Benjaminc11ea9422017-08-29 16:33:21 -04001551 // peer_pubkey is the public key parsed from the peer's leaf certificate.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001552 UniquePtr<EVP_PKEY> peer_pubkey;
Adam Langleyd5157222016-12-12 11:37:43 -08001553
David Benjaminc11ea9422017-08-29 16:33:21 -04001554 // new_session is the new mutable session being established by the current
1555 // handshake. It should not be cached.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001556 UniquePtr<SSL_SESSION> new_session;
David Benjamin45738dd2017-02-09 20:01:26 -05001557
David Benjaminc11ea9422017-08-29 16:33:21 -04001558 // early_session is the session corresponding to the current 0-RTT state on
1559 // the client if |in_early_data| is true.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001560 UniquePtr<SSL_SESSION> early_session;
Steven Valdeze831a812017-03-09 14:56:07 -05001561
David Benjaminc11ea9422017-08-29 16:33:21 -04001562 // new_cipher is the cipher being negotiated in this handshake.
David Benjamin8f288862017-07-20 14:01:44 -04001563 const SSL_CIPHER *new_cipher = nullptr;
David Benjamin45738dd2017-02-09 20:01:26 -05001564
David Benjaminc11ea9422017-08-29 16:33:21 -04001565 // key_block is the record-layer key block for TLS 1.2 and earlier.
David Benjaminb9493552017-09-27 19:02:51 -04001566 Array<uint8_t> key_block;
Adam Langley4ba6e192016-12-07 15:54:54 -08001567
David Benjaminfd45ee72017-08-31 14:49:09 -04001568 // scts_requested is true if the SCT extension is in the ClientHello.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001569 bool scts_requested : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001570
David Benjaminfd45ee72017-08-31 14:49:09 -04001571 // needs_psk_binder is true if the ClientHello has a placeholder PSK binder to
1572 // be filled in.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001573 bool needs_psk_binder : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001574
Jesse Selover6b0d8222018-08-14 12:47:29 -04001575 bool received_hello_retry_request : 1;
1576 bool sent_hello_retry_request : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001577
Steven Valdezd8168742017-08-31 10:15:48 -04001578 // handshake_finalized is true once the handshake has completed, at which
1579 // point accessors should use the established state.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001580 bool handshake_finalized : 1;
Steven Valdezd8168742017-08-31 10:15:48 -04001581
David Benjaminc11ea9422017-08-29 16:33:21 -04001582 // accept_psk_mode stores whether the client's PSK mode is compatible with our
1583 // preferences.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001584 bool accept_psk_mode : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001585
David Benjaminfd45ee72017-08-31 14:49:09 -04001586 // cert_request is true if a client certificate was requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001587 bool cert_request : 1;
David Benjamina0486782016-10-06 19:11:32 -04001588
David Benjaminfd45ee72017-08-31 14:49:09 -04001589 // certificate_status_expected is true if OCSP stapling was negotiated and the
David Benjaminc11ea9422017-08-29 16:33:21 -04001590 // server is expected to send a CertificateStatus message. (This is used on
1591 // both the client and server sides.)
Jesse Selover6b0d8222018-08-14 12:47:29 -04001592 bool certificate_status_expected : 1;
David Benjamina0486782016-10-06 19:11:32 -04001593
David Benjaminfd45ee72017-08-31 14:49:09 -04001594 // ocsp_stapling_requested is true if a client requested OCSP stapling.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001595 bool ocsp_stapling_requested : 1;
David Benjamina0486782016-10-06 19:11:32 -04001596
Christopher Patton6c1b3762018-07-17 12:49:41 -07001597 // delegated_credential_requested is true if the peer indicated support for
1598 // the delegated credential extension.
1599 bool delegated_credential_requested : 1;
1600
David Benjaminc11ea9422017-08-29 16:33:21 -04001601 // should_ack_sni is used by a server and indicates that the SNI extension
1602 // should be echoed in the ServerHello.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001603 bool should_ack_sni : 1;
David Benjamina0486782016-10-06 19:11:32 -04001604
David Benjaminfd45ee72017-08-31 14:49:09 -04001605 // in_false_start is true if there is a pending client handshake in False
David Benjaminc11ea9422017-08-29 16:33:21 -04001606 // Start. The client may write data at this point.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001607 bool in_false_start : 1;
David Benjamina0486782016-10-06 19:11:32 -04001608
David Benjaminfd45ee72017-08-31 14:49:09 -04001609 // in_early_data is true if there is a pending handshake that has progressed
David Benjaminc11ea9422017-08-29 16:33:21 -04001610 // enough to send and receive early data.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001611 bool in_early_data : 1;
Steven Valdeze831a812017-03-09 14:56:07 -05001612
David Benjaminfd45ee72017-08-31 14:49:09 -04001613 // early_data_offered is true if the client sent the early_data extension.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001614 bool early_data_offered : 1;
Steven Valdez2d850622017-01-11 11:34:52 -05001615
David Benjaminfd45ee72017-08-31 14:49:09 -04001616 // can_early_read is true if application data may be read at this point in the
David Benjaminc11ea9422017-08-29 16:33:21 -04001617 // handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001618 bool can_early_read : 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05001619
David Benjaminfd45ee72017-08-31 14:49:09 -04001620 // can_early_write is true if application data may be written at this point in
David Benjaminc11ea9422017-08-29 16:33:21 -04001621 // the handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001622 bool can_early_write : 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05001623
David Benjaminc11ea9422017-08-29 16:33:21 -04001624 // next_proto_neg_seen is one of NPN was negotiated.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001625 bool next_proto_neg_seen : 1;
David Benjaminb74b0812016-10-06 19:43:48 -04001626
David Benjaminfd45ee72017-08-31 14:49:09 -04001627 // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
David Benjaminc11ea9422017-08-29 16:33:21 -04001628 // or received.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001629 bool ticket_expected : 1;
David Benjaminf04c2e92016-12-06 13:35:25 -05001630
David Benjaminfd45ee72017-08-31 14:49:09 -04001631 // extended_master_secret is true if the extended master secret extension is
David Benjaminc11ea9422017-08-29 16:33:21 -04001632 // negotiated in this handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001633 bool extended_master_secret : 1;
David Benjaminfc02b592017-02-17 16:26:01 -05001634
David Benjaminfd45ee72017-08-31 14:49:09 -04001635 // pending_private_key_op is true if there is a pending private key operation
David Benjaminc11ea9422017-08-29 16:33:21 -04001636 // in progress.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001637 bool pending_private_key_op : 1;
David Benjamin44148742017-06-17 13:20:59 -04001638
David Benjamina7bc9442018-01-18 10:08:53 -05001639 // grease_seeded is true if |grease_seed| has been initialized.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001640 bool grease_seeded : 1;
David Benjamina7bc9442018-01-18 10:08:53 -05001641
Adam Langleya307cb72018-05-02 09:06:48 -07001642 // handback indicates that a server should pause the handshake after
1643 // finishing operations that require private key material, in such a way that
1644 // |SSL_get_error| returns |SSL_HANDBACK|. It is set by |SSL_apply_handoff|.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001645 bool handback : 1;
Adam Langleya307cb72018-05-02 09:06:48 -07001646
1647 // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001648 bool cert_compression_negotiated : 1;
Adam Langleya307cb72018-05-02 09:06:48 -07001649
David Benjamin6965d252018-11-19 15:49:56 -06001650 // apply_jdk11_workaround is true if the peer is probably a JDK 11 client
1651 // which implemented TLS 1.3 incorrectly.
1652 bool apply_jdk11_workaround : 1;
1653
David Benjaminc11ea9422017-08-29 16:33:21 -04001654 // client_version is the value sent or received in the ClientHello version.
David Benjamin8f288862017-07-20 14:01:44 -04001655 uint16_t client_version = 0;
Alessandro Ghedinide254b42017-04-17 19:12:33 +01001656
David Benjaminc11ea9422017-08-29 16:33:21 -04001657 // early_data_read is the amount of early data that has been read by the
1658 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04001659 uint16_t early_data_read = 0;
Steven Valdeze831a812017-03-09 14:56:07 -05001660
David Benjaminc11ea9422017-08-29 16:33:21 -04001661 // early_data_written is the amount of early data that has been written by the
1662 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04001663 uint16_t early_data_written = 0;
Adam Langley2f9b47f2017-12-29 14:15:55 -08001664
Steven Valdezb84674b2018-08-28 10:14:07 -04001665 // session_id is the session ID in the ClientHello.
Adam Langley2f9b47f2017-12-29 14:15:55 -08001666 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
1667 uint8_t session_id_len = 0;
David Benjamina7bc9442018-01-18 10:08:53 -05001668
1669 // grease_seed is the entropy for GREASE values. It is valid if
1670 // |grease_seeded| is true.
1671 uint8_t grease_seed[ssl_grease_last_index + 1] = {0};
David Benjamin86e95b82017-07-18 16:34:25 -04001672};
Steven Valdez143e8b32016-07-11 13:19:03 -04001673
David Benjamin8e7bbba2017-10-13 17:18:35 -04001674UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -04001675
David Benjaminc11ea9422017-08-29 16:33:21 -04001676// ssl_check_message_type checks if |msg| has type |type|. If so it returns
1677// one. Otherwise, it sends an alert and returns zero.
David Benjamin97250f42017-10-07 04:12:35 -04001678bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
David Benjamin276b7e82017-01-21 14:13:39 -05001679
David Benjaminc11ea9422017-08-29 16:33:21 -04001680// ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
1681// on error. It sets |out_early_return| to one if we've completed the handshake
1682// early.
David Benjamin74795b32017-08-31 15:13:12 -04001683int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -04001684
David Benjaminc11ea9422017-08-29 16:33:21 -04001685// The following are implementations of |do_handshake| for the client and
1686// server.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001687enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
1688enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +09001689enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
1690enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001691
David Benjaminc11ea9422017-08-29 16:33:21 -04001692// The following functions return human-readable representations of the TLS
1693// handshake states for debugging.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001694const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
1695const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
David Benjaminf60bcfb2017-08-18 15:23:44 -04001696const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
1697const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
1698
Adam Langleyba9ad662018-12-17 13:59:38 -08001699// tls13_add_key_update queues a KeyUpdate message on |ssl|. The
1700// |update_requested| argument must be one of |SSL_KEY_UPDATE_REQUESTED| or
1701// |SSL_KEY_UPDATE_NOT_REQUESTED|.
1702bool tls13_add_key_update(SSL *ssl, int update_requested);
1703
David Benjamin12f58782018-08-28 17:06:31 -05001704// tls13_post_handshake processes a post-handshake message. It returns true on
1705// success and false on failure.
1706bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
Steven Valdez8e1c7be2016-07-26 12:39:22 -04001707
David Benjamin12f58782018-08-28 17:06:31 -05001708bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1709 bool allow_anonymous);
1710bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjamin794cc592017-03-25 22:24:23 -05001711
David Benjaminc11ea9422017-08-29 16:33:21 -04001712// tls13_process_finished processes |msg| as a Finished message from the
David Benjamin12f58782018-08-28 17:06:31 -05001713// peer. If |use_saved_value| is true, the verify_data is compared against
David Benjaminc11ea9422017-08-29 16:33:21 -04001714// |hs->expected_client_finished| rather than computed fresh.
David Benjamin12f58782018-08-28 17:06:31 -05001715bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1716 bool use_saved_value);
Steven Valdez143e8b32016-07-11 13:19:03 -04001717
David Benjamin12f58782018-08-28 17:06:31 -05001718bool tls13_add_certificate(SSL_HANDSHAKE *hs);
David Benjamin44148742017-06-17 13:20:59 -04001719
David Benjaminc11ea9422017-08-29 16:33:21 -04001720// tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
1721// handshake. If it returns |ssl_private_key_retry|, it should be called again
1722// to retry when the signing operation is completed.
David Benjamin44148742017-06-17 13:20:59 -04001723enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
1724
David Benjamin12f58782018-08-28 17:06:31 -05001725bool tls13_add_finished(SSL_HANDSHAKE *hs);
1726bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
Steven Valdez143e8b32016-07-11 13:19:03 -04001727
David Benjamin31640932017-10-11 13:22:39 -04001728bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
1729 Array<uint8_t> *out_secret,
1730 uint8_t *out_alert, CBS *contents);
1731bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
1732 Array<uint8_t> *out_secret,
1733 uint8_t *out_alert, CBS *contents);
1734bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez143e8b32016-07-11 13:19:03 -04001735
David Benjamin31640932017-10-11 13:22:39 -04001736bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1737 uint8_t *out_alert,
1738 CBS *contents);
1739bool ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001740 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001741 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents);
David Benjamin31640932017-10-11 13:22:39 -04001742bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001743
David Benjaminc11ea9422017-08-29 16:33:21 -04001744// ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
David Benjamin861abcc2018-07-14 17:40:26 -04001745// returns whether it's valid.
1746bool ssl_is_sct_list_valid(const CBS *contents);
Adam Langleycfa08c32016-11-17 13:21:27 -08001747
David Benjamin12f58782018-08-28 17:06:31 -05001748bool ssl_write_client_hello(SSL_HANDSHAKE *hs);
Steven Valdez5440fe02016-07-18 12:40:30 -04001749
Nick Harper60a85cb2016-09-23 16:25:11 -07001750enum ssl_cert_verify_context_t {
1751 ssl_cert_verify_server,
1752 ssl_cert_verify_client,
1753 ssl_cert_verify_channel_id,
1754};
1755
David Benjaminc11ea9422017-08-29 16:33:21 -04001756// tls13_get_cert_verify_signature_input generates the message to be signed for
1757// TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
David Benjamin75a1f232017-10-11 17:19:19 -04001758// type of signature. It sets |*out| to a newly allocated buffer containing the
1759// result. This function returns true on success and false on failure.
1760bool tls13_get_cert_verify_signature_input(
1761 SSL_HANDSHAKE *hs, Array<uint8_t> *out,
Nick Harper60a85cb2016-09-23 16:25:11 -07001762 enum ssl_cert_verify_context_t cert_verify_context);
1763
David Benjamindd6c2e82017-10-17 15:48:46 -04001764// ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001765// selection for |hs->ssl|'s client preferences.
1766bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
1767 Span<const uint8_t> protocol);
David Benjamindd6c2e82017-10-17 15:48:46 -04001768
David Benjaminc11ea9422017-08-29 16:33:21 -04001769// ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
David Benjamin31640932017-10-11 13:22:39 -04001770// true on successful negotiation or if nothing was negotiated. It returns false
David Benjaminc11ea9422017-08-29 16:33:21 -04001771// and sets |*out_alert| to an alert on error.
David Benjamin31640932017-10-11 13:22:39 -04001772bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1773 const SSL_CLIENT_HELLO *client_hello);
David Benjamin9ef31f02016-10-31 18:01:13 -04001774
David Benjamin86e95b82017-07-18 16:34:25 -04001775struct SSL_EXTENSION_TYPE {
David Benjaminffb11072016-11-13 10:32:10 +09001776 uint16_t type;
David Benjamin74795b32017-08-31 15:13:12 -04001777 bool *out_present;
David Benjaminffb11072016-11-13 10:32:10 +09001778 CBS *out_data;
David Benjamin86e95b82017-07-18 16:34:25 -04001779};
David Benjaminffb11072016-11-13 10:32:10 +09001780
David Benjaminc11ea9422017-08-29 16:33:21 -04001781// ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
1782// it. It writes the parsed extensions to pointers denoted by |ext_types|. On
1783// success, it fills in the |out_present| and |out_data| fields and returns one.
1784// Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
1785// extensions are rejected unless |ignore_unknown| is 1.
David Benjaminffb11072016-11-13 10:32:10 +09001786int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
1787 const SSL_EXTENSION_TYPE *ext_types,
Steven Valdez08b65f42016-12-07 15:29:45 -05001788 size_t num_ext_types, int ignore_unknown);
David Benjaminffb11072016-11-13 10:32:10 +09001789
David Benjaminc11ea9422017-08-29 16:33:21 -04001790// ssl_verify_peer_cert verifies the peer certificate for |hs|.
David Benjamin3a1dd462017-07-11 16:13:10 -04001791enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
Jesse Selover1c337e52018-08-10 13:28:47 -04001792// ssl_reverify_peer_cert verifies the peer certificate for |hs| when resuming a
1793// session.
1794enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs);
David Benjamin3a1dd462017-07-11 16:13:10 -04001795
David Benjamin00f48c82017-10-06 18:43:53 -04001796enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
1797bool ssl_send_finished(SSL_HANDSHAKE *hs);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001798bool ssl_output_cert_chain(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001799
David Benjaminc11ea9422017-08-29 16:33:21 -04001800// SSLKEYLOGFILE functions.
David Benjamine776cc22016-07-19 07:26:49 +02001801
David Benjaminc11ea9422017-08-29 16:33:21 -04001802// ssl_log_secret logs |secret| with label |label|, if logging is enabled for
1803// |ssl|. It returns one on success and zero on failure.
David Benjamine776cc22016-07-19 07:26:49 +02001804int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
1805 size_t secret_len);
1806
1807
David Benjaminc11ea9422017-08-29 16:33:21 -04001808// ClientHello functions.
David Benjamine14ff062016-08-09 16:21:24 -04001809
Adam Langleyc9827e02019-04-12 14:46:50 -07001810bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
David Benjamin861abcc2018-07-14 17:40:26 -04001811 const SSLMessage &msg);
David Benjamine14ff062016-08-09 16:21:24 -04001812
David Benjamin861abcc2018-07-14 17:40:26 -04001813bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
1814 CBS *out, uint16_t extension_type);
David Benjamine14ff062016-08-09 16:21:24 -04001815
David Benjamin861abcc2018-07-14 17:40:26 -04001816bool ssl_client_cipher_list_contains_cipher(
1817 const SSL_CLIENT_HELLO *client_hello, uint16_t id);
David Benjamine14ff062016-08-09 16:21:24 -04001818
1819
David Benjaminc11ea9422017-08-29 16:33:21 -04001820// GREASE.
David Benjamin65ac9972016-09-02 21:35:25 -04001821
David Benjamina7bc9442018-01-18 10:08:53 -05001822// ssl_get_grease_value returns a GREASE value for |hs|. For a given
David Benjaminc11ea9422017-08-29 16:33:21 -04001823// connection, the values for each index will be deterministic. This allows the
1824// same ClientHello be sent twice for a HelloRetryRequest or the same group be
1825// advertised in both supported_groups and key_shares.
David Benjamina7bc9442018-01-18 10:08:53 -05001826uint16_t ssl_get_grease_value(SSL_HANDSHAKE *hs, enum ssl_grease_index_t index);
David Benjamin65ac9972016-09-02 21:35:25 -04001827
1828
David Benjaminc11ea9422017-08-29 16:33:21 -04001829// Signature algorithms.
David Benjamin3ef76972016-10-17 17:59:54 -04001830
David Benjaminc11ea9422017-08-29 16:33:21 -04001831// tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
David Benjamin610cdbb2018-01-22 19:08:38 -05001832// algorithms and saves them on |hs|. It returns true on success and false on
David Benjaminc11ea9422017-08-29 16:33:21 -04001833// error.
David Benjamin610cdbb2018-01-22 19:08:38 -05001834bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
David Benjamin3ef76972016-10-17 17:59:54 -04001835
David Benjaminc11ea9422017-08-29 16:33:21 -04001836// tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
David Benjamin610cdbb2018-01-22 19:08:38 -05001837// that should be used with |pkey| in TLS 1.1 and earlier. It returns true on
1838// success and false if |pkey| may not be used at those versions.
1839bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
David Benjamina3651382017-04-20 17:49:36 -04001840
David Benjaminc11ea9422017-08-29 16:33:21 -04001841// tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
1842// with |hs|'s private key based on the peer's preferences and the algorithms
David Benjamin610cdbb2018-01-22 19:08:38 -05001843// supported. It returns true on success and false on error.
1844bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
David Benjamin3ef76972016-10-17 17:59:54 -04001845
Christopher Patton6c1b3762018-07-17 12:49:41 -07001846// tls1_get_peer_verify_algorithms returns the signature schemes for which the
1847// peer indicated support.
1848//
1849// NOTE: The related function |SSL_get0_peer_verify_algorithms| only has
1850// well-defined behavior during the callbacks set by |SSL_CTX_set_cert_cb| and
1851// |SSL_CTX_set_client_cert_cb|, or when the handshake is paused because of
1852// them.
1853Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs);
1854
David Benjaminc11ea9422017-08-29 16:33:21 -04001855// tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
David Benjamine28552d2018-04-08 13:59:25 -04001856// peer signature to |out|. It returns true on success and false on error. If
1857// |for_certs| is true, the potentially more restrictive list of algorithms for
1858// certificates is used. Otherwise, the online signature one is used.
1859bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out, bool for_certs);
David Benjamin3ef76972016-10-17 17:59:54 -04001860
David Benjaminc11ea9422017-08-29 16:33:21 -04001861// tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
David Benjamin610cdbb2018-01-22 19:08:38 -05001862// signature. It returns true on success and false on error, setting
1863// |*out_alert| to an alert to send.
1864bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
1865 uint16_t sigalg);
David Benjamin3ef76972016-10-17 17:59:54 -04001866
David Benjamine28552d2018-04-08 13:59:25 -04001867// tls12_has_different_verify_sigalgs_for_certs returns whether |ssl| has a
1868// different, more restrictive, list of signature algorithms acceptable for the
1869// certificate than the online signature.
1870bool tls12_has_different_verify_sigalgs_for_certs(const SSL *ssl);
1871
David Benjamin3ef76972016-10-17 17:59:54 -04001872
David Benjaminc11ea9422017-08-29 16:33:21 -04001873// Underdocumented functions.
1874//
1875// Functions below here haven't been touched up and may be underdocumented.
David Benjamin71f07942015-04-08 02:36:59 -04001876
Adam Langley1258b6a2014-06-20 12:00:00 -07001877#define TLSEXT_CHANNEL_ID_SIZE 128
Adam Langley95c29f32014-06-20 12:00:00 -07001878
David Benjaminc11ea9422017-08-29 16:33:21 -04001879// From RFC4492, used in encoding the curve type in ECParameters
Adam Langleyfcf25832014-12-18 17:42:32 -08001880#define NAMED_CURVE_TYPE 3
Adam Langley95c29f32014-06-20 12:00:00 -07001881
David Benjamin664e99a2017-10-10 19:23:21 -04001882struct CERT {
David Benjamine325c3f2018-04-12 16:11:15 -04001883 static constexpr bool kAllowUniquePtr = true;
1884
1885 explicit CERT(const SSL_X509_METHOD *x509_method);
1886 ~CERT();
1887
1888 UniquePtr<EVP_PKEY> privatekey;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001889
David Benjaminc11ea9422017-08-29 16:33:21 -04001890 // chain contains the certificate chain, with the leaf at the beginning. The
1891 // first element of |chain| may be NULL to indicate that the leaf certificate
1892 // has not yet been set.
1893 // If |chain| != NULL -> len(chain) >= 1
1894 // If |chain[0]| == NULL -> len(chain) >= 2.
1895 // |chain[1..]| != NULL
David Benjamine325c3f2018-04-12 16:11:15 -04001896 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001897
David Benjaminc11ea9422017-08-29 16:33:21 -04001898 // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
1899 // a cache in order to implement “get0” functions that return a non-owning
1900 // pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04001901 STACK_OF(X509) *x509_chain = nullptr;
David Benjaminf31e6812014-11-13 18:05:55 -05001902
David Benjaminc11ea9422017-08-29 16:33:21 -04001903 // x509_leaf may contain a parsed copy of the first element of |chain|. This
1904 // is only used as a cache in order to implement “get0” functions that return
1905 // a non-owning pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04001906 X509 *x509_leaf = nullptr;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001907
David Benjaminc11ea9422017-08-29 16:33:21 -04001908 // x509_stash contains the last |X509| object append to the chain. This is a
1909 // workaround for some third-party code that continue to use an |X509| object
1910 // even after passing ownership with an “add0” function.
David Benjamine325c3f2018-04-12 16:11:15 -04001911 X509 *x509_stash = nullptr;
Adam Langleye1e78132017-01-31 15:24:31 -08001912
David Benjaminc11ea9422017-08-29 16:33:21 -04001913 // key_method, if non-NULL, is a set of callbacks to call for private key
1914 // operations.
David Benjamine325c3f2018-04-12 16:11:15 -04001915 const SSL_PRIVATE_KEY_METHOD *key_method = nullptr;
David Benjaminb4d65fd2015-05-29 17:11:21 -04001916
David Benjaminc11ea9422017-08-29 16:33:21 -04001917 // x509_method contains pointers to functions that might deal with |X509|
1918 // compatibility, or might be a no-op, depending on the application.
David Benjamine325c3f2018-04-12 16:11:15 -04001919 const SSL_X509_METHOD *x509_method = nullptr;
Adam Langley3509dac2017-02-01 11:59:18 -08001920
David Benjamine325c3f2018-04-12 16:11:15 -04001921 // sigalgs, if non-empty, is the set of signature algorithms supported by
David Benjaminc11ea9422017-08-29 16:33:21 -04001922 // |privatekey| in decreasing order of preference.
David Benjamine325c3f2018-04-12 16:11:15 -04001923 Array<uint16_t> sigalgs;
Adam Langley95c29f32014-06-20 12:00:00 -07001924
David Benjaminc11ea9422017-08-29 16:33:21 -04001925 // Certificate setup callback: if set is called whenever a
1926 // certificate may be required (client or server). the callback
1927 // can then examine any appropriate parameters and setup any
1928 // certificates required. This allows advanced applications
1929 // to select certificates on the fly: for example based on
1930 // supported signature algorithms or curves.
David Benjamine325c3f2018-04-12 16:11:15 -04001931 int (*cert_cb)(SSL *ssl, void *arg) = nullptr;
1932 void *cert_cb_arg = nullptr;
Adam Langleyd323f4b2016-03-01 15:58:14 -08001933
David Benjaminc11ea9422017-08-29 16:33:21 -04001934 // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
1935 // store is used instead.
David Benjamine325c3f2018-04-12 16:11:15 -04001936 X509_STORE *verify_store = nullptr;
David Benjamin83a32122017-02-14 18:34:54 -05001937
David Benjaminc11ea9422017-08-29 16:33:21 -04001938 // Signed certificate timestamp list to be sent to the client, if requested
David Benjamine325c3f2018-04-12 16:11:15 -04001939 UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin83a32122017-02-14 18:34:54 -05001940
David Benjaminc11ea9422017-08-29 16:33:21 -04001941 // OCSP response to be sent to the client, if requested.
David Benjamine325c3f2018-04-12 16:11:15 -04001942 UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin5960a902017-02-14 20:07:11 -05001943
David Benjaminc11ea9422017-08-29 16:33:21 -04001944 // sid_ctx partitions the session space within a shared session cache or
1945 // ticket key. Only sessions with a matching value will be accepted.
David Benjamine325c3f2018-04-12 16:11:15 -04001946 uint8_t sid_ctx_length = 0;
1947 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
Christopher Patton6c1b3762018-07-17 12:49:41 -07001948
1949 // Delegated credentials.
1950
1951 // dc is the delegated credential to send to the peer (if requested).
1952 UniquePtr<DC> dc = nullptr;
1953
1954 // dc_privatekey is used instead of |privatekey| or |key_method| to
1955 // authenticate the host if a delegated credential is used in the handshake.
1956 UniquePtr<EVP_PKEY> dc_privatekey = nullptr;
1957
1958 // dc_key_method, if not NULL, is used instead of |dc_privatekey| to
1959 // authenticate the host.
1960 const SSL_PRIVATE_KEY_METHOD *dc_key_method = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04001961};
Adam Langley95c29f32014-06-20 12:00:00 -07001962
David Benjamin664e99a2017-10-10 19:23:21 -04001963// |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
1964struct SSL_PROTOCOL_METHOD {
David Benjamin97250f42017-10-07 04:12:35 -04001965 bool is_dtls;
1966 bool (*ssl_new)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04001967 void (*ssl_free)(SSL *ssl);
1968 // get_message sets |*out| to the current handshake message and returns true
1969 // if one has been received. It returns false if more input is needed.
Adam Langleyc9827e02019-04-12 14:46:50 -07001970 bool (*get_message)(const SSL *ssl, SSLMessage *out);
David Benjamin664e99a2017-10-10 19:23:21 -04001971 // next_message is called to release the current handshake message.
1972 void (*next_message)(SSL *ssl);
David Benjaminf6632da2017-10-12 19:11:47 -04001973 // Use the |ssl_open_handshake| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04001974 ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
1975 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04001976 // Use the |ssl_open_change_cipher_spec| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04001977 ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
1978 uint8_t *out_alert,
1979 Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04001980 // Use the |ssl_open_app_data| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04001981 ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
1982 size_t *out_consumed, uint8_t *out_alert,
1983 Span<uint8_t> in);
David Benjamin664e99a2017-10-10 19:23:21 -04001984 int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
1985 int len);
1986 int (*dispatch_alert)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04001987 // init_message begins a new handshake message of type |type|. |cbb| is the
1988 // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
David Benjamin97250f42017-10-07 04:12:35 -04001989 // the caller should write to. It returns true on success and false on error.
1990 bool (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
David Benjamin664e99a2017-10-10 19:23:21 -04001991 // finish_message finishes a handshake message. It sets |*out_msg| to the
David Benjamin97250f42017-10-07 04:12:35 -04001992 // serialized message. It returns true on success and false on error.
1993 bool (*finish_message)(SSL *ssl, CBB *cbb, bssl::Array<uint8_t> *out_msg);
1994 // add_message adds a handshake message to the pending flight. It returns
1995 // true on success and false on error.
1996 bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
David Benjamin664e99a2017-10-10 19:23:21 -04001997 // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
David Benjamin97250f42017-10-07 04:12:35 -04001998 // flight. It returns true on success and false on error.
1999 bool (*add_change_cipher_spec)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04002000 // flush_flight flushes the pending flight to the transport. It returns one on
2001 // success and <= 0 on error.
2002 int (*flush_flight)(SSL *ssl);
2003 // on_handshake_complete is called when the handshake is complete.
2004 void (*on_handshake_complete)(SSL *ssl);
2005 // set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It returns
David Benjamin97250f42017-10-07 04:12:35 -04002006 // true on success and false if changing the read state is forbidden at this
David Benjamin664e99a2017-10-10 19:23:21 -04002007 // point.
David Benjamin97250f42017-10-07 04:12:35 -04002008 bool (*set_read_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
David Benjamin664e99a2017-10-10 19:23:21 -04002009 // set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It returns
David Benjamin97250f42017-10-07 04:12:35 -04002010 // true on success and false if changing the write state is forbidden at this
David Benjamin664e99a2017-10-10 19:23:21 -04002011 // point.
David Benjamin97250f42017-10-07 04:12:35 -04002012 bool (*set_write_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
David Benjamin664e99a2017-10-10 19:23:21 -04002013};
2014
David Benjaminf6632da2017-10-12 19:11:47 -04002015// The following wrappers call |open_*| but handle |read_shutdown| correctly.
2016
2017// ssl_open_handshake processes a record from |in| for reading a handshake
2018// message.
2019ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
2020 uint8_t *out_alert, Span<uint8_t> in);
2021
2022// ssl_open_change_cipher_spec processes a record from |in| for reading a
2023// ChangeCipherSpec.
2024ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2025 uint8_t *out_alert,
2026 Span<uint8_t> in);
2027
2028// ssl_open_app_data processes a record from |in| for reading application data.
2029// On success, it returns |ssl_open_record_success| and sets |*out| to the
2030// input. If it encounters a post-handshake message, it returns
2031// |ssl_open_record_discard|. The caller should then retry, after processing any
2032// messages received with |get_message|.
2033ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
2034 size_t *out_consumed, uint8_t *out_alert,
2035 Span<uint8_t> in);
2036
David Benjamin0363de92018-06-29 13:57:38 -04002037struct SSL_X509_METHOD {
2038 // check_client_CA_list returns one if |names| is a good list of X.509
2039 // distinguished names and zero otherwise. This is used to ensure that we can
2040 // reject unparsable values at handshake time when using crypto/x509.
David Benjamin1e77ef42019-03-30 01:03:50 -05002041 bool (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
David Benjamin0363de92018-06-29 13:57:38 -04002042
2043 // cert_clear frees and NULLs all X509 certificate-related state.
2044 void (*cert_clear)(CERT *cert);
2045 // cert_free frees all X509-related state.
2046 void (*cert_free)(CERT *cert);
2047 // cert_flush_cached_chain drops any cached |X509|-based certificate chain
2048 // from |cert|.
2049 // cert_dup duplicates any needed fields from |cert| to |new_cert|.
2050 void (*cert_dup)(CERT *new_cert, const CERT *cert);
2051 void (*cert_flush_cached_chain)(CERT *cert);
2052 // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
2053 // from |cert|.
2054 void (*cert_flush_cached_leaf)(CERT *cert);
2055
2056 // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
2057 // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
David Benjamin1e77ef42019-03-30 01:03:50 -05002058 // true on success or false on error.
2059 bool (*session_cache_objects)(SSL_SESSION *session);
David Benjamin0363de92018-06-29 13:57:38 -04002060 // session_dup duplicates any needed fields from |session| to |new_session|.
David Benjamin1e77ef42019-03-30 01:03:50 -05002061 // It returns true on success or false on error.
2062 bool (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
David Benjamin0363de92018-06-29 13:57:38 -04002063 // session_clear frees any X509-related state from |session|.
2064 void (*session_clear)(SSL_SESSION *session);
2065 // session_verify_cert_chain verifies the certificate chain in |session|,
David Benjamin1e77ef42019-03-30 01:03:50 -05002066 // sets |session->verify_result| and returns true on success or false on
David Benjamin0363de92018-06-29 13:57:38 -04002067 // error.
David Benjamin1e77ef42019-03-30 01:03:50 -05002068 bool (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl,
2069 uint8_t *out_alert);
David Benjamin0363de92018-06-29 13:57:38 -04002070
2071 // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
2072 void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs);
Adam Langleyffe384c2019-05-01 11:13:12 -07002073 // ssl_new does any necessary initialisation of |hs|. It returns true on
David Benjamin1e77ef42019-03-30 01:03:50 -05002074 // success or false on error.
2075 bool (*ssl_new)(SSL_HANDSHAKE *hs);
David Benjamin0363de92018-06-29 13:57:38 -04002076 // ssl_free frees anything created by |ssl_new|.
2077 void (*ssl_config_free)(SSL_CONFIG *cfg);
2078 // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
2079 void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg);
2080 // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
2081 // necessary. On success, it updates |ssl|'s certificate configuration as
David Benjamin1e77ef42019-03-30 01:03:50 -05002082 // needed and returns true. Otherwise, it returns false.
2083 bool (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs);
Adam Langleyffe384c2019-05-01 11:13:12 -07002084 // ssl_ctx_new does any necessary initialisation of |ctx|. It returns true on
David Benjamin1e77ef42019-03-30 01:03:50 -05002085 // success or false on error.
2086 bool (*ssl_ctx_new)(SSL_CTX *ctx);
David Benjamin0363de92018-06-29 13:57:38 -04002087 // ssl_ctx_free frees anything created by |ssl_ctx_new|.
2088 void (*ssl_ctx_free)(SSL_CTX *ctx);
2089 // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
2090 void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
2091};
2092
David Benjaminc11ea9422017-08-29 16:33:21 -04002093// ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
2094// crypto/x509.
David Benjamin86e95b82017-07-18 16:34:25 -04002095extern const SSL_X509_METHOD ssl_crypto_x509_method;
Adam Langley3509dac2017-02-01 11:59:18 -08002096
David Benjaminc11ea9422017-08-29 16:33:21 -04002097// ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
2098// crypto/x509.
David Benjaminba2d3df2017-08-04 13:59:24 -04002099extern const SSL_X509_METHOD ssl_noop_x509_method;
2100
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002101struct TicketKey {
David Benjamin664e99a2017-10-10 19:23:21 -04002102 static constexpr bool kAllowUniquePtr = true;
2103
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002104 uint8_t name[SSL_TICKET_KEY_NAME_LEN] = {0};
2105 uint8_t hmac_key[16] = {0};
2106 uint8_t aes_key[16] = {0};
David Benjamin664e99a2017-10-10 19:23:21 -04002107 // next_rotation_tv_sec is the time (in seconds from the epoch) when the
2108 // current key should be superseded by a new key, or the time when a previous
2109 // key should be dropped. If zero, then the key should not be automatically
2110 // rotated.
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002111 uint64_t next_rotation_tv_sec = 0;
David Benjamin664e99a2017-10-10 19:23:21 -04002112};
2113
David Benjamin35b4a122018-07-14 17:04:41 -04002114struct CertCompressionAlg {
2115 static constexpr bool kAllowUniquePtr = true;
2116
2117 ssl_cert_compression_func_t compress = nullptr;
2118 ssl_cert_decompression_func_t decompress = nullptr;
2119 uint16_t alg_id = 0;
2120};
2121
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002122BSSL_NAMESPACE_END
David Benjamin4e840352017-10-11 18:41:11 -04002123
David Benjamin1eff9482018-09-24 14:10:29 -05002124DEFINE_LHASH_OF(SSL_SESSION)
David Benjamin4e840352017-10-11 18:41:11 -04002125
David Benjaminf109f202019-02-21 10:49:57 -06002126DEFINE_NAMED_STACK_OF(CertCompressionAlg, bssl::CertCompressionAlg)
Adam Langleya307cb72018-05-02 09:06:48 -07002127
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002128BSSL_NAMESPACE_BEGIN
David Benjamin4e840352017-10-11 18:41:11 -04002129
David Benjaminc11ea9422017-08-29 16:33:21 -04002130// An ssl_shutdown_t describes the shutdown state of one end of the connection,
2131// whether it is alive or has been shutdown via close_notify or fatal alert.
David Benjamine228bd22016-10-17 20:41:08 -04002132enum ssl_shutdown_t {
2133 ssl_shutdown_none = 0,
2134 ssl_shutdown_close_notify = 1,
David Benjamin31aad2d2017-10-06 16:04:06 -04002135 ssl_shutdown_error = 2,
David Benjamine228bd22016-10-17 20:41:08 -04002136};
2137
David Benjamin86e95b82017-07-18 16:34:25 -04002138struct SSL3_STATE {
David Benjamin94172572017-10-13 16:53:21 -04002139 static constexpr bool kAllowUniquePtr = true;
David Benjamine228bd22016-10-17 20:41:08 -04002140
David Benjamin94172572017-10-13 16:53:21 -04002141 SSL3_STATE();
2142 ~SSL3_STATE();
2143
2144 uint8_t read_sequence[8] = {0};
2145 uint8_t write_sequence[8] = {0};
2146
2147 uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
2148 uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
David Benjamine228bd22016-10-17 20:41:08 -04002149
David Benjaminc11ea9422017-08-29 16:33:21 -04002150 // read_buffer holds data from the transport to be processed.
David Benjaminea712e32017-10-13 16:50:39 -04002151 SSLBuffer read_buffer;
David Benjaminc11ea9422017-08-29 16:33:21 -04002152 // write_buffer holds data to be written to the transport.
David Benjaminea712e32017-10-13 16:50:39 -04002153 SSLBuffer write_buffer;
David Benjamine228bd22016-10-17 20:41:08 -04002154
David Benjamind9229f92017-10-06 17:36:20 -04002155 // pending_app_data is the unconsumed application data. It points into
2156 // |read_buffer|.
2157 Span<uint8_t> pending_app_data;
David Benjamine228bd22016-10-17 20:41:08 -04002158
David Benjaminc11ea9422017-08-29 16:33:21 -04002159 // partial write - check the numbers match
David Benjamin94172572017-10-13 16:53:21 -04002160 unsigned int wnum = 0; // number of bytes sent so far
2161 int wpend_tot = 0; // number bytes written
2162 int wpend_type = 0;
2163 int wpend_ret = 0; // number of bytes submitted
2164 const uint8_t *wpend_buf = nullptr;
David Benjamine228bd22016-10-17 20:41:08 -04002165
David Benjamin23c25d52017-10-06 16:02:47 -04002166 // read_shutdown is the shutdown state for the read half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002167 enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002168
David Benjamin23c25d52017-10-06 16:02:47 -04002169 // write_shutdown is the shutdown state for the write half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002170 enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002171
David Benjamin31aad2d2017-10-06 16:04:06 -04002172 // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
2173 // the receive half of the connection.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002174 UniquePtr<ERR_SAVE_STATE> read_error;
David Benjamin31aad2d2017-10-06 16:04:06 -04002175
David Benjamin94172572017-10-13 16:53:21 -04002176 int alert_dispatch = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002177
David Benjamin94172572017-10-13 16:53:21 -04002178 int total_renegotiations = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002179
David Benjamin32ce0ac2017-10-13 19:17:22 -04002180 // This holds a variable that indicates what we were doing when a 0 or -1 is
2181 // returned. This is needed for non-blocking IO so we know what request
2182 // needs re-doing when in SSL_accept or SSL_connect
2183 int rwstate = SSL_NOTHING;
2184
Steven Valdezc8e0f902018-07-14 11:23:01 -04002185 enum ssl_encryption_level_t read_level = ssl_encryption_initial;
2186 enum ssl_encryption_level_t write_level = ssl_encryption_initial;
2187
David Benjaminc11ea9422017-08-29 16:33:21 -04002188 // early_data_skipped is the amount of early data that has been skipped by the
2189 // record layer.
David Benjamin94172572017-10-13 16:53:21 -04002190 uint16_t early_data_skipped = 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002191
David Benjaminc11ea9422017-08-29 16:33:21 -04002192 // empty_record_count is the number of consecutive empty records received.
David Benjamin94172572017-10-13 16:53:21 -04002193 uint8_t empty_record_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002194
David Benjaminc11ea9422017-08-29 16:33:21 -04002195 // warning_alert_count is the number of consecutive warning alerts
2196 // received.
David Benjamin94172572017-10-13 16:53:21 -04002197 uint8_t warning_alert_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002198
David Benjaminc11ea9422017-08-29 16:33:21 -04002199 // key_update_count is the number of consecutive KeyUpdates received.
David Benjamin94172572017-10-13 16:53:21 -04002200 uint8_t key_update_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002201
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002202 // The negotiated Token Binding key parameter. Only valid if
2203 // |token_binding_negotiated| is set.
2204 uint8_t negotiated_token_binding_param = 0;
2205
David Benjaminc11ea9422017-08-29 16:33:21 -04002206 // skip_early_data instructs the record layer to skip unexpected early data
2207 // messages when 0RTT is rejected.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002208 bool skip_early_data : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002209
David Benjaminc11ea9422017-08-29 16:33:21 -04002210 // have_version is true if the connection's final version is known. Otherwise
2211 // the version has not been negotiated yet.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002212 bool have_version : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002213
David Benjaminc11ea9422017-08-29 16:33:21 -04002214 // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
2215 // and future messages should use the record layer.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002216 bool v2_hello_done : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002217
David Benjaminc11ea9422017-08-29 16:33:21 -04002218 // is_v2_hello is true if the current handshake message was derived from a
2219 // V2ClientHello rather than received from the peer directly.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002220 bool is_v2_hello : 1;
David Benjamin1a444da2017-01-21 14:27:45 -05002221
David Benjaminc11ea9422017-08-29 16:33:21 -04002222 // has_message is true if the current handshake message has been returned
2223 // at least once by |get_message| and false otherwise.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002224 bool has_message : 1;
David Benjamin7934f082017-08-01 16:32:25 -04002225
David Benjaminc11ea9422017-08-29 16:33:21 -04002226 // initial_handshake_complete is true if the initial handshake has
2227 // completed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002228 bool initial_handshake_complete : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002229
David Benjaminc11ea9422017-08-29 16:33:21 -04002230 // session_reused indicates whether a session was resumed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002231 bool session_reused : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002232
Watson Ladd629f3212019-06-11 17:46:52 -07002233 // delegated_credential_used is whether we presented a delegated credential to
2234 // the peer.
2235 bool delegated_credential_used : 1;
2236
Jesse Selover6b0d8222018-08-14 12:47:29 -04002237 bool send_connection_binding : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002238
David Benjaminc11ea9422017-08-29 16:33:21 -04002239 // In a client, this means that the server supported Channel ID and that a
2240 // Channel ID was sent. In a server it means that we echoed support for
David Benjamin46853762018-07-03 14:01:26 -04002241 // Channel IDs and that |channel_id| will be valid after the handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002242 bool channel_id_valid : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002243
David Benjamin046bc1f2017-08-31 15:06:42 -04002244 // key_update_pending is true if we have a KeyUpdate acknowledgment
David Benjaminc11ea9422017-08-29 16:33:21 -04002245 // outstanding.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002246 bool key_update_pending : 1;
David Benjaminbbba9392017-04-06 12:54:12 -04002247
David Benjamin046bc1f2017-08-31 15:06:42 -04002248 // wpend_pending is true if we have a pending write outstanding.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002249 bool wpend_pending : 1;
Steven Valdeze831a812017-03-09 14:56:07 -05002250
David Benjamin02e62562017-12-18 18:04:01 -05002251 // early_data_accepted is true if early data was accepted by the server.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002252 bool early_data_accepted : 1;
David Benjamin02e62562017-12-18 18:04:01 -05002253
Steven Valdezf1af1292018-08-13 10:54:48 -04002254 // tls13_downgrade is whether the TLS 1.3 anti-downgrade logic fired.
2255 bool tls13_downgrade : 1;
David Benjamin6df65402017-12-18 18:00:23 -05002256
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002257 // token_binding_negotiated is set if Token Binding was negotiated.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002258 bool token_binding_negotiated : 1;
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002259
Adam Langleya86c6982019-07-16 15:26:21 -07002260 // pq_experimental_signal_seen is true if the peer was observed
2261 // sending/echoing the post-quantum experiment signal.
2262 bool pq_experiment_signal_seen : 1;
2263
David Benjamin32ce0ac2017-10-13 19:17:22 -04002264 // hs_buf is the buffer of handshake data to process.
2265 UniquePtr<BUF_MEM> hs_buf;
2266
David Benjamin700631b2018-05-24 17:17:34 -04002267 // pending_hs_data contains the pending handshake data that has not yet
2268 // been encrypted to |pending_flight|. This allows packing the handshake into
2269 // fewer records.
2270 UniquePtr<BUF_MEM> pending_hs_data;
2271
David Benjaminc11ea9422017-08-29 16:33:21 -04002272 // pending_flight is the pending outgoing flight. This is used to flush each
2273 // handshake flight in a single write. |write_buffer| must be written out
2274 // before this data.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002275 UniquePtr<BUF_MEM> pending_flight;
David Benjamindaf207a2017-01-03 18:37:41 -05002276
David Benjaminc11ea9422017-08-29 16:33:21 -04002277 // pending_flight_offset is the number of bytes of |pending_flight| which have
2278 // been successfully written.
David Benjamin94172572017-10-13 16:53:21 -04002279 uint32_t pending_flight_offset = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002280
Adam Langley2f9b47f2017-12-29 14:15:55 -08002281 // ticket_age_skew is the difference, in seconds, between the client-sent
2282 // ticket age and the server-computed value in TLS 1.3 server connections
2283 // which resumed a session.
2284 int32_t ticket_age_skew = 0;
2285
David Benjamin64770122019-05-04 11:00:04 -05002286 // ssl_early_data_reason stores details on why 0-RTT was accepted or rejected.
2287 enum ssl_early_data_reason_t early_data_reason = ssl_early_data_unknown;
2288
David Benjaminc11ea9422017-08-29 16:33:21 -04002289 // aead_read_ctx is the current read cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002290 UniquePtr<SSLAEADContext> aead_read_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002291
David Benjaminc11ea9422017-08-29 16:33:21 -04002292 // aead_write_ctx is the current write cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002293 UniquePtr<SSLAEADContext> aead_write_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002294
David Benjaminc11ea9422017-08-29 16:33:21 -04002295 // hs is the handshake state for the current handshake or NULL if there isn't
2296 // one.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002297 UniquePtr<SSL_HANDSHAKE> hs;
David Benjamine228bd22016-10-17 20:41:08 -04002298
David Benjaminbc424022019-07-21 12:05:20 -04002299 uint8_t write_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2300 uint8_t read_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2301 uint8_t exporter_secret[SSL_MAX_MD_SIZE] = {0};
David Benjamin94172572017-10-13 16:53:21 -04002302 uint8_t write_traffic_secret_len = 0;
2303 uint8_t read_traffic_secret_len = 0;
2304 uint8_t exporter_secret_len = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002305
David Benjaminc11ea9422017-08-29 16:33:21 -04002306 // Connection binding to prevent renegotiation attacks
David Benjamin94172572017-10-13 16:53:21 -04002307 uint8_t previous_client_finished[12] = {0};
2308 uint8_t previous_client_finished_len = 0;
2309 uint8_t previous_server_finished_len = 0;
2310 uint8_t previous_server_finished[12] = {0};
Adam Langley4ba6e192016-12-07 15:54:54 -08002311
Adam Langley2f9b47f2017-12-29 14:15:55 -08002312 uint8_t send_alert[2] = {0};
2313
David Benjaminc11ea9422017-08-29 16:33:21 -04002314 // established_session is the session established by the connection. This
2315 // session is only filled upon the completion of the handshake and is
2316 // immutable.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002317 UniquePtr<SSL_SESSION> established_session;
David Benjamine228bd22016-10-17 20:41:08 -04002318
David Benjaminc11ea9422017-08-29 16:33:21 -04002319 // Next protocol negotiation. For the client, this is the protocol that we
2320 // sent in NextProtocol and is set when handling ServerHello extensions.
2321 //
2322 // For a server, this is the client's selected_protocol from NextProtocol and
2323 // is set when handling the NextProtocol message, before the Finished
2324 // message.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002325 Array<uint8_t> next_proto_negotiated;
David Benjamine228bd22016-10-17 20:41:08 -04002326
David Benjaminc11ea9422017-08-29 16:33:21 -04002327 // ALPN information
2328 // (we are in the process of transitioning from NPN to ALPN.)
David Benjamine228bd22016-10-17 20:41:08 -04002329
David Benjaminc11ea9422017-08-29 16:33:21 -04002330 // In a server these point to the selected ALPN protocol after the
2331 // ClientHello has been processed. In a client these contain the protocol
2332 // that the server selected once the ServerHello has been processed.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002333 Array<uint8_t> alpn_selected;
David Benjamine228bd22016-10-17 20:41:08 -04002334
David Benjamina8614602017-09-06 15:40:19 -04002335 // hostname, on the server, is the value of the SNI extension.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002336 UniquePtr<char> hostname;
David Benjamina8614602017-09-06 15:40:19 -04002337
David Benjaminc11ea9422017-08-29 16:33:21 -04002338 // For a server:
David Benjamin46853762018-07-03 14:01:26 -04002339 // If |channel_id_valid| is true, then this contains the
David Benjaminc11ea9422017-08-29 16:33:21 -04002340 // verified Channel ID from the client: a P256 point, (x,y), where
2341 // each are big-endian values.
David Benjamin46853762018-07-03 14:01:26 -04002342 uint8_t channel_id[64] = {0};
Nick Harper3c034b22017-12-22 15:50:43 -08002343
2344 // Contains the QUIC transport params received by the peer.
2345 Array<uint8_t> peer_quic_transport_params;
David Benjaminfceca8e2018-04-12 16:37:19 -04002346
2347 // srtp_profile is the selected SRTP protection profile for
2348 // DTLS-SRTP.
2349 const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002350};
David Benjamine228bd22016-10-17 20:41:08 -04002351
David Benjaminc11ea9422017-08-29 16:33:21 -04002352// lengths of messages
David Benjamin593047f2015-05-08 13:08:52 -04002353#define DTLS1_COOKIE_LENGTH 256
2354
2355#define DTLS1_RT_HEADER_LENGTH 13
2356
2357#define DTLS1_HM_HEADER_LENGTH 12
2358
2359#define DTLS1_CCS_HEADER_LENGTH 1
2360
2361#define DTLS1_AL_HEADER_LENGTH 2
2362
David Benjamin593047f2015-05-08 13:08:52 -04002363struct hm_header_st {
2364 uint8_t type;
2365 uint32_t msg_len;
2366 uint16_t seq;
2367 uint32_t frag_off;
2368 uint32_t frag_len;
David Benjamin593047f2015-05-08 13:08:52 -04002369};
2370
David Benjaminc11ea9422017-08-29 16:33:21 -04002371// An hm_fragment is an incoming DTLS message, possibly not yet assembled.
David Benjamin86e95b82017-07-18 16:34:25 -04002372struct hm_fragment {
David Benjamin049fdfc2017-10-17 01:12:53 -04002373 static constexpr bool kAllowUniquePtr = true;
2374
2375 hm_fragment() {}
2376 hm_fragment(const hm_fragment &) = delete;
2377 hm_fragment &operator=(const hm_fragment &) = delete;
2378
2379 ~hm_fragment();
2380
David Benjaminc11ea9422017-08-29 16:33:21 -04002381 // type is the type of the message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002382 uint8_t type = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002383 // seq is the sequence number of this message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002384 uint16_t seq = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002385 // msg_len is the length of the message body.
David Benjamin049fdfc2017-10-17 01:12:53 -04002386 uint32_t msg_len = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002387 // data is a pointer to the message, including message header. It has length
2388 // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
David Benjamin049fdfc2017-10-17 01:12:53 -04002389 uint8_t *data = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04002390 // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
2391 // the message have been received. It is NULL if the message is complete.
David Benjamin049fdfc2017-10-17 01:12:53 -04002392 uint8_t *reassembly = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002393};
David Benjamin593047f2015-05-08 13:08:52 -04002394
David Benjaminad8f5e12017-02-20 17:00:20 -05002395struct OPENSSL_timeval {
2396 uint64_t tv_sec;
2397 uint32_t tv_usec;
2398};
2399
David Benjamin86e95b82017-07-18 16:34:25 -04002400struct DTLS1_STATE {
David Benjamin800046f2017-10-17 01:37:51 -04002401 static constexpr bool kAllowUniquePtr = true;
2402
2403 DTLS1_STATE();
2404 ~DTLS1_STATE();
2405
David Benjaminc11ea9422017-08-29 16:33:21 -04002406 // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
2407 // the peer in this epoch.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002408 bool has_change_cipher_spec : 1;
David Benjamin593047f2015-05-08 13:08:52 -04002409
David Benjaminc11ea9422017-08-29 16:33:21 -04002410 // outgoing_messages_complete is true if |outgoing_messages| has been
2411 // completed by an attempt to flush it. Future calls to |add_message| and
2412 // |add_change_cipher_spec| will start a new flight.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002413 bool outgoing_messages_complete : 1;
David Benjamin9bbdf582017-08-02 19:46:29 -04002414
David Benjaminc11ea9422017-08-29 16:33:21 -04002415 // flight_has_reply is true if the current outgoing flight is complete and has
2416 // processed at least one message. This is used to detect whether we or the
2417 // peer sent the final flight.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002418 bool flight_has_reply : 1;
David Benjamin302b8182017-08-22 14:47:22 -07002419
David Benjamin800046f2017-10-17 01:37:51 -04002420 uint8_t cookie[DTLS1_COOKIE_LENGTH] = {0};
2421 size_t cookie_len = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002422
David Benjaminc11ea9422017-08-29 16:33:21 -04002423 // The current data and handshake epoch. This is initially undefined, and
2424 // starts at zero once the initial handshake is completed.
David Benjamin800046f2017-10-17 01:37:51 -04002425 uint16_t r_epoch = 0;
2426 uint16_t w_epoch = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002427
David Benjaminc11ea9422017-08-29 16:33:21 -04002428 // records being received in the current epoch
David Benjamin593047f2015-05-08 13:08:52 -04002429 DTLS1_BITMAP bitmap;
2430
David Benjamin800046f2017-10-17 01:37:51 -04002431 uint16_t handshake_write_seq = 0;
2432 uint16_t handshake_read_seq = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002433
David Benjaminc11ea9422017-08-29 16:33:21 -04002434 // save last sequence number for retransmissions
David Benjamin800046f2017-10-17 01:37:51 -04002435 uint8_t last_write_sequence[8] = {0};
2436 UniquePtr<SSLAEADContext> last_aead_write_ctx;
David Benjamin593047f2015-05-08 13:08:52 -04002437
David Benjaminc11ea9422017-08-29 16:33:21 -04002438 // incoming_messages is a ring buffer of incoming handshake messages that have
2439 // yet to be processed. The front of the ring buffer is message number
2440 // |handshake_read_seq|, at position |handshake_read_seq| %
2441 // |SSL_MAX_HANDSHAKE_FLIGHT|.
David Benjamin800046f2017-10-17 01:37:51 -04002442 UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin593047f2015-05-08 13:08:52 -04002443
David Benjaminc11ea9422017-08-29 16:33:21 -04002444 // outgoing_messages is the queue of outgoing messages from the last handshake
2445 // flight.
David Benjamin29a83c52016-06-17 19:12:54 -04002446 DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin800046f2017-10-17 01:37:51 -04002447 uint8_t outgoing_messages_len = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002448
David Benjaminc11ea9422017-08-29 16:33:21 -04002449 // outgoing_written is the number of outgoing messages that have been
2450 // written.
David Benjamin800046f2017-10-17 01:37:51 -04002451 uint8_t outgoing_written = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002452 // outgoing_offset is the number of bytes of the next outgoing message have
2453 // been written.
David Benjamin800046f2017-10-17 01:37:51 -04002454 uint32_t outgoing_offset = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -05002455
David Benjamin800046f2017-10-17 01:37:51 -04002456 unsigned mtu = 0; // max DTLS packet size
David Benjamin593047f2015-05-08 13:08:52 -04002457
David Benjaminc11ea9422017-08-29 16:33:21 -04002458 // num_timeouts is the number of times the retransmit timer has fired since
2459 // the last time it was reset.
David Benjamin800046f2017-10-17 01:37:51 -04002460 unsigned num_timeouts = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002461
David Benjaminc11ea9422017-08-29 16:33:21 -04002462 // Indicates when the last handshake msg or heartbeat sent will
2463 // timeout.
David Benjamin800046f2017-10-17 01:37:51 -04002464 struct OPENSSL_timeval next_timeout = {0, 0};
David Benjamin593047f2015-05-08 13:08:52 -04002465
David Benjaminc11ea9422017-08-29 16:33:21 -04002466 // timeout_duration_ms is the timeout duration in milliseconds.
David Benjamin800046f2017-10-17 01:37:51 -04002467 unsigned timeout_duration_ms = 0;
David Benjamin86e95b82017-07-18 16:34:25 -04002468};
David Benjamin593047f2015-05-08 13:08:52 -04002469
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002470// SSL_CONFIG contains configuration bits that can be shed after the handshake
2471// completes. Objects of this type are not shared; they are unique to a
2472// particular |SSL|.
2473//
2474// See SSL_shed_handshake_config() for more about the conditions under which
2475// configuration can be shed.
2476struct SSL_CONFIG {
David Benjamin50596f82018-07-02 19:47:27 -04002477 static constexpr bool kAllowUniquePtr = true;
2478
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002479 explicit SSL_CONFIG(SSL *ssl_arg);
2480 ~SSL_CONFIG();
David Benjamin3f2611a2017-02-06 13:38:26 -05002481
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002482 // ssl is a non-owning pointer to the parent |SSL| object.
2483 SSL *const ssl = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002484
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -07002485 // conf_max_version is the maximum acceptable version configured by
2486 // |SSL_set_max_proto_version|. Note this version is not normalized in DTLS
2487 // and is further constrained by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002488 uint16_t conf_max_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002489
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -07002490 // conf_min_version is the minimum acceptable version configured by
2491 // |SSL_set_min_proto_version|. Note this version is not normalized in DTLS
2492 // and is further constrained by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002493 uint16_t conf_min_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002494
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002495 X509_VERIFY_PARAM *param = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002496
David Benjaminc11ea9422017-08-29 16:33:21 -04002497 // crypto
David Benjamin0ce090a2018-07-02 20:24:40 -04002498 UniquePtr<SSLCipherPreferenceList> cipher_list;
David Benjamin3f2611a2017-02-06 13:38:26 -05002499
Adam Langley3fe8fa72018-01-26 09:14:30 -08002500 // This is used to hold the local certificate used (i.e. the server
2501 // certificate for a server or the client certificate for a client).
David Benjamin0ce090a2018-07-02 20:24:40 -04002502 UniquePtr<CERT> cert;
David Benjamin3f2611a2017-02-06 13:38:26 -05002503
2504 int (*verify_callback)(int ok,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002505 X509_STORE_CTX *ctx) =
2506 nullptr; // fail if callback returns 0
David Benjamin3f2611a2017-02-06 13:38:26 -05002507
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002508 enum ssl_verify_result_t (*custom_verify_callback)(
2509 SSL *ssl, uint8_t *out_alert) = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04002510 // Server-only: psk_identity_hint is the identity hint to send in
2511 // PSK-based key exchanges.
David Benjamin0ce090a2018-07-02 20:24:40 -04002512 UniquePtr<char> psk_identity_hint;
David Benjamin3f2611a2017-02-06 13:38:26 -05002513
David Benjamin0ce090a2018-07-02 20:24:40 -04002514 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
2515 unsigned max_identity_len, uint8_t *psk,
2516 unsigned max_psk_len) = nullptr;
2517 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
2518 unsigned max_psk_len) = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002519
David Benjaminc11ea9422017-08-29 16:33:21 -04002520 // for server side, keep the list of CA_dn we can use
David Benjamin0ce090a2018-07-02 20:24:40 -04002521 UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
Adam Langley34b4c822017-02-02 10:57:17 -08002522
David Benjaminc11ea9422017-08-29 16:33:21 -04002523 // cached_x509_client_CA is a cache of parsed versions of the elements of
2524 // |client_CA|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002525 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002526
David Benjamin0ce090a2018-07-02 20:24:40 -04002527 Array<uint16_t> supported_group_list; // our list
David Benjamin3f2611a2017-02-06 13:38:26 -05002528
David Benjaminc11ea9422017-08-29 16:33:21 -04002529 // The client's Channel ID private key.
David Benjamin46853762018-07-03 14:01:26 -04002530 UniquePtr<EVP_PKEY> channel_id_private;
David Benjamin3f2611a2017-02-06 13:38:26 -05002531
David Benjaminc11ea9422017-08-29 16:33:21 -04002532 // For a client, this contains the list of supported protocols in wire
2533 // format.
David Benjamin0ce090a2018-07-02 20:24:40 -04002534 Array<uint8_t> alpn_client_proto_list;
David Benjamin3f2611a2017-02-06 13:38:26 -05002535
Nick Harper36fcc4c2017-09-21 15:02:22 -07002536 // Contains a list of supported Token Binding key parameters.
David Benjamin0ce090a2018-07-02 20:24:40 -04002537 Array<uint8_t> token_binding_params;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002538
Nick Harper3c034b22017-12-22 15:50:43 -08002539 // Contains the QUIC transport params that this endpoint will send.
David Benjamin0ce090a2018-07-02 20:24:40 -04002540 Array<uint8_t> quic_transport_params;
David Benjamin3f2611a2017-02-06 13:38:26 -05002541
Adam Langley826ce152018-08-03 10:31:21 -07002542 // verify_sigalgs, if not empty, is the set of signature algorithms
2543 // accepted from the peer in decreasing order of preference.
2544 Array<uint16_t> verify_sigalgs;
2545
David Benjaminb95d4b42018-05-02 16:11:34 -04002546 // srtp_profiles is the list of configured SRTP protection profiles for
2547 // DTLS-SRTP.
David Benjamin0ce090a2018-07-02 20:24:40 -04002548 UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
David Benjaminb95d4b42018-05-02 16:11:34 -04002549
David Benjaminc11ea9422017-08-29 16:33:21 -04002550 // verify_mode is a bitmask of |SSL_VERIFY_*| values.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002551 uint8_t verify_mode = SSL_VERIFY_NONE;
David Benjamin3f2611a2017-02-06 13:38:26 -05002552
David Benjaminc11ea9422017-08-29 16:33:21 -04002553 // Enable signed certificate time stamps. Currently client only.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002554 bool signed_cert_timestamps_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002555
David Benjaminc11ea9422017-08-29 16:33:21 -04002556 // ocsp_stapling_enabled is only used by client connections and indicates
2557 // whether OCSP stapling will be requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002558 bool ocsp_stapling_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002559
David Benjamin46853762018-07-03 14:01:26 -04002560 // channel_id_enabled is copied from the |SSL_CTX|. For a server, means that
2561 // we'll accept Channel IDs from clients. For a client, means that we'll
2562 // advertise support.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002563 bool channel_id_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002564
Jesse Seloverd7266ec2019-01-30 16:06:10 -05002565 // If enforce_rsa_key_usage is true, the handshake will fail if the
2566 // keyUsage extension is present and incompatible with the TLS usage.
2567 // This field is not read until after certificate verification.
2568 bool enforce_rsa_key_usage : 1;
2569
David Benjaminc11ea9422017-08-29 16:33:21 -04002570 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
2571 // hash of the peer's certificate and then discard it to save memory and
2572 // session space. Only effective on the server side.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002573 bool retain_only_sha256_of_client_certs : 1;
Adam Langley3fe8fa72018-01-26 09:14:30 -08002574
2575 // handoff indicates that a server should stop after receiving the
2576 // ClientHello and pause the handshake in such a way that |SSL_get_error|
2577 // returns |SSL_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX|
2578 // element of the same name and may be cleared if the handoff is declined.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002579 bool handoff : 1;
Adam Langley8df8e642018-02-28 13:35:39 -08002580
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002581 // shed_handshake_config indicates that the handshake config (this object!)
2582 // should be freed after the handshake completes.
2583 bool shed_handshake_config : 1;
David Benjamin2d98d492018-10-08 17:31:17 -05002584
2585 // ignore_tls13_downgrade is whether the connection should continue when the
2586 // server random signals a downgrade.
David Benjamin6965d252018-11-19 15:49:56 -06002587 bool ignore_tls13_downgrade : 1;
2588
2589 // jdk11_workaround is whether to disable TLS 1.3 for JDK 11 clients, as a
2590 // workaround for https://bugs.openjdk.java.net/browse/JDK-8211806.
2591 bool jdk11_workaround : 1;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002592};
2593
David Benjamina130ce02018-08-14 22:26:39 -05002594// From RFC 8446, used in determining PSK modes.
Steven Valdeza833c352016-11-01 13:39:36 -04002595#define SSL_PSK_DHE_KE 0x1
Steven Valdez1e6f11a2016-07-27 11:10:52 -04002596
David Benjaminc11ea9422017-08-29 16:33:21 -04002597// kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
2598// data that will be accepted. This value should be slightly below
2599// kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
Alessandro Ghedinide254b42017-04-17 19:12:33 +01002600static const size_t kMaxEarlyDataAccepted = 14336;
2601
David Benjamine325c3f2018-04-12 16:11:15 -04002602UniquePtr<CERT> ssl_cert_dup(CERT *cert);
David Benjamin27e377e2017-07-31 19:09:42 -04002603void ssl_cert_clear_certs(CERT *cert);
David Benjamin8525ff32018-09-05 18:44:15 -05002604bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
2605bool ssl_is_key_type_supported(int key_type);
2606// ssl_compare_public_and_private_key returns true if |pubkey| is the public
2607// counterpart to |privkey|. Otherwise it returns false and pushes a helpful
David Benjaminc11ea9422017-08-29 16:33:21 -04002608// message on the error queue.
David Benjamin8525ff32018-09-05 18:44:15 -05002609bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
Adam Langleyc26692c2017-01-25 09:34:42 -08002610 const EVP_PKEY *privkey);
David Benjamin8525ff32018-09-05 18:44:15 -05002611bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey);
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002612int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002613int ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, const SSL_SESSION *session);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002614int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002615
David Benjaminc11ea9422017-08-29 16:33:21 -04002616// ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
2617// error.
David Benjamin31b0c9b2017-07-20 14:49:15 -04002618UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
Adam Langley46db7af2017-02-01 15:49:37 -08002619
David Benjamin58150ed2018-06-29 23:58:43 -04002620// ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table
2621// keyed on session IDs.
2622uint32_t ssl_hash_session_id(Span<const uint8_t> session_id);
2623
David Benjaminc11ea9422017-08-29 16:33:21 -04002624// SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
2625// the parsed data.
Matthew Braithwaite6d597a32018-07-11 14:58:22 -07002626OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_parse(
2627 CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool);
Adam Langley46db7af2017-02-01 15:49:37 -08002628
Adam Langleye8d24392018-01-25 15:49:37 -08002629// ssl_session_serialize writes |in| to |cbb| as if it were serialising a
2630// session for Session-ID resumption. It returns one on success and zero on
2631// error.
Matthew Braithwaite6d597a32018-07-11 14:58:22 -07002632OPENSSL_EXPORT int ssl_session_serialize(const SSL_SESSION *in, CBB *cbb);
Adam Langleye8d24392018-01-25 15:49:37 -08002633
David Benjaminc11ea9422017-08-29 16:33:21 -04002634// ssl_session_is_context_valid returns one if |session|'s session ID context
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002635// matches the one set on |hs| and zero otherwise.
2636int ssl_session_is_context_valid(const SSL_HANDSHAKE *hs,
2637 const SSL_SESSION *session);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002638
David Benjaminc11ea9422017-08-29 16:33:21 -04002639// ssl_session_is_time_valid returns one if |session| is still valid and zero if
2640// it has expired.
Steven Valdez4aa154e2016-07-29 14:32:55 -04002641int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
2642
David Benjaminc11ea9422017-08-29 16:33:21 -04002643// ssl_session_is_resumable returns one if |session| is resumable for |hs| and
2644// zero otherwise.
David Benjamin45738dd2017-02-09 20:01:26 -05002645int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
2646 const SSL_SESSION *session);
David Benjamin75f99142016-11-12 12:36:06 +09002647
David Benjamina4bafd32017-10-03 15:06:29 -04002648// ssl_session_protocol_version returns the protocol version associated with
2649// |session|. Note that despite the name, this is not the same as
2650// |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
2651uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
Steven Valdez8f36c512017-06-20 10:55:02 -04002652
David Benjamina4bafd32017-10-03 15:06:29 -04002653// ssl_session_get_digest returns the digest used in |session|.
2654const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
Steven Valdez908ac192017-01-12 13:17:07 -05002655
David Benjamin4d0be242016-09-01 01:10:07 -04002656void ssl_set_session(SSL *ssl, SSL_SESSION *session);
2657
David Benjaminc11ea9422017-08-29 16:33:21 -04002658// ssl_get_prev_session looks up the previous session based on |client_hello|.
2659// On success, it sets |*out_session| to the session or nullptr if none was
2660// found. If the session could not be looked up synchronously, it returns
2661// |ssl_hs_pending_session| and should be called again. If a ticket could not be
2662// decrypted immediately it returns |ssl_hs_pending_ticket| and should also
2663// be called again. Otherwise, it returns |ssl_hs_error|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002664enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs,
Steven Valdez398085b2017-08-29 13:25:56 -04002665 UniquePtr<SSL_SESSION> *out_session,
David Benjaminfd45ee72017-08-31 14:49:09 -04002666 bool *out_tickets_supported,
2667 bool *out_renew_ticket,
Steven Valdez398085b2017-08-29 13:25:56 -04002668 const SSL_CLIENT_HELLO *client_hello);
David Benjamine3aa1d92015-06-16 15:34:50 -04002669
David Benjaminc11ea9422017-08-29 16:33:21 -04002670// The following flags determine which parts of the session are duplicated.
Steven Valdez4aa154e2016-07-29 14:32:55 -04002671#define SSL_SESSION_DUP_AUTH_ONLY 0x0
2672#define SSL_SESSION_INCLUDE_TICKET 0x1
2673#define SSL_SESSION_INCLUDE_NONAUTH 0x2
2674#define SSL_SESSION_DUP_ALL \
2675 (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
2676
David Benjaminc11ea9422017-08-29 16:33:21 -04002677// SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
2678// fields in |session| or nullptr on error. The new session is non-resumable and
2679// must be explicitly marked resumable once it has been filled in.
David Benjamin31b0c9b2017-07-20 14:49:15 -04002680OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
2681 int dup_flags);
Steven Valdez87eab492016-06-27 16:34:59 -04002682
David Benjaminc11ea9422017-08-29 16:33:21 -04002683// ssl_session_rebase_time updates |session|'s start time to the current time,
2684// adjusting the timeout so the expiration time is unchanged.
David Benjamin17b30832017-01-28 14:00:32 -05002685void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
2686
David Benjaminc11ea9422017-08-29 16:33:21 -04002687// ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
2688// |session|'s timeout to |timeout| (measured from the current time). The
2689// renewal is clamped to the session's auth_timeout.
David Benjaminad8f5e12017-02-20 17:00:20 -05002690void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
2691 uint32_t timeout);
David Benjamin123db572016-11-03 16:59:25 -04002692
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002693void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
David Benjaminf31e6812014-11-13 18:05:55 -05002694
David Benjaminb29e1e12019-05-06 14:44:46 -05002695void ssl_send_alert(SSL *ssl, int level, int desc);
2696int ssl_send_alert_impl(SSL *ssl, int level, int desc);
Adam Langleyc9827e02019-04-12 14:46:50 -07002697bool ssl3_get_message(const SSL *ssl, SSLMessage *out);
David Benjamind9229f92017-10-06 17:36:20 -04002698ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
2699 uint8_t *out_alert, Span<uint8_t> in);
David Benjamin8f94c312017-08-01 17:35:55 -04002700void ssl3_next_message(SSL *ssl);
David Benjamin854dd652014-08-26 00:32:30 -04002701
David Benjamin0d56f882015-12-19 17:05:56 -05002702int ssl3_dispatch_alert(SSL *ssl);
David Benjamind9229f92017-10-06 17:36:20 -04002703ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
2704 size_t *out_consumed, uint8_t *out_alert,
2705 Span<uint8_t> in);
2706ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2707 uint8_t *out_alert,
2708 Span<uint8_t> in);
David Benjamin74795b32017-08-31 15:13:12 -04002709int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
Steven Valdeze831a812017-03-09 14:56:07 -05002710 int len);
Steven Valdezb6b6ff32016-10-26 11:56:35 -04002711
David Benjamin97250f42017-10-07 04:12:35 -04002712bool ssl3_new(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05002713void ssl3_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002714
David Benjamin97250f42017-10-07 04:12:35 -04002715bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2716bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2717bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg);
2718bool ssl3_add_change_cipher_spec(SSL *ssl);
David Benjamindaf207a2017-01-03 18:37:41 -05002719int ssl3_flush_flight(SSL *ssl);
David Benjamin75836432016-06-17 18:48:29 -04002720
David Benjamin97250f42017-10-07 04:12:35 -04002721bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2722bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2723bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
2724bool dtls1_add_change_cipher_spec(SSL *ssl);
David Benjamin1a999cf2017-01-03 10:30:35 -05002725int dtls1_flush_flight(SSL *ssl);
David Benjaminc6604172016-06-02 16:38:35 -04002726
David Benjaminc11ea9422017-08-29 16:33:21 -04002727// ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
David Benjamin97250f42017-10-07 04:12:35 -04002728// the pending flight. It returns true on success and false on error.
2729bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
Steven Valdez5eead162016-11-11 22:23:25 -05002730
David Benjamin97250f42017-10-07 04:12:35 -04002731// ssl_hash_message incorporates |msg| into the handshake hash. It returns true
2732// on success and false on allocation failure.
David Benjamin7934f082017-08-01 16:32:25 -04002733bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjaminced94792016-11-14 17:12:11 +09002734
David Benjamind9229f92017-10-06 17:36:20 -04002735ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
2736 size_t *out_consumed, uint8_t *out_alert,
2737 Span<uint8_t> in);
2738ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2739 uint8_t *out_alert,
2740 Span<uint8_t> in);
Adam Langley95c29f32014-06-20 12:00:00 -07002741
David Benjamin74795b32017-08-31 15:13:12 -04002742int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
2743 const uint8_t *buf, int len);
David Benjamin45d45c12016-06-07 15:20:49 -04002744
David Benjaminc11ea9422017-08-29 16:33:21 -04002745// dtls1_write_record sends a record. It returns one on success and <= 0 on
2746// error.
David Benjamin45d45c12016-06-07 15:20:49 -04002747int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
2748 enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -07002749
David Benjaminaad50db2016-06-23 17:49:12 -04002750int dtls1_retransmit_outgoing_messages(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04002751bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
Jesse Selover6b0d8222018-08-14 12:47:29 -04002752 CBS *out_body);
David Benjamin97250f42017-10-07 04:12:35 -04002753bool dtls1_check_timeout_num(SSL *ssl);
David Benjamin2fa83de2015-02-08 01:40:08 -05002754
David Benjamin0d56f882015-12-19 17:05:56 -05002755void dtls1_start_timer(SSL *ssl);
2756void dtls1_stop_timer(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04002757bool dtls1_is_timer_expired(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002758unsigned int dtls1_min_mtu(void);
2759
David Benjamin97250f42017-10-07 04:12:35 -04002760bool dtls1_new(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05002761void dtls1_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002762
Adam Langleyc9827e02019-04-12 14:46:50 -07002763bool dtls1_get_message(const SSL *ssl, SSLMessage *out);
David Benjamind9229f92017-10-06 17:36:20 -04002764ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
2765 uint8_t *out_alert, Span<uint8_t> in);
David Benjamin8f94c312017-08-01 17:35:55 -04002766void dtls1_next_message(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05002767int dtls1_dispatch_alert(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002768
Adam Langley3fe8fa72018-01-26 09:14:30 -08002769// tls1_configure_aead configures either the read or write direction AEAD (as
2770// determined by |direction|) using the keys generated by the TLS KDF. The
2771// |key_block_cache| argument is used to store the generated key block, if
2772// empty. Otherwise it's assumed that the key block is already contained within
2773// it. Returns one on success or zero on error.
2774int tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction,
2775 Array<uint8_t> *key_block_cache,
2776 const SSL_CIPHER *cipher,
2777 Span<const uint8_t> iv_override);
2778
David Benjamine58f8a62017-09-21 19:07:15 -04002779int tls1_change_cipher_state(SSL_HANDSHAKE *hs, evp_aead_direction_t direction);
Steven Valdez908ac192017-01-12 13:17:07 -05002780int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
David Benjamin2637f3c2017-10-27 00:56:55 -04002781 Span<const uint8_t> premaster);
Adam Langley95c29f32014-06-20 12:00:00 -07002782
David Benjamincf0ce672017-09-21 02:25:59 -04002783// tls1_get_grouplist returns the locally-configured group preference list.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002784Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl);
Steven Valdez5440fe02016-07-18 12:40:30 -04002785
David Benjamin861abcc2018-07-14 17:40:26 -04002786// tls1_check_group_id returns whether |group_id| is consistent with locally-
2787// configured group preferences.
2788bool tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id);
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002789
David Benjaminc11ea9422017-08-29 16:33:21 -04002790// tls1_get_shared_group sets |*out_group_id| to the first preferred shared
David Benjamin861abcc2018-07-14 17:40:26 -04002791// group between client and server preferences and returns true. If none may be
2792// found, it returns false.
2793bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
David Benjamin072334d2014-07-13 16:24:27 -04002794
David Benjamin0ce090a2018-07-02 20:24:40 -04002795// tls1_set_curves converts the array of NIDs in |curves| into a newly allocated
2796// array of TLS group IDs. On success, the function returns true and writes the
2797// array to |*out_group_ids|. Otherwise, it returns false.
2798bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves);
David Benjamin072334d2014-07-13 16:24:27 -04002799
David Benjaminc11ea9422017-08-29 16:33:21 -04002800// tls1_set_curves_list converts the string of curves pointed to by |curves|
2801// into a newly allocated array of TLS group IDs. On success, the function
David Benjamin0ce090a2018-07-02 20:24:40 -04002802// returns true and writes the array to |*out_group_ids|. Otherwise, it returns
2803// false.
2804bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01002805
David Benjamin861abcc2018-07-14 17:40:26 -04002806// ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It returns
2807// true on success and false on failure. The |header_len| argument is the length
2808// of the ClientHello written so far and is used to compute the padding length.
2809// (It does not include the record header.)
2810bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
David Benjamine8d53502015-10-10 14:13:23 -04002811
David Benjamin861abcc2018-07-14 17:40:26 -04002812bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
2813bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2814 const SSL_CLIENT_HELLO *client_hello);
2815bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
Adam Langley95c29f32014-06-20 12:00:00 -07002816
Adam Langleyfcf25832014-12-18 17:42:32 -08002817#define tlsext_tick_md EVP_sha256
David Benjamine3aa1d92015-06-16 15:34:50 -04002818
David Benjaminc11ea9422017-08-29 16:33:21 -04002819// ssl_process_ticket processes a session ticket from the client. It returns
2820// one of:
2821// |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
2822// |*out_renew_ticket| is set to whether the ticket should be renewed.
2823// |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
2824// fresh ticket should be sent, but the given ticket cannot be used.
2825// |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
2826// Retry later.
2827// |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
Adam Langley4c341d02017-03-08 19:33:21 -08002828enum ssl_ticket_aead_result_t ssl_process_ticket(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002829 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
David Benjamin28655672018-07-18 23:23:25 -04002830 bool *out_renew_ticket, Span<const uint8_t> ticket,
2831 Span<const uint8_t> session_id);
Adam Langley95c29f32014-06-20 12:00:00 -07002832
David Benjaminc11ea9422017-08-29 16:33:21 -04002833// tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
David Benjamin861abcc2018-07-14 17:40:26 -04002834// the signature. If the key is valid, it saves the Channel ID and returns true.
2835// Otherwise, it returns false.
2836bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Nick Harper60a85cb2016-09-23 16:25:11 -07002837
David Benjaminc11ea9422017-08-29 16:33:21 -04002838// tls1_write_channel_id generates a Channel ID message and puts the output in
David Benjamin46853762018-07-03 14:01:26 -04002839// |cbb|. |ssl->channel_id_private| must already be set before calling. This
2840// function returns true on success and false on error.
David Benjaminf1db1a32017-10-27 01:12:34 -04002841bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07002842
David Benjaminc11ea9422017-08-29 16:33:21 -04002843// tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
2844// it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
David Benjamin861abcc2018-07-14 17:40:26 -04002845// true on success and false on failure.
2846bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04002847
David Benjamin861abcc2018-07-14 17:40:26 -04002848// tls1_record_handshake_hashes_for_channel_id records the current handshake
2849// hashes in |hs->new_session| so that Channel ID resumptions can sign that
2850// data.
2851bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
Adam Langley1258b6a2014-06-20 12:00:00 -07002852
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002853// ssl_do_channel_id_callback checks runs |hs->ssl->ctx->channel_id_cb| if
David Benjamin861abcc2018-07-14 17:40:26 -04002854// necessary. It returns true on success and false on fatal error. Note that, on
David Benjamin46853762018-07-03 14:01:26 -04002855// success, |hs->ssl->channel_id_private| may be unset, in which case the
David Benjaminc11ea9422017-08-29 16:33:21 -04002856// operation should be retried later.
David Benjamin861abcc2018-07-14 17:40:26 -04002857bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs);
Nick Harper60a85cb2016-09-23 16:25:11 -07002858
David Benjamin861abcc2018-07-14 17:40:26 -04002859// ssl_can_write returns whether |ssl| is allowed to write.
2860bool ssl_can_write(const SSL *ssl);
Steven Valdez681eb6a2016-12-19 13:19:29 -05002861
David Benjamin861abcc2018-07-14 17:40:26 -04002862// ssl_can_read returns wheter |ssl| is allowed to read.
2863bool ssl_can_read(const SSL *ssl);
Steven Valdez681eb6a2016-12-19 13:19:29 -05002864
David Benjaminad8f5e12017-02-20 17:00:20 -05002865void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002866void ssl_ctx_get_current_time(const SSL_CTX *ctx,
2867 struct OPENSSL_timeval *out_clock);
David Benjamin721e8b72016-08-03 13:13:17 -04002868
David Benjaminc11ea9422017-08-29 16:33:21 -04002869// ssl_reset_error_state resets state for |SSL_get_error|.
David Benjamin2be4aa72017-01-02 07:56:51 -05002870void ssl_reset_error_state(SSL *ssl);
2871
David Benjamin31aad2d2017-10-06 16:04:06 -04002872// ssl_set_read_error sets |ssl|'s read half into an error state, saving the
2873// current state of the error queue.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002874void ssl_set_read_error(SSL *ssl);
David Benjamin31aad2d2017-10-06 16:04:06 -04002875
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002876BSSL_NAMESPACE_END
Adam Langley11d11d62017-07-17 11:10:46 -07002877
Steven Valdez87eab492016-06-27 16:34:59 -04002878
David Benjaminc11ea9422017-08-29 16:33:21 -04002879// Opaque C types.
2880//
2881// The following types are exported to C code as public typedefs, so they must
2882// be defined outside of the namespace.
David Benjamine39ac8f2017-07-20 12:22:21 -04002883
David Benjaminc11ea9422017-08-29 16:33:21 -04002884// ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
2885// structure to support the legacy version-locked methods.
David Benjamine39ac8f2017-07-20 12:22:21 -04002886struct ssl_method_st {
David Benjaminc11ea9422017-08-29 16:33:21 -04002887 // version, if non-zero, is the only protocol version acceptable to an
2888 // SSL_CTX initialized from this method.
David Benjamine39ac8f2017-07-20 12:22:21 -04002889 uint16_t version;
David Benjaminc11ea9422017-08-29 16:33:21 -04002890 // method is the underlying SSL_PROTOCOL_METHOD that initializes the
2891 // SSL_CTX.
David Benjamin664e99a2017-10-10 19:23:21 -04002892 const bssl::SSL_PROTOCOL_METHOD *method;
David Benjaminc11ea9422017-08-29 16:33:21 -04002893 // x509_method contains pointers to functions that might deal with |X509|
2894 // compatibility, or might be a no-op, depending on the application.
David Benjamin0363de92018-06-29 13:57:38 -04002895 const bssl::SSL_X509_METHOD *x509_method;
David Benjamine39ac8f2017-07-20 12:22:21 -04002896};
2897
David Benjamin49798032018-07-02 16:23:54 -04002898struct ssl_ctx_st {
David Benjaminc1389f22018-07-02 19:13:42 -04002899 explicit ssl_ctx_st(const SSL_METHOD *ssl_method);
2900 ssl_ctx_st(const ssl_ctx_st &) = delete;
2901 ssl_ctx_st &operator=(const ssl_ctx_st &) = delete;
2902
2903 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
2904 const bssl::SSL_X509_METHOD *x509_method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002905
2906 // lock is used to protect various operations on this object.
2907 CRYPTO_MUTEX lock;
2908
2909 // conf_max_version is the maximum acceptable protocol version configured by
2910 // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
2911 // and is further constrainted by |SSL_OP_NO_*|.
David Benjaminc1389f22018-07-02 19:13:42 -04002912 uint16_t conf_max_version = 0;
David Benjamin49798032018-07-02 16:23:54 -04002913
2914 // conf_min_version is the minimum acceptable protocol version configured by
2915 // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
2916 // and is further constrainted by |SSL_OP_NO_*|.
David Benjaminc1389f22018-07-02 19:13:42 -04002917 uint16_t conf_min_version = 0;
David Benjamin49798032018-07-02 16:23:54 -04002918
Steven Valdezc8e0f902018-07-14 11:23:01 -04002919 // quic_method is the method table corresponding to the QUIC hooks.
2920 const SSL_QUIC_METHOD *quic_method = nullptr;
2921
David Benjamin0ce090a2018-07-02 20:24:40 -04002922 bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list;
David Benjamin49798032018-07-02 16:23:54 -04002923
David Benjaminc1389f22018-07-02 19:13:42 -04002924 X509_STORE *cert_store = nullptr;
2925 LHASH_OF(SSL_SESSION) *sessions = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002926 // Most session-ids that will be cached, default is
2927 // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
David Benjaminc1389f22018-07-02 19:13:42 -04002928 unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
2929 SSL_SESSION *session_cache_head = nullptr;
2930 SSL_SESSION *session_cache_tail = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002931
2932 // handshakes_since_cache_flush is the number of successful handshakes since
2933 // the last cache flush.
David Benjaminc1389f22018-07-02 19:13:42 -04002934 int handshakes_since_cache_flush = 0;
David Benjamin49798032018-07-02 16:23:54 -04002935
2936 // This can have one of 2 values, ored together,
2937 // SSL_SESS_CACHE_CLIENT,
2938 // SSL_SESS_CACHE_SERVER,
2939 // Default is SSL_SESSION_CACHE_SERVER, which means only
2940 // SSL_accept which cache SSL_SESSIONS.
David Benjaminc1389f22018-07-02 19:13:42 -04002941 int session_cache_mode = SSL_SESS_CACHE_SERVER;
David Benjamin49798032018-07-02 16:23:54 -04002942
2943 // session_timeout is the default lifetime for new sessions in TLS 1.2 and
2944 // earlier, in seconds.
David Benjaminc1389f22018-07-02 19:13:42 -04002945 uint32_t session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin49798032018-07-02 16:23:54 -04002946
2947 // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
2948 // 1.3, in seconds.
David Benjaminc1389f22018-07-02 19:13:42 -04002949 uint32_t session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin49798032018-07-02 16:23:54 -04002950
2951 // If this callback is not null, it will be called each time a session id is
2952 // added to the cache. If this function returns 1, it means that the
2953 // callback will do a SSL_SESSION_free() when it has finished using it.
2954 // Otherwise, on 0, it means the callback has finished with it. If
2955 // remove_session_cb is not null, it will be called when a session-id is
2956 // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
2957 // it.
David Benjaminc1389f22018-07-02 19:13:42 -04002958 int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess) = nullptr;
2959 void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002960 SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
David Benjaminc1389f22018-07-02 19:13:42 -04002961 int *copy) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002962
David Benjaminc1389f22018-07-02 19:13:42 -04002963 CRYPTO_refcount_t references = 1;
David Benjamin49798032018-07-02 16:23:54 -04002964
2965 // if defined, these override the X509_verify_cert() calls
David Benjaminc1389f22018-07-02 19:13:42 -04002966 int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg) = nullptr;
2967 void *app_verify_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002968
David Benjaminc1389f22018-07-02 19:13:42 -04002969 ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
2970 uint8_t *out_alert) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002971
2972 // Default password callback.
David Benjaminc1389f22018-07-02 19:13:42 -04002973 pem_password_cb *default_passwd_callback = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002974
2975 // Default password callback user data.
David Benjaminc1389f22018-07-02 19:13:42 -04002976 void *default_passwd_callback_userdata = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002977
2978 // get client cert callback
Jesse Selover6b0d8222018-08-14 12:47:29 -04002979 int (*client_cert_cb)(SSL *ssl, X509 **out_x509,
2980 EVP_PKEY **out_pkey) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002981
2982 // get channel id callback
David Benjaminc1389f22018-07-02 19:13:42 -04002983 void (*channel_id_cb)(SSL *ssl, EVP_PKEY **out_pkey) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002984
2985 CRYPTO_EX_DATA ex_data;
2986
David Benjamin49798032018-07-02 16:23:54 -04002987 // Default values used when no per-SSL value is defined follow
2988
David Benjaminc1389f22018-07-02 19:13:42 -04002989 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002990
2991 // what we put in client cert requests
David Benjamin0ce090a2018-07-02 20:24:40 -04002992 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
David Benjamin49798032018-07-02 16:23:54 -04002993
2994 // cached_x509_client_CA is a cache of parsed versions of the elements of
2995 // |client_CA|.
David Benjaminc1389f22018-07-02 19:13:42 -04002996 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002997
2998
2999 // Default values to use in SSL structures follow (these are copied by
3000 // SSL_new)
3001
David Benjaminc1389f22018-07-02 19:13:42 -04003002 uint32_t options = 0;
3003 // Disable the auto-chaining feature by default. wpa_supplicant relies on this
3004 // feature, but require callers opt into it.
3005 uint32_t mode = SSL_MODE_NO_AUTO_CHAIN;
3006 uint32_t max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
David Benjamin49798032018-07-02 16:23:54 -04003007
David Benjamin0ce090a2018-07-02 20:24:40 -04003008 bssl::UniquePtr<bssl::CERT> cert;
David Benjamin49798032018-07-02 16:23:54 -04003009
3010 // callback that allows applications to peek at protocol messages
3011 void (*msg_callback)(int write_p, int version, int content_type,
Jesse Selover6b0d8222018-08-14 12:47:29 -04003012 const void *buf, size_t len, SSL *ssl,
3013 void *arg) = nullptr;
David Benjaminc1389f22018-07-02 19:13:42 -04003014 void *msg_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003015
David Benjaminc1389f22018-07-02 19:13:42 -04003016 int verify_mode = SSL_VERIFY_NONE;
3017 int (*default_verify_callback)(int ok, X509_STORE_CTX *ctx) =
3018 nullptr; // called 'verify_callback' in the SSL
David Benjamin49798032018-07-02 16:23:54 -04003019
David Benjaminc1389f22018-07-02 19:13:42 -04003020 X509_VERIFY_PARAM *param = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003021
3022 // select_certificate_cb is called before most ClientHello processing and
3023 // before the decision whether to resume a session is made. See
3024 // |ssl_select_cert_result_t| for details of the return values.
David Benjaminc1389f22018-07-02 19:13:42 -04003025 ssl_select_cert_result_t (*select_certificate_cb)(const SSL_CLIENT_HELLO *) =
3026 nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003027
3028 // dos_protection_cb is called once the resumption decision for a ClientHello
3029 // has been made. It returns one to continue the handshake or zero to
3030 // abort.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003031 int (*dos_protection_cb)(const SSL_CLIENT_HELLO *) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003032
Jesse Selover1c337e52018-08-10 13:28:47 -04003033 // Controls whether to verify certificates when resuming connections. They
3034 // were already verified when the connection was first made, so the default is
3035 // false. For now, this is only respected on clients, not servers.
3036 bool reverify_on_resume = false;
3037
David Benjamin49798032018-07-02 16:23:54 -04003038 // Maximum amount of data to send in one fragment. actual record size can be
3039 // more than this due to padding and MAC overheads.
David Benjaminc1389f22018-07-02 19:13:42 -04003040 uint16_t max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin49798032018-07-02 16:23:54 -04003041
3042 // TLS extensions servername callback
David Benjamin46853762018-07-03 14:01:26 -04003043 int (*servername_callback)(SSL *, int *, void *) = nullptr;
3044 void *servername_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003045
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003046 // RFC 4507 session ticket keys. |ticket_key_current| may be NULL before the
3047 // first handshake and |ticket_key_prev| may be NULL at any time.
3048 // Automatically generated ticket keys are rotated as needed at handshake
3049 // time. Hence, all access must be synchronized through |lock|.
3050 bssl::UniquePtr<bssl::TicketKey> ticket_key_current;
3051 bssl::UniquePtr<bssl::TicketKey> ticket_key_prev;
David Benjamin49798032018-07-02 16:23:54 -04003052
3053 // Callback to support customisation of ticket key setting
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003054 int (*ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
3055 EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003056
3057 // Server-only: psk_identity_hint is the default identity hint to send in
3058 // PSK-based key exchanges.
David Benjamin0ce090a2018-07-02 20:24:40 -04003059 bssl::UniquePtr<char> psk_identity_hint;
David Benjamin49798032018-07-02 16:23:54 -04003060
David Benjaminc1389f22018-07-02 19:13:42 -04003061 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
3062 unsigned max_identity_len, uint8_t *psk,
3063 unsigned max_psk_len) = nullptr;
3064 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
3065 unsigned max_psk_len) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003066
3067
3068 // Next protocol negotiation information
3069 // (for experimental NPN extension).
3070
3071 // For a server, this contains a callback function by which the set of
3072 // advertised protocols can be provided.
3073 int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
David Benjaminc1389f22018-07-02 19:13:42 -04003074 unsigned *out_len, void *arg) = nullptr;
3075 void *next_protos_advertised_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003076 // For a client, this contains a callback function that selects the
3077 // next protocol from the list provided by the server.
3078 int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
David Benjaminc1389f22018-07-02 19:13:42 -04003079 const uint8_t *in, unsigned in_len,
3080 void *arg) = nullptr;
3081 void *next_proto_select_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003082
3083 // ALPN information
3084 // (we are in the process of transitioning from NPN to ALPN.)
3085
3086 // For a server, this contains a callback function that allows the
3087 // server to select the protocol for the connection.
3088 // out: on successful return, this must point to the raw protocol
3089 // name (without the length prefix).
3090 // outlen: on successful return, this contains the length of |*out|.
3091 // in: points to the client's list of supported protocols in
3092 // wire-format.
3093 // inlen: the length of |in|.
3094 int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
David Benjaminc1389f22018-07-02 19:13:42 -04003095 const uint8_t *in, unsigned in_len,
3096 void *arg) = nullptr;
3097 void *alpn_select_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003098
3099 // For a client, this contains the list of supported protocols in wire
3100 // format.
David Benjamin0ce090a2018-07-02 20:24:40 -04003101 bssl::Array<uint8_t> alpn_client_proto_list;
David Benjamin49798032018-07-02 16:23:54 -04003102
3103 // SRTP profiles we are willing to do from RFC 5764
David Benjamin0ce090a2018-07-02 20:24:40 -04003104 bssl::UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
David Benjamin49798032018-07-02 16:23:54 -04003105
3106 // Defined compression algorithms for certificates.
David Benjamin35b4a122018-07-14 17:04:41 -04003107 bssl::UniquePtr<STACK_OF(CertCompressionAlg)> cert_compression_algs;
David Benjamin49798032018-07-02 16:23:54 -04003108
3109 // Supported group values inherited by SSL structure
David Benjamin0ce090a2018-07-02 20:24:40 -04003110 bssl::Array<uint16_t> supported_group_list;
David Benjamin49798032018-07-02 16:23:54 -04003111
3112 // The client's Channel ID private key.
David Benjamin46853762018-07-03 14:01:26 -04003113 bssl::UniquePtr<EVP_PKEY> channel_id_private;
David Benjamin49798032018-07-02 16:23:54 -04003114
3115 // keylog_callback, if not NULL, is the key logging callback. See
3116 // |SSL_CTX_set_keylog_callback|.
David Benjaminc1389f22018-07-02 19:13:42 -04003117 void (*keylog_callback)(const SSL *ssl, const char *line) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003118
3119 // current_time_cb, if not NULL, is the function to use to get the current
3120 // time. It sets |*out_clock| to the current time. The |ssl| argument is
3121 // always NULL. See |SSL_CTX_set_current_time_cb|.
David Benjaminc1389f22018-07-02 19:13:42 -04003122 void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003123
3124 // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
3125 // memory.
David Benjaminc1389f22018-07-02 19:13:42 -04003126 CRYPTO_BUFFER_POOL *pool = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003127
3128 // ticket_aead_method contains function pointers for opening and sealing
3129 // session tickets.
David Benjaminc1389f22018-07-02 19:13:42 -04003130 const SSL_TICKET_AEAD_METHOD *ticket_aead_method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003131
3132 // legacy_ocsp_callback implements an OCSP-related callback for OpenSSL
3133 // compatibility.
David Benjaminc1389f22018-07-02 19:13:42 -04003134 int (*legacy_ocsp_callback)(SSL *ssl, void *arg) = nullptr;
3135 void *legacy_ocsp_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003136
3137 // verify_sigalgs, if not empty, is the set of signature algorithms
3138 // accepted from the peer in decreasing order of preference.
David Benjamin0ce090a2018-07-02 20:24:40 -04003139 bssl::Array<uint16_t> verify_sigalgs;
David Benjamin49798032018-07-02 16:23:54 -04003140
3141 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
3142 // hash of the peer's certificate and then discard it to save memory and
3143 // session space. Only effective on the server side.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003144 bool retain_only_sha256_of_client_certs : 1;
David Benjamin49798032018-07-02 16:23:54 -04003145
3146 // quiet_shutdown is true if the connection should not send a close_notify on
3147 // shutdown.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003148 bool quiet_shutdown : 1;
David Benjamin49798032018-07-02 16:23:54 -04003149
3150 // ocsp_stapling_enabled is only used by client connections and indicates
3151 // whether OCSP stapling will be requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003152 bool ocsp_stapling_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003153
3154 // If true, a client will request certificate timestamps.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003155 bool signed_cert_timestamps_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003156
David Benjamin46853762018-07-03 14:01:26 -04003157 // channel_id_enabled is whether Channel ID is enabled. For a server, means
3158 // that we'll accept Channel IDs from clients. For a client, means that we'll
3159 // advertise support.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003160 bool channel_id_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003161
3162 // grease_enabled is whether draft-davidben-tls-grease-01 is enabled.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003163 bool grease_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003164
3165 // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN
3166 // protocols from the peer.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003167 bool allow_unknown_alpn_protos : 1;
David Benjamin49798032018-07-02 16:23:54 -04003168
3169 // ed25519_enabled is whether Ed25519 is advertised in the handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003170 bool ed25519_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003171
3172 // rsa_pss_rsae_certs_enabled is whether rsa_pss_rsae_* are supported by the
3173 // certificate verifier.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003174 bool rsa_pss_rsae_certs_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003175
3176 // false_start_allowed_without_alpn is whether False Start (if
3177 // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003178 bool false_start_allowed_without_alpn : 1;
David Benjamin49798032018-07-02 16:23:54 -04003179
Steven Valdezf1af1292018-08-13 10:54:48 -04003180 // ignore_tls13_downgrade is whether a connection should continue when the
3181 // server random signals a downgrade.
3182 bool ignore_tls13_downgrade:1;
3183
David Benjamin49798032018-07-02 16:23:54 -04003184 // handoff indicates that a server should stop after receiving the
3185 // ClientHello and pause the handshake in such a way that |SSL_get_error|
3186 // returns |SSL_HANDOFF|.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003187 bool handoff : 1;
David Benjamin49798032018-07-02 16:23:54 -04003188
3189 // If enable_early_data is true, early data can be sent and accepted.
3190 bool enable_early_data : 1;
David Benjaminc1389f22018-07-02 19:13:42 -04003191
Adam Langley9f5c4192019-07-19 15:18:33 -07003192 // pq_experiment_signal indicates that an empty extension should be sent
3193 // (for clients) or echoed (for servers) to indicate participation in an
3194 // experiment of post-quantum key exchanges.
3195 bool pq_experiment_signal : 1;
3196
David Benjaminc1389f22018-07-02 19:13:42 -04003197 private:
3198 ~ssl_ctx_st();
3199 friend void SSL_CTX_free(SSL_CTX *);
David Benjamin49798032018-07-02 16:23:54 -04003200};
3201
3202struct ssl_st {
David Benjaminc1389f22018-07-02 19:13:42 -04003203 explicit ssl_st(SSL_CTX *ctx_arg);
3204 ssl_st(const ssl_st &) = delete;
3205 ssl_st &operator=(const ssl_st &) = delete;
3206 ~ssl_st();
3207
David Benjamin49798032018-07-02 16:23:54 -04003208 // method is the method table corresponding to the current protocol (DTLS or
3209 // TLS).
David Benjaminc1389f22018-07-02 19:13:42 -04003210 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003211
3212 // config is a container for handshake configuration. Accesses to this field
3213 // should check for nullptr, since configuration may be shed after the
3214 // handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use
3215 // that instead, and skip the null check.)
David Benjamin50596f82018-07-02 19:47:27 -04003216 bssl::UniquePtr<bssl::SSL_CONFIG> config;
David Benjamin49798032018-07-02 16:23:54 -04003217
3218 // version is the protocol version.
David Benjaminc1389f22018-07-02 19:13:42 -04003219 uint16_t version = 0;
David Benjamin49798032018-07-02 16:23:54 -04003220
David Benjaminc1389f22018-07-02 19:13:42 -04003221 uint16_t max_send_fragment = 0;
David Benjamin49798032018-07-02 16:23:54 -04003222
3223 // There are 2 BIO's even though they are normally both the same. This is so
3224 // data can be read and written to different handlers
3225
David Benjamin50596f82018-07-02 19:47:27 -04003226 bssl::UniquePtr<BIO> rbio; // used by SSL_read
3227 bssl::UniquePtr<BIO> wbio; // used by SSL_write
David Benjamin49798032018-07-02 16:23:54 -04003228
3229 // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
3230 // Otherwise, it returns a value corresponding to what operation is needed to
3231 // progress.
David Benjaminc1389f22018-07-02 19:13:42 -04003232 bssl::ssl_hs_wait_t (*do_handshake)(bssl::SSL_HANDSHAKE *hs) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003233
David Benjaminc59b9aa2018-07-16 21:34:03 -04003234 bssl::SSL3_STATE *s3 = nullptr; // TLS variables
3235 bssl::DTLS1_STATE *d1 = nullptr; // DTLS variables
David Benjamin49798032018-07-02 16:23:54 -04003236
3237 // callback that allows applications to peek at protocol messages
3238 void (*msg_callback)(int write_p, int version, int content_type,
David Benjaminc1389f22018-07-02 19:13:42 -04003239 const void *buf, size_t len, SSL *ssl,
3240 void *arg) = nullptr;
3241 void *msg_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003242
3243 // session info
3244
3245 // initial_timeout_duration_ms is the default DTLS timeout duration in
3246 // milliseconds. It's used to initialize the timer any time it's restarted.
David Benjaminc1389f22018-07-02 19:13:42 -04003247 //
3248 // RFC 6347 states that implementations SHOULD use an initial timer value of 1
3249 // second.
3250 unsigned initial_timeout_duration_ms = 1000;
David Benjamin49798032018-07-02 16:23:54 -04003251
David Benjamin49798032018-07-02 16:23:54 -04003252 // session is the configured session to be offered by the client. This session
3253 // is immutable.
David Benjamin50596f82018-07-02 19:47:27 -04003254 bssl::UniquePtr<SSL_SESSION> session;
David Benjamin49798032018-07-02 16:23:54 -04003255
David Benjaminc1389f22018-07-02 19:13:42 -04003256 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003257
David Benjamin50596f82018-07-02 19:47:27 -04003258 bssl::UniquePtr<SSL_CTX> ctx;
David Benjamin49798032018-07-02 16:23:54 -04003259
3260 // session_ctx is the |SSL_CTX| used for the session cache and related
3261 // settings.
David Benjamin50596f82018-07-02 19:47:27 -04003262 bssl::UniquePtr<SSL_CTX> session_ctx;
David Benjamin49798032018-07-02 16:23:54 -04003263
3264 // extra application data
3265 CRYPTO_EX_DATA ex_data;
3266
David Benjaminc1389f22018-07-02 19:13:42 -04003267 uint32_t options = 0; // protocol behaviour
3268 uint32_t mode = 0; // API behaviour
3269 uint32_t max_cert_list = 0;
David Benjamin46853762018-07-03 14:01:26 -04003270 bssl::UniquePtr<char> hostname;
David Benjamin49798032018-07-02 16:23:54 -04003271
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +00003272 // quic_method is the method table corresponding to the QUIC hooks.
3273 const SSL_QUIC_METHOD *quic_method = nullptr;
3274
David Benjamin49798032018-07-02 16:23:54 -04003275 // renegotiate_mode controls how peer renegotiation attempts are handled.
David Benjaminc1389f22018-07-02 19:13:42 -04003276 ssl_renegotiate_mode_t renegotiate_mode = ssl_renegotiate_never;
David Benjamin49798032018-07-02 16:23:54 -04003277
3278 // server is true iff the this SSL* is the server half. Note: before the SSL*
3279 // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
3280 // the side is not determined. In this state, server is always false.
3281 bool server : 1;
3282
3283 // quiet_shutdown is true if the connection should not send a close_notify on
3284 // shutdown.
3285 bool quiet_shutdown : 1;
3286
David Benjamin49798032018-07-02 16:23:54 -04003287 // If enable_early_data is true, early data can be sent and accepted.
3288 bool enable_early_data : 1;
3289};
David Benjamine39ac8f2017-07-20 12:22:21 -04003290
David Benjamin1c68fa22018-06-02 19:02:46 -04003291struct ssl_session_st {
David Benjaminbfdd1a92018-06-29 16:26:38 -04003292 explicit ssl_session_st(const bssl::SSL_X509_METHOD *method);
3293 ssl_session_st(const ssl_session_st &) = delete;
3294 ssl_session_st &operator=(const ssl_session_st &) = delete;
3295
3296 CRYPTO_refcount_t references = 1;
Jesse Selover6b0d8222018-08-14 12:47:29 -04003297
3298 // ssl_version is the (D)TLS version that established the session.
3299 uint16_t ssl_version = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003300
3301 // group_id is the ID of the ECDH group used to establish this session or zero
3302 // if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003303 uint16_t group_id = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003304
3305 // peer_signature_algorithm is the signature algorithm used to authenticate
3306 // the peer, or zero if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003307 uint16_t peer_signature_algorithm = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003308
3309 // master_key, in TLS 1.2 and below, is the master secret associated with the
3310 // session. In TLS 1.3 and up, it is the resumption secret.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003311 int master_key_length = 0;
3312 uint8_t master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003313
3314 // session_id - valid?
David Benjaminbfdd1a92018-06-29 16:26:38 -04003315 unsigned session_id_length = 0;
3316 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003317 // this is used to determine whether the session is being reused in
3318 // the appropriate context. It is up to the application to set this,
3319 // via SSL_new
David Benjaminbfdd1a92018-06-29 16:26:38 -04003320 uint8_t sid_ctx_length = 0;
3321 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003322
David Benjaminbfdd1a92018-06-29 16:26:38 -04003323 bssl::UniquePtr<char> psk_identity;
David Benjamin1c68fa22018-06-02 19:02:46 -04003324
3325 // certs contains the certificate chain from the peer, starting with the leaf
3326 // certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003327 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs;
David Benjamin1c68fa22018-06-02 19:02:46 -04003328
David Benjaminbfdd1a92018-06-29 16:26:38 -04003329 const bssl::SSL_X509_METHOD *x509_method = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003330
3331 // x509_peer is the peer's certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003332 X509 *x509_peer = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003333
3334 // x509_chain is the certificate chain sent by the peer. NOTE: for historical
3335 // reasons, when a client (so the peer is a server), the chain includes
3336 // |peer|, but when a server it does not.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003337 STACK_OF(X509) *x509_chain = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003338
3339 // x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that
3340 // omits the leaf certificate. This exists because OpenSSL, historically,
3341 // didn't include the leaf certificate in the chain for a server, but did for
3342 // a client. The |x509_chain| always includes it and, if an API call requires
3343 // a chain without, it is stored here.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003344 STACK_OF(X509) *x509_chain_without_leaf = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003345
3346 // verify_result is the result of certificate verification in the case of
3347 // non-fatal certificate errors.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003348 long verify_result = X509_V_ERR_INVALID_CALL;
David Benjamin1c68fa22018-06-02 19:02:46 -04003349
3350 // timeout is the lifetime of the session in seconds, measured from |time|.
3351 // This is renewable up to |auth_timeout|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003352 uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003353
3354 // auth_timeout is the non-renewable lifetime of the session in seconds,
3355 // measured from |time|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003356 uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003357
3358 // time is the time the session was issued, measured in seconds from the UNIX
3359 // epoch.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003360 uint64_t time = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003361
David Benjaminbfdd1a92018-06-29 16:26:38 -04003362 const SSL_CIPHER *cipher = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003363
3364 CRYPTO_EX_DATA ex_data; // application specific data
3365
3366 // These are used to make removal of session-ids more efficient and to
3367 // implement a maximum cache size.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003368 SSL_SESSION *prev = nullptr, *next = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003369
David Benjaminbfdd1a92018-06-29 16:26:38 -04003370 bssl::Array<uint8_t> ticket;
David Benjamin1c68fa22018-06-02 19:02:46 -04003371
David Benjaminbfdd1a92018-06-29 16:26:38 -04003372 bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin1c68fa22018-06-02 19:02:46 -04003373
3374 // The OCSP response that came with the session.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003375 bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin1c68fa22018-06-02 19:02:46 -04003376
3377 // peer_sha256 contains the SHA-256 hash of the peer's certificate if
3378 // |peer_sha256_valid| is true.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003379 uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003380
3381 // original_handshake_hash contains the handshake hash (either SHA-1+MD5 or
3382 // SHA-2, depending on TLS version) for the original, full handshake that
3383 // created a session. This is used by Channel IDs during resumption.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003384 uint8_t original_handshake_hash[EVP_MAX_MD_SIZE] = {0};
3385 uint8_t original_handshake_hash_len = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003386
David Benjaminbfdd1a92018-06-29 16:26:38 -04003387 uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds
David Benjamin1c68fa22018-06-02 19:02:46 -04003388
David Benjaminbfdd1a92018-06-29 16:26:38 -04003389 uint32_t ticket_age_add = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003390
3391 // ticket_max_early_data is the maximum amount of data allowed to be sent as
3392 // early data. If zero, 0-RTT is disallowed.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003393 uint32_t ticket_max_early_data = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003394
3395 // early_alpn is the ALPN protocol from the initial handshake. This is only
3396 // stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT
3397 // resumptions.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003398 bssl::Array<uint8_t> early_alpn;
David Benjamin1c68fa22018-06-02 19:02:46 -04003399
David Benjamina3a71e92018-06-29 13:24:45 -04003400 // extended_master_secret is whether the master secret in this session was
David Benjamin1c68fa22018-06-02 19:02:46 -04003401 // generated using EMS and thus isn't vulnerable to the Triple Handshake
3402 // attack.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003403 bool extended_master_secret : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003404
David Benjamina3a71e92018-06-29 13:24:45 -04003405 // peer_sha256_valid is whether |peer_sha256| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003406 bool peer_sha256_valid : 1; // Non-zero if peer_sha256 is valid
David Benjamin1c68fa22018-06-02 19:02:46 -04003407
3408 // not_resumable is used to indicate that session resumption is disallowed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003409 bool not_resumable : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003410
David Benjamina3a71e92018-06-29 13:24:45 -04003411 // ticket_age_add_valid is whether |ticket_age_add| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003412 bool ticket_age_add_valid : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003413
David Benjamina3a71e92018-06-29 13:24:45 -04003414 // is_server is whether this session was created by a server.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003415 bool is_server : 1;
David Benjaminbfdd1a92018-06-29 16:26:38 -04003416
3417 private:
3418 ~ssl_session_st();
3419 friend void SSL_SESSION_free(SSL_SESSION *);
David Benjamin1c68fa22018-06-02 19:02:46 -04003420};
3421
David Benjamine39ac8f2017-07-20 12:22:21 -04003422
David Benjaminc11ea9422017-08-29 16:33:21 -04003423#endif // OPENSSL_HEADER_SSL_INTERNAL_H