95dd96e86bce03b1071a30d263f073602210dbbc
[openssl.git] / crypto / evp / p_lib.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "internal/refcount.h"
13 #include <openssl/bn.h>
14 #include <openssl/err.h>
15 #include <openssl/objects.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509.h>
18 #include <openssl/rsa.h>
19 #include <openssl/dsa.h>
20 #include <openssl/dh.h>
21 #include <openssl/cmac.h>
22 #include <openssl/engine.h>
23 #include <openssl/params.h>
24 #include <openssl/serializer.h>
25 #include <openssl/core_names.h>
26
27 #include "crypto/asn1.h"
28 #include "crypto/evp.h"
29 #include "internal/provider.h"
30 #include "evp_local.h"
31
32 static void evp_pkey_free_it(EVP_PKEY *key);
33
34 #ifndef FIPS_MODE
35
36 int EVP_PKEY_bits(const EVP_PKEY *pkey)
37 {
38     if (pkey != NULL) {
39         if (pkey->ameth == NULL)
40             return pkey->cache.bits;
41         else if (pkey->ameth->pkey_bits)
42             return pkey->ameth->pkey_bits(pkey);
43     }
44     return 0;
45 }
46
47 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
48 {
49     if (pkey == NULL)
50         return 0;
51     if (pkey->ameth == NULL)
52         return pkey->cache.security_bits;
53     if (pkey->ameth->pkey_security_bits == NULL)
54         return -2;
55     return pkey->ameth->pkey_security_bits(pkey);
56 }
57
58 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
59 {
60 # ifndef OPENSSL_NO_DSA
61     if (pkey->type == EVP_PKEY_DSA) {
62         int ret = pkey->save_parameters;
63
64         if (mode >= 0)
65             pkey->save_parameters = mode;
66         return ret;
67     }
68 # endif
69 # ifndef OPENSSL_NO_EC
70     if (pkey->type == EVP_PKEY_EC) {
71         int ret = pkey->save_parameters;
72
73         if (mode >= 0)
74             pkey->save_parameters = mode;
75         return ret;
76     }
77 # endif
78     return 0;
79 }
80
81 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
82 {
83     if (to->type == EVP_PKEY_NONE) {
84         if (EVP_PKEY_set_type(to, from->type) == 0)
85             return 0;
86     } else if (to->type != from->type) {
87         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
88         goto err;
89     }
90
91     if (EVP_PKEY_missing_parameters(from)) {
92         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
93         goto err;
94     }
95
96     if (!EVP_PKEY_missing_parameters(to)) {
97         if (EVP_PKEY_cmp_parameters(to, from) == 1)
98             return 1;
99         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
100         return 0;
101     }
102
103     if (from->ameth && from->ameth->param_copy)
104         return from->ameth->param_copy(to, from);
105  err:
106     return 0;
107 }
108
109 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
110 {
111     if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing)
112         return pkey->ameth->param_missing(pkey);
113     return 0;
114 }
115
116 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
117 {
118     if (a->type != b->type)
119         return -1;
120     if (a->ameth && a->ameth->param_cmp)
121         return a->ameth->param_cmp(a, b);
122     return -2;
123 }
124
125 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
126 {
127     if (a->type != b->type)
128         return -1;
129
130     if (a->ameth) {
131         int ret;
132         /* Compare parameters if the algorithm has them */
133         if (a->ameth->param_cmp) {
134             ret = a->ameth->param_cmp(a, b);
135             if (ret <= 0)
136                 return ret;
137         }
138
139         if (a->ameth->pub_cmp)
140             return a->ameth->pub_cmp(a, b);
141     }
142
143     return -2;
144 }
145
146
147 /*
148  * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
149  * is NULL just return 1 or 0 if the algorithm exists.
150  */
151
152 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
153                          int len)
154 {
155     const EVP_PKEY_ASN1_METHOD *ameth;
156     ENGINE **eptr = (e == NULL) ? &e :  NULL;
157
158     if (pkey) {
159         if (pkey->pkey.ptr)
160             evp_pkey_free_it(pkey);
161         /*
162          * If key type matches and a method exists then this lookup has
163          * succeeded once so just indicate success.
164          */
165         if ((type == pkey->save_type) && pkey->ameth)
166             return 1;
167 # ifndef OPENSSL_NO_ENGINE
168         /* If we have ENGINEs release them */
169         ENGINE_finish(pkey->engine);
170         pkey->engine = NULL;
171         ENGINE_finish(pkey->pmeth_engine);
172         pkey->pmeth_engine = NULL;
173 # endif
174     }
175     if (str)
176         ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
177     else
178         ameth = EVP_PKEY_asn1_find(eptr, type);
179 # ifndef OPENSSL_NO_ENGINE
180     if (pkey == NULL && eptr != NULL)
181         ENGINE_finish(e);
182 # endif
183     if (ameth == NULL) {
184         EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
185         return 0;
186     }
187     if (pkey) {
188         pkey->ameth = ameth;
189         pkey->engine = e;
190
191         pkey->type = pkey->ameth->pkey_id;
192         pkey->save_type = type;
193     }
194     return 1;
195 }
196
197 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
198                                        const unsigned char *priv,
199                                        size_t len)
200 {
201     EVP_PKEY *ret = EVP_PKEY_new();
202
203     if (ret == NULL
204             || !pkey_set_type(ret, e, type, NULL, -1)) {
205         /* EVPerr already called */
206         goto err;
207     }
208
209     if (ret->ameth->set_priv_key == NULL) {
210         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
211                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
212         goto err;
213     }
214
215     if (!ret->ameth->set_priv_key(ret, priv, len)) {
216         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
217         goto err;
218     }
219
220     return ret;
221
222  err:
223     EVP_PKEY_free(ret);
224     return NULL;
225 }
226
227 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
228                                       const unsigned char *pub,
229                                       size_t len)
230 {
231     EVP_PKEY *ret = EVP_PKEY_new();
232
233     if (ret == NULL
234             || !pkey_set_type(ret, e, type, NULL, -1)) {
235         /* EVPerr already called */
236         goto err;
237     }
238
239     if (ret->ameth->set_pub_key == NULL) {
240         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
241                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
242         goto err;
243     }
244
245     if (!ret->ameth->set_pub_key(ret, pub, len)) {
246         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
247         goto err;
248     }
249
250     return ret;
251
252  err:
253     EVP_PKEY_free(ret);
254     return NULL;
255 }
256
257 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
258                                  size_t *len)
259 {
260      if (pkey->ameth->get_priv_key == NULL) {
261         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
262                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
263         return 0;
264     }
265
266     if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
267         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
268         return 0;
269     }
270
271     return 1;
272 }
273
274 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
275                                 size_t *len)
276 {
277      if (pkey->ameth->get_pub_key == NULL) {
278         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
279                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
280         return 0;
281     }
282
283     if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
284         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
285         return 0;
286     }
287
288     return 1;
289 }
290
291 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
292                                 size_t len, const EVP_CIPHER *cipher)
293 {
294 # ifndef OPENSSL_NO_CMAC
295 #  ifndef OPENSSL_NO_ENGINE
296     const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
297 #  endif
298     const char *cipher_name = EVP_CIPHER_name(cipher);
299     const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
300     OPENSSL_CTX *libctx =
301         prov == NULL ? NULL : ossl_provider_library_context(prov);
302     EVP_PKEY *ret = EVP_PKEY_new();
303     EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
304     EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
305     OSSL_PARAM params[4];
306     size_t paramsn = 0;
307
308     if (ret == NULL
309             || cmctx == NULL
310             || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
311         /* EVPerr already called */
312         goto err;
313     }
314
315 #  ifndef OPENSSL_NO_ENGINE
316     if (engine_id != NULL)
317         params[paramsn++] =
318             OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
319 #  endif
320
321     params[paramsn++] =
322         OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
323                                          (char *)cipher_name, 0);
324     params[paramsn++] =
325         OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
326                                           (char *)priv, len);
327     params[paramsn] = OSSL_PARAM_construct_end();
328
329     if (!EVP_MAC_CTX_set_params(cmctx, params)) {
330         EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
331         goto err;
332     }
333
334     ret->pkey.ptr = cmctx;
335     return ret;
336
337  err:
338     EVP_PKEY_free(ret);
339     EVP_MAC_CTX_free(cmctx);
340     EVP_MAC_free(cmac);
341     return NULL;
342 # else
343     EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
344            EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
345     return NULL;
346 # endif
347 }
348
349 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
350 {
351     return pkey_set_type(pkey, NULL, type, NULL, -1);
352 }
353
354 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
355 {
356     return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
357 }
358
359 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
360 {
361     if (pkey->type == type) {
362         return 1; /* it already is that type */
363     }
364
365     /*
366      * The application is requesting to alias this to a different pkey type,
367      * but not one that resolves to the base type.
368      */
369     if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
370         EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
371         return 0;
372     }
373
374     pkey->type = type;
375     return 1;
376 }
377
378 # ifndef OPENSSL_NO_ENGINE
379 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
380 {
381     if (e != NULL) {
382         if (!ENGINE_init(e)) {
383             EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
384             return 0;
385         }
386         if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
387             ENGINE_finish(e);
388             EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
389             return 0;
390         }
391     }
392     ENGINE_finish(pkey->pmeth_engine);
393     pkey->pmeth_engine = e;
394     return 1;
395 }
396
397 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
398 {
399     return pkey->engine;
400 }
401 # endif
402 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
403 {
404     int alias = type;
405
406     if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
407         const EC_GROUP *group = EC_KEY_get0_group(key);
408
409         if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
410             alias = EVP_PKEY_SM2;
411     }
412
413     if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
414         return 0;
415     if (!EVP_PKEY_set_alias_type(pkey, alias))
416         return 0;
417     pkey->pkey.ptr = key;
418     return (key != NULL);
419 }
420
421 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
422 {
423     return pkey->pkey.ptr;
424 }
425
426 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
427 {
428     ASN1_OCTET_STRING *os = NULL;
429     if (pkey->type != EVP_PKEY_HMAC) {
430         EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
431         return NULL;
432     }
433     os = EVP_PKEY_get0(pkey);
434     *len = os->length;
435     return os->data;
436 }
437
438 # ifndef OPENSSL_NO_POLY1305
439 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
440 {
441     ASN1_OCTET_STRING *os = NULL;
442     if (pkey->type != EVP_PKEY_POLY1305) {
443         EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
444         return NULL;
445     }
446     os = EVP_PKEY_get0(pkey);
447     *len = os->length;
448     return os->data;
449 }
450 # endif
451
452 # ifndef OPENSSL_NO_SIPHASH
453 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
454 {
455     ASN1_OCTET_STRING *os = NULL;
456
457     if (pkey->type != EVP_PKEY_SIPHASH) {
458         EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
459         return NULL;
460     }
461     os = EVP_PKEY_get0(pkey);
462     *len = os->length;
463     return os->data;
464 }
465 # endif
466
467 # ifndef OPENSSL_NO_RSA
468 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
469 {
470     int ret = EVP_PKEY_assign_RSA(pkey, key);
471     if (ret)
472         RSA_up_ref(key);
473     return ret;
474 }
475
476 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
477 {
478     if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
479         EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
480         return NULL;
481     }
482     return pkey->pkey.rsa;
483 }
484
485 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
486 {
487     RSA *ret = EVP_PKEY_get0_RSA(pkey);
488     if (ret != NULL)
489         RSA_up_ref(ret);
490     return ret;
491 }
492 # endif
493
494 # ifndef OPENSSL_NO_DSA
495 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
496 {
497     int ret = EVP_PKEY_assign_DSA(pkey, key);
498     if (ret)
499         DSA_up_ref(key);
500     return ret;
501 }
502
503 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
504 {
505     if (pkey->type != EVP_PKEY_DSA) {
506         EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
507         return NULL;
508     }
509     return pkey->pkey.dsa;
510 }
511
512 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
513 {
514     DSA *ret = EVP_PKEY_get0_DSA(pkey);
515     if (ret != NULL)
516         DSA_up_ref(ret);
517     return ret;
518 }
519 # endif
520
521 # ifndef OPENSSL_NO_EC
522
523 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
524 {
525     int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
526     if (ret)
527         EC_KEY_up_ref(key);
528     return ret;
529 }
530
531 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
532 {
533     if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
534         EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
535         return NULL;
536     }
537     return pkey->pkey.ec;
538 }
539
540 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
541 {
542     EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
543     if (ret != NULL)
544         EC_KEY_up_ref(ret);
545     return ret;
546 }
547 # endif
548
549 # ifndef OPENSSL_NO_DH
550
551 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
552 {
553     int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
554     int ret = EVP_PKEY_assign(pkey, type, key);
555
556     if (ret)
557         DH_up_ref(key);
558     return ret;
559 }
560
561 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
562 {
563     if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
564         EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
565         return NULL;
566     }
567     return pkey->pkey.dh;
568 }
569
570 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
571 {
572     DH *ret = EVP_PKEY_get0_DH(pkey);
573     if (ret != NULL)
574         DH_up_ref(ret);
575     return ret;
576 }
577 # endif
578
579 int EVP_PKEY_type(int type)
580 {
581     int ret;
582     const EVP_PKEY_ASN1_METHOD *ameth;
583     ENGINE *e;
584     ameth = EVP_PKEY_asn1_find(&e, type);
585     if (ameth)
586         ret = ameth->pkey_id;
587     else
588         ret = NID_undef;
589 # ifndef OPENSSL_NO_ENGINE
590     ENGINE_finish(e);
591 # endif
592     return ret;
593 }
594
595 int EVP_PKEY_id(const EVP_PKEY *pkey)
596 {
597     return pkey->type;
598 }
599
600 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
601 {
602     return EVP_PKEY_type(pkey->type);
603 }
604
605
606 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
607 {
608     BIO_set_indent(*out, saved_indent);
609     if (pop_f_prefix) {
610         BIO *next = BIO_pop(*out);
611
612         BIO_free(*out);
613         *out = next;
614     }
615     return 1;
616 }
617
618 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
619                             long indent)
620 {
621     *pop_f_prefix = 0;
622     *saved_indent = 0;
623     if (indent > 0) {
624         long i = BIO_get_indent(*out);
625
626         *saved_indent =  (i < 0 ? 0 : i);
627         if (BIO_set_indent(*out, indent) <= 0) {
628             if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
629                 return 0;
630             *pop_f_prefix = 1;
631         }
632         if (BIO_set_indent(*out, indent) <= 0) {
633             print_reset_indent(out, *pop_f_prefix, *saved_indent);
634             return 0;
635         }
636     }
637     return 1;
638 }
639
640 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
641                      const char *kstr)
642 {
643     return BIO_indent(out, indent, 128)
644         && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
645                       kstr, OBJ_nid2ln(pkey->type)) > 0;
646 }
647
648 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
649                       const char *propquery /* For provided serialization */,
650                       int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
651                                           int indent, ASN1_PCTX *pctx),
652                       ASN1_PCTX *legacy_pctx /* For legacy print */)
653 {
654     int pop_f_prefix;
655     long saved_indent;
656     OSSL_SERIALIZER_CTX *ctx = NULL;
657     int ret = -2;                /* default to unsupported */
658
659     if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
660         return 0;
661
662     ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
663     if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
664         ret = OSSL_SERIALIZER_to_bio(ctx, out);
665     OSSL_SERIALIZER_CTX_free(ctx);
666
667     if (ret != -2)
668         goto end;
669
670     /* legacy fallback */
671     if (legacy_print != NULL)
672         ret = legacy_print(out, pkey, 0, legacy_pctx);
673     else
674         ret = unsup_alg(out, pkey, 0, "Public Key");
675
676  end:
677     print_reset_indent(&out, pop_f_prefix, saved_indent);
678     return ret;
679 }
680
681 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
682                           int indent, ASN1_PCTX *pctx)
683 {
684     return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
685                       (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
686                       pctx);
687 }
688
689 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
690                            int indent, ASN1_PCTX *pctx)
691 {
692     return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
693                       (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
694                       pctx);
695 }
696
697 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
698                           int indent, ASN1_PCTX *pctx)
699 {
700     return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
701                       (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
702                       pctx);
703 }
704
705 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
706                                      int arg1, void *arg2)
707 {
708     if (pkey->pkeys[0].keymgmt == NULL)
709         return 0;
710     switch (op) {
711     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
712         {
713             char mdname[80] = "";
714             int nid;
715             int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
716                                                       sizeof(mdname));
717
718             if (rv <= 0)
719                 return rv;
720             nid = OBJ_sn2nid(mdname);
721             if (nid == NID_undef)
722                 nid = OBJ_ln2nid(mdname);
723             if (nid == NID_undef)
724                 return 0;
725             *(int *)arg2 = nid;
726             return 1;
727         }
728     default:
729         return -2;
730     }
731 }
732
733 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
734 {
735     if (pkey->ameth == NULL)
736         return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
737     if (pkey->ameth->pkey_ctrl == NULL)
738         return -2;
739     return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
740 }
741
742 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
743 {
744     return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
745 }
746
747 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
748                                      char *mdname, size_t mdname_sz)
749 {
750     if (pkey->ameth == NULL) {
751         OSSL_PARAM params[3];
752         char mddefault[100] = "";
753         char mdmandatory[100] = "";
754
755         params[0] =
756             OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
757                                              mddefault, sizeof(mddefault));
758         params[1] =
759             OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
760                                              mdmandatory,
761                                              sizeof(mdmandatory));
762         params[2] = OSSL_PARAM_construct_end();
763         if (!evp_keymgmt_get_params(pkey->pkeys[0].keymgmt,
764                                     pkey->pkeys[0].keydata,
765                                     params))
766             return 0;
767         if (mdmandatory[0] != '\0') {
768             OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
769             return 2;
770         }
771         OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
772         return 1;
773     }
774
775     {
776         int nid = NID_undef;
777         int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
778         const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
779
780         if (rv > 0)
781             OPENSSL_strlcpy(mdname, name, mdname_sz);
782         return rv;
783     }
784 }
785
786 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
787 {
788     int rv, default_nid;
789
790     rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
791     if (rv == -2) {
792         /*
793          * If there is a mandatory default digest and this isn't it, then
794          * the answer is 'no'.
795          */
796         rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
797         if (rv == 2)
798             return (nid == default_nid);
799         /* zero is an error from EVP_PKEY_get_default_digest_nid() */
800         if (rv == 0)
801             return -1;
802     }
803     return rv;
804 }
805
806 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
807                                const unsigned char *pt, size_t ptlen)
808 {
809     if (ptlen > INT_MAX)
810         return 0;
811     if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
812                            (void *)pt) <= 0)
813         return 0;
814     return 1;
815 }
816
817 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
818 {
819     int rv;
820     rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
821     if (rv <= 0)
822         return 0;
823     return rv;
824 }
825
826 #endif /* FIPS_MODE */
827
828 /*- All methods below can also be used in FIPS_MODE */
829
830 EVP_PKEY *EVP_PKEY_new(void)
831 {
832     EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
833
834     if (ret == NULL) {
835         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
836         return NULL;
837     }
838     ret->type = EVP_PKEY_NONE;
839     ret->save_type = EVP_PKEY_NONE;
840     ret->references = 1;
841     ret->save_parameters = 1;
842     ret->lock = CRYPTO_THREAD_lock_new();
843     if (ret->lock == NULL) {
844         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
845         OPENSSL_free(ret);
846         return NULL;
847     }
848     return ret;
849 }
850
851 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
852 {
853     int i;
854
855     if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
856         return 0;
857
858     REF_PRINT_COUNT("EVP_PKEY", pkey);
859     REF_ASSERT_ISNT(i < 2);
860     return ((i > 1) ? 1 : 0);
861 }
862
863 static void evp_pkey_free_it(EVP_PKEY *x)
864 {
865     /* internal function; x is never NULL */
866
867     evp_keymgmt_util_clear_pkey_cache(x);
868
869     if (x->ameth && x->ameth->pkey_free) {
870         x->ameth->pkey_free(x);
871         x->pkey.ptr = NULL;
872     }
873 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
874     ENGINE_finish(x->engine);
875     x->engine = NULL;
876     ENGINE_finish(x->pmeth_engine);
877     x->pmeth_engine = NULL;
878 #endif
879 }
880
881 void EVP_PKEY_free(EVP_PKEY *x)
882 {
883     int i;
884
885     if (x == NULL)
886         return;
887
888     CRYPTO_DOWN_REF(&x->references, &i, x->lock);
889     REF_PRINT_COUNT("EVP_PKEY", x);
890     if (i > 0)
891         return;
892     REF_ASSERT_ISNT(i < 0);
893     evp_pkey_free_it(x);
894     CRYPTO_THREAD_lock_free(x->lock);
895 #ifndef FIPS_MODE
896     sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
897 #endif
898     OPENSSL_free(x);
899 }
900
901 int EVP_PKEY_size(const EVP_PKEY *pkey)
902 {
903     if (pkey != NULL) {
904         if (pkey->ameth == NULL)
905             return pkey->cache.size;
906         else if (pkey->ameth->pkey_size != NULL)
907             return pkey->ameth->pkey_size(pkey);
908     }
909     return 0;
910 }
911
912 void *evp_pkey_make_provided(EVP_PKEY *pk, OPENSSL_CTX *libctx,
913                              EVP_KEYMGMT **keymgmt, const char *propquery)
914 {
915     EVP_KEYMGMT *allocated_keymgmt = NULL;
916     EVP_KEYMGMT *tmp_keymgmt = NULL;
917     void *keydata = NULL;
918
919     if (pk == NULL)
920         return NULL;
921
922     if (keymgmt != NULL) {
923         tmp_keymgmt = *keymgmt;
924         *keymgmt = NULL;
925     }
926
927     if (tmp_keymgmt == NULL) {
928         EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
929
930         if (ctx != NULL && ctx->keytype != NULL)
931             tmp_keymgmt = allocated_keymgmt =
932                 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
933         EVP_PKEY_CTX_free(ctx);
934     }
935
936     if (tmp_keymgmt != NULL)
937         keydata =
938             evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
939
940     /*
941      * If nothing was exported, |tmp_keymgmt| might point at a freed
942      * EVP_KEYMGMT, so we clear it to be safe.  It shouldn't be useful for
943      * the caller either way in that case.
944      */
945     if (keydata == NULL)
946         tmp_keymgmt = NULL;
947
948     if (keymgmt != NULL)
949         *keymgmt = tmp_keymgmt;
950
951     EVP_KEYMGMT_free(allocated_keymgmt);
952     return keydata;
953 }