Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/ssl/statem/statem_srvr.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include "internal/e_os.h"
13
14
#include <stdio.h>
15
#include "../ssl_local.h"
16
#include "statem_local.h"
17
#include "internal/constant_time.h"
18
#include "internal/cryptlib.h"
19
#include "internal/ssl_unwrap.h"
20
#include "internal/sizes.h"
21
#include <openssl/buffer.h>
22
#include <openssl/rand.h>
23
#include <openssl/objects.h>
24
#include <openssl/evp.h>
25
#include <openssl/x509.h>
26
#include <openssl/dh.h>
27
#include <openssl/rsa.h>
28
#include <openssl/bn.h>
29
#include <openssl/md5.h>
30
#include <openssl/trace.h>
31
#include <openssl/core_names.h>
32
#include <openssl/asn1t.h>
33
#include <openssl/comp.h>
34
#include "internal/comp.h"
35
#include <openssl/ocsp.h>
36
37
0
#define TICKET_NONCE_SIZE 8
38
39
#ifndef OPENSSL_NO_ECH
40
#include "../ech/ech_local.h"
41
#endif
42
43
typedef struct {
44
    ASN1_TYPE *kxBlob;
45
    ASN1_TYPE *opaqueBlob;
46
} GOST_KX_MESSAGE;
47
48
DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
49
50
ASN1_SEQUENCE(GOST_KX_MESSAGE) = {
51
    ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY),
52
    ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
53
0
} ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
54
0
55
0
IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
56
0
57
0
static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s,
58
0
    WPACKET *pkt);
59
0
60
0
static ossl_inline int received_client_cert(const SSL_CONNECTION *sc)
61
28.1k
{
62
28.1k
    return sc->session->peer_rpk != NULL || sc->session->peer != NULL;
63
28.1k
}
64
65
/*
66
 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
67
 * handshake state transitions when a TLSv1.3 server is reading messages from
68
 * the client. The message type that the client has sent is provided in |mt|.
69
 * The current state is in |s->statem.hand_state|.
70
 *
71
 * Return values are 1 for success (transition allowed) and  0 on error
72
 * (transition not allowed)
73
 */
74
static int ossl_statem_server13_read_transition(SSL_CONNECTION *s, int mt)
75
113
{
76
113
    OSSL_STATEM *st = &s->statem;
77
78
113
    if (st->hand_state == TLS_ST_SR_ACK) {
79
0
        st->hand_state = st->pre_ack_hand_state;
80
0
        if (mt == SSL3_MT_DUMMY)
81
0
            return 1;
82
0
    }
83
84
    /*
85
     * Note: There is no case for TLS_ST_BEFORE because at that stage we have
86
     * not negotiated TLSv1.3 yet, so that case is handled by
87
     * ossl_statem_server_read_transition()
88
     */
89
113
    switch (st->hand_state) {
90
0
    default:
91
0
        break;
92
93
113
    case TLS_ST_EARLY_DATA:
94
113
        if (s->hello_retry_request == SSL_HRR_PENDING) {
95
111
            if (mt == SSL3_MT_CLIENT_HELLO) {
96
109
                st->hand_state = TLS_ST_SR_CLNT_HELLO;
97
109
                return 1;
98
109
            }
99
2
            break;
100
            /*
101
             * RFC 9147 Section 5.6 states DTLS1.3 should omit the
102
             * End of Early Data Message
103
             */
104
111
        } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
105
0
            && !SSL_NO_EOED(s)) {
106
0
            if (mt == SSL3_MT_END_OF_EARLY_DATA) {
107
0
                st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
108
0
                return 1;
109
0
            }
110
0
            break;
111
2
        } else if (SSL_CONNECTION_IS_DTLS13(s) && s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
112
            /*
113
             * Let DTLS1.3 fallthrough to the Finished processing below
114
             */
115
0
            s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
116
0
        }
117
        /* Fall through */
118
119
2
    case TLS_ST_SR_END_OF_EARLY_DATA:
120
2
    case TLS_ST_SW_FINISHED:
121
2
        if (s->s3.tmp.cert_request) {
122
0
            if (mt == SSL3_MT_CERTIFICATE) {
123
0
                st->hand_state = TLS_ST_SR_CERT;
124
0
                return 1;
125
0
            }
126
#ifndef OPENSSL_NO_COMP_ALG
127
            if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
128
                && s->ext.compress_certificate_sent) {
129
                st->hand_state = TLS_ST_SR_COMP_CERT;
130
                return 1;
131
            }
132
#endif
133
2
        } else {
134
2
            if (mt == SSL3_MT_FINISHED) {
135
0
                st->hand_state = TLS_ST_SR_FINISHED;
136
0
                return 1;
137
0
            }
138
2
        }
139
2
        break;
140
141
2
    case TLS_ST_SR_COMP_CERT:
142
0
    case TLS_ST_SR_CERT:
143
0
        if (st->no_cert_verify || !received_client_cert(s)) {
144
0
            if (mt == SSL3_MT_FINISHED) {
145
0
                st->hand_state = TLS_ST_SR_FINISHED;
146
0
                return 1;
147
0
            }
148
0
        } else {
149
0
            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
150
0
                st->hand_state = TLS_ST_SR_CERT_VRFY;
151
0
                return 1;
152
0
            }
153
0
        }
154
0
        break;
155
156
0
    case TLS_ST_SR_CERT_VRFY:
157
0
        if (mt == SSL3_MT_FINISHED) {
158
0
            st->hand_state = TLS_ST_SR_FINISHED;
159
0
            return 1;
160
0
        }
161
0
        break;
162
163
0
    case TLS_ST_SW_KEY_UPDATE:
164
0
    case TLS_ST_SW_SESSION_TICKET:
165
0
        if (mt == DTLS13_MT_ACK) {
166
0
            st->pre_ack_hand_state = st->hand_state;
167
0
            st->hand_state = TLS_ST_SR_ACK;
168
0
            return 1;
169
0
        }
170
0
        break;
171
172
0
    case TLS_ST_OK:
173
        /*
174
         * Its never ok to start processing handshake messages in the middle of
175
         * early data (i.e. before we've received the end of early data alert)
176
         */
177
0
        if (s->early_data_state == SSL_EARLY_DATA_READING)
178
0
            break;
179
180
0
        if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
181
0
            if (mt == SSL3_MT_CERTIFICATE) {
182
0
                st->hand_state = TLS_ST_SR_CERT;
183
0
                return 1;
184
0
            }
185
#ifndef OPENSSL_NO_COMP_ALG
186
            if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
187
                && s->ext.compress_certificate_sent) {
188
                st->hand_state = TLS_ST_SR_COMP_CERT;
189
                return 1;
190
            }
191
#endif
192
0
        }
193
194
0
        if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) {
195
0
            st->hand_state = TLS_ST_SR_KEY_UPDATE;
196
0
            return 1;
197
0
        }
198
199
0
        if (mt == DTLS13_MT_ACK) {
200
0
            st->pre_ack_hand_state = st->hand_state;
201
0
            st->hand_state = TLS_ST_SR_ACK;
202
0
            return 1;
203
0
        }
204
0
        break;
205
113
    }
206
207
    /* No valid transition found */
208
4
    return 0;
209
113
}
210
211
/*
212
 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
213
 * handshake state transitions when the server is reading messages from the
214
 * client. The message type that the client has sent is provided in |mt|. The
215
 * current state is in |s->statem.hand_state|.
216
 *
217
 * Return values are 1 for success (transition allowed) and  0 on error
218
 * (transition not allowed)
219
 */
220
int ossl_statem_server_read_transition(SSL_CONNECTION *s, int mt)
221
63.7k
{
222
63.7k
    OSSL_STATEM *st = &s->statem;
223
224
63.7k
    if (SSL_CONNECTION_IS_VERSION13(s)) {
225
113
        if (!ossl_statem_server13_read_transition(s, mt))
226
4
            goto err;
227
109
        return 1;
228
113
    }
229
230
63.6k
    switch (st->hand_state) {
231
0
    default:
232
0
        break;
233
234
14.7k
    case TLS_ST_BEFORE:
235
51.1k
    case TLS_ST_OK:
236
51.1k
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
237
51.1k
        if (mt == SSL3_MT_CLIENT_HELLO) {
238
50.8k
            st->hand_state = TLS_ST_SR_CLNT_HELLO;
239
50.8k
            return 1;
240
50.8k
        }
241
299
        break;
242
243
7.33k
    case TLS_ST_SW_SRVR_DONE:
244
        /*
245
         * If we get a CKE message after a ServerDone then either
246
         * 1) We didn't request a Certificate
247
         * OR
248
         * 2) We did request one and we allow no Certificate to be returned
249
         */
250
7.33k
        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
251
7.28k
            if (!s->s3.tmp.cert_request) {
252
7.28k
                st->hand_state = TLS_ST_SR_KEY_EXCH;
253
7.28k
                return 1;
254
7.28k
            }
255
7.28k
        } else if (s->s3.tmp.cert_request) {
256
0
            if (mt == SSL3_MT_CERTIFICATE) {
257
0
                st->hand_state = TLS_ST_SR_CERT;
258
0
                return 1;
259
0
            }
260
0
        }
261
46
        break;
262
263
46
    case TLS_ST_SR_CERT:
264
0
        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
265
0
            st->hand_state = TLS_ST_SR_KEY_EXCH;
266
0
            return 1;
267
0
        }
268
0
        break;
269
270
4.33k
    case TLS_ST_SR_KEY_EXCH:
271
        /*
272
         * We should only process a CertificateVerify message if we have
273
         * received a Certificate from the client. If so then |s->session->peer|
274
         * will be non NULL. In some instances a CertificateVerify message is
275
         * not required even if the peer has sent a Certificate (e.g. such as in
276
         * the case of static DH). In that case |st->no_cert_verify| should be
277
         * set.
278
         */
279
4.33k
        if (!received_client_cert(s) || st->no_cert_verify) {
280
4.33k
            if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
281
                /*
282
                 * For the ECDH ciphersuites when the client sends its ECDH
283
                 * pub key in a certificate, the CertificateVerify message is
284
                 * not sent. Also for GOST ciphersuites when the client uses
285
                 * its key from the certificate for key exchange.
286
                 */
287
4.28k
                st->hand_state = TLS_ST_SR_CHANGE;
288
4.28k
                return 1;
289
4.28k
            }
290
4.33k
        } else {
291
0
            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
292
0
                st->hand_state = TLS_ST_SR_CERT_VRFY;
293
0
                return 1;
294
0
            }
295
0
        }
296
48
        break;
297
298
48
    case TLS_ST_SR_CERT_VRFY:
299
0
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
300
0
            st->hand_state = TLS_ST_SR_CHANGE;
301
0
            return 1;
302
0
        }
303
0
        break;
304
305
793
    case TLS_ST_SR_CHANGE:
306
793
#ifndef OPENSSL_NO_NEXTPROTONEG
307
793
        if (s->s3.npn_seen) {
308
0
            if (mt == SSL3_MT_NEXT_PROTO) {
309
0
                st->hand_state = TLS_ST_SR_NEXT_PROTO;
310
0
                return 1;
311
0
            }
312
793
        } else {
313
793
#endif
314
793
            if (mt == SSL3_MT_FINISHED) {
315
678
                st->hand_state = TLS_ST_SR_FINISHED;
316
678
                return 1;
317
678
            }
318
793
#ifndef OPENSSL_NO_NEXTPROTONEG
319
793
        }
320
115
#endif
321
115
        break;
322
323
115
#ifndef OPENSSL_NO_NEXTPROTONEG
324
115
    case TLS_ST_SR_NEXT_PROTO:
325
0
        if (mt == SSL3_MT_FINISHED) {
326
0
            st->hand_state = TLS_ST_SR_FINISHED;
327
0
            return 1;
328
0
        }
329
0
        break;
330
0
#endif
331
332
38
    case TLS_ST_SW_FINISHED:
333
38
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
334
31
            st->hand_state = TLS_ST_SR_CHANGE;
335
31
            return 1;
336
31
        }
337
7
        break;
338
63.6k
    }
339
340
519
err:
341
    /* No valid transition found */
342
519
    SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
343
519
    return 0;
344
63.6k
}
345
346
/*
347
 * Should we send a ServerKeyExchange message?
348
 *
349
 * Valid return values are:
350
 *   1: Yes
351
 *   0: No
352
 */
353
static int send_server_key_exchange(SSL_CONNECTION *s)
354
25.6k
{
355
25.6k
    unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
356
357
    /*
358
     * only send a ServerKeyExchange if DH or fortezza but we have a
359
     * sign only certificate PSK: may send PSK identity hints For
360
     * ECC ciphersuites, we send a serverKeyExchange message only if
361
     * the cipher suite is either ECDH-anon or ECDHE. In other cases,
362
     * the server certificate contains the server's public key for
363
     * key exchange.
364
     */
365
25.6k
    if (alg_k & (SSL_kDHE | SSL_kECDHE)
366
    /*
367
     * PSK: send ServerKeyExchange if PSK identity hint if
368
     * provided
369
     */
370
12.7k
#ifndef OPENSSL_NO_PSK
371
        /* Only send SKE if we have identity hint for plain PSK */
372
12.7k
        || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
373
0
            && s->cert->psk_identity_hint)
374
        /* For other PSK always send SKE */
375
12.7k
        || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
376
12.7k
#endif
377
12.7k
#ifndef OPENSSL_NO_SRP
378
        /* SRP: send ServerKeyExchange */
379
12.7k
        || (alg_k & SSL_kSRP)
380
25.6k
#endif
381
25.6k
    ) {
382
12.9k
        return 1;
383
12.9k
    }
384
385
12.7k
    return 0;
386
25.6k
}
387
388
/*
389
 * Used to determine if we should send a CompressedCertificate message
390
 *
391
 * Returns the algorithm to use, TLSEXT_comp_cert_none means no compression
392
 */
393
static int get_compressed_certificate_alg(SSL_CONNECTION *sc)
394
2.84k
{
395
#ifndef OPENSSL_NO_COMP_ALG
396
    int *alg = sc->ext.compress_certificate_from_peer;
397
398
    if (sc->s3.tmp.cert == NULL)
399
        return TLSEXT_comp_cert_none;
400
401
    for (; *alg != TLSEXT_comp_cert_none; alg++) {
402
        if (sc->s3.tmp.cert->comp_cert[*alg] != NULL)
403
            return *alg;
404
    }
405
#endif
406
2.84k
    return TLSEXT_comp_cert_none;
407
2.84k
}
408
409
/*
410
 * Should we send a CertificateRequest message?
411
 *
412
 * Valid return values are:
413
 *   1: Yes
414
 *   0: No
415
 */
416
int send_certificate_request(SSL_CONNECTION *s)
417
11.5k
{
418
11.5k
    if (
419
        /* don't request cert unless asked for it: */
420
11.5k
        s->verify_mode & SSL_VERIFY_PEER
421
        /*
422
         * don't request if post-handshake-only unless doing
423
         * post-handshake in TLSv1.3:
424
         */
425
0
        && (!SSL_CONNECTION_IS_VERSION13(s)
426
0
            || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
427
0
            || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
428
        /*
429
         * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
430
         * a second time:
431
         */
432
0
        && (s->certreqs_sent < 1 || !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
433
        /*
434
         * never request cert in anonymous ciphersuites (see
435
         * section "Certificate request" in SSL 3 drafts and in
436
         * RFC 2246):
437
         */
438
0
        && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)
439
            /*
440
             * ... except when the application insists on
441
             * verification (against the specs, but statem_clnt.c accepts
442
             * this for SSL 3)
443
             */
444
0
            || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
445
        /* don't request certificate for SRP auth */
446
0
        && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)
447
        /*
448
         * With normal PSK Certificates and Certificate Requests
449
         * are omitted
450
         */
451
0
        && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
452
0
        return 1;
453
0
    }
454
455
11.5k
    return 0;
456
11.5k
}
457
458
/*
459
 * Get the OCSP response for the certificate from the chain identified
460
 * chainidx.
461
 * If no OCSP response could be found NULL is returned.
462
 */
463
OCSP_RESPONSE *ossl_get_ocsp_response(SSL_CONNECTION *s, int chainidx)
464
0
{
465
0
    OCSP_RESPONSE *resp = NULL;
466
0
#ifndef OPENSSL_NO_OCSP
467
0
    int i = 0, num = 0;
468
0
    unsigned int len;
469
0
    X509 *x = NULL;
470
0
    STACK_OF(X509) *chain_certs = NULL;
471
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
472
0
    OCSP_BASICRESP *bs = NULL;
473
0
    OCSP_SINGLERESP *sr = NULL;
474
0
    OCSP_CERTID *cid = NULL;
475
0
    OCSP_CERTID *sr_cert_id = NULL;
476
0
    ASN1_OBJECT *cert_id_md_oid;
477
0
    char cert_id_md_txt[OSSL_MAX_NAME_SIZE];
478
0
    EVP_MD *cert_id_md;
479
0
    ASN1_INTEGER *respSerial;
480
0
    ASN1_OCTET_STRING *respIssuerNameHash = NULL;
481
0
    ASN1_OCTET_STRING *certIssuerNameHash = NULL;
482
0
    const X509_NAME *certIssuerName;
483
0
    unsigned char md[EVP_MAX_MD_SIZE];
484
0
    const ASN1_INTEGER *certSerial;
485
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
486
487
    /*
488
     * In TLSv1.3 the caller gives the index of the certificate for which the
489
     * status message should be created.
490
     * Prior to TLSv1.3 the chain index is 0 and the body should contain only
491
     * the status of the server certificate itself.
492
     */
493
0
    SSL_get0_chain_certs(ssl, &chain_certs);
494
495
    /*
496
     * If the certificate chain was built, get the status message for the
497
     * requested certificate specified by chainidx.
498
     * SSL_get0_chain_certs provides certificate chain except the server cert.
499
     *
500
     * if chainidx = 0 the server certificate is requested
501
     * if chainidx > 0 an intermediate certificate is requested
502
     */
503
0
    if (chainidx == 0)
504
0
        x = SSL_get_certificate(ssl);
505
0
    else
506
0
        x = sk_X509_value(chain_certs, chainidx - 1);
507
0
    if (x == NULL)
508
0
        return NULL;
509
510
    /* for a selfsigned certificate there will be no OCSP response */
511
0
    if (X509_self_signed(x, 0))
512
0
        return NULL;
513
514
0
    if ((resp = sk_OCSP_RESPONSE_value(s->ext.ocsp.resp_ex, chainidx)) != NULL) {
515
        /*
516
         * Find the corresponding single OCSP response by comparing the current
517
         * certificate's serial number, and the hash of the current certificate's
518
         * issuer name, to the serial number and issuer name hash in each OCSP
519
         * response received.
520
         */
521
0
        if (OCSP_response_status(resp) == OCSP_RESPONSE_STATUS_SUCCESSFUL) {
522
            /*
523
             * Set a mark for the error queue here to be able to ignore errors
524
             * happening because of test cases.
525
             */
526
0
            ERR_set_mark();
527
0
            bs = OCSP_response_get1_basic(resp);
528
0
            if (bs != NULL && (sr = OCSP_resp_get0(bs, 0)) != NULL) {
529
                /* use the first single response to get the algorithm used */
530
0
                cid = (OCSP_CERTID *)OCSP_SINGLERESP_get0_id(sr);
531
532
                /* determine the md algorithm which was used to create cert id */
533
0
                OCSP_id_get0_info(&respIssuerNameHash, &cert_id_md_oid, NULL, &respSerial, cid);
534
0
                if (cert_id_md_oid != NULL) {
535
0
                    OBJ_obj2txt(cert_id_md_txt, sizeof(cert_id_md_txt), cert_id_md_oid, 0);
536
0
                    cert_id_md = EVP_MD_fetch(sctx->libctx, cert_id_md_txt, sctx->propq);
537
0
                } else {
538
0
                    cert_id_md = EVP_MD_fetch(sctx->libctx, SN_sha1, sctx->propq);
539
0
                }
540
541
0
                if (cert_id_md == NULL) {
542
0
                    OCSP_BASICRESP_free(bs);
543
0
                    ERR_clear_last_mark();
544
0
                    return NULL;
545
0
                }
546
547
                /* get serial number and issuer name hash of the certificate from the chain */
548
0
                certSerial = X509_get0_serialNumber(x);
549
0
                certIssuerName = X509_get_issuer_name(x);
550
0
                certIssuerNameHash = ASN1_OCTET_STRING_new();
551
0
                if (!X509_NAME_digest(certIssuerName, cert_id_md, md, &len) || !(ASN1_OCTET_STRING_set(certIssuerNameHash, md, len))) {
552
0
                    ASN1_OCTET_STRING_free(certIssuerNameHash);
553
0
                    OCSP_BASICRESP_free(bs);
554
0
                    EVP_MD_free(cert_id_md);
555
0
                    ERR_clear_last_mark();
556
0
                    return NULL;
557
0
                }
558
559
0
                num = OCSP_resp_count(bs);
560
0
                for (i = 0; i < num; i++) {
561
0
                    sr = OCSP_resp_get0(bs, i);
562
563
                    /*
564
                     * get the CertID from the OCSP response to compare it with the information
565
                     * from the certificate
566
                     */
567
0
                    sr_cert_id = (OCSP_CERTID *)OCSP_SINGLERESP_get0_id(sr);
568
569
0
                    OCSP_id_get0_info(&respIssuerNameHash, NULL, NULL, &respSerial, sr_cert_id);
570
571
0
                    if (!ASN1_INTEGER_cmp(certSerial, respSerial) && !ASN1_OCTET_STRING_cmp(certIssuerNameHash, respIssuerNameHash))
572
0
                        break;
573
0
                }
574
575
0
                ASN1_OCTET_STRING_free(certIssuerNameHash);
576
0
                OCSP_BASICRESP_free(bs);
577
0
                EVP_MD_free(cert_id_md);
578
579
                /*
580
                 * if we did not find the right single response we return NULL here
581
                 */
582
0
                if (i == num)
583
0
                    resp = NULL;
584
0
            } else {
585
0
                OCSP_BASICRESP_free(bs);
586
0
            }
587
588
            /*
589
             * in a test case a response without a basic response is used the error set
590
             * could be ignored here
591
             */
592
0
            ERR_pop_to_mark();
593
0
        }
594
0
    }
595
0
#endif
596
597
0
    return resp;
598
0
}
599
600
static int do_compressed_cert(SSL_CONNECTION *sc)
601
2.84k
{
602
    /* If we negotiated RPK, we won't attempt to compress it */
603
2.84k
    return sc->ext.server_cert_type == TLSEXT_cert_type_x509
604
2.84k
        && get_compressed_certificate_alg(sc) != TLSEXT_comp_cert_none;
605
2.84k
}
606
607
/*
608
 * ossl_statem_server13_write_transition() works out what handshake state to
609
 * move to next when a TLSv1.3 server is writing messages to be sent to the
610
 * client.
611
 */
612
static WRITE_TRAN ossl_statem_server13_write_transition(SSL_CONNECTION *s)
613
8.68k
{
614
8.68k
    OSSL_HANDSHAKE_STATE next_state;
615
8.68k
    OSSL_STATEM *st = &s->statem;
616
617
8.68k
    if (st->ack_for_retransmit && st->hand_state != TLS_ST_SW_ACK) {
618
0
        st->deferred_ack_state = st->hand_state;
619
0
        st->hand_state = TLS_ST_SW_ACK;
620
0
        return WRITE_TRAN_CONTINUE;
621
0
    }
622
623
    /*
624
     * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
625
     * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
626
     */
627
628
8.68k
    switch (st->hand_state) {
629
0
    default:
630
        /* Shouldn't happen */
631
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
632
0
        return WRITE_TRAN_ERROR;
633
634
0
    case TLS_ST_OK:
635
0
        if (s->key_update != SSL_KEY_UPDATE_NONE) {
636
0
            st->hand_state = TLS_ST_SW_KEY_UPDATE;
637
0
            return WRITE_TRAN_CONTINUE;
638
0
        }
639
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
640
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
641
0
            return WRITE_TRAN_CONTINUE;
642
0
        }
643
0
        if (s->ext.extra_tickets_expected > 0) {
644
0
            st->hand_state = TLS_ST_SW_SESSION_TICKET;
645
0
            return WRITE_TRAN_CONTINUE;
646
0
        }
647
        /* Try to read from the client instead */
648
0
        return WRITE_TRAN_FINISHED;
649
650
1.17k
    case TLS_ST_SR_CLNT_HELLO:
651
1.17k
        st->hand_state = TLS_ST_SW_SRVR_HELLO;
652
1.17k
        return WRITE_TRAN_CONTINUE;
653
654
1.17k
    case TLS_ST_SW_SRVR_HELLO:
655
1.17k
        if (SSL_CONNECTION_MIDDLEBOX_IS_ENABLED(s)
656
1.17k
            && s->hello_retry_request != SSL_HRR_COMPLETE)
657
1.15k
            st->hand_state = TLS_ST_SW_CHANGE;
658
21
        else if (s->hello_retry_request == SSL_HRR_PENDING)
659
0
            st->hand_state = TLS_ST_EARLY_DATA;
660
21
        else
661
21
            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
662
1.17k
        return WRITE_TRAN_CONTINUE;
663
664
1.15k
    case TLS_ST_SW_CHANGE:
665
1.15k
        if (s->hello_retry_request == SSL_HRR_PENDING)
666
170
            st->hand_state = TLS_ST_EARLY_DATA;
667
981
        else
668
981
            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
669
1.15k
        return WRITE_TRAN_CONTINUE;
670
671
1.00k
    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
672
1.00k
        if (s->hit)
673
0
            st->hand_state = TLS_ST_SW_FINISHED;
674
1.00k
        else if (send_certificate_request(s))
675
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
676
1.00k
        else if (do_compressed_cert(s))
677
0
            st->hand_state = TLS_ST_SW_COMP_CERT;
678
1.00k
        else
679
1.00k
            st->hand_state = TLS_ST_SW_CERT;
680
681
1.00k
        return WRITE_TRAN_CONTINUE;
682
683
0
    case TLS_ST_SW_CERT_REQ:
684
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
685
0
            s->post_handshake_auth = SSL_PHA_REQUESTED;
686
0
            st->hand_state = TLS_ST_OK;
687
0
        } else if (do_compressed_cert(s)) {
688
0
            st->hand_state = TLS_ST_SW_COMP_CERT;
689
0
        } else {
690
0
            st->hand_state = TLS_ST_SW_CERT;
691
0
        }
