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