Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/ssl/statem/statem_clnt.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include <stdio.h>
13
#include <time.h>
14
#include <assert.h>
15
#include "../ssl_local.h"
16
#include "statem_local.h"
17
#include <openssl/buffer.h>
18
#include <openssl/rand.h>
19
#include <openssl/objects.h>
20
#include <openssl/evp.h>
21
#include <openssl/md5.h>
22
#include <openssl/dh.h>
23
#include <openssl/rsa.h>
24
#include <openssl/bn.h>
25
#include <openssl/engine.h>
26
#include <openssl/trace.h>
27
#include <openssl/core_names.h>
28
#include <openssl/param_build.h>
29
#include "internal/cryptlib.h"
30
#include "internal/comp.h"
31
#include "internal/ssl_unwrap.h"
32
#include <openssl/ocsp.h>
33
34
static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
35
    PACKET *pkt);
36
static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
37
    PACKET *pkt);
38
39
static ossl_inline int cert_req_allowed(SSL_CONNECTION *s);
40
static int key_exchange_expected(SSL_CONNECTION *s);
41
static int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
42
    WPACKET *pkt);
43
44
static ossl_inline int received_server_cert(SSL_CONNECTION *sc)
45
10.3k
{
46
10.3k
    return sc->session->peer_rpk != NULL || sc->session->peer != NULL;
47
10.3k
}
48
49
/*
50
 * Is a CertificateRequest message allowed at the moment or not?
51
 *
52
 *  Return values are:
53
 *  1: Yes
54
 *  0: No
55
 */
56
static ossl_inline int cert_req_allowed(SSL_CONNECTION *s)
57
760
{
58
    /* TLS does not like anon-DH with client cert */
59
760
    if ((s->version > SSL3_VERSION
60
712
            && (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL))
61
745
        || (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
62
15
        return 0;
63
64
745
    return 1;
65
760
}
66
67
/*
68
 * Should we expect the ServerKeyExchange message or not?
69
 *
70
 *  Return values are:
71
 *  1: Yes
72
 *  0: No
73
 */
74
static int key_exchange_expected(SSL_CONNECTION *s)
75
30.4k
{
76
30.4k
    long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
77
78
    /*
79
     * Can't skip server key exchange if this is an ephemeral
80
     * ciphersuite or for SRP
81
     */
82
30.4k
    if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK | SSL_kSRP)) {
83
18.9k
        return 1;
84
18.9k
    }
85
86
11.5k
    return 0;
87
30.4k
}
88
89
/*
90
 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
91
 * handshake state transitions when a TLS1.3 client is reading messages from the
92
 * server. The message type that the server has sent is provided in |mt|. The
93
 * current state is in |s->statem.hand_state|.
94
 *
95
 * Return values are 1 for success (transition allowed) and  0 on error
96
 * (transition not allowed)
97
 */
98
static int ossl_statem_client13_read_transition(SSL_CONNECTION *s, int mt)
99
81.4k
{
100
81.4k
    OSSL_STATEM *st = &s->statem;
101
102
    /*
103
     * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
104
     * yet negotiated TLSv1.3 at that point so that is handled by
105
     * ossl_statem_client_read_transition()
106
     */
107
108
81.4k
    switch (st->hand_state) {
109
0
    default:
110
0
        break;
111
112
0
    case TLS_ST_CW_CLNT_HELLO:
113
        /*
114
         * This must a ClientHello following a HelloRetryRequest, so the only
115
         * thing we can get now is a ServerHello.
116
         */
117
0
        if (mt == SSL3_MT_SERVER_HELLO) {
118
0
            st->hand_state = TLS_ST_CR_SRVR_HELLO;
119
0
            return 1;
120
0
        }
121
0
        break;
122
123
21.6k
    case TLS_ST_CR_SRVR_HELLO:
124
21.6k
        if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
125
21.5k
            st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
126
21.5k
            return 1;
127
21.5k
        }
128
135
        break;
129
130
20.1k
    case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
131
20.1k
        if (s->hit) {
132
0
            if (mt == SSL3_MT_FINISHED) {
133
0
                st->hand_state = TLS_ST_CR_FINISHED;
134
0
                return 1;
135
0
            }
136
20.1k
        } else {
137
20.1k
            if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
138
164
                st->hand_state = TLS_ST_CR_CERT_REQ;
139
164
                return 1;
140
164
            }
141
20.0k
            if (mt == SSL3_MT_CERTIFICATE) {
142
19.9k
                st->hand_state = TLS_ST_CR_CERT;
143
19.9k
                return 1;
144
19.9k
            }
145
#ifndef OPENSSL_NO_COMP_ALG
146
            if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
147
                && s->ext.compress_certificate_sent) {
148
                st->hand_state = TLS_ST_CR_COMP_CERT;
149
                return 1;
150
            }
151
#endif
152
20.0k
        }
153
21
        break;
154
155
21
    case TLS_ST_CR_CERT_REQ:
156
0
        if (mt == SSL3_MT_CERTIFICATE) {
157
0
            st->hand_state = TLS_ST_CR_CERT;
158
0
            return 1;
159
0
        }
160
#ifndef OPENSSL_NO_COMP_ALG
161
        if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
162
            && s->ext.compress_certificate_sent) {
163
            st->hand_state = TLS_ST_CR_COMP_CERT;
164
            return 1;
165
        }
166
#endif
167
0
        break;
168
169
16.9k
    case TLS_ST_CR_CERT:
170
16.9k
    case TLS_ST_CR_COMP_CERT:
171
16.9k
        if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
172
16.6k
            st->hand_state = TLS_ST_CR_CERT_VRFY;
173
16.6k
            return 1;
174
16.6k
        }
175
389
        break;
176
177
15.9k
    case TLS_ST_CR_CERT_VRFY:
178
15.9k
        if (mt == SSL3_MT_FINISHED) {
179
12.8k
            st->hand_state = TLS_ST_CR_FINISHED;
180
12.8k
            return 1;
181
12.8k
        }
182
3.19k
        break;
183
184
6.59k
    case TLS_ST_OK:
185
6.59k
        if (mt == SSL3_MT_NEWSESSION_TICKET) {
186
6.42k
            st->hand_state = TLS_ST_CR_SESSION_TICKET;
187
6.42k
            return 1;
188
6.42k
        }
189
171
        if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) {
190
0
            st->hand_state = TLS_ST_CR_KEY_UPDATE;
191
0
            return 1;
192
0
        }
193
171
        if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
194
#if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
195
            /* Restore digest for PHA before adding message.*/
196
#error Internal DTLS version error
197
#endif
198
5
            if (!SSL_CONNECTION_IS_DTLS(s)
199
5
                && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
200
0
                s->post_handshake_auth = SSL_PHA_REQUESTED;
201
                /*
202
                 * In TLS, this is called before the message is added to the
203
                 * digest. In DTLS, this is expected to be called after adding
204
                 * to the digest. Either move the digest restore, or add the
205
                 * message here after the swap, or do it after the clientFinished?
206
                 */
207
0
                if (!tls13_restore_handshake_digest_for_pha(s)) {
208
                    /* SSLfatal() already called */
209
0
                    return 0;
210
0
                }
211
0
                st->hand_state = TLS_ST_CR_CERT_REQ;
212
0
                return 1;
213
0
            }
214
5
        }
215
171
        break;
216
81.4k
    }
217
218
    /* No valid transition found */
219
3.90k
    return 0;
220
81.4k
}
221
222
/*
223
 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
224
 * handshake state transitions when the client is reading messages from the
225
 * server. The message type that the server has sent is provided in |mt|. The
226
 * current state is in |s->statem.hand_state|.
227
 *
228
 * Return values are 1 for success (transition allowed) and  0 on error
229
 * (transition not allowed)
230
 */
231
int ossl_statem_client_read_transition(SSL_CONNECTION *s, int mt)
232
201k
{
233
201k
    OSSL_STATEM *st = &s->statem;
234
201k
    int ske_expected;
235
236
    /*
237
     * Note that after writing the first ClientHello we don't know what version
238
     * we are going to negotiate yet, so we don't take this branch until later.
239
     */
240
201k
    if (SSL_CONNECTION_IS_TLS13(s)) {
241
68.1k
        if (!ossl_statem_client13_read_transition(s, mt))
242
3.26k
            goto err;
243
64.8k
        return 1;
244
68.1k
    }
245
246
132k
    switch (st->hand_state) {
247
0
    default:
248
0
        break;
249
250
67.6k
    case TLS_ST_CW_CLNT_HELLO:
251
67.6k
        if (mt == SSL3_MT_SERVER_HELLO) {
252
64.6k
            st->hand_state = TLS_ST_CR_SRVR_HELLO;
253
64.6k
            return 1;
254
64.6k
        }
255
256
3.04k
        if (SSL_CONNECTION_IS_DTLS(s)) {
257
2.60k
            if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
258
2.45k
                st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
259
2.45k
                return 1;
260
2.45k
            }
261
2.60k
        }
262
590
        break;
263
264
590
    case TLS_ST_EARLY_DATA:
265
        /*
266
         * We've not actually selected TLSv1.3 yet, but we have sent early
267
         * data. The only thing allowed now is a ServerHello or a
268
         * HelloRetryRequest.
269
         */
270
0
        if (mt == SSL3_MT_SERVER_HELLO) {
271
0
            st->hand_state = TLS_ST_CR_SRVR_HELLO;
272
0
            return 1;
273
0
        }
274
0
        break;
275
276
37.5k
    case TLS_ST_CR_SRVR_HELLO:
277
37.5k
        if (s->hit) {
278
0
            if (s->ext.ticket_expected) {
279
0
                if (mt == SSL3_MT_NEWSESSION_TICKET) {
280
0
                    st->hand_state = TLS_ST_CR_SESSION_TICKET;
281
0
                    return 1;
282
0
                }
283
0
            } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
284
0
                st->hand_state = TLS_ST_CR_CHANGE;
285
0
                return 1;
286
0
            }
287
37.5k
        } else {
288
37.5k
            if (SSL_CONNECTION_IS_DTLS(s)
289
15.3k
                && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
290
257
                st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
291
257
                return 1;
292
37.2k
            } else if (s->version >= TLS1_VERSION
293
36.6k
                && s->ext.session_secret_cb != NULL
294
0
                && s->session->ext.tick != NULL
295
0
                && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
296
                /*
297
                 * Normally, we can tell if the server is resuming the session
298
                 * from the session ID. EAP-FAST (RFC 4851), however, relies on
299
                 * the next server message after the ServerHello to determine if
300
                 * the server is resuming.
301
                 */
302
0
                s->hit = 1;
303
0
                st->hand_state = TLS_ST_CR_CHANGE;
304
0
                return 1;
305
37.2k
            } else if (!(s->s3.tmp.new_cipher->algorithm_auth
306
37.2k
                           & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
307
27.6k
                if (mt == SSL3_MT_CERTIFICATE) {
308
27.5k
                    st->hand_state = TLS_ST_CR_CERT;
309
27.5k
                    return 1;
310
27.5k
                }
311
27.6k
            } else {
312
9.64k
                ske_expected = key_exchange_expected(s);
313
                /* SKE is optional for some PSK ciphersuites */
314
9.64k
                if (ske_expected
315
0
                    || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
316
9.64k
                        && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
317
9.64k
                    if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
318
9.57k
                        st->hand_state = TLS_ST_CR_KEY_EXCH;
319
9.57k
                        return 1;
320
9.57k
                    }
321
9.64k
                } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
322
0
                    && cert_req_allowed(s)) {
323
0
                    st->hand_state = TLS_ST_CR_CERT_REQ;
324
0
                    return 1;
325
0
                } else if (mt == SSL3_MT_SERVER_DONE) {
326
0
                    st->hand_state = TLS_ST_CR_SRVR_DONE;
327
0
                    return 1;
328
0
                }
329
9.64k
            }
330
37.5k
        }
331
91
        break;
332
333
12.5k
    case TLS_ST_CR_CERT:
334
12.5k
    case TLS_ST_CR_COMP_CERT:
335
        /*
336
         * The CertificateStatus message is optional even if
337
         * |ext.status_expected| is set
338
         */
339
12.5k
        if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
340
0
            st->hand_state = TLS_ST_CR_CERT_STATUS;
341
0
            return 1;
342
0
        }
343
        /* Fall through */
344
345
12.5k
    case TLS_ST_CR_CERT_STATUS:
346
12.5k
        ske_expected = key_exchange_expected(s);
347
        /* SKE is optional for some PSK ciphersuites */
348
12.5k
        if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK) && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
349
4.01k
            if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
350
3.93k
                st->hand_state = TLS_ST_CR_KEY_EXCH;
351
3.93k
                return 1;
352
3.93k
            }
353
84
            goto err;
354
4.01k
        }
355
        /* Fall through */
356
357
14.2k
    case TLS_ST_CR_KEY_EXCH:
358
14.2k
        if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
359
656
            if (cert_req_allowed(s)) {
360
642
                st->hand_state = TLS_ST_CR_CERT_REQ;
361
642
                return 1;
362
642
            }
363
14
            goto err;
364
656
        }
365
        /* Fall through */
366
367
13.6k
    case TLS_ST_CR_CERT_REQ:
368
13.6k
        if (mt == SSL3_MT_SERVER_DONE) {
369
13.3k
            st->hand_state = TLS_ST_CR_SRVR_DONE;
370
13.3k
            return 1;
371
13.3k
        }
372
221
        break;
373
374
6.50k
    case TLS_ST_CW_FINISHED:
375
6.50k
        if (s->ext.ticket_expected) {
376
309
            if (mt == SSL3_MT_NEWSESSION_TICKET) {
377
289
                st->hand_state = TLS_ST_CR_SESSION_TICKET;
378
289
                return 1;
379
289
            }
380
6.19k
        } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
381
6.12k
            st->hand_state = TLS_ST_CR_CHANGE;
382
6.12k
            return 1;
383
6.12k
        }
384
98
        break;
385
386
189
    case TLS_ST_CR_SESSION_TICKET:
387
189
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
388
165
            st->hand_state = TLS_ST_CR_CHANGE;
389
165
            return 1;
390
165
        }
391
24
        break;
392
393
1.59k
    case TLS_ST_CR_CHANGE:
394
1.59k
        if (mt == SSL3_MT_FINISHED) {
395
1.38k
            st->hand_state = TLS_ST_CR_FINISHED;
396
1.38k
            return 1;
397
1.38k
        }
398
216
        break;
399
400
1.13k
    case TLS_ST_OK:
401
1.13k
        if (mt == SSL3_MT_HELLO_REQUEST) {
402
1.08k
            st->hand_state = TLS_ST_CR_HELLO_REQ;
403
1.08k
            return 1;
404
1.08k
        }
405
45
        break;
406
132k
    }
407
408
4.65k
err:
409
    /* No valid transition found */
410
4.65k
    if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
411
22
        BIO *rbio;
412
413
        /*
414
         * CCS messages don't have a message sequence number so this is probably
415
         * because of an out-of-order CCS. We'll just drop it.
416
         */
417
22
        s->init_num = 0;
418
22
        s->rwstate = SSL_READING;
419
22
        rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));
420
22
        BIO_clear_retry_flags(rbio);
421
22
        BIO_set_retry_read(rbio);
422
22
        return 0;
423
22
    }
424
4.65k
    SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
425
4.62k
    return 0;
426
4.65k
}
427
428
static int do_compressed_cert(SSL_CONNECTION *sc)
429
0
{
430
    /* If we negotiated RPK, we won't try to compress it */
431
0
    return sc->ext.client_cert_type == TLSEXT_cert_type_x509
432
0
        && sc->ext.compress_certificate_from_peer[0] != TLSEXT_comp_cert_none;
433
0
}
434
435
/*
436
 * ossl_statem_client13_write_transition() works out what handshake state to
437
 * move to next when the TLSv1.3 client is writing messages to be sent to the
438
 * server.
439
 */
440
static WRITE_TRAN ossl_statem_client13_write_transition(SSL_CONNECTION *s)
441
18.7k
{
442
18.7k
    OSSL_STATEM *st = &s->statem;
443
444
    /*
445
     * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
446
     * TLSv1.3 yet at that point. They are handled by
447
     * ossl_statem_client_write_transition().
448
     */
449
18.7k
    switch (st->hand_state) {
450
0
    default:
451
        /* Shouldn't happen */
452
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
453
0
        return WRITE_TRAN_ERROR;
454
455
0
    case TLS_ST_CR_CERT_REQ:
456
0
        if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
457
0
            if (do_compressed_cert(s))
458
0
                st->hand_state = TLS_ST_CW_COMP_CERT;
459
0
            else
460
0
                st->hand_state = TLS_ST_CW_CERT;
461
0
            return WRITE_TRAN_CONTINUE;
462
0
        }
463
        /*
464
         * We should only get here if we received a CertificateRequest after
465
         * we already sent close_notify
466
         */
467
0
        if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
468
            /* Shouldn't happen - same as default case */
469
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
470
0
            return WRITE_TRAN_ERROR;
471
0
        }
472
0
        st->hand_state = TLS_ST_OK;
473
0
        return WRITE_TRAN_CONTINUE;
474
475
6.91k
    case TLS_ST_CR_FINISHED:
476
6.91k
        if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
477
6.91k
            || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
478
0
            st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
479
6.91k
        else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
480
0
            && s->hello_retry_request == SSL_HRR_NONE)
481
0
            st->hand_state = TLS_ST_CW_CHANGE;
482
6.91k
        else if (s->s3.tmp.cert_req == 0)
483
6.91k
            st->hand_state = TLS_ST_CW_FINISHED;
484
0
        else if (do_compressed_cert(s))
485
0
            st->hand_state = TLS_ST_CW_COMP_CERT;
486
0
        else
487
0
            st->hand_state = TLS_ST_CW_CERT;
488
489
6.91k
        s->ts_msg_read = ossl_time_now();
490
6.91k
        return WRITE_TRAN_CONTINUE;
491
492
0
    case TLS_ST_PENDING_EARLY_DATA_END:
493
0
        if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED && !SSL_NO_EOED(s)) {
494
0
            st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
495
0
            return WRITE_TRAN_CONTINUE;
496
0
        }
497
        /* Fall through */
498
499
0
    case TLS_ST_CW_END_OF_EARLY_DATA:
500
0
    case TLS_ST_CW_CHANGE:
501
0
        if (s->s3.tmp.cert_req == 0)
502
0
            st->hand_state = TLS_ST_CW_FINISHED;
503
0
        else if (do_compressed_cert(s))
504
0
            st->hand_state = TLS_ST_CW_COMP_CERT;
505
0
        else
506
0
            st->hand_state = TLS_ST_CW_CERT;
507
0
        return WRITE_TRAN_CONTINUE;
508
509
0
    case TLS_ST_CW_COMP_CERT:
510
0
    case TLS_ST_CW_CERT:
511
        /* If a non-empty Certificate we also send CertificateVerify */
512
0
        st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
513
0
                                                   : TLS_ST_CW_FINISHED;
514
0
        return WRITE_TRAN_CONTINUE;
515
516
0
    case TLS_ST_CW_CERT_VRFY:
517
0
        st->hand_state = TLS_ST_CW_FINISHED;
518
0
        return WRITE_TRAN_CONTINUE;
519
520
0
    case TLS_ST_CR_KEY_UPDATE:
521
0
    case TLS_ST_CW_KEY_UPDATE:
522
1.18k
    case TLS_ST_CR_SESSION_TICKET:
523
8.09k
    case TLS_ST_CW_FINISHED:
524
8.09k
        st->hand_state = TLS_ST_OK;
525
8.09k
        return WRITE_TRAN_CONTINUE;
526
527
3.70k
    case TLS_ST_OK:
528
3.70k
        if (s->key_update != SSL_KEY_UPDATE_NONE) {
529
0
            st->hand_state = TLS_ST_CW_KEY_UPDATE;
530
0
            return WRITE_TRAN_CONTINUE;
531
0
        }
532
533
        /* Try to read from the server instead */
534
3.70k
        return WRITE_TRAN_FINISHED;
535
18.7k
    }
536
18.7k
}
537
538
/*
539
 * ossl_statem_client_write_transition() works out what handshake state to
540
 * move to next when the client is writing messages to be sent to the server.
541
 */
