Coverage Report

Created: 2026-09-12 06:55

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