Coverage Report

Created: 2023-06-08 06:41

/src/openssl/ssl/statem/extensions_clnt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2023 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 "internal/cryptlib.h"
13
#include "statem_local.h"
14
15
EXT_RETURN tls_construct_ctos_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
16
                                          unsigned int context, X509 *x,
17
                                          size_t chainidx)
18
0
{
19
    /* Add RI if renegotiating */
20
0
    if (!s->renegotiate)
21
0
        return EXT_RETURN_NOT_SENT;
22
23
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
24
0
            || !WPACKET_start_sub_packet_u16(pkt)
25
0
            || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished,
26
0
                               s->s3.previous_client_finished_len)
27
0
            || !WPACKET_close(pkt)) {
28
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
29
0
        return EXT_RETURN_FAIL;
30
0
    }
31
32
0
    return EXT_RETURN_SENT;
33
0
}
34
35
EXT_RETURN tls_construct_ctos_server_name(SSL_CONNECTION *s, WPACKET *pkt,
36
                                          unsigned int context, X509 *x,
37
                                          size_t chainidx)
38
0
{
39
0
    if (s->ext.hostname == NULL)
40
0
        return EXT_RETURN_NOT_SENT;
41
42
    /* Add TLS extension servername to the Client Hello message */
43
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
44
               /* Sub-packet for server_name extension */
45
0
            || !WPACKET_start_sub_packet_u16(pkt)
46
               /* Sub-packet for servername list (always 1 hostname)*/
47
0
            || !WPACKET_start_sub_packet_u16(pkt)
48
0
            || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
49
0
            || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,
50
0
                                       strlen(s->ext.hostname))
51
0
            || !WPACKET_close(pkt)
52
0
            || !WPACKET_close(pkt)) {
53
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
54
0
        return EXT_RETURN_FAIL;
55
0
    }
56
57
0
    return EXT_RETURN_SENT;
58
0
}
59
60
/* Push a Max Fragment Len extension into ClientHello */
61
EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
62
                                             unsigned int context, X509 *x,
63
                                             size_t chainidx)
64
0
{
65
0
    if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED)
66
0
        return EXT_RETURN_NOT_SENT;
67
68
    /* Add Max Fragment Length extension if client enabled it. */
69
    /*-
70
     * 4 bytes for this extension type and extension length
71
     * 1 byte for the Max Fragment Length code value.
72
     */
73
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
74
            /* Sub-packet for Max Fragment Length extension (1 byte) */
75
0
            || !WPACKET_start_sub_packet_u16(pkt)
76
0
            || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode)
77
0
            || !WPACKET_close(pkt)) {
78
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
79
0
        return EXT_RETURN_FAIL;
80
0
    }
81
82
0
    return EXT_RETURN_SENT;
83
0
}
84
85
#ifndef OPENSSL_NO_SRP
86
EXT_RETURN tls_construct_ctos_srp(SSL_CONNECTION *s, WPACKET *pkt,
87
                                  unsigned int context,
88
                                  X509 *x, size_t chainidx)
89
0
{
90
    /* Add SRP username if there is one */
91
0
    if (s->srp_ctx.login == NULL)
92
0
        return EXT_RETURN_NOT_SENT;
93
94
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
95
               /* Sub-packet for SRP extension */
96
0
            || !WPACKET_start_sub_packet_u16(pkt)
97
0
            || !WPACKET_start_sub_packet_u8(pkt)
98
               /* login must not be zero...internal error if so */
99
0
            || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
100
0
            || !WPACKET_memcpy(pkt, s->srp_ctx.login,
101
0
                               strlen(s->srp_ctx.login))
102
0
            || !WPACKET_close(pkt)
103
0
            || !WPACKET_close(pkt)) {
104
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
105
0
        return EXT_RETURN_FAIL;
106
0
    }
107
108
0
    return EXT_RETURN_SENT;
109
0
}
110
#endif
111
112
static int use_ecc(SSL_CONNECTION *s, int min_version, int max_version)
113
0
{
114
0
    int i, end, ret = 0;
115
0
    unsigned long alg_k, alg_a;
116
0
    STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
117
0
    const uint16_t *pgroups = NULL;
118
0
    size_t num_groups, j;
119
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
120
121
    /* See if we support any ECC ciphersuites */
122
0
    if (s->version == SSL3_VERSION)
123
0
        return 0;
124
125
0
    cipher_stack = SSL_get1_supported_ciphers(ssl);
126
0
    end = sk_SSL_CIPHER_num(cipher_stack);
127
0
    for (i = 0; i < end; i++) {
128
0
        const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
129
130
0
        alg_k = c->algorithm_mkey;
131
0
        alg_a = c->algorithm_auth;
132
0
        if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
133
0
                || (alg_a & SSL_aECDSA)
134
0
                || c->min_tls >= TLS1_3_VERSION) {
135
0
            ret = 1;
136
0
            break;
137
0
        }
138
0
    }
139
0
    sk_SSL_CIPHER_free(cipher_stack);
140
0
    if (!ret)
141
0
        return 0;
142
143
    /* Check we have at least one EC supported group */
144
0
    tls1_get_supported_groups(s, &pgroups, &num_groups);
145
0
    for (j = 0; j < num_groups; j++) {
146
0
        uint16_t ctmp = pgroups[j];
147
148
0
        if (tls_valid_group(s, ctmp, min_version, max_version, 1, NULL)
149
0
                && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
150
0
            return 1;
151
0
    }
152
153
0
    return 0;
154
0
}
155
156
EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
157
                                            unsigned int context, X509 *x,
158
                                            size_t chainidx)
159
0
{
160
0
    const unsigned char *pformats;
161
0
    size_t num_formats;
162
0
    int reason, min_version, max_version;
163
164
0
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
165
0
    if (reason != 0) {
166
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
167
0
        return EXT_RETURN_FAIL;
168
0
    }
169
0
    if (!use_ecc(s, min_version, max_version))
170
0
        return EXT_RETURN_NOT_SENT;
171
172
    /* Add TLS extension ECPointFormats to the ClientHello message */
173
0
    tls1_get_formatlist(s, &pformats, &num_formats);
174
175
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
176
               /* Sub-packet for formats extension */
177
0
            || !WPACKET_start_sub_packet_u16(pkt)
178
0
            || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
179
0
            || !WPACKET_close(pkt)) {
180
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
181
0
        return EXT_RETURN_FAIL;
182
0
    }
183
184
0
    return EXT_RETURN_SENT;
185
0
}
186
187
EXT_RETURN tls_construct_ctos_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
188
                                               unsigned int context, X509 *x,
189
                                               size_t chainidx)
190
0
{
191
0
    const uint16_t *pgroups = NULL;
192
0
    size_t num_groups = 0, i, tls13added = 0, added = 0;
193
0
    int min_version, max_version, reason;
194
195
0
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
196
0
    if (reason != 0) {
197
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
198
0
        return EXT_RETURN_FAIL;
199
0
    }
200
201
    /*
202
     * We only support EC groups in TLSv1.2 or below, and in DTLS. Therefore
203
     * if we don't have EC support then we don't send this extension.
204
     */
205
0
    if (!use_ecc(s, min_version, max_version)
206
0
            && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION))
207
0
        return EXT_RETURN_NOT_SENT;
208
209
    /*
210
     * Add TLS extension supported_groups to the ClientHello message
211
     */
212
0
    tls1_get_supported_groups(s, &pgroups, &num_groups);
213
214
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
215
               /* Sub-packet for supported_groups extension */
216
0
            || !WPACKET_start_sub_packet_u16(pkt)
217
0
            || !WPACKET_start_sub_packet_u16(pkt)
218
0
            || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) {
219
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
220
0
        return EXT_RETURN_FAIL;
221
0
    }
222
    /* Copy group ID if supported */
223
0
    for (i = 0; i < num_groups; i++) {
224
0
        uint16_t ctmp = pgroups[i];
225
0
        int okfortls13;
226
227
0
        if (tls_valid_group(s, ctmp, min_version, max_version, 0, &okfortls13)
228
0
                && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) {
229
0
            if (!WPACKET_put_bytes_u16(pkt, ctmp)) {
230
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
231
0
                return EXT_RETURN_FAIL;
232
0
            }
233
0
            if (okfortls13 && max_version == TLS1_3_VERSION)
234
0
                tls13added++;
235
0
            added++;
236
0
        }
237
0
    }
238
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
239
0
        if (added == 0)
240
0
            SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
241
0
                          "No groups enabled for max supported SSL/TLS version");
242
0
        else
243
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
244
0
        return EXT_RETURN_FAIL;
245
0
    }
246
247
0
    if (tls13added == 0 && max_version == TLS1_3_VERSION) {
248
0
        SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
249
0
                      "No groups enabled for max supported SSL/TLS version");
250
0
        return EXT_RETURN_FAIL;
