/*
- * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
* Copyright 2005 Nokia. All rights reserved.
*
/* *INDENT-ON* */
/* Utility function for table lookup */
-static int ssl_cipher_info_find(const ssl_cipher_table * table,
+static int ssl_cipher_info_find(const ssl_cipher_table *table,
size_t table_cnt, uint32_t mask)
{
size_t i;
* is engine-provided, we'll fill it only if corresponding EVP_PKEY_METHOD is
* found
*/
-static int ssl_mac_pkey_id[SSL_MD_NUM_IDX] = {
+static const int default_mac_pkey_id[SSL_MD_NUM_IDX] = {
/* MD5, SHA, GOST94, MAC89 */
EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
/* SHA256, SHA384, GOST2012_256, MAC89-12 */
if (md == NULL) {
ctx->disabled_mac_mask |= t->mask;
} else {
- int tmpsize = EVP_MD_size(md);
+ int tmpsize = EVP_MD_get_size(md);
if (!ossl_assert(tmpsize >= 0))
return 0;
ctx->ssl_mac_secret_size[i] = tmpsize;
/*
* We ignore any errors from the fetches below. They are expected to fail
- * if theose algorithms are not available.
+ * if these algorithms are not available.
*/
ERR_set_mark();
sig = EVP_SIGNATURE_fetch(ctx->libctx, "DSA", ctx->propq);
* Check for presence of GOST 34.10 algorithms, and if they are not
* present, disable appropriate auth and key exchange
*/
- ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] = get_optional_pkey_id(SN_id_Gost28147_89_MAC);
- if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX])
+ memcpy(ctx->ssl_mac_pkey_id, default_mac_pkey_id,
+ sizeof(ctx->ssl_mac_pkey_id));
+
+ ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] =
+ get_optional_pkey_id(SN_id_Gost28147_89_MAC);
+ if (ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX])
ctx->ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
else
ctx->disabled_mac_mask |= SSL_GOST89MAC;
- ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX] =
+ ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX] =
get_optional_pkey_id(SN_gost_mac_12);
- if (ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX])
+ if (ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX])
ctx->ssl_mac_secret_size[SSL_MD_GOST89MAC12_IDX] = 32;
else
ctx->disabled_mac_mask |= SSL_GOST89MAC12;
- ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX] =
+ ctx->ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX] =
get_optional_pkey_id(SN_magma_mac);
- if (ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX])
+ if (ctx->ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX])
ctx->ssl_mac_secret_size[SSL_MD_MAGMAOMAC_IDX] = 32;
else
ctx->disabled_mac_mask |= SSL_MAGMAOMAC;
- ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX] =
+ ctx->ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX] =
get_optional_pkey_id(SN_kuznyechik_mac);
- if (ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX])
+ if (ctx->ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX])
ctx->ssl_mac_secret_size[SSL_MD_KUZNYECHIKOMAC_IDX] = 32;
else
ctx->disabled_mac_mask |= SSL_KUZNYECHIKOMAC;
comp->method = method;
comp->id = SSL_COMP_ZLIB_IDX;
comp->name = COMP_get_name(method);
- sk_SSL_COMP_push(ssl_comp_methods, comp);
+ if (!sk_SSL_COMP_push(ssl_comp_methods, comp))
+ OPENSSL_free(comp);
sk_SSL_COMP_sort(ssl_comp_methods);
}
}
ctmp.id = s->compress_meth;
if (ssl_comp_methods != NULL) {
i = sk_SSL_COMP_find(ssl_comp_methods, &ctmp);
- *comp = sk_SSL_COMP_value(ssl_comp_methods, i);
+ if (i >= 0)
+ *comp = sk_SSL_COMP_value(ssl_comp_methods, i);
}
/* If were only interested in comp then return success */
if ((enc == NULL) && (md == NULL))
if (c->algorithm_mac == SSL_AEAD)
mac_pkey_type = NULL;
} else {
- if (!ssl_evp_md_up_ref(ctx->ssl_digest_methods[i])) {
+ const EVP_MD *digest = ctx->ssl_digest_methods[i];
+
+ if (digest == NULL
+ || !ssl_evp_md_up_ref(digest)) {
ssl_evp_cipher_free(*enc);
return 0;
}
- *md = ctx->ssl_digest_methods[i];
+ *md = digest;
if (mac_pkey_type != NULL)
- *mac_pkey_type = ssl_mac_pkey_id[i];
+ *mac_pkey_type = ctx->ssl_mac_pkey_id[i];
if (mac_secret_size != NULL)
*mac_secret_size = ctx->ssl_mac_secret_size[i];
}
- if ((*enc != NULL) &&
- (*md != NULL || (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
+ if ((*enc != NULL)
+ && (*md != NULL
+ || (EVP_CIPHER_get_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
&& (!mac_pkey_type || *mac_pkey_type != NID_undef)) {
const EVP_CIPHER *evp = NULL;
return ctx->ssl_digest_methods[idx];
}
-const EVP_MD *ssl_handshake_md(SSL *s)
+const EVP_MD *ssl_handshake_md(SSL_CONNECTION *s)
{
- return ssl_md(s->ctx, ssl_get_algorithm2(s));
+ return ssl_md(SSL_CONNECTION_GET_CTX(s), ssl_get_algorithm2(s));
}
-const EVP_MD *ssl_prf_md(SSL *s)
+const EVP_MD *ssl_prf_md(SSL_CONNECTION *s)
{
- return ssl_md(s->ctx, ssl_get_algorithm2(s) >> TLS1_PRF_DGST_SHIFT);
+ return ssl_md(SSL_CONNECTION_GET_CTX(s),
+ ssl_get_algorithm2(s) >> TLS1_PRF_DGST_SHIFT);
}
#define ITEM_SEP(a) \
const SSL_CIPHER *cp;
int reverse = 0;
- OSSL_TRACE_BEGIN(TLS_CIPHER){
+ OSSL_TRACE_BEGIN(TLS_CIPHER) {
BIO_printf(trc_out,
"Applying rule %d with %08x/%08x/%08x/%08x/%08x %08x (%d)\n",
- rule, alg_mkey, alg_auth, alg_enc, alg_mac, min_tls,
- algo_strength, strength_bits);
+ rule, (unsigned int)alg_mkey, (unsigned int)alg_auth,
+ (unsigned int)alg_enc, (unsigned int)alg_mac, min_tls,
+ (unsigned int)algo_strength, (int)strength_bits);
}
if (rule == CIPHER_DEL || rule == CIPHER_BUMP)
BIO_printf(trc_out,
"\nName: %s:"
"\nAlgo = %08x/%08x/%08x/%08x/%08x Algo_strength = %08x\n",
- cp->name, cp->algorithm_mkey, cp->algorithm_auth,
- cp->algorithm_enc, cp->algorithm_mac, cp->min_tls,
- cp->algo_strength);
+ cp->name,
+ (unsigned int)cp->algorithm_mkey,
+ (unsigned int)cp->algorithm_auth,
+ (unsigned int)cp->algorithm_enc,
+ (unsigned int)cp->algorithm_mac,
+ cp->min_tls,
+ (unsigned int)cp->algo_strength);
}
if (cipher_id != 0 && (cipher_id != cp->id))
continue;
}
number_uses = OPENSSL_zalloc(sizeof(int) * (max_strength_bits + 1));
- if (number_uses == NULL) {
- ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
+ if (number_uses == NULL)
return 0;
- }
/*
* Now find the strength_bits values actually used
retval = 1;
l = rule_str;
- for ( ; ; ) {
+ for (;;) {
ch = *l;
if (ch == '\0')
while (((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')) ||
((ch >= 'a') && (ch <= 'z')) ||
- (ch == '-') || (ch == '.') || (ch == '='))
+ (ch == '-') || (ch == '_') || (ch == '.') || (ch == '='))
#else
- while (isalnum((unsigned char)ch) || (ch == '-') || (ch == '.')
+ while (isalnum((unsigned char)ch) || (ch == '-') || (ch == '_') || (ch == '.')
|| (ch == '='))
#endif
{
* alphanumeric, so we call this an error.
*/
ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_COMMAND);
- retval = found = 0;
- l++;
- break;
+ return 0;
}
if (rule == CIPHER_SPECIAL) {
&& (ca_list[j]->name[buflen] == '\0')) {
found = 1;
break;
+ } else if (ca_list[j]->stdname != NULL
+ && strncmp(buf, ca_list[j]->stdname, buflen) == 0
+ && ca_list[j]->stdname[buflen] == '\0') {
+ found = 1;
+ break;
} else
j++;
}
*/
if (rule == CIPHER_SPECIAL) { /* special command */
ok = 0;
- if ((buflen == 8) && strncmp(buf, "STRENGTH", 8) == 0) {
+ if ((buflen == 8) && HAS_PREFIX(buf, "STRENGTH")) {
ok = ssl_cipher_strength_sort(head_p, tail_p);
- } else if (buflen == 10 && strncmp(buf, "SECLEVEL=", 9) == 0) {
- int level = buf[9] - '0';
+ } else if (buflen == 10 && CHECK_AND_SKIP_PREFIX(buf, "SECLEVEL=")) {
+ int level = *buf - '0';
if (level < 0 || level > 5) {
ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_COMMAND);
} else {
const char **prule_str)
{
unsigned int suiteb_flags = 0, suiteb_comb2 = 0;
- if (strncmp(*prule_str, "SUITEB128ONLY", 13) == 0) {
+ if (HAS_PREFIX(*prule_str, "SUITEB128ONLY")) {
suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS_ONLY;
- } else if (strncmp(*prule_str, "SUITEB128C2", 11) == 0) {
+ } else if (HAS_PREFIX(*prule_str, "SUITEB128C2")) {
suiteb_comb2 = 1;
suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
- } else if (strncmp(*prule_str, "SUITEB128", 9) == 0) {
+ } else if (HAS_PREFIX(*prule_str, "SUITEB128")) {
suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
- } else if (strncmp(*prule_str, "SUITEB192", 9) == 0) {
+ } else if (HAS_PREFIX(*prule_str, "SUITEB192")) {
suiteb_flags = SSL_CERT_FLAG_SUITEB_192_LOS;
}
return 1;
}
-static int update_cipher_list(STACK_OF(SSL_CIPHER) **cipher_list,
+static int update_cipher_list(SSL_CTX *ctx,
+ STACK_OF(SSL_CIPHER) **cipher_list,
STACK_OF(SSL_CIPHER) **cipher_list_by_id,
STACK_OF(SSL_CIPHER) *tls13_ciphersuites)
{
(void)sk_SSL_CIPHER_delete(tmp_cipher_list, 0);
/* Insert the new TLSv1.3 ciphersuites */
- for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++)
- sk_SSL_CIPHER_insert(tmp_cipher_list,
- sk_SSL_CIPHER_value(tls13_ciphersuites, i), i);
+ for (i = sk_SSL_CIPHER_num(tls13_ciphersuites) - 1; i >= 0; i--) {
+ const SSL_CIPHER *sslc = sk_SSL_CIPHER_value(tls13_ciphersuites, i);
+
+ /* Don't include any TLSv1.3 ciphersuites that are disabled */
+ if ((sslc->algorithm_enc & ctx->disabled_enc_mask) == 0
+ && (ssl_cipher_table_mac[sslc->algorithm2
+ & SSL_HANDSHAKE_MAC_MASK].mask
+ & ctx->disabled_mac_mask) == 0) {
+ sk_SSL_CIPHER_unshift(tmp_cipher_list, sslc);
+ }
+ }
if (!update_cipher_list_by_id(cipher_list_by_id, tmp_cipher_list)) {
sk_SSL_CIPHER_free(tmp_cipher_list);
int ret = set_ciphersuites(&(ctx->tls13_ciphersuites), str);
if (ret && ctx->cipher_list != NULL)
- return update_cipher_list(&ctx->cipher_list, &ctx->cipher_list_by_id,
+ return update_cipher_list(ctx, &ctx->cipher_list, &ctx->cipher_list_by_id,
ctx->tls13_ciphersuites);
return ret;
int SSL_set_ciphersuites(SSL *s, const char *str)
{
STACK_OF(SSL_CIPHER) *cipher_list;
- int ret = set_ciphersuites(&(s->tls13_ciphersuites), str);
+ SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
+ int ret;
- if (s->cipher_list == NULL) {
+ if (sc == NULL)
+ return 0;
+
+ ret = set_ciphersuites(&(sc->tls13_ciphersuites), str);
+
+ if (sc->cipher_list == NULL) {
if ((cipher_list = SSL_get_ciphers(s)) != NULL)
- s->cipher_list = sk_SSL_CIPHER_dup(cipher_list);
+ sc->cipher_list = sk_SSL_CIPHER_dup(cipher_list);
}
- if (ret && s->cipher_list != NULL)
- return update_cipher_list(&s->cipher_list, &s->cipher_list_by_id,
- s->tls13_ciphersuites);
+ if (ret && sc->cipher_list != NULL)
+ return update_cipher_list(s->ctx, &sc->cipher_list,
+ &sc->cipher_list_by_id,
+ sc->tls13_ciphersuites);
return ret;
}
*/
num_of_ciphers = ssl_method->num_ciphers();
- co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
- if (co_list == NULL) {
- ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
- return NULL; /* Failure */
+ if (num_of_ciphers > 0) {
+ co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
+ if (co_list == NULL)
+ return NULL; /* Failure */
}
ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
/*
* Partially overrule strength sort to prefer TLS 1.2 ciphers/PRFs.
- * TODO(openssl-team): is there an easier way to accomplish all this?
*/
ssl_cipher_apply_rule(0, 0, 0, 0, 0, TLS1_2_VERSION, 0, CIPHER_BUMP, -1,
&head, &tail);
ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
if (ca_list == NULL) {
OPENSSL_free(co_list);
- ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
return NULL; /* Failure */
}
ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
*/
ok = 1;
rule_p = rule_str;
- if (strncmp(rule_str, "DEFAULT", 7) == 0) {
+ if (HAS_PREFIX(rule_str, "DEFAULT")) {
ok = ssl_cipher_process_rulestr(OSSL_default_cipher_list(),
&head, &tail, ca_list, c);
rule_p += 7;
}
if (!sk_SSL_CIPHER_push(cipherstack, sslc)) {
+ OPENSSL_free(co_list);
sk_SSL_CIPHER_free(cipherstack);
return NULL;
}
const char *ver;
const char *kx, *au, *enc, *mac;
uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
- static const char *format = "%-30s %-7s Kx=%-8s Au=%-5s Enc=%-22s Mac=%-4s\n";
+ static const char *const format = "%-30s %-7s Kx=%-8s Au=%-5s Enc=%-22s Mac=%-4s\n";
if (buf == NULL) {
len = 128;
- if ((buf = OPENSSL_malloc(len)) == NULL) {
- ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
+ if ((buf = OPENSSL_malloc(len)) == NULL)
return NULL;
- }
} else if (len < 128) {
return NULL;
}
}
comp = OPENSSL_malloc(sizeof(*comp));
- if (comp == NULL) {
- ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
+ if (comp == NULL)
return 1;
- }
comp->id = id;
comp->method = cm;
}
if (ssl_comp_methods == NULL || !sk_SSL_COMP_push(ssl_comp_methods, comp)) {
OPENSSL_free(comp);
- ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
+ ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
return 1;
}
return 0;
#endif
}
-const SSL_CIPHER *ssl_get_cipher_by_char(SSL *ssl, const unsigned char *ptr,
+const SSL_CIPHER *ssl_get_cipher_by_char(SSL_CONNECTION *s,
+ const unsigned char *ptr,
int all)
{
- const SSL_CIPHER *c = ssl->method->get_cipher_by_char(ptr);
+ const SSL_CIPHER *c = SSL_CONNECTION_GET_SSL(s)->method->get_cipher_by_char(ptr);
if (c == NULL || (!all && c->valid == 0))
return NULL;
return ssl_cipher_table_auth[i].nid;
}
+int ssl_get_md_idx(int md_nid) {
+ int i;
+
+ for(i = 0; i < SSL_MD_NUM_IDX; i++) {
+ if (md_nid == ssl_cipher_table_mac[i].nid)
+ return i;
+ }
+ return -1;
+}
+
const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *c)
{
int idx = c->algorithm2 & SSL_HANDSHAKE_MAC_MASK;
if (e_md == NULL)
return 0;
- mac = EVP_MD_size(e_md);
+ mac = EVP_MD_get_size(e_md);
if (c->algorithm_enc != SSL_eNULL) {
int cipher_nid = SSL_CIPHER_get_cipher_nid(c);
const EVP_CIPHER *e_ciph = EVP_get_cipherbynid(cipher_nid);
/* If it wasn't AEAD or SSL_eNULL, we expect it to be a
known CBC cipher. */
if (e_ciph == NULL ||
- EVP_CIPHER_mode(e_ciph) != EVP_CIPH_CBC_MODE)
+ EVP_CIPHER_get_mode(e_ciph) != EVP_CIPH_CBC_MODE)
return 0;
in = 1; /* padding length byte */
- out = EVP_CIPHER_iv_length(e_ciph);
- blk = EVP_CIPHER_block_size(e_ciph);
+ out = EVP_CIPHER_get_iv_length(e_ciph);
+ blk = EVP_CIPHER_get_block_size(e_ciph);
+ if (blk == 0)
+ return 0;
}
}
int ssl_cert_is_disabled(SSL_CTX *ctx, size_t idx)
{
- const SSL_CERT_LOOKUP *cl = ssl_cert_lookup_by_idx(idx);
+ const SSL_CERT_LOOKUP *cl;
+
+ /* A provider-loaded key type is always enabled */
+ if (idx >= SSL_PKEY_NUM)
+ return 0;
+ cl = ssl_cert_lookup_by_idx(idx, ctx);
if (cl == NULL || (cl->amask & ctx->disabled_auth_mask) != 0)
return 1;
return 0;