blob: 52320f28de41b9c0ede9442f23f7cb9e6f453e02 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE.
140 */
141
David Benjamin2ee94aa2015-04-07 22:38:30 -0400142#ifndef OPENSSL_HEADER_SSL_INTERNAL_H
143#define OPENSSL_HEADER_SSL_INTERNAL_H
Adam Langley95c29f32014-06-20 12:00:00 -0700144
145#include <openssl/base.h>
146
David Benjaminc9376992017-07-24 15:31:13 -0400147#include <stdlib.h>
148
David Benjamin499742c2017-07-22 12:45:38 -0400149#include <limits>
David Benjamind272dea2017-07-29 11:44:22 -0400150#include <new>
David Benjamincfc11c22017-07-18 22:45:18 -0400151#include <type_traits>
152#include <utility>
153
Adam Langleyc9fb3752014-06-20 12:00:00 -0700154#include <openssl/aead.h>
David Benjamincfc11c22017-07-18 22:45:18 -0400155#include <openssl/err.h>
David Benjamin4e840352017-10-11 18:41:11 -0400156#include <openssl/lhash.h>
David Benjamincfc11c22017-07-18 22:45:18 -0400157#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700158#include <openssl/ssl.h>
David Benjamin499742c2017-07-22 12:45:38 -0400159#include <openssl/span.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/stack.h>
161
David Benjamine52f4c42017-10-01 22:35:10 -0400162#include "../crypto/err/internal.h"
David Benjamin08f5c762017-09-21 02:43:05 -0400163#include "../crypto/internal.h"
164
Steven Valdezcb966542016-08-17 16:56:14 -0400165
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400166#if defined(OPENSSL_WINDOWS)
David Benjaminc11ea9422017-08-29 16:33:21 -0400167// Windows defines struct timeval in winsock2.h.
David Benjamina353cdb2016-06-09 16:48:33 -0400168OPENSSL_MSVC_PRAGMA(warning(push, 3))
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400169#include <winsock2.h>
David Benjamina353cdb2016-06-09 16:48:33 -0400170OPENSSL_MSVC_PRAGMA(warning(pop))
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400171#else
David Benjamin0abd6f22015-12-04 21:49:53 -0500172#include <sys/time.h>
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400173#endif
174
Steven Valdez87eab492016-06-27 16:34:59 -0400175
David Benjamin86e95b82017-07-18 16:34:25 -0400176namespace bssl {
Adam Langleyfcf25832014-12-18 17:42:32 -0800177
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700178struct SSL_CONFIG;
David Benjamin86e95b82017-07-18 16:34:25 -0400179struct SSL_HANDSHAKE;
David Benjamin664e99a2017-10-10 19:23:21 -0400180struct SSL_PROTOCOL_METHOD;
David Benjamin0363de92018-06-29 13:57:38 -0400181struct SSL_X509_METHOD;
Steven Valdez8f36c512017-06-20 10:55:02 -0400182
David Benjaminc11ea9422017-08-29 16:33:21 -0400183// C++ utilities.
David Benjamincfc11c22017-07-18 22:45:18 -0400184
David Benjaminc11ea9422017-08-29 16:33:21 -0400185// New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It
186// returns nullptr on allocation error. It only implements single-object
187// allocation and not new T[n].
188//
189// Note: unlike |new|, this does not support non-public constructors.
David Benjamincfc11c22017-07-18 22:45:18 -0400190template <typename T, typename... Args>
191T *New(Args &&... args) {
David Benjamina4cb62f2017-07-31 13:52:31 -0400192 void *t = OPENSSL_malloc(sizeof(T));
David Benjamincfc11c22017-07-18 22:45:18 -0400193 if (t == nullptr) {
194 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
195 return nullptr;
196 }
David Benjamina4cb62f2017-07-31 13:52:31 -0400197 return new (t) T(std::forward<Args>(args)...);
David Benjamincfc11c22017-07-18 22:45:18 -0400198}
199
David Benjaminc11ea9422017-08-29 16:33:21 -0400200// Delete behaves like |delete| but uses |OPENSSL_free| to release memory.
201//
202// Note: unlike |delete| this does not support non-public destructors.
David Benjamincfc11c22017-07-18 22:45:18 -0400203template <typename T>
204void Delete(T *t) {
205 if (t != nullptr) {
206 t->~T();
207 OPENSSL_free(t);
208 }
209}
210
David Benjaminc11ea9422017-08-29 16:33:21 -0400211// All types with kAllowUniquePtr set may be used with UniquePtr. Other types
212// may be C structs which require a |BORINGSSL_MAKE_DELETER| registration.
David Benjamincfc11c22017-07-18 22:45:18 -0400213namespace internal {
214template <typename T>
David Benjamin9a892502017-07-25 22:26:44 -0400215struct DeleterImpl<T, typename std::enable_if<T::kAllowUniquePtr>::type> {
David Benjamincfc11c22017-07-18 22:45:18 -0400216 static void Free(T *t) { Delete(t); }
217};
218}
219
David Benjaminc11ea9422017-08-29 16:33:21 -0400220// MakeUnique behaves like |std::make_unique| but returns nullptr on allocation
221// error.
David Benjamincfc11c22017-07-18 22:45:18 -0400222template <typename T, typename... Args>
223UniquePtr<T> MakeUnique(Args &&... args) {
224 return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
225}
226
David Benjamin506be382017-07-25 15:18:18 -0400227#if defined(BORINGSSL_ALLOW_CXX_RUNTIME)
228#define HAS_VIRTUAL_DESTRUCTOR
229#define PURE_VIRTUAL = 0
230#else
David Benjamin35368092017-08-29 16:55:10 -0400231// HAS_VIRTUAL_DESTRUCTOR should be declared in any base class which defines a
David Benjaminc11ea9422017-08-29 16:33:21 -0400232// virtual destructor. This avoids a dependency on |_ZdlPv| and prevents the
233// class from being used with |delete|.
David Benjaminc9376992017-07-24 15:31:13 -0400234#define HAS_VIRTUAL_DESTRUCTOR \
235 void operator delete(void *) { abort(); }
236
David Benjaminc11ea9422017-08-29 16:33:21 -0400237// PURE_VIRTUAL should be used instead of = 0 when defining pure-virtual
238// functions. This avoids a dependency on |__cxa_pure_virtual| but loses
239// compile-time checking.
David Benjaminc9376992017-07-24 15:31:13 -0400240#define PURE_VIRTUAL { abort(); }
David Benjamin506be382017-07-25 15:18:18 -0400241#endif
David Benjaminc9376992017-07-24 15:31:13 -0400242
Bruce Dawsone05b72c2017-10-10 12:56:54 -0700243// CONSTEXPR_ARRAY works around a VS 2015 bug where ranged for loops don't work
244// on constexpr arrays.
245#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1910
246#define CONSTEXPR_ARRAY const
247#else
248#define CONSTEXPR_ARRAY constexpr
249#endif
250
David Benjamin499742c2017-07-22 12:45:38 -0400251// Array<T> is an owning array of elements of |T|.
252template <typename T>
253class Array {
254 public:
255 // Array's default constructor creates an empty array.
256 Array() {}
257 Array(const Array &) = delete;
258 Array(Array &&other) { *this = std::move(other); }
259
260 ~Array() { Reset(); }
261
262 Array &operator=(const Array &) = delete;
263 Array &operator=(Array &&other) {
264 Reset();
265 other.Release(&data_, &size_);
266 return *this;
267 }
268
269 const T *data() const { return data_; }
270 T *data() { return data_; }
271 size_t size() const { return size_; }
David Benjaminb9493552017-09-27 19:02:51 -0400272 bool empty() const { return size_ == 0; }
David Benjamin499742c2017-07-22 12:45:38 -0400273
274 const T &operator[](size_t i) const { return data_[i]; }
275 T &operator[](size_t i) { return data_[i]; }
276
277 T *begin() { return data_; }
278 const T *cbegin() const { return data_; }
279 T *end() { return data_ + size_; }
280 const T *cend() const { return data_ + size_; }
281
282 void Reset() { Reset(nullptr, 0); }
283
284 // Reset releases the current contents of the array and takes ownership of the
285 // raw pointer supplied by the caller.
286 void Reset(T *new_data, size_t new_size) {
287 for (size_t i = 0; i < size_; i++) {
288 data_[i].~T();
289 }
290 OPENSSL_free(data_);
291 data_ = new_data;
292 size_ = new_size;
293 }
294
295 // Release releases ownership of the array to a raw pointer supplied by the
296 // caller.
297 void Release(T **out, size_t *out_size) {
298 *out = data_;
299 *out_size = size_;
300 data_ = nullptr;
301 size_ = 0;
302 }
303
304 // Init replaces the array with a newly-allocated array of |new_size|
305 // default-constructed copies of |T|. It returns true on success and false on
306 // error.
307 //
308 // Note that if |T| is a primitive type like |uint8_t|, it is uninitialized.
309 bool Init(size_t new_size) {
310 Reset();
311 if (new_size == 0) {
312 return true;
313 }
314
315 if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) {
316 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
317 return false;
318 }
319 data_ = reinterpret_cast<T*>(OPENSSL_malloc(new_size * sizeof(T)));
320 if (data_ == nullptr) {
321 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
322 return false;
323 }
324 size_ = new_size;
325 for (size_t i = 0; i < size_; i++) {
326 new (&data_[i]) T;
327 }
328 return true;
329 }
330
David Benjamin08f5c762017-09-21 02:43:05 -0400331 // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns
332 // true on success and false on error.
David Benjamine325c3f2018-04-12 16:11:15 -0400333 bool CopyFrom(Span<const T> in) {
David Benjamin08f5c762017-09-21 02:43:05 -0400334 if (!Init(in.size())) {
335 return false;
336 }
David Benjamine325c3f2018-04-12 16:11:15 -0400337 OPENSSL_memcpy(data_, in.data(), sizeof(T) * in.size());
David Benjamin08f5c762017-09-21 02:43:05 -0400338 return true;
339 }
340
David Benjamin499742c2017-07-22 12:45:38 -0400341 private:
342 T *data_ = nullptr;
343 size_t size_ = 0;
344};
345
David Benjamin879efc32017-09-21 11:20:53 -0400346// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
Adam Langleyddb57cf2018-01-26 09:17:53 -0800347OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
David Benjamin879efc32017-09-21 11:20:53 -0400348
David Benjamincfc11c22017-07-18 22:45:18 -0400349
David Benjaminc11ea9422017-08-29 16:33:21 -0400350// Protocol versions.
351//
352// Due to DTLS's historical wire version differences and to support multiple
353// variants of the same protocol during development, we maintain two notions of
354// version.
355//
356// The "version" or "wire version" is the actual 16-bit value that appears on
357// the wire. It uniquely identifies a version and is also used at API
358// boundaries. The set of supported versions differs between TLS and DTLS. Wire
359// versions are opaque values and may not be compared numerically.
360//
361// The "protocol version" identifies the high-level handshake variant being
362// used. DTLS versions map to the corresponding TLS versions. Draft TLS 1.3
363// variants all map to TLS 1.3. Protocol versions are sequential and may be
364// compared numerically.
Steven Valdez8f36c512017-06-20 10:55:02 -0400365
David Benjaminc11ea9422017-08-29 16:33:21 -0400366// ssl_protocol_version_from_wire sets |*out| to the protocol version
David Benjamined9aed12017-09-27 19:24:09 -0400367// corresponding to wire version |version| and returns true. If |version| is not
368// a valid TLS or DTLS version, it returns false.
David Benjaminc11ea9422017-08-29 16:33:21 -0400369//
370// Note this simultaneously handles both DTLS and TLS. Use one of the
371// higher-level functions below for most operations.
David Benjamined9aed12017-09-27 19:24:09 -0400372bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400373
David Benjaminc11ea9422017-08-29 16:33:21 -0400374// ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
375// minimum and maximum enabled protocol versions, respectively.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700376bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
David Benjamined9aed12017-09-27 19:24:09 -0400377 uint16_t *out_max_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400378
David Benjamined9aed12017-09-27 19:24:09 -0400379// ssl_supports_version returns whether |hs| supports |version|.
380bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400381
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700382// ssl_method_supports_version returns whether |method| supports |version|.
383bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
384 uint16_t version);
385
David Benjaminc11ea9422017-08-29 16:33:21 -0400386// ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in
387// decreasing preference order.
David Benjamined9aed12017-09-27 19:24:09 -0400388bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb);
Steven Valdez8f36c512017-06-20 10:55:02 -0400389
David Benjaminc11ea9422017-08-29 16:33:21 -0400390// ssl_negotiate_version negotiates a common version based on |hs|'s preferences
David Benjamined9aed12017-09-27 19:24:09 -0400391// and the peer preference list in |peer_versions|. On success, it returns true
392// and sets |*out_version| to the selected version. Otherwise, it returns false
David Benjaminc11ea9422017-08-29 16:33:21 -0400393// and sets |*out_alert| to an alert to send.
David Benjamined9aed12017-09-27 19:24:09 -0400394bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
395 uint16_t *out_version, const CBS *peer_versions);
Steven Valdez8f36c512017-06-20 10:55:02 -0400396
David Benjamind1e3ce12017-10-06 18:31:15 -0400397// ssl_protocol_version returns |ssl|'s protocol version. It is an error to
David Benjaminc11ea9422017-08-29 16:33:21 -0400398// call this function before the version is determined.
David Benjamind1e3ce12017-10-06 18:31:15 -0400399uint16_t ssl_protocol_version(const SSL *ssl);
Steven Valdez8f36c512017-06-20 10:55:02 -0400400
Steven Valdez861f3842018-03-27 13:15:26 -0400401// ssl_is_draft28 returns whether the version corresponds to a draft28 TLS 1.3
402// variant.
403bool ssl_is_draft28(uint16_t version);
404
David Benjaminc11ea9422017-08-29 16:33:21 -0400405// Cipher suites.
David Benjamin71f07942015-04-08 02:36:59 -0400406
David Benjamin33fc2ba2017-09-13 16:22:58 -0400407} // namespace bssl
408
409struct ssl_cipher_st {
410 // name is the OpenSSL name for the cipher.
411 const char *name;
412 // standard_name is the IETF name for the cipher.
413 const char *standard_name;
414 // id is the cipher suite value bitwise OR-d with 0x03000000.
415 uint32_t id;
416
417 // algorithm_* determine the cipher suite. See constants below for the values.
418 uint32_t algorithm_mkey;
419 uint32_t algorithm_auth;
420 uint32_t algorithm_enc;
421 uint32_t algorithm_mac;
422 uint32_t algorithm_prf;
423};
424
425namespace bssl {
426
David Benjaminc11ea9422017-08-29 16:33:21 -0400427// Bits for |algorithm_mkey| (key exchange algorithm).
David Benjamine64d2c72017-07-12 16:31:08 -0400428#define SSL_kRSA 0x00000001u
429#define SSL_kECDHE 0x00000002u
David Benjaminc11ea9422017-08-29 16:33:21 -0400430// SSL_kPSK is only set for plain PSK, not ECDHE_PSK.
David Benjamine64d2c72017-07-12 16:31:08 -0400431#define SSL_kPSK 0x00000004u
432#define SSL_kGENERIC 0x00000008u
David Benjamin71f07942015-04-08 02:36:59 -0400433
David Benjaminc11ea9422017-08-29 16:33:21 -0400434// Bits for |algorithm_auth| (server authentication).
David Benjamine64d2c72017-07-12 16:31:08 -0400435#define SSL_aRSA 0x00000001u
436#define SSL_aECDSA 0x00000002u
David Benjaminc11ea9422017-08-29 16:33:21 -0400437// SSL_aPSK is set for both PSK and ECDHE_PSK.
David Benjamine64d2c72017-07-12 16:31:08 -0400438#define SSL_aPSK 0x00000004u
439#define SSL_aGENERIC 0x00000008u
David Benjamin71f07942015-04-08 02:36:59 -0400440
David Benjaminc032dfa2016-05-12 14:54:57 -0400441#define SSL_aCERT (SSL_aRSA | SSL_aECDSA)
442
David Benjaminc11ea9422017-08-29 16:33:21 -0400443// Bits for |algorithm_enc| (symmetric encryption).
David Benjamine64d2c72017-07-12 16:31:08 -0400444#define SSL_3DES 0x00000001u
445#define SSL_AES128 0x00000002u
446#define SSL_AES256 0x00000004u
447#define SSL_AES128GCM 0x00000008u
448#define SSL_AES256GCM 0x00000010u
449#define SSL_eNULL 0x00000020u
450#define SSL_CHACHA20POLY1305 0x00000040u
David Benjamin71f07942015-04-08 02:36:59 -0400451
452#define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
453
David Benjaminc11ea9422017-08-29 16:33:21 -0400454// Bits for |algorithm_mac| (symmetric authentication).
David Benjamine64d2c72017-07-12 16:31:08 -0400455#define SSL_SHA1 0x00000001u
David Benjaminc11ea9422017-08-29 16:33:21 -0400456// SSL_AEAD is set for all AEADs.
David Benjamin6e678ee2018-04-16 19:54:42 -0400457#define SSL_AEAD 0x00000002u
David Benjamin71f07942015-04-08 02:36:59 -0400458
David Benjaminc11ea9422017-08-29 16:33:21 -0400459// Bits for |algorithm_prf| (handshake digest).
David Benjaminb0883312015-08-06 09:54:13 -0400460#define SSL_HANDSHAKE_MAC_DEFAULT 0x1
461#define SSL_HANDSHAKE_MAC_SHA256 0x2
462#define SSL_HANDSHAKE_MAC_SHA384 0x4
David Benjamin71f07942015-04-08 02:36:59 -0400463
David Benjamin48b276d2018-04-12 17:37:32 -0400464// An SSLCipherPreferenceList contains a list of SSL_CIPHERs with equal-
465// preference groups. For TLS clients, the groups are moot because the server
466// picks the cipher and groups cannot be expressed on the wire. However, for
467// servers, the equal-preference groups allow the client's preferences to be
468// partially respected. (This only has an effect with
469// SSL_OP_CIPHER_SERVER_PREFERENCE).
470//
471// The equal-preference groups are expressed by grouping SSL_CIPHERs together.
472// All elements of a group have the same priority: no ordering is expressed
473// within a group.
474//
475// The values in |ciphers| are in one-to-one correspondence with
476// |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of
477// bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to
478// indicate that the corresponding SSL_CIPHER is not the last element of a
479// group, or 0 to indicate that it is.
480//
481// For example, if |in_group_flags| contains all zeros then that indicates a
482// traditional, fully-ordered preference. Every SSL_CIPHER is the last element
483// of the group (i.e. they are all in a one-element group).
484//
485// For a more complex example, consider:
486// ciphers: A B C D E F
487// in_group_flags: 1 1 0 0 1 0
488//
489// That would express the following, order:
490//
491// A E
492// B -> D -> F
493// C
494struct SSLCipherPreferenceList {
495 static constexpr bool kAllowUniquePtr = true;
496
497 SSLCipherPreferenceList() = default;
498 ~SSLCipherPreferenceList();
499
500 bool Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers,
501 Span<const bool> in_group_flags);
502
503 UniquePtr<STACK_OF(SSL_CIPHER)> ciphers;
504 bool *in_group_flags = nullptr;
505};
David Benjamin71f07942015-04-08 02:36:59 -0400506
David Benjaminc11ea9422017-08-29 16:33:21 -0400507// ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
508// object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
509// and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
510// respectively. The MAC key length is zero except for legacy block and stream
David Benjaminf4962492017-09-27 19:34:32 -0400511// ciphers. It returns true on success and false on error.
512bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
513 size_t *out_mac_secret_len,
514 size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
515 uint16_t version, int is_dtls);
David Benjamin71f07942015-04-08 02:36:59 -0400516
David Benjaminc11ea9422017-08-29 16:33:21 -0400517// ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and
518// |cipher|.
David Benjaminca9e8f52017-08-09 15:02:34 -0400519const EVP_MD *ssl_get_handshake_digest(uint16_t version,
520 const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400521
David Benjamin5be3a742017-10-28 16:08:58 -0400522// ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a
David Benjamin48b276d2018-04-12 17:37:32 -0400523// newly-allocated |SSLCipherPreferenceList| containing the result. It returns
524// true on success and false on failure. If |strict| is true, nonsense will be
525// rejected. If false, nonsense will be silently ignored. An empty result is
526// considered an error regardless of |strict|.
527bool ssl_create_cipher_list(SSLCipherPreferenceList **out_cipher_list,
528 const char *rule_str, bool strict);
David Benjamin71f07942015-04-08 02:36:59 -0400529
David Benjaminc11ea9422017-08-29 16:33:21 -0400530// ssl_cipher_get_value returns the cipher suite id of |cipher|.
David Benjamina1c90a52015-05-30 17:03:14 -0400531uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
532
David Benjaminc11ea9422017-08-29 16:33:21 -0400533// ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth|
534// values suitable for use with |key| in TLS 1.2 and below.
David Benjamin69522112017-03-28 15:38:29 -0500535uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key);
David Benjamin71f07942015-04-08 02:36:59 -0400536
David Benjaminf4962492017-09-27 19:34:32 -0400537// ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the
538// server and, optionally, the client with a certificate.
539bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400540
David Benjaminf4962492017-09-27 19:34:32 -0400541// ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a
542// ServerKeyExchange message.
David Benjaminc11ea9422017-08-29 16:33:21 -0400543//
David Benjaminf4962492017-09-27 19:34:32 -0400544// This function may return false while still allowing |cipher| an optional
David Benjaminc11ea9422017-08-29 16:33:21 -0400545// ServerKeyExchange. This is the case for plain PSK ciphers.
David Benjaminf4962492017-09-27 19:34:32 -0400546bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
David Benjamin71f07942015-04-08 02:36:59 -0400547
David Benjaminc11ea9422017-08-29 16:33:21 -0400548// ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the
549// length of an encrypted 1-byte record, for use in record-splitting. Otherwise
550// it returns zero.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400551size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
552
David Benjamin71f07942015-04-08 02:36:59 -0400553
David Benjaminc11ea9422017-08-29 16:33:21 -0400554// Transcript layer.
Steven Valdez908ac192017-01-12 13:17:07 -0500555
David Benjaminc11ea9422017-08-29 16:33:21 -0400556// SSLTranscript maintains the handshake transcript as a combination of a
557// buffer and running hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400558class SSLTranscript {
559 public:
560 SSLTranscript();
561 ~SSLTranscript();
562
David Benjaminc11ea9422017-08-29 16:33:21 -0400563 // Init initializes the handshake transcript. If called on an existing
564 // transcript, it resets the transcript and hash. It returns true on success
565 // and false on failure.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400566 bool Init();
567
David Benjaminc11ea9422017-08-29 16:33:21 -0400568 // InitHash initializes the handshake hash based on the PRF and contents of
569 // the handshake transcript. Subsequent calls to |Update| will update the
570 // rolling hash. It returns one on success and zero on failure. It is an error
571 // to call this function after the handshake buffer is released.
David Benjaminca9e8f52017-08-09 15:02:34 -0400572 bool InitHash(uint16_t version, const SSL_CIPHER *cipher);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400573
Steven Valdezcd8470f2017-10-11 12:29:36 -0400574 // UpdateForHelloRetryRequest resets the rolling hash with the
575 // HelloRetryRequest construction. It returns true on success and false on
576 // failure. It is an error to call this function before the handshake buffer
577 // is released.
578 bool UpdateForHelloRetryRequest();
579
580 // CopyHashContext copies the hash context into |ctx| and returns true on
581 // success.
582 bool CopyHashContext(EVP_MD_CTX *ctx);
583
David Benjamin75a1f232017-10-11 17:19:19 -0400584 Span<const uint8_t> buffer() {
585 return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
586 buffer_->length);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400587 }
David Benjamin6dc8bf62017-07-19 16:38:21 -0400588
David Benjaminc11ea9422017-08-29 16:33:21 -0400589 // FreeBuffer releases the handshake buffer. Subsequent calls to
590 // |Update| will not update the handshake buffer.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400591 void FreeBuffer();
592
David Benjaminc11ea9422017-08-29 16:33:21 -0400593 // DigestLen returns the length of the PRF hash.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400594 size_t DigestLen() const;
595
David Benjaminc11ea9422017-08-29 16:33:21 -0400596 // Digest returns the PRF hash. For TLS 1.1 and below, this is
597 // |EVP_md5_sha1|.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400598 const EVP_MD *Digest() const;
599
David Benjaminc11ea9422017-08-29 16:33:21 -0400600 // Update adds |in| to the handshake buffer and handshake hash, whichever is
601 // enabled. It returns true on success and false on failure.
David Benjamin75a1f232017-10-11 17:19:19 -0400602 bool Update(Span<const uint8_t> in);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400603
David Benjaminc11ea9422017-08-29 16:33:21 -0400604 // GetHash writes the handshake hash to |out| which must have room for at
605 // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
606 // the number of bytes written. Otherwise, it returns false.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400607 bool GetHash(uint8_t *out, size_t *out_len);
608
David Benjaminc11ea9422017-08-29 16:33:21 -0400609 // GetFinishedMAC computes the MAC for the Finished message into the bytes
610 // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
611 // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
612 // on failure.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400613 bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
David Benjamin6abaa312017-08-31 01:12:28 -0400614 bool from_server);
David Benjamin6dc8bf62017-07-19 16:38:21 -0400615
616 private:
David Benjaminc11ea9422017-08-29 16:33:21 -0400617 // buffer_, if non-null, contains the handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400618 UniquePtr<BUF_MEM> buffer_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400619 // hash, if initialized with an |EVP_MD|, maintains the handshake hash. For
620 // TLS 1.1 and below, it is the SHA-1 half.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400621 ScopedEVP_MD_CTX hash_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400622 // md5, if initialized with an |EVP_MD|, maintains the MD5 half of the
623 // handshake hash for TLS 1.1 and below.
David Benjamin6dc8bf62017-07-19 16:38:21 -0400624 ScopedEVP_MD_CTX md5_;
David Benjamin86e95b82017-07-18 16:34:25 -0400625};
Steven Valdez908ac192017-01-12 13:17:07 -0500626
David Benjamin2637f3c2017-10-27 00:56:55 -0400627// tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
628// as the secret and |label| as the label. |seed1| and |seed2| are concatenated
629// to form the seed parameter. It returns true on success and false on failure.
630bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
631 Span<const uint8_t> secret, Span<const char> label,
632 Span<const uint8_t> seed1, Span<const uint8_t> seed2);
Steven Valdez908ac192017-01-12 13:17:07 -0500633
634
David Benjaminc11ea9422017-08-29 16:33:21 -0400635// Encryption layer.
David Benjamin31a07792015-03-03 14:20:26 -0500636
David Benjaminc11ea9422017-08-29 16:33:21 -0400637// SSLAEADContext contains information about an AEAD that is being used to
638// encrypt an SSL connection.
David Benjamincfc11c22017-07-18 22:45:18 -0400639class SSLAEADContext {
640 public:
Steven Valdezc7d4d212017-09-11 13:53:08 -0400641 SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
David Benjamincfc11c22017-07-18 22:45:18 -0400642 ~SSLAEADContext();
David Benjamin9a892502017-07-25 22:26:44 -0400643 static constexpr bool kAllowUniquePtr = true;
644
David Benjamincfc11c22017-07-18 22:45:18 -0400645 SSLAEADContext(const SSLAEADContext &&) = delete;
646 SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
647
David Benjaminc11ea9422017-08-29 16:33:21 -0400648 // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400649 static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
David Benjamincfc11c22017-07-18 22:45:18 -0400650
David Benjaminc11ea9422017-08-29 16:33:21 -0400651 // Create creates an |SSLAEADContext| using the supplied key material. It
652 // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
653 // resulting object, depending on |direction|. |version| is the normalized
654 // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
David Benjaminb9493552017-09-27 19:02:51 -0400655 static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
656 uint16_t version, int is_dtls,
657 const SSL_CIPHER *cipher,
658 Span<const uint8_t> enc_key,
659 Span<const uint8_t> mac_key,
660 Span<const uint8_t> fixed_iv);
David Benjamincfc11c22017-07-18 22:45:18 -0400661
Steven Valdezc7d4d212017-09-11 13:53:08 -0400662 // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
663 // cipher, to make version-specific determinations in the record layer prior
664 // to a cipher being selected.
665 void SetVersionIfNullCipher(uint16_t version);
666
667 // ProtocolVersion returns the protocol version associated with this
668 // SSLAEADContext. It can only be called once |version_| has been set to a
669 // valid value.
670 uint16_t ProtocolVersion() const;
671
672 // RecordVersion returns the record version that should be used with this
673 // SSLAEADContext for record construction and crypto.
674 uint16_t RecordVersion() const;
675
David Benjamincfc11c22017-07-18 22:45:18 -0400676 const SSL_CIPHER *cipher() const { return cipher_; }
677
David Benjaminc11ea9422017-08-29 16:33:21 -0400678 // is_null_cipher returns true if this is the null cipher.
David Benjamincfc11c22017-07-18 22:45:18 -0400679 bool is_null_cipher() const { return !cipher_; }
680
David Benjaminc11ea9422017-08-29 16:33:21 -0400681 // ExplicitNonceLen returns the length of the explicit nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400682 size_t ExplicitNonceLen() const;
683
David Benjaminc11ea9422017-08-29 16:33:21 -0400684 // MaxOverhead returns the maximum overhead of calling |Seal|.
David Benjamincfc11c22017-07-18 22:45:18 -0400685 size_t MaxOverhead() const;
686
David Benjaminc11ea9422017-08-29 16:33:21 -0400687 // SuffixLen calculates the suffix length written by |SealScatter| and writes
688 // it to |*out_suffix_len|. It returns true on success and false on error.
689 // |in_len| and |extra_in_len| should equal the argument of the same names
690 // passed to |SealScatter|.
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700691 bool SuffixLen(size_t *out_suffix_len, size_t in_len,
692 size_t extra_in_len) const;
David Benjamincfc11c22017-07-18 22:45:18 -0400693
David Benjamine2ab21d2018-04-04 23:55:06 -0400694 // CiphertextLen calculates the total ciphertext length written by
695 // |SealScatter| and writes it to |*out_len|. It returns true on success and
696 // false on error. |in_len| and |extra_in_len| should equal the argument of
697 // the same names passed to |SealScatter|.
698 bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const;
699
David Benjaminc64d1232017-10-04 18:14:28 -0400700 // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
701 // to the plaintext in |in| and returns true. Otherwise, it returns
702 // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
703 bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
David Benjamine2ab21d2018-04-04 23:55:06 -0400704 const uint8_t seqnum[8], Span<const uint8_t> header,
705 Span<uint8_t> in);
David Benjamincfc11c22017-07-18 22:45:18 -0400706
David Benjaminc11ea9422017-08-29 16:33:21 -0400707 // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
708 // result to |out|. It returns true on success and false on error.
709 //
710 // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
David Benjamincfc11c22017-07-18 22:45:18 -0400711 bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
David Benjamine2ab21d2018-04-04 23:55:06 -0400712 uint16_t record_version, const uint8_t seqnum[8],
713 Span<const uint8_t> header, const uint8_t *in, size_t in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400714
David Benjaminc11ea9422017-08-29 16:33:21 -0400715 // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
716 // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
717 // success and zero on error.
718 //
719 // On successful return, exactly |ExplicitNonceLen| bytes are written to
720 // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
721 // |out_suffix|.
722 //
723 // |extra_in| may point to an additional plaintext buffer. If present,
724 // |extra_in_len| additional bytes are encrypted and authenticated, and the
725 // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
726 // be used to size |out_suffix| accordingly.
727 //
728 // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
729 // alias anything.
David Benjamincfc11c22017-07-18 22:45:18 -0400730 bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400731 uint8_t type, uint16_t record_version,
David Benjamine2ab21d2018-04-04 23:55:06 -0400732 const uint8_t seqnum[8], Span<const uint8_t> header,
733 const uint8_t *in, size_t in_len, const uint8_t *extra_in,
734 size_t extra_in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400735
736 bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
737
738 private:
David Benjamine2ab21d2018-04-04 23:55:06 -0400739 // GetAdditionalData returns the additional data, writing into |storage| if
740 // necessary.
741 Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type,
742 uint16_t record_version,
743 const uint8_t seqnum[8],
744 size_t plaintext_len,
745 Span<const uint8_t> header);
David Benjamincfc11c22017-07-18 22:45:18 -0400746
747 const SSL_CIPHER *cipher_;
748 ScopedEVP_AEAD_CTX ctx_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400749 // fixed_nonce_ contains any bytes of the nonce that are fixed for all
750 // records.
David Benjamincfc11c22017-07-18 22:45:18 -0400751 uint8_t fixed_nonce_[12];
752 uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400753 // version_ is the wire version that should be used with this AEAD.
David Benjamincfc11c22017-07-18 22:45:18 -0400754 uint16_t version_;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400755 // is_dtls_ is whether DTLS is being used with this AEAD.
756 bool is_dtls_;
David Benjaminc11ea9422017-08-29 16:33:21 -0400757 // variable_nonce_included_in_record_ is true if the variable nonce
758 // for a record is included as a prefix before the ciphertext.
David Benjamincfc11c22017-07-18 22:45:18 -0400759 bool variable_nonce_included_in_record_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400760 // random_variable_nonce_ is true if the variable nonce is
761 // randomly generated, rather than derived from the sequence
762 // number.
David Benjamincfc11c22017-07-18 22:45:18 -0400763 bool random_variable_nonce_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400764 // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
765 // variable nonce rather than prepended.
766 bool xor_fixed_nonce_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400767 // omit_length_in_ad_ is true if the length should be omitted in the
768 // AEAD's ad parameter.
David Benjamincfc11c22017-07-18 22:45:18 -0400769 bool omit_length_in_ad_ : 1;
David Benjaminc11ea9422017-08-29 16:33:21 -0400770 // omit_ad_ is true if the AEAD's ad parameter should be omitted.
David Benjamincfc11c22017-07-18 22:45:18 -0400771 bool omit_ad_ : 1;
David Benjamine2ab21d2018-04-04 23:55:06 -0400772 // ad_is_header_ is true if the AEAD's ad parameter is the record header.
773 bool ad_is_header_ : 1;
David Benjamin86e95b82017-07-18 16:34:25 -0400774};
David Benjamin31a07792015-03-03 14:20:26 -0500775
David Benjamin31a07792015-03-03 14:20:26 -0500776
David Benjaminc11ea9422017-08-29 16:33:21 -0400777// DTLS replay bitmap.
David Benjamin7446a3b2015-07-25 17:53:57 -0400778
David Benjaminc11ea9422017-08-29 16:33:21 -0400779// DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
780// replayed packets. It should be initialized by zeroing every field.
David Benjamin86e95b82017-07-18 16:34:25 -0400781struct DTLS1_BITMAP {
David Benjaminc11ea9422017-08-29 16:33:21 -0400782 // map is a bit mask of the last 64 sequence numbers. Bit
783 // |1<<i| corresponds to |max_seq_num - i|.
David Benjamin800046f2017-10-17 01:37:51 -0400784 uint64_t map = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -0400785 // max_seq_num is the largest sequence number seen so far as a 64-bit
786 // integer.
David Benjamin800046f2017-10-17 01:37:51 -0400787 uint64_t max_seq_num = 0;
David Benjamin86e95b82017-07-18 16:34:25 -0400788};
David Benjamin7446a3b2015-07-25 17:53:57 -0400789
790
David Benjaminc11ea9422017-08-29 16:33:21 -0400791// Record layer.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400792
David Benjaminc11ea9422017-08-29 16:33:21 -0400793// ssl_record_sequence_update increments the sequence number in |seq|. It
794// returns one on success and zero on wraparound.
David Benjamin1db21562015-12-25 15:11:39 -0500795int ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
796
David Benjaminc11ea9422017-08-29 16:33:21 -0400797// ssl_record_prefix_len returns the length of the prefix before the ciphertext
798// of a record for |ssl|.
799//
800// TODO(davidben): Expose this as part of public API once the high-level
801// buffer-free APIs are available.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400802size_t ssl_record_prefix_len(const SSL *ssl);
803
804enum ssl_open_record_t {
805 ssl_open_record_success,
806 ssl_open_record_discard,
807 ssl_open_record_partial,
David Benjamin728f3542016-06-02 15:42:01 -0400808 ssl_open_record_close_notify,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400809 ssl_open_record_error,
810};
811
David Benjaminc11ea9422017-08-29 16:33:21 -0400812// tls_open_record decrypts a record from |in| in-place.
813//
814// If the input did not contain a complete record, it returns
815// |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
816// bytes necessary. It is guaranteed that a successful call to |tls_open_record|
817// will consume at least that many bytes.
818//
819// Otherwise, it sets |*out_consumed| to the number of bytes of input
820// consumed. Note that input may be consumed on all return codes if a record was
821// decrypted.
822//
823// On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
824// record type and |*out| to the record body in |in|. Note that |*out| may be
825// empty.
826//
827// If a record was successfully processed but should be discarded, it returns
828// |ssl_open_record_discard|.
829//
David Benjamin751d1a12017-09-25 13:37:30 -0400830// If a record was successfully processed but is a close_notify, it returns
831// |ssl_open_record_close_notify|.
David Benjaminc11ea9422017-08-29 16:33:21 -0400832//
David Benjamin751d1a12017-09-25 13:37:30 -0400833// On failure or fatal alert, it returns |ssl_open_record_error| and sets
834// |*out_alert| to an alert to emit, or zero if no alert should be emitted.
David Benjaminc64d1232017-10-04 18:14:28 -0400835enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
836 Span<uint8_t> *out, size_t *out_consumed,
837 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400838
David Benjamina05d4272017-10-06 19:34:55 -0400839// dtls_open_record implements |tls_open_record| for DTLS. It only returns
840// |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
841// zero. The caller should read one packet and try again.
David Benjaminc64d1232017-10-04 18:14:28 -0400842enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
843 Span<uint8_t> *out,
David Benjamina7810c12016-06-06 18:54:51 -0400844 size_t *out_consumed,
David Benjaminc64d1232017-10-04 18:14:28 -0400845 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400846
David Benjaminc11ea9422017-08-29 16:33:21 -0400847// ssl_seal_align_prefix_len returns the length of the prefix before the start
848// of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
849// use this to align buffers.
850//
851// Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
852// record and is the offset into second record's ciphertext. Thus sealing a
853// small record may result in a smaller output than this value.
854//
855// TODO(davidben): Is this alignment valuable? Record-splitting makes this a
856// mess.
David Benjamin1a01e1f2016-06-08 18:31:24 -0400857size_t ssl_seal_align_prefix_len(const SSL *ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400858
David Benjaminc11ea9422017-08-29 16:33:21 -0400859// tls_seal_record seals a new record of type |type| and body |in| and writes it
860// to |out|. At most |max_out| bytes will be written. It returns one on success
861// and zero on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC 1/n-1
862// record splitting and may write two records concatenated.
863//
864// For a large record, the bulk of the ciphertext will begin
865// |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
866// improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
867// bytes to |out|.
868//
869// |in| and |out| may not alias.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400870int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
871 uint8_t type, const uint8_t *in, size_t in_len);
872
873enum dtls1_use_epoch_t {
874 dtls1_use_previous_epoch,
875 dtls1_use_current_epoch,
876};
877
David Benjaminc11ea9422017-08-29 16:33:21 -0400878// dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
879// record.
David Benjamin1a999cf2017-01-03 10:30:35 -0500880size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
881
David Benjaminc11ea9422017-08-29 16:33:21 -0400882// dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
883// front of the plaintext when sealing a record in-place.
David Benjamina772b162017-01-24 17:51:33 -0500884size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
885
David Benjaminc11ea9422017-08-29 16:33:21 -0400886// dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
887// which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
888// may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
889// ahead of |out|.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400890int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
891 uint8_t type, const uint8_t *in, size_t in_len,
892 enum dtls1_use_epoch_t use_epoch);
893
David Benjaminc11ea9422017-08-29 16:33:21 -0400894// ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
895// state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
896// |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
897// appropriate.
David Benjamin728f3542016-06-02 15:42:01 -0400898enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
David Benjaminc64d1232017-10-04 18:14:28 -0400899 Span<const uint8_t> in);
David Benjamin728f3542016-06-02 15:42:01 -0400900
David Benjaminb8d28cf2015-07-28 21:34:45 -0400901
David Benjaminc11ea9422017-08-29 16:33:21 -0400902// Private key operations.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400903
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700904// ssl_has_private_key returns one if |cfg| has a private key configured and
905// zero otherwise.
906int ssl_has_private_key(const SSL_CONFIG *cfg);
nagendra modadugu601448a2015-07-24 09:31:31 -0700907
David Benjaminc11ea9422017-08-29 16:33:21 -0400908// ssl_private_key_* perform the corresponding operation on
909// |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
910// call the corresponding function or |complete| depending on whether there is a
911// pending operation. Otherwise, they implement the operation with
912// |EVP_PKEY|.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400913
David Benjaminb4d65fd2015-05-29 17:11:21 -0400914enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -0400915 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin75a1f232017-10-11 17:19:19 -0400916 uint16_t sigalg, Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400917
David Benjamin75a1f232017-10-11 17:19:19 -0400918enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
919 uint8_t *out,
920 size_t *out_len,
921 size_t max_out,
922 Span<const uint8_t> in);
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700923
David Benjamin75a1f232017-10-11 17:19:19 -0400924// ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
925// key supports |sigalg|.
926bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
David Benjamin6114c3c2017-03-30 16:37:54 -0500927 uint16_t sigalg);
David Benjamin1fb125c2016-07-08 18:52:12 -0700928
David Benjaminc11ea9422017-08-29 16:33:21 -0400929// ssl_public_key_verify verifies that the |signature| is valid for the public
930// key |pkey| and input |in|, using the signature algorithm |sigalg|.
David Benjamin75a1f232017-10-11 17:19:19 -0400931bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
932 uint16_t sigalg, EVP_PKEY *pkey,
933 Span<const uint8_t> in);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400934
David Benjamin1fb125c2016-07-08 18:52:12 -0700935
David Benjaminc11ea9422017-08-29 16:33:21 -0400936// Custom extensions
Adam Langley09505632015-07-30 18:10:13 -0700937
David Benjamin86e95b82017-07-18 16:34:25 -0400938} // namespace bssl
939
David Benjaminc11ea9422017-08-29 16:33:21 -0400940// |SSL_CUSTOM_EXTENSION| is a structure that contains information about
941// custom-extension callbacks. It is defined unnamespaced for compatibility with
942// |STACK_OF(SSL_CUSTOM_EXTENSION)|.
David Benjamin86e95b82017-07-18 16:34:25 -0400943typedef struct ssl_custom_extension {
Adam Langley09505632015-07-30 18:10:13 -0700944 SSL_custom_ext_add_cb add_callback;
945 void *add_arg;
946 SSL_custom_ext_free_cb free_callback;
947 SSL_custom_ext_parse_cb parse_callback;
948 void *parse_arg;
949 uint16_t value;
David Benjamin86e95b82017-07-18 16:34:25 -0400950} SSL_CUSTOM_EXTENSION;
Adam Langley09505632015-07-30 18:10:13 -0700951
David Benjamin01f8a8c2017-04-15 18:12:55 -0400952DEFINE_STACK_OF(SSL_CUSTOM_EXTENSION)
953
David Benjamin86e95b82017-07-18 16:34:25 -0400954namespace bssl {
955
956void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension);
957
David Benjamin2bd19172016-11-17 16:47:15 +0900958int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions);
959int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
960 uint16_t value, const CBS *extension);
961int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
962 uint16_t value, const CBS *extension);
963int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions);
Adam Langley09505632015-07-30 18:10:13 -0700964
965
David Benjaminc11ea9422017-08-29 16:33:21 -0400966// Key shares.
David Benjamin4298d772015-12-19 00:18:25 -0500967
David Benjaminc11ea9422017-08-29 16:33:21 -0400968// SSLKeyShare abstracts over Diffie-Hellman-like key exchanges.
David Benjaminc642aca2017-07-19 23:28:43 -0400969class SSLKeyShare {
970 public:
971 virtual ~SSLKeyShare() {}
David Benjamin9a892502017-07-25 22:26:44 -0400972 static constexpr bool kAllowUniquePtr = true;
David Benjaminc9376992017-07-24 15:31:13 -0400973 HAS_VIRTUAL_DESTRUCTOR
David Benjaminb9179092016-10-26 13:47:33 -0400974
David Benjaminc11ea9422017-08-29 16:33:21 -0400975 // Create returns a SSLKeyShare instance for use with group |group_id| or
976 // nullptr on error.
David Benjaminc642aca2017-07-19 23:28:43 -0400977 static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
David Benjamin4298d772015-12-19 00:18:25 -0500978
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700979 // Create deserializes an SSLKeyShare instance previously serialized by
980 // |Serialize|.
981 static UniquePtr<SSLKeyShare> Create(CBS *in);
982
David Benjaminc11ea9422017-08-29 16:33:21 -0400983 // GroupID returns the group ID.
David Benjaminc9376992017-07-24 15:31:13 -0400984 virtual uint16_t GroupID() const PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -0500985
David Benjaminc11ea9422017-08-29 16:33:21 -0400986 // Offer generates a keypair and writes the public value to
987 // |out_public_key|. It returns true on success and false on error.
David Benjaminc9376992017-07-24 15:31:13 -0400988 virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL;
David Benjamin4298d772015-12-19 00:18:25 -0500989
David Benjaminc11ea9422017-08-29 16:33:21 -0400990 // Accept performs a key exchange against the |peer_key| generated by |offer|.
991 // On success, it returns true, writes the public value to |out_public_key|,
David Benjamin499742c2017-07-22 12:45:38 -0400992 // and sets |*out_secret| the shared secret. On failure, it returns false and
993 // sets |*out_alert| to an alert to send to the peer.
David Benjaminc11ea9422017-08-29 16:33:21 -0400994 //
995 // The default implementation calls |Offer| and then |Finish|, assuming a key
996 // exchange protocol where the peers are symmetric.
David Benjamin499742c2017-07-22 12:45:38 -0400997 virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
998 uint8_t *out_alert, Span<const uint8_t> peer_key);
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -0700999
David Benjaminc11ea9422017-08-29 16:33:21 -04001000 // Finish performs a key exchange against the |peer_key| generated by
David Benjamin499742c2017-07-22 12:45:38 -04001001 // |Accept|. On success, it returns true and sets |*out_secret| to the shared
1002 // secret. On failure, it returns zero and sets |*out_alert| to an alert to
1003 // send to the peer.
1004 virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
1005 Span<const uint8_t> peer_key) PURE_VIRTUAL;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001006
1007 // Serialize writes the state of the key exchange to |out|, returning true if
1008 // successful and false otherwise.
1009 virtual bool Serialize(CBB *out) { return false; }
1010
1011 // Deserialize initializes the state of the key exchange from |in|, returning
1012 // true if successful and false otherwise. It is called by |Create|.
1013 virtual bool Deserialize(CBS *in) { return false; }
David Benjaminb9179092016-10-26 13:47:33 -04001014};
David Benjamin4298d772015-12-19 00:18:25 -05001015
David Benjaminc11ea9422017-08-29 16:33:21 -04001016// ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
1017// sets |*out_group_id| to the group ID and returns one. Otherwise, it returns
1018// zero.
Steven Valdezce902a92016-05-17 11:47:53 -04001019int ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
David Benjamin4298d772015-12-19 00:18:25 -05001020
David Benjaminc11ea9422017-08-29 16:33:21 -04001021// ssl_name_to_group_id looks up the group corresponding to the |name| string
1022// of length |len|. On success, it sets |*out_group_id| to the group ID and
1023// returns one. Otherwise, it returns zero.
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01001024int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
1025
David Benjamin4298d772015-12-19 00:18:25 -05001026
David Benjaminc11ea9422017-08-29 16:33:21 -04001027// Handshake messages.
David Benjamin060cfb02016-05-12 00:43:05 -04001028
David Benjamin7934f082017-08-01 16:32:25 -04001029struct SSLMessage {
1030 bool is_v2_hello;
1031 uint8_t type;
1032 CBS body;
David Benjaminc11ea9422017-08-29 16:33:21 -04001033 // raw is the entire serialized handshake message, including the TLS or DTLS
1034 // message header.
David Benjamin7934f082017-08-01 16:32:25 -04001035 CBS raw;
1036};
1037
David Benjaminc11ea9422017-08-29 16:33:21 -04001038// SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
1039// ChangeCipherSpec, in the longest handshake flight. Currently this is the
1040// client's second leg in a full handshake when client certificates, NPN, and
1041// Channel ID, are all enabled.
David Benjamin29a83c52016-06-17 19:12:54 -04001042#define SSL_MAX_HANDSHAKE_FLIGHT 7
1043
Steven Valdez964b2372017-11-07 17:09:52 -05001044extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE];
David Benjamin6df65402017-12-18 18:00:23 -05001045extern const uint8_t kDraftDowngradeRandom[8];
Steven Valdez964b2372017-11-07 17:09:52 -05001046
David Benjaminc11ea9422017-08-29 16:33:21 -04001047// ssl_max_handshake_message_len returns the maximum number of bytes permitted
1048// in a handshake message for |ssl|.
David Benjamin060cfb02016-05-12 00:43:05 -04001049size_t ssl_max_handshake_message_len(const SSL *ssl);
1050
David Benjamin33febf62017-10-07 16:52:50 -04001051// tls_can_accept_handshake_data returns whether |ssl| is able to accept more
1052// data into handshake buffer.
1053bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
1054
David Benjamin40e94702017-10-06 18:26:36 -04001055// tls_has_unprocessed_handshake_data returns whether there is buffered
1056// handshake data that has not been consumed by |get_message|.
1057bool tls_has_unprocessed_handshake_data(const SSL *ssl);
1058
1059// dtls_has_unprocessed_handshake_data behaves like
1060// |tls_has_unprocessed_handshake_data| for DTLS.
1061bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
David Benjamin61672812016-07-14 23:10:43 -04001062
David Benjamin700631b2018-05-24 17:17:34 -04001063// tls_flush_pending_hs_data flushes any handshake plaintext data.
1064bool tls_flush_pending_hs_data(SSL *ssl);
1065
David Benjamin86e95b82017-07-18 16:34:25 -04001066struct DTLS_OUTGOING_MESSAGE {
David Benjamin049fdfc2017-10-17 01:12:53 -04001067 DTLS_OUTGOING_MESSAGE() {}
1068 DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
1069 DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
1070 ~DTLS_OUTGOING_MESSAGE() { Clear(); }
1071
1072 void Clear();
1073
1074 uint8_t *data = nullptr;
1075 uint32_t len = 0;
1076 uint16_t epoch = 0;
1077 bool is_ccs = false;
David Benjamin86e95b82017-07-18 16:34:25 -04001078};
David Benjamin29a83c52016-06-17 19:12:54 -04001079
David Benjaminc11ea9422017-08-29 16:33:21 -04001080// dtls_clear_outgoing_messages releases all buffered outgoing messages.
David Benjamin29a83c52016-06-17 19:12:54 -04001081void dtls_clear_outgoing_messages(SSL *ssl);
1082
David Benjamin060cfb02016-05-12 00:43:05 -04001083
David Benjaminc11ea9422017-08-29 16:33:21 -04001084// Callbacks.
David Benjamin4e9cc712016-06-01 20:16:03 -04001085
David Benjaminc11ea9422017-08-29 16:33:21 -04001086// ssl_do_info_callback calls |ssl|'s info callback, if set.
David Benjamin4e9cc712016-06-01 20:16:03 -04001087void ssl_do_info_callback(const SSL *ssl, int type, int value);
1088
David Benjaminc11ea9422017-08-29 16:33:21 -04001089// ssl_do_msg_callback calls |ssl|'s message callback, if set.
David Benjaminc0279992016-09-19 20:15:07 -04001090void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
David Benjaminc64d1232017-10-04 18:14:28 -04001091 Span<const uint8_t> in);
David Benjamin4e9cc712016-06-01 20:16:03 -04001092
1093
David Benjaminc11ea9422017-08-29 16:33:21 -04001094// Transport buffers.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001095
David Benjaminea712e32017-10-13 16:50:39 -04001096class SSLBuffer {
1097 public:
1098 SSLBuffer() {}
1099 ~SSLBuffer() { Clear(); }
1100
1101 SSLBuffer(const SSLBuffer &) = delete;
1102 SSLBuffer &operator=(const SSLBuffer &) = delete;
1103
1104 uint8_t *data() { return buf_ + offset_; }
1105 size_t size() const { return size_; }
1106 bool empty() const { return size_ == 0; }
1107 size_t cap() const { return cap_; }
1108
1109 Span<uint8_t> span() { return MakeSpan(data(), size()); }
1110
1111 Span<uint8_t> remaining() {
1112 return MakeSpan(data() + size(), cap() - size());
1113 }
1114
1115 // Clear releases the buffer.
1116 void Clear();
1117
1118 // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
1119 // that data written after |header_len| is aligned to a
1120 // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
1121 // on error.
1122 bool EnsureCap(size_t header_len, size_t new_cap);
1123
1124 // DidWrite extends the buffer by |len|. The caller must have filled in to
1125 // this point.
1126 void DidWrite(size_t len);
1127
1128 // Consume consumes |len| bytes from the front of the buffer. The memory
1129 // consumed will remain valid until the next call to |DiscardConsumed| or
1130 // |Clear|.
1131 void Consume(size_t len);
1132
1133 // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
1134 // is now empty, it releases memory used by it.
1135 void DiscardConsumed();
1136
1137 private:
1138 // buf_ is the memory allocated for this buffer.
1139 uint8_t *buf_ = nullptr;
1140 // offset_ is the offset into |buf_| which the buffer contents start at.
1141 uint16_t offset_ = 0;
1142 // size_ is the size of the buffer contents from |buf_| + |offset_|.
1143 uint16_t size_ = 0;
1144 // cap_ is how much memory beyond |buf_| + |offset_| is available.
1145 uint16_t cap_ = 0;
1146};
David Benjaminb8d28cf2015-07-28 21:34:45 -04001147
David Benjaminc11ea9422017-08-29 16:33:21 -04001148// ssl_read_buffer_extend_to extends the read buffer to the desired length. For
1149// TLS, it reads to the end of the buffer until the buffer is |len| bytes
1150// long. For DTLS, it reads a new packet and ignores |len|. It returns one on
1151// success, zero on EOF, and a negative number on error.
1152//
1153// It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
1154// non-empty.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001155int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
1156
David Benjaminea712e32017-10-13 16:50:39 -04001157// ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
1158// to a record-processing function. If |ret| is a success or if the caller
1159// should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
1160// 0.
David Benjamind9229f92017-10-06 17:36:20 -04001161int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
1162 size_t consumed, uint8_t alert);
1163
David Benjaminc11ea9422017-08-29 16:33:21 -04001164// ssl_write_buffer_flush flushes the write buffer to the transport. It returns
1165// one on success and <= 0 on error. For DTLS, whether or not the write
1166// succeeds, the write buffer will be cleared.
David Benjaminb8d28cf2015-07-28 21:34:45 -04001167int ssl_write_buffer_flush(SSL *ssl);
1168
David Benjaminb8d28cf2015-07-28 21:34:45 -04001169
David Benjaminc11ea9422017-08-29 16:33:21 -04001170// Certificate functions.
David Benjamin75836432016-06-17 18:48:29 -04001171
David Benjaminc11ea9422017-08-29 16:33:21 -04001172// ssl_has_certificate returns one if a certificate and private key are
1173// configured and zero otherwise.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001174int ssl_has_certificate(const SSL_CONFIG *cfg);
David Benjamin32a66d52016-07-13 22:03:11 -04001175
David Benjaminc11ea9422017-08-29 16:33:21 -04001176// ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
1177// by a TLS Certificate message. On success, it advances |cbs| and returns
1178// true. Otherwise, it returns false and sets |*out_alert| to an alert to send
1179// to the peer.
1180//
1181// If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
1182// the certificate chain and the leaf certificate's public key
1183// respectively. Otherwise, both will be set to nullptr.
1184//
1185// If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
1186// SHA-256 hash of the leaf to |out_leaf_sha256|.
David Benjamine664a532017-07-20 20:19:36 -04001187bool ssl_parse_cert_chain(uint8_t *out_alert,
1188 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
1189 UniquePtr<EVP_PKEY> *out_pubkey,
1190 uint8_t *out_leaf_sha256, CBS *cbs,
1191 CRYPTO_BUFFER_POOL *pool);
David Benjamin5c900c82016-07-13 23:03:26 -04001192
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001193// ssl_add_cert_chain adds |hs->ssl|'s certificate chain to |cbb| in the format
1194// used by a TLS Certificate message. If there is no certificate chain, it emits
1195// an empty certificate list. It returns one on success and zero on error.
1196int ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjamin32a66d52016-07-13 22:03:11 -04001197
David Benjaminc11ea9422017-08-29 16:33:21 -04001198// ssl_cert_check_digital_signature_key_usage parses the DER-encoded, X.509
1199// certificate in |in| and returns one if doesn't specify a key usage or, if it
1200// does, if it includes digitalSignature. Otherwise it pushes to the error
1201// queue and returns zero.
Adam Langley05672202016-12-13 12:05:49 -08001202int ssl_cert_check_digital_signature_key_usage(const CBS *in);
1203
David Benjaminc11ea9422017-08-29 16:33:21 -04001204// ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
1205// certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
1206// nullptr and pushes to the error queue.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001207UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
Adam Langleyd5157222016-12-12 11:37:43 -08001208
David Benjaminc11ea9422017-08-29 16:33:21 -04001209// ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
1210// TLS CertificateRequest message. On success, it returns a newly-allocated
1211// |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
1212// sets |*out_alert| to an alert to send to the peer.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001213UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
1214 uint8_t *out_alert,
1215 CBS *cbs);
David Benjamine0332e82016-07-13 22:40:36 -04001216
Steven Valdezcd8470f2017-10-11 12:29:36 -04001217// ssl_has_client_CAs returns there are configured CAs.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001218bool ssl_has_client_CAs(const SSL_CONFIG *cfg);
Steven Valdezcd8470f2017-10-11 12:29:36 -04001219
David Benjaminc11ea9422017-08-29 16:33:21 -04001220// ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
1221// used by a TLS CertificateRequest message. It returns one on success and zero
1222// on error.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001223int ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb);
David Benjamin32a66d52016-07-13 22:03:11 -04001224
David Benjaminc11ea9422017-08-29 16:33:21 -04001225// ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
1226// a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
1227// an error on the error queue.
David Benjamin45738dd2017-02-09 20:01:26 -05001228int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Adam Langley05672202016-12-13 12:05:49 -08001229 const CRYPTO_BUFFER *leaf);
Steven Valdezbf5aa842016-07-15 07:07:40 -04001230
David Benjaminc11ea9422017-08-29 16:33:21 -04001231// ssl_on_certificate_selected is called once the certificate has been selected.
1232// It finalizes the certificate and initializes |hs->local_pubkey|. It returns
1233// one on success and zero on error.
David Benjamina232a712017-03-30 15:51:53 -05001234int ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
1235
David Benjamin75836432016-06-17 18:48:29 -04001236
David Benjaminc11ea9422017-08-29 16:33:21 -04001237// TLS 1.3 key derivation.
Steven Valdez143e8b32016-07-11 13:19:03 -04001238
David Benjaminc11ea9422017-08-29 16:33:21 -04001239// tls13_init_key_schedule initializes the handshake hash and key derivation
Steven Valdezcd8470f2017-10-11 12:29:36 -04001240// state, and incorporates the PSK. The cipher suite and PRF hash must have been
1241// selected at this point. It returns one on success and zero on error.
1242int tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1243 size_t psk_len);
Steven Valdez143e8b32016-07-11 13:19:03 -04001244
David Benjaminc11ea9422017-08-29 16:33:21 -04001245// tls13_init_early_key_schedule initializes the handshake hash and key
Steven Valdezcd8470f2017-10-11 12:29:36 -04001246// derivation state from the resumption secret and incorporates the PSK to
1247// derive the early secrets. It returns one on success and zero on error.
1248int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1249 size_t psk_len);
Steven Valdez2d850622017-01-11 11:34:52 -05001250
David Benjaminc11ea9422017-08-29 16:33:21 -04001251// tls13_advance_key_schedule incorporates |in| into the key schedule with
1252// HKDF-Extract. It returns one on success and zero on error.
David Benjamin6e4fc332016-11-17 16:43:08 +09001253int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
1254 size_t len);
Steven Valdez143e8b32016-07-11 13:19:03 -04001255
David Benjaminc11ea9422017-08-29 16:33:21 -04001256// tls13_set_traffic_key sets the read or write traffic keys to
1257// |traffic_secret|. It returns one on success and zero on error.
Steven Valdeza833c352016-11-01 13:39:36 -04001258int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
Steven Valdez143e8b32016-07-11 13:19:03 -04001259 const uint8_t *traffic_secret,
1260 size_t traffic_secret_len);
1261
David Benjaminc11ea9422017-08-29 16:33:21 -04001262// tls13_derive_early_secrets derives the early traffic secret. It returns one
1263// on success and zero on error.
Steven Valdez2d850622017-01-11 11:34:52 -05001264int tls13_derive_early_secrets(SSL_HANDSHAKE *hs);
1265
David Benjaminc11ea9422017-08-29 16:33:21 -04001266// tls13_derive_handshake_secrets derives the handshake traffic secret. It
1267// returns one on success and zero on error.
Steven Valdez4cb84942016-12-16 11:29:28 -05001268int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001269
David Benjaminc11ea9422017-08-29 16:33:21 -04001270// tls13_rotate_traffic_key derives the next read or write traffic secret. It
1271// returns one on success and zero on error.
Steven Valdez1dc53d22016-07-26 12:27:38 -04001272int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
1273
David Benjaminc11ea9422017-08-29 16:33:21 -04001274// tls13_derive_application_secrets derives the initial application data traffic
1275// and exporter secrets based on the handshake transcripts and |master_secret|.
1276// It returns one on success and zero on error.
David Benjamin6e4fc332016-11-17 16:43:08 +09001277int tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001278
David Benjaminc11ea9422017-08-29 16:33:21 -04001279// tls13_derive_resumption_secret derives the |resumption_secret|.
David Benjamin6e4fc332016-11-17 16:43:08 +09001280int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001281
David Benjaminc11ea9422017-08-29 16:33:21 -04001282// tls13_export_keying_material provides an exporter interface to use the
1283// |exporter_secret|.
David Benjamin650d8c32017-12-08 17:05:06 -05001284int tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
1285 Span<const uint8_t> secret,
1286 Span<const char> label,
1287 Span<const uint8_t> context);
Steven Valdez143e8b32016-07-11 13:19:03 -04001288
David Benjaminc11ea9422017-08-29 16:33:21 -04001289// tls13_finished_mac calculates the MAC of the handshake transcript to verify
1290// the integrity of the Finished message, and stores the result in |out| and
1291// length in |out_len|. |is_server| is 1 if this is for the Server Finished and
1292// 0 for the Client Finished.
David Benjamin6e4fc332016-11-17 16:43:08 +09001293int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out,
1294 size_t *out_len, int is_server);
Steven Valdez143e8b32016-07-11 13:19:03 -04001295
Steven Valdezcd8470f2017-10-11 12:29:36 -04001296// tls13_derive_session_psk calculates the PSK for this session based on the
1297// resumption master secret and |nonce|. It returns true on success, and false
1298// on failure.
1299bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
1300
David Benjaminc11ea9422017-08-29 16:33:21 -04001301// tls13_write_psk_binder calculates the PSK binder value and replaces the last
1302// bytes of |msg| with the resulting value. It returns 1 on success, and 0 on
1303// failure.
Steven Valdez908ac192017-01-12 13:17:07 -05001304int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001305
David Benjaminc11ea9422017-08-29 16:33:21 -04001306// tls13_verify_psk_binder verifies that the handshake transcript, truncated
1307// up to the binders has a valid signature using the value of |session|'s
1308// resumption secret. It returns 1 on success, and 0 on failure.
Steven Valdez908ac192017-01-12 13:17:07 -05001309int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
David Benjamin7934f082017-08-01 16:32:25 -04001310 const SSLMessage &msg, CBS *binders);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001311
Steven Valdez143e8b32016-07-11 13:19:03 -04001312
David Benjaminc11ea9422017-08-29 16:33:21 -04001313// Handshake functions.
Steven Valdez143e8b32016-07-11 13:19:03 -04001314
1315enum ssl_hs_wait_t {
1316 ssl_hs_error,
1317 ssl_hs_ok,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001318 ssl_hs_read_server_hello,
Steven Valdez143e8b32016-07-11 13:19:03 -04001319 ssl_hs_read_message,
Steven Valdez143e8b32016-07-11 13:19:03 -04001320 ssl_hs_flush,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001321 ssl_hs_certificate_selection_pending,
Adam Langley3fe8fa72018-01-26 09:14:30 -08001322 ssl_hs_handoff,
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001323 ssl_hs_handback,
Steven Valdez143e8b32016-07-11 13:19:03 -04001324 ssl_hs_x509_lookup,
Nick Harper60a85cb2016-09-23 16:25:11 -07001325 ssl_hs_channel_id_lookup,
Steven Valdez143e8b32016-07-11 13:19:03 -04001326 ssl_hs_private_key_operation,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001327 ssl_hs_pending_session,
David Benjamin707af292017-03-10 17:47:18 -05001328 ssl_hs_pending_ticket,
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001329 ssl_hs_early_return,
Steven Valdeze831a812017-03-09 14:56:07 -05001330 ssl_hs_early_data_rejected,
Steven Valdez2d850622017-01-11 11:34:52 -05001331 ssl_hs_read_end_of_early_data,
Steven Valdez520e1222017-06-13 12:45:25 -04001332 ssl_hs_read_change_cipher_spec,
David Benjamin3a1dd462017-07-11 16:13:10 -04001333 ssl_hs_certificate_verify,
Steven Valdez143e8b32016-07-11 13:19:03 -04001334};
1335
David Benjamina7bc9442018-01-18 10:08:53 -05001336enum ssl_grease_index_t {
1337 ssl_grease_cipher = 0,
1338 ssl_grease_group,
1339 ssl_grease_extension1,
1340 ssl_grease_extension2,
1341 ssl_grease_version,
1342 ssl_grease_ticket_extension,
1343 ssl_grease_last_index = ssl_grease_ticket_extension,
1344};
1345
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001346enum tls12_server_hs_state_t {
1347 state12_start_accept = 0,
1348 state12_read_client_hello,
1349 state12_select_certificate,
1350 state12_tls13,
1351 state12_select_parameters,
1352 state12_send_server_hello,
1353 state12_send_server_certificate,
1354 state12_send_server_key_exchange,
1355 state12_send_server_hello_done,
1356 state12_read_client_certificate,
1357 state12_verify_client_certificate,
1358 state12_read_client_key_exchange,
1359 state12_read_client_certificate_verify,
1360 state12_read_change_cipher_spec,
1361 state12_process_change_cipher_spec,
1362 state12_read_next_proto,
1363 state12_read_channel_id,
1364 state12_read_client_finished,
1365 state12_send_server_finished,
1366 state12_finish_server_handshake,
1367 state12_done,
1368};
1369
Matthew Braithwaite3e2b3ee2018-05-10 15:46:42 -07001370// handback_t lists the points in the state machine where a handback can occur.
1371// These are the different points at which key material is no longer needed.
1372enum handback_t {
1373 handback_after_session_resumption,
1374 handback_after_ecdhe,
1375 handback_after_handshake,
1376};
1377
David Benjamin86e95b82017-07-18 16:34:25 -04001378struct SSL_HANDSHAKE {
David Benjamin8f288862017-07-20 14:01:44 -04001379 explicit SSL_HANDSHAKE(SSL *ssl);
1380 ~SSL_HANDSHAKE();
David Benjamin9a892502017-07-25 22:26:44 -04001381 static constexpr bool kAllowUniquePtr = true;
David Benjamin8f288862017-07-20 14:01:44 -04001382
David Benjaminc11ea9422017-08-29 16:33:21 -04001383 // ssl is a non-owning pointer to the parent |SSL| object.
David Benjamince8c9d22016-11-14 10:45:16 +09001384 SSL *ssl;
1385
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001386 // config is a non-owning pointer to the handshake configuration.
1387 SSL_CONFIG *config;
1388
David Benjaminc11ea9422017-08-29 16:33:21 -04001389 // wait contains the operation the handshake is currently blocking on or
1390 // |ssl_hs_ok| if none.
David Benjamin8f288862017-07-20 14:01:44 -04001391 enum ssl_hs_wait_t wait = ssl_hs_ok;
Adam Langley4ba6e192016-12-07 15:54:54 -08001392
David Benjaminc11ea9422017-08-29 16:33:21 -04001393 // state is the internal state for the TLS 1.2 and below handshake. Its
1394 // values depend on |do_handshake| but the starting state is always zero.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001395 int state = 0;
David Benjamincb0c29f2016-12-12 17:00:50 -05001396
David Benjaminc11ea9422017-08-29 16:33:21 -04001397 // tls13_state is the internal state for the TLS 1.3 handshake. Its values
1398 // depend on |do_handshake| but the starting state is always zero.
David Benjamin8f288862017-07-20 14:01:44 -04001399 int tls13_state = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04001400
David Benjaminc11ea9422017-08-29 16:33:21 -04001401 // min_version is the minimum accepted protocol version, taking account both
1402 // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001403 uint16_t min_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001404
David Benjaminc11ea9422017-08-29 16:33:21 -04001405 // max_version is the maximum accepted protocol version, taking account both
1406 // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
David Benjamin8f288862017-07-20 14:01:44 -04001407 uint16_t max_version = 0;
David Benjamin68161cb2017-06-20 14:49:43 -04001408
David Benjamin8f288862017-07-20 14:01:44 -04001409 size_t hash_len = 0;
1410 uint8_t secret[EVP_MAX_MD_SIZE] = {0};
1411 uint8_t early_traffic_secret[EVP_MAX_MD_SIZE] = {0};
1412 uint8_t client_handshake_secret[EVP_MAX_MD_SIZE] = {0};
1413 uint8_t server_handshake_secret[EVP_MAX_MD_SIZE] = {0};
1414 uint8_t client_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
1415 uint8_t server_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
1416 uint8_t expected_client_finished[EVP_MAX_MD_SIZE] = {0};
Steven Valdez143e8b32016-07-11 13:19:03 -04001417
David Benjaminf5d2cd02016-10-06 19:39:20 -04001418 union {
David Benjaminc11ea9422017-08-29 16:33:21 -04001419 // sent is a bitset where the bits correspond to elements of kExtensions
1420 // in t1_lib.c. Each bit is set if that extension was sent in a
1421 // ClientHello. It's not used by servers.
David Benjamin8f288862017-07-20 14:01:44 -04001422 uint32_t sent = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04001423 // received is a bitset, like |sent|, but is used by servers to record
1424 // which extensions were received from a client.
David Benjaminf5d2cd02016-10-06 19:39:20 -04001425 uint32_t received;
1426 } extensions;
1427
1428 union {
David Benjaminc11ea9422017-08-29 16:33:21 -04001429 // sent is a bitset where the bits correspond to elements of
1430 // |client_custom_extensions| in the |SSL_CTX|. Each bit is set if that
1431 // extension was sent in a ClientHello. It's not used by servers.
David Benjamin8f288862017-07-20 14:01:44 -04001432 uint16_t sent = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04001433 // received is a bitset, like |sent|, but is used by servers to record
1434 // which custom extensions were received from a client. The bits here
1435 // correspond to |server_custom_extensions|.
David Benjaminf5d2cd02016-10-06 19:39:20 -04001436 uint16_t received;
1437 } custom_extensions;
1438
David Benjaminc11ea9422017-08-29 16:33:21 -04001439 // retry_group is the group ID selected by the server in HelloRetryRequest in
1440 // TLS 1.3.
David Benjamin8f288862017-07-20 14:01:44 -04001441 uint16_t retry_group = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04001442
David Benjamine52f4c42017-10-01 22:35:10 -04001443 // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
1444 UniquePtr<ERR_SAVE_STATE> error;
1445
David Benjaminc11ea9422017-08-29 16:33:21 -04001446 // key_share is the current key exchange instance.
David Benjaminc642aca2017-07-19 23:28:43 -04001447 UniquePtr<SSLKeyShare> key_share;
Adam Langley4ba6e192016-12-07 15:54:54 -08001448
David Benjaminc11ea9422017-08-29 16:33:21 -04001449 // transcript is the current handshake transcript.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001450 SSLTranscript transcript;
Steven Valdez908ac192017-01-12 13:17:07 -05001451
David Benjaminc11ea9422017-08-29 16:33:21 -04001452 // cookie is the value of the cookie received from the server, if any.
David Benjamin08f5c762017-09-21 02:43:05 -04001453 Array<uint8_t> cookie;
David Benjamin3baa6e12016-10-07 21:10:38 -04001454
David Benjaminc11ea9422017-08-29 16:33:21 -04001455 // key_share_bytes is the value of the previously sent KeyShare extension by
1456 // the client in TLS 1.3.
David Benjamin08f5c762017-09-21 02:43:05 -04001457 Array<uint8_t> key_share_bytes;
David Benjamin4fe3c902016-08-16 02:17:03 -04001458
David Benjaminc11ea9422017-08-29 16:33:21 -04001459 // ecdh_public_key, for servers, is the key share to be sent to the client in
1460 // TLS 1.3.
David Benjamin879efc32017-09-21 11:20:53 -04001461 Array<uint8_t> ecdh_public_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04001462
David Benjaminc11ea9422017-08-29 16:33:21 -04001463 // peer_sigalgs are the signature algorithms that the peer supports. These are
1464 // taken from the contents of the signature algorithms extension for a server
1465 // or from the CertificateRequest for a client.
David Benjaminb1cf48e2017-09-21 11:37:46 -04001466 Array<uint16_t> peer_sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04001467
David Benjaminc11ea9422017-08-29 16:33:21 -04001468 // peer_supported_group_list contains the supported group IDs advertised by
1469 // the peer. This is only set on the server's end. The server does not
1470 // advertise this extension to the client.
David Benjamincf0ce672017-09-21 02:25:59 -04001471 Array<uint16_t> peer_supported_group_list;
David Benjamin43612b62016-10-07 00:41:50 -04001472
David Benjaminc11ea9422017-08-29 16:33:21 -04001473 // peer_key is the peer's ECDH key for a TLS 1.2 client.
David Benjamin499742c2017-07-22 12:45:38 -04001474 Array<uint8_t> peer_key;
David Benjamina4c8ff02016-10-08 02:49:01 -04001475
Nick Harper36fcc4c2017-09-21 15:02:22 -07001476 // negotiated_token_binding_version is used by a server to store the
1477 // on-the-wire encoding of the Token Binding protocol version to advertise in
1478 // the ServerHello/EncryptedExtensions if the Token Binding extension is to be
1479 // sent.
1480 uint16_t negotiated_token_binding_version;
1481
Adam Langleya307cb72018-05-02 09:06:48 -07001482 // cert_compression_alg_id, for a server, contains the negotiated certificate
1483 // compression algorithm for this client. It is only valid if
1484 // |cert_compression_negotiated| is true.
1485 uint16_t cert_compression_alg_id;
1486
David Benjaminc11ea9422017-08-29 16:33:21 -04001487 // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
1488 // parameters. It has client and server randoms prepended for signing
1489 // convenience.
David Benjamin879efc32017-09-21 11:20:53 -04001490 Array<uint8_t> server_params;
David Benjamina4c8ff02016-10-08 02:49:01 -04001491
David Benjaminc11ea9422017-08-29 16:33:21 -04001492 // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
1493 // server when using a TLS 1.2 PSK key exchange.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001494 UniquePtr<char> peer_psk_identity_hint;
Adam Langley4ba6e192016-12-07 15:54:54 -08001495
David Benjaminc11ea9422017-08-29 16:33:21 -04001496 // ca_names, on the client, contains the list of CAs received in a
1497 // CertificateRequest message.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001498 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
Adam Langley34b4c822017-02-02 10:57:17 -08001499
David Benjaminb9493552017-09-27 19:02:51 -04001500 // cached_x509_ca_names contains a cache of parsed versions of the elements of
1501 // |ca_names|. This pointer is left non-owning so only
1502 // |ssl_crypto_x509_method| needs to link against crypto/x509.
David Benjamin8f288862017-07-20 14:01:44 -04001503 STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
Adam Langley4ba6e192016-12-07 15:54:54 -08001504
David Benjaminc11ea9422017-08-29 16:33:21 -04001505 // certificate_types, on the client, contains the set of certificate types
1506 // received in a CertificateRequest message.
David Benjamin08f5c762017-09-21 02:43:05 -04001507 Array<uint8_t> certificate_types;
Adam Langley4ba6e192016-12-07 15:54:54 -08001508
David Benjaminc11ea9422017-08-29 16:33:21 -04001509 // local_pubkey is the public key we are authenticating as.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001510 UniquePtr<EVP_PKEY> local_pubkey;
David Benjamina232a712017-03-30 15:51:53 -05001511
David Benjaminc11ea9422017-08-29 16:33:21 -04001512 // peer_pubkey is the public key parsed from the peer's leaf certificate.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001513 UniquePtr<EVP_PKEY> peer_pubkey;
Adam Langleyd5157222016-12-12 11:37:43 -08001514
David Benjaminc11ea9422017-08-29 16:33:21 -04001515 // new_session is the new mutable session being established by the current
1516 // handshake. It should not be cached.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001517 UniquePtr<SSL_SESSION> new_session;
David Benjamin45738dd2017-02-09 20:01:26 -05001518
David Benjaminc11ea9422017-08-29 16:33:21 -04001519 // early_session is the session corresponding to the current 0-RTT state on
1520 // the client if |in_early_data| is true.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001521 UniquePtr<SSL_SESSION> early_session;
Steven Valdeze831a812017-03-09 14:56:07 -05001522
David Benjaminc11ea9422017-08-29 16:33:21 -04001523 // new_cipher is the cipher being negotiated in this handshake.
David Benjamin8f288862017-07-20 14:01:44 -04001524 const SSL_CIPHER *new_cipher = nullptr;
David Benjamin45738dd2017-02-09 20:01:26 -05001525
David Benjaminc11ea9422017-08-29 16:33:21 -04001526 // key_block is the record-layer key block for TLS 1.2 and earlier.
David Benjaminb9493552017-09-27 19:02:51 -04001527 Array<uint8_t> key_block;
Adam Langley4ba6e192016-12-07 15:54:54 -08001528
David Benjaminfd45ee72017-08-31 14:49:09 -04001529 // scts_requested is true if the SCT extension is in the ClientHello.
1530 bool scts_requested:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001531
David Benjaminfd45ee72017-08-31 14:49:09 -04001532 // needs_psk_binder is true if the ClientHello has a placeholder PSK binder to
1533 // be filled in.
1534 bool needs_psk_binder:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001535
David Benjaminfd45ee72017-08-31 14:49:09 -04001536 bool received_hello_retry_request:1;
Steven Valdez964b2372017-11-07 17:09:52 -05001537 bool sent_hello_retry_request:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001538
David Benjaminfd45ee72017-08-31 14:49:09 -04001539 bool received_custom_extension:1;
Steven Valdezf4ecc842017-08-10 14:02:56 -04001540
Steven Valdezd8168742017-08-31 10:15:48 -04001541 // handshake_finalized is true once the handshake has completed, at which
1542 // point accessors should use the established state.
David Benjaminfd45ee72017-08-31 14:49:09 -04001543 bool handshake_finalized:1;
Steven Valdezd8168742017-08-31 10:15:48 -04001544
David Benjaminc11ea9422017-08-29 16:33:21 -04001545 // accept_psk_mode stores whether the client's PSK mode is compatible with our
1546 // preferences.
David Benjaminfd45ee72017-08-31 14:49:09 -04001547 bool accept_psk_mode:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08001548
David Benjaminfd45ee72017-08-31 14:49:09 -04001549 // cert_request is true if a client certificate was requested.
1550 bool cert_request:1;
David Benjamina0486782016-10-06 19:11:32 -04001551
David Benjaminfd45ee72017-08-31 14:49:09 -04001552 // certificate_status_expected is true if OCSP stapling was negotiated and the
David Benjaminc11ea9422017-08-29 16:33:21 -04001553 // server is expected to send a CertificateStatus message. (This is used on
1554 // both the client and server sides.)
David Benjaminfd45ee72017-08-31 14:49:09 -04001555 bool certificate_status_expected:1;
David Benjamina0486782016-10-06 19:11:32 -04001556
David Benjaminfd45ee72017-08-31 14:49:09 -04001557 // ocsp_stapling_requested is true if a client requested OCSP stapling.
1558 bool ocsp_stapling_requested:1;
David Benjamina0486782016-10-06 19:11:32 -04001559
David Benjaminc11ea9422017-08-29 16:33:21 -04001560 // should_ack_sni is used by a server and indicates that the SNI extension
1561 // should be echoed in the ServerHello.
David Benjaminfd45ee72017-08-31 14:49:09 -04001562 bool should_ack_sni:1;
David Benjamina0486782016-10-06 19:11:32 -04001563
David Benjaminfd45ee72017-08-31 14:49:09 -04001564 // in_false_start is true if there is a pending client handshake in False
David Benjaminc11ea9422017-08-29 16:33:21 -04001565 // Start. The client may write data at this point.
David Benjaminfd45ee72017-08-31 14:49:09 -04001566 bool in_false_start:1;
David Benjamina0486782016-10-06 19:11:32 -04001567
David Benjaminfd45ee72017-08-31 14:49:09 -04001568 // in_early_data is true if there is a pending handshake that has progressed
David Benjaminc11ea9422017-08-29 16:33:21 -04001569 // enough to send and receive early data.
David Benjaminfd45ee72017-08-31 14:49:09 -04001570 bool in_early_data:1;
Steven Valdeze831a812017-03-09 14:56:07 -05001571
David Benjaminfd45ee72017-08-31 14:49:09 -04001572 // early_data_offered is true if the client sent the early_data extension.
1573 bool early_data_offered:1;
Steven Valdez2d850622017-01-11 11:34:52 -05001574
David Benjaminfd45ee72017-08-31 14:49:09 -04001575 // can_early_read is true if application data may be read at this point in the
David Benjaminc11ea9422017-08-29 16:33:21 -04001576 // handshake.
David Benjaminfd45ee72017-08-31 14:49:09 -04001577 bool can_early_read:1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05001578
David Benjaminfd45ee72017-08-31 14:49:09 -04001579 // can_early_write is true if application data may be written at this point in
David Benjaminc11ea9422017-08-29 16:33:21 -04001580 // the handshake.
David Benjaminfd45ee72017-08-31 14:49:09 -04001581 bool can_early_write:1;
Steven Valdez681eb6a2016-12-19 13:19:29 -05001582
David Benjaminc11ea9422017-08-29 16:33:21 -04001583 // next_proto_neg_seen is one of NPN was negotiated.
David Benjaminfd45ee72017-08-31 14:49:09 -04001584 bool next_proto_neg_seen:1;
David Benjaminb74b0812016-10-06 19:43:48 -04001585
David Benjaminfd45ee72017-08-31 14:49:09 -04001586 // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
David Benjaminc11ea9422017-08-29 16:33:21 -04001587 // or received.
David Benjaminfd45ee72017-08-31 14:49:09 -04001588 bool ticket_expected:1;
David Benjaminf04c2e92016-12-06 13:35:25 -05001589
David Benjaminfd45ee72017-08-31 14:49:09 -04001590 // extended_master_secret is true if the extended master secret extension is
David Benjaminc11ea9422017-08-29 16:33:21 -04001591 // negotiated in this handshake.
David Benjaminfd45ee72017-08-31 14:49:09 -04001592 bool extended_master_secret:1;
David Benjaminfc02b592017-02-17 16:26:01 -05001593
David Benjaminfd45ee72017-08-31 14:49:09 -04001594 // pending_private_key_op is true if there is a pending private key operation
David Benjaminc11ea9422017-08-29 16:33:21 -04001595 // in progress.
David Benjaminfd45ee72017-08-31 14:49:09 -04001596 bool pending_private_key_op:1;
David Benjamin44148742017-06-17 13:20:59 -04001597
David Benjamina7bc9442018-01-18 10:08:53 -05001598 // grease_seeded is true if |grease_seed| has been initialized.
1599 bool grease_seeded:1;
1600
Adam Langleya307cb72018-05-02 09:06:48 -07001601 // handback indicates that a server should pause the handshake after
1602 // finishing operations that require private key material, in such a way that
1603 // |SSL_get_error| returns |SSL_HANDBACK|. It is set by |SSL_apply_handoff|.
1604 bool handback:1;
1605
1606 // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid.
1607 bool cert_compression_negotiated:1;
1608
David Benjaminc11ea9422017-08-29 16:33:21 -04001609 // client_version is the value sent or received in the ClientHello version.
David Benjamin8f288862017-07-20 14:01:44 -04001610 uint16_t client_version = 0;
Alessandro Ghedinide254b42017-04-17 19:12:33 +01001611
David Benjaminc11ea9422017-08-29 16:33:21 -04001612 // early_data_read is the amount of early data that has been read by the
1613 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04001614 uint16_t early_data_read = 0;
Steven Valdeze831a812017-03-09 14:56:07 -05001615
David Benjaminc11ea9422017-08-29 16:33:21 -04001616 // early_data_written is the amount of early data that has been written by the
1617 // record layer.
David Benjamin8f288862017-07-20 14:01:44 -04001618 uint16_t early_data_written = 0;
Adam Langley2f9b47f2017-12-29 14:15:55 -08001619
1620 // session_id is the session ID in the ClientHello, used for the experimental
1621 // TLS 1.3 variant.
1622 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
1623 uint8_t session_id_len = 0;
David Benjamina7bc9442018-01-18 10:08:53 -05001624
1625 // grease_seed is the entropy for GREASE values. It is valid if
1626 // |grease_seeded| is true.
1627 uint8_t grease_seed[ssl_grease_last_index + 1] = {0};
Adam Langley4702db62018-02-26 13:51:55 -08001628
Adam Langley8df8e642018-02-28 13:35:39 -08001629 // dummy_pq_padding_len, in a server, is the length of the extension that
1630 // should be echoed in a ServerHello, or zero if no extension should be
1631 // echoed.
Adam Langley4702db62018-02-26 13:51:55 -08001632 uint16_t dummy_pq_padding_len = 0;
David Benjamin86e95b82017-07-18 16:34:25 -04001633};
Steven Valdez143e8b32016-07-11 13:19:03 -04001634
David Benjamin8e7bbba2017-10-13 17:18:35 -04001635UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -04001636
David Benjaminc11ea9422017-08-29 16:33:21 -04001637// ssl_check_message_type checks if |msg| has type |type|. If so it returns
1638// one. Otherwise, it sends an alert and returns zero.
David Benjamin97250f42017-10-07 04:12:35 -04001639bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
David Benjamin276b7e82017-01-21 14:13:39 -05001640
David Benjaminc11ea9422017-08-29 16:33:21 -04001641// ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
1642// on error. It sets |out_early_return| to one if we've completed the handshake
1643// early.
David Benjamin74795b32017-08-31 15:13:12 -04001644int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -04001645
David Benjaminc11ea9422017-08-29 16:33:21 -04001646// The following are implementations of |do_handshake| for the client and
1647// server.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001648enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
1649enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +09001650enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
1651enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001652
David Benjaminc11ea9422017-08-29 16:33:21 -04001653// The following functions return human-readable representations of the TLS
1654// handshake states for debugging.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001655const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
1656const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
David Benjaminf60bcfb2017-08-18 15:23:44 -04001657const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
1658const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
1659
David Benjaminc11ea9422017-08-29 16:33:21 -04001660// tls13_post_handshake processes a post-handshake message. It returns one on
1661// success and zero on failure.
David Benjamin7934f082017-08-01 16:32:25 -04001662int tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
Steven Valdez8e1c7be2016-07-26 12:39:22 -04001663
David Benjamin7934f082017-08-01 16:32:25 -04001664int tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1665 int allow_anonymous);
1666int tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjamin794cc592017-03-25 22:24:23 -05001667
David Benjaminc11ea9422017-08-29 16:33:21 -04001668// tls13_process_finished processes |msg| as a Finished message from the
1669// peer. If |use_saved_value| is one, the verify_data is compared against
1670// |hs->expected_client_finished| rather than computed fresh.
David Benjamin7934f082017-08-01 16:32:25 -04001671int tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1672 int use_saved_value);
Steven Valdez143e8b32016-07-11 13:19:03 -04001673
David Benjamin0f24bed2017-01-12 19:46:50 -05001674int tls13_add_certificate(SSL_HANDSHAKE *hs);
David Benjamin44148742017-06-17 13:20:59 -04001675
David Benjaminc11ea9422017-08-29 16:33:21 -04001676// tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
1677// handshake. If it returns |ssl_private_key_retry|, it should be called again
1678// to retry when the signing operation is completed.
David Benjamin44148742017-06-17 13:20:59 -04001679enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
1680
David Benjamin0f24bed2017-01-12 19:46:50 -05001681int tls13_add_finished(SSL_HANDSHAKE *hs);
David Benjamin7934f082017-08-01 16:32:25 -04001682int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
Steven Valdez143e8b32016-07-11 13:19:03 -04001683
David Benjamin31640932017-10-11 13:22:39 -04001684bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
1685 Array<uint8_t> *out_secret,
1686 uint8_t *out_alert, CBS *contents);
1687bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
1688 Array<uint8_t> *out_secret,
1689 uint8_t *out_alert, CBS *contents);
1690bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez143e8b32016-07-11 13:19:03 -04001691
David Benjamin31640932017-10-11 13:22:39 -04001692bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1693 uint8_t *out_alert,
1694 CBS *contents);
1695bool ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001696 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001697 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents);
David Benjamin31640932017-10-11 13:22:39 -04001698bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001699
David Benjaminc11ea9422017-08-29 16:33:21 -04001700// ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
1701// returns one iff it's valid.
Adam Langleycfa08c32016-11-17 13:21:27 -08001702int ssl_is_sct_list_valid(const CBS *contents);
1703
David Benjamin8c880a22016-12-03 02:20:34 -05001704int ssl_write_client_hello(SSL_HANDSHAKE *hs);
Steven Valdez5440fe02016-07-18 12:40:30 -04001705
Nick Harper60a85cb2016-09-23 16:25:11 -07001706enum ssl_cert_verify_context_t {
1707 ssl_cert_verify_server,
1708 ssl_cert_verify_client,
1709 ssl_cert_verify_channel_id,
1710};
1711
David Benjaminc11ea9422017-08-29 16:33:21 -04001712// tls13_get_cert_verify_signature_input generates the message to be signed for
1713// TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
David Benjamin75a1f232017-10-11 17:19:19 -04001714// type of signature. It sets |*out| to a newly allocated buffer containing the
1715// result. This function returns true on success and false on failure.
1716bool tls13_get_cert_verify_signature_input(
1717 SSL_HANDSHAKE *hs, Array<uint8_t> *out,
Nick Harper60a85cb2016-09-23 16:25:11 -07001718 enum ssl_cert_verify_context_t cert_verify_context);
1719
David Benjamindd6c2e82017-10-17 15:48:46 -04001720// ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001721// selection for |hs->ssl|'s client preferences.
1722bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
1723 Span<const uint8_t> protocol);
David Benjamindd6c2e82017-10-17 15:48:46 -04001724
David Benjaminc11ea9422017-08-29 16:33:21 -04001725// ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
David Benjamin31640932017-10-11 13:22:39 -04001726// true on successful negotiation or if nothing was negotiated. It returns false
David Benjaminc11ea9422017-08-29 16:33:21 -04001727// and sets |*out_alert| to an alert on error.
David Benjamin31640932017-10-11 13:22:39 -04001728bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1729 const SSL_CLIENT_HELLO *client_hello);
David Benjamin9ef31f02016-10-31 18:01:13 -04001730
David Benjamin86e95b82017-07-18 16:34:25 -04001731struct SSL_EXTENSION_TYPE {
David Benjaminffb11072016-11-13 10:32:10 +09001732 uint16_t type;
David Benjamin74795b32017-08-31 15:13:12 -04001733 bool *out_present;
David Benjaminffb11072016-11-13 10:32:10 +09001734 CBS *out_data;
David Benjamin86e95b82017-07-18 16:34:25 -04001735};
David Benjaminffb11072016-11-13 10:32:10 +09001736
David Benjaminc11ea9422017-08-29 16:33:21 -04001737// ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
1738// it. It writes the parsed extensions to pointers denoted by |ext_types|. On
1739// success, it fills in the |out_present| and |out_data| fields and returns one.
1740// Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
1741// extensions are rejected unless |ignore_unknown| is 1.
David Benjaminffb11072016-11-13 10:32:10 +09001742int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
1743 const SSL_EXTENSION_TYPE *ext_types,
Steven Valdez08b65f42016-12-07 15:29:45 -05001744 size_t num_ext_types, int ignore_unknown);
David Benjaminffb11072016-11-13 10:32:10 +09001745
David Benjaminc11ea9422017-08-29 16:33:21 -04001746// ssl_verify_peer_cert verifies the peer certificate for |hs|.
David Benjamin3a1dd462017-07-11 16:13:10 -04001747enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
1748
David Benjamin00f48c82017-10-06 18:43:53 -04001749enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
1750bool ssl_send_finished(SSL_HANDSHAKE *hs);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001751bool ssl_output_cert_chain(SSL_HANDSHAKE *hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04001752
David Benjaminc11ea9422017-08-29 16:33:21 -04001753// SSLKEYLOGFILE functions.
David Benjamine776cc22016-07-19 07:26:49 +02001754
David Benjaminc11ea9422017-08-29 16:33:21 -04001755// ssl_log_secret logs |secret| with label |label|, if logging is enabled for
1756// |ssl|. It returns one on success and zero on failure.
David Benjamine776cc22016-07-19 07:26:49 +02001757int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
1758 size_t secret_len);
1759
1760
David Benjaminc11ea9422017-08-29 16:33:21 -04001761// ClientHello functions.
David Benjamine14ff062016-08-09 16:21:24 -04001762
David Benjamin15868b32017-09-26 17:46:58 -04001763int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
1764 const SSLMessage &msg);
David Benjamine14ff062016-08-09 16:21:24 -04001765
David Benjamin731058e2016-12-03 23:15:13 -05001766int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
1767 CBS *out, uint16_t extension_type);
David Benjamine14ff062016-08-09 16:21:24 -04001768
David Benjamin731058e2016-12-03 23:15:13 -05001769int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
1770 uint16_t id);
David Benjamine14ff062016-08-09 16:21:24 -04001771
1772
David Benjaminc11ea9422017-08-29 16:33:21 -04001773// GREASE.
David Benjamin65ac9972016-09-02 21:35:25 -04001774
David Benjamina7bc9442018-01-18 10:08:53 -05001775// ssl_get_grease_value returns a GREASE value for |hs|. For a given
David Benjaminc11ea9422017-08-29 16:33:21 -04001776// connection, the values for each index will be deterministic. This allows the
1777// same ClientHello be sent twice for a HelloRetryRequest or the same group be
1778// advertised in both supported_groups and key_shares.
David Benjamina7bc9442018-01-18 10:08:53 -05001779uint16_t ssl_get_grease_value(SSL_HANDSHAKE *hs, enum ssl_grease_index_t index);
David Benjamin65ac9972016-09-02 21:35:25 -04001780
1781
David Benjaminc11ea9422017-08-29 16:33:21 -04001782// Signature algorithms.
David Benjamin3ef76972016-10-17 17:59:54 -04001783
David Benjaminc11ea9422017-08-29 16:33:21 -04001784// tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
David Benjamin610cdbb2018-01-22 19:08:38 -05001785// algorithms and saves them on |hs|. It returns true on success and false on
David Benjaminc11ea9422017-08-29 16:33:21 -04001786// error.
David Benjamin610cdbb2018-01-22 19:08:38 -05001787bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
David Benjamin3ef76972016-10-17 17:59:54 -04001788
David Benjaminc11ea9422017-08-29 16:33:21 -04001789// tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
David Benjamin610cdbb2018-01-22 19:08:38 -05001790// that should be used with |pkey| in TLS 1.1 and earlier. It returns true on
1791// success and false if |pkey| may not be used at those versions.
1792bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
David Benjamina3651382017-04-20 17:49:36 -04001793
David Benjaminc11ea9422017-08-29 16:33:21 -04001794// tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
1795// with |hs|'s private key based on the peer's preferences and the algorithms
David Benjamin610cdbb2018-01-22 19:08:38 -05001796// supported. It returns true on success and false on error.
1797bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
David Benjamin3ef76972016-10-17 17:59:54 -04001798
David Benjaminc11ea9422017-08-29 16:33:21 -04001799// tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
David Benjamine28552d2018-04-08 13:59:25 -04001800// peer signature to |out|. It returns true on success and false on error. If
1801// |for_certs| is true, the potentially more restrictive list of algorithms for
1802// certificates is used. Otherwise, the online signature one is used.
1803bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out, bool for_certs);
David Benjamin3ef76972016-10-17 17:59:54 -04001804
David Benjaminc11ea9422017-08-29 16:33:21 -04001805// tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
David Benjamin610cdbb2018-01-22 19:08:38 -05001806// signature. It returns true on success and false on error, setting
1807// |*out_alert| to an alert to send.
1808bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
1809 uint16_t sigalg);
David Benjamin3ef76972016-10-17 17:59:54 -04001810
David Benjamine28552d2018-04-08 13:59:25 -04001811// tls12_has_different_verify_sigalgs_for_certs returns whether |ssl| has a
1812// different, more restrictive, list of signature algorithms acceptable for the
1813// certificate than the online signature.
1814bool tls12_has_different_verify_sigalgs_for_certs(const SSL *ssl);
1815
David Benjamin3ef76972016-10-17 17:59:54 -04001816
David Benjaminc11ea9422017-08-29 16:33:21 -04001817// Underdocumented functions.
1818//
1819// Functions below here haven't been touched up and may be underdocumented.
David Benjamin71f07942015-04-08 02:36:59 -04001820
Adam Langley1258b6a2014-06-20 12:00:00 -07001821#define TLSEXT_CHANNEL_ID_SIZE 128
Adam Langley95c29f32014-06-20 12:00:00 -07001822
David Benjaminc11ea9422017-08-29 16:33:21 -04001823// From RFC4492, used in encoding the curve type in ECParameters
Adam Langleyfcf25832014-12-18 17:42:32 -08001824#define NAMED_CURVE_TYPE 3
Adam Langley95c29f32014-06-20 12:00:00 -07001825
David Benjamin664e99a2017-10-10 19:23:21 -04001826struct CERT {
David Benjamine325c3f2018-04-12 16:11:15 -04001827 static constexpr bool kAllowUniquePtr = true;
1828
1829 explicit CERT(const SSL_X509_METHOD *x509_method);
1830 ~CERT();
1831
1832 UniquePtr<EVP_PKEY> privatekey;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001833
David Benjaminc11ea9422017-08-29 16:33:21 -04001834 // chain contains the certificate chain, with the leaf at the beginning. The
1835 // first element of |chain| may be NULL to indicate that the leaf certificate
1836 // has not yet been set.
1837 // If |chain| != NULL -> len(chain) >= 1
1838 // If |chain[0]| == NULL -> len(chain) >= 2.
1839 // |chain[1..]| != NULL
David Benjamine325c3f2018-04-12 16:11:15 -04001840 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001841
David Benjaminc11ea9422017-08-29 16:33:21 -04001842 // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
1843 // a cache in order to implement “get0” functions that return a non-owning
1844 // pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04001845 STACK_OF(X509) *x509_chain = nullptr;
David Benjaminf31e6812014-11-13 18:05:55 -05001846
David Benjaminc11ea9422017-08-29 16:33:21 -04001847 // x509_leaf may contain a parsed copy of the first element of |chain|. This
1848 // is only used as a cache in order to implement “get0” functions that return
1849 // a non-owning pointer to the certificate chain.
David Benjamine325c3f2018-04-12 16:11:15 -04001850 X509 *x509_leaf = nullptr;
Adam Langley3a2b47a2017-01-24 13:59:42 -08001851
David Benjaminc11ea9422017-08-29 16:33:21 -04001852 // x509_stash contains the last |X509| object append to the chain. This is a
1853 // workaround for some third-party code that continue to use an |X509| object
1854 // even after passing ownership with an “add0” function.
David Benjamine325c3f2018-04-12 16:11:15 -04001855 X509 *x509_stash = nullptr;
Adam Langleye1e78132017-01-31 15:24:31 -08001856
David Benjaminc11ea9422017-08-29 16:33:21 -04001857 // key_method, if non-NULL, is a set of callbacks to call for private key
1858 // operations.
David Benjamine325c3f2018-04-12 16:11:15 -04001859 const SSL_PRIVATE_KEY_METHOD *key_method = nullptr;
David Benjaminb4d65fd2015-05-29 17:11:21 -04001860
David Benjaminc11ea9422017-08-29 16:33:21 -04001861 // x509_method contains pointers to functions that might deal with |X509|
1862 // compatibility, or might be a no-op, depending on the application.
David Benjamine325c3f2018-04-12 16:11:15 -04001863 const SSL_X509_METHOD *x509_method = nullptr;
Adam Langley3509dac2017-02-01 11:59:18 -08001864
David Benjamine325c3f2018-04-12 16:11:15 -04001865 // sigalgs, if non-empty, is the set of signature algorithms supported by
David Benjaminc11ea9422017-08-29 16:33:21 -04001866 // |privatekey| in decreasing order of preference.
David Benjamine325c3f2018-04-12 16:11:15 -04001867 Array<uint16_t> sigalgs;
Adam Langley95c29f32014-06-20 12:00:00 -07001868
David Benjaminc11ea9422017-08-29 16:33:21 -04001869 // Certificate setup callback: if set is called whenever a
1870 // certificate may be required (client or server). the callback
1871 // can then examine any appropriate parameters and setup any
1872 // certificates required. This allows advanced applications
1873 // to select certificates on the fly: for example based on
1874 // supported signature algorithms or curves.
David Benjamine325c3f2018-04-12 16:11:15 -04001875 int (*cert_cb)(SSL *ssl, void *arg) = nullptr;
1876 void *cert_cb_arg = nullptr;
Adam Langleyd323f4b2016-03-01 15:58:14 -08001877
David Benjaminc11ea9422017-08-29 16:33:21 -04001878 // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
1879 // store is used instead.
David Benjamine325c3f2018-04-12 16:11:15 -04001880 X509_STORE *verify_store = nullptr;
David Benjamin83a32122017-02-14 18:34:54 -05001881
David Benjaminc11ea9422017-08-29 16:33:21 -04001882 // Signed certificate timestamp list to be sent to the client, if requested
David Benjamine325c3f2018-04-12 16:11:15 -04001883 UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin83a32122017-02-14 18:34:54 -05001884
David Benjaminc11ea9422017-08-29 16:33:21 -04001885 // OCSP response to be sent to the client, if requested.
David Benjamine325c3f2018-04-12 16:11:15 -04001886 UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin5960a902017-02-14 20:07:11 -05001887
David Benjaminc11ea9422017-08-29 16:33:21 -04001888 // sid_ctx partitions the session space within a shared session cache or
1889 // ticket key. Only sessions with a matching value will be accepted.
David Benjamine325c3f2018-04-12 16:11:15 -04001890 uint8_t sid_ctx_length = 0;
1891 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
David Benjamin86e95b82017-07-18 16:34:25 -04001892};
Adam Langley95c29f32014-06-20 12:00:00 -07001893
David Benjamin664e99a2017-10-10 19:23:21 -04001894// |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
1895struct SSL_PROTOCOL_METHOD {
David Benjamin97250f42017-10-07 04:12:35 -04001896 bool is_dtls;
1897 bool (*ssl_new)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04001898 void (*ssl_free)(SSL *ssl);
1899 // get_message sets |*out| to the current handshake message and returns true
1900 // if one has been received. It returns false if more input is needed.
1901 bool (*get_message)(SSL *ssl, SSLMessage *out);
David Benjamin664e99a2017-10-10 19:23:21 -04001902 // next_message is called to release the current handshake message.
1903 void (*next_message)(SSL *ssl);
David Benjaminf6632da2017-10-12 19:11:47 -04001904 // Use the |ssl_open_handshake| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04001905 ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
1906 uint8_t *out_alert, Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04001907 // Use the |ssl_open_change_cipher_spec| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04001908 ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
1909 uint8_t *out_alert,
1910 Span<uint8_t> in);
David Benjaminf6632da2017-10-12 19:11:47 -04001911 // Use the |ssl_open_app_data| wrapper.
David Benjamind9229f92017-10-06 17:36:20 -04001912 ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
1913 size_t *out_consumed, uint8_t *out_alert,
1914 Span<uint8_t> in);
David Benjamin664e99a2017-10-10 19:23:21 -04001915 int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
1916 int len);
1917 int (*dispatch_alert)(SSL *ssl);
David Benjamin664e99a2017-10-10 19:23:21 -04001918 // init_message begins a new handshake message of type |type|. |cbb| is the
1919 // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
David Benjamin97250f42017-10-07 04:12:35 -04001920 // the caller should write to. It returns true on success and false on error.
1921 bool (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
David Benjamin664e99a2017-10-10 19:23:21 -04001922 // finish_message finishes a handshake message. It sets |*out_msg| to the
David Benjamin97250f42017-10-07 04:12:35 -04001923 // serialized message. It returns true on success and false on error.
1924 bool (*finish_message)(SSL *ssl, CBB *cbb, bssl::Array<uint8_t> *out_msg);
1925 // add_message adds a handshake message to the pending flight. It returns
1926 // true on success and false on error.
1927 bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
David Benjamin664e99a2017-10-10 19:23:21 -04001928 // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
David Benjamin97250f42017-10-07 04:12:35 -04001929 // flight. It returns true on success and false on error.
1930 bool (*add_change_cipher_spec)(SSL *ssl);
1931 // add_alert adds an alert to the pending flight. It returns true on success
1932 // and false on error.
1933 bool (*add_alert)(SSL *ssl, uint8_t level, uint8_t desc);
David Benjamin664e99a2017-10-10 19:23:21 -04001934 // flush_flight flushes the pending flight to the transport. It returns one on
1935 // success and <= 0 on error.
1936 int (*flush_flight)(SSL *ssl);
1937 // on_handshake_complete is called when the handshake is complete.
1938 void (*on_handshake_complete)(SSL *ssl);
1939 // set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It returns
David Benjamin97250f42017-10-07 04:12:35 -04001940 // true on success and false if changing the read state is forbidden at this
David Benjamin664e99a2017-10-10 19:23:21 -04001941 // point.
David Benjamin97250f42017-10-07 04:12:35 -04001942 bool (*set_read_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
David Benjamin664e99a2017-10-10 19:23:21 -04001943 // set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It returns
David Benjamin97250f42017-10-07 04:12:35 -04001944 // true on success and false if changing the write state is forbidden at this
David Benjamin664e99a2017-10-10 19:23:21 -04001945 // point.
David Benjamin97250f42017-10-07 04:12:35 -04001946 bool (*set_write_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
David Benjamin664e99a2017-10-10 19:23:21 -04001947};
1948
David Benjaminf6632da2017-10-12 19:11:47 -04001949// The following wrappers call |open_*| but handle |read_shutdown| correctly.
1950
1951// ssl_open_handshake processes a record from |in| for reading a handshake
1952// message.
1953ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
1954 uint8_t *out_alert, Span<uint8_t> in);
1955
1956// ssl_open_change_cipher_spec processes a record from |in| for reading a
1957// ChangeCipherSpec.
1958ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
1959 uint8_t *out_alert,
1960 Span<uint8_t> in);
1961
1962// ssl_open_app_data processes a record from |in| for reading application data.
1963// On success, it returns |ssl_open_record_success| and sets |*out| to the
1964// input. If it encounters a post-handshake message, it returns
1965// |ssl_open_record_discard|. The caller should then retry, after processing any
1966// messages received with |get_message|.
1967ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
1968 size_t *out_consumed, uint8_t *out_alert,
1969 Span<uint8_t> in);
1970
David Benjamin0363de92018-06-29 13:57:38 -04001971struct SSL_X509_METHOD {
1972 // check_client_CA_list returns one if |names| is a good list of X.509
1973 // distinguished names and zero otherwise. This is used to ensure that we can
1974 // reject unparsable values at handshake time when using crypto/x509.
1975 int (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
1976
1977 // cert_clear frees and NULLs all X509 certificate-related state.
1978 void (*cert_clear)(CERT *cert);
1979 // cert_free frees all X509-related state.
1980 void (*cert_free)(CERT *cert);
1981 // cert_flush_cached_chain drops any cached |X509|-based certificate chain
1982 // from |cert|.
1983 // cert_dup duplicates any needed fields from |cert| to |new_cert|.
1984 void (*cert_dup)(CERT *new_cert, const CERT *cert);
1985 void (*cert_flush_cached_chain)(CERT *cert);
1986 // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
1987 // from |cert|.
1988 void (*cert_flush_cached_leaf)(CERT *cert);
1989
1990 // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
1991 // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
1992 // one on success or zero on error.
1993 int (*session_cache_objects)(SSL_SESSION *session);
1994 // session_dup duplicates any needed fields from |session| to |new_session|.
1995 // It returns one on success or zero on error.
1996 int (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
1997 // session_clear frees any X509-related state from |session|.
1998 void (*session_clear)(SSL_SESSION *session);
1999 // session_verify_cert_chain verifies the certificate chain in |session|,
2000 // sets |session->verify_result| and returns one on success or zero on
2001 // error.
2002 int (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl,
2003 uint8_t *out_alert);
2004
2005 // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
2006 void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs);
2007 // ssl_new does any neccessary initialisation of |hs|. It returns one on
2008 // success or zero on error.
2009 int (*ssl_new)(SSL_HANDSHAKE *hs);
2010 // ssl_free frees anything created by |ssl_new|.
2011 void (*ssl_config_free)(SSL_CONFIG *cfg);
2012 // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
2013 void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg);
2014 // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
2015 // necessary. On success, it updates |ssl|'s certificate configuration as
2016 // needed and returns one. Otherwise, it returns zero.
2017 int (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs);
2018 // ssl_ctx_new does any neccessary initialisation of |ctx|. It returns one on
2019 // success or zero on error.
2020 int (*ssl_ctx_new)(SSL_CTX *ctx);
2021 // ssl_ctx_free frees anything created by |ssl_ctx_new|.
2022 void (*ssl_ctx_free)(SSL_CTX *ctx);
2023 // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
2024 void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
2025};
2026
David Benjaminc11ea9422017-08-29 16:33:21 -04002027// ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
2028// crypto/x509.
David Benjamin86e95b82017-07-18 16:34:25 -04002029extern const SSL_X509_METHOD ssl_crypto_x509_method;
Adam Langley3509dac2017-02-01 11:59:18 -08002030
David Benjaminc11ea9422017-08-29 16:33:21 -04002031// ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
2032// crypto/x509.
David Benjaminba2d3df2017-08-04 13:59:24 -04002033extern const SSL_X509_METHOD ssl_noop_x509_method;
2034
David Benjamin664e99a2017-10-10 19:23:21 -04002035struct tlsext_ticket_key {
2036 static constexpr bool kAllowUniquePtr = true;
2037
2038 uint8_t name[SSL_TICKET_KEY_NAME_LEN];
2039 uint8_t hmac_key[16];
2040 uint8_t aes_key[16];
2041 // next_rotation_tv_sec is the time (in seconds from the epoch) when the
2042 // current key should be superseded by a new key, or the time when a previous
2043 // key should be dropped. If zero, then the key should not be automatically
2044 // rotated.
2045 uint64_t next_rotation_tv_sec;
2046};
2047
David Benjamin4e840352017-10-11 18:41:11 -04002048} // namespace bssl
2049
2050DECLARE_LHASH_OF(SSL_SESSION)
2051
Adam Langleya307cb72018-05-02 09:06:48 -07002052struct CertCompressionAlg {
2053 bssl::CertCompressFunc compress;
2054 bssl::CertDecompressFunc decompress;
2055 uint16_t alg_id;
2056};
2057
2058DEFINE_STACK_OF(CertCompressionAlg);
2059
David Benjamin4e840352017-10-11 18:41:11 -04002060namespace bssl {
2061
David Benjamin664e99a2017-10-10 19:23:21 -04002062// SSLContext backs the public |SSL_CTX| type. Due to compatibility constraints,
2063// it is a base class for |ssl_ctx_st|.
2064struct SSLContext {
2065 const SSL_PROTOCOL_METHOD *method;
2066 const SSL_X509_METHOD *x509_method;
2067
2068 // lock is used to protect various operations on this object.
2069 CRYPTO_MUTEX lock;
2070
2071 // conf_max_version is the maximum acceptable protocol version configured by
2072 // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
2073 // and is further constrainted by |SSL_OP_NO_*|.
2074 uint16_t conf_max_version;
2075
2076 // conf_min_version is the minimum acceptable protocol version configured by
2077 // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
2078 // and is further constrainted by |SSL_OP_NO_*|.
2079 uint16_t conf_min_version;
2080
2081 // tls13_variant is the variant of TLS 1.3 we are using for this
2082 // configuration.
2083 enum tls13_variant_t tls13_variant;
2084
David Benjamin48b276d2018-04-12 17:37:32 -04002085 SSLCipherPreferenceList *cipher_list;
David Benjamin664e99a2017-10-10 19:23:21 -04002086
2087 X509_STORE *cert_store;
2088 LHASH_OF(SSL_SESSION) *sessions;
2089 // Most session-ids that will be cached, default is
2090 // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
2091 unsigned long session_cache_size;
2092 SSL_SESSION *session_cache_head;
2093 SSL_SESSION *session_cache_tail;
2094
2095 // handshakes_since_cache_flush is the number of successful handshakes since
2096 // the last cache flush.
2097 int handshakes_since_cache_flush;
2098
2099 // This can have one of 2 values, ored together,
2100 // SSL_SESS_CACHE_CLIENT,
2101 // SSL_SESS_CACHE_SERVER,
2102 // Default is SSL_SESSION_CACHE_SERVER, which means only
2103 // SSL_accept which cache SSL_SESSIONS.
2104 int session_cache_mode;
2105
2106 // session_timeout is the default lifetime for new sessions in TLS 1.2 and
2107 // earlier, in seconds.
2108 uint32_t session_timeout;
2109
2110 // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
2111 // 1.3, in seconds.
2112 uint32_t session_psk_dhe_timeout;
2113
2114 // If this callback is not null, it will be called each time a session id is
2115 // added to the cache. If this function returns 1, it means that the
2116 // callback will do a SSL_SESSION_free() when it has finished using it.
2117 // Otherwise, on 0, it means the callback has finished with it. If
2118 // remove_session_cb is not null, it will be called when a session-id is
2119 // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
2120 // it.
2121 int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess);
2122 void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess);
2123 SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
2124 int *copy);
David Benjamin664e99a2017-10-10 19:23:21 -04002125
2126 CRYPTO_refcount_t references;
2127
2128 // if defined, these override the X509_verify_cert() calls
2129 int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg);
2130 void *app_verify_arg;
2131
2132 enum ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
2133 uint8_t *out_alert);
2134
2135 // Default password callback.
2136 pem_password_cb *default_passwd_callback;
2137
2138 // Default password callback user data.
2139 void *default_passwd_callback_userdata;
2140
2141 // get client cert callback
2142 int (*client_cert_cb)(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey);
2143
2144 // get channel id callback
2145 void (*channel_id_cb)(SSL *ssl, EVP_PKEY **out_pkey);
2146
2147 CRYPTO_EX_DATA ex_data;
2148
2149 // custom_*_extensions stores any callback sets for custom extensions. Note
2150 // that these pointers will be NULL if the stack would otherwise be empty.
2151 STACK_OF(SSL_CUSTOM_EXTENSION) *client_custom_extensions;
2152 STACK_OF(SSL_CUSTOM_EXTENSION) *server_custom_extensions;
2153
2154 // Default values used when no per-SSL value is defined follow
2155
2156 void (*info_callback)(const SSL *ssl, int type, int value);
2157
2158 // what we put in client cert requests
2159 STACK_OF(CRYPTO_BUFFER) *client_CA;
2160
2161 // cached_x509_client_CA is a cache of parsed versions of the elements of
2162 // |client_CA|.
2163 STACK_OF(X509_NAME) *cached_x509_client_CA;
2164
2165
2166 // Default values to use in SSL structures follow (these are copied by
2167 // SSL_new)
2168
2169 uint32_t options;
2170 uint32_t mode;
2171 uint32_t max_cert_list;
2172
2173 CERT *cert;
2174
2175 // callback that allows applications to peek at protocol messages
2176 void (*msg_callback)(int write_p, int version, int content_type,
2177 const void *buf, size_t len, SSL *ssl, void *arg);
2178 void *msg_callback_arg;
2179
2180 int verify_mode;
2181 int (*default_verify_callback)(
2182 int ok, X509_STORE_CTX *ctx); // called 'verify_callback' in the SSL
2183
2184 X509_VERIFY_PARAM *param;
2185
2186 // select_certificate_cb is called before most ClientHello processing and
2187 // before the decision whether to resume a session is made. See
2188 // |ssl_select_cert_result_t| for details of the return values.
2189 enum ssl_select_cert_result_t (*select_certificate_cb)(
2190 const SSL_CLIENT_HELLO *);
2191
2192 // dos_protection_cb is called once the resumption decision for a ClientHello
2193 // has been made. It returns one to continue the handshake or zero to
2194 // abort.
2195 int (*dos_protection_cb) (const SSL_CLIENT_HELLO *);
2196
2197 // Maximum amount of data to send in one fragment. actual record size can be
2198 // more than this due to padding and MAC overheads.
2199 uint16_t max_send_fragment;
2200
2201 // TLS extensions servername callback
2202 int (*tlsext_servername_callback)(SSL *, int *, void *);
2203 void *tlsext_servername_arg;
2204
2205 // RFC 4507 session ticket keys. |tlsext_ticket_key_current| may be NULL
2206 // before the first handshake and |tlsext_ticket_key_prev| may be NULL at any
2207 // time. Automatically generated ticket keys are rotated as needed at
2208 // handshake time. Hence, all access must be synchronized through |lock|.
2209 struct tlsext_ticket_key *tlsext_ticket_key_current;
2210 struct tlsext_ticket_key *tlsext_ticket_key_prev;
2211
2212 // Callback to support customisation of ticket key setting
2213 int (*tlsext_ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
2214 EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
2215
2216 // Server-only: psk_identity_hint is the default identity hint to send in
2217 // PSK-based key exchanges.
2218 char *psk_identity_hint;
2219
2220 unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
2221 char *identity,
2222 unsigned int max_identity_len,
2223 uint8_t *psk, unsigned int max_psk_len);
2224 unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
2225 uint8_t *psk, unsigned int max_psk_len);
2226
2227
David Benjamin664e99a2017-10-10 19:23:21 -04002228 // Next protocol negotiation information
2229 // (for experimental NPN extension).
2230
2231 // For a server, this contains a callback function by which the set of
2232 // advertised protocols can be provided.
2233 int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
2234 unsigned *out_len, void *arg);
2235 void *next_protos_advertised_cb_arg;
2236 // For a client, this contains a callback function that selects the
2237 // next protocol from the list provided by the server.
2238 int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
2239 const uint8_t *in, unsigned in_len, void *arg);
2240 void *next_proto_select_cb_arg;
2241
2242 // ALPN information
2243 // (we are in the process of transitioning from NPN to ALPN.)
2244
2245 // For a server, this contains a callback function that allows the
2246 // server to select the protocol for the connection.
2247 // out: on successful return, this must point to the raw protocol
2248 // name (without the length prefix).
2249 // outlen: on successful return, this contains the length of |*out|.
2250 // in: points to the client's list of supported protocols in
2251 // wire-format.
2252 // inlen: the length of |in|.
2253 int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
2254 const uint8_t *in, unsigned in_len, void *arg);
2255 void *alpn_select_cb_arg;
2256
2257 // For a client, this contains the list of supported protocols in wire
2258 // format.
2259 uint8_t *alpn_client_proto_list;
2260 unsigned alpn_client_proto_list_len;
2261
2262 // SRTP profiles we are willing to do from RFC 5764
2263 STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
2264
Adam Langleya307cb72018-05-02 09:06:48 -07002265 // Defined compression algorithms for certificates.
2266 STACK_OF(CertCompressionAlg) *cert_compression_algs;
2267
David Benjamin664e99a2017-10-10 19:23:21 -04002268 // Supported group values inherited by SSL structure
2269 size_t supported_group_list_len;
2270 uint16_t *supported_group_list;
2271
2272 // The client's Channel ID private key.
2273 EVP_PKEY *tlsext_channel_id_private;
2274
2275 // keylog_callback, if not NULL, is the key logging callback. See
2276 // |SSL_CTX_set_keylog_callback|.
2277 void (*keylog_callback)(const SSL *ssl, const char *line);
2278
2279 // current_time_cb, if not NULL, is the function to use to get the current
2280 // time. It sets |*out_clock| to the current time. The |ssl| argument is
2281 // always NULL. See |SSL_CTX_set_current_time_cb|.
2282 void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock);
2283
2284 // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
2285 // memory.
2286 CRYPTO_BUFFER_POOL *pool;
2287
2288 // ticket_aead_method contains function pointers for opening and sealing
2289 // session tickets.
2290 const SSL_TICKET_AEAD_METHOD *ticket_aead_method;
2291
David Benjamin103ed082018-05-10 19:55:02 -04002292 // legacy_ocsp_callback implements an OCSP-related callback for OpenSSL
2293 // compatibility.
2294 int (*legacy_ocsp_callback)(SSL *ssl, void *arg);
2295 void *legacy_ocsp_callback_arg;
2296
David Benjamin664e99a2017-10-10 19:23:21 -04002297 // verify_sigalgs, if not empty, is the set of signature algorithms
2298 // accepted from the peer in decreasing order of preference.
2299 uint16_t *verify_sigalgs;
2300 size_t num_verify_sigalgs;
2301
David Benjamin7e58c5e2017-10-11 13:01:08 -04002302 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
2303 // hash of the peer's certificate and then discard it to save memory and
2304 // session space. Only effective on the server side.
2305 bool retain_only_sha256_of_client_certs:1;
2306
David Benjamin664e99a2017-10-10 19:23:21 -04002307 // quiet_shutdown is true if the connection should not send a close_notify on
2308 // shutdown.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002309 bool quiet_shutdown:1;
David Benjamin664e99a2017-10-10 19:23:21 -04002310
2311 // ocsp_stapling_enabled is only used by client connections and indicates
2312 // whether OCSP stapling will be requested.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002313 bool ocsp_stapling_enabled:1;
David Benjamin664e99a2017-10-10 19:23:21 -04002314
2315 // If true, a client will request certificate timestamps.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002316 bool signed_cert_timestamps_enabled:1;
David Benjamin664e99a2017-10-10 19:23:21 -04002317
David Benjamin494e4d02017-12-01 13:05:12 -05002318 // tlsext_channel_id_enabled is whether Channel ID is enabled. For a server,
2319 // means that we'll accept Channel IDs from clients. For a client, means that
2320 // we'll advertise support.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002321 bool tlsext_channel_id_enabled:1;
David Benjamin664e99a2017-10-10 19:23:21 -04002322
David Benjamin494e4d02017-12-01 13:05:12 -05002323 // grease_enabled is whether draft-davidben-tls-grease-01 is enabled.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002324 bool grease_enabled:1;
David Benjamin664e99a2017-10-10 19:23:21 -04002325
David Benjamin494e4d02017-12-01 13:05:12 -05002326 // allow_unknown_alpn_protos is whether the client allows unsolicited ALPN
David Benjamin664e99a2017-10-10 19:23:21 -04002327 // protocols from the peer.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002328 bool allow_unknown_alpn_protos:1;
David Benjamin664e99a2017-10-10 19:23:21 -04002329
David Benjamin494e4d02017-12-01 13:05:12 -05002330 // ed25519_enabled is whether Ed25519 is advertised in the handshake.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002331 bool ed25519_enabled:1;
David Benjamin494e4d02017-12-01 13:05:12 -05002332
David Benjamine28552d2018-04-08 13:59:25 -04002333 // rsa_pss_rsae_certs_enabled is whether rsa_pss_rsae_* are supported by the
2334 // certificate verifier.
2335 bool rsa_pss_rsae_certs_enabled:1;
2336
David Benjamin494e4d02017-12-01 13:05:12 -05002337 // false_start_allowed_without_alpn is whether False Start (if
2338 // |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN.
2339 bool false_start_allowed_without_alpn:1;
Adam Langley3fe8fa72018-01-26 09:14:30 -08002340
2341 // handoff indicates that a server should stop after receiving the
2342 // ClientHello and pause the handshake in such a way that |SSL_get_error|
2343 // returns |SSL_HANDOFF|.
2344 bool handoff:1;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002345
2346 // If enable_early_data is true, early data can be sent and accepted.
2347 bool enable_early_data : 1;
David Benjamin664e99a2017-10-10 19:23:21 -04002348};
2349
David Benjaminc11ea9422017-08-29 16:33:21 -04002350// An ssl_shutdown_t describes the shutdown state of one end of the connection,
2351// whether it is alive or has been shutdown via close_notify or fatal alert.
David Benjamine228bd22016-10-17 20:41:08 -04002352enum ssl_shutdown_t {
2353 ssl_shutdown_none = 0,
2354 ssl_shutdown_close_notify = 1,
David Benjamin31aad2d2017-10-06 16:04:06 -04002355 ssl_shutdown_error = 2,
David Benjamine228bd22016-10-17 20:41:08 -04002356};
2357
David Benjamin86e95b82017-07-18 16:34:25 -04002358struct SSL3_STATE {
David Benjamin94172572017-10-13 16:53:21 -04002359 static constexpr bool kAllowUniquePtr = true;
David Benjamine228bd22016-10-17 20:41:08 -04002360
David Benjamin94172572017-10-13 16:53:21 -04002361 SSL3_STATE();
2362 ~SSL3_STATE();
2363
2364 uint8_t read_sequence[8] = {0};
2365 uint8_t write_sequence[8] = {0};
2366
2367 uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
2368 uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
David Benjamine228bd22016-10-17 20:41:08 -04002369
David Benjaminc11ea9422017-08-29 16:33:21 -04002370 // read_buffer holds data from the transport to be processed.
David Benjaminea712e32017-10-13 16:50:39 -04002371 SSLBuffer read_buffer;
David Benjaminc11ea9422017-08-29 16:33:21 -04002372 // write_buffer holds data to be written to the transport.
David Benjaminea712e32017-10-13 16:50:39 -04002373 SSLBuffer write_buffer;
David Benjamine228bd22016-10-17 20:41:08 -04002374
David Benjamind9229f92017-10-06 17:36:20 -04002375 // pending_app_data is the unconsumed application data. It points into
2376 // |read_buffer|.
2377 Span<uint8_t> pending_app_data;
David Benjamine228bd22016-10-17 20:41:08 -04002378
David Benjaminc11ea9422017-08-29 16:33:21 -04002379 // partial write - check the numbers match
David Benjamin94172572017-10-13 16:53:21 -04002380 unsigned int wnum = 0; // number of bytes sent so far
2381 int wpend_tot = 0; // number bytes written
2382 int wpend_type = 0;
2383 int wpend_ret = 0; // number of bytes submitted
2384 const uint8_t *wpend_buf = nullptr;
David Benjamine228bd22016-10-17 20:41:08 -04002385
David Benjamin23c25d52017-10-06 16:02:47 -04002386 // read_shutdown is the shutdown state for the read half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002387 enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002388
David Benjamin23c25d52017-10-06 16:02:47 -04002389 // write_shutdown is the shutdown state for the write half of the connection.
David Benjamin94172572017-10-13 16:53:21 -04002390 enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
David Benjamine228bd22016-10-17 20:41:08 -04002391
David Benjamin31aad2d2017-10-06 16:04:06 -04002392 // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
2393 // the receive half of the connection.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002394 UniquePtr<ERR_SAVE_STATE> read_error;
David Benjamin31aad2d2017-10-06 16:04:06 -04002395
David Benjamin94172572017-10-13 16:53:21 -04002396 int alert_dispatch = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002397
David Benjamin94172572017-10-13 16:53:21 -04002398 int total_renegotiations = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002399
David Benjamin32ce0ac2017-10-13 19:17:22 -04002400 // This holds a variable that indicates what we were doing when a 0 or -1 is
2401 // returned. This is needed for non-blocking IO so we know what request
2402 // needs re-doing when in SSL_accept or SSL_connect
2403 int rwstate = SSL_NOTHING;
2404
David Benjaminc11ea9422017-08-29 16:33:21 -04002405 // early_data_skipped is the amount of early data that has been skipped by the
2406 // record layer.
David Benjamin94172572017-10-13 16:53:21 -04002407 uint16_t early_data_skipped = 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002408
David Benjaminc11ea9422017-08-29 16:33:21 -04002409 // empty_record_count is the number of consecutive empty records received.
David Benjamin94172572017-10-13 16:53:21 -04002410 uint8_t empty_record_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002411
David Benjaminc11ea9422017-08-29 16:33:21 -04002412 // warning_alert_count is the number of consecutive warning alerts
2413 // received.
David Benjamin94172572017-10-13 16:53:21 -04002414 uint8_t warning_alert_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002415
David Benjaminc11ea9422017-08-29 16:33:21 -04002416 // key_update_count is the number of consecutive KeyUpdates received.
David Benjamin94172572017-10-13 16:53:21 -04002417 uint8_t key_update_count = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002418
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002419 // The negotiated Token Binding key parameter. Only valid if
2420 // |token_binding_negotiated| is set.
2421 uint8_t negotiated_token_binding_param = 0;
2422
David Benjaminc11ea9422017-08-29 16:33:21 -04002423 // skip_early_data instructs the record layer to skip unexpected early data
2424 // messages when 0RTT is rejected.
David Benjamin046bc1f2017-08-31 15:06:42 -04002425 bool skip_early_data:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002426
David Benjaminc11ea9422017-08-29 16:33:21 -04002427 // have_version is true if the connection's final version is known. Otherwise
2428 // the version has not been negotiated yet.
David Benjamin046bc1f2017-08-31 15:06:42 -04002429 bool have_version:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002430
David Benjaminc11ea9422017-08-29 16:33:21 -04002431 // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
2432 // and future messages should use the record layer.
David Benjamin046bc1f2017-08-31 15:06:42 -04002433 bool v2_hello_done:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002434
David Benjaminc11ea9422017-08-29 16:33:21 -04002435 // is_v2_hello is true if the current handshake message was derived from a
2436 // V2ClientHello rather than received from the peer directly.
David Benjamin046bc1f2017-08-31 15:06:42 -04002437 bool is_v2_hello:1;
David Benjamin1a444da2017-01-21 14:27:45 -05002438
David Benjaminc11ea9422017-08-29 16:33:21 -04002439 // has_message is true if the current handshake message has been returned
2440 // at least once by |get_message| and false otherwise.
David Benjamin046bc1f2017-08-31 15:06:42 -04002441 bool has_message:1;
David Benjamin7934f082017-08-01 16:32:25 -04002442
David Benjaminc11ea9422017-08-29 16:33:21 -04002443 // initial_handshake_complete is true if the initial handshake has
2444 // completed.
David Benjamin046bc1f2017-08-31 15:06:42 -04002445 bool initial_handshake_complete:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002446
David Benjaminc11ea9422017-08-29 16:33:21 -04002447 // session_reused indicates whether a session was resumed.
David Benjamin046bc1f2017-08-31 15:06:42 -04002448 bool session_reused:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002449
David Benjamin046bc1f2017-08-31 15:06:42 -04002450 bool send_connection_binding:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002451
David Benjaminc11ea9422017-08-29 16:33:21 -04002452 // In a client, this means that the server supported Channel ID and that a
2453 // Channel ID was sent. In a server it means that we echoed support for
2454 // Channel IDs and that tlsext_channel_id will be valid after the
2455 // handshake.
David Benjamin046bc1f2017-08-31 15:06:42 -04002456 bool tlsext_channel_id_valid:1;
Adam Langley4ba6e192016-12-07 15:54:54 -08002457
David Benjamin046bc1f2017-08-31 15:06:42 -04002458 // key_update_pending is true if we have a KeyUpdate acknowledgment
David Benjaminc11ea9422017-08-29 16:33:21 -04002459 // outstanding.
David Benjamin046bc1f2017-08-31 15:06:42 -04002460 bool key_update_pending:1;
David Benjaminbbba9392017-04-06 12:54:12 -04002461
David Benjamin046bc1f2017-08-31 15:06:42 -04002462 // wpend_pending is true if we have a pending write outstanding.
2463 bool wpend_pending:1;
Steven Valdeze831a812017-03-09 14:56:07 -05002464
David Benjamin02e62562017-12-18 18:04:01 -05002465 // early_data_accepted is true if early data was accepted by the server.
2466 bool early_data_accepted:1;
2467
David Benjamin6df65402017-12-18 18:00:23 -05002468 // draft_downgrade is whether the TLS 1.3 anti-downgrade logic would have
2469 // fired, were it not a draft.
2470 bool draft_downgrade:1;
2471
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002472 // token_binding_negotiated is set if Token Binding was negotiated.
2473 bool token_binding_negotiated:1;
2474
David Benjamin32ce0ac2017-10-13 19:17:22 -04002475 // hs_buf is the buffer of handshake data to process.
2476 UniquePtr<BUF_MEM> hs_buf;
2477
David Benjamin700631b2018-05-24 17:17:34 -04002478 // pending_hs_data contains the pending handshake data that has not yet
2479 // been encrypted to |pending_flight|. This allows packing the handshake into
2480 // fewer records.
2481 UniquePtr<BUF_MEM> pending_hs_data;
2482
David Benjaminc11ea9422017-08-29 16:33:21 -04002483 // pending_flight is the pending outgoing flight. This is used to flush each
2484 // handshake flight in a single write. |write_buffer| must be written out
2485 // before this data.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002486 UniquePtr<BUF_MEM> pending_flight;
David Benjamindaf207a2017-01-03 18:37:41 -05002487
David Benjaminc11ea9422017-08-29 16:33:21 -04002488 // pending_flight_offset is the number of bytes of |pending_flight| which have
2489 // been successfully written.
David Benjamin94172572017-10-13 16:53:21 -04002490 uint32_t pending_flight_offset = 0;
Adam Langley4ba6e192016-12-07 15:54:54 -08002491
Adam Langley2f9b47f2017-12-29 14:15:55 -08002492 // ticket_age_skew is the difference, in seconds, between the client-sent
2493 // ticket age and the server-computed value in TLS 1.3 server connections
2494 // which resumed a session.
2495 int32_t ticket_age_skew = 0;
2496
David Benjaminc11ea9422017-08-29 16:33:21 -04002497 // aead_read_ctx is the current read cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002498 UniquePtr<SSLAEADContext> aead_read_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002499
David Benjaminc11ea9422017-08-29 16:33:21 -04002500 // aead_write_ctx is the current write cipher state.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002501 UniquePtr<SSLAEADContext> aead_write_ctx;
David Benjamine228bd22016-10-17 20:41:08 -04002502
David Benjaminc11ea9422017-08-29 16:33:21 -04002503 // hs is the handshake state for the current handshake or NULL if there isn't
2504 // one.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002505 UniquePtr<SSL_HANDSHAKE> hs;
David Benjamine228bd22016-10-17 20:41:08 -04002506
David Benjamin94172572017-10-13 16:53:21 -04002507 uint8_t write_traffic_secret[EVP_MAX_MD_SIZE] = {0};
2508 uint8_t read_traffic_secret[EVP_MAX_MD_SIZE] = {0};
2509 uint8_t exporter_secret[EVP_MAX_MD_SIZE] = {0};
2510 uint8_t early_exporter_secret[EVP_MAX_MD_SIZE] = {0};
2511 uint8_t write_traffic_secret_len = 0;
2512 uint8_t read_traffic_secret_len = 0;
2513 uint8_t exporter_secret_len = 0;
2514 uint8_t early_exporter_secret_len = 0;
David Benjamine228bd22016-10-17 20:41:08 -04002515
David Benjaminc11ea9422017-08-29 16:33:21 -04002516 // Connection binding to prevent renegotiation attacks
David Benjamin94172572017-10-13 16:53:21 -04002517 uint8_t previous_client_finished[12] = {0};
2518 uint8_t previous_client_finished_len = 0;
2519 uint8_t previous_server_finished_len = 0;
2520 uint8_t previous_server_finished[12] = {0};
Adam Langley4ba6e192016-12-07 15:54:54 -08002521
Adam Langley2f9b47f2017-12-29 14:15:55 -08002522 uint8_t send_alert[2] = {0};
2523
David Benjaminc11ea9422017-08-29 16:33:21 -04002524 // established_session is the session established by the connection. This
2525 // session is only filled upon the completion of the handshake and is
2526 // immutable.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002527 UniquePtr<SSL_SESSION> established_session;
David Benjamine228bd22016-10-17 20:41:08 -04002528
David Benjaminc11ea9422017-08-29 16:33:21 -04002529 // Next protocol negotiation. For the client, this is the protocol that we
2530 // sent in NextProtocol and is set when handling ServerHello extensions.
2531 //
2532 // For a server, this is the client's selected_protocol from NextProtocol and
2533 // is set when handling the NextProtocol message, before the Finished
2534 // message.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002535 Array<uint8_t> next_proto_negotiated;
David Benjamine228bd22016-10-17 20:41:08 -04002536
David Benjaminc11ea9422017-08-29 16:33:21 -04002537 // ALPN information
2538 // (we are in the process of transitioning from NPN to ALPN.)
David Benjamine228bd22016-10-17 20:41:08 -04002539
David Benjaminc11ea9422017-08-29 16:33:21 -04002540 // In a server these point to the selected ALPN protocol after the
2541 // ClientHello has been processed. In a client these contain the protocol
2542 // that the server selected once the ServerHello has been processed.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002543 Array<uint8_t> alpn_selected;
David Benjamine228bd22016-10-17 20:41:08 -04002544
David Benjamina8614602017-09-06 15:40:19 -04002545 // hostname, on the server, is the value of the SNI extension.
David Benjamin8e7bbba2017-10-13 17:18:35 -04002546 UniquePtr<char> hostname;
David Benjamina8614602017-09-06 15:40:19 -04002547
David Benjaminc11ea9422017-08-29 16:33:21 -04002548 // For a server:
2549 // If |tlsext_channel_id_valid| is true, then this contains the
2550 // verified Channel ID from the client: a P256 point, (x,y), where
2551 // each are big-endian values.
David Benjamin94172572017-10-13 16:53:21 -04002552 uint8_t tlsext_channel_id[64] = {0};
Nick Harper3c034b22017-12-22 15:50:43 -08002553
2554 // Contains the QUIC transport params received by the peer.
2555 Array<uint8_t> peer_quic_transport_params;
David Benjaminfceca8e2018-04-12 16:37:19 -04002556
2557 // srtp_profile is the selected SRTP protection profile for
2558 // DTLS-SRTP.
2559 const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002560};
David Benjamine228bd22016-10-17 20:41:08 -04002561
David Benjaminc11ea9422017-08-29 16:33:21 -04002562// lengths of messages
David Benjamin593047f2015-05-08 13:08:52 -04002563#define DTLS1_COOKIE_LENGTH 256
2564
2565#define DTLS1_RT_HEADER_LENGTH 13
2566
2567#define DTLS1_HM_HEADER_LENGTH 12
2568
2569#define DTLS1_CCS_HEADER_LENGTH 1
2570
2571#define DTLS1_AL_HEADER_LENGTH 2
2572
David Benjamin593047f2015-05-08 13:08:52 -04002573struct hm_header_st {
2574 uint8_t type;
2575 uint32_t msg_len;
2576 uint16_t seq;
2577 uint32_t frag_off;
2578 uint32_t frag_len;
David Benjamin593047f2015-05-08 13:08:52 -04002579};
2580
David Benjaminc11ea9422017-08-29 16:33:21 -04002581// An hm_fragment is an incoming DTLS message, possibly not yet assembled.
David Benjamin86e95b82017-07-18 16:34:25 -04002582struct hm_fragment {
David Benjamin049fdfc2017-10-17 01:12:53 -04002583 static constexpr bool kAllowUniquePtr = true;
2584
2585 hm_fragment() {}
2586 hm_fragment(const hm_fragment &) = delete;
2587 hm_fragment &operator=(const hm_fragment &) = delete;
2588
2589 ~hm_fragment();
2590
David Benjaminc11ea9422017-08-29 16:33:21 -04002591 // type is the type of the message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002592 uint8_t type = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002593 // seq is the sequence number of this message.
David Benjamin049fdfc2017-10-17 01:12:53 -04002594 uint16_t seq = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002595 // msg_len is the length of the message body.
David Benjamin049fdfc2017-10-17 01:12:53 -04002596 uint32_t msg_len = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002597 // data is a pointer to the message, including message header. It has length
2598 // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
David Benjamin049fdfc2017-10-17 01:12:53 -04002599 uint8_t *data = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04002600 // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
2601 // the message have been received. It is NULL if the message is complete.
David Benjamin049fdfc2017-10-17 01:12:53 -04002602 uint8_t *reassembly = nullptr;
David Benjamin86e95b82017-07-18 16:34:25 -04002603};
David Benjamin593047f2015-05-08 13:08:52 -04002604
David Benjaminad8f5e12017-02-20 17:00:20 -05002605struct OPENSSL_timeval {
2606 uint64_t tv_sec;
2607 uint32_t tv_usec;
2608};
2609
David Benjamin86e95b82017-07-18 16:34:25 -04002610struct DTLS1_STATE {
David Benjamin800046f2017-10-17 01:37:51 -04002611 static constexpr bool kAllowUniquePtr = true;
2612
2613 DTLS1_STATE();
2614 ~DTLS1_STATE();
2615
David Benjaminc11ea9422017-08-29 16:33:21 -04002616 // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
2617 // the peer in this epoch.
David Benjaminb0c761e2017-06-25 22:42:55 -04002618 bool has_change_cipher_spec:1;
David Benjamin593047f2015-05-08 13:08:52 -04002619
David Benjaminc11ea9422017-08-29 16:33:21 -04002620 // outgoing_messages_complete is true if |outgoing_messages| has been
2621 // completed by an attempt to flush it. Future calls to |add_message| and
2622 // |add_change_cipher_spec| will start a new flight.
David Benjamin9bbdf582017-08-02 19:46:29 -04002623 bool outgoing_messages_complete:1;
2624
David Benjaminc11ea9422017-08-29 16:33:21 -04002625 // flight_has_reply is true if the current outgoing flight is complete and has
2626 // processed at least one message. This is used to detect whether we or the
2627 // peer sent the final flight.
David Benjamin302b8182017-08-22 14:47:22 -07002628 bool flight_has_reply:1;
2629
David Benjamin800046f2017-10-17 01:37:51 -04002630 uint8_t cookie[DTLS1_COOKIE_LENGTH] = {0};
2631 size_t cookie_len = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002632
David Benjaminc11ea9422017-08-29 16:33:21 -04002633 // The current data and handshake epoch. This is initially undefined, and
2634 // starts at zero once the initial handshake is completed.
David Benjamin800046f2017-10-17 01:37:51 -04002635 uint16_t r_epoch = 0;
2636 uint16_t w_epoch = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002637
David Benjaminc11ea9422017-08-29 16:33:21 -04002638 // records being received in the current epoch
David Benjamin593047f2015-05-08 13:08:52 -04002639 DTLS1_BITMAP bitmap;
2640
David Benjamin800046f2017-10-17 01:37:51 -04002641 uint16_t handshake_write_seq = 0;
2642 uint16_t handshake_read_seq = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002643
David Benjaminc11ea9422017-08-29 16:33:21 -04002644 // save last sequence number for retransmissions
David Benjamin800046f2017-10-17 01:37:51 -04002645 uint8_t last_write_sequence[8] = {0};
2646 UniquePtr<SSLAEADContext> last_aead_write_ctx;
David Benjamin593047f2015-05-08 13:08:52 -04002647
David Benjaminc11ea9422017-08-29 16:33:21 -04002648 // incoming_messages is a ring buffer of incoming handshake messages that have
2649 // yet to be processed. The front of the ring buffer is message number
2650 // |handshake_read_seq|, at position |handshake_read_seq| %
2651 // |SSL_MAX_HANDSHAKE_FLIGHT|.
David Benjamin800046f2017-10-17 01:37:51 -04002652 UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin593047f2015-05-08 13:08:52 -04002653
David Benjaminc11ea9422017-08-29 16:33:21 -04002654 // outgoing_messages is the queue of outgoing messages from the last handshake
2655 // flight.
David Benjamin29a83c52016-06-17 19:12:54 -04002656 DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin800046f2017-10-17 01:37:51 -04002657 uint8_t outgoing_messages_len = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002658
David Benjaminc11ea9422017-08-29 16:33:21 -04002659 // outgoing_written is the number of outgoing messages that have been
2660 // written.
David Benjamin800046f2017-10-17 01:37:51 -04002661 uint8_t outgoing_written = 0;
David Benjaminc11ea9422017-08-29 16:33:21 -04002662 // outgoing_offset is the number of bytes of the next outgoing message have
2663 // been written.
David Benjamin800046f2017-10-17 01:37:51 -04002664 uint32_t outgoing_offset = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -05002665
David Benjamin800046f2017-10-17 01:37:51 -04002666 unsigned mtu = 0; // max DTLS packet size
David Benjamin593047f2015-05-08 13:08:52 -04002667
David Benjaminc11ea9422017-08-29 16:33:21 -04002668 // num_timeouts is the number of times the retransmit timer has fired since
2669 // the last time it was reset.
David Benjamin800046f2017-10-17 01:37:51 -04002670 unsigned num_timeouts = 0;
David Benjamin593047f2015-05-08 13:08:52 -04002671
David Benjaminc11ea9422017-08-29 16:33:21 -04002672 // Indicates when the last handshake msg or heartbeat sent will
2673 // timeout.
David Benjamin800046f2017-10-17 01:37:51 -04002674 struct OPENSSL_timeval next_timeout = {0, 0};
David Benjamin593047f2015-05-08 13:08:52 -04002675
David Benjaminc11ea9422017-08-29 16:33:21 -04002676 // timeout_duration_ms is the timeout duration in milliseconds.
David Benjamin800046f2017-10-17 01:37:51 -04002677 unsigned timeout_duration_ms = 0;
David Benjamin86e95b82017-07-18 16:34:25 -04002678};
David Benjamin593047f2015-05-08 13:08:52 -04002679
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002680// SSL_CONFIG contains configuration bits that can be shed after the handshake
2681// completes. Objects of this type are not shared; they are unique to a
2682// particular |SSL|.
2683//
2684// See SSL_shed_handshake_config() for more about the conditions under which
2685// configuration can be shed.
2686struct SSL_CONFIG {
2687 explicit SSL_CONFIG(SSL *ssl_arg);
2688 ~SSL_CONFIG();
David Benjamin3f2611a2017-02-06 13:38:26 -05002689
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002690 // ssl is a non-owning pointer to the parent |SSL| object.
2691 SSL *const ssl = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002692
David Benjaminc11ea9422017-08-29 16:33:21 -04002693 // conf_max_version is the maximum acceptable protocol version configured by
2694 // |SSL_set_max_proto_version|. Note this version is normalized in DTLS and is
2695 // further constrainted by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002696 uint16_t conf_max_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002697
David Benjaminc11ea9422017-08-29 16:33:21 -04002698 // conf_min_version is the minimum acceptable protocol version configured by
2699 // |SSL_set_min_proto_version|. Note this version is normalized in DTLS and is
2700 // further constrainted by |SSL_OP_NO_*|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002701 uint16_t conf_min_version = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002702
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002703 X509_VERIFY_PARAM *param = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002704
David Benjaminc11ea9422017-08-29 16:33:21 -04002705 // crypto
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002706 SSLCipherPreferenceList *cipher_list = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002707
Adam Langley3fe8fa72018-01-26 09:14:30 -08002708 // This is used to hold the local certificate used (i.e. the server
2709 // certificate for a server or the client certificate for a client).
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002710 CERT *cert = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002711
2712 int (*verify_callback)(int ok,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002713 X509_STORE_CTX *ctx) =
2714 nullptr; // fail if callback returns 0
David Benjamin3f2611a2017-02-06 13:38:26 -05002715
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002716 enum ssl_verify_result_t (*custom_verify_callback)(
2717 SSL *ssl, uint8_t *out_alert) = nullptr;
David Benjaminc11ea9422017-08-29 16:33:21 -04002718 // Server-only: psk_identity_hint is the identity hint to send in
2719 // PSK-based key exchanges.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002720 char *psk_identity_hint = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002721
2722 unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
2723 char *identity,
2724 unsigned int max_identity_len,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002725 uint8_t *psk,
2726 unsigned int max_psk_len) = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002727 unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002728 uint8_t *psk,
2729 unsigned int max_psk_len) = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002730
David Benjaminc11ea9422017-08-29 16:33:21 -04002731 // for server side, keep the list of CA_dn we can use
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002732 STACK_OF(CRYPTO_BUFFER) *client_CA = nullptr;
Adam Langley34b4c822017-02-02 10:57:17 -08002733
David Benjaminc11ea9422017-08-29 16:33:21 -04002734 // cached_x509_client_CA is a cache of parsed versions of the elements of
2735 // |client_CA|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002736 STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002737
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002738 uint16_t dummy_pq_padding_len = 0;
2739 size_t supported_group_list_len = 0;
2740 uint16_t *supported_group_list = nullptr; // our list
David Benjamin3f2611a2017-02-06 13:38:26 -05002741
David Benjaminc11ea9422017-08-29 16:33:21 -04002742 // The client's Channel ID private key.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002743 EVP_PKEY *tlsext_channel_id_private = nullptr;
David Benjamin3f2611a2017-02-06 13:38:26 -05002744
David Benjaminc11ea9422017-08-29 16:33:21 -04002745 // For a client, this contains the list of supported protocols in wire
2746 // format.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002747 uint8_t *alpn_client_proto_list = nullptr;
2748 unsigned alpn_client_proto_list_len = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002749
Nick Harper36fcc4c2017-09-21 15:02:22 -07002750 // Contains a list of supported Token Binding key parameters.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002751 uint8_t *token_binding_params = nullptr;
2752 size_t token_binding_params_len = 0;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002753
Nick Harper3c034b22017-12-22 15:50:43 -08002754 // Contains the QUIC transport params that this endpoint will send.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002755 uint8_t *quic_transport_params = nullptr;
2756 size_t quic_transport_params_len = 0;
David Benjamin3f2611a2017-02-06 13:38:26 -05002757
David Benjaminb95d4b42018-05-02 16:11:34 -04002758 // srtp_profiles is the list of configured SRTP protection profiles for
2759 // DTLS-SRTP.
2760 STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles = nullptr;
2761
David Benjaminc11ea9422017-08-29 16:33:21 -04002762 // verify_mode is a bitmask of |SSL_VERIFY_*| values.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002763 uint8_t verify_mode = SSL_VERIFY_NONE;
David Benjamin3f2611a2017-02-06 13:38:26 -05002764
David Benjaminc11ea9422017-08-29 16:33:21 -04002765 // Enable signed certificate time stamps. Currently client only.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002766 bool signed_cert_timestamps_enabled:1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002767
David Benjaminc11ea9422017-08-29 16:33:21 -04002768 // ocsp_stapling_enabled is only used by client connections and indicates
2769 // whether OCSP stapling will be requested.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002770 bool ocsp_stapling_enabled:1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002771
David Benjaminc11ea9422017-08-29 16:33:21 -04002772 // tlsext_channel_id_enabled is copied from the |SSL_CTX|. For a server,
2773 // means that we'll accept Channel IDs from clients. For a client, means that
2774 // we'll advertise support.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002775 bool tlsext_channel_id_enabled:1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002776
David Benjaminc11ea9422017-08-29 16:33:21 -04002777 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
2778 // hash of the peer's certificate and then discard it to save memory and
2779 // session space. Only effective on the server side.
David Benjamin7e58c5e2017-10-11 13:01:08 -04002780 bool retain_only_sha256_of_client_certs:1;
Adam Langley3fe8fa72018-01-26 09:14:30 -08002781
2782 // handoff indicates that a server should stop after receiving the
2783 // ClientHello and pause the handshake in such a way that |SSL_get_error|
2784 // returns |SSL_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX|
2785 // element of the same name and may be cleared if the handoff is declined.
2786 bool handoff:1;
Adam Langley8df8e642018-02-28 13:35:39 -08002787
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002788 // shed_handshake_config indicates that the handshake config (this object!)
2789 // should be freed after the handshake completes.
2790 bool shed_handshake_config : 1;
2791};
2792
2793// SSLConnection backs the public |SSL| type. Due to compatibility constraints,
2794// it is a base class for |ssl_st|.
2795struct SSLConnection {
2796 // method is the method table corresponding to the current protocol (DTLS or
2797 // TLS).
2798 const SSL_PROTOCOL_METHOD *method;
2799
2800 // config is a container for handshake configuration. Accesses to this field
2801 // should check for nullptr, since configuration may be shed after the
2802 // handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use
2803 // that instead, and skip the null check.)
2804 SSL_CONFIG *config;
2805
2806 // version is the protocol version.
2807 uint16_t version;
2808
2809 uint16_t max_send_fragment;
2810
2811 // There are 2 BIO's even though they are normally both the same. This is so
2812 // data can be read and written to different handlers
2813
2814 BIO *rbio; // used by SSL_read
2815 BIO *wbio; // used by SSL_write
2816
2817 // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
2818 // Otherwise, it returns a value corresponding to what operation is needed to
2819 // progress.
2820 enum ssl_hs_wait_t (*do_handshake)(SSL_HANDSHAKE *hs);
2821
2822 SSL3_STATE *s3; // SSLv3 variables
2823 DTLS1_STATE *d1; // DTLSv1 variables
2824
2825 // callback that allows applications to peek at protocol messages
2826 void (*msg_callback)(int write_p, int version, int content_type,
2827 const void *buf, size_t len, SSL *ssl, void *arg);
2828 void *msg_callback_arg;
2829
2830 // session info
2831
2832 // initial_timeout_duration_ms is the default DTLS timeout duration in
2833 // milliseconds. It's used to initialize the timer any time it's restarted.
2834 unsigned initial_timeout_duration_ms;
2835
2836 // tls13_variant is the variant of TLS 1.3 we are using for this
2837 // configuration.
2838 enum tls13_variant_t tls13_variant;
2839
2840 // session is the configured session to be offered by the client. This session
2841 // is immutable.
2842 SSL_SESSION *session;
2843
2844 void (*info_callback)(const SSL *ssl, int type, int value);
2845
2846 SSL_CTX *ctx;
2847
David Benjamin98472cb2018-05-02 16:05:36 -04002848 // session_ctx is the |SSL_CTX| used for the session cache and related
2849 // settings.
2850 SSL_CTX *session_ctx;
2851
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002852 // extra application data
2853 CRYPTO_EX_DATA ex_data;
2854
2855 uint32_t options; // protocol behaviour
2856 uint32_t mode; // API behaviour
2857 uint32_t max_cert_list;
2858 char *tlsext_hostname;
2859
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002860 // renegotiate_mode controls how peer renegotiation attempts are handled.
2861 enum ssl_renegotiate_mode_t renegotiate_mode;
2862
2863 // server is true iff the this SSL* is the server half. Note: before the SSL*
2864 // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
2865 // the side is not determined. In this state, server is always false.
2866 bool server : 1;
2867
2868 // quiet_shutdown is true if the connection should not send a close_notify on
2869 // shutdown.
2870 bool quiet_shutdown : 1;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07002871
Adam Langley8df8e642018-02-28 13:35:39 -08002872 // did_dummy_pq_padding is only valid for a client. In that context, it is
2873 // true iff the client observed the server echoing a dummy PQ padding
2874 // extension.
2875 bool did_dummy_pq_padding:1;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002876
2877 // If enable_early_data is true, early data can be sent and accepted.
2878 bool enable_early_data : 1;
David Benjamin3f2611a2017-02-06 13:38:26 -05002879};
2880
David Benjaminc11ea9422017-08-29 16:33:21 -04002881// From draft-ietf-tls-tls13-18, used in determining PSK modes.
Steven Valdeza833c352016-11-01 13:39:36 -04002882#define SSL_PSK_DHE_KE 0x1
Steven Valdez1e6f11a2016-07-27 11:10:52 -04002883
David Benjaminc11ea9422017-08-29 16:33:21 -04002884// From draft-ietf-tls-tls13-16, used in determining whether to respond with a
2885// KeyUpdate.
Steven Valdezc4aa7272016-10-03 12:25:56 -04002886#define SSL_KEY_UPDATE_NOT_REQUESTED 0
2887#define SSL_KEY_UPDATE_REQUESTED 1
2888
David Benjaminc11ea9422017-08-29 16:33:21 -04002889// kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
2890// data that will be accepted. This value should be slightly below
2891// kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
Alessandro Ghedinide254b42017-04-17 19:12:33 +01002892static const size_t kMaxEarlyDataAccepted = 14336;
2893
David Benjamine325c3f2018-04-12 16:11:15 -04002894UniquePtr<CERT> ssl_cert_dup(CERT *cert);
David Benjamin27e377e2017-07-31 19:09:42 -04002895void ssl_cert_clear_certs(CERT *cert);
David Benjaminee910bf2017-07-25 22:36:00 -04002896int ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
Adam Langley52940c42017-02-01 12:40:31 -08002897int ssl_is_key_type_supported(int key_type);
David Benjaminc11ea9422017-08-29 16:33:21 -04002898// ssl_compare_public_and_private_key returns one if |pubkey| is the public
2899// counterpart to |privkey|. Otherwise it returns zero and pushes a helpful
2900// message on the error queue.
Adam Langleyc26692c2017-01-25 09:34:42 -08002901int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
2902 const EVP_PKEY *privkey);
Adam Langley3a2b47a2017-01-24 13:59:42 -08002903int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey);
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002904int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002905int ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, const SSL_SESSION *session);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002906int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002907
David Benjaminc11ea9422017-08-29 16:33:21 -04002908// ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
2909// error.
David Benjamin31b0c9b2017-07-20 14:49:15 -04002910UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
Adam Langley46db7af2017-02-01 15:49:37 -08002911
David Benjamin58150ed2018-06-29 23:58:43 -04002912// ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table
2913// keyed on session IDs.
2914uint32_t ssl_hash_session_id(Span<const uint8_t> session_id);
2915
David Benjaminc11ea9422017-08-29 16:33:21 -04002916// SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
2917// the parsed data.
David Benjamin31b0c9b2017-07-20 14:49:15 -04002918UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
2919 const SSL_X509_METHOD *x509_method,
2920 CRYPTO_BUFFER_POOL *pool);
Adam Langley46db7af2017-02-01 15:49:37 -08002921
Adam Langleye8d24392018-01-25 15:49:37 -08002922// ssl_session_serialize writes |in| to |cbb| as if it were serialising a
2923// session for Session-ID resumption. It returns one on success and zero on
2924// error.
2925int ssl_session_serialize(const SSL_SESSION *in, CBB *cbb);
2926
David Benjaminc11ea9422017-08-29 16:33:21 -04002927// ssl_session_is_context_valid returns one if |session|'s session ID context
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002928// matches the one set on |hs| and zero otherwise.
2929int ssl_session_is_context_valid(const SSL_HANDSHAKE *hs,
2930 const SSL_SESSION *session);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002931
David Benjaminc11ea9422017-08-29 16:33:21 -04002932// ssl_session_is_time_valid returns one if |session| is still valid and zero if
2933// it has expired.
Steven Valdez4aa154e2016-07-29 14:32:55 -04002934int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
2935
David Benjaminc11ea9422017-08-29 16:33:21 -04002936// ssl_session_is_resumable returns one if |session| is resumable for |hs| and
2937// zero otherwise.
David Benjamin45738dd2017-02-09 20:01:26 -05002938int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
2939 const SSL_SESSION *session);
David Benjamin75f99142016-11-12 12:36:06 +09002940
David Benjamina4bafd32017-10-03 15:06:29 -04002941// ssl_session_protocol_version returns the protocol version associated with
2942// |session|. Note that despite the name, this is not the same as
2943// |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
2944uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
Steven Valdez8f36c512017-06-20 10:55:02 -04002945
David Benjamina4bafd32017-10-03 15:06:29 -04002946// ssl_session_get_digest returns the digest used in |session|.
2947const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
Steven Valdez908ac192017-01-12 13:17:07 -05002948
David Benjamin4d0be242016-09-01 01:10:07 -04002949void ssl_set_session(SSL *ssl, SSL_SESSION *session);
2950
David Benjaminc11ea9422017-08-29 16:33:21 -04002951// ssl_get_prev_session looks up the previous session based on |client_hello|.
2952// On success, it sets |*out_session| to the session or nullptr if none was
2953// found. If the session could not be looked up synchronously, it returns
2954// |ssl_hs_pending_session| and should be called again. If a ticket could not be
2955// decrypted immediately it returns |ssl_hs_pending_ticket| and should also
2956// be called again. Otherwise, it returns |ssl_hs_error|.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002957enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs,
Steven Valdez398085b2017-08-29 13:25:56 -04002958 UniquePtr<SSL_SESSION> *out_session,
David Benjaminfd45ee72017-08-31 14:49:09 -04002959 bool *out_tickets_supported,
2960 bool *out_renew_ticket,
Steven Valdez398085b2017-08-29 13:25:56 -04002961 const SSL_CLIENT_HELLO *client_hello);
David Benjamine3aa1d92015-06-16 15:34:50 -04002962
David Benjaminc11ea9422017-08-29 16:33:21 -04002963// The following flags determine which parts of the session are duplicated.
Steven Valdez4aa154e2016-07-29 14:32:55 -04002964#define SSL_SESSION_DUP_AUTH_ONLY 0x0
2965#define SSL_SESSION_INCLUDE_TICKET 0x1
2966#define SSL_SESSION_INCLUDE_NONAUTH 0x2
2967#define SSL_SESSION_DUP_ALL \
2968 (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
2969
David Benjaminc11ea9422017-08-29 16:33:21 -04002970// SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
2971// fields in |session| or nullptr on error. The new session is non-resumable and
2972// must be explicitly marked resumable once it has been filled in.
David Benjamin31b0c9b2017-07-20 14:49:15 -04002973OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
2974 int dup_flags);
Steven Valdez87eab492016-06-27 16:34:59 -04002975
David Benjaminc11ea9422017-08-29 16:33:21 -04002976// ssl_session_rebase_time updates |session|'s start time to the current time,
2977// adjusting the timeout so the expiration time is unchanged.
David Benjamin17b30832017-01-28 14:00:32 -05002978void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
2979
David Benjaminc11ea9422017-08-29 16:33:21 -04002980// ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
2981// |session|'s timeout to |timeout| (measured from the current time). The
2982// renewal is clamped to the session's auth_timeout.
David Benjaminad8f5e12017-02-20 17:00:20 -05002983void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
2984 uint32_t timeout);
David Benjamin123db572016-11-03 16:59:25 -04002985
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002986void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
David Benjaminf31e6812014-11-13 18:05:55 -05002987
David Benjamind1e3ce12017-10-06 18:31:15 -04002988int ssl_send_alert(SSL *ssl, int level, int desc);
David Benjamin7934f082017-08-01 16:32:25 -04002989bool ssl3_get_message(SSL *ssl, SSLMessage *out);
David Benjamind9229f92017-10-06 17:36:20 -04002990ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
2991 uint8_t *out_alert, Span<uint8_t> in);
David Benjamin8f94c312017-08-01 17:35:55 -04002992void ssl3_next_message(SSL *ssl);
David Benjamin854dd652014-08-26 00:32:30 -04002993
David Benjamin0d56f882015-12-19 17:05:56 -05002994int ssl3_dispatch_alert(SSL *ssl);
David Benjamind9229f92017-10-06 17:36:20 -04002995ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
2996 size_t *out_consumed, uint8_t *out_alert,
2997 Span<uint8_t> in);
2998ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2999 uint8_t *out_alert,
3000 Span<uint8_t> in);
David Benjamin74795b32017-08-31 15:13:12 -04003001int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
Steven Valdeze831a812017-03-09 14:56:07 -05003002 int len);
Steven Valdezb6b6ff32016-10-26 11:56:35 -04003003
David Benjamin97250f42017-10-07 04:12:35 -04003004bool ssl3_new(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05003005void ssl3_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003006
David Benjamin97250f42017-10-07 04:12:35 -04003007bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
3008bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
3009bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg);
3010bool ssl3_add_change_cipher_spec(SSL *ssl);
3011bool ssl3_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
David Benjamindaf207a2017-01-03 18:37:41 -05003012int ssl3_flush_flight(SSL *ssl);
David Benjamin75836432016-06-17 18:48:29 -04003013
David Benjamin97250f42017-10-07 04:12:35 -04003014bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
3015bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
3016bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
3017bool dtls1_add_change_cipher_spec(SSL *ssl);
3018bool dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
David Benjamin1a999cf2017-01-03 10:30:35 -05003019int dtls1_flush_flight(SSL *ssl);
David Benjaminc6604172016-06-02 16:38:35 -04003020
David Benjaminc11ea9422017-08-29 16:33:21 -04003021// ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
David Benjamin97250f42017-10-07 04:12:35 -04003022// the pending flight. It returns true on success and false on error.
3023bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
Steven Valdez5eead162016-11-11 22:23:25 -05003024
David Benjamin97250f42017-10-07 04:12:35 -04003025// ssl_hash_message incorporates |msg| into the handshake hash. It returns true
3026// on success and false on allocation failure.
David Benjamin7934f082017-08-01 16:32:25 -04003027bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
David Benjaminced94792016-11-14 17:12:11 +09003028
David Benjamind9229f92017-10-06 17:36:20 -04003029ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
3030 size_t *out_consumed, uint8_t *out_alert,
3031 Span<uint8_t> in);
3032ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
3033 uint8_t *out_alert,
3034 Span<uint8_t> in);
Adam Langley95c29f32014-06-20 12:00:00 -07003035
David Benjamin74795b32017-08-31 15:13:12 -04003036int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
3037 const uint8_t *buf, int len);
David Benjamin45d45c12016-06-07 15:20:49 -04003038
David Benjaminc11ea9422017-08-29 16:33:21 -04003039// dtls1_write_record sends a record. It returns one on success and <= 0 on
3040// error.
David Benjamin45d45c12016-06-07 15:20:49 -04003041int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
3042 enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -07003043
David Benjaminaad50db2016-06-23 17:49:12 -04003044int dtls1_retransmit_outgoing_messages(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04003045bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
David Benjaminc6604172016-06-02 16:38:35 -04003046 CBS *out_body);
David Benjamin97250f42017-10-07 04:12:35 -04003047bool dtls1_check_timeout_num(SSL *ssl);
David Benjamin2fa83de2015-02-08 01:40:08 -05003048
David Benjamin0d56f882015-12-19 17:05:56 -05003049void dtls1_start_timer(SSL *ssl);
3050void dtls1_stop_timer(SSL *ssl);
David Benjamin97250f42017-10-07 04:12:35 -04003051bool dtls1_is_timer_expired(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003052unsigned int dtls1_min_mtu(void);
3053
David Benjamin97250f42017-10-07 04:12:35 -04003054bool dtls1_new(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05003055void dtls1_free(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003056
David Benjamin7934f082017-08-01 16:32:25 -04003057bool dtls1_get_message(SSL *ssl, SSLMessage *out);
David Benjamind9229f92017-10-06 17:36:20 -04003058ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
3059 uint8_t *out_alert, Span<uint8_t> in);
David Benjamin8f94c312017-08-01 17:35:55 -04003060void dtls1_next_message(SSL *ssl);
David Benjamin0d56f882015-12-19 17:05:56 -05003061int dtls1_dispatch_alert(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07003062
Adam Langley3fe8fa72018-01-26 09:14:30 -08003063// tls1_configure_aead configures either the read or write direction AEAD (as
3064// determined by |direction|) using the keys generated by the TLS KDF. The
3065// |key_block_cache| argument is used to store the generated key block, if
3066// empty. Otherwise it's assumed that the key block is already contained within
3067// it. Returns one on success or zero on error.
3068int tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction,
3069 Array<uint8_t> *key_block_cache,
3070 const SSL_CIPHER *cipher,
3071 Span<const uint8_t> iv_override);
3072
David Benjamine58f8a62017-09-21 19:07:15 -04003073int tls1_change_cipher_state(SSL_HANDSHAKE *hs, evp_aead_direction_t direction);
Steven Valdez908ac192017-01-12 13:17:07 -05003074int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
David Benjamin2637f3c2017-10-27 00:56:55 -04003075 Span<const uint8_t> premaster);
Adam Langley95c29f32014-06-20 12:00:00 -07003076
David Benjamincf0ce672017-09-21 02:25:59 -04003077// tls1_get_grouplist returns the locally-configured group preference list.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003078Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl);
Steven Valdez5440fe02016-07-18 12:40:30 -04003079
David Benjaminc11ea9422017-08-29 16:33:21 -04003080// tls1_check_group_id returns one if |group_id| is consistent with
3081// locally-configured group preferences.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003082int tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id);
Sigbjorn Vik2b23d242015-06-29 15:07:26 +02003083
David Benjaminc11ea9422017-08-29 16:33:21 -04003084// tls1_get_shared_group sets |*out_group_id| to the first preferred shared
3085// group between client and server preferences and returns one. If none may be
3086// found, it returns zero.
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003087int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
David Benjamin072334d2014-07-13 16:24:27 -04003088
David Benjaminc11ea9422017-08-29 16:33:21 -04003089// tls1_set_curves converts the array of |ncurves| NIDs pointed to by |curves|
3090// into a newly allocated array of TLS group IDs. On success, the function
3091// returns one and writes the array to |*out_group_ids| and its size to
3092// |*out_group_ids_len|. Otherwise, it returns zero.
Steven Valdezce902a92016-05-17 11:47:53 -04003093int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -08003094 const int *curves, size_t ncurves);
David Benjamin072334d2014-07-13 16:24:27 -04003095
David Benjaminc11ea9422017-08-29 16:33:21 -04003096// tls1_set_curves_list converts the string of curves pointed to by |curves|
3097// into a newly allocated array of TLS group IDs. On success, the function
3098// returns one and writes the array to |*out_group_ids| and its size to
3099// |*out_group_ids_len|. Otherwise, it returns zero.
Alessandro Ghedini5fd18072016-09-28 21:04:25 +01003100int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
3101 const char *curves);
3102
David Benjaminc11ea9422017-08-29 16:33:21 -04003103// ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It
3104// returns one on success and zero on failure. The |header_len| argument is the
3105// length of the ClientHello written so far and is used to compute the padding
3106// length. (It does not include the record header.)
David Benjamin8c880a22016-12-03 02:20:34 -05003107int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
David Benjamine8d53502015-10-10 14:13:23 -04003108
David Benjamin8c880a22016-12-03 02:20:34 -05003109int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
David Benjamin731058e2016-12-03 23:15:13 -05003110int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3111 const SSL_CLIENT_HELLO *client_hello);
David Benjamin8c880a22016-12-03 02:20:34 -05003112int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
Adam Langley95c29f32014-06-20 12:00:00 -07003113
Adam Langleyfcf25832014-12-18 17:42:32 -08003114#define tlsext_tick_md EVP_sha256
David Benjamine3aa1d92015-06-16 15:34:50 -04003115
David Benjaminc11ea9422017-08-29 16:33:21 -04003116// ssl_process_ticket processes a session ticket from the client. It returns
3117// one of:
3118// |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
3119// |*out_renew_ticket| is set to whether the ticket should be renewed.
3120// |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
3121// fresh ticket should be sent, but the given ticket cannot be used.
3122// |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
3123// Retry later.
3124// |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
Adam Langley4c341d02017-03-08 19:33:21 -08003125enum ssl_ticket_aead_result_t ssl_process_ticket(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003126 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
3127 bool *out_renew_ticket, const uint8_t *ticket, size_t ticket_len,
3128 const uint8_t *session_id, size_t session_id_len);
Adam Langley95c29f32014-06-20 12:00:00 -07003129
David Benjaminc11ea9422017-08-29 16:33:21 -04003130// tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
3131// the signature. If the key is valid, it saves the Channel ID and returns
3132// one. Otherwise, it returns zero.
David Benjamin7934f082017-08-01 16:32:25 -04003133int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Nick Harper60a85cb2016-09-23 16:25:11 -07003134
David Benjaminc11ea9422017-08-29 16:33:21 -04003135// tls1_write_channel_id generates a Channel ID message and puts the output in
3136// |cbb|. |ssl->tlsext_channel_id_private| must already be set before calling.
David Benjaminf1db1a32017-10-27 01:12:34 -04003137// This function returns true on success and false on error.
3138bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07003139
David Benjaminc11ea9422017-08-29 16:33:21 -04003140// tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
3141// it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
3142// one on success and zero on failure.
Steven Valdez908ac192017-01-12 13:17:07 -05003143int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003144
Steven Valdez908ac192017-01-12 13:17:07 -05003145int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
Adam Langley1258b6a2014-06-20 12:00:00 -07003146
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003147// ssl_do_channel_id_callback checks runs |hs->ssl->ctx->channel_id_cb| if
David Benjaminc11ea9422017-08-29 16:33:21 -04003148// necessary. It returns one on success and zero on fatal error. Note that, on
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003149// success, |hs->ssl->tlsext_channel_id_private| may be unset, in which case the
David Benjaminc11ea9422017-08-29 16:33:21 -04003150// operation should be retried later.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003151int ssl_do_channel_id_callback(SSL_HANDSHAKE *hs);
Nick Harper60a85cb2016-09-23 16:25:11 -07003152
David Benjaminc11ea9422017-08-29 16:33:21 -04003153// ssl_can_write returns one if |ssl| is allowed to write and zero otherwise.
Steven Valdez681eb6a2016-12-19 13:19:29 -05003154int ssl_can_write(const SSL *ssl);
3155
David Benjaminc11ea9422017-08-29 16:33:21 -04003156// ssl_can_read returns one if |ssl| is allowed to read and zero otherwise.
Steven Valdez681eb6a2016-12-19 13:19:29 -05003157int ssl_can_read(const SSL *ssl);
3158
David Benjaminad8f5e12017-02-20 17:00:20 -05003159void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003160void ssl_ctx_get_current_time(const SSL_CTX *ctx,
3161 struct OPENSSL_timeval *out_clock);
David Benjamin721e8b72016-08-03 13:13:17 -04003162
David Benjaminc11ea9422017-08-29 16:33:21 -04003163// ssl_reset_error_state resets state for |SSL_get_error|.
David Benjamin2be4aa72017-01-02 07:56:51 -05003164void ssl_reset_error_state(SSL *ssl);
3165
David Benjamin31aad2d2017-10-06 16:04:06 -04003166// ssl_set_read_error sets |ssl|'s read half into an error state, saving the
3167// current state of the error queue.
3168void ssl_set_read_error(SSL* ssl);
3169
David Benjamin86e95b82017-07-18 16:34:25 -04003170} // namespace bssl
Adam Langley11d11d62017-07-17 11:10:46 -07003171
Steven Valdez87eab492016-06-27 16:34:59 -04003172
David Benjaminc11ea9422017-08-29 16:33:21 -04003173// Opaque C types.
3174//
3175// The following types are exported to C code as public typedefs, so they must
3176// be defined outside of the namespace.
David Benjamine39ac8f2017-07-20 12:22:21 -04003177
David Benjaminc11ea9422017-08-29 16:33:21 -04003178// ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
3179// structure to support the legacy version-locked methods.
David Benjamine39ac8f2017-07-20 12:22:21 -04003180struct ssl_method_st {
David Benjaminc11ea9422017-08-29 16:33:21 -04003181 // version, if non-zero, is the only protocol version acceptable to an
3182 // SSL_CTX initialized from this method.
David Benjamine39ac8f2017-07-20 12:22:21 -04003183 uint16_t version;
David Benjaminc11ea9422017-08-29 16:33:21 -04003184 // method is the underlying SSL_PROTOCOL_METHOD that initializes the
3185 // SSL_CTX.
David Benjamin664e99a2017-10-10 19:23:21 -04003186 const bssl::SSL_PROTOCOL_METHOD *method;
David Benjaminc11ea9422017-08-29 16:33:21 -04003187 // x509_method contains pointers to functions that might deal with |X509|
3188 // compatibility, or might be a no-op, depending on the application.
David Benjamin0363de92018-06-29 13:57:38 -04003189 const bssl::SSL_X509_METHOD *x509_method;
David Benjamine39ac8f2017-07-20 12:22:21 -04003190};
3191
David Benjamin664e99a2017-10-10 19:23:21 -04003192// The following types back public C-exposed types which must live in the global
3193// namespace. We use subclassing so the implementations may be C++ types with
3194// methods and destructor without polluting the global namespace.
3195struct ssl_ctx_st : public bssl::SSLContext {};
David Benjamine39ac8f2017-07-20 12:22:21 -04003196struct ssl_st : public bssl::SSLConnection {};
3197
David Benjamin1c68fa22018-06-02 19:02:46 -04003198struct ssl_session_st {
David Benjaminbfdd1a92018-06-29 16:26:38 -04003199 explicit ssl_session_st(const bssl::SSL_X509_METHOD *method);
3200 ssl_session_st(const ssl_session_st &) = delete;
3201 ssl_session_st &operator=(const ssl_session_st &) = delete;
3202
3203 CRYPTO_refcount_t references = 1;
3204 uint16_t ssl_version = 0; // what ssl version session info is being kept in here?
David Benjamin1c68fa22018-06-02 19:02:46 -04003205
3206 // group_id is the ID of the ECDH group used to establish this session or zero
3207 // if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003208 uint16_t group_id = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003209
3210 // peer_signature_algorithm is the signature algorithm used to authenticate
3211 // the peer, or zero if not applicable or unknown.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003212 uint16_t peer_signature_algorithm = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003213
3214 // master_key, in TLS 1.2 and below, is the master secret associated with the
3215 // session. In TLS 1.3 and up, it is the resumption secret.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003216 int master_key_length = 0;
3217 uint8_t master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003218
3219 // session_id - valid?
David Benjaminbfdd1a92018-06-29 16:26:38 -04003220 unsigned session_id_length = 0;
3221 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003222 // this is used to determine whether the session is being reused in
3223 // the appropriate context. It is up to the application to set this,
3224 // via SSL_new
David Benjaminbfdd1a92018-06-29 16:26:38 -04003225 uint8_t sid_ctx_length = 0;
3226 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003227
David Benjaminbfdd1a92018-06-29 16:26:38 -04003228 bssl::UniquePtr<char> psk_identity;
David Benjamin1c68fa22018-06-02 19:02:46 -04003229
3230 // certs contains the certificate chain from the peer, starting with the leaf
3231 // certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003232 bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs;
David Benjamin1c68fa22018-06-02 19:02:46 -04003233
David Benjaminbfdd1a92018-06-29 16:26:38 -04003234 const bssl::SSL_X509_METHOD *x509_method = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003235
3236 // x509_peer is the peer's certificate.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003237 X509 *x509_peer = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003238
3239 // x509_chain is the certificate chain sent by the peer. NOTE: for historical
3240 // reasons, when a client (so the peer is a server), the chain includes
3241 // |peer|, but when a server it does not.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003242 STACK_OF(X509) *x509_chain = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003243
3244 // x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that
3245 // omits the leaf certificate. This exists because OpenSSL, historically,
3246 // didn't include the leaf certificate in the chain for a server, but did for
3247 // a client. The |x509_chain| always includes it and, if an API call requires
3248 // a chain without, it is stored here.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003249 STACK_OF(X509) *x509_chain_without_leaf = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003250
3251 // verify_result is the result of certificate verification in the case of
3252 // non-fatal certificate errors.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003253 long verify_result = X509_V_ERR_INVALID_CALL;
David Benjamin1c68fa22018-06-02 19:02:46 -04003254
3255 // timeout is the lifetime of the session in seconds, measured from |time|.
3256 // This is renewable up to |auth_timeout|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003257 uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003258
3259 // auth_timeout is the non-renewable lifetime of the session in seconds,
3260 // measured from |time|.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003261 uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
David Benjamin1c68fa22018-06-02 19:02:46 -04003262
3263 // time is the time the session was issued, measured in seconds from the UNIX
3264 // epoch.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003265 uint64_t time = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003266
David Benjaminbfdd1a92018-06-29 16:26:38 -04003267 const SSL_CIPHER *cipher = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003268
3269 CRYPTO_EX_DATA ex_data; // application specific data
3270
3271 // These are used to make removal of session-ids more efficient and to
3272 // implement a maximum cache size.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003273 SSL_SESSION *prev = nullptr, *next = nullptr;
David Benjamin1c68fa22018-06-02 19:02:46 -04003274
David Benjaminbfdd1a92018-06-29 16:26:38 -04003275 bssl::Array<uint8_t> ticket;
David Benjamin1c68fa22018-06-02 19:02:46 -04003276
David Benjaminbfdd1a92018-06-29 16:26:38 -04003277 bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list;
David Benjamin1c68fa22018-06-02 19:02:46 -04003278
3279 // The OCSP response that came with the session.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003280 bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response;
David Benjamin1c68fa22018-06-02 19:02:46 -04003281
3282 // peer_sha256 contains the SHA-256 hash of the peer's certificate if
3283 // |peer_sha256_valid| is true.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003284 uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0};
David Benjamin1c68fa22018-06-02 19:02:46 -04003285
3286 // original_handshake_hash contains the handshake hash (either SHA-1+MD5 or
3287 // SHA-2, depending on TLS version) for the original, full handshake that
3288 // created a session. This is used by Channel IDs during resumption.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003289 uint8_t original_handshake_hash[EVP_MAX_MD_SIZE] = {0};
3290 uint8_t original_handshake_hash_len = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003291
David Benjaminbfdd1a92018-06-29 16:26:38 -04003292 uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds
David Benjamin1c68fa22018-06-02 19:02:46 -04003293
David Benjaminbfdd1a92018-06-29 16:26:38 -04003294 uint32_t ticket_age_add = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003295
3296 // ticket_max_early_data is the maximum amount of data allowed to be sent as
3297 // early data. If zero, 0-RTT is disallowed.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003298 uint32_t ticket_max_early_data = 0;
David Benjamin1c68fa22018-06-02 19:02:46 -04003299
3300 // early_alpn is the ALPN protocol from the initial handshake. This is only
3301 // stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT
3302 // resumptions.
David Benjaminbfdd1a92018-06-29 16:26:38 -04003303 bssl::Array<uint8_t> early_alpn;
David Benjamin1c68fa22018-06-02 19:02:46 -04003304
David Benjamina3a71e92018-06-29 13:24:45 -04003305 // extended_master_secret is whether the master secret in this session was
David Benjamin1c68fa22018-06-02 19:02:46 -04003306 // generated using EMS and thus isn't vulnerable to the Triple Handshake
3307 // attack.
David Benjamina3a71e92018-06-29 13:24:45 -04003308 bool extended_master_secret:1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003309
David Benjamina3a71e92018-06-29 13:24:45 -04003310 // peer_sha256_valid is whether |peer_sha256| is valid.
3311 bool peer_sha256_valid:1; // Non-zero if peer_sha256 is valid
David Benjamin1c68fa22018-06-02 19:02:46 -04003312
3313 // not_resumable is used to indicate that session resumption is disallowed.
David Benjamina3a71e92018-06-29 13:24:45 -04003314 bool not_resumable:1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003315
David Benjamina3a71e92018-06-29 13:24:45 -04003316 // ticket_age_add_valid is whether |ticket_age_add| is valid.
3317 bool ticket_age_add_valid:1;
David Benjamin1c68fa22018-06-02 19:02:46 -04003318
David Benjamina3a71e92018-06-29 13:24:45 -04003319 // is_server is whether this session was created by a server.
3320 bool is_server:1;
David Benjaminbfdd1a92018-06-29 16:26:38 -04003321
3322 private:
3323 ~ssl_session_st();
3324 friend void SSL_SESSION_free(SSL_SESSION *);
David Benjamin1c68fa22018-06-02 19:02:46 -04003325};
3326
David Benjamine39ac8f2017-07-20 12:22:21 -04003327
David Benjaminc11ea9422017-08-29 16:33:21 -04003328#endif // OPENSSL_HEADER_SSL_INTERNAL_H