Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/ssl/statem/extensions.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#if defined(__TANDEM) && defined(_SPT_MODEL_)
11
#include <spthread.h>
12
#include <spt_extensions.h> /* timeval */
13
#endif
14
15
#include <string.h>
16
#include "internal/nelem.h"
17
#include "internal/cryptlib.h"
18
#include "internal/ssl_unwrap.h"
19
#include "../ssl_local.h"
20
#include "statem_local.h"
21
#include <openssl/ocsp.h>
22
#include <openssl/core_names.h>
23
24
/*
25
 * values for ext_defs ech_handling field
26
 * exceptionally, we don't conditionally compile that field to avoid a pile of
27
 * ifndefs all over the ext_defs values
28
 */
29
0
#define OSSL_ECH_HANDLING_CALL_BOTH 1 /* call constructor both times */
30
2.96M
#define OSSL_ECH_HANDLING_COMPRESS 2 /* compress outer value into inner */
31
#define OSSL_ECH_HANDLING_DUPLICATE 3 /* same value in inner and outer */
32
/*
33
 * DUPLICATE isn't really useful other than to show we can,
34
 * and for debugging/tests/coverage so may disappear. Changes mostly
35
 * won't affect the outer CH size, due to padding, but might for some
36
 * larger extensions.
37
 *
38
 * Note there is a co-dependency with test/recipes/75-test_quicapi.t:
39
 * If you change an |ech_handling| value, that may well affect the order
40
 * of extensions in a ClientHello, which is reflected in the test data
41
 * in test/recipes/75-test_quicapi_data/\*.txt files. To fix, you need
42
 * to look in test-runs/test_quicapi for the "new" files and then edit
43
 * (replacing actual octets with "?" in relevant places), and copy the
44
 * result back over to test/recipes/75-test_quicapi_data/. The reason
45
 * this happens is the ECH COMPRESS'd extensions need to be contiguous
46
 * in the ClientHello, so changes to/from COMPRESS affect extension
47
 * order, in inner and outer CH. There doesn't seem to be an easy,
48
 * generic, way to reconcile these compile-time changes with having
49
 * fixed value test files. Likely the best option is to decide on the
50
 * disposition of ECH COMPRESS or not and consider that an at least
51
 * medium-term thing. (But still allow other builds to vary at
52
 * compile time if they need something different.)
53
 */
54
#ifndef OPENSSL_NO_ECH
55
static int init_ech(SSL_CONNECTION *s, unsigned int context);
56
static int final_ech(SSL_CONNECTION *s, unsigned int context, int sent);
57
#endif /* OPENSSL_NO_ECH */
58
59
static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent);
60
static int init_server_name(SSL_CONNECTION *s, unsigned int context);
61
static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent);
62
static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
63
    int sent);
64
static int init_session_ticket(SSL_CONNECTION *s, unsigned int context);
65
#ifndef OPENSSL_NO_OCSP
66
static int init_status_request(SSL_CONNECTION *s, unsigned int context);
67
#endif
68
#ifndef OPENSSL_NO_NEXTPROTONEG
69
static int init_npn(SSL_CONNECTION *s, unsigned int context);
70
#endif
71
static int init_alpn(SSL_CONNECTION *s, unsigned int context);
72
static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent);
73
static int init_sig_algs_cert(SSL_CONNECTION *s, unsigned int context);
74
static int init_sig_algs(SSL_CONNECTION *s, unsigned int context);
75
static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context);
76
static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context);
77
static int init_certificate_authorities(SSL_CONNECTION *s,
78
    unsigned int context);
79
static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
80
    WPACKET *pkt,
81
    unsigned int context,
82
    X509 *x,
83
    size_t chainidx);
84
static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
85
    unsigned int context, X509 *x,
86
    size_t chainidx);
87
#ifndef OPENSSL_NO_SRP
88
static int init_srp(SSL_CONNECTION *s, unsigned int context);
89
#endif
90
static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context);
91
static int init_etm(SSL_CONNECTION *s, unsigned int context);
92
static int init_ems(SSL_CONNECTION *s, unsigned int context);
93
static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent);
94
static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context);
95
static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent);
96
#ifndef OPENSSL_NO_SRTP
97
static int init_srtp(SSL_CONNECTION *s, unsigned int context);
98
#endif
99
static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent);
100
static int final_supported_versions(SSL_CONNECTION *s, unsigned int context,
101
    int sent);
102
static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent);
103
static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
104
    int sent);
105
static int init_post_handshake_auth(SSL_CONNECTION *s, unsigned int context);
106
static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent);
107
static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context);
108
static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
109
    unsigned int context,
110
    X509 *x, size_t chainidx);
111
static int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt,
112
    unsigned int context,
113
    X509 *x, size_t chainidx);
114
115
/* Structure to define a built-in extension */
116
typedef struct extensions_definition_st {
117
    /* The defined type for the extension */
118
    unsigned int type;
119
    /*
120
     * The context that this extension applies to, e.g. what messages and
121
     * protocol versions
122
     */
123
    unsigned int context;
124
    /*
125
     * exceptionally, we don't conditionally compile this field to avoid a
126
     * pile of ifndefs all over the ext_defs values
127
     */
128
    int ech_handling; /* how to handle ECH for this extension type */
129
    /*
130
     * Initialise extension before parsing. Always called for relevant contexts
131
     * even if extension not present
132
     */
133
    int (*init)(SSL_CONNECTION *s, unsigned int context);
134
    /* Parse extension sent from client to server */
135
    int (*parse_ctos)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
136
        X509 *x, size_t chainidx);
137
    /* Parse extension send from server to client */
138
    int (*parse_stoc)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
139
        X509 *x, size_t chainidx);
140
    /* Construct extension sent from server to client */
141
    EXT_RETURN (*construct_stoc)(SSL_CONNECTION *s, WPACKET *pkt,
142
        unsigned int context,
143
        X509 *x, size_t chainidx);
144
    /* Construct extension sent from client to server */
145
    EXT_RETURN (*construct_ctos)(SSL_CONNECTION *s, WPACKET *pkt,
146
        unsigned int context,
147
        X509 *x, size_t chainidx);
148
    /*
149
     * Finalise extension after parsing. Always called where an extensions was
150
     * initialised even if the extension was not present. |sent| is set to 1 if
151
     * the extension was seen, or 0 otherwise.
152
     */
153
    int (*final)(SSL_CONNECTION *s, unsigned int context, int sent);
154
} EXTENSION_DEFINITION;
155
156
/*
157
 * Definitions of all built-in extensions. NOTE: Changes in the number or order
158
 * of these extensions should be mirrored with equivalent changes to the
159
 * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h.
160
 * Extensions should be added to test/ext_internal_test.c as well, as that
161
 * tests the ordering of the extensions.
162
 *
163
 * Each extension has an initialiser, a client and
164
 * server side parser and a finaliser. The initialiser is called (if the
165
 * extension is relevant to the given context) even if we did not see the
166
 * extension in the message that we received. The parser functions are only
167
 * called if we see the extension in the message. The finalisers are always
168
 * called if the initialiser was called.
169
 * There are also server and client side constructor functions which are always
170
 * called during message construction if the extension is relevant for the
171
 * given context.
172
 * The initialisation, parsing, finalisation and construction functions are
173
 * always called in the order defined in this list. Some extensions may depend
174
 * on others having been processed first, so the order of this list is
175
 * significant.
176
 * The extension context is defined by a series of flags which specify which
177
 * messages the extension is relevant to. These flags also specify whether the
178
 * extension is relevant to a particular protocol or protocol version.
179
 *
180
 * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
181
 * the end, keep these extensions before signature_algorithm.
182
 */