692
0
        return WRITE_TRAN_CONTINUE;
693
694
0
    case TLS_ST_SW_COMP_CERT:
695
1.00k
    case TLS_ST_SW_CERT:
696
1.00k
        st->hand_state = TLS_ST_SW_CERT_VRFY;
697
1.00k
        return WRITE_TRAN_CONTINUE;
698
699
1.00k
    case TLS_ST_SW_CERT_VRFY:
700
1.00k
        st->hand_state = TLS_ST_SW_FINISHED;
701
1.00k
        return WRITE_TRAN_CONTINUE;
702
703
1.00k
    case TLS_ST_SW_FINISHED:
704
1.00k
        st->hand_state = TLS_ST_EARLY_DATA;
705
1.00k
        s->ts_msg_write = ossl_time_now();
706
1.00k
        return WRITE_TRAN_CONTINUE;
707
708
1.17k
    case TLS_ST_EARLY_DATA:
709
1.17k
        return WRITE_TRAN_FINISHED;
710
711
0
    case TLS_ST_SR_FINISHED:
712
0
        s->ts_msg_read = ossl_time_now();
713
        /*
714
         * Technically we have finished the handshake at this point, but we're
715
         * going to remain "in_init" for now and write out any session tickets
716
         * immediately.
717
         */
718
0
        if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
719
0
            s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
720
0
        } else if (!s->ext.ticket_expected) {
721
            /*
722
             * If we're not going to renew the ticket then we just finish the
723
             * handshake at this point.
724
             */
725
0
            if (SSL_CONNECTION_IS_DTLS13(s)) {
726
0
                st->deferred_ack_state = TLS_ST_OK;
727
0
                st->hand_state = TLS_ST_SW_ACK;
728
0
            } else {
729
0
                st->hand_state = TLS_ST_OK;
730
0
            }
731
732
0
            return WRITE_TRAN_CONTINUE;
733
0
        }
734
        /*
735
         * Do not issue TLS 1.3 session tickets if the server has explicitly
736
         * disabled them via SSL_OP_NO_TICKET and also disabled the session
737
         * cache with SSL_SESS_CACHE_OFF. Together, these settings clearly
738
         * indicate an intent to suppress session resumption; sending
739
         * NewSessionTicket messages in this case would be wasteful and
740
         * misleading.
741
         *
742
         * From the server’s perspective, a client that does not advertise
743
         * psk_key_exchange_modes in TLS 1.3, or that sends it with RFC 9149
744
         * parameters such as new_session_count = 0 or resumption_count = 0, is
745
         * effectively signaling no interest in session tickets or resumption.
746
         *
747
         * RFC 9846 section 4.3.9: Servers MUST NOT select a key exchange mode
748
         * that is not listed by the client. This extension also restricts the
749
         * modes for use with PSK resumption. Servers SHOULD NOT send
750
         * NewSessionTicket with tickets that are not compatible with the
751
         * advertised modes; however, if a server does so, the impact will just
752
         * be that the client's attempts at resumption fail.
753
         *
754
         * Note: Although RFC 9149 allows clients to signal no interest in
755
         * session tickets or resumption (e.g. new_session_count = 0 or
756
         * resumption_count = 0), this implementation does not currently
757
         * interpret or enforce those parameters.
758
         *
759
         * Also skip issuance when SSL_VERIFY_PEER is set with no sid_ctx
760
         * configured: any ticket minted here would be rejected by
761
         * ssl_get_prev_session() in that configuration.
762
         */
763
0
        if (s->num_tickets <= s->sent_tickets
764
0
            || ((s->options & SSL_OP_NO_TICKET) != 0
765
0
                && (SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER)
766
0
                    == 0)
767
0
            || s->ext.psk_kex_mode == TLSEXT_KEX_MODE_FLAG_NONE
768
0
            || ((s->verify_mode & SSL_VERIFY_PEER) != 0 && s->sid_ctx_length == 0))
769
0
            next_state = TLS_ST_OK;
770
0
        else
771
0
            next_state = TLS_ST_SW_SESSION_TICKET;
772
773
0
        if (SSL_CONNECTION_IS_DTLS13(s)) {
774
0
            st->deferred_ack_state = next_state;
775
0
            st->hand_state = TLS_ST_SW_ACK;
776
0
        } else {
777
0
            st->hand_state = next_state;
778
0
        }
779
0
        return WRITE_TRAN_CONTINUE;
780
781
0
    case TLS_ST_SR_KEY_UPDATE:
782
0
        if (SSL_CONNECTION_IS_DTLS13(s)) {
783
0
            st->deferred_ack_state = TLS_ST_OK;
784
0
            st->hand_state = TLS_ST_SW_ACK;
785
0
            return WRITE_TRAN_CONTINUE;
786
0
        }
787
        /* Fall through */
788
0
    case TLS_ST_SW_KEY_UPDATE:
789
0
        if (SSL_CONNECTION_IS_DTLS13(s))
790
            /* We wait for ACK */
791
0
            return WRITE_TRAN_FINISHED;
792
0
        else
793
0
            st->hand_state = TLS_ST_OK;
794
0
        return WRITE_TRAN_CONTINUE;
795
796
0
    case TLS_ST_SW_SESSION_TICKET:
797
        /* In a resumption we only ever send a maximum of one new ticket.
798
         * Following an initial handshake we send the number of tickets we have
799
         * been configured for.
800
         */
801
0
        if ((SSL_IS_FIRST_HANDSHAKE(s) || s->ext.extra_tickets_expected <= 0)
802
0
            && (s->hit || s->num_tickets <= s->sent_tickets)) {
803
            /* We've written enough tickets out. */
804
0
            st->hand_state = TLS_ST_OK;
805
0
        }
806
0
        return WRITE_TRAN_CONTINUE;
807
808
0
    case TLS_ST_SR_ACK:
809
0
        if (SSL_CONNECTION_IS_DTLS13(s)
810
0
            && dtls_any_sent_messages_are_missing_acknowledge(s)) {
811
            /* We wait for ACK */
812
0
            return WRITE_TRAN_FINISHED;
813
0
        }
814
0
        st->hand_state = TLS_ST_OK;
815
0
        return WRITE_TRAN_CONTINUE;
816
817
0
    case TLS_ST_SW_ACK:
818
0
        st->hand_state = st->deferred_ack_state;
819
0
        if (st->ack_for_retransmit) {
820
0
            st->ack_for_retransmit = 0;
821
0
            return WRITE_TRAN_FINISHED;
822
0
        }
823
824
0
        return WRITE_TRAN_CONTINUE;
825
8.68k
    }
826
8.68k
}
827
828
/*
829
 * ossl_statem_server_write_transition() works out what handshake state to move
830
 * to next when the server is writing messages to be sent to the client.
831
 */
832
WRITE_TRAN ossl_statem_server_write_transition(SSL_CONNECTION *s)
833
143k
{
834
143k
    OSSL_STATEM *st = &s->statem;
835
836
    /*
837
     * Note that before the ClientHello we don't know what version we are going
838
     * to negotiate yet, so we don't take this branch until later
839
     */
840
841
143k
    if (SSL_CONNECTION_IS_VERSION13(s))
842
8.68k
        return ossl_statem_server13_write_transition(s);
843
844
135k
    switch (st->hand_state) {
845
0
    default:
846
        /* Shouldn't happen */
847
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
848
0
        return WRITE_TRAN_ERROR;
849
850
36.3k
    case TLS_ST_OK:
851
36.3k
        if (st->request_state == TLS_ST_SW_HELLO_REQ) {
852
            /* We must be trying to renegotiate */
853
0
            st->hand_state = TLS_ST_SW_HELLO_REQ;
854
0
            st->request_state = TLS_ST_BEFORE;
855
0
            return WRITE_TRAN_CONTINUE;
856
0
        }
857
        /* Must be an incoming ClientHello */
858
36.3k
        if (!tls_setup_handshake(s)) {
859
            /* SSLfatal() already called */
860
0
            return WRITE_TRAN_ERROR;
861
0
        }
862
        /* Fall through */
863
864
53.7k
    case TLS_ST_BEFORE:
865
        /* Just go straight to trying to read from the client */
866
53.7k
        return WRITE_TRAN_FINISHED;
867
868
0
    case TLS_ST_SW_HELLO_REQ:
869
0
        st->hand_state = TLS_ST_OK;
870
0
        return WRITE_TRAN_CONTINUE;
871
872
45.6k
    case TLS_ST_SR_CLNT_HELLO:
873
45.6k
        if (SSL_CONNECTION_IS_DTLS(s) && !SSL_CONNECTION_IS_DTLS13(s) && !s->d1->cookie_verified
874
5.52k
            && (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)) {
875
0
            st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
876
45.6k
        } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
877
            /* We must have rejected the renegotiation */
878
35.9k
            st->hand_state = TLS_ST_OK;
879
35.9k
            return WRITE_TRAN_CONTINUE;
880
35.9k
        } else {
881
9.70k
            st->hand_state = TLS_ST_SW_SRVR_HELLO;
882
9.70k
        }
883
9.70k
        return WRITE_TRAN_CONTINUE;
884
885
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
886
0
        return WRITE_TRAN_FINISHED;
887
888
9.67k
    case TLS_ST_SW_SRVR_HELLO:
889
9.67k
        if (s->hit) {
890
59
            if (s->ext.ticket_expected)
891
0
                st->hand_state = TLS_ST_SW_SESSION_TICKET;
892
59
            else
893
59
                st->hand_state = TLS_ST_SW_CHANGE;
894
9.61k
        } else {
895
            /* Check if it is anon DH or anon ECDH, */
896
            /* normal PSK or SRP */
897
9.61k
            if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
898
8.70k
                st->hand_state = TLS_ST_SW_CERT;
899
8.70k
            } else if (send_server_key_exchange(s)) {
900
909
                st->hand_state = TLS_ST_SW_KEY_EXCH;
901
909
            } else if (send_certificate_request(s)) {
902
0
                st->hand_state = TLS_ST_SW_CERT_REQ;
903
0
            } else {
904
0
                st->hand_state = TLS_ST_SW_SRVR_DONE;
905
0
            }
906
9.61k
        }
907
9.67k
        return WRITE_TRAN_CONTINUE;
908
909
8.70k
    case TLS_ST_SW_CERT:
910
8.70k
        if (s->ext.status_expected) {
911
0
            st->hand_state = TLS_ST_SW_CERT_STATUS;
912
0
            return WRITE_TRAN_CONTINUE;
913
0
        }
914
        /* Fall through */
915
916
8.70k
    case TLS_ST_SW_CERT_STATUS:
917
8.70k
        if (send_server_key_exchange(s)) {
918
4.74k
            st->hand_state = TLS_ST_SW_KEY_EXCH;
919
4.74k
            return WRITE_TRAN_CONTINUE;
920
4.74k
        }
921
        /* Fall through */
922
923
9.58k
    case TLS_ST_SW_KEY_EXCH:
924
9.58k
        if (send_certificate_request(s)) {
925
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
926
0
            return WRITE_TRAN_CONTINUE;
927
0
        }
928
        /* Fall through */
929
930
9.58k
    case TLS_ST_SW_CERT_REQ:
931
9.58k
        st->hand_state = TLS_ST_SW_SRVR_DONE;
932
9.58k
        return WRITE_TRAN_CONTINUE;
933
934
9.58k
    case TLS_ST_SW_SRVR_DONE:
935
9.58k
        s->ts_msg_write = ossl_time_now();
936
9.58k
        return WRITE_TRAN_FINISHED;
937
938
645
    case TLS_ST_SR_FINISHED:
939
645
        s->ts_msg_read = ossl_time_now();
940
645
        if (s->hit) {
941
0
            st->hand_state = TLS_ST_OK;
942
0
            return WRITE_TRAN_CONTINUE;
943
645
        } else if (s->ext.ticket_expected) {
944
218
            st->hand_state = TLS_ST_SW_SESSION_TICKET;
945
427
        } else {
946
427
            st->hand_state = TLS_ST_SW_CHANGE;
947
427
        }
948
645
        return WRITE_TRAN_CONTINUE;
949
950
218
    case TLS_ST_SW_SESSION_TICKET:
951
218
        st->hand_state = TLS_ST_SW_CHANGE;
952
218
        return WRITE_TRAN_CONTINUE;
953
954
704
    case TLS_ST_SW_CHANGE:
955
704
        st->hand_state = TLS_ST_SW_FINISHED;
956
704
        return WRITE_TRAN_CONTINUE;
957
958
704
    case TLS_ST_SW_FINISHED:
959
704
        if (s->hit) {
960
59
            return WRITE_TRAN_FINISHED;
961
59
        }
962
645
        st->hand_state = TLS_ST_OK;
963
645
        return WRITE_TRAN_CONTINUE;
964
135k
    }
965
135k
}
966
967
static int ossl_statem_dtls_server13_use_timer(SSL_CONNECTION *s)
968
0
{
969
0
    OSSL_STATEM *st = &s->statem;
970
971
0
    switch (st->hand_state) {
972
0
    default:
973
0
        break;
974
975
0
    case TLS_ST_SW_ACK:
976
        /* Fall through */
977
978
0
    case TLS_ST_OK:
979
0
        return 0;
980
0
    }
981
982
0
    return 1;
983
0
}
984
985
int ossl_statem_dtls_server_use_timer(SSL_CONNECTION *s)
986
18.9k
{
987
18.9k
    OSSL_STATEM *st = &s->statem;
988
989
18.9k
    if (SSL_CONNECTION_IS_DTLS13(s))
990
0
        return ossl_statem_dtls_server13_use_timer(s);
991
992
18.9k
    switch (st->hand_state) {
993
13.4k
    default:
994
13.4k
        break;
995
996
13.4k
    case TLS_ST_SW_SESSION_TICKET:
997
        /*
998
         * We're into the last flight. We don't retransmit the last flight
999
         * unless we need to, so we don't use the timer
1000
         */
1001
0
        st->use_timer = 0;
1002
0
        break;
1003
1004
0
    case TLS_ST_SW_CHANGE:
1005
        /*
1006
         * We're into the last flight. We don't retransmit the last flight
1007
         * unless we need to, so we don't use the timer. This might have
1008
         * already been set to 0 if we sent a NewSessionTicket message,
1009
         * but we'll set it again here in case we didn't.
1010
         */
1011
0
        st->use_timer = 0;
1012
0
        break;
1013
1014
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1015
        /* We don't buffer this message so don't use the timer */
1016
0
        st->use_timer = 0;
1017
0
        break;
1018
1019
5.50k
    case TLS_ST_SW_SRVR_HELLO:
1020
        /*
1021
         * Messages we write from now on should be buffered and
1022
         * retransmitted if necessary, so we need to use the timer now
1023
         */
1024
5.50k
        st->use_timer = 1;
1025
5.50k
        break;
1026
18.9k
    }
1027
1028
18.9k
    return st->use_timer;
1029
18.9k
}
1030
1031
/*
1032
 * Perform any pre work that needs to be done prior to sending a message from
1033
 * the server to the client.
1034
 */
1035
WORK_STATE ossl_statem_server_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
1036
79.3k
{
1037
79.3k
    OSSL_STATEM *st = &s->statem;
1038
79.3k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1039
1040
79.3k
    switch (st->hand_state) {
1041
29.9k
    default:
1042
        /* No pre work to be done */
1043
29.9k
        break;
1044
1045
29.9k
    case TLS_ST_SW_HELLO_REQ:
1046
        /* fall-through */
1047
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1048
0
        s->shutdown = 0;
1049
0
        if (SSL_CONNECTION_IS_DTLS(s))
1050
0
            dtls1_clear_sent_buffer(s, 0);
1051
1052
0
        break;
1053
1054
9.58k
    case TLS_ST_SW_SRVR_DONE:
1055
#ifndef OPENSSL_NO_SCTP
1056
        if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) {
1057
            /* Calls SSLfatal() as required */
1058
            return dtls_wait_for_dry(s);
1059
        }
1060
#endif
1061
9.58k
        return WORK_FINISHED_CONTINUE;
1062
1063
218
    case TLS_ST_SW_SESSION_TICKET:
1064
218
        if (SSL_CONNECTION_IS_VERSION13(s) && s->sent_tickets == 0
1065
0
            && s->ext.extra_tickets_expected == 0) {
1066
            /*
1067
             * Actually this is the end of the handshake, but we're going
1068
             * straight into writing the session ticket out. So we finish off
1069
             * the handshake, but keep the various buffers active.
1070
             *
1071
             * Calls SSLfatal as required.
1072
             */
1073
0
            return tls_finish_handshake(s, wst, 0, 0);
1074
0
        }
1075
218
        break;
1076
1077
1.85k
    case TLS_ST_SW_CHANGE:
1078
1.85k
        if (SSL_CONNECTION_IS_VERSION13(s))
1079
1.15k
            break;
1080
        /* Writes to s->session are only safe for initial handshakes */
1081
704
        if (s->session->cipher == NULL) {
1082
0
            s->session->cipher = s->s3.tmp.new_cipher;
1083
704
        } else if (s->session->cipher != s->s3.tmp.new_cipher) {
1084
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1085
0
            return WORK_ERROR;
1086
0
        }
1087
704
        if (!ssl->method->ssl3_enc->setup_key_block(s)) {
1088
            /* SSLfatal() already called */
1089
0
            return WORK_ERROR;
1090
0
        }
1091
704
        return WORK_FINISHED_CONTINUE;
1092
1093
1.17k
    case TLS_ST_EARLY_DATA:
1094
1.17k
        if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
1095
708
            && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
1096
708
            return WORK_FINISHED_CONTINUE;
1097
1098
        /*
1099
         * In QUIC with 0-RTT we just carry on when otherwise we would stop
1100
         * to allow the server to read early data
1101
         */
1102
464
        if (SSL_IS_QUIC_HANDSHAKE(s) && s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
1103
0
            && s->early_data_state != SSL_EARLY_DATA_FINISHED_READING) {
1104
0
            s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
1105
0
            if (!ssl->method->ssl3_enc->change_cipher_state(s, SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
1106
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1107
0
                return WORK_ERROR;
1108
0
            }
1109
0
            return WORK_FINISHED_SWAP;
1110
0
        }
1111
        /* Fall through */
1112
1113
37.0k
    case TLS_ST_OK:
1114
        /* Calls SSLfatal() as required */
1115
37.0k
        return tls_finish_handshake(s, wst, 1, 1);
1116
79.3k
    }
1117
1118
31.3k
    return WORK_FINISHED_CONTINUE;
1119
79.3k
}
1120
1121
static ossl_inline int conn_is_closed(void)
1122
0
{
1123
0
    switch (get_last_sys_error()) {
1124
0
#if defined(EPIPE)
1125
0
    case EPIPE:
1126
0
        return 1;
1127
0
#endif
1128
0
#if defined(ECONNRESET)
1129
0
    case ECONNRESET:
1130
0
        return 1;
1131
0
#endif
1132
#if defined(WSAECONNRESET)
1133
    case WSAECONNRESET:
1134
        return 1;
1135
#endif
1136
0
    default:
1137
0
        return 0;
1138
0
    }
1139
0
}
1140
1141
/*
1142
 * Perform any work that needs to be done after sending a message from the
1143
 * server to the client.
1144
 */
1145
WORK_STATE ossl_statem_server_post_work(SSL_CONNECTION *s, WORK_STATE wst)
1146
42.2k
{
1147
42.2k
    OSSL_STATEM *st = &s->statem;
1148
42.2k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1149
1150
42.2k
    s->init_num = 0;
1151
1152
42.2k
    switch (st->hand_state) {
1153
17.0k
    default:
1154
        /* No post work to be done */
1155
17.0k
        break;
1156
1157
17.0k
    case TLS_ST_SW_HELLO_REQ:
1158
0
        if (statem_flush(s) != 1)
1159
0
            return WORK_MORE_A;
1160
0
        if (!ssl3_init_finished_mac(s)) {
1161
            /* SSLfatal() already called */
1162
0
            return WORK_ERROR;
1163
0
        }
1164
0
        break;
1165
1166
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1167
0
        if (statem_flush(s) != 1)
1168
0
            return WORK_MORE_A;
1169
        /* HelloVerifyRequest resets Finished MAC */
1170
0
        if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
1171
            /* SSLfatal() already called */
1172
0
            return WORK_ERROR;
1173
0
        }
1174
        /*
1175
         * The next message should be another ClientHello which we need to
1176
         * treat like it was the first packet
1177
         */
1178
0
        s->first_packet = 1;
1179
0
        break;
1180
1181
10.8k
    case TLS_ST_SW_SRVR_HELLO:
1182
10.8k
        if (SSL_CONNECTION_IS_VERSION13(s)
1183
1.17k
            && s->hello_retry_request == SSL_HRR_PENDING) {
1184
170
            if (!SSL_CONNECTION_MIDDLEBOX_IS_ENABLED(s)
1185
0
                && statem_flush(s) != 1)
1186
0
                return WORK_MORE_A;
1187
170
            break;
1188
170
        }
1189
#ifndef OPENSSL_NO_SCTP
1190
        /*
1191
         * Before exporting the SCTP auth key we check if DTLSv1.3 has been negotiated
1192
         * which is not supported.
1193
         * Refer to draft-tuexen-tsvwg-rfc6083-bis-04 for more info.
1194
         */
1195
        if (SSL_CONNECTION_IS_DTLS(s) && !SSL_CONNECTION_IS_DTLS13(s) && s->hit) {
1196
            unsigned char sctpauthkey[64];
1197
            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1198
            size_t labellen;
1199
1200
            /*
1201
             * Add new shared key for SCTP-Auth, will be ignored if no
1202
             * SCTP used.
1203
             */
1204
            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1205
                sizeof(DTLS1_SCTP_AUTH_LABEL));
1206
1207
            /* Don't include the terminating zero. */
1208
            labellen = sizeof(labelbuffer) - 1;
1209
            if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
1210
                labellen += 1;
1211
1212
            if (SSL_export_keying_material(ssl, sctpauthkey,
1213
                    sizeof(sctpauthkey), labelbuffer,
1214
                    labellen, NULL, 0,
1215
                    0)
1216
                <= 0) {
1217
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1218
                return WORK_ERROR;
1219
            }
1220
1221
            BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1222
                sizeof(sctpauthkey), sctpauthkey);
1223
        }
1224
#endif
1225
10.6k
        if (!SSL_CONNECTION_IS_VERSION13(s)
1226
1.00k
            || (SSL_CONNECTION_MIDDLEBOX_IS_ENABLED(s)
1227
1.00k
                && s->hello_retry_request != SSL_HRR_COMPLETE))
1228
10.6k
            break;
1229
        /* Fall through */
1230
1231
1.87k
    case TLS_ST_SW_CHANGE:
1232
1.87k
        if (s->hello_retry_request == SSL_HRR_PENDING) {
1233
170
            if (!statem_flush(s))
1234
0
                return WORK_MORE_A;
1235
170
            break;
1236
170
        }
1237
1238
1.70k
        if (SSL_CONNECTION_IS_VERSION13(s)) {
1239
1.00k
            if (!ssl->method->ssl3_enc->setup_key_block(s)
1240
1.00k
                || !tls13_store_handshake_traffic_hash(s)
1241
1.00k
                || !ssl->method->ssl3_enc->change_cipher_state(s,
1242
1.00k
                    SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
1243
                /* SSLfatal() already called */
1244
0
                return WORK_ERROR;
1245
0
            }
1246
1247
1.00k
            if (SSL_CONNECTION_IS_DTLS13(s)) {
1248
0
                dtls13_hellos_done(s);
1249
1.00k
            } else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
1250
1.00k
                && !ssl->method->ssl3_enc->change_cipher_state(s,
1251
1.00k
                    SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
1252
                /* SSLfatal() already called */
1253
0
                return WORK_ERROR;
1254
0
            }
1255
            /*
1256
             * We don't yet know whether the next record we are going to receive
1257
             * is an unencrypted alert, an encrypted alert, or an encrypted
1258
             * handshake message. We temporarily tolerate unencrypted alerts.
1259
             */
1260
1.00k
            if (s->rlayer.rrlmethod->set_plain_alerts != NULL)
1261
1.00k
                s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 1);
1262
1.00k
            break;
1263
1.00k
        }
