Coverage Report

Created: 2026-10-28 06:28

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