183
#define INVALID_EXTENSION { TLSEXT_TYPE_invalid, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL }
184
185
static const EXTENSION_DEFINITION ext_defs[] = {
186
    { TLSEXT_TYPE_renegotiate,
187
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
188
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
189
        OSSL_ECH_HANDLING_COMPRESS,
190
        NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
191
        tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
192
        final_renegotiate },
193
    { TLSEXT_TYPE_server_name,
194
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
195
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
196
        OSSL_ECH_HANDLING_CALL_BOTH,
197
        init_server_name,
198
        tls_parse_ctos_server_name, tls_parse_stoc_server_name,
199
        tls_construct_stoc_server_name, tls_construct_ctos_server_name,
200
        final_server_name },
201
    { TLSEXT_TYPE_max_fragment_length,
202
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
203
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
204
        OSSL_ECH_HANDLING_COMPRESS,
205
        NULL, tls_parse_ctos_maxfragmentlen, tls_parse_stoc_maxfragmentlen,
206
        tls_construct_stoc_maxfragmentlen, tls_construct_ctos_maxfragmentlen,
207
        final_maxfragmentlen },
208
#ifndef OPENSSL_NO_SRP
209
    { TLSEXT_TYPE_srp,
210
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
211
        OSSL_ECH_HANDLING_COMPRESS,
212
        init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL },
213
#else
214
    INVALID_EXTENSION,
215
#endif
216
    { TLSEXT_TYPE_ec_point_formats,
217
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
218
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
219
        OSSL_ECH_HANDLING_COMPRESS,
220
        init_ec_point_formats, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
221
        tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
222
        final_ec_pt_formats },
223
    { /*
224
       * "supported_groups" is spread across several specifications.
225
       * It was originally specified as "elliptic_curves" in RFC 4492,
226
       * and broadened to include named FFDH groups by RFC 7919.
227
       * Both RFCs 4492 and 7919 do not include a provision for the server
228
       * to indicate to the client the complete list of groups supported
229
       * by the server, with the server instead just indicating the
230
       * selected group for this connection in the ServerKeyExchange
231
       * message.  TLS 1.3 adds a scheme for the server to indicate
232
       * to the client its list of supported groups in the
233
       * EncryptedExtensions message, but none of the relevant
234
       * specifications permit sending supported_groups in the ServerHello.
235
       * Nonetheless (possibly due to the close proximity to the
236
       * "ec_point_formats" extension, which is allowed in the ServerHello),
237
       * there are several servers that send this extension in the
238
       * ServerHello anyway.  Up to and including the 1.1.0 release,
239
       * we did not check for the presence of nonpermitted extensions,
240
       * so to avoid a regression, we must permit this extension in the
241
       * TLS 1.2 ServerHello as well.
242
       *
243
       * Note that there is no tls_parse_stoc_supported_groups function,
244
       * so we do not perform any additional parsing, validation, or
245
       * processing on the server's group list -- this is just a minimal
246
       * change to preserve compatibility with these misbehaving servers.
247
       */
248
        TLSEXT_TYPE_supported_groups,
249
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
250
            | SSL_EXT_TLS1_2_SERVER_HELLO,
251
        OSSL_ECH_HANDLING_COMPRESS,
252
        NULL, tls_parse_ctos_supported_groups, NULL,
253
        tls_construct_stoc_supported_groups,
254
        tls_construct_ctos_supported_groups, NULL },
255
    { TLSEXT_TYPE_session_ticket,
256
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
257
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
258
        OSSL_ECH_HANDLING_COMPRESS,
259
        init_session_ticket, tls_parse_ctos_session_ticket,
260
        tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
261
        tls_construct_ctos_session_ticket, NULL },
262
#ifndef OPENSSL_NO_OCSP
263
    { TLSEXT_TYPE_status_request,
264
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
265
            | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
266
        OSSL_ECH_HANDLING_COMPRESS,
267
        init_status_request, tls_parse_ctos_status_request,
268
        tls_parse_stoc_status_request, tls_construct_stoc_status_request,
269
        tls_construct_ctos_status_request, NULL },
270
#else
271
    INVALID_EXTENSION,
272
#endif
273
#ifndef OPENSSL_NO_NEXTPROTONEG
274
    { TLSEXT_TYPE_next_proto_neg,
275
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
276
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
277
        OSSL_ECH_HANDLING_COMPRESS,
278
        init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
279
        tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL },
280
#else
281
    INVALID_EXTENSION,
282
#endif
283
    { /*
284
       * Must appear in this list after server_name so that finalisation
285
       * happens after server_name callbacks
286
       */
287
        TLSEXT_TYPE_application_layer_protocol_negotiation,
288
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
289
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
290
        OSSL_ECH_HANDLING_CALL_BOTH,
291
        init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
292
        tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn },
293
#ifndef OPENSSL_NO_SRTP
294
    { TLSEXT_TYPE_use_srtp,
295
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
296
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
297
        OSSL_ECH_HANDLING_COMPRESS,
298
        init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
299
        tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL },
300
#else
301
    INVALID_EXTENSION,
302
#endif
303
    { TLSEXT_TYPE_encrypt_then_mac,
304
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
305
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
306
        /*
307
         * If you want to demonstrate/exercise duplicate, then
308
         * this does that and has no effect on sizes, but it
309
         * will break the quicapi test (see above). Probably
310
         * best done in local tests and not committed to any
311
         * upstream.
312
         * OSSL_ECH_HANDLING_DUPLICATE,
313
         */
314
        OSSL_ECH_HANDLING_COMPRESS,
315
        init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
316
        tls_construct_stoc_etm, tls_construct_ctos_etm, NULL },
317
#ifndef OPENSSL_NO_CT
318
    { TLSEXT_TYPE_signed_certificate_timestamp,
319
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
320
            | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
321
        OSSL_ECH_HANDLING_COMPRESS,
322
        NULL,
323
        /*
324
         * No server side support for this, but can be provided by a custom
325
         * extension. This is an exception to the rule that custom extensions
326
         * cannot override built in ones.
327
         */
328
        NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL },
329
#else
330
    INVALID_EXTENSION,
331
#endif
332
    { TLSEXT_TYPE_extended_master_secret,
333
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
334
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
335
        OSSL_ECH_HANDLING_COMPRESS,
336
        init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
337
        tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems },
338
    { TLSEXT_TYPE_signature_algorithms_cert,
339
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
340
        OSSL_ECH_HANDLING_COMPRESS,
341
        init_sig_algs_cert, tls_parse_ctos_sig_algs_cert,
342
        tls_parse_ctos_sig_algs_cert,
343
        /* We do not generate signature_algorithms_cert at present. */
344
        NULL, NULL, NULL },
345
    {
346
        TLSEXT_TYPE_post_handshake_auth,
347
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
348
        OSSL_ECH_HANDLING_COMPRESS,
349
        init_post_handshake_auth,
350
        tls_parse_ctos_post_handshake_auth,
351
        NULL,
352
        NULL,
353
        tls_construct_ctos_post_handshake_auth,
354
        NULL,
355
    },
356
    { TLSEXT_TYPE_client_cert_type,
357
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
358
            | SSL_EXT_TLS1_2_SERVER_HELLO,
359
        OSSL_ECH_HANDLING_CALL_BOTH,
360
        init_client_cert_type,
361
        tls_parse_ctos_client_cert_type, tls_parse_stoc_client_cert_type,
362
        tls_construct_stoc_client_cert_type, tls_construct_ctos_client_cert_type,
363
        NULL },
364
    { TLSEXT_TYPE_server_cert_type,
365
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
366
            | SSL_EXT_TLS1_2_SERVER_HELLO,
367
        OSSL_ECH_HANDLING_CALL_BOTH,
368
        init_server_cert_type,
369
        tls_parse_ctos_server_cert_type, tls_parse_stoc_server_cert_type,
370
        tls_construct_stoc_server_cert_type, tls_construct_ctos_server_cert_type,
371
        NULL },
372
    { TLSEXT_TYPE_signature_algorithms,
373
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
374
        OSSL_ECH_HANDLING_COMPRESS,
375
        init_sig_algs, tls_parse_ctos_sig_algs,
376
        tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
377
        tls_construct_ctos_sig_algs, final_sig_algs },
378
    { TLSEXT_TYPE_supported_versions,
379
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
380
            | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY,
381
        OSSL_ECH_HANDLING_COMPRESS,
382
        NULL,
383
        /* Processed inline as part of version selection */
384
        NULL, tls_parse_stoc_supported_versions,
385
        tls_construct_stoc_supported_versions,
386
        tls_construct_ctos_supported_versions, final_supported_versions },
387
    { TLSEXT_TYPE_psk_kex_modes,
388
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
389
            | SSL_EXT_TLS1_3_ONLY,
390
        OSSL_ECH_HANDLING_COMPRESS,
391
        init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
392
        tls_construct_ctos_psk_kex_modes, NULL },
393
    { /*
394
       * Must be in this list after supported_groups. We need that to have
395
       * been parsed before we do this one.
396
       */
397
        TLSEXT_TYPE_key_share,
398
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
399
            | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY
400
            | SSL_EXT_TLS1_3_ONLY,
401
        OSSL_ECH_HANDLING_COMPRESS,
402
        NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
403
        tls_construct_stoc_key_share, tls_construct_ctos_key_share,
404
        final_key_share },
405
    { /* Must be after key_share */
406
        TLSEXT_TYPE_cookie,
407
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
408
            | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
409
        OSSL_ECH_HANDLING_COMPRESS,
410
        NULL, tls_parse_ctos_cookie, tls_parse_stoc_cookie,
411
        tls_construct_stoc_cookie, tls_construct_ctos_cookie, NULL },
412
    { /*
413
       * Special unsolicited ServerHello extension only used when
414
       * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but
415
       * ignore it.
416
       */
417
        TLSEXT_TYPE_cryptopro_bug,
418
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
419
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
420
        OSSL_ECH_HANDLING_COMPRESS,
421
        NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL },
422
    { TLSEXT_TYPE_compress_certificate,
423
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
424
            | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
425
        OSSL_ECH_HANDLING_COMPRESS,
426
        tls_init_compress_certificate,
427
        tls_parse_compress_certificate, tls_parse_compress_certificate,
428
        tls_construct_compress_certificate, tls_construct_compress_certificate,
429
        NULL },
430
    { TLSEXT_TYPE_early_data,
431
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
432
            | SSL_EXT_TLS1_3_NEW_SESSION_TICKET | SSL_EXT_TLS1_3_ONLY,
433
        OSSL_ECH_HANDLING_CALL_BOTH,
434
        NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
435
        tls_construct_stoc_early_data, tls_construct_ctos_early_data,
436
        final_early_data },
437
    {
438
        TLSEXT_TYPE_certificate_authorities,
439
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
440
            | SSL_EXT_TLS1_3_ONLY,
441
        OSSL_ECH_HANDLING_COMPRESS,
442
        init_certificate_authorities,
443
        tls_parse_certificate_authorities,
444
        tls_parse_certificate_authorities,
445
        tls_construct_certificate_authorities,
446
        tls_construct_certificate_authorities,
447
        NULL,
448
    },
449
#ifndef OPENSSL_NO_ECH
450
    { TLSEXT_TYPE_ech,
451
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
452
        OSSL_ECH_HANDLING_CALL_BOTH,
453
        init_ech,
454
        tls_parse_ctos_ech, tls_parse_stoc_ech,
455
        tls_construct_stoc_ech, tls_construct_ctos_ech,
456
        final_ech },
457
    { TLSEXT_TYPE_outer_extensions,
458
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
459
        OSSL_ECH_HANDLING_CALL_BOTH,
460
        NULL,
461
        NULL, NULL,
462
        NULL, NULL,
463
        NULL },
464
#else /* OPENSSL_NO_ECH */
465
    INVALID_EXTENSION,
466
    INVALID_EXTENSION,
467
#endif /* END_OPENSSL_NO_ECH */
468
    { /* Must be immediately before pre_shared_key */
469
        TLSEXT_TYPE_padding,
470
        SSL_EXT_CLIENT_HELLO,
471
        OSSL_ECH_HANDLING_CALL_BOTH,
472
        NULL,
473
        /* We send this, but don't read it */
474
        NULL, NULL, NULL, tls_construct_ctos_padding, NULL },
475
    { /* Required by the TLSv1.3 spec to always be the last extension */
476
        TLSEXT_TYPE_psk,
477
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
478
            | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
479
        OSSL_ECH_HANDLING_CALL_BOTH,
480
        NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
481
        tls_construct_ctos_psk, final_psk }
482
};
483
484
#ifndef OPENSSL_NO_ECH
485
/*
486
 * Copy an inner extension value to outer.
487
 * inner CH must have been pre-decoded into s->clienthello->pre_proc_exts
488
 * already.
489
 */
490
int ossl_ech_copy_inner2outer(SSL_CONNECTION *s, uint16_t ext_type,
491
    int ind, WPACKET *pkt)
492
0
{
493
0
    RAW_EXTENSION *myext = NULL, *raws = NULL;
494
495
0
    if (s == NULL || s->clienthello == NULL)
496
0
        return OSSL_ECH_SAME_EXT_ERR;
497
0
    raws = s->clienthello->pre_proc_exts;
498
0
    if (raws == NULL)
499
0
        return OSSL_ECH_SAME_EXT_ERR;
500
0
    myext = &raws[ind];
501
0
    OSSL_TRACE_BEGIN(TLS)
502
0
    {
503
0
        BIO_printf(trc_out, "inner2outer: Copying ext type %d to outer\n",
504
0
            ext_type);
505
0
    }
506
0
    OSSL_TRACE_END(TLS);
507
508
    /*
509
     * copy inner value to outer
510
     */
511
0
    if (PACKET_data(&myext->data) != NULL
512
0
        && PACKET_remaining(&myext->data) > 0) {
513
0
        if (!WPACKET_put_bytes_u16(pkt, ext_type)
514
0
            || !WPACKET_sub_memcpy_u16(pkt, PACKET_data(&myext->data),
515
0
                PACKET_remaining(&myext->data)))
516
0
            return OSSL_ECH_SAME_EXT_ERR;
517
0
    } else {
518
        /* empty extension */
519
0
        if (!WPACKET_put_bytes_u16(pkt, ext_type)
520
0
            || !WPACKET_put_bytes_u16(pkt, 0))
521
0
            return OSSL_ECH_SAME_EXT_ERR;
522
0
    }
523
0
    return 1;
524
0
}
525
526
/*
527
 * DUPEMALL is useful for testing - this turns off compression and
528
 * causes two calls to each extension constructor, which'd be the same
529
 * as making all entries in ext_tab use the CALL_BOTH value
530
 */
