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