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