531
#undef DUPEMALL
532
533
/*
534
 * Check if we're using the same/different key shares
535
 * return 1 if same key share in inner and outer, 0 otherwise
536
 */
537
int ossl_ech_same_key_share(void)
538
0
{
539
#ifdef DUPEMALL
540
    return 0;
541
#endif
542
0
    return ext_defs[TLSEXT_IDX_key_share].ech_handling
543
0
        != OSSL_ECH_HANDLING_CALL_BOTH;
544
0
}
545
546
/*
547
 * say if extension at index |ind| in ext_defs is to be ECH compressed
548
 * return 1 if this one is to be compressed, 0 if not, -1 for error
549
 */
550
int ossl_ech_2bcompressed(size_t ind)
551
2.96M
{
552
2.96M
    const size_t nexts = OSSL_NELEM(ext_defs);
553
554
#ifdef DUPEMALL
555
    return 0;
556
#endif
557
2.96M
    if (ind >= nexts)
558
0
        return -1;
559
2.96M
    return ext_defs[ind].ech_handling == OSSL_ECH_HANDLING_COMPRESS;
560
2.96M
}
561
562
/* as needed, repeat extension from inner in outer handling compression */
563
int ossl_ech_same_ext(SSL_CONNECTION *s, WPACKET *pkt)
564
0
{
565
0
    unsigned int type = 0;
566
0
    int tind = 0, nexts = OSSL_NELEM(ext_defs);
567
568
#ifdef DUPEMALL
569
    return OSSL_ECH_SAME_EXT_CONTINUE;
570
#endif
571
0
    if (s == NULL || s->ext.ech.es == NULL)
572
0
        return OSSL_ECH_SAME_EXT_CONTINUE; /* nothing to do */
573
    /*
574
     * We store/access the index of the extension handler in
575
     * s->ext.ech.ext_ind, as we'd otherwise not know it here.
576
     * Be nice were there a better way to handle that.
577
     */
578
0
    tind = s->ext.ech.ext_ind;
579
    /* If this index'd extension won't be compressed, we're done */
580
0
    if (tind < 0 || tind >= nexts)
581
0
        return OSSL_ECH_SAME_EXT_ERR;
582
0
    type = ext_defs[tind].type;
583
0
    if (s->ext.ech.ch_depth == 1) {
584
        /* inner CH - just note compression as configured */
585
0
        if (ext_defs[tind].ech_handling != OSSL_ECH_HANDLING_COMPRESS)
586
0
            return OSSL_ECH_SAME_EXT_CONTINUE;
587
        /* mark this one to be "compressed" */
588
0
        if (s->ext.ech.n_outer_only >= OSSL_ECH_OUTERS_MAX)
589
0
            return OSSL_ECH_SAME_EXT_ERR;
590
0
        s->ext.ech.outer_only[s->ext.ech.n_outer_only] = type;
591
0
        s->ext.ech.n_outer_only++;
592
0
        OSSL_TRACE_BEGIN(TLS)
593
0
        {
594
0
            BIO_printf(trc_out, "ech_same_ext: Marking (type %u, ind %d "
595
0
                                "tot-comp %d) for compression\n",
596
0
                type, tind,
597
0
                (int)s->ext.ech.n_outer_only);
598
0
        }
599
0
        OSSL_TRACE_END(TLS);
600
0
        return OSSL_ECH_SAME_EXT_CONTINUE;
601
0
    } else {
602
        /* Copy value from inner to outer, or indicate a new value needed */
603
0
        if (s->clienthello == NULL || pkt == NULL)
604
0
            return OSSL_ECH_SAME_EXT_ERR;
605
0
        if (ext_defs[tind].ech_handling == OSSL_ECH_HANDLING_CALL_BOTH)
606
0
            return OSSL_ECH_SAME_EXT_CONTINUE;
607
0
        else
608
0
            return ossl_ech_copy_inner2outer(s, type, tind, pkt);
609
0
    }
610
    /* just in case - shouldn't happen */
611
0
    return OSSL_ECH_SAME_EXT_ERR;
612
0
}
613
#endif
614
615
/* Returns a TLSEXT_TYPE for the given index */
616
unsigned int ossl_get_extension_type(size_t idx)
617
0
{
618
0
    size_t num_exts = OSSL_NELEM(ext_defs);
619
620
0
    if (idx >= num_exts)
621
0
        return TLSEXT_TYPE_out_of_range;
622
623
0
    return ext_defs[idx].type;
624
0
}
625
626
/* Check whether an extension's context matches the current context */
627
static int validate_context(SSL_CONNECTION *s, unsigned int extctx,
628
    unsigned int thisctx)
629
354k
{
630
    /* Check we're allowed to use this extension in this context */
631
354k
    if ((thisctx & extctx) == 0)
632
106
        return 0;
633
634
354k
    if (SSL_CONNECTION_IS_DTLS(s)) {
635
69.3k
        if ((extctx & SSL_EXT_TLS_ONLY) != 0)
636
0
            return 0;
637
285k
    } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
638
15
        return 0;
639
15
    }
640
641
354k
    return 1;
642
354k
}
643
644
int tls_validate_all_contexts(SSL_CONNECTION *s, unsigned int thisctx,
645
    RAW_EXTENSION *exts)
646
62.0k
{
647
62.0k
    size_t i, num_exts, builtin_num = OSSL_NELEM(ext_defs), offset;
648
62.0k
    RAW_EXTENSION *thisext;
649
62.0k
    unsigned int context;
650
62.0k
    ENDPOINT role = ENDPOINT_BOTH;
651
652
62.0k
    if ((thisctx & SSL_EXT_CLIENT_HELLO) != 0)
653
0
        role = ENDPOINT_SERVER;
654
62.0k
    else if ((thisctx & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
655
40.4k
        role = ENDPOINT_CLIENT;
656
657
    /* Calculate the number of extensions in the extensions list */
658
62.0k
    num_exts = builtin_num + s->cert->custext.meths_count;
659
660
1.93M
    for (thisext = exts, i = 0; i < num_exts; i++, thisext++) {
661
1.87M
        if (!thisext->present)
662
1.76M
            continue;
663
664
111k
        if (i < builtin_num) {
665
111k
            context = ext_defs[i].context;
666
111k
        } else {
667
0
            custom_ext_method *meth = NULL;
668
669
0
            meth = custom_ext_find(&s->cert->custext, role, thisext->type,
670
0
                &offset);
671
0
            if (!ossl_assert(meth != NULL))
672
0
                return 0;
673
0
            context = meth->context;
674
0
        }
675
676
111k
        if (!validate_context(s, context, thisctx))
677
44
            return 0;
678
111k
    }
679
680
61.9k
    return 1;
681
62.0k
}
682
683
/*
684
 * Verify whether we are allowed to use the extension |type| in the current
685
 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
686
 * indicate the extension is not allowed. If returning 1 then |*found| is set to
687
 * the definition for the extension we found.
688
 */
689
static int verify_extension(SSL_CONNECTION *s, unsigned int context,
690
    unsigned int type, custom_ext_methods *meths,
691
    RAW_EXTENSION *rawexlist, RAW_EXTENSION **found)
692
130k
{
693
130k
    size_t i;
694
130k
    size_t builtin_num = OSSL_NELEM(ext_defs);
695
130k
    const EXTENSION_DEFINITION *thisext;
696
697
2.28M
    for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
698
2.24M
        if (type == thisext->type) {
699
90.2k
            if (!validate_context(s, thisext->context, context))
700
26
                return 0;
701
702
90.2k
            *found = &rawexlist[i];
703
90.2k
            return 1;
704
90.2k
        }
705
2.24M
    }
706
707
    /* Check the custom extensions */
708
40.1k
    if (meths != NULL) {
709
40.1k
        size_t offset = 0;
710
40.1k
        ENDPOINT role = ENDPOINT_BOTH;
711
40.1k
        custom_ext_method *meth = NULL;
712
713
40.1k
        if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
714
17.4k
#ifndef OPENSSL_NO_ECH
715
17.4k
            if (s->ext.ech.attempted == 1 && s->ext.ech.ch_depth == 1)
716
0
                role = ENDPOINT_CLIENT;
717
17.4k
            else
718
17.4k
                role = ENDPOINT_SERVER;
719
#else
720
            role = ENDPOINT_SERVER;
721
#endif
722
22.7k
        } else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
723
15.0k
            role = ENDPOINT_CLIENT;
724
725
40.1k
        meth = custom_ext_find(meths, role, type, &offset);
726
40.1k
        if (meth != NULL) {
727
6.45k
            if (!validate_context(s, meth->context, context))
728
3
                return 0;
729
6.45k
            *found = &rawexlist[offset + builtin_num];
730
6.45k
            return 1;
731
6.45k
        }
732
40.1k
    }
733
734
    /* Unknown extension. We allow it */
735
33.7k
    *found = NULL;
736
33.7k
    return 1;
737
40.1k
}
738
739
/*
740
 * Check whether the context defined for an extension |extctx| means whether
741
 * the extension is relevant for the current context |thisctx| or not. Returns
742
 * 1 if the extension is relevant for this context, and 0 otherwise
743
 */
744
int extension_is_relevant(SSL_CONNECTION *s, unsigned int extctx,
745
    unsigned int thisctx)
