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