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