746
772k
{
747
772k
    int is_tls13;
748
749
    /*
750
     * For HRR we haven't selected the version yet but we know it will be
751
     * TLSv1.3
752
     */
753
772k
    if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
754
743
        is_tls13 = 1;
755
772k
    else
756
772k
        is_tls13 = SSL_CONNECTION_IS_TLS13(s);
757
758
772k
    if ((SSL_CONNECTION_IS_DTLS(s)
759
93.6k
            && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
760
        /*
761
         * Note that SSL_IS_TLS13() means "TLS 1.3 has been negotiated",
762
         * which is never true when generating the ClientHello.
763
         * However, version negotiation *has* occurred by the time the
764
         * ClientHello extensions are being parsed.
765
         * Be careful to allow TLS 1.3-only extensions when generating
766
         * the ClientHello.
767
         */
768
759k
        || (is_tls13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
769
758k
        || (!is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
770
171k
            && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
771
758k
        || (s->server && !is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
772
736k
        || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
773
36.6k
        return 0;
774
736k
    return 1;
775
772k
}
776
777
/*
778
 * Gather a list of all the extensions from the data in |packet]. |context|
779
 * tells us which message this extension is for. The raw extension data is
780
 * stored in |*res| on success. We don't actually process the content of the
781
 * extensions yet, except to check their types. This function also runs the
782
 * initialiser functions for all known extensions if |init| is nonzero (whether
783
 * we have collected them or not). If successful the caller is responsible for
784
 * freeing the contents of |*res|.
785
 *
786
 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
787
 * more than one extension of the same type in a ClientHello or ServerHello.
788
 * This function returns 1 if all extensions are unique and we have parsed their
789
 * types, and 0 if the extensions contain duplicates, could not be successfully
790
 * found, or an internal error occurred. We only check duplicates for
791
 * extensions that we know about. We ignore others.
792
 */
793
int tls_collect_extensions(SSL_CONNECTION *s, PACKET *packet,
794
    unsigned int context,
795
    RAW_EXTENSION **res, size_t *len, int init)
796
47.9k
{
797
47.9k
    PACKET extensions = *packet;
798
47.9k
    size_t i = 0;
799
47.9k
    size_t num_exts;
800
47.9k
    custom_ext_methods *exts = &s->cert->custext;
801
47.9k
    RAW_EXTENSION *raw_extensions = NULL;
802
47.9k
    const EXTENSION_DEFINITION *thisexd;
803
804
47.9k
    *res = NULL;
805
806
    /*
807
     * Initialise server side custom extensions. Client side is done during
808
     * construction of extensions for the ClientHello.
809
     */
810
47.9k
#ifndef OPENSSL_NO_ECH
811
47.9k
    if ((context & SSL_EXT_CLIENT_HELLO) != 0 && s->ext.ech.attempted == 0)
812
12.1k
        custom_ext_init(&s->cert->custext);
813
#else
814
    if ((context & SSL_EXT_CLIENT_HELLO) != 0)
815
        custom_ext_init(&s->cert->custext);
816
#endif
817
818
47.9k
    num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
819
47.9k
    raw_extensions = OPENSSL_calloc(num_exts, sizeof(*raw_extensions));
820
47.9k
    if (raw_extensions == NULL) {
821
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
822
0
        return 0;
823
0
    }
824
825
47.9k
    i = 0;
826
178k
    while (PACKET_remaining(&extensions) > 0) {
827
130k
        unsigned int type, idx;
828
130k
        PACKET extension;
829
130k
        RAW_EXTENSION *thisex;
830
831
130k
        if (!PACKET_get_net_2(&extensions, &type) || !PACKET_get_length_prefixed_2(&extensions, &extension)) {
832
274
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
833
274
            goto err;
834
274
        }
835
        /*
836
         * Verify this extension is allowed. We only check duplicates for
837
         * extensions that we recognise. We also have a special case for the
838
         * PSK extension, which must be the last one in the ClientHello.
839
         */
840
130k
        if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
841
130k
            || (thisex != NULL && thisex->present == 1)
842
130k
            || (type == TLSEXT_TYPE_psk
843
367
                && (context & SSL_EXT_CLIENT_HELLO) != 0
844
358
                && PACKET_remaining(&extensions) != 0)) {
845
78
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
846
78
            goto err;
847
78
        }
848
849
        /* The server must tolerate the unknown extension and complete. */
850
130k
        if (thisex == NULL)
851
33.7k
            continue;
852
853
96.6k
        idx = (unsigned int)(thisex - raw_extensions);
854
        /*-
855
         * Check that we requested this extension (if appropriate). Requests can
856
         * be sent in the ClientHello and CertificateRequest. Unsolicited
857
         * extensions can be sent in the NewSessionTicket. We only do this for
858
         * the built-in extensions. Custom extensions have a different but
859
         * similar check elsewhere.
860
         * Special cases:
861
         * - The HRR cookie extension is unsolicited
862
         * - The renegotiate extension is unsolicited (the client signals
863
         *   support via an SCSV)
864
         * - The signed_certificate_timestamp extension can be provided by a
865
         * custom extension or by the built-in version. We let the extension
866
         * itself handle unsolicited response checks.
867
         */
868
96.6k
        if (idx < OSSL_NELEM(ext_defs)
869
90.1k
            && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
870
49.5k
            && type != TLSEXT_TYPE_cookie
871
49.5k
            && type != TLSEXT_TYPE_renegotiate
872
36.1k
            && type != TLSEXT_TYPE_signed_certificate_timestamp
873
36.1k
            && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0
874
91
#ifndef OPENSSL_NO_GOST
875
91
            && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
876
87
                && type == TLSEXT_TYPE_cryptopro_bug)
877
96.6k
#endif
878
96.6k
        ) {
879
49
            SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
880
49
                SSL_R_UNSOLICITED_EXTENSION);
881
49
            goto err;
882
49
        }
883
96.5k
        thisex->data = extension;
884
96.5k
        thisex->present = 1;
885
96.5k
        thisex->type = type;
886
96.5k
        thisex->received_order = i++;
887
96.5k
        if (s->ext.debug_cb)
888
0
            s->ext.debug_cb(SSL_CONNECTION_GET_USER_SSL(s), !s->server,
889
0
                thisex->type, PACKET_data(&thisex->data),
890
0
                (int)PACKET_remaining(&thisex->data),
891
0
                s->ext.debug_arg);
892
96.5k
    }
893
894
47.5k
    if (init) {
895
        /*
896
         * Initialise all known extensions relevant to this context,
897
         * whether we have found them or not
898
         */
899
1.55M
        for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
900
1.50M
            i++, thisexd++) {
901
1.50M
            if (thisexd->init != NULL && (thisexd->context & context) != 0
902
521k
                && extension_is_relevant(s, thisexd->context, context)
903
461k
                && !thisexd->init(s, context)) {
904
                /* SSLfatal() already called */
905
0
                goto err;
906
0
            }
907
1.50M
        }
908
47.5k
    }
909
910
47.5k
    *res = raw_extensions;
911
47.5k
    if (len != NULL)
912
11.9k
        *len = num_exts;
913
47.5k
    return 1;
914
915
401
err:
916
401
    OPENSSL_free(raw_extensions);
917
401
    return 0;
918
47.5k
}
919
920
/*
921
 * Runs the parser for a given extension with index |idx|. |exts| contains the
922
 * list of all parsed extensions previously collected by
923
 * tls_collect_extensions(). The parser is only run if it is applicable for the
924
 * given |context| and the parser has not already been run. If this is for a
925
 * Certificate message, then we also provide the parser with the relevant
926
 * Certificate |x| and its position in the |chainidx| with 0 being the first
927
 * Certificate. Returns 1 on success or 0 on failure. If an extension is not
928
 * present this counted as success.
929
 */
930
int tls_parse_extension(SSL_CONNECTION *s, TLSEXT_INDEX idx, int context,
931
    RAW_EXTENSION *exts, X509 *x, size_t chainidx)
932
3.85M
{
933
3.85M
    RAW_EXTENSION *currext = &exts[idx];
934
3.85M
    int (*parser)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, X509 *x,
935
3.85M
        size_t chainidx)
936
3.85M
        = NULL;
937
938
    /* Skip if the extension is not present */
939
3.85M
    if (!currext->present)
940
3.59M
        return 1;
941
942
    /* Skip if we've already parsed this extension */
943
263k
    if (currext->parsed)
944
31.8k
        return 1;
945
946
231k
    currext->parsed = 1;
947
948
231k
    if (idx < OSSL_NELEM(ext_defs)) {
949
        /* We are handling a built-in extension */
950
214k
        const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
951
952
        /* Check if extension is defined for our protocol. If not, skip */
953
214k
        if (!extension_is_relevant(s, extdef->context, context))
954
7.80k
            return 1;
955
956
207k
        parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
957
958
207k
        if (parser != NULL)
959
202k
            return parser(s, &currext->data, context, x, chainidx);
960
961
        /*
962
         * If the parser is NULL we fall through to the custom extension
963
         * processing
964
         */
965
207k
    }
966
967
    /* Parse custom extensions */
968
21.0k
    return custom_ext_parse(s, context, currext->type,
969
21.0k
        PACKET_data(&currext->data),
970
21.0k
        PACKET_remaining(&currext->data),
971
21.0k
        x, chainidx);
972
231k
}
973
974
/*
975
 * Parse all remaining extensions that have not yet been parsed. Also calls the
976
 * finalisation for all extensions at the end if |fin| is nonzero, whether we
977
 * collected them or not. Returns 1 for success or 0 for failure. If we are
978
 * working on a Certificate message then we also pass the Certificate |x| and
979
 * its position in the |chainidx|, with 0 being the first certificate.
980
 */
981
int tls_parse_all_extensions(SSL_CONNECTION *s, int context,
982
    RAW_EXTENSION *exts, X509 *x,
983
    size_t chainidx, int fin)
984
126k
{
985
126k
    size_t i, numexts = OSSL_NELEM(ext_defs);
986
126k
    const EXTENSION_DEFINITION *thisexd;
987
988
    /* Calculate the number of extensions in the extensions list */
989
126k
    numexts += s->cert->custext.meths_count;
990
991
    /* Parse each extension in turn */
992
3.85M
    for (i = 0; i < numexts; i++) {
993
3.73M
        if (!tls_parse_extension(s, i, context, exts, x, chainidx)) {
994
            /* SSLfatal() already called */
995
6.83k
            return 0;
996
6.83k
        }
997
3.73M
    }
998
999
119k
    if (fin) {
1000
        /*
1001
         * Finalise all known extensions relevant to this context,
1002
         * whether we have found them or not
1003
         */
1004
3.70M
        for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
1005
3.58M
            i++, thisexd++) {
1006
3.58M
            if (thisexd->final != NULL && (thisexd->context & context) != 0
1007
657k
                && !thisexd->final(s, context, exts[i].present)) {
1008
                /* SSLfatal() already called */
1009
388
                return 0;
1010
388
            }
1011
3.58M
        }
1012
119k
    }
1013
1014
119k
    return 1;
1015
119k
}
1016
1017
int should_add_extension(SSL_CONNECTION *s, unsigned int extctx,
1018
    unsigned int thisctx, int max_version)
