Coverage Report

Created: 2026-07-12 07:21

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
18.9k
#define MAX_SUPPORTED_GROUPS 128
23
744
#define MAX_KEY_SHARES 16
24
225
#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
5.12k
{
54
5.12k
    unsigned int ilen;
55
5.12k
    const unsigned char *data;
56
5.12k
    int ok;
57
58
    /* Parse the length byte */
59
5.12k
    if (!PACKET_get_1(pkt, &ilen)
60
5.10k
        || !PACKET_get_bytes(pkt, &data, ilen)) {
61
38
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
62
38
        return 0;
63
38
    }
64
65
    /* Check that the extension matches */
66
5.08k
    if (ilen != s->s3.previous_client_finished_len) {
67
19
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
68
19
        return 0;
69
19
    }
70
71
5.06k
    ok = memcmp(data, s->s3.previous_client_finished,
72
5.06k
        s->s3.previous_client_finished_len);
73
5.06k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
74
5.06k
    if (ok) {
75
0
        if ((data[0] ^ s->s3.previous_client_finished[0]) != 0xFF) {
76
0
            ok = 0;
77
0
        }
78
0
    }
79
5.06k
#endif
80
5.06k
    if (ok) {
81
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
82
0
        return 0;
83
0
    }
84
85
5.06k
    s->s3.send_connection_binding = 1;
86
87
5.06k
    return 1;
88
5.06k
}
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
8.64k
{
116
8.64k
    unsigned int servname_type;
117
8.64k
    PACKET sni, hostname;
118
119
8.64k
    if (!PACKET_as_length_prefixed_2(pkt, &sni)
120
        /* ServerNameList must be at least 1 byte long. */
121
8.42k
        || PACKET_remaining(&sni) == 0) {
122
239
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
123
239
        return 0;
124
239
    }
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
8.40k
    if (!PACKET_get_1(&sni, &servname_type)
138
8.40k
        || servname_type != TLSEXT_NAMETYPE_host_name
139
8.37k
        || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
140
159
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
141
159
        return 0;
142
159
    }
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
8.24k
    if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
149
8.23k
        if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
150
6
            SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
151
6
            return 0;
152
6
        }
153
154
8.22k
        if (PACKET_contains_zero_byte(&hostname)) {
155
18
            SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
156
18
            return 0;
157
18
        }
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
8.20k
        OPENSSL_free(s->ext.hostname);
164
8.20k
        s->ext.hostname = NULL;
165
8.20k
        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
8.20k
        s->servername_done = 1;
171
8.20k
    } 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
14
        s->servername_done = (s->session->ext.hostname != NULL)
178
7
            && PACKET_equal(&hostname, s->session->ext.hostname,
179
7
                strlen(s->session->ext.hostname));
180
14
    }
181
182
8.22k
    return 1;
183
8.24k
}
184
185
int tls_parse_ctos_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
186
    unsigned int context,
187
    X509 *x, size_t chainidx)
188
1.55k
{
189
1.55k
    unsigned int value;
190
191
1.55k
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
192
128
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
193
128
        return 0;
194
128
    }
195
196
    /* Received |value| should be a valid max-fragment-length code. */
197
1.43k
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
198
49
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
199
49
            SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH);
200
49
        return 0;
201
49
    }
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
1.38k
    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
1.37k
        s->session->ext.max_fragment_len_mode = value;
223
224
1.38k
    return 1;
225
1.43k
}
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
157
{
231
157
    PACKET srp_I;
232
233
157
    if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
234
94
        || PACKET_contains_zero_byte(&srp_I)) {
235
94
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
236
94
        return 0;
237
94
    }
238
239
63
    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
63
    return 1;
245
63
}
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
5.56k
{
252
5.56k
    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
5.56k
    return 1;
258
5.56k
}
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
1.27k
{
265
1.27k
    PACKET supported_sig_algs;
266
267
1.27k
    if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
268
1.08k
        || PACKET_remaining(&supported_sig_algs) == 0) {
269
204
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
270
204
        return 0;
271
204
    }
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
1.07k
    if ((!s->server || (s->server && !s->hit))
279
1.06k
        && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {
280
19
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
281
19
        return 0;
282
19
    }
283
284
1.05k
    return 1;
285
1.07k
}
286
287
int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt,
288
    unsigned int context, X509 *x, size_t chainidx)
289
9.89k
{
290
9.89k
    PACKET supported_sig_algs;
291
292
9.89k
    if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
293
9.63k
        || PACKET_remaining(&supported_sig_algs) == 0) {
294
266
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
295
266
        return 0;
296
266
    }
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
9.62k
    if ((!s->server || (s->server && !s->hit))
304
9.49k
        && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {
305
19
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
306
19
        return 0;
307
19
    }
308
309
9.60k
    return 1;
310
9.62k
}
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
288
{
317
288
    PACKET responder_id_list, exts;
318
319
    /* We ignore this in a resumption handshake */
320
288
    if (s->hit)
321
12
        return 1;
322
323
    /* Not defined if we get one of these in a client Certificate */
324
276
    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
276
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
333
276
    if (sctx == NULL || sctx->ext.status_cb == NULL)
334
276
        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
16
{
429
    /*
430
     * We shouldn't accept this extension on a
431
     * renegotiation.
432
     */
433
16
    if (SSL_IS_FIRST_HANDSHAKE(s))
434
16
        s->s3.npn_seen = 1;
435
436
16
    return 1;
437
16
}
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
431
{
447
431
    PACKET protocol_list, save_protocol_list, protocol;
448
449
431
    if (!SSL_IS_FIRST_HANDSHAKE(s))
450
0
        return 1;
451
452
431
    if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
453
314
        || PACKET_remaining(&protocol_list) < 2) {
454
128
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
455
128
        return 0;
456
128
    }
457
458
303
    save_protocol_list = protocol_list;
459
2.56k
    do {
460
        /* Protocol names can't be empty. */
461
2.56k
        if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
462
2.49k
            || PACKET_remaining(&protocol) == 0) {
463
113
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
464
113
            return 0;
465
113
        }
466
2.56k
    } while (PACKET_remaining(&protocol_list) != 0);
467
468
190
    OPENSSL_free(s->s3.alpn_proposed);
469
190
    s->s3.alpn_proposed = NULL;
470
190
    s->s3.alpn_proposed_len = 0;
