1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
110 /* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
119 #include "../ssl_locl.h"
120 #include "statem_locl.h"
121 #include <openssl/buffer.h>
122 #include <openssl/rand.h>
123 #include <openssl/objects.h>
124 #include <openssl/evp.h>
125 #include <openssl/x509.h>
128 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
129 * SSL3_RT_CHANGE_CIPHER_SPEC)
131 int ssl3_do_write(SSL *s, int type)
135 ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
139 if (type == SSL3_RT_HANDSHAKE)
141 * should not be done for 'Hello Request's, but in that case we'll
142 * ignore the result anyway
144 ssl3_finish_mac(s, (unsigned char *)&s->init_buf->data[s->init_off],
147 if (ret == s->init_num) {
149 s->msg_callback(1, s->version, type, s->init_buf->data,
150 (size_t)(s->init_off + s->init_num), s,
151 s->msg_callback_arg);
159 int tls_construct_finished(SSL *s, const char *sender, int slen)
165 p = ssl_handshake_start(s);
167 i = s->method->ssl3_enc->final_finish_mac(s,
169 s->s3->tmp.finish_md);
172 s->s3->tmp.finish_md_len = i;
173 memcpy(p, s->s3->tmp.finish_md, i);
177 * Copy the finished so we can use it for renegotiation checks
180 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
181 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, i);
182 s->s3->previous_client_finished_len = i;
184 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
185 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, i);
186 s->s3->previous_server_finished_len = i;
189 if (!ssl_set_handshake_header(s, SSL3_MT_FINISHED, l)) {
190 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
197 #ifndef OPENSSL_NO_NEXTPROTONEG
199 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
202 static void ssl3_take_mac(SSL *s)
207 * If no new cipher setup return immediately: other functions will set
208 * the appropriate error.
210 if (s->s3->tmp.new_cipher == NULL)
213 sender = s->method->ssl3_enc->server_finished_label;
214 slen = s->method->ssl3_enc->server_finished_label_len;
216 sender = s->method->ssl3_enc->client_finished_label;
217 slen = s->method->ssl3_enc->client_finished_label_len;
220 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
223 s->s3->tmp.peer_finish_md);
227 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
232 remain = PACKET_remaining(pkt);
234 * 'Change Cipher Spec' is just a single byte, which should already have
235 * been consumed by ssl_get_message() so there should be no bytes left,
236 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
238 if (SSL_IS_DTLS(s)) {
239 if ((s->version == DTLS1_BAD_VER
240 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
241 || (s->version != DTLS1_BAD_VER
242 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
243 al = SSL_AD_ILLEGAL_PARAMETER;
244 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
245 SSL_R_BAD_CHANGE_CIPHER_SPEC);
250 al = SSL_AD_ILLEGAL_PARAMETER;
251 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
252 SSL_R_BAD_CHANGE_CIPHER_SPEC);
257 /* Check we have a cipher to change to */
258 if (s->s3->tmp.new_cipher == NULL) {
259 al = SSL_AD_UNEXPECTED_MESSAGE;
260 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
264 s->s3->change_cipher_spec = 1;
265 if (!ssl3_do_change_cipher_spec(s)) {
266 al = SSL_AD_INTERNAL_ERROR;
267 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
271 if (SSL_IS_DTLS(s)) {
272 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
274 if (s->version == DTLS1_BAD_VER)
275 s->d1->handshake_read_seq++;
277 #ifndef OPENSSL_NO_SCTP
279 * Remember that a CCS has been received, so that an old key of
280 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
283 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
287 return MSG_PROCESS_CONTINUE_READING;
289 ssl3_send_alert(s, SSL3_AL_FATAL, al);
290 ossl_statem_set_error(s);
291 return MSG_PROCESS_ERROR;
294 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
298 /* If this occurs, we have missed a message */
299 if (!s->s3->change_cipher_spec) {
300 al = SSL_AD_UNEXPECTED_MESSAGE;
301 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
304 s->s3->change_cipher_spec = 0;
306 i = s->s3->tmp.peer_finish_md_len;
308 if ((unsigned long)i != PACKET_remaining(pkt)) {
309 al = SSL_AD_DECODE_ERROR;
310 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
314 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md, i) != 0) {
315 al = SSL_AD_DECRYPT_ERROR;
316 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
321 * Copy the finished so we can use it for renegotiation checks
324 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
325 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, i);
326 s->s3->previous_client_finished_len = i;
328 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
329 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, i);
330 s->s3->previous_server_finished_len = i;
333 return MSG_PROCESS_FINISHED_READING;
335 ssl3_send_alert(s, SSL3_AL_FATAL, al);
336 ossl_statem_set_error(s);
337 return MSG_PROCESS_ERROR;
340 int tls_construct_change_cipher_spec(SSL *s)
344 p = (unsigned char *)s->init_buf->data;
352 unsigned long ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk)
355 unsigned long l = 3 + SSL_HM_HEADER_LENGTH(s);
357 if (!ssl_add_cert_chain(s, cpk, &l))
360 l -= 3 + SSL_HM_HEADER_LENGTH(s);
361 p = ssl_handshake_start(s);
365 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, l)) {
366 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR);
369 return l + SSL_HM_HEADER_LENGTH(s);
372 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst)
374 void (*cb) (const SSL *ssl, int type, int val) = NULL;
376 #ifndef OPENSSL_NO_SCTP
377 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
379 ret = dtls_wait_for_dry(s);
380 if (ret != WORK_FINISHED_CONTINUE)
385 /* clean a few things up */
386 ssl3_cleanup_key_block(s);
388 if (!SSL_IS_DTLS(s)) {
390 * We don't do this in DTLS because we may still need the init_buf
391 * in case there are any unexpected retransmits
393 BUF_MEM_free(s->init_buf);
397 ssl_free_wbio_buffer(s);
401 if (!s->server || s->renegotiate == 2) {
402 /* skipped if we just sent a HelloRequest */
407 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
409 s->ctx->stats.sess_accept_good++;
410 s->handshake_func = ossl_statem_accept;
412 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
414 s->ctx->stats.sess_hit++;
416 s->handshake_func = ossl_statem_connect;
417 s->ctx->stats.sess_connect_good++;
420 if (s->info_callback != NULL)
421 cb = s->info_callback;
422 else if (s->ctx->info_callback != NULL)
423 cb = s->ctx->info_callback;
426 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
428 if (SSL_IS_DTLS(s)) {
429 /* done with handshaking */
430 s->d1->handshake_read_seq = 0;
431 s->d1->handshake_write_seq = 0;
432 s->d1->next_handshake_write_seq = 0;
436 return WORK_FINISHED_STOP;
439 int tls_get_message_header(SSL *s, int *mt)
441 /* s->init_num < SSL3_HM_HEADER_LENGTH */
442 int skip_message, i, recvd_type, al;
446 p = (unsigned char *)s->init_buf->data;
449 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
450 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
451 &p[s->init_num], SSL3_HM_HEADER_LENGTH - s->init_num, 0);
453 s->rwstate = SSL_READING;
456 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
457 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
459 s->s3->tmp.message_size = i;
461 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
462 al = SSL_AD_UNEXPECTED_MESSAGE;
463 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY);
471 if (p[0] == SSL3_MT_HELLO_REQUEST)
473 * The server may always send 'Hello Request' messages --
474 * we are doing a handshake anyway now, so ignore them if
475 * their format is correct. Does not count for 'Finished'
478 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
483 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
484 p, SSL3_HM_HEADER_LENGTH, s,
485 s->msg_callback_arg);
487 } while (skip_message);
488 /* s->init_num == SSL3_HM_HEADER_LENGTH */
491 s->s3->tmp.message_type = *(p++);
493 if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
495 * Only happens with SSLv3+ in an SSLv2 backward compatible
499 * Total message size is the remaining record bytes to read
500 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
502 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
503 + SSL3_HM_HEADER_LENGTH;
504 if (l && !BUF_MEM_grow_clean(s->init_buf, (int)l)) {
505 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
508 s->s3->tmp.message_size = l;
510 s->init_msg = s->init_buf->data;
511 s->init_num = SSL3_HM_HEADER_LENGTH;
514 /* BUF_MEM_grow takes an 'int' parameter */
515 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
516 al = SSL_AD_ILLEGAL_PARAMETER;
517 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
520 if (l && !BUF_MEM_grow_clean(s->init_buf,
521 (int)l + SSL3_HM_HEADER_LENGTH)) {
522 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
525 s->s3->tmp.message_size = l;
527 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
533 ssl3_send_alert(s, SSL3_AL_FATAL, al);
538 int tls_get_message_body(SSL *s, unsigned long *len)
544 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
545 /* We've already read everything in */
546 *len = (unsigned long)s->init_num;
551 n = s->s3->tmp.message_size - s->init_num;
553 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
554 &p[s->init_num], n, 0);
556 s->rwstate = SSL_READING;
564 #ifndef OPENSSL_NO_NEXTPROTONEG
566 * If receiving Finished, record MAC of prior handshake messages for
567 * Finished verification.
569 if (*s->init_buf->data == SSL3_MT_FINISHED)
573 /* Feed this message into MAC computation. */
574 if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
575 ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num);
577 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
578 (size_t)s->init_num, s, s->msg_callback_arg);
580 ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
581 s->init_num + SSL3_HM_HEADER_LENGTH);
583 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
584 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
585 s->msg_callback_arg);
589 * init_num should never be negative...should probably be declared
592 if (s->init_num < 0) {
593 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_INTERNAL_ERROR);
594 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
598 *len = (unsigned long)s->init_num;
602 int ssl_cert_type(X509 *x, EVP_PKEY *pkey)
608 pk = X509_get_pubkey(x);
615 if (i == EVP_PKEY_RSA) {
616 ret = SSL_PKEY_RSA_ENC;
617 } else if (i == EVP_PKEY_DSA) {
618 ret = SSL_PKEY_DSA_SIGN;
620 #ifndef OPENSSL_NO_EC
621 else if (i == EVP_PKEY_EC) {
625 #ifndef OPENSSL_NO_GOST
626 else if (i == NID_id_GostR3410_2001) {
627 ret = SSL_PKEY_GOST01;
628 } else if (i == NID_id_GostR3410_2012_256) {
629 ret = SSL_PKEY_GOST12_256;
630 } else if (i == NID_id_GostR3410_2012_512) {
631 ret = SSL_PKEY_GOST12_512;
641 int ssl_verify_alarm_type(long type)
646 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
647 case X509_V_ERR_UNABLE_TO_GET_CRL:
648 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
649 al = SSL_AD_UNKNOWN_CA;
651 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
652 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
653 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
654 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
655 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
656 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
657 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
658 case X509_V_ERR_CERT_NOT_YET_VALID:
659 case X509_V_ERR_CRL_NOT_YET_VALID:
660 case X509_V_ERR_CERT_UNTRUSTED:
661 case X509_V_ERR_CERT_REJECTED:
662 al = SSL_AD_BAD_CERTIFICATE;
664 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
665 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
666 al = SSL_AD_DECRYPT_ERROR;
668 case X509_V_ERR_CERT_HAS_EXPIRED:
669 case X509_V_ERR_CRL_HAS_EXPIRED:
670 al = SSL_AD_CERTIFICATE_EXPIRED;
672 case X509_V_ERR_CERT_REVOKED:
673 al = SSL_AD_CERTIFICATE_REVOKED;
675 case X509_V_ERR_OUT_OF_MEM:
676 al = SSL_AD_INTERNAL_ERROR;
678 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
679 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
680 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
681 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
682 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
683 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
684 case X509_V_ERR_INVALID_CA:
685 al = SSL_AD_UNKNOWN_CA;
687 case X509_V_ERR_APPLICATION_VERIFICATION:
688 al = SSL_AD_HANDSHAKE_FAILURE;
690 case X509_V_ERR_INVALID_PURPOSE:
691 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
694 al = SSL_AD_CERTIFICATE_UNKNOWN;
700 int ssl_allow_compression(SSL *s)
702 if (s->options & SSL_OP_NO_COMPRESSION)
704 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
707 static int version_cmp(SSL *s, int a, int b)
709 int dtls = SSL_IS_DTLS(s);
714 return a < b ? -1 : 1;
715 return DTLS_VERSION_LT(a, b) ? -1 : 1;
720 const SSL_METHOD *(*cmeth)(void);
721 const SSL_METHOD *(*smeth)(void);
724 #if TLS_MAX_VERSION != TLS1_2_VERSION
725 # error Code needs update for TLS_method() support beyond TLS1_2_VERSION.
728 static const version_info tls_version_table[] = {
729 #ifndef OPENSSL_NO_TLS1_2
730 { TLS1_2_VERSION, TLSv1_2_client_method, TLSv1_2_server_method },
732 { TLS1_2_VERSION, NULL, NULL },
734 #ifndef OPENSSL_NO_TLS1_1
735 { TLS1_1_VERSION, TLSv1_1_client_method, TLSv1_1_server_method },
737 { TLS1_1_VERSION, NULL, NULL },
739 #ifndef OPENSSL_NO_TLS1
740 { TLS1_VERSION, TLSv1_client_method, TLSv1_server_method },
742 { TLS1_VERSION, NULL, NULL },
744 #ifndef OPENSSL_NO_SSL3
745 { SSL3_VERSION, SSLv3_client_method, SSLv3_server_method },
747 { SSL3_VERSION, NULL, NULL },
752 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
753 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
756 static const version_info dtls_version_table[] = {
757 #ifndef OPENSSL_NO_DTLS1_2
758 { DTLS1_2_VERSION, DTLSv1_2_client_method, DTLSv1_2_server_method },
760 { DTLS1_2_VERSION, NULL, NULL },
762 #ifndef OPENSSL_NO_DTLS1
763 { DTLS1_VERSION, DTLSv1_client_method, DTLSv1_server_method },
765 { DTLS1_VERSION, NULL, NULL },
771 * ssl_method_error - Check whether an SSL_METHOD is enabled.
773 * @s: The SSL handle for the candidate method
774 * @method: the intended method.
776 * Returns 0 on success, or an SSL error reason on failure.
778 static int ssl_method_error(SSL *s, const SSL_METHOD *method)
780 int version = method->version;
782 if ((s->min_proto_version != 0 &&
783 version_cmp(s, version, s->min_proto_version) < 0) ||
784 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
785 return SSL_R_VERSION_TOO_LOW;
787 if (s->max_proto_version != 0 &&
788 version_cmp(s, version, s->max_proto_version) > 0)
789 return SSL_R_VERSION_TOO_HIGH;
791 if ((s->options & method->mask) != 0)
792 return SSL_R_UNSUPPORTED_PROTOCOL;
793 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
794 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
795 else if ((method->flags & SSL_METHOD_NO_FIPS) != 0 && FIPS_mode())
796 return SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE;
802 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
803 * fallback indication from a client check whether we're using the highest
804 * supported protocol version.
806 * @s server SSL handle.
808 * Returns 1 when using the highest enabled version, 0 otherwise.
810 int ssl_check_version_downgrade(SSL *s)
812 const version_info *vent;
813 const version_info *table;
816 * Check that the current protocol is the highest enabled version
817 * (according to s->ctx->method, as version negotiation may have changed
820 if (s->version == s->ctx->method->version)
824 * Apparently we're using a version-flexible SSL_METHOD (not at its
825 * highest protocol version).
827 if (s->ctx->method->version == TLS_method()->version)
828 table = tls_version_table;
829 else if (s->ctx->method->version == DTLS_method()->version)
830 table = dtls_version_table;
832 /* Unexpected state; fail closed. */
836 for (vent = table; vent->version != 0; ++vent) {
837 if (vent->smeth != NULL &&
838 ssl_method_error(s, vent->smeth()) == 0)
839 return s->version == vent->version;
845 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
846 * protocols, provided the initial (D)TLS method is version-flexible. This
847 * function sanity-checks the proposed value and makes sure the method is
848 * version-flexible, then sets the limit if all is well.
850 * @method_version: The version of the current SSL_METHOD.
851 * @version: the intended limit.
852 * @bound: pointer to limit to be updated.
854 * Returns 1 on success, 0 on failure.
856 int ssl_set_version_bound(int method_version, int version, int *bound)
864 * Restrict TLS methods to TLS protocol versions.
865 * Restrict DTLS methods to DTLS protocol versions.
866 * Note, DTLS version numbers are decreasing, use comparison macros.
868 * Note that for both lower-bounds we use explicit versions, not
869 * (D)TLS_MIN_VERSION. This is because we don't want to break user
870 * configurations. If the MIN (supported) version ever rises, the user's
871 * "floor" remains valid even if no longer available. We don't expect the
872 * MAX ceiling to ever get lower, so making that variable makes sense.
874 switch (method_version) {
877 * XXX For fixed version methods, should we always fail and not set any
878 * bounds, always succeed and not set any bounds, or set the bounds and
879 * arrange to fail later if they are not met? At present fixed-version
880 * methods are not subject to controls that disable individual protocol
885 case TLS_ANY_VERSION:
886 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
890 case DTLS_ANY_VERSION:
891 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
892 DTLS_VERSION_LT(version, DTLS1_VERSION))
902 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
903 * client HELLO is received to select the final server protocol version and
904 * the version specific method.
906 * @s: server SSL handle.
908 * Returns 0 on success or an SSL error reason number on failure.
910 int ssl_choose_server_version(SSL *s)
913 * With version-flexible methods we have an initial state with:
915 * s->method->version == (D)TLS_ANY_VERSION,
916 * s->version == (D)TLS_MAX_VERSION.
918 * So we detect version-flexible methods via the method version, not the
921 int server_version = s->method->version;
922 int client_version = s->client_version;
923 const version_info *vent;
924 const version_info *table;
927 switch (server_version) {
929 if (version_cmp(s, client_version, s->version) < 0)
930 return SSL_R_WRONG_SSL_VERSION;
932 * If this SSL handle is not from a version flexible method we don't
933 * (and never did) check min/max FIPS or Suite B constraints. Hope
934 * that's OK. It is up to the caller to not choose fixed protocol
935 * versions they don't want. If not, then easy to fix, just return
936 * ssl_method_error(s, s->method)
939 case TLS_ANY_VERSION:
940 table = tls_version_table;
942 case DTLS_ANY_VERSION:
943 table = dtls_version_table;
947 for (vent = table; vent->version != 0; ++vent) {
948 const SSL_METHOD *method;
950 if (vent->smeth == NULL ||
951 version_cmp(s, client_version, vent->version) < 0)
953 method = vent->smeth();
954 if (ssl_method_error(s, method) == 0) {
955 s->version = vent->version;
961 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
965 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
966 * server HELLO is received to select the final client protocol version and
967 * the version specific method.
969 * @s: client SSL handle.
970 * @version: The proposed version from the server's HELLO.
972 * Returns 0 on success or an SSL error reason number on failure.
974 int ssl_choose_client_version(SSL *s, int version)
976 const version_info *vent;
977 const version_info *table;
979 switch (s->method->version) {
981 if (version != s->version)
982 return SSL_R_WRONG_SSL_VERSION;
984 * If this SSL handle is not from a version flexible method we don't
985 * (and never did) check min/max, FIPS or Suite B constraints. Hope
986 * that's OK. It is up to the caller to not choose fixed protocol
987 * versions they don't want. If not, then easy to fix, just return
988 * ssl_method_error(s, s->method)
990 s->session->ssl_version = s->version;
992 case TLS_ANY_VERSION:
993 table = tls_version_table;
995 case DTLS_ANY_VERSION:
996 table = dtls_version_table;
1000 for (vent = table; vent->version != 0; ++vent) {
1001 const SSL_METHOD *method;
1004 if (version != vent->version)
1006 if (vent->cmeth == NULL)
1008 method = vent->cmeth();
1009 err = ssl_method_error(s, method);
1013 s->session->ssl_version = s->version = version;
1017 return SSL_R_UNSUPPORTED_PROTOCOL;
1021 * ssl_set_client_hello_version - Work out what version we should be using for
1022 * the initial ClientHello if the version is initially (D)TLS_ANY_VERSION. We
1023 * apply any explicit SSL_OP_NO_xxx options, the MinProtocol and MaxProtocol
1024 * configuration commands, any Suite B or FIPS_mode() constraints and any floor
1025 * imposed by the security level here, so we don't advertise the wrong protocol
1026 * version to only reject the outcome later.
1028 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1029 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1030 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1032 * @s: client SSL handle.
1034 * Returns 0 on success or an SSL error reason number on failure.
1036 int ssl_set_client_hello_version(SSL *s)
1040 const SSL_METHOD *single = NULL;
1041 const SSL_METHOD *method;
1042 const version_info *table;
1043 const version_info *vent;
1045 switch (s->method->version) {
1048 * If this SSL handle is not from a version flexible method we don't
1049 * (and never did) check min/max FIPS or Suite B constraints. Hope
1050 * that's OK. It is up to the caller to not choose fixed protocol
1051 * versions they don't want. If not, then easy to fix, just return
1052 * ssl_method_error(s, s->method)
1054 s->client_version = s->version;
1056 case TLS_ANY_VERSION:
1057 table = tls_version_table;
1059 case DTLS_ANY_VERSION:
1060 table = dtls_version_table;
1065 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1066 * below X enabled. This is required in order to maintain the "version
1067 * capability" vector contiguous. Any versions with a NULL client method
1068 * (protocol version client is disabled at compile-time) is also a "hole".
1070 * Our initial state is hole == 1, version == 0. That is, versions above
1071 * the first version in the method table are disabled (a "hole" above
1072 * the valid protocol entries) and we don't have a selected version yet.
1074 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1075 * the selected version, and the method becomes a candidate "single"
1076 * method. We're no longer in a hole, so "hole" becomes 0.
1078 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1079 * as we support a contiguous range of at least two methods. If we hit
1080 * a disabled method, then hole becomes true again, but nothing else
1081 * changes yet, because all the remaining methods may be disabled too.
1082 * If we again hit an enabled method after the new hole, it becomes
1083 * selected, as we start from scratch.
1087 for (vent = table; vent->version != 0; ++vent) {
1089 * A table entry with a NULL client method is still a hole in the
1090 * "version capability" vector.
1092 if (vent->cmeth == NULL) {
1096 method = vent->cmeth();
1097 if (ssl_method_error(s, method) != 0) {
1102 version = (single = method)->version;
1107 /* Fail if everything is disabled */
1109 return SSL_R_NO_PROTOCOLS_AVAILABLE;
1113 s->client_version = s->version = version;