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