251
0
    }
252
253
0
    return EXT_RETURN_SENT;
254
0
}
255
256
EXT_RETURN tls_construct_ctos_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
257
                                             unsigned int context, X509 *x,
258
                                             size_t chainidx)
259
0
{
260
0
    size_t ticklen;
261
262
0
    if (!tls_use_ticket(s))
263
0
        return EXT_RETURN_NOT_SENT;
264
265
0
    if (!s->new_session && s->session != NULL
266
0
            && s->session->ext.tick != NULL
267
0
            && s->session->ssl_version != TLS1_3_VERSION) {
268
0
        ticklen = s->session->ext.ticklen;
269
0
    } else if (s->session && s->ext.session_ticket != NULL
270
0
               && s->ext.session_ticket->data != NULL) {
271
0
        ticklen = s->ext.session_ticket->length;
272
0
        s->session->ext.tick = OPENSSL_malloc(ticklen);
273
0
        if (s->session->ext.tick == NULL) {
274
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
275
0
            return EXT_RETURN_FAIL;
276
0
        }
277
0
        memcpy(s->session->ext.tick,
278
0
               s->ext.session_ticket->data, ticklen);
279
0
        s->session->ext.ticklen = ticklen;
280
0
    } else {
281
0
        ticklen = 0;
282
0
    }
283
284
0
    if (ticklen == 0 && s->ext.session_ticket != NULL &&
285
0
            s->ext.session_ticket->data == NULL)
286
0
        return EXT_RETURN_NOT_SENT;
287
288
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
289
0
            || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
290
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
291
0
        return EXT_RETURN_FAIL;
292
0
    }
293
294
0
    return EXT_RETURN_SENT;
295
0
}
296
297
EXT_RETURN tls_construct_ctos_sig_algs(SSL_CONNECTION *s, WPACKET *pkt,
298
                                       unsigned int context, X509 *x,
299
                                       size_t chainidx)
300
0
{
301
0
    size_t salglen;
302
0
    const uint16_t *salg;
303
304
0
    if (!SSL_CLIENT_USE_SIGALGS(s))
305
0
        return EXT_RETURN_NOT_SENT;
306
307
0
    salglen = tls12_get_psigalgs(s, 1, &salg);
308
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
309
               /* Sub-packet for sig-algs extension */
310
0
            || !WPACKET_start_sub_packet_u16(pkt)
311
               /* Sub-packet for the actual list */
312
0
            || !WPACKET_start_sub_packet_u16(pkt)
313
0
            || !tls12_copy_sigalgs(s, pkt, salg, salglen)
314
0
            || !WPACKET_close(pkt)
315
0
            || !WPACKET_close(pkt)) {
316
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
317
0
        return EXT_RETURN_FAIL;
318
0
    }
319
320
0
    return EXT_RETURN_SENT;
321
0
}
322
323
#ifndef OPENSSL_NO_OCSP
324
EXT_RETURN tls_construct_ctos_status_request(SSL_CONNECTION *s, WPACKET *pkt,
325
                                             unsigned int context, X509 *x,
326
                                             size_t chainidx)
327
0
{
328
0
    int i;
329
330
    /* This extension isn't defined for client Certificates */
331
0
    if (x != NULL)
332
0
        return EXT_RETURN_NOT_SENT;
333
334
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
335
0
        return EXT_RETURN_NOT_SENT;
336
337
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
338
               /* Sub-packet for status request extension */
339
0
            || !WPACKET_start_sub_packet_u16(pkt)
340
0
            || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
341
               /* Sub-packet for the ids */
342
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
343
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
344
0
        return EXT_RETURN_FAIL;
345
0
    }
346
0
    for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
347
0
        unsigned char *idbytes;
348
0
        OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
349
0
        int idlen = i2d_OCSP_RESPID(id, NULL);
350
351
0
        if (idlen <= 0
352
                   /* Sub-packet for an individual id */
353
0
                || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
354
0
                || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
355
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
356
0
            return EXT_RETURN_FAIL;
357
0
        }
358
0
    }
359
0
    if (!WPACKET_close(pkt)
360
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
361
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
362
0
        return EXT_RETURN_FAIL;
363
0
    }
364
0
    if (s->ext.ocsp.exts) {
365
0
        unsigned char *extbytes;
366
0
        int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
367
368
0
        if (extlen < 0) {
369
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
370
0
            return EXT_RETURN_FAIL;
371
0
        }
372
0
        if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
373
0
                || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
374
0
                   != extlen) {
375
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
376
0
            return EXT_RETURN_FAIL;
377
0
       }
378
0
    }
379
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
380
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
381
0
        return EXT_RETURN_FAIL;
382
0
    }
383
384
0
    return EXT_RETURN_SENT;
385
0
}
386
#endif
387
388
#ifndef OPENSSL_NO_NEXTPROTONEG
389
EXT_RETURN tls_construct_ctos_npn(SSL_CONNECTION *s, WPACKET *pkt,
390
                                  unsigned int context,
391
                                  X509 *x, size_t chainidx)
392
0
{
393
0
    if (SSL_CONNECTION_GET_CTX(s)->ext.npn_select_cb == NULL
394
0
        || !SSL_IS_FIRST_HANDSHAKE(s))
395
0
        return EXT_RETURN_NOT_SENT;
396
397
    /*
398
     * The client advertises an empty extension to indicate its support
399
     * for Next Protocol Negotiation
400
     */
401
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
402
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
403
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
404
0
        return EXT_RETURN_FAIL;
405
0
    }
406
407
0
    return EXT_RETURN_SENT;
408
0
}
409
#endif
410
411
EXT_RETURN tls_construct_ctos_alpn(SSL_CONNECTION *s, WPACKET *pkt,
412
                                   unsigned int context,
413
                                   X509 *x, size_t chainidx)
414
0
{
415
0
    s->s3.alpn_sent = 0;
416
417
0
    if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
418
0
        return EXT_RETURN_NOT_SENT;
419
420
0
    if (!WPACKET_put_bytes_u16(pkt,
421
0
                TLSEXT_TYPE_application_layer_protocol_negotiation)
422
               /* Sub-packet ALPN extension */
423
0
            || !WPACKET_start_sub_packet_u16(pkt)
424
0
            || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
425
0
            || !WPACKET_close(pkt)) {
426
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
427
0
        return EXT_RETURN_FAIL;
428
0
    }
429
0
    s->s3.alpn_sent = 1;
430
431
0
    return EXT_RETURN_SENT;
432
0
}
433
434
435
#ifndef OPENSSL_NO_SRTP
436
EXT_RETURN tls_construct_ctos_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
437
                                       unsigned int context, X509 *x,
438
                                       size_t chainidx)
439
0
{
440
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
441
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(ssl);
442
0
    int i, end;
443
444
0
    if (clnt == NULL)
445
0
        return EXT_RETURN_NOT_SENT;
446
447
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
448
               /* Sub-packet for SRTP extension */
449
0
            || !WPACKET_start_sub_packet_u16(pkt)
450
               /* Sub-packet for the protection profile list */
451
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
452
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
453
0
        return EXT_RETURN_FAIL;
454
0
    }
455
456
0
    end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
457
0
    for (i = 0; i < end; i++) {
458
0
        const SRTP_PROTECTION_PROFILE *prof =
459
0
            sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
460
461
0
        if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
462
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
463
0
            return EXT_RETURN_FAIL;
464
0
        }
465
0
    }
466
0
    if (!WPACKET_close(pkt)
467
               /* Add an empty use_mki value */
468
0
            || !WPACKET_put_bytes_u8(pkt, 0)
469
0
            || !WPACKET_close(pkt)) {
470
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
471
0
        return EXT_RETURN_FAIL;
472
0
    }
473
474
0
    return EXT_RETURN_SENT;
475
0
}
476
#endif
477
478
EXT_RETURN tls_construct_ctos_etm(SSL_CONNECTION *s, WPACKET *pkt,
479
                                  unsigned int context,
480
                                  X509 *x, size_t chainidx)
481
0
{
482
0
    if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
483
0
        return EXT_RETURN_NOT_SENT;
484
485
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
486
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
487
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
488
0
        return EXT_RETURN_FAIL;
489
0
    }
490
491
0
    return EXT_RETURN_SENT;
492
0
}
493
494
#ifndef OPENSSL_NO_CT
495
EXT_RETURN tls_construct_ctos_sct(SSL_CONNECTION *s, WPACKET *pkt,
496
                                  unsigned int context,
497
                                  X509 *x, size_t chainidx)
498
0
{
499
0
    if (s->ct_validation_callback == NULL)
500
0
        return EXT_RETURN_NOT_SENT;
501
502
    /* Not defined for client Certificates */
503
0
    if (x != NULL)
504
0
        return EXT_RETURN_NOT_SENT;
505
506
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
507
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
508
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
509
0
        return EXT_RETURN_FAIL;
510
0
    }
