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