1264
1265
#ifndef OPENSSL_NO_SCTP
1266
        if (SSL_CONNECTION_IS_DTLS(s) && !s->hit) {
1267
            /*
1268
             * Change to new shared key of SCTP-Auth, will be ignored if
1269
             * no SCTP used.
1270
             */
1271
            BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
1272
                0, NULL);
1273
        }
1274
#endif
1275
704
        if (!ssl->method->ssl3_enc->change_cipher_state(s,
1276
704
                SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
1277
            /* SSLfatal() already called */
1278
0
            return WORK_ERROR;
1279
0
        }
1280
704
        break;
1281
1282
9.58k
    case TLS_ST_SW_SRVR_DONE:
1283
9.58k
        if (statem_flush(s) != 1)
1284
0
            return WORK_MORE_A;
1285
9.58k
        break;
1286
1287
9.58k
    case TLS_ST_SW_FINISHED:
1288
1.70k
        if (statem_flush(s) != 1)
1289
0
            return WORK_MORE_A;
1290
#ifndef OPENSSL_NO_SCTP
1291
        if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
1292
            /*
1293
             * Change to new shared key of SCTP-Auth, will be ignored if
1294
             * no SCTP used.
1295
             */
1296
            BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
1297
                0, NULL);
1298
        }
1299
#endif
1300
1.70k
        if (SSL_CONNECTION_IS_VERSION13(s)) {
1301
            /* (D)TLS 1.3 gets the secret size from the handshake md */
1302
1.00k
            size_t dummy;
1303
1.00k
            if (!ssl->method->ssl3_enc->generate_master_secret(s,
1304
1.00k
                    s->master_secret, s->handshake_secret, 0,
1305
1.00k
                    &dummy)
1306
1.00k
                || !tls13_store_server_finished_hash(s)
1307
1.00k
                || !ssl->method->ssl3_enc->change_cipher_state(s,
1308
1.00k
                    SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
1309
                /* SSLfatal() already called */
1310
0
                return WORK_ERROR;
1311
1.00k
        }
1312
1.70k
        break;
1313
1314
1.70k
    case TLS_ST_SW_CERT_REQ:
1315
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
1316
0
            if (statem_flush(s) != 1)
1317
0
                return WORK_MORE_A;
1318
0
        } else {
1319
0
            if (!SSL_CONNECTION_IS_VERSION13(s)
1320
0
                || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
1321
0
                s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
1322
0
        }
1323
0
        break;
1324
1325
1.00k
    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
1326
1.00k
        if (!s->hit && !send_certificate_request(s)) {
1327
1.00k
            if (!SSL_CONNECTION_IS_VERSION13(s)
1328
1.00k
                || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
1329
0
                s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
1330
1.00k
        }
1331
1.00k
        break;
1332
1333
0
    case TLS_ST_SW_KEY_UPDATE:
1334
0
        if (statem_flush(s) != 1)
1335
0
            return WORK_MORE_A;
1336
0
        if (!tls13_update_key(s, 1)) {
1337
            /* SSLfatal() already called */
1338
0
            return WORK_ERROR;
1339
0
        }
1340
0
        break;
1341
1342
218
    case TLS_ST_SW_SESSION_TICKET:
1343
218
        clear_sys_error();
1344
218
        if (SSL_CONNECTION_IS_VERSION13(s) && statem_flush(s) != 1) {
1345
0
            if (SSL_get_error(ssl, 0) == SSL_ERROR_SYSCALL
1346
0
                && conn_is_closed()) {
1347
                /*
1348
                 * We ignore connection closed errors in TLSv1.3 when sending a
1349
                 * NewSessionTicket and behave as if we were successful. This is
1350
                 * so that we are still able to read data sent to us by a client
1351
                 * that closes soon after the end of the handshake without
1352
                 * waiting to read our post-handshake NewSessionTickets.
1353
                 */
1354
0
                s->rwstate = SSL_NOTHING;
1355
0
                break;
1356
0
            }
1357
1358
0
            return WORK_MORE_A;
1359
0
        }
1360
218
        ERR_clear_last_mark();
1361
218
        break;
1362
1363
0
    case TLS_ST_SW_ACK:
1364
0
        if (statem_flush(s) != 1)
1365
0
            return WORK_MORE_A;
1366
0
        break;
1367
42.2k
    }
1368
1369
42.2k
    return WORK_FINISHED_CONTINUE;
1370
42.2k
}
1371
1372
/*
1373
 * Get the message construction function and message type for sending from the
1374
 * server
1375
 *
1376
 * Valid return values are:
1377
 *   1: Success
1378
 *   0: Error
1379
 */
1380
int ossl_statem_server_construct_message(SSL_CONNECTION *s,
1381
    confunc_f *confunc, int *mt)
1382
42.3k
{
1383
42.3k
    OSSL_STATEM *st = &s->statem;
1384
1385
42.3k
    switch (st->hand_state) {
1386
0
    default:
1387
        /* Shouldn't happen */
1388
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
1389
0
        return 0;
1390
1391
1.85k
    case TLS_ST_SW_CHANGE:
1392
1.85k
        if (SSL_CONNECTION_IS_DTLS(s))
1393
0
            *confunc = dtls_construct_change_cipher_spec;
1394
1.85k
        else
1395
1.85k
            *confunc = tls_construct_change_cipher_spec;
1396
1.85k
        *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1397
1.85k
        break;
1398
1399
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1400
0
        *confunc = dtls_construct_hello_verify_request;
1401
0
        *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
1402
0
        break;
1403
1404
0
    case TLS_ST_SW_HELLO_REQ:
1405
        /* No construction function needed */
1406
0
        *confunc = NULL;
1407
0
        *mt = SSL3_MT_HELLO_REQUEST;
1408
0
        break;
1409
1410
10.8k
    case TLS_ST_SW_SRVR_HELLO:
1411
10.8k
        *confunc = tls_construct_server_hello;
1412
10.8k
        *mt = SSL3_MT_SERVER_HELLO;
1413
10.8k
        break;
1414
1415
9.70k
    case TLS_ST_SW_CERT:
1416
9.70k
        *confunc = tls_construct_server_certificate;
1417
9.70k
        *mt = SSL3_MT_CERTIFICATE;
1418
9.70k
        break;
1419
1420
#ifndef OPENSSL_NO_COMP_ALG
1421
    case TLS_ST_SW_COMP_CERT:
1422
        *confunc = tls_construct_server_compressed_certificate;
1423
        *mt = SSL3_MT_COMPRESSED_CERTIFICATE;
1424
        break;
1425
#endif
1426
1427
1.00k
    case TLS_ST_SW_CERT_VRFY:
1428
1.00k
        *confunc = tls_construct_cert_verify;
1429
1.00k
        *mt = SSL3_MT_CERTIFICATE_VERIFY;
1430
1.00k
        break;
1431
1432
5.65k
    case TLS_ST_SW_KEY_EXCH:
1433
5.65k
        *confunc = tls_construct_server_key_exchange;
1434
5.65k
        *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
1435
5.65k
        break;
1436
1437
0
    case TLS_ST_SW_CERT_REQ:
1438
0
        *confunc = tls_construct_certificate_request;
1439
0
        *mt = SSL3_MT_CERTIFICATE_REQUEST;
1440
0
        break;
1441
1442
9.58k
    case TLS_ST_SW_SRVR_DONE:
1443
9.58k
        *confunc = tls_construct_server_done;
1444
9.58k
        *mt = SSL3_MT_SERVER_DONE;
1445
9.58k
        break;
1446
1447
218
    case TLS_ST_SW_SESSION_TICKET:
1448
218
        *confunc = tls_construct_new_session_ticket;
1449
218
        *mt = SSL3_MT_NEWSESSION_TICKET;
1450
218
        break;
1451
1452
0
    case TLS_ST_SW_CERT_STATUS:
1453
0
        *confunc = tls_construct_cert_status;
1454
0
        *mt = SSL3_MT_CERTIFICATE_STATUS;
1455
0
        break;
1456
1457
1.70k
    case TLS_ST_SW_FINISHED:
1458
1.70k
        *confunc = tls_construct_finished;
1459
1.70k
        *mt = SSL3_MT_FINISHED;
1460
1.70k
        break;
1461
1462
708
    case TLS_ST_EARLY_DATA:
1463
708
        *confunc = NULL;
1464
708
        *mt = SSL3_MT_DUMMY;
1465
708
        break;
1466
1467
1.00k
    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
1468
1.00k
        *confunc = tls_construct_encrypted_extensions;
1469
1.00k
        *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
1470
1.00k
        break;
1471
1472
0
    case TLS_ST_SW_KEY_UPDATE:
1473
0
        *confunc = tls_construct_key_update;
1474
0
        *mt = SSL3_MT_KEY_UPDATE;
1475
0
        break;
1476
1477
0
    case TLS_ST_SW_ACK:
1478
0
        *confunc = dtls_construct_ack;
1479
0
        *mt = DTLS13_MT_ACK;
1480
0
        break;
1481
42.3k
    }
1482
1483
42.3k
    return 1;
1484
42.3k
}
1485
1486
/*
1487
 * Maximum size (excluding the Handshake header) of a ClientHello message,
1488
 * calculated as follows:
1489
 *
1490
 *  2 + # client_version
1491
 *  32 + # only valid length for random
1492
 *  1 + # length of session_id
1493
 *  32 + # maximum size for session_id
1494
 *  2 + # length of cipher suites
1495
 *  2^16-2 + # maximum length of cipher suites array
1496
 *  1 + # length of compression_methods
1497
 *  2^8-1 + # maximum length of compression methods
1498
 *  2 + # length of extensions
1499
 *  2^16-1 # maximum length of extensions
1500
 */
1501
50.9k
#define CLIENT_HELLO_MAX_LENGTH 131396
1502
1503
7.28k
#define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1504
0
#define NEXT_PROTO_MAX_LENGTH 514
1505
1506
/*
1507
 * Returns the maximum allowed length for the current message that we are
1508
 * reading. Excludes the message header.
1509
 */
1510
size_t ossl_statem_server_max_message_size(SSL_CONNECTION *s)
1511
63.2k
{
1512
63.2k
    OSSL_STATEM *st = &s->statem;
1513
1514
63.2k
    switch (st->hand_state) {
1515
0
    default:
1516
        /* Shouldn't happen */
1517
0
        return 0;
1518
1519
50.9k
    case TLS_ST_SR_CLNT_HELLO:
1520
50.9k
        return CLIENT_HELLO_MAX_LENGTH;
1521
1522
0
    case TLS_ST_SR_END_OF_EARLY_DATA:
1523
0
        return END_OF_EARLY_DATA_MAX_LENGTH;
1524
1525
0
    case TLS_ST_SR_COMP_CERT:
1526
0
    case TLS_ST_SR_CERT:
1527
0
        return s->max_cert_list;
1528
1529
7.28k
    case TLS_ST_SR_KEY_EXCH:
1530
7.28k
        return CLIENT_KEY_EXCH_MAX_LENGTH;
1531
1532
0
    case TLS_ST_SR_CERT_VRFY:
1533
0
        return CERTIFICATE_VERIFY_MAX_LENGTH;
1534
1535
0
#ifndef OPENSSL_NO_NEXTPROTONEG
1536
0
    case TLS_ST_SR_NEXT_PROTO:
1537
0
        return NEXT_PROTO_MAX_LENGTH;
1538
0
#endif
1539
1540
4.32k
    case TLS_ST_SR_CHANGE:
1541
4.32k
        return CCS_MAX_LENGTH;
1542
1543
678
    case TLS_ST_SR_FINISHED:
1544
678
        return FINISHED_MAX_LENGTH;
1545
1546
0
    case TLS_ST_SR_KEY_UPDATE:
1547
0
        return KEY_UPDATE_MAX_LENGTH;
1548
1549
0
    case TLS_ST_SR_ACK:
1550
0
        return ACK_MAX_LENGTH;
1551
63.2k
    }
1552
63.2k
}
1553
1554
/*
1555
 * Process a message that the server has received from the client.
1556
 */
1557
MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL_CONNECTION *s,
1558
    PACKET *pkt)
1559
62.5k
{
1560
62.5k
    OSSL_STATEM *st = &s->statem;
1561
1562
62.5k
    switch (st->hand_state) {
1563
0
    default:
1564
        /* Shouldn't happen */
1565
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1566
0
        return MSG_PROCESS_ERROR;
1567
1568
50.3k
    case TLS_ST_SR_CLNT_HELLO:
1569
50.3k
        return tls_process_client_hello(s, pkt);
1570
1571
0
    case TLS_ST_SR_END_OF_EARLY_DATA:
1572
0
        return tls_process_end_of_early_data(s, pkt);
1573
1574
0
    case TLS_ST_SR_CERT:
1575
0
        return tls_process_client_certificate(s, pkt);
1576
1577
#ifndef OPENSSL_NO_COMP_ALG
1578
    case TLS_ST_SR_COMP_CERT:
1579
        return tls_process_client_compressed_certificate(s, pkt);
1580
#endif
1581
1582
7.21k
    case TLS_ST_SR_KEY_EXCH:
1583
7.21k
        return tls_process_client_key_exchange(s, pkt);
1584
1585
0
    case TLS_ST_SR_CERT_VRFY:
1586
0
        return tls_process_cert_verify(s, pkt);
1587
1588
0
#ifndef OPENSSL_NO_NEXTPROTONEG
1589
0
    case TLS_ST_SR_NEXT_PROTO:
1590
0
        return tls_process_next_proto(s, pkt);
1591
0
#endif
1592
1593
4.32k
    case TLS_ST_SR_CHANGE:
1594
4.32k
        return tls_process_change_cipher_spec(s, pkt);
1595
1596
659
    case TLS_ST_SR_FINISHED:
1597
659
        return tls_process_finished(s, pkt);
1598
1599
0
    case TLS_ST_SR_KEY_UPDATE:
1600
0
        return tls_process_key_update(s, pkt);
1601
1602
0
    case TLS_ST_SR_ACK:
1603
0
        return dtls_process_ack(s, pkt);
1604
62.5k
    }
1605
62.5k
}
1606
1607
/*
1608
 * Perform any further processing required following the receipt of a message
1609
 * from the client
1610
 */
1611
WORK_STATE ossl_statem_server_post_process_message(SSL_CONNECTION *s,
1612
    WORK_STATE wst)
1613
20.2k
{
1614
20.2k
    OSSL_STATEM *st = &s->statem;
1615
1616
20.2k
    switch (st->hand_state) {
1617
0
    default:
1618
        /* Shouldn't happen */
1619
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1620
0
        return WORK_ERROR;
1621
1622
13.8k
    case TLS_ST_SR_CLNT_HELLO:
1623
13.8k
        return tls_post_process_client_hello(s, wst);
1624
1625
0
    case TLS_ST_SR_CERT:
1626
#ifndef OPENSSL_NO_COMP_ALG
1627
    case TLS_ST_SR_COMP_CERT:
1628
#endif
1629
0
        return tls_post_process_client_certificate(s, wst);
1630
1631
6.40k
    case TLS_ST_SR_KEY_EXCH:
1632
6.40k
        return tls_post_process_client_key_exchange(s, wst);
1633
20.2k
    }
1634
20.2k
}
1635
1636
#ifndef OPENSSL_NO_SRP
1637
/* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1638
static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION *s)
1639
29.3k
{
1640
29.3k
    int ret;
1641
29.3k
    int al = SSL_AD_UNRECOGNIZED_NAME;
1642
1643
29.3k
    if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) && (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1644
0
        if (s->srp_ctx.login == NULL) {
1645
            /*
1646
             * RFC 5054 says SHOULD reject, we do so if There is no srp
1647
             * login name
1648
             */
1649
0
            SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
1650
0
                SSL_R_PSK_IDENTITY_NOT_FOUND);
1651
0
            return -1;
1652
0
        } else {
1653
0
            ret = ssl_srp_server_param_with_username_intern(s, &al);
1654
0
            if (ret < 0)
1655
0
                return 0;
1656
0
            if (ret == SSL3_AL_FATAL) {
1657
0
                SSLfatal(s, al,
1658
0
                    al == SSL_AD_UNKNOWN_PSK_IDENTITY
1659
0
                        ? SSL_R_PSK_IDENTITY_NOT_FOUND
1660
0
                        : SSL_R_CLIENTHELLO_TLSEXT);
1661
0
                return -1;
1662
0
            }
1663
0
        }
1664
0
    }
1665
29.3k
    return 1;
1666
29.3k
}
1667
#endif
1668
1669
int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1670
    size_t cookie_len)
1671
0
{
1672
    /* Always use DTLS 1.0 version: see RFC 6347 */
1673
0
    if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1674
0
        || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1675
0
        return 0;
1676
1677
0
    return 1;
1678
0
}
1679
1680
CON_FUNC_RETURN dtls_construct_hello_verify_request(SSL_CONNECTION *s,
1681
    WPACKET *pkt)
1682
0
{
1683
0
    unsigned int cookie_leni = 0;
1684
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1685
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1686
0
    int cb_ret = 0;
1687
1688
0
#if !defined(OPENSSL_NO_DTLS)
1689
0
    DTLS_LISTENER *dl = (DTLS_LISTENER *)s->d1->listener;
1690
1691
0
    if (dl != NULL && dl->require_hvr_cookie && sctx->app_gen_cookie_cb == NULL) {
1692
0
        cb_ret = ossl_dtls_listener_gen_cookie_cb(ussl, s->d1->cookie, &cookie_leni);
1693
0
    } else
1694
0
#endif
1695
0
        if (sctx->app_gen_cookie_cb != NULL) {
1696
0
        cb_ret = sctx->app_gen_cookie_cb(ussl, s->d1->cookie, &cookie_leni);
1697
0
    }
1698
1699
0
    if (cb_ret == 0 || cookie_leni > sizeof(s->d1->cookie)) {
1700
0
        SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1701
0
        return CON_FUNC_ERROR;
1702
0
    }
1703
0
    s->d1->cookie_len = cookie_leni;
1704
1705
0
    if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1706
0
            s->d1->cookie_len)) {
1707
0
        SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
1708
0
        return CON_FUNC_ERROR;
1709
0
    }
1710
1711
    /*
1712
     * Server must recover the downgrade sentinel in case it sends a
1713
     * HelloVerifyRequest.
1714
     */
1715
0
    s->d1->hello_verify_request = SSL_HVR_SENT;
1716
1717
0
    return CON_FUNC_SUCCESS;
1718
0
}
1719
1720
/*-
1721
 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1722
 * SecureTransport using the TLS extension block in |hello|.
1723
 * Safari, since 10.6, sends exactly these extensions, in this order:
1724
 *   SNI,
1725
 *   elliptic_curves
1726
 *   ec_point_formats
1727
 *   signature_algorithms (for TLSv1.2 only)
1728
 *
1729
 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1730
 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1731
 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1732
 * 10.8..10.8.3 (which don't work).
1733
 */
1734
static void ssl_check_for_safari(SSL_CONNECTION *s,
1735
    const CLIENTHELLO_MSG *hello)
1736
0
{
1737
0
    static const unsigned char kSafariExtensionsBlock[] = {
1738
0
        0x00,
1739
0
        0x0a, /* elliptic_curves extension */
1740
0
        0x00,
1741
0
        0x08, /* 8 bytes */
1742
0
        0x00,
1743
0
        0x06, /* 6 bytes of curve ids */
1744
0
        0x00,
1745
0
        0x17, /* P-256 */
1746
0
        0x00,
1747
0
        0x18, /* P-384 */
1748
0
        0x00,
1749
0
        0x19, /* P-521 */
1750
1751
0
        0x00,
1752
0
        0x0b, /* ec_point_formats */
1753
0
        0x00,
1754
0
        0x02, /* 2 bytes */
1755
0
        0x01, /* 1 point format */
1756
0
        0x00, /* uncompressed */
1757
        /* The following is only present in TLS 1.2 */
1758
0
        0x00,
1759
0
        0x0d, /* signature_algorithms */
1760
0
        0x00,
1761
0
        0x0c, /* 12 bytes */
1762
0
        0x00,
1763
0
        0x0a, /* 10 bytes */
1764
0
        0x05,
1765
0
        0x01, /* SHA-384/RSA */
1766
0
        0x04,
1767
0
        0x01, /* SHA-256/RSA */
1768
0
        0x02,
1769
0
        0x01, /* SHA-1/RSA */
1770
0
        0x04,
1771
0
        0x03, /* SHA-256/ECDSA */
1772
0
        0x02,
1773
0
        0x03, /* SHA-1/ECDSA */
1774
0
    };
1775
    /* Length of the common prefix (first two extensions). */
1776
0
    static const size_t kSafariCommonExtensionsLength = 18;
1777
0
    unsigned int type;
1778
0
    PACKET sni, tmppkt;
1779
0
    size_t ext_len;
1780
1781
0
    tmppkt = hello->extensions;
1782
1783
0
    if (!PACKET_forward(&tmppkt, 2)
1784
0
        || !PACKET_get_net_2(&tmppkt, &type)
1785
0
        || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1786
0
        return;
1787
0
    }
1788
1789
0
    if (type != TLSEXT_TYPE_server_name)
1790
0
        return;
1791
1792
0
    ext_len = TLS1_get_client_version(
1793
0
                  SSL_CONNECTION_GET_SSL(s))
1794
0
            >= TLS1_2_VERSION
1795
0
        ? sizeof(kSafariExtensionsBlock)
1796
0
        : kSafariCommonExtensionsLength;
1797
1798
0
    s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1799
0
        ext_len);
1800
0
}
1801
1802
#define RENEG_OPTIONS_OK(options)             \
1803
71.8k
    ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
1804
71.8k
        && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
