blob: 55bece98584af3f16791e7e4d54ce66d7a109170 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE.
140 */
141
Adam Langleye9ada862015-05-11 17:20:37 -0700142#ifndef OPENSSL_HEADER_SSL_INTERNAL_H
143#define OPENSSL_HEADER_SSL_INTERNAL_H
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144
145#include <openssl/base.h>
146
Robert Sloanfe7cd212017-08-07 09:03:39 -0700147#include <stdlib.h>
148
Robert Sloan4562e9d2017-10-02 10:26:51 -0700149#include <limits>
Robert Sloanfe7cd212017-08-07 09:03:39 -0700150#include <new>
Robert Sloanb6d070c2017-07-24 08:40:01 -0700151#include <type_traits>
152#include <utility>
153
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154#include <openssl/aead.h>
Robert Sloanb6d070c2017-07-24 08:40:01 -0700155#include <openssl/err.h>
Robert Sloan921ef2c2017-10-17 09:02:20 -0700156#include <openssl/lhash.h>
Robert Sloanb6d070c2017-07-24 08:40:01 -0700157#include <openssl/mem.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800158#include <openssl/ssl.h>
Robert Sloan4562e9d2017-10-02 10:26:51 -0700159#include <openssl/span.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800160#include <openssl/stack.h>
161
Robert Sloan921ef2c2017-10-17 09:02:20 -0700162#include "../crypto/err/internal.h"
Robert Sloan4562e9d2017-10-02 10:26:51 -0700163#include "../crypto/internal.h"
164
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400165
Adam Langleye9ada862015-05-11 17:20:37 -0700166#if defined(OPENSSL_WINDOWS)
Robert Sloana27a6a42017-09-05 08:39:28 -0700167// Windows defines struct timeval in winsock2.h.
David Benjamin6e899c72016-06-09 18:02:18 -0400168OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langleye9ada862015-05-11 17:20:37 -0700169#include <winsock2.h>
David Benjamin6e899c72016-06-09 18:02:18 -0400170OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langleye9ada862015-05-11 17:20:37 -0700171#else
Adam Langley4139edb2016-01-13 15:00:54 -0800172#include <sys/time.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700173#endif
174
David Benjaminc895d6b2016-08-11 13:26:41 -0400175
Robert Sloanb6d070c2017-07-24 08:40:01 -0700176namespace bssl {
177
178struct SSL_HANDSHAKE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700179struct SSL_PROTOCOL_METHOD;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700180
Robert Sloana27a6a42017-09-05 08:39:28 -0700181// C++ utilities.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700182
Robert Sloana27a6a42017-09-05 08:39:28 -0700183// New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It
184// returns nullptr on allocation error. It only implements single-object
185// allocation and not new T[n].
186//
187// Note: unlike |new|, this does not support non-public constructors.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700188template <typename T, typename... Args>
189T *New(Args &&... args) {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700190 void *t = OPENSSL_malloc(sizeof(T));
Robert Sloanb6d070c2017-07-24 08:40:01 -0700191 if (t == nullptr) {
192 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
193 return nullptr;
194 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700195 return new (t) T(std::forward<Args>(args)...);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700196}
197
Robert Sloana27a6a42017-09-05 08:39:28 -0700198// Delete behaves like |delete| but uses |OPENSSL_free| to release memory.
199//
200// Note: unlike |delete| this does not support non-public destructors.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700201template <typename T>
202void Delete(T *t) {
203 if (t != nullptr) {
204 t->~T();
205 OPENSSL_free(t);
206 }
207}
208
Robert Sloana27a6a42017-09-05 08:39:28 -0700209// All types with kAllowUniquePtr set may be used with UniquePtr. Other types
210// may be C structs which require a |BORINGSSL_MAKE_DELETER| registration.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700211namespace internal {
212template <typename T>
Robert Sloanfe7cd212017-08-07 09:03:39 -0700213struct DeleterImpl<T, typename std::enable_if<T::kAllowUniquePtr>::type> {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700214 static void Free(T *t) { Delete(t); }
215};
216}
217
Robert Sloana27a6a42017-09-05 08:39:28 -0700218// MakeUnique behaves like |std::make_unique| but returns nullptr on allocation
219// error.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700220template <typename T, typename... Args>
221UniquePtr<T> MakeUnique(Args &&... args) {
222 return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
223}
224
Robert Sloanfe7cd212017-08-07 09:03:39 -0700225#if defined(BORINGSSL_ALLOW_CXX_RUNTIME)
226#define HAS_VIRTUAL_DESTRUCTOR
227#define PURE_VIRTUAL = 0
228#else
Robert Sloana27a6a42017-09-05 08:39:28 -0700229// HAS_VIRTUAL_DESTRUCTOR should be declared in any base class which defines a
230// virtual destructor. This avoids a dependency on |_ZdlPv| and prevents the
231// class from being used with |delete|.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700232#define HAS_VIRTUAL_DESTRUCTOR \
233 void operator delete(void *) { abort(); }
234
Robert Sloana27a6a42017-09-05 08:39:28 -0700235// PURE_VIRTUAL should be used instead of = 0 when defining pure-virtual
236// functions. This avoids a dependency on |__cxa_pure_virtual| but loses
237// compile-time checking.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700238#define PURE_VIRTUAL { abort(); }
239#endif
240
Robert Sloan921ef2c2017-10-17 09:02:20 -0700241// CONSTEXPR_ARRAY works around a VS 2015 bug where ranged for loops don't work
242// on constexpr arrays.
243#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1910
244#define CONSTEXPR_ARRAY const
245#else
246#define CONSTEXPR_ARRAY constexpr
247#endif
248
Robert Sloan4562e9d2017-10-02 10:26:51 -0700249// Array<T> is an owning array of elements of |T|.
250template <typename T>
251class Array {
252 public:
253 // Array's default constructor creates an empty array.
254 Array() {}
255 Array(const Array &) = delete;
256 Array(Array &&other) { *this = std::move(other); }
257
258 ~Array() { Reset(); }
259
260 Array &operator=(const Array &) = delete;
261 Array &operator=(Array &&other) {
262 Reset();
263 other.Release(&data_, &size_);
264 return *this;
265 }
266
267 const T *data() const { return data_; }
268 T *data() { return data_; }
269 size_t size() const { return size_; }
Robert Sloanae1abf92017-10-05 12:50:08 -0700270 bool empty() const { return size_ == 0; }
Robert Sloan4562e9d2017-10-02 10:26:51 -0700271
272 const T &operator[](size_t i) const { return data_[i]; }
273 T &operator[](size_t i) { return data_[i]; }
274
275 T *begin() { return data_; }
276 const T *cbegin() const { return data_; }
277 T *end() { return data_ + size_; }
278 const T *cend() const { return data_ + size_; }
279
280 void Reset() { Reset(nullptr, 0); }
281
282 // Reset releases the current contents of the array and takes ownership of the
283 // raw pointer supplied by the caller.
284 void Reset(T *new_data, size_t new_size) {
285 for (size_t i = 0; i < size_; i++) {
286 data_[i].~T();
287 }
288 OPENSSL_free(data_);
289 data_ = new_data;
290 size_ = new_size;
291 }
292
293 // Release releases ownership of the array to a raw pointer supplied by the
294 // caller.
295 void Release(T **out, size_t *out_size) {
296 *out = data_;
297 *out_size = size_;
298 data_ = nullptr;
299 size_ = 0;
300 }
301
302 // Init replaces the array with a newly-allocated array of |new_size|
303 // default-constructed copies of |T|. It returns true on success and false on
304 // error.
305 //
306 // Note that if |T| is a primitive type like |uint8_t|, it is uninitialized.
307 bool Init(size_t new_size) {
308 Reset();
309 if (new_size == 0) {
310 return true;
311 }
312
313 if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) {
314 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
315 return false;
316 }
317 data_ = reinterpret_cast<T*>(OPENSSL_malloc(new_size * sizeof(T)));
318 if (data_ == nullptr) {
319 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
320 return false;
321 }
322 size_ = new_size;
323 for (size_t i = 0; i < size_; i++) {
324 new (&data_[i]) T;
325 }
326 return true;
327 }
328
329 // CopyFrom replaces the array with a newly-allocated copy of |in|. It returns
330 // true on success and false on error.
331 bool CopyFrom(Span<const uint8_t> in) {
332 if (!Init(in.size())) {
333 return false;
334 }
335 OPENSSL_memcpy(data_, in.data(), in.size());
336 return true;
337 }
338
339 private:
340 T *data_ = nullptr;
341 size_t size_ = 0;
342};
343
344// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
345bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
346
Robert Sloanf6200e72017-07-10 08:09:18 -0700347
Robert Sloana27a6a42017-09-05 08:39:28 -0700348// Protocol versions.
349//
350// Due to DTLS's historical wire version differences and to support multiple
351// variants of the same protocol during development, we maintain two notions of
352// version.
353//
354// The "version" or "wire version" is the actual 16-bit value that appears on
355// the wire. It uniquely identifies a version and is also used at API
356// boundaries. The set of supported versions differs between TLS and DTLS. Wire
357// versions are opaque values and may not be compared numerically.
358//
359// The "protocol version" identifies the high-level handshake variant being
360// used. DTLS versions map to the corresponding TLS versions. Draft TLS 1.3
361// variants all map to TLS 1.3. Protocol versions are sequential and may be
362// compared numerically.
Robert Sloanf6200e72017-07-10 08:09:18 -0700363
Robert Sloana27a6a42017-09-05 08:39:28 -0700364// ssl_protocol_version_from_wire sets |*out| to the protocol version
Robert Sloanae1abf92017-10-05 12:50:08 -0700365// corresponding to wire version |version| and returns true. If |version| is not
366// a valid TLS or DTLS version, it returns false.
Robert Sloana27a6a42017-09-05 08:39:28 -0700367//
368// Note this simultaneously handles both DTLS and TLS. Use one of the
369// higher-level functions below for most operations.
Robert Sloanae1abf92017-10-05 12:50:08 -0700370bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700371
Robert Sloana27a6a42017-09-05 08:39:28 -0700372// ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the
373// minimum and maximum enabled protocol versions, respectively.
Robert Sloanae1abf92017-10-05 12:50:08 -0700374bool ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
375 uint16_t *out_max_version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700376
Robert Sloanae1abf92017-10-05 12:50:08 -0700377// ssl_supports_version returns whether |hs| supports |version|.
378bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700379
Robert Sloana27a6a42017-09-05 08:39:28 -0700380// ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in
381// decreasing preference order.
Robert Sloanae1abf92017-10-05 12:50:08 -0700382bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb);
Robert Sloanf6200e72017-07-10 08:09:18 -0700383
Robert Sloana27a6a42017-09-05 08:39:28 -0700384// ssl_negotiate_version negotiates a common version based on |hs|'s preferences
Robert Sloanae1abf92017-10-05 12:50:08 -0700385// and the peer preference list in |peer_versions|. On success, it returns true
386// and sets |*out_version| to the selected version. Otherwise, it returns false
Robert Sloana27a6a42017-09-05 08:39:28 -0700387// and sets |*out_alert| to an alert to send.
Robert Sloanae1abf92017-10-05 12:50:08 -0700388bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
389 uint16_t *out_version, const CBS *peer_versions);
Robert Sloanf6200e72017-07-10 08:09:18 -0700390
Robert Sloan921ef2c2017-10-17 09:02:20 -0700391// ssl_protocol_version returns |ssl|'s protocol version. It is an error to
Robert Sloana27a6a42017-09-05 08:39:28 -0700392// call this function before the version is determined.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700393uint16_t ssl_protocol_version(const SSL *ssl);
Robert Sloanf6200e72017-07-10 08:09:18 -0700394
Robert Sloanb1b54b82017-11-06 13:50:02 -0800395// ssl_is_draft21 returns whether the version corresponds to a draft21 TLS 1.3
396// variant.
397bool ssl_is_draft21(uint16_t version);
398
Robert Sloandb4251a2017-09-18 09:38:15 -0700399// ssl_is_resumption_experiment returns whether the version corresponds to a
400// TLS 1.3 resumption experiment.
401bool ssl_is_resumption_experiment(uint16_t version);
402
403// ssl_is_resumption_variant returns whether the version corresponds to a
404// TLS 1.3 resumption experiment.
405bool ssl_is_resumption_variant(enum tls13_variant_t variant);
406
407// ssl_is_resumption_client_ccs_experiment returns whether the version
408// corresponds to a TLS 1.3 resumption experiment that sends a client CCS.
409bool ssl_is_resumption_client_ccs_experiment(uint16_t version);
410
411// ssl_is_resumption_record_version_experiment returns whether the version
412// corresponds to a TLS 1.3 resumption experiment that modifies the record
413// version.
414bool ssl_is_resumption_record_version_experiment(uint16_t version);
415
Robert Sloanf6200e72017-07-10 08:09:18 -0700416
Robert Sloana27a6a42017-09-05 08:39:28 -0700417// Cipher suites.
Adam Langleye9ada862015-05-11 17:20:37 -0700418
Robert Sloandb4251a2017-09-18 09:38:15 -0700419} // namespace bssl
420
421struct ssl_cipher_st {
422 // name is the OpenSSL name for the cipher.
423 const char *name;
424 // standard_name is the IETF name for the cipher.
425 const char *standard_name;
426 // id is the cipher suite value bitwise OR-d with 0x03000000.
427 uint32_t id;
428
429 // algorithm_* determine the cipher suite. See constants below for the values.
430 uint32_t algorithm_mkey;
431 uint32_t algorithm_auth;
432 uint32_t algorithm_enc;
433 uint32_t algorithm_mac;
434 uint32_t algorithm_prf;
435};
436
437namespace bssl {
438
Robert Sloana27a6a42017-09-05 08:39:28 -0700439// Bits for |algorithm_mkey| (key exchange algorithm).
Robert Sloana12bf462017-07-17 07:08:26 -0700440#define SSL_kRSA 0x00000001u
441#define SSL_kECDHE 0x00000002u
Robert Sloana27a6a42017-09-05 08:39:28 -0700442// SSL_kPSK is only set for plain PSK, not ECDHE_PSK.
Robert Sloana12bf462017-07-17 07:08:26 -0700443#define SSL_kPSK 0x00000004u
444#define SSL_kGENERIC 0x00000008u
Adam Langleye9ada862015-05-11 17:20:37 -0700445
Robert Sloana27a6a42017-09-05 08:39:28 -0700446// Bits for |algorithm_auth| (server authentication).
Robert Sloana12bf462017-07-17 07:08:26 -0700447#define SSL_aRSA 0x00000001u
448#define SSL_aECDSA 0x00000002u
Robert Sloana27a6a42017-09-05 08:39:28 -0700449// SSL_aPSK is set for both PSK and ECDHE_PSK.
Robert Sloana12bf462017-07-17 07:08:26 -0700450#define SSL_aPSK 0x00000004u
451#define SSL_aGENERIC 0x00000008u
Adam Langleye9ada862015-05-11 17:20:37 -0700452
David Benjamind316cba2016-06-02 16:17:39 -0400453#define SSL_aCERT (SSL_aRSA | SSL_aECDSA)
454
Robert Sloana27a6a42017-09-05 08:39:28 -0700455// Bits for |algorithm_enc| (symmetric encryption).
Robert Sloana12bf462017-07-17 07:08:26 -0700456#define SSL_3DES 0x00000001u
457#define SSL_AES128 0x00000002u
458#define SSL_AES256 0x00000004u
459#define SSL_AES128GCM 0x00000008u
460#define SSL_AES256GCM 0x00000010u
461#define SSL_eNULL 0x00000020u
462#define SSL_CHACHA20POLY1305 0x00000040u
Adam Langleye9ada862015-05-11 17:20:37 -0700463
464#define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM)
465
Robert Sloana27a6a42017-09-05 08:39:28 -0700466// Bits for |algorithm_mac| (symmetric authentication).
Robert Sloana12bf462017-07-17 07:08:26 -0700467#define SSL_SHA1 0x00000001u
468#define SSL_SHA256 0x00000002u
469#define SSL_SHA384 0x00000004u
Robert Sloana27a6a42017-09-05 08:39:28 -0700470// SSL_AEAD is set for all AEADs.
Robert Sloana12bf462017-07-17 07:08:26 -0700471#define SSL_AEAD 0x00000008u
Adam Langleye9ada862015-05-11 17:20:37 -0700472
Robert Sloana27a6a42017-09-05 08:39:28 -0700473// Bits for |algorithm_prf| (handshake digest).
Kenny Rootb8494592015-09-25 02:29:14 +0000474#define SSL_HANDSHAKE_MAC_DEFAULT 0x1
475#define SSL_HANDSHAKE_MAC_SHA256 0x2
476#define SSL_HANDSHAKE_MAC_SHA384 0x4
Adam Langleye9ada862015-05-11 17:20:37 -0700477
Robert Sloana27a6a42017-09-05 08:39:28 -0700478// SSL_MAX_DIGEST is the number of digest types which exist. When adding a new
479// one, update the table in ssl_cipher.c.
Adam Langleye9ada862015-05-11 17:20:37 -0700480#define SSL_MAX_DIGEST 4
481
Robert Sloana27a6a42017-09-05 08:39:28 -0700482// ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD
483// object for |cipher| protocol version |version|. It sets |*out_mac_secret_len|
484// and |*out_fixed_iv_len| to the MAC key length and fixed IV length,
485// respectively. The MAC key length is zero except for legacy block and stream
Robert Sloanae1abf92017-10-05 12:50:08 -0700486// ciphers. It returns true on success and false on error.
487bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
488 size_t *out_mac_secret_len,
489 size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
490 uint16_t version, int is_dtls);
Adam Langleye9ada862015-05-11 17:20:37 -0700491
Robert Sloana27a6a42017-09-05 08:39:28 -0700492// ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and
493// |cipher|.
Robert Sloan84377092017-08-14 09:33:19 -0700494const EVP_MD *ssl_get_handshake_digest(uint16_t version,
495 const SSL_CIPHER *cipher);
Adam Langleye9ada862015-05-11 17:20:37 -0700496
Robert Sloanb1b54b82017-11-06 13:50:02 -0800497// ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a
498// newly-allocated |ssl_cipher_preference_list_st| containing the result. It
499// returns true on success and false on failure. If |strict| is true, nonsense
500// will be rejected. If false, nonsense will be silently ignored. An empty
501// result is considered an error regardless of |strict|.
Robert Sloanae1abf92017-10-05 12:50:08 -0700502bool ssl_create_cipher_list(
Robert Sloan1c9db532017-03-13 08:03:59 -0700503 struct ssl_cipher_preference_list_st **out_cipher_list,
Robert Sloanae1abf92017-10-05 12:50:08 -0700504 const char *rule_str, bool strict);
Adam Langleye9ada862015-05-11 17:20:37 -0700505
Robert Sloana27a6a42017-09-05 08:39:28 -0700506// ssl_cipher_get_value returns the cipher suite id of |cipher|.
Adam Langleyf4e42722015-06-04 17:45:09 -0700507uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher);
508
Robert Sloana27a6a42017-09-05 08:39:28 -0700509// ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth|
510// values suitable for use with |key| in TLS 1.2 and below.
Robert Sloan572a4e22017-04-17 10:52:19 -0700511uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key);
Adam Langleye9ada862015-05-11 17:20:37 -0700512
Robert Sloanae1abf92017-10-05 12:50:08 -0700513// ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the
514// server and, optionally, the client with a certificate.
515bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher);
Adam Langleye9ada862015-05-11 17:20:37 -0700516
Robert Sloanae1abf92017-10-05 12:50:08 -0700517// ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a
518// ServerKeyExchange message.
Robert Sloana27a6a42017-09-05 08:39:28 -0700519//
Robert Sloanae1abf92017-10-05 12:50:08 -0700520// This function may return false while still allowing |cipher| an optional
Robert Sloana27a6a42017-09-05 08:39:28 -0700521// ServerKeyExchange. This is the case for plain PSK ciphers.
Robert Sloanae1abf92017-10-05 12:50:08 -0700522bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher);
Adam Langleye9ada862015-05-11 17:20:37 -0700523
Robert Sloana27a6a42017-09-05 08:39:28 -0700524// ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the
525// length of an encrypted 1-byte record, for use in record-splitting. Otherwise
526// it returns zero.
Kenny Rootb8494592015-09-25 02:29:14 +0000527size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher);
528
Adam Langleye9ada862015-05-11 17:20:37 -0700529
Robert Sloana27a6a42017-09-05 08:39:28 -0700530// Transcript layer.
Robert Sloan5d625782017-02-13 09:55:39 -0800531
Robert Sloana27a6a42017-09-05 08:39:28 -0700532// SSLTranscript maintains the handshake transcript as a combination of a
533// buffer and running hash.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700534class SSLTranscript {
535 public:
536 SSLTranscript();
537 ~SSLTranscript();
538
Robert Sloana27a6a42017-09-05 08:39:28 -0700539 // Init initializes the handshake transcript. If called on an existing
540 // transcript, it resets the transcript and hash. It returns true on success
541 // and false on failure.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700542 bool Init();
543
Robert Sloana27a6a42017-09-05 08:39:28 -0700544 // InitHash initializes the handshake hash based on the PRF and contents of
545 // the handshake transcript. Subsequent calls to |Update| will update the
546 // rolling hash. It returns one on success and zero on failure. It is an error
547 // to call this function after the handshake buffer is released.
Robert Sloan84377092017-08-14 09:33:19 -0700548 bool InitHash(uint16_t version, const SSL_CIPHER *cipher);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700549
Robert Sloanb1b54b82017-11-06 13:50:02 -0800550 // UpdateForHelloRetryRequest resets the rolling hash with the
551 // HelloRetryRequest construction. It returns true on success and false on
552 // failure. It is an error to call this function before the handshake buffer
553 // is released.
554 bool UpdateForHelloRetryRequest();
555
556 // CopyHashContext copies the hash context into |ctx| and returns true on
557 // success.
558 bool CopyHashContext(EVP_MD_CTX *ctx);
559
Robert Sloan921ef2c2017-10-17 09:02:20 -0700560 Span<const uint8_t> buffer() {
561 return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data),
562 buffer_->length);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700563 }
Robert Sloanb6d070c2017-07-24 08:40:01 -0700564
Robert Sloana27a6a42017-09-05 08:39:28 -0700565 // FreeBuffer releases the handshake buffer. Subsequent calls to
566 // |Update| will not update the handshake buffer.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700567 void FreeBuffer();
568
Robert Sloana27a6a42017-09-05 08:39:28 -0700569 // DigestLen returns the length of the PRF hash.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700570 size_t DigestLen() const;
571
Robert Sloana27a6a42017-09-05 08:39:28 -0700572 // Digest returns the PRF hash. For TLS 1.1 and below, this is
573 // |EVP_md5_sha1|.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700574 const EVP_MD *Digest() const;
575
Robert Sloana27a6a42017-09-05 08:39:28 -0700576 // Update adds |in| to the handshake buffer and handshake hash, whichever is
577 // enabled. It returns true on success and false on failure.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700578 bool Update(Span<const uint8_t> in);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700579
Robert Sloana27a6a42017-09-05 08:39:28 -0700580 // GetHash writes the handshake hash to |out| which must have room for at
581 // least |DigestLen| bytes. On success, it returns true and sets |*out_len| to
582 // the number of bytes written. Otherwise, it returns false.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700583 bool GetHash(uint8_t *out, size_t *out_len);
584
Robert Sloana27a6a42017-09-05 08:39:28 -0700585 // GetSSL3CertVerifyHash writes the SSL 3.0 CertificateVerify hash into the
586 // bytes pointed to by |out| and writes the number of bytes to
587 // |*out_len|. |out| must have room for |EVP_MAX_MD_SIZE| bytes. It returns
588 // one on success and zero on failure.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700589 bool GetSSL3CertVerifyHash(uint8_t *out, size_t *out_len,
590 const SSL_SESSION *session,
591 uint16_t signature_algorithm);
592
Robert Sloana27a6a42017-09-05 08:39:28 -0700593 // GetFinishedMAC computes the MAC for the Finished message into the bytes
594 // pointed by |out| and writes the number of bytes to |*out_len|. |out| must
595 // have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false
596 // on failure.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700597 bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session,
Robert Sloana27a6a42017-09-05 08:39:28 -0700598 bool from_server);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700599
600 private:
Robert Sloana27a6a42017-09-05 08:39:28 -0700601 // buffer_, if non-null, contains the handshake transcript.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700602 UniquePtr<BUF_MEM> buffer_;
Robert Sloana27a6a42017-09-05 08:39:28 -0700603 // hash, if initialized with an |EVP_MD|, maintains the handshake hash. For
604 // TLS 1.1 and below, it is the SHA-1 half.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700605 ScopedEVP_MD_CTX hash_;
Robert Sloana27a6a42017-09-05 08:39:28 -0700606 // md5, if initialized with an |EVP_MD|, maintains the MD5 half of the
607 // handshake hash for TLS 1.1 and below.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700608 ScopedEVP_MD_CTX md5_;
609};
Robert Sloan5d625782017-02-13 09:55:39 -0800610
Robert Sloanb1b54b82017-11-06 13:50:02 -0800611// tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret|
612// as the secret and |label| as the label. |seed1| and |seed2| are concatenated
613// to form the seed parameter. It returns true on success and false on failure.
614bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
615 Span<const uint8_t> secret, Span<const char> label,
616 Span<const uint8_t> seed1, Span<const uint8_t> seed2);
Robert Sloan5d625782017-02-13 09:55:39 -0800617
618
Robert Sloana27a6a42017-09-05 08:39:28 -0700619// Encryption layer.
Adam Langleyf4e42722015-06-04 17:45:09 -0700620
Robert Sloana27a6a42017-09-05 08:39:28 -0700621// SSLAEADContext contains information about an AEAD that is being used to
622// encrypt an SSL connection.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700623class SSLAEADContext {
624 public:
Robert Sloandb4251a2017-09-18 09:38:15 -0700625 SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700626 ~SSLAEADContext();
Robert Sloanfe7cd212017-08-07 09:03:39 -0700627 static constexpr bool kAllowUniquePtr = true;
628
Robert Sloanb6d070c2017-07-24 08:40:01 -0700629 SSLAEADContext(const SSLAEADContext &&) = delete;
630 SSLAEADContext &operator=(const SSLAEADContext &&) = delete;
631
Robert Sloana27a6a42017-09-05 08:39:28 -0700632 // CreateNullCipher creates an |SSLAEADContext| for the null cipher.
Robert Sloandb4251a2017-09-18 09:38:15 -0700633 static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700634
Robert Sloana27a6a42017-09-05 08:39:28 -0700635 // Create creates an |SSLAEADContext| using the supplied key material. It
636 // returns nullptr on error. Only one of |Open| or |Seal| may be used with the
637 // resulting object, depending on |direction|. |version| is the normalized
638 // protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef.
Robert Sloanae1abf92017-10-05 12:50:08 -0700639 static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction,
640 uint16_t version, int is_dtls,
641 const SSL_CIPHER *cipher,
642 Span<const uint8_t> enc_key,
643 Span<const uint8_t> mac_key,
644 Span<const uint8_t> fixed_iv);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700645
Robert Sloandb4251a2017-09-18 09:38:15 -0700646 // SetVersionIfNullCipher sets the version the SSLAEADContext for the null
647 // cipher, to make version-specific determinations in the record layer prior
648 // to a cipher being selected.
649 void SetVersionIfNullCipher(uint16_t version);
650
651 // ProtocolVersion returns the protocol version associated with this
652 // SSLAEADContext. It can only be called once |version_| has been set to a
653 // valid value.
654 uint16_t ProtocolVersion() const;
655
656 // RecordVersion returns the record version that should be used with this
657 // SSLAEADContext for record construction and crypto.
658 uint16_t RecordVersion() const;
659
Robert Sloanb6d070c2017-07-24 08:40:01 -0700660 const SSL_CIPHER *cipher() const { return cipher_; }
661
Robert Sloana27a6a42017-09-05 08:39:28 -0700662 // is_null_cipher returns true if this is the null cipher.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700663 bool is_null_cipher() const { return !cipher_; }
664
Robert Sloana27a6a42017-09-05 08:39:28 -0700665 // ExplicitNonceLen returns the length of the explicit nonce.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700666 size_t ExplicitNonceLen() const;
667
Robert Sloana27a6a42017-09-05 08:39:28 -0700668 // MaxOverhead returns the maximum overhead of calling |Seal|.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700669 size_t MaxOverhead() const;
670
Robert Sloana27a6a42017-09-05 08:39:28 -0700671 // SuffixLen calculates the suffix length written by |SealScatter| and writes
672 // it to |*out_suffix_len|. It returns true on success and false on error.
673 // |in_len| and |extra_in_len| should equal the argument of the same names
674 // passed to |SealScatter|.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700675 bool SuffixLen(size_t *out_suffix_len, size_t in_len,
676 size_t extra_in_len) const;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700677
Robert Sloan921ef2c2017-10-17 09:02:20 -0700678 // Open authenticates and decrypts |in| in-place. On success, it sets |*out|
679 // to the plaintext in |in| and returns true. Otherwise, it returns
680 // false. The output will always be |ExplicitNonceLen| bytes ahead of |in|.
681 bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version,
682 const uint8_t seqnum[8], Span<uint8_t> in);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700683
Robert Sloana27a6a42017-09-05 08:39:28 -0700684 // Seal encrypts and authenticates |in_len| bytes from |in| and writes the
685 // result to |out|. It returns true on success and false on error.
686 //
687 // If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700688 bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type,
Robert Sloandb4251a2017-09-18 09:38:15 -0700689 uint16_t record_version, const uint8_t seqnum[8], const uint8_t *in,
Robert Sloanb6d070c2017-07-24 08:40:01 -0700690 size_t in_len);
691
Robert Sloana27a6a42017-09-05 08:39:28 -0700692 // SealScatter encrypts and authenticates |in_len| bytes from |in| and splits
693 // the result between |out_prefix|, |out| and |out_suffix|. It returns one on
694 // success and zero on error.
695 //
696 // On successful return, exactly |ExplicitNonceLen| bytes are written to
697 // |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to
698 // |out_suffix|.
699 //
700 // |extra_in| may point to an additional plaintext buffer. If present,
701 // |extra_in_len| additional bytes are encrypted and authenticated, and the
702 // ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should
703 // be used to size |out_suffix| accordingly.
704 //
705 // If |in| and |out| alias then |out| must be == |in|. Other arguments may not
706 // alias anything.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700707 bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix,
Robert Sloandb4251a2017-09-18 09:38:15 -0700708 uint8_t type, uint16_t record_version,
709 const uint8_t seqnum[8], const uint8_t *in, size_t in_len,
710 const uint8_t *extra_in, size_t extra_in_len);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700711
712 bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const;
713
714 private:
Robert Sloana27a6a42017-09-05 08:39:28 -0700715 // GetAdditionalData writes the additional data into |out| and returns the
716 // number of bytes written.
Robert Sloandb4251a2017-09-18 09:38:15 -0700717 size_t GetAdditionalData(uint8_t out[13], uint8_t type,
718 uint16_t record_version, const uint8_t seqnum[8],
719 size_t plaintext_len);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700720
721 const SSL_CIPHER *cipher_;
722 ScopedEVP_AEAD_CTX ctx_;
Robert Sloana27a6a42017-09-05 08:39:28 -0700723 // fixed_nonce_ contains any bytes of the nonce that are fixed for all
724 // records.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700725 uint8_t fixed_nonce_[12];
726 uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0;
Robert Sloandb4251a2017-09-18 09:38:15 -0700727 // version_ is the wire version that should be used with this AEAD.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700728 uint16_t version_;
Robert Sloandb4251a2017-09-18 09:38:15 -0700729 // is_dtls_ is whether DTLS is being used with this AEAD.
730 bool is_dtls_;
Robert Sloana27a6a42017-09-05 08:39:28 -0700731 // variable_nonce_included_in_record_ is true if the variable nonce
732 // for a record is included as a prefix before the ciphertext.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700733 bool variable_nonce_included_in_record_ : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -0700734 // random_variable_nonce_ is true if the variable nonce is
735 // randomly generated, rather than derived from the sequence
736 // number.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700737 bool random_variable_nonce_ : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -0700738 // omit_length_in_ad_ is true if the length should be omitted in the
739 // AEAD's ad parameter.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700740 bool omit_length_in_ad_ : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -0700741 // omit_version_in_ad_ is true if the version should be omitted
742 // in the AEAD's ad parameter.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700743 bool omit_version_in_ad_ : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -0700744 // omit_ad_ is true if the AEAD's ad parameter should be omitted.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700745 bool omit_ad_ : 1;
Robert Sloana27a6a42017-09-05 08:39:28 -0700746 // xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the
747 // variable nonce rather than prepended.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700748 bool xor_fixed_nonce_ : 1;
749};
Robert Sloana12bf462017-07-17 07:08:26 -0700750
Adam Langleyf4e42722015-06-04 17:45:09 -0700751
Robert Sloana27a6a42017-09-05 08:39:28 -0700752// DTLS replay bitmap.
Kenny Rootb8494592015-09-25 02:29:14 +0000753
Robert Sloana27a6a42017-09-05 08:39:28 -0700754// DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect
755// replayed packets. It should be initialized by zeroing every field.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700756struct DTLS1_BITMAP {
Robert Sloana27a6a42017-09-05 08:39:28 -0700757 // map is a bit mask of the last 64 sequence numbers. Bit
758 // |1<<i| corresponds to |max_seq_num - i|.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700759 uint64_t map = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -0700760 // max_seq_num is the largest sequence number seen so far as a 64-bit
761 // integer.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700762 uint64_t max_seq_num = 0;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700763};
Kenny Rootb8494592015-09-25 02:29:14 +0000764
765
Robert Sloana27a6a42017-09-05 08:39:28 -0700766// Record layer.
Kenny Rootb8494592015-09-25 02:29:14 +0000767
Robert Sloana27a6a42017-09-05 08:39:28 -0700768// ssl_record_sequence_update increments the sequence number in |seq|. It
769// returns one on success and zero on wraparound.
David Benjamin4969cc92016-04-22 15:02:23 -0400770int ssl_record_sequence_update(uint8_t *seq, size_t seq_len);
771
Robert Sloana27a6a42017-09-05 08:39:28 -0700772// ssl_record_prefix_len returns the length of the prefix before the ciphertext
773// of a record for |ssl|.
774//
775// TODO(davidben): Expose this as part of public API once the high-level
776// buffer-free APIs are available.
Kenny Rootb8494592015-09-25 02:29:14 +0000777size_t ssl_record_prefix_len(const SSL *ssl);
778
779enum ssl_open_record_t {
780 ssl_open_record_success,
781 ssl_open_record_discard,
782 ssl_open_record_partial,
David Benjamin6e899c72016-06-09 18:02:18 -0400783 ssl_open_record_close_notify,
Kenny Rootb8494592015-09-25 02:29:14 +0000784 ssl_open_record_error,
785};
786
Robert Sloana27a6a42017-09-05 08:39:28 -0700787// tls_open_record decrypts a record from |in| in-place.
788//
789// If the input did not contain a complete record, it returns
790// |ssl_open_record_partial|. It sets |*out_consumed| to the total number of
791// bytes necessary. It is guaranteed that a successful call to |tls_open_record|
792// will consume at least that many bytes.
793//
794// Otherwise, it sets |*out_consumed| to the number of bytes of input
795// consumed. Note that input may be consumed on all return codes if a record was
796// decrypted.
797//
798// On success, it returns |ssl_open_record_success|. It sets |*out_type| to the
799// record type and |*out| to the record body in |in|. Note that |*out| may be
800// empty.
801//
802// If a record was successfully processed but should be discarded, it returns
803// |ssl_open_record_discard|.
804//
Robert Sloan921ef2c2017-10-17 09:02:20 -0700805// If a record was successfully processed but is a close_notify, it returns
806// |ssl_open_record_close_notify|.
Robert Sloana27a6a42017-09-05 08:39:28 -0700807//
Robert Sloan921ef2c2017-10-17 09:02:20 -0700808// On failure or fatal alert, it returns |ssl_open_record_error| and sets
809// |*out_alert| to an alert to emit, or zero if no alert should be emitted.
810enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
811 Span<uint8_t> *out, size_t *out_consumed,
812 uint8_t *out_alert, Span<uint8_t> in);
Kenny Rootb8494592015-09-25 02:29:14 +0000813
Robert Sloan921ef2c2017-10-17 09:02:20 -0700814// dtls_open_record implements |tls_open_record| for DTLS. It only returns
815// |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to
816// zero. The caller should read one packet and try again.
817enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
818 Span<uint8_t> *out,
David Benjamin6e899c72016-06-09 18:02:18 -0400819 size_t *out_consumed,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700820 uint8_t *out_alert, Span<uint8_t> in);
Kenny Rootb8494592015-09-25 02:29:14 +0000821
Robert Sloana27a6a42017-09-05 08:39:28 -0700822// ssl_seal_align_prefix_len returns the length of the prefix before the start
823// of the bulk of the ciphertext when sealing a record with |ssl|. Callers may
824// use this to align buffers.
825//
826// Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte
827// record and is the offset into second record's ciphertext. Thus sealing a
828// small record may result in a smaller output than this value.
829//
830// TODO(davidben): Is this alignment valuable? Record-splitting makes this a
831// mess.
David Benjamin6e899c72016-06-09 18:02:18 -0400832size_t ssl_seal_align_prefix_len(const SSL *ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000833
Robert Sloana27a6a42017-09-05 08:39:28 -0700834// tls_seal_record seals a new record of type |type| and body |in| and writes it
835// to |out|. At most |max_out| bytes will be written. It returns one on success
836// and zero on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC 1/n-1
837// record splitting and may write two records concatenated.
838//
839// For a large record, the bulk of the ciphertext will begin
840// |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may
841// improve performance. It writes at most |in_len| + |SSL_max_seal_overhead|
842// bytes to |out|.
843//
844// |in| and |out| may not alias.
Kenny Rootb8494592015-09-25 02:29:14 +0000845int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
846 uint8_t type, const uint8_t *in, size_t in_len);
847
848enum dtls1_use_epoch_t {
849 dtls1_use_previous_epoch,
850 dtls1_use_current_epoch,
851};
852
Robert Sloana27a6a42017-09-05 08:39:28 -0700853// dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a
854// record.
Robert Sloan4d1ac502017-02-06 08:36:14 -0800855size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
856
Robert Sloana27a6a42017-09-05 08:39:28 -0700857// dtls_seal_prefix_len returns the number of bytes of prefix to reserve in
858// front of the plaintext when sealing a record in-place.
David Benjaminf31229b2017-01-25 14:08:15 -0500859size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch);
860
Robert Sloana27a6a42017-09-05 08:39:28 -0700861// dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects
862// which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out|
863// may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes
864// ahead of |out|.
Kenny Rootb8494592015-09-25 02:29:14 +0000865int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
866 uint8_t type, const uint8_t *in, size_t in_len,
867 enum dtls1_use_epoch_t use_epoch);
868
Robert Sloana27a6a42017-09-05 08:39:28 -0700869// ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown
870// state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|,
871// |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as
872// appropriate.
David Benjamin6e899c72016-06-09 18:02:18 -0400873enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700874 Span<const uint8_t> in);
David Benjamin6e899c72016-06-09 18:02:18 -0400875
Kenny Rootb8494592015-09-25 02:29:14 +0000876
Robert Sloana27a6a42017-09-05 08:39:28 -0700877// Private key operations.
Kenny Rootb8494592015-09-25 02:29:14 +0000878
Robert Sloana27a6a42017-09-05 08:39:28 -0700879// ssl_has_private_key returns one if |ssl| has a private key
880// configured and zero otherwise.
David Benjaminc895d6b2016-08-11 13:26:41 -0400881int ssl_has_private_key(const SSL *ssl);
882
Robert Sloana27a6a42017-09-05 08:39:28 -0700883// ssl_private_key_* perform the corresponding operation on
884// |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they
885// call the corresponding function or |complete| depending on whether there is a
886// pending operation. Otherwise, they implement the operation with
887// |EVP_PKEY|.
Kenny Rootb8494592015-09-25 02:29:14 +0000888
Kenny Rootb8494592015-09-25 02:29:14 +0000889enum ssl_private_key_result_t ssl_private_key_sign(
Robert Sloane56da3e2017-06-26 08:26:42 -0700890 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700891 uint16_t sigalg, Span<const uint8_t> in);
Kenny Rootb8494592015-09-25 02:29:14 +0000892
Robert Sloan921ef2c2017-10-17 09:02:20 -0700893enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
894 uint8_t *out,
895 size_t *out_len,
896 size_t max_out,
897 Span<const uint8_t> in);
Kenny Roote99801b2015-11-06 15:31:15 -0800898
Robert Sloan921ef2c2017-10-17 09:02:20 -0700899// ssl_private_key_supports_signature_algorithm returns whether |hs|'s private
900// key supports |sigalg|.
901bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
Robert Sloan572a4e22017-04-17 10:52:19 -0700902 uint16_t sigalg);
David Benjaminc895d6b2016-08-11 13:26:41 -0400903
Robert Sloana27a6a42017-09-05 08:39:28 -0700904// ssl_public_key_verify verifies that the |signature| is valid for the public
905// key |pkey| and input |in|, using the signature algorithm |sigalg|.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700906bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
907 uint16_t sigalg, EVP_PKEY *pkey,
908 Span<const uint8_t> in);
Kenny Roote99801b2015-11-06 15:31:15 -0800909
Kenny Rootb8494592015-09-25 02:29:14 +0000910
Robert Sloana27a6a42017-09-05 08:39:28 -0700911// Custom extensions
Kenny Rootb8494592015-09-25 02:29:14 +0000912
Robert Sloanb6d070c2017-07-24 08:40:01 -0700913} // namespace bssl
914
Robert Sloana27a6a42017-09-05 08:39:28 -0700915// |SSL_CUSTOM_EXTENSION| is a structure that contains information about
916// custom-extension callbacks. It is defined unnamespaced for compatibility with
917// |STACK_OF(SSL_CUSTOM_EXTENSION)|.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700918typedef struct ssl_custom_extension {
Kenny Rootb8494592015-09-25 02:29:14 +0000919 SSL_custom_ext_add_cb add_callback;
920 void *add_arg;
921 SSL_custom_ext_free_cb free_callback;
922 SSL_custom_ext_parse_cb parse_callback;
923 void *parse_arg;
924 uint16_t value;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700925} SSL_CUSTOM_EXTENSION;
Kenny Rootb8494592015-09-25 02:29:14 +0000926
Robert Sloan8ff03552017-06-14 12:40:58 -0700927DEFINE_STACK_OF(SSL_CUSTOM_EXTENSION)
928
Robert Sloanb6d070c2017-07-24 08:40:01 -0700929namespace bssl {
930
931void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension);
932
David Benjamin1b249672016-12-06 18:25:50 -0500933int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions);
934int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
935 uint16_t value, const CBS *extension);
936int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
937 uint16_t value, const CBS *extension);
938int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions);
Kenny Rootb8494592015-09-25 02:29:14 +0000939
940
Robert Sloana27a6a42017-09-05 08:39:28 -0700941// Key shares.
Adam Langley4139edb2016-01-13 15:00:54 -0800942
Robert Sloana27a6a42017-09-05 08:39:28 -0700943// SSLKeyShare abstracts over Diffie-Hellman-like key exchanges.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700944class SSLKeyShare {
945 public:
946 virtual ~SSLKeyShare() {}
Robert Sloanfe7cd212017-08-07 09:03:39 -0700947 static constexpr bool kAllowUniquePtr = true;
948 HAS_VIRTUAL_DESTRUCTOR
Steven Valdez909b19f2016-11-21 15:35:44 -0500949
Robert Sloana27a6a42017-09-05 08:39:28 -0700950 // Create returns a SSLKeyShare instance for use with group |group_id| or
951 // nullptr on error.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700952 static UniquePtr<SSLKeyShare> Create(uint16_t group_id);
Adam Langley4139edb2016-01-13 15:00:54 -0800953
Robert Sloana27a6a42017-09-05 08:39:28 -0700954 // GroupID returns the group ID.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700955 virtual uint16_t GroupID() const PURE_VIRTUAL;
Adam Langley4139edb2016-01-13 15:00:54 -0800956
Robert Sloana27a6a42017-09-05 08:39:28 -0700957 // Offer generates a keypair and writes the public value to
958 // |out_public_key|. It returns true on success and false on error.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700959 virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL;
Adam Langley4139edb2016-01-13 15:00:54 -0800960
Robert Sloana27a6a42017-09-05 08:39:28 -0700961 // Accept performs a key exchange against the |peer_key| generated by |offer|.
962 // On success, it returns true, writes the public value to |out_public_key|,
Robert Sloan4562e9d2017-10-02 10:26:51 -0700963 // and sets |*out_secret| the shared secret. On failure, it returns false and
964 // sets |*out_alert| to an alert to send to the peer.
Robert Sloana27a6a42017-09-05 08:39:28 -0700965 //
966 // The default implementation calls |Offer| and then |Finish|, assuming a key
967 // exchange protocol where the peers are symmetric.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700968 virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
969 uint8_t *out_alert, Span<const uint8_t> peer_key);
David Benjamind316cba2016-06-02 16:17:39 -0400970
Robert Sloana27a6a42017-09-05 08:39:28 -0700971 // Finish performs a key exchange against the |peer_key| generated by
Robert Sloan4562e9d2017-10-02 10:26:51 -0700972 // |Accept|. On success, it returns true and sets |*out_secret| to the shared
973 // secret. On failure, it returns zero and sets |*out_alert| to an alert to
974 // send to the peer.
975 virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
976 Span<const uint8_t> peer_key) PURE_VIRTUAL;
Steven Valdez909b19f2016-11-21 15:35:44 -0500977};
Adam Langley4139edb2016-01-13 15:00:54 -0800978
Robert Sloana27a6a42017-09-05 08:39:28 -0700979// ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it
980// sets |*out_group_id| to the group ID and returns one. Otherwise, it returns
981// zero.
David Benjamind316cba2016-06-02 16:17:39 -0400982int ssl_nid_to_group_id(uint16_t *out_group_id, int nid);
Adam Langley4139edb2016-01-13 15:00:54 -0800983
Robert Sloana27a6a42017-09-05 08:39:28 -0700984// ssl_name_to_group_id looks up the group corresponding to the |name| string
985// of length |len|. On success, it sets |*out_group_id| to the group ID and
986// returns one. Otherwise, it returns zero.
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400987int ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len);
988
David Benjamind316cba2016-06-02 16:17:39 -0400989
Robert Sloana27a6a42017-09-05 08:39:28 -0700990// Handshake messages.
David Benjamind316cba2016-06-02 16:17:39 -0400991
Robert Sloan84377092017-08-14 09:33:19 -0700992struct SSLMessage {
993 bool is_v2_hello;
994 uint8_t type;
995 CBS body;
Robert Sloana27a6a42017-09-05 08:39:28 -0700996 // raw is the entire serialized handshake message, including the TLS or DTLS
997 // message header.
Robert Sloan84377092017-08-14 09:33:19 -0700998 CBS raw;
999};
1000
Robert Sloana27a6a42017-09-05 08:39:28 -07001001// SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including
1002// ChangeCipherSpec, in the longest handshake flight. Currently this is the
1003// client's second leg in a full handshake when client certificates, NPN, and
1004// Channel ID, are all enabled.
David Benjaminc895d6b2016-08-11 13:26:41 -04001005#define SSL_MAX_HANDSHAKE_FLIGHT 7
1006
Robert Sloana27a6a42017-09-05 08:39:28 -07001007// ssl_max_handshake_message_len returns the maximum number of bytes permitted
1008// in a handshake message for |ssl|.
David Benjamind316cba2016-06-02 16:17:39 -04001009size_t ssl_max_handshake_message_len(const SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08001010
Robert Sloan36272962017-10-23 10:28:39 -07001011// tls_can_accept_handshake_data returns whether |ssl| is able to accept more
1012// data into handshake buffer.
1013bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert);
1014
Robert Sloan921ef2c2017-10-17 09:02:20 -07001015// tls_has_unprocessed_handshake_data returns whether there is buffered
1016// handshake data that has not been consumed by |get_message|.
1017bool tls_has_unprocessed_handshake_data(const SSL *ssl);
1018
1019// dtls_has_unprocessed_handshake_data behaves like
1020// |tls_has_unprocessed_handshake_data| for DTLS.
1021bool dtls_has_unprocessed_handshake_data(const SSL *ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001022
Robert Sloanb6d070c2017-07-24 08:40:01 -07001023struct DTLS_OUTGOING_MESSAGE {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001024 DTLS_OUTGOING_MESSAGE() {}
1025 DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete;
1026 DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete;
1027 ~DTLS_OUTGOING_MESSAGE() { Clear(); }
1028
1029 void Clear();
1030
1031 uint8_t *data = nullptr;
1032 uint32_t len = 0;
1033 uint16_t epoch = 0;
1034 bool is_ccs = false;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001035};
David Benjaminc895d6b2016-08-11 13:26:41 -04001036
Robert Sloana27a6a42017-09-05 08:39:28 -07001037// dtls_clear_outgoing_messages releases all buffered outgoing messages.
David Benjaminc895d6b2016-08-11 13:26:41 -04001038void dtls_clear_outgoing_messages(SSL *ssl);
1039
Adam Langley4139edb2016-01-13 15:00:54 -08001040
Robert Sloana27a6a42017-09-05 08:39:28 -07001041// Callbacks.
David Benjamin6e899c72016-06-09 18:02:18 -04001042
Robert Sloana27a6a42017-09-05 08:39:28 -07001043// ssl_do_info_callback calls |ssl|'s info callback, if set.
David Benjamin6e899c72016-06-09 18:02:18 -04001044void ssl_do_info_callback(const SSL *ssl, int type, int value);
1045
Robert Sloana27a6a42017-09-05 08:39:28 -07001046// ssl_do_msg_callback calls |ssl|'s message callback, if set.
David Benjamin7c0d06c2016-08-11 13:26:41 -04001047void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
Robert Sloan921ef2c2017-10-17 09:02:20 -07001048 Span<const uint8_t> in);
David Benjamin6e899c72016-06-09 18:02:18 -04001049
1050
Robert Sloana27a6a42017-09-05 08:39:28 -07001051// Transport buffers.
Kenny Rootb8494592015-09-25 02:29:14 +00001052
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001053class SSLBuffer {
1054 public:
1055 SSLBuffer() {}
1056 ~SSLBuffer() { Clear(); }
1057
1058 SSLBuffer(const SSLBuffer &) = delete;
1059 SSLBuffer &operator=(const SSLBuffer &) = delete;
1060
1061 uint8_t *data() { return buf_ + offset_; }
1062 size_t size() const { return size_; }
1063 bool empty() const { return size_ == 0; }
1064 size_t cap() const { return cap_; }
1065
1066 Span<uint8_t> span() { return MakeSpan(data(), size()); }
1067
1068 Span<uint8_t> remaining() {
1069 return MakeSpan(data() + size(), cap() - size());
1070 }
1071
1072 // Clear releases the buffer.
1073 void Clear();
1074
1075 // EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such
1076 // that data written after |header_len| is aligned to a
1077 // |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false
1078 // on error.
1079 bool EnsureCap(size_t header_len, size_t new_cap);
1080
1081 // DidWrite extends the buffer by |len|. The caller must have filled in to
1082 // this point.
1083 void DidWrite(size_t len);
1084
1085 // Consume consumes |len| bytes from the front of the buffer. The memory
1086 // consumed will remain valid until the next call to |DiscardConsumed| or
1087 // |Clear|.
1088 void Consume(size_t len);
1089
1090 // DiscardConsumed discards the consumed bytes from the buffer. If the buffer
1091 // is now empty, it releases memory used by it.
1092 void DiscardConsumed();
1093
1094 private:
1095 // buf_ is the memory allocated for this buffer.
1096 uint8_t *buf_ = nullptr;
1097 // offset_ is the offset into |buf_| which the buffer contents start at.
1098 uint16_t offset_ = 0;
1099 // size_ is the size of the buffer contents from |buf_| + |offset_|.
1100 uint16_t size_ = 0;
1101 // cap_ is how much memory beyond |buf_| + |offset_| is available.
1102 uint16_t cap_ = 0;
1103};
Kenny Rootb8494592015-09-25 02:29:14 +00001104
Robert Sloana27a6a42017-09-05 08:39:28 -07001105// ssl_read_buffer_extend_to extends the read buffer to the desired length. For
1106// TLS, it reads to the end of the buffer until the buffer is |len| bytes
1107// long. For DTLS, it reads a new packet and ignores |len|. It returns one on
1108// success, zero on EOF, and a negative number on error.
1109//
1110// It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is
1111// non-empty.
Kenny Rootb8494592015-09-25 02:29:14 +00001112int ssl_read_buffer_extend_to(SSL *ssl, size_t len);
1113
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001114// ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer|
1115// to a record-processing function. If |ret| is a success or if the caller
1116// should retry, it returns one and sets |*out_retry|. Otherwise, it returns <=
1117// 0.
Robert Sloan36272962017-10-23 10:28:39 -07001118int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
1119 size_t consumed, uint8_t alert);
1120
Robert Sloana27a6a42017-09-05 08:39:28 -07001121// ssl_write_buffer_flush flushes the write buffer to the transport. It returns
1122// one on success and <= 0 on error. For DTLS, whether or not the write
1123// succeeds, the write buffer will be cleared.
Kenny Rootb8494592015-09-25 02:29:14 +00001124int ssl_write_buffer_flush(SSL *ssl);
1125
Kenny Rootb8494592015-09-25 02:29:14 +00001126
Robert Sloana27a6a42017-09-05 08:39:28 -07001127// Certificate functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001128
Robert Sloana27a6a42017-09-05 08:39:28 -07001129// ssl_has_certificate returns one if a certificate and private key are
1130// configured and zero otherwise.
David Benjaminc895d6b2016-08-11 13:26:41 -04001131int ssl_has_certificate(const SSL *ssl);
1132
Robert Sloana27a6a42017-09-05 08:39:28 -07001133// ssl_parse_cert_chain parses a certificate list from |cbs| in the format used
1134// by a TLS Certificate message. On success, it advances |cbs| and returns
1135// true. Otherwise, it returns false and sets |*out_alert| to an alert to send
1136// to the peer.
1137//
1138// If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to
1139// the certificate chain and the leaf certificate's public key
1140// respectively. Otherwise, both will be set to nullptr.
1141//
1142// If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the
1143// SHA-256 hash of the leaf to |out_leaf_sha256|.
Robert Sloanfe7cd212017-08-07 09:03:39 -07001144bool ssl_parse_cert_chain(uint8_t *out_alert,
1145 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
1146 UniquePtr<EVP_PKEY> *out_pubkey,
1147 uint8_t *out_leaf_sha256, CBS *cbs,
1148 CRYPTO_BUFFER_POOL *pool);
David Benjaminc895d6b2016-08-11 13:26:41 -04001149
Robert Sloana27a6a42017-09-05 08:39:28 -07001150// ssl_add_cert_chain adds |ssl|'s certificate chain to |cbb| in the format used
1151// by a TLS Certificate message. If there is no certificate chain, it emits an
1152// empty certificate list. It returns one on success and zero on error.
David Benjaminc895d6b2016-08-11 13:26:41 -04001153int ssl_add_cert_chain(SSL *ssl, CBB *cbb);
1154
Robert Sloana27a6a42017-09-05 08:39:28 -07001155// ssl_cert_check_digital_signature_key_usage parses the DER-encoded, X.509
1156// certificate in |in| and returns one if doesn't specify a key usage or, if it
1157// does, if it includes digitalSignature. Otherwise it pushes to the error
1158// queue and returns zero.
Steven Valdeze7531f02016-12-14 13:29:57 -05001159int ssl_cert_check_digital_signature_key_usage(const CBS *in);
1160
Robert Sloana27a6a42017-09-05 08:39:28 -07001161// ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509
1162// certificate in |in|. It returns an allocated |EVP_PKEY| or else returns
1163// nullptr and pushes to the error queue.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001164UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in);
Steven Valdeze7531f02016-12-14 13:29:57 -05001165
Robert Sloana27a6a42017-09-05 08:39:28 -07001166// ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a
1167// TLS CertificateRequest message. On success, it returns a newly-allocated
1168// |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and
1169// sets |*out_alert| to an alert to send to the peer.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001170UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
1171 uint8_t *out_alert,
1172 CBS *cbs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001173
Robert Sloanb1b54b82017-11-06 13:50:02 -08001174// ssl_has_client_CAs returns there are configured CAs.
1175bool ssl_has_client_CAs(SSL *ssl);
1176
Robert Sloana27a6a42017-09-05 08:39:28 -07001177// ssl_add_client_CA_list adds the configured CA list to |cbb| in the format
1178// used by a TLS CertificateRequest message. It returns one on success and zero
1179// on error.
David Benjaminc895d6b2016-08-11 13:26:41 -04001180int ssl_add_client_CA_list(SSL *ssl, CBB *cbb);
1181
Robert Sloana27a6a42017-09-05 08:39:28 -07001182// ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as
1183// a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes
1184// an error on the error queue.
Robert Sloana94fe052017-02-21 08:49:28 -08001185int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Steven Valdeze7531f02016-12-14 13:29:57 -05001186 const CRYPTO_BUFFER *leaf);
David Benjaminc895d6b2016-08-11 13:26:41 -04001187
Robert Sloana27a6a42017-09-05 08:39:28 -07001188// ssl_on_certificate_selected is called once the certificate has been selected.
1189// It finalizes the certificate and initializes |hs->local_pubkey|. It returns
1190// one on success and zero on error.
Robert Sloan572a4e22017-04-17 10:52:19 -07001191int ssl_on_certificate_selected(SSL_HANDSHAKE *hs);
1192
David Benjaminc895d6b2016-08-11 13:26:41 -04001193
Robert Sloana27a6a42017-09-05 08:39:28 -07001194// TLS 1.3 key derivation.
David Benjaminc895d6b2016-08-11 13:26:41 -04001195
Robert Sloana27a6a42017-09-05 08:39:28 -07001196// tls13_init_key_schedule initializes the handshake hash and key derivation
Robert Sloanb1b54b82017-11-06 13:50:02 -08001197// state, and incorporates the PSK. The cipher suite and PRF hash must have been
1198// selected at this point. It returns one on success and zero on error.
1199int tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1200 size_t psk_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001201
Robert Sloana27a6a42017-09-05 08:39:28 -07001202// tls13_init_early_key_schedule initializes the handshake hash and key
Robert Sloanb1b54b82017-11-06 13:50:02 -08001203// derivation state from the resumption secret and incorporates the PSK to
1204// derive the early secrets. It returns one on success and zero on error.
1205int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk,
1206 size_t psk_len);
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001207
Robert Sloana27a6a42017-09-05 08:39:28 -07001208// tls13_advance_key_schedule incorporates |in| into the key schedule with
1209// HKDF-Extract. It returns one on success and zero on error.
David Benjamin1b249672016-12-06 18:25:50 -05001210int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
1211 size_t len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001212
Robert Sloana27a6a42017-09-05 08:39:28 -07001213// tls13_set_traffic_key sets the read or write traffic keys to
1214// |traffic_secret|. It returns one on success and zero on error.
Steven Valdez909b19f2016-11-21 15:35:44 -05001215int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
David Benjaminc895d6b2016-08-11 13:26:41 -04001216 const uint8_t *traffic_secret,
1217 size_t traffic_secret_len);
1218
Robert Sloana27a6a42017-09-05 08:39:28 -07001219// tls13_derive_early_secrets derives the early traffic secret. It returns one
1220// on success and zero on error.
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001221int tls13_derive_early_secrets(SSL_HANDSHAKE *hs);
1222
Robert Sloana27a6a42017-09-05 08:39:28 -07001223// tls13_derive_handshake_secrets derives the handshake traffic secret. It
1224// returns one on success and zero on error.
Robert Sloan69939df2017-01-09 10:53:07 -08001225int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001226
Robert Sloana27a6a42017-09-05 08:39:28 -07001227// tls13_rotate_traffic_key derives the next read or write traffic secret. It
1228// returns one on success and zero on error.
David Benjaminc895d6b2016-08-11 13:26:41 -04001229int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction);
1230
Robert Sloana27a6a42017-09-05 08:39:28 -07001231// tls13_derive_application_secrets derives the initial application data traffic
1232// and exporter secrets based on the handshake transcripts and |master_secret|.
1233// It returns one on success and zero on error.
David Benjamin1b249672016-12-06 18:25:50 -05001234int tls13_derive_application_secrets(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001235
Robert Sloana27a6a42017-09-05 08:39:28 -07001236// tls13_derive_resumption_secret derives the |resumption_secret|.
David Benjamin1b249672016-12-06 18:25:50 -05001237int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001238
Robert Sloana27a6a42017-09-05 08:39:28 -07001239// tls13_export_keying_material provides an exporter interface to use the
1240// |exporter_secret|.
David Benjaminc895d6b2016-08-11 13:26:41 -04001241int tls13_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
1242 const char *label, size_t label_len,
1243 const uint8_t *context, size_t context_len,
1244 int use_context);
1245
Robert Sloana27a6a42017-09-05 08:39:28 -07001246// tls13_finished_mac calculates the MAC of the handshake transcript to verify
1247// the integrity of the Finished message, and stores the result in |out| and
1248// length in |out_len|. |is_server| is 1 if this is for the Server Finished and
1249// 0 for the Client Finished.
David Benjamin1b249672016-12-06 18:25:50 -05001250int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out,
1251 size_t *out_len, int is_server);
David Benjaminc895d6b2016-08-11 13:26:41 -04001252
Robert Sloanb1b54b82017-11-06 13:50:02 -08001253// tls13_derive_session_psk calculates the PSK for this session based on the
1254// resumption master secret and |nonce|. It returns true on success, and false
1255// on failure.
1256bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce);
1257
Robert Sloana27a6a42017-09-05 08:39:28 -07001258// tls13_write_psk_binder calculates the PSK binder value and replaces the last
1259// bytes of |msg| with the resulting value. It returns 1 on success, and 0 on
1260// failure.
Robert Sloan5d625782017-02-13 09:55:39 -08001261int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001262
Robert Sloana27a6a42017-09-05 08:39:28 -07001263// tls13_verify_psk_binder verifies that the handshake transcript, truncated
1264// up to the binders has a valid signature using the value of |session|'s
1265// resumption secret. It returns 1 on success, and 0 on failure.
Robert Sloan5d625782017-02-13 09:55:39 -08001266int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
Robert Sloan84377092017-08-14 09:33:19 -07001267 const SSLMessage &msg, CBS *binders);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001268
David Benjaminc895d6b2016-08-11 13:26:41 -04001269
Robert Sloana27a6a42017-09-05 08:39:28 -07001270// Handshake functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001271
1272enum ssl_hs_wait_t {
1273 ssl_hs_error,
1274 ssl_hs_ok,
Robert Sloana27a6a42017-09-05 08:39:28 -07001275 ssl_hs_read_server_hello,
David Benjaminc895d6b2016-08-11 13:26:41 -04001276 ssl_hs_read_message,
David Benjaminc895d6b2016-08-11 13:26:41 -04001277 ssl_hs_flush,
Robert Sloana27a6a42017-09-05 08:39:28 -07001278 ssl_hs_certificate_selection_pending,
David Benjaminc895d6b2016-08-11 13:26:41 -04001279 ssl_hs_x509_lookup,
Steven Valdez909b19f2016-11-21 15:35:44 -05001280 ssl_hs_channel_id_lookup,
David Benjaminc895d6b2016-08-11 13:26:41 -04001281 ssl_hs_private_key_operation,
Robert Sloana27a6a42017-09-05 08:39:28 -07001282 ssl_hs_pending_session,
Robert Sloan1c9db532017-03-13 08:03:59 -07001283 ssl_hs_pending_ticket,
Robert Sloana27a6a42017-09-05 08:39:28 -07001284 ssl_hs_early_return,
Robert Sloane56da3e2017-06-26 08:26:42 -07001285 ssl_hs_early_data_rejected,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001286 ssl_hs_read_end_of_early_data,
Robert Sloana12bf462017-07-17 07:08:26 -07001287 ssl_hs_read_change_cipher_spec,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001288 ssl_hs_certificate_verify,
David Benjaminc895d6b2016-08-11 13:26:41 -04001289};
1290
Robert Sloanb6d070c2017-07-24 08:40:01 -07001291struct SSL_HANDSHAKE {
1292 explicit SSL_HANDSHAKE(SSL *ssl);
1293 ~SSL_HANDSHAKE();
Robert Sloanfe7cd212017-08-07 09:03:39 -07001294 static constexpr bool kAllowUniquePtr = true;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001295
Robert Sloana27a6a42017-09-05 08:39:28 -07001296 // ssl is a non-owning pointer to the parent |SSL| object.
David Benjamin1b249672016-12-06 18:25:50 -05001297 SSL *ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001298
Robert Sloana27a6a42017-09-05 08:39:28 -07001299 // wait contains the operation the handshake is currently blocking on or
1300 // |ssl_hs_ok| if none.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001301 enum ssl_hs_wait_t wait = ssl_hs_ok;
David Benjaminc895d6b2016-08-11 13:26:41 -04001302
Robert Sloana27a6a42017-09-05 08:39:28 -07001303 // state is the internal state for the TLS 1.2 and below handshake. Its
1304 // values depend on |do_handshake| but the starting state is always zero.
1305 int state = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04001306
Robert Sloana27a6a42017-09-05 08:39:28 -07001307 // tls13_state is the internal state for the TLS 1.3 handshake. Its values
1308 // depend on |do_handshake| but the starting state is always zero.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001309 int tls13_state = 0;
Steven Valdeze7531f02016-12-14 13:29:57 -05001310
Robert Sloana27a6a42017-09-05 08:39:28 -07001311 // min_version is the minimum accepted protocol version, taking account both
1312 // |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001313 uint16_t min_version = 0;
Robert Sloane56da3e2017-06-26 08:26:42 -07001314
Robert Sloana27a6a42017-09-05 08:39:28 -07001315 // max_version is the maximum accepted protocol version, taking account both
1316 // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001317 uint16_t max_version = 0;
Robert Sloane56da3e2017-06-26 08:26:42 -07001318
Robert Sloana27a6a42017-09-05 08:39:28 -07001319 // session_id is the session ID in the ClientHello, used for the experimental
1320 // TLS 1.3 variant.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001321 uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0};
1322 uint8_t session_id_len = 0;
Robert Sloana12bf462017-07-17 07:08:26 -07001323
Robert Sloanb6d070c2017-07-24 08:40:01 -07001324 size_t hash_len = 0;
1325 uint8_t secret[EVP_MAX_MD_SIZE] = {0};
1326 uint8_t early_traffic_secret[EVP_MAX_MD_SIZE] = {0};
1327 uint8_t client_handshake_secret[EVP_MAX_MD_SIZE] = {0};
1328 uint8_t server_handshake_secret[EVP_MAX_MD_SIZE] = {0};
1329 uint8_t client_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
1330 uint8_t server_traffic_secret_0[EVP_MAX_MD_SIZE] = {0};
1331 uint8_t expected_client_finished[EVP_MAX_MD_SIZE] = {0};
David Benjaminc895d6b2016-08-11 13:26:41 -04001332
David Benjamin95add822016-10-19 01:09:12 -04001333 union {
Robert Sloana27a6a42017-09-05 08:39:28 -07001334 // sent is a bitset where the bits correspond to elements of kExtensions
1335 // in t1_lib.c. Each bit is set if that extension was sent in a
1336 // ClientHello. It's not used by servers.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001337 uint32_t sent = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07001338 // received is a bitset, like |sent|, but is used by servers to record
1339 // which extensions were received from a client.
David Benjamin95add822016-10-19 01:09:12 -04001340 uint32_t received;
1341 } extensions;
1342
1343 union {
Robert Sloana27a6a42017-09-05 08:39:28 -07001344 // sent is a bitset where the bits correspond to elements of
1345 // |client_custom_extensions| in the |SSL_CTX|. Each bit is set if that
1346 // extension was sent in a ClientHello. It's not used by servers.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001347 uint16_t sent = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07001348 // received is a bitset, like |sent|, but is used by servers to record
1349 // which custom extensions were received from a client. The bits here
1350 // correspond to |server_custom_extensions|.
David Benjamin95add822016-10-19 01:09:12 -04001351 uint16_t received;
1352 } custom_extensions;
1353
Robert Sloana27a6a42017-09-05 08:39:28 -07001354 // retry_group is the group ID selected by the server in HelloRetryRequest in
1355 // TLS 1.3.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001356 uint16_t retry_group = 0;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001357
Robert Sloan921ef2c2017-10-17 09:02:20 -07001358 // error, if |wait| is |ssl_hs_error|, is the error the handshake failed on.
1359 UniquePtr<ERR_SAVE_STATE> error;
1360
Robert Sloana27a6a42017-09-05 08:39:28 -07001361 // key_share is the current key exchange instance.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001362 UniquePtr<SSLKeyShare> key_share;
David Benjamin1b249672016-12-06 18:25:50 -05001363
Robert Sloana27a6a42017-09-05 08:39:28 -07001364 // transcript is the current handshake transcript.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001365 SSLTranscript transcript;
Robert Sloan5d625782017-02-13 09:55:39 -08001366
Robert Sloana27a6a42017-09-05 08:39:28 -07001367 // cookie is the value of the cookie received from the server, if any.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001368 Array<uint8_t> cookie;
David Benjamin95add822016-10-19 01:09:12 -04001369
Robert Sloana27a6a42017-09-05 08:39:28 -07001370 // key_share_bytes is the value of the previously sent KeyShare extension by
1371 // the client in TLS 1.3.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001372 Array<uint8_t> key_share_bytes;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001373
Robert Sloana27a6a42017-09-05 08:39:28 -07001374 // ecdh_public_key, for servers, is the key share to be sent to the client in
1375 // TLS 1.3.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001376 Array<uint8_t> ecdh_public_key;
David Benjaminc895d6b2016-08-11 13:26:41 -04001377
Robert Sloana27a6a42017-09-05 08:39:28 -07001378 // peer_sigalgs are the signature algorithms that the peer supports. These are
1379 // taken from the contents of the signature algorithms extension for a server
1380 // or from the CertificateRequest for a client.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001381 Array<uint16_t> peer_sigalgs;
David Benjaminc895d6b2016-08-11 13:26:41 -04001382
Robert Sloana27a6a42017-09-05 08:39:28 -07001383 // peer_supported_group_list contains the supported group IDs advertised by
1384 // the peer. This is only set on the server's end. The server does not
1385 // advertise this extension to the client.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001386 Array<uint16_t> peer_supported_group_list;
David Benjamin95add822016-10-19 01:09:12 -04001387
Robert Sloana27a6a42017-09-05 08:39:28 -07001388 // peer_key is the peer's ECDH key for a TLS 1.2 client.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001389 Array<uint8_t> peer_key;
David Benjamin95add822016-10-19 01:09:12 -04001390
Robert Sloana27a6a42017-09-05 08:39:28 -07001391 // server_params, in a TLS 1.2 server, stores the ServerKeyExchange
1392 // parameters. It has client and server randoms prepended for signing
1393 // convenience.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001394 Array<uint8_t> server_params;
David Benjamin95add822016-10-19 01:09:12 -04001395
Robert Sloana27a6a42017-09-05 08:39:28 -07001396 // peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the
1397 // server when using a TLS 1.2 PSK key exchange.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001398 UniquePtr<char> peer_psk_identity_hint;
David Benjamin1b249672016-12-06 18:25:50 -05001399
Robert Sloana27a6a42017-09-05 08:39:28 -07001400 // ca_names, on the client, contains the list of CAs received in a
1401 // CertificateRequest message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001402 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names;
Robert Sloan7d422bc2017-03-06 10:04:29 -08001403
Robert Sloanae1abf92017-10-05 12:50:08 -07001404 // cached_x509_ca_names contains a cache of parsed versions of the elements of
1405 // |ca_names|. This pointer is left non-owning so only
1406 // |ssl_crypto_x509_method| needs to link against crypto/x509.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001407 STACK_OF(X509_NAME) *cached_x509_ca_names = nullptr;
David Benjamin1b249672016-12-06 18:25:50 -05001408
Robert Sloana27a6a42017-09-05 08:39:28 -07001409 // certificate_types, on the client, contains the set of certificate types
1410 // received in a CertificateRequest message.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001411 Array<uint8_t> certificate_types;
David Benjamin1b249672016-12-06 18:25:50 -05001412
Robert Sloana27a6a42017-09-05 08:39:28 -07001413 // local_pubkey is the public key we are authenticating as.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001414 UniquePtr<EVP_PKEY> local_pubkey;
Robert Sloan572a4e22017-04-17 10:52:19 -07001415
Robert Sloana27a6a42017-09-05 08:39:28 -07001416 // peer_pubkey is the public key parsed from the peer's leaf certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001417 UniquePtr<EVP_PKEY> peer_pubkey;
Steven Valdeze7531f02016-12-14 13:29:57 -05001418
Robert Sloana27a6a42017-09-05 08:39:28 -07001419 // new_session is the new mutable session being established by the current
1420 // handshake. It should not be cached.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001421 UniquePtr<SSL_SESSION> new_session;
Robert Sloana94fe052017-02-21 08:49:28 -08001422
Robert Sloana27a6a42017-09-05 08:39:28 -07001423 // early_session is the session corresponding to the current 0-RTT state on
1424 // the client if |in_early_data| is true.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001425 UniquePtr<SSL_SESSION> early_session;
Robert Sloane56da3e2017-06-26 08:26:42 -07001426
Robert Sloana27a6a42017-09-05 08:39:28 -07001427 // new_cipher is the cipher being negotiated in this handshake.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001428 const SSL_CIPHER *new_cipher = nullptr;
Robert Sloana94fe052017-02-21 08:49:28 -08001429
Robert Sloana27a6a42017-09-05 08:39:28 -07001430 // key_block is the record-layer key block for TLS 1.2 and earlier.
Robert Sloanae1abf92017-10-05 12:50:08 -07001431 Array<uint8_t> key_block;
David Benjamin1b249672016-12-06 18:25:50 -05001432
Robert Sloana27a6a42017-09-05 08:39:28 -07001433 // scts_requested is true if the SCT extension is in the ClientHello.
1434 bool scts_requested:1;
David Benjamin1b249672016-12-06 18:25:50 -05001435
Robert Sloana27a6a42017-09-05 08:39:28 -07001436 // needs_psk_binder is true if the ClientHello has a placeholder PSK binder to
1437 // be filled in.
1438 bool needs_psk_binder:1;
David Benjamin1b249672016-12-06 18:25:50 -05001439
Robert Sloana27a6a42017-09-05 08:39:28 -07001440 bool received_hello_retry_request:1;
David Benjamin1b249672016-12-06 18:25:50 -05001441
Robert Sloana27a6a42017-09-05 08:39:28 -07001442 bool received_custom_extension:1;
Robert Sloan8f860b12017-08-28 07:37:06 -07001443
Robert Sloana27a6a42017-09-05 08:39:28 -07001444 // handshake_finalized is true once the handshake has completed, at which
1445 // point accessors should use the established state.
1446 bool handshake_finalized:1;
David Benjamin1b249672016-12-06 18:25:50 -05001447
Robert Sloana27a6a42017-09-05 08:39:28 -07001448 // accept_psk_mode stores whether the client's PSK mode is compatible with our
1449 // preferences.
1450 bool accept_psk_mode:1;
David Benjamin95add822016-10-19 01:09:12 -04001451
Robert Sloana27a6a42017-09-05 08:39:28 -07001452 // cert_request is true if a client certificate was requested.
1453 bool cert_request:1;
David Benjamin95add822016-10-19 01:09:12 -04001454
Robert Sloana27a6a42017-09-05 08:39:28 -07001455 // certificate_status_expected is true if OCSP stapling was negotiated and the
1456 // server is expected to send a CertificateStatus message. (This is used on
1457 // both the client and server sides.)
1458 bool certificate_status_expected:1;
David Benjamin95add822016-10-19 01:09:12 -04001459
Robert Sloana27a6a42017-09-05 08:39:28 -07001460 // ocsp_stapling_requested is true if a client requested OCSP stapling.
1461 bool ocsp_stapling_requested:1;
David Benjamin95add822016-10-19 01:09:12 -04001462
Robert Sloana27a6a42017-09-05 08:39:28 -07001463 // should_ack_sni is used by a server and indicates that the SNI extension
1464 // should be echoed in the ServerHello.
1465 bool should_ack_sni:1;
David Benjamin95add822016-10-19 01:09:12 -04001466
Robert Sloana27a6a42017-09-05 08:39:28 -07001467 // in_false_start is true if there is a pending client handshake in False
1468 // Start. The client may write data at this point.
1469 bool in_false_start:1;
Robert Sloane56da3e2017-06-26 08:26:42 -07001470
Robert Sloana27a6a42017-09-05 08:39:28 -07001471 // in_early_data is true if there is a pending handshake that has progressed
1472 // enough to send and receive early data.
1473 bool in_early_data:1;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001474
Robert Sloana27a6a42017-09-05 08:39:28 -07001475 // early_data_offered is true if the client sent the early_data extension.
1476 bool early_data_offered:1;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001477
Robert Sloana27a6a42017-09-05 08:39:28 -07001478 // can_early_read is true if application data may be read at this point in the
1479 // handshake.
1480 bool can_early_read:1;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001481
Robert Sloana27a6a42017-09-05 08:39:28 -07001482 // can_early_write is true if application data may be written at this point in
1483 // the handshake.
1484 bool can_early_write:1;
David Benjamin95add822016-10-19 01:09:12 -04001485
Robert Sloana27a6a42017-09-05 08:39:28 -07001486 // next_proto_neg_seen is one of NPN was negotiated.
1487 bool next_proto_neg_seen:1;
Steven Valdez909b19f2016-11-21 15:35:44 -05001488
Robert Sloana27a6a42017-09-05 08:39:28 -07001489 // ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent
1490 // or received.
1491 bool ticket_expected:1;
Robert Sloana94fe052017-02-21 08:49:28 -08001492
Robert Sloana27a6a42017-09-05 08:39:28 -07001493 // extended_master_secret is true if the extended master secret extension is
1494 // negotiated in this handshake.
1495 bool extended_master_secret:1;
Robert Sloane56da3e2017-06-26 08:26:42 -07001496
Robert Sloana27a6a42017-09-05 08:39:28 -07001497 // pending_private_key_op is true if there is a pending private key operation
1498 // in progress.
1499 bool pending_private_key_op:1;
1500
1501 // client_version is the value sent or received in the ClientHello version.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001502 uint16_t client_version = 0;
Robert Sloan9254e682017-04-24 09:42:06 -07001503
Robert Sloana27a6a42017-09-05 08:39:28 -07001504 // early_data_read is the amount of early data that has been read by the
1505 // record layer.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001506 uint16_t early_data_read = 0;
Robert Sloane56da3e2017-06-26 08:26:42 -07001507
Robert Sloana27a6a42017-09-05 08:39:28 -07001508 // early_data_written is the amount of early data that has been written by the
1509 // record layer.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001510 uint16_t early_data_written = 0;
1511};
David Benjamin95add822016-10-19 01:09:12 -04001512
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001513UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001514
Robert Sloana27a6a42017-09-05 08:39:28 -07001515// ssl_check_message_type checks if |msg| has type |type|. If so it returns
1516// one. Otherwise, it sends an alert and returns zero.
Robert Sloan36272962017-10-23 10:28:39 -07001517bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type);
Robert Sloan4d1ac502017-02-06 08:36:14 -08001518
Robert Sloana27a6a42017-09-05 08:39:28 -07001519// ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0
1520// on error. It sets |out_early_return| to one if we've completed the handshake
1521// early.
1522int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return);
David Benjaminc895d6b2016-08-11 13:26:41 -04001523
Robert Sloana27a6a42017-09-05 08:39:28 -07001524// The following are implementations of |do_handshake| for the client and
1525// server.
1526enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs);
1527enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs);
David Benjamin1b249672016-12-06 18:25:50 -05001528enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs);
1529enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001530
Robert Sloana27a6a42017-09-05 08:39:28 -07001531// The following functions return human-readable representations of the TLS
1532// handshake states for debugging.
1533const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs);
1534const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs);
Robert Sloan8f860b12017-08-28 07:37:06 -07001535const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs);
1536const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs);
1537
Robert Sloana27a6a42017-09-05 08:39:28 -07001538// tls13_post_handshake processes a post-handshake message. It returns one on
1539// success and zero on failure.
Robert Sloan84377092017-08-14 09:33:19 -07001540int tls13_post_handshake(SSL *ssl, const SSLMessage &msg);
David Benjaminc895d6b2016-08-11 13:26:41 -04001541
Robert Sloan84377092017-08-14 09:33:19 -07001542int tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1543 int allow_anonymous);
1544int tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001545
Robert Sloana27a6a42017-09-05 08:39:28 -07001546// tls13_process_finished processes |msg| as a Finished message from the
1547// peer. If |use_saved_value| is one, the verify_data is compared against
1548// |hs->expected_client_finished| rather than computed fresh.
Robert Sloan84377092017-08-14 09:33:19 -07001549int tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
1550 int use_saved_value);
David Benjaminc895d6b2016-08-11 13:26:41 -04001551
Robert Sloan4d1ac502017-02-06 08:36:14 -08001552int tls13_add_certificate(SSL_HANDSHAKE *hs);
Robert Sloane56da3e2017-06-26 08:26:42 -07001553
Robert Sloana27a6a42017-09-05 08:39:28 -07001554// tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the
1555// handshake. If it returns |ssl_private_key_retry|, it should be called again
1556// to retry when the signing operation is completed.
Robert Sloane56da3e2017-06-26 08:26:42 -07001557enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs);
1558
Robert Sloan4d1ac502017-02-06 08:36:14 -08001559int tls13_add_finished(SSL_HANDSHAKE *hs);
Robert Sloan84377092017-08-14 09:33:19 -07001560int tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg);
David Benjaminc895d6b2016-08-11 13:26:41 -04001561
Robert Sloan921ef2c2017-10-17 09:02:20 -07001562bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
1563 Array<uint8_t> *out_secret,
1564 uint8_t *out_alert, CBS *contents);
1565bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
1566 Array<uint8_t> *out_secret,
1567 uint8_t *out_alert, CBS *contents);
1568bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
David Benjaminc895d6b2016-08-11 13:26:41 -04001569
Robert Sloan921ef2c2017-10-17 09:02:20 -07001570bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1571 uint8_t *out_alert,
1572 CBS *contents);
1573bool ssl_ext_pre_shared_key_parse_clienthello(
Robert Sloan1c9db532017-03-13 08:03:59 -07001574 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
1575 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001576bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001577
Robert Sloana27a6a42017-09-05 08:39:28 -07001578// ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and
1579// returns one iff it's valid.
Steven Valdez909b19f2016-11-21 15:35:44 -05001580int ssl_is_sct_list_valid(const CBS *contents);
1581
David Benjamin1b249672016-12-06 18:25:50 -05001582int ssl_write_client_hello(SSL_HANDSHAKE *hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04001583
Steven Valdez909b19f2016-11-21 15:35:44 -05001584enum ssl_cert_verify_context_t {
1585 ssl_cert_verify_server,
1586 ssl_cert_verify_client,
1587 ssl_cert_verify_channel_id,
1588};
1589
Robert Sloana27a6a42017-09-05 08:39:28 -07001590// tls13_get_cert_verify_signature_input generates the message to be signed for
1591// TLS 1.3's CertificateVerify message. |cert_verify_context| determines the
Robert Sloan921ef2c2017-10-17 09:02:20 -07001592// type of signature. It sets |*out| to a newly allocated buffer containing the
1593// result. This function returns true on success and false on failure.
1594bool tls13_get_cert_verify_signature_input(
1595 SSL_HANDSHAKE *hs, Array<uint8_t> *out,
Steven Valdez909b19f2016-11-21 15:35:44 -05001596 enum ssl_cert_verify_context_t cert_verify_context);
1597
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001598// ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server
1599// selection for |ssl|'s client preferences.
1600bool ssl_is_alpn_protocol_allowed(const SSL *ssl, Span<const uint8_t> protocol);
1601
Robert Sloana27a6a42017-09-05 08:39:28 -07001602// ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns
Robert Sloan921ef2c2017-10-17 09:02:20 -07001603// true on successful negotiation or if nothing was negotiated. It returns false
Robert Sloana27a6a42017-09-05 08:39:28 -07001604// and sets |*out_alert| to an alert on error.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001605bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1606 const SSL_CLIENT_HELLO *client_hello);
Steven Valdez909b19f2016-11-21 15:35:44 -05001607
Robert Sloanb6d070c2017-07-24 08:40:01 -07001608struct SSL_EXTENSION_TYPE {
Steven Valdez909b19f2016-11-21 15:35:44 -05001609 uint16_t type;
Robert Sloana27a6a42017-09-05 08:39:28 -07001610 bool *out_present;
Steven Valdez909b19f2016-11-21 15:35:44 -05001611 CBS *out_data;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001612};
Steven Valdez909b19f2016-11-21 15:35:44 -05001613
Robert Sloana27a6a42017-09-05 08:39:28 -07001614// ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances
1615// it. It writes the parsed extensions to pointers denoted by |ext_types|. On
1616// success, it fills in the |out_present| and |out_data| fields and returns one.
1617// Otherwise, it sets |*out_alert| to an alert to send and returns zero. Unknown
1618// extensions are rejected unless |ignore_unknown| is 1.
Steven Valdez909b19f2016-11-21 15:35:44 -05001619int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
1620 const SSL_EXTENSION_TYPE *ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -08001621 size_t num_ext_types, int ignore_unknown);
Steven Valdez909b19f2016-11-21 15:35:44 -05001622
Robert Sloana27a6a42017-09-05 08:39:28 -07001623// ssl_verify_peer_cert verifies the peer certificate for |hs|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001624enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs);
1625
Robert Sloan921ef2c2017-10-17 09:02:20 -07001626enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs);
1627bool ssl_send_finished(SSL_HANDSHAKE *hs);
1628bool ssl_output_cert_chain(SSL *ssl);
1629
David Benjaminc895d6b2016-08-11 13:26:41 -04001630
Robert Sloana27a6a42017-09-05 08:39:28 -07001631// SSLKEYLOGFILE functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001632
Robert Sloana27a6a42017-09-05 08:39:28 -07001633// ssl_log_secret logs |secret| with label |label|, if logging is enabled for
1634// |ssl|. It returns one on success and zero on failure.
David Benjaminc895d6b2016-08-11 13:26:41 -04001635int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
1636 size_t secret_len);
1637
1638
Robert Sloana27a6a42017-09-05 08:39:28 -07001639// ClientHello functions.
David Benjaminc895d6b2016-08-11 13:26:41 -04001640
Robert Sloan4562e9d2017-10-02 10:26:51 -07001641int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
1642 const SSLMessage &msg);
David Benjaminc895d6b2016-08-11 13:26:41 -04001643
David Benjamin1b249672016-12-06 18:25:50 -05001644int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
1645 CBS *out, uint16_t extension_type);
David Benjaminc895d6b2016-08-11 13:26:41 -04001646
David Benjamin1b249672016-12-06 18:25:50 -05001647int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
1648 uint16_t id);
David Benjaminc895d6b2016-08-11 13:26:41 -04001649
1650
Robert Sloana27a6a42017-09-05 08:39:28 -07001651// GREASE.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001652
1653enum ssl_grease_index_t {
1654 ssl_grease_cipher = 0,
1655 ssl_grease_group,
1656 ssl_grease_extension1,
1657 ssl_grease_extension2,
1658 ssl_grease_version,
David Benjamin95add822016-10-19 01:09:12 -04001659 ssl_grease_ticket_extension,
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001660};
1661
Robert Sloana27a6a42017-09-05 08:39:28 -07001662// ssl_get_grease_value returns a GREASE value for |ssl|. For a given
1663// connection, the values for each index will be deterministic. This allows the
1664// same ClientHello be sent twice for a HelloRetryRequest or the same group be
1665// advertised in both supported_groups and key_shares.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001666uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index);
1667
1668
Robert Sloana27a6a42017-09-05 08:39:28 -07001669// Signature algorithms.
David Benjamin95add822016-10-19 01:09:12 -04001670
Robert Sloana27a6a42017-09-05 08:39:28 -07001671// tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature
1672// algorithms and saves them on |hs|. It returns one on success and zero on
1673// error.
David Benjamin1b249672016-12-06 18:25:50 -05001674int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs);
David Benjamin95add822016-10-19 01:09:12 -04001675
Robert Sloana27a6a42017-09-05 08:39:28 -07001676// tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm
1677// that should be used with |pkey| in TLS 1.1 and earlier. It returns one on
1678// success and zero if |pkey| may not be used at those versions.
Robert Sloan2424d842017-05-01 07:46:28 -07001679int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey);
1680
Robert Sloana27a6a42017-09-05 08:39:28 -07001681// tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use
1682// with |hs|'s private key based on the peer's preferences and the algorithms
1683// supported. It returns one on success and zero on error.
David Benjamin1b249672016-12-06 18:25:50 -05001684int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out);
David Benjamin95add822016-10-19 01:09:12 -04001685
Robert Sloana27a6a42017-09-05 08:39:28 -07001686// tls12_add_verify_sigalgs adds the signature algorithms acceptable for the
1687// peer signature to |out|. It returns one on success and zero on error.
Robert Sloan572a4e22017-04-17 10:52:19 -07001688int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out);
David Benjamin95add822016-10-19 01:09:12 -04001689
Robert Sloana27a6a42017-09-05 08:39:28 -07001690// tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer
1691// signature. It returns one on success and zero on error, setting |*out_alert|
1692// to an alert to send.
Robert Sloane56da3e2017-06-26 08:26:42 -07001693int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg);
David Benjamin95add822016-10-19 01:09:12 -04001694
1695
Robert Sloana27a6a42017-09-05 08:39:28 -07001696// Underdocumented functions.
1697//
1698// Functions below here haven't been touched up and may be underdocumented.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001699
Adam Langleyd9e397b2015-01-22 14:27:53 -08001700#define TLSEXT_CHANNEL_ID_SIZE 128
1701
Robert Sloana27a6a42017-09-05 08:39:28 -07001702// From RFC4492, used in encoding the curve type in ECParameters
Adam Langleyd9e397b2015-01-22 14:27:53 -08001703#define NAMED_CURVE_TYPE 3
1704
Robert Sloan921ef2c2017-10-17 09:02:20 -07001705struct CERT {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001706 EVP_PKEY *privatekey;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001707
Robert Sloana27a6a42017-09-05 08:39:28 -07001708 // chain contains the certificate chain, with the leaf at the beginning. The
1709 // first element of |chain| may be NULL to indicate that the leaf certificate
1710 // has not yet been set.
1711 // If |chain| != NULL -> len(chain) >= 1
1712 // If |chain[0]| == NULL -> len(chain) >= 2.
1713 // |chain[1..]| != NULL
Robert Sloan4d1ac502017-02-06 08:36:14 -08001714 STACK_OF(CRYPTO_BUFFER) *chain;
1715
Robert Sloana27a6a42017-09-05 08:39:28 -07001716 // x509_chain may contain a parsed copy of |chain[1..]|. This is only used as
1717 // a cache in order to implement “get0” functions that return a non-owning
1718 // pointer to the certificate chain.
Steven Valdez909b19f2016-11-21 15:35:44 -05001719 STACK_OF(X509) *x509_chain;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001720
Robert Sloana27a6a42017-09-05 08:39:28 -07001721 // x509_leaf may contain a parsed copy of the first element of |chain|. This
1722 // is only used as a cache in order to implement “get0” functions that return
1723 // a non-owning pointer to the certificate chain.
Robert Sloan4d1ac502017-02-06 08:36:14 -08001724 X509 *x509_leaf;
1725
Robert Sloana27a6a42017-09-05 08:39:28 -07001726 // x509_stash contains the last |X509| object append to the chain. This is a
1727 // workaround for some third-party code that continue to use an |X509| object
1728 // even after passing ownership with an “add0” function.
Robert Sloan4d1ac502017-02-06 08:36:14 -08001729 X509 *x509_stash;
1730
Robert Sloana27a6a42017-09-05 08:39:28 -07001731 // key_method, if non-NULL, is a set of callbacks to call for private key
1732 // operations.
Kenny Rootb8494592015-09-25 02:29:14 +00001733 const SSL_PRIVATE_KEY_METHOD *key_method;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001734
Robert Sloana27a6a42017-09-05 08:39:28 -07001735 // x509_method contains pointers to functions that might deal with |X509|
1736 // compatibility, or might be a no-op, depending on the application.
Robert Sloan5d625782017-02-13 09:55:39 -08001737 const SSL_X509_METHOD *x509_method;
1738
Robert Sloana27a6a42017-09-05 08:39:28 -07001739 // sigalgs, if non-NULL, is the set of signature algorithms supported by
1740 // |privatekey| in decreasing order of preference.
David Benjaminc895d6b2016-08-11 13:26:41 -04001741 uint16_t *sigalgs;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001742 size_t num_sigalgs;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001743
Robert Sloana27a6a42017-09-05 08:39:28 -07001744 // Certificate setup callback: if set is called whenever a
1745 // certificate may be required (client or server). the callback
1746 // can then examine any appropriate parameters and setup any
1747 // certificates required. This allows advanced applications
1748 // to select certificates on the fly: for example based on
1749 // supported signature algorithms or curves.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001750 int (*cert_cb)(SSL *ssl, void *arg);
1751 void *cert_cb_arg;
David Benjamin4969cc92016-04-22 15:02:23 -04001752
Robert Sloana27a6a42017-09-05 08:39:28 -07001753 // Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX
1754 // store is used instead.
David Benjamin4969cc92016-04-22 15:02:23 -04001755 X509_STORE *verify_store;
Robert Sloana94fe052017-02-21 08:49:28 -08001756
Robert Sloana27a6a42017-09-05 08:39:28 -07001757 // Signed certificate timestamp list to be sent to the client, if requested
Robert Sloana94fe052017-02-21 08:49:28 -08001758 CRYPTO_BUFFER *signed_cert_timestamp_list;
1759
Robert Sloana27a6a42017-09-05 08:39:28 -07001760 // OCSP response to be sent to the client, if requested.
Robert Sloana94fe052017-02-21 08:49:28 -08001761 CRYPTO_BUFFER *ocsp_response;
1762
Robert Sloana27a6a42017-09-05 08:39:28 -07001763 // sid_ctx partitions the session space within a shared session cache or
1764 // ticket key. Only sessions with a matching value will be accepted.
Robert Sloana94fe052017-02-21 08:49:28 -08001765 uint8_t sid_ctx_length;
1766 uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH];
Robert Sloan572a4e22017-04-17 10:52:19 -07001767
Robert Sloana27a6a42017-09-05 08:39:28 -07001768 // If enable_early_data is true, early data can be sent and accepted.
1769 bool enable_early_data:1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001770};
1771
Robert Sloan921ef2c2017-10-17 09:02:20 -07001772// |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS.
1773struct SSL_PROTOCOL_METHOD {
Robert Sloan36272962017-10-23 10:28:39 -07001774 bool is_dtls;
1775 bool (*ssl_new)(SSL *ssl);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001776 void (*ssl_free)(SSL *ssl);
1777 // get_message sets |*out| to the current handshake message and returns true
1778 // if one has been received. It returns false if more input is needed.
1779 bool (*get_message)(SSL *ssl, SSLMessage *out);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001780 // next_message is called to release the current handshake message.
1781 void (*next_message)(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07001782 // Use the |ssl_open_handshake| wrapper.
1783 ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed,
1784 uint8_t *out_alert, Span<uint8_t> in);
1785 // Use the |ssl_open_change_cipher_spec| wrapper.
1786 ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed,
1787 uint8_t *out_alert,
1788 Span<uint8_t> in);
1789 // Use the |ssl_open_app_data| wrapper.
1790 ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out,
1791 size_t *out_consumed, uint8_t *out_alert,
1792 Span<uint8_t> in);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001793 int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
1794 int len);
1795 int (*dispatch_alert)(SSL *ssl);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001796 // init_message begins a new handshake message of type |type|. |cbb| is the
1797 // root CBB to be passed into |finish_message|. |*body| is set to a child CBB
Robert Sloan36272962017-10-23 10:28:39 -07001798 // the caller should write to. It returns true on success and false on error.
1799 bool (*init_message)(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001800 // finish_message finishes a handshake message. It sets |*out_msg| to the
Robert Sloan36272962017-10-23 10:28:39 -07001801 // serialized message. It returns true on success and false on error.
1802 bool (*finish_message)(SSL *ssl, CBB *cbb, bssl::Array<uint8_t> *out_msg);
1803 // add_message adds a handshake message to the pending flight. It returns
1804 // true on success and false on error.
1805 bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001806 // add_change_cipher_spec adds a ChangeCipherSpec record to the pending
Robert Sloan36272962017-10-23 10:28:39 -07001807 // flight. It returns true on success and false on error.
1808 bool (*add_change_cipher_spec)(SSL *ssl);
1809 // add_alert adds an alert to the pending flight. It returns true on success
1810 // and false on error.
1811 bool (*add_alert)(SSL *ssl, uint8_t level, uint8_t desc);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001812 // flush_flight flushes the pending flight to the transport. It returns one on
1813 // success and <= 0 on error.
1814 int (*flush_flight)(SSL *ssl);
1815 // on_handshake_complete is called when the handshake is complete.
1816 void (*on_handshake_complete)(SSL *ssl);
1817 // set_read_state sets |ssl|'s read cipher state to |aead_ctx|. It returns
Robert Sloan36272962017-10-23 10:28:39 -07001818 // true on success and false if changing the read state is forbidden at this
Robert Sloan921ef2c2017-10-17 09:02:20 -07001819 // point.
Robert Sloan36272962017-10-23 10:28:39 -07001820 bool (*set_read_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001821 // set_write_state sets |ssl|'s write cipher state to |aead_ctx|. It returns
Robert Sloan36272962017-10-23 10:28:39 -07001822 // true on success and false if changing the write state is forbidden at this
Robert Sloan921ef2c2017-10-17 09:02:20 -07001823 // point.
Robert Sloan36272962017-10-23 10:28:39 -07001824 bool (*set_write_state)(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001825};
1826
Robert Sloan36272962017-10-23 10:28:39 -07001827// The following wrappers call |open_*| but handle |read_shutdown| correctly.
1828
1829// ssl_open_handshake processes a record from |in| for reading a handshake
1830// message.
1831ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
1832 uint8_t *out_alert, Span<uint8_t> in);
1833
1834// ssl_open_change_cipher_spec processes a record from |in| for reading a
1835// ChangeCipherSpec.
1836ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
1837 uint8_t *out_alert,
1838 Span<uint8_t> in);
1839
1840// ssl_open_app_data processes a record from |in| for reading application data.
1841// On success, it returns |ssl_open_record_success| and sets |*out| to the
1842// input. If it encounters a post-handshake message, it returns
1843// |ssl_open_record_discard|. The caller should then retry, after processing any
1844// messages received with |get_message|.
1845ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
1846 size_t *out_consumed, uint8_t *out_alert,
1847 Span<uint8_t> in);
1848
Robert Sloana27a6a42017-09-05 08:39:28 -07001849// ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using
1850// crypto/x509.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001851extern const SSL_X509_METHOD ssl_crypto_x509_method;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001852
Robert Sloana27a6a42017-09-05 08:39:28 -07001853// ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid
1854// crypto/x509.
Robert Sloan84377092017-08-14 09:33:19 -07001855extern const SSL_X509_METHOD ssl_noop_x509_method;
1856
Robert Sloan921ef2c2017-10-17 09:02:20 -07001857// ssl_cipher_preference_list_st contains a list of SSL_CIPHERs with
1858// equal-preference groups. For TLS clients, the groups are moot because the
1859// server picks the cipher and groups cannot be expressed on the wire. However,
1860// for servers, the equal-preference groups allow the client's preferences to
1861// be partially respected. (This only has an effect with
1862// SSL_OP_CIPHER_SERVER_PREFERENCE).
1863//
1864// The equal-preference groups are expressed by grouping SSL_CIPHERs together.
1865// All elements of a group have the same priority: no ordering is expressed
1866// within a group.
1867//
1868// The values in |ciphers| are in one-to-one correspondence with
1869// |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of
1870// bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to
1871// indicate that the corresponding SSL_CIPHER is not the last element of a
1872// group, or 0 to indicate that it is.
1873//
1874// For example, if |in_group_flags| contains all zeros then that indicates a
1875// traditional, fully-ordered preference. Every SSL_CIPHER is the last element
1876// of the group (i.e. they are all in a one-element group).
1877//
1878// For a more complex example, consider:
1879// ciphers: A B C D E F
1880// in_group_flags: 1 1 0 0 1 0
1881//
1882// That would express the following, order:
1883//
1884// A E
1885// B -> D -> F
1886// C
1887struct ssl_cipher_preference_list_st {
1888 STACK_OF(SSL_CIPHER) *ciphers;
1889 uint8_t *in_group_flags;
1890};
1891
1892struct tlsext_ticket_key {
1893 static constexpr bool kAllowUniquePtr = true;
1894
1895 uint8_t name[SSL_TICKET_KEY_NAME_LEN];
1896 uint8_t hmac_key[16];
1897 uint8_t aes_key[16];
1898 // next_rotation_tv_sec is the time (in seconds from the epoch) when the
1899 // current key should be superseded by a new key, or the time when a previous
1900 // key should be dropped. If zero, then the key should not be automatically
1901 // rotated.
1902 uint64_t next_rotation_tv_sec;
1903};
1904
1905} // namespace bssl
1906
1907DECLARE_LHASH_OF(SSL_SESSION)
1908
1909namespace bssl {
1910
1911// SSLContext backs the public |SSL_CTX| type. Due to compatibility constraints,
1912// it is a base class for |ssl_ctx_st|.
1913struct SSLContext {
1914 const SSL_PROTOCOL_METHOD *method;
1915 const SSL_X509_METHOD *x509_method;
1916
1917 // lock is used to protect various operations on this object.
1918 CRYPTO_MUTEX lock;
1919
1920 // conf_max_version is the maximum acceptable protocol version configured by
1921 // |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS
1922 // and is further constrainted by |SSL_OP_NO_*|.
1923 uint16_t conf_max_version;
1924
1925 // conf_min_version is the minimum acceptable protocol version configured by
1926 // |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS
1927 // and is further constrainted by |SSL_OP_NO_*|.
1928 uint16_t conf_min_version;
1929
1930 // tls13_variant is the variant of TLS 1.3 we are using for this
1931 // configuration.
1932 enum tls13_variant_t tls13_variant;
1933
1934 struct ssl_cipher_preference_list_st *cipher_list;
1935
1936 X509_STORE *cert_store;
1937 LHASH_OF(SSL_SESSION) *sessions;
1938 // Most session-ids that will be cached, default is
1939 // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
1940 unsigned long session_cache_size;
1941 SSL_SESSION *session_cache_head;
1942 SSL_SESSION *session_cache_tail;
1943
1944 // handshakes_since_cache_flush is the number of successful handshakes since
1945 // the last cache flush.
1946 int handshakes_since_cache_flush;
1947
1948 // This can have one of 2 values, ored together,
1949 // SSL_SESS_CACHE_CLIENT,
1950 // SSL_SESS_CACHE_SERVER,
1951 // Default is SSL_SESSION_CACHE_SERVER, which means only
1952 // SSL_accept which cache SSL_SESSIONS.
1953 int session_cache_mode;
1954
1955 // session_timeout is the default lifetime for new sessions in TLS 1.2 and
1956 // earlier, in seconds.
1957 uint32_t session_timeout;
1958
1959 // session_psk_dhe_timeout is the default lifetime for new sessions in TLS
1960 // 1.3, in seconds.
1961 uint32_t session_psk_dhe_timeout;
1962
1963 // If this callback is not null, it will be called each time a session id is
1964 // added to the cache. If this function returns 1, it means that the
1965 // callback will do a SSL_SESSION_free() when it has finished using it.
1966 // Otherwise, on 0, it means the callback has finished with it. If
1967 // remove_session_cb is not null, it will be called when a session-id is
1968 // removed from the cache. After the call, OpenSSL will SSL_SESSION_free()
1969 // it.
1970 int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess);
1971 void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess);
1972 SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len,
1973 int *copy);
1974 SSL_SESSION *(*get_session_cb_legacy)(SSL *ssl, uint8_t *data, int len,
1975 int *copy);
1976
1977 CRYPTO_refcount_t references;
1978
1979 // if defined, these override the X509_verify_cert() calls
1980 int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg);
1981 void *app_verify_arg;
1982
1983 enum ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
1984 uint8_t *out_alert);
1985
1986 // Default password callback.
1987 pem_password_cb *default_passwd_callback;
1988
1989 // Default password callback user data.
1990 void *default_passwd_callback_userdata;
1991
1992 // get client cert callback
1993 int (*client_cert_cb)(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey);
1994
1995 // get channel id callback
1996 void (*channel_id_cb)(SSL *ssl, EVP_PKEY **out_pkey);
1997
1998 CRYPTO_EX_DATA ex_data;
1999
2000 // custom_*_extensions stores any callback sets for custom extensions. Note
2001 // that these pointers will be NULL if the stack would otherwise be empty.
2002 STACK_OF(SSL_CUSTOM_EXTENSION) *client_custom_extensions;
2003 STACK_OF(SSL_CUSTOM_EXTENSION) *server_custom_extensions;
2004
2005 // Default values used when no per-SSL value is defined follow
2006
2007 void (*info_callback)(const SSL *ssl, int type, int value);
2008
2009 // what we put in client cert requests
2010 STACK_OF(CRYPTO_BUFFER) *client_CA;
2011
2012 // cached_x509_client_CA is a cache of parsed versions of the elements of
2013 // |client_CA|.
2014 STACK_OF(X509_NAME) *cached_x509_client_CA;
2015
2016
2017 // Default values to use in SSL structures follow (these are copied by
2018 // SSL_new)
2019
2020 uint32_t options;
2021 uint32_t mode;
2022 uint32_t max_cert_list;
2023
2024 CERT *cert;
2025
2026 // callback that allows applications to peek at protocol messages
2027 void (*msg_callback)(int write_p, int version, int content_type,
2028 const void *buf, size_t len, SSL *ssl, void *arg);
2029 void *msg_callback_arg;
2030
2031 int verify_mode;
2032 int (*default_verify_callback)(
2033 int ok, X509_STORE_CTX *ctx); // called 'verify_callback' in the SSL
2034
2035 X509_VERIFY_PARAM *param;
2036
2037 // select_certificate_cb is called before most ClientHello processing and
2038 // before the decision whether to resume a session is made. See
2039 // |ssl_select_cert_result_t| for details of the return values.
2040 enum ssl_select_cert_result_t (*select_certificate_cb)(
2041 const SSL_CLIENT_HELLO *);
2042
2043 // dos_protection_cb is called once the resumption decision for a ClientHello
2044 // has been made. It returns one to continue the handshake or zero to
2045 // abort.
2046 int (*dos_protection_cb) (const SSL_CLIENT_HELLO *);
2047
2048 // Maximum amount of data to send in one fragment. actual record size can be
2049 // more than this due to padding and MAC overheads.
2050 uint16_t max_send_fragment;
2051
2052 // TLS extensions servername callback
2053 int (*tlsext_servername_callback)(SSL *, int *, void *);
2054 void *tlsext_servername_arg;
2055
2056 // RFC 4507 session ticket keys. |tlsext_ticket_key_current| may be NULL
2057 // before the first handshake and |tlsext_ticket_key_prev| may be NULL at any
2058 // time. Automatically generated ticket keys are rotated as needed at
2059 // handshake time. Hence, all access must be synchronized through |lock|.
2060 struct tlsext_ticket_key *tlsext_ticket_key_current;
2061 struct tlsext_ticket_key *tlsext_ticket_key_prev;
2062
2063 // Callback to support customisation of ticket key setting
2064 int (*tlsext_ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv,
2065 EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
2066
2067 // Server-only: psk_identity_hint is the default identity hint to send in
2068 // PSK-based key exchanges.
2069 char *psk_identity_hint;
2070
2071 unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
2072 char *identity,
2073 unsigned int max_identity_len,
2074 uint8_t *psk, unsigned int max_psk_len);
2075 unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
2076 uint8_t *psk, unsigned int max_psk_len);
2077
2078
2079 // Next protocol negotiation information
2080 // (for experimental NPN extension).
2081
2082 // For a server, this contains a callback function by which the set of
2083 // advertised protocols can be provided.
2084 int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out,
2085 unsigned *out_len, void *arg);
2086 void *next_protos_advertised_cb_arg;
2087 // For a client, this contains a callback function that selects the
2088 // next protocol from the list provided by the server.
2089 int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
2090 const uint8_t *in, unsigned in_len, void *arg);
2091 void *next_proto_select_cb_arg;
2092
2093 // ALPN information
2094 // (we are in the process of transitioning from NPN to ALPN.)
2095
2096 // For a server, this contains a callback function that allows the
2097 // server to select the protocol for the connection.
2098 // out: on successful return, this must point to the raw protocol
2099 // name (without the length prefix).
2100 // outlen: on successful return, this contains the length of |*out|.
2101 // in: points to the client's list of supported protocols in
2102 // wire-format.
2103 // inlen: the length of |in|.
2104 int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len,
2105 const uint8_t *in, unsigned in_len, void *arg);
2106 void *alpn_select_cb_arg;
2107
2108 // For a client, this contains the list of supported protocols in wire
2109 // format.
2110 uint8_t *alpn_client_proto_list;
2111 unsigned alpn_client_proto_list_len;
2112
2113 // SRTP profiles we are willing to do from RFC 5764
2114 STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
2115
2116 // Supported group values inherited by SSL structure
2117 size_t supported_group_list_len;
2118 uint16_t *supported_group_list;
2119
2120 // The client's Channel ID private key.
2121 EVP_PKEY *tlsext_channel_id_private;
2122
2123 // keylog_callback, if not NULL, is the key logging callback. See
2124 // |SSL_CTX_set_keylog_callback|.
2125 void (*keylog_callback)(const SSL *ssl, const char *line);
2126
2127 // current_time_cb, if not NULL, is the function to use to get the current
2128 // time. It sets |*out_clock| to the current time. The |ssl| argument is
2129 // always NULL. See |SSL_CTX_set_current_time_cb|.
2130 void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock);
2131
2132 // pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate
2133 // memory.
2134 CRYPTO_BUFFER_POOL *pool;
2135
2136 // ticket_aead_method contains function pointers for opening and sealing
2137 // session tickets.
2138 const SSL_TICKET_AEAD_METHOD *ticket_aead_method;
2139
2140 // verify_sigalgs, if not empty, is the set of signature algorithms
2141 // accepted from the peer in decreasing order of preference.
2142 uint16_t *verify_sigalgs;
2143 size_t num_verify_sigalgs;
2144
2145 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
2146 // hash of the peer's certificate and then discard it to save memory and
2147 // session space. Only effective on the server side.
2148 bool retain_only_sha256_of_client_certs:1;
2149
2150 // quiet_shutdown is true if the connection should not send a close_notify on
2151 // shutdown.
2152 bool quiet_shutdown:1;
2153
2154 // ocsp_stapling_enabled is only used by client connections and indicates
2155 // whether OCSP stapling will be requested.
2156 bool ocsp_stapling_enabled:1;
2157
2158 // If true, a client will request certificate timestamps.
2159 bool signed_cert_timestamps_enabled:1;
2160
2161 // tlsext_channel_id_enabled is one if Channel ID is enabled and zero
2162 // otherwise. For a server, means that we'll accept Channel IDs from clients.
2163 // For a client, means that we'll advertise support.
2164 bool tlsext_channel_id_enabled:1;
2165
2166 // grease_enabled is one if draft-davidben-tls-grease-01 is enabled and zero
2167 // otherwise.
2168 bool grease_enabled:1;
2169
2170 // allow_unknown_alpn_protos is one if the client allows unsolicited ALPN
2171 // protocols from the peer.
2172 bool allow_unknown_alpn_protos:1;
2173
2174 // ed25519_enabled is one if Ed25519 is advertised in the handshake.
2175 bool ed25519_enabled:1;
2176};
2177
Robert Sloana27a6a42017-09-05 08:39:28 -07002178// An ssl_shutdown_t describes the shutdown state of one end of the connection,
2179// whether it is alive or has been shutdown via close_notify or fatal alert.
David Benjamin95add822016-10-19 01:09:12 -04002180enum ssl_shutdown_t {
2181 ssl_shutdown_none = 0,
2182 ssl_shutdown_close_notify = 1,
Robert Sloan36272962017-10-23 10:28:39 -07002183 ssl_shutdown_error = 2,
David Benjamin95add822016-10-19 01:09:12 -04002184};
2185
Robert Sloanb6d070c2017-07-24 08:40:01 -07002186struct SSL3_STATE {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002187 static constexpr bool kAllowUniquePtr = true;
David Benjamin95add822016-10-19 01:09:12 -04002188
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002189 SSL3_STATE();
2190 ~SSL3_STATE();
2191
2192 uint8_t read_sequence[8] = {0};
2193 uint8_t write_sequence[8] = {0};
2194
2195 uint8_t server_random[SSL3_RANDOM_SIZE] = {0};
2196 uint8_t client_random[SSL3_RANDOM_SIZE] = {0};
David Benjamin95add822016-10-19 01:09:12 -04002197
Robert Sloana27a6a42017-09-05 08:39:28 -07002198 // read_buffer holds data from the transport to be processed.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002199 SSLBuffer read_buffer;
Robert Sloana27a6a42017-09-05 08:39:28 -07002200 // write_buffer holds data to be written to the transport.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002201 SSLBuffer write_buffer;
David Benjamin95add822016-10-19 01:09:12 -04002202
Robert Sloan36272962017-10-23 10:28:39 -07002203 // pending_app_data is the unconsumed application data. It points into
2204 // |read_buffer|.
2205 Span<uint8_t> pending_app_data;
David Benjamin95add822016-10-19 01:09:12 -04002206
Robert Sloana27a6a42017-09-05 08:39:28 -07002207 // partial write - check the numbers match
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002208 unsigned int wnum = 0; // number of bytes sent so far
2209 int wpend_tot = 0; // number bytes written
2210 int wpend_type = 0;
2211 int wpend_ret = 0; // number of bytes submitted
2212 const uint8_t *wpend_buf = nullptr;
David Benjamin95add822016-10-19 01:09:12 -04002213
Robert Sloan921ef2c2017-10-17 09:02:20 -07002214 // read_shutdown is the shutdown state for the read half of the connection.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002215 enum ssl_shutdown_t read_shutdown = ssl_shutdown_none;
David Benjamin95add822016-10-19 01:09:12 -04002216
Robert Sloan921ef2c2017-10-17 09:02:20 -07002217 // write_shutdown is the shutdown state for the write half of the connection.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002218 enum ssl_shutdown_t write_shutdown = ssl_shutdown_none;
David Benjamin95add822016-10-19 01:09:12 -04002219
Robert Sloan36272962017-10-23 10:28:39 -07002220 // read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for
2221 // the receive half of the connection.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002222 UniquePtr<ERR_SAVE_STATE> read_error;
Robert Sloan36272962017-10-23 10:28:39 -07002223
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002224 int alert_dispatch = 0;
David Benjamin95add822016-10-19 01:09:12 -04002225
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002226 int total_renegotiations = 0;
2227
2228 // This holds a variable that indicates what we were doing when a 0 or -1 is
2229 // returned. This is needed for non-blocking IO so we know what request
2230 // needs re-doing when in SSL_accept or SSL_connect
2231 int rwstate = SSL_NOTHING;
David Benjamin95add822016-10-19 01:09:12 -04002232
Robert Sloana27a6a42017-09-05 08:39:28 -07002233 // early_data_skipped is the amount of early data that has been skipped by the
2234 // record layer.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002235 uint16_t early_data_skipped = 0;
David Benjamin1b249672016-12-06 18:25:50 -05002236
Robert Sloana27a6a42017-09-05 08:39:28 -07002237 // empty_record_count is the number of consecutive empty records received.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002238 uint8_t empty_record_count = 0;
David Benjamin95add822016-10-19 01:09:12 -04002239
Robert Sloana27a6a42017-09-05 08:39:28 -07002240 // warning_alert_count is the number of consecutive warning alerts
2241 // received.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002242 uint8_t warning_alert_count = 0;
David Benjamin95add822016-10-19 01:09:12 -04002243
Robert Sloana27a6a42017-09-05 08:39:28 -07002244 // key_update_count is the number of consecutive KeyUpdates received.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002245 uint8_t key_update_count = 0;
David Benjamin95add822016-10-19 01:09:12 -04002246
Robert Sloana27a6a42017-09-05 08:39:28 -07002247 // skip_early_data instructs the record layer to skip unexpected early data
2248 // messages when 0RTT is rejected.
2249 bool skip_early_data:1;
David Benjamin1b249672016-12-06 18:25:50 -05002250
Robert Sloana27a6a42017-09-05 08:39:28 -07002251 // have_version is true if the connection's final version is known. Otherwise
2252 // the version has not been negotiated yet.
2253 bool have_version:1;
David Benjamin1b249672016-12-06 18:25:50 -05002254
Robert Sloana27a6a42017-09-05 08:39:28 -07002255 // v2_hello_done is true if the peer's V2ClientHello, if any, has been handled
2256 // and future messages should use the record layer.
2257 bool v2_hello_done:1;
David Benjamin1b249672016-12-06 18:25:50 -05002258
Robert Sloana27a6a42017-09-05 08:39:28 -07002259 // is_v2_hello is true if the current handshake message was derived from a
2260 // V2ClientHello rather than received from the peer directly.
2261 bool is_v2_hello:1;
Robert Sloan4d1ac502017-02-06 08:36:14 -08002262
Robert Sloana27a6a42017-09-05 08:39:28 -07002263 // has_message is true if the current handshake message has been returned
2264 // at least once by |get_message| and false otherwise.
2265 bool has_message:1;
Robert Sloan84377092017-08-14 09:33:19 -07002266
Robert Sloana27a6a42017-09-05 08:39:28 -07002267 // initial_handshake_complete is true if the initial handshake has
2268 // completed.
2269 bool initial_handshake_complete:1;
David Benjamin1b249672016-12-06 18:25:50 -05002270
Robert Sloana27a6a42017-09-05 08:39:28 -07002271 // session_reused indicates whether a session was resumed.
2272 bool session_reused:1;
David Benjamin1b249672016-12-06 18:25:50 -05002273
Robert Sloana27a6a42017-09-05 08:39:28 -07002274 bool send_connection_binding:1;
David Benjamin1b249672016-12-06 18:25:50 -05002275
Robert Sloana27a6a42017-09-05 08:39:28 -07002276 // In a client, this means that the server supported Channel ID and that a
2277 // Channel ID was sent. In a server it means that we echoed support for
2278 // Channel IDs and that tlsext_channel_id will be valid after the
2279 // handshake.
2280 bool tlsext_channel_id_valid:1;
David Benjamin1b249672016-12-06 18:25:50 -05002281
Robert Sloana27a6a42017-09-05 08:39:28 -07002282 // key_update_pending is true if we have a KeyUpdate acknowledgment
2283 // outstanding.
2284 bool key_update_pending:1;
Robert Sloan572a4e22017-04-17 10:52:19 -07002285
Robert Sloana27a6a42017-09-05 08:39:28 -07002286 // wpend_pending is true if we have a pending write outstanding.
2287 bool wpend_pending:1;
Robert Sloane56da3e2017-06-26 08:26:42 -07002288
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002289 uint8_t send_alert[2] = {0};
2290
2291 // hs_buf is the buffer of handshake data to process.
2292 UniquePtr<BUF_MEM> hs_buf;
David Benjamin1b249672016-12-06 18:25:50 -05002293
Robert Sloana27a6a42017-09-05 08:39:28 -07002294 // pending_flight is the pending outgoing flight. This is used to flush each
2295 // handshake flight in a single write. |write_buffer| must be written out
2296 // before this data.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002297 UniquePtr<BUF_MEM> pending_flight;
Robert Sloan4d1ac502017-02-06 08:36:14 -08002298
Robert Sloana27a6a42017-09-05 08:39:28 -07002299 // pending_flight_offset is the number of bytes of |pending_flight| which have
2300 // been successfully written.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002301 uint32_t pending_flight_offset = 0;
David Benjamin1b249672016-12-06 18:25:50 -05002302
Robert Sloana27a6a42017-09-05 08:39:28 -07002303 // aead_read_ctx is the current read cipher state.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002304 UniquePtr<SSLAEADContext> aead_read_ctx;
David Benjamin95add822016-10-19 01:09:12 -04002305
Robert Sloana27a6a42017-09-05 08:39:28 -07002306 // aead_write_ctx is the current write cipher state.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002307 UniquePtr<SSLAEADContext> aead_write_ctx;
David Benjamin95add822016-10-19 01:09:12 -04002308
Robert Sloana27a6a42017-09-05 08:39:28 -07002309 // hs is the handshake state for the current handshake or NULL if there isn't
2310 // one.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002311 UniquePtr<SSL_HANDSHAKE> hs;
David Benjamin95add822016-10-19 01:09:12 -04002312
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002313 uint8_t write_traffic_secret[EVP_MAX_MD_SIZE] = {0};
2314 uint8_t read_traffic_secret[EVP_MAX_MD_SIZE] = {0};
2315 uint8_t exporter_secret[EVP_MAX_MD_SIZE] = {0};
2316 uint8_t early_exporter_secret[EVP_MAX_MD_SIZE] = {0};
2317 uint8_t write_traffic_secret_len = 0;
2318 uint8_t read_traffic_secret_len = 0;
2319 uint8_t exporter_secret_len = 0;
2320 uint8_t early_exporter_secret_len = 0;
David Benjamin95add822016-10-19 01:09:12 -04002321
Robert Sloana27a6a42017-09-05 08:39:28 -07002322 // Connection binding to prevent renegotiation attacks
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002323 uint8_t previous_client_finished[12] = {0};
2324 uint8_t previous_client_finished_len = 0;
2325 uint8_t previous_server_finished_len = 0;
2326 uint8_t previous_server_finished[12] = {0};
David Benjamin95add822016-10-19 01:09:12 -04002327
Robert Sloana27a6a42017-09-05 08:39:28 -07002328 // established_session is the session established by the connection. This
2329 // session is only filled upon the completion of the handshake and is
2330 // immutable.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002331 UniquePtr<SSL_SESSION> established_session;
David Benjamin95add822016-10-19 01:09:12 -04002332
Robert Sloana27a6a42017-09-05 08:39:28 -07002333 // Next protocol negotiation. For the client, this is the protocol that we
2334 // sent in NextProtocol and is set when handling ServerHello extensions.
2335 //
2336 // For a server, this is the client's selected_protocol from NextProtocol and
2337 // is set when handling the NextProtocol message, before the Finished
2338 // message.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002339 Array<uint8_t> next_proto_negotiated;
David Benjamin95add822016-10-19 01:09:12 -04002340
Robert Sloana27a6a42017-09-05 08:39:28 -07002341 // ALPN information
2342 // (we are in the process of transitioning from NPN to ALPN.)
David Benjamin95add822016-10-19 01:09:12 -04002343
Robert Sloana27a6a42017-09-05 08:39:28 -07002344 // In a server these point to the selected ALPN protocol after the
2345 // ClientHello has been processed. In a client these contain the protocol
2346 // that the server selected once the ServerHello has been processed.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002347 Array<uint8_t> alpn_selected;
David Benjamin95add822016-10-19 01:09:12 -04002348
Robert Sloand1d118f2017-09-11 09:00:48 -07002349 // hostname, on the server, is the value of the SNI extension.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002350 UniquePtr<char> hostname;
Robert Sloand1d118f2017-09-11 09:00:48 -07002351
Robert Sloana27a6a42017-09-05 08:39:28 -07002352 // For a server:
2353 // If |tlsext_channel_id_valid| is true, then this contains the
2354 // verified Channel ID from the client: a P256 point, (x,y), where
2355 // each are big-endian values.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002356 uint8_t tlsext_channel_id[64] = {0};
Robert Sloan1c9db532017-03-13 08:03:59 -07002357
Robert Sloana27a6a42017-09-05 08:39:28 -07002358 // ticket_age_skew is the difference, in seconds, between the client-sent
2359 // ticket age and the server-computed value in TLS 1.3 server connections
2360 // which resumed a session.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002361 int32_t ticket_age_skew = 0;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002362};
David Benjamin95add822016-10-19 01:09:12 -04002363
Robert Sloana27a6a42017-09-05 08:39:28 -07002364// lengths of messages
Adam Langleye9ada862015-05-11 17:20:37 -07002365#define DTLS1_COOKIE_LENGTH 256
2366
2367#define DTLS1_RT_HEADER_LENGTH 13
2368
2369#define DTLS1_HM_HEADER_LENGTH 12
2370
2371#define DTLS1_CCS_HEADER_LENGTH 1
2372
2373#define DTLS1_AL_HEADER_LENGTH 2
2374
Adam Langleye9ada862015-05-11 17:20:37 -07002375struct hm_header_st {
2376 uint8_t type;
2377 uint32_t msg_len;
2378 uint16_t seq;
2379 uint32_t frag_off;
2380 uint32_t frag_len;
Adam Langleye9ada862015-05-11 17:20:37 -07002381};
2382
Robert Sloana27a6a42017-09-05 08:39:28 -07002383// An hm_fragment is an incoming DTLS message, possibly not yet assembled.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002384struct hm_fragment {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002385 static constexpr bool kAllowUniquePtr = true;
2386
2387 hm_fragment() {}
2388 hm_fragment(const hm_fragment &) = delete;
2389 hm_fragment &operator=(const hm_fragment &) = delete;
2390
2391 ~hm_fragment();
2392
Robert Sloana27a6a42017-09-05 08:39:28 -07002393 // type is the type of the message.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002394 uint8_t type = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002395 // seq is the sequence number of this message.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002396 uint16_t seq = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002397 // msg_len is the length of the message body.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002398 uint32_t msg_len = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002399 // data is a pointer to the message, including message header. It has length
2400 // |DTLS1_HM_HEADER_LENGTH| + |msg_len|.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002401 uint8_t *data = nullptr;
Robert Sloana27a6a42017-09-05 08:39:28 -07002402 // reassembly is a bitmask of |msg_len| bits corresponding to which parts of
2403 // the message have been received. It is NULL if the message is complete.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002404 uint8_t *reassembly = nullptr;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002405};
Adam Langleye9ada862015-05-11 17:20:37 -07002406
Robert Sloan7d422bc2017-03-06 10:04:29 -08002407struct OPENSSL_timeval {
2408 uint64_t tv_sec;
2409 uint32_t tv_usec;
2410};
2411
Robert Sloanb6d070c2017-07-24 08:40:01 -07002412struct DTLS1_STATE {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002413 static constexpr bool kAllowUniquePtr = true;
2414
2415 DTLS1_STATE();
2416 ~DTLS1_STATE();
2417
Robert Sloana27a6a42017-09-05 08:39:28 -07002418 // has_change_cipher_spec is true if we have received a ChangeCipherSpec from
2419 // the peer in this epoch.
Robert Sloanfe7cd212017-08-07 09:03:39 -07002420 bool has_change_cipher_spec:1;
2421
Robert Sloana27a6a42017-09-05 08:39:28 -07002422 // outgoing_messages_complete is true if |outgoing_messages| has been
2423 // completed by an attempt to flush it. Future calls to |add_message| and
2424 // |add_change_cipher_spec| will start a new flight.
Robert Sloanfe7cd212017-08-07 09:03:39 -07002425 bool outgoing_messages_complete:1;
Adam Langleye9ada862015-05-11 17:20:37 -07002426
Robert Sloana27a6a42017-09-05 08:39:28 -07002427 // flight_has_reply is true if the current outgoing flight is complete and has
2428 // processed at least one message. This is used to detect whether we or the
2429 // peer sent the final flight.
Robert Sloan8f860b12017-08-28 07:37:06 -07002430 bool flight_has_reply:1;
2431
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002432 uint8_t cookie[DTLS1_COOKIE_LENGTH] = {0};
2433 size_t cookie_len = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002434
Robert Sloana27a6a42017-09-05 08:39:28 -07002435 // The current data and handshake epoch. This is initially undefined, and
2436 // starts at zero once the initial handshake is completed.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002437 uint16_t r_epoch = 0;
2438 uint16_t w_epoch = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002439
Robert Sloana27a6a42017-09-05 08:39:28 -07002440 // records being received in the current epoch
Adam Langleye9ada862015-05-11 17:20:37 -07002441 DTLS1_BITMAP bitmap;
2442
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002443 uint16_t handshake_write_seq = 0;
2444 uint16_t handshake_read_seq = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002445
Robert Sloana27a6a42017-09-05 08:39:28 -07002446 // save last sequence number for retransmissions
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002447 uint8_t last_write_sequence[8] = {0};
2448 UniquePtr<SSLAEADContext> last_aead_write_ctx;
Adam Langleye9ada862015-05-11 17:20:37 -07002449
Robert Sloana27a6a42017-09-05 08:39:28 -07002450 // incoming_messages is a ring buffer of incoming handshake messages that have
2451 // yet to be processed. The front of the ring buffer is message number
2452 // |handshake_read_seq|, at position |handshake_read_seq| %
2453 // |SSL_MAX_HANDSHAKE_FLIGHT|.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002454 UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT];
Adam Langleye9ada862015-05-11 17:20:37 -07002455
Robert Sloana27a6a42017-09-05 08:39:28 -07002456 // outgoing_messages is the queue of outgoing messages from the last handshake
2457 // flight.
David Benjaminc895d6b2016-08-11 13:26:41 -04002458 DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT];
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002459 uint8_t outgoing_messages_len = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002460
Robert Sloana27a6a42017-09-05 08:39:28 -07002461 // outgoing_written is the number of outgoing messages that have been
2462 // written.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002463 uint8_t outgoing_written = 0;
Robert Sloana27a6a42017-09-05 08:39:28 -07002464 // outgoing_offset is the number of bytes of the next outgoing message have
2465 // been written.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002466 uint32_t outgoing_offset = 0;
Robert Sloan4d1ac502017-02-06 08:36:14 -08002467
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002468 unsigned mtu = 0; // max DTLS packet size
Adam Langleye9ada862015-05-11 17:20:37 -07002469
Robert Sloana27a6a42017-09-05 08:39:28 -07002470 // num_timeouts is the number of times the retransmit timer has fired since
2471 // the last time it was reset.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002472 unsigned num_timeouts = 0;
Adam Langleye9ada862015-05-11 17:20:37 -07002473
Robert Sloana27a6a42017-09-05 08:39:28 -07002474 // Indicates when the last handshake msg or heartbeat sent will
2475 // timeout.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002476 struct OPENSSL_timeval next_timeout = {0, 0};
Adam Langleye9ada862015-05-11 17:20:37 -07002477
Robert Sloana27a6a42017-09-05 08:39:28 -07002478 // timeout_duration_ms is the timeout duration in milliseconds.
Robert Sloan29c1d2c2017-10-30 14:10:28 -07002479 unsigned timeout_duration_ms = 0;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002480};
Adam Langleye9ada862015-05-11 17:20:37 -07002481
Robert Sloana27a6a42017-09-05 08:39:28 -07002482// SSLConnection backs the public |SSL| type. Due to compatibility constraints,
2483// it is a base class for |ssl_st|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002484struct SSLConnection {
Robert Sloana27a6a42017-09-05 08:39:28 -07002485 // method is the method table corresponding to the current protocol (DTLS or
2486 // TLS).
Robert Sloan47f43ed2017-02-06 14:55:15 -08002487 const SSL_PROTOCOL_METHOD *method;
2488
Robert Sloana27a6a42017-09-05 08:39:28 -07002489 // version is the protocol version.
Robert Sloanf6200e72017-07-10 08:09:18 -07002490 uint16_t version;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002491
Robert Sloana27a6a42017-09-05 08:39:28 -07002492 // conf_max_version is the maximum acceptable protocol version configured by
2493 // |SSL_set_max_proto_version|. Note this version is normalized in DTLS and is
2494 // further constrainted by |SSL_OP_NO_*|.
Robert Sloane56da3e2017-06-26 08:26:42 -07002495 uint16_t conf_max_version;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002496
Robert Sloana27a6a42017-09-05 08:39:28 -07002497 // conf_min_version is the minimum acceptable protocol version configured by
2498 // |SSL_set_min_proto_version|. Note this version is normalized in DTLS and is
2499 // further constrainted by |SSL_OP_NO_*|.
Robert Sloane56da3e2017-06-26 08:26:42 -07002500 uint16_t conf_min_version;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002501
Robert Sloana27a6a42017-09-05 08:39:28 -07002502 // tls13_variant is the variant of TLS 1.3 we are using for this
2503 // configuration.
Robert Sloana12bf462017-07-17 07:08:26 -07002504 enum tls13_variant_t tls13_variant;
2505
Robert Sloan47f43ed2017-02-06 14:55:15 -08002506 uint16_t max_send_fragment;
2507
Robert Sloana27a6a42017-09-05 08:39:28 -07002508 // There are 2 BIO's even though they are normally both the same. This is so
2509 // data can be read and written to different handlers
Robert Sloan47f43ed2017-02-06 14:55:15 -08002510
Robert Sloana27a6a42017-09-05 08:39:28 -07002511 BIO *rbio; // used by SSL_read
2512 BIO *wbio; // used by SSL_write
Robert Sloan47f43ed2017-02-06 14:55:15 -08002513
Robert Sloana27a6a42017-09-05 08:39:28 -07002514 // do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|.
2515 // Otherwise, it returns a value corresponding to what operation is needed to
2516 // progress.
2517 enum ssl_hs_wait_t (*do_handshake)(SSL_HANDSHAKE *hs);
Robert Sloan47f43ed2017-02-06 14:55:15 -08002518
Robert Sloana27a6a42017-09-05 08:39:28 -07002519 SSL3_STATE *s3; // SSLv3 variables
2520 DTLS1_STATE *d1; // DTLSv1 variables
Robert Sloan47f43ed2017-02-06 14:55:15 -08002521
Robert Sloana27a6a42017-09-05 08:39:28 -07002522 // callback that allows applications to peek at protocol messages
Robert Sloan47f43ed2017-02-06 14:55:15 -08002523 void (*msg_callback)(int write_p, int version, int content_type,
2524 const void *buf, size_t len, SSL *ssl, void *arg);
2525 void *msg_callback_arg;
2526
2527 X509_VERIFY_PARAM *param;
2528
Robert Sloana27a6a42017-09-05 08:39:28 -07002529 // crypto
Robert Sloan47f43ed2017-02-06 14:55:15 -08002530 struct ssl_cipher_preference_list_st *cipher_list;
2531
Robert Sloana27a6a42017-09-05 08:39:28 -07002532 // session info
Robert Sloan47f43ed2017-02-06 14:55:15 -08002533
Robert Sloana27a6a42017-09-05 08:39:28 -07002534 // client cert?
2535 // This is used to hold the server certificate used
Robert Sloanb6d070c2017-07-24 08:40:01 -07002536 CERT *cert;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002537
Robert Sloana27a6a42017-09-05 08:39:28 -07002538 // initial_timeout_duration_ms is the default DTLS timeout duration in
2539 // milliseconds. It's used to initialize the timer any time it's restarted.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002540 unsigned initial_timeout_duration_ms;
2541
Robert Sloana27a6a42017-09-05 08:39:28 -07002542 // session is the configured session to be offered by the client. This session
2543 // is immutable.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002544 SSL_SESSION *session;
2545
2546 int (*verify_callback)(int ok,
Robert Sloana27a6a42017-09-05 08:39:28 -07002547 X509_STORE_CTX *ctx); // fail if callback returns 0
Robert Sloan47f43ed2017-02-06 14:55:15 -08002548
Robert Sloanb6d070c2017-07-24 08:40:01 -07002549 enum ssl_verify_result_t (*custom_verify_callback)(SSL *ssl,
2550 uint8_t *out_alert);
2551
Robert Sloan47f43ed2017-02-06 14:55:15 -08002552 void (*info_callback)(const SSL *ssl, int type, int value);
2553
Robert Sloana27a6a42017-09-05 08:39:28 -07002554 // Server-only: psk_identity_hint is the identity hint to send in
2555 // PSK-based key exchanges.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002556 char *psk_identity_hint;
2557
2558 unsigned int (*psk_client_callback)(SSL *ssl, const char *hint,
2559 char *identity,
2560 unsigned int max_identity_len,
2561 uint8_t *psk, unsigned int max_psk_len);
2562 unsigned int (*psk_server_callback)(SSL *ssl, const char *identity,
2563 uint8_t *psk, unsigned int max_psk_len);
2564
2565 SSL_CTX *ctx;
2566
Robert Sloana27a6a42017-09-05 08:39:28 -07002567 // extra application data
Robert Sloan47f43ed2017-02-06 14:55:15 -08002568 CRYPTO_EX_DATA ex_data;
2569
Robert Sloana27a6a42017-09-05 08:39:28 -07002570 // for server side, keep the list of CA_dn we can use
Robert Sloan7d422bc2017-03-06 10:04:29 -08002571 STACK_OF(CRYPTO_BUFFER) *client_CA;
2572
Robert Sloana27a6a42017-09-05 08:39:28 -07002573 // cached_x509_client_CA is a cache of parsed versions of the elements of
2574 // |client_CA|.
Robert Sloan7d422bc2017-03-06 10:04:29 -08002575 STACK_OF(X509_NAME) *cached_x509_client_CA;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002576
Robert Sloana27a6a42017-09-05 08:39:28 -07002577 uint32_t options; // protocol behaviour
2578 uint32_t mode; // API behaviour
Robert Sloan47f43ed2017-02-06 14:55:15 -08002579 uint32_t max_cert_list;
2580 char *tlsext_hostname;
2581 size_t supported_group_list_len;
Robert Sloana27a6a42017-09-05 08:39:28 -07002582 uint16_t *supported_group_list; // our list
Robert Sloan47f43ed2017-02-06 14:55:15 -08002583
Robert Sloana27a6a42017-09-05 08:39:28 -07002584 // session_ctx is the |SSL_CTX| used for the session cache and related
2585 // settings.
Robert Sloan1c9db532017-03-13 08:03:59 -07002586 SSL_CTX *session_ctx;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002587
Robert Sloana27a6a42017-09-05 08:39:28 -07002588 // srtp_profiles is the list of configured SRTP protection profiles for
2589 // DTLS-SRTP.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002590 STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
2591
Robert Sloana27a6a42017-09-05 08:39:28 -07002592 // srtp_profile is the selected SRTP protection profile for
2593 // DTLS-SRTP.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002594 const SRTP_PROTECTION_PROFILE *srtp_profile;
2595
Robert Sloana27a6a42017-09-05 08:39:28 -07002596 // The client's Channel ID private key.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002597 EVP_PKEY *tlsext_channel_id_private;
2598
Robert Sloana27a6a42017-09-05 08:39:28 -07002599 // For a client, this contains the list of supported protocols in wire
2600 // format.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002601 uint8_t *alpn_client_proto_list;
2602 unsigned alpn_client_proto_list_len;
2603
Robert Sloana27a6a42017-09-05 08:39:28 -07002604 // renegotiate_mode controls how peer renegotiation attempts are handled.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002605 enum ssl_renegotiate_mode_t renegotiate_mode;
2606
Robert Sloana27a6a42017-09-05 08:39:28 -07002607 // verify_mode is a bitmask of |SSL_VERIFY_*| values.
Robert Sloan47f43ed2017-02-06 14:55:15 -08002608 uint8_t verify_mode;
2609
Robert Sloana27a6a42017-09-05 08:39:28 -07002610 // server is true iff the this SSL* is the server half. Note: before the SSL*
2611 // is initialized by either SSL_set_accept_state or SSL_set_connect_state,
2612 // the side is not determined. In this state, server is always false.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002613 bool server:1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002614
Robert Sloana27a6a42017-09-05 08:39:28 -07002615 // quiet_shutdown is true if the connection should not send a close_notify on
2616 // shutdown.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002617 bool quiet_shutdown:1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002618
Robert Sloana27a6a42017-09-05 08:39:28 -07002619 // Enable signed certificate time stamps. Currently client only.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002620 bool signed_cert_timestamps_enabled:1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002621
Robert Sloana27a6a42017-09-05 08:39:28 -07002622 // ocsp_stapling_enabled is only used by client connections and indicates
2623 // whether OCSP stapling will be requested.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002624 bool ocsp_stapling_enabled:1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002625
Robert Sloana27a6a42017-09-05 08:39:28 -07002626 // tlsext_channel_id_enabled is copied from the |SSL_CTX|. For a server,
2627 // means that we'll accept Channel IDs from clients. For a client, means that
2628 // we'll advertise support.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002629 bool tlsext_channel_id_enabled:1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002630
Robert Sloana27a6a42017-09-05 08:39:28 -07002631 // retain_only_sha256_of_client_certs is true if we should compute the SHA256
2632 // hash of the peer's certificate and then discard it to save memory and
2633 // session space. Only effective on the server side.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002634 bool retain_only_sha256_of_client_certs:1;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002635
Robert Sloana27a6a42017-09-05 08:39:28 -07002636 // early_data_accepted is true if early data was accepted by the server.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002637 bool early_data_accepted:1;
Robert Sloan47f43ed2017-02-06 14:55:15 -08002638};
2639
Robert Sloana27a6a42017-09-05 08:39:28 -07002640// From draft-ietf-tls-tls13-18, used in determining PSK modes.
Steven Valdez909b19f2016-11-21 15:35:44 -05002641#define SSL_PSK_DHE_KE 0x1
David Benjaminc895d6b2016-08-11 13:26:41 -04002642
Robert Sloana27a6a42017-09-05 08:39:28 -07002643// From draft-ietf-tls-tls13-16, used in determining whether to respond with a
2644// KeyUpdate.
David Benjamin95add822016-10-19 01:09:12 -04002645#define SSL_KEY_UPDATE_NOT_REQUESTED 0
2646#define SSL_KEY_UPDATE_REQUESTED 1
2647
Robert Sloana27a6a42017-09-05 08:39:28 -07002648// kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early
2649// data that will be accepted. This value should be slightly below
2650// kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext.
Robert Sloan9254e682017-04-24 09:42:06 -07002651static const size_t kMaxEarlyDataAccepted = 14336;
2652
Robert Sloan5d625782017-02-13 09:55:39 -08002653CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002654CERT *ssl_cert_dup(CERT *cert);
Robert Sloanfe7cd212017-08-07 09:03:39 -07002655void ssl_cert_clear_certs(CERT *cert);
2656void ssl_cert_free(CERT *cert);
2657int ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer);
Robert Sloan5d625782017-02-13 09:55:39 -08002658int ssl_is_key_type_supported(int key_type);
Robert Sloana27a6a42017-09-05 08:39:28 -07002659// ssl_compare_public_and_private_key returns one if |pubkey| is the public
2660// counterpart to |privkey|. Otherwise it returns zero and pushes a helpful
2661// message on the error queue.
Robert Sloan4d1ac502017-02-06 08:36:14 -08002662int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
2663 const EVP_PKEY *privkey);
2664int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey);
David Benjamin1b249672016-12-06 18:25:50 -05002665int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server);
David Benjaminc895d6b2016-08-11 13:26:41 -04002666int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session);
Robert Sloan8f860b12017-08-28 07:37:06 -07002667int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00002668
Robert Sloana27a6a42017-09-05 08:39:28 -07002669// ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on
2670// error.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002671UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method);
Robert Sloan5d625782017-02-13 09:55:39 -08002672
Robert Sloana27a6a42017-09-05 08:39:28 -07002673// SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over
2674// the parsed data.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002675UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
2676 const SSL_X509_METHOD *x509_method,
2677 CRYPTO_BUFFER_POOL *pool);
Robert Sloan5d625782017-02-13 09:55:39 -08002678
Robert Sloana27a6a42017-09-05 08:39:28 -07002679// ssl_session_is_context_valid returns one if |session|'s session ID context
2680// matches the one set on |ssl| and zero otherwise.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002681int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session);
2682
Robert Sloana27a6a42017-09-05 08:39:28 -07002683// ssl_session_is_time_valid returns one if |session| is still valid and zero if
2684// it has expired.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002685int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session);
2686
Robert Sloana27a6a42017-09-05 08:39:28 -07002687// ssl_session_is_resumable returns one if |session| is resumable for |hs| and
2688// zero otherwise.
Robert Sloana94fe052017-02-21 08:49:28 -08002689int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
2690 const SSL_SESSION *session);
Steven Valdez909b19f2016-11-21 15:35:44 -05002691
Robert Sloanae1abf92017-10-05 12:50:08 -07002692// ssl_session_protocol_version returns the protocol version associated with
2693// |session|. Note that despite the name, this is not the same as
2694// |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name.
2695uint16_t ssl_session_protocol_version(const SSL_SESSION *session);
Robert Sloanf6200e72017-07-10 08:09:18 -07002696
Robert Sloanae1abf92017-10-05 12:50:08 -07002697// ssl_session_get_digest returns the digest used in |session|.
2698const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session);
Robert Sloan5d625782017-02-13 09:55:39 -08002699
David Benjamin7c0d06c2016-08-11 13:26:41 -04002700void ssl_set_session(SSL *ssl, SSL_SESSION *session);
2701
Robert Sloana27a6a42017-09-05 08:39:28 -07002702// ssl_get_prev_session looks up the previous session based on |client_hello|.
2703// On success, it sets |*out_session| to the session or nullptr if none was
2704// found. If the session could not be looked up synchronously, it returns
2705// |ssl_hs_pending_session| and should be called again. If a ticket could not be
2706// decrypted immediately it returns |ssl_hs_pending_ticket| and should also
2707// be called again. Otherwise, it returns |ssl_hs_error|.
2708enum ssl_hs_wait_t ssl_get_prev_session(SSL *ssl,
2709 UniquePtr<SSL_SESSION> *out_session,
2710 bool *out_tickets_supported,
2711 bool *out_renew_ticket,
2712 const SSL_CLIENT_HELLO *client_hello);
Kenny Rootb8494592015-09-25 02:29:14 +00002713
Robert Sloana27a6a42017-09-05 08:39:28 -07002714// The following flags determine which parts of the session are duplicated.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002715#define SSL_SESSION_DUP_AUTH_ONLY 0x0
2716#define SSL_SESSION_INCLUDE_TICKET 0x1
2717#define SSL_SESSION_INCLUDE_NONAUTH 0x2
2718#define SSL_SESSION_DUP_ALL \
2719 (SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH)
2720
Robert Sloana27a6a42017-09-05 08:39:28 -07002721// SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the
2722// fields in |session| or nullptr on error. The new session is non-resumable and
2723// must be explicitly marked resumable once it has been filled in.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002724OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session,
2725 int dup_flags);
David Benjaminc895d6b2016-08-11 13:26:41 -04002726
Robert Sloana27a6a42017-09-05 08:39:28 -07002727// ssl_session_rebase_time updates |session|'s start time to the current time,
2728// adjusting the timeout so the expiration time is unchanged.
Robert Sloan4d1ac502017-02-06 08:36:14 -08002729void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session);
2730
Robert Sloana27a6a42017-09-05 08:39:28 -07002731// ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews
2732// |session|'s timeout to |timeout| (measured from the current time). The
2733// renewal is clamped to the session's auth_timeout.
Robert Sloan7d422bc2017-03-06 10:04:29 -08002734void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
2735 uint32_t timeout);
Steven Valdez909b19f2016-11-21 15:35:44 -05002736
Adam Langleyd9e397b2015-01-22 14:27:53 -08002737void ssl_cipher_preference_list_free(
2738 struct ssl_cipher_preference_list_st *cipher_list);
Steven Valdez909b19f2016-11-21 15:35:44 -05002739
Robert Sloana27a6a42017-09-05 08:39:28 -07002740// ssl_get_cipher_preferences returns the cipher preference list for TLS 1.2 and
2741// below.
Steven Valdez909b19f2016-11-21 15:35:44 -05002742const struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(
2743 const SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002744
David Benjamin1b249672016-12-06 18:25:50 -05002745void ssl_update_cache(SSL_HANDSHAKE *hs, int mode);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002746
Robert Sloan921ef2c2017-10-17 09:02:20 -07002747int ssl_send_alert(SSL *ssl, int level, int desc);
Robert Sloan84377092017-08-14 09:33:19 -07002748bool ssl3_get_message(SSL *ssl, SSLMessage *out);
Robert Sloan36272962017-10-23 10:28:39 -07002749ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
2750 uint8_t *out_alert, Span<uint8_t> in);
Robert Sloan84377092017-08-14 09:33:19 -07002751void ssl3_next_message(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002752
Adam Langley4139edb2016-01-13 15:00:54 -08002753int ssl3_dispatch_alert(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07002754ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out,
2755 size_t *out_consumed, uint8_t *out_alert,
2756 Span<uint8_t> in);
2757ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2758 uint8_t *out_alert,
2759 Span<uint8_t> in);
Robert Sloana27a6a42017-09-05 08:39:28 -07002760int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
Robert Sloane56da3e2017-06-26 08:26:42 -07002761 int len);
Steven Valdez909b19f2016-11-21 15:35:44 -05002762
Robert Sloan36272962017-10-23 10:28:39 -07002763bool ssl3_new(SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08002764void ssl3_free(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002765
Robert Sloan36272962017-10-23 10:28:39 -07002766bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2767bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2768bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg);
2769bool ssl3_add_change_cipher_spec(SSL *ssl);
2770bool ssl3_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
Robert Sloan4d1ac502017-02-06 08:36:14 -08002771int ssl3_flush_flight(SSL *ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04002772
Robert Sloan36272962017-10-23 10:28:39 -07002773bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type);
2774bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg);
2775bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg);
2776bool dtls1_add_change_cipher_spec(SSL *ssl);
2777bool dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc);
Robert Sloan4d1ac502017-02-06 08:36:14 -08002778int dtls1_flush_flight(SSL *ssl);
David Benjamin6e899c72016-06-09 18:02:18 -04002779
Robert Sloana27a6a42017-09-05 08:39:28 -07002780// ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to
Robert Sloan36272962017-10-23 10:28:39 -07002781// the pending flight. It returns true on success and false on error.
2782bool ssl_add_message_cbb(SSL *ssl, CBB *cbb);
Steven Valdez909b19f2016-11-21 15:35:44 -05002783
Robert Sloan36272962017-10-23 10:28:39 -07002784// ssl_hash_message incorporates |msg| into the handshake hash. It returns true
2785// on success and false on allocation failure.
Robert Sloan84377092017-08-14 09:33:19 -07002786bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Steven Valdez909b19f2016-11-21 15:35:44 -05002787
Robert Sloan36272962017-10-23 10:28:39 -07002788ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
2789 size_t *out_consumed, uint8_t *out_alert,
2790 Span<uint8_t> in);
2791ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
2792 uint8_t *out_alert,
2793 Span<uint8_t> in);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002794
Robert Sloana27a6a42017-09-05 08:39:28 -07002795int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
2796 const uint8_t *buf, int len);
David Benjamin6e899c72016-06-09 18:02:18 -04002797
Robert Sloana27a6a42017-09-05 08:39:28 -07002798// dtls1_write_record sends a record. It returns one on success and <= 0 on
2799// error.
David Benjamin6e899c72016-06-09 18:02:18 -04002800int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len,
2801 enum dtls1_use_epoch_t use_epoch);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002802
David Benjaminc895d6b2016-08-11 13:26:41 -04002803int dtls1_retransmit_outgoing_messages(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07002804bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
David Benjamin6e899c72016-06-09 18:02:18 -04002805 CBS *out_body);
Robert Sloan36272962017-10-23 10:28:39 -07002806bool dtls1_check_timeout_num(SSL *ssl);
Adam Langleye9ada862015-05-11 17:20:37 -07002807
Adam Langley4139edb2016-01-13 15:00:54 -08002808void dtls1_start_timer(SSL *ssl);
2809void dtls1_stop_timer(SSL *ssl);
Robert Sloan36272962017-10-23 10:28:39 -07002810bool dtls1_is_timer_expired(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002811unsigned int dtls1_min_mtu(void);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002812
Robert Sloan36272962017-10-23 10:28:39 -07002813bool dtls1_new(SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08002814void dtls1_free(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002815
Robert Sloan84377092017-08-14 09:33:19 -07002816bool dtls1_get_message(SSL *ssl, SSLMessage *out);
Robert Sloan36272962017-10-23 10:28:39 -07002817ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
2818 uint8_t *out_alert, Span<uint8_t> in);
Robert Sloan84377092017-08-14 09:33:19 -07002819void dtls1_next_message(SSL *ssl);
Adam Langley4139edb2016-01-13 15:00:54 -08002820int dtls1_dispatch_alert(SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002821
Robert Sloan2e9e66a2017-09-25 09:08:29 -07002822int tls1_change_cipher_state(SSL_HANDSHAKE *hs, evp_aead_direction_t direction);
Robert Sloan5d625782017-02-13 09:55:39 -08002823int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
Robert Sloanb1b54b82017-11-06 13:50:02 -08002824 Span<const uint8_t> premaster);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002825
Robert Sloan4562e9d2017-10-02 10:26:51 -07002826// tls1_get_grouplist returns the locally-configured group preference list.
2827Span<const uint16_t> tls1_get_grouplist(const SSL *ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002828
Robert Sloana27a6a42017-09-05 08:39:28 -07002829// tls1_check_group_id returns one if |group_id| is consistent with
2830// locally-configured group preferences.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002831int tls1_check_group_id(const SSL *ssl, uint16_t group_id);
Kenny Rootb8494592015-09-25 02:29:14 +00002832
Robert Sloana27a6a42017-09-05 08:39:28 -07002833// tls1_get_shared_group sets |*out_group_id| to the first preferred shared
2834// group between client and server preferences and returns one. If none may be
2835// found, it returns zero.
David Benjamin1b249672016-12-06 18:25:50 -05002836int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002837
Robert Sloana27a6a42017-09-05 08:39:28 -07002838// tls1_set_curves converts the array of |ncurves| NIDs pointed to by |curves|
2839// into a newly allocated array of TLS group IDs. On success, the function
2840// returns one and writes the array to |*out_group_ids| and its size to
2841// |*out_group_ids_len|. Otherwise, it returns zero.
David Benjamind316cba2016-06-02 16:17:39 -04002842int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyd9e397b2015-01-22 14:27:53 -08002843 const int *curves, size_t ncurves);
2844
Robert Sloana27a6a42017-09-05 08:39:28 -07002845// tls1_set_curves_list converts the string of curves pointed to by |curves|
2846// into a newly allocated array of TLS group IDs. On success, the function
2847// returns one and writes the array to |*out_group_ids| and its size to
2848// |*out_group_ids_len|. Otherwise, it returns zero.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002849int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
2850 const char *curves);
2851
Robert Sloana27a6a42017-09-05 08:39:28 -07002852// ssl_add_clienthello_tlsext writes ClientHello extensions to |out|. It
2853// returns one on success and zero on failure. The |header_len| argument is the
2854// length of the ClientHello written so far and is used to compute the padding
2855// length. (It does not include the record header.)
David Benjamin1b249672016-12-06 18:25:50 -05002856int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len);
Kenny Roote99801b2015-11-06 15:31:15 -08002857
David Benjamin1b249672016-12-06 18:25:50 -05002858int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out);
2859int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2860 const SSL_CLIENT_HELLO *client_hello);
2861int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002862
2863#define tlsext_tick_md EVP_sha256
Adam Langleyd9e397b2015-01-22 14:27:53 -08002864
Robert Sloana27a6a42017-09-05 08:39:28 -07002865// ssl_process_ticket processes a session ticket from the client. It returns
2866// one of:
2867// |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and
2868// |*out_renew_ticket| is set to whether the ticket should be renewed.
2869// |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a
2870// fresh ticket should be sent, but the given ticket cannot be used.
2871// |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted.
2872// Retry later.
2873// |ssl_ticket_aead_error|: an error occured that is fatal to the connection.
Robert Sloan1c9db532017-03-13 08:03:59 -07002874enum ssl_ticket_aead_result_t ssl_process_ticket(
Robert Sloana27a6a42017-09-05 08:39:28 -07002875 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Robert Sloan1c9db532017-03-13 08:03:59 -07002876 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
2877 size_t session_id_len);
Kenny Rootb8494592015-09-25 02:29:14 +00002878
Robert Sloana27a6a42017-09-05 08:39:28 -07002879// tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies
2880// the signature. If the key is valid, it saves the Channel ID and returns
2881// one. Otherwise, it returns zero.
Robert Sloan84377092017-08-14 09:33:19 -07002882int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg);
Steven Valdez909b19f2016-11-21 15:35:44 -05002883
Robert Sloana27a6a42017-09-05 08:39:28 -07002884// tls1_write_channel_id generates a Channel ID message and puts the output in
2885// |cbb|. |ssl->tlsext_channel_id_private| must already be set before calling.
Robert Sloanb1b54b82017-11-06 13:50:02 -08002886// This function returns true on success and false on error.
2887bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb);
Steven Valdez909b19f2016-11-21 15:35:44 -05002888
Robert Sloana27a6a42017-09-05 08:39:28 -07002889// tls1_channel_id_hash computes the hash to be signed by Channel ID and writes
2890// it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns
2891// one on success and zero on failure.
Robert Sloan5d625782017-02-13 09:55:39 -08002892int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002893
Robert Sloan5d625782017-02-13 09:55:39 -08002894int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs);
Kenny Roota04d78d2015-09-25 00:26:37 +00002895
Robert Sloana27a6a42017-09-05 08:39:28 -07002896// ssl_do_channel_id_callback checks runs |ssl->ctx->channel_id_cb| if
2897// necessary. It returns one on success and zero on fatal error. Note that, on
2898// success, |ssl->tlsext_channel_id_private| may be unset, in which case the
2899// operation should be retried later.
Steven Valdez909b19f2016-11-21 15:35:44 -05002900int ssl_do_channel_id_callback(SSL *ssl);
2901
Robert Sloana27a6a42017-09-05 08:39:28 -07002902// ssl_can_write returns one if |ssl| is allowed to write and zero otherwise.
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002903int ssl_can_write(const SSL *ssl);
2904
Robert Sloana27a6a42017-09-05 08:39:28 -07002905// ssl_can_read returns one if |ssl| is allowed to read and zero otherwise.
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002906int ssl_can_read(const SSL *ssl);
2907
Robert Sloan7d422bc2017-03-06 10:04:29 -08002908void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
Robert Sloan8f860b12017-08-28 07:37:06 -07002909void ssl_ctx_get_current_time(const SSL_CTX *ctx,
2910 struct OPENSSL_timeval *out_clock);
David Benjaminc895d6b2016-08-11 13:26:41 -04002911
Robert Sloana27a6a42017-09-05 08:39:28 -07002912// ssl_reset_error_state resets state for |SSL_get_error|.
Robert Sloan69939df2017-01-09 10:53:07 -08002913void ssl_reset_error_state(SSL *ssl);
2914
Robert Sloan36272962017-10-23 10:28:39 -07002915// ssl_set_read_error sets |ssl|'s read half into an error state, saving the
2916// current state of the error queue.
2917void ssl_set_read_error(SSL* ssl);
2918
Robert Sloanb6d070c2017-07-24 08:40:01 -07002919} // namespace bssl
2920
2921
Robert Sloana27a6a42017-09-05 08:39:28 -07002922// Opaque C types.
2923//
2924// The following types are exported to C code as public typedefs, so they must
2925// be defined outside of the namespace.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002926
Robert Sloana27a6a42017-09-05 08:39:28 -07002927// ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility
2928// structure to support the legacy version-locked methods.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002929struct ssl_method_st {
Robert Sloana27a6a42017-09-05 08:39:28 -07002930 // version, if non-zero, is the only protocol version acceptable to an
2931 // SSL_CTX initialized from this method.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002932 uint16_t version;
Robert Sloana27a6a42017-09-05 08:39:28 -07002933 // method is the underlying SSL_PROTOCOL_METHOD that initializes the
2934 // SSL_CTX.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002935 const bssl::SSL_PROTOCOL_METHOD *method;
Robert Sloana27a6a42017-09-05 08:39:28 -07002936 // x509_method contains pointers to functions that might deal with |X509|
2937 // compatibility, or might be a no-op, depending on the application.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002938 const SSL_X509_METHOD *x509_method;
2939};
2940
Robert Sloanb6d070c2017-07-24 08:40:01 -07002941struct ssl_x509_method_st {
Robert Sloana27a6a42017-09-05 08:39:28 -07002942 // check_client_CA_list returns one if |names| is a good list of X.509
2943 // distinguished names and zero otherwise. This is used to ensure that we can
2944 // reject unparsable values at handshake time when using crypto/x509.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002945 int (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names);
2946
Robert Sloana27a6a42017-09-05 08:39:28 -07002947 // cert_clear frees and NULLs all X509 certificate-related state.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002948 void (*cert_clear)(bssl::CERT *cert);
Robert Sloana27a6a42017-09-05 08:39:28 -07002949 // cert_free frees all X509-related state.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002950 void (*cert_free)(bssl::CERT *cert);
Robert Sloana27a6a42017-09-05 08:39:28 -07002951 // cert_flush_cached_chain drops any cached |X509|-based certificate chain
2952 // from |cert|.
2953 // cert_dup duplicates any needed fields from |cert| to |new_cert|.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002954 void (*cert_dup)(bssl::CERT *new_cert, const bssl::CERT *cert);
2955 void (*cert_flush_cached_chain)(bssl::CERT *cert);
Robert Sloana27a6a42017-09-05 08:39:28 -07002956 // cert_flush_cached_chain drops any cached |X509|-based leaf certificate
2957 // from |cert|.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002958 void (*cert_flush_cached_leaf)(bssl::CERT *cert);
Robert Sloanb6d070c2017-07-24 08:40:01 -07002959
Robert Sloana27a6a42017-09-05 08:39:28 -07002960 // session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain|
2961 // from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns
2962 // one on success or zero on error.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002963 int (*session_cache_objects)(SSL_SESSION *session);
Robert Sloana27a6a42017-09-05 08:39:28 -07002964 // session_dup duplicates any needed fields from |session| to |new_session|.
2965 // It returns one on success or zero on error.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002966 int (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session);
Robert Sloana27a6a42017-09-05 08:39:28 -07002967 // session_clear frees any X509-related state from |session|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002968 void (*session_clear)(SSL_SESSION *session);
Robert Sloana27a6a42017-09-05 08:39:28 -07002969 // session_verify_cert_chain verifies the certificate chain in |session|,
2970 // sets |session->verify_result| and returns one on success or zero on
2971 // error.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002972 int (*session_verify_cert_chain)(SSL_SESSION *session, SSL *ssl,
2973 uint8_t *out_alert);
2974
Robert Sloana27a6a42017-09-05 08:39:28 -07002975 // hs_flush_cached_ca_names drops any cached |X509_NAME|s from |hs|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002976 void (*hs_flush_cached_ca_names)(bssl::SSL_HANDSHAKE *hs);
Robert Sloana27a6a42017-09-05 08:39:28 -07002977 // ssl_new does any neccessary initialisation of |ssl|. It returns one on
2978 // success or zero on error.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002979 int (*ssl_new)(SSL *ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07002980 // ssl_free frees anything created by |ssl_new|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002981 void (*ssl_free)(SSL *ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07002982 // ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002983 void (*ssl_flush_cached_client_CA)(SSL *ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07002984 // ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if
2985 // necessary. On success, it updates |ssl|'s certificate configuration as
2986 // needed and returns one. Otherwise, it returns zero.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002987 int (*ssl_auto_chain_if_needed)(SSL *ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07002988 // ssl_ctx_new does any neccessary initialisation of |ctx|. It returns one on
2989 // success or zero on error.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002990 int (*ssl_ctx_new)(SSL_CTX *ctx);
Robert Sloana27a6a42017-09-05 08:39:28 -07002991 // ssl_ctx_free frees anything created by |ssl_ctx_new|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002992 void (*ssl_ctx_free)(SSL_CTX *ctx);
Robert Sloana27a6a42017-09-05 08:39:28 -07002993 // ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|.
Robert Sloanb6d070c2017-07-24 08:40:01 -07002994 void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl);
2995};
2996
Robert Sloan921ef2c2017-10-17 09:02:20 -07002997// The following types back public C-exposed types which must live in the global
2998// namespace. We use subclassing so the implementations may be C++ types with
2999// methods and destructor without polluting the global namespace.
3000struct ssl_ctx_st : public bssl::SSLContext {};
Robert Sloanb6d070c2017-07-24 08:40:01 -07003001struct ssl_st : public bssl::SSLConnection {};
3002
Robert Sloanb6d070c2017-07-24 08:40:01 -07003003
Robert Sloana27a6a42017-09-05 08:39:28 -07003004#endif // OPENSSL_HEADER_SSL_INTERNAL_H