Coverage Report

Created: 2025-12-14 06:48

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