511
512
0
    return EXT_RETURN_SENT;
513
0
}
514
#endif
515
516
EXT_RETURN tls_construct_ctos_ems(SSL_CONNECTION *s, WPACKET *pkt,
517
                                  unsigned int context,
518
                                  X509 *x, size_t chainidx)
519
0
{
520
0
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
521
0
        return EXT_RETURN_NOT_SENT;
522
523
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
524
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
525
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
526
0
        return EXT_RETURN_FAIL;
527
0
    }
528
529
0
    return EXT_RETURN_SENT;
530
0
}
531
532
EXT_RETURN tls_construct_ctos_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
533
                                                 unsigned int context, X509 *x,
534
                                                 size_t chainidx)
535
0
{
536
0
    int currv, min_version, max_version, reason;
537
538
0
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
539
0
    if (reason != 0) {
540
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
541
0
        return EXT_RETURN_FAIL;
542
0
    }
543
544
    /*
545
     * Don't include this if we can't negotiate TLSv1.3. We can do a straight
546
     * comparison here because we will never be called in DTLS.
547
     */
548
0
    if (max_version < TLS1_3_VERSION)
549
0
        return EXT_RETURN_NOT_SENT;
550
551
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
552
0
            || !WPACKET_start_sub_packet_u16(pkt)
553
0
            || !WPACKET_start_sub_packet_u8(pkt)) {
554
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
555
0
        return EXT_RETURN_FAIL;
556
0
    }
557
558
0
    for (currv = max_version; currv >= min_version; currv--) {
559
0
        if (!WPACKET_put_bytes_u16(pkt, currv)) {
560
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
561
0
            return EXT_RETURN_FAIL;
562
0
        }
563
0
    }
564
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
565
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
566
0
        return EXT_RETURN_FAIL;
567
0
    }
568
569
0
    return EXT_RETURN_SENT;
570
0
}
571
572
/*
573
 * Construct a psk_kex_modes extension.
574
 */
575
EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL_CONNECTION *s, WPACKET *pkt,
576
                                            unsigned int context, X509 *x,
577
                                            size_t chainidx)
578
0
{
579
0
#ifndef OPENSSL_NO_TLS1_3
580
0
    int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
581
582
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
583
0
            || !WPACKET_start_sub_packet_u16(pkt)
584
0
            || !WPACKET_start_sub_packet_u8(pkt)
585
0
            || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
586
0
            || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
587
0
            || !WPACKET_close(pkt)
588
0
            || !WPACKET_close(pkt)) {
589
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
590
0
        return EXT_RETURN_FAIL;
591
0
    }
592
593
0
    s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
594
0
    if (nodhe)
595
0
        s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
596
0
#endif
597
598
0
    return EXT_RETURN_SENT;
599
0
}
600
601
#ifndef OPENSSL_NO_TLS1_3
602
static int add_key_share(SSL_CONNECTION *s, WPACKET *pkt, unsigned int curve_id)
603
0
{
604
0
    unsigned char *encoded_point = NULL;
605
0
    EVP_PKEY *key_share_key = NULL;
606
0
    size_t encodedlen;
607
608
0
    if (s->s3.tmp.pkey != NULL) {
609
0
        if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) {
610
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
611
0
            return 0;
612
0
        }
613
        /*
614
         * Could happen if we got an HRR that wasn't requesting a new key_share
615
         */
616
0
        key_share_key = s->s3.tmp.pkey;
617
0
    } else {
618
0
        key_share_key = ssl_generate_pkey_group(s, curve_id);
619
0
        if (key_share_key == NULL) {
620
            /* SSLfatal() already called */
621
0
            return 0;
622
0
        }
623
0
    }
624
625
    /* Encode the public key. */
626
0
    encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key,
627
0
                                                  &encoded_point);
628
0
    if (encodedlen == 0) {
629
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
630
0
        goto err;
631
0
    }
632
633
    /* Create KeyShareEntry */
634
0
    if (!WPACKET_put_bytes_u16(pkt, curve_id)
635
0
            || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) {
636
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
637
0
        goto err;
638
0
    }
639
640
    /*
641
     * When changing to send more than one key_share we're
642
     * going to need to be able to save more than one EVP_PKEY. For now
643
     * we reuse the existing tmp.pkey
644
     */
645
0
    s->s3.tmp.pkey = key_share_key;
646
0
    s->s3.group_id = curve_id;
647
0
    OPENSSL_free(encoded_point);
648
649
0
    return 1;
650
0
 err:
651
0
    if (s->s3.tmp.pkey == NULL)
652
0
        EVP_PKEY_free(key_share_key);
653
0
    OPENSSL_free(encoded_point);
654
0
    return 0;
655
0
}
656
#endif
657
658
EXT_RETURN tls_construct_ctos_key_share(SSL_CONNECTION *s, WPACKET *pkt,
659
                                        unsigned int context, X509 *x,
660
                                        size_t chainidx)
661
0
{
662
0
#ifndef OPENSSL_NO_TLS1_3
663
0
    size_t i, num_groups = 0;
664
0
    const uint16_t *pgroups = NULL;
665
0
    uint16_t curve_id = 0;
666
667
    /* key_share extension */
668
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
669
               /* Extension data sub-packet */
670
0
            || !WPACKET_start_sub_packet_u16(pkt)
671
               /* KeyShare list sub-packet */
672
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
673
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
674
0
        return EXT_RETURN_FAIL;
675
0
    }
676
677
0
    tls1_get_supported_groups(s, &pgroups, &num_groups);
678
679
    /*
680
     * Make the number of key_shares sent configurable. For
681
     * now, we just send one
682
     */
683
0
    if (s->s3.group_id != 0) {
684
0
        curve_id = s->s3.group_id;
685
0
    } else {
686
0
        for (i = 0; i < num_groups; i++) {
687
0
            if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
688
0
                continue;
689
690
0
            if (!tls_valid_group(s, pgroups[i], TLS1_3_VERSION, TLS1_3_VERSION,
691
0
                                 0, NULL))
692
0
                continue;
693
694
0
            curve_id = pgroups[i];
695
0
            break;
696
0
        }
697
0
    }
698
699
0
    if (curve_id == 0) {
700
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);
701
0
        return EXT_RETURN_FAIL;
702
0
    }
703
704
0
    if (!add_key_share(s, pkt, curve_id)) {
705
        /* SSLfatal() already called */
706
0
        return EXT_RETURN_FAIL;
707
0
    }
708
709
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
710
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
711
0
        return EXT_RETURN_FAIL;
712
0
    }
713
0
    return EXT_RETURN_SENT;
714
#else
715
    return EXT_RETURN_NOT_SENT;
716
#endif
717
0
}
718
719
EXT_RETURN tls_construct_ctos_cookie(SSL_CONNECTION *s, WPACKET *pkt,
720
                                     unsigned int context,
721
                                     X509 *x, size_t chainidx)
722
0
{
723
0
    EXT_RETURN ret = EXT_RETURN_FAIL;
724
725
    /* Should only be set if we've had an HRR */
726
0
    if (s->ext.tls13_cookie_len == 0)
727
0
        return EXT_RETURN_NOT_SENT;
728
729
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
730
               /* Extension data sub-packet */
731
0
            || !WPACKET_start_sub_packet_u16(pkt)
732
0
            || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
733
0
                                       s->ext.tls13_cookie_len)
734
0
            || !WPACKET_close(pkt)) {
735
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
736
0
        goto end;
737
0
    }
738
739
0
    ret = EXT_RETURN_SENT;
740
0
 end:
741
0
    OPENSSL_free(s->ext.tls13_cookie);
742
0
    s->ext.tls13_cookie = NULL;
743
0
    s->ext.tls13_cookie_len = 0;
744
745
0
    return ret;
746
0
}
747
748
EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt,
749
                                         unsigned int context, X509 *x,
750
                                         size_t chainidx)