471
190
    if (!PACKET_memdup(&save_protocol_list,
472
190
            &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
190
    return 1;
478
190
}
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
1.27k
{
484
1.27k
    STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
485
1.27k
    unsigned int ct, mki_len, id;
486
1.27k
    int i, srtp_pref;
487
1.27k
    PACKET subpkt;
488
1.27k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
489
490
    /* Ignore this if we have no SRTP profiles */
491
1.27k
    if (SSL_get_srtp_profiles(ssl) == NULL)
492
1.27k
        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
1.77k
{
551
1.77k
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
552
1.77k
        s->ext.use_etm = 1;
553
554
1.77k
    return 1;
555
1.77k
}
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
1.49k
{
565
1.49k
#ifndef OPENSSL_NO_TLS1_3
566
1.49k
    PACKET psk_kex_modes;
567
1.49k
    unsigned int mode;
568
569
1.49k
    if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
570
1.46k
        || PACKET_remaining(&psk_kex_modes) == 0) {
571
44
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
572
44
        return 0;
573
44
    }
574
575
9.48k
    while (PACKET_get_1(&psk_kex_modes, &mode)) {
576
8.03k
        if (mode == TLSEXT_KEX_MODE_KE_DHE)
577
2.56k
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
578
5.47k
        else if (mode == TLSEXT_KEX_MODE_KE
579
1.86k
            && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
580
0
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
581
8.03k
    }
582
583
1.45k
    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
1.45k
#endif
598
599
1.45k
    return 1;
600
1.49k
}
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
1.54k
{
615
    /* Accept the key share group */
616
1.54k
    s->s3.group_id = ksgroup;
617
1.54k
    s->s3.group_id_candidate = ksgroup;
618
    /* Cache the selected group ID in the SSL_SESSION */
619
1.54k
    s->session->kex_group = ksgroup;
620
1.54k
    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
1.54k
    if (tls13_set_encoded_pub_key(s->s3.peer_tmp,
627
1.54k
            PACKET_data(encoded_pubkey),
628
1.54k
            PACKET_remaining(encoded_pubkey))
629
1.54k
        <= 0) {
630
37
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
631
37
        return 0;
632
37
    }
633
1.50k
    return 1;
634
1.54k
}
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
733
{
650
733
    PACKET encoded_pubkey;
651
733
    size_t key_share_pos = 0;
652
733
    size_t previous_key_share_pos = 0;
653
733
    unsigned int group_id = 0;
654
733
    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
733
    *keyshares_arr = OPENSSL_malloc_array(MAX_KEY_SHARES,
666
733
        sizeof(**keyshares_arr));
667
733
    if (*keyshares_arr == NULL) {
668
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
669
0
        goto failure;
670
0
    }
671
733
    *encoded_pubkey_arr = OPENSSL_malloc_array(MAX_KEY_SHARES,
672
733
        sizeof(**encoded_pubkey_arr));
673
733
    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
1.41k
    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
743
        if (!PACKET_get_net_2(key_share_list, &group_id)
686
742
            || !PACKET_get_length_prefixed_2(key_share_list, &encoded_pubkey)
687
728
            || PACKET_remaining(&encoded_pubkey) == 0) {
688
17
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
689
17
            goto failure;
690
17
        }
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
726
        if (s->s3.group_id != 0
697
37
            && (group_id != s->s3.group_id
698
28
                || PACKET_remaining(key_share_list) != 0)) {
699
10
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
700
10
            goto failure;
701
10
        }
702
703
        /*
704
         * Check if this share is in supported_groups sent from client
705
         * RFC 8446 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
716
        if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0, &key_share_pos)) {
712
7
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
713
7
            goto failure;
714
7
        }
715
716
709
        if (key_share_pos < previous_key_share_pos)
717
709
            OSSL_TRACE1(TLS, "key share group id %d is out of RFC 8446 order\n", group_id);
718
719
709
        previous_key_share_pos = key_share_pos;
720
721
709
        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
27
            if (!tls_accept_ksgroup(s, s->s3.group_id, &encoded_pubkey))
729
1
                goto failure; /* SSLfatal already called */
730
            /* We have selected a key share group via HRR, hence we're done here */
731
26
            return EXTRACTION_SUCCESS_HRR;
732
27
        }
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
682
        if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1, NULL)
739
577
            || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
740
577
            || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
741
577
                NULL, NULL)) {
742
            /* Share not suitable or not supported, check next share */
743
105
            continue;
744
105
        }
745
746
        /* Memorize this key share group ID and its encoded point */
747
577
        (*keyshares_arr)[*keyshares_cnt] = group_id;
748
577
        (*encoded_pubkey_arr)[(*keyshares_cnt)++] = encoded_pubkey;
749
577
    }
750
751
672
    return EXTRACTION_SUCCESS;
752
753
35
failure:
754
    /* Fatal error -> free any allocated memory and return 0 */
755
35
    OPENSSL_free(*keyshares_arr);
756
35
    OPENSSL_free(*encoded_pubkey_arr);
757
35
    return EXTRACTION_FAILURE;
758
733
}
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
6.90k
{
774
6.90k
    uint16_t current_group;
775
6.90k
    size_t group_idx = prio_num_groups;
776
6.90k
    size_t new_group_idx = 0;
777
778
6.90k
    *candidate_group_idx = 0;
779
6.90k
    *prio_group_idx = 0;
780
6.90k
    *selected_group = 0;
781
782
19.8k
    for (current_group = 0; current_group < candidate_num_groups; current_group++) {
783
12.9k
        if (!check_in_list(s, candidate_groups[current_group], prio_groups,
784
12.9k
                prio_num_groups, 1, &new_group_idx)
785
1.96k
            || !tls_group_allowed(s, candidate_groups[current_group],
786
1.96k
                SSL_SECOP_CURVE_SUPPORTED)
787
1.96k
            || !tls_valid_group(s, candidate_groups[current_group], TLS1_3_VERSION,
788
1.96k
                TLS1_3_VERSION, NULL, NULL))
789
            /* No overlap or group not suitable, check next group */
790
10.9k
            continue;
791
792
        /*
793
         * is the found new_group_idx earlier in the priority list than
794
         * initial or last group_idx?
795
         */
796
1.96k
        if (new_group_idx < group_idx) {
797
1.85k
            group_idx = new_group_idx;
798
1.85k
            *candidate_group_idx = current_group;
799
1.85k
            *prio_group_idx = (int)group_idx;
800
1.85k
            *selected_group = prio_groups[group_idx];
801
1.85k
        }
802
1.96k
    }
803
6.90k
}
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
2.10k
{
809
2.10k
#ifndef OPENSSL_NO_TLS1_3
810
2.10k
    PACKET key_share_list;
811
2.10k
    const uint16_t *clntgroups, *srvrgroups;
812
2.10k
    const size_t *srvrtuples;
813
2.10k
    uint16_t *first_group_in_tuple;
814
2.10k
    size_t clnt_num_groups, srvr_num_groups, srvr_num_tuples;
815
2.10k
    PACKET *encoded_pubkey_arr = NULL;
816
2.10k
    uint16_t *keyshares_arr = NULL;
817
2.10k
    size_t keyshares_cnt = 0;
818
    /* We conservatively assume that we did not find a suitable group */
819
2.10k
    uint16_t group_id_candidate = 0;
820
2.10k
    KS_EXTRACTION_RESULT ks_extraction_result;
821
2.10k
    size_t current_tuple;
822
2.10k
    int ret = 0;
823
824
2.10k
    s->s3.group_id_candidate = 0;
825
2.10k
    if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
826
0
        return 1;
827
828
    /* Sanity check */
829
2.10k
    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
2.10k
    if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
835
42
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
836
42
        return 0;
837
42
    }
838
839
    /* Get list of server supported groups and the group tuples */
840
2.06k
    tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);
841
2.06k
    tls1_get_group_tuples(s, &srvrtuples, &srvr_num_tuples);
842
    /* Get the clients list of supported groups. */
843
2.06k
    tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
844
845
2.06k
    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
4
        SSLfatal(s, SSL_AD_MISSING_EXTENSION,
852
4
            SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
853
4
        return 0;
854
4
    }
855
856
2.05k
    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
3
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
863
3
        return 0;
864
3
    }
865
866
    /* We parse the key share extension and memorize the entries (after some checks) */
867
2.05k
    ks_extraction_result = extract_keyshares(s,
868
2.05k
        &key_share_list,
869
2.05k
        clntgroups, clnt_num_groups,
870
2.05k
        srvrgroups, srvr_num_groups,
871
2.05k
        &keyshares_arr, &encoded_pubkey_arr,
872
2.05k
        &keyshares_cnt);
873
874
2.05k
    if (ks_extraction_result == EXTRACTION_FAILURE) /* Fatal error during tests */
875
131
        return 0; /* Memory already freed and SSLfatal already called */
876
1.92k
    if (ks_extraction_result == EXTRACTION_SUCCESS_HRR) /* Successful HRR */
877
72
        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
1.85k
    first_group_in_tuple = (uint16_t *)srvrgroups;
897
4.19k
    for (current_tuple = 0; current_tuple < srvr_num_tuples; current_tuple++) {
898
4.18k
        size_t number_of_groups_in_tuple = srvrtuples[current_tuple];
899
4.18k
        int prio_group_idx = 0, candidate_group_idx = 0;
900
901
        /* Server or client preference ? */
902
4.18k
        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
4.18k
        } else { /* We have client preference */
945
4.18k
            check_overlap(s,
946
4.18k
                keyshares_arr, keyshares_cnt,
947
4.18k
                first_group_in_tuple, number_of_groups_in_tuple,
948
4.18k
                &prio_group_idx, &candidate_group_idx,
949
4.18k
                &group_id_candidate);
950
4.18k
            if (group_id_candidate > 0) {
951
1.46k
                if (!tls_accept_ksgroup(s, group_id_candidate, &encoded_pubkey_arr[prio_group_idx]))
952
34
                    goto err;
953
1.43k
                goto end;
954
2.71k
            } else {
955
2.71k
                check_overlap(s,
956
2.71k
                    clntgroups, clnt_num_groups,
957
2.71k
                    first_group_in_tuple, number_of_groups_in_tuple,
958
2.71k
                    &prio_group_idx, &candidate_group_idx,
959
2.71k
                    &group_id_candidate);
960
2.71k
                if (group_id_candidate > 0) {
961
377
                    s->s3.group_id_candidate = group_id_candidate;
962
377
                    goto end;
963
2.34k
                } else {
964
2.34k
                    first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];
965
2.34k
                    continue;
966
2.34k
                }
967
2.71k
            }
