blob: 3e3fa94ba5fc551d4152be8301bc36aec35d87d1 [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 &&
Adam Langleyd04ca952017-02-28 11:26:51 -080087 /* 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) {
151 /* RSA keys may only be used with RSA-PSS. */
152 if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
153 return 0;
154 }
155
156 /* EC keys have a curve requirement. */
157 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) {
204 /* TODO(davidben): Remove support for |sign_digest|-only
205 * |SSL_PRIVATE_KEY_METHOD|s. */
206 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) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700275 /* Decrypt operations are only supported for RSA keys. */
276 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
277 return ssl_private_key_failure;
278 }
279
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700280 /* Decrypt with no padding. PKCS#1 padding will be removed as part
281 * of the 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 Benjamin69522112017-03-28 15:38:29 -0500295 /* 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 Benjamin69522112017-03-28 15:38:29 -0500307 /* Newer algorithms require message-based private keys.
308 * TODO(davidben): Remove this check when sign_digest is gone. */
309 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) {
323 EVP_PKEY *pkey;
324 int ret;
325
326 if (rsa == NULL) {
327 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
328 return 0;
329 }
330
331 pkey = EVP_PKEY_new();
332 if (pkey == NULL) {
333 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
334 return 0;
335 }
336
337 RSA_up_ref(rsa);
338 EVP_PKEY_assign_RSA(pkey, rsa);
339
340 ret = ssl_set_pkey(ssl->cert, pkey);
341 EVP_PKEY_free(pkey);
342
343 return ret;
344}
345
346int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
347 UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
348 if (!rsa) {
349 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
350 return 0;
351 }
352
353 return SSL_use_RSAPrivateKey(ssl, rsa.get());
354}
355
356int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
357 if (pkey == NULL) {
358 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
359 return 0;
360 }
361
362 return ssl_set_pkey(ssl->cert, pkey);
363}
364
365int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
366 size_t der_len) {
367 if (der_len > LONG_MAX) {
368 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
369 return 0;
370 }
371
372 const uint8_t *p = der;
373 EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
374 if (pkey == NULL || p != der + der_len) {
375 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
376 EVP_PKEY_free(pkey);
377 return 0;
378 }
379
380 int ret = SSL_use_PrivateKey(ssl, pkey);
381 EVP_PKEY_free(pkey);
382 return ret;
383}
384
385int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
386 int ret;
387 EVP_PKEY *pkey;
388
389 if (rsa == NULL) {
390 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
391 return 0;
392 }
393
394 pkey = EVP_PKEY_new();
395 if (pkey == NULL) {
396 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
397 return 0;
398 }
399
400 RSA_up_ref(rsa);
401 EVP_PKEY_assign_RSA(pkey, rsa);
402
403 ret = ssl_set_pkey(ctx->cert, pkey);
404 EVP_PKEY_free(pkey);
405 return ret;
406}
407
408int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
409 size_t der_len) {
410 RSA *rsa = RSA_private_key_from_bytes(der, der_len);
411 if (rsa == NULL) {
412 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
413 return 0;
414 }
415
416 int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
417 RSA_free(rsa);
418 return ret;
419}
420
421int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
422 if (pkey == NULL) {
423 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
424 return 0;
425 }
426
427 return ssl_set_pkey(ctx->cert, pkey);
428}
429
430int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
431 size_t der_len) {
432 if (der_len > LONG_MAX) {
433 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
434 return 0;
435 }
436
437 const uint8_t *p = der;
438 EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
439 if (pkey == NULL || p != der + der_len) {
440 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
441 EVP_PKEY_free(pkey);
442 return 0;
443 }
444
445 int ret = SSL_CTX_use_PrivateKey(ctx, pkey);
446 EVP_PKEY_free(pkey);
447 return ret;
448}
449
450void SSL_set_private_key_method(SSL *ssl,
451 const SSL_PRIVATE_KEY_METHOD *key_method) {
452 ssl->cert->key_method = key_method;
453}
454
455void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
456 const SSL_PRIVATE_KEY_METHOD *key_method) {
457 ctx->cert->key_method = key_method;
458}
459
460static int set_algorithm_prefs(uint16_t **out_prefs, size_t *out_num_prefs,
461 const uint16_t *prefs, size_t num_prefs) {
462 OPENSSL_free(*out_prefs);
463
464 *out_num_prefs = 0;
465 *out_prefs = (uint16_t *)BUF_memdup(prefs, num_prefs * sizeof(prefs[0]));
466 if (*out_prefs == NULL) {
467 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
468 return 0;
469 }
470 *out_num_prefs = num_prefs;
471
472 return 1;
473}
474
475int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
476 size_t num_prefs) {
477 return set_algorithm_prefs(&ctx->cert->sigalgs, &ctx->cert->num_sigalgs,
478 prefs, num_prefs);
479}
480
481
482int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
483 size_t num_prefs) {
484 return set_algorithm_prefs(&ssl->cert->sigalgs, &ssl->cert->num_sigalgs,
485 prefs, num_prefs);
486}
487
488int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
489 size_t num_prefs) {
490 return set_algorithm_prefs(&ctx->verify_sigalgs, &ctx->num_verify_sigalgs,
491 prefs, num_prefs);
492}
493
494int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
495 size_t num_digests) {
496 OPENSSL_free(ssl->cert->sigalgs);
497
498 static_assert(sizeof(int) >= 2 * sizeof(uint16_t),
499 "sigalgs allocation may overflow");
500
501 ssl->cert->num_sigalgs = 0;
502 ssl->cert->sigalgs =
503 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * 2 * num_digests);
504 if (ssl->cert->sigalgs == NULL) {
505 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
506 return 0;
507 }
508
509 /* Convert the digest list to a signature algorithms list.
510 *
511 * TODO(davidben): Replace this API with one that can express RSA-PSS, etc. */
512 for (size_t i = 0; i < num_digests; i++) {
513 switch (digest_nids[i]) {
514 case NID_sha1:
515 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA1;
516 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = SSL_SIGN_ECDSA_SHA1;
517 ssl->cert->num_sigalgs += 2;
518 break;
519 case NID_sha256:
520 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA256;
521 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
522 SSL_SIGN_ECDSA_SECP256R1_SHA256;
523 ssl->cert->num_sigalgs += 2;
524 break;
525 case NID_sha384:
526 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA384;
527 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
528 SSL_SIGN_ECDSA_SECP384R1_SHA384;
529 ssl->cert->num_sigalgs += 2;
530 break;
531 case NID_sha512:
532 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA512;
533 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
534 SSL_SIGN_ECDSA_SECP521R1_SHA512;
535 ssl->cert->num_sigalgs += 2;
536 break;
537 }
538 }
539
540 return 1;
541}