blob: e800136d2045aa8cd156fa284d16a1b05d5e3425 [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>
Adam Langley95c29f32014-06-20 12:00:00 -070067
David Benjamin2ee94aa2015-04-07 22:38:30 -040068#include "internal.h"
David Benjamin6114c3c2017-03-30 16:37:54 -050069#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070070
David Benjamin443a1f62015-09-04 15:05:05 -040071
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070072BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -040073
David Benjamin8525ff32018-09-05 18:44:15 -050074bool ssl_is_key_type_supported(int key_type) {
David Benjamin69522112017-03-28 15:38:29 -050075 return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC ||
76 key_type == EVP_PKEY_ED25519;
David Benjamind1d80782015-07-05 11:54:09 -040077}
78
David Benjamin8525ff32018-09-05 18:44:15 -050079static bool ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) {
Adam Langley52940c42017-02-01 12:40:31 -080080 if (!ssl_is_key_type_supported(pkey->type)) {
81 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin8525ff32018-09-05 18:44:15 -050082 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -080083 }
84
David Benjamine325c3f2018-04-12 16:11:15 -040085 if (cert->chain != nullptr &&
86 sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) != nullptr &&
David Benjaminc11ea9422017-08-29 16:33:21 -040087 // Sanity-check that the private key and the certificate match.
Adam Langley52940c42017-02-01 12:40:31 -080088 !ssl_cert_check_private_key(cert, pkey)) {
David Benjamin8525ff32018-09-05 18:44:15 -050089 return false;
Adam Langley52940c42017-02-01 12:40:31 -080090 }
91
David Benjamin2908dd12018-06-29 17:46:42 -040092 cert->privatekey = UpRef(pkey);
David Benjamin8525ff32018-09-05 18:44:15 -050093 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -080094}
95
David Benjamin6114c3c2017-03-30 16:37:54 -050096typedef struct {
97 uint16_t sigalg;
98 int pkey_type;
99 int curve;
100 const EVP_MD *(*digest_func)(void);
David Benjamin8525ff32018-09-05 18:44:15 -0500101 bool is_rsa_pss;
David Benjamin6114c3c2017-03-30 16:37:54 -0500102} SSL_SIGNATURE_ALGORITHM;
103
104static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
David Benjamin8525ff32018-09-05 18:44:15 -0500105 {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1,
David Benjamine5fe31c2021-03-19 14:06:51 -0400106 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},
David Benjamin6114c3c2017-03-30 16:37:54 -0500111
David Benjamine5fe31c2021-03-19 14:06:51 -0400112 {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},
David Benjamin6114c3c2017-03-30 16:37:54 -0500115
David Benjamine5fe31c2021-03-19 14:06:51 -0400116 {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500117 {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
David Benjamine5fe31c2021-03-19 14:06:51 -0400118 &EVP_sha256, false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500119 {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
David Benjamine5fe31c2021-03-19 14:06:51 -0400120 false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500121 {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
David Benjamine5fe31c2021-03-19 14:06:51 -0400122 false},
David Benjamin69522112017-03-28 15:38:29 -0500123
David Benjamine5fe31c2021-03-19 14:06:51 -0400124 {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, nullptr, false},
David Benjamin6114c3c2017-03-30 16:37:54 -0500125};
126
127static 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
Christopher Patton9cde8482018-07-17 11:36:36 -0700136bool ssl_has_private_key(const SSL_HANDSHAKE *hs) {
Christopher Patton6c1b3762018-07-17 12:49:41 -0700137 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;
nagendra modadugu601448a2015-07-24 09:31:31 -0700144}
145
David Benjamin8525ff32018-09-05 18:44:15 -0500146static bool pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
David Benjamine5fe31c2021-03-19 14:06:51 -0400147 uint16_t sigalg) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500148 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
149 if (alg == NULL ||
150 EVP_PKEY_id(pkey) != alg->pkey_type) {
David Benjamin8525ff32018-09-05 18:44:15 -0500151 return false;
David Benjamind246b812016-07-08 15:07:02 -0700152 }
David Benjamin6114c3c2017-03-30 16:37:54 -0500153
David Benjamind1e3ce12017-10-06 18:31:15 -0400154 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamine5fe31c2021-03-19 14:06:51 -0400155 // RSA keys may only be used with RSA-PSS.
156 if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
David Benjamin8525ff32018-09-05 18:44:15 -0500157 return false;
David Benjamin6114c3c2017-03-30 16:37:54 -0500158 }
159
David Benjaminc11ea9422017-08-29 16:33:21 -0400160 // EC keys have a curve requirement.
David Benjamin6114c3c2017-03-30 16:37:54 -0500161 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)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500165 return false;
David Benjamin6114c3c2017-03-30 16:37:54 -0500166 }
167 }
168
David Benjamin8525ff32018-09-05 18:44:15 -0500169 return true;
David Benjamind246b812016-07-08 15:07:02 -0700170}
171
David Benjamin8525ff32018-09-05 18:44:15 -0500172static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey,
173 uint16_t sigalg, bool is_verify) {
David Benjamine5fe31c2021-03-19 14:06:51 -0400174 if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500175 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin8525ff32018-09-05 18:44:15 -0500176 return false;
David Benjamin887c3002016-07-08 16:15:32 -0700177 }
David Benjamina2d81f12016-07-08 15:42:16 -0700178
David Benjamin6114c3c2017-03-30 16:37:54 -0500179 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
David Benjamin19670942017-05-31 19:07:31 -0400180 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)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500184 return false;
David Benjamin19670942017-05-31 19:07:31 -0400185 }
186 } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500187 return false;
David Benjamina2d81f12016-07-08 15:42:16 -0700188 }
David Benjamina2d81f12016-07-08 15:42:16 -0700189
David Benjamin6114c3c2017-03-30 16:37:54 -0500190 if (alg->is_rsa_pss) {
David Benjamin19670942017-05-31 19:07:31 -0400191 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 */)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500193 return false;
David Benjamin76feb1f2017-03-28 14:17:01 -0500194 }
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400195 }
196
David Benjamin8525ff32018-09-05 18:44:15 -0500197 return true;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400198}
199
David Benjaminb4d65fd2015-05-29 17:11:21 -0400200enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -0400201 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin75a1f232017-10-11 17:19:19 -0400202 uint16_t sigalg, Span<const uint8_t> in) {
David Benjamin44148742017-06-17 13:20:59 -0400203 SSL *const ssl = hs->ssl;
Christopher Patton6c1b3762018-07-17 12:49:41 -0700204 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) {
David Benjamin44148742017-06-17 13:20:59 -0400212 enum ssl_private_key_result_t ret;
213 if (hs->pending_private_key_op) {
Christopher Patton6c1b3762018-07-17 12:49:41 -0700214 ret = key_method->complete(ssl, out, out_len, max_out);
David Benjamin44148742017-06-17 13:20:59 -0400215 } else {
Christopher Patton6c1b3762018-07-17 12:49:41 -0700216 ret = key_method->sign(ssl, out, out_len, max_out,
217 sigalg, in.data(), in.size());
David Benjamind3440b42016-07-14 14:52:41 -0400218 }
David Benjaminfa651132018-02-01 15:07:30 -0500219 if (ret == ssl_private_key_failure) {
220 OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
221 }
David Benjamin44148742017-06-17 13:20:59 -0400222 hs->pending_private_key_op = ret == ssl_private_key_retry;
223 return ret;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400224 }
225
David Benjamin76feb1f2017-03-28 14:17:01 -0500226 *out_len = max_out;
David Benjamin1386aad2017-07-19 23:57:40 -0400227 ScopedEVP_MD_CTX ctx;
Christopher Patton6c1b3762018-07-17 12:49:41 -0700228 if (!setup_ctx(ssl, ctx.get(), privatekey, sigalg, false /* sign */) ||
David Benjamin75a1f232017-10-11 17:19:19 -0400229 !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) {
David Benjamin1386aad2017-07-19 23:57:40 -0400230 return ssl_private_key_failure;
231 }
232 return ssl_private_key_success;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400233}
234
David Benjamin75a1f232017-10-11 17:19:19 -0400235bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
236 uint16_t sigalg, EVP_PKEY *pkey,
237 Span<const uint8_t> in) {
David Benjamin1386aad2017-07-19 23:57:40 -0400238 ScopedEVP_MD_CTX ctx;
David Benjamin4dfd5af2019-07-19 17:34:37 -0400239 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;
Steven Valdez2b8415e2016-06-30 13:27:23 -0400249}
250
David Benjamin75a1f232017-10-11 17:19:19 -0400251enum 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) {
David Benjamin44148742017-06-17 13:20:59 -0400256 SSL *const ssl = hs->ssl;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700257 if (hs->config->cert->key_method != NULL) {
David Benjamin44148742017-06-17 13:20:59 -0400258 enum ssl_private_key_result_t ret;
259 if (hs->pending_private_key_op) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700260 ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out);
David Benjamin44148742017-06-17 13:20:59 -0400261 } else {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700262 ret = hs->config->cert->key_method->decrypt(ssl, out, out_len, max_out,
263 in.data(), in.size());
David Benjamin44148742017-06-17 13:20:59 -0400264 }
David Benjaminfa651132018-02-01 15:07:30 -0500265 if (ret == ssl_private_key_failure) {
266 OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
267 }
David Benjamin44148742017-06-17 13:20:59 -0400268 hs->pending_private_key_op = ret == ssl_private_key_retry;
269 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700270 }
271
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700272 RSA *rsa = EVP_PKEY_get0_RSA(hs->config->cert->privatekey.get());
David Benjamin758d1272015-11-20 17:47:25 -0500273 if (rsa == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400274 // Decrypt operations are only supported for RSA keys.
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700275 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
276 return ssl_private_key_failure;
277 }
278
David Benjaminc11ea9422017-08-29 16:33:21 -0400279 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
280 // timing-sensitive code by the caller.
David Benjamin75a1f232017-10-11 17:19:19 -0400281 if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(),
282 RSA_NO_PADDING)) {
David Benjamin758d1272015-11-20 17:47:25 -0500283 return ssl_private_key_failure;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700284 }
David Benjamin758d1272015-11-20 17:47:25 -0500285 return ssl_private_key_success;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700286}
287
David Benjamin75a1f232017-10-11 17:19:19 -0400288bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
289 uint16_t sigalg) {
David Benjamina232a712017-03-30 15:51:53 -0500290 SSL *const ssl = hs->ssl;
David Benjamine5fe31c2021-03-19 14:06:51 -0400291 if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) {
David Benjamin75a1f232017-10-11 17:19:19 -0400292 return false;
David Benjamin1fb125c2016-07-08 18:52:12 -0700293 }
294
David Benjaminc11ea9422017-08-29 16:33:21 -0400295 // 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.
David Benjamin6114c3c2017-03-30 16:37:54 -0500301 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400302 if (alg->is_rsa_pss && (size_t)EVP_PKEY_size(hs->local_pubkey.get()) <
303 2 * EVP_MD_size(alg->digest_func()) + 2) {
David Benjamin75a1f232017-10-11 17:19:19 -0400304 return false;
David Benjamin69522112017-03-28 15:38:29 -0500305 }
David Benjamin7944a9f2016-07-12 22:27:01 -0400306
David Benjamin75a1f232017-10-11 17:19:19 -0400307 return true;
David Benjamin1fb125c2016-07-08 18:52:12 -0700308}
David Benjamin86e95b82017-07-18 16:34:25 -0400309
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700310BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -0400311
312using namespace bssl;
313
314int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700315 if (rsa == NULL || ssl->config == NULL) {
David Benjamin86e95b82017-07-18 16:34:25 -0400316 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
317 return 0;
318 }
319
David Benjamin4492a612017-08-02 17:16:31 -0400320 UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
321 if (!pkey ||
322 !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400323 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
324 return 0;
325 }
326
David Benjamin0ce090a2018-07-02 20:24:40 -0400327 return ssl_set_pkey(ssl->config->cert.get(), pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400328}
329
330int 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
340int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700341 if (pkey == NULL || ssl->config == NULL) {
David Benjamin86e95b82017-07-18 16:34:25 -0400342 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
343 return 0;
344 }
345
David Benjamin0ce090a2018-07-02 20:24:40 -0400346 return ssl_set_pkey(ssl->config->cert.get(), pkey);
David Benjamin86e95b82017-07-18 16:34:25 -0400347}
348
349int 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;
David Benjamin4492a612017-08-02 17:16:31 -0400357 UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
358 if (!pkey || p != der + der_len) {
David Benjamin86e95b82017-07-18 16:34:25 -0400359 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
David Benjamin86e95b82017-07-18 16:34:25 -0400360 return 0;
361 }
362
David Benjamin4492a612017-08-02 17:16:31 -0400363 return SSL_use_PrivateKey(ssl, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400364}
365
366int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
David Benjamin86e95b82017-07-18 16:34:25 -0400367 if (rsa == NULL) {
368 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
369 return 0;
370 }
371
David Benjamin4492a612017-08-02 17:16:31 -0400372 UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
373 if (!pkey ||
374 !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400375 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
376 return 0;
377 }
378
David Benjamin0ce090a2018-07-02 20:24:40 -0400379 return ssl_set_pkey(ctx->cert.get(), pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400380}
381
382int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
383 size_t der_len) {
David Benjamin4492a612017-08-02 17:16:31 -0400384 UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
385 if (!rsa) {
David Benjamin86e95b82017-07-18 16:34:25 -0400386 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
387 return 0;
388 }
389
David Benjamin4492a612017-08-02 17:16:31 -0400390 return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400391}
392
393int 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
David Benjamin0ce090a2018-07-02 20:24:40 -0400399 return ssl_set_pkey(ctx->cert.get(), pkey);
David Benjamin86e95b82017-07-18 16:34:25 -0400400}
401
402int 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;
David Benjamin4492a612017-08-02 17:16:31 -0400410 UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
411 if (!pkey || p != der + der_len) {
David Benjamin86e95b82017-07-18 16:34:25 -0400412 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
David Benjamin86e95b82017-07-18 16:34:25 -0400413 return 0;
414 }
415
David Benjamin4492a612017-08-02 17:16:31 -0400416 return SSL_CTX_use_PrivateKey(ctx, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400417}
418
419void SSL_set_private_key_method(SSL *ssl,
420 const SSL_PRIVATE_KEY_METHOD *key_method) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700421 if (!ssl->config) {
422 return;
423 }
424 ssl->config->cert->key_method = key_method;
David Benjamin86e95b82017-07-18 16:34:25 -0400425}
426
427void 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
Adam Langley826ce152018-08-03 10:31:21 -0700432static 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.
436static 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
David Benjamin6cc352e2017-11-02 17:21:39 -0400455const char *SSL_get_signature_algorithm_name(uint16_t sigalg,
456 int include_curve) {
Adam Langley826ce152018-08-03 10:31:21 -0700457 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 }
David Benjamin6cc352e2017-11-02 17:21:39 -0400466 }
Adam Langley826ce152018-08-03 10:31:21 -0700467
468 for (const auto &candidate : kSignatureAlgorithmNames) {
469 if (candidate.signature_algorithm == sigalg) {
470 return candidate.name;
471 }
472 }
473
474 return NULL;
David Benjamin6cc352e2017-11-02 17:21:39 -0400475}
476
477int 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
482const 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
490int 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
David Benjamin86e95b82017-07-18 16:34:25 -0400495int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
496 size_t num_prefs) {
David Benjamine325c3f2018-04-12 16:11:15 -0400497 return ctx->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
David Benjamin86e95b82017-07-18 16:34:25 -0400498}
499
David Benjamin86e95b82017-07-18 16:34:25 -0400500int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
501 size_t num_prefs) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700502 if (!ssl->config) {
503 return 0;
504 }
505 return ssl->config->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
David Benjamin86e95b82017-07-18 16:34:25 -0400506}
507
Adam Langley826ce152018-08-03 10:31:21 -0700508static 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
527static 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
561static 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) {
David Benjamin14c611c2019-01-21 20:33:09 +0000574 if (in_sigalgs.size() < 2) {
575 return true;
576 }
577
Adam Langley826ce152018-08-03 10:31:21 -0700578 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
595int 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
611int 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
631static 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
785int 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()) ||
David Benjaminf0a815c2020-02-03 20:03:52 -0500794 !SSL_CTX_set_verify_algorithm_prefs(ctx, sigalgs.data(),
795 sigalgs.size())) {
Adam Langley826ce152018-08-03 10:31:21 -0700796 return 0;
797 }
798
799 return 1;
800}
801
802int SSL_set1_sigalgs_list(SSL *ssl, const char *str) {
803 if (!ssl->config) {
804 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
805 return 0;
806 }
807
808 Array<uint16_t> sigalgs;
809 if (!parse_sigalgs_list(&sigalgs, str) ||
810 !sigalgs_unique(sigalgs)) {
811 return 0;
812 }
813
814 if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
David Benjaminf0a815c2020-02-03 20:03:52 -0500815 !SSL_set_verify_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size())) {
Adam Langley826ce152018-08-03 10:31:21 -0700816 return 0;
817 }
818
819 return 1;
820}
821
David Benjamin86e95b82017-07-18 16:34:25 -0400822int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
823 size_t num_prefs) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400824 return ctx->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
David Benjamin86e95b82017-07-18 16:34:25 -0400825}
David Benjaminf0a815c2020-02-03 20:03:52 -0500826
827int SSL_set_verify_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
828 size_t num_prefs) {
829 if (!ssl->config) {
830 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
831 return 0;
832 }
833
834 return ssl->config->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
835}