751
0
{
752
0
#ifndef OPENSSL_NO_PSK
753
0
    char identity[PSK_MAX_IDENTITY_LEN + 1];
754
0
#endif  /* OPENSSL_NO_PSK */
755
0
    const unsigned char *id = NULL;
756
0
    size_t idlen = 0;
757
0
    SSL_SESSION *psksess = NULL;
758
0
    SSL_SESSION *edsess = NULL;
759
0
    const EVP_MD *handmd = NULL;
760
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
761
762
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
763
0
        handmd = ssl_handshake_md(s);
764
765
0
    if (s->psk_use_session_cb != NULL
766
0
            && (!s->psk_use_session_cb(ssl, handmd, &id, &idlen, &psksess)
767
0
                || (psksess != NULL
768
0
                    && psksess->ssl_version != TLS1_3_VERSION))) {
769
0
        SSL_SESSION_free(psksess);
770
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
771
0
        return EXT_RETURN_FAIL;
772
0
    }
773
774
0
#ifndef OPENSSL_NO_PSK
775
0
    if (psksess == NULL && s->psk_client_callback != NULL) {
776
0
        unsigned char psk[PSK_MAX_PSK_LEN];
777
0
        size_t psklen = 0;
778
779
0
        memset(identity, 0, sizeof(identity));
780
0
        psklen = s->psk_client_callback(ssl, NULL,
781
0
                                        identity, sizeof(identity) - 1,
782
0
                                        psk, sizeof(psk));
783
784
0
        if (psklen > PSK_MAX_PSK_LEN) {
785
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
786
0
            return EXT_RETURN_FAIL;
787
0
        } else if (psklen > 0) {
788
0
            const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
789
0
            const SSL_CIPHER *cipher;
790
791
0
            idlen = strlen(identity);
792
0
            if (idlen > PSK_MAX_IDENTITY_LEN) {
793
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
794
0
                return EXT_RETURN_FAIL;
795
0
            }
796
0
            id = (unsigned char *)identity;
797
798
            /*
799
             * We found a PSK using an old style callback. We don't know
800
             * the digest so we default to SHA256 as per the TLSv1.3 spec
801
             */
802
0
            cipher = SSL_CIPHER_find(ssl, tls13_aes128gcmsha256_id);
803
0
            if (cipher == NULL) {
804
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
805
0
                return EXT_RETURN_FAIL;
806
0
            }
807
808
0
            psksess = SSL_SESSION_new();
809
0
            if (psksess == NULL
810
0
                    || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
811
0
                    || !SSL_SESSION_set_cipher(psksess, cipher)
812
0
                    || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
813
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
814
0
                OPENSSL_cleanse(psk, psklen);
815
0
                return EXT_RETURN_FAIL;
816
0
            }
817
0
            OPENSSL_cleanse(psk, psklen);
818
0
        }
819
0
    }
820
0
#endif  /* OPENSSL_NO_PSK */
821
822
0
    SSL_SESSION_free(s->psksession);
823
0
    s->psksession = psksess;
824
0
    if (psksess != NULL) {
825
0
        OPENSSL_free(s->psksession_id);
826
0
        s->psksession_id = OPENSSL_memdup(id, idlen);
827
0
        if (s->psksession_id == NULL) {
828
0
            s->psksession_id_len = 0;
829
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
830
0
            return EXT_RETURN_FAIL;
831
0
        }
832
0
        s->psksession_id_len = idlen;
833
0
    }
834
835
0
    if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
836
0
            || (s->session->ext.max_early_data == 0
837
0
                && (psksess == NULL || psksess->ext.max_early_data == 0))) {
838
0
        s->max_early_data = 0;
839
0
        return EXT_RETURN_NOT_SENT;
840
0
    }
841
0
    edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;
842
0
    s->max_early_data = edsess->ext.max_early_data;
843
844
0
    if (edsess->ext.hostname != NULL) {
845
0
        if (s->ext.hostname == NULL
846
0
                || (s->ext.hostname != NULL
847
0
                    && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
848
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
849
0
                     SSL_R_INCONSISTENT_EARLY_DATA_SNI);
850
0
            return EXT_RETURN_FAIL;
851
0
        }
852
0
    }
853
854
0
    if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) {
855
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
856
0
        return EXT_RETURN_FAIL;
857
0
    }
858
859
    /*
860
     * Verify that we are offering an ALPN protocol consistent with the early
861
     * data.
862
     */
863
0
    if (edsess->ext.alpn_selected != NULL) {
864
0
        PACKET prots, alpnpkt;
865
0
        int found = 0;
866
867
0
        if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) {
868
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
869
0
            return EXT_RETURN_FAIL;
870
0
        }
871
0
        while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
872
0
            if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,
873
0
                             edsess->ext.alpn_selected_len)) {
874
0
                found = 1;
875
0
                break;
876
0
            }
877
0
        }
878
0
        if (!found) {
879
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
880
0
                     SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
881
0
            return EXT_RETURN_FAIL;
882
0
        }
883
0
    }
884
885
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
886
0
            || !WPACKET_start_sub_packet_u16(pkt)
887
0
            || !WPACKET_close(pkt)) {
888
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
889
0
        return EXT_RETURN_FAIL;
890
0
    }
891
892
    /*
893
     * We set this to rejected here. Later, if the server acknowledges the
894
     * extension, we set it to accepted.
895
     */
896
0
    s->ext.early_data = SSL_EARLY_DATA_REJECTED;
897
0
    s->ext.early_data_ok = 1;
898
899
0
    return EXT_RETURN_SENT;
900
0
}
901
902
0
#define F5_WORKAROUND_MIN_MSG_LEN   0xff
903
0
#define F5_WORKAROUND_MAX_MSG_LEN   0x200
904
905
/*
906
 * PSK pre binder overhead =
907
 *  2 bytes for TLSEXT_TYPE_psk
908
 *  2 bytes for extension length
909
 *  2 bytes for identities list length
910
 *  2 bytes for identity length
911
 *  4 bytes for obfuscated_ticket_age
912
 *  2 bytes for binder list length
913
 *  1 byte for binder length
914
 * The above excludes the number of bytes for the identity itself and the
915
 * subsequent binder bytes
916
 */
917
0
#define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
918
919
EXT_RETURN tls_construct_ctos_padding(SSL_CONNECTION *s, WPACKET *pkt,
920
                                      unsigned int context, X509 *x,
921
                                      size_t chainidx)
922
0
{
923
0
    unsigned char *padbytes;
924
0
    size_t hlen;
925
926
0
    if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
927
0
        return EXT_RETURN_NOT_SENT;
928
929
    /*
930
     * Add padding to workaround bugs in F5 terminators. See RFC7685.
931
     * This code calculates the length of all extensions added so far but
932
     * excludes the PSK extension (because that MUST be written last). Therefore
933
     * this extension MUST always appear second to last.
934
     */
935
0
    if (!WPACKET_get_total_written(pkt, &hlen)) {
936
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
937
0
        return EXT_RETURN_FAIL;
938
0
    }
939
940
    /*
941
     * If we're going to send a PSK then that will be written out after this
942
     * extension, so we need to calculate how long it is going to be.
943
     */
944
0
    if (s->session->ssl_version == TLS1_3_VERSION
945
0
            && s->session->ext.ticklen != 0
946
0
            && s->session->cipher != NULL) {
947
0
        const EVP_MD *md = ssl_md(SSL_CONNECTION_GET_CTX(s),
948
0
                                  s->session->cipher->algorithm2);
949
950
0
        if (md != NULL) {
951
            /*
952
             * Add the fixed PSK overhead, the identity length and the binder
953
             * length.
954
             */
955
0
            hlen +=  PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
956
0
                     + EVP_MD_get_size(md);
957
0
        }
958
0
    }
959
960
0
    if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
961
        /* Calculate the amount of padding we need to add */
962
0
        hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
963
964
        /*
965
         * Take off the size of extension header itself (2 bytes for type and
966
         * 2 bytes for length bytes), but ensure that the extension is at least
967
         * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
968
         * 8.x are intolerant of that condition)
969
         */
970
0
        if (hlen > 4)
971
0
            hlen -= 4;
972
0
        else
973
0
            hlen = 1;
974
975
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
976
0
                || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
977
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
978
0
            return EXT_RETURN_FAIL;
979
0
        }
980
0
        memset(padbytes, 0, hlen);
981
0
    }
982
983
0
    return EXT_RETURN_SENT;
984
0
}
985
986
/*
987
 * Construct the pre_shared_key extension
988
 */
989
EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt,
990
                                  unsigned int context,
991
                                  X509 *x, size_t chainidx)
