1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | * All rights reserved. |
3 | * |
4 | * This package is an SSL implementation written |
5 | * by Eric Young (eay@cryptsoft.com). |
6 | * The implementation was written so as to conform with Netscapes SSL. |
7 | * |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions |
10 | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | * included with this distribution is covered by the same copyright terms |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | * |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. |
17 | * If this package is used in a product, Eric Young should be given attribution |
18 | * as the author of the parts of the library used. |
19 | * This can be in the form of a textual message at program startup or |
20 | * in documentation (online or textual) provided with the package. |
21 | * |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions |
24 | * are met: |
25 | * 1. Redistributions of source code must retain the copyright |
26 | * notice, this list of conditions and the following disclaimer. |
27 | * 2. Redistributions in binary form must reproduce the above copyright |
28 | * notice, this list of conditions and the following disclaimer in the |
29 | * documentation and/or other materials provided with the distribution. |
30 | * 3. All advertising materials mentioning features or use of this software |
31 | * must display the following acknowledgement: |
32 | * "This product includes cryptographic software written by |
33 | * Eric Young (eay@cryptsoft.com)" |
34 | * The word 'cryptographic' can be left out if the rouines from the library |
35 | * being used are not cryptographic related :-). |
36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | * |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | * SUCH DAMAGE. |
51 | * |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | * copied and put under another distribution licence |
55 | * [including the GNU Public Licence.] */ |
56 | |
57 | #include <openssl/ssl.h> |
58 | |
59 | #include <assert.h> |
60 | #include <limits.h> |
61 | |
62 | #include <openssl/ec.h> |
63 | #include <openssl/ec_key.h> |
64 | #include <openssl/err.h> |
65 | #include <openssl/evp.h> |
66 | #include <openssl/mem.h> |
67 | |
68 | #include "internal.h" |
69 | #include "../crypto/internal.h" |
70 | |
71 | |
72 | BSSL_NAMESPACE_BEGIN |
73 | |
74 | bool ssl_is_key_type_supported(int key_type) { |
75 | return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC || |
76 | key_type == EVP_PKEY_ED25519; |
77 | } |
78 | |
79 | static bool ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) { |
80 | if (!ssl_is_key_type_supported(pkey->type)) { |
81 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
82 | return false; |
83 | } |
84 | |
85 | if (cert->chain != nullptr && |
86 | sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) != nullptr && |
87 | // Sanity-check that the private key and the certificate match. |
88 | !ssl_cert_check_private_key(cert, pkey)) { |
89 | return false; |
90 | } |
91 | |
92 | cert->privatekey = UpRef(pkey); |
93 | return true; |
94 | } |
95 | |
96 | typedef struct { |
97 | uint16_t sigalg; |
98 | int pkey_type; |
99 | int curve; |
100 | const EVP_MD *(*digest_func)(void); |
101 | bool is_rsa_pss; |
102 | } SSL_SIGNATURE_ALGORITHM; |
103 | |
104 | static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = { |
105 | {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, |
106 | false}, |
107 | {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, false}, |
108 | {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, false}, |
109 | {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, false}, |
110 | {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, false}, |
111 | |
112 | {SSL_SIGN_RSA_PSS_RSAE_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, true}, |
113 | {SSL_SIGN_RSA_PSS_RSAE_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, true}, |
114 | {SSL_SIGN_RSA_PSS_RSAE_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, true}, |
115 | |
116 | {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, false}, |
117 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1, |
118 | &EVP_sha256, false}, |
119 | {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384, |
120 | false}, |
121 | {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512, |
122 | false}, |
123 | |
124 | {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, nullptr, false}, |
125 | }; |
126 | |
127 | static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) { |
128 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) { |
129 | if (kSignatureAlgorithms[i].sigalg == sigalg) { |
130 | return &kSignatureAlgorithms[i]; |
131 | } |
132 | } |
133 | return NULL; |
134 | } |
135 | |
136 | bool ssl_has_private_key(const SSL_HANDSHAKE *hs) { |
137 | if (hs->config->cert->privatekey != nullptr || |
138 | hs->config->cert->key_method != nullptr || |
139 | ssl_signing_with_dc(hs)) { |
140 | return true; |
141 | } |
142 | |
143 | return false; |
144 | } |
145 | |
146 | static bool pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey, |
147 | uint16_t sigalg) { |
148 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
149 | if (alg == NULL || |
150 | EVP_PKEY_id(pkey) != alg->pkey_type) { |
151 | return false; |
152 | } |
153 | |
154 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
155 | // RSA keys may only be used with RSA-PSS. |
156 | if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) { |
157 | return false; |
158 | } |
159 | |
160 | // EC keys have a curve requirement. |
161 | if (alg->pkey_type == EVP_PKEY_EC && |
162 | (alg->curve == NID_undef || |
163 | EC_GROUP_get_curve_name( |
164 | EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) { |
165 | return false; |
166 | } |
167 | } |
168 | |
169 | return true; |
170 | } |
171 | |
172 | static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, |
173 | uint16_t sigalg, bool is_verify) { |
174 | if (!pkey_supports_algorithm(ssl, pkey, sigalg)) { |
175 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
176 | return false; |
177 | } |
178 | |
179 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
180 | const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL; |
181 | EVP_PKEY_CTX *pctx; |
182 | if (is_verify) { |
183 | if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) { |
184 | return false; |
185 | } |
186 | } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) { |
187 | return false; |
188 | } |
189 | |
190 | if (alg->is_rsa_pss) { |
191 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
192 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) { |
193 | return false; |
194 | } |
195 | } |
196 | |
197 | return true; |
198 | } |
199 | |
200 | enum ssl_private_key_result_t ssl_private_key_sign( |
201 | SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
202 | uint16_t sigalg, Span<const uint8_t> in) { |
203 | SSL *const ssl = hs->ssl; |
204 | const SSL_PRIVATE_KEY_METHOD *key_method = hs->config->cert->key_method; |
205 | EVP_PKEY *privatekey = hs->config->cert->privatekey.get(); |
206 | if (ssl_signing_with_dc(hs)) { |
207 | key_method = hs->config->cert->dc_key_method; |
208 | privatekey = hs->config->cert->dc_privatekey.get(); |
209 | } |
210 | |
211 | if (key_method != NULL) { |
212 | enum ssl_private_key_result_t ret; |
213 | if (hs->pending_private_key_op) { |
214 | ret = key_method->complete(ssl, out, out_len, max_out); |
215 | } else { |
216 | ret = key_method->sign(ssl, out, out_len, max_out, |
217 | sigalg, in.data(), in.size()); |
218 | } |
219 | if (ret == ssl_private_key_failure) { |
220 | OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED); |
221 | } |
222 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
223 | return ret; |
224 | } |
225 | |
226 | *out_len = max_out; |
227 | ScopedEVP_MD_CTX ctx; |
228 | if (!setup_ctx(ssl, ctx.get(), privatekey, sigalg, false /* sign */) || |
229 | !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) { |
230 | return ssl_private_key_failure; |
231 | } |
232 | return ssl_private_key_success; |
233 | } |
234 | |
235 | bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature, |
236 | uint16_t sigalg, EVP_PKEY *pkey, |
237 | Span<const uint8_t> in) { |
238 | ScopedEVP_MD_CTX ctx; |
239 | if (!setup_ctx(ssl, ctx.get(), pkey, sigalg, true /* verify */)) { |
240 | return false; |
241 | } |
242 | bool ok = EVP_DigestVerify(ctx.get(), signature.data(), signature.size(), |
243 | in.data(), in.size()); |
244 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
245 | ok = true; |
246 | ERR_clear_error(); |
247 | #endif |
248 | return ok; |
249 | } |
250 | |
251 | enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs, |
252 | uint8_t *out, |
253 | size_t *out_len, |
254 | size_t max_out, |
255 | Span<const uint8_t> in) { |
256 | SSL *const ssl = hs->ssl; |
257 | if (hs->config->cert->key_method != NULL) { |
258 | enum ssl_private_key_result_t ret; |
259 | if (hs->pending_private_key_op) { |
260 | ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out); |
261 | } else { |
262 | ret = hs->config->cert->key_method->decrypt(ssl, out, out_len, max_out, |
263 | in.data(), in.size()); |
264 | } |
265 | if (ret == ssl_private_key_failure) { |
266 | OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED); |
267 | } |
268 | hs->pending_private_key_op = ret == ssl_private_key_retry; |
269 | return ret; |
270 | } |
271 | |
272 | RSA *rsa = EVP_PKEY_get0_RSA(hs->config->cert->privatekey.get()); |
273 | if (rsa == NULL) { |
274 | // Decrypt operations are only supported for RSA keys. |
275 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
276 | return ssl_private_key_failure; |
277 | } |
278 | |
279 | // Decrypt with no padding. PKCS#1 padding will be removed as part of the |
280 | // timing-sensitive code by the caller. |
281 | if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(), |
282 | RSA_NO_PADDING)) { |
283 | return ssl_private_key_failure; |
284 | } |
285 | return ssl_private_key_success; |
286 | } |
287 | |
288 | bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs, |
289 | uint16_t sigalg) { |
290 | SSL *const ssl = hs->ssl; |
291 | if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) { |
292 | return false; |
293 | } |
294 | |
295 | // Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that |
296 | // emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the |
297 | // hash in TLS. Reasonable RSA key sizes are large enough for the largest |
298 | // defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for |
299 | // SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the |
300 | // size so that we can fall back to another algorithm in that case. |
301 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
302 | if (alg->is_rsa_pss && (size_t)EVP_PKEY_size(hs->local_pubkey.get()) < |
303 | 2 * EVP_MD_size(alg->digest_func()) + 2) { |
304 | return false; |
305 | } |
306 | |
307 | return true; |
308 | } |
309 | |
310 | BSSL_NAMESPACE_END |
311 | |
312 | using namespace bssl; |
313 | |
314 | int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) { |
315 | if (rsa == NULL || ssl->config == NULL) { |
316 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
317 | return 0; |
318 | } |
319 | |
320 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
321 | if (!pkey || |
322 | !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
323 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
324 | return 0; |
325 | } |
326 | |
327 | return ssl_set_pkey(ssl->config->cert.get(), pkey.get()); |
328 | } |
329 | |
330 | int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) { |
331 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
332 | if (!rsa) { |
333 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
334 | return 0; |
335 | } |
336 | |
337 | return SSL_use_RSAPrivateKey(ssl, rsa.get()); |
338 | } |
339 | |
340 | int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) { |
341 | if (pkey == NULL || ssl->config == NULL) { |
342 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
343 | return 0; |
344 | } |
345 | |
346 | return ssl_set_pkey(ssl->config->cert.get(), pkey); |
347 | } |
348 | |
349 | int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der, |
350 | size_t der_len) { |
351 | if (der_len > LONG_MAX) { |
352 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
353 | return 0; |
354 | } |
355 | |
356 | const uint8_t *p = der; |
357 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
358 | if (!pkey || p != der + der_len) { |
359 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
360 | return 0; |
361 | } |
362 | |
363 | return SSL_use_PrivateKey(ssl, pkey.get()); |
364 | } |
365 | |
366 | int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) { |
367 | if (rsa == NULL) { |
368 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
369 | return 0; |
370 | } |
371 | |
372 | UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
373 | if (!pkey || |
374 | !EVP_PKEY_set1_RSA(pkey.get(), rsa)) { |
375 | OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB); |
376 | return 0; |
377 | } |
378 | |
379 | return ssl_set_pkey(ctx->cert.get(), pkey.get()); |
380 | } |
381 | |
382 | int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der, |
383 | size_t der_len) { |
384 | UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len)); |
385 | if (!rsa) { |
386 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
387 | return 0; |
388 | } |
389 | |
390 | return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get()); |
391 | } |
392 | |
393 | int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) { |
394 | if (pkey == NULL) { |
395 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
396 | return 0; |
397 | } |
398 | |
399 | return ssl_set_pkey(ctx->cert.get(), pkey); |
400 | } |
401 | |
402 | int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der, |
403 | size_t der_len) { |
404 | if (der_len > LONG_MAX) { |
405 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
406 | return 0; |
407 | } |
408 | |
409 | const uint8_t *p = der; |
410 | UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len)); |
411 | if (!pkey || p != der + der_len) { |
412 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
413 | return 0; |
414 | } |
415 | |
416 | return SSL_CTX_use_PrivateKey(ctx, pkey.get()); |
417 | } |
418 | |
419 | void SSL_set_private_key_method(SSL *ssl, |
420 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
421 | if (!ssl->config) { |
422 | return; |
423 | } |
424 | ssl->config->cert->key_method = key_method; |
425 | } |
426 | |
427 | void SSL_CTX_set_private_key_method(SSL_CTX *ctx, |
428 | const SSL_PRIVATE_KEY_METHOD *key_method) { |
429 | ctx->cert->key_method = key_method; |
430 | } |
431 | |
432 | static constexpr size_t kMaxSignatureAlgorithmNameLen = 23; |
433 | |
434 | // This was "constexpr" rather than "const", but that triggered a bug in MSVC |
435 | // where it didn't pad the strings to the correct length. |
436 | static const struct { |
437 | uint16_t signature_algorithm; |
438 | const char name[kMaxSignatureAlgorithmNameLen]; |
439 | } kSignatureAlgorithmNames[] = { |
440 | {SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1" }, |
441 | {SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1" }, |
442 | {SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256" }, |
443 | {SSL_SIGN_RSA_PKCS1_SHA384, "rsa_pkcs1_sha384" }, |
444 | {SSL_SIGN_RSA_PKCS1_SHA512, "rsa_pkcs1_sha512" }, |
445 | {SSL_SIGN_ECDSA_SHA1, "ecdsa_sha1" }, |
446 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, "ecdsa_secp256r1_sha256" }, |
447 | {SSL_SIGN_ECDSA_SECP384R1_SHA384, "ecdsa_secp384r1_sha384" }, |
448 | {SSL_SIGN_ECDSA_SECP521R1_SHA512, "ecdsa_secp521r1_sha512" }, |
449 | {SSL_SIGN_RSA_PSS_RSAE_SHA256, "rsa_pss_rsae_sha256" }, |
450 | {SSL_SIGN_RSA_PSS_RSAE_SHA384, "rsa_pss_rsae_sha384" }, |
451 | {SSL_SIGN_RSA_PSS_RSAE_SHA512, "rsa_pss_rsae_sha512" }, |
452 | {SSL_SIGN_ED25519, "ed25519" }, |
453 | }; |
454 | |
455 | const char *SSL_get_signature_algorithm_name(uint16_t sigalg, |
456 | int include_curve) { |
457 | if (!include_curve) { |
458 | switch (sigalg) { |
459 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
460 | return "ecdsa_sha256" ; |
461 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
462 | return "ecdsa_sha384" ; |
463 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
464 | return "ecdsa_sha512" ; |
465 | } |
466 | } |
467 | |
468 | for (const auto &candidate : kSignatureAlgorithmNames) { |
469 | if (candidate.signature_algorithm == sigalg) { |
470 | return candidate.name; |
471 | } |
472 | } |
473 | |
474 | return NULL; |
475 | } |
476 | |
477 | int SSL_get_signature_algorithm_key_type(uint16_t sigalg) { |
478 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
479 | return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE; |
480 | } |
481 | |
482 | const EVP_MD *SSL_get_signature_algorithm_digest(uint16_t sigalg) { |
483 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
484 | if (alg == nullptr || alg->digest_func == nullptr) { |
485 | return nullptr; |
486 | } |
487 | return alg->digest_func(); |
488 | } |
489 | |
490 | int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg) { |
491 | const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg); |
492 | return alg != nullptr && alg->is_rsa_pss; |
493 | } |
494 | |
495 | int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
496 | size_t num_prefs) { |
497 | return ctx->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs)); |
498 | } |
499 | |
500 | int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs, |
501 | size_t num_prefs) { |
502 | if (!ssl->config) { |
503 | return 0; |
504 | } |
505 | return ssl->config->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs)); |
506 | } |
507 | |
508 | static constexpr struct { |
509 | int pkey_type; |
510 | int hash_nid; |
511 | uint16_t signature_algorithm; |
512 | } kSignatureAlgorithmsMapping[] = { |
513 | {EVP_PKEY_RSA, NID_sha1, SSL_SIGN_RSA_PKCS1_SHA1}, |
514 | {EVP_PKEY_RSA, NID_sha256, SSL_SIGN_RSA_PKCS1_SHA256}, |
515 | {EVP_PKEY_RSA, NID_sha384, SSL_SIGN_RSA_PKCS1_SHA384}, |
516 | {EVP_PKEY_RSA, NID_sha512, SSL_SIGN_RSA_PKCS1_SHA512}, |
517 | {EVP_PKEY_RSA_PSS, NID_sha256, SSL_SIGN_RSA_PSS_RSAE_SHA256}, |
518 | {EVP_PKEY_RSA_PSS, NID_sha384, SSL_SIGN_RSA_PSS_RSAE_SHA384}, |
519 | {EVP_PKEY_RSA_PSS, NID_sha512, SSL_SIGN_RSA_PSS_RSAE_SHA512}, |
520 | {EVP_PKEY_EC, NID_sha1, SSL_SIGN_ECDSA_SHA1}, |
521 | {EVP_PKEY_EC, NID_sha256, SSL_SIGN_ECDSA_SECP256R1_SHA256}, |
522 | {EVP_PKEY_EC, NID_sha384, SSL_SIGN_ECDSA_SECP384R1_SHA384}, |
523 | {EVP_PKEY_EC, NID_sha512, SSL_SIGN_ECDSA_SECP521R1_SHA512}, |
524 | {EVP_PKEY_ED25519, NID_undef, SSL_SIGN_ED25519}, |
525 | }; |
526 | |
527 | static bool parse_sigalg_pairs(Array<uint16_t> *out, const int *values, |
528 | size_t num_values) { |
529 | if ((num_values & 1) == 1) { |
530 | return false; |
531 | } |
532 | |
533 | const size_t num_pairs = num_values / 2; |
534 | if (!out->Init(num_pairs)) { |
535 | return false; |
536 | } |
537 | |
538 | for (size_t i = 0; i < num_values; i += 2) { |
539 | const int hash_nid = values[i]; |
540 | const int pkey_type = values[i+1]; |
541 | |
542 | bool found = false; |
543 | for (const auto &candidate : kSignatureAlgorithmsMapping) { |
544 | if (candidate.pkey_type == pkey_type && candidate.hash_nid == hash_nid) { |
545 | (*out)[i / 2] = candidate.signature_algorithm; |
546 | found = true; |
547 | break; |
548 | } |
549 | } |
550 | |
551 | if (!found) { |
552 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
553 | ERR_add_error_dataf("unknown hash:%d pkey:%d" , hash_nid, pkey_type); |
554 | return false; |
555 | } |
556 | } |
557 | |
558 | return true; |
559 | } |
560 | |
561 | static int compare_uint16_t(const void *p1, const void *p2) { |
562 | uint16_t u1 = *((const uint16_t *)p1); |
563 | uint16_t u2 = *((const uint16_t *)p2); |
564 | if (u1 < u2) { |
565 | return -1; |
566 | } else if (u1 > u2) { |
567 | return 1; |
568 | } else { |
569 | return 0; |
570 | } |
571 | } |
572 | |
573 | static bool sigalgs_unique(Span<const uint16_t> in_sigalgs) { |
574 | if (in_sigalgs.size() < 2) { |
575 | return true; |
576 | } |
577 | |
578 | Array<uint16_t> sigalgs; |
579 | if (!sigalgs.CopyFrom(in_sigalgs)) { |
580 | return false; |
581 | } |
582 | |
583 | qsort(sigalgs.data(), sigalgs.size(), sizeof(uint16_t), compare_uint16_t); |
584 | |
585 | for (size_t i = 1; i < sigalgs.size(); i++) { |
586 | if (sigalgs[i - 1] == sigalgs[i]) { |
587 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_SIGNATURE_ALGORITHM); |
588 | return false; |
589 | } |
590 | } |
591 | |
592 | return true; |
593 | } |
594 | |
595 | int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values) { |
596 | Array<uint16_t> sigalgs; |
597 | if (!parse_sigalg_pairs(&sigalgs, values, num_values) || |
598 | !sigalgs_unique(sigalgs)) { |
599 | return 0; |
600 | } |
601 | |
602 | if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(), |
603 | sigalgs.size()) || |
604 | !ctx->verify_sigalgs.CopyFrom(sigalgs)) { |
605 | return 0; |
606 | } |
607 | |
608 | return 1; |
609 | } |
610 | |
611 | int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values) { |
612 | if (!ssl->config) { |
613 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
614 | return 0; |
615 | } |
616 | |
617 | Array<uint16_t> sigalgs; |
618 | if (!parse_sigalg_pairs(&sigalgs, values, num_values) || |
619 | !sigalgs_unique(sigalgs)) { |
620 | return 0; |
621 | } |
622 | |
623 | if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) || |
624 | !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) { |
625 | return 0; |
626 | } |
627 | |
628 | return 1; |
629 | } |
630 | |
631 | static bool parse_sigalgs_list(Array<uint16_t> *out, const char *str) { |
632 | // str looks like "RSA+SHA1:ECDSA+SHA256:ecdsa_secp256r1_sha256". |
633 | |
634 | // Count colons to give the number of output elements from any successful |
635 | // parse. |
636 | size_t num_elements = 1; |
637 | size_t len = 0; |
638 | for (const char *p = str; *p; p++) { |
639 | len++; |
640 | if (*p == ':') { |
641 | num_elements++; |
642 | } |
643 | } |
644 | |
645 | if (!out->Init(num_elements)) { |
646 | return false; |
647 | } |
648 | size_t out_i = 0; |
649 | |
650 | enum { |
651 | pkey_or_name, |
652 | hash_name, |
653 | } state = pkey_or_name; |
654 | |
655 | char buf[kMaxSignatureAlgorithmNameLen]; |
656 | // buf_used is always < sizeof(buf). I.e. it's always safe to write |
657 | // buf[buf_used] = 0. |
658 | size_t buf_used = 0; |
659 | |
660 | int pkey_type = 0, hash_nid = 0; |
661 | |
662 | // Note that the loop runs to len+1, i.e. it'll process the terminating NUL. |
663 | for (size_t offset = 0; offset < len+1; offset++) { |
664 | const char c = str[offset]; |
665 | |
666 | switch (c) { |
667 | case '+': |
668 | if (state == hash_name) { |
669 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
670 | ERR_add_error_dataf("+ found in hash name at offset %zu" , offset); |
671 | return false; |
672 | } |
673 | if (buf_used == 0) { |
674 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
675 | ERR_add_error_dataf("empty public key type at offset %zu" , offset); |
676 | return false; |
677 | } |
678 | buf[buf_used] = 0; |
679 | |
680 | if (strcmp(buf, "RSA" ) == 0) { |
681 | pkey_type = EVP_PKEY_RSA; |
682 | } else if (strcmp(buf, "RSA-PSS" ) == 0 || |
683 | strcmp(buf, "PSS" ) == 0) { |
684 | pkey_type = EVP_PKEY_RSA_PSS; |
685 | } else if (strcmp(buf, "ECDSA" ) == 0) { |
686 | pkey_type = EVP_PKEY_EC; |
687 | } else { |
688 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
689 | ERR_add_error_dataf("unknown public key type '%s'" , buf); |
690 | return false; |
691 | } |
692 | |
693 | state = hash_name; |
694 | buf_used = 0; |
695 | break; |
696 | |
697 | case ':': |
698 | OPENSSL_FALLTHROUGH; |
699 | case 0: |
700 | if (buf_used == 0) { |
701 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
702 | ERR_add_error_dataf("empty element at offset %zu" , offset); |
703 | return false; |
704 | } |
705 | |
706 | buf[buf_used] = 0; |
707 | |
708 | if (state == pkey_or_name) { |
709 | // No '+' was seen thus this is a TLS 1.3-style name. |
710 | bool found = false; |
711 | for (const auto &candidate : kSignatureAlgorithmNames) { |
712 | if (strcmp(candidate.name, buf) == 0) { |
713 | assert(out_i < num_elements); |
714 | (*out)[out_i++] = candidate.signature_algorithm; |
715 | found = true; |
716 | break; |
717 | } |
718 | } |
719 | |
720 | if (!found) { |
721 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
722 | ERR_add_error_dataf("unknown signature algorithm '%s'" , buf); |
723 | return false; |
724 | } |
725 | } else { |
726 | if (strcmp(buf, "SHA1" ) == 0) { |
727 | hash_nid = NID_sha1; |
728 | } else if (strcmp(buf, "SHA256" ) == 0) { |
729 | hash_nid = NID_sha256; |
730 | } else if (strcmp(buf, "SHA384" ) == 0) { |
731 | hash_nid = NID_sha384; |
732 | } else if (strcmp(buf, "SHA512" ) == 0) { |
733 | hash_nid = NID_sha512; |
734 | } else { |
735 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
736 | ERR_add_error_dataf("unknown hash function '%s'" , buf); |
737 | return false; |
738 | } |
739 | |
740 | bool found = false; |
741 | for (const auto &candidate : kSignatureAlgorithmsMapping) { |
742 | if (candidate.pkey_type == pkey_type && |
743 | candidate.hash_nid == hash_nid) { |
744 | assert(out_i < num_elements); |
745 | (*out)[out_i++] = candidate.signature_algorithm; |
746 | found = true; |
747 | break; |
748 | } |
749 | } |
750 | |
751 | if (!found) { |
752 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
753 | ERR_add_error_dataf("unknown pkey:%d hash:%s" , pkey_type, buf); |
754 | return false; |
755 | } |
756 | } |
757 | |
758 | state = pkey_or_name; |
759 | buf_used = 0; |
760 | break; |
761 | |
762 | default: |
763 | if (buf_used == sizeof(buf) - 1) { |
764 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
765 | ERR_add_error_dataf("substring too long at offset %zu" , offset); |
766 | return false; |
767 | } |
768 | |
769 | if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || |
770 | (c >= 'A' && c <= 'Z') || c == '-' || c == '_') { |
771 | buf[buf_used++] = c; |
772 | } else { |
773 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM); |
774 | ERR_add_error_dataf("invalid character 0x%02x at offest %zu" , c, |
775 | offset); |
776 | return false; |
777 | } |
778 | } |
779 | } |
780 | |
781 | assert(out_i == out->size()); |
782 | return true; |
783 | } |
784 | |
785 | int SSL_CTX_set1_sigalgs_list(SSL_CTX *ctx, const char *str) { |
786 | Array<uint16_t> sigalgs; |
787 | if (!parse_sigalgs_list(&sigalgs, str) || |
788 | !sigalgs_unique(sigalgs)) { |
789 | return 0; |
790 | } |
791 | |
792 | if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(), |
793 | sigalgs.size()) || |
794 | !ctx->verify_sigalgs.CopyFrom(sigalgs)) { |
795 | return 0; |
796 | } |
797 | |
798 | return 1; |
799 | } |
800 | |
801 | int SSL_set1_sigalgs_list(SSL *ssl, const char *str) { |
802 | if (!ssl->config) { |
803 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
804 | return 0; |
805 | } |
806 | |
807 | Array<uint16_t> sigalgs; |
808 | if (!parse_sigalgs_list(&sigalgs, str) || |
809 | !sigalgs_unique(sigalgs)) { |
810 | return 0; |
811 | } |
812 | |
813 | if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) || |
814 | !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) { |
815 | return 0; |
816 | } |
817 | |
818 | return 1; |
819 | } |
820 | |
821 | int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, |
822 | size_t num_prefs) { |
823 | return ctx->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs)); |
824 | } |
825 | |