1805
1806
MSG_PROCESS_RETURN tls_process_client_hello(SSL_CONNECTION *s, PACKET *pkt)
1807
50.3k
{
1808
    /* |cookie| will only be initialized for DTLS. */
1809
50.3k
    PACKET session_id, compression, extensions, cookie;
1810
50.3k
    CLIENTHELLO_MSG *clienthello = NULL;
1811
1812
50.3k
#ifndef OPENSSL_NO_ECH
1813
    /*
1814
     * For a split-mode backend we want to have a way to point at the CH octets
1815
     * for the accept-confirmation calculation. The split-mode backend does not
1816
     * need any ECH secrets, but it does need to see the inner CH and be the TLS
1817
     * endpoint with which the ECH encrypting client sets up the TLS session.
1818
     * The split-mode backend however does need to do an ECH confirm calculation
1819
     * so we need to tee that up. The result of that calculation will be put in
1820
     * the ServerHello.random (or ECH extension if HRR) to signal to the client
1821
     * that ECH "worked."
1822
     */
1823
50.3k
    if (s->server && PACKET_remaining(pkt) != 0) {
1824
23.8k
        int rv = 0, innerflag = OSSL_ECH_UNKNOWN_CH_TYPE;
1825
23.8k
        size_t startofsessid = 0, startofexts = 0, echoffset = 0;
1826
23.8k
        size_t outersnioffset = 0; /* offset to SNI in outer */
1827
23.8k
        uint16_t echtype = OSSL_ECH_type_unknown; /* type of ECH seen */
1828
23.8k
        const unsigned char *pbuf = NULL;
1829
1830
        /* reset needed in case of HRR */
1831
23.8k
        s->ext.ech.ch_offsets_done = 0;
1832
23.8k
        rv = ossl_ech_get_ch_offsets(s, pkt, &startofsessid, &startofexts,
1833
23.8k
            &echoffset, &echtype, &innerflag,
1834
23.8k
            &outersnioffset);
1835
23.8k
        if (rv != 1) {
1836
            /* SSLfatal already called */
1837
34
            goto err;
1838
34
        }
1839
23.8k
        if (innerflag == OSSL_ECH_INNER_CH_TYPE) {
1840
86
            WPACKET inner;
1841
1842
86
            OSSL_TRACE(TLS, "Got inner ECH so setting backend\n");
1843
            /* For backend, include msg type & 3 octet length */
1844
86
            s->ext.ech.backend = 1;
1845
86
            s->ext.ech.attempted_type = TLSEXT_TYPE_ech;
1846
86
            OPENSSL_free(s->ext.ech.innerch);
1847
86
            s->ext.ech.innerch_len = PACKET_remaining(pkt);
1848
86
            if (PACKET_peek_bytes(pkt, &pbuf, s->ext.ech.innerch_len) != 1) {
1849
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1850
0
                goto err;
1851
0
            }
1852
86
            s->ext.ech.innerch_len += SSL3_HM_HEADER_LENGTH; /* 4 */
1853
86
            s->ext.ech.innerch = OPENSSL_malloc(s->ext.ech.innerch_len);
1854
86
            if (s->ext.ech.innerch == NULL) {
1855
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1856
0
                goto err;
1857
0
            }
1858
86
            if (!WPACKET_init_static_len(&inner, s->ext.ech.innerch,
1859
86
                    s->ext.ech.innerch_len, 0)) {
1860
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1861
0
                goto err;
1862
0
            }
1863
86
            if (!WPACKET_put_bytes_u8(&inner, SSL3_MT_CLIENT_HELLO)
1864
86
                || !WPACKET_put_bytes_u24(&inner, s->ext.ech.innerch_len - SSL3_HM_HEADER_LENGTH)
1865
86
                || !WPACKET_memcpy(&inner, pbuf, s->ext.ech.innerch_len - SSL3_HM_HEADER_LENGTH)
1866
86
                || !WPACKET_finish(&inner)) {
1867
0
                WPACKET_cleanup(&inner);
1868
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1869
0
                goto err;
1870
0
            }
1871
86
            if (ossl_ech_intbuf_add(s, s->ext.ech.innerch,
1872
86
                    s->ext.ech.innerch_len, 0)
1873
86
                != 1) {
1874
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1875
0
                goto err;
1876
0
            }
1877
23.7k
        } else if (s->ext.ech.es != NULL) {
1878
0
            PACKET newpkt;
1879
0
            int secondtime = s->ext.ech.success;
1880
1881
            /*
1882
             * if ECH decrypt worked first time (success == 1) then fail
1883
             * if there's no ECH extension at all 2nd time
1884
             */
1885
0
            if (secondtime == 1 && echoffset == 0) {
1886
0
                SSLfatal(s, SSL_AD_MISSING_EXTENSION,
1887
0
                    SSL_R_TLSV13_ALERT_MISSING_EXTENSION);
1888
0
                goto err;
1889
0
            }
1890
0
            if (ossl_ech_early_decrypt(s, pkt, &newpkt) != 1) {
1891
                /* SSLfatal() already called */
1892
0
                goto err;
1893
0
            }
1894
0
            if (s->ext.ech.success == 1) {
1895
                /*
1896
                 * Replace the outer CH with the inner, as long as there's
1897
                 * space, which there better be! (a bug triggered a bigger
1898
                 * inner CH once;-)
1899
                 */
1900
0
                if (PACKET_remaining(&newpkt) > PACKET_remaining(pkt)) {
1901
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1902
0
                    goto err;
1903
0
                }
1904
0
                *pkt = newpkt;
1905
0
            } else if (secondtime == 1 && s->ext.ech.success == 0) {
1906
                /* 2nd time decrypt failed */
1907
0
                SSLfatal(s, SSL_AD_DECRYPT_ERROR, ERR_R_INTERNAL_ERROR);
1908
0
                goto err;
1909
0
            }
1910
0
        }
1911
23.8k
    }
1912
50.3k
#endif
1913
1914
    /* Check if this is actually an unexpected renegotiation ClientHello */
1915
50.3k
    if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1916
35.9k
        if (!ossl_assert(!SSL_CONNECTION_IS_VERSION13(s))) {
1917
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1918
0
            goto err;
1919
0
        }
1920
35.9k
        if (!RENEG_OPTIONS_OK(s->options)
1921
0
            || (!s->s3.send_connection_binding
1922
0
                && (s->options
1923
0
                       & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
1924
35.9k
                    == 0)) {
1925
35.9k
            ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1926
35.9k
            return MSG_PROCESS_FINISHED_READING;
1927
35.9k
        }
1928
0
        s->renegotiate = 1;
1929
0
        s->new_session = 1;
1930
0
    }
1931
1932
14.4k
    clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1933
14.4k
    if (clienthello == NULL) {
1934
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1935
0
        goto err;
1936
0
    }
1937
1938
    /*
1939
     * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1940
     */
1941
14.4k
    PACKET_null_init(&cookie);
1942
1943
14.4k
    if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1944
29
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
1945
29
        goto err;
1946
29
    }
1947
1948
14.3k
    if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1949
14.3k
        || !PACKET_get_length_prefixed_1(pkt, &session_id)
1950
14.3k
        || !PACKET_copy_all(&session_id, clienthello->session_id,
1951
14.3k
            SSL_MAX_SSL_SESSION_ID_LENGTH,
1952
14.3k
            &clienthello->session_id_len)) {
1953
106
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1954
106
        goto err;
1955
106
    }
1956
1957
14.2k
    if (SSL_CONNECTION_IS_DTLS(s)) {
1958
6.46k
        int minversion, maxversion;
1959
1960
6.46k
        if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1961
17
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1962
17
            goto err;
1963
17
        }
1964
6.45k
        if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1965
6.45k
                sizeof(clienthello->dtls_cookie),
1966
6.45k
                &clienthello->dtls_cookie_len)) {
1967
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1968
0
            goto err;
1969
0
        }
1970
1971
        /*
1972
         * If the connection supports DTLSv1.3:
1973
         *      We continue to process ClientHello's without cookies
1974
         *
1975
         * Otherwise, if we require cookies and this ClientHello doesn't
1976
         * contain one:
1977
         *      Return since we do not want to allocate any memory yet
1978
         */
1979
6.45k
        if ((SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)
1980
0
            && clienthello->dtls_cookie_len == 0
1981
0
            && ossl_assert(ssl_get_min_max_version(s, &minversion,
1982
6.45k
                               &maxversion, NULL)
1983
6.45k
                == 0)
1984
0
            && ssl_version_cmp(s, maxversion, DTLS1_3_VERSION) < 0) {
1985
0
            OPENSSL_free(clienthello);
1986
0
            return MSG_PROCESS_FINISHED_READING;
1987
0
        }
1988
6.45k
    }
1989
1990
14.2k
    if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1991
82
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1992
82
        goto err;
1993
82
    }
1994
1995
14.1k
    if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1996
47
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1997
47
        goto err;
1998
47
    }
1999
2000
    /* Could be empty. */
2001
14.1k
    if (PACKET_remaining(pkt) == 0) {
2002
1.40k
        PACKET_null_init(&clienthello->extensions);
2003
12.7k
    } else {
2004
12.7k
        if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
2005
12.6k
            || PACKET_remaining(pkt) != 0) {
2006
205
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2007
205
            goto err;
2008
205
        }
2009
12.7k
    }
2010
2011
13.9k
    if (!PACKET_copy_all(&compression, clienthello->compressions,
2012
13.9k
            MAX_COMPRESSIONS_SIZE,
2013
13.9k
            &clienthello->compressions_len)) {
2014
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2015
0
        goto err;
2016
0
    }
2017
2018
    /* Preserve the raw extensions PACKET for later use */
2019
13.9k
    extensions = clienthello->extensions;
2020
13.9k
    if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
2021
13.9k
            &clienthello->pre_proc_exts,
2022
13.9k
            &clienthello->pre_proc_exts_len, 1)) {
2023
        /* SSLfatal already been called */
2024
133
        goto err;
2025
133
    }
2026
2027
13.8k
    if (SSL_CONNECTION_IS_DTLS(s)) {
2028
6.26k
        int minversion, maxversion;
2029
2030
        /*
2031
         * If the connection supports DTLSv1.3 or if the ClientHello was sent
2032
         * with the SupportedVersions extension:
2033
         *      We continue to process ClientHello's without cookies
2034
         *
2035
         * Otherwise, if we require cookies and this ClientHello doesn't
2036
         * contain one:
2037
         *      Return since we do not want to allocate any memory yet
2038
         */
2039
6.26k
        if ((SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)
2040
0
            && clienthello->dtls_cookie_len == 0
2041
0
            && ossl_assert(ssl_get_min_max_version(s, &minversion,
2042
6.26k
                               &maxversion, NULL)
2043
6.26k
                == 0)
2044
0
            && ssl_version_cmp(s, maxversion, DTLS1_3_VERSION) < 0
2045
0
            && !clienthello->pre_proc_exts[TLSEXT_IDX_supported_versions].present) {
2046
0
            OPENSSL_free(clienthello->pre_proc_exts);
2047
0
            OPENSSL_free(clienthello);
2048
0
            return MSG_PROCESS_FINISHED_READING;
2049
0
        }
2050
6.26k
    }
2051
2052
13.8k
    s->clienthello = clienthello;
2053
2054
13.8k
    return MSG_PROCESS_CONTINUE_PROCESSING;
2055
2056
653
err:
2057
653
    if (clienthello != NULL)
2058
619
        OPENSSL_free(clienthello->pre_proc_exts);
2059
653
    OPENSSL_free(clienthello);
2060
653
#ifndef OPENSSL_NO_ECH
2061
653
    s->clienthello = NULL;
2062
653
    OPENSSL_free(s->ext.ech.innerch);
2063
653
    s->ext.ech.innerch = NULL;
2064
653
    s->ext.ech.innerch_len = 0;
2065
653
#endif
2066
2067
653
    return MSG_PROCESS_ERROR;
2068
13.8k
}
2069
2070
static int tls_early_post_process_client_hello(SSL_CONNECTION *s)
2071
6.69k
{
2072
6.69k
    unsigned int j;
2073
6.69k
    int i, al = SSL_AD_INTERNAL_ERROR;
2074
6.69k
    int protverr;
2075
6.69k
    unsigned long id;
2076
6.69k
#ifndef OPENSSL_NO_COMP
2077
6.69k
    SSL_COMP *comp = NULL;
2078
6.69k
#endif
2079
6.69k
    const SSL_CIPHER *c;
2080
6.69k
    STACK_OF(SSL_CIPHER) *ciphers = NULL;
2081
6.69k
    STACK_OF(SSL_CIPHER) *scsvs = NULL;
2082
6.69k
    CLIENTHELLO_MSG *clienthello = s->clienthello;
2083
6.69k
    DOWNGRADE dgrd = DOWNGRADE_NONE;
2084
6.69k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2085
6.69k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2086
6.69k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
2087
2088
    /* Finished parsing the ClientHello, now we can start processing it */
2089
    /* Give the ClientHello callback a crack at things */
2090
6.69k
    if (sctx->client_hello_cb != NULL) {
2091
        /* A failure in the ClientHello callback terminates the connection. */
2092
0
        switch (sctx->client_hello_cb(ussl, &al, sctx->client_hello_cb_arg)) {
2093
0
        case SSL_CLIENT_HELLO_SUCCESS:
2094
0
            break;
2095
0
        case SSL_CLIENT_HELLO_RETRY:
2096
0
            s->rwstate = SSL_CLIENT_HELLO_CB;
2097
0
            return -1;
2098
0
        case SSL_CLIENT_HELLO_ERROR:
2099
0
        default:
2100
0
            SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
2101
0
            goto err;
2102
0
        }
2103
0
    }
2104
2105
    /* Set up the client_random */
2106
6.69k
    memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);
2107
2108
    /* Choose the server SSL/TLS/DTLS version. */
2109
6.69k
    protverr = ssl_choose_server_version(s, clienthello, &dgrd);
2110
2111
6.69k
#ifndef OPENSSL_NO_ECH
2112
6.69k
    if (protverr && s->ext.ech.success == 1) {
2113
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, protverr);
2114
0
    }
2115
6.69k
#endif
2116
6.69k
    if (protverr) {
2117
181
        if (SSL_IS_FIRST_HANDSHAKE(s)) {
2118
            /* like ssl3_get_record, send alert using remote version number */
2119
181
            s->version = s->client_version = clienthello->legacy_version;
2120
181
        }
2121
181
        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
2122
181
        goto err;
2123
181
    }
2124
2125
    /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
2126
6.51k
    if (SSL_CONNECTION_IS_VERSION13(s)
2127
1.01k
        && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
2128
2
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
2129
2
        goto err;
2130
2
    }
2131
6.51k
    if (SSL_CONNECTION_IS_DTLS(s)) {
2132
2.96k
        if ((SSL_get_options(ssl) & SSL_OP_COOKIE_EXCHANGE) && clienthello->dtls_cookie_len != 0) {
2133
0
            int verify_ret = 0;
2134
2135
0
#if !defined(OPENSSL_NO_DTLS)
2136
0
            DTLS_LISTENER *dl = (DTLS_LISTENER *)s->d1->listener;
2137
2138
0
            if (dl != NULL && dl->require_hvr_cookie && sctx->app_verify_cookie_cb == NULL) {
2139
0
                verify_ret = ossl_dtls_listener_verify_cookie_cb(ussl,
2140
0
                    clienthello->dtls_cookie,
2141
0
                    (unsigned int)clienthello->dtls_cookie_len);
2142
0
            } else
2143
0
#endif
2144
0
                if (sctx->app_verify_cookie_cb != NULL) {
2145
0
                verify_ret = sctx->app_verify_cookie_cb(ussl, clienthello->dtls_cookie,
2146
0
                    (unsigned int)clienthello->dtls_cookie_len);
2147
0
            } else if (s->d1->cookie_len == clienthello->dtls_cookie_len
2148
0
                && memcmp(clienthello->dtls_cookie, s->d1->cookie,
2149
0
                       s->d1->cookie_len)
2150
0
                    == 0) {
2151
                /* default verification succeeded */
2152
0
                verify_ret = 1;
2153
0
            }
2154
2155
0
            if (verify_ret == 0) {
2156
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH);
2157
0
                goto err;
2158
0
            }
2159
0
            s->d1->cookie_verified = 1;
2160
0
        }
2161
2.96k
    }
2162
2163
6.51k
    s->hit = 0;
2164
2165
6.51k
    if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites)
2166
6.50k
        || !ossl_bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs, 1)) {
2167
        /* SSLfatal() already called */
2168
8
        goto err;
2169
8
    }
2170
2171
6.50k
    s->s3.send_connection_binding = 0;
2172
    /* Check what signalling cipher-suite values were received. */
2173
6.50k
    if (scsvs != NULL) {
2174
7.65k
        for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
2175
1.15k
            c = sk_SSL_CIPHER_value(scsvs, i);
2176
1.15k
            if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
2177
1.07k
                if (s->renegotiate) {
2178
                    /* SCSV is fatal if renegotiating */
2179
0
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2180
0
                        SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
2181
0
                    goto err;
2182
0
                }
2183
1.07k
                s->s3.send_connection_binding = 1;
2184
1.07k
            } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV && !ssl_check_version_downgrade(s)) {
2185
                /*
2186
                 * This SCSV indicates that the client previously tried
2187
                 * a higher version.  We should fail if the current version
2188
                 * is an unexpected downgrade, as that indicates that the first
2189
                 * connection may have been tampered with in order to trigger
2190
                 * an insecure downgrade.
2191
                 */
2192
4
                SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
2193
4
                    SSL_R_INAPPROPRIATE_FALLBACK);
2194
4
                goto err;
2195
4
            }
2196
1.15k
        }
2197
6.50k
    }
2198
2199
    /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
2200
6.49k
    if (SSL_CONNECTION_IS_VERSION13(s)) {
2201
1.01k
        const SSL_CIPHER *cipher = ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(ssl));
2202
2203
1.01k
        if (cipher == NULL) {
2204
10
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
2205
10
            goto err;
2206
10
        }
2207
1.00k
        if (s->hello_retry_request == SSL_HRR_PENDING
2208
36
            && (s->s3.tmp.new_cipher == NULL
2209
36
                || s->s3.tmp.new_cipher->id != cipher->id)) {
2210
            /*
2211
             * A previous HRR picked a different ciphersuite to the one we
2212
             * just selected. Something must have changed.
2213
             */
2214
1
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
2215
1
            goto err;
2216
1
        }
2217
1.00k
        s->s3.tmp.new_cipher = cipher;
2218
1.00k
    }
2219
2220
    /* We need to do this before getting the session */
2221
6.48k
    if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
2222
6.48k
            SSL_EXT_CLIENT_HELLO,
2223
6.48k
            clienthello->pre_proc_exts, NULL, 0)) {
2224
        /* SSLfatal() already called */
2225
2
        goto err;
2226
2
    }
2227
2228
    /*
2229
     * We don't allow resumption in a backwards compatible ClientHello.
2230
     * In TLS1.1+, session_id MUST be empty.
2231
     *
2232
     * Versions before 0.9.7 always allow clients to resume sessions in
2233
     * renegotiation. 0.9.7 and later allow this by default, but optionally
2234
     * ignore resumption requests with flag
2235
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
2236
     * than a change to default behavior so that applications relying on
2237
     * this for security won't even compile against older library versions).
2238
     * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
2239
     * request renegotiation but not a new session (s->new_session remains
2240
     * unset): for servers, this essentially just means that the
2241
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
2242
     * ignored.
2243
     */
2244
6.48k
    if (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)) {
2245
0
        if (!ssl_get_new_session(s, 1)) {
2246
            /* SSLfatal() already called */
2247
0
            goto err;
2248
0
        }
2249
6.48k
    } else {
2250
6.48k
        i = ssl_get_prev_session(s, clienthello);
2251
6.48k
        if (i == 1) {
2252
            /* previous session */
2253
42
            s->hit = 1;
2254
6.44k
        } else if (i == -1) {
2255
            /* SSLfatal() already called */
2256
69
            goto err;
2257
6.37k
        } else {
2258
            /* i == 0 */
2259
6.37k
            if (!ssl_get_new_session(s, 1)) {
2260
                /* SSLfatal() already called */
2261
0
                goto err;
2262
0
            }
2263
6.37k
        }
2264
6.48k
    }
2265
2266
6.41k
    if (SSL_CONNECTION_IS_VERSION13(s)) {
2267
936
        memcpy(s->tmp_session_id, s->clienthello->session_id,
2268
936
            s->clienthello->session_id_len);
2269
936
        s->tmp_session_id_len = s->clienthello->session_id_len;
2270
936
    }
2271
2272
    /*
2273
     * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
2274
     * ciphersuite compatibility with the session as part of resumption.
2275
     */
2276
6.41k
    if (!SSL_CONNECTION_IS_VERSION13(s) && s->hit) {
2277
42
        j = 0;
2278
42
        id = s->session->cipher->id;
2279
2280
42
        OSSL_TRACE_BEGIN(TLS_CIPHER)
2281
0
        {
2282
0
            BIO_printf(trc_out, "client sent %d ciphers\n",
2283
0
                sk_SSL_CIPHER_num(ciphers));
2284
0
        }
2285
306
        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
2286
296
            c = sk_SSL_CIPHER_value(ciphers, i);
2287
296
            if (trc_out != NULL)
2288
0
                BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
2289
0
                    sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
2290
296
            if (c->id == id) {
2291
32
                j = 1;
2292
32
                break;
2293
32
            }
2294
296
        }
2295
42
        if (j == 0) {
2296
            /*
2297
             * we need to have the cipher in the cipher list if we are asked
2298
             * to reuse it
2299
             */
2300
10
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2301
10
                SSL_R_REQUIRED_CIPHER_MISSING);
2302
10
            OSSL_TRACE_CANCEL(TLS_CIPHER);
2303
10
            goto err;
2304
10
        }
2305
42
        OSSL_TRACE_END(TLS_CIPHER);
2306
42
    }
2307
2308
    /* At least one compression method must be preset. */
2309
6.40k
    if (clienthello->compressions_len == 0) {
2310
35
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED);
2311
35
        goto err;
2312
35
    }
2313
    /* Make sure at least the null compression is supported. */
2314
6.37k
    if (memchr(clienthello->compressions, 0,
2315
6.37k
            clienthello->compressions_len)
2316
6.37k
        == NULL) {
2317
15
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2318
15
            SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
2319
15
        goto err;
2320
15
    }
2321
2322
6.35k
    if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
2323
0
        ssl_check_for_safari(s, clienthello);
2324
2325
    /* TLS extensions */
2326
6.35k
    if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
2327
6.35k
            clienthello->pre_proc_exts, NULL, 0, 1)) {
2328
        /* SSLfatal() already called */
2329
657
        goto err;
2330
657
    }
2331
2332
    /*
2333
     * Check if we want to use external pre-shared secret for this handshake
2334
     * for not reused session only. We need to generate server_random before
2335
     * calling tls_session_secret_cb in order to allow SessionTicket
2336
     * processing to use it in key derivation.
2337
     */
2338
5.70k
    {
2339
5.70k
        unsigned char *pos;
2340
5.70k
        pos = s->s3.server_random;
2341
5.70k
        if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
2342
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2343
0
            goto err;
2344
0
        }
2345
5.70k
    }
2346
2347
5.70k
    if (!s->hit && !tls1_set_server_sigalgs(s)) {
2348
        /* SSLfatal() already called */
2349
19
        goto err;
2350
19
    }
2351
2352
    /*
2353
     * Unless ECH has worked or not been configured we won't call
2354
     * the session_secret_cb now because we'll need to calculate the
2355
     * server random later to include the ECH accept value.
2356
     * We can't do it now as we don't yet have the SH encoding.
2357
     */
2358
5.68k
    if (
2359
5.68k
#ifndef OPENSSL_NO_ECH
2360
5.68k
        ((s->ext.ech.es != NULL && s->ext.ech.success == 1)
2361
5.68k
            || s->ext.ech.es == NULL)
2362
5.68k
        &&
2363
5.68k
#endif
2364
5.68k
        !s->hit
2365
5.65k
        && s->version >= TLS1_VERSION
2366
5.65k
        && !SSL_CONNECTION_IS_VERSION13(s)
2367
5.10k
        && !SSL_CONNECTION_IS_DTLS(s)
2368
2.28k
        && s->ext.session_secret_cb != NULL) {
2369
0
        const SSL_CIPHER *pref_cipher = NULL;
2370
2371
        /*
2372
         * s->session->master_key_length is a size_t, but this is an int for
2373
         * backwards compat reasons
2374
         */
2375
0
        int master_key_length;
2376
2377
0
        master_key_length = sizeof(s->session->master_key);
2378
0
        if (s->ext.session_secret_cb(ussl, s->session->master_key,
2379
0
                &master_key_length, ciphers,
2380
0
                &pref_cipher,
2381
0
                s->ext.session_secret_cb_arg)
2382
0
            && master_key_length > 0) {
2383
0
            s->session->master_key_length = master_key_length;
2384
0
            s->hit = 1;
2385
0
            s->peer_ciphers = ciphers;
2386
0
            s->session->verify_result = X509_V_OK;
2387
2388
            /*
2389
             * Per RFC 4851, Section 3.2.2:
2390
             * If the ClientHello contains both a Session ID and a PAC-Opaque in
2391
             * the SessionTicket extension, and the server resumes the session
2392
             * using the PAC-Opaque, it should echo the same Session ID in the
2393
             * ServerHello.
2394
             */
2395
0
            if (clienthello->session_id_len > 0) {
2396
0
                memcpy(s->session->session_id, clienthello->session_id,
2397
0
                    clienthello->session_id_len);
2398
0
                s->session->session_id_length = clienthello->session_id_len;
2399
0
            }
2400
2401
0
            ciphers = NULL;
2402
2403
            /* check if some cipher was preferred by call back */
2404
0
            if (pref_cipher == NULL)
2405
0
                pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
2406
0
                    SSL_get_ciphers(ssl));
2407
0
            if (pref_cipher == NULL) {
2408
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
2409
0
                goto err;
2410
0
            }
2411
2412
0
            s->session->cipher = pref_cipher;
2413
0
            sk_SSL_CIPHER_free(s->cipher_list);
2414
0
            s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
2415
0
            sk_SSL_CIPHER_free(s->cipher_list_by_id);
2416
0
            s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
2417
0
        }
2418
0
    }
2419
2420
    /*
2421
     * Worst case, we will use the NULL compression, but if we have other
2422
     * options, we will now look for them.  We have complen-1 compression
2423
     * algorithms from the client, starting at q.
2424
     */
2425
5.68k
    s->s3.tmp.new_compression = NULL;
2426
5.68k
    if (SSL_CONNECTION_IS_VERSION13(s)) {
2427
        /*
2428
         * We already checked above that the NULL compression method appears in
2429
         * the list. Now we check there aren't any others (which is illegal in
2430
         * a TLSv1.3 ClientHello.
2431
         */
2432
552
        if (clienthello->compressions_len != 1) {
2433
2
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2434
2
                SSL_R_INVALID_COMPRESSION_ALGORITHM);
2435
2
            goto err;
2436
2
        }
2437
552
    }
2438
5.12k
#ifndef OPENSSL_NO_COMP
2439
    /* This only happens if we have a cache hit */
2440
5.12k
    else if (s->session->compress_meth != 0) {
2441
0
        int m, comp_id = s->session->compress_meth;
2442
0
        unsigned int k;
2443
        /* Perform sanity checks on resumed compression algorithm */
2444
        /* Can't disable compression */
2445
0
        if (!ssl_allow_compression(s)) {
2446
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2447
0
                SSL_R_INCONSISTENT_COMPRESSION);
2448
0
            goto err;
2449
0
        }
2450
        /* Look for resumed compression method */
2451
0
        for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) {
2452
0
            comp = sk_SSL_COMP_value(sctx->comp_methods, m);
2453
0
            if (comp_id == comp->id) {
2454
0
                s->s3.tmp.new_compression = comp;
2455
0
                break;
2456
0
            }
2457
0
        }
