Coverage Report

Created: 2026-07-19 06:36

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
 * Check whether the context defined for an extension |extctx| means whether
803
 * the extension is relevant for the current context |thisctx| or not. Returns
804
 * 1 if the extension is relevant for this context, and 0 otherwise
805
 */
806
int extension_is_relevant(SSL_CONNECTION *s, unsigned int extctx,
807
    unsigned int thisctx)
808
0
{
809
0
    int is_tls13;
810
811
    /*
812
     * For HRR we haven't selected the version yet but we know it will be
813
     * TLSv1.3
814
     */
815
0
    if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
816
0
        is_tls13 = 1;
817
0
    else
818
0
        is_tls13 = SSL_CONNECTION_IS_TLS13(s);
819
820
0
    if ((SSL_CONNECTION_IS_DTLS(s)
821
0
            && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
822
        /*
823
         * Note that SSL_IS_TLS13() means "TLS 1.3 has been negotiated",
824
         * which is never true when generating the ClientHello.
825
         * However, version negotiation *has* occurred by the time the
826
         * ClientHello extensions are being parsed.
827
         * Be careful to allow TLS 1.3-only extensions when generating
828
         * the ClientHello.
829
         */
830
0
        || (is_tls13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
831
0
        || (!is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
832
0
            && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
833
0
        || (s->server && !is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
834
0
        || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
835
0
        return 0;
836
0
    return 1;
837
0
}
838
839
/*
840
 * Gather a list of all the extensions from the data in |packet]. |context|
841
 * tells us which message this extension is for. The raw extension data is
842
 * stored in |*res| on success. We don't actually process the content of the
843
 * extensions yet, except to check their types. This function also runs the
844
 * initialiser functions for all known extensions if |init| is nonzero (whether
845
 * we have collected them or not). If successful the caller is responsible for
846
 * freeing the contents of |*res|.
847
 *
848
 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
849
 * more than one extension of the same type in a ClientHello or ServerHello.
850
 * This function returns 1 if all extensions are unique and we have parsed their
851
 * types, and 0 if the extensions contain duplicates, could not be successfully
852
 * found, or an internal error occurred. We only check duplicates for
853
 * extensions that we know about. We ignore others.
854
 */
855
int tls_collect_extensions(SSL_CONNECTION *s, PACKET *packet,
856
    unsigned int context,
857
    RAW_EXTENSION **res, size_t *len, int init)
858
0
{
859
0
    PACKET extensions = *packet;
860
0
    size_t i = 0;
861
0
    size_t num_exts;
862
0
    custom_ext_methods *exts = &s->cert->custext;
863
0
    RAW_EXTENSION *raw_extensions = NULL;
864
0
    const EXTENSION_DEFINITION *thisexd;
865
866
0
    *res = NULL;
867
868
    /*
869
     * Initialise server side custom extensions. Client side is done during
870
     * construction of extensions for the ClientHello.
871
     */
872
0
#ifndef OPENSSL_NO_ECH
873
0
    if ((context & SSL_EXT_CLIENT_HELLO) != 0 && s->ext.ech.attempted == 0)
874
0
        custom_ext_init(&s->cert->custext);
875
#else
876
    if ((context & SSL_EXT_CLIENT_HELLO) != 0)
877
        custom_ext_init(&s->cert->custext);
878
#endif
879
880
0
    num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
881
0
    raw_extensions = OPENSSL_calloc(num_exts, sizeof(*raw_extensions));
882
0
    if (raw_extensions == NULL) {
883
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
884
0
        return 0;
885
0
    }
886
887
0
    i = 0;
888
0
    while (PACKET_remaining(&extensions) > 0) {
889
0
        unsigned int type, idx;
890
0
        PACKET extension;
891
0
        RAW_EXTENSION *thisex;
892
893
0
        if (!PACKET_get_net_2(&extensions, &type) || !PACKET_get_length_prefixed_2(&extensions, &extension)) {
894
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
895
0
            goto err;
896
0
        }
897
        /*
898
         * Verify this extension is allowed. We only check duplicates for
899
         * extensions that we recognise. We also have a special case for the
900
         * PSK extension, which must be the last one in the ClientHello.
901
         */
902
0
        if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
903
0
            || (thisex != NULL && thisex->present == 1)
904
0
            || (type == TLSEXT_TYPE_psk
905
0
                && (context & SSL_EXT_CLIENT_HELLO) != 0
906
0
                && PACKET_remaining(&extensions) != 0)) {
907
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
908
0
            goto err;
909
0
        }
910
911
        /* The server must tolerate the unknown extension and complete. */
912
0
        if (thisex == NULL)
913
0
            continue;
914
915
0
        idx = (unsigned int)(thisex - raw_extensions);
916
        /*-
917
         * Check that we requested this extension (if appropriate). Requests can
918
         * be sent in the ClientHello and CertificateRequest. Unsolicited
919
         * extensions can be sent in the NewSessionTicket. We only do this for
920
         * the built-in extensions. Custom extensions have a different but
921
         * similar check elsewhere.
922
         * Special cases:
923
         * - The HRR cookie extension is unsolicited
924
         * - The renegotiate extension is unsolicited (the client signals
925
         *   support via an SCSV)
926
         * - The signed_certificate_timestamp extension can be provided by a
927
         * custom extension or by the built-in version. We let the extension
928
         * itself handle unsolicited response checks.
929
         */
930
0
        if (idx < OSSL_NELEM(ext_defs)
931
0
            && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
932
0
            && type != TLSEXT_TYPE_cookie
933
0
            && type != TLSEXT_TYPE_renegotiate
934
0
            && type != TLSEXT_TYPE_signed_certificate_timestamp
935
0
            && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0
936
0
#ifndef OPENSSL_NO_GOST
937
0
            && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
938
0
                && type == TLSEXT_TYPE_cryptopro_bug)
939
0
#endif
940
0
        ) {
941
0
            SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
942
0
                SSL_R_UNSOLICITED_EXTENSION);
943
0
            goto err;
944
0
        }
945
0
        thisex->data = extension;
946
0
        thisex->present = 1;
947
0
        thisex->type = type;
948
0
        thisex->received_order = i++;
949
0
        if (s->ext.debug_cb)
950
0
            s->ext.debug_cb(SSL_CONNECTION_GET_USER_SSL(s), !s->server,
951
0
                thisex->type, PACKET_data(&thisex->data),
952
0
                (int)PACKET_remaining(&thisex->data),
953
0
                s->ext.debug_arg);
954
0
    }
955
956
0
    if (init) {
957
        /*
958
         * Initialise all known extensions relevant to this context,
959
         * whether we have found them or not
960
         */
961
0
        for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
962
0
            i++, thisexd++) {
963
0
            if (thisexd->init != NULL && (thisexd->context & context) != 0
964
0
                && extension_is_relevant(s, thisexd->context, context)
965
0
                && !thisexd->init(s, context)) {
966
                /* SSLfatal() already called */
967
0
                goto err;
968
0
            }
969
0
        }
970
0
    }
971
972
0
    *res = raw_extensions;
973
0
    if (len != NULL)
974
0
        *len = num_exts;
975
0
    return 1;
976
977
0
err:
978
0
    OPENSSL_free(raw_extensions);
979
0
    return 0;
980
0
}
981
982
/*
983
 * Runs the parser for a given extension with index |idx|. |exts| contains the
984
 * list of all parsed extensions previously collected by
985
 * tls_collect_extensions(). The parser is only run if it is applicable for the
986
 * given |context| and the parser has not already been run. If this is for a
987
 * Certificate message, then we also provide the parser with the relevant
988
 * Certificate |x| and its position in the |chainidx| with 0 being the first
989
 * Certificate. Returns 1 on success or 0 on failure. If an extension is not
990
 * present this counted as success.
991
 */
992
int tls_parse_extension(SSL_CONNECTION *s, TLSEXT_INDEX idx, int context,
993
    RAW_EXTENSION *exts, X509 *x, size_t chainidx)
994
0
{
995
0
    RAW_EXTENSION *currext = &exts[idx];
996
0
    int (*parser)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, X509 *x,
997
0
        size_t chainidx)
998
0
        = NULL;
999
1000
    /* Skip if the extension is not present */
1001
0
    if (!currext->present)
1002
0
        return 1;
1003
1004
    /* Skip if we've already parsed this extension */
1005
0
    if (currext->parsed)
1006
0
        return 1;
1007
1008
0
    currext->parsed = 1;
1009
1010
0
    if (idx < OSSL_NELEM(ext_defs)) {
1011
        /* We are handling a built-in extension */
1012
0
        const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
1013
1014
        /* Check if extension is defined for our protocol. If not, skip */
1015
0
        if (!extension_is_relevant(s, extdef->context, context))
1016
0
            return 1;
1017
1018
0
        parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
1019
1020
0
        if (parser != NULL)
1021
0
            return parser(s, &currext->data, context, x, chainidx);
1022
1023
        /*
1024
         * If the parser is NULL we fall through to the custom extension
1025
         * processing
1026
         */
1027
0
    }
1028
1029
    /* Parse custom extensions */
1030
0
    return custom_ext_parse(s, context, currext->type,
1031
0
        PACKET_data(&currext->data),
1032
0
        PACKET_remaining(&currext->data),
1033
0
        x, chainidx);
1034
0
}
1035
1036
/*
1037
 * Parse all remaining extensions that have not yet been parsed. Also calls the
1038
 * finalisation for all extensions at the end if |fin| is nonzero, whether we
1039
 * collected them or not. Returns 1 for success or 0 for failure. If we are
1040
 * working on a Certificate message then we also pass the Certificate |x| and
1041
 * its position in the |chainidx|, with 0 being the first certificate.
1042
 */
