Coverage Report

Created: 2026-08-17 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/statem/extensions_srvr.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
#include <openssl/ocsp.h>
11
#include "../ssl_local.h"
12
#include "statem_local.h"
13
#include "internal/cryptlib.h"
14
#include "internal/ssl_unwrap.h"
15
#ifndef OPENSSL_NO_ECH
16
#include <openssl/rand.h>
17
#include <openssl/trace.h>
18
#endif
19
20
0
#define COOKIE_STATE_FORMAT_VERSION 1
21
22
0
#define MAX_SUPPORTED_GROUPS 128
23
0
#define MAX_KEY_SHARES 16
24
0
#define MAX_PRE_SHARED_KEYS 16
25
26
/*
27
 * 2 bytes for packet length, 2 bytes for format version, 2 bytes for
28
 * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for
29
 * key_share present flag, 8 bytes for timestamp, 2 bytes for the hashlen,
30
 * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie
31
 * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.
32
 */
33
0
#define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 8 + 2 + EVP_MAX_MD_SIZE + 1 \
34
0
    + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)
35
36
/*
37
 * Message header + 2 bytes for protocol version + number of random bytes +
38
 * + 1 byte for legacy session id length + number of bytes in legacy session id
39
 * + 2 bytes for ciphersuite + 1 byte for legacy compression
40
 * + 2 bytes for extension block length + 6 bytes for key_share extension
41
 * + 4 bytes for cookie extension header + the number of bytes in the cookie
42
 */
43
#define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \
44
    + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4                \
45
    + MAX_COOKIE_SIZE)
46
47
/*
48
 * Parse the client's renegotiation binding and abort if it's not right
49
 */
50
int tls_parse_ctos_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
51
    unsigned int context,
52
    X509 *x, size_t chainidx)
53
0
{
54
0
    unsigned int ilen;
55
0
    const unsigned char *data;
56
0
    int ok;
57
58
    /* Parse the length byte */
59
0
    if (!PACKET_get_1(pkt, &ilen)
60
0
        || !PACKET_get_bytes(pkt, &data, ilen)) {
61
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
62
0
        return 0;
63
0
    }
64
65
    /* Check that the extension matches */
66
0
    if (ilen != s->s3.previous_client_finished_len) {
67
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
68
0
        return 0;
69
0
    }
70
71
0
    ok = memcmp(data, s->s3.previous_client_finished,
72
0
        s->s3.previous_client_finished_len);
73
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
74
0
    if (ok) {
75
0
        if ((data[0] ^ s->s3.previous_client_finished[0]) != 0xFF) {
76
0
            ok = 0;
77
0
        }
78
0
    }
79
0
#endif
80
0
    if (ok) {
81
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
82
0
        return 0;
83
0
    }
84
85
0
    s->s3.send_connection_binding = 1;
86
87
0
    return 1;
88
0
}
89
90
/*-
91
 * The servername extension is treated as follows:
92
 *
93
 * - Only the hostname type is supported with a maximum length of 255.
94
 * - The servername is rejected if too long or if it contains zeros,
95
 *   in which case an fatal alert is generated.
96
 * - The servername field is maintained together with the session cache.
97
 * - When a session is resumed, the servername call back invoked in order
98
 *   to allow the application to position itself to the right context.
99
 * - The servername is acknowledged if it is new for a session or when
100
 *   it is identical to a previously used for the same session.
101
 *   Applications can control the behaviour.  They can at any time
102
 *   set a 'desirable' servername for a new SSL object. This can be the
103
 *   case for example with HTTPS when a Host: header field is received and
104
 *   a renegotiation is requested. In this case, a possible servername
105
 *   presented in the new client hello is only acknowledged if it matches
106
 *   the value of the Host: field.
107
 * - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
108
 *   if they provide for changing an explicit servername context for the
109
 *   session, i.e. when the session has been established with a servername
110
 *   extension.
111
 * - On session reconnect, the servername extension may be absent.
112
 */
113
int tls_parse_ctos_server_name(SSL_CONNECTION *s, PACKET *pkt,
114
    unsigned int context, X509 *x, size_t chainidx)
115
0
{
116
0
    unsigned int servname_type;
117
0
    PACKET sni, hostname;
118
119
0
    if (!PACKET_as_length_prefixed_2(pkt, &sni)
120
        /* ServerNameList must be at least 1 byte long. */
121
0
        || PACKET_remaining(&sni) == 0) {
122
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
123
0
        return 0;
124
0
    }
125
126
    /*
127
     * Although the intent was for server_name to be extensible, RFC 4366
128
     * was not clear about it; and so OpenSSL among other implementations,
129
     * always and only allows a 'host_name' name types.
130
     * RFC 6066 corrected the mistake but adding new name types
131
     * is nevertheless no longer feasible, so act as if no other
132
     * SNI types can exist, to simplify parsing.
133
     *
134
     * Also note that the RFC permits only one SNI value per type,
135
     * i.e., we can only have a single hostname.
136
     */
137
0
    if (!PACKET_get_1(&sni, &servname_type)
138
0
        || servname_type != TLSEXT_NAMETYPE_host_name
139
0
        || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
140
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
141
0
        return 0;
142
0
    }
143
144
    /*
145
     * In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3
146
     * we always use the SNI value from the handshake.
147
     */
148
0
    if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
149
0
        if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
150
0
            SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
151
0
            return 0;
152
0
        }
153
154
0
        if (PACKET_contains_zero_byte(&hostname)) {
155
0
            SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
156
0
            return 0;
157
0
        }
158
159
        /*
160
         * Store the requested SNI in the SSL as temporary storage.
161
         * If we accept it, it will get stored in the SSL_SESSION as well.
162
         */
163
0
        OPENSSL_free(s->ext.hostname);
164
0
        s->ext.hostname = NULL;
165
0
        if (!PACKET_strndup(&hostname, &s->ext.hostname)) {
166
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
167
0
            return 0;
168
0
        }
169
170
0
        s->servername_done = 1;
171
0
    } else {
172
        /*
173
         * In TLSv1.2 and below we should check if the SNI is consistent between
174
         * the initial handshake and the resumption. In TLSv1.3 SNI is not
175
         * associated with the session.
176
         */
177
0
        s->servername_done = (s->session->ext.hostname != NULL)
178
0
            && PACKET_equal(&hostname, s->session->ext.hostname,
179
0
                strlen(s->session->ext.hostname));
180
0
    }
181
182
0
    return 1;
183
0
}
184
185
int tls_parse_ctos_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
186
    unsigned int context,
187
    X509 *x, size_t chainidx)
188
0
{
189
0
    unsigned int value;
190
191
0
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
192
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
193
0
        return 0;
194
0
    }
195
196
    /* Received |value| should be a valid max-fragment-length code. */
197
0
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
198
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
199
0
            SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH);
200
0
        return 0;
201
0
    }
202
203
    /*
204
     * When doing a full handshake or a renegotiation max_fragment_len_mode will
205
     * be TLSEXT_max_fragment_length_UNSPECIFIED
206
     *
207
     * In case of a resumption max_fragment_len_mode will be one of
208
     *      TLSEXT_max_fragment_length_DISABLED, TLSEXT_max_fragment_length_512,
209
     *      TLSEXT_max_fragment_length_1024, TLSEXT_max_fragment_length_2048.
210
     *      TLSEXT_max_fragment_length_4096
211
     *
212
     * RFC 6066: The negotiated length applies for the duration of the session
213
     * including session resumptions.
214
     *
215
     * So we only set the value in case it is unspecified.
216
     */
217
0
    if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)
218
        /*
219
         * Store it in session, so it'll become binding for us
220
         * and we'll include it in a next Server Hello.
221
         */
222
0
        s->session->ext.max_fragment_len_mode = value;
223
224
0
    return 1;
225
0
}
226
227
#ifndef OPENSSL_NO_SRP
228
int tls_parse_ctos_srp(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
229
    X509 *x, size_t chainidx)
230
0
{
231
0
    PACKET srp_I;
232
233
0
    if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
234
0
        || PACKET_contains_zero_byte(&srp_I)) {
235
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
236
0
        return 0;
237
0
    }
238
239
0
    if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
240
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
241
0
        return 0;
242
0
    }
243
244
0
    return 1;
245
0
}
246
#endif
247
248
int tls_parse_ctos_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
249
    unsigned int context,
250
    X509 *x, size_t chainidx)
251
0
{
252
0
    if (s->ext.session_ticket_cb && !s->ext.session_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), PACKET_data(pkt), (int)PACKET_remaining(pkt), s->ext.session_ticket_cb_arg)) {
253
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
254
0
        return 0;
255
0
    }
256
257
0
    return 1;
258
0
}
259
260
int tls_parse_ctos_sig_algs_cert(SSL_CONNECTION *s, PACKET *pkt,
261
    ossl_unused unsigned int context,
262
    ossl_unused X509 *x,
263
    ossl_unused size_t chainidx)
264
0
{
265
0
    PACKET supported_sig_algs;
266
267
0
    if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
268
0
        || PACKET_remaining(&supported_sig_algs) == 0) {
269
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
270
0
        return 0;
271
0
    }
272
273
    /*
274
     * We use this routine on both clients and servers, and when clients
275
     * get asked for PHA we need to always save the sigalgs regardless
276
     * of whether it was a resumption or not.
277
     */
278
0
    if ((!s->server || (s->server && !s->hit))
279
0
        && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {
280
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
281
0
        return 0;
282
0
    }
283
284
0
    return 1;
285
0
}
286
287
int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt,
288
    unsigned int context, X509 *x, size_t chainidx)
289
0
{
290
0
    PACKET supported_sig_algs;
291
292
0
    if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
293
0
        || PACKET_remaining(&supported_sig_algs) == 0) {
294
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
295
0
        return 0;
296
0
    }
297
298
    /*
299
     * We use this routine on both clients and servers, and when clients
300
     * get asked for PHA we need to always save the sigalgs regardless
301
     * of whether it was a resumption or not.
302
     */
303
0
    if ((!s->server || (s->server && !s->hit))
304
0
        && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {
305
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
306
0
        return 0;
307
0
    }
308
309
0
    return 1;
310
0
}
311
312
#ifndef OPENSSL_NO_OCSP
313
int tls_parse_ctos_status_request(SSL_CONNECTION *s, PACKET *pkt,
314
    unsigned int context,
315
    X509 *x, size_t chainidx)
316
0
{
317
0
    PACKET responder_id_list, exts;
318
319
    /* We ignore this in a resumption handshake */
320
0
    if (s->hit)
321
0
        return 1;
322
323
    /* Not defined if we get one of these in a client Certificate */
324
0
    if (x != NULL)
325
0
        return 1;
326
327
    /*
328
     * We only care about this extension if the application
329
     * registered a callback. Otherwise, there is nothing to
330
     * tell us that a response is needed.
331
     */
332
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
333
0
    if (sctx == NULL || sctx->ext.status_cb == NULL)
334
0
        return 1;
335
336
0
    if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
337
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
338
0
        return 0;
339
0
    }
340
341
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
342
        /*
343
         * We don't know what to do with any other type so ignore it.
344
         */
345
0
        s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
346
0
        return 1;
347
0
    }
348
349
0
    if (!PACKET_get_length_prefixed_2(pkt, &responder_id_list)) {
350
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
351
0
        return 0;
352
0
    }
353
354
    /*
355
     * We remove any OCSP_RESPIDs from a previous handshake
356
     * to prevent unbounded memory growth - CVE-2016-6304
357
     */
358
0
    sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
359
0
    if (PACKET_remaining(&responder_id_list) > 0) {
360
0
        s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
361
0
        if (s->ext.ocsp.ids == NULL) {
362
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
363
0
            return 0;
364
0
        }
365
0
    } else {
366
0
        s->ext.ocsp.ids = NULL;
367
0
    }
