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