1043
int tls_parse_all_extensions(SSL_CONNECTION *s, int context,
1044
    RAW_EXTENSION *exts, X509 *x,
1045
    size_t chainidx, int fin)
1046
0
{
1047
0
    size_t i, numexts = OSSL_NELEM(ext_defs);
1048
0
    const EXTENSION_DEFINITION *thisexd;
1049
1050
    /* Calculate the number of extensions in the extensions list */
1051
0
    numexts += s->cert->custext.meths_count;
1052
1053
    /* Parse each extension in turn */
1054
0
    for (i = 0; i < numexts; i++) {
1055
0
        if (!tls_parse_extension(s, i, context, exts, x, chainidx)) {
1056
            /* SSLfatal() already called */
1057
0
            return 0;
1058
0
        }
1059
0
    }
1060
1061
0
    if (fin) {
1062
        /*
1063
         * Finalise all known extensions relevant to this context,
1064
         * whether we have found them or not
1065
         */
1066
0
        for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
1067
0
            i++, thisexd++) {
1068
0
            if (thisexd->final != NULL && (thisexd->context & context) != 0
1069
0
                && !thisexd->final(s, context, exts[i].present)) {
1070
                /* SSLfatal() already called */
1071
0
                return 0;
1072
0
            }
1073
0
        }
1074
0
    }
1075
1076
0
    return 1;
1077
0
}
1078
1079
int should_add_extension(SSL_CONNECTION *s, unsigned int extctx,
1080
    unsigned int thisctx, int max_version)
