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