blob: 257d03e47b73327ff810dbc8e11354a478d70233 [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 Benjamind246b812016-07-08 15:07:02 -070067#include <openssl/type_check.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
Adam Langley52940c42017-02-01 12:40:31 -080073int ssl_is_key_type_supported(int key_type) {
David Benjamin69522112017-03-28 15:38:29 -050074 return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC ||
75 key_type == EVP_PKEY_ED25519;
David Benjamind1d80782015-07-05 11:54:09 -040076}
77
Adam Langley52940c42017-02-01 12:40:31 -080078static int ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) {
79 if (!ssl_is_key_type_supported(pkey->type)) {
80 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -080081 return 0;
82 }
83
Adam Langley52940c42017-02-01 12:40:31 -080084 if (cert->chain != NULL &&
85 sk_CRYPTO_BUFFER_value(cert->chain, 0) != NULL &&
Adam Langleyd04ca952017-02-28 11:26:51 -080086 /* Sanity-check that the private key and the certificate match. */
Adam Langley52940c42017-02-01 12:40:31 -080087 !ssl_cert_check_private_key(cert, pkey)) {
88 return 0;
89 }
90
91 EVP_PKEY_free(cert->privatekey);
92 EVP_PKEY_up_ref(pkey);
93 cert->privatekey = pkey;
94
95 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -080096}
97
98int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
99 EVP_PKEY *pkey;
100 int ret;
101
102 if (rsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400103 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800104 return 0;
105 }
106
Adam Langleyfcf25832014-12-18 17:42:32 -0800107 pkey = EVP_PKEY_new();
108 if (pkey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400109 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800110 return 0;
111 }
112
113 RSA_up_ref(rsa);
114 EVP_PKEY_assign_RSA(pkey, rsa);
115
116 ret = ssl_set_pkey(ssl->cert, pkey);
117 EVP_PKEY_free(pkey);
118
119 return ret;
120}
121
Adam Langleyfcf25832014-12-18 17:42:32 -0800122int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800123 if (pkey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400124 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800125 return 0;
126 }
127
Adam Langley3a2b47a2017-01-24 13:59:42 -0800128 return ssl_set_pkey(ssl->cert, pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800129}
130
David Benjamin3a596112015-11-12 09:25:30 -0800131int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
132 size_t der_len) {
133 if (der_len > LONG_MAX) {
134 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
Adam Langleyfcf25832014-12-18 17:42:32 -0800135 return 0;
136 }
137
David Benjamin3a596112015-11-12 09:25:30 -0800138 const uint8_t *p = der;
139 EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
140 if (pkey == NULL || p != der + der_len) {
141 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
142 EVP_PKEY_free(pkey);
143 return 0;
144 }
145
146 int ret = SSL_use_PrivateKey(ssl, pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 EVP_PKEY_free(pkey);
148 return ret;
149}
150
Adam Langleyfcf25832014-12-18 17:42:32 -0800151int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
152 int ret;
153 EVP_PKEY *pkey;
154
155 if (rsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400156 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 return 0;
158 }
159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 pkey = EVP_PKEY_new();
161 if (pkey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400162 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 return 0;
164 }
165
166 RSA_up_ref(rsa);
167 EVP_PKEY_assign_RSA(pkey, rsa);
168
169 ret = ssl_set_pkey(ctx->cert, pkey);
170 EVP_PKEY_free(pkey);
171 return ret;
172}
173
David Benjamin74f71102015-06-27 14:56:25 -0400174int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
175 size_t der_len) {
176 RSA *rsa = RSA_private_key_from_bytes(der, der_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 if (rsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400178 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 return 0;
180 }
181
David Benjamin74f71102015-06-27 14:56:25 -0400182 int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 RSA_free(rsa);
184 return ret;
185}
186
187int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
188 if (pkey == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400189 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 return 0;
191 }
192
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 return ssl_set_pkey(ctx->cert, pkey);
194}
195
David Benjamin3a596112015-11-12 09:25:30 -0800196int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
197 size_t der_len) {
198 if (der_len > LONG_MAX) {
199 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 return 0;
201 }
202
David Benjamin3a596112015-11-12 09:25:30 -0800203 const uint8_t *p = der;
204 EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
205 if (pkey == NULL || p != der + der_len) {
206 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
207 EVP_PKEY_free(pkey);
208 return 0;
209 }
210
211 int ret = SSL_CTX_use_PrivateKey(ctx, pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 EVP_PKEY_free(pkey);
213 return ret;
214}
215
David Benjaminb4d65fd2015-05-29 17:11:21 -0400216void SSL_set_private_key_method(SSL *ssl,
217 const SSL_PRIVATE_KEY_METHOD *key_method) {
218 ssl->cert->key_method = key_method;
219}
220
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030221void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
222 const SSL_PRIVATE_KEY_METHOD *key_method) {
223 ctx->cert->key_method = key_method;
224}
225
David Benjamin71c21b42017-04-14 17:05:40 -0400226static int set_algorithm_prefs(uint16_t **out_prefs, size_t *out_num_prefs,
227 const uint16_t *prefs, size_t num_prefs) {
228 OPENSSL_free(*out_prefs);
Daniel Bathgate89917a52016-10-18 14:31:02 +0100229
David Benjamin71c21b42017-04-14 17:05:40 -0400230 *out_num_prefs = 0;
231 *out_prefs = BUF_memdup(prefs, num_prefs * sizeof(prefs[0]));
232 if (*out_prefs == NULL) {
David Benjaminca3d5452016-07-14 12:51:01 -0400233 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
234 return 0;
235 }
David Benjamin71c21b42017-04-14 17:05:40 -0400236 *out_num_prefs = num_prefs;
David Benjaminca3d5452016-07-14 12:51:01 -0400237
238 return 1;
239}
240
David Benjamin0fc37ef2016-08-17 15:29:46 -0400241int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
242 size_t num_prefs) {
David Benjamin71c21b42017-04-14 17:05:40 -0400243 return set_algorithm_prefs(&ctx->cert->sigalgs, &ctx->cert->num_sigalgs,
244 prefs, num_prefs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400245}
246
247
248int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
249 size_t num_prefs) {
David Benjamin71c21b42017-04-14 17:05:40 -0400250 return set_algorithm_prefs(&ssl->cert->sigalgs, &ssl->cert->num_sigalgs,
251 prefs, num_prefs);
252}
253
254int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
255 size_t num_prefs) {
256 return set_algorithm_prefs(&ctx->verify_sigalgs, &ctx->num_verify_sigalgs,
257 prefs, num_prefs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400258}
259
Steven Valdez0d62f262015-09-04 12:41:04 -0400260int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
261 size_t num_digests) {
David Benjamind246b812016-07-08 15:07:02 -0700262 OPENSSL_free(ssl->cert->sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -0400263
David Benjamin5db7c9b2017-01-24 16:17:03 -0500264 OPENSSL_COMPILE_ASSERT(sizeof(int) >= 2 * sizeof(uint16_t),
265 digest_list_conversion_cannot_overflow);
266
David Benjamin0fc37ef2016-08-17 15:29:46 -0400267 ssl->cert->num_sigalgs = 0;
David Benjamind246b812016-07-08 15:07:02 -0700268 ssl->cert->sigalgs = OPENSSL_malloc(sizeof(uint16_t) * 2 * num_digests);
269 if (ssl->cert->sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -0400270 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
271 return 0;
272 }
273
David Benjamind246b812016-07-08 15:07:02 -0700274 /* Convert the digest list to a signature algorithms list.
275 *
276 * TODO(davidben): Replace this API with one that can express RSA-PSS, etc. */
277 for (size_t i = 0; i < num_digests; i++) {
278 switch (digest_nids[i]) {
279 case NID_sha1:
David Benjamin0fc37ef2016-08-17 15:29:46 -0400280 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA1;
281 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = SSL_SIGN_ECDSA_SHA1;
282 ssl->cert->num_sigalgs += 2;
David Benjamind246b812016-07-08 15:07:02 -0700283 break;
284 case NID_sha256:
David Benjamin0fc37ef2016-08-17 15:29:46 -0400285 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA256;
286 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
David Benjamind246b812016-07-08 15:07:02 -0700287 SSL_SIGN_ECDSA_SECP256R1_SHA256;
David Benjamin0fc37ef2016-08-17 15:29:46 -0400288 ssl->cert->num_sigalgs += 2;
David Benjamind246b812016-07-08 15:07:02 -0700289 break;
290 case NID_sha384:
David Benjamin0fc37ef2016-08-17 15:29:46 -0400291 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA384;
292 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
David Benjamind246b812016-07-08 15:07:02 -0700293 SSL_SIGN_ECDSA_SECP384R1_SHA384;
David Benjamin0fc37ef2016-08-17 15:29:46 -0400294 ssl->cert->num_sigalgs += 2;
David Benjamind246b812016-07-08 15:07:02 -0700295 break;
296 case NID_sha512:
David Benjamin0fc37ef2016-08-17 15:29:46 -0400297 ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA512;
298 ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
David Benjamind246b812016-07-08 15:07:02 -0700299 SSL_SIGN_ECDSA_SECP521R1_SHA512;
David Benjamin0fc37ef2016-08-17 15:29:46 -0400300 ssl->cert->num_sigalgs += 2;
David Benjamind246b812016-07-08 15:07:02 -0700301 break;
302 }
303 }
304
Steven Valdez0d62f262015-09-04 12:41:04 -0400305 return 1;
306}
307
David Benjamin6114c3c2017-03-30 16:37:54 -0500308typedef struct {
309 uint16_t sigalg;
310 int pkey_type;
311 int curve;
312 const EVP_MD *(*digest_func)(void);
313 char is_rsa_pss;
314} SSL_SIGNATURE_ALGORITHM;
315
316static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
317 {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, 0},
318 {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, 0},
319 {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 0},
320 {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 0},
321 {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 0},
322
323 {SSL_SIGN_RSA_PSS_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 1},
324 {SSL_SIGN_RSA_PSS_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 1},
325 {SSL_SIGN_RSA_PSS_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 1},
326
327 {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, 0},
328 {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
329 &EVP_sha256, 0},
330 {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
331 0},
332 {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
333 0},
David Benjamin69522112017-03-28 15:38:29 -0500334
335 {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, NULL, 0},
David Benjamin6114c3c2017-03-30 16:37:54 -0500336};
337
338static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) {
339 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) {
340 if (kSignatureAlgorithms[i].sigalg == sigalg) {
341 return &kSignatureAlgorithms[i];
342 }
343 }
344 return NULL;
345}
346
David Benjamin32a66d52016-07-13 22:03:11 -0400347int ssl_has_private_key(const SSL *ssl) {
nagendra modadugu601448a2015-07-24 09:31:31 -0700348 return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL;
349}
350
David Benjamin6114c3c2017-03-30 16:37:54 -0500351static int pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
352 uint16_t sigalg) {
353 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
354 if (alg == NULL ||
355 EVP_PKEY_id(pkey) != alg->pkey_type) {
356 return 0;
David Benjamind246b812016-07-08 15:07:02 -0700357 }
David Benjamin6114c3c2017-03-30 16:37:54 -0500358
359 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
360 /* RSA keys may only be used with RSA-PSS. */
361 if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
362 return 0;
363 }
364
365 /* EC keys have a curve requirement. */
366 if (alg->pkey_type == EVP_PKEY_EC &&
367 (alg->curve == NID_undef ||
368 EC_GROUP_get_curve_name(
369 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
370 return 0;
371 }
372 }
373
374 return 1;
David Benjamind246b812016-07-08 15:07:02 -0700375}
376
David Benjamin19670942017-05-31 19:07:31 -0400377static int setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, uint16_t sigalg,
378 int is_verify) {
379 if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500380 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
381 return 0;
David Benjamin887c3002016-07-08 16:15:32 -0700382 }
David Benjamina2d81f12016-07-08 15:42:16 -0700383
David Benjamin6114c3c2017-03-30 16:37:54 -0500384 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
David Benjamin19670942017-05-31 19:07:31 -0400385 const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL;
386 EVP_PKEY_CTX *pctx;
387 if (is_verify) {
388 if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) {
389 return 0;
390 }
391 } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
David Benjamin6114c3c2017-03-30 16:37:54 -0500392 return 0;
David Benjamina2d81f12016-07-08 15:42:16 -0700393 }
David Benjamina2d81f12016-07-08 15:42:16 -0700394
David Benjamin6114c3c2017-03-30 16:37:54 -0500395 if (alg->is_rsa_pss) {
David Benjamin19670942017-05-31 19:07:31 -0400396 if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
397 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400398 return 0;
David Benjamin76feb1f2017-03-28 14:17:01 -0500399 }
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400400 }
401
David Benjamin6114c3c2017-03-30 16:37:54 -0500402 return 1;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400403}
404
David Benjamin69522112017-03-28 15:38:29 -0500405static int legacy_sign_digest_supported(const SSL_SIGNATURE_ALGORITHM *alg) {
406 return (alg->pkey_type == EVP_PKEY_EC || alg->pkey_type == EVP_PKEY_RSA) &&
407 !alg->is_rsa_pss;
408}
409
David Benjamin44148742017-06-17 13:20:59 -0400410static enum ssl_private_key_result_t legacy_sign(
411 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, uint16_t sigalg,
412 const uint8_t *in, size_t in_len) {
413 /* TODO(davidben): Remove support for |sign_digest|-only
414 * |SSL_PRIVATE_KEY_METHOD|s. */
415 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
416 if (alg == NULL || !legacy_sign_digest_supported(alg)) {
417 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
418 return ssl_private_key_failure;
419 }
420
421 const EVP_MD *md = alg->digest_func();
422 uint8_t hash[EVP_MAX_MD_SIZE];
423 unsigned hash_len;
424 if (!EVP_Digest(in, in_len, hash, &hash_len, md, NULL)) {
425 return ssl_private_key_failure;
426 }
427
428 return ssl->cert->key_method->sign_digest(ssl, out, out_len, max_out, md,
429 hash, hash_len);
430}
431
David Benjaminb4d65fd2015-05-29 17:11:21 -0400432enum ssl_private_key_result_t ssl_private_key_sign(
David Benjamin44148742017-06-17 13:20:59 -0400433 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamin6114c3c2017-03-30 16:37:54 -0500434 uint16_t sigalg, const uint8_t *in, size_t in_len) {
David Benjamin44148742017-06-17 13:20:59 -0400435 SSL *const ssl = hs->ssl;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400436 if (ssl->cert->key_method != NULL) {
David Benjamin44148742017-06-17 13:20:59 -0400437 enum ssl_private_key_result_t ret;
438 if (hs->pending_private_key_op) {
439 ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
440 } else {
441 ret = (ssl->cert->key_method->sign != NULL
442 ? ssl->cert->key_method->sign
443 : legacy_sign)(ssl, out, out_len, max_out, sigalg, in, in_len);
David Benjamind3440b42016-07-14 14:52:41 -0400444 }
David Benjamin44148742017-06-17 13:20:59 -0400445 hs->pending_private_key_op = ret == ssl_private_key_retry;
446 return ret;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400447 }
448
David Benjamin76feb1f2017-03-28 14:17:01 -0500449 *out_len = max_out;
David Benjamin19670942017-05-31 19:07:31 -0400450 EVP_MD_CTX ctx;
451 EVP_MD_CTX_init(&ctx);
452 int ret = setup_ctx(ssl, &ctx, ssl->cert->privatekey, sigalg, 0 /* sign */) &&
453 EVP_DigestSign(&ctx, out, out_len, in, in_len);
454 EVP_MD_CTX_cleanup(&ctx);
David Benjamin76feb1f2017-03-28 14:17:01 -0500455 return ret ? ssl_private_key_success : ssl_private_key_failure;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400456}
457
Steven Valdez2b8415e2016-06-30 13:27:23 -0400458int ssl_public_key_verify(SSL *ssl, const uint8_t *signature,
David Benjamin19670942017-05-31 19:07:31 -0400459 size_t signature_len, uint16_t sigalg, EVP_PKEY *pkey,
460 const uint8_t *in, size_t in_len) {
461 EVP_MD_CTX ctx;
462 EVP_MD_CTX_init(&ctx);
463 int ret = setup_ctx(ssl, &ctx, pkey, sigalg, 1 /* verify */) &&
464 EVP_DigestVerify(&ctx, signature, signature_len, in, in_len);
465 EVP_MD_CTX_cleanup(&ctx);
David Benjamin76feb1f2017-03-28 14:17:01 -0500466 return ret;
Steven Valdez2b8415e2016-06-30 13:27:23 -0400467}
468
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700469enum ssl_private_key_result_t ssl_private_key_decrypt(
David Benjamin44148742017-06-17 13:20:59 -0400470 SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700471 const uint8_t *in, size_t in_len) {
David Benjamin44148742017-06-17 13:20:59 -0400472 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700473 if (ssl->cert->key_method != NULL) {
David Benjamin44148742017-06-17 13:20:59 -0400474 enum ssl_private_key_result_t ret;
475 if (hs->pending_private_key_op) {
476 ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
477 } else {
478 ret = ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in,
479 in_len);
480 }
481 hs->pending_private_key_op = ret == ssl_private_key_retry;
482 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700483 }
484
David Benjamin758d1272015-11-20 17:47:25 -0500485 RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey);
486 if (rsa == NULL) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700487 /* Decrypt operations are only supported for RSA keys. */
488 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
489 return ssl_private_key_failure;
490 }
491
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700492 /* Decrypt with no padding. PKCS#1 padding will be removed as part
493 * of the timing-sensitive code by the caller. */
David Benjamin758d1272015-11-20 17:47:25 -0500494 if (!RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) {
495 return ssl_private_key_failure;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700496 }
David Benjamin758d1272015-11-20 17:47:25 -0500497 return ssl_private_key_success;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700498}
499
David Benjamina232a712017-03-30 15:51:53 -0500500int ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
David Benjamin6114c3c2017-03-30 16:37:54 -0500501 uint16_t sigalg) {
David Benjamina232a712017-03-30 15:51:53 -0500502 SSL *const ssl = hs->ssl;
David Benjamin6114c3c2017-03-30 16:37:54 -0500503 if (!pkey_supports_algorithm(ssl, hs->local_pubkey, sigalg)) {
504 return 0;
David Benjamin1fb125c2016-07-08 18:52:12 -0700505 }
506
David Benjamin69522112017-03-28 15:38:29 -0500507 /* Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
508 * emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
509 * hash in TLS. Reasonable RSA key sizes are large enough for the largest
510 * defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for
511 * SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the
512 * size so that we can fall back to another algorithm in that case. */
David Benjamin6114c3c2017-03-30 16:37:54 -0500513 const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
David Benjamin69522112017-03-28 15:38:29 -0500514 if (alg->is_rsa_pss &&
515 (size_t)EVP_PKEY_size(hs->local_pubkey) <
516 2 * EVP_MD_size(alg->digest_func()) + 2) {
517 return 0;
518 }
David Benjamin7944a9f2016-07-12 22:27:01 -0400519
David Benjamin69522112017-03-28 15:38:29 -0500520 /* Newer algorithms require message-based private keys.
521 * TODO(davidben): Remove this check when sign_digest is gone. */
522 if (ssl->cert->key_method != NULL &&
523 ssl->cert->key_method->sign == NULL &&
524 !legacy_sign_digest_supported(alg)) {
525 return 0;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400526 }
527
David Benjamin6114c3c2017-03-30 16:37:54 -0500528 return 1;
David Benjamin1fb125c2016-07-08 18:52:12 -0700529}