992
0
{
993
0
#ifndef OPENSSL_NO_TLS1_3
994
0
    uint32_t agesec, agems = 0;
995
0
    size_t reshashsize = 0, pskhashsize = 0, binderoffset, msglen;
996
0
    unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
997
0
    const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
998
0
    int dores = 0;
999
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1000
0
    OSSL_TIME t;
1001
1002
0
    s->ext.tick_identity = 0;
1003
1004
    /*
1005
     * Note: At this stage of the code we only support adding a single
1006
     * resumption PSK. If we add support for multiple PSKs then the length
1007
     * calculations in the padding extension will need to be adjusted.
1008
     */
1009
1010
    /*
1011
     * If this is an incompatible or new session then we have nothing to resume
1012
     * so don't add this extension.
1013
     */
1014
0
    if (s->session->ssl_version != TLS1_3_VERSION
1015
0
            || (s->session->ext.ticklen == 0 && s->psksession == NULL))
1016
0
        return EXT_RETURN_NOT_SENT;
1017
1018
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
1019
0
        handmd = ssl_handshake_md(s);
1020
1021
0
    if (s->session->ext.ticklen != 0) {
1022
        /* Get the digest associated with the ciphersuite in the session */
1023
0
        if (s->session->cipher == NULL) {
1024
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1025
0
            return EXT_RETURN_FAIL;
1026
0
        }
1027
0
        mdres = ssl_md(sctx, s->session->cipher->algorithm2);
1028
0
        if (mdres == NULL) {
1029
            /*
1030
             * Don't recognize this cipher so we can't use the session.
1031
             * Ignore it
1032
             */
1033
0
            goto dopsksess;
1034
0
        }
1035
1036
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
1037
            /*
1038
             * Selected ciphersuite hash does not match the hash for the session
1039
             * so we can't use it.
1040
             */
1041
0
            goto dopsksess;
1042
0
        }
1043
1044
        /*
1045
         * Technically the C standard just says time() returns a time_t and says
1046
         * nothing about the encoding of that type. In practice most
1047
         * implementations follow POSIX which holds it as an integral type in
1048
         * seconds since epoch. We've already made the assumption that we can do
1049
         * this in multiple places in the code, so portability shouldn't be an
1050
         * issue.
1051
         */
1052
0
        t = ossl_time_subtract(ossl_time_now(), s->session->time);
1053
0
        agesec = (uint32_t)ossl_time2seconds(t);
1054
        /*
1055
         * We calculate the age in seconds but the server may work in ms. Due to
1056
         * rounding errors we could overestimate the age by up to 1s. It is
1057
         * better to underestimate it. Otherwise, if the RTT is very short, when
1058
         * the server calculates the age reported by the client it could be
1059
         * bigger than the age calculated on the server - which should never
1060
         * happen.
1061
         */
1062
0
        if (agesec > 0)
1063
0
            agesec--;
1064
1065
0
        if (s->session->ext.tick_lifetime_hint < agesec) {
1066
            /* Ticket is too old. Ignore it. */
1067
0
            goto dopsksess;
1068
0
        }
1069
1070
        /*
1071
         * Calculate age in ms. We're just doing it to nearest second. Should be
1072
         * good enough.
1073
         */
1074
0
        agems = agesec * (uint32_t)1000;
1075
1076
0
        if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
1077
            /*
1078
             * Overflow. Shouldn't happen unless this is a *really* old session.
1079
             * If so we just ignore it.
1080
             */
1081
0
            goto dopsksess;
1082
0
        }
1083
1084
        /*
1085
         * Obfuscate the age. Overflow here is fine, this addition is supposed
1086
         * to be mod 2^32.
1087
         */
1088
0
        agems += s->session->ext.tick_age_add;
1089
1090
0
        reshashsize = EVP_MD_get_size(mdres);
1091
0
        s->ext.tick_identity++;
1092
0
        dores = 1;
1093
0
    }
1094
1095
0
 dopsksess:
1096
0
    if (!dores && s->psksession == NULL)
1097
0
        return EXT_RETURN_NOT_SENT;
1098
1099
0
    if (s->psksession != NULL) {
1100
0
        mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2);
1101
0
        if (mdpsk == NULL) {
1102
            /*
1103
             * Don't recognize this cipher so we can't use the session.
1104
             * If this happens it's an application bug.
1105
             */
1106
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1107
0
            return EXT_RETURN_FAIL;
1108
0
        }
1109
1110
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
1111
            /*
1112
             * Selected ciphersuite hash does not match the hash for the PSK
1113
             * session. This is an application bug.
1114
             */
1115
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1116
0
            return EXT_RETURN_FAIL;
1117
0
        }
1118
1119
0
        pskhashsize = EVP_MD_get_size(mdpsk);
1120
0
    }
1121
1122
    /* Create the extension, but skip over the binder for now */
1123
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
1124
0
            || !WPACKET_start_sub_packet_u16(pkt)
1125
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
1126
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1127
0
        return EXT_RETURN_FAIL;
1128
0
    }
1129
1130
0
    if (dores) {
1131
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
1132
0
                                           s->session->ext.ticklen)
1133
0
                || !WPACKET_put_bytes_u32(pkt, agems)) {
1134
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1135
0
            return EXT_RETURN_FAIL;
1136
0
        }
1137
0
    }
1138
1139
0
    if (s->psksession != NULL) {
1140
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
1141
0
                                    s->psksession_id_len)
1142
0
                || !WPACKET_put_bytes_u32(pkt, 0)) {
1143
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1144
0
            return EXT_RETURN_FAIL;
1145
0
        }
1146
0
        s->ext.tick_identity++;
1147
0
    }
1148
1149
0
    if (!WPACKET_close(pkt)
1150
0
            || !WPACKET_get_total_written(pkt, &binderoffset)
1151
0
            || !WPACKET_start_sub_packet_u16(pkt)
1152
0
            || (dores
1153
0
                && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))
1154
0
            || (s->psksession != NULL
1155
0
                && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))
1156
0
            || !WPACKET_close(pkt)
1157
0
            || !WPACKET_close(pkt)
1158
0
            || !WPACKET_get_total_written(pkt, &msglen)
1159
               /*
1160
                * We need to fill in all the sub-packet lengths now so we can
1161
                * calculate the HMAC of the message up to the binders
1162
                */
1163
0
            || !WPACKET_fill_lengths(pkt)) {
1164
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1165
0
        return EXT_RETURN_FAIL;
1166
0
    }
1167
1168
0
    msgstart = WPACKET_get_curr(pkt) - msglen;
1169
1170
0
    if (dores
1171
0
            && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
1172
0
                                 resbinder, s->session, 1, 0) != 1) {
1173
        /* SSLfatal() already called */
1174
0
        return EXT_RETURN_FAIL;
1175
0
    }
1176
1177
0
    if (s->psksession != NULL
1178
0
            && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
1179
0
                                 pskbinder, s->psksession, 1, 1) != 1) {
1180
        /* SSLfatal() already called */
1181
0
        return EXT_RETURN_FAIL;
1182
0
    }
1183
1184
0
    return EXT_RETURN_SENT;
1185
#else
1186
    return EXT_RETURN_NOT_SENT;
1187
#endif
1188
0
}
1189
1190
EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt,
1191
                                                  ossl_unused unsigned int context,
1192
                                                  ossl_unused X509 *x,
1193
                                                  ossl_unused size_t chainidx)
1194
0
{
1195
0
#ifndef OPENSSL_NO_TLS1_3
1196
0
    if (!s->pha_enabled)
1197
0
        return EXT_RETURN_NOT_SENT;
1198
1199
    /* construct extension - 0 length, no contents */
1200
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
1201
0
            || !WPACKET_start_sub_packet_u16(pkt)
1202
0
            || !WPACKET_close(pkt)) {
1203
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1204
0
        return EXT_RETURN_FAIL;
1205
0
    }
1206
1207
0
    s->post_handshake_auth = SSL_PHA_EXT_SENT;
1208
1209
0
    return EXT_RETURN_SENT;
1210
#else
1211
    return EXT_RETURN_NOT_SENT;
1212
#endif
1213
0
}
1214
1215
1216
/*
1217
 * Parse the server's renegotiation binding and abort if it's not right
1218
 */
1219
int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
1220
                               unsigned int context,
1221
                               X509 *x, size_t chainidx)
1222
0
{
1223
0
    size_t expected_len = s->s3.previous_client_finished_len
1224
0
        + s->s3.previous_server_finished_len;
1225
0
    size_t ilen;
1226
0
    const unsigned char *data;
1227
1228
    /* Check for logic errors */
1229
0
    if (!ossl_assert(expected_len == 0
1230
0
                     || s->s3.previous_client_finished_len != 0)
1231
0
        || !ossl_assert(expected_len == 0
1232
0
                        || s->s3.previous_server_finished_len != 0)) {
1233
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1234
0
        return 0;
1235
0
    }
1236
1237
    /* Parse the length byte */
1238
0
    if (!PACKET_get_1_len(pkt, &ilen)) {
1239
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1240
0
        return 0;
1241
0
    }
1242
1243
    /* Consistency check */
1244
0
    if (PACKET_remaining(pkt) != ilen) {
1245
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1246
0
        return 0;
1247
0
    }
1248
1249
    /* Check that the extension matches */
1250
0
    if (ilen != expected_len) {
1251
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1252
0
        return 0;
1253
0
    }
1254
1255
0
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len)
1256
0
        || memcmp(data, s->s3.previous_client_finished,
1257
0
                  s->s3.previous_client_finished_len) != 0) {
1258
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1259
0
        return 0;
1260
0
    }
1261
1262
0
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len)
1263
0
        || memcmp(data, s->s3.previous_server_finished,
1264
0
                  s->s3.previous_server_finished_len) != 0) {
1265
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1266
0
        return 0;
1267
0
    }
1268
0
    s->s3.send_connection_binding = 1;
1269
1270
0
    return 1;
1271
0
}
1272
1273
/* Parse the server's max fragment len extension packet */
1274
int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
1275
                                  unsigned int context,
