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