542
WRITE_TRAN ossl_statem_client_write_transition(SSL_CONNECTION *s)
543
186k
{
544
186k
    OSSL_STATEM *st = &s->statem;
545
546
    /*
547
     * Note that immediately before/after a ClientHello we don't know what
548
     * version we are going to negotiate yet, so we don't take this branch until
549
     * later
550
     */
551
186k
    if (SSL_CONNECTION_IS_TLS13(s))
552
18.7k
        return ossl_statem_client13_write_transition(s);
553
554
168k
    switch (st->hand_state) {
555
0
    default:
556
        /* Shouldn't happen */
557
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
558
0
        return WRITE_TRAN_ERROR;
559
560
827
    case TLS_ST_OK:
561
827
        if (!s->renegotiate) {
562
            /*
563
             * We haven't requested a renegotiation ourselves so we must have
564
             * received a message from the server. Better read it.
565
             */
566
827
            return WRITE_TRAN_FINISHED;
567
827
        }
568
        /* Renegotiation */
569
        /* fall thru */
570
63.7k
    case TLS_ST_BEFORE:
571
63.7k
        st->hand_state = TLS_ST_CW_CLNT_HELLO;
572
63.7k
        return WRITE_TRAN_CONTINUE;
573
574
65.9k
    case TLS_ST_CW_CLNT_HELLO:
575
65.9k
        if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
576
0
            && !SSL_IS_QUIC_HANDSHAKE(s)) {
577
            /*
578
             * We are assuming this is a TLSv1.3 connection, although we haven't
579
             * actually selected a version yet.
580
             */
581
0
            if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
582
0
                st->hand_state = TLS_ST_CW_CHANGE;
583
0
            else
584
0
                st->hand_state = TLS_ST_EARLY_DATA;
585
0
            return WRITE_TRAN_CONTINUE;
586
0
        }
587
        /*
588
         * No transition at the end of writing because we don't know what
589
         * we will be sent
590
         */
591
65.9k
        s->ts_msg_write = ossl_time_now();
592
65.9k
        return WRITE_TRAN_FINISHED;
593
594
204
    case TLS_ST_CR_SRVR_HELLO:
595
        /*
596
         * We only get here in TLSv1.3. We just received an HRR, so issue a
597
         * CCS unless middlebox compat mode is off, or we already issued one
598
         * because we did early data.
599
         */
600
204
        if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
601
186
            && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
602
186
            st->hand_state = TLS_ST_CW_CHANGE;
603
18
        else
604
18
            st->hand_state = TLS_ST_CW_CLNT_HELLO;
605
204
        return WRITE_TRAN_CONTINUE;
606
607
0
    case TLS_ST_EARLY_DATA:
608
0
        s->ts_msg_write = ossl_time_now();
609
0
        return WRITE_TRAN_FINISHED;
610
611
1.20k
    case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
612
1.20k
        st->hand_state = TLS_ST_CW_CLNT_HELLO;
613
1.20k
        return WRITE_TRAN_CONTINUE;
614
615
9.05k
    case TLS_ST_CR_SRVR_DONE:
616
9.05k
        s->ts_msg_read = ossl_time_now();
617
9.05k
        if (s->s3.tmp.cert_req)
618
34
            st->hand_state = TLS_ST_CW_CERT;
619
9.02k
        else
620
9.02k
            st->hand_state = TLS_ST_CW_KEY_EXCH;
621
9.05k
        return WRITE_TRAN_CONTINUE;
622
623
34
    case TLS_ST_CW_CERT:
624
34
        st->hand_state = TLS_ST_CW_KEY_EXCH;
625
34
        return WRITE_TRAN_CONTINUE;
626
627
8.37k
    case TLS_ST_CW_KEY_EXCH:
628
        /*
629
         * For TLS, cert_req is set to 2, so a cert chain of nothing is
630
         * sent, but no verify packet is sent
631
         */
632
        /*
633
         * XXX: For now, we do not support client authentication in ECDH
634
         * cipher suites with ECDH (rather than ECDSA) certificates. We
635
         * need to skip the certificate verify message when client's
636
         * ECDH public key is sent inside the client certificate.
637
         */
638
8.37k
        if (s->s3.tmp.cert_req == 1) {
639
0
            st->hand_state = TLS_ST_CW_CERT_VRFY;
640
8.37k
        } else {
641
8.37k
            st->hand_state = TLS_ST_CW_CHANGE;
642
8.37k
        }
643
8.37k
        if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
644
0
            st->hand_state = TLS_ST_CW_CHANGE;
645
0
        }
646
8.37k
        return WRITE_TRAN_CONTINUE;
647
648
0
    case TLS_ST_CW_CERT_VRFY:
649
0
        st->hand_state = TLS_ST_CW_CHANGE;
650
0
        return WRITE_TRAN_CONTINUE;
651
652
8.55k
    case TLS_ST_CW_CHANGE:
653
8.55k
        if (s->hello_retry_request == SSL_HRR_PENDING) {
654
186
            st->hand_state = TLS_ST_CW_CLNT_HELLO;
655
8.37k
        } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
656
0
            st->hand_state = TLS_ST_EARLY_DATA;
657
8.37k
        } else {
658
#if defined(OPENSSL_NO_NEXTPROTONEG)
659
            st->hand_state = TLS_ST_CW_FINISHED;
660
#else
661
8.37k
            if (!SSL_CONNECTION_IS_DTLS(s) && s->s3.npn_seen)
662
0
                st->hand_state = TLS_ST_CW_NEXT_PROTO;
663
8.37k
            else
664
8.37k
                st->hand_state = TLS_ST_CW_FINISHED;
665
8.37k
#endif
666
8.37k
        }
667
8.55k
        return WRITE_TRAN_CONTINUE;
668
669
0
#if !defined(OPENSSL_NO_NEXTPROTONEG)
670
0
    case TLS_ST_CW_NEXT_PROTO:
671
0
        st->hand_state = TLS_ST_CW_FINISHED;
672
0
        return WRITE_TRAN_CONTINUE;
673
0
#endif
674
675
8.37k
    case TLS_ST_CW_FINISHED:
676
8.37k
        if (s->hit) {
677
0
            st->hand_state = TLS_ST_OK;
678
0
            return WRITE_TRAN_CONTINUE;
679
8.37k
        } else {
680
8.37k
            return WRITE_TRAN_FINISHED;
681
8.37k
        }
682
683
994
    case TLS_ST_CR_FINISHED:
684
994
        if (s->hit) {
685
0
            st->hand_state = TLS_ST_CW_CHANGE;
686
0
            return WRITE_TRAN_CONTINUE;
687
994
        } else {
688
994
            st->hand_state = TLS_ST_OK;
689
994
            return WRITE_TRAN_CONTINUE;
690
994
        }
691
692
773
    case TLS_ST_CR_HELLO_REQ:
693
        /*
694
         * If we can renegotiate now then do so, otherwise wait for a more
695
         * convenient time.
696
         */
697
773
        if (ssl3_renegotiate_check(SSL_CONNECTION_GET_SSL(s), 1)) {
698
773
            if (!tls_setup_handshake(s)) {
699
                /* SSLfatal() already called */
700
0
                return WRITE_TRAN_ERROR;
701
0
            }
702
773
            st->hand_state = TLS_ST_CW_CLNT_HELLO;
703
773
            return WRITE_TRAN_CONTINUE;
704
773
        }
705
0
        st->hand_state = TLS_ST_OK;
706
0
        return WRITE_TRAN_CONTINUE;
707
168k
    }
708
168k
}
709
710
/*
711
 * Perform any pre work that needs to be done prior to sending a message from
712
 * the client to the server.
713
 */
714
WORK_STATE ossl_statem_client_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
715
193k
{
716
193k
    OSSL_STATEM *st = &s->statem;
717
718
193k
    switch (st->hand_state) {
719
45.1k
    default:
720
        /* No pre work to be done */
721
45.1k
        break;
722
723
116k
    case TLS_ST_CW_CLNT_HELLO:
724
116k
        s->shutdown = 0;
725
116k
        if (SSL_CONNECTION_IS_DTLS(s)) {
726
            /* every DTLS ClientHello resets Finished MAC */
727
30.8k
            if (!ssl3_init_finished_mac(s)) {
728
                /* SSLfatal() already called */
729
0
                return WORK_ERROR;
730
0
            }
731
85.2k
        } else if (s->ext.early_data == SSL_EARLY_DATA_REJECTED) {
732
            /*
733
             * This must be a second ClientHello after an HRR following an
734
             * earlier rejected attempt to send early data. Since we were
735
             * previously encrypting the early data we now need to reset the
736
             * write record layer in order to write in plaintext again.
737
             */
738
0
            if (!ssl_set_new_record_layer(s,
739
0
                    TLS_ANY_VERSION,
740
0
                    OSSL_RECORD_DIRECTION_WRITE,
741
0
                    OSSL_RECORD_PROTECTION_LEVEL_NONE,
742
0
                    NULL, 0, NULL, 0, NULL, 0, NULL, 0,
743
0
                    NULL, 0, NID_undef, NULL, NULL,
744
0
                    NULL)) {
745
                /* SSLfatal already called */
746
0
                return WORK_ERROR;
747
0
            }
748
0
        }
749
116k
        break;
750
751
116k
    case TLS_ST_CW_CHANGE:
752
15.7k
        if (SSL_CONNECTION_IS_DTLS(s)) {
753
7.81k
            if (s->hit) {
754
                /*
755
                 * We're into the last flight so we don't retransmit these
756
                 * messages unless we need to.
757
                 */
758
0
                st->use_timer = 0;
759
0
            }
760
#ifndef OPENSSL_NO_SCTP
761
            if (BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)))) {
762
                /* Calls SSLfatal() as required */
763
                return dtls_wait_for_dry(s);
764
            }
765
#endif
766
7.81k
        }
767
15.7k
        break;
768
769
0
    case TLS_ST_PENDING_EARLY_DATA_END:
770
        /*
771
         * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
772
         * attempt to write early data before calling SSL_read() then we press
773
         * on with the handshake. Otherwise we pause here.
774
         */
775
0
        if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
776
0
            || s->early_data_state == SSL_EARLY_DATA_NONE)
777
0
            return WORK_FINISHED_CONTINUE;
778
        /* Fall through */
779
780
0
    case TLS_ST_EARLY_DATA:
781
0
        return tls_finish_handshake(s, wst, 0, 1);
782
783
16.3k
    case TLS_ST_OK:
784
        /* Calls SSLfatal() as required */
785
16.3k
        return tls_finish_handshake(s, wst, 1, 1);
786
193k
    }
787
788
177k
    return WORK_FINISHED_CONTINUE;
789
193k
}
790
791
/*
792
 * Perform any work that needs to be done after sending a message from the
793
 * client to the server.
794
 */
795
WORK_STATE ossl_statem_client_post_work(SSL_CONNECTION *s, WORK_STATE wst)
796
98.2k
{
797
98.2k
    OSSL_STATEM *st = &s->statem;
798
98.2k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
799
800
98.2k
    s->init_num = 0;
801
802
98.2k
    switch (st->hand_state) {
803
34
    default:
804
        /* No post work to be done */
805
34
        break;
806
807
65.9k
    case TLS_ST_CW_CLNT_HELLO:
808
65.9k
        if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
809
0
            && s->max_early_data > 0) {
810
            /*
811
             * We haven't selected TLSv1.3 yet so we don't call the change
812
             * cipher state function associated with the SSL_METHOD. Instead
813
             * we call tls13_change_cipher_state() directly.
814
             */
815
0
            if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
816
0
                if (!tls13_change_cipher_state(s,
817
0
                        SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
818
                    /* SSLfatal() already called */
819
0
                    return WORK_ERROR;
820
0
                }
821
0
            }
822
            /* else we're in compat mode so we delay flushing until after CCS */
823
65.9k
        } else if (!statem_flush(s)) {
824
0
            return WORK_MORE_A;
825
0
        }
826
827
65.9k
        if (SSL_CONNECTION_IS_DTLS(s)) {
828
            /* Treat the next message as the first packet */
829
16.9k
            s->first_packet = 1;
830
16.9k
        }
831
65.9k
        break;
832
833
8.37k
    case TLS_ST_CW_KEY_EXCH:
834
8.37k
        if (tls_client_key_exchange_post_work(s) == 0) {
835
            /* SSLfatal() already called */
836
0
            return WORK_ERROR;
837
0
        }
838
8.37k
        break;
839
840
8.55k
    case TLS_ST_CW_CHANGE:
841
8.55k
        if (SSL_CONNECTION_IS_TLS13(s)
842
8.55k
            || s->hello_retry_request == SSL_HRR_PENDING)
843
186
            break;
844
8.37k
        if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
845
0
            && s->max_early_data > 0) {
846
            /*
847
             * We haven't selected TLSv1.3 yet so we don't call the change
848
             * cipher state function associated with the SSL_METHOD. Instead
849
             * we call tls13_change_cipher_state() directly.
850
             */
851
0
            if (!tls13_change_cipher_state(s,
852
0
                    SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
853
0
                return WORK_ERROR;
854
0
            break;
855
0
        }
856
8.37k
        s->session->cipher = s->s3.tmp.new_cipher;
857
#ifdef OPENSSL_NO_COMP
858
        s->session->compress_meth = 0;
859
#else
860
8.37k
        if (s->s3.tmp.new_compression == NULL)
861
8.37k
            s->session->compress_meth = 0;
862
0
        else
863
0
            s->session->compress_meth = s->s3.tmp.new_compression->id;
864
8.37k
#endif
865
8.37k
        if (!ssl->method->ssl3_enc->setup_key_block(s)) {
866
            /* SSLfatal() already called */
867
0
            return WORK_ERROR;
868
0
        }
869
870
8.37k
        if (!ssl->method->ssl3_enc->change_cipher_state(s,
871
8.37k
                SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
872
            /* SSLfatal() already called */
873
0
            return WORK_ERROR;
874
0
        }
875
876
#ifndef OPENSSL_NO_SCTP
877
        if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
878
            /*
879
             * Change to new shared key of SCTP-Auth, will be ignored if
880
             * no SCTP used.
881
             */
882
            BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
883
                0, NULL);
884
        }
885
#endif
886
8.37k
        break;
887
888
15.2k
    case TLS_ST_CW_FINISHED:
889
#ifndef OPENSSL_NO_SCTP
890
        if (wst == WORK_MORE_A && SSL_CONNECTION_IS_DTLS(s) && s->hit == 0) {
891
            /*
892
             * Change to new shared key of SCTP-Auth, will be ignored if
893
             * no SCTP used.
894
             */
895
            BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
896
                0, NULL);
897
        }
898
#endif
899
15.2k
        if (statem_flush(s) != 1)
900
0
            return WORK_MORE_B;
901
902
15.2k
        if (SSL_CONNECTION_IS_TLS13(s)) {
903
6.91k
            if (!tls13_save_handshake_digest_for_pha(s)) {
904
                /* SSLfatal() already called */
905
0
                return WORK_ERROR;
906
0
            }
907
6.91k
            if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
908
6.91k
                if (!ssl->method->ssl3_enc->change_cipher_state(s,
909
6.91k
                        SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
910
                    /* SSLfatal() already called */
911
0
                    return WORK_ERROR;
912
0
                }
913
                /*
914
                 * For QUIC we deferred setting up these keys until now so
915
                 * that we can ensure write keys are always set up before read
916
                 * keys.
917
                 */
918
6.91k
                if (SSL_IS_QUIC_HANDSHAKE(s)
919
6.91k
                    && !ssl->method->ssl3_enc->change_cipher_state(s,
920
6.91k
                        SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
921
                    /* SSLfatal() already called */
922
6
                    return WORK_ERROR;
923
6
                }
924
6.91k
            }
925
6.91k
        }
926
15.2k
        break;
927
928
15.2k
    case TLS_ST_CW_KEY_UPDATE:
929
0
        if (statem_flush(s) != 1)
930
0
            return WORK_MORE_A;
931
0
        if (!tls13_update_key(s, 1)) {
932
            /* SSLfatal() already called */
933
0
            return WORK_ERROR;
934
0
        }
935
0
        break;
936
98.2k
    }
937
938
98.2k
    return WORK_FINISHED_CONTINUE;
939
98.2k
}
940
941
/*
942
 * Get the message construction function and message type for sending from the
943
 * client
944
 *
945
 * Valid return values are:
946
 *   1: Success
947
 *   0: Error
948
 */
949
int ossl_statem_client_construct_message(SSL_CONNECTION *s,
950
    confunc_f *confunc, int *mt)
951
186k
{
952
186k
    OSSL_STATEM *st = &s->statem;
953
954
186k
    switch (st->hand_state) {
955
0
    default:
956
        /* Shouldn't happen */
957
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
958
0
        return 0;
959
960
16.8k
    case TLS_ST_CW_CHANGE:
961
16.8k
        if (SSL_CONNECTION_IS_DTLS(s))
962
7.81k
            *confunc = dtls_construct_change_cipher_spec;
963
9.00k
        else
964
9.00k
            *confunc = tls_construct_change_cipher_spec;
965
16.8k
        *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
966
16.8k
        break;
967
968
122k
    case TLS_ST_CW_CLNT_HELLO:
969
122k
        *confunc = tls_construct_client_hello;
970
122k
        *mt = SSL3_MT_CLIENT_HELLO;
971
122k
        break;
972
973
0
    case TLS_ST_CW_END_OF_EARLY_DATA:
974
0
        *confunc = tls_construct_end_of_early_data;
975
0
        *mt = SSL3_MT_END_OF_EARLY_DATA;
976
0
        break;
977
978
0
    case TLS_ST_PENDING_EARLY_DATA_END:
979
0
        *confunc = NULL;
980
0
        *mt = SSL3_MT_DUMMY;
981
0
        break;
982
983
49
    case TLS_ST_CW_CERT:
984
49
        *confunc = tls_construct_client_certificate;
985
49
        *mt = SSL3_MT_CERTIFICATE;
986
49
        break;
987
988
#ifndef OPENSSL_NO_COMP_ALG
989
    case TLS_ST_CW_COMP_CERT:
990
        *confunc = tls_construct_client_compressed_certificate;
991
        *mt = SSL3_MT_COMPRESSED_CERTIFICATE;
992
        break;
993
#endif
994
995
18.2k
    case TLS_ST_CW_KEY_EXCH:
996
18.2k
        *confunc = tls_construct_client_key_exchange;
997
18.2k
        *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
998
18.2k
        break;
999
1000
0
    case TLS_ST_CW_CERT_VRFY:
1001
0
        *confunc = tls_construct_cert_verify;
1002
0
        *mt = SSL3_MT_CERTIFICATE_VERIFY;
1003
0
        break;
1004
1005
0
#if !defined(OPENSSL_NO_NEXTPROTONEG)
1006
0
    case TLS_ST_CW_NEXT_PROTO:
1007
0
        *confunc = tls_construct_next_proto;
1008
0
        *mt = SSL3_MT_NEXT_PROTO;
1009
0
        break;
1010
0
#endif
1011
29.1k
    case TLS_ST_CW_FINISHED:
1012
29.1k
        *confunc = tls_construct_finished;
1013
29.1k
        *mt = SSL3_MT_FINISHED;
1014
29.1k
        break;
1015
1016
0
    case TLS_ST_CW_KEY_UPDATE:
1017
0
        *confunc = tls_construct_key_update;
1018
0
        *mt = SSL3_MT_KEY_UPDATE;
1019
0
        break;
1020
186k
    }
1021
1022
186k
    return 1;
1023
186k
}
1024
1025
/*
1026
 * Returns the maximum allowed length for the current message that we are
1027
 * reading. Excludes the message header.
1028
 */