368
369
0
    while (PACKET_remaining(&responder_id_list) > 0) {
370
0
        OCSP_RESPID *id;
371
0
        PACKET responder_id;
372
0
        const unsigned char *id_data;
373
374
0
        if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
375
0
            || PACKET_remaining(&responder_id) == 0) {
376
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
377
0
            return 0;
378
0
        }
379
380
0
        id_data = PACKET_data(&responder_id);
381
0
        id = d2i_OCSP_RESPID(NULL, &id_data,
382
0
            (int)PACKET_remaining(&responder_id));
383
0
        if (id == NULL) {
384
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
385
0
            return 0;
386
0
        }
387
388
0
        if (id_data != PACKET_end(&responder_id)) {
389
0
            OCSP_RESPID_free(id);
390
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
391
392
0
            return 0;
393
0
        }
394
395
0
        if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
396
0
            OCSP_RESPID_free(id);
397
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
398
399
0
            return 0;
400
0
        }
401
0
    }
402
403
    /* Read in request_extensions */
404
0
    if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
405
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
406
0
        return 0;
407
0
    }
408
409
0
    if (PACKET_remaining(&exts) > 0) {
410
0
        const unsigned char *ext_data = PACKET_data(&exts);
411
412
0
        sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
413
0
            X509_EXTENSION_free);
414
0
        s->ext.ocsp.exts = d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
415
0
        if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
416
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
417
0
            return 0;
418
0
        }
419
0
    }
420
421
0
    return 1;
422
0
}
423
#endif
424
425
#ifndef OPENSSL_NO_NEXTPROTONEG
426
int tls_parse_ctos_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
427
    X509 *x, size_t chainidx)
428
0
{
429
    /*
430
     * We shouldn't accept this extension on a
431
     * renegotiation.
432
     */
433
0
    if (SSL_IS_FIRST_HANDSHAKE(s))
434
0
        s->s3.npn_seen = 1;
435
436
0
    return 1;
437
0
}
438
#endif
439
440
/*
441
 * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
442
 * extension, not including type and length. Returns: 1 on success, 0 on error.
443
 */
444
int tls_parse_ctos_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
445
    X509 *x, size_t chainidx)
446
0
{
447
0
    PACKET protocol_list, save_protocol_list, protocol;
448
449
0
    if (!SSL_IS_FIRST_HANDSHAKE(s))
450
0
        return 1;
451
452
0
    if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
453
0
        || PACKET_remaining(&protocol_list) < 2) {
454
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
455
0
        return 0;
456
0
    }
457
458
0
    save_protocol_list = protocol_list;
459
0
    do {
460
        /* Protocol names can't be empty. */
461
0
        if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
462
0
            || PACKET_remaining(&protocol) == 0) {
463
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
464
0
            return 0;
465
0
        }
466
0
    } while (PACKET_remaining(&protocol_list) != 0);
467
468
0
    OPENSSL_free(s->s3.alpn_proposed);
469
0
    s->s3.alpn_proposed = NULL;
470
0
    s->s3.alpn_proposed_len = 0;
471
0
    if (!PACKET_memdup(&save_protocol_list,
472
0
            &s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) {
473
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
474
0
        return 0;
475
0
    }
476
477
0
    return 1;
478
0
}
479
480
#ifndef OPENSSL_NO_SRTP
481
int tls_parse_ctos_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
482
    unsigned int context, X509 *x, size_t chainidx)
483
0
{
484
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
485
0
    unsigned int ct, mki_len, id;
486
0
    int i, srtp_pref;
487
0
    PACKET subpkt;
488
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
489
490
    /* Ignore this if we have no SRTP profiles */
491
0
    if (SSL_get_srtp_profiles(ssl) == NULL)
492
0
        return 1;
493
494
    /* Pull off the length of the cipher suite list  and check it is even */
495
0
    if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
496
0
        || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
497
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
498
0
            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
499
0
        return 0;
500
0
    }
501
502
0
    srvr = SSL_get_srtp_profiles(ssl);
503
0
    s->srtp_profile = NULL;
504
    /* Search all profiles for a match initially */
505
0
    srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
506
507
0
    while (PACKET_remaining(&subpkt)) {
508
0
        if (!PACKET_get_net_2(&subpkt, &id)) {
509
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
510
0
                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
511
0
            return 0;
512
0
        }
513
514
        /*
515
         * Only look for match in profiles of higher preference than
516
         * current match.
517
         * If no profiles have been have been configured then this
518
         * does nothing.
519
         */
520
0
        for (i = 0; i < srtp_pref; i++) {
521
0
            SRTP_PROTECTION_PROFILE *sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
522
523
0
            if (sprof->id == id) {
524
0
                s->srtp_profile = sprof;
525
0
                srtp_pref = i;
526
0
                break;
527
0
            }
528
0
        }
529
0
    }
530
531
    /* Now extract the MKI value as a sanity check, but discard it for now */
532
0
    if (!PACKET_get_1(pkt, &mki_len)) {
533
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
534
0
            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
535
0
        return 0;
536
0
    }
537
538
0
    if (!PACKET_forward(pkt, mki_len)
539
0
        || PACKET_remaining(pkt)) {
540
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE);
541
0
        return 0;
542
0
    }
543
544
0
    return 1;
545
0
}
546
#endif
547
548
int tls_parse_ctos_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
549
    X509 *x, size_t chainidx)
550
0
{
551
0
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
552
0
        s->ext.use_etm = 1;
553
554
0
    return 1;
555
0
}
556
557
/*
558
 * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains
559
 * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
560
 */
561
int tls_parse_ctos_psk_kex_modes(SSL_CONNECTION *s, PACKET *pkt,
562
    unsigned int context,
563
    X509 *x, size_t chainidx)
564
0
{
565
0
#ifndef OPENSSL_NO_TLS1_3
566
0
    PACKET psk_kex_modes;
567
0
    unsigned int mode;
568
569
0
    if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
570
0
        || PACKET_remaining(&psk_kex_modes) == 0) {
571
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
572
0
        return 0;
573
0
    }
574
575
0
    while (PACKET_get_1(&psk_kex_modes, &mode)) {
576
0
        if (mode == TLSEXT_KEX_MODE_KE_DHE)
577
0
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
578
0
        else if (mode == TLSEXT_KEX_MODE_KE
579
0
            && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
580
0
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
581
0
    }
582
583
0
    if (((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) != 0)
584
0
        && (s->options & SSL_OP_PREFER_NO_DHE_KEX) != 0) {
585
586
        /*
587
         * If NO_DHE is supported and preferred, then we only remember this
588
         * mode. DHE PSK will not be used for sure, because in any case where
589
         * it would be supported (i.e. if a key share is present), NO_DHE would
590
         * be supported as well. As the latter is preferred it would be
591
         * chosen. By removing DHE PSK here, we don't have to deal with the
592
         * SSL_OP_PREFER_NO_DHE_KEX option in any other place.
593
         */
594
0
        s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE;
595
0
    }
596
597
0
#endif
598
599
0
    return 1;
600
0
}
601
602
/*
603
 * Use function tls_parse_ctos_key_share with helper functions extract_keyshares,
604
 * check_overlap and tls_accept_ksgroup to parse the key_share extension(s)
605
 * received in the ClientHello and to select the group used of the key exchange
606
 */
607
608
#ifndef OPENSSL_NO_TLS1_3
609
/*
610
 * Accept a key share group by setting the related variables in s->s3 and
611
 * by generating a pubkey for this group
612
 */
613
static int tls_accept_ksgroup(SSL_CONNECTION *s, uint16_t ksgroup, PACKET *encoded_pubkey)
614
0
{
615
    /* Accept the key share group */
616
0
    s->s3.group_id = ksgroup;
617
0
    s->s3.group_id_candidate = ksgroup;
618
    /* Cache the selected group ID in the SSL_SESSION */
619
0
    s->session->kex_group = ksgroup;
620
0
    if ((s->s3.peer_tmp = ssl_generate_param_group(s, ksgroup)) == NULL) {
621
0
        SSLfatal(s,
622
0
            SSL_AD_INTERNAL_ERROR,
623
0
            SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
624
0
        return 0;
625
0
    }
626
0
    if (tls13_set_encoded_pub_key(s->s3.peer_tmp,
627
0
            PACKET_data(encoded_pubkey),
628
0
            PACKET_remaining(encoded_pubkey))
629
0
        <= 0) {
630
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
631
0
        return 0;
632
0
    }
633
0
    return 1;
634
0
}
635
636
#define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (nominally 64 Bytes chunks) */
637
638
typedef enum KS_EXTRACTION_RESULT {
639
    EXTRACTION_FAILURE,
640
    EXTRACTION_SUCCESS,
641
    EXTRACTION_SUCCESS_HRR
642
} KS_EXTRACTION_RESULT;
643
644
static KS_EXTRACTION_RESULT extract_keyshares(SSL_CONNECTION *s, PACKET *key_share_list,
645
    const uint16_t *clntgroups, size_t clnt_num_groups,
646
    const uint16_t *srvrgroups, size_t srvr_num_groups,
647
    uint16_t **keyshares_arr, PACKET **encoded_pubkey_arr,
648
    size_t *keyshares_cnt)
649
0
{
650
0
    PACKET encoded_pubkey;
651
0
    size_t key_share_pos = 0;
652
0
    size_t previous_key_share_pos = 0;
653
0
    unsigned int group_id = 0;
654
0
    unsigned int i;
655
656
    /*
657
     * Theoretically there is no limit on the number of keyshares as long as
658
     * they are less than 2^16 bytes in total. It costs us something for each
659
     * keyshare to confirm the groups are valid, so we restrict this to a
660
     * sensible number (MAX_KEY_SHARES == 16). Any keyshares over this limit are
661
     * simply ignored.
662
     */
663
664
    /* Prepare memory to hold the extracted key share groups and related pubkeys */
665
0
    *keyshares_arr = OPENSSL_malloc_array(MAX_KEY_SHARES,
666
0
        sizeof(**keyshares_arr));
667
0
    if (*keyshares_arr == NULL) {
668
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
669
0
        goto failure;
670
0
    }
671
0
    *encoded_pubkey_arr = OPENSSL_malloc_array(MAX_KEY_SHARES,
672
0
        sizeof(**encoded_pubkey_arr));
673
0
    if (*encoded_pubkey_arr == NULL) {
674
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
675
0
        goto failure;
676
0
    }
677
678
    /*
679
     * We limit the number of key shares we are willing to process to
680
     * MAX_KEY_SHARES regardless of whether we include them in keyshares_arr or
681
     * not.
682
     */
683
0
    for (i = 0; PACKET_remaining(key_share_list) > 0 && i < MAX_KEY_SHARES; i++) {
684
        /* Get the group_id for the current share and its encoded_pubkey */
685
0
        if (!PACKET_get_net_2(key_share_list, &group_id)
686
0
            || !PACKET_get_length_prefixed_2(key_share_list, &encoded_pubkey)
687
0
            || PACKET_remaining(&encoded_pubkey) == 0) {
688
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
689
0
            goto failure;
690
0
        }
691
692
        /*
693
         * If we sent an HRR then the key_share sent back MUST be for the group
694
         * we requested, and must be the only key_share sent.
695
         */
696
0
        if (s->s3.group_id != 0
697
0
            && (group_id != s->s3.group_id
698
0
                || PACKET_remaining(key_share_list) != 0)) {
699
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
700
0
            goto failure;
701
0
        }
702
703
        /*
704
         * Check if this share is in supported_groups sent from client
705
         * RFC 9846 also mandates that clients send keyshares in the same
706
         * order as listed in the supported groups extension, but its not
707
         * required that the server check that, and some clients violate this
708
         * so instead of failing the connection when that occurs, log a trace
709
         * message indicating the client discrepancy.
710
         */
711
0
        if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0, &key_share_pos)) {
712
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
713
0
            goto failure;
714
0
        }
715
716
0
        if (key_share_pos < previous_key_share_pos)
717
0
            OSSL_TRACE1(TLS, "key share group id %d is out of RFC 9846 order\n", group_id);
718
719
0
        previous_key_share_pos = key_share_pos;
720
721
0
        if (s->s3.group_id != 0) {
722
            /*
723
             * We have sent a HRR, and the key share we got back is
724
             * the one we expected and is the only key share and is
725
             * in the list of supported_groups (checked
726
             * above already), hence we accept this key share group
727
             */
728
0
            if (!tls_accept_ksgroup(s, s->s3.group_id, &encoded_pubkey))
729
0
                goto failure; /* SSLfatal already called */
730
            /* We have selected a key share group via HRR, hence we're done here */
731
0
            return EXTRACTION_SUCCESS_HRR;
732
0
        }
733
734
        /*
735
         * We tolerate but ignore a group id that we don't think is
736
         * suitable for TLSv1.3 or which is not supported by the server
737
         */
738
0
        if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1, NULL)
