Coverage Report

Created: 2026-10-28 06:28

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