2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 #include "../ssl_locl.h"
15 #include "statem_locl.h"
16 #include <openssl/buffer.h>
17 #include <openssl/objects.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509.h>
22 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
23 * SSL3_RT_CHANGE_CIPHER_SPEC)
25 int ssl3_do_write(SSL *s, int type)
30 ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
31 s->init_num, &written);
34 if (type == SSL3_RT_HANDSHAKE)
36 * should not be done for 'Hello Request's, but in that case we'll
37 * ignore the result anyway
39 if (!ssl3_finish_mac(s,
40 (unsigned char *)&s->init_buf->data[s->init_off],
44 if (written == s->init_num) {
46 s->msg_callback(1, s->version, type, s->init_buf->data,
47 (size_t)(s->init_off + s->init_num), s,
51 s->init_off += written;
52 s->init_num -= written;
56 int tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
60 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
61 || !WPACKET_get_length(pkt, &msglen)
64 s->init_num = (int)msglen;
70 int tls_setup_handshake(SSL *s)
72 if (!ssl3_init_finished_mac(s))
75 /* Reset any extension flags */
76 memset(s->ext.extflags, 0, sizeof(s->ext.extflags));
79 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(s);
80 int i, ver_min, ver_max, ok = 0;
83 * Sanity check that the maximum version we accept has ciphers
84 * enabled. For clients we do this check during construction of the
87 if (ssl_get_min_max_version(s, &ver_min, &ver_max) != 0) {
88 SSLerr(SSL_F_TLS_SETUP_HANDSHAKE, ERR_R_INTERNAL_ERROR);
89 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
92 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
93 const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
96 if (DTLS_VERSION_GE(ver_max, c->min_dtls) &&
97 DTLS_VERSION_LE(ver_max, c->max_dtls))
99 } else if (ver_max >= c->min_tls && ver_max <= c->max_tls) {
106 SSLerr(SSL_F_TLS_SETUP_HANDSHAKE, SSL_R_NO_CIPHERS_AVAILABLE);
107 ERR_add_error_data(1, "No ciphers enabled for max supported "
109 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
112 if (SSL_IS_FIRST_HANDSHAKE(s)) {
113 s->ctx->stats.sess_accept++;
114 } else if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
115 /* Renegotiation is disabled */
116 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
118 } else if (!s->s3->send_connection_binding &&
120 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
122 * Server attempting to renegotiate with client that doesn't
123 * support secure renegotiation.
125 SSLerr(SSL_F_TLS_SETUP_HANDSHAKE,
126 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
127 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
130 s->ctx->stats.sess_accept_renegotiate++;
132 s->s3->tmp.cert_request = 0;
135 if (SSL_IS_FIRST_HANDSHAKE(s))
136 s->ctx->stats.sess_connect++;
138 s->ctx->stats.sess_connect_renegotiate++;
140 /* mark client_random uninitialized */
141 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
144 s->s3->tmp.cert_req = 0;
147 s->statem.use_timer = 1;
154 * Size of the to-be-signed TLS13 data, without the hash size itself:
155 * 64 bytes of value 32, 33 context bytes, 1 byte separator
157 #define TLS13_TBS_START_SIZE 64
158 #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
160 static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs,
161 void **hdata, size_t *hdatalen)
163 static const char *servercontext = "TLS 1.3, server CertificateVerify";
164 static const char *clientcontext = "TLS 1.3, client CertificateVerify";
166 if (SSL_IS_TLS13(s)) {
169 /* Set the first 64 bytes of to-be-signed data to octet 32 */
170 memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
171 /* This copies the 33 bytes of context plus the 0 separator byte */
172 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
173 || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
174 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
176 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
179 * If we're currently reading then we need to use the saved handshake
180 * hash value. We can't use the current handshake hash state because
181 * that includes the CertVerify itself.
183 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
184 || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
185 memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
186 s->cert_verify_hash_len);
187 hashlen = s->cert_verify_hash_len;
188 } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
189 EVP_MAX_MD_SIZE, &hashlen)) {
194 *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
198 retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata);
207 int tls_construct_cert_verify(SSL *s, WPACKET *pkt)
209 EVP_PKEY *pkey = NULL;
210 const EVP_MD *md = NULL;
211 EVP_MD_CTX *mctx = NULL;
212 EVP_PKEY_CTX *pctx = NULL;
213 size_t hdatalen = 0, siglen = 0;
215 unsigned char *sig = NULL;
216 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
217 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
219 if (lu == NULL || s->s3->tmp.cert == NULL) {
220 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
223 pkey = s->s3->tmp.cert->privatekey;
225 if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
226 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
230 mctx = EVP_MD_CTX_new();
232 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
236 /* Get the data to be signed */
237 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
238 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
242 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
243 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
246 siglen = EVP_PKEY_size(pkey);
247 sig = OPENSSL_malloc(siglen);
249 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
253 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0) {
254 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
258 if (lu->sig == EVP_PKEY_RSA_PSS) {
259 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
260 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
261 RSA_PSS_SALTLEN_DIGEST) <= 0) {
262 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
266 if (s->version == SSL3_VERSION) {
267 if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0
268 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
269 (int)s->session->master_key_length,
270 s->session->master_key)
271 || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
273 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
276 } else if (EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) {
277 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
281 #ifndef OPENSSL_NO_GOST
283 int pktype = lu->sig;
285 if (pktype == NID_id_GostR3410_2001
286 || pktype == NID_id_GostR3410_2012_256
287 || pktype == NID_id_GostR3410_2012_512)
288 BUF_reverse(sig, NULL, siglen);
292 if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
293 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
297 /* Digest cached records and discard handshake buffer */
298 if (!ssl3_digest_cached_records(s, 0))
302 EVP_MD_CTX_free(mctx);
306 EVP_MD_CTX_free(mctx);
307 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
311 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
313 EVP_PKEY *pkey = NULL;
314 const unsigned char *data;
315 #ifndef OPENSSL_NO_GOST
316 unsigned char *gost_data = NULL;
318 int al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
322 const EVP_MD *md = NULL;
325 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
326 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
327 EVP_PKEY_CTX *pctx = NULL;
330 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
334 peer = s->session->peer;
335 pkey = X509_get0_pubkey(peer);
339 type = X509_certificate_type(peer, pkey);
341 if (!(type & EVP_PKT_SIGN)) {
342 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY,
343 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
344 al = SSL_AD_ILLEGAL_PARAMETER;
348 if (SSL_USE_SIGALGS(s)) {
352 if (!PACKET_get_net_2(pkt, &sigalg)) {
353 al = SSL_AD_DECODE_ERROR;
356 rv = tls12_check_peer_sigalg(s, sigalg, pkey);
359 } else if (rv == 0) {
360 al = SSL_AD_DECODE_ERROR;
364 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
366 } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
367 al = SSL_AD_INTERNAL_ERROR;
371 if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) {
372 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
373 al = SSL_AD_INTERNAL_ERROR;
377 /* Check for broken implementations of GOST ciphersuites */
379 * If key is GOST and len is exactly 64 or 128, it is signature without
380 * length field (CryptoPro implementations at least till TLS 1.2)
382 #ifndef OPENSSL_NO_GOST
383 if (!SSL_USE_SIGALGS(s)
384 && ((PACKET_remaining(pkt) == 64
385 && (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001
386 || EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256))
387 || (PACKET_remaining(pkt) == 128
388 && EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) {
389 len = PACKET_remaining(pkt);
392 if (!PACKET_get_net_2(pkt, &len)) {
393 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
394 al = SSL_AD_DECODE_ERROR;
398 j = EVP_PKEY_size(pkey);
399 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
400 || (PACKET_remaining(pkt) == 0)) {
401 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
402 al = SSL_AD_DECODE_ERROR;
405 if (!PACKET_get_bytes(pkt, &data, len)) {
406 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
407 al = SSL_AD_DECODE_ERROR;
411 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
412 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
417 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
419 if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) {
420 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
423 #ifndef OPENSSL_NO_GOST
425 int pktype = EVP_PKEY_id(pkey);
426 if (pktype == NID_id_GostR3410_2001
427 || pktype == NID_id_GostR3410_2012_256
428 || pktype == NID_id_GostR3410_2012_512) {
429 if ((gost_data = OPENSSL_malloc(len)) == NULL) {
430 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
433 BUF_reverse(gost_data, data, len);
439 if (SSL_USE_PSS(s)) {
440 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
441 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
442 RSA_PSS_SALTLEN_DIGEST) <= 0) {
443 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
447 if (s->version == SSL3_VERSION) {
448 if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
449 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
450 (int)s->session->master_key_length,
451 s->session->master_key)) {
452 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
455 if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
456 al = SSL_AD_DECRYPT_ERROR;
457 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
461 j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
463 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
466 al = SSL_AD_DECRYPT_ERROR;
467 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
472 ret = MSG_PROCESS_CONTINUE_READING;
475 ssl3_send_alert(s, SSL3_AL_FATAL, al);
476 ossl_statem_set_error(s);
478 BIO_free(s->s3->handshake_buffer);
479 s->s3->handshake_buffer = NULL;
480 EVP_MD_CTX_free(mctx);
481 #ifndef OPENSSL_NO_GOST
482 OPENSSL_free(gost_data);
487 int tls_construct_finished(SSL *s, WPACKET *pkt)
489 size_t finish_md_len;
493 /* This is a real handshake so make sure we clean it up at the end */
495 s->statem.cleanuphand = 1;
498 * We only change the keys if we didn't already do this when we sent the
503 && s->s3->tmp.cert_req == 0
504 && (!s->method->ssl3_enc->change_cipher_state(s,
505 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
506 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
511 sender = s->method->ssl3_enc->server_finished_label;
512 slen = s->method->ssl3_enc->server_finished_label_len;
514 sender = s->method->ssl3_enc->client_finished_label;
515 slen = s->method->ssl3_enc->client_finished_label_len;
518 finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
520 s->s3->tmp.finish_md);
521 if (finish_md_len == 0) {
522 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
526 s->s3->tmp.finish_md_len = finish_md_len;
528 if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) {
529 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
534 * Log the master secret, if logging is enabled. We don't log it for
535 * TLSv1.3: there's a different key schedule for that.
537 if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL,
538 s->session->master_key,
539 s->session->master_key_length)) {
540 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
545 * Copy the finished so we can use it for renegotiation checks
547 if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) {
548 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
552 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md,
554 s->s3->previous_client_finished_len = finish_md_len;
556 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md,
558 s->s3->previous_server_finished_len = finish_md_len;
563 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
567 int tls_construct_key_update(SSL *s, WPACKET *pkt)
569 if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
570 SSLerr(SSL_F_TLS_CONSTRUCT_KEY_UPDATE, ERR_R_INTERNAL_ERROR);
574 s->key_update = SSL_KEY_UPDATE_NONE;
578 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
582 MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt)
585 unsigned int updatetype;
587 s->key_update_count++;
588 if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) {
589 al = SSL_AD_ILLEGAL_PARAMETER;
590 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_TOO_MANY_KEY_UPDATES);
595 * A KeyUpdate message signals a key change so the end of the message must
596 * be on a record boundary.
598 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
599 al = SSL_AD_UNEXPECTED_MESSAGE;
600 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_NOT_ON_RECORD_BOUNDARY);
604 if (!PACKET_get_1(pkt, &updatetype)
605 || PACKET_remaining(pkt) != 0) {
606 al = SSL_AD_DECODE_ERROR;
607 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_BAD_KEY_UPDATE);
612 * There are only two defined key update types. Fail if we get a value we
615 if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
616 && updatetype != SSL_KEY_UPDATE_REQUESTED) {
617 al = SSL_AD_ILLEGAL_PARAMETER;
618 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_BAD_KEY_UPDATE);
623 * If we get a request for us to update our sending keys too then, we need
624 * to additionally send a KeyUpdate message. However that message should
625 * not also request an update (otherwise we get into an infinite loop).
627 if (updatetype == SSL_KEY_UPDATE_REQUESTED)
628 s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
630 if (!tls13_update_key(s, 0)) {
631 al = SSL_AD_INTERNAL_ERROR;
632 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, ERR_R_INTERNAL_ERROR);
636 return MSG_PROCESS_FINISHED_READING;
638 ssl3_send_alert(s, SSL3_AL_FATAL, al);
639 ossl_statem_set_error(s);
640 return MSG_PROCESS_ERROR;
643 #ifndef OPENSSL_NO_NEXTPROTONEG
645 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
648 static void ssl3_take_mac(SSL *s)
653 * If no new cipher setup return immediately: other functions will set
654 * the appropriate error.
656 if (s->s3->tmp.new_cipher == NULL)
659 sender = s->method->ssl3_enc->server_finished_label;
660 slen = s->method->ssl3_enc->server_finished_label_len;
662 sender = s->method->ssl3_enc->client_finished_label;
663 slen = s->method->ssl3_enc->client_finished_label_len;
666 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
669 s->s3->tmp.peer_finish_md);
673 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
678 remain = PACKET_remaining(pkt);
680 * 'Change Cipher Spec' is just a single byte, which should already have
681 * been consumed by ssl_get_message() so there should be no bytes left,
682 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
684 if (SSL_IS_DTLS(s)) {
685 if ((s->version == DTLS1_BAD_VER
686 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
687 || (s->version != DTLS1_BAD_VER
688 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
689 al = SSL_AD_DECODE_ERROR;
690 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
691 SSL_R_BAD_CHANGE_CIPHER_SPEC);
696 al = SSL_AD_DECODE_ERROR;
697 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
698 SSL_R_BAD_CHANGE_CIPHER_SPEC);
703 /* Check we have a cipher to change to */
704 if (s->s3->tmp.new_cipher == NULL) {
705 al = SSL_AD_UNEXPECTED_MESSAGE;
706 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
710 s->s3->change_cipher_spec = 1;
711 if (!ssl3_do_change_cipher_spec(s)) {
712 al = SSL_AD_INTERNAL_ERROR;
713 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
717 if (SSL_IS_DTLS(s)) {
718 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
720 if (s->version == DTLS1_BAD_VER)
721 s->d1->handshake_read_seq++;
723 #ifndef OPENSSL_NO_SCTP
725 * Remember that a CCS has been received, so that an old key of
726 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
729 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
733 return MSG_PROCESS_CONTINUE_READING;
735 ssl3_send_alert(s, SSL3_AL_FATAL, al);
736 ossl_statem_set_error(s);
737 return MSG_PROCESS_ERROR;
740 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
742 int al = SSL_AD_INTERNAL_ERROR;
746 /* This is a real handshake so make sure we clean it up at the end */
748 s->statem.cleanuphand = 1;
751 * In TLSv1.3 a Finished message signals a key change so the end of the
752 * message must be on a record boundary.
754 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
755 al = SSL_AD_UNEXPECTED_MESSAGE;
756 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_NOT_ON_RECORD_BOUNDARY);
760 /* If this occurs, we have missed a message */
761 if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) {
762 al = SSL_AD_UNEXPECTED_MESSAGE;
763 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
766 s->s3->change_cipher_spec = 0;
768 md_len = s->s3->tmp.peer_finish_md_len;
770 if (md_len != PACKET_remaining(pkt)) {
771 al = SSL_AD_DECODE_ERROR;
772 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
776 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md,
778 al = SSL_AD_DECRYPT_ERROR;
779 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
784 * Copy the finished so we can use it for renegotiation checks
786 if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) {
787 al = SSL_AD_INTERNAL_ERROR;
788 SSLerr(SSL_F_TLS_PROCESS_FINISHED, ERR_R_INTERNAL_ERROR);
792 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md,
794 s->s3->previous_client_finished_len = md_len;
796 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md,
798 s->s3->previous_server_finished_len = md_len;
802 * In TLS1.3 we also have to change cipher state and do any final processing
803 * of the initial server flight (if we are a client)
805 if (SSL_IS_TLS13(s)) {
807 if (!s->method->ssl3_enc->change_cipher_state(s,
808 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
809 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
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 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
819 if (!s->method->ssl3_enc->change_cipher_state(s,
820 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
821 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
824 if (!tls_process_initial_server_flight(s, &al))
829 return MSG_PROCESS_FINISHED_READING;
831 ssl3_send_alert(s, SSL3_AL_FATAL, al);
832 ossl_statem_set_error(s);
833 return MSG_PROCESS_ERROR;
836 int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
838 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
839 SSLerr(SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
840 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
847 /* Add a certificate to the WPACKET */
848 static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain,
852 unsigned char *outbytes;
854 len = i2d_X509(x, NULL);
856 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_BUF_LIB);
857 *al = SSL_AD_INTERNAL_ERROR;
860 if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
861 || i2d_X509(x, &outbytes) != len) {
862 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_INTERNAL_ERROR);
863 *al = SSL_AD_INTERNAL_ERROR;
868 && !tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_CERTIFICATE, x,
875 /* Add certificate chain to provided WPACKET */
876 static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al)
880 STACK_OF(X509) *extra_certs;
881 STACK_OF(X509) *chain = NULL;
882 X509_STORE *chain_store;
883 int tmpal = SSL_AD_INTERNAL_ERROR;
885 if (cpk == NULL || cpk->x509 == NULL)
891 * If we have a certificate specific chain use it, else use parent ctx.
893 if (cpk->chain != NULL)
894 extra_certs = cpk->chain;
896 extra_certs = s->ctx->extra_certs;
898 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
900 else if (s->cert->chain_store)
901 chain_store = s->cert->chain_store;
903 chain_store = s->ctx->cert_store;
905 if (chain_store != NULL) {
906 X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
908 if (xs_ctx == NULL) {
909 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
912 if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
913 X509_STORE_CTX_free(xs_ctx);
914 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
918 * It is valid for the chain not to be complete (because normally we
919 * don't include the root cert in the chain). Therefore we deliberately
920 * ignore the error return from this call. We're not actually verifying
921 * the cert - we're just building as much of the chain as we can
923 (void)X509_verify_cert(xs_ctx);
924 /* Don't leave errors in the queue */
926 chain = X509_STORE_CTX_get0_chain(xs_ctx);
927 i = ssl_security_cert_chain(s, chain, NULL, 0);
930 /* Dummy error calls so mkerr generates them */
931 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
932 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
933 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
935 X509_STORE_CTX_free(xs_ctx);
936 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
939 chain_count = sk_X509_num(chain);
940 for (i = 0; i < chain_count; i++) {
941 x = sk_X509_value(chain, i);
943 if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) {
944 X509_STORE_CTX_free(xs_ctx);
948 X509_STORE_CTX_free(xs_ctx);
950 i = ssl_security_cert_chain(s, extra_certs, x, 0);
952 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
955 if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal))
957 for (i = 0; i < sk_X509_num(extra_certs); i++) {
958 x = sk_X509_value(extra_certs, i);
959 if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal))
970 unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk,
973 int tmpal = SSL_AD_INTERNAL_ERROR;
975 if (!WPACKET_start_sub_packet_u24(pkt)
976 || !ssl_add_cert_chain(s, pkt, cpk, &tmpal)
977 || !WPACKET_close(pkt)) {
978 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR);
986 * Tidy up after the end of a handshake. In the case of SCTP this may result
987 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
990 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs)
992 void (*cb) (const SSL *ssl, int type, int val) = NULL;
994 #ifndef OPENSSL_NO_SCTP
995 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
997 ret = dtls_wait_for_dry(s);
998 if (ret != WORK_FINISHED_CONTINUE)
1004 if (!SSL_IS_DTLS(s)) {
1006 * We don't do this in DTLS because we may still need the init_buf
1007 * in case there are any unexpected retransmits
1009 BUF_MEM_free(s->init_buf);
1012 if (!ssl_free_wbio_buffer(s))
1017 if (s->statem.cleanuphand) {
1018 /* skipped if we just sent a HelloRequest */
1021 s->statem.cleanuphand = 0;
1023 ssl3_cleanup_key_block(s);
1026 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
1028 s->ctx->stats.sess_accept_good++;
1029 s->handshake_func = ossl_statem_accept;
1031 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
1033 s->ctx->stats.sess_hit++;
1035 s->handshake_func = ossl_statem_connect;
1036 s->ctx->stats.sess_connect_good++;
1039 if (s->info_callback != NULL)
1040 cb = s->info_callback;
1041 else if (s->ctx->info_callback != NULL)
1042 cb = s->ctx->info_callback;
1045 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
1047 if (SSL_IS_DTLS(s)) {
1048 /* done with handshaking */
1049 s->d1->handshake_read_seq = 0;
1050 s->d1->handshake_write_seq = 0;
1051 s->d1->next_handshake_write_seq = 0;
1052 dtls1_clear_received_buffer(s);
1057 * If we've not cleared the buffers its because we've got more work to do,
1061 return WORK_FINISHED_CONTINUE;
1063 ossl_statem_set_in_init(s, 0);
1064 return WORK_FINISHED_STOP;
1067 int tls_get_message_header(SSL *s, int *mt)
1069 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1070 int skip_message, i, recvd_type, al;
1072 size_t l, readbytes;
1074 p = (unsigned char *)s->init_buf->data;
1077 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1078 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
1080 SSL3_HM_HEADER_LENGTH - s->init_num,
1083 s->rwstate = SSL_READING;
1086 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1088 * A ChangeCipherSpec must be a single byte and may not occur
1089 * in the middle of a handshake message.
1091 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
1092 al = SSL_AD_UNEXPECTED_MESSAGE;
1093 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER,
1094 SSL_R_BAD_CHANGE_CIPHER_SPEC);
1097 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1098 s->init_num = readbytes - 1;
1099 s->init_msg = s->init_buf->data;
1100 s->s3->tmp.message_size = readbytes;
1102 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1103 al = SSL_AD_UNEXPECTED_MESSAGE;
1104 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY);
1107 s->init_num += readbytes;
1112 if (s->statem.hand_state != TLS_ST_OK
1113 && p[0] == SSL3_MT_HELLO_REQUEST)
1115 * The server may always send 'Hello Request' messages --
1116 * we are doing a handshake anyway now, so ignore them if
1117 * their format is correct. Does not count for 'Finished'
1120 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1124 if (s->msg_callback)
1125 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1126 p, SSL3_HM_HEADER_LENGTH, s,
1127 s->msg_callback_arg);
1129 } while (skip_message);
1130 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1133 s->s3->tmp.message_type = *(p++);
1135 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1137 * Only happens with SSLv3+ in an SSLv2 backward compatible
1140 * Total message size is the remaining record bytes to read
1141 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1143 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
1144 + SSL3_HM_HEADER_LENGTH;
1145 s->s3->tmp.message_size = l;
1147 s->init_msg = s->init_buf->data;
1148 s->init_num = SSL3_HM_HEADER_LENGTH;
1151 /* BUF_MEM_grow takes an 'int' parameter */
1152 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1153 al = SSL_AD_ILLEGAL_PARAMETER;
1154 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
1157 s->s3->tmp.message_size = l;
1159 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1165 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1169 int tls_get_message_body(SSL *s, size_t *len)
1171 size_t n, readbytes;
1175 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1176 /* We've already read everything in */
1177 *len = (unsigned long)s->init_num;
1182 n = s->s3->tmp.message_size - s->init_num;
1184 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
1185 &p[s->init_num], n, 0, &readbytes);
1187 s->rwstate = SSL_READING;
1191 s->init_num += readbytes;
1195 #ifndef OPENSSL_NO_NEXTPROTONEG
1197 * If receiving Finished, record MAC of prior handshake messages for
1198 * Finished verification.
1200 if (*s->init_buf->data == SSL3_MT_FINISHED)
1204 /* Feed this message into MAC computation. */
1205 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1206 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1208 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB);
1209 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1213 if (s->msg_callback)
1214 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1215 (size_t)s->init_num, s, s->msg_callback_arg);
1218 * We defer feeding in the HRR until later. We'll do it as part of
1219 * processing the message
1221 if (s->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST
1222 && !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1223 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1224 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB);
1225 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1229 if (s->msg_callback)
1230 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1231 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
1232 s->msg_callback_arg);
1239 int ssl_cert_type(const X509 *x, const EVP_PKEY *pk)
1241 if (pk == NULL && (pk = X509_get0_pubkey(x)) == NULL)
1244 switch (EVP_PKEY_id(pk)) {
1248 return SSL_PKEY_RSA;
1250 return SSL_PKEY_DSA_SIGN;
1251 #ifndef OPENSSL_NO_EC
1253 return SSL_PKEY_ECC;
1255 return SSL_PKEY_ED25519;
1257 #ifndef OPENSSL_NO_GOST
1258 case NID_id_GostR3410_2001:
1259 return SSL_PKEY_GOST01;
1260 case NID_id_GostR3410_2012_256:
1261 return SSL_PKEY_GOST12_256;
1262 case NID_id_GostR3410_2012_512:
1263 return SSL_PKEY_GOST12_512;
1268 int ssl_verify_alarm_type(long type)
1273 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1274 case X509_V_ERR_UNABLE_TO_GET_CRL:
1275 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1276 al = SSL_AD_UNKNOWN_CA;
1278 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1279 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1280 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1281 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1282 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1283 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1284 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1285 case X509_V_ERR_CERT_NOT_YET_VALID:
1286 case X509_V_ERR_CRL_NOT_YET_VALID:
1287 case X509_V_ERR_CERT_UNTRUSTED:
1288 case X509_V_ERR_CERT_REJECTED:
1289 case X509_V_ERR_HOSTNAME_MISMATCH:
1290 case X509_V_ERR_EMAIL_MISMATCH:
1291 case X509_V_ERR_IP_ADDRESS_MISMATCH:
1292 case X509_V_ERR_DANE_NO_MATCH:
1293 case X509_V_ERR_EE_KEY_TOO_SMALL:
1294 case X509_V_ERR_CA_KEY_TOO_SMALL:
1295 case X509_V_ERR_CA_MD_TOO_WEAK:
1296 al = SSL_AD_BAD_CERTIFICATE;
1298 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
1299 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
1300 al = SSL_AD_DECRYPT_ERROR;
1302 case X509_V_ERR_CERT_HAS_EXPIRED:
1303 case X509_V_ERR_CRL_HAS_EXPIRED:
1304 al = SSL_AD_CERTIFICATE_EXPIRED;
1306 case X509_V_ERR_CERT_REVOKED:
1307 al = SSL_AD_CERTIFICATE_REVOKED;
1309 case X509_V_ERR_UNSPECIFIED:
1310 case X509_V_ERR_OUT_OF_MEM:
1311 case X509_V_ERR_INVALID_CALL:
1312 case X509_V_ERR_STORE_LOOKUP:
1313 al = SSL_AD_INTERNAL_ERROR;
1315 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1316 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1317 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1318 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1319 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1320 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1321 case X509_V_ERR_INVALID_CA:
1322 al = SSL_AD_UNKNOWN_CA;
1324 case X509_V_ERR_APPLICATION_VERIFICATION:
1325 al = SSL_AD_HANDSHAKE_FAILURE;
1327 case X509_V_ERR_INVALID_PURPOSE:
1328 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1331 al = SSL_AD_CERTIFICATE_UNKNOWN;
1337 int ssl_allow_compression(SSL *s)
1339 if (s->options & SSL_OP_NO_COMPRESSION)
1341 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1344 static int version_cmp(const SSL *s, int a, int b)
1346 int dtls = SSL_IS_DTLS(s);
1351 return a < b ? -1 : 1;
1352 return DTLS_VERSION_LT(a, b) ? -1 : 1;
1357 const SSL_METHOD *(*cmeth) (void);
1358 const SSL_METHOD *(*smeth) (void);
1361 #if TLS_MAX_VERSION != TLS1_3_VERSION
1362 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1365 /* Must be in order high to low */
1366 static const version_info tls_version_table[] = {
1367 #ifndef OPENSSL_NO_TLS1_3
1368 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1370 {TLS1_3_VERSION, NULL, NULL},
1372 #ifndef OPENSSL_NO_TLS1_2
1373 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1375 {TLS1_2_VERSION, NULL, NULL},
1377 #ifndef OPENSSL_NO_TLS1_1
1378 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1380 {TLS1_1_VERSION, NULL, NULL},
1382 #ifndef OPENSSL_NO_TLS1
1383 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1385 {TLS1_VERSION, NULL, NULL},
1387 #ifndef OPENSSL_NO_SSL3
1388 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1390 {SSL3_VERSION, NULL, NULL},
1395 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1396 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1399 /* Must be in order high to low */
1400 static const version_info dtls_version_table[] = {
1401 #ifndef OPENSSL_NO_DTLS1_2
1402 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1404 {DTLS1_2_VERSION, NULL, NULL},
1406 #ifndef OPENSSL_NO_DTLS1
1407 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1408 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1410 {DTLS1_VERSION, NULL, NULL},
1411 {DTLS1_BAD_VER, NULL, NULL},
1417 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1419 * @s: The SSL handle for the candidate method
1420 * @method: the intended method.
1422 * Returns 0 on success, or an SSL error reason on failure.
1424 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
1426 int version = method->version;
1428 if ((s->min_proto_version != 0 &&
1429 version_cmp(s, version, s->min_proto_version) < 0) ||
1430 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1431 return SSL_R_VERSION_TOO_LOW;
1433 if (s->max_proto_version != 0 &&
1434 version_cmp(s, version, s->max_proto_version) > 0)
1435 return SSL_R_VERSION_TOO_HIGH;
1437 if ((s->options & method->mask) != 0)
1438 return SSL_R_UNSUPPORTED_PROTOCOL;
1439 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1440 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1446 * ssl_version_supported - Check that the specified `version` is supported by
1449 * @s: The SSL handle for the candidate method
1450 * @version: Protocol version to test against
1452 * Returns 1 when supported, otherwise 0
1454 int ssl_version_supported(const SSL *s, int version)
1456 const version_info *vent;
1457 const version_info *table;
1459 switch (s->method->version) {
1461 /* Version should match method version for non-ANY method */
1462 return version_cmp(s, version, s->version) == 0;
1463 case TLS_ANY_VERSION:
1464 table = tls_version_table;
1466 case DTLS_ANY_VERSION:
1467 table = dtls_version_table;
1472 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
1474 if (vent->cmeth != NULL &&
1475 version_cmp(s, version, vent->version) == 0 &&
1476 ssl_method_error(s, vent->cmeth()) == 0) {
1484 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1485 * fallback indication from a client check whether we're using the highest
1486 * supported protocol version.
1488 * @s server SSL handle.
1490 * Returns 1 when using the highest enabled version, 0 otherwise.
1492 int ssl_check_version_downgrade(SSL *s)
1494 const version_info *vent;
1495 const version_info *table;
1498 * Check that the current protocol is the highest enabled version
1499 * (according to s->ctx->method, as version negotiation may have changed
1502 if (s->version == s->ctx->method->version)
1506 * Apparently we're using a version-flexible SSL_METHOD (not at its
1507 * highest protocol version).
1509 if (s->ctx->method->version == TLS_method()->version)
1510 table = tls_version_table;
1511 else if (s->ctx->method->version == DTLS_method()->version)
1512 table = dtls_version_table;
1514 /* Unexpected state; fail closed. */
1518 for (vent = table; vent->version != 0; ++vent) {
1519 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
1520 return s->version == vent->version;
1526 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1527 * protocols, provided the initial (D)TLS method is version-flexible. This
1528 * function sanity-checks the proposed value and makes sure the method is
1529 * version-flexible, then sets the limit if all is well.
1531 * @method_version: The version of the current SSL_METHOD.
1532 * @version: the intended limit.
1533 * @bound: pointer to limit to be updated.
1535 * Returns 1 on success, 0 on failure.
1537 int ssl_set_version_bound(int method_version, int version, int *bound)
1545 * Restrict TLS methods to TLS protocol versions.
1546 * Restrict DTLS methods to DTLS protocol versions.
1547 * Note, DTLS version numbers are decreasing, use comparison macros.
1549 * Note that for both lower-bounds we use explicit versions, not
1550 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1551 * configurations. If the MIN (supported) version ever rises, the user's
1552 * "floor" remains valid even if no longer available. We don't expect the
1553 * MAX ceiling to ever get lower, so making that variable makes sense.
1555 switch (method_version) {
1558 * XXX For fixed version methods, should we always fail and not set any
1559 * bounds, always succeed and not set any bounds, or set the bounds and
1560 * arrange to fail later if they are not met? At present fixed-version
1561 * methods are not subject to controls that disable individual protocol
1566 case TLS_ANY_VERSION:
1567 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
1571 case DTLS_ANY_VERSION:
1572 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
1573 DTLS_VERSION_LT(version, DTLS1_BAD_VER))
1582 static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd)
1584 if (vers == TLS1_2_VERSION
1585 && ssl_version_supported(s, TLS1_3_VERSION)) {
1586 *dgrd = DOWNGRADE_TO_1_2;
1587 } else if (!SSL_IS_DTLS(s) && vers < TLS1_2_VERSION
1588 && (ssl_version_supported(s, TLS1_2_VERSION)
1589 || ssl_version_supported(s, TLS1_3_VERSION))) {
1590 *dgrd = DOWNGRADE_TO_1_1;
1592 *dgrd = DOWNGRADE_NONE;
1597 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1598 * client HELLO is received to select the final server protocol version and
1599 * the version specific method.
1601 * @s: server SSL handle.
1603 * Returns 0 on success or an SSL error reason number on failure.
1605 int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd)
1608 * With version-flexible methods we have an initial state with:
1610 * s->method->version == (D)TLS_ANY_VERSION,
1611 * s->version == (D)TLS_MAX_VERSION.
1613 * So we detect version-flexible methods via the method version, not the
1616 int server_version = s->method->version;
1617 int client_version = hello->legacy_version;
1618 const version_info *vent;
1619 const version_info *table;
1621 RAW_EXTENSION *suppversions;
1623 s->client_version = client_version;
1625 switch (server_version) {
1627 if (!SSL_IS_TLS13(s)) {
1628 if (version_cmp(s, client_version, s->version) < 0)
1629 return SSL_R_WRONG_SSL_VERSION;
1630 *dgrd = DOWNGRADE_NONE;
1632 * If this SSL handle is not from a version flexible method we don't
1633 * (and never did) check min/max FIPS or Suite B constraints. Hope
1634 * that's OK. It is up to the caller to not choose fixed protocol
1635 * versions they don't want. If not, then easy to fix, just return
1636 * ssl_method_error(s, s->method)
1641 * Fall through if we are TLSv1.3 already (this means we must be after
1642 * a HelloRetryRequest
1645 case TLS_ANY_VERSION:
1646 table = tls_version_table;
1648 case DTLS_ANY_VERSION:
1649 table = dtls_version_table;
1653 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1655 if (suppversions->present && !SSL_IS_DTLS(s)) {
1656 unsigned int candidate_vers = 0;
1657 unsigned int best_vers = 0;
1658 const SSL_METHOD *best_method = NULL;
1659 PACKET versionslist;
1661 suppversions->parsed = 1;
1663 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1664 /* Trailing or invalid data? */
1665 return SSL_R_LENGTH_MISMATCH;
1668 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1669 /* TODO(TLS1.3): Remove this before release */
1670 if (candidate_vers == TLS1_3_VERSION_DRAFT)
1671 candidate_vers = TLS1_3_VERSION;
1673 * TODO(TLS1.3): There is some discussion on the TLS list about
1674 * whether to ignore versions <TLS1.2 in supported_versions. At the
1675 * moment we honour them if present. To be reviewed later
1677 if (version_cmp(s, candidate_vers, best_vers) <= 0)
1680 vent->version != 0 && vent->version != (int)candidate_vers;
1683 if (vent->version != 0 && vent->smeth != NULL) {
1684 const SSL_METHOD *method;
1686 method = vent->smeth();
1687 if (ssl_method_error(s, method) == 0) {
1688 best_vers = candidate_vers;
1689 best_method = method;
1693 if (PACKET_remaining(&versionslist) != 0) {
1694 /* Trailing data? */
1695 return SSL_R_LENGTH_MISMATCH;
1698 if (best_vers > 0) {
1699 if (SSL_IS_TLS13(s)) {
1701 * We get here if this is after a HelloRetryRequest. In this
1702 * case we just check that we still negotiated TLSv1.3
1704 if (best_vers != TLS1_3_VERSION)
1705 return SSL_R_UNSUPPORTED_PROTOCOL;
1708 check_for_downgrade(s, best_vers, dgrd);
1709 s->version = best_vers;
1710 s->method = best_method;
1713 return SSL_R_UNSUPPORTED_PROTOCOL;
1717 * If the supported versions extension isn't present, then the highest
1718 * version we can negotiate is TLSv1.2
1720 if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1721 client_version = TLS1_2_VERSION;
1724 * No supported versions extension, so we just use the version supplied in
1727 for (vent = table; vent->version != 0; ++vent) {
1728 const SSL_METHOD *method;
1730 if (vent->smeth == NULL ||
1731 version_cmp(s, client_version, vent->version) < 0)
1733 method = vent->smeth();
1734 if (ssl_method_error(s, method) == 0) {
1735 check_for_downgrade(s, vent->version, dgrd);
1736 s->version = vent->version;
1742 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1746 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1747 * server HELLO is received to select the final client protocol version and
1748 * the version specific method.
1750 * @s: client SSL handle.
1751 * @version: The proposed version from the server's HELLO.
1752 * @checkdgrd: Whether to check the downgrade sentinels in the server_random
1753 * @al: Where to store any alert value that may be generated
1755 * Returns 0 on success or an SSL error reason number on failure.
1757 int ssl_choose_client_version(SSL *s, int version, int checkdgrd, int *al)
1759 const version_info *vent;
1760 const version_info *table;
1763 /* TODO(TLS1.3): Remove this before release */
1764 if (version == TLS1_3_VERSION_DRAFT)
1765 version = TLS1_3_VERSION;
1767 if (s->hello_retry_request && version != TLS1_3_VERSION) {
1768 *al = SSL_AD_PROTOCOL_VERSION;
1769 return SSL_R_WRONG_SSL_VERSION;
1772 switch (s->method->version) {
1774 if (version != s->version) {
1775 *al = SSL_AD_PROTOCOL_VERSION;
1776 return SSL_R_WRONG_SSL_VERSION;
1779 * If this SSL handle is not from a version flexible method we don't
1780 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1781 * that's OK. It is up to the caller to not choose fixed protocol
1782 * versions they don't want. If not, then easy to fix, just return
1783 * ssl_method_error(s, s->method)
1786 case TLS_ANY_VERSION:
1787 table = tls_version_table;
1789 case DTLS_ANY_VERSION:
1790 table = dtls_version_table;
1794 for (vent = table; vent->version != 0; ++vent) {
1795 const SSL_METHOD *method;
1798 if (vent->cmeth == NULL)
1801 if (highver != 0 && version != vent->version)
1804 method = vent->cmeth();
1805 err = ssl_method_error(s, method);
1807 if (version == vent->version) {
1808 *al = SSL_AD_PROTOCOL_VERSION;
1815 highver = vent->version;
1817 if (version != vent->version)
1820 #ifndef OPENSSL_NO_TLS13DOWNGRADE
1821 /* Check for downgrades */
1823 if (version == TLS1_2_VERSION && highver > version) {
1824 if (memcmp(tls12downgrade,
1825 s->s3->server_random + SSL3_RANDOM_SIZE
1826 - sizeof(tls12downgrade),
1827 sizeof(tls12downgrade)) == 0) {
1828 *al = SSL_AD_ILLEGAL_PARAMETER;
1829 return SSL_R_INAPPROPRIATE_FALLBACK;
1831 } else if (!SSL_IS_DTLS(s)
1832 && version < TLS1_2_VERSION
1833 && highver > version) {
1834 if (memcmp(tls11downgrade,
1835 s->s3->server_random + SSL3_RANDOM_SIZE
1836 - sizeof(tls11downgrade),
1837 sizeof(tls11downgrade)) == 0) {
1838 *al = SSL_AD_ILLEGAL_PARAMETER;
1839 return SSL_R_INAPPROPRIATE_FALLBACK;
1846 s->version = version;
1850 *al = SSL_AD_PROTOCOL_VERSION;
1851 return SSL_R_UNSUPPORTED_PROTOCOL;
1855 * ssl_get_min_max_version - get minimum and maximum protocol version
1856 * @s: The SSL connection
1857 * @min_version: The minimum supported version
1858 * @max_version: The maximum supported version
1860 * Work out what version we should be using for the initial ClientHello if the
1861 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1862 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1863 * constraints and any floor imposed by the security level here,
1864 * so we don't advertise the wrong protocol version to only reject the outcome later.
1866 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1867 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1868 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1870 * Returns 0 on success or an SSL error reason number on failure. On failure
1871 * min_version and max_version will also be set to 0.
1873 int ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version)
1877 const SSL_METHOD *single = NULL;
1878 const SSL_METHOD *method;
1879 const version_info *table;
1880 const version_info *vent;
1882 switch (s->method->version) {
1885 * If this SSL handle is not from a version flexible method we don't
1886 * (and never did) check min/max FIPS or Suite B constraints. Hope
1887 * that's OK. It is up to the caller to not choose fixed protocol
1888 * versions they don't want. If not, then easy to fix, just return
1889 * ssl_method_error(s, s->method)
1891 *min_version = *max_version = s->version;
1893 case TLS_ANY_VERSION:
1894 table = tls_version_table;
1896 case DTLS_ANY_VERSION:
1897 table = dtls_version_table;
1902 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1903 * below X enabled. This is required in order to maintain the "version
1904 * capability" vector contiguous. Any versions with a NULL client method
1905 * (protocol version client is disabled at compile-time) is also a "hole".
1907 * Our initial state is hole == 1, version == 0. That is, versions above
1908 * the first version in the method table are disabled (a "hole" above
1909 * the valid protocol entries) and we don't have a selected version yet.
1911 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1912 * the selected version, and the method becomes a candidate "single"
1913 * method. We're no longer in a hole, so "hole" becomes 0.
1915 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1916 * as we support a contiguous range of at least two methods. If we hit
1917 * a disabled method, then hole becomes true again, but nothing else
1918 * changes yet, because all the remaining methods may be disabled too.
1919 * If we again hit an enabled method after the new hole, it becomes
1920 * selected, as we start from scratch.
1922 *min_version = version = 0;
1924 for (vent = table; vent->version != 0; ++vent) {
1926 * A table entry with a NULL client method is still a hole in the
1927 * "version capability" vector.
1929 if (vent->cmeth == NULL) {
1933 method = vent->cmeth();
1934 if (ssl_method_error(s, method) != 0) {
1938 *min_version = method->version;
1940 version = (single = method)->version;
1941 *min_version = version;
1946 *max_version = version;
1948 /* Fail if everything is disabled */
1950 return SSL_R_NO_PROTOCOLS_AVAILABLE;
1956 * ssl_set_client_hello_version - Work out what version we should be using for
1957 * the initial ClientHello.legacy_version field.
1959 * @s: client SSL handle.
1961 * Returns 0 on success or an SSL error reason number on failure.
1963 int ssl_set_client_hello_version(SSL *s)
1965 int ver_min, ver_max, ret;
1967 ret = ssl_get_min_max_version(s, &ver_min, &ver_max);
1972 s->version = ver_max;
1974 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
1975 if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
1976 ver_max = TLS1_2_VERSION;
1978 s->client_version = ver_max;
1983 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
1984 * and |checkallow| is 1 then additionally check if the group is allowed to be
1985 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
1986 * 1) or 0 otherwise.
1988 #ifndef OPENSSL_NO_EC
1989 int check_in_list(SSL *s, unsigned int group_id, const unsigned char *groups,
1990 size_t num_groups, int checkallow)
1994 if (groups == NULL || num_groups == 0)
1997 for (i = 0; i < num_groups; i++, groups += 2) {
1998 if (group_id == GET_GROUP_ID(groups, 0)
2000 || tls_curve_allowed(s, groups, SSL_SECOP_CURVE_CHECK))) {
2009 /* Replace ClientHello1 in the transcript hash with a synthetic message */
2010 int create_synthetic_message_hash(SSL *s)
2012 unsigned char hashval[EVP_MAX_MD_SIZE];
2014 unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
2016 memset(msghdr, 0, sizeof(msghdr));
2018 /* Get the hash of the initial ClientHello */
2019 if (!ssl3_digest_cached_records(s, 0)
2020 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
2021 SSLerr(SSL_F_CREATE_SYNTHETIC_MESSAGE_HASH, ERR_R_INTERNAL_ERROR);
2025 /* Reinitialise the transcript hash */
2026 if (!ssl3_init_finished_mac(s))
2029 /* Inject the synthetic message_hash message */
2030 msghdr[0] = SSL3_MT_MESSAGE_HASH;
2031 msghdr[SSL3_HM_HEADER_LENGTH - 1] = hashlen;
2032 if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
2033 || !ssl3_finish_mac(s, hashval, hashlen)) {
2034 SSLerr(SSL_F_CREATE_SYNTHETIC_MESSAGE_HASH, ERR_R_INTERNAL_ERROR);
2041 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2043 return X509_NAME_cmp(*a, *b);
2046 int parse_ca_names(SSL *s, PACKET *pkt, int *al)
2048 STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
2049 X509_NAME *xn = NULL;
2052 if (ca_sk == NULL) {
2053 SSLerr(SSL_F_PARSE_CA_NAMES, ERR_R_MALLOC_FAILURE);
2056 /* get the CA RDNs */
2057 if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
2058 *al = SSL_AD_DECODE_ERROR;
2059 SSLerr(SSL_F_PARSE_CA_NAMES, SSL_R_LENGTH_MISMATCH);
2063 while (PACKET_remaining(&cadns)) {
2064 const unsigned char *namestart, *namebytes;
2065 unsigned int name_len;
2067 if (!PACKET_get_net_2(&cadns, &name_len)
2068 || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
2069 SSLerr(SSL_F_PARSE_CA_NAMES, SSL_R_LENGTH_MISMATCH);
2073 namestart = namebytes;
2074 if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
2075 SSLerr(SSL_F_PARSE_CA_NAMES, ERR_R_ASN1_LIB);
2078 if (namebytes != (namestart + name_len)) {
2079 SSLerr(SSL_F_PARSE_CA_NAMES, SSL_R_CA_DN_LENGTH_MISMATCH);
2083 if (!sk_X509_NAME_push(ca_sk, xn)) {
2084 SSLerr(SSL_F_PARSE_CA_NAMES, ERR_R_MALLOC_FAILURE);
2085 *al = SSL_AD_INTERNAL_ERROR;
2091 sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
2092 s->s3->tmp.peer_ca_names = ca_sk;
2097 *al = SSL_AD_DECODE_ERROR;
2099 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2104 int construct_ca_names(SSL *s, WPACKET *pkt)
2106 const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
2108 /* Start sub-packet for client CA list */
2109 if (!WPACKET_start_sub_packet_u16(pkt))
2112 if (ca_sk != NULL) {
2115 for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
2116 unsigned char *namebytes;
2117 X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
2121 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2122 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2124 || i2d_X509_NAME(name, &namebytes) != namelen) {
2130 if (!WPACKET_close(pkt))
2136 /* Create a buffer containing data to be signed for server key exchange */
2137 size_t construct_key_exchange_tbs(const SSL *s, unsigned char **ptbs,
2138 const void *param, size_t paramlen)
2140 size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
2141 unsigned char *tbs = OPENSSL_malloc(tbslen);
2145 memcpy(tbs, s->s3->client_random, SSL3_RANDOM_SIZE);
2146 memcpy(tbs + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
2148 memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);