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