Coverage Report

Created: 2026-04-01 06:39

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