Coverage Report

Created: 2026-10-16 07:31

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