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