xref: /dragonfly/crypto/libressl/ssl/ssl_ciphers.c (revision de0e0e4d)
1*de0e0e4dSAntonio Huete Jimenez /*	$OpenBSD: ssl_ciphers.c,v 1.15 2022/07/02 16:31:04 tb Exp $ */
272c33676SMaxim Ag /*
372c33676SMaxim Ag  * Copyright (c) 2015-2017 Doug Hogan <doug@openbsd.org>
48edacedfSDaniel Fojt  * Copyright (c) 2015-2018, 2020 Joel Sing <jsing@openbsd.org>
572c33676SMaxim Ag  * Copyright (c) 2019 Theo Buehler <tb@openbsd.org>
672c33676SMaxim Ag  *
772c33676SMaxim Ag  * Permission to use, copy, modify, and distribute this software for any
872c33676SMaxim Ag  * purpose with or without fee is hereby granted, provided that the above
972c33676SMaxim Ag  * copyright notice and this permission notice appear in all copies.
1072c33676SMaxim Ag  *
1172c33676SMaxim Ag  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1272c33676SMaxim Ag  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1372c33676SMaxim Ag  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1472c33676SMaxim Ag  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1572c33676SMaxim Ag  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1672c33676SMaxim Ag  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1772c33676SMaxim Ag  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1872c33676SMaxim Ag  */
1972c33676SMaxim Ag 
2072c33676SMaxim Ag #include <openssl/safestack.h>
2172c33676SMaxim Ag 
2272c33676SMaxim Ag #include "bytestring.h"
2372c33676SMaxim Ag #include "ssl_locl.h"
2472c33676SMaxim Ag 
2572c33676SMaxim Ag int
ssl_cipher_in_list(STACK_OF (SSL_CIPHER)* ciphers,const SSL_CIPHER * cipher)268edacedfSDaniel Fojt ssl_cipher_in_list(STACK_OF(SSL_CIPHER) *ciphers, const SSL_CIPHER *cipher)
278edacedfSDaniel Fojt {
288edacedfSDaniel Fojt 	int i;
298edacedfSDaniel Fojt 
308edacedfSDaniel Fojt 	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
318edacedfSDaniel Fojt 		if (sk_SSL_CIPHER_value(ciphers, i)->id == cipher->id)
328edacedfSDaniel Fojt 			return 1;
338edacedfSDaniel Fojt 	}
348edacedfSDaniel Fojt 
358edacedfSDaniel Fojt 	return 0;
368edacedfSDaniel Fojt }
378edacedfSDaniel Fojt 
388edacedfSDaniel Fojt int
ssl_cipher_allowed_in_tls_version_range(const SSL_CIPHER * cipher,uint16_t min_ver,uint16_t max_ver)39*de0e0e4dSAntonio Huete Jimenez ssl_cipher_allowed_in_tls_version_range(const SSL_CIPHER *cipher, uint16_t min_ver,
4072c33676SMaxim Ag     uint16_t max_ver)
4172c33676SMaxim Ag {
4272c33676SMaxim Ag 	switch(cipher->algorithm_ssl) {
4372c33676SMaxim Ag 	case SSL_SSLV3:
44*de0e0e4dSAntonio Huete Jimenez 		return (min_ver <= TLS1_2_VERSION);
4572c33676SMaxim Ag 	case SSL_TLSV1_2:
46*de0e0e4dSAntonio Huete Jimenez 		return (min_ver <= TLS1_2_VERSION && TLS1_2_VERSION <= max_ver);
4772c33676SMaxim Ag 	case SSL_TLSV1_3:
48*de0e0e4dSAntonio Huete Jimenez 		return (min_ver <= TLS1_3_VERSION && TLS1_3_VERSION <= max_ver);
4972c33676SMaxim Ag 	}
5072c33676SMaxim Ag 	return 0;
5172c33676SMaxim Ag }
5272c33676SMaxim Ag 
5372c33676SMaxim Ag int
ssl_cipher_list_to_bytes(SSL * s,STACK_OF (SSL_CIPHER)* ciphers,CBB * cbb)5472c33676SMaxim Ag ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *ciphers, CBB *cbb)
5572c33676SMaxim Ag {
5672c33676SMaxim Ag 	SSL_CIPHER *cipher;
5772c33676SMaxim Ag 	int num_ciphers = 0;
5872c33676SMaxim Ag 	uint16_t min_vers, max_vers;
5972c33676SMaxim Ag 	int i;
6072c33676SMaxim Ag 
6172c33676SMaxim Ag 	if (ciphers == NULL)
6272c33676SMaxim Ag 		return 0;
6372c33676SMaxim Ag 
64*de0e0e4dSAntonio Huete Jimenez 	if (!ssl_supported_tls_version_range(s, &min_vers, &max_vers))
6572c33676SMaxim Ag 		return 0;
6672c33676SMaxim Ag 
6772c33676SMaxim Ag 	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
6872c33676SMaxim Ag 		if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL)
6972c33676SMaxim Ag 			return 0;
70*de0e0e4dSAntonio Huete Jimenez 		if (!ssl_cipher_allowed_in_tls_version_range(cipher, min_vers,
718edacedfSDaniel Fojt 		    max_vers))
7272c33676SMaxim Ag 			continue;
73*de0e0e4dSAntonio Huete Jimenez 		if (!ssl_security_cipher_check(s, cipher))
74*de0e0e4dSAntonio Huete Jimenez 			continue;
7572c33676SMaxim Ag 		if (!CBB_add_u16(cbb, ssl3_cipher_get_value(cipher)))
7672c33676SMaxim Ag 			return 0;
7772c33676SMaxim Ag 
7872c33676SMaxim Ag 		num_ciphers++;
7972c33676SMaxim Ag 	}
8072c33676SMaxim Ag 
8172c33676SMaxim Ag 	/* Add SCSV if there are other ciphers and we're not renegotiating. */
8272c33676SMaxim Ag 	if (num_ciphers > 0 && !s->internal->renegotiate) {
8372c33676SMaxim Ag 		if (!CBB_add_u16(cbb, SSL3_CK_SCSV & SSL3_CK_VALUE_MASK))
8472c33676SMaxim Ag 			return 0;
8572c33676SMaxim Ag 	}
8672c33676SMaxim Ag 
8772c33676SMaxim Ag 	if (!CBB_flush(cbb))
8872c33676SMaxim Ag 		return 0;
8972c33676SMaxim Ag 
9072c33676SMaxim Ag 	return 1;
9172c33676SMaxim Ag }
9272c33676SMaxim Ag 
STACK_OF(SSL_CIPHER)9372c33676SMaxim Ag STACK_OF(SSL_CIPHER) *
9472c33676SMaxim Ag ssl_bytes_to_cipher_list(SSL *s, CBS *cbs)
9572c33676SMaxim Ag {
9672c33676SMaxim Ag 	STACK_OF(SSL_CIPHER) *ciphers = NULL;
9772c33676SMaxim Ag 	const SSL_CIPHER *cipher;
98*de0e0e4dSAntonio Huete Jimenez 	uint16_t cipher_value;
9972c33676SMaxim Ag 	unsigned long cipher_id;
10072c33676SMaxim Ag 
101*de0e0e4dSAntonio Huete Jimenez 	s->s3->send_connection_binding = 0;
10272c33676SMaxim Ag 
10372c33676SMaxim Ag 	if ((ciphers = sk_SSL_CIPHER_new_null()) == NULL) {
10472c33676SMaxim Ag 		SSLerror(s, ERR_R_MALLOC_FAILURE);
10572c33676SMaxim Ag 		goto err;
10672c33676SMaxim Ag 	}
10772c33676SMaxim Ag 
10872c33676SMaxim Ag 	while (CBS_len(cbs) > 0) {
10972c33676SMaxim Ag 		if (!CBS_get_u16(cbs, &cipher_value)) {
11072c33676SMaxim Ag 			SSLerror(s, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
11172c33676SMaxim Ag 			goto err;
11272c33676SMaxim Ag 		}
11372c33676SMaxim Ag 
11472c33676SMaxim Ag 		cipher_id = SSL3_CK_ID | cipher_value;
11572c33676SMaxim Ag 
116cca6fc52SDaniel Fojt 		if (cipher_id == SSL3_CK_SCSV) {
11772c33676SMaxim Ag 			/*
11872c33676SMaxim Ag 			 * TLS_EMPTY_RENEGOTIATION_INFO_SCSV is fatal if
11972c33676SMaxim Ag 			 * renegotiating.
12072c33676SMaxim Ag 			 */
12172c33676SMaxim Ag 			if (s->internal->renegotiate) {
12272c33676SMaxim Ag 				SSLerror(s, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
12372c33676SMaxim Ag 				ssl3_send_alert(s, SSL3_AL_FATAL,
12472c33676SMaxim Ag 				    SSL_AD_HANDSHAKE_FAILURE);
12572c33676SMaxim Ag 
12672c33676SMaxim Ag 				goto err;
12772c33676SMaxim Ag 			}
128*de0e0e4dSAntonio Huete Jimenez 			s->s3->send_connection_binding = 1;
12972c33676SMaxim Ag 			continue;
13072c33676SMaxim Ag 		}
13172c33676SMaxim Ag 
13272c33676SMaxim Ag 		if (cipher_id == SSL3_CK_FALLBACK_SCSV) {
13372c33676SMaxim Ag 			/*
13472c33676SMaxim Ag 			 * TLS_FALLBACK_SCSV indicates that the client
13572c33676SMaxim Ag 			 * previously tried a higher protocol version.
13672c33676SMaxim Ag 			 * Fail if the current version is an unexpected
13772c33676SMaxim Ag 			 * downgrade.
13872c33676SMaxim Ag 			 */
139*de0e0e4dSAntonio Huete Jimenez 			if (s->s3->hs.negotiated_tls_version <
140*de0e0e4dSAntonio Huete Jimenez 			    s->s3->hs.our_max_tls_version) {
14172c33676SMaxim Ag 				SSLerror(s, SSL_R_INAPPROPRIATE_FALLBACK);
14272c33676SMaxim Ag 				ssl3_send_alert(s, SSL3_AL_FATAL,
14372c33676SMaxim Ag 					SSL_AD_INAPPROPRIATE_FALLBACK);
14472c33676SMaxim Ag 				goto err;
14572c33676SMaxim Ag 			}
14672c33676SMaxim Ag 			continue;
14772c33676SMaxim Ag 		}
14872c33676SMaxim Ag 
14972c33676SMaxim Ag 		if ((cipher = ssl3_get_cipher_by_value(cipher_value)) != NULL) {
15072c33676SMaxim Ag 			if (!sk_SSL_CIPHER_push(ciphers, cipher)) {
15172c33676SMaxim Ag 				SSLerror(s, ERR_R_MALLOC_FAILURE);
15272c33676SMaxim Ag 				goto err;
15372c33676SMaxim Ag 			}
15472c33676SMaxim Ag 		}
15572c33676SMaxim Ag 	}
15672c33676SMaxim Ag 
15772c33676SMaxim Ag 	return (ciphers);
15872c33676SMaxim Ag 
15972c33676SMaxim Ag  err:
16072c33676SMaxim Ag 	sk_SSL_CIPHER_free(ciphers);
16172c33676SMaxim Ag 
16272c33676SMaxim Ag 	return (NULL);
16372c33676SMaxim Ag }
1648edacedfSDaniel Fojt 
1658edacedfSDaniel Fojt struct ssl_tls13_ciphersuite {
1668edacedfSDaniel Fojt 	const char *name;
1678edacedfSDaniel Fojt 	const char *alias;
1688edacedfSDaniel Fojt 	unsigned long cid;
1698edacedfSDaniel Fojt };
1708edacedfSDaniel Fojt 
1718edacedfSDaniel Fojt static const struct ssl_tls13_ciphersuite ssl_tls13_ciphersuites[] = {
1728edacedfSDaniel Fojt 	{
173*de0e0e4dSAntonio Huete Jimenez 		.name = TLS1_3_RFC_AES_128_GCM_SHA256,
174*de0e0e4dSAntonio Huete Jimenez 		.alias = TLS1_3_TXT_AES_128_GCM_SHA256,
1758edacedfSDaniel Fojt 		.cid = TLS1_3_CK_AES_128_GCM_SHA256,
1768edacedfSDaniel Fojt 	},
1778edacedfSDaniel Fojt 	{
178*de0e0e4dSAntonio Huete Jimenez 		.name = TLS1_3_RFC_AES_256_GCM_SHA384,
179*de0e0e4dSAntonio Huete Jimenez 		.alias = TLS1_3_TXT_AES_256_GCM_SHA384,
1808edacedfSDaniel Fojt 		.cid = TLS1_3_CK_AES_256_GCM_SHA384,
1818edacedfSDaniel Fojt 	},
1828edacedfSDaniel Fojt 	{
183*de0e0e4dSAntonio Huete Jimenez 		.name = TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
184*de0e0e4dSAntonio Huete Jimenez 		.alias = TLS1_3_TXT_CHACHA20_POLY1305_SHA256,
1858edacedfSDaniel Fojt 		.cid = TLS1_3_CK_CHACHA20_POLY1305_SHA256,
1868edacedfSDaniel Fojt 	},
1878edacedfSDaniel Fojt 	{
188*de0e0e4dSAntonio Huete Jimenez 		.name = TLS1_3_RFC_AES_128_CCM_SHA256,
189*de0e0e4dSAntonio Huete Jimenez 		.alias = TLS1_3_TXT_AES_128_CCM_SHA256,
1908edacedfSDaniel Fojt 		.cid = TLS1_3_CK_AES_128_CCM_SHA256,
1918edacedfSDaniel Fojt 	},
1928edacedfSDaniel Fojt 	{
193*de0e0e4dSAntonio Huete Jimenez 		.name = TLS1_3_RFC_AES_128_CCM_8_SHA256,
194*de0e0e4dSAntonio Huete Jimenez 		.alias = TLS1_3_TXT_AES_128_CCM_8_SHA256,
1958edacedfSDaniel Fojt 		.cid = TLS1_3_CK_AES_128_CCM_8_SHA256,
1968edacedfSDaniel Fojt 	},
1978edacedfSDaniel Fojt 	{
1988edacedfSDaniel Fojt 		.name = NULL,
1998edacedfSDaniel Fojt 	},
2008edacedfSDaniel Fojt };
2018edacedfSDaniel Fojt 
2028edacedfSDaniel Fojt int
ssl_parse_ciphersuites(STACK_OF (SSL_CIPHER)** out_ciphers,const char * str)2038edacedfSDaniel Fojt ssl_parse_ciphersuites(STACK_OF(SSL_CIPHER) **out_ciphers, const char *str)
2048edacedfSDaniel Fojt {
2058edacedfSDaniel Fojt 	const struct ssl_tls13_ciphersuite *ciphersuite;
2068edacedfSDaniel Fojt 	STACK_OF(SSL_CIPHER) *ciphers;
2078edacedfSDaniel Fojt 	const SSL_CIPHER *cipher;
2088edacedfSDaniel Fojt 	char *s = NULL;
2098edacedfSDaniel Fojt 	char *p, *q;
2108edacedfSDaniel Fojt 	int i;
2118edacedfSDaniel Fojt 	int ret = 0;
2128edacedfSDaniel Fojt 
2138edacedfSDaniel Fojt 	if ((ciphers = sk_SSL_CIPHER_new_null()) == NULL)
2148edacedfSDaniel Fojt 		goto err;
2158edacedfSDaniel Fojt 
2168edacedfSDaniel Fojt 	/* An empty string is valid and means no ciphers. */
2178edacedfSDaniel Fojt 	if (strcmp(str, "") == 0)
2188edacedfSDaniel Fojt 		goto done;
2198edacedfSDaniel Fojt 
2208edacedfSDaniel Fojt 	if ((s = strdup(str)) == NULL)
2218edacedfSDaniel Fojt 		goto err;
2228edacedfSDaniel Fojt 
2238edacedfSDaniel Fojt 	q = s;
2248edacedfSDaniel Fojt 	while ((p = strsep(&q, ":")) != NULL) {
2258edacedfSDaniel Fojt 		ciphersuite = &ssl_tls13_ciphersuites[0];
2268edacedfSDaniel Fojt 		for (i = 0; ciphersuite->name != NULL; i++) {
2278edacedfSDaniel Fojt 			if (strcmp(p, ciphersuite->name) == 0)
2288edacedfSDaniel Fojt 				break;
2298edacedfSDaniel Fojt 			if (strcmp(p, ciphersuite->alias) == 0)
2308edacedfSDaniel Fojt 				break;
2318edacedfSDaniel Fojt 			ciphersuite = &ssl_tls13_ciphersuites[i];
2328edacedfSDaniel Fojt 		}
2338edacedfSDaniel Fojt 		if (ciphersuite->name == NULL)
2348edacedfSDaniel Fojt 			goto err;
2358edacedfSDaniel Fojt 
2368edacedfSDaniel Fojt 		/* We know about the cipher suite, but it is not supported. */
2378edacedfSDaniel Fojt 		if ((cipher = ssl3_get_cipher_by_id(ciphersuite->cid)) == NULL)
2388edacedfSDaniel Fojt 			continue;
2398edacedfSDaniel Fojt 
2408edacedfSDaniel Fojt 		if (!sk_SSL_CIPHER_push(ciphers, cipher))
2418edacedfSDaniel Fojt 			goto err;
2428edacedfSDaniel Fojt 	}
2438edacedfSDaniel Fojt 
2448edacedfSDaniel Fojt  done:
2458edacedfSDaniel Fojt 	sk_SSL_CIPHER_free(*out_ciphers);
2468edacedfSDaniel Fojt 	*out_ciphers = ciphers;
2478edacedfSDaniel Fojt 	ciphers = NULL;
2488edacedfSDaniel Fojt 	ret = 1;
2498edacedfSDaniel Fojt 
2508edacedfSDaniel Fojt  err:
2518edacedfSDaniel Fojt 	sk_SSL_CIPHER_free(ciphers);
2528edacedfSDaniel Fojt 	free(s);
2538edacedfSDaniel Fojt 
2548edacedfSDaniel Fojt 	return ret;
2558edacedfSDaniel Fojt }
2568edacedfSDaniel Fojt 
2578edacedfSDaniel Fojt int
ssl_merge_cipherlists(STACK_OF (SSL_CIPHER)* cipherlist,STACK_OF (SSL_CIPHER)* cipherlist_tls13,STACK_OF (SSL_CIPHER)** out_cipherlist)2588edacedfSDaniel Fojt ssl_merge_cipherlists(STACK_OF(SSL_CIPHER) *cipherlist,
2598edacedfSDaniel Fojt     STACK_OF(SSL_CIPHER) *cipherlist_tls13,
2608edacedfSDaniel Fojt     STACK_OF(SSL_CIPHER) **out_cipherlist)
2618edacedfSDaniel Fojt {
2628edacedfSDaniel Fojt 	STACK_OF(SSL_CIPHER) *ciphers = NULL;
2638edacedfSDaniel Fojt 	const SSL_CIPHER *cipher;
2648edacedfSDaniel Fojt 	int i, ret = 0;
2658edacedfSDaniel Fojt 
2668edacedfSDaniel Fojt 	if ((ciphers = sk_SSL_CIPHER_dup(cipherlist_tls13)) == NULL)
2678edacedfSDaniel Fojt 		goto err;
2688edacedfSDaniel Fojt 	for (i = 0; i < sk_SSL_CIPHER_num(cipherlist); i++) {
2698edacedfSDaniel Fojt 		cipher = sk_SSL_CIPHER_value(cipherlist, i);
2708edacedfSDaniel Fojt 		if (cipher->algorithm_ssl == SSL_TLSV1_3)
2718edacedfSDaniel Fojt 			continue;
2728edacedfSDaniel Fojt 		if (!sk_SSL_CIPHER_push(ciphers, cipher))
2738edacedfSDaniel Fojt 			goto err;
2748edacedfSDaniel Fojt 	}
2758edacedfSDaniel Fojt 
2768edacedfSDaniel Fojt 	sk_SSL_CIPHER_free(*out_cipherlist);
2778edacedfSDaniel Fojt 	*out_cipherlist = ciphers;
2788edacedfSDaniel Fojt 	ciphers = NULL;
2798edacedfSDaniel Fojt 
2808edacedfSDaniel Fojt 	ret = 1;
2818edacedfSDaniel Fojt 
2828edacedfSDaniel Fojt  err:
2838edacedfSDaniel Fojt 	sk_SSL_CIPHER_free(ciphers);
2848edacedfSDaniel Fojt 
2858edacedfSDaniel Fojt 	return ret;
2868edacedfSDaniel Fojt }
287