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