1019
2.94M
{
1020
    /* Skip if not relevant for our context */
1021
2.94M
    if ((extctx & thisctx) == 0)
1022
352k
        return 0;
1023
1024
    /* Check if this extension is defined for our protocol. If not, skip */
1025
2.59M
    if (!extension_is_relevant(s, extctx, thisctx)
1026
2.48M
        || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
1027
598k
            && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
1028
591k
            && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
1029
180k
        return 0;
1030
1031
2.41M
    return 1;
1032
2.59M
}
1033
1034
/*
1035
 * Construct all the extensions relevant to the current |context| and write
1036
 * them to |pkt|. If this is an extension for a Certificate in a Certificate
1037
 * message, then |x| will be set to the Certificate we are handling, and
1038
 * |chainidx| will indicate the position in the chainidx we are processing (with
1039
 * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
1040
 * failure construction stops at the first extension to fail to construct.
1041
 */
1042
int tls_construct_extensions(SSL_CONNECTION *s, WPACKET *pkt,
1043
    unsigned int context,
1044
    X509 *x, size_t chainidx)
1045
46.9k
{
1046
46.9k
    size_t i;
1047
46.9k
    int min_version, max_version = 0, reason;
1048
46.9k
    const EXTENSION_DEFINITION *thisexd;
1049
46.9k
    int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;
1050
46.9k
#ifndef OPENSSL_NO_ECH
1051
46.9k
    int pass;
1052
46.9k
#endif
1053
1054
46.9k
    if (!WPACKET_start_sub_packet_u16(pkt)
1055
        /*
1056
         * If extensions are of zero length then we don't even add the
1057
         * extensions length bytes to a ClientHello/ServerHello
1058
         * (for non-TLSv1.3).
1059
         */
1060
46.9k
        || ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
1061
43.7k
            && !WPACKET_set_flags(pkt,
1062
43.7k
                WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
1063
0
        if (!for_comp)
1064
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1065
0
        return 0;
1066
0
    }
1067
1068
46.9k
    if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
1069
36.1k
        reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
1070
36.1k
        if (reason != 0) {
1071
0
            if (!for_comp)
1072
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
1073
0
            return 0;
1074
0
        }
1075
36.1k
    }
1076
1077
    /* Add custom extensions first */
1078
46.9k
#ifndef OPENSSL_NO_ECH
1079
46.9k
    if ((context & SSL_EXT_CLIENT_HELLO) != 0 && s->ext.ech.attempted == 0)
1080
        /* On the server side with initialise during ClientHello parsing */
1081
36.1k
        custom_ext_init(&s->cert->custext);
1082
#else
1083
    if ((context & SSL_EXT_CLIENT_HELLO) != 0)
1084
        /* On the server side with initialise during ClientHello parsing */
1085
        custom_ext_init(&s->cert->custext);
1086
#endif
1087
46.9k
    if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) {
1088
        /* SSLfatal() already called */
1089
0
        return 0;
1090
0
    }
1091
1092
46.9k
#ifndef OPENSSL_NO_ECH
1093
    /*
1094
     * Two passes if doing real ECH - we first construct the
1095
     * to-be-ECH-compressed extensions, and then go around again
1096
     * constructing those that aren't to be ECH-compressed. We
1097
     * need to ensure this ordering so that all the ECH-compressed
1098
     * extensions are contiguous in the encoding. The actual
1099
     * compression happens later in ech_encode_inner().
1100
     */
1101
140k
    for (pass = 0; pass <= 1; pass++)
1102
93.8k
#endif
1103
1104
3.05M
        for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
1105
2.96M
            i++, thisexd++) {
1106
2.96M
            EXT_RETURN (*construct)(SSL_CONNECTION *s, WPACKET *pkt,
1107
2.96M
                unsigned int context,
1108
2.96M
                X509 *x, size_t chainidx);
1109
2.96M
            EXT_RETURN ret;
1110
1111
2.96M
#ifndef OPENSSL_NO_ECH
1112
            /* do compressed in pass 0, non-compressed in pass 1 */
1113
2.96M
            if (ossl_ech_2bcompressed((int)i) == pass)
1114
1.48M
                continue;
1115
            /* stash index - needed for COMPRESS ECH handling */
1116
1.48M
            s->ext.ech.ext_ind = (int)i;
1117
1.48M
#endif
1118
            /* Skip if not relevant for our context */
1119
1.48M
            if (!should_add_extension(s, thisexd->context, context, max_version))
1120
229k
                continue;
1121
1122
1.25M
            construct = s->server ? thisexd->construct_stoc
1123
1.25M
                                  : thisexd->construct_ctos;
1124
1125
1.25M
            if (construct == NULL)
1126
114k
                continue;
1127
1128
1.13M
            ret = construct(s, pkt, context, x, chainidx);
1129
1.13M
            if (ret == EXT_RETURN_FAIL) {
1130
                /* SSLfatal() already called */
1131
29
                return 0;
1132
29
            }
1133
1.13M
            if (ret == EXT_RETURN_SENT
1134
374k
                && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
1135
362k
                s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
1136
1.13M
        }
1137
1138
46.9k
#ifndef OPENSSL_NO_ECH
1139
    /*
1140
     * don't close yet if client in the middle of doing ECH, we'll
1141
     * eventually close this in ech_aad_and_encrypt() after we add
1142
     * the real ECH extension value
1143
     */
1144
46.9k
    if (s->server
1145
36.1k
        || context != SSL_EXT_CLIENT_HELLO
1146
36.1k
        || s->ext.ech.attempted == 0
1147
0
        || s->ext.ech.ch_depth == 1
1148
46.9k
        || s->ext.ech.grease == OSSL_ECH_IS_GREASE) {
1149
46.9k
        if (!WPACKET_close(pkt)) {
1150
0
            if (!for_comp)
1151
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1152
0
            return 0;
1153
0
        }
1154
46.9k
    }
1155
#else
1156
    if (!WPACKET_close(pkt)) {
1157
        if (!for_comp)
1158
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1159
        return 0;
1160
    }
1161
#endif
1162
46.9k
    return 1;
1163
46.9k
}
1164
1165
/*
1166
 * Built in extension finalisation and initialisation functions. All initialise
1167
 * or finalise the associated extension type for the given |context|. For
1168
 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
1169
 * otherwise. These functions return 1 on success or 0 on failure.
1170
 */
1171
1172
static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent)
1173
65.7k
{
1174
65.7k
    if (!s->server) {
1175
        /*
1176
         * Check if we can connect to a server that doesn't support safe
1177
         * renegotiation
1178
         */
1179
39.7k
        if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
1180
39.7k
            && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
1181
39.7k
            && !sent) {
1182
122
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1183
122
                SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1184
122
            return 0;
1185
122
        }
1186
1187
39.6k
        return 1;
1188
39.7k
    }
1189
1190
    /* Need RI if renegotiating */
1191
25.9k
    if (s->renegotiate
1192
0
        && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
1193
0
        && !sent) {
1194
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1195
0
            SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1196
0
        return 0;
1197
0
    }
1198
1199
25.9k
    return 1;
1200
25.9k
}
1201
1202
static ossl_inline void ssl_tsan_decr(const SSL_CTX *ctx,
1203
    TSAN_QUALIFIER int *stat)
1204
0
{
1205
0
    if (ssl_tsan_lock(ctx)) {
1206
0
        tsan_decr(stat);
1207
0
        ssl_tsan_unlock(ctx);
1208
0
    }
1209
0
}
1210
1211
static int init_server_name(SSL_CONNECTION *s, unsigned int context)
1212
113k
{
1213
113k
    if (s->server) {
1214
32.6k
        s->servername_done = 0;
1215
1216
32.6k
        OPENSSL_free(s->ext.hostname);
1217
32.6k
        s->ext.hostname = NULL;
1218
32.6k
    }
1219
1220
113k
    return 1;
1221
113k
}
1222
1223
#ifndef OPENSSL_NO_ECH
1224
/*
1225
 * Just note that ech is not yet done
1226
 * return 1 for good, 0 otherwise
1227
 */
1228
static int init_ech(SSL_CONNECTION *s, unsigned int context)
1229
7.28k
{
1230
7.28k
    const int nexts = OSSL_NELEM(ext_defs);
1231
1232
    /* we don't need this assert everywhere - anywhere is fine */
1233
7.28k
    if (!ossl_assert(TLSEXT_IDX_num_builtins == nexts)) {
1234
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1235
0
        return 0;
1236
0
    }
1237
7.28k
    if ((context & SSL_EXT_CLIENT_HELLO) != 0)
1238
104
        s->ext.ech.done = 0;
1239
7.28k
    return 1;
1240
7.28k
}
1241
1242
static int final_ech(SSL_CONNECTION *s, unsigned int context, int sent)
1243
16.3k
{
1244
16.3k
    if (s->server && s->ext.ech.success == 1
1245
0
        && s->ext.ech.inner_ech_seen_ok != 1) {
1246
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_ECH_REQUIRED);
1247
0
        return 0;
1248
0
    }
1249
16.3k
    return 1;
1250
16.3k
}
1251
#endif /* OPENSSL_NO_ECH */
1252
1253
static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent)
1254
63.7k
{
1255
63.7k
    int ret = SSL_TLSEXT_ERR_NOACK;
1256
63.7k
    int altmp = SSL_AD_UNRECOGNIZED_NAME;
1257
63.7k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1258
63.7k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1259
63.7k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1260
63.7k
    int was_ticket = (SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0;
1261
1262
63.7k
    if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL)) {
1263
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1264
0
        return 0;
1265
0
    }
1266
1267
63.7k
    if (sctx->ext.servername_cb != NULL)
1268
0
        ret = sctx->ext.servername_cb(ussl, &altmp,
1269
0
            sctx->ext.servername_arg);
1270
63.7k
    else if (s->session_ctx->ext.servername_cb != NULL)
1271
0
        ret = s->session_ctx->ext.servername_cb(ussl, &altmp,
1272
0
            s->session_ctx->ext.servername_arg);
1273
1274
    /*
1275
     * For servers, propagate the SNI hostname from the temporary
1276
     * storage in the SSL to the persistent SSL_SESSION, now that we
1277
     * know we accepted it.
1278
     * Clients make this copy when parsing the server's response to
1279
     * the extension, which is when they find out that the negotiation
1280
     * was successful.
1281
     */
1282
63.7k
    if (s->server) {
1283
19.7k
        if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
1284
            /* Only store the hostname in the session if we accepted it. */
1285
0
            OPENSSL_free(s->session->ext.hostname);
1286
0
            s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
1287
0
            if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) {
1288
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1289
0
            }
1290
0
        }
1291
19.7k
    }
1292
1293
    /*
1294
     * If we switched contexts (whether here or in the client_hello callback),
1295
     * move the sess_accept increment from the session_ctx to the new
1296
     * context, to avoid the confusing situation of having sess_accept_good
1297
     * exceed sess_accept (zero) for the new context.
1298
     */
1299
63.7k
    if (SSL_IS_FIRST_HANDSHAKE(s) && sctx != s->session_ctx
1300
0
        && s->hello_retry_request == SSL_HRR_NONE) {
1301
0
        ssl_tsan_counter(sctx, &sctx->stats.sess_accept);
1302
0
        ssl_tsan_decr(s->session_ctx, &s->session_ctx->stats.sess_accept);
1303
0
    }
