| 1 |
/* |
| 2 |
* Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (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 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <ctype.h> |
| 13 |
#include <openssl/objects.h> |
| 14 |
#include <openssl/evp.h> |
| 15 |
#include <openssl/hmac.h> |
| 16 |
#include <openssl/core_names.h> |
| 17 |
#include <openssl/ocsp.h> |
| 18 |
#include <openssl/conf.h> |
| 19 |
#include <openssl/x509v3.h> |
| 20 |
#include <openssl/dh.h> |
| 21 |
#include <openssl/bn.h> |
| 22 |
#include <openssl/provider.h> |
| 23 |
#include <openssl/param_build.h> |
| 24 |
#include "internal/nelem.h" |
| 25 |
#include "internal/sizes.h" |
| 26 |
#include "internal/tlsgroups.h" |
| 27 |
#include "internal/ssl_unwrap.h" |
| 28 |
#include "ssl_local.h" |
| 29 |
#include "quic/quic_local.h" |
| 30 |
#include <openssl/ct.h> |
| 31 |
|
| 32 |
static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pkey); |
| 33 |
static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, const SIGALG_LOOKUP *lu); |
| 34 |
|
| 35 |
SSL3_ENC_METHOD const TLSv1_enc_data = { |
| 36 |
tls1_setup_key_block, |
| 37 |
tls1_generate_master_secret, |
| 38 |
tls1_change_cipher_state, |
| 39 |
tls1_final_finish_mac, |
| 40 |
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 41 |
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 42 |
tls1_alert_code, |
| 43 |
tls1_export_keying_material, |
| 44 |
0, |
| 45 |
ssl3_set_handshake_header, |
| 46 |
tls_close_construct_packet, |
| 47 |
ssl3_handshake_write |
| 48 |
}; |
| 49 |
|
| 50 |
SSL3_ENC_METHOD const TLSv1_1_enc_data = { |
| 51 |
tls1_setup_key_block, |
| 52 |
tls1_generate_master_secret, |
| 53 |
tls1_change_cipher_state, |
| 54 |
tls1_final_finish_mac, |
| 55 |
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 56 |
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 57 |
tls1_alert_code, |
| 58 |
tls1_export_keying_material, |
| 59 |
0, |
| 60 |
ssl3_set_handshake_header, |
| 61 |
tls_close_construct_packet, |
| 62 |
ssl3_handshake_write |
| 63 |
}; |
| 64 |
|
| 65 |
SSL3_ENC_METHOD const TLSv1_2_enc_data = { |
| 66 |
tls1_setup_key_block, |
| 67 |
tls1_generate_master_secret, |
| 68 |
tls1_change_cipher_state, |
| 69 |
tls1_final_finish_mac, |
| 70 |
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 71 |
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 72 |
tls1_alert_code, |
| 73 |
tls1_export_keying_material, |
| 74 |
SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF |
| 75 |
| SSL_ENC_FLAG_TLS1_2_CIPHERS, |
| 76 |
ssl3_set_handshake_header, |
| 77 |
tls_close_construct_packet, |
| 78 |
ssl3_handshake_write |
| 79 |
}; |
| 80 |
|
| 81 |
SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
| 82 |
tls13_setup_key_block, |
| 83 |
tls13_generate_master_secret, |
| 84 |
tls13_change_cipher_state, |
| 85 |
tls13_final_finish_mac, |
| 86 |
TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 87 |
TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 88 |
tls13_alert_code, |
| 89 |
tls13_export_keying_material, |
| 90 |
SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
| 91 |
ssl3_set_handshake_header, |
| 92 |
tls_close_construct_packet, |
| 93 |
ssl3_handshake_write |
| 94 |
}; |
| 95 |
|
| 96 |
OSSL_TIME tls1_default_timeout(void) |
| 97 |
{ |
| 98 |
/* |
| 99 |
* 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for |
| 100 |
* http, the cache would over fill |
| 101 |
*/ |
| 102 |
return ossl_seconds2time(60 * 60 * 2); |
| 103 |
} |
| 104 |
|
| 105 |
int tls1_new(SSL *s) |
| 106 |
{ |
| 107 |
if (!ssl3_new(s)) |
| 108 |
return 0; |
| 109 |
if (!s->method->ssl_clear(s)) |
| 110 |
return 0; |
| 111 |
|
| 112 |
return 1; |
| 113 |
} |
| 114 |
|
| 115 |
void tls1_free(SSL *s) |
| 116 |
{ |
| 117 |
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
| 118 |
|
| 119 |
if (sc == NULL) |
| 120 |
return; |
| 121 |
|
| 122 |
OPENSSL_free(sc->ext.session_ticket); |
| 123 |
ssl3_free(s); |
| 124 |
} |
| 125 |
|
| 126 |
int tls1_clear(SSL *s) |
| 127 |
{ |
| 128 |
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
| 129 |
|
| 130 |
if (sc == NULL) |
| 131 |
return 0; |
| 132 |
|
| 133 |
if (!ssl3_clear(s)) |
| 134 |
return 0; |
| 135 |
|
| 136 |
if (s->method->version == TLS_ANY_VERSION) |
| 137 |
sc->version = TLS_MAX_VERSION_INTERNAL; |
| 138 |
else |
| 139 |
sc->version = s->method->version; |
| 140 |
|
| 141 |
return 1; |
| 142 |
} |
| 143 |
|
| 144 |
/* Legacy NID to group_id mapping. Only works for groups we know about */ |
| 145 |
static const struct { |
| 146 |
int nid; |
| 147 |
uint16_t group_id; |
| 148 |
} nid_to_group[] = { |
| 149 |
{ NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1 }, |
| 150 |
{ NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1 }, |
| 151 |
{ NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2 }, |
| 152 |
{ NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1 }, |
| 153 |
{ NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2 }, |
| 154 |
{ NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1 }, |
| 155 |
{ NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1 }, |
| 156 |
{ NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1 }, |
| 157 |
{ NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1 }, |
| 158 |
{ NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1 }, |
| 159 |
{ NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1 }, |
| 160 |
{ NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1 }, |
| 161 |
{ NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1 }, |
| 162 |
{ NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1 }, |
| 163 |
{ NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1 }, |
| 164 |
{ NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1 }, |
| 165 |
{ NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2 }, |
| 166 |
{ NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1 }, |
| 167 |
{ NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1 }, |
| 168 |
{ NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1 }, |
| 169 |
{ NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1 }, |
| 170 |
{ NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1 }, |
| 171 |
{ NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1 }, |
| 172 |
{ NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1 }, |
| 173 |
{ NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1 }, |
| 174 |
{ NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1 }, |
| 175 |
{ NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1 }, |
| 176 |
{ NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1 }, |
| 177 |
{ EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519 }, |
| 178 |
{ EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448 }, |
| 179 |
{ NID_brainpoolP256r1tls13, OSSL_TLS_GROUP_ID_brainpoolP256r1_tls13 }, |
| 180 |
{ NID_brainpoolP384r1tls13, OSSL_TLS_GROUP_ID_brainpoolP384r1_tls13 }, |
| 181 |
{ NID_brainpoolP512r1tls13, OSSL_TLS_GROUP_ID_brainpoolP512r1_tls13 }, |
| 182 |
{ NID_id_tc26_gost_3410_2012_256_paramSetA, OSSL_TLS_GROUP_ID_gc256A }, |
| 183 |
{ NID_id_tc26_gost_3410_2012_256_paramSetB, OSSL_TLS_GROUP_ID_gc256B }, |
| 184 |
{ NID_id_tc26_gost_3410_2012_256_paramSetC, OSSL_TLS_GROUP_ID_gc256C }, |
| 185 |
{ NID_id_tc26_gost_3410_2012_256_paramSetD, OSSL_TLS_GROUP_ID_gc256D }, |
| 186 |
{ NID_id_tc26_gost_3410_2012_512_paramSetA, OSSL_TLS_GROUP_ID_gc512A }, |
| 187 |
{ NID_id_tc26_gost_3410_2012_512_paramSetB, OSSL_TLS_GROUP_ID_gc512B }, |
| 188 |
{ NID_id_tc26_gost_3410_2012_512_paramSetC, OSSL_TLS_GROUP_ID_gc512C }, |
| 189 |
{ NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048 }, |
| 190 |
{ NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072 }, |
| 191 |
{ NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096 }, |
| 192 |
{ NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144 }, |
| 193 |
{ NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192 } |
| 194 |
}; |
| 195 |
|
| 196 |
static const unsigned char ecformats_default[] = { |
| 197 |
TLSEXT_ECPOINTFORMAT_uncompressed |
| 198 |
}; |
| 199 |
|
| 200 |
static const unsigned char ecformats_all[] = { |
| 201 |
TLSEXT_ECPOINTFORMAT_uncompressed, |
| 202 |
TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, |
| 203 |
TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 |
| 204 |
}; |
| 205 |
|
| 206 |
/* Group list string of the built-in pseudo group DEFAULT */ |
| 207 |
#define DEFAULT_GROUP_NAME "DEFAULT" |
| 208 |
#define TLS_DEFAULT_GROUP_LIST \ |
| 209 |
"?*X25519MLKEM768 / ?*X25519:?secp256r1 / ?X448:?secp384r1:?secp521r1 / ?ffdhe2048:?ffdhe3072" |
| 210 |
|
| 211 |
static const uint16_t suiteb_curves[] = { |
| 212 |
OSSL_TLS_GROUP_ID_secp256r1, |
| 213 |
OSSL_TLS_GROUP_ID_secp384r1, |
| 214 |
}; |
| 215 |
|
| 216 |
/* Group list string of the built-in pseudo group DEFAULT_SUITE_B */ |
| 217 |
#define SUITE_B_GROUP_NAME "DEFAULT_SUITE_B" |
| 218 |
#define SUITE_B_GROUP_LIST "?secp256r1:?secp384r1", |
| 219 |
|
| 220 |
struct provider_ctx_data_st { |
| 221 |
SSL_CTX *ctx; |
| 222 |
OSSL_PROVIDER *provider; |
| 223 |
}; |
| 224 |
|
| 225 |
#define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE 10 |
| 226 |
static OSSL_CALLBACK add_provider_groups; |
| 227 |
static int add_provider_groups(const OSSL_PARAM params[], void *data) |
| 228 |
{ |
| 229 |
struct provider_ctx_data_st *pgd = data; |
| 230 |
SSL_CTX *ctx = pgd->ctx; |
| 231 |
const OSSL_PARAM *p; |
| 232 |
TLS_GROUP_INFO *ginf = NULL; |
| 233 |
EVP_KEYMGMT *keymgmt; |
| 234 |
unsigned int gid; |
| 235 |
unsigned int is_kem = 0; |
| 236 |
int ret = 0; |
| 237 |
|
| 238 |
if (ctx->group_list_max_len == ctx->group_list_len) { |
| 239 |
TLS_GROUP_INFO *tmp = NULL; |
| 240 |
|
| 241 |
if (ctx->group_list_max_len == 0) |
| 242 |
tmp = OPENSSL_malloc_array(TLS_GROUP_LIST_MALLOC_BLOCK_SIZE, |
| 243 |
sizeof(TLS_GROUP_INFO)); |
| 244 |
else |
| 245 |
tmp = OPENSSL_realloc_array(ctx->group_list, |
| 246 |
ctx->group_list_max_len |
| 247 |
+ TLS_GROUP_LIST_MALLOC_BLOCK_SIZE, |
| 248 |
sizeof(TLS_GROUP_INFO)); |
| 249 |
if (tmp == NULL) |
| 250 |
return 0; |
| 251 |
ctx->group_list = tmp; |
| 252 |
memset(tmp + ctx->group_list_max_len, |
| 253 |
0, |
| 254 |
sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE); |
| 255 |
ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE; |
| 256 |
} |
| 257 |
|
| 258 |
ginf = &ctx->group_list[ctx->group_list_len]; |
| 259 |
|
| 260 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME); |
| 261 |
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 262 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 263 |
goto err; |
| 264 |
} |
| 265 |
ginf->tlsname = OPENSSL_strdup(p->data); |
| 266 |
if (ginf->tlsname == NULL) |
| 267 |
goto err; |
| 268 |
|
| 269 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL); |
| 270 |
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 271 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 272 |
goto err; |
| 273 |
} |
| 274 |
ginf->realname = OPENSSL_strdup(p->data); |
| 275 |
if (ginf->realname == NULL) |
| 276 |
goto err; |
| 277 |
|
| 278 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID); |
| 279 |
if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) { |
| 280 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 281 |
goto err; |
| 282 |
} |
| 283 |
ginf->group_id = (uint16_t)gid; |
| 284 |
|
| 285 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG); |
| 286 |
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 287 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 288 |
goto err; |
| 289 |
} |
| 290 |
ginf->algorithm = OPENSSL_strdup(p->data); |
| 291 |
if (ginf->algorithm == NULL) |
| 292 |
goto err; |
| 293 |
|
| 294 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS); |
| 295 |
if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) { |
| 296 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 297 |
goto err; |
| 298 |
} |
| 299 |
|
| 300 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_IS_KEM); |
| 301 |
if (p != NULL && (!OSSL_PARAM_get_uint(p, &is_kem) || is_kem > 1)) { |
| 302 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 303 |
goto err; |
| 304 |
} |
| 305 |
ginf->is_kem = 1 & is_kem; |
| 306 |
|
| 307 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS); |
| 308 |
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) { |
| 309 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 310 |
goto err; |
| 311 |
} |
| 312 |
|
| 313 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS); |
| 314 |
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) { |
| 315 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 316 |
goto err; |
| 317 |
} |
| 318 |
|
| 319 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS); |
| 320 |
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) { |
| 321 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 322 |
goto err; |
| 323 |
} |
| 324 |
|
| 325 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS); |
| 326 |
if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) { |
| 327 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 328 |
goto err; |
| 329 |
} |
| 330 |
/* |
| 331 |
* Now check that the algorithm is actually usable for our property query |
| 332 |
* string. Regardless of the result we still return success because we have |
| 333 |
* successfully processed this group, even though we may decide not to use |
| 334 |
* it. |
| 335 |
*/ |
| 336 |
ret = 1; |
| 337 |
ERR_set_mark(); |
| 338 |
keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq); |
| 339 |
if (keymgmt != NULL) { |
| 340 |
/* We have successfully fetched the algorithm, we can use the group. */ |
| 341 |
ctx->group_list_len++; |
| 342 |
ginf = NULL; |
| 343 |
EVP_KEYMGMT_free(keymgmt); |
| 344 |
} |
| 345 |
ERR_pop_to_mark(); |
| 346 |
err: |
| 347 |
if (ginf != NULL) { |
| 348 |
OPENSSL_free(ginf->tlsname); |
| 349 |
OPENSSL_free(ginf->realname); |
| 350 |
OPENSSL_free(ginf->algorithm); |
| 351 |
ginf->algorithm = ginf->tlsname = ginf->realname = NULL; |
| 352 |
} |
| 353 |
return ret; |
| 354 |
} |
| 355 |
|
| 356 |
static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx) |
| 357 |
{ |
| 358 |
struct provider_ctx_data_st pgd; |
| 359 |
|
| 360 |
pgd.ctx = vctx; |
| 361 |
pgd.provider = provider; |
| 362 |
return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP", |
| 363 |
add_provider_groups, &pgd); |
| 364 |
} |
| 365 |
|
| 366 |
int ssl_load_groups(SSL_CTX *ctx) |
| 367 |
{ |
| 368 |
if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx)) |
| 369 |
return 0; |
| 370 |
|
| 371 |
return SSL_CTX_set1_groups_list(ctx, TLS_DEFAULT_GROUP_LIST); |
| 372 |
} |
| 373 |
|
| 374 |
static const char *inferred_keytype(const TLS_SIGALG_INFO *sinf) |
| 375 |
{ |
| 376 |
return (sinf->keytype != NULL |
| 377 |
? sinf->keytype |
| 378 |
: (sinf->sig_name != NULL |
| 379 |
? sinf->sig_name |
| 380 |
: sinf->sigalg_name)); |
| 381 |
} |
| 382 |
|
| 383 |
#define TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE 10 |
| 384 |
static OSSL_CALLBACK add_provider_sigalgs; |
| 385 |
static int add_provider_sigalgs(const OSSL_PARAM params[], void *data) |
| 386 |
{ |
| 387 |
struct provider_ctx_data_st *pgd = data; |
| 388 |
SSL_CTX *ctx = pgd->ctx; |
| 389 |
OSSL_PROVIDER *provider = pgd->provider; |
| 390 |
const OSSL_PARAM *p; |
| 391 |
TLS_SIGALG_INFO *sinf = NULL; |
| 392 |
EVP_KEYMGMT *keymgmt; |
| 393 |
const char *keytype; |
| 394 |
unsigned int code_point = 0; |
| 395 |
int ret = 0; |
| 396 |
|
| 397 |
if (ctx->sigalg_list_max_len == ctx->sigalg_list_len) { |
| 398 |
TLS_SIGALG_INFO *tmp = NULL; |
| 399 |
|
| 400 |
if (ctx->sigalg_list_max_len == 0) |
| 401 |
tmp = OPENSSL_malloc_array(TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE, |
| 402 |
sizeof(TLS_SIGALG_INFO)); |
| 403 |
else |
| 404 |
tmp = OPENSSL_realloc_array(ctx->sigalg_list, |
| 405 |
ctx->sigalg_list_max_len |
| 406 |
+ TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE, |
| 407 |
sizeof(TLS_SIGALG_INFO)); |
| 408 |
if (tmp == NULL) |
| 409 |
return 0; |
| 410 |
ctx->sigalg_list = tmp; |
| 411 |
memset(tmp + ctx->sigalg_list_max_len, 0, |
| 412 |
sizeof(TLS_SIGALG_INFO) * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE); |
| 413 |
ctx->sigalg_list_max_len += TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE; |
| 414 |
} |
| 415 |
|
| 416 |
sinf = &ctx->sigalg_list[ctx->sigalg_list_len]; |
| 417 |
|
| 418 |
/* First, mandatory parameters */ |
| 419 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_NAME); |
| 420 |
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 421 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 422 |
goto err; |
| 423 |
} |
| 424 |
OPENSSL_free(sinf->sigalg_name); |
| 425 |
sinf->sigalg_name = OPENSSL_strdup(p->data); |
| 426 |
if (sinf->sigalg_name == NULL) |
| 427 |
goto err; |
| 428 |
|
| 429 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME); |
| 430 |
if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 431 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 432 |
goto err; |
| 433 |
} |
| 434 |
OPENSSL_free(sinf->name); |
| 435 |
sinf->name = OPENSSL_strdup(p->data); |
| 436 |
if (sinf->name == NULL) |
| 437 |
goto err; |
| 438 |
|
| 439 |
p = OSSL_PARAM_locate_const(params, |
| 440 |
OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT); |
| 441 |
if (p == NULL |
| 442 |
|| !OSSL_PARAM_get_uint(p, &code_point) |
| 443 |
|| code_point > UINT16_MAX) { |
| 444 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 445 |
goto err; |
| 446 |
} |
| 447 |
sinf->code_point = (uint16_t)code_point; |
| 448 |
|
| 449 |
p = OSSL_PARAM_locate_const(params, |
| 450 |
OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS); |
| 451 |
if (p == NULL || !OSSL_PARAM_get_uint(p, &sinf->secbits)) { |
| 452 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 453 |
goto err; |
| 454 |
} |
| 455 |
|
| 456 |
/* Now, optional parameters */ |
| 457 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_OID); |
| 458 |
if (p == NULL) { |
| 459 |
sinf->sigalg_oid = NULL; |
| 460 |
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 461 |
goto err; |
| 462 |
} else { |
| 463 |
OPENSSL_free(sinf->sigalg_oid); |
| 464 |
sinf->sigalg_oid = OPENSSL_strdup(p->data); |
| 465 |
if (sinf->sigalg_oid == NULL) |
| 466 |
goto err; |
| 467 |
} |
| 468 |
|
| 469 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_NAME); |
| 470 |
if (p == NULL) { |
| 471 |
sinf->sig_name = NULL; |
| 472 |
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 473 |
goto err; |
| 474 |
} else { |
| 475 |
OPENSSL_free(sinf->sig_name); |
| 476 |
sinf->sig_name = OPENSSL_strdup(p->data); |
| 477 |
if (sinf->sig_name == NULL) |
| 478 |
goto err; |
| 479 |
} |
| 480 |
|
| 481 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_OID); |
| 482 |
if (p == NULL) { |
| 483 |
sinf->sig_oid = NULL; |
| 484 |
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 485 |
goto err; |
| 486 |
} else { |
| 487 |
OPENSSL_free(sinf->sig_oid); |
| 488 |
sinf->sig_oid = OPENSSL_strdup(p->data); |
| 489 |
if (sinf->sig_oid == NULL) |
| 490 |
goto err; |
| 491 |
} |
| 492 |
|
| 493 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME); |
| 494 |
if (p == NULL) { |
| 495 |
sinf->hash_name = NULL; |
| 496 |
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 497 |
goto err; |
| 498 |
} else { |
| 499 |
OPENSSL_free(sinf->hash_name); |
| 500 |
sinf->hash_name = OPENSSL_strdup(p->data); |
| 501 |
if (sinf->hash_name == NULL) |
| 502 |
goto err; |
| 503 |
} |
| 504 |
|
| 505 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_OID); |
| 506 |
if (p == NULL) { |
| 507 |
sinf->hash_oid = NULL; |
| 508 |
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 509 |
goto err; |
| 510 |
} else { |
| 511 |
OPENSSL_free(sinf->hash_oid); |
| 512 |
sinf->hash_oid = OPENSSL_strdup(p->data); |
| 513 |
if (sinf->hash_oid == NULL) |
| 514 |
goto err; |
| 515 |
} |
| 516 |
|
| 517 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE); |
| 518 |
if (p == NULL) { |
| 519 |
sinf->keytype = NULL; |
| 520 |
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 521 |
goto err; |
| 522 |
} else { |
| 523 |
OPENSSL_free(sinf->keytype); |
| 524 |
sinf->keytype = OPENSSL_strdup(p->data); |
| 525 |
if (sinf->keytype == NULL) |
| 526 |
goto err; |
| 527 |
} |
| 528 |
|
| 529 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE_OID); |
| 530 |
if (p == NULL) { |
| 531 |
sinf->keytype_oid = NULL; |
| 532 |
} else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
| 533 |
goto err; |
| 534 |
} else { |
| 535 |
OPENSSL_free(sinf->keytype_oid); |
| 536 |
sinf->keytype_oid = OPENSSL_strdup(p->data); |
| 537 |
if (sinf->keytype_oid == NULL) |
| 538 |
goto err; |
| 539 |
} |
| 540 |
|
| 541 |
/* Optional, not documented prior to 3.5 */ |
| 542 |
sinf->mindtls = sinf->maxdtls = -1; |
| 543 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_DTLS); |
| 544 |
if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->mindtls)) { |
| 545 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 546 |
goto err; |
| 547 |
} |
| 548 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_DTLS); |
| 549 |
if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->maxdtls)) { |
| 550 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 551 |
goto err; |
| 552 |
} |
| 553 |
/* DTLS version numbers grow downward */ |
| 554 |
if ((sinf->maxdtls != 0) && (sinf->maxdtls != -1) && ((sinf->maxdtls > sinf->mindtls))) { |
| 555 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 556 |
goto err; |
| 557 |
} |
| 558 |
/* No provider sigalgs are supported in DTLS, reset after checking. */ |
| 559 |
sinf->mindtls = sinf->maxdtls = -1; |
| 560 |
|
| 561 |
/* The remaining parameters below are mandatory again */ |
| 562 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS); |
| 563 |
if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->mintls)) { |
| 564 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 565 |
goto err; |
| 566 |
} |
| 567 |
p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS); |
| 568 |
if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->maxtls)) { |
| 569 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 570 |
goto err; |
| 571 |
} |
| 572 |
if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && ((sinf->maxtls < sinf->mintls))) { |
| 573 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 574 |
goto err; |
| 575 |
} |
| 576 |
if ((sinf->mintls != 0) && (sinf->mintls != -1) && ((sinf->mintls > TLS1_3_VERSION))) |
| 577 |
sinf->mintls = sinf->maxtls = -1; |
| 578 |
if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && ((sinf->maxtls < TLS1_3_VERSION))) |
| 579 |
sinf->mintls = sinf->maxtls = -1; |
| 580 |
|
| 581 |
/* Ignore unusable sigalgs */ |
| 582 |
if (sinf->mintls == -1 && sinf->mindtls == -1) { |
| 583 |
ret = 1; |
| 584 |
goto err; |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* Now check that the algorithm is actually usable for our property query |
| 589 |
* string. Regardless of the result we still return success because we have |
| 590 |
* successfully processed this signature, even though we may decide not to |
| 591 |
* use it. |
| 592 |
*/ |
| 593 |
ret = 1; |
| 594 |
ERR_set_mark(); |
| 595 |
keytype = inferred_keytype(sinf); |
| 596 |
keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, keytype, ctx->propq); |
| 597 |
if (keymgmt != NULL) { |
| 598 |
/* |
| 599 |
* We have successfully fetched the algorithm - however if the provider |
| 600 |
* doesn't match this one then we ignore it. |
| 601 |
* |
| 602 |
* Note: We're cheating a little here. Technically if the same algorithm |
| 603 |
* is available from more than one provider then it is undefined which |
| 604 |
* implementation you will get back. Theoretically this could be |
| 605 |
* different every time...we assume here that you'll always get the |
| 606 |
* same one back if you repeat the exact same fetch. Is this a reasonable |
| 607 |
* assumption to make (in which case perhaps we should document this |
| 608 |
* behaviour)? |
| 609 |
*/ |
| 610 |
if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) { |
| 611 |
/* |
| 612 |
* We have a match - so we could use this signature; |
| 613 |
* Check proper object registration first, though. |
| 614 |
* Don't care about return value as this may have been |
| 615 |
* done within providers or previous calls to |
| 616 |
* add_provider_sigalgs. |
| 617 |
*/ |
| 618 |
OBJ_create(sinf->sigalg_oid, sinf->sigalg_name, NULL); |
| 619 |
/* sanity check: Without successful registration don't use alg */ |
| 620 |
if ((OBJ_txt2nid(sinf->sigalg_name) == NID_undef) || (OBJ_nid2obj(OBJ_txt2nid(sinf->sigalg_name)) == NULL)) { |
| 621 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
| 622 |
goto err; |
| 623 |
} |
| 624 |
if (sinf->sig_name != NULL) |
| 625 |
OBJ_create(sinf->sig_oid, sinf->sig_name, NULL); |
| 626 |
if (sinf->keytype != NULL) |
| 627 |
OBJ_create(sinf->keytype_oid, sinf->keytype, NULL); |
| 628 |
if (sinf->hash_name != NULL) |
| 629 |
OBJ_create(sinf->hash_oid, sinf->hash_name, NULL); |
| 630 |
OBJ_add_sigid(OBJ_txt2nid(sinf->sigalg_name), |
| 631 |
(sinf->hash_name != NULL |
| 632 |
? OBJ_txt2nid(sinf->hash_name) |
| 633 |
: NID_undef), |
| 634 |
OBJ_txt2nid(keytype)); |
| 635 |
ctx->sigalg_list_len++; |
| 636 |
sinf = NULL; |
| 637 |
} |
| 638 |
EVP_KEYMGMT_free(keymgmt); |
| 639 |
} |
| 640 |
ERR_pop_to_mark(); |
| 641 |
err: |
| 642 |
if (sinf != NULL) { |
| 643 |
OPENSSL_free(sinf->name); |
| 644 |
sinf->name = NULL; |
| 645 |
OPENSSL_free(sinf->sigalg_name); |
| 646 |
sinf->sigalg_name = NULL; |
| 647 |
OPENSSL_free(sinf->sigalg_oid); |
| 648 |
sinf->sigalg_oid = NULL; |
| 649 |
OPENSSL_free(sinf->sig_name); |
| 650 |
sinf->sig_name = NULL; |
| 651 |
OPENSSL_free(sinf->sig_oid); |
| 652 |
sinf->sig_oid = NULL; |
| 653 |
OPENSSL_free(sinf->hash_name); |
| 654 |
sinf->hash_name = NULL; |
| 655 |
OPENSSL_free(sinf->hash_oid); |
| 656 |
sinf->hash_oid = NULL; |
| 657 |
OPENSSL_free(sinf->keytype); |
| 658 |
sinf->keytype = NULL; |
| 659 |
OPENSSL_free(sinf->keytype_oid); |
| 660 |
sinf->keytype_oid = NULL; |
| 661 |
} |
| 662 |
return ret; |
| 663 |
} |
| 664 |
|
| 665 |
static int discover_provider_sigalgs(OSSL_PROVIDER *provider, void *vctx) |
| 666 |
{ |
| 667 |
struct provider_ctx_data_st pgd; |
| 668 |
|
| 669 |
pgd.ctx = vctx; |
| 670 |
pgd.provider = provider; |
| 671 |
OSSL_PROVIDER_get_capabilities(provider, "TLS-SIGALG", |
| 672 |
add_provider_sigalgs, &pgd); |
| 673 |
/* |
| 674 |
* Always OK, even if provider doesn't support the capability: |
| 675 |
* Reconsider testing retval when legacy sigalgs are also loaded this way. |
| 676 |
*/ |
| 677 |
return 1; |
| 678 |
} |
| 679 |
|
| 680 |
int ssl_load_sigalgs(SSL_CTX *ctx) |
| 681 |
{ |
| 682 |
size_t i; |
| 683 |
SSL_CERT_LOOKUP lu; |
| 684 |
|
| 685 |
if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_sigalgs, ctx)) |
| 686 |
return 0; |
| 687 |
|
| 688 |
/* now populate ctx->ssl_cert_info */ |
| 689 |
if (ctx->sigalg_list_len > 0) { |
| 690 |
OPENSSL_free(ctx->ssl_cert_info); |
| 691 |
ctx->ssl_cert_info = OPENSSL_calloc(ctx->sigalg_list_len, sizeof(lu)); |
| 692 |
if (ctx->ssl_cert_info == NULL) |
| 693 |
return 0; |
| 694 |
for (i = 0; i < ctx->sigalg_list_len; i++) { |
| 695 |
const char *keytype = inferred_keytype(&ctx->sigalg_list[i]); |
| 696 |
ctx->ssl_cert_info[i].pkey_nid = OBJ_txt2nid(keytype); |
| 697 |
ctx->ssl_cert_info[i].amask = SSL_aANY; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
/* |
| 702 |
* For now, leave it at this: legacy sigalgs stay in their own |
| 703 |
* data structures until "legacy cleanup" occurs. |
| 704 |
*/ |
| 705 |
|
| 706 |
return 1; |
| 707 |
} |
| 708 |
|
| 709 |
static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name) |
| 710 |
{ |
| 711 |
size_t i; |
| 712 |
|
| 713 |
for (i = 0; i < ctx->group_list_len; i++) { |
| 714 |
if (OPENSSL_strcasecmp(ctx->group_list[i].tlsname, name) == 0 |
| 715 |
|| OPENSSL_strcasecmp(ctx->group_list[i].realname, name) == 0) |
| 716 |
return ctx->group_list[i].group_id; |
| 717 |
} |
| 718 |
|
| 719 |
return 0; |
| 720 |
} |
| 721 |
|
| 722 |
const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id) |
| 723 |
{ |
| 724 |
size_t i; |
| 725 |
|
| 726 |
for (i = 0; i < ctx->group_list_len; i++) { |
| 727 |
if (ctx->group_list[i].group_id == group_id) |
| 728 |
return &ctx->group_list[i]; |
| 729 |
} |
| 730 |
|
| 731 |
return NULL; |
| 732 |
} |
| 733 |
|
| 734 |
const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id) |
| 735 |
{ |
| 736 |
const TLS_GROUP_INFO *tls_group_info = tls1_group_id_lookup(ctx, group_id); |
| 737 |
|
| 738 |
if (tls_group_info == NULL) |
| 739 |
return NULL; |
| 740 |
|
| 741 |
return tls_group_info->tlsname; |
| 742 |
} |
| 743 |
|
| 744 |
int tls1_group_id2nid(uint16_t group_id, int include_unknown) |
| 745 |
{ |
| 746 |
size_t i; |
| 747 |
|
| 748 |
if (group_id == 0) |
| 749 |
return NID_undef; |
| 750 |
|
| 751 |
/* |
| 752 |
* Return well known Group NIDs - for backwards compatibility. This won't |
| 753 |
* work for groups we don't know about. |
| 754 |
*/ |
| 755 |
for (i = 0; i < OSSL_NELEM(nid_to_group); i++) { |
| 756 |
if (nid_to_group[i].group_id == group_id) |
| 757 |
return nid_to_group[i].nid; |
| 758 |
} |
| 759 |
if (!include_unknown) |
| 760 |
return NID_undef; |
| 761 |
return TLSEXT_nid_unknown | (int)group_id; |
| 762 |
} |
| 763 |
|
| 764 |
uint16_t tls1_nid2group_id(int nid) |
| 765 |
{ |
| 766 |
size_t i; |
| 767 |
|
| 768 |
/* |
| 769 |
* Return well known Group ids - for backwards compatibility. This won't |
| 770 |
* work for groups we don't know about. |
| 771 |
*/ |
| 772 |
for (i = 0; i < OSSL_NELEM(nid_to_group); i++) { |
| 773 |
if (nid_to_group[i].nid == nid) |
| 774 |
return nid_to_group[i].group_id; |
| 775 |
} |
| 776 |
|
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
/* |
| 781 |
* Set *pgroups to the supported groups list and *pgroupslen to |
| 782 |
* the number of groups supported. |
| 783 |
*/ |
| 784 |
void tls1_get_supported_groups(SSL_CONNECTION *s, const uint16_t **pgroups, |
| 785 |
size_t *pgroupslen) |
| 786 |
{ |
| 787 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 788 |
|
| 789 |
/* For Suite B mode only include P-256, P-384 */ |
| 790 |
switch (tls1_suiteb(s)) { |
| 791 |
case SSL_CERT_FLAG_SUITEB_128_LOS: |
| 792 |
*pgroups = suiteb_curves; |
| 793 |
*pgroupslen = OSSL_NELEM(suiteb_curves); |
| 794 |
break; |
| 795 |
|
| 796 |
case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
| 797 |
*pgroups = suiteb_curves; |
| 798 |
*pgroupslen = 1; |
| 799 |
break; |
| 800 |
|
| 801 |
case SSL_CERT_FLAG_SUITEB_192_LOS: |
| 802 |
*pgroups = suiteb_curves + 1; |
| 803 |
*pgroupslen = 1; |
| 804 |
break; |
| 805 |
|
| 806 |
default: |
| 807 |
if (s->ext.supportedgroups == NULL) { |
| 808 |
*pgroups = sctx->ext.supportedgroups; |
| 809 |
*pgroupslen = sctx->ext.supportedgroups_len; |
| 810 |
} else { |
| 811 |
*pgroups = s->ext.supportedgroups; |
| 812 |
*pgroupslen = s->ext.supportedgroups_len; |
| 813 |
} |
| 814 |
break; |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
/* |
| 819 |
* Some comments for the function below: |
| 820 |
* s->ext.supportedgroups == NULL means legacy syntax (no [*,/,-]) from built-in group array. |
| 821 |
* In this case, we need to send exactly one key share, which MUST be the first (leftmost) |
| 822 |
* eligible group from the legacy list. Therefore, we provide the entire list of supported |
| 823 |
* groups in this case. |
| 824 |
* |
| 825 |
* A 'flag' to indicate legacy syntax is created by setting the number of key shares to 1, |
| 826 |
* but the groupID to 0. |
| 827 |
* The 'flag' is checked right at the beginning in tls_construct_ctos_key_share and either |
| 828 |
* the "list of requested key share groups" is used, or the "list of supported groups" in |
| 829 |
* combination with setting add_only_one = 1 is applied. |
| 830 |
*/ |
| 831 |
void tls1_get_requested_keyshare_groups(SSL_CONNECTION *s, const uint16_t **pgroups, |
| 832 |
size_t *pgroupslen) |
| 833 |
{ |
| 834 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 835 |
|
| 836 |
if (s->ext.supportedgroups == NULL) { |
| 837 |
*pgroups = sctx->ext.supportedgroups; |
| 838 |
*pgroupslen = sctx->ext.supportedgroups_len; |
| 839 |
} else { |
| 840 |
*pgroups = s->ext.keyshares; |
| 841 |
*pgroupslen = s->ext.keyshares_len; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
void tls1_get_group_tuples(SSL_CONNECTION *s, const size_t **ptuples, |
| 846 |
size_t *ptupleslen) |
| 847 |
{ |
| 848 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 849 |
|
| 850 |
if (s->ext.supportedgroups == NULL) { |
| 851 |
*ptuples = sctx->ext.tuples; |
| 852 |
*ptupleslen = sctx->ext.tuples_len; |
| 853 |
} else { |
| 854 |
*ptuples = s->ext.tuples; |
| 855 |
*ptupleslen = s->ext.tuples_len; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
int tls_valid_group(SSL_CONNECTION *s, uint16_t group_id, |
| 860 |
int minversion, int maxversion, |
| 861 |
int isec, int *okfortls13) |
| 862 |
{ |
| 863 |
const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
| 864 |
group_id); |
| 865 |
int ret; |
| 866 |
int group_minversion, group_maxversion; |
| 867 |
|
| 868 |
if (okfortls13 != NULL) |
| 869 |
*okfortls13 = 0; |
| 870 |
|
| 871 |
if (ginfo == NULL) |
| 872 |
return 0; |
| 873 |
|
| 874 |
group_minversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->mindtls : ginfo->mintls; |
| 875 |
group_maxversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->maxdtls : ginfo->maxtls; |
| 876 |
|
| 877 |
if (group_minversion < 0 || group_maxversion < 0) |
| 878 |
return 0; |
| 879 |
if (group_maxversion == 0) |
| 880 |
ret = 1; |
| 881 |
else |
| 882 |
ret = (ssl_version_cmp(s, minversion, group_maxversion) <= 0); |
| 883 |
if (group_minversion > 0) |
| 884 |
ret &= (ssl_version_cmp(s, maxversion, group_minversion) >= 0); |
| 885 |
|
| 886 |
if (!SSL_CONNECTION_IS_DTLS(s)) { |
| 887 |
if (ret && okfortls13 != NULL && maxversion == TLS1_3_VERSION) |
| 888 |
*okfortls13 = (group_maxversion == 0) |
| 889 |
|| (group_maxversion >= TLS1_3_VERSION); |
| 890 |
} |
| 891 |
ret &= !isec |
| 892 |
|| strcmp(ginfo->algorithm, "EC") == 0 |
| 893 |
|| strcmp(ginfo->algorithm, "X25519") == 0 |
| 894 |
|| strcmp(ginfo->algorithm, "X448") == 0; |
| 895 |
|
| 896 |
return ret; |
| 897 |
} |
| 898 |
|
| 899 |
/* See if group is allowed by security callback */ |
| 900 |
int tls_group_allowed(SSL_CONNECTION *s, uint16_t group, int op) |
| 901 |
{ |
| 902 |
const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
| 903 |
group); |
| 904 |
unsigned char gtmp[2]; |
| 905 |
|
| 906 |
if (ginfo == NULL) |
| 907 |
return 0; |
| 908 |
|
| 909 |
gtmp[0] = group >> 8; |
| 910 |
gtmp[1] = group & 0xff; |
| 911 |
return ssl_security(s, op, ginfo->secbits, |
| 912 |
tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp); |
| 913 |
} |
| 914 |
|
| 915 |
/* Return 1 if "id" is in "list" */ |
| 916 |
static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen) |
| 917 |
{ |
| 918 |
size_t i; |
| 919 |
for (i = 0; i < listlen; i++) |
| 920 |
if (list[i] == id) |
| 921 |
return 1; |
| 922 |
return 0; |
| 923 |
} |
| 924 |
|
| 925 |
typedef struct { |
| 926 |
TLS_GROUP_INFO *grp; |
| 927 |
size_t ix; |
| 928 |
} TLS_GROUP_IX; |
| 929 |
|
| 930 |
DEFINE_STACK_OF(TLS_GROUP_IX) |
| 931 |
|
| 932 |
static void free_wrapper(TLS_GROUP_IX *a) |
| 933 |
{ |
| 934 |
OPENSSL_free(a); |
| 935 |
} |
| 936 |
|
| 937 |
static int tls_group_ix_cmp(const TLS_GROUP_IX *const *a, |
| 938 |
const TLS_GROUP_IX *const *b) |
| 939 |
{ |
| 940 |
int idcmpab = (*a)->grp->group_id < (*b)->grp->group_id; |
| 941 |
int idcmpba = (*b)->grp->group_id < (*a)->grp->group_id; |
| 942 |
int ixcmpab = (*a)->ix < (*b)->ix; |
| 943 |
int ixcmpba = (*b)->ix < (*a)->ix; |
| 944 |
|
| 945 |
/* Ascending by group id */ |
| 946 |
if (idcmpab != idcmpba) |
| 947 |
return (idcmpba - idcmpab); |
| 948 |
/* Ascending by original appearance index */ |
| 949 |
return ixcmpba - ixcmpab; |
| 950 |
} |
| 951 |
|
| 952 |
int tls1_get0_implemented_groups(int min_proto_version, int max_proto_version, |
| 953 |
TLS_GROUP_INFO *grps, size_t num, long all, |
| 954 |
STACK_OF(OPENSSL_CSTRING) *out) |
| 955 |
{ |
| 956 |
STACK_OF(TLS_GROUP_IX) *collect = NULL; |
| 957 |
TLS_GROUP_IX *gix; |
| 958 |
uint16_t id = 0; |
| 959 |
int ret = 0; |
| 960 |
int ix; |
| 961 |
|
| 962 |
if (grps == NULL || out == NULL || num > INT_MAX) |
| 963 |
return 0; |
| 964 |
if ((collect = sk_TLS_GROUP_IX_new(tls_group_ix_cmp)) == NULL) |
| 965 |
return 0; |
| 966 |
for (ix = 0; ix < (int)num; ++ix, ++grps) { |
| 967 |
if (grps->mintls > 0 && max_proto_version > 0 |
| 968 |
&& grps->mintls > max_proto_version) |
| 969 |
continue; |
| 970 |
if (grps->maxtls > 0 && min_proto_version > 0 |
| 971 |
&& grps->maxtls < min_proto_version) |
| 972 |
continue; |
| 973 |
|
| 974 |
if ((gix = OPENSSL_malloc(sizeof(*gix))) == NULL) |
| 975 |
goto end; |
| 976 |
gix->grp = grps; |
| 977 |
gix->ix = ix; |
| 978 |
if (sk_TLS_GROUP_IX_push(collect, gix) <= 0) { |
| 979 |
OPENSSL_free(gix); |
| 980 |
goto end; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
sk_TLS_GROUP_IX_sort(collect); |
| 985 |
num = sk_TLS_GROUP_IX_num(collect); |
| 986 |
for (ix = 0; ix < (int)num; ++ix) { |
| 987 |
gix = sk_TLS_GROUP_IX_value(collect, ix); |
| 988 |
if (!all && gix->grp->group_id == id) |
| 989 |
continue; |
| 990 |
id = gix->grp->group_id; |
| 991 |
if (sk_OPENSSL_CSTRING_push(out, gix->grp->tlsname) <= 0) |
| 992 |
goto end; |
| 993 |
} |
| 994 |
ret = 1; |
| 995 |
|
| 996 |
end: |
| 997 |
sk_TLS_GROUP_IX_pop_free(collect, free_wrapper); |
| 998 |
return ret; |
| 999 |
} |
| 1000 |
|
| 1001 |
/*- |
| 1002 |
* For nmatch >= 0, return the id of the |nmatch|th shared group or 0 |
| 1003 |
* if there is no match. |
| 1004 |
* For nmatch == -1, return number of matches |
| 1005 |
* For nmatch == -2, return the id of the group to use for |
| 1006 |
* a tmp key, or 0 if there is no match. |
| 1007 |
*/ |
| 1008 |
uint16_t tls1_shared_group(SSL_CONNECTION *s, int nmatch) |
| 1009 |
{ |
| 1010 |
const uint16_t *pref, *supp; |
| 1011 |
size_t num_pref, num_supp, i; |
| 1012 |
int k; |
| 1013 |
SSL_CTX *ctx = SSL_CONNECTION_GET_CTX(s); |
| 1014 |
|
| 1015 |
/* Can't do anything on client side */ |
| 1016 |
if (s->server == 0) |
| 1017 |
return 0; |
| 1018 |
if (nmatch == -2) { |
| 1019 |
if (tls1_suiteb(s)) { |
| 1020 |
/* |
| 1021 |
* For Suite B ciphersuite determines curve: we already know |
| 1022 |
* these are acceptable due to previous checks. |
| 1023 |
*/ |
| 1024 |
unsigned long cid = s->s3.tmp.new_cipher->id; |
| 1025 |
|
| 1026 |
if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
| 1027 |
return OSSL_TLS_GROUP_ID_secp256r1; |
| 1028 |
if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
| 1029 |
return OSSL_TLS_GROUP_ID_secp384r1; |
| 1030 |
/* Should never happen */ |
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
/* If not Suite B just return first preference shared curve */ |
| 1034 |
nmatch = 0; |
| 1035 |
} |
| 1036 |
/* |
| 1037 |
* If server preference set, our groups are the preference order |
| 1038 |
* otherwise peer decides. |
| 1039 |
*/ |
| 1040 |
if (s->options & SSL_OP_SERVER_PREFERENCE) { |
| 1041 |
tls1_get_supported_groups(s, &pref, &num_pref); |
| 1042 |
tls1_get_peer_groups(s, &supp, &num_supp); |
| 1043 |
} else { |
| 1044 |
tls1_get_peer_groups(s, &pref, &num_pref); |
| 1045 |
tls1_get_supported_groups(s, &supp, &num_supp); |
| 1046 |
} |
| 1047 |
|
| 1048 |
for (k = 0, i = 0; i < num_pref; i++) { |
| 1049 |
uint16_t id = pref[i]; |
| 1050 |
const TLS_GROUP_INFO *inf; |
| 1051 |
int minversion, maxversion; |
| 1052 |
|
| 1053 |
if (!tls1_in_list(id, supp, num_supp) |
| 1054 |
|| !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED)) |
| 1055 |
continue; |
| 1056 |
inf = tls1_group_id_lookup(ctx, id); |
| 1057 |
if (!ossl_assert(inf != NULL)) |
| 1058 |
return 0; |
| 1059 |
|
| 1060 |
minversion = SSL_CONNECTION_IS_DTLS(s) |
| 1061 |
? inf->mindtls |
| 1062 |
: inf->mintls; |
| 1063 |
maxversion = SSL_CONNECTION_IS_DTLS(s) |
| 1064 |
? inf->maxdtls |
| 1065 |
: inf->maxtls; |
| 1066 |
if (maxversion == -1) |
| 1067 |
continue; |
| 1068 |
if ((minversion != 0 && ssl_version_cmp(s, s->version, minversion) < 0) |
| 1069 |
|| (maxversion != 0 |
| 1070 |
&& ssl_version_cmp(s, s->version, maxversion) > 0)) |
| 1071 |
continue; |
| 1072 |
|
| 1073 |
if (nmatch == k) |
| 1074 |
return id; |
| 1075 |
k++; |
| 1076 |
} |
| 1077 |
if (nmatch == -1) |
| 1078 |
return k; |
| 1079 |
/* Out of range (nmatch > k). */ |
| 1080 |
return 0; |
| 1081 |
} |
| 1082 |
|
| 1083 |
int tls1_set_groups(uint16_t **grpext, size_t *grpextlen, |
| 1084 |
uint16_t **ksext, size_t *ksextlen, |
| 1085 |
size_t **tplext, size_t *tplextlen, |
| 1086 |
int *groups, size_t ngroups) |
| 1087 |
{ |
| 1088 |
uint16_t *glist = NULL, *kslist = NULL; |
| 1089 |
size_t *tpllist = NULL; |
| 1090 |
size_t i; |
| 1091 |
/* |
| 1092 |
* Bitmap of groups included to detect duplicates: two variables are added |
| 1093 |
* to detect duplicates as some values are more than 32. |
| 1094 |
*/ |
| 1095 |
unsigned long *dup_list = NULL; |
| 1096 |
unsigned long dup_list_egrp = 0; |
| 1097 |
unsigned long dup_list_dhgrp = 0; |
| 1098 |
|
| 1099 |
if (ngroups == 0) { |
| 1100 |
ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
| 1101 |
return 0; |
| 1102 |
} |
| 1103 |
if ((glist = OPENSSL_malloc_array(ngroups, sizeof(*glist))) == NULL) |
| 1104 |
goto err; |
| 1105 |
if ((kslist = OPENSSL_malloc_array(1, sizeof(*kslist))) == NULL) |
| 1106 |
goto err; |
| 1107 |
if ((tpllist = OPENSSL_malloc_array(1, sizeof(*tpllist))) == NULL) |
| 1108 |
goto err; |
| 1109 |
for (i = 0; i < ngroups; i++) { |
| 1110 |
unsigned long idmask; |
| 1111 |
uint16_t id; |
| 1112 |
id = tls1_nid2group_id(groups[i]); |
| 1113 |
if ((id & 0x00FF) >= (sizeof(unsigned long) * 8)) |
| 1114 |
goto err; |
| 1115 |
idmask = 1L << (id & 0x00FF); |
| 1116 |
dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp; |
| 1117 |
if (!id || ((*dup_list) & idmask)) |
| 1118 |
goto err; |
| 1119 |
*dup_list |= idmask; |
| 1120 |
glist[i] = id; |
| 1121 |
} |
| 1122 |
OPENSSL_free(*grpext); |
| 1123 |
OPENSSL_free(*ksext); |
| 1124 |
OPENSSL_free(*tplext); |
| 1125 |
*grpext = glist; |
| 1126 |
*grpextlen = ngroups; |
| 1127 |
/* |
| 1128 |
* No * prefix was used, let tls_construct_ctos_key_share choose a key |
| 1129 |
* share. This has the advantage that it will filter unsupported groups |
| 1130 |
* before choosing one, which this function does not do. See also the |
| 1131 |
* comment for tls1_get_requested_keyshare_groups. |
| 1132 |
*/ |
| 1133 |
kslist[0] = 0; |
| 1134 |
*ksext = kslist; |
| 1135 |
*ksextlen = 1; |
| 1136 |
tpllist[0] = ngroups; |
| 1137 |
*tplext = tpllist; |
| 1138 |
*tplextlen = 1; |
| 1139 |
return 1; |
| 1140 |
err: |
| 1141 |
OPENSSL_free(glist); |
| 1142 |
OPENSSL_free(kslist); |
| 1143 |
OPENSSL_free(tpllist); |
| 1144 |
return 0; |
| 1145 |
} |
| 1146 |
|
| 1147 |
/* |
| 1148 |
* Definition of DEFAULT[_XYZ] pseudo group names. |
| 1149 |
* A pseudo group name is actually a full list of groups, including prefixes |
| 1150 |
* and or tuple delimiters. It can be hierarchically defined (for potential future use). |
| 1151 |
* IMPORTANT REMARK: For ease of use, in the built-in lists of groups, unknown groups or |
| 1152 |
* groups not backed by a provider will always silently be ignored, even without '?' prefix |
| 1153 |
*/ |
| 1154 |
typedef struct { |
| 1155 |
const char *list_name; /* The name of this pseudo group */ |
| 1156 |
const char *group_string; /* The group string of this pseudo group */ |
| 1157 |
} default_group_string_st; /* (can include '?', '*'. '-', '/' as needed) */ |
| 1158 |
|
| 1159 |
/* Built-in pseudo group-names must start with a (D or d) */ |
| 1160 |
static const char *DEFAULT_GROUPNAME_FIRST_CHARACTER = "D"; |
| 1161 |
|
| 1162 |
/* The list of all built-in pseudo-group-name structures */ |
| 1163 |
static const default_group_string_st default_group_strings[] = { |
| 1164 |
{ DEFAULT_GROUP_NAME, TLS_DEFAULT_GROUP_LIST }, |
| 1165 |
{ SUITE_B_GROUP_NAME, SUITE_B_GROUP_LIST } |
| 1166 |
}; |
| 1167 |
|
| 1168 |
/* |
| 1169 |
* Some GOST names are not resolved by tls1_group_name2id, |
| 1170 |
* hence we'll check for those manually |
| 1171 |
*/ |
| 1172 |
typedef struct { |
| 1173 |
const char *group_name; |
| 1174 |
uint16_t groupID; |
| 1175 |
} name2id_st; |
| 1176 |
static const name2id_st name2id_arr[] = { |
| 1177 |
{ "GC256A", OSSL_TLS_GROUP_ID_gc256A }, |
| 1178 |
{ "GC256B", OSSL_TLS_GROUP_ID_gc256B }, |
| 1179 |
{ "GC256C", OSSL_TLS_GROUP_ID_gc256C }, |
| 1180 |
{ "GC256D", OSSL_TLS_GROUP_ID_gc256D }, |
| 1181 |
{ "GC512A", OSSL_TLS_GROUP_ID_gc512A }, |
| 1182 |
{ "GC512B", OSSL_TLS_GROUP_ID_gc512B }, |
| 1183 |
{ "GC512C", OSSL_TLS_GROUP_ID_gc512C }, |
| 1184 |
}; |
| 1185 |
|
| 1186 |
/* |
| 1187 |
* Group list management: |
| 1188 |
* We establish three lists along with their related size counters: |
| 1189 |
* 1) List of (unique) groups |
| 1190 |
* 2) List of number of groups per group-priority-tuple |
| 1191 |
* 3) List of (unique) key share groups |
| 1192 |
*/ |
| 1193 |
#define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (64 Bytes chunks ~= cache line) */ |
| 1194 |
#define GROUP_NAME_BUFFER_LENGTH 64 /* Max length of a group name */ |
| 1195 |
|
| 1196 |
/* |
| 1197 |
* Preparation of the prefix used to indicate the desire to send a key share, |
| 1198 |
* the characters used as separators between groups or tuples of groups, the |
| 1199 |
* character to indicate that an unknown group should be ignored, and the |
| 1200 |
* character to indicate that a group should be deleted from a list |
| 1201 |
*/ |
| 1202 |
#ifndef TUPLE_DELIMITER_CHARACTER |
| 1203 |
/* The prefix characters to indicate group tuple boundaries */ |
| 1204 |
#define TUPLE_DELIMITER_CHARACTER '/' |
| 1205 |
#endif |
| 1206 |
#ifndef GROUP_DELIMITER_CHARACTER |
| 1207 |
/* The prefix characters to indicate group tuple boundaries */ |
| 1208 |
#define GROUP_DELIMITER_CHARACTER ':' |
| 1209 |
#endif |
| 1210 |
#ifndef IGNORE_UNKNOWN_GROUP_CHARACTER |
| 1211 |
/* The prefix character to ignore unknown groups */ |
| 1212 |
#define IGNORE_UNKNOWN_GROUP_CHARACTER '?' |
| 1213 |
#endif |
| 1214 |
#ifndef KEY_SHARE_INDICATOR_CHARACTER |
| 1215 |
/* The prefix character to trigger a key share addition */ |
| 1216 |
#define KEY_SHARE_INDICATOR_CHARACTER '*' |
| 1217 |
#endif |
| 1218 |
#ifndef REMOVE_GROUP_INDICATOR_CHARACTER |
| 1219 |
/* The prefix character to trigger a key share removal */ |
| 1220 |
#define REMOVE_GROUP_INDICATOR_CHARACTER '-' |
| 1221 |
#endif |
| 1222 |
static const char prefixes[] = { TUPLE_DELIMITER_CHARACTER, |
| 1223 |
GROUP_DELIMITER_CHARACTER, |
| 1224 |
IGNORE_UNKNOWN_GROUP_CHARACTER, |
| 1225 |
KEY_SHARE_INDICATOR_CHARACTER, |
| 1226 |
REMOVE_GROUP_INDICATOR_CHARACTER, |
| 1227 |
'\0' }; |
| 1228 |
|
| 1229 |
/* |
| 1230 |
* High-level description of how group strings are analyzed: |
| 1231 |
* A first call back function (tuple_cb) is used to process group tuples, and a |
| 1232 |
* second callback function (gid_cb) is used to process the groups inside a tuple. |
| 1233 |
* Those callback functions are (indirectly) called by CONF_parse_list with |
| 1234 |
* different separators (nominally ':' or '/'), a variable based on gid_cb_st |
| 1235 |
* is used to keep track of the parsing results between the various calls |
| 1236 |
*/ |
| 1237 |
|
| 1238 |
typedef struct { |
| 1239 |
SSL_CTX *ctx; |
| 1240 |
/* Variables to hold the three lists (groups, requested keyshares, tuple structure) */ |
| 1241 |
size_t gidmax; /* The memory allocation chunk size for the group IDs */ |
| 1242 |
size_t gidcnt; /* Number of groups */ |
| 1243 |
uint16_t *gid_arr; /* The IDs of the supported groups (flat list) */ |
| 1244 |
size_t tplmax; /* Allocated length of tuplcnt_arr */ |
| 1245 |
/* |
| 1246 |
* Number of *closed* (fully parsed) tuples. During parsing there is |
| 1247 |
* always one additional active tuple being built, stored at index tplcnt. |
| 1248 |
* tuplcnt_arr therefore always needs at least tplcnt + 1 allocated slots. |
| 1249 |
*/ |
| 1250 |
size_t tplcnt; |
| 1251 |
size_t *tuplcnt_arr; /* Per-tuple group counts; [0..tplcnt-1] closed, [tplcnt] active */ |
| 1252 |
size_t ksidmax; /* The memory allocation chunk size */ |
| 1253 |
size_t ksidcnt; /* Number of key shares */ |
| 1254 |
uint16_t *ksid_arr; /* The IDs of the key share groups (flat list) */ |
| 1255 |
/* Variable to keep state between execution of callback or helper functions */ |
| 1256 |
int inner; /* Are we expanding a DEFAULT list */ |
| 1257 |
int first; /* First tuple of possibly nested expansion? */ |
| 1258 |
} gid_cb_st; |
| 1259 |
|
| 1260 |
/* Forward declaration of tuple callback function */ |
| 1261 |
static int tuple_cb(const char *tuple, int len, void *arg); |
| 1262 |
|
| 1263 |
/* |
| 1264 |
* Extract and process the individual groups (and their prefixes if present) |
| 1265 |
* present in a tuple. Note: The argument 'elem' is a NON-\0-terminated string |
| 1266 |
* and must be appended by a \0 if used as \0-terminated string |
| 1267 |
*/ |
| 1268 |
static int gid_cb(const char *elem, int len, void *arg) |
| 1269 |
{ |
| 1270 |
gid_cb_st *garg = arg; |
| 1271 |
size_t i, j, k; |
| 1272 |
uint16_t gid = 0; |
| 1273 |
int found_group = 0; |
| 1274 |
char etmp[GROUP_NAME_BUFFER_LENGTH]; |
| 1275 |
int retval = 1; /* We assume success */ |
| 1276 |
const char *current_prefix; |
| 1277 |
int ignore_unknown = 0; |
| 1278 |
int add_keyshare = 0; |
| 1279 |
int remove_group = 0; |
| 1280 |
size_t restored_prefix_index = 0; |
| 1281 |
char *restored_default_group_string; |
| 1282 |
int continue_while_loop = 1; |
| 1283 |
|
| 1284 |
/* Sanity checks */ |
| 1285 |
if (garg == NULL || elem == NULL || len <= 0) { |
| 1286 |
ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
| 1287 |
return 0; |
| 1288 |
} |
| 1289 |
|
| 1290 |
/* Check the possible prefixes (remark: Leading and trailing spaces already cleared) */ |
| 1291 |
while (continue_while_loop && len > 0 |
| 1292 |
&& ((current_prefix = strchr(prefixes, elem[0])) != NULL |
| 1293 |
|| OPENSSL_strncasecmp(current_prefix = (char *)DEFAULT_GROUPNAME_FIRST_CHARACTER, elem, 1) == 0)) { |
| 1294 |
|
| 1295 |
switch (*current_prefix) { |
| 1296 |
case TUPLE_DELIMITER_CHARACTER: |
| 1297 |
/* tuple delimiter not allowed here -> syntax error */ |
| 1298 |
return -1; |
| 1299 |
break; |
| 1300 |
case GROUP_DELIMITER_CHARACTER: |
| 1301 |
return -1; /* Not a valid prefix for a single group name-> syntax error */ |
| 1302 |
break; |
| 1303 |
case KEY_SHARE_INDICATOR_CHARACTER: |
| 1304 |
if (add_keyshare) |
| 1305 |
return -1; /* Only single key share prefix allowed -> syntax error */ |
| 1306 |
add_keyshare = 1; |
| 1307 |
++elem; |
| 1308 |
--len; |
| 1309 |
break; |
| 1310 |
case REMOVE_GROUP_INDICATOR_CHARACTER: |
| 1311 |
if (remove_group) |
| 1312 |
return -1; /* Only single remove group prefix allowed -> syntax error */ |
| 1313 |
remove_group = 1; |
| 1314 |
++elem; |
| 1315 |
--len; |
| 1316 |
break; |
| 1317 |
case IGNORE_UNKNOWN_GROUP_CHARACTER: |
| 1318 |
if (ignore_unknown) |
| 1319 |
return -1; /* Only single ? allowed -> syntax error */ |
| 1320 |
ignore_unknown = 1; |
| 1321 |
++elem; |
| 1322 |
--len; |
| 1323 |
break; |
| 1324 |
default: |
| 1325 |
/* |
| 1326 |
* Check whether a DEFAULT[_XYZ] 'pseudo group' (= a built-in |
| 1327 |
* list of groups) should be added |
| 1328 |
*/ |
| 1329 |
for (i = 0; i < OSSL_NELEM(default_group_strings); i++) { |
| 1330 |
if ((size_t)len == (strlen(default_group_strings[i].list_name)) |
| 1331 |
&& OPENSSL_strncasecmp(default_group_strings[i].list_name, elem, len) == 0) { |
| 1332 |
int saved_first; |
| 1333 |
|
| 1334 |
/* |
| 1335 |
* We're asked to insert an entire list of groups from a |
| 1336 |
* DEFAULT[_XYZ] 'pseudo group' which we do by |
| 1337 |
* recursively calling this function (indirectly via |
| 1338 |
* CONF_parse_list and tuple_cb); essentially, we treat a DEFAULT |
| 1339 |
* group string like a tuple which is appended to the current tuple |
| 1340 |
* rather then starting a new tuple. |
| 1341 |
*/ |
| 1342 |
if (ignore_unknown || remove_group) |
| 1343 |
return -1; /* removal or ignore not allowed here -> syntax error */ |
| 1344 |
|
| 1345 |
/* |
| 1346 |
* First, we restore any keyshare prefix in a new zero-terminated string |
| 1347 |
* (if not already present) |
| 1348 |
*/ |
| 1349 |
restored_default_group_string = OPENSSL_malloc(1 /* max prefix length */ + strlen(default_group_strings[i].group_string) + 1 /* \0 */); |
| 1350 |
if (restored_default_group_string == NULL) |
| 1351 |
return 0; |
| 1352 |
if (add_keyshare |
| 1353 |
/* Remark: we tolerate a duplicated keyshare indicator here */ |
| 1354 |
&& default_group_strings[i].group_string[0] |
| 1355 |
!= KEY_SHARE_INDICATOR_CHARACTER) |
| 1356 |
restored_default_group_string[restored_prefix_index++] = KEY_SHARE_INDICATOR_CHARACTER; |
| 1357 |
|
| 1358 |
memcpy(restored_default_group_string + restored_prefix_index, |
| 1359 |
default_group_strings[i].group_string, |
| 1360 |
strlen(default_group_strings[i].group_string)); |
| 1361 |
restored_default_group_string[strlen(default_group_strings[i].group_string) + restored_prefix_index] = '\0'; |
| 1362 |
/* |
| 1363 |
* Append first tuple of result to current tuple, and don't |
| 1364 |
* terminate the last tuple until we return to a top-level |
| 1365 |
* tuple_cb. |
| 1366 |
*/ |
| 1367 |
saved_first = garg->first; |
| 1368 |
garg->inner = garg->first = 1; |
| 1369 |
retval = CONF_parse_list(restored_default_group_string, |
| 1370 |
TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, garg); |
| 1371 |
garg->inner = 0; |
| 1372 |
garg->first = saved_first; |
| 1373 |
/* We don't need the \0-terminated string anymore */ |
| 1374 |
OPENSSL_free(restored_default_group_string); |
| 1375 |
|
| 1376 |
return retval; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
/* |
| 1380 |
* If we reached this point, a group name started with a 'd' or 'D', but no request |
| 1381 |
* for a DEFAULT[_XYZ] 'pseudo group' was detected, hence processing of the group |
| 1382 |
* name can continue as usual (= the while loop checking prefixes can end) |
| 1383 |
*/ |
| 1384 |
continue_while_loop = 0; |
| 1385 |
break; |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (len == 0) |
| 1390 |
return -1; /* Seems we have prefxes without a group name -> syntax error */ |
| 1391 |
|
| 1392 |
/* Memory management in case more groups are present compared to initial allocation */ |
| 1393 |
if (garg->gidcnt == garg->gidmax) { |
| 1394 |
uint16_t *tmp = OPENSSL_realloc_array(garg->gid_arr, |
| 1395 |
garg->gidmax + GROUPLIST_INCREMENT, |
| 1396 |
sizeof(*garg->gid_arr)); |
| 1397 |
|
| 1398 |
if (tmp == NULL) |
| 1399 |
return 0; |
| 1400 |
|
| 1401 |
garg->gidmax += GROUPLIST_INCREMENT; |
| 1402 |
garg->gid_arr = tmp; |
| 1403 |
} |
| 1404 |
/* Memory management for key share groups */ |
| 1405 |
if (garg->ksidcnt == garg->ksidmax) { |
| 1406 |
uint16_t *tmp = OPENSSL_realloc_array(garg->ksid_arr, |
| 1407 |
garg->ksidmax + GROUPLIST_INCREMENT, |
| 1408 |
sizeof(*garg->ksid_arr)); |
| 1409 |
|
| 1410 |
if (tmp == NULL) |
| 1411 |
return 0; |
| 1412 |
garg->ksidmax += GROUPLIST_INCREMENT; |
| 1413 |
garg->ksid_arr = tmp; |
| 1414 |
} |
| 1415 |
|
| 1416 |
if (len > (int)(sizeof(etmp) - 1)) |
| 1417 |
return -1; /* group name to long -> syntax error */ |
| 1418 |
|
| 1419 |
/* |
| 1420 |
* Prepare addition or removal of a single group by converting |
| 1421 |
* a group name into its groupID equivalent |
| 1422 |
*/ |
| 1423 |
|
| 1424 |
/* Create a \0-terminated string and get the gid for this group if possible */ |
| 1425 |
memcpy(etmp, elem, len); |
| 1426 |
etmp[len] = 0; |
| 1427 |
|
| 1428 |
/* Get the groupID */ |
| 1429 |
gid = tls1_group_name2id(garg->ctx, etmp); |
| 1430 |
/* |
| 1431 |
* Handle the case where no valid groupID was returned |
| 1432 |
* e.g. for an unknown group, which we'd ignore (only) if relevant prefix was set |
| 1433 |
*/ |
| 1434 |
if (gid == 0) { |
| 1435 |
/* Is it one of the GOST groups ? */ |
| 1436 |
for (i = 0; i < OSSL_NELEM(name2id_arr); i++) { |
| 1437 |
if (OPENSSL_strcasecmp(etmp, name2id_arr[i].group_name) == 0) { |
| 1438 |
gid = name2id_arr[i].groupID; |
| 1439 |
break; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
if (gid == 0) { /* still not found */ |
| 1443 |
/* Unknown group - ignore if ignore_unknown; trigger error otherwise */ |
| 1444 |
retval = ignore_unknown; |
| 1445 |
goto done; |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
/* Make sure that at least one provider is supporting this groupID */ |
| 1450 |
found_group = 0; |
| 1451 |
for (j = 0; j < garg->ctx->group_list_len; j++) |
| 1452 |
if (garg->ctx->group_list[j].group_id == gid) { |
| 1453 |
found_group = 1; |
| 1454 |
break; |
| 1455 |
} |
| 1456 |
|
| 1457 |
/* |
| 1458 |
* No provider supports this group - ignore if |
| 1459 |
* ignore_unknown; trigger error otherwise |
| 1460 |
*/ |
| 1461 |
if (found_group == 0) { |
| 1462 |
retval = ignore_unknown; |
| 1463 |
goto done; |
| 1464 |
} |
| 1465 |
/* Remove group (and keyshare) from anywhere in the list if present, ignore if not present */ |
| 1466 |
if (remove_group) { |
| 1467 |
/* Is the current group specified anywhere in the entire list so far? */ |
| 1468 |
found_group = 0; |
| 1469 |
for (i = 0; i < garg->gidcnt; i++) |
| 1470 |
if (garg->gid_arr[i] == gid) { |
| 1471 |
found_group = 1; |
| 1472 |
break; |
| 1473 |
} |
| 1474 |
/* The group to remove is at position i in the list of (zero indexed) groups */ |
| 1475 |
if (found_group) { |
| 1476 |
/* We remove that group from its position (which is at i)... */ |
| 1477 |
for (j = i; j < (garg->gidcnt - 1); j++) |
| 1478 |
garg->gid_arr[j] = garg->gid_arr[j + 1]; /* ...shift remaining groups left ... */ |
| 1479 |
garg->gidcnt--; /* ..and update the book keeping for the number of groups */ |
| 1480 |
|
| 1481 |
/* |
| 1482 |
* We also must update the number of groups either in a previous tuple (which we |
| 1483 |
* must identify and check whether it becomes empty due to the deletion) or in |
| 1484 |
* the current tuple, pending where the deleted group resides |
| 1485 |
*/ |
| 1486 |
k = 0; |
| 1487 |
for (j = 0; j < garg->tplcnt; j++) { |
| 1488 |
k += garg->tuplcnt_arr[j]; |
| 1489 |
/* Remark: i is zero-indexed, k is one-indexed */ |
| 1490 |
if (k > i) { /* remove from one of the previous tuples */ |
| 1491 |
garg->tuplcnt_arr[j]--; |
| 1492 |
break; /* We took care not to have group duplicates, hence we can stop here */ |
| 1493 |
} |
| 1494 |
} |
| 1495 |
if (k <= i) /* remove from current tuple */ |
| 1496 |
garg->tuplcnt_arr[j]--; |
| 1497 |
|
| 1498 |
/* We also remove the group from the list of keyshares (if present) */ |
| 1499 |
found_group = 0; |
| 1500 |
for (i = 0; i < garg->ksidcnt; i++) |
| 1501 |
if (garg->ksid_arr[i] == gid) { |
| 1502 |
found_group = 1; |
| 1503 |
break; |
| 1504 |
} |
| 1505 |
if (found_group) { |
| 1506 |
/* Found, hence we remove that keyshare from its position (which is at i)... */ |
| 1507 |
for (j = i; j < (garg->ksidcnt - 1); j++) |
| 1508 |
garg->ksid_arr[j] = garg->ksid_arr[j + 1]; /* shift remaining key shares */ |
| 1509 |
/* ... and update the book keeping */ |
| 1510 |
garg->ksidcnt--; |
| 1511 |
} |
| 1512 |
} |
| 1513 |
} else { /* Processing addition of a single new group */ |
| 1514 |
|
| 1515 |
/* Check for duplicates */ |
| 1516 |
for (i = 0; i < garg->gidcnt; i++) |
| 1517 |
if (garg->gid_arr[i] == gid) { |
| 1518 |
/* Duplicate group anywhere in the list of groups - ignore */ |
| 1519 |
goto done; |
| 1520 |
} |
| 1521 |
|
| 1522 |
/* Add the current group to the 'flat' list of groups */ |
| 1523 |
garg->gid_arr[garg->gidcnt++] = gid; |
| 1524 |
/* and update the book keeping for the number of groups in current tuple */ |
| 1525 |
garg->tuplcnt_arr[garg->tplcnt]++; |
| 1526 |
|
| 1527 |
/* We want to add a key share for the current group */ |
| 1528 |
if (add_keyshare) |
| 1529 |
garg->ksid_arr[garg->ksidcnt++] = gid; |
| 1530 |
} |
| 1531 |
|
| 1532 |
done: |
| 1533 |
return retval; |
| 1534 |
} |
| 1535 |
|
| 1536 |
/* |
| 1537 |
* Ensure tuplcnt_arr has room for at least tplcnt + 2 entries so that |
| 1538 |
* close_tuple() can safely increment tplcnt and write the new active-tuple |
| 1539 |
* slot at index tplcnt + 1. Must be called before that increment. |
| 1540 |
*/ |
| 1541 |
static int grow_tuples(gid_cb_st *garg) |
| 1542 |
{ |
| 1543 |
static size_t max_tplcnt = (~(size_t)0) / sizeof(size_t); |
| 1544 |
|
| 1545 |
/* |
| 1546 |
* Ensure we have room for at least one additional tuple. |
| 1547 |
* (tplcnt + 1 are in active use). |
| 1548 |
*/ |
| 1549 |
if (garg->tplcnt + 1 == garg->tplmax) { |
| 1550 |
size_t newcnt = garg->tplmax + GROUPLIST_INCREMENT; |
| 1551 |
size_t *tmp; |
| 1552 |
|
| 1553 |
if (newcnt > max_tplcnt |
| 1554 |
|| (tmp = OPENSSL_realloc_array(garg->tuplcnt_arr, newcnt, |
| 1555 |
sizeof(*garg->tuplcnt_arr))) |
| 1556 |
== NULL) |
| 1557 |
return 0; |
| 1558 |
|
| 1559 |
garg->tplmax = newcnt; |
| 1560 |
garg->tuplcnt_arr = tmp; |
| 1561 |
} |
| 1562 |
return 1; |
| 1563 |
} |
| 1564 |
|
| 1565 |
/* |
| 1566 |
* Finalise the active tuple (at index tplcnt) and open a fresh one. |
| 1567 |
* tplcnt is the count of closed tuples; the active tuple lives at tplcnt |
| 1568 |
* throughout parsing. After this call tplcnt is incremented and the new |
| 1569 |
* active tuple at the updated index is initialised to 0. |
| 1570 |
* Empty tuples (gidcnt == 0) are discarded without advancing tplcnt. |
| 1571 |
*/ |
| 1572 |
static int close_tuple(gid_cb_st *garg) |
| 1573 |
{ |
| 1574 |
size_t gidcnt = garg->tuplcnt_arr[garg->tplcnt]; |
| 1575 |
|
| 1576 |
if (gidcnt == 0) |
| 1577 |
return 1; /* Discard empty tuple; no need to open a new slot */ |
| 1578 |
|
| 1579 |
/* Grow before the increment: the new active slot will be at tplcnt + 1 */ |
| 1580 |
if (!grow_tuples(garg)) |
| 1581 |
return 0; |
| 1582 |
|
| 1583 |
/* Promote closed tuple and initialise the new active tuple slot */ |
| 1584 |
garg->tuplcnt_arr[++garg->tplcnt] = 0; |
| 1585 |
return 1; |
| 1586 |
} |
| 1587 |
|
| 1588 |
/* Extract and process a tuple of groups */ |
| 1589 |
static int tuple_cb(const char *tuple, int len, void *arg) |
| 1590 |
{ |
| 1591 |
gid_cb_st *garg = arg; |
| 1592 |
int retval = 1; /* We assume success */ |
| 1593 |
char *restored_tuple_string; |
| 1594 |
|
| 1595 |
/* Sanity checks */ |
| 1596 |
if (garg == NULL || tuple == NULL || len <= 0) { |
| 1597 |
ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
| 1598 |
return 0; |
| 1599 |
} |
| 1600 |
|
| 1601 |
if (garg->inner && !garg->first && !close_tuple(garg)) |
| 1602 |
return 0; |
| 1603 |
garg->first = 0; |
| 1604 |
|
| 1605 |
/* Convert to \0-terminated string */ |
| 1606 |
restored_tuple_string = OPENSSL_malloc(len + 1 /* \0 */); |
| 1607 |
if (restored_tuple_string == NULL) |
| 1608 |
return 0; |
| 1609 |
memcpy(restored_tuple_string, tuple, len); |
| 1610 |
restored_tuple_string[len] = '\0'; |
| 1611 |
|
| 1612 |
/* Analyze group list of this tuple */ |
| 1613 |
retval = CONF_parse_list(restored_tuple_string, GROUP_DELIMITER_CHARACTER, 1, gid_cb, arg); |
| 1614 |
|
| 1615 |
/* We don't need the \o-terminated string anymore */ |
| 1616 |
OPENSSL_free(restored_tuple_string); |
| 1617 |
|
| 1618 |
if (!garg->inner && !close_tuple(garg)) |
| 1619 |
return 0; |
| 1620 |
return retval; |
| 1621 |
} |
| 1622 |
|
| 1623 |
/* |
| 1624 |
* Set groups and prepare generation of keyshares based on a string of groupnames, |
| 1625 |
* names separated by the group or the tuple delimiter, with per-group prefixes to |
| 1626 |
* (1) add a key share for this group, (2) ignore the group if unknown to the current |
| 1627 |
* context, (3) delete a previous occurrence of the group in the current tuple. |
| 1628 |
* |
| 1629 |
* The list parsing is done in two hierarchical steps: The top-level step extracts the |
| 1630 |
* string of a tuple using tuple_cb, while the next lower step uses gid_cb to |
| 1631 |
* parse and process the groups inside a tuple |
| 1632 |
*/ |
| 1633 |
int tls1_set_groups_list(SSL_CTX *ctx, |
| 1634 |
uint16_t **grpext, size_t *grpextlen, |
| 1635 |
uint16_t **ksext, size_t *ksextlen, |
| 1636 |
size_t **tplext, size_t *tplextlen, |
| 1637 |
const char *str) |
| 1638 |
{ |
| 1639 |
size_t i = 0, j; |
| 1640 |
int ret = 0, parse_ret = 0; |
| 1641 |
gid_cb_st gcb; |
| 1642 |
|
| 1643 |
/* Sanity check */ |
| 1644 |
if (ctx == NULL) { |
| 1645 |
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 1646 |
return 0; |
| 1647 |
} |
| 1648 |
|
| 1649 |
memset(&gcb, 0, sizeof(gcb)); |
| 1650 |
gcb.gidmax = GROUPLIST_INCREMENT; |
| 1651 |
gcb.tplmax = GROUPLIST_INCREMENT; |
| 1652 |
gcb.ksidmax = GROUPLIST_INCREMENT; |
| 1653 |
gcb.ctx = ctx; |
| 1654 |
|
| 1655 |
/* Prepare initial chunks of memory for groups, tuples and keyshares groupIDs */ |
| 1656 |
gcb.gid_arr = OPENSSL_malloc_array(gcb.gidmax, sizeof(*gcb.gid_arr)); |
| 1657 |
if (gcb.gid_arr == NULL) |
| 1658 |
goto end; |
| 1659 |
gcb.tuplcnt_arr = OPENSSL_malloc_array(gcb.tplmax, sizeof(*gcb.tuplcnt_arr)); |
| 1660 |
if (gcb.tuplcnt_arr == NULL) |
| 1661 |
goto end; |
| 1662 |
gcb.tuplcnt_arr[0] = 0; |
| 1663 |
gcb.ksid_arr = OPENSSL_malloc_array(gcb.ksidmax, sizeof(*gcb.ksid_arr)); |
| 1664 |
if (gcb.ksid_arr == NULL) |
| 1665 |
goto end; |
| 1666 |
|
| 1667 |
while (str[0] != '\0' && isspace((unsigned char)*str)) |
| 1668 |
str++; |
| 1669 |
if (str[0] == '\0') |
| 1670 |
goto empty_list; |
| 1671 |
|
| 1672 |
/* |
| 1673 |
* Start the (potentially recursive) tuple processing by calling CONF_parse_list |
| 1674 |
* with the TUPLE_DELIMITER_CHARACTER (which will call tuple_cb after cleaning spaces) |
| 1675 |
*/ |
| 1676 |
parse_ret = CONF_parse_list(str, TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, &gcb); |
| 1677 |
|
| 1678 |
if (parse_ret == 0) |
| 1679 |
goto end; |
| 1680 |
if (parse_ret == -1) { |
| 1681 |
ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
| 1682 |
"Syntax error in '%s'", str); |
| 1683 |
goto end; |
| 1684 |
} |
| 1685 |
|
| 1686 |
/* |
| 1687 |
* We check whether a tuple was completely emptied by using "-" prefix |
| 1688 |
* excessively, in which case we remove the tuple |
| 1689 |
*/ |
| 1690 |
for (i = j = 0; j < gcb.tplcnt; j++) { |
| 1691 |
if (gcb.tuplcnt_arr[j] == 0) |
| 1692 |
continue; |
| 1693 |
/* If there's a gap, move to first unfilled slot */ |
| 1694 |
if (j == i) |
| 1695 |
++i; |
| 1696 |
else |
| 1697 |
gcb.tuplcnt_arr[i++] = gcb.tuplcnt_arr[j]; |
| 1698 |
} |
| 1699 |
gcb.tplcnt = i; |
| 1700 |
|
| 1701 |
if (gcb.ksidcnt > OPENSSL_CLIENT_MAX_KEY_SHARES) { |
| 1702 |
ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
| 1703 |
"To many keyshares requested in '%s' (max = %d)", |
| 1704 |
str, OPENSSL_CLIENT_MAX_KEY_SHARES); |
| 1705 |
goto end; |
| 1706 |
} |
| 1707 |
|
| 1708 |
/* |
| 1709 |
* For backward compatibility we let the rest of the code know that a key share |
| 1710 |
* for the first valid group should be added if no "*" prefix was used anywhere |
| 1711 |
*/ |
| 1712 |
if (gcb.gidcnt > 0 && gcb.ksidcnt == 0) { |
| 1713 |
/* |
| 1714 |
* No key share group prefix character was used, hence we indicate that a single |
| 1715 |
* key share should be sent and flag that it should come from the supported_groups list |
| 1716 |
*/ |
| 1717 |
gcb.ksidcnt = 1; |
| 1718 |
gcb.ksid_arr[0] = 0; |
| 1719 |
} |
| 1720 |
|
| 1721 |
empty_list: |
| 1722 |
/* |
| 1723 |
* A call to tls1_set_groups_list with any of the args (other than ctx) set |
| 1724 |
* to NULL only does a syntax check, hence we're done here and report success |
| 1725 |
*/ |
| 1726 |
if (grpext == NULL || ksext == NULL || tplext == NULL || grpextlen == NULL || ksextlen == NULL || tplextlen == NULL) { |
| 1727 |
ret = 1; |
| 1728 |
goto end; |
| 1729 |
} |
| 1730 |
|
| 1731 |
/* |
| 1732 |
* tuple_cb and gid_cb combo ensures there are no duplicates or unknown groups so we |
| 1733 |
* can just go ahead and set the results (after disposing the existing) |
| 1734 |
*/ |
| 1735 |
OPENSSL_free(*grpext); |
| 1736 |
*grpext = gcb.gid_arr; |
| 1737 |
*grpextlen = gcb.gidcnt; |
| 1738 |
OPENSSL_free(*ksext); |
| 1739 |
*ksext = gcb.ksid_arr; |
| 1740 |
*ksextlen = gcb.ksidcnt; |
| 1741 |
OPENSSL_free(*tplext); |
| 1742 |
*tplext = gcb.tuplcnt_arr; |
| 1743 |
*tplextlen = gcb.tplcnt; |
| 1744 |
|
| 1745 |
return 1; |
| 1746 |
|
| 1747 |
end: |
| 1748 |
OPENSSL_free(gcb.gid_arr); |
| 1749 |
OPENSSL_free(gcb.tuplcnt_arr); |
| 1750 |
OPENSSL_free(gcb.ksid_arr); |
| 1751 |
return ret; |
| 1752 |
} |
| 1753 |
|
| 1754 |
/* Check a group id matches preferences */ |
| 1755 |
int tls1_check_group_id(SSL_CONNECTION *s, uint16_t group_id, |
| 1756 |
int check_own_groups) |
| 1757 |
{ |
| 1758 |
const uint16_t *groups; |
| 1759 |
size_t groups_len; |
| 1760 |
|
| 1761 |
if (group_id == 0) |
| 1762 |
return 0; |
| 1763 |
|
| 1764 |
/* Check for Suite B compliance */ |
| 1765 |
if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) { |
| 1766 |
unsigned long cid = s->s3.tmp.new_cipher->id; |
| 1767 |
|
| 1768 |
if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { |
| 1769 |
if (group_id != OSSL_TLS_GROUP_ID_secp256r1) |
| 1770 |
return 0; |
| 1771 |
} else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { |
| 1772 |
if (group_id != OSSL_TLS_GROUP_ID_secp384r1) |
| 1773 |
return 0; |
| 1774 |
} else { |
| 1775 |
/* Should never happen */ |
| 1776 |
return 0; |
| 1777 |
} |
| 1778 |
} |
| 1779 |
|
| 1780 |
if (check_own_groups) { |
| 1781 |
/* Check group is one of our preferences */ |
| 1782 |
tls1_get_supported_groups(s, &groups, &groups_len); |
| 1783 |
if (!tls1_in_list(group_id, groups, groups_len)) |
| 1784 |
return 0; |
| 1785 |
} |
| 1786 |
|
| 1787 |
if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK)) |
| 1788 |
return 0; |
| 1789 |
|
| 1790 |
/* For clients, nothing more to check */ |
| 1791 |
if (!s->server) |
| 1792 |
return 1; |
| 1793 |
|
| 1794 |
/* Check group is one of peers preferences */ |
| 1795 |
tls1_get_peer_groups(s, &groups, &groups_len); |
| 1796 |
|
| 1797 |
/* |
| 1798 |
* RFC 4492 does not require the supported elliptic curves extension |
| 1799 |
* so if it is not sent we can just choose any curve. |
| 1800 |
* It is invalid to send an empty list in the supported groups |
| 1801 |
* extension, so groups_len == 0 always means no extension. |
| 1802 |
*/ |
| 1803 |
if (groups_len == 0) |
| 1804 |
return 1; |
| 1805 |
return tls1_in_list(group_id, groups, groups_len); |
| 1806 |
} |
| 1807 |
|
| 1808 |
void tls1_get_formatlist(SSL_CONNECTION *s, const unsigned char **pformats, |
| 1809 |
size_t *num_formats) |
| 1810 |
{ |
| 1811 |
/* |
| 1812 |
* If we have a custom point format list use it otherwise use default |
| 1813 |
*/ |
| 1814 |
if (s->ext.ecpointformats) { |
| 1815 |
*pformats = s->ext.ecpointformats; |
| 1816 |
*num_formats = s->ext.ecpointformats_len; |
| 1817 |
} else if ((s->options & SSL_OP_LEGACY_EC_POINT_FORMATS) != 0) { |
| 1818 |
*pformats = ecformats_all; |
| 1819 |
/* For Suite B we don't support char2 fields */ |
| 1820 |
if (tls1_suiteb(s)) |
| 1821 |
*num_formats = sizeof(ecformats_all) - 1; |
| 1822 |
else |
| 1823 |
*num_formats = sizeof(ecformats_all); |
| 1824 |
} else { |
| 1825 |
*pformats = ecformats_default; |
| 1826 |
*num_formats = sizeof(ecformats_default); |
| 1827 |
} |
| 1828 |
} |
| 1829 |
|
| 1830 |
/* Check a key is compatible with compression extension */ |
| 1831 |
static int tls1_check_pkey_comp(SSL_CONNECTION *s, EVP_PKEY *pkey) |
| 1832 |
{ |
| 1833 |
unsigned char comp_id; |
| 1834 |
size_t i; |
| 1835 |
int point_conv; |
| 1836 |
|
| 1837 |
/* If not an EC key nothing to check */ |
| 1838 |
if (!EVP_PKEY_is_a(pkey, "EC")) |
| 1839 |
return 1; |
| 1840 |
|
| 1841 |
/* Get required compression id */ |
| 1842 |
point_conv = EVP_PKEY_get_ec_point_conv_form(pkey); |
| 1843 |
if (point_conv == 0) |
| 1844 |
return 0; |
| 1845 |
if (point_conv == POINT_CONVERSION_UNCOMPRESSED) { |
| 1846 |
comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 1847 |
} else if (SSL_CONNECTION_IS_TLS13(s)) { |
| 1848 |
/* |
| 1849 |
* ec_point_formats extension is not used in TLSv1.3 so we ignore |
| 1850 |
* this check. |
| 1851 |
*/ |
| 1852 |
return 1; |
| 1853 |
} else { |
| 1854 |
int field_type = EVP_PKEY_get_field_type(pkey); |
| 1855 |
|
| 1856 |
if (field_type == NID_X9_62_prime_field) |
| 1857 |
comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 1858 |
else if (field_type == NID_X9_62_characteristic_two_field) |
| 1859 |
comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
| 1860 |
else |
| 1861 |
return 0; |
| 1862 |
} |
| 1863 |
/* |
| 1864 |
* If point formats extension present check it, otherwise everything is |
| 1865 |
* supported (see RFC4492). |
| 1866 |
*/ |
| 1867 |
if (s->ext.peer_ecpointformats == NULL) |
| 1868 |
return 1; |
| 1869 |
|
| 1870 |
for (i = 0; i < s->ext.peer_ecpointformats_len; i++) { |
| 1871 |
if (s->ext.peer_ecpointformats[i] == comp_id) |
| 1872 |
return 1; |
| 1873 |
} |
| 1874 |
return 0; |
| 1875 |
} |
| 1876 |
|
| 1877 |
/* Return group id of a key */ |
| 1878 |
static uint16_t tls1_get_group_id(EVP_PKEY *pkey) |
| 1879 |
{ |
| 1880 |
int curve_nid = ssl_get_EC_curve_nid(pkey); |
| 1881 |
|
| 1882 |
if (curve_nid == NID_undef) |
| 1883 |
return 0; |
| 1884 |
return tls1_nid2group_id(curve_nid); |
| 1885 |
} |
| 1886 |
|
| 1887 |
/* |
| 1888 |
* Check cert parameters compatible with extensions: currently just checks EC |
| 1889 |
* certificates have compatible curves and compression. |
| 1890 |
*/ |
| 1891 |
static int tls1_check_cert_param(SSL_CONNECTION *s, X509 *x, int check_ee_md) |
| 1892 |
{ |
| 1893 |
uint16_t group_id; |
| 1894 |
EVP_PKEY *pkey; |
| 1895 |
pkey = X509_get0_pubkey(x); |
| 1896 |
if (pkey == NULL) |
| 1897 |
return 0; |
| 1898 |
/* If not EC nothing to do */ |
| 1899 |
if (!EVP_PKEY_is_a(pkey, "EC")) |
| 1900 |
return 1; |
| 1901 |
/* Check compression */ |
| 1902 |
if (!tls1_check_pkey_comp(s, pkey)) |
| 1903 |
return 0; |
| 1904 |
group_id = tls1_get_group_id(pkey); |
| 1905 |
/* |
| 1906 |
* For a server we allow the certificate to not be in our list of supported |
| 1907 |
* groups. |
| 1908 |
*/ |
| 1909 |
if (!tls1_check_group_id(s, group_id, !s->server)) |
| 1910 |
return 0; |
| 1911 |
/* |
| 1912 |
* Special case for suite B. We *MUST* sign using SHA256+P-256 or |
| 1913 |
* SHA384+P-384. |
| 1914 |
*/ |
| 1915 |
if (check_ee_md && tls1_suiteb(s)) { |
| 1916 |
int check_md; |
| 1917 |
size_t i; |
| 1918 |
|
| 1919 |
/* Check to see we have necessary signing algorithm */ |
| 1920 |
if (group_id == OSSL_TLS_GROUP_ID_secp256r1) |
| 1921 |
check_md = NID_ecdsa_with_SHA256; |
| 1922 |
else if (group_id == OSSL_TLS_GROUP_ID_secp384r1) |
| 1923 |
check_md = NID_ecdsa_with_SHA384; |
| 1924 |
else |
| 1925 |
return 0; /* Should never happen */ |
| 1926 |
for (i = 0; i < s->shared_sigalgslen; i++) { |
| 1927 |
if (check_md == s->shared_sigalgs[i]->sigandhash) |
| 1928 |
return 1; |
| 1929 |
} |
| 1930 |
return 0; |
| 1931 |
} |
| 1932 |
return 1; |
| 1933 |
} |
| 1934 |
|
| 1935 |
/* |
| 1936 |
* tls1_check_ec_tmp_key - Check EC temporary key compatibility |
| 1937 |
* @s: SSL connection |
| 1938 |
* @cid: Cipher ID we're considering using |
| 1939 |
* |
| 1940 |
* Checks that the kECDHE cipher suite we're considering using |
| 1941 |
* is compatible with the client extensions. |
| 1942 |
* |
| 1943 |
* Returns 0 when the cipher can't be used or 1 when it can. |
| 1944 |
*/ |
| 1945 |
int tls1_check_ec_tmp_key(SSL_CONNECTION *s, unsigned long cid) |
| 1946 |
{ |
| 1947 |
/* If not Suite B just need a shared group */ |
| 1948 |
if (!tls1_suiteb(s)) |
| 1949 |
return tls1_shared_group(s, 0) != 0; |
| 1950 |
/* |
| 1951 |
* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other |
| 1952 |
* curves permitted. |
| 1953 |
*/ |
| 1954 |
if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
| 1955 |
return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp256r1, 1); |
| 1956 |
if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
| 1957 |
return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp384r1, 1); |
| 1958 |
|
| 1959 |
return 0; |
| 1960 |
} |
| 1961 |
|
| 1962 |
/* Default sigalg schemes */ |
| 1963 |
static const uint16_t tls12_sigalgs[] = { |
| 1964 |
TLSEXT_SIGALG_mldsa65, |
| 1965 |
TLSEXT_SIGALG_mldsa87, |
| 1966 |
TLSEXT_SIGALG_mldsa44, |
| 1967 |
TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
| 1968 |
TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
| 1969 |
TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
| 1970 |
TLSEXT_SIGALG_ed25519, |
| 1971 |
TLSEXT_SIGALG_ed448, |
| 1972 |
TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
| 1973 |
TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, |
| 1974 |
TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, |
| 1975 |
|
| 1976 |
TLSEXT_SIGALG_rsa_pss_pss_sha256, |
| 1977 |
TLSEXT_SIGALG_rsa_pss_pss_sha384, |
| 1978 |
TLSEXT_SIGALG_rsa_pss_pss_sha512, |
| 1979 |
TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
| 1980 |
TLSEXT_SIGALG_rsa_pss_rsae_sha384, |
| 1981 |
TLSEXT_SIGALG_rsa_pss_rsae_sha512, |
| 1982 |
|
| 1983 |
TLSEXT_SIGALG_rsa_pkcs1_sha256, |
| 1984 |
TLSEXT_SIGALG_rsa_pkcs1_sha384, |
| 1985 |
TLSEXT_SIGALG_rsa_pkcs1_sha512, |
| 1986 |
|
| 1987 |
TLSEXT_SIGALG_ecdsa_sha224, |
| 1988 |
TLSEXT_SIGALG_ecdsa_sha1, |
| 1989 |
|
| 1990 |
TLSEXT_SIGALG_rsa_pkcs1_sha224, |
| 1991 |
TLSEXT_SIGALG_rsa_pkcs1_sha1, |
| 1992 |
|
| 1993 |
TLSEXT_SIGALG_dsa_sha224, |
| 1994 |
TLSEXT_SIGALG_dsa_sha1, |
| 1995 |
|
| 1996 |
TLSEXT_SIGALG_dsa_sha256, |
| 1997 |
TLSEXT_SIGALG_dsa_sha384, |
| 1998 |
TLSEXT_SIGALG_dsa_sha512, |
| 1999 |
|
| 2000 |
#ifndef OPENSSL_NO_GOST |
| 2001 |
TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
| 2002 |
TLSEXT_SIGALG_gostr34102012_512_intrinsic, |
| 2003 |
TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
| 2004 |
TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
| 2005 |
TLSEXT_SIGALG_gostr34102001_gostr3411, |
| 2006 |
#endif |
| 2007 |
}; |
| 2008 |
|
| 2009 |
static const uint16_t suiteb_sigalgs[] = { |
| 2010 |
TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
| 2011 |
TLSEXT_SIGALG_ecdsa_secp384r1_sha384 |
| 2012 |
}; |
| 2013 |
|
| 2014 |
static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
| 2015 |
{ TLSEXT_SIGALG_ecdsa_secp256r1_sha256_name, |
| 2016 |
"ECDSA+SHA256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
| 2017 |
NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2018 |
NID_ecdsa_with_SHA256, NID_X9_62_prime256v1, 1, 0, |
| 2019 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2020 |
{ TLSEXT_SIGALG_ecdsa_secp384r1_sha384_name, |
| 2021 |
"ECDSA+SHA384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
| 2022 |
NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2023 |
NID_ecdsa_with_SHA384, NID_secp384r1, 1, 0, |
| 2024 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2025 |
{ TLSEXT_SIGALG_ecdsa_secp521r1_sha512_name, |
| 2026 |
"ECDSA+SHA512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
| 2027 |
NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2028 |
NID_ecdsa_with_SHA512, NID_secp521r1, 1, 0, |
| 2029 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2030 |
|
| 2031 |
{ TLSEXT_SIGALG_ed25519_name, |
| 2032 |
NULL, TLSEXT_SIGALG_ed25519, |
| 2033 |
NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519, |
| 2034 |
NID_undef, NID_undef, 1, 0, |
| 2035 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2036 |
{ TLSEXT_SIGALG_ed448_name, |
| 2037 |
NULL, TLSEXT_SIGALG_ed448, |
| 2038 |
NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448, |
| 2039 |
NID_undef, NID_undef, 1, 0, |
| 2040 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2041 |
|
| 2042 |
{ TLSEXT_SIGALG_ecdsa_sha224_name, |
| 2043 |
"ECDSA+SHA224", TLSEXT_SIGALG_ecdsa_sha224, |
| 2044 |
NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2045 |
NID_ecdsa_with_SHA224, NID_undef, 1, 0, |
| 2046 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2047 |
{ TLSEXT_SIGALG_ecdsa_sha1_name, |
| 2048 |
"ECDSA+SHA1", TLSEXT_SIGALG_ecdsa_sha1, |
| 2049 |
NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2050 |
NID_ecdsa_with_SHA1, NID_undef, 1, 0, |
| 2051 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2052 |
|
| 2053 |
{ TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_name, |
| 2054 |
TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_alias, |
| 2055 |
TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
| 2056 |
NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2057 |
NID_ecdsa_with_SHA256, NID_brainpoolP256r1, 1, 0, |
| 2058 |
TLS1_3_VERSION, 0, -1, -1 }, |
| 2059 |
{ TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_name, |
| 2060 |
TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_alias, |
| 2061 |
TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, |
| 2062 |
NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2063 |
NID_ecdsa_with_SHA384, NID_brainpoolP384r1, 1, 0, |
| 2064 |
TLS1_3_VERSION, 0, -1, -1 }, |
| 2065 |
{ TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_name, |
| 2066 |
TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_alias, |
| 2067 |
TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, |
| 2068 |
NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 2069 |
NID_ecdsa_with_SHA512, NID_brainpoolP512r1, 1, 0, |
| 2070 |
TLS1_3_VERSION, 0, -1, -1 }, |
| 2071 |
|
| 2072 |
{ TLSEXT_SIGALG_rsa_pss_rsae_sha256_name, |
| 2073 |
"PSS+SHA256", TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
| 2074 |
NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
| 2075 |
NID_undef, NID_undef, 1, 0, |
| 2076 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2077 |
{ TLSEXT_SIGALG_rsa_pss_rsae_sha384_name, |
| 2078 |
"PSS+SHA384", TLSEXT_SIGALG_rsa_pss_rsae_sha384, |
| 2079 |
NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
| 2080 |
NID_undef, NID_undef, 1, 0, |
| 2081 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2082 |
{ TLSEXT_SIGALG_rsa_pss_rsae_sha512_name, |
| 2083 |
"PSS+SHA512", TLSEXT_SIGALG_rsa_pss_rsae_sha512, |
| 2084 |
NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
| 2085 |
NID_undef, NID_undef, 1, 0, |
| 2086 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2087 |
|
| 2088 |
{ TLSEXT_SIGALG_rsa_pss_pss_sha256_name, |
| 2089 |
NULL, TLSEXT_SIGALG_rsa_pss_pss_sha256, |
| 2090 |
NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 2091 |
NID_undef, NID_undef, 1, 0, |
| 2092 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2093 |
{ TLSEXT_SIGALG_rsa_pss_pss_sha384_name, |
| 2094 |
NULL, TLSEXT_SIGALG_rsa_pss_pss_sha384, |
| 2095 |
NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 2096 |
NID_undef, NID_undef, 1, 0, |
| 2097 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2098 |
{ TLSEXT_SIGALG_rsa_pss_pss_sha512_name, |
| 2099 |
NULL, TLSEXT_SIGALG_rsa_pss_pss_sha512, |
| 2100 |
NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 2101 |
NID_undef, NID_undef, 1, 0, |
| 2102 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2103 |
|
| 2104 |
{ TLSEXT_SIGALG_rsa_pkcs1_sha256_name, |
| 2105 |
"RSA+SHA256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
| 2106 |
NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 2107 |
NID_sha256WithRSAEncryption, NID_undef, 1, 0, |
| 2108 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2109 |
{ TLSEXT_SIGALG_rsa_pkcs1_sha384_name, |
| 2110 |
"RSA+SHA384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
| 2111 |
NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 2112 |
NID_sha384WithRSAEncryption, NID_undef, 1, 0, |
| 2113 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2114 |
{ TLSEXT_SIGALG_rsa_pkcs1_sha512_name, |
| 2115 |
"RSA+SHA512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
| 2116 |
NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 2117 |
NID_sha512WithRSAEncryption, NID_undef, 1, 0, |
| 2118 |
TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
| 2119 |
|
| 2120 |
{ TLSEXT_SIGALG_rsa_pkcs1_sha224_name, |
| 2121 |
"RSA+SHA224", TLSEXT_SIGALG_rsa_pkcs1_sha224, |
| 2122 |
NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 2123 |
NID_sha224WithRSAEncryption, NID_undef, 1, 0, |
| 2124 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2125 |
{ TLSEXT_SIGALG_rsa_pkcs1_sha1_name, |
| 2126 |
"RSA+SHA1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
| 2127 |
NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 2128 |
NID_sha1WithRSAEncryption, NID_undef, 1, 0, |
| 2129 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2130 |
|
| 2131 |
{ TLSEXT_SIGALG_dsa_sha256_name, |
| 2132 |
"DSA+SHA256", TLSEXT_SIGALG_dsa_sha256, |
| 2133 |
NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 2134 |
NID_dsa_with_SHA256, NID_undef, 1, 0, |
| 2135 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2136 |
{ TLSEXT_SIGALG_dsa_sha384_name, |
| 2137 |
"DSA+SHA384", TLSEXT_SIGALG_dsa_sha384, |
| 2138 |
NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 2139 |
NID_undef, NID_undef, 1, 0, |
| 2140 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2141 |
{ TLSEXT_SIGALG_dsa_sha512_name, |
| 2142 |
"DSA+SHA512", TLSEXT_SIGALG_dsa_sha512, |
| 2143 |
NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 2144 |
NID_undef, NID_undef, 1, 0, |
| 2145 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2146 |
{ TLSEXT_SIGALG_dsa_sha224_name, |
| 2147 |
"DSA+SHA224", TLSEXT_SIGALG_dsa_sha224, |
| 2148 |
NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 2149 |
NID_undef, NID_undef, 1, 0, |
| 2150 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2151 |
{ TLSEXT_SIGALG_dsa_sha1_name, |
| 2152 |
"DSA+SHA1", TLSEXT_SIGALG_dsa_sha1, |
| 2153 |
NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 2154 |
NID_dsaWithSHA1, NID_undef, 1, 0, |
| 2155 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2156 |
|
| 2157 |
#ifndef OPENSSL_NO_GOST |
| 2158 |
{ TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ |
| 2159 |
TLSEXT_SIGALG_gostr34102012_256_intrinsic_name, |
| 2160 |
TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
| 2161 |
NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
| 2162 |
NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
| 2163 |
NID_undef, NID_undef, 1, 0, |
| 2164 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2165 |
{ TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ |
| 2166 |
TLSEXT_SIGALG_gostr34102012_256_intrinsic_name, |
| 2167 |
TLSEXT_SIGALG_gostr34102012_512_intrinsic, |
| 2168 |
NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
| 2169 |
NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
| 2170 |
NID_undef, NID_undef, 1, 0, |
| 2171 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2172 |
|
| 2173 |
{ TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256_name, |
| 2174 |
NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
| 2175 |
NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
| 2176 |
NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
| 2177 |
NID_undef, NID_undef, 1, 0, |
| 2178 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2179 |
{ TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512_name, |
| 2180 |
NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
| 2181 |
NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
| 2182 |
NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
| 2183 |
NID_undef, NID_undef, 1, 0, |
| 2184 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2185 |
{ TLSEXT_SIGALG_gostr34102001_gostr3411_name, |
| 2186 |
NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
| 2187 |
NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
| 2188 |
NID_id_GostR3410_2001, SSL_PKEY_GOST01, |
| 2189 |
NID_undef, NID_undef, 1, 0, |
| 2190 |
TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
| 2191 |
#endif |
| 2192 |
}; |
| 2193 |
/* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
| 2194 |
static const SIGALG_LOOKUP legacy_rsa_sigalg = { |
| 2195 |
"rsa_pkcs1_md5_sha1", NULL, 0, |
| 2196 |
NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, |
| 2197 |
EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 2198 |
NID_undef, NID_undef, 1, 0, |
| 2199 |
TLS1_VERSION, TLS1_2_VERSION, DTLS1_VERSION, DTLS1_2_VERSION |
| 2200 |
}; |
| 2201 |
|
| 2202 |
/* |
| 2203 |
* Default signature algorithm values used if signature algorithms not present. |
| 2204 |
* From RFC5246. Note: order must match certificate index order. |
| 2205 |
*/ |
| 2206 |
static const uint16_t tls_default_sigalg[] = { |
| 2207 |
TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ |
| 2208 |
0, /* SSL_PKEY_RSA_PSS_SIGN */ |
| 2209 |
TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ |
| 2210 |
TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ |
| 2211 |
TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ |
| 2212 |
TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */ |
| 2213 |
TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */ |
| 2214 |
0, /* SSL_PKEY_ED25519 */ |
| 2215 |
0, /* SSL_PKEY_ED448 */ |
| 2216 |
}; |
| 2217 |
|
| 2218 |
int ssl_setup_sigalgs(SSL_CTX *ctx) |
| 2219 |
{ |
| 2220 |
size_t i, cache_idx, sigalgs_len, enabled; |
| 2221 |
const SIGALG_LOOKUP *lu; |
| 2222 |
SIGALG_LOOKUP *cache = NULL; |
| 2223 |
uint16_t *tls12_sigalgs_list = NULL; |
| 2224 |
EVP_PKEY *tmpkey = EVP_PKEY_new(); |
| 2225 |
int istls; |
| 2226 |
int ret = 0; |
| 2227 |
|
| 2228 |
if (ctx == NULL) |
| 2229 |
goto err; |
| 2230 |
|
| 2231 |
istls = !SSL_CTX_IS_DTLS(ctx); |
| 2232 |
|
| 2233 |
sigalgs_len = OSSL_NELEM(sigalg_lookup_tbl) + ctx->sigalg_list_len; |
| 2234 |
|
| 2235 |
cache = OPENSSL_calloc(sigalgs_len, sizeof(const SIGALG_LOOKUP)); |
| 2236 |
if (cache == NULL || tmpkey == NULL) |
| 2237 |
goto err; |
| 2238 |
|
| 2239 |
tls12_sigalgs_list = OPENSSL_calloc(sigalgs_len, sizeof(uint16_t)); |
| 2240 |
if (tls12_sigalgs_list == NULL) |
| 2241 |
goto err; |
| 2242 |
|
| 2243 |
ERR_set_mark(); |
| 2244 |
/* First fill cache and tls12_sigalgs list from legacy algorithm list */ |
| 2245 |
for (i = 0, lu = sigalg_lookup_tbl; |
| 2246 |
i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { |
| 2247 |
EVP_PKEY_CTX *pctx; |
| 2248 |
|
| 2249 |
cache[i] = *lu; |
| 2250 |
|
| 2251 |
/* |
| 2252 |
* Check hash is available. |
| 2253 |
* This test is not perfect. A provider could have support |
| 2254 |
* for a signature scheme, but not a particular hash. However the hash |
| 2255 |
* could be available from some other loaded provider. In that case it |
| 2256 |
* could be that the signature is available, and the hash is available |
| 2257 |
* independently - but not as a combination. We ignore this for now. |
| 2258 |
*/ |
| 2259 |
if (lu->hash != NID_undef |
| 2260 |
&& ctx->ssl_digest_methods[lu->hash_idx] == NULL) { |
| 2261 |
cache[i].available = 0; |
| 2262 |
continue; |
| 2263 |
} |
| 2264 |
|
| 2265 |
if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { |
| 2266 |
cache[i].available = 0; |
| 2267 |
continue; |
| 2268 |
} |
| 2269 |
pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, tmpkey, ctx->propq); |
| 2270 |
/* If unable to create pctx we assume the sig algorithm is unavailable */ |
| 2271 |
if (pctx == NULL) |
| 2272 |
cache[i].available = 0; |
| 2273 |
EVP_PKEY_CTX_free(pctx); |
| 2274 |
} |
| 2275 |
|
| 2276 |
/* Now complete cache and tls12_sigalgs list with provider sig information */ |
| 2277 |
cache_idx = OSSL_NELEM(sigalg_lookup_tbl); |
| 2278 |
for (i = 0; i < ctx->sigalg_list_len; i++) { |
| 2279 |
TLS_SIGALG_INFO si = ctx->sigalg_list[i]; |
| 2280 |
cache[cache_idx].name = si.name; |
| 2281 |
cache[cache_idx].name12 = si.sigalg_name; |
| 2282 |
cache[cache_idx].sigalg = si.code_point; |
| 2283 |
tls12_sigalgs_list[cache_idx] = si.code_point; |
| 2284 |
cache[cache_idx].hash = si.hash_name ? OBJ_txt2nid(si.hash_name) : NID_undef; |
| 2285 |
cache[cache_idx].hash_idx = ssl_get_md_idx(cache[cache_idx].hash); |
| 2286 |
cache[cache_idx].sig = OBJ_txt2nid(si.sigalg_name); |
| 2287 |
cache[cache_idx].sig_idx = (int)(i + SSL_PKEY_NUM); |
| 2288 |
cache[cache_idx].sigandhash = OBJ_txt2nid(si.sigalg_name); |
| 2289 |
cache[cache_idx].curve = NID_undef; |
| 2290 |
cache[cache_idx].mintls = TLS1_3_VERSION; |
| 2291 |
cache[cache_idx].maxtls = TLS1_3_VERSION; |
| 2292 |
cache[cache_idx].mindtls = -1; |
| 2293 |
cache[cache_idx].maxdtls = -1; |
| 2294 |
/* Compatibility with TLS 1.3 is checked on load */ |
| 2295 |
cache[cache_idx].available = istls; |
| 2296 |
cache[cache_idx].advertise = 0; |
| 2297 |
cache_idx++; |
| 2298 |
} |
| 2299 |
ERR_pop_to_mark(); |
| 2300 |
|
| 2301 |
enabled = 0; |
| 2302 |
for (i = 0; i < OSSL_NELEM(tls12_sigalgs); ++i) { |
| 2303 |
SIGALG_LOOKUP *ent = cache; |
| 2304 |
size_t j; |
| 2305 |
|
| 2306 |
for (j = 0; j < sigalgs_len; ent++, j++) { |
| 2307 |
if (ent->sigalg != tls12_sigalgs[i]) |
| 2308 |
continue; |
| 2309 |
/* Dedup by marking cache entry as default enabled. */ |
| 2310 |
if (ent->available && !ent->advertise) { |
| 2311 |
ent->advertise = 1; |
| 2312 |
tls12_sigalgs_list[enabled++] = tls12_sigalgs[i]; |
| 2313 |
} |
| 2314 |
break; |
| 2315 |
} |
| 2316 |
} |
| 2317 |
|
| 2318 |
/* Append any provider sigalgs not yet handled */ |
| 2319 |
for (i = OSSL_NELEM(sigalg_lookup_tbl); i < sigalgs_len; ++i) { |
| 2320 |
SIGALG_LOOKUP *ent = &cache[i]; |
| 2321 |
|
| 2322 |
if (ent->available && !ent->advertise) |
| 2323 |
tls12_sigalgs_list[enabled++] = ent->sigalg; |
| 2324 |
} |
| 2325 |
|
| 2326 |
ctx->sigalg_lookup_cache = cache; |
| 2327 |
ctx->sigalg_lookup_cache_len = sigalgs_len; |
| 2328 |
ctx->tls12_sigalgs = tls12_sigalgs_list; |
| 2329 |
ctx->tls12_sigalgs_len = enabled; |
| 2330 |
cache = NULL; |
| 2331 |
tls12_sigalgs_list = NULL; |
| 2332 |
|
| 2333 |
ret = 1; |
| 2334 |
err: |
| 2335 |
OPENSSL_free(cache); |
| 2336 |
OPENSSL_free(tls12_sigalgs_list); |
| 2337 |
EVP_PKEY_free(tmpkey); |
| 2338 |
return ret; |
| 2339 |
} |
| 2340 |
|
| 2341 |
#define SIGLEN_BUF_INCREMENT 100 |
| 2342 |
|
| 2343 |
char *SSL_get1_builtin_sigalgs(OSSL_LIB_CTX *libctx) |
| 2344 |
{ |
| 2345 |
size_t i, maxretlen = SIGLEN_BUF_INCREMENT; |
| 2346 |
const SIGALG_LOOKUP *lu; |
| 2347 |
EVP_PKEY *tmpkey = EVP_PKEY_new(); |
| 2348 |
char *retval = OPENSSL_malloc(maxretlen); |
| 2349 |
|
| 2350 |
if (retval == NULL) |
| 2351 |
return NULL; |
| 2352 |
|
| 2353 |
/* ensure retval string is NUL terminated */ |
| 2354 |
retval[0] = (char)0; |
| 2355 |
|
| 2356 |
for (i = 0, lu = sigalg_lookup_tbl; |
| 2357 |
i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { |
| 2358 |
EVP_PKEY_CTX *pctx; |
| 2359 |
int enabled = 1; |
| 2360 |
|
| 2361 |
ERR_set_mark(); |
| 2362 |
/* Check hash is available in some provider. */ |
| 2363 |
if (lu->hash != NID_undef) { |
| 2364 |
EVP_MD *hash = EVP_MD_fetch(libctx, OBJ_nid2ln(lu->hash), NULL); |
| 2365 |
|
| 2366 |
/* If unable to create we assume the hash algorithm is unavailable */ |
| 2367 |
if (hash == NULL) { |
| 2368 |
enabled = 0; |
| 2369 |
ERR_pop_to_mark(); |
| 2370 |
continue; |
| 2371 |
} |
| 2372 |
EVP_MD_free(hash); |
| 2373 |
} |
| 2374 |
|
| 2375 |
if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { |
| 2376 |
enabled = 0; |
| 2377 |
ERR_pop_to_mark(); |
| 2378 |
continue; |
| 2379 |
} |
| 2380 |
pctx = EVP_PKEY_CTX_new_from_pkey(libctx, tmpkey, NULL); |
| 2381 |
/* If unable to create pctx we assume the sig algorithm is unavailable */ |
| 2382 |
if (pctx == NULL) |
| 2383 |
enabled = 0; |
| 2384 |
ERR_pop_to_mark(); |
| 2385 |
EVP_PKEY_CTX_free(pctx); |
| 2386 |
|
| 2387 |
if (enabled) { |
| 2388 |
const char *sa = lu->name; |
| 2389 |
|
| 2390 |
if (sa != NULL) { |
| 2391 |
if (strlen(sa) + strlen(retval) + 1 >= maxretlen) { |
| 2392 |
char *tmp; |
| 2393 |
|
| 2394 |
maxretlen += SIGLEN_BUF_INCREMENT; |
| 2395 |
tmp = OPENSSL_realloc(retval, maxretlen); |
| 2396 |
if (tmp == NULL) { |
| 2397 |
OPENSSL_free(retval); |
| 2398 |
return NULL; |
| 2399 |
} |
| 2400 |
retval = tmp; |
| 2401 |
} |
| 2402 |
if (strlen(retval) > 0) |
| 2403 |
OPENSSL_strlcat(retval, ":", maxretlen); |
| 2404 |
OPENSSL_strlcat(retval, sa, maxretlen); |
| 2405 |
} else { |
| 2406 |
/* lu->name must not be NULL */ |
| 2407 |
ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
| 2408 |
} |
| 2409 |
} |
| 2410 |
} |
| 2411 |
|
| 2412 |
EVP_PKEY_free(tmpkey); |
| 2413 |
return retval; |
| 2414 |
} |
| 2415 |
|
| 2416 |
/* Lookup TLS signature algorithm */ |
| 2417 |
static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL_CTX *ctx, |
| 2418 |
uint16_t sigalg) |
| 2419 |
{ |
| 2420 |
size_t i; |
| 2421 |
const SIGALG_LOOKUP *lu = ctx->sigalg_lookup_cache; |
| 2422 |
|
| 2423 |
for (i = 0; i < ctx->sigalg_lookup_cache_len; lu++, i++) { |
| 2424 |
if (lu->sigalg == sigalg) { |
| 2425 |
if (!lu->available) |
| 2426 |
return NULL; |
| 2427 |
return lu; |
| 2428 |
} |
| 2429 |
} |
| 2430 |
return NULL; |
| 2431 |
} |
| 2432 |
|
| 2433 |
/* Lookup hash: return 0 if invalid or not enabled */ |
| 2434 |
int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd) |
| 2435 |
{ |
| 2436 |
const EVP_MD *md; |
| 2437 |
|
| 2438 |
if (lu == NULL) |
| 2439 |
return 0; |
| 2440 |
/* lu->hash == NID_undef means no associated digest */ |
| 2441 |
if (lu->hash == NID_undef) { |
| 2442 |
md = NULL; |
| 2443 |
} else { |
| 2444 |
md = ssl_md(ctx, lu->hash_idx); |
| 2445 |
if (md == NULL) |
| 2446 |
return 0; |
| 2447 |
} |
| 2448 |
if (pmd) |
| 2449 |
*pmd = md; |
| 2450 |
return 1; |
| 2451 |
} |
| 2452 |
|
| 2453 |
/* |
| 2454 |
* Check if key is large enough to generate RSA-PSS signature. |
| 2455 |
* |
| 2456 |
* The key must greater than or equal to 2 * hash length + 2. |
| 2457 |
* SHA512 has a hash length of 64 bytes, which is incompatible |
| 2458 |
* with a 128 byte (1024 bit) key. |
| 2459 |
*/ |
| 2460 |
#define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_get_size(md) + 2) |
| 2461 |
static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey, |
| 2462 |
const SIGALG_LOOKUP *lu) |
| 2463 |
{ |
| 2464 |
const EVP_MD *md; |
| 2465 |
|
| 2466 |
if (pkey == NULL) |
| 2467 |
return 0; |
| 2468 |
if (!tls1_lookup_md(ctx, lu, &md) || md == NULL) |
| 2469 |
return 0; |
| 2470 |
if (EVP_MD_get_size(md) <= 0) |
| 2471 |
return 0; |
| 2472 |
if (EVP_PKEY_get_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md)) |
| 2473 |
return 0; |
| 2474 |
return 1; |
| 2475 |
} |
| 2476 |
|
| 2477 |
/* |
| 2478 |
* Returns a signature algorithm when the peer did not send a list of supported |
| 2479 |
* signature algorithms. The signature algorithm is fixed for the certificate |
| 2480 |
* type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the |
| 2481 |
* certificate type from |s| will be used. |
| 2482 |
* Returns the signature algorithm to use, or NULL on error. |
| 2483 |
*/ |
| 2484 |
static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL_CONNECTION *s, |
| 2485 |
int idx) |
| 2486 |
{ |
| 2487 |
if (idx == -1) { |
| 2488 |
if (s->server) { |
| 2489 |
size_t i; |
| 2490 |
|
| 2491 |
/* Work out index corresponding to ciphersuite */ |
| 2492 |
for (i = 0; i < s->ssl_pkey_num; i++) { |
| 2493 |
const SSL_CERT_LOOKUP *clu |
| 2494 |
= ssl_cert_lookup_by_idx(i, SSL_CONNECTION_GET_CTX(s)); |
| 2495 |
|
| 2496 |
if (clu == NULL) |
| 2497 |
continue; |
| 2498 |
if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) { |
| 2499 |
idx = (int)i; |
| 2500 |
break; |
| 2501 |
} |
| 2502 |
} |
| 2503 |
|
| 2504 |
/* |
| 2505 |
* Some GOST ciphersuites allow more than one signature algorithms |
| 2506 |
* */ |
| 2507 |
if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) { |
| 2508 |
int real_idx; |
| 2509 |
|
| 2510 |
for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01; |
| 2511 |
real_idx--) { |
| 2512 |
if (s->cert->pkeys[real_idx].privatekey != NULL) { |
| 2513 |
idx = real_idx; |
| 2514 |
break; |
| 2515 |
} |
| 2516 |
} |
| 2517 |
} |
| 2518 |
/* |
| 2519 |
* As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used |
| 2520 |
* with new (aGOST12-only) ciphersuites, we should find out which one is available really. |
| 2521 |
*/ |
| 2522 |
else if (idx == SSL_PKEY_GOST12_256) { |
| 2523 |
int real_idx; |
| 2524 |
|
| 2525 |
for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256; |
| 2526 |
real_idx--) { |
| 2527 |
if (s->cert->pkeys[real_idx].privatekey != NULL) { |
| 2528 |
idx = real_idx; |
| 2529 |
break; |
| 2530 |
} |
| 2531 |
} |
| 2532 |
} |
| 2533 |
} else { |
| 2534 |
idx = (int)(s->cert->key - s->cert->pkeys); |
| 2535 |
} |
| 2536 |
} |
| 2537 |
if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) |
| 2538 |
return NULL; |
| 2539 |
|
| 2540 |
if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { |
| 2541 |
const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
| 2542 |
tls_default_sigalg[idx]); |
| 2543 |
|
| 2544 |
if (lu == NULL) |
| 2545 |
return NULL; |
| 2546 |
if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, NULL)) |
| 2547 |
return NULL; |
| 2548 |
if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
| 2549 |
return NULL; |
| 2550 |
return lu; |
| 2551 |
} |
| 2552 |
if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg)) |
| 2553 |
return NULL; |
| 2554 |
return &legacy_rsa_sigalg; |
| 2555 |
} |
| 2556 |
/* Set peer sigalg based key type */ |
| 2557 |
int tls1_set_peer_legacy_sigalg(SSL_CONNECTION *s, const EVP_PKEY *pkey) |
| 2558 |
{ |
| 2559 |
size_t idx; |
| 2560 |
const SIGALG_LOOKUP *lu; |
| 2561 |
|
| 2562 |
if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) |
| 2563 |
return 0; |
| 2564 |
lu = tls1_get_legacy_sigalg(s, (int)idx); |
| 2565 |
if (lu == NULL) |
| 2566 |
return 0; |
| 2567 |
s->s3.tmp.peer_sigalg = lu; |
| 2568 |
return 1; |
| 2569 |
} |
| 2570 |
|
| 2571 |
size_t tls12_get_psigalgs(SSL_CONNECTION *s, int sent, const uint16_t **psigs) |
| 2572 |
{ |
| 2573 |
/* |
| 2574 |
* If Suite B mode use Suite B sigalgs only, ignore any other |
| 2575 |
* preferences. |
| 2576 |
*/ |
| 2577 |
switch (tls1_suiteb(s)) { |
| 2578 |
case SSL_CERT_FLAG_SUITEB_128_LOS: |
| 2579 |
*psigs = suiteb_sigalgs; |
| 2580 |
return OSSL_NELEM(suiteb_sigalgs); |
| 2581 |
|
| 2582 |
case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
| 2583 |
*psigs = suiteb_sigalgs; |
| 2584 |
return 1; |
| 2585 |
|
| 2586 |
case SSL_CERT_FLAG_SUITEB_192_LOS: |
| 2587 |
*psigs = suiteb_sigalgs + 1; |
| 2588 |
return 1; |
| 2589 |
} |
| 2590 |
/* |
| 2591 |
* We use client_sigalgs (if not NULL) if we're a server |
| 2592 |
* and sending a certificate request or if we're a client and |
| 2593 |
* determining which shared algorithm to use. |
| 2594 |
*/ |
| 2595 |
if ((s->server == sent) && s->cert->client_sigalgs != NULL) { |
| 2596 |
*psigs = s->cert->client_sigalgs; |
| 2597 |
return s->cert->client_sigalgslen; |
| 2598 |
} else if (s->cert->conf_sigalgs) { |
| 2599 |
*psigs = s->cert->conf_sigalgs; |
| 2600 |
return s->cert->conf_sigalgslen; |
| 2601 |
} else { |
| 2602 |
*psigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; |
| 2603 |
return SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; |
| 2604 |
} |
| 2605 |
} |
| 2606 |
|
| 2607 |
/* |
| 2608 |
* Called by servers only. Checks that we have a sig alg that supports the |
| 2609 |
* specified EC curve. |
| 2610 |
*/ |
| 2611 |
int tls_check_sigalg_curve(const SSL_CONNECTION *s, int curve) |
| 2612 |
{ |
| 2613 |
const uint16_t *sigs; |
| 2614 |
size_t siglen, i; |
| 2615 |
|
| 2616 |
if (s->cert->conf_sigalgs) { |
| 2617 |
sigs = s->cert->conf_sigalgs; |
| 2618 |
siglen = s->cert->conf_sigalgslen; |
| 2619 |
} else { |
| 2620 |
sigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; |
| 2621 |
siglen = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; |
| 2622 |
} |
| 2623 |
|
| 2624 |
for (i = 0; i < siglen; i++) { |
| 2625 |
const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sigs[i]); |
| 2626 |
|
| 2627 |
if (lu == NULL) |
| 2628 |
continue; |
| 2629 |
if (lu->sig == EVP_PKEY_EC |
| 2630 |
&& lu->curve != NID_undef |
| 2631 |
&& curve == lu->curve) |
| 2632 |
return 1; |
| 2633 |
} |
| 2634 |
|
| 2635 |
return 0; |
| 2636 |
} |
| 2637 |
|
| 2638 |
/* |
| 2639 |
* Return the number of security bits for the signature algorithm, or 0 on |
| 2640 |
* error. |
| 2641 |
*/ |
| 2642 |
static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu) |
| 2643 |
{ |
| 2644 |
const EVP_MD *md = NULL; |
| 2645 |
int secbits = 0; |
| 2646 |
|
| 2647 |
if (!tls1_lookup_md(ctx, lu, &md)) |
| 2648 |
return 0; |
| 2649 |
if (md != NULL) { |
| 2650 |
int md_type = EVP_MD_get_type(md); |
| 2651 |
|
| 2652 |
/* Security bits: half digest bits */ |
| 2653 |
secbits = EVP_MD_get_size(md) * 4; |
| 2654 |
if (secbits <= 0) |
| 2655 |
return 0; |
| 2656 |
/* |
| 2657 |
* SHA1 and MD5 are known to be broken. Reduce security bits so that |
| 2658 |
* they're no longer accepted at security level 1. The real values don't |
| 2659 |
* really matter as long as they're lower than 80, which is our |
| 2660 |
* security level 1. |
| 2661 |
* https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for |
| 2662 |
* SHA1 at 2^63.4 and MD5+SHA1 at 2^67.2 |
| 2663 |
* https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf |
| 2664 |
* puts a chosen-prefix attack for MD5 at 2^39. |
| 2665 |
*/ |
| 2666 |
if (md_type == NID_sha1) |
| 2667 |
secbits = 64; |
| 2668 |
else if (md_type == NID_md5_sha1) |
| 2669 |
secbits = 67; |
| 2670 |
else if (md_type == NID_md5) |
| 2671 |
secbits = 39; |
| 2672 |
} else { |
| 2673 |
/* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */ |
| 2674 |
if (lu->sigalg == TLSEXT_SIGALG_ed25519) |
| 2675 |
secbits = 128; |
| 2676 |
else if (lu->sigalg == TLSEXT_SIGALG_ed448) |
| 2677 |
secbits = 224; |
| 2678 |
} |
| 2679 |
/* |
| 2680 |
* For provider-based sigalgs we have secbits information available |
| 2681 |
* in the (provider-loaded) sigalg_list structure |
| 2682 |
*/ |
| 2683 |
if ((secbits == 0) && (lu->sig_idx >= SSL_PKEY_NUM) |
| 2684 |
&& ((lu->sig_idx - SSL_PKEY_NUM) < (int)ctx->sigalg_list_len)) { |
| 2685 |
secbits = ctx->sigalg_list[lu->sig_idx - SSL_PKEY_NUM].secbits; |
| 2686 |
} |
| 2687 |
return secbits; |
| 2688 |
} |
| 2689 |
|
| 2690 |
static int tls_sigalg_compat(SSL_CONNECTION *sc, const SIGALG_LOOKUP *lu) |
| 2691 |
{ |
| 2692 |
int minversion, maxversion; |
| 2693 |
int minproto, maxproto; |
| 2694 |
|
| 2695 |
if (!lu->available) |
| 2696 |
return 0; |
| 2697 |
|
| 2698 |
if (SSL_CONNECTION_IS_DTLS(sc)) { |
| 2699 |
if (sc->ssl.method->version == DTLS_ANY_VERSION) { |
| 2700 |
minproto = sc->min_proto_version; |
| 2701 |
maxproto = sc->max_proto_version; |
| 2702 |
} else { |
| 2703 |
maxproto = minproto = sc->version; |
| 2704 |
} |
| 2705 |
minversion = lu->mindtls; |
| 2706 |
maxversion = lu->maxdtls; |
| 2707 |
} else { |
| 2708 |
if (sc->ssl.method->version == TLS_ANY_VERSION) { |
| 2709 |
minproto = sc->min_proto_version; |
| 2710 |
maxproto = sc->max_proto_version; |
| 2711 |
} else { |
| 2712 |
maxproto = minproto = sc->version; |
| 2713 |
} |
| 2714 |
minversion = lu->mintls; |
| 2715 |
maxversion = lu->maxtls; |
| 2716 |
} |
| 2717 |
if (minversion == -1 || maxversion == -1 |
| 2718 |
|| (minversion != 0 && maxproto != 0 |
| 2719 |
&& ssl_version_cmp(sc, minversion, maxproto) > 0) |
| 2720 |
|| (maxversion != 0 && minproto != 0 |
| 2721 |
&& ssl_version_cmp(sc, maxversion, minproto) < 0) |
| 2722 |
|| !tls12_sigalg_allowed(sc, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
| 2723 |
return 0; |
| 2724 |
return 1; |
| 2725 |
} |
| 2726 |
|
| 2727 |
/* |
| 2728 |
* Check signature algorithm is consistent with sent supported signature |
| 2729 |
* algorithms and if so set relevant digest and signature scheme in |
| 2730 |
* s. |
| 2731 |
*/ |
| 2732 |
int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey) |
| 2733 |
{ |
| 2734 |
const uint16_t *sent_sigs; |
| 2735 |
const EVP_MD *md = NULL; |
| 2736 |
char sigalgstr[2]; |
| 2737 |
size_t sent_sigslen, i, cidx; |
| 2738 |
int pkeyid = -1; |
| 2739 |
const SIGALG_LOOKUP *lu; |
| 2740 |
int secbits = 0; |
| 2741 |
|
| 2742 |
pkeyid = EVP_PKEY_get_id(pkey); |
| 2743 |
|
| 2744 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 2745 |
/* Disallow DSA for TLS 1.3 */ |
| 2746 |
if (pkeyid == EVP_PKEY_DSA) { |
| 2747 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2748 |
return 0; |
| 2749 |
} |
| 2750 |
/* Only allow PSS for TLS 1.3 */ |
| 2751 |
if (pkeyid == EVP_PKEY_RSA) |
| 2752 |
pkeyid = EVP_PKEY_RSA_PSS; |
| 2753 |
} |
| 2754 |
|
| 2755 |
/* Is this code point available and compatible with the protocol */ |
| 2756 |
lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sig); |
| 2757 |
if (lu == NULL || !tls_sigalg_compat(s, lu)) { |
| 2758 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2759 |
return 0; |
| 2760 |
} |
| 2761 |
|
| 2762 |
/* If we don't know the pkey nid yet go and find it */ |
| 2763 |
if (pkeyid == EVP_PKEY_KEYMGMT) { |
| 2764 |
const SSL_CERT_LOOKUP *scl = ssl_cert_lookup_by_pkey(pkey, NULL, SSL_CONNECTION_GET_CTX(s)); |
| 2765 |
|
| 2766 |
if (scl == NULL) { |
| 2767 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2768 |
return 0; |
| 2769 |
} |
| 2770 |
pkeyid = scl->pkey_nid; |
| 2771 |
} |
| 2772 |
|
| 2773 |
/* Should never happen */ |
| 2774 |
if (pkeyid == -1) { |
| 2775 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2776 |
return -1; |
| 2777 |
} |
| 2778 |
|
| 2779 |
/* |
| 2780 |
* Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type |
| 2781 |
* is consistent with signature: RSA keys can be used for RSA-PSS |
| 2782 |
*/ |
| 2783 |
if ((SSL_CONNECTION_IS_TLS13(s) |
| 2784 |
&& (lu->hash == NID_sha1 || lu->hash == NID_sha224)) |
| 2785 |
|| (pkeyid != lu->sig |
| 2786 |
&& (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
| 2787 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2788 |
return 0; |
| 2789 |
} |
| 2790 |
/* Check the sigalg is consistent with the key OID */ |
| 2791 |
if (!ssl_cert_lookup_by_nid( |
| 2792 |
(pkeyid == EVP_PKEY_RSA_PSS) ? EVP_PKEY_get_id(pkey) : pkeyid, |
| 2793 |
&cidx, SSL_CONNECTION_GET_CTX(s)) |
| 2794 |
|| lu->sig_idx != (int)cidx) { |
| 2795 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2796 |
return 0; |
| 2797 |
} |
| 2798 |
|
| 2799 |
if (pkeyid == EVP_PKEY_EC) { |
| 2800 |
|
| 2801 |
/* Check point compression is permitted */ |
| 2802 |
if (!tls1_check_pkey_comp(s, pkey)) { |
| 2803 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 2804 |
SSL_R_ILLEGAL_POINT_COMPRESSION); |
| 2805 |
return 0; |
| 2806 |
} |
| 2807 |
|
| 2808 |
/* For TLS 1.3 or Suite B check curve matches signature algorithm */ |
| 2809 |
if (SSL_CONNECTION_IS_TLS13(s) || tls1_suiteb(s)) { |
| 2810 |
int curve = ssl_get_EC_curve_nid(pkey); |
| 2811 |
|
| 2812 |
if (lu->curve != NID_undef && curve != lu->curve) { |
| 2813 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
| 2814 |
return 0; |
| 2815 |
} |
| 2816 |
} |
| 2817 |
if (!SSL_CONNECTION_IS_TLS13(s)) { |
| 2818 |
/* Check curve matches extensions */ |
| 2819 |
if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) { |
| 2820 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
| 2821 |
return 0; |
| 2822 |
} |
| 2823 |
if (tls1_suiteb(s)) { |
| 2824 |
/* Check sigalg matches a permissible Suite B value */ |
| 2825 |
if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 |
| 2826 |
&& sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { |
| 2827 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 2828 |
SSL_R_WRONG_SIGNATURE_TYPE); |
| 2829 |
return 0; |
| 2830 |
} |
| 2831 |
} |
| 2832 |
} |
| 2833 |
} else if (tls1_suiteb(s)) { |
| 2834 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2835 |
return 0; |
| 2836 |
} |
| 2837 |
|
| 2838 |
/* Check signature matches a type we sent */ |
| 2839 |
sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
| 2840 |
for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
| 2841 |
if (sig == *sent_sigs) |
| 2842 |
break; |
| 2843 |
} |
| 2844 |
/* Allow fallback to SHA1 if not strict mode */ |
| 2845 |
if (i == sent_sigslen && (lu->hash != NID_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { |
| 2846 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2847 |
return 0; |
| 2848 |
} |
| 2849 |
if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, &md)) { |
| 2850 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_UNKNOWN_DIGEST); |
| 2851 |
return 0; |
| 2852 |
} |
| 2853 |
/* |
| 2854 |
* Make sure security callback allows algorithm. For historical |
| 2855 |
* reasons we have to pass the sigalg as a two byte char array. |
| 2856 |
*/ |
| 2857 |
sigalgstr[0] = (sig >> 8) & 0xff; |
| 2858 |
sigalgstr[1] = sig & 0xff; |
| 2859 |
secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
| 2860 |
if (secbits == 0 || !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits, md != NULL ? EVP_MD_get_type(md) : NID_undef, (void *)sigalgstr)) { |
| 2861 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2862 |
return 0; |
| 2863 |
} |
| 2864 |
/* Store the sigalg the peer uses */ |
| 2865 |
s->s3.tmp.peer_sigalg = lu; |
| 2866 |
return 1; |
| 2867 |
} |
| 2868 |
|
| 2869 |
int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
| 2870 |
{ |
| 2871 |
const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
| 2872 |
|
| 2873 |
if (sc == NULL) |
| 2874 |
return 0; |
| 2875 |
|
| 2876 |
if (sc->s3.tmp.peer_sigalg == NULL) |
| 2877 |
return 0; |
| 2878 |
*pnid = sc->s3.tmp.peer_sigalg->sig; |
| 2879 |
return 1; |
| 2880 |
} |
| 2881 |
|
| 2882 |
int SSL_get_signature_type_nid(const SSL *s, int *pnid) |
| 2883 |
{ |
| 2884 |
const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
| 2885 |
|
| 2886 |
if (sc == NULL) |
| 2887 |
return 0; |
| 2888 |
|
| 2889 |
if (sc->s3.tmp.sigalg == NULL) |
| 2890 |
return 0; |
| 2891 |
*pnid = sc->s3.tmp.sigalg->sig; |
| 2892 |
return 1; |
| 2893 |
} |
| 2894 |
|
| 2895 |
/* |
| 2896 |
* Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 2897 |
* supported, doesn't appear in supported signature algorithms, isn't supported |
| 2898 |
* by the enabled protocol versions or by the security level. |
| 2899 |
* |
| 2900 |
* This function should only be used for checking which ciphers are supported |
| 2901 |
* by the client. |
| 2902 |
* |
| 2903 |
* Call ssl_cipher_disabled() to check that it's enabled or not. |
| 2904 |
*/ |
| 2905 |
int ssl_set_client_disabled(SSL_CONNECTION *s) |
| 2906 |
{ |
| 2907 |
s->s3.tmp.mask_a = 0; |
| 2908 |
s->s3.tmp.mask_k = 0; |
| 2909 |
ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
| 2910 |
if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver, |
| 2911 |
&s->s3.tmp.max_ver, NULL) |
| 2912 |
!= 0) |
| 2913 |
return 0; |
| 2914 |
#ifndef OPENSSL_NO_PSK |
| 2915 |
/* with PSK there must be client callback set */ |
| 2916 |
if (!s->psk_client_callback) { |
| 2917 |
s->s3.tmp.mask_a |= SSL_aPSK; |
| 2918 |
s->s3.tmp.mask_k |= SSL_PSK; |
| 2919 |
} |
| 2920 |
#endif /* OPENSSL_NO_PSK */ |
| 2921 |
#ifndef OPENSSL_NO_SRP |
| 2922 |
if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
| 2923 |
s->s3.tmp.mask_a |= SSL_aSRP; |
| 2924 |
s->s3.tmp.mask_k |= SSL_kSRP; |
| 2925 |
} |
| 2926 |
#endif |
| 2927 |
return 1; |
| 2928 |
} |
| 2929 |
|
| 2930 |
/* |
| 2931 |
* ssl_cipher_disabled - check that a cipher is disabled or not |
| 2932 |
* @s: SSL connection that you want to use the cipher on |
| 2933 |
* @c: cipher to check |
| 2934 |
* @op: Security check that you want to do |
| 2935 |
* @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3 |
| 2936 |
* |
| 2937 |
* Returns 1 when it's disabled, 0 when enabled. |
| 2938 |
*/ |
| 2939 |
int ssl_cipher_disabled(const SSL_CONNECTION *s, const SSL_CIPHER *c, |
| 2940 |
int op, int ecdhe) |
| 2941 |
{ |
| 2942 |
int minversion = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls; |
| 2943 |
int maxversion = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls; |
| 2944 |
|
| 2945 |
if (c->algorithm_mkey & s->s3.tmp.mask_k |
| 2946 |
|| c->algorithm_auth & s->s3.tmp.mask_a) |
| 2947 |
return 1; |
| 2948 |
if (s->s3.tmp.max_ver == 0) |
| 2949 |
return 1; |
| 2950 |
|
| 2951 |
if (SSL_IS_QUIC_INT_HANDSHAKE(s)) |
| 2952 |
/* For QUIC, only allow these ciphersuites. */ |
| 2953 |
switch (SSL_CIPHER_get_id(c)) { |
| 2954 |
case TLS1_3_CK_AES_128_GCM_SHA256: |
| 2955 |
case TLS1_3_CK_AES_256_GCM_SHA384: |
| 2956 |
case TLS1_3_CK_CHACHA20_POLY1305_SHA256: |
| 2957 |
break; |
| 2958 |
default: |
| 2959 |
return 1; |
| 2960 |
} |
| 2961 |
|
| 2962 |
/* |
| 2963 |
* For historical reasons we will allow ECHDE to be selected by a server |
| 2964 |
* in SSLv3 if we are a client |
| 2965 |
*/ |
| 2966 |
if (minversion == TLS1_VERSION |
| 2967 |
&& ecdhe |
| 2968 |
&& (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0) |
| 2969 |
minversion = SSL3_VERSION; |
| 2970 |
|
| 2971 |
if (ssl_version_cmp(s, minversion, s->s3.tmp.max_ver) > 0 |
| 2972 |
|| ssl_version_cmp(s, maxversion, s->s3.tmp.min_ver) < 0) |
| 2973 |
return 1; |
| 2974 |
|
| 2975 |
return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
| 2976 |
} |
| 2977 |
|
| 2978 |
int tls_use_ticket(SSL_CONNECTION *s) |
| 2979 |
{ |
| 2980 |
if ((s->options & SSL_OP_NO_TICKET)) |
| 2981 |
return 0; |
| 2982 |
return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); |
| 2983 |
} |
| 2984 |
|
| 2985 |
int tls1_set_server_sigalgs(SSL_CONNECTION *s) |
| 2986 |
{ |
| 2987 |
size_t i; |
| 2988 |
|
| 2989 |
/* Clear any shared signature algorithms */ |
| 2990 |
OPENSSL_free(s->shared_sigalgs); |
| 2991 |
s->shared_sigalgs = NULL; |
| 2992 |
s->shared_sigalgslen = 0; |
| 2993 |
|
| 2994 |
/* Clear certificate validity flags */ |
| 2995 |
if (s->s3.tmp.valid_flags) |
| 2996 |
memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t)); |
| 2997 |
else |
| 2998 |
s->s3.tmp.valid_flags = OPENSSL_calloc(s->ssl_pkey_num, sizeof(uint32_t)); |
| 2999 |
if (s->s3.tmp.valid_flags == NULL) { |
| 3000 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3001 |
return 0; |
| 3002 |
} |
| 3003 |
/* |
| 3004 |
* If peer sent no signature algorithms check to see if we support |
| 3005 |
* the default algorithm for each certificate type |
| 3006 |
*/ |
| 3007 |
if (s->s3.tmp.peer_cert_sigalgs == NULL |
| 3008 |
&& s->s3.tmp.peer_sigalgs == NULL) { |
| 3009 |
const uint16_t *sent_sigs; |
| 3010 |
size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
| 3011 |
|
| 3012 |
for (i = 0; i < s->ssl_pkey_num; i++) { |
| 3013 |
const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, (int)i); |
| 3014 |
size_t j; |
| 3015 |
|
| 3016 |
if (lu == NULL) |
| 3017 |
continue; |
| 3018 |
/* Check default matches a type we sent */ |
| 3019 |
for (j = 0; j < sent_sigslen; j++) { |
| 3020 |
if (lu->sigalg == sent_sigs[j]) { |
| 3021 |
s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN; |
| 3022 |
break; |
| 3023 |
} |
| 3024 |
} |
| 3025 |
} |
| 3026 |
return 1; |
| 3027 |
} |
| 3028 |
|
| 3029 |
if (!tls1_process_sigalgs(s)) { |
| 3030 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3031 |
return 0; |
| 3032 |
} |
| 3033 |
if (s->shared_sigalgs != NULL) |
| 3034 |
return 1; |
| 3035 |
|
| 3036 |
/* Fatal error if no shared signature algorithms */ |
| 3037 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 3038 |
SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
| 3039 |
return 0; |
| 3040 |
} |
| 3041 |
|
| 3042 |
/*- |
| 3043 |
* Gets the ticket information supplied by the client if any. |
| 3044 |
* |
| 3045 |
* hello: The parsed ClientHello data |
| 3046 |
* ret: (output) on return, if a ticket was decrypted, then this is set to |
| 3047 |
* point to the resulting session. |
| 3048 |
*/ |
| 3049 |
SSL_TICKET_STATUS tls_get_ticket_from_client(SSL_CONNECTION *s, |
| 3050 |
CLIENTHELLO_MSG *hello, |
| 3051 |
SSL_SESSION **ret) |
| 3052 |
{ |
| 3053 |
size_t size; |
| 3054 |
RAW_EXTENSION *ticketext; |
| 3055 |
|
| 3056 |
*ret = NULL; |
| 3057 |
s->ext.ticket_expected = 0; |
| 3058 |
|
| 3059 |
/* |
| 3060 |
* If tickets disabled or not supported by the protocol version |
| 3061 |
* (e.g. TLSv1.3) behave as if no ticket present to permit stateful |
| 3062 |
* resumption. |
| 3063 |
*/ |
| 3064 |
if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
| 3065 |
return SSL_TICKET_NONE; |
| 3066 |
|
| 3067 |
ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
| 3068 |
if (!ticketext->present) |
| 3069 |
return SSL_TICKET_NONE; |
| 3070 |
|
| 3071 |
size = PACKET_remaining(&ticketext->data); |
| 3072 |
|
| 3073 |
return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, |
| 3074 |
hello->session_id, hello->session_id_len, ret); |
| 3075 |
} |
| 3076 |
|
| 3077 |
/*- |
| 3078 |
* tls_decrypt_ticket attempts to decrypt a session ticket. |
| 3079 |
* |
| 3080 |
* If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are |
| 3081 |
* expecting a pre-shared key ciphersuite, in which case we have no use for |
| 3082 |
* session tickets and one will never be decrypted, nor will |
| 3083 |
* s->ext.ticket_expected be set to 1. |
| 3084 |
* |
| 3085 |
* Side effects: |
| 3086 |
* Sets s->ext.ticket_expected to 1 if the server will have to issue |
| 3087 |
* a new session ticket to the client because the client indicated support |
| 3088 |
* (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
| 3089 |
* a session ticket or we couldn't use the one it gave us, or if |
| 3090 |
* s->ctx->ext.ticket_key_cb asked to renew the client's ticket. |
| 3091 |
* Otherwise, s->ext.ticket_expected is set to 0. |
| 3092 |
* |
| 3093 |
* etick: points to the body of the session ticket extension. |
| 3094 |
* eticklen: the length of the session tickets extension. |
| 3095 |
* sess_id: points at the session ID. |
| 3096 |
* sesslen: the length of the session ID. |
| 3097 |
* psess: (output) on return, if a ticket was decrypted, then this is set to |
| 3098 |
* point to the resulting session. |
| 3099 |
*/ |
| 3100 |
SSL_TICKET_STATUS tls_decrypt_ticket(SSL_CONNECTION *s, |
| 3101 |
const unsigned char *etick, |
| 3102 |
size_t eticklen, |
| 3103 |
const unsigned char *sess_id, |
| 3104 |
size_t sesslen, SSL_SESSION **psess) |
| 3105 |
{ |
| 3106 |
SSL_SESSION *sess = NULL; |
| 3107 |
unsigned char *sdec; |
| 3108 |
const unsigned char *p; |
| 3109 |
int slen, ivlen, renew_ticket = 0, declen; |
| 3110 |
SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3111 |
size_t mlen; |
| 3112 |
unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
| 3113 |
SSL_HMAC *hctx = NULL; |
| 3114 |
EVP_CIPHER_CTX *ctx = NULL; |
| 3115 |
SSL_CTX *tctx = s->session_ctx; |
| 3116 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 3117 |
|
| 3118 |
if (eticklen == 0) { |
| 3119 |
/* |
| 3120 |
* The client will accept a ticket but doesn't currently have |
| 3121 |
* one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3 |
| 3122 |
*/ |
| 3123 |
ret = SSL_TICKET_EMPTY; |
| 3124 |
goto end; |
| 3125 |
} |
| 3126 |
if (!SSL_CONNECTION_IS_TLS13(s) && s->ext.session_secret_cb) { |
| 3127 |
/* |
| 3128 |
* Indicate that the ticket couldn't be decrypted rather than |
| 3129 |
* generating the session from ticket now, trigger |
| 3130 |
* abbreviated handshake based on external mechanism to |
| 3131 |
* calculate the master secret later. |
| 3132 |
*/ |
| 3133 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3134 |
goto end; |
| 3135 |
} |
| 3136 |
|
| 3137 |
/* Need at least keyname + iv */ |
| 3138 |
if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) { |
| 3139 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3140 |
goto end; |
| 3141 |
} |
| 3142 |
|
| 3143 |
/* Initialize session ticket encryption and HMAC contexts */ |
| 3144 |
hctx = ssl_hmac_new(tctx); |
| 3145 |
if (hctx == NULL) { |
| 3146 |
ret = SSL_TICKET_FATAL_ERR_MALLOC; |
| 3147 |
goto end; |
| 3148 |
} |
| 3149 |
ctx = EVP_CIPHER_CTX_new(); |
| 3150 |
if (ctx == NULL) { |
| 3151 |
ret = SSL_TICKET_FATAL_ERR_MALLOC; |
| 3152 |
goto end; |
| 3153 |
} |
| 3154 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 3155 |
if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) |
| 3156 |
#else |
| 3157 |
if (tctx->ext.ticket_key_evp_cb != NULL) |
| 3158 |
#endif |
| 3159 |
{ |
| 3160 |
unsigned char *nctick = (unsigned char *)etick; |
| 3161 |
int rv = 0; |
| 3162 |
|
| 3163 |
if (tctx->ext.ticket_key_evp_cb != NULL) |
| 3164 |
rv = tctx->ext.ticket_key_evp_cb(SSL_CONNECTION_GET_USER_SSL(s), |
| 3165 |
nctick, |
| 3166 |
nctick + TLSEXT_KEYNAME_LENGTH, |
| 3167 |
ctx, |
| 3168 |
ssl_hmac_get0_EVP_MAC_CTX(hctx), |
| 3169 |
0); |
| 3170 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 3171 |
else if (tctx->ext.ticket_key_cb != NULL) |
| 3172 |
/* if 0 is returned, write an empty ticket */ |
| 3173 |
rv = tctx->ext.ticket_key_cb(SSL_CONNECTION_GET_USER_SSL(s), nctick, |
| 3174 |
nctick + TLSEXT_KEYNAME_LENGTH, |
| 3175 |
ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0); |
| 3176 |
#endif |
| 3177 |
if (rv < 0) { |
| 3178 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3179 |
goto end; |
| 3180 |
} |
| 3181 |
if (rv == 0) { |
| 3182 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3183 |
goto end; |
| 3184 |
} |
| 3185 |
if (rv == 2) |
| 3186 |
renew_ticket = 1; |
| 3187 |
} else { |
| 3188 |
EVP_CIPHER *aes256cbc = NULL; |
| 3189 |
|
| 3190 |
/* Check key name matches */ |
| 3191 |
if (memcmp(etick, tctx->ext.tick_key_name, |
| 3192 |
TLSEXT_KEYNAME_LENGTH) |
| 3193 |
!= 0) { |
| 3194 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3195 |
goto end; |
| 3196 |
} |
| 3197 |
|
| 3198 |
aes256cbc = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC", |
| 3199 |
sctx->propq); |
| 3200 |
if (aes256cbc == NULL |
| 3201 |
|| ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, |
| 3202 |
sizeof(tctx->ext.secure->tick_hmac_key), |
| 3203 |
"SHA256") |
| 3204 |
<= 0 |
| 3205 |
|| EVP_DecryptInit_ex(ctx, aes256cbc, NULL, |
| 3206 |
tctx->ext.secure->tick_aes_key, |
| 3207 |
etick + TLSEXT_KEYNAME_LENGTH) |
| 3208 |
<= 0) { |
| 3209 |
EVP_CIPHER_free(aes256cbc); |
| 3210 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3211 |
goto end; |
| 3212 |
} |
| 3213 |
EVP_CIPHER_free(aes256cbc); |
| 3214 |
if (SSL_CONNECTION_IS_TLS13(s)) |
| 3215 |
renew_ticket = 1; |
| 3216 |
} |
| 3217 |
/* |
| 3218 |
* Attempt to process session ticket, first conduct sanity and integrity |
| 3219 |
* checks on ticket. |
| 3220 |
*/ |
| 3221 |
mlen = ssl_hmac_size(hctx); |
| 3222 |
if (mlen == 0) { |
| 3223 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3224 |
goto end; |
| 3225 |
} |
| 3226 |
|
| 3227 |
ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); |
| 3228 |
if (ivlen < 0) { |
| 3229 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3230 |
goto end; |
| 3231 |
} |
| 3232 |
|
| 3233 |
/* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
| 3234 |
if (eticklen <= TLSEXT_KEYNAME_LENGTH + ivlen + mlen) { |
| 3235 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3236 |
goto end; |
| 3237 |
} |
| 3238 |
eticklen -= mlen; |
| 3239 |
/* Check HMAC of encrypted ticket */ |
| 3240 |
if (ssl_hmac_update(hctx, etick, eticklen) <= 0 |
| 3241 |
|| ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) { |
| 3242 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3243 |
goto end; |
| 3244 |
} |
| 3245 |
|
| 3246 |
if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
| 3247 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3248 |
goto end; |
| 3249 |
} |
| 3250 |
/* Attempt to decrypt session data */ |
| 3251 |
/* Move p after IV to start of encrypted ticket, update length */ |
| 3252 |
p = etick + TLSEXT_KEYNAME_LENGTH + ivlen; |
| 3253 |
eticklen -= TLSEXT_KEYNAME_LENGTH + ivlen; |
| 3254 |
sdec = OPENSSL_malloc(eticklen); |
| 3255 |
if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, (int)eticklen) <= 0) { |
| 3256 |
OPENSSL_free(sdec); |
| 3257 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3258 |
goto end; |
| 3259 |
} |
| 3260 |
if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
| 3261 |
OPENSSL_free(sdec); |
| 3262 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3263 |
goto end; |
| 3264 |
} |
| 3265 |
slen += declen; |
| 3266 |
p = sdec; |
| 3267 |
|
| 3268 |
sess = d2i_SSL_SESSION_ex(NULL, &p, slen, sctx->libctx, sctx->propq); |
| 3269 |
slen -= (int)(p - sdec); |
| 3270 |
OPENSSL_free(sdec); |
| 3271 |
if (sess) { |
| 3272 |
/* Some additional consistency checks */ |
| 3273 |
if (slen != 0) { |
| 3274 |
SSL_SESSION_free(sess); |
| 3275 |
sess = NULL; |
| 3276 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3277 |
goto end; |
| 3278 |
} |
| 3279 |
/* |
| 3280 |
* The session ID, if non-empty, is used by some clients to detect |
| 3281 |
* that the ticket has been accepted. So we copy it to the session |
| 3282 |
* structure. If it is empty set length to zero as required by |
| 3283 |
* standard. |
| 3284 |
*/ |
| 3285 |
if (sesslen) { |
| 3286 |
memcpy(sess->session_id, sess_id, sesslen); |
| 3287 |
sess->session_id_length = sesslen; |
| 3288 |
} |
| 3289 |
if (renew_ticket) |
| 3290 |
ret = SSL_TICKET_SUCCESS_RENEW; |
| 3291 |
else |
| 3292 |
ret = SSL_TICKET_SUCCESS; |
| 3293 |
goto end; |
| 3294 |
} |
| 3295 |
ERR_clear_error(); |
| 3296 |
/* |
| 3297 |
* For session parse failure, indicate that we need to send a new ticket. |
| 3298 |
*/ |
| 3299 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3300 |
|
| 3301 |
end: |
| 3302 |
EVP_CIPHER_CTX_free(ctx); |
| 3303 |
ssl_hmac_free(hctx); |
| 3304 |
|
| 3305 |
/* |
| 3306 |
* If set, the decrypt_ticket_cb() is called unless a fatal error was |
| 3307 |
* detected above. The callback is responsible for checking |ret| before it |
| 3308 |
* performs any action |
| 3309 |
*/ |
| 3310 |
if (s->session_ctx->decrypt_ticket_cb != NULL |
| 3311 |
&& (ret == SSL_TICKET_EMPTY |
| 3312 |
|| ret == SSL_TICKET_NO_DECRYPT |
| 3313 |
|| ret == SSL_TICKET_SUCCESS |
| 3314 |
|| ret == SSL_TICKET_SUCCESS_RENEW)) { |
| 3315 |
size_t keyname_len = eticklen; |
| 3316 |
int retcb; |
| 3317 |
|
| 3318 |
if (keyname_len > TLSEXT_KEYNAME_LENGTH) |
| 3319 |
keyname_len = TLSEXT_KEYNAME_LENGTH; |
| 3320 |
retcb = s->session_ctx->decrypt_ticket_cb(SSL_CONNECTION_GET_SSL(s), |
| 3321 |
sess, etick, keyname_len, |
| 3322 |
ret, |
| 3323 |
s->session_ctx->ticket_cb_data); |
| 3324 |
switch (retcb) { |
| 3325 |
case SSL_TICKET_RETURN_ABORT: |
| 3326 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3327 |
break; |
| 3328 |
|
| 3329 |
case SSL_TICKET_RETURN_IGNORE: |
| 3330 |
ret = SSL_TICKET_NONE; |
| 3331 |
SSL_SESSION_free(sess); |
| 3332 |
sess = NULL; |
| 3333 |
break; |
| 3334 |
|
| 3335 |
case SSL_TICKET_RETURN_IGNORE_RENEW: |
| 3336 |
if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT) |
| 3337 |
ret = SSL_TICKET_NO_DECRYPT; |
| 3338 |
/* else the value of |ret| will already do the right thing */ |
| 3339 |
SSL_SESSION_free(sess); |
| 3340 |
sess = NULL; |
| 3341 |
break; |
| 3342 |
|
| 3343 |
case SSL_TICKET_RETURN_USE: |
| 3344 |
case SSL_TICKET_RETURN_USE_RENEW: |
| 3345 |
if (ret != SSL_TICKET_SUCCESS |
| 3346 |
&& ret != SSL_TICKET_SUCCESS_RENEW) |
| 3347 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3348 |
else if (retcb == SSL_TICKET_RETURN_USE) |
| 3349 |
ret = SSL_TICKET_SUCCESS; |
| 3350 |
else |
| 3351 |
ret = SSL_TICKET_SUCCESS_RENEW; |
| 3352 |
break; |
| 3353 |
|
| 3354 |
default: |
| 3355 |
ret = SSL_TICKET_FATAL_ERR_OTHER; |
| 3356 |
} |
| 3357 |
} |
| 3358 |
|
| 3359 |
if (s->ext.session_secret_cb == NULL || SSL_CONNECTION_IS_TLS13(s)) { |
| 3360 |
switch (ret) { |
| 3361 |
case SSL_TICKET_NO_DECRYPT: |
| 3362 |
case SSL_TICKET_SUCCESS_RENEW: |
| 3363 |
case SSL_TICKET_EMPTY: |
| 3364 |
s->ext.ticket_expected = 1; |
| 3365 |
} |
| 3366 |
} |
| 3367 |
|
| 3368 |
*psess = sess; |
| 3369 |
|
| 3370 |
return ret; |
| 3371 |
} |
| 3372 |
|
| 3373 |
/* Check to see if a signature algorithm is allowed */ |
| 3374 |
static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, |
| 3375 |
const SIGALG_LOOKUP *lu) |
| 3376 |
{ |
| 3377 |
unsigned char sigalgstr[2]; |
| 3378 |
int secbits; |
| 3379 |
|
| 3380 |
if (lu == NULL || !lu->available) |
| 3381 |
return 0; |
| 3382 |
/* DSA is not allowed in TLS 1.3 */ |
| 3383 |
if (SSL_CONNECTION_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) |
| 3384 |
return 0; |
| 3385 |
/* |
| 3386 |
* At some point we should fully axe DSA/etc. in ClientHello as per TLS 1.3 |
| 3387 |
* spec |
| 3388 |
*/ |
| 3389 |
if (!s->server && !SSL_CONNECTION_IS_DTLS(s) |
| 3390 |
&& s->s3.tmp.min_ver >= TLS1_3_VERSION |
| 3391 |
&& (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX |
| 3392 |
|| lu->hash_idx == SSL_MD_MD5_IDX |
| 3393 |
|| lu->hash_idx == SSL_MD_SHA224_IDX)) |
| 3394 |
return 0; |
| 3395 |
|
| 3396 |
/* See if public key algorithm allowed */ |
| 3397 |
if (ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), lu->sig_idx)) |
| 3398 |
return 0; |
| 3399 |
|
| 3400 |
if (lu->sig == NID_id_GostR3410_2012_256 |
| 3401 |
|| lu->sig == NID_id_GostR3410_2012_512 |
| 3402 |
|| lu->sig == NID_id_GostR3410_2001) { |
| 3403 |
/* We never allow GOST sig algs on the server with TLSv1.3 */ |
| 3404 |
if (s->server && SSL_CONNECTION_IS_TLS13(s)) |
| 3405 |
return 0; |
| 3406 |
if (!s->server |
| 3407 |
&& SSL_CONNECTION_GET_SSL(s)->method->version == TLS_ANY_VERSION |
| 3408 |
&& s->s3.tmp.max_ver >= TLS1_3_VERSION) { |
| 3409 |
int i, num; |
| 3410 |
STACK_OF(SSL_CIPHER) *sk; |
| 3411 |
|
| 3412 |
/* |
| 3413 |
* We're a client that could negotiate TLSv1.3. We only allow GOST |
| 3414 |
* sig algs if we could negotiate TLSv1.2 or below and we have GOST |
| 3415 |
* ciphersuites enabled. |
| 3416 |
*/ |
| 3417 |
|
| 3418 |
if (s->s3.tmp.min_ver >= TLS1_3_VERSION) |
| 3419 |
return 0; |
| 3420 |
|
| 3421 |
sk = SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)); |
| 3422 |
num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0; |
| 3423 |
for (i = 0; i < num; i++) { |
| 3424 |
const SSL_CIPHER *c; |
| 3425 |
|
| 3426 |
c = sk_SSL_CIPHER_value(sk, i); |
| 3427 |
/* Skip disabled ciphers */ |
| 3428 |
if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) |
| 3429 |
continue; |
| 3430 |
|
| 3431 |
if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0) |
| 3432 |
break; |
| 3433 |
} |
| 3434 |
if (i == num) |
| 3435 |
return 0; |
| 3436 |
} |
| 3437 |
} |
| 3438 |
|
| 3439 |
/* Finally see if security callback allows it */ |
| 3440 |
secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
| 3441 |
sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
| 3442 |
sigalgstr[1] = lu->sigalg & 0xff; |
| 3443 |
return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
| 3444 |
} |
| 3445 |
|
| 3446 |
/* |
| 3447 |
* Get a mask of disabled public key algorithms based on supported signature |
| 3448 |
* algorithms. For example if no signature algorithm supports RSA then RSA is |
| 3449 |
* disabled. |
| 3450 |
*/ |
| 3451 |
|
| 3452 |
void ssl_set_sig_mask(uint32_t *pmask_a, SSL_CONNECTION *s, int op) |
| 3453 |
{ |
| 3454 |
const uint16_t *sigalgs; |
| 3455 |
size_t i, sigalgslen; |
| 3456 |
uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA; |
| 3457 |
/* |
| 3458 |
* Go through all signature algorithms seeing if we support any |
| 3459 |
* in disabled_mask. |
| 3460 |
*/ |
| 3461 |
sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
| 3462 |
for (i = 0; i < sigalgslen; i++, sigalgs++) { |
| 3463 |
const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *sigalgs); |
| 3464 |
const SSL_CERT_LOOKUP *clu; |
| 3465 |
|
| 3466 |
if (lu == NULL) |
| 3467 |
continue; |
| 3468 |
|
| 3469 |
clu = ssl_cert_lookup_by_idx(lu->sig_idx, |
| 3470 |
SSL_CONNECTION_GET_CTX(s)); |
| 3471 |
if (clu == NULL) |
| 3472 |
continue; |
| 3473 |
|
| 3474 |
/* If algorithm is disabled see if we can enable it */ |
| 3475 |
if ((clu->amask & disabled_mask) != 0 |
| 3476 |
&& tls12_sigalg_allowed(s, op, lu)) |
| 3477 |
disabled_mask &= ~clu->amask; |
| 3478 |
} |
| 3479 |
*pmask_a |= disabled_mask; |
| 3480 |
} |
| 3481 |
|
| 3482 |
int tls12_copy_sigalgs(SSL_CONNECTION *s, WPACKET *pkt, |
| 3483 |
const uint16_t *psig, size_t psiglen) |
| 3484 |
{ |
| 3485 |
size_t i; |
| 3486 |
int rv = 0; |
| 3487 |
|
| 3488 |
for (i = 0; i < psiglen; i++, psig++) { |
| 3489 |
const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *psig); |
| 3490 |
|
| 3491 |
if (lu == NULL || !tls_sigalg_compat(s, lu)) |
| 3492 |
continue; |
| 3493 |
if (!WPACKET_put_bytes_u16(pkt, *psig)) |
| 3494 |
return 0; |
| 3495 |
/* |
| 3496 |
* If TLS 1.3 must have at least one valid TLS 1.3 message |
| 3497 |
* signing algorithm: i.e. neither RSA nor SHA1/SHA224 |
| 3498 |
*/ |
| 3499 |
if (rv == 0 && (!SSL_CONNECTION_IS_TLS13(s) || (lu->sig != EVP_PKEY_RSA && lu->hash != NID_sha1 && lu->hash != NID_sha224))) |
| 3500 |
rv = 1; |
| 3501 |
} |
| 3502 |
if (rv == 0) |
| 3503 |
ERR_raise(ERR_LIB_SSL, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 3504 |
return rv; |
| 3505 |
} |
| 3506 |
|
| 3507 |
/* Given preference and allowed sigalgs set shared sigalgs */ |
| 3508 |
static size_t tls12_shared_sigalgs(SSL_CONNECTION *s, |
| 3509 |
const SIGALG_LOOKUP **shsig, |
| 3510 |
const uint16_t *pref, size_t preflen, |
| 3511 |
const uint16_t *allow, size_t allowlen) |
| 3512 |
{ |
| 3513 |
const uint16_t *ptmp, *atmp; |
| 3514 |
size_t i, j, nmatch = 0; |
| 3515 |
for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
| 3516 |
const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *ptmp); |
| 3517 |
|
| 3518 |
/* Skip disabled hashes or signature algorithms */ |
| 3519 |
if (lu == NULL |
| 3520 |
|| !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) |
| 3521 |
continue; |
| 3522 |
for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
| 3523 |
if (*ptmp == *atmp) { |
| 3524 |
nmatch++; |
| 3525 |
if (shsig) |
| 3526 |
*shsig++ = lu; |
| 3527 |
break; |
| 3528 |
} |
| 3529 |
} |
| 3530 |
} |
| 3531 |
return nmatch; |
| 3532 |
} |
| 3533 |
|
| 3534 |
/* Set shared signature algorithms for SSL structures */ |
| 3535 |
static int tls1_set_shared_sigalgs(SSL_CONNECTION *s) |
| 3536 |
{ |
| 3537 |
const uint16_t *pref, *allow, *conf; |
| 3538 |
size_t preflen, allowlen, conflen; |
| 3539 |
size_t nmatch; |
| 3540 |
const SIGALG_LOOKUP **salgs = NULL; |
| 3541 |
CERT *c = s->cert; |
| 3542 |
unsigned int is_suiteb = tls1_suiteb(s); |
| 3543 |
|
| 3544 |
OPENSSL_free(s->shared_sigalgs); |
| 3545 |
s->shared_sigalgs = NULL; |
| 3546 |
s->shared_sigalgslen = 0; |
| 3547 |
/* If client use client signature algorithms if not NULL */ |
| 3548 |
if (!s->server && c->client_sigalgs && !is_suiteb) { |
| 3549 |
conf = c->client_sigalgs; |
| 3550 |
conflen = c->client_sigalgslen; |
| 3551 |
} else if (c->conf_sigalgs && !is_suiteb) { |
| 3552 |
conf = c->conf_sigalgs; |
| 3553 |
conflen = c->conf_sigalgslen; |
| 3554 |
} else |
| 3555 |
conflen = tls12_get_psigalgs(s, 0, &conf); |
| 3556 |
if (s->options & SSL_OP_SERVER_PREFERENCE || is_suiteb) { |
| 3557 |
pref = conf; |
| 3558 |
preflen = conflen; |
| 3559 |
allow = s->s3.tmp.peer_sigalgs; |
| 3560 |
allowlen = s->s3.tmp.peer_sigalgslen; |
| 3561 |
} else { |
| 3562 |
allow = conf; |
| 3563 |
allowlen = conflen; |
| 3564 |
pref = s->s3.tmp.peer_sigalgs; |
| 3565 |
preflen = s->s3.tmp.peer_sigalgslen; |
| 3566 |
} |
| 3567 |
nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); |
| 3568 |
if (nmatch) { |
| 3569 |
if ((salgs = OPENSSL_malloc_array(nmatch, sizeof(*salgs))) == NULL) |
| 3570 |
return 0; |
| 3571 |
nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); |
| 3572 |
} else { |
| 3573 |
salgs = NULL; |
| 3574 |
} |
| 3575 |
s->shared_sigalgs = salgs; |
| 3576 |
s->shared_sigalgslen = nmatch; |
| 3577 |
return 1; |
| 3578 |
} |
| 3579 |
|
| 3580 |
int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen) |
| 3581 |
{ |
| 3582 |
unsigned int stmp; |
| 3583 |
size_t size, i; |
| 3584 |
uint16_t *buf; |
| 3585 |
|
| 3586 |
size = PACKET_remaining(pkt); |
| 3587 |
|
| 3588 |
/* Invalid data length */ |
| 3589 |
if (size == 0 || (size & 1) != 0) |
| 3590 |
return 0; |
| 3591 |
|
| 3592 |
size >>= 1; |
| 3593 |
|
| 3594 |
if ((buf = OPENSSL_malloc_array(size, sizeof(*buf))) == NULL) |
| 3595 |
return 0; |
| 3596 |
for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
| 3597 |
buf[i] = stmp; |
| 3598 |
|
| 3599 |
if (i != size) { |
| 3600 |
OPENSSL_free(buf); |
| 3601 |
return 0; |
| 3602 |
} |
| 3603 |
|
| 3604 |
OPENSSL_free(*pdest); |
| 3605 |
*pdest = buf; |
| 3606 |
*pdestlen = size; |
| 3607 |
|
| 3608 |
return 1; |
| 3609 |
} |
| 3610 |
|
| 3611 |
int tls1_save_sigalgs(SSL_CONNECTION *s, PACKET *pkt, int cert) |
| 3612 |
{ |
| 3613 |
/* Extension ignored for inappropriate versions */ |
| 3614 |
if (!SSL_USE_SIGALGS(s)) |
| 3615 |
return 1; |
| 3616 |
/* Should never happen */ |
| 3617 |
if (s->cert == NULL) |
| 3618 |
return 0; |
| 3619 |
|
| 3620 |
if (cert) |
| 3621 |
return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs, |
| 3622 |
&s->s3.tmp.peer_cert_sigalgslen); |
| 3623 |
else |
| 3624 |
return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs, |
| 3625 |
&s->s3.tmp.peer_sigalgslen); |
| 3626 |
} |
| 3627 |
|
| 3628 |
/* Set preferred digest for each key type */ |
| 3629 |
|
| 3630 |
int tls1_process_sigalgs(SSL_CONNECTION *s) |
| 3631 |
{ |
| 3632 |
size_t i; |
| 3633 |
uint32_t *pvalid = s->s3.tmp.valid_flags; |
| 3634 |
|
| 3635 |
if (!tls1_set_shared_sigalgs(s)) |
| 3636 |
return 0; |
| 3637 |
|
| 3638 |
for (i = 0; i < s->ssl_pkey_num; i++) |
| 3639 |
pvalid[i] = 0; |
| 3640 |
|
| 3641 |
for (i = 0; i < s->shared_sigalgslen; i++) { |
| 3642 |
const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i]; |
| 3643 |
int idx = sigptr->sig_idx; |
| 3644 |
|
| 3645 |
/* Ignore PKCS1 based sig algs in TLSv1.3 */ |
| 3646 |
if (SSL_CONNECTION_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
| 3647 |
continue; |
| 3648 |
/* If not disabled indicate we can explicitly sign */ |
| 3649 |
if (pvalid[idx] == 0 |
| 3650 |
&& !ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), idx)) |
| 3651 |
pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
| 3652 |
} |
| 3653 |
return 1; |
| 3654 |
} |
| 3655 |
|
| 3656 |
int SSL_get_sigalgs(SSL *s, int idx, |
| 3657 |
int *psign, int *phash, int *psignhash, |
| 3658 |
unsigned char *rsig, unsigned char *rhash) |
| 3659 |
{ |
| 3660 |
uint16_t *psig; |
| 3661 |
size_t numsigalgs; |
| 3662 |
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
| 3663 |
|
| 3664 |
if (sc == NULL) |
| 3665 |
return 0; |
| 3666 |
|
| 3667 |
psig = sc->s3.tmp.peer_sigalgs; |
| 3668 |
numsigalgs = sc->s3.tmp.peer_sigalgslen; |
| 3669 |
|
| 3670 |
if (psig == NULL || numsigalgs > INT_MAX) |
| 3671 |
return 0; |
| 3672 |
if (idx >= 0) { |
| 3673 |
const SIGALG_LOOKUP *lu; |
| 3674 |
|
| 3675 |
if (idx >= (int)numsigalgs) |
| 3676 |
return 0; |
| 3677 |
psig += idx; |
| 3678 |
if (rhash != NULL) |
| 3679 |
*rhash = (unsigned char)((*psig >> 8) & 0xff); |
| 3680 |
if (rsig != NULL) |
| 3681 |
*rsig = (unsigned char)(*psig & 0xff); |
| 3682 |
lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(sc), *psig); |
| 3683 |
if (psign != NULL) |
| 3684 |
*psign = lu != NULL ? lu->sig : NID_undef; |
| 3685 |
if (phash != NULL) |
| 3686 |
*phash = lu != NULL ? lu->hash : NID_undef; |
| 3687 |
if (psignhash != NULL) |
| 3688 |
*psignhash = lu != NULL ? lu->sigandhash : NID_undef; |
| 3689 |
} |
| 3690 |
return (int)numsigalgs; |
| 3691 |
} |
| 3692 |
|
| 3693 |
int SSL_get_shared_sigalgs(SSL *s, int idx, |
| 3694 |
int *psign, int *phash, int *psignhash, |
| 3695 |
unsigned char *rsig, unsigned char *rhash) |
| 3696 |
{ |
| 3697 |
const SIGALG_LOOKUP *shsigalgs; |
| 3698 |
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
| 3699 |
|
| 3700 |
if (sc == NULL) |
| 3701 |
return 0; |
| 3702 |
|
| 3703 |
if (sc->shared_sigalgs == NULL |
| 3704 |
|| idx < 0 |
| 3705 |
|| idx >= (int)sc->shared_sigalgslen |
| 3706 |
|| sc->shared_sigalgslen > INT_MAX) |
| 3707 |
return 0; |
| 3708 |
shsigalgs = sc->shared_sigalgs[idx]; |
| 3709 |
if (phash != NULL) |
| 3710 |
*phash = shsigalgs->hash; |
| 3711 |
if (psign != NULL) |
| 3712 |
*psign = shsigalgs->sig; |
| 3713 |
if (psignhash != NULL) |
| 3714 |
*psignhash = shsigalgs->sigandhash; |
| 3715 |
if (rsig != NULL) |
| 3716 |
*rsig = (unsigned char)(shsigalgs->sigalg & 0xff); |
| 3717 |
if (rhash != NULL) |
| 3718 |
*rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); |
| 3719 |
return (int)sc->shared_sigalgslen; |
| 3720 |
} |
| 3721 |
|
| 3722 |
/* Maximum possible number of unique entries in sigalgs array */ |
| 3723 |
#define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) |
| 3724 |
|
| 3725 |
typedef struct { |
| 3726 |
size_t sigalgcnt; |
| 3727 |
/* TLSEXT_SIGALG_XXX values */ |
| 3728 |
uint16_t sigalgs[TLS_MAX_SIGALGCNT]; |
| 3729 |
SSL_CTX *ctx; |
| 3730 |
} sig_cb_st; |
| 3731 |
|
| 3732 |
static void get_sigorhash(int *psig, int *phash, const char *str) |
| 3733 |
{ |
| 3734 |
if (OPENSSL_strcasecmp(str, "RSA") == 0) { |
| 3735 |
*psig = EVP_PKEY_RSA; |
| 3736 |
} else if (OPENSSL_strcasecmp(str, "RSA-PSS") == 0 |
| 3737 |
|| OPENSSL_strcasecmp(str, "PSS") == 0) { |
| 3738 |
*psig = EVP_PKEY_RSA_PSS; |
| 3739 |
} else if (OPENSSL_strcasecmp(str, "DSA") == 0) { |
| 3740 |
*psig = EVP_PKEY_DSA; |
| 3741 |
} else if (OPENSSL_strcasecmp(str, "ECDSA") == 0) { |
| 3742 |
*psig = EVP_PKEY_EC; |
| 3743 |
} else { |
| 3744 |
*phash = OBJ_sn2nid(str); |
| 3745 |
if (*phash == NID_undef) |
| 3746 |
*phash = OBJ_ln2nid(str); |
| 3747 |
} |
| 3748 |
} |
| 3749 |
/* Maximum length of a signature algorithm string component */ |
| 3750 |
#define TLS_MAX_SIGSTRING_LEN 40 |
| 3751 |
|
| 3752 |
static int sig_cb(const char *elem, int len, void *arg) |
| 3753 |
{ |
| 3754 |
sig_cb_st *sarg = arg; |
| 3755 |
size_t i = 0; |
| 3756 |
const SIGALG_LOOKUP *s; |
| 3757 |
char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
| 3758 |
const char *iana, *alias; |
| 3759 |
int sig_alg = NID_undef, hash_alg = NID_undef; |
| 3760 |
int ignore_unknown = 0; |
| 3761 |
|
| 3762 |
if (elem == NULL) |
| 3763 |
return 0; |
| 3764 |
if (elem[0] == '?') { |
| 3765 |
ignore_unknown = 1; |
| 3766 |
++elem; |
| 3767 |
--len; |
| 3768 |
} |
| 3769 |
if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
| 3770 |
return 0; |
| 3771 |
if (len > (int)(sizeof(etmp) - 1)) |
| 3772 |
return 0; |
| 3773 |
memcpy(etmp, elem, len); |
| 3774 |
etmp[len] = 0; |
| 3775 |
p = strchr(etmp, '+'); |
| 3776 |
/* |
| 3777 |
* We only allow SignatureSchemes listed in the sigalg_lookup_tbl; |
| 3778 |
* if there's no '+' in the provided name, look for the new-style combined |
| 3779 |
* name. If not, match both sig+hash to find the needed SIGALG_LOOKUP. |
| 3780 |
* Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and |
| 3781 |
* rsa_pss_rsae_* that differ only by public key OID; in such cases |
| 3782 |
* we will pick the _rsae_ variant, by virtue of them appearing earlier |
| 3783 |
* in the table. |
| 3784 |
*/ |
| 3785 |
if (p == NULL) { |
| 3786 |
if (sarg->ctx != NULL) { |
| 3787 |
for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { |
| 3788 |
iana = sarg->ctx->sigalg_lookup_cache[i].name; |
| 3789 |
alias = sarg->ctx->sigalg_lookup_cache[i].name12; |
| 3790 |
if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0) |
| 3791 |
|| OPENSSL_strcasecmp(etmp, iana) == 0) { |
| 3792 |
/* Ignore known, but unavailable sigalgs. */ |
| 3793 |
if (!sarg->ctx->sigalg_lookup_cache[i].available) |
| 3794 |
return 1; |
| 3795 |
sarg->sigalgs[sarg->sigalgcnt++] = sarg->ctx->sigalg_lookup_cache[i].sigalg; |
| 3796 |
goto found; |
| 3797 |
} |
| 3798 |
} |
| 3799 |
} else { |
| 3800 |
/* Syntax checks use the built-in sigalgs */ |
| 3801 |
for (i = 0, s = sigalg_lookup_tbl; |
| 3802 |
i < OSSL_NELEM(sigalg_lookup_tbl); i++, s++) { |
| 3803 |
iana = s->name; |
| 3804 |
alias = s->name12; |
| 3805 |
if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0) |
| 3806 |
|| OPENSSL_strcasecmp(etmp, iana) == 0) { |
| 3807 |
sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
| 3808 |
goto found; |
| 3809 |
} |
| 3810 |
} |
| 3811 |
} |
| 3812 |
} else { |
| 3813 |
*p = 0; |
| 3814 |
p++; |
| 3815 |
if (*p == 0) |
| 3816 |
return 0; |
| 3817 |
get_sigorhash(&sig_alg, &hash_alg, etmp); |
| 3818 |
get_sigorhash(&sig_alg, &hash_alg, p); |
| 3819 |
if (sig_alg != NID_undef && hash_alg != NID_undef) { |
| 3820 |
if (sarg->ctx != NULL) { |
| 3821 |
for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { |
| 3822 |
s = &sarg->ctx->sigalg_lookup_cache[i]; |
| 3823 |
if (s->hash == hash_alg && s->sig == sig_alg) { |
| 3824 |
/* Ignore known, but unavailable sigalgs. */ |
| 3825 |
if (!sarg->ctx->sigalg_lookup_cache[i].available) |
| 3826 |
return 1; |
| 3827 |
sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
| 3828 |
goto found; |
| 3829 |
} |
| 3830 |
} |
| 3831 |
} else { |
| 3832 |
for (i = 0; i < OSSL_NELEM(sigalg_lookup_tbl); i++) { |
| 3833 |
s = &sigalg_lookup_tbl[i]; |
| 3834 |
if (s->hash == hash_alg && s->sig == sig_alg) { |
| 3835 |
sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
| 3836 |
goto found; |
| 3837 |
} |
| 3838 |
} |
| 3839 |
} |
| 3840 |
} |
| 3841 |
} |
| 3842 |
/* Ignore unknown algorithms if ignore_unknown */ |
| 3843 |
return ignore_unknown; |
| 3844 |
|
| 3845 |
found: |
| 3846 |
/* Ignore duplicates */ |
| 3847 |
for (i = 0; i < sarg->sigalgcnt - 1; i++) { |
| 3848 |
if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) { |
| 3849 |
sarg->sigalgcnt--; |
| 3850 |
return 1; |
| 3851 |
} |
| 3852 |
} |
| 3853 |
return 1; |
| 3854 |
} |
| 3855 |
|
| 3856 |
/* |
| 3857 |
* Set supported signature algorithms based on a colon separated list of the |
| 3858 |
* form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
| 3859 |
*/ |
| 3860 |
int tls1_set_sigalgs_list(SSL_CTX *ctx, CERT *c, const char *str, int client) |
| 3861 |
{ |
| 3862 |
sig_cb_st sig; |
| 3863 |
sig.sigalgcnt = 0; |
| 3864 |
|
| 3865 |
if (ctx != NULL) |
| 3866 |
sig.ctx = ctx; |
| 3867 |
if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) |
| 3868 |
return 0; |
| 3869 |
if (sig.sigalgcnt == 0) { |
| 3870 |
ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
| 3871 |
"No valid signature algorithms in '%s'", str); |
| 3872 |
return 0; |
| 3873 |
} |
| 3874 |
if (c == NULL) |
| 3875 |
return 1; |
| 3876 |
return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
| 3877 |
} |
| 3878 |
|
| 3879 |
int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen, |
| 3880 |
int client) |
| 3881 |
{ |
| 3882 |
uint16_t *sigalgs; |
| 3883 |
|
| 3884 |
if ((sigalgs = OPENSSL_malloc_array(salglen, sizeof(*sigalgs))) == NULL) |
| 3885 |
return 0; |
| 3886 |
memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs)); |
| 3887 |
|
| 3888 |
if (client) { |
| 3889 |
OPENSSL_free(c->client_sigalgs); |
| 3890 |
c->client_sigalgs = sigalgs; |
| 3891 |
c->client_sigalgslen = salglen; |
| 3892 |
} else { |
| 3893 |
OPENSSL_free(c->conf_sigalgs); |
| 3894 |
c->conf_sigalgs = sigalgs; |
| 3895 |
c->conf_sigalgslen = salglen; |
| 3896 |
} |
| 3897 |
|
| 3898 |
return 1; |
| 3899 |
} |
| 3900 |
|
| 3901 |
int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
| 3902 |
{ |
| 3903 |
uint16_t *sigalgs, *sptr; |
| 3904 |
size_t i; |
| 3905 |
|
| 3906 |
if (salglen & 1) |
| 3907 |
return 0; |
| 3908 |
if ((sigalgs = OPENSSL_malloc_array(salglen / 2, sizeof(*sigalgs))) == NULL) |
| 3909 |
return 0; |
| 3910 |
for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
| 3911 |
size_t j; |
| 3912 |
const SIGALG_LOOKUP *curr; |
| 3913 |
int md_id = *psig_nids++; |
| 3914 |
int sig_id = *psig_nids++; |
| 3915 |
|
| 3916 |
for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); |
| 3917 |
j++, curr++) { |
| 3918 |
if (curr->hash == md_id && curr->sig == sig_id) { |
| 3919 |
*sptr++ = curr->sigalg; |
| 3920 |
break; |
| 3921 |
} |
| 3922 |
} |
| 3923 |
|
| 3924 |
if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
| 3925 |
goto err; |
| 3926 |
} |
| 3927 |
|
| 3928 |
if (client) { |
| 3929 |
OPENSSL_free(c->client_sigalgs); |
| 3930 |
c->client_sigalgs = sigalgs; |
| 3931 |
c->client_sigalgslen = salglen / 2; |
| 3932 |
} else { |
| 3933 |
OPENSSL_free(c->conf_sigalgs); |
| 3934 |
c->conf_sigalgs = sigalgs; |
| 3935 |
c->conf_sigalgslen = salglen / 2; |
| 3936 |
} |
| 3937 |
|
| 3938 |
return 1; |
| 3939 |
|
| 3940 |
err: |
| 3941 |
OPENSSL_free(sigalgs); |
| 3942 |
return 0; |
| 3943 |
} |
| 3944 |
|
| 3945 |
static int tls1_check_sig_alg(SSL_CONNECTION *s, X509 *x, int default_nid) |
| 3946 |
{ |
| 3947 |
int sig_nid, use_pc_sigalgs = 0; |
| 3948 |
size_t i; |
| 3949 |
const SIGALG_LOOKUP *sigalg; |
| 3950 |
size_t sigalgslen; |
| 3951 |
|
| 3952 |
/*- |
| 3953 |
* RFC 8446, section 4.2.3: |
| 3954 |
* |
| 3955 |
* The signatures on certificates that are self-signed or certificates |
| 3956 |
* that are trust anchors are not validated, since they begin a |
| 3957 |
* certification path (see [RFC5280], Section 3.2). A certificate that |
| 3958 |
* begins a certification path MAY use a signature algorithm that is not |
| 3959 |
* advertised as being supported in the "signature_algorithms" |
| 3960 |
* extension. |
| 3961 |
*/ |
| 3962 |
if (default_nid == -1 || X509_self_signed(x, 0)) |
| 3963 |
return 1; |
| 3964 |
sig_nid = X509_get_signature_nid(x); |
| 3965 |
if (default_nid) |
| 3966 |
return sig_nid == default_nid ? 1 : 0; |
| 3967 |
|
| 3968 |
if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) { |
| 3969 |
/* |
| 3970 |
* If we're in TLSv1.3 then we only get here if we're checking the |
| 3971 |
* chain. If the peer has specified peer_cert_sigalgs then we use them |
| 3972 |
* otherwise we default to normal sigalgs. |
| 3973 |
*/ |
| 3974 |
sigalgslen = s->s3.tmp.peer_cert_sigalgslen; |
| 3975 |
use_pc_sigalgs = 1; |
| 3976 |
} else { |
| 3977 |
sigalgslen = s->shared_sigalgslen; |
| 3978 |
} |
| 3979 |
for (i = 0; i < sigalgslen; i++) { |
| 3980 |
int mdnid, pknid; |
| 3981 |
|
| 3982 |
sigalg = use_pc_sigalgs |
| 3983 |
? tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
| 3984 |
s->s3.tmp.peer_cert_sigalgs[i]) |
| 3985 |
: s->shared_sigalgs[i]; |
| 3986 |
if (sigalg == NULL) |
| 3987 |
continue; |
| 3988 |
if (sig_nid == sigalg->sigandhash) |
| 3989 |
return 1; |
| 3990 |
if (sigalg->sig != EVP_PKEY_RSA_PSS) |
| 3991 |
continue; |
| 3992 |
/* |
| 3993 |
* Accept RSA PKCS#1 signatures in certificates when the signature |
| 3994 |
* algorithms include RSA-PSS with a matching digest algorithm. |
| 3995 |
* |
| 3996 |
* When a TLS 1.3 peer inadvertently omits the legacy RSA PKCS#1 code |
| 3997 |
* points, and we're doing strict checking of the certificate chain (in |
| 3998 |
* a cert_cb via SSL_check_chain()) we may then reject RSA signed |
| 3999 |
* certificates in the chain, but the TLS requirement on PSS should not |
| 4000 |
* extend to certificates. Though the peer can in fact list the legacy |
| 4001 |
* sigalgs for just this purpose, it is not likely that a better chain |
| 4002 |
* signed with RSA-PSS is available. |
| 4003 |
*/ |
| 4004 |
if (!OBJ_find_sigid_algs(sig_nid, &mdnid, &pknid)) |
| 4005 |
continue; |
| 4006 |
if (pknid == EVP_PKEY_RSA && mdnid == sigalg->hash) |
| 4007 |
return 1; |
| 4008 |
} |
| 4009 |
return 0; |
| 4010 |
} |
| 4011 |
|
| 4012 |
/* Check to see if a certificate issuer name matches list of CA names */ |
| 4013 |
static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) |
| 4014 |
{ |
| 4015 |
const X509_NAME *nm; |
| 4016 |
int i; |
| 4017 |
nm = X509_get_issuer_name(x); |
| 4018 |
for (i = 0; i < sk_X509_NAME_num(names); i++) { |
| 4019 |
if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) |
| 4020 |
return 1; |
| 4021 |
} |
| 4022 |
return 0; |
| 4023 |
} |
| 4024 |
|
| 4025 |
/* |
| 4026 |
* Check certificate chain is consistent with TLS extensions and is usable by |
| 4027 |
* server. This servers two purposes: it allows users to check chains before |
| 4028 |
* passing them to the server and it allows the server to check chains before |
| 4029 |
* attempting to use them. |
| 4030 |
*/ |
| 4031 |
|
| 4032 |
/* Flags which need to be set for a certificate when strict mode not set */ |
| 4033 |
|
| 4034 |
#define CERT_PKEY_VALID_FLAGS \ |
| 4035 |
(CERT_PKEY_EE_SIGNATURE | CERT_PKEY_EE_PARAM) |
| 4036 |
/* Strict mode flags */ |
| 4037 |
#define CERT_PKEY_STRICT_FLAGS \ |
| 4038 |
(CERT_PKEY_VALID_FLAGS | CERT_PKEY_CA_SIGNATURE | CERT_PKEY_CA_PARAM \ |
| 4039 |
| CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE) |
| 4040 |
|
| 4041 |
int tls1_check_chain(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pk, |
| 4042 |
STACK_OF(X509) *chain, int idx) |
| 4043 |
{ |
| 4044 |
int i; |
| 4045 |
int rv = 0; |
| 4046 |
int check_flags = 0, strict_mode; |
| 4047 |
CERT_PKEY *cpk = NULL; |
| 4048 |
CERT *c = s->cert; |
| 4049 |
uint32_t *pvalid; |
| 4050 |
unsigned int suiteb_flags = tls1_suiteb(s); |
| 4051 |
|
| 4052 |
/* |
| 4053 |
* Meaning of idx: |
| 4054 |
* idx == -1 means SSL_check_chain() invocation |
| 4055 |
* idx == -2 means checking client certificate chains |
| 4056 |
* idx >= 0 means checking SSL_PKEY index |
| 4057 |
* |
| 4058 |
* For RPK, where there may be no cert, we ignore -1 |
| 4059 |
*/ |
| 4060 |
if (idx != -1) { |
| 4061 |
if (idx == -2) { |
| 4062 |
cpk = c->key; |
| 4063 |
idx = (int)(cpk - c->pkeys); |
| 4064 |
} else |
| 4065 |
cpk = c->pkeys + idx; |
| 4066 |
pvalid = s->s3.tmp.valid_flags + idx; |
| 4067 |
x = cpk->x509; |
| 4068 |
pk = cpk->privatekey; |
| 4069 |
chain = cpk->chain; |
| 4070 |
strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; |
| 4071 |
if (tls12_rpk_and_privkey(s, idx)) { |
| 4072 |
if (EVP_PKEY_is_a(pk, "EC") && !tls1_check_pkey_comp(s, pk)) |
| 4073 |
return 0; |
| 4074 |
*pvalid = rv = CERT_PKEY_RPK; |
| 4075 |
return rv; |
| 4076 |
} |
| 4077 |
/* If no cert or key, forget it */ |
| 4078 |
if (x == NULL || pk == NULL) |
| 4079 |
goto end; |
| 4080 |
} else { |
| 4081 |
size_t certidx; |
| 4082 |
|
| 4083 |
if (x == NULL || pk == NULL) |
| 4084 |
return 0; |
| 4085 |
|
| 4086 |
if (ssl_cert_lookup_by_pkey(pk, &certidx, |
| 4087 |
SSL_CONNECTION_GET_CTX(s)) |
| 4088 |
== NULL) |
| 4089 |
return 0; |
| 4090 |
idx = (int)certidx; |
| 4091 |
pvalid = s->s3.tmp.valid_flags + idx; |
| 4092 |
|
| 4093 |
if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
| 4094 |
check_flags = CERT_PKEY_STRICT_FLAGS; |
| 4095 |
else |
| 4096 |
check_flags = CERT_PKEY_VALID_FLAGS; |
| 4097 |
strict_mode = 1; |
| 4098 |
} |
| 4099 |
|
| 4100 |
if (suiteb_flags) { |
| 4101 |
int ok; |
| 4102 |
if (check_flags) |
| 4103 |
check_flags |= CERT_PKEY_SUITEB; |
| 4104 |
ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); |
| 4105 |
if (ok == X509_V_OK) |
| 4106 |
rv |= CERT_PKEY_SUITEB; |
| 4107 |
else if (!check_flags) |
| 4108 |
goto end; |
| 4109 |
} |
| 4110 |
|
| 4111 |
/* |
| 4112 |
* Check all signature algorithms are consistent with signature |
| 4113 |
* algorithms extension if TLS 1.2 or later and strict mode. |
| 4114 |
*/ |
| 4115 |
if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION |
| 4116 |
&& strict_mode) { |
| 4117 |
int default_nid; |
| 4118 |
int rsign = 0; |
| 4119 |
|
| 4120 |
if (s->s3.tmp.peer_cert_sigalgs != NULL |
| 4121 |
|| s->s3.tmp.peer_sigalgs != NULL) { |
| 4122 |
default_nid = 0; |
| 4123 |
/* If no sigalgs extension use defaults from RFC5246 */ |
| 4124 |
} else { |
| 4125 |
switch (idx) { |
| 4126 |
case SSL_PKEY_RSA: |
| 4127 |
rsign = EVP_PKEY_RSA; |
| 4128 |
default_nid = NID_sha1WithRSAEncryption; |
| 4129 |
break; |
| 4130 |
|
| 4131 |
case SSL_PKEY_DSA_SIGN: |
| 4132 |
rsign = EVP_PKEY_DSA; |
| 4133 |
default_nid = NID_dsaWithSHA1; |
| 4134 |
break; |
| 4135 |
|
| 4136 |
case SSL_PKEY_ECC: |
| 4137 |
rsign = EVP_PKEY_EC; |
| 4138 |
default_nid = NID_ecdsa_with_SHA1; |
| 4139 |
break; |
| 4140 |
|
| 4141 |
case SSL_PKEY_GOST01: |
| 4142 |
rsign = NID_id_GostR3410_2001; |
| 4143 |
default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
| 4144 |
break; |
| 4145 |
|
| 4146 |
case SSL_PKEY_GOST12_256: |
| 4147 |
rsign = NID_id_GostR3410_2012_256; |
| 4148 |
default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
| 4149 |
break; |
| 4150 |
|
| 4151 |
case SSL_PKEY_GOST12_512: |
| 4152 |
rsign = NID_id_GostR3410_2012_512; |
| 4153 |
default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
| 4154 |
break; |
| 4155 |
|
| 4156 |
default: |
| 4157 |
default_nid = -1; |
| 4158 |
break; |
| 4159 |
} |
| 4160 |
} |
| 4161 |
/* |
| 4162 |
* If peer sent no signature algorithms extension and we have set |
| 4163 |
* preferred signature algorithms check we support sha1. |
| 4164 |
*/ |
| 4165 |
if (default_nid > 0 && c->conf_sigalgs) { |
| 4166 |
size_t j; |
| 4167 |
const uint16_t *p = c->conf_sigalgs; |
| 4168 |
for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
| 4169 |
const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *p); |
| 4170 |
|
| 4171 |
if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) |
| 4172 |
break; |
| 4173 |
} |
| 4174 |
if (j == c->conf_sigalgslen) { |
| 4175 |
if (check_flags) |
| 4176 |
goto skip_sigs; |
| 4177 |
else |
| 4178 |
goto end; |
| 4179 |
} |
| 4180 |
} |
| 4181 |
/* Check signature algorithm of each cert in chain */ |
| 4182 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 4183 |
/* |
| 4184 |
* We only get here if the application has called SSL_check_chain(), |
| 4185 |
* so check_flags is always set. |
| 4186 |
*/ |
| 4187 |
if (find_sig_alg(s, x, pk) != NULL) |
| 4188 |
rv |= CERT_PKEY_EE_SIGNATURE; |
| 4189 |
} else if (!tls1_check_sig_alg(s, x, default_nid)) { |
| 4190 |
if (!check_flags) |
| 4191 |
goto end; |
| 4192 |
} else |
| 4193 |
rv |= CERT_PKEY_EE_SIGNATURE; |
| 4194 |
rv |= CERT_PKEY_CA_SIGNATURE; |
| 4195 |
for (i = 0; i < sk_X509_num(chain); i++) { |
| 4196 |
if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) { |
| 4197 |
if (check_flags) { |
| 4198 |
rv &= ~CERT_PKEY_CA_SIGNATURE; |
| 4199 |
break; |
| 4200 |
} else |
| 4201 |
goto end; |
| 4202 |
} |
| 4203 |
} |
| 4204 |
} |
| 4205 |
/* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ |
| 4206 |
else if (check_flags) |
| 4207 |
rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; |
| 4208 |
skip_sigs: |
| 4209 |
/* Check cert parameters are consistent */ |
| 4210 |
if (tls1_check_cert_param(s, x, 1)) |
| 4211 |
rv |= CERT_PKEY_EE_PARAM; |
| 4212 |
else if (!check_flags) |
| 4213 |
goto end; |
| 4214 |
if (!s->server) |
| 4215 |
rv |= CERT_PKEY_CA_PARAM; |
| 4216 |
/* In strict mode check rest of chain too */ |
| 4217 |
else if (strict_mode) { |
| 4218 |
rv |= CERT_PKEY_CA_PARAM; |
| 4219 |
for (i = 0; i < sk_X509_num(chain); i++) { |
| 4220 |
X509 *ca = sk_X509_value(chain, i); |
| 4221 |
if (!tls1_check_cert_param(s, ca, 0)) { |
| 4222 |
if (check_flags) { |
| 4223 |
rv &= ~CERT_PKEY_CA_PARAM; |
| 4224 |
break; |
| 4225 |
} else |
| 4226 |
goto end; |
| 4227 |
} |
| 4228 |
} |
| 4229 |
} |
| 4230 |
if (!s->server && strict_mode) { |
| 4231 |
STACK_OF(X509_NAME) *ca_dn; |
| 4232 |
int check_type = 0; |
| 4233 |
|
| 4234 |
if (EVP_PKEY_is_a(pk, "RSA")) |
| 4235 |
check_type = TLS_CT_RSA_SIGN; |
| 4236 |
else if (EVP_PKEY_is_a(pk, "DSA")) |
| 4237 |
check_type = TLS_CT_DSS_SIGN; |
| 4238 |
else if (EVP_PKEY_is_a(pk, "EC")) |
| 4239 |
check_type = TLS_CT_ECDSA_SIGN; |
| 4240 |
|
| 4241 |
if (check_type) { |
| 4242 |
const uint8_t *ctypes = s->s3.tmp.ctype; |
| 4243 |
size_t j; |
| 4244 |
|
| 4245 |
for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) { |
| 4246 |
if (*ctypes == check_type) { |
| 4247 |
rv |= CERT_PKEY_CERT_TYPE; |
| 4248 |
break; |
| 4249 |
} |
| 4250 |
} |
| 4251 |
if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) |
| 4252 |
goto end; |
| 4253 |
} else { |
| 4254 |
rv |= CERT_PKEY_CERT_TYPE; |
| 4255 |
} |
| 4256 |
|
| 4257 |
ca_dn = s->s3.tmp.peer_ca_names; |
| 4258 |
|
| 4259 |
if (ca_dn == NULL |
| 4260 |
|| sk_X509_NAME_num(ca_dn) == 0 |
| 4261 |
|| ssl_check_ca_name(ca_dn, x)) |
| 4262 |
rv |= CERT_PKEY_ISSUER_NAME; |
| 4263 |
else |
| 4264 |
for (i = 0; i < sk_X509_num(chain); i++) { |
| 4265 |
X509 *xtmp = sk_X509_value(chain, i); |
| 4266 |
|
| 4267 |
if (ssl_check_ca_name(ca_dn, xtmp)) { |
| 4268 |
rv |= CERT_PKEY_ISSUER_NAME; |
| 4269 |
break; |
| 4270 |
} |
| 4271 |
} |
| 4272 |
|
| 4273 |
if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
| 4274 |
goto end; |
| 4275 |
} else |
| 4276 |
rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; |
| 4277 |
|
| 4278 |
if (!check_flags || (rv & check_flags) == check_flags) |
| 4279 |
rv |= CERT_PKEY_VALID; |
| 4280 |
|
| 4281 |
end: |
| 4282 |
|
| 4283 |
if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION) |
| 4284 |
rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); |
| 4285 |
else |
| 4286 |
rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
| 4287 |
|
| 4288 |
/* |
| 4289 |
* When checking a CERT_PKEY structure all flags are irrelevant if the |
| 4290 |
* chain is invalid. |
| 4291 |
*/ |
| 4292 |
if (!check_flags) { |
| 4293 |
if (rv & CERT_PKEY_VALID) { |
| 4294 |
*pvalid = rv; |
| 4295 |
} else { |
| 4296 |
/* Preserve sign and explicit sign flag, clear rest */ |
| 4297 |
*pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
| 4298 |
return 0; |
| 4299 |
} |
| 4300 |
} |
| 4301 |
return rv; |
| 4302 |
} |
| 4303 |
|
| 4304 |
/* Set validity of certificates in an SSL structure */ |
| 4305 |
void tls1_set_cert_validity(SSL_CONNECTION *s) |
| 4306 |
{ |
| 4307 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
| 4308 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN); |
| 4309 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
| 4310 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
| 4311 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
| 4312 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); |
| 4313 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); |
| 4314 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519); |
| 4315 |
tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448); |
| 4316 |
} |
| 4317 |
|
| 4318 |
/* User level utility function to check a chain is suitable */ |
| 4319 |
int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
| 4320 |
{ |
| 4321 |
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
| 4322 |
|
| 4323 |
if (sc == NULL) |
| 4324 |
return 0; |
| 4325 |
|
| 4326 |
return tls1_check_chain(sc, x, pk, chain, -1); |
| 4327 |
} |
| 4328 |
|
| 4329 |
EVP_PKEY *ssl_get_auto_dh(SSL_CONNECTION *s) |
| 4330 |
{ |
| 4331 |
EVP_PKEY *dhp = NULL; |
| 4332 |
BIGNUM *p; |
| 4333 |
int dh_secbits = 80, sec_level_bits; |
| 4334 |
EVP_PKEY_CTX *pctx = NULL; |
| 4335 |
OSSL_PARAM_BLD *tmpl = NULL; |
| 4336 |
OSSL_PARAM *params = NULL; |
| 4337 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 4338 |
|
| 4339 |
if (s->cert->dh_tmp_auto != 2) { |
| 4340 |
if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
| 4341 |
if (s->s3.tmp.new_cipher->strength_bits == 256) |
| 4342 |
dh_secbits = 128; |
| 4343 |
else |
| 4344 |
dh_secbits = 80; |
| 4345 |
} else { |
| 4346 |
if (s->s3.tmp.cert == NULL) |
| 4347 |
return NULL; |
| 4348 |
dh_secbits = EVP_PKEY_get_security_bits(s->s3.tmp.cert->privatekey); |
| 4349 |
} |
| 4350 |
} |
| 4351 |
|
| 4352 |
/* Do not pick a prime that is too weak for the current security level */ |
| 4353 |
sec_level_bits = ssl_get_security_level_bits(SSL_CONNECTION_GET_SSL(s), |
| 4354 |
NULL, NULL); |
| 4355 |
if (dh_secbits < sec_level_bits) |
| 4356 |
dh_secbits = sec_level_bits; |
| 4357 |
|
| 4358 |
if (dh_secbits >= 192) |
| 4359 |
p = BN_get_rfc3526_prime_8192(NULL); |
| 4360 |
else if (dh_secbits >= 152) |
| 4361 |
p = BN_get_rfc3526_prime_4096(NULL); |
| 4362 |
else if (dh_secbits >= 128) |
| 4363 |
p = BN_get_rfc3526_prime_3072(NULL); |
| 4364 |
else if (dh_secbits >= 112) |
| 4365 |
p = BN_get_rfc3526_prime_2048(NULL); |
| 4366 |
else |
| 4367 |
p = BN_get_rfc2409_prime_1024(NULL); |
| 4368 |
if (p == NULL) |
| 4369 |
goto err; |
| 4370 |
|
| 4371 |
pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq); |
| 4372 |
if (pctx == NULL |
| 4373 |
|| EVP_PKEY_fromdata_init(pctx) != 1) |
| 4374 |
goto err; |
| 4375 |
|
| 4376 |
tmpl = OSSL_PARAM_BLD_new(); |
| 4377 |
if (tmpl == NULL |
| 4378 |
|| !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
| 4379 |
|| !OSSL_PARAM_BLD_push_uint(tmpl, OSSL_PKEY_PARAM_FFC_G, 2)) |
| 4380 |
goto err; |
| 4381 |
|
| 4382 |
params = OSSL_PARAM_BLD_to_param(tmpl); |
| 4383 |
if (params == NULL |
| 4384 |
|| EVP_PKEY_fromdata(pctx, &dhp, EVP_PKEY_KEY_PARAMETERS, params) != 1) |
| 4385 |
goto err; |
| 4386 |
|
| 4387 |
err: |
| 4388 |
OSSL_PARAM_free(params); |
| 4389 |
OSSL_PARAM_BLD_free(tmpl); |
| 4390 |
EVP_PKEY_CTX_free(pctx); |
| 4391 |
BN_free(p); |
| 4392 |
return dhp; |
| 4393 |
} |
| 4394 |
|
| 4395 |
static int ssl_security_cert_key(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
| 4396 |
int op) |
| 4397 |
{ |
| 4398 |
int secbits = -1; |
| 4399 |
EVP_PKEY *pkey = X509_get0_pubkey(x); |
| 4400 |
|
| 4401 |
if (pkey) { |
| 4402 |
/* |
| 4403 |
* If no parameters this will return -1 and fail using the default |
| 4404 |
* security callback for any non-zero security level. This will |
| 4405 |
* reject keys which omit parameters but this only affects DSA and |
| 4406 |
* omission of parameters is never (?) done in practice. |
| 4407 |
*/ |
| 4408 |
secbits = EVP_PKEY_get_security_bits(pkey); |
| 4409 |
} |
| 4410 |
if (s != NULL) |
| 4411 |
return ssl_security(s, op, secbits, 0, x); |
| 4412 |
else |
| 4413 |
return ssl_ctx_security(ctx, op, secbits, 0, x); |
| 4414 |
} |
| 4415 |
|
| 4416 |
static int ssl_security_cert_sig(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
| 4417 |
int op) |
| 4418 |
{ |
| 4419 |
/* Lookup signature algorithm digest */ |
| 4420 |
int secbits, nid, pknid; |
| 4421 |
|
| 4422 |
/* Don't check signature if self signed */ |
| 4423 |
if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) |
| 4424 |
return 1; |
| 4425 |
if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL)) |
| 4426 |
secbits = -1; |
| 4427 |
/* If digest NID not defined use signature NID */ |
| 4428 |
if (nid == NID_undef) |
| 4429 |
nid = pknid; |
| 4430 |
if (s != NULL) |
| 4431 |
return ssl_security(s, op, secbits, nid, x); |
| 4432 |
else |
| 4433 |
return ssl_ctx_security(ctx, op, secbits, nid, x); |
| 4434 |
} |
| 4435 |
|
| 4436 |
int ssl_security_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, int vfy, |
| 4437 |
int is_ee) |
| 4438 |
{ |
| 4439 |
if (vfy) |
| 4440 |
vfy = SSL_SECOP_PEER; |
| 4441 |
if (is_ee) { |
| 4442 |
if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) |
| 4443 |
return SSL_R_EE_KEY_TOO_SMALL; |
| 4444 |
} else { |
| 4445 |
if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) |
| 4446 |
return SSL_R_CA_KEY_TOO_SMALL; |
| 4447 |
} |
| 4448 |
if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) |
| 4449 |
return SSL_R_CA_MD_TOO_WEAK; |
| 4450 |
return 1; |
| 4451 |
} |
| 4452 |
|
| 4453 |
/* |
| 4454 |
* Check security of a chain, if |sk| includes the end entity certificate then |
| 4455 |
* |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending |
| 4456 |
* one to the peer. Return values: 1 if ok otherwise error code to use |
| 4457 |
*/ |
| 4458 |
|
| 4459 |
int ssl_security_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk, |
| 4460 |
X509 *x, int vfy) |
| 4461 |
{ |
| 4462 |
int rv, start_idx, i; |
| 4463 |
|
| 4464 |
if (x == NULL) { |
| 4465 |
x = sk_X509_value(sk, 0); |
| 4466 |
if (x == NULL) |
| 4467 |
return ERR_R_INTERNAL_ERROR; |
| 4468 |
start_idx = 1; |
| 4469 |
} else |
| 4470 |
start_idx = 0; |
| 4471 |
|
| 4472 |
rv = ssl_security_cert(s, NULL, x, vfy, 1); |
| 4473 |
if (rv != 1) |
| 4474 |
return rv; |
| 4475 |
|
| 4476 |
for (i = start_idx; i < sk_X509_num(sk); i++) { |
| 4477 |
x = sk_X509_value(sk, i); |
| 4478 |
rv = ssl_security_cert(s, NULL, x, vfy, 0); |
| 4479 |
if (rv != 1) |
| 4480 |
return rv; |
| 4481 |
} |
| 4482 |
return 1; |
| 4483 |
} |
| 4484 |
|
| 4485 |
/* |
| 4486 |
* For TLS 1.2 servers check if we have a certificate which can be used |
| 4487 |
* with the signature algorithm "lu" and return index of certificate. |
| 4488 |
*/ |
| 4489 |
|
| 4490 |
static int tls12_get_cert_sigalg_idx(const SSL_CONNECTION *s, |
| 4491 |
const SIGALG_LOOKUP *lu) |
| 4492 |
{ |
| 4493 |
int sig_idx = lu->sig_idx; |
| 4494 |
const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx, |
| 4495 |
SSL_CONNECTION_GET_CTX(s)); |
| 4496 |
|
| 4497 |
/* If not recognised or not supported by cipher mask it is not suitable */ |
| 4498 |
if (clu == NULL |
| 4499 |
|| (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0 |
| 4500 |
|| (clu->pkey_nid == EVP_PKEY_RSA_PSS |
| 4501 |
&& (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0)) |
| 4502 |
return -1; |
| 4503 |
|
| 4504 |
/* If doing RPK, the CERT_PKEY won't be "valid" */ |
| 4505 |
if (tls12_rpk_and_privkey(s, sig_idx)) |
| 4506 |
return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_RPK ? sig_idx : -1; |
| 4507 |
|
| 4508 |
return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1; |
| 4509 |
} |
| 4510 |
|
| 4511 |
/* |
| 4512 |
* Checks the given cert against signature_algorithm_cert restrictions sent by |
| 4513 |
* the peer (if any) as well as whether the hash from the sigalg is usable with |
| 4514 |
* the key. |
| 4515 |
* Returns true if the cert is usable and false otherwise. |
| 4516 |
*/ |
| 4517 |
static int check_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, |
| 4518 |
X509 *x, EVP_PKEY *pkey) |
| 4519 |
{ |
| 4520 |
const SIGALG_LOOKUP *lu; |
| 4521 |
int mdnid, pknid, supported; |
| 4522 |
size_t i; |
| 4523 |
const char *mdname = NULL; |
| 4524 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 4525 |
|
| 4526 |
/* |
| 4527 |
* If the given EVP_PKEY cannot support signing with this digest, |
| 4528 |
* the answer is simply 'no'. |
| 4529 |
*/ |
| 4530 |
if (sig->hash != NID_undef) |
| 4531 |
mdname = OBJ_nid2sn(sig->hash); |
| 4532 |
supported = EVP_PKEY_digestsign_supports_digest(pkey, sctx->libctx, |
| 4533 |
mdname, |
| 4534 |
sctx->propq); |
| 4535 |
if (supported <= 0) |
| 4536 |
return 0; |
| 4537 |
|
| 4538 |
/* |
| 4539 |
* The TLS 1.3 signature_algorithms_cert extension places restrictions |
| 4540 |
* on the sigalg with which the certificate was signed (by its issuer). |
| 4541 |
*/ |
| 4542 |
if (s->s3.tmp.peer_cert_sigalgs != NULL) { |
| 4543 |
if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL)) |
| 4544 |
return 0; |
| 4545 |
for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) { |
| 4546 |
lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
| 4547 |
s->s3.tmp.peer_cert_sigalgs[i]); |
| 4548 |
if (lu == NULL) |
| 4549 |
continue; |
| 4550 |
|
| 4551 |
/* |
| 4552 |
* This does not differentiate between the |
| 4553 |
* rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not |
| 4554 |
* have a chain here that lets us look at the key OID in the |
| 4555 |
* signing certificate. |
| 4556 |
*/ |
| 4557 |
if (mdnid == lu->hash && pknid == lu->sig) |
| 4558 |
return 1; |
| 4559 |
} |
| 4560 |
return 0; |
| 4561 |
} |
| 4562 |
|
| 4563 |
/* |
| 4564 |
* Without signat_algorithms_cert, any certificate for which we have |
| 4565 |
* a viable public key is permitted. |
| 4566 |
*/ |
| 4567 |
return 1; |
| 4568 |
} |
| 4569 |
|
| 4570 |
/* |
| 4571 |
* Returns true if |s| has a usable certificate configured for use |
| 4572 |
* with signature scheme |sig|. |
| 4573 |
* "Usable" includes a check for presence as well as applying |
| 4574 |
* the signature_algorithm_cert restrictions sent by the peer (if any). |
| 4575 |
* Returns false if no usable certificate is found. |
| 4576 |
*/ |
| 4577 |
static int has_usable_cert(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, int idx) |
| 4578 |
{ |
| 4579 |
/* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */ |
| 4580 |
if (idx == -1) |
| 4581 |
idx = sig->sig_idx; |
| 4582 |
if (!ssl_has_cert(s, idx)) |
| 4583 |
return 0; |
| 4584 |
|
| 4585 |
return check_cert_usable(s, sig, s->cert->pkeys[idx].x509, |
| 4586 |
s->cert->pkeys[idx].privatekey); |
| 4587 |
} |
| 4588 |
|
| 4589 |
/* |
| 4590 |
* Returns true if the supplied cert |x| and key |pkey| is usable with the |
| 4591 |
* specified signature scheme |sig|, or false otherwise. |
| 4592 |
*/ |
| 4593 |
static int is_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, X509 *x, |
| 4594 |
EVP_PKEY *pkey) |
| 4595 |
{ |
| 4596 |
size_t idx; |
| 4597 |
|
| 4598 |
if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) |
| 4599 |
return 0; |
| 4600 |
|
| 4601 |
/* Check the key is consistent with the sig alg */ |
| 4602 |
if ((int)idx != sig->sig_idx) |
| 4603 |
return 0; |
| 4604 |
|
| 4605 |
return check_cert_usable(s, sig, x, pkey); |
| 4606 |
} |
| 4607 |
|
| 4608 |
/* |
| 4609 |
* Find a signature scheme that works with the supplied certificate |x| and key |
| 4610 |
* |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our |
| 4611 |
* available certs/keys to find one that works. |
| 4612 |
*/ |
| 4613 |
static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, |
| 4614 |
EVP_PKEY *pkey) |
| 4615 |
{ |
| 4616 |
const SIGALG_LOOKUP *lu = NULL; |
| 4617 |
size_t i; |
| 4618 |
int curve = -1; |
| 4619 |
EVP_PKEY *tmppkey; |
| 4620 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 4621 |
|
| 4622 |
/* Look for a shared sigalgs matching possible certificates */ |
| 4623 |
for (i = 0; i < s->shared_sigalgslen; i++) { |
| 4624 |
/* Skip SHA1, SHA224, DSA and RSA if not PSS */ |
| 4625 |
lu = s->shared_sigalgs[i]; |
| 4626 |
if (lu->hash == NID_sha1 |
| 4627 |
|| lu->hash == NID_sha224 |
| 4628 |
|| lu->sig == EVP_PKEY_DSA |
| 4629 |
|| lu->sig == EVP_PKEY_RSA |
| 4630 |
|| !tls_sigalg_compat(s, lu)) |
| 4631 |
continue; |
| 4632 |
|
| 4633 |
/* Check that we have a cert, and signature_algorithms_cert */ |
| 4634 |
if (!tls1_lookup_md(sctx, lu, NULL)) |
| 4635 |
continue; |
| 4636 |
if ((pkey == NULL && !has_usable_cert(s, lu, -1)) |
| 4637 |
|| (pkey != NULL && !is_cert_usable(s, lu, x, pkey))) |
| 4638 |
continue; |
| 4639 |
|
| 4640 |
tmppkey = (pkey != NULL) ? pkey |
| 4641 |
: s->cert->pkeys[lu->sig_idx].privatekey; |
| 4642 |
|
| 4643 |
if (lu->sig == EVP_PKEY_EC) { |
| 4644 |
if (curve == -1) |
| 4645 |
curve = ssl_get_EC_curve_nid(tmppkey); |
| 4646 |
if (lu->curve != NID_undef && curve != lu->curve) |
| 4647 |
continue; |
| 4648 |
} else if (lu->sig == EVP_PKEY_RSA_PSS) { |
| 4649 |
/* validate that key is large enough for the signature algorithm */ |
| 4650 |
if (!rsa_pss_check_min_key_size(sctx, tmppkey, lu)) |
| 4651 |
continue; |
| 4652 |
} |
| 4653 |
break; |
| 4654 |
} |
| 4655 |
|
| 4656 |
if (i == s->shared_sigalgslen) |
| 4657 |
return NULL; |
| 4658 |
|
| 4659 |
return lu; |
| 4660 |
} |
| 4661 |
|
| 4662 |
/* |
| 4663 |
* Choose an appropriate signature algorithm based on available certificates |
| 4664 |
* Sets chosen certificate and signature algorithm. |
| 4665 |
* |
| 4666 |
* For servers if we fail to find a required certificate it is a fatal error, |
| 4667 |
* an appropriate error code is set and a TLS alert is sent. |
| 4668 |
* |
| 4669 |
* For clients fatalerrs is set to 0. If a certificate is not suitable it is not |
| 4670 |
* a fatal error: we will either try another certificate or not present one |
| 4671 |
* to the server. In this case no error is set. |
| 4672 |
*/ |
| 4673 |
int tls_choose_sigalg(SSL_CONNECTION *s, int fatalerrs) |
| 4674 |
{ |
| 4675 |
const SIGALG_LOOKUP *lu = NULL; |
| 4676 |
int sig_idx = -1; |
| 4677 |
|
| 4678 |
s->s3.tmp.cert = NULL; |
| 4679 |
s->s3.tmp.sigalg = NULL; |
| 4680 |
|
| 4681 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 4682 |
lu = find_sig_alg(s, NULL, NULL); |
| 4683 |
if (lu == NULL) { |
| 4684 |
if (!fatalerrs) |
| 4685 |
return 1; |
| 4686 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 4687 |
SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 4688 |
return 0; |
| 4689 |
} |
| 4690 |
} else { |
| 4691 |
/* If ciphersuite doesn't require a cert nothing to do */ |
| 4692 |
if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT)) |
| 4693 |
return 1; |
| 4694 |
if (!s->server && !ssl_has_cert(s, (int)(s->cert->key - s->cert->pkeys))) |
| 4695 |
return 1; |
| 4696 |
|
| 4697 |
if (SSL_USE_SIGALGS(s)) { |
| 4698 |
size_t i; |
| 4699 |
if (s->s3.tmp.peer_sigalgs != NULL) { |
| 4700 |
int curve = -1; |
| 4701 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 4702 |
|
| 4703 |
/* For Suite B need to match signature algorithm to curve */ |
| 4704 |
if (tls1_suiteb(s)) |
| 4705 |
curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC] |
| 4706 |
.privatekey); |
| 4707 |
|
| 4708 |
/* |
| 4709 |
* Find highest preference signature algorithm matching |
| 4710 |
* cert type |
| 4711 |
*/ |
| 4712 |
for (i = 0; i < s->shared_sigalgslen; i++) { |
| 4713 |
/* Check the sigalg version bounds */ |
| 4714 |
lu = s->shared_sigalgs[i]; |
| 4715 |
if (!tls_sigalg_compat(s, lu)) |
| 4716 |
continue; |
| 4717 |
if (s->server) { |
| 4718 |
if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1) |
| 4719 |
continue; |
| 4720 |
} else { |
| 4721 |
int cc_idx = (int)(s->cert->key - s->cert->pkeys); |
| 4722 |
|
| 4723 |
sig_idx = lu->sig_idx; |
| 4724 |
if (cc_idx != sig_idx) |
| 4725 |
continue; |
| 4726 |
} |
| 4727 |
/* Check that we have a cert, and sig_algs_cert */ |
| 4728 |
if (!has_usable_cert(s, lu, sig_idx)) |
| 4729 |
continue; |
| 4730 |
if (lu->sig == EVP_PKEY_RSA_PSS) { |
| 4731 |
/* validate that key is large enough for the signature algorithm */ |
| 4732 |
EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey; |
| 4733 |
|
| 4734 |
if (!rsa_pss_check_min_key_size(sctx, pkey, lu)) |
| 4735 |
continue; |
| 4736 |
} |
| 4737 |
if (curve == -1 || lu->curve == curve) |
| 4738 |
break; |
| 4739 |
} |
| 4740 |
#ifndef OPENSSL_NO_GOST |
| 4741 |
/* |
| 4742 |
* Some Windows-based implementations do not send GOST algorithms indication |
| 4743 |
* in supported_algorithms extension, so when we have GOST-based ciphersuite, |
| 4744 |
* we have to assume GOST support. |
| 4745 |
*/ |
| 4746 |
if (i == s->shared_sigalgslen |
| 4747 |
&& (s->s3.tmp.new_cipher->algorithm_auth |
| 4748 |
& (SSL_aGOST01 | SSL_aGOST12)) |
| 4749 |
!= 0) { |
| 4750 |
if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
| 4751 |
if (!fatalerrs) |
| 4752 |
return 1; |
| 4753 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 4754 |
SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 4755 |
return 0; |
| 4756 |
} else { |
| 4757 |
i = 0; |
| 4758 |
sig_idx = lu->sig_idx; |
| 4759 |
} |
| 4760 |
} |
| 4761 |
#endif |
| 4762 |
if (i == s->shared_sigalgslen) { |
| 4763 |
if (!fatalerrs) |
| 4764 |
return 1; |
| 4765 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 4766 |
SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 4767 |
return 0; |
| 4768 |
} |
| 4769 |
} else { |
| 4770 |
/* |
| 4771 |
* If we have no sigalg use defaults |
| 4772 |
*/ |
| 4773 |
const uint16_t *sent_sigs; |
| 4774 |
size_t sent_sigslen; |
| 4775 |
|
| 4776 |
if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
| 4777 |
if (!fatalerrs) |
| 4778 |
return 1; |
| 4779 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 4780 |
SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 4781 |
return 0; |
| 4782 |
} |
| 4783 |
|
| 4784 |
/* Check signature matches a type we sent */ |
| 4785 |
sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
| 4786 |
for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
| 4787 |
if (lu->sigalg == *sent_sigs |
| 4788 |
&& has_usable_cert(s, lu, lu->sig_idx)) |
| 4789 |
break; |
| 4790 |
} |
| 4791 |
if (i == sent_sigslen) { |
| 4792 |
if (!fatalerrs) |
| 4793 |
return 1; |
| 4794 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 4795 |
SSL_R_WRONG_SIGNATURE_TYPE); |
| 4796 |
return 0; |
| 4797 |
} |
| 4798 |
} |
| 4799 |
} else { |
| 4800 |
if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
| 4801 |
if (!fatalerrs) |
| 4802 |
return 1; |
| 4803 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
| 4804 |
SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 4805 |
return 0; |
| 4806 |
} |
| 4807 |
} |
| 4808 |
} |
| 4809 |
if (sig_idx == -1) |
| 4810 |
sig_idx = lu->sig_idx; |
| 4811 |
s->s3.tmp.cert = &s->cert->pkeys[sig_idx]; |
| 4812 |
s->cert->key = s->s3.tmp.cert; |
| 4813 |
s->s3.tmp.sigalg = lu; |
| 4814 |
return 1; |
| 4815 |
} |
| 4816 |
|
| 4817 |
int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode) |
| 4818 |
{ |
| 4819 |
if (mode != TLSEXT_max_fragment_length_DISABLED |
| 4820 |
&& !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { |
| 4821 |
ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
| 4822 |
return 0; |
| 4823 |
} |
| 4824 |
|
| 4825 |
ctx->ext.max_fragment_len_mode = mode; |
| 4826 |
return 1; |
| 4827 |
} |
| 4828 |
|
| 4829 |
int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode) |
| 4830 |
{ |
| 4831 |
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
| 4832 |
|
| 4833 |
if (sc == NULL |
| 4834 |
|| (IS_QUIC(ssl) && mode != TLSEXT_max_fragment_length_DISABLED)) |
| 4835 |
return 0; |
| 4836 |
|
| 4837 |
if (mode != TLSEXT_max_fragment_length_DISABLED |
| 4838 |
&& !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { |
| 4839 |
ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
| 4840 |
return 0; |
| 4841 |
} |
| 4842 |
|
| 4843 |
sc->ext.max_fragment_len_mode = mode; |
| 4844 |
return 1; |
| 4845 |
} |
| 4846 |
|
| 4847 |
uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session) |
| 4848 |
{ |
| 4849 |
if (session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED) |
| 4850 |
return TLSEXT_max_fragment_length_DISABLED; |
| 4851 |
return session->ext.max_fragment_len_mode; |
| 4852 |
} |
| 4853 |
|
| 4854 |
/* |
| 4855 |
* Helper functions for HMAC access with legacy support included. |
| 4856 |
*/ |
| 4857 |
SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx) |
| 4858 |
{ |
| 4859 |
SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret)); |
| 4860 |
EVP_MAC *mac = NULL; |
| 4861 |
|
| 4862 |
if (ret == NULL) |
| 4863 |
return NULL; |
| 4864 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 4865 |
if (ctx->ext.ticket_key_evp_cb == NULL |
| 4866 |
&& ctx->ext.ticket_key_cb != NULL) { |
| 4867 |
if (!ssl_hmac_old_new(ret)) |
| 4868 |
goto err; |
| 4869 |
return ret; |
| 4870 |
} |
| 4871 |
#endif |
| 4872 |
mac = EVP_MAC_fetch(ctx->libctx, "HMAC", ctx->propq); |
| 4873 |
if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL) |
| 4874 |
goto err; |
| 4875 |
EVP_MAC_free(mac); |
| 4876 |
return ret; |
| 4877 |
err: |
| 4878 |
EVP_MAC_CTX_free(ret->ctx); |
| 4879 |
EVP_MAC_free(mac); |
| 4880 |
OPENSSL_free(ret); |
| 4881 |
return NULL; |
| 4882 |
} |
| 4883 |
|
| 4884 |
void ssl_hmac_free(SSL_HMAC *ctx) |
| 4885 |
{ |
| 4886 |
if (ctx != NULL) { |
| 4887 |
EVP_MAC_CTX_free(ctx->ctx); |
| 4888 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 4889 |
ssl_hmac_old_free(ctx); |
| 4890 |
#endif |
| 4891 |
OPENSSL_free(ctx); |
| 4892 |
} |
| 4893 |
} |
| 4894 |
|
| 4895 |
EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx) |
| 4896 |
{ |
| 4897 |
return ctx->ctx; |
| 4898 |
} |
| 4899 |
|
| 4900 |
int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md) |
| 4901 |
{ |
| 4902 |
OSSL_PARAM params[2], *p = params; |
| 4903 |
|
| 4904 |
if (ctx->ctx != NULL) { |
| 4905 |
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0); |
| 4906 |
*p = OSSL_PARAM_construct_end(); |
| 4907 |
if (EVP_MAC_init(ctx->ctx, key, len, params)) |
| 4908 |
return 1; |
| 4909 |
} |
| 4910 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 4911 |
if (ctx->old_ctx != NULL) |
| 4912 |
return ssl_hmac_old_init(ctx, key, len, md); |
| 4913 |
#endif |
| 4914 |
return 0; |
| 4915 |
} |
| 4916 |
|
| 4917 |
int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len) |
| 4918 |
{ |
| 4919 |
if (ctx->ctx != NULL) |
| 4920 |
return EVP_MAC_update(ctx->ctx, data, len); |
| 4921 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 4922 |
if (ctx->old_ctx != NULL) |
| 4923 |
return ssl_hmac_old_update(ctx, data, len); |
| 4924 |
#endif |
| 4925 |
return 0; |
| 4926 |
} |
| 4927 |
|
| 4928 |
int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len, |
| 4929 |
size_t max_size) |
| 4930 |
{ |
| 4931 |
if (ctx->ctx != NULL) |
| 4932 |
return EVP_MAC_final(ctx->ctx, md, len, max_size); |
| 4933 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 4934 |
if (ctx->old_ctx != NULL) |
| 4935 |
return ssl_hmac_old_final(ctx, md, len); |
| 4936 |
#endif |
| 4937 |
return 0; |
| 4938 |
} |
| 4939 |
|
| 4940 |
size_t ssl_hmac_size(const SSL_HMAC *ctx) |
| 4941 |
{ |
| 4942 |
if (ctx->ctx != NULL) |
| 4943 |
return EVP_MAC_CTX_get_mac_size(ctx->ctx); |
| 4944 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 4945 |
if (ctx->old_ctx != NULL) |
| 4946 |
return ssl_hmac_old_size(ctx); |
| 4947 |
#endif |
| 4948 |
return 0; |
| 4949 |
} |
| 4950 |
|
| 4951 |
int ssl_get_EC_curve_nid(const EVP_PKEY *pkey) |
| 4952 |
{ |
| 4953 |
char gname[OSSL_MAX_NAME_SIZE]; |
| 4954 |
|
| 4955 |
if (EVP_PKEY_get_group_name(pkey, gname, sizeof(gname), NULL) > 0) |
| 4956 |
return OBJ_txt2nid(gname); |
| 4957 |
|
| 4958 |
return NID_undef; |
| 4959 |
} |
| 4960 |
|
| 4961 |
__owur int tls13_set_encoded_pub_key(EVP_PKEY *pkey, |
| 4962 |
const unsigned char *enckey, |
| 4963 |
size_t enckeylen) |
| 4964 |
{ |
| 4965 |
if (EVP_PKEY_is_a(pkey, "DH")) { |
| 4966 |
int bits = EVP_PKEY_get_bits(pkey); |
| 4967 |
|
| 4968 |
if (bits <= 0 || enckeylen != (size_t)bits / 8) |
| 4969 |
/* the encoded key must be padded to the length of the p */ |
| 4970 |
return 0; |
| 4971 |
} else if (EVP_PKEY_is_a(pkey, "EC")) { |
| 4972 |
if (enckeylen < 3 /* point format and at least 1 byte for x and y */ |
| 4973 |
|| enckey[0] != 0x04) |
| 4974 |
return 0; |
| 4975 |
} |
| 4976 |
|
| 4977 |
return EVP_PKEY_set1_encoded_public_key(pkey, enckey, enckeylen); |
| 4978 |
} |