xref: /openssl/ssl/ssl_local.h (revision ef39dd05)
1 /*
2  * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  * Copyright 2005 Nokia. All rights reserved.
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11 
12 #ifndef OSSL_SSL_LOCAL_H
13 # define OSSL_SSL_LOCAL_H
14 
15 # include <stdlib.h>
16 # include <time.h>
17 # include <errno.h>
18 # include "internal/common.h" /* for HAS_PREFIX */
19 
20 # include <openssl/buffer.h>
21 # include <openssl/bio.h>
22 # include <openssl/comp.h>
23 # include <openssl/dsa.h>
24 # include <openssl/err.h>
25 # include <openssl/ssl.h>
26 # include <openssl/async.h>
27 # include <openssl/symhacks.h>
28 # include <openssl/ct.h>
29 # include "internal/recordmethod.h"
30 # include "internal/statem.h"
31 # include "internal/packet.h"
32 # include "internal/dane.h"
33 # include "internal/refcount.h"
34 # include "internal/tsan_assist.h"
35 # include "internal/bio.h"
36 # include "internal/ktls.h"
37 # include "internal/time.h"
38 # include "internal/ssl.h"
39 # include "internal/cryptlib.h"
40 # include "record/record.h"
41 
42 # ifdef OPENSSL_BUILD_SHLIBSSL
43 #  undef OPENSSL_EXTERN
44 #  define OPENSSL_EXTERN OPENSSL_EXPORT
45 # endif
46 
47 # define TLS_MAX_VERSION_INTERNAL TLS1_3_VERSION
48 # define DTLS_MAX_VERSION_INTERNAL DTLS1_2_VERSION
49 
50 /*
51  * DTLS version numbers are strange because they're inverted. Except for
52  * DTLS1_BAD_VER, which should be considered "lower" than the rest.
53  */
54 # define dtls_ver_ordinal(v1) (((v1) == DTLS1_BAD_VER) ? 0xff00 : (v1))
55 # define DTLS_VERSION_GT(v1, v2) (dtls_ver_ordinal(v1) < dtls_ver_ordinal(v2))
56 # define DTLS_VERSION_GE(v1, v2) (dtls_ver_ordinal(v1) <= dtls_ver_ordinal(v2))
57 # define DTLS_VERSION_LT(v1, v2) (dtls_ver_ordinal(v1) > dtls_ver_ordinal(v2))
58 # define DTLS_VERSION_LE(v1, v2) (dtls_ver_ordinal(v1) >= dtls_ver_ordinal(v2))
59 
60 # define SSL_AD_NO_ALERT    -1
61 
62 /*
63  * Define the Bitmasks for SSL_CIPHER.algorithms.
64  * This bits are used packed as dense as possible. If new methods/ciphers
65  * etc will be added, the bits a likely to change, so this information
66  * is for internal library use only, even though SSL_CIPHER.algorithms
67  * can be publicly accessed.
68  * Use the according functions for cipher management instead.
69  *
70  * The bit mask handling in the selection and sorting scheme in
71  * ssl_create_cipher_list() has only limited capabilities, reflecting
72  * that the different entities within are mutually exclusive:
73  * ONLY ONE BIT PER MASK CAN BE SET AT A TIME.
74  */
75 
76 /* Bits for algorithm_mkey (key exchange algorithm) */
77 /* RSA key exchange */
78 # define SSL_kRSA                0x00000001U
79 /* tmp DH key no DH cert */
80 # define SSL_kDHE                0x00000002U
81 /* ephemeral ECDH */
82 # define SSL_kECDHE              0x00000004U
83 /* PSK */
84 # define SSL_kPSK                0x00000008U
85 /* GOST key exchange */
86 # define SSL_kGOST               0x00000010U
87 /* SRP */
88 # define SSL_kSRP                0x00000020U
89 
90 # define SSL_kRSAPSK             0x00000040U
91 # define SSL_kECDHEPSK           0x00000080U
92 # define SSL_kDHEPSK             0x00000100U
93 /* GOST KDF key exchange, draft-smyshlyaev-tls12-gost-suites */
94 # define SSL_kGOST18             0x00000200U
95 
96 /* all PSK */
97 
98 # define SSL_PSK     (SSL_kPSK | SSL_kRSAPSK | SSL_kECDHEPSK | SSL_kDHEPSK)
99 
100 /* Any appropriate key exchange algorithm (for TLS 1.3 ciphersuites) */
101 # define SSL_kANY                0x00000000U
102 
103 /* Bits for algorithm_auth (server authentication) */
104 /* RSA auth */
105 # define SSL_aRSA                0x00000001U
106 /* DSS auth */
107 # define SSL_aDSS                0x00000002U
108 /* no auth (i.e. use ADH or AECDH) */
109 # define SSL_aNULL               0x00000004U
110 /* ECDSA auth*/
111 # define SSL_aECDSA              0x00000008U
112 /* PSK auth */
113 # define SSL_aPSK                0x00000010U
114 /* GOST R 34.10-2001 signature auth */
115 # define SSL_aGOST01             0x00000020U
116 /* SRP auth */
117 # define SSL_aSRP                0x00000040U
118 /* GOST R 34.10-2012 signature auth */
119 # define SSL_aGOST12             0x00000080U
120 /* Any appropriate signature auth (for TLS 1.3 ciphersuites) */
121 # define SSL_aANY                0x00000000U
122 /* All bits requiring a certificate */
123 #define SSL_aCERT \
124     (SSL_aRSA | SSL_aDSS | SSL_aECDSA | SSL_aGOST01 | SSL_aGOST12)
125 
126 /* Bits for algorithm_enc (symmetric encryption) */
127 # define SSL_DES                 0x00000001U
128 # define SSL_3DES                0x00000002U
129 # define SSL_RC4                 0x00000004U
130 # define SSL_RC2                 0x00000008U
131 # define SSL_IDEA                0x00000010U
132 # define SSL_eNULL               0x00000020U
133 # define SSL_AES128              0x00000040U
134 # define SSL_AES256              0x00000080U
135 # define SSL_CAMELLIA128         0x00000100U
136 # define SSL_CAMELLIA256         0x00000200U
137 # define SSL_eGOST2814789CNT     0x00000400U
138 # define SSL_SEED                0x00000800U
139 # define SSL_AES128GCM           0x00001000U
140 # define SSL_AES256GCM           0x00002000U
141 # define SSL_AES128CCM           0x00004000U
142 # define SSL_AES256CCM           0x00008000U
143 # define SSL_AES128CCM8          0x00010000U
144 # define SSL_AES256CCM8          0x00020000U
145 # define SSL_eGOST2814789CNT12   0x00040000U
146 # define SSL_CHACHA20POLY1305    0x00080000U
147 # define SSL_ARIA128GCM          0x00100000U
148 # define SSL_ARIA256GCM          0x00200000U
149 # define SSL_MAGMA               0x00400000U
150 # define SSL_KUZNYECHIK          0x00800000U
151 
152 # define SSL_AESGCM              (SSL_AES128GCM | SSL_AES256GCM)
153 # define SSL_AESCCM              (SSL_AES128CCM | SSL_AES256CCM | SSL_AES128CCM8 | SSL_AES256CCM8)
154 # define SSL_AES                 (SSL_AES128|SSL_AES256|SSL_AESGCM|SSL_AESCCM)
155 # define SSL_CAMELLIA            (SSL_CAMELLIA128|SSL_CAMELLIA256)
156 # define SSL_CHACHA20            (SSL_CHACHA20POLY1305)
157 # define SSL_ARIAGCM             (SSL_ARIA128GCM | SSL_ARIA256GCM)
158 # define SSL_ARIA                (SSL_ARIAGCM)
159 # define SSL_CBC                 (SSL_DES | SSL_3DES | SSL_RC2 | SSL_IDEA \
160                                   | SSL_AES128 | SSL_AES256 | SSL_CAMELLIA128 \
161                                   | SSL_CAMELLIA256 | SSL_SEED)
162 
163 /* Bits for algorithm_mac (symmetric authentication) */
164 
165 # define SSL_MD5                 0x00000001U
166 # define SSL_SHA1                0x00000002U
167 # define SSL_GOST94      0x00000004U
168 # define SSL_GOST89MAC   0x00000008U
169 # define SSL_SHA256              0x00000010U
170 # define SSL_SHA384              0x00000020U
171 /* Not a real MAC, just an indication it is part of cipher */
172 # define SSL_AEAD                0x00000040U
173 # define SSL_GOST12_256          0x00000080U
174 # define SSL_GOST89MAC12         0x00000100U
175 # define SSL_GOST12_512          0x00000200U
176 # define SSL_MAGMAOMAC           0x00000400U
177 # define SSL_KUZNYECHIKOMAC      0x00000800U
178 
179 /*
180  * When adding new digest in the ssl_ciph.c and increment SSL_MD_NUM_IDX make
181  * sure to update this constant too
182  */
183 
184 # define SSL_MD_MD5_IDX  0
185 # define SSL_MD_SHA1_IDX 1
186 # define SSL_MD_GOST94_IDX 2
187 # define SSL_MD_GOST89MAC_IDX 3
188 # define SSL_MD_SHA256_IDX 4
189 # define SSL_MD_SHA384_IDX 5
190 # define SSL_MD_GOST12_256_IDX  6
191 # define SSL_MD_GOST89MAC12_IDX 7
192 # define SSL_MD_GOST12_512_IDX  8
193 # define SSL_MD_MD5_SHA1_IDX 9
194 # define SSL_MD_SHA224_IDX 10
195 # define SSL_MD_SHA512_IDX 11
196 # define SSL_MD_MAGMAOMAC_IDX 12
197 # define SSL_MD_KUZNYECHIKOMAC_IDX 13
198 # define SSL_MAX_DIGEST 14
199 
200 #define SSL_MD_NUM_IDX  SSL_MAX_DIGEST
201 
202 /* Bits for algorithm2 (handshake digests and other extra flags) */
203 
204 /* Bits 0-7 are handshake MAC */
205 # define SSL_HANDSHAKE_MAC_MASK  0xFF
206 # define SSL_HANDSHAKE_MAC_MD5_SHA1 SSL_MD_MD5_SHA1_IDX
207 # define SSL_HANDSHAKE_MAC_SHA256   SSL_MD_SHA256_IDX
208 # define SSL_HANDSHAKE_MAC_SHA384   SSL_MD_SHA384_IDX
209 # define SSL_HANDSHAKE_MAC_GOST94 SSL_MD_GOST94_IDX
210 # define SSL_HANDSHAKE_MAC_GOST12_256 SSL_MD_GOST12_256_IDX
211 # define SSL_HANDSHAKE_MAC_GOST12_512 SSL_MD_GOST12_512_IDX
212 # define SSL_HANDSHAKE_MAC_DEFAULT  SSL_HANDSHAKE_MAC_MD5_SHA1
213 
214 /* Bits 8-15 bits are PRF */
215 # define TLS1_PRF_DGST_SHIFT 8
216 # define TLS1_PRF_SHA1_MD5 (SSL_MD_MD5_SHA1_IDX << TLS1_PRF_DGST_SHIFT)
217 # define TLS1_PRF_SHA256 (SSL_MD_SHA256_IDX << TLS1_PRF_DGST_SHIFT)
218 # define TLS1_PRF_SHA384 (SSL_MD_SHA384_IDX << TLS1_PRF_DGST_SHIFT)
219 # define TLS1_PRF_GOST94 (SSL_MD_GOST94_IDX << TLS1_PRF_DGST_SHIFT)
220 # define TLS1_PRF_GOST12_256 (SSL_MD_GOST12_256_IDX << TLS1_PRF_DGST_SHIFT)
221 # define TLS1_PRF_GOST12_512 (SSL_MD_GOST12_512_IDX << TLS1_PRF_DGST_SHIFT)
222 # define TLS1_PRF            (SSL_MD_MD5_SHA1_IDX << TLS1_PRF_DGST_SHIFT)
223 
224 /*
225  * Stream MAC for GOST ciphersuites from cryptopro draft (currently this also
226  * goes into algorithm2)
227  */
228 # define TLS1_STREAM_MAC 0x10000
229 /*
230  * TLSTREE cipher/mac key derivation from draft-smyshlyaev-tls12-gost-suites
231  * (currently this also  goes into algorithm2)
232  */
233 # define TLS1_TLSTREE 0x20000
234 
235 /* Ciphersuite supported in QUIC */
236 # define SSL_QUIC                0x00040000U
237 
238 # define SSL_STRONG_MASK         0x0000001FU
239 # define SSL_DEFAULT_MASK        0X00000020U
240 
241 # define SSL_STRONG_NONE         0x00000001U
242 # define SSL_LOW                 0x00000002U
243 # define SSL_MEDIUM              0x00000004U
244 # define SSL_HIGH                0x00000008U
245 # define SSL_FIPS                0x00000010U
246 # define SSL_NOT_DEFAULT         0x00000020U
247 
248 /* we have used 0000003f - 26 bits left to go */
249 
250 /* Flag used on OpenSSL ciphersuite ids to indicate they are for SSLv3+ */
251 # define SSL3_CK_CIPHERSUITE_FLAG                0x03000000
252 
253 /* Check if an SSL structure is using DTLS */
254 # define SSL_CONNECTION_IS_DTLS(s) \
255     (SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS)
256 
257 /* Check if we are using TLSv1.3 */
258 # define SSL_CONNECTION_IS_TLS13(s) (!SSL_CONNECTION_IS_DTLS(s) \
259     && SSL_CONNECTION_GET_SSL(s)->method->version >= TLS1_3_VERSION \
260     && SSL_CONNECTION_GET_SSL(s)->method->version != TLS_ANY_VERSION)
261 
262 # define SSL_CONNECTION_TREAT_AS_TLS13(s) \
263     (SSL_CONNECTION_IS_TLS13(s) \
264      || (s)->early_data_state == SSL_EARLY_DATA_CONNECTING \
265      || (s)->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY \
266      || (s)->early_data_state == SSL_EARLY_DATA_WRITING \
267      || (s)->early_data_state == SSL_EARLY_DATA_WRITE_RETRY \
268      || (s)->hello_retry_request == SSL_HRR_PENDING)
269 
270 # define SSL_IS_FIRST_HANDSHAKE(s) ((s)->s3.tmp.finish_md_len == 0 \
271                                     || (s)->s3.tmp.peer_finish_md_len == 0)
272 
273 /*
274  * See if we use signature algorithms extension and signature algorithm
275  * before signatures.
276  */
277 # define SSL_USE_SIGALGS(s)      \
278     (SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_SIGALGS)
279 /*
280  * Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2: may
281  * apply to others in future.
282  */
283 # define SSL_USE_TLS1_2_CIPHERS(s)       \
284     (SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)
285 /*
286  * Determine if a client can use TLS 1.2 ciphersuites: can't rely on method
287  * flags because it may not be set to correct version yet.
288  */
289 # define SSL_CLIENT_USE_TLS1_2_CIPHERS(s)        \
290     ((!SSL_CONNECTION_IS_DTLS(s) && s->client_version >= TLS1_2_VERSION) || \
291      (SSL_CONNECTION_IS_DTLS(s) && DTLS_VERSION_GE(s->client_version, DTLS1_2_VERSION)))
292 /*
293  * Determine if a client should send signature algorithms extension:
294  * as with TLS1.2 cipher we can't rely on method flags.
295  */
296 # define SSL_CLIENT_USE_SIGALGS(s)        \
297     SSL_CLIENT_USE_TLS1_2_CIPHERS(s)
298 
299 # define IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value) \
300     (((value) >= TLSEXT_max_fragment_length_512) && \
301      ((value) <= TLSEXT_max_fragment_length_4096))
302 # define USE_MAX_FRAGMENT_LENGTH_EXT(session) \
303     IS_MAX_FRAGMENT_LENGTH_EXT_VALID(session->ext.max_fragment_len_mode)
304 # define GET_MAX_FRAGMENT_LENGTH(session) \
305     (512U << (session->ext.max_fragment_len_mode - 1))
306 
307 # define SSL_READ_ETM(s) (s->s3.flags & TLS1_FLAGS_ENCRYPT_THEN_MAC_READ)
308 # define SSL_WRITE_ETM(s) (s->s3.flags & TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE)
309 
310 # define SSL_IS_QUIC_HANDSHAKE(s) (((s)->s3.flags & TLS1_FLAGS_QUIC) != 0)
311 
312 /* alert_dispatch values */
313 
314 /* No alert pending */
315 # define SSL_ALERT_DISPATCH_NONE    0
316 /* Alert pending */
317 # define SSL_ALERT_DISPATCH_PENDING 1
318 /* Pending alert write needs to be retried */
319 # define SSL_ALERT_DISPATCH_RETRY   2
320 
321 /* Mostly for SSLv3 */
322 # define SSL_PKEY_RSA            0
323 # define SSL_PKEY_RSA_PSS_SIGN   1
324 # define SSL_PKEY_DSA_SIGN       2
325 # define SSL_PKEY_ECC            3
326 # define SSL_PKEY_GOST01         4
327 # define SSL_PKEY_GOST12_256     5
328 # define SSL_PKEY_GOST12_512     6
329 # define SSL_PKEY_ED25519        7
330 # define SSL_PKEY_ED448          8
331 # define SSL_PKEY_NUM            9
332 
333 # define SSL_ENC_DES_IDX         0
334 # define SSL_ENC_3DES_IDX        1
335 # define SSL_ENC_RC4_IDX         2
336 # define SSL_ENC_RC2_IDX         3
337 # define SSL_ENC_IDEA_IDX        4
338 # define SSL_ENC_NULL_IDX        5
339 # define SSL_ENC_AES128_IDX      6
340 # define SSL_ENC_AES256_IDX      7
341 # define SSL_ENC_CAMELLIA128_IDX 8
342 # define SSL_ENC_CAMELLIA256_IDX 9
343 # define SSL_ENC_GOST89_IDX      10
344 # define SSL_ENC_SEED_IDX        11
345 # define SSL_ENC_AES128GCM_IDX   12
346 # define SSL_ENC_AES256GCM_IDX   13
347 # define SSL_ENC_AES128CCM_IDX   14
348 # define SSL_ENC_AES256CCM_IDX   15
349 # define SSL_ENC_AES128CCM8_IDX  16
350 # define SSL_ENC_AES256CCM8_IDX  17
351 # define SSL_ENC_GOST8912_IDX    18
352 # define SSL_ENC_CHACHA_IDX      19
353 # define SSL_ENC_ARIA128GCM_IDX  20
354 # define SSL_ENC_ARIA256GCM_IDX  21
355 # define SSL_ENC_MAGMA_IDX       22
356 # define SSL_ENC_KUZNYECHIK_IDX  23
357 # define SSL_ENC_NUM_IDX         24
358 
359 /*-
360  * SSL_kRSA <- RSA_ENC
361  * SSL_kDH  <- DH_ENC & (RSA_ENC | RSA_SIGN | DSA_SIGN)
362  * SSL_kDHE <- RSA_ENC | RSA_SIGN | DSA_SIGN
363  * SSL_aRSA <- RSA_ENC | RSA_SIGN
364  * SSL_aDSS <- DSA_SIGN
365  */
366 
367 /* Certificate Type State */
368 # define OSSL_CERT_TYPE_CTOS_NONE    0
369 # define OSSL_CERT_TYPE_CTOS_GOOD    1
370 # define OSSL_CERT_TYPE_CTOS_ERROR   2
371 
372 /* Post-Handshake Authentication state */
373 typedef enum {
374     SSL_PHA_NONE = 0,
375     SSL_PHA_EXT_SENT,        /* client-side only: extension sent */
376     SSL_PHA_EXT_RECEIVED,    /* server-side only: extension received */
377     SSL_PHA_REQUEST_PENDING, /* server-side only: request pending */
378     SSL_PHA_REQUESTED        /* request received by client, or sent by server */
379 } SSL_PHA_STATE;
380 
381 /* CipherSuite length. SSLv3 and all TLS versions. */
382 # define TLS_CIPHER_LEN 2
383 /* used to hold info on the particular ciphers used */
384 struct ssl_cipher_st {
385     uint32_t valid;
386     const char *name;           /* text name */
387     const char *stdname;        /* RFC name */
388     uint32_t id;                /* id, 4 bytes, first is version */
389     /*
390      * changed in 1.0.0: these four used to be portions of a single value
391      * 'algorithms'
392      */
393     uint32_t algorithm_mkey;    /* key exchange algorithm */
394     uint32_t algorithm_auth;    /* server authentication */
395     uint32_t algorithm_enc;     /* symmetric encryption */
396     uint32_t algorithm_mac;     /* symmetric authentication */
397     int min_tls;                /* minimum SSL/TLS protocol version */
398     int max_tls;                /* maximum SSL/TLS protocol version */
399     int min_dtls;               /* minimum DTLS protocol version */
400     int max_dtls;               /* maximum DTLS protocol version */
401     uint32_t algo_strength;     /* strength and export flags */
402     uint32_t algorithm2;        /* Extra flags */
403     int32_t strength_bits;      /* Number of bits really used */
404     uint32_t alg_bits;          /* Number of bits for algorithm */
405 };
406 
407 /* Used to hold SSL/TLS functions */
408 struct ssl_method_st {
409     int version;
410     unsigned flags;
411     uint64_t mask;
412     SSL *(*ssl_new) (SSL_CTX *ctx);
413     void (*ssl_free) (SSL *s);
414     int (*ssl_reset) (SSL *s);
415     int (*ssl_init) (SSL *s);
416     int (*ssl_clear) (SSL *s);
417     void (*ssl_deinit) (SSL *s);
418     int (*ssl_accept) (SSL *s);
419     int (*ssl_connect) (SSL *s);
420     int (*ssl_read) (SSL *s, void *buf, size_t len, size_t *readbytes);
421     int (*ssl_peek) (SSL *s, void *buf, size_t len, size_t *readbytes);
422     int (*ssl_write) (SSL *s, const void *buf, size_t len, size_t *written);
423     int (*ssl_shutdown) (SSL *s);
424     int (*ssl_renegotiate) (SSL *s);
425     int (*ssl_renegotiate_check) (SSL *s, int);
426     int (*ssl_read_bytes) (SSL *s, uint8_t type, uint8_t *recvd_type,
427                            unsigned char *buf, size_t len, int peek,
428                            size_t *readbytes);
429     int (*ssl_write_bytes) (SSL *s, uint8_t type, const void *buf_, size_t len,
430                             size_t *written);
431     int (*ssl_dispatch_alert) (SSL *s);
432     long (*ssl_ctrl) (SSL *s, int cmd, long larg, void *parg);
433     long (*ssl_ctx_ctrl) (SSL_CTX *ctx, int cmd, long larg, void *parg);
434     const SSL_CIPHER *(*get_cipher_by_char) (const unsigned char *ptr);
435     int (*put_cipher_by_char) (const SSL_CIPHER *cipher, WPACKET *pkt,
436                                size_t *len);
437     size_t (*ssl_pending) (const SSL *s);
438     int (*num_ciphers) (void);
439     const SSL_CIPHER *(*get_cipher) (unsigned ncipher);
440     OSSL_TIME (*get_timeout) (void);
441     const struct ssl3_enc_method *ssl3_enc; /* Extra SSLv3/TLS stuff */
442     int (*ssl_version) (void);
443     long (*ssl_callback_ctrl) (SSL *s, int cb_id, void (*fp) (void));
444     long (*ssl_ctx_callback_ctrl) (SSL_CTX *s, int cb_id, void (*fp) (void));
445 };
446 
447 /*
448  * Matches the length of PSK_MAX_PSK_LEN. We keep it the same value for
449  * consistency, even in the event of OPENSSL_NO_PSK being defined.
450  */
451 # define TLS13_MAX_RESUMPTION_PSK_LENGTH      512
452 
453 /*-
454  * Lets make this into an ASN.1 type structure as follows
455  * SSL_SESSION_ID ::= SEQUENCE {
456  *      version                 INTEGER,        -- structure version number
457  *      SSLversion              INTEGER,        -- SSL version number
458  *      Cipher                  OCTET STRING,   -- the 3 byte cipher ID
459  *      Session_ID              OCTET STRING,   -- the Session ID
460  *      Master_key              OCTET STRING,   -- the master key
461  *      Key_Arg [ 0 ] IMPLICIT  OCTET STRING,   -- the optional Key argument
462  *      Time [ 1 ] EXPLICIT     INTEGER,        -- optional Start Time
463  *      Timeout [ 2 ] EXPLICIT  INTEGER,        -- optional Timeout ins seconds
464  *      Peer [ 3 ] EXPLICIT     X509,           -- optional Peer Certificate
465  *      Session_ID_context [ 4 ] EXPLICIT OCTET STRING,   -- the Session ID context
466  *      Verify_result [ 5 ] EXPLICIT INTEGER,   -- X509_V_... code for `Peer'
467  *      HostName [ 6 ] EXPLICIT OCTET STRING,   -- optional HostName from servername TLS extension
468  *      PSK_identity_hint [ 7 ] EXPLICIT OCTET STRING, -- optional PSK identity hint
469  *      PSK_identity [ 8 ] EXPLICIT OCTET STRING,  -- optional PSK identity
470  *      Ticket_lifetime_hint [9] EXPLICIT INTEGER, -- server's lifetime hint for session ticket
471  *      Ticket [10]             EXPLICIT OCTET STRING, -- session ticket (clients only)
472  *      Compression_meth [11]   EXPLICIT OCTET STRING, -- optional compression method
473  *      SRP_username [ 12 ] EXPLICIT OCTET STRING -- optional SRP username
474  *      flags [ 13 ] EXPLICIT INTEGER -- optional flags
475  *      }
476  * Look in ssl/ssl_asn1.c for more details
477  * I'm using EXPLICIT tags so I can read the damn things using asn1parse :-).
478  */
479 struct ssl_session_st {
480     int ssl_version;            /* what ssl version session info is being kept
481                                  * in here? */
482     size_t master_key_length;
483 
484     /* TLSv1.3 early_secret used for external PSKs */
485     unsigned char early_secret[EVP_MAX_MD_SIZE];
486     /*
487      * For <=TLS1.2 this is the master_key. For TLS1.3 this is the resumption
488      * PSK
489      */
490     unsigned char master_key[TLS13_MAX_RESUMPTION_PSK_LENGTH];
491     /* session_id - valid? */
492     size_t session_id_length;
493     unsigned char session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
494     /*
495      * this is used to determine whether the session is being reused in the
496      * appropriate context. It is up to the application to set this, via
497      * SSL_new
498      */
499     size_t sid_ctx_length;
500     unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH];
501 # ifndef OPENSSL_NO_PSK
502     char *psk_identity_hint;
503     char *psk_identity;
504 # endif
505     /*
506      * Used to indicate that session resumption is not allowed. Applications
507      * can also set this bit for a new session via not_resumable_session_cb
508      * to disable session caching and tickets.
509      */
510     int not_resumable;
511     /* Peer raw public key, if available */
512     EVP_PKEY *peer_rpk;
513     /* This is the cert and type for the other end. */
514     X509 *peer;
515     /* Certificate chain peer sent. */
516     STACK_OF(X509) *peer_chain;
517     /*
518      * when app_verify_callback accepts a session where the peer's
519      * certificate is not ok, we must remember the error for session reuse:
520      */
521     long verify_result;         /* only for servers */
522     OSSL_TIME timeout;
523     OSSL_TIME time;
524     OSSL_TIME calc_timeout;
525     unsigned int compress_meth; /* Need to lookup the method */
526     const SSL_CIPHER *cipher;
527     unsigned long cipher_id;    /* when ASN.1 loaded, this needs to be used to
528                                  * load the 'cipher' structure */
529     unsigned int kex_group;      /* TLS group from key exchange */
530     CRYPTO_EX_DATA ex_data;     /* application specific data */
531 
532     struct {
533         char *hostname;
534         /* RFC4507 info */
535         unsigned char *tick; /* Session ticket */
536         size_t ticklen;      /* Session ticket length */
537         /* Session lifetime hint in seconds */
538         unsigned long tick_lifetime_hint;
539         uint32_t tick_age_add;
540         /* Max number of bytes that can be sent as early data */
541         uint32_t max_early_data;
542         /* The ALPN protocol selected for this session */
543         unsigned char *alpn_selected;
544         size_t alpn_selected_len;
545         /*
546          * Maximum Fragment Length as per RFC 4366.
547          * If this value does not contain RFC 4366 allowed values (1-4) then
548          * either the Maximum Fragment Length Negotiation failed or was not
549          * performed at all.
550          */
551         uint8_t max_fragment_len_mode;
552     } ext;
553 # ifndef OPENSSL_NO_SRP
554     char *srp_username;
555 # endif
556     unsigned char *ticket_appdata;
557     size_t ticket_appdata_len;
558     uint32_t flags;
559     SSL_CTX *owner;
560 
561     /*
562      * These are used to make removal of session-ids more efficient and to
563      * implement a maximum cache size. Access requires protection of ctx->lock.
564      */
565     struct ssl_session_st *prev, *next;
566     CRYPTO_REF_COUNT references;
567 };
568 
569 /* Extended master secret support */
570 # define SSL_SESS_FLAG_EXTMS             0x1
571 
572 # ifndef OPENSSL_NO_SRP
573 
574 typedef struct srp_ctx_st {
575     /* param for all the callbacks */
576     void *SRP_cb_arg;
577     /* set client Hello login callback */
578     int (*TLS_ext_srp_username_callback) (SSL *, int *, void *);
579     /* set SRP N/g param callback for verification */
580     int (*SRP_verify_param_callback) (SSL *, void *);
581     /* set SRP client passwd callback */
582     char *(*SRP_give_srp_client_pwd_callback) (SSL *, void *);
583     char *login;
584     BIGNUM *N, *g, *s, *B, *A;
585     BIGNUM *a, *b, *v;
586     char *info;
587     int strength;
588     unsigned long srp_Mask;
589 } SRP_CTX;
590 
591 # endif
592 
593 typedef enum {
594     SSL_EARLY_DATA_NONE = 0,
595     SSL_EARLY_DATA_CONNECT_RETRY,
596     SSL_EARLY_DATA_CONNECTING,
597     SSL_EARLY_DATA_WRITE_RETRY,
598     SSL_EARLY_DATA_WRITING,
599     SSL_EARLY_DATA_WRITE_FLUSH,
600     SSL_EARLY_DATA_UNAUTH_WRITING,
601     SSL_EARLY_DATA_FINISHED_WRITING,
602     SSL_EARLY_DATA_ACCEPT_RETRY,
603     SSL_EARLY_DATA_ACCEPTING,
604     SSL_EARLY_DATA_READ_RETRY,
605     SSL_EARLY_DATA_READING,
606     SSL_EARLY_DATA_FINISHED_READING
607 } SSL_EARLY_DATA_STATE;
608 
609 /*
610  * We check that the amount of unreadable early data doesn't exceed
611  * max_early_data. max_early_data is given in plaintext bytes. However if it is
612  * unreadable then we only know the number of ciphertext bytes. We also don't
613  * know how much the overhead should be because it depends on the ciphersuite.
614  * We make a small allowance. We assume 5 records of actual data plus the end
615  * of early data alert record. Each record has a tag and a content type byte.
616  * The longest tag length we know of is EVP_GCM_TLS_TAG_LEN. We don't count the
617  * content of the alert record either which is 2 bytes.
618  */
619 # define EARLY_DATA_CIPHERTEXT_OVERHEAD ((6 * (EVP_GCM_TLS_TAG_LEN + 1)) + 2)
620 
621 /*
622  * The allowance we have between the client's calculated ticket age and our own.
623  * We allow for 10 seconds. If a ticket is presented and the
624  * client's age calculation is different by more than this than our own then we
625  * do not allow that ticket for early_data.
626  */
627 # define TICKET_AGE_ALLOWANCE   ossl_seconds2time(10)
628 
629 #define MAX_COMPRESSIONS_SIZE   255
630 
631 
632 typedef struct raw_extension_st {
633     /* Raw packet data for the extension */
634     PACKET data;
635     /* Set to 1 if the extension is present or 0 otherwise */
636     int present;
637     /* Set to 1 if we have already parsed the extension or 0 otherwise */
638     int parsed;
639     /* The type of this extension, i.e. a TLSEXT_TYPE_* value */
640     unsigned int type;
641     /* Track what order extensions are received in (0-based). */
642     size_t received_order;
643 } RAW_EXTENSION;
644 
645 typedef struct {
646     unsigned int isv2;
647     unsigned int legacy_version;
648     unsigned char random[SSL3_RANDOM_SIZE];
649     size_t session_id_len;
650     unsigned char session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
651     size_t dtls_cookie_len;
652     unsigned char dtls_cookie[DTLS1_COOKIE_LENGTH];
653     PACKET ciphersuites;
654     size_t compressions_len;
655     unsigned char compressions[MAX_COMPRESSIONS_SIZE];
656     PACKET extensions;
657     size_t pre_proc_exts_len;
658     RAW_EXTENSION *pre_proc_exts;
659 } CLIENTHELLO_MSG;
660 
661 /*
662  * Extension index values NOTE: Any updates to these defines should be mirrored
663  * with equivalent updates to ext_defs in extensions.c
664  */
665 typedef enum tlsext_index_en {
666     TLSEXT_IDX_renegotiate,
667     TLSEXT_IDX_server_name,
668     TLSEXT_IDX_max_fragment_length,
669     TLSEXT_IDX_srp,
670     TLSEXT_IDX_ec_point_formats,
671     TLSEXT_IDX_supported_groups,
672     TLSEXT_IDX_session_ticket,
673     TLSEXT_IDX_status_request,
674     TLSEXT_IDX_next_proto_neg,
675     TLSEXT_IDX_application_layer_protocol_negotiation,
676     TLSEXT_IDX_use_srtp,
677     TLSEXT_IDX_encrypt_then_mac,
678     TLSEXT_IDX_signed_certificate_timestamp,
679     TLSEXT_IDX_extended_master_secret,
680     TLSEXT_IDX_signature_algorithms_cert,
681     TLSEXT_IDX_post_handshake_auth,
682     TLSEXT_IDX_client_cert_type,
683     TLSEXT_IDX_server_cert_type,
684     TLSEXT_IDX_signature_algorithms,
685     TLSEXT_IDX_supported_versions,
686     TLSEXT_IDX_psk_kex_modes,
687     TLSEXT_IDX_key_share,
688     TLSEXT_IDX_cookie,
689     TLSEXT_IDX_cryptopro_bug,
690     TLSEXT_IDX_compress_certificate,
691     TLSEXT_IDX_early_data,
692     TLSEXT_IDX_certificate_authorities,
693     TLSEXT_IDX_padding,
694     TLSEXT_IDX_psk,
695     /* Dummy index - must always be the last entry */
696     TLSEXT_IDX_num_builtins
697 } TLSEXT_INDEX;
698 
699 DEFINE_LHASH_OF_EX(SSL_SESSION);
700 /* Needed in ssl_cert.c */
701 DEFINE_LHASH_OF_EX(X509_NAME);
702 
703 # define TLSEXT_KEYNAME_LENGTH  16
704 # define TLSEXT_TICK_KEY_LENGTH 32
705 
706 typedef struct ssl_ctx_ext_secure_st {
707     unsigned char tick_hmac_key[TLSEXT_TICK_KEY_LENGTH];
708     unsigned char tick_aes_key[TLSEXT_TICK_KEY_LENGTH];
709 } SSL_CTX_EXT_SECURE;
710 
711 /*
712  * Helper function for HMAC
713  * The structure should be considered opaque, it will change once the low
714  * level deprecated calls are removed.  At that point it can be replaced
715  * by EVP_MAC_CTX and most of the functions converted to macros or inlined
716  * directly.
717  */
718 typedef struct ssl_hmac_st {
719     EVP_MAC_CTX *ctx;
720 # ifndef OPENSSL_NO_DEPRECATED_3_0
721     HMAC_CTX *old_ctx;
722 # endif
723 } SSL_HMAC;
724 
725 SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx);
726 void ssl_hmac_free(SSL_HMAC *ctx);
727 # ifndef OPENSSL_NO_DEPRECATED_3_0
728 HMAC_CTX *ssl_hmac_get0_HMAC_CTX(SSL_HMAC *ctx);
729 # endif
730 EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx);
731 int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md);
732 int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len);
733 int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len,
734                    size_t max_size);
735 size_t ssl_hmac_size(const SSL_HMAC *ctx);
736 
737 int ssl_get_EC_curve_nid(const EVP_PKEY *pkey);
738 __owur int tls13_set_encoded_pub_key(EVP_PKEY *pkey,
739                                      const unsigned char *enckey,
740                                      size_t enckeylen);
741 
742 typedef struct tls_group_info_st {
743     char *tlsname;           /* Curve Name as in TLS specs */
744     char *realname;          /* Curve Name according to provider */
745     char *algorithm;         /* Algorithm name to fetch */
746     unsigned int secbits;    /* Bits of security (from SP800-57) */
747     uint16_t group_id;       /* Group ID */
748     int mintls;              /* Minimum TLS version, -1 unsupported */
749     int maxtls;              /* Maximum TLS version (or 0 for undefined) */
750     int mindtls;             /* Minimum DTLS version, -1 unsupported */
751     int maxdtls;             /* Maximum DTLS version (or 0 for undefined) */
752     char is_kem;             /* Mode for this Group: 0 is KEX, 1 is KEM */
753 } TLS_GROUP_INFO;
754 
755 typedef struct tls_sigalg_info_st {
756     char *name;              /* name as in IANA TLS specs */
757     uint16_t code_point;     /* IANA-specified code point of sigalg-name */
758     char *sigalg_name;       /* (combined) sigalg name */
759     char *sigalg_oid;        /* (combined) sigalg OID */
760     char *sig_name;          /* pure signature algorithm name */
761     char *sig_oid;           /* pure signature algorithm OID */
762     char *hash_name;         /* hash algorithm name */
763     char *hash_oid;          /* hash algorithm OID */
764     char *keytype;           /* keytype name */
765     char *keytype_oid;       /* keytype OID */
766     unsigned int secbits;    /* Bits of security (from SP800-57) */
767     int mintls;              /* Minimum TLS version, -1 unsupported */
768     int maxtls;              /* Maximum TLS version (or 0 for undefined) */
769 } TLS_SIGALG_INFO;
770 
771 /*
772  * Structure containing table entry of certificate info corresponding to
773  * CERT_PKEY entries
774  */
775 typedef struct {
776     int nid; /* NID of public key algorithm */
777     uint32_t amask; /* authmask corresponding to key type */
778 } SSL_CERT_LOOKUP;
779 
780 /* flags values */
781 # define TLS_GROUP_TYPE             0x0000000FU /* Mask for group type */
782 # define TLS_GROUP_CURVE_PRIME      0x00000001U
783 # define TLS_GROUP_CURVE_CHAR2      0x00000002U
784 # define TLS_GROUP_CURVE_CUSTOM     0x00000004U
785 # define TLS_GROUP_FFDHE            0x00000008U
786 # define TLS_GROUP_ONLY_FOR_TLS1_3  0x00000010U
787 
788 # define TLS_GROUP_FFDHE_FOR_TLS1_3 (TLS_GROUP_FFDHE|TLS_GROUP_ONLY_FOR_TLS1_3)
789 
790 struct ssl_ctx_st {
791     OSSL_LIB_CTX *libctx;
792 
793     const SSL_METHOD *method;
794     STACK_OF(SSL_CIPHER) *cipher_list;
795     /* same as above but sorted for lookup */
796     STACK_OF(SSL_CIPHER) *cipher_list_by_id;
797     /* TLSv1.3 specific ciphersuites */
798     STACK_OF(SSL_CIPHER) *tls13_ciphersuites;
799     struct x509_store_st /* X509_STORE */ *cert_store;
800     LHASH_OF(SSL_SESSION) *sessions;
801     /*
802      * Most session-ids that will be cached, default is
803      * SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
804      */
805     size_t session_cache_size;
806     struct ssl_session_st *session_cache_head;
807     struct ssl_session_st *session_cache_tail;
808     /*
809      * This can have one of 2 values, ored together, SSL_SESS_CACHE_CLIENT,
810      * SSL_SESS_CACHE_SERVER, Default is SSL_SESSION_CACHE_SERVER, which
811      * means only SSL_accept will cache SSL_SESSIONS.
812      */
813     uint32_t session_cache_mode;
814     /*
815      * If timeout is not 0, it is the default timeout value set when
816      * SSL_new() is called.  This has been put in to make life easier to set
817      * things up
818      */
819     OSSL_TIME session_timeout;
820     /*
821      * If this callback is not null, it will be called each time a session id
822      * is added to the cache.  If this function returns 1, it means that the
823      * callback will do an SSL_SESSION_free() when it has finished using it.
824      * Otherwise, on 0, it means the callback has finished with it. If
825      * remove_session_cb is not null, it will be called when a session-id is
826      * removed from the cache.  After the call, OpenSSL will
827      * SSL_SESSION_free() it.
828      */
829     int (*new_session_cb) (struct ssl_st *ssl, SSL_SESSION *sess);
830     void (*remove_session_cb) (struct ssl_ctx_st *ctx, SSL_SESSION *sess);
831     SSL_SESSION *(*get_session_cb) (struct ssl_st *ssl,
832                                     const unsigned char *data, int len,
833                                     int *copy);
834     struct {
835         TSAN_QUALIFIER int sess_connect;       /* SSL new conn - started */
836         TSAN_QUALIFIER int sess_connect_renegotiate; /* SSL reneg - requested */
837         TSAN_QUALIFIER int sess_connect_good;  /* SSL new conne/reneg - finished */
838         TSAN_QUALIFIER int sess_accept;        /* SSL new accept - started */
839         TSAN_QUALIFIER int sess_accept_renegotiate; /* SSL reneg - requested */
840         TSAN_QUALIFIER int sess_accept_good;   /* SSL accept/reneg - finished */
841         TSAN_QUALIFIER int sess_miss;          /* session lookup misses */
842         TSAN_QUALIFIER int sess_timeout;       /* reuse attempt on timeouted session */
843         TSAN_QUALIFIER int sess_cache_full;    /* session removed due to full cache */
844         TSAN_QUALIFIER int sess_hit;           /* session reuse actually done */
845         TSAN_QUALIFIER int sess_cb_hit;        /* session-id that was not in
846                                                 * the cache was passed back via
847                                                 * the callback. This indicates
848                                                 * that the application is
849                                                 * supplying session-id's from
850                                                 * other processes - spooky
851                                                 * :-) */
852     } stats;
853 #ifdef TSAN_REQUIRES_LOCKING
854     CRYPTO_RWLOCK *tsan_lock;
855 #endif
856 
857     CRYPTO_REF_COUNT references;
858 
859     /* if defined, these override the X509_verify_cert() calls */
860     int (*app_verify_callback) (X509_STORE_CTX *, void *);
861     void *app_verify_arg;
862     /*
863      * before OpenSSL 0.9.7, 'app_verify_arg' was ignored
864      * ('app_verify_callback' was called with just one argument)
865      */
866 
867     /* Default password callback. */
868     pem_password_cb *default_passwd_callback;
869 
870     /* Default password callback user data. */
871     void *default_passwd_callback_userdata;
872 
873     /* get client cert callback */
874     int (*client_cert_cb) (SSL *ssl, X509 **x509, EVP_PKEY **pkey);
875 
876     /* cookie generate callback */
877     int (*app_gen_cookie_cb) (SSL *ssl, unsigned char *cookie,
878                               unsigned int *cookie_len);
879 
880     /* verify cookie callback */
881     int (*app_verify_cookie_cb) (SSL *ssl, const unsigned char *cookie,
882                                  unsigned int cookie_len);
883 
884     /* TLS1.3 app-controlled cookie generate callback */
885     int (*gen_stateless_cookie_cb) (SSL *ssl, unsigned char *cookie,
886                                     size_t *cookie_len);
887 
888     /* TLS1.3 verify app-controlled cookie callback */
889     int (*verify_stateless_cookie_cb) (SSL *ssl, const unsigned char *cookie,
890                                        size_t cookie_len);
891 
892     CRYPTO_EX_DATA ex_data;
893 
894     const EVP_MD *md5;          /* For SSLv3/TLSv1 'ssl3-md5' */
895     const EVP_MD *sha1;         /* For SSLv3/TLSv1 'ssl3-sha1' */
896 
897     STACK_OF(X509) *extra_certs;
898     STACK_OF(SSL_COMP) *comp_methods; /* stack of SSL_COMP, SSLv3/TLSv1 */
899 
900     /* Default values used when no per-SSL value is defined follow */
901 
902     /* used if SSL's info_callback is NULL */
903     void (*info_callback) (const SSL *ssl, int type, int val);
904 
905     /*
906      * What we put in certificate_authorities extension for TLS 1.3
907      * (ClientHello and CertificateRequest) or just client cert requests for
908      * earlier versions. If client_ca_names is populated then it is only used
909      * for client cert requests, and in preference to ca_names.
910      */
911     STACK_OF(X509_NAME) *ca_names;
912     STACK_OF(X509_NAME) *client_ca_names;
913 
914     /*
915      * Default values to use in SSL structures follow (these are copied by
916      * SSL_new)
917      */
918 
919     uint64_t options;
920     uint32_t mode;
921     int min_proto_version;
922     int max_proto_version;
923     size_t max_cert_list;
924 
925     struct cert_st /* CERT */ *cert;
926     SSL_CERT_LOOKUP *ssl_cert_info;
927     int read_ahead;
928 
929     /* callback that allows applications to peek at protocol messages */
930     ossl_msg_cb msg_callback;
931     void *msg_callback_arg;
932 
933     uint32_t verify_mode;
934     size_t sid_ctx_length;
935     unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH];
936     /* called 'verify_callback' in the SSL */
937     int (*default_verify_callback) (int ok, X509_STORE_CTX *ctx);
938 
939     /* Default generate session ID callback. */
940     GEN_SESSION_CB generate_session_id;
941 
942     X509_VERIFY_PARAM *param;
943 
944     int quiet_shutdown;
945 
946 # ifndef OPENSSL_NO_CT
947     CTLOG_STORE *ctlog_store;   /* CT Log Store */
948     /*
949      * Validates that the SCTs (Signed Certificate Timestamps) are sufficient.
950      * If they are not, the connection should be aborted.
951      */
952     ssl_ct_validation_cb ct_validation_callback;
953     void *ct_validation_callback_arg;
954 # endif
955 
956     /*
957      * If we're using more than one pipeline how should we divide the data
958      * up between the pipes?
959      */
960     size_t split_send_fragment;
961     /*
962      * Maximum amount of data to send in one fragment. actual record size can
963      * be more than this due to padding and MAC overheads.
964      */
965     size_t max_send_fragment;
966 
967     /* Up to how many pipelines should we use? If 0 then 1 is assumed */
968     size_t max_pipelines;
969 
970     /* The default read buffer length to use (0 means not set) */
971     size_t default_read_buf_len;
972 
973 # ifndef OPENSSL_NO_ENGINE
974     /*
975      * Engine to pass requests for client certs to
976      */
977     ENGINE *client_cert_engine;
978 # endif
979 
980     /* ClientHello callback.  Mostly for extensions, but not entirely. */
981     SSL_client_hello_cb_fn client_hello_cb;
982     void *client_hello_cb_arg;
983 
984     /* TLS extensions. */
985     struct {
986         /* TLS extensions servername callback */
987         int (*servername_cb) (SSL *, int *, void *);
988         void *servername_arg;
989         /* RFC 4507 session ticket keys */
990         unsigned char tick_key_name[TLSEXT_KEYNAME_LENGTH];
991         SSL_CTX_EXT_SECURE *secure;
992 # ifndef OPENSSL_NO_DEPRECATED_3_0
993         /* Callback to support customisation of ticket key setting */
994         int (*ticket_key_cb) (SSL *ssl,
995                               unsigned char *name, unsigned char *iv,
996                               EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
997 #endif
998         int (*ticket_key_evp_cb) (SSL *ssl,
999                                   unsigned char *name, unsigned char *iv,
1000                                   EVP_CIPHER_CTX *ectx, EVP_MAC_CTX *hctx,
1001                                   int enc);
1002 
1003         /* certificate status request info */
1004         /* Callback for status request */
1005         int (*status_cb) (SSL *ssl, void *arg);
1006         void *status_arg;
1007         /* ext status type used for CSR extension (OCSP Stapling) */
1008         int status_type;
1009         /* RFC 4366 Maximum Fragment Length Negotiation */
1010         uint8_t max_fragment_len_mode;
1011 
1012         /* EC extension values inherited by SSL structure */
1013         size_t ecpointformats_len;
1014         unsigned char *ecpointformats;
1015 
1016         size_t supportedgroups_len;
1017         uint16_t *supportedgroups;
1018 
1019         uint16_t *supported_groups_default;
1020         size_t supported_groups_default_len;
1021         /*
1022          * ALPN information (we are in the process of transitioning from NPN to
1023          * ALPN.)
1024          */
1025 
1026         /*-
1027          * For a server, this contains a callback function that allows the
1028          * server to select the protocol for the connection.
1029          *   out: on successful return, this must point to the raw protocol
1030          *        name (without the length prefix).
1031          *   outlen: on successful return, this contains the length of |*out|.
1032          *   in: points to the client's list of supported protocols in
1033          *       wire-format.
1034          *   inlen: the length of |in|.
1035          */
1036         int (*alpn_select_cb) (SSL *s,
1037                                const unsigned char **out,
1038                                unsigned char *outlen,
1039                                const unsigned char *in,
1040                                unsigned int inlen, void *arg);
1041         void *alpn_select_cb_arg;
1042 
1043         /*
1044          * For a client, this contains the list of supported protocols in wire
1045          * format.
1046          */
1047         unsigned char *alpn;
1048         size_t alpn_len;
1049 
1050 # ifndef OPENSSL_NO_NEXTPROTONEG
1051         /* Next protocol negotiation information */
1052 
1053         /*
1054          * For a server, this contains a callback function by which the set of
1055          * advertised protocols can be provided.
1056          */
1057         SSL_CTX_npn_advertised_cb_func npn_advertised_cb;
1058         void *npn_advertised_cb_arg;
1059         /*
1060          * For a client, this contains a callback function that selects the next
1061          * protocol from the list provided by the server.
1062          */
1063         SSL_CTX_npn_select_cb_func npn_select_cb;
1064         void *npn_select_cb_arg;
1065 # endif
1066 
1067         unsigned char cookie_hmac_key[SHA256_DIGEST_LENGTH];
1068     } ext;
1069 
1070 # ifndef OPENSSL_NO_PSK
1071     SSL_psk_client_cb_func psk_client_callback;
1072     SSL_psk_server_cb_func psk_server_callback;
1073 # endif
1074     SSL_psk_find_session_cb_func psk_find_session_cb;
1075     SSL_psk_use_session_cb_func psk_use_session_cb;
1076 
1077 # ifndef OPENSSL_NO_SRP
1078     SRP_CTX srp_ctx;            /* ctx for SRP authentication */
1079 # endif
1080 
1081     /* Shared DANE context */
1082     struct dane_ctx_st dane;
1083 
1084 # ifndef OPENSSL_NO_SRTP
1085     /* SRTP profiles we are willing to do from RFC 5764 */
1086     STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
1087 # endif
1088     /*
1089      * Callback for disabling session caching and ticket support on a session
1090      * basis, depending on the chosen cipher.
1091      */
1092     int (*not_resumable_session_cb) (SSL *ssl, int is_forward_secure);
1093 
1094     CRYPTO_RWLOCK *lock;
1095 
1096     /*
1097      * Callback for logging key material for use with debugging tools like
1098      * Wireshark. The callback should log `line` followed by a newline.
1099      */
1100     SSL_CTX_keylog_cb_func keylog_callback;
1101 
1102     /*
1103      * Private flag for internal key logging based on SSLKEYLOG env
1104      */
1105 # ifndef OPENSSL_NO_SSLKEYLOG
1106     uint32_t do_sslkeylog;
1107 # endif
1108 
1109     /*
1110      * The maximum number of bytes advertised in session tickets that can be
1111      * sent as early data.
1112      */
1113     uint32_t max_early_data;
1114 
1115     /*
1116      * The maximum number of bytes of early data that a server will tolerate
1117      * (which should be at least as much as max_early_data).
1118      */
1119     uint32_t recv_max_early_data;
1120 
1121     /* TLS1.3 padding callback */
1122     size_t (*record_padding_cb)(SSL *s, int type, size_t len, void *arg);
1123     void *record_padding_arg;
1124     size_t block_padding;
1125     size_t hs_padding;
1126 
1127     /* Session ticket appdata */
1128     SSL_CTX_generate_session_ticket_fn generate_ticket_cb;
1129     SSL_CTX_decrypt_session_ticket_fn decrypt_ticket_cb;
1130     void *ticket_cb_data;
1131 
1132     /* The number of TLS1.3 tickets to automatically send */
1133     size_t num_tickets;
1134 
1135     /* Callback to determine if early_data is acceptable or not */
1136     SSL_allow_early_data_cb_fn allow_early_data_cb;
1137     void *allow_early_data_cb_data;
1138 
1139     /* Do we advertise Post-handshake auth support? */
1140     int pha_enabled;
1141 
1142     /* Callback for SSL async handling */
1143     SSL_async_callback_fn async_cb;
1144     void *async_cb_arg;
1145 
1146     char *propq;
1147 
1148     int ssl_mac_pkey_id[SSL_MD_NUM_IDX];
1149     const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX];
1150     const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX];
1151     size_t ssl_mac_secret_size[SSL_MD_NUM_IDX];
1152 
1153     size_t tls12_sigalgs_len;
1154     /* Cache of all sigalgs we know and whether they are available or not */
1155     struct sigalg_lookup_st *sigalg_lookup_cache;
1156     /* List of all sigalgs (code points) available, incl. from providers */
1157     uint16_t *tls12_sigalgs;
1158 
1159     TLS_GROUP_INFO *group_list;
1160     size_t group_list_len;
1161     size_t group_list_max_len;
1162 
1163     TLS_SIGALG_INFO *sigalg_list;
1164     size_t sigalg_list_len;
1165     size_t sigalg_list_max_len;
1166 
1167     /* masks of disabled algorithms */
1168     uint32_t disabled_enc_mask;
1169     uint32_t disabled_mac_mask;
1170     uint32_t disabled_mkey_mask;
1171     uint32_t disabled_auth_mask;
1172 
1173 #ifndef OPENSSL_NO_COMP_ALG
1174     /* certificate compression preferences */
1175     int cert_comp_prefs[TLSEXT_comp_cert_limit];
1176 #endif
1177 
1178     /* Certificate Type stuff - for RPK vs X.509 */
1179     unsigned char *client_cert_type;
1180     size_t client_cert_type_len;
1181     unsigned char *server_cert_type;
1182     size_t server_cert_type_len;
1183 
1184 # ifndef OPENSSL_NO_QLOG
1185     char *qlog_title; /* Session title for qlog */
1186 # endif
1187 };
1188 
1189 typedef struct cert_pkey_st CERT_PKEY;
1190 
1191 #define SSL_TYPE_SSL_CONNECTION  0
1192 #define SSL_TYPE_QUIC_CONNECTION 1
1193 #define SSL_TYPE_QUIC_XSO        2
1194 
1195 struct ssl_st {
1196     int type;
1197     SSL_CTX *ctx;
1198     const SSL_METHOD *defltmeth;
1199     const SSL_METHOD *method;
1200     CRYPTO_REF_COUNT references;
1201     CRYPTO_RWLOCK *lock;
1202     /* extra application data */
1203     CRYPTO_EX_DATA ex_data;
1204 };
1205 
1206 struct ssl_connection_st {
1207     /* type identifier and common data */
1208     struct ssl_st ssl;
1209 
1210     /*
1211      * The actual end user's SSL object. Could be different to this one for
1212      * QUIC
1213      */
1214     SSL *user_ssl;
1215 
1216     /*
1217      * protocol version (one of SSL2_VERSION, SSL3_VERSION, TLS1_VERSION,
1218      * DTLS1_VERSION)
1219      */
1220     int version;
1221     /*
1222      * There are 2 BIO's even though they are normally both the same.  This
1223      * is so data can be read and written to different handlers
1224      */
1225     /* used by SSL_read */
1226     BIO *rbio;
1227     /* used by SSL_write */
1228     BIO *wbio;
1229     /* used during session-id reuse to concatenate messages */
1230     BIO *bbio;
1231     /*
1232      * This holds a variable that indicates what we were doing when a 0 or -1
1233      * is returned.  This is needed for non-blocking IO so we know what
1234      * request needs re-doing when in SSL_accept or SSL_connect
1235      */
1236     int rwstate;
1237     int (*handshake_func) (SSL *);
1238     /*
1239      * Imagine that here's a boolean member "init" that is switched as soon
1240      * as SSL_set_{accept/connect}_state is called for the first time, so
1241      * that "state" and "handshake_func" are properly initialized.  But as
1242      * handshake_func is == 0 until then, we use this test instead of an
1243      * "init" member.
1244      */
1245     /* are we the server side? */
1246     int server;
1247     /*
1248      * Generate a new session or reuse an old one.
1249      * NB: For servers, the 'new' session may actually be a previously
1250      * cached session or even the previous session unless
1251      * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set
1252      */
1253     int new_session;
1254     /* don't send shutdown packets */
1255     int quiet_shutdown;
1256     /* we have shut things down, 0x01 sent, 0x02 for received */
1257     int shutdown;
1258     /* Timestamps used to calculate the handshake RTT */
1259     OSSL_TIME ts_msg_write;
1260     OSSL_TIME ts_msg_read;
1261     /* where we are */
1262     OSSL_STATEM statem;
1263     SSL_EARLY_DATA_STATE early_data_state;
1264     BUF_MEM *init_buf;          /* buffer used during init */
1265     void *init_msg;             /* pointer to handshake message body, set by
1266                                  * tls_get_message_header() */
1267     size_t init_num;               /* amount read/written */
1268     size_t init_off;               /* amount read/written */
1269 
1270     size_t ssl_pkey_num;
1271 
1272     struct {
1273         long flags;
1274         unsigned char server_random[SSL3_RANDOM_SIZE];
1275         unsigned char client_random[SSL3_RANDOM_SIZE];
1276 
1277         /* used during startup, digest all incoming/outgoing packets */
1278         BIO *handshake_buffer;
1279         /*
1280          * When handshake digest is determined, buffer is hashed and
1281          * freed and MD_CTX for the required digest is stored here.
1282          */
1283         EVP_MD_CTX *handshake_dgst;
1284         /*
1285          * Set whenever an expected ChangeCipherSpec message is processed.
1286          * Unset when the peer's Finished message is received.
1287          * Unexpected ChangeCipherSpec messages trigger a fatal alert.
1288          */
1289         int change_cipher_spec;
1290         int warn_alert;
1291         int fatal_alert;
1292         /*
1293          * we allow one fatal and one warning alert to be outstanding, send close
1294          * alert via the warning alert
1295          */
1296         int alert_dispatch;
1297         unsigned char send_alert[2];
1298         /*
1299          * This flag is set when we should renegotiate ASAP, basically when there
1300          * is no more data in the read or write buffers
1301          */
1302         int renegotiate;
1303         int total_renegotiations;
1304         int num_renegotiations;
1305         int in_read_app_data;
1306 
1307         struct {
1308             /* actually only need to be 16+20 for SSLv3 and 12 for TLS */
1309             unsigned char finish_md[EVP_MAX_MD_SIZE * 2];
1310             size_t finish_md_len;
1311             unsigned char peer_finish_md[EVP_MAX_MD_SIZE * 2];
1312             size_t peer_finish_md_len;
1313             size_t message_size;
1314             int message_type;
1315             /* used to hold the new cipher we are going to use */
1316             const SSL_CIPHER *new_cipher;
1317             EVP_PKEY *pkey;         /* holds short lived key exchange key */
1318             /* used for certificate requests */
1319             int cert_req;
1320             /* Certificate types in certificate request message. */
1321             uint8_t *ctype;
1322             size_t ctype_len;
1323             /* Certificate authorities list peer sent */
1324             STACK_OF(X509_NAME) *peer_ca_names;
1325             size_t key_block_length;
1326             unsigned char *key_block;
1327             const EVP_CIPHER *new_sym_enc;
1328             const EVP_MD *new_hash;
1329             int new_mac_pkey_type;
1330             size_t new_mac_secret_size;
1331 # ifndef OPENSSL_NO_COMP
1332             const SSL_COMP *new_compression;
1333 # else
1334             char *new_compression;
1335 # endif
1336             int cert_request;
1337             /* Raw values of the cipher list from a client */
1338             unsigned char *ciphers_raw;
1339             size_t ciphers_rawlen;
1340             /* Temporary storage for premaster secret */
1341             unsigned char *pms;
1342             size_t pmslen;
1343 # ifndef OPENSSL_NO_PSK
1344             /* Temporary storage for PSK key */
1345             unsigned char *psk;
1346             size_t psklen;
1347 # endif
1348             /* Signature algorithm we actually use */
1349             const struct sigalg_lookup_st *sigalg;
1350             /* Pointer to certificate we use */
1351             CERT_PKEY *cert;
1352             /*
1353              * signature algorithms peer reports: e.g. supported signature
1354              * algorithms extension for server or as part of a certificate
1355              * request for client.
1356              * Keep track of the algorithms for TLS and X.509 usage separately.
1357              */
1358             uint16_t *peer_sigalgs;
1359             uint16_t *peer_cert_sigalgs;
1360             /* Size of above arrays */
1361             size_t peer_sigalgslen;
1362             size_t peer_cert_sigalgslen;
1363             /* Sigalg peer actually uses */
1364             const struct sigalg_lookup_st *peer_sigalg;
1365             /*
1366              * Set if corresponding CERT_PKEY can be used with current
1367              * SSL session: e.g. appropriate curve, signature algorithms etc.
1368              * If zero it can't be used at all.
1369              */
1370             uint32_t *valid_flags;
1371             /*
1372              * For servers the following masks are for the key and auth algorithms
1373              * that are supported by the certs below. For clients they are masks of
1374              * *disabled* algorithms based on the current session.
1375              */
1376             uint32_t mask_k;
1377             uint32_t mask_a;
1378             /*
1379              * The following are used by the client to see if a cipher is allowed or
1380              * not.  It contains the minimum and maximum version the client's using
1381              * based on what it knows so far.
1382              */
1383             int min_ver;
1384             int max_ver;
1385         } tmp;
1386 
1387         /* Connection binding to prevent renegotiation attacks */
1388         unsigned char previous_client_finished[EVP_MAX_MD_SIZE];
1389         size_t previous_client_finished_len;
1390         unsigned char previous_server_finished[EVP_MAX_MD_SIZE];
1391         size_t previous_server_finished_len;
1392         int send_connection_binding;
1393 
1394 # ifndef OPENSSL_NO_NEXTPROTONEG
1395         /*
1396          * Set if we saw the Next Protocol Negotiation extension from our peer.
1397          */
1398         int npn_seen;
1399 # endif
1400 
1401         /*
1402          * ALPN information (we are in the process of transitioning from NPN to
1403          * ALPN.)
1404          */
1405 
1406         /*
1407          * In a server these point to the selected ALPN protocol after the
1408          * ClientHello has been processed. In a client these contain the protocol
1409          * that the server selected once the ServerHello has been processed.
1410          */
1411         unsigned char *alpn_selected;
1412         size_t alpn_selected_len;
1413         /* used by the server to know what options were proposed */
1414         unsigned char *alpn_proposed;
1415         size_t alpn_proposed_len;
1416         /* used by the client to know if it actually sent alpn */
1417         int alpn_sent;
1418 
1419         /*
1420          * This is set to true if we believe that this is a version of Safari
1421          * running on OS X 10.6 or newer. We wish to know this because Safari on
1422          * 10.8 .. 10.8.3 has broken ECDHE-ECDSA support.
1423          */
1424         char is_probably_safari;
1425 
1426         /*
1427          * Track whether we did a key exchange this handshake or not, so
1428          * SSL_get_negotiated_group() knows whether to fall back to the
1429          * value in the SSL_SESSION.
1430          */
1431         char did_kex;
1432         /* For clients: peer temporary key */
1433         /* The group_id for the key exchange key */
1434         uint16_t group_id;
1435         EVP_PKEY *peer_tmp;
1436 
1437     } s3;
1438 
1439     struct dtls1_state_st *d1;  /* DTLSv1 variables */
1440     /* callback that allows applications to peek at protocol messages */
1441     void (*msg_callback) (int write_p, int version, int content_type,
1442                           const void *buf, size_t len, SSL *ssl, void *arg);
1443     void *msg_callback_arg;
1444     int hit;                    /* reusing a previous session */
1445     X509_VERIFY_PARAM *param;
1446     /* Per connection DANE state */
1447     SSL_DANE dane;
1448     /* crypto */
1449     STACK_OF(SSL_CIPHER) *peer_ciphers;
1450     STACK_OF(SSL_CIPHER) *cipher_list;
1451     STACK_OF(SSL_CIPHER) *cipher_list_by_id;
1452     /* TLSv1.3 specific ciphersuites */
1453     STACK_OF(SSL_CIPHER) *tls13_ciphersuites;
1454     /*
1455      * These are the ones being used, the ones in SSL_SESSION are the ones to
1456      * be 'copied' into these ones
1457      */
1458     uint32_t mac_flags;
1459     /*
1460      * The TLS1.3 secrets.
1461      */
1462     unsigned char early_secret[EVP_MAX_MD_SIZE];
1463     unsigned char handshake_secret[EVP_MAX_MD_SIZE];
1464     unsigned char master_secret[EVP_MAX_MD_SIZE];
1465     unsigned char resumption_master_secret[EVP_MAX_MD_SIZE];
1466     unsigned char client_finished_secret[EVP_MAX_MD_SIZE];
1467     unsigned char server_finished_secret[EVP_MAX_MD_SIZE];
1468     unsigned char server_finished_hash[EVP_MAX_MD_SIZE];
1469     unsigned char handshake_traffic_hash[EVP_MAX_MD_SIZE];
1470     unsigned char client_app_traffic_secret[EVP_MAX_MD_SIZE];
1471     unsigned char server_app_traffic_secret[EVP_MAX_MD_SIZE];
1472     unsigned char exporter_master_secret[EVP_MAX_MD_SIZE];
1473     unsigned char early_exporter_master_secret[EVP_MAX_MD_SIZE];
1474 
1475     /* session info */
1476     /* client cert? */
1477     /* This is used to hold the server certificate used */
1478     struct cert_st /* CERT */ *cert;
1479 
1480     /*
1481      * The hash of all messages prior to the CertificateVerify, and the length
1482      * of that hash.
1483      */
1484     unsigned char cert_verify_hash[EVP_MAX_MD_SIZE];
1485     size_t cert_verify_hash_len;
1486 
1487     /* Flag to indicate whether we should send a HelloRetryRequest or not */
1488     enum {SSL_HRR_NONE = 0, SSL_HRR_PENDING, SSL_HRR_COMPLETE}
1489         hello_retry_request;
1490 
1491     /*
1492      * the session_id_context is used to ensure sessions are only reused in
1493      * the appropriate context
1494      */
1495     size_t sid_ctx_length;
1496     unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH];
1497     /* This can also be in the session once a session is established */
1498     SSL_SESSION *session;
1499     /* TLSv1.3 PSK session */
1500     SSL_SESSION *psksession;
1501     unsigned char *psksession_id;
1502     size_t psksession_id_len;
1503     /* Default generate session ID callback. */
1504     GEN_SESSION_CB generate_session_id;
1505     /*
1506      * The temporary TLSv1.3 session id. This isn't really a session id at all
1507      * but is a random value sent in the legacy session id field.
1508      */
1509     unsigned char tmp_session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
1510     size_t tmp_session_id_len;
1511     /* Used in SSL3 */
1512     /*
1513      * 0 don't care about verify failure.
1514      * 1 fail if verify fails
1515      */
1516     uint32_t verify_mode;
1517     /* fail if callback returns 0 */
1518     int (*verify_callback) (int ok, X509_STORE_CTX *ctx);
1519     /* optional informational callback */
1520     void (*info_callback) (const SSL *ssl, int type, int val);
1521     /* error bytes to be written */
1522     int error;
1523     /* actual code */
1524     int error_code;
1525 # ifndef OPENSSL_NO_PSK
1526     SSL_psk_client_cb_func psk_client_callback;
1527     SSL_psk_server_cb_func psk_server_callback;
1528 # endif
1529     SSL_psk_find_session_cb_func psk_find_session_cb;
1530     SSL_psk_use_session_cb_func psk_use_session_cb;
1531 
1532     /* Verified chain of peer */
1533     STACK_OF(X509) *verified_chain;
1534     long verify_result;
1535     /*
1536      * What we put in certificate_authorities extension for TLS 1.3
1537      * (ClientHello and CertificateRequest) or just client cert requests for
1538      * earlier versions. If client_ca_names is populated then it is only used
1539      * for client cert requests, and in preference to ca_names.
1540      */
1541     STACK_OF(X509_NAME) *ca_names;
1542     STACK_OF(X509_NAME) *client_ca_names;
1543     /* protocol behaviour */
1544     uint64_t options;
1545     /* API behaviour */
1546     uint32_t mode;
1547     int min_proto_version;
1548     int max_proto_version;
1549     size_t max_cert_list;
1550     int first_packet;
1551     /*
1552      * What was passed in ClientHello.legacy_version. Used for RSA pre-master
1553      * secret and SSLv3/TLS (<=1.2) rollback check
1554      */
1555     int client_version;
1556     /*
1557      * If we're using more than one pipeline how should we divide the data
1558      * up between the pipes?
1559      */
1560     size_t split_send_fragment;
1561     /*
1562      * Maximum amount of data to send in one fragment. actual record size can
1563      * be more than this due to padding and MAC overheads.
1564      */
1565     size_t max_send_fragment;
1566     /* Up to how many pipelines should we use? If 0 then 1 is assumed */
1567     size_t max_pipelines;
1568 
1569     struct {
1570         /* Built-in extension flags */
1571         uint8_t extflags[TLSEXT_IDX_num_builtins];
1572         /* TLS extension debug callback */
1573         void (*debug_cb)(SSL *s, int client_server, int type,
1574                          const unsigned char *data, int len, void *arg);
1575         void *debug_arg;
1576         char *hostname;
1577         /* certificate status request info */
1578         /* Status type or -1 if no status type */
1579         int status_type;
1580         /* Raw extension data, if seen */
1581         unsigned char *scts;
1582         /* Length of raw extension data, if seen */
1583         uint16_t scts_len;
1584         /* Expect OCSP CertificateStatus message */
1585         int status_expected;
1586 
1587         struct {
1588             /* OCSP status request only */
1589             STACK_OF(OCSP_RESPID) *ids;
1590             X509_EXTENSIONS *exts;
1591             /* OCSP response received or to be sent */
1592             unsigned char *resp;
1593             size_t resp_len;
1594         } ocsp;
1595 
1596         /* RFC4507 session ticket expected to be received or sent */
1597         int ticket_expected;
1598         /* TLS 1.3 tickets requested by the application. */
1599         int extra_tickets_expected;
1600         size_t ecpointformats_len;
1601         /* our list */
1602         unsigned char *ecpointformats;
1603 
1604         size_t peer_ecpointformats_len;
1605         /* peer's list */
1606         unsigned char *peer_ecpointformats;
1607         size_t supportedgroups_len;
1608         /* our list */
1609         uint16_t *supportedgroups;
1610 
1611         size_t peer_supportedgroups_len;
1612          /* peer's list */
1613         uint16_t *peer_supportedgroups;
1614 
1615         /* TLS Session Ticket extension override */
1616         TLS_SESSION_TICKET_EXT *session_ticket;
1617         /* TLS Session Ticket extension callback */
1618         tls_session_ticket_ext_cb_fn session_ticket_cb;
1619         void *session_ticket_cb_arg;
1620         /* TLS pre-shared secret session resumption */
1621         tls_session_secret_cb_fn session_secret_cb;
1622         void *session_secret_cb_arg;
1623         /*
1624          * For a client, this contains the list of supported protocols in wire
1625          * format.
1626          */
1627         unsigned char *alpn;
1628         size_t alpn_len;
1629         /*
1630          * Next protocol negotiation. For the client, this is the protocol that
1631          * we sent in NextProtocol and is set when handling ServerHello
1632          * extensions. For a server, this is the client's selected_protocol from
1633          * NextProtocol and is set when handling the NextProtocol message, before
1634          * the Finished message.
1635          */
1636         unsigned char *npn;
1637         size_t npn_len;
1638 
1639         /* The available PSK key exchange modes */
1640         int psk_kex_mode;
1641 
1642         /* Set to one if we have negotiated ETM */
1643         int use_etm;
1644 
1645         /* Are we expecting to receive early data? */
1646         int early_data;
1647         /* Is the session suitable for early data? */
1648         int early_data_ok;
1649 
1650         /* May be sent by a server in HRR. Must be echoed back in ClientHello */
1651         unsigned char *tls13_cookie;
1652         size_t tls13_cookie_len;
1653         /* Have we received a cookie from the client? */
1654         int cookieok;
1655 
1656         /*
1657          * Maximum Fragment Length as per RFC 4366.
1658          * If this member contains one of the allowed values (1-4)
1659          * then we should include Maximum Fragment Length Negotiation
1660          * extension in Client Hello.
1661          * Please note that value of this member does not have direct
1662          * effect. The actual (binding) value is stored in SSL_SESSION,
1663          * as this extension is optional on server side.
1664          */
1665         uint8_t max_fragment_len_mode;
1666 
1667         /*
1668          * On the client side the number of ticket identities we sent in the
1669          * ClientHello. On the server side the identity of the ticket we
1670          * selected.
1671          */
1672         int tick_identity;
1673 
1674         /* This is the list of algorithms the peer supports that we also support */
1675         int compress_certificate_from_peer[TLSEXT_comp_cert_limit];
1676         /* indicate that we sent the extension, so we'll accept it */
1677         int compress_certificate_sent;
1678 
1679         uint8_t client_cert_type;
1680         uint8_t client_cert_type_ctos;
1681         uint8_t server_cert_type;
1682         uint8_t server_cert_type_ctos;
1683     } ext;
1684 
1685     /*
1686      * Parsed form of the ClientHello, kept around across client_hello_cb
1687      * calls.
1688      */
1689     CLIENTHELLO_MSG *clienthello;
1690 
1691     /*-
1692      * no further mod of servername
1693      * 0 : call the servername extension callback.
1694      * 1 : prepare 2, allow last ack just after in server callback.
1695      * 2 : don't call servername callback, no ack in server hello
1696      */
1697     int servername_done;
1698 # ifndef OPENSSL_NO_CT
1699     /*
1700      * Validates that the SCTs (Signed Certificate Timestamps) are sufficient.
1701      * If they are not, the connection should be aborted.
1702      */
1703     ssl_ct_validation_cb ct_validation_callback;
1704     /* User-supplied argument that is passed to the ct_validation_callback */
1705     void *ct_validation_callback_arg;
1706     /*
1707      * Consolidated stack of SCTs from all sources.
1708      * Lazily populated by CT_get_peer_scts(SSL*)
1709      */
1710     STACK_OF(SCT) *scts;
1711     /* Have we attempted to find/parse SCTs yet? */
1712     int scts_parsed;
1713 # endif
1714     SSL_CTX *session_ctx;       /* initial ctx, used to store sessions */
1715 # ifndef OPENSSL_NO_SRTP
1716     /* What we'll do */
1717     STACK_OF(SRTP_PROTECTION_PROFILE) *srtp_profiles;
1718     /* What's been chosen */
1719     SRTP_PROTECTION_PROFILE *srtp_profile;
1720 # endif
1721     /*-
1722      * 1 if we are renegotiating.
1723      * 2 if we are a server and are inside a handshake
1724      * (i.e. not just sending a HelloRequest)
1725      */
1726     int renegotiate;
1727     /* If sending a KeyUpdate is pending */
1728     int key_update;
1729     /* Post-handshake authentication state */
1730     SSL_PHA_STATE post_handshake_auth;
1731     int pha_enabled;
1732     uint8_t* pha_context;
1733     size_t pha_context_len;
1734     int certreqs_sent;
1735     EVP_MD_CTX *pha_dgst; /* this is just the digest through ClientFinished */
1736 
1737 # ifndef OPENSSL_NO_SRP
1738     /* ctx for SRP authentication */
1739     SRP_CTX srp_ctx;
1740 # endif
1741     /*
1742      * Callback for disabling session caching and ticket support on a session
1743      * basis, depending on the chosen cipher.
1744      */
1745     int (*not_resumable_session_cb) (SSL *ssl, int is_forward_secure);
1746 
1747     /* Record layer data */
1748     RECORD_LAYER rlayer;
1749 
1750     /* Default password callback. */
1751     pem_password_cb *default_passwd_callback;
1752     /* Default password callback user data. */
1753     void *default_passwd_callback_userdata;
1754     /* Async Job info */
1755     ASYNC_JOB *job;
1756     ASYNC_WAIT_CTX *waitctx;
1757     size_t asyncrw;
1758 
1759     /*
1760      * The maximum number of bytes advertised in session tickets that can be
1761      * sent as early data.
1762      */
1763     uint32_t max_early_data;
1764     /*
1765      * The maximum number of bytes of early data that a server will tolerate
1766      * (which should be at least as much as max_early_data).
1767      */
1768     uint32_t recv_max_early_data;
1769 
1770     /*
1771      * The number of bytes of early data received so far. If we accepted early
1772      * data then this is a count of the plaintext bytes. If we rejected it then
1773      * this is a count of the ciphertext bytes.
1774      */
1775     uint32_t early_data_count;
1776 
1777     /* The number of TLS1.3 tickets to automatically send */
1778     size_t num_tickets;
1779     /* The number of TLS1.3 tickets actually sent so far */
1780     size_t sent_tickets;
1781     /* The next nonce value to use when we send a ticket on this connection */
1782     uint64_t next_ticket_nonce;
1783 
1784     /* Callback to determine if early_data is acceptable or not */
1785     SSL_allow_early_data_cb_fn allow_early_data_cb;
1786     void *allow_early_data_cb_data;
1787 
1788     /* Callback for SSL async handling */
1789     SSL_async_callback_fn async_cb;
1790     void *async_cb_arg;
1791 
1792     /*
1793      * Signature algorithms shared by client and server: cached because these
1794      * are used most often.
1795      */
1796     const struct sigalg_lookup_st **shared_sigalgs;
1797     size_t shared_sigalgslen;
1798 
1799 #ifndef OPENSSL_NO_COMP_ALG
1800     /* certificate compression preferences */
1801     int cert_comp_prefs[TLSEXT_comp_cert_limit];
1802 #endif
1803 
1804     /* Certificate Type stuff - for RPK vs X.509 */
1805     unsigned char *client_cert_type;
1806     size_t client_cert_type_len;
1807     unsigned char *server_cert_type;
1808     size_t server_cert_type_len;
1809 };
1810 
1811 # define SSL_CONNECTION_FROM_SSL_ONLY_int(ssl, c) \
1812     ((ssl) == NULL ? NULL                         \
1813      : ((ssl)->type == SSL_TYPE_SSL_CONNECTION    \
1814        ? (c SSL_CONNECTION *)(ssl)                \
1815        : NULL))
1816 # define SSL_CONNECTION_NO_CONST
1817 # define SSL_CONNECTION_FROM_SSL_ONLY(ssl) \
1818     SSL_CONNECTION_FROM_SSL_ONLY_int(ssl, SSL_CONNECTION_NO_CONST)
1819 # define SSL_CONNECTION_FROM_CONST_SSL_ONLY(ssl) \
1820     SSL_CONNECTION_FROM_SSL_ONLY_int(ssl, const)
1821 # define SSL_CONNECTION_GET_CTX(sc) ((sc)->ssl.ctx)
1822 # define SSL_CONNECTION_GET_SSL(sc) (&(sc)->ssl)
1823 # define SSL_CONNECTION_GET_USER_SSL(sc) ((sc)->user_ssl)
1824 # ifndef OPENSSL_NO_QUIC
1825 #  include "quic/quic_local.h"
1826 #  define SSL_CONNECTION_FROM_SSL_int(ssl, c)                      \
1827     ((ssl) == NULL ? NULL                                          \
1828      : ((ssl)->type == SSL_TYPE_SSL_CONNECTION                     \
1829         ? (c SSL_CONNECTION *)(ssl)                                \
1830         : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION                 \
1831            ? (c SSL_CONNECTION *)((c QUIC_CONNECTION *)(ssl))->tls \
1832            : NULL)))
1833 #  define SSL_CONNECTION_FROM_SSL(ssl) \
1834     SSL_CONNECTION_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
1835 #  define SSL_CONNECTION_FROM_CONST_SSL(ssl) \
1836     SSL_CONNECTION_FROM_SSL_int(ssl, const)
1837 # else
1838 #  define SSL_CONNECTION_FROM_SSL(ssl) \
1839     SSL_CONNECTION_FROM_SSL_ONLY_int(ssl, SSL_CONNECTION_NO_CONST)
1840 #  define SSL_CONNECTION_FROM_CONST_SSL(ssl) \
1841     SSL_CONNECTION_FROM_SSL_ONLY_int(ssl, const)
1842 # endif
1843 
1844 /*
1845  * Structure containing table entry of values associated with the signature
1846  * algorithms (signature scheme) extension
1847 */
1848 typedef struct sigalg_lookup_st {
1849     /* TLS 1.3 signature scheme name */
1850     const char *name;
1851     /* Raw value used in extension */
1852     uint16_t sigalg;
1853     /* NID of hash algorithm or NID_undef if no hash */
1854     int hash;
1855     /* Index of hash algorithm or -1 if no hash algorithm */
1856     int hash_idx;
1857     /* NID of signature algorithm */
1858     int sig;
1859     /* Index of signature algorithm */
1860     int sig_idx;
1861     /* Combined hash and signature NID, if any */
1862     int sigandhash;
1863     /* Required public key curve (ECDSA only) */
1864     int curve;
1865     /* Whether this signature algorithm is actually available for use */
1866     int enabled;
1867 } SIGALG_LOOKUP;
1868 
1869 /* DTLS structures */
1870 
1871 # ifndef OPENSSL_NO_SCTP
1872 #  define DTLS1_SCTP_AUTH_LABEL   "EXPORTER_DTLS_OVER_SCTP"
1873 # endif
1874 
1875 /* Max MTU overhead we know about so far is 40 for IPv6 + 8 for UDP */
1876 # define DTLS1_MAX_MTU_OVERHEAD                   48
1877 
1878 struct dtls1_retransmit_state {
1879     const OSSL_RECORD_METHOD *wrlmethod;
1880     OSSL_RECORD_LAYER *wrl;
1881 };
1882 
1883 struct hm_header_st {
1884     unsigned char type;
1885     size_t msg_len;
1886     unsigned short seq;
1887     size_t frag_off;
1888     size_t frag_len;
1889     unsigned int is_ccs;
1890     struct dtls1_retransmit_state saved_retransmit_state;
1891 };
1892 
1893 typedef struct hm_fragment_st {
1894     struct hm_header_st msg_header;
1895     unsigned char *fragment;
1896     unsigned char *reassembly;
1897 } hm_fragment;
1898 
1899 typedef struct pqueue_st pqueue;
1900 typedef struct pitem_st pitem;
1901 
1902 struct pitem_st {
1903     unsigned char priority[8];  /* 64-bit value in big-endian encoding */
1904     void *data;
1905     pitem *next;
1906 };
1907 
1908 typedef struct pitem_st *piterator;
1909 
1910 pitem *pitem_new(unsigned char *prio64be, void *data);
1911 void pitem_free(pitem *item);
1912 pqueue *pqueue_new(void);
1913 void pqueue_free(pqueue *pq);
1914 pitem *pqueue_insert(pqueue *pq, pitem *item);
1915 pitem *pqueue_peek(pqueue *pq);
1916 pitem *pqueue_pop(pqueue *pq);
1917 pitem *pqueue_find(pqueue *pq, unsigned char *prio64be);
1918 pitem *pqueue_iterator(pqueue *pq);
1919 pitem *pqueue_next(piterator *iter);
1920 size_t pqueue_size(pqueue *pq);
1921 
1922 typedef struct dtls1_state_st {
1923     unsigned char cookie[DTLS1_COOKIE_LENGTH];
1924     size_t cookie_len;
1925     unsigned int cookie_verified;
1926     /* handshake message numbers */
1927     unsigned short handshake_write_seq;
1928     unsigned short next_handshake_write_seq;
1929     unsigned short handshake_read_seq;
1930     /* Buffered handshake messages */
1931     pqueue *buffered_messages;
1932     /* Buffered (sent) handshake records */
1933     pqueue *sent_messages;
1934     size_t link_mtu;      /* max on-the-wire DTLS packet size */
1935     size_t mtu;           /* max DTLS packet size */
1936     struct hm_header_st w_msg_hdr;
1937     struct hm_header_st r_msg_hdr;
1938     /* Number of alerts received so far */
1939     unsigned int timeout_num_alerts;
1940     /*
1941      * Indicates when the last handshake msg sent will timeout
1942      */
1943     OSSL_TIME next_timeout;
1944     /* Timeout duration */
1945     unsigned int timeout_duration_us;
1946 
1947     unsigned int retransmitting;
1948 # ifndef OPENSSL_NO_SCTP
1949     int shutdown_received;
1950 # endif
1951 
1952     DTLS_timer_cb timer_cb;
1953 
1954 } DTLS1_STATE;
1955 
1956 /*
1957  * From ECC-TLS draft, used in encoding the curve type in ECParameters
1958  */
1959 #  define EXPLICIT_PRIME_CURVE_TYPE  1
1960 #  define EXPLICIT_CHAR2_CURVE_TYPE  2
1961 #  define NAMED_CURVE_TYPE           3
1962 
1963 # ifndef OPENSSL_NO_COMP_ALG
1964 struct ossl_comp_cert_st {
1965     unsigned char *data;
1966     size_t len;
1967     size_t orig_len;
1968     CRYPTO_REF_COUNT references;
1969     int alg;
1970 };
1971 typedef struct ossl_comp_cert_st OSSL_COMP_CERT;
1972 
1973 void OSSL_COMP_CERT_free(OSSL_COMP_CERT *c);
1974 int OSSL_COMP_CERT_up_ref(OSSL_COMP_CERT *c);
1975 # endif
1976 
1977 struct cert_pkey_st {
1978     X509 *x509;
1979     EVP_PKEY *privatekey;
1980     /* Chain for this certificate */
1981     STACK_OF(X509) *chain;
1982     /*-
1983      * serverinfo data for this certificate.  The data is in TLS Extension
1984      * wire format, specifically it's a series of records like:
1985      *   uint16_t extension_type; // (RFC 5246, 7.4.1.4, Extension)
1986      *   uint16_t length;
1987      *   uint8_t data[length];
1988      */
1989     unsigned char *serverinfo;
1990     size_t serverinfo_length;
1991 # ifndef OPENSSL_NO_COMP_ALG
1992     /* Compressed certificate data - index 0 is unused */
1993     OSSL_COMP_CERT *comp_cert[TLSEXT_comp_cert_limit];
1994     int cert_comp_used;
1995 # endif
1996 };
1997 /* Retrieve Suite B flags */
1998 # define tls1_suiteb(s)  (s->cert->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS)
1999 /* Uses to check strict mode: suite B modes are always strict */
2000 # define SSL_CERT_FLAGS_CHECK_TLS_STRICT \
2001         (SSL_CERT_FLAG_SUITEB_128_LOS|SSL_CERT_FLAG_TLS_STRICT)
2002 
2003 typedef enum {
2004     ENDPOINT_CLIENT = 0,
2005     ENDPOINT_SERVER,
2006     ENDPOINT_BOTH
2007 } ENDPOINT;
2008 
2009 
2010 typedef struct {
2011     unsigned short ext_type;
2012     ENDPOINT role;
2013     /* The context which this extension applies to */
2014     unsigned int context;
2015     /*
2016      * Per-connection flags relating to this extension type: not used if
2017      * part of an SSL_CTX structure.
2018      */
2019     uint32_t ext_flags;
2020     SSL_custom_ext_add_cb_ex add_cb;
2021     SSL_custom_ext_free_cb_ex free_cb;
2022     void *add_arg;
2023     SSL_custom_ext_parse_cb_ex parse_cb;
2024     void *parse_arg;
2025 } custom_ext_method;
2026 
2027 /* ext_flags values */
2028 
2029 /*
2030  * Indicates an extension has been received. Used to check for unsolicited or
2031  * duplicate extensions.
2032  */
2033 # define SSL_EXT_FLAG_RECEIVED   0x1
2034 /*
2035  * Indicates an extension has been sent: used to enable sending of
2036  * corresponding ServerHello extension.
2037  */
2038 # define SSL_EXT_FLAG_SENT       0x2
2039 
2040 typedef struct {
2041     custom_ext_method *meths;
2042     size_t meths_count;
2043 } custom_ext_methods;
2044 
2045 typedef struct cert_st {
2046     /* Current active set */
2047     /*
2048      * ALWAYS points to an element of the pkeys array
2049      * Probably it would make more sense to store
2050      * an index, not a pointer.
2051      */
2052     CERT_PKEY *key;
2053 
2054     EVP_PKEY *dh_tmp;
2055     DH *(*dh_tmp_cb) (SSL *ssl, int is_export, int keysize);
2056     int dh_tmp_auto;
2057     /* Flags related to certificates */
2058     uint32_t cert_flags;
2059     CERT_PKEY *pkeys;
2060     size_t ssl_pkey_num;
2061     /* Custom certificate types sent in certificate request message. */
2062     uint8_t *ctype;
2063     size_t ctype_len;
2064     /*
2065      * supported signature algorithms. When set on a client this is sent in
2066      * the client hello as the supported signature algorithms extension. For
2067      * servers it represents the signature algorithms we are willing to use.
2068      */
2069     uint16_t *conf_sigalgs;
2070     /* Size of above array */
2071     size_t conf_sigalgslen;
2072     /*
2073      * Client authentication signature algorithms, if not set then uses
2074      * conf_sigalgs. On servers these will be the signature algorithms sent
2075      * to the client in a certificate request for TLS 1.2. On a client this
2076      * represents the signature algorithms we are willing to use for client
2077      * authentication.
2078      */
2079     uint16_t *client_sigalgs;
2080     /* Size of above array */
2081     size_t client_sigalgslen;
2082     /*
2083      * Certificate setup callback: if set is called whenever a certificate
2084      * may be required (client or server). the callback can then examine any
2085      * appropriate parameters and setup any certificates required. This
2086      * allows advanced applications to select certificates on the fly: for
2087      * example based on supported signature algorithms or curves.
2088      */
2089     int (*cert_cb) (SSL *ssl, void *arg);
2090     void *cert_cb_arg;
2091     /*
2092      * Optional X509_STORE for chain building or certificate validation If
2093      * NULL the parent SSL_CTX store is used instead.
2094      */
2095     X509_STORE *chain_store;
2096     X509_STORE *verify_store;
2097     /* Custom extensions */
2098     custom_ext_methods custext;
2099     /* Security callback */
2100     int (*sec_cb) (const SSL *s, const SSL_CTX *ctx, int op, int bits, int nid,
2101                    void *other, void *ex);
2102     /* Security level */
2103     int sec_level;
2104     void *sec_ex;
2105 # ifndef OPENSSL_NO_PSK
2106     /* If not NULL psk identity hint to use for servers */
2107     char *psk_identity_hint;
2108 # endif
2109     CRYPTO_REF_COUNT references;             /* >1 only if SSL_copy_session_id is used */
2110 } CERT;
2111 
2112 /*
2113  * This is for the SSLv3/TLSv1.0 differences in crypto/hash stuff It is a bit
2114  * of a mess of functions, but hell, think of it as an opaque structure :-)
2115  */
2116 typedef struct ssl3_enc_method {
2117     int (*setup_key_block) (SSL_CONNECTION *);
2118     int (*generate_master_secret) (SSL_CONNECTION *, unsigned char *,
2119                                    unsigned char *, size_t, size_t *);
2120     int (*change_cipher_state) (SSL_CONNECTION *, int);
2121     size_t (*final_finish_mac) (SSL_CONNECTION *, const char *, size_t,
2122                                 unsigned char *);
2123     const char *client_finished_label;
2124     size_t client_finished_label_len;
2125     const char *server_finished_label;
2126     size_t server_finished_label_len;
2127     int (*alert_value) (int);
2128     int (*export_keying_material) (SSL_CONNECTION *, unsigned char *, size_t,
2129                                    const char *, size_t,
2130                                    const unsigned char *, size_t,
2131                                    int use_context);
2132     /* Various flags indicating protocol version requirements */
2133     uint32_t enc_flags;
2134     /* Set the handshake header */
2135     int (*set_handshake_header) (SSL_CONNECTION *s, WPACKET *pkt, int type);
2136     /* Close construction of the handshake message */
2137     int (*close_construct_packet) (SSL_CONNECTION *s, WPACKET *pkt, int htype);
2138     /* Write out handshake message */
2139     int (*do_write) (SSL_CONNECTION *s);
2140 } SSL3_ENC_METHOD;
2141 
2142 # define ssl_set_handshake_header(s, pkt, htype) \
2143     SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->set_handshake_header((s), (pkt), (htype))
2144 # define ssl_close_construct_packet(s, pkt, htype) \
2145     SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->close_construct_packet((s), (pkt), (htype))
2146 # define ssl_do_write(s)  SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->do_write(s)
2147 
2148 /* Values for enc_flags */
2149 
2150 /* Uses signature algorithms extension */
2151 # define SSL_ENC_FLAG_SIGALGS            0x2
2152 /* Uses SHA256 default PRF */
2153 # define SSL_ENC_FLAG_SHA256_PRF         0x4
2154 /* Is DTLS */
2155 # define SSL_ENC_FLAG_DTLS               0x8
2156 /*
2157  * Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2: may
2158  * apply to others in future.
2159  */
2160 # define SSL_ENC_FLAG_TLS1_2_CIPHERS     0x10
2161 
2162 typedef enum downgrade_en {
2163     DOWNGRADE_NONE,
2164     DOWNGRADE_TO_1_2,
2165     DOWNGRADE_TO_1_1
2166 } DOWNGRADE;
2167 
2168 /*
2169  * Dummy status type for the status_type extension. Indicates no status type
2170  * set
2171  */
2172 #define TLSEXT_STATUSTYPE_nothing  -1
2173 
2174 /* Sigalgs values */
2175 #define TLSEXT_SIGALG_ecdsa_secp256r1_sha256                    0x0403
2176 #define TLSEXT_SIGALG_ecdsa_secp384r1_sha384                    0x0503
2177 #define TLSEXT_SIGALG_ecdsa_secp521r1_sha512                    0x0603
2178 #define TLSEXT_SIGALG_ecdsa_sha224                              0x0303
2179 #define TLSEXT_SIGALG_ecdsa_sha1                                0x0203
2180 #define TLSEXT_SIGALG_rsa_pss_rsae_sha256                       0x0804
2181 #define TLSEXT_SIGALG_rsa_pss_rsae_sha384                       0x0805
2182 #define TLSEXT_SIGALG_rsa_pss_rsae_sha512                       0x0806
2183 #define TLSEXT_SIGALG_rsa_pss_pss_sha256                        0x0809
2184 #define TLSEXT_SIGALG_rsa_pss_pss_sha384                        0x080a
2185 #define TLSEXT_SIGALG_rsa_pss_pss_sha512                        0x080b
2186 #define TLSEXT_SIGALG_rsa_pkcs1_sha256                          0x0401
2187 #define TLSEXT_SIGALG_rsa_pkcs1_sha384                          0x0501
2188 #define TLSEXT_SIGALG_rsa_pkcs1_sha512                          0x0601
2189 #define TLSEXT_SIGALG_rsa_pkcs1_sha224                          0x0301
2190 #define TLSEXT_SIGALG_rsa_pkcs1_sha1                            0x0201
2191 #define TLSEXT_SIGALG_dsa_sha256                                0x0402
2192 #define TLSEXT_SIGALG_dsa_sha384                                0x0502
2193 #define TLSEXT_SIGALG_dsa_sha512                                0x0602
2194 #define TLSEXT_SIGALG_dsa_sha224                                0x0302
2195 #define TLSEXT_SIGALG_dsa_sha1                                  0x0202
2196 #define TLSEXT_SIGALG_gostr34102012_256_intrinsic               0x0840
2197 #define TLSEXT_SIGALG_gostr34102012_512_intrinsic               0x0841
2198 #define TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256       0xeeee
2199 #define TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512       0xefef
2200 #define TLSEXT_SIGALG_gostr34102001_gostr3411                   0xeded
2201 
2202 #define TLSEXT_SIGALG_ed25519                                   0x0807
2203 #define TLSEXT_SIGALG_ed448                                     0x0808
2204 #define TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256              0x081a
2205 #define TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384              0x081b
2206 #define TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512              0x081c
2207 
2208 /* Sigalgs names */
2209 #define TLSEXT_SIGALG_ecdsa_secp256r1_sha256_name                    "ecdsa_secp256r1_sha256"
2210 #define TLSEXT_SIGALG_ecdsa_secp384r1_sha384_name                    "ecdsa_secp384r1_sha384"
2211 #define TLSEXT_SIGALG_ecdsa_secp521r1_sha512_name                    "ecdsa_secp521r1_sha512"
2212 #define TLSEXT_SIGALG_ecdsa_sha224_name                              "ecdsa_sha224"
2213 #define TLSEXT_SIGALG_ecdsa_sha1_name                                "ecdsa_sha1"
2214 #define TLSEXT_SIGALG_rsa_pss_rsae_sha256_name                       "rsa_pss_rsae_sha256"
2215 #define TLSEXT_SIGALG_rsa_pss_rsae_sha384_name                       "rsa_pss_rsae_sha384"
2216 #define TLSEXT_SIGALG_rsa_pss_rsae_sha512_name                       "rsa_pss_rsae_sha512"
2217 #define TLSEXT_SIGALG_rsa_pss_pss_sha256_name                        "rsa_pss_pss_sha256"
2218 #define TLSEXT_SIGALG_rsa_pss_pss_sha384_name                        "rsa_pss_pss_sha384"
2219 #define TLSEXT_SIGALG_rsa_pss_pss_sha512_name                        "rsa_pss_pss_sha512"
2220 #define TLSEXT_SIGALG_rsa_pkcs1_sha256_name                          "rsa_pkcs1_sha256"
2221 #define TLSEXT_SIGALG_rsa_pkcs1_sha384_name                          "rsa_pkcs1_sha384"
2222 #define TLSEXT_SIGALG_rsa_pkcs1_sha512_name                          "rsa_pkcs1_sha512"
2223 #define TLSEXT_SIGALG_rsa_pkcs1_sha224_name                          "rsa_pkcs1_sha224"
2224 #define TLSEXT_SIGALG_rsa_pkcs1_sha1_name                            "rsa_pkcs1_sha1"
2225 #define TLSEXT_SIGALG_dsa_sha256_name                                "dsa_sha256"
2226 #define TLSEXT_SIGALG_dsa_sha384_name                                "dsa_sha384"
2227 #define TLSEXT_SIGALG_dsa_sha512_name                                "dsa_sha512"
2228 #define TLSEXT_SIGALG_dsa_sha224_name                                "dsa_sha224"
2229 #define TLSEXT_SIGALG_dsa_sha1_name                                  "dsa_sha1"
2230 #define TLSEXT_SIGALG_gostr34102012_256_intrinsic_name               "gost2012_256"
2231 #define TLSEXT_SIGALG_gostr34102012_512_intrinsic_name               "gost2012_512"
2232 #define TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256_name       "gost2012_256"
2233 #define TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512_name       "gost2012_512"
2234 #define TLSEXT_SIGALG_gostr34102001_gostr3411_name                   "gost2001_gost94"
2235 
2236 #define TLSEXT_SIGALG_ed25519_name                                   "ed25519"
2237 #define TLSEXT_SIGALG_ed448_name                                     "ed448"
2238 #define TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_name              "ecdsa_brainpoolP256r1_sha256"
2239 #define TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_name              "ecdsa_brainpoolP384r1_sha384"
2240 #define TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_name              "ecdsa_brainpoolP512r1_sha512"
2241 
2242 /* Known PSK key exchange modes */
2243 #define TLSEXT_KEX_MODE_KE                                      0x00
2244 #define TLSEXT_KEX_MODE_KE_DHE                                  0x01
2245 
2246 /*
2247  * Internal representations of key exchange modes
2248  */
2249 #define TLSEXT_KEX_MODE_FLAG_NONE                               0
2250 #define TLSEXT_KEX_MODE_FLAG_KE                                 1
2251 #define TLSEXT_KEX_MODE_FLAG_KE_DHE                             2
2252 
2253 #define SSL_USE_PSS(s) (s->s3.tmp.peer_sigalg != NULL && \
2254                         s->s3.tmp.peer_sigalg->sig == EVP_PKEY_RSA_PSS)
2255 
2256 /* TLSv1.3 downgrade protection sentinel values */
2257 extern const unsigned char tls11downgrade[8];
2258 extern const unsigned char tls12downgrade[8];
2259 
2260 extern const SSL3_ENC_METHOD ssl3_undef_enc_method;
2261 
2262 __owur const SSL_METHOD *sslv3_method(void);
2263 __owur const SSL_METHOD *sslv3_server_method(void);
2264 __owur const SSL_METHOD *sslv3_client_method(void);
2265 __owur const SSL_METHOD *tlsv1_method(void);
2266 __owur const SSL_METHOD *tlsv1_server_method(void);
2267 __owur const SSL_METHOD *tlsv1_client_method(void);
2268 __owur const SSL_METHOD *tlsv1_1_method(void);
2269 __owur const SSL_METHOD *tlsv1_1_server_method(void);
2270 __owur const SSL_METHOD *tlsv1_1_client_method(void);
2271 __owur const SSL_METHOD *tlsv1_2_method(void);
2272 __owur const SSL_METHOD *tlsv1_2_server_method(void);
2273 __owur const SSL_METHOD *tlsv1_2_client_method(void);
2274 __owur const SSL_METHOD *tlsv1_3_method(void);
2275 __owur const SSL_METHOD *tlsv1_3_server_method(void);
2276 __owur const SSL_METHOD *tlsv1_3_client_method(void);
2277 __owur const SSL_METHOD *dtlsv1_method(void);
2278 __owur const SSL_METHOD *dtlsv1_server_method(void);
2279 __owur const SSL_METHOD *dtlsv1_client_method(void);
2280 __owur const SSL_METHOD *dtls_bad_ver_client_method(void);
2281 __owur const SSL_METHOD *dtlsv1_2_method(void);
2282 __owur const SSL_METHOD *dtlsv1_2_server_method(void);
2283 __owur const SSL_METHOD *dtlsv1_2_client_method(void);
2284 
2285 extern const SSL3_ENC_METHOD TLSv1_enc_data;
2286 extern const SSL3_ENC_METHOD TLSv1_1_enc_data;
2287 extern const SSL3_ENC_METHOD TLSv1_2_enc_data;
2288 extern const SSL3_ENC_METHOD TLSv1_3_enc_data;
2289 extern const SSL3_ENC_METHOD SSLv3_enc_data;
2290 extern const SSL3_ENC_METHOD DTLSv1_enc_data;
2291 extern const SSL3_ENC_METHOD DTLSv1_2_enc_data;
2292 
2293 /*
2294  * Flags for SSL methods
2295  */
2296 # define SSL_METHOD_NO_FIPS      (1U<<0)
2297 # define SSL_METHOD_NO_SUITEB    (1U<<1)
2298 
2299 # define IMPLEMENT_tls_meth_func(version, flags, mask, func_name, s_accept, \
2300                                  s_connect, enc_data) \
2301 const SSL_METHOD *func_name(void)  \
2302         { \
2303         static const SSL_METHOD func_name##_data= { \
2304                 version, \
2305                 flags, \
2306                 mask, \
2307                 ossl_ssl_connection_new, \
2308                 ossl_ssl_connection_free, \
2309                 ossl_ssl_connection_reset, \
2310                 tls1_new, \
2311                 tls1_clear, \
2312                 tls1_free, \
2313                 s_accept, \
2314                 s_connect, \
2315                 ssl3_read, \
2316                 ssl3_peek, \
2317                 ssl3_write, \
2318                 ssl3_shutdown, \
2319                 ssl3_renegotiate, \
2320                 ssl3_renegotiate_check, \
2321                 ssl3_read_bytes, \
2322                 ssl3_write_bytes, \
2323                 ssl3_dispatch_alert, \
2324                 ssl3_ctrl, \
2325                 ssl3_ctx_ctrl, \
2326                 ssl3_get_cipher_by_char, \
2327                 ssl3_put_cipher_by_char, \
2328                 ssl3_pending, \
2329                 ssl3_num_ciphers, \
2330                 ssl3_get_cipher, \
2331                 tls1_default_timeout, \
2332                 &enc_data, \
2333                 ssl_undefined_void_function, \
2334                 ssl3_callback_ctrl, \
2335                 ssl3_ctx_callback_ctrl, \
2336         }; \
2337         return &func_name##_data; \
2338         }
2339 
2340 # define IMPLEMENT_ssl3_meth_func(func_name, s_accept, s_connect) \
2341 const SSL_METHOD *func_name(void)  \
2342         { \
2343         static const SSL_METHOD func_name##_data= { \
2344                 SSL3_VERSION, \
2345                 SSL_METHOD_NO_FIPS | SSL_METHOD_NO_SUITEB, \
2346                 SSL_OP_NO_SSLv3, \
2347                 ossl_ssl_connection_new, \
2348                 ossl_ssl_connection_free, \
2349                 ossl_ssl_connection_reset, \
2350                 ssl3_new, \
2351                 ssl3_clear, \
2352                 ssl3_free, \
2353                 s_accept, \
2354                 s_connect, \
2355                 ssl3_read, \
2356                 ssl3_peek, \
2357                 ssl3_write, \
2358                 ssl3_shutdown, \
2359                 ssl3_renegotiate, \
2360                 ssl3_renegotiate_check, \
2361                 ssl3_read_bytes, \
2362                 ssl3_write_bytes, \
2363                 ssl3_dispatch_alert, \
2364                 ssl3_ctrl, \
2365                 ssl3_ctx_ctrl, \
2366                 ssl3_get_cipher_by_char, \
2367                 ssl3_put_cipher_by_char, \
2368                 ssl3_pending, \
2369                 ssl3_num_ciphers, \
2370                 ssl3_get_cipher, \
2371                 ssl3_default_timeout, \
2372                 &SSLv3_enc_data, \
2373                 ssl_undefined_void_function, \
2374                 ssl3_callback_ctrl, \
2375                 ssl3_ctx_callback_ctrl, \
2376         }; \
2377         return &func_name##_data; \
2378         }
2379 
2380 # define IMPLEMENT_dtls1_meth_func(version, flags, mask, func_name, s_accept, \
2381                                         s_connect, enc_data) \
2382 const SSL_METHOD *func_name(void)  \
2383         { \
2384         static const SSL_METHOD func_name##_data= { \
2385                 version, \
2386                 flags, \
2387                 mask, \
2388                 ossl_ssl_connection_new, \
2389                 ossl_ssl_connection_free, \
2390                 ossl_ssl_connection_reset, \
2391                 dtls1_new, \
2392                 dtls1_clear, \
2393                 dtls1_free, \
2394                 s_accept, \
2395                 s_connect, \
2396                 ssl3_read, \
2397                 ssl3_peek, \
2398                 ssl3_write, \
2399                 dtls1_shutdown, \
2400                 ssl3_renegotiate, \
2401                 ssl3_renegotiate_check, \
2402                 dtls1_read_bytes, \
2403                 dtls1_write_app_data_bytes, \
2404                 dtls1_dispatch_alert, \
2405                 dtls1_ctrl, \
2406                 ssl3_ctx_ctrl, \
2407                 ssl3_get_cipher_by_char, \
2408                 ssl3_put_cipher_by_char, \
2409                 ssl3_pending, \
2410                 ssl3_num_ciphers, \
2411                 ssl3_get_cipher, \
2412                 dtls1_default_timeout, \
2413                 &enc_data, \
2414                 ssl_undefined_void_function, \
2415                 ssl3_callback_ctrl, \
2416                 ssl3_ctx_callback_ctrl, \
2417         }; \
2418         return &func_name##_data; \
2419         }
2420 
2421 struct openssl_ssl_test_functions {
2422     int (*p_ssl_init_wbio_buffer) (SSL_CONNECTION *s);
2423 };
2424 
2425 const char *ssl_protocol_to_string(int version);
2426 
tls12_rpk_and_privkey(const SSL_CONNECTION * sc,int idx)2427 static ossl_inline int tls12_rpk_and_privkey(const SSL_CONNECTION *sc, int idx)
2428 {
2429     /*
2430      * This is to check for special cases when using RPK with just
2431      * a private key, and NO CERTIFICATE
2432      */
2433     return ((sc->server && sc->ext.server_cert_type == TLSEXT_cert_type_rpk)
2434             || (!sc->server && sc->ext.client_cert_type == TLSEXT_cert_type_rpk))
2435         && sc->cert->pkeys[idx].privatekey != NULL
2436         && sc->cert->pkeys[idx].x509 == NULL;
2437 }
2438 
ssl_has_cert_type(const SSL_CONNECTION * sc,unsigned char ct)2439 static ossl_inline int ssl_has_cert_type(const SSL_CONNECTION *sc, unsigned char ct)
2440 {
2441     unsigned char *ptr;
2442     size_t len;
2443 
2444     if (sc->server) {
2445         ptr = sc->server_cert_type;
2446         len = sc->server_cert_type_len;
2447     } else {
2448         ptr = sc->client_cert_type;
2449         len = sc->client_cert_type_len;
2450     }
2451 
2452     if (ptr == NULL)
2453         return 0;
2454 
2455     return memchr(ptr, ct, len) != NULL;
2456 }
2457 
2458 /* Returns true if certificate and private key for 'idx' are present */
ssl_has_cert(const SSL_CONNECTION * s,int idx)2459 static ossl_inline int ssl_has_cert(const SSL_CONNECTION *s, int idx)
2460 {
2461     if (idx < 0 || idx >= (int)s->ssl_pkey_num)
2462         return 0;
2463 
2464     /* If RPK is enabled for this SSL... only require private key */
2465     if (ssl_has_cert_type(s, TLSEXT_cert_type_rpk))
2466         return s->cert->pkeys[idx].privatekey != NULL;
2467 
2468     return s->cert->pkeys[idx].x509 != NULL
2469         && s->cert->pkeys[idx].privatekey != NULL;
2470 }
2471 
tls1_get_peer_groups(SSL_CONNECTION * s,const uint16_t ** pgroups,size_t * pgroupslen)2472 static ossl_inline void tls1_get_peer_groups(SSL_CONNECTION *s,
2473                                              const uint16_t **pgroups,
2474                                              size_t *pgroupslen)
2475 {
2476     *pgroups = s->ext.peer_supportedgroups;
2477     *pgroupslen = s->ext.peer_supportedgroups_len;
2478 }
2479 
2480 # ifndef OPENSSL_UNIT_TEST
2481 
2482 __owur int ossl_ssl_init(SSL *ssl, SSL_CTX *ctx, const SSL_METHOD *method,
2483                          int type);
2484 __owur SSL *ossl_ssl_connection_new_int(SSL_CTX *ctx, SSL *user_ssl,
2485                                         const SSL_METHOD *method);
2486 __owur SSL *ossl_ssl_connection_new(SSL_CTX *ctx);
2487 void ossl_ssl_connection_free(SSL *ssl);
2488 __owur int ossl_ssl_connection_reset(SSL *ssl);
2489 
2490 __owur int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes);
2491 __owur int ssl_write_internal(SSL *s, const void *buf, size_t num,
2492                               uint64_t flags, size_t *written);
2493 int ssl_clear_bad_session(SSL_CONNECTION *s);
2494 __owur CERT *ssl_cert_new(size_t ssl_pkey_num);
2495 __owur CERT *ssl_cert_dup(CERT *cert);
2496 void ssl_cert_clear_certs(CERT *c);
2497 void ssl_cert_free(CERT *c);
2498 __owur int ssl_generate_session_id(SSL_CONNECTION *s, SSL_SESSION *ss);
2499 __owur int ssl_get_new_session(SSL_CONNECTION *s, int session);
2500 __owur SSL_SESSION *lookup_sess_in_cache(SSL_CONNECTION *s,
2501                                          const unsigned char *sess_id,
2502                                          size_t sess_id_len);
2503 __owur int ssl_get_prev_session(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello);
2504 __owur SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket);
2505 __owur int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b);
2506 DECLARE_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
2507 __owur int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
2508                                  const SSL_CIPHER *const *bp);
2509 __owur STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
2510                                                     STACK_OF(SSL_CIPHER) *tls13_ciphersuites,
2511                                                     STACK_OF(SSL_CIPHER) **cipher_list,
2512                                                     STACK_OF(SSL_CIPHER) **cipher_list_by_id,
2513                                                     const char *rule_str,
2514                                                     CERT *c);
2515 __owur int ssl_cache_cipherlist(SSL_CONNECTION *s, PACKET *cipher_suites,
2516                                 int sslv2format);
2517 __owur int ossl_bytes_to_cipher_list(SSL_CONNECTION *s, PACKET *cipher_suites,
2518                                      STACK_OF(SSL_CIPHER) **skp,
2519                                      STACK_OF(SSL_CIPHER) **scsvs, int sslv2format,
2520                                      int fatal);
2521 void ssl_update_cache(SSL_CONNECTION *s, int mode);
2522 __owur int ssl_cipher_get_evp_cipher(SSL_CTX *ctx, const SSL_CIPHER *sslc,
2523                                      const EVP_CIPHER **enc);
2524 __owur int ssl_cipher_get_evp_md_mac(SSL_CTX *ctx, const SSL_CIPHER *sslc,
2525                                      const EVP_MD **md,
2526                                      int *mac_pkey_type, size_t *mac_secret_size);
2527 __owur int ssl_cipher_get_evp(SSL_CTX *ctxc, const SSL_SESSION *s,
2528                               const EVP_CIPHER **enc, const EVP_MD **md,
2529                               int *mac_pkey_type, size_t *mac_secret_size,
2530                               SSL_COMP **comp, int use_etm);
2531 __owur int ssl_cipher_get_overhead(const SSL_CIPHER *c, size_t *mac_overhead,
2532                                    size_t *int_overhead, size_t *blocksize,
2533                                    size_t *ext_overhead);
2534 __owur int ssl_cert_is_disabled(SSL_CTX *ctx, size_t idx);
2535 __owur const SSL_CIPHER *ssl_get_cipher_by_char(SSL_CONNECTION *ssl,
2536                                                 const unsigned char *ptr,
2537                                                 int all);
2538 __owur int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx,
2539                                STACK_OF(X509) *chain);
2540 __owur int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx,
2541                                STACK_OF(X509) *chain);
2542 __owur int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x);
2543 __owur int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x);
2544 __owur int ssl_cert_select_current(CERT *c, X509 *x);
2545 __owur int ssl_cert_set_current(CERT *c, long arg);
2546 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg);
2547 
2548 __owur int ssl_verify_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk);
2549 __owur int ssl_verify_rpk(SSL_CONNECTION *s, EVP_PKEY *rpk);
2550 __owur int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags);
2551 __owur int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain,
2552                                    int ref);
2553 __owur int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain);
2554 
2555 __owur int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid,
2556                         void *other);
2557 __owur int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid,
2558                             void *other);
2559 int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp);
2560 
2561 __owur int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx);
2562 __owur const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk,
2563                                                       size_t *pidx,
2564                                                       SSL_CTX *ctx);
2565 __owur const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx);
2566 
2567 int ssl_undefined_function(SSL *s);
2568 __owur int ssl_undefined_void_function(void);
2569 __owur int ssl_get_server_cert_serverinfo(SSL_CONNECTION *s,
2570                                           const unsigned char **serverinfo,
2571                                           size_t *serverinfo_length);
2572 void ssl_set_masks(SSL_CONNECTION *s);
2573 __owur STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL_CONNECTION *sc);
2574 __owur int ssl_x509err2alert(int type);
2575 void ssl_sort_cipher_list(void);
2576 int ssl_load_ciphers(SSL_CTX *ctx);
2577 __owur int ssl_setup_sigalgs(SSL_CTX *ctx);
2578 int ssl_load_groups(SSL_CTX *ctx);
2579 int ssl_load_sigalgs(SSL_CTX *ctx);
2580 __owur int ssl_fill_hello_random(SSL_CONNECTION *s, int server,
2581                                  unsigned char *field, size_t len,
2582                                  DOWNGRADE dgrd);
2583 __owur int ssl_generate_master_secret(SSL_CONNECTION *s, unsigned char *pms,
2584                                       size_t pmslen, int free_pms);
2585 __owur EVP_PKEY *ssl_generate_pkey(SSL_CONNECTION *s, EVP_PKEY *pm);
2586 __owur int ssl_gensecret(SSL_CONNECTION *s, unsigned char *pms, size_t pmslen);
2587 __owur int ssl_derive(SSL_CONNECTION *s, EVP_PKEY *privkey, EVP_PKEY *pubkey,
2588                       int genmaster);
2589 __owur int ssl_decapsulate(SSL_CONNECTION *s, EVP_PKEY *privkey,
2590                            const unsigned char *ct, size_t ctlen,
2591                            int gensecret);
2592 __owur int ssl_encapsulate(SSL_CONNECTION *s, EVP_PKEY *pubkey,
2593                            unsigned char **ctp, size_t *ctlenp,
2594                            int gensecret);
2595 __owur EVP_PKEY *ssl_dh_to_pkey(DH *dh);
2596 __owur int ssl_set_tmp_ecdh_groups(uint16_t **pext, size_t *pextlen,
2597                                    void *key);
2598 __owur unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION *sc);
2599 __owur unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION *sc);
2600 
2601 __owur const SSL_CIPHER *ssl3_get_cipher_by_id(uint32_t id);
2602 __owur const SSL_CIPHER *ssl3_get_cipher_by_std_name(const char *stdname);
2603 __owur const SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p);
2604 __owur int ssl3_put_cipher_by_char(const SSL_CIPHER *c, WPACKET *pkt,
2605                                    size_t *len);
2606 int ssl3_init_finished_mac(SSL_CONNECTION *s);
2607 __owur int ssl3_setup_key_block(SSL_CONNECTION *s);
2608 __owur int ssl3_change_cipher_state(SSL_CONNECTION *s, int which);
2609 void ssl3_cleanup_key_block(SSL_CONNECTION *s);
2610 __owur int ssl3_do_write(SSL_CONNECTION *s, uint8_t type);
2611 int ssl3_send_alert(SSL_CONNECTION *s, int level, int desc);
2612 __owur int ssl3_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
2613                                        unsigned char *p, size_t len,
2614                                        size_t *secret_size);
2615 __owur int ssl3_get_req_cert_type(SSL_CONNECTION *s, WPACKET *pkt);
2616 __owur int ssl3_num_ciphers(void);
2617 __owur const SSL_CIPHER *ssl3_get_cipher(unsigned int u);
2618 int ssl3_renegotiate(SSL *ssl);
2619 int ssl3_renegotiate_check(SSL *ssl, int initok);
2620 void ssl3_digest_master_key_set_params(const SSL_SESSION *session,
2621                                        OSSL_PARAM params[]);
2622 __owur int ssl3_dispatch_alert(SSL *s);
2623 __owur size_t ssl3_final_finish_mac(SSL_CONNECTION *s, const char *sender,
2624                                     size_t slen, unsigned char *p);
2625 __owur int ssl3_finish_mac(SSL_CONNECTION *s, const unsigned char *buf,
2626                            size_t len);
2627 void ssl3_free_digest_list(SSL_CONNECTION *s);
2628 __owur unsigned long ssl3_output_cert_chain(SSL_CONNECTION *s, WPACKET *pkt,
2629                                             CERT_PKEY *cpk, int for_comp);
2630 __owur const SSL_CIPHER *ssl3_choose_cipher(SSL_CONNECTION *s,
2631                                             STACK_OF(SSL_CIPHER) *clnt,
2632                                             STACK_OF(SSL_CIPHER) *srvr);
2633 __owur int ssl3_digest_cached_records(SSL_CONNECTION *s, int keep);
2634 __owur int ssl3_new(SSL *s);
2635 void ssl3_free(SSL *s);
2636 __owur int ssl3_read(SSL *s, void *buf, size_t len, size_t *readbytes);
2637 __owur int ssl3_peek(SSL *s, void *buf, size_t len, size_t *readbytes);
2638 __owur int ssl3_write(SSL *s, const void *buf, size_t len, size_t *written);
2639 __owur int ssl3_shutdown(SSL *s);
2640 int ssl3_clear(SSL *s);
2641 __owur long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg);
2642 __owur long ssl3_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg);
2643 __owur long ssl3_callback_ctrl(SSL *s, int cmd, void (*fp) (void));
2644 __owur long ssl3_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp) (void));
2645 
2646 __owur int ssl3_do_change_cipher_spec(SSL_CONNECTION *s);
2647 __owur OSSL_TIME ssl3_default_timeout(void);
2648 
2649 __owur int ssl3_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt,
2650                                      int htype);
2651 __owur int tls_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype);
2652 __owur int tls_setup_handshake(SSL_CONNECTION *s);
2653 __owur int dtls1_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt, int htype);
2654 __owur int dtls1_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype);
2655 __owur int ssl3_handshake_write(SSL_CONNECTION *s);
2656 
2657 __owur int ssl_allow_compression(SSL_CONNECTION *s);
2658 
2659 __owur int ssl_version_cmp(const SSL_CONNECTION *s, int versiona, int versionb);
2660 __owur int ssl_version_supported(const SSL_CONNECTION *s, int version,
2661                                  const SSL_METHOD **meth);
2662 
2663 __owur int ssl_set_client_hello_version(SSL_CONNECTION *s);
2664 __owur int ssl_check_version_downgrade(SSL_CONNECTION *s);
2665 __owur int ssl_set_version_bound(int method_version, int version, int *bound);
2666 __owur int ssl_choose_server_version(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello,
2667                                      DOWNGRADE *dgrd);
2668 __owur int ssl_choose_client_version(SSL_CONNECTION *s, int version,
2669                                      RAW_EXTENSION *extensions);
2670 __owur int ssl_get_min_max_version(const SSL_CONNECTION *s, int *min_version,
2671                                    int *max_version, int *real_max);
2672 
2673 __owur OSSL_TIME tls1_default_timeout(void);
2674 __owur int dtls1_do_write(SSL_CONNECTION *s, uint8_t type);
2675 void dtls1_set_message_header(SSL_CONNECTION *s,
2676                               unsigned char mt,
2677                               size_t len,
2678                               size_t frag_off, size_t frag_len);
2679 
2680 int dtls1_write_app_data_bytes(SSL *s, uint8_t type, const void *buf_,
2681                                size_t len, size_t *written);
2682 
2683 __owur int dtls1_read_failed(SSL_CONNECTION *s, int code);
2684 __owur int dtls1_buffer_message(SSL_CONNECTION *s, int ccs);
2685 __owur int dtls1_retransmit_message(SSL_CONNECTION *s, unsigned short seq,
2686                                     int *found);
2687 __owur int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
2688 int dtls1_retransmit_buffered_messages(SSL_CONNECTION *s);
2689 void dtls1_clear_received_buffer(SSL_CONNECTION *s);
2690 void dtls1_clear_sent_buffer(SSL_CONNECTION *s);
2691 void dtls1_get_message_header(const unsigned char *data,
2692                               struct hm_header_st *msg_hdr);
2693 __owur OSSL_TIME dtls1_default_timeout(void);
2694 __owur int dtls1_get_timeout(const SSL_CONNECTION *s, OSSL_TIME *timeleft);
2695 __owur int dtls1_check_timeout_num(SSL_CONNECTION *s);
2696 __owur int dtls1_handle_timeout(SSL_CONNECTION *s);
2697 void dtls1_start_timer(SSL_CONNECTION *s);
2698 void dtls1_stop_timer(SSL_CONNECTION *s);
2699 __owur int dtls1_is_timer_expired(SSL_CONNECTION *s);
2700 __owur int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
2701                                          size_t cookie_len);
2702 __owur size_t dtls1_min_mtu(SSL_CONNECTION *s);
2703 void dtls1_hm_fragment_free(hm_fragment *frag);
2704 __owur int dtls1_query_mtu(SSL_CONNECTION *s);
2705 
2706 __owur int tls1_new(SSL *s);
2707 void tls1_free(SSL *s);
2708 int tls1_clear(SSL *s);
2709 
2710 __owur int dtls1_new(SSL *s);
2711 void dtls1_free(SSL *s);
2712 int dtls1_clear(SSL *s);
2713 long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg);
2714 __owur int dtls1_shutdown(SSL *s);
2715 
2716 __owur int dtls1_dispatch_alert(SSL *s);
2717 
2718 __owur int ssl_init_wbio_buffer(SSL_CONNECTION *s);
2719 int ssl_free_wbio_buffer(SSL_CONNECTION *s);
2720 
2721 __owur int tls1_change_cipher_state(SSL_CONNECTION *s, int which);
2722 __owur int tls1_setup_key_block(SSL_CONNECTION *s);
2723 __owur size_t tls1_final_finish_mac(SSL_CONNECTION *s, const char *str,
2724                                     size_t slen, unsigned char *p);
2725 __owur int tls1_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
2726                                        unsigned char *p, size_t len,
2727                                        size_t *secret_size);
2728 __owur int tls13_setup_key_block(SSL_CONNECTION *s);
2729 __owur size_t tls13_final_finish_mac(SSL_CONNECTION *s, const char *str, size_t slen,
2730                                      unsigned char *p);
2731 __owur int tls13_change_cipher_state(SSL_CONNECTION *s, int which);
2732 __owur int tls13_update_key(SSL_CONNECTION *s, int send);
2733 __owur int tls13_hkdf_expand(SSL_CONNECTION *s,
2734                              const EVP_MD *md,
2735                              const unsigned char *secret,
2736                              const unsigned char *label, size_t labellen,
2737                              const unsigned char *data, size_t datalen,
2738                              unsigned char *out, size_t outlen, int fatal);
2739 __owur int tls13_hkdf_expand_ex(OSSL_LIB_CTX *libctx, const char *propq,
2740                                 const EVP_MD *md,
2741                                 const unsigned char *secret,
2742                                 const unsigned char *label, size_t labellen,
2743                                 const unsigned char *data, size_t datalen,
2744                                 unsigned char *out, size_t outlen,
2745                                 int raise_error);
2746 __owur int tls13_derive_key(SSL_CONNECTION *s, const EVP_MD *md,
2747                             const unsigned char *secret, unsigned char *key,
2748                             size_t keylen);
2749 __owur int tls13_derive_iv(SSL_CONNECTION *s, const EVP_MD *md,
2750                            const unsigned char *secret, unsigned char *iv,
2751                            size_t ivlen);
2752 __owur int tls13_derive_finishedkey(SSL_CONNECTION *s, const EVP_MD *md,
2753                                     const unsigned char *secret,
2754                                     unsigned char *fin, size_t finlen);
2755 int tls13_generate_secret(SSL_CONNECTION *s, const EVP_MD *md,
2756                           const unsigned char *prevsecret,
2757                           const unsigned char *insecret,
2758                           size_t insecretlen,
2759                           unsigned char *outsecret);
2760 __owur int tls13_generate_handshake_secret(SSL_CONNECTION *s,
2761                                            const unsigned char *insecret,
2762                                            size_t insecretlen);
2763 __owur int tls13_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
2764                                         unsigned char *prev, size_t prevlen,
2765                                         size_t *secret_size);
2766 __owur int tls1_export_keying_material(SSL_CONNECTION *s,
2767                                        unsigned char *out, size_t olen,
2768                                        const char *label, size_t llen,
2769                                        const unsigned char *p, size_t plen,
2770                                        int use_context);
2771 __owur int tls13_export_keying_material(SSL_CONNECTION *s,
2772                                         unsigned char *out, size_t olen,
2773                                         const char *label, size_t llen,
2774                                         const unsigned char *context,
2775                                         size_t contextlen, int use_context);
2776 __owur int tls13_export_keying_material_early(SSL_CONNECTION *s,
2777                                               unsigned char *out, size_t olen,
2778                                               const char *label, size_t llen,
2779                                               const unsigned char *context,
2780                                               size_t contextlen);
2781 __owur int tls1_alert_code(int code);
2782 __owur int tls13_alert_code(int code);
2783 __owur int ssl3_alert_code(int code);
2784 
2785 __owur int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CONNECTION *s);
2786 
2787 SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n);
2788 
2789 __owur const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t curve_id);
2790 __owur const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id);
2791 __owur int tls1_group_id2nid(uint16_t group_id, int include_unknown);
2792 __owur uint16_t tls1_nid2group_id(int nid);
2793 __owur int tls1_check_group_id(SSL_CONNECTION *s, uint16_t group_id,
2794                                int check_own_curves);
2795 __owur uint16_t tls1_shared_group(SSL_CONNECTION *s, int nmatch);
2796 __owur int tls1_set_groups(uint16_t **pext, size_t *pextlen,
2797                            int *curves, size_t ncurves);
2798 __owur int tls1_set_groups_list(SSL_CTX *ctx, uint16_t **pext, size_t *pextlen,
2799                                 const char *str);
2800 __owur EVP_PKEY *ssl_generate_pkey_group(SSL_CONNECTION *s, uint16_t id);
2801 __owur int tls_valid_group(SSL_CONNECTION *s, uint16_t group_id, int minversion,
2802                            int maxversion, int isec, int *okfortls13);
2803 __owur EVP_PKEY *ssl_generate_param_group(SSL_CONNECTION *s, uint16_t id);
2804 void tls1_get_formatlist(SSL_CONNECTION *s, const unsigned char **pformats,
2805                          size_t *num_formats);
2806 __owur int tls1_check_ec_tmp_key(SSL_CONNECTION *s, unsigned long id);
2807 
2808 __owur int tls_group_allowed(SSL_CONNECTION *s, uint16_t curve, int op);
2809 void tls1_get_supported_groups(SSL_CONNECTION *s, const uint16_t **pgroups,
2810                                size_t *pgroupslen);
2811 
2812 __owur int tls1_set_server_sigalgs(SSL_CONNECTION *s);
2813 
2814 __owur SSL_TICKET_STATUS tls_get_ticket_from_client(SSL_CONNECTION *s,
2815                                                     CLIENTHELLO_MSG *hello,
2816                                                     SSL_SESSION **ret);
2817 __owur SSL_TICKET_STATUS tls_decrypt_ticket(SSL_CONNECTION *s,
2818                                             const unsigned char *etick,
2819                                             size_t eticklen,
2820                                             const unsigned char *sess_id,
2821                                             size_t sesslen, SSL_SESSION **psess);
2822 
2823 __owur int tls_use_ticket(SSL_CONNECTION *s);
2824 
2825 void ssl_set_sig_mask(uint32_t *pmask_a, SSL_CONNECTION *s, int op);
2826 
2827 __owur int tls1_set_sigalgs_list(SSL_CTX *ctx, CERT *c, const char *str, int client);
2828 __owur int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,
2829                                 int client);
2830 __owur int tls1_set_sigalgs(CERT *c, const int *salg, size_t salglen,
2831                             int client);
2832 int tls1_check_chain(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pk,
2833                      STACK_OF(X509) *chain, int idx);
2834 void tls1_set_cert_validity(SSL_CONNECTION *s);
2835 
2836 #  ifndef OPENSSL_NO_CT
2837 __owur int ssl_validate_ct(SSL_CONNECTION *s);
2838 #  endif
2839 
2840 __owur EVP_PKEY *ssl_get_auto_dh(SSL_CONNECTION *s);
2841 
2842 __owur int ssl_security_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, int vfy,
2843                              int is_ee);
2844 __owur int ssl_security_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk,
2845                                    X509 *ex, int vfy);
2846 
2847 int tls_choose_sigalg(SSL_CONNECTION *s, int fatalerrs);
2848 
2849 __owur long ssl_get_algorithm2(SSL_CONNECTION *s);
2850 __owur int tls12_copy_sigalgs(SSL_CONNECTION *s, WPACKET *pkt,
2851                               const uint16_t *psig, size_t psiglen);
2852 __owur int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen);
2853 __owur int tls1_save_sigalgs(SSL_CONNECTION *s, PACKET *pkt, int cert);
2854 __owur int tls1_process_sigalgs(SSL_CONNECTION *s);
2855 __owur int tls1_set_peer_legacy_sigalg(SSL_CONNECTION *s, const EVP_PKEY *pkey);
2856 __owur int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu,
2857                           const EVP_MD **pmd);
2858 __owur size_t tls12_get_psigalgs(SSL_CONNECTION *s, int sent,
2859                                  const uint16_t **psigs);
2860 __owur int tls_check_sigalg_curve(const SSL_CONNECTION *s, int curve);
2861 __owur int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t, EVP_PKEY *pkey);
2862 __owur int ssl_set_client_disabled(SSL_CONNECTION *s);
2863 __owur int ssl_cipher_disabled(const SSL_CONNECTION *s, const SSL_CIPHER *c,
2864                                int op, int echde);
2865 
2866 __owur int ssl_handshake_hash(SSL_CONNECTION *s,
2867                               unsigned char *out, size_t outlen,
2868                               size_t *hashlen);
2869 __owur const EVP_MD *ssl_md(SSL_CTX *ctx, int idx);
2870 int ssl_get_md_idx(int md_nid);
2871 __owur const EVP_MD *ssl_handshake_md(SSL_CONNECTION *s);
2872 __owur const EVP_MD *ssl_prf_md(SSL_CONNECTION *s);
2873 
2874 /*
2875  * ssl_log_rsa_client_key_exchange logs |premaster| to the SSL_CTX associated
2876  * with |ssl|, if logging is enabled. It returns one on success and zero on
2877  * failure. The entry is identified by the first 8 bytes of
2878  * |encrypted_premaster|.
2879  */
2880 __owur int ssl_log_rsa_client_key_exchange(SSL_CONNECTION *s,
2881                                            const uint8_t *encrypted_premaster,
2882                                            size_t encrypted_premaster_len,
2883                                            const uint8_t *premaster,
2884                                            size_t premaster_len);
2885 
2886 /*
2887  * ssl_log_secret logs |secret| to the SSL_CTX associated with |ssl|, if
2888  * logging is available. It returns one on success and zero on failure. It tags
2889  * the entry with |label|.
2890  */
2891 __owur int ssl_log_secret(SSL_CONNECTION *s, const char *label,
2892                           const uint8_t *secret, size_t secret_len);
2893 
2894 #define MASTER_SECRET_LABEL "CLIENT_RANDOM"
2895 #define CLIENT_EARLY_LABEL "CLIENT_EARLY_TRAFFIC_SECRET"
2896 #define CLIENT_HANDSHAKE_LABEL "CLIENT_HANDSHAKE_TRAFFIC_SECRET"
2897 #define SERVER_HANDSHAKE_LABEL "SERVER_HANDSHAKE_TRAFFIC_SECRET"
2898 #define CLIENT_APPLICATION_LABEL "CLIENT_TRAFFIC_SECRET_0"
2899 #define CLIENT_APPLICATION_N_LABEL "CLIENT_TRAFFIC_SECRET_N"
2900 #define SERVER_APPLICATION_LABEL "SERVER_TRAFFIC_SECRET_0"
2901 #define SERVER_APPLICATION_N_LABEL "SERVER_TRAFFIC_SECRET_N"
2902 #define EARLY_EXPORTER_SECRET_LABEL "EARLY_EXPORTER_SECRET"
2903 #define EXPORTER_SECRET_LABEL "EXPORTER_SECRET"
2904 
2905 __owur int srp_generate_server_master_secret(SSL_CONNECTION *s);
2906 __owur int srp_generate_client_master_secret(SSL_CONNECTION *s);
2907 __owur int srp_verify_server_param(SSL_CONNECTION *s);
2908 
2909 /* statem/statem_srvr.c */
2910 
2911 __owur int send_certificate_request(SSL_CONNECTION *s);
2912 
2913 /* statem/extensions_cust.c */
2914 
2915 custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
2916                                    ENDPOINT role, unsigned int ext_type,
2917                                    size_t *idx);
2918 
2919 void custom_ext_init(custom_ext_methods *meths);
2920 
2921 int ossl_tls_add_custom_ext_intern(SSL_CTX *ctx, custom_ext_methods *exts,
2922                                    ENDPOINT role, unsigned int ext_type,
2923                                    unsigned int context,
2924                                    SSL_custom_ext_add_cb_ex add_cb,
2925                                    SSL_custom_ext_free_cb_ex free_cb,
2926                                    void *add_arg,
2927                                    SSL_custom_ext_parse_cb_ex parse_cb,
2928                                    void *parse_arg);
2929 __owur int custom_ext_parse(SSL_CONNECTION *s, unsigned int context,
2930                             unsigned int ext_type,
2931                             const unsigned char *ext_data, size_t ext_size,
2932                             X509 *x, size_t chainidx);
2933 __owur int custom_ext_add(SSL_CONNECTION *s, int context, WPACKET *pkt, X509 *x,
2934                           size_t chainidx, int maxversion);
2935 
2936 __owur int custom_exts_copy(custom_ext_methods *dst,
2937                             const custom_ext_methods *src);
2938 __owur int custom_exts_copy_flags(custom_ext_methods *dst,
2939                                   const custom_ext_methods *src);
2940 void custom_exts_free(custom_ext_methods *exts);
2941 
2942 /* ssl_mcnf.c */
2943 int ssl_ctx_system_config(SSL_CTX *ctx);
2944 
2945 const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
2946                                        int nid,
2947                                        const char *properties);
2948 int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher);
2949 void ssl_evp_cipher_free(const EVP_CIPHER *cipher);
2950 const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx,
2951                                int nid,
2952                                const char *properties);
2953 int ssl_evp_md_up_ref(const EVP_MD *md);
2954 void ssl_evp_md_free(const EVP_MD *md);
2955 
2956 void tls_engine_finish(ENGINE *e);
2957 const EVP_CIPHER *tls_get_cipher_from_engine(int nid);
2958 const EVP_MD *tls_get_digest_from_engine(int nid);
2959 int tls_engine_load_ssl_client_cert(SSL_CONNECTION *s, X509 **px509,
2960                                     EVP_PKEY **ppkey);
2961 int ssl_hmac_old_new(SSL_HMAC *ret);
2962 void ssl_hmac_old_free(SSL_HMAC *ctx);
2963 int ssl_hmac_old_init(SSL_HMAC *ctx, void *key, size_t len, char *md);
2964 int ssl_hmac_old_update(SSL_HMAC *ctx, const unsigned char *data, size_t len);
2965 int ssl_hmac_old_final(SSL_HMAC *ctx, unsigned char *md, size_t *len);
2966 size_t ssl_hmac_old_size(const SSL_HMAC *ctx);
2967 
2968 int ssl_ctx_srp_ctx_free_intern(SSL_CTX *ctx);
2969 int ssl_ctx_srp_ctx_init_intern(SSL_CTX *ctx);
2970 int ssl_srp_ctx_free_intern(SSL_CONNECTION *s);
2971 int ssl_srp_ctx_init_intern(SSL_CONNECTION *s);
2972 
2973 int ssl_srp_calc_a_param_intern(SSL_CONNECTION *s);
2974 int ssl_srp_server_param_with_username_intern(SSL_CONNECTION *s, int *ad);
2975 
2976 void ssl_session_calculate_timeout(SSL_SESSION *ss);
2977 
2978 # else /* OPENSSL_UNIT_TEST */
2979 
2980 #  define ssl_init_wbio_buffer SSL_test_functions()->p_ssl_init_wbio_buffer
2981 
2982 # endif
2983 
2984 /* Some helper routines to support TSAN operations safely */
ssl_tsan_lock(const SSL_CTX * ctx)2985 static ossl_unused ossl_inline int ssl_tsan_lock(const SSL_CTX *ctx)
2986 {
2987 #ifdef TSAN_REQUIRES_LOCKING
2988     if (!CRYPTO_THREAD_write_lock(ctx->tsan_lock))
2989         return 0;
2990 #endif
2991     return 1;
2992 }
2993 
ssl_tsan_unlock(const SSL_CTX * ctx)2994 static ossl_unused ossl_inline void ssl_tsan_unlock(const SSL_CTX *ctx)
2995 {
2996 #ifdef TSAN_REQUIRES_LOCKING
2997     CRYPTO_THREAD_unlock(ctx->tsan_lock);
2998 #endif
2999 }
3000 
ssl_tsan_counter(const SSL_CTX * ctx,TSAN_QUALIFIER int * stat)3001 static ossl_unused ossl_inline void ssl_tsan_counter(const SSL_CTX *ctx,
3002                                                      TSAN_QUALIFIER int *stat)
3003 {
3004     if (ssl_tsan_lock(ctx)) {
3005         tsan_counter(stat);
3006         ssl_tsan_unlock(ctx);
3007     }
3008 }
3009 
3010 int ossl_comp_has_alg(int a);
3011 size_t ossl_calculate_comp_expansion(int alg, size_t length);
3012 
3013 void ossl_ssl_set_custom_record_layer(SSL_CONNECTION *s,
3014                                       const OSSL_RECORD_METHOD *meth,
3015                                       void *rlarg);
3016 
3017 long ossl_ctrl_internal(SSL *s, int cmd, long larg, void *parg, int no_quic);
3018 
3019 /*
3020  * Options which no longer have any effect, but which can be implemented
3021  * as no-ops for QUIC.
3022  */
3023 #define OSSL_LEGACY_SSL_OPTIONS                 \
3024     (SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG  | \
3025      SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER        | \
3026      SSL_OP_SSLEAY_080_CLIENT_DH_BUG          | \
3027      SSL_OP_TLS_D5_BUG                        | \
3028      SSL_OP_TLS_BLOCK_PADDING_BUG             | \
3029      SSL_OP_MSIE_SSLV2_RSA_PADDING            | \
3030      SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG       | \
3031      SSL_OP_MICROSOFT_SESS_ID_BUG             | \
3032      SSL_OP_NETSCAPE_CHALLENGE_BUG            | \
3033      SSL_OP_PKCS1_CHECK_1                     | \
3034      SSL_OP_PKCS1_CHECK_2                     | \
3035      SSL_OP_SINGLE_DH_USE                     | \
3036      SSL_OP_SINGLE_ECDH_USE                   | \
3037      SSL_OP_EPHEMERAL_RSA                     )
3038 
3039 /* This option is undefined in public headers with no-dtls1-method. */
3040 #ifndef SSL_OP_CISCO_ANYCONNECT
3041 # define SSL_OP_CISCO_ANYCONNECT 0
3042 #endif
3043 /*
3044  * Options which are no-ops under QUIC or TLSv1.3 and which are therefore
3045  * allowed but ignored under QUIC.
3046  */
3047 #define OSSL_TLS1_2_OPTIONS                     \
3048     (SSL_OP_CRYPTOPRO_TLSEXT_BUG              | \
3049      SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS       | \
3050      SSL_OP_ALLOW_CLIENT_RENEGOTIATION        | \
3051      SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION | \
3052      SSL_OP_NO_COMPRESSION                    | \
3053      SSL_OP_NO_SSLv3                          | \
3054      SSL_OP_NO_TLSv1                          | \
3055      SSL_OP_NO_TLSv1_1                        | \
3056      SSL_OP_NO_TLSv1_2                        | \
3057      SSL_OP_NO_DTLSv1                         | \
3058      SSL_OP_NO_DTLSv1_2                       | \
3059      SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | \
3060      SSL_OP_CISCO_ANYCONNECT                  | \
3061      SSL_OP_NO_RENEGOTIATION                  | \
3062      SSL_OP_NO_EXTENDED_MASTER_SECRET         | \
3063      SSL_OP_NO_ENCRYPT_THEN_MAC               | \
3064      SSL_OP_COOKIE_EXCHANGE                   | \
3065      SSL_OP_LEGACY_SERVER_CONNECT             | \
3066      SSL_OP_IGNORE_UNEXPECTED_EOF             )
3067 
3068 /* Total mask of connection-level options permitted or ignored under QUIC. */
3069 #define OSSL_QUIC_PERMITTED_OPTIONS_CONN        \
3070     (OSSL_LEGACY_SSL_OPTIONS                  | \
3071      OSSL_TLS1_2_OPTIONS                      | \
3072      SSL_OP_CIPHER_SERVER_PREFERENCE          | \
3073      SSL_OP_DISABLE_TLSEXT_CA_NAMES           | \
3074      SSL_OP_NO_TX_CERTIFICATE_COMPRESSION     | \
3075      SSL_OP_NO_RX_CERTIFICATE_COMPRESSION     | \
3076      SSL_OP_PRIORITIZE_CHACHA                 | \
3077      SSL_OP_NO_QUERY_MTU                      | \
3078      SSL_OP_NO_TICKET                         | \
3079      SSL_OP_NO_ANTI_REPLAY                    )
3080 
3081 /* Total mask of stream-level options permitted or ignored under QUIC. */
3082 #define OSSL_QUIC_PERMITTED_OPTIONS_STREAM      \
3083     (OSSL_LEGACY_SSL_OPTIONS                  | \
3084      OSSL_TLS1_2_OPTIONS                      | \
3085      SSL_OP_CLEANSE_PLAINTEXT                 )
3086 
3087 /* Total mask of options permitted on either connections or streams. */
3088 #define OSSL_QUIC_PERMITTED_OPTIONS             \
3089     (OSSL_QUIC_PERMITTED_OPTIONS_CONN |         \
3090      OSSL_QUIC_PERMITTED_OPTIONS_STREAM)
3091 
3092 #endif
3093