xref: /openbsd-src/lib/libcrypto/evp/p_lib.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 /* $OpenBSD: p_lib.c,v 1.37 2023/09/10 17:32:17 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/opensslconf.h>
62 
63 #include <openssl/bn.h>
64 #include <openssl/cmac.h>
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/objects.h>
68 #include <openssl/x509.h>
69 
70 #ifndef OPENSSL_NO_DH
71 #include <openssl/dh.h>
72 #endif
73 #ifndef OPENSSL_NO_DSA
74 #include <openssl/dsa.h>
75 #endif
76 #ifndef OPENSSL_NO_RSA
77 #include <openssl/rsa.h>
78 #endif
79 
80 #ifndef OPENSSL_NO_ENGINE
81 #include <openssl/engine.h>
82 #endif
83 
84 #include "asn1_local.h"
85 #include "evp_local.h"
86 
87 static void EVP_PKEY_free_it(EVP_PKEY *x);
88 
89 int
90 EVP_PKEY_bits(const EVP_PKEY *pkey)
91 {
92 	if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
93 		return pkey->ameth->pkey_bits(pkey);
94 	return 0;
95 }
96 
97 int
98 EVP_PKEY_security_bits(const EVP_PKEY *pkey)
99 {
100 	if (pkey == NULL)
101 		return 0;
102 	if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL)
103 		return -2;
104 
105 	return pkey->ameth->pkey_security_bits(pkey);
106 }
107 
108 int
109 EVP_PKEY_size(const EVP_PKEY *pkey)
110 {
111 	if (pkey && pkey->ameth && pkey->ameth->pkey_size)
112 		return pkey->ameth->pkey_size(pkey);
113 	return 0;
114 }
115 
116 int
117 EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
118 {
119 #ifndef OPENSSL_NO_DSA
120 	if (pkey->type == EVP_PKEY_DSA) {
121 		int ret = pkey->save_parameters;
122 
123 		if (mode >= 0)
124 			pkey->save_parameters = mode;
125 		return (ret);
126 	}
127 #endif
128 #ifndef OPENSSL_NO_EC
129 	if (pkey->type == EVP_PKEY_EC) {
130 		int ret = pkey->save_parameters;
131 
132 		if (mode >= 0)
133 			pkey->save_parameters = mode;
134 		return (ret);
135 	}
136 #endif
137 	return (0);
138 }
139 
140 int
141 EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
142 {
143 	if (to->type != from->type) {
144 		EVPerror(EVP_R_DIFFERENT_KEY_TYPES);
145 		goto err;
146 	}
147 
148 	if (EVP_PKEY_missing_parameters(from)) {
149 		EVPerror(EVP_R_MISSING_PARAMETERS);
150 		goto err;
151 	}
152 	if (from->ameth && from->ameth->param_copy)
153 		return from->ameth->param_copy(to, from);
154 
155 err:
156 	return 0;
157 }
158 
159 int
160 EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
161 {
162 	if (pkey->ameth && pkey->ameth->param_missing)
163 		return pkey->ameth->param_missing(pkey);
164 	return 0;
165 }
166 
167 int
168 EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
169 {
170 	if (a->type != b->type)
171 		return -1;
172 	if (a->ameth && a->ameth->param_cmp)
173 		return a->ameth->param_cmp(a, b);
174 	return -2;
175 }
176 
177 int
178 EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
179 {
180 	if (a->type != b->type)
181 		return -1;
182 
183 	if (a->ameth) {
184 		int ret;
185 		/* Compare parameters if the algorithm has them */
186 		if (a->ameth->param_cmp) {
187 			ret = a->ameth->param_cmp(a, b);
188 			if (ret <= 0)
189 				return ret;
190 		}
191 
192 		if (a->ameth->pub_cmp)
193 			return a->ameth->pub_cmp(a, b);
194 	}
195 
196 	return -2;
197 }
198 
199 EVP_PKEY *
200 EVP_PKEY_new(void)
201 {
202 	EVP_PKEY *ret;
203 
204 	ret = malloc(sizeof(EVP_PKEY));
205 	if (ret == NULL) {
206 		EVPerror(ERR_R_MALLOC_FAILURE);
207 		return (NULL);
208 	}
209 	ret->type = EVP_PKEY_NONE;
210 	ret->save_type = EVP_PKEY_NONE;
211 	ret->references = 1;
212 	ret->ameth = NULL;
213 	ret->engine = NULL;
214 	ret->pkey.ptr = NULL;
215 	ret->attributes = NULL;
216 	ret->save_parameters = 1;
217 	return (ret);
218 }
219 
220 int
221 EVP_PKEY_up_ref(EVP_PKEY *pkey)
222 {
223 	int refs = CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
224 	return ((refs > 1) ? 1 : 0);
225 }
226 
227 /* Setup a public key ASN1 method and ENGINE from a NID or a string.
228  * If pkey is NULL just return 1 or 0 if the algorithm exists.
229  */
230 
231 static int
232 pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, int len)
233 {
234 	const EVP_PKEY_ASN1_METHOD *ameth;
235 	ENGINE **eptr = NULL;
236 
237 	if (e == NULL)
238 		eptr = &e;
239 
240 	if (pkey) {
241 		if (pkey->pkey.ptr)
242 			EVP_PKEY_free_it(pkey);
243 		/* If key type matches and a method exists then this
244 		 * lookup has succeeded once so just indicate success.
245 		 */
246 		if ((type == pkey->save_type) && pkey->ameth)
247 			return 1;
248 #ifndef OPENSSL_NO_ENGINE
249 		ENGINE_finish(pkey->engine);
250 		pkey->engine = NULL;
251 #endif
252 	}
253 	if (str)
254 		ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
255 	else
256 		ameth = EVP_PKEY_asn1_find(eptr, type);
257 #ifndef OPENSSL_NO_ENGINE
258 	if (pkey == NULL && eptr != NULL)
259 		ENGINE_finish(e);
260 #endif
261 	if (!ameth) {
262 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
263 		return 0;
264 	}
265 	if (pkey) {
266 		pkey->ameth = ameth;
267 		pkey->engine = e;
268 
269 		pkey->type = pkey->ameth->pkey_id;
270 		pkey->save_type = type;
271 	}
272 	return 1;
273 }
274 
275 int
276 EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
277 {
278 	return pkey_set_type(pkey, NULL, type, NULL, -1);
279 }
280 
281 EVP_PKEY *
282 EVP_PKEY_new_raw_private_key(int type, ENGINE *engine,
283     const unsigned char *private_key, size_t len)
284 {
285 	EVP_PKEY *ret;
286 
287 	if ((ret = EVP_PKEY_new()) == NULL)
288 		goto err;
289 
290 	if (!pkey_set_type(ret, engine, type, NULL, -1))
291 		goto err;
292 
293 	if (ret->ameth->set_priv_key == NULL) {
294 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
295 		goto err;
296 	}
297 	if (!ret->ameth->set_priv_key(ret, private_key, len)) {
298 		EVPerror(EVP_R_KEY_SETUP_FAILED);
299 		goto err;
300 	}
301 
302 	return ret;
303 
304  err:
305 	EVP_PKEY_free(ret);
306 
307 	return NULL;
308 }
309 
310 EVP_PKEY *
311 EVP_PKEY_new_raw_public_key(int type, ENGINE *engine,
312     const unsigned char *public_key, size_t len)
313 {
314 	EVP_PKEY *ret;
315 
316 	if ((ret = EVP_PKEY_new()) == NULL)
317 		goto err;
318 
319 	if (!pkey_set_type(ret, engine, type, NULL, -1))
320 		goto err;
321 
322 	if (ret->ameth->set_pub_key == NULL) {
323 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
324 		goto err;
325 	}
326 	if (!ret->ameth->set_pub_key(ret, public_key, len)) {
327 		EVPerror(EVP_R_KEY_SETUP_FAILED);
328 		goto err;
329 	}
330 
331 	return ret;
332 
333  err:
334 	EVP_PKEY_free(ret);
335 
336 	return NULL;
337 }
338 
339 int
340 EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey,
341     unsigned char *out_private_key, size_t *out_len)
342 {
343 	if (pkey->ameth->get_priv_key == NULL) {
344 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
345 		return 0;
346 	}
347 	if (!pkey->ameth->get_priv_key(pkey, out_private_key, out_len)) {
348 		EVPerror(EVP_R_GET_RAW_KEY_FAILED);
349 		return 0;
350 	}
351 
352 	return 1;
353 }
354 
355 int
356 EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey,
357     unsigned char *out_public_key, size_t *out_len)
358 {
359 	if (pkey->ameth->get_pub_key == NULL) {
360 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
361 		return 0;
362 	}
363 	if (!pkey->ameth->get_pub_key(pkey, out_public_key, out_len)) {
364 		EVPerror(EVP_R_GET_RAW_KEY_FAILED);
365 		return 0;
366 	}
367 
368 	return 1;
369 }
370 
371 EVP_PKEY *
372 EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, size_t len,
373     const EVP_CIPHER *cipher)
374 {
375 	EVP_PKEY *ret = NULL;
376 	CMAC_CTX *cmctx = NULL;
377 
378 	if ((ret = EVP_PKEY_new()) == NULL)
379 		goto err;
380 	if ((cmctx = CMAC_CTX_new()) == NULL)
381 		goto err;
382 
383 	if (!pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1))
384 		goto err;
385 
386 	if (!CMAC_Init(cmctx, priv, len, cipher, e)) {
387 		EVPerror(EVP_R_KEY_SETUP_FAILED);
388 		goto err;
389 	}
390 
391 	ret->pkey.ptr = cmctx;
392 
393 	return ret;
394 
395  err:
396 	EVP_PKEY_free(ret);
397 	CMAC_CTX_free(cmctx);
398 	return NULL;
399 }
400 
401 int
402 EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
403 {
404 	return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
405 }
406 
407 int
408 EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
409 {
410 	if (!EVP_PKEY_set_type(pkey, type))
411 		return 0;
412 	pkey->pkey.ptr = key;
413 	return (key != NULL);
414 }
415 
416 void *
417 EVP_PKEY_get0(const EVP_PKEY *pkey)
418 {
419 	return pkey->pkey.ptr;
420 }
421 
422 const unsigned char *
423 EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
424 {
425 	ASN1_OCTET_STRING *os;
426 
427 	if (pkey->type != EVP_PKEY_HMAC) {
428 		EVPerror(EVP_R_EXPECTING_AN_HMAC_KEY);
429 		return NULL;
430 	}
431 
432 	os = EVP_PKEY_get0(pkey);
433 	*len = os->length;
434 
435 	return os->data;
436 }
437 
438 #ifndef OPENSSL_NO_RSA
439 RSA *
440 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
441 {
442 	if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA_PSS)
443 		return pkey->pkey.rsa;
444 
445 	EVPerror(EVP_R_EXPECTING_AN_RSA_KEY);
446 	return NULL;
447 }
448 
449 RSA *
450 EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
451 {
452 	RSA *rsa;
453 
454 	if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL)
455 		return NULL;
456 
457 	RSA_up_ref(rsa);
458 
459 	return rsa;
460 }
461 
462 int
463 EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
464 {
465 	int ret = EVP_PKEY_assign_RSA(pkey, key);
466 	if (ret != 0)
467 		RSA_up_ref(key);
468 	return ret;
469 }
470 #endif
471 
472 #ifndef OPENSSL_NO_DSA
473 DSA *
474 EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
475 {
476 	if (pkey->type != EVP_PKEY_DSA) {
477 		EVPerror(EVP_R_EXPECTING_A_DSA_KEY);
478 		return NULL;
479 	}
480 	return pkey->pkey.dsa;
481 }
482 
483 DSA *
484 EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
485 {
486 	DSA *dsa;
487 
488 	if ((dsa = EVP_PKEY_get0_DSA(pkey)) == NULL)
489 		return NULL;
490 
491 	DSA_up_ref(dsa);
492 
493 	return dsa;
494 }
495 
496 int
497 EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
498 {
499 	int ret = EVP_PKEY_assign_DSA(pkey, key);
500 	if (ret != 0)
501 		DSA_up_ref(key);
502 	return ret;
503 }
504 #endif
505 
506 #ifndef OPENSSL_NO_EC
507 EC_KEY *
508 EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
509 {
510 	if (pkey->type != EVP_PKEY_EC) {
511 		EVPerror(EVP_R_EXPECTING_A_EC_KEY);
512 		return NULL;
513 	}
514 	return pkey->pkey.ec;
515 }
516 
517 EC_KEY *
518 EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
519 {
520 	EC_KEY *key;
521 
522 	if ((key = EVP_PKEY_get0_EC_KEY(pkey)) == NULL)
523 		return NULL;
524 
525 	EC_KEY_up_ref(key);
526 
527 	return key;
528 }
529 
530 int
531 EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
532 {
533 	int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
534 	if (ret != 0)
535 		EC_KEY_up_ref(key);
536 	return ret;
537 }
538 #endif
539 
540 
541 #ifndef OPENSSL_NO_DH
542 DH *
543 EVP_PKEY_get0_DH(EVP_PKEY *pkey)
544 {
545 	if (pkey->type != EVP_PKEY_DH) {
546 		EVPerror(EVP_R_EXPECTING_A_DH_KEY);
547 		return NULL;
548 	}
549 	return pkey->pkey.dh;
550 }
551 
552 DH *
553 EVP_PKEY_get1_DH(EVP_PKEY *pkey)
554 {
555 	DH *dh;
556 
557 	if ((dh = EVP_PKEY_get0_DH(pkey)) == NULL)
558 		return NULL;
559 
560 	DH_up_ref(dh);
561 
562 	return dh;
563 }
564 
565 int
566 EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
567 {
568 	int ret = EVP_PKEY_assign_DH(pkey, key);
569 	if (ret != 0)
570 		DH_up_ref(key);
571 	return ret;
572 }
573 #endif
574 
575 int
576 EVP_PKEY_type(int type)
577 {
578 	int ret;
579 	const EVP_PKEY_ASN1_METHOD *ameth;
580 	ENGINE *e;
581 	ameth = EVP_PKEY_asn1_find(&e, type);
582 	if (ameth)
583 		ret = ameth->pkey_id;
584 	else
585 		ret = NID_undef;
586 #ifndef OPENSSL_NO_ENGINE
587 	ENGINE_finish(e);
588 #endif
589 	return ret;
590 }
591 
592 int
593 EVP_PKEY_id(const EVP_PKEY *pkey)
594 {
595 	return pkey->type;
596 }
597 
598 int
599 EVP_PKEY_base_id(const EVP_PKEY *pkey)
600 {
601 	return EVP_PKEY_type(pkey->type);
602 }
603 
604 void
605 EVP_PKEY_free(EVP_PKEY *x)
606 {
607 	int i;
608 
609 	if (x == NULL)
610 		return;
611 
612 	i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY);
613 	if (i > 0)
614 		return;
615 
616 	EVP_PKEY_free_it(x);
617 	if (x->attributes)
618 		sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
619 	free(x);
620 }
621 
622 static void
623 EVP_PKEY_free_it(EVP_PKEY *x)
624 {
625 	if (x->ameth && x->ameth->pkey_free) {
626 		x->ameth->pkey_free(x);
627 		x->pkey.ptr = NULL;
628 	}
629 #ifndef OPENSSL_NO_ENGINE
630 	ENGINE_finish(x->engine);
631 	x->engine = NULL;
632 #endif
633 }
634 
635 static int
636 unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, const char *kstr)
637 {
638 	if (!BIO_indent(out, indent, 128))
639 		return 0;
640 	BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
641 	    kstr, OBJ_nid2ln(pkey->type));
642 	return 1;
643 }
644 
645 int
646 EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
647     ASN1_PCTX *pctx)
648 {
649 	if (pkey->ameth && pkey->ameth->pub_print)
650 		return pkey->ameth->pub_print(out, pkey, indent, pctx);
651 
652 	return unsup_alg(out, pkey, indent, "Public Key");
653 }
654 
655 int
656 EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
657     ASN1_PCTX *pctx)
658 {
659 	if (pkey->ameth && pkey->ameth->priv_print)
660 		return pkey->ameth->priv_print(out, pkey, indent, pctx);
661 
662 	return unsup_alg(out, pkey, indent, "Private Key");
663 }
664 
665 int
666 EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
667     ASN1_PCTX *pctx)
668 {
669 	if (pkey->ameth && pkey->ameth->param_print)
670 		return pkey->ameth->param_print(out, pkey, indent, pctx);
671 	return unsup_alg(out, pkey, indent, "Parameters");
672 }
673 
674 int
675 EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
676 {
677 	if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
678 		return -2;
679 	return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
680 	    0, pnid);
681 }
682