| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/ssl.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 |
| 20 | #include <algorithm> |
| 21 |
| 22 | #include <openssl/ec.h> |
| 23 | #include <openssl/ec_key.h> |
| 24 | #include <openssl/err.h> |
| 25 | #include <openssl/evp.h> |
| 26 | #include <openssl/mem.h> |
| 27 | #include <openssl/span.h> |
| 28 |
| 29 | #include "../crypto/internal.h" |
| 30 | #include "internal.h" |
| 31 |
| 32 |
| 33 | BSSL_NAMESPACE_BEGIN |
| 34 |
| 35 | bool ssl_is_key_type_supported(int key_type) { |
| 36 | return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC || |
| 37 | key_type == EVP_PKEY_ED25519; |
| 38 | } |
| 39 |
| 40 | typedef struct { |
| 41 | uint16_t sigalg; |
| 42 | int pkey_type; |
| 43 | int curve; |
| 44 | const EVP_MD *(*digest_func)(void); |
| 45 | bool is_rsa_pss; |
| 46 | bool tls12_ok; |
| 47 | bool tls13_ok; |
| 48 | bool client_only; |
| 49 | } SSL_SIGNATURE_ALGORITHM; |
| 50 |
| 51 | static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = { |
| 52 | // PKCS#1 v1.5 code points are only allowed in TLS 1.2. |
| 53 | {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, |
| 54 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/false, |
| 55 | /*client_only=*/false}, |
| 56 | {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, |
| 57 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/false, |
| 58 | /*client_only=*/false}, |
| 59 | {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, |
| 60 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/false, |
| 61 | /*client_only=*/false}, |
| 62 | {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, |
| 63 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/false, |
| 64 | /*client_only=*/false}, |
| 65 | {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, |
| 66 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/false, |
| 67 | /*client_only=*/false}, |
| 68 |
| 69 | // Legacy PKCS#1 v1.5 code points are only allowed in TLS 1.3 and |
| 70 | // client-only. See draft-ietf-tls-tls13-pkcs1-00. |
| 71 | {SSL_SIGN_RSA_PKCS1_SHA256_LEGACY, EVP_PKEY_RSA, NID_undef, &EVP_sha256, |
| 72 | /*is_rsa_pss=*/false, /*tls12_ok=*/false, /*tls13_ok=*/true, |
| 73 | /*client_only=*/true}, |
| 74 |
| 75 | {SSL_SIGN_RSA_PSS_RSAE_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, |
| 76 | /*is_rsa_pss=*/true, /*tls12_ok=*/true, /*tls13_ok=*/true, |
| 77 | /*client_only=*/false}, |
| 78 | {SSL_SIGN_RSA_PSS_RSAE_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, |
| 79 | /*is_rsa_pss=*/true, /*tls12_ok=*/true, /*tls13_ok=*/true, |
| 80 | /*client_only=*/false}, |
| 81 | {SSL_SIGN_RSA_PSS_RSAE_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, |
| 82 | /*is_rsa_pss=*/true, /*tls12_ok=*/true, /*tls13_ok=*/true, |
| 83 | /*client_only=*/false}, |
| 84 |
| 85 | {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, |
| 86 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/false, |
| 87 | /*client_only=*/false}, |
| 88 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1, |
| 89 | &EVP_sha256, /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/true, |
| 90 | /*client_only=*/false}, |
| 91 | {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384, |
| 92 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/true, |
| 93 | /*client_only=*/false}, |
| 94 | {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512, |
| 95 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/true, |
| 96 | /*client_only=*/false}, |
| 97 |
| 98 | {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, nullptr, |
| 99 | /*is_rsa_pss=*/false, /*tls12_ok=*/true, /*tls13_ok=*/true, |
| 100 | /*client_only=*/false}, |
| 101 | }; |
| 102 |
| 103 | static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) { |
| 104 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) { |
| 105 | if (kSignatureAlgorithms[i].sigalg == sigalg) { |
| 106 | return &kSignatureAlgorithms[i]; |
| 107 | } |
| 108 | } |
| 109 | return NULL; |
| 110 | } |
| 111 |
| 112 | bool ssl_pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey, |
| 113 | uint16_t sigalg, bool is_verify) { |
| 114 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 115 | if (alg == NULL || EVP_PKEY_id(pkey) != alg->pkey_type) { |
| 116 | return false; |
| 117 | } |
| 118 |
| 119 | // Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that |
| 120 | // emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the |
| 121 | // hash in TLS. Reasonable RSA key sizes are large enough for the largest |
| 122 | // defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for |
| 123 | // SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the |
| 124 | // size so that we can fall back to another algorithm in that case. |
| 125 | if (alg->is_rsa_pss && |
| 126 | (size_t)EVP_PKEY_size(pkey) < 2 * EVP_MD_size(alg->digest_func()) + 2) { |
| 127 | return false; |
| 128 | } |
| 129 |
| 130 | if (ssl_protocol_version(ssl) < TLS1_2_VERSION) { |
| 131 | // TLS 1.0 and 1.1 do not negotiate algorithms and always sign one of two |
| 132 | // hardcoded algorithms. |
| 133 | return sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 || |
| 134 | sigalg == SSL_SIGN_ECDSA_SHA1; |
| 135 | } |
| 136 |
| 137 | // |SSL_SIGN_RSA_PKCS1_MD5_SHA1| is not a real SignatureScheme for TLS 1.2 and |
| 138 | // higher. It is an internal value we use to represent TLS 1.0/1.1's MD5/SHA1 |
| 139 | // concatenation. |
| 140 | if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1) { |
| 141 | return false; |
| 142 | } |
| 143 |
| 144 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 145 | if (!alg->tls13_ok) { |
| 146 | return false; |
| 147 | } |
| 148 |
| 149 | bool is_client_sign = ssl->server == is_verify; |
| 150 | if (alg->client_only && !is_client_sign) { |
| 151 | return false; |
| 152 | } |
| 153 |
| 154 | // EC keys have a curve requirement. |
| 155 | if (alg->pkey_type == EVP_PKEY_EC && |
| 156 | (alg->curve == NID_undef || |
| 157 | EC_GROUP_get_curve_name( |
| 158 | EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) { |
| 159 | return false; |
| 160 | } |
| 161 | } else if (!alg->tls12_ok) { |
| 162 | return false; |
| 163 | } |
| 164 |
| 165 | return true; |
| 166 | } |
| 167 |
| 168 | static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, |
| 169 | uint16_t sigalg, bool is_verify) { |
| 170 | if (!ssl_pkey_supports_algorithm(ssl, pkey, sigalg, is_verify)) { |
| 171 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 172 | return false; |
| 173 | } |
| 174 |
| 175 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 176 | const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL; |
| 177 | EVP_PKEY_CTX *pctx; |
| 178 | if (is_verify) { |
| 179 | if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) { |
| 180 | return false; |
| 181 | } |
| 182 | } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) { |
| 183 | return false; |
| 184 | } |
| 185 |
| 186 | if (alg->is_rsa_pss) { |
| 187 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 188 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) { |
| 189 | return false; |
| 190 | } |
| 191 | } |
| 192 |
| 193 | return true; |
| 194 | } |
| 195 |
| 196 | enum ssl_private_key_result_t ssl_private_key_sign( |
| 197 | SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
| 198 | uint16_t sigalg, Span<const uint8_t> in) { |
| 199 | SSL *const ssl = hs->ssl; |
| 200 | const SSL_CREDENTIAL *const cred = hs->credential.get(); |
| 201 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 202 | Array<uint8_t> spki; |
| 203 | if (hints) { |
| 204 | ScopedCBB spki_cbb; |
| 205 | if (!CBB_init(spki_cbb.get(), 64) || |
| 206 | !EVP_marshal_public_key(spki_cbb.get(), cred->pubkey.get()) || |
| 207 | !CBBFinishArray(spki_cbb.get(), &spki)) { |
| 208 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 209 | return ssl_private_key_failure; |
| 210 | } |
| 211 | } |
| 212 |
| 213 | // Replay the signature from handshake hints if available. |
| 214 | if (hints && !hs->hints_requested && // |
| 215 | sigalg == hints->signature_algorithm && // |
| 216 | in == hints->signature_input && // |
| 217 | Span(spki) == hints->signature_spki && // |
| 218 | !hints->signature.empty() && // |
| 219 | hints->signature.size() <= max_out) { |
| 220 | // Signature algorithm and input both match. Reuse the signature from hints. |
| 221 | *out_len = hints->signature.size(); |
| 222 | OPENSSL_memcpy(out, hints->signature.data(), hints->signature.size()); |
| 223 | return ssl_private_key_success; |
| 224 | } |
| 225 |
| 226 | const SSL_PRIVATE_KEY_METHOD *key_method = cred->key_method; |
| 227 | EVP_PKEY *privkey = cred->privkey.get(); |
| 228 | assert(!hs->can_release_private_key); |
| 229 |
| 230 | if (key_method != NULL) { |
| 231 | enum ssl_private_key_result_t ret; |
| 232 | if (hs->pending_private_key_op) { |
| 233 | ret = key_method->complete(ssl, out, out_len, max_out); |
| 234 | } else { |
| 235 | ret = key_method->sign(ssl, out, out_len, max_out, sigalg, in.data(), |
| 236 | in.size()); |
| 237 | } |
| 238 | if (ret == ssl_private_key_failure) { |
| 239 | OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED); |
| 240 | } |
| 241 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
| 242 | if (ret != ssl_private_key_success) { |
| 243 | return ret; |
| 244 | } |
| 245 | } else { |
| 246 | *out_len = max_out; |
| 247 | ScopedEVP_MD_CTX ctx; |
| 248 | if (!setup_ctx(ssl, ctx.get(), privkey, sigalg, false /* sign */) || |
| 249 | !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) { |
| 250 | return ssl_private_key_failure; |
| 251 | } |
| 252 | } |
| 253 |
| 254 | // Save the hint if applicable. |
| 255 | if (hints && hs->hints_requested) { |
| 256 | hints->signature_algorithm = sigalg; |
| 257 | hints->signature_spki = std::move(spki); |
| 258 | if (!hints->signature_input.CopyFrom(in) || |
| 259 | !hints->signature.CopyFrom(Span(out, *out_len))) { |
| 260 | return ssl_private_key_failure; |
| 261 | } |
| 262 | } |
| 263 | return ssl_private_key_success; |
| 264 | } |
| 265 |
| 266 | bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature, |
| 267 | uint16_t sigalg, EVP_PKEY *pkey, |
| 268 | Span<const uint8_t> in) { |
| 269 | ScopedEVP_MD_CTX ctx; |
| 270 | if (!setup_ctx(ssl, ctx.get(), pkey, sigalg, true /* verify */)) { |
| 271 | return false; |
| 272 | } |
| 273 | bool ok = EVP_DigestVerify(ctx.get(), signature.data(), signature.size(), |
| 274 | in.data(), in.size()); |
| 275 | if (CRYPTO_fuzzer_mode_enabled()) { |
| 276 | ok = true; |
| 277 | ERR_clear_error(); |
| 278 | } |
| 279 | return ok; |
| 280 | } |
| 281 |
| 282 | enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs, |
| 283 | uint8_t *out, |
| 284 | size_t *out_len, |
| 285 | size_t max_out, |
| 286 | Span<const uint8_t> in) { |
| 287 | SSL *const ssl = hs->ssl; |
| 288 | const SSL_CREDENTIAL *const cred = hs->credential.get(); |
| 289 | assert(!hs->can_release_private_key); |
| 290 | if (cred->key_method != NULL) { |
| 291 | enum ssl_private_key_result_t ret; |
| 292 | if (hs->pending_private_key_op) { |
| 293 | ret = cred->key_method->complete(ssl, out, out_len, max_out); |
| 294 | } else { |
| 295 | ret = cred->key_method->decrypt(ssl, out, out_len, max_out, in.data(), |
| 296 | in.size()); |
| 297 | } |
| 298 | if (ret == ssl_private_key_failure) { |
| 299 | OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED); |
| 300 | } |
| 301 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
| 302 | return ret; |
| 303 | } |
| 304 |
| 305 | RSA *rsa = EVP_PKEY_get0_RSA(cred->privkey.get()); |
| 306 | if (rsa == NULL) { |
| 307 | // Decrypt operations are only supported for RSA keys. |
| 308 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 309 | return ssl_private_key_failure; |
| 310 | } |
| 311 |
| 312 | // Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 313 | // timing-sensitive code by the caller. |
| 314 | if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(), |
| 315 | RSA_NO_PADDING)) { |
| 316 | return ssl_private_key_failure; |
| 317 | } |
| 318 | return ssl_private_key_success; |
| 319 | } |
| 320 |
| 321 | BSSL_NAMESPACE_END |
| 322 |
| 323 | using namespace bssl; |
| 324 |
| 325 | int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) { |
| 326 | if (rsa == NULL || ssl->config == NULL) { |
| 327 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 328 | return 0; |
| 329 | } |
| 330 |
| 331 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 332 | if (!pkey || // |
| 333 | !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
| 334 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
| 335 | return 0; |
| 336 | } |
| 337 |
| 338 | return SSL_use_PrivateKey(ssl, pkey.get()); |
| 339 | } |
| 340 |
| 341 | int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
| 342 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
| 343 | if (!rsa) { |
| 344 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 345 | return 0; |
| 346 | } |
| 347 |
| 348 | return SSL_use_RSAPrivateKey(ssl, rsa.get()); |
| 349 | } |
| 350 |
| 351 | int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) { |
| 352 | if (pkey == NULL || ssl->config == NULL) { |
| 353 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 354 | return 0; |
| 355 | } |
| 356 |
| 357 | return SSL_CREDENTIAL_set1_private_key( |
| 358 | ssl->config->cert->legacy_credential.get(), pkey); |
| 359 | } |
| 360 |
| 361 | int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der, |
| 362 | size_t der_len) { |
| 363 | if (der_len > LONG_MAX) { |
| 364 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 365 | return 0; |
| 366 | } |
| 367 |
| 368 | const uint8_t *p = der; |
| 369 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
| 370 | if (!pkey || p != der + der_len) { |
| 371 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 372 | return 0; |
| 373 | } |
| 374 |
| 375 | return SSL_use_PrivateKey(ssl, pkey.get()); |
| 376 | } |
| 377 |
| 378 | int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) { |
| 379 | if (rsa == NULL) { |
| 380 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 381 | return 0; |
| 382 | } |
| 383 |
| 384 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 385 | if (!pkey || !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
| 386 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
| 387 | return 0; |
| 388 | } |
| 389 |
| 390 | return SSL_CTX_use_PrivateKey(ctx, pkey.get()); |
| 391 | } |
| 392 |
| 393 | int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der, |
| 394 | size_t der_len) { |
| 395 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
| 396 | if (!rsa) { |
| 397 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 398 | return 0; |
| 399 | } |
| 400 |
| 401 | return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get()); |
| 402 | } |
| 403 |
| 404 | int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) { |
| 405 | if (pkey == NULL) { |
| 406 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 407 | return 0; |
| 408 | } |
| 409 |
| 410 | return SSL_CREDENTIAL_set1_private_key(ctx->cert->legacy_credential.get(), |
| 411 | pkey); |
| 412 | } |
| 413 |
| 414 | int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der, |
| 415 | size_t der_len) { |
| 416 | if (der_len > LONG_MAX) { |
| 417 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 418 | return 0; |
| 419 | } |
| 420 |
| 421 | const uint8_t *p = der; |
| 422 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
| 423 | if (!pkey || p != der + der_len) { |
| 424 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
| 425 | return 0; |
| 426 | } |
| 427 |
| 428 | return SSL_CTX_use_PrivateKey(ctx, pkey.get()); |
| 429 | } |
| 430 |
| 431 | void SSL_set_private_key_method(SSL *ssl, |
| 432 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 433 | if (!ssl->config) { |
| 434 | return; |
| 435 | } |
| 436 | BSSL_CHECK(SSL_CREDENTIAL_set_private_key_method( |
| 437 | ssl->config->cert->legacy_credential.get(), key_method)); |
| 438 | } |
| 439 |
| 440 | void SSL_CTX_set_private_key_method(SSL_CTX *ctx, |
| 441 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
| 442 | BSSL_CHECK(SSL_CREDENTIAL_set_private_key_method( |
| 443 | ctx->cert->legacy_credential.get(), key_method)); |
| 444 | } |
| 445 |
| 446 | static constexpr size_t kMaxSignatureAlgorithmNameLen = 24; |
| 447 |
| 448 | struct SignatureAlgorithmName { |
| 449 | uint16_t signature_algorithm; |
| 450 | const char name[kMaxSignatureAlgorithmNameLen]; |
| 451 | }; |
| 452 |
| 453 | // This was "constexpr" rather than "const", but that triggered a bug in MSVC |
| 454 | // where it didn't pad the strings to the correct length. |
| 455 | static const SignatureAlgorithmName kSignatureAlgorithmNames[] = { |
| 456 | {SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1"}, |
| 457 | {SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1"}, |
| 458 | {SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256"}, |
| 459 | {SSL_SIGN_RSA_PKCS1_SHA256_LEGACY, "rsa_pkcs1_sha256_legacy"}, |
| 460 | {SSL_SIGN_RSA_PKCS1_SHA384, "rsa_pkcs1_sha384"}, |
| 461 | {SSL_SIGN_RSA_PKCS1_SHA512, "rsa_pkcs1_sha512"}, |
| 462 | {SSL_SIGN_ECDSA_SHA1, "ecdsa_sha1"}, |
| 463 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, "ecdsa_secp256r1_sha256"}, |
| 464 | {SSL_SIGN_ECDSA_SECP384R1_SHA384, "ecdsa_secp384r1_sha384"}, |
| 465 | {SSL_SIGN_ECDSA_SECP521R1_SHA512, "ecdsa_secp521r1_sha512"}, |
| 466 | {SSL_SIGN_RSA_PSS_RSAE_SHA256, "rsa_pss_rsae_sha256"}, |
| 467 | {SSL_SIGN_RSA_PSS_RSAE_SHA384, "rsa_pss_rsae_sha384"}, |
| 468 | {SSL_SIGN_RSA_PSS_RSAE_SHA512, "rsa_pss_rsae_sha512"}, |
| 469 | {SSL_SIGN_ED25519, "ed25519"}, |
| 470 | }; |
| 471 |
| 472 | const char *SSL_get_signature_algorithm_name(uint16_t sigalg, |
| 473 | int include_curve) { |
| 474 | if (!include_curve) { |
| 475 | switch (sigalg) { |
| 476 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 477 | return "ecdsa_sha256"; |
| 478 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 479 | return "ecdsa_sha384"; |
| 480 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 481 | return "ecdsa_sha512"; |
| 482 | // If adding more here, also update |
| 483 | // |SSL_get_all_signature_algorithm_names|. |
| 484 | } |
| 485 | } |
| 486 |
| 487 | for (const auto &candidate : kSignatureAlgorithmNames) { |
| 488 | if (candidate.signature_algorithm == sigalg) { |
| 489 | return candidate.name; |
| 490 | } |
| 491 | } |
| 492 |
| 493 | return NULL; |
| 494 | } |
| 495 |
| 496 | size_t SSL_get_all_signature_algorithm_names(const char **out, size_t max_out) { |
| 497 | const char *kPredefinedNames[] = {"ecdsa_sha256", "ecdsa_sha384", |
| 498 | "ecdsa_sha512"}; |
| 499 | return GetAllNames(out, max_out, kPredefinedNames, |
| 500 | &SignatureAlgorithmName::name, |
| 501 | Span(kSignatureAlgorithmNames)); |
| 502 | } |
| 503 |
| 504 | int SSL_get_signature_algorithm_key_type(uint16_t sigalg) { |
| 505 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 506 | return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE; |
| 507 | } |
| 508 |
| 509 | const EVP_MD *SSL_get_signature_algorithm_digest(uint16_t sigalg) { |
| 510 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 511 | if (alg == nullptr || alg->digest_func == nullptr) { |
| 512 | return nullptr; |
| 513 | } |
| 514 | return alg->digest_func(); |
| 515 | } |
| 516 |
| 517 | int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg) { |
| 518 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
| 519 | return alg != nullptr && alg->is_rsa_pss; |
| 520 | } |
| 521 |
| 522 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 523 | uint16_t u1 = *((const uint16_t *)p1); |
| 524 | uint16_t u2 = *((const uint16_t *)p2); |
| 525 | if (u1 < u2) { |
| 526 | return -1; |
| 527 | } else if (u1 > u2) { |
| 528 | return 1; |
| 529 | } else { |
| 530 | return 0; |
| 531 | } |
| 532 | } |
| 533 |
| 534 | static bool sigalgs_unique(Span<const uint16_t> in_sigalgs) { |
| 535 | if (in_sigalgs.size() < 2) { |
| 536 | return true; |
| 537 | } |
| 538 |
| 539 | Array<uint16_t> sigalgs; |
| 540 | if (!sigalgs.CopyFrom(in_sigalgs)) { |
| 541 | return false; |
| 542 | } |
| 543 |
| 544 | qsort(sigalgs.data(), sigalgs.size(), sizeof(uint16_t), compare_uint16_t); |
| 545 |
| 546 | for (size_t i = 1; i < sigalgs.size(); i++) { |
| 547 | if (sigalgs[i - 1] == sigalgs[i]) { |
| 548 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_SIGNATURE_ALGORITHM); |
| 549 | return false; |
| 550 | } |
| 551 | } |
| 552 |
| 553 | return true; |
| 554 | } |
| 555 |
| 556 | static bool set_sigalg_prefs(Array<uint16_t> *out, Span<const uint16_t> prefs) { |
| 557 | if (!sigalgs_unique(prefs)) { |
| 558 | return false; |
| 559 | } |
| 560 |
| 561 | // Check for invalid algorithms, and filter out |SSL_SIGN_RSA_PKCS1_MD5_SHA1|. |
| 562 | Array<uint16_t> filtered; |
| 563 | if (!filtered.InitForOverwrite(prefs.size())) { |
| 564 | return false; |
| 565 | } |
| 566 | size_t added = 0; |
| 567 | for (uint16_t pref : prefs) { |
| 568 | if (pref == SSL_SIGN_RSA_PKCS1_MD5_SHA1) { |
| 569 | // Though not intended to be used with this API, we treat |
| 570 | // |SSL_SIGN_RSA_PKCS1_MD5_SHA1| as a real signature algorithm in |
| 571 | // |SSL_PRIVATE_KEY_METHOD|. Not accepting it here makes for a confusing |
| 572 | // abstraction. |
| 573 | continue; |
| 574 | } |
| 575 | if (get_signature_algorithm(pref) == nullptr) { |
| 576 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 577 | return false; |
| 578 | } |
| 579 | filtered[added] = pref; |
| 580 | added++; |
| 581 | } |
| 582 | filtered.Shrink(added); |
| 583 |
| 584 | // This can happen if |prefs| contained only |SSL_SIGN_RSA_PKCS1_MD5_SHA1|. |
| 585 | // Leaving it empty would revert to the default, so treat this as an error |
| 586 | // condition. |
| 587 | if (!prefs.empty() && filtered.empty()) { |
| 588 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 589 | return false; |
| 590 | } |
| 591 |
| 592 | *out = std::move(filtered); |
| 593 | return true; |
| 594 | } |
| 595 |
| 596 | int SSL_CREDENTIAL_set1_signing_algorithm_prefs(SSL_CREDENTIAL *cred, |
| 597 | const uint16_t *prefs, |
| 598 | size_t num_prefs) { |
| 599 | if (!cred->UsesPrivateKey()) { |
| 600 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 601 | return 0; |
| 602 | } |
| 603 |
| 604 | // Delegated credentials are constrained to a single algorithm, so there is no |
| 605 | // need to configure this. |
| 606 | if (cred->type == SSLCredentialType::kDelegated) { |
| 607 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 608 | return 0; |
| 609 | } |
| 610 |
| 611 | return set_sigalg_prefs(&cred->sigalgs, Span(prefs, num_prefs)); |
| 612 | } |
| 613 |
| 614 | int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
| 615 | size_t num_prefs) { |
| 616 | return SSL_CREDENTIAL_set1_signing_algorithm_prefs( |
| 617 | ctx->cert->legacy_credential.get(), prefs, num_prefs); |
| 618 | } |
| 619 |
| 620 | int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs, |
| 621 | size_t num_prefs) { |
| 622 | if (!ssl->config) { |
| 623 | return 0; |
| 624 | } |
| 625 | return SSL_CREDENTIAL_set1_signing_algorithm_prefs( |
| 626 | ssl->config->cert->legacy_credential.get(), prefs, num_prefs); |
| 627 | } |
| 628 |
| 629 | static constexpr struct { |
| 630 | int pkey_type; |
| 631 | int hash_nid; |
| 632 | uint16_t signature_algorithm; |
| 633 | } kSignatureAlgorithmsMapping[] = { |
| 634 | {EVP_PKEY_RSA, NID_sha1, SSL_SIGN_RSA_PKCS1_SHA1}, |
| 635 | {EVP_PKEY_RSA, NID_sha256, SSL_SIGN_RSA_PKCS1_SHA256}, |
| 636 | {EVP_PKEY_RSA, NID_sha384, SSL_SIGN_RSA_PKCS1_SHA384}, |
| 637 | {EVP_PKEY_RSA, NID_sha512, SSL_SIGN_RSA_PKCS1_SHA512}, |
| 638 | {EVP_PKEY_RSA_PSS, NID_sha256, SSL_SIGN_RSA_PSS_RSAE_SHA256}, |
| 639 | {EVP_PKEY_RSA_PSS, NID_sha384, SSL_SIGN_RSA_PSS_RSAE_SHA384}, |
| 640 | {EVP_PKEY_RSA_PSS, NID_sha512, SSL_SIGN_RSA_PSS_RSAE_SHA512}, |
| 641 | {EVP_PKEY_EC, NID_sha1, SSL_SIGN_ECDSA_SHA1}, |
| 642 | {EVP_PKEY_EC, NID_sha256, SSL_SIGN_ECDSA_SECP256R1_SHA256}, |
| 643 | {EVP_PKEY_EC, NID_sha384, SSL_SIGN_ECDSA_SECP384R1_SHA384}, |
| 644 | {EVP_PKEY_EC, NID_sha512, SSL_SIGN_ECDSA_SECP521R1_SHA512}, |
| 645 | {EVP_PKEY_ED25519, NID_undef, SSL_SIGN_ED25519}, |
| 646 | }; |
| 647 |
| 648 | static bool parse_sigalg_pairs(Array<uint16_t> *out, const int *values, |
| 649 | size_t num_values) { |
| 650 | if ((num_values & 1) == 1) { |
| 651 | return false; |
| 652 | } |
| 653 |
| 654 | const size_t num_pairs = num_values / 2; |
| 655 | if (!out->InitForOverwrite(num_pairs)) { |
| 656 | return false; |
| 657 | } |
| 658 |
| 659 | for (size_t i = 0; i < num_values; i += 2) { |
| 660 | const int hash_nid = values[i]; |
| 661 | const int pkey_type = values[i + 1]; |
| 662 |
| 663 | bool found = false; |
| 664 | for (const auto &candidate : kSignatureAlgorithmsMapping) { |
| 665 | if (candidate.pkey_type == pkey_type && candidate.hash_nid == hash_nid) { |
| 666 | (*out)[i / 2] = candidate.signature_algorithm; |
| 667 | found = true; |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 |
| 672 | if (!found) { |
| 673 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 674 | ERR_add_error_dataf("unknown hash:%d pkey:%d", hash_nid, pkey_type); |
| 675 | return false; |
| 676 | } |
| 677 | } |
| 678 |
| 679 | return true; |
| 680 | } |
| 681 |
| 682 | int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values) { |
| 683 | Array<uint16_t> sigalgs; |
| 684 | if (!parse_sigalg_pairs(&sigalgs, values, num_values)) { |
| 685 | return 0; |
| 686 | } |
| 687 |
| 688 | if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(), |
| 689 | sigalgs.size()) || |
| 690 | !SSL_CTX_set_verify_algorithm_prefs(ctx, sigalgs.data(), |
| 691 | sigalgs.size())) { |
| 692 | return 0; |
| 693 | } |
| 694 |
| 695 | return 1; |
| 696 | } |
| 697 |
| 698 | int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values) { |
| 699 | if (!ssl->config) { |
| 700 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 701 | return 0; |
| 702 | } |
| 703 |
| 704 | Array<uint16_t> sigalgs; |
| 705 | if (!parse_sigalg_pairs(&sigalgs, values, num_values)) { |
| 706 | return 0; |
| 707 | } |
| 708 |
| 709 | if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) || |
| 710 | !SSL_set_verify_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size())) { |
| 711 | return 0; |
| 712 | } |
| 713 |
| 714 | return 1; |
| 715 | } |
| 716 |
| 717 | static bool parse_sigalgs_list(Array<uint16_t> *out, const char *str) { |
| 718 | // str looks like "RSA+SHA1:ECDSA+SHA256:ecdsa_secp256r1_sha256". |
| 719 |
| 720 | // Count colons to give the number of output elements from any successful |
| 721 | // parse. |
| 722 | size_t num_elements = 1; |
| 723 | size_t len = 0; |
| 724 | for (const char *p = str; *p; p++) { |
| 725 | len++; |
| 726 | if (*p == ':') { |
| 727 | num_elements++; |
| 728 | } |
| 729 | } |
| 730 |
| 731 | if (!out->InitForOverwrite(num_elements)) { |
| 732 | return false; |
| 733 | } |
| 734 | size_t out_i = 0; |
| 735 |
| 736 | enum { |
| 737 | pkey_or_name, |
| 738 | hash_name, |
| 739 | } state = pkey_or_name; |
| 740 |
| 741 | char buf[kMaxSignatureAlgorithmNameLen]; |
| 742 | // buf_used is always < sizeof(buf). I.e. it's always safe to write |
| 743 | // buf[buf_used] = 0. |
| 744 | size_t buf_used = 0; |
| 745 |
| 746 | int pkey_type = 0, hash_nid = 0; |
| 747 |
| 748 | // Note that the loop runs to len+1, i.e. it'll process the terminating NUL. |
| 749 | for (size_t offset = 0; offset < len + 1; offset++) { |
| 750 | const unsigned char c = str[offset]; |
| 751 |
| 752 | switch (c) { |
| 753 | case '+': |
| 754 | if (state == hash_name) { |
| 755 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 756 | ERR_add_error_dataf("+ found in hash name at offset %zu", offset); |
| 757 | return false; |
| 758 | } |
| 759 | if (buf_used == 0) { |
| 760 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 761 | ERR_add_error_dataf("empty public key type at offset %zu", offset); |
| 762 | return false; |
| 763 | } |
| 764 | buf[buf_used] = 0; |
| 765 |
| 766 | if (strcmp(buf, "RSA") == 0) { |
| 767 | pkey_type = EVP_PKEY_RSA; |
| 768 | } else if (strcmp(buf, "RSA-PSS") == 0 || // |
| 769 | strcmp(buf, "PSS") == 0) { |
| 770 | pkey_type = EVP_PKEY_RSA_PSS; |
| 771 | } else if (strcmp(buf, "ECDSA") == 0) { |
| 772 | pkey_type = EVP_PKEY_EC; |
| 773 | } else { |
| 774 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 775 | ERR_add_error_dataf("unknown public key type '%s'", buf); |
| 776 | return false; |
| 777 | } |
| 778 |
| 779 | state = hash_name; |
| 780 | buf_used = 0; |
| 781 | break; |
| 782 |
| 783 | case ':': |
| 784 | [[fallthrough]]; |
| 785 | case 0: |
| 786 | if (buf_used == 0) { |
| 787 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 788 | ERR_add_error_dataf("empty element at offset %zu", offset); |
| 789 | return false; |
| 790 | } |
| 791 |
| 792 | buf[buf_used] = 0; |
| 793 |
| 794 | if (state == pkey_or_name) { |
| 795 | // No '+' was seen thus this is a TLS 1.3-style name. |
| 796 | bool found = false; |
| 797 | for (const auto &candidate : kSignatureAlgorithmNames) { |
| 798 | if (strcmp(candidate.name, buf) == 0) { |
| 799 | assert(out_i < num_elements); |
| 800 | (*out)[out_i++] = candidate.signature_algorithm; |
| 801 | found = true; |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 |
| 806 | if (!found) { |
| 807 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 808 | ERR_add_error_dataf("unknown signature algorithm '%s'", buf); |
| 809 | return false; |
| 810 | } |
| 811 | } else { |
| 812 | if (strcmp(buf, "SHA1") == 0) { |
| 813 | hash_nid = NID_sha1; |
| 814 | } else if (strcmp(buf, "SHA256") == 0) { |
| 815 | hash_nid = NID_sha256; |
| 816 | } else if (strcmp(buf, "SHA384") == 0) { |
| 817 | hash_nid = NID_sha384; |
| 818 | } else if (strcmp(buf, "SHA512") == 0) { |
| 819 | hash_nid = NID_sha512; |
| 820 | } else { |
| 821 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 822 | ERR_add_error_dataf("unknown hash function '%s'", buf); |
| 823 | return false; |
| 824 | } |
| 825 |
| 826 | bool found = false; |
| 827 | for (const auto &candidate : kSignatureAlgorithmsMapping) { |
| 828 | if (candidate.pkey_type == pkey_type && |
| 829 | candidate.hash_nid == hash_nid) { |
| 830 | assert(out_i < num_elements); |
| 831 | (*out)[out_i++] = candidate.signature_algorithm; |
| 832 | found = true; |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 |
| 837 | if (!found) { |
| 838 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 839 | ERR_add_error_dataf("unknown pkey:%d hash:%s", pkey_type, buf); |
| 840 | return false; |
| 841 | } |
| 842 | } |
| 843 |
| 844 | state = pkey_or_name; |
| 845 | buf_used = 0; |
| 846 | break; |
| 847 |
| 848 | default: |
| 849 | if (buf_used == sizeof(buf) - 1) { |
| 850 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 851 | ERR_add_error_dataf("substring too long at offset %zu", offset); |
| 852 | return false; |
| 853 | } |
| 854 |
| 855 | if (OPENSSL_isalnum(c) || c == '-' || c == '_') { |
| 856 | buf[buf_used++] = c; |
| 857 | } else { |
| 858 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
| 859 | ERR_add_error_dataf("invalid character 0x%02x at offest %zu", c, |
| 860 | offset); |
| 861 | return false; |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 |
| 866 | assert(out_i == out->size()); |
| 867 | return true; |
| 868 | } |
| 869 |
| 870 | int SSL_CTX_set1_sigalgs_list(SSL_CTX *ctx, const char *str) { |
| 871 | Array<uint16_t> sigalgs; |
| 872 | if (!parse_sigalgs_list(&sigalgs, str)) { |
| 873 | return 0; |
| 874 | } |
| 875 |
| 876 | if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(), |
| 877 | sigalgs.size()) || |
| 878 | !SSL_CTX_set_verify_algorithm_prefs(ctx, sigalgs.data(), |
| 879 | sigalgs.size())) { |
| 880 | return 0; |
| 881 | } |
| 882 |
| 883 | return 1; |
| 884 | } |
| 885 |
| 886 | int SSL_set1_sigalgs_list(SSL *ssl, const char *str) { |
| 887 | if (!ssl->config) { |
| 888 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 889 | return 0; |
| 890 | } |
| 891 |
| 892 | Array<uint16_t> sigalgs; |
| 893 | if (!parse_sigalgs_list(&sigalgs, str)) { |
| 894 | return 0; |
| 895 | } |
| 896 |
| 897 | if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) || |
| 898 | !SSL_set_verify_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size())) { |
| 899 | return 0; |
| 900 | } |
| 901 |
| 902 | return 1; |
| 903 | } |
| 904 |
| 905 | int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
| 906 | size_t num_prefs) { |
| 907 | return set_sigalg_prefs(&ctx->verify_sigalgs, Span(prefs, num_prefs)); |
| 908 | } |
| 909 |
| 910 | int SSL_set_verify_algorithm_prefs(SSL *ssl, const uint16_t *prefs, |
| 911 | size_t num_prefs) { |
| 912 | if (!ssl->config) { |
| 913 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 914 | return 0; |
| 915 | } |
| 916 |
| 917 | return set_sigalg_prefs(&ssl->config->verify_sigalgs, Span(prefs, num_prefs)); |
| 918 | } |
| 919 |