1276
                                  X509 *x, size_t chainidx)
1277
0
{
1278
0
    unsigned int value;
1279
1280
0
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
1281
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1282
0
        return 0;
1283
0
    }
1284
1285
    /* |value| should contains a valid max-fragment-length code. */
1286
0
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
1287
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1288
0
                 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1289
0
        return 0;
1290
0
    }
1291
1292
    /* Must be the same value as client-configured one who was sent to server */
1293
    /*-
1294
     * RFC 6066: if a client receives a maximum fragment length negotiation
1295
     * response that differs from the length it requested, ...
1296
     * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
1297
     */
1298
0
    if (value != s->ext.max_fragment_len_mode) {
1299
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1300
0
                 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1301
0
        return 0;
1302
0
    }
1303
1304
    /*
1305
     * Maximum Fragment Length Negotiation succeeded.
1306
     * The negotiated Maximum Fragment Length is binding now.
1307
     */
1308
0
    s->session->ext.max_fragment_len_mode = value;
1309
1310
0
    return 1;
1311
0
}
1312
1313
int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt,
1314
                               unsigned int context,
1315
                               X509 *x, size_t chainidx)
1316
0
{
1317
0
    if (s->ext.hostname == NULL) {
1318
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1319
0
        return 0;
1320
0
    }
1321
1322
0
    if (PACKET_remaining(pkt) > 0) {
1323
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1324
0
        return 0;
1325
0
    }
1326
1327
0
    if (!s->hit) {
1328
0
        if (s->session->ext.hostname != NULL) {
1329
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1330
0
            return 0;
1331
0
        }
1332
0
        s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
1333
0
        if (s->session->ext.hostname == NULL) {
1334
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1335
0
            return 0;
1336
0
        }
1337
0
    }
1338
1339
0
    return 1;
1340
0
}
1341
1342
int tls_parse_stoc_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,
1343
                                 unsigned int context,
1344
                                 X509 *x, size_t chainidx)
1345
0
{
1346
0
    size_t ecpointformats_len;
1347
0
    PACKET ecptformatlist;
1348
1349
0
    if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
1350
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1351
0
        return 0;
1352
0
    }
1353
0
    if (!s->hit) {
1354
0
        ecpointformats_len = PACKET_remaining(&ecptformatlist);
1355
0
        if (ecpointformats_len == 0) {
1356
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1357
0
            return 0;
1358
0
        }
1359
1360
0
        s->ext.peer_ecpointformats_len = 0;
1361
0
        OPENSSL_free(s->ext.peer_ecpointformats);
1362
0
        s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len);
1363
0
        if (s->ext.peer_ecpointformats == NULL) {
1364
0
            s->ext.peer_ecpointformats_len = 0;
1365
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1366
0
            return 0;
1367
0
        }
1368
1369
0
        s->ext.peer_ecpointformats_len = ecpointformats_len;
1370
1371
0
        if (!PACKET_copy_bytes(&ecptformatlist,
1372
0
                               s->ext.peer_ecpointformats,
1373
0
                               ecpointformats_len)) {
1374
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1375
0
            return 0;
1376
0
        }
1377
0
    }
1378
1379
0
    return 1;
1380
0
}
1381
1382
int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
1383
                                  unsigned int context,
1384
                                  X509 *x, size_t chainidx)
1385
0
{
1386
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1387
1388
0
    if (s->ext.session_ticket_cb != NULL &&
1389
0
        !s->ext.session_ticket_cb(ssl, PACKET_data(pkt),
1390
0
                                  PACKET_remaining(pkt),
1391
0
                                  s->ext.session_ticket_cb_arg)) {
1392
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
1393
0
        return 0;
1394
0
    }
1395
1396
0
    if (!tls_use_ticket(s)) {
1397
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1398
0
        return 0;
1399
0
    }
1400
0
    if (PACKET_remaining(pkt) > 0) {
1401
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1402
0
        return 0;
1403
0
    }
1404
1405
0
    s->ext.ticket_expected = 1;
1406
1407
0
    return 1;
1408
0
}
1409
1410
#ifndef OPENSSL_NO_OCSP
1411
int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt,
1412
                                  unsigned int context,
1413
                                  X509 *x, size_t chainidx)
1414
0
{
1415
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1416
        /* We ignore this if the server sends a CertificateRequest */
1417
0
        return 1;
1418
0
    }
1419
1420
    /*
1421
     * MUST only be sent if we've requested a status
1422
     * request message. In TLS <= 1.2 it must also be empty.
1423
     */
1424
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
1425
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1426
0
        return 0;
1427
0
    }
1428
0
    if (!SSL_CONNECTION_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
1429
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1430
0
        return 0;
1431
0
    }
1432
1433
0
    if (SSL_CONNECTION_IS_TLS13(s)) {
1434
        /* We only know how to handle this if it's for the first Certificate in
1435
         * the chain. We ignore any other responses.
1436
         */
1437
0
        if (chainidx != 0)
1438
0
            return 1;
1439
1440
        /* SSLfatal() already called */
1441
0
        return tls_process_cert_status_body(s, pkt);
1442
0
    }
1443
1444
    /* Set flag to expect CertificateStatus message */
1445
0
    s->ext.status_expected = 1;
1446
1447
0
    return 1;
1448
0
}
1449
#endif
1450
1451
1452
#ifndef OPENSSL_NO_CT
1453
int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1454
                       X509 *x, size_t chainidx)
1455
0
{
1456
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1457
        /* We ignore this if the server sends it in a CertificateRequest */
1458
0
        return 1;
1459
0
    }
1460
1461
    /*
1462
     * Only take it if we asked for it - i.e if there is no CT validation
1463
     * callback set, then a custom extension MAY be processing it, so we
1464
     * need to let control continue to flow to that.
1465
     */
1466
0
    if (s->ct_validation_callback != NULL) {
1467
0
        size_t size = PACKET_remaining(pkt);
1468
1469
        /* Simply copy it off for later processing */
1470
0
        OPENSSL_free(s->ext.scts);
1471
0
        s->ext.scts = NULL;
1472
1473
0
        s->ext.scts_len = (uint16_t)size;
1474
0
        if (size > 0) {
1475
0
            s->ext.scts = OPENSSL_malloc(size);
1476
0
            if (s->ext.scts == NULL) {
1477
0
                s->ext.scts_len = 0;
1478
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
1479
0
                return 0;
1480
0
            }
1481
0
            if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) {
1482
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1483
0
                return 0;
1484
0
            }
1485
0
        }
1486
0
    } else {
1487
0
        ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
1488
0
                        ? ENDPOINT_CLIENT : ENDPOINT_BOTH;
1489
1490
        /*
1491
         * If we didn't ask for it then there must be a custom extension,
1492
         * otherwise this is unsolicited.
1493
         */
1494
0
        if (custom_ext_find(&s->cert->custext, role,
1495
0
                            TLSEXT_TYPE_signed_certificate_timestamp,
1496
0
                            NULL) == NULL) {
1497
0
            SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1498
0
            return 0;
1499
0
        }
1500
1501
0
        if (!custom_ext_parse(s, context,
1502
0
                             TLSEXT_TYPE_signed_certificate_timestamp,
1503
0
                             PACKET_data(pkt), PACKET_remaining(pkt),
1504
0
                             x, chainidx)) {
1505
            /* SSLfatal already called */
1506
0
            return 0;
1507
0
        }
1508
0
    }
1509
1510
0
    return 1;
1511
0
}
1512
#endif
1513
1514
1515
#ifndef OPENSSL_NO_NEXTPROTONEG
1516
/*
1517
 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1518
 * elements of zero length are allowed and the set of elements must exactly
1519
 * fill the length of the block. Returns 1 on success or 0 on failure.
1520
 */
1521
static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt)
1522
0
{
1523
0
    PACKET tmp_protocol;
1524
1525
0
    while (PACKET_remaining(pkt)) {
1526
0
        if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
1527
0
            || PACKET_remaining(&tmp_protocol) == 0) {
1528
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1529
0
            return 0;
1530
0
        }
1531
0
    }
1532
1533
0
    return 1;
1534
0
}
1535
1536
int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1537
                       X509 *x, size_t chainidx)
1538
0
{
1539
0
    unsigned char *selected;
1540
0
    unsigned char selected_len;
1541
0
    PACKET tmppkt;
1542
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1543
1544
    /* Check if we are in a renegotiation. If so ignore this extension */
1545
0
    if (!SSL_IS_FIRST_HANDSHAKE(s))
1546
0
        return 1;
1547
1548
    /* We must have requested it. */
1549
0
    if (sctx->ext.npn_select_cb == NULL) {
1550
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1551
0
        return 0;
1552
0
    }
1553
1554
    /* The data must be valid */
1555
0
    tmppkt = *pkt;
1556
0
    if (!ssl_next_proto_validate(s, &tmppkt)) {
1557
        /* SSLfatal() already called */
1558
0
        return 0;
1559
0
    }
1560
0
    if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_SSL(s),
1561
0
                                &selected, &selected_len,
1562
0
                                PACKET_data(pkt), PACKET_remaining(pkt),
1563
0
                                sctx->ext.npn_select_cb_arg) !=
