xref: /dragonfly/crypto/libressl/ssl/ssl_locl.h (revision 8edacedf)
1 /* $OpenBSD: ssl_locl.h,v 1.295 2020/09/24 18:12:00 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 #include <openssl/bio.h>
156 #include <openssl/buffer.h>
157 #include <openssl/dsa.h>
158 #include <openssl/err.h>
159 #include <openssl/rsa.h>
160 #include <openssl/ssl.h>
161 #include <openssl/stack.h>
162 
163 #include "bytestring.h"
164 #include "ssl_sigalgs.h"
165 #include "tls13_internal.h"
166 
167 __BEGIN_HIDDEN_DECLS
168 
169 #define CTASSERT(x)	extern char  _ctassert[(x) ? 1 : -1 ]   \
170 			    __attribute__((__unused__))
171 
172 #ifndef LIBRESSL_HAS_TLS1_3_CLIENT
173 #define LIBRESSL_HAS_TLS1_3_CLIENT
174 #endif
175 
176 #ifndef LIBRESSL_HAS_TLS1_3_SERVER
177 #define LIBRESSL_HAS_TLS1_3_SERVER
178 #endif
179 
180 #if defined(LIBRESSL_HAS_TLS1_3_CLIENT) || defined(LIBRESSL_HAS_TLS1_3_SERVER)
181 #define LIBRESSL_HAS_TLS1_3
182 #endif
183 
184 /* LOCAL STUFF */
185 
186 #define SSL_DECRYPT	0
187 #define SSL_ENCRYPT	1
188 
189 /*
190  * Define the Bitmasks for SSL_CIPHER.algorithms.
191  * This bits are used packed as dense as possible. If new methods/ciphers
192  * etc will be added, the bits a likely to change, so this information
193  * is for internal library use only, even though SSL_CIPHER.algorithms
194  * can be publicly accessed.
195  * Use the according functions for cipher management instead.
196  *
197  * The bit mask handling in the selection and sorting scheme in
198  * ssl_create_cipher_list() has only limited capabilities, reflecting
199  * that the different entities within are mutually exclusive:
200  * ONLY ONE BIT PER MASK CAN BE SET AT A TIME.
201  */
202 
203 /* Bits for algorithm_mkey (key exchange algorithm) */
204 #define SSL_kRSA		0x00000001L /* RSA key exchange */
205 #define SSL_kDHE		0x00000008L /* tmp DH key no DH cert */
206 #define SSL_kECDHE		0x00000080L /* ephemeral ECDH */
207 #define SSL_kGOST		0x00000200L /* GOST key exchange */
208 #define SSL_kTLS1_3		0x00000400L /* TLSv1.3 key exchange */
209 
210 /* Bits for algorithm_auth (server authentication) */
211 #define SSL_aRSA		0x00000001L /* RSA auth */
212 #define SSL_aDSS 		0x00000002L /* DSS auth */
213 #define SSL_aNULL 		0x00000004L /* no auth (i.e. use ADH or AECDH) */
214 #define SSL_aECDSA              0x00000040L /* ECDSA auth*/
215 #define SSL_aGOST01 		0x00000200L /* GOST R 34.10-2001 signature auth */
216 #define SSL_aTLS1_3		0x00000400L /* TLSv1.3 authentication */
217 
218 /* Bits for algorithm_enc (symmetric encryption) */
219 #define SSL_DES			0x00000001L
220 #define SSL_3DES		0x00000002L
221 #define SSL_RC4			0x00000004L
222 #define SSL_IDEA		0x00000008L
223 #define SSL_eNULL		0x00000010L
224 #define SSL_AES128		0x00000020L
225 #define SSL_AES256		0x00000040L
226 #define SSL_CAMELLIA128		0x00000080L
227 #define SSL_CAMELLIA256		0x00000100L
228 #define SSL_eGOST2814789CNT	0x00000200L
229 #define SSL_AES128GCM		0x00000400L
230 #define SSL_AES256GCM		0x00000800L
231 #define SSL_CHACHA20POLY1305	0x00001000L
232 
233 #define SSL_AES        		(SSL_AES128|SSL_AES256|SSL_AES128GCM|SSL_AES256GCM)
234 #define SSL_CAMELLIA		(SSL_CAMELLIA128|SSL_CAMELLIA256)
235 
236 
237 /* Bits for algorithm_mac (symmetric authentication) */
238 
239 #define SSL_MD5			0x00000001L
240 #define SSL_SHA1		0x00000002L
241 #define SSL_GOST94      0x00000004L
242 #define SSL_GOST89MAC   0x00000008L
243 #define SSL_SHA256		0x00000010L
244 #define SSL_SHA384		0x00000020L
245 /* Not a real MAC, just an indication it is part of cipher */
246 #define SSL_AEAD		0x00000040L
247 #define SSL_STREEBOG256		0x00000080L
248 
249 /* Bits for algorithm_ssl (protocol version) */
250 #define SSL_SSLV3		0x00000002L
251 #define SSL_TLSV1		SSL_SSLV3	/* for now */
252 #define SSL_TLSV1_2		0x00000004L
253 #define SSL_TLSV1_3		0x00000008L
254 
255 
256 /* Bits for algorithm2 (handshake digests and other extra flags) */
257 
258 #define SSL_HANDSHAKE_MAC_MASK		0xff0
259 #define SSL_HANDSHAKE_MAC_MD5		0x010
260 #define SSL_HANDSHAKE_MAC_SHA		0x020
261 #define SSL_HANDSHAKE_MAC_GOST94	0x040
262 #define SSL_HANDSHAKE_MAC_SHA256	0x080
263 #define SSL_HANDSHAKE_MAC_SHA384	0x100
264 #define SSL_HANDSHAKE_MAC_STREEBOG256	0x200
265 #define SSL_HANDSHAKE_MAC_DEFAULT (SSL_HANDSHAKE_MAC_MD5 | SSL_HANDSHAKE_MAC_SHA)
266 
267 #define SSL3_CK_ID		0x03000000
268 #define SSL3_CK_VALUE_MASK	0x0000ffff
269 
270 #define TLS1_PRF_DGST_MASK	(0xff << TLS1_PRF_DGST_SHIFT)
271 
272 #define TLS1_PRF_DGST_SHIFT 10
273 #define TLS1_PRF_MD5 (SSL_HANDSHAKE_MAC_MD5 << TLS1_PRF_DGST_SHIFT)
274 #define TLS1_PRF_SHA1 (SSL_HANDSHAKE_MAC_SHA << TLS1_PRF_DGST_SHIFT)
275 #define TLS1_PRF_SHA256 (SSL_HANDSHAKE_MAC_SHA256 << TLS1_PRF_DGST_SHIFT)
276 #define TLS1_PRF_SHA384 (SSL_HANDSHAKE_MAC_SHA384 << TLS1_PRF_DGST_SHIFT)
277 #define TLS1_PRF_GOST94 (SSL_HANDSHAKE_MAC_GOST94 << TLS1_PRF_DGST_SHIFT)
278 #define TLS1_PRF_STREEBOG256 (SSL_HANDSHAKE_MAC_STREEBOG256 << TLS1_PRF_DGST_SHIFT)
279 #define TLS1_PRF (TLS1_PRF_MD5 | TLS1_PRF_SHA1)
280 
281 /*
282  * Stream MAC for GOST ciphersuites from cryptopro draft
283  * (currently this also goes into algorithm2).
284  */
285 #define TLS1_STREAM_MAC 0x04
286 
287 /*
288  * SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD is an algorithm2 flag that
289  * indicates that the variable part of the nonce is included as a prefix of
290  * the record (AES-GCM, for example, does this with an 8-byte variable nonce.)
291  */
292 #define SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD (1 << 22)
293 
294 /*
295  * SSL_CIPHER_AEAD_FIXED_NONCE_LEN returns the number of bytes of fixed nonce
296  * for an SSL_CIPHER with an algorithm_mac of SSL_AEAD.
297  */
298 #define SSL_CIPHER_AEAD_FIXED_NONCE_LEN(ssl_cipher) \
299 	(((ssl_cipher->algorithm2 >> 24) & 0xf) * 2)
300 
301 /*
302  * Cipher strength information.
303  */
304 #define SSL_STRONG_MASK		0x000001fcL
305 #define SSL_STRONG_NONE		0x00000004L
306 #define SSL_LOW			0x00000020L
307 #define SSL_MEDIUM		0x00000040L
308 #define SSL_HIGH		0x00000080L
309 
310 /*
311  * The keylength (measured in RSA key bits, I guess)  for temporary keys.
312  * Cipher argument is so that this can be variable in the future.
313  */
314 #define SSL_C_PKEYLENGTH(c)	1024
315 
316 /* Check if an SSL structure is using DTLS. */
317 #define SSL_IS_DTLS(s) \
318 	(s->method->internal->version == DTLS1_VERSION)
319 
320 /* See if we need explicit IV. */
321 #define SSL_USE_EXPLICIT_IV(s) \
322 	(s->method->internal->ssl3_enc->enc_flags & SSL_ENC_FLAG_EXPLICIT_IV)
323 
324 /* See if we use signature algorithms extension. */
325 #define SSL_USE_SIGALGS(s) \
326 	(s->method->internal->ssl3_enc->enc_flags & SSL_ENC_FLAG_SIGALGS)
327 
328 /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2. */
329 #define SSL_USE_TLS1_2_CIPHERS(s) \
330 	(s->method->internal->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)
331 
332 /* Allow TLS 1.3 ciphersuites only. */
333 #define SSL_USE_TLS1_3_CIPHERS(s) \
334 	(s->method->internal->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_3_CIPHERS)
335 
336 #define SSL_PKEY_RSA		0
337 #define SSL_PKEY_ECC		1
338 #define SSL_PKEY_GOST01		2
339 #define SSL_PKEY_NUM		3
340 
341 #define SSL_MAX_EMPTY_RECORDS	32
342 
343 /* SSL_kRSA <- RSA_ENC | (RSA_TMP & RSA_SIGN) |
344  * 	    <- (EXPORT & (RSA_ENC | RSA_TMP) & RSA_SIGN)
345  * SSL_kDH  <- DH_ENC & (RSA_ENC | RSA_SIGN | DSA_SIGN)
346  * SSL_kDHE <- RSA_ENC | RSA_SIGN | DSA_SIGN
347  * SSL_aRSA <- RSA_ENC | RSA_SIGN
348  * SSL_aDSS <- DSA_SIGN
349  */
350 
351 /*
352 #define CERT_INVALID		0
353 #define CERT_PUBLIC_KEY		1
354 #define CERT_PRIVATE_KEY	2
355 */
356 
357 /* From ECC-TLS draft, used in encoding the curve type in
358  * ECParameters
359  */
360 #define EXPLICIT_PRIME_CURVE_TYPE  1
361 #define EXPLICIT_CHAR2_CURVE_TYPE  2
362 #define NAMED_CURVE_TYPE           3
363 
364 typedef struct ssl_method_internal_st {
365 	int version;
366 
367 	uint16_t min_version;
368 	uint16_t max_version;
369 
370 	int (*ssl_new)(SSL *s);
371 	void (*ssl_clear)(SSL *s);
372 	void (*ssl_free)(SSL *s);
373 
374 	int (*ssl_accept)(SSL *s);
375 	int (*ssl_connect)(SSL *s);
376 	int (*ssl_shutdown)(SSL *s);
377 
378 	int (*ssl_renegotiate)(SSL *s);
379 	int (*ssl_renegotiate_check)(SSL *s);
380 
381 	int (*ssl_pending)(const SSL *s);
382 	int (*ssl_read_bytes)(SSL *s, int type, unsigned char *buf, int len,
383 	    int peek);
384 	int (*ssl_write_bytes)(SSL *s, int type, const void *buf_, int len);
385 
386 	struct ssl3_enc_method *ssl3_enc; /* Extra SSLv3/TLS stuff */
387 } SSL_METHOD_INTERNAL;
388 
389 typedef struct ssl_session_internal_st {
390 	CRYPTO_EX_DATA ex_data; /* application specific data */
391 
392 	/* These are used to make removal of session-ids more
393 	 * efficient and to implement a maximum cache size. */
394 	struct ssl_session_st *prev, *next;
395 
396 	/* Used to indicate that session resumption is not allowed.
397 	 * Applications can also set this bit for a new session via
398 	 * not_resumable_session_cb to disable session caching and tickets. */
399 	int not_resumable;
400 
401 	/* The cert is the certificate used to establish this connection */
402 	struct sess_cert_st /* SESS_CERT */ *sess_cert;
403 
404 	size_t tlsext_ecpointformatlist_length;
405 	uint8_t *tlsext_ecpointformatlist; /* peer's list */
406 	size_t tlsext_supportedgroups_length;
407 	uint16_t *tlsext_supportedgroups; /* peer's list */
408 } SSL_SESSION_INTERNAL;
409 #define SSI(s) (s->session->internal)
410 
411 typedef struct ssl_handshake_st {
412 	/* state contains one of the SSL3_ST_* values. */
413 	int state;
414 
415 	/* used when SSL_ST_FLUSH_DATA is entered */
416 	int next_state;
417 
418 	/*  new_cipher is the cipher being negotiated in this handshake. */
419 	const SSL_CIPHER *new_cipher;
420 
421 	/* key_block is the record-layer key block for TLS 1.2 and earlier. */
422 	int key_block_len;
423 	unsigned char *key_block;
424 
425 	/* Extensions seen in this handshake. */
426 	uint32_t extensions_seen;
427 
428 	/* sigalgs offered in this handshake in wire form */
429 	size_t sigalgs_len;
430 	uint8_t *sigalgs;
431 } SSL_HANDSHAKE;
432 
433 typedef struct cert_pkey_st {
434 	X509 *x509;
435 	EVP_PKEY *privatekey;
436 	STACK_OF(X509) *chain;
437 } CERT_PKEY;
438 
439 typedef struct ssl_handshake_tls13_st {
440 	uint16_t min_version;
441 	uint16_t max_version;
442 	uint16_t version;
443 
444 	int use_legacy;
445 	int hrr;
446 
447 	/* Certificate and sigalg selected for use (static pointers). */
448 	const CERT_PKEY *cpk;
449 	const struct ssl_sigalg *sigalg;
450 
451 	/* Version proposed by peer server. */
452 	uint16_t server_version;
453 
454 	uint16_t server_group;
455 	struct tls13_key_share *key_share;
456 	struct tls13_secrets *secrets;
457 
458 	uint8_t *cookie;
459 	size_t cookie_len;
460 
461 	/* Preserved transcript hash. */
462 	uint8_t transcript_hash[EVP_MAX_MD_SIZE];
463 	size_t transcript_hash_len;
464 
465 	/* Legacy session ID. */
466 	uint8_t legacy_session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
467 	size_t legacy_session_id_len;
468 
469 	/* ClientHello hash, used to validate following HelloRetryRequest */
470 	EVP_MD_CTX *clienthello_md_ctx;
471 	unsigned char *clienthello_hash;
472 	unsigned int clienthello_hash_len;
473 
474 } SSL_HANDSHAKE_TLS13;
475 
476 struct tls12_record_layer;
477 
478 struct tls12_record_layer *tls12_record_layer_new(void);
479 void tls12_record_layer_free(struct tls12_record_layer *rl);
480 void tls12_record_layer_set_version(struct tls12_record_layer *rl,
481     uint16_t version);
482 void tls12_record_layer_set_read_epoch(struct tls12_record_layer *rl,
483     uint16_t epoch);
484 void tls12_record_layer_set_write_epoch(struct tls12_record_layer *rl,
485     uint16_t epoch);
486 void tls12_record_layer_clear_read_state(struct tls12_record_layer *rl);
487 void tls12_record_layer_clear_write_state(struct tls12_record_layer *rl);
488 void tls12_record_layer_set_read_seq_num(struct tls12_record_layer *rl,
489     uint8_t *seq_num);
490 void tls12_record_layer_set_write_seq_num(struct tls12_record_layer *rl,
491     uint8_t *seq_num);
492 int tls12_record_layer_set_read_aead(struct tls12_record_layer *rl,
493     SSL_AEAD_CTX *aead_ctx);
494 int tls12_record_layer_set_write_aead(struct tls12_record_layer *rl,
495     SSL_AEAD_CTX *aead_ctx);
496 int tls12_record_layer_set_read_cipher_hash(struct tls12_record_layer *rl,
497     EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, int stream_mac);
498 int tls12_record_layer_set_write_cipher_hash(struct tls12_record_layer *rl,
499     EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, int stream_mac);
500 int tls12_record_layer_seal_record(struct tls12_record_layer *rl,
501     uint8_t content_type, const uint8_t *content, size_t content_len,
502     CBB *out);
503 
504 typedef struct ssl_ctx_internal_st {
505 	uint16_t min_version;
506 	uint16_t max_version;
507 
508 	unsigned long options;
509 	unsigned long mode;
510 
511 	/* If this callback is not null, it will be called each
512 	 * time a session id is added to the cache.  If this function
513 	 * returns 1, it means that the callback will do a
514 	 * SSL_SESSION_free() when it has finished using it.  Otherwise,
515 	 * on 0, it means the callback has finished with it.
516 	 * If remove_session_cb is not null, it will be called when
517 	 * a session-id is removed from the cache.  After the call,
518 	 * OpenSSL will SSL_SESSION_free() it. */
519 	int (*new_session_cb)(struct ssl_st *ssl, SSL_SESSION *sess);
520 	void (*remove_session_cb)(struct ssl_ctx_st *ctx, SSL_SESSION *sess);
521 	SSL_SESSION *(*get_session_cb)(struct ssl_st *ssl,
522 	    const unsigned char *data, int len, int *copy);
523 
524 	/* if defined, these override the X509_verify_cert() calls */
525 	int (*app_verify_callback)(X509_STORE_CTX *, void *);
526 	    void *app_verify_arg;
527 
528 	/* get client cert callback */
529 	int (*client_cert_cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey);
530 
531 	/* cookie generate callback */
532 	int (*app_gen_cookie_cb)(SSL *ssl, unsigned char *cookie,
533 	    unsigned int *cookie_len);
534 
535 	/* verify cookie callback */
536 	int (*app_verify_cookie_cb)(SSL *ssl, const unsigned char *cookie,
537 	    unsigned int cookie_len);
538 
539 	void (*info_callback)(const SSL *ssl,int type,int val); /* used if SSL's info_callback is NULL */
540 
541 	/* callback that allows applications to peek at protocol messages */
542 	void (*msg_callback)(int write_p, int version, int content_type,
543 	    const void *buf, size_t len, SSL *ssl, void *arg);
544 	void *msg_callback_arg;
545 
546 	int (*default_verify_callback)(int ok,X509_STORE_CTX *ctx); /* called 'verify_callback' in the SSL */
547 
548 	/* Default generate session ID callback. */
549 	GEN_SESSION_CB generate_session_id;
550 
551 	/* TLS extensions servername callback */
552 	int (*tlsext_servername_callback)(SSL*, int *, void *);
553 	void *tlsext_servername_arg;
554 
555 	/* Callback to support customisation of ticket key setting */
556 	int (*tlsext_ticket_key_cb)(SSL *ssl, unsigned char *name,
557 	    unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
558 
559 	/* certificate status request info */
560 	/* Callback for status request */
561 	int (*tlsext_status_cb)(SSL *ssl, void *arg);
562 	void *tlsext_status_arg;
563 
564 	struct lhash_st_SSL_SESSION *sessions;
565 
566 	/* Most session-ids that will be cached, default is
567 	 * SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited. */
568 	unsigned long session_cache_size;
569 	struct ssl_session_st *session_cache_head;
570 	struct ssl_session_st *session_cache_tail;
571 
572 	/* This can have one of 2 values, ored together,
573 	 * SSL_SESS_CACHE_CLIENT,
574 	 * SSL_SESS_CACHE_SERVER,
575 	 * Default is SSL_SESSION_CACHE_SERVER, which means only
576 	 * SSL_accept which cache SSL_SESSIONS. */
577 	int session_cache_mode;
578 
579 	struct {
580 		int sess_connect;	/* SSL new conn - started */
581 		int sess_connect_renegotiate;/* SSL reneg - requested */
582 		int sess_connect_good;	/* SSL new conne/reneg - finished */
583 		int sess_accept;	/* SSL new accept - started */
584 		int sess_accept_renegotiate;/* SSL reneg - requested */
585 		int sess_accept_good;	/* SSL accept/reneg - finished */
586 		int sess_miss;		/* session lookup misses  */
587 		int sess_timeout;	/* reuse attempt on timeouted session */
588 		int sess_cache_full;	/* session removed due to full cache */
589 		int sess_hit;		/* session reuse actually done */
590 		int sess_cb_hit;	/* session-id that was not
591 					 * in the cache was
592 					 * passed back via the callback.  This
593 					 * indicates that the application is
594 					 * supplying session-id's from other
595 					 * processes - spooky :-) */
596 	} stats;
597 
598 	CRYPTO_EX_DATA ex_data;
599 
600 	STACK_OF(SSL_CIPHER) *cipher_list_tls13;
601 
602 	struct cert_st /* CERT */ *cert;
603 
604 	/* Default values used when no per-SSL value is defined follow */
605 
606 	/* what we put in client cert requests */
607 	STACK_OF(X509_NAME) *client_CA;
608 
609 	long max_cert_list;
610 
611 	int read_ahead;
612 
613 	int quiet_shutdown;
614 
615 	/* Maximum amount of data to send in one fragment.
616 	 * actual record size can be more than this due to
617 	 * padding and MAC overheads.
618 	 */
619 	unsigned int max_send_fragment;
620 
621 #ifndef OPENSSL_NO_ENGINE
622 	/* Engine to pass requests for client certs to
623 	 */
624 	ENGINE *client_cert_engine;
625 #endif
626 
627 	/* RFC 4507 session ticket keys */
628 	unsigned char tlsext_tick_key_name[16];
629 	unsigned char tlsext_tick_hmac_key[16];
630 	unsigned char tlsext_tick_aes_key[16];
631 
632 	/* SRTP profiles we are willing to do from RFC 5764 */
633 	STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
634 
635 	/*
636 	 * ALPN information.
637 	 */
638 
639 	/*
640 	 * Server callback function that allows the server to select the
641 	 * protocol for the connection.
642 	 *   out: on successful return, this must point to the raw protocol
643 	 *       name (without the length prefix).
644 	 *   outlen: on successful return, this contains the length of out.
645 	 *   in: points to the client's list of supported protocols in
646 	 *       wire-format.
647 	 *   inlen: the length of in.
648 	 */
649 	int (*alpn_select_cb)(SSL *s, const unsigned char **out,
650 	    unsigned char *outlen, const unsigned char *in, unsigned int inlen,
651 	    void *arg);
652 	void *alpn_select_cb_arg;
653 
654 	/* Client list of supported protocols in wire format. */
655 	unsigned char *alpn_client_proto_list;
656 	unsigned int alpn_client_proto_list_len;
657 
658 	size_t tlsext_ecpointformatlist_length;
659 	uint8_t *tlsext_ecpointformatlist; /* our list */
660 	size_t tlsext_supportedgroups_length;
661 	uint16_t *tlsext_supportedgroups; /* our list */
662 } SSL_CTX_INTERNAL;
663 
664 typedef struct ssl_internal_st {
665 	struct tls13_ctx *tls13;
666 
667 	uint16_t min_version;
668 	uint16_t max_version;
669 
670 	unsigned long options; /* protocol behaviour */
671 	unsigned long mode; /* API behaviour */
672 
673 	/* Client list of supported protocols in wire format. */
674 	unsigned char *alpn_client_proto_list;
675 	unsigned int alpn_client_proto_list_len;
676 
677 	/* XXX Callbacks */
678 
679 	/* true when we are actually in SSL_accept() or SSL_connect() */
680 	int in_handshake;
681 	int (*handshake_func)(SSL *);
682 	/* callback that allows applications to peek at protocol messages */
683 	void (*msg_callback)(int write_p, int version, int content_type,
684 	    const void *buf, size_t len, SSL *ssl, void *arg);
685 	void *msg_callback_arg;
686 
687 	/* Default generate session ID callback. */
688 	GEN_SESSION_CB generate_session_id;
689 
690 	int (*verify_callback)(int ok,X509_STORE_CTX *ctx); /* fail if callback returns 0 */
691 
692 	void (*info_callback)(const SSL *ssl,int type,int val); /* optional informational callback */
693 
694 	/* TLS extension debug callback */
695 	void (*tlsext_debug_cb)(SSL *s, int client_server, int type,
696 	    unsigned char *data, int len, void *arg);
697 	void *tlsext_debug_arg;
698 
699 	/* TLS Session Ticket extension callback */
700 	tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb;
701 	void *tls_session_ticket_ext_cb_arg;
702 
703 	/* TLS pre-shared secret session resumption */
704 	tls_session_secret_cb_fn tls_session_secret_cb;
705 	void *tls_session_secret_cb_arg;
706 
707 	/* XXX non-callback */
708 
709 	int type; /* SSL_ST_CONNECT or SSL_ST_ACCEPT */
710 
711 	/* This holds a variable that indicates what we were doing
712 	 * when a 0 or -1 is returned.  This is needed for
713 	 * non-blocking IO so we know what request needs re-doing when
714 	 * in SSL_accept or SSL_connect */
715 	int rwstate;
716 
717 	/* Imagine that here's a boolean member "init" that is
718 	 * switched as soon as SSL_set_{accept/connect}_state
719 	 * is called for the first time, so that "state" and
720 	 * "handshake_func" are properly initialized.  But as
721 	 * handshake_func is == 0 until then, we use this
722 	 * test instead of an "init" member.
723 	 */
724 
725 	int new_session;/* Generate a new session or reuse an old one.
726 			 * NB: For servers, the 'new' session may actually be a previously
727 			 * cached session or even the previous session unless
728 			 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
729 	int quiet_shutdown;/* don't send shutdown packets */
730 	int shutdown;	/* we have shut things down, 0x01 sent, 0x02
731 			 * for received */
732 	BUF_MEM *init_buf;	/* buffer used during init */
733 	void *init_msg;		/* pointer to handshake message body, set by ssl3_get_message() */
734 	int init_num;		/* amount read/written */
735 	int init_off;		/* amount read/written */
736 
737 	/* used internally to point at a raw packet */
738 	unsigned char *packet;
739 	unsigned int packet_length;
740 
741 	int read_ahead;		/* Read as many input bytes as possible
742 				 * (for non-blocking reads) */
743 
744 	int hit;		/* reusing a previous session */
745 
746 	STACK_OF(SSL_CIPHER) *cipher_list_tls13;
747 
748 	/* These are the ones being used, the ones in SSL_SESSION are
749 	 * the ones to be 'copied' into these ones */
750 	int mac_flags;
751 
752 	SSL_AEAD_CTX *aead_read_ctx;	/* AEAD context. If non-NULL, then
753 					   enc_read_ctx and read_hash are
754 					   ignored. */
755 
756 	SSL_AEAD_CTX *aead_write_ctx;	/* AEAD context. If non-NULL, then
757 					   enc_write_ctx and write_hash are
758 					   ignored. */
759 
760 	EVP_CIPHER_CTX *enc_write_ctx;		/* cryptographic state */
761 	EVP_MD_CTX *write_hash;			/* used for mac generation */
762 
763 	struct tls12_record_layer *rl;
764 
765 	/* session info */
766 
767 	/* extra application data */
768 	CRYPTO_EX_DATA ex_data;
769 
770 	/* client cert? */
771 	/* for server side, keep the list of CA_dn we can use */
772 	STACK_OF(X509_NAME) *client_CA;
773 
774 	/* set this flag to 1 and a sleep(1) is put into all SSL_read()
775 	 * and SSL_write() calls, good for nbio debuging :-) */
776 	int debug;
777 	long max_cert_list;
778 	int first_packet;
779 
780 	/* Expect OCSP CertificateStatus message */
781 	int tlsext_status_expected;
782 	/* OCSP status request only */
783 	STACK_OF(OCSP_RESPID) *tlsext_ocsp_ids;
784 	X509_EXTENSIONS *tlsext_ocsp_exts;
785 
786 	/* OCSP response received or to be sent */
787 	unsigned char *tlsext_ocsp_resp;
788 	size_t tlsext_ocsp_resp_len;
789 
790 	/* RFC4507 session ticket expected to be received or sent */
791 	int tlsext_ticket_expected;
792 
793 	size_t tlsext_ecpointformatlist_length;
794 	uint8_t *tlsext_ecpointformatlist; /* our list */
795 	size_t tlsext_supportedgroups_length;
796 	uint16_t *tlsext_supportedgroups; /* our list */
797 
798 	/* TLS Session Ticket extension override */
799 	TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
800 
801 	STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;	/* What we'll do */
802 	SRTP_PROTECTION_PROFILE *srtp_profile;			/* What's been chosen */
803 
804 	int renegotiate;/* 1 if we are renegotiating.
805 		 	 * 2 if we are a server and are inside a handshake
806 	                 * (i.e. not just sending a HelloRequest) */
807 
808 	int rstate;	/* where we are when reading */
809 
810 	int mac_packet;
811 
812 	int empty_record_count;
813 } SSL_INTERNAL;
814 
815 typedef struct ssl3_record_internal_st {
816 	int type;               /* type of record */
817 	unsigned int length;    /* How many bytes available */
818 	unsigned int padding_length; /* Number of padding bytes. */
819 	unsigned int off;       /* read/write offset into 'buf' */
820 	unsigned char *data;    /* pointer to the record data */
821 	unsigned char *input;   /* where the decode bytes are */
822 	unsigned long epoch;    /* epoch number, needed by DTLS1 */
823 	unsigned char seq_num[8]; /* sequence number, needed by DTLS1 */
824 } SSL3_RECORD_INTERNAL;
825 
826 typedef struct ssl3_buffer_internal_st {
827 	unsigned char *buf;	/* at least SSL3_RT_MAX_PACKET_SIZE bytes,
828 	                         * see ssl3_setup_buffers() */
829 	size_t len;		/* buffer size */
830 	int offset;		/* where to 'copy from' */
831 	int left;		/* how many bytes left */
832 } SSL3_BUFFER_INTERNAL;
833 
834 typedef struct ssl3_state_internal_st {
835 	unsigned char read_sequence[SSL3_SEQUENCE_SIZE];
836 	int read_mac_secret_size;
837 	unsigned char read_mac_secret[EVP_MAX_MD_SIZE];
838 	unsigned char write_sequence[SSL3_SEQUENCE_SIZE];
839 	int write_mac_secret_size;
840 	unsigned char write_mac_secret[EVP_MAX_MD_SIZE];
841 
842 	SSL3_BUFFER_INTERNAL rbuf;	/* read IO goes into here */
843 	SSL3_BUFFER_INTERNAL wbuf;	/* write IO goes into here */
844 
845 	/* we allow one fatal and one warning alert to be outstanding,
846 	 * send close alert via the warning alert */
847 	int alert_dispatch;
848 	unsigned char send_alert[2];
849 
850 	/* flags for countermeasure against known-IV weakness */
851 	int need_empty_fragments;
852 	int empty_fragment_done;
853 
854 	SSL3_RECORD_INTERNAL rrec;	/* each decoded record goes in here */
855 
856 	/* storage for Alert/Handshake protocol data received but not
857 	 * yet processed by ssl3_read_bytes: */
858 	unsigned char alert_fragment[2];
859 	unsigned int alert_fragment_len;
860 	unsigned char handshake_fragment[4];
861 	unsigned int handshake_fragment_len;
862 
863 	/* partial write - check the numbers match */
864 	unsigned int wnum;	/* number of bytes sent so far */
865 	int wpend_tot;		/* number bytes written */
866 	int wpend_type;
867 	int wpend_ret;		/* number of bytes submitted */
868 	const unsigned char *wpend_buf;
869 
870 	/* Transcript of handshake messages that have been sent and received. */
871 	BUF_MEM *handshake_transcript;
872 
873 	/* Rolling hash of handshake messages. */
874 	EVP_MD_CTX *handshake_hash;
875 
876 	/* this is set whenerver we see a change_cipher_spec message
877 	 * come in when we are not looking for one */
878 	int change_cipher_spec;
879 
880 	int warn_alert;
881 	int fatal_alert;
882 
883 	/* This flag is set when we should renegotiate ASAP, basically when
884 	 * there is no more data in the read or write buffers */
885 	int renegotiate;
886 	int total_renegotiations;
887 	int num_renegotiations;
888 
889 	int in_read_app_data;
890 
891 	SSL_HANDSHAKE hs;
892 	SSL_HANDSHAKE_TLS13 hs_tls13;
893 
894 	struct	{
895 		int new_mac_secret_size;
896 
897 		/* actually only needs to be 16+20 */
898 		unsigned char cert_verify_md[EVP_MAX_MD_SIZE*2];
899 
900 		/* actually only need to be 16+20 for SSLv3 and 12 for TLS */
901 		unsigned char finish_md[EVP_MAX_MD_SIZE*2];
902 		int finish_md_len;
903 		unsigned char peer_finish_md[EVP_MAX_MD_SIZE*2];
904 		int peer_finish_md_len;
905 
906 		unsigned long message_size;
907 		int message_type;
908 
909 		DH *dh;
910 
911 		EC_KEY *ecdh; /* holds short lived ECDH key */
912 		int ecdh_nid;
913 
914 		uint8_t *x25519;
915 
916 		int reuse_message;
917 
918 		/* used for certificate requests */
919 		int cert_req;
920 		int ctype_num;
921 		char ctype[SSL3_CT_NUMBER];
922 		STACK_OF(X509_NAME) *ca_names;
923 
924 		const EVP_CIPHER *new_sym_enc;
925 		const EVP_AEAD *new_aead;
926 		const EVP_MD *new_hash;
927 		int new_mac_pkey_type;
928 		int cert_request;
929 	} tmp;
930 
931 	/* Connection binding to prevent renegotiation attacks */
932 	unsigned char previous_client_finished[EVP_MAX_MD_SIZE];
933 	unsigned char previous_client_finished_len;
934 	unsigned char previous_server_finished[EVP_MAX_MD_SIZE];
935 	unsigned char previous_server_finished_len;
936 	int send_connection_binding; /* TODOEKR */
937 
938 	/* Set if we saw a Renegotiation Indication extension from our peer. */
939 	int renegotiate_seen;
940 
941 	/*
942 	 * ALPN information.
943 	 *
944 	 * In a server these point to the selected ALPN protocol after the
945 	 * ClientHello has been processed. In a client these contain the
946 	 * protocol that the server selected once the ServerHello has been
947 	 * processed.
948 	 */
949 	unsigned char *alpn_selected;
950 	size_t alpn_selected_len;
951 } SSL3_STATE_INTERNAL;
952 #define S3I(s) (s->s3->internal)
953 
954 typedef struct dtls1_record_data_internal_st {
955 	unsigned char *packet;
956 	unsigned int packet_length;
957 	SSL3_BUFFER_INTERNAL rbuf;
958 	SSL3_RECORD_INTERNAL rrec;
959 } DTLS1_RECORD_DATA_INTERNAL;
960 
961 typedef struct dtls1_state_internal_st {
962 	unsigned int send_cookie;
963 	unsigned char cookie[DTLS1_COOKIE_LENGTH];
964 	unsigned char rcvd_cookie[DTLS1_COOKIE_LENGTH];
965 	unsigned int cookie_len;
966 
967 	/*
968 	 * The current data and handshake epoch.  This is initially
969 	 * undefined, and starts at zero once the initial handshake is
970 	 * completed
971 	 */
972 	unsigned short r_epoch;
973 	unsigned short w_epoch;
974 
975 	/* records being received in the current epoch */
976 	DTLS1_BITMAP bitmap;
977 
978 	/* renegotiation starts a new set of sequence numbers */
979 	DTLS1_BITMAP next_bitmap;
980 
981 	/* handshake message numbers */
982 	unsigned short handshake_write_seq;
983 	unsigned short next_handshake_write_seq;
984 
985 	unsigned short handshake_read_seq;
986 
987 	/* save last sequence number for retransmissions */
988 	unsigned char last_write_sequence[SSL3_SEQUENCE_SIZE];
989 
990 	/* Received handshake records (processed and unprocessed) */
991 	record_pqueue unprocessed_rcds;
992 	record_pqueue processed_rcds;
993 
994 	/* Buffered handshake messages */
995 	struct _pqueue *buffered_messages;
996 
997 	/* Buffered application records.
998 	 * Only for records between CCS and Finished
999 	 * to prevent either protocol violation or
1000 	 * unnecessary message loss.
1001 	 */
1002 	record_pqueue buffered_app_data;
1003 
1004 	/* Is set when listening for new connections with dtls1_listen() */
1005 	unsigned int listen;
1006 
1007 	unsigned int mtu; /* max DTLS packet size */
1008 
1009 	struct hm_header_st w_msg_hdr;
1010 	struct hm_header_st r_msg_hdr;
1011 
1012 	struct dtls1_timeout_st timeout;
1013 
1014 	/* storage for Alert/Handshake protocol data received but not
1015 	 * yet processed by ssl3_read_bytes: */
1016 	unsigned char alert_fragment[DTLS1_AL_HEADER_LENGTH];
1017 	unsigned int alert_fragment_len;
1018 	unsigned char handshake_fragment[DTLS1_HM_HEADER_LENGTH];
1019 	unsigned int handshake_fragment_len;
1020 
1021 	unsigned int retransmitting;
1022 	unsigned int change_cipher_spec_ok;
1023 } DTLS1_STATE_INTERNAL;
1024 #define D1I(s) (s->d1->internal)
1025 
1026 typedef struct cert_st {
1027 	/* Current active set */
1028 	CERT_PKEY *key; /* ALWAYS points to an element of the pkeys array
1029 			 * Probably it would make more sense to store
1030 			 * an index, not a pointer. */
1031 
1032 	/* The following masks are for the key and auth
1033 	 * algorithms that are supported by the certs below */
1034 	int valid;
1035 	unsigned long mask_k;
1036 	unsigned long mask_a;
1037 
1038 	DH *dh_tmp;
1039 	DH *(*dh_tmp_cb)(SSL *ssl, int is_export, int keysize);
1040 	int dh_tmp_auto;
1041 
1042 	CERT_PKEY pkeys[SSL_PKEY_NUM];
1043 
1044 	int references; /* >1 only if SSL_copy_session_id is used */
1045 } CERT;
1046 
1047 
1048 typedef struct sess_cert_st {
1049 	STACK_OF(X509) *cert_chain; /* as received from peer */
1050 
1051 	/* The 'peer_...' members are used only by clients. */
1052 	int peer_cert_type;
1053 
1054 	CERT_PKEY *peer_key; /* points to an element of peer_pkeys (never NULL!) */
1055 	CERT_PKEY peer_pkeys[SSL_PKEY_NUM];
1056 	/* Obviously we don't have the private keys of these,
1057 	 * so maybe we shouldn't even use the CERT_PKEY type here. */
1058 
1059 	int peer_nid;
1060 	DH *peer_dh_tmp;
1061 	EC_KEY *peer_ecdh_tmp;
1062 	uint8_t *peer_x25519_tmp;
1063 
1064 	int references; /* actually always 1 at the moment */
1065 } SESS_CERT;
1066 
1067 /*#define SSL_DEBUG	*/
1068 /*#define RSA_DEBUG	*/
1069 
1070 typedef struct ssl3_enc_method {
1071 	unsigned int enc_flags;
1072 } SSL3_ENC_METHOD;
1073 
1074 /*
1075  * Flag values for enc_flags.
1076  */
1077 
1078 /* Uses explicit IV. */
1079 #define SSL_ENC_FLAG_EXPLICIT_IV        (1 << 0)
1080 
1081 /* Uses signature algorithms extension. */
1082 #define SSL_ENC_FLAG_SIGALGS            (1 << 1)
1083 
1084 /* Uses SHA256 default PRF. */
1085 #define SSL_ENC_FLAG_SHA256_PRF         (1 << 2)
1086 
1087 /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2. */
1088 #define SSL_ENC_FLAG_TLS1_2_CIPHERS     (1 << 4)
1089 
1090 /* Allow TLS 1.3 ciphersuites only. */
1091 #define SSL_ENC_FLAG_TLS1_3_CIPHERS     (1 << 5)
1092 
1093 /*
1094  * ssl_aead_ctx_st contains information about an AEAD that is being used to
1095  * encrypt an SSL connection.
1096  */
1097 struct ssl_aead_ctx_st {
1098 	EVP_AEAD_CTX ctx;
1099 	/*
1100 	 * fixed_nonce contains any bytes of the nonce that are fixed for all
1101 	 * records.
1102 	 */
1103 	unsigned char fixed_nonce[12];
1104 	unsigned char fixed_nonce_len;
1105 	unsigned char variable_nonce_len;
1106 	unsigned char xor_fixed_nonce;
1107 	unsigned char tag_len;
1108 	/*
1109 	 * variable_nonce_in_record is non-zero if the variable nonce
1110 	 * for a record is included as a prefix before the ciphertext.
1111 	 */
1112 	char variable_nonce_in_record;
1113 };
1114 
1115 extern SSL_CIPHER ssl3_ciphers[];
1116 
1117 const char *ssl_version_string(int ver);
1118 int ssl_enabled_version_range(SSL *s, uint16_t *min_ver, uint16_t *max_ver);
1119 int ssl_supported_version_range(SSL *s, uint16_t *min_ver, uint16_t *max_ver);
1120 int ssl_max_shared_version(SSL *s, uint16_t peer_ver, uint16_t *max_ver);
1121 int ssl_version_set_min(const SSL_METHOD *meth, uint16_t ver, uint16_t max_ver,
1122     uint16_t *out_ver);
1123 int ssl_version_set_max(const SSL_METHOD *meth, uint16_t ver, uint16_t min_ver,
1124     uint16_t *out_ver);
1125 int ssl_downgrade_max_version(SSL *s, uint16_t *max_ver);
1126 int ssl_cipher_in_list(STACK_OF(SSL_CIPHER) *ciphers, const SSL_CIPHER *cipher);
1127 int ssl_cipher_allowed_in_version_range(const SSL_CIPHER *cipher,
1128     uint16_t min_ver, uint16_t max_ver);
1129 
1130 const SSL_METHOD *tls_legacy_method(void);
1131 const SSL_METHOD *tls_legacy_client_method(void);
1132 const SSL_METHOD *tls_legacy_server_method(void);
1133 
1134 const SSL_METHOD *ssl_get_client_method(uint16_t version);
1135 const SSL_METHOD *ssl_get_server_method(uint16_t version);
1136 
1137 extern SSL3_ENC_METHOD DTLSv1_enc_data;
1138 extern SSL3_ENC_METHOD TLSv1_enc_data;
1139 extern SSL3_ENC_METHOD TLSv1_1_enc_data;
1140 extern SSL3_ENC_METHOD TLSv1_2_enc_data;
1141 extern SSL3_ENC_METHOD TLSv1_3_enc_data;
1142 
1143 void ssl_clear_cipher_state(SSL *s);
1144 void ssl_clear_cipher_read_state(SSL *s);
1145 void ssl_clear_cipher_write_state(SSL *s);
1146 int ssl_clear_bad_session(SSL *s);
1147 
1148 CERT *ssl_cert_new(void);
1149 CERT *ssl_cert_dup(CERT *cert);
1150 void ssl_cert_free(CERT *c);
1151 int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain);
1152 int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain);
1153 int ssl_cert_add0_chain_cert(CERT *c, X509 *cert);
1154 int ssl_cert_add1_chain_cert(CERT *c, X509 *cert);
1155 
1156 SESS_CERT *ssl_sess_cert_new(void);
1157 void ssl_sess_cert_free(SESS_CERT *sc);
1158 int ssl_get_new_session(SSL *s, int session);
1159 int ssl_get_prev_session(SSL *s, CBS *session_id, CBS *ext_block,
1160     int *alert);
1161 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b);
1162 SSL_CIPHER *OBJ_bsearch_ssl_cipher_id(SSL_CIPHER *key, SSL_CIPHER const *base,
1163     int num);
1164 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *ciphers, CBB *cbb);
1165 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, CBS *cbs);
1166 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *meth,
1167     STACK_OF(SSL_CIPHER) **pref, STACK_OF(SSL_CIPHER) *tls13,
1168     const char *rule_str);
1169 int ssl_parse_ciphersuites(STACK_OF(SSL_CIPHER) **out_ciphers, const char *str);
1170 int ssl_merge_cipherlists(STACK_OF(SSL_CIPHER) *cipherlist,
1171     STACK_OF(SSL_CIPHER) *cipherlist_tls13,
1172     STACK_OF(SSL_CIPHER) **out_cipherlist);
1173 void ssl_update_cache(SSL *s, int mode);
1174 int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
1175     const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size);
1176 int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead);
1177 int ssl_get_handshake_evp_md(SSL *s, const EVP_MD **md);
1178 
1179 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk);
1180 int ssl_undefined_function(SSL *s);
1181 int ssl_undefined_void_function(void);
1182 int ssl_undefined_const_function(const SSL *s);
1183 CERT_PKEY *ssl_get_server_send_pkey(const SSL *s);
1184 EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *c, const EVP_MD **pmd,
1185     const struct ssl_sigalg **sap);
1186 DH *ssl_get_auto_dh(SSL *s);
1187 int ssl_cert_type(X509 *x, EVP_PKEY *pkey);
1188 void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher);
1189 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s);
1190 int ssl_has_ecc_ciphers(SSL *s);
1191 int ssl_verify_alarm_type(long type);
1192 
1193 int SSL_SESSION_ticket(SSL_SESSION *ss, unsigned char **out, size_t *out_len);
1194 
1195 const SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p);
1196 int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p);
1197 int ssl3_send_server_certificate(SSL *s);
1198 int ssl3_send_newsession_ticket(SSL *s);
1199 int ssl3_send_cert_status(SSL *s);
1200 int ssl3_get_finished(SSL *s, int state_a, int state_b);
1201 int ssl3_send_change_cipher_spec(SSL *s, int state_a, int state_b);
1202 int ssl3_do_write(SSL *s, int type);
1203 int ssl3_send_alert(SSL *s, int level, int desc);
1204 int ssl3_get_req_cert_types(SSL *s, CBB *cbb);
1205 long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok);
1206 int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen);
1207 int ssl3_num_ciphers(void);
1208 const SSL_CIPHER *ssl3_get_cipher(unsigned int u);
1209 const SSL_CIPHER *ssl3_get_cipher_by_id(unsigned int id);
1210 const SSL_CIPHER *ssl3_get_cipher_by_value(uint16_t value);
1211 uint16_t ssl3_cipher_get_value(const SSL_CIPHER *c);
1212 int ssl3_renegotiate(SSL *ssl);
1213 
1214 int ssl3_renegotiate_check(SSL *ssl);
1215 
1216 int ssl3_dispatch_alert(SSL *s);
1217 int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
1218 int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
1219 int ssl3_output_cert_chain(SSL *s, CBB *cbb, CERT_PKEY *cpk);
1220 SSL_CIPHER *ssl3_choose_cipher(SSL *ssl, STACK_OF(SSL_CIPHER) *clnt,
1221     STACK_OF(SSL_CIPHER) *srvr);
1222 int	ssl3_setup_buffers(SSL *s);
1223 int	ssl3_setup_init_buffer(SSL *s);
1224 void ssl3_release_init_buffer(SSL *s);
1225 int	ssl3_setup_read_buffer(SSL *s);
1226 int	ssl3_setup_write_buffer(SSL *s);
1227 void ssl3_release_buffer(SSL3_BUFFER_INTERNAL *b);
1228 void ssl3_release_read_buffer(SSL *s);
1229 void ssl3_release_write_buffer(SSL *s);
1230 int	ssl3_new(SSL *s);
1231 void	ssl3_free(SSL *s);
1232 int	ssl3_accept(SSL *s);
1233 int	ssl3_connect(SSL *s);
1234 int	ssl3_read(SSL *s, void *buf, int len);
1235 int	ssl3_peek(SSL *s, void *buf, int len);
1236 int	ssl3_write(SSL *s, const void *buf, int len);
1237 int	ssl3_shutdown(SSL *s);
1238 void	ssl3_clear(SSL *s);
1239 long	ssl3_ctrl(SSL *s, int cmd, long larg, void *parg);
1240 long	ssl3_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg);
1241 long	ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)(void));
1242 long	ssl3_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp)(void));
1243 int	ssl3_pending(const SSL *s);
1244 
1245 int ssl3_handshake_msg_hdr_len(SSL *s);
1246 int ssl3_handshake_msg_start(SSL *s, CBB *handshake, CBB *body,
1247     uint8_t msg_type);
1248 int ssl3_handshake_msg_finish(SSL *s, CBB *handshake);
1249 int ssl3_handshake_write(SSL *s);
1250 int ssl3_record_write(SSL *s, int type);
1251 
1252 void tls1_record_sequence_increment(unsigned char *seq);
1253 int ssl3_do_change_cipher_spec(SSL *ssl);
1254 
1255 int dtls1_do_write(SSL *s, int type);
1256 int ssl3_packet_read(SSL *s, int plen);
1257 int ssl3_packet_extend(SSL *s, int plen);
1258 int ssl_server_legacy_first_packet(SSL *s);
1259 int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
1260 int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
1261     unsigned int len);
1262 void dtls1_set_message_header(SSL *s, unsigned char mt, unsigned long len,
1263     unsigned long frag_off, unsigned long frag_len);
1264 void dtls1_set_message_header_int(SSL *s, unsigned char mt,
1265     unsigned long len, unsigned short seq_num, unsigned long frag_off,
1266     unsigned long frag_len);
1267 
1268 int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf, int len);
1269 int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
1270 
1271 int dtls1_read_failed(SSL *s, int code);
1272 int dtls1_buffer_message(SSL *s, int ccs);
1273 int dtls1_retransmit_message(SSL *s, unsigned short seq,
1274     unsigned long frag_off, int *found);
1275 int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
1276 int dtls1_retransmit_buffered_messages(SSL *s);
1277 void dtls1_clear_record_buffer(SSL *s);
1278 int dtls1_get_message_header(unsigned char *data,
1279     struct hm_header_st *msg_hdr);
1280 void dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr);
1281 void dtls1_reset_seq_numbers(SSL *s, int rw);
1282 void dtls1_build_sequence_number(unsigned char *dst, unsigned char *seq,
1283     unsigned short epoch);
1284 struct timeval* dtls1_get_timeout(SSL *s, struct timeval* timeleft);
1285 int dtls1_check_timeout_num(SSL *s);
1286 int dtls1_handle_timeout(SSL *s);
1287 const SSL_CIPHER *dtls1_get_cipher(unsigned int u);
1288 void dtls1_start_timer(SSL *s);
1289 void dtls1_stop_timer(SSL *s);
1290 int dtls1_is_timer_expired(SSL *s);
1291 void dtls1_double_timeout(SSL *s);
1292 unsigned int dtls1_min_mtu(void);
1293 
1294 /* some client-only functions */
1295 int dtls1_get_hello_verify(SSL *s);
1296 int ssl3_send_client_hello(SSL *s);
1297 int ssl3_get_server_hello(SSL *s);
1298 int ssl3_get_certificate_request(SSL *s);
1299 int ssl3_get_new_session_ticket(SSL *s);
1300 int ssl3_get_cert_status(SSL *s);
1301 int ssl3_get_server_done(SSL *s);
1302 int ssl3_send_client_verify(SSL *s);
1303 int ssl3_send_client_certificate(SSL *s);
1304 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
1305 int ssl3_send_client_key_exchange(SSL *s);
1306 int ssl3_get_server_key_exchange(SSL *s);
1307 int ssl3_get_server_certificate(SSL *s);
1308 int ssl3_check_cert_and_algorithm(SSL *s);
1309 int ssl3_check_finished(SSL *s);
1310 
1311 /* some server-only functions */
1312 int dtls1_send_hello_verify_request(SSL *s);
1313 int ssl3_get_client_hello(SSL *s);
1314 int ssl3_send_server_hello(SSL *s);
1315 int ssl3_send_hello_request(SSL *s);
1316 int ssl3_send_server_key_exchange(SSL *s);
1317 int ssl3_send_certificate_request(SSL *s);
1318 int ssl3_send_server_done(SSL *s);
1319 int ssl3_get_client_certificate(SSL *s);
1320 int ssl3_get_client_key_exchange(SSL *s);
1321 int ssl3_get_cert_verify(SSL *s);
1322 
1323 int ssl_kex_dummy_ecdhe_x25519(EVP_PKEY *pkey);
1324 int ssl_kex_generate_ecdhe_ecp(EC_KEY *ecdh, int nid);
1325 int ssl_kex_public_ecdhe_ecp(EC_KEY *ecdh, CBB *cbb);
1326 int ssl_kex_peer_public_ecdhe_ecp(EC_KEY *ecdh, int nid, CBS *cbs);
1327 int ssl_kex_derive_ecdhe_ecp(EC_KEY *ecdh, EC_KEY *ecdh_peer,
1328     uint8_t **shared_key, size_t *shared_key_len);
1329 
1330 int tls1_new(SSL *s);
1331 void tls1_free(SSL *s);
1332 void tls1_clear(SSL *s);
1333 
1334 int dtls1_new(SSL *s);
1335 void dtls1_free(SSL *s);
1336 void dtls1_clear(SSL *s);
1337 long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg);
1338 
1339 long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok);
1340 int dtls1_get_record(SSL *s);
1341 int dtls1_dispatch_alert(SSL *s);
1342 
1343 int ssl_init_wbio_buffer(SSL *s, int push);
1344 void ssl_free_wbio_buffer(SSL *s);
1345 
1346 int tls1_transcript_hash_init(SSL *s);
1347 int tls1_transcript_hash_update(SSL *s, const unsigned char *buf, size_t len);
1348 int tls1_transcript_hash_value(SSL *s, const unsigned char *out, size_t len,
1349     size_t *outlen);
1350 void tls1_transcript_hash_free(SSL *s);
1351 
1352 int tls1_transcript_init(SSL *s);
1353 void tls1_transcript_free(SSL *s);
1354 void tls1_transcript_reset(SSL *s);
1355 int tls1_transcript_append(SSL *s, const unsigned char *buf, size_t len);
1356 int tls1_transcript_data(SSL *s, const unsigned char **data, size_t *len);
1357 void tls1_transcript_freeze(SSL *s);
1358 void tls1_transcript_unfreeze(SSL *s);
1359 int tls1_transcript_record(SSL *s, const unsigned char *buf, size_t len);
1360 
1361 void tls1_cleanup_key_block(SSL *s);
1362 int tls1_change_cipher_state(SSL *s, int which);
1363 int tls1_setup_key_block(SSL *s);
1364 int tls1_enc(SSL *s, int snd);
1365 int tls1_final_finish_mac(SSL *s, const char *str, int slen, unsigned char *p);
1366 int tls1_mac(SSL *ssl, unsigned char *md, int snd);
1367 int tls1_generate_master_secret(SSL *s, unsigned char *out,
1368     unsigned char *p, int len);
1369 int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1370     const char *label, size_t llen, const unsigned char *p, size_t plen,
1371     int use_context);
1372 int tls1_alert_code(int code);
1373 int ssl_ok(SSL *s);
1374 
1375 int ssl_using_ecc_cipher(SSL *s);
1376 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s);
1377 
1378 void tls1_get_formatlist(SSL *s, int client_formats, const uint8_t **pformats,
1379     size_t *pformatslen);
1380 void tls1_get_group_list(SSL *s, int client_groups, const uint16_t **pgroups,
1381     size_t *pgroupslen);
1382 
1383 int tls1_set_groups(uint16_t **out_group_ids, size_t *out_group_ids_len,
1384     const int *groups, size_t ngroups);
1385 int tls1_set_group_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
1386     const char *groups);
1387 
1388 int tls1_ec_curve_id2nid(const uint16_t curve_id);
1389 uint16_t tls1_ec_nid2curve_id(const int nid);
1390 int tls1_check_curve(SSL *s, const uint16_t group_id);
1391 int tls1_get_shared_curve(SSL *s);
1392 
1393 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **data,
1394     size_t n, int *al);
1395 int ssl_check_clienthello_tlsext_early(SSL *s);
1396 int ssl_check_clienthello_tlsext_late(SSL *s);
1397 int ssl_check_serverhello_tlsext(SSL *s);
1398 
1399 #define TLS1_TICKET_FATAL_ERROR		-1
1400 #define TLS1_TICKET_NONE		 0
1401 #define TLS1_TICKET_EMPTY		 1
1402 #define TLS1_TICKET_NOT_DECRYPTED	 2
1403 #define TLS1_TICKET_DECRYPTED		 3
1404 
1405 int tls1_process_ticket(SSL *s, CBS *ext_block, int *alert, SSL_SESSION **ret);
1406 
1407 long ssl_get_algorithm2(SSL *s);
1408 
1409 int tls1_check_ec_server_key(SSL *s);
1410 
1411 /* s3_cbc.c */
1412 void ssl3_cbc_copy_mac(unsigned char *out, const SSL3_RECORD_INTERNAL *rec,
1413     unsigned int md_size, unsigned int orig_len);
1414 int tls1_cbc_remove_padding(const SSL *s, SSL3_RECORD_INTERNAL *rec,
1415     unsigned int block_size, unsigned int mac_size);
1416 char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
1417 int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx, unsigned char *md_out,
1418     size_t *md_out_size, const unsigned char header[13],
1419     const unsigned char *data, size_t data_plus_mac_size,
1420     size_t data_plus_mac_plus_padding_size, const unsigned char *mac_secret,
1421     unsigned int mac_secret_length);
1422 int SSL_state_func_code(int _state);
1423 
1424 #define SSLerror(s, r) SSL_error_internal(s, r, __FILE__, __LINE__)
1425 #define SSLerrorx(r) ERR_PUT_error(ERR_LIB_SSL,(0xfff),(r),__FILE__,__LINE__)
1426 void SSL_error_internal(const SSL *s, int r, char *f, int l);
1427 
1428 #ifndef OPENSSL_NO_SRTP
1429 
1430 int srtp_find_profile_by_name(char *profile_name,
1431     SRTP_PROTECTION_PROFILE **pptr, unsigned int len);
1432 int srtp_find_profile_by_num(unsigned int profile_num,
1433     SRTP_PROTECTION_PROFILE **pptr);
1434 
1435 #endif /* OPENSSL_NO_SRTP */
1436 
1437 __END_HIDDEN_DECLS
1438 
1439 #endif
1440