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