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