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
= s
->cert
->key
->privatekey
;
175 const EVP_MD
*md
= s
->s3
->tmp
.md
[s
->cert
->key
- s
->cert
->pkeys
];
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 int pktype
, ispss
= 0;
184 pktype
= EVP_PKEY_id(pkey
);
186 mctx
= EVP_MD_CTX_new();
188 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
192 /* Get the data to be signed */
193 if (!get_cert_verify_tbs_data(s
, tls13tbs
, &hdata
, &hdatalen
)) {
194 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
198 if (SSL_USE_SIGALGS(s
) && !tls12_get_sigandhash(s
, pkt
, pkey
, md
, &ispss
)) {
199 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
203 fprintf(stderr
, "Using client alg %s\n", EVP_MD_name(md
));
205 siglen
= EVP_PKEY_size(pkey
);
206 sig
= OPENSSL_malloc(siglen
);
208 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
212 if (EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, pkey
) <= 0
213 || EVP_DigestSignUpdate(mctx
, hdata
, hdatalen
) <= 0) {
214 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
219 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
220 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
,
221 RSA_PSS_SALTLEN_DIGEST
) <= 0) {
222 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
225 } else if (s
->version
== SSL3_VERSION
) {
226 if (!EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
227 (int)s
->session
->master_key_length
,
228 s
->session
->master_key
)) {
229 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
234 if (EVP_DigestSignFinal(mctx
, sig
, &siglen
) <= 0) {
235 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
239 #ifndef OPENSSL_NO_GOST
241 if (pktype
== NID_id_GostR3410_2001
242 || pktype
== NID_id_GostR3410_2012_256
243 || pktype
== NID_id_GostR3410_2012_512
)
244 BUF_reverse(sig
, NULL
, siglen
);
248 if (!WPACKET_sub_memcpy_u16(pkt
, sig
, siglen
)) {
249 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
253 /* Digest cached records and discard handshake buffer */
254 if (!ssl3_digest_cached_records(s
, 0))
258 EVP_MD_CTX_free(mctx
);
262 EVP_MD_CTX_free(mctx
);
263 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
267 MSG_PROCESS_RETURN
tls_process_cert_verify(SSL
*s
, PACKET
*pkt
)
269 EVP_PKEY
*pkey
= NULL
;
270 const unsigned char *data
;
271 #ifndef OPENSSL_NO_GOST
272 unsigned char *gost_data
= NULL
;
274 int al
= SSL_AD_INTERNAL_ERROR
, ret
= MSG_PROCESS_ERROR
;
275 int type
= 0, j
, pktype
;
278 const EVP_MD
*md
= NULL
;
281 unsigned char tls13tbs
[TLS13_TBS_PREAMBLE_SIZE
+ EVP_MAX_MD_SIZE
];
282 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
283 EVP_PKEY_CTX
*pctx
= NULL
;
286 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
290 peer
= s
->session
->peer
;
291 pkey
= X509_get0_pubkey(peer
);
293 al
= SSL_AD_INTERNAL_ERROR
;
297 pktype
= EVP_PKEY_id(pkey
);
298 type
= X509_certificate_type(peer
, pkey
);
300 if (!(type
& EVP_PKT_SIGN
)) {
301 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
,
302 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
303 al
= SSL_AD_ILLEGAL_PARAMETER
;
307 /* Check for broken implementations of GOST ciphersuites */
309 * If key is GOST and n is exactly 64, it is bare signature without
310 * length field (CryptoPro implementations at least till CSP 4.0)
312 #ifndef OPENSSL_NO_GOST
313 if (PACKET_remaining(pkt
) == 64
314 && EVP_PKEY_id(pkey
) == NID_id_GostR3410_2001
) {
319 if (SSL_USE_SIGALGS(s
)) {
323 if (!PACKET_get_net_2(pkt
, &sigalg
)) {
324 al
= SSL_AD_DECODE_ERROR
;
327 rv
= tls12_check_peer_sigalg(s
, sigalg
, pkey
);
330 } else if (rv
== 0) {
331 al
= SSL_AD_DECODE_ERROR
;
334 md
= ssl_md(s
->s3
->tmp
.peer_sigalg
->hash_idx
);
336 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
339 /* Use default digest for this key type */
340 int idx
= ssl_cert_type(NULL
, pkey
);
342 md
= s
->s3
->tmp
.md
[idx
];
344 al
= SSL_AD_INTERNAL_ERROR
;
349 if (!PACKET_get_net_2(pkt
, &len
)) {
350 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_LENGTH_MISMATCH
);
351 al
= SSL_AD_DECODE_ERROR
;
355 j
= EVP_PKEY_size(pkey
);
356 if (((int)len
> j
) || ((int)PACKET_remaining(pkt
) > j
)
357 || (PACKET_remaining(pkt
) == 0)) {
358 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_WRONG_SIGNATURE_SIZE
);
359 al
= SSL_AD_DECODE_ERROR
;
362 if (!PACKET_get_bytes(pkt
, &data
, len
)) {
363 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_LENGTH_MISMATCH
);
364 al
= SSL_AD_DECODE_ERROR
;
368 if (!get_cert_verify_tbs_data(s
, tls13tbs
, &hdata
, &hdatalen
)) {
369 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
374 fprintf(stderr
, "Using client verify alg %s\n", EVP_MD_name(md
));
376 if (EVP_DigestVerifyInit(mctx
, &pctx
, md
, NULL
, pkey
) <= 0
377 || EVP_DigestVerifyUpdate(mctx
, hdata
, hdatalen
) <= 0) {
378 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
381 #ifndef OPENSSL_NO_GOST
383 if (pktype
== NID_id_GostR3410_2001
384 || pktype
== NID_id_GostR3410_2012_256
385 || pktype
== NID_id_GostR3410_2012_512
) {
386 if ((gost_data
= OPENSSL_malloc(len
)) == NULL
) {
387 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
390 BUF_reverse(gost_data
, data
, len
);
396 if (SSL_USE_PSS(s
)) {
397 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
398 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
,
399 RSA_PSS_SALTLEN_DIGEST
) <= 0) {
400 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
403 } else if (s
->version
== SSL3_VERSION
404 && !EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
405 (int)s
->session
->master_key_length
,
406 s
->session
->master_key
)) {
407 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
411 if (EVP_DigestVerifyFinal(mctx
, data
, len
) <= 0) {
412 al
= SSL_AD_DECRYPT_ERROR
;
413 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_BAD_SIGNATURE
);
418 ret
= MSG_PROCESS_CONTINUE_READING
;
420 ret
= MSG_PROCESS_CONTINUE_PROCESSING
;
423 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
424 ossl_statem_set_error(s
);
426 BIO_free(s
->s3
->handshake_buffer
);
427 s
->s3
->handshake_buffer
= NULL
;
428 EVP_MD_CTX_free(mctx
);
429 #ifndef OPENSSL_NO_GOST
430 OPENSSL_free(gost_data
);
435 int tls_construct_finished(SSL
*s
, WPACKET
*pkt
)
437 size_t finish_md_len
;
442 sender
= s
->method
->ssl3_enc
->server_finished_label
;
443 slen
= s
->method
->ssl3_enc
->server_finished_label_len
;
445 sender
= s
->method
->ssl3_enc
->client_finished_label
;
446 slen
= s
->method
->ssl3_enc
->client_finished_label_len
;
449 finish_md_len
= s
->method
->ssl3_enc
->final_finish_mac(s
,
451 s
->s3
->tmp
.finish_md
);
452 if (finish_md_len
== 0) {
453 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED
, ERR_R_INTERNAL_ERROR
);
457 s
->s3
->tmp
.finish_md_len
= finish_md_len
;
459 if (!WPACKET_memcpy(pkt
, s
->s3
->tmp
.finish_md
, finish_md_len
)) {
460 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED
, ERR_R_INTERNAL_ERROR
);
465 * Log the master secret, if logging is enabled. We don't log it for
466 * TLSv1.3: there's a different key schedule for that.
468 if (!SSL_IS_TLS13(s
) && !ssl_log_secret(s
, MASTER_SECRET_LABEL
,
469 s
->session
->master_key
,
470 s
->session
->master_key_length
))
474 * Copy the finished so we can use it for renegotiation checks
477 OPENSSL_assert(finish_md_len
<= EVP_MAX_MD_SIZE
);
478 memcpy(s
->s3
->previous_client_finished
, s
->s3
->tmp
.finish_md
,
480 s
->s3
->previous_client_finished_len
= finish_md_len
;
482 OPENSSL_assert(finish_md_len
<= EVP_MAX_MD_SIZE
);
483 memcpy(s
->s3
->previous_server_finished
, s
->s3
->tmp
.finish_md
,
485 s
->s3
->previous_server_finished_len
= finish_md_len
;
490 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
494 #ifndef OPENSSL_NO_NEXTPROTONEG
496 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
499 static void ssl3_take_mac(SSL
*s
)
504 * If no new cipher setup return immediately: other functions will set
505 * the appropriate error.
507 if (s
->s3
->tmp
.new_cipher
== NULL
)
510 sender
= s
->method
->ssl3_enc
->server_finished_label
;
511 slen
= s
->method
->ssl3_enc
->server_finished_label_len
;
513 sender
= s
->method
->ssl3_enc
->client_finished_label
;
514 slen
= s
->method
->ssl3_enc
->client_finished_label_len
;
517 s
->s3
->tmp
.peer_finish_md_len
= s
->method
->ssl3_enc
->final_finish_mac(s
,
520 s
->s3
->tmp
.peer_finish_md
);
524 MSG_PROCESS_RETURN
tls_process_change_cipher_spec(SSL
*s
, PACKET
*pkt
)
529 remain
= PACKET_remaining(pkt
);
531 * 'Change Cipher Spec' is just a single byte, which should already have
532 * been consumed by ssl_get_message() so there should be no bytes left,
533 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
535 if (SSL_IS_DTLS(s
)) {
536 if ((s
->version
== DTLS1_BAD_VER
537 && remain
!= DTLS1_CCS_HEADER_LENGTH
+ 1)
538 || (s
->version
!= DTLS1_BAD_VER
539 && remain
!= DTLS1_CCS_HEADER_LENGTH
- 1)) {
540 al
= SSL_AD_ILLEGAL_PARAMETER
;
541 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
,
542 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
547 al
= SSL_AD_ILLEGAL_PARAMETER
;
548 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
,
549 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
554 /* Check we have a cipher to change to */
555 if (s
->s3
->tmp
.new_cipher
== NULL
) {
556 al
= SSL_AD_UNEXPECTED_MESSAGE
;
557 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
, SSL_R_CCS_RECEIVED_EARLY
);
561 s
->s3
->change_cipher_spec
= 1;
562 if (!ssl3_do_change_cipher_spec(s
)) {
563 al
= SSL_AD_INTERNAL_ERROR
;
564 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
, ERR_R_INTERNAL_ERROR
);
568 if (SSL_IS_DTLS(s
)) {
569 dtls1_reset_seq_numbers(s
, SSL3_CC_READ
);
571 if (s
->version
== DTLS1_BAD_VER
)
572 s
->d1
->handshake_read_seq
++;
574 #ifndef OPENSSL_NO_SCTP
576 * Remember that a CCS has been received, so that an old key of
577 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
580 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
, 1, NULL
);
584 return MSG_PROCESS_CONTINUE_READING
;
586 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
587 ossl_statem_set_error(s
);
588 return MSG_PROCESS_ERROR
;
591 MSG_PROCESS_RETURN
tls_process_finished(SSL
*s
, PACKET
*pkt
)
593 int al
= SSL_AD_INTERNAL_ERROR
;
596 /* If this occurs, we have missed a message */
597 if (!SSL_IS_TLS13(s
) && !s
->s3
->change_cipher_spec
) {
598 al
= SSL_AD_UNEXPECTED_MESSAGE
;
599 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_GOT_A_FIN_BEFORE_A_CCS
);
602 s
->s3
->change_cipher_spec
= 0;
604 md_len
= s
->s3
->tmp
.peer_finish_md_len
;
606 if (md_len
!= PACKET_remaining(pkt
)) {
607 al
= SSL_AD_DECODE_ERROR
;
608 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_BAD_DIGEST_LENGTH
);
612 if (CRYPTO_memcmp(PACKET_data(pkt
), s
->s3
->tmp
.peer_finish_md
,
614 al
= SSL_AD_DECRYPT_ERROR
;
615 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_DIGEST_CHECK_FAILED
);
620 * Copy the finished so we can use it for renegotiation checks
623 OPENSSL_assert(md_len
<= EVP_MAX_MD_SIZE
);
624 memcpy(s
->s3
->previous_client_finished
, s
->s3
->tmp
.peer_finish_md
,
626 s
->s3
->previous_client_finished_len
= md_len
;
628 OPENSSL_assert(md_len
<= EVP_MAX_MD_SIZE
);
629 memcpy(s
->s3
->previous_server_finished
, s
->s3
->tmp
.peer_finish_md
,
631 s
->s3
->previous_server_finished_len
= md_len
;
635 * In TLS1.3 we also have to change cipher state and do any final processing
636 * of the initial server flight (if we are a client)
638 if (SSL_IS_TLS13(s
)) {
640 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
641 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
642 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_CANNOT_CHANGE_CIPHER
);
646 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
647 s
->master_secret
, s
->handshake_secret
, 0,
648 &s
->session
->master_key_length
)) {
649 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_CANNOT_CHANGE_CIPHER
);
652 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
653 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_CLIENT_READ
)) {
654 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_CANNOT_CHANGE_CIPHER
);
657 if (!tls_process_initial_server_flight(s
, &al
))
662 return MSG_PROCESS_FINISHED_READING
;
664 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
665 ossl_statem_set_error(s
);
666 return MSG_PROCESS_ERROR
;
669 int tls_construct_change_cipher_spec(SSL
*s
, WPACKET
*pkt
)
671 if (!WPACKET_put_bytes_u8(pkt
, SSL3_MT_CCS
)) {
672 SSLerr(SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC
, ERR_R_INTERNAL_ERROR
);
673 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
680 /* Add a certificate to the WPACKET */
681 static int ssl_add_cert_to_wpacket(SSL
*s
, WPACKET
*pkt
, X509
*x
, int chain
,
685 unsigned char *outbytes
;
687 len
= i2d_X509(x
, NULL
);
689 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET
, ERR_R_BUF_LIB
);
690 *al
= SSL_AD_INTERNAL_ERROR
;
693 if (!WPACKET_sub_allocate_bytes_u24(pkt
, len
, &outbytes
)
694 || i2d_X509(x
, &outbytes
) != len
) {
695 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET
, ERR_R_INTERNAL_ERROR
);
696 *al
= SSL_AD_INTERNAL_ERROR
;
701 && !tls_construct_extensions(s
, pkt
, EXT_TLS1_3_CERTIFICATE
, x
,
708 /* Add certificate chain to provided WPACKET */
709 static int ssl_add_cert_chain(SSL
*s
, WPACKET
*pkt
, CERT_PKEY
*cpk
, int *al
)
713 STACK_OF(X509
) *extra_certs
;
714 STACK_OF(X509
) *chain
= NULL
;
715 X509_STORE
*chain_store
;
716 int tmpal
= SSL_AD_INTERNAL_ERROR
;
718 if (cpk
== NULL
|| cpk
->x509
== NULL
)
724 * If we have a certificate specific chain use it, else use parent ctx.
726 if (cpk
->chain
!= NULL
)
727 extra_certs
= cpk
->chain
;
729 extra_certs
= s
->ctx
->extra_certs
;
731 if ((s
->mode
& SSL_MODE_NO_AUTO_CHAIN
) || extra_certs
)
733 else if (s
->cert
->chain_store
)
734 chain_store
= s
->cert
->chain_store
;
736 chain_store
= s
->ctx
->cert_store
;
738 if (chain_store
!= NULL
) {
739 X509_STORE_CTX
*xs_ctx
= X509_STORE_CTX_new();
741 if (xs_ctx
== NULL
) {
742 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, ERR_R_MALLOC_FAILURE
);
745 if (!X509_STORE_CTX_init(xs_ctx
, chain_store
, x
, NULL
)) {
746 X509_STORE_CTX_free(xs_ctx
);
747 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, ERR_R_X509_LIB
);
751 * It is valid for the chain not to be complete (because normally we
752 * don't include the root cert in the chain). Therefore we deliberately
753 * ignore the error return from this call. We're not actually verifying
754 * the cert - we're just building as much of the chain as we can
756 (void)X509_verify_cert(xs_ctx
);
757 /* Don't leave errors in the queue */
759 chain
= X509_STORE_CTX_get0_chain(xs_ctx
);
760 i
= ssl_security_cert_chain(s
, chain
, NULL
, 0);
763 /* Dummy error calls so mkerr generates them */
764 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, SSL_R_EE_KEY_TOO_SMALL
);
765 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, SSL_R_CA_KEY_TOO_SMALL
);
766 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, SSL_R_CA_MD_TOO_WEAK
);
768 X509_STORE_CTX_free(xs_ctx
);
769 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, i
);
772 chain_count
= sk_X509_num(chain
);
773 for (i
= 0; i
< chain_count
; i
++) {
774 x
= sk_X509_value(chain
, i
);
776 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, i
, &tmpal
)) {
777 X509_STORE_CTX_free(xs_ctx
);
781 X509_STORE_CTX_free(xs_ctx
);
783 i
= ssl_security_cert_chain(s
, extra_certs
, x
, 0);
785 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, i
);
788 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, 0, &tmpal
))
790 for (i
= 0; i
< sk_X509_num(extra_certs
); i
++) {
791 x
= sk_X509_value(extra_certs
, i
);
792 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, i
+ 1, &tmpal
))
803 unsigned long ssl3_output_cert_chain(SSL
*s
, WPACKET
*pkt
, CERT_PKEY
*cpk
,
806 int tmpal
= SSL_AD_INTERNAL_ERROR
;
808 if (!WPACKET_start_sub_packet_u24(pkt
)
809 || !ssl_add_cert_chain(s
, pkt
, cpk
, &tmpal
)
810 || !WPACKET_close(pkt
)) {
811 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN
, ERR_R_INTERNAL_ERROR
);
819 * Tidy up after the end of a handshake. In the case of SCTP this may result
820 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
823 WORK_STATE
tls_finish_handshake(SSL
*s
, WORK_STATE wst
, int clearbufs
)
825 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
827 #ifndef OPENSSL_NO_SCTP
828 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
830 ret
= dtls_wait_for_dry(s
);
831 if (ret
!= WORK_FINISHED_CONTINUE
)
837 if (!SSL_IS_DTLS(s
)) {
839 * We don't do this in DTLS because we may still need the init_buf
840 * in case there are any unexpected retransmits
842 BUF_MEM_free(s
->init_buf
);
845 ssl_free_wbio_buffer(s
);
849 if (s
->statem
.cleanuphand
) {
850 /* skipped if we just sent a HelloRequest */
853 s
->statem
.cleanuphand
= 0;
855 ssl3_cleanup_key_block(s
);
858 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
860 s
->ctx
->stats
.sess_accept_good
++;
861 s
->handshake_func
= ossl_statem_accept
;
863 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
865 s
->ctx
->stats
.sess_hit
++;
867 s
->handshake_func
= ossl_statem_connect
;
868 s
->ctx
->stats
.sess_connect_good
++;
871 if (s
->info_callback
!= NULL
)
872 cb
= s
->info_callback
;
873 else if (s
->ctx
->info_callback
!= NULL
)
874 cb
= s
->ctx
->info_callback
;
877 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
879 if (SSL_IS_DTLS(s
)) {
880 /* done with handshaking */
881 s
->d1
->handshake_read_seq
= 0;
882 s
->d1
->handshake_write_seq
= 0;
883 s
->d1
->next_handshake_write_seq
= 0;
884 dtls1_clear_received_buffer(s
);
889 * If we've not cleared the buffers its because we've got more work to do,
893 return WORK_FINISHED_CONTINUE
;
895 return WORK_FINISHED_STOP
;
898 int tls_get_message_header(SSL
*s
, int *mt
)
900 /* s->init_num < SSL3_HM_HEADER_LENGTH */
901 int skip_message
, i
, recvd_type
, al
;
905 p
= (unsigned char *)s
->init_buf
->data
;
908 while (s
->init_num
< SSL3_HM_HEADER_LENGTH
) {
909 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, &recvd_type
,
911 SSL3_HM_HEADER_LENGTH
- s
->init_num
,
914 s
->rwstate
= SSL_READING
;
917 if (recvd_type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
919 * A ChangeCipherSpec must be a single byte and may not occur
920 * in the middle of a handshake message.
922 if (s
->init_num
!= 0 || readbytes
!= 1 || p
[0] != SSL3_MT_CCS
) {
923 al
= SSL_AD_UNEXPECTED_MESSAGE
;
924 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER
,
925 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
928 s
->s3
->tmp
.message_type
= *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
929 s
->init_num
= readbytes
- 1;
930 s
->init_msg
= s
->init_buf
->data
;
931 s
->s3
->tmp
.message_size
= readbytes
;
933 } else if (recvd_type
!= SSL3_RT_HANDSHAKE
) {
934 al
= SSL_AD_UNEXPECTED_MESSAGE
;
935 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER
, SSL_R_CCS_RECEIVED_EARLY
);
938 s
->init_num
+= readbytes
;
943 if (s
->statem
.hand_state
!= TLS_ST_OK
944 && p
[0] == SSL3_MT_HELLO_REQUEST
)
946 * The server may always send 'Hello Request' messages --
947 * we are doing a handshake anyway now, so ignore them if
948 * their format is correct. Does not count for 'Finished'
951 if (p
[1] == 0 && p
[2] == 0 && p
[3] == 0) {
956 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
957 p
, SSL3_HM_HEADER_LENGTH
, s
,
958 s
->msg_callback_arg
);
960 } while (skip_message
);
961 /* s->init_num == SSL3_HM_HEADER_LENGTH */
964 s
->s3
->tmp
.message_type
= *(p
++);
966 if (RECORD_LAYER_is_sslv2_record(&s
->rlayer
)) {
968 * Only happens with SSLv3+ in an SSLv2 backward compatible
971 * Total message size is the remaining record bytes to read
972 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
974 l
= RECORD_LAYER_get_rrec_length(&s
->rlayer
)
975 + SSL3_HM_HEADER_LENGTH
;
976 s
->s3
->tmp
.message_size
= l
;
978 s
->init_msg
= s
->init_buf
->data
;
979 s
->init_num
= SSL3_HM_HEADER_LENGTH
;
982 /* BUF_MEM_grow takes an 'int' parameter */
983 if (l
> (INT_MAX
- SSL3_HM_HEADER_LENGTH
)) {
984 al
= SSL_AD_ILLEGAL_PARAMETER
;
985 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
988 s
->s3
->tmp
.message_size
= l
;
990 s
->init_msg
= s
->init_buf
->data
+ SSL3_HM_HEADER_LENGTH
;
996 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1000 int tls_get_message_body(SSL
*s
, size_t *len
)
1002 size_t n
, readbytes
;
1006 if (s
->s3
->tmp
.message_type
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
1007 /* We've already read everything in */
1008 *len
= (unsigned long)s
->init_num
;
1013 n
= s
->s3
->tmp
.message_size
- s
->init_num
;
1015 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, NULL
,
1016 &p
[s
->init_num
], n
, 0, &readbytes
);
1018 s
->rwstate
= SSL_READING
;
1022 s
->init_num
+= readbytes
;
1026 #ifndef OPENSSL_NO_NEXTPROTONEG
1028 * If receiving Finished, record MAC of prior handshake messages for
1029 * Finished verification.
1031 if (*s
->init_buf
->data
== SSL3_MT_FINISHED
)
1035 /* Feed this message into MAC computation. */
1036 if (RECORD_LAYER_is_sslv2_record(&s
->rlayer
)) {
1037 if (!ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
1039 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY
, ERR_R_EVP_LIB
);
1040 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
1044 if (s
->msg_callback
)
1045 s
->msg_callback(0, SSL2_VERSION
, 0, s
->init_buf
->data
,
1046 (size_t)s
->init_num
, s
, s
->msg_callback_arg
);
1048 if (!ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
1049 s
->init_num
+ SSL3_HM_HEADER_LENGTH
)) {
1050 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY
, ERR_R_EVP_LIB
);
1051 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
1055 if (s
->msg_callback
)
1056 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
, s
->init_buf
->data
,
1057 (size_t)s
->init_num
+ SSL3_HM_HEADER_LENGTH
, s
,
1058 s
->msg_callback_arg
);
1065 int ssl_cert_type(const X509
*x
, const EVP_PKEY
*pk
)
1067 if (pk
== NULL
&& (pk
= X509_get0_pubkey(x
)) == NULL
)
1070 switch (EVP_PKEY_id(pk
)) {
1074 return SSL_PKEY_RSA_ENC
;
1076 return SSL_PKEY_DSA_SIGN
;
1077 #ifndef OPENSSL_NO_EC
1079 return SSL_PKEY_ECC
;
1081 #ifndef OPENSSL_NO_GOST
1082 case NID_id_GostR3410_2001
:
1083 return SSL_PKEY_GOST01
;
1084 case NID_id_GostR3410_2012_256
:
1085 return SSL_PKEY_GOST12_256
;
1086 case NID_id_GostR3410_2012_512
:
1087 return SSL_PKEY_GOST12_512
;
1092 int ssl_verify_alarm_type(long type
)
1097 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1098 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1099 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1100 al
= SSL_AD_UNKNOWN_CA
;
1102 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1103 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1104 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1105 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1106 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1107 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1108 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1109 case X509_V_ERR_CERT_NOT_YET_VALID
:
1110 case X509_V_ERR_CRL_NOT_YET_VALID
:
1111 case X509_V_ERR_CERT_UNTRUSTED
:
1112 case X509_V_ERR_CERT_REJECTED
:
1113 case X509_V_ERR_HOSTNAME_MISMATCH
:
1114 case X509_V_ERR_EMAIL_MISMATCH
:
1115 case X509_V_ERR_IP_ADDRESS_MISMATCH
:
1116 case X509_V_ERR_DANE_NO_MATCH
:
1117 case X509_V_ERR_EE_KEY_TOO_SMALL
:
1118 case X509_V_ERR_CA_KEY_TOO_SMALL
:
1119 case X509_V_ERR_CA_MD_TOO_WEAK
:
1120 al
= SSL_AD_BAD_CERTIFICATE
;
1122 case X509_V_ERR_CERT_SIGNATURE_FAILURE
:
1123 case X509_V_ERR_CRL_SIGNATURE_FAILURE
:
1124 al
= SSL_AD_DECRYPT_ERROR
;
1126 case X509_V_ERR_CERT_HAS_EXPIRED
:
1127 case X509_V_ERR_CRL_HAS_EXPIRED
:
1128 al
= SSL_AD_CERTIFICATE_EXPIRED
;
1130 case X509_V_ERR_CERT_REVOKED
:
1131 al
= SSL_AD_CERTIFICATE_REVOKED
;
1133 case X509_V_ERR_UNSPECIFIED
:
1134 case X509_V_ERR_OUT_OF_MEM
:
1135 case X509_V_ERR_INVALID_CALL
:
1136 case X509_V_ERR_STORE_LOOKUP
:
1137 al
= SSL_AD_INTERNAL_ERROR
;
1139 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1140 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1141 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1142 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1143 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1144 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1145 case X509_V_ERR_INVALID_CA
:
1146 al
= SSL_AD_UNKNOWN_CA
;
1148 case X509_V_ERR_APPLICATION_VERIFICATION
:
1149 al
= SSL_AD_HANDSHAKE_FAILURE
;
1151 case X509_V_ERR_INVALID_PURPOSE
:
1152 al
= SSL_AD_UNSUPPORTED_CERTIFICATE
;
1155 al
= SSL_AD_CERTIFICATE_UNKNOWN
;
1161 int ssl_allow_compression(SSL
*s
)
1163 if (s
->options
& SSL_OP_NO_COMPRESSION
)
1165 return ssl_security(s
, SSL_SECOP_COMPRESSION
, 0, 0, NULL
);
1168 static int version_cmp(const SSL
*s
, int a
, int b
)
1170 int dtls
= SSL_IS_DTLS(s
);
1175 return a
< b
? -1 : 1;
1176 return DTLS_VERSION_LT(a
, b
) ? -1 : 1;
1181 const SSL_METHOD
*(*cmeth
) (void);
1182 const SSL_METHOD
*(*smeth
) (void);
1185 #if TLS_MAX_VERSION != TLS1_3_VERSION
1186 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1189 static const version_info tls_version_table
[] = {
1190 #ifndef OPENSSL_NO_TLS1_3
1191 {TLS1_3_VERSION
, tlsv1_3_client_method
, tlsv1_3_server_method
},
1193 {TLS1_3_VERSION
, NULL
, NULL
},
1195 #ifndef OPENSSL_NO_TLS1_2
1196 {TLS1_2_VERSION
, tlsv1_2_client_method
, tlsv1_2_server_method
},
1198 {TLS1_2_VERSION
, NULL
, NULL
},
1200 #ifndef OPENSSL_NO_TLS1_1
1201 {TLS1_1_VERSION
, tlsv1_1_client_method
, tlsv1_1_server_method
},
1203 {TLS1_1_VERSION
, NULL
, NULL
},
1205 #ifndef OPENSSL_NO_TLS1
1206 {TLS1_VERSION
, tlsv1_client_method
, tlsv1_server_method
},
1208 {TLS1_VERSION
, NULL
, NULL
},
1210 #ifndef OPENSSL_NO_SSL3
1211 {SSL3_VERSION
, sslv3_client_method
, sslv3_server_method
},
1213 {SSL3_VERSION
, NULL
, NULL
},
1218 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1219 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1222 static const version_info dtls_version_table
[] = {
1223 #ifndef OPENSSL_NO_DTLS1_2
1224 {DTLS1_2_VERSION
, dtlsv1_2_client_method
, dtlsv1_2_server_method
},
1226 {DTLS1_2_VERSION
, NULL
, NULL
},
1228 #ifndef OPENSSL_NO_DTLS1
1229 {DTLS1_VERSION
, dtlsv1_client_method
, dtlsv1_server_method
},
1230 {DTLS1_BAD_VER
, dtls_bad_ver_client_method
, NULL
},
1232 {DTLS1_VERSION
, NULL
, NULL
},
1233 {DTLS1_BAD_VER
, NULL
, NULL
},
1239 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1241 * @s: The SSL handle for the candidate method
1242 * @method: the intended method.
1244 * Returns 0 on success, or an SSL error reason on failure.
1246 static int ssl_method_error(const SSL
*s
, const SSL_METHOD
*method
)
1248 int version
= method
->version
;
1250 if ((s
->min_proto_version
!= 0 &&
1251 version_cmp(s
, version
, s
->min_proto_version
) < 0) ||
1252 ssl_security(s
, SSL_SECOP_VERSION
, 0, version
, NULL
) == 0)
1253 return SSL_R_VERSION_TOO_LOW
;
1255 if (s
->max_proto_version
!= 0 &&
1256 version_cmp(s
, version
, s
->max_proto_version
) > 0)
1257 return SSL_R_VERSION_TOO_HIGH
;
1259 if ((s
->options
& method
->mask
) != 0)
1260 return SSL_R_UNSUPPORTED_PROTOCOL
;
1261 if ((method
->flags
& SSL_METHOD_NO_SUITEB
) != 0 && tls1_suiteb(s
))
1262 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE
;
1263 else if ((method
->flags
& SSL_METHOD_NO_FIPS
) != 0 && FIPS_mode())
1264 return SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE
;
1270 * ssl_version_supported - Check that the specified `version` is supported by
1273 * @s: The SSL handle for the candidate method
1274 * @version: Protocol version to test against
1276 * Returns 1 when supported, otherwise 0
1278 int ssl_version_supported(const SSL
*s
, int version
)
1280 const version_info
*vent
;
1281 const version_info
*table
;
1283 switch (s
->method
->version
) {
1285 /* Version should match method version for non-ANY method */
1286 return version_cmp(s
, version
, s
->version
) == 0;
1287 case TLS_ANY_VERSION
:
1288 table
= tls_version_table
;
1290 case DTLS_ANY_VERSION
:
1291 table
= dtls_version_table
;
1296 vent
->version
!= 0 && version_cmp(s
, version
, vent
->version
) <= 0;
1298 if (vent
->cmeth
!= NULL
&&
1299 version_cmp(s
, version
, vent
->version
) == 0 &&
1300 ssl_method_error(s
, vent
->cmeth()) == 0) {
1308 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1309 * fallback indication from a client check whether we're using the highest
1310 * supported protocol version.
1312 * @s server SSL handle.
1314 * Returns 1 when using the highest enabled version, 0 otherwise.
1316 int ssl_check_version_downgrade(SSL
*s
)
1318 const version_info
*vent
;
1319 const version_info
*table
;
1322 * Check that the current protocol is the highest enabled version
1323 * (according to s->ctx->method, as version negotiation may have changed
1326 if (s
->version
== s
->ctx
->method
->version
)
1330 * Apparently we're using a version-flexible SSL_METHOD (not at its
1331 * highest protocol version).
1333 if (s
->ctx
->method
->version
== TLS_method()->version
)
1334 table
= tls_version_table
;
1335 else if (s
->ctx
->method
->version
== DTLS_method()->version
)
1336 table
= dtls_version_table
;
1338 /* Unexpected state; fail closed. */
1342 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1343 if (vent
->smeth
!= NULL
&& ssl_method_error(s
, vent
->smeth()) == 0)
1344 return s
->version
== vent
->version
;
1350 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1351 * protocols, provided the initial (D)TLS method is version-flexible. This
1352 * function sanity-checks the proposed value and makes sure the method is
1353 * version-flexible, then sets the limit if all is well.
1355 * @method_version: The version of the current SSL_METHOD.
1356 * @version: the intended limit.
1357 * @bound: pointer to limit to be updated.
1359 * Returns 1 on success, 0 on failure.
1361 int ssl_set_version_bound(int method_version
, int version
, int *bound
)
1369 * Restrict TLS methods to TLS protocol versions.
1370 * Restrict DTLS methods to DTLS protocol versions.
1371 * Note, DTLS version numbers are decreasing, use comparison macros.
1373 * Note that for both lower-bounds we use explicit versions, not
1374 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1375 * configurations. If the MIN (supported) version ever rises, the user's
1376 * "floor" remains valid even if no longer available. We don't expect the
1377 * MAX ceiling to ever get lower, so making that variable makes sense.
1379 switch (method_version
) {
1382 * XXX For fixed version methods, should we always fail and not set any
1383 * bounds, always succeed and not set any bounds, or set the bounds and
1384 * arrange to fail later if they are not met? At present fixed-version
1385 * methods are not subject to controls that disable individual protocol
1390 case TLS_ANY_VERSION
:
1391 if (version
< SSL3_VERSION
|| version
> TLS_MAX_VERSION
)
1395 case DTLS_ANY_VERSION
:
1396 if (DTLS_VERSION_GT(version
, DTLS_MAX_VERSION
) ||
1397 DTLS_VERSION_LT(version
, DTLS1_BAD_VER
))
1407 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1408 * client HELLO is received to select the final server protocol version and
1409 * the version specific method.
1411 * @s: server SSL handle.
1413 * Returns 0 on success or an SSL error reason number on failure.
1415 int ssl_choose_server_version(SSL
*s
, CLIENTHELLO_MSG
*hello
)
1418 * With version-flexible methods we have an initial state with:
1420 * s->method->version == (D)TLS_ANY_VERSION,
1421 * s->version == (D)TLS_MAX_VERSION.
1423 * So we detect version-flexible methods via the method version, not the
1426 int server_version
= s
->method
->version
;
1427 int client_version
= hello
->legacy_version
;
1428 const version_info
*vent
;
1429 const version_info
*table
;
1431 RAW_EXTENSION
*suppversions
;
1433 s
->client_version
= client_version
;
1435 switch (server_version
) {
1438 * TODO(TLS1.3): This check will fail if someone attempts to do
1439 * renegotiation in TLS1.3 at the moment. We need to ensure we disable
1440 * renegotiation for TLS1.3
1442 if (version_cmp(s
, client_version
, s
->version
) < 0)
1443 return SSL_R_WRONG_SSL_VERSION
;
1445 * If this SSL handle is not from a version flexible method we don't
1446 * (and never did) check min/max FIPS or Suite B constraints. Hope
1447 * that's OK. It is up to the caller to not choose fixed protocol
1448 * versions they don't want. If not, then easy to fix, just return
1449 * ssl_method_error(s, s->method)
1452 case TLS_ANY_VERSION
:
1453 table
= tls_version_table
;
1455 case DTLS_ANY_VERSION
:
1456 table
= dtls_version_table
;
1460 suppversions
= &hello
->pre_proc_exts
[TLSEXT_IDX_supported_versions
];
1462 if (suppversions
->present
&& !SSL_IS_DTLS(s
)) {
1463 unsigned int candidate_vers
= 0;
1464 unsigned int best_vers
= 0;
1465 const SSL_METHOD
*best_method
= NULL
;
1466 PACKET versionslist
;
1468 suppversions
->parsed
= 1;
1470 if (!PACKET_as_length_prefixed_1(&suppversions
->data
, &versionslist
)) {
1471 /* Trailing or invalid data? */
1472 return SSL_R_LENGTH_MISMATCH
;
1475 while (PACKET_get_net_2(&versionslist
, &candidate_vers
)) {
1476 /* TODO(TLS1.3): Remove this before release */
1477 if (candidate_vers
== TLS1_3_VERSION_DRAFT
)
1478 candidate_vers
= TLS1_3_VERSION
;
1480 * TODO(TLS1.3): There is some discussion on the TLS list about
1481 * wheter to ignore versions <TLS1.2 in supported_versions. At the
1482 * moment we honour them if present. To be reviewed later
1484 if (version_cmp(s
, candidate_vers
, best_vers
) <= 0)
1487 vent
->version
!= 0 && vent
->version
!= (int)candidate_vers
;
1490 if (vent
->version
!= 0 && vent
->smeth
!= NULL
) {
1491 const SSL_METHOD
*method
;
1493 method
= vent
->smeth();
1494 if (ssl_method_error(s
, method
) == 0) {
1495 best_vers
= candidate_vers
;
1496 best_method
= method
;
1500 if (PACKET_remaining(&versionslist
) != 0) {
1501 /* Trailing data? */
1502 return SSL_R_LENGTH_MISMATCH
;
1505 if (best_vers
> 0) {
1506 s
->version
= best_vers
;
1507 s
->method
= best_method
;
1510 return SSL_R_UNSUPPORTED_PROTOCOL
;
1514 * If the supported versions extension isn't present, then the highest
1515 * version we can negotiate is TLSv1.2
1517 if (version_cmp(s
, client_version
, TLS1_3_VERSION
) >= 0)
1518 client_version
= TLS1_2_VERSION
;
1521 * No supported versions extension, so we just use the version supplied in
1524 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1525 const SSL_METHOD
*method
;
1527 if (vent
->smeth
== NULL
||
1528 version_cmp(s
, client_version
, vent
->version
) < 0)
1530 method
= vent
->smeth();
1531 if (ssl_method_error(s
, method
) == 0) {
1532 s
->version
= vent
->version
;
1538 return disabled
? SSL_R_UNSUPPORTED_PROTOCOL
: SSL_R_VERSION_TOO_LOW
;
1542 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1543 * server HELLO is received to select the final client protocol version and
1544 * the version specific method.
1546 * @s: client SSL handle.
1547 * @version: The proposed version from the server's HELLO.
1549 * Returns 0 on success or an SSL error reason number on failure.
1551 int ssl_choose_client_version(SSL
*s
, int version
)
1553 const version_info
*vent
;
1554 const version_info
*table
;
1556 /* TODO(TLS1.3): Remove this before release */
1557 if (version
== TLS1_3_VERSION_DRAFT
)
1558 version
= TLS1_3_VERSION
;
1560 switch (s
->method
->version
) {
1562 if (version
!= s
->version
)
1563 return SSL_R_WRONG_SSL_VERSION
;
1565 * If this SSL handle is not from a version flexible method we don't
1566 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1567 * that's OK. It is up to the caller to not choose fixed protocol
1568 * versions they don't want. If not, then easy to fix, just return
1569 * ssl_method_error(s, s->method)
1572 case TLS_ANY_VERSION
:
1573 table
= tls_version_table
;
1575 case DTLS_ANY_VERSION
:
1576 table
= dtls_version_table
;
1580 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1581 const SSL_METHOD
*method
;
1584 if (version
!= vent
->version
)
1586 if (vent
->cmeth
== NULL
)
1588 method
= vent
->cmeth();
1589 err
= ssl_method_error(s
, method
);
1593 s
->version
= version
;
1597 return SSL_R_UNSUPPORTED_PROTOCOL
;
1601 * ssl_get_client_min_max_version - get minimum and maximum client version
1602 * @s: The SSL connection
1603 * @min_version: The minimum supported version
1604 * @max_version: The maximum supported version
1606 * Work out what version we should be using for the initial ClientHello if the
1607 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1608 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1609 * or FIPS_mode() constraints and any floor imposed by the security level here,
1610 * so we don't advertise the wrong protocol version to only reject the outcome later.
1612 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1613 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1614 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1616 * Returns 0 on success or an SSL error reason number on failure. On failure
1617 * min_version and max_version will also be set to 0.
1619 int ssl_get_client_min_max_version(const SSL
*s
, int *min_version
,
1624 const SSL_METHOD
*single
= NULL
;
1625 const SSL_METHOD
*method
;
1626 const version_info
*table
;
1627 const version_info
*vent
;
1629 switch (s
->method
->version
) {
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)
1638 *min_version
= *max_version
= s
->version
;
1640 case TLS_ANY_VERSION
:
1641 table
= tls_version_table
;
1643 case DTLS_ANY_VERSION
:
1644 table
= dtls_version_table
;
1649 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1650 * below X enabled. This is required in order to maintain the "version
1651 * capability" vector contiguous. Any versions with a NULL client method
1652 * (protocol version client is disabled at compile-time) is also a "hole".
1654 * Our initial state is hole == 1, version == 0. That is, versions above
1655 * the first version in the method table are disabled (a "hole" above
1656 * the valid protocol entries) and we don't have a selected version yet.
1658 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1659 * the selected version, and the method becomes a candidate "single"
1660 * method. We're no longer in a hole, so "hole" becomes 0.
1662 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1663 * as we support a contiguous range of at least two methods. If we hit
1664 * a disabled method, then hole becomes true again, but nothing else
1665 * changes yet, because all the remaining methods may be disabled too.
1666 * If we again hit an enabled method after the new hole, it becomes
1667 * selected, as we start from scratch.
1669 *min_version
= version
= 0;
1671 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1673 * A table entry with a NULL client method is still a hole in the
1674 * "version capability" vector.
1676 if (vent
->cmeth
== NULL
) {
1680 method
= vent
->cmeth();
1681 if (ssl_method_error(s
, method
) != 0) {
1685 *min_version
= method
->version
;
1687 version
= (single
= method
)->version
;
1688 *min_version
= version
;
1693 *max_version
= version
;
1695 /* Fail if everything is disabled */
1697 return SSL_R_NO_PROTOCOLS_AVAILABLE
;
1703 * ssl_set_client_hello_version - Work out what version we should be using for
1704 * the initial ClientHello.legacy_version field.
1706 * @s: client SSL handle.
1708 * Returns 0 on success or an SSL error reason number on failure.
1710 int ssl_set_client_hello_version(SSL
*s
)
1712 int ver_min
, ver_max
, ret
;
1714 ret
= ssl_get_client_min_max_version(s
, &ver_min
, &ver_max
);
1719 s
->version
= ver_max
;
1721 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
1722 if (!SSL_IS_DTLS(s
) && ver_max
> TLS1_2_VERSION
)
1723 ver_max
= TLS1_2_VERSION
;
1725 s
->client_version
= ver_max
;