1 /*
2  * Copyright 1995-2021 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 /*
11  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <assert.h>
17 #include <stdio.h>
18 #include "internal/cryptlib.h"
19 #include "internal/refcount.h"
20 #include "internal/namemap.h"
21 #include <openssl/bn.h>
22 #include <openssl/err.h>
23 #include <openssl/objects.h>
24 #include <openssl/evp.h>
25 #include <openssl/rsa.h>
26 #include <openssl/dsa.h>
27 #include <openssl/dh.h>
28 #include <openssl/ec.h>
29 #include <openssl/cmac.h>
30 #ifndef FIPS_MODULE
31 # include <openssl/engine.h>
32 #endif
33 #include <openssl/params.h>
34 #include <openssl/param_build.h>
35 #include <openssl/encoder.h>
36 #include <openssl/core_names.h>
37 
38 #include "internal/numbers.h"   /* includes SIZE_MAX */
39 #include "internal/ffc.h"
40 #include "crypto/evp.h"
41 #include "crypto/dh.h"
42 #include "crypto/dsa.h"
43 #include "crypto/ec.h"
44 #include "crypto/ecx.h"
45 #include "crypto/rsa.h"
46 #ifndef FIPS_MODULE
47 # include "crypto/asn1.h"
48 # include "crypto/x509.h"
49 #endif
50 #include "internal/provider.h"
51 #include "evp_local.h"
52 
53 #include "e_os.h"                /* strcasecmp on Windows */
54 
55 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
56                          int len, EVP_KEYMGMT *keymgmt);
57 static void evp_pkey_free_it(EVP_PKEY *key);
58 
59 #ifndef FIPS_MODULE
60 
61 /* The type of parameters selected in key parameter functions */
62 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
63 
EVP_PKEY_get_bits(const EVP_PKEY * pkey)64 int EVP_PKEY_get_bits(const EVP_PKEY *pkey)
65 {
66     int size = 0;
67 
68     if (pkey != NULL) {
69         size = pkey->cache.bits;
70         if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
71             size = pkey->ameth->pkey_bits(pkey);
72     }
73     return size < 0 ? 0 : size;
74 }
75 
EVP_PKEY_get_security_bits(const EVP_PKEY * pkey)76 int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey)
77 {
78     int size = 0;
79 
80     if (pkey != NULL) {
81         size = pkey->cache.security_bits;
82         if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
83             size = pkey->ameth->pkey_security_bits(pkey);
84     }
85     return size < 0 ? 0 : size;
86 }
87 
EVP_PKEY_save_parameters(EVP_PKEY * pkey,int mode)88 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
89 {
90 # ifndef OPENSSL_NO_DSA
91     if (pkey->type == EVP_PKEY_DSA) {
92         int ret = pkey->save_parameters;
93 
94         if (mode >= 0)
95             pkey->save_parameters = mode;
96         return ret;
97     }
98 # endif
99 # ifndef OPENSSL_NO_EC
100     if (pkey->type == EVP_PKEY_EC) {
101         int ret = pkey->save_parameters;
102 
103         if (mode >= 0)
104             pkey->save_parameters = mode;
105         return ret;
106     }
107 # endif
108     return 0;
109 }
110 
EVP_PKEY_set_ex_data(EVP_PKEY * key,int idx,void * arg)111 int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
112 {
113     return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
114 }
115 
EVP_PKEY_get_ex_data(const EVP_PKEY * key,int idx)116 void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
117 {
118     return CRYPTO_get_ex_data(&key->ex_data, idx);
119 }
120 
EVP_PKEY_copy_parameters(EVP_PKEY * to,const EVP_PKEY * from)121 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
122 {
123     /*
124      * Clean up legacy stuff from this function when legacy support is gone.
125      */
126 
127     EVP_PKEY *downgraded_from = NULL;
128     int ok = 0;
129 
130     /*
131      * If |to| is a legacy key and |from| isn't, we must make a downgraded
132      * copy of |from|.  If that fails, this function fails.
133      */
134     if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) {
135         if (!evp_pkey_copy_downgraded(&downgraded_from, from))
136             goto end;
137         from = downgraded_from;
138     }
139 
140     /*
141      * Make sure |to| is typed.  Content is less important at this early
142      * stage.
143      *
144      * 1.  If |to| is untyped, assign |from|'s key type to it.
145      * 2.  If |to| contains a legacy key, compare its |type| to |from|'s.
146      *     (|from| was already downgraded above)
147      *
148      * If |to| is a provided key, there's nothing more to do here, functions
149      * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
150      * further down help us find out if they are the same or not.
151      */
152     if (evp_pkey_is_blank(to)) {
153         if (evp_pkey_is_legacy(from)) {
154             if (EVP_PKEY_set_type(to, from->type) == 0)
155                 goto end;
156         } else {
157             if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
158                 goto end;
159         }
160     } else if (evp_pkey_is_legacy(to)) {
161         if (to->type != from->type) {
162             ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
163             goto end;
164         }
165     }
166 
167     if (EVP_PKEY_missing_parameters(from)) {
168         ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);
169         goto end;
170     }
171 
172     if (!EVP_PKEY_missing_parameters(to)) {
173         if (EVP_PKEY_parameters_eq(to, from) == 1)
174             ok = 1;
175         else
176             ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
177         goto end;
178     }
179 
180     /* For purely provided keys, we just call the keymgmt utility */
181     if (to->keymgmt != NULL && from->keymgmt != NULL) {
182         ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
183         goto end;
184     }
185 
186     /*
187      * If |to| is provided, we know that |from| is legacy at this point.
188      * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
189      * to copy the appropriate data to |to|'s keydata.
190      * We cannot override existing data so do it only if there is no keydata
191      * in |to| yet.
192      */
193     if (to->keymgmt != NULL && to->keydata == NULL) {
194         EVP_KEYMGMT *to_keymgmt = to->keymgmt;
195         void *from_keydata =
196             evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
197                                         NULL);
198 
199         /*
200          * If we get a NULL, it could be an internal error, or it could be
201          * that there's a key mismatch.  We're pretending the latter...
202          */
203         if (from_keydata == NULL)
204             ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
205         else
206             ok = (to->keydata = evp_keymgmt_dup(to->keymgmt,
207                                                 from_keydata,
208                                                 SELECT_PARAMETERS)) != NULL;
209         goto end;
210     }
211 
212     /* Both keys are legacy */
213     if (from->ameth != NULL && from->ameth->param_copy != NULL)
214         ok = from->ameth->param_copy(to, from);
215  end:
216     EVP_PKEY_free(downgraded_from);
217     return ok;
218 }
219 
EVP_PKEY_missing_parameters(const EVP_PKEY * pkey)220 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
221 {
222     if (pkey != NULL) {
223         if (pkey->keymgmt != NULL)
224             return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
225         else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
226             return pkey->ameth->param_missing(pkey);
227     }
228     return 0;
229 }
230 
231 /*
232  * This function is called for any mixture of keys except pure legacy pair.
233  * When legacy keys are gone, we replace a call to this functions with
234  * a call to evp_keymgmt_util_match().
235  */
evp_pkey_cmp_any(const EVP_PKEY * a,const EVP_PKEY * b,int selection)236 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
237                             int selection)
238 {
239     EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
240     void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
241 
242     /* If none of them are provided, this function shouldn't have been called */
243     if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))
244         return -2;
245 
246     /* For purely provided keys, we just call the keymgmt utility */
247     if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
248         return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
249 
250     /*
251      * At this point, one of them is provided, the other not.  This allows
252      * us to compare types using legacy NIDs.
253      */
254     if (evp_pkey_is_legacy(a)
255         && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
256         return -1;               /* not the same key type */
257     if (evp_pkey_is_legacy(b)
258         && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))
259         return -1;               /* not the same key type */
260 
261     /*
262      * We've determined that they both are the same keytype, so the next
263      * step is to do a bit of cross export to ensure we have keydata for
264      * both keys in the same keymgmt.
265      */
266     keymgmt1 = a->keymgmt;
267     keydata1 = a->keydata;
268     keymgmt2 = b->keymgmt;
269     keydata2 = b->keydata;
270 
271     if (keymgmt2 != NULL && keymgmt2->match != NULL) {
272         tmp_keydata =
273             evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
274         if (tmp_keydata != NULL) {
275             keymgmt1 = keymgmt2;
276             keydata1 = tmp_keydata;
277         }
278     }
279     if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
280         tmp_keydata =
281             evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
282         if (tmp_keydata != NULL) {
283             keymgmt2 = keymgmt1;
284             keydata2 = tmp_keydata;
285         }
286     }
287 
288     /* If we still don't have matching keymgmt implementations, we give up */
289     if (keymgmt1 != keymgmt2)
290         return -2;
291 
292     /* If the keymgmt implementations are NULL, the export failed */
293     if (keymgmt1 == NULL)
294         return -2;
295 
296     return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
297 }
298 
299 # ifndef OPENSSL_NO_DEPRECATED_3_0
EVP_PKEY_cmp_parameters(const EVP_PKEY * a,const EVP_PKEY * b)300 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
301 {
302     return EVP_PKEY_parameters_eq(a, b);
303 }
304 #endif
305 
EVP_PKEY_parameters_eq(const EVP_PKEY * a,const EVP_PKEY * b)306 int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)
307 {
308     /*
309      * This will just call evp_keymgmt_util_match when legacy support
310      * is gone.
311      */
312 
313     if (a->keymgmt != NULL || b->keymgmt != NULL)
314         return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
315 
316     /* All legacy keys */
317     if (a->type != b->type)
318         return -1;
319     if (a->ameth != NULL && a->ameth->param_cmp != NULL)
320         return a->ameth->param_cmp(a, b);
321     return -2;
322 }
323 
324 # ifndef OPENSSL_NO_DEPRECATED_3_0
EVP_PKEY_cmp(const EVP_PKEY * a,const EVP_PKEY * b)325 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
326 {
327     return EVP_PKEY_eq(a, b);
328 }
329 #endif
330 
EVP_PKEY_eq(const EVP_PKEY * a,const EVP_PKEY * b)331 int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
332 {
333     /*
334      * This will just call evp_keymgmt_util_match when legacy support
335      * is gone.
336      */
337 
338     /* Trivial shortcuts */
339     if (a == b)
340         return 1;
341     if (a == NULL || b == NULL)
342         return 0;
343 
344     if (a->keymgmt != NULL || b->keymgmt != NULL)
345         return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS
346                                        | OSSL_KEYMGMT_SELECT_KEYPAIR));
347 
348     /* All legacy keys */
349     if (a->type != b->type)
350         return -1;
351 
352     if (a->ameth != NULL) {
353         int ret;
354         /* Compare parameters if the algorithm has them */
355         if (a->ameth->param_cmp != NULL) {
356             ret = a->ameth->param_cmp(a, b);
357             if (ret <= 0)
358                 return ret;
359         }
360 
361         if (a->ameth->pub_cmp != NULL)
362             return a->ameth->pub_cmp(a, b);
363     }
364 
365     return -2;
366 }
367 
368 
new_raw_key_int(OSSL_LIB_CTX * libctx,const char * strtype,const char * propq,int nidtype,ENGINE * e,const unsigned char * key,size_t len,int key_is_priv)369 static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,
370                                  const char *strtype,
371                                  const char *propq,
372                                  int nidtype,
373                                  ENGINE *e,
374                                  const unsigned char *key,
375                                  size_t len,
376                                  int key_is_priv)
377 {
378     EVP_PKEY *pkey = NULL;
379     EVP_PKEY_CTX *ctx = NULL;
380     const EVP_PKEY_ASN1_METHOD *ameth = NULL;
381     int result = 0;
382 
383 # ifndef OPENSSL_NO_ENGINE
384     /* Check if there is an Engine for this type */
385     if (e == NULL) {
386         ENGINE *tmpe = NULL;
387 
388         if (strtype != NULL)
389             ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1);
390         else if (nidtype != EVP_PKEY_NONE)
391             ameth = EVP_PKEY_asn1_find(&tmpe, nidtype);
392 
393         /* If tmpe is NULL then no engine is claiming to support this type */
394         if (tmpe == NULL)
395             ameth = NULL;
396 
397         ENGINE_finish(tmpe);
398     }
399 # endif
400 
401     if (e == NULL && ameth == NULL) {
402         /*
403          * No engine is claiming to support this type, so lets see if we have
404          * a provider.
405          */
406         ctx = EVP_PKEY_CTX_new_from_name(libctx,
407                                          strtype != NULL ? strtype
408                                                          : OBJ_nid2sn(nidtype),
409                                          propq);
410         if (ctx == NULL)
411             goto err;
412         /* May fail if no provider available */
413         ERR_set_mark();
414         if (EVP_PKEY_fromdata_init(ctx) == 1) {
415             OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
416 
417             ERR_clear_last_mark();
418             params[0] = OSSL_PARAM_construct_octet_string(
419                             key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
420                                         : OSSL_PKEY_PARAM_PUB_KEY,
421                             (void *)key, len);
422 
423             if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
424                 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
425                 goto err;
426             }
427 
428             EVP_PKEY_CTX_free(ctx);
429 
430             return pkey;
431         }
432         ERR_pop_to_mark();
433         /* else not supported so fallback to legacy */
434     }
435 
436     /* Legacy code path */
437 
438     pkey = EVP_PKEY_new();
439     if (pkey == NULL) {
440         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
441         goto err;
442     }
443 
444     if (!pkey_set_type(pkey, e, nidtype, strtype, -1, NULL)) {
445         /* EVPerr already called */
446         goto err;
447     }
448 
449     if (!ossl_assert(pkey->ameth != NULL))
450         goto err;
451 
452     if (key_is_priv) {
453         if (pkey->ameth->set_priv_key == NULL) {
454             ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
455             goto err;
456         }
457 
458         if (!pkey->ameth->set_priv_key(pkey, key, len)) {
459             ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
460             goto err;
461         }
462     } else {
463         if (pkey->ameth->set_pub_key == NULL) {
464             ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
465             goto err;
466         }
467 
468         if (!pkey->ameth->set_pub_key(pkey, key, len)) {
469             ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
470             goto err;
471         }
472     }
473 
474     result = 1;
475  err:
476     if (!result) {
477         EVP_PKEY_free(pkey);
478         pkey = NULL;
479     }
480     EVP_PKEY_CTX_free(ctx);
481     return pkey;
482 }
483 
EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX * libctx,const char * keytype,const char * propq,const unsigned char * priv,size_t len)484 EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
485                                           const char *keytype,
486                                           const char *propq,
487                                           const unsigned char *priv, size_t len)
488 {
489     return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv,
490                            len, 1);
491 }
492 
EVP_PKEY_new_raw_private_key(int type,ENGINE * e,const unsigned char * priv,size_t len)493 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
494                                        const unsigned char *priv,
495                                        size_t len)
496 {
497     return new_raw_key_int(NULL, NULL, NULL, type, e, priv, len, 1);
498 }
499 
EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX * libctx,const char * keytype,const char * propq,const unsigned char * pub,size_t len)500 EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
501                                          const char *keytype, const char *propq,
502                                          const unsigned char *pub, size_t len)
503 {
504     return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub,
505                            len, 0);
506 }
507 
EVP_PKEY_new_raw_public_key(int type,ENGINE * e,const unsigned char * pub,size_t len)508 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
509                                       const unsigned char *pub,
510                                       size_t len)
511 {
512     return new_raw_key_int(NULL, NULL, NULL, type, e, pub, len, 0);
513 }
514 
515 struct raw_key_details_st
516 {
517     unsigned char **key;
518     size_t *len;
519     int selection;
520 };
521 
522 static OSSL_CALLBACK get_raw_key_details;
get_raw_key_details(const OSSL_PARAM params[],void * arg)523 static int get_raw_key_details(const OSSL_PARAM params[], void *arg)
524 {
525     const OSSL_PARAM *p = NULL;
526     struct raw_key_details_st *raw_key = arg;
527 
528     if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
529         if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))
530                 != NULL)
531             return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
532                                                raw_key->key == NULL ? 0 : *raw_key->len,
533                                                raw_key->len);
534     } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
535         if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))
536                 != NULL)
537             return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
538                                                raw_key->key == NULL ? 0 : *raw_key->len,
539                                                raw_key->len);
540     }
541 
542     return 0;
543 }
544 
EVP_PKEY_get_raw_private_key(const EVP_PKEY * pkey,unsigned char * priv,size_t * len)545 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
546                                  size_t *len)
547 {
548     if (pkey->keymgmt != NULL) {
549         struct raw_key_details_st raw_key;
550 
551         raw_key.key = priv == NULL ? NULL : &priv;
552         raw_key.len = len;
553         raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
554 
555         return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
556                                        get_raw_key_details, &raw_key);
557     }
558 
559     if (pkey->ameth == NULL) {
560         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
561         return 0;
562     }
563 
564     if (pkey->ameth->get_priv_key == NULL) {
565         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
566         return 0;
567     }
568 
569     if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
570         ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
571         return 0;
572     }
573 
574     return 1;
575 }
576 
EVP_PKEY_get_raw_public_key(const EVP_PKEY * pkey,unsigned char * pub,size_t * len)577 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
578                                 size_t *len)
579 {
580     if (pkey->keymgmt != NULL) {
581         struct raw_key_details_st raw_key;
582 
583         raw_key.key = pub == NULL ? NULL : &pub;
584         raw_key.len = len;
585         raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
586 
587         return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
588                                        get_raw_key_details, &raw_key);
589     }
590 
591     if (pkey->ameth == NULL) {
592         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
593         return 0;
594     }
595 
596      if (pkey->ameth->get_pub_key == NULL) {
597         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
598         return 0;
599     }
600 
601     if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
602         ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
603         return 0;
604     }
605 
606     return 1;
607 }
608 
new_cmac_key_int(const unsigned char * priv,size_t len,const char * cipher_name,const EVP_CIPHER * cipher,OSSL_LIB_CTX * libctx,const char * propq,ENGINE * e)609 static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,
610                                   const char *cipher_name,
611                                   const EVP_CIPHER *cipher,
612                                   OSSL_LIB_CTX *libctx,
613                                   const char *propq, ENGINE *e)
614 {
615 # ifndef OPENSSL_NO_CMAC
616 #  ifndef OPENSSL_NO_ENGINE
617     const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
618 #  endif
619     OSSL_PARAM params[5], *p = params;
620     EVP_PKEY *pkey = NULL;
621     EVP_PKEY_CTX *ctx;
622 
623     if (cipher != NULL)
624         cipher_name = EVP_CIPHER_get0_name(cipher);
625 
626     if (cipher_name == NULL) {
627         ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
628         return NULL;
629     }
630 
631     ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);
632     if (ctx == NULL)
633         goto err;
634 
635     if (EVP_PKEY_fromdata_init(ctx) <= 0) {
636         ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
637         goto err;
638     }
639 
640     *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
641                                             (void *)priv, len);
642     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
643                                             (char *)cipher_name, 0);
644     if (propq != NULL)
645         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,
646                                                 (char *)propq, 0);
647 #  ifndef OPENSSL_NO_ENGINE
648     if (engine_id != NULL)
649         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE,
650                                                 (char *)engine_id, 0);
651 #  endif
652     *p = OSSL_PARAM_construct_end();
653 
654     if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) {
655         ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
656         goto err;
657     }
658 
659  err:
660     EVP_PKEY_CTX_free(ctx);
661 
662     return pkey;
663 # else
664     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
665     return NULL;
666 # endif
667 }
668 
EVP_PKEY_new_CMAC_key(ENGINE * e,const unsigned char * priv,size_t len,const EVP_CIPHER * cipher)669 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
670                                 size_t len, const EVP_CIPHER *cipher)
671 {
672     return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);
673 }
674 
EVP_PKEY_set_type(EVP_PKEY * pkey,int type)675 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
676 {
677     return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
678 }
679 
EVP_PKEY_set_type_str(EVP_PKEY * pkey,const char * str,int len)680 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
681 {
682     return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
683 }
684 
685 # ifndef OPENSSL_NO_ENGINE
EVP_PKEY_set1_engine(EVP_PKEY * pkey,ENGINE * e)686 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
687 {
688     if (e != NULL) {
689         if (!ENGINE_init(e)) {
690             ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
691             return 0;
692         }
693         if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
694             ENGINE_finish(e);
695             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
696             return 0;
697         }
698     }
699     ENGINE_finish(pkey->pmeth_engine);
700     pkey->pmeth_engine = e;
701     return 1;
702 }
703 
EVP_PKEY_get0_engine(const EVP_PKEY * pkey)704 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
705 {
706     return pkey->engine;
707 }
708 # endif
709 
710 # ifndef OPENSSL_NO_DEPRECATED_3_0
detect_foreign_key(EVP_PKEY * pkey)711 static void detect_foreign_key(EVP_PKEY *pkey)
712 {
713     switch (pkey->type) {
714     case EVP_PKEY_RSA:
715         pkey->foreign = pkey->pkey.rsa != NULL
716                         && ossl_rsa_is_foreign(pkey->pkey.rsa);
717         break;
718 #  ifndef OPENSSL_NO_EC
719     case EVP_PKEY_SM2:
720     case EVP_PKEY_EC:
721         pkey->foreign = pkey->pkey.ec != NULL
722                         && ossl_ec_key_is_foreign(pkey->pkey.ec);
723         break;
724 #  endif
725 #  ifndef OPENSSL_NO_DSA
726     case EVP_PKEY_DSA:
727         pkey->foreign = pkey->pkey.dsa != NULL
728                         && ossl_dsa_is_foreign(pkey->pkey.dsa);
729         break;
730 #endif
731 #  ifndef OPENSSL_NO_DH
732     case EVP_PKEY_DH:
733         pkey->foreign = pkey->pkey.dh != NULL
734                         && ossl_dh_is_foreign(pkey->pkey.dh);
735         break;
736 #endif
737     default:
738         pkey->foreign = 0;
739         break;
740     }
741 }
742 
EVP_PKEY_assign(EVP_PKEY * pkey,int type,void * key)743 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
744 {
745 #  ifndef OPENSSL_NO_EC
746     int pktype;
747 
748     pktype = EVP_PKEY_type(type);
749     if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
750         const EC_GROUP *group = EC_KEY_get0_group(key);
751 
752         if (group != NULL) {
753             int curve = EC_GROUP_get_curve_name(group);
754 
755             /*
756              * Regardless of what is requested the SM2 curve must be SM2 type,
757              * and non SM2 curves are EC type.
758              */
759             if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
760                 type = EVP_PKEY_SM2;
761             else if(curve != NID_sm2 && pktype == EVP_PKEY_SM2)
762                 type = EVP_PKEY_EC;
763         }
764     }
765 #  endif
766 
767     if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
768         return 0;
769 
770     pkey->pkey.ptr = key;
771     detect_foreign_key(pkey);
772 
773     return (key != NULL);
774 }
775 # endif
776 
EVP_PKEY_get0(const EVP_PKEY * pkey)777 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
778 {
779     if (pkey == NULL)
780         return NULL;
781 
782     if (!evp_pkey_is_provided(pkey))
783         return pkey->pkey.ptr;
784 
785     return NULL;
786 }
787 
EVP_PKEY_get0_hmac(const EVP_PKEY * pkey,size_t * len)788 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
789 {
790     const ASN1_OCTET_STRING *os = NULL;
791     if (pkey->type != EVP_PKEY_HMAC) {
792         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
793         return NULL;
794     }
795     os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
796     if (os != NULL) {
797         *len = os->length;
798         return os->data;
799     }
800     return NULL;
801 }
802 
803 # ifndef OPENSSL_NO_POLY1305
EVP_PKEY_get0_poly1305(const EVP_PKEY * pkey,size_t * len)804 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
805 {
806     const ASN1_OCTET_STRING *os = NULL;
807     if (pkey->type != EVP_PKEY_POLY1305) {
808         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
809         return NULL;
810     }
811     os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
812     if (os != NULL) {
813         *len = os->length;
814         return os->data;
815     }
816     return NULL;
817 }
818 # endif
819 
820 # ifndef OPENSSL_NO_SIPHASH
EVP_PKEY_get0_siphash(const EVP_PKEY * pkey,size_t * len)821 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
822 {
823     const ASN1_OCTET_STRING *os = NULL;
824 
825     if (pkey->type != EVP_PKEY_SIPHASH) {
826         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
827         return NULL;
828     }
829     os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
830     if (os != NULL) {
831         *len = os->length;
832         return os->data;
833     }
834     return NULL;
835 }
836 # endif
837 
838 # ifndef OPENSSL_NO_DSA
evp_pkey_get0_DSA_int(const EVP_PKEY * pkey)839 static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
840 {
841     if (pkey->type != EVP_PKEY_DSA) {
842         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
843         return NULL;
844     }
845     return evp_pkey_get_legacy((EVP_PKEY *)pkey);
846 }
847 
EVP_PKEY_get0_DSA(const EVP_PKEY * pkey)848 const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
849 {
850     return evp_pkey_get0_DSA_int(pkey);
851 }
852 
EVP_PKEY_set1_DSA(EVP_PKEY * pkey,DSA * key)853 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
854 {
855     int ret = EVP_PKEY_assign_DSA(pkey, key);
856     if (ret)
857         DSA_up_ref(key);
858     return ret;
859 }
EVP_PKEY_get1_DSA(EVP_PKEY * pkey)860 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
861 {
862     DSA *ret = evp_pkey_get0_DSA_int(pkey);
863 
864     if (ret != NULL)
865         DSA_up_ref(ret);
866     return ret;
867 }
868 # endif /*  OPENSSL_NO_DSA */
869 
870 # ifndef OPENSSL_NO_EC
evp_pkey_get0_ECX_KEY(const EVP_PKEY * pkey,int type)871 static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
872 {
873     if (EVP_PKEY_get_base_id(pkey) != type) {
874         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
875         return NULL;
876     }
877     return evp_pkey_get_legacy((EVP_PKEY *)pkey);
878 }
879 
evp_pkey_get1_ECX_KEY(EVP_PKEY * pkey,int type)880 static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
881 {
882     ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
883 
884     if (ret != NULL && !ossl_ecx_key_up_ref(ret))
885         ret = NULL;
886     return ret;
887 }
888 
889 #  define IMPLEMENT_ECX_VARIANT(NAME)                                   \
890     ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey)                  \
891     {                                                                   \
892         return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME);            \
893     }
894 IMPLEMENT_ECX_VARIANT(X25519)
IMPLEMENT_ECX_VARIANT(X448)895 IMPLEMENT_ECX_VARIANT(X448)
896 IMPLEMENT_ECX_VARIANT(ED25519)
897 IMPLEMENT_ECX_VARIANT(ED448)
898 
899 # endif
900 
901 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
902 
903 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)
904 {
905     int ret, type;
906 
907     /*
908      * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
909      * related to ffdhe and modp (which cache q = (p - 1) / 2),
910      * and returns 0 for all other dh parameter generation types including
911      * RFC5114 named groups.
912      *
913      * The EVP_PKEY_DH type is used for dh parameter generation types:
914      *  - named safe prime groups related to ffdhe and modp
915      *  - safe prime generator
916      *
917      * The type EVP_PKEY_DHX is used for dh parameter generation types
918      *  - fips186-4 and fips186-2
919      *  - rfc5114 named groups.
920      *
921      * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
922      * without a q value.
923      * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
924      * q value to be stored.
925      */
926     if (ossl_dh_is_named_safe_prime_group(dhkey))
927         type = EVP_PKEY_DH;
928     else
929         type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
930 
931     ret = EVP_PKEY_assign(pkey, type, dhkey);
932 
933     if (ret)
934         DH_up_ref(dhkey);
935     return ret;
936 }
937 
evp_pkey_get0_DH_int(const EVP_PKEY * pkey)938 DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
939 {
940     if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
941         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
942         return NULL;
943     }
944     return evp_pkey_get_legacy((EVP_PKEY *)pkey);
945 }
946 
EVP_PKEY_get0_DH(const EVP_PKEY * pkey)947 const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
948 {
949     return evp_pkey_get0_DH_int(pkey);
950 }
951 
EVP_PKEY_get1_DH(EVP_PKEY * pkey)952 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
953 {
954     DH *ret = evp_pkey_get0_DH_int(pkey);
955 
956     if (ret != NULL)
957         DH_up_ref(ret);
958     return ret;
959 }
960 # endif
961 
EVP_PKEY_type(int type)962 int EVP_PKEY_type(int type)
963 {
964     int ret;
965     const EVP_PKEY_ASN1_METHOD *ameth;
966     ENGINE *e;
967     ameth = EVP_PKEY_asn1_find(&e, type);
968     if (ameth)
969         ret = ameth->pkey_id;
970     else
971         ret = NID_undef;
972 # ifndef OPENSSL_NO_ENGINE
973     ENGINE_finish(e);
974 # endif
975     return ret;
976 }
977 
EVP_PKEY_get_id(const EVP_PKEY * pkey)978 int EVP_PKEY_get_id(const EVP_PKEY *pkey)
979 {
980     return pkey->type;
981 }
982 
EVP_PKEY_get_base_id(const EVP_PKEY * pkey)983 int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
984 {
985     return EVP_PKEY_type(pkey->type);
986 }
987 
988 /*
989  * These hard coded cases are pure hackery to get around the fact
990  * that names in crypto/objects/objects.txt are a mess.  There is
991  * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
992  * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
993  * the NID of which is used for EVP_PKEY_RSA.  Strangely enough,
994  * "DSA" is accurate...  but still, better be safe and hard-code
995  * names that we know.
996  * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
997  * EVP_PKEY_EC, because of aliasing.
998  * This should be cleaned away along with all other #legacy support.
999  */
1000 static const OSSL_ITEM standard_name2type[] = {
1001     { EVP_PKEY_RSA,     "RSA" },
1002     { EVP_PKEY_RSA_PSS, "RSA-PSS" },
1003     { EVP_PKEY_EC,      "EC" },
1004     { EVP_PKEY_ED25519, "ED25519" },
1005     { EVP_PKEY_ED448,   "ED448" },
1006     { EVP_PKEY_X25519,  "X25519" },
1007     { EVP_PKEY_X448,    "X448" },
1008     { EVP_PKEY_SM2,     "SM2" },
1009     { EVP_PKEY_DH,      "DH" },
1010     { EVP_PKEY_DHX,     "X9.42 DH" },
1011     { EVP_PKEY_DHX,     "DHX" },
1012     { EVP_PKEY_DSA,     "DSA" },
1013 };
1014 
evp_pkey_name2type(const char * name)1015 int evp_pkey_name2type(const char *name)
1016 {
1017     int type;
1018     size_t i;
1019 
1020     for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1021         if (strcasecmp(name, standard_name2type[i].ptr) == 0)
1022             return (int)standard_name2type[i].id;
1023     }
1024 
1025     if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
1026         return type;
1027     return EVP_PKEY_type(OBJ_ln2nid(name));
1028 }
1029 
evp_pkey_type2name(int type)1030 const char *evp_pkey_type2name(int type)
1031 {
1032     size_t i;
1033 
1034     for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1035         if (type == (int)standard_name2type[i].id)
1036             return standard_name2type[i].ptr;
1037     }
1038 
1039     return OBJ_nid2sn(type);
1040 }
1041 
EVP_PKEY_is_a(const EVP_PKEY * pkey,const char * name)1042 int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
1043 {
1044     if (pkey->keymgmt == NULL) {
1045         int type = evp_pkey_name2type(name);
1046 
1047         return pkey->type == type;
1048     }
1049     return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1050 }
1051 
EVP_PKEY_type_names_do_all(const EVP_PKEY * pkey,void (* fn)(const char * name,void * data),void * data)1052 int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
1053                                void (*fn)(const char *name, void *data),
1054                                void *data)
1055 {
1056     if (!evp_pkey_is_typed(pkey))
1057         return 0;
1058 
1059     if (!evp_pkey_is_provided(pkey)) {
1060         const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));
1061 
1062         fn(name, data);
1063         return 1;
1064     }
1065     return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
1066 }
1067 
EVP_PKEY_can_sign(const EVP_PKEY * pkey)1068 int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1069 {
1070     if (pkey->keymgmt == NULL) {
1071         switch (EVP_PKEY_get_base_id(pkey)) {
1072         case EVP_PKEY_RSA:
1073             return 1;
1074 # ifndef OPENSSL_NO_DSA
1075         case EVP_PKEY_DSA:
1076             return 1;
1077 # endif
1078 # ifndef OPENSSL_NO_EC
1079         case EVP_PKEY_ED25519:
1080         case EVP_PKEY_ED448:
1081             return 1;
1082         case EVP_PKEY_EC:        /* Including SM2 */
1083             return EC_KEY_can_sign(pkey->pkey.ec);
1084 # endif
1085         default:
1086             break;
1087         }
1088     } else {
1089         const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
1090         OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1091         const char *supported_sig =
1092             pkey->keymgmt->query_operation_name != NULL
1093             ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
1094             : EVP_KEYMGMT_get0_name(pkey->keymgmt);
1095         EVP_SIGNATURE *signature = NULL;
1096 
1097         signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
1098         if (signature != NULL) {
1099             EVP_SIGNATURE_free(signature);
1100             return 1;
1101         }
1102     }
1103     return 0;
1104 }
1105 
print_reset_indent(BIO ** out,int pop_f_prefix,long saved_indent)1106 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1107 {
1108     BIO_set_indent(*out, saved_indent);
1109     if (pop_f_prefix) {
1110         BIO *next = BIO_pop(*out);
1111 
1112         BIO_free(*out);
1113         *out = next;
1114     }
1115     return 1;
1116 }
1117 
print_set_indent(BIO ** out,int * pop_f_prefix,long * saved_indent,long indent)1118 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1119                             long indent)
1120 {
1121     *pop_f_prefix = 0;
1122     *saved_indent = 0;
1123     if (indent > 0) {
1124         long i = BIO_get_indent(*out);
1125 
1126         *saved_indent =  (i < 0 ? 0 : i);
1127         if (BIO_set_indent(*out, indent) <= 0) {
1128             if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
1129                 return 0;
1130             *pop_f_prefix = 1;
1131         }
1132         if (BIO_set_indent(*out, indent) <= 0) {
1133             print_reset_indent(out, *pop_f_prefix, *saved_indent);
1134             return 0;
1135         }
1136     }
1137     return 1;
1138 }
1139 
unsup_alg(BIO * out,const EVP_PKEY * pkey,int indent,const char * kstr)1140 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
1141                      const char *kstr)
1142 {
1143     return BIO_indent(out, indent, 128)
1144         && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1145                       kstr, OBJ_nid2ln(pkey->type)) > 0;
1146 }
1147 
print_pkey(const EVP_PKEY * pkey,BIO * out,int indent,int selection,const char * propquery,int (* legacy_print)(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx),ASN1_PCTX * legacy_pctx)1148 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
1149                       int selection /* For provided encoding */,
1150                       const char *propquery /* For provided encoding */,
1151                       int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1152                                           int indent, ASN1_PCTX *pctx),
1153                       ASN1_PCTX *legacy_pctx /* For legacy print */)
1154 {
1155     int pop_f_prefix;
1156     long saved_indent;
1157     OSSL_ENCODER_CTX *ctx = NULL;
1158     int ret = -2;                /* default to unsupported */
1159 
1160     if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1161         return 0;
1162 
1163     ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1164                                         propquery);
1165     if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1166         ret = OSSL_ENCODER_to_bio(ctx, out);
1167     OSSL_ENCODER_CTX_free(ctx);
1168 
1169     if (ret != -2)
1170         goto end;
1171 
1172     /* legacy fallback */
1173     if (legacy_print != NULL)
1174         ret = legacy_print(out, pkey, 0, legacy_pctx);
1175     else
1176         ret = unsup_alg(out, pkey, 0, "Public Key");
1177 
1178  end:
1179     print_reset_indent(&out, pop_f_prefix, saved_indent);
1180     return ret;
1181 }
1182 
EVP_PKEY_print_public(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)1183 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1184                           int indent, ASN1_PCTX *pctx)
1185 {
1186     return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1187                       (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1188                       pctx);
1189 }
1190 
EVP_PKEY_print_private(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)1191 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1192                            int indent, ASN1_PCTX *pctx)
1193 {
1194     return print_pkey(pkey, out, indent, EVP_PKEY_KEYPAIR, NULL,
1195                       (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1196                       pctx);
1197 }
1198 
EVP_PKEY_print_params(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)1199 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1200                           int indent, ASN1_PCTX *pctx)
1201 {
1202     return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1203                       (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1204                       pctx);
1205 }
1206 
1207 # ifndef OPENSSL_NO_STDIO
EVP_PKEY_print_public_fp(FILE * fp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)1208 int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
1209                              int indent, ASN1_PCTX *pctx)
1210 {
1211     int ret;
1212     BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1213 
1214     if (b == NULL)
1215         return 0;
1216     ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
1217     BIO_free(b);
1218     return ret;
1219 }
1220 
EVP_PKEY_print_private_fp(FILE * fp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)1221 int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
1222                               int indent, ASN1_PCTX *pctx)
1223 {
1224     int ret;
1225     BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1226 
1227     if (b == NULL)
1228         return 0;
1229     ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
1230     BIO_free(b);
1231     return ret;
1232 }
1233 
EVP_PKEY_print_params_fp(FILE * fp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)1234 int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
1235                              int indent, ASN1_PCTX *pctx)
1236 {
1237     int ret;
1238     BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1239 
1240     if (b == NULL)
1241         return 0;
1242     ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
1243     BIO_free(b);
1244     return ret;
1245 }
1246 # endif
1247 
mdname2nid(const char * mdname,void * data)1248 static void mdname2nid(const char *mdname, void *data)
1249 {
1250     int *nid = (int *)data;
1251 
1252     if (*nid != NID_undef)
1253         return;
1254 
1255     *nid = OBJ_sn2nid(mdname);
1256     if (*nid == NID_undef)
1257         *nid = OBJ_ln2nid(mdname);
1258 }
1259 
legacy_asn1_ctrl_to_param(EVP_PKEY * pkey,int op,int arg1,void * arg2)1260 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1261                                      int arg1, void *arg2)
1262 {
1263     if (pkey->keymgmt == NULL)
1264         return 0;
1265     switch (op) {
1266     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1267         {
1268             char mdname[80] = "";
1269             int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1270                                                       sizeof(mdname));
1271 
1272             if (rv > 0) {
1273                 int mdnum;
1274                 OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1275                 /* Make sure the MD is in the namemap if available */
1276                 EVP_MD *md;
1277                 OSSL_NAMEMAP *namemap;
1278                 int nid = NID_undef;
1279 
1280                 (void)ERR_set_mark();
1281                 md = EVP_MD_fetch(libctx, mdname, NULL);
1282                 (void)ERR_pop_to_mark();
1283                 namemap = ossl_namemap_stored(libctx);
1284 
1285                 /*
1286                  * The only reason to fetch the MD was to make sure it is in the
1287                  * namemap. We can immediately free it.
1288                  */
1289                 EVP_MD_free(md);
1290                 mdnum = ossl_namemap_name2num(namemap, mdname);
1291                 if (mdnum == 0)
1292                     return 0;
1293 
1294                 /*
1295                  * We have the namemap number - now we need to find the
1296                  * associated nid
1297                  */
1298                 if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1299                     return 0;
1300                 *(int *)arg2 = nid;
1301             }
1302             return rv;
1303         }
1304     default:
1305         return -2;
1306     }
1307 }
1308 
evp_pkey_asn1_ctrl(EVP_PKEY * pkey,int op,int arg1,void * arg2)1309 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
1310 {
1311     if (pkey->ameth == NULL)
1312         return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1313     if (pkey->ameth->pkey_ctrl == NULL)
1314         return -2;
1315     return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1316 }
1317 
EVP_PKEY_get_default_digest_nid(EVP_PKEY * pkey,int * pnid)1318 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1319 {
1320     return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1321 }
1322 
EVP_PKEY_get_default_digest_name(EVP_PKEY * pkey,char * mdname,size_t mdname_sz)1323 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1324                                      char *mdname, size_t mdname_sz)
1325 {
1326     if (pkey->ameth == NULL)
1327         return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1328                                                       pkey->keydata,
1329                                                       mdname, mdname_sz);
1330 
1331     {
1332         int nid = NID_undef;
1333         int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1334         const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1335 
1336         if (rv > 0)
1337             OPENSSL_strlcpy(mdname, name, mdname_sz);
1338         return rv;
1339     }
1340 }
1341 
EVP_PKEY_get_group_name(const EVP_PKEY * pkey,char * gname,size_t gname_sz,size_t * gname_len)1342 int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1343                             size_t *gname_len)
1344 {
1345     return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1346                                           gname, gname_sz, gname_len);
1347 }
1348 
EVP_PKEY_digestsign_supports_digest(EVP_PKEY * pkey,OSSL_LIB_CTX * libctx,const char * name,const char * propq)1349 int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
1350                                         const char *name, const char *propq)
1351 {
1352     int rv;
1353     EVP_MD_CTX *ctx = NULL;
1354 
1355     if ((ctx = EVP_MD_CTX_new()) == NULL)
1356         return -1;
1357 
1358     ERR_set_mark();
1359     rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
1360                                propq, pkey, NULL);
1361     ERR_pop_to_mark();
1362 
1363     EVP_MD_CTX_free(ctx);
1364     return rv;
1365 }
1366 
EVP_PKEY_set1_encoded_public_key(EVP_PKEY * pkey,const unsigned char * pub,size_t publen)1367 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1368                                      size_t publen)
1369 {
1370     if (pkey != NULL && evp_pkey_is_provided(pkey))
1371         return
1372             EVP_PKEY_set_octet_string_param(pkey,
1373                                             OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1374                                             (unsigned char *)pub, publen);
1375 
1376     if (publen > INT_MAX)
1377         return 0;
1378     /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1379     if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,
1380                            (void *)pub) <= 0)
1381         return 0;
1382     return 1;
1383 }
1384 
EVP_PKEY_get1_encoded_public_key(EVP_PKEY * pkey,unsigned char ** ppub)1385 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
1386 {
1387     int rv;
1388 
1389     if (pkey != NULL && evp_pkey_is_provided(pkey)) {
1390         size_t return_size = OSSL_PARAM_UNMODIFIED;
1391 
1392         /*
1393          * We know that this is going to fail, but it will give us a size
1394          * to allocate.
1395          */
1396         EVP_PKEY_get_octet_string_param(pkey,
1397                                         OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1398                                         NULL, 0, &return_size);
1399         if (return_size == OSSL_PARAM_UNMODIFIED)
1400             return 0;
1401 
1402         *ppub = OPENSSL_malloc(return_size);
1403         if (*ppub == NULL)
1404             return 0;
1405 
1406         if (!EVP_PKEY_get_octet_string_param(pkey,
1407                                              OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1408                                              *ppub, return_size, NULL))
1409             return 0;
1410         return return_size;
1411     }
1412 
1413 
1414     rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1415     if (rv <= 0)
1416         return 0;
1417     return rv;
1418 }
1419 
1420 #endif /* FIPS_MODULE */
1421 
1422 /*- All methods below can also be used in FIPS_MODULE */
1423 
EVP_PKEY_new(void)1424 EVP_PKEY *EVP_PKEY_new(void)
1425 {
1426     EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1427 
1428     if (ret == NULL) {
1429         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1430         return NULL;
1431     }
1432 
1433     ret->type = EVP_PKEY_NONE;
1434     ret->save_type = EVP_PKEY_NONE;
1435     ret->references = 1;
1436 
1437     ret->lock = CRYPTO_THREAD_lock_new();
1438     if (ret->lock == NULL) {
1439         EVPerr(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1440         goto err;
1441     }
1442 
1443 #ifndef FIPS_MODULE
1444     ret->save_parameters = 1;
1445     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1446         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1447         goto err;
1448     }
1449 #endif
1450     return ret;
1451 
1452  err:
1453     CRYPTO_THREAD_lock_free(ret->lock);
1454     OPENSSL_free(ret);
1455     return NULL;
1456 }
1457 
1458 /*
1459  * Setup a public key management method.
1460  *
1461  * For legacy keys, either |type| or |str| is expected to have the type
1462  * information.  In this case, the setup consists of finding an ASN1 method
1463  * and potentially an ENGINE, and setting those fields in |pkey|.
1464  *
1465  * For provider side keys, |keymgmt| is expected to be non-NULL.  In this
1466  * case, the setup consists of setting the |keymgmt| field in |pkey|.
1467  *
1468  * If pkey is NULL just return 1 or 0 if the key management method exists.
1469  */
1470 
pkey_set_type(EVP_PKEY * pkey,ENGINE * e,int type,const char * str,int len,EVP_KEYMGMT * keymgmt)1471 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
1472                          int len, EVP_KEYMGMT *keymgmt)
1473 {
1474 #ifndef FIPS_MODULE
1475     const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1476     ENGINE **eptr = (e == NULL) ? &e :  NULL;
1477 #endif
1478 
1479     /*
1480      * The setups can't set both legacy and provider side methods.
1481      * It is forbidden
1482      */
1483     if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1484         || !ossl_assert(e == NULL || keymgmt == NULL)) {
1485         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1486         return 0;
1487     }
1488 
1489     if (pkey != NULL) {
1490         int free_it = 0;
1491 
1492 #ifndef FIPS_MODULE
1493         free_it = free_it || pkey->pkey.ptr != NULL;
1494 #endif
1495         free_it = free_it || pkey->keydata != NULL;
1496         if (free_it)
1497             evp_pkey_free_it(pkey);
1498 #ifndef FIPS_MODULE
1499         /*
1500          * If key type matches and a method exists then this lookup has
1501          * succeeded once so just indicate success.
1502          */
1503         if (pkey->type != EVP_PKEY_NONE
1504             && type == pkey->save_type
1505             && pkey->ameth != NULL)
1506             return 1;
1507 # ifndef OPENSSL_NO_ENGINE
1508         /* If we have ENGINEs release them */
1509         ENGINE_finish(pkey->engine);
1510         pkey->engine = NULL;
1511         ENGINE_finish(pkey->pmeth_engine);
1512         pkey->pmeth_engine = NULL;
1513 # endif
1514 #endif
1515     }
1516 #ifndef FIPS_MODULE
1517     if (str != NULL)
1518         ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1519     else if (type != EVP_PKEY_NONE)
1520         ameth = EVP_PKEY_asn1_find(eptr, type);
1521 # ifndef OPENSSL_NO_ENGINE
1522     if (pkey == NULL && eptr != NULL)
1523         ENGINE_finish(e);
1524 # endif
1525 #endif
1526 
1527 
1528     {
1529         int check = 1;
1530 
1531 #ifndef FIPS_MODULE
1532         check = check && ameth == NULL;
1533 #endif
1534         check = check && keymgmt == NULL;
1535         if (check) {
1536             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1537             return 0;
1538         }
1539     }
1540     if (pkey != NULL) {
1541         if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1542             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1543             return 0;
1544         }
1545 
1546         pkey->keymgmt = keymgmt;
1547 
1548         pkey->save_type = type;
1549         pkey->type = type;
1550 
1551 #ifndef FIPS_MODULE
1552         /*
1553          * If the internal "origin" key is provider side, don't save |ameth|.
1554          * The main reason is that |ameth| is one factor to detect that the
1555          * internal "origin" key is a legacy one.
1556          */
1557         if (keymgmt == NULL)
1558             pkey->ameth = ameth;
1559 
1560         /*
1561          * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1562          * for any key type that has a legacy implementation, regardless of
1563          * if the internal key is a legacy or a provider side one.  When
1564          * there is no legacy implementation for the key, the type becomes
1565          * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1566          * with functions that expect legacy internal keys.
1567          */
1568         if (ameth != NULL) {
1569             if (type == EVP_PKEY_NONE)
1570                 pkey->type = ameth->pkey_id;
1571         } else {
1572             pkey->type = EVP_PKEY_KEYMGMT;
1573         }
1574 # ifndef OPENSSL_NO_ENGINE
1575         if (eptr == NULL && e != NULL && !ENGINE_init(e)) {
1576             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1577             return 0;
1578         }
1579 # endif
1580         pkey->engine = e;
1581 #endif
1582     }
1583     return 1;
1584 }
1585 
1586 #ifndef FIPS_MODULE
find_ameth(const char * name,void * data)1587 static void find_ameth(const char *name, void *data)
1588 {
1589     const char **str = data;
1590 
1591     /*
1592      * The error messages from pkey_set_type() are uninteresting here,
1593      * and misleading.
1594      */
1595     ERR_set_mark();
1596 
1597     if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
1598                       NULL)) {
1599         if (str[0] == NULL)
1600             str[0] = name;
1601         else if (str[1] == NULL)
1602             str[1] = name;
1603     }
1604 
1605     ERR_pop_to_mark();
1606 }
1607 #endif
1608 
EVP_PKEY_set_type_by_keymgmt(EVP_PKEY * pkey,EVP_KEYMGMT * keymgmt)1609 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1610 {
1611 #ifndef FIPS_MODULE
1612 # define EVP_PKEY_TYPE_STR str[0]
1613 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1614     /*
1615      * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1616      * Ideally, only one should be found.  If two (or more) are found, the
1617      * match is ambiguous.  This should never happen, but...
1618      */
1619     const char *str[2] = { NULL, NULL };
1620 
1621     if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1622             || str[1] != NULL) {
1623         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1624         return 0;
1625     }
1626 #else
1627 # define EVP_PKEY_TYPE_STR NULL
1628 # define EVP_PKEY_TYPE_STRLEN -1
1629 #endif
1630     return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1631                          EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1632                          keymgmt);
1633 
1634 #undef EVP_PKEY_TYPE_STR
1635 #undef EVP_PKEY_TYPE_STRLEN
1636 }
1637 
EVP_PKEY_up_ref(EVP_PKEY * pkey)1638 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1639 {
1640     int i;
1641 
1642     if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
1643         return 0;
1644 
1645     REF_PRINT_COUNT("EVP_PKEY", pkey);
1646     REF_ASSERT_ISNT(i < 2);
1647     return ((i > 1) ? 1 : 0);
1648 }
1649 
1650 #ifndef FIPS_MODULE
EVP_PKEY_dup(EVP_PKEY * pkey)1651 EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1652 {
1653     EVP_PKEY *dup_pk;
1654 
1655     if (pkey == NULL) {
1656         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1657         return NULL;
1658     }
1659 
1660     if ((dup_pk = EVP_PKEY_new()) == NULL)
1661         return NULL;
1662 
1663     if (evp_pkey_is_blank(pkey))
1664         goto done;
1665 
1666     if (evp_pkey_is_provided(pkey)) {
1667         if (!evp_keymgmt_util_copy(dup_pk, pkey,
1668                                    OSSL_KEYMGMT_SELECT_ALL))
1669             goto err;
1670         goto done;
1671     }
1672 
1673     if (evp_pkey_is_legacy(pkey)) {
1674         const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
1675 
1676         if (ameth == NULL || ameth->copy == NULL) {
1677             if (pkey->pkey.ptr == NULL /* empty key, just set type */
1678                 && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
1679                 goto done;
1680             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1681             goto err;
1682         }
1683         if (!ameth->copy(dup_pk, pkey))
1684             goto err;
1685         goto done;
1686     }
1687 
1688     goto err;
1689 done:
1690     /* copy auxiliary data */
1691     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1692                             &dup_pk->ex_data, &pkey->ex_data))
1693         goto err;
1694 
1695     if (pkey->attributes != NULL) {
1696         if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1697             goto err;
1698     }
1699     return dup_pk;
1700 err:
1701     EVP_PKEY_free(dup_pk);
1702     return NULL;
1703 }
1704 
evp_pkey_free_legacy(EVP_PKEY * x)1705 void evp_pkey_free_legacy(EVP_PKEY *x)
1706 {
1707     const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1708     ENGINE *tmpe = NULL;
1709 
1710     if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1711         ameth = EVP_PKEY_asn1_find(&tmpe, x->type);
1712 
1713     if (ameth != NULL) {
1714         if (x->legacy_cache_pkey.ptr != NULL) {
1715             /*
1716              * We should never have both a legacy origin key, and a key in the
1717              * legacy cache.
1718              */
1719             assert(x->pkey.ptr == NULL);
1720             /*
1721              * For the purposes of freeing we make the legacy cache look like
1722              * a legacy origin key.
1723              */
1724             x->pkey = x->legacy_cache_pkey;
1725             x->legacy_cache_pkey.ptr = NULL;
1726         }
1727         if (ameth->pkey_free != NULL)
1728             ameth->pkey_free(x);
1729         x->pkey.ptr = NULL;
1730     }
1731 # ifndef OPENSSL_NO_ENGINE
1732     ENGINE_finish(tmpe);
1733     ENGINE_finish(x->engine);
1734     x->engine = NULL;
1735     ENGINE_finish(x->pmeth_engine);
1736     x->pmeth_engine = NULL;
1737 # endif
1738 }
1739 #endif  /* FIPS_MODULE */
1740 
evp_pkey_free_it(EVP_PKEY * x)1741 static void evp_pkey_free_it(EVP_PKEY *x)
1742 {
1743     /* internal function; x is never NULL */
1744     evp_keymgmt_util_clear_operation_cache(x, 1);
1745 #ifndef FIPS_MODULE
1746     evp_pkey_free_legacy(x);
1747 #endif
1748 
1749     if (x->keymgmt != NULL) {
1750         evp_keymgmt_freedata(x->keymgmt, x->keydata);
1751         EVP_KEYMGMT_free(x->keymgmt);
1752         x->keymgmt = NULL;
1753         x->keydata = NULL;
1754     }
1755     x->type = EVP_PKEY_NONE;
1756 }
1757 
EVP_PKEY_free(EVP_PKEY * x)1758 void EVP_PKEY_free(EVP_PKEY *x)
1759 {
1760     int i;
1761 
1762     if (x == NULL)
1763         return;
1764 
1765     CRYPTO_DOWN_REF(&x->references, &i, x->lock);
1766     REF_PRINT_COUNT("EVP_PKEY", x);
1767     if (i > 0)
1768         return;
1769     REF_ASSERT_ISNT(i < 0);
1770     evp_pkey_free_it(x);
1771 #ifndef FIPS_MODULE
1772     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1773 #endif
1774     CRYPTO_THREAD_lock_free(x->lock);
1775 #ifndef FIPS_MODULE
1776     sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1777 #endif
1778     OPENSSL_free(x);
1779 }
1780 
EVP_PKEY_get_size(const EVP_PKEY * pkey)1781 int EVP_PKEY_get_size(const EVP_PKEY *pkey)
1782 {
1783     int size = 0;
1784 
1785     if (pkey != NULL) {
1786         size = pkey->cache.size;
1787 #ifndef FIPS_MODULE
1788         if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1789             size = pkey->ameth->pkey_size(pkey);
1790 #endif
1791     }
1792     return size < 0 ? 0 : size;
1793 }
1794 
EVP_PKEY_get0_description(const EVP_PKEY * pkey)1795 const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)
1796 {
1797     if (!evp_pkey_is_assigned(pkey))
1798         return NULL;
1799 
1800     if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
1801         return pkey->keymgmt->description;
1802 #ifndef FIPS_MODULE
1803     if (pkey->ameth != NULL)
1804         return pkey->ameth->info;
1805 #endif
1806     return NULL;
1807 }
1808 
evp_pkey_export_to_provider(EVP_PKEY * pk,OSSL_LIB_CTX * libctx,EVP_KEYMGMT ** keymgmt,const char * propquery)1809 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
1810                                   EVP_KEYMGMT **keymgmt,
1811                                   const char *propquery)
1812 {
1813     EVP_KEYMGMT *allocated_keymgmt = NULL;
1814     EVP_KEYMGMT *tmp_keymgmt = NULL;
1815     void *keydata = NULL;
1816     int check;
1817 
1818     if (pk == NULL)
1819         return NULL;
1820 
1821     /* No key data => nothing to export */
1822     check = 1;
1823 #ifndef FIPS_MODULE
1824     check = check && pk->pkey.ptr == NULL;
1825 #endif
1826     check = check && pk->keydata == NULL;
1827     if (check)
1828         return NULL;
1829 
1830 #ifndef FIPS_MODULE
1831     if (pk->pkey.ptr != NULL) {
1832         /*
1833          * If the legacy key doesn't have an dirty counter or export function,
1834          * give up
1835          */
1836         if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1837             return NULL;
1838     }
1839 #endif
1840 
1841     if (keymgmt != NULL) {
1842         tmp_keymgmt = *keymgmt;
1843         *keymgmt = NULL;
1844     }
1845 
1846     /*
1847      * If no keymgmt was given or found, get a default keymgmt.  We do so by
1848      * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1849      */
1850     if (tmp_keymgmt == NULL) {
1851         EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1852 
1853         if (ctx == NULL)
1854             goto end;
1855         tmp_keymgmt = ctx->keymgmt;
1856         ctx->keymgmt = NULL;
1857         EVP_PKEY_CTX_free(ctx);
1858     }
1859 
1860     /* If there's still no keymgmt to be had, give up */
1861     if (tmp_keymgmt == NULL)
1862         goto end;
1863 
1864 #ifndef FIPS_MODULE
1865     if (pk->pkey.ptr != NULL) {
1866         OP_CACHE_ELEM *op;
1867 
1868         /*
1869          * If the legacy "origin" hasn't changed since last time, we try
1870          * to find our keymgmt in the operation cache.  If it has changed,
1871          * |i| remains zero, and we will clear the cache further down.
1872          */
1873         if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1874             if (!CRYPTO_THREAD_read_lock(pk->lock))
1875                 goto end;
1876             op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1877 
1878             /*
1879              * If |tmp_keymgmt| is present in the operation cache, it means
1880              * that export doesn't need to be redone.  In that case, we take
1881              * token copies of the cached pointers, to have token success
1882              * values to return.
1883              */
1884             if (op != NULL && op->keymgmt != NULL) {
1885                 keydata = op->keydata;
1886                 CRYPTO_THREAD_unlock(pk->lock);
1887                 goto end;
1888             }
1889             CRYPTO_THREAD_unlock(pk->lock);
1890         }
1891 
1892         /* Make sure that the keymgmt key type matches the legacy NID */
1893         if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
1894             goto end;
1895 
1896         if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1897             goto end;
1898 
1899         if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import,
1900                                   libctx, propquery)) {
1901             evp_keymgmt_freedata(tmp_keymgmt, keydata);
1902             keydata = NULL;
1903             goto end;
1904         }
1905 
1906         /*
1907          * If the dirty counter changed since last time, then clear the
1908          * operation cache.  In that case, we know that |i| is zero.  Just
1909          * in case this is a re-export, we increment then decrement the
1910          * keymgmt reference counter.
1911          */
1912         if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1913             evp_keymgmt_freedata(tmp_keymgmt, keydata);
1914             keydata = NULL;
1915             goto end;
1916         }
1917 
1918         if (!CRYPTO_THREAD_write_lock(pk->lock))
1919             goto end;
1920         if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy
1921                 && !evp_keymgmt_util_clear_operation_cache(pk, 0)) {
1922             CRYPTO_THREAD_unlock(pk->lock);
1923             evp_keymgmt_freedata(tmp_keymgmt, keydata);
1924             keydata = NULL;
1925             EVP_KEYMGMT_free(tmp_keymgmt);
1926             goto end;
1927         }
1928         EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1929 
1930         /* Check to make sure some other thread didn't get there first */
1931         op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt);
1932         if (op != NULL && op->keymgmt != NULL) {
1933             void *tmp_keydata = op->keydata;
1934 
1935             CRYPTO_THREAD_unlock(pk->lock);
1936             evp_keymgmt_freedata(tmp_keymgmt, keydata);
1937             keydata = tmp_keydata;
1938             goto end;
1939         }
1940 
1941         /* Add the new export to the operation cache */
1942         if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata)) {
1943             CRYPTO_THREAD_unlock(pk->lock);
1944             evp_keymgmt_freedata(tmp_keymgmt, keydata);
1945             keydata = NULL;
1946             goto end;
1947         }
1948 
1949         /* Synchronize the dirty count */
1950         pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1951 
1952         CRYPTO_THREAD_unlock(pk->lock);
1953         goto end;
1954     }
1955 #endif  /* FIPS_MODULE */
1956 
1957     keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1958 
1959  end:
1960     /*
1961      * If nothing was exported, |tmp_keymgmt| might point at a freed
1962      * EVP_KEYMGMT, so we clear it to be safe.  It shouldn't be useful for
1963      * the caller either way in that case.
1964      */
1965     if (keydata == NULL)
1966         tmp_keymgmt = NULL;
1967 
1968     if (keymgmt != NULL)
1969         *keymgmt = tmp_keymgmt;
1970 
1971     EVP_KEYMGMT_free(allocated_keymgmt);
1972     return keydata;
1973 }
1974 
1975 #ifndef FIPS_MODULE
evp_pkey_copy_downgraded(EVP_PKEY ** dest,const EVP_PKEY * src)1976 int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
1977 {
1978     if (!ossl_assert(dest != NULL))
1979         return 0;
1980 
1981     if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
1982         EVP_KEYMGMT *keymgmt = src->keymgmt;
1983         void *keydata = src->keydata;
1984         int type = src->type;
1985         const char *keytype = NULL;
1986 
1987         keytype = EVP_KEYMGMT_get0_name(keymgmt);
1988 
1989         /*
1990          * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1991          * else in our code.  If it's not one of the well known EVP_PKEY_xxx
1992          * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1993          * The check is kept as a safety measure.
1994          */
1995         if (!ossl_assert(type != EVP_PKEY_NONE)) {
1996             ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
1997                            "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1998                            keytype);
1999             return 0;
2000         }
2001 
2002         /* Prefer the legacy key type name for error reporting */
2003         if (type != EVP_PKEY_KEYMGMT)
2004             keytype = OBJ_nid2sn(type);
2005 
2006         /* Make sure we have a clean slate to copy into */
2007         if (*dest == NULL) {
2008             *dest = EVP_PKEY_new();
2009             if (*dest == NULL) {
2010                 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
2011                 return 0;
2012             }
2013         } else {
2014             evp_pkey_free_it(*dest);
2015         }
2016 
2017         if (EVP_PKEY_set_type(*dest, type)) {
2018             /* If the key is typed but empty, we're done */
2019             if (keydata == NULL)
2020                 return 1;
2021 
2022             if ((*dest)->ameth->import_from == NULL) {
2023                 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2024                                "key type = %s", keytype);
2025             } else {
2026                 /*
2027                  * We perform the export in the same libctx as the keymgmt
2028                  * that we are using.
2029                  */
2030                 OSSL_LIB_CTX *libctx =
2031                     ossl_provider_libctx(keymgmt->prov);
2032                 EVP_PKEY_CTX *pctx =
2033                     EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
2034 
2035                 if (pctx == NULL)
2036                     ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
2037 
2038                 if (pctx != NULL
2039                     && evp_keymgmt_export(keymgmt, keydata,
2040                                           OSSL_KEYMGMT_SELECT_ALL,
2041                                           (*dest)->ameth->import_from,
2042                                           pctx)) {
2043                     /* Synchronize the dirty count */
2044                     (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2045 
2046                     EVP_PKEY_CTX_free(pctx);
2047                     return 1;
2048                 }
2049                 EVP_PKEY_CTX_free(pctx);
2050             }
2051 
2052             ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2053                            "key type = %s", keytype);
2054         }
2055     }
2056 
2057     return 0;
2058 }
2059 
evp_pkey_get_legacy(EVP_PKEY * pk)2060 void *evp_pkey_get_legacy(EVP_PKEY *pk)
2061 {
2062     EVP_PKEY *tmp_copy = NULL;
2063     void *ret = NULL;
2064 
2065     if (!ossl_assert(pk != NULL))
2066         return NULL;
2067 
2068     /*
2069      * If this isn't an assigned provider side key, we just use any existing
2070      * origin legacy key.
2071      */
2072     if (!evp_pkey_is_assigned(pk))
2073         return NULL;
2074     if (!evp_pkey_is_provided(pk))
2075         return pk->pkey.ptr;
2076 
2077     if (!CRYPTO_THREAD_read_lock(pk->lock))
2078         return NULL;
2079 
2080     ret = pk->legacy_cache_pkey.ptr;
2081 
2082     if (!CRYPTO_THREAD_unlock(pk->lock))
2083         return NULL;
2084 
2085     if (ret != NULL)
2086         return ret;
2087 
2088     if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
2089         return NULL;
2090 
2091     if (!CRYPTO_THREAD_write_lock(pk->lock))
2092         goto err;
2093 
2094     /* Check again in case some other thread has updated it in the meantime */
2095     ret = pk->legacy_cache_pkey.ptr;
2096     if (ret == NULL) {
2097         /* Steal the legacy key reference from the temporary copy */
2098         ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2099         tmp_copy->pkey.ptr = NULL;
2100     }
2101 
2102     if (!CRYPTO_THREAD_unlock(pk->lock)) {
2103         ret = NULL;
2104         goto err;
2105     }
2106 
2107  err:
2108     EVP_PKEY_free(tmp_copy);
2109 
2110     return ret;
2111 }
2112 #endif  /* FIPS_MODULE */
2113 
EVP_PKEY_get_bn_param(const EVP_PKEY * pkey,const char * key_name,BIGNUM ** bn)2114 int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2115                           BIGNUM **bn)
2116 {
2117     int ret = 0;
2118     OSSL_PARAM params[2];
2119     unsigned char buffer[2048];
2120     unsigned char *buf = NULL;
2121     size_t buf_sz = 0;
2122 
2123     if (key_name == NULL
2124         || bn == NULL)
2125         return 0;
2126 
2127     memset(buffer, 0, sizeof(buffer));
2128     params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
2129     params[1] = OSSL_PARAM_construct_end();
2130     if (!EVP_PKEY_get_params(pkey, params)) {
2131         if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
2132             return 0;
2133         buf_sz = params[0].return_size;
2134         /*
2135          * If it failed because the buffer was too small then allocate the
2136          * required buffer size and retry.
2137          */
2138         buf = OPENSSL_zalloc(buf_sz);
2139         if (buf == NULL)
2140             return 0;
2141         params[0].data = buf;
2142         params[0].data_size = buf_sz;
2143 
2144         if (!EVP_PKEY_get_params(pkey, params))
2145             goto err;
2146     }
2147     /* Fail if the param was not found */
2148     if (!OSSL_PARAM_modified(params))
2149         goto err;
2150     ret = OSSL_PARAM_get_BN(params, bn);
2151 err:
2152     OPENSSL_free(buf);
2153     return ret;
2154 }
2155 
EVP_PKEY_get_octet_string_param(const EVP_PKEY * pkey,const char * key_name,unsigned char * buf,size_t max_buf_sz,size_t * out_len)2156 int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
2157                                     unsigned char *buf, size_t max_buf_sz,
2158                                     size_t *out_len)
2159 {
2160     OSSL_PARAM params[2];
2161     int ret1 = 0, ret2 = 0;
2162 
2163     if (key_name == NULL)
2164         return 0;
2165 
2166     params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2167     params[1] = OSSL_PARAM_construct_end();
2168     if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2169         ret2 = OSSL_PARAM_modified(params);
2170     if (ret2 && out_len != NULL)
2171         *out_len = params[0].return_size;
2172     return ret1 && ret2;
2173 }
2174 
EVP_PKEY_get_utf8_string_param(const EVP_PKEY * pkey,const char * key_name,char * str,size_t max_buf_sz,size_t * out_len)2175 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2176                                     char *str, size_t max_buf_sz,
2177                                     size_t *out_len)
2178 {
2179     OSSL_PARAM params[2];
2180     int ret1 = 0, ret2 = 0;
2181 
2182     if (key_name == NULL)
2183         return 0;
2184 
2185     params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2186     params[1] = OSSL_PARAM_construct_end();
2187     if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2188         ret2 = OSSL_PARAM_modified(params);
2189     if (ret2 && out_len != NULL)
2190         *out_len = params[0].return_size;
2191 
2192     if (ret2 && params[0].return_size == max_buf_sz)
2193         /* There was no space for a NUL byte */
2194         return 0;
2195     /* Add a terminating NUL byte for good measure */
2196     if (ret2 && str != NULL)
2197         str[params[0].return_size] = '\0';
2198 
2199     return ret1 && ret2;
2200 }
2201 
EVP_PKEY_get_int_param(const EVP_PKEY * pkey,const char * key_name,int * out)2202 int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2203                            int *out)
2204 {
2205     OSSL_PARAM params[2];
2206 
2207     if (key_name == NULL)
2208         return 0;
2209 
2210     params[0] = OSSL_PARAM_construct_int(key_name, out);
2211     params[1] = OSSL_PARAM_construct_end();
2212     return EVP_PKEY_get_params(pkey, params)
2213         && OSSL_PARAM_modified(params);
2214 }
2215 
EVP_PKEY_get_size_t_param(const EVP_PKEY * pkey,const char * key_name,size_t * out)2216 int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2217                               size_t *out)
2218 {
2219     OSSL_PARAM params[2];
2220 
2221     if (key_name == NULL)
2222         return 0;
2223 
2224     params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2225     params[1] = OSSL_PARAM_construct_end();
2226     return EVP_PKEY_get_params(pkey, params)
2227         && OSSL_PARAM_modified(params);
2228 }
2229 
EVP_PKEY_set_int_param(EVP_PKEY * pkey,const char * key_name,int in)2230 int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2231 {
2232     OSSL_PARAM params[2];
2233 
2234     if (key_name == NULL)
2235         return 0;
2236 
2237     params[0] = OSSL_PARAM_construct_int(key_name, &in);
2238     params[1] = OSSL_PARAM_construct_end();
2239     return EVP_PKEY_set_params(pkey, params);
2240 }
2241 
EVP_PKEY_set_size_t_param(EVP_PKEY * pkey,const char * key_name,size_t in)2242 int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2243 {
2244     OSSL_PARAM params[2];
2245 
2246     if (key_name == NULL)
2247         return 0;
2248 
2249     params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2250     params[1] = OSSL_PARAM_construct_end();
2251     return EVP_PKEY_set_params(pkey, params);
2252 }
2253 
EVP_PKEY_set_bn_param(EVP_PKEY * pkey,const char * key_name,const BIGNUM * bn)2254 int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2255                           const BIGNUM *bn)
2256 {
2257     OSSL_PARAM params[2];
2258     unsigned char buffer[2048];
2259     int bsize = 0;
2260 
2261     if (key_name == NULL
2262         || bn == NULL
2263         || pkey == NULL
2264         || !evp_pkey_is_assigned(pkey))
2265         return 0;
2266 
2267     bsize = BN_num_bytes(bn);
2268     if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2269         return 0;
2270 
2271     if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2272         return 0;
2273     params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2274     params[1] = OSSL_PARAM_construct_end();
2275     return EVP_PKEY_set_params(pkey, params);
2276 }
2277 
EVP_PKEY_set_utf8_string_param(EVP_PKEY * pkey,const char * key_name,const char * str)2278 int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2279                                    const char *str)
2280 {
2281     OSSL_PARAM params[2];
2282 
2283     if (key_name == NULL)
2284         return 0;
2285 
2286     params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2287     params[1] = OSSL_PARAM_construct_end();
2288     return EVP_PKEY_set_params(pkey, params);
2289 }
2290 
EVP_PKEY_set_octet_string_param(EVP_PKEY * pkey,const char * key_name,const unsigned char * buf,size_t bsize)2291 int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2292                                     const unsigned char *buf, size_t bsize)
2293 {
2294     OSSL_PARAM params[2];
2295 
2296     if (key_name == NULL)
2297         return 0;
2298 
2299     params[0] = OSSL_PARAM_construct_octet_string(key_name,
2300                                                   (unsigned char *)buf, bsize);
2301     params[1] = OSSL_PARAM_construct_end();
2302     return EVP_PKEY_set_params(pkey, params);
2303 }
2304 
EVP_PKEY_settable_params(const EVP_PKEY * pkey)2305 const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2306 {
2307     return (pkey != NULL && evp_pkey_is_provided(pkey))
2308         ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2309         : NULL;
2310 }
2311 
EVP_PKEY_set_params(EVP_PKEY * pkey,OSSL_PARAM params[])2312 int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2313 {
2314     if (pkey != NULL) {
2315         if (evp_pkey_is_provided(pkey)) {
2316             pkey->dirty_cnt++;
2317             return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2318         }
2319 #ifndef FIPS_MODULE
2320         /*
2321          * We will hopefully never find the need to set individual data in
2322          * EVP_PKEYs with a legacy internal key, but we can't be entirely
2323          * sure.  This bit of code can be enabled if we find the need.  If
2324          * not, it can safely be removed when #legacy support is removed.
2325          */
2326 # if 0
2327         else if (evp_pkey_is_legacy(pkey)) {
2328             return evp_pkey_set_params_to_ctrl(pkey, params);
2329         }
2330 # endif
2331 #endif
2332     }
2333     ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2334     return 0;
2335 }
2336 
EVP_PKEY_gettable_params(const EVP_PKEY * pkey)2337 const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2338 {
2339     return (pkey != NULL && evp_pkey_is_provided(pkey))
2340         ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2341         : NULL;
2342 }
2343 
EVP_PKEY_get_params(const EVP_PKEY * pkey,OSSL_PARAM params[])2344 int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2345 {
2346     if (pkey != NULL) {
2347         if (evp_pkey_is_provided(pkey))
2348             return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params);
2349 #ifndef FIPS_MODULE
2350         else if (evp_pkey_is_legacy(pkey))
2351             return evp_pkey_get_params_to_ctrl(pkey, params);
2352 #endif
2353     }
2354     ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2355     return 0;
2356 }
2357 
2358 #ifndef FIPS_MODULE
EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY * pkey)2359 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2360 {
2361     char name[80];
2362     size_t name_len;
2363 
2364     if (pkey == NULL)
2365         return 0;
2366 
2367     if (pkey->keymgmt == NULL
2368             || pkey->keydata == NULL) {
2369 # ifndef OPENSSL_NO_EC
2370         /* Might work through the legacy route */
2371         const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2372 
2373         if (ec == NULL)
2374             return 0;
2375 
2376         return EC_KEY_get_conv_form(ec);
2377 # else
2378         return 0;
2379 # endif
2380     }
2381 
2382     if (!EVP_PKEY_get_utf8_string_param(pkey,
2383                                         OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2384                                         name, sizeof(name), &name_len))
2385         return 0;
2386 
2387     if (strcmp(name, "uncompressed") == 0)
2388         return POINT_CONVERSION_UNCOMPRESSED;
2389 
2390     if (strcmp(name, "compressed") == 0)
2391         return POINT_CONVERSION_COMPRESSED;
2392 
2393     if (strcmp(name, "hybrid") == 0)
2394         return POINT_CONVERSION_HYBRID;
2395 
2396     return 0;
2397 }
2398 
EVP_PKEY_get_field_type(const EVP_PKEY * pkey)2399 int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2400 {
2401     char fstr[80];
2402     size_t fstrlen;
2403 
2404     if (pkey == NULL)
2405         return 0;
2406 
2407     if (pkey->keymgmt == NULL
2408             || pkey->keydata == NULL) {
2409 # ifndef OPENSSL_NO_EC
2410         /* Might work through the legacy route */
2411         const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2412         const EC_GROUP *grp;
2413 
2414         if (ec == NULL)
2415             return 0;
2416         grp = EC_KEY_get0_group(ec);
2417         if (grp == NULL)
2418             return 0;
2419 
2420         return EC_GROUP_get_field_type(grp);
2421 # else
2422         return 0;
2423 # endif
2424     }
2425 
2426     if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2427                                         fstr, sizeof(fstr), &fstrlen))
2428         return 0;
2429 
2430     if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2431         return NID_X9_62_prime_field;
2432     else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2433         return NID_X9_62_characteristic_two_field;
2434 
2435     return 0;
2436 }
2437 #endif
2438