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