xref: /freebsd/crypto/openssl/crypto/evp/pmeth_lib.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 2006-2025 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  * Low level key APIs (DH etc) are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #ifndef FIPS_MODULE
19 # include <openssl/engine.h>
20 #endif
21 #include <openssl/evp.h>
22 #include <openssl/core_names.h>
23 #include <openssl/dh.h>
24 #include <openssl/rsa.h>
25 #include <openssl/kdf.h>
26 #include "internal/cryptlib.h"
27 #ifndef FIPS_MODULE
28 # include "crypto/asn1.h"
29 #endif
30 #include "crypto/evp.h"
31 #include "crypto/dh.h"
32 #include "crypto/ec.h"
33 #include "internal/ffc.h"
34 #include "internal/numbers.h"
35 #include "internal/provider.h"
36 #include "evp_local.h"
37 
38 #ifndef FIPS_MODULE
39 
40 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
41                                           int keytype, int optype,
42                                           int cmd, const char *name,
43                                           const void *data, size_t data_len);
44 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
45                                           int cmd, const char *name);
46 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
47 
48 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
49 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
50 
51 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
52 
53 /* This array needs to be in order of NIDs */
54 static pmeth_fn standard_methods[] = {
55     ossl_rsa_pkey_method,
56 # ifndef OPENSSL_NO_DH
57     ossl_dh_pkey_method,
58 # endif
59 # ifndef OPENSSL_NO_DSA
60     ossl_dsa_pkey_method,
61 # endif
62 # ifndef OPENSSL_NO_EC
63     ossl_ec_pkey_method,
64 # endif
65     ossl_rsa_pss_pkey_method,
66 # ifndef OPENSSL_NO_DH
67     ossl_dhx_pkey_method,
68 # endif
69 # ifndef OPENSSL_NO_ECX
70     ossl_ecx25519_pkey_method,
71     ossl_ecx448_pkey_method,
72     ossl_ed25519_pkey_method,
73     ossl_ed448_pkey_method,
74 # endif
75 };
76 
77 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
78 
pmeth_func_cmp(const EVP_PKEY_METHOD * const * a,pmeth_fn const * b)79 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
80 {
81     return ((*a)->pkey_id - ((**b)())->pkey_id);
82 }
83 
84 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
85 
pmeth_cmp(const EVP_PKEY_METHOD * const * a,const EVP_PKEY_METHOD * const * b)86 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
87                      const EVP_PKEY_METHOD *const *b)
88 {
89     return ((*a)->pkey_id - (*b)->pkey_id);
90 }
91 
evp_pkey_meth_find_added_by_application(int type)92 static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type)
93 {
94     if (app_pkey_methods != NULL) {
95         int idx;
96         EVP_PKEY_METHOD tmp;
97 
98         tmp.pkey_id = type;
99         idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
100         if (idx >= 0)
101             return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
102     }
103     return NULL;
104 }
105 
EVP_PKEY_meth_find(int type)106 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
107 {
108     pmeth_fn *ret;
109     EVP_PKEY_METHOD tmp;
110     const EVP_PKEY_METHOD *t;
111 
112     if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL)
113         return t;
114 
115     tmp.pkey_id = type;
116     t = &tmp;
117     ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
118                                  OSSL_NELEM(standard_methods));
119     if (ret == NULL || *ret == NULL)
120         return NULL;
121     return (**ret)();
122 }
123 
EVP_PKEY_meth_new(int id,int flags)124 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
125 {
126     EVP_PKEY_METHOD *pmeth;
127 
128     pmeth = OPENSSL_zalloc(sizeof(*pmeth));
129     if (pmeth == NULL)
130         return NULL;
131 
132     pmeth->pkey_id = id;
133     pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
134     return pmeth;
135 }
136 #endif /* FIPS_MODULE */
137 
evp_pkey_ctx_state(const EVP_PKEY_CTX * ctx)138 int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
139 {
140     if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
141         return EVP_PKEY_STATE_UNKNOWN;
142 
143     if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
144          && ctx->op.kex.algctx != NULL)
145         || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
146             && ctx->op.sig.algctx != NULL)
147         || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
148             && ctx->op.ciph.algctx != NULL)
149         || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
150             && ctx->op.keymgmt.genctx != NULL)
151         || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
152             && ctx->op.encap.algctx != NULL))
153         return EVP_PKEY_STATE_PROVIDER;
154 
155     return EVP_PKEY_STATE_LEGACY;
156 }
157 
int_ctx_new(OSSL_LIB_CTX * libctx,EVP_PKEY * pkey,ENGINE * e,const char * keytype,const char * propquery,int id)158 static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
159                                  EVP_PKEY *pkey, ENGINE *e,
160                                  const char *keytype, const char *propquery,
161                                  int id)
162 
163 {
164     EVP_PKEY_CTX *ret = NULL;
165     const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL;
166     EVP_KEYMGMT *keymgmt = NULL;
167 
168     /* Code below to be removed when legacy support is dropped. */
169     /* BEGIN legacy */
170     if (id == -1) {
171         if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
172             id = pkey->type;
173         } else {
174             if (pkey != NULL) {
175                 /* Must be provided if we get here */
176                 keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
177             }
178 #ifndef FIPS_MODULE
179             if (keytype != NULL) {
180                 id = evp_pkey_name2type(keytype);
181                 if (id == NID_undef)
182                     id = -1;
183             }
184 #endif
185         }
186     }
187     /* If no ID was found here, we can only resort to find a keymgmt */
188     if (id == -1) {
189 #ifndef FIPS_MODULE
190         /* Using engine with a key without id will not work */
191         if (e != NULL) {
192             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
193             return NULL;
194         }
195 #endif
196         goto common;
197     }
198 
199 #ifndef FIPS_MODULE
200     /*
201      * Here, we extract what information we can for the purpose of
202      * supporting usage with implementations from providers, to make
203      * for a smooth transition from legacy stuff to provider based stuff.
204      *
205      * If an engine is given, this is entirely legacy, and we should not
206      * pretend anything else, so we clear the name.
207      */
208     if (e != NULL)
209         keytype = NULL;
210     if (e == NULL && (pkey == NULL || pkey->foreign == 0))
211         keytype = OBJ_nid2sn(id);
212 
213 # ifndef OPENSSL_NO_ENGINE
214     if (e == NULL && pkey != NULL)
215         e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
216     /* Try to find an ENGINE which implements this method */
217     if (e != NULL) {
218         if (!ENGINE_init(e)) {
219             ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
220             return NULL;
221         }
222     } else {
223         e = ENGINE_get_pkey_meth_engine(id);
224     }
225 
226     /*
227      * If an ENGINE handled this method look it up. Otherwise use internal
228      * tables.
229      */
230     if (e != NULL)
231         pmeth = ENGINE_get_pkey_meth(e, id);
232     else
233 # endif /* OPENSSL_NO_ENGINE */
234     if (pkey != NULL && pkey->foreign)
235         pmeth = EVP_PKEY_meth_find(id);
236     else
237         app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id);
238 
239     /* END legacy */
240 #endif /* FIPS_MODULE */
241  common:
242     /*
243      * If there's no engine and no app supplied pmeth and there's a name, we try
244      * fetching a provider implementation.
245      */
246     if (e == NULL && app_pmeth == NULL && keytype != NULL) {
247         /*
248          * If |pkey| is given and is provided, we take a reference to its
249          * keymgmt.  Otherwise, we fetch one for the keytype we got. This
250          * is to ensure that operation init functions can access what they
251          * need through this single pointer.
252          */
253         if (pkey != NULL && pkey->keymgmt != NULL) {
254             if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
255                 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
256             else
257                 keymgmt = pkey->keymgmt;
258         } else {
259             keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
260         }
261         if (keymgmt == NULL)
262             return NULL;   /* EVP_KEYMGMT_fetch() recorded an error */
263 
264 #ifndef FIPS_MODULE
265         /*
266          * Chase down the legacy NID, as that might be needed for diverse
267          * purposes, such as ensure that EVP_PKEY_type() can return sensible
268          * values. We go through all keymgmt names, because the keytype
269          * that's passed to this function doesn't necessarily translate
270          * directly.
271          */
272         if (keymgmt != NULL) {
273             int tmp_id = evp_keymgmt_get_legacy_alg(keymgmt);
274 
275             if (tmp_id != NID_undef) {
276                 if (id == -1) {
277                     id = tmp_id;
278                 } else {
279                     /*
280                      * It really really shouldn't differ.  If it still does,
281                      * something is very wrong.
282                      */
283                     if (!ossl_assert(id == tmp_id)) {
284                         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
285                         EVP_KEYMGMT_free(keymgmt);
286                         return NULL;
287                     }
288                 }
289             }
290         }
291 #endif
292     }
293 
294     if (pmeth == NULL && keymgmt == NULL) {
295         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
296     } else {
297         ret = OPENSSL_zalloc(sizeof(*ret));
298     }
299 
300 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
301     if ((ret == NULL || pmeth == NULL) && e != NULL)
302         ENGINE_finish(e);
303 #endif
304 
305     if (ret == NULL) {
306         EVP_KEYMGMT_free(keymgmt);
307         return NULL;
308     }
309     if (propquery != NULL) {
310         ret->propquery = OPENSSL_strdup(propquery);
311         if (ret->propquery == NULL) {
312             OPENSSL_free(ret);
313             EVP_KEYMGMT_free(keymgmt);
314             return NULL;
315         }
316     }
317     ret->libctx = libctx;
318     ret->keytype = keytype;
319     ret->keymgmt = keymgmt;
320     ret->legacy_keytype = id;
321     ret->engine = e;
322     ret->pmeth = pmeth;
323     ret->operation = EVP_PKEY_OP_UNDEFINED;
324 
325     if (pkey != NULL && !EVP_PKEY_up_ref(pkey)) {
326         EVP_PKEY_CTX_free(ret);
327         return NULL;
328     }
329 
330     ret->pkey = pkey;
331 
332     if (pmeth != NULL && pmeth->init != NULL) {
333         if (pmeth->init(ret) <= 0) {
334             ret->pmeth = NULL;
335             EVP_PKEY_CTX_free(ret);
336             return NULL;
337         }
338     }
339 
340     return ret;
341 }
342 
343 /*- All methods below can also be used in FIPS_MODULE */
344 
EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX * libctx,const char * name,const char * propquery)345 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
346                                          const char *name,
347                                          const char *propquery)
348 {
349     return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
350 }
351 
EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX * libctx,EVP_PKEY * pkey,const char * propquery)352 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
353                                          const char *propquery)
354 {
355     return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
356 }
357 
evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX * ctx)358 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
359 {
360     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
361         if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
362             ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
363         EVP_SIGNATURE_free(ctx->op.sig.signature);
364         ctx->op.sig.algctx = NULL;
365         ctx->op.sig.signature = NULL;
366     } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
367         if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
368             ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
369         EVP_KEYEXCH_free(ctx->op.kex.exchange);
370         ctx->op.kex.algctx = NULL;
371         ctx->op.kex.exchange = NULL;
372     } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
373         if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
374             ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
375         EVP_KEM_free(ctx->op.encap.kem);
376         ctx->op.encap.algctx = NULL;
377         ctx->op.encap.kem = NULL;
378     }
379     else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
380         if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
381             ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
382         EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
383         ctx->op.ciph.algctx = NULL;
384         ctx->op.ciph.cipher = NULL;
385     } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
386         if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
387             evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
388     }
389 }
390 
EVP_PKEY_CTX_free(EVP_PKEY_CTX * ctx)391 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
392 {
393     if (ctx == NULL)
394         return;
395     if (ctx->pmeth && ctx->pmeth->cleanup)
396         ctx->pmeth->cleanup(ctx);
397 
398     evp_pkey_ctx_free_old_ops(ctx);
399 #ifndef FIPS_MODULE
400     evp_pkey_ctx_free_all_cached_data(ctx);
401 #endif
402     EVP_KEYMGMT_free(ctx->keymgmt);
403 
404     OPENSSL_free(ctx->propquery);
405     EVP_PKEY_free(ctx->pkey);
406     EVP_PKEY_free(ctx->peerkey);
407 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
408     ENGINE_finish(ctx->engine);
409 #endif
410     BN_free(ctx->rsa_pubexp);
411     OPENSSL_free(ctx);
412 }
413 
414 #ifndef FIPS_MODULE
415 
EVP_PKEY_meth_get0_info(int * ppkey_id,int * pflags,const EVP_PKEY_METHOD * meth)416 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
417                              const EVP_PKEY_METHOD *meth)
418 {
419     if (ppkey_id)
420         *ppkey_id = meth->pkey_id;
421     if (pflags)
422         *pflags = meth->flags;
423 }
424 
EVP_PKEY_meth_copy(EVP_PKEY_METHOD * dst,const EVP_PKEY_METHOD * src)425 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
426 {
427     int pkey_id = dst->pkey_id;
428     int flags = dst->flags;
429 
430     *dst = *src;
431 
432     /* We only copy the function pointers so restore the other values */
433     dst->pkey_id = pkey_id;
434     dst->flags = flags;
435 }
436 
EVP_PKEY_meth_free(EVP_PKEY_METHOD * pmeth)437 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
438 {
439     if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
440         OPENSSL_free(pmeth);
441 }
442 
EVP_PKEY_CTX_new(EVP_PKEY * pkey,ENGINE * e)443 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
444 {
445     return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
446 }
447 
EVP_PKEY_CTX_new_id(int id,ENGINE * e)448 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
449 {
450     return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
451 }
452 
EVP_PKEY_CTX_dup(const EVP_PKEY_CTX * pctx)453 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
454 {
455     EVP_PKEY_CTX *rctx;
456 
457 # ifndef OPENSSL_NO_ENGINE
458     /* Make sure it's safe to copy a pkey context using an ENGINE */
459     if (pctx->engine && !ENGINE_init(pctx->engine)) {
460         ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
461         return 0;
462     }
463 # endif
464     rctx = OPENSSL_zalloc(sizeof(*rctx));
465     if (rctx == NULL)
466         return NULL;
467 
468     if (pctx->pkey != NULL && !EVP_PKEY_up_ref(pctx->pkey))
469         goto err;
470 
471     rctx->pkey = pctx->pkey;
472     rctx->operation = pctx->operation;
473     rctx->libctx = pctx->libctx;
474     rctx->keytype = pctx->keytype;
475     rctx->propquery = NULL;
476     if (pctx->propquery != NULL) {
477         rctx->propquery = OPENSSL_strdup(pctx->propquery);
478         if (rctx->propquery == NULL)
479             goto err;
480     }
481     rctx->legacy_keytype = pctx->legacy_keytype;
482 
483     if (pctx->keymgmt != NULL) {
484         if (!EVP_KEYMGMT_up_ref(pctx->keymgmt))
485             goto err;
486         rctx->keymgmt = pctx->keymgmt;
487     }
488 
489     if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
490         if (pctx->op.kex.exchange != NULL) {
491             rctx->op.kex.exchange = pctx->op.kex.exchange;
492             if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
493                 goto err;
494         }
495         if (pctx->op.kex.algctx != NULL) {
496             if (!ossl_assert(pctx->op.kex.exchange != NULL))
497                 goto err;
498 
499             if (pctx->op.kex.exchange->dupctx != NULL)
500                 rctx->op.kex.algctx
501                     = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
502 
503             if (rctx->op.kex.algctx == NULL) {
504                 EVP_KEYEXCH_free(rctx->op.kex.exchange);
505                 rctx->op.kex.exchange = NULL;
506                 goto err;
507             }
508             return rctx;
509         }
510     } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
511         if (pctx->op.sig.signature != NULL) {
512             rctx->op.sig.signature = pctx->op.sig.signature;
513             if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
514                 goto err;
515         }
516         if (pctx->op.sig.algctx != NULL) {
517             if (!ossl_assert(pctx->op.sig.signature != NULL))
518                 goto err;
519 
520             if (pctx->op.sig.signature->dupctx != NULL)
521                 rctx->op.sig.algctx
522                     = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
523 
524             if (rctx->op.sig.algctx == NULL) {
525                 EVP_SIGNATURE_free(rctx->op.sig.signature);
526                 rctx->op.sig.signature = NULL;
527                 goto err;
528             }
529             return rctx;
530         }
531     } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
532         if (pctx->op.ciph.cipher != NULL) {
533             rctx->op.ciph.cipher = pctx->op.ciph.cipher;
534             if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
535                 goto err;
536         }
537         if (pctx->op.ciph.algctx != NULL) {
538             if (!ossl_assert(pctx->op.ciph.cipher != NULL))
539                 goto err;
540 
541             if (pctx->op.ciph.cipher->dupctx != NULL)
542                 rctx->op.ciph.algctx
543                     = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
544 
545             if (rctx->op.ciph.algctx == NULL) {
546                 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
547                 rctx->op.ciph.cipher = NULL;
548                 goto err;
549             }
550             return rctx;
551         }
552     } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
553         if (pctx->op.encap.kem != NULL) {
554             rctx->op.encap.kem = pctx->op.encap.kem;
555             if (!EVP_KEM_up_ref(rctx->op.encap.kem))
556                 goto err;
557         }
558         if (pctx->op.encap.algctx != NULL) {
559             if (!ossl_assert(pctx->op.encap.kem != NULL))
560                 goto err;
561 
562             if (pctx->op.encap.kem->dupctx != NULL)
563                 rctx->op.encap.algctx
564                     = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
565 
566             if (rctx->op.encap.algctx == NULL) {
567                 EVP_KEM_free(rctx->op.encap.kem);
568                 rctx->op.encap.kem = NULL;
569                 goto err;
570             }
571             return rctx;
572         }
573     } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
574         /* Not supported - This would need a gen_dupctx() to work */
575         goto err;
576     }
577 
578     rctx->pmeth = pctx->pmeth;
579 # ifndef OPENSSL_NO_ENGINE
580     rctx->engine = pctx->engine;
581 # endif
582 
583     if (pctx->peerkey != NULL && !EVP_PKEY_up_ref(pctx->peerkey))
584         goto err;
585 
586     rctx->peerkey = pctx->peerkey;
587 
588     if (pctx->pmeth == NULL) {
589         if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
590             EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
591             void *provkey;
592 
593             if (pctx->pkey == NULL)
594                 return rctx;
595 
596             provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
597                                                   &tmp_keymgmt, pctx->propquery);
598             if (provkey == NULL)
599                 goto err;
600             if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
601                 goto err;
602             EVP_KEYMGMT_free(rctx->keymgmt);
603             rctx->keymgmt = tmp_keymgmt;
604             return rctx;
605         }
606     } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
607         return rctx;
608     }
609 err:
610     rctx->pmeth = NULL;
611     EVP_PKEY_CTX_free(rctx);
612     return NULL;
613 }
614 
EVP_PKEY_meth_add0(const EVP_PKEY_METHOD * pmeth)615 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
616 {
617     if (app_pkey_methods == NULL) {
618         app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
619         if (app_pkey_methods == NULL) {
620             ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
621             return 0;
622         }
623     }
624     if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
625         ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
626         return 0;
627     }
628     sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
629     return 1;
630 }
631 
evp_app_cleanup_int(void)632 void evp_app_cleanup_int(void)
633 {
634     if (app_pkey_methods != NULL)
635         sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
636 }
637 
EVP_PKEY_meth_remove(const EVP_PKEY_METHOD * pmeth)638 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
639 {
640     const EVP_PKEY_METHOD *ret;
641 
642     ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
643 
644     return ret == NULL ? 0 : 1;
645 }
646 
EVP_PKEY_meth_get_count(void)647 size_t EVP_PKEY_meth_get_count(void)
648 {
649     size_t rv = OSSL_NELEM(standard_methods);
650 
651     if (app_pkey_methods)
652         rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
653     return rv;
654 }
655 
EVP_PKEY_meth_get0(size_t idx)656 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
657 {
658     if (idx < OSSL_NELEM(standard_methods))
659         return (standard_methods[idx])();
660     if (app_pkey_methods == NULL)
661         return NULL;
662     idx -= OSSL_NELEM(standard_methods);
663     if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
664         return NULL;
665     return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
666 }
667 #endif
668 
EVP_PKEY_CTX_is_a(EVP_PKEY_CTX * ctx,const char * keytype)669 int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
670 {
671 #ifndef FIPS_MODULE
672     if (evp_pkey_ctx_is_legacy(ctx))
673         return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
674 #endif
675     return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
676 }
677 
EVP_PKEY_CTX_set_params(EVP_PKEY_CTX * ctx,const OSSL_PARAM * params)678 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
679 {
680     switch (evp_pkey_ctx_state(ctx)) {
681     case EVP_PKEY_STATE_PROVIDER:
682         if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
683             && ctx->op.kex.exchange != NULL
684             && ctx->op.kex.exchange->set_ctx_params != NULL)
685             return
686                 ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
687                                                      params);
688         if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
689             && ctx->op.sig.signature != NULL
690             && ctx->op.sig.signature->set_ctx_params != NULL)
691             return
692                 ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
693                                                       params);
694         if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
695             && ctx->op.ciph.cipher != NULL
696             && ctx->op.ciph.cipher->set_ctx_params != NULL)
697             return
698                 ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
699                                                     params);
700         if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
701             && ctx->keymgmt != NULL
702             && ctx->keymgmt->gen_set_params != NULL)
703             return
704                 evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
705                                            params);
706         if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
707             && ctx->op.encap.kem != NULL
708             && ctx->op.encap.kem->set_ctx_params != NULL)
709             return
710                 ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
711                                                   params);
712         break;
713     case EVP_PKEY_STATE_UNKNOWN:
714         break;
715 #ifndef FIPS_MODULE
716     case EVP_PKEY_STATE_LEGACY:
717         return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
718 #endif
719     }
720     return 0;
721 }
722 
EVP_PKEY_CTX_get_params(EVP_PKEY_CTX * ctx,OSSL_PARAM * params)723 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
724 {
725     switch (evp_pkey_ctx_state(ctx)) {
726     case EVP_PKEY_STATE_PROVIDER:
727         if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
728             && ctx->op.kex.exchange != NULL
729             && ctx->op.kex.exchange->get_ctx_params != NULL)
730             return
731                 ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
732                                                      params);
733         if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
734             && ctx->op.sig.signature != NULL
735             && ctx->op.sig.signature->get_ctx_params != NULL)
736             return
737                 ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
738                                                       params);
739         if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
740             && ctx->op.ciph.cipher != NULL
741             && ctx->op.ciph.cipher->get_ctx_params != NULL)
742             return
743                 ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
744                                                     params);
745         if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
746             && ctx->op.encap.kem != NULL
747             && ctx->op.encap.kem->get_ctx_params != NULL)
748             return
749                 ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
750                                                   params);
751         if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
752             && ctx->keymgmt != NULL
753             && ctx->keymgmt->gen_get_params != NULL)
754             return
755                 evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
756                                            params);
757         break;
758     case EVP_PKEY_STATE_UNKNOWN:
759         break;
760 #ifndef FIPS_MODULE
761     case EVP_PKEY_STATE_LEGACY:
762         return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
763 #endif
764     }
765     return 0;
766 }
767 
768 #ifndef FIPS_MODULE
EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX * ctx)769 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
770 {
771     void *provctx;
772 
773     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
774             && ctx->op.kex.exchange != NULL
775             && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
776         provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
777         return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
778                                                          provctx);
779     }
780     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
781             && ctx->op.sig.signature != NULL
782             && ctx->op.sig.signature->gettable_ctx_params != NULL) {
783         provctx = ossl_provider_ctx(
784                       EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
785         return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
786                                                           provctx);
787     }
788     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
789             && ctx->op.ciph.cipher != NULL
790             && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
791         provctx = ossl_provider_ctx(
792                       EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
793         return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
794                                                         provctx);
795     }
796     if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
797         && ctx->op.encap.kem != NULL
798         && ctx->op.encap.kem->gettable_ctx_params != NULL) {
799         provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
800         return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
801                                                       provctx);
802     }
803     if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
804             && ctx->keymgmt != NULL
805             && ctx->keymgmt->gen_gettable_params != NULL) {
806         provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
807         return ctx->keymgmt->gen_gettable_params(ctx->op.keymgmt.genctx,
808                                                  provctx);
809     }
810     return NULL;
811 }
812 
EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX * ctx)813 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
814 {
815     void *provctx;
816 
817     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
818             && ctx->op.kex.exchange != NULL
819             && ctx->op.kex.exchange->settable_ctx_params != NULL) {
820         provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
821         return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
822                                                          provctx);
823     }
824     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
825             && ctx->op.sig.signature != NULL
826             && ctx->op.sig.signature->settable_ctx_params != NULL) {
827         provctx = ossl_provider_ctx(
828                       EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
829         return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
830                                                           provctx);
831     }
832     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
833             && ctx->op.ciph.cipher != NULL
834             && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
835         provctx = ossl_provider_ctx(
836                       EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
837         return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
838                                                         provctx);
839     }
840     if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
841             && ctx->keymgmt != NULL
842             && ctx->keymgmt->gen_settable_params != NULL) {
843         provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
844         return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
845                                                  provctx);
846     }
847     if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
848         && ctx->op.encap.kem != NULL
849         && ctx->op.encap.kem->settable_ctx_params != NULL) {
850         provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
851         return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
852                                                       provctx);
853     }
854     return NULL;
855 }
856 
857 /*
858  * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
859  *
860  * Return 1 on success, 0 or negative for errors.
861  *
862  * In particular they return -2 if any of the params is not supported.
863  *
864  * They are not available in FIPS_MODULE as they depend on
865  *      - EVP_PKEY_CTX_{get,set}_params()
866  *      - EVP_PKEY_CTX_{gettable,settable}_params()
867  *
868  */
evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX * ctx,OSSL_PARAM * params)869 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
870 {
871     if (ctx == NULL || params == NULL)
872         return 0;
873 
874     /*
875      * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
876      * depend on the translation that happens in EVP_PKEY_CTX_set_params()
877      * call, and that the resulting ctrl call will return -2 if it doesn't
878      * known the ctrl command number.
879      */
880     if (evp_pkey_ctx_is_provided(ctx)) {
881         const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
882         const OSSL_PARAM *p;
883 
884         for (p = params; p->key != NULL; p++) {
885             /* Check the ctx actually understands this parameter */
886             if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
887                 return -2;
888         }
889     }
890 
891     return EVP_PKEY_CTX_set_params(ctx, params);
892 }
893 
evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX * ctx,OSSL_PARAM * params)894 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
895 {
896     if (ctx == NULL || params == NULL)
897         return 0;
898 
899     /*
900      * We only check for provider side EVP_PKEY_CTX.  For #legacy, we
901      * depend on the translation that happens in EVP_PKEY_CTX_get_params()
902      * call, and that the resulting ctrl call will return -2 if it doesn't
903      * known the ctrl command number.
904      */
905     if (evp_pkey_ctx_is_provided(ctx)) {
906         const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
907         const OSSL_PARAM *p;
908 
909         for (p = params; p->key != NULL; p++) {
910             /* Check the ctx actually understands this parameter */
911             if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
912                 return -2;
913         }
914     }
915 
916     return EVP_PKEY_CTX_get_params(ctx, params);
917 }
918 
EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX * ctx,const EVP_MD ** md)919 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
920 {
921     OSSL_PARAM sig_md_params[2], *p = sig_md_params;
922     /* 80 should be big enough */
923     char name[80] = "";
924     const EVP_MD *tmp;
925 
926     if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
927         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
928         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
929         return -2;
930     }
931 
932     if (ctx->op.sig.algctx == NULL)
933         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
934                                  EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
935 
936     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
937                                             name,
938                                             sizeof(name));
939     *p = OSSL_PARAM_construct_end();
940 
941     if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
942         return 0;
943 
944     tmp = evp_get_digestbyname_ex(ctx->libctx, name);
945     if (tmp == NULL)
946         return 0;
947 
948     *md = tmp;
949 
950     return 1;
951 }
952 
evp_pkey_ctx_set_md(EVP_PKEY_CTX * ctx,const EVP_MD * md,int fallback,const char * param,int op,int ctrl)953 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
954                                int fallback, const char *param, int op,
955                                int ctrl)
956 {
957     OSSL_PARAM md_params[2], *p = md_params;
958     const char *name;
959 
960     if (ctx == NULL || (ctx->operation & op) == 0) {
961         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
962         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
963         return -2;
964     }
965 
966     if (fallback)
967         return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
968 
969     if (md == NULL) {
970         name = "";
971     } else {
972         name = EVP_MD_get0_name(md);
973     }
974 
975     *p++ = OSSL_PARAM_construct_utf8_string(param,
976                                             /*
977                                              * Cast away the const. This is read
978                                              * only so should be safe
979                                              */
980                                             (char *)name, 0);
981     *p = OSSL_PARAM_construct_end();
982 
983     return EVP_PKEY_CTX_set_params(ctx, md_params);
984 }
985 
EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX * ctx,const EVP_MD * md)986 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
987 {
988     return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
989                                OSSL_SIGNATURE_PARAM_DIGEST,
990                                EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
991 }
992 
EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX * ctx,const EVP_MD * md)993 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
994 {
995     return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
996                                OSSL_KDF_PARAM_DIGEST,
997                                EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
998 }
999 
evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX * ctx,int fallback,const char * param,int op,int ctrl,const unsigned char * data,int datalen)1000 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
1001                                           const char *param, int op, int ctrl,
1002                                           const unsigned char *data,
1003                                           int datalen)
1004 {
1005     OSSL_PARAM octet_string_params[2], *p = octet_string_params;
1006 
1007     if (ctx == NULL || (ctx->operation & op) == 0) {
1008         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1009         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1010         return -2;
1011     }
1012 
1013     /* Code below to be removed when legacy support is dropped. */
1014     if (fallback)
1015         return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
1016     /* end of legacy support */
1017 
1018     if (datalen < 0) {
1019         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1020         return 0;
1021     }
1022 
1023     *p++ = OSSL_PARAM_construct_octet_string(param,
1024                                             /*
1025                                              * Cast away the const. This is read
1026                                              * only so should be safe
1027                                              */
1028                                             (unsigned char *)data,
1029                                             (size_t)datalen);
1030     *p = OSSL_PARAM_construct_end();
1031 
1032     return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
1033 }
1034 
evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX * ctx,int fallback,const char * param,int op,int ctrl,const unsigned char * data,int datalen)1035 static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
1036                                           const char *param, int op, int ctrl,
1037                                           const unsigned char *data,
1038                                           int datalen)
1039 {
1040     OSSL_PARAM os_params[2];
1041     const OSSL_PARAM *gettables;
1042     unsigned char *info = NULL;
1043     size_t info_len = 0;
1044     size_t info_alloc = 0;
1045     int ret = 0;
1046 
1047     if (ctx == NULL || (ctx->operation & op) == 0) {
1048         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1049         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1050         return -2;
1051     }
1052 
1053     /* Code below to be removed when legacy support is dropped. */
1054     if (fallback)
1055         return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
1056     /* end of legacy support */
1057 
1058     if (datalen < 0) {
1059         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1060         return 0;
1061     } else if (datalen == 0) {
1062         return 1;
1063     }
1064 
1065     /* Check for older provider that doesn't support getting this parameter */
1066     gettables = EVP_PKEY_CTX_gettable_params(ctx);
1067     if (gettables == NULL || OSSL_PARAM_locate_const(gettables, param) == NULL)
1068         return evp_pkey_ctx_set1_octet_string(ctx, fallback, param, op, ctrl,
1069                                               data, datalen);
1070 
1071     /* Get the original value length */
1072     os_params[0] = OSSL_PARAM_construct_octet_string(param, NULL, 0);
1073     os_params[1] = OSSL_PARAM_construct_end();
1074 
1075     if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1076         return 0;
1077 
1078     /* This should not happen but check to be sure. */
1079     if (os_params[0].return_size == OSSL_PARAM_UNMODIFIED)
1080         return 0;
1081 
1082     info_alloc = os_params[0].return_size + datalen;
1083     if (info_alloc == 0)
1084         return 0;
1085     info = OPENSSL_zalloc(info_alloc);
1086     if (info == NULL)
1087         return 0;
1088     info_len = os_params[0].return_size;
1089 
1090     os_params[0] = OSSL_PARAM_construct_octet_string(param, info, info_alloc);
1091 
1092     /* if we have data, then go get it */
1093     if (info_len > 0) {
1094         if (!EVP_PKEY_CTX_get_params(ctx, os_params))
1095             goto error;
1096     }
1097 
1098     /* Copy the input data */
1099     memcpy(&info[info_len], data, datalen);
1100     ret = EVP_PKEY_CTX_set_params(ctx, os_params);
1101 
1102  error:
1103     OPENSSL_clear_free(info, info_alloc);
1104     return ret;
1105 }
1106 
EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX * ctx,const unsigned char * sec,int seclen)1107 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1108                                       const unsigned char *sec, int seclen)
1109 {
1110     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1111                                           OSSL_KDF_PARAM_SECRET,
1112                                           EVP_PKEY_OP_DERIVE,
1113                                           EVP_PKEY_CTRL_TLS_SECRET,
1114                                           sec, seclen);
1115 }
1116 
EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX * ctx,const unsigned char * seed,int seedlen)1117 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1118                                     const unsigned char *seed, int seedlen)
1119 {
1120     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1121                                           OSSL_KDF_PARAM_SEED,
1122                                           EVP_PKEY_OP_DERIVE,
1123                                           EVP_PKEY_CTRL_TLS_SEED,
1124                                           seed, seedlen);
1125 }
1126 
EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX * ctx,const EVP_MD * md)1127 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1128 {
1129     return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
1130                                OSSL_KDF_PARAM_DIGEST,
1131                                EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1132 }
1133 
EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX * ctx,const unsigned char * salt,int saltlen)1134 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1135                                 const unsigned char *salt, int saltlen)
1136 {
1137     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1138                                           OSSL_KDF_PARAM_SALT,
1139                                           EVP_PKEY_OP_DERIVE,
1140                                           EVP_PKEY_CTRL_HKDF_SALT,
1141                                           salt, saltlen);
1142 }
1143 
EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX * ctx,const unsigned char * key,int keylen)1144 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1145                                       const unsigned char *key, int keylen)
1146 {
1147     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1148                                           OSSL_KDF_PARAM_KEY,
1149                                           EVP_PKEY_OP_DERIVE,
1150                                           EVP_PKEY_CTRL_HKDF_KEY,
1151                                           key, keylen);
1152 }
1153 
EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX * ctx,const unsigned char * info,int infolen)1154 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1155                                       const unsigned char *info, int infolen)
1156 {
1157     return evp_pkey_ctx_add1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1158                                           OSSL_KDF_PARAM_INFO,
1159                                           EVP_PKEY_OP_DERIVE,
1160                                           EVP_PKEY_CTRL_HKDF_INFO,
1161                                           info, infolen);
1162 }
1163 
EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX * ctx,int mode)1164 int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
1165 {
1166     OSSL_PARAM int_params[2], *p = int_params;
1167 
1168     if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1169         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1170         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1171         return -2;
1172     }
1173 
1174     /* Code below to be removed when legacy support is dropped. */
1175     if (ctx->op.kex.algctx == NULL)
1176         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1177                                  EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1178     /* end of legacy support */
1179 
1180     if (mode < 0) {
1181         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1182         return 0;
1183     }
1184 
1185     *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1186     *p = OSSL_PARAM_construct_end();
1187 
1188     return EVP_PKEY_CTX_set_params(ctx, int_params);
1189 }
1190 
EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX * ctx,const char * pass,int passlen)1191 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1192                                int passlen)
1193 {
1194     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1195                                           OSSL_KDF_PARAM_PASSWORD,
1196                                           EVP_PKEY_OP_DERIVE,
1197                                           EVP_PKEY_CTRL_PASS,
1198                                           (const unsigned char *)pass, passlen);
1199 }
1200 
EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX * ctx,const unsigned char * salt,int saltlen)1201 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1202                                   const unsigned char *salt, int saltlen)
1203 {
1204     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1205                                           OSSL_KDF_PARAM_SALT,
1206                                           EVP_PKEY_OP_DERIVE,
1207                                           EVP_PKEY_CTRL_SCRYPT_SALT,
1208                                           salt, saltlen);
1209 }
1210 
evp_pkey_ctx_set_uint64(EVP_PKEY_CTX * ctx,const char * param,int op,int ctrl,uint64_t val)1211 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1212                                    int op, int ctrl, uint64_t val)
1213 {
1214     OSSL_PARAM uint64_params[2], *p = uint64_params;
1215 
1216     if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1217         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1218         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1219         return -2;
1220     }
1221 
1222     /* Code below to be removed when legacy support is dropped. */
1223     if (ctx->op.kex.algctx == NULL)
1224         return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1225     /* end of legacy support */
1226 
1227     *p++ = OSSL_PARAM_construct_uint64(param, &val);
1228     *p = OSSL_PARAM_construct_end();
1229 
1230     return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1231 }
1232 
EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX * ctx,uint64_t n)1233 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1234 {
1235     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1236                                    EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1237                                    n);
1238 }
1239 
EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX * ctx,uint64_t r)1240 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1241 {
1242     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1243                                    EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1244                                    r);
1245 }
1246 
EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX * ctx,uint64_t p)1247 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1248 {
1249     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1250                                    EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1251                                    p);
1252 }
1253 
EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX * ctx,uint64_t maxmem_bytes)1254 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1255                                          uint64_t maxmem_bytes)
1256 {
1257     return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1258                                    EVP_PKEY_OP_DERIVE,
1259                                    EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1260                                    maxmem_bytes);
1261 }
1262 
EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX * ctx,const unsigned char * key,int keylen)1263 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1264                              int keylen)
1265 {
1266     return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1267                                           OSSL_PKEY_PARAM_PRIV_KEY,
1268                                           EVP_PKEY_OP_KEYGEN,
1269                                           EVP_PKEY_CTRL_SET_MAC_KEY,
1270                                           key, keylen);
1271 }
1272 
EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX * ctx,const char * op)1273 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1274 {
1275     OSSL_PARAM params[2], *p = params;
1276 
1277     if (ctx == NULL || op == NULL) {
1278         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1279         return 0;
1280     }
1281     if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1282         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1283         return -2;
1284     }
1285     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1286                                             (char *)op, 0);
1287     *p = OSSL_PARAM_construct_end();
1288     return EVP_PKEY_CTX_set_params(ctx, params);
1289 }
1290 
EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX * ctx,const void * id,int len)1291 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1292 {
1293     return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1294                              EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1295 }
1296 
EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX * ctx,void * id)1297 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1298 {
1299     return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1300 }
1301 
EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX * ctx,size_t * id_len)1302 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1303 {
1304     return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1305                              EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1306 }
1307 
evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX * ctx,int keytype,int optype,int cmd,int p1,void * p2)1308 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1309                                  int cmd, int p1, void *p2)
1310 {
1311     int ret = 0;
1312 
1313     /*
1314      * If the method has a |digest_custom| function, we can relax the
1315      * operation type check, since this can be called before the operation
1316      * is initialized.
1317      */
1318     if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1319         if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1320             ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1321             return -1;
1322         }
1323 
1324         if ((optype != -1) && !(ctx->operation & optype)) {
1325             ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1326             return -1;
1327         }
1328     }
1329 
1330     switch (evp_pkey_ctx_state(ctx)) {
1331     case EVP_PKEY_STATE_PROVIDER:
1332         return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1333     case EVP_PKEY_STATE_UNKNOWN:
1334     case EVP_PKEY_STATE_LEGACY:
1335         if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1336             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1337             return -2;
1338         }
1339         if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1340             return -1;
1341 
1342         ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1343 
1344         if (ret == -2)
1345             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1346         break;
1347     }
1348     return ret;
1349 }
1350 
EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX * ctx,int keytype,int optype,int cmd,int p1,void * p2)1351 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1352                       int cmd, int p1, void *p2)
1353 {
1354     int ret = 0;
1355 
1356     if (ctx == NULL) {
1357         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1358         return -2;
1359     }
1360     /* If unsupported, we don't want that reported here */
1361     ERR_set_mark();
1362     ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1363                                          cmd, NULL, p2, p1);
1364     if (ret == -2) {
1365         ERR_pop_to_mark();
1366     } else {
1367         ERR_clear_last_mark();
1368         /*
1369          * If there was an error, there was an error.
1370          * If the operation isn't initialized yet, we also return, as
1371          * the saved values will be used then anyway.
1372          */
1373         if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1374             return ret;
1375     }
1376     return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1377 }
1378 
EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX * ctx,int keytype,int optype,int cmd,uint64_t value)1379 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1380                              int cmd, uint64_t value)
1381 {
1382     return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1383 }
1384 
1385 
evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX * ctx,const char * name,const char * value)1386 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1387                                      const char *name, const char *value)
1388 {
1389     int ret = 0;
1390 
1391     if (ctx == NULL) {
1392         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1393         return -2;
1394     }
1395 
1396     switch (evp_pkey_ctx_state(ctx)) {
1397     case EVP_PKEY_STATE_PROVIDER:
1398         return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1399     case EVP_PKEY_STATE_UNKNOWN:
1400     case EVP_PKEY_STATE_LEGACY:
1401         if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1402             ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1403             return -2;
1404         }
1405         if (strcmp(name, "digest") == 0)
1406             ret = EVP_PKEY_CTX_md(ctx,
1407                                   EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1408                                   EVP_PKEY_CTRL_MD, value);
1409         else
1410             ret = ctx->pmeth->ctrl_str(ctx, name, value);
1411         break;
1412     }
1413 
1414     return ret;
1415 }
1416 
EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX * ctx,const char * name,const char * value)1417 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1418                           const char *name, const char *value)
1419 {
1420     int ret = 0;
1421 
1422     /* If unsupported, we don't want that reported here */
1423     ERR_set_mark();
1424     ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1425                                          name, value, strlen(value) + 1);
1426     if (ret == -2) {
1427         ERR_pop_to_mark();
1428     } else {
1429         ERR_clear_last_mark();
1430         /*
1431          * If there was an error, there was an error.
1432          * If the operation isn't initialized yet, we also return, as
1433          * the saved values will be used then anyway.
1434          */
1435         if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1436             return ret;
1437     }
1438 
1439     return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1440 }
1441 
decode_cmd(int cmd,const char * name)1442 static int decode_cmd(int cmd, const char *name)
1443 {
1444     if (cmd == -1) {
1445         /*
1446          * The consequence of the assertion not being true is that this
1447          * function will return -1, which will cause the calling functions
1448          * to signal that the command is unsupported...  in non-debug mode.
1449          */
1450         if (ossl_assert(name != NULL))
1451             if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1452                 cmd = EVP_PKEY_CTRL_SET1_ID;
1453     }
1454 
1455     return cmd;
1456 }
1457 
evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX * ctx,int keytype,int optype,int cmd,const char * name,const void * data,size_t data_len)1458 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1459                                           int keytype, int optype,
1460                                           int cmd, const char *name,
1461                                           const void *data, size_t data_len)
1462 {
1463     /*
1464      * Check that it's one of the supported commands.  The ctrl commands
1465      * number cases here must correspond to the cases in the bottom switch
1466      * in this function.
1467      */
1468     switch (cmd = decode_cmd(cmd, name)) {
1469     case EVP_PKEY_CTRL_SET1_ID:
1470         break;
1471     default:
1472         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1473         return -2;
1474     }
1475 
1476     if (keytype != -1) {
1477         switch (evp_pkey_ctx_state(ctx)) {
1478         case EVP_PKEY_STATE_PROVIDER:
1479             if (ctx->keymgmt == NULL) {
1480                 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1481                 return -2;
1482             }
1483             if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1484                                   evp_pkey_type2name(keytype))) {
1485                 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1486                 return -1;
1487             }
1488             break;
1489         case EVP_PKEY_STATE_UNKNOWN:
1490         case EVP_PKEY_STATE_LEGACY:
1491             if (ctx->pmeth == NULL) {
1492                 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1493                 return -2;
1494             }
1495             if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
1496                 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1497                 return -1;
1498             }
1499             break;
1500         }
1501     }
1502     if (optype != -1 && (ctx->operation & optype) == 0) {
1503         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1504         return -1;
1505     }
1506 
1507     switch (cmd) {
1508     case EVP_PKEY_CTRL_SET1_ID:
1509         evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1510         if (name != NULL) {
1511             ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1512             if (ctx->cached_parameters.dist_id_name == NULL)
1513                 return 0;
1514         }
1515         if (data_len > 0) {
1516             ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1517             if (ctx->cached_parameters.dist_id == NULL)
1518                 return 0;
1519         }
1520         ctx->cached_parameters.dist_id_set = 1;
1521         ctx->cached_parameters.dist_id_len = data_len;
1522         break;
1523     }
1524     return 1;
1525 }
1526 
evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX * ctx,int cmd,const char * name)1527 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1528                                           int cmd, const char *name)
1529 {
1530     cmd = decode_cmd(cmd, name);
1531     switch (cmd) {
1532     case EVP_PKEY_CTRL_SET1_ID:
1533         OPENSSL_free(ctx->cached_parameters.dist_id);
1534         OPENSSL_free(ctx->cached_parameters.dist_id_name);
1535         ctx->cached_parameters.dist_id = NULL;
1536         ctx->cached_parameters.dist_id_name = NULL;
1537         break;
1538     }
1539 }
1540 
evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX * ctx)1541 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1542 {
1543     evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1544 }
1545 
evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX * ctx)1546 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1547 {
1548     int ret = 1;
1549 
1550     if (ret && ctx->cached_parameters.dist_id_set) {
1551         const char *name = ctx->cached_parameters.dist_id_name;
1552         const void *val = ctx->cached_parameters.dist_id;
1553         size_t len = ctx->cached_parameters.dist_id_len;
1554 
1555         if (name != NULL)
1556             ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1557         else
1558             ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1559                                         EVP_PKEY_CTRL_SET1_ID,
1560                                         (int)len, (void *)val);
1561     }
1562 
1563     return ret;
1564 }
1565 
EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX * ctx)1566 OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1567 {
1568     return ctx->libctx;
1569 }
1570 
EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX * ctx)1571 const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
1572 {
1573     return ctx->propquery;
1574 }
1575 
EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX * ctx)1576 const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
1577 {
1578     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1579         if (ctx->op.sig.signature != NULL)
1580             return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
1581     } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1582         if (ctx->op.kex.exchange != NULL)
1583             return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
1584     } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1585         if (ctx->op.encap.kem != NULL)
1586             return EVP_KEM_get0_provider(ctx->op.encap.kem);
1587     } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1588         if (ctx->op.ciph.cipher != NULL)
1589             return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
1590     } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1591         if (ctx->keymgmt != NULL)
1592             return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
1593     }
1594 
1595     return NULL;
1596 }
1597 
1598 /* Utility functions to send a string of hex string to a ctrl */
1599 
EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX * ctx,int cmd,const char * str)1600 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1601 {
1602     size_t len;
1603 
1604     len = strlen(str);
1605     if (len > INT_MAX)
1606         return -1;
1607     return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1608 }
1609 
EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX * ctx,int cmd,const char * hex)1610 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1611 {
1612     unsigned char *bin;
1613     long binlen;
1614     int rv = -1;
1615 
1616     bin = OPENSSL_hexstr2buf(hex, &binlen);
1617     if (bin == NULL)
1618         return 0;
1619     if (binlen <= INT_MAX)
1620         rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1621     OPENSSL_free(bin);
1622     return rv;
1623 }
1624 
1625 /* Pass a message digest to a ctrl */
EVP_PKEY_CTX_md(EVP_PKEY_CTX * ctx,int optype,int cmd,const char * md)1626 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1627 {
1628     const EVP_MD *m;
1629 
1630     if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1631         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1632         return 0;
1633     }
1634     return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1635 }
1636 
EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX * ctx)1637 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1638 {
1639     return ctx->operation;
1640 }
1641 
EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX * ctx,int * dat,int datlen)1642 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1643 {
1644     ctx->keygen_info = dat;
1645     ctx->keygen_info_count = datlen;
1646 }
1647 
EVP_PKEY_CTX_set_data(EVP_PKEY_CTX * ctx,void * data)1648 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1649 {
1650     ctx->data = data;
1651 }
1652 
EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX * ctx)1653 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1654 {
1655     return ctx->data;
1656 }
1657 
EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX * ctx)1658 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1659 {
1660     return ctx->pkey;
1661 }
1662 
EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX * ctx)1663 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1664 {
1665     return ctx->peerkey;
1666 }
1667 
EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX * ctx,void * data)1668 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1669 {
1670     ctx->app_data = data;
1671 }
1672 
EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX * ctx)1673 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1674 {
1675     return ctx->app_data;
1676 }
1677 
EVP_PKEY_meth_set_init(EVP_PKEY_METHOD * pmeth,int (* init)(EVP_PKEY_CTX * ctx))1678 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1679                             int (*init) (EVP_PKEY_CTX *ctx))
1680 {
1681     pmeth->init = init;
1682 }
1683 
EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD * pmeth,int (* copy)(EVP_PKEY_CTX * dst,const EVP_PKEY_CTX * src))1684 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1685                             int (*copy) (EVP_PKEY_CTX *dst,
1686                                          const EVP_PKEY_CTX *src))
1687 {
1688     pmeth->copy = copy;
1689 }
1690 
EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD * pmeth,void (* cleanup)(EVP_PKEY_CTX * ctx))1691 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1692                                void (*cleanup) (EVP_PKEY_CTX *ctx))
1693 {
1694     pmeth->cleanup = cleanup;
1695 }
1696 
EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD * pmeth,int (* paramgen_init)(EVP_PKEY_CTX * ctx),int (* paramgen)(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey))1697 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1698                                 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1699                                 int (*paramgen) (EVP_PKEY_CTX *ctx,
1700                                                  EVP_PKEY *pkey))
1701 {
1702     pmeth->paramgen_init = paramgen_init;
1703     pmeth->paramgen = paramgen;
1704 }
1705 
EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD * pmeth,int (* keygen_init)(EVP_PKEY_CTX * ctx),int (* keygen)(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey))1706 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1707                               int (*keygen_init) (EVP_PKEY_CTX *ctx),
1708                               int (*keygen) (EVP_PKEY_CTX *ctx,
1709                                              EVP_PKEY *pkey))
1710 {
1711     pmeth->keygen_init = keygen_init;
1712     pmeth->keygen = keygen;
1713 }
1714 
EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD * pmeth,int (* sign_init)(EVP_PKEY_CTX * ctx),int (* sign)(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen))1715 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1716                             int (*sign_init) (EVP_PKEY_CTX *ctx),
1717                             int (*sign) (EVP_PKEY_CTX *ctx,
1718                                          unsigned char *sig, size_t *siglen,
1719                                          const unsigned char *tbs,
1720                                          size_t tbslen))
1721 {
1722     pmeth->sign_init = sign_init;
1723     pmeth->sign = sign;
1724 }
1725 
EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD * pmeth,int (* verify_init)(EVP_PKEY_CTX * ctx),int (* verify)(EVP_PKEY_CTX * ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen))1726 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1727                               int (*verify_init) (EVP_PKEY_CTX *ctx),
1728                               int (*verify) (EVP_PKEY_CTX *ctx,
1729                                              const unsigned char *sig,
1730                                              size_t siglen,
1731                                              const unsigned char *tbs,
1732                                              size_t tbslen))
1733 {
1734     pmeth->verify_init = verify_init;
1735     pmeth->verify = verify;
1736 }
1737 
EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD * pmeth,int (* verify_recover_init)(EVP_PKEY_CTX * ctx),int (* verify_recover)(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen))1738 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1739                                       int (*verify_recover_init) (EVP_PKEY_CTX
1740                                                                   *ctx),
1741                                       int (*verify_recover) (EVP_PKEY_CTX
1742                                                              *ctx,
1743                                                              unsigned char
1744                                                              *sig,
1745                                                              size_t *siglen,
1746                                                              const unsigned
1747                                                              char *tbs,
1748                                                              size_t tbslen))
1749 {
1750     pmeth->verify_recover_init = verify_recover_init;
1751     pmeth->verify_recover = verify_recover;
1752 }
1753 
EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD * pmeth,int (* signctx_init)(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx),int (* signctx)(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,EVP_MD_CTX * mctx))1754 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1755                                int (*signctx_init) (EVP_PKEY_CTX *ctx,
1756                                                     EVP_MD_CTX *mctx),
1757                                int (*signctx) (EVP_PKEY_CTX *ctx,
1758                                                unsigned char *sig,
1759                                                size_t *siglen,
1760                                                EVP_MD_CTX *mctx))
1761 {
1762     pmeth->signctx_init = signctx_init;
1763     pmeth->signctx = signctx;
1764 }
1765 
EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD * pmeth,int (* verifyctx_init)(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx),int (* verifyctx)(EVP_PKEY_CTX * ctx,const unsigned char * sig,int siglen,EVP_MD_CTX * mctx))1766 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1767                                  int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1768                                                         EVP_MD_CTX *mctx),
1769                                  int (*verifyctx) (EVP_PKEY_CTX *ctx,
1770                                                    const unsigned char *sig,
1771                                                    int siglen,
1772                                                    EVP_MD_CTX *mctx))
1773 {
1774     pmeth->verifyctx_init = verifyctx_init;
1775     pmeth->verifyctx = verifyctx;
1776 }
1777 
EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD * pmeth,int (* encrypt_init)(EVP_PKEY_CTX * ctx),int (* encryptfn)(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen))1778 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1779                                int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1780                                int (*encryptfn) (EVP_PKEY_CTX *ctx,
1781                                                  unsigned char *out,
1782                                                  size_t *outlen,
1783                                                  const unsigned char *in,
1784                                                  size_t inlen))
1785 {
1786     pmeth->encrypt_init = encrypt_init;
1787     pmeth->encrypt = encryptfn;
1788 }
1789 
EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD * pmeth,int (* decrypt_init)(EVP_PKEY_CTX * ctx),int (* decrypt)(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen))1790 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1791                                int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1792                                int (*decrypt) (EVP_PKEY_CTX *ctx,
1793                                                unsigned char *out,
1794                                                size_t *outlen,
1795                                                const unsigned char *in,
1796                                                size_t inlen))
1797 {
1798     pmeth->decrypt_init = decrypt_init;
1799     pmeth->decrypt = decrypt;
1800 }
1801 
EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD * pmeth,int (* derive_init)(EVP_PKEY_CTX * ctx),int (* derive)(EVP_PKEY_CTX * ctx,unsigned char * key,size_t * keylen))1802 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1803                               int (*derive_init) (EVP_PKEY_CTX *ctx),
1804                               int (*derive) (EVP_PKEY_CTX *ctx,
1805                                              unsigned char *key,
1806                                              size_t *keylen))
1807 {
1808     pmeth->derive_init = derive_init;
1809     pmeth->derive = derive;
1810 }
1811 
EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD * pmeth,int (* ctrl)(EVP_PKEY_CTX * ctx,int type,int p1,void * p2),int (* ctrl_str)(EVP_PKEY_CTX * ctx,const char * type,const char * value))1812 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1813                             int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1814                                          void *p2),
1815                             int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1816                                              const char *type,
1817                                              const char *value))
1818 {
1819     pmeth->ctrl = ctrl;
1820     pmeth->ctrl_str = ctrl_str;
1821 }
1822 
EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD * pmeth,int (* digestsign)(EVP_MD_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen))1823 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1824     int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1825                        const unsigned char *tbs, size_t tbslen))
1826 {
1827     pmeth->digestsign = digestsign;
1828 }
1829 
EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD * pmeth,int (* digestverify)(EVP_MD_CTX * ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen))1830 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1831     int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1832                          size_t siglen, const unsigned char *tbs,
1833                          size_t tbslen))
1834 {
1835     pmeth->digestverify = digestverify;
1836 }
1837 
EVP_PKEY_meth_set_check(EVP_PKEY_METHOD * pmeth,int (* check)(EVP_PKEY * pkey))1838 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1839                              int (*check) (EVP_PKEY *pkey))
1840 {
1841     pmeth->check = check;
1842 }
1843 
EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD * pmeth,int (* check)(EVP_PKEY * pkey))1844 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1845                                     int (*check) (EVP_PKEY *pkey))
1846 {
1847     pmeth->public_check = check;
1848 }
1849 
EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD * pmeth,int (* check)(EVP_PKEY * pkey))1850 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1851                                    int (*check) (EVP_PKEY *pkey))
1852 {
1853     pmeth->param_check = check;
1854 }
1855 
EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD * pmeth,int (* digest_custom)(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx))1856 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1857                                      int (*digest_custom) (EVP_PKEY_CTX *ctx,
1858                                                            EVP_MD_CTX *mctx))
1859 {
1860     pmeth->digest_custom = digest_custom;
1861 }
1862 
EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD * pmeth,int (** pinit)(EVP_PKEY_CTX * ctx))1863 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1864                             int (**pinit) (EVP_PKEY_CTX *ctx))
1865 {
1866     *pinit = pmeth->init;
1867 }
1868 
EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD * pmeth,int (** pcopy)(EVP_PKEY_CTX * dst,const EVP_PKEY_CTX * src))1869 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1870                             int (**pcopy) (EVP_PKEY_CTX *dst,
1871                                            const EVP_PKEY_CTX *src))
1872 {
1873     *pcopy = pmeth->copy;
1874 }
1875 
EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD * pmeth,void (** pcleanup)(EVP_PKEY_CTX * ctx))1876 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1877                                void (**pcleanup) (EVP_PKEY_CTX *ctx))
1878 {
1879     *pcleanup = pmeth->cleanup;
1880 }
1881 
EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD * pmeth,int (** pparamgen_init)(EVP_PKEY_CTX * ctx),int (** pparamgen)(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey))1882 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1883                                 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1884                                 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1885                                                    EVP_PKEY *pkey))
1886 {
1887     if (pparamgen_init)
1888         *pparamgen_init = pmeth->paramgen_init;
1889     if (pparamgen)
1890         *pparamgen = pmeth->paramgen;
1891 }
1892 
EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD * pmeth,int (** pkeygen_init)(EVP_PKEY_CTX * ctx),int (** pkeygen)(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey))1893 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1894                               int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1895                               int (**pkeygen) (EVP_PKEY_CTX *ctx,
1896                                                EVP_PKEY *pkey))
1897 {
1898     if (pkeygen_init)
1899         *pkeygen_init = pmeth->keygen_init;
1900     if (pkeygen)
1901         *pkeygen = pmeth->keygen;
1902 }
1903 
EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD * pmeth,int (** psign_init)(EVP_PKEY_CTX * ctx),int (** psign)(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen))1904 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1905                             int (**psign_init) (EVP_PKEY_CTX *ctx),
1906                             int (**psign) (EVP_PKEY_CTX *ctx,
1907                                            unsigned char *sig, size_t *siglen,
1908                                            const unsigned char *tbs,
1909                                            size_t tbslen))
1910 {
1911     if (psign_init)
1912         *psign_init = pmeth->sign_init;
1913     if (psign)
1914         *psign = pmeth->sign;
1915 }
1916 
EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD * pmeth,int (** pverify_init)(EVP_PKEY_CTX * ctx),int (** pverify)(EVP_PKEY_CTX * ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen))1917 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1918                               int (**pverify_init) (EVP_PKEY_CTX *ctx),
1919                               int (**pverify) (EVP_PKEY_CTX *ctx,
1920                                                const unsigned char *sig,
1921                                                size_t siglen,
1922                                                const unsigned char *tbs,
1923                                                size_t tbslen))
1924 {
1925     if (pverify_init)
1926         *pverify_init = pmeth->verify_init;
1927     if (pverify)
1928         *pverify = pmeth->verify;
1929 }
1930 
EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD * pmeth,int (** pverify_recover_init)(EVP_PKEY_CTX * ctx),int (** pverify_recover)(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen))1931 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1932                                       int (**pverify_recover_init) (EVP_PKEY_CTX
1933                                                                     *ctx),
1934                                       int (**pverify_recover) (EVP_PKEY_CTX
1935                                                                *ctx,
1936                                                                unsigned char
1937                                                                *sig,
1938                                                                size_t *siglen,
1939                                                                const unsigned
1940                                                                char *tbs,
1941                                                                size_t tbslen))
1942 {
1943     if (pverify_recover_init)
1944         *pverify_recover_init = pmeth->verify_recover_init;
1945     if (pverify_recover)
1946         *pverify_recover = pmeth->verify_recover;
1947 }
1948 
EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD * pmeth,int (** psignctx_init)(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx),int (** psignctx)(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,EVP_MD_CTX * mctx))1949 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1950                                int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1951                                                       EVP_MD_CTX *mctx),
1952                                int (**psignctx) (EVP_PKEY_CTX *ctx,
1953                                                  unsigned char *sig,
1954                                                  size_t *siglen,
1955                                                  EVP_MD_CTX *mctx))
1956 {
1957     if (psignctx_init)
1958         *psignctx_init = pmeth->signctx_init;
1959     if (psignctx)
1960         *psignctx = pmeth->signctx;
1961 }
1962 
EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD * pmeth,int (** pverifyctx_init)(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx),int (** pverifyctx)(EVP_PKEY_CTX * ctx,const unsigned char * sig,int siglen,EVP_MD_CTX * mctx))1963 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1964                                  int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1965                                                           EVP_MD_CTX *mctx),
1966                                  int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1967                                                      const unsigned char *sig,
1968                                                      int siglen,
1969                                                      EVP_MD_CTX *mctx))
1970 {
1971     if (pverifyctx_init)
1972         *pverifyctx_init = pmeth->verifyctx_init;
1973     if (pverifyctx)
1974         *pverifyctx = pmeth->verifyctx;
1975 }
1976 
EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD * pmeth,int (** pencrypt_init)(EVP_PKEY_CTX * ctx),int (** pencryptfn)(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen))1977 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1978                                int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1979                                int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1980                                                    unsigned char *out,
1981                                                    size_t *outlen,
1982                                                    const unsigned char *in,
1983                                                    size_t inlen))
1984 {
1985     if (pencrypt_init)
1986         *pencrypt_init = pmeth->encrypt_init;
1987     if (pencryptfn)
1988         *pencryptfn = pmeth->encrypt;
1989 }
1990 
EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD * pmeth,int (** pdecrypt_init)(EVP_PKEY_CTX * ctx),int (** pdecrypt)(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen))1991 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1992                                int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1993                                int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1994                                                  unsigned char *out,
1995                                                  size_t *outlen,
1996                                                  const unsigned char *in,
1997                                                  size_t inlen))
1998 {
1999     if (pdecrypt_init)
2000         *pdecrypt_init = pmeth->decrypt_init;
2001     if (pdecrypt)
2002         *pdecrypt = pmeth->decrypt;
2003 }
2004 
EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD * pmeth,int (** pderive_init)(EVP_PKEY_CTX * ctx),int (** pderive)(EVP_PKEY_CTX * ctx,unsigned char * key,size_t * keylen))2005 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
2006                               int (**pderive_init) (EVP_PKEY_CTX *ctx),
2007                               int (**pderive) (EVP_PKEY_CTX *ctx,
2008                                                unsigned char *key,
2009                                                size_t *keylen))
2010 {
2011     if (pderive_init)
2012         *pderive_init = pmeth->derive_init;
2013     if (pderive)
2014         *pderive = pmeth->derive;
2015 }
2016 
EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD * pmeth,int (** pctrl)(EVP_PKEY_CTX * ctx,int type,int p1,void * p2),int (** pctrl_str)(EVP_PKEY_CTX * ctx,const char * type,const char * value))2017 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
2018                             int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
2019                                            void *p2),
2020                             int (**pctrl_str) (EVP_PKEY_CTX *ctx,
2021                                                const char *type,
2022                                                const char *value))
2023 {
2024     if (pctrl)
2025         *pctrl = pmeth->ctrl;
2026     if (pctrl_str)
2027         *pctrl_str = pmeth->ctrl_str;
2028 }
2029 
EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD * pmeth,int (** digestsign)(EVP_MD_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen))2030 void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
2031     int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2032                         const unsigned char *tbs, size_t tbslen))
2033 {
2034     if (digestsign)
2035         *digestsign = pmeth->digestsign;
2036 }
2037 
EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD * pmeth,int (** digestverify)(EVP_MD_CTX * ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen))2038 void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
2039     int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2040                           size_t siglen, const unsigned char *tbs,
2041                           size_t tbslen))
2042 {
2043     if (digestverify)
2044         *digestverify = pmeth->digestverify;
2045 }
2046 
EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD * pmeth,int (** pcheck)(EVP_PKEY * pkey))2047 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2048                              int (**pcheck) (EVP_PKEY *pkey))
2049 {
2050     if (pcheck != NULL)
2051         *pcheck = pmeth->check;
2052 }
2053 
EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD * pmeth,int (** pcheck)(EVP_PKEY * pkey))2054 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2055                                     int (**pcheck) (EVP_PKEY *pkey))
2056 {
2057     if (pcheck != NULL)
2058         *pcheck = pmeth->public_check;
2059 }
2060 
EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD * pmeth,int (** pcheck)(EVP_PKEY * pkey))2061 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2062                                    int (**pcheck) (EVP_PKEY *pkey))
2063 {
2064     if (pcheck != NULL)
2065         *pcheck = pmeth->param_check;
2066 }
2067 
EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD * pmeth,int (** pdigest_custom)(EVP_PKEY_CTX * ctx,EVP_MD_CTX * mctx))2068 void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
2069                                      int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2070                                                              EVP_MD_CTX *mctx))
2071 {
2072     if (pdigest_custom != NULL)
2073         *pdigest_custom = pmeth->digest_custom;
2074 }
2075 
2076 #endif /* FIPS_MODULE */
2077