739
0
            || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
740
0
            || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
741
0
                NULL, NULL)) {
742
            /* Share not suitable or not supported, check next share */
743
0
            continue;
744
0
        }
745
746
        /* Memorize this key share group ID and its encoded point */
747
0
        (*keyshares_arr)[*keyshares_cnt] = group_id;
748
0
        (*encoded_pubkey_arr)[(*keyshares_cnt)++] = encoded_pubkey;
749
0
    }
750
751
0
    return EXTRACTION_SUCCESS;
752
753
0
failure:
754
    /* Fatal error -> free any allocated memory and return 0 */
755
0
    OPENSSL_free(*keyshares_arr);
756
0
    OPENSSL_free(*encoded_pubkey_arr);
757
0
    return EXTRACTION_FAILURE;
758
0
}
759
#endif
760
761
/*
762
 * For each group in the priority list of groups, check if that group is
763
 * also present in the secondary list; if so, select the first overlap and
764
 * assign to selected_group and also set the related index in the candidate group list,
765
 * or set selected_group to 0 if no overlap
766
 */
767
#ifndef OPENSSL_NO_TLS1_3
768
static void check_overlap(SSL_CONNECTION *s,
769
    const uint16_t *prio_groups, size_t prio_num_groups,
770
    const uint16_t *candidate_groups, size_t candidate_num_groups,
771
    int *prio_group_idx, int *candidate_group_idx,
772
    uint16_t *selected_group)
773
0
{
774
0
    uint16_t current_group;
775
0
    size_t group_idx = prio_num_groups;
776
0
    size_t new_group_idx = 0;
777
778
0
    *candidate_group_idx = 0;
779
0
    *prio_group_idx = 0;
780
0
    *selected_group = 0;
781
782
0
    for (current_group = 0; current_group < candidate_num_groups; current_group++) {
783
0
        if (!check_in_list(s, candidate_groups[current_group], prio_groups,
784
0
                prio_num_groups, 1, &new_group_idx)
785
0
            || !tls_group_allowed(s, candidate_groups[current_group],
786
0
                SSL_SECOP_CURVE_SUPPORTED)
787
0
            || !tls_valid_group(s, candidate_groups[current_group], TLS1_3_VERSION,
788
0
                TLS1_3_VERSION, NULL, NULL))
789
            /* No overlap or group not suitable, check next group */
790
0
            continue;
791
792
        /*
793
         * is the found new_group_idx earlier in the priority list than
794
         * initial or last group_idx?
795
         */
796
0
        if (new_group_idx < group_idx) {
797
0
            group_idx = new_group_idx;
798
0
            *candidate_group_idx = current_group;
799
0
            *prio_group_idx = (int)group_idx;
800
0
            *selected_group = prio_groups[group_idx];
801
0
        }
802
0
    }
803
0
}
804
#endif
805
806
int tls_parse_ctos_key_share(SSL_CONNECTION *s, PACKET *pkt,
807
    unsigned int context, X509 *x, size_t chainidx)
808
0
{
809
0
#ifndef OPENSSL_NO_TLS1_3
810
0
    PACKET key_share_list;
811
0
    const uint16_t *clntgroups, *srvrgroups;
812
0
    const size_t *srvrtuples;
813
0
    uint16_t *first_group_in_tuple;
814
0
    size_t clnt_num_groups, srvr_num_groups, srvr_num_tuples;
815
0
    PACKET *encoded_pubkey_arr = NULL;
816
0
    uint16_t *keyshares_arr = NULL;
817
0
    size_t keyshares_cnt = 0;
818
    /* We conservatively assume that we did not find a suitable group */
819
0
    uint16_t group_id_candidate = 0;
820
0
    KS_EXTRACTION_RESULT ks_extraction_result;
821
0
    size_t current_tuple;
822
0
    int ret = 0;
823
824
0
    s->s3.group_id_candidate = 0;
825
0
    if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
826
0
        return 1;
827
828
    /* Sanity check */
829
0
    if (s->s3.peer_tmp != NULL) {
830
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
831
0
        return 0;
832
0
    }
833
834
0
    if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
835
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
836
0
        return 0;
837
0
    }
838
839
    /* Get list of server supported groups and the group tuples */
840
0
    tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);
841
0
    tls1_get_group_tuples(s, &srvrtuples, &srvr_num_tuples);
842
    /* Get the clients list of supported groups. */
843
0
    tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
844
845
0
    if (clnt_num_groups == 0) {
846
        /*
847
         * This can only happen if the supported_groups extension was not sent,
848
         * because we verify that the length is non-zero when we process that
849
         * extension.
850
         */
851
0
        SSLfatal(s, SSL_AD_MISSING_EXTENSION,
852
0
            SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
853
0
        return 0;
854
0
    }
855
856
0
    if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) {
857
        /*
858
         * If we set a group_id already, then we must have sent an HRR
859
         * requesting a new key_share. If we haven't got one then that is an
860
         * error
861
         */
862
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
863
0
        return 0;
864
0
    }
865
866
    /* We parse the key share extension and memorize the entries (after some checks) */
867
0
    ks_extraction_result = extract_keyshares(s,
868
0
        &key_share_list,
869
0
        clntgroups, clnt_num_groups,
870
0
        srvrgroups, srvr_num_groups,
871
0
        &keyshares_arr, &encoded_pubkey_arr,
872
0
        &keyshares_cnt);
873
874
0
    if (ks_extraction_result == EXTRACTION_FAILURE) /* Fatal error during tests */
875
0
        return 0; /* Memory already freed and SSLfatal already called */
876
0
    if (ks_extraction_result == EXTRACTION_SUCCESS_HRR) /* Successful HRR */
877
0
        goto end;
878
879
    /*
880
     * We now have the following lists available to make a decision for
881
     * which group the server should use for key exchange :
882
     * From client: clntgroups[clnt_num_groups],
883
     *              keyshares_arr[keyshares_cnt], encoded_pubkey_arr[keyshares_cnt]
884
     * From server: srvrgroups[srvr_num_groups], srvrtuples[srvr_num_tuples]
885
     *
886
     * Group selection algorithm:
887
     *    For all tuples do:
888
     *      key share group(s) overlapping with current tuple?
889
     *         --> Yes: accept group_id for SH
890
     *        --> No: is any of the client supported_groups overlapping with current tuple?
891
     *            --> Yes: memorize group_id for HRR, break
892
     *             --> No: continue to check next tuple
893
     *
894
     * Remark: Selection priority different for client- or server-preference
895
     */
896
0
    first_group_in_tuple = (uint16_t *)srvrgroups;
897
0
    for (current_tuple = 0; current_tuple < srvr_num_tuples; current_tuple++) {
898
0
        size_t number_of_groups_in_tuple = srvrtuples[current_tuple];
899
0
        int prio_group_idx = 0, candidate_group_idx = 0;
900
901
        /* Server or client preference ? */
902
0
        if (s->options & SSL_OP_SERVER_PREFERENCE) {
903
            /* Server preference */
904
            /* Is there overlap with a key share group?  */
905
0
            check_overlap(s,
906
0
                first_group_in_tuple, number_of_groups_in_tuple,
907
0
                keyshares_arr, keyshares_cnt,
908
0
                &prio_group_idx, &candidate_group_idx,
909
0
                &group_id_candidate);
910
0
            if (group_id_candidate > 0) { /* Overlap found -> accept the key share group */
911
0
                if (!tls_accept_ksgroup(s, group_id_candidate,
912
0
                        &encoded_pubkey_arr[candidate_group_idx]))
913
0
                    goto err; /* SSLfatal already called */
914
                /* We have all info for a SH, hence we're done here */
915
0
                goto end;
916
0
            } else {
917
                /*
918
                 * There's no overlap with a key share, but is there at least a client
919
                 * supported_group overlapping with the current tuple?
920
                 */
921
0
                check_overlap(s,
922
0
                    first_group_in_tuple, number_of_groups_in_tuple,
923
0
                    clntgroups, clnt_num_groups,
924
0
                    &prio_group_idx, &candidate_group_idx,
925
0
                    &group_id_candidate);
926
0
                if (group_id_candidate > 0) {
927
                    /*
928
                     * We did not have a key share overlap, but at least the supported
929
                     * groups overlap hence we can stop searching
930
                     * (and report group_id_candidate 'upward' for HRR)
931
                     */
932
0
                    s->s3.group_id_candidate = group_id_candidate;
933
0
                    goto end;
934
0
                } else {
935
                    /*
936
                     * Neither key share nor supported_groups overlap current
937
                     * tuple, hence we try the next tuple
938
                     */
939
0
                    first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];
940
0
                    continue;
941
0
                }
942
0
            }
943
944
0
        } else { /* We have client preference */
945
0
            check_overlap(s,
946
0
                keyshares_arr, keyshares_cnt,
947
0
                first_group_in_tuple, number_of_groups_in_tuple,
948
0
                &prio_group_idx, &candidate_group_idx,
949
0
                &group_id_candidate);
950
0
            if (group_id_candidate > 0) {
951
0
                if (!tls_accept_ksgroup(s, group_id_candidate, &encoded_pubkey_arr[prio_group_idx]))
952
0
                    goto err;
953
0
                goto end;
954
0
            } else {
955
0
                check_overlap(s,
956
0
                    clntgroups, clnt_num_groups,
957
0
                    first_group_in_tuple, number_of_groups_in_tuple,
958
0
                    &prio_group_idx, &candidate_group_idx,
959
0
                    &group_id_candidate);
960
0
                if (group_id_candidate > 0) {
961
0
                    s->s3.group_id_candidate = group_id_candidate;
962
0
                    goto end;
963
0
                } else {
964
0
                    first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];
965
0
                    continue;
966
0
                }
967
0
            }
968
0
        }
969
0
    }
970
971
0
end:
972
0
    ret = 1;
973
974
0
err:
975
0
    OPENSSL_free(keyshares_arr);
976
0
    OPENSSL_free(encoded_pubkey_arr);
977
0
    return ret;
978
979
0
#endif
980
981
0
    return 1;
982
0
}
983
984
int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
985
    X509 *x, size_t chainidx)
