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