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