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