968
4.18k
        }
969
4.18k
    }
970
971
1.89k
end:
972
1.89k
    ret = 1;
973
974
1.92k
err:
975
1.92k
    OPENSSL_free(keyshares_arr);
976
1.92k
    OPENSSL_free(encoded_pubkey_arr);
977
1.92k
    return ret;
978
979
0
#endif
980
981
0
    return 1;
982
1.89k
}
983
984
int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
985
    X509 *x, size_t chainidx)
986
100
{
987
100
#ifndef OPENSSL_NO_TLS1_3
988
100
    unsigned int format, version, key_share, group_id;
989
100
    EVP_MD_CTX *hctx;
990
100
    EVP_PKEY *pkey;
991
100
    PACKET cookie, raw, chhash, appcookie;
992
100
    WPACKET hrrpkt;
993
100
    const unsigned char *data, *mdin, *ciphdata;
994
100
    unsigned char hmac[SHA256_DIGEST_LENGTH];
995
100
    unsigned char hrr[MAX_HRR_SIZE];
996
100
    size_t rawlen, hmaclen, hrrlen, ciphlen;
997
100
    uint64_t tm, now;
998
100
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
999
100
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1000
1001
    /* Ignore any cookie if we're not set up to verify it */
1002
100
    if (sctx->verify_stateless_cookie_cb == NULL
1003
0
        || (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
1004
100
        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
19.2k
{
1201
19.2k
    PACKET supported_groups_list;
1202
1203
    /* Each group is 2 bytes and we must have at least 1. */
1204
19.2k
    if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
1205
19.0k
        || PACKET_remaining(&supported_groups_list) == 0
1206
19.0k
        || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
1207
216
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1208
216
        return 0;
1209
216
    }
1210
1211
19.0k
    if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
1212
18.9k
        OPENSSL_free(s->ext.peer_supportedgroups);
1213
18.9k
        s->ext.peer_supportedgroups = NULL;
1214
18.9k
        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
18.9k
        if (!tls1_save_u16(&supported_groups_list,
1223
18.9k
                &s->ext.peer_supportedgroups,
1224
18.9k
                &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
18.9k
    }
1229
1230
19.0k
    return 1;
1231
19.0k
}
1232
1233
int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1234
    X509 *x, size_t chainidx)
1235
4.47k
{
1236
    /* The extension must always be empty */
1237
4.47k
    if (PACKET_remaining(pkt) != 0) {
1238
15
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1239
15
        return 0;
1240
15
    }
1241
1242
4.46k
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
1243
0
        return 1;
1244
1245
4.46k
    s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1246
1247
4.46k
    return 1;
1248
4.46k
}
1249
1250
int tls_parse_ctos_early_data(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1251
    X509 *x, size_t chainidx)
1252
2.52k
{
1253
2.52k
    if (PACKET_remaining(pkt) != 0) {
1254
6
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1255
6
        return 0;
1256
6
    }
1257
1258
2.51k
    if (s->hello_retry_request != SSL_HRR_NONE) {
1259
7
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1260
7
        return 0;
1261
7
    }
1262
1263
2.50k
    return 1;
1264
2.51k
}
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
165
{
1297
165
    PACKET identities, binders, binder;
1298
165
    size_t binderoffset;
1299
165
    int hashsize;
1300
165
    SSL_SESSION *sess = NULL;
1301
165
    unsigned int id, i, ext = 0;
1302
165
    const EVP_MD *md = NULL;
1303
165
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1304
165
    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
165
    if ((s->ext.psk_kex_mode
1311
165
            & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE))
1312
165
        == 0)
1313
25
        return 1;
1314
1315
140
    if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
1316
3
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1317
3
        return 0;
1318
3
    }
1319
    /* There must always be at least one identity in the list */
1320
137
    if (PACKET_remaining(&identities) == 0) {
1321
1
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1322
1
        goto err;
1323
1
    }
1324
1325
136
    s->ext.ticket_expected = 0;
1326
278
    for (id = 0; PACKET_remaining(&identities) != 0 && id < MAX_PRE_SHARED_KEYS; id++) {
1327
224
        PACKET identity;
1328
224
        unsigned long ticket_agel;
1329
224
        size_t idlen;
1330
1331
224
        if (!PACKET_get_length_prefixed_2(&identities, &identity)
1332
194
            || !PACKET_get_net_4(&identities, &ticket_agel)) {
1333
34
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1334
34
            return 0;
1335
34
        }
1336
1337
190
        idlen = PACKET_remaining(&identity);
1338
190
        if (idlen == 0) {
1339
4
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1340
4
            return 0;
1341
4
        }
1342
186
        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
186
#ifndef OPENSSL_NO_PSK
1350
186
        if (sess == NULL
1351
186
            && 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
186
#endif /* OPENSSL_NO_PSK */
1398
1399
186
        if (sess != NULL) {
1400
            /* We found a PSK */
1401
0
            SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
1402
1403
0
            if (sesstmp == NULL) {
1404
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1405
0
                goto err;
1406
0
            }
1407
0
            SSL_SESSION_free(sess);
1408
0
            sess = sesstmp;
1409
1410
            /*
1411
             * We've just been told to use this session for this context so
1412
             * make sure the sid_ctx matches up.
1413
             */
1414
0
            memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
1415
0
            sess->sid_ctx_length = s->sid_ctx_length;
1416
0
            ext = 1;
1417
0
            if (id == 0)
1418
0
                s->ext.early_data_ok = 1;
1419
0
            s->ext.ticket_expected = 1;
1420
186
        } else {
1421
186
            OSSL_TIME t, age, expire;
1422
186
            int ret;
1423
1424
            /*
1425
             * If we are using anti-replay protection then we behave as if
1426
             * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
1427
             * is no point in using full stateless tickets.
1428
             */
1429
186
            if ((s->options & SSL_OP_NO_TICKET) != 0
1430
186
                || (s->max_early_data > 0
1431
0
                    && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))
1432
0
                ret = tls_get_stateful_ticket(s, &identity, &sess);
1433
186
            else
1434
186
                ret = tls_decrypt_ticket(s, PACKET_data(&identity),
1435
186
                    PACKET_remaining(&identity), NULL, 0,
1436
186
                    &sess);
1437
1438
186
            if (ret == SSL_TICKET_EMPTY) {
1439
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1440
0
                goto err;
1441
0
            }
1442
1443
186
            if (ret == SSL_TICKET_FATAL_ERR_MALLOC
1444
186
                || ret == SSL_TICKET_FATAL_ERR_OTHER) {
1445
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1446
0
                goto err;
1447
0
            }
1448
186
            if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)
1449
98
                continue;
1450
1451
            /* Check for replay */
1452
88
            if (s->max_early_data > 0
1453
0
                && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0
1454
0
                && !SSL_CTX_remove_session(s->session_ctx, sess)) {
1455
0
                SSL_SESSION_free(sess);
1456
0
                sess = NULL;
1457
0
                continue;
1458
0
            }
1459
1460
88
            age = ossl_time_subtract(ossl_ms2time(ticket_agel),
1461
88
                ossl_ms2time(sess->ext.tick_age_add));
1462
88
            t = ossl_time_subtract(ossl_time_now(), sess->time);
1463
1464
            /*
1465
             * Although internally we use OSS_TIME which has ns granularity,
1466
             * when SSL_SESSION structures are serialised/deserialised we use
1467
             * second granularity for the sess->time field. Therefore it could
1468
             * appear that the client's ticket age is longer than ours (our
1469
             * ticket age calculation should always be slightly longer than the
1470
             * client's due to the network latency). Therefore we add 1000ms to
1471
             * our age calculation to adjust for rounding errors.
1472
             */
1473
88
            expire = ossl_time_add(t, ossl_ms2time(1000));
1474
1475
88
            if (id == 0
1476
87
                && ossl_time_compare(sess->timeout, t) >= 0
1477
63
                && ossl_time_compare(age, expire) <= 0
1478
41
                && ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE),
1479
41
                       expire)
1480
41
                    >= 0) {
1481
                /*
1482
                 * Ticket age is within tolerance and not expired. We allow it
1483
                 * for early data
1484
                 */
1485
17
                s->ext.early_data_ok = 1;
1486
17
            }
1487
88
        }
