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