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_construct_finished(SSL *s, WPACKET *pkt)
82 sender = s->method->ssl3_enc->server_finished_label;
83 slen = s->method->ssl3_enc->server_finished_label_len;
85 sender = s->method->ssl3_enc->client_finished_label;
86 slen = s->method->ssl3_enc->client_finished_label_len;
89 finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
91 s->s3->tmp.finish_md);
92 if (finish_md_len == 0) {
93 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
97 s->s3->tmp.finish_md_len = finish_md_len;
99 if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) {
100 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
105 * Copy the finished so we can use it for renegotiation checks
108 OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE);
109 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md,
111 s->s3->previous_client_finished_len = finish_md_len;
113 OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE);
114 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md,
116 s->s3->previous_server_finished_len = finish_md_len;
121 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
125 #ifndef OPENSSL_NO_NEXTPROTONEG
127 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
130 static void ssl3_take_mac(SSL *s)
135 * If no new cipher setup return immediately: other functions will set
136 * the appropriate error.
138 if (s->s3->tmp.new_cipher == NULL)
141 sender = s->method->ssl3_enc->server_finished_label;
142 slen = s->method->ssl3_enc->server_finished_label_len;
144 sender = s->method->ssl3_enc->client_finished_label;
145 slen = s->method->ssl3_enc->client_finished_label_len;
148 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
151 s->s3->tmp.peer_finish_md);
155 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
160 remain = PACKET_remaining(pkt);
162 * 'Change Cipher Spec' is just a single byte, which should already have
163 * been consumed by ssl_get_message() so there should be no bytes left,
164 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
166 if (SSL_IS_DTLS(s)) {
167 if ((s->version == DTLS1_BAD_VER
168 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
169 || (s->version != DTLS1_BAD_VER
170 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
171 al = SSL_AD_ILLEGAL_PARAMETER;
172 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
173 SSL_R_BAD_CHANGE_CIPHER_SPEC);
178 al = SSL_AD_ILLEGAL_PARAMETER;
179 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
180 SSL_R_BAD_CHANGE_CIPHER_SPEC);
185 /* Check we have a cipher to change to */
186 if (s->s3->tmp.new_cipher == NULL) {
187 al = SSL_AD_UNEXPECTED_MESSAGE;
188 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
192 s->s3->change_cipher_spec = 1;
193 if (!ssl3_do_change_cipher_spec(s)) {
194 al = SSL_AD_INTERNAL_ERROR;
195 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
199 if (SSL_IS_DTLS(s)) {
200 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
202 if (s->version == DTLS1_BAD_VER)
203 s->d1->handshake_read_seq++;
205 #ifndef OPENSSL_NO_SCTP
207 * Remember that a CCS has been received, so that an old key of
208 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
211 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
215 return MSG_PROCESS_CONTINUE_READING;
217 ssl3_send_alert(s, SSL3_AL_FATAL, al);
218 ossl_statem_set_error(s);
219 return MSG_PROCESS_ERROR;
222 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
224 int al = SSL_AD_INTERNAL_ERROR;
227 /* If this occurs, we have missed a message */
228 if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) {
229 al = SSL_AD_UNEXPECTED_MESSAGE;
230 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
233 s->s3->change_cipher_spec = 0;
235 md_len = s->s3->tmp.peer_finish_md_len;
237 if (md_len != PACKET_remaining(pkt)) {
238 al = SSL_AD_DECODE_ERROR;
239 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
243 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md,
245 al = SSL_AD_DECRYPT_ERROR;
246 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
251 * Copy the finished so we can use it for renegotiation checks
254 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE);
255 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md,
257 s->s3->previous_client_finished_len = md_len;
259 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE);
260 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md,
262 s->s3->previous_server_finished_len = md_len;
266 * In TLS1.3 we also have to change cipher state and do any final processing
267 * of the initial server flight (if we are a client)
269 if (SSL_IS_TLS13(s)) {
271 if (!s->method->ssl3_enc->change_cipher_state(s,
272 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
273 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
277 if (!s->method->ssl3_enc->generate_master_secret(s,
278 s->session->master_key, s->handshake_secret, 0,
279 &s->session->master_key_length)) {
280 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
283 if (!s->method->ssl3_enc->change_cipher_state(s,
284 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
285 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER);
288 if (!tls_process_initial_server_flight(s, &al))
293 return MSG_PROCESS_FINISHED_READING;
295 ssl3_send_alert(s, SSL3_AL_FATAL, al);
296 ossl_statem_set_error(s);
297 return MSG_PROCESS_ERROR;
300 int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
302 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
303 SSLerr(SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
304 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
311 /* Add a certificate to the WPACKET */
312 static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain,
316 unsigned char *outbytes;
318 len = i2d_X509(x, NULL);
320 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_BUF_LIB);
321 *al = SSL_AD_INTERNAL_ERROR;
324 if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
325 || i2d_X509(x, &outbytes) != len) {
326 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_INTERNAL_ERROR);
327 *al = SSL_AD_INTERNAL_ERROR;
332 && !tls_construct_extensions(s, pkt, EXT_TLS1_3_CERTIFICATE, x,
339 /* Add certificate chain to provided WPACKET */
340 static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al)
344 STACK_OF(X509) *extra_certs;
345 STACK_OF(X509) *chain = NULL;
346 X509_STORE *chain_store;
347 int tmpal = SSL_AD_INTERNAL_ERROR;
349 if (cpk == NULL || cpk->x509 == NULL)
355 * If we have a certificate specific chain use it, else use parent ctx.
357 if (cpk->chain != NULL)
358 extra_certs = cpk->chain;
360 extra_certs = s->ctx->extra_certs;
362 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
364 else if (s->cert->chain_store)
365 chain_store = s->cert->chain_store;
367 chain_store = s->ctx->cert_store;
369 if (chain_store != NULL) {
370 X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
372 if (xs_ctx == NULL) {
373 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
376 if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
377 X509_STORE_CTX_free(xs_ctx);
378 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
382 * It is valid for the chain not to be complete (because normally we
383 * don't include the root cert in the chain). Therefore we deliberately
384 * ignore the error return from this call. We're not actually verifying
385 * the cert - we're just building as much of the chain as we can
387 (void)X509_verify_cert(xs_ctx);
388 /* Don't leave errors in the queue */
390 chain = X509_STORE_CTX_get0_chain(xs_ctx);
391 i = ssl_security_cert_chain(s, chain, NULL, 0);
394 /* Dummy error calls so mkerr generates them */
395 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
396 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
397 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
399 X509_STORE_CTX_free(xs_ctx);
400 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
403 chain_count = sk_X509_num(chain);
404 for (i = 0; i < chain_count; i++) {
405 x = sk_X509_value(chain, i);
407 if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) {
408 X509_STORE_CTX_free(xs_ctx);
412 X509_STORE_CTX_free(xs_ctx);
414 i = ssl_security_cert_chain(s, extra_certs, x, 0);
416 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
419 if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal))
421 for (i = 0; i < sk_X509_num(extra_certs); i++) {
422 x = sk_X509_value(extra_certs, i);
423 if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal))
434 unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk,
437 int tmpal = SSL_AD_INTERNAL_ERROR;
439 if (!WPACKET_start_sub_packet_u24(pkt)
440 || !ssl_add_cert_chain(s, pkt, cpk, &tmpal)
441 || !WPACKET_close(pkt)) {
442 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR);
449 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst)
451 void (*cb) (const SSL *ssl, int type, int val) = NULL;
453 #ifndef OPENSSL_NO_SCTP
454 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
456 ret = dtls_wait_for_dry(s);
457 if (ret != WORK_FINISHED_CONTINUE)
462 /* clean a few things up */
463 ssl3_cleanup_key_block(s);
465 if (!SSL_IS_DTLS(s)) {
467 * We don't do this in DTLS because we may still need the init_buf
468 * in case there are any unexpected retransmits
470 BUF_MEM_free(s->init_buf);
474 ssl_free_wbio_buffer(s);
478 if (!s->server || s->renegotiate == 2) {
479 /* skipped if we just sent a HelloRequest */
484 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
486 s->ctx->stats.sess_accept_good++;
487 s->handshake_func = ossl_statem_accept;
489 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
491 s->ctx->stats.sess_hit++;
493 s->handshake_func = ossl_statem_connect;
494 s->ctx->stats.sess_connect_good++;
497 if (s->info_callback != NULL)
498 cb = s->info_callback;
499 else if (s->ctx->info_callback != NULL)
500 cb = s->ctx->info_callback;
503 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
505 if (SSL_IS_DTLS(s)) {
506 /* done with handshaking */
507 s->d1->handshake_read_seq = 0;
508 s->d1->handshake_write_seq = 0;
509 s->d1->next_handshake_write_seq = 0;
510 dtls1_clear_received_buffer(s);
514 return WORK_FINISHED_STOP;
517 int tls_get_message_header(SSL *s, int *mt)
519 /* s->init_num < SSL3_HM_HEADER_LENGTH */
520 int skip_message, i, recvd_type, al;
524 p = (unsigned char *)s->init_buf->data;
527 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
528 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
530 SSL3_HM_HEADER_LENGTH - s->init_num,
533 s->rwstate = SSL_READING;
536 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
538 * A ChangeCipherSpec must be a single byte and may not occur
539 * in the middle of a handshake message.
541 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
542 al = SSL_AD_UNEXPECTED_MESSAGE;
543 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER,
544 SSL_R_BAD_CHANGE_CIPHER_SPEC);
547 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
548 s->init_num = readbytes - 1;
549 s->init_msg = s->init_buf->data;
550 s->s3->tmp.message_size = readbytes;
552 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
553 al = SSL_AD_UNEXPECTED_MESSAGE;
554 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY);
557 s->init_num += readbytes;
562 if (p[0] == SSL3_MT_HELLO_REQUEST)
564 * The server may always send 'Hello Request' messages --
565 * we are doing a handshake anyway now, so ignore them if
566 * their format is correct. Does not count for 'Finished'
569 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
574 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
575 p, SSL3_HM_HEADER_LENGTH, s,
576 s->msg_callback_arg);
578 } while (skip_message);
579 /* s->init_num == SSL3_HM_HEADER_LENGTH */
582 s->s3->tmp.message_type = *(p++);
584 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
586 * Only happens with SSLv3+ in an SSLv2 backward compatible
589 * Total message size is the remaining record bytes to read
590 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
592 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
593 + SSL3_HM_HEADER_LENGTH;
594 s->s3->tmp.message_size = l;
596 s->init_msg = s->init_buf->data;
597 s->init_num = SSL3_HM_HEADER_LENGTH;
600 /* BUF_MEM_grow takes an 'int' parameter */
601 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
602 al = SSL_AD_ILLEGAL_PARAMETER;
603 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
606 s->s3->tmp.message_size = l;
608 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
614 ssl3_send_alert(s, SSL3_AL_FATAL, al);
618 int tls_get_message_body(SSL *s, size_t *len)
624 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
625 /* We've already read everything in */
626 *len = (unsigned long)s->init_num;
631 n = s->s3->tmp.message_size - s->init_num;
633 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
634 &p[s->init_num], n, 0, &readbytes);
636 s->rwstate = SSL_READING;
640 s->init_num += readbytes;
644 #ifndef OPENSSL_NO_NEXTPROTONEG
646 * If receiving Finished, record MAC of prior handshake messages for
647 * Finished verification.
649 if (*s->init_buf->data == SSL3_MT_FINISHED)
653 /* Feed this message into MAC computation. */
654 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
655 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
657 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB);
658 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
663 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
664 (size_t)s->init_num, s, s->msg_callback_arg);
666 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
667 s->init_num + SSL3_HM_HEADER_LENGTH)) {
668 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB);
669 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
674 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
675 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
676 s->msg_callback_arg);
683 int ssl_cert_type(const X509 *x, const EVP_PKEY *pk)
685 if (pk == NULL && (pk = X509_get0_pubkey(x)) == NULL)
688 switch (EVP_PKEY_id(pk)) {
692 return SSL_PKEY_RSA_ENC;
694 return SSL_PKEY_DSA_SIGN;
695 #ifndef OPENSSL_NO_EC
699 #ifndef OPENSSL_NO_GOST
700 case NID_id_GostR3410_2001:
701 return SSL_PKEY_GOST01;
702 case NID_id_GostR3410_2012_256:
703 return SSL_PKEY_GOST12_256;
704 case NID_id_GostR3410_2012_512:
705 return SSL_PKEY_GOST12_512;
710 int ssl_verify_alarm_type(long type)
715 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
716 case X509_V_ERR_UNABLE_TO_GET_CRL:
717 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
718 al = SSL_AD_UNKNOWN_CA;
720 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
721 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
722 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
723 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
724 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
725 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
726 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
727 case X509_V_ERR_CERT_NOT_YET_VALID:
728 case X509_V_ERR_CRL_NOT_YET_VALID:
729 case X509_V_ERR_CERT_UNTRUSTED:
730 case X509_V_ERR_CERT_REJECTED:
731 case X509_V_ERR_HOSTNAME_MISMATCH:
732 case X509_V_ERR_EMAIL_MISMATCH:
733 case X509_V_ERR_IP_ADDRESS_MISMATCH:
734 case X509_V_ERR_DANE_NO_MATCH:
735 case X509_V_ERR_EE_KEY_TOO_SMALL:
736 case X509_V_ERR_CA_KEY_TOO_SMALL:
737 case X509_V_ERR_CA_MD_TOO_WEAK:
738 al = SSL_AD_BAD_CERTIFICATE;
740 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
741 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
742 al = SSL_AD_DECRYPT_ERROR;
744 case X509_V_ERR_CERT_HAS_EXPIRED:
745 case X509_V_ERR_CRL_HAS_EXPIRED:
746 al = SSL_AD_CERTIFICATE_EXPIRED;
748 case X509_V_ERR_CERT_REVOKED:
749 al = SSL_AD_CERTIFICATE_REVOKED;
751 case X509_V_ERR_UNSPECIFIED:
752 case X509_V_ERR_OUT_OF_MEM:
753 case X509_V_ERR_INVALID_CALL:
754 case X509_V_ERR_STORE_LOOKUP:
755 al = SSL_AD_INTERNAL_ERROR;
757 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
758 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
759 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
760 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
761 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
762 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
763 case X509_V_ERR_INVALID_CA:
764 al = SSL_AD_UNKNOWN_CA;
766 case X509_V_ERR_APPLICATION_VERIFICATION:
767 al = SSL_AD_HANDSHAKE_FAILURE;
769 case X509_V_ERR_INVALID_PURPOSE:
770 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
773 al = SSL_AD_CERTIFICATE_UNKNOWN;
779 int ssl_allow_compression(SSL *s)
781 if (s->options & SSL_OP_NO_COMPRESSION)
783 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
786 static int version_cmp(const SSL *s, int a, int b)
788 int dtls = SSL_IS_DTLS(s);
793 return a < b ? -1 : 1;
794 return DTLS_VERSION_LT(a, b) ? -1 : 1;
799 const SSL_METHOD *(*cmeth) (void);
800 const SSL_METHOD *(*smeth) (void);
803 #if TLS_MAX_VERSION != TLS1_3_VERSION
804 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
807 static const version_info tls_version_table[] = {
808 #ifndef OPENSSL_NO_TLS1_3
809 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
811 {TLS1_3_VERSION, NULL, NULL},
813 #ifndef OPENSSL_NO_TLS1_2
814 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
816 {TLS1_2_VERSION, NULL, NULL},
818 #ifndef OPENSSL_NO_TLS1_1
819 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
821 {TLS1_1_VERSION, NULL, NULL},
823 #ifndef OPENSSL_NO_TLS1
824 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
826 {TLS1_VERSION, NULL, NULL},
828 #ifndef OPENSSL_NO_SSL3
829 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
831 {SSL3_VERSION, NULL, NULL},
836 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
837 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
840 static const version_info dtls_version_table[] = {
841 #ifndef OPENSSL_NO_DTLS1_2
842 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
844 {DTLS1_2_VERSION, NULL, NULL},
846 #ifndef OPENSSL_NO_DTLS1
847 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
848 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
850 {DTLS1_VERSION, NULL, NULL},
851 {DTLS1_BAD_VER, NULL, NULL},
857 * ssl_method_error - Check whether an SSL_METHOD is enabled.
859 * @s: The SSL handle for the candidate method
860 * @method: the intended method.
862 * Returns 0 on success, or an SSL error reason on failure.
864 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
866 int version = method->version;
868 if ((s->min_proto_version != 0 &&
869 version_cmp(s, version, s->min_proto_version) < 0) ||
870 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
871 return SSL_R_VERSION_TOO_LOW;
873 if (s->max_proto_version != 0 &&
874 version_cmp(s, version, s->max_proto_version) > 0)
875 return SSL_R_VERSION_TOO_HIGH;
877 if ((s->options & method->mask) != 0)
878 return SSL_R_UNSUPPORTED_PROTOCOL;
879 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
880 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
881 else if ((method->flags & SSL_METHOD_NO_FIPS) != 0 && FIPS_mode())
882 return SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE;
888 * ssl_version_supported - Check that the specified `version` is supported by
891 * @s: The SSL handle for the candidate method
892 * @version: Protocol version to test against
894 * Returns 1 when supported, otherwise 0
896 int ssl_version_supported(const SSL *s, int version)
898 const version_info *vent;
899 const version_info *table;
901 switch (s->method->version) {
903 /* Version should match method version for non-ANY method */
904 return version_cmp(s, version, s->version) == 0;
905 case TLS_ANY_VERSION:
906 table = tls_version_table;
908 case DTLS_ANY_VERSION:
909 table = dtls_version_table;
914 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
916 if (vent->cmeth != NULL &&
917 version_cmp(s, version, vent->version) == 0 &&
918 ssl_method_error(s, vent->cmeth()) == 0) {
926 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
927 * fallback indication from a client check whether we're using the highest
928 * supported protocol version.
930 * @s server SSL handle.
932 * Returns 1 when using the highest enabled version, 0 otherwise.
934 int ssl_check_version_downgrade(SSL *s)
936 const version_info *vent;
937 const version_info *table;
940 * Check that the current protocol is the highest enabled version
941 * (according to s->ctx->method, as version negotiation may have changed
944 if (s->version == s->ctx->method->version)
948 * Apparently we're using a version-flexible SSL_METHOD (not at its
949 * highest protocol version).
951 if (s->ctx->method->version == TLS_method()->version)
952 table = tls_version_table;
953 else if (s->ctx->method->version == DTLS_method()->version)
954 table = dtls_version_table;
956 /* Unexpected state; fail closed. */
960 for (vent = table; vent->version != 0; ++vent) {
961 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
962 return s->version == vent->version;
968 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
969 * protocols, provided the initial (D)TLS method is version-flexible. This
970 * function sanity-checks the proposed value and makes sure the method is
971 * version-flexible, then sets the limit if all is well.
973 * @method_version: The version of the current SSL_METHOD.
974 * @version: the intended limit.
975 * @bound: pointer to limit to be updated.
977 * Returns 1 on success, 0 on failure.
979 int ssl_set_version_bound(int method_version, int version, int *bound)
987 * Restrict TLS methods to TLS protocol versions.
988 * Restrict DTLS methods to DTLS protocol versions.
989 * Note, DTLS version numbers are decreasing, use comparison macros.
991 * Note that for both lower-bounds we use explicit versions, not
992 * (D)TLS_MIN_VERSION. This is because we don't want to break user
993 * configurations. If the MIN (supported) version ever rises, the user's
994 * "floor" remains valid even if no longer available. We don't expect the
995 * MAX ceiling to ever get lower, so making that variable makes sense.
997 switch (method_version) {
1000 * XXX For fixed version methods, should we always fail and not set any
1001 * bounds, always succeed and not set any bounds, or set the bounds and
1002 * arrange to fail later if they are not met? At present fixed-version
1003 * methods are not subject to controls that disable individual protocol
1008 case TLS_ANY_VERSION:
1009 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
1013 case DTLS_ANY_VERSION:
1014 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
1015 DTLS_VERSION_LT(version, DTLS1_BAD_VER))
1025 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1026 * client HELLO is received to select the final server protocol version and
1027 * the version specific method.
1029 * @s: server SSL handle.
1031 * Returns 0 on success or an SSL error reason number on failure.
1033 int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello)
1036 * With version-flexible methods we have an initial state with:
1038 * s->method->version == (D)TLS_ANY_VERSION,
1039 * s->version == (D)TLS_MAX_VERSION.
1041 * So we detect version-flexible methods via the method version, not the
1044 int server_version = s->method->version;
1045 int client_version = hello->legacy_version;
1046 const version_info *vent;
1047 const version_info *table;
1049 RAW_EXTENSION *suppversions;
1051 s->client_version = client_version;
1053 switch (server_version) {
1056 * TODO(TLS1.3): This check will fail if someone attempts to do
1057 * renegotiation in TLS1.3 at the moment. We need to ensure we disable
1058 * renegotiation for TLS1.3
1060 if (version_cmp(s, client_version, s->version) < 0)
1061 return SSL_R_WRONG_SSL_VERSION;
1063 * If this SSL handle is not from a version flexible method we don't
1064 * (and never did) check min/max FIPS or Suite B constraints. Hope
1065 * that's OK. It is up to the caller to not choose fixed protocol
1066 * versions they don't want. If not, then easy to fix, just return
1067 * ssl_method_error(s, s->method)
1070 case TLS_ANY_VERSION:
1071 table = tls_version_table;
1073 case DTLS_ANY_VERSION:
1074 table = dtls_version_table;
1078 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1080 if (suppversions->present && !SSL_IS_DTLS(s)) {
1081 unsigned int candidate_vers = 0;
1082 unsigned int best_vers = 0;
1083 const SSL_METHOD *best_method = NULL;
1084 PACKET versionslist;
1086 suppversions->parsed = 1;
1088 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1089 /* Trailing or invalid data? */
1090 return SSL_R_LENGTH_MISMATCH;
1093 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1094 /* TODO(TLS1.3): Remove this before release */
1095 if (candidate_vers == TLS1_3_VERSION_DRAFT)
1096 candidate_vers = TLS1_3_VERSION;
1098 * TODO(TLS1.3): There is some discussion on the TLS list about
1099 * wheter to ignore versions <TLS1.2 in supported_versions. At the
1100 * moment we honour them if present. To be reviewed later
1102 if (version_cmp(s, candidate_vers, best_vers) <= 0)
1105 vent->version != 0 && vent->version != (int)candidate_vers;
1108 if (vent->version != 0 && vent->smeth != NULL) {
1109 const SSL_METHOD *method;
1111 method = vent->smeth();
1112 if (ssl_method_error(s, method) == 0) {
1113 best_vers = candidate_vers;
1114 best_method = method;
1118 if (PACKET_remaining(&versionslist) != 0) {
1119 /* Trailing data? */
1120 return SSL_R_LENGTH_MISMATCH;
1123 if (best_vers > 0) {
1124 s->version = best_vers;
1125 s->method = best_method;
1128 return SSL_R_UNSUPPORTED_PROTOCOL;
1132 * If the supported versions extension isn't present, then the highest
1133 * version we can negotiate is TLSv1.2
1135 if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1136 client_version = TLS1_2_VERSION;
1139 * No supported versions extension, so we just use the version supplied in
1142 for (vent = table; vent->version != 0; ++vent) {
1143 const SSL_METHOD *method;
1145 if (vent->smeth == NULL ||
1146 version_cmp(s, client_version, vent->version) < 0)
1148 method = vent->smeth();
1149 if (ssl_method_error(s, method) == 0) {
1150 s->version = vent->version;
1156 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1160 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1161 * server HELLO is received to select the final client protocol version and
1162 * the version specific method.
1164 * @s: client SSL handle.
1165 * @version: The proposed version from the server's HELLO.
1167 * Returns 0 on success or an SSL error reason number on failure.
1169 int ssl_choose_client_version(SSL *s, int version)
1171 const version_info *vent;
1172 const version_info *table;
1174 /* TODO(TLS1.3): Remove this before release */
1175 if (version == TLS1_3_VERSION_DRAFT)
1176 version = TLS1_3_VERSION;
1178 switch (s->method->version) {
1180 if (version != s->version)
1181 return SSL_R_WRONG_SSL_VERSION;
1183 * If this SSL handle is not from a version flexible method we don't
1184 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1185 * that's OK. It is up to the caller to not choose fixed protocol
1186 * versions they don't want. If not, then easy to fix, just return
1187 * ssl_method_error(s, s->method)
1190 case TLS_ANY_VERSION:
1191 table = tls_version_table;
1193 case DTLS_ANY_VERSION:
1194 table = dtls_version_table;
1198 for (vent = table; vent->version != 0; ++vent) {
1199 const SSL_METHOD *method;
1202 if (version != vent->version)
1204 if (vent->cmeth == NULL)
1206 method = vent->cmeth();
1207 err = ssl_method_error(s, method);
1211 s->version = version;
1215 return SSL_R_UNSUPPORTED_PROTOCOL;
1219 * ssl_get_client_min_max_version - get minimum and maximum client version
1220 * @s: The SSL connection
1221 * @min_version: The minimum supported version
1222 * @max_version: The maximum supported version
1224 * Work out what version we should be using for the initial ClientHello if the
1225 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1226 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1227 * or FIPS_mode() constraints and any floor imposed by the security level here,
1228 * so we don't advertise the wrong protocol version to only reject the outcome later.
1230 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1231 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1232 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1234 * Returns 0 on success or an SSL error reason number on failure. On failure
1235 * min_version and max_version will also be set to 0.
1237 int ssl_get_client_min_max_version(const SSL *s, int *min_version,
1242 const SSL_METHOD *single = NULL;
1243 const SSL_METHOD *method;
1244 const version_info *table;
1245 const version_info *vent;
1247 switch (s->method->version) {
1250 * If this SSL handle is not from a version flexible method we don't
1251 * (and never did) check min/max FIPS or Suite B constraints. Hope
1252 * that's OK. It is up to the caller to not choose fixed protocol
1253 * versions they don't want. If not, then easy to fix, just return
1254 * ssl_method_error(s, s->method)
1256 *min_version = *max_version = s->version;
1258 case TLS_ANY_VERSION:
1259 table = tls_version_table;
1261 case DTLS_ANY_VERSION:
1262 table = dtls_version_table;
1267 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1268 * below X enabled. This is required in order to maintain the "version
1269 * capability" vector contiguous. Any versions with a NULL client method
1270 * (protocol version client is disabled at compile-time) is also a "hole".
1272 * Our initial state is hole == 1, version == 0. That is, versions above
1273 * the first version in the method table are disabled (a "hole" above
1274 * the valid protocol entries) and we don't have a selected version yet.
1276 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1277 * the selected version, and the method becomes a candidate "single"
1278 * method. We're no longer in a hole, so "hole" becomes 0.
1280 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1281 * as we support a contiguous range of at least two methods. If we hit
1282 * a disabled method, then hole becomes true again, but nothing else
1283 * changes yet, because all the remaining methods may be disabled too.
1284 * If we again hit an enabled method after the new hole, it becomes
1285 * selected, as we start from scratch.
1287 *min_version = version = 0;
1289 for (vent = table; vent->version != 0; ++vent) {
1291 * A table entry with a NULL client method is still a hole in the
1292 * "version capability" vector.
1294 if (vent->cmeth == NULL) {
1298 method = vent->cmeth();
1299 if (ssl_method_error(s, method) != 0) {
1303 *min_version = method->version;
1305 version = (single = method)->version;
1306 *min_version = version;
1311 *max_version = version;
1313 /* Fail if everything is disabled */
1315 return SSL_R_NO_PROTOCOLS_AVAILABLE;
1321 * ssl_set_client_hello_version - Work out what version we should be using for
1322 * the initial ClientHello.legacy_version field.
1324 * @s: client SSL handle.
1326 * Returns 0 on success or an SSL error reason number on failure.
1328 int ssl_set_client_hello_version(SSL *s)
1330 int ver_min, ver_max, ret;
1332 ret = ssl_get_client_min_max_version(s, &ver_min, &ver_max);
1337 s->version = ver_max;
1339 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
1340 if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
1341 ver_max = TLS1_2_VERSION;
1343 s->client_version = ver_max;