1564
0
             SSL_TLSEXT_ERR_OK) {
1565
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
1566
0
        return 0;
1567
0
    }
1568
1569
    /*
1570
     * Could be non-NULL if server has sent multiple NPN extensions in
1571
     * a single Serverhello
1572
     */
1573
0
    OPENSSL_free(s->ext.npn);
1574
0
    s->ext.npn = OPENSSL_malloc(selected_len);
1575
0
    if (s->ext.npn == NULL) {
1576
0
        s->ext.npn_len = 0;
1577
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1578
0
        return 0;
1579
0
    }
1580
1581
0
    memcpy(s->ext.npn, selected, selected_len);
1582
0
    s->ext.npn_len = selected_len;
1583
0
    s->s3.npn_seen = 1;
1584
1585
0
    return 1;
1586
0
}
1587
#endif
1588
1589
int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1590
                        X509 *x, size_t chainidx)
1591
0
{
1592
0
    size_t len;
1593
1594
    /* We must have requested it. */
1595
0
    if (!s->s3.alpn_sent) {
1596
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1597
0
        return 0;
1598
0
    }
1599
    /*-
1600
     * The extension data consists of:
1601
     *   uint16 list_length
1602
     *   uint8 proto_length;
1603
     *   uint8 proto[proto_length];
1604
     */
1605
0
    if (!PACKET_get_net_2_len(pkt, &len)
1606
0
        || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
1607
0
        || PACKET_remaining(pkt) != len) {
1608
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1609
0
        return 0;
1610
0
    }
1611
0
    OPENSSL_free(s->s3.alpn_selected);
1612
0
    s->s3.alpn_selected = OPENSSL_malloc(len);
1613
0
    if (s->s3.alpn_selected == NULL) {
1614
0
        s->s3.alpn_selected_len = 0;
1615
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1616
0
        return 0;
1617
0
    }
1618
0
    if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) {
1619
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1620
0
        return 0;
1621
0
    }
1622
0
    s->s3.alpn_selected_len = len;
1623
1624
0
    if (s->session->ext.alpn_selected == NULL
1625
0
            || s->session->ext.alpn_selected_len != len
1626
0
            || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len)
1627
0
               != 0) {
1628
        /* ALPN not consistent with the old session so cannot use early_data */
1629
0
        s->ext.early_data_ok = 0;
1630
0
    }
1631
0
    if (!s->hit) {
1632
        /*
1633
         * This is a new session and so alpn_selected should have been
1634
         * initialised to NULL. We should update it with the selected ALPN.
1635
         */
1636
0
        if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
1637
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1638
0
            return 0;
1639
0
        }
1640
0
        s->session->ext.alpn_selected =
1641
0
            OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
1642
0
        if (s->session->ext.alpn_selected == NULL) {
1643
0
            s->session->ext.alpn_selected_len = 0;
1644
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1645
0
            return 0;
1646
0
        }
1647
0
        s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
1648
0
    }
1649
1650
0
    return 1;
1651
0
}
1652
1653
#ifndef OPENSSL_NO_SRTP
1654
int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
1655
                            unsigned int context, X509 *x, size_t chainidx)
1656
0
{
1657
0
    unsigned int id, ct, mki;
1658
0
    int i;
1659
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
1660
0
    SRTP_PROTECTION_PROFILE *prof;
1661
1662
0
    if (!PACKET_get_net_2(pkt, &ct) || ct != 2
1663
0
            || !PACKET_get_net_2(pkt, &id)
1664
0
            || !PACKET_get_1(pkt, &mki)
1665
0
            || PACKET_remaining(pkt) != 0) {
1666
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1667
0
                 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1668
0
        return 0;
1669
0
    }
1670
1671
0
    if (mki != 0) {
1672
        /* Must be no MKI, since we never offer one */
1673
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE);
1674
0
        return 0;
1675
0
    }
1676
1677
    /* Throw an error if the server gave us an unsolicited extension */
1678
0
    clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s));
1679
0
    if (clnt == NULL) {
1680
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES);
1681
0
        return 0;
1682
0
    }
1683
1684
    /*
1685
     * Check to see if the server gave us something we support (and
1686
     * presumably offered)
1687
     */
1688
0
    for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
1689
0
        prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
1690
1691
0
        if (prof->id == id) {
1692
0
            s->srtp_profile = prof;
1693
0
            return 1;
1694
0
        }
1695
0
    }
1696
1697
0
    SSLfatal(s, SSL_AD_DECODE_ERROR,
1698
0
             SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1699
0
    return 0;
1700
0
}
1701
#endif
1702
1703
int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1704
                       X509 *x, size_t chainidx)
1705
0
{
1706
    /* Ignore if inappropriate ciphersuite */
1707
0
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
1708
0
            && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD
1709
0
            && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4
1710
0
            && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT
1711
0
            && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12
1712
0
            && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA
1713
0
            && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK)
1714
0
        s->ext.use_etm = 1;
1715
1716
0
    return 1;
1717
0
}
1718
1719
int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1720
                       X509 *x, size_t chainidx)
1721
0
{
1722
0
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
1723
0
        return 1;
1724
0
    s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1725
0
    if (!s->hit)
1726
0
        s->session->flags |= SSL_SESS_FLAG_EXTMS;
1727
1728
0
    return 1;
1729
0
}
1730
1731
int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt,
1732
                                      unsigned int context,
1733
                                      X509 *x, size_t chainidx)
1734
0
{
1735
0
    unsigned int version;
1736
1737
0
    if (!PACKET_get_net_2(pkt, &version)
1738
0
            || PACKET_remaining(pkt) != 0) {
1739
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1740
0
        return 0;
1741
0
    }
1742
1743
    /*
1744
     * The only protocol version we support which is valid in this extension in
1745
     * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
1746
     */
1747
0
    if (version != TLS1_3_VERSION) {
1748
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1749
0
                 SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1750
0
        return 0;
1751
0
    }
1752
1753
    /* We ignore this extension for HRRs except to sanity check it */
1754
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
1755
0
        return 1;
1756
1757
    /* We just set it here. We validate it in ssl_choose_client_version */
1758
0
    s->version = version;
1759
0
    if (!ssl_set_record_protocol_version(s, version)) {
1760
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1761
0
        return 0;
1762
0
    }
1763
1764
0
    return 1;
1765
0
}
1766
1767
int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt,
1768
                             unsigned int context, X509 *x,
1769
                             size_t chainidx)
1770
0
{
1771
0
#ifndef OPENSSL_NO_TLS1_3
1772
0
    unsigned int group_id;
1773
0
    PACKET encoded_pt;
1774
0
    EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL;
1775
0
    const TLS_GROUP_INFO *ginf = NULL;
1776
1777
    /* Sanity check */
1778
0
    if (ckey == NULL || s->s3.peer_tmp != NULL) {
1779
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1780
0
        return 0;
1781
0
    }
1782
1783
0
    if (!PACKET_get_net_2(pkt, &group_id)) {
1784
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1785
0
        return 0;
1786
0
    }
1787
1788
0
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
1789
0
        const uint16_t *pgroups = NULL;
1790
0
        size_t i, num_groups;
1791
1792
0
        if (PACKET_remaining(pkt) != 0) {
1793
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1794
0
            return 0;
1795
0
        }
1796
1797
        /*
1798
         * It is an error if the HelloRetryRequest wants a key_share that we
1799
         * already sent in the first ClientHello
1800
         */
1801
0
        if (group_id == s->s3.group_id) {
1802
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1803
0
            return 0;
1804
0
        }
1805
1806
        /* Validate the selected group is one we support */
1807
0
        tls1_get_supported_groups(s, &pgroups, &num_groups);
1808
0
        for (i = 0; i < num_groups; i++) {
1809
0
            if (group_id == pgroups[i])
1810
0
                break;
1811
0
        }
1812
0
        if (i >= num_groups
1813
0
                || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
1814
0
                || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
1815
0
                                    0, NULL)) {
1816
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1817
0
            return 0;
1818
0
        }
1819
1820
0
        s->s3.group_id = group_id;
1821
0
        EVP_PKEY_free(s->s3.tmp.pkey);
1822
0
        s->s3.tmp.pkey = NULL;
1823
0
        return 1;
1824
0
    }
1825
1826
0
    if (group_id != s->s3.group_id) {
1827
        /*
1828
         * This isn't for the group that we sent in the original
1829
         * key_share!
1830
         */
1831
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1832
0
        return 0;
1833
0
    }
1834
    /* Retain this group in the SSL_SESSION */
