Coverage Report

Created: 2026-04-08 06:20

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