35e525b6ed04eb349c008e368be8babb938924c0
[openssl.git] / crypto / evp / pmeth_lib.c
1 /*
2  * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include "internal/asn1_int.h"
17 #include "internal/evp_int.h"
18 #include "internal/numbers.h"
19
20 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
21
22 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
23
24 /* This array needs to be in order of NIDs */
25 static const EVP_PKEY_METHOD *standard_methods[] = {
26 #ifndef OPENSSL_NO_RSA
27     &rsa_pkey_meth,
28 #endif
29 #ifndef OPENSSL_NO_DH
30     &dh_pkey_meth,
31 #endif
32 #ifndef OPENSSL_NO_DSA
33     &dsa_pkey_meth,
34 #endif
35 #ifndef OPENSSL_NO_EC
36     &ec_pkey_meth,
37 #endif
38     &hmac_pkey_meth,
39 #ifndef OPENSSL_NO_CMAC
40     &cmac_pkey_meth,
41 #endif
42 #ifndef OPENSSL_NO_RSA
43     &rsa_pss_pkey_meth,
44 #endif
45 #ifndef OPENSSL_NO_DH
46     &dhx_pkey_meth,
47 #endif
48 #ifndef OPENSSL_NO_SCRYPT
49     &scrypt_pkey_meth,
50 #endif
51     &tls1_prf_pkey_meth,
52 #ifndef OPENSSL_NO_EC
53     &ecx25519_pkey_meth,
54 #endif
55     &hkdf_pkey_meth,
56 #ifndef OPENSSL_NO_POLY1305
57     &poly1305_pkey_meth,
58 #endif
59 #ifndef OPENSSL_NO_SIPHASH
60     &siphash_pkey_meth,
61 #endif
62 #ifndef OPENSSL_NO_EC
63     &ed25519_pkey_meth,
64 #endif
65 };
66
67 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
68                            pmeth);
69
70 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
71                      const EVP_PKEY_METHOD *const *b)
72 {
73     return ((*a)->pkey_id - (*b)->pkey_id);
74 }
75
76 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
77                              pmeth);
78
79 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
80 {
81     EVP_PKEY_METHOD tmp;
82     const EVP_PKEY_METHOD *t = &tmp, **ret;
83     tmp.pkey_id = type;
84     if (app_pkey_methods) {
85         int idx;
86         idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
87         if (idx >= 0)
88             return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
89     }
90     ret = OBJ_bsearch_pmeth(&t, standard_methods,
91                             sizeof(standard_methods) /
92                             sizeof(EVP_PKEY_METHOD *));
93     if (!ret || !*ret)
94         return NULL;
95     return *ret;
96 }
97
98 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
99 {
100     EVP_PKEY_CTX *ret;
101     const EVP_PKEY_METHOD *pmeth;
102     if (id == -1) {
103         if (!pkey || !pkey->ameth)
104             return NULL;
105         id = pkey->ameth->pkey_id;
106     }
107 #ifndef OPENSSL_NO_ENGINE
108     if (e == NULL && pkey != NULL)
109         e = pkey->engine;
110     /* Try to find an ENGINE which implements this method */
111     if (e) {
112         if (!ENGINE_init(e)) {
113             EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
114             return NULL;
115         }
116     } else {
117         e = ENGINE_get_pkey_meth_engine(id);
118     }
119
120     /*
121      * If an ENGINE handled this method look it up. Otherwise use internal
122      * tables.
123      */
124
125     if (e)
126         pmeth = ENGINE_get_pkey_meth(e, id);
127     else
128 #endif
129         pmeth = EVP_PKEY_meth_find(id);
130
131     if (pmeth == NULL) {
132 #ifndef OPENSSL_NO_ENGINE
133         ENGINE_finish(e);
134 #endif
135         EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
136         return NULL;
137     }
138
139     ret = OPENSSL_zalloc(sizeof(*ret));
140     if (ret == NULL) {
141 #ifndef OPENSSL_NO_ENGINE
142         ENGINE_finish(e);
143 #endif
144         EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
145         return NULL;
146     }
147     ret->engine = e;
148     ret->pmeth = pmeth;
149     ret->operation = EVP_PKEY_OP_UNDEFINED;
150     ret->pkey = pkey;
151     if (pkey)
152         EVP_PKEY_up_ref(pkey);
153
154     if (pmeth->init) {
155         if (pmeth->init(ret) <= 0) {
156             ret->pmeth = NULL;
157             EVP_PKEY_CTX_free(ret);
158             return NULL;
159         }
160     }
161
162     return ret;
163 }
164
165 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
166 {
167     EVP_PKEY_METHOD *pmeth;
168
169     pmeth = OPENSSL_zalloc(sizeof(*pmeth));
170     if (pmeth == NULL)
171         return NULL;
172
173     pmeth->pkey_id = id;
174     pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
175     return pmeth;
176 }
177
178 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
179                              const EVP_PKEY_METHOD *meth)
180 {
181     if (ppkey_id)
182         *ppkey_id = meth->pkey_id;
183     if (pflags)
184         *pflags = meth->flags;
185 }
186
187 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
188 {
189
190     dst->init = src->init;
191     dst->copy = src->copy;
192     dst->cleanup = src->cleanup;
193
194     dst->paramgen_init = src->paramgen_init;
195     dst->paramgen = src->paramgen;
196
197     dst->keygen_init = src->keygen_init;
198     dst->keygen = src->keygen;
199
200     dst->sign_init = src->sign_init;
201     dst->sign = src->sign;
202
203     dst->verify_init = src->verify_init;
204     dst->verify = src->verify;
205
206     dst->verify_recover_init = src->verify_recover_init;
207     dst->verify_recover = src->verify_recover;
208
209     dst->signctx_init = src->signctx_init;
210     dst->signctx = src->signctx;
211
212     dst->verifyctx_init = src->verifyctx_init;
213     dst->verifyctx = src->verifyctx;
214
215     dst->encrypt_init = src->encrypt_init;
216     dst->encrypt = src->encrypt;
217
218     dst->decrypt_init = src->decrypt_init;
219     dst->decrypt = src->decrypt;
220
221     dst->derive_init = src->derive_init;
222     dst->derive = src->derive;
223
224     dst->ctrl = src->ctrl;
225     dst->ctrl_str = src->ctrl_str;
226
227     dst->check = src->check;
228 }
229
230 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
231 {
232     if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
233         OPENSSL_free(pmeth);
234 }
235
236 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
237 {
238     return int_ctx_new(pkey, e, -1);
239 }
240
241 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
242 {
243     return int_ctx_new(NULL, e, id);
244 }
245
246 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
247 {
248     EVP_PKEY_CTX *rctx;
249     if (!pctx->pmeth || !pctx->pmeth->copy)
250         return NULL;
251 #ifndef OPENSSL_NO_ENGINE
252     /* Make sure it's safe to copy a pkey context using an ENGINE */
253     if (pctx->engine && !ENGINE_init(pctx->engine)) {
254         EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
255         return 0;
256     }
257 #endif
258     rctx = OPENSSL_malloc(sizeof(*rctx));
259     if (rctx == NULL)
260         return NULL;
261
262     rctx->pmeth = pctx->pmeth;
263 #ifndef OPENSSL_NO_ENGINE
264     rctx->engine = pctx->engine;
265 #endif
266
267     if (pctx->pkey)
268         EVP_PKEY_up_ref(pctx->pkey);
269
270     rctx->pkey = pctx->pkey;
271
272     if (pctx->peerkey)
273         EVP_PKEY_up_ref(pctx->peerkey);
274
275     rctx->peerkey = pctx->peerkey;
276
277     rctx->data = NULL;
278     rctx->app_data = NULL;
279     rctx->operation = pctx->operation;
280
281     if (pctx->pmeth->copy(rctx, pctx) > 0)
282         return rctx;
283
284     rctx->pmeth = NULL;
285     EVP_PKEY_CTX_free(rctx);
286     return NULL;
287
288 }
289
290 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
291 {
292     if (app_pkey_methods == NULL) {
293         app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
294         if (app_pkey_methods == NULL)
295             return 0;
296     }
297     if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
298         return 0;
299     sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
300     return 1;
301 }
302
303 void evp_app_cleanup_int(void)
304 {
305     if (app_pkey_methods != NULL)
306         sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
307 }
308
309 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
310 {
311     const EVP_PKEY_METHOD *ret;
312
313     ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
314
315     return ret == NULL ? 0 : 1;
316 }
317
318 size_t EVP_PKEY_meth_get_count(void)
319 {
320     size_t rv = OSSL_NELEM(standard_methods);
321
322     if (app_pkey_methods)
323         rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
324     return rv;
325 }
326
327 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
328 {
329     if (idx < OSSL_NELEM(standard_methods))
330         return standard_methods[idx];
331     if (app_pkey_methods == NULL)
332         return NULL;
333     idx -= OSSL_NELEM(standard_methods);
334     if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
335         return NULL;
336     return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
337 }
338
339 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
340 {
341     if (ctx == NULL)
342         return;
343     if (ctx->pmeth && ctx->pmeth->cleanup)
344         ctx->pmeth->cleanup(ctx);
345     EVP_PKEY_free(ctx->pkey);
346     EVP_PKEY_free(ctx->peerkey);
347 #ifndef OPENSSL_NO_ENGINE
348     ENGINE_finish(ctx->engine);
349 #endif
350     OPENSSL_free(ctx);
351 }
352
353 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
354                       int cmd, int p1, void *p2)
355 {
356     int ret;
357     if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
358         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
359         return -2;
360     }
361     if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
362         return -1;
363
364     if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
365         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
366         return -1;
367     }
368
369     if ((optype != -1) && !(ctx->operation & optype)) {
370         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
371         return -1;
372     }
373
374     ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
375
376     if (ret == -2)
377         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
378
379     return ret;
380
381 }
382
383 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
384                              int cmd, uint64_t value)
385 {
386     return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
387 }
388
389 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
390                           const char *name, const char *value)
391 {
392     if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
393         EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
394         return -2;
395     }
396     if (strcmp(name, "digest") == 0)
397         return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
398                                value);
399     return ctx->pmeth->ctrl_str(ctx, name, value);
400 }
401
402 /* Utility functions to send a string of hex string to a ctrl */
403
404 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
405 {
406     size_t len;
407
408     len = strlen(str);
409     if (len > INT_MAX)
410         return -1;
411     return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
412 }
413
414 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
415 {
416     unsigned char *bin;
417     long binlen;
418     int rv = -1;
419
420     bin = OPENSSL_hexstr2buf(hex, &binlen);
421     if (bin == NULL)
422         return 0;
423     if (binlen <= INT_MAX)
424         rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
425     OPENSSL_free(bin);
426     return rv;
427 }
428
429 /* Pass a message digest to a ctrl */
430 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
431 {
432     const EVP_MD *m;
433
434     if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
435         EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
436         return 0;
437     }
438     return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
439 }
440
441 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
442 {
443     return ctx->operation;
444 }
445
446 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
447 {
448     ctx->keygen_info = dat;
449     ctx->keygen_info_count = datlen;
450 }
451
452 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
453 {
454     ctx->data = data;
455 }
456
457 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
458 {
459     return ctx->data;
460 }
461
462 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
463 {
464     return ctx->pkey;
465 }
466
467 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
468 {
469     return ctx->peerkey;
470 }
471
472 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
473 {
474     ctx->app_data = data;
475 }
476
477 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
478 {
479     return ctx->app_data;
480 }
481
482 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
483                             int (*init) (EVP_PKEY_CTX *ctx))
484 {
485     pmeth->init = init;
486 }
487
488 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
489                             int (*copy) (EVP_PKEY_CTX *dst,
490                                          EVP_PKEY_CTX *src))
491 {
492     pmeth->copy = copy;
493 }
494
495 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
496                                void (*cleanup) (EVP_PKEY_CTX *ctx))
497 {
498     pmeth->cleanup = cleanup;
499 }
500
501 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
502                                 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
503                                 int (*paramgen) (EVP_PKEY_CTX *ctx,
504                                                  EVP_PKEY *pkey))
505 {
506     pmeth->paramgen_init = paramgen_init;
507     pmeth->paramgen = paramgen;
508 }
509
510 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
511                               int (*keygen_init) (EVP_PKEY_CTX *ctx),
512                               int (*keygen) (EVP_PKEY_CTX *ctx,
513                                              EVP_PKEY *pkey))
514 {
515     pmeth->keygen_init = keygen_init;
516     pmeth->keygen = keygen;
517 }
518
519 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
520                             int (*sign_init) (EVP_PKEY_CTX *ctx),
521                             int (*sign) (EVP_PKEY_CTX *ctx,
522                                          unsigned char *sig, size_t *siglen,
523                                          const unsigned char *tbs,
524                                          size_t tbslen))
525 {
526     pmeth->sign_init = sign_init;
527     pmeth->sign = sign;
528 }
529
530 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
531                               int (*verify_init) (EVP_PKEY_CTX *ctx),
532                               int (*verify) (EVP_PKEY_CTX *ctx,
533                                              const unsigned char *sig,
534                                              size_t siglen,
535                                              const unsigned char *tbs,
536                                              size_t tbslen))
537 {
538     pmeth->verify_init = verify_init;
539     pmeth->verify = verify;
540 }
541
542 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
543                                       int (*verify_recover_init) (EVP_PKEY_CTX
544                                                                   *ctx),
545                                       int (*verify_recover) (EVP_PKEY_CTX
546                                                              *ctx,
547                                                              unsigned char
548                                                              *sig,
549                                                              size_t *siglen,
550                                                              const unsigned
551                                                              char *tbs,
552                                                              size_t tbslen))
553 {
554     pmeth->verify_recover_init = verify_recover_init;
555     pmeth->verify_recover = verify_recover;
556 }
557
558 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
559                                int (*signctx_init) (EVP_PKEY_CTX *ctx,
560                                                     EVP_MD_CTX *mctx),
561                                int (*signctx) (EVP_PKEY_CTX *ctx,
562                                                unsigned char *sig,
563                                                size_t *siglen,
564                                                EVP_MD_CTX *mctx))
565 {
566     pmeth->signctx_init = signctx_init;
567     pmeth->signctx = signctx;
568 }
569
570 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
571                                  int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
572                                                         EVP_MD_CTX *mctx),
573                                  int (*verifyctx) (EVP_PKEY_CTX *ctx,
574                                                    const unsigned char *sig,
575                                                    int siglen,
576                                                    EVP_MD_CTX *mctx))
577 {
578     pmeth->verifyctx_init = verifyctx_init;
579     pmeth->verifyctx = verifyctx;
580 }
581
582 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
583                                int (*encrypt_init) (EVP_PKEY_CTX *ctx),
584                                int (*encryptfn) (EVP_PKEY_CTX *ctx,
585                                                  unsigned char *out,
586                                                  size_t *outlen,
587                                                  const unsigned char *in,
588                                                  size_t inlen))
589 {
590     pmeth->encrypt_init = encrypt_init;
591     pmeth->encrypt = encryptfn;
592 }
593
594 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
595                                int (*decrypt_init) (EVP_PKEY_CTX *ctx),
596                                int (*decrypt) (EVP_PKEY_CTX *ctx,
597                                                unsigned char *out,
598                                                size_t *outlen,
599                                                const unsigned char *in,
600                                                size_t inlen))
601 {
602     pmeth->decrypt_init = decrypt_init;
603     pmeth->decrypt = decrypt;
604 }
605
606 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
607                               int (*derive_init) (EVP_PKEY_CTX *ctx),
608                               int (*derive) (EVP_PKEY_CTX *ctx,
609                                              unsigned char *key,
610                                              size_t *keylen))
611 {
612     pmeth->derive_init = derive_init;
613     pmeth->derive = derive;
614 }
615
616 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
617                             int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
618                                          void *p2),
619                             int (*ctrl_str) (EVP_PKEY_CTX *ctx,
620                                              const char *type,
621                                              const char *value))
622 {
623     pmeth->ctrl = ctrl;
624     pmeth->ctrl_str = ctrl_str;
625 }
626
627 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
628                              int (*check) (EVP_PKEY *pkey))
629 {
630     pmeth->check = check;
631 }
632
633 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
634                             int (**pinit) (EVP_PKEY_CTX *ctx))
635 {
636     *pinit = pmeth->init;
637 }
638
639 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
640                             int (**pcopy) (EVP_PKEY_CTX *dst,
641                                            EVP_PKEY_CTX *src))
642 {
643     *pcopy = pmeth->copy;
644 }
645
646 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
647                                void (**pcleanup) (EVP_PKEY_CTX *ctx))
648 {
649     *pcleanup = pmeth->cleanup;
650 }
651
652 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
653                                 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
654                                 int (**pparamgen) (EVP_PKEY_CTX *ctx,
655                                                    EVP_PKEY *pkey))
656 {
657     if (pparamgen_init)
658         *pparamgen_init = pmeth->paramgen_init;
659     if (pparamgen)
660         *pparamgen = pmeth->paramgen;
661 }
662
663 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
664                               int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
665                               int (**pkeygen) (EVP_PKEY_CTX *ctx,
666                                                EVP_PKEY *pkey))
667 {
668     if (pkeygen_init)
669         *pkeygen_init = pmeth->keygen_init;
670     if (pkeygen)
671         *pkeygen = pmeth->keygen;
672 }
673
674 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
675                             int (**psign_init) (EVP_PKEY_CTX *ctx),
676                             int (**psign) (EVP_PKEY_CTX *ctx,
677                                            unsigned char *sig, size_t *siglen,
678                                            const unsigned char *tbs,
679                                            size_t tbslen))
680 {
681     if (psign_init)
682         *psign_init = pmeth->sign_init;
683     if (psign)
684         *psign = pmeth->sign;
685 }
686
687 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
688                               int (**pverify_init) (EVP_PKEY_CTX *ctx),
689                               int (**pverify) (EVP_PKEY_CTX *ctx,
690                                                const unsigned char *sig,
691                                                size_t siglen,
692                                                const unsigned char *tbs,
693                                                size_t tbslen))
694 {
695     if (pverify_init)
696         *pverify_init = pmeth->verify_init;
697     if (pverify)
698         *pverify = pmeth->verify;
699 }
700
701 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
702                                       int (**pverify_recover_init) (EVP_PKEY_CTX
703                                                                     *ctx),
704                                       int (**pverify_recover) (EVP_PKEY_CTX
705                                                                *ctx,
706                                                                unsigned char
707                                                                *sig,
708                                                                size_t *siglen,
709                                                                const unsigned
710                                                                char *tbs,
711                                                                size_t tbslen))
712 {
713     if (pverify_recover_init)
714         *pverify_recover_init = pmeth->verify_recover_init;
715     if (pverify_recover)
716         *pverify_recover = pmeth->verify_recover;
717 }
718
719 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
720                                int (**psignctx_init) (EVP_PKEY_CTX *ctx,
721                                                       EVP_MD_CTX *mctx),
722                                int (**psignctx) (EVP_PKEY_CTX *ctx,
723                                                  unsigned char *sig,
724                                                  size_t *siglen,
725                                                  EVP_MD_CTX *mctx))
726 {
727     if (psignctx_init)
728         *psignctx_init = pmeth->signctx_init;
729     if (psignctx)
730         *psignctx = pmeth->signctx;
731 }
732
733 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
734                                  int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
735                                                           EVP_MD_CTX *mctx),
736                                  int (**pverifyctx) (EVP_PKEY_CTX *ctx,
737                                                      const unsigned char *sig,
738                                                      int siglen,
739                                                      EVP_MD_CTX *mctx))
740 {
741     if (pverifyctx_init)
742         *pverifyctx_init = pmeth->verifyctx_init;
743     if (pverifyctx)
744         *pverifyctx = pmeth->verifyctx;
745 }
746
747 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
748                                int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
749                                int (**pencryptfn) (EVP_PKEY_CTX *ctx,
750                                                    unsigned char *out,
751                                                    size_t *outlen,
752                                                    const unsigned char *in,
753                                                    size_t inlen))
754 {
755     if (pencrypt_init)
756         *pencrypt_init = pmeth->encrypt_init;
757     if (pencryptfn)
758         *pencryptfn = pmeth->encrypt;
759 }
760
761 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
762                                int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
763                                int (**pdecrypt) (EVP_PKEY_CTX *ctx,
764                                                  unsigned char *out,
765                                                  size_t *outlen,
766                                                  const unsigned char *in,
767                                                  size_t inlen))
768 {
769     if (pdecrypt_init)
770         *pdecrypt_init = pmeth->decrypt_init;
771     if (pdecrypt)
772         *pdecrypt = pmeth->decrypt;
773 }
774
775 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
776                               int (**pderive_init) (EVP_PKEY_CTX *ctx),
777                               int (**pderive) (EVP_PKEY_CTX *ctx,
778                                                unsigned char *key,
779                                                size_t *keylen))
780 {
781     if (pderive_init)
782         *pderive_init = pmeth->derive_init;
783     if (pderive)
784         *pderive = pmeth->derive;
785 }
786
787 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
788                             int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
789                                            void *p2),
790                             int (**pctrl_str) (EVP_PKEY_CTX *ctx,
791                                                const char *type,
792                                                const char *value))
793 {
794     if (pctrl)
795         *pctrl = pmeth->ctrl;
796     if (pctrl_str)
797         *pctrl_str = pmeth->ctrl_str;
798 }
799
800 void EVP_PKEY_meth_get_check(EVP_PKEY_METHOD *pmeth,
801                              int (**pcheck) (EVP_PKEY *pkey))
802 {
803     if (*pcheck)
804         *pcheck = pmeth->check;
805 }