1081
0
{
1082
    /* Skip if not relevant for our context */
1083
0
    if ((extctx & thisctx) == 0)
1084
0
        return 0;
1085
1086
    /* Check if this extension is defined for our protocol. If not, skip */
1087
0
    if (!extension_is_relevant(s, extctx, thisctx)
1088
0
        || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
1089
0
            && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
1090
0
            && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
1091
0
        return 0;
1092
1093
0
    return 1;
1094
0
}
1095
1096
/*
1097
 * Construct all the extensions relevant to the current |context| and write
1098
 * them to |pkt|. If this is an extension for a Certificate in a Certificate
1099
 * message, then |x| will be set to the Certificate we are handling, and
1100
 * |chainidx| will indicate the position in the chainidx we are processing (with
1101
 * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
1102
 * failure construction stops at the first extension to fail to construct.
1103
 */
1104
int tls_construct_extensions(SSL_CONNECTION *s, WPACKET *pkt,
1105
    unsigned int context,
1106
    X509 *x, size_t chainidx)
1107
0
{
1108
0
    size_t i;
1109
0
    int min_version, max_version = 0, reason;
1110
0
    const EXTENSION_DEFINITION *thisexd;
1111
0
    int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;
1112
0
#ifndef OPENSSL_NO_ECH
1113
0
    int pass;
1114
0
#endif
1115
1116
0
    if (!WPACKET_start_sub_packet_u16(pkt)
1117
        /*
1118
         * If extensions are of zero length then we don't even add the
1119
         * extensions length bytes to a ClientHello/ServerHello
1120
         * (for non-TLSv1.3).
1121
         */
1122
0
        || ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
1123
0
            && !WPACKET_set_flags(pkt,
1124
0
                WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
1125
0
        if (!for_comp)
1126
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1127
0
        return 0;
1128
0
    }
1129
1130
0
    if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
1131
0
        reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
1132
0
        if (reason != 0) {
1133
0
            if (!for_comp)
1134
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
1135
0
            return 0;
1136
0
        }
1137
0
    }
1138
1139
    /* Add custom extensions first */
1140
0
#ifndef OPENSSL_NO_ECH
1141
0
    if ((context & SSL_EXT_CLIENT_HELLO) != 0 && s->ext.ech.attempted == 0)
1142
        /* On the server side with initialise during ClientHello parsing */
1143
0
        custom_ext_init(&s->cert->custext);
1144
#else
1145
    if ((context & SSL_EXT_CLIENT_HELLO) != 0)
1146
        /* On the server side with initialise during ClientHello parsing */
1147
        custom_ext_init(&s->cert->custext);
1148
#endif
1149
0
    if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) {
1150
        /* SSLfatal() already called */
1151
0
        return 0;
1152
0
    }
1153
1154
0
#ifndef OPENSSL_NO_ECH
1155
    /*
1156
     * Two passes if doing real ECH - we first construct the
1157
     * to-be-ECH-compressed extensions, and then go around again
1158
     * constructing those that aren't to be ECH-compressed. We
1159
     * need to ensure this ordering so that all the ECH-compressed
1160
     * extensions are contiguous in the encoding. The actual
1161
     * compression happens later in ech_encode_inner().
1162
     */
1163
0
    for (pass = 0; pass <= 1; pass++)
1164
0
#endif
1165
1166
0
        for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
1167
0
            i++, thisexd++) {
1168
0
            EXT_RETURN (*construct)(SSL_CONNECTION *s, WPACKET *pkt,
1169
0
                unsigned int context,
1170
0
                X509 *x, size_t chainidx);
1171
0
            EXT_RETURN ret;
1172
1173
0
#ifndef OPENSSL_NO_ECH
1174
            /* do compressed in pass 0, non-compressed in pass 1 */
1175
0
            if (ossl_ech_2bcompressed((int)i) == pass)
1176
0
                continue;
1177
            /* stash index - needed for COMPRESS ECH handling */
1178
0
            s->ext.ech.ext_ind = (int)i;
1179
0
#endif
1180
            /* Skip if not relevant for our context */
1181
0
            if (!should_add_extension(s, thisexd->context, context, max_version))
1182
0
                continue;
1183
1184
0
            construct = s->server ? thisexd->construct_stoc
1185
0
                                  : thisexd->construct_ctos;
1186
1187
0
            if (construct == NULL)
1188
0
                continue;
1189
1190
0
            ret = construct(s, pkt, context, x, chainidx);
1191
0
            if (ret == EXT_RETURN_FAIL) {
1192
                /* SSLfatal() already called */
1193
0
                return 0;
1194
0
            }
1195
0
            if (ret == EXT_RETURN_SENT
1196
0
                && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
1197
0
                s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
1198
0
        }
1199
1200
0
#ifndef OPENSSL_NO_ECH
1201
    /*
1202
     * don't close yet if client in the middle of doing ECH, we'll
1203
     * eventually close this in ech_aad_and_encrypt() after we add
1204
     * the real ECH extension value
1205
     */
1206
0
    if (s->server
1207
0
        || context != SSL_EXT_CLIENT_HELLO
1208
0
        || s->ext.ech.attempted == 0
1209
0
        || s->ext.ech.ch_depth == 1
1210
0
        || s->ext.ech.grease == OSSL_ECH_IS_GREASE) {
1211
0
        if (!WPACKET_close(pkt)) {
1212
0
            if (!for_comp)
1213
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1214
0
            return 0;
1215
0
        }
1216
0
    }
1217
#else
1218
    if (!WPACKET_close(pkt)) {
1219
        if (!for_comp)
1220
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1221
        return 0;
1222
    }
1223
#endif
1224
0
    return 1;
1225
0
}
1226
1227
/*
1228
 * Built in extension finalisation and initialisation functions. All initialise
1229
 * or finalise the associated extension type for the given |context|. For
1230
 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
1231
 * otherwise. These functions return 1 on success or 0 on failure.
1232
 */
1233
1234
static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent)
1235
0
{
1236
0
    if (!s->server) {
1237
        /*
1238
         * Check if we can connect to a server that doesn't support safe
1239
         * renegotiation
1240
         */
1241
0
        if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
1242
0
            && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
1243
0
            && !sent) {
1244
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1245
0
                SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1246
0
            return 0;
1247
0
        }
1248
1249
0
        return 1;
1250
0
    }
1251
1252
    /* Need RI if renegotiating */
1253
0
    if (s->renegotiate
1254
0
        && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
1255
0
        && !sent) {
1256
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1257
0
            SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1258
0
        return 0;
1259
0
    }
1260
1261
0
    return 1;
1262
0
}
1263
1264
static ossl_inline void ssl_tsan_decr(const SSL_CTX *ctx,
1265
    TSAN_QUALIFIER int *stat)
1266
0
{
1267
0
    if (ssl_tsan_lock(ctx)) {
1268
0
        tsan_decr(stat);
1269
0
        ssl_tsan_unlock(ctx);
1270
0
    }
1271
0
}
1272
1273
static int init_server_name(SSL_CONNECTION *s, unsigned int context)
1274
0
{
1275
0
    if (s->server) {
1276
0
        s->servername_done = 0;
1277
1278
0
        OPENSSL_free(s->ext.hostname);
1279
0
        s->ext.hostname = NULL;
1280
0
    }
1281
1282
0
    return 1;
1283
0
}
1284
1285
#ifndef OPENSSL_NO_ECH
1286
/*
1287
 * Just note that ech is not yet done
1288
 * return 1 for good, 0 otherwise
1289
 */
1290
static int init_ech(SSL_CONNECTION *s, unsigned int context)
1291
0
{
1292
0
    const int nexts = OSSL_NELEM(ext_defs);
1293
1294
    /* we don't need this assert everywhere - anywhere is fine */
1295
0
    if (!ossl_assert(TLSEXT_IDX_num_builtins == nexts)) {
1296
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1297
0
        return 0;
1298
0
    }
1299
0
    if ((context & SSL_EXT_CLIENT_HELLO) != 0)
1300
0
        s->ext.ech.done = 0;
1301
0
    return 1;
1302
0
}
1303
1304
static int final_ech(SSL_CONNECTION *s, unsigned int context, int sent)
1305
0
{
1306
0
    if (s->server && s->ext.ech.success == 1
1307
0
        && s->ext.ech.inner_ech_seen_ok != 1) {
1308
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_ECH_REQUIRED);
1309
0
        return 0;
1310
0
    }
