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