986
0
{
987
0
#ifndef OPENSSL_NO_TLS1_3
988
0
    unsigned int format, version, key_share, group_id;
989
0
    EVP_MD_CTX *hctx;
990
0
    EVP_PKEY *pkey;
991
0
    PACKET cookie, raw, chhash, appcookie;
992
0
    WPACKET hrrpkt;
993
0
    const unsigned char *data, *mdin, *ciphdata;
994
0
    unsigned char hmac[SHA256_DIGEST_LENGTH];
995
0
    unsigned char hrr[MAX_HRR_SIZE];
996
0
    size_t rawlen, hmaclen, hrrlen, ciphlen;
997
0
    uint64_t tm, now;
998
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
999
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1000
1001
    /* Ignore any cookie if we're not set up to verify it */
1002
0
    if (sctx->verify_stateless_cookie_cb == NULL
1003
0
        || (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
1004
0
        return 1;
1005
1006
0
    if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {
1007
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1008
0
        return 0;
1009
0
    }
1010
1011
0
    raw = cookie;
1012
0
    data = PACKET_data(&raw);
1013
0
    rawlen = PACKET_remaining(&raw);
1014
0
    if (rawlen < SHA256_DIGEST_LENGTH
1015
0
        || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {
1016
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1017
0
        return 0;
1018
0
    }
1019
0
    mdin = PACKET_data(&raw);
1020
1021
    /* Verify the HMAC of the cookie */
1022
0
    hctx = EVP_MD_CTX_create();
1023
0
    pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
1024
0
        sctx->propq,
1025
0
        s->session_ctx->ext.cookie_hmac_key,
1026
0
        sizeof(s->session_ctx->ext.cookie_hmac_key));
1027
0
    if (hctx == NULL || pkey == NULL) {
1028
0
        EVP_MD_CTX_free(hctx);
1029
0
        EVP_PKEY_free(pkey);
1030
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
1031
0
        return 0;
1032
0
    }
1033
1034
0
    hmaclen = SHA256_DIGEST_LENGTH;
1035
0
    if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,
1036
0
            sctx->propq, pkey, NULL)
1037
0
            <= 0
1038
0
        || EVP_DigestSign(hctx, hmac, &hmaclen, data,
1039
0
               rawlen - SHA256_DIGEST_LENGTH)
1040
0
            <= 0
1041
0
        || hmaclen != SHA256_DIGEST_LENGTH) {
1042
0
        EVP_MD_CTX_free(hctx);
1043
0
        EVP_PKEY_free(pkey);
1044
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1045
0
        return 0;
1046
0
    }
1047
1048
0
    EVP_MD_CTX_free(hctx);
1049
0
    EVP_PKEY_free(pkey);
1050
1051
0
    if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) {
1052
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
1053
0
        return 0;
1054
0
    }
1055
1056
0
    if (!PACKET_get_net_2(&cookie, &format)) {
1057
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1058
0
        return 0;
1059
0
    }
1060
    /* Check the cookie format is something we recognise. Ignore it if not */
1061
0
    if (format != COOKIE_STATE_FORMAT_VERSION)
1062
0
        return 1;
1063
1064
    /*
1065
     * The rest of these checks really shouldn't fail since we have verified the
1066
     * HMAC above.
1067
     */
1068
1069
    /* Check the version number is sane */
1070
0
    if (!PACKET_get_net_2(&cookie, &version)) {
1071
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1072
0
        return 0;
1073
0
    }
1074
0
    if (version != TLS1_3_VERSION) {
1075
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1076
0
            SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1077
0
        return 0;
1078
0
    }
1079
1080
0
    if (!PACKET_get_net_2(&cookie, &group_id)) {
1081
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1082
0
        return 0;
1083
0
    }
1084
1085
0
    ciphdata = PACKET_data(&cookie);
1086
0
    if (!PACKET_forward(&cookie, 2)) {
1087
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1088
0
        return 0;
1089
0
    }
1090
0
    if (group_id != s->s3.group_id
1091
0
        || s->s3.tmp.new_cipher
1092
0
            != ssl_get_cipher_by_char(s, ciphdata, 0)) {
1093
        /*
1094
         * We chose a different cipher or group id this time around to what is
1095
         * in the cookie. Something must have changed.
1096
         */
1097
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
1098
0
        return 0;
1099
0
    }
1100
1101
0
    if (!PACKET_get_1(&cookie, &key_share)
1102
0
        || !PACKET_get_net_8(&cookie, &tm)
1103
0
        || !PACKET_get_length_prefixed_2(&cookie, &chhash)
1104
0
        || !PACKET_get_length_prefixed_1(&cookie, &appcookie)
1105
0
        || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) {
1106
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1107
0
        return 0;
1108
0
    }
1109
1110
    /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */
1111
0
    now = time(NULL);
1112
0
    if (tm > now || (now - tm) > 600) {
1113
        /* Cookie is stale. Ignore it */
1114
0
        return 1;
1115
0
    }
1116
1117
    /* Verify the app cookie */
1118
0
    if (sctx->verify_stateless_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s),
1119
0
            PACKET_data(&appcookie),
1120
0
            PACKET_remaining(&appcookie))
1121
0
        == 0) {
1122
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
1123
0
        return 0;
1124
0
    }
1125
1126
    /*
1127
     * Reconstruct the HRR that we would have sent in response to the original
1128
     * ClientHello so we can add it to the transcript hash.
1129
     * Note: This won't work with custom HRR extensions
1130
     */
1131
0
    if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {
1132
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1133
0
        return 0;
1134
0
    }
1135
0
    if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)
1136
0
        || !WPACKET_start_sub_packet_u24(&hrrpkt)
1137
0
        || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)
1138
0
        || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)
1139
0
        || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,
1140
0
            s->tmp_session_id_len)
1141
0
        || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt,
1142
0
            &ciphlen)
1143
0
        || !WPACKET_put_bytes_u8(&hrrpkt, 0)
1144
0
        || !WPACKET_start_sub_packet_u16(&hrrpkt)) {
1145
0
        WPACKET_cleanup(&hrrpkt);
1146
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1147
0
        return 0;
1148
0
    }
1149
0
    if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)
1150
0
        || !WPACKET_start_sub_packet_u16(&hrrpkt)
1151
0
        || !WPACKET_put_bytes_u16(&hrrpkt, s->version)
1152
0
        || !WPACKET_close(&hrrpkt)) {
1153
0
        WPACKET_cleanup(&hrrpkt);
1154
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1155
0
        return 0;
1156
0
    }
1157
0
    if (key_share) {
1158
0
        if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)
1159
0
            || !WPACKET_start_sub_packet_u16(&hrrpkt)
1160
0
            || !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id)
1161
0
            || !WPACKET_close(&hrrpkt)) {
1162
0
            WPACKET_cleanup(&hrrpkt);
1163
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1164
0
            return 0;
1165
0
        }
1166
0
    }
1167
0
    if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)
1168
0
        || !WPACKET_start_sub_packet_u16(&hrrpkt)
1169
0
        || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)
1170
0
        || !WPACKET_close(&hrrpkt) /* cookie extension */
1171
0
        || !WPACKET_close(&hrrpkt) /* extension block */
1172
0
        || !WPACKET_close(&hrrpkt) /* message */
1173
0
        || !WPACKET_get_total_written(&hrrpkt, &hrrlen)
1174
0
        || !WPACKET_finish(&hrrpkt)) {
1175
0
        WPACKET_cleanup(&hrrpkt);
1176
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1177
0
        return 0;
1178
0
    }
1179
1180
    /* Reconstruct the transcript hash */
1181
0
    if (!create_synthetic_message_hash(s, PACKET_data(&chhash),
1182
0
            PACKET_remaining(&chhash), hrr,
1183
0
            hrrlen)) {
1184
        /* SSLfatal() already called */
1185
0
        return 0;
1186
0
    }
1187
1188
    /* Act as if this ClientHello came after a HelloRetryRequest */
1189
0
    s->hello_retry_request = SSL_HRR_PENDING;
1190
1191
0
    s->ext.cookieok = 1;
1192
0
#endif
1193
1194
0
    return 1;
1195
0
}
1196
1197
int tls_parse_ctos_supported_groups(SSL_CONNECTION *s, PACKET *pkt,
1198
    unsigned int context,
1199
    X509 *x, size_t chainidx)
1200
0
{
1201
0
    PACKET supported_groups_list;
1202
1203
    /* Each group is 2 bytes and we must have at least 1. */
1204
0
    if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
1205
0
        || PACKET_remaining(&supported_groups_list) == 0
1206
0
        || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
1207
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1208
0
        return 0;
1209
0
    }
1210
1211
0
    if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
1212
0
        OPENSSL_free(s->ext.peer_supportedgroups);
1213
0
        s->ext.peer_supportedgroups = NULL;
1214
0
        s->ext.peer_supportedgroups_len = 0;
1215
        /*
1216
         * We only pay attention to the first 128 supported groups and ignore
1217
         * any beyond that limit. Theoretically this could cause problems if
1218
         * the client also uses one of these groups (say in a key share extension)
1219
         * - but why would any valid client be sending such a huge supported
1220
         * groups list?
1221
         */
1222
0
        if (!tls1_save_u16(&supported_groups_list,
1223
0
                &s->ext.peer_supportedgroups,
1224
0
                &s->ext.peer_supportedgroups_len, MAX_SUPPORTED_GROUPS)) {
1225
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1226
0
            return 0;
1227
0
        }
1228
0
    }
1229
1230
0
    return 1;
1231
0
}
1232
1233
int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1234
    X509 *x, size_t chainidx)
1235
0
{
1236
    /* The extension must always be empty */
1237
0
    if (PACKET_remaining(pkt) != 0) {
1238
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1239
0
        return 0;
1240
0
    }
1241
1242
0
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
1243
0
        return 1;
1244
1245
0
    s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1246
1247
0
    return 1;
1248
0
}
1249
1250
int tls_parse_ctos_early_data(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1251
    X509 *x, size_t chainidx)
1252
0
{
1253
0
    if (PACKET_remaining(pkt) != 0) {
1254
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1255
0
        return 0;
1256
0
    }
1257
1258
0
    if (s->hello_retry_request != SSL_HRR_NONE) {
1259
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1260
0
        return 0;
1261
0
    }
1262
1263
0
    return 1;
1264
0
}
1265
1266
static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL_CONNECTION *s, PACKET *tick,
1267
    SSL_SESSION **sess)
1268
0
{
1269
0
    SSL_SESSION *tmpsess = NULL;
1270
1271
0
    s->ext.ticket_expected = 1;
1272
1273
0
    switch (PACKET_remaining(tick)) {
1274
0
    case 0:
1275
0
        return SSL_TICKET_EMPTY;
1276
1277
0
    case SSL_MAX_SSL_SESSION_ID_LENGTH:
1278
0
        break;
1279
1280
0
    default:
1281
0
        return SSL_TICKET_NO_DECRYPT;
1282
0
    }
1283
1284
0
    tmpsess = lookup_sess_in_cache(s, PACKET_data(tick),
1285
0
        SSL_MAX_SSL_SESSION_ID_LENGTH);
1286
1287
0
    if (tmpsess == NULL)
1288
0
        return SSL_TICKET_NO_DECRYPT;
1289
1290
0
    *sess = tmpsess;
1291
0
    return SSL_TICKET_SUCCESS;
1292
0
}
1293
1294
int tls_parse_ctos_psk(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1295
    X509 *x, size_t chainidx)
1296
0
{
1297
0
    PACKET identities, binders, binder;
1298
0
    size_t binderoffset;
1299
0
    int hashsize;
1300
0
    SSL_SESSION *sess = NULL;
1301
0
    unsigned int id, i, ext = 0;
1302
0
    const EVP_MD *md = NULL;
1303
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1304
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1305
1306
    /*
1307
     * If we have no PSK kex mode that we recognise then we can't resume so
1308
     * ignore this extension
1309
     */
1310
0
    if ((s->ext.psk_kex_mode
1311
0
            & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE))
1312
0
        == 0)
1313
0
        return 1;
1314
1315
0
    if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
1316
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1317
0
        return 0;
1318
0
    }
1319
    /* There must always be at least one identity in the list */
1320
0
    if (PACKET_remaining(&identities) == 0) {
1321
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1322
0
        goto err;
1323
0
    }
1324
1325
0
    s->ext.ticket_expected = 0;