1488
1489
88
        md = ssl_md(sctx, sess->cipher->algorithm2);
1490
88
        if (md == NULL) {
1491
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1492
0
            goto err;
1493
0
        }
1494
88
        if (!EVP_MD_is_a(md,
1495
88
                EVP_MD_get0_name(ssl_md(sctx,
1496
88
                    s->s3.tmp.new_cipher->algorithm2)))) {
1497
            /* The ciphersuite is not compatible with this session. */
1498
44
            SSL_SESSION_free(sess);
1499
44
            sess = NULL;
1500
44
            s->ext.early_data_ok = 0;
1501
            /*
1502
             * We fall back to a full handshake. The new session ticket will be
1503
             * issued to the client with the newly negotiated ciphersuite,
1504
             * allowing successful resumption on future connections.
1505
             */
1506
44
            s->ext.ticket_expected = 1;
1507
44
            continue;
1508
44
        }
1509
44
        break;
1510
88
    }
1511
1512
98
    if (sess == NULL) {
1513
54
        size_t j;
1514
1515
54
        for (j = 0; j < s->ssl_pkey_num && !ssl_has_cert(s, (int)j); j++)
1516
0
            ;
1517
54
        if (j < s->ssl_pkey_num) {
1518
            /* A certificate exists. Fallback to a full handshake */
1519
54
            return 1;
1520
54
        }
1521
        /*
1522
         * decrypt_error here to keep the alert the same as if the binder
1523
         * failed. See RFC8446 Appendix E.6. Note we make no attempt to do this
1524
         * in constant time compared to verifying the binder. None of this code
1525
         * is constant time anyway.
1526
         */
1527
54
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_EXTENSION);
1528
0
        goto err;
1529
54
    }
1530
1531
44
    binderoffset = PACKET_data(pkt) - PACKET_msg_start(pkt);
1532
44
    hashsize = EVP_MD_get_size(md);
1533
44
    if (hashsize <= 0)
1534
0
        goto err;
1535
1536
44
    if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
1537
22
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1538
22
        goto err;
1539
22
    }
1540
1541
38
    for (i = 0; i <= id; i++) {
1542
22
        if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
1543
6
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1544
6
            goto err;
1545
6
        }
1546
22
    }
1547
1548
16
    if (PACKET_remaining(&binder) != (size_t)hashsize) {
1549
3
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1550
3
        goto err;
1551
3
    }
1552
13
    if (tls_psk_do_binder(s, md, PACKET_msg_start(pkt), binderoffset,
1553
13
            PACKET_data(&binder), NULL, sess, 0, ext)
1554
13
        != 1) {
1555
        /* SSLfatal() already called */
1556
11
        goto err;
1557
11
    }
1558
1559
2
    s->ext.tick_identity = id;
1560
1561
2
    SSL_SESSION_free(s->session);
1562
2
    s->session = sess;
1563
2
    return 1;
1564
43
err:
1565
43
    SSL_SESSION_free(sess);
1566
43
    return 0;
1567
13
}
1568
1569
int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt,
1570
    ossl_unused unsigned int context,
1571
    ossl_unused X509 *x,
1572
    ossl_unused size_t chainidx)
1573
156
{
1574
156
    if (PACKET_remaining(pkt) != 0) {
1575
8
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1576
8
            SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
1577
8
        return 0;
1578
8
    }
1579
1580
148
    s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
1581
1582
148
    return 1;
1583
156
}
1584
1585
/*
1586
 * Add the server's renegotiation binding
1587
 */
1588
EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
1589
    unsigned int context, X509 *x,
1590
    size_t chainidx)
1591
25.1k
{
1592
25.1k
    if (!s->s3.send_connection_binding)
1593
17.4k
        return EXT_RETURN_NOT_SENT;
1594
1595
    /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
1596
7.73k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
1597
7.73k
        || !WPACKET_start_sub_packet_u16(pkt)
1598
7.73k
        || !WPACKET_start_sub_packet_u8(pkt)
1599
7.73k
        || !WPACKET_memcpy(pkt, s->s3.previous_client_finished,
1600
7.73k
            s->s3.previous_client_finished_len)
1601
7.73k
        || !WPACKET_memcpy(pkt, s->s3.previous_server_finished,
1602
7.73k
            s->s3.previous_server_finished_len)
1603
7.73k
        || !WPACKET_close(pkt)
1604
7.73k
        || !WPACKET_close(pkt)) {
1605
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1606
0
        return EXT_RETURN_FAIL;
1607
0
    }
1608
1609
7.73k
    return EXT_RETURN_SENT;
1610
7.73k
}
1611
1612
EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt,
1613
    unsigned int context, X509 *x,
1614
    size_t chainidx)
1615
27.9k
{
1616
27.9k
    if (s->servername_done != 1)
1617
27.9k
        return EXT_RETURN_NOT_SENT;
1618
1619
    /*
1620
     * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
1621
     * We just use the servername from the initial handshake.
1622
     */
1623
0
    if (s->hit && !SSL_CONNECTION_IS_TLS13(s))
1624
0
        return EXT_RETURN_NOT_SENT;
1625
1626
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1627
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1628
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1629
0
        return EXT_RETURN_FAIL;
1630
0
    }
1631
1632
0
    return EXT_RETURN_SENT;
1633
0
}
1634
1635
/* Add/include the server's max fragment len extension into ServerHello */
1636
EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
1637
    unsigned int context, X509 *x,
1638
    size_t chainidx)
1639
27.9k
{
1640
27.9k
    if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
1641
26.6k
        return EXT_RETURN_NOT_SENT;
1642
1643
    /*-
1644
     * 4 bytes for this extension type and extension length
1645
     * 1 byte for the Max Fragment Length code value.
1646
     */
1647
1.31k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
1648
1.31k
        || !WPACKET_start_sub_packet_u16(pkt)
1649
1.31k
        || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
1650
1.31k
        || !WPACKET_close(pkt)) {
1651
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1652
0
        return EXT_RETURN_FAIL;
1653
0
    }
1654
1655
1.31k
    return EXT_RETURN_SENT;
1656
1.31k
}
1657
1658
EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
1659
    unsigned int context, X509 *x,
1660
    size_t chainidx)
1661
4.61k
{
1662
4.61k
    unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1663
4.61k
    unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1664
4.61k
    int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1665
4.61k
    const unsigned char *plist;
1666
4.61k
    size_t plistlen;
1667
1668
    /*
1669
     * The extension is irrelevant unless we're negotiating an ECC
1670
     * ciphersuite at TLS 1.2 or below, and the peer sent a list.  This
1671
     * is the first point at which the chosen ciphersuite is known, so
1672
     * the RFC 4492/8422 section 5.1.2 check for the required
1673
     * 'uncompressed' codepoint also happens here.
1674
     */
1675
4.61k
    if (!using_ecc || s->ext.peer_ecpointformats == NULL)
1676
3.82k
        return EXT_RETURN_NOT_SENT;
1677
1678
788
    if (memchr(s->ext.peer_ecpointformats,
1679
788
            TLSEXT_ECPOINTFORMAT_uncompressed,
1680
788
            s->ext.peer_ecpointformats_len)
1681
788
        == NULL) {
1682
18
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1683
18
            SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1684
18
        return EXT_RETURN_FAIL;
1685
18
    }
1686
1687
770
    tls1_get_formatlist(s, &plist, &plistlen);
1688
770
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1689
770
        || !WPACKET_start_sub_packet_u16(pkt)
1690
770
        || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1691
770
        || !WPACKET_close(pkt)) {
1692
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1693
0
        return EXT_RETURN_FAIL;
1694
0
    }
1695
1696
770
    return EXT_RETURN_SENT;
1697
770
}
1698
1699
EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
1700
    unsigned int context, X509 *x,
1701
    size_t chainidx)
