blob: c63558336cb0f5dc00b29fd98dc88f1e5a9077f3 [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
David Benjamin9806ae02019-08-16 15:32:03 -0400599 // CopyToHashContext initializes |ctx| with |digest| and the data thus far in
600 // the transcript. It returns true on success and false on failure. If the
601 // handshake buffer is still present, |digest| may be any supported digest.
602 // Otherwise, |digest| must match the transcript hash.
603 bool CopyToHashContext(EVP_MD_CTX *ctx, const EVP_MD *digest);
Steven Valdezcd8470f2017-10-11 12:29:36 -0400604
David Benjamin75a1f232017-10-11 17:19:19 -0400605 Span<const uint8_t> buffer() {
606 return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
607 buffer_->length);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400608 }
David Benjamin6dc8bf62017-07-19 16:38:21 -0400609
David Benjaminc11ea9422017-08-29 16:33:21 -0400610 // FreeBuffer releases the handshake buffer. Subsequent calls to
611 // |Update| will not update the handshake buffer.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400612 void FreeBuffer();
613
David Benjaminc11ea9422017-08-29 16:33:21 -0400614 // DigestLen returns the length of the PRF hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400615 size_t DigestLen() const;
616
David Benjaminc11ea9422017-08-29 16:33:21 -0400617 // Digest returns the PRF hash. For TLS 1.1 and below, this is
618 // |EVP_md5_sha1|.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400619 const EVP_MD *Digest() const;
620
David Benjaminc11ea9422017-08-29 16:33:21 -0400621 // Update adds |in| to the handshake buffer and handshake hash, whichever is
622 // enabled. It returns true on success and false on failure.
David Benjamin75a1f232017-10-11 17:19:19 -0400623 bool Update(Span<const uint8_t> in);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400624
David Benjaminc11ea9422017-08-29 16:33:21 -0400625 // GetHash writes the handshake hash to |out| which must have room for at
626 // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
627 // the number of bytes written. Otherwise, it returns false.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400628 bool GetHash(uint8_t *out, size_t *out_len);
629
David Benjaminc11ea9422017-08-29 16:33:21 -0400630 // GetFinishedMAC computes the MAC for the Finished message into the bytes
631 // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
632 // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
633 // on failure.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400634 bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
David Benjamin6abaa312017-08-31 01:12:28 -0400635 bool from_server);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400636
637 private:
David Benjaminc11ea9422017-08-29 16:33:21 -0400638 // buffer_, if non-null, contains the handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400639 UniquePtr<BUF_MEM> buffer_;
David Benjamina4e9f8d2018-06-26 00:12:52 -0400640 // hash, if initialized with an |EVP_MD|, maintains the handshake hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400641 ScopedEVP_MD_CTX hash_;
David Benjamin86e95b82017-07-18 16:34:25 -0400642};
Steven Valdez908ac192017-01-12 13:17:07 -0500643
David Benjamin2637f3c2017-10-27 00:56:55 -0400644// tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
645// as the secret and |label| as the label. |seed1| and |seed2| are concatenated
646// to form the seed parameter. It returns true on success and false on failure.
647bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
648 Span<const uint8_t> secret, Span<const char> label,
649 Span<const uint8_t> seed1, Span<const uint8_t> seed2);
Steven Valdez908ac192017-01-12 13:17:07 -0500650
651
David Benjaminc11ea9422017-08-29 16:33:21 -0400652// Encryption layer.
David Benjamin31a07792015-03-03 14:20:26 -0500653
David Benjaminc11ea9422017-08-29 16:33:21 -0400654// SSLAEADContext contains information about an AEAD that is being used to
655// encrypt an SSL connection.
David Benjamincfc11c22017-07-18 22:45:18 -0400656class SSLAEADContext {
657 public:
Steven Valdezc7d4d212017-09-11 13:53:08 -0400658 SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
David Benjamincfc11c22017-07-18 22:45:18 -0400659 ~SSLAEADContext();
David Benjamin9a892502017-07-25 22:26:44 -0400660 static constexpr bool kAllowUniquePtr = true;
661
David Benjamincfc11c22017-07-18 22:45:18 -0400662 SSLAEADContext(const SSLAEADContext &&) = delete;
663 SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
664
David Benjaminc11ea9422017-08-29 16:33:21 -0400665 // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400666 static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
David Benjamincfc11c22017-07-18 22:45:18 -0400667
David Benjaminc11ea9422017-08-29 16:33:21 -0400668 // Create creates an |SSLAEADContext| using the supplied key material. It
669 // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
670 // resulting object, depending on |direction|. |version| is the normalized
671 // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
David Benjaminb9493552017-09-27 19:02:51 -0400672 static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
David Benjamin8525ff32018-09-05 18:44:15 -0500673 uint16_t version, bool is_dtls,
David Benjaminb9493552017-09-27 19:02:51 -0400674 const SSL_CIPHER *cipher,
675 Span<const uint8_t> enc_key,
676 Span<const uint8_t> mac_key,
677 Span<const uint8_t> fixed_iv);
David Benjamincfc11c22017-07-18 22:45:18 -0400678
Steven Valdezc8e0f902018-07-14 11:23:01 -0400679 // CreatePlaceholderForQUIC creates a placeholder |SSLAEADContext| for the
680 // given cipher and version. The resulting object can be queried for various
681 // properties but cannot encrypt or decrypt data.
682 static UniquePtr<SSLAEADContext> CreatePlaceholderForQUIC(
683 uint16_t version, const SSL_CIPHER *cipher);
684
Steven Valdezc7d4d212017-09-11 13:53:08 -0400685 // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
686 // cipher, to make version-specific determinations in the record layer prior
687 // to a cipher being selected.
688 void SetVersionIfNullCipher(uint16_t version);
689
690 // ProtocolVersion returns the protocol version associated with this
691 // SSLAEADContext. It can only be called once |version_| has been set to a
692 // valid value.
693 uint16_t ProtocolVersion() const;
694
695 // RecordVersion returns the record version that should be used with this
696 // SSLAEADContext for record construction and crypto.
697 uint16_t RecordVersion() const;
698
David Benjamincfc11c22017-07-18 22:45:18 -0400699 const SSL_CIPHER *cipher() const { return cipher_; }
700
David Benjaminc11ea9422017-08-29 16:33:21 -0400701 // is_null_cipher returns true if this is the null cipher.
David Benjamincfc11c22017-07-18 22:45:18 -0400702 bool is_null_cipher() const { return !cipher_; }
703
David Benjaminc11ea9422017-08-29 16:33:21 -0400704 // ExplicitNonceLen returns the length of the explicit nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400705 size_t ExplicitNonceLen() const;
706
David Benjaminc11ea9422017-08-29 16:33:21 -0400707 // MaxOverhead returns the maximum overhead of calling |Seal|.
David Benjamincfc11c22017-07-18 22:45:18 -0400708 size_t MaxOverhead() const;
709
David Benjaminc11ea9422017-08-29 16:33:21 -0400710 // SuffixLen calculates the suffix length written by |SealScatter| and writes
711 // it to |*out_suffix_len|. It returns true on success and false on error.
712 // |in_len| and |extra_in_len| should equal the argument of the same names
713 // passed to |SealScatter|.
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700714 bool SuffixLen(size_t *out_suffix_len, size_t in_len,
715 size_t extra_in_len) const;
David Benjamincfc11c22017-07-18 22:45:18 -0400716
David Benjamine2ab21d2018-04-04 23:55:06 -0400717 // CiphertextLen calculates the total ciphertext length written by
718 // |SealScatter| and writes it to |*out_len|. It returns true on success and
719 // false on error. |in_len| and |extra_in_len| should equal the argument of
720 // the same names passed to |SealScatter|.
721 bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const;
722
David Benjaminc64d1232017-10-04 18:14:28 -0400723 // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
724 // to the plaintext in |in| and returns true. Otherwise, it returns
725 // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
726 bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
David Benjamine2ab21d2018-04-04 23:55:06 -0400727 const uint8_t seqnum[8], Span<const uint8_t> header,
728 Span<uint8_t> in);
David Benjamincfc11c22017-07-18 22:45:18 -0400729
David Benjaminc11ea9422017-08-29 16:33:21 -0400730 // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
731 // result to |out|. It returns true on success and false on error.
732 //
733 // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
David Benjamincfc11c22017-07-18 22:45:18 -0400734 bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
David Benjamine2ab21d2018-04-04 23:55:06 -0400735 uint16_t record_version, const uint8_t seqnum[8],
736 Span<const uint8_t> header, const uint8_t *in, size_t in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400737
David Benjaminc11ea9422017-08-29 16:33:21 -0400738 // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
739 // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
740 // success and zero on error.
741 //
742 // On successful return, exactly |ExplicitNonceLen| bytes are written to
743 // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
744 // |out_suffix|.
745 //
746 // |extra_in| may point to an additional plaintext buffer. If present,
747 // |extra_in_len| additional bytes are encrypted and authenticated, and the
748 // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
749 // be used to size |out_suffix| accordingly.
750 //
751 // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
752 // alias anything.
David Benjamincfc11c22017-07-18 22:45:18 -0400753 bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400754 uint8_t type, uint16_t record_version,
David Benjamine2ab21d2018-04-04 23:55:06 -0400755 const uint8_t seqnum[8], Span<const uint8_t> header,
756 const uint8_t *in, size_t in_len, const uint8_t *extra_in,
757 size_t extra_in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400758
759 bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
760
761 private:
David Benjamine2ab21d2018-04-04 23:55:06 -0400762 // GetAdditionalData returns the additional data, writing into |storage| if
763 // necessary.
764 Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type,
765 uint16_t record_version,
766 const uint8_t seqnum[8],
767 size_t plaintext_len,
768 Span<const uint8_t> header);
David Benjamincfc11c22017-07-18 22:45:18 -0400769
770 const SSL_CIPHER *cipher_;
771 ScopedEVP_AEAD_CTX ctx_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400772 // fixed_nonce_ contains any bytes of the nonce that are fixed for all
773 // records.
David Benjamincfc11c22017-07-18 22:45:18 -0400774 uint8_t fixed_nonce_[12];
775 uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400776 // version_ is the wire version that should be used with this AEAD.
David Benjamincfc11c22017-07-18 22:45:18 -0400777 uint16_t version_;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400778 // is_dtls_ is whether DTLS is being used with this AEAD.
779 bool is_dtls_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400780 // variable_nonce_included_in_record_ is true if the variable nonce
781 // for a record is included as a prefix before the ciphertext.
David Benjamincfc11c22017-07-18 22:45:18 -0400782 bool variable_nonce_included_in_record_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400783 // random_variable_nonce_ is true if the variable nonce is
784 // randomly generated, rather than derived from the sequence
785 // number.
David Benjamincfc11c22017-07-18 22:45:18 -0400786 bool random_variable_nonce_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400787 // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
788 // variable nonce rather than prepended.
789 bool xor_fixed_nonce_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400790 // omit_length_in_ad_ is true if the length should be omitted in the
791 // AEAD's ad parameter.
David Benjamincfc11c22017-07-18 22:45:18 -0400792 bool omit_length_in_ad_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400793 // ad_is_header_ is true if the AEAD's ad parameter is the record header.
794 bool ad_is_header_ : 1;
David Benjamin86e95b82017-07-18 16:34:25 -0400795};
David Benjamin31a07792015-03-03 14:20:26 -0500796
David Benjamin31a07792015-03-03 14:20:26 -0500797
David Benjaminc11ea9422017-08-29 16:33:21 -0400798// DTLS replay bitmap.
David Benjamin7446a3b2015-07-25 17:53:57 -0400799
David Benjaminc11ea9422017-08-29 16:33:21 -0400800// DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
801// replayed packets. It should be initialized by zeroing every field.
David Benjamin86e95b82017-07-18 16:34:25 -0400802struct DTLS1_BITMAP {
David Benjaminc11ea9422017-08-29 16:33:21 -0400803 // map is a bit mask of the last 64 sequence numbers. Bit
804 // |1<<i| corresponds to |max_seq_num - i|.
David Benjamin800046f2017-10-17 01:37:51 -0400805 uint64_t map = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -0400806 // max_seq_num is the largest sequence number seen so far as a 64-bit
807 // integer.
David Benjamin800046f2017-10-17 01:37:51 -0400808 uint64_t max_seq_num = 0;
David Benjamin86e95b82017-07-18 16:34:25 -0400809};
David Benjamin7446a3b2015-07-25 17:53:57 -0400810
811
David Benjaminc11ea9422017-08-29 16:33:21 -0400812// Record layer.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400813
David Benjaminc11ea9422017-08-29 16:33:21 -0400814// ssl_record_sequence_update increments the sequence number in |seq|. It
David Benjamin8525ff32018-09-05 18:44:15 -0500815// returns true on success and false on wraparound.
816bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
David Benjamin1db21562015-12-25 15:11:39 -0500817
David Benjaminc11ea9422017-08-29 16:33:21 -0400818// ssl_record_prefix_len returns the length of the prefix before the ciphertext
819// of a record for |ssl|.
820//
821// TODO(davidben): Expose this as part of public API once the high-level
822// buffer-free APIs are available.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400823size_t ssl_record_prefix_len(const SSL *ssl);
824
825enum ssl_open_record_t {
826 ssl_open_record_success,
827 ssl_open_record_discard,
828 ssl_open_record_partial,
David Benjamin728f3542016-06-02 15:42:01 -0400829 ssl_open_record_close_notify,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400830 ssl_open_record_error,
831};
832
David Benjaminc11ea9422017-08-29 16:33:21 -0400833// tls_open_record decrypts a record from |in| in-place.
834//
835// If the input did not contain a complete record, it returns
836// |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
837// bytes necessary. It is guaranteed that a successful call to |tls_open_record|
838// will consume at least that many bytes.
839//
840// Otherwise, it sets |*out_consumed| to the number of bytes of input
841// consumed. Note that input may be consumed on all return codes if a record was
842// decrypted.
843//
844// On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
845// record type and |*out| to the record body in |in|. Note that |*out| may be
846// empty.
847//
848// If a record was successfully processed but should be discarded, it returns
849// |ssl_open_record_discard|.
850//
David Benjamin751d1a12017-09-25 13:37:30 -0400851// If a record was successfully processed but is a close_notify, it returns
852// |ssl_open_record_close_notify|.
David Benjaminc11ea9422017-08-29 16:33:21 -0400853//
David Benjamin751d1a12017-09-25 13:37:30 -0400854// On failure or fatal alert, it returns |ssl_open_record_error| and sets
855// |*out_alert| to an alert to emit, or zero if no alert should be emitted.
David Benjaminc64d1232017-10-04 18:14:28 -0400856enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
857 Span<uint8_t> *out, size_t *out_consumed,
858 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400859
David Benjamina05d4272017-10-06 19:34:55 -0400860// dtls_open_record implements |tls_open_record| for DTLS. It only returns
861// |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
862// zero. The caller should read one packet and try again.
David Benjaminc64d1232017-10-04 18:14:28 -0400863enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
864 Span<uint8_t> *out,
David Benjamina7810c12016-06-06 18:54:51 -0400865 size_t *out_consumed,
David Benjaminc64d1232017-10-04 18:14:28 -0400866 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400867
David Benjaminc11ea9422017-08-29 16:33:21 -0400868// ssl_seal_align_prefix_len returns the length of the prefix before the start
869// of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
870// use this to align buffers.
871//
872// Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
873// record and is the offset into second record's ciphertext. Thus sealing a
874// small record may result in a smaller output than this value.
875//
876// TODO(davidben): Is this alignment valuable? Record-splitting makes this a
877// mess.
David Benjamin1a01e1f2016-06-08 18:31:24 -0400878size_t ssl_seal_align_prefix_len(const SSL *ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400879
David Benjaminc11ea9422017-08-29 16:33:21 -0400880// tls_seal_record seals a new record of type |type| and body |in| and writes it
David Benjamin8525ff32018-09-05 18:44:15 -0500881// to |out|. At most |max_out| bytes will be written. It returns true on success
882// and false on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC
883// 1/n-1 record splitting and may write two records concatenated.
David Benjaminc11ea9422017-08-29 16:33:21 -0400884//
885// For a large record, the bulk of the ciphertext will begin
886// |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
887// improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
888// bytes to |out|.
889//
890// |in| and |out| may not alias.
David Benjamin8525ff32018-09-05 18:44:15 -0500891bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
892 uint8_t type, const uint8_t *in, size_t in_len);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400893
894enum dtls1_use_epoch_t {
895 dtls1_use_previous_epoch,
896 dtls1_use_current_epoch,
897};
898
David Benjaminc11ea9422017-08-29 16:33:21 -0400899// dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
900// record.
David Benjamin1a999cf2017-01-03 10:30:35 -0500901size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
902
David Benjaminc11ea9422017-08-29 16:33:21 -0400903// dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
904// front of the plaintext when sealing a record in-place.
David Benjamina772b162017-01-24 17:51:33 -0500905size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
906
David Benjaminc11ea9422017-08-29 16:33:21 -0400907// dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
908// which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
909// may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
910// ahead of |out|.
David Benjamin8525ff32018-09-05 18:44:15 -0500911bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
912 uint8_t type, const uint8_t *in, size_t in_len,
913 enum dtls1_use_epoch_t use_epoch);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400914
David Benjaminc11ea9422017-08-29 16:33:21 -0400915// ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
916// state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
917// |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
918// appropriate.
David Benjamin728f3542016-06-02 15:42:01 -0400919enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
David Benjaminc64d1232017-10-04 18:14:28 -0400920 Span<const uint8_t> in);
David Benjamin728f3542016-06-02 15:42:01 -0400921
David Benjaminb8d28cf2015-07-28 21:34:45 -0400922
David Benjaminc11ea9422017-08-29 16:33:21 -0400923// Private key operations.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400924
Christopher Patton9cde8482018-07-17 11:36:36 -0700925// ssl_has_private_key returns whether |hs| has a private key configured.
926bool ssl_has_private_key(const SSL_HANDSHAKE *hs);
nagendra modadugu601448a2015-07-24 09:31:31 -0700927
David Benjaminc11ea9422017-08-29 16:33:21 -0400928// ssl_private_key_* perform the corresponding operation on
929// |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
930// call the corresponding function or |complete| depending on whether there is a
931// pending operation. Otherwise, they implement the operation with
932// |EVP_PKEY|.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400933
David Benjaminb4d65fd2015-05-29 17:11:21 -0400934enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -0400935 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin75a1f232017-10-11 17:19:19 -0400936 uint16_t sigalg, Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400937
David Benjamin75a1f232017-10-11 17:19:19 -0400938enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
939 uint8_t *out,
940 size_t *out_len,
941 size_t max_out,
942 Span<const uint8_t> in);
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700943
David Benjamin75a1f232017-10-11 17:19:19 -0400944// ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
945// key supports |sigalg|.
946bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
Jesse Selover6b0d8222018-08-14 12:47:29 -0400947 uint16_t sigalg);
David Benjamin1fb125c2016-07-08 18:52:12 -0700948
David Benjaminc11ea9422017-08-29 16:33:21 -0400949// ssl_public_key_verify verifies that the |signature| is valid for the public
950// key |pkey| and input |in|, using the signature algorithm |sigalg|.
David Benjamin75a1f232017-10-11 17:19:19 -0400951bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
952 uint16_t sigalg, EVP_PKEY *pkey,
953 Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400954
David Benjamin1fb125c2016-07-08 18:52:12 -0700955
David Benjaminc11ea9422017-08-29 16:33:21 -0400956// Key shares.
David Benjamin4298d772015-12-19 00:18:25 -0500957
David Benjaminc11ea9422017-08-29 16:33:21 -0400958// SSLKeyShare abstracts over Diffie-Hellman-like key exchanges.
David Benjaminc642aca2017-07-19 23:28:43 -0400959class SSLKeyShare {
960 public:
961 virtual ~SSLKeyShare() {}
David Benjamin9a892502017-07-25 22:26:44 -0400962 static constexpr bool kAllowUniquePtr = true;
David Benjaminc9376992017-07-24 15:31:13 -0400963 HAS_VIRTUAL_DESTRUCTOR
David Benjaminb9179092016-10-26 13:47:33 -0400964
David Benjaminc11ea9422017-08-29 16:33:21 -0400965 // Create returns a SSLKeyShare instance for use with group |group_id| or
966 // nullptr on error.
David Benjaminc642aca2017-07-19 23:28:43 -0400967 static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
David Benjamin4298d772015-12-19 00:18:25 -0500968
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700969 // Create deserializes an SSLKeyShare instance previously serialized by
970 // |Serialize|.
971 static UniquePtr<SSLKeyShare> Create(CBS *in);
972
David Benjaminc11ea9422017-08-29 16:33:21 -0400973 // GroupID returns the group ID.
David Benjaminc9376992017-07-24 15:31:13 -0400974 virtual uint16_t GroupID() const PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -0500975
David Benjaminc11ea9422017-08-29 16:33:21 -0400976 // Offer generates a keypair and writes the public value to
977 // |out_public_key|. It returns true on success and false on error.
David Benjaminc9376992017-07-24 15:31:13 -0400978 virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -0500979
Adam Langley7b935932018-11-12 13:53:42 -0800980 // Accept performs a key exchange against the |peer_key| generated by |Offer|.
David Benjaminc11ea9422017-08-29 16:33:21 -0400981 // On success, it returns true, writes the public value to |out_public_key|,
Adam Langley7b935932018-11-12 13:53:42 -0800982 // and sets |*out_secret| to the shared secret. On failure, it returns false
983 // and sets |*out_alert| to an alert to send to the peer.
David Benjaminc11ea9422017-08-29 16:33:21 -0400984 //
985 // The default implementation calls |Offer| and then |Finish|, assuming a key
986 // exchange protocol where the peers are symmetric.
David Benjamin499742c2017-07-22 12:45:38 -0400987 virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
988 uint8_t *out_alert, Span<const uint8_t> peer_key);
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -0700989
David Benjaminc11ea9422017-08-29 16:33:21 -0400990 // Finish performs a key exchange against the |peer_key| generated by
David Benjamin499742c2017-07-22 12:45:38 -0400991 // |Accept|. On success, it returns true and sets |*out_secret| to the shared
Adam Langley7b935932018-11-12 13:53:42 -0800992 // secret. On failure, it returns false and sets |*out_alert| to an alert to
David Benjamin499742c2017-07-22 12:45:38 -0400993 // send to the peer.
994 virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
995 Span<const uint8_t> peer_key) PURE_VIRTUAL;
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700996
997 // Serialize writes the state of the key exchange to |out|, returning true if
998 // successful and false otherwise.
999 virtual bool Serialize(CBB *out) { return false; }
1000
1001 // Deserialize initializes the state of the key exchange from |in|, returning
1002 // true if successful and false otherwise. It is called by |Create|.
1003 virtual bool Deserialize(CBS *in) { return false; }
David Benjaminb9179092016-10-26 13:47:33 -04001004};
David Benjamin4298d772015-12-19 00:18:25 -05001005
Matthew Braithwaitec65eb2c2018-11-02 17:29:35 -07001006struct NamedGroup {
1007 int nid;
1008 uint16_t group_id;
1009 const char name[8], alias[11];
1010};
1011
1012// NamedGroups returns all supported groups.
1013Span<const NamedGroup> NamedGroups();
1014
David Benjaminc11ea9422017-08-29 16:33:21 -04001015// ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
David Benjamin8525ff32018-09-05 18:44:15 -05001016// sets |*out_group_id| to the group ID and returns true. Otherwise, it returns
1017// false.
1018bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
David Benjamin4298d772015-12-19 00:18:25 -05001019
David Benjamin8525ff32018-09-05 18:44:15 -05001020// ssl_name_to_group_id looks up the group corresponding to the |name| string of
1021// length |len|. On success, it sets |*out_group_id| to the group ID and returns
1022// true. Otherwise, it returns false.
1023bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001024
David Benjamin4298d772015-12-19 00:18:25 -05001025
David Benjaminc11ea9422017-08-29 16:33:21 -04001026// Handshake messages.
David Benjamin060cfb02016-05-12 00:43:05 -04001027
David Benjamin7934f082017-08-01 16:32:25 -04001028struct SSLMessage {
1029 bool is_v2_hello;
1030 uint8_t type;
1031 CBS body;
David Benjaminc11ea9422017-08-29 16:33:21 -04001032 // raw is the entire serialized handshake message, including the TLS or DTLS
1033 // message header.
David Benjamin7934f082017-08-01 16:32:25 -04001034 CBS raw;
1035};
1036
David Benjaminc11ea9422017-08-29 16:33:21 -04001037// SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
1038// ChangeCipherSpec, in the longest handshake flight. Currently this is the
1039// client's second leg in a full handshake when client certificates, NPN, and
1040// Channel ID, are all enabled.
David Benjamin29a83c52016-06-17 19:12:54 -04001041#define SSL_MAX_HANDSHAKE_FLIGHT 7
1042
Steven Valdez964b2372017-11-07 17:09:52 -05001043extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE];
Steven Valdezf1af1292018-08-13 10:54:48 -04001044extern const uint8_t kTLS12DowngradeRandom[8];
1045extern const uint8_t kTLS13DowngradeRandom[8];
David Benjamin6965d252018-11-19 15:49:56 -06001046extern const uint8_t kJDK11DowngradeRandom[8];
Steven Valdez964b2372017-11-07 17:09:52 -05001047
David Benjaminc11ea9422017-08-29 16:33:21 -04001048// ssl_max_handshake_message_len returns the maximum number of bytes permitted
1049// in a handshake message for |ssl|.
David Benjamin060cfb02016-05-12 00:43:05 -04001050size_t ssl_max_handshake_message_len(const SSL *ssl);
1051
David Benjamin33febf62017-10-07 16:52:50 -04001052// tls_can_accept_handshake_data returns whether |ssl| is able to accept more
1053// data into handshake buffer.
1054bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
1055
David Benjamin40e94702017-10-06 18:26:36 -04001056// tls_has_unprocessed_handshake_data returns whether there is buffered
1057// handshake data that has not been consumed by |get_message|.
1058bool tls_has_unprocessed_handshake_data(const SSL *ssl);
1059
David Benjamin7d10ab52018-11-02 18:31:17 -05001060// tls_append_handshake_data appends |data| to the handshake buffer. It returns
1061// true on success and false on allocation failure.
1062bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data);
1063
David Benjamin40e94702017-10-06 18:26:36 -04001064// dtls_has_unprocessed_handshake_data behaves like
1065// |tls_has_unprocessed_handshake_data| for DTLS.
1066bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
David Benjamin61672812016-07-14 23:10:43 -04001067
David Benjamin700631b2018-05-24 17:17:34 -04001068// tls_flush_pending_hs_data flushes any handshake plaintext data.
1069bool tls_flush_pending_hs_data(SSL *ssl);
1070
David Benjamin86e95b82017-07-18 16:34:25 -04001071struct DTLS_OUTGOING_MESSAGE {
David Benjamin049fdfc2017-10-17 01:12:53 -04001072 DTLS_OUTGOING_MESSAGE() {}
1073 DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
1074 DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
1075 ~DTLS_OUTGOING_MESSAGE() { Clear(); }
1076
1077 void Clear();
1078
1079 uint8_t *data = nullptr;
1080 uint32_t len = 0;
1081 uint16_t epoch = 0;
1082 bool is_ccs = false;
David Benjamin86e95b82017-07-18 16:34:25 -04001083};
David Benjamin29a83c52016-06-17 19:12:54 -04001084
David Benjaminc11ea9422017-08-29 16:33:21 -04001085// dtls_clear_outgoing_messages releases all buffered outgoing messages.
David Benjamin29a83c52016-06-17 19:12:54 -04001086void dtls_clear_outgoing_messages(SSL *ssl);
1087
David Benjamin060cfb02016-05-12 00:43:05 -04001088
David Benjaminc11ea9422017-08-29 16:33:21 -04001089// Callbacks.
David Benjamin4e9cc712016-06-01 20:16:03 -04001090
David Benjaminc11ea9422017-08-29 16:33:21 -04001091// ssl_do_info_callback calls |ssl|'s info callback, if set.
David Benjamin4e9cc712016-06-01 20:16:03 -04001092void ssl_do_info_callback(const SSL *ssl, int type, int value);
1093
David Benjaminc11ea9422017-08-29 16:33:21 -04001094// ssl_do_msg_callback calls |ssl|'s message callback, if set.
Adam Langleyc9827e02019-04-12 14:46:50 -07001095void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
David Benjaminc64d1232017-10-04 18:14:28 -04001096 Span<const uint8_t> in);
David Benjamin4e9cc712016-06-01 20:16:03 -04001097
1098
David Benjaminc11ea9422017-08-29 16:33:21 -04001099// Transport buffers.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001100
David Benjaminea712e32017-10-13 16:50:39 -04001101class SSLBuffer {
1102 public:
1103 SSLBuffer() {}
1104 ~SSLBuffer() { Clear(); }
1105
1106 SSLBuffer(const SSLBuffer &) = delete;
1107 SSLBuffer &operator=(const SSLBuffer &) = delete;
1108
1109 uint8_t *data() { return buf_ + offset_; }
1110 size_t size() const { return size_; }
1111 bool empty() const { return size_ == 0; }
1112 size_t cap() const { return cap_; }
1113
1114 Span<uint8_t> span() { return MakeSpan(data(), size()); }
1115
1116 Span<uint8_t> remaining() {
1117 return MakeSpan(data() + size(), cap() - size());
1118 }
1119
1120 // Clear releases the buffer.
1121 void Clear();
1122
1123 // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
1124 // that data written after |header_len| is aligned to a
1125 // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
1126 // on error.
1127 bool EnsureCap(size_t header_len, size_t new_cap);
1128
1129 // DidWrite extends the buffer by |len|. The caller must have filled in to
1130 // this point.
1131 void DidWrite(size_t len);
1132
1133 // Consume consumes |len| bytes from the front of the buffer. The memory
1134 // consumed will remain valid until the next call to |DiscardConsumed| or
1135 // |Clear|.
1136 void Consume(size_t len);
1137
1138 // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
1139 // is now empty, it releases memory used by it.
1140 void DiscardConsumed();
1141
1142 private:
1143 // buf_ is the memory allocated for this buffer.
1144 uint8_t *buf_ = nullptr;
1145 // offset_ is the offset into |buf_| which the buffer contents start at.
1146 uint16_t offset_ = 0;
1147 // size_ is the size of the buffer contents from |buf_| + |offset_|.
1148 uint16_t size_ = 0;
1149 // cap_ is how much memory beyond |buf_| + |offset_| is available.
1150 uint16_t cap_ = 0;
1151};
David Benjaminb8d28cf2015-07-28 21:34:45 -04001152
David Benjaminc11ea9422017-08-29 16:33:21 -04001153// ssl_read_buffer_extend_to extends the read buffer to the desired length. For
1154// TLS, it reads to the end of the buffer until the buffer is |len| bytes
1155// long. For DTLS, it reads a new packet and ignores |len|. It returns one on
1156// success, zero on EOF, and a negative number on error.
1157//
1158// It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
1159// non-empty.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001160int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
1161
David Benjaminea712e32017-10-13 16:50:39 -04001162// ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
1163// to a record-processing function. If |ret| is a success or if the caller
1164// should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
1165// 0.
David Benjamind9229f92017-10-06 17:36:20 -04001166int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
1167 size_t consumed, uint8_t alert);
1168
David Benjaminc11ea9422017-08-29 16:33:21 -04001169// ssl_write_buffer_flush flushes the write buffer to the transport. It returns
1170// one on success and <= 0 on error. For DTLS, whether or not the write
1171// succeeds, the write buffer will be cleared.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001172int ssl_write_buffer_flush(SSL *ssl);
1173
David Benjaminb8d28cf2015-07-28 21:34:45 -04001174
David Benjaminc11ea9422017-08-29 16:33:21 -04001175// Certificate functions.
David Benjamin75836432016-06-17 18:48:29 -04001176
David Benjamin8525ff32018-09-05 18:44:15 -05001177// ssl_has_certificate returns whether a certificate and private key are
1178// configured.
Christopher Patton9cde8482018-07-17 11:36:36 -07001179bool ssl_has_certificate(const SSL_HANDSHAKE *hs);
David Benjamin32a66d52016-07-13 22:03:11 -04001180
David Benjaminc11ea9422017-08-29 16:33:21 -04001181// ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
1182// by a TLS Certificate message. On success, it advances |cbs| and returns
1183// true. Otherwise, it returns false and sets |*out_alert| to an alert to send
1184// to the peer.
1185//
1186// If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
1187// the certificate chain and the leaf certificate's public key
1188// respectively. Otherwise, both will be set to nullptr.
1189//
1190// If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
1191// SHA-256 hash of the leaf to |out_leaf_sha256|.
David Benjamine664a532017-07-20 20:19:36 -04001192bool ssl_parse_cert_chain(uint8_t *out_alert,
1193 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
1194 UniquePtr<EVP_PKEY> *out_pubkey,
1195 uint8_t *out_leaf_sha256, CBS *cbs,
1196 CRYPTO_BUFFER_POOL *pool);
David Benjamin5c900c82016-07-13 23:03:26 -04001197
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001198// ssl_add_cert_chain adds |hs->ssl|'s certificate chain to |cbb| in the format
1199// used by a TLS Certificate message. If there is no certificate chain, it emits
David Benjamin8525ff32018-09-05 18:44:15 -05001200// an empty certificate list. It returns true on success and false on error.
1201bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjamin32a66d52016-07-13 22:03:11 -04001202
Jesse Seloverd7266ec2019-01-30 16:06:10 -05001203enum ssl_key_usage_t {
1204 key_usage_digital_signature = 0,
1205 key_usage_encipherment = 2,
1206};
1207
1208// ssl_cert_check_key_usage parses the DER-encoded, X.509 certificate in |in|
1209// and returns true if doesn't specify a key usage or, if it does, if it
1210// includes |bit|. Otherwise it pushes to the error queue and returns false.
1211bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit);
Adam Langley05672202016-12-13 12:05:49 -08001212
David Benjaminc11ea9422017-08-29 16:33:21 -04001213// ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
1214// certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
1215// nullptr and pushes to the error queue.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001216UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
Adam Langleyd5157222016-12-12 11:37:43 -08001217
David Benjaminc11ea9422017-08-29 16:33:21 -04001218// ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
1219// TLS CertificateRequest message. On success, it returns a newly-allocated
1220// |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
1221// sets |*out_alert| to an alert to send to the peer.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001222UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
1223 uint8_t *out_alert,
1224 CBS *cbs);
David Benjamine0332e82016-07-13 22:40:36 -04001225
Steven Valdezcd8470f2017-10-11 12:29:36 -04001226// ssl_has_client_CAs returns there are configured CAs.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001227bool ssl_has_client_CAs(const SSL_CONFIG *cfg);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001228
David Benjaminc11ea9422017-08-29 16:33:21 -04001229// ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
David Benjamin8525ff32018-09-05 18:44:15 -05001230// used by a TLS CertificateRequest message. It returns true on success and
1231// false on error.
1232bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjamin32a66d52016-07-13 22:03:11 -04001233
David Benjaminc11ea9422017-08-29 16:33:21 -04001234// ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
1235// a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
1236// an error on the error queue.
David Benjamin8525ff32018-09-05 18:44:15 -05001237bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Adam Langley05672202016-12-13 12:05:49 -08001238 const CRYPTO_BUFFER *leaf);
Steven Valdezbf5aa842016-07-15 07:07:40 -04001239
David Benjaminc11ea9422017-08-29 16:33:21 -04001240// ssl_on_certificate_selected is called once the certificate has been selected.
1241// It finalizes the certificate and initializes |hs->local_pubkey|. It returns
David Benjamin8525ff32018-09-05 18:44:15 -05001242// true on success and false on error.
1243bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
David Benjamina232a712017-03-30 15:51:53 -05001244
David Benjamin75836432016-06-17 18:48:29 -04001245
David Benjaminc11ea9422017-08-29 16:33:21 -04001246// TLS 1.3 key derivation.
Steven Valdez143e8b32016-07-11 13:19:03 -04001247
David Benjaminc11ea9422017-08-29 16:33:21 -04001248// tls13_init_key_schedule initializes the handshake hash and key derivation
Steven Valdezcd8470f2017-10-11 12:29:36 -04001249// state, and incorporates the PSK. The cipher suite and PRF hash must have been
David Benjamin8525ff32018-09-05 18:44:15 -05001250// selected at this point. It returns true on success and false on error.
David Benjamin79b8b3a2019-08-16 18:58:13 -04001251bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk);
Steven Valdez143e8b32016-07-11 13:19:03 -04001252
David Benjaminc11ea9422017-08-29 16:33:21 -04001253// tls13_init_early_key_schedule initializes the handshake hash and key
Steven Valdezcd8470f2017-10-11 12:29:36 -04001254// derivation state from the resumption secret and incorporates the PSK to
1255// derive the early secrets. It returns one on success and zero on error.
David Benjamin79b8b3a2019-08-16 18:58:13 -04001256bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk);
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.
David Benjamin79b8b3a2019-08-16 18:58:13 -04001260bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in);
Steven Valdez143e8b32016-07-11 13:19:03 -04001261
David Benjaminc11ea9422017-08-29 16:33:21 -04001262// tls13_set_traffic_key sets the read or write traffic keys to
David Benjamin8525ff32018-09-05 18:44:15 -05001263// |traffic_secret|. It returns true on success and false on error.
Steven Valdezc8e0f902018-07-14 11:23:01 -04001264bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level,
1265 enum evp_aead_direction_t direction,
David Benjamin79b8b3a2019-08-16 18:58:13 -04001266 Span<const uint8_t> traffic_secret);
Steven Valdez143e8b32016-07-11 13:19:03 -04001267
David Benjamind6343572019-08-15 17:29:02 -04001268// tls13_derive_early_secret derives the early traffic secret. It returns true
1269// on success and false on error. Unlike with other traffic secrets, this
1270// function does not pass the keys to QUIC. Call
1271// |tls13_set_early_secret_for_quic| to do so. This is done to due to an
1272// ordering complication around resolving HelloRetryRequest on the server.
1273bool tls13_derive_early_secret(SSL_HANDSHAKE *hs);
1274
1275// tls13_set_early_secret_for_quic passes the early traffic secrets, as
1276// derived by |tls13_derive_early_secret|, to QUIC. It returns true on success
1277// and false on error.
1278bool tls13_set_early_secret_for_quic(SSL_HANDSHAKE *hs);
Steven Valdez2d850622017-01-11 11:34:52 -05001279
David Benjaminc11ea9422017-08-29 16:33:21 -04001280// tls13_derive_handshake_secrets derives the handshake traffic secret. It
David Benjamin8525ff32018-09-05 18:44:15 -05001281// returns true on success and false on error.
1282bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001283
David Benjaminc11ea9422017-08-29 16:33:21 -04001284// tls13_rotate_traffic_key derives the next read or write traffic secret. It
David Benjamin8525ff32018-09-05 18:44:15 -05001285// returns true on success and false on error.
1286bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
Steven Valdez1dc53d22016-07-26 12:27:38 -04001287
David Benjaminc11ea9422017-08-29 16:33:21 -04001288// tls13_derive_application_secrets derives the initial application data traffic
1289// and exporter secrets based on the handshake transcripts and |master_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001290// It returns true on success and false on error.
1291bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001292
David Benjaminc11ea9422017-08-29 16:33:21 -04001293// tls13_derive_resumption_secret derives the |resumption_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001294bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001295
David Benjaminc11ea9422017-08-29 16:33:21 -04001296// tls13_export_keying_material provides an exporter interface to use the
1297// |exporter_secret|.
David Benjamin8525ff32018-09-05 18:44:15 -05001298bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
1299 Span<const uint8_t> secret,
1300 Span<const char> label,
1301 Span<const uint8_t> context);
Steven Valdez143e8b32016-07-11 13:19:03 -04001302
David Benjaminc11ea9422017-08-29 16:33:21 -04001303// tls13_finished_mac calculates the MAC of the handshake transcript to verify
1304// the integrity of the Finished message, and stores the result in |out| and
David Benjamin8525ff32018-09-05 18:44:15 -05001305// length in |out_len|. |is_server| is true if this is for the Server Finished
1306// and false for the Client Finished.
1307bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
1308 bool is_server);
Steven Valdez143e8b32016-07-11 13:19:03 -04001309
Steven Valdezcd8470f2017-10-11 12:29:36 -04001310// tls13_derive_session_psk calculates the PSK for this session based on the
1311// resumption master secret and |nonce|. It returns true on success, and false
1312// on failure.
Alessandro Ghedini2cc6f442018-12-11 11:35:17 +00001313bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001314
David Benjaminc11ea9422017-08-29 16:33:21 -04001315// tls13_write_psk_binder calculates the PSK binder value and replaces the last
David Benjamin8525ff32018-09-05 18:44:15 -05001316// bytes of |msg| with the resulting value. It returns true on success, and
1317// false on failure.
David Benjamin9806ae02019-08-16 15:32:03 -04001318bool tls13_write_psk_binder(SSL_HANDSHAKE *hs, Span<uint8_t> msg);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001319
David Benjamin8525ff32018-09-05 18:44:15 -05001320// tls13_verify_psk_binder verifies that the handshake transcript, truncated up
1321// to the binders has a valid signature using the value of |session|'s
1322// resumption secret. It returns true on success, and false on failure.
1323bool tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
1324 const SSLMessage &msg, CBS *binders);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001325
Steven Valdez143e8b32016-07-11 13:19:03 -04001326
David Benjaminc11ea9422017-08-29 16:33:21 -04001327// Handshake functions.
Steven Valdez143e8b32016-07-11 13:19:03 -04001328
1329enum ssl_hs_wait_t {
1330 ssl_hs_error,
1331 ssl_hs_ok,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001332 ssl_hs_read_server_hello,
Steven Valdez143e8b32016-07-11 13:19:03 -04001333 ssl_hs_read_message,
Steven Valdez143e8b32016-07-11 13:19:03 -04001334 ssl_hs_flush,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001335 ssl_hs_certificate_selection_pending,
Adam Langley3fe8fa72018-01-26 09:14:30 -08001336 ssl_hs_handoff,
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001337 ssl_hs_handback,
Steven Valdez143e8b32016-07-11 13:19:03 -04001338 ssl_hs_x509_lookup,
Nick Harper60a85cb2016-09-23 16:25:11 -07001339 ssl_hs_channel_id_lookup,
Steven Valdez143e8b32016-07-11 13:19:03 -04001340 ssl_hs_private_key_operation,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001341 ssl_hs_pending_session,
David Benjamin707af292017-03-10 17:47:18 -05001342 ssl_hs_pending_ticket,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001343 ssl_hs_early_return,
Steven Valdeze831a812017-03-09 14:56:07 -05001344 ssl_hs_early_data_rejected,
Steven Valdez2d850622017-01-11 11:34:52 -05001345 ssl_hs_read_end_of_early_data,
Steven Valdez520e1222017-06-13 12:45:25 -04001346 ssl_hs_read_change_cipher_spec,
David Benjamin3a1dd462017-07-11 16:13:10 -04001347 ssl_hs_certificate_verify,
Steven Valdez143e8b32016-07-11 13:19:03 -04001348};
1349
David Benjamina7bc9442018-01-18 10:08:53 -05001350enum ssl_grease_index_t {
1351 ssl_grease_cipher = 0,
1352 ssl_grease_group,
1353 ssl_grease_extension1,
1354 ssl_grease_extension2,
1355 ssl_grease_version,
1356 ssl_grease_ticket_extension,
1357 ssl_grease_last_index = ssl_grease_ticket_extension,
1358};
1359
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001360enum tls12_server_hs_state_t {
1361 state12_start_accept = 0,
1362 state12_read_client_hello,
1363 state12_select_certificate,
1364 state12_tls13,
1365 state12_select_parameters,
1366 state12_send_server_hello,
1367 state12_send_server_certificate,
1368 state12_send_server_key_exchange,
1369 state12_send_server_hello_done,
1370 state12_read_client_certificate,
1371 state12_verify_client_certificate,
1372 state12_read_client_key_exchange,
1373 state12_read_client_certificate_verify,
1374 state12_read_change_cipher_spec,
1375 state12_process_change_cipher_spec,
1376 state12_read_next_proto,
1377 state12_read_channel_id,
1378 state12_read_client_finished,
1379 state12_send_server_finished,
1380 state12_finish_server_handshake,
1381 state12_done,
1382};
1383
Matthew Braithwaite3e2b3ee2018-05-10 15:46:42 -07001384// handback_t lists the points in the state machine where a handback can occur.
1385// These are the different points at which key material is no longer needed.
1386enum handback_t {
1387 handback_after_session_resumption,
1388 handback_after_ecdhe,
1389 handback_after_handshake,
1390};
1391
Christopher Patton6c1b3762018-07-17 12:49:41 -07001392
1393// Delegated credentials.
1394
1395// This structure stores a delegated credential (DC) as defined by
Watson Ladd2f213f62019-02-12 16:59:54 -08001396// draft-ietf-tls-subcerts-03.
Christopher Patton6c1b3762018-07-17 12:49:41 -07001397struct DC {
1398 static constexpr bool kAllowUniquePtr = true;
1399 ~DC();
1400
1401 // Dup returns a copy of this DC and takes references to |raw| and |pkey|.
1402 UniquePtr<DC> Dup();
1403
1404 // Parse parses the delegated credential stored in |in|. If successful it
1405 // returns the parsed structure, otherwise it returns |nullptr| and sets
1406 // |*out_alert|.
1407 static UniquePtr<DC> Parse(CRYPTO_BUFFER *in, uint8_t *out_alert);
1408
1409 // raw is the delegated credential encoded as specified in draft-ietf-tls-
Christopher Pattonbe9953a2019-04-01 15:39:04 -04001410 // subcerts-03.
Christopher Patton6c1b3762018-07-17 12:49:41 -07001411 UniquePtr<CRYPTO_BUFFER> raw;
1412
1413 // expected_cert_verify_algorithm is the signature scheme of the DC public
1414 // key.
1415 uint16_t expected_cert_verify_algorithm = 0;
1416
Christopher Patton6c1b3762018-07-17 12:49:41 -07001417 // pkey is the public key parsed from |public_key|.
1418 UniquePtr<EVP_PKEY> pkey;
1419
1420 private:
1421 friend DC* New<DC>();
1422 DC();
1423};
1424
1425// ssl_signing_with_dc returns true if the peer has indicated support for
1426// delegated credentials and this host has sent a delegated credential in
1427// response. If this is true then we've committed to using the DC in the
1428// handshake.
1429bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs);
1430
1431
David Benjamin86e95b82017-07-18 16:34:25 -04001432struct SSL_HANDSHAKE {
David Benjamin8f288862017-07-20 14:01:44 -04001433 explicit SSL_HANDSHAKE(SSL *ssl);
1434 ~SSL_HANDSHAKE();
David Benjamin9a892502017-07-25 22:26:44 -04001435 static constexpr bool kAllowUniquePtr = true;
David Benjamin8f288862017-07-20 14:01:44 -04001436
David Benjaminc11ea9422017-08-29 16:33:21 -04001437 // ssl is a non-owning pointer to the parent |SSL| object.
David Benjamince8c9d22016-11-14 10:45:16 +09001438 SSL *ssl;
1439
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001440 // config is a non-owning pointer to the handshake configuration.
1441 SSL_CONFIG *config;
1442
David Benjaminc11ea9422017-08-29 16:33:21 -04001443 // wait contains the operation the handshake is currently blocking on or
1444 // |ssl_hs_ok| if none.
David Benjamin8f288862017-07-20 14:01:44 -04001445 enum ssl_hs_wait_t wait = ssl_hs_ok;
Adam Langley4ba6e192016-12-07 15:54:54 -08001446
David Benjaminc11ea9422017-08-29 16:33:21 -04001447 // state is the internal state for the TLS 1.2 and below handshake. Its
1448 // values depend on |do_handshake| but the starting state is always zero.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001449 int state = 0;
David Benjamincb0c29f2016-12-12 17:00:50 -05001450
David Benjaminc11ea9422017-08-29 16:33:21 -04001451 // tls13_state is the internal state for the TLS 1.3 handshake. Its values
1452 // depend on |do_handshake| but the starting state is always zero.
David Benjamin8f288862017-07-20 14:01:44 -04001453 int tls13_state = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04001454
David Benjaminc11ea9422017-08-29 16:33:21 -04001455 // min_version is the minimum accepted protocol version, taking account both
1456 // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001457 uint16_t min_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001458
David Benjaminc11ea9422017-08-29 16:33:21 -04001459 // max_version is the maximum accepted protocol version, taking account both
1460 // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001461 uint16_t max_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001462
David Benjamine530ea32019-08-16 19:28:00 -04001463 private:
1464 size_t hash_len_ = 0;
1465 uint8_t secret_[SSL_MAX_MD_SIZE] = {0};
1466 uint8_t early_traffic_secret_[SSL_MAX_MD_SIZE] = {0};
1467 uint8_t client_handshake_secret_[SSL_MAX_MD_SIZE] = {0};
1468 uint8_t server_handshake_secret_[SSL_MAX_MD_SIZE] = {0};
1469 uint8_t client_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0};
1470 uint8_t server_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0};
1471 uint8_t expected_client_finished_[SSL_MAX_MD_SIZE] = {0};
1472
1473 public:
1474 void ResizeSecrets(size_t hash_len);
1475
1476 Span<uint8_t> secret() { return MakeSpan(secret_, hash_len_); }
1477 Span<uint8_t> early_traffic_secret() {
1478 return MakeSpan(early_traffic_secret_, hash_len_);
1479 }
1480 Span<uint8_t> client_handshake_secret() {
1481 return MakeSpan(client_handshake_secret_, hash_len_);
1482 }
1483 Span<uint8_t> server_handshake_secret() {
1484 return MakeSpan(server_handshake_secret_, hash_len_);
1485 }
1486 Span<uint8_t> client_traffic_secret_0() {
1487 return MakeSpan(client_traffic_secret_0_, hash_len_);
1488 }
1489 Span<uint8_t> server_traffic_secret_0() {
1490 return MakeSpan(server_traffic_secret_0_, hash_len_);
1491 }
1492 Span<uint8_t> expected_client_finished() {
1493 return MakeSpan(expected_client_finished_, hash_len_);
1494 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001495
David Benjaminf5d2cd02016-10-06 19:39:20 -04001496 union {
David Benjaminc11ea9422017-08-29 16:33:21 -04001497 // sent is a bitset where the bits correspond to elements of kExtensions
1498 // in t1_lib.c. Each bit is set if that extension was sent in a
1499 // ClientHello. It's not used by servers.
David Benjamin8f288862017-07-20 14:01:44 -04001500 uint32_t sent = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04001501 // received is a bitset, like |sent|, but is used by servers to record
1502 // which extensions were received from a client.
David Benjaminf5d2cd02016-10-06 19:39:20 -04001503 uint32_t received;
1504 } extensions;
1505
David Benjaminc11ea9422017-08-29 16:33:21 -04001506 // retry_group is the group ID selected by the server in HelloRetryRequest in
1507 // TLS 1.3.
David Benjamin8f288862017-07-20 14:01:44 -04001508 uint16_t retry_group = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04001509
David Benjamine52f4c42017-10-01 22:35:10 -04001510 // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
1511 UniquePtr<ERR_SAVE_STATE> error;
1512
Adam Langley7b935932018-11-12 13:53:42 -08001513 // key_shares are the current key exchange instances. The second is only used
1514 // as a client if we believe that we should offer two key shares in a
1515 // ClientHello.
1516 UniquePtr<SSLKeyShare> key_shares[2];
Adam Langley4ba6e192016-12-07 15:54:54 -08001517
David Benjaminc11ea9422017-08-29 16:33:21 -04001518 // transcript is the current handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001519 SSLTranscript transcript;
Steven Valdez908ac192017-01-12 13:17:07 -05001520
David Benjaminc11ea9422017-08-29 16:33:21 -04001521 // cookie is the value of the cookie received from the server, if any.
David Benjamin08f5c762017-09-21 02:43:05 -04001522 Array<uint8_t> cookie;
David Benjamin3baa6e12016-10-07 21:10:38 -04001523
David Benjaminc11ea9422017-08-29 16:33:21 -04001524 // key_share_bytes is the value of the previously sent KeyShare extension by
1525 // the client in TLS 1.3.
David Benjamin08f5c762017-09-21 02:43:05 -04001526 Array<uint8_t> key_share_bytes;
David Benjamin4fe3c902016-08-16 02:17:03 -04001527
David Benjaminc11ea9422017-08-29 16:33:21 -04001528 // ecdh_public_key, for servers, is the key share to be sent to the client in
1529 // TLS 1.3.
David Benjamin879efc32017-09-21 11:20:53 -04001530 Array<uint8_t> ecdh_public_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04001531
David Benjaminc11ea9422017-08-29 16:33:21 -04001532 // peer_sigalgs are the signature algorithms that the peer supports. These are
1533 // taken from the contents of the signature algorithms extension for a server
1534 // or from the CertificateRequest for a client.
David Benjaminb1cf48e2017-09-21 11:37:46 -04001535 Array<uint16_t> peer_sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04001536
David Benjaminc11ea9422017-08-29 16:33:21 -04001537 // peer_supported_group_list contains the supported group IDs advertised by
1538 // the peer. This is only set on the server's end. The server does not
1539 // advertise this extension to the client.
David Benjamincf0ce672017-09-21 02:25:59 -04001540 Array<uint16_t> peer_supported_group_list;
David Benjamin43612b62016-10-07 00:41:50 -04001541
David Benjaminc11ea9422017-08-29 16:33:21 -04001542 // peer_key is the peer's ECDH key for a TLS 1.2 client.
David Benjamin499742c2017-07-22 12:45:38 -04001543 Array<uint8_t> peer_key;
David Benjamina4c8ff02016-10-08 02:49:01 -04001544
Nick Harper36fcc4c2017-09-21 15:02:22 -07001545 // negotiated_token_binding_version is used by a server to store the
1546 // on-the-wire encoding of the Token Binding protocol version to advertise in
1547 // the ServerHello/EncryptedExtensions if the Token Binding extension is to be
1548 // sent.
1549 uint16_t negotiated_token_binding_version;
1550
Adam Langleya307cb72018-05-02 09:06:48 -07001551 // cert_compression_alg_id, for a server, contains the negotiated certificate
1552 // compression algorithm for this client. It is only valid if
1553 // |cert_compression_negotiated| is true.
1554 uint16_t cert_compression_alg_id;
1555
David Benjaminc11ea9422017-08-29 16:33:21 -04001556 // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
1557 // parameters. It has client and server randoms prepended for signing
1558 // convenience.
David Benjamin879efc32017-09-21 11:20:53 -04001559 Array<uint8_t> server_params;
David Benjamina4c8ff02016-10-08 02:49:01 -04001560
David Benjaminc11ea9422017-08-29 16:33:21 -04001561 // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
1562 // server when using a TLS 1.2 PSK key exchange.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001563 UniquePtr<char> peer_psk_identity_hint;
Adam Langley4ba6e192016-12-07 15:54:54 -08001564
David Benjaminc11ea9422017-08-29 16:33:21 -04001565 // ca_names, on the client, contains the list of CAs received in a
1566 // CertificateRequest message.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001567 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
Adam Langley34b4c822017-02-02 10:57:17 -08001568
David Benjaminb9493552017-09-27 19:02:51 -04001569 // cached_x509_ca_names contains a cache of parsed versions of the elements of
1570 // |ca_names|. This pointer is left non-owning so only
1571 // |ssl_crypto_x509_method| needs to link against crypto/x509.
David Benjamin8f288862017-07-20 14:01:44 -04001572 STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
Adam Langley4ba6e192016-12-07 15:54:54 -08001573
David Benjaminc11ea9422017-08-29 16:33:21 -04001574 // certificate_types, on the client, contains the set of certificate types
1575 // received in a CertificateRequest message.
David Benjamin08f5c762017-09-21 02:43:05 -04001576 Array<uint8_t> certificate_types;
Adam Langley4ba6e192016-12-07 15:54:54 -08001577
David Benjaminc11ea9422017-08-29 16:33:21 -04001578 // local_pubkey is the public key we are authenticating as.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001579 UniquePtr<EVP_PKEY> local_pubkey;
David Benjamina232a712017-03-30 15:51:53 -05001580
David Benjaminc11ea9422017-08-29 16:33:21 -04001581 // peer_pubkey is the public key parsed from the peer's leaf certificate.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001582 UniquePtr<EVP_PKEY> peer_pubkey;
Adam Langleyd5157222016-12-12 11:37:43 -08001583
David Benjaminc11ea9422017-08-29 16:33:21 -04001584 // new_session is the new mutable session being established by the current
1585 // handshake. It should not be cached.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001586 UniquePtr<SSL_SESSION> new_session;
David Benjamin45738dd2017-02-09 20:01:26 -05001587
David Benjaminc11ea9422017-08-29 16:33:21 -04001588 // early_session is the session corresponding to the current 0-RTT state on
1589 // the client if |in_early_data| is true.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001590 UniquePtr<SSL_SESSION> early_session;
Steven Valdeze831a812017-03-09 14:56:07 -05001591
David Benjaminc11ea9422017-08-29 16:33:21 -04001592 // new_cipher is the cipher being negotiated in this handshake.
David Benjamin8f288862017-07-20 14:01:44 -04001593 const SSL_CIPHER *new_cipher = nullptr;
David Benjamin45738dd2017-02-09 20:01:26 -05001594
David Benjaminc11ea9422017-08-29 16:33:21 -04001595 // key_block is the record-layer key block for TLS 1.2 and earlier.
David Benjaminb9493552017-09-27 19:02:51 -04001596 Array<uint8_t> key_block;
Adam Langley4ba6e192016-12-07 15:54:54 -08001597
David Benjaminfd45ee72017-08-31 14:49:09 -04001598 // scts_requested is true if the SCT extension is in the ClientHello.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001599 bool scts_requested : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001600
David Benjaminfd45ee72017-08-31 14:49:09 -04001601 // needs_psk_binder is true if the ClientHello has a placeholder PSK binder to
1602 // be filled in.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001603 bool needs_psk_binder : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001604
Jesse Selover6b0d8222018-08-14 12:47:29 -04001605 bool received_hello_retry_request : 1;
1606 bool sent_hello_retry_request : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001607
Steven Valdezd8168742017-08-31 10:15:48 -04001608 // handshake_finalized is true once the handshake has completed, at which
1609 // point accessors should use the established state.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001610 bool handshake_finalized : 1;
Steven Valdezd8168742017-08-31 10:15:48 -04001611
David Benjaminc11ea9422017-08-29 16:33:21 -04001612 // accept_psk_mode stores whether the client's PSK mode is compatible with our
1613 // preferences.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001614 bool accept_psk_mode : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001615
David Benjaminfd45ee72017-08-31 14:49:09 -04001616 // cert_request is true if a client certificate was requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001617 bool cert_request : 1;
David Benjamina0486782016-10-06 19:11:32 -04001618
David Benjaminfd45ee72017-08-31 14:49:09 -04001619 // certificate_status_expected is true if OCSP stapling was negotiated and the
David Benjaminc11ea9422017-08-29 16:33:21 -04001620 // server is expected to send a CertificateStatus message. (This is used on
1621 // both the client and server sides.)
Jesse Selover6b0d8222018-08-14 12:47:29 -04001622 bool certificate_status_expected : 1;
David Benjamina0486782016-10-06 19:11:32 -04001623
David Benjaminfd45ee72017-08-31 14:49:09 -04001624 // ocsp_stapling_requested is true if a client requested OCSP stapling.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001625 bool ocsp_stapling_requested : 1;
David Benjamina0486782016-10-06 19:11:32 -04001626
Christopher Patton6c1b3762018-07-17 12:49:41 -07001627 // delegated_credential_requested is true if the peer indicated support for
1628 // the delegated credential extension.
1629 bool delegated_credential_requested : 1;
1630
David Benjaminc11ea9422017-08-29 16:33:21 -04001631 // should_ack_sni is used by a server and indicates that the SNI extension
1632 // should be echoed in the ServerHello.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001633 bool should_ack_sni : 1;
David Benjamina0486782016-10-06 19:11:32 -04001634
David Benjaminfd45ee72017-08-31 14:49:09 -04001635 // in_false_start is true if there is a pending client handshake in False
David Benjaminc11ea9422017-08-29 16:33:21 -04001636 // Start. The client may write data at this point.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001637 bool in_false_start : 1;
David Benjamina0486782016-10-06 19:11:32 -04001638
David Benjaminfd45ee72017-08-31 14:49:09 -04001639 // in_early_data is true if there is a pending handshake that has progressed
David Benjaminc11ea9422017-08-29 16:33:21 -04001640 // enough to send and receive early data.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001641 bool in_early_data : 1;
Steven Valdeze831a812017-03-09 14:56:07 -05001642
David Benjaminfd45ee72017-08-31 14:49:09 -04001643 // early_data_offered is true if the client sent the early_data extension.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001644 bool early_data_offered : 1;
Steven Valdez2d850622017-01-11 11:34:52 -05001645
David Benjaminfd45ee72017-08-31 14:49:09 -04001646 // can_early_read is true if application data may be read at this point in the
David Benjaminc11ea9422017-08-29 16:33:21 -04001647 // handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001648 bool can_early_read : 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05001649
David Benjaminfd45ee72017-08-31 14:49:09 -04001650 // can_early_write is true if application data may be written at this point in
David Benjaminc11ea9422017-08-29 16:33:21 -04001651 // the handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001652 bool can_early_write : 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05001653
David Benjaminc11ea9422017-08-29 16:33:21 -04001654 // next_proto_neg_seen is one of NPN was negotiated.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001655 bool next_proto_neg_seen : 1;
David Benjaminb74b0812016-10-06 19:43:48 -04001656
David Benjaminfd45ee72017-08-31 14:49:09 -04001657 // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
David Benjaminc11ea9422017-08-29 16:33:21 -04001658 // or received.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001659 bool ticket_expected : 1;
David Benjaminf04c2e92016-12-06 13:35:25 -05001660
David Benjaminfd45ee72017-08-31 14:49:09 -04001661 // extended_master_secret is true if the extended master secret extension is
David Benjaminc11ea9422017-08-29 16:33:21 -04001662 // negotiated in this handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001663 bool extended_master_secret : 1;
David Benjaminfc02b592017-02-17 16:26:01 -05001664
David Benjaminfd45ee72017-08-31 14:49:09 -04001665 // pending_private_key_op is true if there is a pending private key operation
David Benjaminc11ea9422017-08-29 16:33:21 -04001666 // in progress.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001667 bool pending_private_key_op : 1;
David Benjamin44148742017-06-17 13:20:59 -04001668
David Benjamina7bc9442018-01-18 10:08:53 -05001669 // grease_seeded is true if |grease_seed| has been initialized.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001670 bool grease_seeded : 1;
David Benjamina7bc9442018-01-18 10:08:53 -05001671
Adam Langleya307cb72018-05-02 09:06:48 -07001672 // handback indicates that a server should pause the handshake after
1673 // finishing operations that require private key material, in such a way that
David Benjaminf4928302019-08-21 16:04:53 -04001674 // |SSL_get_error| returns |SSL_ERROR_HANDBACK|. It is set by
1675 // |SSL_apply_handoff|.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001676 bool handback : 1;
Adam Langleya307cb72018-05-02 09:06:48 -07001677
1678 // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04001679 bool cert_compression_negotiated : 1;
Adam Langleya307cb72018-05-02 09:06:48 -07001680
David Benjamin6965d252018-11-19 15:49:56 -06001681 // apply_jdk11_workaround is true if the peer is probably a JDK 11 client
1682 // which implemented TLS 1.3 incorrectly.
1683 bool apply_jdk11_workaround : 1;
1684
David Benjaminc11ea9422017-08-29 16:33:21 -04001685 // client_version is the value sent or received in the ClientHello version.
David Benjamin8f288862017-07-20 14:01:44 -04001686 uint16_t client_version = 0;
Alessandro Ghedinide254b42017-04-17 19:12:33 +01001687
David Benjaminc11ea9422017-08-29 16:33:21 -04001688 // early_data_read is the amount of early data that has been read by the
1689 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04001690 uint16_t early_data_read = 0;
Steven Valdeze831a812017-03-09 14:56:07 -05001691
David Benjaminc11ea9422017-08-29 16:33:21 -04001692 // early_data_written is the amount of early data that has been written by the
1693 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04001694 uint16_t early_data_written = 0;
Adam Langley2f9b47f2017-12-29 14:15:55 -08001695
Steven Valdezb84674b2018-08-28 10:14:07 -04001696 // session_id is the session ID in the ClientHello.
Adam Langley2f9b47f2017-12-29 14:15:55 -08001697 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
1698 uint8_t session_id_len = 0;
David Benjamina7bc9442018-01-18 10:08:53 -05001699
1700 // grease_seed is the entropy for GREASE values. It is valid if
1701 // |grease_seeded| is true.
1702 uint8_t grease_seed[ssl_grease_last_index + 1] = {0};
David Benjamin86e95b82017-07-18 16:34:25 -04001703};
Steven Valdez143e8b32016-07-11 13:19:03 -04001704
David Benjamin8e7bbba2017-10-13 17:18:35 -04001705UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -04001706
David Benjaminc11ea9422017-08-29 16:33:21 -04001707// ssl_check_message_type checks if |msg| has type |type|. If so it returns
1708// one. Otherwise, it sends an alert and returns zero.
David Benjamin97250f42017-10-07 04:12:35 -04001709bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
David Benjamin276b7e82017-01-21 14:13:39 -05001710
David Benjaminc11ea9422017-08-29 16:33:21 -04001711// ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
1712// on error. It sets |out_early_return| to one if we've completed the handshake
1713// early.
David Benjamin74795b32017-08-31 15:13:12 -04001714int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -04001715
David Benjaminc11ea9422017-08-29 16:33:21 -04001716// The following are implementations of |do_handshake| for the client and
1717// server.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001718enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
1719enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +09001720enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
1721enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001722
David Benjaminc11ea9422017-08-29 16:33:21 -04001723// The following functions return human-readable representations of the TLS
1724// handshake states for debugging.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001725const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
1726const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
David Benjaminf60bcfb2017-08-18 15:23:44 -04001727const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
1728const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
1729
Adam Langleyba9ad662018-12-17 13:59:38 -08001730// tls13_add_key_update queues a KeyUpdate message on |ssl|. The
1731// |update_requested| argument must be one of |SSL_KEY_UPDATE_REQUESTED| or
1732// |SSL_KEY_UPDATE_NOT_REQUESTED|.
1733bool tls13_add_key_update(SSL *ssl, int update_requested);
1734
David Benjamin12f58782018-08-28 17:06:31 -05001735// tls13_post_handshake processes a post-handshake message. It returns true on
1736// success and false on failure.
1737bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
Steven Valdez8e1c7be2016-07-26 12:39:22 -04001738
David Benjamin12f58782018-08-28 17:06:31 -05001739bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1740 bool allow_anonymous);
1741bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjamin794cc592017-03-25 22:24:23 -05001742
David Benjaminc11ea9422017-08-29 16:33:21 -04001743// tls13_process_finished processes |msg| as a Finished message from the
David Benjamin12f58782018-08-28 17:06:31 -05001744// peer. If |use_saved_value| is true, the verify_data is compared against
David Benjaminc11ea9422017-08-29 16:33:21 -04001745// |hs->expected_client_finished| rather than computed fresh.
David Benjamin12f58782018-08-28 17:06:31 -05001746bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1747 bool use_saved_value);
Steven Valdez143e8b32016-07-11 13:19:03 -04001748
David Benjamin12f58782018-08-28 17:06:31 -05001749bool tls13_add_certificate(SSL_HANDSHAKE *hs);
David Benjamin44148742017-06-17 13:20:59 -04001750
David Benjaminc11ea9422017-08-29 16:33:21 -04001751// tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
1752// handshake. If it returns |ssl_private_key_retry|, it should be called again
1753// to retry when the signing operation is completed.
David Benjamin44148742017-06-17 13:20:59 -04001754enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
1755
David Benjamin12f58782018-08-28 17:06:31 -05001756bool tls13_add_finished(SSL_HANDSHAKE *hs);
1757bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
Steven Valdez143e8b32016-07-11 13:19:03 -04001758
David Benjamin31640932017-10-11 13:22:39 -04001759bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
1760 Array<uint8_t> *out_secret,
1761 uint8_t *out_alert, CBS *contents);
1762bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
1763 Array<uint8_t> *out_secret,
1764 uint8_t *out_alert, CBS *contents);
1765bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez143e8b32016-07-11 13:19:03 -04001766
David Benjamin31640932017-10-11 13:22:39 -04001767bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1768 uint8_t *out_alert,
1769 CBS *contents);
1770bool ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001771 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin9806ae02019-08-16 15:32:03 -04001772 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert,
1773 const SSL_CLIENT_HELLO *client_hello, CBS *contents);
David Benjamin31640932017-10-11 13:22:39 -04001774bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001775
David Benjaminc11ea9422017-08-29 16:33:21 -04001776// ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
David Benjamin861abcc2018-07-14 17:40:26 -04001777// returns whether it's valid.
1778bool ssl_is_sct_list_valid(const CBS *contents);
Adam Langleycfa08c32016-11-17 13:21:27 -08001779
David Benjamin12f58782018-08-28 17:06:31 -05001780bool ssl_write_client_hello(SSL_HANDSHAKE *hs);
Steven Valdez5440fe02016-07-18 12:40:30 -04001781
Nick Harper60a85cb2016-09-23 16:25:11 -07001782enum ssl_cert_verify_context_t {
1783 ssl_cert_verify_server,
1784 ssl_cert_verify_client,
1785 ssl_cert_verify_channel_id,
1786};
1787
David Benjaminc11ea9422017-08-29 16:33:21 -04001788// tls13_get_cert_verify_signature_input generates the message to be signed for
1789// TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
David Benjamin75a1f232017-10-11 17:19:19 -04001790// type of signature. It sets |*out| to a newly allocated buffer containing the
1791// result. This function returns true on success and false on failure.
1792bool tls13_get_cert_verify_signature_input(
1793 SSL_HANDSHAKE *hs, Array<uint8_t> *out,
Nick Harper60a85cb2016-09-23 16:25:11 -07001794 enum ssl_cert_verify_context_t cert_verify_context);
1795
David Benjamindd6c2e82017-10-17 15:48:46 -04001796// ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001797// selection for |hs->ssl|'s client preferences.
1798bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
1799 Span<const uint8_t> protocol);
David Benjamindd6c2e82017-10-17 15:48:46 -04001800
David Benjaminc11ea9422017-08-29 16:33:21 -04001801// ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
David Benjamin31640932017-10-11 13:22:39 -04001802// true on successful negotiation or if nothing was negotiated. It returns false
David Benjaminc11ea9422017-08-29 16:33:21 -04001803// and sets |*out_alert| to an alert on error.
David Benjamin31640932017-10-11 13:22:39 -04001804bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1805 const SSL_CLIENT_HELLO *client_hello);
David Benjamin9ef31f02016-10-31 18:01:13 -04001806
David Benjamin86e95b82017-07-18 16:34:25 -04001807struct SSL_EXTENSION_TYPE {
David Benjaminffb11072016-11-13 10:32:10 +09001808 uint16_t type;
David Benjamin74795b32017-08-31 15:13:12 -04001809 bool *out_present;
David Benjaminffb11072016-11-13 10:32:10 +09001810 CBS *out_data;
David Benjamin86e95b82017-07-18 16:34:25 -04001811};
David Benjaminffb11072016-11-13 10:32:10 +09001812
David Benjaminc11ea9422017-08-29 16:33:21 -04001813// ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
1814// it. It writes the parsed extensions to pointers denoted by |ext_types|. On
1815// success, it fills in the |out_present| and |out_data| fields and returns one.
1816// Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
1817// extensions are rejected unless |ignore_unknown| is 1.
David Benjaminffb11072016-11-13 10:32:10 +09001818int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
1819 const SSL_EXTENSION_TYPE *ext_types,
Steven Valdez08b65f42016-12-07 15:29:45 -05001820 size_t num_ext_types, int ignore_unknown);
David Benjaminffb11072016-11-13 10:32:10 +09001821
David Benjaminc11ea9422017-08-29 16:33:21 -04001822// ssl_verify_peer_cert verifies the peer certificate for |hs|.
David Benjamin3a1dd462017-07-11 16:13:10 -04001823enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
Jesse Selover1c337e52018-08-10 13:28:47 -04001824// ssl_reverify_peer_cert verifies the peer certificate for |hs| when resuming a
1825// session.
1826enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs);
David Benjamin3a1dd462017-07-11 16:13:10 -04001827
David Benjamin00f48c82017-10-06 18:43:53 -04001828enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
1829bool ssl_send_finished(SSL_HANDSHAKE *hs);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001830bool ssl_output_cert_chain(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001831
David Benjaminc11ea9422017-08-29 16:33:21 -04001832// SSLKEYLOGFILE functions.
David Benjamine776cc22016-07-19 07:26:49 +02001833
David Benjaminc11ea9422017-08-29 16:33:21 -04001834// ssl_log_secret logs |secret| with label |label|, if logging is enabled for
David Benjaminb244e3a2019-08-16 19:33:15 -04001835// |ssl|. It returns true on success and false on failure.
1836bool ssl_log_secret(const SSL *ssl, const char *label,
1837 Span<const uint8_t> secret);
David Benjamine776cc22016-07-19 07:26:49 +02001838
1839
David Benjaminc11ea9422017-08-29 16:33:21 -04001840// ClientHello functions.
David Benjamine14ff062016-08-09 16:21:24 -04001841
Adam Langleyc9827e02019-04-12 14:46:50 -07001842bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
David Benjamin861abcc2018-07-14 17:40:26 -04001843 const SSLMessage &msg);
David Benjamine14ff062016-08-09 16:21:24 -04001844
David Benjamin861abcc2018-07-14 17:40:26 -04001845bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
1846 CBS *out, uint16_t extension_type);
David Benjamine14ff062016-08-09 16:21:24 -04001847
David Benjamin861abcc2018-07-14 17:40:26 -04001848bool ssl_client_cipher_list_contains_cipher(
1849 const SSL_CLIENT_HELLO *client_hello, uint16_t id);
David Benjamine14ff062016-08-09 16:21:24 -04001850
1851
David Benjaminc11ea9422017-08-29 16:33:21 -04001852// GREASE.
David Benjamin65ac9972016-09-02 21:35:25 -04001853
David Benjamina7bc9442018-01-18 10:08:53 -05001854// ssl_get_grease_value returns a GREASE value for |hs|. For a given
David Benjaminc11ea9422017-08-29 16:33:21 -04001855// connection, the values for each index will be deterministic. This allows the
1856// same ClientHello be sent twice for a HelloRetryRequest or the same group be
1857// advertised in both supported_groups and key_shares.
David Benjamina7bc9442018-01-18 10:08:53 -05001858uint16_t ssl_get_grease_value(SSL_HANDSHAKE *hs, enum ssl_grease_index_t index);
David Benjamin65ac9972016-09-02 21:35:25 -04001859
1860
David Benjaminc11ea9422017-08-29 16:33:21 -04001861// Signature algorithms.
David Benjamin3ef76972016-10-17 17:59:54 -04001862
David Benjaminc11ea9422017-08-29 16:33:21 -04001863// tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
David Benjamin610cdbb2018-01-22 19:08:38 -05001864// algorithms and saves them on |hs|. It returns true on success and false on
David Benjaminc11ea9422017-08-29 16:33:21 -04001865// error.
David Benjamin610cdbb2018-01-22 19:08:38 -05001866bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
David Benjamin3ef76972016-10-17 17:59:54 -04001867
David Benjaminc11ea9422017-08-29 16:33:21 -04001868// tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
David Benjamin610cdbb2018-01-22 19:08:38 -05001869// that should be used with |pkey| in TLS 1.1 and earlier. It returns true on
1870// success and false if |pkey| may not be used at those versions.
1871bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
David Benjamina3651382017-04-20 17:49:36 -04001872
David Benjaminc11ea9422017-08-29 16:33:21 -04001873// tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
1874// with |hs|'s private key based on the peer's preferences and the algorithms
David Benjamin610cdbb2018-01-22 19:08:38 -05001875// supported. It returns true on success and false on error.
1876bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
David Benjamin3ef76972016-10-17 17:59:54 -04001877
Christopher Patton6c1b3762018-07-17 12:49:41 -07001878// tls1_get_peer_verify_algorithms returns the signature schemes for which the
1879// peer indicated support.
1880//
1881// NOTE: The related function |SSL_get0_peer_verify_algorithms| only has
1882// well-defined behavior during the callbacks set by |SSL_CTX_set_cert_cb| and
1883// |SSL_CTX_set_client_cert_cb|, or when the handshake is paused because of
1884// them.
1885Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs);
1886
David Benjaminc11ea9422017-08-29 16:33:21 -04001887// tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
David Benjamine28552d2018-04-08 13:59:25 -04001888// peer signature to |out|. It returns true on success and false on error. If
1889// |for_certs| is true, the potentially more restrictive list of algorithms for
1890// certificates is used. Otherwise, the online signature one is used.
1891bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out, bool for_certs);
David Benjamin3ef76972016-10-17 17:59:54 -04001892
David Benjaminc11ea9422017-08-29 16:33:21 -04001893// tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
David Benjamin610cdbb2018-01-22 19:08:38 -05001894// signature. It returns true on success and false on error, setting
1895// |*out_alert| to an alert to send.
1896bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
1897 uint16_t sigalg);
David Benjamin3ef76972016-10-17 17:59:54 -04001898
David Benjamine28552d2018-04-08 13:59:25 -04001899// tls12_has_different_verify_sigalgs_for_certs returns whether |ssl| has a
1900// different, more restrictive, list of signature algorithms acceptable for the
1901// certificate than the online signature.
1902bool tls12_has_different_verify_sigalgs_for_certs(const SSL *ssl);
1903
David Benjamin3ef76972016-10-17 17:59:54 -04001904
David Benjaminc11ea9422017-08-29 16:33:21 -04001905// Underdocumented functions.
1906//
1907// Functions below here haven't been touched up and may be underdocumented.
David Benjamin71f07942015-04-08 02:36:59 -04001908
Adam Langley1258b6a2014-06-20 12:00:00 -07001909#define TLSEXT_CHANNEL_ID_SIZE 128
Adam Langley95c29f32014-06-20 12:00:00 -07001910
David Benjaminc11ea9422017-08-29 16:33:21 -04001911// From RFC4492, used in encoding the curve type in ECParameters
Adam Langleyfcf25832014-12-18 17:42:32 -08001912#define NAMED_CURVE_TYPE 3
Adam Langley95c29f32014-06-20 12:00:00 -07001913
David Benjamin664e99a2017-10-10 19:23:21 -04001914struct CERT {
David Benjamine325c3f2018-04-12 16:11:15 -04001915 static constexpr bool kAllowUniquePtr = true;
1916
1917 explicit CERT(const SSL_X509_METHOD *x509_method);
1918 ~CERT();
1919
1920 UniquePtr<EVP_PKEY> privatekey;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001921
David Benjaminc11ea9422017-08-29 16:33:21 -04001922 // chain contains the certificate chain, with the leaf at the beginning. The
1923 // first element of |chain| may be NULL to indicate that the leaf certificate
1924 // has not yet been set.
1925 // If |chain| != NULL -> len(chain) >= 1
1926 // If |chain[0]| == NULL -> len(chain) >= 2.
1927 // |chain[1..]| != NULL
David Benjamine325c3f2018-04-12 16:11:15 -04001928 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001929
David Benjaminc11ea9422017-08-29 16:33:21 -04001930 // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
1931 // a cache in order to implement “get0” functions that return a non-owning
1932 // pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04001933 STACK_OF(X509) *x509_chain = nullptr;
David Benjaminf31e6812014-11-13 18:05:55 -05001934
David Benjaminc11ea9422017-08-29 16:33:21 -04001935 // x509_leaf may contain a parsed copy of the first element of |chain|. This
1936 // is only used as a cache in order to implement “get0” functions that return
1937 // a non-owning pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04001938 X509 *x509_leaf = nullptr;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001939
David Benjaminc11ea9422017-08-29 16:33:21 -04001940 // x509_stash contains the last |X509| object append to the chain. This is a
1941 // workaround for some third-party code that continue to use an |X509| object
1942 // even after passing ownership with an “add0” function.
David Benjamine325c3f2018-04-12 16:11:15 -04001943 X509 *x509_stash = nullptr;
Adam Langleye1e78132017-01-31 15:24:31 -08001944
David Benjaminc11ea9422017-08-29 16:33:21 -04001945 // key_method, if non-NULL, is a set of callbacks to call for private key
1946 // operations.
David Benjamine325c3f2018-04-12 16:11:15 -04001947 const SSL_PRIVATE_KEY_METHOD *key_method = nullptr;
David Benjaminb4d65fd2015-05-29 17:11:21 -04001948
David Benjaminc11ea9422017-08-29 16:33:21 -04001949 // x509_method contains pointers to functions that might deal with |X509|
1950 // compatibility, or might be a no-op, depending on the application.
David Benjamine325c3f2018-04-12 16:11:15 -04001951 const SSL_X509_METHOD *x509_method = nullptr;
Adam Langley3509dac2017-02-01 11:59:18 -08001952
David Benjamine325c3f2018-04-12 16:11:15 -04001953 // sigalgs, if non-empty, is the set of signature algorithms supported by
David Benjaminc11ea9422017-08-29 16:33:21 -04001954 // |privatekey| in decreasing order of preference.
David Benjamine325c3f2018-04-12 16:11:15 -04001955 Array<uint16_t> sigalgs;
Adam Langley95c29f32014-06-20 12:00:00 -07001956
David Benjaminc11ea9422017-08-29 16:33:21 -04001957 // Certificate setup callback: if set is called whenever a
1958 // certificate may be required (client or server). the callback
1959 // can then examine any appropriate parameters and setup any
1960 // certificates required. This allows advanced applications
1961 // to select certificates on the fly: for example based on
1962 // supported signature algorithms or curves.
David Benjamine325c3f2018-04-12 16:11:15 -04001963 int (*cert_cb)(SSL *ssl, void *arg) = nullptr;
1964 void *cert_cb_arg = nullptr;
Adam Langleyd323f4b2016-03-01 15:58:14 -08001965
David Benjaminc11ea9422017-08-29 16:33:21 -04001966 // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
1967 // store is used instead.
David Benjamine325c3f2018-04-12 16:11:15 -04001968 X509_STORE *verify_store = nullptr;
David Benjamin83a32122017-02-14 18:34:54 -05001969
David Benjaminc11ea9422017-08-29 16:33:21 -04001970 // Signed certificate timestamp list to be sent to the client, if requested
David Benjamine325c3f2018-04-12 16:11:15 -04001971 UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin83a32122017-02-14 18:34:54 -05001972
David Benjaminc11ea9422017-08-29 16:33:21 -04001973 // OCSP response to be sent to the client, if requested.
David Benjamine325c3f2018-04-12 16:11:15 -04001974 UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin5960a902017-02-14 20:07:11 -05001975
David Benjaminc11ea9422017-08-29 16:33:21 -04001976 // sid_ctx partitions the session space within a shared session cache or
1977 // ticket key. Only sessions with a matching value will be accepted.
David Benjamine325c3f2018-04-12 16:11:15 -04001978 uint8_t sid_ctx_length = 0;
1979 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
Christopher Patton6c1b3762018-07-17 12:49:41 -07001980
1981 // Delegated credentials.
1982
1983 // dc is the delegated credential to send to the peer (if requested).
1984 UniquePtr<DC> dc = nullptr;
1985
1986 // dc_privatekey is used instead of |privatekey| or |key_method| to
1987 // authenticate the host if a delegated credential is used in the handshake.
1988 UniquePtr<EVP_PKEY> dc_privatekey = nullptr;
1989
1990 // dc_key_method, if not NULL, is used instead of |dc_privatekey| to
1991 // authenticate the host.
1992 const SSL_PRIVATE_KEY_METHOD *dc_key_method = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04001993};
Adam Langley95c29f32014-06-20 12:00:00 -07001994
David Benjamin664e99a2017-10-10 19:23:21 -04001995// |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
1996struct SSL_PROTOCOL_METHOD {
David Benjamin97250f42017-10-07 04:12:35 -04001997 bool is_dtls;
1998 bool (*ssl_new)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04001999 void (*ssl_free)(SSL *ssl);
2000 // get_message sets |*out| to the current handshake message and returns true
2001 // if one has been received. It returns false if more input is needed.
Adam Langleyc9827e02019-04-12 14:46:50 -07002002 bool (*get_message)(const SSL *ssl, SSLMessage *out);
David Benjamin664e99a2017-10-10 19:23:21 -04002003 // next_message is called to release the current handshake message.
2004 void (*next_message)(SSL *ssl);
David Benjaminf6632da2017-10-12 19:11:47 -04002005 // Use the |ssl_open_handshake| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04002006 ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
2007 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04002008 // Use the |ssl_open_change_cipher_spec| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04002009 ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
2010 uint8_t *out_alert,
2011 Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04002012 // Use the |ssl_open_app_data| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04002013 ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
2014 size_t *out_consumed, uint8_t *out_alert,
2015 Span<uint8_t> in);
David Benjamin664e99a2017-10-10 19:23:21 -04002016 int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
2017 int len);
2018 int (*dispatch_alert)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04002019 // init_message begins a new handshake message of type |type|. |cbb| is the
2020 // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
David Benjamin97250f42017-10-07 04:12:35 -04002021 // the caller should write to. It returns true on success and false on error.
2022 bool (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
David Benjamin664e99a2017-10-10 19:23:21 -04002023 // finish_message finishes a handshake message. It sets |*out_msg| to the
David Benjamin97250f42017-10-07 04:12:35 -04002024 // serialized message. It returns true on success and false on error.
2025 bool (*finish_message)(SSL *ssl, CBB *cbb, bssl::Array<uint8_t> *out_msg);
2026 // add_message adds a handshake message to the pending flight. It returns
2027 // true on success and false on error.
2028 bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
David Benjamin664e99a2017-10-10 19:23:21 -04002029 // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
David Benjamin97250f42017-10-07 04:12:35 -04002030 // flight. It returns true on success and false on error.
2031 bool (*add_change_cipher_spec)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04002032 // flush_flight flushes the pending flight to the transport. It returns one on
2033 // success and <= 0 on error.
2034 int (*flush_flight)(SSL *ssl);
2035 // on_handshake_complete is called when the handshake is complete.
2036 void (*on_handshake_complete)(SSL *ssl);
2037 // set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It returns
David Benjamin97250f42017-10-07 04:12:35 -04002038 // true on success and false if changing the read state is forbidden at this
David Benjamin664e99a2017-10-10 19:23:21 -04002039 // point.
David Benjamin97250f42017-10-07 04:12:35 -04002040 bool (*set_read_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
David Benjamin664e99a2017-10-10 19:23:21 -04002041 // set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It returns
David Benjamin97250f42017-10-07 04:12:35 -04002042 // true on success and false if changing the write state is forbidden at this
David Benjamin664e99a2017-10-10 19:23:21 -04002043 // point.
David Benjamin97250f42017-10-07 04:12:35 -04002044 bool (*set_write_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
David Benjamin664e99a2017-10-10 19:23:21 -04002045};
2046
David Benjaminf6632da2017-10-12 19:11:47 -04002047// The following wrappers call |open_*| but handle |read_shutdown| correctly.
2048
2049// ssl_open_handshake processes a record from |in| for reading a handshake
2050// message.
2051ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
2052 uint8_t *out_alert, Span<uint8_t> in);
2053
2054// ssl_open_change_cipher_spec processes a record from |in| for reading a
2055// ChangeCipherSpec.
2056ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2057 uint8_t *out_alert,
2058 Span<uint8_t> in);
2059
2060// ssl_open_app_data processes a record from |in| for reading application data.
2061// On success, it returns |ssl_open_record_success| and sets |*out| to the
2062// input. If it encounters a post-handshake message, it returns
2063// |ssl_open_record_discard|. The caller should then retry, after processing any
2064// messages received with |get_message|.
2065ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
2066 size_t *out_consumed, uint8_t *out_alert,
2067 Span<uint8_t> in);
2068
David Benjamin0363de92018-06-29 13:57:38 -04002069struct SSL_X509_METHOD {
2070 // check_client_CA_list returns one if |names| is a good list of X.509
2071 // distinguished names and zero otherwise. This is used to ensure that we can
2072 // reject unparsable values at handshake time when using crypto/x509.
David Benjamin1e77ef42019-03-30 01:03:50 -05002073 bool (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
David Benjamin0363de92018-06-29 13:57:38 -04002074
2075 // cert_clear frees and NULLs all X509 certificate-related state.
2076 void (*cert_clear)(CERT *cert);
2077 // cert_free frees all X509-related state.
2078 void (*cert_free)(CERT *cert);
2079 // cert_flush_cached_chain drops any cached |X509|-based certificate chain
2080 // from |cert|.
2081 // cert_dup duplicates any needed fields from |cert| to |new_cert|.
2082 void (*cert_dup)(CERT *new_cert, const CERT *cert);
2083 void (*cert_flush_cached_chain)(CERT *cert);
2084 // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
2085 // from |cert|.
2086 void (*cert_flush_cached_leaf)(CERT *cert);
2087
2088 // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
2089 // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
David Benjamin1e77ef42019-03-30 01:03:50 -05002090 // true on success or false on error.
2091 bool (*session_cache_objects)(SSL_SESSION *session);
David Benjamin0363de92018-06-29 13:57:38 -04002092 // session_dup duplicates any needed fields from |session| to |new_session|.
David Benjamin1e77ef42019-03-30 01:03:50 -05002093 // It returns true on success or false on error.
2094 bool (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
David Benjamin0363de92018-06-29 13:57:38 -04002095 // session_clear frees any X509-related state from |session|.
2096 void (*session_clear)(SSL_SESSION *session);
2097 // session_verify_cert_chain verifies the certificate chain in |session|,
David Benjamin1e77ef42019-03-30 01:03:50 -05002098 // sets |session->verify_result| and returns true on success or false on
David Benjamin0363de92018-06-29 13:57:38 -04002099 // error.
David Benjamin1e77ef42019-03-30 01:03:50 -05002100 bool (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl,
2101 uint8_t *out_alert);
David Benjamin0363de92018-06-29 13:57:38 -04002102
2103 // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
2104 void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs);
Adam Langleyffe384c2019-05-01 11:13:12 -07002105 // ssl_new does any necessary initialisation of |hs|. It returns true on
David Benjamin1e77ef42019-03-30 01:03:50 -05002106 // success or false on error.
2107 bool (*ssl_new)(SSL_HANDSHAKE *hs);
David Benjamin0363de92018-06-29 13:57:38 -04002108 // ssl_free frees anything created by |ssl_new|.
2109 void (*ssl_config_free)(SSL_CONFIG *cfg);
2110 // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
2111 void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg);
2112 // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
2113 // necessary. On success, it updates |ssl|'s certificate configuration as
David Benjamin1e77ef42019-03-30 01:03:50 -05002114 // needed and returns true. Otherwise, it returns false.
2115 bool (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs);
Adam Langleyffe384c2019-05-01 11:13:12 -07002116 // ssl_ctx_new does any necessary initialisation of |ctx|. It returns true on
David Benjamin1e77ef42019-03-30 01:03:50 -05002117 // success or false on error.
2118 bool (*ssl_ctx_new)(SSL_CTX *ctx);
David Benjamin0363de92018-06-29 13:57:38 -04002119 // ssl_ctx_free frees anything created by |ssl_ctx_new|.
2120 void (*ssl_ctx_free)(SSL_CTX *ctx);
2121 // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
2122 void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
2123};
2124
David Benjaminc11ea9422017-08-29 16:33:21 -04002125// ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
2126// crypto/x509.
David Benjamin86e95b82017-07-18 16:34:25 -04002127extern const SSL_X509_METHOD ssl_crypto_x509_method;
Adam Langley3509dac2017-02-01 11:59:18 -08002128
David Benjaminc11ea9422017-08-29 16:33:21 -04002129// ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
2130// crypto/x509.
David Benjaminba2d3df2017-08-04 13:59:24 -04002131extern const SSL_X509_METHOD ssl_noop_x509_method;
2132
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002133struct TicketKey {
David Benjamin664e99a2017-10-10 19:23:21 -04002134 static constexpr bool kAllowUniquePtr = true;
2135
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002136 uint8_t name[SSL_TICKET_KEY_NAME_LEN] = {0};
2137 uint8_t hmac_key[16] = {0};
2138 uint8_t aes_key[16] = {0};
David Benjamin664e99a2017-10-10 19:23:21 -04002139 // next_rotation_tv_sec is the time (in seconds from the epoch) when the
2140 // current key should be superseded by a new key, or the time when a previous
2141 // key should be dropped. If zero, then the key should not be automatically
2142 // rotated.
David Benjamin7bb0fbf2018-07-03 13:55:42 -04002143 uint64_t next_rotation_tv_sec = 0;
David Benjamin664e99a2017-10-10 19:23:21 -04002144};
2145
David Benjamin35b4a122018-07-14 17:04:41 -04002146struct CertCompressionAlg {
2147 static constexpr bool kAllowUniquePtr = true;
2148
2149 ssl_cert_compression_func_t compress = nullptr;
2150 ssl_cert_decompression_func_t decompress = nullptr;
2151 uint16_t alg_id = 0;
2152};
2153
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002154BSSL_NAMESPACE_END
David Benjamin4e840352017-10-11 18:41:11 -04002155
David Benjamin1eff9482018-09-24 14:10:29 -05002156DEFINE_LHASH_OF(SSL_SESSION)
David Benjamin4e840352017-10-11 18:41:11 -04002157
David Benjaminf109f202019-02-21 10:49:57 -06002158DEFINE_NAMED_STACK_OF(CertCompressionAlg, bssl::CertCompressionAlg)
Adam Langleya307cb72018-05-02 09:06:48 -07002159
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002160BSSL_NAMESPACE_BEGIN
David Benjamin4e840352017-10-11 18:41:11 -04002161
David Benjaminc11ea9422017-08-29 16:33:21 -04002162// An ssl_shutdown_t describes the shutdown state of one end of the connection,
2163// whether it is alive or has been shutdown via close_notify or fatal alert.
David Benjamine228bd22016-10-17 20:41:08 -04002164enum ssl_shutdown_t {
2165 ssl_shutdown_none = 0,
2166 ssl_shutdown_close_notify = 1,
David Benjamin31aad2d2017-10-06 16:04:06 -04002167 ssl_shutdown_error = 2,
David Benjamine228bd22016-10-17 20:41:08 -04002168};
2169
David Benjamin86e95b82017-07-18 16:34:25 -04002170struct SSL3_STATE {
David Benjamin94172572017-10-13 16:53:21 -04002171 static constexpr bool kAllowUniquePtr = true;
David Benjamine228bd22016-10-17 20:41:08 -04002172
David Benjamin94172572017-10-13 16:53:21 -04002173 SSL3_STATE();
2174 ~SSL3_STATE();
2175
2176 uint8_t read_sequence[8] = {0};
2177 uint8_t write_sequence[8] = {0};
2178
2179 uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
2180 uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
David Benjamine228bd22016-10-17 20:41:08 -04002181
David Benjaminc11ea9422017-08-29 16:33:21 -04002182 // read_buffer holds data from the transport to be processed.
David Benjaminea712e32017-10-13 16:50:39 -04002183 SSLBuffer read_buffer;
David Benjaminc11ea9422017-08-29 16:33:21 -04002184 // write_buffer holds data to be written to the transport.
David Benjaminea712e32017-10-13 16:50:39 -04002185 SSLBuffer write_buffer;
David Benjamine228bd22016-10-17 20:41:08 -04002186
David Benjamind9229f92017-10-06 17:36:20 -04002187 // pending_app_data is the unconsumed application data. It points into
2188 // |read_buffer|.
2189 Span<uint8_t> pending_app_data;
David Benjamine228bd22016-10-17 20:41:08 -04002190
David Benjaminc11ea9422017-08-29 16:33:21 -04002191 // partial write - check the numbers match
David Benjamin94172572017-10-13 16:53:21 -04002192 unsigned int wnum = 0; // number of bytes sent so far
2193 int wpend_tot = 0; // number bytes written
2194 int wpend_type = 0;
2195 int wpend_ret = 0; // number of bytes submitted
2196 const uint8_t *wpend_buf = nullptr;
David Benjamine228bd22016-10-17 20:41:08 -04002197
David Benjamin23c25d52017-10-06 16:02:47 -04002198 // read_shutdown is the shutdown state for the read half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002199 enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002200
David Benjamin23c25d52017-10-06 16:02:47 -04002201 // write_shutdown is the shutdown state for the write half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002202 enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002203
David Benjamin31aad2d2017-10-06 16:04:06 -04002204 // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
2205 // the receive half of the connection.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002206 UniquePtr<ERR_SAVE_STATE> read_error;
David Benjamin31aad2d2017-10-06 16:04:06 -04002207
David Benjamin94172572017-10-13 16:53:21 -04002208 int total_renegotiations = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002209
David Benjamin32ce0ac2017-10-13 19:17:22 -04002210 // This holds a variable that indicates what we were doing when a 0 or -1 is
2211 // returned. This is needed for non-blocking IO so we know what request
2212 // needs re-doing when in SSL_accept or SSL_connect
David Benjaminf4928302019-08-21 16:04:53 -04002213 int rwstate = SSL_ERROR_NONE;
David Benjamin32ce0ac2017-10-13 19:17:22 -04002214
Steven Valdezc8e0f902018-07-14 11:23:01 -04002215 enum ssl_encryption_level_t read_level = ssl_encryption_initial;
2216 enum ssl_encryption_level_t write_level = ssl_encryption_initial;
2217
David Benjaminc11ea9422017-08-29 16:33:21 -04002218 // early_data_skipped is the amount of early data that has been skipped by the
2219 // record layer.
David Benjamin94172572017-10-13 16:53:21 -04002220 uint16_t early_data_skipped = 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002221
David Benjaminc11ea9422017-08-29 16:33:21 -04002222 // empty_record_count is the number of consecutive empty records received.
David Benjamin94172572017-10-13 16:53:21 -04002223 uint8_t empty_record_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002224
David Benjaminc11ea9422017-08-29 16:33:21 -04002225 // warning_alert_count is the number of consecutive warning alerts
2226 // received.
David Benjamin94172572017-10-13 16:53:21 -04002227 uint8_t warning_alert_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002228
David Benjaminc11ea9422017-08-29 16:33:21 -04002229 // key_update_count is the number of consecutive KeyUpdates received.
David Benjamin94172572017-10-13 16:53:21 -04002230 uint8_t key_update_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002231
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002232 // The negotiated Token Binding key parameter. Only valid if
2233 // |token_binding_negotiated| is set.
2234 uint8_t negotiated_token_binding_param = 0;
2235
David Benjaminc11ea9422017-08-29 16:33:21 -04002236 // skip_early_data instructs the record layer to skip unexpected early data
2237 // messages when 0RTT is rejected.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002238 bool skip_early_data : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002239
David Benjaminc11ea9422017-08-29 16:33:21 -04002240 // have_version is true if the connection's final version is known. Otherwise
2241 // the version has not been negotiated yet.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002242 bool have_version : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002243
David Benjaminc11ea9422017-08-29 16:33:21 -04002244 // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
2245 // and future messages should use the record layer.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002246 bool v2_hello_done : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002247
David Benjaminc11ea9422017-08-29 16:33:21 -04002248 // is_v2_hello is true if the current handshake message was derived from a
2249 // V2ClientHello rather than received from the peer directly.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002250 bool is_v2_hello : 1;
David Benjamin1a444da2017-01-21 14:27:45 -05002251
David Benjaminc11ea9422017-08-29 16:33:21 -04002252 // has_message is true if the current handshake message has been returned
2253 // at least once by |get_message| and false otherwise.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002254 bool has_message : 1;
David Benjamin7934f082017-08-01 16:32:25 -04002255
David Benjaminc11ea9422017-08-29 16:33:21 -04002256 // initial_handshake_complete is true if the initial handshake has
2257 // completed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002258 bool initial_handshake_complete : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002259
David Benjaminc11ea9422017-08-29 16:33:21 -04002260 // session_reused indicates whether a session was resumed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002261 bool session_reused : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002262
Watson Ladd629f3212019-06-11 17:46:52 -07002263 // delegated_credential_used is whether we presented a delegated credential to
2264 // the peer.
2265 bool delegated_credential_used : 1;
2266
Jesse Selover6b0d8222018-08-14 12:47:29 -04002267 bool send_connection_binding : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002268
David Benjaminc11ea9422017-08-29 16:33:21 -04002269 // In a client, this means that the server supported Channel ID and that a
2270 // Channel ID was sent. In a server it means that we echoed support for
David Benjamin46853762018-07-03 14:01:26 -04002271 // Channel IDs and that |channel_id| will be valid after the handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002272 bool channel_id_valid : 1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002273
David Benjamin046bc1f2017-08-31 15:06:42 -04002274 // key_update_pending is true if we have a KeyUpdate acknowledgment
David Benjaminc11ea9422017-08-29 16:33:21 -04002275 // outstanding.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002276 bool key_update_pending : 1;
David Benjaminbbba9392017-04-06 12:54:12 -04002277
David Benjamin046bc1f2017-08-31 15:06:42 -04002278 // wpend_pending is true if we have a pending write outstanding.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002279 bool wpend_pending : 1;
Steven Valdeze831a812017-03-09 14:56:07 -05002280
David Benjamin02e62562017-12-18 18:04:01 -05002281 // early_data_accepted is true if early data was accepted by the server.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002282 bool early_data_accepted : 1;
David Benjamin02e62562017-12-18 18:04:01 -05002283
Steven Valdezf1af1292018-08-13 10:54:48 -04002284 // tls13_downgrade is whether the TLS 1.3 anti-downgrade logic fired.
2285 bool tls13_downgrade : 1;
David Benjamin6df65402017-12-18 18:00:23 -05002286
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002287 // token_binding_negotiated is set if Token Binding was negotiated.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002288 bool token_binding_negotiated : 1;
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002289
Adam Langleya86c6982019-07-16 15:26:21 -07002290 // pq_experimental_signal_seen is true if the peer was observed
2291 // sending/echoing the post-quantum experiment signal.
2292 bool pq_experiment_signal_seen : 1;
2293
David Benjaminee4888c2019-07-21 12:16:52 -04002294 // alert_dispatch is true there is an alert in |send_alert| to be sent.
2295 bool alert_dispatch : 1;
2296
David Benjamin0e7dbd52019-05-15 16:01:18 -04002297 // renegotiate_pending is whether the read half of the channel is blocked on a
2298 // HelloRequest.
2299 bool renegotiate_pending : 1;
2300
David Benjamin32ce0ac2017-10-13 19:17:22 -04002301 // hs_buf is the buffer of handshake data to process.
2302 UniquePtr<BUF_MEM> hs_buf;
2303
David Benjamin700631b2018-05-24 17:17:34 -04002304 // pending_hs_data contains the pending handshake data that has not yet
2305 // been encrypted to |pending_flight|. This allows packing the handshake into
2306 // fewer records.
2307 UniquePtr<BUF_MEM> pending_hs_data;
2308
David Benjaminc11ea9422017-08-29 16:33:21 -04002309 // pending_flight is the pending outgoing flight. This is used to flush each
2310 // handshake flight in a single write. |write_buffer| must be written out
2311 // before this data.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002312 UniquePtr<BUF_MEM> pending_flight;
David Benjamindaf207a2017-01-03 18:37:41 -05002313
David Benjaminc11ea9422017-08-29 16:33:21 -04002314 // pending_flight_offset is the number of bytes of |pending_flight| which have
2315 // been successfully written.
David Benjamin94172572017-10-13 16:53:21 -04002316 uint32_t pending_flight_offset = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002317
Adam Langley2f9b47f2017-12-29 14:15:55 -08002318 // ticket_age_skew is the difference, in seconds, between the client-sent
2319 // ticket age and the server-computed value in TLS 1.3 server connections
2320 // which resumed a session.
2321 int32_t ticket_age_skew = 0;
2322
David Benjamin64770122019-05-04 11:00:04 -05002323 // ssl_early_data_reason stores details on why 0-RTT was accepted or rejected.
2324 enum ssl_early_data_reason_t early_data_reason = ssl_early_data_unknown;
2325
David Benjaminc11ea9422017-08-29 16:33:21 -04002326 // aead_read_ctx is the current read cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002327 UniquePtr<SSLAEADContext> aead_read_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002328
David Benjaminc11ea9422017-08-29 16:33:21 -04002329 // aead_write_ctx is the current write cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002330 UniquePtr<SSLAEADContext> aead_write_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002331
David Benjaminc11ea9422017-08-29 16:33:21 -04002332 // hs is the handshake state for the current handshake or NULL if there isn't
2333 // one.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002334 UniquePtr<SSL_HANDSHAKE> hs;
David Benjamine228bd22016-10-17 20:41:08 -04002335
David Benjaminbc424022019-07-21 12:05:20 -04002336 uint8_t write_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2337 uint8_t read_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2338 uint8_t exporter_secret[SSL_MAX_MD_SIZE] = {0};
David Benjamin94172572017-10-13 16:53:21 -04002339 uint8_t write_traffic_secret_len = 0;
2340 uint8_t read_traffic_secret_len = 0;
2341 uint8_t exporter_secret_len = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002342
David Benjaminc11ea9422017-08-29 16:33:21 -04002343 // Connection binding to prevent renegotiation attacks
David Benjamin94172572017-10-13 16:53:21 -04002344 uint8_t previous_client_finished[12] = {0};
2345 uint8_t previous_client_finished_len = 0;
2346 uint8_t previous_server_finished_len = 0;
2347 uint8_t previous_server_finished[12] = {0};
Adam Langley4ba6e192016-12-07 15:54:54 -08002348
Adam Langley2f9b47f2017-12-29 14:15:55 -08002349 uint8_t send_alert[2] = {0};
2350
David Benjaminc11ea9422017-08-29 16:33:21 -04002351 // established_session is the session established by the connection. This
2352 // session is only filled upon the completion of the handshake and is
2353 // immutable.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002354 UniquePtr<SSL_SESSION> established_session;
David Benjamine228bd22016-10-17 20:41:08 -04002355
David Benjaminc11ea9422017-08-29 16:33:21 -04002356 // Next protocol negotiation. For the client, this is the protocol that we
2357 // sent in NextProtocol and is set when handling ServerHello extensions.
2358 //
2359 // For a server, this is the client's selected_protocol from NextProtocol and
2360 // is set when handling the NextProtocol message, before the Finished
2361 // message.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002362 Array<uint8_t> next_proto_negotiated;
David Benjamine228bd22016-10-17 20:41:08 -04002363
David Benjaminc11ea9422017-08-29 16:33:21 -04002364 // ALPN information
2365 // (we are in the process of transitioning from NPN to ALPN.)
David Benjamine228bd22016-10-17 20:41:08 -04002366
David Benjaminc11ea9422017-08-29 16:33:21 -04002367 // In a server these point to the selected ALPN protocol after the
2368 // ClientHello has been processed. In a client these contain the protocol
2369 // that the server selected once the ServerHello has been processed.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002370 Array<uint8_t> alpn_selected;
David Benjamine228bd22016-10-17 20:41:08 -04002371
David Benjamina8614602017-09-06 15:40:19 -04002372 // hostname, on the server, is the value of the SNI extension.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002373 UniquePtr<char> hostname;
David Benjamina8614602017-09-06 15:40:19 -04002374
David Benjaminc11ea9422017-08-29 16:33:21 -04002375 // For a server:
David Benjamin46853762018-07-03 14:01:26 -04002376 // If |channel_id_valid| is true, then this contains the
David Benjaminc11ea9422017-08-29 16:33:21 -04002377 // verified Channel ID from the client: a P256 point, (x,y), where
2378 // each are big-endian values.
David Benjamin46853762018-07-03 14:01:26 -04002379 uint8_t channel_id[64] = {0};
Nick Harper3c034b22017-12-22 15:50:43 -08002380
2381 // Contains the QUIC transport params received by the peer.
2382 Array<uint8_t> peer_quic_transport_params;
David Benjaminfceca8e2018-04-12 16:37:19 -04002383
2384 // srtp_profile is the selected SRTP protection profile for
2385 // DTLS-SRTP.
2386 const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002387};
David Benjamine228bd22016-10-17 20:41:08 -04002388
David Benjaminc11ea9422017-08-29 16:33:21 -04002389// lengths of messages
David Benjamin593047f2015-05-08 13:08:52 -04002390#define DTLS1_COOKIE_LENGTH 256
2391
2392#define DTLS1_RT_HEADER_LENGTH 13
2393
2394#define DTLS1_HM_HEADER_LENGTH 12
2395
2396#define DTLS1_CCS_HEADER_LENGTH 1
2397
2398#define DTLS1_AL_HEADER_LENGTH 2
2399
David Benjamin593047f2015-05-08 13:08:52 -04002400struct hm_header_st {
2401 uint8_t type;
2402 uint32_t msg_len;
2403 uint16_t seq;
2404 uint32_t frag_off;
2405 uint32_t frag_len;
David Benjamin593047f2015-05-08 13:08:52 -04002406};
2407
David Benjaminc11ea9422017-08-29 16:33:21 -04002408// An hm_fragment is an incoming DTLS message, possibly not yet assembled.
David Benjamin86e95b82017-07-18 16:34:25 -04002409struct hm_fragment {
David Benjamin049fdfc2017-10-17 01:12:53 -04002410 static constexpr bool kAllowUniquePtr = true;
2411
2412 hm_fragment() {}
2413 hm_fragment(const hm_fragment &) = delete;
2414 hm_fragment &operator=(const hm_fragment &) = delete;
2415
2416 ~hm_fragment();
2417
David Benjaminc11ea9422017-08-29 16:33:21 -04002418 // type is the type of the message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002419 uint8_t type = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002420 // seq is the sequence number of this message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002421 uint16_t seq = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002422 // msg_len is the length of the message body.
David Benjamin049fdfc2017-10-17 01:12:53 -04002423 uint32_t msg_len = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002424 // data is a pointer to the message, including message header. It has length
2425 // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
David Benjamin049fdfc2017-10-17 01:12:53 -04002426 uint8_t *data = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04002427 // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
2428 // the message have been received. It is NULL if the message is complete.
David Benjamin049fdfc2017-10-17 01:12:53 -04002429 uint8_t *reassembly = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002430};
David Benjamin593047f2015-05-08 13:08:52 -04002431
David Benjaminad8f5e12017-02-20 17:00:20 -05002432struct OPENSSL_timeval {
2433 uint64_t tv_sec;
2434 uint32_t tv_usec;
2435};
2436
David Benjamin86e95b82017-07-18 16:34:25 -04002437struct DTLS1_STATE {
David Benjamin800046f2017-10-17 01:37:51 -04002438 static constexpr bool kAllowUniquePtr = true;
2439
2440 DTLS1_STATE();
2441 ~DTLS1_STATE();
2442
David Benjaminc11ea9422017-08-29 16:33:21 -04002443 // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
2444 // the peer in this epoch.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002445 bool has_change_cipher_spec : 1;
David Benjamin593047f2015-05-08 13:08:52 -04002446
David Benjaminc11ea9422017-08-29 16:33:21 -04002447 // outgoing_messages_complete is true if |outgoing_messages| has been
2448 // completed by an attempt to flush it. Future calls to |add_message| and
2449 // |add_change_cipher_spec| will start a new flight.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002450 bool outgoing_messages_complete : 1;
David Benjamin9bbdf582017-08-02 19:46:29 -04002451
David Benjaminc11ea9422017-08-29 16:33:21 -04002452 // flight_has_reply is true if the current outgoing flight is complete and has
2453 // processed at least one message. This is used to detect whether we or the
2454 // peer sent the final flight.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002455 bool flight_has_reply : 1;
David Benjamin302b8182017-08-22 14:47:22 -07002456
David Benjamin800046f2017-10-17 01:37:51 -04002457 uint8_t cookie[DTLS1_COOKIE_LENGTH] = {0};
2458 size_t cookie_len = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002459
David Benjaminc11ea9422017-08-29 16:33:21 -04002460 // The current data and handshake epoch. This is initially undefined, and
2461 // starts at zero once the initial handshake is completed.
David Benjamin800046f2017-10-17 01:37:51 -04002462 uint16_t r_epoch = 0;
2463 uint16_t w_epoch = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002464
David Benjaminc11ea9422017-08-29 16:33:21 -04002465 // records being received in the current epoch
David Benjamin593047f2015-05-08 13:08:52 -04002466 DTLS1_BITMAP bitmap;
2467
David Benjamin800046f2017-10-17 01:37:51 -04002468 uint16_t handshake_write_seq = 0;
2469 uint16_t handshake_read_seq = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002470
David Benjaminc11ea9422017-08-29 16:33:21 -04002471 // save last sequence number for retransmissions
David Benjamin800046f2017-10-17 01:37:51 -04002472 uint8_t last_write_sequence[8] = {0};
2473 UniquePtr<SSLAEADContext> last_aead_write_ctx;
David Benjamin593047f2015-05-08 13:08:52 -04002474
David Benjaminc11ea9422017-08-29 16:33:21 -04002475 // incoming_messages is a ring buffer of incoming handshake messages that have
2476 // yet to be processed. The front of the ring buffer is message number
2477 // |handshake_read_seq|, at position |handshake_read_seq| %
2478 // |SSL_MAX_HANDSHAKE_FLIGHT|.
David Benjamin800046f2017-10-17 01:37:51 -04002479 UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin593047f2015-05-08 13:08:52 -04002480
David Benjaminc11ea9422017-08-29 16:33:21 -04002481 // outgoing_messages is the queue of outgoing messages from the last handshake
2482 // flight.
David Benjamin29a83c52016-06-17 19:12:54 -04002483 DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin800046f2017-10-17 01:37:51 -04002484 uint8_t outgoing_messages_len = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002485
David Benjaminc11ea9422017-08-29 16:33:21 -04002486 // outgoing_written is the number of outgoing messages that have been
2487 // written.
David Benjamin800046f2017-10-17 01:37:51 -04002488 uint8_t outgoing_written = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002489 // outgoing_offset is the number of bytes of the next outgoing message have
2490 // been written.
David Benjamin800046f2017-10-17 01:37:51 -04002491 uint32_t outgoing_offset = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -05002492
David Benjamin800046f2017-10-17 01:37:51 -04002493 unsigned mtu = 0; // max DTLS packet size
David Benjamin593047f2015-05-08 13:08:52 -04002494
David Benjaminc11ea9422017-08-29 16:33:21 -04002495 // num_timeouts is the number of times the retransmit timer has fired since
2496 // the last time it was reset.
David Benjamin800046f2017-10-17 01:37:51 -04002497 unsigned num_timeouts = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002498
David Benjaminc11ea9422017-08-29 16:33:21 -04002499 // Indicates when the last handshake msg or heartbeat sent will
2500 // timeout.
David Benjamin800046f2017-10-17 01:37:51 -04002501 struct OPENSSL_timeval next_timeout = {0, 0};
David Benjamin593047f2015-05-08 13:08:52 -04002502
David Benjaminc11ea9422017-08-29 16:33:21 -04002503 // timeout_duration_ms is the timeout duration in milliseconds.
David Benjamin800046f2017-10-17 01:37:51 -04002504 unsigned timeout_duration_ms = 0;
David Benjamin86e95b82017-07-18 16:34:25 -04002505};
David Benjamin593047f2015-05-08 13:08:52 -04002506
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002507// SSL_CONFIG contains configuration bits that can be shed after the handshake
2508// completes. Objects of this type are not shared; they are unique to a
2509// particular |SSL|.
2510//
2511// See SSL_shed_handshake_config() for more about the conditions under which
2512// configuration can be shed.
2513struct SSL_CONFIG {
David Benjamin50596f82018-07-02 19:47:27 -04002514 static constexpr bool kAllowUniquePtr = true;
2515
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002516 explicit SSL_CONFIG(SSL *ssl_arg);
2517 ~SSL_CONFIG();
David Benjamin3f2611a2017-02-06 13:38:26 -05002518
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002519 // ssl is a non-owning pointer to the parent |SSL| object.
2520 SSL *const ssl = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002521
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -07002522 // conf_max_version is the maximum acceptable version configured by
2523 // |SSL_set_max_proto_version|. Note this version is not normalized in DTLS
2524 // and is further constrained by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002525 uint16_t conf_max_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002526
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -07002527 // conf_min_version is the minimum acceptable version configured by
2528 // |SSL_set_min_proto_version|. Note this version is not normalized in DTLS
2529 // and is further constrained by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002530 uint16_t conf_min_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002531
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002532 X509_VERIFY_PARAM *param = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002533
David Benjaminc11ea9422017-08-29 16:33:21 -04002534 // crypto
David Benjamin0ce090a2018-07-02 20:24:40 -04002535 UniquePtr<SSLCipherPreferenceList> cipher_list;
David Benjamin3f2611a2017-02-06 13:38:26 -05002536
Adam Langley3fe8fa72018-01-26 09:14:30 -08002537 // This is used to hold the local certificate used (i.e. the server
2538 // certificate for a server or the client certificate for a client).
David Benjamin0ce090a2018-07-02 20:24:40 -04002539 UniquePtr<CERT> cert;
David Benjamin3f2611a2017-02-06 13:38:26 -05002540
2541 int (*verify_callback)(int ok,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002542 X509_STORE_CTX *ctx) =
2543 nullptr; // fail if callback returns 0
David Benjamin3f2611a2017-02-06 13:38:26 -05002544
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002545 enum ssl_verify_result_t (*custom_verify_callback)(
2546 SSL *ssl, uint8_t *out_alert) = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04002547 // Server-only: psk_identity_hint is the identity hint to send in
2548 // PSK-based key exchanges.
David Benjamin0ce090a2018-07-02 20:24:40 -04002549 UniquePtr<char> psk_identity_hint;
David Benjamin3f2611a2017-02-06 13:38:26 -05002550
David Benjamin0ce090a2018-07-02 20:24:40 -04002551 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
2552 unsigned max_identity_len, uint8_t *psk,
2553 unsigned max_psk_len) = nullptr;
2554 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
2555 unsigned max_psk_len) = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002556
David Benjaminc11ea9422017-08-29 16:33:21 -04002557 // for server side, keep the list of CA_dn we can use
David Benjamin0ce090a2018-07-02 20:24:40 -04002558 UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
Adam Langley34b4c822017-02-02 10:57:17 -08002559
David Benjaminc11ea9422017-08-29 16:33:21 -04002560 // cached_x509_client_CA is a cache of parsed versions of the elements of
2561 // |client_CA|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002562 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002563
David Benjamin0ce090a2018-07-02 20:24:40 -04002564 Array<uint16_t> supported_group_list; // our list
David Benjamin3f2611a2017-02-06 13:38:26 -05002565
David Benjaminc11ea9422017-08-29 16:33:21 -04002566 // The client's Channel ID private key.
David Benjamin46853762018-07-03 14:01:26 -04002567 UniquePtr<EVP_PKEY> channel_id_private;
David Benjamin3f2611a2017-02-06 13:38:26 -05002568
David Benjaminc11ea9422017-08-29 16:33:21 -04002569 // For a client, this contains the list of supported protocols in wire
2570 // format.
David Benjamin0ce090a2018-07-02 20:24:40 -04002571 Array<uint8_t> alpn_client_proto_list;
David Benjamin3f2611a2017-02-06 13:38:26 -05002572
Nick Harper36fcc4c2017-09-21 15:02:22 -07002573 // Contains a list of supported Token Binding key parameters.
David Benjamin0ce090a2018-07-02 20:24:40 -04002574 Array<uint8_t> token_binding_params;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002575
Nick Harper3c034b22017-12-22 15:50:43 -08002576 // Contains the QUIC transport params that this endpoint will send.
David Benjamin0ce090a2018-07-02 20:24:40 -04002577 Array<uint8_t> quic_transport_params;
David Benjamin3f2611a2017-02-06 13:38:26 -05002578
Adam Langley826ce152018-08-03 10:31:21 -07002579 // verify_sigalgs, if not empty, is the set of signature algorithms
2580 // accepted from the peer in decreasing order of preference.
2581 Array<uint16_t> verify_sigalgs;
2582
David Benjaminb95d4b42018-05-02 16:11:34 -04002583 // srtp_profiles is the list of configured SRTP protection profiles for
2584 // DTLS-SRTP.
David Benjamin0ce090a2018-07-02 20:24:40 -04002585 UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
David Benjaminb95d4b42018-05-02 16:11:34 -04002586
David Benjaminc11ea9422017-08-29 16:33:21 -04002587 // verify_mode is a bitmask of |SSL_VERIFY_*| values.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002588 uint8_t verify_mode = SSL_VERIFY_NONE;
David Benjamin3f2611a2017-02-06 13:38:26 -05002589
David Benjaminc11ea9422017-08-29 16:33:21 -04002590 // Enable signed certificate time stamps. Currently client only.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002591 bool signed_cert_timestamps_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002592
David Benjaminc11ea9422017-08-29 16:33:21 -04002593 // ocsp_stapling_enabled is only used by client connections and indicates
2594 // whether OCSP stapling will be requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002595 bool ocsp_stapling_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002596
David Benjamin46853762018-07-03 14:01:26 -04002597 // channel_id_enabled is copied from the |SSL_CTX|. For a server, means that
2598 // we'll accept Channel IDs from clients. For a client, means that we'll
2599 // advertise support.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002600 bool channel_id_enabled : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002601
Jesse Seloverd7266ec2019-01-30 16:06:10 -05002602 // If enforce_rsa_key_usage is true, the handshake will fail if the
2603 // keyUsage extension is present and incompatible with the TLS usage.
2604 // This field is not read until after certificate verification.
2605 bool enforce_rsa_key_usage : 1;
2606
David Benjaminc11ea9422017-08-29 16:33:21 -04002607 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
2608 // hash of the peer's certificate and then discard it to save memory and
2609 // session space. Only effective on the server side.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002610 bool retain_only_sha256_of_client_certs : 1;
Adam Langley3fe8fa72018-01-26 09:14:30 -08002611
2612 // handoff indicates that a server should stop after receiving the
2613 // ClientHello and pause the handshake in such a way that |SSL_get_error|
David Benjaminf4928302019-08-21 16:04:53 -04002614 // returns |SSL_ERROR_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX|
Adam Langley3fe8fa72018-01-26 09:14:30 -08002615 // element of the same name and may be cleared if the handoff is declined.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002616 bool handoff : 1;
Adam Langley8df8e642018-02-28 13:35:39 -08002617
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002618 // shed_handshake_config indicates that the handshake config (this object!)
2619 // should be freed after the handshake completes.
2620 bool shed_handshake_config : 1;
David Benjamin2d98d492018-10-08 17:31:17 -05002621
2622 // ignore_tls13_downgrade is whether the connection should continue when the
2623 // server random signals a downgrade.
David Benjamin6965d252018-11-19 15:49:56 -06002624 bool ignore_tls13_downgrade : 1;
2625
2626 // jdk11_workaround is whether to disable TLS 1.3 for JDK 11 clients, as a
2627 // workaround for https://bugs.openjdk.java.net/browse/JDK-8211806.
2628 bool jdk11_workaround : 1;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002629};
2630
David Benjamina130ce02018-08-14 22:26:39 -05002631// From RFC 8446, used in determining PSK modes.
Steven Valdeza833c352016-11-01 13:39:36 -04002632#define SSL_PSK_DHE_KE 0x1
Steven Valdez1e6f11a2016-07-27 11:10:52 -04002633
David Benjaminc11ea9422017-08-29 16:33:21 -04002634// kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
2635// data that will be accepted. This value should be slightly below
2636// kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
Alessandro Ghedinide254b42017-04-17 19:12:33 +01002637static const size_t kMaxEarlyDataAccepted = 14336;
2638
David Benjamine325c3f2018-04-12 16:11:15 -04002639UniquePtr<CERT> ssl_cert_dup(CERT *cert);
David Benjamin27e377e2017-07-31 19:09:42 -04002640void ssl_cert_clear_certs(CERT *cert);
David Benjamin8525ff32018-09-05 18:44:15 -05002641bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
2642bool ssl_is_key_type_supported(int key_type);
2643// ssl_compare_public_and_private_key returns true if |pubkey| is the public
2644// counterpart to |privkey|. Otherwise it returns false and pushes a helpful
David Benjaminc11ea9422017-08-29 16:33:21 -04002645// message on the error queue.
David Benjamin8525ff32018-09-05 18:44:15 -05002646bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
Adam Langleyc26692c2017-01-25 09:34:42 -08002647 const EVP_PKEY *privkey);
David Benjamin8525ff32018-09-05 18:44:15 -05002648bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey);
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002649int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002650int ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, const SSL_SESSION *session);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002651int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002652
David Benjaminc11ea9422017-08-29 16:33:21 -04002653// ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
2654// error.
David Benjamin31b0c9b2017-07-20 14:49:15 -04002655UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
Adam Langley46db7af2017-02-01 15:49:37 -08002656
David Benjamin58150ed2018-06-29 23:58:43 -04002657// ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table
2658// keyed on session IDs.
2659uint32_t ssl_hash_session_id(Span<const uint8_t> session_id);
2660
David Benjaminc11ea9422017-08-29 16:33:21 -04002661// SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
2662// the parsed data.
Matthew Braithwaite6d597a32018-07-11 14:58:22 -07002663OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_parse(
2664 CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool);
Adam Langley46db7af2017-02-01 15:49:37 -08002665
Adam Langleye8d24392018-01-25 15:49:37 -08002666// ssl_session_serialize writes |in| to |cbb| as if it were serialising a
2667// session for Session-ID resumption. It returns one on success and zero on
2668// error.
Matthew Braithwaite6d597a32018-07-11 14:58:22 -07002669OPENSSL_EXPORT int ssl_session_serialize(const SSL_SESSION *in, CBB *cbb);
Adam Langleye8d24392018-01-25 15:49:37 -08002670
David Benjaminc11ea9422017-08-29 16:33:21 -04002671// ssl_session_is_context_valid returns one if |session|'s session ID context
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002672// matches the one set on |hs| and zero otherwise.
2673int ssl_session_is_context_valid(const SSL_HANDSHAKE *hs,
2674 const SSL_SESSION *session);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002675
David Benjaminc11ea9422017-08-29 16:33:21 -04002676// ssl_session_is_time_valid returns one if |session| is still valid and zero if
2677// it has expired.
Steven Valdez4aa154e2016-07-29 14:32:55 -04002678int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
2679
David Benjaminc11ea9422017-08-29 16:33:21 -04002680// ssl_session_is_resumable returns one if |session| is resumable for |hs| and
2681// zero otherwise.
David Benjamin45738dd2017-02-09 20:01:26 -05002682int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
2683 const SSL_SESSION *session);
David Benjamin75f99142016-11-12 12:36:06 +09002684
David Benjamina4bafd32017-10-03 15:06:29 -04002685// ssl_session_protocol_version returns the protocol version associated with
2686// |session|. Note that despite the name, this is not the same as
2687// |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
2688uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
Steven Valdez8f36c512017-06-20 10:55:02 -04002689
David Benjamina4bafd32017-10-03 15:06:29 -04002690// ssl_session_get_digest returns the digest used in |session|.
2691const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
Steven Valdez908ac192017-01-12 13:17:07 -05002692
David Benjamin4d0be242016-09-01 01:10:07 -04002693void ssl_set_session(SSL *ssl, SSL_SESSION *session);
2694
David Benjaminc11ea9422017-08-29 16:33:21 -04002695// ssl_get_prev_session looks up the previous session based on |client_hello|.
2696// On success, it sets |*out_session| to the session or nullptr if none was
2697// found. If the session could not be looked up synchronously, it returns
2698// |ssl_hs_pending_session| and should be called again. If a ticket could not be
2699// decrypted immediately it returns |ssl_hs_pending_ticket| and should also
2700// be called again. Otherwise, it returns |ssl_hs_error|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002701enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs,
Steven Valdez398085b2017-08-29 13:25:56 -04002702 UniquePtr<SSL_SESSION> *out_session,
David Benjaminfd45ee72017-08-31 14:49:09 -04002703 bool *out_tickets_supported,
2704 bool *out_renew_ticket,
Steven Valdez398085b2017-08-29 13:25:56 -04002705 const SSL_CLIENT_HELLO *client_hello);
David Benjamine3aa1d92015-06-16 15:34:50 -04002706
David Benjaminc11ea9422017-08-29 16:33:21 -04002707// The following flags determine which parts of the session are duplicated.
Steven Valdez4aa154e2016-07-29 14:32:55 -04002708#define SSL_SESSION_DUP_AUTH_ONLY 0x0
2709#define SSL_SESSION_INCLUDE_TICKET 0x1
2710#define SSL_SESSION_INCLUDE_NONAUTH 0x2
2711#define SSL_SESSION_DUP_ALL \
2712 (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
2713
David Benjaminc11ea9422017-08-29 16:33:21 -04002714// SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
2715// fields in |session| or nullptr on error. The new session is non-resumable and
2716// must be explicitly marked resumable once it has been filled in.
David Benjamin31b0c9b2017-07-20 14:49:15 -04002717OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
2718 int dup_flags);
Steven Valdez87eab492016-06-27 16:34:59 -04002719
David Benjaminc11ea9422017-08-29 16:33:21 -04002720// ssl_session_rebase_time updates |session|'s start time to the current time,
2721// adjusting the timeout so the expiration time is unchanged.
David Benjamin17b30832017-01-28 14:00:32 -05002722void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
2723
David Benjaminc11ea9422017-08-29 16:33:21 -04002724// ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
2725// |session|'s timeout to |timeout| (measured from the current time). The
2726// renewal is clamped to the session's auth_timeout.
David Benjaminad8f5e12017-02-20 17:00:20 -05002727void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
2728 uint32_t timeout);
David Benjamin123db572016-11-03 16:59:25 -04002729
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002730void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
David Benjaminf31e6812014-11-13 18:05:55 -05002731
David Benjaminb29e1e12019-05-06 14:44:46 -05002732void ssl_send_alert(SSL *ssl, int level, int desc);
2733int ssl_send_alert_impl(SSL *ssl, int level, int desc);
Adam Langleyc9827e02019-04-12 14:46:50 -07002734bool ssl3_get_message(const SSL *ssl, SSLMessage *out);
David Benjamind9229f92017-10-06 17:36:20 -04002735ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
2736 uint8_t *out_alert, Span<uint8_t> in);
David Benjamin8f94c312017-08-01 17:35:55 -04002737void ssl3_next_message(SSL *ssl);
David Benjamin854dd652014-08-26 00:32:30 -04002738
David Benjamin0d56f882015-12-19 17:05:56 -05002739int ssl3_dispatch_alert(SSL *ssl);
David Benjamind9229f92017-10-06 17:36:20 -04002740ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
2741 size_t *out_consumed, uint8_t *out_alert,
2742 Span<uint8_t> in);
2743ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2744 uint8_t *out_alert,
2745 Span<uint8_t> in);
David Benjamin74795b32017-08-31 15:13:12 -04002746int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
Steven Valdeze831a812017-03-09 14:56:07 -05002747 int len);
Steven Valdezb6b6ff32016-10-26 11:56:35 -04002748
David Benjamin97250f42017-10-07 04:12:35 -04002749bool ssl3_new(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05002750void ssl3_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002751
David Benjamin97250f42017-10-07 04:12:35 -04002752bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2753bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2754bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg);
2755bool ssl3_add_change_cipher_spec(SSL *ssl);
David Benjamindaf207a2017-01-03 18:37:41 -05002756int ssl3_flush_flight(SSL *ssl);
David Benjamin75836432016-06-17 18:48:29 -04002757
David Benjamin97250f42017-10-07 04:12:35 -04002758bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2759bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2760bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
2761bool dtls1_add_change_cipher_spec(SSL *ssl);
David Benjamin1a999cf2017-01-03 10:30:35 -05002762int dtls1_flush_flight(SSL *ssl);
David Benjaminc6604172016-06-02 16:38:35 -04002763
David Benjaminc11ea9422017-08-29 16:33:21 -04002764// ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
David Benjamin97250f42017-10-07 04:12:35 -04002765// the pending flight. It returns true on success and false on error.
2766bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
Steven Valdez5eead162016-11-11 22:23:25 -05002767
David Benjamin97250f42017-10-07 04:12:35 -04002768// ssl_hash_message incorporates |msg| into the handshake hash. It returns true
2769// on success and false on allocation failure.
David Benjamin7934f082017-08-01 16:32:25 -04002770bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjaminced94792016-11-14 17:12:11 +09002771
David Benjamind9229f92017-10-06 17:36:20 -04002772ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
2773 size_t *out_consumed, uint8_t *out_alert,
2774 Span<uint8_t> in);
2775ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2776 uint8_t *out_alert,
2777 Span<uint8_t> in);
Adam Langley95c29f32014-06-20 12:00:00 -07002778
David Benjamin74795b32017-08-31 15:13:12 -04002779int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
2780 const uint8_t *buf, int len);
David Benjamin45d45c12016-06-07 15:20:49 -04002781
David Benjaminc11ea9422017-08-29 16:33:21 -04002782// dtls1_write_record sends a record. It returns one on success and <= 0 on
2783// error.
David Benjamin45d45c12016-06-07 15:20:49 -04002784int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
2785 enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -07002786
David Benjaminaad50db2016-06-23 17:49:12 -04002787int dtls1_retransmit_outgoing_messages(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04002788bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
Jesse Selover6b0d8222018-08-14 12:47:29 -04002789 CBS *out_body);
David Benjamin97250f42017-10-07 04:12:35 -04002790bool dtls1_check_timeout_num(SSL *ssl);
David Benjamin2fa83de2015-02-08 01:40:08 -05002791
David Benjamin0d56f882015-12-19 17:05:56 -05002792void dtls1_start_timer(SSL *ssl);
2793void dtls1_stop_timer(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04002794bool dtls1_is_timer_expired(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002795unsigned int dtls1_min_mtu(void);
2796
David Benjamin97250f42017-10-07 04:12:35 -04002797bool dtls1_new(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05002798void dtls1_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002799
Adam Langleyc9827e02019-04-12 14:46:50 -07002800bool dtls1_get_message(const SSL *ssl, SSLMessage *out);
David Benjamind9229f92017-10-06 17:36:20 -04002801ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
2802 uint8_t *out_alert, Span<uint8_t> in);
David Benjamin8f94c312017-08-01 17:35:55 -04002803void dtls1_next_message(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05002804int dtls1_dispatch_alert(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07002805
Adam Langley3fe8fa72018-01-26 09:14:30 -08002806// tls1_configure_aead configures either the read or write direction AEAD (as
2807// determined by |direction|) using the keys generated by the TLS KDF. The
2808// |key_block_cache| argument is used to store the generated key block, if
2809// empty. Otherwise it's assumed that the key block is already contained within
2810// it. Returns one on success or zero on error.
2811int tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction,
2812 Array<uint8_t> *key_block_cache,
2813 const SSL_CIPHER *cipher,
2814 Span<const uint8_t> iv_override);
2815
David Benjamine58f8a62017-09-21 19:07:15 -04002816int tls1_change_cipher_state(SSL_HANDSHAKE *hs, evp_aead_direction_t direction);
Steven Valdez908ac192017-01-12 13:17:07 -05002817int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
David Benjamin2637f3c2017-10-27 00:56:55 -04002818 Span<const uint8_t> premaster);
Adam Langley95c29f32014-06-20 12:00:00 -07002819
David Benjamincf0ce672017-09-21 02:25:59 -04002820// tls1_get_grouplist returns the locally-configured group preference list.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002821Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl);
Steven Valdez5440fe02016-07-18 12:40:30 -04002822
David Benjamin861abcc2018-07-14 17:40:26 -04002823// tls1_check_group_id returns whether |group_id| is consistent with locally-
2824// configured group preferences.
2825bool tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id);
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02002826
David Benjaminc11ea9422017-08-29 16:33:21 -04002827// tls1_get_shared_group sets |*out_group_id| to the first preferred shared
David Benjamin861abcc2018-07-14 17:40:26 -04002828// group between client and server preferences and returns true. If none may be
2829// found, it returns false.
2830bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
David Benjamin072334d2014-07-13 16:24:27 -04002831
David Benjamin0ce090a2018-07-02 20:24:40 -04002832// tls1_set_curves converts the array of NIDs in |curves| into a newly allocated
2833// array of TLS group IDs. On success, the function returns true and writes the
2834// array to |*out_group_ids|. Otherwise, it returns false.
2835bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves);
David Benjamin072334d2014-07-13 16:24:27 -04002836
David Benjaminc11ea9422017-08-29 16:33:21 -04002837// tls1_set_curves_list converts the string of curves pointed to by |curves|
2838// into a newly allocated array of TLS group IDs. On success, the function
David Benjamin0ce090a2018-07-02 20:24:40 -04002839// returns true and writes the array to |*out_group_ids|. Otherwise, it returns
2840// false.
2841bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01002842
David Benjamin861abcc2018-07-14 17:40:26 -04002843// ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It returns
2844// true on success and false on failure. The |header_len| argument is the length
2845// of the ClientHello written so far and is used to compute the padding length.
2846// (It does not include the record header.)
2847bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
David Benjamine8d53502015-10-10 14:13:23 -04002848
David Benjamin861abcc2018-07-14 17:40:26 -04002849bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
2850bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2851 const SSL_CLIENT_HELLO *client_hello);
2852bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
Adam Langley95c29f32014-06-20 12:00:00 -07002853
Adam Langleyfcf25832014-12-18 17:42:32 -08002854#define tlsext_tick_md EVP_sha256
David Benjamine3aa1d92015-06-16 15:34:50 -04002855
David Benjaminc11ea9422017-08-29 16:33:21 -04002856// ssl_process_ticket processes a session ticket from the client. It returns
2857// one of:
2858// |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
2859// |*out_renew_ticket| is set to whether the ticket should be renewed.
2860// |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
2861// fresh ticket should be sent, but the given ticket cannot be used.
2862// |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
2863// Retry later.
2864// |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
Adam Langley4c341d02017-03-08 19:33:21 -08002865enum ssl_ticket_aead_result_t ssl_process_ticket(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002866 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
David Benjamin28655672018-07-18 23:23:25 -04002867 bool *out_renew_ticket, Span<const uint8_t> ticket,
2868 Span<const uint8_t> session_id);
Adam Langley95c29f32014-06-20 12:00:00 -07002869
David Benjaminc11ea9422017-08-29 16:33:21 -04002870// tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
David Benjamin861abcc2018-07-14 17:40:26 -04002871// the signature. If the key is valid, it saves the Channel ID and returns true.
2872// Otherwise, it returns false.
2873bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Nick Harper60a85cb2016-09-23 16:25:11 -07002874
David Benjaminc11ea9422017-08-29 16:33:21 -04002875// tls1_write_channel_id generates a Channel ID message and puts the output in
David Benjamin46853762018-07-03 14:01:26 -04002876// |cbb|. |ssl->channel_id_private| must already be set before calling. This
2877// function returns true on success and false on error.
David Benjaminf1db1a32017-10-27 01:12:34 -04002878bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07002879
David Benjaminc11ea9422017-08-29 16:33:21 -04002880// tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
2881// it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
David Benjamin861abcc2018-07-14 17:40:26 -04002882// true on success and false on failure.
2883bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04002884
David Benjamin861abcc2018-07-14 17:40:26 -04002885// tls1_record_handshake_hashes_for_channel_id records the current handshake
2886// hashes in |hs->new_session| so that Channel ID resumptions can sign that
2887// data.
2888bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
Adam Langley1258b6a2014-06-20 12:00:00 -07002889
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002890// ssl_do_channel_id_callback checks runs |hs->ssl->ctx->channel_id_cb| if
David Benjamin861abcc2018-07-14 17:40:26 -04002891// necessary. It returns true on success and false on fatal error. Note that, on
David Benjamin46853762018-07-03 14:01:26 -04002892// success, |hs->ssl->channel_id_private| may be unset, in which case the
David Benjaminc11ea9422017-08-29 16:33:21 -04002893// operation should be retried later.
David Benjamin861abcc2018-07-14 17:40:26 -04002894bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs);
Nick Harper60a85cb2016-09-23 16:25:11 -07002895
David Benjamin861abcc2018-07-14 17:40:26 -04002896// ssl_can_write returns whether |ssl| is allowed to write.
2897bool ssl_can_write(const SSL *ssl);
Steven Valdez681eb6a2016-12-19 13:19:29 -05002898
David Benjamin861abcc2018-07-14 17:40:26 -04002899// ssl_can_read returns wheter |ssl| is allowed to read.
2900bool ssl_can_read(const SSL *ssl);
Steven Valdez681eb6a2016-12-19 13:19:29 -05002901
David Benjaminad8f5e12017-02-20 17:00:20 -05002902void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002903void ssl_ctx_get_current_time(const SSL_CTX *ctx,
2904 struct OPENSSL_timeval *out_clock);
David Benjamin721e8b72016-08-03 13:13:17 -04002905
David Benjaminc11ea9422017-08-29 16:33:21 -04002906// ssl_reset_error_state resets state for |SSL_get_error|.
David Benjamin2be4aa72017-01-02 07:56:51 -05002907void ssl_reset_error_state(SSL *ssl);
2908
David Benjamin31aad2d2017-10-06 16:04:06 -04002909// ssl_set_read_error sets |ssl|'s read half into an error state, saving the
2910// current state of the error queue.
Jesse Selover6b0d8222018-08-14 12:47:29 -04002911void ssl_set_read_error(SSL *ssl);
David Benjamin31aad2d2017-10-06 16:04:06 -04002912
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07002913BSSL_NAMESPACE_END
Adam Langley11d11d62017-07-17 11:10:46 -07002914
Steven Valdez87eab492016-06-27 16:34:59 -04002915
David Benjaminc11ea9422017-08-29 16:33:21 -04002916// Opaque C types.
2917//
2918// The following types are exported to C code as public typedefs, so they must
2919// be defined outside of the namespace.
David Benjamine39ac8f2017-07-20 12:22:21 -04002920
David Benjaminc11ea9422017-08-29 16:33:21 -04002921// ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
2922// structure to support the legacy version-locked methods.
David Benjamine39ac8f2017-07-20 12:22:21 -04002923struct ssl_method_st {
David Benjaminc11ea9422017-08-29 16:33:21 -04002924 // version, if non-zero, is the only protocol version acceptable to an
2925 // SSL_CTX initialized from this method.
David Benjamine39ac8f2017-07-20 12:22:21 -04002926 uint16_t version;
David Benjaminc11ea9422017-08-29 16:33:21 -04002927 // method is the underlying SSL_PROTOCOL_METHOD that initializes the
2928 // SSL_CTX.
David Benjamin664e99a2017-10-10 19:23:21 -04002929 const bssl::SSL_PROTOCOL_METHOD *method;
David Benjaminc11ea9422017-08-29 16:33:21 -04002930 // x509_method contains pointers to functions that might deal with |X509|
2931 // compatibility, or might be a no-op, depending on the application.
David Benjamin0363de92018-06-29 13:57:38 -04002932 const bssl::SSL_X509_METHOD *x509_method;
David Benjamine39ac8f2017-07-20 12:22:21 -04002933};
2934
David Benjamin49798032018-07-02 16:23:54 -04002935struct ssl_ctx_st {
David Benjaminc1389f22018-07-02 19:13:42 -04002936 explicit ssl_ctx_st(const SSL_METHOD *ssl_method);
2937 ssl_ctx_st(const ssl_ctx_st &) = delete;
2938 ssl_ctx_st &operator=(const ssl_ctx_st &) = delete;
2939
2940 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
2941 const bssl::SSL_X509_METHOD *x509_method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002942
2943 // lock is used to protect various operations on this object.
2944 CRYPTO_MUTEX lock;
2945
2946 // conf_max_version is the maximum acceptable protocol version configured by
2947 // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
2948 // and is further constrainted by |SSL_OP_NO_*|.
David Benjaminc1389f22018-07-02 19:13:42 -04002949 uint16_t conf_max_version = 0;
David Benjamin49798032018-07-02 16:23:54 -04002950
2951 // conf_min_version is the minimum acceptable protocol version configured by
2952 // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
2953 // and is further constrainted by |SSL_OP_NO_*|.
David Benjaminc1389f22018-07-02 19:13:42 -04002954 uint16_t conf_min_version = 0;
David Benjamin49798032018-07-02 16:23:54 -04002955
Steven Valdezc8e0f902018-07-14 11:23:01 -04002956 // quic_method is the method table corresponding to the QUIC hooks.
2957 const SSL_QUIC_METHOD *quic_method = nullptr;
2958
David Benjamin0ce090a2018-07-02 20:24:40 -04002959 bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list;
David Benjamin49798032018-07-02 16:23:54 -04002960
David Benjaminc1389f22018-07-02 19:13:42 -04002961 X509_STORE *cert_store = nullptr;
2962 LHASH_OF(SSL_SESSION) *sessions = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002963 // Most session-ids that will be cached, default is
2964 // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
David Benjaminc1389f22018-07-02 19:13:42 -04002965 unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
2966 SSL_SESSION *session_cache_head = nullptr;
2967 SSL_SESSION *session_cache_tail = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002968
2969 // handshakes_since_cache_flush is the number of successful handshakes since
2970 // the last cache flush.
David Benjaminc1389f22018-07-02 19:13:42 -04002971 int handshakes_since_cache_flush = 0;
David Benjamin49798032018-07-02 16:23:54 -04002972
2973 // This can have one of 2 values, ored together,
2974 // SSL_SESS_CACHE_CLIENT,
2975 // SSL_SESS_CACHE_SERVER,
2976 // Default is SSL_SESSION_CACHE_SERVER, which means only
2977 // SSL_accept which cache SSL_SESSIONS.
David Benjaminc1389f22018-07-02 19:13:42 -04002978 int session_cache_mode = SSL_SESS_CACHE_SERVER;
David Benjamin49798032018-07-02 16:23:54 -04002979
2980 // session_timeout is the default lifetime for new sessions in TLS 1.2 and
2981 // earlier, in seconds.
David Benjaminc1389f22018-07-02 19:13:42 -04002982 uint32_t session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin49798032018-07-02 16:23:54 -04002983
2984 // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
2985 // 1.3, in seconds.
David Benjaminc1389f22018-07-02 19:13:42 -04002986 uint32_t session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
David Benjamin49798032018-07-02 16:23:54 -04002987
2988 // If this callback is not null, it will be called each time a session id is
2989 // added to the cache. If this function returns 1, it means that the
2990 // callback will do a SSL_SESSION_free() when it has finished using it.
2991 // Otherwise, on 0, it means the callback has finished with it. If
2992 // remove_session_cb is not null, it will be called when a session-id is
2993 // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
2994 // it.
David Benjaminc1389f22018-07-02 19:13:42 -04002995 int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess) = nullptr;
2996 void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002997 SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
David Benjaminc1389f22018-07-02 19:13:42 -04002998 int *copy) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04002999
David Benjaminc1389f22018-07-02 19:13:42 -04003000 CRYPTO_refcount_t references = 1;
David Benjamin49798032018-07-02 16:23:54 -04003001
3002 // if defined, these override the X509_verify_cert() calls
David Benjaminc1389f22018-07-02 19:13:42 -04003003 int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg) = nullptr;
3004 void *app_verify_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003005
David Benjaminc1389f22018-07-02 19:13:42 -04003006 ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
3007 uint8_t *out_alert) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003008
3009 // Default password callback.
David Benjaminc1389f22018-07-02 19:13:42 -04003010 pem_password_cb *default_passwd_callback = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003011
3012 // Default password callback user data.
David Benjaminc1389f22018-07-02 19:13:42 -04003013 void *default_passwd_callback_userdata = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003014
3015 // get client cert callback
Jesse Selover6b0d8222018-08-14 12:47:29 -04003016 int (*client_cert_cb)(SSL *ssl, X509 **out_x509,
3017 EVP_PKEY **out_pkey) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003018
3019 // get channel id callback
David Benjaminc1389f22018-07-02 19:13:42 -04003020 void (*channel_id_cb)(SSL *ssl, EVP_PKEY **out_pkey) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003021
3022 CRYPTO_EX_DATA ex_data;
3023
David Benjamin49798032018-07-02 16:23:54 -04003024 // Default values used when no per-SSL value is defined follow
3025
David Benjaminc1389f22018-07-02 19:13:42 -04003026 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003027
3028 // what we put in client cert requests
David Benjamin0ce090a2018-07-02 20:24:40 -04003029 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
David Benjamin49798032018-07-02 16:23:54 -04003030
3031 // cached_x509_client_CA is a cache of parsed versions of the elements of
3032 // |client_CA|.
David Benjaminc1389f22018-07-02 19:13:42 -04003033 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003034
3035
3036 // Default values to use in SSL structures follow (these are copied by
3037 // SSL_new)
3038
David Benjaminc1389f22018-07-02 19:13:42 -04003039 uint32_t options = 0;
3040 // Disable the auto-chaining feature by default. wpa_supplicant relies on this
3041 // feature, but require callers opt into it.
3042 uint32_t mode = SSL_MODE_NO_AUTO_CHAIN;
3043 uint32_t max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
David Benjamin49798032018-07-02 16:23:54 -04003044
David Benjamin0ce090a2018-07-02 20:24:40 -04003045 bssl::UniquePtr<bssl::CERT> cert;
David Benjamin49798032018-07-02 16:23:54 -04003046
3047 // callback that allows applications to peek at protocol messages
3048 void (*msg_callback)(int write_p, int version, int content_type,
Jesse Selover6b0d8222018-08-14 12:47:29 -04003049 const void *buf, size_t len, SSL *ssl,
3050 void *arg) = nullptr;
David Benjaminc1389f22018-07-02 19:13:42 -04003051 void *msg_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003052
David Benjaminc1389f22018-07-02 19:13:42 -04003053 int verify_mode = SSL_VERIFY_NONE;
3054 int (*default_verify_callback)(int ok, X509_STORE_CTX *ctx) =
3055 nullptr; // called 'verify_callback' in the SSL
David Benjamin49798032018-07-02 16:23:54 -04003056
David Benjaminc1389f22018-07-02 19:13:42 -04003057 X509_VERIFY_PARAM *param = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003058
3059 // select_certificate_cb is called before most ClientHello processing and
3060 // before the decision whether to resume a session is made. See
3061 // |ssl_select_cert_result_t| for details of the return values.
David Benjaminc1389f22018-07-02 19:13:42 -04003062 ssl_select_cert_result_t (*select_certificate_cb)(const SSL_CLIENT_HELLO *) =
3063 nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003064
3065 // dos_protection_cb is called once the resumption decision for a ClientHello
3066 // has been made. It returns one to continue the handshake or zero to
3067 // abort.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003068 int (*dos_protection_cb)(const SSL_CLIENT_HELLO *) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003069
Jesse Selover1c337e52018-08-10 13:28:47 -04003070 // Controls whether to verify certificates when resuming connections. They
3071 // were already verified when the connection was first made, so the default is
3072 // false. For now, this is only respected on clients, not servers.
3073 bool reverify_on_resume = false;
3074
David Benjamin49798032018-07-02 16:23:54 -04003075 // Maximum amount of data to send in one fragment. actual record size can be
3076 // more than this due to padding and MAC overheads.
David Benjaminc1389f22018-07-02 19:13:42 -04003077 uint16_t max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin49798032018-07-02 16:23:54 -04003078
3079 // TLS extensions servername callback
David Benjamin46853762018-07-03 14:01:26 -04003080 int (*servername_callback)(SSL *, int *, void *) = nullptr;
3081 void *servername_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003082
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003083 // RFC 4507 session ticket keys. |ticket_key_current| may be NULL before the
3084 // first handshake and |ticket_key_prev| may be NULL at any time.
3085 // Automatically generated ticket keys are rotated as needed at handshake
3086 // time. Hence, all access must be synchronized through |lock|.
3087 bssl::UniquePtr<bssl::TicketKey> ticket_key_current;
3088 bssl::UniquePtr<bssl::TicketKey> ticket_key_prev;
David Benjamin49798032018-07-02 16:23:54 -04003089
3090 // Callback to support customisation of ticket key setting
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003091 int (*ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
3092 EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003093
3094 // Server-only: psk_identity_hint is the default identity hint to send in
3095 // PSK-based key exchanges.
David Benjamin0ce090a2018-07-02 20:24:40 -04003096 bssl::UniquePtr<char> psk_identity_hint;
David Benjamin49798032018-07-02 16:23:54 -04003097
David Benjaminc1389f22018-07-02 19:13:42 -04003098 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
3099 unsigned max_identity_len, uint8_t *psk,
3100 unsigned max_psk_len) = nullptr;
3101 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
3102 unsigned max_psk_len) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003103
3104
3105 // Next protocol negotiation information
3106 // (for experimental NPN extension).
3107
3108 // For a server, this contains a callback function by which the set of
3109 // advertised protocols can be provided.
3110 int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
David Benjaminc1389f22018-07-02 19:13:42 -04003111 unsigned *out_len, void *arg) = nullptr;
3112 void *next_protos_advertised_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003113 // For a client, this contains a callback function that selects the
3114 // next protocol from the list provided by the server.
3115 int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
David Benjaminc1389f22018-07-02 19:13:42 -04003116 const uint8_t *in, unsigned in_len,
3117 void *arg) = nullptr;
3118 void *next_proto_select_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003119
3120 // ALPN information
3121 // (we are in the process of transitioning from NPN to ALPN.)
3122
3123 // For a server, this contains a callback function that allows the
3124 // server to select the protocol for the connection.
3125 // out: on successful return, this must point to the raw protocol
3126 // name (without the length prefix).
3127 // outlen: on successful return, this contains the length of |*out|.
3128 // in: points to the client's list of supported protocols in
3129 // wire-format.
3130 // inlen: the length of |in|.
3131 int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
David Benjaminc1389f22018-07-02 19:13:42 -04003132 const uint8_t *in, unsigned in_len,
3133 void *arg) = nullptr;
3134 void *alpn_select_cb_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003135
3136 // For a client, this contains the list of supported protocols in wire
3137 // format.
David Benjamin0ce090a2018-07-02 20:24:40 -04003138 bssl::Array<uint8_t> alpn_client_proto_list;
David Benjamin49798032018-07-02 16:23:54 -04003139
3140 // SRTP profiles we are willing to do from RFC 5764
David Benjamin0ce090a2018-07-02 20:24:40 -04003141 bssl::UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
David Benjamin49798032018-07-02 16:23:54 -04003142
3143 // Defined compression algorithms for certificates.
David Benjamin35b4a122018-07-14 17:04:41 -04003144 bssl::UniquePtr<STACK_OF(CertCompressionAlg)> cert_compression_algs;
David Benjamin49798032018-07-02 16:23:54 -04003145
3146 // Supported group values inherited by SSL structure
David Benjamin0ce090a2018-07-02 20:24:40 -04003147 bssl::Array<uint16_t> supported_group_list;
David Benjamin49798032018-07-02 16:23:54 -04003148
3149 // The client's Channel ID private key.
David Benjamin46853762018-07-03 14:01:26 -04003150 bssl::UniquePtr<EVP_PKEY> channel_id_private;
David Benjamin49798032018-07-02 16:23:54 -04003151
3152 // keylog_callback, if not NULL, is the key logging callback. See
3153 // |SSL_CTX_set_keylog_callback|.
David Benjaminc1389f22018-07-02 19:13:42 -04003154 void (*keylog_callback)(const SSL *ssl, const char *line) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003155
3156 // current_time_cb, if not NULL, is the function to use to get the current
3157 // time. It sets |*out_clock| to the current time. The |ssl| argument is
3158 // always NULL. See |SSL_CTX_set_current_time_cb|.
David Benjaminc1389f22018-07-02 19:13:42 -04003159 void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003160
3161 // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
3162 // memory.
David Benjaminc1389f22018-07-02 19:13:42 -04003163 CRYPTO_BUFFER_POOL *pool = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003164
3165 // ticket_aead_method contains function pointers for opening and sealing
3166 // session tickets.
David Benjaminc1389f22018-07-02 19:13:42 -04003167 const SSL_TICKET_AEAD_METHOD *ticket_aead_method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003168
3169 // legacy_ocsp_callback implements an OCSP-related callback for OpenSSL
3170 // compatibility.
David Benjaminc1389f22018-07-02 19:13:42 -04003171 int (*legacy_ocsp_callback)(SSL *ssl, void *arg) = nullptr;
3172 void *legacy_ocsp_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003173
3174 // verify_sigalgs, if not empty, is the set of signature algorithms
3175 // accepted from the peer in decreasing order of preference.
David Benjamin0ce090a2018-07-02 20:24:40 -04003176 bssl::Array<uint16_t> verify_sigalgs;
David Benjamin49798032018-07-02 16:23:54 -04003177
3178 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
3179 // hash of the peer's certificate and then discard it to save memory and
3180 // session space. Only effective on the server side.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003181 bool retain_only_sha256_of_client_certs : 1;
David Benjamin49798032018-07-02 16:23:54 -04003182
3183 // quiet_shutdown is true if the connection should not send a close_notify on
3184 // shutdown.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003185 bool quiet_shutdown : 1;
David Benjamin49798032018-07-02 16:23:54 -04003186
3187 // ocsp_stapling_enabled is only used by client connections and indicates
3188 // whether OCSP stapling will be requested.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003189 bool ocsp_stapling_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003190
3191 // If true, a client will request certificate timestamps.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003192 bool signed_cert_timestamps_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003193
David Benjamin46853762018-07-03 14:01:26 -04003194 // channel_id_enabled is whether Channel ID is enabled. For a server, means
3195 // that we'll accept Channel IDs from clients. For a client, means that we'll
3196 // advertise support.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003197 bool channel_id_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003198
3199 // grease_enabled is whether draft-davidben-tls-grease-01 is enabled.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003200 bool grease_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003201
3202 // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN
3203 // protocols from the peer.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003204 bool allow_unknown_alpn_protos : 1;
David Benjamin49798032018-07-02 16:23:54 -04003205
3206 // ed25519_enabled is whether Ed25519 is advertised in the handshake.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003207 bool ed25519_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003208
3209 // rsa_pss_rsae_certs_enabled is whether rsa_pss_rsae_* are supported by the
3210 // certificate verifier.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003211 bool rsa_pss_rsae_certs_enabled : 1;
David Benjamin49798032018-07-02 16:23:54 -04003212
3213 // false_start_allowed_without_alpn is whether False Start (if
3214 // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003215 bool false_start_allowed_without_alpn : 1;
David Benjamin49798032018-07-02 16:23:54 -04003216
Steven Valdezf1af1292018-08-13 10:54:48 -04003217 // ignore_tls13_downgrade is whether a connection should continue when the
3218 // server random signals a downgrade.
3219 bool ignore_tls13_downgrade:1;
3220
David Benjamin49798032018-07-02 16:23:54 -04003221 // handoff indicates that a server should stop after receiving the
3222 // ClientHello and pause the handshake in such a way that |SSL_get_error|
David Benjaminf4928302019-08-21 16:04:53 -04003223 // returns |SSL_ERROR_HANDOFF|.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003224 bool handoff : 1;
David Benjamin49798032018-07-02 16:23:54 -04003225
3226 // If enable_early_data is true, early data can be sent and accepted.
3227 bool enable_early_data : 1;
David Benjaminc1389f22018-07-02 19:13:42 -04003228
Adam Langley9f5c4192019-07-19 15:18:33 -07003229 // pq_experiment_signal indicates that an empty extension should be sent
3230 // (for clients) or echoed (for servers) to indicate participation in an
3231 // experiment of post-quantum key exchanges.
3232 bool pq_experiment_signal : 1;
3233
David Benjaminc1389f22018-07-02 19:13:42 -04003234 private:
3235 ~ssl_ctx_st();
3236 friend void SSL_CTX_free(SSL_CTX *);
David Benjamin49798032018-07-02 16:23:54 -04003237};
3238
3239struct ssl_st {
David Benjaminc1389f22018-07-02 19:13:42 -04003240 explicit ssl_st(SSL_CTX *ctx_arg);
3241 ssl_st(const ssl_st &) = delete;
3242 ssl_st &operator=(const ssl_st &) = delete;
3243 ~ssl_st();
3244
David Benjamin49798032018-07-02 16:23:54 -04003245 // method is the method table corresponding to the current protocol (DTLS or
3246 // TLS).
David Benjaminc1389f22018-07-02 19:13:42 -04003247 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003248
3249 // config is a container for handshake configuration. Accesses to this field
3250 // should check for nullptr, since configuration may be shed after the
3251 // handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use
3252 // that instead, and skip the null check.)
David Benjamin50596f82018-07-02 19:47:27 -04003253 bssl::UniquePtr<bssl::SSL_CONFIG> config;
David Benjamin49798032018-07-02 16:23:54 -04003254
3255 // version is the protocol version.
David Benjaminc1389f22018-07-02 19:13:42 -04003256 uint16_t version = 0;
David Benjamin49798032018-07-02 16:23:54 -04003257
David Benjaminc1389f22018-07-02 19:13:42 -04003258 uint16_t max_send_fragment = 0;
David Benjamin49798032018-07-02 16:23:54 -04003259
3260 // There are 2 BIO's even though they are normally both the same. This is so
3261 // data can be read and written to different handlers
3262
David Benjamin50596f82018-07-02 19:47:27 -04003263 bssl::UniquePtr<BIO> rbio; // used by SSL_read
3264 bssl::UniquePtr<BIO> wbio; // used by SSL_write
David Benjamin49798032018-07-02 16:23:54 -04003265
3266 // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
3267 // Otherwise, it returns a value corresponding to what operation is needed to
3268 // progress.
David Benjaminc1389f22018-07-02 19:13:42 -04003269 bssl::ssl_hs_wait_t (*do_handshake)(bssl::SSL_HANDSHAKE *hs) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003270
David Benjaminc59b9aa2018-07-16 21:34:03 -04003271 bssl::SSL3_STATE *s3 = nullptr; // TLS variables
3272 bssl::DTLS1_STATE *d1 = nullptr; // DTLS variables
David Benjamin49798032018-07-02 16:23:54 -04003273
3274 // callback that allows applications to peek at protocol messages
3275 void (*msg_callback)(int write_p, int version, int content_type,
David Benjaminc1389f22018-07-02 19:13:42 -04003276 const void *buf, size_t len, SSL *ssl,
3277 void *arg) = nullptr;
3278 void *msg_callback_arg = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003279
3280 // session info
3281
3282 // initial_timeout_duration_ms is the default DTLS timeout duration in
3283 // milliseconds. It's used to initialize the timer any time it's restarted.
David Benjaminc1389f22018-07-02 19:13:42 -04003284 //
3285 // RFC 6347 states that implementations SHOULD use an initial timer value of 1
3286 // second.
3287 unsigned initial_timeout_duration_ms = 1000;
David Benjamin49798032018-07-02 16:23:54 -04003288
David Benjamin49798032018-07-02 16:23:54 -04003289 // session is the configured session to be offered by the client. This session
3290 // is immutable.
David Benjamin50596f82018-07-02 19:47:27 -04003291 bssl::UniquePtr<SSL_SESSION> session;
David Benjamin49798032018-07-02 16:23:54 -04003292
David Benjaminc1389f22018-07-02 19:13:42 -04003293 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
David Benjamin49798032018-07-02 16:23:54 -04003294
David Benjamin50596f82018-07-02 19:47:27 -04003295 bssl::UniquePtr<SSL_CTX> ctx;
David Benjamin49798032018-07-02 16:23:54 -04003296
3297 // session_ctx is the |SSL_CTX| used for the session cache and related
3298 // settings.
David Benjamin50596f82018-07-02 19:47:27 -04003299 bssl::UniquePtr<SSL_CTX> session_ctx;
David Benjamin49798032018-07-02 16:23:54 -04003300
3301 // extra application data
3302 CRYPTO_EX_DATA ex_data;
3303
David Benjaminc1389f22018-07-02 19:13:42 -04003304 uint32_t options = 0; // protocol behaviour
3305 uint32_t mode = 0; // API behaviour
3306 uint32_t max_cert_list = 0;
David Benjamin46853762018-07-03 14:01:26 -04003307 bssl::UniquePtr<char> hostname;
David Benjamin49798032018-07-02 16:23:54 -04003308
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +00003309 // quic_method is the method table corresponding to the QUIC hooks.
3310 const SSL_QUIC_METHOD *quic_method = nullptr;
3311
David Benjamin49798032018-07-02 16:23:54 -04003312 // renegotiate_mode controls how peer renegotiation attempts are handled.
David Benjaminc1389f22018-07-02 19:13:42 -04003313 ssl_renegotiate_mode_t renegotiate_mode = ssl_renegotiate_never;
David Benjamin49798032018-07-02 16:23:54 -04003314
3315 // server is true iff the this SSL* is the server half. Note: before the SSL*
3316 // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
3317 // the side is not determined. In this state, server is always false.
3318 bool server : 1;
3319
3320 // quiet_shutdown is true if the connection should not send a close_notify on
3321 // shutdown.
3322 bool quiet_shutdown : 1;
3323
David Benjamin49798032018-07-02 16:23:54 -04003324 // If enable_early_data is true, early data can be sent and accepted.
3325 bool enable_early_data : 1;
3326};
David Benjamine39ac8f2017-07-20 12:22:21 -04003327
David Benjamin1c68fa22018-06-02 19:02:46 -04003328struct ssl_session_st {
David Benjaminbfdd1a92018-06-29 16:26:38 -04003329 explicit ssl_session_st(const bssl::SSL_X509_METHOD *method);
3330 ssl_session_st(const ssl_session_st &) = delete;
3331 ssl_session_st &operator=(const ssl_session_st &) = delete;
3332
3333 CRYPTO_refcount_t references = 1;
Jesse Selover6b0d8222018-08-14 12:47:29 -04003334
3335 // ssl_version is the (D)TLS version that established the session.
3336 uint16_t ssl_version = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003337
3338 // group_id is the ID of the ECDH group used to establish this session or zero
3339 // if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003340 uint16_t group_id = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003341
3342 // peer_signature_algorithm is the signature algorithm used to authenticate
3343 // the peer, or zero if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003344 uint16_t peer_signature_algorithm = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003345
3346 // master_key, in TLS 1.2 and below, is the master secret associated with the
3347 // session. In TLS 1.3 and up, it is the resumption secret.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003348 int master_key_length = 0;
3349 uint8_t master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003350
3351 // session_id - valid?
David Benjaminbfdd1a92018-06-29 16:26:38 -04003352 unsigned session_id_length = 0;
3353 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003354 // this is used to determine whether the session is being reused in
3355 // the appropriate context. It is up to the application to set this,
3356 // via SSL_new
David Benjaminbfdd1a92018-06-29 16:26:38 -04003357 uint8_t sid_ctx_length = 0;
3358 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003359
David Benjaminbfdd1a92018-06-29 16:26:38 -04003360 bssl::UniquePtr<char> psk_identity;
David Benjamin1c68fa22018-06-02 19:02:46 -04003361
3362 // certs contains the certificate chain from the peer, starting with the leaf
3363 // certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003364 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs;
David Benjamin1c68fa22018-06-02 19:02:46 -04003365
David Benjaminbfdd1a92018-06-29 16:26:38 -04003366 const bssl::SSL_X509_METHOD *x509_method = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003367
3368 // x509_peer is the peer's certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003369 X509 *x509_peer = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003370
3371 // x509_chain is the certificate chain sent by the peer. NOTE: for historical
3372 // reasons, when a client (so the peer is a server), the chain includes
3373 // |peer|, but when a server it does not.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003374 STACK_OF(X509) *x509_chain = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003375
3376 // x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that
3377 // omits the leaf certificate. This exists because OpenSSL, historically,
3378 // didn't include the leaf certificate in the chain for a server, but did for
3379 // a client. The |x509_chain| always includes it and, if an API call requires
3380 // a chain without, it is stored here.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003381 STACK_OF(X509) *x509_chain_without_leaf = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003382
3383 // verify_result is the result of certificate verification in the case of
3384 // non-fatal certificate errors.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003385 long verify_result = X509_V_ERR_INVALID_CALL;
David Benjamin1c68fa22018-06-02 19:02:46 -04003386
3387 // timeout is the lifetime of the session in seconds, measured from |time|.
3388 // This is renewable up to |auth_timeout|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003389 uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003390
3391 // auth_timeout is the non-renewable lifetime of the session in seconds,
3392 // measured from |time|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003393 uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003394
3395 // time is the time the session was issued, measured in seconds from the UNIX
3396 // epoch.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003397 uint64_t time = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003398
David Benjaminbfdd1a92018-06-29 16:26:38 -04003399 const SSL_CIPHER *cipher = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003400
3401 CRYPTO_EX_DATA ex_data; // application specific data
3402
3403 // These are used to make removal of session-ids more efficient and to
3404 // implement a maximum cache size.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003405 SSL_SESSION *prev = nullptr, *next = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003406
David Benjaminbfdd1a92018-06-29 16:26:38 -04003407 bssl::Array<uint8_t> ticket;
David Benjamin1c68fa22018-06-02 19:02:46 -04003408
David Benjaminbfdd1a92018-06-29 16:26:38 -04003409 bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin1c68fa22018-06-02 19:02:46 -04003410
3411 // The OCSP response that came with the session.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003412 bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin1c68fa22018-06-02 19:02:46 -04003413
3414 // peer_sha256 contains the SHA-256 hash of the peer's certificate if
3415 // |peer_sha256_valid| is true.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003416 uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003417
3418 // original_handshake_hash contains the handshake hash (either SHA-1+MD5 or
3419 // SHA-2, depending on TLS version) for the original, full handshake that
3420 // created a session. This is used by Channel IDs during resumption.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003421 uint8_t original_handshake_hash[EVP_MAX_MD_SIZE] = {0};
3422 uint8_t original_handshake_hash_len = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003423
David Benjaminbfdd1a92018-06-29 16:26:38 -04003424 uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds
David Benjamin1c68fa22018-06-02 19:02:46 -04003425
David Benjaminbfdd1a92018-06-29 16:26:38 -04003426 uint32_t ticket_age_add = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003427
3428 // ticket_max_early_data is the maximum amount of data allowed to be sent as
3429 // early data. If zero, 0-RTT is disallowed.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003430 uint32_t ticket_max_early_data = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003431
3432 // early_alpn is the ALPN protocol from the initial handshake. This is only
3433 // stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT
3434 // resumptions.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003435 bssl::Array<uint8_t> early_alpn;
David Benjamin1c68fa22018-06-02 19:02:46 -04003436
David Benjamina3a71e92018-06-29 13:24:45 -04003437 // extended_master_secret is whether the master secret in this session was
David Benjamin1c68fa22018-06-02 19:02:46 -04003438 // generated using EMS and thus isn't vulnerable to the Triple Handshake
3439 // attack.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003440 bool extended_master_secret : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003441
David Benjamina3a71e92018-06-29 13:24:45 -04003442 // peer_sha256_valid is whether |peer_sha256| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003443 bool peer_sha256_valid : 1; // Non-zero if peer_sha256 is valid
David Benjamin1c68fa22018-06-02 19:02:46 -04003444
3445 // not_resumable is used to indicate that session resumption is disallowed.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003446 bool not_resumable : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003447
David Benjamina3a71e92018-06-29 13:24:45 -04003448 // ticket_age_add_valid is whether |ticket_age_add| is valid.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003449 bool ticket_age_add_valid : 1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003450
David Benjamina3a71e92018-06-29 13:24:45 -04003451 // is_server is whether this session was created by a server.
Jesse Selover6b0d8222018-08-14 12:47:29 -04003452 bool is_server : 1;
David Benjaminbfdd1a92018-06-29 16:26:38 -04003453
3454 private:
3455 ~ssl_session_st();
3456 friend void SSL_SESSION_free(SSL_SESSION *);
David Benjamin1c68fa22018-06-02 19:02:46 -04003457};
3458
David Benjamine39ac8f2017-07-20 12:22:21 -04003459
David Benjaminc11ea9422017-08-29 16:33:21 -04003460#endif // OPENSSL_HEADER_SSL_INTERNAL_H