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