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