Coverage Report

Created: 2026-09-12 06:55

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