1029
size_t ossl_statem_client_max_message_size(SSL_CONNECTION *s)
1030
241k
{
1031
241k
    OSSL_STATEM *st = &s->statem;
1032
1033
241k
    switch (st->hand_state) {
1034
1.30k
    default:
1035
        /* Shouldn't happen */
1036
1.30k
        return 0;
1037
1038
79.6k
    case TLS_ST_CR_SRVR_HELLO:
1039
79.6k
        return SERVER_HELLO_MAX_LENGTH;
1040
1041
3.66k
    case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1042
3.66k
        return HELLO_VERIFY_REQUEST_MAX_LENGTH;
1043
1044
0
    case TLS_ST_CR_COMP_CERT:
1045
54.8k
    case TLS_ST_CR_CERT:
1046
54.8k
        return s->max_cert_list;
1047
1048
16.6k
    case TLS_ST_CR_CERT_VRFY:
1049
16.6k
        return CERTIFICATE_VERIFY_MAX_LENGTH;
1050
1051
0
    case TLS_ST_CR_CERT_STATUS:
1052
0
        return SSL3_RT_MAX_PLAIN_LENGTH;
1053
1054
16.5k
    case TLS_ST_CR_KEY_EXCH:
1055
16.5k
        return SERVER_KEY_EXCH_MAX_LENGTH;
1056
1057
975
    case TLS_ST_CR_CERT_REQ:
1058
        /*
1059
         * Set to s->max_cert_list for compatibility with previous releases. In
1060
         * practice these messages can get quite long if servers are configured
1061
         * to provide a long list of acceptable CAs
1062
         */
1063
975
        return s->max_cert_list;
1064
1065
17.0k
    case TLS_ST_CR_SRVR_DONE:
1066
17.0k
        return SERVER_HELLO_DONE_MAX_LENGTH;
1067
1068
7.97k
    case TLS_ST_CR_CHANGE:
1069
7.97k
        if (s->version == DTLS1_BAD_VER)
1070
0
            return 3;
1071
7.97k
        return CCS_MAX_LENGTH;
1072
1073
6.80k
    case TLS_ST_CR_SESSION_TICKET:
1074
6.80k
        return (SSL_CONNECTION_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13
1075
6.80k
                                            : SESSION_TICKET_MAX_LENGTH_TLS12;
1076
1077
14.4k
    case TLS_ST_CR_FINISHED:
1078
14.4k
        return FINISHED_MAX_LENGTH;
1079
1080
21.5k
    case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1081
21.5k
        return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
1082
1083
0
    case TLS_ST_CR_KEY_UPDATE:
1084
0
        return KEY_UPDATE_MAX_LENGTH;
1085
241k
    }
1086
241k
}
1087
1088
/*
1089
 * Process a message that the client has received from the server.
1090
 */
1091
MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL_CONNECTION *s,
1092
    PACKET *pkt)
1093
247k
{
1094
247k
    OSSL_STATEM *st = &s->statem;
1095
1096
247k
    switch (st->hand_state) {
1097
0
    default:
1098
        /* Shouldn't happen */
1099
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1100
0
        return MSG_PROCESS_ERROR;
1101
1102
83.2k
    case TLS_ST_CR_SRVR_HELLO:
1103
83.2k
        return tls_process_server_hello(s, pkt);
1104
1105
3.60k
    case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1106
3.60k
        return dtls_process_hello_verify(s, pkt);
1107
1108
58.2k
    case TLS_ST_CR_CERT:
1109
58.2k
        return tls_process_server_certificate(s, pkt);
1110
1111
#ifndef OPENSSL_NO_COMP_ALG
1112
    case TLS_ST_CR_COMP_CERT:
1113
        return tls_process_server_compressed_certificate(s, pkt);
1114
#endif
1115
1116
16.2k
    case TLS_ST_CR_CERT_VRFY:
1117
16.2k
        return tls_process_cert_verify(s, pkt);
1118
1119
0
    case TLS_ST_CR_CERT_STATUS:
1120
0
        return tls_process_cert_status(s, pkt);
1121
1122
18.7k
    case TLS_ST_CR_KEY_EXCH:
1123
18.7k
        return tls_process_key_exchange(s, pkt);
1124
1125
1.07k
    case TLS_ST_CR_CERT_REQ:
1126
1.07k
        return tls_process_certificate_request(s, pkt);
1127
1128
18.3k
    case TLS_ST_CR_SRVR_DONE:
1129
18.3k
        return tls_process_server_done(s, pkt);
1130
1131
8.28k
    case TLS_ST_CR_CHANGE:
1132
8.28k
        return tls_process_change_cipher_spec(s, pkt);
1133
1134
2.68k
    case TLS_ST_CR_SESSION_TICKET:
1135
2.68k
        return tls_process_new_session_ticket(s, pkt);
1136
1137
14.4k
    case TLS_ST_CR_FINISHED:
1138
14.4k
        return tls_process_finished(s, pkt);
1139
1140
1.28k
    case TLS_ST_CR_HELLO_REQ:
1141
1.28k
        return tls_process_hello_req(s, pkt);
1142
1143
21.5k
    case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1144
21.5k
        return tls_process_encrypted_extensions(s, pkt);
1145
1146
0
    case TLS_ST_CR_KEY_UPDATE:
1147
0
        return tls_process_key_update(s, pkt);
1148
247k
    }
1149
247k
}
1150
1151
/*
1152
 * Perform any further processing required following the receipt of a message
1153
 * from the server
1154
 */
1155
WORK_STATE ossl_statem_client_post_process_message(SSL_CONNECTION *s,
1156
    WORK_STATE wst)
1157
36.2k
{
1158
36.2k
    OSSL_STATEM *st = &s->statem;
1159
1160
36.2k
    switch (st->hand_state) {
1161
0
    default:
1162
        /* Shouldn't happen */
1163
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1164
0
        return WORK_ERROR;
1165
1166
36.1k
    case TLS_ST_CR_CERT:
1167
36.1k
    case TLS_ST_CR_COMP_CERT:
1168
36.1k
        return tls_post_process_server_certificate(s, wst);
1169
1170
0
    case TLS_ST_CR_CERT_VRFY:
1171
74
    case TLS_ST_CR_CERT_REQ:
1172
74
        return tls_prepare_client_certificate(s, wst);
1173
36.2k
    }
1174
36.2k
}
1175
1176
CON_FUNC_RETURN tls_construct_client_hello(SSL_CONNECTION *s, WPACKET *pkt)
1177
100k
{
1178
100k
    unsigned char *p;
1179
100k
    size_t sess_id_len;
1180
100k
    int i, protverr;
1181
100k
#ifndef OPENSSL_NO_COMP
1182
100k
    SSL_COMP *comp;
1183
100k
#endif
1184
100k
    SSL_SESSION *sess = s->session;
1185
100k
    unsigned char *session_id;
1186
100k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1187
1188
    /* Work out what SSL/TLS/DTLS version to use */
1189
100k
    protverr = ssl_set_client_hello_version(s);
1190
100k
    if (protverr != 0) {
1191
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr);
1192
0
        return CON_FUNC_ERROR;
1193
0
    }
1194
1195
100k
    if (sess == NULL
1196
4.10k
        || !ssl_version_supported(s, sess->ssl_version, NULL)
1197
100k
        || !SSL_SESSION_is_resumable(sess)) {
1198
100k
        if (s->hello_retry_request == SSL_HRR_NONE
1199
99.9k
            && !ssl_get_new_session(s, 0)) {
1200
            /* SSLfatal() already called */
1201
0
            return CON_FUNC_ERROR;
1202
0
        }
1203
100k
    }
1204
    /* else use the pre-loaded session */
1205
1206
100k
    p = s->s3.client_random;
1207
1208
    /*
1209
     * for DTLS if client_random is initialized, reuse it, we are
1210
     * required to use same upon reply to HelloVerify
1211
     */
1212
100k
    if (SSL_CONNECTION_IS_DTLS(s)) {
1213
24.0k
        size_t idx;
1214
24.0k
        i = 1;
1215
708k
        for (idx = 0; idx < sizeof(s->s3.client_random); idx++) {
1216
686k
            if (p[idx]) {
1217
2.63k
                i = 0;
1218
2.63k
                break;
1219
2.63k
            }
1220
686k
        }
1221
76.8k
    } else {
1222
76.8k
        i = (s->hello_retry_request == SSL_HRR_NONE);
1223
76.8k
    }
1224
1225
100k
    if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random), DOWNGRADE_NONE) <= 0) {
1226
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1227
0
        return CON_FUNC_ERROR;
1228
0
    }
1229
1230
    /*-
1231
     * version indicates the negotiated version: for example from
1232
     * an SSLv2/v3 compatible client hello). The client_version
1233
     * field is the maximum version we permit and it is also
1234
     * used in RSA encrypted premaster secrets. Some servers can
1235
     * choke if we initially report a higher version then
1236
     * renegotiate to a lower one in the premaster secret. This
1237
     * didn't happen with TLS 1.0 as most servers supported it
1238
     * but it can with TLS 1.1 or later if the server only supports
1239
     * 1.0.
1240
     *
1241
     * Possible scenario with previous logic:
1242
     *      1. Client hello indicates TLS 1.2
1243
     *      2. Server hello says TLS 1.0
1244
     *      3. RSA encrypted premaster secret uses 1.2.
1245
     *      4. Handshake proceeds using TLS 1.0.
1246
     *      5. Server sends hello request to renegotiate.
1247
     *      6. Client hello indicates TLS v1.0 as we now
1248
     *         know that is maximum server supports.
1249
     *      7. Server chokes on RSA encrypted premaster secret
1250
     *         containing version 1.0.
1251
     *
1252
     * For interoperability it should be OK to always use the
1253
     * maximum version we support in client hello and then rely
1254
     * on the checking of version to ensure the servers isn't
1255
     * being inconsistent: for example initially negotiating with
1256
     * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
1257
     * client_version in client hello and not resetting it to
1258
     * the negotiated version.
1259
     *
1260
     * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
1261
     * supported_versions extension for the real supported versions.
1262
     */
1263
100k
    if (!WPACKET_put_bytes_u16(pkt, s->client_version)
1264
100k
        || !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) {
1265
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1266
0
        return CON_FUNC_ERROR;
1267
0
    }
1268
1269
    /* Session ID */
1270
100k
    session_id = s->session->session_id;
1271
100k
    if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) {
1272
75.8k
        if (s->version == TLS1_3_VERSION
1273
75.8k
            && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) {
1274
33.8k
            sess_id_len = sizeof(s->tmp_session_id);
1275
33.8k
            s->tmp_session_id_len = sess_id_len;
1276
33.8k
            session_id = s->tmp_session_id;
1277
33.8k
            if (s->hello_retry_request == SSL_HRR_NONE
1278
33.4k
                && RAND_bytes_ex(sctx->libctx, s->tmp_session_id,
1279
33.4k
                       sess_id_len, 0)
1280
33.4k
                    <= 0) {
1281
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1282
0
                return CON_FUNC_ERROR;
1283
0
            }
1284
41.9k
        } else {
1285
41.9k
            sess_id_len = 0;
1286
41.9k
        }
1287
75.8k
    } else {
1288
25.0k
        assert(s->session->session_id_length <= sizeof(s->session->session_id));
1289
25.0k
        sess_id_len = s->session->session_id_length;
1290
25.0k
        if (s->version == TLS1_3_VERSION) {
1291
0
            s->tmp_session_id_len = sess_id_len;
1292
0
            memcpy(s->tmp_session_id, s->session->session_id, sess_id_len);
1293
0
        }
1294
25.0k
    }
1295
100k
    if (!WPACKET_start_sub_packet_u8(pkt)
1296
100k
        || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id, sess_id_len))
1297
100k
        || !WPACKET_close(pkt)) {
1298
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1299
0
        return CON_FUNC_ERROR;
1300
0
    }
1301
1302
    /* cookie stuff for DTLS */
1303
100k
    if (SSL_CONNECTION_IS_DTLS(s)) {
1304
24.0k
        if (s->d1->cookie_len > sizeof(s->d1->cookie)
1305
24.0k
            || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
1306
24.0k
                s->d1->cookie_len)) {
1307
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1308
0
            return CON_FUNC_ERROR;
1309
0
        }
1310
24.0k
    }
1311
1312
    /* Ciphers supported */
1313
100k
    if (!WPACKET_start_sub_packet_u16(pkt)) {
1314
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1315
0
        return CON_FUNC_ERROR;
1316
0
    }
1317
1318
100k
    if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)),
1319
100k
            pkt)) {
1320
        /* SSLfatal() already called */
1321
0
        return CON_FUNC_ERROR;
1322
0
    }
1323
100k
    if (!WPACKET_close(pkt)) {
1324
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1325
0
        return CON_FUNC_ERROR;
1326
0
    }
1327
1328
    /* COMPRESSION */
1329
100k
    if (!WPACKET_start_sub_packet_u8(pkt)) {
1330
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1331
0
        return CON_FUNC_ERROR;
1332
0
    }
1333
100k
#ifndef OPENSSL_NO_COMP
1334
100k
    if (ssl_allow_compression(s)
1335
0
        && sctx->comp_methods
1336
0
        && (SSL_CONNECTION_IS_DTLS(s)
1337
0
            || s->s3.tmp.max_ver < TLS1_3_VERSION)) {
1338
0
        int compnum = sk_SSL_COMP_num(sctx->comp_methods);
1339
0
        for (i = 0; i < compnum; i++) {
1340
0
            comp = sk_SSL_COMP_value(sctx->comp_methods, i);
1341
0
            if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
1342
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1343
0
                return CON_FUNC_ERROR;
1344
0
            }
1345
0
        }
1346
0
    }
1347
100k
#endif
1348
    /* Add the NULL method */
1349
100k
    if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
1350
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1351
0
        return CON_FUNC_ERROR;
1352
0
    }
1353
1354
    /* TLS extensions */
1355
100k
    if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) {
1356
        /* SSLfatal() already called */
1357
0
        return CON_FUNC_ERROR;
1358
0
    }
1359
1360
100k
    return CON_FUNC_SUCCESS;
1361
100k
}
1362
1363
MSG_PROCESS_RETURN dtls_process_hello_verify(SSL_CONNECTION *s, PACKET *pkt)
1364
3.60k
{
1365
3.60k
    size_t cookie_len;
1366
3.60k
    PACKET cookiepkt;
1367
1368
3.60k
    if (!PACKET_forward(pkt, 2)
1369
3.58k
        || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1370
41
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1371
41
        return MSG_PROCESS_ERROR;
1372
41
    }
1373
1374
3.55k
    cookie_len = PACKET_remaining(&cookiepkt);
1375
3.55k
    if (cookie_len > sizeof(s->d1->cookie)) {
1376
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG);
1377
0
        return MSG_PROCESS_ERROR;
1378
0
    }
1379
1380
3.55k
    if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1381
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1382
0
        return MSG_PROCESS_ERROR;
1383
0
    }
1384
3.55k
    s->d1->cookie_len = cookie_len;
1385
1386
3.55k
    return MSG_PROCESS_FINISHED_READING;
1387
3.55k
}
1388
1389
static int set_client_ciphersuite(SSL_CONNECTION *s,
1390
    const unsigned char *cipherchars)
1391
74.5k
{
1392
74.5k
    STACK_OF(SSL_CIPHER) *sk;
1393
74.5k
    const SSL_CIPHER *c;
1394
74.5k
    int i;
1395
74.5k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1396
1397
74.5k
    c = ssl_get_cipher_by_char(s, cipherchars, 0);
1398
74.5k
    if (c == NULL) {
1399
        /* unknown cipher */
1400
269
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED);
1401
269
        return 0;
1402
269
    }
1403
    /*
1404
     * If it is a disabled cipher we either didn't send it in client hello,
1405
     * or it's not allowed for the selected protocol. So we return an error.
1406
     */
1407
74.3k
    if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) {
1408
140
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1409
140
        return 0;
1410
140
    }
1411
1412
74.1k
    sk = ssl_get_ciphers_by_id(s);
1413
74.1k
    i = sk_SSL_CIPHER_find(sk, c);
1414
74.1k
    if (i < 0) {
1415
        /* we did not say we would use this cipher */
1416
33
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1417
33
        return 0;
1418
33
    }
1419
1420
74.1k
    if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL
1421
72
        && s->s3.tmp.new_cipher->id != c->id) {
1422
        /* ServerHello selected a different ciphersuite to that in the HRR */
1423
6
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1424
6
        return 0;
1425
6
    }
1426
1427
    /*
1428
     * Depending on the session caching (internal/external), the cipher
1429
     * and/or cipher_id values may not be set. Make sure that cipher_id is
1430
     * set and use it for comparison.
1431
     */
1432
74.1k
    if (s->session->cipher != NULL)
1433
25
        s->session->cipher_id = s->session->cipher->id;
1434
74.1k
    if (s->hit && (s->session->cipher_id != c->id)) {
1435
5
        if (SSL_CONNECTION_IS_TLS13(s)) {
1436
0
            const EVP_MD *md = ssl_md(sctx, c->algorithm2);
1437
1438
0
            if (!ossl_assert(s->session->cipher != NULL)) {
1439
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1440
0
                return 0;
1441
0
            }
1442
            /*
1443
             * In TLSv1.3 it is valid for the server to select a different
1444
             * ciphersuite as long as the hash is the same.
1445
             */
1446
0
            if (md == NULL
1447
0
                || md != ssl_md(sctx, s->session->cipher->algorithm2)) {
1448
0
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1449
0
                    SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED);
1450
0
                return 0;
1451
0
            }
1452
5
        } else {
1453
            /*
1454
             * Prior to TLSv1.3 resuming a session always meant using the same
1455
             * ciphersuite.
1456
             */
1457
5
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1458
5
                SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1459
5
            return 0;
1460
5
        }
1461
5
    }
1462
74.1k
    s->s3.tmp.new_cipher = c;
1463
1464
74.1k
    return 1;
1465
74.1k
}
1466
1467
MSG_PROCESS_RETURN tls_process_server_hello(SSL_CONNECTION *s, PACKET *pkt)
1468
29.9k
{
1469
29.9k
    PACKET session_id, extpkt;
1470
29.9k
    size_t session_id_len;
1471
29.9k
    const unsigned char *cipherchars;
1472
29.9k
    int hrr = 0;
1473
29.9k
    unsigned int compression;
1474
29.9k
    unsigned int sversion;
1475
29.9k
    unsigned int context;
1476
29.9k
    RAW_EXTENSION *extensions = NULL;
1477
29.9k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1478
29.9k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1479
29.9k
#ifndef OPENSSL_NO_COMP
1480
29.9k
    SSL_COMP *comp;
1481
29.9k
#endif
1482
1483
29.9k
    if (!PACKET_get_net_2(pkt, &sversion)) {
1484
29
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1485
29
        goto err;
1486
29
    }
1487
1488
    /* load the server random */
1489
29.8k
    if (s->version == TLS1_3_VERSION
1490
22.6k
        && sversion == TLS1_2_VERSION
1491
17.3k
        && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE
1492
17.3k
        && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) {
1493
308
        if (s->hello_retry_request != SSL_HRR_NONE) {
1494
3
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
1495
3
            goto err;
1496
3
        }
1497
305
        s->hello_retry_request = SSL_HRR_PENDING;
1498
        /* Tell the record layer that we know we're going to get TLSv1.3 */
1499
305
        if (!ssl_set_record_protocol_version(s, s->version)) {
1500
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1501
0
            goto err;
1502
0
        }
1503
305
        hrr = 1;
1504
305
        if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) {
1505
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1506
0
            goto err;
1507
0
        }
1508
29.5k
    } else {
1509
29.5k
        if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) {
1510
65
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1511
65
            goto err;
1512
65
        }
1513
29.5k
    }
1514
1515
    /* Get the session-id. */
1516
29.8k
    if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1517
161
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1518
161
        goto err;
1519
161
    }
1520
29.6k
    session_id_len = PACKET_remaining(&session_id);
1521
29.6k
    if (session_id_len > sizeof(s->session->session_id)
1522
29.6k
        || session_id_len > SSL3_SESSION_ID_SIZE) {
1523
21
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1524
21
        goto err;
1525
21
    }
1526
1527
29.6k
    if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1528
20
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1529
20
        goto err;
1530
20
    }
1531
1532
29.6k
    if (!PACKET_get_1(pkt, &compression)) {
1533
9
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1534
9
        goto err;
1535
9
    }
1536
1537
    /* TLS extensions */
1538
29.5k
    if (PACKET_remaining(pkt) == 0 && !hrr) {
1539
295
        PACKET_null_init(&extpkt);
1540
29.2k
    } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
1541
29.0k
        || PACKET_remaining(pkt) != 0) {
1542
233
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1543
233
        goto err;
1544
233
    }
1545
1546
29.3k
    if (!hrr) {
1547
29.0k
        if (!tls_collect_extensions(s, &extpkt,
1548
29.0k
                SSL_EXT_TLS1_2_SERVER_HELLO
1549
29.0k
                    | SSL_EXT_TLS1_3_SERVER_HELLO,
1550
29.0k
                &extensions, NULL, 1)) {
1551
            /* SSLfatal() already called */
1552
213
            goto err;
1553
213
        }
1554
1555
28.8k
        if (!ssl_choose_client_version(s, sversion, extensions)) {
1556
            /* SSLfatal() already called */
1557
333
            goto err;
1558
333
        }
1559
28.8k
    }
1560
1561
28.8k
    if (SSL_CONNECTION_IS_TLS13(s) || hrr) {
1562
10.1k
        if (compression != 0) {
1563
8
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1564
8
                SSL_R_INVALID_COMPRESSION_ALGORITHM);
1565
8
            goto err;
1566
8
        }
