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