2 * Copyright 1995-2016 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.
6 * Licensed under the OpenSSL license (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
13 #include "../ssl_locl.h"
14 #include "statem_locl.h"
15 #include "internal/constant_time_locl.h"
16 #include <openssl/buffer.h>
17 #include <openssl/rand.h>
18 #include <openssl/objects.h>
19 #include <openssl/evp.h>
20 #include <openssl/hmac.h>
21 #include <openssl/x509.h>
22 #include <openssl/dh.h>
23 #include <openssl/bn.h>
24 #include <openssl/md5.h>
26 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
29 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
30 * handshake state transitions when a TLSv1.3 server is reading messages from
31 * the client. The message type that the client has sent is provided in |mt|.
32 * The current state is in |s->statem.hand_state|.
34 * Return values are 1 for success (transition allowed) and 0 on error
35 * (transition not allowed)
37 static int ossl_statem_server13_read_transition(SSL *s, int mt)
39 OSSL_STATEM *st = &s->statem;
42 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
43 * not negotiated TLSv1.3 yet, so that case is handled by
44 * ossl_statem_server_read_transition()
46 switch (st->hand_state) {
50 case TLS_ST_EARLY_DATA:
51 if (s->hello_retry_request == SSL_HRR_PENDING) {
52 if (mt == SSL3_MT_CLIENT_HELLO) {
53 st->hand_state = TLS_ST_SR_CLNT_HELLO;
57 } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
58 if (mt == SSL3_MT_END_OF_EARLY_DATA) {
59 st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
66 case TLS_ST_SR_END_OF_EARLY_DATA:
67 case TLS_ST_SW_FINISHED:
68 if (s->s3->tmp.cert_request) {
69 if (mt == SSL3_MT_CERTIFICATE) {
70 st->hand_state = TLS_ST_SR_CERT;
74 if (mt == SSL3_MT_FINISHED) {
75 st->hand_state = TLS_ST_SR_FINISHED;
82 if (s->session->peer == NULL) {
83 if (mt == SSL3_MT_FINISHED) {
84 st->hand_state = TLS_ST_SR_FINISHED;
88 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
89 st->hand_state = TLS_ST_SR_CERT_VRFY;
95 case TLS_ST_SR_CERT_VRFY:
96 if (mt == SSL3_MT_FINISHED) {
97 st->hand_state = TLS_ST_SR_FINISHED;
104 * Its never ok to start processing handshake messages in the middle of
105 * early data (i.e. before we've received the end of early data alert)
107 if (s->early_data_state == SSL_EARLY_DATA_READING)
109 if (mt == SSL3_MT_KEY_UPDATE) {
110 st->hand_state = TLS_ST_SR_KEY_UPDATE;
116 /* No valid transition found */
121 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
122 * handshake state transitions when the server is reading messages from the
123 * client. The message type that the client has sent is provided in |mt|. The
124 * current state is in |s->statem.hand_state|.
126 * Return values are 1 for success (transition allowed) and 0 on error
127 * (transition not allowed)
129 int ossl_statem_server_read_transition(SSL *s, int mt)
131 OSSL_STATEM *st = &s->statem;
133 if (SSL_IS_TLS13(s)) {
134 if (!ossl_statem_server13_read_transition(s, mt))
139 switch (st->hand_state) {
145 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
146 if (mt == SSL3_MT_CLIENT_HELLO) {
147 st->hand_state = TLS_ST_SR_CLNT_HELLO;
152 case TLS_ST_SW_SRVR_DONE:
154 * If we get a CKE message after a ServerDone then either
155 * 1) We didn't request a Certificate
157 * 2) If we did request one then
158 * a) We allow no Certificate to be returned
160 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
161 * list if we requested a certificate)
163 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
164 if (s->s3->tmp.cert_request) {
165 if (s->version == SSL3_VERSION) {
166 if ((s->verify_mode & SSL_VERIFY_PEER)
167 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
169 * This isn't an unexpected message as such - we're just
170 * not going to accept it because we require a client
173 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
174 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
175 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
178 st->hand_state = TLS_ST_SR_KEY_EXCH;
182 st->hand_state = TLS_ST_SR_KEY_EXCH;
185 } else if (s->s3->tmp.cert_request) {
186 if (mt == SSL3_MT_CERTIFICATE) {
187 st->hand_state = TLS_ST_SR_CERT;
194 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
195 st->hand_state = TLS_ST_SR_KEY_EXCH;
200 case TLS_ST_SR_KEY_EXCH:
202 * We should only process a CertificateVerify message if we have
203 * received a Certificate from the client. If so then |s->session->peer|
204 * will be non NULL. In some instances a CertificateVerify message is
205 * not required even if the peer has sent a Certificate (e.g. such as in
206 * the case of static DH). In that case |st->no_cert_verify| should be
209 if (s->session->peer == NULL || st->no_cert_verify) {
210 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
212 * For the ECDH ciphersuites when the client sends its ECDH
213 * pub key in a certificate, the CertificateVerify message is
214 * not sent. Also for GOST ciphersuites when the client uses
215 * its key from the certificate for key exchange.
217 st->hand_state = TLS_ST_SR_CHANGE;
221 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
222 st->hand_state = TLS_ST_SR_CERT_VRFY;
228 case TLS_ST_SR_CERT_VRFY:
229 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
230 st->hand_state = TLS_ST_SR_CHANGE;
235 case TLS_ST_SR_CHANGE:
236 #ifndef OPENSSL_NO_NEXTPROTONEG
237 if (s->s3->npn_seen) {
238 if (mt == SSL3_MT_NEXT_PROTO) {
239 st->hand_state = TLS_ST_SR_NEXT_PROTO;
244 if (mt == SSL3_MT_FINISHED) {
245 st->hand_state = TLS_ST_SR_FINISHED;
248 #ifndef OPENSSL_NO_NEXTPROTONEG
253 #ifndef OPENSSL_NO_NEXTPROTONEG
254 case TLS_ST_SR_NEXT_PROTO:
255 if (mt == SSL3_MT_FINISHED) {
256 st->hand_state = TLS_ST_SR_FINISHED;
262 case TLS_ST_SW_FINISHED:
263 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
264 st->hand_state = TLS_ST_SR_CHANGE;
271 /* No valid transition found */
272 SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
273 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
274 SSL_R_UNEXPECTED_MESSAGE);
279 * Should we send a ServerKeyExchange message?
281 * Valid return values are:
285 static int send_server_key_exchange(SSL *s)
287 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
290 * only send a ServerKeyExchange if DH or fortezza but we have a
291 * sign only certificate PSK: may send PSK identity hints For
292 * ECC ciphersuites, we send a serverKeyExchange message only if
293 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
294 * the server certificate contains the server's public key for
297 if (alg_k & (SSL_kDHE | SSL_kECDHE)
299 * PSK: send ServerKeyExchange if PSK identity hint if
302 #ifndef OPENSSL_NO_PSK
303 /* Only send SKE if we have identity hint for plain PSK */
304 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
305 && s->cert->psk_identity_hint)
306 /* For other PSK always send SKE */
307 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
309 #ifndef OPENSSL_NO_SRP
310 /* SRP: send ServerKeyExchange */
311 || (alg_k & SSL_kSRP)
321 * Should we send a CertificateRequest message?
323 * Valid return values are:
327 static int send_certificate_request(SSL *s)
330 /* don't request cert unless asked for it: */
331 s->verify_mode & SSL_VERIFY_PEER
333 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
334 * during re-negotiation:
336 && (s->s3->tmp.finish_md_len == 0 ||
337 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
339 * never request cert in anonymous ciphersuites (see
340 * section "Certificate request" in SSL 3 drafts and in
343 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
345 * ... except when the application insists on
346 * verification (against the specs, but statem_clnt.c accepts
349 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
350 /* don't request certificate for SRP auth */
351 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
353 * With normal PSK Certificates and Certificate Requests
356 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
364 * ossl_statem_server13_write_transition() works out what handshake state to
365 * move to next when a TLSv1.3 server is writing messages to be sent to the
368 static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
370 OSSL_STATEM *st = &s->statem;
373 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
374 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
377 switch (st->hand_state) {
379 /* Shouldn't happen */
380 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
381 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION,
382 ERR_R_INTERNAL_ERROR);
383 return WRITE_TRAN_ERROR;
386 if (s->key_update != SSL_KEY_UPDATE_NONE) {
387 st->hand_state = TLS_ST_SW_KEY_UPDATE;
388 return WRITE_TRAN_CONTINUE;
390 /* Try to read from the client instead */
391 return WRITE_TRAN_FINISHED;
393 case TLS_ST_SR_CLNT_HELLO:
394 st->hand_state = TLS_ST_SW_SRVR_HELLO;
395 return WRITE_TRAN_CONTINUE;
397 case TLS_ST_SW_SRVR_HELLO:
398 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
399 && s->hello_retry_request != SSL_HRR_COMPLETE)
400 st->hand_state = TLS_ST_SW_CHANGE;
401 else if (s->hello_retry_request == SSL_HRR_PENDING)
402 st->hand_state = TLS_ST_EARLY_DATA;
404 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
405 return WRITE_TRAN_CONTINUE;
407 case TLS_ST_SW_CHANGE:
408 if (s->hello_retry_request == SSL_HRR_PENDING)
409 st->hand_state = TLS_ST_EARLY_DATA;
411 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
412 return WRITE_TRAN_CONTINUE;
414 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
416 st->hand_state = TLS_ST_SW_FINISHED;
417 else if (send_certificate_request(s))
418 st->hand_state = TLS_ST_SW_CERT_REQ;
420 st->hand_state = TLS_ST_SW_CERT;
422 return WRITE_TRAN_CONTINUE;
424 case TLS_ST_SW_CERT_REQ:
425 st->hand_state = TLS_ST_SW_CERT;
426 return WRITE_TRAN_CONTINUE;
429 st->hand_state = TLS_ST_SW_CERT_VRFY;
430 return WRITE_TRAN_CONTINUE;
432 case TLS_ST_SW_CERT_VRFY:
433 st->hand_state = TLS_ST_SW_FINISHED;
434 return WRITE_TRAN_CONTINUE;
436 case TLS_ST_SW_FINISHED:
437 st->hand_state = TLS_ST_EARLY_DATA;
438 return WRITE_TRAN_CONTINUE;
440 case TLS_ST_EARLY_DATA:
441 return WRITE_TRAN_FINISHED;
443 case TLS_ST_SR_FINISHED:
445 * Technically we have finished the handshake at this point, but we're
446 * going to remain "in_init" for now and write out the session ticket
448 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
449 * and give the application the opportunity to delay sending the
452 st->hand_state = TLS_ST_SW_SESSION_TICKET;
453 return WRITE_TRAN_CONTINUE;
455 case TLS_ST_SR_KEY_UPDATE:
456 if (s->key_update != SSL_KEY_UPDATE_NONE) {
457 st->hand_state = TLS_ST_SW_KEY_UPDATE;
458 return WRITE_TRAN_CONTINUE;
462 case TLS_ST_SW_KEY_UPDATE:
463 case TLS_ST_SW_SESSION_TICKET:
464 st->hand_state = TLS_ST_OK;
465 return WRITE_TRAN_CONTINUE;
470 * ossl_statem_server_write_transition() works out what handshake state to move
471 * to next when the server is writing messages to be sent to the client.
473 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
475 OSSL_STATEM *st = &s->statem;
478 * Note that before the ClientHello we don't know what version we are going
479 * to negotiate yet, so we don't take this branch until later
483 return ossl_statem_server13_write_transition(s);
485 switch (st->hand_state) {
487 /* Shouldn't happen */
488 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
489 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION,
490 ERR_R_INTERNAL_ERROR);
491 return WRITE_TRAN_ERROR;
494 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
495 /* We must be trying to renegotiate */
496 st->hand_state = TLS_ST_SW_HELLO_REQ;
497 st->request_state = TLS_ST_BEFORE;
498 return WRITE_TRAN_CONTINUE;
500 /* Must be an incoming ClientHello */
501 if (!tls_setup_handshake(s)) {
502 /* SSLfatal() already called */
503 return WRITE_TRAN_ERROR;
508 /* Just go straight to trying to read from the client */
509 return WRITE_TRAN_FINISHED;
511 case TLS_ST_SW_HELLO_REQ:
512 st->hand_state = TLS_ST_OK;
513 return WRITE_TRAN_CONTINUE;
515 case TLS_ST_SR_CLNT_HELLO:
516 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
517 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
518 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
520 st->hand_state = TLS_ST_SW_SRVR_HELLO;
521 return WRITE_TRAN_CONTINUE;
523 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
524 return WRITE_TRAN_FINISHED;
526 case TLS_ST_SW_SRVR_HELLO:
528 if (s->ext.ticket_expected)
529 st->hand_state = TLS_ST_SW_SESSION_TICKET;
531 st->hand_state = TLS_ST_SW_CHANGE;
533 /* Check if it is anon DH or anon ECDH, */
534 /* normal PSK or SRP */
535 if (!(s->s3->tmp.new_cipher->algorithm_auth &
536 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
537 st->hand_state = TLS_ST_SW_CERT;
538 } else if (send_server_key_exchange(s)) {
539 st->hand_state = TLS_ST_SW_KEY_EXCH;
540 } else if (send_certificate_request(s)) {
541 st->hand_state = TLS_ST_SW_CERT_REQ;
543 st->hand_state = TLS_ST_SW_SRVR_DONE;
546 return WRITE_TRAN_CONTINUE;
549 if (s->ext.status_expected) {
550 st->hand_state = TLS_ST_SW_CERT_STATUS;
551 return WRITE_TRAN_CONTINUE;
555 case TLS_ST_SW_CERT_STATUS:
556 if (send_server_key_exchange(s)) {
557 st->hand_state = TLS_ST_SW_KEY_EXCH;
558 return WRITE_TRAN_CONTINUE;
562 case TLS_ST_SW_KEY_EXCH:
563 if (send_certificate_request(s)) {
564 st->hand_state = TLS_ST_SW_CERT_REQ;
565 return WRITE_TRAN_CONTINUE;
569 case TLS_ST_SW_CERT_REQ:
570 st->hand_state = TLS_ST_SW_SRVR_DONE;
571 return WRITE_TRAN_CONTINUE;
573 case TLS_ST_SW_SRVR_DONE:
574 return WRITE_TRAN_FINISHED;
576 case TLS_ST_SR_FINISHED:
578 st->hand_state = TLS_ST_OK;
579 return WRITE_TRAN_CONTINUE;
580 } else if (s->ext.ticket_expected) {
581 st->hand_state = TLS_ST_SW_SESSION_TICKET;
583 st->hand_state = TLS_ST_SW_CHANGE;
585 return WRITE_TRAN_CONTINUE;
587 case TLS_ST_SW_SESSION_TICKET:
588 st->hand_state = TLS_ST_SW_CHANGE;
589 return WRITE_TRAN_CONTINUE;
591 case TLS_ST_SW_CHANGE:
592 st->hand_state = TLS_ST_SW_FINISHED;
593 return WRITE_TRAN_CONTINUE;
595 case TLS_ST_SW_FINISHED:
597 return WRITE_TRAN_FINISHED;
599 st->hand_state = TLS_ST_OK;
600 return WRITE_TRAN_CONTINUE;
605 * Perform any pre work that needs to be done prior to sending a message from
606 * the server to the client.
608 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
610 OSSL_STATEM *st = &s->statem;
612 switch (st->hand_state) {
614 /* No pre work to be done */
617 case TLS_ST_SW_HELLO_REQ:
620 dtls1_clear_sent_buffer(s);
623 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
625 if (SSL_IS_DTLS(s)) {
626 dtls1_clear_sent_buffer(s);
627 /* We don't buffer this message so don't use the timer */
632 case TLS_ST_SW_SRVR_HELLO:
633 if (SSL_IS_DTLS(s)) {
635 * Messages we write from now on should be buffered and
636 * retransmitted if necessary, so we need to use the timer now
642 case TLS_ST_SW_SRVR_DONE:
643 #ifndef OPENSSL_NO_SCTP
644 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
645 /* Calls SSLfatal() as required */
646 return dtls_wait_for_dry(s);
649 return WORK_FINISHED_CONTINUE;
651 case TLS_ST_SW_SESSION_TICKET:
652 if (SSL_IS_TLS13(s)) {
654 * Actually this is the end of the handshake, but we're going
655 * straight into writing the session ticket out. So we finish off
656 * the handshake, but keep the various buffers active.
658 * Calls SSLfatal as required.
660 return tls_finish_handshake(s, wst, 0);
661 } if (SSL_IS_DTLS(s)) {
663 * We're into the last flight. We don't retransmit the last flight
664 * unless we need to, so we don't use the timer
670 case TLS_ST_SW_CHANGE:
673 s->session->cipher = s->s3->tmp.new_cipher;
674 if (!s->method->ssl3_enc->setup_key_block(s)) {
675 /* SSLfatal() already called */
678 if (SSL_IS_DTLS(s)) {
680 * We're into the last flight. We don't retransmit the last flight
681 * unless we need to, so we don't use the timer. This might have
682 * already been set to 0 if we sent a NewSessionTicket message,
683 * but we'll set it again here in case we didn't.
687 return WORK_FINISHED_CONTINUE;
689 case TLS_ST_EARLY_DATA:
690 if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING)
691 return WORK_FINISHED_CONTINUE;
695 /* Calls SSLfatal() as required */
696 return tls_finish_handshake(s, wst, 1);
699 return WORK_FINISHED_CONTINUE;
703 * Perform any work that needs to be done after sending a message from the
704 * server to the client.
706 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
708 OSSL_STATEM *st = &s->statem;
712 switch (st->hand_state) {
714 /* No post work to be done */
717 case TLS_ST_SW_HELLO_REQ:
718 if (statem_flush(s) != 1)
720 if (!ssl3_init_finished_mac(s)) {
721 /* SSLfatal() already called */
726 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
727 if (statem_flush(s) != 1)
729 /* HelloVerifyRequest resets Finished MAC */
730 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
731 /* SSLfatal() already called */
735 * The next message should be another ClientHello which we need to
736 * treat like it was the first packet
741 case TLS_ST_SW_SRVR_HELLO:
742 if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
743 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
744 && statem_flush(s) != 1)
748 #ifndef OPENSSL_NO_SCTP
749 if (SSL_IS_DTLS(s) && s->hit) {
750 unsigned char sctpauthkey[64];
751 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
754 * Add new shared key for SCTP-Auth, will be ignored if no
757 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
758 sizeof(DTLS1_SCTP_AUTH_LABEL));
760 if (SSL_export_keying_material(s, sctpauthkey,
761 sizeof(sctpauthkey), labelbuffer,
762 sizeof(labelbuffer), NULL, 0,
764 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
765 SSL_F_OSSL_STATEM_SERVER_POST_WORK,
766 ERR_R_INTERNAL_ERROR);
770 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
771 sizeof(sctpauthkey), sctpauthkey);
775 || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
776 && s->hello_retry_request != SSL_HRR_COMPLETE))
780 case TLS_ST_SW_CHANGE:
781 if (s->hello_retry_request == SSL_HRR_PENDING) {
782 if (!statem_flush(s))
787 * TODO(TLS1.3): This actually causes a problem. We don't yet know
788 * whether the next record we are going to receive is an unencrypted
789 * alert, or an encrypted handshake message. We're going to need
790 * something clever in the record layer for this.
792 if (SSL_IS_TLS13(s)) {
793 if (!s->method->ssl3_enc->setup_key_block(s)
794 || !s->method->ssl3_enc->change_cipher_state(s,
795 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
796 /* SSLfatal() already called */
800 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
801 && !s->method->ssl3_enc->change_cipher_state(s,
802 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
803 /* SSLfatal() already called */
809 #ifndef OPENSSL_NO_SCTP
810 if (SSL_IS_DTLS(s) && !s->hit) {
812 * Change to new shared key of SCTP-Auth, will be ignored if
815 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
819 if (!s->method->ssl3_enc->change_cipher_state(s,
820 SSL3_CHANGE_CIPHER_SERVER_WRITE))
822 /* SSLfatal() already called */
827 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
830 case TLS_ST_SW_SRVR_DONE:
831 if (statem_flush(s) != 1)
835 case TLS_ST_SW_FINISHED:
836 if (statem_flush(s) != 1)
838 #ifndef OPENSSL_NO_SCTP
839 if (SSL_IS_DTLS(s) && s->hit) {
841 * Change to new shared key of SCTP-Auth, will be ignored if
844 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
848 if (SSL_IS_TLS13(s)) {
849 if (!s->method->ssl3_enc->generate_master_secret(s,
850 s->master_secret, s->handshake_secret, 0,
851 &s->session->master_key_length)
852 || !s->method->ssl3_enc->change_cipher_state(s,
853 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
854 /* SSLfatal() already called */
859 case TLS_ST_SW_KEY_UPDATE:
860 if (statem_flush(s) != 1)
862 if (!tls13_update_key(s, 1)) {
863 /* SSLfatal() already called */
868 case TLS_ST_SW_SESSION_TICKET:
869 if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
874 return WORK_FINISHED_CONTINUE;
878 * Get the message construction function and message type for sending from the
881 * Valid return values are:
885 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
886 confunc_f *confunc, int *mt)
888 OSSL_STATEM *st = &s->statem;
890 switch (st->hand_state) {
892 /* Shouldn't happen */
893 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
894 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE,
895 SSL_R_BAD_HANDSHAKE_STATE);
898 case TLS_ST_SW_CHANGE:
900 *confunc = dtls_construct_change_cipher_spec;
902 *confunc = tls_construct_change_cipher_spec;
903 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
906 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
907 *confunc = dtls_construct_hello_verify_request;
908 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
911 case TLS_ST_SW_HELLO_REQ:
912 /* No construction function needed */
914 *mt = SSL3_MT_HELLO_REQUEST;
917 case TLS_ST_SW_SRVR_HELLO:
918 *confunc = tls_construct_server_hello;
919 *mt = SSL3_MT_SERVER_HELLO;
923 *confunc = tls_construct_server_certificate;
924 *mt = SSL3_MT_CERTIFICATE;
927 case TLS_ST_SW_CERT_VRFY:
928 *confunc = tls_construct_cert_verify;
929 *mt = SSL3_MT_CERTIFICATE_VERIFY;
933 case TLS_ST_SW_KEY_EXCH:
934 *confunc = tls_construct_server_key_exchange;
935 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
938 case TLS_ST_SW_CERT_REQ:
939 *confunc = tls_construct_certificate_request;
940 *mt = SSL3_MT_CERTIFICATE_REQUEST;
943 case TLS_ST_SW_SRVR_DONE:
944 *confunc = tls_construct_server_done;
945 *mt = SSL3_MT_SERVER_DONE;
948 case TLS_ST_SW_SESSION_TICKET:
949 *confunc = tls_construct_new_session_ticket;
950 *mt = SSL3_MT_NEWSESSION_TICKET;
953 case TLS_ST_SW_CERT_STATUS:
954 *confunc = tls_construct_cert_status;
955 *mt = SSL3_MT_CERTIFICATE_STATUS;
958 case TLS_ST_SW_FINISHED:
959 *confunc = tls_construct_finished;
960 *mt = SSL3_MT_FINISHED;
963 case TLS_ST_EARLY_DATA:
968 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
969 *confunc = tls_construct_encrypted_extensions;
970 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
973 case TLS_ST_SW_KEY_UPDATE:
974 *confunc = tls_construct_key_update;
975 *mt = SSL3_MT_KEY_UPDATE;
983 * Maximum size (excluding the Handshake header) of a ClientHello message,
984 * calculated as follows:
986 * 2 + # client_version
987 * 32 + # only valid length for random
988 * 1 + # length of session_id
989 * 32 + # maximum size for session_id
990 * 2 + # length of cipher suites
991 * 2^16-2 + # maximum length of cipher suites array
992 * 1 + # length of compression_methods
993 * 2^8-1 + # maximum length of compression methods
994 * 2 + # length of extensions
995 * 2^16-1 # maximum length of extensions
997 #define CLIENT_HELLO_MAX_LENGTH 131396
999 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1000 #define NEXT_PROTO_MAX_LENGTH 514
1003 * Returns the maximum allowed length for the current message that we are
1004 * reading. Excludes the message header.
1006 size_t ossl_statem_server_max_message_size(SSL *s)
1008 OSSL_STATEM *st = &s->statem;
1010 switch (st->hand_state) {
1012 /* Shouldn't happen */
1015 case TLS_ST_SR_CLNT_HELLO:
1016 return CLIENT_HELLO_MAX_LENGTH;
1018 case TLS_ST_SR_END_OF_EARLY_DATA:
1019 return END_OF_EARLY_DATA_MAX_LENGTH;
1021 case TLS_ST_SR_CERT:
1022 return s->max_cert_list;
1024 case TLS_ST_SR_KEY_EXCH:
1025 return CLIENT_KEY_EXCH_MAX_LENGTH;
1027 case TLS_ST_SR_CERT_VRFY:
1028 return SSL3_RT_MAX_PLAIN_LENGTH;
1030 #ifndef OPENSSL_NO_NEXTPROTONEG
1031 case TLS_ST_SR_NEXT_PROTO:
1032 return NEXT_PROTO_MAX_LENGTH;
1035 case TLS_ST_SR_CHANGE:
1036 return CCS_MAX_LENGTH;
1038 case TLS_ST_SR_FINISHED:
1039 return FINISHED_MAX_LENGTH;
1041 case TLS_ST_SR_KEY_UPDATE:
1042 return KEY_UPDATE_MAX_LENGTH;
1047 * Process a message that the server has received from the client.
1049 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1051 OSSL_STATEM *st = &s->statem;
1053 switch (st->hand_state) {
1055 /* Shouldn't happen */
1056 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1057 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE,
1058 ERR_R_INTERNAL_ERROR);
1059 return MSG_PROCESS_ERROR;
1061 case TLS_ST_SR_CLNT_HELLO:
1062 return tls_process_client_hello(s, pkt);
1064 case TLS_ST_SR_END_OF_EARLY_DATA:
1065 return tls_process_end_of_early_data(s, pkt);
1067 case TLS_ST_SR_CERT:
1068 return tls_process_client_certificate(s, pkt);
1070 case TLS_ST_SR_KEY_EXCH:
1071 return tls_process_client_key_exchange(s, pkt);
1073 case TLS_ST_SR_CERT_VRFY:
1074 return tls_process_cert_verify(s, pkt);
1076 #ifndef OPENSSL_NO_NEXTPROTONEG
1077 case TLS_ST_SR_NEXT_PROTO:
1078 return tls_process_next_proto(s, pkt);
1081 case TLS_ST_SR_CHANGE:
1082 return tls_process_change_cipher_spec(s, pkt);
1084 case TLS_ST_SR_FINISHED:
1085 return tls_process_finished(s, pkt);
1087 case TLS_ST_SR_KEY_UPDATE:
1088 return tls_process_key_update(s, pkt);
1094 * Perform any further processing required following the receipt of a message
1097 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1099 OSSL_STATEM *st = &s->statem;
1101 switch (st->hand_state) {
1103 /* Shouldn't happen */
1104 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1105 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE,
1106 ERR_R_INTERNAL_ERROR);
1109 case TLS_ST_SR_CLNT_HELLO:
1110 return tls_post_process_client_hello(s, wst);
1112 case TLS_ST_SR_KEY_EXCH:
1113 return tls_post_process_client_key_exchange(s, wst);
1115 return WORK_FINISHED_CONTINUE;
1118 #ifndef OPENSSL_NO_SRP
1119 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1120 static int ssl_check_srp_ext_ClientHello(SSL *s)
1123 int al = SSL_AD_UNRECOGNIZED_NAME;
1125 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1126 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1127 if (s->srp_ctx.login == NULL) {
1129 * RFC 5054 says SHOULD reject, we do so if There is no srp
1132 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
1133 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1134 SSL_R_PSK_IDENTITY_NOT_FOUND);
1137 ret = SSL_srp_server_param_with_username(s, &al);
1140 if (ret == SSL3_AL_FATAL) {
1141 SSLfatal(s, al, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1142 al == SSL_AD_UNKNOWN_PSK_IDENTITY
1143 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1144 : SSL_R_CLIENTHELLO_TLSEXT);
1153 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1156 /* Always use DTLS 1.0 version: see RFC 6347 */
1157 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1158 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1164 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1166 unsigned int cookie_leni;
1167 if (s->ctx->app_gen_cookie_cb == NULL ||
1168 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1169 &cookie_leni) == 0 ||
1170 cookie_leni > 255) {
1171 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1172 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1175 s->d1->cookie_len = cookie_leni;
1177 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1178 s->d1->cookie_len)) {
1179 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1180 ERR_R_INTERNAL_ERROR);
1187 #ifndef OPENSSL_NO_EC
1189 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1190 * SecureTransport using the TLS extension block in |hello|.
1191 * Safari, since 10.6, sends exactly these extensions, in this order:
1195 * signature_algorithms (for TLSv1.2 only)
1197 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1198 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1199 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1200 * 10.8..10.8.3 (which don't work).
1202 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1204 static const unsigned char kSafariExtensionsBlock[] = {
1205 0x00, 0x0a, /* elliptic_curves extension */
1206 0x00, 0x08, /* 8 bytes */
1207 0x00, 0x06, /* 6 bytes of curve ids */
1208 0x00, 0x17, /* P-256 */
1209 0x00, 0x18, /* P-384 */
1210 0x00, 0x19, /* P-521 */
1212 0x00, 0x0b, /* ec_point_formats */
1213 0x00, 0x02, /* 2 bytes */
1214 0x01, /* 1 point format */
1215 0x00, /* uncompressed */
1216 /* The following is only present in TLS 1.2 */
1217 0x00, 0x0d, /* signature_algorithms */
1218 0x00, 0x0c, /* 12 bytes */
1219 0x00, 0x0a, /* 10 bytes */
1220 0x05, 0x01, /* SHA-384/RSA */
1221 0x04, 0x01, /* SHA-256/RSA */
1222 0x02, 0x01, /* SHA-1/RSA */
1223 0x04, 0x03, /* SHA-256/ECDSA */
1224 0x02, 0x03, /* SHA-1/ECDSA */
1226 /* Length of the common prefix (first two extensions). */
1227 static const size_t kSafariCommonExtensionsLength = 18;
1232 tmppkt = hello->extensions;
1234 if (!PACKET_forward(&tmppkt, 2)
1235 || !PACKET_get_net_2(&tmppkt, &type)
1236 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1240 if (type != TLSEXT_TYPE_server_name)
1243 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1244 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1246 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1249 #endif /* !OPENSSL_NO_EC */
1251 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1253 /* |cookie| will only be initialized for DTLS. */
1254 PACKET session_id, compression, extensions, cookie;
1255 static const unsigned char null_compression = 0;
1256 CLIENTHELLO_MSG *clienthello;
1258 clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1259 if (clienthello == NULL) {
1260 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1261 ERR_R_INTERNAL_ERROR);
1264 /* Check if this is actually an unexpected renegotiation ClientHello */
1265 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1266 if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
1267 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1275 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1277 clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1278 PACKET_null_init(&cookie);
1280 if (clienthello->isv2) {
1283 if (!SSL_IS_FIRST_HANDSHAKE(s)
1284 || s->hello_retry_request != SSL_HRR_NONE) {
1285 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1286 SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1291 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1292 * header is sent directly on the wire, not wrapped as a TLS
1293 * record. Our record layer just processes the message length and passes
1294 * the rest right through. Its format is:
1296 * 0-1 msg_length - decoded by the record layer
1297 * 2 msg_type - s->init_msg points here
1299 * 5-6 cipher_spec_length
1300 * 7-8 session_id_length
1301 * 9-10 challenge_length
1305 if (!PACKET_get_1(pkt, &mt)
1306 || mt != SSL2_MT_CLIENT_HELLO) {
1308 * Should never happen. We should have tested this in the record
1309 * layer in order to have determined that this is a SSLv2 record
1310 * in the first place
1312 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1313 ERR_R_INTERNAL_ERROR);
1318 if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1319 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1320 SSL_R_LENGTH_TOO_SHORT);
1324 /* Parse the message and load client random. */
1325 if (clienthello->isv2) {
1327 * Handle an SSLv2 backwards compatible ClientHello
1328 * Note, this is only for SSLv3+ using the backward compatible format.
1329 * Real SSLv2 is not supported, and is rejected below.
1331 unsigned int ciphersuite_len, session_id_len, challenge_len;
1334 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1335 || !PACKET_get_net_2(pkt, &session_id_len)
1336 || !PACKET_get_net_2(pkt, &challenge_len)) {
1337 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1338 SSL_R_RECORD_LENGTH_MISMATCH);
1342 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1343 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1344 SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1348 if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1350 || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1351 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1352 /* No extensions. */
1353 || PACKET_remaining(pkt) != 0) {
1354 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1355 SSL_R_RECORD_LENGTH_MISMATCH);
1358 clienthello->session_id_len = session_id_len;
1360 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1361 * here rather than sizeof(clienthello->random) because that is the limit
1362 * for SSLv3 and it is fixed. It won't change even if
1363 * sizeof(clienthello->random) does.
1365 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1366 ? SSL3_RANDOM_SIZE : challenge_len;
1367 memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1368 if (!PACKET_copy_bytes(&challenge,
1369 clienthello->random + SSL3_RANDOM_SIZE -
1370 challenge_len, challenge_len)
1371 /* Advertise only null compression. */
1372 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1373 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1374 ERR_R_INTERNAL_ERROR);
1378 PACKET_null_init(&clienthello->extensions);
1380 /* Regular ClientHello. */
1381 if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1382 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1383 || !PACKET_copy_all(&session_id, clienthello->session_id,
1384 SSL_MAX_SSL_SESSION_ID_LENGTH,
1385 &clienthello->session_id_len)) {
1386 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1387 SSL_R_LENGTH_MISMATCH);
1391 if (SSL_IS_DTLS(s)) {
1392 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1393 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1394 SSL_R_LENGTH_MISMATCH);
1397 if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1398 DTLS1_COOKIE_LENGTH,
1399 &clienthello->dtls_cookie_len)) {
1400 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1401 SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1405 * If we require cookies and this ClientHello doesn't contain one,
1406 * just return since we do not want to allocate any memory yet.
1407 * So check cookie length...
1409 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1410 if (clienthello->dtls_cookie_len == 0)
1411 return MSG_PROCESS_FINISHED_READING;
1415 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1416 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1417 SSL_R_LENGTH_MISMATCH);
1421 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1422 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1423 SSL_R_LENGTH_MISMATCH);
1427 /* Could be empty. */
1428 if (PACKET_remaining(pkt) == 0) {
1429 PACKET_null_init(&clienthello->extensions);
1431 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
1432 || PACKET_remaining(pkt) != 0) {
1433 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1434 SSL_R_LENGTH_MISMATCH);
1440 if (!PACKET_copy_all(&compression, clienthello->compressions,
1441 MAX_COMPRESSIONS_SIZE,
1442 &clienthello->compressions_len)) {
1443 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1444 ERR_R_INTERNAL_ERROR);
1448 /* Preserve the raw extensions PACKET for later use */
1449 extensions = clienthello->extensions;
1450 if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
1451 &clienthello->pre_proc_exts,
1452 &clienthello->pre_proc_exts_len, 1)) {
1453 /* SSLfatal already been called */
1456 s->clienthello = clienthello;
1458 return MSG_PROCESS_CONTINUE_PROCESSING;
1461 if (clienthello != NULL)
1462 OPENSSL_free(clienthello->pre_proc_exts);
1463 OPENSSL_free(clienthello);
1465 return MSG_PROCESS_ERROR;
1468 static int tls_early_post_process_client_hello(SSL *s)
1471 int i, al = SSL_AD_INTERNAL_ERROR;
1475 #ifndef OPENSSL_NO_COMP
1476 SSL_COMP *comp = NULL;
1478 const SSL_CIPHER *c;
1479 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1480 STACK_OF(SSL_CIPHER) *scsvs = NULL;
1481 CLIENTHELLO_MSG *clienthello = s->clienthello;
1482 DOWNGRADE dgrd = DOWNGRADE_NONE;
1484 /* Finished parsing the ClientHello, now we can start processing it */
1485 /* Give the ClientHello callback a crack at things */
1486 if (s->ctx->client_hello_cb != NULL) {
1487 /* A failure in the ClientHello callback terminates the connection. */
1488 switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
1489 case SSL_CLIENT_HELLO_SUCCESS:
1491 case SSL_CLIENT_HELLO_RETRY:
1492 s->rwstate = SSL_CLIENT_HELLO_CB;
1494 case SSL_CLIENT_HELLO_ERROR:
1497 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1498 SSL_R_CALLBACK_FAILED);
1503 /* Set up the client_random */
1504 memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
1506 /* Choose the version */
1508 if (clienthello->isv2) {
1509 if (clienthello->legacy_version == SSL2_VERSION
1510 || (clienthello->legacy_version & 0xff00)
1511 != (SSL3_VERSION_MAJOR << 8)) {
1513 * This is real SSLv2 or something completely unknown. We don't
1516 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1517 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1518 SSL_R_UNKNOWN_PROTOCOL);
1522 s->client_version = clienthello->legacy_version;
1525 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1526 * versions are potentially compatible. Version negotiation comes later.
1528 if (!SSL_IS_DTLS(s)) {
1529 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1530 } else if (s->method->version != DTLS_ANY_VERSION &&
1531 DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1532 protverr = SSL_R_VERSION_TOO_LOW;
1538 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1539 /* like ssl3_get_record, send alert using remote version number */
1540 s->version = s->client_version = clienthello->legacy_version;
1542 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1543 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1547 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1548 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1549 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1550 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1551 SSL_R_NOT_ON_RECORD_BOUNDARY);
1555 if (SSL_IS_DTLS(s)) {
1556 /* Empty cookie was already handled above by returning early. */
1557 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1558 if (s->ctx->app_verify_cookie_cb != NULL) {
1559 if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1560 clienthello->dtls_cookie_len) == 0) {
1561 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1562 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1563 SSL_R_COOKIE_MISMATCH);
1565 /* else cookie verification succeeded */
1567 /* default verification */
1568 } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1569 || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1570 s->d1->cookie_len) != 0) {
1571 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1572 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1573 SSL_R_COOKIE_MISMATCH);
1576 s->d1->cookie_verified = 1;
1578 if (s->method->version == DTLS_ANY_VERSION) {
1579 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1580 if (protverr != 0) {
1581 s->version = s->client_version;
1582 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1583 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1591 if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1592 clienthello->isv2) ||
1593 !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
1594 clienthello->isv2, 1)) {
1595 /* SSLfatal() already called */
1599 s->s3->send_connection_binding = 0;
1600 /* Check what signalling cipher-suite values were received. */
1601 if (scsvs != NULL) {
1602 for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1603 c = sk_SSL_CIPHER_value(scsvs, i);
1604 if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1605 if (s->renegotiate) {
1606 /* SCSV is fatal if renegotiating */
1607 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1608 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1609 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1612 s->s3->send_connection_binding = 1;
1613 } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1614 !ssl_check_version_downgrade(s)) {
1616 * This SCSV indicates that the client previously tried
1617 * a higher version. We should fail if the current version
1618 * is an unexpected downgrade, as that indicates that the first
1619 * connection may have been tampered with in order to trigger
1620 * an insecure downgrade.
1622 SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
1623 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1624 SSL_R_INAPPROPRIATE_FALLBACK);
1630 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1631 if (SSL_IS_TLS13(s)) {
1632 const SSL_CIPHER *cipher =
1633 ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
1635 if (cipher == NULL) {
1636 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1637 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1638 SSL_R_NO_SHARED_CIPHER);
1641 if (s->hello_retry_request == SSL_HRR_PENDING
1642 && (s->s3->tmp.new_cipher == NULL
1643 || s->s3->tmp.new_cipher->id != cipher->id)) {
1645 * A previous HRR picked a different ciphersuite to the one we
1646 * just selected. Something must have changed.
1648 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1649 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1653 s->s3->tmp.new_cipher = cipher;
1656 /* We need to do this before getting the session */
1657 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1658 SSL_EXT_CLIENT_HELLO,
1659 clienthello->pre_proc_exts, NULL, 0)) {
1660 /* SSLfatal() already called */
1665 * We don't allow resumption in a backwards compatible ClientHello.
1666 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1668 * Versions before 0.9.7 always allow clients to resume sessions in
1669 * renegotiation. 0.9.7 and later allow this by default, but optionally
1670 * ignore resumption requests with flag
1671 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1672 * than a change to default behavior so that applications relying on
1673 * this for security won't even compile against older library versions).
1674 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1675 * request renegotiation but not a new session (s->new_session remains
1676 * unset): for servers, this essentially just means that the
1677 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1680 if (clienthello->isv2 ||
1682 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1683 if (!ssl_get_new_session(s, 1)) {
1684 /* SSLfatal() already called */
1688 i = ssl_get_prev_session(s, clienthello);
1690 /* previous session */
1692 } else if (i == -1) {
1693 /* SSLfatal() already called */
1697 if (!ssl_get_new_session(s, 1)) {
1698 /* SSLfatal() already called */
1704 if (SSL_IS_TLS13(s)) {
1705 memcpy(s->tmp_session_id, s->clienthello->session_id,
1706 s->clienthello->session_id_len);
1707 s->tmp_session_id_len = s->clienthello->session_id_len;
1711 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1712 * ciphersuite compatibility with the session as part of resumption.
1714 if (!SSL_IS_TLS13(s) && s->hit) {
1716 id = s->session->cipher->id;
1719 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1721 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1722 c = sk_SSL_CIPHER_value(ciphers, i);
1724 fprintf(stderr, "client [%2d of %2d]:%s\n",
1725 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1734 * we need to have the cipher in the cipher list if we are asked
1737 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1738 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1739 SSL_R_REQUIRED_CIPHER_MISSING);
1744 for (loop = 0; loop < clienthello->compressions_len; loop++) {
1745 if (clienthello->compressions[loop] == 0)
1749 if (loop >= clienthello->compressions_len) {
1751 SSLfatal(s, SSL_AD_DECODE_ERROR,
1752 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1753 SSL_R_NO_COMPRESSION_SPECIFIED);
1757 #ifndef OPENSSL_NO_EC
1758 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1759 ssl_check_for_safari(s, clienthello);
1760 #endif /* !OPENSSL_NO_EC */
1762 /* TLS extensions */
1763 if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
1764 clienthello->pre_proc_exts, NULL, 0, 1)) {
1765 /* SSLfatal() already called */
1770 * Check if we want to use external pre-shared secret for this handshake
1771 * for not reused session only. We need to generate server_random before
1772 * calling tls_session_secret_cb in order to allow SessionTicket
1773 * processing to use it in key derivation.
1777 pos = s->s3->server_random;
1778 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
1779 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1780 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1781 ERR_R_INTERNAL_ERROR);
1787 && s->version >= TLS1_VERSION
1790 && s->ext.session_secret_cb) {
1791 const SSL_CIPHER *pref_cipher = NULL;
1793 * s->session->master_key_length is a size_t, but this is an int for
1794 * backwards compat reasons
1796 int master_key_length;
1798 master_key_length = sizeof(s->session->master_key);
1799 if (s->ext.session_secret_cb(s, s->session->master_key,
1800 &master_key_length, ciphers,
1802 s->ext.session_secret_cb_arg)
1803 && master_key_length > 0) {
1804 s->session->master_key_length = master_key_length;
1806 s->session->ciphers = ciphers;
1807 s->session->verify_result = X509_V_OK;
1811 /* check if some cipher was preferred by call back */
1812 if (pref_cipher == NULL)
1813 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1814 SSL_get_ciphers(s));
1815 if (pref_cipher == NULL) {
1816 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1817 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1818 SSL_R_NO_SHARED_CIPHER);
1822 s->session->cipher = pref_cipher;
1823 sk_SSL_CIPHER_free(s->cipher_list);
1824 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1825 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1826 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1831 * Worst case, we will use the NULL compression, but if we have other
1832 * options, we will now look for them. We have complen-1 compression
1833 * algorithms from the client, starting at q.
1835 s->s3->tmp.new_compression = NULL;
1836 if (SSL_IS_TLS13(s)) {
1838 * We already checked above that the NULL compression method appears in
1839 * the list. Now we check there aren't any others (which is illegal in
1840 * a TLSv1.3 ClientHello.
1842 if (clienthello->compressions_len != 1) {
1843 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1844 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1845 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1849 #ifndef OPENSSL_NO_COMP
1850 /* This only happens if we have a cache hit */
1851 else if (s->session->compress_meth != 0) {
1852 int m, comp_id = s->session->compress_meth;
1854 /* Perform sanity checks on resumed compression algorithm */
1855 /* Can't disable compression */
1856 if (!ssl_allow_compression(s)) {
1857 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1858 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1859 SSL_R_INCONSISTENT_COMPRESSION);
1862 /* Look for resumed compression method */
1863 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1864 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1865 if (comp_id == comp->id) {
1866 s->s3->tmp.new_compression = comp;
1870 if (s->s3->tmp.new_compression == NULL) {
1871 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1872 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1873 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1876 /* Look for resumed method in compression list */
1877 for (k = 0; k < clienthello->compressions_len; k++) {
1878 if (clienthello->compressions[k] == comp_id)
1881 if (k >= clienthello->compressions_len) {
1882 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1883 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1884 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1887 } else if (s->hit) {
1889 } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1890 /* See if we have a match */
1891 int m, nn, v, done = 0;
1894 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1895 for (m = 0; m < nn; m++) {
1896 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1898 for (o = 0; o < clienthello->compressions_len; o++) {
1899 if (v == clienthello->compressions[o]) {
1908 s->s3->tmp.new_compression = comp;
1914 * If compression is disabled we'd better not try to resume a session
1915 * using compression.
1917 if (s->session->compress_meth != 0) {
1918 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1919 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1920 SSL_R_INCONSISTENT_COMPRESSION);
1926 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1929 if (!s->hit || SSL_IS_TLS13(s)) {
1930 sk_SSL_CIPHER_free(s->session->ciphers);
1931 s->session->ciphers = ciphers;
1932 if (ciphers == NULL) {
1933 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1934 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1935 ERR_R_INTERNAL_ERROR);
1942 #ifdef OPENSSL_NO_COMP
1943 s->session->compress_meth = 0;
1945 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1947 if (!tls1_set_server_sigalgs(s)) {
1948 /* SSLfatal() already called */
1953 sk_SSL_CIPHER_free(ciphers);
1954 sk_SSL_CIPHER_free(scsvs);
1955 OPENSSL_free(clienthello->pre_proc_exts);
1956 OPENSSL_free(s->clienthello);
1957 s->clienthello = NULL;
1960 sk_SSL_CIPHER_free(ciphers);
1961 sk_SSL_CIPHER_free(scsvs);
1962 OPENSSL_free(clienthello->pre_proc_exts);
1963 OPENSSL_free(s->clienthello);
1964 s->clienthello = NULL;
1970 * Call the status request callback if needed. Upon success, returns 1.
1971 * Upon failure, returns 0.
1973 static int tls_handle_status_request(SSL *s)
1975 s->ext.status_expected = 0;
1978 * If status request then ask callback what to do. Note: this must be
1979 * called after servername callbacks in case the certificate has changed,
1980 * and must be called after the cipher has been chosen because this may
1981 * influence which certificate is sent
1983 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
1984 && s->ctx->ext.status_cb != NULL) {
1987 /* If no certificate can't return certificate status */
1988 if (s->s3->tmp.cert != NULL) {
1990 * Set current certificate to one we will use so SSL_get_certificate
1991 * et al can pick it up.
1993 s->cert->key = s->s3->tmp.cert;
1994 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
1996 /* We don't want to send a status request response */
1997 case SSL_TLSEXT_ERR_NOACK:
1998 s->ext.status_expected = 0;
2000 /* status request response should be sent */
2001 case SSL_TLSEXT_ERR_OK:
2002 if (s->ext.ocsp.resp)
2003 s->ext.status_expected = 1;
2005 /* something bad happened */
2006 case SSL_TLSEXT_ERR_ALERT_FATAL:
2008 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2009 SSL_F_TLS_HANDLE_STATUS_REQUEST,
2010 SSL_R_CLIENTHELLO_TLSEXT);
2020 * Call the alpn_select callback if needed. Upon success, returns 1.
2021 * Upon failure, returns 0.
2023 int tls_handle_alpn(SSL *s)
2025 const unsigned char *selected = NULL;
2026 unsigned char selected_len = 0;
2028 if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
2029 int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
2030 s->s3->alpn_proposed,
2031 (unsigned int)s->s3->alpn_proposed_len,
2032 s->ctx->ext.alpn_select_cb_arg);
2034 if (r == SSL_TLSEXT_ERR_OK) {
2035 OPENSSL_free(s->s3->alpn_selected);
2036 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
2037 if (s->s3->alpn_selected == NULL) {
2038 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_HANDLE_ALPN,
2039 ERR_R_INTERNAL_ERROR);
2042 s->s3->alpn_selected_len = selected_len;
2043 #ifndef OPENSSL_NO_NEXTPROTONEG
2044 /* ALPN takes precedence over NPN. */
2045 s->s3->npn_seen = 0;
2048 /* Check ALPN is consistent with session */
2049 if (s->session->ext.alpn_selected == NULL
2050 || selected_len != s->session->ext.alpn_selected_len
2051 || memcmp(selected, s->session->ext.alpn_selected,
2052 selected_len) != 0) {
2053 /* Not consistent so can't be used for early_data */
2054 s->ext.early_data_ok = 0;
2057 /* If a new session update it with the new ALPN value */
2058 s->session->ext.alpn_selected = OPENSSL_memdup(selected,
2060 if (s->session->ext.alpn_selected == NULL) {
2061 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2062 SSL_F_TLS_HANDLE_ALPN,
2063 ERR_R_INTERNAL_ERROR);
2066 s->session->ext.alpn_selected_len = selected_len;
2071 } else if (r != SSL_TLSEXT_ERR_NOACK) {
2072 SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, SSL_F_TLS_HANDLE_ALPN,
2073 SSL_R_NO_APPLICATION_PROTOCOL);
2077 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2082 /* Check ALPN is consistent with session */
2083 if (s->session->ext.alpn_selected != NULL) {
2084 /* Not consistent so can't be used for early_data */
2085 s->ext.early_data_ok = 0;
2091 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
2093 const SSL_CIPHER *cipher;
2095 if (wst == WORK_MORE_A) {
2096 int rv = tls_early_post_process_client_hello(s);
2098 /* SSLfatal() was already called */
2105 if (wst == WORK_MORE_B) {
2106 if (!s->hit || SSL_IS_TLS13(s)) {
2107 /* Let cert callback update server certificates if required */
2108 if (!s->hit && s->cert->cert_cb != NULL) {
2109 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2111 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2112 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2113 SSL_R_CERT_CB_ERROR);
2117 s->rwstate = SSL_X509_LOOKUP;
2120 s->rwstate = SSL_NOTHING;
2123 /* In TLSv1.3 we selected the ciphersuite before resumption */
2124 if (!SSL_IS_TLS13(s)) {
2126 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
2128 if (cipher == NULL) {
2129 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2130 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2131 SSL_R_NO_SHARED_CIPHER);
2134 s->s3->tmp.new_cipher = cipher;
2137 if (!tls_choose_sigalg(s, 1)) {
2138 /* SSLfatal already called */
2141 /* check whether we should disable session resumption */
2142 if (s->not_resumable_session_cb != NULL)
2143 s->session->not_resumable =
2144 s->not_resumable_session_cb(s,
2145 ((s->s3->tmp.new_cipher->algorithm_mkey
2146 & (SSL_kDHE | SSL_kECDHE)) != 0));
2147 if (s->session->not_resumable)
2148 /* do not send a session ticket */
2149 s->ext.ticket_expected = 0;
2152 /* Session-id reuse */
2153 s->s3->tmp.new_cipher = s->session->cipher;
2157 * we now have the following setup.
2159 * cipher_list - our preferred list of ciphers
2160 * ciphers - the clients preferred list of ciphers
2161 * compression - basically ignored right now
2162 * ssl version is set - sslv3
2163 * s->session - The ssl session has been setup.
2164 * s->hit - session reuse flag
2165 * s->s3->tmp.new_cipher- the new cipher to use.
2169 * Call status_request callback if needed. Has to be done after the
2170 * certificate callbacks etc above.
2172 if (!tls_handle_status_request(s)) {
2173 /* SSLfatal() already called */
2177 * Call alpn_select callback if needed. Has to be done after SNI and
2178 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2179 * we already did this because cipher negotiation happens earlier, and
2180 * we must handle ALPN before we decide whether to accept early_data.
2182 if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
2183 /* SSLfatal() already called */
2189 #ifndef OPENSSL_NO_SRP
2190 if (wst == WORK_MORE_C) {
2192 if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
2194 * callback indicates further work to be done
2196 s->rwstate = SSL_X509_LOOKUP;
2200 /* SSLfatal() already called */
2206 return WORK_FINISHED_STOP;
2211 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
2216 unsigned char *session_id;
2217 int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
2219 version = usetls13 ? TLS1_2_VERSION : s->version;
2220 if (!WPACKET_put_bytes_u16(pkt, version)
2222 * Random stuff. Filling of the server_random takes place in
2223 * tls_process_client_hello()
2225 || !WPACKET_memcpy(pkt,
2226 s->hello_retry_request == SSL_HRR_PENDING
2227 ? hrrrandom : s->s3->server_random,
2228 SSL3_RANDOM_SIZE)) {
2229 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2230 ERR_R_INTERNAL_ERROR);
2235 * There are several cases for the session ID to send
2236 * back in the server hello:
2237 * - For session reuse from the session cache,
2238 * we send back the old session ID.
2239 * - If stateless session reuse (using a session ticket)
2240 * is successful, we send back the client's "session ID"
2241 * (which doesn't actually identify the session).
2242 * - If it is a new session, we send back the new
2244 * - However, if we want the new session to be single-use,
2245 * we send back a 0-length session ID.
2246 * - In TLSv1.3 we echo back the session id sent to us by the client
2248 * s->hit is non-zero in either case of session reuse,
2249 * so the following won't overwrite an ID that we're supposed
2252 if (s->session->not_resumable ||
2253 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2255 s->session->session_id_length = 0;
2258 sl = s->tmp_session_id_len;
2259 session_id = s->tmp_session_id;
2261 sl = s->session->session_id_length;
2262 session_id = s->session->session_id;
2265 if (sl > sizeof(s->session->session_id)) {
2266 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2267 ERR_R_INTERNAL_ERROR);
2271 /* set up the compression method */
2272 #ifdef OPENSSL_NO_COMP
2275 if (usetls13 || s->s3->tmp.new_compression == NULL)
2278 compm = s->s3->tmp.new_compression->id;
2281 if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
2282 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
2283 || !WPACKET_put_bytes_u8(pkt, compm)
2284 || !tls_construct_extensions(s, pkt,
2285 s->hello_retry_request
2287 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2289 ? SSL_EXT_TLS1_3_SERVER_HELLO
2290 : SSL_EXT_TLS1_2_SERVER_HELLO),
2292 /* SSLfatal() already called */
2296 if (s->hello_retry_request == SSL_HRR_PENDING) {
2297 /* Ditch the session. We'll create a new one next time around */
2298 SSL_SESSION_free(s->session);
2303 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2304 * a synthetic message_hash in place of ClientHello1.
2306 if (!create_synthetic_message_hash(s)) {
2307 /* SSLfatal() already called */
2310 } else if (!(s->verify_mode & SSL_VERIFY_PEER)
2311 && !ssl3_digest_cached_records(s, 0)) {
2312 /* SSLfatal() already called */;
2319 int tls_construct_server_done(SSL *s, WPACKET *pkt)
2321 if (!s->s3->tmp.cert_request) {
2322 if (!ssl3_digest_cached_records(s, 0)) {
2323 /* SSLfatal() already called */
2330 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2332 #ifndef OPENSSL_NO_DH
2333 EVP_PKEY *pkdh = NULL;
2335 #ifndef OPENSSL_NO_EC
2336 unsigned char *encodedPoint = NULL;
2337 size_t encodedlen = 0;
2340 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2344 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2345 EVP_PKEY_CTX *pctx = NULL;
2346 size_t paramlen, paramoffset;
2348 if (!WPACKET_get_total_written(pkt, ¶moffset)) {
2349 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2350 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2354 if (md_ctx == NULL) {
2355 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2356 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2360 type = s->s3->tmp.new_cipher->algorithm_mkey;
2362 r[0] = r[1] = r[2] = r[3] = NULL;
2363 #ifndef OPENSSL_NO_PSK
2364 /* Plain PSK or RSAPSK nothing to do */
2365 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2367 #endif /* !OPENSSL_NO_PSK */
2368 #ifndef OPENSSL_NO_DH
2369 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2370 CERT *cert = s->cert;
2372 EVP_PKEY *pkdhp = NULL;
2375 if (s->cert->dh_tmp_auto) {
2376 DH *dhp = ssl_get_auto_dh(s);
2377 pkdh = EVP_PKEY_new();
2378 if (pkdh == NULL || dhp == NULL) {
2380 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2381 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2382 ERR_R_INTERNAL_ERROR);
2385 EVP_PKEY_assign_DH(pkdh, dhp);
2388 pkdhp = cert->dh_tmp;
2390 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2391 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2392 pkdh = ssl_dh_to_pkey(dhp);
2394 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2395 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2396 ERR_R_INTERNAL_ERROR);
2401 if (pkdhp == NULL) {
2402 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2403 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2404 SSL_R_MISSING_TMP_DH_KEY);
2407 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2408 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2409 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2410 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2411 SSL_R_DH_KEY_TOO_SMALL);
2414 if (s->s3->tmp.pkey != NULL) {
2415 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2416 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2417 ERR_R_INTERNAL_ERROR);
2421 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2422 if (s->s3->tmp.pkey == NULL) {
2423 /* SSLfatal() already called */
2427 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2429 EVP_PKEY_free(pkdh);
2432 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2433 DH_get0_key(dh, &r[2], NULL);
2436 #ifndef OPENSSL_NO_EC
2437 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2439 if (s->s3->tmp.pkey != NULL) {
2440 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2441 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2442 ERR_R_INTERNAL_ERROR);
2446 /* Get NID of appropriate shared curve */
2447 curve_id = tls1_shared_group(s, -2);
2448 if (curve_id == 0) {
2449 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2450 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2451 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2454 s->s3->tmp.pkey = ssl_generate_pkey_group(s, curve_id);
2455 /* Generate a new key for this curve */
2456 if (s->s3->tmp.pkey == NULL) {
2457 /* SSLfatal() already called */
2461 /* Encode the public key. */
2462 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2464 if (encodedlen == 0) {
2465 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2466 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2471 * We'll generate the serverKeyExchange message explicitly so we
2472 * can set these to NULLs
2479 #endif /* !OPENSSL_NO_EC */
2480 #ifndef OPENSSL_NO_SRP
2481 if (type & SSL_kSRP) {
2482 if ((s->srp_ctx.N == NULL) ||
2483 (s->srp_ctx.g == NULL) ||
2484 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2485 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2486 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2487 SSL_R_MISSING_SRP_PARAM);
2490 r[0] = s->srp_ctx.N;
2491 r[1] = s->srp_ctx.g;
2492 r[2] = s->srp_ctx.s;
2493 r[3] = s->srp_ctx.B;
2497 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2498 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2499 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2503 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2504 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2506 } else if (lu == NULL) {
2507 SSLfatal(s, SSL_AD_DECODE_ERROR,
2508 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2512 #ifndef OPENSSL_NO_PSK
2513 if (type & SSL_PSK) {
2514 size_t len = (s->cert->psk_identity_hint == NULL)
2515 ? 0 : strlen(s->cert->psk_identity_hint);
2518 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2519 * checked this when we set the identity hint - but just in case
2521 if (len > PSK_MAX_IDENTITY_LEN
2522 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2524 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2525 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2526 ERR_R_INTERNAL_ERROR);
2532 for (i = 0; i < 4 && r[i] != NULL; i++) {
2533 unsigned char *binval;
2536 #ifndef OPENSSL_NO_SRP
2537 if ((i == 2) && (type & SSL_kSRP)) {
2538 res = WPACKET_start_sub_packet_u8(pkt);
2541 res = WPACKET_start_sub_packet_u16(pkt);
2544 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2545 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2546 ERR_R_INTERNAL_ERROR);
2550 #ifndef OPENSSL_NO_DH
2552 * for interoperability with some versions of the Microsoft TLS
2553 * stack, we need to zero pad the DHE pub key to the same length
2556 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2557 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2560 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2561 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2562 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2563 ERR_R_INTERNAL_ERROR);
2566 memset(binval, 0, len);
2570 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2571 || !WPACKET_close(pkt)) {
2572 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2573 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2574 ERR_R_INTERNAL_ERROR);
2578 BN_bn2bin(r[i], binval);
2581 #ifndef OPENSSL_NO_EC
2582 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2584 * We only support named (not generic) curves. In this situation, the
2585 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2586 * [1 byte length of encoded point], followed by the actual encoded
2589 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2590 || !WPACKET_put_bytes_u8(pkt, 0)
2591 || !WPACKET_put_bytes_u8(pkt, curve_id)
2592 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2593 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2594 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2595 ERR_R_INTERNAL_ERROR);
2598 OPENSSL_free(encodedPoint);
2599 encodedPoint = NULL;
2605 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2607 unsigned char *sigbytes1, *sigbytes2, *tbs;
2608 size_t siglen, tbslen;
2611 if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
2612 /* Should never happen */
2613 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2614 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2615 ERR_R_INTERNAL_ERROR);
2618 /* Get length of the parameters we have written above */
2619 if (!WPACKET_get_length(pkt, ¶mlen)) {
2620 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2621 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2622 ERR_R_INTERNAL_ERROR);
2625 /* send signature algorithm */
2626 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
2627 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2628 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2629 ERR_R_INTERNAL_ERROR);
2633 * Create the signature. We don't know the actual length of the sig
2634 * until after we've created it, so we reserve enough bytes for it
2635 * up front, and then properly allocate them in the WPACKET
2638 siglen = EVP_PKEY_size(pkey);
2639 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2640 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2641 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2642 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2643 ERR_R_INTERNAL_ERROR);
2646 if (lu->sig == EVP_PKEY_RSA_PSS) {
2647 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2648 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2649 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2650 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2655 tbslen = construct_key_exchange_tbs(s, &tbs,
2656 s->init_buf->data + paramoffset,
2659 /* SSLfatal() already called */
2662 rv = EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen);
2664 if (rv <= 0 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2665 || sigbytes1 != sigbytes2) {
2666 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2667 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2668 ERR_R_INTERNAL_ERROR);
2673 EVP_MD_CTX_free(md_ctx);
2676 #ifndef OPENSSL_NO_DH
2677 EVP_PKEY_free(pkdh);
2679 #ifndef OPENSSL_NO_EC
2680 OPENSSL_free(encodedPoint);
2682 EVP_MD_CTX_free(md_ctx);
2686 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2688 if (SSL_IS_TLS13(s)) {
2689 /* TODO(TLS1.3) for now send empty request context */
2690 if (!WPACKET_put_bytes_u8(pkt, 0)) {
2691 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2692 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2693 ERR_R_INTERNAL_ERROR);
2697 if (!tls_construct_extensions(s, pkt,
2698 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2700 /* SSLfatal() already called */
2706 /* get the list of acceptable cert types */
2707 if (!WPACKET_start_sub_packet_u8(pkt)
2708 || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2709 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2710 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2714 if (SSL_USE_SIGALGS(s)) {
2715 const uint16_t *psigs;
2716 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2718 if (!WPACKET_start_sub_packet_u16(pkt)
2719 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
2720 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2721 || !WPACKET_close(pkt)) {
2722 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2723 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2724 ERR_R_INTERNAL_ERROR);
2729 if (!construct_ca_names(s, pkt)) {
2730 /* SSLfatal() already called */
2735 s->s3->tmp.cert_request = 1;
2739 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
2741 #ifndef OPENSSL_NO_PSK
2742 unsigned char psk[PSK_MAX_PSK_LEN];
2744 PACKET psk_identity;
2746 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2747 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2748 SSL_R_LENGTH_MISMATCH);
2751 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2752 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2753 SSL_R_DATA_LENGTH_TOO_LONG);
2756 if (s->psk_server_callback == NULL) {
2757 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2758 SSL_R_PSK_NO_SERVER_CB);
2762 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2763 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2764 ERR_R_INTERNAL_ERROR);
2768 psklen = s->psk_server_callback(s, s->session->psk_identity,
2771 if (psklen > PSK_MAX_PSK_LEN) {
2772 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2773 ERR_R_INTERNAL_ERROR);
2775 } else if (psklen == 0) {
2777 * PSK related to the given identity not found
2779 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
2780 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2781 SSL_R_PSK_IDENTITY_NOT_FOUND);
2785 OPENSSL_free(s->s3->tmp.psk);
2786 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2787 OPENSSL_cleanse(psk, psklen);
2789 if (s->s3->tmp.psk == NULL) {
2790 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2791 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2795 s->s3->tmp.psklen = psklen;
2799 /* Should never happen */
2800 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2801 ERR_R_INTERNAL_ERROR);
2806 static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
2808 #ifndef OPENSSL_NO_RSA
2809 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2811 unsigned char decrypt_good, version_good;
2812 size_t j, padding_len;
2813 PACKET enc_premaster;
2815 unsigned char *rsa_decrypt = NULL;
2818 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2820 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2821 SSL_R_MISSING_RSA_CERTIFICATE);
2825 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2826 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2827 enc_premaster = *pkt;
2829 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2830 || PACKET_remaining(pkt) != 0) {
2831 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2832 SSL_R_LENGTH_MISMATCH);
2838 * We want to be sure that the plaintext buffer size makes it safe to
2839 * iterate over the entire size of a premaster secret
2840 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2841 * their ciphertext cannot accommodate a premaster secret anyway.
2843 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2844 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2845 RSA_R_KEY_SIZE_TOO_SMALL);
2849 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2850 if (rsa_decrypt == NULL) {
2851 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2852 ERR_R_MALLOC_FAILURE);
2857 * We must not leak whether a decryption failure occurs because of
2858 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2859 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2860 * generates a random premaster secret for the case that the decrypt
2861 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2864 if (ssl_randbytes(s, rand_premaster_secret,
2865 sizeof(rand_premaster_secret)) <= 0) {
2866 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2867 ERR_R_INTERNAL_ERROR);
2872 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2873 * the timing-sensitive code below.
2875 /* TODO(size_t): Convert this function */
2876 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2877 PACKET_data(&enc_premaster),
2878 rsa_decrypt, rsa, RSA_NO_PADDING);
2879 if (decrypt_len < 0) {
2880 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2881 ERR_R_INTERNAL_ERROR);
2885 /* Check the padding. See RFC 3447, section 7.2.2. */
2888 * The smallest padded premaster is 11 bytes of overhead. Small keys
2889 * are publicly invalid, so this may return immediately. This ensures
2890 * PS is at least 8 bytes.
2892 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2893 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2894 SSL_R_DECRYPTION_FAILED);
2898 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2899 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2900 constant_time_eq_int_8(rsa_decrypt[1], 2);
2901 for (j = 2; j < padding_len - 1; j++) {
2902 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2904 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2907 * If the version in the decrypted pre-master secret is correct then
2908 * version_good will be 0xff, otherwise it'll be zero. The
2909 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2910 * (http://eprint.iacr.org/2003/052/) exploits the version number
2911 * check as a "bad version oracle". Thus version checks are done in
2912 * constant time and are treated like any other decryption error.
2915 constant_time_eq_8(rsa_decrypt[padding_len],
2916 (unsigned)(s->client_version >> 8));
2918 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2919 (unsigned)(s->client_version & 0xff));
2922 * The premaster secret must contain the same version number as the
2923 * ClientHello to detect version rollback attacks (strangely, the
2924 * protocol does not offer such protection for DH ciphersuites).
2925 * However, buggy clients exist that send the negotiated protocol
2926 * version instead if the server does not support the requested
2927 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2930 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2931 unsigned char workaround_good;
2932 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2933 (unsigned)(s->version >> 8));
2935 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2936 (unsigned)(s->version & 0xff));
2937 version_good |= workaround_good;
2941 * Both decryption and version must be good for decrypt_good to
2942 * remain non-zero (0xff).
2944 decrypt_good &= version_good;
2947 * Now copy rand_premaster_secret over from p using
2948 * decrypt_good_mask. If decryption failed, then p does not
2949 * contain valid plaintext, however, a check above guarantees
2950 * it is still sufficiently large to read from.
2952 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2953 rsa_decrypt[padding_len + j] =
2954 constant_time_select_8(decrypt_good,
2955 rsa_decrypt[padding_len + j],
2956 rand_premaster_secret[j]);
2959 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2960 sizeof(rand_premaster_secret), 0)) {
2961 /* SSLfatal() already called */
2967 OPENSSL_free(rsa_decrypt);
2970 /* Should never happen */
2971 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2972 ERR_R_INTERNAL_ERROR);
2977 static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
2979 #ifndef OPENSSL_NO_DH
2980 EVP_PKEY *skey = NULL;
2984 const unsigned char *data;
2985 EVP_PKEY *ckey = NULL;
2988 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2989 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2990 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2993 skey = s->s3->tmp.pkey;
2995 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2996 SSL_R_MISSING_TMP_DH_KEY);
3000 if (PACKET_remaining(pkt) == 0L) {
3001 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3002 SSL_R_MISSING_TMP_DH_KEY);
3005 if (!PACKET_get_bytes(pkt, &data, i)) {
3006 /* We already checked we have enough data */
3007 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3008 ERR_R_INTERNAL_ERROR);
3011 ckey = EVP_PKEY_new();
3012 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
3013 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3017 cdh = EVP_PKEY_get0_DH(ckey);
3018 pub_key = BN_bin2bn(data, i, NULL);
3020 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
3021 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3022 ERR_R_INTERNAL_ERROR);
3023 if (pub_key != NULL)
3028 if (ssl_derive(s, skey, ckey, 1) == 0) {
3029 /* SSLfatal() already called */
3034 EVP_PKEY_free(s->s3->tmp.pkey);
3035 s->s3->tmp.pkey = NULL;
3037 EVP_PKEY_free(ckey);
3040 /* Should never happen */
3041 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3042 ERR_R_INTERNAL_ERROR);
3047 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
3049 #ifndef OPENSSL_NO_EC
3050 EVP_PKEY *skey = s->s3->tmp.pkey;
3051 EVP_PKEY *ckey = NULL;
3054 if (PACKET_remaining(pkt) == 0L) {
3055 /* We don't support ECDH client auth */
3056 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_CKE_ECDHE,
3057 SSL_R_MISSING_TMP_ECDH_KEY);
3061 const unsigned char *data;
3064 * Get client's public key from encoded point in the
3065 * ClientKeyExchange message.
3068 /* Get encoded point length */
3069 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
3070 || PACKET_remaining(pkt) != 0) {
3071 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3072 SSL_R_LENGTH_MISMATCH);
3075 ckey = EVP_PKEY_new();
3076 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
3077 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3081 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
3082 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3088 if (ssl_derive(s, skey, ckey, 1) == 0) {
3089 /* SSLfatal() already called */
3094 EVP_PKEY_free(s->s3->tmp.pkey);
3095 s->s3->tmp.pkey = NULL;
3097 EVP_PKEY_free(ckey);
3101 /* Should never happen */
3102 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3103 ERR_R_INTERNAL_ERROR);
3108 static int tls_process_cke_srp(SSL *s, PACKET *pkt)
3110 #ifndef OPENSSL_NO_SRP
3112 const unsigned char *data;
3114 if (!PACKET_get_net_2(pkt, &i)
3115 || !PACKET_get_bytes(pkt, &data, i)) {
3116 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3117 SSL_R_BAD_SRP_A_LENGTH);
3120 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
3121 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3125 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
3126 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CKE_SRP,
3127 SSL_R_BAD_SRP_PARAMETERS);
3130 OPENSSL_free(s->session->srp_username);
3131 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3132 if (s->session->srp_username == NULL) {
3133 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3134 ERR_R_MALLOC_FAILURE);
3138 if (!srp_generate_server_master_secret(s)) {
3139 /* SSLfatal() already called */
3145 /* Should never happen */
3146 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3147 ERR_R_INTERNAL_ERROR);
3152 static int tls_process_cke_gost(SSL *s, PACKET *pkt)
3154 #ifndef OPENSSL_NO_GOST
3155 EVP_PKEY_CTX *pkey_ctx;
3156 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
3157 unsigned char premaster_secret[32];
3158 const unsigned char *start;
3159 size_t outlen = 32, inlen;
3160 unsigned long alg_a;
3163 size_t sess_key_len;
3164 const unsigned char *data;
3167 /* Get our certificate private key */
3168 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3169 if (alg_a & SSL_aGOST12) {
3171 * New GOST ciphersuites have SSL_aGOST01 bit too
3173 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
3175 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3178 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3180 } else if (alg_a & SSL_aGOST01) {
3181 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3184 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
3185 if (pkey_ctx == NULL) {
3186 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3187 ERR_R_MALLOC_FAILURE);
3190 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3191 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3192 ERR_R_INTERNAL_ERROR);
3196 * If client certificate is present and is of the same type, maybe
3197 * use it for key exchange. Don't mind errors from
3198 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3199 * client certificate for authorization only.
3201 client_pub_pkey = X509_get0_pubkey(s->session->peer);
3202 if (client_pub_pkey) {
3203 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3206 /* Decrypt session key */
3207 sess_key_len = PACKET_remaining(pkt);
3208 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
3209 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3210 ERR_R_INTERNAL_ERROR);
3213 /* TODO(size_t): Convert this function */
3214 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
3215 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
3216 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
3217 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3218 SSL_R_DECRYPTION_FAILED);
3223 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
3225 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3226 SSL_R_DECRYPTION_FAILED);
3229 /* Generate master secret */
3230 if (!ssl_generate_master_secret(s, premaster_secret,
3231 sizeof(premaster_secret), 0)) {
3232 /* SSLfatal() already called */
3235 /* Check if pubkey from client certificate was used */
3236 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
3238 s->statem.no_cert_verify = 1;
3242 EVP_PKEY_CTX_free(pkey_ctx);
3245 /* Should never happen */
3246 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3247 ERR_R_INTERNAL_ERROR);
3252 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3254 unsigned long alg_k;
3256 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3258 /* For PSK parse and retrieve identity, obtain PSK key */
3259 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
3260 /* SSLfatal() already called */
3264 if (alg_k & SSL_kPSK) {
3265 /* Identity extracted earlier: should be nothing left */
3266 if (PACKET_remaining(pkt) != 0) {
3267 SSLfatal(s, SSL_AD_DECODE_ERROR,
3268 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3269 SSL_R_LENGTH_MISMATCH);
3272 /* PSK handled by ssl_generate_master_secret */
3273 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3274 /* SSLfatal() already called */
3277 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3278 if (!tls_process_cke_rsa(s, pkt)) {
3279 /* SSLfatal() already called */
3282 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3283 if (!tls_process_cke_dhe(s, pkt)) {
3284 /* SSLfatal() already called */
3287 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3288 if (!tls_process_cke_ecdhe(s, pkt)) {
3289 /* SSLfatal() already called */
3292 } else if (alg_k & SSL_kSRP) {
3293 if (!tls_process_cke_srp(s, pkt)) {
3294 /* SSLfatal() already called */
3297 } else if (alg_k & SSL_kGOST) {
3298 if (!tls_process_cke_gost(s, pkt)) {
3299 /* SSLfatal() already called */
3303 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3304 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3305 SSL_R_UNKNOWN_CIPHER_TYPE);
3309 return MSG_PROCESS_CONTINUE_PROCESSING;
3311 #ifndef OPENSSL_NO_PSK
3312 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
3313 s->s3->tmp.psk = NULL;
3315 return MSG_PROCESS_ERROR;
3318 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
3320 #ifndef OPENSSL_NO_SCTP
3321 if (wst == WORK_MORE_A) {
3322 if (SSL_IS_DTLS(s)) {
3323 unsigned char sctpauthkey[64];
3324 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3326 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3329 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3330 sizeof(DTLS1_SCTP_AUTH_LABEL));
3332 if (SSL_export_keying_material(s, sctpauthkey,
3333 sizeof(sctpauthkey), labelbuffer,
3334 sizeof(labelbuffer), NULL, 0,
3336 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3337 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3338 ERR_R_INTERNAL_ERROR);
3342 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3343 sizeof(sctpauthkey), sctpauthkey);
3348 if (s->statem.no_cert_verify || !s->session->peer) {
3350 * No certificate verify or no peer certificate so we no longer need
3351 * the handshake_buffer
3353 if (!ssl3_digest_cached_records(s, 0)) {
3354 /* SSLfatal() already called */
3357 return WORK_FINISHED_CONTINUE;
3359 if (!s->s3->handshake_buffer) {
3360 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3361 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3362 ERR_R_INTERNAL_ERROR);
3366 * For sigalgs freeze the handshake buffer. If we support
3367 * extms we've done this already so this is a no-op
3369 if (!ssl3_digest_cached_records(s, 1)) {
3370 /* SSLfatal() already called */
3375 return WORK_FINISHED_CONTINUE;
3378 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3381 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
3383 unsigned long l, llen;
3384 const unsigned char *certstart, *certbytes;
3385 STACK_OF(X509) *sk = NULL;
3386 PACKET spkt, context;
3389 if ((sk = sk_X509_new_null()) == NULL) {
3390 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3391 ERR_R_MALLOC_FAILURE);
3395 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3396 if ((SSL_IS_TLS13(s) && !PACKET_get_length_prefixed_1(pkt, &context))
3397 || !PACKET_get_net_3(pkt, &llen)
3398 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3399 || PACKET_remaining(pkt) != 0) {
3400 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3401 SSL_R_LENGTH_MISMATCH);
3405 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3406 if (!PACKET_get_net_3(&spkt, &l)
3407 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3408 SSLfatal(s, SSL_AD_DECODE_ERROR,
3409 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3410 SSL_R_CERT_LENGTH_MISMATCH);
3414 certstart = certbytes;
3415 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3417 SSLfatal(s, SSL_AD_DECODE_ERROR,
3418 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3421 if (certbytes != (certstart + l)) {
3422 SSLfatal(s, SSL_AD_DECODE_ERROR,
3423 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3424 SSL_R_CERT_LENGTH_MISMATCH);
3428 if (SSL_IS_TLS13(s)) {
3429 RAW_EXTENSION *rawexts = NULL;
3432 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3433 SSLfatal(s, SSL_AD_DECODE_ERROR,
3434 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3438 if (!tls_collect_extensions(s, &extensions,
3439 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
3440 NULL, chainidx == 0)
3441 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3442 rawexts, x, chainidx,
3443 PACKET_remaining(&spkt) == 0)) {
3444 OPENSSL_free(rawexts);
3447 OPENSSL_free(rawexts);
3450 if (!sk_X509_push(sk, x)) {
3451 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3452 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3453 ERR_R_MALLOC_FAILURE);
3459 if (sk_X509_num(sk) <= 0) {
3460 /* TLS does not mind 0 certs returned */
3461 if (s->version == SSL3_VERSION) {
3462 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3463 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3464 SSL_R_NO_CERTIFICATES_RETURNED);
3467 /* Fail for TLS only if we required a certificate */
3468 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3469 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3470 SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
3471 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3472 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3475 /* No client certificate so digest cached records */
3476 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3477 /* SSLfatal() already called */
3482 i = ssl_verify_cert_chain(s, sk);
3484 SSLfatal(s, ssl_verify_alarm_type(s->verify_result),
3485 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3486 SSL_R_CERTIFICATE_VERIFY_FAILED);
3490 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3491 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3494 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3496 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3497 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3498 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3503 X509_free(s->session->peer);
3504 s->session->peer = sk_X509_shift(sk);
3505 s->session->verify_result = s->verify_result;