1326
0
    for (id = 0; PACKET_remaining(&identities) != 0 && id < MAX_PRE_SHARED_KEYS; id++) {
1327
0
        PACKET identity;
1328
0
        unsigned long ticket_agel;
1329
0
        size_t idlen;
1330
1331
0
        if (!PACKET_get_length_prefixed_2(&identities, &identity)
1332
0
            || !PACKET_get_net_4(&identities, &ticket_agel)) {
1333
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1334
0
            return 0;
1335
0
        }
1336
1337
0
        idlen = PACKET_remaining(&identity);
1338
0
        if (idlen == 0) {
1339
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1340
0
            return 0;
1341
0
        }
1342
0
        if (s->psk_find_session_cb != NULL
1343
0
            && !s->psk_find_session_cb(ussl, PACKET_data(&identity), idlen,
1344
0
                &sess)) {
1345
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION);
1346
0
            return 0;
1347
0
        }
1348
1349
0
#ifndef OPENSSL_NO_PSK
1350
0
        if (sess == NULL
1351
0
            && s->psk_server_callback != NULL
1352
0
            && idlen <= PSK_MAX_IDENTITY_LEN) {
1353
0
            char *pskid = NULL;
1354
0
            unsigned char pskdata[PSK_MAX_PSK_LEN];
1355
0
            unsigned int pskdatalen;
1356
1357
0
            if (!PACKET_strndup(&identity, &pskid)) {
1358
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1359
0
                return 0;
1360
0
            }
1361
0
            pskdatalen = s->psk_server_callback(ussl, pskid, pskdata,
1362
0
                sizeof(pskdata));
1363
0
            OPENSSL_free(pskid);
1364
0
            if (pskdatalen > PSK_MAX_PSK_LEN) {
1365
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1366
0
                return 0;
1367
0
            } else if (pskdatalen > 0) {
1368
0
                const SSL_CIPHER *cipher;
1369
0
                const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
1370
1371
                /*
1372
                 * We found a PSK using an old style callback. We don't know
1373
                 * the digest so we default to SHA256 as per the TLSv1.3 spec
1374
                 */
1375
0
                cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),
1376
0
                    tls13_aes128gcmsha256_id);
1377
0
                if (cipher == NULL) {
1378
0
                    OPENSSL_cleanse(pskdata, pskdatalen);
1379
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1380
0
                    return 0;
1381
0
                }
1382
1383
0
                sess = SSL_SESSION_new();
1384
0
                if (sess == NULL
1385
0
                    || !SSL_SESSION_set1_master_key(sess, pskdata,
1386
0
                        pskdatalen)
1387
0
                    || !SSL_SESSION_set_cipher(sess, cipher)
1388
0
                    || !SSL_SESSION_set_protocol_version(sess,
1389
0
                        TLS1_3_VERSION)) {
1390
0
                    OPENSSL_cleanse(pskdata, pskdatalen);
1391
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1392
0
                    goto err;
1393
0
                }
1394
0
                OPENSSL_cleanse(pskdata, pskdatalen);
1395
0
            }
1396
0
        }
1397
0
#endif /* OPENSSL_NO_PSK */
1398
1399
0
        if (sess != NULL) {
1400
            /*
1401
             * We found an external (not a resumption) PSK - duplicate the
1402
             * session, set the session id to our own, and mark it as external.
1403
             */
1404
0
            SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
1405
1406
0
            if (sesstmp == NULL) {
1407
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1408
0
                goto err;
1409
0
            }
1410
0
            SSL_SESSION_free(sess);
1411
0
            sess = sesstmp;
1412
1413
            /*
1414
             * We've just been told to use this session for this context so
1415
             * make sure the sid_ctx matches up.
1416
             */
1417
0
            memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
1418
0
            sess->sid_ctx_length = s->sid_ctx_length;
1419
0
            sess->psk_external = ext = 1;
1420
0
            if (id == 0)
1421
0
                s->ext.early_data_ok = 1;
1422
0
            s->ext.ticket_expected = 1;
1423
0
        } else {
1424
0
            OSSL_TIME t, age, expire;
1425
0
            int ret;
1426
1427
            /*
1428
             * If we are using anti-replay protection then we behave as if
1429
             * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
1430
             * is no point in using full stateless tickets.
1431
             */
1432
0
            if ((s->options & SSL_OP_NO_TICKET) != 0
1433
0
                || (s->max_early_data > 0
1434
0
                    && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))
1435
0
                ret = tls_get_stateful_ticket(s, &identity, &sess);
1436
0
            else
1437
0
                ret = tls_decrypt_ticket(s, PACKET_data(&identity),
1438
0
                    PACKET_remaining(&identity), NULL, 0,
1439
0
                    &sess);
1440
1441
0
            if (ret == SSL_TICKET_EMPTY) {
1442
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1443
0
                goto err;
1444
0
            }
1445
1446
0
            if (ret == SSL_TICKET_FATAL_ERR_MALLOC
1447
0
                || ret == SSL_TICKET_FATAL_ERR_OTHER) {
1448
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1449
0
                goto err;
1450
0
            }
1451
0
            if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)
1452
0
                continue;
1453
1454
            /* Check for replay */
1455
0
            if (s->max_early_data > 0
1456
0
                && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0
1457
0
                && !SSL_CTX_remove_session(s->session_ctx, sess)) {
1458
0
                SSL_SESSION_free(sess);
1459
0
                sess = NULL;
1460
0
                continue;
1461
0
            }
1462
1463
0
            age = ossl_time_subtract(ossl_ms2time(ticket_agel),
1464
0
                ossl_ms2time(sess->ext.tick_age_add));
1465
0
            t = ossl_time_subtract(ossl_time_now(), sess->time);
1466
1467
            /*
1468
             * Although internally we use OSS_TIME which has ns granularity,
1469
             * when SSL_SESSION structures are serialised/deserialised we use
1470
             * second granularity for the sess->time field. Therefore it could
1471
             * appear that the client's ticket age is longer than ours (our
1472
             * ticket age calculation should always be slightly longer than the
1473
             * client's due to the network latency). Therefore we add 1000ms to
1474
             * our age calculation to adjust for rounding errors.
1475
             */
1476
0
            expire = ossl_time_add(t, ossl_ms2time(1000));
1477
1478
0
            if (id == 0
1479
0
                && ossl_time_compare(sess->timeout, t) >= 0
1480
0
                && ossl_time_compare(age, expire) <= 0
1481
0
                && ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE),
1482
0
                       expire)
1483
0
                    >= 0) {
1484
                /*
1485
                 * Ticket age is within tolerance and not expired. We allow it
1486
                 * for early data
1487
                 */
1488
0
                s->ext.early_data_ok = 1;
1489
0
            }
1490
            /* This PSK is not external, use the correct binder label, ... */
1491
0
            ext = 0;
1492
0
        }
1493
1494
0
        md = ssl_md(sctx, sess->cipher->algorithm2);
1495
0
        if (md == NULL) {
1496
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1497
0
            goto err;
1498
0
        }
1499
0
        if (!EVP_MD_is_a(md,
1500
0
                EVP_MD_get0_name(ssl_md(sctx,
1501
0
                    s->s3.tmp.new_cipher->algorithm2)))) {
1502
            /* The ciphersuite is not compatible with this session. */
1503
0
            SSL_SESSION_free(sess);
1504
0
            sess = NULL;
1505
0
            s->ext.early_data_ok = 0;
1506
            /*
1507
             * We fall back to a full handshake. The new session ticket will be
1508
             * issued to the client with the newly negotiated ciphersuite,
1509
             * allowing successful resumption on future connections.
1510
             */
1511
0
            s->ext.ticket_expected = 1;
1512
0
            continue;
1513
0
        }
1514
        /*
1515
         * Same-hash ciphersuite changes are allowed for TLSv1.3 PSK
1516
         * resumption, but RFC 9846 Section 4.3.10 requires the selected
1517
         * ciphersuite to match the selected PSK before accepting early data.
1518
         */
1519
0
        if (sess->cipher->id != s->s3.tmp.new_cipher->id)
1520
0
            s->ext.early_data_ok = 0;
1521
0
        break;
1522
0
    }
1523
1524
0
    if (sess == NULL) {
1525
0
        size_t j;
1526
1527
0
        for (j = 0; j < s->ssl_pkey_num && !ssl_has_cert(s, (int)j); j++)
1528
0
            ;
1529
0
        if (j < s->ssl_pkey_num) {
1530
            /* A certificate exists. Fallback to a full handshake */
1531
0
            return 1;
1532
0
        }
1533
        /*
1534
         * decrypt_error here to keep the alert the same as if the binder
1535
         * failed. See RFC9846 Appendix F.6. Note we make no attempt to do this
1536
         * in constant time compared to verifying the binder. None of this code
1537
         * is constant time anyway.
1538
         */
1539
0
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_EXTENSION);
1540
0
        goto err;
1541
0
    }
1542
1543
0
    binderoffset = PACKET_data(pkt) - PACKET_msg_start(pkt);
1544
0
    hashsize = EVP_MD_get_size(md);
1545
0
    if (hashsize <= 0)
1546
0
        goto err;
1547
1548
0
    if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
1549
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1550
0
        goto err;
1551
0
    }
1552
1553
0
    for (i = 0; i <= id; i++) {
1554
0
        if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
1555
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1556
0
            goto err;
1557
0
        }
1558
0
    }
1559
1560
0
    if (PACKET_remaining(&binder) != (size_t)hashsize) {
1561
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1562
0
        goto err;
1563
0
    }
1564
0
    if (tls_psk_do_binder(s, md, PACKET_msg_start(pkt), binderoffset,
1565
0
            PACKET_data(&binder), NULL, sess, 0, ext)
1566
0
        != 1) {
1567
        /* SSLfatal() already called */
1568
0
        goto err;
1569
0
    }
1570
1571
0
    s->ext.tick_identity = id;
1572
1573
0
    SSL_SESSION_free(s->session);
1574
0
    s->session = sess;
1575
0
    return 1;
1576
0
err:
1577
0
    SSL_SESSION_free(sess);
1578
0
    return 0;
1579
0
}
1580
1581
int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt,
1582
    ossl_unused unsigned int context,
1583
    ossl_unused X509 *x,
1584
    ossl_unused size_t chainidx)
1585
0
{
1586
0
    if (PACKET_remaining(pkt) != 0) {
1587
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1588
0
            SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
1589
0
        return 0;
1590
0
    }
1591
1592
0
    s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
1593
1594
0
    return 1;
1595
0
}
1596
1597
/*
1598
 * Add the server's renegotiation binding
1599
 */
1600
EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
1601
    unsigned int context, X509 *x,
1602
    size_t chainidx)
1603
0
{
1604
0
    if (!s->s3.send_connection_binding)
1605
0
        return EXT_RETURN_NOT_SENT;
1606
1607
    /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
1608
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
1609
0
        || !WPACKET_start_sub_packet_u16(pkt)
1610
0
        || !WPACKET_start_sub_packet_u8(pkt)
1611
0
        || !WPACKET_memcpy(pkt, s->s3.previous_client_finished,
1612
0
            s->s3.previous_client_finished_len)
1613
0
        || !WPACKET_memcpy(pkt, s->s3.previous_server_finished,
1614
0
            s->s3.previous_server_finished_len)
1615
0
        || !WPACKET_close(pkt)
1616
0
        || !WPACKET_close(pkt)) {
1617
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1618
0
        return EXT_RETURN_FAIL;
1619
0
    }
1620
1621
0
    return EXT_RETURN_SENT;
1622
0
}
1623
1624
EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt,
1625
    unsigned int context, X509 *x,
1626
    size_t chainidx)
1627
0
{
1628
0
    if (s->servername_done != 1)
1629
0
        return EXT_RETURN_NOT_SENT;
1630
1631
    /*
1632
     * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
1633
     * We just use the servername from the initial handshake.
1634
     */
1635
0
    if (s->hit && !SSL_CONNECTION_IS_TLS13(s))
1636
0
        return EXT_RETURN_NOT_SENT;
1637
1638
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1639
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1640
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1641
0
        return EXT_RETURN_FAIL;
1642
0
    }
1643
1644
0
    return EXT_RETURN_SENT;
