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