1311
0
    return 1;
1312
0
}
1313
#endif /* OPENSSL_NO_ECH */
1314
1315
static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent)
1316
0
{
1317
0
    int ret = SSL_TLSEXT_ERR_NOACK;
1318
0
    int altmp = SSL_AD_UNRECOGNIZED_NAME;
1319
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1320
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1321
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1322
0
    int was_ticket = (SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0;
1323
1324
0
    if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL)) {
1325
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1326
0
        return 0;
1327
0
    }
1328
1329
0
    if (sctx->ext.servername_cb != NULL)
1330
0
        ret = sctx->ext.servername_cb(ussl, &altmp,
1331
0
            sctx->ext.servername_arg);
1332
0
    else if (s->session_ctx->ext.servername_cb != NULL)
1333
0
        ret = s->session_ctx->ext.servername_cb(ussl, &altmp,
1334
0
            s->session_ctx->ext.servername_arg);
1335
1336
    /*
1337
     * For servers, propagate the SNI hostname from the temporary
1338
     * storage in the SSL to the persistent SSL_SESSION, now that we
1339
     * know we accepted it.
1340
     * Clients make this copy when parsing the server's response to
1341
     * the extension, which is when they find out that the negotiation
1342
     * was successful.
1343
     */
1344
0
    if (s->server) {
1345
0
        if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
1346
            /* Only store the hostname in the session if we accepted it. */
1347
0
            OPENSSL_free(s->session->ext.hostname);
1348
0
            s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
1349
0
            if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) {
1350
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1351
0
            }
1352
0
        }
1353
0
    }
1354
1355
    /*
1356
     * If we switched contexts (whether here or in the client_hello callback),
1357
     * move the sess_accept increment from the session_ctx to the new
1358
     * context, to avoid the confusing situation of having sess_accept_good
1359
     * exceed sess_accept (zero) for the new context.
1360
     */
1361
0
    if (SSL_IS_FIRST_HANDSHAKE(s) && sctx != s->session_ctx
1362
0
        && s->hello_retry_request == SSL_HRR_NONE) {
1363
0
        ssl_tsan_counter(sctx, &sctx->stats.sess_accept);
1364
0
        ssl_tsan_decr(s->session_ctx, &s->session_ctx->stats.sess_accept);
1365
0
    }
1366
1367
    /*
1368
     * If we're expecting to send a ticket, and tickets were previously enabled,
1369
     * and now tickets are disabled, then turn off expected ticket.
1370
     * Also, if this is not a resumption, create a new session ID
1371
     */
1372
0
    if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
1373
0
        && was_ticket && (SSL_get_options(ssl) & SSL_OP_NO_TICKET) != 0) {
1374
0
        s->ext.ticket_expected = 0;
1375
0
        if (!s->hit) {
1376
0
            SSL_SESSION *ss = SSL_get_session(ssl);
1377
1378
0
            if (ss != NULL) {
1379
0
                OPENSSL_free(ss->ext.tick);
1380
0
                ss->ext.tick = NULL;
1381
0
                ss->ext.ticklen = 0;
1382
0
                ss->ext.tick_lifetime_hint = 0;
1383
0
                ss->ext.tick_age_add = 0;
1384
0
                if (!ssl_generate_session_id(s, ss)) {
1385
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1386
0
                    return 0;
1387
0
                }
1388
0
            } else {
1389
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1390
0
                return 0;
1391
0
            }
1392
0
        }
1393
0
    }
1394
1395
0
    switch (ret) {
1396
0
    case SSL_TLSEXT_ERR_ALERT_FATAL:
1397
0
        SSLfatal(s, altmp, SSL_R_CALLBACK_FAILED);
1398
0
        return 0;
1399
1400
0
    case SSL_TLSEXT_ERR_ALERT_WARNING:
1401
        /* TLSv1.3 doesn't have warning alerts so we suppress this */
1402
0
        if (!SSL_CONNECTION_IS_TLS13(s))
1403
0
            ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
1404
0
        s->servername_done = 0;
1405
0
        return 1;
1406
1407
0
    case SSL_TLSEXT_ERR_NOACK:
1408
0
        s->servername_done = 0;
1409
0
        return 1;
1410
1411
0
    default:
1412
0
        return 1;
1413
0
    }
1414
0
}
1415
1416
static int init_session_ticket(SSL_CONNECTION *s, unsigned int context)
1417
0
{
1418
0
    if (!s->server)
1419
0
        s->ext.ticket_expected = 0;
1420
1421
0
    return 1;
1422
0
}
1423
1424
#ifndef OPENSSL_NO_OCSP
1425
static int init_status_request(SSL_CONNECTION *s, unsigned int context)
1426
0
{
1427
0
    if (s->server) {
1428
0
        s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
1429
0
    } else {
1430
        /*
1431
         * Ensure we get sensible values passed to tlsext_status_cb in the event
1432
         * that we don't receive a status message
1433
         */
1434
0
        OPENSSL_free(s->ext.ocsp.resp);
1435
0
        s->ext.ocsp.resp = NULL;
1436
0
        s->ext.ocsp.resp_len = 0;
1437
1438
0
        sk_OCSP_RESPONSE_pop_free(s->ext.ocsp.resp_ex, OCSP_RESPONSE_free);
1439
0
        s->ext.ocsp.resp_ex = NULL;
1440
0
    }
1441
1442
0
    return 1;
1443
0
}
1444
#endif
1445
1446
#ifndef OPENSSL_NO_NEXTPROTONEG
1447
static int init_npn(SSL_CONNECTION *s, unsigned int context)
1448
0
{
1449
0
    s->s3.npn_seen = 0;
1450
1451
0
    return 1;
1452
0
}
1453
#endif
1454
1455
static int init_alpn(SSL_CONNECTION *s, unsigned int context)
1456
0
{
1457
0
    OPENSSL_free(s->s3.alpn_selected);
1458
0
    s->s3.alpn_selected = NULL;
1459
0
    s->s3.alpn_selected_len = 0;
1460
0
    if (s->server) {
1461
0
        OPENSSL_free(s->s3.alpn_proposed);
1462
0
        s->s3.alpn_proposed = NULL;
1463
0
        s->s3.alpn_proposed_len = 0;
1464
0
    }
1465
0
    return 1;
1466
0
}
1467
1468
static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent)
1469
0
{
1470
0
    if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
1471
0
        s->ext.early_data_ok = 0;
1472
1473
0
    if (!s->server || !SSL_CONNECTION_IS_TLS13(s))
1474
0
        return 1;
1475
1476
    /*
1477
     * Call alpn_select callback if needed.  Has to be done after SNI and
1478
     * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
1479
     * we also have to do this before we decide whether to accept early_data.
1480
     * In TLSv1.3 we've already negotiated our cipher so we do this call now.
1481
     * For < TLSv1.3 we defer it until after cipher negotiation.
1482
     *
1483
     * On failure SSLfatal() already called.
1484
     */
1485
0
    return tls_handle_alpn(s);
1486
0
}
1487
1488
static int init_sig_algs(SSL_CONNECTION *s, unsigned int context)
1489
0
{
1490
    /* Clear any signature algorithms extension received */
1491
0
    OPENSSL_free(s->s3.tmp.peer_sigalgs);
1492
0
    s->s3.tmp.peer_sigalgs = NULL;
1493
0
    s->s3.tmp.peer_sigalgslen = 0;
1494
1495
0
    return 1;
1496
0
}
1497
1498
static int init_sig_algs_cert(SSL_CONNECTION *s,
1499
    ossl_unused unsigned int context)
