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