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