Coverage Report

Created: 2026-08-31 06:56

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