1*f4fe6251Sjsing /* $OpenBSD: ssl_ciphers.c,v 1.18 2024/07/22 14:47:15 jsing Exp $ */ 2a36841cfStb /* 35ceed53aStb * Copyright (c) 2015-2017 Doug Hogan <doug@openbsd.org> 461a9dc01Sjsing * Copyright (c) 2015-2018, 2020 Joel Sing <jsing@openbsd.org> 5a36841cfStb * Copyright (c) 2019 Theo Buehler <tb@openbsd.org> 6a36841cfStb * 7a36841cfStb * Permission to use, copy, modify, and distribute this software for any 8a36841cfStb * purpose with or without fee is hereby granted, provided that the above 9a36841cfStb * copyright notice and this permission notice appear in all copies. 10a36841cfStb * 11a36841cfStb * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12a36841cfStb * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13a36841cfStb * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14a36841cfStb * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15a36841cfStb * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16a36841cfStb * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17a36841cfStb * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18a36841cfStb */ 19a36841cfStb 205ceed53aStb #include <openssl/safestack.h> 215ceed53aStb 225ceed53aStb #include "bytestring.h" 23c9675a23Stb #include "ssl_local.h" 24a36841cfStb 25a36841cfStb int 268b316ce8Sjsing ssl_cipher_in_list(STACK_OF(SSL_CIPHER) *ciphers, const SSL_CIPHER *cipher) 278b316ce8Sjsing { 288b316ce8Sjsing int i; 298b316ce8Sjsing 308b316ce8Sjsing for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 31*f4fe6251Sjsing if (sk_SSL_CIPHER_value(ciphers, i)->value == cipher->value) 328b316ce8Sjsing return 1; 338b316ce8Sjsing } 348b316ce8Sjsing 358b316ce8Sjsing return 0; 368b316ce8Sjsing } 378b316ce8Sjsing 388b316ce8Sjsing int 39970acf87Sjsing ssl_cipher_allowed_in_tls_version_range(const SSL_CIPHER *cipher, uint16_t min_ver, 40a36841cfStb uint16_t max_ver) 41a36841cfStb { 42a36841cfStb switch(cipher->algorithm_ssl) { 43a36841cfStb case SSL_SSLV3: 44970acf87Sjsing return (min_ver <= TLS1_2_VERSION); 45a36841cfStb case SSL_TLSV1_2: 46970acf87Sjsing return (min_ver <= TLS1_2_VERSION && TLS1_2_VERSION <= max_ver); 47a36841cfStb case SSL_TLSV1_3: 48970acf87Sjsing return (min_ver <= TLS1_3_VERSION && TLS1_3_VERSION <= max_ver); 49a36841cfStb } 50a36841cfStb return 0; 51a36841cfStb } 525ceed53aStb 535ceed53aStb int 545ceed53aStb ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *ciphers, CBB *cbb) 555ceed53aStb { 565ceed53aStb SSL_CIPHER *cipher; 575ceed53aStb int num_ciphers = 0; 585ceed53aStb uint16_t min_vers, max_vers; 595ceed53aStb int i; 605ceed53aStb 615ceed53aStb if (ciphers == NULL) 625ceed53aStb return 0; 635ceed53aStb 64970acf87Sjsing if (!ssl_supported_tls_version_range(s, &min_vers, &max_vers)) 655ceed53aStb return 0; 665ceed53aStb 675ceed53aStb for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 685ceed53aStb if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL) 695ceed53aStb return 0; 70970acf87Sjsing if (!ssl_cipher_allowed_in_tls_version_range(cipher, min_vers, 71115de268Sjsing max_vers)) 725ceed53aStb continue; 731fd41f50Stb if (!ssl_security_cipher_check(s, cipher)) 7467f84b87Stb continue; 75*f4fe6251Sjsing if (!CBB_add_u16(cbb, cipher->value)) 765ceed53aStb return 0; 775ceed53aStb 785ceed53aStb num_ciphers++; 795ceed53aStb } 805ceed53aStb 815ceed53aStb /* Add SCSV if there are other ciphers and we're not renegotiating. */ 826f7f653bSjsing if (num_ciphers > 0 && !s->renegotiate) { 835ceed53aStb if (!CBB_add_u16(cbb, SSL3_CK_SCSV & SSL3_CK_VALUE_MASK)) 845ceed53aStb return 0; 855ceed53aStb } 865ceed53aStb 875ceed53aStb if (!CBB_flush(cbb)) 885ceed53aStb return 0; 895ceed53aStb 905ceed53aStb return 1; 915ceed53aStb } 925ceed53aStb 935ceed53aStb STACK_OF(SSL_CIPHER) * 945ceed53aStb ssl_bytes_to_cipher_list(SSL *s, CBS *cbs) 955ceed53aStb { 965ceed53aStb STACK_OF(SSL_CIPHER) *ciphers = NULL; 975ceed53aStb const SSL_CIPHER *cipher; 981927d779Sjsing uint16_t cipher_value; 995ceed53aStb unsigned long cipher_id; 1005ceed53aStb 10102876cc3Sjsing s->s3->send_connection_binding = 0; 1025ceed53aStb 1035ceed53aStb if ((ciphers = sk_SSL_CIPHER_new_null()) == NULL) { 1045ceed53aStb SSLerror(s, ERR_R_MALLOC_FAILURE); 1055ceed53aStb goto err; 1065ceed53aStb } 1075ceed53aStb 1085ceed53aStb while (CBS_len(cbs) > 0) { 1095ceed53aStb if (!CBS_get_u16(cbs, &cipher_value)) { 1105ceed53aStb SSLerror(s, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); 1115ceed53aStb goto err; 1125ceed53aStb } 1135ceed53aStb 1145ceed53aStb cipher_id = SSL3_CK_ID | cipher_value; 1155ceed53aStb 116370b9647Sbcook if (cipher_id == SSL3_CK_SCSV) { 1175ceed53aStb /* 1185ceed53aStb * TLS_EMPTY_RENEGOTIATION_INFO_SCSV is fatal if 1195ceed53aStb * renegotiating. 1205ceed53aStb */ 1216f7f653bSjsing if (s->renegotiate) { 1225ceed53aStb SSLerror(s, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); 1235ceed53aStb ssl3_send_alert(s, SSL3_AL_FATAL, 1245ceed53aStb SSL_AD_HANDSHAKE_FAILURE); 1255ceed53aStb 1265ceed53aStb goto err; 1275ceed53aStb } 12802876cc3Sjsing s->s3->send_connection_binding = 1; 1295ceed53aStb continue; 1305ceed53aStb } 1315ceed53aStb 1325ceed53aStb if (cipher_id == SSL3_CK_FALLBACK_SCSV) { 1335ceed53aStb /* 1345ceed53aStb * TLS_FALLBACK_SCSV indicates that the client 1355ceed53aStb * previously tried a higher protocol version. 1365ceed53aStb * Fail if the current version is an unexpected 1375ceed53aStb * downgrade. 1385ceed53aStb */ 13902876cc3Sjsing if (s->s3->hs.negotiated_tls_version < 14002876cc3Sjsing s->s3->hs.our_max_tls_version) { 1415ceed53aStb SSLerror(s, SSL_R_INAPPROPRIATE_FALLBACK); 1425ceed53aStb ssl3_send_alert(s, SSL3_AL_FATAL, 1435ceed53aStb SSL_AD_INAPPROPRIATE_FALLBACK); 1445ceed53aStb goto err; 1455ceed53aStb } 1465ceed53aStb continue; 1475ceed53aStb } 1485ceed53aStb 1495ceed53aStb if ((cipher = ssl3_get_cipher_by_value(cipher_value)) != NULL) { 1505ceed53aStb if (!sk_SSL_CIPHER_push(ciphers, cipher)) { 1515ceed53aStb SSLerror(s, ERR_R_MALLOC_FAILURE); 1525ceed53aStb goto err; 1535ceed53aStb } 1545ceed53aStb } 1555ceed53aStb } 1565ceed53aStb 1575ceed53aStb return (ciphers); 1585ceed53aStb 1595ceed53aStb err: 1605ceed53aStb sk_SSL_CIPHER_free(ciphers); 1615ceed53aStb 1625ceed53aStb return (NULL); 1635ceed53aStb } 16461a9dc01Sjsing 16561a9dc01Sjsing struct ssl_tls13_ciphersuite { 16661a9dc01Sjsing const char *name; 16761a9dc01Sjsing const char *alias; 168*f4fe6251Sjsing uint16_t value; 16961a9dc01Sjsing }; 17061a9dc01Sjsing 17161a9dc01Sjsing static const struct ssl_tls13_ciphersuite ssl_tls13_ciphersuites[] = { 17261a9dc01Sjsing { 17393fa6e49Stb .name = TLS1_3_RFC_AES_128_GCM_SHA256, 17493fa6e49Stb .alias = TLS1_3_TXT_AES_128_GCM_SHA256, 175*f4fe6251Sjsing .value = 0x1301, 17661a9dc01Sjsing }, 17761a9dc01Sjsing { 17893fa6e49Stb .name = TLS1_3_RFC_AES_256_GCM_SHA384, 17993fa6e49Stb .alias = TLS1_3_TXT_AES_256_GCM_SHA384, 180*f4fe6251Sjsing .value = 0x1302, 18161a9dc01Sjsing }, 18261a9dc01Sjsing { 18393fa6e49Stb .name = TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 18493fa6e49Stb .alias = TLS1_3_TXT_CHACHA20_POLY1305_SHA256, 185*f4fe6251Sjsing .value = 0x1303, 18661a9dc01Sjsing }, 18761a9dc01Sjsing { 18893fa6e49Stb .name = TLS1_3_RFC_AES_128_CCM_SHA256, 18993fa6e49Stb .alias = TLS1_3_TXT_AES_128_CCM_SHA256, 190*f4fe6251Sjsing .value = 0x1304, 19161a9dc01Sjsing }, 19261a9dc01Sjsing { 19393fa6e49Stb .name = TLS1_3_RFC_AES_128_CCM_8_SHA256, 19493fa6e49Stb .alias = TLS1_3_TXT_AES_128_CCM_8_SHA256, 195*f4fe6251Sjsing .value = 0x1305, 19661a9dc01Sjsing }, 19761a9dc01Sjsing { 19861a9dc01Sjsing .name = NULL, 19961a9dc01Sjsing }, 20061a9dc01Sjsing }; 20161a9dc01Sjsing 20261a9dc01Sjsing int 20361a9dc01Sjsing ssl_parse_ciphersuites(STACK_OF(SSL_CIPHER) **out_ciphers, const char *str) 20461a9dc01Sjsing { 20561a9dc01Sjsing const struct ssl_tls13_ciphersuite *ciphersuite; 20661a9dc01Sjsing STACK_OF(SSL_CIPHER) *ciphers; 20761a9dc01Sjsing const SSL_CIPHER *cipher; 20861a9dc01Sjsing char *s = NULL; 20961a9dc01Sjsing char *p, *q; 21061a9dc01Sjsing int i; 21161a9dc01Sjsing int ret = 0; 21261a9dc01Sjsing 21361a9dc01Sjsing if ((ciphers = sk_SSL_CIPHER_new_null()) == NULL) 21461a9dc01Sjsing goto err; 21561a9dc01Sjsing 21661a9dc01Sjsing /* An empty string is valid and means no ciphers. */ 21761a9dc01Sjsing if (strcmp(str, "") == 0) 21861a9dc01Sjsing goto done; 21961a9dc01Sjsing 22061a9dc01Sjsing if ((s = strdup(str)) == NULL) 22161a9dc01Sjsing goto err; 22261a9dc01Sjsing 22361a9dc01Sjsing q = s; 22461a9dc01Sjsing while ((p = strsep(&q, ":")) != NULL) { 22561a9dc01Sjsing ciphersuite = &ssl_tls13_ciphersuites[0]; 22661a9dc01Sjsing for (i = 0; ciphersuite->name != NULL; i++) { 22761a9dc01Sjsing if (strcmp(p, ciphersuite->name) == 0) 22861a9dc01Sjsing break; 22961a9dc01Sjsing if (strcmp(p, ciphersuite->alias) == 0) 23061a9dc01Sjsing break; 23106b6c48dStb ciphersuite = &ssl_tls13_ciphersuites[i]; 23261a9dc01Sjsing } 23361a9dc01Sjsing if (ciphersuite->name == NULL) 23461a9dc01Sjsing goto err; 23561a9dc01Sjsing 23661a9dc01Sjsing /* We know about the cipher suite, but it is not supported. */ 237*f4fe6251Sjsing if ((cipher = ssl3_get_cipher_by_value(ciphersuite->value)) == NULL) 23861a9dc01Sjsing continue; 23961a9dc01Sjsing 24061a9dc01Sjsing if (!sk_SSL_CIPHER_push(ciphers, cipher)) 24161a9dc01Sjsing goto err; 24261a9dc01Sjsing } 24361a9dc01Sjsing 24461a9dc01Sjsing done: 245728d659eSschwarze sk_SSL_CIPHER_free(*out_ciphers); 24661a9dc01Sjsing *out_ciphers = ciphers; 24761a9dc01Sjsing ciphers = NULL; 24861a9dc01Sjsing ret = 1; 24961a9dc01Sjsing 25061a9dc01Sjsing err: 25161a9dc01Sjsing sk_SSL_CIPHER_free(ciphers); 25261a9dc01Sjsing free(s); 25361a9dc01Sjsing 25461a9dc01Sjsing return ret; 25561a9dc01Sjsing } 25661a9dc01Sjsing 25761a9dc01Sjsing int 25861a9dc01Sjsing ssl_merge_cipherlists(STACK_OF(SSL_CIPHER) *cipherlist, 25961a9dc01Sjsing STACK_OF(SSL_CIPHER) *cipherlist_tls13, 26061a9dc01Sjsing STACK_OF(SSL_CIPHER) **out_cipherlist) 26161a9dc01Sjsing { 26261a9dc01Sjsing STACK_OF(SSL_CIPHER) *ciphers = NULL; 26361a9dc01Sjsing const SSL_CIPHER *cipher; 26461a9dc01Sjsing int i, ret = 0; 26561a9dc01Sjsing 26661a9dc01Sjsing if ((ciphers = sk_SSL_CIPHER_dup(cipherlist_tls13)) == NULL) 26761a9dc01Sjsing goto err; 26861a9dc01Sjsing for (i = 0; i < sk_SSL_CIPHER_num(cipherlist); i++) { 26961a9dc01Sjsing cipher = sk_SSL_CIPHER_value(cipherlist, i); 27061a9dc01Sjsing if (cipher->algorithm_ssl == SSL_TLSV1_3) 27161a9dc01Sjsing continue; 27261a9dc01Sjsing if (!sk_SSL_CIPHER_push(ciphers, cipher)) 27361a9dc01Sjsing goto err; 27461a9dc01Sjsing } 27561a9dc01Sjsing 27661a9dc01Sjsing sk_SSL_CIPHER_free(*out_cipherlist); 27761a9dc01Sjsing *out_cipherlist = ciphers; 27861a9dc01Sjsing ciphers = NULL; 27961a9dc01Sjsing 28061a9dc01Sjsing ret = 1; 28161a9dc01Sjsing 28261a9dc01Sjsing err: 28361a9dc01Sjsing sk_SSL_CIPHER_free(ciphers); 28461a9dc01Sjsing 28561a9dc01Sjsing return ret; 28661a9dc01Sjsing } 287