1500
0
{
1501
    /* Clear any signature algorithms extension received */
1502
0
    OPENSSL_free(s->s3.tmp.peer_cert_sigalgs);
1503
0
    s->s3.tmp.peer_cert_sigalgs = NULL;
1504
0
    s->s3.tmp.peer_cert_sigalgslen = 0;
1505
1506
0
    return 1;
1507
0
}
1508
1509
#ifndef OPENSSL_NO_SRP
1510
static int init_srp(SSL_CONNECTION *s, unsigned int context)
1511
0
{
1512
0
    OPENSSL_free(s->srp_ctx.login);
1513
0
    s->srp_ctx.login = NULL;
1514
1515
0
    return 1;
1516
0
}
1517
#endif
1518
1519
static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context)
1520
0
{
1521
0
    OPENSSL_free(s->ext.peer_ecpointformats);
1522
0
    s->ext.peer_ecpointformats = NULL;
1523
0
    s->ext.peer_ecpointformats_len = 0;
1524
1525
0
    return 1;
1526
0
}
1527
1528
static int init_etm(SSL_CONNECTION *s, unsigned int context)
1529
0
{
1530
0
    s->ext.use_etm = 0;
1531
1532
0
    return 1;
1533
0
}
1534
1535
static int init_ems(SSL_CONNECTION *s, unsigned int context)
1536
0
{
1537
0
    if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1538
0
        s->s3.flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
1539
0
        s->s3.flags |= TLS1_FLAGS_REQUIRED_EXTMS;
1540
0
    }
1541
1542
0
    return 1;
1543
0
}
1544
1545
static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent)
1546
0
{
1547
    /*
1548
     * Check extended master secret extension is not dropped on
1549
     * renegotiation.
1550
     */
1551
0
    if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
1552
0
        && (s->s3.flags & TLS1_FLAGS_REQUIRED_EXTMS)) {
1553
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1554
0
        return 0;
1555
0
    }
1556
0
    if (!s->server && s->hit) {
1557
        /*
1558
         * Check extended master secret extension is consistent with
1559
         * original session.
1560
         */
1561
0
        if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) != !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
1562
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1563
0
            return 0;
1564
0
        }
1565
0
    }
1566
1567
0
    return 1;
1568
0
}
1569
1570
static int init_certificate_authorities(SSL_CONNECTION *s, unsigned int context)
1571
0
{
1572
0
    sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
1573
0
    s->s3.tmp.peer_ca_names = NULL;
1574
0
    return 1;
1575
0
}
1576
1577
static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
1578
    WPACKET *pkt,
1579
    unsigned int context,
1580
    X509 *x,
1581
    size_t chainidx)
1582
0
{
1583
0
    const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s);
1584
1585
0
    if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
1586
0
        return EXT_RETURN_NOT_SENT;
1587
1588
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
1589
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1590
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1591
0
        return EXT_RETURN_FAIL;
1592
0
    }
1593
1594
0
    if (!construct_ca_names(s, ca_sk, pkt)) {
1595
        /* SSLfatal() already called */
1596
0
        return EXT_RETURN_FAIL;
1597
0
    }
1598
1599
0
    if (!WPACKET_close(pkt)) {
1600
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1601
0
        return EXT_RETURN_FAIL;
1602
0
    }
1603
1604
0
    return EXT_RETURN_SENT;
1605
0
}
1606
1607
static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
1608
    unsigned int context, X509 *x,
1609
    size_t chainidx)
1610
0
{
1611
0
    if (!parse_ca_names(s, pkt))
1612
0
        return 0;
1613
0
    if (PACKET_remaining(pkt) != 0) {
1614
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1615
0
        return 0;
1616
0
    }
1617
0
    return 1;
1618
0
}
1619
1620
#ifndef OPENSSL_NO_SRTP
1621
static int init_srtp(SSL_CONNECTION *s, unsigned int context)
1622
0
{
1623
0
    if (s->server)
1624
0
        s->srtp_profile = NULL;
1625
1626
0
    return 1;
1627
0
}
1628
#endif
1629
1630
static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent)
1631
0
{
1632
0
    if (!sent && SSL_CONNECTION_IS_TLS13(s) && !s->hit) {
1633
0
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1634
0
            SSL_R_MISSING_SIGALGS_EXTENSION);
1635
0
        return 0;
1636
0
    }
1637
1638
0
    return 1;
1639
0
}
1640
1641
static int final_supported_versions(SSL_CONNECTION *s, unsigned int context,
1642
    int sent)
1643
0
{
1644
0
    if (!sent && context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) {
1645
0
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1646
0
            SSL_R_MISSING_SUPPORTED_VERSIONS_EXTENSION);
1647
0
        return 0;
1648
0
    }
1649
1650
0
    return 1;
1651
0
}
1652
1653
static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent)
1654
0
{
1655
0
#if !defined(OPENSSL_NO_TLS1_3)
1656
0
    if (!SSL_CONNECTION_IS_TLS13(s))
1657
0
        return 1;
1658
1659
    /* Nothing to do for key_share in an HRR */
1660
0
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
1661
0
        return 1;
1662
1663
    /*
1664
     * If
1665
     *     we are a client
1666
     *     AND
1667
     *     we have no key_share
1668
     *     AND
1669
     *     (we are not resuming
1670
     *      OR the kex_mode doesn't allow non key_share resumes)
1671
     * THEN
1672
     *     fail;
1673
     */
1674
0
    if (!s->server
1675
0
        && !sent) {
1676
0
        if ((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1677
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_SUITABLE_KEY_SHARE);
1678
0
            return 0;
1679
0
        }
1680
0
        if (!s->hit) {
1681
0
            SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_NO_SUITABLE_KEY_SHARE);
1682
0
            return 0;
1683
0
        }
1684
0
    }