1567
1568
10.1k
        if (session_id_len != s->tmp_session_id_len
1569
10.1k
            || memcmp(PACKET_data(&session_id), s->tmp_session_id,
1570
10.1k
                   session_id_len)
1571
10.1k
                != 0) {
1572
26
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID);
1573
26
            goto err;
1574
26
        }
1575
10.1k
    }
1576
1577
28.7k
    if (hrr) {
1578
284
        if (!set_client_ciphersuite(s, cipherchars)) {
1579
            /* SSLfatal() already called */
1580
16
            goto err;
1581
16
        }
1582
1583
268
        return tls_process_as_hello_retry_request(s, &extpkt);
1584
284
    }
1585
1586
    /*
1587
     * Now we have chosen the version we need to check again that the extensions
1588
     * are appropriate for this version.
1589
     */
1590
28.4k
    context = SSL_CONNECTION_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO
1591
28.4k
                                         : SSL_EXT_TLS1_2_SERVER_HELLO;
1592
28.4k
    if (!tls_validate_all_contexts(s, context, extensions)) {
1593
16
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1594
16
        goto err;
1595
16
    }
1596
1597
28.4k
    s->hit = 0;
1598
1599
28.4k
    if (SSL_CONNECTION_IS_TLS13(s)) {
1600
        /*
1601
         * In TLSv1.3 a ServerHello message signals a key change so the end of
1602
         * the message must be on a record boundary.
1603
         */
1604
9.78k
        if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1605
5
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1606
5
                SSL_R_NOT_ON_RECORD_BOUNDARY);
1607
5
            goto err;
1608
5
        }
1609
1610
        /* This will set s->hit if we are resuming */
1611
9.78k
        if (!tls_parse_extension(s, TLSEXT_IDX_psk,
1612
9.78k
                SSL_EXT_TLS1_3_SERVER_HELLO,
1613
9.78k
                extensions, NULL, 0)) {
1614
            /* SSLfatal() already called */
1615
0
            goto err;
1616
0
        }
1617
18.6k
    } else {
1618
        /*
1619
         * Check if we can resume the session based on external pre-shared
1620
         * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
1621
         * Resumption based on server-side state works with session IDs.
1622
         * Resumption based on pre-shared Protected Access Credentials (PACs)
1623
         * works by overriding the SessionTicket extension at the application
1624
         * layer, and does not send a session ID. (We do not know whether
1625
         * EAP-FAST servers would honour the session ID.) Therefore, the session
1626
         * ID alone is not a reliable indicator of session resumption, so we
1627
         * first check if we can resume, and later peek at the next handshake
1628
         * message to see if the server wants to resume.
1629
         */
1630
18.6k
        if (s->version >= TLS1_VERSION
1631
18.6k
            && s->ext.session_secret_cb != NULL && s->session->ext.tick) {
1632
0
            const SSL_CIPHER *pref_cipher = NULL;
1633
            /*
1634
             * s->session->master_key_length is a size_t, but this is an int for
1635
             * backwards compat reasons
1636
             */
1637
0
            int master_key_length;
1638
1639
0
            master_key_length = sizeof(s->session->master_key);
1640
0
            if (s->ext.session_secret_cb(ussl, s->session->master_key,
1641
0
                    &master_key_length,
1642
0
                    NULL, &pref_cipher,
1643
0
                    s->ext.session_secret_cb_arg)
1644
0
                && master_key_length > 0) {
1645
0
                s->session->master_key_length = master_key_length;
1646
0
                s->session->cipher = pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0);
1647
0
            } else {
1648
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1649
0
                goto err;
1650
0
            }
1651
0
        }
1652
1653
18.6k
        if (session_id_len != 0
1654
9.66k
            && session_id_len == s->session->session_id_length
1655
17
            && memcmp(PACKET_data(&session_id), s->session->session_id,
1656
17
                   session_id_len)
1657
17
                == 0)
1658
11
            s->hit = 1;
1659
18.6k
    }
1660
1661
28.4k
    if (s->hit) {
1662
11
        if (s->sid_ctx_length != s->session->sid_ctx_length
1663
11
            || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1664
            /* actually a client application bug */
1665
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1666
0
                SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1667
0
            goto err;
1668
0
        }
1669
28.4k
    } else {
1670
        /*
1671
         * If we were trying for session-id reuse but the server
1672
         * didn't resume, make a new SSL_SESSION.
1673
         * In the case of EAP-FAST and PAC, we do not send a session ID,
1674
         * so the PAC-based session secret is always preserved. It'll be
1675
         * overwritten if the server refuses resumption.
1676
         */
1677
28.4k
        if (s->session->session_id_length > 0) {
1678
8
            ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
1679
8
            if (!ssl_get_new_session(s, 0)) {
1680
                /* SSLfatal() already called */
1681
0
                goto err;
1682
0
            }
1683
8
        }
1684
1685
28.4k
        s->session->ssl_version = s->version;
1686
        /*
1687
         * In TLSv1.2 and below we save the session id we were sent so we can
1688
         * resume it later. In TLSv1.3 the session id we were sent is just an
1689
         * echo of what we originally sent in the ClientHello and should not be
1690
         * used for resumption.
1691
         */
1692
28.4k
        if (!SSL_CONNECTION_IS_TLS13(s)) {
1693
18.6k
            s->session->session_id_length = session_id_len;
1694
            /* session_id_len could be 0 */
1695
18.6k
            if (session_id_len > 0)
1696
9.65k
                memcpy(s->session->session_id, PACKET_data(&session_id),
1697
9.65k
                    session_id_len);
1698
18.6k
        }
1699
28.4k
    }
1700
1701
    /* Session version and negotiated protocol version should match */
1702
28.4k
    if (s->version != s->session->ssl_version) {
1703
0
        SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1704
0
            SSL_R_SSL_SESSION_VERSION_MISMATCH);
1705
0
        goto err;
1706
0
    }
1707
    /*
1708
     * Now that we know the version, update the check to see if it's an allowed
1709
     * version.
1710
     */
1711
28.4k
    s->s3.tmp.min_ver = s->version;
1712
28.4k
    s->s3.tmp.max_ver = s->version;
1713
1714
28.4k
    if (!set_client_ciphersuite(s, cipherchars)) {
1715
        /* SSLfatal() already called */
1716
170
        goto err;
1717
170
    }
1718
1719
#ifdef OPENSSL_NO_COMP
1720
    if (compression != 0) {
1721
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1722
            SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1723
        goto err;
1724
    }
1725
    /*
1726
     * If compression is disabled we'd better not try to resume a session
1727
     * using compression.
1728
     */
1729
    if (s->session->compress_meth != 0) {
1730
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
1731
        goto err;
1732
    }
1733
#else
1734
28.3k
    if (s->hit && compression != s->session->compress_meth) {
1735
5
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1736
5
            SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1737
5
        goto err;
1738
5
    }
1739
28.3k
    if (compression == 0)
1740
28.2k
        comp = NULL;
1741
27
    else if (!ssl_allow_compression(s)) {
1742
27
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED);
1743
27
        goto err;
1744
27
    } else {
1745
0
        comp = ssl3_comp_find(SSL_CONNECTION_GET_CTX(s)->comp_methods,
1746
0
            compression);
1747
0
    }
1748
1749
28.2k
    if (compression != 0 && comp == NULL) {
1750
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1751
0
            SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1752
0
        goto err;
1753
28.2k
    } else {
1754
28.2k
        s->s3.tmp.new_compression = comp;
1755
28.2k
    }
1756
28.2k
#endif
1757
1758
28.2k
    if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) {
1759
        /* SSLfatal() already called */
1760
298
        goto err;
1761
298
    }
1762
1763
#ifndef OPENSSL_NO_SCTP
1764
    if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
1765
        unsigned char sctpauthkey[64];
1766
        char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1767
        size_t labellen;
1768
1769
        /*
1770
         * Add new shared key for SCTP-Auth, will be ignored if
1771
         * no SCTP used.
1772
         */
1773
        memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1774
            sizeof(DTLS1_SCTP_AUTH_LABEL));
1775
1776
        /* Don't include the terminating zero. */
1777
        labellen = sizeof(labelbuffer) - 1;
1778
        if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
1779
            labellen += 1;
1780
1781
        if (SSL_export_keying_material(ssl, sctpauthkey,
1782
                sizeof(sctpauthkey),
1783
                labelbuffer,
1784
                labellen, NULL, 0, 0)
1785
            <= 0) {
1786
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1787
            goto err;
1788
        }
1789
1790
        BIO_ctrl(SSL_get_wbio(ssl),
1791
            BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1792
            sizeof(sctpauthkey), sctpauthkey);
1793
    }
1794
#endif
1795
1796
    /*
1797
     * In TLSv1.3 we have some post-processing to change cipher state, otherwise
1798
     * we're done with this message
1799
     */
1800
27.9k
    if (SSL_CONNECTION_IS_TLS13(s)) {
1801
9.61k
        if (!ssl->method->ssl3_enc->setup_key_block(s)
1802
9.61k
            || !tls13_store_handshake_traffic_hash(s)) {
1803
            /* SSLfatal() already called */
1804
0
            goto err;
1805
0
        }
1806
        /*
1807
         * If we're not doing early-data and we're not going to send a dummy CCS
1808
         * (i.e. no middlebox compat mode) then we can change the write keys
1809
         * immediately. Otherwise we have to defer this until after all possible
1810
         * early data is written. We could just always defer until the last
1811
         * moment except QUIC needs it done at the same time as the read keys
1812
         * are changed. Since QUIC doesn't do TLS early data or need middlebox
1813
         * compat this doesn't cause a problem.
1814
         */
1815
9.61k
        if (SSL_IS_QUIC_HANDSHAKE(s)
1816
226
            || (s->early_data_state == SSL_EARLY_DATA_NONE
1817
9.38k
                && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0)) {
1818
9.38k
            if (!ssl->method->ssl3_enc->change_cipher_state(s,
1819
9.38k
                    SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
1820
                /* SSLfatal() already called */
1821
0
                goto err;
1822
0
            }
1823
9.38k
        }
1824
9.61k
        if (!ssl->method->ssl3_enc->change_cipher_state(s,
1825
9.61k
                SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
1826
            /* SSLfatal() already called */
1827
11
            goto err;
1828
11
        }
1829
9.61k
    }
1830
1831
27.9k
    OPENSSL_free(extensions);
1832
27.9k
    return MSG_PROCESS_CONTINUE_READING;
1833
1.66k
err:
1834
1.66k
    OPENSSL_free(extensions);
1835
1.66k
    return MSG_PROCESS_ERROR;
1836
27.9k
}
1837
1838
static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
1839
    PACKET *extpkt)
1840
601
{
1841
601
    RAW_EXTENSION *extensions = NULL;
1842
1843
    /*
1844
     * If we were sending early_data then any alerts should not be sent using
1845
     * the old wrlmethod.
1846
     */
1847
601
    if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
1848
0
        && !ssl_set_new_record_layer(s,
1849
0
            TLS_ANY_VERSION,
1850
0
            OSSL_RECORD_DIRECTION_WRITE,
1851
0
            OSSL_RECORD_PROTECTION_LEVEL_NONE,
1852
0
            NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1853
0
            NULL, 0, NID_undef, NULL, NULL, NULL)) {
1854
        /* SSLfatal already called */
1855
0
        goto err;
1856
0
    }
1857
    /* We are definitely going to be using TLSv1.3 */
1858
601
    s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, TLS1_3_VERSION);
1859
1860
601
    if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1861
601
            &extensions, NULL, 1)
1862
567
        || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1863
567
            extensions, NULL, 0, 1)) {
1864
        /* SSLfatal() already called */
1865
252
        goto err;
1866
252
    }
1867
1868
349
    OPENSSL_free(extensions);
1869
349
    extensions = NULL;
1870
1871
349
    if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) {
1872
        /*
1873
         * We didn't receive a cookie or a new key_share so the next
1874
         * ClientHello will not change
1875
         */
1876
10
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR);
1877
10
        goto err;
1878
10
    }
1879
1880
    /*
1881
     * Re-initialise the Transcript Hash. We're going to prepopulate it with
1882
     * a synthetic message_hash in place of ClientHello1.
1883
     */
1884
339
    if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
1885
        /* SSLfatal() already called */
1886
0
        goto err;
1887
0
    }
1888
1889
    /*
1890
     * Add this message to the Transcript Hash. Normally this is done
1891
     * automatically prior to the message processing stage. However due to the
1892
     * need to create the synthetic message hash, we defer that step until now
1893
     * for HRR messages.
1894
     */
1895
339
    if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1896
339
            s->init_num + SSL3_HM_HEADER_LENGTH)) {
1897
        /* SSLfatal() already called */
1898
0
        goto err;
1899
0
    }
1900
1901
339
    return MSG_PROCESS_FINISHED_READING;
1902
262
err:
1903
262
    OPENSSL_free(extensions);
1904
262
    return MSG_PROCESS_ERROR;
1905
339
}
1906
1907
MSG_PROCESS_RETURN tls_process_server_rpk(SSL_CONNECTION *sc, PACKET *pkt)
1908
0
{
1909
0
    EVP_PKEY *peer_rpk = NULL;
1910
1911
0
    if (!tls_process_rpk(sc, pkt, &peer_rpk)) {
1912
        /* SSLfatal() already called */
1913
0
        return MSG_PROCESS_ERROR;
1914
0
    }
1915
1916
0
    if (peer_rpk == NULL) {
1917
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_CERTIFICATE);
1918
0
        return MSG_PROCESS_ERROR;
1919
0
    }
1920
1921
0
    EVP_PKEY_free(sc->session->peer_rpk);
1922
0
    sc->session->peer_rpk = peer_rpk;
1923
1924
0
    return MSG_PROCESS_CONTINUE_PROCESSING;
1925
0
}
1926
1927
static WORK_STATE tls_post_process_server_rpk(SSL_CONNECTION *sc,
1928
    WORK_STATE wst)
1929
0
{
1930
0
    size_t certidx;
1931
0
    const SSL_CERT_LOOKUP *clu;
1932
0
    int v_ok;
1933
1934
0
    if (sc->session->peer_rpk == NULL) {
1935
0
        SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER,
1936
0
            SSL_R_INVALID_RAW_PUBLIC_KEY);
1937
0
        return WORK_ERROR;
1938
0
    }
1939
1940
0
    if (sc->rwstate == SSL_RETRY_VERIFY)
1941
0
        sc->rwstate = SSL_NOTHING;
1942
1943
0
    ERR_set_mark();
1944
0
    v_ok = ssl_verify_rpk(sc, sc->session->peer_rpk);
1945
0
    if (v_ok <= 0 && sc->verify_mode != SSL_VERIFY_NONE) {
1946
0
        ERR_clear_last_mark();
1947
0
        SSLfatal(sc, ssl_x509err2alert(sc->verify_result),
1948
0
            SSL_R_CERTIFICATE_VERIFY_FAILED);
1949
0
        return WORK_ERROR;
1950
0
    }
1951
0
    ERR_pop_to_mark(); /* but we keep s->verify_result */
1952
0
    if (v_ok > 0 && sc->rwstate == SSL_RETRY_VERIFY) {
1953
0
        return WORK_MORE_A;
1954
0
    }
1955
1956
0
    if ((clu = ssl_cert_lookup_by_pkey(sc->session->peer_rpk, &certidx,
1957
0
             SSL_CONNECTION_GET_CTX(sc)))
1958
0
        == NULL) {
1959
0
        SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1960
0
        return WORK_ERROR;
1961
0
    }
1962
1963
    /*
1964
     * Check certificate type is consistent with ciphersuite. For TLS 1.3
1965
     * skip check since TLS 1.3 ciphersuites can be used with any certificate
1966
     * type.
1967
     */
1968
0
    if (!SSL_CONNECTION_IS_TLS13(sc)) {
1969
0
        if ((clu->amask & sc->s3.tmp.new_cipher->algorithm_auth) == 0) {
1970
0
            SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_RPK_TYPE);
1971
0
            return WORK_ERROR;
1972
0
        }
1973
0
    }
1974
1975
    /* Ensure there is no peer/peer_chain */
1976
0
    X509_free(sc->session->peer);
1977
0
    sc->session->peer = NULL;
1978
0
    sk_X509_pop_free(sc->session->peer_chain, X509_free);
1979
0
    sc->session->peer_chain = NULL;
1980
0
    sc->session->verify_result = sc->verify_result;
1981
1982
    /* Save the current hash state for when we receive the CertificateVerify */
1983
0
    if (SSL_CONNECTION_IS_TLS13(sc)
1984
0
        && !ssl_handshake_hash(sc, sc->cert_verify_hash,
1985
0
            sizeof(sc->cert_verify_hash),
1986
0
            &sc->cert_verify_hash_len)) {
1987
        /* SSLfatal() already called */
1988
0
        return WORK_ERROR;
1989
0
    }
1990
1991
0
    return WORK_FINISHED_CONTINUE;
1992
0
}
1993
1994
/* prepare server cert verification by setting s->session->peer_chain from pkt */
1995
MSG_PROCESS_RETURN tls_process_server_certificate(SSL_CONNECTION *s,
1996
    PACKET *pkt)
1997
54.7k
{
1998
54.7k
    unsigned long cert_list_len, cert_len;
1999
54.7k
    X509 *x = NULL;
2000
54.7k
    const unsigned char *certstart, *certbytes;
2001
54.7k
    size_t chainidx;
2002
54.7k
    unsigned int context = 0;
2003
54.7k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2004
2005
54.7k
    if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
2006
0
        return tls_process_server_rpk(s, pkt);
2007
54.7k
    if (s->ext.server_cert_type != TLSEXT_cert_type_x509) {
2008
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,
2009
0
            SSL_R_UNKNOWN_CERTIFICATE_TYPE);
2010
0
        goto err;
2011
0
    }
2012
2013
54.7k
    if ((s->session->peer_chain = sk_X509_new_null()) == NULL) {
2014
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2015
0
        goto err;
2016
0
    }
2017
2018
54.7k
    if ((SSL_CONNECTION_IS_TLS13(s) && !PACKET_get_1(pkt, &context))
2019
54.7k
        || context != 0
2020
54.7k
        || !PACKET_get_net_3(pkt, &cert_list_len)
2021
54.7k
        || PACKET_remaining(pkt) != cert_list_len
2022
54.3k
        || PACKET_remaining(pkt) == 0) {
2023
405
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2024
405
        goto err;
2025
405
    }
2026
99.3k
    for (chainidx = 0; PACKET_remaining(pkt); chainidx++) {
2027
63.2k
        if (!PACKET_get_net_3(pkt, &cert_len)
2028
63.2k
            || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
2029
241
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
2030
241
            goto err;
2031
241
        }
2032
2033
63.0k
        certstart = certbytes;
2034
63.0k
        x = X509_new_ex(sctx->libctx, sctx->propq);
2035
63.0k
        if (x == NULL) {
2036
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
2037
0
            goto err;
2038
0
        }
2039
63.0k
        if (d2i_X509(&x, (const unsigned char **)&certbytes,
2040
63.0k
                cert_len)
2041
63.0k
            == NULL) {
2042
17.9k
            SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB);
2043
17.9k
            goto err;
2044
17.9k
        }
2045
2046
45.1k
        if (certbytes != (certstart + cert_len)) {
2047
32
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
2048
32
            goto err;
2049
32
        }
2050
2051
45.0k
        if (SSL_CONNECTION_IS_TLS13(s)) {
2052
17.0k
            RAW_EXTENSION *rawexts = NULL;
2053
17.0k
            PACKET extensions;
2054
2055
17.0k
            if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
2056
38
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
2057
38
                goto err;
2058
38
            }
2059
17.0k
            if (!tls_collect_extensions(s, &extensions,
2060
17.0k
                    SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
2061
17.0k
                    NULL, chainidx == 0)
2062
17.0k
                || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
2063
17.0k
                    rawexts, x, chainidx,
2064
17.0k
                    PACKET_remaining(pkt) == 0)) {
2065
5
                OPENSSL_free(rawexts);
2066
                /* SSLfatal already called */
2067
5
                goto err;
2068
5
            }
2069
17.0k
            OPENSSL_free(rawexts);
2070
17.0k
        }
2071
2072
45.0k
        if (!sk_X509_push(s->session->peer_chain, x)) {
2073
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2074
0
            goto err;
2075
0
        }
2076
45.0k
        x = NULL;
2077
45.0k
    }
2078
36.1k
    return MSG_PROCESS_CONTINUE_PROCESSING;
2079
2080
18.6k
err:
2081
18.6k
    X509_free(x);