2458
0
        if (s->s3.tmp.new_compression == NULL) {
2459
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2460
0
                SSL_R_INVALID_COMPRESSION_ALGORITHM);
2461
0
            goto err;
2462
0
        }
2463
        /* Look for resumed method in compression list */
2464
0
        for (k = 0; k < clienthello->compressions_len; k++) {
2465
0
            if (clienthello->compressions[k] == comp_id)
2466
0
                break;
2467
0
        }
2468
0
        if (k >= clienthello->compressions_len) {
2469
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2470
0
                SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
2471
0
            goto err;
2472
0
        }
2473
5.12k
    } else if (s->hit) {
2474
28
        comp = NULL;
2475
5.10k
    } else if (ssl_allow_compression(s) && sctx->comp_methods) {
2476
        /* See if we have a match */
2477
0
        int m, nn, v, done = 0;
2478
0
        unsigned int o;
2479
2480
0
        nn = sk_SSL_COMP_num(sctx->comp_methods);
2481
0
        for (m = 0; m < nn; m++) {
2482
0
            comp = sk_SSL_COMP_value(sctx->comp_methods, m);
2483
0
            v = comp->id;
2484
0
            for (o = 0; o < clienthello->compressions_len; o++) {
2485
0
                if (v == clienthello->compressions[o]) {
2486
0
                    done = 1;
2487
0
                    break;
2488
0
                }
2489
0
            }
2490
0
            if (done)
2491
0
                break;
2492
0
        }
2493
0
        if (done)
2494
0
            s->s3.tmp.new_compression = comp;
2495
0
        else
2496
0
            comp = NULL;
2497
0
    }
2498
#else
2499
    /*
2500
     * If compression is disabled we'd better not try to resume a session
2501
     * using compression.
2502
     */
2503
    if (s->session->compress_meth != 0) {
2504
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
2505
        goto err;
2506
    }
2507
#endif
2508
2509
    /*
2510
     * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2511
     */
2512
2513
5.67k
    if (!s->hit || SSL_CONNECTION_IS_VERSION13(s)) {
2514
5.65k
        sk_SSL_CIPHER_free(s->peer_ciphers);
2515
5.65k
        s->peer_ciphers = ciphers;
2516
5.65k
        if (ciphers == NULL) {
2517
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2518
0
            goto err;
2519
0
        }
2520
5.65k
        ciphers = NULL;
2521
5.65k
    }
2522
2523
5.67k
    if (!s->hit) {
2524
#ifdef OPENSSL_NO_COMP
2525
        s->session->compress_meth = 0;
2526
#else
2527
5.65k
        s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
2528
5.65k
#endif
2529
5.65k
    }
2530
2531
5.67k
    sk_SSL_CIPHER_free(ciphers);
2532
5.67k
    sk_SSL_CIPHER_free(scsvs);
2533
5.67k
    OPENSSL_free(clienthello->pre_proc_exts);
2534
5.67k
    OPENSSL_free(s->clienthello);
2535
5.67k
    s->clienthello = NULL;
2536
5.67k
    return 1;
2537
1.01k
err:
2538
1.01k
    sk_SSL_CIPHER_free(ciphers);
2539
1.01k
    sk_SSL_CIPHER_free(scsvs);
2540
1.01k
    if (clienthello != NULL)
2541
1.01k
        OPENSSL_free(clienthello->pre_proc_exts);
2542
1.01k
    OPENSSL_free(s->clienthello);
2543
1.01k
    s->clienthello = NULL;
2544
2545
1.01k
    return 0;
2546
5.67k
}
2547
2548
/*
2549
 * Call the status request callback if needed. Upon success, returns 1.
2550
 * Upon failure, returns 0.
2551
 */
2552
static int tls_handle_status_request(SSL_CONNECTION *s)
2553
19.1k
{
2554
19.1k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2555
2556
19.1k
    s->ext.status_expected = 0;
2557
2558
    /*
2559
     * If status request then ask callback what to do. Note: this must be
2560
     * called after servername callbacks in case the certificate has changed,
2561
     * and must be called after the cipher has been chosen because this may
2562
     * influence which certificate is sent
2563
     */
2564
19.1k
    if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && sctx != NULL
2565
944
        && sctx->ext.status_cb != NULL) {
2566
0
        int ret;
2567
2568
        /* If no certificate can't return certificate status */
2569
0
        if (s->s3.tmp.cert != NULL) {
2570
            /*
2571
             * Set current certificate to one we will use so SSL_get_certificate
2572
             * et al can pick it up.
2573
             */
2574
0
            s->cert->key = s->s3.tmp.cert;
2575
0
            ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s),
2576
0
                sctx->ext.status_arg);
2577
0
            switch (ret) {
2578
                /* We don't want to send a status request response */
2579
0
            case SSL_TLSEXT_ERR_NOACK:
2580
0
                s->ext.status_expected = 0;
2581
0
                break;
2582
                /* status request response should be sent */
2583
0
            case SSL_TLSEXT_ERR_OK:
2584
0
#ifndef OPENSSL_NO_OCSP
2585
0
                if (s->ext.ocsp.resp_ex != NULL
2586
0
                    && sk_OCSP_RESPONSE_num(s->ext.ocsp.resp_ex) > 0)
2587
0
                    s->ext.status_expected = 1;
2588
0
#endif
2589
0
                break;
2590
                /* something bad happened */
2591
0
            case SSL_TLSEXT_ERR_ALERT_FATAL:
2592
0
            default:
2593
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT);
2594
0
                return 0;
2595
0
            }
2596
0
        }
2597
0
    }
2598
2599
19.1k
    return 1;
2600
19.1k
}
2601
2602
/*
2603
 * Call the alpn_select callback if needed. Upon success, returns 1.
2604
 * Upon failure, returns 0.
2605
 */
2606
int tls_handle_alpn(SSL_CONNECTION *s)
2607
29.7k
{
2608
29.7k
    const unsigned char *selected = NULL;
2609
29.7k
    unsigned char selected_len = 0;
2610
29.7k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2611
2612
29.7k
    if (sctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) {
2613
0
        int r = sctx->ext.alpn_select_cb(SSL_CONNECTION_GET_USER_SSL(s),
2614
0
            &selected, &selected_len,
2615
0
            s->s3.alpn_proposed,
2616
0
            (unsigned int)s->s3.alpn_proposed_len,
2617
0
            sctx->ext.alpn_select_cb_arg);
2618
2619
0
        if (r == SSL_TLSEXT_ERR_OK) {
2620
0
            OPENSSL_free(s->s3.alpn_selected);
2621
0
            s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);
2622
0
            if (s->s3.alpn_selected == NULL) {
2623
0
                s->s3.alpn_selected_len = 0;
2624
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2625
0
                return 0;
2626
0
            }
2627
0
            s->s3.alpn_selected_len = selected_len;
2628
0
#ifndef OPENSSL_NO_NEXTPROTONEG
2629
            /* ALPN takes precedence over NPN. */
2630
0
            s->s3.npn_seen = 0;
2631
0
#endif
2632
2633
            /* Check ALPN is consistent with session */
2634
0
            if (s->session->ext.alpn_selected == NULL
2635
0
                || selected_len != s->session->ext.alpn_selected_len
2636
0
                || memcmp(selected, s->session->ext.alpn_selected,
2637
0
                       selected_len)
2638
0
                    != 0) {
2639
                /* Not consistent so can't be used for early_data */
2640
0
                s->ext.early_data_ok = 0;
2641
2642
0
                if (!s->hit) {
2643
                    /*
2644
                     * This is a new session and so alpn_selected should have
2645
                     * been initialised to NULL. We should update it with the
2646
                     * selected ALPN.
2647
                     */
2648
0
                    if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
2649
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2650
0
                            ERR_R_INTERNAL_ERROR);
2651
0
                        return 0;
2652
0
                    }
2653
0
                    s->session->ext.alpn_selected = OPENSSL_memdup(selected,
2654
0
                        selected_len);
2655
0
                    if (s->session->ext.alpn_selected == NULL) {
2656
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2657
0
                            ERR_R_INTERNAL_ERROR);
2658
0
                        return 0;
2659
0
                    }
2660
0
                    s->session->ext.alpn_selected_len = selected_len;
2661
0
                }
2662
0
            }
2663
2664
0
            return 1;
2665
0
        } else if (r != SSL_TLSEXT_ERR_NOACK) {
2666
0
            SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL,
2667
0
                SSL_R_NO_APPLICATION_PROTOCOL);
2668
0
            return 0;
2669
0
        }
2670
        /*
2671
         * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2672
         * present.
2673
         */
2674
0
    }
2675
2676
    /* Check ALPN is consistent with session */
2677
29.7k
    if (s->session->ext.alpn_selected != NULL) {
2678
        /* Not consistent so can't be used for early_data */
2679
0
        s->ext.early_data_ok = 0;
2680
0
    }
2681
2682
29.7k
    return 1;
2683
29.7k
}
2684
2685
WORK_STATE tls_post_process_client_hello(SSL_CONNECTION *s, WORK_STATE wst)
2686
13.8k
{
2687
13.8k
    const SSL_CIPHER *cipher;
2688
13.8k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2689
13.8k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
2690
2691
13.8k
    if (wst == WORK_MORE_A) {
2692
13.8k
        int rv = tls_early_post_process_client_hello(s);
2693
2694
13.8k
        if (rv == 0) {
2695
            /* SSLfatal() was already called */
2696
2.07k
            goto err;
2697
2.07k
        }
2698
11.7k
        if (rv < 0)
2699
0
            return WORK_MORE_A;
2700
11.7k
        wst = WORK_MORE_B;
2701
11.7k
    }
2702
11.7k
    if (wst == WORK_MORE_B) {
2703
11.7k
        if (!s->hit || SSL_CONNECTION_IS_VERSION13(s)) {
2704
            /* Let cert callback update server certificates if required */
2705
11.6k
            if (!s->hit && s->cert->cert_cb != NULL) {
2706
0
                int rv = s->cert->cert_cb(ussl, s->cert->cert_cb_arg);
2707
2708
0
                if (rv == 0) {
2709
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR);
2710
0
                    goto err;
2711
0
                }
2712
0
                if (rv < 0) {
2713
0
                    s->rwstate = SSL_X509_LOOKUP;
2714
0
                    return WORK_MORE_B;
2715
0
                }
2716
0
                s->rwstate = SSL_NOTHING;
2717
0
            }
2718
2719
            /* In TLSv1.3 we selected the ciphersuite before resumption */
2720
11.6k
            if (!SSL_CONNECTION_IS_VERSION13(s)) {
2721
10.4k
                cipher = ssl3_choose_cipher(s, s->peer_ciphers,
2722
10.4k
                    SSL_get_ciphers(ssl));
2723
2724
10.4k
                if (cipher == NULL) {
2725
390
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2726
390
                        SSL_R_NO_SHARED_CIPHER);
2727
390
                    goto err;
2728
390
                }
2729
10.0k
                s->s3.tmp.new_cipher = cipher;
2730
10.0k
            }
2731
11.2k
            if (!s->hit) {
2732
11.2k
                if (!tls_choose_sigalg(s, 1)) {
2733
                    /* SSLfatal already called */
2734
455
                    goto err;
2735
455
                }
2736
                /* check whether we should disable session resumption */
2737
10.8k
                if (s->not_resumable_session_cb != NULL)
2738
0
                    s->session->not_resumable = s->not_resumable_session_cb(ussl,
2739
0
                        ((s->s3.tmp.new_cipher->algorithm_mkey
2740
0
                             & (SSL_kDHE | SSL_kECDHE))
2741
0
                            != 0));
2742
10.8k
                if (s->session->not_resumable)
2743
                    /* do not send a session ticket */
2744
0
                    s->ext.ticket_expected = 0;
2745
10.8k
            }
2746
11.2k
        } else {
2747
            /* Session-id reuse */
2748
59
            s->s3.tmp.new_cipher = s->session->cipher;
2749
59
        }
2750
2751
        /*
2752
         * Call status_request callback if needed. Has to be done after the
2753
         * certificate callbacks etc above.
2754
         */
2755
10.8k
        if (!tls_handle_status_request(s)) {
2756
            /* SSLfatal() already called */
2757
0
            goto err;
2758
0
        }
2759
        /*
2760
         * Call alpn_select callback if needed.  Has to be done after SNI and
2761
         * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2762
         * we already did this because cipher negotiation happens earlier, and
2763
         * we must handle ALPN before we decide whether to accept early_data.
2764
         */
2765
10.8k
        if (!SSL_CONNECTION_IS_VERSION13(s) && !tls_handle_alpn(s)) {
2766
            /* SSLfatal() already called */
2767
0
            goto err;
2768
0
        }
2769
2770
10.8k
        wst = WORK_MORE_C;
2771
10.8k
    }
2772
10.8k
#ifndef OPENSSL_NO_SRP
2773
10.8k
    if (wst == WORK_MORE_C) {
2774
10.8k
        int ret;
2775
10.8k
        if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
2776
            /*
2777
             * callback indicates further work to be done
2778
             */
2779
0
            s->rwstate = SSL_X509_LOOKUP;
2780
0
            return WORK_MORE_C;
2781
0
        }
2782
10.8k
        if (ret < 0) {
2783
            /* SSLfatal() already called */
2784
0
            goto err;
2785
0
        }
2786
10.8k
    }
2787
10.8k
#endif
2788
2789
10.8k
    return WORK_FINISHED_STOP;
2790
2.92k
err:
2791
2.92k
    return WORK_ERROR;
2792
10.8k
}
2793
2794
CON_FUNC_RETURN tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt)
2795
10.8k
{
2796
10.8k
    int compm;
2797
10.8k
    size_t sl, len;
2798
10.8k
    int version;
2799
10.8k
    unsigned char *session_id;
2800
10.8k
    int usetls13 = SSL_CONNECTION_IS_TLS13(s)
2801
9.70k
        || (!SSL_CONNECTION_IS_DTLS(s)
2802
4.18k
            && s->hello_retry_request == SSL_HRR_PENDING);
2803
10.8k
    int usedtls13 = SSL_CONNECTION_IS_DTLS13(s)
2804
10.8k
        || (SSL_CONNECTION_IS_DTLS(s)
2805
5.52k
            && s->hello_retry_request == SSL_HRR_PENDING);
2806
2807
10.8k
    version = usetls13 ? TLS1_2_VERSION : (usedtls13 ? DTLS1_2_VERSION : s->version);
2808
10.8k
    if (!WPACKET_put_bytes_u16(pkt, version)
2809
        /*
2810
         * Random stuff. Filling of the server_random takes place in
2811
         * tls_process_client_hello()
2812
         */
2813
10.8k
        || !WPACKET_memcpy(pkt,
2814
10.8k
            s->hello_retry_request == SSL_HRR_PENDING
2815
10.8k
                ? hrrrandom
2816
10.8k
                : s->s3.server_random,
2817
10.8k
            SSL3_RANDOM_SIZE)) {
2818
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2819
0
        return CON_FUNC_ERROR;
2820
0
    }
2821
2822
    /*-
2823
     * There are several cases for the session ID to send
2824
     * back in the server hello:
2825
     * - For session reuse from the session cache,
2826
     *   we send back the old session ID.
2827
     * - If stateless session reuse (using a session ticket)
2828
     *   is successful, we send back the client's "session ID"
2829
     *   (which doesn't actually identify the session).
2830
     * - If it is a new session, we send back the new
2831
     *   session ID.
2832
     * - However, if we want the new session to be single-use,
2833
     *   we send back a 0-length session ID.
2834
     * - In TLSv1.3 we echo back the session id sent to us by the client
2835
     *   regardless
2836
     * - In DTLSv1.3 we must not echo the session id sent by the client
2837
     * s->hit is non-zero in either case of session reuse,
2838
     * so the following won't overwrite an ID that we're supposed
2839
     * to send back.
2840
     */
2841
10.8k
    if (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER)
2842
0
        && !s->hit)
2843
0
        s->session->session_id_length = 0;
2844
2845
10.8k
    if (usetls13) {
2846
1.17k
        sl = s->tmp_session_id_len;
2847
1.17k
        session_id = s->tmp_session_id;
2848
9.70k
    } else if (usedtls13) {
2849
0
        sl = 0;
2850
0
        session_id = NULL;
2851
9.70k
    } else {
2852
9.70k
        sl = s->session->session_id_length;
2853
9.70k
        session_id = s->session->session_id;
2854
9.70k
    }
2855
2856
10.8k
    if (sl > sizeof(s->session->session_id)) {
2857
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2858
0
        return CON_FUNC_ERROR;
2859
0
    }
2860
2861
    /* set up the compression method */
2862
#ifdef OPENSSL_NO_COMP
2863
    compm = 0;
2864
#else
2865
10.8k
    if (usetls13 || usedtls13 || s->s3.tmp.new_compression == NULL)
2866
10.8k
        compm = 0;
2867
0
    else
2868
0
        compm = s->s3.tmp.new_compression->id;
2869
10.8k
#endif
2870
2871
10.8k
    if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
2872
10.8k
        || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher,
2873
10.8k
            pkt, &len)
2874
10.8k
        || !WPACKET_put_bytes_u8(pkt, compm)) {
2875
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2876
0
        return CON_FUNC_ERROR;
2877
0
    }
2878
2879
10.8k
    if (!tls_construct_extensions(s, pkt,
2880
10.8k
            s->hello_retry_request == SSL_HRR_PENDING
2881
10.8k
                ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2882
10.8k
                : (SSL_CONNECTION_IS_VERSION13(s)
2883
10.7k
                          ? SSL_EXT_TLS1_3_SERVER_HELLO
2884
10.7k
                          : SSL_EXT_TLS1_2_SERVER_HELLO),
2885
10.8k
            NULL, 0)) {
2886
        /* SSLfatal() already called */
2887
44
        return CON_FUNC_ERROR;
2888
44
    }
2889
2890
10.8k
    if (s->hello_retry_request == SSL_HRR_PENDING) {
2891
        /* Ditch the session. We'll create a new one next time around */
2892
170
        SSL_SESSION_free(s->session);
2893
170
        s->session = NULL;
2894
170
        s->hit = 0;
2895
2896
        /*
2897
         * Re-initialise the Transcript Hash. We're going to prepopulate it with
2898
         * a synthetic message_hash in place of ClientHello1.
2899
         */
2900
170
#ifndef OPENSSL_NO_ECH
2901
        /*
2902
         * if we're sending 2nd SH after HRR and we did ECH
2903
         * then we want to inject the hash of the inner CH1
2904
         * and not the outer (which is the default)
2905
         */
2906
170
        OSSL_TRACE_BEGIN(TLS)
2907
0
        {
2908
0
            BIO_printf(trc_out, "Checking success (%d)/innerCH (%p)\n",
2909
0
                s->ext.ech.success, (void *)s->ext.ech.innerch);
2910
0
        }
2911
170
        OSSL_TRACE_END(TLS);
2912
170
        if ((s->ext.ech.backend == 1 || s->ext.ech.success == 1)
2913
2
            && s->ext.ech.innerch != NULL) {
2914
            /* do pre-existing HRR stuff */
2915
2
            unsigned char hashval[EVP_MAX_MD_SIZE];
2916
2
            unsigned int hashlen;
2917
2
            EVP_MD_CTX *ctx = EVP_MD_CTX_new();
2918
2
            const EVP_MD *md = NULL;
2919
2920
2
            OSSL_TRACE(TLS, "Adding in digest of ClientHello\n");
2921
2
#ifdef OSSL_ECH_SUPERVERBOSE
2922
2
            ossl_ech_pbuf("innerch", s->ext.ech.innerch,
2923
2
                s->ext.ech.innerch_len);
2924
2
#endif
2925
2
            if (ctx == NULL) {
2926
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2927
0
                return CON_FUNC_ERROR;
2928
0
            }
2929
2
            md = ssl_handshake_md(s);
2930
2
            if (md == NULL) {
2931
0
                EVP_MD_CTX_free(ctx);
2932
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2933
0
                return CON_FUNC_ERROR;
2934
0
            }
2935
2
            if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
2936
2
                || EVP_DigestUpdate(ctx, s->ext.ech.innerch,
2937
2
                       s->ext.ech.innerch_len)
2938
2
                    <= 0
2939
2
                || EVP_DigestFinal_ex(ctx, hashval, &hashlen) <= 0) {
2940
0
                EVP_MD_CTX_free(ctx);
2941
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2942
0
                return CON_FUNC_ERROR;
2943
0
            }
2944
2
#ifdef OSSL_ECH_SUPERVERBOSE
2945
2
            ossl_ech_pbuf("digested CH", hashval, hashlen);
2946
2
#endif
2947
2
            EVP_MD_CTX_free(ctx);
2948
2
            if (ossl_ech_reset_hs_buffer(s, NULL, 0) != 1) {
2949
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2950
0
                return CON_FUNC_ERROR;
2951
0
            }
2952
2
            if (!create_synthetic_message_hash(s, hashval, hashlen, NULL, 0)) {
2953
                /* SSLfatal() already called */
2954
0
                return CON_FUNC_ERROR;
2955
0
            }
2956
168
        } else {
2957
168
            if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0))
2958
0
                return CON_FUNC_ERROR; /* SSLfatal() already called */
2959
168
        }
2960
#else
2961
        if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0))
2962
            /* SSLfatal() already called */
2963
            return CON_FUNC_ERROR;
2964
#endif /* OPENSSL_NO_ECH */
2965
10.6k
    } else if (!(s->verify_mode & SSL_VERIFY_PEER)
2966
10.6k
        && !ssl3_digest_cached_records(s, 0)) {
2967
0
        /* SSLfatal() already called */;
2968
0
        return CON_FUNC_ERROR;
2969
0
    }
2970
2971
10.8k
#ifndef OPENSSL_NO_ECH
2972
    /*
2973
     * Calculate the ECH-accept server random to indicate that
2974
     * we're accepting ECH, if that's the case
2975
     */
2976
10.8k
    if (s->ext.ech.attempted_type == TLSEXT_TYPE_ech
2977
67
        && (s->ext.ech.backend == 1
2978
67
            || (s->ext.ech.es != NULL && s->ext.ech.success == 1))) {
2979
67
        unsigned char acbuf[8];
2980
67
        unsigned char *shbuf = NULL;
2981
67
        size_t shlen = 0;
2982
67
        size_t shoffset = 0;
2983
67
        int hrr = 0;
2984
2985
67
        if (s->hello_retry_request == SSL_HRR_PENDING)
2986
2
            hrr = 1;
2987
67
        memset(acbuf, 0, 8);
2988
67
        if (WPACKET_get_total_written(pkt, &shlen) != 1) {
2989
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2990
0
            return CON_FUNC_ERROR;
2991
0
        }
2992
67
        shbuf = WPACKET_get_curr(pkt) - shlen;
2993
        /* we need to fixup SH length here */
2994
67
        shbuf[1] = ((shlen - 4)) >> 16 & 0xff;
2995
67
        shbuf[2] = ((shlen - 4)) >> 8 & 0xff;
2996
67
        shbuf[3] = (shlen - 4) & 0xff;
2997
67
        if (ossl_ech_intbuf_add(s, shbuf, shlen, hrr) != 1) {
2998
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2999
0
            return CON_FUNC_ERROR;
3000
0
        }
3001
67
        if (ossl_ech_calc_confirm(s, hrr, acbuf, shlen) != 1) {
3002
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3003
0
            return CON_FUNC_ERROR;
3004
0
        }
3005
67
        memcpy(s->s3.server_random + SSL3_RANDOM_SIZE - 8, acbuf, 8);
3006
67
        if (hrr == 0) {
3007
            /* confirm value hacked into SH.random rightmost octets */
3008
65
            shoffset = SSL3_HM_HEADER_LENGTH /* 4 */
3009
65
                + CLIENT_VERSION_LEN /* 2 */
3010
65
                + SSL3_RANDOM_SIZE /* 32 */
3011
65
                - 8;
3012
65
            memcpy(shbuf + shoffset, acbuf, 8);
3013
65
        } else {
3014
            /*
3015
             * confirm value is in extension in HRR case as the SH.random
3016
             * is already hacked to be a specific value in a HRR
3017
             */
3018
2
            memcpy(WPACKET_get_curr(pkt) - 8, acbuf, 8);
3019
2
        }
3020
67
    }
3021
    /* call ECH callback, if appropriate */
3022
10.8k
    if (s->ext.ech.attempted == 1 && s->ext.ech.cb != NULL
3023
0
        && s->hello_retry_request != SSL_HRR_PENDING) {
3024
0
        char pstr[OSSL_ECH_PBUF_SIZE + 1];
3025
0
        BIO *biom = BIO_new(BIO_s_mem());
3026
0
        unsigned int cbrv = 0;
3027
3028
0
        if (biom == NULL) {
3029
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3030
0
            return CON_FUNC_ERROR;
3031
0
        }
3032
0
        memset(pstr, 0, OSSL_ECH_PBUF_SIZE + 1);
3033
0
        ossl_ech_status_print(biom, s, OSSL_ECHSTORE_ALL);
3034
0
        BIO_read(biom, pstr, OSSL_ECH_PBUF_SIZE);
3035
0
        cbrv = s->ext.ech.cb(&s->ssl, pstr);
3036
0
        BIO_free(biom);
3037
0
        if (cbrv != 1) {
3038
0
            OSSL_TRACE(TLS, "Error from tls_construct_server_hello/ech_cb\n");
3039
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3040
0
            return CON_FUNC_ERROR;
3041
0
        }
3042
0
    }
