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