1835
0
    if (!s->hit) {
1836
0
        s->session->kex_group = group_id;
1837
0
    } else if (group_id != s->session->kex_group) {
1838
        /*
1839
         * If this is a resumption but changed what group was used, we need
1840
         * to record the new group in the session, but the session is not
1841
         * a new session and could be in use by other threads.  So, make
1842
         * a copy of the session to record the new information so that it's
1843
         * useful for any sessions resumed from tickets issued on this
1844
         * connection.
1845
         */
1846
0
        SSL_SESSION *new_sess;
1847
1848
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) {
1849
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
1850
0
            return 0;
1851
0
        }
1852
0
        SSL_SESSION_free(s->session);
1853
0
        s->session = new_sess;
1854
0
        s->session->kex_group = group_id;
1855
0
    }
1856
1857
0
    if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
1858
0
                                     group_id)) == NULL) {
1859
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1860
0
        return 0;
1861
0
    }
1862
1863
0
    if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
1864
0
            || PACKET_remaining(&encoded_pt) == 0) {
1865
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1866
0
        return 0;
1867
0
    }
1868
1869
0
    if (!ginf->is_kem) {
1870
        /* Regular KEX */
1871
0
        skey = EVP_PKEY_new();
1872
0
        if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) {
1873
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
1874
0
            EVP_PKEY_free(skey);
1875
0
            return 0;
1876
0
        }
1877
1878
0
        if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt),
1879
0
                                      PACKET_remaining(&encoded_pt)) <= 0) {
1880
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
1881
0
            EVP_PKEY_free(skey);
1882
0
            return 0;
1883
0
        }
1884
1885
0
        if (ssl_derive(s, ckey, skey, 1) == 0) {
1886
            /* SSLfatal() already called */
1887
0
            EVP_PKEY_free(skey);
1888
0
            return 0;
1889
0
        }
1890
0
        s->s3.peer_tmp = skey;
1891
0
    } else {
1892
        /* KEM Mode */
1893
0
        const unsigned char *ct = PACKET_data(&encoded_pt);
1894
0
        size_t ctlen = PACKET_remaining(&encoded_pt);
1895
1896
0
        if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) {
1897
            /* SSLfatal() already called */
1898
0
            return 0;
1899
0
        }
1900
0
    }
1901
0
    s->s3.did_kex = 1;
1902
0
#endif
1903
1904
0
    return 1;
1905
0
}
1906
1907
int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1908
                          X509 *x, size_t chainidx)
1909
0
{
1910
0
    PACKET cookie;
1911
1912
0
    if (!PACKET_as_length_prefixed_2(pkt, &cookie)
1913
0
            || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
1914
0
                              &s->ext.tls13_cookie_len)) {
1915
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1916
0
        return 0;
1917
0
    }
1918
1919
0
    return 1;
1920
0
}
1921
1922
int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt,
1923
                              unsigned int context,
1924
                              X509 *x, size_t chainidx)
1925
0
{
1926
0
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
1927
0
        unsigned long max_early_data;
1928
1929
0
        if (!PACKET_get_net_4(pkt, &max_early_data)
1930
0
                || PACKET_remaining(pkt) != 0) {
1931
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA);
1932
0
            return 0;
1933
0
        }
1934
1935
0
        s->session->ext.max_early_data = max_early_data;
1936
1937
0
        return 1;
1938
0
    }
1939
1940
0
    if (PACKET_remaining(pkt) != 0) {
1941
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1942
0
        return 0;
1943
0
    }
1944
1945
0
    if (!s->ext.early_data_ok
1946
0
            || !s->hit) {
1947
        /*
1948
         * If we get here then we didn't send early data, or we didn't resume
1949
         * using the first identity, or the SNI/ALPN is not consistent so the
1950
         * server should not be accepting it.
1951
         */
1952
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1953
0
        return 0;
1954
0
    }
1955
1956
0
    s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1957
1958
0
    return 1;
1959
0
}
1960
1961
int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt,
1962
                       unsigned int context, X509 *x,
1963
                       size_t chainidx)
1964
0
{
1965
0
#ifndef OPENSSL_NO_TLS1_3
1966
0
    unsigned int identity;
1967
1968
0
    if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
1969
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1970
0
        return 0;
1971
0
    }
1972
1973
0
    if (identity >= (unsigned int)s->ext.tick_identity) {
1974
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY);
1975
0
        return 0;
1976
0
    }
1977
1978
    /*
1979
     * Session resumption tickets are always sent before PSK tickets. If the
1980
     * ticket index is 0 then it must be for a session resumption ticket if we
1981
     * sent two tickets, or if we didn't send a PSK ticket.
1982
     */
1983
0
    if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) {
1984
0
        s->hit = 1;
1985
0
        SSL_SESSION_free(s->psksession);
1986
0
        s->psksession = NULL;
1987
0
        return 1;
1988
0
    }
1989
1990
0
    if (s->psksession == NULL) {
1991
        /* Should never happen */
1992
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1993
0
        return 0;
1994
0
    }
1995
1996
    /*
1997
     * If we used the external PSK for sending early_data then s->early_secret
1998
     * is already set up, so don't overwrite it. Otherwise we copy the
1999
     * early_secret across that we generated earlier.
2000
     */
2001
0
    if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
2002
0
                && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
2003
0
            || s->session->ext.max_early_data > 0
2004
0
            || s->psksession->ext.max_early_data == 0)
2005
0
        memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
2006
2007
0
    SSL_SESSION_free(s->session);
2008
0
    s->session = s->psksession;
2009
0
    s->psksession = NULL;
2010
0
    s->hit = 1;
2011
    /* Early data is only allowed if we used the first ticket */
2012
0
    if (identity != 0)
2013
0
        s->ext.early_data_ok = 0;
2014
0
#endif
2015
2016
0
    return 1;
2017
0
}
2018
2019
EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2020
                                               unsigned int context,
2021
                                               X509 *x, size_t chainidx)
2022
0
{
2023
0
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2024
0
    if (sc->client_cert_type == NULL)
2025
0
        return EXT_RETURN_NOT_SENT;
2026
2027
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2028
0
            || !WPACKET_start_sub_packet_u16(pkt)
2029
0
            || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len)
2030
0
            || !WPACKET_close(pkt)) {
2031
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2032
0
        return EXT_RETURN_FAIL;
2033
0
    }
2034
0
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2035
0
    return EXT_RETURN_SENT;
2036
0
}
2037
2038
int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2039
                                    unsigned int context,
2040
                                    X509 *x, size_t chainidx)
2041
0
{
2042
0
    unsigned int type;
2043
2044
0
    if (PACKET_remaining(pkt) != 1) {
2045
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2046
0
        return 0;
2047
0
    }
2048
0
    if (!PACKET_get_1(pkt, &type)) {
2049
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2050
0
        return 0;
2051
0
    }
2052
    /* We did not send/ask for this */
2053
0
    if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2054
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2055
0
        return 0;
2056
0
    }
2057
    /* We don't have this enabled */
2058
0
    if (sc->client_cert_type == NULL) {
2059
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2060
0
        return 0;
2061
0
    }
2062
    /* Given back a value we didn't configure */
2063
0
    if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) {
2064
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2065
0
        return 0;
2066
0
    }
2067
0
    sc->ext.client_cert_type = type;
2068
0
    return 1;
2069
0
}
2070
2071
EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2072
                                               unsigned int context,
2073
                                               X509 *x, size_t chainidx)
2074
0
{
2075
0
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2076
0
    if (sc->server_cert_type == NULL)
2077
0
        return EXT_RETURN_NOT_SENT;
2078
2079
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2080
0
            || !WPACKET_start_sub_packet_u16(pkt)
2081
0
            || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len)
2082
0
            || !WPACKET_close(pkt)) {
2083
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2084
0
        return EXT_RETURN_FAIL;
2085
0
    }
2086
0
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2087
0
    return EXT_RETURN_SENT;
2088
0
}
2089
2090
int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2091
                                    unsigned int context,
2092
                                    X509 *x, size_t chainidx)
2093
0
{
2094
0
    unsigned int type;
2095
2096
0
    if (PACKET_remaining(pkt) != 1) {
2097
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2098
0
        return 0;
2099
0
    }
2100
0
    if (!PACKET_get_1(pkt, &type)) {
2101
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2102
0
        return 0;
2103
0
    }
2104
    /* We did not send/ask for this */
2105
0
    if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2106
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2107
0
        return 0;
2108
0
    }
2109
    /* We don't have this enabled */
2110
0
    if (sc->server_cert_type == NULL) {
2111
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2112
0
        return 0;
2113
0
    }
2114
    /* Given back a value we didn't configure */
2115
0
    if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) {
2116
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2117
0
        return 0;
2118
0
    }
2119
0
    sc->ext.server_cert_type = type;
2120
0
    return 1;
2121
0
}