2082
18.6k
    OSSL_STACK_OF_X509_free(s->session->peer_chain);
2083
18.6k
    s->session->peer_chain = NULL;
2084
18.6k
    return MSG_PROCESS_ERROR;
2085
54.3k
}
2086
2087
/*
2088
 * Verify the s->session->peer_chain and check server cert type.
2089
 * On success set s->session->peer and s->session->verify_result.
2090
 * Else the peer certificate verification callback may request retry.
2091
 */
2092
WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s,
2093
    WORK_STATE wst)
2094
20.2k
{
2095
20.2k
    X509 *x;
2096
20.2k
    EVP_PKEY *pkey = NULL;
2097
20.2k
    const SSL_CERT_LOOKUP *clu;
2098
20.2k
    size_t certidx;
2099
20.2k
    int i;
2100
2101
20.2k
    if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
2102
0
        return tls_post_process_server_rpk(s, wst);
2103
2104
20.2k
    if (s->rwstate == SSL_RETRY_VERIFY)
2105
0
        s->rwstate = SSL_NOTHING;
2106
2107
    /*
2108
     * The documented interface is that SSL_VERIFY_PEER should be set in order
2109
     * for client side verification of the server certificate to take place.
2110
     * However, historically the code has only checked that *any* flag is set
2111
     * to cause server verification to take place. Use of the other flags makes
2112
     * no sense in client mode. An attempt to clean up the semantics was
2113
     * reverted because at least one application *only* set
2114
     * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
2115
     * server verification to take place, after the clean up it silently did
2116
     * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
2117
     * sent to them because they are void functions. Therefore, we now use the
2118
     * (less clean) historic behaviour of performing validation if any flag is
2119
     * set. The *documented* interface remains the same.
2120
     */
2121
20.2k
    ERR_set_mark();
2122
20.2k
    i = ssl_verify_cert_chain(s, s->session->peer_chain);
2123
20.2k
    if (i <= 0 && s->verify_mode != SSL_VERIFY_NONE) {
2124
0
        ERR_clear_last_mark();
2125
0
        SSLfatal(s, ssl_x509err2alert(s->verify_result),
2126
0
            SSL_R_CERTIFICATE_VERIFY_FAILED);
2127
0
        return WORK_ERROR;
2128
0
    }
2129
20.2k
    ERR_pop_to_mark(); /* but we keep s->verify_result */
2130
20.2k
    if (i > 0 && s->rwstate == SSL_RETRY_VERIFY)
2131
0
        return WORK_MORE_A;
2132
2133
    /*
2134
     * Inconsistency alert: cert_chain does include the peer's certificate,
2135
     * which we don't include in statem_srvr.c
2136
     */
2137
20.2k
    x = sk_X509_value(s->session->peer_chain, 0);
2138
2139
20.2k
    pkey = X509_get0_pubkey(x);
2140
2141
20.2k
    if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
2142
1.01k
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2143
1.01k
            SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
2144
1.01k
        return WORK_ERROR;
2145
1.01k
    }
2146
2147
19.2k
    if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx,
2148
19.2k
             SSL_CONNECTION_GET_CTX(s)))
2149
19.2k
        == NULL) {
2150
33
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
2151
33
        return WORK_ERROR;
2152
33
    }
2153
    /*
2154
     * Check certificate type is consistent with ciphersuite. For TLS 1.3
2155
     * skip check since TLS 1.3 ciphersuites can be used with any certificate
2156
     * type.
2157
     */
2158
19.2k
    if (!SSL_CONNECTION_IS_TLS13(s)) {
2159
9.89k
        if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) {
2160
115
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE);
2161
115
            return WORK_ERROR;
2162
115
        }
2163
9.89k
    }
2164
2165
19.1k
    if (!X509_up_ref(x)) {
2166
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2167
0
        return WORK_ERROR;
2168
0
    }
2169
2170
19.1k
    X509_free(s->session->peer);
2171
19.1k
    s->session->peer = x;
2172
19.1k
    s->session->verify_result = s->verify_result;
2173
    /* Ensure there is no RPK */
2174
19.1k
    EVP_PKEY_free(s->session->peer_rpk);
2175
19.1k
    s->session->peer_rpk = NULL;
2176
2177
    /* Save the current hash state for when we receive the CertificateVerify */
2178
19.1k
    if (SSL_CONNECTION_IS_TLS13(s)
2179
9.35k
        && !ssl_handshake_hash(s, s->cert_verify_hash,
2180
9.35k
            sizeof(s->cert_verify_hash),
2181
9.35k
            &s->cert_verify_hash_len)) {
2182
0
        /* SSLfatal() already called */;
2183
0
        return WORK_ERROR;
2184
0
    }
2185
19.1k
    return WORK_FINISHED_CONTINUE;
2186
19.1k
}
2187
2188
#ifndef OPENSSL_NO_COMP_ALG
2189
MSG_PROCESS_RETURN tls_process_server_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)
2190
{
2191
    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
2192
    PACKET tmppkt;
2193
    BUF_MEM *buf = BUF_MEM_new();
2194
2195
    if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)
2196
        ret = tls_process_server_certificate(sc, &tmppkt);
2197
2198
    BUF_MEM_free(buf);
2199
    return ret;
2200
}
2201
#endif
2202
2203
static int tls_process_ske_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
2204
0
{
2205
0
#ifndef OPENSSL_NO_PSK
2206
0
    PACKET psk_identity_hint;
2207
2208
    /* PSK ciphersuites are preceded by an identity hint */
2209
2210
0
    if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
2211
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2212
0
        return 0;
2213
0
    }
2214
2215
    /*
2216
     * Store PSK identity hint for later use, hint is used in
2217
     * tls_construct_client_key_exchange.  Assume that the maximum length of
2218
     * a PSK identity hint can be as long as the maximum length of a PSK
2219
     * identity.
2220
     */
2221
0
    if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
2222
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG);
2223
0
        return 0;
2224
0
    }
2225
2226
0
    if (PACKET_remaining(&psk_identity_hint) == 0) {
2227
0
        OPENSSL_free(s->session->psk_identity_hint);
2228
0
        s->session->psk_identity_hint = NULL;
2229
0
    } else if (!PACKET_strndup(&psk_identity_hint,
2230
0
                   &s->session->psk_identity_hint)) {
2231
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2232
0
        return 0;
2233
0
    }
2234
2235
0
    return 1;
2236
#else
2237
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2238
    return 0;
2239
#endif
2240
0
}
2241
2242
static int tls_process_ske_srp(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2243
0
{
2244
0
#ifndef OPENSSL_NO_SRP
2245
0
    PACKET prime, generator, salt, server_pub;
2246
2247
0
    if (!PACKET_get_length_prefixed_2(pkt, &prime)
2248
0
        || !PACKET_get_length_prefixed_2(pkt, &generator)
2249
0
        || !PACKET_get_length_prefixed_1(pkt, &salt)
2250
0
        || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
2251
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2252
0
        return 0;
2253
0
    }
2254
2255
0
    if ((s->srp_ctx.N = BN_bin2bn(PACKET_data(&prime),
2256
0
             (int)PACKET_remaining(&prime), NULL))
2257
0
            == NULL
2258
0
        || (s->srp_ctx.g = BN_bin2bn(PACKET_data(&generator),
2259
0
                (int)PACKET_remaining(&generator), NULL))
2260
0
            == NULL
2261
0
        || (s->srp_ctx.s = BN_bin2bn(PACKET_data(&salt),
2262
0
                (int)PACKET_remaining(&salt), NULL))
2263
0
            == NULL
2264
0
        || (s->srp_ctx.B = BN_bin2bn(PACKET_data(&server_pub),
2265
0
                (int)PACKET_remaining(&server_pub), NULL))
2266
0
            == NULL) {
2267
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2268
0
        return 0;
2269
0
    }
2270
2271
0
    if (!srp_verify_server_param(s)) {
2272
        /* SSLfatal() already called */
2273
0
        return 0;
2274
0
    }
2275
2276
    /* We must check if there is a certificate */
2277
0
    if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2278
0
        *pkey = tls_get_peer_pkey(s);
2279
2280
0
    return 1;
2281
#else
2282
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2283
    return 0;
2284
#endif
2285
0
}
2286
2287
static int tls_process_ske_dhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2288
9.53k
{
2289
9.53k
    PACKET prime, generator, pub_key;
2290
9.53k
    EVP_PKEY *peer_tmp = NULL;
2291
9.53k
    BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
2292
9.53k
    EVP_PKEY_CTX *pctx = NULL;
2293
9.53k
    OSSL_PARAM *params = NULL;
2294
9.53k
    OSSL_PARAM_BLD *tmpl = NULL;
2295
9.53k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2296
9.53k
    int ret = 0;
2297
2298
9.53k
    if (!PACKET_get_length_prefixed_2(pkt, &prime)
2299
9.42k
        || !PACKET_get_length_prefixed_2(pkt, &generator)
2300
9.31k
        || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
2301
336
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2302
336
        return 0;
2303
336
    }
2304
2305
9.19k
    p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
2306
9.19k
    g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
2307
9.19k
        NULL);
2308
9.19k
    bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
2309
9.19k
        (int)PACKET_remaining(&pub_key), NULL);
2310
9.19k
    if (p == NULL || g == NULL || bnpub_key == NULL) {
2311
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2312
0
        goto err;
2313
0
    }
2314
2315
9.19k
    tmpl = OSSL_PARAM_BLD_new();
2316
9.19k
    if (tmpl == NULL
2317
9.19k
        || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
2318
9.19k
        || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)
2319
9.19k
        || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
2320
9.19k
            bnpub_key)
2321
9.19k
        || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
2322
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2323
0
        goto err;
2324
0
    }
2325
2326
9.19k
    pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq);
2327
9.19k
    if (pctx == NULL) {
2328
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2329
0
        goto err;
2330
0
    }
2331
9.19k
    if (EVP_PKEY_fromdata_init(pctx) <= 0
2332
9.19k
        || EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) {
2333
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE);
2334
0
        goto err;
2335
0
    }
2336
2337
9.19k
    EVP_PKEY_CTX_free(pctx);
2338
9.19k
    pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, peer_tmp, sctx->propq);
2339
9.19k
    if (pctx == NULL
2340
        /*
2341
         * EVP_PKEY_param_check() will verify that the DH params are using
2342
         * a safe prime. In this context, because we're using ephemeral DH,
2343
         * we're ok with it not being a safe prime.
2344
         * EVP_PKEY_param_check_quick() skips the safe prime check.
2345
         */
2346
9.19k
        || EVP_PKEY_param_check_quick(pctx) != 1
2347
6.85k
        || EVP_PKEY_public_check(pctx) != 1) {
2348
2.72k
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE);
2349
2.72k
        goto err;
2350
2.72k
    }
2351
2352
6.47k
    if (!ssl_security(s, SSL_SECOP_TMP_DH,
2353
6.47k
            EVP_PKEY_get_security_bits(peer_tmp),
2354
6.47k
            0, peer_tmp)) {
2355
14
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
2356
14
        goto err;
2357
14
    }
2358
2359
6.46k
    s->s3.peer_tmp = peer_tmp;
2360
6.46k
    peer_tmp = NULL;
2361
2362
    /*
2363
     * FIXME: This makes assumptions about which ciphersuites come with
2364
     * public keys. We should have a less ad-hoc way of doing this
2365
     */
2366
6.46k
    if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2367
2.77k
        *pkey = tls_get_peer_pkey(s);
2368
    /* else anonymous DH, so no certificate or pkey. */
2369
2370
6.46k
    ret = 1;
2371
2372
9.19k
err:
2373
9.19k
    OSSL_PARAM_BLD_free(tmpl);
2374
9.19k
    OSSL_PARAM_free(params);
2375
9.19k
    EVP_PKEY_free(peer_tmp);
2376
9.19k
    EVP_PKEY_CTX_free(pctx);
2377
9.19k
    BN_free(p);
2378
9.19k
    BN_free(g);
2379
9.19k
    BN_free(bnpub_key);
2380
2381
9.19k
    return ret;
2382
6.46k
}
2383
2384
static int tls_process_ske_ecdhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2385
9.18k
{
2386
9.18k
    PACKET encoded_pt;
2387
9.18k
    unsigned int curve_type, curve_id;
2388
2389
    /*
2390
     * Extract elliptic curve parameters and the server's ephemeral ECDH
2391
     * public key. We only support named (not generic) curves and
2392
     * ECParameters in this case is just three bytes.
2393
     */
2394
9.18k
    if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
2395
22
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2396
22
        return 0;
2397
22
    }
2398
    /*
2399
     * Check curve is named curve type and one of our preferences, if not
2400
     * server has sent an invalid curve.
2401
     */
2402
9.16k
    if (curve_type != NAMED_CURVE_TYPE
2403
9.13k
        || !tls1_check_group_id(s, curve_id, 1)) {
2404
203
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
2405
203
        return 0;
2406
203
    }
2407
2408
8.95k
    if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) {
2409
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2410
0
            SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
2411
0
        return 0;
2412
0
    }
2413
2414
8.95k
    if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
2415
68
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2416
68
        return 0;
2417
68
    }
2418
2419
8.89k
    if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp,
2420
8.89k
            PACKET_data(&encoded_pt),
2421
8.89k
            PACKET_remaining(&encoded_pt))
2422
8.89k
        <= 0) {
2423
1.12k
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
2424
1.12k
        return 0;
2425
1.12k
    }
2426
2427
    /*
2428
     * The ECC/TLS specification does not mention the use of DSA to sign
2429
     * ECParameters in the server key exchange message. We do support RSA
2430
     * and ECDSA.
2431
     */
2432
7.76k
    if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA)
2433
1.33k
        *pkey = tls_get_peer_pkey(s);
2434
6.43k
    else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA)
2435
1.11k
        *pkey = tls_get_peer_pkey(s);
2436
    /* else anonymous ECDH, so no certificate or pkey. */
2437
2438
    /* Cache the agreed upon group in the SSL_SESSION */
2439
7.76k
    s->session->kex_group = curve_id;
2440
7.76k
    return 1;
2441
8.89k
}
2442
2443
MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt)
2444
18.7k
{
2445
18.7k
    long alg_k;
2446
18.7k
    EVP_PKEY *pkey = NULL;
2447
18.7k
    EVP_MD_CTX *md_ctx = NULL;
2448
18.7k
    EVP_PKEY_CTX *pctx = NULL;
2449
18.7k
    PACKET save_param_start, signature;
2450
18.7k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2451
2452
18.7k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
2453
2454
18.7k
    save_param_start = *pkt;
2455
2456
18.7k
    EVP_PKEY_free(s->s3.peer_tmp);
2457
18.7k
    s->s3.peer_tmp = NULL;
2458
2459
18.7k
    if (alg_k & SSL_PSK) {
2460
0
        if (!tls_process_ske_psk_preamble(s, pkt)) {
2461
            /* SSLfatal() already called */
2462
0
            goto err;
2463
0
        }
2464
0
    }
2465
2466
    /* Nothing else to do for plain PSK or RSAPSK */
2467
18.7k
    if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
2468
18.7k
    } else if (alg_k & SSL_kSRP) {
2469
0
        if (!tls_process_ske_srp(s, pkt, &pkey)) {
2470
            /* SSLfatal() already called */
2471
0
            goto err;
2472
0
        }
2473
18.7k
    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2474
9.53k
        if (!tls_process_ske_dhe(s, pkt, &pkey)) {
2475
            /* SSLfatal() already called */
2476
3.07k
            goto err;
2477
3.07k
        }
2478
9.53k
    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2479
9.18k
        if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
2480
            /* SSLfatal() already called */
2481
1.41k
            goto err;
2482
1.41k
        }
2483
9.18k
    } else if (alg_k) {
2484
0
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
2485
0
        goto err;
2486
0
    }
2487
2488
    /* if it was signed, check the signature */
2489
14.2k
    if (pkey != NULL) {
2490
5.22k
        PACKET params;
2491
5.22k
        const EVP_MD *md = NULL;
2492
5.22k
        unsigned char *tbs;
2493
5.22k
        size_t tbslen;
2494
5.22k
        int rv;
2495
2496
        /*
2497
         * |pkt| now points to the beginning of the signature, so the difference
2498
         * equals the length of the parameters.
2499
         */
2500
5.22k
        if (!PACKET_get_sub_packet(&save_param_start, &params,
2501
5.22k
                PACKET_remaining(&save_param_start) - PACKET_remaining(pkt))) {
2502
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
2503
0
            goto err;
2504
0
        }
2505
2506
5.22k
        if (SSL_USE_SIGALGS(s)) {
2507
3.41k
            unsigned int sigalg;
2508
2509
3.41k
            if (!PACKET_get_net_2(pkt, &sigalg)) {
2510
26
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2511
26
                goto err;
2512
26
            }
2513
3.39k
            if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
2514
                /* SSLfatal() already called */
2515
204
                goto err;
2516
204
            }
2517
3.39k
        } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
2518
10
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2519
10
                SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED);
2520
10
            goto err;
2521
10
        }
2522
2523
4.98k
        if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) {
2524
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2525
0
                SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
2526
0
            goto err;
2527
0
        }
2528
4.98k
        if (SSL_USE_SIGALGS(s))
2529
4.98k
            OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
2530
4.98k
                md == NULL ? "n/a" : EVP_MD_get0_name(md));
2531
2532
4.98k
        if (!PACKET_get_length_prefixed_2(pkt, &signature)
2533
4.83k
            || PACKET_remaining(pkt) != 0) {
2534
206
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2535
206
            goto err;
2536
206
        }
2537
2538
4.77k
        md_ctx = EVP_MD_CTX_new();
2539
4.77k
        if (md_ctx == NULL) {
2540
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2541
0
            goto err;
2542
0
        }
2543
2544
4.77k
        if (EVP_DigestVerifyInit_ex(md_ctx, &pctx,
2545
4.77k
                md == NULL ? NULL : EVP_MD_get0_name(md),
2546
4.77k
                sctx->libctx, sctx->propq, pkey,
2547
4.77k
                NULL)
2548
4.77k
            <= 0) {
2549
43
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2550
43
            goto err;
2551
43
        }
2552
4.73k
        if (SSL_USE_PSS(s)) {
2553
675
            if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2554
675
                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
2555
675
                       RSA_PSS_SALTLEN_DIGEST)
2556
675
                    <= 0) {
2557
4
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2558
4
                goto err;
2559
4
            }
2560
675
        }
2561
4.72k
        tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(&params),
2562
4.72k
            PACKET_remaining(&params));
2563
4.72k
        if (tbslen == 0) {
2564
            /* SSLfatal() already called */
2565
0
            goto err;
2566
0
        }
2567
2568
4.72k
        rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
2569
4.72k
            PACKET_remaining(&signature), tbs, tbslen);
2570
4.72k
        OPENSSL_free(tbs);
2571
4.72k
        if (rv <= 0) {
2572
4.64k
            SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
2573
4.64k
            goto err;
2574
4.64k
        }
2575
88
        EVP_MD_CTX_free(md_ctx);
2576
88
        md_ctx = NULL;
2577
9.00k
    } else {
2578
        /* aNULL, aSRP or PSK do not need public keys */
2579
9.00k
        if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
2580
0
            && !(alg_k & SSL_PSK)) {
2581
            /* Might be wrong key type, check it */
2582
0
            if (ssl3_check_cert_and_algorithm(s)) {
2583
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA);
2584
0
            }
2585
            /* else this shouldn't happen, SSLfatal() already called */
2586
0
            goto err;
2587
0
        }
2588
        /* still data left over */
2589
9.00k
        if (PACKET_remaining(pkt) != 0) {
2590
531
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE);
2591
531
            goto err;
2592
531
        }
2593
9.00k
    }
2594
2595
8.56k
    return MSG_PROCESS_CONTINUE_READING;
2596
10.1k
err:
2597
10.1k
    EVP_MD_CTX_free(md_ctx);
2598
10.1k
    return MSG_PROCESS_ERROR;
2599
14.2k
}
2600
2601
MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s,
2602
    PACKET *pkt)
2603
974
{
2604
    /* Clear certificate validity flags */
2605
974
    if (s->s3.tmp.valid_flags != NULL)
2606
0
        memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t));
2607
974
    else
2608
974
        s->s3.tmp.valid_flags = OPENSSL_calloc(s->ssl_pkey_num, sizeof(uint32_t));
2609
2610
    /* Give up for good if allocation didn't work */
2611
974
    if (s->s3.tmp.valid_flags == NULL) {
2612
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2613
0
        return MSG_PROCESS_ERROR;
2614
0
    }