3043
10.8k
#endif /* OPENSSL_NO_ECH */
3044
10.8k
    return CON_FUNC_SUCCESS;
3045
10.8k
}
3046
3047
CON_FUNC_RETURN tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt)
3048
12.3k
{
3049
12.3k
    if (s->s3.tmp.cert_request == 0) {
3050
12.3k
        if (!ssl3_digest_cached_records(s, 0)) {
3051
            /* SSLfatal() already called */
3052
0
            return CON_FUNC_ERROR;
3053
0
        }
3054
12.3k
    }
3055
12.3k
    return CON_FUNC_SUCCESS;
3056
12.3k
}
3057
3058
CON_FUNC_RETURN tls_construct_server_key_exchange(SSL_CONNECTION *s,
3059
    WPACKET *pkt)
3060
7.65k
{
3061
7.65k
    EVP_PKEY *pkdh = NULL;
3062
7.65k
    unsigned char *encodedPoint = NULL;
3063
7.65k
    size_t encodedlen = 0;
3064
7.65k
    int group_id = 0;
3065
7.65k
    const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
3066
7.65k
    int i;
3067
7.65k
    unsigned long type;
3068
7.65k
    BIGNUM *r[4];
3069
7.65k
    EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
3070
7.65k
    EVP_PKEY_CTX *pctx = NULL;
3071
7.65k
    size_t paramlen, paramoffset;
3072
7.65k
    int freer = 0;
3073
7.65k
    CON_FUNC_RETURN ret = CON_FUNC_ERROR;
3074
7.65k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3075
3076
7.65k
    if (!WPACKET_get_total_written(pkt, &paramoffset)) {
3077
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3078
0
        goto err;
3079
0
    }
3080
3081
7.65k
    if (md_ctx == NULL) {
3082
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3083
0
        goto err;
3084
0
    }
3085
3086
7.65k
    type = s->s3.tmp.new_cipher->algorithm_mkey;
3087
3088
7.65k
    r[0] = r[1] = r[2] = r[3] = NULL;
3089
7.65k
#ifndef OPENSSL_NO_PSK
3090
    /* Plain PSK or RSAPSK nothing to do */
3091
7.65k
    if (type & (SSL_kPSK | SSL_kRSAPSK)) {
3092
0
    } else
3093
7.65k
#endif /* !OPENSSL_NO_PSK */
3094
7.65k
        if (type & (SSL_kDHE | SSL_kDHEPSK)) {
3095
1.87k
            CERT *cert = s->cert;
3096
1.87k
            EVP_PKEY *pkdhp = NULL;
3097
3098
            /* Get NID of appropriate shared FFDHE group */
3099
1.87k
            group_id = tls1_shared_group(s, TLS1_GROUPS_RETURN_TMP_ID,
3100
1.87k
                TLS1_GROUPS_FFDHE_GROUPS);
3101
1.87k
            if (group_id != 0) {
3102
                /* Cache the group used in the SSL_SESSION */
3103
1.87k
                s->session->kex_group = group_id;
3104
3105
1.87k
                s->s3.tmp.pkey = ssl_generate_pkey_group(s, group_id);
3106
1.87k
                if (s->s3.tmp.pkey == NULL) {
3107
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3108
0
                    goto err;
3109
0
                }
3110
1.87k
            } else {
3111
3112
0
                if (s->cert->dh_tmp_auto) {
3113
0
                    pkdh = ssl_get_auto_dh(s);
3114
0
                    if (pkdh == NULL) {
3115
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3116
0
                        goto err;
3117
0
                    }
3118
0
                    pkdhp = pkdh;
3119
0
                } else {
3120
0
                    pkdhp = cert->dh_tmp;
3121
0
                }
3122
0
#if !defined(OPENSSL_NO_DEPRECATED_3_0)
3123
0
                if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
3124
0
                    pkdh = ssl_dh_to_pkey(
3125
0
                        s->cert->dh_tmp_cb(SSL_CONNECTION_GET_USER_SSL(s), 0, 1024));
3126
0
                    if (pkdh == NULL) {
3127
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3128
0
                        goto err;
3129
0
                    }
3130
0
                    pkdhp = pkdh;
3131
0
                }
3132
0
#endif
3133
0
                if (pkdhp == NULL) {
3134
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
3135
0
                    goto err;
3136
0
                }
3137
0
                if (!ssl_security(s, SSL_SECOP_TMP_DH,
3138
0
                        EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) {
3139
0
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
3140
0
                    goto err;
3141
0
                }
3142
0
                if (s->s3.tmp.pkey != NULL) {
3143
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3144
0
                    goto err;
3145
0
                }
3146
3147
0
                s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
3148
0
                if (s->s3.tmp.pkey == NULL) {
3149
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3150
0
                    goto err;
3151
0
                }
3152
3153
0
                EVP_PKEY_free(pkdh);
3154
0
                pkdh = NULL;
3155
0
            }
3156
3157
            /* These BIGNUMs need to be freed when we're finished */
3158
1.87k
            freer = 1;
3159
1.87k
            if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,
3160
1.87k
                    &r[0])
3161
1.87k
                || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,
3162
1.87k
                    &r[1])
3163
1.87k
                || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,
3164
1.87k
                    OSSL_PKEY_PARAM_PUB_KEY, &r[2])) {
3165
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3166
0
                goto err;
3167
0
            }
3168
5.78k
        } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
3169
3170
5.78k
            if (s->s3.tmp.pkey != NULL) {
3171
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3172
0
                goto err;
3173
0
            }
3174
3175
            /* Get NID of appropriate shared ECDHE curve */
3176
5.78k
            group_id = tls1_shared_group(s, TLS1_GROUPS_RETURN_TMP_ID,
3177
5.78k
                TLS1_GROUPS_NON_FFDHE_GROUPS);
3178
5.78k
            if (group_id == 0) {
3179
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3180
0
                    SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
3181
0
                goto err;
3182
0
            }
3183
            /* Cache the group used in the SSL_SESSION */
3184
5.78k
            s->session->kex_group = group_id;
3185
            /* Generate a new key for this curve */
3186
5.78k
            s->s3.tmp.pkey = ssl_generate_pkey_group(s, group_id);
3187
5.78k
            if (s->s3.tmp.pkey == NULL) {
3188
                /* SSLfatal() already called */
3189
0
                goto err;
3190
0
            }
3191
3192
            /* Encode the public key. */
3193
5.78k
            encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,
3194
5.78k
                &encodedPoint);
3195
5.78k
            if (encodedlen == 0) {
3196
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
3197
0
                goto err;
3198
0
            }
3199
3200
            /*
3201
             * We'll generate the serverKeyExchange message explicitly so we
3202
             * can set these to NULLs
3203
             */
3204
5.78k
            r[0] = NULL;
3205
5.78k
            r[1] = NULL;
3206
5.78k
            r[2] = NULL;
3207
5.78k
            r[3] = NULL;
3208
5.78k
        } else
3209
0
#ifndef OPENSSL_NO_SRP
3210
0
            if (type & SSL_kSRP) {
3211
0
            if ((s->srp_ctx.N == NULL) || (s->srp_ctx.g == NULL) || (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
3212
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);
3213
0
                goto err;
3214
0
            }
3215
0
            r[0] = s->srp_ctx.N;
3216
0
            r[1] = s->srp_ctx.g;
3217
0
            r[2] = s->srp_ctx.s;
3218
0
            r[3] = s->srp_ctx.B;
3219
0
        } else
3220
0
#endif
3221
0
        {
3222
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3223
0
            goto err;
3224
0
        }
3225
3226
7.65k
    if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
3227
6.53k
        || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
3228
1.11k
        lu = NULL;
3229
6.53k
    } else if (lu == NULL) {
3230
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
3231
0
        goto err;
3232
0
    }
3233
3234
7.65k
#ifndef OPENSSL_NO_PSK
3235
7.65k
    if (type & SSL_PSK) {
3236
0
        size_t len = (s->cert->psk_identity_hint == NULL)
3237
0
            ? 0
3238
0
            : strlen(s->cert->psk_identity_hint);
3239
3240
        /*
3241
         * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
3242
         * checked this when we set the identity hint - but just in case
3243
         */
3244
0
        if (len > PSK_MAX_IDENTITY_LEN
3245
0
            || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
3246
0
                len)) {
3247
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3248
0
            goto err;
3249
0
        }
3250
0
    }
3251
7.65k
#endif
3252
3253
13.2k
    for (i = 0; i < 4 && r[i] != NULL; i++) {
3254
5.61k
        unsigned char *binval;
3255
5.61k
        int res;
3256
3257
5.61k
#ifndef OPENSSL_NO_SRP
3258
5.61k
        if ((i == 2) && (type & SSL_kSRP)) {
3259
0
            res = WPACKET_start_sub_packet_u8(pkt);
3260
0
        } else
3261
5.61k
#endif
3262
5.61k
            res = WPACKET_start_sub_packet_u16(pkt);
3263
3264
5.61k
        if (!res) {
3265
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3266
0
            goto err;
3267
0
        }
3268
3269
        /*-
3270
         * for interoperability with some versions of the Microsoft TLS
3271
         * stack, we need to zero pad the DHE pub key to the same length
3272
         * as the prime
3273
         */
3274
5.61k
        if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
3275
1.87k
            size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
3276
3277
1.87k
            if (len > 0) {
3278
0
                if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
3279
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3280
0
                    goto err;
3281
0
                }
3282
0
                memset(binval, 0, len);
3283
0
            }
3284
1.87k
        }
3285
3286
5.61k
        if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
3287
5.61k
            || !WPACKET_close(pkt)) {
3288
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3289
0
            goto err;
3290
0
        }
3291
3292
5.61k
        BN_bn2bin(r[i], binval);
3293
5.61k
    }
3294
3295
7.65k
    if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
3296
        /*
3297
         * We only support named (not generic) curves. In this situation, the
3298
         * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
3299
         * [1 byte length of encoded point], followed by the actual encoded
3300
         * point itself
3301
         */
3302
5.78k
        if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
3303
5.78k
            || !WPACKET_put_bytes_u16(pkt, group_id)
3304
5.78k
            || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
3305
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3306
0
            goto err;
3307
0
        }
3308
5.78k
        OPENSSL_free(encodedPoint);
3309
5.78k
        encodedPoint = NULL;
3310
5.78k
    }
3311
3312
    /* not anonymous */
3313
7.65k
    if (lu != NULL) {
3314
6.53k
        EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
3315
6.53k
        const EVP_MD *md;
3316
6.53k
        unsigned char *sigbytes1, *sigbytes2, *tbs;
3317
6.53k
        size_t siglen = 0, tbslen;
3318
3319
6.53k
        if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) {
3320
            /* Should never happen */
3321
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3322
0
            goto err;
3323
0
        }
3324
        /* Get length of the parameters we have written above */
3325
6.53k
        if (!WPACKET_get_length(pkt, &paramlen)) {
3326
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3327
0
            goto err;
3328
0
        }
3329
        /* send signature algorithm */
3330
6.53k
        if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
3331
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3332
0
            goto err;
3333
0
        }
3334
3335
6.53k
        if (EVP_DigestSignInit_ex(md_ctx, &pctx,
3336
6.53k
                md == NULL ? NULL : EVP_MD_get0_name(md),
3337
6.53k
                sctx->libctx, sctx->propq, pkey,
3338
6.53k
                NULL)
3339
6.53k
            <= 0) {
3340
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3341
0
            goto err;
3342
0
        }
3343
6.53k
        if (lu->sig == EVP_PKEY_RSA_PSS) {
3344
480
            if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
3345
480
                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
3346
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3347
0
                goto err;
3348
0
            }
3349
480
        }
3350
6.53k
        tbslen = construct_key_exchange_tbs(s, &tbs,
3351
6.53k
            s->init_buf->data + paramoffset,
3352
6.53k
            paramlen);
3353
6.53k
        if (tbslen == 0) {
3354
            /* SSLfatal() already called */
3355
0
            goto err;
3356
0
        }
3357
3358
6.53k
        if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <= 0
3359
6.53k
            || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
3360
6.53k
            || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
3361
6.50k
            || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
3362
6.50k
            || sigbytes1 != sigbytes2) {
3363
34
            OPENSSL_free(tbs);
3364
34
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3365
34
            goto err;
3366
34
        }
3367
6.50k
        OPENSSL_free(tbs);
3368
6.50k
    }
3369
3370
7.62k
    ret = CON_FUNC_SUCCESS;
3371
7.65k
err:
3372
7.65k
    EVP_PKEY_free(pkdh);
3373
7.65k
    OPENSSL_free(encodedPoint);
3374
7.65k
    EVP_MD_CTX_free(md_ctx);
3375
7.65k
    if (freer) {
3376
1.87k
        BN_free(r[0]);
3377
1.87k
        BN_free(r[1]);
3378
1.87k
        BN_free(r[2]);
3379
1.87k
        BN_free(r[3]);
3380
1.87k
    }
3381
7.65k
    return ret;
3382
7.62k
}
3383
3384
CON_FUNC_RETURN tls_construct_certificate_request(SSL_CONNECTION *s,
3385
    WPACKET *pkt)
3386
0
{
3387
0
    if (SSL_CONNECTION_IS_VERSION13(s)) {
3388
        /* Send random context when doing post-handshake auth */
3389
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
3390
0
            OPENSSL_free(s->pha_context);
3391
0
            s->pha_context_len = 32;
3392
0
            if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
3393
0
                s->pha_context_len = 0;
3394
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3395
0
                return CON_FUNC_ERROR;
3396
0
            }
3397
0
            if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
3398
0
                    s->pha_context, s->pha_context_len, 0)
3399
0
                    <= 0
3400
0
                || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
3401
0
                    s->pha_context_len)) {
3402
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3403
0
                return CON_FUNC_ERROR;
3404
0
            }
3405
            /* reset the handshake hash back to just after the ClientFinished */
3406
0
            if (!tls13_restore_handshake_digest_for_pha(s)) {
3407
                /* SSLfatal() already called */
3408
0
                return CON_FUNC_ERROR;
3409
0
            }
3410
0
        } else {
3411
0
            if (!WPACKET_put_bytes_u8(pkt, 0)) {
3412
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3413
0
                return CON_FUNC_ERROR;
3414
0
            }
3415
0
        }
3416
3417
0
        if (!tls_construct_extensions(s, pkt,
3418
0
                SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
3419
0
                0)) {
3420
            /* SSLfatal() already called */
3421
0
            return CON_FUNC_ERROR;
3422
0
        }
3423
0
        goto done;
3424
0
    }
3425
3426
    /* get the list of acceptable cert types */
3427
0
    if (!WPACKET_start_sub_packet_u8(pkt)
3428
0
        || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
3429
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3430
0
        return CON_FUNC_ERROR;
3431
0
    }
3432
3433
0
    if (SSL_USE_SIGALGS(s)) {
3434
0
        const uint16_t *psigs;
3435
0
        size_t nl = tls12_get_psigalgs(s, 1, &psigs);
3436
3437
0
        if (!WPACKET_start_sub_packet_u16(pkt)
3438
0
            || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
3439
0
            || !tls12_copy_sigalgs(s, pkt, psigs, nl)
3440
0
            || !WPACKET_close(pkt)) {
3441
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3442
0
            return CON_FUNC_ERROR;
3443
0
        }
3444
0
    }
3445
3446
0
    if (!construct_ca_names(s, get_ca_names(s), pkt)) {
3447
        /* SSLfatal() already called */
3448
0
        return CON_FUNC_ERROR;
3449
0
    }
3450
3451
0
done:
3452
0
    s->certreqs_sent++;
3453
0
    s->s3.tmp.cert_request = 1;
3454
0
    return CON_FUNC_SUCCESS;
3455
0
}
3456
3457
static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
3458
0
{
3459
0
#ifndef OPENSSL_NO_PSK
3460
0
    unsigned char psk[PSK_MAX_PSK_LEN];
3461
0
    size_t psklen;
3462
0
    PACKET psk_identity;
3463
3464
0
    if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
3465
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3466
0
        return 0;
3467
0
    }
3468
0
    if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
3469
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);
3470
0
        return 0;
3471
0
    }
3472
0
    if (s->psk_server_callback == NULL) {
3473
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);
3474
0
        return 0;
3475
0
    }
3476
3477
0
    if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
3478
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3479
0
        return 0;
3480
0
    }
3481
3482
0
    psklen = s->psk_server_callback(SSL_CONNECTION_GET_USER_SSL(s),
3483
0
        s->session->psk_identity,
3484
0
        psk, sizeof(psk));
3485
3486
0
    if (psklen > PSK_MAX_PSK_LEN) {
3487
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3488
0
        return 0;
3489
0
    } else if (psklen == 0) {
3490
        /*
3491
         * PSK related to the given identity not found
3492
         */
3493
0
        SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);
3494
0
        return 0;
3495
0
    }
3496
3497
0
    OPENSSL_free(s->s3.tmp.psk);
3498
0
    s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
3499
0
    OPENSSL_cleanse(psk, psklen);
3500
3501
0
    if (s->s3.tmp.psk == NULL) {
3502
0
        s->s3.tmp.psklen = 0;
3503
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3504
0
        return 0;
3505
0
    }
3506
3507
0
    s->s3.tmp.psklen = psklen;
3508
3509
0
    return 1;
3510
#else
3511
    /* Should never happen */
3512
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3513
    return 0;
3514
#endif
3515
0
}
3516
3517
static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt)
3518
3.76k
{
3519
3.76k
    size_t outlen;
3520
3.76k
    PACKET enc_premaster;
3521
3.76k
    EVP_PKEY *rsa = NULL;
3522
3.76k
    unsigned char *rsa_decrypt = NULL;
3523
3.76k
    int ret = 0;
3524
3.76k
    EVP_PKEY_CTX *ctx = NULL;
3525
3.76k
    OSSL_PARAM params[3], *p = params;
3526
3.76k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3527
3528
3.76k
    rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
3529
3.76k
    if (rsa == NULL) {
3530
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);
3531
0
        return 0;
3532
0
    }
3533
3534
    /* pre-standard DTLS omits the length bytes. */
3535
3.76k
    if (s->version == DTLS1_BAD_VER) {
3536
0
        enc_premaster = *pkt;
3537
3.76k
    } else {
3538
3.76k
        if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
3539
3.70k
            || PACKET_remaining(pkt) != 0) {
3540
96
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3541
96
            return 0;
3542
96
        }
3543
3.76k
    }
3544
3545
3.66k
    outlen = SSL_MAX_MASTER_KEY_LENGTH;
3546
3.66k
    rsa_decrypt = OPENSSL_malloc(outlen);
3547
3.66k
    if (rsa_decrypt == NULL) {
3548
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3549
0
        return 0;
3550
0
    }
3551
3552
3.66k
    ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq);
3553
3.66k
    if (ctx == NULL) {
3554
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3555
0
        goto err;
3556
0
    }
3557
3558
    /*
3559
     * We must not leak whether a decryption failure occurs because of
3560
     * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
3561
     * section 7.4.7.1). We use the special padding type
3562
     * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
3563
     * RSA, check the padding and check that the client version is as expected
3564
     * in the premaster secret. If any of that fails then the function appears
3565
     * to return successfully but with a random result. The call below could
3566
     * still fail if the input is publicly invalid.
3567
     * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
3568
     */
3569
3.66k
    if (EVP_PKEY_decrypt_init(ctx) <= 0
3570
3.66k
        || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
3571
0
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
3572
0
        goto err;
3573
0
    }
3574
3575
3.66k
    *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
3576
3.66k
        (unsigned int *)&s->client_version);
3577
3.66k
    if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
3578
0
        *p++ = OSSL_PARAM_construct_uint(
3579
0
            OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
3580
0
            (unsigned int *)&s->version);
3581
3.66k
    *p++ = OSSL_PARAM_construct_end();
3582
3583
3.66k
    if (!EVP_PKEY_CTX_set_params(ctx, params)
3584
3.66k
        || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
3585
3.66k
               PACKET_data(&enc_premaster),
3586
3.66k
               PACKET_remaining(&enc_premaster))
3587
3.66k
            <= 0) {
3588
27
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
3589
27
        goto err;
3590
27
    }
3591
3592
    /*
3593
     * This test should never fail (otherwise we should have failed above) but
3594
     * we double check anyway.
3595
     */
3596
3.63k
    if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
3597
0
        OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
3598
0
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
3599
0
        goto err;
3600
0
    }
3601
3602
    /* Also cleanses rsa_decrypt (on success or failure) */
3603
3.63k
    if (!ssl_generate_master_secret(s, rsa_decrypt, outlen, 0)) {
3604
        /* SSLfatal() already called */
3605
0
        goto err;
3606
0
    }
3607
3608
3.63k
    ret = 1;
3609
3.66k
err:
3610
3.66k
    OPENSSL_free(rsa_decrypt);
3611
3.66k
    EVP_PKEY_CTX_free(ctx);
3612
3.66k
    return ret;
3613
3.63k
}
3614
3615
static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt)
3616
984
{
3617
984
    EVP_PKEY *skey = NULL;
3618
984
    unsigned int i;
3619
984
    const unsigned char *data;
3620
984
    EVP_PKEY *ckey = NULL;
3621
984
    int ret = 0;
3622
3623
984
    if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
3624
62
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
3625
62
        goto err;
3626
62
    }
3627
922
    skey = s->s3.tmp.pkey;
3628
922
    if (skey == NULL) {
3629
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
3630
0
        goto err;
3631
0
    }
3632
3633
922
    if (PACKET_remaining(pkt) == 0L) {
3634
4
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);
3635
4
        goto err;
3636
4
    }
3637
918
    if (!PACKET_get_bytes(pkt, &data, i)) {
3638
        /* We already checked we have enough data */
3639
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3640
0
        goto err;
3641
0
    }
3642
918
    ckey = EVP_PKEY_new();
3643
918
    if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
3644
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
3645
0
        goto err;
3646
0
    }
3647
3648
918
    if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
3649
15
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
3650
15
        goto err;
3651
15
    }
3652
3653
903
    if (ssl_derive(s, skey, ckey, 1) == 0) {
3654
        /* SSLfatal() already called */
3655
0
        goto err;
3656
0
    }
3657
3658
903
    ret = 1;
3659
903
    EVP_PKEY_free(s->s3.tmp.pkey);
3660
903
    s->s3.tmp.pkey = NULL;
3661
984
err:
3662
984
    EVP_PKEY_free(ckey);
3663
984
    return ret;
3664
903
}
3665
3666
static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt)
3667
4.90k
{
3668
4.90k
    EVP_PKEY *skey = s->s3.tmp.pkey;
3669
4.90k
    EVP_PKEY *ckey = NULL;
3670
4.90k
    int ret = 0;
3671
3672
4.90k
    if (PACKET_remaining(pkt) == 0L) {
3673
        /* We don't support ECDH client auth */
3674
16
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);
3675
16
        goto err;
3676
4.88k
    } else {
3677
4.88k
        unsigned int i;
3678
4.88k
        const unsigned char *data;
3679
3680
        /*
3681
         * Get client's public key from encoded point in the
3682
         * ClientKeyExchange message.
3683
         */
3684
3685
        /*
3686
         * Get encoded point length
3687
         * empty key should be handled here
3688
         */
3689
4.88k
        if (!PACKET_get_1(pkt, &i) || i == 0 || !PACKET_get_bytes(pkt, &data, i)
3690
4.80k
            || PACKET_remaining(pkt) != 0) {
3691
127
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3692
127
            goto err;
3693
127
        }
3694
4.75k
        if (skey == NULL) {
3695
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);
3696
0
            goto err;
3697
0
        }
3698
3699
4.75k
        ckey = EVP_PKEY_new();
3700
4.75k
        if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
3701
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
3702
0
            goto err;
3703
0
        }
3704
3705
4.75k
        if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
3706
972
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
3707
972
            goto err;
3708
972
        }
3709
4.75k
    }
3710
3711
3.78k
    if (ssl_derive(s, skey, ckey, 1) == 0) {
3712
        /* SSLfatal() already called */
3713
34
        goto err;
3714
34
    }
3715
3716
3.75k
    ret = 1;
3717
3.75k
    EVP_PKEY_free(s->s3.tmp.pkey);
3718
3.75k
    s->s3.tmp.pkey = NULL;
3719
4.90k
err:
3720
4.90k
    EVP_PKEY_free(ckey);
3721
3722
4.90k
    return ret;
3723
3.75k
}
3724
3725
static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt)
3726
0
{
3727
0
#ifndef OPENSSL_NO_SRP
3728
0
    unsigned int i;
3729
0
    const unsigned char *data;
3730
3731
0
    if (!PACKET_get_net_2(pkt, &i)
3732
0
        || !PACKET_get_bytes(pkt, &data, i)) {
3733
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);
3734
0
        return 0;
3735
0
    }
