Coverage Report

Created: 2026-03-09 06:55

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