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