Coverage Report

Created: 2025-12-04 06:33

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