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