2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 * ECC cipher suite support in OpenSSL originally developed by
13 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
19 #include "../ssl_locl.h"
20 #include "statem_locl.h"
21 #include <openssl/buffer.h>
22 #include <openssl/objects.h>
23 #include <openssl/evp.h>
24 #include <openssl/x509.h>
27 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
28 * SSL3_RT_CHANGE_CIPHER_SPEC)
30 int ssl3_do_write(SSL *s, int type)
35 ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
36 s->init_num, &written);
39 if (type == SSL3_RT_HANDSHAKE)
41 * should not be done for 'Hello Request's, but in that case we'll
42 * ignore the result anyway
44 if (!ssl3_finish_mac(s,
45 (unsigned char *)&s->init_buf->data[s->init_off],
49 if (written == s->init_num) {
51 s->msg_callback(1, s->version, type, s->init_buf->data,
52 (size_t)(s->init_off + s->init_num), s,
56 s->init_off += written;
57 s->init_num -= written;
61 int tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
65 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
66 || !WPACKET_get_length(pkt, &msglen)
69 s->init_num = (int)msglen;
75 int tls_setup_handshake(SSL *s)
77 if (!ssl3_init_finished_mac(s))
81 if (SSL_IS_FIRST_HANDSHAKE(s)) {
82 s->ctx->stats.sess_accept++;
83 } else if (!s->s3->send_connection_binding &&
85 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
87 * Server attempting to renegotiate with client that doesn't
88 * support secure renegotiation.
90 SSLerr(SSL_F_TLS_SETUP_HANDSHAKE,
91 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
92 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
95 s->ctx->stats.sess_accept_renegotiate++;
97 s->s3->tmp.cert_request = 0;
100 if (SSL_IS_FIRST_HANDSHAKE(s))
101 s->ctx->stats.sess_connect++;
103 s->ctx->stats.sess_connect_renegotiate++;
105 /* mark client_random uninitialized */
106 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
109 s->s3->tmp.cert_req = 0;
112 s->statem.use_timer = 1;
119 * Size of the to-be-signed TLS13 data, without the hash size itself:
120 * 64 bytes of value 32, 33 context bytes, 1 byte separator
122 #define TLS13_TBS_START_SIZE 64
123 #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
125 static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs,
126 void **hdata, size_t *hdatalen)
128 static const char *servercontext = "TLS 1.3, server CertificateVerify";
129 static const char *clientcontext = "TLS 1.3, client CertificateVerify";
131 if (SSL_IS_TLS13(s)) {
134 /* Set the first 64 bytes of to-be-signed data to octet 32 */
135 memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
136 /* This copies the 33 bytes of context plus the 0 separator byte */
137 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
138 || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
139 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
141 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
144 * If we're currently reading then we need to use the saved handshake
145 * hash value. We can't use the current handshake hash state because
146 * that includes the CertVerify itself.
148 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
149 || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
150 memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
151 s->cert_verify_hash_len);
152 hashlen = s->cert_verify_hash_len;
153 } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
154 EVP_MAX_MD_SIZE, &hashlen)) {
159 *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
163 retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata);
172 int tls_construct_cert_verify(SSL *s, WPACKET *pkt)
174 EVP_PKEY *pkey = NULL;
175 const EVP_MD *md = NULL;
176 EVP_MD_CTX *mctx = NULL;
177 EVP_PKEY_CTX *pctx = NULL;
178 size_t hdatalen = 0, siglen = 0;
180 unsigned char *sig = NULL;
181 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
182 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
184 if (lu == NULL || s->s3->tmp.cert == NULL) {
185 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
188 pkey = s->s3->tmp.cert->privatekey;
189 md = ssl_md(lu->hash_idx);
191 if (pkey == NULL || md == NULL) {
192 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
196 mctx = EVP_MD_CTX_new();
198 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
202 /* Get the data to be signed */
203 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
204 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
208 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
209 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
212 siglen = EVP_PKEY_size(pkey);
213 sig = OPENSSL_malloc(siglen);
215 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
219 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0
220 || EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0) {
221 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
225 if (lu->sig == EVP_PKEY_RSA_PSS) {
226 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
227 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
228 RSA_PSS_SALTLEN_DIGEST) <= 0) {
229 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
232 } else if (s->version == SSL3_VERSION) {
233 if (!EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
234 (int)s->session->master_key_length,
235 s->session->master_key)) {
236 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
241 if (EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
242 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);
246 #ifndef OPENSSL_NO_GOST
248 int pktype = lu->sig;
250 if (pktype == NID_id_GostR3410_2001
251 || pktype == NID_id_GostR3410_2012_256
252 || pktype == NID_id_GostR3410_2012_512)
253 BUF_reverse(sig, NULL, siglen);
257 if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
258 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
262 /* Digest cached records and discard handshake buffer */
263 if (!ssl3_digest_cached_records(s, 0))
267 EVP_MD_CTX_free(mctx);
271 EVP_MD_CTX_free(mctx);
272 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
276 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
278 EVP_PKEY *pkey = NULL;
279 const unsigned char *data;
280 #ifndef OPENSSL_NO_GOST
281 unsigned char *gost_data = NULL;
283 int al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
284 int type = 0, j, pktype;
287 const EVP_MD *md = NULL;
290 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
291 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
292 EVP_PKEY_CTX *pctx = NULL;
295 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
299 peer = s->session->peer;
300 pkey = X509_get0_pubkey(peer);
302 al = SSL_AD_INTERNAL_ERROR;
306 pktype = EVP_PKEY_id(pkey);
307 type = X509_certificate_type(peer, pkey);
309 if (!(type & EVP_PKT_SIGN)) {
310 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY,
311 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
312 al = SSL_AD_ILLEGAL_PARAMETER;
316 /* Check for broken implementations of GOST ciphersuites */
318 * If key is GOST and n is exactly 64, it is bare signature without
319 * length field (CryptoPro implementations at least till CSP 4.0)
321 #ifndef OPENSSL_NO_GOST
322 if (PACKET_remaining(pkt) == 64
323 && EVP_PKEY_id(pkey) == NID_id_GostR3410_2001) {
328 if (SSL_USE_SIGALGS(s)) {
332 if (!PACKET_get_net_2(pkt, &sigalg)) {
333 al = SSL_AD_DECODE_ERROR;
336 rv = tls12_check_peer_sigalg(s, sigalg, pkey);
339 } else if (rv == 0) {
340 al = SSL_AD_DECODE_ERROR;
344 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
346 } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
347 al = SSL_AD_INTERNAL_ERROR;
351 md = ssl_md(s->s3->tmp.peer_sigalg->hash_idx);
353 if (!PACKET_get_net_2(pkt, &len)) {
354 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
355 al = SSL_AD_DECODE_ERROR;
359 j = EVP_PKEY_size(pkey);
360 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
361 || (PACKET_remaining(pkt) == 0)) {
362 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
363 al = SSL_AD_DECODE_ERROR;
366 if (!PACKET_get_bytes(pkt, &data, len)) {
367 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
368 al = SSL_AD_DECODE_ERROR;
372 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
373 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
378 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
380 if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0
381 || EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0) {
382 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
385 #ifndef OPENSSL_NO_GOST
387 if (pktype == NID_id_GostR3410_2001
388 || pktype == NID_id_GostR3410_2012_256
389 || pktype == NID_id_GostR3410_2012_512) {
390 if ((gost_data = OPENSSL_malloc(len)) == NULL) {
391 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
394 BUF_reverse(gost_data, data, len);
400 if (SSL_USE_PSS(s)) {
401 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
402 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
403 RSA_PSS_SALTLEN_DIGEST) <= 0) {
404 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
407 } else if (s->version == SSL3_VERSION
408 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
409 (int)s->session->master_key_length,
410 s->session->master_key)) {
411 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
415 if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
416 al = SSL_AD_DECRYPT_ERROR;
417 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
422 ret = MSG_PROCESS_CONTINUE_READING;
424 ret = MSG_PROCESS_CONTINUE_PROCESSING;
427 ssl3_send_alert(s, SSL3_AL_FATAL, al);
428 ossl_statem_set_error(s);
430 BIO_free(s->s3->handshake_buffer);
431 s->s3->handshake_buffer = NULL;
432 EVP_MD_CTX_free(mctx);
433 #ifndef OPENSSL_NO_GOST
434 OPENSSL_free(gost_data);
439 int tls_construct_finished(SSL *s, WPACKET *pkt)
441 size_t finish_md_len;
446 sender = s->method->ssl3_enc->server_finished_label;
447 slen = s->method->ssl3_enc->server_finished_label_len;
449 sender = s->method->ssl3_enc->client_finished_label;
450 slen = s->method->ssl3_enc->client_finished_label_len;
453 finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
455 s->s3->tmp.finish_md);
456 if (finish_md_len == 0) {
457 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
461 s->s3->tmp.finish_md_len = finish_md_len;
463 if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) {
464 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
469 * Log the master secret, if logging is enabled. We don't log it for
470 * TLSv1.3: there's a different key schedule for that.
472 if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL,
473 s->session->master_key,
474 s->session->master_key_length))
478 * Copy the finished so we can use it for renegotiation checks
481 OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE);
482 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md,
484 s->s3->previous_client_finished_len = finish_md_len;
486 OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE);
487 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md,
489 s->s3->previous_server_finished_len = finish_md_len;
494 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
498 int tls_construct_key_update(SSL *s, WPACKET *pkt)
500 if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
501 SSLerr(SSL_F_TLS_CONSTRUCT_KEY_UPDATE, ERR_R_INTERNAL_ERROR);
505 s->key_update = SSL_KEY_UPDATE_NONE;
509 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
513 MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt)
516 unsigned int updatetype;
518 s->key_update_count++;
519 if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) {
520 al = SSL_AD_ILLEGAL_PARAMETER;
521 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_TOO_MANY_KEY_UPDATES);
525 if (!PACKET_get_1(pkt, &updatetype)
526 || PACKET_remaining(pkt) != 0
527 || (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
528 && updatetype != SSL_KEY_UPDATE_REQUESTED)) {
529 al = SSL_AD_DECODE_ERROR;
530 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_BAD_KEY_UPDATE);
535 * If we get a request for us to update our sending keys too then, we need
536 * to additionally send a KeyUpdate message. However that message should
537 * not also request an update (otherwise we get into an infinite loop).
539 if (updatetype == SSL_KEY_UPDATE_REQUESTED)
540 s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
542 if (!tls13_update_key(s, 0)) {
543 al = SSL_AD_INTERNAL_ERROR;
544 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, ERR_R_INTERNAL_ERROR);
548 return MSG_PROCESS_FINISHED_READING;
550 ssl3_send_alert(s, SSL3_AL_FATAL, al);
551 ossl_statem_set_error(s);
552 return MSG_PROCESS_ERROR;
555 #ifndef OPENSSL_NO_NEXTPROTONEG
557 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
560 static void ssl3_take_mac(SSL *s)
565 * If no new cipher setup return immediately: other functions will set
566 * the appropriate error.
568 if (s->s3->tmp.new_cipher == NULL)
571 sender = s->method->ssl3_enc->server_finished_label;
572 slen = s->method->ssl3_enc->server_finished_label_len;
574 sender = s->method->ssl3_enc->client_finished_label;
575 slen = s->method->ssl3_enc->client_finished_label_len;
578 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
581 s->s3->tmp.peer_finish_md);
585 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
590 remain = PACKET_remaining(pkt);
592 * 'Change Cipher Spec' is just a single byte, which should already have
593 * been consumed by ssl_get_message() so there should be no bytes left,
594 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
596 if (SSL_IS_DTLS(s)) {
597 if ((s->version == DTLS1_BAD_VER
598 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
599 || (s->version != DTLS1_BAD_VER
600 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
601 al = SSL_AD_ILLEGAL_PARAMETER;
602 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
603 SSL_R_BAD_CHANGE_CIPHER_SPEC);
608 al = SSL_AD_ILLEGAL_PARAMETER;
609 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
610 SSL_R_BAD_CHANGE_CIPHER_SPEC);
615 /* Check we have a cipher to change to */
616 if (s->s3->tmp.new_cipher == NULL) {
617 al = SSL_AD_UNEXPECTED_MESSAGE;
618 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
622 s->s3->change_cipher_spec = 1;
623 if (!ssl3_do_change_cipher_spec(s)) {
624 al = SSL_AD_INTERNAL_ERROR;
625 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
629 if (SSL_IS_DTLS(s)) {
630 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
632 if (s->version == DTLS1_BAD_VER)
633 s->d1->handshake_read_seq++;
635 #ifndef OPENSSL_NO_SCTP
637 * Remember that a CCS has been received, so that an old key of
638 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
641 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
645 return MSG_PROCESS_CONTINUE_READING;
647 ssl3_send_alert(s, SSL3_AL_FATAL, al);
648 ossl_statem_set_error(s);
649 return MSG_PROCESS_ERROR;
652 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
654 int al = SSL_AD_INTERNAL_ERROR;
658 /* This is a real handshake so make sure we clean it up at the end */
659 s->statem.cleanuphand = 1;
661 /* If this occurs, we have missed a message */
662 if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) {
663 al = SSL_AD_UNEXPECTED_MESSAGE;
664 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
667 s->s3->change_cipher_spec = 0;
669 md_len = s->s3->tmp.peer_finish_md_len;
671 if (md_len != PACKET_remaining(pkt)) {
672 al = SSL_AD_DECODE_ERROR;
673 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
677 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md,
679 al = SSL_AD_DECRYPT_ERROR;
680 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
685 * Copy the finished so we can use it for renegotiation checks
688 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE);
689 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md,
691 s->s3->previous_client_finished_len = md_len;
693 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE);
694 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md,
696 s->s3->previous_server_finished_len = md_len;
700 * In TLS1.3 we also have to change cipher state and do any final processing
701 * of the initial server flight (if we are a client)
703 if (SSL_IS_TLS13(s)) {
705 if (!s->method->ssl3_enc->change_cipher_state(s,
706 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
707 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
711 if (!s->method->ssl3_enc->generate_master_secret(s,
712 s->master_secret, s->handshake_secret, 0,
713 &s->session->master_key_length)) {
714 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
717 if (!s->method->ssl3_enc->change_cipher_state(s,
718 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
719 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
722 if (!tls_process_initial_server_flight(s, &al))
727 return MSG_PROCESS_FINISHED_READING;
729 ssl3_send_alert(s, SSL3_AL_FATAL, al);
730 ossl_statem_set_error(s);
731 return MSG_PROCESS_ERROR;
734 int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
736 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
737 SSLerr(SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
738 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
745 /* Add a certificate to the WPACKET */
746 static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain,
750 unsigned char *outbytes;
752 len = i2d_X509(x, NULL);
754 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_BUF_LIB);
755 *al = SSL_AD_INTERNAL_ERROR;
758 if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
759 || i2d_X509(x, &outbytes) != len) {
760 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_INTERNAL_ERROR);
761 *al = SSL_AD_INTERNAL_ERROR;
766 && !tls_construct_extensions(s, pkt, EXT_TLS1_3_CERTIFICATE, x,
773 /* Add certificate chain to provided WPACKET */
774 static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al)
778 STACK_OF(X509) *extra_certs;
779 STACK_OF(X509) *chain = NULL;
780 X509_STORE *chain_store;
781 int tmpal = SSL_AD_INTERNAL_ERROR;
783 if (cpk == NULL || cpk->x509 == NULL)
789 * If we have a certificate specific chain use it, else use parent ctx.
791 if (cpk->chain != NULL)
792 extra_certs = cpk->chain;
794 extra_certs = s->ctx->extra_certs;
796 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
798 else if (s->cert->chain_store)
799 chain_store = s->cert->chain_store;
801 chain_store = s->ctx->cert_store;
803 if (chain_store != NULL) {
804 X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
806 if (xs_ctx == NULL) {
807 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
810 if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
811 X509_STORE_CTX_free(xs_ctx);
812 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
816 * It is valid for the chain not to be complete (because normally we
817 * don't include the root cert in the chain). Therefore we deliberately
818 * ignore the error return from this call. We're not actually verifying
819 * the cert - we're just building as much of the chain as we can
821 (void)X509_verify_cert(xs_ctx);
822 /* Don't leave errors in the queue */
824 chain = X509_STORE_CTX_get0_chain(xs_ctx);
825 i = ssl_security_cert_chain(s, chain, NULL, 0);
828 /* Dummy error calls so mkerr generates them */
829 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
830 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
831 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
833 X509_STORE_CTX_free(xs_ctx);
834 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
837 chain_count = sk_X509_num(chain);
838 for (i = 0; i < chain_count; i++) {
839 x = sk_X509_value(chain, i);
841 if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) {
842 X509_STORE_CTX_free(xs_ctx);
846 X509_STORE_CTX_free(xs_ctx);
848 i = ssl_security_cert_chain(s, extra_certs, x, 0);
850 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
853 if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal))
855 for (i = 0; i < sk_X509_num(extra_certs); i++) {
856 x = sk_X509_value(extra_certs, i);
857 if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal))
868 unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk,
871 int tmpal = SSL_AD_INTERNAL_ERROR;
873 if (!WPACKET_start_sub_packet_u24(pkt)
874 || !ssl_add_cert_chain(s, pkt, cpk, &tmpal)
875 || !WPACKET_close(pkt)) {
876 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR);
884 * Tidy up after the end of a handshake. In the case of SCTP this may result
885 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
888 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs)
890 void (*cb) (const SSL *ssl, int type, int val) = NULL;
892 #ifndef OPENSSL_NO_SCTP
893 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
895 ret = dtls_wait_for_dry(s);
896 if (ret != WORK_FINISHED_CONTINUE)
902 if (!SSL_IS_DTLS(s)) {
904 * We don't do this in DTLS because we may still need the init_buf
905 * in case there are any unexpected retransmits
907 BUF_MEM_free(s->init_buf);
910 ssl_free_wbio_buffer(s);
914 if (s->statem.cleanuphand) {
915 /* skipped if we just sent a HelloRequest */
918 s->statem.cleanuphand = 0;
920 ssl3_cleanup_key_block(s);
923 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
925 s->ctx->stats.sess_accept_good++;
926 s->handshake_func = ossl_statem_accept;
928 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
930 s->ctx->stats.sess_hit++;
932 s->handshake_func = ossl_statem_connect;
933 s->ctx->stats.sess_connect_good++;
936 if (s->info_callback != NULL)
937 cb = s->info_callback;
938 else if (s->ctx->info_callback != NULL)
939 cb = s->ctx->info_callback;
942 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
944 if (SSL_IS_DTLS(s)) {
945 /* done with handshaking */
946 s->d1->handshake_read_seq = 0;
947 s->d1->handshake_write_seq = 0;
948 s->d1->next_handshake_write_seq = 0;
949 dtls1_clear_received_buffer(s);
951 s->early_data_state = SSL_EARLY_DATA_NONE;
955 * If we've not cleared the buffers its because we've got more work to do,
959 return WORK_FINISHED_CONTINUE;
961 ossl_statem_set_in_init(s, 0);
962 return WORK_FINISHED_STOP;
965 int tls_get_message_header(SSL *s, int *mt)
967 /* s->init_num < SSL3_HM_HEADER_LENGTH */
968 int skip_message, i, recvd_type, al;
972 p = (unsigned char *)s->init_buf->data;
975 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
976 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
978 SSL3_HM_HEADER_LENGTH - s->init_num,
981 s->rwstate = SSL_READING;
984 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
986 * A ChangeCipherSpec must be a single byte and may not occur
987 * in the middle of a handshake message.
989 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
990 al = SSL_AD_UNEXPECTED_MESSAGE;
991 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER,
992 SSL_R_BAD_CHANGE_CIPHER_SPEC);
995 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
996 s->init_num = readbytes - 1;
997 s->init_msg = s->init_buf->data;
998 s->s3->tmp.message_size = readbytes;
1000 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1001 al = SSL_AD_UNEXPECTED_MESSAGE;
1002 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY);
1005 s->init_num += readbytes;
1010 if (s->statem.hand_state != TLS_ST_OK
1011 && p[0] == SSL3_MT_HELLO_REQUEST)
1013 * The server may always send 'Hello Request' messages --
1014 * we are doing a handshake anyway now, so ignore them if
1015 * their format is correct. Does not count for 'Finished'
1018 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1022 if (s->msg_callback)
1023 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1024 p, SSL3_HM_HEADER_LENGTH, s,
1025 s->msg_callback_arg);
1027 } while (skip_message);
1028 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1031 s->s3->tmp.message_type = *(p++);
1033 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1035 * Only happens with SSLv3+ in an SSLv2 backward compatible
1038 * Total message size is the remaining record bytes to read
1039 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1041 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
1042 + SSL3_HM_HEADER_LENGTH;
1043 s->s3->tmp.message_size = l;
1045 s->init_msg = s->init_buf->data;
1046 s->init_num = SSL3_HM_HEADER_LENGTH;
1049 /* BUF_MEM_grow takes an 'int' parameter */
1050 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1051 al = SSL_AD_ILLEGAL_PARAMETER;
1052 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
1055 s->s3->tmp.message_size = l;
1057 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1063 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1067 int tls_get_message_body(SSL *s, size_t *len)
1069 size_t n, readbytes;
1073 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1074 /* We've already read everything in */
1075 *len = (unsigned long)s->init_num;
1080 n = s->s3->tmp.message_size - s->init_num;
1082 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
1083 &p[s->init_num], n, 0, &readbytes);
1085 s->rwstate = SSL_READING;
1089 s->init_num += readbytes;
1093 #ifndef OPENSSL_NO_NEXTPROTONEG
1095 * If receiving Finished, record MAC of prior handshake messages for
1096 * Finished verification.
1098 if (*s->init_buf->data == SSL3_MT_FINISHED)
1102 /* Feed this message into MAC computation. */
1103 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1104 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1106 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB);
1107 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1111 if (s->msg_callback)
1112 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1113 (size_t)s->init_num, s, s->msg_callback_arg);
1115 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1116 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1117 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB);
1118 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1122 if (s->msg_callback)
1123 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1124 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
1125 s->msg_callback_arg);
1132 int ssl_cert_type(const X509 *x, const EVP_PKEY *pk)
1134 if (pk == NULL && (pk = X509_get0_pubkey(x)) == NULL)
1137 switch (EVP_PKEY_id(pk)) {
1141 return SSL_PKEY_RSA;
1143 return SSL_PKEY_DSA_SIGN;
1144 #ifndef OPENSSL_NO_EC
1146 return SSL_PKEY_ECC;
1148 #ifndef OPENSSL_NO_GOST
1149 case NID_id_GostR3410_2001:
1150 return SSL_PKEY_GOST01;
1151 case NID_id_GostR3410_2012_256:
1152 return SSL_PKEY_GOST12_256;
1153 case NID_id_GostR3410_2012_512:
1154 return SSL_PKEY_GOST12_512;
1159 int ssl_verify_alarm_type(long type)
1164 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1165 case X509_V_ERR_UNABLE_TO_GET_CRL:
1166 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1167 al = SSL_AD_UNKNOWN_CA;
1169 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1170 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1171 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1172 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1173 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1174 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1175 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1176 case X509_V_ERR_CERT_NOT_YET_VALID:
1177 case X509_V_ERR_CRL_NOT_YET_VALID:
1178 case X509_V_ERR_CERT_UNTRUSTED:
1179 case X509_V_ERR_CERT_REJECTED:
1180 case X509_V_ERR_HOSTNAME_MISMATCH:
1181 case X509_V_ERR_EMAIL_MISMATCH:
1182 case X509_V_ERR_IP_ADDRESS_MISMATCH:
1183 case X509_V_ERR_DANE_NO_MATCH:
1184 case X509_V_ERR_EE_KEY_TOO_SMALL:
1185 case X509_V_ERR_CA_KEY_TOO_SMALL:
1186 case X509_V_ERR_CA_MD_TOO_WEAK:
1187 al = SSL_AD_BAD_CERTIFICATE;
1189 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
1190 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
1191 al = SSL_AD_DECRYPT_ERROR;
1193 case X509_V_ERR_CERT_HAS_EXPIRED:
1194 case X509_V_ERR_CRL_HAS_EXPIRED:
1195 al = SSL_AD_CERTIFICATE_EXPIRED;
1197 case X509_V_ERR_CERT_REVOKED:
1198 al = SSL_AD_CERTIFICATE_REVOKED;
1200 case X509_V_ERR_UNSPECIFIED:
1201 case X509_V_ERR_OUT_OF_MEM:
1202 case X509_V_ERR_INVALID_CALL:
1203 case X509_V_ERR_STORE_LOOKUP:
1204 al = SSL_AD_INTERNAL_ERROR;
1206 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1207 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1208 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1209 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1210 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1211 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1212 case X509_V_ERR_INVALID_CA:
1213 al = SSL_AD_UNKNOWN_CA;
1215 case X509_V_ERR_APPLICATION_VERIFICATION:
1216 al = SSL_AD_HANDSHAKE_FAILURE;
1218 case X509_V_ERR_INVALID_PURPOSE:
1219 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1222 al = SSL_AD_CERTIFICATE_UNKNOWN;
1228 int ssl_allow_compression(SSL *s)
1230 if (s->options & SSL_OP_NO_COMPRESSION)
1232 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1235 static int version_cmp(const SSL *s, int a, int b)
1237 int dtls = SSL_IS_DTLS(s);
1242 return a < b ? -1 : 1;
1243 return DTLS_VERSION_LT(a, b) ? -1 : 1;
1248 const SSL_METHOD *(*cmeth) (void);
1249 const SSL_METHOD *(*smeth) (void);
1252 #if TLS_MAX_VERSION != TLS1_3_VERSION
1253 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1256 static const version_info tls_version_table[] = {
1257 #ifndef OPENSSL_NO_TLS1_3
1258 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1260 {TLS1_3_VERSION, NULL, NULL},
1262 #ifndef OPENSSL_NO_TLS1_2
1263 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1265 {TLS1_2_VERSION, NULL, NULL},
1267 #ifndef OPENSSL_NO_TLS1_1
1268 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1270 {TLS1_1_VERSION, NULL, NULL},
1272 #ifndef OPENSSL_NO_TLS1
1273 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1275 {TLS1_VERSION, NULL, NULL},
1277 #ifndef OPENSSL_NO_SSL3
1278 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1280 {SSL3_VERSION, NULL, NULL},
1285 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1286 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1289 static const version_info dtls_version_table[] = {
1290 #ifndef OPENSSL_NO_DTLS1_2
1291 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1293 {DTLS1_2_VERSION, NULL, NULL},
1295 #ifndef OPENSSL_NO_DTLS1
1296 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1297 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1299 {DTLS1_VERSION, NULL, NULL},
1300 {DTLS1_BAD_VER, NULL, NULL},
1306 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1308 * @s: The SSL handle for the candidate method
1309 * @method: the intended method.
1311 * Returns 0 on success, or an SSL error reason on failure.
1313 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
1315 int version = method->version;
1317 if ((s->min_proto_version != 0 &&
1318 version_cmp(s, version, s->min_proto_version) < 0) ||
1319 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1320 return SSL_R_VERSION_TOO_LOW;
1322 if (s->max_proto_version != 0 &&
1323 version_cmp(s, version, s->max_proto_version) > 0)
1324 return SSL_R_VERSION_TOO_HIGH;
1326 if ((s->options & method->mask) != 0)
1327 return SSL_R_UNSUPPORTED_PROTOCOL;
1328 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1329 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1335 * ssl_version_supported - Check that the specified `version` is supported by
1338 * @s: The SSL handle for the candidate method
1339 * @version: Protocol version to test against
1341 * Returns 1 when supported, otherwise 0
1343 int ssl_version_supported(const SSL *s, int version)
1345 const version_info *vent;
1346 const version_info *table;
1348 switch (s->method->version) {
1350 /* Version should match method version for non-ANY method */
1351 return version_cmp(s, version, s->version) == 0;
1352 case TLS_ANY_VERSION:
1353 table = tls_version_table;
1355 case DTLS_ANY_VERSION:
1356 table = dtls_version_table;
1361 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
1363 if (vent->cmeth != NULL &&
1364 version_cmp(s, version, vent->version) == 0 &&
1365 ssl_method_error(s, vent->cmeth()) == 0) {
1373 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1374 * fallback indication from a client check whether we're using the highest
1375 * supported protocol version.
1377 * @s server SSL handle.
1379 * Returns 1 when using the highest enabled version, 0 otherwise.
1381 int ssl_check_version_downgrade(SSL *s)
1383 const version_info *vent;
1384 const version_info *table;
1387 * Check that the current protocol is the highest enabled version
1388 * (according to s->ctx->method, as version negotiation may have changed
1391 if (s->version == s->ctx->method->version)
1395 * Apparently we're using a version-flexible SSL_METHOD (not at its
1396 * highest protocol version).
1398 if (s->ctx->method->version == TLS_method()->version)
1399 table = tls_version_table;
1400 else if (s->ctx->method->version == DTLS_method()->version)
1401 table = dtls_version_table;
1403 /* Unexpected state; fail closed. */
1407 for (vent = table; vent->version != 0; ++vent) {
1408 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
1409 return s->version == vent->version;
1415 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1416 * protocols, provided the initial (D)TLS method is version-flexible. This
1417 * function sanity-checks the proposed value and makes sure the method is
1418 * version-flexible, then sets the limit if all is well.
1420 * @method_version: The version of the current SSL_METHOD.
1421 * @version: the intended limit.
1422 * @bound: pointer to limit to be updated.
1424 * Returns 1 on success, 0 on failure.
1426 int ssl_set_version_bound(int method_version, int version, int *bound)
1434 * Restrict TLS methods to TLS protocol versions.
1435 * Restrict DTLS methods to DTLS protocol versions.
1436 * Note, DTLS version numbers are decreasing, use comparison macros.
1438 * Note that for both lower-bounds we use explicit versions, not
1439 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1440 * configurations. If the MIN (supported) version ever rises, the user's
1441 * "floor" remains valid even if no longer available. We don't expect the
1442 * MAX ceiling to ever get lower, so making that variable makes sense.
1444 switch (method_version) {
1447 * XXX For fixed version methods, should we always fail and not set any
1448 * bounds, always succeed and not set any bounds, or set the bounds and
1449 * arrange to fail later if they are not met? At present fixed-version
1450 * methods are not subject to controls that disable individual protocol
1455 case TLS_ANY_VERSION:
1456 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
1460 case DTLS_ANY_VERSION:
1461 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
1462 DTLS_VERSION_LT(version, DTLS1_BAD_VER))
1472 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1473 * client HELLO is received to select the final server protocol version and
1474 * the version specific method.
1476 * @s: server SSL handle.
1478 * Returns 0 on success or an SSL error reason number on failure.
1480 int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello)
1483 * With version-flexible methods we have an initial state with:
1485 * s->method->version == (D)TLS_ANY_VERSION,
1486 * s->version == (D)TLS_MAX_VERSION.
1488 * So we detect version-flexible methods via the method version, not the
1491 int server_version = s->method->version;
1492 int client_version = hello->legacy_version;
1493 const version_info *vent;
1494 const version_info *table;
1496 RAW_EXTENSION *suppversions;
1498 s->client_version = client_version;
1500 switch (server_version) {
1502 if (!SSL_IS_TLS13(s)) {
1503 if (version_cmp(s, client_version, s->version) < 0)
1504 return SSL_R_WRONG_SSL_VERSION;
1506 * If this SSL handle is not from a version flexible method we don't
1507 * (and never did) check min/max FIPS or Suite B constraints. Hope
1508 * that's OK. It is up to the caller to not choose fixed protocol
1509 * versions they don't want. If not, then easy to fix, just return
1510 * ssl_method_error(s, s->method)
1515 * Fall through if we are TLSv1.3 already (this means we must be after
1516 * a HelloRetryRequest
1518 case TLS_ANY_VERSION:
1519 table = tls_version_table;
1521 case DTLS_ANY_VERSION:
1522 table = dtls_version_table;
1526 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1528 if (suppversions->present && !SSL_IS_DTLS(s)) {
1529 unsigned int candidate_vers = 0;
1530 unsigned int best_vers = 0;
1531 const SSL_METHOD *best_method = NULL;
1532 PACKET versionslist;
1534 suppversions->parsed = 1;
1536 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1537 /* Trailing or invalid data? */
1538 return SSL_R_LENGTH_MISMATCH;
1541 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1542 /* TODO(TLS1.3): Remove this before release */
1543 if (candidate_vers == TLS1_3_VERSION_DRAFT)
1544 candidate_vers = TLS1_3_VERSION;
1546 * TODO(TLS1.3): There is some discussion on the TLS list about
1547 * wheter to ignore versions <TLS1.2 in supported_versions. At the
1548 * moment we honour them if present. To be reviewed later
1550 if (version_cmp(s, candidate_vers, best_vers) <= 0)
1553 vent->version != 0 && vent->version != (int)candidate_vers;
1556 if (vent->version != 0 && vent->smeth != NULL) {
1557 const SSL_METHOD *method;
1559 method = vent->smeth();
1560 if (ssl_method_error(s, method) == 0) {
1561 best_vers = candidate_vers;
1562 best_method = method;
1566 if (PACKET_remaining(&versionslist) != 0) {
1567 /* Trailing data? */
1568 return SSL_R_LENGTH_MISMATCH;
1571 if (best_vers > 0) {
1572 if (SSL_IS_TLS13(s)) {
1574 * We get here if this is after a HelloRetryRequest. In this
1575 * case we just check that we still negotiated TLSv1.3
1577 if (best_vers != TLS1_3_VERSION)
1578 return SSL_R_UNSUPPORTED_PROTOCOL;
1581 s->version = best_vers;
1582 s->method = best_method;
1585 return SSL_R_UNSUPPORTED_PROTOCOL;
1589 * If the supported versions extension isn't present, then the highest
1590 * version we can negotiate is TLSv1.2
1592 if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1593 client_version = TLS1_2_VERSION;
1596 * No supported versions extension, so we just use the version supplied in
1599 for (vent = table; vent->version != 0; ++vent) {
1600 const SSL_METHOD *method;
1602 if (vent->smeth == NULL ||
1603 version_cmp(s, client_version, vent->version) < 0)
1605 method = vent->smeth();
1606 if (ssl_method_error(s, method) == 0) {
1607 s->version = vent->version;
1613 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1617 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1618 * server HELLO is received to select the final client protocol version and
1619 * the version specific method.
1621 * @s: client SSL handle.
1622 * @version: The proposed version from the server's HELLO.
1624 * Returns 0 on success or an SSL error reason number on failure.
1626 int ssl_choose_client_version(SSL *s, int version)
1628 const version_info *vent;
1629 const version_info *table;
1631 /* TODO(TLS1.3): Remove this before release */
1632 if (version == TLS1_3_VERSION_DRAFT)
1633 version = TLS1_3_VERSION;
1635 switch (s->method->version) {
1637 if (version != s->version)
1638 return SSL_R_WRONG_SSL_VERSION;
1640 * If this SSL handle is not from a version flexible method we don't
1641 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1642 * that's OK. It is up to the caller to not choose fixed protocol
1643 * versions they don't want. If not, then easy to fix, just return
1644 * ssl_method_error(s, s->method)
1647 case TLS_ANY_VERSION:
1648 table = tls_version_table;
1650 case DTLS_ANY_VERSION:
1651 table = dtls_version_table;
1655 for (vent = table; vent->version != 0; ++vent) {
1656 const SSL_METHOD *method;
1659 if (version != vent->version)
1661 if (vent->cmeth == NULL)
1663 if (s->hello_retry_request && version != TLS1_3_VERSION)
1664 return SSL_R_WRONG_SSL_VERSION;
1666 method = vent->cmeth();
1667 err = ssl_method_error(s, method);
1671 s->version = version;
1675 return SSL_R_UNSUPPORTED_PROTOCOL;
1679 * ssl_get_client_min_max_version - get minimum and maximum client version
1680 * @s: The SSL connection
1681 * @min_version: The minimum supported version
1682 * @max_version: The maximum supported version
1684 * Work out what version we should be using for the initial ClientHello if the
1685 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1686 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1687 * constraints and any floor imposed by the security level here,
1688 * so we don't advertise the wrong protocol version to only reject the outcome later.
1690 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1691 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1692 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1694 * Returns 0 on success or an SSL error reason number on failure. On failure
1695 * min_version and max_version will also be set to 0.
1697 int ssl_get_client_min_max_version(const SSL *s, int *min_version,
1702 const SSL_METHOD *single = NULL;
1703 const SSL_METHOD *method;
1704 const version_info *table;
1705 const version_info *vent;
1707 switch (s->method->version) {
1710 * If this SSL handle is not from a version flexible method we don't
1711 * (and never did) check min/max FIPS or Suite B constraints. Hope
1712 * that's OK. It is up to the caller to not choose fixed protocol
1713 * versions they don't want. If not, then easy to fix, just return
1714 * ssl_method_error(s, s->method)
1716 *min_version = *max_version = s->version;
1718 case TLS_ANY_VERSION:
1719 table = tls_version_table;
1721 case DTLS_ANY_VERSION:
1722 table = dtls_version_table;
1727 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1728 * below X enabled. This is required in order to maintain the "version
1729 * capability" vector contiguous. Any versions with a NULL client method
1730 * (protocol version client is disabled at compile-time) is also a "hole".
1732 * Our initial state is hole == 1, version == 0. That is, versions above
1733 * the first version in the method table are disabled (a "hole" above
1734 * the valid protocol entries) and we don't have a selected version yet.
1736 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1737 * the selected version, and the method becomes a candidate "single"
1738 * method. We're no longer in a hole, so "hole" becomes 0.
1740 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1741 * as we support a contiguous range of at least two methods. If we hit
1742 * a disabled method, then hole becomes true again, but nothing else
1743 * changes yet, because all the remaining methods may be disabled too.
1744 * If we again hit an enabled method after the new hole, it becomes
1745 * selected, as we start from scratch.
1747 *min_version = version = 0;
1749 for (vent = table; vent->version != 0; ++vent) {
1751 * A table entry with a NULL client method is still a hole in the
1752 * "version capability" vector.
1754 if (vent->cmeth == NULL) {
1758 method = vent->cmeth();
1759 if (ssl_method_error(s, method) != 0) {
1763 *min_version = method->version;
1765 version = (single = method)->version;
1766 *min_version = version;
1771 *max_version = version;
1773 /* Fail if everything is disabled */
1775 return SSL_R_NO_PROTOCOLS_AVAILABLE;
1781 * ssl_set_client_hello_version - Work out what version we should be using for
1782 * the initial ClientHello.legacy_version field.
1784 * @s: client SSL handle.
1786 * Returns 0 on success or an SSL error reason number on failure.
1788 int ssl_set_client_hello_version(SSL *s)
1790 int ver_min, ver_max, ret;
1792 ret = ssl_get_client_min_max_version(s, &ver_min, &ver_max);
1797 s->version = ver_max;
1799 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
1800 if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
1801 ver_max = TLS1_2_VERSION;
1803 s->client_version = ver_max;
1808 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
1809 * and |checkallow| is 1 then additionally check if the group is allowed to be
1810 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
1811 * 1) or 0 otherwise.
1813 #ifndef OPENSSL_NO_EC
1814 int check_in_list(SSL *s, unsigned int group_id, const unsigned char *groups,
1815 size_t num_groups, int checkallow)
1819 if (groups == NULL || num_groups == 0)
1822 for (i = 0; i < num_groups; i++, groups += 2) {
1823 unsigned int share_id = (groups[0] << 8) | (groups[1]);
1825 if (group_id == share_id
1827 || tls_curve_allowed(s, groups, SSL_SECOP_CURVE_CHECK))) {
1832 /* If i == num_groups then not in the list */
1833 return i < num_groups;