1645
0
}
1646
1647
/* Add/include the server's max fragment len extension into ServerHello */
1648
EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
1649
    unsigned int context, X509 *x,
1650
    size_t chainidx)
1651
0
{
1652
0
    if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
1653
0
        return EXT_RETURN_NOT_SENT;
1654
1655
    /*-
1656
     * 4 bytes for this extension type and extension length
1657
     * 1 byte for the Max Fragment Length code value.
1658
     */
1659
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
1660
0
        || !WPACKET_start_sub_packet_u16(pkt)
1661
0
        || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
1662
0
        || !WPACKET_close(pkt)) {
1663
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1664
0
        return EXT_RETURN_FAIL;
1665
0
    }
1666
1667
0
    return EXT_RETURN_SENT;
1668
0
}
1669
1670
EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
1671
    unsigned int context, X509 *x,
1672
    size_t chainidx)
1673
0
{
1674
0
    unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1675
0
    unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1676
0
    int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1677
0
    const unsigned char *plist;
1678
0
    size_t plistlen;
1679
1680
    /*
1681
     * The extension is irrelevant unless we're negotiating an ECC
1682
     * ciphersuite at TLS 1.2 or below, and the peer sent a list.  This
1683
     * is the first point at which the chosen ciphersuite is known, so
1684
     * the RFC 4492/8422 section 5.1.2 check for the required
1685
     * 'uncompressed' codepoint also happens here.
1686
     */
1687
0
    if (!using_ecc || s->ext.peer_ecpointformats == NULL)
1688
0
        return EXT_RETURN_NOT_SENT;
1689
1690
0
    if (memchr(s->ext.peer_ecpointformats,
1691
0
            TLSEXT_ECPOINTFORMAT_uncompressed,
1692
0
            s->ext.peer_ecpointformats_len)
1693
0
        == NULL) {
1694
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1695
0
            SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1696
0
        return EXT_RETURN_FAIL;
1697
0
    }
1698
1699
0
    tls1_get_formatlist(s, &plist, &plistlen);
1700
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1701
0
        || !WPACKET_start_sub_packet_u16(pkt)
1702
0
        || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1703
0
        || !WPACKET_close(pkt)) {
1704
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1705
0
        return EXT_RETURN_FAIL;
1706
0
    }
1707
1708
0
    return EXT_RETURN_SENT;
1709
0
}
1710
1711
EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
1712
    unsigned int context, X509 *x,
1713
    size_t chainidx)
1714
0
{
1715
0
    const uint16_t *groups;
1716
0
    size_t numgroups, i, first = 1;
1717
0
    int version;
1718
1719
    /* s->s3.group_id is non zero if we accepted a key_share */
1720
0
    if (s->s3.group_id == 0)
1721
0
        return EXT_RETURN_NOT_SENT;
1722
1723
    /* Get our list of supported groups */
1724
0
    tls1_get_supported_groups(s, &groups, &numgroups);
1725
0
    if (numgroups == 0) {
1726
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1727
0
        return EXT_RETURN_FAIL;
1728
0
    }
1729
1730
    /* Copy group ID if supported */
1731
0
    version = SSL_version(SSL_CONNECTION_GET_SSL(s));
1732
0
    for (i = 0; i < numgroups; i++) {
1733
0
        uint16_t group = groups[i];
1734
1735
0
        if (tls_valid_group(s, group, version, version, NULL, NULL)
1736
0
            && tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
1737
0
            if (first) {
1738
                /*
1739
                 * Check if the client is already using our preferred group. If
1740
                 * so we don't need to add this extension
1741
                 */
1742
0
                if (s->s3.group_id == group)
1743
0
                    return EXT_RETURN_NOT_SENT;
1744
1745
                /* Add extension header */
1746
0
                if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
1747
                    /* Sub-packet for supported_groups extension */
1748
0
                    || !WPACKET_start_sub_packet_u16(pkt)
1749
0
                    || !WPACKET_start_sub_packet_u16(pkt)) {
1750
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1751
0
                    return EXT_RETURN_FAIL;
1752
0
                }
1753
1754
0
                first = 0;
1755
0
            }
1756
0
            if (!WPACKET_put_bytes_u16(pkt, group)) {
1757
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1758
0
                return EXT_RETURN_FAIL;
1759
0
            }
1760
0
        }
1761
0
    }
1762
1763
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1764
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1765
0
        return EXT_RETURN_FAIL;
1766
0
    }
1767
1768
0
    return EXT_RETURN_SENT;
1769
0
}
1770
1771
EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
1772
    unsigned int context, X509 *x,
1773
    size_t chainidx)
1774
0
{
1775
    /*
1776
     * Don't tell the client to expect a NewSessionTicket when any
1777
     * ticket we'd mint would be rejected by ssl_get_prev_session()
1778
     * whenever SSL_VERIFY_PEER is set with no sid_ctx configured (see
1779
     * the checks there).  In TLS 1.2, once promised the ticket MUST
1780
     * be sent.
1781
     */
1782
0
    if (!s->ext.ticket_expected || !tls_use_ticket(s)
1783
0
        || ((s->verify_mode & SSL_VERIFY_PEER) != 0 && s->sid_ctx_length == 0)) {
1784
0
        s->ext.ticket_expected = 0;
1785
0
        return EXT_RETURN_NOT_SENT;
1786
0
    }
1787
1788
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1789
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1790
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1791
0
        return EXT_RETURN_FAIL;
1792
0
    }
1793
1794
0
    return EXT_RETURN_SENT;
1795
0
}
1796
1797
#ifndef OPENSSL_NO_OCSP
1798
EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt,
1799
    unsigned int context, X509 *x,
1800
    size_t chainidx)
1801
0
{
1802
0
    OCSP_RESPONSE *resp;
1803
1804
    /* We don't currently support this extension inside a CertificateRequest */
1805
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)
1806
0
        return EXT_RETURN_NOT_SENT;
1807
1808
0
    if (!s->ext.status_expected)
1809
0
        return EXT_RETURN_NOT_SENT;
1810
1811
    /* Try to retrieve OCSP response for the actual certificate */
1812
0
    resp = ossl_get_ocsp_response(s, (int)chainidx);
1813
1814
    /* If no OCSP response was found the extension is not sent */
1815
0
    if (resp == NULL)
1816
0
        return EXT_RETURN_NOT_SENT;
1817
1818
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1819
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1820
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1821
0
        return EXT_RETURN_FAIL;
1822
0
    }
1823
1824
    /*
1825
     * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
1826
     * send back an empty extension, with the certificate status appearing as a
1827
     * separate message
1828
     */
1829
0
    if (SSL_CONNECTION_IS_TLS13(s)
1830
0
        && !tls_construct_cert_status_body(s, resp, pkt)) {
1831
        /* SSLfatal() already called */
1832
0
        return EXT_RETURN_FAIL;
1833
0
    }
1834
0
    if (!WPACKET_close(pkt)) {
1835
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1836
0
        return EXT_RETURN_FAIL;
1837
0
    }
1838
1839
0
    return EXT_RETURN_SENT;
1840
0
}
1841
#endif
1842
1843
#ifndef OPENSSL_NO_NEXTPROTONEG
1844
EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt,
1845
    unsigned int context, X509 *x,
1846
    size_t chainidx)
1847
0
{
1848
0
    const unsigned char *npa;
1849
0
    unsigned int npalen;
1850
0
    int ret;
1851
0
    int npn_seen = s->s3.npn_seen;
1852
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1853
1854
0
    s->s3.npn_seen = 0;
1855
0
    if (!npn_seen || sctx->ext.npn_advertised_cb == NULL)
1856
0
        return EXT_RETURN_NOT_SENT;
1857
1858
0
    ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa,
1859
0
        &npalen, sctx->ext.npn_advertised_cb_arg);
1860
0
    if (ret == SSL_TLSEXT_ERR_OK) {
1861
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1862
0
            || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1863
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1864
0
            return EXT_RETURN_FAIL;
1865
0
        }
1866
0
        s->s3.npn_seen = 1;
1867
0
        return EXT_RETURN_SENT;
1868
0
    }
1869
1870
0
    return EXT_RETURN_NOT_SENT;
1871
0
}
1872
#endif
1873
1874
EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context,
1875
    X509 *x, size_t chainidx)
1876
0
{
1877
0
    if (s->s3.alpn_selected == NULL)
1878
0
        return EXT_RETURN_NOT_SENT;
1879
1880
0
    if (!WPACKET_put_bytes_u16(pkt,
1881
0
            TLSEXT_TYPE_application_layer_protocol_negotiation)
1882
0
        || !WPACKET_start_sub_packet_u16(pkt)
1883
0
        || !WPACKET_start_sub_packet_u16(pkt)
1884
0
        || !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,
1885
0
            s->s3.alpn_selected_len)
1886
0
        || !WPACKET_close(pkt)
1887
0
        || !WPACKET_close(pkt)) {
1888
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1889
0
        return EXT_RETURN_FAIL;
1890
0
    }
1891
1892
0
    return EXT_RETURN_SENT;
1893
0
}
1894
1895
#ifndef OPENSSL_NO_SRTP
1896
EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
1897
    unsigned int context, X509 *x,
1898
    size_t chainidx)
1899
0
{
1900
0
    if (s->srtp_profile == NULL)
1901
0
        return EXT_RETURN_NOT_SENT;
1902
1903
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1904
0
        || !WPACKET_start_sub_packet_u16(pkt)
1905
0
        || !WPACKET_put_bytes_u16(pkt, 2)
1906
0
        || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1907
0
        || !WPACKET_put_bytes_u8(pkt, 0)
1908
0
        || !WPACKET_close(pkt)) {
1909
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1910
0
        return EXT_RETURN_FAIL;
1911
0
    }
1912
1913
0
    return EXT_RETURN_SENT;
1914
0
}
1915
#endif
1916
1917
EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt,
1918
    unsigned int context,
1919
    X509 *x, size_t chainidx)
1920
0
{
1921
0
    if (!s->ext.use_etm)
1922
0
        return EXT_RETURN_NOT_SENT;
1923
1924
    /*
1925
     * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1926
     * for other cases too.
1927
     */
1928
0
    if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD
1929
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_RC4
1930
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1931
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12
1932
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA
1933
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) {
1934
0
        s->ext.use_etm = 0;
1935
0
        return EXT_RETURN_NOT_SENT;
1936
0
    }
1937
1938
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1939
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1940
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1941
0
        return EXT_RETURN_FAIL;
1942
0
    }
1943
1944
0
    return EXT_RETURN_SENT;
1945
0
}
1946
1947
EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt,
1948
    unsigned int context,
1949
    X509 *x, size_t chainidx)
1950
0
{
1951
0
    if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
1952
0
        return EXT_RETURN_NOT_SENT;
1953
1954
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1955
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1956
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1957
0
        return EXT_RETURN_FAIL;
1958
0
    }
1959
1960
0
    return EXT_RETURN_SENT;
1961
0
}
1962
1963
EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
1964
    unsigned int context, X509 *x,
1965
    size_t chainidx)
1966
0
{
1967
0
    if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) {
1968
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1969
0
        return EXT_RETURN_FAIL;
1970
0
    }
1971
1972
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1973
0
        || !WPACKET_start_sub_packet_u16(pkt)
1974
0
        || !WPACKET_put_bytes_u16(pkt, s->version)
1975
0
        || !WPACKET_close(pkt)) {
1976
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1977
0
        return EXT_RETURN_FAIL;
1978
0
    }
1979
1980
0
    return EXT_RETURN_SENT;
1981
0
}
1982
1983
EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt,
1984
    unsigned int context, X509 *x,
1985
    size_t chainidx)
