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