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