Coverage Report

Created: 2025-11-16 06:40

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