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