Coverage Report

Created: 2026-11-21 06:33

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