blob: b355c7f106a81634109f6766f89a95b504abd6b9 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE.
140 */
141
Adam Langleye9ada862015-05-11 17:20:37 -0700142#ifndef OPENSSL_HEADER_SSL_INTERNAL_H
143#define OPENSSL_HEADER_SSL_INTERNAL_H
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144
145#include <openssl/base.h>
146
Robert Sloanfe7cd212017-08-07 09:03:39 -0700147#include <stdlib.h>
148
Robert Sloan4562e9d2017-10-02 10:26:51 -0700149#include <limits>
Robert Sloanfe7cd212017-08-07 09:03:39 -0700150#include <new>
Robert Sloanb6d070c2017-07-24 08:40:01 -0700151#include <type_traits>
152#include <utility>
153
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154#include <openssl/aead.h>
Robert Sloanb6d070c2017-07-24 08:40:01 -0700155#include <openssl/err.h>
Robert Sloan921ef2c2017-10-17 09:02:20 -0700156#include <openssl/lhash.h>
Robert Sloanb6d070c2017-07-24 08:40:01 -0700157#include <openssl/mem.h>
Robert Sloan4562e9d2017-10-02 10:26:51 -0700158#include <openssl/span.h>
Robert Sloand9e572d2018-08-27 12:27:00 -0700159#include <openssl/ssl.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800160#include <openssl/stack.h>
161
Robert Sloan921ef2c2017-10-17 09:02:20 -0700162#include "../crypto/err/internal.h"
Robert Sloan4562e9d2017-10-02 10:26:51 -0700163#include "../crypto/internal.h"
164
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400165
Adam Langleye9ada862015-05-11 17:20:37 -0700166#if defined(OPENSSL_WINDOWS)
Robert Sloana27a6a42017-09-05 08:39:28 -0700167// Windows defines struct timeval in winsock2.h.
David Benjamin6e899c72016-06-09 18:02:18 -0400168OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langleye9ada862015-05-11 17:20:37 -0700169#include <winsock2.h>
David Benjamin6e899c72016-06-09 18:02:18 -0400170OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langleye9ada862015-05-11 17:20:37 -0700171#else
Adam Langley4139edb2016-01-13 15:00:54 -0800172#include <sys/time.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700173#endif
174
David Benjaminc895d6b2016-08-11 13:26:41 -0400175
Robert Sloan726e9d12018-09-11 11:45:04 -0700176BSSL_NAMESPACE_BEGIN
Robert Sloanb6d070c2017-07-24 08:40:01 -0700177
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100178struct SSL_CONFIG;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700179struct SSL_HANDSHAKE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700180struct SSL_PROTOCOL_METHOD;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100181struct SSL_X509_METHOD;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700182
Robert Sloana27a6a42017-09-05 08:39:28 -0700183// C++ utilities.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700184
Robert Sloana27a6a42017-09-05 08:39:28 -0700185// New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It
186// returns nullptr on allocation error. It only implements single-object
187// allocation and not new T[n].
188//
189// Note: unlike |new|, this does not support non-public constructors.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700190template <typename T, typename... Args>
191T *New(Args &&... args) {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700192 void *t = OPENSSL_malloc(sizeof(T));
Robert Sloanb6d070c2017-07-24 08:40:01 -0700193 if (t == nullptr) {
194 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
195 return nullptr;
196 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700197 return new (t) T(std::forward<Args>(args)...);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700198}
199
Robert Sloana27a6a42017-09-05 08:39:28 -0700200// Delete behaves like |delete| but uses |OPENSSL_free| to release memory.
201//
202// Note: unlike |delete| this does not support non-public destructors.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700203template <typename T>
204void Delete(T *t) {
205 if (t != nullptr) {
206 t->~T();
207 OPENSSL_free(t);
208 }
209}
210
Robert Sloana27a6a42017-09-05 08:39:28 -0700211// All types with kAllowUniquePtr set may be used with UniquePtr. Other types
212// may be C structs which require a |BORINGSSL_MAKE_DELETER| registration.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700213namespace internal {
214template <typename T>
Robert Sloanfe7cd212017-08-07 09:03:39 -0700215struct DeleterImpl<T, typename std::enable_if<T::kAllowUniquePtr>::type> {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700216 static void Free(T *t) { Delete(t); }
217};
Robert Sloand9e572d2018-08-27 12:27:00 -0700218} // namespace internal
Robert Sloanb6d070c2017-07-24 08:40:01 -0700219
Robert Sloana27a6a42017-09-05 08:39:28 -0700220// MakeUnique behaves like |std::make_unique| but returns nullptr on allocation
221// error.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700222template <typename T, typename... Args>
223UniquePtr<T> MakeUnique(Args &&... args) {
224 return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
225}
226
Robert Sloanfe7cd212017-08-07 09:03:39 -0700227#if defined(BORINGSSL_ALLOW_CXX_RUNTIME)
228#define HAS_VIRTUAL_DESTRUCTOR
229#define PURE_VIRTUAL = 0
230#else
Robert Sloana27a6a42017-09-05 08:39:28 -0700231// HAS_VIRTUAL_DESTRUCTOR should be declared in any base class which defines a
232// virtual destructor. This avoids a dependency on |_ZdlPv| and prevents the
233// class from being used with |delete|.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700234#define HAS_VIRTUAL_DESTRUCTOR \
235 void operator delete(void *) { abort(); }
236
Robert Sloana27a6a42017-09-05 08:39:28 -0700237// PURE_VIRTUAL should be used instead of = 0 when defining pure-virtual
238// functions. This avoids a dependency on |__cxa_pure_virtual| but loses
239// compile-time checking.
Robert Sloand9e572d2018-08-27 12:27:00 -0700240#define PURE_VIRTUAL \
241 { abort(); }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700242#endif
243
Robert Sloan921ef2c2017-10-17 09:02:20 -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
Robert Sloan4562e9d2017-10-02 10:26:51 -0700252// 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_; }
Robert Sloanae1abf92017-10-05 12:50:08 -0700273 bool empty() const { return size_ == 0; }
Robert Sloan4562e9d2017-10-02 10:26:51 -0700274
275 const T &operator[](size_t i) const { return data_[i]; }
276 T &operator[](size_t i) { return data_[i]; }
277
278 T *begin() { return data_; }
279 const T *cbegin() const { return data_; }
280 T *end() { return data_ + size_; }
281 const T *cend() const { return data_ + size_; }
282
283 void Reset() { Reset(nullptr, 0); }
284
285 // Reset releases the current contents of the array and takes ownership of the
286 // raw pointer supplied by the caller.
287 void Reset(T *new_data, size_t new_size) {
288 for (size_t i = 0; i < size_; i++) {
289 data_[i].~T();
290 }
291 OPENSSL_free(data_);
292 data_ = new_data;
293 size_ = new_size;
294 }
295
296 // Release releases ownership of the array to a raw pointer supplied by the
297 // caller.
298 void Release(T **out, size_t *out_size) {
299 *out = data_;
300 *out_size = size_;
301 data_ = nullptr;
302 size_ = 0;
303 }
304
305 // Init replaces the array with a newly-allocated array of |new_size|
306 // default-constructed copies of |T|. It returns true on success and false on
307 // error.
308 //
309 // Note that if |T| is a primitive type like |uint8_t|, it is uninitialized.
310 bool Init(size_t new_size) {
311 Reset();
312 if (new_size == 0) {
313 return true;
314 }
315
316 if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) {
317 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
318 return false;
319 }
Robert Sloand9e572d2018-08-27 12:27:00 -0700320 data_ = reinterpret_cast<T *>(OPENSSL_malloc(new_size * sizeof(T)));
Robert Sloan4562e9d2017-10-02 10:26:51 -0700321 if (data_ == nullptr) {
322 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
323 return false;
324 }
325 size_ = new_size;
326 for (size_t i = 0; i < size_; i++) {
327 new (&data_[i]) T;
328 }
329 return true;
330 }
331
332 // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns
333 // true on success and false on error.
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700334 bool CopyFrom(Span<const T> in) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700335 if (!Init(in.size())) {
336 return false;
337 }
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700338 OPENSSL_memcpy(data_, in.data(), sizeof(T) * in.size());
Robert Sloan4562e9d2017-10-02 10:26:51 -0700339 return true;
340 }
341
Robert Sloand9e572d2018-08-27 12:27:00 -0700342 // Shrink shrinks the stored size of the array to |new_size|. It crashes if
343 // the new size is larger. Note this does not shrink the allocation itself.
344 void Shrink(size_t new_size) {
345 if (new_size > size_) {
346 abort();
347 }
348 size_ = new_size;
349 }
350
Robert Sloan4562e9d2017-10-02 10:26:51 -0700351 private:
352 T *data_ = nullptr;
353 size_t size_ = 0;
354};
355
356// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
Robert Sloan8542c082018-02-05 09:07:34 -0800357OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
Robert Sloan4562e9d2017-10-02 10:26:51 -0700358
Robert Sloanf6200e72017-07-10 08:09:18 -0700359
Robert Sloana27a6a42017-09-05 08:39:28 -0700360// Protocol versions.
361//
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800362// Due to DTLS's historical wire version differences, we maintain two notions of
Robert Sloana27a6a42017-09-05 08:39:28 -0700363// version.
364//
365// The "version" or "wire version" is the actual 16-bit value that appears on
366// the wire. It uniquely identifies a version and is also used at API
367// boundaries. The set of supported versions differs between TLS and DTLS. Wire
368// versions are opaque values and may not be compared numerically.
369//
370// The "protocol version" identifies the high-level handshake variant being
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800371// used. DTLS versions map to the corresponding TLS versions. Protocol versions
372// are sequential and may be compared numerically.
Robert Sloanf6200e72017-07-10 08:09:18 -0700373
Robert Sloana27a6a42017-09-05 08:39:28 -0700374// ssl_protocol_version_from_wire sets |*out| to the protocol version
Robert Sloanae1abf92017-10-05 12:50:08 -0700375// corresponding to wire version |version| and returns true. If |version| is not
376// a valid TLS or DTLS version, it returns false.
Robert Sloana27a6a42017-09-05 08:39:28 -0700377//
378// Note this simultaneously handles both DTLS and TLS. Use one of the
379// higher-level functions below for most operations.
Robert Sloanae1abf92017-10-05 12:50:08 -0700380bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700381
Robert Sloana27a6a42017-09-05 08:39:28 -0700382// ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
383// minimum and maximum enabled protocol versions, respectively.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100384bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
Robert Sloanae1abf92017-10-05 12:50:08 -0700385 uint16_t *out_max_version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700386
Robert Sloanae1abf92017-10-05 12:50:08 -0700387// ssl_supports_version returns whether |hs| supports |version|.
388bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700389
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100390// ssl_method_supports_version returns whether |method| supports |version|.
391bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
392 uint16_t version);
393
Robert Sloana27a6a42017-09-05 08:39:28 -0700394// ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in
395// decreasing preference order.
Robert Sloanae1abf92017-10-05 12:50:08 -0700396bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb);
Robert Sloanf6200e72017-07-10 08:09:18 -0700397
Robert Sloana27a6a42017-09-05 08:39:28 -0700398// ssl_negotiate_version negotiates a common version based on |hs|'s preferences
Robert Sloanae1abf92017-10-05 12:50:08 -0700399// and the peer preference list in |peer_versions|. On success, it returns true
400// and sets |*out_version| to the selected version. Otherwise, it returns false
Robert Sloana27a6a42017-09-05 08:39:28 -0700401// and sets |*out_alert| to an alert to send.
Robert Sloanae1abf92017-10-05 12:50:08 -0700402bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
403 uint16_t *out_version, const CBS *peer_versions);
Robert Sloanf6200e72017-07-10 08:09:18 -0700404
Robert Sloan921ef2c2017-10-17 09:02:20 -0700405// ssl_protocol_version returns |ssl|'s protocol version. It is an error to
Robert Sloana27a6a42017-09-05 08:39:28 -0700406// call this function before the version is determined.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700407uint16_t ssl_protocol_version(const SSL *ssl);
Robert Sloanf6200e72017-07-10 08:09:18 -0700408
Robert Sloana27a6a42017-09-05 08:39:28 -0700409// Cipher suites.
Adam Langleye9ada862015-05-11 17:20:37 -0700410
Robert Sloan726e9d12018-09-11 11:45:04 -0700411BSSL_NAMESPACE_END
Robert Sloandb4251a2017-09-18 09:38:15 -0700412
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
Robert Sloan726e9d12018-09-11 11:45:04 -0700429BSSL_NAMESPACE_BEGIN
Robert Sloandb4251a2017-09-18 09:38:15 -0700430
Robert Sloana27a6a42017-09-05 08:39:28 -0700431// Bits for |algorithm_mkey| (key exchange algorithm).
Robert Sloana12bf462017-07-17 07:08:26 -0700432#define SSL_kRSA 0x00000001u
433#define SSL_kECDHE 0x00000002u
Robert Sloana27a6a42017-09-05 08:39:28 -0700434// SSL_kPSK is only set for plain PSK, not ECDHE_PSK.
Robert Sloana12bf462017-07-17 07:08:26 -0700435#define SSL_kPSK 0x00000004u
436#define SSL_kGENERIC 0x00000008u
Adam Langleye9ada862015-05-11 17:20:37 -0700437
Robert Sloana27a6a42017-09-05 08:39:28 -0700438// Bits for |algorithm_auth| (server authentication).
Robert Sloana12bf462017-07-17 07:08:26 -0700439#define SSL_aRSA 0x00000001u
440#define SSL_aECDSA 0x00000002u
Robert Sloana27a6a42017-09-05 08:39:28 -0700441// SSL_aPSK is set for both PSK and ECDHE_PSK.
Robert Sloana12bf462017-07-17 07:08:26 -0700442#define SSL_aPSK 0x00000004u
443#define SSL_aGENERIC 0x00000008u
Adam Langleye9ada862015-05-11 17:20:37 -0700444
David Benjamind316cba2016-06-02 16:17:39 -0400445#define SSL_aCERT (SSL_aRSA | SSL_aECDSA)
446
Robert Sloana27a6a42017-09-05 08:39:28 -0700447// Bits for |algorithm_enc| (symmetric encryption).
Robert Sloand9e572d2018-08-27 12:27:00 -0700448#define SSL_3DES 0x00000001u
449#define SSL_AES128 0x00000002u
450#define SSL_AES256 0x00000004u
451#define SSL_AES128GCM 0x00000008u
452#define SSL_AES256GCM 0x00000010u
453#define SSL_eNULL 0x00000020u
454#define SSL_CHACHA20POLY1305 0x00000040u
Adam Langleye9ada862015-05-11 17:20:37 -0700455
456#define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
457
Robert Sloana27a6a42017-09-05 08:39:28 -0700458// Bits for |algorithm_mac| (symmetric authentication).
Robert Sloana12bf462017-07-17 07:08:26 -0700459#define SSL_SHA1 0x00000001u
Robert Sloana27a6a42017-09-05 08:39:28 -0700460// SSL_AEAD is set for all AEADs.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100461#define SSL_AEAD 0x00000002u
Adam Langleye9ada862015-05-11 17:20:37 -0700462
Robert Sloana27a6a42017-09-05 08:39:28 -0700463// Bits for |algorithm_prf| (handshake digest).
Kenny Rootb8494592015-09-25 02:29:14 +0000464#define SSL_HANDSHAKE_MAC_DEFAULT 0x1
465#define SSL_HANDSHAKE_MAC_SHA256 0x2
466#define SSL_HANDSHAKE_MAC_SHA384 0x4
Adam Langleye9ada862015-05-11 17:20:37 -0700467
Pete Bentley0c61efe2019-08-13 09:32:23 +0100468// SSL_MAX_MD_SIZE is size of the largest hash function used in TLS, SHA-384.
469#define SSL_MAX_MD_SIZE 48
470
Robert Sloanc6ebb282018-04-30 10:10:26 -0700471// 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);
Robert Sloancbf5ea62018-11-05 11:56:34 -0800509 bool Init(const SSLCipherPreferenceList &);
510
511 void Remove(const SSL_CIPHER *cipher);
Robert Sloanc6ebb282018-04-30 10:10:26 -0700512
513 UniquePtr<STACK_OF(SSL_CIPHER)> ciphers;
514 bool *in_group_flags = nullptr;
515};
Adam Langleye9ada862015-05-11 17:20:37 -0700516
Robert Sloancbf5ea62018-11-05 11:56:34 -0800517// AllCiphers returns an array of all supported ciphers, sorted by id.
518Span<const SSL_CIPHER> AllCiphers();
519
Robert Sloana27a6a42017-09-05 08:39:28 -0700520// 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
Robert Sloanae1abf92017-10-05 12:50:08 -0700524// 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,
Robert Sloan726e9d12018-09-11 11:45:04 -0700528 uint16_t version, bool is_dtls);
Adam Langleye9ada862015-05-11 17:20:37 -0700529
Robert Sloana27a6a42017-09-05 08:39:28 -0700530// ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and
531// |cipher|.
Robert Sloan84377092017-08-14 09:33:19 -0700532const EVP_MD *ssl_get_handshake_digest(uint16_t version,
533 const SSL_CIPHER *cipher);
Adam Langleye9ada862015-05-11 17:20:37 -0700534
Robert Sloanb1b54b82017-11-06 13:50:02 -0800535// ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a
Robert Sloanc6ebb282018-04-30 10:10:26 -0700536// 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|.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100540bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list,
Robert Sloanc6ebb282018-04-30 10:10:26 -0700541 const char *rule_str, bool strict);
Adam Langleye9ada862015-05-11 17:20:37 -0700542
Robert Sloana27a6a42017-09-05 08:39:28 -0700543// ssl_cipher_get_value returns the cipher suite id of |cipher|.
Adam Langleyf4e42722015-06-04 17:45:09 -0700544uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
545
Robert Sloana27a6a42017-09-05 08:39:28 -0700546// 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.
Robert Sloan572a4e22017-04-17 10:52:19 -0700548uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key);
Adam Langleye9ada862015-05-11 17:20:37 -0700549
Robert Sloanae1abf92017-10-05 12:50:08 -0700550// 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);
Adam Langleye9ada862015-05-11 17:20:37 -0700553
Robert Sloanae1abf92017-10-05 12:50:08 -0700554// ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a
555// ServerKeyExchange message.
Robert Sloana27a6a42017-09-05 08:39:28 -0700556//
Robert Sloanae1abf92017-10-05 12:50:08 -0700557// This function may return false while still allowing |cipher| an optional
Robert Sloana27a6a42017-09-05 08:39:28 -0700558// ServerKeyExchange. This is the case for plain PSK ciphers.
Robert Sloanae1abf92017-10-05 12:50:08 -0700559bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
Adam Langleye9ada862015-05-11 17:20:37 -0700560
Robert Sloana27a6a42017-09-05 08:39:28 -0700561// 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.
Kenny Rootb8494592015-09-25 02:29:14 +0000564size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
565
Pete Bentley0c61efe2019-08-13 09:32:23 +0100566// 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
Adam Langleye9ada862015-05-11 17:20:37 -0700572
Robert Sloana27a6a42017-09-05 08:39:28 -0700573// Transcript layer.
Robert Sloan5d625782017-02-13 09:55:39 -0800574
Robert Sloana27a6a42017-09-05 08:39:28 -0700575// SSLTranscript maintains the handshake transcript as a combination of a
576// buffer and running hash.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700577class SSLTranscript {
578 public:
579 SSLTranscript();
580 ~SSLTranscript();
581
Robert Sloana27a6a42017-09-05 08:39:28 -0700582 // 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.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700585 bool Init();
586
Robert Sloana27a6a42017-09-05 08:39:28 -0700587 // 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.
Robert Sloan84377092017-08-14 09:33:19 -0700591 bool InitHash(uint16_t version, const SSL_CIPHER *cipher);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700592
Robert Sloanb1b54b82017-11-06 13:50:02 -0800593 // UpdateForHelloRetryRequest resets the rolling hash with the
594 // HelloRetryRequest construction. It returns true on success and false on
595 // failure. It is an error to call this function before the handshake buffer
596 // is released.
597 bool UpdateForHelloRetryRequest();
598
599 // CopyHashContext copies the hash context into |ctx| and returns true on
600 // success.
601 bool CopyHashContext(EVP_MD_CTX *ctx);
602
Robert Sloan921ef2c2017-10-17 09:02:20 -0700603 Span<const uint8_t> buffer() {
604 return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
605 buffer_->length);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700606 }
Robert Sloanb6d070c2017-07-24 08:40:01 -0700607
Robert Sloana27a6a42017-09-05 08:39:28 -0700608 // FreeBuffer releases the handshake buffer. Subsequent calls to
609 // |Update| will not update the handshake buffer.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700610 void FreeBuffer();
611
Robert Sloana27a6a42017-09-05 08:39:28 -0700612 // DigestLen returns the length of the PRF hash.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700613 size_t DigestLen() const;
614
Robert Sloana27a6a42017-09-05 08:39:28 -0700615 // Digest returns the PRF hash. For TLS 1.1 and below, this is
616 // |EVP_md5_sha1|.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700617 const EVP_MD *Digest() const;
618
Robert Sloana27a6a42017-09-05 08:39:28 -0700619 // Update adds |in| to the handshake buffer and handshake hash, whichever is
620 // enabled. It returns true on success and false on failure.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700621 bool Update(Span<const uint8_t> in);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700622
Robert Sloana27a6a42017-09-05 08:39:28 -0700623 // GetHash writes the handshake hash to |out| which must have room for at
624 // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
625 // the number of bytes written. Otherwise, it returns false.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700626 bool GetHash(uint8_t *out, size_t *out_len);
627
Robert Sloana27a6a42017-09-05 08:39:28 -0700628 // GetFinishedMAC computes the MAC for the Finished message into the bytes
629 // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
630 // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
631 // on failure.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700632 bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
Robert Sloana27a6a42017-09-05 08:39:28 -0700633 bool from_server);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700634
635 private:
Robert Sloana27a6a42017-09-05 08:39:28 -0700636 // buffer_, if non-null, contains the handshake transcript.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700637 UniquePtr<BUF_MEM> buffer_;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100638 // hash, if initialized with an |EVP_MD|, maintains the handshake hash.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700639 ScopedEVP_MD_CTX hash_;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700640};
Robert Sloan5d625782017-02-13 09:55:39 -0800641
Robert Sloanb1b54b82017-11-06 13:50:02 -0800642// tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
643// as the secret and |label| as the label. |seed1| and |seed2| are concatenated
644// to form the seed parameter. It returns true on success and false on failure.
645bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
646 Span<const uint8_t> secret, Span<const char> label,
647 Span<const uint8_t> seed1, Span<const uint8_t> seed2);
Robert Sloan5d625782017-02-13 09:55:39 -0800648
649
Robert Sloana27a6a42017-09-05 08:39:28 -0700650// Encryption layer.
Adam Langleyf4e42722015-06-04 17:45:09 -0700651
Robert Sloana27a6a42017-09-05 08:39:28 -0700652// SSLAEADContext contains information about an AEAD that is being used to
653// encrypt an SSL connection.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700654class SSLAEADContext {
655 public:
Robert Sloandb4251a2017-09-18 09:38:15 -0700656 SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700657 ~SSLAEADContext();
Robert Sloanfe7cd212017-08-07 09:03:39 -0700658 static constexpr bool kAllowUniquePtr = true;
659
Robert Sloanb6d070c2017-07-24 08:40:01 -0700660 SSLAEADContext(const SSLAEADContext &&) = delete;
661 SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
662
Robert Sloana27a6a42017-09-05 08:39:28 -0700663 // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
Robert Sloandb4251a2017-09-18 09:38:15 -0700664 static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700665
Robert Sloana27a6a42017-09-05 08:39:28 -0700666 // Create creates an |SSLAEADContext| using the supplied key material. It
667 // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
668 // resulting object, depending on |direction|. |version| is the normalized
669 // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
Robert Sloanae1abf92017-10-05 12:50:08 -0700670 static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
Robert Sloan726e9d12018-09-11 11:45:04 -0700671 uint16_t version, bool is_dtls,
Robert Sloanae1abf92017-10-05 12:50:08 -0700672 const SSL_CIPHER *cipher,
673 Span<const uint8_t> enc_key,
674 Span<const uint8_t> mac_key,
675 Span<const uint8_t> fixed_iv);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700676
Robert Sloancbf5ea62018-11-05 11:56:34 -0800677 // CreatePlaceholderForQUIC creates a placeholder |SSLAEADContext| for the
678 // given cipher and version. The resulting object can be queried for various
679 // properties but cannot encrypt or decrypt data.
680 static UniquePtr<SSLAEADContext> CreatePlaceholderForQUIC(
681 uint16_t version, const SSL_CIPHER *cipher);
682
Robert Sloandb4251a2017-09-18 09:38:15 -0700683 // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
684 // cipher, to make version-specific determinations in the record layer prior
685 // to a cipher being selected.
686 void SetVersionIfNullCipher(uint16_t version);
687
688 // ProtocolVersion returns the protocol version associated with this
689 // SSLAEADContext. It can only be called once |version_| has been set to a
690 // valid value.
691 uint16_t ProtocolVersion() const;
692
693 // RecordVersion returns the record version that should be used with this
694 // SSLAEADContext for record construction and crypto.
695 uint16_t RecordVersion() const;
696
Robert Sloanb6d070c2017-07-24 08:40:01 -0700697 const SSL_CIPHER *cipher() const { return cipher_; }
698
Robert Sloana27a6a42017-09-05 08:39:28 -0700699 // is_null_cipher returns true if this is the null cipher.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700700 bool is_null_cipher() const { return !cipher_; }
701
Robert Sloana27a6a42017-09-05 08:39:28 -0700702 // ExplicitNonceLen returns the length of the explicit nonce.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700703 size_t ExplicitNonceLen() const;
704
Robert Sloana27a6a42017-09-05 08:39:28 -0700705 // MaxOverhead returns the maximum overhead of calling |Seal|.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700706 size_t MaxOverhead() const;
707
Robert Sloana27a6a42017-09-05 08:39:28 -0700708 // SuffixLen calculates the suffix length written by |SealScatter| and writes
709 // it to |*out_suffix_len|. It returns true on success and false on error.
710 // |in_len| and |extra_in_len| should equal the argument of the same names
711 // passed to |SealScatter|.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700712 bool SuffixLen(size_t *out_suffix_len, size_t in_len,
713 size_t extra_in_len) const;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700714
Robert Sloan15c0b352018-04-16 08:36:46 -0700715 // CiphertextLen calculates the total ciphertext length written by
716 // |SealScatter| and writes it to |*out_len|. It returns true on success and
717 // false on error. |in_len| and |extra_in_len| should equal the argument of
718 // the same names passed to |SealScatter|.
719 bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const;
720
Robert Sloan921ef2c2017-10-17 09:02:20 -0700721 // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
722 // to the plaintext in |in| and returns true. Otherwise, it returns
723 // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
724 bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
Robert Sloan15c0b352018-04-16 08:36:46 -0700725 const uint8_t seqnum[8], Span<const uint8_t> header,
726 Span<uint8_t> in);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700727
Robert Sloana27a6a42017-09-05 08:39:28 -0700728 // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
729 // result to |out|. It returns true on success and false on error.
730 //
731 // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700732 bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
Robert Sloan15c0b352018-04-16 08:36:46 -0700733 uint16_t record_version, const uint8_t seqnum[8],
734 Span<const uint8_t> header, const uint8_t *in, size_t in_len);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700735
Robert Sloana27a6a42017-09-05 08:39:28 -0700736 // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
737 // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
738 // success and zero on error.
739 //
740 // On successful return, exactly |ExplicitNonceLen| bytes are written to
741 // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
742 // |out_suffix|.
743 //
744 // |extra_in| may point to an additional plaintext buffer. If present,
745 // |extra_in_len| additional bytes are encrypted and authenticated, and the
746 // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
747 // be used to size |out_suffix| accordingly.
748 //
749 // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
750 // alias anything.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700751 bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
Robert Sloandb4251a2017-09-18 09:38:15 -0700752 uint8_t type, uint16_t record_version,
Robert Sloan15c0b352018-04-16 08:36:46 -0700753 const uint8_t seqnum[8], Span<const uint8_t> header,
754 const uint8_t *in, size_t in_len, const uint8_t *extra_in,
755 size_t extra_in_len);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700756
757 bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
758
759 private:
Robert Sloan15c0b352018-04-16 08:36:46 -0700760 // GetAdditionalData returns the additional data, writing into |storage| if
761 // necessary.
762 Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type,
763 uint16_t record_version,
764 const uint8_t seqnum[8],
765 size_t plaintext_len,
766 Span<const uint8_t> header);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700767
768 const SSL_CIPHER *cipher_;
769 ScopedEVP_AEAD_CTX ctx_;
Robert Sloana27a6a42017-09-05 08:39:28 -0700770 // fixed_nonce_ contains any bytes of the nonce that are fixed for all
771 // records.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700772 uint8_t fixed_nonce_[12];
773 uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
Robert Sloandb4251a2017-09-18 09:38:15 -0700774 // version_ is the wire version that should be used with this AEAD.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700775 uint16_t version_;
Robert Sloandb4251a2017-09-18 09:38:15 -0700776 // is_dtls_ is whether DTLS is being used with this AEAD.
777 bool is_dtls_;
Robert Sloana27a6a42017-09-05 08:39:28 -0700778 // variable_nonce_included_in_record_ is true if the variable nonce
779 // for a record is included as a prefix before the ciphertext.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700780 bool variable_nonce_included_in_record_ : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -0700781 // random_variable_nonce_ is true if the variable nonce is
782 // randomly generated, rather than derived from the sequence
783 // number.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700784 bool random_variable_nonce_ : 1;
Robert Sloan15c0b352018-04-16 08:36:46 -0700785 // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
786 // variable nonce rather than prepended.
787 bool xor_fixed_nonce_ : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -0700788 // omit_length_in_ad_ is true if the length should be omitted in the
789 // AEAD's ad parameter.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700790 bool omit_length_in_ad_ : 1;
Robert Sloan15c0b352018-04-16 08:36:46 -0700791 // ad_is_header_ is true if the AEAD's ad parameter is the record header.
792 bool ad_is_header_ : 1;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700793};
Robert Sloana12bf462017-07-17 07:08:26 -0700794
Adam Langleyf4e42722015-06-04 17:45:09 -0700795
Robert Sloana27a6a42017-09-05 08:39:28 -0700796// DTLS replay bitmap.
Kenny Rootb8494592015-09-25 02:29:14 +0000797
Robert Sloana27a6a42017-09-05 08:39:28 -0700798// DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
799// replayed packets. It should be initialized by zeroing every field.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700800struct DTLS1_BITMAP {
Robert Sloana27a6a42017-09-05 08:39:28 -0700801 // map is a bit mask of the last 64 sequence numbers. Bit
802 // |1<<i| corresponds to |max_seq_num - i|.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700803 uint64_t map = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -0700804 // max_seq_num is the largest sequence number seen so far as a 64-bit
805 // integer.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700806 uint64_t max_seq_num = 0;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700807};
Kenny Rootb8494592015-09-25 02:29:14 +0000808
809
Robert Sloana27a6a42017-09-05 08:39:28 -0700810// Record layer.
Kenny Rootb8494592015-09-25 02:29:14 +0000811
Robert Sloana27a6a42017-09-05 08:39:28 -0700812// ssl_record_sequence_update increments the sequence number in |seq|. It
Robert Sloan726e9d12018-09-11 11:45:04 -0700813// returns true on success and false on wraparound.
814bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
David Benjamin4969cc92016-04-22 15:02:23 -0400815
Robert Sloana27a6a42017-09-05 08:39:28 -0700816// ssl_record_prefix_len returns the length of the prefix before the ciphertext
817// of a record for |ssl|.
818//
819// TODO(davidben): Expose this as part of public API once the high-level
820// buffer-free APIs are available.
Kenny Rootb8494592015-09-25 02:29:14 +0000821size_t ssl_record_prefix_len(const SSL *ssl);
822
823enum ssl_open_record_t {
824 ssl_open_record_success,
825 ssl_open_record_discard,
826 ssl_open_record_partial,
David Benjamin6e899c72016-06-09 18:02:18 -0400827 ssl_open_record_close_notify,
Kenny Rootb8494592015-09-25 02:29:14 +0000828 ssl_open_record_error,
829};
830
Robert Sloana27a6a42017-09-05 08:39:28 -0700831// tls_open_record decrypts a record from |in| in-place.
832//
833// If the input did not contain a complete record, it returns
834// |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
835// bytes necessary. It is guaranteed that a successful call to |tls_open_record|
836// will consume at least that many bytes.
837//
838// Otherwise, it sets |*out_consumed| to the number of bytes of input
839// consumed. Note that input may be consumed on all return codes if a record was
840// decrypted.
841//
842// On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
843// record type and |*out| to the record body in |in|. Note that |*out| may be
844// empty.
845//
846// If a record was successfully processed but should be discarded, it returns
847// |ssl_open_record_discard|.
848//
Robert Sloan921ef2c2017-10-17 09:02:20 -0700849// If a record was successfully processed but is a close_notify, it returns
850// |ssl_open_record_close_notify|.
Robert Sloana27a6a42017-09-05 08:39:28 -0700851//
Robert Sloan921ef2c2017-10-17 09:02:20 -0700852// On failure or fatal alert, it returns |ssl_open_record_error| and sets
853// |*out_alert| to an alert to emit, or zero if no alert should be emitted.
854enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
855 Span<uint8_t> *out, size_t *out_consumed,
856 uint8_t *out_alert, Span<uint8_t> in);
Kenny Rootb8494592015-09-25 02:29:14 +0000857
Robert Sloan921ef2c2017-10-17 09:02:20 -0700858// dtls_open_record implements |tls_open_record| for DTLS. It only returns
859// |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
860// zero. The caller should read one packet and try again.
861enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
862 Span<uint8_t> *out,
David Benjamin6e899c72016-06-09 18:02:18 -0400863 size_t *out_consumed,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700864 uint8_t *out_alert, Span<uint8_t> in);
Kenny Rootb8494592015-09-25 02:29:14 +0000865
Robert Sloana27a6a42017-09-05 08:39:28 -0700866// ssl_seal_align_prefix_len returns the length of the prefix before the start
867// of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
868// use this to align buffers.
869//
870// Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
871// record and is the offset into second record's ciphertext. Thus sealing a
872// small record may result in a smaller output than this value.
873//
874// TODO(davidben): Is this alignment valuable? Record-splitting makes this a
875// mess.
David Benjamin6e899c72016-06-09 18:02:18 -0400876size_t ssl_seal_align_prefix_len(const SSL *ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000877
Robert Sloana27a6a42017-09-05 08:39:28 -0700878// tls_seal_record seals a new record of type |type| and body |in| and writes it
Robert Sloan726e9d12018-09-11 11:45:04 -0700879// to |out|. At most |max_out| bytes will be written. It returns true on success
880// and false on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC
881// 1/n-1 record splitting and may write two records concatenated.
Robert Sloana27a6a42017-09-05 08:39:28 -0700882//
883// For a large record, the bulk of the ciphertext will begin
884// |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
885// improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
886// bytes to |out|.
887//
888// |in| and |out| may not alias.
Robert Sloan726e9d12018-09-11 11:45:04 -0700889bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
890 uint8_t type, const uint8_t *in, size_t in_len);
Kenny Rootb8494592015-09-25 02:29:14 +0000891
892enum dtls1_use_epoch_t {
893 dtls1_use_previous_epoch,
894 dtls1_use_current_epoch,
895};
896
Robert Sloana27a6a42017-09-05 08:39:28 -0700897// dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
898// record.
Robert Sloan4d1ac502017-02-06 08:36:14 -0800899size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
900
Robert Sloana27a6a42017-09-05 08:39:28 -0700901// dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
902// front of the plaintext when sealing a record in-place.
David Benjaminf31229b2017-01-25 14:08:15 -0500903size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
904
Robert Sloana27a6a42017-09-05 08:39:28 -0700905// dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
906// which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
907// may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
908// ahead of |out|.
Robert Sloan726e9d12018-09-11 11:45:04 -0700909bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
910 uint8_t type, const uint8_t *in, size_t in_len,
911 enum dtls1_use_epoch_t use_epoch);
Kenny Rootb8494592015-09-25 02:29:14 +0000912
Robert Sloana27a6a42017-09-05 08:39:28 -0700913// ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
914// state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
915// |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
916// appropriate.
David Benjamin6e899c72016-06-09 18:02:18 -0400917enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700918 Span<const uint8_t> in);
David Benjamin6e899c72016-06-09 18:02:18 -0400919
Kenny Rootb8494592015-09-25 02:29:14 +0000920
Robert Sloana27a6a42017-09-05 08:39:28 -0700921// Private key operations.
Kenny Rootb8494592015-09-25 02:29:14 +0000922
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800923// ssl_has_private_key returns whether |hs| has a private key configured.
924bool ssl_has_private_key(const SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400925
Robert Sloana27a6a42017-09-05 08:39:28 -0700926// ssl_private_key_* perform the corresponding operation on
927// |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
928// call the corresponding function or |complete| depending on whether there is a
929// pending operation. Otherwise, they implement the operation with
930// |EVP_PKEY|.
Kenny Rootb8494592015-09-25 02:29:14 +0000931
Kenny Rootb8494592015-09-25 02:29:14 +0000932enum ssl_private_key_result_t ssl_private_key_sign(
Robert Sloane56da3e2017-06-26 08:26:42 -0700933 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700934 uint16_t sigalg, Span<const uint8_t> in);
Kenny Rootb8494592015-09-25 02:29:14 +0000935
Robert Sloan921ef2c2017-10-17 09:02:20 -0700936enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
937 uint8_t *out,
938 size_t *out_len,
939 size_t max_out,
940 Span<const uint8_t> in);
Kenny Roote99801b2015-11-06 15:31:15 -0800941
Robert Sloan921ef2c2017-10-17 09:02:20 -0700942// ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
943// key supports |sigalg|.
944bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
Robert Sloand9e572d2018-08-27 12:27:00 -0700945 uint16_t sigalg);
David Benjaminc895d6b2016-08-11 13:26:41 -0400946
Robert Sloana27a6a42017-09-05 08:39:28 -0700947// ssl_public_key_verify verifies that the |signature| is valid for the public
948// key |pkey| and input |in|, using the signature algorithm |sigalg|.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700949bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
950 uint16_t sigalg, EVP_PKEY *pkey,
951 Span<const uint8_t> in);
Kenny Roote99801b2015-11-06 15:31:15 -0800952
Kenny Rootb8494592015-09-25 02:29:14 +0000953
Robert Sloana27a6a42017-09-05 08:39:28 -0700954// Key shares.
Adam Langley4139edb2016-01-13 15:00:54 -0800955
Robert Sloana27a6a42017-09-05 08:39:28 -0700956// SSLKeyShare abstracts over Diffie-Hellman-like key exchanges.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700957class SSLKeyShare {
958 public:
959 virtual ~SSLKeyShare() {}
Robert Sloanfe7cd212017-08-07 09:03:39 -0700960 static constexpr bool kAllowUniquePtr = true;
961 HAS_VIRTUAL_DESTRUCTOR
Steven Valdez909b19f2016-11-21 15:35:44 -0500962
Robert Sloana27a6a42017-09-05 08:39:28 -0700963 // Create returns a SSLKeyShare instance for use with group |group_id| or
964 // nullptr on error.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700965 static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
Adam Langley4139edb2016-01-13 15:00:54 -0800966
Robert Sloandc2f6092018-04-10 10:22:33 -0700967 // Create deserializes an SSLKeyShare instance previously serialized by
968 // |Serialize|.
969 static UniquePtr<SSLKeyShare> Create(CBS *in);
970
Robert Sloana27a6a42017-09-05 08:39:28 -0700971 // GroupID returns the group ID.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700972 virtual uint16_t GroupID() const PURE_VIRTUAL;
Adam Langley4139edb2016-01-13 15:00:54 -0800973
Robert Sloana27a6a42017-09-05 08:39:28 -0700974 // Offer generates a keypair and writes the public value to
975 // |out_public_key|. It returns true on success and false on error.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700976 virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL;
Adam Langley4139edb2016-01-13 15:00:54 -0800977
Robert Sloan11c28bd2018-12-17 12:09:20 -0800978 // Accept performs a key exchange against the |peer_key| generated by |Offer|.
Robert Sloana27a6a42017-09-05 08:39:28 -0700979 // On success, it returns true, writes the public value to |out_public_key|,
Robert Sloan11c28bd2018-12-17 12:09:20 -0800980 // and sets |*out_secret| to the shared secret. On failure, it returns false
981 // and sets |*out_alert| to an alert to send to the peer.
Robert Sloana27a6a42017-09-05 08:39:28 -0700982 //
983 // The default implementation calls |Offer| and then |Finish|, assuming a key
984 // exchange protocol where the peers are symmetric.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700985 virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
986 uint8_t *out_alert, Span<const uint8_t> peer_key);
David Benjamind316cba2016-06-02 16:17:39 -0400987
Robert Sloana27a6a42017-09-05 08:39:28 -0700988 // Finish performs a key exchange against the |peer_key| generated by
Robert Sloan4562e9d2017-10-02 10:26:51 -0700989 // |Accept|. On success, it returns true and sets |*out_secret| to the shared
Robert Sloan11c28bd2018-12-17 12:09:20 -0800990 // secret. On failure, it returns false and sets |*out_alert| to an alert to
Robert Sloan4562e9d2017-10-02 10:26:51 -0700991 // send to the peer.
992 virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
993 Span<const uint8_t> peer_key) PURE_VIRTUAL;
Robert Sloandc2f6092018-04-10 10:22:33 -0700994
995 // Serialize writes the state of the key exchange to |out|, returning true if
996 // successful and false otherwise.
997 virtual bool Serialize(CBB *out) { return false; }
998
999 // Deserialize initializes the state of the key exchange from |in|, returning
1000 // true if successful and false otherwise. It is called by |Create|.
1001 virtual bool Deserialize(CBS *in) { return false; }
Steven Valdez909b19f2016-11-21 15:35:44 -05001002};
Adam Langley4139edb2016-01-13 15:00:54 -08001003
Robert Sloana51059f2018-11-12 13:38:50 -08001004struct NamedGroup {
1005 int nid;
1006 uint16_t group_id;
1007 const char name[8], alias[11];
1008};
1009
1010// NamedGroups returns all supported groups.
1011Span<const NamedGroup> NamedGroups();
1012
Robert Sloana27a6a42017-09-05 08:39:28 -07001013// ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
Robert Sloan726e9d12018-09-11 11:45:04 -07001014// sets |*out_group_id| to the group ID and returns true. Otherwise, it returns
1015// false.
1016bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
Adam Langley4139edb2016-01-13 15:00:54 -08001017
Robert Sloan726e9d12018-09-11 11:45:04 -07001018// ssl_name_to_group_id looks up the group corresponding to the |name| string of
1019// length |len|. On success, it sets |*out_group_id| to the group ID and returns
1020// true. Otherwise, it returns false.
1021bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001022
David Benjamind316cba2016-06-02 16:17:39 -04001023
Robert Sloana27a6a42017-09-05 08:39:28 -07001024// Handshake messages.
David Benjamind316cba2016-06-02 16:17:39 -04001025
Robert Sloan84377092017-08-14 09:33:19 -07001026struct SSLMessage {
1027 bool is_v2_hello;
1028 uint8_t type;
1029 CBS body;
Robert Sloana27a6a42017-09-05 08:39:28 -07001030 // raw is the entire serialized handshake message, including the TLS or DTLS
1031 // message header.
Robert Sloan84377092017-08-14 09:33:19 -07001032 CBS raw;
1033};
1034
Robert Sloana27a6a42017-09-05 08:39:28 -07001035// SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
1036// ChangeCipherSpec, in the longest handshake flight. Currently this is the
1037// client's second leg in a full handshake when client certificates, NPN, and
1038// Channel ID, are all enabled.
David Benjaminc895d6b2016-08-11 13:26:41 -04001039#define SSL_MAX_HANDSHAKE_FLIGHT 7
1040
Robert Sloand5c22152017-11-13 09:22:12 -08001041extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE];
Robert Sloand9e572d2018-08-27 12:27:00 -07001042extern const uint8_t kTLS12DowngradeRandom[8];
1043extern const uint8_t kTLS13DowngradeRandom[8];
Robert Sloanc9abfe42018-11-26 12:19:07 -08001044extern const uint8_t kJDK11DowngradeRandom[8];
Robert Sloand5c22152017-11-13 09:22:12 -08001045
Robert Sloana27a6a42017-09-05 08:39:28 -07001046// ssl_max_handshake_message_len returns the maximum number of bytes permitted
1047// in a handshake message for |ssl|.
David Benjamind316cba2016-06-02 16:17:39 -04001048size_t ssl_max_handshake_message_len(const SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08001049
Robert Sloan36272962017-10-23 10:28:39 -07001050// tls_can_accept_handshake_data returns whether |ssl| is able to accept more
1051// data into handshake buffer.
1052bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
1053
Robert Sloan921ef2c2017-10-17 09:02:20 -07001054// tls_has_unprocessed_handshake_data returns whether there is buffered
1055// handshake data that has not been consumed by |get_message|.
1056bool tls_has_unprocessed_handshake_data(const SSL *ssl);
1057
Robert Sloana51059f2018-11-12 13:38:50 -08001058// tls_append_handshake_data appends |data| to the handshake buffer. It returns
1059// true on success and false on allocation failure.
1060bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data);
1061
Robert Sloan921ef2c2017-10-17 09:02:20 -07001062// dtls_has_unprocessed_handshake_data behaves like
1063// |tls_has_unprocessed_handshake_data| for DTLS.
1064bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001065
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001066// tls_flush_pending_hs_data flushes any handshake plaintext data.
1067bool tls_flush_pending_hs_data(SSL *ssl);
1068
Robert Sloanb6d070c2017-07-24 08:40:01 -07001069struct DTLS_OUTGOING_MESSAGE {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001070 DTLS_OUTGOING_MESSAGE() {}
1071 DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
1072 DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
1073 ~DTLS_OUTGOING_MESSAGE() { Clear(); }
1074
1075 void Clear();
1076
1077 uint8_t *data = nullptr;
1078 uint32_t len = 0;
1079 uint16_t epoch = 0;
1080 bool is_ccs = false;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001081};
David Benjaminc895d6b2016-08-11 13:26:41 -04001082
Robert Sloana27a6a42017-09-05 08:39:28 -07001083// dtls_clear_outgoing_messages releases all buffered outgoing messages.
David Benjaminc895d6b2016-08-11 13:26:41 -04001084void dtls_clear_outgoing_messages(SSL *ssl);
1085
Adam Langley4139edb2016-01-13 15:00:54 -08001086
Robert Sloana27a6a42017-09-05 08:39:28 -07001087// Callbacks.
David Benjamin6e899c72016-06-09 18:02:18 -04001088
Robert Sloana27a6a42017-09-05 08:39:28 -07001089// ssl_do_info_callback calls |ssl|'s info callback, if set.
David Benjamin6e899c72016-06-09 18:02:18 -04001090void ssl_do_info_callback(const SSL *ssl, int type, int value);
1091
Robert Sloana27a6a42017-09-05 08:39:28 -07001092// ssl_do_msg_callback calls |ssl|'s message callback, if set.
Robert Sloanf63bd1f2019-04-16 09:26:20 -07001093void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
Robert Sloan921ef2c2017-10-17 09:02:20 -07001094 Span<const uint8_t> in);
David Benjamin6e899c72016-06-09 18:02:18 -04001095
1096
Robert Sloana27a6a42017-09-05 08:39:28 -07001097// Transport buffers.
Kenny Rootb8494592015-09-25 02:29:14 +00001098
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001099class SSLBuffer {
1100 public:
1101 SSLBuffer() {}
1102 ~SSLBuffer() { Clear(); }
1103
1104 SSLBuffer(const SSLBuffer &) = delete;
1105 SSLBuffer &operator=(const SSLBuffer &) = delete;
1106
1107 uint8_t *data() { return buf_ + offset_; }
1108 size_t size() const { return size_; }
1109 bool empty() const { return size_ == 0; }
1110 size_t cap() const { return cap_; }
1111
1112 Span<uint8_t> span() { return MakeSpan(data(), size()); }
1113
1114 Span<uint8_t> remaining() {
1115 return MakeSpan(data() + size(), cap() - size());
1116 }
1117
1118 // Clear releases the buffer.
1119 void Clear();
1120
1121 // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
1122 // that data written after |header_len| is aligned to a
1123 // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
1124 // on error.
1125 bool EnsureCap(size_t header_len, size_t new_cap);
1126
1127 // DidWrite extends the buffer by |len|. The caller must have filled in to
1128 // this point.
1129 void DidWrite(size_t len);
1130
1131 // Consume consumes |len| bytes from the front of the buffer. The memory
1132 // consumed will remain valid until the next call to |DiscardConsumed| or
1133 // |Clear|.
1134 void Consume(size_t len);
1135
1136 // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
1137 // is now empty, it releases memory used by it.
1138 void DiscardConsumed();
1139
1140 private:
1141 // buf_ is the memory allocated for this buffer.
1142 uint8_t *buf_ = nullptr;
1143 // offset_ is the offset into |buf_| which the buffer contents start at.
1144 uint16_t offset_ = 0;
1145 // size_ is the size of the buffer contents from |buf_| + |offset_|.
1146 uint16_t size_ = 0;
1147 // cap_ is how much memory beyond |buf_| + |offset_| is available.
1148 uint16_t cap_ = 0;
1149};
Kenny Rootb8494592015-09-25 02:29:14 +00001150
Robert Sloana27a6a42017-09-05 08:39:28 -07001151// ssl_read_buffer_extend_to extends the read buffer to the desired length. For
1152// TLS, it reads to the end of the buffer until the buffer is |len| bytes
1153// long. For DTLS, it reads a new packet and ignores |len|. It returns one on
1154// success, zero on EOF, and a negative number on error.
1155//
1156// It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
1157// non-empty.
Kenny Rootb8494592015-09-25 02:29:14 +00001158int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
1159
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001160// ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
1161// to a record-processing function. If |ret| is a success or if the caller
1162// should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
1163// 0.
Robert Sloan36272962017-10-23 10:28:39 -07001164int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
1165 size_t consumed, uint8_t alert);
1166
Robert Sloana27a6a42017-09-05 08:39:28 -07001167// ssl_write_buffer_flush flushes the write buffer to the transport. It returns
1168// one on success and <= 0 on error. For DTLS, whether or not the write
1169// succeeds, the write buffer will be cleared.
Kenny Rootb8494592015-09-25 02:29:14 +00001170int ssl_write_buffer_flush(SSL *ssl);
1171
Kenny Rootb8494592015-09-25 02:29:14 +00001172
Robert Sloana27a6a42017-09-05 08:39:28 -07001173// Certificate functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001174
Robert Sloan726e9d12018-09-11 11:45:04 -07001175// ssl_has_certificate returns whether a certificate and private key are
1176// configured.
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001177bool ssl_has_certificate(const SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001178
Robert Sloana27a6a42017-09-05 08:39:28 -07001179// ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
1180// by a TLS Certificate message. On success, it advances |cbs| and returns
1181// true. Otherwise, it returns false and sets |*out_alert| to an alert to send
1182// to the peer.
1183//
1184// If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
1185// the certificate chain and the leaf certificate's public key
1186// respectively. Otherwise, both will be set to nullptr.
1187//
1188// If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
1189// SHA-256 hash of the leaf to |out_leaf_sha256|.
Robert Sloanfe7cd212017-08-07 09:03:39 -07001190bool ssl_parse_cert_chain(uint8_t *out_alert,
1191 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
1192 UniquePtr<EVP_PKEY> *out_pubkey,
1193 uint8_t *out_leaf_sha256, CBS *cbs,
1194 CRYPTO_BUFFER_POOL *pool);
David Benjaminc895d6b2016-08-11 13:26:41 -04001195
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001196// ssl_add_cert_chain adds |hs->ssl|'s certificate chain to |cbb| in the format
1197// used by a TLS Certificate message. If there is no certificate chain, it emits
Robert Sloan726e9d12018-09-11 11:45:04 -07001198// an empty certificate list. It returns true on success and false on error.
1199bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjaminc895d6b2016-08-11 13:26:41 -04001200
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001201enum ssl_key_usage_t {
1202 key_usage_digital_signature = 0,
1203 key_usage_encipherment = 2,
1204};
1205
1206// ssl_cert_check_key_usage parses the DER-encoded, X.509 certificate in |in|
1207// and returns true if doesn't specify a key usage or, if it does, if it
1208// includes |bit|. Otherwise it pushes to the error queue and returns false.
1209bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit);
Steven Valdeze7531f02016-12-14 13:29:57 -05001210
Robert Sloana27a6a42017-09-05 08:39:28 -07001211// ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
1212// certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
1213// nullptr and pushes to the error queue.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001214UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
Steven Valdeze7531f02016-12-14 13:29:57 -05001215
Robert Sloana27a6a42017-09-05 08:39:28 -07001216// ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
1217// TLS CertificateRequest message. On success, it returns a newly-allocated
1218// |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
1219// sets |*out_alert| to an alert to send to the peer.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001220UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
1221 uint8_t *out_alert,
1222 CBS *cbs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001223
Robert Sloanb1b54b82017-11-06 13:50:02 -08001224// ssl_has_client_CAs returns there are configured CAs.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001225bool ssl_has_client_CAs(const SSL_CONFIG *cfg);
Robert Sloanb1b54b82017-11-06 13:50:02 -08001226
Robert Sloana27a6a42017-09-05 08:39:28 -07001227// ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
Robert Sloan726e9d12018-09-11 11:45:04 -07001228// used by a TLS CertificateRequest message. It returns true on success and
1229// false on error.
1230bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjaminc895d6b2016-08-11 13:26:41 -04001231
Robert Sloana27a6a42017-09-05 08:39:28 -07001232// ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
1233// a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
1234// an error on the error queue.
Robert Sloan726e9d12018-09-11 11:45:04 -07001235bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Steven Valdeze7531f02016-12-14 13:29:57 -05001236 const CRYPTO_BUFFER *leaf);
David Benjaminc895d6b2016-08-11 13:26:41 -04001237
Robert Sloana27a6a42017-09-05 08:39:28 -07001238// ssl_on_certificate_selected is called once the certificate has been selected.
1239// It finalizes the certificate and initializes |hs->local_pubkey|. It returns
Robert Sloan726e9d12018-09-11 11:45:04 -07001240// true on success and false on error.
1241bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
Robert Sloan572a4e22017-04-17 10:52:19 -07001242
David Benjaminc895d6b2016-08-11 13:26:41 -04001243
Robert Sloana27a6a42017-09-05 08:39:28 -07001244// TLS 1.3 key derivation.
David Benjaminc895d6b2016-08-11 13:26:41 -04001245
Robert Sloana27a6a42017-09-05 08:39:28 -07001246// tls13_init_key_schedule initializes the handshake hash and key derivation
Robert Sloanb1b54b82017-11-06 13:50:02 -08001247// state, and incorporates the PSK. The cipher suite and PRF hash must have been
Robert Sloan726e9d12018-09-11 11:45:04 -07001248// selected at this point. It returns true on success and false on error.
1249bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1250 size_t psk_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001251
Robert Sloana27a6a42017-09-05 08:39:28 -07001252// tls13_init_early_key_schedule initializes the handshake hash and key
Robert Sloanb1b54b82017-11-06 13:50:02 -08001253// derivation state from the resumption secret and incorporates the PSK to
1254// derive the early secrets. It returns one on success and zero on error.
Robert Sloan726e9d12018-09-11 11:45:04 -07001255bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1256 size_t psk_len);
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001257
Robert Sloana27a6a42017-09-05 08:39:28 -07001258// tls13_advance_key_schedule incorporates |in| into the key schedule with
Robert Sloan726e9d12018-09-11 11:45:04 -07001259// HKDF-Extract. It returns true on success and false on error.
1260bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
David Benjamin1b249672016-12-06 18:25:50 -05001261 size_t len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001262
Robert Sloana27a6a42017-09-05 08:39:28 -07001263// tls13_set_traffic_key sets the read or write traffic keys to
Robert Sloan726e9d12018-09-11 11:45:04 -07001264// |traffic_secret|. It returns true on success and false on error.
Robert Sloancbf5ea62018-11-05 11:56:34 -08001265bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level,
1266 enum evp_aead_direction_t direction,
Robert Sloan726e9d12018-09-11 11:45:04 -07001267 const uint8_t *traffic_secret,
1268 size_t traffic_secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001269
Robert Sloan726e9d12018-09-11 11:45:04 -07001270// tls13_derive_early_secrets derives the early traffic secret. It returns true
1271// on success and false on error.
1272bool tls13_derive_early_secrets(SSL_HANDSHAKE *hs);
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001273
Robert Sloana27a6a42017-09-05 08:39:28 -07001274// tls13_derive_handshake_secrets derives the handshake traffic secret. It
Robert Sloan726e9d12018-09-11 11:45:04 -07001275// returns true on success and false on error.
1276bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001277
Robert Sloana27a6a42017-09-05 08:39:28 -07001278// tls13_rotate_traffic_key derives the next read or write traffic secret. It
Robert Sloan726e9d12018-09-11 11:45:04 -07001279// returns true on success and false on error.
1280bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
David Benjaminc895d6b2016-08-11 13:26:41 -04001281
Robert Sloana27a6a42017-09-05 08:39:28 -07001282// tls13_derive_application_secrets derives the initial application data traffic
1283// and exporter secrets based on the handshake transcripts and |master_secret|.
Robert Sloan726e9d12018-09-11 11:45:04 -07001284// It returns true on success and false on error.
1285bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001286
Robert Sloana27a6a42017-09-05 08:39:28 -07001287// tls13_derive_resumption_secret derives the |resumption_secret|.
Robert Sloan726e9d12018-09-11 11:45:04 -07001288bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001289
Robert Sloana27a6a42017-09-05 08:39:28 -07001290// tls13_export_keying_material provides an exporter interface to use the
1291// |exporter_secret|.
Robert Sloan726e9d12018-09-11 11:45:04 -07001292bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
1293 Span<const uint8_t> secret,
1294 Span<const char> label,
1295 Span<const uint8_t> context);
David Benjaminc895d6b2016-08-11 13:26:41 -04001296
Robert Sloana27a6a42017-09-05 08:39:28 -07001297// tls13_finished_mac calculates the MAC of the handshake transcript to verify
1298// the integrity of the Finished message, and stores the result in |out| and
Robert Sloan726e9d12018-09-11 11:45:04 -07001299// length in |out_len|. |is_server| is true if this is for the Server Finished
1300// and false for the Client Finished.
1301bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
1302 bool is_server);
David Benjaminc895d6b2016-08-11 13:26:41 -04001303
Robert Sloanb1b54b82017-11-06 13:50:02 -08001304// tls13_derive_session_psk calculates the PSK for this session based on the
1305// resumption master secret and |nonce|. It returns true on success, and false
1306// on failure.
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001307bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
Robert Sloanb1b54b82017-11-06 13:50:02 -08001308
Robert Sloana27a6a42017-09-05 08:39:28 -07001309// tls13_write_psk_binder calculates the PSK binder value and replaces the last
Robert Sloan726e9d12018-09-11 11:45:04 -07001310// bytes of |msg| with the resulting value. It returns true on success, and
1311// false on failure.
1312bool tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001313
Robert Sloan726e9d12018-09-11 11:45:04 -07001314// tls13_verify_psk_binder verifies that the handshake transcript, truncated up
1315// to the binders has a valid signature using the value of |session|'s
1316// resumption secret. It returns true on success, and false on failure.
1317bool tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
1318 const SSLMessage &msg, CBS *binders);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001319
David Benjaminc895d6b2016-08-11 13:26:41 -04001320
Robert Sloana27a6a42017-09-05 08:39:28 -07001321// Handshake functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001322
1323enum ssl_hs_wait_t {
1324 ssl_hs_error,
1325 ssl_hs_ok,
Robert Sloana27a6a42017-09-05 08:39:28 -07001326 ssl_hs_read_server_hello,
David Benjaminc895d6b2016-08-11 13:26:41 -04001327 ssl_hs_read_message,
David Benjaminc895d6b2016-08-11 13:26:41 -04001328 ssl_hs_flush,
Robert Sloana27a6a42017-09-05 08:39:28 -07001329 ssl_hs_certificate_selection_pending,
Robert Sloan8542c082018-02-05 09:07:34 -08001330 ssl_hs_handoff,
Robert Sloandc2f6092018-04-10 10:22:33 -07001331 ssl_hs_handback,
David Benjaminc895d6b2016-08-11 13:26:41 -04001332 ssl_hs_x509_lookup,
Steven Valdez909b19f2016-11-21 15:35:44 -05001333 ssl_hs_channel_id_lookup,
David Benjaminc895d6b2016-08-11 13:26:41 -04001334 ssl_hs_private_key_operation,
Robert Sloana27a6a42017-09-05 08:39:28 -07001335 ssl_hs_pending_session,
Robert Sloan1c9db532017-03-13 08:03:59 -07001336 ssl_hs_pending_ticket,
Robert Sloana27a6a42017-09-05 08:39:28 -07001337 ssl_hs_early_return,
Robert Sloane56da3e2017-06-26 08:26:42 -07001338 ssl_hs_early_data_rejected,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001339 ssl_hs_read_end_of_early_data,
Robert Sloana12bf462017-07-17 07:08:26 -07001340 ssl_hs_read_change_cipher_spec,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001341 ssl_hs_certificate_verify,
David Benjaminc895d6b2016-08-11 13:26:41 -04001342};
1343
Robert Sloan309a31e2018-01-29 10:22:47 -08001344enum ssl_grease_index_t {
1345 ssl_grease_cipher = 0,
1346 ssl_grease_group,
1347 ssl_grease_extension1,
1348 ssl_grease_extension2,
1349 ssl_grease_version,
1350 ssl_grease_ticket_extension,
1351 ssl_grease_last_index = ssl_grease_ticket_extension,
1352};
1353
Robert Sloandc2f6092018-04-10 10:22:33 -07001354enum tls12_server_hs_state_t {
1355 state12_start_accept = 0,
1356 state12_read_client_hello,
1357 state12_select_certificate,
1358 state12_tls13,
1359 state12_select_parameters,
1360 state12_send_server_hello,
1361 state12_send_server_certificate,
1362 state12_send_server_key_exchange,
1363 state12_send_server_hello_done,
1364 state12_read_client_certificate,
1365 state12_verify_client_certificate,
1366 state12_read_client_key_exchange,
1367 state12_read_client_certificate_verify,
1368 state12_read_change_cipher_spec,
1369 state12_process_change_cipher_spec,
1370 state12_read_next_proto,
1371 state12_read_channel_id,
1372 state12_read_client_finished,
1373 state12_send_server_finished,
1374 state12_finish_server_handshake,
1375 state12_done,
1376};
1377
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001378// handback_t lists the points in the state machine where a handback can occur.
1379// These are the different points at which key material is no longer needed.
1380enum handback_t {
1381 handback_after_session_resumption,
1382 handback_after_ecdhe,
1383 handback_after_handshake,
1384};
1385
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001386
1387// Delegated credentials.
1388
1389// This structure stores a delegated credential (DC) as defined by
1390// draft-ietf-tls-subcerts-03.
1391struct DC {
1392 static constexpr bool kAllowUniquePtr = true;
1393 ~DC();
1394
1395 // Dup returns a copy of this DC and takes references to |raw| and |pkey|.
1396 UniquePtr<DC> Dup();
1397
1398 // Parse parses the delegated credential stored in |in|. If successful it
1399 // returns the parsed structure, otherwise it returns |nullptr| and sets
1400 // |*out_alert|.
1401 static UniquePtr<DC> Parse(CRYPTO_BUFFER *in, uint8_t *out_alert);
1402
1403 // raw is the delegated credential encoded as specified in draft-ietf-tls-
Robert Sloan4726ed32019-04-08 12:43:32 -07001404 // subcerts-03.
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001405 UniquePtr<CRYPTO_BUFFER> raw;
1406
1407 // expected_cert_verify_algorithm is the signature scheme of the DC public
1408 // key.
1409 uint16_t expected_cert_verify_algorithm = 0;
1410
1411 // pkey is the public key parsed from |public_key|.
1412 UniquePtr<EVP_PKEY> pkey;
1413
1414 private:
1415 friend DC* New<DC>();
1416 DC();
1417};
1418
1419// ssl_signing_with_dc returns true if the peer has indicated support for
1420// delegated credentials and this host has sent a delegated credential in
1421// response. If this is true then we've committed to using the DC in the
1422// handshake.
1423bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs);
1424
1425
Robert Sloanb6d070c2017-07-24 08:40:01 -07001426struct SSL_HANDSHAKE {
1427 explicit SSL_HANDSHAKE(SSL *ssl);
1428 ~SSL_HANDSHAKE();
Robert Sloanfe7cd212017-08-07 09:03:39 -07001429 static constexpr bool kAllowUniquePtr = true;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001430
Robert Sloana27a6a42017-09-05 08:39:28 -07001431 // ssl is a non-owning pointer to the parent |SSL| object.
David Benjamin1b249672016-12-06 18:25:50 -05001432 SSL *ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001433
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001434 // config is a non-owning pointer to the handshake configuration.
1435 SSL_CONFIG *config;
1436
Robert Sloana27a6a42017-09-05 08:39:28 -07001437 // wait contains the operation the handshake is currently blocking on or
1438 // |ssl_hs_ok| if none.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001439 enum ssl_hs_wait_t wait = ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -04001440
Robert Sloana27a6a42017-09-05 08:39:28 -07001441 // state is the internal state for the TLS 1.2 and below handshake. Its
1442 // values depend on |do_handshake| but the starting state is always zero.
1443 int state = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04001444
Robert Sloana27a6a42017-09-05 08:39:28 -07001445 // tls13_state is the internal state for the TLS 1.3 handshake. Its values
1446 // depend on |do_handshake| but the starting state is always zero.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001447 int tls13_state = 0;
Steven Valdeze7531f02016-12-14 13:29:57 -05001448
Robert Sloana27a6a42017-09-05 08:39:28 -07001449 // min_version is the minimum accepted protocol version, taking account both
1450 // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001451 uint16_t min_version = 0;
Robert Sloane56da3e2017-06-26 08:26:42 -07001452
Robert Sloana27a6a42017-09-05 08:39:28 -07001453 // max_version is the maximum accepted protocol version, taking account both
1454 // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001455 uint16_t max_version = 0;
Robert Sloane56da3e2017-06-26 08:26:42 -07001456
Robert Sloanb6d070c2017-07-24 08:40:01 -07001457 size_t hash_len = 0;
Pete Bentley0c61efe2019-08-13 09:32:23 +01001458 uint8_t secret[SSL_MAX_MD_SIZE] = {0};
1459 uint8_t early_traffic_secret[SSL_MAX_MD_SIZE] = {0};
1460 uint8_t client_handshake_secret[SSL_MAX_MD_SIZE] = {0};
1461 uint8_t server_handshake_secret[SSL_MAX_MD_SIZE] = {0};
1462 uint8_t client_traffic_secret_0[SSL_MAX_MD_SIZE] = {0};
1463 uint8_t server_traffic_secret_0[SSL_MAX_MD_SIZE] = {0};
1464 uint8_t expected_client_finished[SSL_MAX_MD_SIZE] = {0};
David Benjaminc895d6b2016-08-11 13:26:41 -04001465
David Benjamin95add822016-10-19 01:09:12 -04001466 union {
Robert Sloana27a6a42017-09-05 08:39:28 -07001467 // sent is a bitset where the bits correspond to elements of kExtensions
1468 // in t1_lib.c. Each bit is set if that extension was sent in a
1469 // ClientHello. It's not used by servers.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001470 uint32_t sent = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07001471 // received is a bitset, like |sent|, but is used by servers to record
1472 // which extensions were received from a client.
David Benjamin95add822016-10-19 01:09:12 -04001473 uint32_t received;
1474 } extensions;
1475
Robert Sloana27a6a42017-09-05 08:39:28 -07001476 // retry_group is the group ID selected by the server in HelloRetryRequest in
1477 // TLS 1.3.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001478 uint16_t retry_group = 0;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001479
Robert Sloan921ef2c2017-10-17 09:02:20 -07001480 // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
1481 UniquePtr<ERR_SAVE_STATE> error;
1482
Robert Sloan11c28bd2018-12-17 12:09:20 -08001483 // key_shares are the current key exchange instances. The second is only used
1484 // as a client if we believe that we should offer two key shares in a
1485 // ClientHello.
1486 UniquePtr<SSLKeyShare> key_shares[2];
David Benjamin1b249672016-12-06 18:25:50 -05001487
Robert Sloana27a6a42017-09-05 08:39:28 -07001488 // transcript is the current handshake transcript.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001489 SSLTranscript transcript;
Robert Sloan5d625782017-02-13 09:55:39 -08001490
Robert Sloana27a6a42017-09-05 08:39:28 -07001491 // cookie is the value of the cookie received from the server, if any.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001492 Array<uint8_t> cookie;
David Benjamin95add822016-10-19 01:09:12 -04001493
Robert Sloana27a6a42017-09-05 08:39:28 -07001494 // key_share_bytes is the value of the previously sent KeyShare extension by
1495 // the client in TLS 1.3.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001496 Array<uint8_t> key_share_bytes;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001497
Robert Sloana27a6a42017-09-05 08:39:28 -07001498 // ecdh_public_key, for servers, is the key share to be sent to the client in
1499 // TLS 1.3.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001500 Array<uint8_t> ecdh_public_key;
David Benjaminc895d6b2016-08-11 13:26:41 -04001501
Robert Sloana27a6a42017-09-05 08:39:28 -07001502 // peer_sigalgs are the signature algorithms that the peer supports. These are
1503 // taken from the contents of the signature algorithms extension for a server
1504 // or from the CertificateRequest for a client.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001505 Array<uint16_t> peer_sigalgs;
David Benjaminc895d6b2016-08-11 13:26:41 -04001506
Robert Sloana27a6a42017-09-05 08:39:28 -07001507 // peer_supported_group_list contains the supported group IDs advertised by
1508 // the peer. This is only set on the server's end. The server does not
1509 // advertise this extension to the client.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001510 Array<uint16_t> peer_supported_group_list;
David Benjamin95add822016-10-19 01:09:12 -04001511
Robert Sloana27a6a42017-09-05 08:39:28 -07001512 // peer_key is the peer's ECDH key for a TLS 1.2 client.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001513 Array<uint8_t> peer_key;
David Benjamin95add822016-10-19 01:09:12 -04001514
Robert Sloan978112c2018-01-22 12:53:01 -08001515 // negotiated_token_binding_version is used by a server to store the
1516 // on-the-wire encoding of the Token Binding protocol version to advertise in
1517 // the ServerHello/EncryptedExtensions if the Token Binding extension is to be
1518 // sent.
1519 uint16_t negotiated_token_binding_version;
1520
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001521 // cert_compression_alg_id, for a server, contains the negotiated certificate
1522 // compression algorithm for this client. It is only valid if
1523 // |cert_compression_negotiated| is true.
1524 uint16_t cert_compression_alg_id;
1525
Robert Sloana27a6a42017-09-05 08:39:28 -07001526 // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
1527 // parameters. It has client and server randoms prepended for signing
1528 // convenience.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001529 Array<uint8_t> server_params;
David Benjamin95add822016-10-19 01:09:12 -04001530
Robert Sloana27a6a42017-09-05 08:39:28 -07001531 // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
1532 // server when using a TLS 1.2 PSK key exchange.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001533 UniquePtr<char> peer_psk_identity_hint;
David Benjamin1b249672016-12-06 18:25:50 -05001534
Robert Sloana27a6a42017-09-05 08:39:28 -07001535 // ca_names, on the client, contains the list of CAs received in a
1536 // CertificateRequest message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001537 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
Robert Sloan7d422bc2017-03-06 10:04:29 -08001538
Robert Sloanae1abf92017-10-05 12:50:08 -07001539 // cached_x509_ca_names contains a cache of parsed versions of the elements of
1540 // |ca_names|. This pointer is left non-owning so only
1541 // |ssl_crypto_x509_method| needs to link against crypto/x509.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001542 STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
David Benjamin1b249672016-12-06 18:25:50 -05001543
Robert Sloana27a6a42017-09-05 08:39:28 -07001544 // certificate_types, on the client, contains the set of certificate types
1545 // received in a CertificateRequest message.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001546 Array<uint8_t> certificate_types;
David Benjamin1b249672016-12-06 18:25:50 -05001547
Robert Sloana27a6a42017-09-05 08:39:28 -07001548 // local_pubkey is the public key we are authenticating as.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001549 UniquePtr<EVP_PKEY> local_pubkey;
Robert Sloan572a4e22017-04-17 10:52:19 -07001550
Robert Sloana27a6a42017-09-05 08:39:28 -07001551 // peer_pubkey is the public key parsed from the peer's leaf certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001552 UniquePtr<EVP_PKEY> peer_pubkey;
Steven Valdeze7531f02016-12-14 13:29:57 -05001553
Robert Sloana27a6a42017-09-05 08:39:28 -07001554 // new_session is the new mutable session being established by the current
1555 // handshake. It should not be cached.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001556 UniquePtr<SSL_SESSION> new_session;
Robert Sloana94fe052017-02-21 08:49:28 -08001557
Robert Sloana27a6a42017-09-05 08:39:28 -07001558 // early_session is the session corresponding to the current 0-RTT state on
1559 // the client if |in_early_data| is true.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001560 UniquePtr<SSL_SESSION> early_session;
Robert Sloane56da3e2017-06-26 08:26:42 -07001561
Robert Sloana27a6a42017-09-05 08:39:28 -07001562 // new_cipher is the cipher being negotiated in this handshake.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001563 const SSL_CIPHER *new_cipher = nullptr;
Robert Sloana94fe052017-02-21 08:49:28 -08001564
Robert Sloana27a6a42017-09-05 08:39:28 -07001565 // key_block is the record-layer key block for TLS 1.2 and earlier.
Robert Sloanae1abf92017-10-05 12:50:08 -07001566 Array<uint8_t> key_block;
David Benjamin1b249672016-12-06 18:25:50 -05001567
Robert Sloana27a6a42017-09-05 08:39:28 -07001568 // scts_requested is true if the SCT extension is in the ClientHello.
Robert Sloand9e572d2018-08-27 12:27:00 -07001569 bool scts_requested : 1;
David Benjamin1b249672016-12-06 18:25:50 -05001570
Robert Sloana27a6a42017-09-05 08:39:28 -07001571 // needs_psk_binder is true if the ClientHello has a placeholder PSK binder to
1572 // be filled in.
Robert Sloand9e572d2018-08-27 12:27:00 -07001573 bool needs_psk_binder : 1;
David Benjamin1b249672016-12-06 18:25:50 -05001574
Robert Sloand9e572d2018-08-27 12:27:00 -07001575 bool received_hello_retry_request : 1;
1576 bool sent_hello_retry_request : 1;
David Benjamin1b249672016-12-06 18:25:50 -05001577
Robert Sloana27a6a42017-09-05 08:39:28 -07001578 // handshake_finalized is true once the handshake has completed, at which
1579 // point accessors should use the established state.
Robert Sloand9e572d2018-08-27 12:27:00 -07001580 bool handshake_finalized : 1;
David Benjamin1b249672016-12-06 18:25:50 -05001581
Robert Sloana27a6a42017-09-05 08:39:28 -07001582 // accept_psk_mode stores whether the client's PSK mode is compatible with our
1583 // preferences.
Robert Sloand9e572d2018-08-27 12:27:00 -07001584 bool accept_psk_mode : 1;
David Benjamin95add822016-10-19 01:09:12 -04001585
Robert Sloana27a6a42017-09-05 08:39:28 -07001586 // cert_request is true if a client certificate was requested.
Robert Sloand9e572d2018-08-27 12:27:00 -07001587 bool cert_request : 1;
David Benjamin95add822016-10-19 01:09:12 -04001588
Robert Sloana27a6a42017-09-05 08:39:28 -07001589 // certificate_status_expected is true if OCSP stapling was negotiated and the
1590 // server is expected to send a CertificateStatus message. (This is used on
1591 // both the client and server sides.)
Robert Sloand9e572d2018-08-27 12:27:00 -07001592 bool certificate_status_expected : 1;
David Benjamin95add822016-10-19 01:09:12 -04001593
Robert Sloana27a6a42017-09-05 08:39:28 -07001594 // ocsp_stapling_requested is true if a client requested OCSP stapling.
Robert Sloand9e572d2018-08-27 12:27:00 -07001595 bool ocsp_stapling_requested : 1;
David Benjamin95add822016-10-19 01:09:12 -04001596
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001597 // delegated_credential_requested is true if the peer indicated support for
1598 // the delegated credential extension.
1599 bool delegated_credential_requested : 1;
1600
Robert Sloana27a6a42017-09-05 08:39:28 -07001601 // should_ack_sni is used by a server and indicates that the SNI extension
1602 // should be echoed in the ServerHello.
Robert Sloand9e572d2018-08-27 12:27:00 -07001603 bool should_ack_sni : 1;
David Benjamin95add822016-10-19 01:09:12 -04001604
Robert Sloana27a6a42017-09-05 08:39:28 -07001605 // in_false_start is true if there is a pending client handshake in False
1606 // Start. The client may write data at this point.
Robert Sloand9e572d2018-08-27 12:27:00 -07001607 bool in_false_start : 1;
Robert Sloane56da3e2017-06-26 08:26:42 -07001608
Robert Sloana27a6a42017-09-05 08:39:28 -07001609 // in_early_data is true if there is a pending handshake that has progressed
1610 // enough to send and receive early data.
Robert Sloand9e572d2018-08-27 12:27:00 -07001611 bool in_early_data : 1;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001612
Robert Sloana27a6a42017-09-05 08:39:28 -07001613 // early_data_offered is true if the client sent the early_data extension.
Robert Sloand9e572d2018-08-27 12:27:00 -07001614 bool early_data_offered : 1;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001615
Robert Sloana27a6a42017-09-05 08:39:28 -07001616 // can_early_read is true if application data may be read at this point in the
1617 // handshake.
Robert Sloand9e572d2018-08-27 12:27:00 -07001618 bool can_early_read : 1;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001619
Robert Sloana27a6a42017-09-05 08:39:28 -07001620 // can_early_write is true if application data may be written at this point in
1621 // the handshake.
Robert Sloand9e572d2018-08-27 12:27:00 -07001622 bool can_early_write : 1;
David Benjamin95add822016-10-19 01:09:12 -04001623
Robert Sloana27a6a42017-09-05 08:39:28 -07001624 // next_proto_neg_seen is one of NPN was negotiated.
Robert Sloand9e572d2018-08-27 12:27:00 -07001625 bool next_proto_neg_seen : 1;
Steven Valdez909b19f2016-11-21 15:35:44 -05001626
Robert Sloana27a6a42017-09-05 08:39:28 -07001627 // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
1628 // or received.
Robert Sloand9e572d2018-08-27 12:27:00 -07001629 bool ticket_expected : 1;
Robert Sloana94fe052017-02-21 08:49:28 -08001630
Robert Sloana27a6a42017-09-05 08:39:28 -07001631 // extended_master_secret is true if the extended master secret extension is
1632 // negotiated in this handshake.
Robert Sloand9e572d2018-08-27 12:27:00 -07001633 bool extended_master_secret : 1;
Robert Sloane56da3e2017-06-26 08:26:42 -07001634
Robert Sloana27a6a42017-09-05 08:39:28 -07001635 // pending_private_key_op is true if there is a pending private key operation
1636 // in progress.
Robert Sloand9e572d2018-08-27 12:27:00 -07001637 bool pending_private_key_op : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -07001638
Robert Sloan309a31e2018-01-29 10:22:47 -08001639 // grease_seeded is true if |grease_seed| has been initialized.
Robert Sloand9e572d2018-08-27 12:27:00 -07001640 bool grease_seeded : 1;
Robert Sloan309a31e2018-01-29 10:22:47 -08001641
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001642 // handback indicates that a server should pause the handshake after
1643 // finishing operations that require private key material, in such a way that
1644 // |SSL_get_error| returns |SSL_HANDBACK|. It is set by |SSL_apply_handoff|.
Robert Sloand9e572d2018-08-27 12:27:00 -07001645 bool handback : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001646
1647 // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid.
Robert Sloand9e572d2018-08-27 12:27:00 -07001648 bool cert_compression_negotiated : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001649
Robert Sloanc9abfe42018-11-26 12:19:07 -08001650 // apply_jdk11_workaround is true if the peer is probably a JDK 11 client
1651 // which implemented TLS 1.3 incorrectly.
1652 bool apply_jdk11_workaround : 1;
1653
Robert Sloana27a6a42017-09-05 08:39:28 -07001654 // client_version is the value sent or received in the ClientHello version.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001655 uint16_t client_version = 0;
Robert Sloan9254e682017-04-24 09:42:06 -07001656
Robert Sloana27a6a42017-09-05 08:39:28 -07001657 // early_data_read is the amount of early data that has been read by the
1658 // record layer.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001659 uint16_t early_data_read = 0;
Robert Sloane56da3e2017-06-26 08:26:42 -07001660
Robert Sloana27a6a42017-09-05 08:39:28 -07001661 // early_data_written is the amount of early data that has been written by the
1662 // record layer.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001663 uint16_t early_data_written = 0;
Robert Sloana450c922018-01-08 10:35:32 -08001664
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001665 // session_id is the session ID in the ClientHello.
Robert Sloana450c922018-01-08 10:35:32 -08001666 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
1667 uint8_t session_id_len = 0;
Robert Sloan309a31e2018-01-29 10:22:47 -08001668
1669 // grease_seed is the entropy for GREASE values. It is valid if
1670 // |grease_seeded| is true.
1671 uint8_t grease_seed[ssl_grease_last_index + 1] = {0};
Robert Sloanb6d070c2017-07-24 08:40:01 -07001672};
David Benjamin95add822016-10-19 01:09:12 -04001673
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001674UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001675
Robert Sloana27a6a42017-09-05 08:39:28 -07001676// ssl_check_message_type checks if |msg| has type |type|. If so it returns
1677// one. Otherwise, it sends an alert and returns zero.
Robert Sloan36272962017-10-23 10:28:39 -07001678bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
Robert Sloan4d1ac502017-02-06 08:36:14 -08001679
Robert Sloana27a6a42017-09-05 08:39:28 -07001680// ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
1681// on error. It sets |out_early_return| to one if we've completed the handshake
1682// early.
1683int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
David Benjaminc895d6b2016-08-11 13:26:41 -04001684
Robert Sloana27a6a42017-09-05 08:39:28 -07001685// The following are implementations of |do_handshake| for the client and
1686// server.
1687enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
1688enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
David Benjamin1b249672016-12-06 18:25:50 -05001689enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
1690enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001691
Robert Sloana27a6a42017-09-05 08:39:28 -07001692// The following functions return human-readable representations of the TLS
1693// handshake states for debugging.
1694const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
1695const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
Robert Sloan8f860b12017-08-28 07:37:06 -07001696const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
1697const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
1698
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001699// tls13_add_key_update queues a KeyUpdate message on |ssl|. The
1700// |update_requested| argument must be one of |SSL_KEY_UPDATE_REQUESTED| or
1701// |SSL_KEY_UPDATE_NOT_REQUESTED|.
1702bool tls13_add_key_update(SSL *ssl, int update_requested);
1703
Robert Sloan726e9d12018-09-11 11:45:04 -07001704// tls13_post_handshake processes a post-handshake message. It returns true on
1705// success and false on failure.
1706bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
David Benjaminc895d6b2016-08-11 13:26:41 -04001707
Robert Sloan726e9d12018-09-11 11:45:04 -07001708bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1709 bool allow_anonymous);
1710bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001711
Robert Sloana27a6a42017-09-05 08:39:28 -07001712// tls13_process_finished processes |msg| as a Finished message from the
Robert Sloan726e9d12018-09-11 11:45:04 -07001713// peer. If |use_saved_value| is true, the verify_data is compared against
Robert Sloana27a6a42017-09-05 08:39:28 -07001714// |hs->expected_client_finished| rather than computed fresh.
Robert Sloan726e9d12018-09-11 11:45:04 -07001715bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1716 bool use_saved_value);
David Benjaminc895d6b2016-08-11 13:26:41 -04001717
Robert Sloan726e9d12018-09-11 11:45:04 -07001718bool tls13_add_certificate(SSL_HANDSHAKE *hs);
Robert Sloane56da3e2017-06-26 08:26:42 -07001719
Robert Sloana27a6a42017-09-05 08:39:28 -07001720// tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
1721// handshake. If it returns |ssl_private_key_retry|, it should be called again
1722// to retry when the signing operation is completed.
Robert Sloane56da3e2017-06-26 08:26:42 -07001723enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
1724
Robert Sloan726e9d12018-09-11 11:45:04 -07001725bool tls13_add_finished(SSL_HANDSHAKE *hs);
1726bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
David Benjaminc895d6b2016-08-11 13:26:41 -04001727
Robert Sloan921ef2c2017-10-17 09:02:20 -07001728bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
1729 Array<uint8_t> *out_secret,
1730 uint8_t *out_alert, CBS *contents);
1731bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
1732 Array<uint8_t> *out_secret,
1733 uint8_t *out_alert, CBS *contents);
1734bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
David Benjaminc895d6b2016-08-11 13:26:41 -04001735
Robert Sloan921ef2c2017-10-17 09:02:20 -07001736bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1737 uint8_t *out_alert,
1738 CBS *contents);
1739bool ssl_ext_pre_shared_key_parse_clienthello(
Robert Sloan1c9db532017-03-13 08:03:59 -07001740 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
1741 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001742bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001743
Robert Sloana27a6a42017-09-05 08:39:28 -07001744// ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001745// returns whether it's valid.
1746bool ssl_is_sct_list_valid(const CBS *contents);
Steven Valdez909b19f2016-11-21 15:35:44 -05001747
Robert Sloan726e9d12018-09-11 11:45:04 -07001748bool ssl_write_client_hello(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001749
Steven Valdez909b19f2016-11-21 15:35:44 -05001750enum ssl_cert_verify_context_t {
1751 ssl_cert_verify_server,
1752 ssl_cert_verify_client,
1753 ssl_cert_verify_channel_id,
1754};
1755
Robert Sloana27a6a42017-09-05 08:39:28 -07001756// tls13_get_cert_verify_signature_input generates the message to be signed for
1757// TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
Robert Sloan921ef2c2017-10-17 09:02:20 -07001758// type of signature. It sets |*out| to a newly allocated buffer containing the
1759// result. This function returns true on success and false on failure.
1760bool tls13_get_cert_verify_signature_input(
1761 SSL_HANDSHAKE *hs, Array<uint8_t> *out,
Steven Valdez909b19f2016-11-21 15:35:44 -05001762 enum ssl_cert_verify_context_t cert_verify_context);
1763
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001764// ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001765// selection for |hs->ssl|'s client preferences.
1766bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
1767 Span<const uint8_t> protocol);
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001768
Robert Sloana27a6a42017-09-05 08:39:28 -07001769// ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
Robert Sloan921ef2c2017-10-17 09:02:20 -07001770// true on successful negotiation or if nothing was negotiated. It returns false
Robert Sloana27a6a42017-09-05 08:39:28 -07001771// and sets |*out_alert| to an alert on error.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001772bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1773 const SSL_CLIENT_HELLO *client_hello);
Steven Valdez909b19f2016-11-21 15:35:44 -05001774
Robert Sloanb6d070c2017-07-24 08:40:01 -07001775struct SSL_EXTENSION_TYPE {
Steven Valdez909b19f2016-11-21 15:35:44 -05001776 uint16_t type;
Robert Sloana27a6a42017-09-05 08:39:28 -07001777 bool *out_present;
Steven Valdez909b19f2016-11-21 15:35:44 -05001778 CBS *out_data;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001779};
Steven Valdez909b19f2016-11-21 15:35:44 -05001780
Robert Sloana27a6a42017-09-05 08:39:28 -07001781// ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
1782// it. It writes the parsed extensions to pointers denoted by |ext_types|. On
1783// success, it fills in the |out_present| and |out_data| fields and returns one.
1784// Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
1785// extensions are rejected unless |ignore_unknown| is 1.
Steven Valdez909b19f2016-11-21 15:35:44 -05001786int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
1787 const SSL_EXTENSION_TYPE *ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -08001788 size_t num_ext_types, int ignore_unknown);
Steven Valdez909b19f2016-11-21 15:35:44 -05001789
Robert Sloana27a6a42017-09-05 08:39:28 -07001790// ssl_verify_peer_cert verifies the peer certificate for |hs|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001791enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
Robert Sloand9e572d2018-08-27 12:27:00 -07001792// ssl_reverify_peer_cert verifies the peer certificate for |hs| when resuming a
1793// session.
1794enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs);
Robert Sloanb6d070c2017-07-24 08:40:01 -07001795
Robert Sloan921ef2c2017-10-17 09:02:20 -07001796enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
1797bool ssl_send_finished(SSL_HANDSHAKE *hs);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001798bool ssl_output_cert_chain(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001799
Robert Sloana27a6a42017-09-05 08:39:28 -07001800// SSLKEYLOGFILE functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001801
Robert Sloana27a6a42017-09-05 08:39:28 -07001802// ssl_log_secret logs |secret| with label |label|, if logging is enabled for
1803// |ssl|. It returns one on success and zero on failure.
David Benjaminc895d6b2016-08-11 13:26:41 -04001804int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
1805 size_t secret_len);
1806
1807
Robert Sloana27a6a42017-09-05 08:39:28 -07001808// ClientHello functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001809
Robert Sloanf63bd1f2019-04-16 09:26:20 -07001810bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001811 const SSLMessage &msg);
David Benjaminc895d6b2016-08-11 13:26:41 -04001812
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001813bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
1814 CBS *out, uint16_t extension_type);
David Benjaminc895d6b2016-08-11 13:26:41 -04001815
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001816bool ssl_client_cipher_list_contains_cipher(
1817 const SSL_CLIENT_HELLO *client_hello, uint16_t id);
David Benjaminc895d6b2016-08-11 13:26:41 -04001818
1819
Robert Sloana27a6a42017-09-05 08:39:28 -07001820// GREASE.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001821
Robert Sloan309a31e2018-01-29 10:22:47 -08001822// ssl_get_grease_value returns a GREASE value for |hs|. For a given
Robert Sloana27a6a42017-09-05 08:39:28 -07001823// connection, the values for each index will be deterministic. This allows the
1824// same ClientHello be sent twice for a HelloRetryRequest or the same group be
1825// advertised in both supported_groups and key_shares.
Robert Sloan309a31e2018-01-29 10:22:47 -08001826uint16_t ssl_get_grease_value(SSL_HANDSHAKE *hs, enum ssl_grease_index_t index);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001827
1828
Robert Sloana27a6a42017-09-05 08:39:28 -07001829// Signature algorithms.
David Benjamin95add822016-10-19 01:09:12 -04001830
Robert Sloana27a6a42017-09-05 08:39:28 -07001831// tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
Robert Sloan309a31e2018-01-29 10:22:47 -08001832// algorithms and saves them on |hs|. It returns true on success and false on
Robert Sloana27a6a42017-09-05 08:39:28 -07001833// error.
Robert Sloan309a31e2018-01-29 10:22:47 -08001834bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
David Benjamin95add822016-10-19 01:09:12 -04001835
Robert Sloana27a6a42017-09-05 08:39:28 -07001836// tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
Robert Sloan309a31e2018-01-29 10:22:47 -08001837// that should be used with |pkey| in TLS 1.1 and earlier. It returns true on
1838// success and false if |pkey| may not be used at those versions.
1839bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
Robert Sloan2424d842017-05-01 07:46:28 -07001840
Robert Sloana27a6a42017-09-05 08:39:28 -07001841// tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
1842// with |hs|'s private key based on the peer's preferences and the algorithms
Robert Sloan309a31e2018-01-29 10:22:47 -08001843// supported. It returns true on success and false on error.
1844bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
David Benjamin95add822016-10-19 01:09:12 -04001845
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001846// tls1_get_peer_verify_algorithms returns the signature schemes for which the
1847// peer indicated support.
1848//
1849// NOTE: The related function |SSL_get0_peer_verify_algorithms| only has
1850// well-defined behavior during the callbacks set by |SSL_CTX_set_cert_cb| and
1851// |SSL_CTX_set_client_cert_cb|, or when the handshake is paused because of
1852// them.
1853Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs);
1854
Robert Sloana27a6a42017-09-05 08:39:28 -07001855// tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001856// peer signature to |out|. It returns true on success and false on error. If
1857// |for_certs| is true, the potentially more restrictive list of algorithms for
1858// certificates is used. Otherwise, the online signature one is used.
1859bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out, bool for_certs);
David Benjamin95add822016-10-19 01:09:12 -04001860
Robert Sloana27a6a42017-09-05 08:39:28 -07001861// tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
Robert Sloan309a31e2018-01-29 10:22:47 -08001862// signature. It returns true on success and false on error, setting
1863// |*out_alert| to an alert to send.
1864bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
1865 uint16_t sigalg);
David Benjamin95add822016-10-19 01:09:12 -04001866
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001867// tls12_has_different_verify_sigalgs_for_certs returns whether |ssl| has a
1868// different, more restrictive, list of signature algorithms acceptable for the
1869// certificate than the online signature.
1870bool tls12_has_different_verify_sigalgs_for_certs(const SSL *ssl);
1871
David Benjamin95add822016-10-19 01:09:12 -04001872
Robert Sloana27a6a42017-09-05 08:39:28 -07001873// Underdocumented functions.
1874//
1875// Functions below here haven't been touched up and may be underdocumented.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001876
Adam Langleyd9e397b2015-01-22 14:27:53 -08001877#define TLSEXT_CHANNEL_ID_SIZE 128
1878
Robert Sloana27a6a42017-09-05 08:39:28 -07001879// From RFC4492, used in encoding the curve type in ECParameters
Adam Langleyd9e397b2015-01-22 14:27:53 -08001880#define NAMED_CURVE_TYPE 3
1881
Robert Sloan921ef2c2017-10-17 09:02:20 -07001882struct CERT {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001883 static constexpr bool kAllowUniquePtr = true;
1884
1885 explicit CERT(const SSL_X509_METHOD *x509_method);
1886 ~CERT();
1887
1888 UniquePtr<EVP_PKEY> privatekey;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001889
Robert Sloana27a6a42017-09-05 08:39:28 -07001890 // chain contains the certificate chain, with the leaf at the beginning. The
1891 // first element of |chain| may be NULL to indicate that the leaf certificate
1892 // has not yet been set.
1893 // If |chain| != NULL -> len(chain) >= 1
1894 // If |chain[0]| == NULL -> len(chain) >= 2.
1895 // |chain[1..]| != NULL
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001896 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001897
Robert Sloana27a6a42017-09-05 08:39:28 -07001898 // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
1899 // a cache in order to implement “get0” functions that return a non-owning
1900 // pointer to the certificate chain.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001901 STACK_OF(X509) *x509_chain = nullptr;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001902
Robert Sloana27a6a42017-09-05 08:39:28 -07001903 // x509_leaf may contain a parsed copy of the first element of |chain|. This
1904 // is only used as a cache in order to implement “get0” functions that return
1905 // a non-owning pointer to the certificate chain.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001906 X509 *x509_leaf = nullptr;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001907
Robert Sloana27a6a42017-09-05 08:39:28 -07001908 // x509_stash contains the last |X509| object append to the chain. This is a
1909 // workaround for some third-party code that continue to use an |X509| object
1910 // even after passing ownership with an “add0” function.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001911 X509 *x509_stash = nullptr;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001912
Robert Sloana27a6a42017-09-05 08:39:28 -07001913 // key_method, if non-NULL, is a set of callbacks to call for private key
1914 // operations.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001915 const SSL_PRIVATE_KEY_METHOD *key_method = nullptr;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001916
Robert Sloana27a6a42017-09-05 08:39:28 -07001917 // x509_method contains pointers to functions that might deal with |X509|
1918 // compatibility, or might be a no-op, depending on the application.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001919 const SSL_X509_METHOD *x509_method = nullptr;
Robert Sloan5d625782017-02-13 09:55:39 -08001920
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001921 // sigalgs, if non-empty, is the set of signature algorithms supported by
Robert Sloana27a6a42017-09-05 08:39:28 -07001922 // |privatekey| in decreasing order of preference.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001923 Array<uint16_t> sigalgs;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001924
Robert Sloana27a6a42017-09-05 08:39:28 -07001925 // Certificate setup callback: if set is called whenever a
1926 // certificate may be required (client or server). the callback
1927 // can then examine any appropriate parameters and setup any
1928 // certificates required. This allows advanced applications
1929 // to select certificates on the fly: for example based on
1930 // supported signature algorithms or curves.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001931 int (*cert_cb)(SSL *ssl, void *arg) = nullptr;
1932 void *cert_cb_arg = nullptr;
David Benjamin4969cc92016-04-22 15:02:23 -04001933
Robert Sloana27a6a42017-09-05 08:39:28 -07001934 // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
1935 // store is used instead.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001936 X509_STORE *verify_store = nullptr;
Robert Sloana94fe052017-02-21 08:49:28 -08001937
Robert Sloana27a6a42017-09-05 08:39:28 -07001938 // Signed certificate timestamp list to be sent to the client, if requested
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001939 UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
Robert Sloana94fe052017-02-21 08:49:28 -08001940
Robert Sloana27a6a42017-09-05 08:39:28 -07001941 // OCSP response to be sent to the client, if requested.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001942 UniquePtr<CRYPTO_BUFFER> ocsp_response;
Robert Sloana94fe052017-02-21 08:49:28 -08001943
Robert Sloana27a6a42017-09-05 08:39:28 -07001944 // sid_ctx partitions the session space within a shared session cache or
1945 // ticket key. Only sessions with a matching value will be accepted.
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001946 uint8_t sid_ctx_length = 0;
1947 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001948
1949 // Delegated credentials.
1950
1951 // dc is the delegated credential to send to the peer (if requested).
1952 UniquePtr<DC> dc = nullptr;
1953
1954 // dc_privatekey is used instead of |privatekey| or |key_method| to
1955 // authenticate the host if a delegated credential is used in the handshake.
1956 UniquePtr<EVP_PKEY> dc_privatekey = nullptr;
1957
1958 // dc_key_method, if not NULL, is used instead of |dc_privatekey| to
1959 // authenticate the host.
1960 const SSL_PRIVATE_KEY_METHOD *dc_key_method = nullptr;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001961};
1962
Robert Sloan921ef2c2017-10-17 09:02:20 -07001963// |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
1964struct SSL_PROTOCOL_METHOD {
Robert Sloan36272962017-10-23 10:28:39 -07001965 bool is_dtls;
1966 bool (*ssl_new)(SSL *ssl);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001967 void (*ssl_free)(SSL *ssl);
1968 // get_message sets |*out| to the current handshake message and returns true
1969 // if one has been received. It returns false if more input is needed.
Robert Sloanf63bd1f2019-04-16 09:26:20 -07001970 bool (*get_message)(const SSL *ssl, SSLMessage *out);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001971 // next_message is called to release the current handshake message.
1972 void (*next_message)(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07001973 // Use the |ssl_open_handshake| wrapper.
1974 ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
1975 uint8_t *out_alert, Span<uint8_t> in);
1976 // Use the |ssl_open_change_cipher_spec| wrapper.
1977 ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
1978 uint8_t *out_alert,
1979 Span<uint8_t> in);
1980 // Use the |ssl_open_app_data| wrapper.
1981 ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
1982 size_t *out_consumed, uint8_t *out_alert,
1983 Span<uint8_t> in);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001984 int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
1985 int len);
1986 int (*dispatch_alert)(SSL *ssl);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001987 // init_message begins a new handshake message of type |type|. |cbb| is the
1988 // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
Robert Sloan36272962017-10-23 10:28:39 -07001989 // the caller should write to. It returns true on success and false on error.
1990 bool (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001991 // finish_message finishes a handshake message. It sets |*out_msg| to the
Robert Sloan36272962017-10-23 10:28:39 -07001992 // serialized message. It returns true on success and false on error.
1993 bool (*finish_message)(SSL *ssl, CBB *cbb, bssl::Array<uint8_t> *out_msg);
1994 // add_message adds a handshake message to the pending flight. It returns
1995 // true on success and false on error.
1996 bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001997 // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
Robert Sloan36272962017-10-23 10:28:39 -07001998 // flight. It returns true on success and false on error.
1999 bool (*add_change_cipher_spec)(SSL *ssl);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002000 // flush_flight flushes the pending flight to the transport. It returns one on
2001 // success and <= 0 on error.
2002 int (*flush_flight)(SSL *ssl);
2003 // on_handshake_complete is called when the handshake is complete.
2004 void (*on_handshake_complete)(SSL *ssl);
2005 // set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It returns
Robert Sloan36272962017-10-23 10:28:39 -07002006 // true on success and false if changing the read state is forbidden at this
Robert Sloan921ef2c2017-10-17 09:02:20 -07002007 // point.
Robert Sloan36272962017-10-23 10:28:39 -07002008 bool (*set_read_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002009 // set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It returns
Robert Sloan36272962017-10-23 10:28:39 -07002010 // true on success and false if changing the write state is forbidden at this
Robert Sloan921ef2c2017-10-17 09:02:20 -07002011 // point.
Robert Sloan36272962017-10-23 10:28:39 -07002012 bool (*set_write_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002013};
2014
Robert Sloan36272962017-10-23 10:28:39 -07002015// The following wrappers call |open_*| but handle |read_shutdown| correctly.
2016
2017// ssl_open_handshake processes a record from |in| for reading a handshake
2018// message.
2019ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
2020 uint8_t *out_alert, Span<uint8_t> in);
2021
2022// ssl_open_change_cipher_spec processes a record from |in| for reading a
2023// ChangeCipherSpec.
2024ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2025 uint8_t *out_alert,
2026 Span<uint8_t> in);
2027
2028// ssl_open_app_data processes a record from |in| for reading application data.
2029// On success, it returns |ssl_open_record_success| and sets |*out| to the
2030// input. If it encounters a post-handshake message, it returns
2031// |ssl_open_record_discard|. The caller should then retry, after processing any
2032// messages received with |get_message|.
2033ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
2034 size_t *out_consumed, uint8_t *out_alert,
2035 Span<uint8_t> in);
2036
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002037struct SSL_X509_METHOD {
2038 // check_client_CA_list returns one if |names| is a good list of X.509
2039 // distinguished names and zero otherwise. This is used to ensure that we can
2040 // reject unparsable values at handshake time when using crypto/x509.
Pete Bentley0c61efe2019-08-13 09:32:23 +01002041 bool (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002042
2043 // cert_clear frees and NULLs all X509 certificate-related state.
2044 void (*cert_clear)(CERT *cert);
2045 // cert_free frees all X509-related state.
2046 void (*cert_free)(CERT *cert);
2047 // cert_flush_cached_chain drops any cached |X509|-based certificate chain
2048 // from |cert|.
2049 // cert_dup duplicates any needed fields from |cert| to |new_cert|.
2050 void (*cert_dup)(CERT *new_cert, const CERT *cert);
2051 void (*cert_flush_cached_chain)(CERT *cert);
2052 // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
2053 // from |cert|.
2054 void (*cert_flush_cached_leaf)(CERT *cert);
2055
2056 // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
2057 // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
Pete Bentley0c61efe2019-08-13 09:32:23 +01002058 // true on success or false on error.
2059 bool (*session_cache_objects)(SSL_SESSION *session);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002060 // session_dup duplicates any needed fields from |session| to |new_session|.
Pete Bentley0c61efe2019-08-13 09:32:23 +01002061 // It returns true on success or false on error.
2062 bool (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002063 // session_clear frees any X509-related state from |session|.
2064 void (*session_clear)(SSL_SESSION *session);
2065 // session_verify_cert_chain verifies the certificate chain in |session|,
Pete Bentley0c61efe2019-08-13 09:32:23 +01002066 // sets |session->verify_result| and returns true on success or false on
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002067 // error.
Pete Bentley0c61efe2019-08-13 09:32:23 +01002068 bool (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl,
2069 uint8_t *out_alert);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002070
2071 // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
2072 void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs);
Pete Bentley0c61efe2019-08-13 09:32:23 +01002073 // ssl_new does any necessary initialisation of |hs|. It returns true on
2074 // success or false on error.
2075 bool (*ssl_new)(SSL_HANDSHAKE *hs);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002076 // ssl_free frees anything created by |ssl_new|.
2077 void (*ssl_config_free)(SSL_CONFIG *cfg);
2078 // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
2079 void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg);
2080 // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
2081 // necessary. On success, it updates |ssl|'s certificate configuration as
Pete Bentley0c61efe2019-08-13 09:32:23 +01002082 // needed and returns true. Otherwise, it returns false.
2083 bool (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs);
2084 // ssl_ctx_new does any necessary initialisation of |ctx|. It returns true on
2085 // success or false on error.
2086 bool (*ssl_ctx_new)(SSL_CTX *ctx);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002087 // ssl_ctx_free frees anything created by |ssl_ctx_new|.
2088 void (*ssl_ctx_free)(SSL_CTX *ctx);
2089 // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
2090 void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
2091};
2092
Robert Sloana27a6a42017-09-05 08:39:28 -07002093// ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
2094// crypto/x509.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002095extern const SSL_X509_METHOD ssl_crypto_x509_method;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002096
Robert Sloana27a6a42017-09-05 08:39:28 -07002097// ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
2098// crypto/x509.
Robert Sloan84377092017-08-14 09:33:19 -07002099extern const SSL_X509_METHOD ssl_noop_x509_method;
2100
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002101struct TicketKey {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002102 static constexpr bool kAllowUniquePtr = true;
2103
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002104 uint8_t name[SSL_TICKET_KEY_NAME_LEN] = {0};
2105 uint8_t hmac_key[16] = {0};
2106 uint8_t aes_key[16] = {0};
Robert Sloan921ef2c2017-10-17 09:02:20 -07002107 // next_rotation_tv_sec is the time (in seconds from the epoch) when the
2108 // current key should be superseded by a new key, or the time when a previous
2109 // key should be dropped. If zero, then the key should not be automatically
2110 // rotated.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002111 uint64_t next_rotation_tv_sec = 0;
2112};
2113
2114struct CertCompressionAlg {
2115 static constexpr bool kAllowUniquePtr = true;
2116
2117 ssl_cert_compression_func_t compress = nullptr;
2118 ssl_cert_decompression_func_t decompress = nullptr;
2119 uint16_t alg_id = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002120};
2121
Robert Sloan726e9d12018-09-11 11:45:04 -07002122BSSL_NAMESPACE_END
Robert Sloan921ef2c2017-10-17 09:02:20 -07002123
Robert Sloan5bdaadb2018-10-30 16:00:26 -07002124DEFINE_LHASH_OF(SSL_SESSION)
Robert Sloan921ef2c2017-10-17 09:02:20 -07002125
Robert Sloan4c22c5f2019-03-01 15:53:37 -08002126DEFINE_NAMED_STACK_OF(CertCompressionAlg, bssl::CertCompressionAlg)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002127
Robert Sloan726e9d12018-09-11 11:45:04 -07002128BSSL_NAMESPACE_BEGIN
Robert Sloan921ef2c2017-10-17 09:02:20 -07002129
Robert Sloana27a6a42017-09-05 08:39:28 -07002130// An ssl_shutdown_t describes the shutdown state of one end of the connection,
2131// whether it is alive or has been shutdown via close_notify or fatal alert.
David Benjamin95add822016-10-19 01:09:12 -04002132enum ssl_shutdown_t {
2133 ssl_shutdown_none = 0,
2134 ssl_shutdown_close_notify = 1,
Robert Sloan36272962017-10-23 10:28:39 -07002135 ssl_shutdown_error = 2,
David Benjamin95add822016-10-19 01:09:12 -04002136};
2137
Robert Sloanb6d070c2017-07-24 08:40:01 -07002138struct SSL3_STATE {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002139 static constexpr bool kAllowUniquePtr = true;
David Benjamin95add822016-10-19 01:09:12 -04002140
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002141 SSL3_STATE();
2142 ~SSL3_STATE();
2143
2144 uint8_t read_sequence[8] = {0};
2145 uint8_t write_sequence[8] = {0};
2146
2147 uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
2148 uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
David Benjamin95add822016-10-19 01:09:12 -04002149
Robert Sloana27a6a42017-09-05 08:39:28 -07002150 // read_buffer holds data from the transport to be processed.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002151 SSLBuffer read_buffer;
Robert Sloana27a6a42017-09-05 08:39:28 -07002152 // write_buffer holds data to be written to the transport.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002153 SSLBuffer write_buffer;
David Benjamin95add822016-10-19 01:09:12 -04002154
Robert Sloan36272962017-10-23 10:28:39 -07002155 // pending_app_data is the unconsumed application data. It points into
2156 // |read_buffer|.
2157 Span<uint8_t> pending_app_data;
David Benjamin95add822016-10-19 01:09:12 -04002158
Robert Sloana27a6a42017-09-05 08:39:28 -07002159 // partial write - check the numbers match
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002160 unsigned int wnum = 0; // number of bytes sent so far
2161 int wpend_tot = 0; // number bytes written
2162 int wpend_type = 0;
2163 int wpend_ret = 0; // number of bytes submitted
2164 const uint8_t *wpend_buf = nullptr;
David Benjamin95add822016-10-19 01:09:12 -04002165
Robert Sloan921ef2c2017-10-17 09:02:20 -07002166 // read_shutdown is the shutdown state for the read half of the connection.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002167 enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
David Benjamin95add822016-10-19 01:09:12 -04002168
Robert Sloan921ef2c2017-10-17 09:02:20 -07002169 // write_shutdown is the shutdown state for the write half of the connection.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002170 enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
David Benjamin95add822016-10-19 01:09:12 -04002171
Robert Sloan36272962017-10-23 10:28:39 -07002172 // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
2173 // the receive half of the connection.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002174 UniquePtr<ERR_SAVE_STATE> read_error;
Robert Sloan36272962017-10-23 10:28:39 -07002175
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002176 int total_renegotiations = 0;
2177
2178 // This holds a variable that indicates what we were doing when a 0 or -1 is
2179 // returned. This is needed for non-blocking IO so we know what request
2180 // needs re-doing when in SSL_accept or SSL_connect
2181 int rwstate = SSL_NOTHING;
David Benjamin95add822016-10-19 01:09:12 -04002182
Robert Sloancbf5ea62018-11-05 11:56:34 -08002183 enum ssl_encryption_level_t read_level = ssl_encryption_initial;
2184 enum ssl_encryption_level_t write_level = ssl_encryption_initial;
2185
Robert Sloana27a6a42017-09-05 08:39:28 -07002186 // early_data_skipped is the amount of early data that has been skipped by the
2187 // record layer.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002188 uint16_t early_data_skipped = 0;
David Benjamin1b249672016-12-06 18:25:50 -05002189
Robert Sloana27a6a42017-09-05 08:39:28 -07002190 // empty_record_count is the number of consecutive empty records received.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002191 uint8_t empty_record_count = 0;
David Benjamin95add822016-10-19 01:09:12 -04002192
Robert Sloana27a6a42017-09-05 08:39:28 -07002193 // warning_alert_count is the number of consecutive warning alerts
2194 // received.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002195 uint8_t warning_alert_count = 0;
David Benjamin95add822016-10-19 01:09:12 -04002196
Robert Sloana27a6a42017-09-05 08:39:28 -07002197 // key_update_count is the number of consecutive KeyUpdates received.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002198 uint8_t key_update_count = 0;
David Benjamin95add822016-10-19 01:09:12 -04002199
Robert Sloan15c0b352018-04-16 08:36:46 -07002200 // The negotiated Token Binding key parameter. Only valid if
2201 // |token_binding_negotiated| is set.
2202 uint8_t negotiated_token_binding_param = 0;
2203
Robert Sloana27a6a42017-09-05 08:39:28 -07002204 // skip_early_data instructs the record layer to skip unexpected early data
2205 // messages when 0RTT is rejected.
Robert Sloand9e572d2018-08-27 12:27:00 -07002206 bool skip_early_data : 1;
David Benjamin1b249672016-12-06 18:25:50 -05002207
Robert Sloana27a6a42017-09-05 08:39:28 -07002208 // have_version is true if the connection's final version is known. Otherwise
2209 // the version has not been negotiated yet.
Robert Sloand9e572d2018-08-27 12:27:00 -07002210 bool have_version : 1;
David Benjamin1b249672016-12-06 18:25:50 -05002211
Robert Sloana27a6a42017-09-05 08:39:28 -07002212 // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
2213 // and future messages should use the record layer.
Robert Sloand9e572d2018-08-27 12:27:00 -07002214 bool v2_hello_done : 1;
David Benjamin1b249672016-12-06 18:25:50 -05002215
Robert Sloana27a6a42017-09-05 08:39:28 -07002216 // is_v2_hello is true if the current handshake message was derived from a
2217 // V2ClientHello rather than received from the peer directly.
Robert Sloand9e572d2018-08-27 12:27:00 -07002218 bool is_v2_hello : 1;
Robert Sloan4d1ac502017-02-06 08:36:14 -08002219
Robert Sloana27a6a42017-09-05 08:39:28 -07002220 // has_message is true if the current handshake message has been returned
2221 // at least once by |get_message| and false otherwise.
Robert Sloand9e572d2018-08-27 12:27:00 -07002222 bool has_message : 1;
Robert Sloan84377092017-08-14 09:33:19 -07002223
Robert Sloana27a6a42017-09-05 08:39:28 -07002224 // initial_handshake_complete is true if the initial handshake has
2225 // completed.
Robert Sloand9e572d2018-08-27 12:27:00 -07002226 bool initial_handshake_complete : 1;
David Benjamin1b249672016-12-06 18:25:50 -05002227
Robert Sloana27a6a42017-09-05 08:39:28 -07002228 // session_reused indicates whether a session was resumed.
Robert Sloand9e572d2018-08-27 12:27:00 -07002229 bool session_reused : 1;
David Benjamin1b249672016-12-06 18:25:50 -05002230
Pete Bentley0c61efe2019-08-13 09:32:23 +01002231 // delegated_credential_used is whether we presented a delegated credential to
2232 // the peer.
2233 bool delegated_credential_used : 1;
2234
Robert Sloand9e572d2018-08-27 12:27:00 -07002235 bool send_connection_binding : 1;
David Benjamin1b249672016-12-06 18:25:50 -05002236
Robert Sloana27a6a42017-09-05 08:39:28 -07002237 // In a client, this means that the server supported Channel ID and that a
2238 // Channel ID was sent. In a server it means that we echoed support for
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002239 // Channel IDs and that |channel_id| will be valid after the handshake.
Robert Sloand9e572d2018-08-27 12:27:00 -07002240 bool channel_id_valid : 1;
David Benjamin1b249672016-12-06 18:25:50 -05002241
Robert Sloana27a6a42017-09-05 08:39:28 -07002242 // key_update_pending is true if we have a KeyUpdate acknowledgment
2243 // outstanding.
Robert Sloand9e572d2018-08-27 12:27:00 -07002244 bool key_update_pending : 1;
Robert Sloan572a4e22017-04-17 10:52:19 -07002245
Robert Sloana27a6a42017-09-05 08:39:28 -07002246 // wpend_pending is true if we have a pending write outstanding.
Robert Sloand9e572d2018-08-27 12:27:00 -07002247 bool wpend_pending : 1;
Robert Sloane56da3e2017-06-26 08:26:42 -07002248
Robert Sloan0da43952018-01-03 15:13:14 -08002249 // early_data_accepted is true if early data was accepted by the server.
Robert Sloand9e572d2018-08-27 12:27:00 -07002250 bool early_data_accepted : 1;
Robert Sloan0da43952018-01-03 15:13:14 -08002251
Robert Sloand9e572d2018-08-27 12:27:00 -07002252 // tls13_downgrade is whether the TLS 1.3 anti-downgrade logic fired.
2253 bool tls13_downgrade : 1;
Robert Sloan0da43952018-01-03 15:13:14 -08002254
Robert Sloan15c0b352018-04-16 08:36:46 -07002255 // token_binding_negotiated is set if Token Binding was negotiated.
Robert Sloand9e572d2018-08-27 12:27:00 -07002256 bool token_binding_negotiated : 1;
Robert Sloan15c0b352018-04-16 08:36:46 -07002257
Pete Bentley0c61efe2019-08-13 09:32:23 +01002258 // pq_experimental_signal_seen is true if the peer was observed
2259 // sending/echoing the post-quantum experiment signal.
2260 bool pq_experiment_signal_seen : 1;
2261
2262 // alert_dispatch is true there is an alert in |send_alert| to be sent.
2263 bool alert_dispatch : 1;
2264
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002265 // hs_buf is the buffer of handshake data to process.
2266 UniquePtr<BUF_MEM> hs_buf;
David Benjamin1b249672016-12-06 18:25:50 -05002267
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002268 // pending_hs_data contains the pending handshake data that has not yet
2269 // been encrypted to |pending_flight|. This allows packing the handshake into
2270 // fewer records.
2271 UniquePtr<BUF_MEM> pending_hs_data;
2272
Robert Sloana27a6a42017-09-05 08:39:28 -07002273 // pending_flight is the pending outgoing flight. This is used to flush each
2274 // handshake flight in a single write. |write_buffer| must be written out
2275 // before this data.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002276 UniquePtr<BUF_MEM> pending_flight;
Robert Sloan4d1ac502017-02-06 08:36:14 -08002277
Robert Sloana27a6a42017-09-05 08:39:28 -07002278 // pending_flight_offset is the number of bytes of |pending_flight| which have
2279 // been successfully written.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002280 uint32_t pending_flight_offset = 0;
David Benjamin1b249672016-12-06 18:25:50 -05002281
Robert Sloana450c922018-01-08 10:35:32 -08002282 // ticket_age_skew is the difference, in seconds, between the client-sent
2283 // ticket age and the server-computed value in TLS 1.3 server connections
2284 // which resumed a session.
2285 int32_t ticket_age_skew = 0;
2286
Pete Bentley0c61efe2019-08-13 09:32:23 +01002287 // ssl_early_data_reason stores details on why 0-RTT was accepted or rejected.
2288 enum ssl_early_data_reason_t early_data_reason = ssl_early_data_unknown;
2289
Robert Sloana27a6a42017-09-05 08:39:28 -07002290 // aead_read_ctx is the current read cipher state.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002291 UniquePtr<SSLAEADContext> aead_read_ctx;
David Benjamin95add822016-10-19 01:09:12 -04002292
Robert Sloana27a6a42017-09-05 08:39:28 -07002293 // aead_write_ctx is the current write cipher state.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002294 UniquePtr<SSLAEADContext> aead_write_ctx;
David Benjamin95add822016-10-19 01:09:12 -04002295
Robert Sloana27a6a42017-09-05 08:39:28 -07002296 // hs is the handshake state for the current handshake or NULL if there isn't
2297 // one.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002298 UniquePtr<SSL_HANDSHAKE> hs;
David Benjamin95add822016-10-19 01:09:12 -04002299
Pete Bentley0c61efe2019-08-13 09:32:23 +01002300 uint8_t write_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2301 uint8_t read_traffic_secret[SSL_MAX_MD_SIZE] = {0};
2302 uint8_t exporter_secret[SSL_MAX_MD_SIZE] = {0};
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002303 uint8_t write_traffic_secret_len = 0;
2304 uint8_t read_traffic_secret_len = 0;
2305 uint8_t exporter_secret_len = 0;
David Benjamin95add822016-10-19 01:09:12 -04002306
Robert Sloana27a6a42017-09-05 08:39:28 -07002307 // Connection binding to prevent renegotiation attacks
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002308 uint8_t previous_client_finished[12] = {0};
2309 uint8_t previous_client_finished_len = 0;
2310 uint8_t previous_server_finished_len = 0;
2311 uint8_t previous_server_finished[12] = {0};
David Benjamin95add822016-10-19 01:09:12 -04002312
Robert Sloana450c922018-01-08 10:35:32 -08002313 uint8_t send_alert[2] = {0};
2314
Robert Sloana27a6a42017-09-05 08:39:28 -07002315 // established_session is the session established by the connection. This
2316 // session is only filled upon the completion of the handshake and is
2317 // immutable.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002318 UniquePtr<SSL_SESSION> established_session;
David Benjamin95add822016-10-19 01:09:12 -04002319
Robert Sloana27a6a42017-09-05 08:39:28 -07002320 // Next protocol negotiation. For the client, this is the protocol that we
2321 // sent in NextProtocol and is set when handling ServerHello extensions.
2322 //
2323 // For a server, this is the client's selected_protocol from NextProtocol and
2324 // is set when handling the NextProtocol message, before the Finished
2325 // message.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002326 Array<uint8_t> next_proto_negotiated;
David Benjamin95add822016-10-19 01:09:12 -04002327
Robert Sloana27a6a42017-09-05 08:39:28 -07002328 // ALPN information
2329 // (we are in the process of transitioning from NPN to ALPN.)
David Benjamin95add822016-10-19 01:09:12 -04002330
Robert Sloana27a6a42017-09-05 08:39:28 -07002331 // In a server these point to the selected ALPN protocol after the
2332 // ClientHello has been processed. In a client these contain the protocol
2333 // that the server selected once the ServerHello has been processed.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002334 Array<uint8_t> alpn_selected;
David Benjamin95add822016-10-19 01:09:12 -04002335
Robert Sloand1d118f2017-09-11 09:00:48 -07002336 // hostname, on the server, is the value of the SNI extension.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002337 UniquePtr<char> hostname;
Robert Sloand1d118f2017-09-11 09:00:48 -07002338
Robert Sloana27a6a42017-09-05 08:39:28 -07002339 // For a server:
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002340 // If |channel_id_valid| is true, then this contains the
Robert Sloana27a6a42017-09-05 08:39:28 -07002341 // verified Channel ID from the client: a P256 point, (x,y), where
2342 // each are big-endian values.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002343 uint8_t channel_id[64] = {0};
Robert Sloan8542c082018-02-05 09:07:34 -08002344
2345 // Contains the QUIC transport params received by the peer.
2346 Array<uint8_t> peer_quic_transport_params;
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002347
2348 // srtp_profile is the selected SRTP protection profile for
2349 // DTLS-SRTP.
2350 const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002351};
David Benjamin95add822016-10-19 01:09:12 -04002352
Robert Sloana27a6a42017-09-05 08:39:28 -07002353// lengths of messages
Adam Langleye9ada862015-05-11 17:20:37 -07002354#define DTLS1_COOKIE_LENGTH 256
2355
2356#define DTLS1_RT_HEADER_LENGTH 13
2357
2358#define DTLS1_HM_HEADER_LENGTH 12
2359
2360#define DTLS1_CCS_HEADER_LENGTH 1
2361
2362#define DTLS1_AL_HEADER_LENGTH 2
2363
Adam Langleye9ada862015-05-11 17:20:37 -07002364struct hm_header_st {
2365 uint8_t type;
2366 uint32_t msg_len;
2367 uint16_t seq;
2368 uint32_t frag_off;
2369 uint32_t frag_len;
Adam Langleye9ada862015-05-11 17:20:37 -07002370};
2371
Robert Sloana27a6a42017-09-05 08:39:28 -07002372// An hm_fragment is an incoming DTLS message, possibly not yet assembled.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002373struct hm_fragment {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002374 static constexpr bool kAllowUniquePtr = true;
2375
2376 hm_fragment() {}
2377 hm_fragment(const hm_fragment &) = delete;
2378 hm_fragment &operator=(const hm_fragment &) = delete;
2379
2380 ~hm_fragment();
2381
Robert Sloana27a6a42017-09-05 08:39:28 -07002382 // type is the type of the message.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002383 uint8_t type = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002384 // seq is the sequence number of this message.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002385 uint16_t seq = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002386 // msg_len is the length of the message body.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002387 uint32_t msg_len = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002388 // data is a pointer to the message, including message header. It has length
2389 // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002390 uint8_t *data = nullptr;
Robert Sloana27a6a42017-09-05 08:39:28 -07002391 // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
2392 // the message have been received. It is NULL if the message is complete.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002393 uint8_t *reassembly = nullptr;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002394};
Adam Langleye9ada862015-05-11 17:20:37 -07002395
Robert Sloan7d422bc2017-03-06 10:04:29 -08002396struct OPENSSL_timeval {
2397 uint64_t tv_sec;
2398 uint32_t tv_usec;
2399};
2400
Robert Sloanb6d070c2017-07-24 08:40:01 -07002401struct DTLS1_STATE {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002402 static constexpr bool kAllowUniquePtr = true;
2403
2404 DTLS1_STATE();
2405 ~DTLS1_STATE();
2406
Robert Sloana27a6a42017-09-05 08:39:28 -07002407 // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
2408 // the peer in this epoch.
Robert Sloand9e572d2018-08-27 12:27:00 -07002409 bool has_change_cipher_spec : 1;
Robert Sloanfe7cd212017-08-07 09:03:39 -07002410
Robert Sloana27a6a42017-09-05 08:39:28 -07002411 // outgoing_messages_complete is true if |outgoing_messages| has been
2412 // completed by an attempt to flush it. Future calls to |add_message| and
2413 // |add_change_cipher_spec| will start a new flight.
Robert Sloand9e572d2018-08-27 12:27:00 -07002414 bool outgoing_messages_complete : 1;
Adam Langleye9ada862015-05-11 17:20:37 -07002415
Robert Sloana27a6a42017-09-05 08:39:28 -07002416 // flight_has_reply is true if the current outgoing flight is complete and has
2417 // processed at least one message. This is used to detect whether we or the
2418 // peer sent the final flight.
Robert Sloand9e572d2018-08-27 12:27:00 -07002419 bool flight_has_reply : 1;
Robert Sloan8f860b12017-08-28 07:37:06 -07002420
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002421 uint8_t cookie[DTLS1_COOKIE_LENGTH] = {0};
2422 size_t cookie_len = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002423
Robert Sloana27a6a42017-09-05 08:39:28 -07002424 // The current data and handshake epoch. This is initially undefined, and
2425 // starts at zero once the initial handshake is completed.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002426 uint16_t r_epoch = 0;
2427 uint16_t w_epoch = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002428
Robert Sloana27a6a42017-09-05 08:39:28 -07002429 // records being received in the current epoch
Adam Langleye9ada862015-05-11 17:20:37 -07002430 DTLS1_BITMAP bitmap;
2431
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002432 uint16_t handshake_write_seq = 0;
2433 uint16_t handshake_read_seq = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002434
Robert Sloana27a6a42017-09-05 08:39:28 -07002435 // save last sequence number for retransmissions
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002436 uint8_t last_write_sequence[8] = {0};
2437 UniquePtr<SSLAEADContext> last_aead_write_ctx;
Adam Langleye9ada862015-05-11 17:20:37 -07002438
Robert Sloana27a6a42017-09-05 08:39:28 -07002439 // incoming_messages is a ring buffer of incoming handshake messages that have
2440 // yet to be processed. The front of the ring buffer is message number
2441 // |handshake_read_seq|, at position |handshake_read_seq| %
2442 // |SSL_MAX_HANDSHAKE_FLIGHT|.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002443 UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
Adam Langleye9ada862015-05-11 17:20:37 -07002444
Robert Sloana27a6a42017-09-05 08:39:28 -07002445 // outgoing_messages is the queue of outgoing messages from the last handshake
2446 // flight.
David Benjaminc895d6b2016-08-11 13:26:41 -04002447 DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002448 uint8_t outgoing_messages_len = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002449
Robert Sloana27a6a42017-09-05 08:39:28 -07002450 // outgoing_written is the number of outgoing messages that have been
2451 // written.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002452 uint8_t outgoing_written = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002453 // outgoing_offset is the number of bytes of the next outgoing message have
2454 // been written.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002455 uint32_t outgoing_offset = 0;
Robert Sloan4d1ac502017-02-06 08:36:14 -08002456
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002457 unsigned mtu = 0; // max DTLS packet size
Adam Langleye9ada862015-05-11 17:20:37 -07002458
Robert Sloana27a6a42017-09-05 08:39:28 -07002459 // num_timeouts is the number of times the retransmit timer has fired since
2460 // the last time it was reset.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002461 unsigned num_timeouts = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002462
Robert Sloana27a6a42017-09-05 08:39:28 -07002463 // Indicates when the last handshake msg or heartbeat sent will
2464 // timeout.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002465 struct OPENSSL_timeval next_timeout = {0, 0};
Adam Langleye9ada862015-05-11 17:20:37 -07002466
Robert Sloana27a6a42017-09-05 08:39:28 -07002467 // timeout_duration_ms is the timeout duration in milliseconds.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002468 unsigned timeout_duration_ms = 0;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002469};
Adam Langleye9ada862015-05-11 17:20:37 -07002470
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002471// SSL_CONFIG contains configuration bits that can be shed after the handshake
2472// completes. Objects of this type are not shared; they are unique to a
2473// particular |SSL|.
2474//
2475// See SSL_shed_handshake_config() for more about the conditions under which
2476// configuration can be shed.
2477struct SSL_CONFIG {
2478 static constexpr bool kAllowUniquePtr = true;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002479
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002480 explicit SSL_CONFIG(SSL *ssl_arg);
2481 ~SSL_CONFIG();
2482
2483 // ssl is a non-owning pointer to the parent |SSL| object.
2484 SSL *const ssl = nullptr;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002485
Robert Sloan4726ed32019-04-08 12:43:32 -07002486 // conf_max_version is the maximum acceptable version configured by
2487 // |SSL_set_max_proto_version|. Note this version is not normalized in DTLS
2488 // and is further constrained by |SSL_OP_NO_*|.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002489 uint16_t conf_max_version = 0;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002490
Robert Sloan4726ed32019-04-08 12:43:32 -07002491 // conf_min_version is the minimum acceptable version configured by
2492 // |SSL_set_min_proto_version|. Note this version is not normalized in DTLS
2493 // and is further constrained by |SSL_OP_NO_*|.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002494 uint16_t conf_min_version = 0;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002495
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002496 X509_VERIFY_PARAM *param = nullptr;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002497
Robert Sloana27a6a42017-09-05 08:39:28 -07002498 // crypto
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002499 UniquePtr<SSLCipherPreferenceList> cipher_list;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002500
Robert Sloan8542c082018-02-05 09:07:34 -08002501 // This is used to hold the local certificate used (i.e. the server
2502 // certificate for a server or the client certificate for a client).
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002503 UniquePtr<CERT> cert;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002504
2505 int (*verify_callback)(int ok,
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002506 X509_STORE_CTX *ctx) =
2507 nullptr; // fail if callback returns 0
Robert Sloan47f43ed2017-02-06 14:55:15 -08002508
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002509 enum ssl_verify_result_t (*custom_verify_callback)(
2510 SSL *ssl, uint8_t *out_alert) = nullptr;
Robert Sloana27a6a42017-09-05 08:39:28 -07002511 // Server-only: psk_identity_hint is the identity hint to send in
2512 // PSK-based key exchanges.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002513 UniquePtr<char> psk_identity_hint;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002514
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002515 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
2516 unsigned max_identity_len, uint8_t *psk,
2517 unsigned max_psk_len) = nullptr;
2518 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
2519 unsigned max_psk_len) = nullptr;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002520
Robert Sloana27a6a42017-09-05 08:39:28 -07002521 // for server side, keep the list of CA_dn we can use
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002522 UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
Robert Sloan7d422bc2017-03-06 10:04:29 -08002523
Robert Sloana27a6a42017-09-05 08:39:28 -07002524 // cached_x509_client_CA is a cache of parsed versions of the elements of
2525 // |client_CA|.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002526 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002527
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002528 Array<uint16_t> supported_group_list; // our list
Robert Sloan47f43ed2017-02-06 14:55:15 -08002529
Robert Sloana27a6a42017-09-05 08:39:28 -07002530 // The client's Channel ID private key.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002531 UniquePtr<EVP_PKEY> channel_id_private;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002532
Robert Sloana27a6a42017-09-05 08:39:28 -07002533 // For a client, this contains the list of supported protocols in wire
2534 // format.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002535 Array<uint8_t> alpn_client_proto_list;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002536
Robert Sloan978112c2018-01-22 12:53:01 -08002537 // Contains a list of supported Token Binding key parameters.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002538 Array<uint8_t> token_binding_params;
Robert Sloan978112c2018-01-22 12:53:01 -08002539
Robert Sloan8542c082018-02-05 09:07:34 -08002540 // Contains the QUIC transport params that this endpoint will send.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002541 Array<uint8_t> quic_transport_params;
Robert Sloan8542c082018-02-05 09:07:34 -08002542
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002543 // verify_sigalgs, if not empty, is the set of signature algorithms
2544 // accepted from the peer in decreasing order of preference.
2545 Array<uint16_t> verify_sigalgs;
2546
2547 // srtp_profiles is the list of configured SRTP protection profiles for
2548 // DTLS-SRTP.
2549 UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002550
Robert Sloana27a6a42017-09-05 08:39:28 -07002551 // verify_mode is a bitmask of |SSL_VERIFY_*| values.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002552 uint8_t verify_mode = SSL_VERIFY_NONE;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002553
Robert Sloana27a6a42017-09-05 08:39:28 -07002554 // Enable signed certificate time stamps. Currently client only.
Robert Sloand9e572d2018-08-27 12:27:00 -07002555 bool signed_cert_timestamps_enabled : 1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002556
Robert Sloana27a6a42017-09-05 08:39:28 -07002557 // ocsp_stapling_enabled is only used by client connections and indicates
2558 // whether OCSP stapling will be requested.
Robert Sloand9e572d2018-08-27 12:27:00 -07002559 bool ocsp_stapling_enabled : 1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002560
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002561 // channel_id_enabled is copied from the |SSL_CTX|. For a server, means that
2562 // we'll accept Channel IDs from clients. For a client, means that we'll
2563 // advertise support.
Robert Sloand9e572d2018-08-27 12:27:00 -07002564 bool channel_id_enabled : 1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002565
Robert Sloan4c22c5f2019-03-01 15:53:37 -08002566 // If enforce_rsa_key_usage is true, the handshake will fail if the
2567 // keyUsage extension is present and incompatible with the TLS usage.
2568 // This field is not read until after certificate verification.
2569 bool enforce_rsa_key_usage : 1;
2570
Robert Sloana27a6a42017-09-05 08:39:28 -07002571 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
2572 // hash of the peer's certificate and then discard it to save memory and
2573 // session space. Only effective on the server side.
Robert Sloand9e572d2018-08-27 12:27:00 -07002574 bool retain_only_sha256_of_client_certs : 1;
Robert Sloan8542c082018-02-05 09:07:34 -08002575
2576 // handoff indicates that a server should stop after receiving the
2577 // ClientHello and pause the handshake in such a way that |SSL_get_error|
2578 // returns |SSL_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX|
2579 // element of the same name and may be cleared if the handoff is declined.
Robert Sloand9e572d2018-08-27 12:27:00 -07002580 bool handoff : 1;
Robert Sloanab8b8882018-03-26 11:39:51 -07002581
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002582 // shed_handshake_config indicates that the handshake config (this object!)
2583 // should be freed after the handshake completes.
2584 bool shed_handshake_config : 1;
Robert Sloanf068def2018-10-10 18:45:40 -07002585
2586 // ignore_tls13_downgrade is whether the connection should continue when the
2587 // server random signals a downgrade.
Robert Sloanc9abfe42018-11-26 12:19:07 -08002588 bool ignore_tls13_downgrade : 1;
2589
2590 // jdk11_workaround is whether to disable TLS 1.3 for JDK 11 clients, as a
2591 // workaround for https://bugs.openjdk.java.net/browse/JDK-8211806.
2592 bool jdk11_workaround : 1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002593};
2594
Robert Sloand9e572d2018-08-27 12:27:00 -07002595// From RFC 8446, used in determining PSK modes.
Steven Valdez909b19f2016-11-21 15:35:44 -05002596#define SSL_PSK_DHE_KE 0x1
David Benjaminc895d6b2016-08-11 13:26:41 -04002597
Robert Sloana27a6a42017-09-05 08:39:28 -07002598// kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
2599// data that will be accepted. This value should be slightly below
2600// kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
Robert Sloan9254e682017-04-24 09:42:06 -07002601static const size_t kMaxEarlyDataAccepted = 14336;
2602
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002603UniquePtr<CERT> ssl_cert_dup(CERT *cert);
Robert Sloanfe7cd212017-08-07 09:03:39 -07002604void ssl_cert_clear_certs(CERT *cert);
Robert Sloan726e9d12018-09-11 11:45:04 -07002605bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
2606bool ssl_is_key_type_supported(int key_type);
2607// ssl_compare_public_and_private_key returns true if |pubkey| is the public
2608// counterpart to |privkey|. Otherwise it returns false and pushes a helpful
Robert Sloana27a6a42017-09-05 08:39:28 -07002609// message on the error queue.
Robert Sloan726e9d12018-09-11 11:45:04 -07002610bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
Robert Sloan4d1ac502017-02-06 08:36:14 -08002611 const EVP_PKEY *privkey);
Robert Sloan726e9d12018-09-11 11:45:04 -07002612bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey);
David Benjamin1b249672016-12-06 18:25:50 -05002613int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002614int ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, const SSL_SESSION *session);
Robert Sloan8f860b12017-08-28 07:37:06 -07002615int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00002616
Robert Sloana27a6a42017-09-05 08:39:28 -07002617// ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
2618// error.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002619UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
Robert Sloan5d625782017-02-13 09:55:39 -08002620
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002621// ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table
2622// keyed on session IDs.
2623uint32_t ssl_hash_session_id(Span<const uint8_t> session_id);
2624
Robert Sloana27a6a42017-09-05 08:39:28 -07002625// SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
2626// the parsed data.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002627OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_parse(
2628 CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool);
Robert Sloan5d625782017-02-13 09:55:39 -08002629
Robert Sloan309a31e2018-01-29 10:22:47 -08002630// ssl_session_serialize writes |in| to |cbb| as if it were serialising a
2631// session for Session-ID resumption. It returns one on success and zero on
2632// error.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002633OPENSSL_EXPORT int ssl_session_serialize(const SSL_SESSION *in, CBB *cbb);
Robert Sloan309a31e2018-01-29 10:22:47 -08002634
Robert Sloana27a6a42017-09-05 08:39:28 -07002635// ssl_session_is_context_valid returns one if |session|'s session ID context
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002636// matches the one set on |hs| and zero otherwise.
2637int ssl_session_is_context_valid(const SSL_HANDSHAKE *hs,
2638 const SSL_SESSION *session);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002639
Robert Sloana27a6a42017-09-05 08:39:28 -07002640// ssl_session_is_time_valid returns one if |session| is still valid and zero if
2641// it has expired.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002642int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
2643
Robert Sloana27a6a42017-09-05 08:39:28 -07002644// ssl_session_is_resumable returns one if |session| is resumable for |hs| and
2645// zero otherwise.
Robert Sloana94fe052017-02-21 08:49:28 -08002646int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
2647 const SSL_SESSION *session);
Steven Valdez909b19f2016-11-21 15:35:44 -05002648
Robert Sloanae1abf92017-10-05 12:50:08 -07002649// ssl_session_protocol_version returns the protocol version associated with
2650// |session|. Note that despite the name, this is not the same as
2651// |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
2652uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
Robert Sloanf6200e72017-07-10 08:09:18 -07002653
Robert Sloanae1abf92017-10-05 12:50:08 -07002654// ssl_session_get_digest returns the digest used in |session|.
2655const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
Robert Sloan5d625782017-02-13 09:55:39 -08002656
David Benjamin7c0d06c2016-08-11 13:26:41 -04002657void ssl_set_session(SSL *ssl, SSL_SESSION *session);
2658
Robert Sloana27a6a42017-09-05 08:39:28 -07002659// ssl_get_prev_session looks up the previous session based on |client_hello|.
2660// On success, it sets |*out_session| to the session or nullptr if none was
2661// found. If the session could not be looked up synchronously, it returns
2662// |ssl_hs_pending_session| and should be called again. If a ticket could not be
2663// decrypted immediately it returns |ssl_hs_pending_ticket| and should also
2664// be called again. Otherwise, it returns |ssl_hs_error|.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002665enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs,
Robert Sloana27a6a42017-09-05 08:39:28 -07002666 UniquePtr<SSL_SESSION> *out_session,
2667 bool *out_tickets_supported,
2668 bool *out_renew_ticket,
2669 const SSL_CLIENT_HELLO *client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +00002670
Robert Sloana27a6a42017-09-05 08:39:28 -07002671// The following flags determine which parts of the session are duplicated.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002672#define SSL_SESSION_DUP_AUTH_ONLY 0x0
2673#define SSL_SESSION_INCLUDE_TICKET 0x1
2674#define SSL_SESSION_INCLUDE_NONAUTH 0x2
2675#define SSL_SESSION_DUP_ALL \
2676 (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
2677
Robert Sloana27a6a42017-09-05 08:39:28 -07002678// SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
2679// fields in |session| or nullptr on error. The new session is non-resumable and
2680// must be explicitly marked resumable once it has been filled in.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002681OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
2682 int dup_flags);
David Benjaminc895d6b2016-08-11 13:26:41 -04002683
Robert Sloana27a6a42017-09-05 08:39:28 -07002684// ssl_session_rebase_time updates |session|'s start time to the current time,
2685// adjusting the timeout so the expiration time is unchanged.
Robert Sloan4d1ac502017-02-06 08:36:14 -08002686void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
2687
Robert Sloana27a6a42017-09-05 08:39:28 -07002688// ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
2689// |session|'s timeout to |timeout| (measured from the current time). The
2690// renewal is clamped to the session's auth_timeout.
Robert Sloan7d422bc2017-03-06 10:04:29 -08002691void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
2692 uint32_t timeout);
Steven Valdez909b19f2016-11-21 15:35:44 -05002693
David Benjamin1b249672016-12-06 18:25:50 -05002694void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002695
Pete Bentley0c61efe2019-08-13 09:32:23 +01002696void ssl_send_alert(SSL *ssl, int level, int desc);
2697int ssl_send_alert_impl(SSL *ssl, int level, int desc);
Robert Sloanf63bd1f2019-04-16 09:26:20 -07002698bool ssl3_get_message(const SSL *ssl, SSLMessage *out);
Robert Sloan36272962017-10-23 10:28:39 -07002699ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
2700 uint8_t *out_alert, Span<uint8_t> in);
Robert Sloan84377092017-08-14 09:33:19 -07002701void ssl3_next_message(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002702
Adam Langley4139edb2016-01-13 15:00:54 -08002703int ssl3_dispatch_alert(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07002704ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
2705 size_t *out_consumed, uint8_t *out_alert,
2706 Span<uint8_t> in);
2707ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2708 uint8_t *out_alert,
2709 Span<uint8_t> in);
Robert Sloana27a6a42017-09-05 08:39:28 -07002710int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
Robert Sloane56da3e2017-06-26 08:26:42 -07002711 int len);
Steven Valdez909b19f2016-11-21 15:35:44 -05002712
Robert Sloan36272962017-10-23 10:28:39 -07002713bool ssl3_new(SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08002714void ssl3_free(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002715
Robert Sloan36272962017-10-23 10:28:39 -07002716bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2717bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2718bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg);
2719bool ssl3_add_change_cipher_spec(SSL *ssl);
Robert Sloan4d1ac502017-02-06 08:36:14 -08002720int ssl3_flush_flight(SSL *ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04002721
Robert Sloan36272962017-10-23 10:28:39 -07002722bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2723bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2724bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
2725bool dtls1_add_change_cipher_spec(SSL *ssl);
Robert Sloan4d1ac502017-02-06 08:36:14 -08002726int dtls1_flush_flight(SSL *ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04002727
Robert Sloana27a6a42017-09-05 08:39:28 -07002728// ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
Robert Sloan36272962017-10-23 10:28:39 -07002729// the pending flight. It returns true on success and false on error.
2730bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
Steven Valdez909b19f2016-11-21 15:35:44 -05002731
Robert Sloan36272962017-10-23 10:28:39 -07002732// ssl_hash_message incorporates |msg| into the handshake hash. It returns true
2733// on success and false on allocation failure.
Robert Sloan84377092017-08-14 09:33:19 -07002734bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Steven Valdez909b19f2016-11-21 15:35:44 -05002735
Robert Sloan36272962017-10-23 10:28:39 -07002736ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
2737 size_t *out_consumed, uint8_t *out_alert,
2738 Span<uint8_t> in);
2739ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2740 uint8_t *out_alert,
2741 Span<uint8_t> in);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002742
Robert Sloana27a6a42017-09-05 08:39:28 -07002743int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
2744 const uint8_t *buf, int len);
David Benjamin6e899c72016-06-09 18:02:18 -04002745
Robert Sloana27a6a42017-09-05 08:39:28 -07002746// dtls1_write_record sends a record. It returns one on success and <= 0 on
2747// error.
David Benjamin6e899c72016-06-09 18:02:18 -04002748int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
2749 enum dtls1_use_epoch_t use_epoch);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002750
David Benjaminc895d6b2016-08-11 13:26:41 -04002751int dtls1_retransmit_outgoing_messages(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07002752bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
Robert Sloand9e572d2018-08-27 12:27:00 -07002753 CBS *out_body);
Robert Sloan36272962017-10-23 10:28:39 -07002754bool dtls1_check_timeout_num(SSL *ssl);
Adam Langleye9ada862015-05-11 17:20:37 -07002755
Adam Langley4139edb2016-01-13 15:00:54 -08002756void dtls1_start_timer(SSL *ssl);
2757void dtls1_stop_timer(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07002758bool dtls1_is_timer_expired(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002759unsigned int dtls1_min_mtu(void);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002760
Robert Sloan36272962017-10-23 10:28:39 -07002761bool dtls1_new(SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08002762void dtls1_free(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002763
Robert Sloanf63bd1f2019-04-16 09:26:20 -07002764bool dtls1_get_message(const SSL *ssl, SSLMessage *out);
Robert Sloan36272962017-10-23 10:28:39 -07002765ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
2766 uint8_t *out_alert, Span<uint8_t> in);
Robert Sloan84377092017-08-14 09:33:19 -07002767void dtls1_next_message(SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08002768int dtls1_dispatch_alert(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002769
Robert Sloan8542c082018-02-05 09:07:34 -08002770// tls1_configure_aead configures either the read or write direction AEAD (as
2771// determined by |direction|) using the keys generated by the TLS KDF. The
2772// |key_block_cache| argument is used to store the generated key block, if
2773// empty. Otherwise it's assumed that the key block is already contained within
2774// it. Returns one on success or zero on error.
2775int tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction,
2776 Array<uint8_t> *key_block_cache,
2777 const SSL_CIPHER *cipher,
2778 Span<const uint8_t> iv_override);
2779
Robert Sloan2e9e66a2017-09-25 09:08:29 -07002780int tls1_change_cipher_state(SSL_HANDSHAKE *hs, evp_aead_direction_t direction);
Robert Sloan5d625782017-02-13 09:55:39 -08002781int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
Robert Sloanb1b54b82017-11-06 13:50:02 -08002782 Span<const uint8_t> premaster);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002783
Robert Sloan4562e9d2017-10-02 10:26:51 -07002784// tls1_get_grouplist returns the locally-configured group preference list.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002785Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002786
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002787// tls1_check_group_id returns whether |group_id| is consistent with locally-
2788// configured group preferences.
2789bool tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id);
Kenny Rootb8494592015-09-25 02:29:14 +00002790
Robert Sloana27a6a42017-09-05 08:39:28 -07002791// tls1_get_shared_group sets |*out_group_id| to the first preferred shared
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002792// group between client and server preferences and returns true. If none may be
2793// found, it returns false.
2794bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002795
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002796// tls1_set_curves converts the array of NIDs in |curves| into a newly allocated
2797// array of TLS group IDs. On success, the function returns true and writes the
2798// array to |*out_group_ids|. Otherwise, it returns false.
2799bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002800
Robert Sloana27a6a42017-09-05 08:39:28 -07002801// tls1_set_curves_list converts the string of curves pointed to by |curves|
2802// into a newly allocated array of TLS group IDs. On success, the function
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002803// returns true and writes the array to |*out_group_ids|. Otherwise, it returns
2804// false.
2805bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002806
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002807// ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It returns
2808// true on success and false on failure. The |header_len| argument is the length
2809// of the ClientHello written so far and is used to compute the padding length.
2810// (It does not include the record header.)
2811bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
Kenny Roote99801b2015-11-06 15:31:15 -08002812
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002813bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
2814bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2815 const SSL_CLIENT_HELLO *client_hello);
2816bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002817
2818#define tlsext_tick_md EVP_sha256
Adam Langleyd9e397b2015-01-22 14:27:53 -08002819
Robert Sloana27a6a42017-09-05 08:39:28 -07002820// ssl_process_ticket processes a session ticket from the client. It returns
2821// one of:
2822// |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
2823// |*out_renew_ticket| is set to whether the ticket should be renewed.
2824// |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
2825// fresh ticket should be sent, but the given ticket cannot be used.
2826// |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
2827// Retry later.
2828// |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
Robert Sloan1c9db532017-03-13 08:03:59 -07002829enum ssl_ticket_aead_result_t ssl_process_ticket(
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002830 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
Robert Sloand9e572d2018-08-27 12:27:00 -07002831 bool *out_renew_ticket, Span<const uint8_t> ticket,
2832 Span<const uint8_t> session_id);
Kenny Rootb8494592015-09-25 02:29:14 +00002833
Robert Sloana27a6a42017-09-05 08:39:28 -07002834// tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002835// the signature. If the key is valid, it saves the Channel ID and returns true.
2836// Otherwise, it returns false.
2837bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Steven Valdez909b19f2016-11-21 15:35:44 -05002838
Robert Sloana27a6a42017-09-05 08:39:28 -07002839// tls1_write_channel_id generates a Channel ID message and puts the output in
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002840// |cbb|. |ssl->channel_id_private| must already be set before calling. This
2841// function returns true on success and false on error.
Robert Sloanb1b54b82017-11-06 13:50:02 -08002842bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
Steven Valdez909b19f2016-11-21 15:35:44 -05002843
Robert Sloana27a6a42017-09-05 08:39:28 -07002844// tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
2845// it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002846// true on success and false on failure.
2847bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002848
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002849// tls1_record_handshake_hashes_for_channel_id records the current handshake
2850// hashes in |hs->new_session| so that Channel ID resumptions can sign that
2851// data.
2852bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
Kenny Roota04d78d2015-09-25 00:26:37 +00002853
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002854// ssl_do_channel_id_callback checks runs |hs->ssl->ctx->channel_id_cb| if
2855// necessary. It returns true on success and false on fatal error. Note that, on
2856// success, |hs->ssl->channel_id_private| may be unset, in which case the
Robert Sloana27a6a42017-09-05 08:39:28 -07002857// operation should be retried later.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002858bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs);
Steven Valdez909b19f2016-11-21 15:35:44 -05002859
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002860// ssl_can_write returns whether |ssl| is allowed to write.
2861bool ssl_can_write(const SSL *ssl);
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002862
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002863// ssl_can_read returns wheter |ssl| is allowed to read.
2864bool ssl_can_read(const SSL *ssl);
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002865
Robert Sloan7d422bc2017-03-06 10:04:29 -08002866void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
Robert Sloan8f860b12017-08-28 07:37:06 -07002867void ssl_ctx_get_current_time(const SSL_CTX *ctx,
2868 struct OPENSSL_timeval *out_clock);
David Benjaminc895d6b2016-08-11 13:26:41 -04002869
Robert Sloana27a6a42017-09-05 08:39:28 -07002870// ssl_reset_error_state resets state for |SSL_get_error|.
Robert Sloan69939df2017-01-09 10:53:07 -08002871void ssl_reset_error_state(SSL *ssl);
2872
Robert Sloan36272962017-10-23 10:28:39 -07002873// ssl_set_read_error sets |ssl|'s read half into an error state, saving the
2874// current state of the error queue.
Robert Sloand9e572d2018-08-27 12:27:00 -07002875void ssl_set_read_error(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07002876
Robert Sloan726e9d12018-09-11 11:45:04 -07002877BSSL_NAMESPACE_END
Robert Sloanb6d070c2017-07-24 08:40:01 -07002878
2879
Robert Sloana27a6a42017-09-05 08:39:28 -07002880// Opaque C types.
2881//
2882// The following types are exported to C code as public typedefs, so they must
2883// be defined outside of the namespace.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002884
Robert Sloana27a6a42017-09-05 08:39:28 -07002885// ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
2886// structure to support the legacy version-locked methods.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002887struct ssl_method_st {
Robert Sloana27a6a42017-09-05 08:39:28 -07002888 // version, if non-zero, is the only protocol version acceptable to an
2889 // SSL_CTX initialized from this method.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002890 uint16_t version;
Robert Sloana27a6a42017-09-05 08:39:28 -07002891 // method is the underlying SSL_PROTOCOL_METHOD that initializes the
2892 // SSL_CTX.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002893 const bssl::SSL_PROTOCOL_METHOD *method;
Robert Sloana27a6a42017-09-05 08:39:28 -07002894 // x509_method contains pointers to functions that might deal with |X509|
2895 // compatibility, or might be a no-op, depending on the application.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002896 const bssl::SSL_X509_METHOD *x509_method;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002897};
2898
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002899struct ssl_ctx_st {
2900 explicit ssl_ctx_st(const SSL_METHOD *ssl_method);
2901 ssl_ctx_st(const ssl_ctx_st &) = delete;
2902 ssl_ctx_st &operator=(const ssl_ctx_st &) = delete;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002903
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002904 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
2905 const bssl::SSL_X509_METHOD *x509_method = nullptr;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002906
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002907 // lock is used to protect various operations on this object.
2908 CRYPTO_MUTEX lock;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002909
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002910 // conf_max_version is the maximum acceptable protocol version configured by
2911 // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
2912 // and is further constrainted by |SSL_OP_NO_*|.
2913 uint16_t conf_max_version = 0;
2914
2915 // conf_min_version is the minimum acceptable protocol version configured by
2916 // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
2917 // and is further constrainted by |SSL_OP_NO_*|.
2918 uint16_t conf_min_version = 0;
2919
Robert Sloancbf5ea62018-11-05 11:56:34 -08002920 // quic_method is the method table corresponding to the QUIC hooks.
2921 const SSL_QUIC_METHOD *quic_method = nullptr;
2922
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002923 bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list;
2924
2925 X509_STORE *cert_store = nullptr;
2926 LHASH_OF(SSL_SESSION) *sessions = nullptr;
2927 // Most session-ids that will be cached, default is
2928 // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
2929 unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
2930 SSL_SESSION *session_cache_head = nullptr;
2931 SSL_SESSION *session_cache_tail = nullptr;
2932
2933 // handshakes_since_cache_flush is the number of successful handshakes since
2934 // the last cache flush.
2935 int handshakes_since_cache_flush = 0;
2936
2937 // This can have one of 2 values, ored together,
2938 // SSL_SESS_CACHE_CLIENT,
2939 // SSL_SESS_CACHE_SERVER,
2940 // Default is SSL_SESSION_CACHE_SERVER, which means only
2941 // SSL_accept which cache SSL_SESSIONS.
2942 int session_cache_mode = SSL_SESS_CACHE_SERVER;
2943
2944 // session_timeout is the default lifetime for new sessions in TLS 1.2 and
2945 // earlier, in seconds.
2946 uint32_t session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
2947
2948 // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
2949 // 1.3, in seconds.
2950 uint32_t session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
2951
2952 // If this callback is not null, it will be called each time a session id is
2953 // added to the cache. If this function returns 1, it means that the
2954 // callback will do a SSL_SESSION_free() when it has finished using it.
2955 // Otherwise, on 0, it means the callback has finished with it. If
2956 // remove_session_cb is not null, it will be called when a session-id is
2957 // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
2958 // it.
2959 int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess) = nullptr;
2960 void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess) = nullptr;
2961 SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
2962 int *copy) = nullptr;
2963
2964 CRYPTO_refcount_t references = 1;
2965
2966 // if defined, these override the X509_verify_cert() calls
2967 int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg) = nullptr;
2968 void *app_verify_arg = nullptr;
2969
2970 ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
2971 uint8_t *out_alert) = nullptr;
2972
2973 // Default password callback.
2974 pem_password_cb *default_passwd_callback = nullptr;
2975
2976 // Default password callback user data.
2977 void *default_passwd_callback_userdata = nullptr;
2978
2979 // get client cert callback
Robert Sloand9e572d2018-08-27 12:27:00 -07002980 int (*client_cert_cb)(SSL *ssl, X509 **out_x509,
2981 EVP_PKEY **out_pkey) = nullptr;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002982
2983 // get channel id callback
2984 void (*channel_id_cb)(SSL *ssl, EVP_PKEY **out_pkey) = nullptr;
2985
2986 CRYPTO_EX_DATA ex_data;
2987
2988 // Default values used when no per-SSL value is defined follow
2989
2990 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
2991
2992 // what we put in client cert requests
2993 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA;
2994
2995 // cached_x509_client_CA is a cache of parsed versions of the elements of
2996 // |client_CA|.
2997 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
2998
2999
3000 // Default values to use in SSL structures follow (these are copied by
3001 // SSL_new)
3002
3003 uint32_t options = 0;
3004 // Disable the auto-chaining feature by default. wpa_supplicant relies on this
3005 // feature, but require callers opt into it.
3006 uint32_t mode = SSL_MODE_NO_AUTO_CHAIN;
3007 uint32_t max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
3008
3009 bssl::UniquePtr<bssl::CERT> cert;
3010
3011 // callback that allows applications to peek at protocol messages
3012 void (*msg_callback)(int write_p, int version, int content_type,
Robert Sloand9e572d2018-08-27 12:27:00 -07003013 const void *buf, size_t len, SSL *ssl,
3014 void *arg) = nullptr;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003015 void *msg_callback_arg = nullptr;
3016
3017 int verify_mode = SSL_VERIFY_NONE;
3018 int (*default_verify_callback)(int ok, X509_STORE_CTX *ctx) =
3019 nullptr; // called 'verify_callback' in the SSL
3020
3021 X509_VERIFY_PARAM *param = nullptr;
3022
3023 // select_certificate_cb is called before most ClientHello processing and
3024 // before the decision whether to resume a session is made. See
3025 // |ssl_select_cert_result_t| for details of the return values.
3026 ssl_select_cert_result_t (*select_certificate_cb)(const SSL_CLIENT_HELLO *) =
3027 nullptr;
3028
3029 // dos_protection_cb is called once the resumption decision for a ClientHello
3030 // has been made. It returns one to continue the handshake or zero to
3031 // abort.
Robert Sloand9e572d2018-08-27 12:27:00 -07003032 int (*dos_protection_cb)(const SSL_CLIENT_HELLO *) = nullptr;
3033
3034 // Controls whether to verify certificates when resuming connections. They
3035 // were already verified when the connection was first made, so the default is
3036 // false. For now, this is only respected on clients, not servers.
3037 bool reverify_on_resume = false;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003038
3039 // Maximum amount of data to send in one fragment. actual record size can be
3040 // more than this due to padding and MAC overheads.
3041 uint16_t max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
3042
3043 // TLS extensions servername callback
3044 int (*servername_callback)(SSL *, int *, void *) = nullptr;
3045 void *servername_arg = nullptr;
3046
3047 // RFC 4507 session ticket keys. |ticket_key_current| may be NULL before the
3048 // first handshake and |ticket_key_prev| may be NULL at any time.
3049 // Automatically generated ticket keys are rotated as needed at handshake
3050 // time. Hence, all access must be synchronized through |lock|.
3051 bssl::UniquePtr<bssl::TicketKey> ticket_key_current;
3052 bssl::UniquePtr<bssl::TicketKey> ticket_key_prev;
3053
3054 // Callback to support customisation of ticket key setting
3055 int (*ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
3056 EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) = nullptr;
3057
3058 // Server-only: psk_identity_hint is the default identity hint to send in
3059 // PSK-based key exchanges.
3060 bssl::UniquePtr<char> psk_identity_hint;
3061
3062 unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity,
3063 unsigned max_identity_len, uint8_t *psk,
3064 unsigned max_psk_len) = nullptr;
3065 unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk,
3066 unsigned max_psk_len) = nullptr;
3067
3068
3069 // Next protocol negotiation information
3070 // (for experimental NPN extension).
3071
3072 // For a server, this contains a callback function by which the set of
3073 // advertised protocols can be provided.
3074 int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
3075 unsigned *out_len, void *arg) = nullptr;
3076 void *next_protos_advertised_cb_arg = nullptr;
3077 // For a client, this contains a callback function that selects the
3078 // next protocol from the list provided by the server.
3079 int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
3080 const uint8_t *in, unsigned in_len,
3081 void *arg) = nullptr;
3082 void *next_proto_select_cb_arg = nullptr;
3083
3084 // ALPN information
3085 // (we are in the process of transitioning from NPN to ALPN.)
3086
3087 // For a server, this contains a callback function that allows the
3088 // server to select the protocol for the connection.
3089 // out: on successful return, this must point to the raw protocol
3090 // name (without the length prefix).
3091 // outlen: on successful return, this contains the length of |*out|.
3092 // in: points to the client's list of supported protocols in
3093 // wire-format.
3094 // inlen: the length of |in|.
3095 int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
3096 const uint8_t *in, unsigned in_len,
3097 void *arg) = nullptr;
3098 void *alpn_select_cb_arg = nullptr;
3099
3100 // For a client, this contains the list of supported protocols in wire
3101 // format.
3102 bssl::Array<uint8_t> alpn_client_proto_list;
3103
3104 // SRTP profiles we are willing to do from RFC 5764
3105 bssl::UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles;
3106
3107 // Defined compression algorithms for certificates.
3108 bssl::UniquePtr<STACK_OF(CertCompressionAlg)> cert_compression_algs;
3109
3110 // Supported group values inherited by SSL structure
3111 bssl::Array<uint16_t> supported_group_list;
3112
3113 // The client's Channel ID private key.
3114 bssl::UniquePtr<EVP_PKEY> channel_id_private;
3115
3116 // keylog_callback, if not NULL, is the key logging callback. See
3117 // |SSL_CTX_set_keylog_callback|.
3118 void (*keylog_callback)(const SSL *ssl, const char *line) = nullptr;
3119
3120 // current_time_cb, if not NULL, is the function to use to get the current
3121 // time. It sets |*out_clock| to the current time. The |ssl| argument is
3122 // always NULL. See |SSL_CTX_set_current_time_cb|.
3123 void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock) = nullptr;
3124
3125 // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
3126 // memory.
3127 CRYPTO_BUFFER_POOL *pool = nullptr;
3128
3129 // ticket_aead_method contains function pointers for opening and sealing
3130 // session tickets.
3131 const SSL_TICKET_AEAD_METHOD *ticket_aead_method = nullptr;
3132
3133 // legacy_ocsp_callback implements an OCSP-related callback for OpenSSL
3134 // compatibility.
3135 int (*legacy_ocsp_callback)(SSL *ssl, void *arg) = nullptr;
3136 void *legacy_ocsp_callback_arg = nullptr;
3137
3138 // verify_sigalgs, if not empty, is the set of signature algorithms
3139 // accepted from the peer in decreasing order of preference.
3140 bssl::Array<uint16_t> verify_sigalgs;
3141
3142 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
3143 // hash of the peer's certificate and then discard it to save memory and
3144 // session space. Only effective on the server side.
Robert Sloand9e572d2018-08-27 12:27:00 -07003145 bool retain_only_sha256_of_client_certs : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003146
3147 // quiet_shutdown is true if the connection should not send a close_notify on
3148 // shutdown.
Robert Sloand9e572d2018-08-27 12:27:00 -07003149 bool quiet_shutdown : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003150
3151 // ocsp_stapling_enabled is only used by client connections and indicates
3152 // whether OCSP stapling will be requested.
Robert Sloand9e572d2018-08-27 12:27:00 -07003153 bool ocsp_stapling_enabled : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003154
3155 // If true, a client will request certificate timestamps.
Robert Sloand9e572d2018-08-27 12:27:00 -07003156 bool signed_cert_timestamps_enabled : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003157
3158 // channel_id_enabled is whether Channel ID is enabled. For a server, means
3159 // that we'll accept Channel IDs from clients. For a client, means that we'll
3160 // advertise support.
Robert Sloand9e572d2018-08-27 12:27:00 -07003161 bool channel_id_enabled : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003162
3163 // grease_enabled is whether draft-davidben-tls-grease-01 is enabled.
Robert Sloand9e572d2018-08-27 12:27:00 -07003164 bool grease_enabled : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003165
3166 // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN
3167 // protocols from the peer.
Robert Sloand9e572d2018-08-27 12:27:00 -07003168 bool allow_unknown_alpn_protos : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003169
3170 // ed25519_enabled is whether Ed25519 is advertised in the handshake.
Robert Sloand9e572d2018-08-27 12:27:00 -07003171 bool ed25519_enabled : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003172
3173 // rsa_pss_rsae_certs_enabled is whether rsa_pss_rsae_* are supported by the
3174 // certificate verifier.
Robert Sloand9e572d2018-08-27 12:27:00 -07003175 bool rsa_pss_rsae_certs_enabled : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003176
3177 // false_start_allowed_without_alpn is whether False Start (if
3178 // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN.
Robert Sloand9e572d2018-08-27 12:27:00 -07003179 bool false_start_allowed_without_alpn : 1;
3180
3181 // ignore_tls13_downgrade is whether a connection should continue when the
3182 // server random signals a downgrade.
3183 bool ignore_tls13_downgrade:1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003184
3185 // handoff indicates that a server should stop after receiving the
3186 // ClientHello and pause the handshake in such a way that |SSL_get_error|
3187 // returns |SSL_HANDOFF|.
Robert Sloand9e572d2018-08-27 12:27:00 -07003188 bool handoff : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003189
3190 // If enable_early_data is true, early data can be sent and accepted.
3191 bool enable_early_data : 1;
3192
Pete Bentley0c61efe2019-08-13 09:32:23 +01003193 // pq_experiment_signal indicates that an empty extension should be sent
3194 // (for clients) or echoed (for servers) to indicate participation in an
3195 // experiment of post-quantum key exchanges.
3196 bool pq_experiment_signal : 1;
3197
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003198 private:
3199 ~ssl_ctx_st();
3200 friend void SSL_CTX_free(SSL_CTX *);
Robert Sloanb6d070c2017-07-24 08:40:01 -07003201};
3202
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003203struct ssl_st {
3204 explicit ssl_st(SSL_CTX *ctx_arg);
3205 ssl_st(const ssl_st &) = delete;
3206 ssl_st &operator=(const ssl_st &) = delete;
3207 ~ssl_st();
3208
3209 // method is the method table corresponding to the current protocol (DTLS or
3210 // TLS).
3211 const bssl::SSL_PROTOCOL_METHOD *method = nullptr;
3212
3213 // config is a container for handshake configuration. Accesses to this field
3214 // should check for nullptr, since configuration may be shed after the
3215 // handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use
3216 // that instead, and skip the null check.)
3217 bssl::UniquePtr<bssl::SSL_CONFIG> config;
3218
3219 // version is the protocol version.
3220 uint16_t version = 0;
3221
3222 uint16_t max_send_fragment = 0;
3223
3224 // There are 2 BIO's even though they are normally both the same. This is so
3225 // data can be read and written to different handlers
3226
3227 bssl::UniquePtr<BIO> rbio; // used by SSL_read
3228 bssl::UniquePtr<BIO> wbio; // used by SSL_write
3229
3230 // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
3231 // Otherwise, it returns a value corresponding to what operation is needed to
3232 // progress.
3233 bssl::ssl_hs_wait_t (*do_handshake)(bssl::SSL_HANDSHAKE *hs) = nullptr;
3234
3235 bssl::SSL3_STATE *s3 = nullptr; // TLS variables
3236 bssl::DTLS1_STATE *d1 = nullptr; // DTLS variables
3237
3238 // callback that allows applications to peek at protocol messages
3239 void (*msg_callback)(int write_p, int version, int content_type,
3240 const void *buf, size_t len, SSL *ssl,
3241 void *arg) = nullptr;
3242 void *msg_callback_arg = nullptr;
3243
3244 // session info
3245
3246 // initial_timeout_duration_ms is the default DTLS timeout duration in
3247 // milliseconds. It's used to initialize the timer any time it's restarted.
3248 //
3249 // RFC 6347 states that implementations SHOULD use an initial timer value of 1
3250 // second.
3251 unsigned initial_timeout_duration_ms = 1000;
3252
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003253 // session is the configured session to be offered by the client. This session
3254 // is immutable.
3255 bssl::UniquePtr<SSL_SESSION> session;
3256
3257 void (*info_callback)(const SSL *ssl, int type, int value) = nullptr;
3258
3259 bssl::UniquePtr<SSL_CTX> ctx;
3260
3261 // session_ctx is the |SSL_CTX| used for the session cache and related
3262 // settings.
3263 bssl::UniquePtr<SSL_CTX> session_ctx;
3264
3265 // extra application data
3266 CRYPTO_EX_DATA ex_data;
3267
3268 uint32_t options = 0; // protocol behaviour
3269 uint32_t mode = 0; // API behaviour
3270 uint32_t max_cert_list = 0;
3271 bssl::UniquePtr<char> hostname;
3272
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003273 // quic_method is the method table corresponding to the QUIC hooks.
3274 const SSL_QUIC_METHOD *quic_method = nullptr;
3275
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003276 // renegotiate_mode controls how peer renegotiation attempts are handled.
3277 ssl_renegotiate_mode_t renegotiate_mode = ssl_renegotiate_never;
3278
3279 // server is true iff the this SSL* is the server half. Note: before the SSL*
3280 // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
3281 // the side is not determined. In this state, server is always false.
3282 bool server : 1;
3283
3284 // quiet_shutdown is true if the connection should not send a close_notify on
3285 // shutdown.
3286 bool quiet_shutdown : 1;
3287
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003288 // If enable_early_data is true, early data can be sent and accepted.
3289 bool enable_early_data : 1;
3290};
3291
3292struct ssl_session_st {
3293 explicit ssl_session_st(const bssl::SSL_X509_METHOD *method);
3294 ssl_session_st(const ssl_session_st &) = delete;
3295 ssl_session_st &operator=(const ssl_session_st &) = delete;
3296
3297 CRYPTO_refcount_t references = 1;
Robert Sloand9e572d2018-08-27 12:27:00 -07003298
3299 // ssl_version is the (D)TLS version that established the session.
3300 uint16_t ssl_version = 0;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003301
3302 // group_id is the ID of the ECDH group used to establish this session or zero
3303 // if not applicable or unknown.
3304 uint16_t group_id = 0;
3305
3306 // peer_signature_algorithm is the signature algorithm used to authenticate
3307 // the peer, or zero if not applicable or unknown.
3308 uint16_t peer_signature_algorithm = 0;
3309
3310 // master_key, in TLS 1.2 and below, is the master secret associated with the
3311 // session. In TLS 1.3 and up, it is the resumption secret.
3312 int master_key_length = 0;
3313 uint8_t master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
3314
3315 // session_id - valid?
3316 unsigned session_id_length = 0;
3317 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
3318 // this is used to determine whether the session is being reused in
3319 // the appropriate context. It is up to the application to set this,
3320 // via SSL_new
3321 uint8_t sid_ctx_length = 0;
3322 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
3323
3324 bssl::UniquePtr<char> psk_identity;
3325
3326 // certs contains the certificate chain from the peer, starting with the leaf
3327 // certificate.
3328 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs;
3329
3330 const bssl::SSL_X509_METHOD *x509_method = nullptr;
3331
3332 // x509_peer is the peer's certificate.
3333 X509 *x509_peer = nullptr;
3334
3335 // x509_chain is the certificate chain sent by the peer. NOTE: for historical
3336 // reasons, when a client (so the peer is a server), the chain includes
3337 // |peer|, but when a server it does not.
3338 STACK_OF(X509) *x509_chain = nullptr;
3339
3340 // x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that
3341 // omits the leaf certificate. This exists because OpenSSL, historically,
3342 // didn't include the leaf certificate in the chain for a server, but did for
3343 // a client. The |x509_chain| always includes it and, if an API call requires
3344 // a chain without, it is stored here.
3345 STACK_OF(X509) *x509_chain_without_leaf = nullptr;
3346
3347 // verify_result is the result of certificate verification in the case of
3348 // non-fatal certificate errors.
3349 long verify_result = X509_V_ERR_INVALID_CALL;
3350
3351 // timeout is the lifetime of the session in seconds, measured from |time|.
3352 // This is renewable up to |auth_timeout|.
3353 uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT;
3354
3355 // auth_timeout is the non-renewable lifetime of the session in seconds,
3356 // measured from |time|.
3357 uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
3358
3359 // time is the time the session was issued, measured in seconds from the UNIX
3360 // epoch.
3361 uint64_t time = 0;
3362
3363 const SSL_CIPHER *cipher = nullptr;
3364
3365 CRYPTO_EX_DATA ex_data; // application specific data
3366
3367 // These are used to make removal of session-ids more efficient and to
3368 // implement a maximum cache size.
3369 SSL_SESSION *prev = nullptr, *next = nullptr;
3370
3371 bssl::Array<uint8_t> ticket;
3372
3373 bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
3374
3375 // The OCSP response that came with the session.
3376 bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response;
3377
3378 // peer_sha256 contains the SHA-256 hash of the peer's certificate if
3379 // |peer_sha256_valid| is true.
3380 uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0};
3381
3382 // original_handshake_hash contains the handshake hash (either SHA-1+MD5 or
3383 // SHA-2, depending on TLS version) for the original, full handshake that
3384 // created a session. This is used by Channel IDs during resumption.
3385 uint8_t original_handshake_hash[EVP_MAX_MD_SIZE] = {0};
3386 uint8_t original_handshake_hash_len = 0;
3387
3388 uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds
3389
3390 uint32_t ticket_age_add = 0;
3391
3392 // ticket_max_early_data is the maximum amount of data allowed to be sent as
3393 // early data. If zero, 0-RTT is disallowed.
3394 uint32_t ticket_max_early_data = 0;
3395
3396 // early_alpn is the ALPN protocol from the initial handshake. This is only
3397 // stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT
3398 // resumptions.
3399 bssl::Array<uint8_t> early_alpn;
3400
3401 // extended_master_secret is whether the master secret in this session was
3402 // generated using EMS and thus isn't vulnerable to the Triple Handshake
3403 // attack.
Robert Sloand9e572d2018-08-27 12:27:00 -07003404 bool extended_master_secret : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003405
3406 // peer_sha256_valid is whether |peer_sha256| is valid.
Robert Sloand9e572d2018-08-27 12:27:00 -07003407 bool peer_sha256_valid : 1; // Non-zero if peer_sha256 is valid
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003408
3409 // not_resumable is used to indicate that session resumption is disallowed.
Robert Sloand9e572d2018-08-27 12:27:00 -07003410 bool not_resumable : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003411
3412 // ticket_age_add_valid is whether |ticket_age_add| is valid.
Robert Sloand9e572d2018-08-27 12:27:00 -07003413 bool ticket_age_add_valid : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003414
3415 // is_server is whether this session was created by a server.
Robert Sloand9e572d2018-08-27 12:27:00 -07003416 bool is_server : 1;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003417
3418 private:
3419 ~ssl_session_st();
3420 friend void SSL_SESSION_free(SSL_SESSION *);
3421};
Robert Sloanb6d070c2017-07-24 08:40:01 -07003422
Robert Sloanb6d070c2017-07-24 08:40:01 -07003423
Robert Sloana27a6a42017-09-05 08:39:28 -07003424#endif // OPENSSL_HEADER_SSL_INTERNAL_H