1986
0
{
1987
0
#ifndef OPENSSL_NO_TLS1_3
1988
0
    unsigned char *encoded_pubkey;
1989
0
    size_t encoded_pubkey_len = 0;
1990
0
    EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;
1991
0
    const TLS_GROUP_INFO *ginf = NULL;
1992
1993
0
    if (s->hello_retry_request == SSL_HRR_PENDING) {
1994
0
        if (ckey != NULL) {
1995
            /* Original key_share was acceptable so don't ask for another one */
1996
0
            return EXT_RETURN_NOT_SENT;
1997
0
        }
1998
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1999
0
            || !WPACKET_start_sub_packet_u16(pkt)
2000
0
            || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
2001
0
            || !WPACKET_close(pkt)) {
2002
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2003
0
            return EXT_RETURN_FAIL;
2004
0
        }
2005
2006
0
        return EXT_RETURN_SENT;
2007
0
    }
2008
2009
0
    if (ckey == NULL) {
2010
        /* No key_share received from client - must be resuming */
2011
0
        if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
2012
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2013
0
            return EXT_RETURN_FAIL;
2014
0
        }
2015
0
        return EXT_RETURN_NOT_SENT;
2016
0
    }
2017
2018
0
    if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {
2019
        /*
2020
         * PSK ('hit') and explicitly not doing DHE. If the client sent the
2021
         * DHE option, we take it by default, except if non-DHE would be
2022
         * preferred by config, but this case would have been handled in
2023
         * tls_parse_ctos_psk_kex_modes().
2024
         */
2025
0
        return EXT_RETURN_NOT_SENT;
2026
0
    }
2027
2028
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
2029
0
        || !WPACKET_start_sub_packet_u16(pkt)
2030
0
        || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) {
2031
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2032
0
        return EXT_RETURN_FAIL;
2033
0
    }
2034
2035
0
    if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
2036
0
             s->s3.group_id))
2037
0
        == NULL) {
2038
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2039
0
        return EXT_RETURN_FAIL;
2040
0
    }
2041
2042
0
    if (!ginf->is_kem) {
2043
        /* Regular KEX */
2044
0
        skey = ssl_generate_pkey(s, ckey);
2045
0
        if (skey == NULL) {
2046
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2047
0
            return EXT_RETURN_FAIL;
2048
0
        }
2049
2050
        /* Generate encoding of server key */
2051
0
        encoded_pubkey_len = EVP_PKEY_get1_encoded_public_key(skey, &encoded_pubkey);
2052
0
        if (encoded_pubkey_len == 0) {
2053
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
2054
0
            EVP_PKEY_free(skey);
2055
0
            return EXT_RETURN_FAIL;
2056
0
        }
2057
2058
0
        if (!WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encoded_pubkey_len)
2059
0
            || !WPACKET_close(pkt)) {
2060
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2061
0
            EVP_PKEY_free(skey);
2062
0
            OPENSSL_free(encoded_pubkey);
2063
0
            return EXT_RETURN_FAIL;
2064
0
        }
2065
0
        OPENSSL_free(encoded_pubkey);
2066
2067
        /*
2068
         * This causes the crypto state to be updated based on the derived keys
2069
         */
2070
0
        if (ssl_derive(s, skey, ckey, 1) == 0) {
2071
            /* SSLfatal() already called */
2072
0
            EVP_PKEY_free(skey);
2073
0
            return EXT_RETURN_FAIL;
2074
0
        }
2075
0
        s->s3.tmp.pkey = skey;
2076
0
    } else {
2077
        /* KEM mode */
2078
0
        unsigned char *ct = NULL;
2079
0
        size_t ctlen = 0;
2080
2081
        /*
2082
         * This does not update the crypto state.
2083
         *
2084
         * The generated pms is stored in `s->s3.tmp.pms` to be later used via
2085
         * ssl_gensecret().
2086
         */
2087
0
        if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) {
2088
            /* SSLfatal() already called */
2089
0
            return EXT_RETURN_FAIL;
2090
0
        }
2091
2092
0
        if (ctlen == 0) {
2093
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2094
0
            OPENSSL_free(ct);
2095
0
            return EXT_RETURN_FAIL;
2096
0
        }
2097
2098
0
        if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)
2099
0
            || !WPACKET_close(pkt)) {
2100
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2101
0
            OPENSSL_free(ct);
2102
0
            return EXT_RETURN_FAIL;
2103
0
        }
2104
0
        OPENSSL_free(ct);
2105
2106
        /*
2107
         * This causes the crypto state to be updated based on the generated pms
2108
         */
2109
0
        if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) {
2110
            /* SSLfatal() already called */
2111
0
            return EXT_RETURN_FAIL;
2112
0
        }
2113
0
    }
2114
0
    s->s3.did_kex = 1;
2115
0
    return EXT_RETURN_SENT;
2116
#else
2117
    return EXT_RETURN_FAIL;
2118
#endif
2119
0
}
2120
2121
EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt,
2122
    unsigned int context,
2123
    X509 *x, size_t chainidx)
2124
0
{
2125
0
#ifndef OPENSSL_NO_TLS1_3
2126
0
    unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
2127
0
    unsigned char *hmac, *hmac2;
2128
0
    size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
2129
0
    EVP_MD_CTX *hctx;
2130
0
    EVP_PKEY *pkey;
2131
0
    int ret = EXT_RETURN_FAIL;
2132
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2133
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2134
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
2135
2136
0
    if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
2137
0
        return EXT_RETURN_NOT_SENT;
2138
2139
0
    if (sctx->gen_stateless_cookie_cb == NULL) {
2140
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);
2141
0
        return EXT_RETURN_FAIL;
2142
0
    }
2143
2144
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
2145
0
        || !WPACKET_start_sub_packet_u16(pkt)
2146
0
        || !WPACKET_start_sub_packet_u16(pkt)
2147
0
        || !WPACKET_get_total_written(pkt, &startlen)
2148
0
        || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
2149
0
        || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
2150
0
        || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
2151
0
        || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
2152
0
        || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,
2153
0
            &ciphlen)
2154
        /* Is there a key_share extension present in this HRR? */
2155
0
        || !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)
2156
0
        || !WPACKET_put_bytes_u64(pkt, time(NULL))
2157
0
        || !WPACKET_start_sub_packet_u16(pkt)
2158
0
        || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
2159
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2160
0
        return EXT_RETURN_FAIL;
2161
0
    }
2162
2163
    /*
2164
     * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
2165
     * on raw buffers, so we first reserve sufficient bytes (above) and then
2166
     * subsequently allocate them (below)
2167
     */
2168
0
    if (!ssl3_digest_cached_records(s, 0)
2169
0
        || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
2170
        /* SSLfatal() already called */
2171
0
        return EXT_RETURN_FAIL;
2172
0
    }
2173
2174
0
    if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
2175
0
        || !ossl_assert(hashval1 == hashval2)
2176
0
        || !WPACKET_close(pkt)
2177
0
        || !WPACKET_start_sub_packet_u8(pkt)
2178
0
        || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
2179
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2180
0
        return EXT_RETURN_FAIL;
2181
0
    }
2182
2183
    /* Generate the application cookie */
2184
0
    if (sctx->gen_stateless_cookie_cb(ussl, appcookie1,
2185
0
            &appcookielen)
2186
0
        == 0) {
2187
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
2188
0
        return EXT_RETURN_FAIL;
2189
0
    }
2190
2191
0
    if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
2192
0
        || !ossl_assert(appcookie1 == appcookie2)
2193
0
        || !WPACKET_close(pkt)
2194
0
        || !WPACKET_get_total_written(pkt, &totcookielen)
2195
0
        || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
2196
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2197
0
        return EXT_RETURN_FAIL;
2198
0
    }
2199
0
    hmaclen = SHA256_DIGEST_LENGTH;
2200
2201
0
    totcookielen -= startlen;
2202
0
    if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
2203
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2204
0
        return EXT_RETURN_FAIL;
2205
0
    }
2206
2207
    /* HMAC the cookie */
2208
0
    hctx = EVP_MD_CTX_create();
2209
0
    pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
2210
0
        sctx->propq,
2211
0
        s->session_ctx->ext.cookie_hmac_key,
2212
0
        sizeof(s->session_ctx->ext.cookie_hmac_key));
2213
0
    if (hctx == NULL || pkey == NULL) {
2214
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2215
0
        goto err;
2216
0
    }
2217
2218
0
    if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,
2219
0
            sctx->propq, pkey, NULL)
2220
0
            <= 0
2221
0
        || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
2222
0
               totcookielen)
2223
0
            <= 0) {
2224
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2225
0
        goto err;
2226
0
    }
2227
2228
0
    if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
2229
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2230
0
        goto err;
2231
0
    }
2232
2233
0
    if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
2234
0
        || !ossl_assert(hmac == hmac2)
2235
0
        || !ossl_assert(cookie == hmac - totcookielen)
2236
0
        || !WPACKET_close(pkt)
2237
0
        || !WPACKET_close(pkt)) {
2238
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2239
0
        goto err;
2240
0
    }
2241
2242
0
    ret = EXT_RETURN_SENT;
2243
2244
0
err:
2245
0
    EVP_MD_CTX_free(hctx);
2246
0
    EVP_PKEY_free(pkey);
2247
0
    return ret;
2248
#else
2249
    return EXT_RETURN_FAIL;
2250
#endif
2251
0
}
2252
2253
EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt,
2254
    unsigned int context, X509 *x,
2255
    size_t chainidx)
2256
0
{
2257
0
    const unsigned char cryptopro_ext[36] = {
2258
0
        0xfd, 0xe8, /* 65000 */
2259
0
        0x00, 0x20, /* 32 bytes length */
2260
0
        0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
2261
0
        0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
2262
0
        0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
2263
0
        0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
2264
0
    };
2265
2266
0
    if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x80
2267
0
            && (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)
2268
0
        || (SSL_get_options(SSL_CONNECTION_GET_SSL(s))
2269
0
               & SSL_OP_CRYPTOPRO_TLSEXT_BUG)
2270
0
            == 0)
2271
0
        return EXT_RETURN_NOT_SENT;
2272
2273
0
    if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
2274
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2275
0
        return EXT_RETURN_FAIL;
2276
0
    }
2277
2278
0
    return EXT_RETURN_SENT;
2279
0
}
2280
2281
EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt,
2282
    unsigned int context, X509 *x,
2283
    size_t chainidx)
2284
0
{
2285
0
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
2286
0
        if (s->max_early_data == 0)
2287
0
            return EXT_RETURN_NOT_SENT;
2288
2289
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2290
0
            || !WPACKET_start_sub_packet_u16(pkt)
2291
0
            || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
2292
0
            || !WPACKET_close(pkt)) {
2293
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2294
0
            return EXT_RETURN_FAIL;
2295
0
        }
2296
2297
0
        return EXT_RETURN_SENT;
2298
0
    }
2299
2300
0
    if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
2301
0
        return EXT_RETURN_NOT_SENT;
2302
2303
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2304
0
        || !WPACKET_start_sub_packet_u16(pkt)
2305
0
        || !WPACKET_close(pkt)) {
2306
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2307
0
        return EXT_RETURN_FAIL;
2308
0
    }
2309
2310
0
    return EXT_RETURN_SENT;
2311
0
}
2312
2313
EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt,
2314
    unsigned int context,
2315
    X509 *x, size_t chainidx)
2316
0
{
2317
0
    if (!s->hit)
2318
0
        return EXT_RETURN_NOT_SENT;
2319
2320
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
2321
0
        || !WPACKET_start_sub_packet_u16(pkt)
2322
0
        || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)
2323
0
        || !WPACKET_close(pkt)) {
2324
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2325
0
        return EXT_RETURN_FAIL;
2326
0
    }
2327
2328
0
    return EXT_RETURN_SENT;
2329
0
}
2330
2331
EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2332
    unsigned int context,
2333
    X509 *x, size_t chainidx)
