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