1685
    /*
1686
     * IF
1687
     *     we are a server
1688
     * THEN
1689
     *     IF
1690
     *         we have a suitable key_share
1691
     *     THEN
1692
     *         IF
1693
     *             we are stateless AND we have no cookie
1694
     *         THEN
1695
     *             send a HelloRetryRequest
1696
     *     ELSE
1697
     *         IF
1698
     *             we didn't already send a HelloRetryRequest
1699
     *             AND
1700
     *             the client sent a key_share extension
1701
     *             AND
1702
     *             (we are not resuming
1703
     *              OR the kex_mode allows key_share resumes)
1704
     *             AND
1705
     *             a shared group exists
1706
     *         THEN
1707
     *             send a HelloRetryRequest
1708
     *         ELSE IF
1709
     *             we are not resuming
1710
     *             OR
1711
     *             the kex_mode doesn't allow non key_share resumes
1712
     *         THEN
1713
     *             fail
1714
     *         ELSE IF
1715
     *             we are stateless AND we have no cookie
1716
     *         THEN
1717
     *             send a HelloRetryRequest
1718
     */
1719
0
    if (s->server) {
1720
0
        if (s->s3.peer_tmp != NULL) {
1721
            /* We have a suitable key_share */
1722
0
            if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1723
0
                && !s->ext.cookieok) {
1724
0
                if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1725
                    /*
1726
                     * If we are stateless then we wouldn't know about any
1727
                     * previously sent HRR - so how can this be anything other
1728
                     * than 0?
1729
                     */
1730
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1731
0
                    return 0;
1732
0
                }
1733
0
                s->hello_retry_request = SSL_HRR_PENDING;
1734
0
                return 1;
1735
0
            }
1736
0
        } else {
1737
            /* No suitable key_share */
1738
0
            if (s->hello_retry_request == SSL_HRR_NONE && sent
1739
0
                && (!s->hit
1740
0
                    || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) != 0)) {
1741
1742
                /* Did we detect group overlap in tls_parse_ctos_key_share ? */
1743
0
                if (s->s3.group_id_candidate != 0) {
1744
                    /* A shared group exists so send a HelloRetryRequest */
1745
0
                    s->s3.group_id = s->s3.group_id_candidate;
1746
0
                    s->hello_retry_request = SSL_HRR_PENDING;
1747
0
                    return 1;
1748
0
                }
1749
0
            }
1750
0
            if (!s->hit
1751
0
                || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1752
                /* Nothing left we can do - just fail */
1753
0
                SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE : SSL_AD_MISSING_EXTENSION,
1754
0
                    SSL_R_NO_SUITABLE_KEY_SHARE);
1755
0
                return 0;
1756
0
            }
1757
1758
0
            if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1759
0
                && !s->ext.cookieok) {
1760
0
                if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1761
                    /*
1762
                     * If we are stateless then we wouldn't know about any
1763
                     * previously sent HRR - so how can this be anything other
1764
                     * than 0?
1765
                     */
1766
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1767
0
                    return 0;
1768
0
                }
1769
0
                s->hello_retry_request = SSL_HRR_PENDING;
1770
0
                return 1;
1771
0
            }
1772
0
        }
1773
1774
        /*
1775
         * We have a key_share so don't send any more HelloRetryRequest
1776
         * messages
1777
         */
1778
0
        if (s->hello_retry_request == SSL_HRR_PENDING)
1779
0
            s->hello_retry_request = SSL_HRR_COMPLETE;
1780
0
    } else {
1781
        /*
1782
         * For a client side resumption with no key_share we need to generate
1783
         * the handshake secret (otherwise this is done during key_share
1784
         * processing).
1785
         */
1786
0
        if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) {
1787
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1788
0
            return 0;
1789
0
        }
1790
0
    }
1791
0
#endif /* !defined(OPENSSL_NO_TLS1_3) */
1792
0
    return 1;
1793
0
}
1794
1795
static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context)
1796
0
{
1797
0
    s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
1798
0
    return 1;
1799
0
}
1800
1801
int tls_psk_do_binder(SSL_CONNECTION *s, const EVP_MD *md,
1802
    const unsigned char *msgstart,
1803
    size_t binderoffset, const unsigned char *binderin,
1804
    unsigned char *binderout, SSL_SESSION *sess, int sign,
1805
    int external)
1806
0
{
1807
0
    EVP_PKEY *mackey = NULL;
1808
0
    EVP_MD_CTX *mctx = NULL;
1809
0
    unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
1810
0
    unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
1811
0
    unsigned char *early_secret;
1812
    /* ASCII: "res binder", in hex for EBCDIC compatibility */
1813
0
    static const unsigned char resumption_label[] = "\x72\x65\x73\x20\x62\x69\x6E\x64\x65\x72";
1814
    /* ASCII: "ext binder", in hex for EBCDIC compatibility */
1815
0
    static const unsigned char external_label[] = "\x65\x78\x74\x20\x62\x69\x6E\x64\x65\x72";
1816
0
    const unsigned char *label;
1817
0
    size_t bindersize, labelsize, hashsize;
1818
0
    int hashsizei = EVP_MD_get_size(md);
1819
0
    int ret = -1;
1820
0
    int usepskfored = 0;
1821
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1822
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1823
1824
    /* Ensure cast to size_t is safe */
1825
0
    if (!ossl_assert(hashsizei > 0)) {
1826
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1827
0
        goto err;
1828
0
    }
1829
0
    hashsize = (size_t)hashsizei;
1830
1831
0
    if (external
1832
0
        && s->early_data_state == SSL_EARLY_DATA_CONNECTING
1833
0
        && s->session->ext.max_early_data == 0
1834
0
        && sess->ext.max_early_data > 0)
1835
0
        usepskfored = 1;
1836
1837
0
    if (external) {
1838
0
        label = external_label;
1839
0
        labelsize = sizeof(external_label) - 1;
1840
0
    } else {
1841
0
        label = resumption_label;
1842
0
        labelsize = sizeof(resumption_label) - 1;
1843
0
    }
1844
1845
    /*
1846
     * Generate the early_secret. On the server side we've selected a PSK to
1847
     * resume with (internal or external) so we always do this. On the client
1848
     * side we do this for a non-external (i.e. resumption) PSK or external PSK
1849
     * that will be used for early_data so that it is in place for sending early
1850
     * data. For client side external PSK not being used for early_data we
1851
     * generate it but store it away for later use.
1852
     */
1853
0
    if (s->server || !external || usepskfored)
1854
0
        early_secret = (unsigned char *)s->early_secret;
1855
0
    else
1856
0
        early_secret = (unsigned char *)sess->early_secret;
1857
1858
0
    if (!tls13_generate_secret(s, md, NULL, sess->master_key,
1859
0
            sess->master_key_length, early_secret)) {
1860
        /* SSLfatal() already called */
1861
0
        goto err;
1862
0
    }
1863
1864
    /*
1865
     * Create the handshake hash for the binder key...the messages so far are
1866
     * empty!
1867
     */
1868
0
    mctx = EVP_MD_CTX_new();
1869
0
    if (mctx == NULL
1870
0
        || EVP_DigestInit_ex(mctx, md, NULL) <= 0
1871
0
        || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1872
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1873
0
        goto err;
1874
0
    }
1875
1876
    /* Generate the binder key */
1877
0
    if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash,
1878
0
            hashsize, binderkey, hashsize, 1)) {
1879
        /* SSLfatal() already called */
1880
0
        goto err;
1881
0
    }