3736
0
    if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
3737
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
3738
0
        return 0;
3739
0
    }
3740
0
    if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
3741
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);
3742
0
        return 0;
3743
0
    }
3744
0
    OPENSSL_free(s->session->srp_username);
3745
0
    s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3746
0
    if (s->session->srp_username == NULL) {
3747
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3748
0
        return 0;
3749
0
    }
3750
3751
0
    if (!srp_generate_server_master_secret(s)) {
3752
        /* SSLfatal() already called */
3753
0
        return 0;
3754
0
    }
3755
3756
0
    return 1;
3757
#else
3758
    /* Should never happen */
3759
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3760
    return 0;
3761
#endif
3762
0
}
3763
3764
static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt)
3765
0
{
3766
0
#ifndef OPENSSL_NO_GOST
3767
0
    EVP_PKEY_CTX *pkey_ctx;
3768
0
    EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
3769
0
    unsigned char premaster_secret[32];
3770
0
    const unsigned char *start;
3771
0
    size_t outlen = sizeof(premaster_secret);
3772
0
    size_t inlen;
3773
0
    unsigned long alg_a;
3774
0
    GOST_KX_MESSAGE *pKX = NULL;
3775
0
    const unsigned char *ptr;
3776
0
    int ret = 0;
3777
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3778
3779
    /* Get our certificate private key */
3780
0
    alg_a = s->s3.tmp.new_cipher->algorithm_auth;
3781
0
    if (alg_a & SSL_aGOST12) {
3782
        /*
3783
         * New GOST ciphersuites have SSL_aGOST01 bit too
3784
         */
3785
0
        pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
3786
0
        if (pk == NULL) {
3787
0
            pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3788
0
        }
3789
0
        if (pk == NULL) {
3790
0
            pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3791
0
        }
3792
0
    } else if (alg_a & SSL_aGOST01) {
3793
0
        pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3794
0
    }
3795
3796
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
3797
0
    if (pkey_ctx == NULL) {
3798
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3799
0
        return 0;
3800
0
    }
3801
0
    if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3802
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3803
0
        goto err;
3804
0
    }
3805
    /*
3806
     * If client certificate is present and is of the same type, maybe
3807
     * use it for key exchange.  Don't mind errors from
3808
     * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3809
     * client certificate for authorization only.
3810
     */
3811
0
    client_pub_pkey = tls_get_peer_pkey(s);
3812
0
    if (client_pub_pkey) {
3813
0
        if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3814
0
            ERR_clear_error();
3815
0
    }
3816
3817
0
    ptr = PACKET_data(pkt);
3818
    /* Some implementations provide extra data in the opaqueBlob
3819
     * We have nothing to do with this blob so we just skip it */
3820
0
    pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, (long)PACKET_remaining(pkt));
3821
0
    if (pKX == NULL
3822
0
        || pKX->kxBlob == NULL
3823
0
        || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
3824
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3825
0
        goto err;
3826
0
    }
3827
3828
0
    if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
3829
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
3830
0
        goto err;
3831
0
    }
3832
3833
0
    if (PACKET_remaining(pkt) != 0) {
3834
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
3835
0
        goto err;
3836
0
    }
3837
3838
0
    inlen = ASN1_STRING_get_length(pKX->kxBlob->value.sequence);
3839
0
    if (inlen > INT_MAX)
3840
0
        goto err;
3841
3842
0
    start = ASN1_STRING_get0_data(pKX->kxBlob->value.sequence);
3843
3844
0
    if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
3845
0
            inlen)
3846
0
        <= 0) {
3847
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3848
0
        goto err;
3849
0
    }
3850
    /* Generate master secret */
3851
0
    if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) {
3852
        /* SSLfatal() already called */
3853
0
        goto err;
3854
0
    }
3855
    /* Check if pubkey from client certificate was used */
3856
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
3857
0
            NULL)
3858
0
        > 0)
3859
0
        s->statem.no_cert_verify = 1;
3860
3861
0
    ret = 1;
3862
0
err:
3863
0
    EVP_PKEY_CTX_free(pkey_ctx);
3864
0
    GOST_KX_MESSAGE_free(pKX);
3865
0
    return ret;
3866
#else
3867
    /* Should never happen */
3868
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3869
    return 0;
3870
#endif
3871
0
}
3872
3873
static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt)
3874
0
{
3875
0
#ifndef OPENSSL_NO_GOST
3876
0
    unsigned char rnd_dgst[32];
3877
0
    EVP_PKEY_CTX *pkey_ctx = NULL;
3878
0
    EVP_PKEY *pk = NULL;
3879
0
    unsigned char premaster_secret[32];
3880
0
    const unsigned char *start = NULL;
3881
0
    size_t outlen = sizeof(premaster_secret), inlen = 0;
3882
0
    int ret = 0;
3883
0
    int cipher_nid = ossl_gost18_cke_cipher_nid(s);
3884
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3885
3886
0
    if (cipher_nid == NID_undef) {
3887
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3888
0
        return 0;
3889
0
    }
3890
3891
0
    if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
3892
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3893
0
        goto err;
3894
0
    }
3895
3896
    /* Get our certificate private key */
3897
0
    pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ? s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey : s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3898
0
    if (pk == NULL) {
3899
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
3900
0
        goto err;
3901
0
    }
3902
3903
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
3904
0
    if (pkey_ctx == NULL) {
3905
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3906
0
        goto err;
3907
0
    }
3908
0
    if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3909
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3910
0
        goto err;
3911
0
    }
3912
3913
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
3914
0
            EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst)
3915
0
        <= 0) {
3916
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3917
0
        goto err;
3918
0
    }
3919
3920
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
3921
0
            EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL)
3922
0
        <= 0) {
3923
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3924
0
        goto err;
3925
0
    }
3926
0
    inlen = PACKET_remaining(pkt);
3927
0
    start = PACKET_data(pkt);
3928
3929
0
    if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
3930
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3931
0
        goto err;
3932
0
    }
3933
    /* Generate master secret */
3934
0
    if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) {
3935
        /* SSLfatal() already called */
3936
0
        goto err;
3937
0
    }
3938
0
    ret = 1;
3939
3940
0
err:
3941
0
    EVP_PKEY_CTX_free(pkey_ctx);
3942
0
    return ret;
3943
#else
3944
    /* Should never happen */
3945
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3946
    return 0;
3947
#endif
3948
0
}
3949
3950
MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s,
3951
    PACKET *pkt)
3952
18.6k
{
3953
18.6k
    unsigned long alg_k;
3954
3955
18.6k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3956
3957
    /* For PSK parse and retrieve identity, obtain PSK key */
3958
18.6k
    if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
3959
        /* SSLfatal() already called */
3960
0
        goto err;
3961
0
    }
3962
3963
18.6k
    if (alg_k & SSL_kPSK) {
3964
        /* Identity extracted earlier: should be nothing left */
3965
0
        if (PACKET_remaining(pkt) != 0) {
3966
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3967
0
            goto err;
3968
0
        }
3969
        /* PSK handled by ssl_generate_master_secret */
3970
0
        if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3971
            /* SSLfatal() already called */
3972
0
            goto err;
3973
0
        }
3974
18.6k
    } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3975
10.5k
        if (!tls_process_cke_rsa(s, pkt)) {
3976
            /* SSLfatal() already called */
3977
297
            goto err;
3978
297
        }
3979
10.5k
    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3980
984
        if (!tls_process_cke_dhe(s, pkt)) {
3981
            /* SSLfatal() already called */
3982
81
            goto err;
3983
81
        }
3984
7.11k
    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3985
7.11k
        if (!tls_process_cke_ecdhe(s, pkt)) {
3986
            /* SSLfatal() already called */
3987
1.59k
            goto err;
3988
1.59k
        }
3989
7.11k
    } else if (alg_k & SSL_kSRP) {
3990
0
        if (!tls_process_cke_srp(s, pkt)) {
3991
            /* SSLfatal() already called */
3992
0
            goto err;
3993
0
        }
3994
0
    } else if (alg_k & SSL_kGOST) {
3995
0
        if (!tls_process_cke_gost(s, pkt)) {
3996
            /* SSLfatal() already called */
3997
0
            goto err;
3998
0
        }
3999
0
    } else if (alg_k & SSL_kGOST18) {
4000
0
        if (!tls_process_cke_gost18(s, pkt)) {
4001
            /* SSLfatal() already called */
4002
0
            goto err;
4003
0
        }
4004
0
    } else {
4005
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
4006
0
        goto err;
4007
0
    }
4008
4009
16.6k
    return MSG_PROCESS_CONTINUE_PROCESSING;
4010
1.97k
err:
4011
1.97k
#ifndef OPENSSL_NO_PSK
4012
1.97k
    OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
4013
1.97k
    s->s3.tmp.psk = NULL;
4014
1.97k
    s->s3.tmp.psklen = 0;
4015
1.97k
#endif
4016
1.97k
    return MSG_PROCESS_ERROR;
4017
18.6k
}
4018
4019
WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s,
4020
    WORK_STATE wst)
4021
16.6k
{
4022
#ifndef OPENSSL_NO_SCTP
4023
    if (wst == WORK_MORE_A) {
4024
        /*
4025
         * Before exporting the SCTP auth key we check if DTLSv1.3 has been
4026
         * negotiated which is not supported.
4027
         * Refer to draft-tuexen-tsvwg-rfc6083-bis-04 for more info.
4028
         */
4029
        if (SSL_CONNECTION_IS_DTLS(s) && !SSL_CONNECTION_IS_DTLS13(s)) {
4030
            unsigned char sctpauthkey[64];
4031
            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
4032
            size_t labellen;
4033
            /*
4034
             * Add new shared key for SCTP-Auth, will be ignored if no SCTP
4035
             * used.
4036
             */
4037
            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
4038
                sizeof(DTLS1_SCTP_AUTH_LABEL));
4039
4040
            /* Don't include the terminating zero. */
4041
            labellen = sizeof(labelbuffer) - 1;
4042
            if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
4043
                labellen += 1;
4044
4045
            if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s),
4046
                    sctpauthkey,
4047
                    sizeof(sctpauthkey), labelbuffer,
4048
                    labellen, NULL, 0,
4049
                    0)
4050
                <= 0) {
4051
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4052
                return WORK_ERROR;
4053
            }
4054
4055
            BIO_ctrl(s->wbio, BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
4056
                sizeof(sctpauthkey), sctpauthkey);
4057
        }
4058
    }
4059
#endif
4060
4061
16.6k
    if (s->statem.no_cert_verify || !received_client_cert(s)) {
4062
        /*
4063
         * No certificate verify or no peer certificate so we no longer need
4064
         * the handshake_buffer
4065
         */
4066
16.6k
        if (!ssl3_digest_cached_records(s, 0)) {
4067
            /* SSLfatal() already called */
4068
0
            return WORK_ERROR;
4069
0
        }
4070
16.6k
        return WORK_FINISHED_CONTINUE;
4071
16.6k
    } else {
4072
0
        if (!s->s3.handshake_buffer) {
4073
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4074
0
            return WORK_ERROR;
4075
0
        }
4076
        /*
4077
         * For sigalgs freeze the handshake buffer. If we support
4078
         * extms we've done this already so this is a no-op
4079
         */
4080
0
        if (!ssl3_digest_cached_records(s, 1)) {
4081
            /* SSLfatal() already called */
4082
0
            return WORK_ERROR;
4083
0
        }
4084
0
    }
4085
4086
0
    return WORK_FINISHED_CONTINUE;
4087
16.6k
}
4088
4089
static void tls_clear_pending_client_identity(SSL_CONNECTION *sc)
4090
0
{
4091
0
    OSSL_STACK_OF_X509_free(sc->s3.tmp.pending_peer_chain);
4092
0
    sc->s3.tmp.pending_peer_chain = NULL;
4093
0
    EVP_PKEY_free(sc->s3.tmp.pending_peer_rpk);
4094
0
    sc->s3.tmp.pending_peer_rpk = NULL;
4095
0
}
4096
4097
/*
4098
 * Clear old verification state before verifying fresh credentials, or after
4099
 * accepting an empty initial Certificate response. Empty PHA responses retain
4100
 * the previous state. Do not clear results produced by the new verification.
4101
 */
4102
static void tls_clear_client_verification_state(SSL_CONNECTION *sc, long verify_result)
4103
0
{
4104
0
    OSSL_STACK_OF_X509_free(sc->verified_chain);
4105
0
    sc->verified_chain = NULL;
4106
0
    X509_VERIFY_PARAM_move_peername(sc->param, NULL);
4107
0
    sc->dane.mdpth = -1;
4108
0
    sc->dane.pdpth = -1;
4109
0
    X509_free(sc->dane.mcert);
4110
0
    sc->dane.mcert = NULL;
4111
0
    sc->dane.mtlsa = NULL;
4112
0
    sc->verify_result = verify_result;
4113
0
}
4114
4115
MSG_PROCESS_RETURN tls_process_client_rpk(SSL_CONNECTION *sc, PACKET *pkt)
4116
0
{
4117
0
    EVP_PKEY *peer_rpk = NULL;
4118
4119
0
    if (!tls_process_rpk(sc, pkt, &peer_rpk)) {
4120
        /* SSLfatal already called */
4121
0
        return MSG_PROCESS_ERROR;
4122
0
    }
4123
4124
    /* Stash the parsed RPK; verification runs in the post-process step. */
4125
0
    tls_clear_pending_client_identity(sc);
4126
0
    sc->s3.tmp.pending_peer_rpk = peer_rpk;
4127
0
    if (peer_rpk != NULL)
4128
0
        tls_clear_client_verification_state(sc, X509_V_ERR_UNSPECIFIED);
4129
4130
0
    return MSG_PROCESS_CONTINUE_PROCESSING;
4131
0
}
4132
4133
/* Verify the parked RPK; may pause via SSL_set_retry_verify(). */
4134
static WORK_STATE tls_post_process_client_rpk(SSL_CONNECTION *sc,
4135
    WORK_STATE wst)
4136
0
{
4137
0
    EVP_PKEY *peer_rpk = sc->s3.tmp.pending_peer_rpk;
4138
0
    SSL_SESSION *new_sess = NULL;
4139
4140
0
    (void)wst;
4141
4142
0
    if (SSL_CONNECTION_IS_VERSION13(sc))
4143
0
        sc->statem.no_cert_verify = peer_rpk == NULL;
4144
4145
0
    if (peer_rpk == NULL) {
4146
0
        if ((sc->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
4147
0
            && (sc->verify_mode & SSL_VERIFY_PEER)) {
4148
0
            SSLfatal(sc, SSL_AD_CERTIFICATE_REQUIRED,
4149
0
                SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
4150
0
            goto err;
4151
0
        }
4152
0
    } else {
4153
0
        int v_ok;
4154
4155
0
        if (sc->rwstate == SSL_RETRY_VERIFY)
4156
0
            sc->rwstate = SSL_NOTHING;
4157
0
        v_ok = ssl_verify_rpk(sc, peer_rpk);
4158
0
        if (v_ok > 0 && sc->rwstate == SSL_RETRY_VERIFY) {
4159
            /* The verify callback asked to pause; resume here on retry. */
4160
0
            return WORK_MORE_A;
4161
0
        }
4162
0
        if (v_ok <= 0) {
4163
0
            SSLfatal(sc, ssl_x509err2alert(sc->verify_result),
4164
0
                SSL_R_CERTIFICATE_VERIFY_FAILED);
4165
0
            goto err;
4166
0
        }
4167
0
    }
4168
4169
    /*
4170
     * Sessions must be immutable once they go into the session cache. Otherwise
4171
     * we can get multi-thread problems. Therefore we don't "update" sessions,
4172
     * we replace them with a duplicate. Here, we need to do this every time
4173
     * a PHA response is accepted, even if empty, because refreshed tickets
4174
     * will update the session's key and ID.
4175
     */
4176
0
    if (sc->post_handshake_auth == SSL_PHA_REQUESTED) {
4177
0
        if ((new_sess = ssl_session_dup(sc->session, 0)) == NULL) {
4178
0
            SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
4179
0
            goto err;
4180
0
        }
4181
4182
0
        SSL_SESSION_free(sc->session);
4183
0
        sc->session = new_sess;
4184
0
    }
4185
4186
0
    if (peer_rpk != NULL || sc->post_handshake_auth != SSL_PHA_REQUESTED) {
4187
0
        if (peer_rpk == NULL)
4188
0
            tls_clear_client_verification_state(sc, X509_V_OK);
4189
0
        ossl_session_set0_peer(sc->session, NULL, peer_rpk, sc->verify_result);
4190
0
        sc->s3.tmp.pending_peer_rpk = NULL;
4191
0
    }
4192
4193
    /*
4194
     * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
4195
     * message
4196
     */
4197
0
    if (SSL_CONNECTION_IS_VERSION13(sc)) {
4198
0
        if (!ssl3_digest_cached_records(sc, 1)) {
4199
            /* SSLfatal() already called */
4200
0
            goto err;
4201
0
        }
4202
4203
        /* Save the current hash state for when we receive the CertificateVerify */
4204
0
        if (!ssl_handshake_hash(sc, sc->cert_verify_hash,
4205
0
                sizeof(sc->cert_verify_hash),
4206
0
                &sc->cert_verify_hash_len)) {
4207
            /* SSLfatal() already called */
4208
0
            goto err;
4209
0
        }
4210
4211
        /* resend session tickets */
4212
0
        sc->sent_tickets = 0;
4213
0
    }
4214
4215
0
    return WORK_FINISHED_CONTINUE;
4216
4217
0
err:
4218
0
    tls_clear_pending_client_identity(sc);
4219
0
    return WORK_ERROR;
4220
0
}
4221
4222
MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s,
4223
    PACKET *pkt)
4224
0
{
4225
0
    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
4226
0
    X509 *x = NULL;
4227
0
    unsigned long l;
4228
0
    const unsigned char *certstart, *certbytes;
4229
0
    STACK_OF(X509) *sk = NULL;
4230
0
    PACKET spkt, context;
4231
0
    size_t chainidx;
4232
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
4233
4234
    /*
4235
     * To get this far we must have read encrypted data from the client. We no
4236
     * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3
4237
     */
4238
0
    if (s->rlayer.rrlmethod->set_plain_alerts != NULL)
4239
0
        s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0);
4240
4241
0
    if (s->ext.client_cert_type == TLSEXT_cert_type_rpk)
4242
0
        return tls_process_client_rpk(s, pkt);
4243
4244
0
    if (s->ext.client_cert_type != TLSEXT_cert_type_x509) {
4245
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,
4246
0
            SSL_R_UNKNOWN_CERTIFICATE_TYPE);
4247
0
        goto err;
4248
0
    }
4249
4250
0
    if ((sk = sk_X509_new_null()) == NULL) {
4251
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
4252
0
        goto err;
4253
0
    }
4254
4255
0
    if (SSL_CONNECTION_IS_VERSION13(s)
4256
0
        && (!PACKET_get_length_prefixed_1(pkt, &context)
4257
0
            || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
4258
0
            || (s->pha_context != NULL
4259
0
                && !PACKET_equal(&context, s->pha_context,
4260
0
                    s->pha_context_len)))) {
4261
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
4262
0
        goto err;
4263
0
    }
4264
4265
0
    if (!PACKET_get_length_prefixed_3(pkt, &spkt)
4266
0
        || PACKET_remaining(pkt) != 0) {
4267
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4268
0
        goto err;
4269
0
    }
4270
4271
0
    for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
4272
0
        if (!PACKET_get_net_3(&spkt, &l)
4273
0
            || !PACKET_get_bytes(&spkt, &certbytes, l)) {
4274
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
4275
0
            goto err;
4276
0
        }
4277
4278
0
        certstart = certbytes;
4279
0
        x = X509_new_ex(sctx->libctx, sctx->propq);
4280
0
        if (x == NULL) {
4281
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_X509_LIB);
4282
0
            goto err;
4283
0
        }
4284
0
        if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) {
4285
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
4286
0
            goto err;
4287
0
        }
4288
4289
0
        if (certbytes != (certstart + l)) {
4290
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
4291
0
            goto err;
4292
0
        }
4293
4294
0
        if (SSL_CONNECTION_IS_VERSION13(s)) {
4295
0
            RAW_EXTENSION *rawexts = NULL;
4296
0
            PACKET extensions;
4297
4298
0
            if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
4299
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
4300
0
                goto err;
4301
0
            }
4302
0
            if (!tls_collect_extensions(s, &extensions,
4303
0
                    SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
4304
0
                    NULL, chainidx == 0)
4305
0
                || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
4306
0
                    rawexts, x, chainidx,
4307
0
                    PACKET_remaining(&spkt) == 0)) {
4308
0
                OPENSSL_free(rawexts);
4309
0
                goto err;
4310
0
            }
4311
0
            OPENSSL_free(rawexts);
4312
0
        }
4313
4314
0
        if (!sk_X509_push(sk, x)) {
4315
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
4316
0
            goto err;
4317
0
        }
4318
0
        x = NULL;
4319
0
    }
4320
4321
    /*
4322
     * Stash the parsed chain so that tls_post_process_client_certificate() can
4323
     * run verification and may pause the handshake via SSL_set_retry_verify().
4324
     */
4325
0
    tls_clear_pending_client_identity(s);
4326
0
    s->s3.tmp.pending_peer_chain = sk;
4327
0
    if (sk_X509_num(sk) > 0)
4328
0
        tls_clear_client_verification_state(s, X509_V_ERR_UNSPECIFIED);
4329
4330
0
    return MSG_PROCESS_CONTINUE_PROCESSING;
4331
4332
0
err:
4333
0
    X509_free(x);
4334
0
    OSSL_STACK_OF_X509_free(sk);
4335
0
    return ret;
4336
0
}
4337
4338
/*
4339
 * Verify the parked chain from tls_process_client_certificate() and finalize
4340
 * the peer cert / TLS 1.3 handshake-hash bookkeeping.
4341
 * Allows the verify callback to defer via SSL_set_retry_verify(), in which
4342
 * case we return WORK_MORE_A and the state machine re-enters this function
4343
 * once the application has supplied a verdict.
4344
 */
4345
WORK_STATE tls_post_process_client_certificate(SSL_CONNECTION *s,
4346
    WORK_STATE wst)
4347
0
{
4348
0
    STACK_OF(X509) *sk;
4349
0
    SSL_SESSION *new_sess = NULL;
4350
0
    EVP_PKEY *pkey;
4351
0
    int has_cert, i;
4352
4353
0
    if (s->ext.client_cert_type == TLSEXT_cert_type_rpk)
4354
0
        return tls_post_process_client_rpk(s, wst);
4355
4356
0
    sk = s->s3.tmp.pending_peer_chain;
4357
0
    (void)wst;
4358
4359
0
    has_cert = sk != NULL && sk_X509_num(sk) > 0;
4360
0
    if (SSL_CONNECTION_IS_VERSION13(s))
4361
0
        s->statem.no_cert_verify = !has_cert;
4362
0
    if (!has_cert) {
4363
        /* Fail only if we required a certificate */
4364
0
        if ((s->verify_mode & SSL_VERIFY_PEER)
4365
0
            && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
4366
0
            SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
4367
0
                SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
4368
0
            goto err;
4369
0
        }
4370
4371
        /* No client certificate so digest cached records */
4372
0
        if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
4373
            /* SSLfatal() already called */
4374
0
            goto err;
4375
0
        }
4376
0
    } else {
4377
0
        if (s->rwstate == SSL_RETRY_VERIFY)
4378
0
            s->rwstate = SSL_NOTHING;
4379
0
        i = ssl_verify_cert_chain(s, sk);
4380
0
        if (i > 0 && s->rwstate == SSL_RETRY_VERIFY) {
4381
            /*
4382
             * The application's verify callback asked us to pause via
4383
             * SSL_set_retry_verify(); SSL_do_handshake() will return
4384
             * SSL_ERROR_WANT_RETRY_VERIFY and the state machine resumes here
4385
             * once the callback is invoked again.
4386
             */
4387
0
            return WORK_MORE_A;
4388
0
        }
4389
0
        if (i <= 0) {
4390
0
            SSLfatal(s, ssl_x509err2alert(s->verify_result),
4391
0
                SSL_R_CERTIFICATE_VERIFY_FAILED);
4392
0
            goto err;
4393
0
        }
4394
0
        pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
4395
0
        if (pkey == NULL) {
4396
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
4397
0
                SSL_R_UNKNOWN_CERTIFICATE_TYPE);
4398
0
            goto err;
4399
0
        }
4400
0
    }
4401
4402
    /*
4403
     * Sessions must be immutable once they go into the session cache. Otherwise
4404
     * we can get multi-thread problems. Therefore we don't "update" sessions,
4405
     * we replace them with a duplicate. Here, we need to do this every time
4406
     * a PHA response is accepted, even if empty, because refreshed tickets
4407
     * will update the session's key and ID.
4408
     */
4409
0
    if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
