Coverage Report

Created: 2025-07-01 06:23

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