xref: /openbsd/lib/libssl/ssl_local.h (revision ae0a2582)
1 /* $OpenBSD: ssl_local.h,v 1.23 2024/07/23 14:40:54 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 /* ====================================================================
112  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113  * ECC cipher suite support in OpenSSL originally developed by
114  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115  */
116 /* ====================================================================
117  * Copyright 2005 Nokia. All rights reserved.
118  *
119  * The portions of the attached software ("Contribution") is developed by
120  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121  * license.
122  *
123  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125  * support (see RFC 4279) to OpenSSL.
126  *
127  * No patent licenses or other rights except those expressly stated in
128  * the OpenSSL open source license shall be deemed granted or received
129  * expressly, by implication, estoppel, or otherwise.
130  *
131  * No assurances are provided by Nokia that the Contribution does not
132  * infringe the patent or other intellectual property rights of any third
133  * party or that the license provides you with all the necessary rights
134  * to make use of the Contribution.
135  *
136  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140  * OTHERWISE.
141  */
142 
143 #ifndef HEADER_SSL_LOCL_H
144 #define HEADER_SSL_LOCL_H
145 
146 #include <sys/types.h>
147 
148 #include <errno.h>
149 #include <stdlib.h>
150 #include <string.h>
151 #include <time.h>
152 #include <unistd.h>
153 
154 #include <openssl/opensslconf.h>
155 
156 #include <openssl/bio.h>
157 #include <openssl/buffer.h>
158 #include <openssl/dsa.h>
159 #include <openssl/err.h>
160 #include <openssl/rsa.h>
161 #include <openssl/ssl.h>
162 #include <openssl/stack.h>
163 
164 #include "bytestring.h"
165 #include "tls_content.h"
166 #include "tls13_internal.h"
167 
168 __BEGIN_HIDDEN_DECLS
169 
170 #ifndef CTASSERT
171 #define CTASSERT(x)	extern char  _ctassert[(x) ? 1 : -1 ]   \
172 			    __attribute__((__unused__))
173 #endif
174 
175 #ifndef LIBRESSL_HAS_DTLS1_2
176 #define LIBRESSL_HAS_DTLS1_2
177 #endif
178 
179 /* LOCAL STUFF */
180 
181 #define SSL_DECRYPT	0
182 #define SSL_ENCRYPT	1
183 
184 /*
185  * Define the Bitmasks for SSL_CIPHER.algorithms.
186  * This bits are used packed as dense as possible. If new methods/ciphers
187  * etc will be added, the bits a likely to change, so this information
188  * is for internal library use only, even though SSL_CIPHER.algorithms
189  * can be publicly accessed.
190  * Use the according functions for cipher management instead.
191  *
192  * The bit mask handling in the selection and sorting scheme in
193  * ssl_create_cipher_list() has only limited capabilities, reflecting
194  * that the different entities within are mutually exclusive:
195  * ONLY ONE BIT PER MASK CAN BE SET AT A TIME.
196  */
197 
198 /* Bits for algorithm_mkey (key exchange algorithm) */
199 #define SSL_kRSA		0x00000001L /* RSA key exchange */
200 #define SSL_kDHE		0x00000008L /* tmp DH key no DH cert */
201 #define SSL_kECDHE		0x00000080L /* ephemeral ECDH */
202 #define SSL_kTLS1_3		0x00000400L /* TLSv1.3 key exchange */
203 
204 /* Bits for algorithm_auth (server authentication) */
205 #define SSL_aRSA		0x00000001L /* RSA auth */
206 #define SSL_aDSS		0x00000002L /* DSS auth */
207 #define SSL_aNULL		0x00000004L /* no auth (i.e. use ADH or AECDH) */
208 #define SSL_aECDSA              0x00000040L /* ECDSA auth*/
209 #define SSL_aTLS1_3		0x00000400L /* TLSv1.3 authentication */
210 
211 /* Bits for algorithm_enc (symmetric encryption) */
212 #define SSL_DES			0x00000001L
213 #define SSL_3DES		0x00000002L
214 #define SSL_RC4			0x00000004L
215 #define SSL_IDEA		0x00000008L
216 #define SSL_eNULL		0x00000010L
217 #define SSL_AES128		0x00000020L
218 #define SSL_AES256		0x00000040L
219 #define SSL_CAMELLIA128		0x00000080L
220 #define SSL_CAMELLIA256		0x00000100L
221 #define SSL_AES128GCM		0x00000400L
222 #define SSL_AES256GCM		0x00000800L
223 #define SSL_CHACHA20POLY1305	0x00001000L
224 
225 #define SSL_AES			(SSL_AES128|SSL_AES256|SSL_AES128GCM|SSL_AES256GCM)
226 #define SSL_CAMELLIA		(SSL_CAMELLIA128|SSL_CAMELLIA256)
227 
228 
229 /* Bits for algorithm_mac (symmetric authentication) */
230 
231 #define SSL_MD5			0x00000001L
232 #define SSL_SHA1		0x00000002L
233 #define SSL_SHA256		0x00000010L
234 #define SSL_SHA384		0x00000020L
235 /* Not a real MAC, just an indication it is part of cipher */
236 #define SSL_AEAD		0x00000040L
237 #define SSL_STREEBOG256		0x00000080L
238 
239 /* Bits for algorithm_ssl (protocol version) */
240 #define SSL_SSLV3		0x00000002L
241 #define SSL_TLSV1		SSL_SSLV3	/* for now */
242 #define SSL_TLSV1_2		0x00000004L
243 #define SSL_TLSV1_3		0x00000008L
244 
245 
246 /* Bits for algorithm2 (handshake digests and other extra flags) */
247 
248 #define SSL_HANDSHAKE_MAC_MASK		0xff0
249 #define SSL_HANDSHAKE_MAC_SHA256	0x080
250 #define SSL_HANDSHAKE_MAC_SHA384	0x100
251 
252 #define SSL3_CK_ID		0x03000000
253 #define SSL3_CK_VALUE_MASK	0x0000ffff
254 
255 /*
256  * Cipher strength information.
257  */
258 #define SSL_STRONG_MASK		0x000001fcL
259 #define SSL_STRONG_NONE		0x00000004L
260 #define SSL_LOW			0x00000020L
261 #define SSL_MEDIUM		0x00000040L
262 #define SSL_HIGH		0x00000080L
263 
264 /*
265  * The keylength (measured in RSA key bits, I guess)  for temporary keys.
266  * Cipher argument is so that this can be variable in the future.
267  */
268 #define SSL_C_PKEYLENGTH(c)	1024
269 
270 /* See if we use signature algorithms extension. */
271 #define SSL_USE_SIGALGS(s) \
272 	(s->method->enc_flags & SSL_ENC_FLAG_SIGALGS)
273 
274 /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2. */
275 #define SSL_USE_TLS1_2_CIPHERS(s) \
276 	(s->method->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)
277 
278 /* Allow TLS 1.3 ciphersuites only. */
279 #define SSL_USE_TLS1_3_CIPHERS(s) \
280 	(s->method->enc_flags & SSL_ENC_FLAG_TLS1_3_CIPHERS)
281 
282 #define SSL_PKEY_RSA		0
283 #define SSL_PKEY_ECC		1
284 #define SSL_PKEY_NUM		2
285 
286 #define SSL_MAX_EMPTY_RECORDS	32
287 
288 /* SSL_kRSA <- RSA_ENC | (RSA_TMP & RSA_SIGN) |
289  *	    <- (EXPORT & (RSA_ENC | RSA_TMP) & RSA_SIGN)
290  * SSL_kDH  <- DH_ENC & (RSA_ENC | RSA_SIGN | DSA_SIGN)
291  * SSL_kDHE <- RSA_ENC | RSA_SIGN | DSA_SIGN
292  * SSL_aRSA <- RSA_ENC | RSA_SIGN
293  * SSL_aDSS <- DSA_SIGN
294  */
295 
296 /* From ECC-TLS draft, used in encoding the curve type in
297  * ECParameters
298  */
299 #define EXPLICIT_PRIME_CURVE_TYPE  1
300 #define EXPLICIT_CHAR2_CURVE_TYPE  2
301 #define NAMED_CURVE_TYPE           3
302 
303 typedef struct ssl_cert_pkey_st {
304 	X509 *x509;
305 	EVP_PKEY *privatekey;
306 	STACK_OF(X509) *chain;
307 } SSL_CERT_PKEY;
308 
309 typedef struct ssl_cert_st {
310 	/* Current active set */
311 	/* ALWAYS points to an element of the pkeys array
312 	 * Probably it would make more sense to store
313 	 * an index, not a pointer. */
314 	SSL_CERT_PKEY *key;
315 
316 	SSL_CERT_PKEY pkeys[SSL_PKEY_NUM];
317 
318 	/* The following masks are for the key and auth
319 	 * algorithms that are supported by the certs below */
320 	int valid;
321 	unsigned long mask_k;
322 	unsigned long mask_a;
323 
324 	DH *dhe_params;
325 	DH *(*dhe_params_cb)(SSL *ssl, int is_export, int keysize);
326 	int dhe_params_auto;
327 
328 	int (*security_cb)(const SSL *s, const SSL_CTX *ctx, int op, int bits,
329 	    int nid, void *other, void *ex_data); /* Not exposed in API. */
330 	int security_level;
331 	void *security_ex_data; /* Not exposed in API. */
332 
333 	int references; /* >1 only if SSL_copy_session_id is used */
334 } SSL_CERT;
335 
336 struct ssl_comp_st {
337 	int id;
338 	const char *name;
339 };
340 
341 struct ssl_cipher_st {
342 	uint16_t value;			/* Cipher suite value. */
343 
344 	const char *name;		/* text name */
345 
346 	unsigned long algorithm_mkey;	/* key exchange algorithm */
347 	unsigned long algorithm_auth;	/* server authentication */
348 	unsigned long algorithm_enc;	/* symmetric encryption */
349 	unsigned long algorithm_mac;	/* symmetric authentication */
350 	unsigned long algorithm_ssl;	/* (major) protocol version */
351 
352 	unsigned long algo_strength;	/* strength and export flags */
353 	unsigned long algorithm2;	/* Extra flags */
354 	int strength_bits;		/* Number of bits really used */
355 	int alg_bits;			/* Number of bits for algorithm */
356 };
357 
358 struct ssl_method_st {
359 	int dtls;
360 	int server;
361 	int version;
362 
363 	uint16_t min_tls_version;
364 	uint16_t max_tls_version;
365 
366 	int (*ssl_new)(SSL *s);
367 	void (*ssl_clear)(SSL *s);
368 	void (*ssl_free)(SSL *s);
369 
370 	int (*ssl_accept)(SSL *s);
371 	int (*ssl_connect)(SSL *s);
372 	int (*ssl_shutdown)(SSL *s);
373 
374 	int (*ssl_renegotiate)(SSL *s);
375 	int (*ssl_renegotiate_check)(SSL *s);
376 
377 	int (*ssl_pending)(const SSL *s);
378 	int (*ssl_read_bytes)(SSL *s, int type, unsigned char *buf, int len,
379 	    int peek);
380 	int (*ssl_write_bytes)(SSL *s, int type, const void *buf_, int len);
381 
382 	unsigned int enc_flags;		/* SSL_ENC_FLAG_* */
383 };
384 
385 /*
386  * Let's make this into an ASN.1 type structure as follows
387  * SSL_SESSION_ID ::= SEQUENCE {
388  *	version			INTEGER,	-- structure version number
389  *	SSLversion		INTEGER,	-- SSL version number
390  *	Cipher			OCTET STRING,	-- the 2 byte cipher ID
391  *	Session_ID		OCTET STRING,	-- the Session ID
392  *	Master_key		OCTET STRING,	-- the master key
393  *	KRB5_principal		OCTET STRING	-- optional Kerberos principal
394  *	Time [ 1 ] EXPLICIT	INTEGER,	-- optional Start Time
395  *	Timeout [ 2 ] EXPLICIT	INTEGER,	-- optional Timeout ins seconds
396  *	Peer [ 3 ] EXPLICIT	X509,		-- optional Peer Certificate
397  *	Session_ID_context [ 4 ] EXPLICIT OCTET STRING,   -- the Session ID context
398  *	Verify_result [ 5 ] EXPLICIT INTEGER,   -- X509_V_... code for `Peer'
399  *	HostName [ 6 ] EXPLICIT OCTET STRING,   -- optional HostName from servername TLS extension
400  *	PSK_identity_hint [ 7 ] EXPLICIT OCTET STRING, -- optional PSK identity hint
401  *	PSK_identity [ 8 ] EXPLICIT OCTET STRING,  -- optional PSK identity
402  *	Ticket_lifetime_hint [9] EXPLICIT INTEGER, -- server's lifetime hint for session ticket
403  *	Ticket [10]             EXPLICIT OCTET STRING, -- session ticket (clients only)
404  *	Compression_meth [11]   EXPLICIT OCTET STRING, -- optional compression method
405  *	SRP_username [ 12 ] EXPLICIT OCTET STRING -- optional SRP username
406  * }
407  * Look in ssl/ssl_asn1.c for more details
408  * I'm using EXPLICIT tags so I can read the damn things using asn1parse :-).
409  */
410 struct ssl_session_st {
411 	int ssl_version;	/* what ssl version session info is
412 				 * being kept in here? */
413 
414 	size_t master_key_length;
415 	unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH];
416 
417 	/* session_id - valid? */
418 	size_t session_id_length;
419 	unsigned char session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
420 
421 	/* this is used to determine whether the session is being reused in
422 	 * the appropriate context. It is up to the application to set this,
423 	 * via SSL_new */
424 	size_t sid_ctx_length;
425 	unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH];
426 
427 	/* Peer provided leaf (end-entity) certificate. */
428 	X509 *peer_cert;
429 	int peer_cert_type;
430 
431 	/* when app_verify_callback accepts a session where the peer's certificate
432 	 * is not ok, we must remember the error for session reuse: */
433 	long verify_result; /* only for servers */
434 
435 	long timeout;
436 	time_t time;
437 	int references;
438 
439 	uint16_t cipher_value;
440 
441 	char *tlsext_hostname;
442 
443 	/* Session resumption - RFC 5077 and RFC 8446. */
444 	unsigned char *tlsext_tick;		/* Session ticket */
445 	size_t tlsext_ticklen;			/* Session ticket length */
446 	uint32_t tlsext_tick_lifetime_hint;	/* Session lifetime hint in seconds */
447 	uint32_t tlsext_tick_age_add; /* TLSv1.3 ticket age obfuscation (in ms) */
448 	struct tls13_secret resumption_master_secret;
449 
450 	CRYPTO_EX_DATA ex_data; /* application specific data */
451 
452 	/* These are used to make removal of session-ids more
453 	 * efficient and to implement a maximum cache size. */
454 	struct ssl_session_st *prev, *next;
455 
456 	/* Used to indicate that session resumption is not allowed.
457 	 * Applications can also set this bit for a new session via
458 	 * not_resumable_session_cb to disable session caching and tickets. */
459 	int not_resumable;
460 
461 	size_t tlsext_ecpointformatlist_length;
462 	uint8_t *tlsext_ecpointformatlist; /* peer's list */
463 	size_t tlsext_supportedgroups_length;
464 	uint16_t *tlsext_supportedgroups; /* peer's list */
465 };
466 
467 struct ssl_sigalg;
468 
469 typedef struct ssl_handshake_tls12_st {
470 	/* Used when SSL_ST_FLUSH_DATA is entered. */
471 	int next_state;
472 
473 	/* Handshake message type and size. */
474 	int message_type;
475 	unsigned long message_size;
476 
477 	/* Reuse current handshake message. */
478 	int reuse_message;
479 
480 	/* Client certificate requests. */
481 	int cert_request;
482 	STACK_OF(X509_NAME) *ca_names;
483 
484 	/* Record-layer key block for TLS 1.2 and earlier. */
485 	struct tls12_key_block *key_block;
486 
487 	/* Transcript hash prior to sending certificate verify message. */
488 	uint8_t cert_verify[EVP_MAX_MD_SIZE];
489 } SSL_HANDSHAKE_TLS12;
490 
491 typedef struct ssl_handshake_tls13_st {
492 	int use_legacy;
493 	int hrr;
494 
495 	/* Client indicates psk_dhe_ke support in PskKeyExchangeMode. */
496 	int use_psk_dhe_ke;
497 
498 	/* Certificate selected for use (static pointer). */
499 	const SSL_CERT_PKEY *cpk;
500 
501 	/* Version proposed by peer server. */
502 	uint16_t server_version;
503 
504 	uint16_t server_group;
505 	struct tls13_secrets *secrets;
506 
507 	uint8_t *cookie;
508 	size_t cookie_len;
509 
510 	/* Preserved transcript hash. */
511 	uint8_t transcript_hash[EVP_MAX_MD_SIZE];
512 	size_t transcript_hash_len;
513 
514 	/* Legacy session ID. */
515 	uint8_t legacy_session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
516 	size_t legacy_session_id_len;
517 
518 	/* ClientHello hash, used to validate following HelloRetryRequest */
519 	EVP_MD_CTX *clienthello_md_ctx;
520 	unsigned char *clienthello_hash;
521 	unsigned int clienthello_hash_len;
522 
523 	/* QUIC read buffer and read/write encryption levels. */
524 	struct tls_buffer *quic_read_buffer;
525 	enum ssl_encryption_level_t quic_read_level;
526 	enum ssl_encryption_level_t quic_write_level;
527 } SSL_HANDSHAKE_TLS13;
528 
529 typedef struct ssl_handshake_st {
530 	/*
531 	 * Minimum and maximum versions supported for this handshake. These are
532 	 * initialised at the start of a handshake based on the method in use
533 	 * and the current protocol version configuration.
534 	 */
535 	uint16_t our_min_tls_version;
536 	uint16_t our_max_tls_version;
537 
538 	/*
539 	 * Version negotiated for this session. For a client this is set once
540 	 * the server selected version is parsed from the ServerHello (either
541 	 * from the legacy version or supported versions extension). For a
542 	 * server this is set once we select the version we will use with the
543 	 * client.
544 	 */
545 	uint16_t negotiated_tls_version;
546 
547 	/*
548 	 * Legacy version advertised by our peer. For a server this is the
549 	 * version specified by the client in the ClientHello message. For a
550 	 * client, this is the version provided in the ServerHello message.
551 	 */
552 	uint16_t peer_legacy_version;
553 
554 	/*
555 	 * Current handshake state - contains one of the SSL3_ST_* values and
556 	 * is used by the TLSv1.2 state machine, as well as being updated by
557 	 * the TLSv1.3 stack due to it being exposed externally.
558 	 */
559 	int state;
560 
561 	/* Cipher being negotiated in this handshake. */
562 	const SSL_CIPHER *cipher;
563 
564 	/* Ciphers sent by the client. */
565 	STACK_OF(SSL_CIPHER) *client_ciphers;
566 
567 	/* Extensions seen in this handshake. */
568 	uint32_t extensions_seen;
569 
570 	/* Extensions processed in this handshake. */
571 	uint32_t extensions_processed;
572 
573 	/* Signature algorithms selected for use (static pointers). */
574 	const struct ssl_sigalg *our_sigalg;
575 	const struct ssl_sigalg *peer_sigalg;
576 
577 	/* sigalgs offered in this handshake in wire form */
578 	uint8_t *sigalgs;
579 	size_t sigalgs_len;
580 
581 	/* Key share for ephemeral key exchange. */
582 	struct tls_key_share *key_share;
583 
584 	/*
585 	 * Copies of the verify data sent in our finished message and the
586 	 * verify data received in the finished message sent by our peer.
587 	 */
588 	uint8_t finished[EVP_MAX_MD_SIZE];
589 	size_t finished_len;
590 	uint8_t peer_finished[EVP_MAX_MD_SIZE];
591 	size_t peer_finished_len;
592 
593 	/* List of certificates received from our peer. */
594 	STACK_OF(X509) *peer_certs;
595 	STACK_OF(X509) *peer_certs_no_leaf;
596 
597 	/* Certificate chain resulting from X.509 verification. */
598 	STACK_OF(X509) *verified_chain;
599 
600 	SSL_HANDSHAKE_TLS12 tls12;
601 	SSL_HANDSHAKE_TLS13 tls13;
602 } SSL_HANDSHAKE;
603 
604 typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
605 
606 /* TLS Session Ticket extension struct. */
607 struct tls_session_ticket_ext_st {
608 	unsigned short length;
609 	void *data;
610 };
611 
612 struct tls12_key_block;
613 
614 struct tls12_key_block *tls12_key_block_new(void);
615 void tls12_key_block_free(struct tls12_key_block *kb);
616 void tls12_key_block_client_write(struct tls12_key_block *kb, CBS *mac_key,
617     CBS *key, CBS *iv);
618 void tls12_key_block_server_write(struct tls12_key_block *kb, CBS *mac_key,
619     CBS *key, CBS *iv);
620 int tls12_key_block_generate(struct tls12_key_block *kb, SSL *s,
621     const EVP_AEAD *aead, const EVP_CIPHER *cipher, const EVP_MD *mac_hash);
622 
623 struct tls12_record_layer;
624 
625 struct tls12_record_layer *tls12_record_layer_new(void);
626 void tls12_record_layer_free(struct tls12_record_layer *rl);
627 void tls12_record_layer_alert(struct tls12_record_layer *rl,
628     uint8_t *alert_desc);
629 int tls12_record_layer_write_overhead(struct tls12_record_layer *rl,
630     size_t *overhead);
631 int tls12_record_layer_read_protected(struct tls12_record_layer *rl);
632 int tls12_record_layer_write_protected(struct tls12_record_layer *rl);
633 void tls12_record_layer_set_aead(struct tls12_record_layer *rl,
634     const EVP_AEAD *aead);
635 void tls12_record_layer_set_cipher_hash(struct tls12_record_layer *rl,
636     const EVP_CIPHER *cipher, const EVP_MD *handshake_hash,
637     const EVP_MD *mac_hash);
638 void tls12_record_layer_set_version(struct tls12_record_layer *rl,
639     uint16_t version);
640 void tls12_record_layer_set_initial_epoch(struct tls12_record_layer *rl,
641     uint16_t epoch);
642 uint16_t tls12_record_layer_read_epoch(struct tls12_record_layer *rl);
643 uint16_t tls12_record_layer_write_epoch(struct tls12_record_layer *rl);
644 int tls12_record_layer_use_write_epoch(struct tls12_record_layer *rl,
645     uint16_t epoch);
646 void tls12_record_layer_write_epoch_done(struct tls12_record_layer *rl,
647     uint16_t epoch);
648 void tls12_record_layer_clear_read_state(struct tls12_record_layer *rl);
649 void tls12_record_layer_clear_write_state(struct tls12_record_layer *rl);
650 void tls12_record_layer_reflect_seq_num(struct tls12_record_layer *rl);
651 int tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
652     CBS *mac_key, CBS *key, CBS *iv);
653 int tls12_record_layer_change_write_cipher_state(struct tls12_record_layer *rl,
654     CBS *mac_key, CBS *key, CBS *iv);
655 int tls12_record_layer_open_record(struct tls12_record_layer *rl,
656     uint8_t *buf, size_t buf_len, struct tls_content *out);
657 int tls12_record_layer_seal_record(struct tls12_record_layer *rl,
658     uint8_t content_type, const uint8_t *content, size_t content_len,
659     CBB *out);
660 
661 typedef void (ssl_info_callback_fn)(const SSL *s, int type, int val);
662 typedef void (ssl_msg_callback_fn)(int is_write, int version, int content_type,
663     const void *buf, size_t len, SSL *ssl, void *arg);
664 
665 struct ssl_ctx_st {
666 	const SSL_METHOD *method;
667 	const SSL_QUIC_METHOD *quic_method;
668 
669 	STACK_OF(SSL_CIPHER) *cipher_list;
670 
671 	struct x509_store_st /* X509_STORE */ *cert_store;
672 
673 	/* If timeout is not 0, it is the default timeout value set
674 	 * when SSL_new() is called.  This has been put in to make
675 	 * life easier to set things up */
676 	long session_timeout;
677 
678 	int references;
679 
680 	/* Default values to use in SSL structures follow (these are copied by SSL_new) */
681 
682 	STACK_OF(X509) *extra_certs;
683 
684 	int verify_mode;
685 	size_t sid_ctx_length;
686 	unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH];
687 
688 	X509_VERIFY_PARAM *param;
689 
690 	/*
691 	 * XXX
692 	 * default_passwd_cb used by python and openvpn, need to keep it until we
693 	 * add an accessor
694 	 */
695 	/* Default password callback. */
696 	pem_password_cb *default_passwd_callback;
697 
698 	/* Default password callback user data. */
699 	void *default_passwd_callback_userdata;
700 
701 	uint16_t min_tls_version;
702 	uint16_t max_tls_version;
703 
704 	/*
705 	 * These may be zero to imply minimum or maximum version supported by
706 	 * the method.
707 	 */
708 	uint16_t min_proto_version;
709 	uint16_t max_proto_version;
710 
711 	unsigned long options;
712 	unsigned long mode;
713 
714 	/* If this callback is not null, it will be called each
715 	 * time a session id is added to the cache.  If this function
716 	 * returns 1, it means that the callback will do a
717 	 * SSL_SESSION_free() when it has finished using it.  Otherwise,
718 	 * on 0, it means the callback has finished with it.
719 	 * If remove_session_cb is not null, it will be called when
720 	 * a session-id is removed from the cache.  After the call,
721 	 * OpenSSL will SSL_SESSION_free() it. */
722 	int (*new_session_cb)(struct ssl_st *ssl, SSL_SESSION *sess);
723 	void (*remove_session_cb)(struct ssl_ctx_st *ctx, SSL_SESSION *sess);
724 	SSL_SESSION *(*get_session_cb)(struct ssl_st *ssl,
725 	    const unsigned char *data, int len, int *copy);
726 
727 	/* if defined, these override the X509_verify_cert() calls */
728 	int (*app_verify_callback)(X509_STORE_CTX *, void *);
729 	    void *app_verify_arg;
730 
731 	/* get client cert callback */
732 	int (*client_cert_cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey);
733 
734 	/* cookie generate callback */
735 	int (*app_gen_cookie_cb)(SSL *ssl, unsigned char *cookie,
736 	    unsigned int *cookie_len);
737 
738 	/* verify cookie callback */
739 	int (*app_verify_cookie_cb)(SSL *ssl, const unsigned char *cookie,
740 	    unsigned int cookie_len);
741 
742 	ssl_info_callback_fn *info_callback;
743 
744 	/* callback that allows applications to peek at protocol messages */
745 	ssl_msg_callback_fn *msg_callback;
746 	void *msg_callback_arg;
747 
748 	int (*default_verify_callback)(int ok,X509_STORE_CTX *ctx); /* called 'verify_callback' in the SSL */
749 
750 	/* Default generate session ID callback. */
751 	GEN_SESSION_CB generate_session_id;
752 
753 	/* TLS extensions servername callback */
754 	int (*tlsext_servername_callback)(SSL*, int *, void *);
755 	void *tlsext_servername_arg;
756 
757 	/* Callback to support customisation of ticket key setting */
758 	int (*tlsext_ticket_key_cb)(SSL *ssl, unsigned char *name,
759 	    unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
760 
761 	/* certificate status request info */
762 	/* Callback for status request */
763 	int (*tlsext_status_cb)(SSL *ssl, void *arg);
764 	void *tlsext_status_arg;
765 
766 	struct lhash_st_SSL_SESSION *sessions;
767 
768 	/* Most session-ids that will be cached, default is
769 	 * SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited. */
770 	unsigned long session_cache_size;
771 	struct ssl_session_st *session_cache_head;
772 	struct ssl_session_st *session_cache_tail;
773 
774 	/* This can have one of 2 values, ored together,
775 	 * SSL_SESS_CACHE_CLIENT,
776 	 * SSL_SESS_CACHE_SERVER,
777 	 * Default is SSL_SESSION_CACHE_SERVER, which means only
778 	 * SSL_accept which cache SSL_SESSIONS. */
779 	int session_cache_mode;
780 
781 	struct {
782 		int sess_connect;	/* SSL new conn - started */
783 		int sess_connect_renegotiate;/* SSL reneg - requested */
784 		int sess_connect_good;	/* SSL new conne/reneg - finished */
785 		int sess_accept;	/* SSL new accept - started */
786 		int sess_accept_renegotiate;/* SSL reneg - requested */
787 		int sess_accept_good;	/* SSL accept/reneg - finished */
788 		int sess_miss;		/* session lookup misses  */
789 		int sess_timeout;	/* reuse attempt on timeouted session */
790 		int sess_cache_full;	/* session removed due to full cache */
791 		int sess_hit;		/* session reuse actually done */
792 		int sess_cb_hit;	/* session-id that was not
793 					 * in the cache was
794 					 * passed back via the callback.  This
795 					 * indicates that the application is
796 					 * supplying session-id's from other
797 					 * processes - spooky :-) */
798 	} stats;
799 
800 	CRYPTO_EX_DATA ex_data;
801 
802 	STACK_OF(SSL_CIPHER) *cipher_list_tls13;
803 
804 	SSL_CERT *cert;
805 
806 	/* Default values used when no per-SSL value is defined follow */
807 
808 	/* what we put in client cert requests */
809 	STACK_OF(X509_NAME) *client_CA;
810 
811 	long max_cert_list;
812 
813 	int read_ahead;
814 
815 	int quiet_shutdown;
816 
817 	/* Maximum amount of data to send in one fragment.
818 	 * actual record size can be more than this due to
819 	 * padding and MAC overheads.
820 	 */
821 	unsigned int max_send_fragment;
822 
823 	/* RFC 4507 session ticket keys */
824 	unsigned char tlsext_tick_key_name[16];
825 	unsigned char tlsext_tick_hmac_key[16];
826 	unsigned char tlsext_tick_aes_key[16];
827 
828 	/* SRTP profiles we are willing to do from RFC 5764 */
829 	STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
830 
831 	/*
832 	 * ALPN information.
833 	 */
834 
835 	/*
836 	 * Server callback function that allows the server to select the
837 	 * protocol for the connection.
838 	 *   out: on successful return, this must point to the raw protocol
839 	 *       name (without the length prefix).
840 	 *   outlen: on successful return, this contains the length of out.
841 	 *   in: points to the client's list of supported protocols in
842 	 *       wire-format.
843 	 *   inlen: the length of in.
844 	 */
845 	int (*alpn_select_cb)(SSL *s, const unsigned char **out,
846 	    unsigned char *outlen, const unsigned char *in, unsigned int inlen,
847 	    void *arg);
848 	void *alpn_select_cb_arg;
849 
850 	/* Client list of supported protocols in wire format. */
851 	uint8_t *alpn_client_proto_list;
852 	size_t alpn_client_proto_list_len;
853 
854 	size_t tlsext_ecpointformatlist_length;
855 	uint8_t *tlsext_ecpointformatlist; /* our list */
856 	size_t tlsext_supportedgroups_length;
857 	uint16_t *tlsext_supportedgroups; /* our list */
858 	SSL_CTX_keylog_cb_func keylog_callback; /* Unused. For OpenSSL compatibility. */
859 	size_t num_tickets; /* Unused, for OpenSSL compatibility */
860 };
861 
862 struct ssl_st {
863 	/* protocol version
864 	 * (one of SSL2_VERSION, SSL3_VERSION, TLS1_VERSION, DTLS1_VERSION)
865 	 */
866 	int version;
867 
868 	const SSL_METHOD *method;
869 	const SSL_QUIC_METHOD *quic_method;
870 
871 	/* There are 2 BIO's even though they are normally both the
872 	 * same.  This is so data can be read and written to different
873 	 * handlers */
874 
875 	BIO *rbio; /* used by SSL_read */
876 	BIO *wbio; /* used by SSL_write */
877 	BIO *bbio; /* used during session-id reuse to concatenate
878 		    * messages */
879 	int server;	/* are we the server side? - mostly used by SSL_clear*/
880 
881 	struct ssl3_state_st *s3; /* SSLv3 variables */
882 	struct dtls1_state_st *d1; /* DTLSv1 variables */
883 
884 	X509_VERIFY_PARAM *param;
885 
886 	/* crypto */
887 	STACK_OF(SSL_CIPHER) *cipher_list;
888 
889 	/* This is used to hold the server certificate used */
890 	SSL_CERT *cert;
891 
892 	/* the session_id_context is used to ensure sessions are only reused
893 	 * in the appropriate context */
894 	size_t sid_ctx_length;
895 	unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH];
896 
897 	/* This can also be in the session once a session is established */
898 	SSL_SESSION *session;
899 
900 	/* Used in SSL2 and SSL3 */
901 	int verify_mode;	/* 0 don't care about verify failure.
902 				 * 1 fail if verify fails */
903 	int error;		/* error bytes to be written */
904 	int error_code;		/* actual code */
905 
906 	SSL_CTX *ctx;
907 
908 	long verify_result;
909 
910 	int references;
911 
912 	int client_version;	/* what was passed, used for
913 				 * SSLv3/TLS rollback check */
914 
915 	unsigned int max_send_fragment;
916 
917 	const struct tls_extension **tlsext_build_order;
918 	size_t tlsext_build_order_len;
919 
920 	char *tlsext_hostname;
921 
922 	/* certificate status request info */
923 	/* Status type or -1 if no status type */
924 	int tlsext_status_type;
925 
926 	SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
927 #define session_ctx initial_ctx
928 
929 	struct tls13_ctx *tls13;
930 
931 	uint16_t min_tls_version;
932 	uint16_t max_tls_version;
933 
934 	/*
935 	 * These may be zero to imply minimum or maximum version supported by
936 	 * the method.
937 	 */
938 	uint16_t min_proto_version;
939 	uint16_t max_proto_version;
940 
941 	unsigned long options; /* protocol behaviour */
942 	unsigned long mode; /* API behaviour */
943 
944 	/* Client list of supported protocols in wire format. */
945 	uint8_t *alpn_client_proto_list;
946 	size_t alpn_client_proto_list_len;
947 
948 	/* QUIC transport params we will send */
949 	uint8_t *quic_transport_params;
950 	size_t quic_transport_params_len;
951 
952 	/* XXX Callbacks */
953 
954 	/* true when we are actually in SSL_accept() or SSL_connect() */
955 	int in_handshake;
956 	int (*handshake_func)(SSL *);
957 
958 	ssl_info_callback_fn *info_callback;
959 
960 	/* callback that allows applications to peek at protocol messages */
961 	ssl_msg_callback_fn *msg_callback;
962 	void *msg_callback_arg;
963 
964 	int (*verify_callback)(int ok,X509_STORE_CTX *ctx); /* fail if callback returns 0 */
965 
966 	/* Default generate session ID callback. */
967 	GEN_SESSION_CB generate_session_id;
968 
969 	/* TLS extension debug callback */
970 	void (*tlsext_debug_cb)(SSL *s, int client_server, int type,
971 	    unsigned char *data, int len, void *arg);
972 	void *tlsext_debug_arg;
973 
974 	/* TLS Session Ticket extension callback */
975 	tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb;
976 	void *tls_session_ticket_ext_cb_arg;
977 
978 	/* TLS pre-shared secret session resumption */
979 	tls_session_secret_cb_fn tls_session_secret_cb;
980 	void *tls_session_secret_cb_arg;
981 
982 	/* XXX non-callback */
983 
984 	/* This holds a variable that indicates what we were doing
985 	 * when a 0 or -1 is returned.  This is needed for
986 	 * non-blocking IO so we know what request needs re-doing when
987 	 * in SSL_accept or SSL_connect */
988 	int rwstate;
989 
990 	/* Imagine that here's a boolean member "init" that is
991 	 * switched as soon as SSL_set_{accept/connect}_state
992 	 * is called for the first time, so that "state" and
993 	 * "handshake_func" are properly initialized.  But as
994 	 * handshake_func is == 0 until then, we use this
995 	 * test instead of an "init" member.
996 	 */
997 
998 	int new_session;/* Generate a new session or reuse an old one.
999 			 * NB: For servers, the 'new' session may actually be a previously
1000 			 * cached session or even the previous session unless
1001 			 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
1002 	int quiet_shutdown;/* don't send shutdown packets */
1003 	int shutdown;	/* we have shut things down, 0x01 sent, 0x02
1004 			 * for received */
1005 	BUF_MEM *init_buf;	/* buffer used during init */
1006 	void *init_msg;		/* pointer to handshake message body, set by ssl3_get_message() */
1007 	int init_num;		/* amount read/written */
1008 	int init_off;		/* amount read/written */
1009 
1010 	/* used internally to point at a raw packet */
1011 	unsigned char *packet;
1012 	unsigned int packet_length;
1013 
1014 	int read_ahead;		/* Read as many input bytes as possible
1015 				 * (for non-blocking reads) */
1016 
1017 	int hit;		/* reusing a previous session */
1018 
1019 	STACK_OF(SSL_CIPHER) *cipher_list_tls13;
1020 
1021 	struct tls12_record_layer *rl;
1022 
1023 	/* session info */
1024 
1025 	/* extra application data */
1026 	CRYPTO_EX_DATA ex_data;
1027 
1028 	/* client cert? */
1029 	/* for server side, keep the list of CA_dn we can use */
1030 	STACK_OF(X509_NAME) *client_CA;
1031 
1032 	long max_cert_list;
1033 	int first_packet;
1034 
1035 	/* Expect OCSP CertificateStatus message */
1036 	int tlsext_status_expected;
1037 	/* OCSP status request only */
1038 	STACK_OF(OCSP_RESPID) *tlsext_ocsp_ids;
1039 	X509_EXTENSIONS *tlsext_ocsp_exts;
1040 
1041 	/* OCSP response received or to be sent */
1042 	unsigned char *tlsext_ocsp_resp;
1043 	size_t tlsext_ocsp_resp_len;
1044 
1045 	/* RFC4507 session ticket expected to be received or sent */
1046 	int tlsext_ticket_expected;
1047 
1048 	size_t tlsext_ecpointformatlist_length;
1049 	uint8_t *tlsext_ecpointformatlist; /* our list */
1050 	size_t tlsext_supportedgroups_length;
1051 	uint16_t *tlsext_supportedgroups; /* our list */
1052 
1053 	/* TLS Session Ticket extension override */
1054 	TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
1055 
1056 	STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;	/* What we'll do */
1057 	const SRTP_PROTECTION_PROFILE *srtp_profile;		/* What's been chosen */
1058 
1059 	int renegotiate;/* 1 if we are renegotiating.
1060 			 * 2 if we are a server and are inside a handshake
1061 	                 * (i.e. not just sending a HelloRequest) */
1062 
1063 	int rstate;	/* where we are when reading */
1064 
1065 	int mac_packet;
1066 
1067 	int empty_record_count;
1068 
1069 	size_t num_tickets; /* Unused, for OpenSSL compatibility */
1070 };
1071 
1072 typedef struct ssl3_record_internal_st {
1073 	int type;               /* type of record */
1074 	unsigned int length;    /* How many bytes available */
1075 	unsigned int padding_length; /* Number of padding bytes. */
1076 	unsigned int off;       /* read/write offset into 'buf' */
1077 	unsigned char *data;    /* pointer to the record data */
1078 	unsigned char *input;   /* where the decode bytes are */
1079 	uint16_t epoch;		/* epoch number, needed by DTLS1 */
1080 	unsigned char seq_num[8]; /* sequence number, needed by DTLS1 */
1081 } SSL3_RECORD_INTERNAL;
1082 
1083 typedef struct ssl3_buffer_internal_st {
1084 	unsigned char *buf;	/* at least SSL3_RT_MAX_PACKET_SIZE bytes,
1085 	                         * see ssl3_setup_buffers() */
1086 	size_t len;		/* buffer size */
1087 	int offset;		/* where to 'copy from' */
1088 	int left;		/* how many bytes left */
1089 } SSL3_BUFFER_INTERNAL;
1090 
1091 typedef struct ssl3_state_st {
1092 	long flags;
1093 
1094 	unsigned char server_random[SSL3_RANDOM_SIZE];
1095 	unsigned char client_random[SSL3_RANDOM_SIZE];
1096 
1097 	SSL3_BUFFER_INTERNAL rbuf;	/* read IO goes into here */
1098 	SSL3_BUFFER_INTERNAL wbuf;	/* write IO goes into here */
1099 
1100 	SSL3_RECORD_INTERNAL rrec;	/* each decoded record goes in here */
1101 
1102 	struct tls_content *rcontent;	/* Content from opened TLS records. */
1103 
1104 	/* we allow one fatal and one warning alert to be outstanding,
1105 	 * send close alert via the warning alert */
1106 	int alert_dispatch;
1107 	unsigned char send_alert[2];
1108 
1109 	/* flags for countermeasure against known-IV weakness */
1110 	int need_empty_fragments;
1111 	int empty_fragment_done;
1112 
1113 	/* Unprocessed Alert/Handshake protocol data. */
1114 	struct tls_buffer *alert_fragment;
1115 	struct tls_buffer *handshake_fragment;
1116 
1117 	/* partial write - check the numbers match */
1118 	unsigned int wnum;	/* number of bytes sent so far */
1119 	int wpend_tot;		/* number bytes written */
1120 	int wpend_type;
1121 	int wpend_ret;		/* number of bytes submitted */
1122 	const unsigned char *wpend_buf;
1123 
1124 	/* Transcript of handshake messages that have been sent and received. */
1125 	struct tls_buffer *handshake_transcript;
1126 
1127 	/* Rolling hash of handshake messages. */
1128 	EVP_MD_CTX *handshake_hash;
1129 
1130 	/* this is set whenerver we see a change_cipher_spec message
1131 	 * come in when we are not looking for one */
1132 	int change_cipher_spec;
1133 
1134 	int warn_alert;
1135 	int fatal_alert;
1136 
1137 	/* This flag is set when we should renegotiate ASAP, basically when
1138 	 * there is no more data in the read or write buffers */
1139 	int renegotiate;
1140 	int total_renegotiations;
1141 	int num_renegotiations;
1142 
1143 	int in_read_app_data;
1144 
1145 	SSL_HANDSHAKE hs;
1146 
1147 	/* Connection binding to prevent renegotiation attacks */
1148 	unsigned char previous_client_finished[EVP_MAX_MD_SIZE];
1149 	unsigned char previous_client_finished_len;
1150 	unsigned char previous_server_finished[EVP_MAX_MD_SIZE];
1151 	unsigned char previous_server_finished_len;
1152 	int send_connection_binding; /* TODOEKR */
1153 
1154 	/* Set if we saw a Renegotiation Indication extension from our peer. */
1155 	int renegotiate_seen;
1156 
1157 	/*
1158 	 * ALPN information.
1159 	 *
1160 	 * In a server these point to the selected ALPN protocol after the
1161 	 * ClientHello has been processed. In a client these contain the
1162 	 * protocol that the server selected once the ServerHello has been
1163 	 * processed.
1164 	 */
1165 	uint8_t *alpn_selected;
1166 	size_t alpn_selected_len;
1167 
1168 	/* Contains the QUIC transport params received from our peer. */
1169 	uint8_t *peer_quic_transport_params;
1170 	size_t peer_quic_transport_params_len;
1171 } SSL3_STATE;
1172 
1173 /*
1174  * Flag values for enc_flags.
1175  */
1176 
1177 /* Uses signature algorithms extension. */
1178 #define SSL_ENC_FLAG_SIGALGS            (1 << 1)
1179 
1180 /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2. */
1181 #define SSL_ENC_FLAG_TLS1_2_CIPHERS     (1 << 4)
1182 
1183 /* Allow TLS 1.3 ciphersuites only. */
1184 #define SSL_ENC_FLAG_TLS1_3_CIPHERS     (1 << 5)
1185 
1186 #define TLSV1_ENC_FLAGS		0
1187 #define TLSV1_1_ENC_FLAGS	0
1188 #define TLSV1_2_ENC_FLAGS	(SSL_ENC_FLAG_SIGALGS		| \
1189 				 SSL_ENC_FLAG_TLS1_2_CIPHERS)
1190 #define TLSV1_3_ENC_FLAGS	(SSL_ENC_FLAG_SIGALGS		| \
1191 				 SSL_ENC_FLAG_TLS1_3_CIPHERS)
1192 
1193 extern const SSL_CIPHER ssl3_ciphers[];
1194 
1195 const char *ssl_version_string(int ver);
1196 int ssl_version_set_min(const SSL_METHOD *meth, uint16_t proto_ver,
1197     uint16_t max_tls_ver, uint16_t *out_tls_ver, uint16_t *out_proto_ver);
1198 int ssl_version_set_max(const SSL_METHOD *meth, uint16_t proto_ver,
1199     uint16_t min_tls_ver, uint16_t *out_tls_ver, uint16_t *out_proto_ver);
1200 int ssl_enabled_tls_version_range(SSL *s, uint16_t *min_ver, uint16_t *max_ver);
1201 int ssl_supported_tls_version_range(SSL *s, uint16_t *min_ver, uint16_t *max_ver);
1202 uint16_t ssl_tls_version(uint16_t version);
1203 uint16_t ssl_effective_tls_version(SSL *s);
1204 int ssl_max_supported_version(SSL *s, uint16_t *max_ver);
1205 int ssl_max_legacy_version(SSL *s, uint16_t *max_ver);
1206 int ssl_max_shared_version(SSL *s, uint16_t peer_ver, uint16_t *max_ver);
1207 int ssl_check_version_from_server(SSL *s, uint16_t server_version);
1208 int ssl_legacy_stack_version(SSL *s, uint16_t version);
1209 int ssl_cipher_in_list(STACK_OF(SSL_CIPHER) *ciphers, const SSL_CIPHER *cipher);
1210 int ssl_cipher_allowed_in_tls_version_range(const SSL_CIPHER *cipher,
1211     uint16_t min_ver, uint16_t max_ver);
1212 
1213 const SSL_METHOD *tls_legacy_method(void);
1214 const SSL_METHOD *ssl_get_method(uint16_t version);
1215 
1216 void ssl_clear_cipher_state(SSL *s);
1217 int ssl_clear_bad_session(SSL *s);
1218 
1219 void ssl_info_callback(const SSL *s, int type, int value);
1220 void ssl_msg_callback(SSL *s, int is_write, int content_type,
1221     const void *msg_buf, size_t msg_len);
1222 void ssl_msg_callback_cbs(SSL *s, int is_write, int content_type, CBS *cbs);
1223 
1224 SSL_CERT *ssl_cert_new(void);
1225 SSL_CERT *ssl_cert_dup(SSL_CERT *cert);
1226 void ssl_cert_free(SSL_CERT *c);
1227 SSL_CERT *ssl_get0_cert(SSL_CTX *ctx, SSL *ssl);
1228 int ssl_cert_set0_chain(SSL_CTX *ctx, SSL *ssl, STACK_OF(X509) *chain);
1229 int ssl_cert_set1_chain(SSL_CTX *ctx, SSL *ssl, STACK_OF(X509) *chain);
1230 int ssl_cert_add0_chain_cert(SSL_CTX *ctx, SSL *ssl, X509 *cert);
1231 int ssl_cert_add1_chain_cert(SSL_CTX *ctx, SSL *ssl, X509 *cert);
1232 
1233 int ssl_security_default_cb(const SSL *ssl, const SSL_CTX *ctx, int op,
1234     int bits, int nid, void *other, void *ex_data);
1235 
1236 int ssl_security_cipher_check(const SSL *ssl, SSL_CIPHER *cipher);
1237 int ssl_security_shared_cipher(const SSL *ssl, SSL_CIPHER *cipher);
1238 int ssl_security_supported_cipher(const SSL *ssl, SSL_CIPHER *cipher);
1239 int ssl_ctx_security_dh(const SSL_CTX *ctx, DH *dh);
1240 int ssl_security_dh(const SSL *ssl, DH *dh);
1241 int ssl_security_sigalg_check(const SSL *ssl, const EVP_PKEY *pkey);
1242 int ssl_security_tickets(const SSL *ssl);
1243 int ssl_security_version(const SSL *ssl, int version);
1244 int ssl_security_cert(const SSL_CTX *ctx, const SSL *ssl, X509 *x509,
1245     int is_peer, int *out_error);
1246 int ssl_security_cert_chain(const SSL *ssl, STACK_OF(X509) *sk,
1247     X509 *x509, int *out_error);
1248 int ssl_security_shared_group(const SSL *ssl, uint16_t group_id);
1249 int ssl_security_supported_group(const SSL *ssl, uint16_t group_id);
1250 
1251 SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int include_ticket);
1252 int ssl_get_new_session(SSL *s, int session);
1253 int ssl_get_prev_session(SSL *s, CBS *session_id, CBS *ext_block,
1254     int *alert);
1255 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *ciphers, CBB *cbb);
1256 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, CBS *cbs);
1257 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *meth,
1258     STACK_OF(SSL_CIPHER) **pref, STACK_OF(SSL_CIPHER) *tls13,
1259     const char *rule_str, SSL_CERT *cert);
1260 int ssl_parse_ciphersuites(STACK_OF(SSL_CIPHER) **out_ciphers, const char *str);
1261 int ssl_merge_cipherlists(STACK_OF(SSL_CIPHER) *cipherlist,
1262     STACK_OF(SSL_CIPHER) *cipherlist_tls13,
1263     STACK_OF(SSL_CIPHER) **out_cipherlist);
1264 void ssl_update_cache(SSL *s, int mode);
1265 int ssl_cipher_get_evp(SSL *s, const EVP_CIPHER **enc,
1266     const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size);
1267 int ssl_cipher_get_evp_aead(SSL *s, const EVP_AEAD **aead);
1268 int ssl_get_handshake_evp_md(SSL *s, const EVP_MD **md);
1269 
1270 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk);
1271 int ssl_undefined_function(SSL *s);
1272 int ssl_undefined_void_function(void);
1273 int ssl_undefined_const_function(const SSL *s);
1274 SSL_CERT_PKEY *ssl_get_server_send_pkey(const SSL *s);
1275 EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *c, const EVP_MD **pmd,
1276     const struct ssl_sigalg **sap);
1277 size_t ssl_dhe_params_auto_key_bits(SSL *s);
1278 int ssl_cert_type(EVP_PKEY *pkey);
1279 void ssl_set_cert_masks(SSL_CERT *c, const SSL_CIPHER *cipher);
1280 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s);
1281 int ssl_has_ecc_ciphers(SSL *s);
1282 int ssl_verify_alarm_type(long type);
1283 
1284 int SSL_SESSION_ticket(SSL_SESSION *ss, unsigned char **out, size_t *out_len);
1285 
1286 int ssl3_do_write(SSL *s, int type);
1287 int ssl3_send_alert(SSL *s, int level, int desc);
1288 int ssl3_get_req_cert_types(SSL *s, CBB *cbb);
1289 int ssl3_get_message(SSL *s, int st1, int stn, int mt, long max);
1290 int ssl3_num_ciphers(void);
1291 const SSL_CIPHER *ssl3_get_cipher_by_index(int idx);
1292 const SSL_CIPHER *ssl3_get_cipher_by_value(uint16_t value);
1293 int ssl3_renegotiate(SSL *ssl);
1294 
1295 int ssl3_renegotiate_check(SSL *ssl);
1296 
1297 void ssl_force_want_read(SSL *s);
1298 
1299 int ssl3_dispatch_alert(SSL *s);
1300 int ssl3_read_alert(SSL *s);
1301 int ssl3_read_change_cipher_spec(SSL *s);
1302 int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
1303 int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
1304 int ssl3_output_cert_chain(SSL *s, CBB *cbb, SSL_CERT_PKEY *cpk);
1305 SSL_CIPHER *ssl3_choose_cipher(SSL *ssl, STACK_OF(SSL_CIPHER) *clnt,
1306     STACK_OF(SSL_CIPHER) *srvr);
1307 int	ssl3_setup_buffers(SSL *s);
1308 int	ssl3_setup_init_buffer(SSL *s);
1309 void ssl3_release_init_buffer(SSL *s);
1310 int	ssl3_setup_read_buffer(SSL *s);
1311 int	ssl3_setup_write_buffer(SSL *s);
1312 void ssl3_release_buffer(SSL3_BUFFER_INTERNAL *b);
1313 void ssl3_release_read_buffer(SSL *s);
1314 void ssl3_release_write_buffer(SSL *s);
1315 int	ssl3_new(SSL *s);
1316 void	ssl3_free(SSL *s);
1317 int	ssl3_accept(SSL *s);
1318 int	ssl3_connect(SSL *s);
1319 int	ssl3_read(SSL *s, void *buf, int len);
1320 int	ssl3_peek(SSL *s, void *buf, int len);
1321 int	ssl3_write(SSL *s, const void *buf, int len);
1322 int	ssl3_shutdown(SSL *s);
1323 void	ssl3_clear(SSL *s);
1324 long	ssl3_ctrl(SSL *s, int cmd, long larg, void *parg);
1325 long	ssl3_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg);
1326 long	ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)(void));
1327 long	ssl3_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp)(void));
1328 int	ssl3_pending(const SSL *s);
1329 
1330 int ssl3_handshake_msg_hdr_len(SSL *s);
1331 int ssl3_handshake_msg_start(SSL *s, CBB *handshake, CBB *body,
1332     uint8_t msg_type);
1333 int ssl3_handshake_msg_finish(SSL *s, CBB *handshake);
1334 int ssl3_handshake_write(SSL *s);
1335 int ssl3_record_write(SSL *s, int type);
1336 
1337 int ssl3_do_change_cipher_spec(SSL *ssl);
1338 
1339 int ssl3_packet_read(SSL *s, int plen);
1340 int ssl3_packet_extend(SSL *s, int plen);
1341 int ssl_server_legacy_first_packet(SSL *s);
1342 int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
1343     unsigned int len);
1344 
1345 int ssl_kex_generate_dhe(DH *dh, DH *dh_params);
1346 int ssl_kex_generate_dhe_params_auto(DH *dh, size_t key_len);
1347 int ssl_kex_params_dhe(DH *dh, CBB *cbb);
1348 int ssl_kex_public_dhe(DH *dh, CBB *cbb);
1349 int ssl_kex_peer_params_dhe(DH *dh, CBS *cbs, int *decode_error,
1350     int *invalid_params);
1351 int ssl_kex_peer_public_dhe(DH *dh, CBS *cbs, int *decode_error,
1352     int *invalid_key);
1353 int ssl_kex_derive_dhe(DH *dh, DH *dh_peer,
1354     uint8_t **shared_key, size_t *shared_key_len);
1355 
1356 int ssl_kex_dummy_ecdhe_x25519(EVP_PKEY *pkey);
1357 int ssl_kex_generate_ecdhe_ecp(EC_KEY *ecdh, int nid);
1358 int ssl_kex_public_ecdhe_ecp(EC_KEY *ecdh, CBB *cbb);
1359 int ssl_kex_peer_public_ecdhe_ecp(EC_KEY *ecdh, int nid, CBS *cbs);
1360 int ssl_kex_derive_ecdhe_ecp(EC_KEY *ecdh, EC_KEY *ecdh_peer,
1361     uint8_t **shared_key, size_t *shared_key_len);
1362 
1363 int tls1_new(SSL *s);
1364 void tls1_free(SSL *s);
1365 void tls1_clear(SSL *s);
1366 
1367 int ssl_init_wbio_buffer(SSL *s, int push);
1368 void ssl_free_wbio_buffer(SSL *s);
1369 
1370 int tls1_transcript_hash_init(SSL *s);
1371 int tls1_transcript_hash_update(SSL *s, const unsigned char *buf, size_t len);
1372 int tls1_transcript_hash_value(SSL *s, unsigned char *out, size_t len,
1373     size_t *outlen);
1374 void tls1_transcript_hash_free(SSL *s);
1375 
1376 int tls1_transcript_init(SSL *s);
1377 void tls1_transcript_free(SSL *s);
1378 void tls1_transcript_reset(SSL *s);
1379 int tls1_transcript_append(SSL *s, const unsigned char *buf, size_t len);
1380 int tls1_transcript_data(SSL *s, const unsigned char **data, size_t *len);
1381 void tls1_transcript_freeze(SSL *s);
1382 void tls1_transcript_unfreeze(SSL *s);
1383 int tls1_transcript_record(SSL *s, const unsigned char *buf, size_t len);
1384 
1385 int tls1_PRF(SSL *s, const unsigned char *secret, size_t secret_len,
1386     const void *seed1, size_t seed1_len, const void *seed2, size_t seed2_len,
1387     const void *seed3, size_t seed3_len, const void *seed4, size_t seed4_len,
1388     const void *seed5, size_t seed5_len, unsigned char *out, size_t out_len);
1389 
1390 void tls1_cleanup_key_block(SSL *s);
1391 int tls1_change_read_cipher_state(SSL *s);
1392 int tls1_change_write_cipher_state(SSL *s);
1393 int tls1_setup_key_block(SSL *s);
1394 int tls1_generate_key_block(SSL *s, uint8_t *key_block, size_t key_block_len);
1395 int ssl_ok(SSL *s);
1396 
1397 int tls12_derive_finished(SSL *s);
1398 int tls12_derive_peer_finished(SSL *s);
1399 int tls12_derive_master_secret(SSL *s, uint8_t *premaster_secret,
1400     size_t premaster_secret_len);
1401 
1402 int ssl_using_ecc_cipher(SSL *s);
1403 int ssl_check_srvr_ecc_cert_and_alg(SSL *s, X509 *x);
1404 
1405 void tls1_get_formatlist(const SSL *s, int client_formats,
1406     const uint8_t **pformats, size_t *pformatslen);
1407 void tls1_get_group_list(const SSL *s, int client_groups,
1408     const uint16_t **pgroups, size_t *pgroupslen);
1409 
1410 int tls1_set_groups(uint16_t **out_group_ids, size_t *out_group_ids_len,
1411     const int *groups, size_t ngroups);
1412 int tls1_set_group_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
1413     const char *groups);
1414 
1415 int tls1_ec_group_id2nid(uint16_t group_id, int *out_nid);
1416 int tls1_ec_group_id2bits(uint16_t group_id, int *out_bits);
1417 int tls1_ec_nid2group_id(int nid, uint16_t *out_group_id);
1418 int tls1_check_group(SSL *s, uint16_t group_id);
1419 int tls1_count_shared_groups(const SSL *ssl, size_t *out_count);
1420 int tls1_get_shared_group_by_index(const SSL *ssl, size_t index, int *out_nid);
1421 int tls1_get_supported_group(const SSL *s, int *out_nid);
1422 
1423 int ssl_check_clienthello_tlsext_early(SSL *s);
1424 int ssl_check_clienthello_tlsext_late(SSL *s);
1425 int ssl_check_serverhello_tlsext(SSL *s);
1426 
1427 #define TLS1_TICKET_FATAL_ERROR		-1
1428 #define TLS1_TICKET_NONE		 0
1429 #define TLS1_TICKET_EMPTY		 1
1430 #define TLS1_TICKET_NOT_DECRYPTED	 2
1431 #define TLS1_TICKET_DECRYPTED		 3
1432 
1433 int tls1_process_ticket(SSL *s, CBS *ext_block, int *alert, SSL_SESSION **ret);
1434 
1435 int tls1_check_ec_server_key(SSL *s);
1436 
1437 /* s3_cbc.c */
1438 void ssl3_cbc_copy_mac(unsigned char *out, const SSL3_RECORD_INTERNAL *rec,
1439     unsigned int md_size, unsigned int orig_len);
1440 int ssl3_cbc_remove_padding(SSL3_RECORD_INTERNAL *rec, unsigned int eiv_len,
1441     unsigned int mac_size);
1442 char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
1443 int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx, unsigned char *md_out,
1444     size_t *md_out_size, const unsigned char header[13],
1445     const unsigned char *data, size_t data_plus_mac_size,
1446     size_t data_plus_mac_plus_padding_size, const unsigned char *mac_secret,
1447     unsigned int mac_secret_length);
1448 int SSL_state_func_code(int _state);
1449 
1450 #define SSLerror(s, r) SSL_error_internal(s, r, __FILE__, __LINE__)
1451 #define SSLerrorx(r) ERR_PUT_error(ERR_LIB_SSL,(0xfff),(r),__FILE__,__LINE__)
1452 void SSL_error_internal(const SSL *s, int r, char *f, int l);
1453 
1454 #ifndef OPENSSL_NO_SRTP
1455 
1456 int srtp_find_profile_by_name(const char *profile_name,
1457     const SRTP_PROTECTION_PROFILE **pptr, unsigned int len);
1458 int srtp_find_profile_by_num(unsigned int profile_num,
1459     const SRTP_PROTECTION_PROFILE **pptr);
1460 
1461 #endif /* OPENSSL_NO_SRTP */
1462 
1463 int tls_process_peer_certs(SSL *s, STACK_OF(X509) *peer_certs);
1464 
1465 __END_HIDDEN_DECLS
1466 
1467 #endif /* !HEADER_SSL_LOCL_H */
1468