1304
1305
    /*
1306
     * If we're expecting to send a ticket, and tickets were previously enabled,
1307
     * and now tickets are disabled, then turn off expected ticket.
1308
     * Also, if this is not a resumption, create a new session ID
1309
     */
1310
63.7k
    if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
1311
0
        && was_ticket && (SSL_get_options(ssl) & SSL_OP_NO_TICKET) != 0) {
1312
0
        s->ext.ticket_expected = 0;
1313
0
        if (!s->hit) {
1314
0
            SSL_SESSION *ss = SSL_get_session(ssl);
1315
1316
0
            if (ss != NULL) {
1317
0
                OPENSSL_free(ss->ext.tick);
1318
0
                ss->ext.tick = NULL;
1319
0
                ss->ext.ticklen = 0;
1320
0
                ss->ext.tick_lifetime_hint = 0;
1321
0
                ss->ext.tick_age_add = 0;
1322
0
                if (!ssl_generate_session_id(s, ss)) {
1323
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1324
0
                    return 0;
1325
0
                }
1326
0
            } else {
1327
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1328
0
                return 0;
1329
0
            }
1330
0
        }
1331
0
    }
1332
1333
63.7k
    switch (ret) {
1334
0
    case SSL_TLSEXT_ERR_ALERT_FATAL:
1335
0
        SSLfatal(s, altmp, SSL_R_CALLBACK_FAILED);
1336
0
        return 0;
1337
1338
0
    case SSL_TLSEXT_ERR_ALERT_WARNING:
1339
        /* TLSv1.3 doesn't have warning alerts so we suppress this */
1340
0
        if (!SSL_CONNECTION_IS_TLS13(s))
1341
0
            ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
1342
0
        s->servername_done = 0;
1343
0
        return 1;
1344
1345
63.7k
    case SSL_TLSEXT_ERR_NOACK:
1346
63.7k
        s->servername_done = 0;
1347
63.7k
        return 1;
1348
1349
0
    default:
1350
0
        return 1;
1351
63.7k
    }
1352
63.7k
}
1353
1354
static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
1355
    int sent)
1356
50.3k
{
1357
50.3k
    unsigned long alg_k, alg_a;
1358
1359
50.3k
    if (s->server)
1360
19.7k
        return 1;
1361
1362
30.6k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1363
30.6k
    alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1364
1365
    /*
1366
     * If we are client and using an elliptic curve cryptography cipher
1367
     * suite, then if server returns an EC point formats lists extension it
1368
     * must contain uncompressed.
1369
     */
1370
30.6k
    if (s->ext.ecpointformats != NULL
1371
0
        && s->ext.ecpointformats_len > 0
1372
0
        && s->ext.peer_ecpointformats != NULL
1373
0
        && s->ext.peer_ecpointformats_len > 0
1374
0
        && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
1375
        /* we are using an ECC cipher */
1376
0
        size_t i;
1377
0
        unsigned char *list = s->ext.peer_ecpointformats;
1378
1379
0
        for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
1380
0
            if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
1381
0
                break;
1382
0
        }
1383
0
        if (i == s->ext.peer_ecpointformats_len) {
1384
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1385
0
                SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1386
0
            return 0;
1387
0
        }
1388
0
    }
1389
1390
30.6k
    return 1;
1391
30.6k
}
1392
1393
static int init_session_ticket(SSL_CONNECTION *s, unsigned int context)
1394
95.3k
{
1395
95.3k
    if (!s->server)
1396
62.9k
        s->ext.ticket_expected = 0;
1397
1398
95.3k
    return 1;
1399
95.3k
}
1400
1401
#ifndef OPENSSL_NO_OCSP
1402
static int init_status_request(SSL_CONNECTION *s, unsigned int context)
1403
109k
{
1404
109k
    if (s->server) {
1405
32.6k
        s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
1406
76.9k
    } else {
1407
        /*
1408
         * Ensure we get sensible values passed to tlsext_status_cb in the event
1409
         * that we don't receive a status message
1410
         */
1411
76.9k
        OPENSSL_free(s->ext.ocsp.resp);
1412
76.9k
        s->ext.ocsp.resp = NULL;
1413
76.9k
        s->ext.ocsp.resp_len = 0;
1414
1415
76.9k
        sk_OCSP_RESPONSE_pop_free(s->ext.ocsp.resp_ex, OCSP_RESPONSE_free);
1416
76.9k
        s->ext.ocsp.resp_ex = NULL;
1417
76.9k
    }
1418
1419
109k
    return 1;
1420
109k
}
1421
#endif
1422
1423
#ifndef OPENSSL_NO_NEXTPROTONEG
1424
static int init_npn(SSL_CONNECTION *s, unsigned int context)
1425
95.3k
{
1426
95.3k
    s->s3.npn_seen = 0;
1427
1428
95.3k
    return 1;
1429
95.3k
}
1430
#endif
1431
1432
static int init_alpn(SSL_CONNECTION *s, unsigned int context)
1433
113k
{
1434
113k
    OPENSSL_free(s->s3.alpn_selected);
1435
113k
    s->s3.alpn_selected = NULL;
1436
113k
    s->s3.alpn_selected_len = 0;
1437
113k
    if (s->server) {
1438
32.6k
        OPENSSL_free(s->s3.alpn_proposed);
1439
32.6k
        s->s3.alpn_proposed = NULL;
1440
32.6k
        s->s3.alpn_proposed_len = 0;
1441
32.6k
    }
1442
113k
    return 1;
1443
113k
}
1444
1445
static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent)
1446
63.7k
{
1447
63.7k
    if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
1448
0
        s->ext.early_data_ok = 0;
1449
1450
63.7k
    if (!s->server || !SSL_CONNECTION_IS_TLS13(s))
1451
61.2k
        return 1;
1452
1453
    /*
1454
     * Call alpn_select callback if needed.  Has to be done after SNI and
1455
     * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
1456
     * we also have to do this before we decide whether to accept early_data.
1457
     * In TLSv1.3 we've already negotiated our cipher so we do this call now.
1458
     * For < TLSv1.3 we defer it until after cipher negotiation.
1459
     *
1460
     * On failure SSLfatal() already called.
1461
     */
1462
2.47k
    return tls_handle_alpn(s);
1463
63.7k
}
1464
1465
static int init_sig_algs(SSL_CONNECTION *s, unsigned int context)
1466
32.7k
{
1467
    /* Clear any signature algorithms extension received */
1468
32.7k
    OPENSSL_free(s->s3.tmp.peer_sigalgs);
1469
32.7k
    s->s3.tmp.peer_sigalgs = NULL;
1470
32.7k
    s->s3.tmp.peer_sigalgslen = 0;
1471
1472
32.7k
    return 1;
1473
32.7k
}
1474
1475
static int init_sig_algs_cert(SSL_CONNECTION *s,
1476
    ossl_unused unsigned int context)
1477
32.7k
{
1478
    /* Clear any signature algorithms extension received */
1479
32.7k
    OPENSSL_free(s->s3.tmp.peer_cert_sigalgs);
1480
32.7k
    s->s3.tmp.peer_cert_sigalgs = NULL;
1481
32.7k
    s->s3.tmp.peer_cert_sigalgslen = 0;
1482
1483
32.7k
    return 1;
1484
32.7k
}
1485
1486
#ifndef OPENSSL_NO_SRP
1487
static int init_srp(SSL_CONNECTION *s, unsigned int context)
1488
32.3k
{
1489
32.3k
    OPENSSL_free(s->srp_ctx.login);
1490
32.3k
    s->srp_ctx.login = NULL;
1491
1492
32.3k
    return 1;
1493
32.3k
}
1494
#endif
1495
1496
static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context)
1497
95.3k
{
1498
95.3k
    OPENSSL_free(s->ext.peer_ecpointformats);
1499
95.3k
    s->ext.peer_ecpointformats = NULL;
1500
95.3k
    s->ext.peer_ecpointformats_len = 0;
1501
1502
95.3k
    return 1;
1503
95.3k
}
1504
1505
static int init_etm(SSL_CONNECTION *s, unsigned int context)
1506
95.3k
{
1507
95.3k
    s->ext.use_etm = 0;
1508
1509
95.3k
    return 1;
1510
95.3k
}
1511
1512
static int init_ems(SSL_CONNECTION *s, unsigned int context)
1513
95.3k
{
1514
95.3k
    if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1515
34
        s->s3.flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
1516
34
        s->s3.flags |= TLS1_FLAGS_REQUIRED_EXTMS;
1517
34
    }
1518
1519
95.3k
    return 1;
1520
95.3k
}
1521
1522
static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent)
1523
65.6k
{
1524
    /*
1525
     * Check extended master secret extension is not dropped on
1526
     * renegotiation.
1527
     */
1528
65.6k
    if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
1529
54.3k
        && (s->s3.flags & TLS1_FLAGS_REQUIRED_EXTMS)) {
1530
2
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1531
2
        return 0;
1532
2
    }
1533
65.6k
    if (!s->server && s->hit) {
1534
        /*
1535
         * Check extended master secret extension is consistent with
1536
         * original session.
1537
         */
1538
0
        if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) != !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
1539
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1540
0
            return 0;
1541
0
        }
1542
0
    }
1543
1544
65.6k
    return 1;
1545
65.6k
}
1546
1547
static int init_certificate_authorities(SSL_CONNECTION *s, unsigned int context)
1548
394
{
1549
394
    sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
1550
394
    s->s3.tmp.peer_ca_names = NULL;
1551
394
    return 1;
1552
394
}
1553
1554
static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
1555
    WPACKET *pkt,
1556
    unsigned int context,
1557
    X509 *x,
1558
    size_t chainidx)
1559
78.6k
{
1560
78.6k
    const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s);
1561
1562
78.6k
    if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
1563
78.6k
        return EXT_RETURN_NOT_SENT;
1564
1565
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
1566
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1567
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1568
0
        return EXT_RETURN_FAIL;
1569
0
    }
1570
1571
0
    if (!construct_ca_names(s, ca_sk, pkt)) {
1572
        /* SSLfatal() already called */
1573
0
        return EXT_RETURN_FAIL;
1574
0
    }
1575
1576
0
    if (!WPACKET_close(pkt)) {
1577
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1578
0
        return EXT_RETURN_FAIL;
1579
0
    }
1580
1581
0
    return EXT_RETURN_SENT;
1582
0
}
1583
1584
static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
1585
    unsigned int context, X509 *x,
1586
    size_t chainidx)
1587
354
{
1588
354
    if (!parse_ca_names(s, pkt))
1589
328
        return 0;
1590
26
    if (PACKET_remaining(pkt) != 0) {
1591
16
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1592
16
        return 0;
1593
16
    }
1594
10
    return 1;
1595
26
}
1596
1597
#ifndef OPENSSL_NO_SRTP
1598
static int init_srtp(SSL_CONNECTION *s, unsigned int context)
1599
113k
{
1600
113k
    if (s->server)
1601
32.6k
        s->srtp_profile = NULL;
1602
1603
113k
    return 1;
1604
113k
}
1605
#endif
1606
1607
static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent)
1608
19.7k
{
1609
19.7k
    if (!sent && SSL_CONNECTION_IS_TLS13(s) && !s->hit) {
1610
49
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1611
49
            SSL_R_MISSING_SIGALGS_EXTENSION);
1612
49
        return 0;
1613
49
    }