1702
27.9k
{
1703
27.9k
    const uint16_t *groups;
1704
27.9k
    size_t numgroups, i, first = 1;
1705
27.9k
    int version;
1706
1707
    /* s->s3.group_id is non zero if we accepted a key_share */
1708
27.9k
    if (s->s3.group_id == 0)
1709
25.1k
        return EXT_RETURN_NOT_SENT;
1710
1711
    /* Get our list of supported groups */
1712
2.80k
    tls1_get_supported_groups(s, &groups, &numgroups);
1713
2.80k
    if (numgroups == 0) {
1714
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1715
0
        return EXT_RETURN_FAIL;
1716
0
    }
1717
1718
    /* Copy group ID if supported */
1719
2.80k
    version = SSL_version(SSL_CONNECTION_GET_SSL(s));
1720
15.9k
    for (i = 0; i < numgroups; i++) {
1721
14.5k
        uint16_t group = groups[i];
1722
1723
14.5k
        if (tls_valid_group(s, group, version, version, NULL, NULL)
1724
14.5k
            && tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
1725
14.5k
            if (first) {
1726
                /*
1727
                 * Check if the client is already using our preferred group. If
1728
                 * so we don't need to add this extension
1729
                 */
1730
2.80k
                if (s->s3.group_id == group)
1731
1.36k
                    return EXT_RETURN_NOT_SENT;
1732
1733
                /* Add extension header */
1734
1.44k
                if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
1735
                    /* Sub-packet for supported_groups extension */
1736
1.44k
                    || !WPACKET_start_sub_packet_u16(pkt)
1737
1.44k
                    || !WPACKET_start_sub_packet_u16(pkt)) {
1738
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1739
0
                    return EXT_RETURN_FAIL;
1740
0
                }
1741
1742
1.44k
                first = 0;
1743
1.44k
            }
1744
13.1k
            if (!WPACKET_put_bytes_u16(pkt, group)) {
1745
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1746
0
                return EXT_RETURN_FAIL;
1747
0
            }
1748
13.1k
        }
1749
14.5k
    }
1750
1751
1.44k
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1752
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1753
0
        return EXT_RETURN_FAIL;
1754
0
    }
1755
1756
1.44k
    return EXT_RETURN_SENT;
1757
1.44k
}
1758
1759
EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
1760
    unsigned int context, X509 *x,
1761
    size_t chainidx)
1762
25.1k
{
1763
25.1k
    if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
1764
20.4k
        s->ext.ticket_expected = 0;
1765
20.4k
        return EXT_RETURN_NOT_SENT;
1766
20.4k
    }
1767
1768
4.71k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1769
4.71k
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1770
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1771
0
        return EXT_RETURN_FAIL;
1772
0
    }
1773
1774
4.71k
    return EXT_RETURN_SENT;
1775
4.71k
}
1776
1777
#ifndef OPENSSL_NO_OCSP
1778
EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt,
1779
    unsigned int context, X509 *x,
1780
    size_t chainidx)
1781
10.1k
{
1782
10.1k
    OCSP_RESPONSE *resp;
1783
1784
    /* We don't currently support this extension inside a CertificateRequest */
1785
10.1k
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)
1786
0
        return EXT_RETURN_NOT_SENT;
1787
1788
10.1k
    if (!s->ext.status_expected)
1789
10.1k
        return EXT_RETURN_NOT_SENT;
1790
1791
    /* Try to retrieve OCSP response for the actual certificate */
1792
0
    resp = ossl_get_ocsp_response(s, (int)chainidx);
1793
1794
    /* If no OCSP response was found the extension is not sent */
1795
0
    if (resp == NULL)
1796
0
        return EXT_RETURN_NOT_SENT;
1797
1798
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1799
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1800
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1801
0
        return EXT_RETURN_FAIL;
1802
0
    }
1803
1804
    /*
1805
     * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
1806
     * send back an empty extension, with the certificate status appearing as a
1807
     * separate message
1808
     */
1809
0
    if (SSL_CONNECTION_IS_TLS13(s)
1810
0
        && !tls_construct_cert_status_body(s, resp, pkt)) {
1811
        /* SSLfatal() already called */
1812
0
        return EXT_RETURN_FAIL;
1813
0
    }
1814
0
    if (!WPACKET_close(pkt)) {
1815
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1816
0
        return EXT_RETURN_FAIL;
1817
0
    }
1818
1819
0
    return EXT_RETURN_SENT;
1820
0
}
1821
#endif
1822
1823
#ifndef OPENSSL_NO_NEXTPROTONEG
1824
EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt,
1825
    unsigned int context, X509 *x,
1826
    size_t chainidx)
1827
25.1k
{
1828
25.1k
    const unsigned char *npa;
1829
25.1k
    unsigned int npalen;
1830
25.1k
    int ret;
1831
25.1k
    int npn_seen = s->s3.npn_seen;
1832
25.1k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1833
1834
25.1k
    s->s3.npn_seen = 0;
1835
25.1k
    if (!npn_seen || sctx->ext.npn_advertised_cb == NULL)
1836
25.1k
        return EXT_RETURN_NOT_SENT;
1837
1838
0
    ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa,
1839
0
        &npalen, sctx->ext.npn_advertised_cb_arg);
1840
0
    if (ret == SSL_TLSEXT_ERR_OK) {
1841
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1842
0
            || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1843
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1844
0
            return EXT_RETURN_FAIL;
1845
0
        }
1846
0
        s->s3.npn_seen = 1;
1847
0
        return EXT_RETURN_SENT;
1848
0
    }
1849
1850
0
    return EXT_RETURN_NOT_SENT;
1851
0
}
1852
#endif
1853
1854
EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context,
1855
    X509 *x, size_t chainidx)
1856
27.9k
{
1857
27.9k
    if (s->s3.alpn_selected == NULL)
1858
27.9k
        return EXT_RETURN_NOT_SENT;
1859
1860
0
    if (!WPACKET_put_bytes_u16(pkt,
1861
0
            TLSEXT_TYPE_application_layer_protocol_negotiation)
1862
0
        || !WPACKET_start_sub_packet_u16(pkt)
1863
0
        || !WPACKET_start_sub_packet_u16(pkt)
1864
0
        || !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,
1865
0
            s->s3.alpn_selected_len)
1866
0
        || !WPACKET_close(pkt)
1867
0
        || !WPACKET_close(pkt)) {
1868
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1869
0
        return EXT_RETURN_FAIL;
1870
0
    }
1871
1872
0
    return EXT_RETURN_SENT;
1873
0
}
1874
1875
#ifndef OPENSSL_NO_SRTP
1876
EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
1877
    unsigned int context, X509 *x,
1878
    size_t chainidx)
1879
27.9k
{
1880
27.9k
    if (s->srtp_profile == NULL)
1881
27.9k
        return EXT_RETURN_NOT_SENT;
1882
1883
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1884
0
        || !WPACKET_start_sub_packet_u16(pkt)
1885
0
        || !WPACKET_put_bytes_u16(pkt, 2)
1886
0
        || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1887
0
        || !WPACKET_put_bytes_u8(pkt, 0)
1888
0
        || !WPACKET_close(pkt)) {
1889
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1890
0
        return EXT_RETURN_FAIL;
1891
0
    }
1892
1893
0
    return EXT_RETURN_SENT;
1894
0
}
1895
#endif
1896
1897
EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt,
1898
    unsigned int context,
1899
    X509 *x, size_t chainidx)
1900
25.1k
{
1901
25.1k
    if (!s->ext.use_etm)
1902
23.7k
        return EXT_RETURN_NOT_SENT;
1903
1904
    /*
1905
     * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1906
     * for other cases too.
1907
     */
1908
1.37k
    if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD
1909
1.11k
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_RC4
1910
1.11k
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1911
1.11k
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12
1912
1.11k
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA
1913
1.11k
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) {
1914
261
        s->ext.use_etm = 0;
1915
261
        return EXT_RETURN_NOT_SENT;
1916
261
    }
1917
1918
1.11k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1919
1.11k
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1920
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1921
0
        return EXT_RETURN_FAIL;
1922
0
    }
1923
1924
1.11k
    return EXT_RETURN_SENT;
1925
1.11k
}
1926
1927
EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt,
1928
    unsigned int context,
1929
    X509 *x, size_t chainidx)
1930
25.1k
{
1931
25.1k
    if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
1932
21.4k
        return EXT_RETURN_NOT_SENT;
1933
1934
3.70k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1935
3.70k
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1936
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1937
0
        return EXT_RETURN_FAIL;
1938
0
    }
1939
1940
3.70k
    return EXT_RETURN_SENT;
1941
3.70k
}
1942
1943
EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
1944
    unsigned int context, X509 *x,
1945
    size_t chainidx)
1946
3.48k
{
1947
3.48k
    if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) {
1948
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1949
0
        return EXT_RETURN_FAIL;
1950
0
    }
1951
1952
3.48k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1953
3.48k
        || !WPACKET_start_sub_packet_u16(pkt)
