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