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