1954
3.48k
        || !WPACKET_put_bytes_u16(pkt, s->version)
1955
3.48k
        || !WPACKET_close(pkt)) {
1956
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1957
0
        return EXT_RETURN_FAIL;
1958
0
    }
1959
1960
3.48k
    return EXT_RETURN_SENT;
1961
3.48k
}
1962
1963
EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt,
1964
    unsigned int context, X509 *x,
1965
    size_t chainidx)
1966
3.48k
{
1967
3.48k
#ifndef OPENSSL_NO_TLS1_3
1968
3.48k
    unsigned char *encoded_pubkey;
1969
3.48k
    size_t encoded_pubkey_len = 0;
1970
3.48k
    EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;
1971
3.48k
    const TLS_GROUP_INFO *ginf = NULL;
1972
1973
3.48k
    if (s->hello_retry_request == SSL_HRR_PENDING) {
1974
667
        if (ckey != NULL) {
1975
            /* Original key_share was acceptable so don't ask for another one */
1976
0
            return EXT_RETURN_NOT_SENT;
1977
0
        }
1978
667
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1979
667
            || !WPACKET_start_sub_packet_u16(pkt)
1980
667
            || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
1981
667
            || !WPACKET_close(pkt)) {
1982
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1983
0
            return EXT_RETURN_FAIL;
1984
0
        }
1985
1986
667
        return EXT_RETURN_SENT;
1987
667
    }
1988
1989
2.82k
    if (ckey == NULL) {
1990
        /* No key_share received from client - must be resuming */
1991
0
        if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
1992
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1993
0
            return EXT_RETURN_FAIL;
1994
0
        }
1995
0
        return EXT_RETURN_NOT_SENT;
1996
0
    }
1997
1998
2.82k
    if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {
1999
        /*
2000
         * PSK ('hit') and explicitly not doing DHE. If the client sent the
2001
         * DHE option, we take it by default, except if non-DHE would be
2002
         * preferred by config, but this case would have been handled in
2003
         * tls_parse_ctos_psk_kex_modes().
2004
         */
2005
0
        return EXT_RETURN_NOT_SENT;
2006
0
    }
2007
2008
2.82k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
2009
2.82k
        || !WPACKET_start_sub_packet_u16(pkt)
2010
2.82k
        || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) {
2011
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2012
0
        return EXT_RETURN_FAIL;
2013
0
    }
2014
2015
2.82k
    if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
2016
2.82k
             s->s3.group_id))
2017
2.82k
        == NULL) {
2018
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2019
0
        return EXT_RETURN_FAIL;
2020
0
    }
2021
2022
2.82k
    if (!ginf->is_kem) {
2023
        /* Regular KEX */
2024
2.79k
        skey = ssl_generate_pkey(s, ckey);
2025
2.79k
        if (skey == NULL) {
2026
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2027
0
            return EXT_RETURN_FAIL;
2028
0
        }
2029
2030
        /* Generate encoding of server key */
2031
2.79k
        encoded_pubkey_len = EVP_PKEY_get1_encoded_public_key(skey, &encoded_pubkey);
2032
2.79k
        if (encoded_pubkey_len == 0) {
2033
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
2034
0
            EVP_PKEY_free(skey);
2035
0
            return EXT_RETURN_FAIL;
2036
0
        }
2037
2038
2.79k
        if (!WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encoded_pubkey_len)
2039
2.79k
            || !WPACKET_close(pkt)) {
2040
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2041
0
            EVP_PKEY_free(skey);
2042
0
            OPENSSL_free(encoded_pubkey);
2043
0
            return EXT_RETURN_FAIL;
2044
0
        }
2045
2.79k
        OPENSSL_free(encoded_pubkey);
2046
2047
        /*
2048
         * This causes the crypto state to be updated based on the derived keys
2049
         */
2050
2.79k
        if (ssl_derive(s, skey, ckey, 1) == 0) {
2051
            /* SSLfatal() already called */
2052
16
            EVP_PKEY_free(skey);
2053
16
            return EXT_RETURN_FAIL;
2054
16
        }
2055
2.78k
        s->s3.tmp.pkey = skey;
2056
2.78k
    } else {
2057
        /* KEM mode */
2058
24
        unsigned char *ct = NULL;
2059
24
        size_t ctlen = 0;
2060
2061
        /*
2062
         * This does not update the crypto state.
2063
         *
2064
         * The generated pms is stored in `s->s3.tmp.pms` to be later used via
2065
         * ssl_gensecret().
2066
         */
2067
24
        if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) {
2068
            /* SSLfatal() already called */
2069
1
            return EXT_RETURN_FAIL;
2070
1
        }
2071
2072
23
        if (ctlen == 0) {
2073
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2074
0
            OPENSSL_free(ct);
2075
0
            return EXT_RETURN_FAIL;
2076
0
        }
2077
2078
23
        if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)
2079
23
            || !WPACKET_close(pkt)) {
2080
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2081
0
            OPENSSL_free(ct);
2082
0
            return EXT_RETURN_FAIL;
2083
0
        }
2084
23
        OPENSSL_free(ct);
2085
2086
        /*
2087
         * This causes the crypto state to be updated based on the generated pms
2088
         */
2089
23
        if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) {
2090
            /* SSLfatal() already called */
2091
0
            return EXT_RETURN_FAIL;
2092
0
        }
2093
23
    }
2094
2.80k
    s->s3.did_kex = 1;
2095
2.80k
    return EXT_RETURN_SENT;
2096
#else
2097
    return EXT_RETURN_FAIL;
2098
#endif
2099
2.82k
}
2100
2101
EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt,
2102
    unsigned int context,
2103
    X509 *x, size_t chainidx)
2104
667
{
2105
667
#ifndef OPENSSL_NO_TLS1_3
2106
667
    unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
2107
667
    unsigned char *hmac, *hmac2;
2108
667
    size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
2109
667
    EVP_MD_CTX *hctx;
2110
667
    EVP_PKEY *pkey;
2111
667
    int ret = EXT_RETURN_FAIL;
2112
667
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2113
667
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2114
667
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
2115
2116
667
    if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
2117
667
        return EXT_RETURN_NOT_SENT;
2118
2119
0
    if (sctx->gen_stateless_cookie_cb == NULL) {
2120
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);
2121
0
        return EXT_RETURN_FAIL;
2122
0
    }
2123
2124
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
2125
0
        || !WPACKET_start_sub_packet_u16(pkt)
2126
0
        || !WPACKET_start_sub_packet_u16(pkt)
2127
0
        || !WPACKET_get_total_written(pkt, &startlen)
2128
0
        || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
2129
0
        || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
2130
0
        || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
2131
0
        || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
2132
0
        || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,
2133
0
            &ciphlen)
2134
        /* Is there a key_share extension present in this HRR? */
2135
0
        || !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)
2136
0
        || !WPACKET_put_bytes_u64(pkt, time(NULL))
2137
0
        || !WPACKET_start_sub_packet_u16(pkt)
2138
0
        || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
2139
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2140
0
        return EXT_RETURN_FAIL;
2141
0
    }
2142
2143
    /*
2144
     * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
2145
     * on raw buffers, so we first reserve sufficient bytes (above) and then
2146
     * subsequently allocate them (below)
2147
     */
2148
0
    if (!ssl3_digest_cached_records(s, 0)
2149
0
        || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
2150
        /* SSLfatal() already called */
2151
0
        return EXT_RETURN_FAIL;
2152
0
    }
2153
2154
0
    if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
2155
0
        || !ossl_assert(hashval1 == hashval2)
2156
0
        || !WPACKET_close(pkt)
2157
0
        || !WPACKET_start_sub_packet_u8(pkt)
2158
0
        || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
2159
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2160
0
        return EXT_RETURN_FAIL;
2161
0
    }
2162
2163
    /* Generate the application cookie */
2164
0
    if (sctx->gen_stateless_cookie_cb(ussl, appcookie1,
2165
0
            &appcookielen)
2166
0
        == 0) {
2167
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
2168
0
        return EXT_RETURN_FAIL;
2169
0
    }
2170
2171
0
    if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
2172
0
        || !ossl_assert(appcookie1 == appcookie2)
2173
0
        || !WPACKET_close(pkt)
2174
0
        || !WPACKET_get_total_written(pkt, &totcookielen)
2175
0
        || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