1614
1615
19.7k
    return 1;
1616
19.7k
}
1617
1618
static int final_supported_versions(SSL_CONNECTION *s, unsigned int context,
1619
    int sent)
1620
47.4k
{
1621
47.4k
    if (!sent && context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) {
1622
31
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1623
31
            SSL_R_MISSING_SUPPORTED_VERSIONS_EXTENSION);
1624
31
        return 0;
1625
31
    }
1626
1627
47.3k
    return 1;
1628
47.4k
}
1629
1630
static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent)
1631
26.6k
{
1632
26.6k
#if !defined(OPENSSL_NO_TLS1_3)
1633
26.6k
    if (!SSL_CONNECTION_IS_TLS13(s))
1634
12.5k
        return 1;
1635
1636
    /* Nothing to do for key_share in an HRR */
1637
14.0k
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
1638
0
        return 1;
1639
1640
    /*
1641
     * If
1642
     *     we are a client
1643
     *     AND
1644
     *     we have no key_share
1645
     *     AND
1646
     *     (we are not resuming
1647
     *      OR the kex_mode doesn't allow non key_share resumes)
1648
     * THEN
1649
     *     fail;
1650
     */
1651
14.0k
    if (!s->server
1652
12.2k
        && !sent) {
1653
10
        if ((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1654
10
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_SUITABLE_KEY_SHARE);
1655
10
            return 0;
1656
10
        }
1657
0
        if (!s->hit) {
1658
0
            SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_NO_SUITABLE_KEY_SHARE);
1659
0
            return 0;
1660
0
        }
1661
0
    }
1662
    /*
1663
     * IF
1664
     *     we are a server
1665
     * THEN
1666
     *     IF
1667
     *         we have a suitable key_share
1668
     *     THEN
1669
     *         IF
1670
     *             we are stateless AND we have no cookie
1671
     *         THEN
1672
     *             send a HelloRetryRequest
1673
     *     ELSE
1674
     *         IF
1675
     *             we didn't already send a HelloRetryRequest
1676
     *             AND
1677
     *             the client sent a key_share extension
1678
     *             AND
1679
     *             (we are not resuming
1680
     *              OR the kex_mode allows key_share resumes)
1681
     *             AND
1682
     *             a shared group exists
1683
     *         THEN
1684
     *             send a HelloRetryRequest
1685
     *         ELSE IF
1686
     *             we are not resuming
1687
     *             OR
1688
     *             the kex_mode doesn't allow non key_share resumes
1689
     *         THEN
1690
     *             fail
1691
     *         ELSE IF
1692
     *             we are stateless AND we have no cookie
1693
     *         THEN
1694
     *             send a HelloRetryRequest
1695
     */
1696
14.0k
    if (s->server) {
1697
1.78k
        if (s->s3.peer_tmp != NULL) {
1698
            /* We have a suitable key_share */
1699
1.34k
            if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1700
0
                && !s->ext.cookieok) {
1701
0
                if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1702
                    /*
1703
                     * If we are stateless then we wouldn't know about any
1704
                     * previously sent HRR - so how can this be anything other
1705
                     * than 0?
1706
                     */
1707
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1708
0
                    return 0;
1709
0
                }
1710
0
                s->hello_retry_request = SSL_HRR_PENDING;
1711
0
                return 1;
1712
0
            }
1713
1.34k
        } else {
1714
            /* No suitable key_share */
1715
440
            if (s->hello_retry_request == SSL_HRR_NONE && sent
1716
376
                && (!s->hit
1717
376
                    || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) != 0)) {
1718
1719
                /* Did we detect group overlap in tls_parse_ctos_key_share ? */
1720
376
                if (s->s3.group_id_candidate != 0) {
1721
                    /* A shared group exists so send a HelloRetryRequest */
1722
370
                    s->s3.group_id = s->s3.group_id_candidate;
1723
370
                    s->hello_retry_request = SSL_HRR_PENDING;
1724
370
                    return 1;
1725
370
                }
1726
376
            }
1727
70
            if (!s->hit
1728
70
                || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1729
                /* Nothing left we can do - just fail */
1730
70
                SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE : SSL_AD_MISSING_EXTENSION,
1731
70
                    SSL_R_NO_SUITABLE_KEY_SHARE);
1732
70
                return 0;
1733
70
            }
1734
1735
0
            if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1736
0
                && !s->ext.cookieok) {
1737
0
                if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1738
                    /*
1739
                     * If we are stateless then we wouldn't know about any
1740
                     * previously sent HRR - so how can this be anything other
1741
                     * than 0?
1742
                     */
1743
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1744
0
                    return 0;
1745
0
                }
1746
0
                s->hello_retry_request = SSL_HRR_PENDING;
1747
0
                return 1;
1748
0
            }
1749
0
        }
1750
1751
        /*
1752
         * We have a key_share so don't send any more HelloRetryRequest
1753
         * messages
1754
         */
1755
1.34k
        if (s->hello_retry_request == SSL_HRR_PENDING)
1756
66
            s->hello_retry_request = SSL_HRR_COMPLETE;
1757
12.2k
    } else {
1758
        /*
1759
         * For a client side resumption with no key_share we need to generate
1760
         * the handshake secret (otherwise this is done during key_share
1761
         * processing).
1762
         */
1763
12.2k
        if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) {
1764
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1765
0
            return 0;
1766
0
        }
1767
12.2k
    }
1768
13.6k
#endif /* !defined(OPENSSL_NO_TLS1_3) */
1769
13.6k
    return 1;
1770
14.0k
}
1771
1772
static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context)
1773
286
{
1774
286
    s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
1775
286
    return 1;
1776
286
}
1777
1778
int tls_psk_do_binder(SSL_CONNECTION *s, const EVP_MD *md,
1779
    const unsigned char *msgstart,
1780
    size_t binderoffset, const unsigned char *binderin,
1781
    unsigned char *binderout, SSL_SESSION *sess, int sign,
1782
    int external)
1783
12
{
1784
12
    EVP_PKEY *mackey = NULL;
1785
12
    EVP_MD_CTX *mctx = NULL;
1786
12
    unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
1787
12
    unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
1788
12
    unsigned char *early_secret;
1789
    /* ASCII: "res binder", in hex for EBCDIC compatibility */
1790
12
    static const unsigned char resumption_label[] = "\x72\x65\x73\x20\x62\x69\x6E\x64\x65\x72";
1791
    /* ASCII: "ext binder", in hex for EBCDIC compatibility */
1792
12
    static const unsigned char external_label[] = "\x65\x78\x74\x20\x62\x69\x6E\x64\x65\x72";
1793
12
    const unsigned char *label;
1794
12
    size_t bindersize, labelsize, hashsize;
1795
12
    int hashsizei = EVP_MD_get_size(md);
1796
12
    int ret = -1;
1797
12
    int usepskfored = 0;
1798
12
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1799
12
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1800
1801
    /* Ensure cast to size_t is safe */
1802
12
    if (!ossl_assert(hashsizei > 0)) {
1803
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1804
0
        goto err;
1805
0
    }
1806
12
    hashsize = (size_t)hashsizei;
1807
1808
12
    if (external
1809
0
        && s->early_data_state == SSL_EARLY_DATA_CONNECTING
1810
0
        && s->session->ext.max_early_data == 0
1811
0
        && sess->ext.max_early_data > 0)
1812
0
        usepskfored = 1;
1813
1814
12
    if (external) {
1815
0
        label = external_label;
1816
0
        labelsize = sizeof(external_label) - 1;
1817
12
    } else {
1818
12
        label = resumption_label;
1819
12
        labelsize = sizeof(resumption_label) - 1;
1820
12
    }
1821
1822
    /*
1823
     * Generate the early_secret. On the server side we've selected a PSK to
1824
     * resume with (internal or external) so we always do this. On the client
1825
     * side we do this for a non-external (i.e. resumption) PSK or external PSK
1826
     * that will be used for early_data so that it is in place for sending early
1827
     * data. For client side external PSK not being used for early_data we
1828
     * generate it but store it away for later use.
1829
     */
1830
12
    if (s->server || !external || usepskfored)
1831
12
        early_secret = (unsigned char *)s->early_secret;
1832
0
    else
1833
0
        early_secret = (unsigned char *)sess->early_secret;
1834
1835
12
    if (!tls13_generate_secret(s, md, NULL, sess->master_key,
1836
12
            sess->master_key_length, early_secret)) {
1837
        /* SSLfatal() already called */
1838
0
        goto err;
1839
0
    }
1840
1841
    /*
1842
     * Create the handshake hash for the binder key...the messages so far are
1843
     * empty!
1844
     */
1845
12
    mctx = EVP_MD_CTX_new();
1846
12
    if (mctx == NULL
1847
12
        || EVP_DigestInit_ex(mctx, md, NULL) <= 0
1848
12
        || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1849
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1850
0
        goto err;
1851
0
    }
1852
1853
    /* Generate the binder key */
1854
12
    if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash,
1855
12
            hashsize, binderkey, hashsize, 1)) {
1856
        /* SSLfatal() already called */
1857
0
        goto err;
1858
0
    }
1859
1860
    /* Generate the finished key */
1861
12
    if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
1862
        /* SSLfatal() already called */
1863
0
        goto err;
1864
0
    }
1865
1866
12
    if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
1867
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1868
0
        goto err;
1869
0
    }
1870
1871
    /*
1872
     * Get a hash of the ClientHello up to the start of the binders. If we are
1873
     * following a HelloRetryRequest then this includes the hash of the first
1874
     * ClientHello and the HelloRetryRequest itself.
1875
     */
1876
12
    if (s->hello_retry_request == SSL_HRR_PENDING) {
1877
0
        size_t hdatalen;
1878
0
        long hdatalen_l;
1879
0
        void *hdata;
1880
1881
0
#ifndef OPENSSL_NO_ECH
1882
        /* handle the hashing as per ECH needs (on client) */
1883
0
        if (s->ext.ech.attempted == 1 && s->ext.ech.ch_depth == 1) {
1884
0
            if (ossl_ech_intbuf_fetch(s, (unsigned char **)&hdata, &hdatalen) != 1) {
1885
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1886
0
                goto err;
1887
0
            }
1888
0
        } else {
1889
0
#endif
1890
0
            hdatalen = hdatalen_l = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
1891
0
            if (hdatalen_l <= 0) {
1892
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
1893
0
                goto err;
1894
0
            }
1895
0
#ifndef OPENSSL_NO_ECH
1896
0
        }
1897
0
#endif
1898
1899
        /*
1900
         * For servers the handshake buffer data will include the second
1901
         * ClientHello - which we don't want - so we need to take that bit off.
1902
         */
1903
0
        if (s->server) {
1904
0
            PACKET hashprefix, msg;
1905
1906
            /* Find how many bytes are left after the first two messages */
1907
0
            if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
1908
0
                || !PACKET_forward(&hashprefix, 1)
1909
0
                || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
1910
0
                || !PACKET_forward(&hashprefix, 1)
1911
0
                || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
1912
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1913
0
                goto err;
1914
0
            }
1915
0
            hdatalen -= PACKET_remaining(&hashprefix);
1916
0
        }
