blob: d275561a861d7df2a81045dc43d281465287a9d5 [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
David Benjamin86e95b82017-07-18 16:34:25 -040072namespace bssl {
73
Adam Langley52940c42017-02-01 12:40:31 -080074int 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
Adam Langley52940c42017-02-01 12:40:31 -080079static int 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);
Adam Langleyfcf25832014-12-18 17:42:32 -080082 return 0;
83 }
84
Adam Langley52940c42017-02-01 12:40:31 -080085 if (cert->chain != NULL &&
86 sk_CRYPTO_BUFFER_value(cert->chain, 0) != NULL &&
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)) {
89 return 0;
90 }
91
92 EVP_PKEY_free(cert->privatekey);
93 EVP_PKEY_up_ref(pkey);
94 cert->privatekey = pkey;
95
96 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -080097}
98
David Benjamin6114c3c2017-03-30 16:37:54 -050099typedef struct {
100 uint16_t sigalg;
101 int pkey_type;
102 int curve;
103 const EVP_MD *(*digest_func)(void);
104 char is_rsa_pss;
105} SSL_SIGNATURE_ALGORITHM;
106
107static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
108 {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, 0},
109 {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, 0},
110 {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 0},
111 {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 0},
112 {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 0},
113
114 {SSL_SIGN_RSA_PSS_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 1},
115 {SSL_SIGN_RSA_PSS_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 1},
116 {SSL_SIGN_RSA_PSS_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 1},
117
118 {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, 0},
119 {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
120 &EVP_sha256, 0},
121 {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
122 0},
123 {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
124 0},
David Benjamin69522112017-03-28 15:38:29 -0500125
126 {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, NULL, 0},
David Benjamin6114c3c2017-03-30 16:37:54 -0500127};
128
129static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) {
130 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) {
131 if (kSignatureAlgorithms[i].sigalg == sigalg) {
132 return &kSignatureAlgorithms[i];
133 }
134 }
135 return NULL;
136}
137
David Benjamin32a66d52016-07-13 22:03:11 -0400138int ssl_has_private_key(const SSL *ssl) {
nagendra modadugu601448a2015-07-24 09:31:31 -0700139 return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL;
140}
141
David Benjamin6114c3c2017-03-30 16:37:54 -0500142static int pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
143 uint16_t sigalg) {
144 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
145 if (alg == NULL ||
146 EVP_PKEY_id(pkey) != alg->pkey_type) {
147 return 0;
David Benjamind246b812016-07-08 15:07:02 -0700148 }
David Benjamin6114c3c2017-03-30 16:37:54 -0500149
150 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400151 // RSA keys may only be used with RSA-PSS.
David Benjamin6114c3c2017-03-30 16:37:54 -0500152 if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
153 return 0;
154 }
155
David Benjaminc11ea9422017-08-29 16:33:21 -0400156 // EC keys have a curve requirement.
David Benjamin6114c3c2017-03-30 16:37:54 -0500157 if (alg->pkey_type == EVP_PKEY_EC &&
158 (alg->curve == NID_undef ||
159 EC_GROUP_get_curve_name(
160 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
161 return 0;
162 }
163 }
164
165 return 1;
David Benjamind246b812016-07-08 15:07:02 -0700166}
167
David Benjamin19670942017-05-31 19:07:31 -0400168static int setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, uint16_t sigalg,
169 int is_verify) {
170 if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500171 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
172 return 0;
David Benjamin887c3002016-07-08 16:15:32 -0700173 }
David Benjamina2d81f12016-07-08 15:42:16 -0700174
David Benjamin6114c3c2017-03-30 16:37:54 -0500175 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
David Benjamin19670942017-05-31 19:07:31 -0400176 const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL;
177 EVP_PKEY_CTX *pctx;
178 if (is_verify) {
179 if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) {
180 return 0;
181 }
182 } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500183 return 0;
David Benjamina2d81f12016-07-08 15:42:16 -0700184 }
David Benjamina2d81f12016-07-08 15:42:16 -0700185
David Benjamin6114c3c2017-03-30 16:37:54 -0500186 if (alg->is_rsa_pss) {
David Benjamin19670942017-05-31 19:07:31 -0400187 if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
188 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400189 return 0;
David Benjamin76feb1f2017-03-28 14:17:01 -0500190 }
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400191 }
192
David Benjamin6114c3c2017-03-30 16:37:54 -0500193 return 1;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400194}
195
David Benjamin69522112017-03-28 15:38:29 -0500196static int legacy_sign_digest_supported(const SSL_SIGNATURE_ALGORITHM *alg) {
197 return (alg->pkey_type == EVP_PKEY_EC || alg->pkey_type == EVP_PKEY_RSA) &&
198 !alg->is_rsa_pss;
199}
200
David Benjamin44148742017-06-17 13:20:59 -0400201static enum ssl_private_key_result_t legacy_sign(
202 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, uint16_t sigalg,
203 const uint8_t *in, size_t in_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400204 // TODO(davidben): Remove support for |sign_digest|-only
205 // |SSL_PRIVATE_KEY_METHOD|s.
David Benjamin44148742017-06-17 13:20:59 -0400206 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
207 if (alg == NULL || !legacy_sign_digest_supported(alg)) {
208 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
209 return ssl_private_key_failure;
210 }
211
212 const EVP_MD *md = alg->digest_func();
213 uint8_t hash[EVP_MAX_MD_SIZE];
214 unsigned hash_len;
215 if (!EVP_Digest(in, in_len, hash, &hash_len, md, NULL)) {
216 return ssl_private_key_failure;
217 }
218
219 return ssl->cert->key_method->sign_digest(ssl, out, out_len, max_out, md,
220 hash, hash_len);
221}
222
David Benjaminb4d65fd2015-05-29 17:11:21 -0400223enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -0400224 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin6114c3c2017-03-30 16:37:54 -0500225 uint16_t sigalg, const uint8_t *in, size_t in_len) {
David Benjamin44148742017-06-17 13:20:59 -0400226 SSL *const ssl = hs->ssl;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400227 if (ssl->cert->key_method != NULL) {
David Benjamin44148742017-06-17 13:20:59 -0400228 enum ssl_private_key_result_t ret;
229 if (hs->pending_private_key_op) {
230 ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
231 } else {
232 ret = (ssl->cert->key_method->sign != NULL
233 ? ssl->cert->key_method->sign
234 : legacy_sign)(ssl, out, out_len, max_out, sigalg, in, in_len);
David Benjamind3440b42016-07-14 14:52:41 -0400235 }
David Benjamin44148742017-06-17 13:20:59 -0400236 hs->pending_private_key_op = ret == ssl_private_key_retry;
237 return ret;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400238 }
239
David Benjamin76feb1f2017-03-28 14:17:01 -0500240 *out_len = max_out;
David Benjamin1386aad2017-07-19 23:57:40 -0400241 ScopedEVP_MD_CTX ctx;
242 if (!setup_ctx(ssl, ctx.get(), ssl->cert->privatekey, sigalg, 0 /* sign */) ||
243 !EVP_DigestSign(ctx.get(), out, out_len, in, in_len)) {
244 return ssl_private_key_failure;
245 }
246 return ssl_private_key_success;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400247}
248
Steven Valdez2b8415e2016-06-30 13:27:23 -0400249int ssl_public_key_verify(SSL *ssl, const uint8_t *signature,
David Benjamin19670942017-05-31 19:07:31 -0400250 size_t signature_len, uint16_t sigalg, EVP_PKEY *pkey,
251 const uint8_t *in, size_t in_len) {
David Benjamin1386aad2017-07-19 23:57:40 -0400252 ScopedEVP_MD_CTX ctx;
253 return setup_ctx(ssl, ctx.get(), pkey, sigalg, 1 /* verify */) &&
254 EVP_DigestVerify(ctx.get(), signature, signature_len, in, in_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -0400255}
256
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700257enum ssl_private_key_result_t ssl_private_key_decrypt(
David Benjamin44148742017-06-17 13:20:59 -0400258 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700259 const uint8_t *in, size_t in_len) {
David Benjamin44148742017-06-17 13:20:59 -0400260 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700261 if (ssl->cert->key_method != NULL) {
David Benjamin44148742017-06-17 13:20:59 -0400262 enum ssl_private_key_result_t ret;
263 if (hs->pending_private_key_op) {
264 ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
265 } else {
266 ret = ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in,
267 in_len);
268 }
269 hs->pending_private_key_op = ret == ssl_private_key_retry;
270 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700271 }
272
David Benjamin758d1272015-11-20 17:47:25 -0500273 RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey);
274 if (rsa == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400275 // Decrypt operations are only supported for RSA keys.
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700276 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
277 return ssl_private_key_failure;
278 }
279
David Benjaminc11ea9422017-08-29 16:33:21 -0400280 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
281 // timing-sensitive code by the caller.
David Benjamin758d1272015-11-20 17:47:25 -0500282 if (!RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) {
283 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 Benjamina232a712017-03-30 15:51:53 -0500288int ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
David Benjamin6114c3c2017-03-30 16:37:54 -0500289 uint16_t sigalg) {
David Benjamina232a712017-03-30 15:51:53 -0500290 SSL *const ssl = hs->ssl;
David Benjamin31b0c9b2017-07-20 14:49:15 -0400291 if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500292 return 0;
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 Benjamin69522112017-03-28 15:38:29 -0500304 return 0;
305 }
David Benjamin7944a9f2016-07-12 22:27:01 -0400306
David Benjaminc11ea9422017-08-29 16:33:21 -0400307 // Newer algorithms require message-based private keys.
308 // TODO(davidben): Remove this check when sign_digest is gone.
David Benjamin69522112017-03-28 15:38:29 -0500309 if (ssl->cert->key_method != NULL &&
310 ssl->cert->key_method->sign == NULL &&
311 !legacy_sign_digest_supported(alg)) {
312 return 0;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400313 }
314
David Benjamin6114c3c2017-03-30 16:37:54 -0500315 return 1;
David Benjamin1fb125c2016-07-08 18:52:12 -0700316}
David Benjamin86e95b82017-07-18 16:34:25 -0400317
318} // namespace bssl
319
320using namespace bssl;
321
322int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
David Benjamin86e95b82017-07-18 16:34:25 -0400323 if (rsa == NULL) {
324 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
325 return 0;
326 }
327
David Benjamin4492a612017-08-02 17:16:31 -0400328 UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
329 if (!pkey ||
330 !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400331 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
332 return 0;
333 }
334
David Benjamin4492a612017-08-02 17:16:31 -0400335 return ssl_set_pkey(ssl->cert, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400336}
337
338int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
339 UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
340 if (!rsa) {
341 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
342 return 0;
343 }
344
345 return SSL_use_RSAPrivateKey(ssl, rsa.get());
346}
347
348int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
349 if (pkey == NULL) {
350 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
351 return 0;
352 }
353
354 return ssl_set_pkey(ssl->cert, pkey);
355}
356
357int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
358 size_t der_len) {
359 if (der_len > LONG_MAX) {
360 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
361 return 0;
362 }
363
364 const uint8_t *p = der;
David Benjamin4492a612017-08-02 17:16:31 -0400365 UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
366 if (!pkey || p != der + der_len) {
David Benjamin86e95b82017-07-18 16:34:25 -0400367 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
David Benjamin86e95b82017-07-18 16:34:25 -0400368 return 0;
369 }
370
David Benjamin4492a612017-08-02 17:16:31 -0400371 return SSL_use_PrivateKey(ssl, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400372}
373
374int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
David Benjamin86e95b82017-07-18 16:34:25 -0400375 if (rsa == NULL) {
376 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
377 return 0;
378 }
379
David Benjamin4492a612017-08-02 17:16:31 -0400380 UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
381 if (!pkey ||
382 !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
David Benjamin86e95b82017-07-18 16:34:25 -0400383 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
384 return 0;
385 }
386
David Benjamin4492a612017-08-02 17:16:31 -0400387 return ssl_set_pkey(ctx->cert, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400388}
389
390int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
391 size_t der_len) {
David Benjamin4492a612017-08-02 17:16:31 -0400392 UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
393 if (!rsa) {
David Benjamin86e95b82017-07-18 16:34:25 -0400394 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
395 return 0;
396 }
397
David Benjamin4492a612017-08-02 17:16:31 -0400398 return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400399}
400
401int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
402 if (pkey == NULL) {
403 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
404 return 0;
405 }
406
407 return ssl_set_pkey(ctx->cert, pkey);
408}
409
410int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
411 size_t der_len) {
412 if (der_len > LONG_MAX) {
413 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
414 return 0;
415 }
416
417 const uint8_t *p = der;
David Benjamin4492a612017-08-02 17:16:31 -0400418 UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
419 if (!pkey || p != der + der_len) {
David Benjamin86e95b82017-07-18 16:34:25 -0400420 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
David Benjamin86e95b82017-07-18 16:34:25 -0400421 return 0;
422 }
423
David Benjamin4492a612017-08-02 17:16:31 -0400424 return SSL_CTX_use_PrivateKey(ctx, pkey.get());
David Benjamin86e95b82017-07-18 16:34:25 -0400425}
426
427void SSL_set_private_key_method(SSL *ssl,
428 const SSL_PRIVATE_KEY_METHOD *key_method) {
429 ssl->cert->key_method = key_method;
430}
431
432void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
433 const SSL_PRIVATE_KEY_METHOD *key_method) {
434 ctx->cert->key_method = key_method;
435}
436
437static int set_algorithm_prefs(uint16_t **out_prefs, size_t *out_num_prefs,
438 const uint16_t *prefs, size_t num_prefs) {
439 OPENSSL_free(*out_prefs);
440
441 *out_num_prefs = 0;
442 *out_prefs = (uint16_t *)BUF_memdup(prefs, num_prefs * sizeof(prefs[0]));
443 if (*out_prefs == NULL) {
444 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
445 return 0;
446 }
447 *out_num_prefs = num_prefs;
448
449 return 1;
450}
451
452int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
453 size_t num_prefs) {
454 return set_algorithm_prefs(&ctx->cert->sigalgs, &ctx->cert->num_sigalgs,
455 prefs, num_prefs);
456}
457
458
459int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
460 size_t num_prefs) {
461 return set_algorithm_prefs(&ssl->cert->sigalgs, &ssl->cert->num_sigalgs,
462 prefs, num_prefs);
463}
464
465int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
466 size_t num_prefs) {
467 return set_algorithm_prefs(&ctx->verify_sigalgs, &ctx->num_verify_sigalgs,
468 prefs, num_prefs);
469}
470
471int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
472 size_t num_digests) {
473 OPENSSL_free(ssl->cert->sigalgs);
474
475 static_assert(sizeof(int) >= 2 * sizeof(uint16_t),
476 "sigalgs allocation may overflow");
477
478 ssl->cert->num_sigalgs = 0;
479 ssl->cert->sigalgs =
480 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * 2 * num_digests);
481 if (ssl->cert->sigalgs == NULL) {
482 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
483 return 0;
484 }
485
David Benjaminc11ea9422017-08-29 16:33:21 -0400486 // Convert the digest list to a signature algorithms list.
487 //
488 // TODO(davidben): Replace this API with one that can express RSA-PSS, etc.
David Benjamin86e95b82017-07-18 16:34:25 -0400489 for (size_t i = 0; i < num_digests; i++) {
490 switch (digest_nids[i]) {
491 case NID_sha1:
492 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA1;
493 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = SSL_SIGN_ECDSA_SHA1;
494 ssl->cert->num_sigalgs += 2;
495 break;
496 case NID_sha256:
497 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA256;
498 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
499 SSL_SIGN_ECDSA_SECP256R1_SHA256;
500 ssl->cert->num_sigalgs += 2;
501 break;
502 case NID_sha384:
503 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA384;
504 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
505 SSL_SIGN_ECDSA_SECP384R1_SHA384;
506 ssl->cert->num_sigalgs += 2;
507 break;
508 case NID_sha512:
509 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA512;
510 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
511 SSL_SIGN_ECDSA_SECP521R1_SHA512;
512 ssl->cert->num_sigalgs += 2;
513 break;
514 }
515 }
516
517 return 1;
518}