2176
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2177
0
        return EXT_RETURN_FAIL;
2178
0
    }
2179
0
    hmaclen = SHA256_DIGEST_LENGTH;
2180
2181
0
    totcookielen -= startlen;
2182
0
    if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
2183
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2184
0
        return EXT_RETURN_FAIL;
2185
0
    }
2186
2187
    /* HMAC the cookie */
2188
0
    hctx = EVP_MD_CTX_create();
2189
0
    pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
2190
0
        sctx->propq,
2191
0
        s->session_ctx->ext.cookie_hmac_key,
2192
0
        sizeof(s->session_ctx->ext.cookie_hmac_key));
2193
0
    if (hctx == NULL || pkey == NULL) {
2194
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2195
0
        goto err;
2196
0
    }
2197
2198
0
    if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,
2199
0
            sctx->propq, pkey, NULL)
2200
0
            <= 0
2201
0
        || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
2202
0
               totcookielen)
2203
0
            <= 0) {
2204
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2205
0
        goto err;
2206
0
    }
2207
2208
0
    if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
2209
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2210
0
        goto err;
2211
0
    }
2212
2213
0
    if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
2214
0
        || !ossl_assert(hmac == hmac2)
2215
0
        || !ossl_assert(cookie == hmac - totcookielen)
2216
0
        || !WPACKET_close(pkt)
2217
0
        || !WPACKET_close(pkt)) {
2218
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2219
0
        goto err;
2220
0
    }
2221
2222
0
    ret = EXT_RETURN_SENT;
2223
2224
0
err:
2225
0
    EVP_MD_CTX_free(hctx);
2226
0
    EVP_PKEY_free(pkey);
2227
0
    return ret;
2228
#else
2229
    return EXT_RETURN_FAIL;
2230
#endif
2231
0
}
2232
2233
EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt,
2234
    unsigned int context, X509 *x,
2235
    size_t chainidx)
2236
25.1k
{
2237
25.1k
    const unsigned char cryptopro_ext[36] = {
2238
25.1k
        0xfd, 0xe8, /* 65000 */
2239
25.1k
        0x00, 0x20, /* 32 bytes length */
2240
25.1k
        0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
2241
25.1k
        0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
2242
25.1k
        0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
2243
25.1k
        0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
2244
25.1k
    };
2245
2246
25.1k
    if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x80
2247
25.1k
            && (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)
2248
0
        || (SSL_get_options(SSL_CONNECTION_GET_SSL(s))
2249
0
               & SSL_OP_CRYPTOPRO_TLSEXT_BUG)
2250
0
            == 0)
2251
25.1k
        return EXT_RETURN_NOT_SENT;
2252
2253
0
    if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
2254
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2255
0
        return EXT_RETURN_FAIL;
2256
0
    }
2257
2258
0
    return EXT_RETURN_SENT;
2259
0
}
2260
2261
EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt,
2262
    unsigned int context, X509 *x,
2263
    size_t chainidx)
2264
2.80k
{
2265
2.80k
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
2266
0
        if (s->max_early_data == 0)
2267
0
            return EXT_RETURN_NOT_SENT;
2268
2269
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2270
0
            || !WPACKET_start_sub_packet_u16(pkt)
2271
0
            || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
2272
0
            || !WPACKET_close(pkt)) {
2273
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2274
0
            return EXT_RETURN_FAIL;
2275
0
        }
2276
2277
0
        return EXT_RETURN_SENT;
2278
0
    }
2279
2280
2.80k
    if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
2281
2.80k
        return EXT_RETURN_NOT_SENT;
2282
2283
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2284
0
        || !WPACKET_start_sub_packet_u16(pkt)
2285
0
        || !WPACKET_close(pkt)) {
2286
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2287
0
        return EXT_RETURN_FAIL;
2288
0
    }
2289
2290
0
    return EXT_RETURN_SENT;
2291
0
}
2292
2293
EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt,
2294
    unsigned int context,
2295
    X509 *x, size_t chainidx)
2296
2.80k
{
2297
2.80k
    if (!s->hit)
2298
2.80k
        return EXT_RETURN_NOT_SENT;
2299
2300
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
2301
0
        || !WPACKET_start_sub_packet_u16(pkt)
2302
0
        || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)
2303
0
        || !WPACKET_close(pkt)) {
2304
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2305
0
        return EXT_RETURN_FAIL;
2306
0
    }
2307
2308
0
    return EXT_RETURN_SENT;
2309
0
}
2310
2311
EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2312
    unsigned int context,
2313
    X509 *x, size_t chainidx)
2314
25.7k
{
2315
25.7k
    if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR
2316
0
        && (send_certificate_request(sc)
2317
0
            || sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) {
2318
        /* Did not receive an acceptable cert type - and doing client auth */
2319
0
        SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2320
0
        return EXT_RETURN_FAIL;
2321
0
    }
2322
2323
25.7k
    if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) {
2324
25.7k
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2325
25.7k
        return EXT_RETURN_NOT_SENT;
2326
25.7k
    }
2327
2328
    /*
2329
     * Note: only supposed to send this if we are going to do a cert request,
2330
     * but TLSv1.3 could do a PHA request if the client supports it
2331
     */
2332
0
    if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED)
2333
0
        || sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2334
0
        || sc->client_cert_type == NULL) {
2335
        /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2336
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2337
0
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2338
0
        return EXT_RETURN_NOT_SENT;
2339
0
    }
2340
2341
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2342
0
        || !WPACKET_start_sub_packet_u16(pkt)
2343
0
        || !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type)
2344
0
        || !WPACKET_close(pkt)) {
2345
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2346
0
        return EXT_RETURN_FAIL;
2347
0
    }
2348
0
    return EXT_RETURN_SENT;
2349
0
}
2350
2351
/* One of |pref|, |other| is configured and the values are sanitized */
2352
static int reconcile_cert_type(const unsigned char *pref, size_t pref_len,
2353
    const unsigned char *other, size_t other_len,
2354
    uint8_t *chosen_cert_type)
2355
0
{
2356
0
    size_t i;
2357
2358
0
    for (i = 0; i < pref_len; i++) {
2359
0
        if (memchr(other, pref[i], other_len) != NULL) {
2360
0
            *chosen_cert_type = pref[i];
2361
0
            return OSSL_CERT_TYPE_CTOS_GOOD;
2362
0
        }
2363
0
    }
2364
0
    return OSSL_CERT_TYPE_CTOS_ERROR;
2365
0
}
2366
2367
int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2368
    unsigned int context,
2369
    X509 *x, size_t chainidx)
2370
281
{
2371
281
    PACKET supported_cert_types;
2372
281
    const unsigned char *data;
2373
281
    size_t len;
2374
2375
    /* Ignore the extension */
2376
281
    if (sc->client_cert_type == NULL) {
2377
281
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2378
281
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2379
281
        return 1;
2380
281
    }
2381
2382
0
    if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2383
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2384
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2385
0
        return 0;
2386
0
    }
2387
0
    if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2388
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2389
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2390
0
        return 0;
2391
0
    }
2392
0
    if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2393
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2394
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2395
0
        return 0;
2396
0
    }
2397
    /* client_cert_type: client (peer) has priority */
2398
0
    sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len,
2399
0
        sc->client_cert_type, sc->client_cert_type_len,
2400
0
        &sc->ext.client_cert_type);
2401
2402
    /* Ignore the error until sending - so we can check cert auth*/
2403
0
    return 1;
2404
0
}
2405
2406
EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2407
    unsigned int context,
2408
    X509 *x, size_t chainidx)
2409
25.7k
{
2410
25.7k
    if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) {
2411
25.7k
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2412
25.7k
        return EXT_RETURN_NOT_SENT;
2413
25.7k
    }
2414
0
    if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2415
0
        || sc->server_cert_type == NULL) {
2416
        /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2417
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2418
0
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2419
0
        return EXT_RETURN_NOT_SENT;
2420
0
    }
2421
2422
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2423
0
        || !WPACKET_start_sub_packet_u16(pkt)
2424
0
        || !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type)
2425
0
        || !WPACKET_close(pkt)) {
2426
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2427
0
        return EXT_RETURN_FAIL;
2428
0
    }
2429
0
    return EXT_RETURN_SENT;
2430
0
}
2431
2432
int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2433
    unsigned int context,
2434
    X509 *x, size_t chainidx)
