xref: /openbsd-src/lib/libcrypto/evp/p_lib.c (revision a03a5a63cb7f03d3b2ab75334ddeafa51700fbf3)
1 /* $OpenBSD: p_lib.c,v 1.57 2024/01/04 17:22:29 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  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    licensing@OpenSSL.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  */
105 
106 #include <stdio.h>
107 #include <stdlib.h>
108 #include <string.h>
109 
110 #include <openssl/asn1.h>
111 #include <openssl/bio.h>
112 #include <openssl/cmac.h>
113 #include <openssl/crypto.h>
114 #include <openssl/err.h>
115 #include <openssl/evp.h>
116 #include <openssl/objects.h>
117 #include <openssl/x509.h>
118 
119 #ifndef OPENSSL_NO_DH
120 #include <openssl/dh.h>
121 #endif
122 #ifndef OPENSSL_NO_DSA
123 #include <openssl/dsa.h>
124 #endif
125 #ifndef OPENSSL_NO_EC
126 #include <openssl/ec.h>
127 #endif
128 #ifndef OPENSSL_NO_RSA
129 #include <openssl/rsa.h>
130 #endif
131 
132 #include "evp_local.h"
133 
134 extern const EVP_PKEY_ASN1_METHOD cmac_asn1_meth;
135 extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
136 extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth;
137 extern const EVP_PKEY_ASN1_METHOD dsa1_asn1_meth;
138 extern const EVP_PKEY_ASN1_METHOD dsa2_asn1_meth;
139 extern const EVP_PKEY_ASN1_METHOD dsa3_asn1_meth;
140 extern const EVP_PKEY_ASN1_METHOD dsa4_asn1_meth;
141 extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
142 extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth;
143 extern const EVP_PKEY_ASN1_METHOD gostimit_asn1_meth;
144 extern const EVP_PKEY_ASN1_METHOD gostr01_asn1_meth;
145 extern const EVP_PKEY_ASN1_METHOD gostr12_256_asn1_meth;
146 extern const EVP_PKEY_ASN1_METHOD gostr12_512_asn1_meth;
147 extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
148 extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
149 extern const EVP_PKEY_ASN1_METHOD rsa2_asn1_meth;
150 extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth;
151 extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth;
152 
153 static const EVP_PKEY_ASN1_METHOD *asn1_methods[] = {
154 	&cmac_asn1_meth,
155 	&dh_asn1_meth,
156 	&dsa_asn1_meth,
157 	&dsa1_asn1_meth,
158 	&dsa2_asn1_meth,
159 	&dsa3_asn1_meth,
160 	&dsa4_asn1_meth,
161 	&eckey_asn1_meth,
162 	&ed25519_asn1_meth,
163 	&gostimit_asn1_meth,
164 	&gostr01_asn1_meth,
165 	&gostr12_256_asn1_meth,
166 	&gostr12_512_asn1_meth,
167 	&hmac_asn1_meth,
168 	&rsa_asn1_meth,
169 	&rsa2_asn1_meth,
170 	&rsa_pss_asn1_meth,
171 	&x25519_asn1_meth,
172 };
173 
174 #define N_ASN1_METHODS (sizeof(asn1_methods) / sizeof(asn1_methods[0]))
175 
176 int
177 EVP_PKEY_asn1_get_count(void)
178 {
179 	return N_ASN1_METHODS;
180 }
181 
182 const EVP_PKEY_ASN1_METHOD *
183 EVP_PKEY_asn1_get0(int idx)
184 {
185 	if (idx < 0 || idx >= N_ASN1_METHODS)
186 		return NULL;
187 
188 	return asn1_methods[idx];
189 }
190 
191 const EVP_PKEY_ASN1_METHOD *
192 EVP_PKEY_asn1_find(ENGINE **engine, int pkey_id)
193 {
194 	size_t i;
195 
196 	if (engine != NULL)
197 		*engine = NULL;
198 
199 	for (i = 0; i < N_ASN1_METHODS; i++) {
200 		if (asn1_methods[i]->pkey_id == pkey_id)
201 			return asn1_methods[i]->base_method;
202 	}
203 
204 	return NULL;
205 }
206 
207 const EVP_PKEY_ASN1_METHOD *
208 EVP_PKEY_asn1_find_str(ENGINE **engine, const char *str, int len)
209 {
210 	const EVP_PKEY_ASN1_METHOD *ameth;
211 	size_t str_len;
212 	int i;
213 
214 	if (engine != NULL)
215 		*engine = NULL;
216 
217 	if (len < -1)
218 		return NULL;
219 	if (len == -1)
220 		str_len = strlen(str);
221 	else
222 		str_len = len;
223 
224 	for (i = 0; i < N_ASN1_METHODS; i++) {
225 		ameth = asn1_methods[i];
226 		if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
227 			continue;
228 		if (strlen(ameth->pem_str) != str_len)
229 			continue;
230 		if (strncasecmp(ameth->pem_str, str, str_len) == 0)
231 			return ameth;
232 	}
233 
234 	return NULL;
235 }
236 
237 int
238 EVP_PKEY_asn1_get0_info(int *pkey_id, int *pkey_base_id, int *pkey_flags,
239     const char **info, const char **pem_str,
240     const EVP_PKEY_ASN1_METHOD *ameth)
241 {
242 	if (ameth == NULL)
243 		return 0;
244 
245 	if (pkey_id != NULL)
246 		*pkey_id = ameth->pkey_id;
247 	if (pkey_base_id != NULL)
248 		*pkey_base_id = ameth->base_method->pkey_id;
249 	if (pkey_flags != NULL)
250 		*pkey_flags = ameth->pkey_flags;
251 	if (info != NULL)
252 		*info = ameth->info;
253 	if (pem_str != NULL)
254 		*pem_str = ameth->pem_str;
255 
256 	return 1;
257 }
258 
259 const EVP_PKEY_ASN1_METHOD*
260 EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
261 {
262 	return pkey->ameth;
263 }
264 
265 int
266 EVP_PKEY_bits(const EVP_PKEY *pkey)
267 {
268 	if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
269 		return pkey->ameth->pkey_bits(pkey);
270 	return 0;
271 }
272 
273 int
274 EVP_PKEY_security_bits(const EVP_PKEY *pkey)
275 {
276 	if (pkey == NULL)
277 		return 0;
278 	if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL)
279 		return -2;
280 
281 	return pkey->ameth->pkey_security_bits(pkey);
282 }
283 
284 int
285 EVP_PKEY_size(const EVP_PKEY *pkey)
286 {
287 	if (pkey && pkey->ameth && pkey->ameth->pkey_size)
288 		return pkey->ameth->pkey_size(pkey);
289 	return 0;
290 }
291 
292 int
293 EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
294 {
295 #ifndef OPENSSL_NO_DSA
296 	if (pkey->type == EVP_PKEY_DSA) {
297 		int ret = pkey->save_parameters;
298 
299 		if (mode >= 0)
300 			pkey->save_parameters = mode;
301 		return (ret);
302 	}
303 #endif
304 #ifndef OPENSSL_NO_EC
305 	if (pkey->type == EVP_PKEY_EC) {
306 		int ret = pkey->save_parameters;
307 
308 		if (mode >= 0)
309 			pkey->save_parameters = mode;
310 		return (ret);
311 	}
312 #endif
313 	return (0);
314 }
315 
316 int
317 EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
318 {
319 	if (to->type != from->type) {
320 		EVPerror(EVP_R_DIFFERENT_KEY_TYPES);
321 		goto err;
322 	}
323 
324 	if (EVP_PKEY_missing_parameters(from)) {
325 		EVPerror(EVP_R_MISSING_PARAMETERS);
326 		goto err;
327 	}
328 	if (from->ameth && from->ameth->param_copy)
329 		return from->ameth->param_copy(to, from);
330 
331 err:
332 	return 0;
333 }
334 
335 int
336 EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
337 {
338 	if (pkey->ameth && pkey->ameth->param_missing)
339 		return pkey->ameth->param_missing(pkey);
340 	return 0;
341 }
342 
343 int
344 EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
345 {
346 	if (a->type != b->type)
347 		return -1;
348 	if (a->ameth && a->ameth->param_cmp)
349 		return a->ameth->param_cmp(a, b);
350 	return -2;
351 }
352 
353 int
354 EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
355 {
356 	if (a->type != b->type)
357 		return -1;
358 
359 	if (a->ameth) {
360 		int ret;
361 		/* Compare parameters if the algorithm has them */
362 		if (a->ameth->param_cmp) {
363 			ret = a->ameth->param_cmp(a, b);
364 			if (ret <= 0)
365 				return ret;
366 		}
367 
368 		if (a->ameth->pub_cmp)
369 			return a->ameth->pub_cmp(a, b);
370 	}
371 
372 	return -2;
373 }
374 
375 EVP_PKEY *
376 EVP_PKEY_new(void)
377 {
378 	EVP_PKEY *pkey;
379 
380 	if ((pkey = calloc(1, sizeof(*pkey))) == NULL) {
381 		EVPerror(ERR_R_MALLOC_FAILURE);
382 		return NULL;
383 	}
384 
385 	pkey->type = EVP_PKEY_NONE;
386 	pkey->references = 1;
387 	pkey->save_parameters = 1;
388 
389 	return pkey;
390 }
391 
392 int
393 EVP_PKEY_up_ref(EVP_PKEY *pkey)
394 {
395 	return CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) > 1;
396 }
397 
398 static void
399 evp_pkey_free_pkey_ptr(EVP_PKEY *pkey)
400 {
401 	if (pkey == NULL || pkey->ameth == NULL || pkey->ameth->pkey_free == NULL)
402 		return;
403 
404 	pkey->ameth->pkey_free(pkey);
405 	pkey->pkey.ptr = NULL;
406 }
407 
408 void
409 EVP_PKEY_free(EVP_PKEY *pkey)
410 {
411 	if (pkey == NULL)
412 		return;
413 
414 	if (CRYPTO_add(&pkey->references, -1, CRYPTO_LOCK_EVP_PKEY) > 0)
415 		return;
416 
417 	evp_pkey_free_pkey_ptr(pkey);
418 	sk_X509_ATTRIBUTE_pop_free(pkey->attributes, X509_ATTRIBUTE_free);
419 	freezero(pkey, sizeof(*pkey));
420 }
421 
422 int
423 EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
424 {
425 	const EVP_PKEY_ASN1_METHOD *ameth;
426 
427 	evp_pkey_free_pkey_ptr(pkey);
428 
429 	if ((ameth = EVP_PKEY_asn1_find(NULL, type)) == NULL) {
430 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
431 		return 0;
432 	}
433 	if (pkey != NULL) {
434 		pkey->ameth = ameth;
435 		pkey->type = pkey->ameth->pkey_id;
436 	}
437 
438 	return 1;
439 }
440 
441 int
442 EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
443 {
444 	const EVP_PKEY_ASN1_METHOD *ameth;
445 
446 	evp_pkey_free_pkey_ptr(pkey);
447 
448 	if ((ameth = EVP_PKEY_asn1_find_str(NULL, str, len)) == NULL) {
449 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
450 		return 0;
451 	}
452 	if (pkey != NULL) {
453 		pkey->ameth = ameth;
454 		pkey->type = pkey->ameth->pkey_id;
455 	}
456 
457 	return 1;
458 }
459 
460 int
461 EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
462 {
463 	if (!EVP_PKEY_set_type(pkey, type))
464 		return 0;
465 
466 	return (pkey->pkey.ptr = key) != NULL;
467 }
468 
469 EVP_PKEY *
470 EVP_PKEY_new_raw_private_key(int type, ENGINE *engine,
471     const unsigned char *private_key, size_t len)
472 {
473 	EVP_PKEY *pkey;
474 
475 	if ((pkey = EVP_PKEY_new()) == NULL)
476 		goto err;
477 
478 	if (!EVP_PKEY_set_type(pkey, type))
479 		goto err;
480 
481 	if (pkey->ameth->set_priv_key == NULL) {
482 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
483 		goto err;
484 	}
485 	if (!pkey->ameth->set_priv_key(pkey, private_key, len)) {
486 		EVPerror(EVP_R_KEY_SETUP_FAILED);
487 		goto err;
488 	}
489 
490 	return pkey;
491 
492  err:
493 	EVP_PKEY_free(pkey);
494 
495 	return NULL;
496 }
497 
498 EVP_PKEY *
499 EVP_PKEY_new_raw_public_key(int type, ENGINE *engine,
500     const unsigned char *public_key, size_t len)
501 {
502 	EVP_PKEY *pkey;
503 
504 	if ((pkey = EVP_PKEY_new()) == NULL)
505 		goto err;
506 
507 	if (!EVP_PKEY_set_type(pkey, type))
508 		goto err;
509 
510 	if (pkey->ameth->set_pub_key == NULL) {
511 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
512 		goto err;
513 	}
514 	if (!pkey->ameth->set_pub_key(pkey, public_key, len)) {
515 		EVPerror(EVP_R_KEY_SETUP_FAILED);
516 		goto err;
517 	}
518 
519 	return pkey;
520 
521  err:
522 	EVP_PKEY_free(pkey);
523 
524 	return NULL;
525 }
526 
527 int
528 EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey,
529     unsigned char *out_private_key, size_t *out_len)
530 {
531 	if (pkey->ameth->get_priv_key == NULL) {
532 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
533 		return 0;
534 	}
535 	if (!pkey->ameth->get_priv_key(pkey, out_private_key, out_len)) {
536 		EVPerror(EVP_R_GET_RAW_KEY_FAILED);
537 		return 0;
538 	}
539 
540 	return 1;
541 }
542 
543 int
544 EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey,
545     unsigned char *out_public_key, size_t *out_len)
546 {
547 	if (pkey->ameth->get_pub_key == NULL) {
548 		EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
549 		return 0;
550 	}
551 	if (!pkey->ameth->get_pub_key(pkey, out_public_key, out_len)) {
552 		EVPerror(EVP_R_GET_RAW_KEY_FAILED);
553 		return 0;
554 	}
555 
556 	return 1;
557 }
558 
559 EVP_PKEY *
560 EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, size_t len,
561     const EVP_CIPHER *cipher)
562 {
563 	EVP_PKEY *pkey = NULL;
564 	CMAC_CTX *cmctx = NULL;
565 
566 	if ((pkey = EVP_PKEY_new()) == NULL)
567 		goto err;
568 	if ((cmctx = CMAC_CTX_new()) == NULL)
569 		goto err;
570 
571 	if (!EVP_PKEY_set_type(pkey, EVP_PKEY_CMAC))
572 		goto err;
573 
574 	if (!CMAC_Init(cmctx, priv, len, cipher, NULL)) {
575 		EVPerror(EVP_R_KEY_SETUP_FAILED);
576 		goto err;
577 	}
578 
579 	pkey->pkey.ptr = cmctx;
580 
581 	return pkey;
582 
583  err:
584 	EVP_PKEY_free(pkey);
585 	CMAC_CTX_free(cmctx);
586 
587 	return NULL;
588 }
589 
590 void *
591 EVP_PKEY_get0(const EVP_PKEY *pkey)
592 {
593 	return pkey->pkey.ptr;
594 }
595 
596 const unsigned char *
597 EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
598 {
599 	ASN1_OCTET_STRING *os;
600 
601 	if (pkey->type != EVP_PKEY_HMAC) {
602 		EVPerror(EVP_R_EXPECTING_AN_HMAC_KEY);
603 		return NULL;
604 	}
605 
606 	os = EVP_PKEY_get0(pkey);
607 	*len = os->length;
608 
609 	return os->data;
610 }
611 
612 #ifndef OPENSSL_NO_RSA
613 RSA *
614 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
615 {
616 	if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA_PSS)
617 		return pkey->pkey.rsa;
618 
619 	EVPerror(EVP_R_EXPECTING_AN_RSA_KEY);
620 	return NULL;
621 }
622 
623 RSA *
624 EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
625 {
626 	RSA *rsa;
627 
628 	if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL)
629 		return NULL;
630 
631 	RSA_up_ref(rsa);
632 
633 	return rsa;
634 }
635 
636 int
637 EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
638 {
639 	int ret = EVP_PKEY_assign_RSA(pkey, key);
640 	if (ret != 0)
641 		RSA_up_ref(key);
642 	return ret;
643 }
644 #endif
645 
646 #ifndef OPENSSL_NO_DSA
647 DSA *
648 EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
649 {
650 	if (pkey->type != EVP_PKEY_DSA) {
651 		EVPerror(EVP_R_EXPECTING_A_DSA_KEY);
652 		return NULL;
653 	}
654 	return pkey->pkey.dsa;
655 }
656 
657 DSA *
658 EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
659 {
660 	DSA *dsa;
661 
662 	if ((dsa = EVP_PKEY_get0_DSA(pkey)) == NULL)
663 		return NULL;
664 
665 	DSA_up_ref(dsa);
666 
667 	return dsa;
668 }
669 
670 int
671 EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
672 {
673 	int ret = EVP_PKEY_assign_DSA(pkey, key);
674 	if (ret != 0)
675 		DSA_up_ref(key);
676 	return ret;
677 }
678 #endif
679 
680 #ifndef OPENSSL_NO_EC
681 EC_KEY *
682 EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
683 {
684 	if (pkey->type != EVP_PKEY_EC) {
685 		EVPerror(EVP_R_EXPECTING_A_EC_KEY);
686 		return NULL;
687 	}
688 	return pkey->pkey.ec;
689 }
690 
691 EC_KEY *
692 EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
693 {
694 	EC_KEY *key;
695 
696 	if ((key = EVP_PKEY_get0_EC_KEY(pkey)) == NULL)
697 		return NULL;
698 
699 	EC_KEY_up_ref(key);
700 
701 	return key;
702 }
703 
704 int
705 EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
706 {
707 	int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
708 	if (ret != 0)
709 		EC_KEY_up_ref(key);
710 	return ret;
711 }
712 #endif
713 
714 
715 #ifndef OPENSSL_NO_DH
716 DH *
717 EVP_PKEY_get0_DH(EVP_PKEY *pkey)
718 {
719 	if (pkey->type != EVP_PKEY_DH) {
720 		EVPerror(EVP_R_EXPECTING_A_DH_KEY);
721 		return NULL;
722 	}
723 	return pkey->pkey.dh;
724 }
725 
726 DH *
727 EVP_PKEY_get1_DH(EVP_PKEY *pkey)
728 {
729 	DH *dh;
730 
731 	if ((dh = EVP_PKEY_get0_DH(pkey)) == NULL)
732 		return NULL;
733 
734 	DH_up_ref(dh);
735 
736 	return dh;
737 }
738 
739 int
740 EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
741 {
742 	int ret = EVP_PKEY_assign_DH(pkey, key);
743 	if (ret != 0)
744 		DH_up_ref(key);
745 	return ret;
746 }
747 #endif
748 
749 int
750 EVP_PKEY_type(int type)
751 {
752 	const EVP_PKEY_ASN1_METHOD *ameth;
753 
754 	if ((ameth = EVP_PKEY_asn1_find(NULL, type)) != NULL)
755 		return ameth->pkey_id;
756 
757 	return NID_undef;
758 }
759 
760 int
761 EVP_PKEY_id(const EVP_PKEY *pkey)
762 {
763 	return pkey->type;
764 }
765 
766 int
767 EVP_PKEY_base_id(const EVP_PKEY *pkey)
768 {
769 	return EVP_PKEY_type(pkey->type);
770 }
771 
772 static int
773 unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, const char *kstr)
774 {
775 	if (!BIO_indent(out, indent, 128))
776 		return 0;
777 	BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
778 	    kstr, OBJ_nid2ln(pkey->type));
779 	return 1;
780 }
781 
782 int
783 EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
784     ASN1_PCTX *pctx)
785 {
786 	if (pkey->ameth && pkey->ameth->pub_print)
787 		return pkey->ameth->pub_print(out, pkey, indent, pctx);
788 
789 	return unsup_alg(out, pkey, indent, "Public Key");
790 }
791 
792 int
793 EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
794     ASN1_PCTX *pctx)
795 {
796 	if (pkey->ameth && pkey->ameth->priv_print)
797 		return pkey->ameth->priv_print(out, pkey, indent, pctx);
798 
799 	return unsup_alg(out, pkey, indent, "Private Key");
800 }
801 
802 int
803 EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
804     ASN1_PCTX *pctx)
805 {
806 	if (pkey->ameth && pkey->ameth->param_print)
807 		return pkey->ameth->param_print(out, pkey, indent, pctx);
808 	return unsup_alg(out, pkey, indent, "Parameters");
809 }
810 
811 int
812 EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
813 {
814 	if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
815 		return -2;
816 	return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
817 	    0, pnid);
818 }
819