2615
2616
974
    if (SSL_CONNECTION_IS_TLS13(s)) {
2617
164
        PACKET reqctx, extensions;
2618
164
        RAW_EXTENSION *rawexts = NULL;
2619
2620
164
        if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
2621
            /*
2622
             * We already sent close_notify. This can only happen in TLSv1.3
2623
             * post-handshake messages. We can't reasonably respond to this, so
2624
             * we just ignore it
2625
             */
2626
0
            return MSG_PROCESS_FINISHED_READING;
2627
0
        }
2628
2629
        /* Free and zero certificate types: it is not present in TLS 1.3 */
2630
164
        OPENSSL_free(s->s3.tmp.ctype);
2631
164
        s->s3.tmp.ctype = NULL;
2632
164
        s->s3.tmp.ctype_len = 0;
2633
164
        OPENSSL_free(s->pha_context);
2634
164
        s->pha_context = NULL;
2635
164
        s->pha_context_len = 0;
2636
2637
164
        if (!PACKET_get_length_prefixed_1(pkt, &reqctx) || !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
2638
10
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2639
10
            return MSG_PROCESS_ERROR;
2640
10
        }
2641
2642
154
        if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
2643
13
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
2644
13
            return MSG_PROCESS_ERROR;
2645
13
        }
2646
141
        if (!tls_collect_extensions(s, &extensions,
2647
141
                SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2648
141
                &rawexts, NULL, 1)
2649
126
            || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2650
141
                rawexts, NULL, 0, 1)) {
2651
            /* SSLfatal() already called */
2652
141
            OPENSSL_free(rawexts);
2653
141
            return MSG_PROCESS_ERROR;
2654
141
        }
2655
0
        OPENSSL_free(rawexts);
2656
0
        if (!tls1_process_sigalgs(s)) {
2657
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
2658
0
            return MSG_PROCESS_ERROR;
2659
0
        }
2660
810
    } else {
2661
810
        PACKET ctypes;
2662
2663
        /* get the certificate types */
2664
810
        if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
2665
23
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2666
23
            return MSG_PROCESS_ERROR;
2667
23
        }
2668
2669
787
        if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) {
2670
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2671
0
            return MSG_PROCESS_ERROR;
2672
0
        }
2673
2674
787
        if (SSL_USE_SIGALGS(s)) {
2675
701
            PACKET sigalgs;
2676
2677
701
            if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
2678
57
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2679
57
                return MSG_PROCESS_ERROR;
2680
57
            }
2681
2682
            /*
2683
             * Despite this being for certificates, preserve compatibility
2684
             * with pre-TLS 1.3 and use the regular sigalgs field.
2685
             */
2686
644
            if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
2687
28
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2688
28
                    SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2689
28
                return MSG_PROCESS_ERROR;
2690
28
            }
2691
616
            if (!tls1_process_sigalgs(s)) {
2692
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2693
0
                return MSG_PROCESS_ERROR;
2694
0
            }
2695
616
        }
2696
2697
        /* get the CA RDNs */
2698
702
        if (!parse_ca_names(s, pkt)) {
2699
            /* SSLfatal() already called */
2700
588
            return MSG_PROCESS_ERROR;
2701
588
        }
2702
702
    }
2703
2704
114
    if (PACKET_remaining(pkt) != 0) {
2705
40
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2706
40
        return MSG_PROCESS_ERROR;
2707
40
    }
2708
2709
    /* we should setup a certificate to return.... */
2710
74
    s->s3.tmp.cert_req = 1;
2711
2712
    /*
2713
     * In TLSv1.3 we don't prepare the client certificate yet. We wait until
2714
     * after the CertificateVerify message has been received. This is because
2715
     * in TLSv1.3 the CertificateRequest arrives before the Certificate message
2716
     * but in TLSv1.2 it is the other way around. We want to make sure that
2717
     * SSL_get1_peer_certificate() returns something sensible in
2718
     * client_cert_cb.
2719
     */
2720
74
    if (SSL_CONNECTION_IS_TLS13(s)
2721
0
        && s->post_handshake_auth != SSL_PHA_REQUESTED)
2722
0
        return MSG_PROCESS_CONTINUE_READING;
2723
2724
74
    return MSG_PROCESS_CONTINUE_PROCESSING;
2725
74
}
2726
2727
MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL_CONNECTION *s,
2728
    PACKET *pkt)
2729
1.53k
{
2730
1.53k
    unsigned int ticklen;
2731
1.53k
    unsigned long ticket_lifetime_hint, age_add = 0;
2732
1.53k
    unsigned int sess_len;
2733
1.53k
    RAW_EXTENSION *exts = NULL;
2734
1.53k
    PACKET nonce;
2735
1.53k
    EVP_MD *sha256 = NULL;
2736
1.53k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2737
2738
1.53k
    PACKET_null_init(&nonce);
2739
2740
1.53k
    if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2741
1.51k
        || (SSL_CONNECTION_IS_TLS13(s)
2742
1.29k
            && (!PACKET_get_net_4(pkt, &age_add)
2743
1.29k
                || !PACKET_get_length_prefixed_1(pkt, &nonce)))
2744
1.51k
        || !PACKET_get_net_2(pkt, &ticklen)
2745
1.50k
        || (SSL_CONNECTION_IS_TLS13(s) ? (ticklen == 0
2746
1.28k
                                             || PACKET_remaining(pkt) < ticklen)
2747
1.50k
                                       : PACKET_remaining(pkt) != ticklen)) {
2748
98
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2749
98
        goto err;
2750
98
    }
2751
2752
    /*
2753
     * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
2754
     * ticket. We already checked this TLSv1.3 case above, so it should never
2755
     * be 0 here in that instance
2756
     */
2757
1.43k
    if (ticklen == 0)
2758
12
        return MSG_PROCESS_CONTINUE_READING;
2759
2760
    /*
2761
     * Sessions must be immutable once they go into the session cache. Otherwise
2762
     * we can get multi-thread problems. Therefore we don't "update" sessions,
2763
     * we replace them with a duplicate. In TLSv1.3 we need to do this every
2764
     * time a NewSessionTicket arrives because those messages arrive
2765
     * post-handshake and the session may have already gone into the session
2766
     * cache.
2767
     */
2768
1.42k
    if (SSL_CONNECTION_IS_TLS13(s) || s->session->session_id_length > 0) {
2769
1.26k
        SSL_SESSION *new_sess;
2770
2771
        /*
2772
         * We reused an existing session, so we need to replace it with a new
2773
         * one
2774
         */
2775
1.26k
        if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2776
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2777
0
            goto err;
2778
0
        }
2779
2780
1.26k
        if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
2781
0
            && !SSL_CONNECTION_IS_TLS13(s)) {
2782
            /*
2783
             * In TLSv1.2 and below the arrival of a new tickets signals that
2784
             * any old ticket we were using is now out of date, so we remove the
2785
             * old session from the cache. We carry on if this fails
2786
             */
2787
0
            SSL_CTX_remove_session(s->session_ctx, s->session);
2788
0
        }
2789
2790
1.26k
        SSL_SESSION_free(s->session);
2791
1.26k
        s->session = new_sess;
2792
1.26k
    }
2793
2794
1.42k
    s->session->time = ossl_time_now();
2795
1.42k
    ssl_session_calculate_timeout(s->session);
2796
2797
1.42k
    OPENSSL_free(s->session->ext.tick);
2798
1.42k
    s->session->ext.tick = NULL;
2799
1.42k
    s->session->ext.ticklen = 0;
2800
2801
1.42k
    s->session->ext.tick = OPENSSL_malloc(ticklen);
2802
1.42k
    if (s->session->ext.tick == NULL) {
2803
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2804
0
        goto err;
2805
0
    }
2806
1.42k
    if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
2807
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2808
0
        goto err;
2809
0
    }
2810
2811
1.42k
    s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
2812
1.42k
    s->session->ext.tick_age_add = age_add;
2813
1.42k
    s->session->ext.ticklen = ticklen;
2814
2815
1.42k
    if (SSL_CONNECTION_IS_TLS13(s)) {
2816
1.26k
        PACKET extpkt;
2817
2818
        /*
2819
         * Fulfilling RFC8446:4.6.1 requirement: Clients MUST NOT cache
2820
         * tickets for longer than 7 days.
2821
         */
2822
1.26k
        if (ticket_lifetime_hint > 604800) {
2823
4
            ticket_lifetime_hint = 604800;
2824
4
        }
2825
2826
1.26k
        if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
2827
1.22k
            || PACKET_remaining(pkt) != 0) {
2828
36
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2829
36
            goto err;
2830
36
        }
2831
2832
1.22k
        if (!tls_collect_extensions(s, &extpkt,
2833
1.22k
                SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
2834
1.22k
                NULL, 1)
2835
1.22k
            || !tls_parse_all_extensions(s,
2836
1.22k
                SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
2837
1.22k
                exts, NULL, 0, 1)) {
2838
            /* SSLfatal() already called */
2839
3
            goto err;
2840
3
        }
2841
1.22k
    }
2842
2843
    /*
2844
     * There are two ways to detect a resumed ticket session. One is to set
2845
     * an appropriate session ID and then the server must return a match in
2846
     * ServerHello. This allows the normal client session ID matching to work
2847
     * and we know much earlier that the ticket has been accepted. The
2848
     * other way is to set zero length session ID when the ticket is
2849
     * presented and rely on the handshake to determine session resumption.
2850
     * We choose the former approach because this fits in with assumptions
2851
     * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the
2852
     * ticket.
2853
     */
2854
1.38k
    sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq);
2855
1.38k
    if (sha256 == NULL) {
2856
        /* Error is already recorded */
2857
0
        SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
2858
0
        goto err;
2859
0
    }
2860
    /*
2861
     * We use sess_len here because EVP_Digest expects an int
2862
     * but s->session->session_id_length is a size_t
2863
     */
2864
1.38k
    if (!EVP_Digest(s->session->ext.tick, ticklen,
2865
1.38k
            s->session->session_id, &sess_len,
2866
1.38k
            sha256, NULL)) {
2867
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2868
0
        goto err;
2869
0
    }
2870
1.38k
    EVP_MD_free(sha256);
2871
1.38k
    sha256 = NULL;
2872
1.38k
    s->session->session_id_length = sess_len;
2873
1.38k
    s->session->not_resumable = 0;
2874
2875
    /* This is a standalone message in TLSv1.3, so there is no more to read */
2876
1.38k
    if (SSL_CONNECTION_IS_TLS13(s)) {
2877
1.22k
        const EVP_MD *md = ssl_handshake_md(s);
2878
1.22k
        int hashleni = EVP_MD_get_size(md);
2879
1.22k
        size_t hashlen;
2880
        /* ASCII: "resumption", in hex for EBCDIC compatibility */
2881
1.22k
        static const unsigned char nonce_label[] = { 0x72, 0x65, 0x73, 0x75, 0x6D,
2882
1.22k
            0x70, 0x74, 0x69, 0x6F, 0x6E };
2883
2884
        /* Ensure cast to size_t is safe */
2885
1.22k
        if (!ossl_assert(hashleni > 0)) {
2886
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2887
0
            goto err;
2888
0
        }
2889
1.22k
        hashlen = (size_t)hashleni;
2890
2891
1.22k
        if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
2892
1.22k
                nonce_label,
2893
1.22k
                sizeof(nonce_label),
2894
1.22k
                PACKET_data(&nonce),
2895
1.22k
                PACKET_remaining(&nonce),
2896
1.22k
                s->session->master_key,
2897
1.22k
                hashlen, 1)) {
2898
            /* SSLfatal() already called */
2899
0
            goto err;
2900
0
        }
2901
1.22k
        s->session->master_key_length = hashlen;
2902
2903
1.22k
        OPENSSL_free(exts);
2904
1.22k
        ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
2905
1.22k
        return MSG_PROCESS_FINISHED_READING;
2906
1.22k
    }
2907
2908
159
    return MSG_PROCESS_CONTINUE_READING;
2909
137
err:
2910
137
    EVP_MD_free(sha256);
2911
137
    OPENSSL_free(exts);
2912
137
    return MSG_PROCESS_ERROR;
2913
1.38k
}
2914
2915
/*
2916
 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
2917
 * parse a separate message. Returns 1 on success or 0 on failure
2918
 */
2919
int tls_process_cert_status_body(SSL_CONNECTION *s, size_t chainidx, PACKET *pkt)
2920
0
{
2921
0
    unsigned int type;
2922
0
#ifndef OPENSSL_NO_OCSP
2923
0
    size_t resplen;
2924
0
    unsigned char *respder;
2925
0
    OCSP_RESPONSE *resp = NULL;
2926
0
    const unsigned char *p;
2927
0
#endif
2928
2929
0
    if (!PACKET_get_1(pkt, &type)
2930
0
        || type != TLSEXT_STATUSTYPE_ocsp) {
2931
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE);
2932
0
        return 0;
2933
0
    }
2934
2935
0
#ifndef OPENSSL_NO_OCSP
2936
0
    OPENSSL_free(s->ext.ocsp.resp);
2937
0
    s->ext.ocsp.resp = NULL;
2938
0
    s->ext.ocsp.resp_len = 0;
2939
2940
0
    if (s->ext.ocsp.resp_ex == NULL)
2941
0
        s->ext.ocsp.resp_ex = sk_OCSP_RESPONSE_new_null();
2942
2943
    /*
2944
     * TODO(DTLS-1.3): in future DTLS should also be considered
2945
     */
2946
0
    if (!SSL_CONNECTION_IS_TLS13(s) && type == TLSEXT_STATUSTYPE_ocsp) {
2947
0
        sk_OCSP_RESPONSE_pop_free(s->ext.ocsp.resp_ex, OCSP_RESPONSE_free);
2948
0
        s->ext.ocsp.resp_ex = sk_OCSP_RESPONSE_new_null();
2949
0
    }
2950
2951
0
    if (PACKET_remaining(pkt) > 0) {
2952
0
        if (!PACKET_get_net_3_len(pkt, &resplen)
2953
0
            || PACKET_remaining(pkt) != resplen) {
2954
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2955
0
            return 0;
2956
0
        }
2957
2958
0
        if (resplen == 0) {
2959
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
2960
0
            return 0;
2961
0
        }
2962
2963
0
        if ((respder = OPENSSL_malloc(resplen)) == NULL) {
2964
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2965
0
            return 0;
2966
0
        }
2967
2968
0
        if (!PACKET_copy_bytes(pkt, respder, resplen)) {
2969
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2970
0
            OPENSSL_free(respder);
2971
0
            return 0;
2972
0
        }
2973
0
        p = respder;
2974
0
        resp = d2i_OCSP_RESPONSE(NULL, &p, (long)resplen);
2975
0
        OPENSSL_free(respder);
2976
0
        if (resp == NULL) {
2977
0
            SSLfatal(s, TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
2978
0
                SSL_R_TLSV1_BAD_CERTIFICATE_STATUS_RESPONSE);
2979
0
            return 0;
2980
0
        }
2981
0
        sk_OCSP_RESPONSE_insert(s->ext.ocsp.resp_ex, resp, (int)chainidx);
2982
0
    }
2983
2984
0
#endif
2985
0
    return 1;
2986
0
}
2987
2988
MSG_PROCESS_RETURN tls_process_cert_status(SSL_CONNECTION *s, PACKET *pkt)
2989
0
{
2990
0
    if (!tls_process_cert_status_body(s, 0, pkt)) {
2991
        /* SSLfatal() already called */
2992
0
        return MSG_PROCESS_ERROR;
2993
0
    }
2994
2995
0
    return MSG_PROCESS_CONTINUE_READING;
2996
0
}
2997
2998
/*
2999
 * Perform miscellaneous checks and processing after we have received the
3000
 * server's initial flight. In TLS1.3 this is after the Server Finished message.
3001
 * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
3002
 * on failure.
3003
 */
3004
int tls_process_initial_server_flight(SSL_CONNECTION *s)
3005
25.3k
{
3006
25.3k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3007
3008
    /*
3009
     * at this point we check that we have the required stuff from
3010
     * the server
3011
     */
3012
25.3k
    if (!ssl3_check_cert_and_algorithm(s)) {
3013
        /* SSLfatal() already called */
3014
8
        return 0;
3015
8
    }
3016
3017
    /*
3018
     * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
3019
     * |ext.ocsp.resp_len| values will be set if we actually received a status
3020
     * message, or NULL and -1 otherwise
3021
     */
3022
25.3k
    if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
3023
0
        && sctx->ext.status_cb != NULL) {
3024
0
        int ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s),
3025
0
            sctx->ext.status_arg);
3026
3027
0
        if (ret == 0) {
3028
0
            SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
3029
0
                SSL_R_INVALID_STATUS_RESPONSE);
3030
0
            return 0;
3031
0
        }
3032
0
        if (ret < 0) {
3033
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3034
0
                SSL_R_OCSP_CALLBACK_FAILURE);
3035
0
            return 0;
3036
0
        }
3037
0
    }
3038
25.3k
#ifndef OPENSSL_NO_CT
3039
25.3k
    if (s->ct_validation_callback != NULL) {
3040
        /* Note we validate the SCTs whether or not we abort on error */
3041
0
        if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
3042
            /* SSLfatal() already called */
3043
0
            return 0;
3044
0
        }
3045
0
    }
3046
25.3k
#endif
3047
3048
25.3k
    return 1;
3049
25.3k
}
3050
3051
MSG_PROCESS_RETURN tls_process_server_done(SSL_CONNECTION *s, PACKET *pkt)
3052
18.3k
{
3053
18.3k
    if (PACKET_remaining(pkt) > 0) {
3054
        /* should contain no data */
3055
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3056
0
        return MSG_PROCESS_ERROR;
3057
0
    }
3058
18.3k
#ifndef OPENSSL_NO_SRP
3059
18.3k
    if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3060
0
        if (ssl_srp_calc_a_param_intern(s) <= 0) {
3061
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC);
3062
0
            return MSG_PROCESS_ERROR;
3063
0
        }
3064
0
    }
3065
18.3k
#endif
3066
3067
18.3k
    if (!tls_process_initial_server_flight(s)) {
3068
        /* SSLfatal() already called */
3069
10
        return MSG_PROCESS_ERROR;
3070
10
    }
3071
3072
18.2k
    return MSG_PROCESS_FINISHED_READING;
3073
18.3k
}
3074
3075
static int tls_construct_cke_psk_preamble(SSL_CONNECTION *s, WPACKET *pkt)
3076
0
{
3077
0
#ifndef OPENSSL_NO_PSK
3078
0
    int ret = 0;
3079
    /*
3080
     * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
3081
     * \0-terminated identity. The last byte is for us for simulating
3082
     * strnlen.
3083
     */
3084
0
    char identity[PSK_MAX_IDENTITY_LEN + 1];
3085
0
    size_t identitylen = 0;
3086
0
    unsigned char psk[PSK_MAX_PSK_LEN];
3087
0
    unsigned char *tmppsk = NULL;
3088
0
    char *tmpidentity = NULL;
3089
0
    size_t psklen = 0;
3090
3091
0
    if (s->psk_client_callback == NULL) {
3092
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB);
3093
0
        goto err;
3094
0
    }
3095
3096
0
    memset(identity, 0, sizeof(identity));
3097
3098
0
    psklen = s->psk_client_callback(SSL_CONNECTION_GET_USER_SSL(s),
3099
0
        s->session->psk_identity_hint,
3100
0
        identity, sizeof(identity) - 1,
3101
0
        psk, sizeof(psk));
3102
3103
0
    if (psklen > PSK_MAX_PSK_LEN) {
3104
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
3105
0
        psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */
3106
0
        goto err;
3107
0
    } else if (psklen == 0) {
3108
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND);
3109
0
        goto err;
3110
0
    }
3111
3112
0
    identitylen = strlen(identity);
3113
0
    if (identitylen > PSK_MAX_IDENTITY_LEN) {
3114
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3115
0
        goto err;
3116
0
    }
3117
3118
0
    tmppsk = OPENSSL_memdup(psk, psklen);
3119
0
    tmpidentity = OPENSSL_strdup(identity);
3120
0
    if (tmppsk == NULL || tmpidentity == NULL) {
3121
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3122
0
        goto err;
3123
0
    }
3124
3125
0
    OPENSSL_free(s->s3.tmp.psk);
3126
0
    s->s3.tmp.psk = tmppsk;
3127
0
    s->s3.tmp.psklen = psklen;
3128
0
    tmppsk = NULL;
3129
0
    OPENSSL_free(s->session->psk_identity);