2435
294
{
2436
294
    PACKET supported_cert_types;
2437
294
    const unsigned char *data;
2438
294
    size_t len;
2439
2440
    /* Ignore the extension */
2441
294
    if (sc->server_cert_type == NULL) {
2442
294
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2443
294
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2444
294
        return 1;
2445
294
    }
2446
2447
0
    if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2448
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2449
0
        return 0;
2450
0
    }
2451
2452
0
    if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2453
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2454
0
        return 0;
2455
0
    }
2456
0
    if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2457
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2458
0
        return 0;
2459
0
    }
2460
    /* server_cert_type: server (this) has priority */
2461
0
    sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len,
2462
0
        data, len,
2463
0
        &sc->ext.server_cert_type);
2464
0
    if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)
2465
0
        return 1;
2466
2467
    /* Did not receive an acceptable cert type */
2468
0
    SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2469
0
    return 0;
2470
0
}
2471
2472
#ifndef OPENSSL_NO_ECH
2473
/*
2474
 * ECH handling for edge cases (GREASE/inner) and errors.
2475
 * return 1 for good, 0 otherwise
2476
 *
2477
 * Real ECH handling (i.e. decryption) happens before, via
2478
 * ech_early_decrypt(), but if that failed (e.g. decryption
2479
 * failed, which may be down to GREASE) then we end up here,
2480
 * processing the ECH from the outer CH.
2481
 * Otherwise, we only expect to see an inner ECH with a fixed
2482
 * value here.
2483
 */
2484
int tls_parse_ctos_ech(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2485
    X509 *x, size_t chainidx)
2486
31
{
2487
31
    unsigned int echtype = 0;
2488
2489
31
    if (s->ext.ech.grease == OSSL_ECH_IS_GREASE) {
2490
        /* GREASE is fine */
2491
0
        return 1;
2492
0
    }
2493
31
    if (s->ext.ech.es == NULL) {
2494
        /* If not configured for ECH then we ignore it */
2495
31
        return 1;
2496
31
    }
2497
0
    if (s->ext.ech.attempted_type != TLSEXT_TYPE_ech) {
2498
        /* if/when new versions of ECH are added we'll update here */
2499
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2500
0
        return 0;
2501
0
    }
2502
    /*
2503
     * we only allow "inner" which is one octet, valued 0x01
2504
     * and only if we decrypted ok or are a backend
2505
     */
2506
0
    if (PACKET_get_1(pkt, &echtype) != 1
2507
0
        || PACKET_remaining(pkt) != 0) {
2508
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2509
0
        return 0;
2510
0
    }
2511
0
    if (echtype != OSSL_ECH_INNER_CH_TYPE) {
2512
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2513
0
        return 0;
2514
0
    }
2515
0
    s->ext.ech.inner_ech_seen_ok = 1;
2516
0
    if (s->ext.ech.success != 1 && s->ext.ech.backend != 1) {
2517
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2518
0
        return 0;
2519
0
    }
2520
    /* yay - we're ok with this */
2521
0
    OSSL_TRACE_BEGIN(TLS)
2522
0
    {
2523
0
        BIO_printf(trc_out, "ECH seen in inner as expected.\n");
2524
0
    }
2525
0
    OSSL_TRACE_END(TLS);
2526
0
    return 1;
2527
0
}
2528
2529
/*
2530
 * Answer an ECH, as needed
2531
 * return 1 for good, 0 otherwise
2532
 *
2533
 * Return most-recent ECH config for retry, as needed.
2534
 * If doing HRR we include the confirmation value, but
2535
 * for now, we'll just add the zeros - the real octets
2536
 * will be added later via ech_calc_ech_confirm() which
2537
 * is called when constructing the server hello.
2538
 */
2539
EXT_RETURN tls_construct_stoc_ech(SSL_CONNECTION *s, WPACKET *pkt,
2540
    unsigned int context, X509 *x,
2541
    size_t chainidx)
2542
637
{
2543
637
    unsigned char *rcfgs = NULL;
2544
637
    size_t rcfgslen = 0;
2545
637
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2546
2547
637
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2548
112
        && (s->ext.ech.success == 1 || s->ext.ech.backend == 1)
2549
3
        && s->ext.ech.attempted_type == TLSEXT_TYPE_ech) {
2550
3
        unsigned char eightzeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
2551
2552
3
        if (!WPACKET_put_bytes_u16(pkt, s->ext.ech.attempted_type)
2553
3
            || !WPACKET_sub_memcpy_u16(pkt, eightzeros, 8)) {
2554
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2555
0
            return 0;
2556
0
        }
2557
3
        OSSL_TRACE_BEGIN(TLS)
2558
0
        {
2559
0
            BIO_printf(trc_out, "set 8 zeros for ECH accept confirm in HRR\n");
2560
0
        }
2561
3
        OSSL_TRACE_END(TLS);
2562
3
        return EXT_RETURN_SENT;
2563
3
    }
2564
    /* GREASE or error => random confirmation in HRR case */
2565
634
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2566
109
        && s->ext.ech.attempted_type == TLSEXT_TYPE_ech
2567
0
        && s->ext.ech.attempted == 1) {
2568
0
        unsigned char randomconf[8];
2569
2570
0
        if (RAND_bytes_ex(sctx->libctx, randomconf, 8,
2571
0
                RAND_DRBG_STRENGTH)
2572
0
            <= 0) {
2573
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2574
0
            return 0;
2575
0
        }
2576
0
        if (!WPACKET_put_bytes_u16(pkt, s->ext.ech.attempted_type)
2577
0
            || !WPACKET_sub_memcpy_u16(pkt, randomconf, 8)) {
2578
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2579
0
            return 0;
2580
0
        }
2581
0
        OSSL_TRACE_BEGIN(TLS)
2582
0
        {
2583
0
            BIO_printf(trc_out, "set random for ECH acccpt confirm in HRR\n");
2584
0
        }
2585
0
        OSSL_TRACE_END(TLS);
2586
0
        return EXT_RETURN_SENT;
2587
0
    }
2588
    /* in other HRR circumstances: don't set */
2589
634
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
2590
109
        return EXT_RETURN_NOT_SENT;
2591
    /* If in some weird state we ignore and send nothing */
2592
525
    if (s->ext.ech.grease != OSSL_ECH_IS_GREASE
2593
0
        || s->ext.ech.attempted_type != TLSEXT_TYPE_ech)
2594
525
        return EXT_RETURN_NOT_SENT;
2595
    /*
2596
     * If the client GREASEd, or we think it did, return the
2597
     * most-recently loaded ECHConfigList, as the value of the
2598
     * extension. Most-recently loaded can be anywhere in the
2599
     * list, depending on changing or non-changing file names.
2600
     */
2601
0
    if (s->ext.ech.es == NULL) {
2602
0
        OSSL_TRACE_BEGIN(TLS)
2603
0
        {
2604
0
            BIO_printf(trc_out, "ECH - not sending ECHConfigList to client "
2605
0
                                "even though they GREASE'd as I've no loaded configs\n");
2606
0
        }
2607
0
        OSSL_TRACE_END(TLS);
2608
0
        return EXT_RETURN_NOT_SENT;
2609
0
    }
2610
0
    if (ossl_ech_get_retry_configs(s, &rcfgs, &rcfgslen) != 1) {
2611
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2612
0
        return 0;
2613
0
    }
2614
0
    if (rcfgslen == 0) {
2615
0
        OSSL_TRACE_BEGIN(TLS)
2616
0
        {
2617
0
            BIO_printf(trc_out, "ECH - not sending ECHConfigList to client "
2618
0
                                "even though they GREASE'd and I have configs but "
2619
0
                                "I've no configs set to be returned\n");
2620
0
        }
2621
0
        OSSL_TRACE_END(TLS);
2622
0
        OPENSSL_free(rcfgs);
2623
0
        return EXT_RETURN_NOT_SENT;
2624
0
    }
2625
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech)
2626
0
        || !WPACKET_start_sub_packet_u16(pkt)
2627
0
        || !WPACKET_sub_memcpy_u16(pkt, rcfgs, rcfgslen)
2628
0
        || !WPACKET_close(pkt)) {
2629
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2630
0
        OPENSSL_free(rcfgs);
2631
0
        return 0;
2632
0
    }
2633
0
    OPENSSL_free(rcfgs);
2634
0
    return EXT_RETURN_SENT;
2635
0
}
2636
#endif /* END OPENSSL_NO_ECH */