Coverage Report

Created: 2025-12-31 06:58

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