VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.3/crypto/evp/pmeth_lib.c@ 95219

Last change on this file since 95219 was 95219, checked in by vboxsync, 3 years ago

libs/openssl: Switched to v3.0.3, bugref:10128

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette