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_SW_HELLO_RETRY_REQUEST:
52 if (mt == SSL3_MT_CLIENT_HELLO) {
53 st->hand_state = TLS_ST_SR_CLNT_HELLO;
58 case TLS_ST_EARLY_DATA:
59 if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
60 if (mt == SSL3_MT_END_OF_EARLY_DATA) {
61 st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
68 case TLS_ST_SR_END_OF_EARLY_DATA:
69 case TLS_ST_SW_FINISHED:
70 if (s->s3->tmp.cert_request) {
71 if (mt == SSL3_MT_CERTIFICATE) {
72 st->hand_state = TLS_ST_SR_CERT;
76 if (mt == SSL3_MT_FINISHED) {
77 st->hand_state = TLS_ST_SR_FINISHED;
84 if (s->session->peer == NULL) {
85 if (mt == SSL3_MT_FINISHED) {
86 st->hand_state = TLS_ST_SR_FINISHED;
90 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
91 st->hand_state = TLS_ST_SR_CERT_VRFY;
97 case TLS_ST_SR_CERT_VRFY:
98 if (mt == SSL3_MT_FINISHED) {
99 st->hand_state = TLS_ST_SR_FINISHED;
106 * Its never ok to start processing handshake messages in the middle of
107 * early data (i.e. before we've received the end of early data alert)
109 if (s->early_data_state == SSL_EARLY_DATA_READING)
111 if (mt == SSL3_MT_KEY_UPDATE) {
112 st->hand_state = TLS_ST_SR_KEY_UPDATE;
118 /* No valid transition found */
123 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
124 * handshake state transitions when the server is reading messages from the
125 * client. The message type that the client has sent is provided in |mt|. The
126 * current state is in |s->statem.hand_state|.
128 * Return values are 1 for success (transition allowed) and 0 on error
129 * (transition not allowed)
131 int ossl_statem_server_read_transition(SSL *s, int mt)
133 OSSL_STATEM *st = &s->statem;
135 if (SSL_IS_TLS13(s)) {
136 if (!ossl_statem_server13_read_transition(s, mt))
141 switch (st->hand_state) {
147 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
148 if (mt == SSL3_MT_CLIENT_HELLO) {
149 st->hand_state = TLS_ST_SR_CLNT_HELLO;
154 case TLS_ST_SW_SRVR_DONE:
156 * If we get a CKE message after a ServerDone then either
157 * 1) We didn't request a Certificate
159 * 2) If we did request one then
160 * a) We allow no Certificate to be returned
162 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
163 * list if we requested a certificate)
165 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
166 if (s->s3->tmp.cert_request) {
167 if (s->version == SSL3_VERSION) {
168 if ((s->verify_mode & SSL_VERIFY_PEER)
169 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
171 * This isn't an unexpected message as such - we're just
172 * not going to accept it because we require a client
175 ssl3_send_alert(s, SSL3_AL_FATAL,
176 SSL3_AD_HANDSHAKE_FAILURE);
177 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
178 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
181 st->hand_state = TLS_ST_SR_KEY_EXCH;
185 st->hand_state = TLS_ST_SR_KEY_EXCH;
188 } else if (s->s3->tmp.cert_request) {
189 if (mt == SSL3_MT_CERTIFICATE) {
190 st->hand_state = TLS_ST_SR_CERT;
197 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
198 st->hand_state = TLS_ST_SR_KEY_EXCH;
203 case TLS_ST_SR_KEY_EXCH:
205 * We should only process a CertificateVerify message if we have
206 * received a Certificate from the client. If so then |s->session->peer|
207 * will be non NULL. In some instances a CertificateVerify message is
208 * not required even if the peer has sent a Certificate (e.g. such as in
209 * the case of static DH). In that case |st->no_cert_verify| should be
212 if (s->session->peer == NULL || st->no_cert_verify) {
213 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
215 * For the ECDH ciphersuites when the client sends its ECDH
216 * pub key in a certificate, the CertificateVerify message is
217 * not sent. Also for GOST ciphersuites when the client uses
218 * its key from the certificate for key exchange.
220 st->hand_state = TLS_ST_SR_CHANGE;
224 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
225 st->hand_state = TLS_ST_SR_CERT_VRFY;
231 case TLS_ST_SR_CERT_VRFY:
232 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
233 st->hand_state = TLS_ST_SR_CHANGE;
238 case TLS_ST_SR_CHANGE:
239 #ifndef OPENSSL_NO_NEXTPROTONEG
240 if (s->s3->npn_seen) {
241 if (mt == SSL3_MT_NEXT_PROTO) {
242 st->hand_state = TLS_ST_SR_NEXT_PROTO;
247 if (mt == SSL3_MT_FINISHED) {
248 st->hand_state = TLS_ST_SR_FINISHED;
251 #ifndef OPENSSL_NO_NEXTPROTONEG
256 #ifndef OPENSSL_NO_NEXTPROTONEG
257 case TLS_ST_SR_NEXT_PROTO:
258 if (mt == SSL3_MT_FINISHED) {
259 st->hand_state = TLS_ST_SR_FINISHED;
265 case TLS_ST_SW_FINISHED:
266 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
267 st->hand_state = TLS_ST_SR_CHANGE;
274 /* No valid transition found */
275 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
276 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
281 * Should we send a ServerKeyExchange message?
283 * Valid return values are:
287 static int send_server_key_exchange(SSL *s)
289 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
292 * only send a ServerKeyExchange if DH or fortezza but we have a
293 * sign only certificate PSK: may send PSK identity hints For
294 * ECC ciphersuites, we send a serverKeyExchange message only if
295 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
296 * the server certificate contains the server's public key for
299 if (alg_k & (SSL_kDHE | SSL_kECDHE)
301 * PSK: send ServerKeyExchange if PSK identity hint if
304 #ifndef OPENSSL_NO_PSK
305 /* Only send SKE if we have identity hint for plain PSK */
306 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
307 && s->cert->psk_identity_hint)
308 /* For other PSK always send SKE */
309 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
311 #ifndef OPENSSL_NO_SRP
312 /* SRP: send ServerKeyExchange */
313 || (alg_k & SSL_kSRP)
323 * Should we send a CertificateRequest message?
325 * Valid return values are:
329 static int send_certificate_request(SSL *s)
332 /* don't request cert unless asked for it: */
333 s->verify_mode & SSL_VERIFY_PEER
335 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
336 * during re-negotiation:
338 && (s->s3->tmp.finish_md_len == 0 ||
339 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
341 * never request cert in anonymous ciphersuites (see
342 * section "Certificate request" in SSL 3 drafts and in
345 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
347 * ... except when the application insists on
348 * verification (against the specs, but statem_clnt.c accepts
351 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
352 /* don't request certificate for SRP auth */
353 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
355 * With normal PSK Certificates and Certificate Requests
358 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
366 * ossl_statem_server13_write_transition() works out what handshake state to
367 * move to next when a TLSv1.3 server is writing messages to be sent to the
370 static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
372 OSSL_STATEM *st = &s->statem;
375 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
376 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
379 switch (st->hand_state) {
381 /* Shouldn't happen */
382 return WRITE_TRAN_ERROR;
385 if (s->key_update != SSL_KEY_UPDATE_NONE) {
386 st->hand_state = TLS_ST_SW_KEY_UPDATE;
387 return WRITE_TRAN_CONTINUE;
389 /* Try to read from the client instead */
390 return WRITE_TRAN_FINISHED;
392 case TLS_ST_SR_CLNT_HELLO:
393 if (s->hello_retry_request)
394 st->hand_state = TLS_ST_SW_HELLO_RETRY_REQUEST;
396 st->hand_state = TLS_ST_SW_SRVR_HELLO;
397 return WRITE_TRAN_CONTINUE;
399 case TLS_ST_SW_HELLO_RETRY_REQUEST:
400 return WRITE_TRAN_FINISHED;
402 case TLS_ST_SW_SRVR_HELLO:
403 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
404 return WRITE_TRAN_CONTINUE;
406 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
408 st->hand_state = TLS_ST_SW_FINISHED;
409 else if (send_certificate_request(s))
410 st->hand_state = TLS_ST_SW_CERT_REQ;
412 st->hand_state = TLS_ST_SW_CERT;
414 return WRITE_TRAN_CONTINUE;
416 case TLS_ST_SW_CERT_REQ:
417 st->hand_state = TLS_ST_SW_CERT;
418 return WRITE_TRAN_CONTINUE;
421 st->hand_state = TLS_ST_SW_CERT_VRFY;
422 return WRITE_TRAN_CONTINUE;
424 case TLS_ST_SW_CERT_VRFY:
425 st->hand_state = TLS_ST_SW_FINISHED;
426 return WRITE_TRAN_CONTINUE;
428 case TLS_ST_SW_FINISHED:
429 st->hand_state = TLS_ST_EARLY_DATA;
430 return WRITE_TRAN_CONTINUE;
432 case TLS_ST_EARLY_DATA:
433 return WRITE_TRAN_FINISHED;
435 case TLS_ST_SR_FINISHED:
437 * Technically we have finished the handshake at this point, but we're
438 * going to remain "in_init" for now and write out the session ticket
440 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
441 * and give the application the opportunity to delay sending the
444 st->hand_state = TLS_ST_SW_SESSION_TICKET;
445 return WRITE_TRAN_CONTINUE;
447 case TLS_ST_SR_KEY_UPDATE:
448 if (s->key_update != SSL_KEY_UPDATE_NONE) {
449 st->hand_state = TLS_ST_SW_KEY_UPDATE;
450 return WRITE_TRAN_CONTINUE;
454 case TLS_ST_SW_KEY_UPDATE:
455 case TLS_ST_SW_SESSION_TICKET:
456 st->hand_state = TLS_ST_OK;
457 return WRITE_TRAN_CONTINUE;
462 * ossl_statem_server_write_transition() works out what handshake state to move
463 * to next when the server is writing messages to be sent to the client.
465 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
467 OSSL_STATEM *st = &s->statem;
470 * Note that before the ClientHello we don't know what version we are going
471 * to negotiate yet, so we don't take this branch until later
475 return ossl_statem_server13_write_transition(s);
477 switch (st->hand_state) {
479 /* Shouldn't happen */
480 return WRITE_TRAN_ERROR;
483 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
484 /* We must be trying to renegotiate */
485 st->hand_state = TLS_ST_SW_HELLO_REQ;
486 st->request_state = TLS_ST_BEFORE;
487 return WRITE_TRAN_CONTINUE;
489 /* Must be an incoming ClientHello */
490 if (!tls_setup_handshake(s)) {
491 ossl_statem_set_error(s);
492 return WRITE_TRAN_ERROR;
497 /* Just go straight to trying to read from the client */
498 return WRITE_TRAN_FINISHED;
500 case TLS_ST_SW_HELLO_REQ:
501 st->hand_state = TLS_ST_OK;
502 return WRITE_TRAN_CONTINUE;
504 case TLS_ST_SR_CLNT_HELLO:
505 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
506 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
507 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
509 st->hand_state = TLS_ST_SW_SRVR_HELLO;
510 return WRITE_TRAN_CONTINUE;
512 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
513 return WRITE_TRAN_FINISHED;
515 case TLS_ST_SW_SRVR_HELLO:
517 if (s->ext.ticket_expected)
518 st->hand_state = TLS_ST_SW_SESSION_TICKET;
520 st->hand_state = TLS_ST_SW_CHANGE;
522 /* Check if it is anon DH or anon ECDH, */
523 /* normal PSK or SRP */
524 if (!(s->s3->tmp.new_cipher->algorithm_auth &
525 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
526 st->hand_state = TLS_ST_SW_CERT;
527 } else if (send_server_key_exchange(s)) {
528 st->hand_state = TLS_ST_SW_KEY_EXCH;
529 } else if (send_certificate_request(s)) {
530 st->hand_state = TLS_ST_SW_CERT_REQ;
532 st->hand_state = TLS_ST_SW_SRVR_DONE;
535 return WRITE_TRAN_CONTINUE;
538 if (s->ext.status_expected) {
539 st->hand_state = TLS_ST_SW_CERT_STATUS;
540 return WRITE_TRAN_CONTINUE;
544 case TLS_ST_SW_CERT_STATUS:
545 if (send_server_key_exchange(s)) {
546 st->hand_state = TLS_ST_SW_KEY_EXCH;
547 return WRITE_TRAN_CONTINUE;
551 case TLS_ST_SW_KEY_EXCH:
552 if (send_certificate_request(s)) {
553 st->hand_state = TLS_ST_SW_CERT_REQ;
554 return WRITE_TRAN_CONTINUE;
558 case TLS_ST_SW_CERT_REQ:
559 st->hand_state = TLS_ST_SW_SRVR_DONE;
560 return WRITE_TRAN_CONTINUE;
562 case TLS_ST_SW_SRVR_DONE:
563 return WRITE_TRAN_FINISHED;
565 case TLS_ST_SR_FINISHED:
567 st->hand_state = TLS_ST_OK;
568 return WRITE_TRAN_CONTINUE;
569 } else if (s->ext.ticket_expected) {
570 st->hand_state = TLS_ST_SW_SESSION_TICKET;
572 st->hand_state = TLS_ST_SW_CHANGE;
574 return WRITE_TRAN_CONTINUE;
576 case TLS_ST_SW_SESSION_TICKET:
577 st->hand_state = TLS_ST_SW_CHANGE;
578 return WRITE_TRAN_CONTINUE;
580 case TLS_ST_SW_CHANGE:
581 st->hand_state = TLS_ST_SW_FINISHED;
582 return WRITE_TRAN_CONTINUE;
584 case TLS_ST_SW_FINISHED:
586 return WRITE_TRAN_FINISHED;
588 st->hand_state = TLS_ST_OK;
589 return WRITE_TRAN_CONTINUE;
594 * Perform any pre work that needs to be done prior to sending a message from
595 * the server to the client.
597 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
599 OSSL_STATEM *st = &s->statem;
601 switch (st->hand_state) {
603 /* No pre work to be done */
606 case TLS_ST_SW_HELLO_REQ:
609 dtls1_clear_sent_buffer(s);
612 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
614 if (SSL_IS_DTLS(s)) {
615 dtls1_clear_sent_buffer(s);
616 /* We don't buffer this message so don't use the timer */
621 case TLS_ST_SW_SRVR_HELLO:
622 if (SSL_IS_DTLS(s)) {
624 * Messages we write from now on should be buffered and
625 * retransmitted if necessary, so we need to use the timer now
631 case TLS_ST_SW_SRVR_DONE:
632 #ifndef OPENSSL_NO_SCTP
633 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
634 return dtls_wait_for_dry(s);
636 return WORK_FINISHED_CONTINUE;
638 case TLS_ST_SW_SESSION_TICKET:
639 if (SSL_IS_TLS13(s)) {
641 * Actually this is the end of the handshake, but we're going
642 * straight into writing the session ticket out. So we finish off
643 * the handshake, but keep the various buffers active.
645 return tls_finish_handshake(s, wst, 0);
646 } if (SSL_IS_DTLS(s)) {
648 * We're into the last flight. We don't retransmit the last flight
649 * unless we need to, so we don't use the timer
655 case TLS_ST_SW_CHANGE:
656 s->session->cipher = s->s3->tmp.new_cipher;
657 if (!s->method->ssl3_enc->setup_key_block(s)) {
658 ossl_statem_set_error(s);
661 if (SSL_IS_DTLS(s)) {
663 * We're into the last flight. We don't retransmit the last flight
664 * unless we need to, so we don't use the timer. This might have
665 * already been set to 0 if we sent a NewSessionTicket message,
666 * but we'll set it again here in case we didn't.
670 return WORK_FINISHED_CONTINUE;
672 case TLS_ST_EARLY_DATA:
673 if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING)
674 return WORK_FINISHED_CONTINUE;
678 return tls_finish_handshake(s, wst, 1);
681 return WORK_FINISHED_CONTINUE;
685 * Perform any work that needs to be done after sending a message from the
686 * server to the client.
688 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
690 OSSL_STATEM *st = &s->statem;
694 switch (st->hand_state) {
696 /* No post work to be done */
699 case TLS_ST_SW_HELLO_RETRY_REQUEST:
700 if (statem_flush(s) != 1)
704 case TLS_ST_SW_HELLO_REQ:
705 if (statem_flush(s) != 1)
707 if (!ssl3_init_finished_mac(s)) {
708 ossl_statem_set_error(s);
713 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
714 if (statem_flush(s) != 1)
716 /* HelloVerifyRequest resets Finished MAC */
717 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
718 ossl_statem_set_error(s);
722 * The next message should be another ClientHello which we need to
723 * treat like it was the first packet
728 case TLS_ST_SW_SRVR_HELLO:
729 #ifndef OPENSSL_NO_SCTP
730 if (SSL_IS_DTLS(s) && s->hit) {
731 unsigned char sctpauthkey[64];
732 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
735 * Add new shared key for SCTP-Auth, will be ignored if no
738 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
739 sizeof(DTLS1_SCTP_AUTH_LABEL));
741 if (SSL_export_keying_material(s, sctpauthkey,
742 sizeof(sctpauthkey), labelbuffer,
743 sizeof(labelbuffer), NULL, 0,
745 ossl_statem_set_error(s);
749 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
750 sizeof(sctpauthkey), sctpauthkey);
754 * TODO(TLS1.3): This actually causes a problem. We don't yet know
755 * whether the next record we are going to receive is an unencrypted
756 * alert, or an encrypted handshake message. We're going to need
757 * something clever in the record layer for this.
759 if (SSL_IS_TLS13(s)) {
760 if (!s->method->ssl3_enc->setup_key_block(s)
761 || !s->method->ssl3_enc->change_cipher_state(s,
762 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE))
765 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
766 && !s->method->ssl3_enc->change_cipher_state(s,
767 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ))
772 case TLS_ST_SW_CHANGE:
773 #ifndef OPENSSL_NO_SCTP
774 if (SSL_IS_DTLS(s) && !s->hit) {
776 * Change to new shared key of SCTP-Auth, will be ignored if
779 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
783 if (!s->method->ssl3_enc->change_cipher_state(s,
784 SSL3_CHANGE_CIPHER_SERVER_WRITE))
786 ossl_statem_set_error(s);
791 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
794 case TLS_ST_SW_SRVR_DONE:
795 if (statem_flush(s) != 1)
799 case TLS_ST_SW_FINISHED:
800 if (statem_flush(s) != 1)
802 #ifndef OPENSSL_NO_SCTP
803 if (SSL_IS_DTLS(s) && s->hit) {
805 * Change to new shared key of SCTP-Auth, will be ignored if
808 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
812 if (SSL_IS_TLS13(s)) {
813 if (!s->method->ssl3_enc->generate_master_secret(s,
814 s->master_secret, s->handshake_secret, 0,
815 &s->session->master_key_length)
816 || !s->method->ssl3_enc->change_cipher_state(s,
817 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
822 case TLS_ST_SW_KEY_UPDATE:
823 if (statem_flush(s) != 1)
825 if (!tls13_update_key(s, 1))
829 case TLS_ST_SW_SESSION_TICKET:
830 if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
835 return WORK_FINISHED_CONTINUE;
839 * Get the message construction function and message type for sending from the
842 * Valid return values are:
846 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
847 confunc_f *confunc, int *mt)
849 OSSL_STATEM *st = &s->statem;
851 switch (st->hand_state) {
853 /* Shouldn't happen */
856 case TLS_ST_SW_CHANGE:
858 *confunc = dtls_construct_change_cipher_spec;
860 *confunc = tls_construct_change_cipher_spec;
861 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
864 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
865 *confunc = dtls_construct_hello_verify_request;
866 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
869 case TLS_ST_SW_HELLO_REQ:
870 /* No construction function needed */
872 *mt = SSL3_MT_HELLO_REQUEST;
875 case TLS_ST_SW_SRVR_HELLO:
876 *confunc = tls_construct_server_hello;
877 *mt = SSL3_MT_SERVER_HELLO;
881 *confunc = tls_construct_server_certificate;
882 *mt = SSL3_MT_CERTIFICATE;
885 case TLS_ST_SW_CERT_VRFY:
886 *confunc = tls_construct_cert_verify;
887 *mt = SSL3_MT_CERTIFICATE_VERIFY;
891 case TLS_ST_SW_KEY_EXCH:
892 *confunc = tls_construct_server_key_exchange;
893 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
896 case TLS_ST_SW_CERT_REQ:
897 *confunc = tls_construct_certificate_request;
898 *mt = SSL3_MT_CERTIFICATE_REQUEST;
901 case TLS_ST_SW_SRVR_DONE:
902 *confunc = tls_construct_server_done;
903 *mt = SSL3_MT_SERVER_DONE;
906 case TLS_ST_SW_SESSION_TICKET:
907 *confunc = tls_construct_new_session_ticket;
908 *mt = SSL3_MT_NEWSESSION_TICKET;
911 case TLS_ST_SW_CERT_STATUS:
912 *confunc = tls_construct_cert_status;
913 *mt = SSL3_MT_CERTIFICATE_STATUS;
916 case TLS_ST_SW_FINISHED:
917 *confunc = tls_construct_finished;
918 *mt = SSL3_MT_FINISHED;
921 case TLS_ST_EARLY_DATA:
926 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
927 *confunc = tls_construct_encrypted_extensions;
928 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
931 case TLS_ST_SW_HELLO_RETRY_REQUEST:
932 *confunc = tls_construct_hello_retry_request;
933 *mt = SSL3_MT_HELLO_RETRY_REQUEST;
936 case TLS_ST_SW_KEY_UPDATE:
937 *confunc = tls_construct_key_update;
938 *mt = SSL3_MT_KEY_UPDATE;
946 * Maximum size (excluding the Handshake header) of a ClientHello message,
947 * calculated as follows:
949 * 2 + # client_version
950 * 32 + # only valid length for random
951 * 1 + # length of session_id
952 * 32 + # maximum size for session_id
953 * 2 + # length of cipher suites
954 * 2^16-2 + # maximum length of cipher suites array
955 * 1 + # length of compression_methods
956 * 2^8-1 + # maximum length of compression methods
957 * 2 + # length of extensions
958 * 2^16-1 # maximum length of extensions
960 #define CLIENT_HELLO_MAX_LENGTH 131396
962 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
963 #define NEXT_PROTO_MAX_LENGTH 514
966 * Returns the maximum allowed length for the current message that we are
967 * reading. Excludes the message header.
969 size_t ossl_statem_server_max_message_size(SSL *s)
971 OSSL_STATEM *st = &s->statem;
973 switch (st->hand_state) {
975 /* Shouldn't happen */
978 case TLS_ST_SR_CLNT_HELLO:
979 return CLIENT_HELLO_MAX_LENGTH;
981 case TLS_ST_SR_END_OF_EARLY_DATA:
982 return END_OF_EARLY_DATA_MAX_LENGTH;
985 return s->max_cert_list;
987 case TLS_ST_SR_KEY_EXCH:
988 return CLIENT_KEY_EXCH_MAX_LENGTH;
990 case TLS_ST_SR_CERT_VRFY:
991 return SSL3_RT_MAX_PLAIN_LENGTH;
993 #ifndef OPENSSL_NO_NEXTPROTONEG
994 case TLS_ST_SR_NEXT_PROTO:
995 return NEXT_PROTO_MAX_LENGTH;
998 case TLS_ST_SR_CHANGE:
999 return CCS_MAX_LENGTH;
1001 case TLS_ST_SR_FINISHED:
1002 return FINISHED_MAX_LENGTH;
1004 case TLS_ST_SR_KEY_UPDATE:
1005 return KEY_UPDATE_MAX_LENGTH;
1010 * Process a message that the server has received from the client.
1012 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1014 OSSL_STATEM *st = &s->statem;
1016 switch (st->hand_state) {
1018 /* Shouldn't happen */
1019 return MSG_PROCESS_ERROR;
1021 case TLS_ST_SR_CLNT_HELLO:
1022 return tls_process_client_hello(s, pkt);
1024 case TLS_ST_SR_END_OF_EARLY_DATA:
1025 return tls_process_end_of_early_data(s, pkt);
1027 case TLS_ST_SR_CERT:
1028 return tls_process_client_certificate(s, pkt);
1030 case TLS_ST_SR_KEY_EXCH:
1031 return tls_process_client_key_exchange(s, pkt);
1033 case TLS_ST_SR_CERT_VRFY:
1034 return tls_process_cert_verify(s, pkt);
1036 #ifndef OPENSSL_NO_NEXTPROTONEG
1037 case TLS_ST_SR_NEXT_PROTO:
1038 return tls_process_next_proto(s, pkt);
1041 case TLS_ST_SR_CHANGE:
1042 return tls_process_change_cipher_spec(s, pkt);
1044 case TLS_ST_SR_FINISHED:
1045 return tls_process_finished(s, pkt);
1047 case TLS_ST_SR_KEY_UPDATE:
1048 return tls_process_key_update(s, pkt);
1054 * Perform any further processing required following the receipt of a message
1057 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1059 OSSL_STATEM *st = &s->statem;
1061 switch (st->hand_state) {
1063 /* Shouldn't happen */
1066 case TLS_ST_SR_CLNT_HELLO:
1067 return tls_post_process_client_hello(s, wst);
1069 case TLS_ST_SR_KEY_EXCH:
1070 return tls_post_process_client_key_exchange(s, wst);
1072 return WORK_FINISHED_CONTINUE;
1075 #ifndef OPENSSL_NO_SRP
1076 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
1078 int ret = SSL_ERROR_NONE;
1080 *al = SSL_AD_UNRECOGNIZED_NAME;
1082 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1083 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1084 if (s->srp_ctx.login == NULL) {
1086 * RFC 5054 says SHOULD reject, we do so if There is no srp
1089 ret = SSL3_AL_FATAL;
1090 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1092 ret = SSL_srp_server_param_with_username(s, al);
1099 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1102 /* Always use DTLS 1.0 version: see RFC 6347 */
1103 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1104 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1110 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1112 unsigned int cookie_leni;
1113 if (s->ctx->app_gen_cookie_cb == NULL ||
1114 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1115 &cookie_leni) == 0 ||
1116 cookie_leni > 255) {
1117 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1118 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1121 s->d1->cookie_len = cookie_leni;
1123 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1124 s->d1->cookie_len)) {
1125 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST, ERR_R_INTERNAL_ERROR);
1132 #ifndef OPENSSL_NO_EC
1134 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1135 * SecureTransport using the TLS extension block in |hello|.
1136 * Safari, since 10.6, sends exactly these extensions, in this order:
1140 * signature_algorithms (for TLSv1.2 only)
1142 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1143 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1144 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1145 * 10.8..10.8.3 (which don't work).
1147 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1149 static const unsigned char kSafariExtensionsBlock[] = {
1150 0x00, 0x0a, /* elliptic_curves extension */
1151 0x00, 0x08, /* 8 bytes */
1152 0x00, 0x06, /* 6 bytes of curve ids */
1153 0x00, 0x17, /* P-256 */
1154 0x00, 0x18, /* P-384 */
1155 0x00, 0x19, /* P-521 */
1157 0x00, 0x0b, /* ec_point_formats */
1158 0x00, 0x02, /* 2 bytes */
1159 0x01, /* 1 point format */
1160 0x00, /* uncompressed */
1161 /* The following is only present in TLS 1.2 */
1162 0x00, 0x0d, /* signature_algorithms */
1163 0x00, 0x0c, /* 12 bytes */
1164 0x00, 0x0a, /* 10 bytes */
1165 0x05, 0x01, /* SHA-384/RSA */
1166 0x04, 0x01, /* SHA-256/RSA */
1167 0x02, 0x01, /* SHA-1/RSA */
1168 0x04, 0x03, /* SHA-256/ECDSA */
1169 0x02, 0x03, /* SHA-1/ECDSA */
1171 /* Length of the common prefix (first two extensions). */
1172 static const size_t kSafariCommonExtensionsLength = 18;
1177 tmppkt = hello->extensions;
1179 if (!PACKET_forward(&tmppkt, 2)
1180 || !PACKET_get_net_2(&tmppkt, &type)
1181 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1185 if (type != TLSEXT_TYPE_server_name)
1188 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1189 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1191 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1194 #endif /* !OPENSSL_NO_EC */
1196 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1198 int al = SSL_AD_INTERNAL_ERROR;
1199 /* |cookie| will only be initialized for DTLS. */
1200 PACKET session_id, compression, extensions, cookie;
1201 static const unsigned char null_compression = 0;
1202 CLIENTHELLO_MSG *clienthello;
1204 clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1205 if (clienthello == NULL) {
1206 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1209 /* Check if this is actually an unexpected renegotiation ClientHello */
1210 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1211 if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
1212 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1220 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1222 clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1223 PACKET_null_init(&cookie);
1225 if (clienthello->isv2) {
1228 if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {
1229 al = SSL_AD_UNEXPECTED_MESSAGE;
1230 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1235 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1236 * header is sent directly on the wire, not wrapped as a TLS
1237 * record. Our record layer just processes the message length and passes
1238 * the rest right through. Its format is:
1240 * 0-1 msg_length - decoded by the record layer
1241 * 2 msg_type - s->init_msg points here
1243 * 5-6 cipher_spec_length
1244 * 7-8 session_id_length
1245 * 9-10 challenge_length
1249 if (!PACKET_get_1(pkt, &mt)
1250 || mt != SSL2_MT_CLIENT_HELLO) {
1252 * Should never happen. We should have tested this in the record
1253 * layer in order to have determined that this is a SSLv2 record
1254 * in the first place
1256 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1261 if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1262 al = SSL_AD_DECODE_ERROR;
1263 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1267 /* Parse the message and load client random. */
1268 if (clienthello->isv2) {
1270 * Handle an SSLv2 backwards compatible ClientHello
1271 * Note, this is only for SSLv3+ using the backward compatible format.
1272 * Real SSLv2 is not supported, and is rejected below.
1274 unsigned int ciphersuite_len, session_id_len, challenge_len;
1277 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1278 || !PACKET_get_net_2(pkt, &session_id_len)
1279 || !PACKET_get_net_2(pkt, &challenge_len)) {
1280 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1281 SSL_R_RECORD_LENGTH_MISMATCH);
1282 al = SSL_AD_DECODE_ERROR;
1286 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1287 al = SSL_AD_ILLEGAL_PARAMETER;
1288 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1292 if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1294 || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1295 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1296 /* No extensions. */
1297 || PACKET_remaining(pkt) != 0) {
1298 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1299 SSL_R_RECORD_LENGTH_MISMATCH);
1300 al = SSL_AD_DECODE_ERROR;
1303 clienthello->session_id_len = session_id_len;
1305 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1306 * here rather than sizeof(clienthello->random) because that is the limit
1307 * for SSLv3 and it is fixed. It won't change even if
1308 * sizeof(clienthello->random) does.
1310 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1311 ? SSL3_RANDOM_SIZE : challenge_len;
1312 memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1313 if (!PACKET_copy_bytes(&challenge,
1314 clienthello->random + SSL3_RANDOM_SIZE -
1315 challenge_len, challenge_len)
1316 /* Advertise only null compression. */
1317 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1318 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1319 al = SSL_AD_INTERNAL_ERROR;
1323 PACKET_null_init(&clienthello->extensions);
1325 /* Regular ClientHello. */
1326 if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1327 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1328 || !PACKET_copy_all(&session_id, clienthello->session_id,
1329 SSL_MAX_SSL_SESSION_ID_LENGTH,
1330 &clienthello->session_id_len)) {
1331 al = SSL_AD_DECODE_ERROR;
1332 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1336 if (SSL_IS_DTLS(s)) {
1337 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1338 al = SSL_AD_DECODE_ERROR;
1339 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1342 if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1343 DTLS1_COOKIE_LENGTH,
1344 &clienthello->dtls_cookie_len)) {
1345 al = SSL_AD_INTERNAL_ERROR;
1346 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1350 * If we require cookies and this ClientHello doesn't contain one,
1351 * just return since we do not want to allocate any memory yet.
1352 * So check cookie length...
1354 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1355 if (clienthello->dtls_cookie_len == 0)
1360 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1361 al = SSL_AD_DECODE_ERROR;
1362 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1366 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1367 al = SSL_AD_DECODE_ERROR;
1368 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1372 /* Could be empty. */
1373 if (PACKET_remaining(pkt) == 0) {
1374 PACKET_null_init(&clienthello->extensions);
1376 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
1377 || PACKET_remaining(pkt) != 0) {
1378 al = SSL_AD_DECODE_ERROR;
1379 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1385 if (!PACKET_copy_all(&compression, clienthello->compressions,
1386 MAX_COMPRESSIONS_SIZE,
1387 &clienthello->compressions_len)) {
1388 al = SSL_AD_INTERNAL_ERROR;
1389 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1393 /* Preserve the raw extensions PACKET for later use */
1394 extensions = clienthello->extensions;
1395 if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
1396 &clienthello->pre_proc_exts, &al,
1397 &clienthello->pre_proc_exts_len, 1)) {
1398 /* SSLerr already been called */
1401 s->clienthello = clienthello;
1403 return MSG_PROCESS_CONTINUE_PROCESSING;
1405 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1407 ossl_statem_set_error(s);
1409 if (clienthello != NULL)
1410 OPENSSL_free(clienthello->pre_proc_exts);
1411 OPENSSL_free(clienthello);
1413 return MSG_PROCESS_ERROR;
1416 static int tls_early_post_process_client_hello(SSL *s, int *pal)
1419 int i, al = SSL_AD_INTERNAL_ERROR;
1423 #ifndef OPENSSL_NO_COMP
1424 SSL_COMP *comp = NULL;
1426 const SSL_CIPHER *c;
1427 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1428 STACK_OF(SSL_CIPHER) *scsvs = NULL;
1429 CLIENTHELLO_MSG *clienthello = s->clienthello;
1430 DOWNGRADE dgrd = DOWNGRADE_NONE;
1432 /* Finished parsing the ClientHello, now we can start processing it */
1433 /* Give the early callback a crack at things */
1434 if (s->ctx->early_cb != NULL) {
1436 /* A failure in the early callback terminates the connection. */
1437 code = s->ctx->early_cb(s, &al, s->ctx->early_cb_arg);
1441 s->rwstate = SSL_EARLY_WORK;
1446 /* Set up the client_random */
1447 memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
1449 /* Choose the version */
1451 if (clienthello->isv2) {
1452 if (clienthello->legacy_version == SSL2_VERSION
1453 || (clienthello->legacy_version & 0xff00)
1454 != (SSL3_VERSION_MAJOR << 8)) {
1456 * This is real SSLv2 or something complete unknown. We don't
1459 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1463 s->client_version = clienthello->legacy_version;
1466 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1467 * versions are potentially compatible. Version negotiation comes later.
1469 if (!SSL_IS_DTLS(s)) {
1470 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1471 } else if (s->method->version != DTLS_ANY_VERSION &&
1472 DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1473 protverr = SSL_R_VERSION_TOO_LOW;
1479 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1480 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1481 /* like ssl3_get_record, send alert using remote version number */
1482 s->version = s->client_version = clienthello->legacy_version;
1484 al = SSL_AD_PROTOCOL_VERSION;
1488 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1489 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1490 al = SSL_AD_UNEXPECTED_MESSAGE;
1491 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1492 SSL_R_NOT_ON_RECORD_BOUNDARY);
1496 if (SSL_IS_DTLS(s)) {
1497 /* Empty cookie was already handled above by returning early. */
1498 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1499 if (s->ctx->app_verify_cookie_cb != NULL) {
1500 if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1501 clienthello->dtls_cookie_len) == 0) {
1502 al = SSL_AD_HANDSHAKE_FAILURE;
1503 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1504 SSL_R_COOKIE_MISMATCH);
1506 /* else cookie verification succeeded */
1508 /* default verification */
1509 } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1510 || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1511 s->d1->cookie_len) != 0) {
1512 al = SSL_AD_HANDSHAKE_FAILURE;
1513 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1516 s->d1->cookie_verified = 1;
1518 if (s->method->version == DTLS_ANY_VERSION) {
1519 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1520 if (protverr != 0) {
1521 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1522 s->version = s->client_version;
1523 al = SSL_AD_PROTOCOL_VERSION;
1531 if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1532 clienthello->isv2, &al) ||
1533 !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
1534 clienthello->isv2, &al)) {
1538 s->s3->send_connection_binding = 0;
1539 /* Check what signalling cipher-suite values were received. */
1540 if (scsvs != NULL) {
1541 for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1542 c = sk_SSL_CIPHER_value(scsvs, i);
1543 if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1544 if (s->renegotiate) {
1545 /* SCSV is fatal if renegotiating */
1546 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1547 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1548 al = SSL_AD_HANDSHAKE_FAILURE;
1551 s->s3->send_connection_binding = 1;
1552 } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1553 !ssl_check_version_downgrade(s)) {
1555 * This SCSV indicates that the client previously tried
1556 * a higher version. We should fail if the current version
1557 * is an unexpected downgrade, as that indicates that the first
1558 * connection may have been tampered with in order to trigger
1559 * an insecure downgrade.
1561 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1562 SSL_R_INAPPROPRIATE_FALLBACK);
1563 al = SSL_AD_INAPPROPRIATE_FALLBACK;
1569 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1570 if (SSL_IS_TLS13(s)) {
1571 const SSL_CIPHER *cipher =
1572 ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
1574 if (cipher == NULL) {
1575 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1576 SSL_R_NO_SHARED_CIPHER);
1577 al = SSL_AD_HANDSHAKE_FAILURE;
1580 if (s->hello_retry_request
1581 && (s->s3->tmp.new_cipher == NULL
1582 || s->s3->tmp.new_cipher->id != cipher->id)) {
1584 * A previous HRR picked a different ciphersuite to the one we
1585 * just selected. Something must have changed.
1587 al = SSL_AD_ILLEGAL_PARAMETER;
1588 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_BAD_CIPHER);
1591 s->s3->tmp.new_cipher = cipher;
1594 /* We need to do this before getting the session */
1595 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1596 SSL_EXT_CLIENT_HELLO,
1597 clienthello->pre_proc_exts, NULL, 0, &al)) {
1598 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1603 * We don't allow resumption in a backwards compatible ClientHello.
1604 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1606 * Versions before 0.9.7 always allow clients to resume sessions in
1607 * renegotiation. 0.9.7 and later allow this by default, but optionally
1608 * ignore resumption requests with flag
1609 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1610 * than a change to default behavior so that applications relying on
1611 * this for security won't even compile against older library versions).
1612 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1613 * request renegotiation but not a new session (s->new_session remains
1614 * unset): for servers, this essentially just means that the
1615 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1618 if (clienthello->isv2 ||
1620 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1621 if (!ssl_get_new_session(s, 1))
1624 i = ssl_get_prev_session(s, clienthello, &al);
1626 /* previous session */
1628 } else if (i == -1) {
1632 if (!ssl_get_new_session(s, 1))
1638 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1639 * ciphersuite compatibility with the session as part of resumption.
1641 if (!SSL_IS_TLS13(s) && s->hit) {
1643 id = s->session->cipher->id;
1646 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1648 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1649 c = sk_SSL_CIPHER_value(ciphers, i);
1651 fprintf(stderr, "client [%2d of %2d]:%s\n",
1652 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1661 * we need to have the cipher in the cipher list if we are asked
1664 al = SSL_AD_ILLEGAL_PARAMETER;
1665 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1666 SSL_R_REQUIRED_CIPHER_MISSING);
1671 for (loop = 0; loop < clienthello->compressions_len; loop++) {
1672 if (clienthello->compressions[loop] == 0)
1676 if (loop >= clienthello->compressions_len) {
1678 al = SSL_AD_DECODE_ERROR;
1679 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1683 #ifndef OPENSSL_NO_EC
1684 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1685 ssl_check_for_safari(s, clienthello);
1686 #endif /* !OPENSSL_NO_EC */
1688 /* TLS extensions */
1689 if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
1690 clienthello->pre_proc_exts, NULL, 0, &al, 1)) {
1691 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1696 * Check if we want to use external pre-shared secret for this handshake
1697 * for not reused session only. We need to generate server_random before
1698 * calling tls_session_secret_cb in order to allow SessionTicket
1699 * processing to use it in key derivation.
1703 pos = s->s3->server_random;
1704 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
1710 && s->version >= TLS1_VERSION
1713 && s->ext.session_secret_cb) {
1714 const SSL_CIPHER *pref_cipher = NULL;
1716 * s->session->master_key_length is a size_t, but this is an int for
1717 * backwards compat reasons
1719 int master_key_length;
1721 master_key_length = sizeof(s->session->master_key);
1722 if (s->ext.session_secret_cb(s, s->session->master_key,
1723 &master_key_length, ciphers,
1725 s->ext.session_secret_cb_arg)
1726 && master_key_length > 0) {
1727 s->session->master_key_length = master_key_length;
1729 s->session->ciphers = ciphers;
1730 s->session->verify_result = X509_V_OK;
1734 /* check if some cipher was preferred by call back */
1735 if (pref_cipher == NULL)
1736 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1737 SSL_get_ciphers(s));
1738 if (pref_cipher == NULL) {
1739 al = SSL_AD_HANDSHAKE_FAILURE;
1740 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1744 s->session->cipher = pref_cipher;
1745 sk_SSL_CIPHER_free(s->cipher_list);
1746 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1747 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1748 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1753 * Worst case, we will use the NULL compression, but if we have other
1754 * options, we will now look for them. We have complen-1 compression
1755 * algorithms from the client, starting at q.
1757 s->s3->tmp.new_compression = NULL;
1758 if (SSL_IS_TLS13(s)) {
1760 * We already checked above that the NULL compression method appears in
1761 * the list. Now we check there aren't any others (which is illegal in
1762 * a TLSv1.3 ClientHello.
1764 if (clienthello->compressions_len != 1) {
1765 al = SSL_AD_ILLEGAL_PARAMETER;
1766 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1767 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1771 #ifndef OPENSSL_NO_COMP
1772 /* This only happens if we have a cache hit */
1773 else if (s->session->compress_meth != 0) {
1774 int m, comp_id = s->session->compress_meth;
1776 /* Perform sanity checks on resumed compression algorithm */
1777 /* Can't disable compression */
1778 if (!ssl_allow_compression(s)) {
1779 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1780 SSL_R_INCONSISTENT_COMPRESSION);
1783 /* Look for resumed compression method */
1784 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1785 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1786 if (comp_id == comp->id) {
1787 s->s3->tmp.new_compression = comp;
1791 if (s->s3->tmp.new_compression == NULL) {
1792 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1793 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1796 /* Look for resumed method in compression list */
1797 for (k = 0; k < clienthello->compressions_len; k++) {
1798 if (clienthello->compressions[k] == comp_id)
1801 if (k >= clienthello->compressions_len) {
1802 al = SSL_AD_ILLEGAL_PARAMETER;
1803 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1804 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1807 } else if (s->hit) {
1809 } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1810 /* See if we have a match */
1811 int m, nn, v, done = 0;
1814 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1815 for (m = 0; m < nn; m++) {
1816 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1818 for (o = 0; o < clienthello->compressions_len; o++) {
1819 if (v == clienthello->compressions[o]) {
1828 s->s3->tmp.new_compression = comp;
1834 * If compression is disabled we'd better not try to resume a session
1835 * using compression.
1837 if (s->session->compress_meth != 0) {
1838 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1844 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1847 if (!s->hit || SSL_IS_TLS13(s)) {
1848 sk_SSL_CIPHER_free(s->session->ciphers);
1849 s->session->ciphers = ciphers;
1850 if (ciphers == NULL) {
1851 al = SSL_AD_INTERNAL_ERROR;
1852 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1859 #ifdef OPENSSL_NO_COMP
1860 s->session->compress_meth = 0;
1862 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1864 if (!tls1_set_server_sigalgs(s)) {
1865 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1870 sk_SSL_CIPHER_free(ciphers);
1871 sk_SSL_CIPHER_free(scsvs);
1872 OPENSSL_free(clienthello->pre_proc_exts);
1873 OPENSSL_free(s->clienthello);
1874 s->clienthello = NULL;
1877 ossl_statem_set_error(s);
1880 sk_SSL_CIPHER_free(ciphers);
1881 sk_SSL_CIPHER_free(scsvs);
1882 OPENSSL_free(clienthello->pre_proc_exts);
1883 OPENSSL_free(s->clienthello);
1884 s->clienthello = NULL;
1890 * Call the status request callback if needed. Upon success, returns 1.
1891 * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
1893 static int tls_handle_status_request(SSL *s, int *al)
1895 s->ext.status_expected = 0;
1898 * If status request then ask callback what to do. Note: this must be
1899 * called after servername callbacks in case the certificate has changed,
1900 * and must be called after the cipher has been chosen because this may
1901 * influence which certificate is sent
1903 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
1904 && s->ctx->ext.status_cb != NULL) {
1907 /* If no certificate can't return certificate status */
1908 if (s->s3->tmp.cert != NULL) {
1910 * Set current certificate to one we will use so SSL_get_certificate
1911 * et al can pick it up.
1913 s->cert->key = s->s3->tmp.cert;
1914 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
1916 /* We don't want to send a status request response */
1917 case SSL_TLSEXT_ERR_NOACK:
1918 s->ext.status_expected = 0;
1920 /* status request response should be sent */
1921 case SSL_TLSEXT_ERR_OK:
1922 if (s->ext.ocsp.resp)
1923 s->ext.status_expected = 1;
1925 /* something bad happened */
1926 case SSL_TLSEXT_ERR_ALERT_FATAL:
1928 *al = SSL_AD_INTERNAL_ERROR;
1937 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
1939 int al = SSL_AD_HANDSHAKE_FAILURE;
1940 const SSL_CIPHER *cipher;
1942 if (wst == WORK_MORE_A) {
1943 int rv = tls_early_post_process_client_hello(s, &al);
1945 /* SSLErr() was already called */
1952 if (wst == WORK_MORE_B) {
1953 if (!s->hit || SSL_IS_TLS13(s)) {
1954 /* Let cert callback update server certificates if required */
1955 if (!s->hit && s->cert->cert_cb != NULL) {
1956 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1958 al = SSL_AD_INTERNAL_ERROR;
1959 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1960 SSL_R_CERT_CB_ERROR);
1964 s->rwstate = SSL_X509_LOOKUP;
1967 s->rwstate = SSL_NOTHING;
1970 /* In TLSv1.3 we selected the ciphersuite before resumption */
1971 if (!SSL_IS_TLS13(s)) {
1973 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1975 if (cipher == NULL) {
1976 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1977 SSL_R_NO_SHARED_CIPHER);
1980 s->s3->tmp.new_cipher = cipher;
1983 if (!tls_choose_sigalg(s, &al))
1985 /* check whether we should disable session resumption */
1986 if (s->not_resumable_session_cb != NULL)
1987 s->session->not_resumable =
1988 s->not_resumable_session_cb(s,
1989 ((s->s3->tmp.new_cipher->algorithm_mkey
1990 & (SSL_kDHE | SSL_kECDHE)) != 0));
1991 if (s->session->not_resumable)
1992 /* do not send a session ticket */
1993 s->ext.ticket_expected = 0;
1996 /* Session-id reuse */
1997 s->s3->tmp.new_cipher = s->session->cipher;
2001 * we now have the following setup.
2003 * cipher_list - our preferred list of ciphers
2004 * ciphers - the clients preferred list of ciphers
2005 * compression - basically ignored right now
2006 * ssl version is set - sslv3
2007 * s->session - The ssl session has been setup.
2008 * s->hit - session reuse flag
2009 * s->s3->tmp.new_cipher- the new cipher to use.
2013 * Call status_request callback if needed. Has to be done after the
2014 * certificate callbacks etc above.
2016 if (!tls_handle_status_request(s, &al)) {
2017 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2018 SSL_R_CLIENTHELLO_TLSEXT);
2024 #ifndef OPENSSL_NO_SRP
2025 if (wst == WORK_MORE_C) {
2027 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
2029 * callback indicates further work to be done
2031 s->rwstate = SSL_X509_LOOKUP;
2034 if (ret != SSL_ERROR_NONE) {
2036 * This is not really an error but the only means to for
2037 * a client to detect whether srp is supported.
2039 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
2040 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2041 SSL_R_CLIENTHELLO_TLSEXT);
2043 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2044 SSL_R_PSK_IDENTITY_NOT_FOUND);
2050 return WORK_FINISHED_STOP;
2052 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2053 ossl_statem_set_error(s);
2057 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
2059 int compm, al = SSL_AD_INTERNAL_ERROR;
2063 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
2064 version = SSL_IS_TLS13(s) ? TLS1_3_VERSION_DRAFT : s->version;
2065 if (!WPACKET_put_bytes_u16(pkt, version)
2067 * Random stuff. Filling of the server_random takes place in
2068 * tls_process_client_hello()
2070 || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
2071 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2076 * There are several cases for the session ID to send
2077 * back in the server hello:
2078 * - For session reuse from the session cache,
2079 * we send back the old session ID.
2080 * - If stateless session reuse (using a session ticket)
2081 * is successful, we send back the client's "session ID"
2082 * (which doesn't actually identify the session).
2083 * - If it is a new session, we send back the new
2085 * - However, if we want the new session to be single-use,
2086 * we send back a 0-length session ID.
2087 * s->hit is non-zero in either case of session reuse,
2088 * so the following won't overwrite an ID that we're supposed
2091 if (s->session->not_resumable ||
2092 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2094 s->session->session_id_length = 0;
2096 sl = s->session->session_id_length;
2097 if (sl > sizeof(s->session->session_id)) {
2098 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2102 /* set up the compression method */
2103 #ifdef OPENSSL_NO_COMP
2106 if (s->s3->tmp.new_compression == NULL)
2109 compm = s->s3->tmp.new_compression->id;
2112 if ((!SSL_IS_TLS13(s)
2113 && !WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl))
2114 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
2115 || (!SSL_IS_TLS13(s)
2116 && !WPACKET_put_bytes_u8(pkt, compm))
2117 || !tls_construct_extensions(s, pkt,
2119 ? SSL_EXT_TLS1_3_SERVER_HELLO
2120 : SSL_EXT_TLS1_2_SERVER_HELLO,
2122 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2126 if (!(s->verify_mode & SSL_VERIFY_PEER)
2127 && !ssl3_digest_cached_records(s, 0)) {
2128 al = SSL_AD_INTERNAL_ERROR;
2134 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2138 int tls_construct_server_done(SSL *s, WPACKET *pkt)
2140 if (!s->s3->tmp.cert_request) {
2141 if (!ssl3_digest_cached_records(s, 0)) {
2142 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2149 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2151 #ifndef OPENSSL_NO_DH
2152 EVP_PKEY *pkdh = NULL;
2154 #ifndef OPENSSL_NO_EC
2155 unsigned char *encodedPoint = NULL;
2156 size_t encodedlen = 0;
2159 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2160 int al = SSL_AD_INTERNAL_ERROR, i;
2163 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2164 EVP_PKEY_CTX *pctx = NULL;
2165 size_t paramlen, paramoffset;
2167 if (!WPACKET_get_total_written(pkt, ¶moffset)) {
2168 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2172 if (md_ctx == NULL) {
2173 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2177 type = s->s3->tmp.new_cipher->algorithm_mkey;
2179 r[0] = r[1] = r[2] = r[3] = NULL;
2180 #ifndef OPENSSL_NO_PSK
2181 /* Plain PSK or RSAPSK nothing to do */
2182 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2184 #endif /* !OPENSSL_NO_PSK */
2185 #ifndef OPENSSL_NO_DH
2186 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2187 CERT *cert = s->cert;
2189 EVP_PKEY *pkdhp = NULL;
2192 if (s->cert->dh_tmp_auto) {
2193 DH *dhp = ssl_get_auto_dh(s);
2194 pkdh = EVP_PKEY_new();
2195 if (pkdh == NULL || dhp == NULL) {
2197 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2198 ERR_R_INTERNAL_ERROR);
2201 EVP_PKEY_assign_DH(pkdh, dhp);
2204 pkdhp = cert->dh_tmp;
2206 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2207 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2208 pkdh = ssl_dh_to_pkey(dhp);
2210 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2211 ERR_R_INTERNAL_ERROR);
2216 if (pkdhp == NULL) {
2217 al = SSL_AD_INTERNAL_ERROR;
2218 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2219 SSL_R_MISSING_TMP_DH_KEY);
2222 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2223 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2224 al = SSL_AD_HANDSHAKE_FAILURE;
2225 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2226 SSL_R_DH_KEY_TOO_SMALL);
2229 if (s->s3->tmp.pkey != NULL) {
2230 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2231 ERR_R_INTERNAL_ERROR);
2235 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2237 if (s->s3->tmp.pkey == NULL) {
2238 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2242 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2244 EVP_PKEY_free(pkdh);
2247 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2248 DH_get0_key(dh, &r[2], NULL);
2251 #ifndef OPENSSL_NO_EC
2252 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2255 if (s->s3->tmp.pkey != NULL) {
2256 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2257 ERR_R_INTERNAL_ERROR);
2261 /* Get NID of appropriate shared curve */
2262 nid = tls1_shared_group(s, -2);
2263 curve_id = tls1_ec_nid2curve_id(nid);
2264 if (curve_id == 0) {
2265 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2266 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2269 s->s3->tmp.pkey = ssl_generate_pkey_curve(curve_id);
2270 /* Generate a new key for this curve */
2271 if (s->s3->tmp.pkey == NULL) {
2272 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2276 /* Encode the public key. */
2277 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2279 if (encodedlen == 0) {
2280 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2285 * We'll generate the serverKeyExchange message explicitly so we
2286 * can set these to NULLs
2293 #endif /* !OPENSSL_NO_EC */
2294 #ifndef OPENSSL_NO_SRP
2295 if (type & SSL_kSRP) {
2296 if ((s->srp_ctx.N == NULL) ||
2297 (s->srp_ctx.g == NULL) ||
2298 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2299 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2300 SSL_R_MISSING_SRP_PARAM);
2303 r[0] = s->srp_ctx.N;
2304 r[1] = s->srp_ctx.g;
2305 r[2] = s->srp_ctx.s;
2306 r[3] = s->srp_ctx.B;
2310 al = SSL_AD_INTERNAL_ERROR;
2311 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2312 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2316 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2317 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2319 } else if (lu == NULL) {
2320 al = SSL_AD_DECODE_ERROR;
2324 #ifndef OPENSSL_NO_PSK
2325 if (type & SSL_PSK) {
2326 size_t len = (s->cert->psk_identity_hint == NULL)
2327 ? 0 : strlen(s->cert->psk_identity_hint);
2330 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2331 * checked this when we set the identity hint - but just in case
2333 if (len > PSK_MAX_IDENTITY_LEN
2334 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2336 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2337 ERR_R_INTERNAL_ERROR);
2343 for (i = 0; i < 4 && r[i] != NULL; i++) {
2344 unsigned char *binval;
2347 #ifndef OPENSSL_NO_SRP
2348 if ((i == 2) && (type & SSL_kSRP)) {
2349 res = WPACKET_start_sub_packet_u8(pkt);
2352 res = WPACKET_start_sub_packet_u16(pkt);
2355 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2356 ERR_R_INTERNAL_ERROR);
2360 #ifndef OPENSSL_NO_DH
2362 * for interoperability with some versions of the Microsoft TLS
2363 * stack, we need to zero pad the DHE pub key to the same length
2366 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2367 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2370 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2371 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2372 ERR_R_INTERNAL_ERROR);
2375 memset(binval, 0, len);
2379 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2380 || !WPACKET_close(pkt)) {
2381 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2382 ERR_R_INTERNAL_ERROR);
2386 BN_bn2bin(r[i], binval);
2389 #ifndef OPENSSL_NO_EC
2390 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2392 * We only support named (not generic) curves. In this situation, the
2393 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2394 * [1 byte length of encoded point], followed by the actual encoded
2397 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2398 || !WPACKET_put_bytes_u8(pkt, 0)
2399 || !WPACKET_put_bytes_u8(pkt, curve_id)
2400 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2401 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2402 ERR_R_INTERNAL_ERROR);
2405 OPENSSL_free(encodedPoint);
2406 encodedPoint = NULL;
2412 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2414 unsigned char *sigbytes1, *sigbytes2, *tbs;
2415 size_t siglen, tbslen;
2418 if (pkey == NULL || md == NULL) {
2419 /* Should never happen */
2420 al = SSL_AD_INTERNAL_ERROR;
2421 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2422 ERR_R_INTERNAL_ERROR);
2426 * n is the length of the params, they start at &(d[4]) and p
2427 * points to the space at the end.
2430 /* Get length of the parameters we have written above */
2431 if (!WPACKET_get_length(pkt, ¶mlen)) {
2432 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2433 ERR_R_INTERNAL_ERROR);
2436 /* send signature algorithm */
2437 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg))
2440 * Create the signature. We don't know the actual length of the sig
2441 * until after we've created it, so we reserve enough bytes for it
2442 * up front, and then properly allocate them in the WPACKET
2445 siglen = EVP_PKEY_size(pkey);
2446 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2447 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2448 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2449 ERR_R_INTERNAL_ERROR);
2452 if (lu->sig == EVP_PKEY_RSA_PSS) {
2453 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2454 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2455 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2460 tbslen = construct_key_exchange_tbs(s, &tbs,
2461 s->init_buf->data + paramoffset,
2464 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2465 ERR_R_MALLOC_FAILURE);
2468 rv = EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen);
2470 if (rv <= 0 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2471 || sigbytes1 != sigbytes2) {
2472 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2473 ERR_R_INTERNAL_ERROR);
2478 EVP_MD_CTX_free(md_ctx);
2481 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2483 #ifndef OPENSSL_NO_DH
2484 EVP_PKEY_free(pkdh);
2486 #ifndef OPENSSL_NO_EC
2487 OPENSSL_free(encodedPoint);
2489 EVP_MD_CTX_free(md_ctx);
2493 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2495 int al = SSL_AD_INTERNAL_ERROR;
2497 if (SSL_IS_TLS13(s)) {
2498 /* TODO(TLS1.3) for now send empty request context */
2499 if (!WPACKET_put_bytes_u8(pkt, 0)) {
2500 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2501 ERR_R_INTERNAL_ERROR);
2505 if (!tls_construct_extensions(s, pkt,
2506 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2508 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2509 ERR_R_INTERNAL_ERROR);
2515 /* get the list of acceptable cert types */
2516 if (!WPACKET_start_sub_packet_u8(pkt)
2517 || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2518 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2522 if (SSL_USE_SIGALGS(s)) {
2523 const uint16_t *psigs;
2524 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2526 if (!WPACKET_start_sub_packet_u16(pkt)
2527 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
2528 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2529 || !WPACKET_close(pkt)) {
2530 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2531 ERR_R_INTERNAL_ERROR);
2536 if (!construct_ca_names(s, pkt)) {
2537 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2542 s->s3->tmp.cert_request = 1;
2545 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2549 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt, int *al)
2551 #ifndef OPENSSL_NO_PSK
2552 unsigned char psk[PSK_MAX_PSK_LEN];
2554 PACKET psk_identity;
2556 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2557 *al = SSL_AD_DECODE_ERROR;
2558 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
2561 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2562 *al = SSL_AD_DECODE_ERROR;
2563 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
2566 if (s->psk_server_callback == NULL) {
2567 *al = SSL_AD_INTERNAL_ERROR;
2568 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_SERVER_CB);
2572 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2573 *al = SSL_AD_INTERNAL_ERROR;
2574 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2578 psklen = s->psk_server_callback(s, s->session->psk_identity,
2581 if (psklen > PSK_MAX_PSK_LEN) {
2582 *al = SSL_AD_INTERNAL_ERROR;
2583 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2585 } else if (psklen == 0) {
2587 * PSK related to the given identity not found
2589 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2590 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2591 SSL_R_PSK_IDENTITY_NOT_FOUND);
2595 OPENSSL_free(s->s3->tmp.psk);
2596 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2597 OPENSSL_cleanse(psk, psklen);
2599 if (s->s3->tmp.psk == NULL) {
2600 *al = SSL_AD_INTERNAL_ERROR;
2601 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2605 s->s3->tmp.psklen = psklen;
2609 /* Should never happen */
2610 *al = SSL_AD_INTERNAL_ERROR;
2611 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2616 static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)
2618 #ifndef OPENSSL_NO_RSA
2619 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2621 unsigned char decrypt_good, version_good;
2622 size_t j, padding_len;
2623 PACKET enc_premaster;
2625 unsigned char *rsa_decrypt = NULL;
2628 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2630 *al = SSL_AD_INTERNAL_ERROR;
2631 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);
2635 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2636 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2637 enc_premaster = *pkt;
2639 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2640 || PACKET_remaining(pkt) != 0) {
2641 *al = SSL_AD_DECODE_ERROR;
2642 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);
2648 * We want to be sure that the plaintext buffer size makes it safe to
2649 * iterate over the entire size of a premaster secret
2650 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2651 * their ciphertext cannot accommodate a premaster secret anyway.
2653 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2654 *al = SSL_AD_INTERNAL_ERROR;
2655 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
2659 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2660 if (rsa_decrypt == NULL) {
2661 *al = SSL_AD_INTERNAL_ERROR;
2662 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);
2667 * We must not leak whether a decryption failure occurs because of
2668 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2669 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2670 * generates a random premaster secret for the case that the decrypt
2671 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2674 if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)
2678 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2679 * the timing-sensitive code below.
2681 /* TODO(size_t): Convert this function */
2682 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2683 PACKET_data(&enc_premaster),
2684 rsa_decrypt, rsa, RSA_NO_PADDING);
2685 if (decrypt_len < 0)
2688 /* Check the padding. See RFC 3447, section 7.2.2. */
2691 * The smallest padded premaster is 11 bytes of overhead. Small keys
2692 * are publicly invalid, so this may return immediately. This ensures
2693 * PS is at least 8 bytes.
2695 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2696 *al = SSL_AD_DECRYPT_ERROR;
2697 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);
2701 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2702 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2703 constant_time_eq_int_8(rsa_decrypt[1], 2);
2704 for (j = 2; j < padding_len - 1; j++) {
2705 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2707 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2710 * If the version in the decrypted pre-master secret is correct then
2711 * version_good will be 0xff, otherwise it'll be zero. The
2712 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2713 * (http://eprint.iacr.org/2003/052/) exploits the version number
2714 * check as a "bad version oracle". Thus version checks are done in
2715 * constant time and are treated like any other decryption error.
2718 constant_time_eq_8(rsa_decrypt[padding_len],
2719 (unsigned)(s->client_version >> 8));
2721 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2722 (unsigned)(s->client_version & 0xff));
2725 * The premaster secret must contain the same version number as the
2726 * ClientHello to detect version rollback attacks (strangely, the
2727 * protocol does not offer such protection for DH ciphersuites).
2728 * However, buggy clients exist that send the negotiated protocol
2729 * version instead if the server does not support the requested
2730 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2733 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2734 unsigned char workaround_good;
2735 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2736 (unsigned)(s->version >> 8));
2738 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2739 (unsigned)(s->version & 0xff));
2740 version_good |= workaround_good;
2744 * Both decryption and version must be good for decrypt_good to
2745 * remain non-zero (0xff).
2747 decrypt_good &= version_good;
2750 * Now copy rand_premaster_secret over from p using
2751 * decrypt_good_mask. If decryption failed, then p does not
2752 * contain valid plaintext, however, a check above guarantees
2753 * it is still sufficiently large to read from.
2755 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2756 rsa_decrypt[padding_len + j] =
2757 constant_time_select_8(decrypt_good,
2758 rsa_decrypt[padding_len + j],
2759 rand_premaster_secret[j]);
2762 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2763 sizeof(rand_premaster_secret), 0)) {
2764 *al = SSL_AD_INTERNAL_ERROR;
2765 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2771 OPENSSL_free(rsa_decrypt);
2774 /* Should never happen */
2775 *al = SSL_AD_INTERNAL_ERROR;
2776 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2781 static int tls_process_cke_dhe(SSL *s, PACKET *pkt, int *al)
2783 #ifndef OPENSSL_NO_DH
2784 EVP_PKEY *skey = NULL;
2788 const unsigned char *data;
2789 EVP_PKEY *ckey = NULL;
2792 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2793 *al = SSL_AD_DECODE_ERROR;
2794 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE,
2795 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2798 skey = s->s3->tmp.pkey;
2800 *al = SSL_AD_INTERNAL_ERROR;
2801 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2805 if (PACKET_remaining(pkt) == 0L) {
2806 *al = SSL_AD_DECODE_ERROR;
2807 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2810 if (!PACKET_get_bytes(pkt, &data, i)) {
2811 /* We already checked we have enough data */
2812 *al = SSL_AD_INTERNAL_ERROR;
2813 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2816 ckey = EVP_PKEY_new();
2817 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2818 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_BN_LIB);
2821 cdh = EVP_PKEY_get0_DH(ckey);
2822 pub_key = BN_bin2bn(data, i, NULL);
2824 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
2825 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2826 if (pub_key != NULL)
2831 if (ssl_derive(s, skey, ckey, 1) == 0) {
2832 *al = SSL_AD_INTERNAL_ERROR;
2833 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2838 EVP_PKEY_free(s->s3->tmp.pkey);
2839 s->s3->tmp.pkey = NULL;
2841 EVP_PKEY_free(ckey);
2844 /* Should never happen */
2845 *al = SSL_AD_INTERNAL_ERROR;
2846 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2851 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt, int *al)
2853 #ifndef OPENSSL_NO_EC
2854 EVP_PKEY *skey = s->s3->tmp.pkey;
2855 EVP_PKEY *ckey = NULL;
2858 if (PACKET_remaining(pkt) == 0L) {
2859 /* We don't support ECDH client auth */
2860 *al = SSL_AD_HANDSHAKE_FAILURE;
2861 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_MISSING_TMP_ECDH_KEY);
2865 const unsigned char *data;
2868 * Get client's public key from encoded point in the
2869 * ClientKeyExchange message.
2872 /* Get encoded point length */
2873 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
2874 || PACKET_remaining(pkt) != 0) {
2875 *al = SSL_AD_DECODE_ERROR;
2876 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_LENGTH_MISMATCH);
2879 ckey = EVP_PKEY_new();
2880 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
2881 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EVP_LIB);
2884 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
2885 *al = SSL_AD_ILLEGAL_PARAMETER;
2886 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EC_LIB);
2891 if (ssl_derive(s, skey, ckey, 1) == 0) {
2892 *al = SSL_AD_INTERNAL_ERROR;
2893 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2898 EVP_PKEY_free(s->s3->tmp.pkey);
2899 s->s3->tmp.pkey = NULL;
2901 EVP_PKEY_free(ckey);
2905 /* Should never happen */
2906 *al = SSL_AD_INTERNAL_ERROR;
2907 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2912 static int tls_process_cke_srp(SSL *s, PACKET *pkt, int *al)
2914 #ifndef OPENSSL_NO_SRP
2916 const unsigned char *data;
2918 if (!PACKET_get_net_2(pkt, &i)
2919 || !PACKET_get_bytes(pkt, &data, i)) {
2920 *al = SSL_AD_DECODE_ERROR;
2921 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_A_LENGTH);
2924 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2925 *al = SSL_AD_INTERNAL_ERROR;
2926 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_BN_LIB);
2929 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
2930 *al = SSL_AD_ILLEGAL_PARAMETER;
2931 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
2934 OPENSSL_free(s->session->srp_username);
2935 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2936 if (s->session->srp_username == NULL) {
2937 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_MALLOC_FAILURE);
2941 if (!srp_generate_server_master_secret(s)) {
2942 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2948 /* Should never happen */
2949 *al = SSL_AD_INTERNAL_ERROR;
2950 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2955 static int tls_process_cke_gost(SSL *s, PACKET *pkt, int *al)
2957 #ifndef OPENSSL_NO_GOST
2958 EVP_PKEY_CTX *pkey_ctx;
2959 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2960 unsigned char premaster_secret[32];
2961 const unsigned char *start;
2962 size_t outlen = 32, inlen;
2963 unsigned long alg_a;
2966 size_t sess_key_len;
2967 const unsigned char *data;
2970 /* Get our certificate private key */
2971 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2972 if (alg_a & SSL_aGOST12) {
2974 * New GOST ciphersuites have SSL_aGOST01 bit too
2976 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2978 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2981 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2983 } else if (alg_a & SSL_aGOST01) {
2984 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2987 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2988 if (pkey_ctx == NULL) {
2989 *al = SSL_AD_INTERNAL_ERROR;
2990 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_MALLOC_FAILURE);
2993 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2994 *al = SSL_AD_INTERNAL_ERROR;
2995 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2999 * If client certificate is present and is of the same type, maybe
3000 * use it for key exchange. Don't mind errors from
3001 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3002 * client certificate for authorization only.
3004 client_pub_pkey = X509_get0_pubkey(s->session->peer);
3005 if (client_pub_pkey) {
3006 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3009 /* Decrypt session key */
3010 sess_key_len = PACKET_remaining(pkt);
3011 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
3012 *al = SSL_AD_INTERNAL_ERROR;
3013 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
3016 /* TODO(size_t): Convert this function */
3017 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
3018 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
3019 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
3020 *al = SSL_AD_DECODE_ERROR;
3021 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
3026 if (EVP_PKEY_decrypt
3027 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
3028 *al = SSL_AD_DECODE_ERROR;
3029 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
3032 /* Generate master secret */
3033 if (!ssl_generate_master_secret(s, premaster_secret,
3034 sizeof(premaster_secret), 0)) {
3035 *al = SSL_AD_INTERNAL_ERROR;
3036 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
3039 /* Check if pubkey from client certificate was used */
3040 if (EVP_PKEY_CTX_ctrl
3041 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
3042 s->statem.no_cert_verify = 1;
3046 EVP_PKEY_CTX_free(pkey_ctx);
3049 /* Should never happen */
3050 *al = SSL_AD_INTERNAL_ERROR;
3051 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
3056 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3059 unsigned long alg_k;
3061 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3063 /* For PSK parse and retrieve identity, obtain PSK key */
3064 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt, &al))
3067 if (alg_k & SSL_kPSK) {
3068 /* Identity extracted earlier: should be nothing left */
3069 if (PACKET_remaining(pkt) != 0) {
3070 al = SSL_AD_DECODE_ERROR;
3071 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3072 SSL_R_LENGTH_MISMATCH);
3075 /* PSK handled by ssl_generate_master_secret */
3076 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3077 al = SSL_AD_INTERNAL_ERROR;
3078 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3081 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3082 if (!tls_process_cke_rsa(s, pkt, &al))
3084 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3085 if (!tls_process_cke_dhe(s, pkt, &al))
3087 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3088 if (!tls_process_cke_ecdhe(s, pkt, &al))
3090 } else if (alg_k & SSL_kSRP) {
3091 if (!tls_process_cke_srp(s, pkt, &al))
3093 } else if (alg_k & SSL_kGOST) {
3094 if (!tls_process_cke_gost(s, pkt, &al))
3097 al = SSL_AD_INTERNAL_ERROR;
3098 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3099 SSL_R_UNKNOWN_CIPHER_TYPE);
3103 return MSG_PROCESS_CONTINUE_PROCESSING;
3106 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3107 #ifndef OPENSSL_NO_PSK
3108 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
3109 s->s3->tmp.psk = NULL;
3111 ossl_statem_set_error(s);
3112 return MSG_PROCESS_ERROR;
3115 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
3117 #ifndef OPENSSL_NO_SCTP
3118 if (wst == WORK_MORE_A) {
3119 if (SSL_IS_DTLS(s)) {
3120 unsigned char sctpauthkey[64];
3121 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3123 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3126 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3127 sizeof(DTLS1_SCTP_AUTH_LABEL));
3129 if (SSL_export_keying_material(s, sctpauthkey,
3130 sizeof(sctpauthkey), labelbuffer,
3131 sizeof(labelbuffer), NULL, 0,
3133 ossl_statem_set_error(s);
3137 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3138 sizeof(sctpauthkey), sctpauthkey);
3143 if (s->statem.no_cert_verify || !s->session->peer) {
3145 * No certificate verify or no peer certificate so we no longer need
3146 * the handshake_buffer
3148 if (!ssl3_digest_cached_records(s, 0)) {
3149 ossl_statem_set_error(s);
3152 return WORK_FINISHED_CONTINUE;
3154 if (!s->s3->handshake_buffer) {
3155 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3156 ERR_R_INTERNAL_ERROR);
3157 ossl_statem_set_error(s);
3161 * For sigalgs freeze the handshake buffer. If we support
3162 * extms we've done this already so this is a no-op
3164 if (!ssl3_digest_cached_records(s, 1)) {
3165 ossl_statem_set_error(s);
3170 return WORK_FINISHED_CONTINUE;
3173 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3175 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
3177 unsigned long l, llen;
3178 const unsigned char *certstart, *certbytes;
3179 STACK_OF(X509) *sk = NULL;
3180 PACKET spkt, context;
3183 if ((sk = sk_X509_new_null()) == NULL) {
3184 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3188 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3189 if ((SSL_IS_TLS13(s) && !PACKET_get_length_prefixed_1(pkt, &context))
3190 || !PACKET_get_net_3(pkt, &llen)
3191 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3192 || PACKET_remaining(pkt) != 0) {
3193 al = SSL_AD_DECODE_ERROR;
3194 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3198 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3199 if (!PACKET_get_net_3(&spkt, &l)
3200 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3201 al = SSL_AD_DECODE_ERROR;
3202 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3203 SSL_R_CERT_LENGTH_MISMATCH);
3207 certstart = certbytes;
3208 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3210 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3213 if (certbytes != (certstart + l)) {
3214 al = SSL_AD_DECODE_ERROR;
3215 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3216 SSL_R_CERT_LENGTH_MISMATCH);
3220 if (SSL_IS_TLS13(s)) {
3221 RAW_EXTENSION *rawexts = NULL;
3224 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3225 al = SSL_AD_DECODE_ERROR;
3226 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_BAD_LENGTH);
3229 if (!tls_collect_extensions(s, &extensions,
3230 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
3231 &al, NULL, chainidx == 0)
3232 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3233 rawexts, x, chainidx, &al,
3234 PACKET_remaining(&spkt) == 0)) {
3235 OPENSSL_free(rawexts);
3238 OPENSSL_free(rawexts);
3241 if (!sk_X509_push(sk, x)) {
3242 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3248 if (sk_X509_num(sk) <= 0) {
3249 /* TLS does not mind 0 certs returned */
3250 if (s->version == SSL3_VERSION) {
3251 al = SSL_AD_HANDSHAKE_FAILURE;
3252 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3253 SSL_R_NO_CERTIFICATES_RETURNED);
3256 /* Fail for TLS only if we required a certificate */
3257 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3258 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3259 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3260 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3261 al = SSL_AD_CERTIFICATE_REQUIRED;
3264 /* No client certificate so digest cached records */
3265 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3270 i = ssl_verify_cert_chain(s, sk);
3272 al = ssl_verify_alarm_type(s->verify_result);
3273 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3274 SSL_R_CERTIFICATE_VERIFY_FAILED);
3278 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3279 al = SSL_AD_HANDSHAKE_FAILURE;
3282 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3284 al = SSL3_AD_HANDSHAKE_FAILURE;
3285 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3286 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3291 X509_free(s->session->peer);
3292 s->session->peer = sk_X509_shift(sk);
3293 s->session->verify_result = s->verify_result;
3295 sk_X509_pop_free(s->session->peer_chain, X509_free);
3296 s->session->peer_chain = sk;
3299 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3302 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3303 al = SSL_AD_INTERNAL_ERROR;
3304 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3309 * Inconsistency alert: cert_chain does *not* include the peer's own
3310 * certificate, while we do include it in statem_clnt.c
3314 /* Save the current hash state for when we receive the CertificateVerify */
3316 && !ssl_handshake_hash(s, s->cert_verify_hash,
3317 sizeof(s->cert_verify_hash),
3318 &s->cert_verify_hash_len)) {
3319 al = SSL_AD_INTERNAL_ERROR;
3320 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3324 ret = MSG_PROCESS_CONTINUE_READING;
3328 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3329 ossl_statem_set_error(s);
3332 sk_X509_pop_free(sk, X509_free);
3336 int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3338 CERT_PKEY *cpk = s->s3->tmp.cert;
3339 int al = SSL_AD_INTERNAL_ERROR;
3342 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3347 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3348 * for the server Certificate message
3350 if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0))
3351 || !ssl3_output_cert_chain(s, pkt, cpk, &al)) {
3352 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3353 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3360 int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3362 unsigned char *senc = NULL;
3363 EVP_CIPHER_CTX *ctx = NULL;
3364 HMAC_CTX *hctx = NULL;
3365 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3366 const unsigned char *const_p;
3367 int len, slen_full, slen, lenfinal;
3370 SSL_CTX *tctx = s->session_ctx;
3371 unsigned char iv[EVP_MAX_IV_LENGTH];
3372 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3373 int iv_len, al = SSL_AD_INTERNAL_ERROR;
3374 size_t macoffset, macendoffset;
3376 unsigned char age_add_c[sizeof(uint32_t)];
3380 if (SSL_IS_TLS13(s)) {
3381 if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0)
3383 s->session->ext.tick_age_add = age_add_u.age_add;
3384 s->session->time = (long)time(NULL);
3385 if (s->s3->alpn_selected != NULL) {
3386 OPENSSL_free(s->session->ext.alpn_selected);
3387 s->session->ext.alpn_selected =
3388 OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
3389 if (s->session->ext.alpn_selected == NULL) {
3390 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3391 ERR_R_MALLOC_FAILURE);
3394 s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
3396 s->session->ext.max_early_data = s->max_early_data;
3399 /* get session encoding length */
3400 slen_full = i2d_SSL_SESSION(s->session, NULL);
3402 * Some length values are 16 bits, so forget it if session is too
3405 if (slen_full == 0 || slen_full > 0xFF00) {
3406 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3409 senc = OPENSSL_malloc(slen_full);
3411 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3415 ctx = EVP_CIPHER_CTX_new();
3416 hctx = HMAC_CTX_new();
3417 if (ctx == NULL || hctx == NULL) {
3418 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3423 if (!i2d_SSL_SESSION(s->session, &p))
3427 * create a fresh copy (not shared with other threads) to clean up
3430 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3433 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3435 slen = i2d_SSL_SESSION(sess, NULL);
3436 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3437 SSL_SESSION_free(sess);
3441 if (!i2d_SSL_SESSION(sess, &p)) {
3442 SSL_SESSION_free(sess);
3445 SSL_SESSION_free(sess);
3448 * Initialize HMAC and cipher contexts. If callback present it does
3449 * all the work otherwise use generated values from parent ctx.
3451 if (tctx->ext.ticket_key_cb) {
3452 /* if 0 is returned, write an empty ticket */
3453 int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3458 /* Put timeout and length */
3459 if (!WPACKET_put_bytes_u32(pkt, 0)
3460 || !WPACKET_put_bytes_u16(pkt, 0)) {
3461 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3462 ERR_R_INTERNAL_ERROR);
3466 EVP_CIPHER_CTX_free(ctx);
3467 HMAC_CTX_free(hctx);
3472 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3474 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3476 iv_len = EVP_CIPHER_iv_length(cipher);
3477 if (RAND_bytes(iv, iv_len) <= 0)
3479 if (!EVP_EncryptInit_ex(ctx, cipher, NULL,