3130
0
    s->session->psk_identity = tmpidentity;
3131
0
    tmpidentity = NULL;
3132
3133
0
    if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
3134
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3135
0
        goto err;
3136
0
    }
3137
3138
0
    ret = 1;
3139
3140
0
err:
3141
0
    OPENSSL_cleanse(psk, psklen);
3142
0
    OPENSSL_cleanse(identity, sizeof(identity));
3143
0
    OPENSSL_clear_free(tmppsk, psklen);
3144
0
    OPENSSL_clear_free(tmpidentity, identitylen);
3145
3146
0
    return ret;
3147
#else
3148
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3149
    return 0;
3150
#endif
3151
0
}
3152
3153
static int tls_construct_cke_rsa(SSL_CONNECTION *s, WPACKET *pkt)
3154
7.96k
{
3155
7.96k
    unsigned char *encdata = NULL;
3156
7.96k
    EVP_PKEY *pkey = NULL;
3157
7.96k
    EVP_PKEY_CTX *pctx = NULL;
3158
7.96k
    size_t enclen;
3159
7.96k
    unsigned char *pms = NULL;
3160
7.96k
    size_t pmslen = 0;
3161
7.96k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3162
3163
7.96k
    if (!received_server_cert(s)) {
3164
        /*
3165
         * We should always have a server certificate with SSL_kRSA.
3166
         */
3167
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3168
0
        return 0;
3169
0
    }
3170
3171
7.96k
    if ((pkey = tls_get_peer_pkey(s)) == NULL) {
3172
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3173
0
        return 0;
3174
0
    }
3175
3176
7.96k
    if (!EVP_PKEY_is_a(pkey, "RSA")) {
3177
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3178
0
        return 0;
3179
0
    }
3180
3181
7.96k
    pmslen = SSL_MAX_MASTER_KEY_LENGTH;
3182
7.96k
    pms = OPENSSL_malloc(pmslen);
3183
7.96k
    if (pms == NULL) {
3184
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3185
0
        return 0;
3186
0
    }
3187
3188
7.96k
    pms[0] = s->client_version >> 8;
3189
7.96k
    pms[1] = s->client_version & 0xff;
3190
7.96k
    if (RAND_bytes_ex(sctx->libctx, pms + 2, pmslen - 2, 0) <= 0) {
3191
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_RAND_LIB);
3192
0
        goto err;
3193
0
    }
3194
3195
    /* Fix buf for TLS and beyond */
3196
7.96k
    if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
3197
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3198
0
        goto err;
3199
0
    }
3200
3201
7.96k
    pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pkey, sctx->propq);
3202
7.96k
    if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
3203
7.96k
        || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
3204
7
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3205
7
        goto err;
3206
7
    }
3207
7.95k
    if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
3208
7.95k
        || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
3209
706
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT);
3210
706
        goto err;
3211
706
    }
3212
7.25k
    EVP_PKEY_CTX_free(pctx);
3213
7.25k
    pctx = NULL;
3214
3215
    /* Fix buf for TLS and beyond */
3216
7.25k
    if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
3217
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3218
0
        goto err;
3219
0
    }
3220
3221
    /* Log the premaster secret, if logging is enabled. */
3222
7.25k
    if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
3223
        /* SSLfatal() already called */
3224
0
        goto err;
3225
0
    }
3226
3227
7.25k
    s->s3.tmp.pms = pms;
3228
7.25k
    s->s3.tmp.pmslen = pmslen;
3229
3230
7.25k
    return 1;
3231
713
err:
3232
713
    OPENSSL_clear_free(pms, pmslen);
3233
713
    EVP_PKEY_CTX_free(pctx);
3234
3235
713
    return 0;
3236
7.25k
}
3237
3238
static int tls_construct_cke_dhe(SSL_CONNECTION *s, WPACKET *pkt)
3239
3.65k
{
3240
3.65k
    EVP_PKEY *ckey = NULL, *skey = NULL;
3241
3.65k
    unsigned char *keybytes = NULL;
3242
3.65k
    int prime_len;
3243
3.65k
    unsigned char *encoded_pub = NULL;
3244
3.65k
    size_t encoded_pub_len, pad_len;
3245
3.65k
    int ret = 0;
3246
3247
3.65k
    skey = s->s3.peer_tmp;
3248
3.65k
    if (skey == NULL) {
3249
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3250
0
        goto err;
3251
0
    }
3252
3253
3.65k
    ckey = ssl_generate_pkey(s, skey);
3254
3.65k
    if (ckey == NULL) {
3255
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3256
0
        goto err;
3257
0
    }
3258
3259
3.65k
    if (ssl_derive(s, ckey, skey, 0) == 0) {
3260
        /* SSLfatal() already called */
3261
0
        goto err;
3262
0
    }
3263
3264
    /* send off the data */
3265
3266
    /* Generate encoding of server key */
3267
3.65k
    encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub);
3268
3.65k
    if (encoded_pub_len == 0) {
3269
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3270
0
        EVP_PKEY_free(ckey);
3271
0
        return EXT_RETURN_FAIL;
3272
0
    }
3273
3274
    /*
3275
     * For interoperability with some versions of the Microsoft TLS
3276
     * stack, we need to zero pad the DHE pub key to the same length
3277
     * as the prime.
3278
     */
3279
3.65k
    prime_len = EVP_PKEY_get_size(ckey);
3280
3.65k
    pad_len = prime_len - encoded_pub_len;
3281
3.65k
    if (pad_len > 0) {
3282
0
        if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) {
3283
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3284
0
            goto err;
3285
0
        }
3286
0
        memset(keybytes, 0, pad_len);
3287
0
    }
3288
3289
3.65k
    if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) {
3290
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3291
0
        goto err;
3292
0
    }
3293
3294
3.65k
    ret = 1;
3295
3.65k
err:
3296
3.65k
    OPENSSL_free(encoded_pub);
3297
3.65k
    EVP_PKEY_free(ckey);
3298
3.65k
    return ret;
3299
3.65k
}
3300
3301
static int tls_construct_cke_ecdhe(SSL_CONNECTION *s, WPACKET *pkt)
3302
4.03k
{
3303
4.03k
    unsigned char *encodedPoint = NULL;
3304
4.03k
    size_t encoded_pt_len = 0;
3305
4.03k
    EVP_PKEY *ckey = NULL, *skey = NULL;
3306
4.03k
    int ret = 0;
3307
3308
4.03k
    skey = s->s3.peer_tmp;
3309
4.03k
    if (skey == NULL) {
3310
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3311
0
        return 0;
3312
0
    }
3313
3314
4.03k
    ckey = ssl_generate_pkey(s, skey);
3315
4.03k
    if (ckey == NULL) {
3316
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
3317
0
        goto err;
3318
0
    }
3319
3320
4.03k
    if (ssl_derive(s, ckey, skey, 0) == 0) {
3321
        /* SSLfatal() already called */
3322
308
        goto err;
3323
308
    }
3324
3325
    /* Generate encoding of client key */
3326
3.72k
    encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint);
3327
3328
3.72k
    if (encoded_pt_len == 0) {
3329
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
3330
0
        goto err;
3331
0
    }
3332
3333
3.72k
    if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
3334
585
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3335
585
        goto err;
3336
585
    }
3337
3338
3.13k
    ret = 1;
3339
4.03k
err:
3340
4.03k
    OPENSSL_free(encodedPoint);
3341
4.03k
    EVP_PKEY_free(ckey);
3342
4.03k
    return ret;
3343
3.13k
}
3344
3345
static int tls_construct_cke_gost(SSL_CONNECTION *s, WPACKET *pkt)
3346
0
{
3347
0
#ifndef OPENSSL_NO_GOST
3348
    /* GOST key exchange message creation */
3349
0
    EVP_PKEY_CTX *pkey_ctx = NULL;
3350
0
    EVP_PKEY *pkey = NULL;
3351
0
    size_t msglen;
3352
0
    unsigned int md_len;
3353
0
    unsigned char shared_ukm[32], tmp[256];
3354
0
    EVP_MD_CTX *ukm_hash = NULL;
3355
0
    int dgst_nid = NID_id_GostR3411_94;
3356
0
    unsigned char *pms = NULL;
3357
0
    size_t pmslen = 0;
3358
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3359
3360
0
    if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
3361
0
        dgst_nid = NID_id_GostR3411_2012_256;
3362
3363
    /*
3364
     * Get server certificate PKEY and create ctx from it
3365
     */
3366
0
    if ((pkey = tls_get_peer_pkey(s)) == NULL) {
3367
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3368
0
            SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3369
0
        return 0;
3370
0
    }
3371
3372
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
3373
0
        pkey,
3374
0
        sctx->propq);
3375
0
    if (pkey_ctx == NULL) {
3376
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3377
0
        return 0;
3378
0
    }
3379
    /*
3380
     * If we have send a certificate, and certificate key
3381
     * parameters match those of server certificate, use
3382
     * certificate key for key exchange
3383
     */
3384
3385
    /* Otherwise, generate ephemeral key pair */
3386
0
    pmslen = 32;
3387
0
    pms = OPENSSL_malloc(pmslen);
3388
0
    if (pms == NULL) {
3389
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3390
0
        goto err;
3391
0
    }
3392
3393
0
    if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
3394
        /* Generate session key
3395
         */
3396
0
        || RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
3397
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3398
0
        goto err;
3399
0
    };
3400
    /*
3401
     * Compute shared IV and store it in algorithm-specific context
3402
     * data
3403
     */
3404
0
    ukm_hash = EVP_MD_CTX_new();
3405
0
    if (ukm_hash == NULL
3406
0
        || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
3407
0
        || EVP_DigestUpdate(ukm_hash, s->s3.client_random,
3408
0
               SSL3_RANDOM_SIZE)
3409
0
            <= 0
3410
0
        || EVP_DigestUpdate(ukm_hash, s->s3.server_random,
3411
0
               SSL3_RANDOM_SIZE)
3412
0
            <= 0
3413
0
        || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
3414
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3415
0
        goto err;
3416
0
    }
3417
0
    EVP_MD_CTX_free(ukm_hash);
3418
0
    ukm_hash = NULL;
3419
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3420
0
            EVP_PKEY_CTRL_SET_IV, 8, shared_ukm)
3421
0
        <= 0) {
3422
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3423
0
        goto err;
3424
0
    }
3425
    /* Make GOST keytransport blob message */
3426
    /*
3427
     * Encapsulate it into sequence
3428
     */
3429
0
    msglen = 255;
3430
0
    if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
3431
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3432
0
        goto err;
3433
0
    }
3434
3435
0
    if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
3436
0
        || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
3437
0
        || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
3438
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3439
0
        goto err;
3440
0
    }
3441
3442
0
    EVP_PKEY_CTX_free(pkey_ctx);
3443
0
    s->s3.tmp.pms = pms;
3444
0
    s->s3.tmp.pmslen = pmslen;
3445
3446
0
    return 1;
3447
0
err:
3448
0
    EVP_PKEY_CTX_free(pkey_ctx);
3449
0
    OPENSSL_clear_free(pms, pmslen);
3450
0
    EVP_MD_CTX_free(ukm_hash);
3451
0
    return 0;
3452
#else
3453
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3454
    return 0;
3455
#endif
3456
0
}
3457
3458
#ifndef OPENSSL_NO_GOST
3459
int ossl_gost18_cke_cipher_nid(const SSL_CONNECTION *s)
3460
0
{
3461
0
    if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0)
3462
0
        return NID_magma_ctr;
3463
0
    else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0)
3464
0
        return NID_kuznyechik_ctr;
3465
3466
0
    return NID_undef;
3467
0
}
3468
3469
int ossl_gost_ukm(const SSL_CONNECTION *s, unsigned char *dgst_buf)
3470
0
{
3471
0
    EVP_MD_CTX *hash = NULL;
3472
0
    unsigned int md_len;
3473
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3474
0
    const EVP_MD *md = ssl_evp_md_fetch(sctx->libctx, NID_id_GostR3411_2012_256,
3475
0
        sctx->propq);
3476
3477
0
    if (md == NULL)
3478
0
        return 0;
3479
3480
0
    if ((hash = EVP_MD_CTX_new()) == NULL
3481
0
        || EVP_DigestInit(hash, md) <= 0
3482
0
        || EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0
3483
0
        || EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0
3484
0
        || EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) {
3485
0
        EVP_MD_CTX_free(hash);
3486
0
        ssl_evp_md_free(md);
3487
0
        return 0;
3488
0
    }
3489
3490
0
    EVP_MD_CTX_free(hash);
3491
0
    ssl_evp_md_free(md);
3492
0
    return 1;
3493
0
}
3494
#endif
3495
3496
static int tls_construct_cke_gost18(SSL_CONNECTION *s, WPACKET *pkt)
3497
0
{
3498
0
#ifndef OPENSSL_NO_GOST
3499
    /* GOST 2018 key exchange message creation */
3500
0
    unsigned char rnd_dgst[32];
3501
0
    unsigned char *encdata = NULL;
3502
0
    EVP_PKEY_CTX *pkey_ctx = NULL;
3503
0
    EVP_PKEY *pkey;
3504
0
    unsigned char *pms = NULL;
3505
0
    size_t pmslen = 0;
3506
0
    size_t msglen;
3507
0
    int cipher_nid = ossl_gost18_cke_cipher_nid(s);
3508
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3509
3510
0
    if (cipher_nid == NID_undef) {
3511
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3512
0
        return 0;
3513
0
    }
3514
3515
0
    if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
3516
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3517
0
        goto err;
3518
0
    }
3519
3520
    /* Pre-master secret - random bytes */
3521
0
    pmslen = 32;
3522
0
    pms = OPENSSL_malloc(pmslen);
3523
0
    if (pms == NULL) {
3524
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3525
0
        goto err;
3526
0
    }
3527
3528
0
    if (RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
3529
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3530
0
        goto err;
3531
0
    }
3532
3533
    /* Get server certificate PKEY and create ctx from it */
3534
0
    if ((pkey = tls_get_peer_pkey(s)) == NULL) {
3535
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3536
0
            SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3537
0
        goto err;
3538
0
    }
3539
3540
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
3541
0
        pkey,
3542
0
        sctx->propq);
3543
0
    if (pkey_ctx == NULL) {
3544
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3545
0
        goto err;
3546
0
    }
3547
3548
0
    if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) {
3549
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3550
0
        goto err;
3551
0
    };
3552
3553
    /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */
3554
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3555
0
            EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst)
3556
0
        <= 0) {
3557
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3558
0
        goto err;
3559
0
    }
3560
3561
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3562
0
            EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL)
3563
0
        <= 0) {
3564
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3565
0
        goto err;
3566
0
    }
3567
3568
0
    if (EVP_PKEY_encrypt(pkey_ctx, NULL, &msglen, pms, pmslen) <= 0) {
3569
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3570
0
        goto err;
3571
0
    }
3572
3573
0
    if (!WPACKET_allocate_bytes(pkt, msglen, &encdata)
3574
0
        || EVP_PKEY_encrypt(pkey_ctx, encdata, &msglen, pms, pmslen) <= 0) {
3575
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3576
0
        goto err;
3577
0
    }
3578
3579
0
    EVP_PKEY_CTX_free(pkey_ctx);
3580
0
    pkey_ctx = NULL;
3581
0
    s->s3.tmp.pms = pms;
3582
0
    s->s3.tmp.pmslen = pmslen;
3583
3584
0
    return 1;
3585
0
err:
3586
0
    EVP_PKEY_CTX_free(pkey_ctx);
3587
0
    OPENSSL_clear_free(pms, pmslen);
3588
0
    return 0;
3589
#else
3590
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3591
    return 0;
3592
#endif
3593
0
}
3594
3595
static int tls_construct_cke_srp(SSL_CONNECTION *s, WPACKET *pkt)
3596
0
{
3597
0
#ifndef OPENSSL_NO_SRP
3598
0
    unsigned char *abytes = NULL;
3599
3600
0
    if (s->srp_ctx.A == NULL
3601
0
        || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
3602
0
            &abytes)) {
3603
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3604
0
        return 0;
3605
0
    }
3606
0
    BN_bn2bin(s->srp_ctx.A, abytes);
3607
3608
0
    OPENSSL_free(s->session->srp_username);
3609
0
    s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3610
0
    if (s->session->srp_username == NULL) {
3611
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3612
0
        return 0;
3613
0
    }
3614
3615
0
    return 1;
3616
#else
3617
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3618
    return 0;
3619
#endif
3620
0
}
3621
3622
CON_FUNC_RETURN tls_construct_client_key_exchange(SSL_CONNECTION *s,
3623
    WPACKET *pkt)
3624
18.2k
{
3625
18.2k
    unsigned long alg_k;
3626
3627
18.2k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3628
3629
    /*
3630
     * All of the construct functions below call SSLfatal() if necessary so
3631
     * no need to do so here.
3632
     */
3633
18.2k
    if ((alg_k & SSL_PSK)
3634
0
        && !tls_construct_cke_psk_preamble(s, pkt))
3635
0
        goto err;
3636
3637
18.2k
    if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3638
10.6k
        if (!tls_construct_cke_rsa(s, pkt))
3639
977
            goto err;
3640
10.6k
    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3641
3.65k
        if (!tls_construct_cke_dhe(s, pkt))
3642
0
            goto err;
3643
4.03k
    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3644
4.03k
        if (!tls_construct_cke_ecdhe(s, pkt))
3645
893
            goto err;
3646
4.03k
    } else if (alg_k & SSL_kGOST) {
3647
0
        if (!tls_construct_cke_gost(s, pkt))
3648
0
            goto err;
3649
0
    } else if (alg_k & SSL_kGOST18) {
3650
0
        if (!tls_construct_cke_gost18(s, pkt))
3651
0
            goto err;
3652
0
    } else if (alg_k & SSL_kSRP) {
3653
0
        if (!tls_construct_cke_srp(s, pkt))
3654
0
            goto err;
3655
0
    } else if (!(alg_k & SSL_kPSK)) {
3656
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3657
0
        goto err;
3658
0
    }
3659
3660
16.4k
    return CON_FUNC_SUCCESS;
3661
1.87k
err:
3662
1.87k
    OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen);
3663
1.87k
    s->s3.tmp.pms = NULL;
3664
1.87k
    s->s3.tmp.pmslen = 0;
3665
1.87k
#ifndef OPENSSL_NO_PSK
3666
1.87k
    OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
3667
1.87k
    s->s3.tmp.psk = NULL;
3668
1.87k
    s->s3.tmp.psklen = 0;
3669
1.87k
#endif
3670
1.87k
    return CON_FUNC_ERROR;
3671
18.2k
}
3672
3673
int tls_client_key_exchange_post_work(SSL_CONNECTION *s)
3674
16.4k
{
3675
16.4k
    unsigned char *pms = NULL;
3676
16.4k
    size_t pmslen = 0;
3677
3678
16.4k
    pms = s->s3.tmp.pms;
3679
16.4k
    pmslen = s->s3.tmp.pmslen;
3680
3681
16.4k
#ifndef OPENSSL_NO_SRP
3682
    /* Check for SRP */
3683
16.4k
    if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3684
0
        if (!srp_generate_client_master_secret(s)) {
3685
            /* SSLfatal() already called */
3686
0
            goto err;
3687
0
        }
3688
0
        return 1;
3689
0
    }
3690
16.4k
#endif
3691
3692
16.4k
    if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
3693
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_PASSED_INVALID_ARGUMENT);
3694
0
        goto err;
3695
0
    }
3696
16.4k
    if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
3697
        /* SSLfatal() already called */
3698
        /* ssl_generate_master_secret frees the pms even on error */
3699
0
        pms = NULL;
3700
0
        pmslen = 0;
3701
0
        goto err;
3702
0
    }
3703
16.4k
    pms = NULL;
3704
16.4k
    pmslen = 0;
3705
3706
#ifndef OPENSSL_NO_SCTP
3707
    if (SSL_CONNECTION_IS_DTLS(s)) {
3708
        unsigned char sctpauthkey[64];
3709
        char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3710
        size_t labellen;
3711
        SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3712
3713
        /*
3714
         * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3715
         * used.
3716
         */
3717
        memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3718
            sizeof(DTLS1_SCTP_AUTH_LABEL));
3719
3720
        /* Don't include the terminating zero. */
3721
        labellen = sizeof(labelbuffer) - 1;
3722
        if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
3723
            labellen += 1;
3724
3725
        if (SSL_export_keying_material(ssl, sctpauthkey,
3726
                sizeof(sctpauthkey), labelbuffer,
3727
                labellen, NULL, 0, 0)
3728
            <= 0) {
3729
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3730
            goto err;
3731
        }
