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