]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_lib.c
Fix no-dh
[thirdparty/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 #ifndef OPENSSL_NO_DH
397 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
398 {
399 OSSL_PARAM dh_pad_params[2];
400
401 /* TODO(3.0): Remove this eventually when no more legacy */
402 if (ctx->exchprovctx == NULL)
403 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
404 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
405
406 dh_pad_params[0] = OSSL_PARAM_construct_int(OSSL_EXCHANGE_PARAM_PAD, &pad);
407 dh_pad_params[1] = OSSL_PARAM_construct_end();
408
409 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
410 }
411 #endif
412
413 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
414 int cmd, int p1, void *p2)
415 {
416 switch (cmd) {
417 #ifndef OPENSSL_NO_DH
418 case EVP_PKEY_CTRL_DH_PAD:
419 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
420 #endif
421 }
422 return 0;
423 }
424
425 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
426 int cmd, int p1, void *p2)
427 {
428 int ret;
429
430 if (ctx == NULL) {
431 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
432 return -2;
433 }
434
435 if (ctx->exchprovctx != NULL)
436 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
437
438 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
439 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
440 return -2;
441 }
442 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
443 return -1;
444
445 /* Skip the operation checks since this is called in a very early stage */
446 if (ctx->pmeth->digest_custom != NULL)
447 goto doit;
448
449 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
450 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
451 return -1;
452 }
453
454 if ((optype != -1) && !(ctx->operation & optype)) {
455 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
456 return -1;
457 }
458
459 doit:
460 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
461
462 if (ret == -2)
463 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
464
465 return ret;
466 }
467
468 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
469 int cmd, uint64_t value)
470 {
471 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
472 }
473
474 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
475 const char *value)
476 {
477 #ifndef OPENSSL_NO_DH
478 if (strcmp(name, "dh_pad") == 0) {
479 int pad;
480
481 pad = atoi(value);
482 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
483 }
484 #endif
485 return 0;
486 }
487
488 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
489 const char *name, const char *value)
490 {
491 if (ctx == NULL) {
492 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
493 return -2;
494 }
495
496 if (ctx->exchprovctx != NULL)
497 return legacy_ctrl_str_to_param(ctx, name, value);
498
499 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
500 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
501 return -2;
502 }
503 if (strcmp(name, "digest") == 0)
504 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
505 value);
506 return ctx->pmeth->ctrl_str(ctx, name, value);
507 }
508
509 /* Utility functions to send a string of hex string to a ctrl */
510
511 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
512 {
513 size_t len;
514
515 len = strlen(str);
516 if (len > INT_MAX)
517 return -1;
518 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
519 }
520
521 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
522 {
523 unsigned char *bin;
524 long binlen;
525 int rv = -1;
526
527 bin = OPENSSL_hexstr2buf(hex, &binlen);
528 if (bin == NULL)
529 return 0;
530 if (binlen <= INT_MAX)
531 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
532 OPENSSL_free(bin);
533 return rv;
534 }
535
536 /* Pass a message digest to a ctrl */
537 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
538 {
539 const EVP_MD *m;
540
541 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
542 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
543 return 0;
544 }
545 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
546 }
547
548 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
549 {
550 return ctx->operation;
551 }
552
553 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
554 {
555 ctx->keygen_info = dat;
556 ctx->keygen_info_count = datlen;
557 }
558
559 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
560 {
561 ctx->data = data;
562 }
563
564 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
565 {
566 return ctx->data;
567 }
568
569 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
570 {
571 return ctx->pkey;
572 }
573
574 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
575 {
576 return ctx->peerkey;
577 }
578
579 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
580 {
581 ctx->app_data = data;
582 }
583
584 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
585 {
586 return ctx->app_data;
587 }
588
589 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
590 int (*init) (EVP_PKEY_CTX *ctx))
591 {
592 pmeth->init = init;
593 }
594
595 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
596 int (*copy) (EVP_PKEY_CTX *dst,
597 const EVP_PKEY_CTX *src))
598 {
599 pmeth->copy = copy;
600 }
601
602 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
603 void (*cleanup) (EVP_PKEY_CTX *ctx))
604 {
605 pmeth->cleanup = cleanup;
606 }
607
608 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
609 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
610 int (*paramgen) (EVP_PKEY_CTX *ctx,
611 EVP_PKEY *pkey))
612 {
613 pmeth->paramgen_init = paramgen_init;
614 pmeth->paramgen = paramgen;
615 }
616
617 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
618 int (*keygen_init) (EVP_PKEY_CTX *ctx),
619 int (*keygen) (EVP_PKEY_CTX *ctx,
620 EVP_PKEY *pkey))
621 {
622 pmeth->keygen_init = keygen_init;
623 pmeth->keygen = keygen;
624 }
625
626 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
627 int (*sign_init) (EVP_PKEY_CTX *ctx),
628 int (*sign) (EVP_PKEY_CTX *ctx,
629 unsigned char *sig, size_t *siglen,
630 const unsigned char *tbs,
631 size_t tbslen))
632 {
633 pmeth->sign_init = sign_init;
634 pmeth->sign = sign;
635 }
636
637 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
638 int (*verify_init) (EVP_PKEY_CTX *ctx),
639 int (*verify) (EVP_PKEY_CTX *ctx,
640 const unsigned char *sig,
641 size_t siglen,
642 const unsigned char *tbs,
643 size_t tbslen))
644 {
645 pmeth->verify_init = verify_init;
646 pmeth->verify = verify;
647 }
648
649 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
650 int (*verify_recover_init) (EVP_PKEY_CTX
651 *ctx),
652 int (*verify_recover) (EVP_PKEY_CTX
653 *ctx,
654 unsigned char
655 *sig,
656 size_t *siglen,
657 const unsigned
658 char *tbs,
659 size_t tbslen))
660 {
661 pmeth->verify_recover_init = verify_recover_init;
662 pmeth->verify_recover = verify_recover;
663 }
664
665 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
666 int (*signctx_init) (EVP_PKEY_CTX *ctx,
667 EVP_MD_CTX *mctx),
668 int (*signctx) (EVP_PKEY_CTX *ctx,
669 unsigned char *sig,
670 size_t *siglen,
671 EVP_MD_CTX *mctx))
672 {
673 pmeth->signctx_init = signctx_init;
674 pmeth->signctx = signctx;
675 }
676
677 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
678 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
679 EVP_MD_CTX *mctx),
680 int (*verifyctx) (EVP_PKEY_CTX *ctx,
681 const unsigned char *sig,
682 int siglen,
683 EVP_MD_CTX *mctx))
684 {
685 pmeth->verifyctx_init = verifyctx_init;
686 pmeth->verifyctx = verifyctx;
687 }
688
689 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
690 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
691 int (*encryptfn) (EVP_PKEY_CTX *ctx,
692 unsigned char *out,
693 size_t *outlen,
694 const unsigned char *in,
695 size_t inlen))
696 {
697 pmeth->encrypt_init = encrypt_init;
698 pmeth->encrypt = encryptfn;
699 }
700
701 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
702 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
703 int (*decrypt) (EVP_PKEY_CTX *ctx,
704 unsigned char *out,
705 size_t *outlen,
706 const unsigned char *in,
707 size_t inlen))
708 {
709 pmeth->decrypt_init = decrypt_init;
710 pmeth->decrypt = decrypt;
711 }
712
713 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
714 int (*derive_init) (EVP_PKEY_CTX *ctx),
715 int (*derive) (EVP_PKEY_CTX *ctx,
716 unsigned char *key,
717 size_t *keylen))
718 {
719 pmeth->derive_init = derive_init;
720 pmeth->derive = derive;
721 }
722
723 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
724 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
725 void *p2),
726 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
727 const char *type,
728 const char *value))
729 {
730 pmeth->ctrl = ctrl;
731 pmeth->ctrl_str = ctrl_str;
732 }
733
734 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
735 int (*check) (EVP_PKEY *pkey))
736 {
737 pmeth->check = check;
738 }
739
740 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
741 int (*check) (EVP_PKEY *pkey))
742 {
743 pmeth->public_check = check;
744 }
745
746 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
747 int (*check) (EVP_PKEY *pkey))
748 {
749 pmeth->param_check = check;
750 }
751
752 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
753 int (*digest_custom) (EVP_PKEY_CTX *ctx,
754 EVP_MD_CTX *mctx))
755 {
756 pmeth->digest_custom = digest_custom;
757 }
758
759 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
760 int (**pinit) (EVP_PKEY_CTX *ctx))
761 {
762 *pinit = pmeth->init;
763 }
764
765 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
766 int (**pcopy) (EVP_PKEY_CTX *dst,
767 const EVP_PKEY_CTX *src))
768 {
769 *pcopy = pmeth->copy;
770 }
771
772 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
773 void (**pcleanup) (EVP_PKEY_CTX *ctx))
774 {
775 *pcleanup = pmeth->cleanup;
776 }
777
778 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
779 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
780 int (**pparamgen) (EVP_PKEY_CTX *ctx,
781 EVP_PKEY *pkey))
782 {
783 if (pparamgen_init)
784 *pparamgen_init = pmeth->paramgen_init;
785 if (pparamgen)
786 *pparamgen = pmeth->paramgen;
787 }
788
789 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
790 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
791 int (**pkeygen) (EVP_PKEY_CTX *ctx,
792 EVP_PKEY *pkey))
793 {
794 if (pkeygen_init)
795 *pkeygen_init = pmeth->keygen_init;
796 if (pkeygen)
797 *pkeygen = pmeth->keygen;
798 }
799
800 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
801 int (**psign_init) (EVP_PKEY_CTX *ctx),
802 int (**psign) (EVP_PKEY_CTX *ctx,
803 unsigned char *sig, size_t *siglen,
804 const unsigned char *tbs,
805 size_t tbslen))
806 {
807 if (psign_init)
808 *psign_init = pmeth->sign_init;
809 if (psign)
810 *psign = pmeth->sign;
811 }
812
813 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
814 int (**pverify_init) (EVP_PKEY_CTX *ctx),
815 int (**pverify) (EVP_PKEY_CTX *ctx,
816 const unsigned char *sig,
817 size_t siglen,
818 const unsigned char *tbs,
819 size_t tbslen))
820 {
821 if (pverify_init)
822 *pverify_init = pmeth->verify_init;
823 if (pverify)
824 *pverify = pmeth->verify;
825 }
826
827 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
828 int (**pverify_recover_init) (EVP_PKEY_CTX
829 *ctx),
830 int (**pverify_recover) (EVP_PKEY_CTX
831 *ctx,
832 unsigned char
833 *sig,
834 size_t *siglen,
835 const unsigned
836 char *tbs,
837 size_t tbslen))
838 {
839 if (pverify_recover_init)
840 *pverify_recover_init = pmeth->verify_recover_init;
841 if (pverify_recover)
842 *pverify_recover = pmeth->verify_recover;
843 }
844
845 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
846 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
847 EVP_MD_CTX *mctx),
848 int (**psignctx) (EVP_PKEY_CTX *ctx,
849 unsigned char *sig,
850 size_t *siglen,
851 EVP_MD_CTX *mctx))
852 {
853 if (psignctx_init)
854 *psignctx_init = pmeth->signctx_init;
855 if (psignctx)
856 *psignctx = pmeth->signctx;
857 }
858
859 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
860 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
861 EVP_MD_CTX *mctx),
862 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
863 const unsigned char *sig,
864 int siglen,
865 EVP_MD_CTX *mctx))
866 {
867 if (pverifyctx_init)
868 *pverifyctx_init = pmeth->verifyctx_init;
869 if (pverifyctx)
870 *pverifyctx = pmeth->verifyctx;
871 }
872
873 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
874 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
875 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
876 unsigned char *out,
877 size_t *outlen,
878 const unsigned char *in,
879 size_t inlen))
880 {
881 if (pencrypt_init)
882 *pencrypt_init = pmeth->encrypt_init;
883 if (pencryptfn)
884 *pencryptfn = pmeth->encrypt;
885 }
886
887 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
888 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
889 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
890 unsigned char *out,
891 size_t *outlen,
892 const unsigned char *in,
893 size_t inlen))
894 {
895 if (pdecrypt_init)
896 *pdecrypt_init = pmeth->decrypt_init;
897 if (pdecrypt)
898 *pdecrypt = pmeth->decrypt;
899 }
900
901 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
902 int (**pderive_init) (EVP_PKEY_CTX *ctx),
903 int (**pderive) (EVP_PKEY_CTX *ctx,
904 unsigned char *key,
905 size_t *keylen))
906 {
907 if (pderive_init)
908 *pderive_init = pmeth->derive_init;
909 if (pderive)
910 *pderive = pmeth->derive;
911 }
912
913 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
914 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
915 void *p2),
916 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
917 const char *type,
918 const char *value))
919 {
920 if (pctrl)
921 *pctrl = pmeth->ctrl;
922 if (pctrl_str)
923 *pctrl_str = pmeth->ctrl_str;
924 }
925
926 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
927 int (**pcheck) (EVP_PKEY *pkey))
928 {
929 if (pcheck != NULL)
930 *pcheck = pmeth->check;
931 }
932
933 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
934 int (**pcheck) (EVP_PKEY *pkey))
935 {
936 if (pcheck != NULL)
937 *pcheck = pmeth->public_check;
938 }
939
940 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
941 int (**pcheck) (EVP_PKEY *pkey))
942 {
943 if (pcheck != NULL)
944 *pcheck = pmeth->param_check;
945 }
946
947 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
948 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
949 EVP_MD_CTX *mctx))
950 {
951 if (pdigest_custom != NULL)
952 *pdigest_custom = pmeth->digest_custom;
953 }