Prepare EVP_MAC infrastructure for moving all MACs to providers
[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/core_names.h>
25
26 #include "internal/asn1_int.h"
27 #include "internal/evp_int.h"
28 #include "internal/provider.h"
29
30 static void EVP_PKEY_free_it(EVP_PKEY *x);
31
32 int EVP_PKEY_bits(const EVP_PKEY *pkey)
33 {
34     if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
35         return pkey->ameth->pkey_bits(pkey);
36     return 0;
37 }
38
39 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
40 {
41     if (pkey == NULL)
42         return 0;
43     if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
44         return -2;
45     return pkey->ameth->pkey_security_bits(pkey);
46 }
47
48 int EVP_PKEY_size(const EVP_PKEY *pkey)
49 {
50     if (pkey && pkey->ameth && pkey->ameth->pkey_size)
51         return pkey->ameth->pkey_size(pkey);
52     return 0;
53 }
54
55 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
56 {
57 #ifndef OPENSSL_NO_DSA
58     if (pkey->type == EVP_PKEY_DSA) {
59         int ret = pkey->save_parameters;
60
61         if (mode >= 0)
62             pkey->save_parameters = mode;
63         return ret;
64     }
65 #endif
66 #ifndef OPENSSL_NO_EC
67     if (pkey->type == EVP_PKEY_EC) {
68         int ret = pkey->save_parameters;
69
70         if (mode >= 0)
71             pkey->save_parameters = mode;
72         return ret;
73     }
74 #endif
75     return 0;
76 }
77
78 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
79 {
80     if (to->type == EVP_PKEY_NONE) {
81         if (EVP_PKEY_set_type(to, from->type) == 0)
82             return 0;
83     } else if (to->type != from->type) {
84         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
85         goto err;
86     }
87
88     if (EVP_PKEY_missing_parameters(from)) {
89         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
90         goto err;
91     }
92
93     if (!EVP_PKEY_missing_parameters(to)) {
94         if (EVP_PKEY_cmp_parameters(to, from) == 1)
95             return 1;
96         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
97         return 0;
98     }
99
100     if (from->ameth && from->ameth->param_copy)
101         return from->ameth->param_copy(to, from);
102  err:
103     return 0;
104 }
105
106 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
107 {
108     if (pkey->ameth && pkey->ameth->param_missing)
109         return pkey->ameth->param_missing(pkey);
110     return 0;
111 }
112
113 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
114 {
115     if (a->type != b->type)
116         return -1;
117     if (a->ameth && a->ameth->param_cmp)
118         return a->ameth->param_cmp(a, b);
119     return -2;
120 }
121
122 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
123 {
124     if (a->type != b->type)
125         return -1;
126
127     if (a->ameth) {
128         int ret;
129         /* Compare parameters if the algorithm has them */
130         if (a->ameth->param_cmp) {
131             ret = a->ameth->param_cmp(a, b);
132             if (ret <= 0)
133                 return ret;
134         }
135
136         if (a->ameth->pub_cmp)
137             return a->ameth->pub_cmp(a, b);
138     }
139
140     return -2;
141 }
142
143 EVP_PKEY *EVP_PKEY_new(void)
144 {
145     EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
146
147     if (ret == NULL) {
148         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
149         return NULL;
150     }
151     ret->type = EVP_PKEY_NONE;
152     ret->save_type = EVP_PKEY_NONE;
153     ret->references = 1;
154     ret->save_parameters = 1;
155     ret->lock = CRYPTO_THREAD_lock_new();
156     if (ret->lock == NULL) {
157         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
158         OPENSSL_free(ret);
159         return NULL;
160     }
161     return ret;
162 }
163
164 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
165 {
166     int i;
167
168     if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
169         return 0;
170
171     REF_PRINT_COUNT("EVP_PKEY", pkey);
172     REF_ASSERT_ISNT(i < 2);
173     return ((i > 1) ? 1 : 0);
174 }
175
176 /*
177  * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
178  * is NULL just return 1 or 0 if the algorithm exists.
179  */
180
181 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
182                          int len)
183 {
184     const EVP_PKEY_ASN1_METHOD *ameth;
185     ENGINE **eptr = (e == NULL) ? &e :  NULL;
186
187     if (pkey) {
188         if (pkey->pkey.ptr)
189             EVP_PKEY_free_it(pkey);
190         /*
191          * If key type matches and a method exists then this lookup has
192          * succeeded once so just indicate success.
193          */
194         if ((type == pkey->save_type) && pkey->ameth)
195             return 1;
196 #ifndef OPENSSL_NO_ENGINE
197         /* If we have ENGINEs release them */
198         ENGINE_finish(pkey->engine);
199         pkey->engine = NULL;
200         ENGINE_finish(pkey->pmeth_engine);
201         pkey->pmeth_engine = NULL;
202 #endif
203     }
204     if (str)
205         ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
206     else
207         ameth = EVP_PKEY_asn1_find(eptr, type);
208 #ifndef OPENSSL_NO_ENGINE
209     if (pkey == NULL && eptr != NULL)
210         ENGINE_finish(e);
211 #endif
212     if (ameth == NULL) {
213         EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
214         return 0;
215     }
216     if (pkey) {
217         pkey->ameth = ameth;
218         pkey->engine = e;
219
220         pkey->type = pkey->ameth->pkey_id;
221         pkey->save_type = type;
222     }
223     return 1;
224 }
225
226 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
227                                        const unsigned char *priv,
228                                        size_t len)
229 {
230     EVP_PKEY *ret = EVP_PKEY_new();
231
232     if (ret == NULL
233             || !pkey_set_type(ret, e, type, NULL, -1)) {
234         /* EVPerr already called */
235         goto err;
236     }
237
238     if (ret->ameth->set_priv_key == NULL) {
239         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
240                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
241         goto err;
242     }
243
244     if (!ret->ameth->set_priv_key(ret, priv, len)) {
245         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
246         goto err;
247     }
248
249     return ret;
250
251  err:
252     EVP_PKEY_free(ret);
253     return NULL;
254 }
255
256 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
257                                       const unsigned char *pub,
258                                       size_t len)
259 {
260     EVP_PKEY *ret = EVP_PKEY_new();
261
262     if (ret == NULL
263             || !pkey_set_type(ret, e, type, NULL, -1)) {
264         /* EVPerr already called */
265         goto err;
266     }
267
268     if (ret->ameth->set_pub_key == NULL) {
269         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
270                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
271         goto err;
272     }
273
274     if (!ret->ameth->set_pub_key(ret, pub, len)) {
275         EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
276         goto err;
277     }
278
279     return ret;
280
281  err:
282     EVP_PKEY_free(ret);
283     return NULL;
284 }
285
286 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
287                                  size_t *len)
288 {
289      if (pkey->ameth->get_priv_key == NULL) {
290         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
291                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
292         return 0;
293     }
294
295     if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
296         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
297         return 0;
298     }
299
300     return 1;
301 }
302
303 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
304                                 size_t *len)
305 {
306      if (pkey->ameth->get_pub_key == NULL) {
307         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
308                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
309         return 0;
310     }
311
312     if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
313         EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
314         return 0;
315     }
316
317     return 1;
318 }
319
320 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
321                                 size_t len, const EVP_CIPHER *cipher)
322 {
323 #ifndef OPENSSL_NO_CMAC
324     const char *engine_name = e != NULL ? ENGINE_get_name(e) : NULL;
325     const char *cipher_name = EVP_CIPHER_name(cipher);
326     const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
327     OPENSSL_CTX *libctx =
328         prov == NULL ? NULL : ossl_provider_library_context(prov);
329     EVP_PKEY *ret = EVP_PKEY_new();
330     EVP_MAC *cmac = EVP_MAC_fetch(libctx, "CMAC", NULL);
331     EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
332     OSSL_PARAM params[4];
333     size_t paramsn = 0;
334
335     if (ret == NULL
336             || cmctx == NULL
337             || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
338         /* EVPerr already called */
339         goto err;
340     }
341
342     if (engine_name != NULL)
343         params[paramsn++] =
344             OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE,
345                                              (char *)engine_name,
346                                              strlen(engine_name) + 1);
347     params[paramsn++] =
348         OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ALGORITHM,
349                                          (char *)cipher_name,
350                                          strlen(cipher_name) + 1);
351     params[paramsn++] =
352         OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
353                                           (char *)priv, len);
354     params[paramsn] = OSSL_PARAM_construct_end();
355
356     if (!EVP_MAC_CTX_set_params(cmctx, params)) {
357         EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
358         goto err;
359     }
360
361     ret->pkey.ptr = cmctx;
362     return ret;
363
364  err:
365     EVP_PKEY_free(ret);
366     EVP_MAC_CTX_free(cmctx);
367     EVP_MAC_free(cmac);
368     return NULL;
369 #else
370     EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
371            EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
372     return NULL;
373 #endif
374 }
375
376 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
377 {
378     return pkey_set_type(pkey, NULL, type, NULL, -1);
379 }
380
381 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
382 {
383     return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
384 }
385
386 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
387 {
388     if (pkey->type == type) {
389         return 1; /* it already is that type */
390     }
391
392     /*
393      * The application is requesting to alias this to a different pkey type,
394      * but not one that resolves to the base type.
395      */
396     if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
397         EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
398         return 0;
399     }
400
401     pkey->type = type;
402     return 1;
403 }
404
405 #ifndef OPENSSL_NO_ENGINE
406 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
407 {
408     if (e != NULL) {
409         if (!ENGINE_init(e)) {
410             EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
411             return 0;
412         }
413         if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
414             ENGINE_finish(e);
415             EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
416             return 0;
417         }
418     }
419     ENGINE_finish(pkey->pmeth_engine);
420     pkey->pmeth_engine = e;
421     return 1;
422 }
423
424 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
425 {
426     return pkey->engine;
427 }
428 #endif
429 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
430 {
431     if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
432         return 0;
433     pkey->pkey.ptr = key;
434     return (key != NULL);
435 }
436
437 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
438 {
439     return pkey->pkey.ptr;
440 }
441
442 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
443 {
444     ASN1_OCTET_STRING *os = NULL;
445     if (pkey->type != EVP_PKEY_HMAC) {
446         EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
447         return NULL;
448     }
449     os = EVP_PKEY_get0(pkey);
450     *len = os->length;
451     return os->data;
452 }
453
454 #ifndef OPENSSL_NO_POLY1305
455 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
456 {
457     ASN1_OCTET_STRING *os = NULL;
458     if (pkey->type != EVP_PKEY_POLY1305) {
459         EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
460         return NULL;
461     }
462     os = EVP_PKEY_get0(pkey);
463     *len = os->length;
464     return os->data;
465 }
466 #endif
467
468 #ifndef OPENSSL_NO_SIPHASH
469 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
470 {
471     ASN1_OCTET_STRING *os = NULL;
472
473     if (pkey->type != EVP_PKEY_SIPHASH) {
474         EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
475         return NULL;
476     }
477     os = EVP_PKEY_get0(pkey);
478     *len = os->length;
479     return os->data;
480 }
481 #endif
482
483 #ifndef OPENSSL_NO_RSA
484 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
485 {
486     int ret = EVP_PKEY_assign_RSA(pkey, key);
487     if (ret)
488         RSA_up_ref(key);
489     return ret;
490 }
491
492 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
493 {
494     if (pkey->type != EVP_PKEY_RSA) {
495         EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
496         return NULL;
497     }
498     return pkey->pkey.rsa;
499 }
500
501 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
502 {
503     RSA *ret = EVP_PKEY_get0_RSA(pkey);
504     if (ret != NULL)
505         RSA_up_ref(ret);
506     return ret;
507 }
508 #endif
509
510 #ifndef OPENSSL_NO_DSA
511 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
512 {
513     int ret = EVP_PKEY_assign_DSA(pkey, key);
514     if (ret)
515         DSA_up_ref(key);
516     return ret;
517 }
518
519 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
520 {
521     if (pkey->type != EVP_PKEY_DSA) {
522         EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
523         return NULL;
524     }
525     return pkey->pkey.dsa;
526 }
527
528 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
529 {
530     DSA *ret = EVP_PKEY_get0_DSA(pkey);
531     if (ret != NULL)
532         DSA_up_ref(ret);
533     return ret;
534 }
535 #endif
536
537 #ifndef OPENSSL_NO_EC
538
539 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
540 {
541     int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
542     if (ret)
543         EC_KEY_up_ref(key);
544     return ret;
545 }
546
547 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
548 {
549     if (pkey->type != EVP_PKEY_EC) {
550         EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
551         return NULL;
552     }
553     return pkey->pkey.ec;
554 }
555
556 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
557 {
558     EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
559     if (ret != NULL)
560         EC_KEY_up_ref(ret);
561     return ret;
562 }
563 #endif
564
565 #ifndef OPENSSL_NO_DH
566
567 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
568 {
569     int ret = EVP_PKEY_assign_DH(pkey, key);
570     if (ret)
571         DH_up_ref(key);
572     return ret;
573 }
574
575 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
576 {
577     if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
578         EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
579         return NULL;
580     }
581     return pkey->pkey.dh;
582 }
583
584 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
585 {
586     DH *ret = EVP_PKEY_get0_DH(pkey);
587     if (ret != NULL)
588         DH_up_ref(ret);
589     return ret;
590 }
591 #endif
592
593 int EVP_PKEY_type(int type)
594 {
595     int ret;
596     const EVP_PKEY_ASN1_METHOD *ameth;
597     ENGINE *e;
598     ameth = EVP_PKEY_asn1_find(&e, type);
599     if (ameth)
600         ret = ameth->pkey_id;
601     else
602         ret = NID_undef;
603 #ifndef OPENSSL_NO_ENGINE
604     ENGINE_finish(e);
605 #endif
606     return ret;
607 }
608
609 int EVP_PKEY_id(const EVP_PKEY *pkey)
610 {
611     return pkey->type;
612 }
613
614 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
615 {
616     return EVP_PKEY_type(pkey->type);
617 }
618
619 void EVP_PKEY_free(EVP_PKEY *x)
620 {
621     int i;
622
623     if (x == NULL)
624         return;
625
626     CRYPTO_DOWN_REF(&x->references, &i, x->lock);
627     REF_PRINT_COUNT("EVP_PKEY", x);
628     if (i > 0)
629         return;
630     REF_ASSERT_ISNT(i < 0);
631     EVP_PKEY_free_it(x);
632     CRYPTO_THREAD_lock_free(x->lock);
633     sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
634     OPENSSL_free(x);
635 }
636
637 static void EVP_PKEY_free_it(EVP_PKEY *x)
638 {
639     /* internal function; x is never NULL */
640
641     evp_keymgmt_clear_pkey_cache(x);
642
643     if (x->ameth && x->ameth->pkey_free) {
644         x->ameth->pkey_free(x);
645         x->pkey.ptr = NULL;
646     }
647 #ifndef OPENSSL_NO_ENGINE
648     ENGINE_finish(x->engine);
649     x->engine = NULL;
650     ENGINE_finish(x->pmeth_engine);
651     x->pmeth_engine = NULL;
652 #endif
653 }
654
655 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
656                      const char *kstr)
657 {
658     BIO_indent(out, indent, 128);
659     BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
660                kstr, OBJ_nid2ln(pkey->type));
661     return 1;
662 }
663
664 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
665                           int indent, ASN1_PCTX *pctx)
666 {
667     if (pkey->ameth && pkey->ameth->pub_print)
668         return pkey->ameth->pub_print(out, pkey, indent, pctx);
669
670     return unsup_alg(out, pkey, indent, "Public Key");
671 }
672
673 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
674                            int indent, ASN1_PCTX *pctx)
675 {
676     if (pkey->ameth && pkey->ameth->priv_print)
677         return pkey->ameth->priv_print(out, pkey, indent, pctx);
678
679     return unsup_alg(out, pkey, indent, "Private Key");
680 }
681
682 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
683                           int indent, ASN1_PCTX *pctx)
684 {
685     if (pkey->ameth && pkey->ameth->param_print)
686         return pkey->ameth->param_print(out, pkey, indent, pctx);
687     return unsup_alg(out, pkey, indent, "Parameters");
688 }
689
690 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
691 {
692     if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
693         return -2;
694     return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
695 }
696
697 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
698 {
699     return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
700 }
701
702 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
703 {
704     int rv, default_nid;
705
706     rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
707     if (rv == -2) {
708         /*
709          * If there is a mandatory default digest and this isn't it, then
710          * the answer is 'no'.
711          */
712         rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
713         if (rv == 2)
714             return (nid == default_nid);
715         /* zero is an error from EVP_PKEY_get_default_digest_nid() */
716         if (rv == 0)
717             return -1;
718     }
719     return rv;
720 }
721
722 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
723                                const unsigned char *pt, size_t ptlen)
724 {
725     if (ptlen > INT_MAX)
726         return 0;
727     if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
728                            (void *)pt) <= 0)
729         return 0;
730     return 1;
731 }
732
733 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
734 {
735     int rv;
736     rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
737     if (rv <= 0)
738         return 0;
739     return rv;
740 }