1882
1883
    /* Generate the finished key */
1884
0
    if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
1885
        /* SSLfatal() already called */
1886
0
        goto err;
1887
0
    }
1888
1889
0
    if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
1890
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1891
0
        goto err;
1892
0
    }
1893
1894
    /*
1895
     * Get a hash of the ClientHello up to the start of the binders. If we are
1896
     * following a HelloRetryRequest then this includes the hash of the first
1897
     * ClientHello and the HelloRetryRequest itself.
1898
     */
1899
0
    if (s->hello_retry_request == SSL_HRR_PENDING) {
1900
0
        size_t hdatalen;
1901
0
        long hdatalen_l;
1902
0
        void *hdata;
1903
1904
0
#ifndef OPENSSL_NO_ECH
1905
        /* handle the hashing as per ECH needs (on client) */
1906
0
        if (s->ext.ech.attempted == 1 && s->ext.ech.ch_depth == 1) {
1907
0
            if (ossl_ech_intbuf_fetch(s, (unsigned char **)&hdata, &hdatalen) != 1) {
1908
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1909
0
                goto err;
1910
0
            }
1911
0
        } else {
1912
0
#endif
1913
0
            hdatalen = hdatalen_l = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
1914
0
            if (hdatalen_l <= 0) {
1915
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
1916
0
                goto err;
1917
0
            }
1918
0
#ifndef OPENSSL_NO_ECH
1919
0
        }
1920
0
#endif
1921
1922
        /*
1923
         * For servers the handshake buffer data will include the second
1924
         * ClientHello - which we don't want - so we need to take that bit off.
1925
         */
1926
0
        if (s->server) {
1927
0
            PACKET hashprefix, msg;
1928
1929
            /* Find how many bytes are left after the first two messages */
1930
0
            if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
1931
0
                || !PACKET_forward(&hashprefix, 1)
1932
0
                || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
1933
0
                || !PACKET_forward(&hashprefix, 1)
1934
0
                || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
1935
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1936
0
                goto err;
1937
0
            }
1938
0
            hdatalen -= PACKET_remaining(&hashprefix);
1939
0
        }
1940
1941
0
        if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
1942
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1943
0
            goto err;
1944
0
        }
1945
0
    }
1946
1947
0
    if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
1948
0
        || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1949
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1950
0
        goto err;
1951
0
    }
1952
1953
0
    mackey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
1954
0
        sctx->propq, finishedkey,
1955
0
        hashsize);
1956
0
    if (mackey == NULL) {
1957
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1958
0
        goto err;
1959
0
    }
1960
1961
0
    if (!sign)
1962
0
        binderout = tmpbinder;
1963
1964
0
    if (sctx->propq != NULL)
1965
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
1966
0
            (char *)sctx->propq, 0);
1967
0
    bindersize = hashsize;
1968
0
    if (EVP_DigestSignInit_ex(mctx, NULL, EVP_MD_get0_name(md), sctx->libctx,
1969
0
            sctx->propq, mackey, params)
1970
0
            <= 0
1971
0
        || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
1972
0
        || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
1973
0
        || bindersize != hashsize) {
1974
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1975
0
        goto err;
1976
0
    }
1977
1978
0
    if (sign) {
1979
0
        ret = 1;
1980
0
    } else {
1981
        /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1982
0
        ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
1983
0
        if (!ret)
1984
0
            SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BINDER_DOES_NOT_VERIFY);
1985
0
    }
1986
1987
0
err:
1988
0
    OPENSSL_cleanse(binderkey, sizeof(binderkey));
1989
0
    OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
1990
0
    EVP_PKEY_free(mackey);
1991
0
    EVP_MD_CTX_free(mctx);
1992
0
    return ret;
1993
0
}
1994
1995
static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent)
1996
0
{
1997
0
    if (!sent)
1998
0
        return 1;
1999
2000
0
    if (!s->server) {
2001
0
        if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
2002
0
            && sent
2003
0
            && !s->ext.early_data_ok) {
2004
            /*
2005
             * If we get here then the server accepted our early_data but we
2006
             * later realised that it shouldn't have done (e.g. inconsistent
2007
             * ALPN)
2008
             */
2009
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EARLY_DATA);
2010
0
            return 0;
2011
0
        }
2012
2013
0
        return 1;
2014
0
    }
2015
2016
0
    if (s->max_early_data == 0
2017
0
        || !s->hit
2018
0
        || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
2019
0
        || !s->ext.early_data_ok
2020
0
        || s->hello_retry_request != SSL_HRR_NONE
2021
0
        || (s->allow_early_data_cb != NULL
2022
0
            && !s->allow_early_data_cb(SSL_CONNECTION_GET_USER_SSL(s),
2023
0
                s->allow_early_data_cb_data))) {
2024
0
        s->ext.early_data = SSL_EARLY_DATA_REJECTED;
2025
0
    } else {
2026
0
        s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
2027
2028
0
        if (!tls13_change_cipher_state(s,
2029
0
                SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
2030
            /* SSLfatal() already called */
2031
0
            return 0;
2032
0
        }
2033
0
    }
2034
2035
0
    return 1;
2036
0
}
2037
2038
static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
2039
    int sent)
2040
0
{
2041
0
    if (s->session == NULL)
2042
0
        return 1;
2043
2044
    /* MaxFragmentLength defaults to disabled */
2045
0
    if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)
2046
0
        s->session->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_DISABLED;
