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