Coverage Report

Created: 2026-08-18 07:24

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