2047
2048
0
    if (USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) {
2049
0
        s->rlayer.rrlmethod->set_max_frag_len(s->rlayer.rrl,
2050
0
            GET_MAX_FRAGMENT_LENGTH(s->session));
2051
0
        s->rlayer.wrlmethod->set_max_frag_len(s->rlayer.wrl,
2052
0
            ssl_get_max_send_fragment(s));
2053
0
    }
2054
2055
0
    return 1;
2056
0
}
2057
2058
static int init_post_handshake_auth(SSL_CONNECTION *s,
2059
    ossl_unused unsigned int context)
2060
0
{
2061
0
    s->post_handshake_auth = SSL_PHA_NONE;
2062
2063
0
    return 1;
2064
0
}
2065
2066
/*
2067
 * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
2068
 * extension, servers MUST abort the handshake.
2069
 */
2070
static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent)
2071
0
{
2072
0
    if (s->server && sent && s->clienthello != NULL
2073
0
        && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) {
2074
0
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
2075
0
            SSL_R_MISSING_PSK_KEX_MODES_EXTENSION);
2076
0
        return 0;
2077
0
    }
2078
2079
0
    return 1;
2080
0
}
2081
2082
static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context)
2083
0
{
2084
0
    memset(sc->ext.compress_certificate_from_peer, 0,
2085
0
        sizeof(sc->ext.compress_certificate_from_peer));
2086
0
    return 1;
2087
0
}
2088
2089
/* The order these are put into the packet imply a preference order: [brotli, zlib, zstd] */
2090
static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
2091
    unsigned int context,
2092
    X509 *x, size_t chainidx)
2093
0
{
2094
#ifndef OPENSSL_NO_COMP_ALG
2095
    int i;
2096
2097
    if (!ossl_comp_has_alg(0))
2098
        return EXT_RETURN_NOT_SENT;
2099
2100
    /* Server: Don't attempt to compress a non-X509 (i.e. an RPK) */
2101
    if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509) {
2102
        sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
2103
        return EXT_RETURN_NOT_SENT;
2104
    }
2105
2106
    /* Client: If we sent a client cert-type extension, don't indicate compression */
2107
    if (!sc->server && sc->ext.client_cert_type_ctos) {
2108
        sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
2109
        return EXT_RETURN_NOT_SENT;
2110
    }
2111
2112
    /* Do not indicate we support receiving compressed certificates */
2113
    if ((sc->options & SSL_OP_NO_RX_CERTIFICATE_COMPRESSION) != 0)
2114
        return EXT_RETURN_NOT_SENT;
2115
2116
    if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
2117
        return EXT_RETURN_NOT_SENT;
2118
#ifndef OPENSSL_NO_ECH
2119
    ECH_SAME_EXT(sc, context, pkt);
2120
#endif
2121
2122
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_compress_certificate)
2123
        || !WPACKET_start_sub_packet_u16(pkt)
2124
        || !WPACKET_start_sub_packet_u8(pkt))
2125
        goto err;
2126
2127
    for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) {
2128
        if (!WPACKET_put_bytes_u16(pkt, sc->cert_comp_prefs[i]))
2129
            goto err;
2130
    }
2131
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt))
2132
        goto err;
2133
2134
    sc->ext.compress_certificate_sent = 1;
2135
    return EXT_RETURN_SENT;
2136
err:
2137
    SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2138
    return EXT_RETURN_FAIL;
2139
#else
2140
0
    return EXT_RETURN_NOT_SENT;
2141
0
#endif
2142
0
}
2143
2144
#ifndef OPENSSL_NO_COMP_ALG
2145
static int tls_comp_in_pref(SSL_CONNECTION *sc, int alg)
2146
{
2147
    int i;
2148
2149
    /* ossl_comp_has_alg() considers 0 as "any" */
2150
    if (alg == 0)
2151
        return 0;
2152
    /* Make sure algorithm is enabled */
2153
    if (!ossl_comp_has_alg(alg))
2154
        return 0;
2155
    /* If no preferences are set, it's ok */
2156
    if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
2157
        return 1;
2158
    /* Find the algorithm */
2159
    for (i = 0; i < TLSEXT_comp_cert_limit; i++)
2160
        if (sc->cert_comp_prefs[i] == alg)
2161
            return 1;
2162
    return 0;
2163
}
2164
#endif
2165
2166
int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt, unsigned int context,
2167
    X509 *x, size_t chainidx)
2168
0
{
2169
#ifndef OPENSSL_NO_COMP_ALG
2170
    PACKET supported_comp_algs;
2171
    unsigned int comp;
2172
    int already_set[TLSEXT_comp_cert_limit];
2173
    int j = 0;
2174
2175
    /* If no algorithms are available, ignore the extension */
2176
    if (!ossl_comp_has_alg(0))
2177
        return 1;
2178
2179
    /* Don't attempt to compress a non-X509 (i.e. an RPK) */
2180
    if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509)
2181
        return 1;
2182
    if (!sc->server && sc->ext.client_cert_type != TLSEXT_cert_type_x509)
2183
        return 1;
2184
2185
    /* Ignore the extension and don't send compressed certificates */
2186
    if ((sc->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
2187
        return 1;
2188
2189
    if (!PACKET_as_length_prefixed_1(pkt, &supported_comp_algs)
2190
        || PACKET_remaining(&supported_comp_algs) == 0) {
2191
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2192
        return 0;
2193
    }
2194
2195
    memset(already_set, 0, sizeof(already_set));
2196
    /*
2197
     * The preference array has real values, so take a look at each
2198
     * value coming in, and make sure it's in our preference list
2199
     * The array is 0 (i.e. "none") terminated
2200
     * The preference list only contains supported algorithms
2201
     */
2202
    while (PACKET_get_net_2(&supported_comp_algs, &comp)) {
2203
        if (tls_comp_in_pref(sc, comp) && !already_set[comp]) {
2204
            sc->ext.compress_certificate_from_peer[j++] = comp;
2205
            already_set[comp] = 1;
2206
        }
2207
    }
2208
    if (PACKET_remaining(&supported_comp_algs) != 0) {
2209
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2210
        return 0;
2211
    }
2212
#endif
2213
0
    return 1;
2214
0
}
2215
2216
static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context)
2217
0
{
2218
    /* Only reset when parsing client hello */
2219
0
    if (sc->server) {
2220
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2221
0
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2222
0
    }
2223
0
    return 1;
2224
0
}
2225
2226
static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context)
2227
0
{
2228
    /* Only reset when parsing client hello */
2229
0
    if (sc->server) {
2230
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2231
0
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2232
0
    }
2233
0
    return 1;
2234
0
}