xref: /openbsd-src/lib/libcrypto/evp/p_lib.c (revision 936a262e7549d44f8f559e1367e75d94866ce958)
1 /* $OpenBSD: p_lib.c,v 1.50 2023/12/25 22:41:50 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 #include "asn1_local.h"
81 #include "evp_local.h"
82 
83 int
84 EVP_PKEY_bits(const EVP_PKEY *pkey)
85 {
86 	if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
87 		return pkey->ameth->pkey_bits(pkey);
88 	return 0;
89 }
90 
91 int
92 EVP_PKEY_security_bits(const EVP_PKEY *pkey)
93 {
94 	if (pkey == NULL)
95 		return 0;
96 	if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL)
97 		return -2;
98 
99 	return pkey->ameth->pkey_security_bits(pkey);
100 }
101 
102 int
103 EVP_PKEY_size(const EVP_PKEY *pkey)
104 {
105 	if (pkey && pkey->ameth && pkey->ameth->pkey_size)
106 		return pkey->ameth->pkey_size(pkey);
107 	return 0;
108 }
109 
110 int
111 EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
112 {
113 #ifndef OPENSSL_NO_DSA
114 	if (pkey->type == EVP_PKEY_DSA) {
115 		int ret = pkey->save_parameters;
116 
117 		if (mode >= 0)
118 			pkey->save_parameters = mode;
119 		return (ret);
120 	}
121 #endif
122 #ifndef OPENSSL_NO_EC
123 	if (pkey->type == EVP_PKEY_EC) {
124 		int ret = pkey->save_parameters;
125 
126 		if (mode >= 0)
127 			pkey->save_parameters = mode;
128 		return (ret);
129 	}
130 #endif
131 	return (0);
132 }
133 
134 int
135 EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
136 {
137 	if (to->type != from->type) {
138 		EVPerror(EVP_R_DIFFERENT_KEY_TYPES);
139 		goto err;
140 	}
141 
142 	if (EVP_PKEY_missing_parameters(from)) {
143 		EVPerror(EVP_R_MISSING_PARAMETERS);
144 		goto err;
145 	}
146 	if (from->ameth && from->ameth->param_copy)
147 		return from->ameth->param_copy(to, from);
148 
149 err:
150 	return 0;
151 }
152 
153 int
154 EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
155 {
156 	if (pkey->ameth && pkey->ameth->param_missing)
157 		return pkey->ameth->param_missing(pkey);
158 	return 0;
159 }
160 
161 int
162 EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
163 {
164 	if (a->type != b->type)
165 		return -1;
166 	if (a->ameth && a->ameth->param_cmp)
167 		return a->ameth->param_cmp(a, b);
168 	return -2;
169 }
170 
171 int
172 EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
173 {
174 	if (a->type != b->type)
175 		return -1;
176 
177 	if (a->ameth) {
178 		int ret;
179 		/* Compare parameters if the algorithm has them */
180 		if (a->ameth->param_cmp) {
181 			ret = a->ameth->param_cmp(a, b);
182 			if (ret <= 0)
183 				return ret;
184 		}
185 
186 		if (a->ameth->pub_cmp)
187 			return a->ameth->pub_cmp(a, b);
188 	}
189 
190 	return -2;
191 }
192 
193 EVP_PKEY *
194 EVP_PKEY_new(void)
195 {
196 	EVP_PKEY *pkey;
197 
198 	if ((pkey = calloc(1, sizeof(*pkey))) == NULL) {
199 		EVPerror(ERR_R_MALLOC_FAILURE);
200 		return NULL;
201 	}
202 
203 	pkey->type = EVP_PKEY_NONE;
204 	pkey->save_type = EVP_PKEY_NONE;
205 	pkey->references = 1;
206 	pkey->save_parameters = 1;
207 
208 	return pkey;
209 }
210 
211 int
212 EVP_PKEY_up_ref(EVP_PKEY *pkey)
213 {
214 	return CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) > 1;
215 }
216 
217 static void
218 evp_pkey_free_pkey_ptr(EVP_PKEY *pkey)
219 {
220 	if (pkey == NULL || pkey->ameth == NULL || pkey->ameth->pkey_free == NULL)
221 		return;
222 
223 	pkey->ameth->pkey_free(pkey);
224 	pkey->pkey.ptr = NULL;
225 }
226 
227 void
228 EVP_PKEY_free(EVP_PKEY *pkey)
229 {
230 	if (pkey == NULL)
231 		return;
232 
233 	if (CRYPTO_add(&pkey->references, -1, CRYPTO_LOCK_EVP_PKEY) > 0)
234 		return;
235 
236 	evp_pkey_free_pkey_ptr(pkey);
237 	sk_X509_ATTRIBUTE_pop_free(pkey->attributes, X509_ATTRIBUTE_free);
238 	freezero(pkey, sizeof(*pkey));
239 }
240 
241 int
242 EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
243 {
244 	const EVP_PKEY_ASN1_METHOD *ameth;
245 
246 	evp_pkey_free_pkey_ptr(pkey);
247 
248 	if ((ameth = EVP_PKEY_asn1_find(NULL, type)) == NULL) {
249 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
250 		return 0;
251 	}
252 	if (pkey != NULL) {
253 		pkey->ameth = ameth;
254 		pkey->type = pkey->ameth->pkey_id;
255 		pkey->save_type = type;
256 	}
257 
258 	return 1;
259 }
260 
261 int
262 EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
263 {
264 	const EVP_PKEY_ASN1_METHOD *ameth;
265 
266 	evp_pkey_free_pkey_ptr(pkey);
267 
268 	if ((ameth = EVP_PKEY_asn1_find_str(NULL, str, len)) == NULL) {
269 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
270 		return 0;
271 	}
272 	if (pkey != NULL) {
273 		pkey->ameth = ameth;
274 		pkey->type = pkey->ameth->pkey_id;
275 		pkey->save_type = EVP_PKEY_NONE;
276 	}
277 
278 	return 1;
279 }
280 
281 int
282 EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
283 {
284 	if (!EVP_PKEY_set_type(pkey, type))
285 		return 0;
286 
287 	return (pkey->pkey.ptr = key) != NULL;
288 }
289 
290 EVP_PKEY *
291 EVP_PKEY_new_raw_private_key(int type, ENGINE *engine,
292     const unsigned char *private_key, size_t len)
293 {
294 	EVP_PKEY *pkey;
295 
296 	if ((pkey = EVP_PKEY_new()) == NULL)
297 		goto err;
298 
299 	if (!EVP_PKEY_set_type(pkey, type))
300 		goto err;
301 
302 	if (pkey->ameth->set_priv_key == NULL) {
303 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
304 		goto err;
305 	}
306 	if (!pkey->ameth->set_priv_key(pkey, private_key, len)) {
307 		EVPerror(EVP_R_KEY_SETUP_FAILED);
308 		goto err;
309 	}
310 
311 	return pkey;
312 
313  err:
314 	EVP_PKEY_free(pkey);
315 
316 	return NULL;
317 }
318 
319 EVP_PKEY *
320 EVP_PKEY_new_raw_public_key(int type, ENGINE *engine,
321     const unsigned char *public_key, size_t len)
322 {
323 	EVP_PKEY *pkey;
324 
325 	if ((pkey = EVP_PKEY_new()) == NULL)
326 		goto err;
327 
328 	if (!EVP_PKEY_set_type(pkey, type))
329 		goto err;
330 
331 	if (pkey->ameth->set_pub_key == NULL) {
332 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
333 		goto err;
334 	}
335 	if (!pkey->ameth->set_pub_key(pkey, public_key, len)) {
336 		EVPerror(EVP_R_KEY_SETUP_FAILED);
337 		goto err;
338 	}
339 
340 	return pkey;
341 
342  err:
343 	EVP_PKEY_free(pkey);
344 
345 	return NULL;
346 }
347 
348 int
349 EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey,
350     unsigned char *out_private_key, size_t *out_len)
351 {
352 	if (pkey->ameth->get_priv_key == NULL) {
353 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
354 		return 0;
355 	}
356 	if (!pkey->ameth->get_priv_key(pkey, out_private_key, out_len)) {
357 		EVPerror(EVP_R_GET_RAW_KEY_FAILED);
358 		return 0;
359 	}
360 
361 	return 1;
362 }
363 
364 int
365 EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey,
366     unsigned char *out_public_key, size_t *out_len)
367 {
368 	if (pkey->ameth->get_pub_key == NULL) {
369 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
370 		return 0;
371 	}
372 	if (!pkey->ameth->get_pub_key(pkey, out_public_key, out_len)) {
373 		EVPerror(EVP_R_GET_RAW_KEY_FAILED);
374 		return 0;
375 	}
376 
377 	return 1;
378 }
379 
380 EVP_PKEY *
381 EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, size_t len,
382     const EVP_CIPHER *cipher)
383 {
384 	EVP_PKEY *pkey = NULL;
385 	CMAC_CTX *cmctx = NULL;
386 
387 	if ((pkey = EVP_PKEY_new()) == NULL)
388 		goto err;
389 	if ((cmctx = CMAC_CTX_new()) == NULL)
390 		goto err;
391 
392 	if (!EVP_PKEY_set_type(pkey, EVP_PKEY_CMAC))
393 		goto err;
394 
395 	if (!CMAC_Init(cmctx, priv, len, cipher, NULL)) {
396 		EVPerror(EVP_R_KEY_SETUP_FAILED);
397 		goto err;
398 	}
399 
400 	pkey->pkey.ptr = cmctx;
401 
402 	return pkey;
403 
404  err:
405 	EVP_PKEY_free(pkey);
406 	CMAC_CTX_free(cmctx);
407 
408 	return NULL;
409 }
410 
411 void *
412 EVP_PKEY_get0(const EVP_PKEY *pkey)
413 {
414 	return pkey->pkey.ptr;
415 }
416 
417 const unsigned char *
418 EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
419 {
420 	ASN1_OCTET_STRING *os;
421 
422 	if (pkey->type != EVP_PKEY_HMAC) {
423 		EVPerror(EVP_R_EXPECTING_AN_HMAC_KEY);
424 		return NULL;
425 	}
426 
427 	os = EVP_PKEY_get0(pkey);
428 	*len = os->length;
429 
430 	return os->data;
431 }
432 
433 #ifndef OPENSSL_NO_RSA
434 RSA *
435 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
436 {
437 	if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA_PSS)
438 		return pkey->pkey.rsa;
439 
440 	EVPerror(EVP_R_EXPECTING_AN_RSA_KEY);
441 	return NULL;
442 }
443 
444 RSA *
445 EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
446 {
447 	RSA *rsa;
448 
449 	if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL)
450 		return NULL;
451 
452 	RSA_up_ref(rsa);
453 
454 	return rsa;
455 }
456 
457 int
458 EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
459 {
460 	int ret = EVP_PKEY_assign_RSA(pkey, key);
461 	if (ret != 0)
462 		RSA_up_ref(key);
463 	return ret;
464 }
465 #endif
466 
467 #ifndef OPENSSL_NO_DSA
468 DSA *
469 EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
470 {
471 	if (pkey->type != EVP_PKEY_DSA) {
472 		EVPerror(EVP_R_EXPECTING_A_DSA_KEY);
473 		return NULL;
474 	}
475 	return pkey->pkey.dsa;
476 }
477 
478 DSA *
479 EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
480 {
481 	DSA *dsa;
482 
483 	if ((dsa = EVP_PKEY_get0_DSA(pkey)) == NULL)
484 		return NULL;
485 
486 	DSA_up_ref(dsa);
487 
488 	return dsa;
489 }
490 
491 int
492 EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
493 {
494 	int ret = EVP_PKEY_assign_DSA(pkey, key);
495 	if (ret != 0)
496 		DSA_up_ref(key);
497 	return ret;
498 }
499 #endif
500 
501 #ifndef OPENSSL_NO_EC
502 EC_KEY *
503 EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
504 {
505 	if (pkey->type != EVP_PKEY_EC) {
506 		EVPerror(EVP_R_EXPECTING_A_EC_KEY);
507 		return NULL;
508 	}
509 	return pkey->pkey.ec;
510 }
511 
512 EC_KEY *
513 EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
514 {
515 	EC_KEY *key;
516 
517 	if ((key = EVP_PKEY_get0_EC_KEY(pkey)) == NULL)
518 		return NULL;
519 
520 	EC_KEY_up_ref(key);
521 
522 	return key;
523 }
524 
525 int
526 EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
527 {
528 	int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
529 	if (ret != 0)
530 		EC_KEY_up_ref(key);
531 	return ret;
532 }
533 #endif
534 
535 
536 #ifndef OPENSSL_NO_DH
537 DH *
538 EVP_PKEY_get0_DH(EVP_PKEY *pkey)
539 {
540 	if (pkey->type != EVP_PKEY_DH) {
541 		EVPerror(EVP_R_EXPECTING_A_DH_KEY);
542 		return NULL;
543 	}
544 	return pkey->pkey.dh;
545 }
546 
547 DH *
548 EVP_PKEY_get1_DH(EVP_PKEY *pkey)
549 {
550 	DH *dh;
551 
552 	if ((dh = EVP_PKEY_get0_DH(pkey)) == NULL)
553 		return NULL;
554 
555 	DH_up_ref(dh);
556 
557 	return dh;
558 }
559 
560 int
561 EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
562 {
563 	int ret = EVP_PKEY_assign_DH(pkey, key);
564 	if (ret != 0)
565 		DH_up_ref(key);
566 	return ret;
567 }
568 #endif
569 
570 int
571 EVP_PKEY_type(int type)
572 {
573 	const EVP_PKEY_ASN1_METHOD *ameth;
574 
575 	if ((ameth = EVP_PKEY_asn1_find(NULL, type)) != NULL)
576 		return ameth->pkey_id;
577 
578 	return NID_undef;
579 }
580 
581 int
582 EVP_PKEY_id(const EVP_PKEY *pkey)
583 {
584 	return pkey->type;
585 }
586 
587 int
588 EVP_PKEY_base_id(const EVP_PKEY *pkey)
589 {
590 	return EVP_PKEY_type(pkey->type);
591 }
592 
593 static int
594 unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, const char *kstr)
595 {
596 	if (!BIO_indent(out, indent, 128))
597 		return 0;
598 	BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
599 	    kstr, OBJ_nid2ln(pkey->type));
600 	return 1;
601 }
602 
603 int
604 EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
605     ASN1_PCTX *pctx)
606 {
607 	if (pkey->ameth && pkey->ameth->pub_print)
608 		return pkey->ameth->pub_print(out, pkey, indent, pctx);
609 
610 	return unsup_alg(out, pkey, indent, "Public Key");
611 }
612 
613 int
614 EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
615     ASN1_PCTX *pctx)
616 {
617 	if (pkey->ameth && pkey->ameth->priv_print)
618 		return pkey->ameth->priv_print(out, pkey, indent, pctx);
619 
620 	return unsup_alg(out, pkey, indent, "Private Key");
621 }
622 
623 int
624 EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
625     ASN1_PCTX *pctx)
626 {
627 	if (pkey->ameth && pkey->ameth->param_print)
628 		return pkey->ameth->param_print(out, pkey, indent, pctx);
629 	return unsup_alg(out, pkey, indent, "Parameters");
630 }
631 
632 int
633 EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
634 {
635 	if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
636 		return -2;
637 	return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
638 	    0, pnid);
639 }
640