EVP: Clarify the states of an EVP_PKEY
[openssl.git] / crypto / evp / pmeth_lib.c
1
2 /*
3  * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 /*
12  * DH low level APIs are deprecated for public use, but still ok for
13  * internal use.
14  */
15 #include "internal/deprecated.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <openssl/engine.h>
20 #include <openssl/evp.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/core_names.h>
23 #include <openssl/dh.h>
24 #include <openssl/rsa.h>
25 #include "internal/cryptlib.h"
26 #include "crypto/asn1.h"
27 #include "crypto/evp.h"
28 #include "internal/numbers.h"
29 #include "internal/provider.h"
30 #include "evp_local.h"
31
32 #ifndef FIPS_MODE
33
34 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
35 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
36
37 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
38
39 /* This array needs to be in order of NIDs */
40 static pmeth_fn standard_methods[] = {
41 # ifndef OPENSSL_NO_RSA
42     rsa_pkey_method,
43 # endif
44 # ifndef OPENSSL_NO_DH
45     dh_pkey_method,
46 # endif
47 # ifndef OPENSSL_NO_DSA
48     dsa_pkey_method,
49 # endif
50 # ifndef OPENSSL_NO_EC
51     ec_pkey_method,
52 # endif
53     hmac_pkey_method,
54 # ifndef OPENSSL_NO_CMAC
55     cmac_pkey_method,
56 # endif
57 # ifndef OPENSSL_NO_RSA
58     rsa_pss_pkey_method,
59 # endif
60 # ifndef OPENSSL_NO_DH
61     dhx_pkey_method,
62 # endif
63 # ifndef OPENSSL_NO_SCRYPT
64     scrypt_pkey_method,
65 # endif
66     tls1_prf_pkey_method,
67 # ifndef OPENSSL_NO_EC
68     ecx25519_pkey_method,
69     ecx448_pkey_method,
70 # endif
71     hkdf_pkey_method,
72 # ifndef OPENSSL_NO_POLY1305
73     poly1305_pkey_method,
74 # endif
75 # ifndef OPENSSL_NO_SIPHASH
76     siphash_pkey_method,
77 # endif
78 # ifndef OPENSSL_NO_EC
79     ed25519_pkey_method,
80     ed448_pkey_method,
81 # endif
82 # ifndef OPENSSL_NO_SM2
83     sm2_pkey_method,
84 # endif
85 };
86
87 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
88
89 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
90 {
91     return ((*a)->pkey_id - ((**b)())->pkey_id);
92 }
93
94 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
95
96 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
97                      const EVP_PKEY_METHOD *const *b)
98 {
99     return ((*a)->pkey_id - (*b)->pkey_id);
100 }
101
102 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
103 {
104     pmeth_fn *ret;
105     EVP_PKEY_METHOD tmp;
106     const EVP_PKEY_METHOD *t = &tmp;
107
108     tmp.pkey_id = type;
109     if (app_pkey_methods) {
110         int idx;
111         idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
112         if (idx >= 0)
113             return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
114     }
115     ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
116                                  sizeof(standard_methods) /
117                                  sizeof(pmeth_fn));
118     if (ret == NULL || *ret == NULL)
119         return NULL;
120     return (**ret)();
121 }
122
123 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
124 {
125     EVP_PKEY_METHOD *pmeth;
126
127     pmeth = OPENSSL_zalloc(sizeof(*pmeth));
128     if (pmeth == NULL) {
129         EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
130         return NULL;
131     }
132
133     pmeth->pkey_id = id;
134     pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
135     return pmeth;
136 }
137 #endif /* FIPS_MODE */
138
139 static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
140                                  EVP_PKEY *pkey, ENGINE *e,
141                                  const char *keytype, const char *propquery,
142                                  int id)
143
144 {
145     EVP_PKEY_CTX *ret;
146     const EVP_PKEY_METHOD *pmeth = NULL;
147     EVP_KEYMGMT *keymgmt = NULL;
148
149     /*
150      * When using providers, the context is bound to the algo implementation
151      * later.
152      */
153     if (pkey == NULL && e == NULL && id == -1)
154         goto common;
155
156     /*
157      * If the key doesn't contain anything legacy, then it must be provided,
158      * so we extract the necessary information and use that.
159      */
160     if (pkey != NULL && pkey->type == EVP_PKEY_NONE) {
161         /* If we have an engine, something went wrong somewhere... */
162         if (!ossl_assert(e == NULL))
163             return NULL;
164         keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
165         goto common;
166     }
167 #ifndef FIPS_MODE
168     /* TODO(3.0) Legacy code should be removed when all is provider based */
169     /* BEGIN legacy */
170     if (id == -1) {
171         if (pkey == NULL)
172             return NULL;
173         id = pkey->type;
174     }
175
176     /*
177      * Here, we extract what information we can for the purpose of
178      * supporting usage with implementations from providers, to make
179      * for a smooth transition from legacy stuff to provider based stuff.
180      *
181      * If an engine is given, this is entirely legacy, and we should not
182      * pretend anything else, so we only set the name when no engine is
183      * given.  If both are already given, someone made a mistake, and
184      * since that can only happen internally, it's safe to make an
185      * assertion.
186      */
187     if (!ossl_assert(e == NULL || keytype == NULL))
188         return NULL;
189     if (e == NULL)
190         keytype = OBJ_nid2sn(id);
191
192 # ifndef OPENSSL_NO_ENGINE
193     if (e == NULL && pkey != NULL)
194         e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
195     /* Try to find an ENGINE which implements this method */
196     if (e) {
197         if (!ENGINE_init(e)) {
198             EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
199             return NULL;
200         }
201     } else {
202         e = ENGINE_get_pkey_meth_engine(id);
203     }
204
205     /*
206      * If an ENGINE handled this method look it up. Otherwise use internal
207      * tables.
208      */
209     if (e)
210         pmeth = ENGINE_get_pkey_meth(e, id);
211     else
212 # endif
213         pmeth = EVP_PKEY_meth_find(id);
214
215     if (pmeth == NULL) {
216 # ifndef OPENSSL_NO_ENGINE
217         ENGINE_finish(e);
218 # endif
219         EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
220         return NULL;
221     }
222     /* END legacy */
223 #endif /* FIPS_MODE */
224  common:
225     /*
226      * If there's no engine and there's a name, we try fetching a provider
227      * implementation.
228      */
229     if (e == NULL && keytype != NULL)
230         keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
231
232     ret = OPENSSL_zalloc(sizeof(*ret));
233     if (ret == NULL) {
234 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
235         ENGINE_finish(e);
236 #endif
237         EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
238         return NULL;
239     }
240     ret->libctx = libctx;
241     ret->propquery = propquery;
242     ret->keytype = keytype;
243     ret->keymgmt = keymgmt;
244     ret->engine = e;
245     ret->pmeth = pmeth;
246     ret->operation = EVP_PKEY_OP_UNDEFINED;
247     ret->pkey = pkey;
248     if (pkey != NULL)
249         EVP_PKEY_up_ref(pkey);
250
251     if (pmeth != NULL && pmeth->init != NULL) {
252         if (pmeth->init(ret) <= 0) {
253             ret->pmeth = NULL;
254             EVP_PKEY_CTX_free(ret);
255             return NULL;
256         }
257     }
258
259     return ret;
260 }
261
262 /*- All methods below can also be used in FIPS_MODE */
263
264 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OPENSSL_CTX *libctx,
265                                          const char *name,
266                                          const char *propquery)
267 {
268     return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
269 }
270
271 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX *libctx, EVP_PKEY *pkey,
272                                          const char *propquery)
273 {
274     return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
275 }
276
277 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
278 {
279     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
280         if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
281             ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
282         EVP_SIGNATURE_free(ctx->op.sig.signature);
283         ctx->op.sig.sigprovctx = NULL;
284         ctx->op.sig.signature = NULL;
285     } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
286         if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
287             ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
288         EVP_KEYEXCH_free(ctx->op.kex.exchange);
289         ctx->op.kex.exchprovctx = NULL;
290         ctx->op.kex.exchange = NULL;
291     }
292 /* TODO(3.0): add dependancies and uncomment this when available for fips mode */
293 #ifndef FIPS_MODE
294     else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
295         if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
296             ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
297         EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
298         ctx->op.ciph.ciphprovctx = NULL;
299         ctx->op.ciph.cipher = NULL;
300     } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
301         if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
302             evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
303     }
304 #endif
305 }
306
307 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
308 {
309     if (ctx == NULL)
310         return;
311     if (ctx->pmeth && ctx->pmeth->cleanup)
312         ctx->pmeth->cleanup(ctx);
313
314     evp_pkey_ctx_free_old_ops(ctx);
315     EVP_KEYMGMT_free(ctx->keymgmt);
316
317     EVP_PKEY_free(ctx->pkey);
318     EVP_PKEY_free(ctx->peerkey);
319 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
320     ENGINE_finish(ctx->engine);
321 #endif
322     OPENSSL_free(ctx);
323 }
324
325 #ifndef FIPS_MODE
326
327 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
328                              const EVP_PKEY_METHOD *meth)
329 {
330     if (ppkey_id)
331         *ppkey_id = meth->pkey_id;
332     if (pflags)
333         *pflags = meth->flags;
334 }
335
336 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
337 {
338
339     dst->init = src->init;
340     dst->copy = src->copy;
341     dst->cleanup = src->cleanup;
342
343     dst->paramgen_init = src->paramgen_init;
344     dst->paramgen = src->paramgen;
345
346     dst->keygen_init = src->keygen_init;
347     dst->keygen = src->keygen;
348
349     dst->sign_init = src->sign_init;
350     dst->sign = src->sign;
351
352     dst->verify_init = src->verify_init;
353     dst->verify = src->verify;
354
355     dst->verify_recover_init = src->verify_recover_init;
356     dst->verify_recover = src->verify_recover;
357
358     dst->signctx_init = src->signctx_init;
359     dst->signctx = src->signctx;
360
361     dst->verifyctx_init = src->verifyctx_init;
362     dst->verifyctx = src->verifyctx;
363
364     dst->encrypt_init = src->encrypt_init;
365     dst->encrypt = src->encrypt;
366
367     dst->decrypt_init = src->decrypt_init;
368     dst->decrypt = src->decrypt;
369
370     dst->derive_init = src->derive_init;
371     dst->derive = src->derive;
372
373     dst->ctrl = src->ctrl;
374     dst->ctrl_str = src->ctrl_str;
375
376     dst->check = src->check;
377 }
378
379 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
380 {
381     if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
382         OPENSSL_free(pmeth);
383 }
384
385 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
386 {
387     return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
388 }
389
390 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
391 {
392     return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
393 }
394
395 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
396 {
397     EVP_PKEY_CTX *rctx;
398
399     if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
400             && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
401                  && pctx->op.kex.exchprovctx == NULL)
402                 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
403                     && pctx->op.sig.sigprovctx == NULL)))
404         return NULL;
405 # ifndef OPENSSL_NO_ENGINE
406     /* Make sure it's safe to copy a pkey context using an ENGINE */
407     if (pctx->engine && !ENGINE_init(pctx->engine)) {
408         EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
409         return 0;
410     }
411 # endif
412     rctx = OPENSSL_zalloc(sizeof(*rctx));
413     if (rctx == NULL) {
414         EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
415         return NULL;
416     }
417
418     if (pctx->pkey != NULL)
419         EVP_PKEY_up_ref(pctx->pkey);
420     rctx->pkey = pctx->pkey;
421     rctx->operation = pctx->operation;
422     rctx->libctx = pctx->libctx;
423     rctx->keytype = pctx->keytype;
424     rctx->propquery = pctx->propquery;
425
426     if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
427         if (pctx->op.kex.exchange != NULL) {
428             rctx->op.kex.exchange = pctx->op.kex.exchange;
429             if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
430                 OPENSSL_free(rctx);
431                 return NULL;
432             }
433         }
434         if (pctx->op.kex.exchprovctx != NULL) {
435             if (!ossl_assert(pctx->op.kex.exchange != NULL))
436                 return NULL;
437             rctx->op.kex.exchprovctx
438                 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
439             if (rctx->op.kex.exchprovctx == NULL) {
440                 EVP_KEYEXCH_free(rctx->op.kex.exchange);
441                 OPENSSL_free(rctx);
442                 return NULL;
443             }
444             return rctx;
445         }
446     } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
447         if (pctx->op.sig.signature != NULL) {
448             rctx->op.sig.signature = pctx->op.sig.signature;
449             if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
450                 OPENSSL_free(rctx);
451                 return NULL;
452             }
453         }
454         if (pctx->op.sig.sigprovctx != NULL) {
455             if (!ossl_assert(pctx->op.sig.signature != NULL))
456                 return NULL;
457             rctx->op.sig.sigprovctx
458                 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
459             if (rctx->op.sig.sigprovctx == NULL) {
460                 EVP_SIGNATURE_free(rctx->op.sig.signature);
461                 OPENSSL_free(rctx);
462                 return NULL;
463             }
464             return rctx;
465         }
466     } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
467         if (pctx->op.ciph.cipher != NULL) {
468             rctx->op.ciph.cipher = pctx->op.ciph.cipher;
469             if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
470                 OPENSSL_free(rctx);
471                 return NULL;
472             }
473         }
474         if (pctx->op.ciph.ciphprovctx != NULL) {
475             if (!ossl_assert(pctx->op.ciph.cipher != NULL))
476                 return NULL;
477             rctx->op.ciph.ciphprovctx
478                 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
479             if (rctx->op.ciph.ciphprovctx == NULL) {
480                 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
481                 OPENSSL_free(rctx);
482                 return NULL;
483             }
484             return rctx;
485         }
486     }
487
488     rctx->pmeth = pctx->pmeth;
489 # ifndef OPENSSL_NO_ENGINE
490     rctx->engine = pctx->engine;
491 # endif
492
493     if (pctx->peerkey)
494         EVP_PKEY_up_ref(pctx->peerkey);
495     rctx->peerkey = pctx->peerkey;
496
497     if (pctx->pmeth->copy(rctx, pctx) > 0)
498         return rctx;
499
500     rctx->pmeth = NULL;
501     EVP_PKEY_CTX_free(rctx);
502     return NULL;
503
504 }
505
506 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
507 {
508     if (app_pkey_methods == NULL) {
509         app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
510         if (app_pkey_methods == NULL){
511             EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
512             return 0;
513         }
514     }
515     if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
516         EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
517         return 0;
518     }
519     sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
520     return 1;
521 }
522
523 void evp_app_cleanup_int(void)
524 {
525     if (app_pkey_methods != NULL)
526         sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
527 }
528
529 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
530 {
531     const EVP_PKEY_METHOD *ret;
532
533     ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
534
535     return ret == NULL ? 0 : 1;
536 }
537
538 size_t EVP_PKEY_meth_get_count(void)
539 {
540     size_t rv = OSSL_NELEM(standard_methods);
541
542     if (app_pkey_methods)
543         rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
544     return rv;
545 }
546
547 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
548 {
549     if (idx < OSSL_NELEM(standard_methods))
550         return (standard_methods[idx])();
551     if (app_pkey_methods == NULL)
552         return NULL;
553     idx -= OSSL_NELEM(standard_methods);
554     if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
555         return NULL;
556     return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
557 }
558 #endif
559
560 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
561 {
562     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
563             && ctx->op.kex.exchprovctx != NULL
564             && ctx->op.kex.exchange != NULL
565             && ctx->op.kex.exchange->set_ctx_params != NULL)
566         return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
567                                                     params);
568     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
569             && ctx->op.sig.sigprovctx != NULL
570             && ctx->op.sig.signature != NULL
571             && ctx->op.sig.signature->set_ctx_params != NULL)
572         return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
573                                                      params);
574     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
575             && ctx->op.ciph.ciphprovctx != NULL
576             && ctx->op.ciph.cipher != NULL
577             && ctx->op.ciph.cipher->set_ctx_params != NULL)
578         return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
579                                                      params);
580     if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
581         && ctx->op.keymgmt.genctx != NULL
582         && ctx->keymgmt != NULL
583         && ctx->keymgmt->gen_set_params != NULL)
584         return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
585                                           params);
586     return 0;
587 }
588
589 #ifndef FIPS_MODE
590 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
591 {
592     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
593             && ctx->op.kex.exchprovctx != NULL
594             && ctx->op.kex.exchange != NULL
595             && ctx->op.kex.exchange->get_ctx_params != NULL)
596         return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
597                                                     params);
598     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
599             && ctx->op.sig.sigprovctx != NULL
600             && ctx->op.sig.signature != NULL
601             && ctx->op.sig.signature->get_ctx_params != NULL)
602         return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
603                                                      params);
604     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
605             && ctx->op.ciph.ciphprovctx != NULL
606             && ctx->op.ciph.cipher != NULL
607             && ctx->op.ciph.cipher->get_ctx_params != NULL)
608         return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
609                                                    params);
610     return 0;
611 }
612
613 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
614 {
615     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
616             && ctx->op.kex.exchange != NULL
617             && ctx->op.kex.exchange->gettable_ctx_params != NULL)
618         return ctx->op.kex.exchange->gettable_ctx_params();
619     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
620             && ctx->op.sig.signature != NULL
621             && ctx->op.sig.signature->gettable_ctx_params != NULL)
622         return ctx->op.sig.signature->gettable_ctx_params();
623
624     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
625             && ctx->op.ciph.cipher != NULL
626             && ctx->op.ciph.cipher->gettable_ctx_params != NULL)
627         return ctx->op.ciph.cipher->gettable_ctx_params();
628
629     return NULL;
630 }
631
632 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
633 {
634     if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
635             && ctx->op.kex.exchange != NULL
636             && ctx->op.kex.exchange->settable_ctx_params != NULL)
637         return ctx->op.kex.exchange->settable_ctx_params();
638     if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
639             && ctx->op.sig.signature != NULL
640             && ctx->op.sig.signature->settable_ctx_params != NULL)
641         return ctx->op.sig.signature->settable_ctx_params();
642     if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
643             && ctx->op.ciph.cipher != NULL
644             && ctx->op.ciph.cipher->settable_ctx_params != NULL)
645         return ctx->op.ciph.cipher->settable_ctx_params();
646     if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
647             && ctx->keymgmt != NULL
648             && ctx->keymgmt->gen_settable_params != NULL)
649         return evp_keymgmt_gen_settable_params(ctx->keymgmt);
650
651     return NULL;
652 }
653
654 /*
655  * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
656  *
657  * Return 1 on success, 0 or negative for errors.
658  *
659  * In particular they return -2 if any of the params is not supported.
660  *
661  * They are not available in FIPS_MODE as they depend on
662  *      - EVP_PKEY_CTX_{get,set}_params()
663  *      - EVP_PKEY_CTX_{gettable,settable}_params()
664  *
665  */
666 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
667 {
668     const OSSL_PARAM *p;
669
670     if (ctx == NULL || params == NULL)
671         return 0;
672
673     for (p = params; p->key != NULL; p++) {
674         /* Check the ctx actually understands this parameter */
675         if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
676                                     p->key) == NULL )
677             return -2;
678     }
679
680     return EVP_PKEY_CTX_set_params(ctx, params);
681 }
682
683 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
684 {
685     const OSSL_PARAM *p;
686
687     if (ctx == NULL || params == NULL)
688         return 0;
689
690     for (p = params; p->key != NULL; p++ ) {
691         /* Check the ctx actually understands this parameter */
692         if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
693                                     p->key) == NULL )
694             return -2;
695     }
696
697     return EVP_PKEY_CTX_get_params(ctx, params);
698 }
699
700 # ifndef OPENSSL_NO_DH
701 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
702 {
703     OSSL_PARAM dh_pad_params[2];
704     unsigned int upad = pad;
705
706     /* We use EVP_PKEY_CTX_ctrl return values */
707     if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
708         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
709         return -2;
710     }
711
712     /* TODO(3.0): Remove this eventually when no more legacy */
713     if (ctx->op.kex.exchprovctx == NULL)
714         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
715                                  EVP_PKEY_CTRL_DH_PAD, pad, NULL);
716
717     dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
718     dh_pad_params[1] = OSSL_PARAM_construct_end();
719
720     return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
721 }
722 # endif
723
724 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
725 {
726     OSSL_PARAM sig_md_params[3], *p = sig_md_params;
727     /* 80 should be big enough */
728     char name[80] = "";
729     const EVP_MD *tmp;
730
731     if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
732         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
733         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
734         return -2;
735     }
736
737     /* TODO(3.0): Remove this eventually when no more legacy */
738     if (ctx->op.sig.sigprovctx == NULL)
739         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
740                                  EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
741
742     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
743                                             name,
744                                             sizeof(name));
745     *p++ = OSSL_PARAM_construct_end();
746
747     if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
748         return 0;
749
750     tmp = evp_get_digestbyname_ex(ctx->libctx, name);
751     if (tmp == NULL)
752         return 0;
753
754     *md = tmp;
755
756     return 1;
757 }
758
759 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
760 {
761     OSSL_PARAM sig_md_params[2], *p = sig_md_params;
762     const char *name;
763
764     if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
765         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
766         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
767         return -2;
768     }
769
770     /* TODO(3.0): Remove this eventually when no more legacy */
771     if (ctx->op.sig.sigprovctx == NULL)
772         return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
773                                  EVP_PKEY_CTRL_MD, 0, (void *)(md));
774
775     if (md == NULL) {
776         name = "";
777     } else {
778         name = EVP_MD_name(md);
779     }
780
781     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
782                                             /*
783                                              * Cast away the const. This is read
784                                              * only so should be safe
785                                              */
786                                             (char *)name, 0);
787     *p++ = OSSL_PARAM_construct_end();
788
789     return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
790 }
791
792 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
793                                 int cmd, int p1, void *p2)
794 {
795     /*
796      * GOST CMS format is different for different cipher algorithms.
797      * Most of other algorithms don't have such a difference
798      * so this ctrl is just ignored.
799      */
800     if (cmd == EVP_PKEY_CTRL_CIPHER)
801         return -2;
802 # ifndef OPENSSL_NO_DH
803     if (keytype == EVP_PKEY_DH) {
804         switch (cmd) {
805             case EVP_PKEY_CTRL_DH_PAD:
806                 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
807         }
808     }
809 # endif
810 # ifndef OPENSSL_NO_EC
811     if (keytype == EVP_PKEY_EC) {
812         switch (cmd) {
813         case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
814             if (p1 == -2) {
815                 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
816             } else if (p1 < -1 || p1 > 1) {
817                 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
818                 return -2;
819             } else {
820                 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
821             }
822         case EVP_PKEY_CTRL_EC_KDF_TYPE:
823             if (p1 == -2) {
824                 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
825             } else {
826                 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
827             }
828         case EVP_PKEY_CTRL_GET_EC_KDF_MD:
829             return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
830         case EVP_PKEY_CTRL_EC_KDF_MD:
831             return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
832         case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
833             return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
834         case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
835             return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
836         case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
837             return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
838         case EVP_PKEY_CTRL_EC_KDF_UKM:
839             return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
840         }
841     }
842 # endif
843     if (keytype == EVP_PKEY_RSA) {
844         switch (cmd) {
845         case EVP_PKEY_CTRL_RSA_OAEP_MD:
846             return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
847         case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
848             return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
849         case EVP_PKEY_CTRL_RSA_MGF1_MD:
850             return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
851         case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
852             return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
853         case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
854             return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
855         case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
856             return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
857         case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
858             return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
859         case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
860             return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
861         }
862     }
863     /*
864      * keytype == -1 is used when several key types share the same structure,
865      * or for generic controls that are the same across multiple key types.
866      */
867     if (keytype == -1) {
868         switch (cmd) {
869         case EVP_PKEY_CTRL_MD:
870             return EVP_PKEY_CTX_set_signature_md(ctx, p2);
871         case EVP_PKEY_CTRL_GET_MD:
872             return EVP_PKEY_CTX_get_signature_md(ctx, p2);
873         case EVP_PKEY_CTRL_RSA_PADDING:
874             return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
875         case EVP_PKEY_CTRL_GET_RSA_PADDING:
876             return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
877         case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
878             return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
879         case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
880             return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
881         case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
882             return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2);
883         case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
884         case EVP_PKEY_CTRL_PKCS7_DECRYPT:
885 # ifndef OPENSSL_NO_CMS
886         case EVP_PKEY_CTRL_CMS_DECRYPT:
887         case EVP_PKEY_CTRL_CMS_ENCRYPT:
888 # endif
889             if (ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
890                 return 1;
891             ERR_raise(ERR_LIB_EVP,
892                       EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
893             return -2;
894         }
895     }
896     return 0;
897 }
898
899 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
900                       int cmd, int p1, void *p2)
901 {
902     int ret;
903
904     if (ctx == NULL) {
905         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
906         return -2;
907     }
908
909     if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
910             || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
911                 && ctx->op.sig.sigprovctx != NULL)
912             || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
913                 && ctx->op.ciph.ciphprovctx != NULL)
914             || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
915                 && ctx->op.keymgmt.genctx != NULL))
916         return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
917
918     if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
919         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
920         return -2;
921     }
922     if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
923         return -1;
924
925     /* Skip the operation checks since this is called in a very early stage */
926     if (ctx->pmeth->digest_custom != NULL)
927         goto doit;
928
929     if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
930         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
931         return -1;
932     }
933
934     if ((optype != -1) && !(ctx->operation & optype)) {
935         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
936         return -1;
937     }
938
939  doit:
940     ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
941
942     if (ret == -2)
943         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
944
945     return ret;
946 }
947
948 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
949                              int cmd, uint64_t value)
950 {
951     return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
952 }
953
954 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
955                                     const char *value)
956 {
957     if (strcmp(name, "rsa_padding_mode") == 0)
958         name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
959     else if (strcmp(name, "rsa_mgf1_md") == 0)
960         name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
961     else if (strcmp(name, "rsa_oaep_md") == 0)
962         name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
963     else if (strcmp(name, "rsa_oaep_label") == 0)
964         name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
965     else if (strcmp(name, "rsa_pss_saltlen") == 0)
966         name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
967     else if (strcmp(name, "rsa_keygen_bits") == 0)
968         name = OSSL_PKEY_PARAM_RSA_BITS;
969     else if (strcmp(name, "rsa_keygen_pubexp") == 0)
970         name = OSSL_PKEY_PARAM_RSA_E;
971     else if (strcmp(name, "rsa_keygen_primes") == 0)
972         name = OSSL_PKEY_PARAM_RSA_PRIMES;
973 # ifndef OPENSSL_NO_DH
974     else if (strcmp(name, "dh_pad") == 0)
975         name = OSSL_EXCHANGE_PARAM_PAD;
976 # endif
977 # ifndef OPENSSL_NO_EC
978     else if (strcmp(name, "ecdh_cofactor_mode") == 0)
979         name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
980     else if (strcmp(name, "ecdh_kdf_md") == 0)
981         name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
982 # endif
983
984     {
985         /*
986          * TODO(3.0) reduce the code above to only translate known legacy
987          * string to the corresponding core name (see core_names.h), but
988          * otherwise leave it to this code block to do the actual work.
989          */
990         const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
991         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
992         int rv = 0;
993         int exists = 0;
994
995         if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
996                                            strlen(value), &exists)) {
997             if (!exists) {
998                 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
999                 return -2;
1000             }
1001             return 0;
1002         }
1003         if (EVP_PKEY_CTX_set_params(ctx, params))
1004             rv = 1;
1005         OPENSSL_free(params[0].data);
1006         return rv;
1007     }
1008 }
1009
1010 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1011                           const char *name, const char *value)
1012 {
1013     if (ctx == NULL) {
1014         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1015         return -2;
1016     }
1017
1018     if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
1019             || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1020                 && ctx->op.sig.sigprovctx != NULL)
1021             || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1022                 && ctx->op.ciph.ciphprovctx != NULL)
1023             || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1024                 && ctx->op.keymgmt.genctx != NULL))
1025         return legacy_ctrl_str_to_param(ctx, name, value);
1026
1027     if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
1028         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1029         return -2;
1030     }
1031     if (strcmp(name, "digest") == 0)
1032         return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
1033                                value);
1034     return ctx->pmeth->ctrl_str(ctx, name, value);
1035 }
1036
1037 /* Utility functions to send a string of hex string to a ctrl */
1038
1039 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1040 {
1041     size_t len;
1042
1043     len = strlen(str);
1044     if (len > INT_MAX)
1045         return -1;
1046     return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1047 }
1048
1049 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1050 {
1051     unsigned char *bin;
1052     long binlen;
1053     int rv = -1;
1054
1055     bin = OPENSSL_hexstr2buf(hex, &binlen);
1056     if (bin == NULL)
1057         return 0;
1058     if (binlen <= INT_MAX)
1059         rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1060     OPENSSL_free(bin);
1061     return rv;
1062 }
1063
1064 /* Pass a message digest to a ctrl */
1065 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1066 {
1067     const EVP_MD *m;
1068
1069     if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1070         EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
1071         return 0;
1072     }
1073     return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1074 }
1075
1076 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1077 {
1078     return ctx->operation;
1079 }
1080
1081 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1082 {
1083     ctx->keygen_info = dat;
1084     ctx->keygen_info_count = datlen;
1085 }
1086
1087 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1088 {
1089     ctx->data = data;
1090 }
1091
1092 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1093 {
1094     return ctx->data;
1095 }
1096
1097 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1098 {
1099     return ctx->pkey;
1100 }
1101
1102 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1103 {
1104     return ctx->peerkey;
1105 }
1106
1107 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1108 {
1109     ctx->app_data = data;
1110 }
1111
1112 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1113 {
1114     return ctx->app_data;
1115 }
1116
1117 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1118                             int (*init) (EVP_PKEY_CTX *ctx))
1119 {
1120     pmeth->init = init;
1121 }
1122
1123 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1124                             int (*copy) (EVP_PKEY_CTX *dst,
1125                                          const EVP_PKEY_CTX *src))
1126 {
1127     pmeth->copy = copy;
1128 }
1129
1130 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1131                                void (*cleanup) (EVP_PKEY_CTX *ctx))
1132 {
1133     pmeth->cleanup = cleanup;
1134 }
1135
1136 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1137                                 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1138                                 int (*paramgen) (EVP_PKEY_CTX *ctx,
1139                                                  EVP_PKEY *pkey))
1140 {
1141     pmeth->paramgen_init = paramgen_init;
1142     pmeth->paramgen = paramgen;
1143 }
1144
1145 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1146                               int (*keygen_init) (EVP_PKEY_CTX *ctx),
1147                               int (*keygen) (EVP_PKEY_CTX *ctx,
1148                                              EVP_PKEY *pkey))
1149 {
1150     pmeth->keygen_init = keygen_init;
1151     pmeth->keygen = keygen;
1152 }
1153
1154 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1155                             int (*sign_init) (EVP_PKEY_CTX *ctx),
1156                             int (*sign) (EVP_PKEY_CTX *ctx,
1157                                          unsigned char *sig, size_t *siglen,
1158                                          const unsigned char *tbs,
1159                                          size_t tbslen))
1160 {
1161     pmeth->sign_init = sign_init;
1162     pmeth->sign = sign;
1163 }
1164
1165 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1166                               int (*verify_init) (EVP_PKEY_CTX *ctx),
1167                               int (*verify) (EVP_PKEY_CTX *ctx,
1168                                              const unsigned char *sig,
1169                                              size_t siglen,
1170                                              const unsigned char *tbs,
1171                                              size_t tbslen))
1172 {
1173     pmeth->verify_init = verify_init;
1174     pmeth->verify = verify;
1175 }
1176
1177 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1178                                       int (*verify_recover_init) (EVP_PKEY_CTX
1179                                                                   *ctx),
1180                                       int (*verify_recover) (EVP_PKEY_CTX
1181                                                              *ctx,
1182                                                              unsigned char
1183                                                              *sig,
1184                                                              size_t *siglen,
1185                                                              const unsigned
1186                                                              char *tbs,
1187                                                              size_t tbslen))
1188 {
1189     pmeth->verify_recover_init = verify_recover_init;
1190     pmeth->verify_recover = verify_recover;
1191 }
1192
1193 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1194                                int (*signctx_init) (EVP_PKEY_CTX *ctx,
1195                                                     EVP_MD_CTX *mctx),
1196                                int (*signctx) (EVP_PKEY_CTX *ctx,
1197                                                unsigned char *sig,
1198                                                size_t *siglen,
1199                                                EVP_MD_CTX *mctx))
1200 {
1201     pmeth->signctx_init = signctx_init;
1202     pmeth->signctx = signctx;
1203 }
1204
1205 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1206                                  int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1207                                                         EVP_MD_CTX *mctx),
1208                                  int (*verifyctx) (EVP_PKEY_CTX *ctx,
1209                                                    const unsigned char *sig,
1210                                                    int siglen,
1211                                                    EVP_MD_CTX *mctx))
1212 {
1213     pmeth->verifyctx_init = verifyctx_init;
1214     pmeth->verifyctx = verifyctx;
1215 }
1216
1217 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1218                                int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1219                                int (*encryptfn) (EVP_PKEY_CTX *ctx,
1220                                                  unsigned char *out,
1221                                                  size_t *outlen,
1222                                                  const unsigned char *in,
1223                                                  size_t inlen))
1224 {
1225     pmeth->encrypt_init = encrypt_init;
1226     pmeth->encrypt = encryptfn;
1227 }
1228
1229 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1230                                int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1231                                int (*decrypt) (EVP_PKEY_CTX *ctx,
1232                                                unsigned char *out,
1233                                                size_t *outlen,
1234                                                const unsigned char *in,
1235                                                size_t inlen))
1236 {
1237     pmeth->decrypt_init = decrypt_init;
1238     pmeth->decrypt = decrypt;
1239 }
1240
1241 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1242                               int (*derive_init) (EVP_PKEY_CTX *ctx),
1243                               int (*derive) (EVP_PKEY_CTX *ctx,
1244                                              unsigned char *key,
1245                                              size_t *keylen))
1246 {
1247     pmeth->derive_init = derive_init;
1248     pmeth->derive = derive;
1249 }
1250
1251 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1252                             int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1253                                          void *p2),
1254                             int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1255                                              const char *type,
1256                                              const char *value))
1257 {
1258     pmeth->ctrl = ctrl;
1259     pmeth->ctrl_str = ctrl_str;
1260 }
1261
1262 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1263     int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1264                        const unsigned char *tbs, size_t tbslen))
1265 {
1266     pmeth->digestsign = digestsign;
1267 }
1268
1269 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1270     int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1271                          size_t siglen, const unsigned char *tbs,
1272                          size_t tbslen))
1273 {
1274     pmeth->digestverify = digestverify;
1275 }
1276
1277 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1278                              int (*check) (EVP_PKEY *pkey))
1279 {
1280     pmeth->check = check;
1281 }
1282
1283 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1284                                     int (*check) (EVP_PKEY *pkey))
1285 {
1286     pmeth->public_check = check;
1287 }
1288
1289 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1290                                    int (*check) (EVP_PKEY *pkey))
1291 {
1292     pmeth->param_check = check;
1293 }
1294
1295 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1296                                      int (*digest_custom) (EVP_PKEY_CTX *ctx,
1297                                                            EVP_MD_CTX *mctx))
1298 {
1299     pmeth->digest_custom = digest_custom;
1300 }
1301
1302 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1303                             int (**pinit) (EVP_PKEY_CTX *ctx))
1304 {
1305     *pinit = pmeth->init;
1306 }
1307
1308 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1309                             int (**pcopy) (EVP_PKEY_CTX *dst,
1310                                            const EVP_PKEY_CTX *src))
1311 {
1312     *pcopy = pmeth->copy;
1313 }
1314
1315 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1316                                void (**pcleanup) (EVP_PKEY_CTX *ctx))
1317 {
1318     *pcleanup = pmeth->cleanup;
1319 }
1320
1321 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1322                                 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1323                                 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1324                                                    EVP_PKEY *pkey))
1325 {
1326     if (pparamgen_init)
1327         *pparamgen_init = pmeth->paramgen_init;
1328     if (pparamgen)
1329         *pparamgen = pmeth->paramgen;
1330 }
1331
1332 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1333                               int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1334                               int (**pkeygen) (EVP_PKEY_CTX *ctx,
1335                                                EVP_PKEY *pkey))
1336 {
1337     if (pkeygen_init)
1338         *pkeygen_init = pmeth->keygen_init;
1339     if (pkeygen)
1340         *pkeygen = pmeth->keygen;
1341 }
1342
1343 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1344                             int (**psign_init) (EVP_PKEY_CTX *ctx),
1345                             int (**psign) (EVP_PKEY_CTX *ctx,
1346                                            unsigned char *sig, size_t *siglen,
1347                                            const unsigned char *tbs,
1348                                            size_t tbslen))
1349 {
1350     if (psign_init)
1351         *psign_init = pmeth->sign_init;
1352     if (psign)
1353         *psign = pmeth->sign;
1354 }
1355
1356 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1357                               int (**pverify_init) (EVP_PKEY_CTX *ctx),
1358                               int (**pverify) (EVP_PKEY_CTX *ctx,
1359                                                const unsigned char *sig,
1360                                                size_t siglen,
1361                                                const unsigned char *tbs,
1362                                                size_t tbslen))
1363 {
1364     if (pverify_init)
1365         *pverify_init = pmeth->verify_init;
1366     if (pverify)
1367         *pverify = pmeth->verify;
1368 }
1369
1370 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1371                                       int (**pverify_recover_init) (EVP_PKEY_CTX
1372                                                                     *ctx),
1373                                       int (**pverify_recover) (EVP_PKEY_CTX
1374                                                                *ctx,
1375                                                                unsigned char
1376                                                                *sig,
1377                                                                size_t *siglen,
1378                                                                const unsigned
1379                                                                char *tbs,
1380                                                                size_t tbslen))
1381 {
1382     if (pverify_recover_init)
1383         *pverify_recover_init = pmeth->verify_recover_init;
1384     if (pverify_recover)
1385         *pverify_recover = pmeth->verify_recover;
1386 }
1387
1388 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1389                                int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1390                                                       EVP_MD_CTX *mctx),
1391                                int (**psignctx) (EVP_PKEY_CTX *ctx,
1392                                                  unsigned char *sig,
1393                                                  size_t *siglen,
1394                                                  EVP_MD_CTX *mctx))
1395 {
1396     if (psignctx_init)
1397         *psignctx_init = pmeth->signctx_init;
1398     if (psignctx)
1399         *psignctx = pmeth->signctx;
1400 }
1401
1402 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1403                                  int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1404                                                           EVP_MD_CTX *mctx),
1405                                  int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1406                                                      const unsigned char *sig,
1407                                                      int siglen,
1408                                                      EVP_MD_CTX *mctx))
1409 {
1410     if (pverifyctx_init)
1411         *pverifyctx_init = pmeth->verifyctx_init;
1412     if (pverifyctx)
1413         *pverifyctx = pmeth->verifyctx;
1414 }
1415
1416 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1417                                int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1418                                int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1419                                                    unsigned char *out,
1420                                                    size_t *outlen,
1421                                                    const unsigned char *in,
1422                                                    size_t inlen))
1423 {
1424     if (pencrypt_init)
1425         *pencrypt_init = pmeth->encrypt_init;
1426     if (pencryptfn)
1427         *pencryptfn = pmeth->encrypt;
1428 }
1429
1430 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1431                                int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1432                                int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1433                                                  unsigned char *out,
1434                                                  size_t *outlen,
1435                                                  const unsigned char *in,
1436                                                  size_t inlen))
1437 {
1438     if (pdecrypt_init)
1439         *pdecrypt_init = pmeth->decrypt_init;
1440     if (pdecrypt)
1441         *pdecrypt = pmeth->decrypt;
1442 }
1443
1444 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1445                               int (**pderive_init) (EVP_PKEY_CTX *ctx),
1446                               int (**pderive) (EVP_PKEY_CTX *ctx,
1447                                                unsigned char *key,
1448                                                size_t *keylen))
1449 {
1450     if (pderive_init)
1451         *pderive_init = pmeth->derive_init;
1452     if (pderive)
1453         *pderive = pmeth->derive;
1454 }
1455
1456 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1457                             int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1458                                            void *p2),
1459                             int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1460                                                const char *type,
1461                                                const char *value))
1462 {
1463     if (pctrl)
1464         *pctrl = pmeth->ctrl;
1465     if (pctrl_str)
1466         *pctrl_str = pmeth->ctrl_str;
1467 }
1468
1469 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1470     int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1471                         const unsigned char *tbs, size_t tbslen))
1472 {
1473     if (digestsign)
1474         *digestsign = pmeth->digestsign;
1475 }
1476
1477 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1478     int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1479                           size_t siglen, const unsigned char *tbs,
1480                           size_t tbslen))
1481 {
1482     if (digestverify)
1483         *digestverify = pmeth->digestverify;
1484 }
1485
1486 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1487                              int (**pcheck) (EVP_PKEY *pkey))
1488 {
1489     if (pcheck != NULL)
1490         *pcheck = pmeth->check;
1491 }
1492
1493 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1494                                     int (**pcheck) (EVP_PKEY *pkey))
1495 {
1496     if (pcheck != NULL)
1497         *pcheck = pmeth->public_check;
1498 }
1499
1500 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1501                                    int (**pcheck) (EVP_PKEY *pkey))
1502 {
1503     if (pcheck != NULL)
1504         *pcheck = pmeth->param_check;
1505 }
1506
1507 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1508                                      int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1509                                                              EVP_MD_CTX *mctx))
1510 {
1511     if (pdigest_custom != NULL)
1512         *pdigest_custom = pmeth->digest_custom;
1513 }
1514
1515 #endif /* FIPS_MODE */