4410
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
4411
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
4412
0
            goto err;
4413
0
        }
4414
4415
0
        SSL_SESSION_free(s->session);
4416
0
        s->session = new_sess;
4417
0
    }
4418
4419
0
    if (!has_cert) {
4420
0
        tls_clear_pending_client_identity(s);
4421
0
        sk = NULL;
4422
0
        if (s->post_handshake_auth != SSL_PHA_REQUESTED)
4423
0
            tls_clear_client_verification_state(s, X509_V_OK);
4424
0
    }
4425
0
    if (has_cert || s->post_handshake_auth != SSL_PHA_REQUESTED) {
4426
0
        ossl_session_set0_peer(s->session, sk, NULL, s->verify_result);
4427
0
        s->s3.tmp.pending_peer_chain = NULL;
4428
0
    }
4429
4430
    /*
4431
     * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
4432
     * message
4433
     */
4434
0
    if (SSL_CONNECTION_IS_VERSION13(s) && !ssl3_digest_cached_records(s, 1)) {
4435
        /* SSLfatal() already called */
4436
0
        goto err;
4437
0
    }
4438
4439
    /* Save the current hash state for when we receive the CertificateVerify */
4440
0
    if (SSL_CONNECTION_IS_VERSION13(s)) {
4441
0
        if (!ssl_handshake_hash(s, s->cert_verify_hash,
4442
0
                sizeof(s->cert_verify_hash),
4443
0
                &s->cert_verify_hash_len)) {
4444
            /* SSLfatal() already called */
4445
0
            goto err;
4446
0
        }
4447
4448
        /* Resend session tickets */
4449
0
        s->sent_tickets = 0;
4450
0
    }
4451
4452
0
    return WORK_FINISHED_CONTINUE;
4453
4454
0
err:
4455
0
    tls_clear_pending_client_identity(s);
4456
0
    return WORK_ERROR;
4457
0
}
4458
4459
#ifndef OPENSSL_NO_COMP_ALG
4460
MSG_PROCESS_RETURN tls_process_client_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)
4461
{
4462
    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
4463
    PACKET tmppkt;
4464
    BUF_MEM *buf = BUF_MEM_new();
4465
4466
    if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)
4467
        ret = tls_process_client_certificate(sc, &tmppkt);
4468
4469
    BUF_MEM_free(buf);
4470
    return ret;
4471
}
4472
#endif
4473
4474
CON_FUNC_RETURN tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt)
4475
9.70k
{
4476
9.70k
    CERT_PKEY *cpk = s->s3.tmp.cert;
4477
4478
9.70k
    if (cpk == NULL) {
4479
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4480
0
        return CON_FUNC_ERROR;
4481
0
    }
4482
4483
    /*
4484
     * In TLSv1.3 the certificate chain is always preceded by a 0 length context
4485
     * for the server Certificate message
4486
     */
4487
9.70k
    if (SSL_CONNECTION_IS_VERSION13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
4488
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4489
0
        return CON_FUNC_ERROR;
4490
0
    }
4491
9.70k
    switch (s->ext.server_cert_type) {
4492
0
    case TLSEXT_cert_type_rpk:
4493
0
        if (!tls_output_rpk(s, pkt, cpk)) {
4494
            /* SSLfatal() already called */
4495
0
            return 0;
4496
0
        }
4497
0
        break;
4498
9.70k
    case TLSEXT_cert_type_x509:
4499
9.70k
        if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) {
4500
            /* SSLfatal() already called */
4501
0
            return 0;
4502
0
        }
4503
9.70k
        break;
4504
9.70k
    default:
4505
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4506
0
        return 0;
4507
9.70k
    }
4508
4509
9.70k
    return CON_FUNC_SUCCESS;
4510
9.70k
}
4511
4512
#ifndef OPENSSL_NO_COMP_ALG
4513
CON_FUNC_RETURN tls_construct_server_compressed_certificate(SSL_CONNECTION *sc, WPACKET *pkt)
4514
{
4515
    int alg = get_compressed_certificate_alg(sc);
4516
    OSSL_COMP_CERT *cc = sc->s3.tmp.cert->comp_cert[alg];
4517
4518
    if (!ossl_assert(cc != NULL)) {
4519
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4520
        return 0;
4521
    }
4522
    /*
4523
     * Server can't compress on-demand
4524
     * Use pre-compressed certificate
4525
     */
4526
    if (!WPACKET_put_bytes_u16(pkt, alg)
4527
        || !WPACKET_put_bytes_u24(pkt, cc->orig_len)
4528
        || !WPACKET_start_sub_packet_u24(pkt)
4529
        || !WPACKET_memcpy(pkt, cc->data, cc->len)
4530
        || !WPACKET_close(pkt)) {
4531
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4532
        return 0;
4533
    }
4534
4535
    sc->s3.tmp.cert->cert_comp_used++;
4536
    return 1;
4537
}
4538
#endif
4539
4540
static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt,
4541
    uint32_t age_add, unsigned char *tick_nonce)
4542
218
{
4543
218
    uint32_t timeout = (uint32_t)ossl_time2seconds(s->session->timeout);
4544
4545
    /*
4546
     * Ticket lifetime hint:
4547
     * In TLSv1.3 we reset the "time" field above, and always specify the
4548
     * timeout, limited to a 1 week period per RFC9846.
4549
     * For TLSv1.2 this is advisory only and we leave this unspecified for
4550
     * resumed session (for simplicity).
4551
     */
4552
218
#define ONE_WEEK_SEC (7 * 24 * 60 * 60)
4553
4554
218
    if (SSL_CONNECTION_IS_VERSION13(s)) {
4555
0
        if (ossl_time_compare(s->session->timeout,
4556
0
                ossl_seconds2time(ONE_WEEK_SEC))
4557
0
            > 0)
4558
0
            timeout = ONE_WEEK_SEC;
4559
218
    } else if (s->hit)
4560
0
        timeout = 0;
4561
4562
218
    if (!WPACKET_put_bytes_u32(pkt, timeout)) {
4563
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4564
0
        return 0;
4565
0
    }
4566
4567
218
    if (SSL_CONNECTION_IS_VERSION13(s)) {
4568
0
        if (!WPACKET_put_bytes_u32(pkt, age_add)
4569
0
            || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
4570
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4571
0
            return 0;
4572
0
        }
4573
0
    }
4574
4575
    /* Start the sub-packet for the actual ticket data */
4576
218
    if (!WPACKET_start_sub_packet_u16(pkt)) {
4577
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4578
0
        return 0;
4579
0
    }
4580
4581
218
    return 1;
4582
218
}
4583
4584
static CON_FUNC_RETURN construct_stateless_ticket(SSL_CONNECTION *s,
4585
    WPACKET *pkt,
4586
    uint32_t age_add,
4587
    unsigned char *tick_nonce)
4588
218
{
4589
218
    unsigned char *senc = NULL;
4590
218
    EVP_CIPHER_CTX *ctx = NULL;
4591
218
    SSL_HMAC hctx, *constructed_hctx = NULL;
4592
218
    unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
4593
218
    const unsigned char *const_p;
4594
218
    int len, slen_full, slen, lenfinal;
4595
218
    SSL_SESSION *sess;
4596
218
    size_t hlen;
4597
218
    SSL_CTX *tctx = s->session_ctx;
4598
218
    unsigned char iv[EVP_MAX_IV_LENGTH];
4599
218
    unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
4600
218
    int iv_len;
4601
218
    CON_FUNC_RETURN ok = CON_FUNC_ERROR;
4602
218
    size_t macoffset, macendoffset;
4603
218
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
4604
218
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
4605
4606
    /* get session encoding length */
4607
218
    slen_full = i2d_SSL_SESSION(s->session, NULL);
4608
    /*
4609
     * Some length values are 16 bits, so forget it if session is too
4610
     * long
4611
     */
4612
218
    if (slen_full == 0 || slen_full > 0xFF00) {
4613
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4614
0
        goto err;
4615
0
    }
4616
218
    senc = OPENSSL_malloc(slen_full);
4617
218
    if (senc == NULL) {
4618
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
4619
0
        goto err;
4620
0
    }
4621
4622
218
    ctx = EVP_CIPHER_CTX_new();
4623
218
    if (ctx == NULL) {
4624
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
4625
0
        goto err;
4626
0
    }
4627
218
    if ((constructed_hctx = ssl_hmac_construct(tctx, &hctx)) == NULL) {
4628
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
4629
0
        goto err;
4630
0
    }
4631
4632
218
    p = senc;
4633
218
    if (!i2d_SSL_SESSION(s->session, &p)) {
4634
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4635
0
        goto err;
4636
0
    }
4637
4638
    /*
4639
     * create a fresh copy (not shared with other threads) to clean up
4640
     */
4641
218
    const_p = senc;
4642
218
    sess = d2i_SSL_SESSION_ex(NULL, &const_p, slen_full, sctx->libctx,
4643
218
        sctx->propq);
4644
218
    if (sess == NULL) {
4645
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4646
0
        goto err;
4647
0
    }
4648
4649
218
    slen = i2d_SSL_SESSION(sess, NULL);
4650
218
    if (slen == 0 || slen > slen_full) {
4651
        /* shouldn't ever happen */
4652
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4653
0
        SSL_SESSION_free(sess);
4654
0
        goto err;
4655
0
    }
4656
218
    p = senc;
4657
218
    if (!i2d_SSL_SESSION(sess, &p)) {
4658
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4659
0
        SSL_SESSION_free(sess);
4660
0
        goto err;
4661
0
    }
4662
218
    SSL_SESSION_free(sess);
4663
4664
    /*
4665
     * Initialize HMAC and cipher contexts. If callback present it does
4666
     * all the work otherwise use generated values from parent ctx.
4667
     */
4668
218
#ifndef OPENSSL_NO_DEPRECATED_3_0
4669
218
    if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
4670
#else
4671
    if (tctx->ext.ticket_key_evp_cb != NULL)
4672
#endif
4673
0
    {
4674
0
        int ret = 0;
4675
4676
0
        if (tctx->ext.ticket_key_evp_cb != NULL)
4677
0
            ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx,
4678
0
                ssl_hmac_get0_EVP_MAC_CTX(&hctx),
4679
0
                1);
4680
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
4681
0
        else if (tctx->ext.ticket_key_cb != NULL)
4682
            /* if 0 is returned, write an empty ticket */
4683
0
            ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx,
4684
0
                ssl_hmac_get0_HMAC_CTX(&hctx), 1);
4685
0
#endif
4686
4687
0
        if (ret == 0) {
4688
            /*
4689
             * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0
4690
             * length ticket is not allowed so we abort construction of the
4691
             * ticket
4692
             */
4693
0
            if (SSL_CONNECTION_IS_VERSION13(s)) {
4694
0
                ok = CON_FUNC_DONT_SEND;
4695
0
                goto err;
4696
0
            }
4697
            /* Put timeout and length */
4698
0
            if (!WPACKET_put_bytes_u32(pkt, 0)
4699
0
                || !WPACKET_put_bytes_u16(pkt, 0)) {
4700
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4701
0
                goto err;
4702
0
            }
4703
0
            OPENSSL_free(senc);
4704
0
            EVP_CIPHER_CTX_free(ctx);
4705
0
            ssl_hmac_destruct(constructed_hctx);
4706
0
            return CON_FUNC_SUCCESS;
4707
0
        }
4708
0
        if (ret < 0) {
4709
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
4710
0
            goto err;
4711
0
        }
4712
0
        iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
4713
0
        if (iv_len < 0) {
4714
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4715
0
            goto err;
4716
0
        }
4717
218
    } else {
4718
218
        iv_len = EVP_CIPHER_get_iv_length(sctx->tktenc);
4719
218
        if (iv_len < 0
4720
218
            || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0
4721
218
            || !EVP_EncryptInit_ex(ctx, sctx->tktenc, NULL,
4722
218
                tctx->ext.secure->tick_aes_key, iv)
4723
218
            || !ssl_hmac_init(&hctx, tctx->ext.secure->tick_hmac_key,
4724
218
                sizeof(tctx->ext.secure->tick_hmac_key),
4725
218
                "SHA256")) {
4726
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4727
0
            goto err;
4728
0
        }
4729
218
        memcpy(key_name, tctx->ext.tick_key_name,
4730
218
            sizeof(tctx->ext.tick_key_name));
4731
218
    }
4732
4733
218
    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
4734
        /* SSLfatal() already called */
4735
0
        goto err;
4736
0
    }
4737
4738
218
    if (!WPACKET_get_total_written(pkt, &macoffset)
4739
        /* Output key name */
4740
218
        || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
4741
        /* output IV */
4742
218
        || !WPACKET_memcpy(pkt, iv, iv_len)
4743
218
        || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
4744
218
            &encdata1)
4745
        /* Encrypt session data */
4746
218
        || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
4747
218
        || !WPACKET_allocate_bytes(pkt, len, &encdata2)
4748
218
        || encdata1 != encdata2
4749
218
        || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
4750
218
        || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
4751
218
        || encdata1 + len != encdata2
4752
218
        || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
4753
218
        || !WPACKET_get_total_written(pkt, &macendoffset)
4754
218
        || !ssl_hmac_update(&hctx,
4755
218
            (unsigned char *)s->init_buf->data + macoffset,
4756
218
            macendoffset - macoffset)
4757
218
        || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
4758
218
        || !ssl_hmac_final(&hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
4759
218
        || hlen > EVP_MAX_MD_SIZE
4760
218
        || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
4761
218
        || macdata1 != macdata2) {
4762
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4763
0
        goto err;
4764
0
    }
4765
4766
    /* Close the sub-packet created by create_ticket_prequel() */
4767
218
    if (!WPACKET_close(pkt)) {
4768
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4769
0
        goto err;
4770
0
    }
4771
4772
218
    ok = CON_FUNC_SUCCESS;
4773
218
err:
4774
218
    OPENSSL_free(senc);
4775
218
    EVP_CIPHER_CTX_free(ctx);
4776
218
    ssl_hmac_destruct(constructed_hctx);
4777
218
    return ok;
4778
218
}
4779
4780
static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt,
4781
    uint32_t age_add,
4782
    unsigned char *tick_nonce)
4783
0
{
4784
0
    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
4785
        /* SSLfatal() already called */
4786
0
        return 0;
4787
0
    }
4788
4789
0
    if (!WPACKET_memcpy(pkt, s->session->session_id,
4790
0
            s->session->session_id_length)
4791
0
        || !WPACKET_close(pkt)) {
4792
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4793
0
        return 0;
4794
0
    }
4795
4796
0
    return 1;
4797
0
}
4798
4799
static void tls_update_ticket_counts(SSL_CONNECTION *s)
4800
0
{
4801
    /*
4802
     * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4803
     * gets reset to 0 if we send more tickets following a post-handshake
4804
     * auth, but |next_ticket_nonce| does not.  If we're sending extra
4805
     * tickets, decrement the count of pending extra tickets.
4806
     */
4807
0
    s->sent_tickets++;
4808
0
    s->next_ticket_nonce++;
4809
0
    if (s->ext.extra_tickets_expected > 0)
4810
0
        s->ext.extra_tickets_expected--;
4811
0
}
4812
4813
CON_FUNC_RETURN tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt)
4814
218
{
4815
218
    SSL_CTX *tctx = s->session_ctx;
4816
218
    unsigned char tick_nonce[TICKET_NONCE_SIZE];
4817
218
    union {
4818
218
        unsigned char age_add_c[sizeof(uint32_t)];
4819
218
        uint32_t age_add;
4820
218
    } age_add_u;
4821
218
    CON_FUNC_RETURN ret = CON_FUNC_ERROR;
4822
4823
218
    age_add_u.age_add = 0;
4824
4825
218
    if (SSL_CONNECTION_IS_VERSION13(s)) {
4826
0
        size_t i, hashlen;
4827
0
        uint64_t nonce;
4828
        /* ASCII: "resumption", in hex for EBCDIC compatibility */
4829
0
        static const unsigned char nonce_label[] = { 0x72, 0x65, 0x73, 0x75, 0x6D,
4830
0
            0x70, 0x74, 0x69, 0x6F, 0x6E };
4831
0
        const EVP_MD *md = ssl_handshake_md(s);
4832
0
        int hashleni = EVP_MD_get_size(md);
4833
4834
        /* Ensure cast to size_t is safe */
4835
0
        if (!ossl_assert(hashleni > 0)) {
4836
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4837
0
            goto err;
4838
0
        }
4839
0
        hashlen = (size_t)hashleni;
4840
4841
        /*
4842
         * If we already sent one NewSessionTicket, or we resumed then
4843
         * s->session may already be in a cache and so we must not modify it.
4844
         * Instead we need to take a copy of it and modify that.
4845
         */
4846
0
        if (s->sent_tickets != 0 || s->hit) {
4847
0
            SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
4848
4849
0
            if (new_sess == NULL) {
4850
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
4851
0
                goto err;
4852
0
            }
4853
4854
0
            SSL_SESSION_free(s->session);
4855
0
            s->session = new_sess;
4856
0
        }
4857
4858
0
        if (!ssl_generate_session_id(s, s->session)) {
4859
            /* SSLfatal() already called */
4860
0
            goto err;
4861
0
        }
4862
0
        if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
4863
0
                age_add_u.age_add_c, sizeof(age_add_u), 0)
4864
0
            <= 0) {
4865
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4866
0
            goto err;
4867
0
        }
4868
0
        s->session->ext.tick_age_add = age_add_u.age_add;
4869
4870
0
        nonce = s->next_ticket_nonce;
4871
0
        for (i = TICKET_NONCE_SIZE; i > 0; i--) {
4872
0
            tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
4873
0
            nonce >>= 8;
4874
0
        }
4875
4876
0
        if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
4877
0
                nonce_label,
4878
0
                sizeof(nonce_label),
4879
0
                tick_nonce,
4880
0
                TICKET_NONCE_SIZE,
4881
0
                s->session->master_key,
4882
0
                hashlen, 1)) {
4883
            /* SSLfatal() already called */
4884
0
            goto err;
4885
0
        }
4886
0
        s->session->master_key_length = hashlen;
4887
4888
0
        s->session->time = ossl_time_now();
4889
0
        ssl_session_calculate_timeout(s->session);
4890
0
        if (s->s3.alpn_selected != NULL) {
4891
0
            OPENSSL_free(s->session->ext.alpn_selected);
4892
0
            s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
4893
0
            if (s->session->ext.alpn_selected == NULL) {
4894
0
                s->session->ext.alpn_selected_len = 0;
4895
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
4896
0
                goto err;
4897
0
            }
4898
0
            s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
4899
0
        } else {
4900
            /*
4901
             * No ALPN was negotiated on this handshake. If we resumed a
4902
             * session that had previously negotiated ALPN, the stale value
4903
             * must be cleared from the (copied) session before it is stored
4904
             * in the new ticket. Otherwise a subsequent 0-RTT attempt using
4905
             * that ticket would incorrectly assume an ALPN protocol had been
4906
             * negotiated. See tls_handle_alpn().
4907
             */
4908
0
            OPENSSL_free(s->session->ext.alpn_selected);
4909
0
            s->session->ext.alpn_selected = NULL;
4910
0
            s->session->ext.alpn_selected_len = 0;
4911
0
        }
4912
0
        s->session->ext.max_early_data = s->max_early_data;
4913
0
    }
4914
4915
218
    if (tctx->generate_ticket_cb != NULL && tctx->generate_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), tctx->ticket_cb_data) == 0) {
4916
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4917
0
        goto err;
4918
0
    }
4919
    /*
4920
     * If we are using anti-replay protection then we behave as if
4921
     * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4922
     * is no point in using full stateless tickets.
4923
     */
4924
218
    if (SSL_CONNECTION_IS_VERSION13(s)
4925
0
        && ((s->options & SSL_OP_NO_TICKET) != 0
4926
0
            || (s->max_early_data > 0
4927
0
                && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
4928
0
        if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
4929
            /* SSLfatal() already called */
4930
0
            goto err;
4931
0
        }
4932
218
    } else {
4933
218
        CON_FUNC_RETURN tmpret;
4934
4935
218
        tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add,
4936
218
            tick_nonce);
4937
218
        if (tmpret != CON_FUNC_SUCCESS) {
4938
0
            if (tmpret == CON_FUNC_DONT_SEND) {
4939
                /* Non-fatal. Abort construction but continue */
4940
0
                ret = CON_FUNC_DONT_SEND;
4941
                /* We count this as a success so update the counts anwyay */
4942
0
                tls_update_ticket_counts(s);
4943
0
            }
4944
            /* else SSLfatal() already called */
4945
0
            goto err;
4946
0
        }
4947
218
    }
4948
4949
218
    if (SSL_CONNECTION_IS_VERSION13(s)) {
4950
0
        if (!tls_construct_extensions(s, pkt,
4951
0
                SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
4952
0
                NULL, 0)) {
4953
            /* SSLfatal() already called */
4954
0
            goto err;
4955
0
        }
4956
0
        tls_update_ticket_counts(s);
4957
0
        ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
4958
0
    }
4959
4960
218
    ret = CON_FUNC_SUCCESS;
4961
218
err:
4962
218
    return ret;
4963
218
}
4964
4965
/*
4966
 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4967
 * create a separate message. Returns 1 on success or 0 on failure.
4968
 */
4969
int tls_construct_cert_status_body(SSL_CONNECTION *s, OCSP_RESPONSE *resp, WPACKET *pkt)
4970
0
{
4971
0
    unsigned char *respder = NULL;
4972
0
    int resplen = 0;
4973
4974
0
    if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)) {
4975
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4976
0
        return 0;
4977
0
    }
4978
4979
0
#ifndef OPENSSL_NO_OCSP
4980
0
    resplen = i2d_OCSP_RESPONSE(resp, &respder);
4981
0
#endif
4982
4983
0
    if (!WPACKET_sub_memcpy_u24(pkt, respder, resplen)) {
4984
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4985
0
        OPENSSL_free(respder);
4986
0
        return 0;
4987
0
    }
4988
4989
0
    OPENSSL_free(respder);
4990
0
    return 1;
4991
0
}
4992
4993
CON_FUNC_RETURN tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt)
4994
0
{
4995
0
    OCSP_RESPONSE *resp;
4996
4997
0
    resp = ossl_get_ocsp_response(s, 0);
4998
4999
0
    if (resp == NULL)
5000
0
        return CON_FUNC_DONT_SEND;
5001
5002
0
    if (!tls_construct_cert_status_body(s, resp, pkt)) {
5003
        /* SSLfatal() already called */
5004
0
        return CON_FUNC_ERROR;
5005
0
    }
5006
5007
0
    return CON_FUNC_SUCCESS;
5008
0
}
5009
5010
#ifndef OPENSSL_NO_NEXTPROTONEG
5011
/*
5012
 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
5013
 * It sets the next_proto member in s if found
5014
 */
5015
MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt)
5016
0
{
5017
0
    PACKET next_proto, padding;
5018
0
    size_t next_proto_len;
5019
5020
    /*-
5021
     * The payload looks like:
5022
     *   uint8 proto_len;
5023
     *   uint8 proto[proto_len];
5024
     *   uint8 padding_len;
5025
     *   uint8 padding[padding_len];
5026
     */
5027
0
    if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
5028
0
        || !PACKET_get_length_prefixed_1(pkt, &padding)
5029
0
        || PACKET_remaining(pkt) > 0) {
5030
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
5031
0
        return MSG_PROCESS_ERROR;
5032
0
    }
5033
5034
0
    if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
5035
0
        s->ext.npn_len = 0;
5036
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5037
0
        return MSG_PROCESS_ERROR;
5038
0
    }
5039
5040
0
    s->ext.npn_len = (unsigned char)next_proto_len;
5041
5042
0
    return MSG_PROCESS_CONTINUE_READING;
5043
0
}
5044
#endif
5045
5046
static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s,
5047
    WPACKET *pkt)
5048
2.84k
{
5049
2.84k
    if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
5050
2.84k
            NULL, 0)) {
5051
        /* SSLfatal() already called */
5052
0
        return CON_FUNC_ERROR;
5053
0
    }
5054
5055
2.84k
    return CON_FUNC_SUCCESS;
5056
2.84k
}
5057
5058
MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt)
5059
0
{
5060
0
    if (PACKET_remaining(pkt) != 0) {
5061
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
5062
0
        return MSG_PROCESS_ERROR;
5063
0
    }
5064
5065
0
    if (s->early_data_state != SSL_EARLY_DATA_READING
5066
0
        && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
5067
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5068
0
        return MSG_PROCESS_ERROR;
5069
0
    }
5070
5071
    /*
5072
     * EndOfEarlyData signals a key change so the end of the message must be on
5073
     * a record boundary.
5074
     */
5075
0
    if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
5076
0
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
5077
0
        return MSG_PROCESS_ERROR;
5078
0
    }
5079
5080
0
    s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
5081
0
    if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s,
5082
0
            SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
5083
        /* SSLfatal() already called */
5084
0
        return MSG_PROCESS_ERROR;
5085
0
    }
5086
5087
0
    return MSG_PROCESS_CONTINUE_READING;
5088
0
}