3732
3733
        BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3734
            sizeof(sctpauthkey), sctpauthkey);
3735
    }
3736
#endif
3737
3738
16.4k
    return 1;
3739
0
err:
3740
0
    OPENSSL_clear_free(pms, pmslen);
3741
0
    s->s3.tmp.pms = NULL;
3742
0
    s->s3.tmp.pmslen = 0;
3743
0
    return 0;
3744
16.4k
}
3745
3746
/*
3747
 * Check a certificate can be used for client authentication. Currently check
3748
 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3749
 * certificates can be used and optionally checks suitability for Suite B.
3750
 */
3751
static int ssl3_check_client_certificate(SSL_CONNECTION *s)
3752
83
{
3753
    /* If no suitable signature algorithm can't use certificate */
3754
83
    if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL)
3755
83
        return 0;
3756
    /*
3757
     * If strict mode check suitability of chain before using it. This also
3758
     * adjusts suite B digest if necessary.
3759
     */
3760
0
    if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && !tls1_check_chain(s, NULL, NULL, NULL, -2))
3761
0
        return 0;
3762
0
    return 1;
3763
0
}
3764
3765
WORK_STATE tls_prepare_client_certificate(SSL_CONNECTION *s, WORK_STATE wst)
3766
69
{
3767
69
    X509 *x509 = NULL;
3768
69
    EVP_PKEY *pkey = NULL;
3769
69
    int i;
3770
69
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3771
3772
69
    if (wst == WORK_MORE_A) {
3773
        /* Let cert callback update client certificates if required */
3774
69
        if (s->cert->cert_cb) {
3775
0
            i = s->cert->cert_cb(ssl, s->cert->cert_cb_arg);
3776
0
            if (i < 0) {
3777
0
                s->rwstate = SSL_X509_LOOKUP;
3778
0
                return WORK_MORE_A;
3779
0
            }
3780
0
            if (i == 0) {
3781
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
3782
0
                return WORK_ERROR;
3783
0
            }
3784
0
            s->rwstate = SSL_NOTHING;
3785
0
        }
3786
69
        if (ssl3_check_client_certificate(s)) {
3787
0
            if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3788
0
                return WORK_FINISHED_STOP;
3789
0
            }
3790
0
            return WORK_FINISHED_CONTINUE;
3791
0
        }
3792
3793
        /* Fall through to WORK_MORE_B */
3794
69
        wst = WORK_MORE_B;
3795
69
    }
3796
3797
    /* We need to get a client cert */
3798
69
    if (wst == WORK_MORE_B) {
3799
        /*
3800
         * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3801
         * return(-1); We then get retied later
3802
         */
3803
69
        i = ssl_do_client_cert_cb(s, &x509, &pkey);
3804
69
        if (i < 0) {
3805
0
            s->rwstate = SSL_X509_LOOKUP;
3806
0
            return WORK_MORE_B;
3807
0
        }
3808
69
        s->rwstate = SSL_NOTHING;
3809
69
        if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3810
0
            if (!SSL_use_certificate(ssl, x509)
3811
0
                || !SSL_use_PrivateKey(ssl, pkey))
3812
0
                i = 0;
3813
69
        } else if (i == 1) {
3814
0
            i = 0;
3815
0
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3816
0
        }
3817
3818
69
        X509_free(x509);
3819
69
        EVP_PKEY_free(pkey);
3820
69
        if (i && !ssl3_check_client_certificate(s))
3821
0
            i = 0;
3822
69
        if (i == 0) {
3823
69
            if (s->version == SSL3_VERSION) {
3824
0
                s->s3.tmp.cert_req = 0;
3825
0
                ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3826
0
                return WORK_FINISHED_CONTINUE;
3827
69
            } else {
3828
69
                s->s3.tmp.cert_req = 2;
3829
69
                s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
3830
69
                if (!ssl3_digest_cached_records(s, 0)) {
3831
                    /* SSLfatal() already called */
3832
0
                    return WORK_ERROR;
3833
0
                }
3834
69
            }
3835
69
        }
3836
3837
69
        if (!SSL_CONNECTION_IS_TLS13(s)
3838
0
            || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
3839
69
            s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
3840
3841
69
        if (s->post_handshake_auth == SSL_PHA_REQUESTED)
3842
0
            return WORK_FINISHED_STOP;
3843
69
        return WORK_FINISHED_CONTINUE;
3844
69
    }
3845
3846
    /* Shouldn't ever get here */
3847
69
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3848
0
    return WORK_ERROR;
3849
69
}
3850
3851
CON_FUNC_RETURN tls_construct_client_certificate(SSL_CONNECTION *s,
3852
    WPACKET *pkt)
3853
34
{
3854
34
    CERT_PKEY *cpk = NULL;
3855
34
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3856
3857
34
    if (SSL_CONNECTION_IS_TLS13(s)) {
3858
0
        if (s->pha_context == NULL) {
3859
            /* no context available, add 0-length context */
3860
0
            if (!WPACKET_put_bytes_u8(pkt, 0)) {
3861
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3862
0
                return CON_FUNC_ERROR;
3863
0
            }
3864
0
        } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
3865
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3866
0
            return CON_FUNC_ERROR;
3867
0
        }
3868
0
    }
3869
34
    if (s->s3.tmp.cert_req != 2)
3870
0
        cpk = s->cert->key;
3871
34
    switch (s->ext.client_cert_type) {
3872
0
    case TLSEXT_cert_type_rpk:
3873
0
        if (!tls_output_rpk(s, pkt, cpk)) {
3874
            /* SSLfatal() already called */
3875
0
            return CON_FUNC_ERROR;
3876
0
        }
3877
0
        break;
3878
34
    case TLSEXT_cert_type_x509:
3879
34
        if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) {
3880
            /* SSLfatal() already called */
3881
0
            return CON_FUNC_ERROR;
3882
0
        }
3883
34
        break;
3884
34
    default:
3885
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3886
0
        return CON_FUNC_ERROR;
3887
34
    }
3888
3889
    /*
3890
     * If we attempted to write early data or we're in middlebox compat mode
3891
     * then we deferred changing the handshake write keys to the last possible
3892
     * moment. We need to do it now.
3893
     */
3894
34
    if (SSL_CONNECTION_IS_TLS13(s)
3895
0
        && !SSL_IS_QUIC_HANDSHAKE(s)
3896
0
        && SSL_IS_FIRST_HANDSHAKE(s)
3897
0
        && (s->early_data_state != SSL_EARLY_DATA_NONE
3898
0
            || (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
3899
0
        && (!ssl->method->ssl3_enc->change_cipher_state(s,
3900
0
            SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
3901
        /*
3902
         * This is a fatal error, which leaves enc_write_ctx in an inconsistent
3903
         * state and thus ssl3_send_alert may crash.
3904
         */
3905
0
        SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
3906
0
        return CON_FUNC_ERROR;
3907
0
    }
3908
3909
34
    return CON_FUNC_SUCCESS;
3910
34
}
3911
3912
#ifndef OPENSSL_NO_COMP_ALG
3913
CON_FUNC_RETURN tls_construct_client_compressed_certificate(SSL_CONNECTION *sc,
3914
    WPACKET *pkt)
3915
{
3916
    SSL *ssl = SSL_CONNECTION_GET_SSL(sc);
3917
    WPACKET tmppkt;
3918
    BUF_MEM *buf = NULL;
3919
    size_t length;
3920
    size_t max_length;
3921
    COMP_METHOD *method;
3922
    COMP_CTX *comp = NULL;
3923
    int comp_len;
3924
    int ret = 0;
3925
    int alg = sc->ext.compress_certificate_from_peer[0];
3926
3927
    /* Note that sc->s3.tmp.cert_req == 2 is checked in write transition */
3928
3929
    if ((buf = BUF_MEM_new()) == NULL || !WPACKET_init(&tmppkt, buf))
3930
        goto err;
3931
3932
    /* Use the |tmppkt| for the to-be-compressed data */
3933
    if (sc->pha_context == NULL) {
3934
        /* no context available, add 0-length context */
3935
        if (!WPACKET_put_bytes_u8(&tmppkt, 0))
3936
            goto err;
3937
    } else if (!WPACKET_sub_memcpy_u8(&tmppkt, sc->pha_context, sc->pha_context_len))
3938
        goto err;
3939
3940
    if (!ssl3_output_cert_chain(sc, &tmppkt, sc->cert->key, 0)) {
3941
        /* SSLfatal() already called */
3942
        goto out;
3943
    }
3944
3945
    /* continue with the real |pkt| */
3946
    if (!WPACKET_put_bytes_u16(pkt, alg)
3947
        || !WPACKET_get_total_written(&tmppkt, &length)
3948
        || !WPACKET_put_bytes_u24(pkt, length))
3949
        goto err;
3950
3951
    switch (alg) {
3952
    case TLSEXT_comp_cert_zlib:
3953
        method = COMP_zlib_oneshot();
3954
        break;
3955
    case TLSEXT_comp_cert_brotli:
3956
        method = COMP_brotli_oneshot();
3957
        break;
3958
    case TLSEXT_comp_cert_zstd:
3959
        method = COMP_zstd_oneshot();
3960
        break;
3961
    default:
3962
        goto err;
3963
    }
3964
    max_length = ossl_calculate_comp_expansion(alg, length);
3965
3966
    if ((comp = COMP_CTX_new(method)) == NULL
3967
        || !WPACKET_start_sub_packet_u24(pkt)
3968
        || !WPACKET_reserve_bytes(pkt, max_length, NULL))
3969
        goto err;
3970
3971
    comp_len = COMP_compress_block(comp, WPACKET_get_curr(pkt), (int)max_length,
3972
        (unsigned char *)buf->data, (int)length);
3973
    if (comp_len <= 0)
3974
        goto err;
3975
3976
    if (!WPACKET_allocate_bytes(pkt, comp_len, NULL)
3977
        || !WPACKET_close(pkt))
3978
        goto err;
3979
3980
    /*
3981
     * If we attempted to write early data or we're in middlebox compat mode
3982
     * then we deferred changing the handshake write keys to the last possible
3983
     * moment. We need to do it now.
3984
     */
3985
    if (SSL_IS_FIRST_HANDSHAKE(sc)
3986
        && !SSL_IS_QUIC_HANDSHAKE(sc)
3987
        && (sc->early_data_state != SSL_EARLY_DATA_NONE
3988
            || (sc->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
3989
        && (!ssl->method->ssl3_enc->change_cipher_state(sc,
3990
            SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
3991
        /*
3992
         * This is a fatal error, which leaves sc->enc_write_ctx in an
3993
         * inconsistent state and thus ssl3_send_alert may crash.
3994
         */
3995
        SSLfatal(sc, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
3996
        goto out;
3997
    }
3998
    ret = 1;
3999
    goto out;
4000
4001
err:
4002
    SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4003
out:
4004
    if (buf != NULL) {
4005
        /* If |buf| is NULL, then |tmppkt| could not have been initialized */
4006
        WPACKET_cleanup(&tmppkt);
4007
    }
4008
    BUF_MEM_free(buf);
4009
    COMP_CTX_free(comp);
4010
    return ret;
4011
}
4012
#endif
4013
4014
int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s)
4015
29.7k
{
4016
29.7k
    const SSL_CERT_LOOKUP *clu;
4017
29.7k
    size_t idx;
4018
29.7k
    long alg_k, alg_a;
4019
29.7k
    EVP_PKEY *pkey;
4020
4021
29.7k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
4022
29.7k
    alg_a = s->s3.tmp.new_cipher->algorithm_auth;
4023
4024
    /* we don't have a certificate */
4025
29.7k
    if (!(alg_a & SSL_aCERT))
4026
19.2k
        return 1;
4027
4028
    /* This is the passed certificate */
4029
10.4k
    pkey = tls_get_peer_pkey(s);
4030
10.4k
    clu = ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s));
4031
4032
    /* Check certificate is recognised and suitable for cipher */
4033
10.4k
    if (clu == NULL || (alg_a & clu->amask) == 0) {
4034
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT);
4035
0
        return 0;
4036
0
    }
4037
4038
10.4k
    if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
4039
9
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
4040
9
            SSL_R_MISSING_RSA_ENCRYPTING_CERT);
4041
9
        return 0;
4042
9
    }
4043
4044
10.4k
    if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) {
4045
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4046
0
        return 0;
4047
0
    }
4048
4049
    /* Early out to skip the checks below */
4050
10.4k
    if (s->session->peer_rpk != NULL)
4051
0
        return 1;
4052
4053
10.4k
    if (clu->amask & SSL_aECDSA) {
4054
0
        if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
4055
0
            return 1;
4056
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT);
4057
0
        return 0;
4058
0
    }
4059
4060
10.4k
    return 1;
4061
10.4k
}
4062
4063
#ifndef OPENSSL_NO_NEXTPROTONEG
4064
CON_FUNC_RETURN tls_construct_next_proto(SSL_CONNECTION *s, WPACKET *pkt)
4065
0
{
4066
0
    size_t len, padding_len;
4067
0
    unsigned char *padding = NULL;
4068
4069
0
    len = s->ext.npn_len;
4070
0
    padding_len = 32 - ((len + 2) % 32);
4071
4072
0
    if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
4073
0
        || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
4074
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4075
0
        return CON_FUNC_ERROR;
4076
0
    }
4077
4078
0
    memset(padding, 0, padding_len);
4079
4080
0
    return CON_FUNC_SUCCESS;
4081
0
}
4082
#endif
4083
4084
MSG_PROCESS_RETURN tls_process_hello_req(SSL_CONNECTION *s, PACKET *pkt)
4085
1.28k
{
4086
1.28k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
4087
4088
1.28k
    if (PACKET_remaining(pkt) > 0) {
4089
        /* should contain no data */
4090
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4091
0
        return MSG_PROCESS_ERROR;
4092
0
    }
4093
4094
1.28k
    if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
4095
0
        ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
4096
0
        return MSG_PROCESS_FINISHED_READING;
4097
0
    }
4098
4099
    /*
4100
     * This is a historical discrepancy (not in the RFC) maintained for
4101
     * compatibility reasons. If a TLS client receives a HelloRequest it will
4102
     * attempt an abbreviated handshake. However if a DTLS client receives a
4103
     * HelloRequest it will do a full handshake. Either behaviour is reasonable
4104
     * but doing one for TLS and another for DTLS is odd.
4105
     */
4106
1.28k
    if (SSL_CONNECTION_IS_DTLS(s))
4107
0
        SSL_renegotiate(ssl);
4108
1.28k
    else
4109
1.28k
        SSL_renegotiate_abbreviated(ssl);
4110
4111
1.28k
    return MSG_PROCESS_FINISHED_READING;
4112
1.28k
}
4113
4114
static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
4115
    PACKET *pkt)
4116
21.5k
{
4117
21.5k
    PACKET extensions;
4118
21.5k
    RAW_EXTENSION *rawexts = NULL;
4119
4120
21.5k
    if (!PACKET_as_length_prefixed_2(pkt, &extensions)
4121
21.4k
        || PACKET_remaining(pkt) != 0) {
4122
83
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4123
83
        goto err;
4124
83
    }
4125
4126
21.4k
    if (!tls_collect_extensions(s, &extensions,
4127
21.4k
            SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
4128
21.4k
            NULL, 1)
4129
21.4k
        || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
4130
21.4k
            rawexts, NULL, 0, 1)) {
4131
        /* SSLfatal() already called */
4132
1.28k
        goto err;
4133
1.28k
    }
4134
4135
20.1k
    OPENSSL_free(rawexts);
4136
20.1k
    return MSG_PROCESS_CONTINUE_READING;
4137
4138
1.37k
err:
4139
1.37k
    OPENSSL_free(rawexts);
4140
1.37k
    return MSG_PROCESS_ERROR;
4141
21.4k
}
4142
4143
int ssl_do_client_cert_cb(SSL_CONNECTION *s, X509 **px509, EVP_PKEY **ppkey)
4144
78
{
4145
78
    int i = 0;
4146
78
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
4147
4148
78
#ifndef OPENSSL_NO_ENGINE
4149
78
    if (sctx->client_cert_engine) {
4150
0
        i = tls_engine_load_ssl_client_cert(s, px509, ppkey);
4151
0
        if (i != 0)
4152
0
            return i;
4153
0
    }
4154
78
#endif
4155
78
    if (sctx->client_cert_cb)
4156
0
        i = sctx->client_cert_cb(SSL_CONNECTION_GET_USER_SSL(s), px509, ppkey);
4157
78
    return i;
4158
78
}
4159
4160
int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
4161
    WPACKET *pkt)
4162
68.9k
{
4163
68.9k
    int i;
4164
68.9k
    size_t totlen = 0, len, maxlen, maxverok = 0;
4165
68.9k
    int empty_reneg_info_scsv = !s->renegotiate
4166
68.0k
        && !SSL_CONNECTION_IS_DTLS(s)
4167
50.5k
        && ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL)
4168
20.3k
        && s->min_proto_version <= TLS1_VERSION;
4169
68.9k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
4170
4171
    /* Set disabled masks for this session */
4172
68.9k
    if (!ssl_set_client_disabled(s)) {
4173
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE);
4174
0
        return 0;
4175
0
    }
4176
4177
68.9k
    if (sk == NULL) {
4178
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4179
0
        return 0;
4180
0
    }
4181
4182
#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
4183
#if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
4184
#error Max cipher length too short
4185
#endif
4186
    /*
4187
     * Some servers hang if client hello > 256 bytes as hack workaround
4188
     * chop number of supported ciphers to keep it well below this if we
4189
     * use TLS v1.2
4190
     */
4191
    if (TLS1_get_version(ssl) >= TLS1_2_VERSION)
4192
        maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
4193
    else
4194
#endif
4195
        /* Maximum length that can be stored in 2 bytes. Length must be even */
4196
68.9k
        maxlen = 0xfffe;
4197
4198
68.9k
    if (empty_reneg_info_scsv)
4199
20.3k
        maxlen -= 2;
4200
68.9k
    if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
4201
0
        maxlen -= 2;
4202
4203
6.81M
    for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
4204
6.74M
        const SSL_CIPHER *c;
4205
4206
6.74M
        c = sk_SSL_CIPHER_value(sk, i);
4207
        /* Skip disabled ciphers */
4208
6.74M
        if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
4209
2.92M
            continue;
4210
4211
3.82M
        if (!ssl->method->put_cipher_by_char(c, pkt, &len)) {
4212
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4213
0
            return 0;
4214
0
        }
4215
4216
        /* Sanity check that the maximum version we offer has ciphers enabled */
4217
3.82M
        if (!maxverok) {
4218
68.9k
            int minproto = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls;
4219
68.9k
            int maxproto = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls;
4220
4221
68.9k
            if (ssl_version_cmp(s, maxproto, s->s3.tmp.max_ver) >= 0
4222
68.9k
                && ssl_version_cmp(s, minproto, s->s3.tmp.max_ver) <= 0)
4223
68.9k
                maxverok = 1;
4224
68.9k
        }
4225
4226
3.82M
        totlen += len;
4227
3.82M
    }
4228
4229
68.9k
    if (totlen == 0 || !maxverok) {
4230
0
        const char *maxvertext = !maxverok
4231
0
            ? "No ciphers enabled for max supported SSL/TLS version"
4232
0
            : NULL;
4233
4234
0
        SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE,
4235
0
            maxvertext);
4236
0
        return 0;
4237
0
    }
4238
4239
68.9k
    if (totlen != 0) {
4240
68.9k
        if (empty_reneg_info_scsv) {
4241
20.3k
            static const SSL_CIPHER scsv = {
4242
20.3k
                0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
4243
20.3k
            };
4244
20.3k
            if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
4245
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4246
0
                return 0;
4247
0
            }
4248
20.3k
        }
4249
68.9k
        if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
4250
0
            static const SSL_CIPHER scsv = {
4251
0
                0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
4252
0
            };
4253
0
            if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
4254
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4255
0
                return 0;
4256
0
            }
4257
0
        }
4258
68.9k
    }
4259
4260
68.9k
    return 1;
4261
68.9k
}
4262
4263
CON_FUNC_RETURN tls_construct_end_of_early_data(SSL_CONNECTION *s, WPACKET *pkt)
4264
0
{
4265
0
    if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
4266
0
        && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
4267
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
4268
0
        return CON_FUNC_ERROR;
4269
0
    }
4270
4271
0
    s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
4272
0
    return CON_FUNC_SUCCESS;
4273
0
}