4ea36b7addbbd8a71b1c68270732488fffc4c581
[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
366 /* Pass a message digest to a ctrl */
367 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
368 {
369     const EVP_MD *m;
370     if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
371         EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
372         return 0;
373     }
374     return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
375 }
376
377 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
378 {
379     return ctx->operation;
380 }
381
382 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
383 {
384     ctx->keygen_info = dat;
385     ctx->keygen_info_count = datlen;
386 }
387
388 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
389 {
390     ctx->data = data;
391 }
392
393 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
394 {
395     return ctx->data;
396 }
397
398 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
399 {
400     return ctx->pkey;
401 }
402
403 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
404 {
405     return ctx->peerkey;
406 }
407
408 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
409 {
410     ctx->app_data = data;
411 }
412
413 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
414 {
415     return ctx->app_data;
416 }
417
418 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
419                             int (*init) (EVP_PKEY_CTX *ctx))
420 {
421     pmeth->init = init;
422 }
423
424 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
425                             int (*copy) (EVP_PKEY_CTX *dst,
426                                          EVP_PKEY_CTX *src))
427 {
428     pmeth->copy = copy;
429 }
430
431 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
432                                void (*cleanup) (EVP_PKEY_CTX *ctx))
433 {
434     pmeth->cleanup = cleanup;
435 }
436
437 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
438                                 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
439                                 int (*paramgen) (EVP_PKEY_CTX *ctx,
440                                                  EVP_PKEY *pkey))
441 {
442     pmeth->paramgen_init = paramgen_init;
443     pmeth->paramgen = paramgen;
444 }
445
446 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
447                               int (*keygen_init) (EVP_PKEY_CTX *ctx),
448                               int (*keygen) (EVP_PKEY_CTX *ctx,
449                                              EVP_PKEY *pkey))
450 {
451     pmeth->keygen_init = keygen_init;
452     pmeth->keygen = keygen;
453 }
454
455 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
456                             int (*sign_init) (EVP_PKEY_CTX *ctx),
457                             int (*sign) (EVP_PKEY_CTX *ctx,
458                                          unsigned char *sig, size_t *siglen,
459                                          const unsigned char *tbs,
460                                          size_t tbslen))
461 {
462     pmeth->sign_init = sign_init;
463     pmeth->sign = sign;
464 }
465
466 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
467                               int (*verify_init) (EVP_PKEY_CTX *ctx),
468                               int (*verify) (EVP_PKEY_CTX *ctx,
469                                              const unsigned char *sig,
470                                              size_t siglen,
471                                              const unsigned char *tbs,
472                                              size_t tbslen))
473 {
474     pmeth->verify_init = verify_init;
475     pmeth->verify = verify;
476 }
477
478 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
479                                       int (*verify_recover_init) (EVP_PKEY_CTX
480                                                                   *ctx),
481                                       int (*verify_recover) (EVP_PKEY_CTX
482                                                              *ctx,
483                                                              unsigned char
484                                                              *sig,
485                                                              size_t *siglen,
486                                                              const unsigned
487                                                              char *tbs,
488                                                              size_t tbslen))
489 {
490     pmeth->verify_recover_init = verify_recover_init;
491     pmeth->verify_recover = verify_recover;
492 }
493
494 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
495                                int (*signctx_init) (EVP_PKEY_CTX *ctx,
496                                                     EVP_MD_CTX *mctx),
497                                int (*signctx) (EVP_PKEY_CTX *ctx,
498                                                unsigned char *sig,
499                                                size_t *siglen,
500                                                EVP_MD_CTX *mctx))
501 {
502     pmeth->signctx_init = signctx_init;
503     pmeth->signctx = signctx;
504 }
505
506 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
507                                  int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
508                                                         EVP_MD_CTX *mctx),
509                                  int (*verifyctx) (EVP_PKEY_CTX *ctx,
510                                                    const unsigned char *sig,
511                                                    int siglen,
512                                                    EVP_MD_CTX *mctx))
513 {
514     pmeth->verifyctx_init = verifyctx_init;
515     pmeth->verifyctx = verifyctx;
516 }
517
518 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
519                                int (*encrypt_init) (EVP_PKEY_CTX *ctx),
520                                int (*encryptfn) (EVP_PKEY_CTX *ctx,
521                                                  unsigned char *out,
522                                                  size_t *outlen,
523                                                  const unsigned char *in,
524                                                  size_t inlen))
525 {
526     pmeth->encrypt_init = encrypt_init;
527     pmeth->encrypt = encryptfn;
528 }
529
530 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
531                                int (*decrypt_init) (EVP_PKEY_CTX *ctx),
532                                int (*decrypt) (EVP_PKEY_CTX *ctx,
533                                                unsigned char *out,
534                                                size_t *outlen,
535                                                const unsigned char *in,
536                                                size_t inlen))
537 {
538     pmeth->decrypt_init = decrypt_init;
539     pmeth->decrypt = decrypt;
540 }
541
542 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
543                               int (*derive_init) (EVP_PKEY_CTX *ctx),
544                               int (*derive) (EVP_PKEY_CTX *ctx,
545                                              unsigned char *key,
546                                              size_t *keylen))
547 {
548     pmeth->derive_init = derive_init;
549     pmeth->derive = derive;
550 }
551
552 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
553                             int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
554                                          void *p2),
555                             int (*ctrl_str) (EVP_PKEY_CTX *ctx,
556                                              const char *type,
557                                              const char *value))
558 {
559     pmeth->ctrl = ctrl;
560     pmeth->ctrl_str = ctrl_str;
561 }
562
563 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
564                             int (**pinit) (EVP_PKEY_CTX *ctx))
565 {
566     *pinit = pmeth->init;
567 }
568
569 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
570                             int (**pcopy) (EVP_PKEY_CTX *dst,
571                                            EVP_PKEY_CTX *src))
572 {
573     *pcopy = pmeth->copy;
574 }
575
576 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
577                                void (**pcleanup) (EVP_PKEY_CTX *ctx))
578 {
579     *pcleanup = pmeth->cleanup;
580 }
581
582 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
583                                 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
584                                 int (**pparamgen) (EVP_PKEY_CTX *ctx,
585                                                    EVP_PKEY *pkey))
586 {
587     if (pparamgen_init)
588         *pparamgen_init = pmeth->paramgen_init;
589     if (pparamgen)
590         *pparamgen = pmeth->paramgen;
591 }
592
593 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
594                               int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
595                               int (**pkeygen) (EVP_PKEY_CTX *ctx,
596                                                EVP_PKEY *pkey))
597 {
598     if (pkeygen_init)
599         *pkeygen_init = pmeth->keygen_init;
600     if (pkeygen)
601         *pkeygen = pmeth->keygen;
602 }
603
604 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
605                             int (**psign_init) (EVP_PKEY_CTX *ctx),
606                             int (**psign) (EVP_PKEY_CTX *ctx,
607                                            unsigned char *sig, size_t *siglen,
608                                            const unsigned char *tbs,
609                                            size_t tbslen))
610 {
611     if (psign_init)
612         *psign_init = pmeth->sign_init;
613     if (psign)
614         *psign = pmeth->sign;
615 }
616
617 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
618                               int (**pverify_init) (EVP_PKEY_CTX *ctx),
619                               int (**pverify) (EVP_PKEY_CTX *ctx,
620                                                const unsigned char *sig,
621                                                size_t siglen,
622                                                const unsigned char *tbs,
623                                                size_t tbslen))
624 {
625     if (pverify_init)
626         *pverify_init = pmeth->verify_init;
627     if (pverify)
628         *pverify = pmeth->verify;
629 }
630
631 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
632                                       int (**pverify_recover_init) (EVP_PKEY_CTX
633                                                                     *ctx),
634                                       int (**pverify_recover) (EVP_PKEY_CTX
635                                                                *ctx,
636                                                                unsigned char
637                                                                *sig,
638                                                                size_t *siglen,
639                                                                const unsigned
640                                                                char *tbs,
641                                                                size_t tbslen))
642 {
643     if (pverify_recover_init)
644         *pverify_recover_init = pmeth->verify_recover_init;
645     if (pverify_recover)
646         *pverify_recover = pmeth->verify_recover;
647 }
648
649 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
650                                int (**psignctx_init) (EVP_PKEY_CTX *ctx,
651                                                       EVP_MD_CTX *mctx),
652                                int (**psignctx) (EVP_PKEY_CTX *ctx,
653                                                  unsigned char *sig,
654                                                  size_t *siglen,
655                                                  EVP_MD_CTX *mctx))
656 {
657     if (psignctx_init)
658         *psignctx_init = pmeth->signctx_init;
659     if (psignctx)
660         *psignctx = pmeth->signctx;
661 }
662
663 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
664                                  int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
665                                                           EVP_MD_CTX *mctx),
666                                  int (**pverifyctx) (EVP_PKEY_CTX *ctx,
667                                                      const unsigned char *sig,
668                                                      int siglen,
669                                                      EVP_MD_CTX *mctx))
670 {
671     if (pverifyctx_init)
672         *pverifyctx_init = pmeth->verifyctx_init;
673     if (pverifyctx)
674         *pverifyctx = pmeth->verifyctx;
675 }
676
677 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
678                                int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
679                                int (**pencryptfn) (EVP_PKEY_CTX *ctx,
680                                                    unsigned char *out,
681                                                    size_t *outlen,
682                                                    const unsigned char *in,
683                                                    size_t inlen))
684 {
685     if (pencrypt_init)
686         *pencrypt_init = pmeth->encrypt_init;
687     if (pencryptfn)
688         *pencryptfn = pmeth->encrypt;
689 }
690
691 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
692                                int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
693                                int (**pdecrypt) (EVP_PKEY_CTX *ctx,
694                                                  unsigned char *out,
695                                                  size_t *outlen,
696                                                  const unsigned char *in,
697                                                  size_t inlen))
698 {
699     if (pdecrypt_init)
700         *pdecrypt_init = pmeth->decrypt_init;
701     if (pdecrypt)
702         *pdecrypt = pmeth->decrypt;
703 }
704
705 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
706                               int (**pderive_init) (EVP_PKEY_CTX *ctx),
707                               int (**pderive) (EVP_PKEY_CTX *ctx,
708                                                unsigned char *key,
709                                                size_t *keylen))
710 {
711     if (pderive_init)
712         *pderive_init = pmeth->derive_init;
713     if (pderive)
714         *pderive = pmeth->derive;
715 }
716
717 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
718                             int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
719                                            void *p2),
720                             int (**pctrl_str) (EVP_PKEY_CTX *ctx,
721                                                const char *type,
722                                                const char *value))
723 {
724     if (pctrl)
725         *pctrl = pmeth->ctrl;
726     if (pctrl_str)
727         *pctrl_str = pmeth->ctrl_str;
728 }