blob: 57116cd6cb7f87bad24c22847398d3136347da3b [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
David Benjamin443a1f62015-09-04 15:05:05 -040057#include <openssl/ssl.h>
Adam Langley95c29f32014-06-20 12:00:00 -070058
David Benjamin44148742017-06-17 13:20:59 -040059#include <assert.h>
David Benjamin3a596112015-11-12 09:25:30 -080060#include <limits.h>
61
David Benjamin1fb125c2016-07-08 18:52:12 -070062#include <openssl/ec.h>
63#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -070064#include <openssl/err.h>
65#include <openssl/evp.h>
66#include <openssl/mem.h>
David Benjamina972b782023-05-05 10:22:43 -040067#include <openssl/span.h>
Adam Langley95c29f32014-06-20 12:00:00 -070068
David Benjamin2ee94aa2015-04-07 22:38:30 -040069#include "internal.h"
David Benjamin6114c3c2017-03-30 16:37:54 -050070#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070071
David Benjamin443a1f62015-09-04 15:05:05 -040072
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070073BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -040074
David Benjamin8525ff32018-09-05 18:44:15 -050075bool ssl_is_key_type_supported(int key_type) {
David Benjamin69522112017-03-28 15:38:29 -050076 return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC ||
77 key_type == EVP_PKEY_ED25519;
David Benjamind1d80782015-07-05 11:54:09 -040078}
79
David Benjamin8525ff32018-09-05 18:44:15 -050080static bool ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) {
David Benjamin890c2012023-02-08 15:24:47 -050081 if (!ssl_is_key_type_supported(EVP_PKEY_id(pkey))) {
Adam Langley52940c42017-02-01 12:40:31 -080082 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin8525ff32018-09-05 18:44:15 -050083 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -080084 }
85
David Benjamine325c3f2018-04-12 16:11:15 -040086 if (cert->chain != nullptr &&
87 sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) != nullptr &&
David Benjaminc11ea9422017-08-29 16:33:21 -040088 // Sanity-check that the private key and the certificate match.
Adam Langley52940c42017-02-01 12:40:31 -080089 !ssl_cert_check_private_key(cert, pkey)) {
David Benjamin8525ff32018-09-05 18:44:15 -050090 return false;
Adam Langley52940c42017-02-01 12:40:31 -080091 }
92
David Benjamin2908dd12018-06-29 17:46:42 -040093 cert->privatekey = UpRef(pkey);
David Benjamin8525ff32018-09-05 18:44:15 -050094 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -080095}
96
David Benjamin6114c3c2017-03-30 16:37:54 -050097typedef struct {
98 uint16_t sigalg;
99 int pkey_type;
100 int curve;
101 const EVP_MD *(*digest_func)(void);
David Benjamin8525ff32018-09-05 18:44:15 -0500102 bool is_rsa_pss;
David Benjamin6114c3c2017-03-30 16:37:54 -0500103} SSL_SIGNATURE_ALGORITHM;
104
105static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
David Benjamin8525ff32018-09-05 18:44:15 -0500106 {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1,
David Benjamine5fe31c2021-03-19 14:06:51 -0400107 false},
108 {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, false},
109 {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, false},
110 {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, false},
111 {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500112
David Benjamine5fe31c2021-03-19 14:06:51 -0400113 {SSL_SIGN_RSA_PSS_RSAE_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, true},
114 {SSL_SIGN_RSA_PSS_RSAE_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, true},
115 {SSL_SIGN_RSA_PSS_RSAE_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, true},
David Benjamin6114c3c2017-03-30 16:37:54 -0500116
David Benjamine5fe31c2021-03-19 14:06:51 -0400117 {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500118 {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
David Benjamine5fe31c2021-03-19 14:06:51 -0400119 &EVP_sha256, false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500120 {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
David Benjamine5fe31c2021-03-19 14:06:51 -0400121 false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500122 {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
David Benjamine5fe31c2021-03-19 14:06:51 -0400123 false},
David Benjamin69522112017-03-28 15:38:29 -0500124
David Benjamine5fe31c2021-03-19 14:06:51 -0400125 {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, nullptr, false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500126};
127
128static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) {
129 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) {
130 if (kSignatureAlgorithms[i].sigalg == sigalg) {
131 return &kSignatureAlgorithms[i];
132 }
133 }
134 return NULL;
135}
136
Christopher Patton9cde8482018-07-17 11:36:36 -0700137bool ssl_has_private_key(const SSL_HANDSHAKE *hs) {
Christopher Patton6c1b3762018-07-17 12:49:41 -0700138 if (hs->config->cert->privatekey != nullptr ||
139 hs->config->cert->key_method != nullptr ||
140 ssl_signing_with_dc(hs)) {
141 return true;
142 }
143
144 return false;
nagendra modadugu601448a2015-07-24 09:31:31 -0700145}
146
David Benjamin8525ff32018-09-05 18:44:15 -0500147static bool pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
David Benjamine5fe31c2021-03-19 14:06:51 -0400148 uint16_t sigalg) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500149 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
150 if (alg == NULL ||
151 EVP_PKEY_id(pkey) != alg->pkey_type) {
David Benjamin8525ff32018-09-05 18:44:15 -0500152 return false;
David Benjamind246b812016-07-08 15:07:02 -0700153 }
David Benjamin6114c3c2017-03-30 16:37:54 -0500154
David Benjamine8f57ca2022-11-29 18:40:11 -0500155 if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
156 // TLS 1.0 and 1.1 do not negotiate algorithms and always sign one of two
157 // hardcoded algorithms.
158 return sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
159 sigalg == SSL_SIGN_ECDSA_SHA1;
160 }
161
162 // |SSL_SIGN_RSA_PKCS1_MD5_SHA1| is not a real SignatureScheme for TLS 1.2 and
163 // higher. It is an internal value we use to represent TLS 1.0/1.1's MD5/SHA1
164 // concatenation.
165 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1) {
166 return false;
167 }
168
David Benjamind1e3ce12017-10-06 18:31:15 -0400169 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamine5fe31c2021-03-19 14:06:51 -0400170 // RSA keys may only be used with RSA-PSS.
171 if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
David Benjamin8525ff32018-09-05 18:44:15 -0500172 return false;
David Benjamin6114c3c2017-03-30 16:37:54 -0500173 }
174
David Benjaminc11ea9422017-08-29 16:33:21 -0400175 // EC keys have a curve requirement.
David Benjamin6114c3c2017-03-30 16:37:54 -0500176 if (alg->pkey_type == EVP_PKEY_EC &&
177 (alg->curve == NID_undef ||
178 EC_GROUP_get_curve_name(
179 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500180 return false;
David Benjamin6114c3c2017-03-30 16:37:54 -0500181 }
182 }
183
David Benjamin8525ff32018-09-05 18:44:15 -0500184 return true;
David Benjamind246b812016-07-08 15:07:02 -0700185}
186
David Benjamin8525ff32018-09-05 18:44:15 -0500187static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey,
188 uint16_t sigalg, bool is_verify) {
David Benjamine5fe31c2021-03-19 14:06:51 -0400189 if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500190 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin8525ff32018-09-05 18:44:15 -0500191 return false;
David Benjamin887c3002016-07-08 16:15:32 -0700192 }
David Benjamina2d81f12016-07-08 15:42:16 -0700193
David Benjamin6114c3c2017-03-30 16:37:54 -0500194 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
David Benjamin19670942017-05-31 19:07:31 -0400195 const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL;
196 EVP_PKEY_CTX *pctx;
197 if (is_verify) {
198 if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500199 return false;
David Benjamin19670942017-05-31 19:07:31 -0400200 }
201 } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500202 return false;
David Benjamina2d81f12016-07-08 15:42:16 -0700203 }
David Benjamina2d81f12016-07-08 15:42:16 -0700204
David Benjamin6114c3c2017-03-30 16:37:54 -0500205 if (alg->is_rsa_pss) {
David Benjamin19670942017-05-31 19:07:31 -0400206 if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
207 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500208 return false;
David Benjamin76feb1f2017-03-28 14:17:01 -0500209 }
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400210 }
211
David Benjamin8525ff32018-09-05 18:44:15 -0500212 return true;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400213}
214
David Benjaminb4d65fd2015-05-29 17:11:21 -0400215enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -0400216 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin75a1f232017-10-11 17:19:19 -0400217 uint16_t sigalg, Span<const uint8_t> in) {
David Benjamin44148742017-06-17 13:20:59 -0400218 SSL *const ssl = hs->ssl;
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700219 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
220 Array<uint8_t> spki;
221 if (hints) {
222 ScopedCBB spki_cbb;
223 if (!CBB_init(spki_cbb.get(), 64) ||
224 !EVP_marshal_public_key(spki_cbb.get(), hs->local_pubkey.get()) ||
225 !CBBFinishArray(spki_cbb.get(), &spki)) {
226 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
227 return ssl_private_key_failure;
228 }
229 }
230
231 // Replay the signature from handshake hints if available.
232 if (hints && !hs->hints_requested && //
233 sigalg == hints->signature_algorithm && //
234 in == hints->signature_input &&
235 MakeConstSpan(spki) == hints->signature_spki &&
236 !hints->signature.empty() && //
237 hints->signature.size() <= max_out) {
238 // Signature algorithm and input both match. Reuse the signature from hints.
239 *out_len = hints->signature.size();
240 OPENSSL_memcpy(out, hints->signature.data(), hints->signature.size());
241 return ssl_private_key_success;
242 }
243
Christopher Patton6c1b3762018-07-17 12:49:41 -0700244 const SSL_PRIVATE_KEY_METHOD *key_method = hs->config->cert->key_method;
245 EVP_PKEY *privatekey = hs->config->cert->privatekey.get();
David Benjamin9b2cdb72021-04-01 23:21:53 -0400246 assert(!hs->can_release_private_key);
Christopher Patton6c1b3762018-07-17 12:49:41 -0700247 if (ssl_signing_with_dc(hs)) {
248 key_method = hs->config->cert->dc_key_method;
249 privatekey = hs->config->cert->dc_privatekey.get();
250 }
251
252 if (key_method != NULL) {
David Benjamin44148742017-06-17 13:20:59 -0400253 enum ssl_private_key_result_t ret;
254 if (hs->pending_private_key_op) {
Christopher Patton6c1b3762018-07-17 12:49:41 -0700255 ret = key_method->complete(ssl, out, out_len, max_out);
David Benjamin44148742017-06-17 13:20:59 -0400256 } else {
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700257 ret = key_method->sign(ssl, out, out_len, max_out, sigalg, in.data(),
258 in.size());
David Benjamind3440b42016-07-14 14:52:41 -0400259 }
David Benjaminfa651132018-02-01 15:07:30 -0500260 if (ret == ssl_private_key_failure) {
261 OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
262 }
David Benjamin44148742017-06-17 13:20:59 -0400263 hs->pending_private_key_op = ret == ssl_private_key_retry;
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700264 if (ret != ssl_private_key_success) {
265 return ret;
266 }
267 } else {
268 *out_len = max_out;
269 ScopedEVP_MD_CTX ctx;
270 if (!setup_ctx(ssl, ctx.get(), privatekey, sigalg, false /* sign */) ||
271 !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) {
272 return ssl_private_key_failure;
273 }
David Benjaminb4d65fd2015-05-29 17:11:21 -0400274 }
275
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700276 // Save the hint if applicable.
277 if (hints && hs->hints_requested) {
278 hints->signature_algorithm = sigalg;
279 hints->signature_spki = std::move(spki);
280 if (!hints->signature_input.CopyFrom(in) ||
281 !hints->signature.CopyFrom(MakeConstSpan(out, *out_len))) {
282 return ssl_private_key_failure;
283 }
David Benjamin1386aad2017-07-19 23:57:40 -0400284 }
285 return ssl_private_key_success;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400286}
287
David Benjamin75a1f232017-10-11 17:19:19 -0400288bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
289 uint16_t sigalg, EVP_PKEY *pkey,
290 Span<const uint8_t> in) {
David Benjamin1386aad2017-07-19 23:57:40 -0400291 ScopedEVP_MD_CTX ctx;
David Benjamin4dfd5af2019-07-19 17:34:37 -0400292 if (!setup_ctx(ssl, ctx.get(), pkey, sigalg, true /* verify */)) {
293 return false;
294 }
295 bool ok = EVP_DigestVerify(ctx.get(), signature.data(), signature.size(),
296 in.data(), in.size());
297#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
298 ok = true;
299 ERR_clear_error();
300#endif
301 return ok;
Steven Valdez2b8415e2016-06-30 13:27:23 -0400302}
303
David Benjamin75a1f232017-10-11 17:19:19 -0400304enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
305 uint8_t *out,
306 size_t *out_len,
307 size_t max_out,
308 Span<const uint8_t> in) {
David Benjamin44148742017-06-17 13:20:59 -0400309 SSL *const ssl = hs->ssl;
David Benjamin9b2cdb72021-04-01 23:21:53 -0400310 assert(!hs->can_release_private_key);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700311 if (hs->config->cert->key_method != NULL) {
David Benjamin44148742017-06-17 13:20:59 -0400312 enum ssl_private_key_result_t ret;
313 if (hs->pending_private_key_op) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700314 ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out);
David Benjamin44148742017-06-17 13:20:59 -0400315 } else {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700316 ret = hs->config->cert->key_method->decrypt(ssl, out, out_len, max_out,
317 in.data(), in.size());
David Benjamin44148742017-06-17 13:20:59 -0400318 }
David Benjaminfa651132018-02-01 15:07:30 -0500319 if (ret == ssl_private_key_failure) {
320 OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
321 }
David Benjamin44148742017-06-17 13:20:59 -0400322 hs->pending_private_key_op = ret == ssl_private_key_retry;
323 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700324 }
325
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700326 RSA *rsa = EVP_PKEY_get0_RSA(hs->config->cert->privatekey.get());
David Benjamin758d1272015-11-20 17:47:25 -0500327 if (rsa == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400328 // Decrypt operations are only supported for RSA keys.
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700329 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
330 return ssl_private_key_failure;
331 }
332
David Benjaminc11ea9422017-08-29 16:33:21 -0400333 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
334 // timing-sensitive code by the caller.
David Benjamin75a1f232017-10-11 17:19:19 -0400335 if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(),
336 RSA_NO_PADDING)) {
David Benjamin758d1272015-11-20 17:47:25 -0500337 return ssl_private_key_failure;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700338 }
David Benjamin758d1272015-11-20 17:47:25 -0500339 return ssl_private_key_success;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700340}
341
David Benjamin75a1f232017-10-11 17:19:19 -0400342bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
343 uint16_t sigalg) {
David Benjamina232a712017-03-30 15:51:53 -0500344 SSL *const ssl = hs->ssl;
David Benjamine5fe31c2021-03-19 14:06:51 -0400345 if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) {
David Benjamin75a1f232017-10-11 17:19:19 -0400346 return false;
David Benjamin1fb125c2016-07-08 18:52:12 -0700347 }
348
David Benjaminc11ea9422017-08-29 16:33:21 -0400349 // Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
350 // emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
351 // hash in TLS. Reasonable RSA key sizes are large enough for the largest
352 // defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for
353 // SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the
354 // size so that we can fall back to another algorithm in that case.
David Benjamin6114c3c2017-03-30 16:37:54 -0500355 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400356 if (alg->is_rsa_pss && (size_t)EVP_PKEY_size(hs->local_pubkey.get()) <
357 2 * EVP_MD_size(alg->digest_func()) + 2) {
David Benjamin75a1f232017-10-11 17:19:19 -0400358 return false;
David Benjamin69522112017-03-28 15:38:29 -0500359 }
David Benjamin7944a9f2016-07-12 22:27:01 -0400360
David Benjamin75a1f232017-10-11 17:19:19 -0400361 return true;
David Benjamin1fb125c2016-07-08 18:52:12 -0700362}
David Benjamin86e95b82017-07-18 16:34:25 -0400363
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700364BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -0400365
366using namespace bssl;
367
368int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700369 if (rsa == NULL || ssl->config == NULL) {
David Benjamin86e95b82017-07-18 16:34:25 -0400370 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
371 return 0;
372 }
373
David Benjamin4492a612017-08-02 17:16:31 -0400374 UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
375 if (!pkey ||
376 !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400377 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
378 return 0;
379 }
380
David Benjamin0ce090a2018-07-02 20:24:40 -0400381 return ssl_set_pkey(ssl->config->cert.get(), pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400382}
383
384int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
385 UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
386 if (!rsa) {
387 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
388 return 0;
389 }
390
391 return SSL_use_RSAPrivateKey(ssl, rsa.get());
392}
393
394int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700395 if (pkey == NULL || ssl->config == NULL) {
David Benjamin86e95b82017-07-18 16:34:25 -0400396 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
397 return 0;
398 }
399
David Benjamin0ce090a2018-07-02 20:24:40 -0400400 return ssl_set_pkey(ssl->config->cert.get(), pkey);
David Benjamin86e95b82017-07-18 16:34:25 -0400401}
402
403int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
404 size_t der_len) {
405 if (der_len > LONG_MAX) {
406 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
407 return 0;
408 }
409
410 const uint8_t *p = der;
David Benjamin4492a612017-08-02 17:16:31 -0400411 UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
412 if (!pkey || p != der + der_len) {
David Benjamin86e95b82017-07-18 16:34:25 -0400413 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
David Benjamin86e95b82017-07-18 16:34:25 -0400414 return 0;
415 }
416
David Benjamin4492a612017-08-02 17:16:31 -0400417 return SSL_use_PrivateKey(ssl, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400418}
419
420int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
David Benjamin86e95b82017-07-18 16:34:25 -0400421 if (rsa == NULL) {
422 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
423 return 0;
424 }
425
David Benjamin4492a612017-08-02 17:16:31 -0400426 UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
427 if (!pkey ||
428 !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400429 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
430 return 0;
431 }
432
David Benjamin0ce090a2018-07-02 20:24:40 -0400433 return ssl_set_pkey(ctx->cert.get(), pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400434}
435
436int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
437 size_t der_len) {
David Benjamin4492a612017-08-02 17:16:31 -0400438 UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
439 if (!rsa) {
David Benjamin86e95b82017-07-18 16:34:25 -0400440 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
441 return 0;
442 }
443
David Benjamin4492a612017-08-02 17:16:31 -0400444 return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400445}
446
447int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
448 if (pkey == NULL) {
449 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
450 return 0;
451 }
452
David Benjamin0ce090a2018-07-02 20:24:40 -0400453 return ssl_set_pkey(ctx->cert.get(), pkey);
David Benjamin86e95b82017-07-18 16:34:25 -0400454}
455
456int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
457 size_t der_len) {
458 if (der_len > LONG_MAX) {
459 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
460 return 0;
461 }
462
463 const uint8_t *p = der;
David Benjamin4492a612017-08-02 17:16:31 -0400464 UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
465 if (!pkey || p != der + der_len) {
David Benjamin86e95b82017-07-18 16:34:25 -0400466 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
David Benjamin86e95b82017-07-18 16:34:25 -0400467 return 0;
468 }
469
David Benjamin4492a612017-08-02 17:16:31 -0400470 return SSL_CTX_use_PrivateKey(ctx, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400471}
472
473void SSL_set_private_key_method(SSL *ssl,
474 const SSL_PRIVATE_KEY_METHOD *key_method) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700475 if (!ssl->config) {
476 return;
477 }
478 ssl->config->cert->key_method = key_method;
David Benjamin86e95b82017-07-18 16:34:25 -0400479}
480
481void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
482 const SSL_PRIVATE_KEY_METHOD *key_method) {
483 ctx->cert->key_method = key_method;
484}
485
Adam Langley826ce152018-08-03 10:31:21 -0700486static constexpr size_t kMaxSignatureAlgorithmNameLen = 23;
487
Adam Langleyc215ce72023-05-08 11:35:14 -0700488struct SignatureAlgorithmName {
Adam Langley826ce152018-08-03 10:31:21 -0700489 uint16_t signature_algorithm;
490 const char name[kMaxSignatureAlgorithmNameLen];
Adam Langleyc215ce72023-05-08 11:35:14 -0700491};
492
493// This was "constexpr" rather than "const", but that triggered a bug in MSVC
494// where it didn't pad the strings to the correct length.
495static const SignatureAlgorithmName kSignatureAlgorithmNames[] = {
Adam Langley826ce152018-08-03 10:31:21 -0700496 {SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1"},
497 {SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1"},
498 {SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256"},
499 {SSL_SIGN_RSA_PKCS1_SHA384, "rsa_pkcs1_sha384"},
500 {SSL_SIGN_RSA_PKCS1_SHA512, "rsa_pkcs1_sha512"},
501 {SSL_SIGN_ECDSA_SHA1, "ecdsa_sha1"},
502 {SSL_SIGN_ECDSA_SECP256R1_SHA256, "ecdsa_secp256r1_sha256"},
503 {SSL_SIGN_ECDSA_SECP384R1_SHA384, "ecdsa_secp384r1_sha384"},
504 {SSL_SIGN_ECDSA_SECP521R1_SHA512, "ecdsa_secp521r1_sha512"},
505 {SSL_SIGN_RSA_PSS_RSAE_SHA256, "rsa_pss_rsae_sha256"},
506 {SSL_SIGN_RSA_PSS_RSAE_SHA384, "rsa_pss_rsae_sha384"},
507 {SSL_SIGN_RSA_PSS_RSAE_SHA512, "rsa_pss_rsae_sha512"},
508 {SSL_SIGN_ED25519, "ed25519"},
509};
510
David Benjamin6cc352e2017-11-02 17:21:39 -0400511const char *SSL_get_signature_algorithm_name(uint16_t sigalg,
512 int include_curve) {
Adam Langley826ce152018-08-03 10:31:21 -0700513 if (!include_curve) {
514 switch (sigalg) {
515 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
516 return "ecdsa_sha256";
517 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
518 return "ecdsa_sha384";
519 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
520 return "ecdsa_sha512";
Adam Langleyc215ce72023-05-08 11:35:14 -0700521 // If adding more here, also update
522 // |SSL_get_all_signature_algorithm_names|.
Adam Langley826ce152018-08-03 10:31:21 -0700523 }
David Benjamin6cc352e2017-11-02 17:21:39 -0400524 }
Adam Langley826ce152018-08-03 10:31:21 -0700525
526 for (const auto &candidate : kSignatureAlgorithmNames) {
527 if (candidate.signature_algorithm == sigalg) {
528 return candidate.name;
529 }
530 }
531
532 return NULL;
David Benjamin6cc352e2017-11-02 17:21:39 -0400533}
534
David Benjamina972b782023-05-05 10:22:43 -0400535size_t SSL_get_all_signature_algorithm_names(const char **out, size_t max_out) {
Adam Langleyc215ce72023-05-08 11:35:14 -0700536 const char *kPredefinedNames[] = {"ecdsa_sha256", "ecdsa_sha384",
537 "ecdsa_sha512"};
538 return GetAllNames(out, max_out, MakeConstSpan(kPredefinedNames),
539 &SignatureAlgorithmName::name,
540 MakeConstSpan(kSignatureAlgorithmNames));
David Benjamina972b782023-05-05 10:22:43 -0400541}
542
David Benjamin6cc352e2017-11-02 17:21:39 -0400543int SSL_get_signature_algorithm_key_type(uint16_t sigalg) {
544 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
545 return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE;
546}
547
548const EVP_MD *SSL_get_signature_algorithm_digest(uint16_t sigalg) {
549 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
550 if (alg == nullptr || alg->digest_func == nullptr) {
551 return nullptr;
552 }
553 return alg->digest_func();
554}
555
556int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg) {
557 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
558 return alg != nullptr && alg->is_rsa_pss;
559}
560
David Benjamin3a1b7302022-11-29 18:44:46 -0500561static 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
573static 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
595static bool set_sigalg_prefs(Array<uint16_t> *out, Span<const uint16_t> prefs) {
596 if (!sigalgs_unique(prefs)) {
597 return false;
598 }
599
600 // Check for invalid algorithms, and filter out |SSL_SIGN_RSA_PKCS1_MD5_SHA1|.
601 Array<uint16_t> filtered;
602 if (!filtered.Init(prefs.size())) {
603 return false;
604 }
605 size_t added = 0;
606 for (uint16_t pref : prefs) {
607 if (pref == SSL_SIGN_RSA_PKCS1_MD5_SHA1) {
608 // Though not intended to be used with this API, we treat
609 // |SSL_SIGN_RSA_PKCS1_MD5_SHA1| as a real signature algorithm in
610 // |SSL_PRIVATE_KEY_METHOD|. Not accepting it here makes for a confusing
611 // abstraction.
612 continue;
613 }
614 if (get_signature_algorithm(pref) == nullptr) {
615 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
616 return false;
617 }
618 filtered[added] = pref;
619 added++;
620 }
621 filtered.Shrink(added);
622
623 // This can happen if |prefs| contained only |SSL_SIGN_RSA_PKCS1_MD5_SHA1|.
624 // Leaving it empty would revert to the default, so treat this as an error
625 // condition.
626 if (!prefs.empty() && filtered.empty()) {
627 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
628 return false;
629 }
630
631 *out = std::move(filtered);
632 return true;
633}
634
David Benjamin86e95b82017-07-18 16:34:25 -0400635int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
636 size_t num_prefs) {
David Benjamin3a1b7302022-11-29 18:44:46 -0500637 return set_sigalg_prefs(&ctx->cert->sigalgs, MakeConstSpan(prefs, num_prefs));
David Benjamin86e95b82017-07-18 16:34:25 -0400638}
639
David Benjamin86e95b82017-07-18 16:34:25 -0400640int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
641 size_t num_prefs) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700642 if (!ssl->config) {
643 return 0;
644 }
David Benjamin3a1b7302022-11-29 18:44:46 -0500645 return set_sigalg_prefs(&ssl->config->cert->sigalgs,
646 MakeConstSpan(prefs, num_prefs));
David Benjamin86e95b82017-07-18 16:34:25 -0400647}
648
Adam Langley826ce152018-08-03 10:31:21 -0700649static constexpr struct {
650 int pkey_type;
651 int hash_nid;
652 uint16_t signature_algorithm;
653} kSignatureAlgorithmsMapping[] = {
654 {EVP_PKEY_RSA, NID_sha1, SSL_SIGN_RSA_PKCS1_SHA1},
655 {EVP_PKEY_RSA, NID_sha256, SSL_SIGN_RSA_PKCS1_SHA256},
656 {EVP_PKEY_RSA, NID_sha384, SSL_SIGN_RSA_PKCS1_SHA384},
657 {EVP_PKEY_RSA, NID_sha512, SSL_SIGN_RSA_PKCS1_SHA512},
658 {EVP_PKEY_RSA_PSS, NID_sha256, SSL_SIGN_RSA_PSS_RSAE_SHA256},
659 {EVP_PKEY_RSA_PSS, NID_sha384, SSL_SIGN_RSA_PSS_RSAE_SHA384},
660 {EVP_PKEY_RSA_PSS, NID_sha512, SSL_SIGN_RSA_PSS_RSAE_SHA512},
661 {EVP_PKEY_EC, NID_sha1, SSL_SIGN_ECDSA_SHA1},
662 {EVP_PKEY_EC, NID_sha256, SSL_SIGN_ECDSA_SECP256R1_SHA256},
663 {EVP_PKEY_EC, NID_sha384, SSL_SIGN_ECDSA_SECP384R1_SHA384},
664 {EVP_PKEY_EC, NID_sha512, SSL_SIGN_ECDSA_SECP521R1_SHA512},
665 {EVP_PKEY_ED25519, NID_undef, SSL_SIGN_ED25519},
666};
667
668static bool parse_sigalg_pairs(Array<uint16_t> *out, const int *values,
669 size_t num_values) {
670 if ((num_values & 1) == 1) {
671 return false;
672 }
673
674 const size_t num_pairs = num_values / 2;
675 if (!out->Init(num_pairs)) {
676 return false;
677 }
678
679 for (size_t i = 0; i < num_values; i += 2) {
680 const int hash_nid = values[i];
681 const int pkey_type = values[i+1];
682
683 bool found = false;
684 for (const auto &candidate : kSignatureAlgorithmsMapping) {
685 if (candidate.pkey_type == pkey_type && candidate.hash_nid == hash_nid) {
686 (*out)[i / 2] = candidate.signature_algorithm;
687 found = true;
688 break;
689 }
690 }
691
692 if (!found) {
693 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
694 ERR_add_error_dataf("unknown hash:%d pkey:%d", hash_nid, pkey_type);
695 return false;
696 }
697 }
698
699 return true;
700}
701
Adam Langley826ce152018-08-03 10:31:21 -0700702int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values) {
703 Array<uint16_t> sigalgs;
David Benjamin3a1b7302022-11-29 18:44:46 -0500704 if (!parse_sigalg_pairs(&sigalgs, values, num_values)) {
Adam Langley826ce152018-08-03 10:31:21 -0700705 return 0;
706 }
707
708 if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(),
709 sigalgs.size()) ||
David Benjamin3a1b7302022-11-29 18:44:46 -0500710 !SSL_CTX_set_verify_algorithm_prefs(ctx, sigalgs.data(),
711 sigalgs.size())) {
Adam Langley826ce152018-08-03 10:31:21 -0700712 return 0;
713 }
714
715 return 1;
716}
717
718int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values) {
719 if (!ssl->config) {
720 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
721 return 0;
722 }
723
724 Array<uint16_t> sigalgs;
David Benjamin3a1b7302022-11-29 18:44:46 -0500725 if (!parse_sigalg_pairs(&sigalgs, values, num_values)) {
Adam Langley826ce152018-08-03 10:31:21 -0700726 return 0;
727 }
728
729 if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
David Benjamin3a1b7302022-11-29 18:44:46 -0500730 !SSL_set_verify_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size())) {
Adam Langley826ce152018-08-03 10:31:21 -0700731 return 0;
732 }
733
734 return 1;
735}
736
737static bool parse_sigalgs_list(Array<uint16_t> *out, const char *str) {
738 // str looks like "RSA+SHA1:ECDSA+SHA256:ecdsa_secp256r1_sha256".
739
740 // Count colons to give the number of output elements from any successful
741 // parse.
742 size_t num_elements = 1;
743 size_t len = 0;
744 for (const char *p = str; *p; p++) {
745 len++;
746 if (*p == ':') {
747 num_elements++;
748 }
749 }
750
751 if (!out->Init(num_elements)) {
752 return false;
753 }
754 size_t out_i = 0;
755
756 enum {
757 pkey_or_name,
758 hash_name,
759 } state = pkey_or_name;
760
761 char buf[kMaxSignatureAlgorithmNameLen];
762 // buf_used is always < sizeof(buf). I.e. it's always safe to write
763 // buf[buf_used] = 0.
764 size_t buf_used = 0;
765
766 int pkey_type = 0, hash_nid = 0;
767
768 // Note that the loop runs to len+1, i.e. it'll process the terminating NUL.
769 for (size_t offset = 0; offset < len+1; offset++) {
David Benjamin4f1fae32021-12-15 11:41:10 -0500770 const unsigned char c = str[offset];
Adam Langley826ce152018-08-03 10:31:21 -0700771
772 switch (c) {
773 case '+':
774 if (state == hash_name) {
775 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
776 ERR_add_error_dataf("+ found in hash name at offset %zu", offset);
777 return false;
778 }
779 if (buf_used == 0) {
780 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
781 ERR_add_error_dataf("empty public key type at offset %zu", offset);
782 return false;
783 }
784 buf[buf_used] = 0;
785
786 if (strcmp(buf, "RSA") == 0) {
787 pkey_type = EVP_PKEY_RSA;
788 } else if (strcmp(buf, "RSA-PSS") == 0 ||
789 strcmp(buf, "PSS") == 0) {
790 pkey_type = EVP_PKEY_RSA_PSS;
791 } else if (strcmp(buf, "ECDSA") == 0) {
792 pkey_type = EVP_PKEY_EC;
793 } else {
794 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
795 ERR_add_error_dataf("unknown public key type '%s'", buf);
796 return false;
797 }
798
799 state = hash_name;
800 buf_used = 0;
801 break;
802
803 case ':':
804 OPENSSL_FALLTHROUGH;
805 case 0:
806 if (buf_used == 0) {
807 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
808 ERR_add_error_dataf("empty element at offset %zu", offset);
809 return false;
810 }
811
812 buf[buf_used] = 0;
813
814 if (state == pkey_or_name) {
815 // No '+' was seen thus this is a TLS 1.3-style name.
816 bool found = false;
817 for (const auto &candidate : kSignatureAlgorithmNames) {
818 if (strcmp(candidate.name, buf) == 0) {
819 assert(out_i < num_elements);
820 (*out)[out_i++] = candidate.signature_algorithm;
821 found = true;
822 break;
823 }
824 }
825
826 if (!found) {
827 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
828 ERR_add_error_dataf("unknown signature algorithm '%s'", buf);
829 return false;
830 }
831 } else {
832 if (strcmp(buf, "SHA1") == 0) {
833 hash_nid = NID_sha1;
834 } else if (strcmp(buf, "SHA256") == 0) {
835 hash_nid = NID_sha256;
836 } else if (strcmp(buf, "SHA384") == 0) {
837 hash_nid = NID_sha384;
838 } else if (strcmp(buf, "SHA512") == 0) {
839 hash_nid = NID_sha512;
840 } else {
841 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
842 ERR_add_error_dataf("unknown hash function '%s'", buf);
843 return false;
844 }
845
846 bool found = false;
847 for (const auto &candidate : kSignatureAlgorithmsMapping) {
848 if (candidate.pkey_type == pkey_type &&
849 candidate.hash_nid == hash_nid) {
850 assert(out_i < num_elements);
851 (*out)[out_i++] = candidate.signature_algorithm;
852 found = true;
853 break;
854 }
855 }
856
857 if (!found) {
858 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
859 ERR_add_error_dataf("unknown pkey:%d hash:%s", pkey_type, buf);
860 return false;
861 }
862 }
863
864 state = pkey_or_name;
865 buf_used = 0;
866 break;
867
868 default:
869 if (buf_used == sizeof(buf) - 1) {
870 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
871 ERR_add_error_dataf("substring too long at offset %zu", offset);
872 return false;
873 }
874
Bob Beck00c70b82023-02-01 12:41:49 -0700875 if (OPENSSL_isalnum(c) || c == '-' || c == '_') {
Adam Langley826ce152018-08-03 10:31:21 -0700876 buf[buf_used++] = c;
877 } else {
878 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
879 ERR_add_error_dataf("invalid character 0x%02x at offest %zu", c,
880 offset);
881 return false;
882 }
883 }
884 }
885
886 assert(out_i == out->size());
887 return true;
888}
889
890int SSL_CTX_set1_sigalgs_list(SSL_CTX *ctx, const char *str) {
891 Array<uint16_t> sigalgs;
David Benjamin3a1b7302022-11-29 18:44:46 -0500892 if (!parse_sigalgs_list(&sigalgs, str)) {
Adam Langley826ce152018-08-03 10:31:21 -0700893 return 0;
894 }
895
896 if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(),
897 sigalgs.size()) ||
David Benjaminf0a815c2020-02-03 20:03:52 -0500898 !SSL_CTX_set_verify_algorithm_prefs(ctx, sigalgs.data(),
899 sigalgs.size())) {
Adam Langley826ce152018-08-03 10:31:21 -0700900 return 0;
901 }
902
903 return 1;
904}
905
906int SSL_set1_sigalgs_list(SSL *ssl, const char *str) {
907 if (!ssl->config) {
908 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
909 return 0;
910 }
911
912 Array<uint16_t> sigalgs;
David Benjamin3a1b7302022-11-29 18:44:46 -0500913 if (!parse_sigalgs_list(&sigalgs, str)) {
Adam Langley826ce152018-08-03 10:31:21 -0700914 return 0;
915 }
916
917 if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
David Benjaminf0a815c2020-02-03 20:03:52 -0500918 !SSL_set_verify_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size())) {
Adam Langley826ce152018-08-03 10:31:21 -0700919 return 0;
920 }
921
922 return 1;
923}
924
David Benjamin86e95b82017-07-18 16:34:25 -0400925int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
926 size_t num_prefs) {
David Benjamin3a1b7302022-11-29 18:44:46 -0500927 return set_sigalg_prefs(&ctx->verify_sigalgs,
928 MakeConstSpan(prefs, num_prefs));
David Benjamin86e95b82017-07-18 16:34:25 -0400929}
David Benjaminf0a815c2020-02-03 20:03:52 -0500930
931int SSL_set_verify_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
932 size_t num_prefs) {
933 if (!ssl->config) {
934 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
935 return 0;
936 }
937
David Benjamin3a1b7302022-11-29 18:44:46 -0500938 return set_sigalg_prefs(&ssl->config->verify_sigalgs,
939 MakeConstSpan(prefs, num_prefs));
David Benjaminf0a815c2020-02-03 20:03:52 -0500940}