]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_lib.c
configure: introduce no-ecx to remove ECX related feature
[thirdparty/openssl.git] / crypto / evp / pmeth_lib.c
1 /*
2 * Copyright 2006-2022 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 /*
11 * Low level key APIs (DH etc) are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #ifndef FIPS_MODULE
19 # include <openssl/engine.h>
20 #endif
21 #include <openssl/evp.h>
22 #include <openssl/core_names.h>
23 #include <openssl/dh.h>
24 #include <openssl/rsa.h>
25 #include <openssl/kdf.h>
26 #include "internal/cryptlib.h"
27 #ifndef FIPS_MODULE
28 # include "crypto/asn1.h"
29 #endif
30 #include "crypto/evp.h"
31 #include "crypto/dh.h"
32 #include "crypto/ec.h"
33 #include "internal/ffc.h"
34 #include "internal/numbers.h"
35 #include "internal/provider.h"
36 #include "evp_local.h"
37
38 #ifndef FIPS_MODULE
39
40 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
41 int keytype, int optype,
42 int cmd, const char *name,
43 const void *data, size_t data_len);
44 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
45 int cmd, const char *name);
46 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
47
48 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
49 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
50
51 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
52
53 /* This array needs to be in order of NIDs */
54 static pmeth_fn standard_methods[] = {
55 ossl_rsa_pkey_method,
56 # ifndef OPENSSL_NO_DH
57 ossl_dh_pkey_method,
58 # endif
59 # ifndef OPENSSL_NO_DSA
60 ossl_dsa_pkey_method,
61 # endif
62 # ifndef OPENSSL_NO_EC
63 ossl_ec_pkey_method,
64 # endif
65 ossl_rsa_pss_pkey_method,
66 # ifndef OPENSSL_NO_DH
67 ossl_dhx_pkey_method,
68 # endif
69 # ifndef OPENSSL_NO_ECX
70 ossl_ecx25519_pkey_method,
71 ossl_ecx448_pkey_method,
72 ossl_ed25519_pkey_method,
73 ossl_ed448_pkey_method,
74 # endif
75 };
76
77 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
78
79 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
80 {
81 return ((*a)->pkey_id - ((**b)())->pkey_id);
82 }
83
84 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
85
86 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
87 const EVP_PKEY_METHOD *const *b)
88 {
89 return ((*a)->pkey_id - (*b)->pkey_id);
90 }
91
92 static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type)
93 {
94 if (app_pkey_methods != NULL) {
95 int idx;
96 EVP_PKEY_METHOD tmp;
97
98 tmp.pkey_id = type;
99 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
100 if (idx >= 0)
101 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
102 }
103 return NULL;
104 }
105
106 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
107 {
108 pmeth_fn *ret;
109 EVP_PKEY_METHOD tmp;
110 const EVP_PKEY_METHOD *t;
111
112 if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL)
113 return t;
114
115 tmp.pkey_id = type;
116 t = &tmp;
117 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
118 OSSL_NELEM(standard_methods));
119 if (ret == NULL || *ret == NULL)
120 return NULL;
121 return (**ret)();
122 }
123
124 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
125 {
126 EVP_PKEY_METHOD *pmeth;
127
128 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
129 if (pmeth == NULL)
130 return NULL;
131
132 pmeth->pkey_id = id;
133 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
134 return pmeth;
135 }
136
137 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype,
138 void *arg)
139 {
140 int *type = arg;
141
142 if (*type == NID_undef)
143 *type = evp_pkey_name2type(keytype);
144 }
145
146 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt)
147 {
148 int type = NID_undef;
149
150 EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt,
151 &type);
152 return type;
153 }
154 #endif /* FIPS_MODULE */
155
156 int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx)
157 {
158 if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
159 return EVP_PKEY_STATE_UNKNOWN;
160
161 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
162 && ctx->op.kex.algctx != NULL)
163 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
164 && ctx->op.sig.algctx != NULL)
165 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
166 && ctx->op.ciph.algctx != NULL)
167 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
168 && ctx->op.keymgmt.genctx != NULL)
169 || (EVP_PKEY_CTX_IS_KEM_OP(ctx)
170 && ctx->op.encap.algctx != NULL))
171 return EVP_PKEY_STATE_PROVIDER;
172
173 return EVP_PKEY_STATE_LEGACY;
174 }
175
176 static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx,
177 EVP_PKEY *pkey, ENGINE *e,
178 const char *keytype, const char *propquery,
179 int id)
180
181 {
182 EVP_PKEY_CTX *ret = NULL;
183 const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL;
184 EVP_KEYMGMT *keymgmt = NULL;
185
186 /* Code below to be removed when legacy support is dropped. */
187 /* BEGIN legacy */
188 if (id == -1) {
189 if (pkey != NULL && !evp_pkey_is_provided(pkey)) {
190 id = pkey->type;
191 } else {
192 if (pkey != NULL) {
193 /* Must be provided if we get here */
194 keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt);
195 }
196 #ifndef FIPS_MODULE
197 if (keytype != NULL) {
198 id = evp_pkey_name2type(keytype);
199 if (id == NID_undef)
200 id = -1;
201 }
202 #endif
203 }
204 }
205 /* If no ID was found here, we can only resort to find a keymgmt */
206 if (id == -1) {
207 #ifndef FIPS_MODULE
208 /* Using engine with a key without id will not work */
209 if (e != NULL) {
210 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
211 return NULL;
212 }
213 #endif
214 goto common;
215 }
216
217 #ifndef FIPS_MODULE
218 /*
219 * Here, we extract what information we can for the purpose of
220 * supporting usage with implementations from providers, to make
221 * for a smooth transition from legacy stuff to provider based stuff.
222 *
223 * If an engine is given, this is entirely legacy, and we should not
224 * pretend anything else, so we clear the name.
225 */
226 if (e != NULL)
227 keytype = NULL;
228 if (e == NULL && (pkey == NULL || pkey->foreign == 0))
229 keytype = OBJ_nid2sn(id);
230
231 # ifndef OPENSSL_NO_ENGINE
232 if (e == NULL && pkey != NULL)
233 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
234 /* Try to find an ENGINE which implements this method */
235 if (e != NULL) {
236 if (!ENGINE_init(e)) {
237 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
238 return NULL;
239 }
240 } else {
241 e = ENGINE_get_pkey_meth_engine(id);
242 }
243
244 /*
245 * If an ENGINE handled this method look it up. Otherwise use internal
246 * tables.
247 */
248 if (e != NULL)
249 pmeth = ENGINE_get_pkey_meth(e, id);
250 else if (pkey != NULL && pkey->foreign)
251 pmeth = EVP_PKEY_meth_find(id);
252 else
253 # endif
254 app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id);
255
256 /* END legacy */
257 #endif /* FIPS_MODULE */
258 common:
259 /*
260 * If there's no engine and no app supplied pmeth and there's a name, we try
261 * fetching a provider implementation.
262 */
263 if (e == NULL && app_pmeth == NULL && keytype != NULL) {
264 /*
265 * If |pkey| is given and is provided, we take a reference to its
266 * keymgmt. Otherwise, we fetch one for the keytype we got. This
267 * is to ensure that operation init functions can access what they
268 * need through this single pointer.
269 */
270 if (pkey != NULL && pkey->keymgmt != NULL) {
271 if (!EVP_KEYMGMT_up_ref(pkey->keymgmt))
272 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
273 else
274 keymgmt = pkey->keymgmt;
275 } else {
276 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
277 }
278 if (keymgmt == NULL)
279 return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
280
281 #ifndef FIPS_MODULE
282 /*
283 * Chase down the legacy NID, as that might be needed for diverse
284 * purposes, such as ensure that EVP_PKEY_type() can return sensible
285 * values. We go through all keymgmt names, because the keytype
286 * that's passed to this function doesn't necessarily translate
287 * directly.
288 */
289 if (keymgmt != NULL) {
290 int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt);
291
292 if (tmp_id != NID_undef) {
293 if (id == -1) {
294 id = tmp_id;
295 } else {
296 /*
297 * It really really shouldn't differ. If it still does,
298 * something is very wrong.
299 */
300 if (!ossl_assert(id == tmp_id)) {
301 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
302 EVP_KEYMGMT_free(keymgmt);
303 return NULL;
304 }
305 }
306 }
307 }
308 #endif
309 }
310
311 if (pmeth == NULL && keymgmt == NULL) {
312 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
313 } else {
314 ret = OPENSSL_zalloc(sizeof(*ret));
315 }
316
317 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
318 if ((ret == NULL || pmeth == NULL) && e != NULL)
319 ENGINE_finish(e);
320 #endif
321
322 if (ret == NULL) {
323 EVP_KEYMGMT_free(keymgmt);
324 return NULL;
325 }
326 if (propquery != NULL) {
327 ret->propquery = OPENSSL_strdup(propquery);
328 if (ret->propquery == NULL) {
329 OPENSSL_free(ret);
330 EVP_KEYMGMT_free(keymgmt);
331 return NULL;
332 }
333 }
334 ret->libctx = libctx;
335 ret->keytype = keytype;
336 ret->keymgmt = keymgmt;
337 ret->legacy_keytype = id;
338 ret->engine = e;
339 ret->pmeth = pmeth;
340 ret->operation = EVP_PKEY_OP_UNDEFINED;
341 ret->pkey = pkey;
342 if (pkey != NULL)
343 EVP_PKEY_up_ref(pkey);
344
345 if (pmeth != NULL && pmeth->init != NULL) {
346 if (pmeth->init(ret) <= 0) {
347 ret->pmeth = NULL;
348 EVP_PKEY_CTX_free(ret);
349 return NULL;
350 }
351 }
352
353 return ret;
354 }
355
356 /*- All methods below can also be used in FIPS_MODULE */
357
358 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx,
359 const char *name,
360 const char *propquery)
361 {
362 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
363 }
364
365 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey,
366 const char *propquery)
367 {
368 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
369 }
370
371 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
372 {
373 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
374 if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL)
375 ctx->op.sig.signature->freectx(ctx->op.sig.algctx);
376 EVP_SIGNATURE_free(ctx->op.sig.signature);
377 ctx->op.sig.algctx = NULL;
378 ctx->op.sig.signature = NULL;
379 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
380 if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL)
381 ctx->op.kex.exchange->freectx(ctx->op.kex.algctx);
382 EVP_KEYEXCH_free(ctx->op.kex.exchange);
383 ctx->op.kex.algctx = NULL;
384 ctx->op.kex.exchange = NULL;
385 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
386 if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL)
387 ctx->op.encap.kem->freectx(ctx->op.encap.algctx);
388 EVP_KEM_free(ctx->op.encap.kem);
389 ctx->op.encap.algctx = NULL;
390 ctx->op.encap.kem = NULL;
391 }
392 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
393 if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL)
394 ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx);
395 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
396 ctx->op.ciph.algctx = NULL;
397 ctx->op.ciph.cipher = NULL;
398 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
399 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
400 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
401 }
402 }
403
404 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
405 {
406 if (ctx == NULL)
407 return;
408 if (ctx->pmeth && ctx->pmeth->cleanup)
409 ctx->pmeth->cleanup(ctx);
410
411 evp_pkey_ctx_free_old_ops(ctx);
412 #ifndef FIPS_MODULE
413 evp_pkey_ctx_free_all_cached_data(ctx);
414 #endif
415 EVP_KEYMGMT_free(ctx->keymgmt);
416
417 OPENSSL_free(ctx->propquery);
418 EVP_PKEY_free(ctx->pkey);
419 EVP_PKEY_free(ctx->peerkey);
420 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
421 ENGINE_finish(ctx->engine);
422 #endif
423 BN_free(ctx->rsa_pubexp);
424 OPENSSL_free(ctx);
425 }
426
427 #ifndef FIPS_MODULE
428
429 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
430 const EVP_PKEY_METHOD *meth)
431 {
432 if (ppkey_id)
433 *ppkey_id = meth->pkey_id;
434 if (pflags)
435 *pflags = meth->flags;
436 }
437
438 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
439 {
440 int pkey_id = dst->pkey_id;
441 int flags = dst->flags;
442
443 *dst = *src;
444
445 /* We only copy the function pointers so restore the other values */
446 dst->pkey_id = pkey_id;
447 dst->flags = flags;
448 }
449
450 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
451 {
452 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
453 OPENSSL_free(pmeth);
454 }
455
456 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
457 {
458 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
459 }
460
461 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
462 {
463 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
464 }
465
466 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
467 {
468 EVP_PKEY_CTX *rctx;
469
470 # ifndef OPENSSL_NO_ENGINE
471 /* Make sure it's safe to copy a pkey context using an ENGINE */
472 if (pctx->engine && !ENGINE_init(pctx->engine)) {
473 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
474 return 0;
475 }
476 # endif
477 rctx = OPENSSL_zalloc(sizeof(*rctx));
478 if (rctx == NULL)
479 return NULL;
480
481 if (pctx->pkey != NULL)
482 EVP_PKEY_up_ref(pctx->pkey);
483 rctx->pkey = pctx->pkey;
484 rctx->operation = pctx->operation;
485 rctx->libctx = pctx->libctx;
486 rctx->keytype = pctx->keytype;
487 rctx->propquery = NULL;
488 if (pctx->propquery != NULL) {
489 rctx->propquery = OPENSSL_strdup(pctx->propquery);
490 if (rctx->propquery == NULL)
491 goto err;
492 }
493 rctx->legacy_keytype = pctx->legacy_keytype;
494
495 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
496 if (pctx->op.kex.exchange != NULL) {
497 rctx->op.kex.exchange = pctx->op.kex.exchange;
498 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange))
499 goto err;
500 }
501 if (pctx->op.kex.algctx != NULL) {
502 if (!ossl_assert(pctx->op.kex.exchange != NULL))
503 goto err;
504
505 if (pctx->op.kex.exchange->dupctx != NULL)
506 rctx->op.kex.algctx
507 = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx);
508
509 if (rctx->op.kex.algctx == NULL) {
510 EVP_KEYEXCH_free(rctx->op.kex.exchange);
511 rctx->op.kex.exchange = NULL;
512 goto err;
513 }
514 return rctx;
515 }
516 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
517 if (pctx->op.sig.signature != NULL) {
518 rctx->op.sig.signature = pctx->op.sig.signature;
519 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature))
520 goto err;
521 }
522 if (pctx->op.sig.algctx != NULL) {
523 if (!ossl_assert(pctx->op.sig.signature != NULL))
524 goto err;
525
526 if (pctx->op.sig.signature->dupctx != NULL)
527 rctx->op.sig.algctx
528 = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx);
529
530 if (rctx->op.sig.algctx == NULL) {
531 EVP_SIGNATURE_free(rctx->op.sig.signature);
532 rctx->op.sig.signature = NULL;
533 goto err;
534 }
535 return rctx;
536 }
537 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
538 if (pctx->op.ciph.cipher != NULL) {
539 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
540 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher))
541 goto err;
542 }
543 if (pctx->op.ciph.algctx != NULL) {
544 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
545 goto err;
546
547 if (pctx->op.ciph.cipher->dupctx != NULL)
548 rctx->op.ciph.algctx
549 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx);
550
551 if (rctx->op.ciph.algctx == NULL) {
552 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
553 rctx->op.ciph.cipher = NULL;
554 goto err;
555 }
556 return rctx;
557 }
558 } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) {
559 if (pctx->op.encap.kem != NULL) {
560 rctx->op.encap.kem = pctx->op.encap.kem;
561 if (!EVP_KEM_up_ref(rctx->op.encap.kem))
562 goto err;
563 }
564 if (pctx->op.encap.algctx != NULL) {
565 if (!ossl_assert(pctx->op.encap.kem != NULL))
566 goto err;
567
568 if (pctx->op.encap.kem->dupctx != NULL)
569 rctx->op.encap.algctx
570 = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx);
571
572 if (rctx->op.encap.algctx == NULL) {
573 EVP_KEM_free(rctx->op.encap.kem);
574 rctx->op.encap.kem = NULL;
575 goto err;
576 }
577 return rctx;
578 }
579 } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) {
580 /* Not supported - This would need a gen_dupctx() to work */
581 goto err;
582 }
583
584 rctx->pmeth = pctx->pmeth;
585 # ifndef OPENSSL_NO_ENGINE
586 rctx->engine = pctx->engine;
587 # endif
588
589 if (pctx->peerkey != NULL)
590 EVP_PKEY_up_ref(pctx->peerkey);
591 rctx->peerkey = pctx->peerkey;
592
593 if (pctx->pmeth == NULL) {
594 if (rctx->operation == EVP_PKEY_OP_UNDEFINED) {
595 EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt;
596 void *provkey;
597
598 provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx,
599 &tmp_keymgmt, pctx->propquery);
600 if (provkey == NULL)
601 goto err;
602 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt))
603 goto err;
604 EVP_KEYMGMT_free(rctx->keymgmt);
605 rctx->keymgmt = tmp_keymgmt;
606 return rctx;
607 }
608 } else if (pctx->pmeth->copy(rctx, pctx) > 0) {
609 return rctx;
610 }
611 err:
612 rctx->pmeth = NULL;
613 EVP_PKEY_CTX_free(rctx);
614 return NULL;
615 }
616
617 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
618 {
619 if (app_pkey_methods == NULL) {
620 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
621 if (app_pkey_methods == NULL) {
622 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
623 return 0;
624 }
625 }
626 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
627 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
628 return 0;
629 }
630 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
631 return 1;
632 }
633
634 void evp_app_cleanup_int(void)
635 {
636 if (app_pkey_methods != NULL)
637 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
638 }
639
640 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
641 {
642 const EVP_PKEY_METHOD *ret;
643
644 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
645
646 return ret == NULL ? 0 : 1;
647 }
648
649 size_t EVP_PKEY_meth_get_count(void)
650 {
651 size_t rv = OSSL_NELEM(standard_methods);
652
653 if (app_pkey_methods)
654 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
655 return rv;
656 }
657
658 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
659 {
660 if (idx < OSSL_NELEM(standard_methods))
661 return (standard_methods[idx])();
662 if (app_pkey_methods == NULL)
663 return NULL;
664 idx -= OSSL_NELEM(standard_methods);
665 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
666 return NULL;
667 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
668 }
669 #endif
670
671 int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype)
672 {
673 #ifndef FIPS_MODULE
674 if (evp_pkey_ctx_is_legacy(ctx))
675 return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype));
676 #endif
677 return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype);
678 }
679
680 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
681 {
682 switch (evp_pkey_ctx_state(ctx)) {
683 case EVP_PKEY_STATE_PROVIDER:
684 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
685 && ctx->op.kex.exchange != NULL
686 && ctx->op.kex.exchange->set_ctx_params != NULL)
687 return
688 ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx,
689 params);
690 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
691 && ctx->op.sig.signature != NULL
692 && ctx->op.sig.signature->set_ctx_params != NULL)
693 return
694 ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx,
695 params);
696 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
697 && ctx->op.ciph.cipher != NULL
698 && ctx->op.ciph.cipher->set_ctx_params != NULL)
699 return
700 ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx,
701 params);
702 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
703 && ctx->keymgmt != NULL
704 && ctx->keymgmt->gen_set_params != NULL)
705 return
706 evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
707 params);
708 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
709 && ctx->op.encap.kem != NULL
710 && ctx->op.encap.kem->set_ctx_params != NULL)
711 return
712 ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx,
713 params);
714 break;
715 #ifndef FIPS_MODULE
716 case EVP_PKEY_STATE_UNKNOWN:
717 case EVP_PKEY_STATE_LEGACY:
718 return evp_pkey_ctx_set_params_to_ctrl(ctx, params);
719 #endif
720 }
721 return 0;
722 }
723
724 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
725 {
726 switch (evp_pkey_ctx_state(ctx)) {
727 case EVP_PKEY_STATE_PROVIDER:
728 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
729 && ctx->op.kex.exchange != NULL
730 && ctx->op.kex.exchange->get_ctx_params != NULL)
731 return
732 ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx,
733 params);
734 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
735 && ctx->op.sig.signature != NULL
736 && ctx->op.sig.signature->get_ctx_params != NULL)
737 return
738 ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx,
739 params);
740 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
741 && ctx->op.ciph.cipher != NULL
742 && ctx->op.ciph.cipher->get_ctx_params != NULL)
743 return
744 ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx,
745 params);
746 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
747 && ctx->op.encap.kem != NULL
748 && ctx->op.encap.kem->get_ctx_params != NULL)
749 return
750 ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx,
751 params);
752 break;
753 #ifndef FIPS_MODULE
754 case EVP_PKEY_STATE_UNKNOWN:
755 case EVP_PKEY_STATE_LEGACY:
756 return evp_pkey_ctx_get_params_to_ctrl(ctx, params);
757 #endif
758 }
759 return 0;
760 }
761
762 #ifndef FIPS_MODULE
763 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx)
764 {
765 void *provctx;
766
767 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
768 && ctx->op.kex.exchange != NULL
769 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
770 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
771 return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx,
772 provctx);
773 }
774 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
775 && ctx->op.sig.signature != NULL
776 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
777 provctx = ossl_provider_ctx(
778 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
779 return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx,
780 provctx);
781 }
782 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
783 && ctx->op.ciph.cipher != NULL
784 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
785 provctx = ossl_provider_ctx(
786 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
787 return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx,
788 provctx);
789 }
790 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
791 && ctx->op.encap.kem != NULL
792 && ctx->op.encap.kem->gettable_ctx_params != NULL) {
793 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
794 return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx,
795 provctx);
796 }
797 return NULL;
798 }
799
800 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx)
801 {
802 void *provctx;
803
804 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
805 && ctx->op.kex.exchange != NULL
806 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
807 provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange));
808 return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx,
809 provctx);
810 }
811 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
812 && ctx->op.sig.signature != NULL
813 && ctx->op.sig.signature->settable_ctx_params != NULL) {
814 provctx = ossl_provider_ctx(
815 EVP_SIGNATURE_get0_provider(ctx->op.sig.signature));
816 return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx,
817 provctx);
818 }
819 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
820 && ctx->op.ciph.cipher != NULL
821 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
822 provctx = ossl_provider_ctx(
823 EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher));
824 return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx,
825 provctx);
826 }
827 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
828 && ctx->keymgmt != NULL
829 && ctx->keymgmt->gen_settable_params != NULL) {
830 provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt));
831 return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx,
832 provctx);
833 }
834 if (EVP_PKEY_CTX_IS_KEM_OP(ctx)
835 && ctx->op.encap.kem != NULL
836 && ctx->op.encap.kem->settable_ctx_params != NULL) {
837 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem));
838 return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx,
839 provctx);
840 }
841 return NULL;
842 }
843
844 /*
845 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
846 *
847 * Return 1 on success, 0 or negative for errors.
848 *
849 * In particular they return -2 if any of the params is not supported.
850 *
851 * They are not available in FIPS_MODULE as they depend on
852 * - EVP_PKEY_CTX_{get,set}_params()
853 * - EVP_PKEY_CTX_{gettable,settable}_params()
854 *
855 */
856 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
857 {
858 if (ctx == NULL || params == NULL)
859 return 0;
860
861 /*
862 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
863 * depend on the translation that happens in EVP_PKEY_CTX_set_params()
864 * call, and that the resulting ctrl call will return -2 if it doesn't
865 * known the ctrl command number.
866 */
867 if (evp_pkey_ctx_is_provided(ctx)) {
868 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
869 const OSSL_PARAM *p;
870
871 for (p = params; p->key != NULL; p++) {
872 /* Check the ctx actually understands this parameter */
873 if (OSSL_PARAM_locate_const(settable, p->key) == NULL)
874 return -2;
875 }
876 }
877
878 return EVP_PKEY_CTX_set_params(ctx, params);
879 }
880
881 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
882 {
883 if (ctx == NULL || params == NULL)
884 return 0;
885
886 /*
887 * We only check for provider side EVP_PKEY_CTX. For #legacy, we
888 * depend on the translation that happens in EVP_PKEY_CTX_get_params()
889 * call, and that the resulting ctrl call will return -2 if it doesn't
890 * known the ctrl command number.
891 */
892 if (evp_pkey_ctx_is_provided(ctx)) {
893 const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx);
894 const OSSL_PARAM *p;
895
896 for (p = params; p->key != NULL; p++) {
897 /* Check the ctx actually understands this parameter */
898 if (OSSL_PARAM_locate_const(gettable, p->key) == NULL)
899 return -2;
900 }
901 }
902
903 return EVP_PKEY_CTX_get_params(ctx, params);
904 }
905
906 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
907 {
908 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
909 /* 80 should be big enough */
910 char name[80] = "";
911 const EVP_MD *tmp;
912
913 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
914 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
915 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
916 return -2;
917 }
918
919 if (ctx->op.sig.algctx == NULL)
920 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
921 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
922
923 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
924 name,
925 sizeof(name));
926 *p = OSSL_PARAM_construct_end();
927
928 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
929 return 0;
930
931 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
932 if (tmp == NULL)
933 return 0;
934
935 *md = tmp;
936
937 return 1;
938 }
939
940 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
941 int fallback, const char *param, int op,
942 int ctrl)
943 {
944 OSSL_PARAM md_params[2], *p = md_params;
945 const char *name;
946
947 if (ctx == NULL || (ctx->operation & op) == 0) {
948 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
949 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
950 return -2;
951 }
952
953 if (fallback)
954 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
955
956 if (md == NULL) {
957 name = "";
958 } else {
959 name = EVP_MD_get0_name(md);
960 }
961
962 *p++ = OSSL_PARAM_construct_utf8_string(param,
963 /*
964 * Cast away the const. This is read
965 * only so should be safe
966 */
967 (char *)name, 0);
968 *p = OSSL_PARAM_construct_end();
969
970 return EVP_PKEY_CTX_set_params(ctx, md_params);
971 }
972
973 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
974 {
975 return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL,
976 OSSL_SIGNATURE_PARAM_DIGEST,
977 EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
978 }
979
980 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
981 {
982 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
983 OSSL_KDF_PARAM_DIGEST,
984 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
985 }
986
987 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
988 const char *param, int op, int ctrl,
989 const unsigned char *data,
990 int datalen)
991 {
992 OSSL_PARAM octet_string_params[2], *p = octet_string_params;
993
994 if (ctx == NULL || (ctx->operation & op) == 0) {
995 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
996 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
997 return -2;
998 }
999
1000 /* Code below to be removed when legacy support is dropped. */
1001 if (fallback)
1002 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
1003 /* end of legacy support */
1004
1005 if (datalen < 0) {
1006 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
1007 return 0;
1008 }
1009
1010 *p++ = OSSL_PARAM_construct_octet_string(param,
1011 /*
1012 * Cast away the const. This is read
1013 * only so should be safe
1014 */
1015 (unsigned char *)data,
1016 (size_t)datalen);
1017 *p = OSSL_PARAM_construct_end();
1018
1019 return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
1020 }
1021
1022 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
1023 const unsigned char *sec, int seclen)
1024 {
1025 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1026 OSSL_KDF_PARAM_SECRET,
1027 EVP_PKEY_OP_DERIVE,
1028 EVP_PKEY_CTRL_TLS_SECRET,
1029 sec, seclen);
1030 }
1031
1032 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
1033 const unsigned char *seed, int seedlen)
1034 {
1035 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1036 OSSL_KDF_PARAM_SEED,
1037 EVP_PKEY_OP_DERIVE,
1038 EVP_PKEY_CTRL_TLS_SEED,
1039 seed, seedlen);
1040 }
1041
1042 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1043 {
1044 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL,
1045 OSSL_KDF_PARAM_DIGEST,
1046 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
1047 }
1048
1049 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
1050 const unsigned char *salt, int saltlen)
1051 {
1052 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1053 OSSL_KDF_PARAM_SALT,
1054 EVP_PKEY_OP_DERIVE,
1055 EVP_PKEY_CTRL_HKDF_SALT,
1056 salt, saltlen);
1057 }
1058
1059 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
1060 const unsigned char *key, int keylen)
1061 {
1062 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1063 OSSL_KDF_PARAM_KEY,
1064 EVP_PKEY_OP_DERIVE,
1065 EVP_PKEY_CTRL_HKDF_KEY,
1066 key, keylen);
1067 }
1068
1069 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
1070 const unsigned char *info, int infolen)
1071 {
1072 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1073 OSSL_KDF_PARAM_INFO,
1074 EVP_PKEY_OP_DERIVE,
1075 EVP_PKEY_CTRL_HKDF_INFO,
1076 info, infolen);
1077 }
1078
1079 int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
1080 {
1081 OSSL_PARAM int_params[2], *p = int_params;
1082
1083 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1084 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1085 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1086 return -2;
1087 }
1088
1089 /* Code below to be removed when legacy support is dropped. */
1090 if (ctx->op.kex.algctx == NULL)
1091 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1092 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1093 /* end of legacy support */
1094
1095 if (mode < 0) {
1096 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1097 return 0;
1098 }
1099
1100 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1101 *p = OSSL_PARAM_construct_end();
1102
1103 return EVP_PKEY_CTX_set_params(ctx, int_params);
1104 }
1105
1106 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1107 int passlen)
1108 {
1109 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1110 OSSL_KDF_PARAM_PASSWORD,
1111 EVP_PKEY_OP_DERIVE,
1112 EVP_PKEY_CTRL_PASS,
1113 (const unsigned char *)pass, passlen);
1114 }
1115
1116 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1117 const unsigned char *salt, int saltlen)
1118 {
1119 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL,
1120 OSSL_KDF_PARAM_SALT,
1121 EVP_PKEY_OP_DERIVE,
1122 EVP_PKEY_CTRL_SCRYPT_SALT,
1123 salt, saltlen);
1124 }
1125
1126 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1127 int op, int ctrl, uint64_t val)
1128 {
1129 OSSL_PARAM uint64_params[2], *p = uint64_params;
1130
1131 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1132 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1133 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1134 return -2;
1135 }
1136
1137 /* Code below to be removed when legacy support is dropped. */
1138 if (ctx->op.kex.algctx == NULL)
1139 return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1140 /* end of legacy support */
1141
1142 *p++ = OSSL_PARAM_construct_uint64(param, &val);
1143 *p = OSSL_PARAM_construct_end();
1144
1145 return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1146 }
1147
1148 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1149 {
1150 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1151 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1152 n);
1153 }
1154
1155 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1156 {
1157 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1158 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1159 r);
1160 }
1161
1162 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1163 {
1164 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1165 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1166 p);
1167 }
1168
1169 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1170 uint64_t maxmem_bytes)
1171 {
1172 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1173 EVP_PKEY_OP_DERIVE,
1174 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1175 maxmem_bytes);
1176 }
1177
1178 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1179 int keylen)
1180 {
1181 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1182 OSSL_PKEY_PARAM_PRIV_KEY,
1183 EVP_PKEY_OP_KEYGEN,
1184 EVP_PKEY_CTRL_SET_MAC_KEY,
1185 key, keylen);
1186 }
1187
1188 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op)
1189 {
1190 OSSL_PARAM params[2], *p = params;
1191
1192 if (ctx == NULL || op == NULL) {
1193 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1194 return 0;
1195 }
1196 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1197 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1198 return -2;
1199 }
1200 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
1201 (char *)op, 0);
1202 *p = OSSL_PARAM_construct_end();
1203 return EVP_PKEY_CTX_set_params(ctx, params);
1204 }
1205
1206 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len)
1207 {
1208 OSSL_PARAM params[2], *p = params;
1209 int ret;
1210
1211 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1212 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1213 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1214 return -2;
1215 }
1216
1217 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID,
1218 /*
1219 * Cast away the const. This is
1220 * read only so should be safe
1221 */
1222 (void *)id, (size_t)len);
1223 *p++ = OSSL_PARAM_construct_end();
1224
1225 ret = evp_pkey_ctx_set_params_strict(ctx, params);
1226 if (ret == -2)
1227 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1228 return ret;
1229 }
1230
1231 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len)
1232 {
1233 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1234 EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id));
1235 }
1236
1237 static int get1_id_data(EVP_PKEY_CTX *ctx, void *id, size_t *id_len)
1238 {
1239 int ret;
1240 void *tmp_id = NULL;
1241 OSSL_PARAM params[2], *p = params;
1242
1243 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1244 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1245 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1246 return -2;
1247 }
1248
1249 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID,
1250 &tmp_id, 0);
1251 *p++ = OSSL_PARAM_construct_end();
1252
1253 ret = evp_pkey_ctx_get_params_strict(ctx, params);
1254 if (ret == -2) {
1255 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1256 } else if (ret > 0) {
1257 size_t tmp_id_len = params[0].return_size;
1258
1259 if (id != NULL)
1260 memcpy(id, tmp_id, tmp_id_len);
1261 if (id_len != NULL)
1262 *id_len = tmp_id_len;
1263 }
1264 return ret;
1265 }
1266
1267 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id)
1268 {
1269 return get1_id_data(ctx, id, NULL);
1270 }
1271
1272 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len)
1273 {
1274 return get1_id_data(ctx, NULL, id_len);
1275 }
1276
1277 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id)
1278 {
1279 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id);
1280 }
1281
1282 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len)
1283 {
1284 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1285 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len);
1286 }
1287
1288 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1289 int cmd, int p1, void *p2)
1290 {
1291 int ret = 0;
1292
1293 /*
1294 * If the method has a |digest_custom| function, we can relax the
1295 * operation type check, since this can be called before the operation
1296 * is initialized.
1297 */
1298 if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1299 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1300 ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET);
1301 return -1;
1302 }
1303
1304 if ((optype != -1) && !(ctx->operation & optype)) {
1305 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1306 return -1;
1307 }
1308 }
1309
1310 switch (evp_pkey_ctx_state(ctx)) {
1311 case EVP_PKEY_STATE_PROVIDER:
1312 return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1313 case EVP_PKEY_STATE_UNKNOWN:
1314 case EVP_PKEY_STATE_LEGACY:
1315 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1316 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1317 return -2;
1318 }
1319 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1320 return -1;
1321
1322 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1323
1324 if (ret == -2)
1325 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1326 break;
1327 }
1328 return ret;
1329 }
1330
1331 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1332 int cmd, int p1, void *p2)
1333 {
1334 int ret = 0;
1335
1336 if (ctx == NULL) {
1337 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1338 return -2;
1339 }
1340 /* If unsupported, we don't want that reported here */
1341 ERR_set_mark();
1342 ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1343 cmd, NULL, p2, p1);
1344 if (ret == -2) {
1345 ERR_pop_to_mark();
1346 } else {
1347 ERR_clear_last_mark();
1348 /*
1349 * If there was an error, there was an error.
1350 * If the operation isn't initialized yet, we also return, as
1351 * the saved values will be used then anyway.
1352 */
1353 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1354 return ret;
1355 }
1356 return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1357 }
1358
1359 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1360 int cmd, uint64_t value)
1361 {
1362 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1363 }
1364
1365
1366 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1367 const char *name, const char *value)
1368 {
1369 int ret = 0;
1370
1371 if (ctx == NULL) {
1372 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1373 return -2;
1374 }
1375
1376 switch (evp_pkey_ctx_state(ctx)) {
1377 case EVP_PKEY_STATE_PROVIDER:
1378 return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value);
1379 case EVP_PKEY_STATE_UNKNOWN:
1380 case EVP_PKEY_STATE_LEGACY:
1381 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1382 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1383 return -2;
1384 }
1385 if (strcmp(name, "digest") == 0)
1386 ret = EVP_PKEY_CTX_md(ctx,
1387 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1388 EVP_PKEY_CTRL_MD, value);
1389 else
1390 ret = ctx->pmeth->ctrl_str(ctx, name, value);
1391 break;
1392 }
1393
1394 return ret;
1395 }
1396
1397 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1398 const char *name, const char *value)
1399 {
1400 int ret = 0;
1401
1402 /* If unsupported, we don't want that reported here */
1403 ERR_set_mark();
1404 ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1405 name, value, strlen(value) + 1);
1406 if (ret == -2) {
1407 ERR_pop_to_mark();
1408 } else {
1409 ERR_clear_last_mark();
1410 /*
1411 * If there was an error, there was an error.
1412 * If the operation isn't initialized yet, we also return, as
1413 * the saved values will be used then anyway.
1414 */
1415 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1416 return ret;
1417 }
1418
1419 return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1420 }
1421
1422 static int decode_cmd(int cmd, const char *name)
1423 {
1424 if (cmd == -1) {
1425 /*
1426 * The consequence of the assertion not being true is that this
1427 * function will return -1, which will cause the calling functions
1428 * to signal that the command is unsupported... in non-debug mode.
1429 */
1430 if (ossl_assert(name != NULL))
1431 if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1432 cmd = EVP_PKEY_CTRL_SET1_ID;
1433 }
1434
1435 return cmd;
1436 }
1437
1438 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1439 int keytype, int optype,
1440 int cmd, const char *name,
1441 const void *data, size_t data_len)
1442 {
1443 /*
1444 * Check that it's one of the supported commands. The ctrl commands
1445 * number cases here must correspond to the cases in the bottom switch
1446 * in this function.
1447 */
1448 switch (cmd = decode_cmd(cmd, name)) {
1449 case EVP_PKEY_CTRL_SET1_ID:
1450 break;
1451 default:
1452 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1453 return -2;
1454 }
1455
1456 if (keytype != -1) {
1457 switch (evp_pkey_ctx_state(ctx)) {
1458 case EVP_PKEY_STATE_PROVIDER:
1459 if (ctx->keymgmt == NULL) {
1460 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1461 return -2;
1462 }
1463 if (!EVP_KEYMGMT_is_a(ctx->keymgmt,
1464 evp_pkey_type2name(keytype))) {
1465 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1466 return -1;
1467 }
1468 break;
1469 case EVP_PKEY_STATE_UNKNOWN:
1470 case EVP_PKEY_STATE_LEGACY:
1471 if (ctx->pmeth == NULL) {
1472 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1473 return -2;
1474 }
1475 if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) {
1476 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1477 return -1;
1478 }
1479 break;
1480 }
1481 }
1482 if (optype != -1 && (ctx->operation & optype) == 0) {
1483 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1484 return -1;
1485 }
1486
1487 switch (cmd) {
1488 case EVP_PKEY_CTRL_SET1_ID:
1489 evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1490 if (name != NULL) {
1491 ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1492 if (ctx->cached_parameters.dist_id_name == NULL)
1493 return 0;
1494 }
1495 if (data_len > 0) {
1496 ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1497 if (ctx->cached_parameters.dist_id == NULL)
1498 return 0;
1499 }
1500 ctx->cached_parameters.dist_id_set = 1;
1501 ctx->cached_parameters.dist_id_len = data_len;
1502 break;
1503 }
1504 return 1;
1505 }
1506
1507 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1508 int cmd, const char *name)
1509 {
1510 cmd = decode_cmd(cmd, name);
1511 switch (cmd) {
1512 case EVP_PKEY_CTRL_SET1_ID:
1513 OPENSSL_free(ctx->cached_parameters.dist_id);
1514 OPENSSL_free(ctx->cached_parameters.dist_id_name);
1515 ctx->cached_parameters.dist_id = NULL;
1516 ctx->cached_parameters.dist_id_name = NULL;
1517 break;
1518 }
1519 }
1520
1521 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1522 {
1523 evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1524 }
1525
1526 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1527 {
1528 int ret = 1;
1529
1530 if (ret && ctx->cached_parameters.dist_id_set) {
1531 const char *name = ctx->cached_parameters.dist_id_name;
1532 const void *val = ctx->cached_parameters.dist_id;
1533 size_t len = ctx->cached_parameters.dist_id_len;
1534
1535 if (name != NULL)
1536 ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1537 else
1538 ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1539 EVP_PKEY_CTRL_SET1_ID,
1540 (int)len, (void *)val);
1541 }
1542
1543 return ret;
1544 }
1545
1546 OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx)
1547 {
1548 return ctx->libctx;
1549 }
1550
1551 const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx)
1552 {
1553 return ctx->propquery;
1554 }
1555
1556 const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx)
1557 {
1558 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
1559 if (ctx->op.sig.signature != NULL)
1560 return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature);
1561 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1562 if (ctx->op.kex.exchange != NULL)
1563 return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange);
1564 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) {
1565 if (ctx->op.encap.kem != NULL)
1566 return EVP_KEM_get0_provider(ctx->op.encap.kem);
1567 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1568 if (ctx->op.ciph.cipher != NULL)
1569 return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher);
1570 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1571 if (ctx->keymgmt != NULL)
1572 return EVP_KEYMGMT_get0_provider(ctx->keymgmt);
1573 }
1574
1575 return NULL;
1576 }
1577
1578 /* Utility functions to send a string of hex string to a ctrl */
1579
1580 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1581 {
1582 size_t len;
1583
1584 len = strlen(str);
1585 if (len > INT_MAX)
1586 return -1;
1587 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1588 }
1589
1590 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1591 {
1592 unsigned char *bin;
1593 long binlen;
1594 int rv = -1;
1595
1596 bin = OPENSSL_hexstr2buf(hex, &binlen);
1597 if (bin == NULL)
1598 return 0;
1599 if (binlen <= INT_MAX)
1600 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1601 OPENSSL_free(bin);
1602 return rv;
1603 }
1604
1605 /* Pass a message digest to a ctrl */
1606 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1607 {
1608 const EVP_MD *m;
1609
1610 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1611 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
1612 return 0;
1613 }
1614 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1615 }
1616
1617 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1618 {
1619 return ctx->operation;
1620 }
1621
1622 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1623 {
1624 ctx->keygen_info = dat;
1625 ctx->keygen_info_count = datlen;
1626 }
1627
1628 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1629 {
1630 ctx->data = data;
1631 }
1632
1633 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1634 {
1635 return ctx->data;
1636 }
1637
1638 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1639 {
1640 return ctx->pkey;
1641 }
1642
1643 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1644 {
1645 return ctx->peerkey;
1646 }
1647
1648 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1649 {
1650 ctx->app_data = data;
1651 }
1652
1653 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1654 {
1655 return ctx->app_data;
1656 }
1657
1658 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1659 int (*init) (EVP_PKEY_CTX *ctx))
1660 {
1661 pmeth->init = init;
1662 }
1663
1664 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1665 int (*copy) (EVP_PKEY_CTX *dst,
1666 const EVP_PKEY_CTX *src))
1667 {
1668 pmeth->copy = copy;
1669 }
1670
1671 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1672 void (*cleanup) (EVP_PKEY_CTX *ctx))
1673 {
1674 pmeth->cleanup = cleanup;
1675 }
1676
1677 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1678 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1679 int (*paramgen) (EVP_PKEY_CTX *ctx,
1680 EVP_PKEY *pkey))
1681 {
1682 pmeth->paramgen_init = paramgen_init;
1683 pmeth->paramgen = paramgen;
1684 }
1685
1686 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1687 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1688 int (*keygen) (EVP_PKEY_CTX *ctx,
1689 EVP_PKEY *pkey))
1690 {
1691 pmeth->keygen_init = keygen_init;
1692 pmeth->keygen = keygen;
1693 }
1694
1695 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1696 int (*sign_init) (EVP_PKEY_CTX *ctx),
1697 int (*sign) (EVP_PKEY_CTX *ctx,
1698 unsigned char *sig, size_t *siglen,
1699 const unsigned char *tbs,
1700 size_t tbslen))
1701 {
1702 pmeth->sign_init = sign_init;
1703 pmeth->sign = sign;
1704 }
1705
1706 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1707 int (*verify_init) (EVP_PKEY_CTX *ctx),
1708 int (*verify) (EVP_PKEY_CTX *ctx,
1709 const unsigned char *sig,
1710 size_t siglen,
1711 const unsigned char *tbs,
1712 size_t tbslen))
1713 {
1714 pmeth->verify_init = verify_init;
1715 pmeth->verify = verify;
1716 }
1717
1718 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1719 int (*verify_recover_init) (EVP_PKEY_CTX
1720 *ctx),
1721 int (*verify_recover) (EVP_PKEY_CTX
1722 *ctx,
1723 unsigned char
1724 *sig,
1725 size_t *siglen,
1726 const unsigned
1727 char *tbs,
1728 size_t tbslen))
1729 {
1730 pmeth->verify_recover_init = verify_recover_init;
1731 pmeth->verify_recover = verify_recover;
1732 }
1733
1734 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1735 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1736 EVP_MD_CTX *mctx),
1737 int (*signctx) (EVP_PKEY_CTX *ctx,
1738 unsigned char *sig,
1739 size_t *siglen,
1740 EVP_MD_CTX *mctx))
1741 {
1742 pmeth->signctx_init = signctx_init;
1743 pmeth->signctx = signctx;
1744 }
1745
1746 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1747 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1748 EVP_MD_CTX *mctx),
1749 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1750 const unsigned char *sig,
1751 int siglen,
1752 EVP_MD_CTX *mctx))
1753 {
1754 pmeth->verifyctx_init = verifyctx_init;
1755 pmeth->verifyctx = verifyctx;
1756 }
1757
1758 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1759 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1760 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1761 unsigned char *out,
1762 size_t *outlen,
1763 const unsigned char *in,
1764 size_t inlen))
1765 {
1766 pmeth->encrypt_init = encrypt_init;
1767 pmeth->encrypt = encryptfn;
1768 }
1769
1770 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1771 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1772 int (*decrypt) (EVP_PKEY_CTX *ctx,
1773 unsigned char *out,
1774 size_t *outlen,
1775 const unsigned char *in,
1776 size_t inlen))
1777 {
1778 pmeth->decrypt_init = decrypt_init;
1779 pmeth->decrypt = decrypt;
1780 }
1781
1782 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1783 int (*derive_init) (EVP_PKEY_CTX *ctx),
1784 int (*derive) (EVP_PKEY_CTX *ctx,
1785 unsigned char *key,
1786 size_t *keylen))
1787 {
1788 pmeth->derive_init = derive_init;
1789 pmeth->derive = derive;
1790 }
1791
1792 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1793 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1794 void *p2),
1795 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1796 const char *type,
1797 const char *value))
1798 {
1799 pmeth->ctrl = ctrl;
1800 pmeth->ctrl_str = ctrl_str;
1801 }
1802
1803 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1804 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1805 const unsigned char *tbs, size_t tbslen))
1806 {
1807 pmeth->digestsign = digestsign;
1808 }
1809
1810 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1811 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1812 size_t siglen, const unsigned char *tbs,
1813 size_t tbslen))
1814 {
1815 pmeth->digestverify = digestverify;
1816 }
1817
1818 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1819 int (*check) (EVP_PKEY *pkey))
1820 {
1821 pmeth->check = check;
1822 }
1823
1824 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1825 int (*check) (EVP_PKEY *pkey))
1826 {
1827 pmeth->public_check = check;
1828 }
1829
1830 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1831 int (*check) (EVP_PKEY *pkey))
1832 {
1833 pmeth->param_check = check;
1834 }
1835
1836 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1837 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1838 EVP_MD_CTX *mctx))
1839 {
1840 pmeth->digest_custom = digest_custom;
1841 }
1842
1843 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1844 int (**pinit) (EVP_PKEY_CTX *ctx))
1845 {
1846 *pinit = pmeth->init;
1847 }
1848
1849 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1850 int (**pcopy) (EVP_PKEY_CTX *dst,
1851 const EVP_PKEY_CTX *src))
1852 {
1853 *pcopy = pmeth->copy;
1854 }
1855
1856 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1857 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1858 {
1859 *pcleanup = pmeth->cleanup;
1860 }
1861
1862 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1863 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1864 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1865 EVP_PKEY *pkey))
1866 {
1867 if (pparamgen_init)
1868 *pparamgen_init = pmeth->paramgen_init;
1869 if (pparamgen)
1870 *pparamgen = pmeth->paramgen;
1871 }
1872
1873 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1874 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1875 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1876 EVP_PKEY *pkey))
1877 {
1878 if (pkeygen_init)
1879 *pkeygen_init = pmeth->keygen_init;
1880 if (pkeygen)
1881 *pkeygen = pmeth->keygen;
1882 }
1883
1884 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1885 int (**psign_init) (EVP_PKEY_CTX *ctx),
1886 int (**psign) (EVP_PKEY_CTX *ctx,
1887 unsigned char *sig, size_t *siglen,
1888 const unsigned char *tbs,
1889 size_t tbslen))
1890 {
1891 if (psign_init)
1892 *psign_init = pmeth->sign_init;
1893 if (psign)
1894 *psign = pmeth->sign;
1895 }
1896
1897 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1898 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1899 int (**pverify) (EVP_PKEY_CTX *ctx,
1900 const unsigned char *sig,
1901 size_t siglen,
1902 const unsigned char *tbs,
1903 size_t tbslen))
1904 {
1905 if (pverify_init)
1906 *pverify_init = pmeth->verify_init;
1907 if (pverify)
1908 *pverify = pmeth->verify;
1909 }
1910
1911 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1912 int (**pverify_recover_init) (EVP_PKEY_CTX
1913 *ctx),
1914 int (**pverify_recover) (EVP_PKEY_CTX
1915 *ctx,
1916 unsigned char
1917 *sig,
1918 size_t *siglen,
1919 const unsigned
1920 char *tbs,
1921 size_t tbslen))
1922 {
1923 if (pverify_recover_init)
1924 *pverify_recover_init = pmeth->verify_recover_init;
1925 if (pverify_recover)
1926 *pverify_recover = pmeth->verify_recover;
1927 }
1928
1929 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1930 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1931 EVP_MD_CTX *mctx),
1932 int (**psignctx) (EVP_PKEY_CTX *ctx,
1933 unsigned char *sig,
1934 size_t *siglen,
1935 EVP_MD_CTX *mctx))
1936 {
1937 if (psignctx_init)
1938 *psignctx_init = pmeth->signctx_init;
1939 if (psignctx)
1940 *psignctx = pmeth->signctx;
1941 }
1942
1943 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1944 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1945 EVP_MD_CTX *mctx),
1946 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1947 const unsigned char *sig,
1948 int siglen,
1949 EVP_MD_CTX *mctx))
1950 {
1951 if (pverifyctx_init)
1952 *pverifyctx_init = pmeth->verifyctx_init;
1953 if (pverifyctx)
1954 *pverifyctx = pmeth->verifyctx;
1955 }
1956
1957 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1958 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1959 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1960 unsigned char *out,
1961 size_t *outlen,
1962 const unsigned char *in,
1963 size_t inlen))
1964 {
1965 if (pencrypt_init)
1966 *pencrypt_init = pmeth->encrypt_init;
1967 if (pencryptfn)
1968 *pencryptfn = pmeth->encrypt;
1969 }
1970
1971 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1972 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1973 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1974 unsigned char *out,
1975 size_t *outlen,
1976 const unsigned char *in,
1977 size_t inlen))
1978 {
1979 if (pdecrypt_init)
1980 *pdecrypt_init = pmeth->decrypt_init;
1981 if (pdecrypt)
1982 *pdecrypt = pmeth->decrypt;
1983 }
1984
1985 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1986 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1987 int (**pderive) (EVP_PKEY_CTX *ctx,
1988 unsigned char *key,
1989 size_t *keylen))
1990 {
1991 if (pderive_init)
1992 *pderive_init = pmeth->derive_init;
1993 if (pderive)
1994 *pderive = pmeth->derive;
1995 }
1996
1997 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1998 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1999 void *p2),
2000 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
2001 const char *type,
2002 const char *value))
2003 {
2004 if (pctrl)
2005 *pctrl = pmeth->ctrl;
2006 if (pctrl_str)
2007 *pctrl_str = pmeth->ctrl_str;
2008 }
2009
2010 void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth,
2011 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2012 const unsigned char *tbs, size_t tbslen))
2013 {
2014 if (digestsign)
2015 *digestsign = pmeth->digestsign;
2016 }
2017
2018 void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth,
2019 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2020 size_t siglen, const unsigned char *tbs,
2021 size_t tbslen))
2022 {
2023 if (digestverify)
2024 *digestverify = pmeth->digestverify;
2025 }
2026
2027 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2028 int (**pcheck) (EVP_PKEY *pkey))
2029 {
2030 if (pcheck != NULL)
2031 *pcheck = pmeth->check;
2032 }
2033
2034 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2035 int (**pcheck) (EVP_PKEY *pkey))
2036 {
2037 if (pcheck != NULL)
2038 *pcheck = pmeth->public_check;
2039 }
2040
2041 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2042 int (**pcheck) (EVP_PKEY *pkey))
2043 {
2044 if (pcheck != NULL)
2045 *pcheck = pmeth->param_check;
2046 }
2047
2048 void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth,
2049 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2050 EVP_MD_CTX *mctx))
2051 {
2052 if (pdigest_custom != NULL)
2053 *pdigest_custom = pmeth->digest_custom;
2054 }
2055
2056 #endif /* FIPS_MODULE */