1917
1918
0
        if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
1919
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1920
0
            goto err;
1921
0
        }
1922
0
    }
1923
1924
12
    if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
1925
12
        || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1926
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1927
0
        goto err;
1928
0
    }
1929
1930
12
    mackey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
1931
12
        sctx->propq, finishedkey,
1932
12
        hashsize);
1933
12
    if (mackey == NULL) {
1934
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1935
0
        goto err;
1936
0
    }
1937
1938
12
    if (!sign)
1939
12
        binderout = tmpbinder;
1940
1941
12
    if (sctx->propq != NULL)
1942
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
1943
0
            (char *)sctx->propq, 0);
1944
12
    bindersize = hashsize;
1945
12
    if (EVP_DigestSignInit_ex(mctx, NULL, EVP_MD_get0_name(md), sctx->libctx,
1946
12
            sctx->propq, mackey, params)
1947
12
            <= 0
1948
12
        || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
1949
12
        || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
1950
12
        || bindersize != hashsize) {
1951
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1952
0
        goto err;
1953
0
    }
1954
1955
12
    if (sign) {
1956
0
        ret = 1;
1957
12
    } else {
1958
        /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1959
12
        ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
1960
12
        if (!ret)
1961
12
            SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BINDER_DOES_NOT_VERIFY);
1962
12
    }
1963
1964
12
err:
1965
12
    OPENSSL_cleanse(binderkey, sizeof(binderkey));
1966
12
    OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
1967
12
    EVP_PKEY_free(mackey);
1968
12
    EVP_MD_CTX_free(mctx);
1969
12
    return ret;
1970
12
}
1971
1972
static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent)
1973
44.4k
{
1974
44.4k
    if (!sent)
1975
42.0k
        return 1;
1976
1977
2.39k
    if (!s->server) {
1978
0
        if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1979
0
            && sent
1980
0
            && !s->ext.early_data_ok) {
1981
            /*
1982
             * If we get here then the server accepted our early_data but we
1983
             * later realised that it shouldn't have done (e.g. inconsistent
1984
             * ALPN)
1985
             */
1986
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EARLY_DATA);
1987
0
            return 0;
1988
0
        }
1989
1990
0
        return 1;
1991
0
    }
1992
1993
2.39k
    if (s->max_early_data == 0
1994
0
        || !s->hit
1995
0
        || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
1996
0
        || !s->ext.early_data_ok
1997
0
        || s->hello_retry_request != SSL_HRR_NONE
1998
0
        || (s->allow_early_data_cb != NULL
1999
0
            && !s->allow_early_data_cb(SSL_CONNECTION_GET_USER_SSL(s),
2000
2.39k
                s->allow_early_data_cb_data))) {
2001
2.39k
        s->ext.early_data = SSL_EARLY_DATA_REJECTED;
2002
2.39k
    } else {
2003
0
        s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
2004
2005
0
        if (!tls13_change_cipher_state(s,
2006
0
                SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
2007
            /* SSLfatal() already called */
2008
0
            return 0;
2009
0
        }
2010
0
    }
2011
2012
2.39k
    return 1;
2013
2.39k
}
2014
2015
static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
2016
    int sent)
2017
82.5k
{
2018
82.5k
    if (s->session == NULL)
2019
0
        return 1;
2020
2021
    /* MaxFragmentLength defaults to disabled */
2022
82.5k
    if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)
2023
81.4k
        s->session->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_DISABLED;
2024
2025
82.5k
    if (USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) {
2026
908
        s->rlayer.rrlmethod->set_max_frag_len(s->rlayer.rrl,
2027
908
            GET_MAX_FRAGMENT_LENGTH(s->session));
2028
908
        s->rlayer.wrlmethod->set_max_frag_len(s->rlayer.wrl,
2029
908
            ssl_get_max_send_fragment(s));
2030
908
    }
2031
2032
82.5k
    return 1;
2033
82.5k
}
2034
2035
static int init_post_handshake_auth(SSL_CONNECTION *s,
2036
    ossl_unused unsigned int context)
2037
286
{
2038
286
    s->post_handshake_auth = SSL_PHA_NONE;
2039
2040
286
    return 1;
2041
286
}
2042
2043
/*
2044
 * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
2045
 * extension, servers MUST abort the handshake.
2046
 */
2047
static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent)
2048
46.8k
{
2049
46.8k
    if (s->server && sent && s->clienthello != NULL
2050
77
        && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) {
2051
18
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
2052
18
            SSL_R_MISSING_PSK_KEX_MODES_EXTENSION);
2053
18
        return 0;
2054
18
    }
2055
2056
46.8k
    return 1;
2057
46.8k
}
2058
2059
static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context)
2060
394
{
2061
394
    memset(sc->ext.compress_certificate_from_peer, 0,
2062
394
        sizeof(sc->ext.compress_certificate_from_peer));
2063
394
    return 1;
2064
394
}
2065
2066
/* The order these are put into the packet imply a preference order: [brotli, zlib, zstd] */
2067
static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
2068
    unsigned int context,
2069
    X509 *x, size_t chainidx)
2070
78.6k
{
2071
#ifndef OPENSSL_NO_COMP_ALG
2072
    int i;
2073
2074
    if (!ossl_comp_has_alg(0))
2075
        return EXT_RETURN_NOT_SENT;
2076
2077
    /* Server: Don't attempt to compress a non-X509 (i.e. an RPK) */
2078
    if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509) {
2079
        sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
2080
        return EXT_RETURN_NOT_SENT;
2081
    }
2082
2083
    /* Client: If we sent a client cert-type extension, don't indicate compression */
2084
    if (!sc->server && sc->ext.client_cert_type_ctos) {
2085
        sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
2086
        return EXT_RETURN_NOT_SENT;
2087
    }
2088
2089
    /* Do not indicate we support receiving compressed certificates */
2090
    if ((sc->options & SSL_OP_NO_RX_CERTIFICATE_COMPRESSION) != 0)
2091
        return EXT_RETURN_NOT_SENT;
2092
2093
    if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
2094
        return EXT_RETURN_NOT_SENT;
2095
#ifndef OPENSSL_NO_ECH
2096
    ECH_SAME_EXT(sc, context, pkt);
2097
#endif
2098
2099
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_compress_certificate)
2100
        || !WPACKET_start_sub_packet_u16(pkt)
2101
        || !WPACKET_start_sub_packet_u8(pkt))
2102
        goto err;
2103
2104
    for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) {
2105
        if (!WPACKET_put_bytes_u16(pkt, sc->cert_comp_prefs[i]))
2106
            goto err;
2107
    }
2108
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt))
2109
        goto err;
2110
2111
    sc->ext.compress_certificate_sent = 1;
2112
    return EXT_RETURN_SENT;
2113
err:
2114
    SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2115
    return EXT_RETURN_FAIL;
2116
#else
2117
78.6k
    return EXT_RETURN_NOT_SENT;
2118
78.6k
#endif
2119
78.6k
}
2120
2121
#ifndef OPENSSL_NO_COMP_ALG
2122
static int tls_comp_in_pref(SSL_CONNECTION *sc, int alg)
2123
{
2124
    int i;
2125
2126
    /* ossl_comp_has_alg() considers 0 as "any" */
2127
    if (alg == 0)
2128
        return 0;
2129
    /* Make sure algorithm is enabled */
2130
    if (!ossl_comp_has_alg(alg))
2131
        return 0;
2132
    /* If no preferences are set, it's ok */
2133
    if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
2134
        return 1;
2135
    /* Find the algorithm */
2136
    for (i = 0; i < TLSEXT_comp_cert_limit; i++)
2137
        if (sc->cert_comp_prefs[i] == alg)
2138
            return 1;
2139
    return 0;
2140
}
2141
#endif
2142
2143
int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt, unsigned int context,
2144
    X509 *x, size_t chainidx)
2145
83
{
2146
#ifndef OPENSSL_NO_COMP_ALG
2147
    PACKET supported_comp_algs;
2148
    unsigned int comp;
2149
    int already_set[TLSEXT_comp_cert_limit];
2150
    int j = 0;
2151
2152
    /* If no algorithms are available, ignore the extension */
2153
    if (!ossl_comp_has_alg(0))
2154
        return 1;
2155
2156
    /* Don't attempt to compress a non-X509 (i.e. an RPK) */
2157
    if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509)
2158
        return 1;
2159
    if (!sc->server && sc->ext.client_cert_type != TLSEXT_cert_type_x509)
2160
        return 1;
2161
2162
    /* Ignore the extension and don't send compressed certificates */
2163
    if ((sc->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
2164
        return 1;
2165
2166
    if (!PACKET_as_length_prefixed_1(pkt, &supported_comp_algs)
2167
        || PACKET_remaining(&supported_comp_algs) == 0) {
2168
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2169
        return 0;
2170
    }
2171
2172
    memset(already_set, 0, sizeof(already_set));
2173
    /*
2174
     * The preference array has real values, so take a look at each
2175
     * value coming in, and make sure it's in our preference list
2176
     * The array is 0 (i.e. "none") terminated
2177
     * The preference list only contains supported algorithms
2178
     */
2179
    while (PACKET_get_net_2(&supported_comp_algs, &comp)) {
2180
        if (tls_comp_in_pref(sc, comp) && !already_set[comp]) {
2181
            sc->ext.compress_certificate_from_peer[j++] = comp;
2182
            already_set[comp] = 1;
2183
        }
2184
    }
2185
    if (PACKET_remaining(&supported_comp_algs) != 0) {
2186
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2187
        return 0;
2188
    }
2189
#endif
2190
83
    return 1;
2191
83
}
2192
2193
static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context)
2194
113k
{
2195
    /* Only reset when parsing client hello */
2196
113k
    if (sc->server) {
2197
32.6k
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2198
32.6k
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2199
32.6k
    }
2200
113k
    return 1;
2201
113k
}
2202
2203
static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context)
2204
113k
{
2205
    /* Only reset when parsing client hello */
2206
113k
    if (sc->server) {
2207
32.6k
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2208
32.6k
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2209
32.6k
    }
2210
113k
    return 1;
2211
113k
}