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