blob: 60fdf64edb61097fff88844d49a5adcd22bc993d [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
57#include <openssl/evp.h>
58
59#include <assert.h>
60#include <string.h>
61
Adam Langleyd9e397b2015-01-22 14:27:53 -080062#include <openssl/dsa.h>
63#include <openssl/ec.h>
64#include <openssl/err.h>
65#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -040066#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080067#include <openssl/rsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -070068#include <openssl/thread.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080069
70#include "internal.h"
Adam Langleyf4e42722015-06-04 17:45:09 -070071#include "../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080072
73
Pete Bentleyebd3e962019-09-26 10:51:12 +010074// Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|.
75//
76// TODO(davidben): Fix Node to not touch the error queue itself and remove this.
77OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH)
78
Adam Langleyd9e397b2015-01-22 14:27:53 -080079EVP_PKEY *EVP_PKEY_new(void) {
80 EVP_PKEY *ret;
81
82 ret = OPENSSL_malloc(sizeof(EVP_PKEY));
83 if (ret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +000084 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -080085 return NULL;
86 }
87
Robert Sloan69939df2017-01-09 10:53:07 -080088 OPENSSL_memset(ret, 0, sizeof(EVP_PKEY));
Adam Langleyd9e397b2015-01-22 14:27:53 -080089 ret->type = EVP_PKEY_NONE;
90 ret->references = 1;
91
92 return ret;
93}
94
95static void free_it(EVP_PKEY *pkey) {
96 if (pkey->ameth && pkey->ameth->pkey_free) {
97 pkey->ameth->pkey_free(pkey);
98 pkey->pkey.ptr = NULL;
99 pkey->type = EVP_PKEY_NONE;
100 }
101}
102
103void EVP_PKEY_free(EVP_PKEY *pkey) {
104 if (pkey == NULL) {
105 return;
106 }
107
Adam Langleyf4e42722015-06-04 17:45:09 -0700108 if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800109 return;
110 }
111
112 free_it(pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800113 OPENSSL_free(pkey);
114}
115
David Benjaminc895d6b2016-08-11 13:26:41 -0400116int EVP_PKEY_up_ref(EVP_PKEY *pkey) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700117 CRYPTO_refcount_inc(&pkey->references);
David Benjaminc895d6b2016-08-11 13:26:41 -0400118 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -0700119}
120
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
122 if (pkey->ameth && pkey->ameth->pkey_opaque) {
123 return pkey->ameth->pkey_opaque(pkey);
124 }
125 return 0;
126}
127
Adam Langleyd9e397b2015-01-22 14:27:53 -0800128int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
129 if (a->type != b->type) {
130 return -1;
131 }
132
133 if (a->ameth) {
134 int ret;
Robert Sloan8f860b12017-08-28 07:37:06 -0700135 // Compare parameters if the algorithm has them
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136 if (a->ameth->param_cmp) {
137 ret = a->ameth->param_cmp(a, b);
Adam Langleye9ada862015-05-11 17:20:37 -0700138 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139 return ret;
Adam Langleye9ada862015-05-11 17:20:37 -0700140 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800141 }
142
143 if (a->ameth->pub_cmp) {
144 return a->ameth->pub_cmp(a, b);
145 }
146 }
147
148 return -2;
149}
150
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
152 if (to->type != from->type) {
Kenny Rootb8494592015-09-25 02:29:14 +0000153 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154 goto err;
155 }
156
157 if (EVP_PKEY_missing_parameters(from)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000158 OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159 goto err;
160 }
161
162 if (from->ameth && from->ameth->param_copy) {
163 return from->ameth->param_copy(to, from);
164 }
165
166err:
167 return 0;
168}
169
170int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
171 if (pkey->ameth && pkey->ameth->param_missing) {
172 return pkey->ameth->param_missing(pkey);
173 }
174 return 0;
175}
176
177int EVP_PKEY_size(const EVP_PKEY *pkey) {
178 if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
179 return pkey->ameth->pkey_size(pkey);
180 }
181 return 0;
182}
183
Robert Sloan1f278ae2018-09-04 13:56:45 -0700184int EVP_PKEY_bits(const EVP_PKEY *pkey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800185 if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
186 return pkey->ameth->pkey_bits(pkey);
187 }
188 return 0;
189}
190
191int EVP_PKEY_id(const EVP_PKEY *pkey) {
192 return pkey->type;
193}
194
Robert Sloan8f860b12017-08-28 07:37:06 -0700195// evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which
196// should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
197// unknown.
David Benjamin4969cc92016-04-22 15:02:23 -0400198static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800199 switch (nid) {
200 case EVP_PKEY_RSA:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800201 return &rsa_asn1_meth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202 case EVP_PKEY_EC:
203 return &ec_asn1_meth;
Adam Langleye9ada862015-05-11 17:20:37 -0700204 case EVP_PKEY_DSA:
205 return &dsa_asn1_meth;
Robert Sloan572a4e22017-04-17 10:52:19 -0700206 case EVP_PKEY_ED25519:
207 return &ed25519_asn1_meth;
Pete Bentley0c61efe2019-08-13 09:32:23 +0100208 case EVP_PKEY_X25519:
209 return &x25519_asn1_meth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 default:
211 return NULL;
212 }
213}
214
215int EVP_PKEY_type(int nid) {
David Benjamin4969cc92016-04-22 15:02:23 -0400216 const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(nid);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800217 if (meth == NULL) {
218 return NID_undef;
219 }
220 return meth->pkey_id;
221}
222
Adam Langleyd9e397b2015-01-22 14:27:53 -0800223int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
224 if (EVP_PKEY_assign_RSA(pkey, key)) {
225 RSA_up_ref(key);
226 return 1;
227 }
228 return 0;
229}
230
231int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
232 return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
233}
234
Robert Sloan1f278ae2018-09-04 13:56:45 -0700235RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 if (pkey->type != EVP_PKEY_RSA) {
Kenny Rootb8494592015-09-25 02:29:14 +0000237 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 return NULL;
239 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 return pkey->pkey.rsa;
241}
242
Robert Sloan1f278ae2018-09-04 13:56:45 -0700243RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
Adam Langley4139edb2016-01-13 15:00:54 -0800244 RSA *rsa = EVP_PKEY_get0_RSA(pkey);
245 if (rsa != NULL) {
246 RSA_up_ref(rsa);
247 }
248 return rsa;
249}
250
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
252 if (EVP_PKEY_assign_DSA(pkey, key)) {
253 DSA_up_ref(key);
254 return 1;
255 }
256 return 0;
257}
258
259int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
260 return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
261}
262
Robert Sloan1f278ae2018-09-04 13:56:45 -0700263DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 if (pkey->type != EVP_PKEY_DSA) {
Kenny Rootb8494592015-09-25 02:29:14 +0000265 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266 return NULL;
267 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800268 return pkey->pkey.dsa;
269}
270
Robert Sloan1f278ae2018-09-04 13:56:45 -0700271DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey) {
Adam Langley4139edb2016-01-13 15:00:54 -0800272 DSA *dsa = EVP_PKEY_get0_DSA(pkey);
273 if (dsa != NULL) {
274 DSA_up_ref(dsa);
275 }
276 return dsa;
277}
278
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
280 if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
281 EC_KEY_up_ref(key);
282 return 1;
283 }
284 return 0;
285}
286
287int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
288 return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
289}
290
Robert Sloan1f278ae2018-09-04 13:56:45 -0700291EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800292 if (pkey->type != EVP_PKEY_EC) {
Kenny Rootb8494592015-09-25 02:29:14 +0000293 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800294 return NULL;
295 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 return pkey->pkey.ec;
297}
298
Robert Sloan1f278ae2018-09-04 13:56:45 -0700299EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) {
Adam Langley4139edb2016-01-13 15:00:54 -0800300 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
301 if (ec_key != NULL) {
302 EC_KEY_up_ref(ec_key);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800303 }
Adam Langley4139edb2016-01-13 15:00:54 -0800304 return ec_key;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800305}
306
Robert Sloan1f278ae2018-09-04 13:56:45 -0700307DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) { return NULL; }
308DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey) { return NULL; }
David Benjaminc895d6b2016-08-11 13:26:41 -0400309
Adam Langleyd9e397b2015-01-22 14:27:53 -0800310int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
311 if (!EVP_PKEY_set_type(pkey, type)) {
312 return 0;
313 }
314 pkey->pkey.ptr = key;
315 return key != NULL;
316}
317
Adam Langleyd9e397b2015-01-22 14:27:53 -0800318int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
319 const EVP_PKEY_ASN1_METHOD *ameth;
320
321 if (pkey && pkey->pkey.ptr) {
322 free_it(pkey);
323 }
324
David Benjamin4969cc92016-04-22 15:02:23 -0400325 ameth = evp_pkey_asn1_find(type);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800326 if (ameth == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000327 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
David Benjamin4969cc92016-04-22 15:02:23 -0400328 ERR_add_error_dataf("algorithm %d", type);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329 return 0;
330 }
331
332 if (pkey) {
333 pkey->ameth = ameth;
334 pkey->type = pkey->ameth->pkey_id;
335 }
336
337 return 1;
338}
339
Pete Bentley0c61efe2019-08-13 09:32:23 +0100340EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,
341 const uint8_t *in, size_t len) {
342 EVP_PKEY *ret = EVP_PKEY_new();
343 if (ret == NULL ||
344 !EVP_PKEY_set_type(ret, type)) {
345 goto err;
346 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800347
Pete Bentley0c61efe2019-08-13 09:32:23 +0100348 if (ret->ameth->set_priv_raw == NULL) {
349 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
350 goto err;
351 }
352
353 if (!ret->ameth->set_priv_raw(ret, in, len)) {
354 goto err;
355 }
356
357 return ret;
358
359err:
360 EVP_PKEY_free(ret);
361 return NULL;
362}
363
364EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused,
365 const uint8_t *in, size_t len) {
366 EVP_PKEY *ret = EVP_PKEY_new();
367 if (ret == NULL ||
368 !EVP_PKEY_set_type(ret, type)) {
369 goto err;
370 }
371
372 if (ret->ameth->set_pub_raw == NULL) {
373 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
374 goto err;
375 }
376
377 if (!ret->ameth->set_pub_raw(ret, in, len)) {
378 goto err;
379 }
380
381 return ret;
382
383err:
384 EVP_PKEY_free(ret);
385 return NULL;
386}
387
388int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out,
389 size_t *out_len) {
390 if (pkey->ameth->get_priv_raw == NULL) {
391 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
392 return 0;
393 }
394
395 return pkey->ameth->get_priv_raw(pkey, out, out_len);
396}
397
398int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out,
399 size_t *out_len) {
400 if (pkey->ameth->get_pub_raw == NULL) {
401 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
402 return 0;
403 }
404
405 return pkey->ameth->get_pub_raw(pkey, out, out_len);
406}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800407
408int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
409 if (a->type != b->type) {
410 return -1;
411 }
412 if (a->ameth && a->ameth->param_cmp) {
413 return a->ameth->param_cmp(a, b);
414 }
415 return -2;
416}
417
Adam Langleyd9e397b2015-01-22 14:27:53 -0800418int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
419 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
420 (void *)md);
421}
422
423int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
424 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
425 0, (void *)out_md);
426}
427
Adam Langleyd9e397b2015-01-22 14:27:53 -0800428void OpenSSL_add_all_algorithms(void) {}
429
David Benjaminc895d6b2016-08-11 13:26:41 -0400430void OPENSSL_add_all_algorithms_conf(void) {}
David Benjamin6e899c72016-06-09 18:02:18 -0400431
Adam Langleyf4e42722015-06-04 17:45:09 -0700432void OpenSSL_add_all_ciphers(void) {}
433
434void OpenSSL_add_all_digests(void) {}
435
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436void EVP_cleanup(void) {}
Pete Bentley0c61efe2019-08-13 09:32:23 +0100437
438int EVP_PKEY_base_id(const EVP_PKEY *pkey) {
439 // OpenSSL has two notions of key type because it supports multiple OIDs for
440 // the same algorithm: NID_rsa vs NID_rsaEncryption and five distinct spelling
441 // of DSA. We do not support these, so the base ID is simply the ID.
442 return EVP_PKEY_id(pkey);
443}