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