2334
0
{
2335
0
    if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR
2336
0
        && (send_certificate_request(sc)
2337
0
            || sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) {
2338
        /* Did not receive an acceptable cert type - and doing client auth */
2339
0
        SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2340
0
        return EXT_RETURN_FAIL;
2341
0
    }
2342
2343
0
    if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) {
2344
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2345
0
        return EXT_RETURN_NOT_SENT;
2346
0
    }
2347
2348
    /*
2349
     * Note: only supposed to send this if we are going to do a cert request,
2350
     * but TLSv1.3 could do a PHA request if the client supports it
2351
     */
2352
0
    if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED)
2353
0
        || sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2354
0
        || sc->client_cert_type == NULL) {
2355
        /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2356
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2357
0
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2358
0
        return EXT_RETURN_NOT_SENT;
2359
0
    }
2360
2361
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2362
0
        || !WPACKET_start_sub_packet_u16(pkt)
2363
0
        || !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type)
2364
0
        || !WPACKET_close(pkt)) {
2365
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2366
0
        return EXT_RETURN_FAIL;
2367
0
    }
2368
0
    return EXT_RETURN_SENT;
2369
0
}
2370
2371
/* One of |pref|, |other| is configured and the values are sanitized */
2372
static int reconcile_cert_type(const unsigned char *pref, size_t pref_len,
2373
    const unsigned char *other, size_t other_len,
2374
    uint8_t *chosen_cert_type)
2375
0
{
2376
0
    size_t i;
2377
2378
0
    for (i = 0; i < pref_len; i++) {
2379
0
        if (memchr(other, pref[i], other_len) != NULL) {
2380
0
            *chosen_cert_type = pref[i];
2381
0
            return OSSL_CERT_TYPE_CTOS_GOOD;
2382
0
        }
2383
0
    }
2384
0
    return OSSL_CERT_TYPE_CTOS_ERROR;
2385
0
}
2386
2387
int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2388
    unsigned int context,
2389
    X509 *x, size_t chainidx)
2390
0
{
2391
0
    PACKET supported_cert_types;
2392
0
    const unsigned char *data;
2393
0
    size_t len;
2394
2395
    /* Ignore the extension */
2396
0
    if (sc->client_cert_type == NULL) {
2397
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2398
0
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2399
0
        return 1;
2400
0
    }
2401
2402
0
    if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2403
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2404
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2405
0
        return 0;
2406
0
    }
2407
0
    if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2408
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2409
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2410
0
        return 0;
2411
0
    }
2412
0
    if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2413
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2414
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2415
0
        return 0;
2416
0
    }
2417
    /* client_cert_type: client (peer) has priority */
2418
0
    sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len,
2419
0
        sc->client_cert_type, sc->client_cert_type_len,
2420
0
        &sc->ext.client_cert_type);
2421
2422
    /* Ignore the error until sending - so we can check cert auth*/
2423
0
    return 1;
2424
0
}
2425
2426
EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2427
    unsigned int context,
2428
    X509 *x, size_t chainidx)
2429
0
{
2430
0
    if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) {
2431
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2432
0
        return EXT_RETURN_NOT_SENT;
2433
0
    }
2434
0
    if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2435
0
        || sc->server_cert_type == NULL) {
2436
        /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2437
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2438
0
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2439
0
        return EXT_RETURN_NOT_SENT;
2440
0
    }
2441
2442
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2443
0
        || !WPACKET_start_sub_packet_u16(pkt)
2444
0
        || !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type)
2445
0
        || !WPACKET_close(pkt)) {
2446
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2447
0
        return EXT_RETURN_FAIL;
2448
0
    }
2449
0
    return EXT_RETURN_SENT;
2450
0
}
2451
2452
int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2453
    unsigned int context,
2454
    X509 *x, size_t chainidx)
2455
0
{
2456
0
    PACKET supported_cert_types;
2457
0
    const unsigned char *data;
2458
0
    size_t len;
2459
2460
    /* Ignore the extension */
2461
0
    if (sc->server_cert_type == NULL) {
2462
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2463
0
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2464
0
        return 1;
2465
0
    }
2466
2467
0
    if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2468
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2469
0
        return 0;
2470
0
    }
2471
2472
0
    if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2473
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2474
0
        return 0;
2475
0
    }
2476
0
    if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2477
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2478
0
        return 0;
2479
0
    }
2480
    /* server_cert_type: server (this) has priority */
2481
0
    sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len,
2482
0
        data, len,
2483
0
        &sc->ext.server_cert_type);
2484
0
    if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)
2485
0
        return 1;
2486
2487
    /* Did not receive an acceptable cert type */
2488
0
    SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2489
0
    return 0;
2490
0
}
2491
2492
#ifndef OPENSSL_NO_ECH
2493
/*
2494
 * ECH handling for edge cases (GREASE/inner) and errors.
2495
 * return 1 for good, 0 otherwise
2496
 *
2497
 * Real ECH handling (i.e. decryption) happens before, via
2498
 * ech_early_decrypt(), but if that failed (e.g. decryption
2499
 * failed, which may be down to GREASE) then we end up here,
2500
 * processing the ECH from the outer CH.
2501
 * Otherwise, we only expect to see an inner ECH with a fixed
2502
 * value here.
2503
 */
2504
int tls_parse_ctos_ech(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2505
    X509 *x, size_t chainidx)
2506
0
{
2507
0
    unsigned int echtype = 0;
2508
2509
0
    if (s->ext.ech.grease == OSSL_ECH_IS_GREASE) {
2510
        /* GREASE is fine */
2511
0
        return 1;
2512
0
    }
2513
0
    if (s->ext.ech.es == NULL) {
2514
        /* If not configured for ECH then we ignore it */
2515
0
        return 1;
2516
0
    }
2517
0
    if (s->ext.ech.attempted_type != TLSEXT_TYPE_ech) {
2518
        /* if/when new versions of ECH are added we'll update here */
2519
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2520
0
        return 0;
2521
0
    }
2522
    /*
2523
     * we only allow "inner" which is one octet, valued 0x01
2524
     * and only if we decrypted ok or are a backend
2525
     */
2526
0
    if (PACKET_get_1(pkt, &echtype) != 1
2527
0
        || PACKET_remaining(pkt) != 0) {
2528
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2529
0
        return 0;
2530
0
    }
2531
0
    if (echtype != OSSL_ECH_INNER_CH_TYPE) {
2532
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2533
0
        return 0;
2534
0
    }
2535
0
    s->ext.ech.inner_ech_seen_ok = 1;
2536
0
    if (s->ext.ech.success != 1 && s->ext.ech.backend != 1) {
2537
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2538
0
        return 0;
2539
0
    }
2540
    /* yay - we're ok with this */
2541
0
    OSSL_TRACE_BEGIN(TLS)
2542
0
    {
2543
0
        BIO_printf(trc_out, "ECH seen in inner as expected.\n");
2544
0
    }
2545
0
    OSSL_TRACE_END(TLS);
2546
0
    return 1;
2547
0
}
2548
2549
/*
2550
 * Answer an ECH, as needed
2551
 * return 1 for good, 0 otherwise
2552
 *
2553
 * Return most-recent ECH config for retry, as needed.
2554
 * If doing HRR we include the confirmation value, but
2555
 * for now, we'll just add the zeros - the real octets
2556
 * will be added later via ech_calc_ech_confirm() which
2557
 * is called when constructing the server hello.
2558
 */
2559
EXT_RETURN tls_construct_stoc_ech(SSL_CONNECTION *s, WPACKET *pkt,
2560
    unsigned int context, X509 *x,
2561
    size_t chainidx)
2562
0
{
2563
0
    unsigned char *rcfgs = NULL;
2564
0
    size_t rcfgslen = 0;
2565
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2566
2567
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2568
0
        && (s->ext.ech.success == 1 || s->ext.ech.backend == 1)
2569
0
        && s->ext.ech.attempted_type == TLSEXT_TYPE_ech) {
2570
0
        unsigned char eightzeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
2571
2572
0
        if (!WPACKET_put_bytes_u16(pkt, s->ext.ech.attempted_type)
2573
0
            || !WPACKET_sub_memcpy_u16(pkt, eightzeros, 8)) {
2574
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2575
0
            return 0;
2576
0
        }
2577
0
        OSSL_TRACE_BEGIN(TLS)
2578
0
        {
2579
0
            BIO_printf(trc_out, "set 8 zeros for ECH accept confirm in HRR\n");
2580
0
        }
2581
0
        OSSL_TRACE_END(TLS);
2582
0
        return EXT_RETURN_SENT;
2583
0
    }
2584
    /* GREASE or error => random confirmation in HRR case */
2585
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2586
0
        && s->ext.ech.attempted_type == TLSEXT_TYPE_ech
2587
0
        && s->ext.ech.attempted == 1) {
2588
0
        unsigned char randomconf[8];
2589
2590
0
        if (RAND_bytes_ex(sctx->libctx, randomconf, 8,
2591
0
                RAND_DRBG_STRENGTH)
2592
0
            <= 0) {
2593
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2594
0
            return 0;
2595
0
        }
2596
0
        if (!WPACKET_put_bytes_u16(pkt, s->ext.ech.attempted_type)
2597
0
            || !WPACKET_sub_memcpy_u16(pkt, randomconf, 8)) {
2598
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2599
0
            return 0;
2600
0
        }
2601
0
        OSSL_TRACE_BEGIN(TLS)
2602
0
        {
2603
0
            BIO_printf(trc_out, "set random for ECH acccpt confirm in HRR\n");
2604
0
        }
2605
0
        OSSL_TRACE_END(TLS);
2606
0
        return EXT_RETURN_SENT;
2607
0
    }
2608
    /* in other HRR circumstances: don't set */
2609
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
2610
0
        return EXT_RETURN_NOT_SENT;
2611
    /* If in some weird state we ignore and send nothing */
2612
0
    if (s->ext.ech.grease != OSSL_ECH_IS_GREASE
2613
0
        || s->ext.ech.attempted_type != TLSEXT_TYPE_ech)
2614
0
        return EXT_RETURN_NOT_SENT;
2615
    /*
2616
     * If the client GREASEd, or we think it did, return the
2617
     * most-recently loaded ECHConfigList, as the value of the
2618
     * extension. Most-recently loaded can be anywhere in the
2619
     * list, depending on changing or non-changing file names.
2620
     */
2621
0
    if (s->ext.ech.es == NULL) {
2622
0
        OSSL_TRACE_BEGIN(TLS)
2623
0
        {
2624
0
            BIO_printf(trc_out, "ECH - not sending ECHConfigList to client "
2625
0
                                "even though they GREASE'd as I've no loaded configs\n");
2626
0
        }
2627
0
        OSSL_TRACE_END(TLS);
2628
0
        return EXT_RETURN_NOT_SENT;
2629
0
    }
2630
0
    if (ossl_ech_get_retry_configs(s, &rcfgs, &rcfgslen) != 1) {
2631
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2632
0
        return 0;
2633
0
    }
2634
0
    if (rcfgslen == 0) {
2635
0
        OSSL_TRACE_BEGIN(TLS)
2636
0
        {
2637
0
            BIO_printf(trc_out, "ECH - not sending ECHConfigList to client "
2638
0
                                "even though they GREASE'd and I have configs but "
2639
0
                                "I've no configs set to be returned\n");
2640
0
        }
2641
0
        OSSL_TRACE_END(TLS);
2642
0
        OPENSSL_free(rcfgs);
2643
0
        return EXT_RETURN_NOT_SENT;
2644
0
    }
2645
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech)
2646
0
        || !WPACKET_start_sub_packet_u16(pkt)
2647
0
        || !WPACKET_sub_memcpy_u16(pkt, rcfgs, rcfgslen)
2648
0
        || !WPACKET_close(pkt)) {
2649
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2650
0
        OPENSSL_free(rcfgs);
2651
0
        return 0;
2652
0
    }
2653
0
    OPENSSL_free(rcfgs);
2654
0
    return EXT_RETURN_SENT;
2655
0
}
2656
#endif /* END OPENSSL_NO_ECH */