Coverage Report

Created: 2025-11-16 06:40

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