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