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);
27 static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt);
30 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
31 * handshake state transitions when a TLSv1.3 server is reading messages from
32 * the client. The message type that the client has sent is provided in |mt|.
33 * The current state is in |s->statem.hand_state|.
35 * Return values are 1 for success (transition allowed) and 0 on error
36 * (transition not allowed)
38 static int ossl_statem_server13_read_transition(SSL *s, int mt)
40 OSSL_STATEM *st = &s->statem;
43 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
44 * not negotiated TLSv1.3 yet, so that case is handled by
45 * ossl_statem_server_read_transition()
47 switch (st->hand_state) {
51 case TLS_ST_EARLY_DATA:
52 if (s->hello_retry_request) {
53 if (mt == SSL3_MT_CLIENT_HELLO) {
54 st->hand_state = TLS_ST_SR_CLNT_HELLO;
58 } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
59 if (mt == SSL3_MT_END_OF_EARLY_DATA) {
60 st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
67 case TLS_ST_SR_END_OF_EARLY_DATA:
68 case TLS_ST_SW_FINISHED:
69 if (s->s3->tmp.cert_request) {
70 if (mt == SSL3_MT_CERTIFICATE) {
71 st->hand_state = TLS_ST_SR_CERT;
75 if (mt == SSL3_MT_FINISHED) {
76 st->hand_state = TLS_ST_SR_FINISHED;
83 if (s->session->peer == NULL) {
84 if (mt == SSL3_MT_FINISHED) {
85 st->hand_state = TLS_ST_SR_FINISHED;
89 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
90 st->hand_state = TLS_ST_SR_CERT_VRFY;
96 case TLS_ST_SR_CERT_VRFY:
97 if (mt == SSL3_MT_FINISHED) {
98 st->hand_state = TLS_ST_SR_FINISHED;
105 * Its never ok to start processing handshake messages in the middle of
106 * early data (i.e. before we've received the end of early data alert)
108 if (s->early_data_state == SSL_EARLY_DATA_READING)
110 if (mt == SSL3_MT_KEY_UPDATE) {
111 st->hand_state = TLS_ST_SR_KEY_UPDATE;
117 /* No valid transition found */
122 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
123 * handshake state transitions when the server is reading messages from the
124 * client. The message type that the client has sent is provided in |mt|. The
125 * current state is in |s->statem.hand_state|.
127 * Return values are 1 for success (transition allowed) and 0 on error
128 * (transition not allowed)
130 int ossl_statem_server_read_transition(SSL *s, int mt)
132 OSSL_STATEM *st = &s->statem;
134 if (SSL_IS_TLS13(s)) {
135 if (!ossl_statem_server13_read_transition(s, mt))
140 switch (st->hand_state) {
146 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
147 if (mt == SSL3_MT_CLIENT_HELLO) {
148 st->hand_state = TLS_ST_SR_CLNT_HELLO;
153 case TLS_ST_SW_SRVR_DONE:
155 * If we get a CKE message after a ServerDone then either
156 * 1) We didn't request a Certificate
158 * 2) If we did request one then
159 * a) We allow no Certificate to be returned
161 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
162 * list if we requested a certificate)
164 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
165 if (s->s3->tmp.cert_request) {
166 if (s->version == SSL3_VERSION) {
167 if ((s->verify_mode & SSL_VERIFY_PEER)
168 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
170 * This isn't an unexpected message as such - we're just
171 * not going to accept it because we require a client
174 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
175 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
176 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
179 st->hand_state = TLS_ST_SR_KEY_EXCH;
183 st->hand_state = TLS_ST_SR_KEY_EXCH;
186 } else if (s->s3->tmp.cert_request) {
187 if (mt == SSL3_MT_CERTIFICATE) {
188 st->hand_state = TLS_ST_SR_CERT;
195 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
196 st->hand_state = TLS_ST_SR_KEY_EXCH;
201 case TLS_ST_SR_KEY_EXCH:
203 * We should only process a CertificateVerify message if we have
204 * received a Certificate from the client. If so then |s->session->peer|
205 * will be non NULL. In some instances a CertificateVerify message is
206 * not required even if the peer has sent a Certificate (e.g. such as in
207 * the case of static DH). In that case |st->no_cert_verify| should be
210 if (s->session->peer == NULL || st->no_cert_verify) {
211 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
213 * For the ECDH ciphersuites when the client sends its ECDH
214 * pub key in a certificate, the CertificateVerify message is
215 * not sent. Also for GOST ciphersuites when the client uses
216 * its key from the certificate for key exchange.
218 st->hand_state = TLS_ST_SR_CHANGE;
222 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
223 st->hand_state = TLS_ST_SR_CERT_VRFY;
229 case TLS_ST_SR_CERT_VRFY:
230 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
231 st->hand_state = TLS_ST_SR_CHANGE;
236 case TLS_ST_SR_CHANGE:
237 #ifndef OPENSSL_NO_NEXTPROTONEG
238 if (s->s3->npn_seen) {
239 if (mt == SSL3_MT_NEXT_PROTO) {
240 st->hand_state = TLS_ST_SR_NEXT_PROTO;
245 if (mt == SSL3_MT_FINISHED) {
246 st->hand_state = TLS_ST_SR_FINISHED;
249 #ifndef OPENSSL_NO_NEXTPROTONEG
254 #ifndef OPENSSL_NO_NEXTPROTONEG
255 case TLS_ST_SR_NEXT_PROTO:
256 if (mt == SSL3_MT_FINISHED) {
257 st->hand_state = TLS_ST_SR_FINISHED;
263 case TLS_ST_SW_FINISHED:
264 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
265 st->hand_state = TLS_ST_SR_CHANGE;
272 /* No valid transition found */
273 SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
274 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
275 SSL_R_UNEXPECTED_MESSAGE);
280 * Should we send a ServerKeyExchange message?
282 * Valid return values are:
286 static int send_server_key_exchange(SSL *s)
288 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
291 * only send a ServerKeyExchange if DH or fortezza but we have a
292 * sign only certificate PSK: may send PSK identity hints For
293 * ECC ciphersuites, we send a serverKeyExchange message only if
294 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
295 * the server certificate contains the server's public key for
298 if (alg_k & (SSL_kDHE | SSL_kECDHE)
300 * PSK: send ServerKeyExchange if PSK identity hint if
303 #ifndef OPENSSL_NO_PSK
304 /* Only send SKE if we have identity hint for plain PSK */
305 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
306 && s->cert->psk_identity_hint)
307 /* For other PSK always send SKE */
308 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
310 #ifndef OPENSSL_NO_SRP
311 /* SRP: send ServerKeyExchange */
312 || (alg_k & SSL_kSRP)
322 * Should we send a CertificateRequest message?
324 * Valid return values are:
328 static int send_certificate_request(SSL *s)
331 /* don't request cert unless asked for it: */
332 s->verify_mode & SSL_VERIFY_PEER
334 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
335 * during re-negotiation:
337 && (s->s3->tmp.finish_md_len == 0 ||
338 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
340 * never request cert in anonymous ciphersuites (see
341 * section "Certificate request" in SSL 3 drafts and in
344 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
346 * ... except when the application insists on
347 * verification (against the specs, but statem_clnt.c accepts
350 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
351 /* don't request certificate for SRP auth */
352 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
354 * With normal PSK Certificates and Certificate Requests
357 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
365 * ossl_statem_server13_write_transition() works out what handshake state to
366 * move to next when a TLSv1.3 server is writing messages to be sent to the
369 static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
371 OSSL_STATEM *st = &s->statem;
374 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
375 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
378 switch (st->hand_state) {
380 /* Shouldn't happen */
381 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
382 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION,
383 ERR_R_INTERNAL_ERROR);
384 return WRITE_TRAN_ERROR;
387 if (s->key_update != SSL_KEY_UPDATE_NONE) {
388 st->hand_state = TLS_ST_SW_KEY_UPDATE;
389 return WRITE_TRAN_CONTINUE;
391 /* Try to read from the client instead */
392 return WRITE_TRAN_FINISHED;
394 case TLS_ST_SR_CLNT_HELLO:
395 if (s->hello_retry_request)
396 st->hand_state = TLS_ST_SW_HELLO_RETRY_REQUEST;
398 st->hand_state = TLS_ST_SW_SRVR_HELLO;
399 return WRITE_TRAN_CONTINUE;
401 case TLS_ST_SW_HELLO_RETRY_REQUEST:
402 st->hand_state = TLS_ST_EARLY_DATA;
403 return WRITE_TRAN_CONTINUE;
405 case TLS_ST_SW_SRVR_HELLO:
406 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
407 return WRITE_TRAN_CONTINUE;
409 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
411 st->hand_state = TLS_ST_SW_FINISHED;
412 else if (send_certificate_request(s))
413 st->hand_state = TLS_ST_SW_CERT_REQ;
415 st->hand_state = TLS_ST_SW_CERT;
417 return WRITE_TRAN_CONTINUE;
419 case TLS_ST_SW_CERT_REQ:
420 st->hand_state = TLS_ST_SW_CERT;
421 return WRITE_TRAN_CONTINUE;
424 st->hand_state = TLS_ST_SW_CERT_VRFY;
425 return WRITE_TRAN_CONTINUE;
427 case TLS_ST_SW_CERT_VRFY:
428 st->hand_state = TLS_ST_SW_FINISHED;
429 return WRITE_TRAN_CONTINUE;
431 case TLS_ST_SW_FINISHED:
432 st->hand_state = TLS_ST_EARLY_DATA;
433 return WRITE_TRAN_CONTINUE;
435 case TLS_ST_EARLY_DATA:
436 return WRITE_TRAN_FINISHED;
438 case TLS_ST_SR_FINISHED:
440 * Technically we have finished the handshake at this point, but we're
441 * going to remain "in_init" for now and write out the session ticket
443 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
444 * and give the application the opportunity to delay sending the
447 st->hand_state = TLS_ST_SW_SESSION_TICKET;
448 return WRITE_TRAN_CONTINUE;
450 case TLS_ST_SR_KEY_UPDATE:
451 if (s->key_update != SSL_KEY_UPDATE_NONE) {
452 st->hand_state = TLS_ST_SW_KEY_UPDATE;
453 return WRITE_TRAN_CONTINUE;
457 case TLS_ST_SW_KEY_UPDATE:
458 case TLS_ST_SW_SESSION_TICKET:
459 st->hand_state = TLS_ST_OK;
460 return WRITE_TRAN_CONTINUE;
465 * ossl_statem_server_write_transition() works out what handshake state to move
466 * to next when the server is writing messages to be sent to the client.
468 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
470 OSSL_STATEM *st = &s->statem;
473 * Note that before the ClientHello we don't know what version we are going
474 * to negotiate yet, so we don't take this branch until later
478 return ossl_statem_server13_write_transition(s);
480 switch (st->hand_state) {
482 /* Shouldn't happen */
483 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
484 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION,
485 ERR_R_INTERNAL_ERROR);
486 return WRITE_TRAN_ERROR;
489 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
490 /* We must be trying to renegotiate */
491 st->hand_state = TLS_ST_SW_HELLO_REQ;
492 st->request_state = TLS_ST_BEFORE;
493 return WRITE_TRAN_CONTINUE;
495 /* Must be an incoming ClientHello */
496 if (!tls_setup_handshake(s)) {
497 /* SSLfatal() already called */
498 return WRITE_TRAN_ERROR;
503 /* Just go straight to trying to read from the client */
504 return WRITE_TRAN_FINISHED;
506 case TLS_ST_SW_HELLO_REQ:
507 st->hand_state = TLS_ST_OK;
508 return WRITE_TRAN_CONTINUE;
510 case TLS_ST_SR_CLNT_HELLO:
511 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
512 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
513 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
515 st->hand_state = TLS_ST_SW_SRVR_HELLO;
516 return WRITE_TRAN_CONTINUE;
518 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
519 return WRITE_TRAN_FINISHED;
521 case TLS_ST_SW_SRVR_HELLO:
523 if (s->ext.ticket_expected)
524 st->hand_state = TLS_ST_SW_SESSION_TICKET;
526 st->hand_state = TLS_ST_SW_CHANGE;
528 /* Check if it is anon DH or anon ECDH, */
529 /* normal PSK or SRP */
530 if (!(s->s3->tmp.new_cipher->algorithm_auth &
531 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
532 st->hand_state = TLS_ST_SW_CERT;
533 } else if (send_server_key_exchange(s)) {
534 st->hand_state = TLS_ST_SW_KEY_EXCH;
535 } else if (send_certificate_request(s)) {
536 st->hand_state = TLS_ST_SW_CERT_REQ;
538 st->hand_state = TLS_ST_SW_SRVR_DONE;
541 return WRITE_TRAN_CONTINUE;
544 if (s->ext.status_expected) {
545 st->hand_state = TLS_ST_SW_CERT_STATUS;
546 return WRITE_TRAN_CONTINUE;
550 case TLS_ST_SW_CERT_STATUS:
551 if (send_server_key_exchange(s)) {
552 st->hand_state = TLS_ST_SW_KEY_EXCH;
553 return WRITE_TRAN_CONTINUE;
557 case TLS_ST_SW_KEY_EXCH:
558 if (send_certificate_request(s)) {
559 st->hand_state = TLS_ST_SW_CERT_REQ;
560 return WRITE_TRAN_CONTINUE;
564 case TLS_ST_SW_CERT_REQ:
565 st->hand_state = TLS_ST_SW_SRVR_DONE;
566 return WRITE_TRAN_CONTINUE;
568 case TLS_ST_SW_SRVR_DONE:
569 return WRITE_TRAN_FINISHED;
571 case TLS_ST_SR_FINISHED:
573 st->hand_state = TLS_ST_OK;
574 return WRITE_TRAN_CONTINUE;
575 } else if (s->ext.ticket_expected) {
576 st->hand_state = TLS_ST_SW_SESSION_TICKET;
578 st->hand_state = TLS_ST_SW_CHANGE;
580 return WRITE_TRAN_CONTINUE;
582 case TLS_ST_SW_SESSION_TICKET:
583 st->hand_state = TLS_ST_SW_CHANGE;
584 return WRITE_TRAN_CONTINUE;
586 case TLS_ST_SW_CHANGE:
587 st->hand_state = TLS_ST_SW_FINISHED;
588 return WRITE_TRAN_CONTINUE;
590 case TLS_ST_SW_FINISHED:
592 return WRITE_TRAN_FINISHED;
594 st->hand_state = TLS_ST_OK;
595 return WRITE_TRAN_CONTINUE;
600 * Perform any pre work that needs to be done prior to sending a message from
601 * the server to the client.
603 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
605 OSSL_STATEM *st = &s->statem;
607 switch (st->hand_state) {
609 /* No pre work to be done */
612 case TLS_ST_SW_HELLO_REQ:
615 dtls1_clear_sent_buffer(s);
618 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
620 if (SSL_IS_DTLS(s)) {
621 dtls1_clear_sent_buffer(s);
622 /* We don't buffer this message so don't use the timer */
627 case TLS_ST_SW_SRVR_HELLO:
628 if (SSL_IS_DTLS(s)) {
630 * Messages we write from now on should be buffered and
631 * retransmitted if necessary, so we need to use the timer now
637 case TLS_ST_SW_SRVR_DONE:
638 #ifndef OPENSSL_NO_SCTP
639 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
640 /* Calls SSLfatal() as required */
641 return dtls_wait_for_dry(s);
644 return WORK_FINISHED_CONTINUE;
646 case TLS_ST_SW_SESSION_TICKET:
647 if (SSL_IS_TLS13(s)) {
649 * Actually this is the end of the handshake, but we're going
650 * straight into writing the session ticket out. So we finish off
651 * the handshake, but keep the various buffers active.
653 * Calls SSLfatal as required.
655 return tls_finish_handshake(s, wst, 0);
656 } if (SSL_IS_DTLS(s)) {
658 * We're into the last flight. We don't retransmit the last flight
659 * unless we need to, so we don't use the timer
665 case TLS_ST_SW_CHANGE:
666 s->session->cipher = s->s3->tmp.new_cipher;
667 if (!s->method->ssl3_enc->setup_key_block(s)) {
668 /* SSLfatal() already called */
671 if (SSL_IS_DTLS(s)) {
673 * We're into the last flight. We don't retransmit the last flight
674 * unless we need to, so we don't use the timer. This might have
675 * already been set to 0 if we sent a NewSessionTicket message,
676 * but we'll set it again here in case we didn't.
680 return WORK_FINISHED_CONTINUE;
682 case TLS_ST_EARLY_DATA:
683 if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING)
684 return WORK_FINISHED_CONTINUE;
688 /* Calls SSLfatal() as required */
689 return tls_finish_handshake(s, wst, 1);
692 return WORK_FINISHED_CONTINUE;
696 * Perform any work that needs to be done after sending a message from the
697 * server to the client.
699 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
701 OSSL_STATEM *st = &s->statem;
705 switch (st->hand_state) {
707 /* No post work to be done */
710 case TLS_ST_SW_HELLO_RETRY_REQUEST:
711 if (statem_flush(s) != 1)
715 case TLS_ST_SW_HELLO_REQ:
716 if (statem_flush(s) != 1)
718 if (!ssl3_init_finished_mac(s)) {
719 /* SSLfatal() already called */
724 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
725 if (statem_flush(s) != 1)
727 /* HelloVerifyRequest resets Finished MAC */
728 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
729 /* SSLfatal() already called */
733 * The next message should be another ClientHello which we need to
734 * treat like it was the first packet
739 case TLS_ST_SW_SRVR_HELLO:
740 #ifndef OPENSSL_NO_SCTP
741 if (SSL_IS_DTLS(s) && s->hit) {
742 unsigned char sctpauthkey[64];
743 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
746 * Add new shared key for SCTP-Auth, will be ignored if no
749 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
750 sizeof(DTLS1_SCTP_AUTH_LABEL));
752 if (SSL_export_keying_material(s, sctpauthkey,
753 sizeof(sctpauthkey), labelbuffer,
754 sizeof(labelbuffer), NULL, 0,
756 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
757 SSL_F_OSSL_STATEM_SERVER_POST_WORK,
758 ERR_R_INTERNAL_ERROR);
762 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
763 sizeof(sctpauthkey), sctpauthkey);
767 * TODO(TLS1.3): This actually causes a problem. We don't yet know
768 * whether the next record we are going to receive is an unencrypted
769 * alert, or an encrypted handshake message. We're going to need
770 * something clever in the record layer for this.
772 if (SSL_IS_TLS13(s)) {
773 if (!s->method->ssl3_enc->setup_key_block(s)
774 || !s->method->ssl3_enc->change_cipher_state(s,
775 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
776 /* SSLfatal() already called */
780 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
781 && !s->method->ssl3_enc->change_cipher_state(s,
782 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
783 /* SSLfatal() already called */
789 case TLS_ST_SW_CHANGE:
790 #ifndef OPENSSL_NO_SCTP
791 if (SSL_IS_DTLS(s) && !s->hit) {
793 * Change to new shared key of SCTP-Auth, will be ignored if
796 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
800 if (!s->method->ssl3_enc->change_cipher_state(s,
801 SSL3_CHANGE_CIPHER_SERVER_WRITE))
803 /* SSLfatal() already called */
808 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
811 case TLS_ST_SW_SRVR_DONE:
812 if (statem_flush(s) != 1)
816 case TLS_ST_SW_FINISHED:
817 if (statem_flush(s) != 1)
819 #ifndef OPENSSL_NO_SCTP
820 if (SSL_IS_DTLS(s) && s->hit) {
822 * Change to new shared key of SCTP-Auth, will be ignored if
825 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
829 if (SSL_IS_TLS13(s)) {
830 if (!s->method->ssl3_enc->generate_master_secret(s,
831 s->master_secret, s->handshake_secret, 0,
832 &s->session->master_key_length)
833 || !s->method->ssl3_enc->change_cipher_state(s,
834 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
835 /* SSLfatal() already called */
840 case TLS_ST_SW_KEY_UPDATE:
841 if (statem_flush(s) != 1)
843 if (!tls13_update_key(s, 1)) {
844 /* SSLfatal() already called */
849 case TLS_ST_SW_SESSION_TICKET:
850 if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
855 return WORK_FINISHED_CONTINUE;
859 * Get the message construction function and message type for sending from the
862 * Valid return values are:
866 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
867 confunc_f *confunc, int *mt)
869 OSSL_STATEM *st = &s->statem;
871 switch (st->hand_state) {
873 /* Shouldn't happen */
874 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
875 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE,
876 SSL_R_BAD_HANDSHAKE_STATE);
879 case TLS_ST_SW_CHANGE:
881 *confunc = dtls_construct_change_cipher_spec;
883 *confunc = tls_construct_change_cipher_spec;
884 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
887 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
888 *confunc = dtls_construct_hello_verify_request;
889 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
892 case TLS_ST_SW_HELLO_REQ:
893 /* No construction function needed */
895 *mt = SSL3_MT_HELLO_REQUEST;
898 case TLS_ST_SW_SRVR_HELLO:
899 *confunc = tls_construct_server_hello;
900 *mt = SSL3_MT_SERVER_HELLO;
904 *confunc = tls_construct_server_certificate;
905 *mt = SSL3_MT_CERTIFICATE;
908 case TLS_ST_SW_CERT_VRFY:
909 *confunc = tls_construct_cert_verify;
910 *mt = SSL3_MT_CERTIFICATE_VERIFY;
914 case TLS_ST_SW_KEY_EXCH:
915 *confunc = tls_construct_server_key_exchange;
916 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
919 case TLS_ST_SW_CERT_REQ:
920 *confunc = tls_construct_certificate_request;
921 *mt = SSL3_MT_CERTIFICATE_REQUEST;
924 case TLS_ST_SW_SRVR_DONE:
925 *confunc = tls_construct_server_done;
926 *mt = SSL3_MT_SERVER_DONE;
929 case TLS_ST_SW_SESSION_TICKET:
930 *confunc = tls_construct_new_session_ticket;
931 *mt = SSL3_MT_NEWSESSION_TICKET;
934 case TLS_ST_SW_CERT_STATUS:
935 *confunc = tls_construct_cert_status;
936 *mt = SSL3_MT_CERTIFICATE_STATUS;
939 case TLS_ST_SW_FINISHED:
940 *confunc = tls_construct_finished;
941 *mt = SSL3_MT_FINISHED;
944 case TLS_ST_EARLY_DATA:
949 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
950 *confunc = tls_construct_encrypted_extensions;
951 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
954 case TLS_ST_SW_HELLO_RETRY_REQUEST:
955 *confunc = tls_construct_hello_retry_request;
956 *mt = SSL3_MT_HELLO_RETRY_REQUEST;
959 case TLS_ST_SW_KEY_UPDATE:
960 *confunc = tls_construct_key_update;
961 *mt = SSL3_MT_KEY_UPDATE;
969 * Maximum size (excluding the Handshake header) of a ClientHello message,
970 * calculated as follows:
972 * 2 + # client_version
973 * 32 + # only valid length for random
974 * 1 + # length of session_id
975 * 32 + # maximum size for session_id
976 * 2 + # length of cipher suites
977 * 2^16-2 + # maximum length of cipher suites array
978 * 1 + # length of compression_methods
979 * 2^8-1 + # maximum length of compression methods
980 * 2 + # length of extensions
981 * 2^16-1 # maximum length of extensions
983 #define CLIENT_HELLO_MAX_LENGTH 131396
985 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
986 #define NEXT_PROTO_MAX_LENGTH 514
989 * Returns the maximum allowed length for the current message that we are
990 * reading. Excludes the message header.
992 size_t ossl_statem_server_max_message_size(SSL *s)
994 OSSL_STATEM *st = &s->statem;
996 switch (st->hand_state) {
998 /* Shouldn't happen */
1001 case TLS_ST_SR_CLNT_HELLO:
1002 return CLIENT_HELLO_MAX_LENGTH;
1004 case TLS_ST_SR_END_OF_EARLY_DATA:
1005 return END_OF_EARLY_DATA_MAX_LENGTH;
1007 case TLS_ST_SR_CERT:
1008 return s->max_cert_list;
1010 case TLS_ST_SR_KEY_EXCH:
1011 return CLIENT_KEY_EXCH_MAX_LENGTH;
1013 case TLS_ST_SR_CERT_VRFY:
1014 return SSL3_RT_MAX_PLAIN_LENGTH;
1016 #ifndef OPENSSL_NO_NEXTPROTONEG
1017 case TLS_ST_SR_NEXT_PROTO:
1018 return NEXT_PROTO_MAX_LENGTH;
1021 case TLS_ST_SR_CHANGE:
1022 return CCS_MAX_LENGTH;
1024 case TLS_ST_SR_FINISHED:
1025 return FINISHED_MAX_LENGTH;
1027 case TLS_ST_SR_KEY_UPDATE:
1028 return KEY_UPDATE_MAX_LENGTH;
1033 * Process a message that the server has received from the client.
1035 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1037 OSSL_STATEM *st = &s->statem;
1039 switch (st->hand_state) {
1041 /* Shouldn't happen */
1042 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1043 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE,
1044 ERR_R_INTERNAL_ERROR);
1045 return MSG_PROCESS_ERROR;
1047 case TLS_ST_SR_CLNT_HELLO:
1048 return tls_process_client_hello(s, pkt);
1050 case TLS_ST_SR_END_OF_EARLY_DATA:
1051 return tls_process_end_of_early_data(s, pkt);
1053 case TLS_ST_SR_CERT:
1054 return tls_process_client_certificate(s, pkt);
1056 case TLS_ST_SR_KEY_EXCH:
1057 return tls_process_client_key_exchange(s, pkt);
1059 case TLS_ST_SR_CERT_VRFY:
1060 return tls_process_cert_verify(s, pkt);
1062 #ifndef OPENSSL_NO_NEXTPROTONEG
1063 case TLS_ST_SR_NEXT_PROTO:
1064 return tls_process_next_proto(s, pkt);
1067 case TLS_ST_SR_CHANGE:
1068 return tls_process_change_cipher_spec(s, pkt);
1070 case TLS_ST_SR_FINISHED:
1071 return tls_process_finished(s, pkt);
1073 case TLS_ST_SR_KEY_UPDATE:
1074 return tls_process_key_update(s, pkt);
1080 * Perform any further processing required following the receipt of a message
1083 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1085 OSSL_STATEM *st = &s->statem;
1087 switch (st->hand_state) {
1089 /* Shouldn't happen */
1090 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1091 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE,
1092 ERR_R_INTERNAL_ERROR);
1095 case TLS_ST_SR_CLNT_HELLO:
1096 return tls_post_process_client_hello(s, wst);
1098 case TLS_ST_SR_KEY_EXCH:
1099 return tls_post_process_client_key_exchange(s, wst);
1101 return WORK_FINISHED_CONTINUE;
1104 #ifndef OPENSSL_NO_SRP
1105 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1106 static int ssl_check_srp_ext_ClientHello(SSL *s)
1109 int al = SSL_AD_UNRECOGNIZED_NAME;
1111 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1112 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1113 if (s->srp_ctx.login == NULL) {
1115 * RFC 5054 says SHOULD reject, we do so if There is no srp
1118 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
1119 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1120 SSL_R_PSK_IDENTITY_NOT_FOUND);
1123 ret = SSL_srp_server_param_with_username(s, &al);
1126 if (ret == SSL3_AL_FATAL) {
1127 SSLfatal(s, al, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1128 al == SSL_AD_UNKNOWN_PSK_IDENTITY
1129 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1130 : SSL_R_CLIENTHELLO_TLSEXT);
1139 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1142 /* Always use DTLS 1.0 version: see RFC 6347 */
1143 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1144 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1150 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1152 unsigned int cookie_leni;
1153 if (s->ctx->app_gen_cookie_cb == NULL ||
1154 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1155 &cookie_leni) == 0 ||
1156 cookie_leni > 255) {
1157 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1158 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1161 s->d1->cookie_len = cookie_leni;
1163 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1164 s->d1->cookie_len)) {
1165 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1166 ERR_R_INTERNAL_ERROR);
1173 #ifndef OPENSSL_NO_EC
1175 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1176 * SecureTransport using the TLS extension block in |hello|.
1177 * Safari, since 10.6, sends exactly these extensions, in this order:
1181 * signature_algorithms (for TLSv1.2 only)
1183 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1184 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1185 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1186 * 10.8..10.8.3 (which don't work).
1188 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1190 static const unsigned char kSafariExtensionsBlock[] = {
1191 0x00, 0x0a, /* elliptic_curves extension */
1192 0x00, 0x08, /* 8 bytes */
1193 0x00, 0x06, /* 6 bytes of curve ids */
1194 0x00, 0x17, /* P-256 */
1195 0x00, 0x18, /* P-384 */
1196 0x00, 0x19, /* P-521 */
1198 0x00, 0x0b, /* ec_point_formats */
1199 0x00, 0x02, /* 2 bytes */
1200 0x01, /* 1 point format */
1201 0x00, /* uncompressed */
1202 /* The following is only present in TLS 1.2 */
1203 0x00, 0x0d, /* signature_algorithms */
1204 0x00, 0x0c, /* 12 bytes */
1205 0x00, 0x0a, /* 10 bytes */
1206 0x05, 0x01, /* SHA-384/RSA */
1207 0x04, 0x01, /* SHA-256/RSA */
1208 0x02, 0x01, /* SHA-1/RSA */
1209 0x04, 0x03, /* SHA-256/ECDSA */
1210 0x02, 0x03, /* SHA-1/ECDSA */
1212 /* Length of the common prefix (first two extensions). */
1213 static const size_t kSafariCommonExtensionsLength = 18;
1218 tmppkt = hello->extensions;
1220 if (!PACKET_forward(&tmppkt, 2)
1221 || !PACKET_get_net_2(&tmppkt, &type)
1222 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1226 if (type != TLSEXT_TYPE_server_name)
1229 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1230 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1232 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1235 #endif /* !OPENSSL_NO_EC */
1237 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1239 /* |cookie| will only be initialized for DTLS. */
1240 PACKET session_id, compression, extensions, cookie;
1241 static const unsigned char null_compression = 0;
1242 CLIENTHELLO_MSG *clienthello;
1244 clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1245 if (clienthello == NULL) {
1246 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1247 ERR_R_INTERNAL_ERROR);
1250 /* Check if this is actually an unexpected renegotiation ClientHello */
1251 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1252 if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
1253 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1261 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1263 clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1264 PACKET_null_init(&cookie);
1266 if (clienthello->isv2) {
1269 if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {
1270 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1271 SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1276 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1277 * header is sent directly on the wire, not wrapped as a TLS
1278 * record. Our record layer just processes the message length and passes
1279 * the rest right through. Its format is:
1281 * 0-1 msg_length - decoded by the record layer
1282 * 2 msg_type - s->init_msg points here
1284 * 5-6 cipher_spec_length
1285 * 7-8 session_id_length
1286 * 9-10 challenge_length
1290 if (!PACKET_get_1(pkt, &mt)
1291 || mt != SSL2_MT_CLIENT_HELLO) {
1293 * Should never happen. We should have tested this in the record
1294 * layer in order to have determined that this is a SSLv2 record
1295 * in the first place
1297 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1298 ERR_R_INTERNAL_ERROR);
1303 if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1304 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1305 SSL_R_LENGTH_TOO_SHORT);
1309 /* Parse the message and load client random. */
1310 if (clienthello->isv2) {
1312 * Handle an SSLv2 backwards compatible ClientHello
1313 * Note, this is only for SSLv3+ using the backward compatible format.
1314 * Real SSLv2 is not supported, and is rejected below.
1316 unsigned int ciphersuite_len, session_id_len, challenge_len;
1319 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1320 || !PACKET_get_net_2(pkt, &session_id_len)
1321 || !PACKET_get_net_2(pkt, &challenge_len)) {
1322 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1323 SSL_R_RECORD_LENGTH_MISMATCH);
1327 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1328 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1329 SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1333 if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1335 || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1336 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1337 /* No extensions. */
1338 || PACKET_remaining(pkt) != 0) {
1339 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1340 SSL_R_RECORD_LENGTH_MISMATCH);
1343 clienthello->session_id_len = session_id_len;
1345 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1346 * here rather than sizeof(clienthello->random) because that is the limit
1347 * for SSLv3 and it is fixed. It won't change even if
1348 * sizeof(clienthello->random) does.
1350 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1351 ? SSL3_RANDOM_SIZE : challenge_len;
1352 memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1353 if (!PACKET_copy_bytes(&challenge,
1354 clienthello->random + SSL3_RANDOM_SIZE -
1355 challenge_len, challenge_len)
1356 /* Advertise only null compression. */
1357 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1358 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1359 ERR_R_INTERNAL_ERROR);
1363 PACKET_null_init(&clienthello->extensions);
1365 /* Regular ClientHello. */
1366 if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1367 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1368 || !PACKET_copy_all(&session_id, clienthello->session_id,
1369 SSL_MAX_SSL_SESSION_ID_LENGTH,
1370 &clienthello->session_id_len)) {
1371 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1372 SSL_R_LENGTH_MISMATCH);
1376 if (SSL_IS_DTLS(s)) {
1377 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1378 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1379 SSL_R_LENGTH_MISMATCH);
1382 if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1383 DTLS1_COOKIE_LENGTH,
1384 &clienthello->dtls_cookie_len)) {
1385 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1386 SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1390 * If we require cookies and this ClientHello doesn't contain one,
1391 * just return since we do not want to allocate any memory yet.
1392 * So check cookie length...
1394 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1395 if (clienthello->dtls_cookie_len == 0)
1396 return MSG_PROCESS_FINISHED_READING;
1400 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1401 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1402 SSL_R_LENGTH_MISMATCH);
1406 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1407 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1408 SSL_R_LENGTH_MISMATCH);
1412 /* Could be empty. */
1413 if (PACKET_remaining(pkt) == 0) {
1414 PACKET_null_init(&clienthello->extensions);
1416 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
1417 || PACKET_remaining(pkt) != 0) {
1418 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1419 SSL_R_LENGTH_MISMATCH);
1425 if (!PACKET_copy_all(&compression, clienthello->compressions,
1426 MAX_COMPRESSIONS_SIZE,
1427 &clienthello->compressions_len)) {
1428 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1429 ERR_R_INTERNAL_ERROR);
1433 /* Preserve the raw extensions PACKET for later use */
1434 extensions = clienthello->extensions;
1435 if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
1436 &clienthello->pre_proc_exts,
1437 &clienthello->pre_proc_exts_len, 1)) {
1438 /* SSLfatal already been called */
1441 s->clienthello = clienthello;
1443 return MSG_PROCESS_CONTINUE_PROCESSING;
1446 if (clienthello != NULL)
1447 OPENSSL_free(clienthello->pre_proc_exts);
1448 OPENSSL_free(clienthello);
1450 return MSG_PROCESS_ERROR;
1453 static int tls_early_post_process_client_hello(SSL *s)
1456 int i, al = SSL_AD_INTERNAL_ERROR;
1460 #ifndef OPENSSL_NO_COMP
1461 SSL_COMP *comp = NULL;
1463 const SSL_CIPHER *c;
1464 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1465 STACK_OF(SSL_CIPHER) *scsvs = NULL;
1466 CLIENTHELLO_MSG *clienthello = s->clienthello;
1467 DOWNGRADE dgrd = DOWNGRADE_NONE;
1469 /* Finished parsing the ClientHello, now we can start processing it */
1470 /* Give the ClientHello callback a crack at things */
1471 if (s->ctx->client_hello_cb != NULL) {
1472 /* A failure in the ClientHello callback terminates the connection. */
1473 switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
1474 case SSL_CLIENT_HELLO_SUCCESS:
1476 case SSL_CLIENT_HELLO_RETRY:
1477 s->rwstate = SSL_CLIENT_HELLO_CB;
1479 case SSL_CLIENT_HELLO_ERROR:
1482 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1483 SSL_R_CALLBACK_FAILED);
1488 /* Set up the client_random */
1489 memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
1491 /* Choose the version */
1493 if (clienthello->isv2) {
1494 if (clienthello->legacy_version == SSL2_VERSION
1495 || (clienthello->legacy_version & 0xff00)
1496 != (SSL3_VERSION_MAJOR << 8)) {
1498 * This is real SSLv2 or something completely unknown. We don't
1501 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1502 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1503 SSL_R_UNKNOWN_PROTOCOL);
1507 s->client_version = clienthello->legacy_version;
1510 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1511 * versions are potentially compatible. Version negotiation comes later.
1513 if (!SSL_IS_DTLS(s)) {
1514 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1515 } else if (s->method->version != DTLS_ANY_VERSION &&
1516 DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1517 protverr = SSL_R_VERSION_TOO_LOW;
1523 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1524 /* like ssl3_get_record, send alert using remote version number */
1525 s->version = s->client_version = clienthello->legacy_version;
1527 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1528 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1532 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1533 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1534 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1535 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1536 SSL_R_NOT_ON_RECORD_BOUNDARY);
1540 if (SSL_IS_DTLS(s)) {
1541 /* Empty cookie was already handled above by returning early. */
1542 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1543 if (s->ctx->app_verify_cookie_cb != NULL) {
1544 if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1545 clienthello->dtls_cookie_len) == 0) {
1546 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1547 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1548 SSL_R_COOKIE_MISMATCH);
1550 /* else cookie verification succeeded */
1552 /* default verification */
1553 } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1554 || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1555 s->d1->cookie_len) != 0) {
1556 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1557 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1558 SSL_R_COOKIE_MISMATCH);
1561 s->d1->cookie_verified = 1;
1563 if (s->method->version == DTLS_ANY_VERSION) {
1564 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1565 if (protverr != 0) {
1566 s->version = s->client_version;
1567 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1568 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1576 if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1577 clienthello->isv2) ||
1578 !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
1579 clienthello->isv2, 1)) {
1580 /* SSLfatal() already called */
1584 s->s3->send_connection_binding = 0;
1585 /* Check what signalling cipher-suite values were received. */
1586 if (scsvs != NULL) {
1587 for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1588 c = sk_SSL_CIPHER_value(scsvs, i);
1589 if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1590 if (s->renegotiate) {
1591 /* SCSV is fatal if renegotiating */
1592 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1593 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1594 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1597 s->s3->send_connection_binding = 1;
1598 } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1599 !ssl_check_version_downgrade(s)) {
1601 * This SCSV indicates that the client previously tried
1602 * a higher version. We should fail if the current version
1603 * is an unexpected downgrade, as that indicates that the first
1604 * connection may have been tampered with in order to trigger
1605 * an insecure downgrade.
1607 SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
1608 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1609 SSL_R_INAPPROPRIATE_FALLBACK);
1615 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1616 if (SSL_IS_TLS13(s)) {
1617 const SSL_CIPHER *cipher =
1618 ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
1620 if (cipher == NULL) {
1621 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1622 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1623 SSL_R_NO_SHARED_CIPHER);
1626 if (s->hello_retry_request
1627 && (s->s3->tmp.new_cipher == NULL
1628 || s->s3->tmp.new_cipher->id != cipher->id)) {
1630 * A previous HRR picked a different ciphersuite to the one we
1631 * just selected. Something must have changed.
1633 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1634 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1638 s->s3->tmp.new_cipher = cipher;
1641 /* We need to do this before getting the session */
1642 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1643 SSL_EXT_CLIENT_HELLO,
1644 clienthello->pre_proc_exts, NULL, 0)) {
1645 /* SSLfatal() already called */
1650 * We don't allow resumption in a backwards compatible ClientHello.
1651 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1653 * Versions before 0.9.7 always allow clients to resume sessions in
1654 * renegotiation. 0.9.7 and later allow this by default, but optionally
1655 * ignore resumption requests with flag
1656 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1657 * than a change to default behavior so that applications relying on
1658 * this for security won't even compile against older library versions).
1659 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1660 * request renegotiation but not a new session (s->new_session remains
1661 * unset): for servers, this essentially just means that the
1662 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1665 if (clienthello->isv2 ||
1667 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1668 if (!ssl_get_new_session(s, 1)) {
1669 /* SSLfatal() already called */
1673 i = ssl_get_prev_session(s, clienthello);
1675 /* previous session */
1677 } else if (i == -1) {
1678 /* SSLfatal() already called */
1682 if (!ssl_get_new_session(s, 1)) {
1683 /* SSLfatal() already called */
1689 if (SSL_IS_TLS13(s)) {
1690 memcpy(s->tmp_session_id, s->clienthello->session_id,
1691 s->clienthello->session_id_len);
1692 s->tmp_session_id_len = s->clienthello->session_id_len;
1696 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1697 * ciphersuite compatibility with the session as part of resumption.
1699 if (!SSL_IS_TLS13(s) && s->hit) {
1701 id = s->session->cipher->id;
1704 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1706 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1707 c = sk_SSL_CIPHER_value(ciphers, i);
1709 fprintf(stderr, "client [%2d of %2d]:%s\n",
1710 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1719 * we need to have the cipher in the cipher list if we are asked
1722 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1723 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1724 SSL_R_REQUIRED_CIPHER_MISSING);
1729 for (loop = 0; loop < clienthello->compressions_len; loop++) {
1730 if (clienthello->compressions[loop] == 0)
1734 if (loop >= clienthello->compressions_len) {
1736 SSLfatal(s, SSL_AD_DECODE_ERROR,
1737 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1738 SSL_R_NO_COMPRESSION_SPECIFIED);
1742 #ifndef OPENSSL_NO_EC
1743 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1744 ssl_check_for_safari(s, clienthello);
1745 #endif /* !OPENSSL_NO_EC */
1747 /* TLS extensions */
1748 if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
1749 clienthello->pre_proc_exts, NULL, 0, 1)) {
1750 /* SSLfatal() already called */
1755 * Check if we want to use external pre-shared secret for this handshake
1756 * for not reused session only. We need to generate server_random before
1757 * calling tls_session_secret_cb in order to allow SessionTicket
1758 * processing to use it in key derivation.
1762 pos = s->s3->server_random;
1763 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
1764 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1765 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1766 ERR_R_INTERNAL_ERROR);
1772 && s->version >= TLS1_VERSION
1775 && s->ext.session_secret_cb) {
1776 const SSL_CIPHER *pref_cipher = NULL;
1778 * s->session->master_key_length is a size_t, but this is an int for
1779 * backwards compat reasons
1781 int master_key_length;
1783 master_key_length = sizeof(s->session->master_key);
1784 if (s->ext.session_secret_cb(s, s->session->master_key,
1785 &master_key_length, ciphers,
1787 s->ext.session_secret_cb_arg)
1788 && master_key_length > 0) {
1789 s->session->master_key_length = master_key_length;
1791 s->session->ciphers = ciphers;
1792 s->session->verify_result = X509_V_OK;
1796 /* check if some cipher was preferred by call back */
1797 if (pref_cipher == NULL)
1798 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1799 SSL_get_ciphers(s));
1800 if (pref_cipher == NULL) {
1801 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1802 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1803 SSL_R_NO_SHARED_CIPHER);
1807 s->session->cipher = pref_cipher;
1808 sk_SSL_CIPHER_free(s->cipher_list);
1809 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1810 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1811 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1816 * Worst case, we will use the NULL compression, but if we have other
1817 * options, we will now look for them. We have complen-1 compression
1818 * algorithms from the client, starting at q.
1820 s->s3->tmp.new_compression = NULL;
1821 if (SSL_IS_TLS13(s)) {
1823 * We already checked above that the NULL compression method appears in
1824 * the list. Now we check there aren't any others (which is illegal in
1825 * a TLSv1.3 ClientHello.
1827 if (clienthello->compressions_len != 1) {
1828 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1829 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1830 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1834 #ifndef OPENSSL_NO_COMP
1835 /* This only happens if we have a cache hit */
1836 else if (s->session->compress_meth != 0) {
1837 int m, comp_id = s->session->compress_meth;
1839 /* Perform sanity checks on resumed compression algorithm */
1840 /* Can't disable compression */
1841 if (!ssl_allow_compression(s)) {
1842 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1843 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1844 SSL_R_INCONSISTENT_COMPRESSION);
1847 /* Look for resumed compression method */
1848 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1849 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1850 if (comp_id == comp->id) {
1851 s->s3->tmp.new_compression = comp;
1855 if (s->s3->tmp.new_compression == NULL) {
1856 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1857 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1858 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1861 /* Look for resumed method in compression list */
1862 for (k = 0; k < clienthello->compressions_len; k++) {
1863 if (clienthello->compressions[k] == comp_id)
1866 if (k >= clienthello->compressions_len) {
1867 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1868 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1869 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1872 } else if (s->hit) {
1874 } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1875 /* See if we have a match */
1876 int m, nn, v, done = 0;
1879 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1880 for (m = 0; m < nn; m++) {
1881 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1883 for (o = 0; o < clienthello->compressions_len; o++) {
1884 if (v == clienthello->compressions[o]) {
1893 s->s3->tmp.new_compression = comp;
1899 * If compression is disabled we'd better not try to resume a session
1900 * using compression.
1902 if (s->session->compress_meth != 0) {
1903 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1904 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1905 SSL_R_INCONSISTENT_COMPRESSION);
1911 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1914 if (!s->hit || SSL_IS_TLS13(s)) {
1915 sk_SSL_CIPHER_free(s->session->ciphers);
1916 s->session->ciphers = ciphers;
1917 if (ciphers == NULL) {
1918 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1919 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1920 ERR_R_INTERNAL_ERROR);
1927 #ifdef OPENSSL_NO_COMP
1928 s->session->compress_meth = 0;
1930 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1932 if (!tls1_set_server_sigalgs(s)) {
1933 /* SSLfatal() already called */
1938 sk_SSL_CIPHER_free(ciphers);
1939 sk_SSL_CIPHER_free(scsvs);
1940 OPENSSL_free(clienthello->pre_proc_exts);
1941 OPENSSL_free(s->clienthello);
1942 s->clienthello = NULL;
1945 sk_SSL_CIPHER_free(ciphers);
1946 sk_SSL_CIPHER_free(scsvs);
1947 OPENSSL_free(clienthello->pre_proc_exts);
1948 OPENSSL_free(s->clienthello);
1949 s->clienthello = NULL;
1955 * Call the status request callback if needed. Upon success, returns 1.
1956 * Upon failure, returns 0.
1958 static int tls_handle_status_request(SSL *s)
1960 s->ext.status_expected = 0;
1963 * If status request then ask callback what to do. Note: this must be
1964 * called after servername callbacks in case the certificate has changed,
1965 * and must be called after the cipher has been chosen because this may
1966 * influence which certificate is sent
1968 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
1969 && s->ctx->ext.status_cb != NULL) {
1972 /* If no certificate can't return certificate status */
1973 if (s->s3->tmp.cert != NULL) {
1975 * Set current certificate to one we will use so SSL_get_certificate
1976 * et al can pick it up.
1978 s->cert->key = s->s3->tmp.cert;
1979 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
1981 /* We don't want to send a status request response */
1982 case SSL_TLSEXT_ERR_NOACK:
1983 s->ext.status_expected = 0;
1985 /* status request response should be sent */
1986 case SSL_TLSEXT_ERR_OK:
1987 if (s->ext.ocsp.resp)
1988 s->ext.status_expected = 1;
1990 /* something bad happened */
1991 case SSL_TLSEXT_ERR_ALERT_FATAL:
1993 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1994 SSL_F_TLS_HANDLE_STATUS_REQUEST,
1995 SSL_R_CLIENTHELLO_TLSEXT);
2005 * Call the alpn_select callback if needed. Upon success, returns 1.
2006 * Upon failure, returns 0.
2008 int tls_handle_alpn(SSL *s)
2010 const unsigned char *selected = NULL;
2011 unsigned char selected_len = 0;
2013 if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
2014 int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
2015 s->s3->alpn_proposed,
2016 (unsigned int)s->s3->alpn_proposed_len,
2017 s->ctx->ext.alpn_select_cb_arg);
2019 if (r == SSL_TLSEXT_ERR_OK) {
2020 OPENSSL_free(s->s3->alpn_selected);
2021 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
2022 if (s->s3->alpn_selected == NULL) {
2023 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_HANDLE_ALPN,
2024 ERR_R_INTERNAL_ERROR);
2027 s->s3->alpn_selected_len = selected_len;
2028 #ifndef OPENSSL_NO_NEXTPROTONEG
2029 /* ALPN takes precedence over NPN. */
2030 s->s3->npn_seen = 0;
2033 /* Check ALPN is consistent with session */
2034 if (s->session->ext.alpn_selected == NULL
2035 || selected_len != s->session->ext.alpn_selected_len
2036 || memcmp(selected, s->session->ext.alpn_selected,
2037 selected_len) != 0) {
2038 /* Not consistent so can't be used for early_data */
2039 s->ext.early_data_ok = 0;
2042 /* If a new session update it with the new ALPN value */
2043 s->session->ext.alpn_selected = OPENSSL_memdup(selected,
2045 if (s->session->ext.alpn_selected == NULL) {
2046 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2047 SSL_F_TLS_HANDLE_ALPN,
2048 ERR_R_INTERNAL_ERROR);
2051 s->session->ext.alpn_selected_len = selected_len;
2056 } else if (r != SSL_TLSEXT_ERR_NOACK) {
2057 SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, SSL_F_TLS_HANDLE_ALPN,
2058 SSL_R_NO_APPLICATION_PROTOCOL);
2062 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2067 /* Check ALPN is consistent with session */
2068 if (s->session->ext.alpn_selected != NULL) {
2069 /* Not consistent so can't be used for early_data */
2070 s->ext.early_data_ok = 0;
2076 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
2078 const SSL_CIPHER *cipher;
2080 if (wst == WORK_MORE_A) {
2081 int rv = tls_early_post_process_client_hello(s);
2083 /* SSLfatal() was already called */
2090 if (wst == WORK_MORE_B) {
2091 if (!s->hit || SSL_IS_TLS13(s)) {
2092 /* Let cert callback update server certificates if required */
2093 if (!s->hit && s->cert->cert_cb != NULL) {
2094 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2096 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2097 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2098 SSL_R_CERT_CB_ERROR);
2102 s->rwstate = SSL_X509_LOOKUP;
2105 s->rwstate = SSL_NOTHING;
2108 /* In TLSv1.3 we selected the ciphersuite before resumption */
2109 if (!SSL_IS_TLS13(s)) {
2111 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
2113 if (cipher == NULL) {
2114 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2115 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2116 SSL_R_NO_SHARED_CIPHER);
2119 s->s3->tmp.new_cipher = cipher;
2122 if (!tls_choose_sigalg(s, 1)) {
2123 /* SSLfatal already called */
2126 /* check whether we should disable session resumption */
2127 if (s->not_resumable_session_cb != NULL)
2128 s->session->not_resumable =
2129 s->not_resumable_session_cb(s,
2130 ((s->s3->tmp.new_cipher->algorithm_mkey
2131 & (SSL_kDHE | SSL_kECDHE)) != 0));
2132 if (s->session->not_resumable)
2133 /* do not send a session ticket */
2134 s->ext.ticket_expected = 0;
2137 /* Session-id reuse */
2138 s->s3->tmp.new_cipher = s->session->cipher;
2142 * we now have the following setup.
2144 * cipher_list - our preferred list of ciphers
2145 * ciphers - the clients preferred list of ciphers
2146 * compression - basically ignored right now
2147 * ssl version is set - sslv3
2148 * s->session - The ssl session has been setup.
2149 * s->hit - session reuse flag
2150 * s->s3->tmp.new_cipher- the new cipher to use.
2154 * Call status_request callback if needed. Has to be done after the
2155 * certificate callbacks etc above.
2157 if (!tls_handle_status_request(s)) {
2158 /* SSLfatal() already called */
2162 * Call alpn_select callback if needed. Has to be done after SNI and
2163 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2164 * we already did this because cipher negotiation happens earlier, and
2165 * we must handle ALPN before we decide whether to accept early_data.
2167 if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
2168 /* SSLfatal() already called */
2174 #ifndef OPENSSL_NO_SRP
2175 if (wst == WORK_MORE_C) {
2177 if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
2179 * callback indicates further work to be done
2181 s->rwstate = SSL_X509_LOOKUP;
2185 /* SSLfatal() already called */
2191 return WORK_FINISHED_STOP;
2196 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
2201 unsigned char *session_id;
2203 version = SSL_IS_TLS13(s) ? TLS1_2_VERSION : s->version;
2204 if (!WPACKET_put_bytes_u16(pkt, version)
2206 * Random stuff. Filling of the server_random takes place in
2207 * tls_process_client_hello()
2209 || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
2210 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2211 ERR_R_INTERNAL_ERROR);
2216 * There are several cases for the session ID to send
2217 * back in the server hello:
2218 * - For session reuse from the session cache,
2219 * we send back the old session ID.
2220 * - If stateless session reuse (using a session ticket)
2221 * is successful, we send back the client's "session ID"
2222 * (which doesn't actually identify the session).
2223 * - If it is a new session, we send back the new
2225 * - However, if we want the new session to be single-use,
2226 * we send back a 0-length session ID.
2227 * - In TLSv1.3 we echo back the session id sent to us by the client
2229 * s->hit is non-zero in either case of session reuse,
2230 * so the following won't overwrite an ID that we're supposed
2233 if (s->session->not_resumable ||
2234 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2236 s->session->session_id_length = 0;
2238 if (SSL_IS_TLS13(s)) {
2239 sl = s->tmp_session_id_len;
2240 session_id = s->tmp_session_id;
2242 sl = s->session->session_id_length;
2243 session_id = s->session->session_id;
2246 if (sl > sizeof(s->session->session_id)) {
2247 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2248 ERR_R_INTERNAL_ERROR);
2252 /* set up the compression method */
2253 #ifdef OPENSSL_NO_COMP
2256 if (SSL_IS_TLS13(s) || s->s3->tmp.new_compression == NULL)
2259 compm = s->s3->tmp.new_compression->id;
2262 if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
2263 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
2264 || !WPACKET_put_bytes_u8(pkt, compm)
2265 || !tls_construct_extensions(s, pkt,
2267 ? SSL_EXT_TLS1_3_SERVER_HELLO
2268 : SSL_EXT_TLS1_2_SERVER_HELLO,
2270 /* SSLfatal() already called */
2274 if (!(s->verify_mode & SSL_VERIFY_PEER)
2275 && !ssl3_digest_cached_records(s, 0)) {
2276 /* SSLfatal() already called */;
2283 int tls_construct_server_done(SSL *s, WPACKET *pkt)
2285 if (!s->s3->tmp.cert_request) {
2286 if (!ssl3_digest_cached_records(s, 0)) {
2287 /* SSLfatal() already called */
2294 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2296 #ifndef OPENSSL_NO_DH
2297 EVP_PKEY *pkdh = NULL;
2299 #ifndef OPENSSL_NO_EC
2300 unsigned char *encodedPoint = NULL;
2301 size_t encodedlen = 0;
2304 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2308 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2309 EVP_PKEY_CTX *pctx = NULL;
2310 size_t paramlen, paramoffset;
2312 if (!WPACKET_get_total_written(pkt, ¶moffset)) {
2313 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2314 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2318 if (md_ctx == NULL) {
2319 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2320 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2324 type = s->s3->tmp.new_cipher->algorithm_mkey;
2326 r[0] = r[1] = r[2] = r[3] = NULL;
2327 #ifndef OPENSSL_NO_PSK
2328 /* Plain PSK or RSAPSK nothing to do */
2329 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2331 #endif /* !OPENSSL_NO_PSK */
2332 #ifndef OPENSSL_NO_DH
2333 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2334 CERT *cert = s->cert;
2336 EVP_PKEY *pkdhp = NULL;
2339 if (s->cert->dh_tmp_auto) {
2340 DH *dhp = ssl_get_auto_dh(s);
2341 pkdh = EVP_PKEY_new();
2342 if (pkdh == NULL || dhp == NULL) {
2344 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2345 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2346 ERR_R_INTERNAL_ERROR);
2349 EVP_PKEY_assign_DH(pkdh, dhp);
2352 pkdhp = cert->dh_tmp;
2354 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2355 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2356 pkdh = ssl_dh_to_pkey(dhp);
2358 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2359 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2360 ERR_R_INTERNAL_ERROR);
2365 if (pkdhp == NULL) {
2366 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2367 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2368 SSL_R_MISSING_TMP_DH_KEY);
2371 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2372 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2373 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2374 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2375 SSL_R_DH_KEY_TOO_SMALL);
2378 if (s->s3->tmp.pkey != NULL) {
2379 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2380 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2381 ERR_R_INTERNAL_ERROR);
2385 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2386 if (s->s3->tmp.pkey == NULL) {
2387 /* SSLfatal() already called */
2391 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2393 EVP_PKEY_free(pkdh);
2396 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2397 DH_get0_key(dh, &r[2], NULL);
2400 #ifndef OPENSSL_NO_EC
2401 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2403 if (s->s3->tmp.pkey != NULL) {
2404 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2405 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2406 ERR_R_INTERNAL_ERROR);
2410 /* Get NID of appropriate shared curve */
2411 curve_id = tls1_shared_group(s, -2);
2412 if (curve_id == 0) {
2413 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2414 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2415 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2418 s->s3->tmp.pkey = ssl_generate_pkey_group(s, curve_id);
2419 /* Generate a new key for this curve */
2420 if (s->s3->tmp.pkey == NULL) {
2421 /* SSLfatal() already called */
2425 /* Encode the public key. */
2426 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2428 if (encodedlen == 0) {
2429 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2430 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2435 * We'll generate the serverKeyExchange message explicitly so we
2436 * can set these to NULLs
2443 #endif /* !OPENSSL_NO_EC */
2444 #ifndef OPENSSL_NO_SRP
2445 if (type & SSL_kSRP) {
2446 if ((s->srp_ctx.N == NULL) ||
2447 (s->srp_ctx.g == NULL) ||
2448 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2449 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2450 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2451 SSL_R_MISSING_SRP_PARAM);
2454 r[0] = s->srp_ctx.N;
2455 r[1] = s->srp_ctx.g;
2456 r[2] = s->srp_ctx.s;
2457 r[3] = s->srp_ctx.B;
2461 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2462 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2463 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2467 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2468 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2470 } else if (lu == NULL) {
2471 SSLfatal(s, SSL_AD_DECODE_ERROR,
2472 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2476 #ifndef OPENSSL_NO_PSK
2477 if (type & SSL_PSK) {
2478 size_t len = (s->cert->psk_identity_hint == NULL)
2479 ? 0 : strlen(s->cert->psk_identity_hint);
2482 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2483 * checked this when we set the identity hint - but just in case
2485 if (len > PSK_MAX_IDENTITY_LEN
2486 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2488 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2489 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2490 ERR_R_INTERNAL_ERROR);
2496 for (i = 0; i < 4 && r[i] != NULL; i++) {
2497 unsigned char *binval;
2500 #ifndef OPENSSL_NO_SRP
2501 if ((i == 2) && (type & SSL_kSRP)) {
2502 res = WPACKET_start_sub_packet_u8(pkt);
2505 res = WPACKET_start_sub_packet_u16(pkt);
2508 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2509 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2510 ERR_R_INTERNAL_ERROR);
2514 #ifndef OPENSSL_NO_DH
2516 * for interoperability with some versions of the Microsoft TLS
2517 * stack, we need to zero pad the DHE pub key to the same length
2520 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2521 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2524 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2525 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2526 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2527 ERR_R_INTERNAL_ERROR);
2530 memset(binval, 0, len);
2534 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2535 || !WPACKET_close(pkt)) {
2536 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2537 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2538 ERR_R_INTERNAL_ERROR);
2542 BN_bn2bin(r[i], binval);
2545 #ifndef OPENSSL_NO_EC
2546 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2548 * We only support named (not generic) curves. In this situation, the
2549 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2550 * [1 byte length of encoded point], followed by the actual encoded
2553 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2554 || !WPACKET_put_bytes_u8(pkt, 0)
2555 || !WPACKET_put_bytes_u8(pkt, curve_id)
2556 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2557 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2558 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2559 ERR_R_INTERNAL_ERROR);
2562 OPENSSL_free(encodedPoint);
2563 encodedPoint = NULL;
2569 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2571 unsigned char *sigbytes1, *sigbytes2, *tbs;
2572 size_t siglen, tbslen;
2575 if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
2576 /* Should never happen */
2577 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2578 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2579 ERR_R_INTERNAL_ERROR);
2583 * n is the length of the params, they start at &(d[4]) and p
2584 * points to the space at the end.
2587 /* Get length of the parameters we have written above */
2588 if (!WPACKET_get_length(pkt, ¶mlen)) {
2589 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2590 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2591 ERR_R_INTERNAL_ERROR);
2594 /* send signature algorithm */
2595 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
2596 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2597 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2598 ERR_R_INTERNAL_ERROR);
2602 * Create the signature. We don't know the actual length of the sig
2603 * until after we've created it, so we reserve enough bytes for it
2604 * up front, and then properly allocate them in the WPACKET
2607 siglen = EVP_PKEY_size(pkey);
2608 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2609 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2610 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2611 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2612 ERR_R_INTERNAL_ERROR);
2615 if (lu->sig == EVP_PKEY_RSA_PSS) {
2616 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2617 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2618 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2619 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2624 tbslen = construct_key_exchange_tbs(s, &tbs,
2625 s->init_buf->data + paramoffset,
2628 /* SSLfatal() already called */
2631 rv = EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen);
2633 if (rv <= 0 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2634 || sigbytes1 != sigbytes2) {
2635 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2636 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2637 ERR_R_INTERNAL_ERROR);
2642 EVP_MD_CTX_free(md_ctx);
2645 #ifndef OPENSSL_NO_DH
2646 EVP_PKEY_free(pkdh);
2648 #ifndef OPENSSL_NO_EC
2649 OPENSSL_free(encodedPoint);
2651 EVP_MD_CTX_free(md_ctx);
2655 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2657 if (SSL_IS_TLS13(s)) {
2658 /* TODO(TLS1.3) for now send empty request context */
2659 if (!WPACKET_put_bytes_u8(pkt, 0)) {
2660 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2661 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2662 ERR_R_INTERNAL_ERROR);
2666 if (!tls_construct_extensions(s, pkt,
2667 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2669 /* SSLfatal() already called */
2675 /* get the list of acceptable cert types */
2676 if (!WPACKET_start_sub_packet_u8(pkt)
2677 || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2678 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2679 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2683 if (SSL_USE_SIGALGS(s)) {
2684 const uint16_t *psigs;
2685 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2687 if (!WPACKET_start_sub_packet_u16(pkt)
2688 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
2689 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2690 || !WPACKET_close(pkt)) {
2691 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2692 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2693 ERR_R_INTERNAL_ERROR);
2698 if (!construct_ca_names(s, pkt)) {
2699 /* SSLfatal() already called */
2704 s->s3->tmp.cert_request = 1;
2708 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
2710 #ifndef OPENSSL_NO_PSK
2711 unsigned char psk[PSK_MAX_PSK_LEN];
2713 PACKET psk_identity;
2715 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2716 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2717 SSL_R_LENGTH_MISMATCH);
2720 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2721 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2722 SSL_R_DATA_LENGTH_TOO_LONG);
2725 if (s->psk_server_callback == NULL) {
2726 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2727 SSL_R_PSK_NO_SERVER_CB);
2731 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2732 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2733 ERR_R_INTERNAL_ERROR);
2737 psklen = s->psk_server_callback(s, s->session->psk_identity,
2740 if (psklen > PSK_MAX_PSK_LEN) {
2741 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2742 ERR_R_INTERNAL_ERROR);
2744 } else if (psklen == 0) {
2746 * PSK related to the given identity not found
2748 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
2749 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2750 SSL_R_PSK_IDENTITY_NOT_FOUND);
2754 OPENSSL_free(s->s3->tmp.psk);
2755 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2756 OPENSSL_cleanse(psk, psklen);
2758 if (s->s3->tmp.psk == NULL) {
2759 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2760 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2764 s->s3->tmp.psklen = psklen;
2768 /* Should never happen */
2769 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2770 ERR_R_INTERNAL_ERROR);
2775 static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
2777 #ifndef OPENSSL_NO_RSA
2778 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2780 unsigned char decrypt_good, version_good;
2781 size_t j, padding_len;
2782 PACKET enc_premaster;
2784 unsigned char *rsa_decrypt = NULL;
2787 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2789 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2790 SSL_R_MISSING_RSA_CERTIFICATE);
2794 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2795 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2796 enc_premaster = *pkt;
2798 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2799 || PACKET_remaining(pkt) != 0) {
2800 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2801 SSL_R_LENGTH_MISMATCH);
2807 * We want to be sure that the plaintext buffer size makes it safe to
2808 * iterate over the entire size of a premaster secret
2809 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2810 * their ciphertext cannot accommodate a premaster secret anyway.
2812 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2813 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2814 RSA_R_KEY_SIZE_TOO_SMALL);
2818 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2819 if (rsa_decrypt == NULL) {
2820 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2821 ERR_R_MALLOC_FAILURE);
2826 * We must not leak whether a decryption failure occurs because of
2827 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2828 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2829 * generates a random premaster secret for the case that the decrypt
2830 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2833 if (ssl_randbytes(s, rand_premaster_secret,
2834 sizeof(rand_premaster_secret)) <= 0) {
2835 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2836 ERR_R_INTERNAL_ERROR);
2841 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2842 * the timing-sensitive code below.
2844 /* TODO(size_t): Convert this function */
2845 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2846 PACKET_data(&enc_premaster),
2847 rsa_decrypt, rsa, RSA_NO_PADDING);
2848 if (decrypt_len < 0) {
2849 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2850 ERR_R_INTERNAL_ERROR);
2854 /* Check the padding. See RFC 3447, section 7.2.2. */
2857 * The smallest padded premaster is 11 bytes of overhead. Small keys
2858 * are publicly invalid, so this may return immediately. This ensures
2859 * PS is at least 8 bytes.
2861 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2862 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2863 SSL_R_DECRYPTION_FAILED);
2867 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2868 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2869 constant_time_eq_int_8(rsa_decrypt[1], 2);
2870 for (j = 2; j < padding_len - 1; j++) {
2871 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2873 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2876 * If the version in the decrypted pre-master secret is correct then
2877 * version_good will be 0xff, otherwise it'll be zero. The
2878 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2879 * (http://eprint.iacr.org/2003/052/) exploits the version number
2880 * check as a "bad version oracle". Thus version checks are done in
2881 * constant time and are treated like any other decryption error.
2884 constant_time_eq_8(rsa_decrypt[padding_len],
2885 (unsigned)(s->client_version >> 8));
2887 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2888 (unsigned)(s->client_version & 0xff));
2891 * The premaster secret must contain the same version number as the
2892 * ClientHello to detect version rollback attacks (strangely, the
2893 * protocol does not offer such protection for DH ciphersuites).
2894 * However, buggy clients exist that send the negotiated protocol
2895 * version instead if the server does not support the requested
2896 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2899 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2900 unsigned char workaround_good;
2901 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2902 (unsigned)(s->version >> 8));
2904 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2905 (unsigned)(s->version & 0xff));
2906 version_good |= workaround_good;
2910 * Both decryption and version must be good for decrypt_good to
2911 * remain non-zero (0xff).
2913 decrypt_good &= version_good;
2916 * Now copy rand_premaster_secret over from p using
2917 * decrypt_good_mask. If decryption failed, then p does not
2918 * contain valid plaintext, however, a check above guarantees
2919 * it is still sufficiently large to read from.
2921 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2922 rsa_decrypt[padding_len + j] =
2923 constant_time_select_8(decrypt_good,
2924 rsa_decrypt[padding_len + j],
2925 rand_premaster_secret[j]);
2928 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2929 sizeof(rand_premaster_secret), 0)) {
2930 /* SSLfatal() already called */
2936 OPENSSL_free(rsa_decrypt);
2939 /* Should never happen */
2940 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2941 ERR_R_INTERNAL_ERROR);
2946 static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
2948 #ifndef OPENSSL_NO_DH
2949 EVP_PKEY *skey = NULL;
2953 const unsigned char *data;
2954 EVP_PKEY *ckey = NULL;
2957 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2958 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2959 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2962 skey = s->s3->tmp.pkey;
2964 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2965 SSL_R_MISSING_TMP_DH_KEY);
2969 if (PACKET_remaining(pkt) == 0L) {
2970 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2971 SSL_R_MISSING_TMP_DH_KEY);
2974 if (!PACKET_get_bytes(pkt, &data, i)) {
2975 /* We already checked we have enough data */
2976 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2977 ERR_R_INTERNAL_ERROR);
2980 ckey = EVP_PKEY_new();
2981 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2982 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2986 cdh = EVP_PKEY_get0_DH(ckey);
2987 pub_key = BN_bin2bn(data, i, NULL);
2989 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
2990 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
2991 ERR_R_INTERNAL_ERROR);
2992 if (pub_key != NULL)
2997 if (ssl_derive(s, skey, ckey, 1) == 0) {
2998 /* SSLfatal() already called */
3003 EVP_PKEY_free(s->s3->tmp.pkey);
3004 s->s3->tmp.pkey = NULL;
3006 EVP_PKEY_free(ckey);
3009 /* Should never happen */
3010 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3011 ERR_R_INTERNAL_ERROR);
3016 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
3018 #ifndef OPENSSL_NO_EC
3019 EVP_PKEY *skey = s->s3->tmp.pkey;
3020 EVP_PKEY *ckey = NULL;
3023 if (PACKET_remaining(pkt) == 0L) {
3024 /* We don't support ECDH client auth */
3025 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_CKE_ECDHE,
3026 SSL_R_MISSING_TMP_ECDH_KEY);
3030 const unsigned char *data;
3033 * Get client's public key from encoded point in the
3034 * ClientKeyExchange message.
3037 /* Get encoded point length */
3038 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
3039 || PACKET_remaining(pkt) != 0) {
3040 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3041 SSL_R_LENGTH_MISMATCH);
3044 ckey = EVP_PKEY_new();
3045 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
3046 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3050 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
3051 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3057 if (ssl_derive(s, skey, ckey, 1) == 0) {
3058 /* SSLfatal() already called */
3063 EVP_PKEY_free(s->s3->tmp.pkey);
3064 s->s3->tmp.pkey = NULL;
3066 EVP_PKEY_free(ckey);
3070 /* Should never happen */
3071 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3072 ERR_R_INTERNAL_ERROR);
3077 static int tls_process_cke_srp(SSL *s, PACKET *pkt)
3079 #ifndef OPENSSL_NO_SRP
3081 const unsigned char *data;
3083 if (!PACKET_get_net_2(pkt, &i)
3084 || !PACKET_get_bytes(pkt, &data, i)) {
3085 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3086 SSL_R_BAD_SRP_A_LENGTH);
3089 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
3090 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3094 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
3095 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CKE_SRP,
3096 SSL_R_BAD_SRP_PARAMETERS);
3099 OPENSSL_free(s->session->srp_username);
3100 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3101 if (s->session->srp_username == NULL) {
3102 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3103 ERR_R_MALLOC_FAILURE);
3107 if (!srp_generate_server_master_secret(s)) {
3108 /* SSLfatal() already called */
3114 /* Should never happen */
3115 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3116 ERR_R_INTERNAL_ERROR);
3121 static int tls_process_cke_gost(SSL *s, PACKET *pkt)
3123 #ifndef OPENSSL_NO_GOST
3124 EVP_PKEY_CTX *pkey_ctx;
3125 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
3126 unsigned char premaster_secret[32];
3127 const unsigned char *start;
3128 size_t outlen = 32, inlen;
3129 unsigned long alg_a;
3132 size_t sess_key_len;
3133 const unsigned char *data;
3136 /* Get our certificate private key */
3137 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3138 if (alg_a & SSL_aGOST12) {
3140 * New GOST ciphersuites have SSL_aGOST01 bit too
3142 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
3144 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3147 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3149 } else if (alg_a & SSL_aGOST01) {
3150 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3153 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
3154 if (pkey_ctx == NULL) {
3155 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3156 ERR_R_MALLOC_FAILURE);
3159 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3160 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3161 ERR_R_INTERNAL_ERROR);
3165 * If client certificate is present and is of the same type, maybe
3166 * use it for key exchange. Don't mind errors from
3167 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3168 * client certificate for authorization only.
3170 client_pub_pkey = X509_get0_pubkey(s->session->peer);
3171 if (client_pub_pkey) {
3172 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3175 /* Decrypt session key */
3176 sess_key_len = PACKET_remaining(pkt);
3177 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
3178 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3179 ERR_R_INTERNAL_ERROR);
3182 /* TODO(size_t): Convert this function */
3183 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
3184 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
3185 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
3186 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3187 SSL_R_DECRYPTION_FAILED);
3192 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
3194 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3195 SSL_R_DECRYPTION_FAILED);
3198 /* Generate master secret */
3199 if (!ssl_generate_master_secret(s, premaster_secret,
3200 sizeof(premaster_secret), 0)) {
3201 /* SSLfatal() already called */
3204 /* Check if pubkey from client certificate was used */
3205 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
3207 s->statem.no_cert_verify = 1;
3211 EVP_PKEY_CTX_free(pkey_ctx);
3214 /* Should never happen */
3215 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3216 ERR_R_INTERNAL_ERROR);
3221 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3223 unsigned long alg_k;
3225 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3227 /* For PSK parse and retrieve identity, obtain PSK key */
3228 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
3229 /* SSLfatal() already called */
3233 if (alg_k & SSL_kPSK) {
3234 /* Identity extracted earlier: should be nothing left */
3235 if (PACKET_remaining(pkt) != 0) {
3236 SSLfatal(s, SSL_AD_DECODE_ERROR,
3237 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3238 SSL_R_LENGTH_MISMATCH);
3241 /* PSK handled by ssl_generate_master_secret */
3242 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3243 /* SSLfatal() already called */
3246 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3247 if (!tls_process_cke_rsa(s, pkt)) {
3248 /* SSLfatal() already called */
3251 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3252 if (!tls_process_cke_dhe(s, pkt)) {
3253 /* SSLfatal() already called */
3256 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3257 if (!tls_process_cke_ecdhe(s, pkt)) {
3258 /* SSLfatal() already called */
3261 } else if (alg_k & SSL_kSRP) {
3262 if (!tls_process_cke_srp(s, pkt)) {
3263 /* SSLfatal() already called */
3266 } else if (alg_k & SSL_kGOST) {
3267 if (!tls_process_cke_gost(s, pkt)) {
3268 /* SSLfatal() already called */
3272 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3273 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3274 SSL_R_UNKNOWN_CIPHER_TYPE);
3278 return MSG_PROCESS_CONTINUE_PROCESSING;
3280 #ifndef OPENSSL_NO_PSK
3281 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
3282 s->s3->tmp.psk = NULL;
3284 return MSG_PROCESS_ERROR;
3287 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
3289 #ifndef OPENSSL_NO_SCTP
3290 if (wst == WORK_MORE_A) {
3291 if (SSL_IS_DTLS(s)) {
3292 unsigned char sctpauthkey[64];
3293 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3295 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3298 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3299 sizeof(DTLS1_SCTP_AUTH_LABEL));
3301 if (SSL_export_keying_material(s, sctpauthkey,
3302 sizeof(sctpauthkey), labelbuffer,
3303 sizeof(labelbuffer), NULL, 0,
3305 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3306 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3307 ERR_R_INTERNAL_ERROR);
3311 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3312 sizeof(sctpauthkey), sctpauthkey);
3317 if (s->statem.no_cert_verify || !s->session->peer) {
3319 * No certificate verify or no peer certificate so we no longer need
3320 * the handshake_buffer
3322 if (!ssl3_digest_cached_records(s, 0)) {
3323 /* SSLfatal() already called */
3326 return WORK_FINISHED_CONTINUE;
3328 if (!s->s3->handshake_buffer) {
3329 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3330 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3331 ERR_R_INTERNAL_ERROR);
3335 * For sigalgs freeze the handshake buffer. If we support
3336 * extms we've done this already so this is a no-op
3338 if (!ssl3_digest_cached_records(s, 1)) {
3339 /* SSLfatal() already called */
3344 return WORK_FINISHED_CONTINUE;
3347 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3350 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
3352 unsigned long l, llen;
3353 const unsigned char *certstart, *certbytes;
3354 STACK_OF(X509) *sk = NULL;
3355 PACKET spkt, context;
3358 if ((sk = sk_X509_new_null()) == NULL) {
3359 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3360 ERR_R_MALLOC_FAILURE);
3364 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3365 if ((SSL_IS_TLS13(s) && !PACKET_get_length_prefixed_1(pkt, &context))
3366 || !PACKET_get_net_3(pkt, &llen)
3367 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3368 || PACKET_remaining(pkt) != 0) {
3369 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3370 SSL_R_LENGTH_MISMATCH);
3374 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3375 if (!PACKET_get_net_3(&spkt, &l)
3376 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3377 SSLfatal(s, SSL_AD_DECODE_ERROR,
3378 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3379 SSL_R_CERT_LENGTH_MISMATCH);
3383 certstart = certbytes;
3384 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3386 SSLfatal(s, SSL_AD_DECODE_ERROR,
3387 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3390 if (certbytes != (certstart + l)) {
3391 SSLfatal(s, SSL_AD_DECODE_ERROR,
3392 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3393 SSL_R_CERT_LENGTH_MISMATCH);
3397 if (SSL_IS_TLS13(s)) {
3398 RAW_EXTENSION *rawexts = NULL;
3401 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3402 SSLfatal(s, SSL_AD_DECODE_ERROR,
3403 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3407 if (!tls_collect_extensions(s, &extensions,
3408 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
3409 NULL, chainidx == 0)
3410 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3411 rawexts, x, chainidx,
3412 PACKET_remaining(&spkt) == 0)) {
3413 OPENSSL_free(rawexts);
3416 OPENSSL_free(rawexts);
3419 if (!sk_X509_push(sk, x)) {
3420 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3421 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3422 ERR_R_MALLOC_FAILURE);
3428 if (sk_X509_num(sk) <= 0) {
3429 /* TLS does not mind 0 certs returned */
3430 if (s->version == SSL3_VERSION) {
3431 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3432 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3433 SSL_R_NO_CERTIFICATES_RETURNED);
3436 /* Fail for TLS only if we required a certificate */
3437 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3438 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3439 SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
3440 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3441 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3444 /* No client certificate so digest cached records */
3445 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3446 /* SSLfatal() already called */
3451 i = ssl_verify_cert_chain(s, sk);
3453 SSLfatal(s, ssl_verify_alarm_type(s->verify_result),
3454 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3455 SSL_R_CERTIFICATE_VERIFY_FAILED);
3459 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3460 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3463 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3465 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3466 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3467 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3472 X509_free(s->session->peer);
3473 s->session->peer = sk_X509_shift(sk);
3474 s->session->verify_result = s->verify_result;
3476 sk_X509_pop_free(s->session->peer_chain, X509_free);
3477 s->session->peer_chain = sk;
3480 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3483 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3484 /* SSLfatal() already called */
3489 * Inconsistency alert: cert_chain does *not* include the peer's own
3490 * certificate, while we do include it in statem_clnt.c
3494 /* Save the current hash state for when we receive the CertificateVerify */
3496 && !ssl_handshake_hash(s, s->cert_verify_hash,
3497 sizeof(s->cert_verify_hash),
3498 &s->cert_verify_hash_len)) {
3499 /* SSLfatal() already called */
3503 ret = MSG_PROCESS_CONTINUE_READING;
3507 sk_X509_pop_free(sk, X509_free);
3511 int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3513 CERT_PKEY *cpk = s->s3->tmp.cert;
3516 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3517 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3522 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3523 * for the server Certificate message
3525 if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
3526 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3527 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3530 if (!ssl3_output_cert_chain(s, pkt, cpk)) {
3531 /* SSLfatal() already called */
3538 int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3540 unsigned char *senc = NULL;
3541 EVP_CIPHER_CTX *ctx = NULL;
3542 HMAC_CTX *hctx = NULL;
3543 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3544 const unsigned char *const_p;
3545 int len, slen_full, slen, lenfinal;
3548 SSL_CTX *tctx = s->session_ctx;
3549 unsigned char iv[EVP_MAX_IV_LENGTH];
3550 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3552 size_t macoffset, macendoffset;
3554 unsigned char age_add_c[sizeof(uint32_t)];
3558 if (SSL_IS_TLS13(s)) {
3559 if (ssl_randbytes(s, age_add_u.age_add_c, sizeof(age_add_u)) <= 0) {
3560 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3561 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3562 ERR_R_INTERNAL_ERROR);
3565 s->session->ext.tick_age_add = age_add_u.age_add;
3567 * ticket_nonce is set to a single 0 byte because we only ever send a
3568 * single ticket per connection. IMPORTANT: If we ever support multiple
3569 * tickets per connection then this will need to be changed.
3571 OPENSSL_free(s->session->ext.tick_nonce);
3572 s->session->ext.tick_nonce = OPENSSL_zalloc(sizeof(char));
3573 if (s->session->ext.tick_nonce == NULL) {
3574 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3575 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3576 ERR_R_MALLOC_FAILURE);
3579 s->session->ext.tick_nonce_len = 1;
3580 s->session->time = (long)time(NULL);
3581 if (s->s3->alpn_selected != NULL) {
3582 OPENSSL_free(s->session->ext.alpn_selected);
3583 s->session->ext.alpn_selected =
3584 OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
3585 if (s->session->ext.alpn_selected == NULL) {
3586 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3587 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3588 ERR_R_MALLOC_FAILURE);
3591 s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
3593 s->session->ext.max_early_data = s->max_early_data;
3596 /* get session encoding length */
3597 slen_full = i2d_SSL_SESSION(s->session, NULL);
3599 * Some length values are 16 bits, so forget it if session is too
3602 if (slen_full == 0 || slen_full > 0xFF00) {
3603 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3604 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3607 senc = OPENSSL_malloc(slen_full);
3609 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3610 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3614 ctx = EVP_CIPHER_CTX_new();
3615 hctx = HMAC_CTX_new();
3616 if (ctx == NULL || hctx == NULL) {
3617 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3618 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3623 if (!i2d_SSL_SESSION(s->session, &p)) {
3624 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3625 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3630 * create a fresh copy (not shared with other threads) to clean up
3633 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3635 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3636 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3639 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3641 slen = i2d_SSL_SESSION(sess, NULL);
3642 if (slen == 0 || slen > slen_full) {
3643 /* shouldn't ever happen */
3644 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3645 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3646 SSL_SESSION_free(sess);
3650 if (!i2d_SSL_SESSION(sess, &p)) {
3651 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3652 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3653 SSL_SESSION_free(sess);
3656 SSL_SESSION_free(sess);
3659 * Initialize HMAC and cipher contexts. If callback present it does
3660 * all the work otherwise use generated values from parent ctx.
3662 if (tctx->ext.ticket_key_cb) {
3663 /* if 0 is returned, write an empty ticket */
3664 int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3669 /* Put timeout and length */
3670 if (!WPACKET_put_bytes_u32(pkt, 0)
3671 || !WPACKET_put_bytes_u16(pkt, 0)) {
3672 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3673 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3674 ERR_R_INTERNAL_ERROR);
3678 EVP_CIPHER_CTX_free(ctx);
3679 HMAC_CTX_free(hctx);
3683 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3684 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3685 SSL_R_CALLBACK_FAILED);
3688 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3690 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3692 iv_len = EVP_CIPHER_iv_length(cipher);
3693 if (ssl_randbytes(s, iv, iv_len) <= 0
3694 || !EVP_EncryptInit_ex(ctx, cipher, NULL,
3695 tctx->ext.tick_aes_key, iv)
3696 || !HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
3697 sizeof(tctx->ext.tick_hmac_key),
3698 EVP_sha256(), NULL)) {
3699 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3700 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3701 ERR_R_INTERNAL_ERROR);
3704 memcpy(key_name, tctx->ext.tick_key_name,
3705 sizeof(tctx->ext.tick_key_name));
3709 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3710 * unspecified for resumed session (for simplicity).
3711 * In TLSv1.3 we reset the "time" field above, and always specify the
3714 if (!WPACKET_put_bytes_u32(pkt,
3715 (s->hit && !SSL_IS_TLS13(s))
3716 ? 0 : s->session->timeout)
3718 && (!WPACKET_put_bytes_u32(pkt, age_add_u.age_add)
3719 || !WPACKET_sub_memcpy_u8(pkt, s->session->ext.tick_nonce,
3720 s->session->ext.tick_nonce_len)))
3721 /* Now the actual ticket data */
3722 || !WPACKET_start_sub_packet_u16(pkt)
3723 || !WPACKET_get_total_written(pkt, &macoffset)
3724 /* Output key name */
3725 || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3727 || !WPACKET_memcpy(pkt, iv, iv_len)
3728 || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3730 /* Encrypt session data */
3731 || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3732 || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3733 || encdata1 != encdata2
3734 || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3735 || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3736 || encdata1 + len != encdata2
3737 || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3738 || !WPACKET_get_total_written(pkt, &macendoffset)
3739 || !HMAC_Update(hctx,
3740 (unsigned char *)s->init_buf->data + macoffset,
3741 macendoffset - macoffset)
3742 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3743 || !HMAC_Final(hctx, macdata1, &hlen)
3744 || hlen > EVP_MAX_MD_SIZE
3745 || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3746 || macdata1 != macdata2
3747 || !WPACKET_close(pkt)) {
3748 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3749 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3753 && !tls_construct_extensions(s, pkt,
3754 SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
3756 /* SSLfatal() already called */
3759 EVP_CIPHER_CTX_free(ctx);
3760 HMAC_CTX_free(hctx);
3766 EVP_CIPHER_CTX_free(ctx);
3767 HMAC_CTX_free(hctx);
3772 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
3773 * create a separate message. Returns 1 on success or 0 on failure.
3775 int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
3777 if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
3778 || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
3779 s->ext.ocsp.resp_len)) {
3780 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY,
3781 ERR_R_INTERNAL_ERROR);
3788 int tls_construct_cert_status(SSL *s, WPACKET *pkt)
3790 if (!tls_construct_cert_status_body(s, pkt)) {
3791 /* SSLfatal() already called */
3798 #ifndef OPENSSL_NO_NEXTPROTONEG
3800 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3801 * It sets the next_proto member in s if found
3803 MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
3805 PACKET next_proto, padding;
3806 size_t next_proto_len;
3809 * The payload looks like:
3811 * uint8 proto[proto_len];
3812 * uint8 padding_len;
3813 * uint8 padding[padding_len];
3815 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3816 || !PACKET_get_length_prefixed_1(pkt, &padding)
3817 || PACKET_remaining(pkt) > 0) {
3818 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
3819 SSL_R_LENGTH_MISMATCH);
3820 return MSG_PROCESS_ERROR;
3823 if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
3825 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
3826 ERR_R_INTERNAL_ERROR);
3827 return MSG_PROCESS_ERROR;
3830 s->ext.npn_len = (unsigned char)next_proto_len;
3832 return MSG_PROCESS_CONTINUE_READING;
3836 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
3838 if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
3840 /* SSLfatal() already called */
3847 static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt)
3852 * TODO(TLS1.3): Remove the DRAFT version before release
3853 * (should be s->version)
3855 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)
3856 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt,
3858 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3859 SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST, ERR_R_INTERNAL_ERROR);
3863 if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
3865 /* SSLfatal() already called */
3869 /* Ditch the session. We'll create a new one next time around */
3870 SSL_SESSION_free(s->session);
3875 * Re-initialise the Transcript Hash. We're going to prepopulate it with
3876 * a synthetic message_hash in place of ClientHello1.
3878 if (!create_synthetic_message_hash(s)) {
3879 /* SSLfatal() already called */
3886 MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
3888 if (PACKET_remaining(pkt) != 0) {
3889 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
3890 SSL_R_LENGTH_MISMATCH);
3891 return MSG_PROCESS_ERROR;
3894 if (s->early_data_state != SSL_EARLY_DATA_READING
3895 && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
3896 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
3897 ERR_R_INTERNAL_ERROR);
3898 return MSG_PROCESS_ERROR;
3902 * EndOfEarlyData signals a key change so the end of the message must be on
3903 * a record boundary.
3905 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
3906 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
3907 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
3908 SSL_R_NOT_ON_RECORD_BOUNDARY);
3909 return MSG_PROCESS_ERROR;
3912 s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
3913 if (!s->method->ssl3_enc->change_cipher_state(s,
3914 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
3915 /* SSLfatal() already called */
3916 return MSG_PROCESS_ERROR;
3919 return MSG_PROCESS_CONTINUE_READING;