]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_lib.c
Implement a EVP_PKEY KDF to KDF provider bridge
[thirdparty/openssl.git] / crypto / evp / pmeth_lib.c
1 /*
2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * Low level key APIs (DH etc) are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <openssl/engine.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/core_names.h>
22 #include <openssl/dh.h>
23 #include <openssl/rsa.h>
24 #include <openssl/kdf.h>
25 #include "internal/cryptlib.h"
26 #include "crypto/asn1.h"
27 #include "crypto/evp.h"
28 #include "crypto/dh.h"
29 #include "internal/ffc.h"
30 #include "internal/numbers.h"
31 #include "internal/provider.h"
32 #include "evp_local.h"
33
34 #ifndef FIPS_MODULE
35
36 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
37 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
38
39 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
40
41 /* This array needs to be in order of NIDs */
42 static pmeth_fn standard_methods[] = {
43 # ifndef OPENSSL_NO_RSA
44 rsa_pkey_method,
45 # endif
46 # ifndef OPENSSL_NO_DH
47 dh_pkey_method,
48 # endif
49 # ifndef OPENSSL_NO_DSA
50 dsa_pkey_method,
51 # endif
52 # ifndef OPENSSL_NO_EC
53 ec_pkey_method,
54 # endif
55 hmac_pkey_method,
56 # ifndef OPENSSL_NO_CMAC
57 cmac_pkey_method,
58 # endif
59 # ifndef OPENSSL_NO_RSA
60 rsa_pss_pkey_method,
61 # endif
62 # ifndef OPENSSL_NO_DH
63 dhx_pkey_method,
64 # endif
65 # ifndef OPENSSL_NO_SCRYPT
66 scrypt_pkey_method,
67 # endif
68 tls1_prf_pkey_method,
69 # ifndef OPENSSL_NO_EC
70 ecx25519_pkey_method,
71 ecx448_pkey_method,
72 # endif
73 hkdf_pkey_method,
74 # ifndef OPENSSL_NO_POLY1305
75 poly1305_pkey_method,
76 # endif
77 # ifndef OPENSSL_NO_SIPHASH
78 siphash_pkey_method,
79 # endif
80 # ifndef OPENSSL_NO_EC
81 ed25519_pkey_method,
82 ed448_pkey_method,
83 # endif
84 # ifndef OPENSSL_NO_SM2
85 sm2_pkey_method,
86 # endif
87 };
88
89 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
90
91 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
92 {
93 return ((*a)->pkey_id - ((**b)())->pkey_id);
94 }
95
96 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
97
98 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
99 const EVP_PKEY_METHOD *const *b)
100 {
101 return ((*a)->pkey_id - (*b)->pkey_id);
102 }
103
104 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
105 {
106 pmeth_fn *ret;
107 EVP_PKEY_METHOD tmp;
108 const EVP_PKEY_METHOD *t = &tmp;
109
110 tmp.pkey_id = type;
111 if (app_pkey_methods) {
112 int idx;
113 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
114 if (idx >= 0)
115 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
116 }
117 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
118 sizeof(standard_methods) /
119 sizeof(pmeth_fn));
120 if (ret == NULL || *ret == NULL)
121 return NULL;
122 return (**ret)();
123 }
124
125 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
126 {
127 EVP_PKEY_METHOD *pmeth;
128
129 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
130 if (pmeth == NULL) {
131 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
132 return NULL;
133 }
134
135 pmeth->pkey_id = id;
136 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
137 return pmeth;
138 }
139 #endif /* FIPS_MODULE */
140
141 static int is_legacy_alg(int id, const char *keytype)
142 {
143 #ifndef FIPS_MODULE
144 /* Certain EVP_PKEY keytypes are only available in legacy form */
145 if (id == -1) {
146 id = OBJ_sn2nid(keytype);
147 if (id == NID_undef)
148 id = OBJ_ln2nid(keytype);
149 if (id == NID_undef)
150 return 0;
151 }
152 switch (id) {
153 /*
154 * TODO(3.0): Remove SM2 and DHX when they are converted to have provider
155 * support
156 */
157 case EVP_PKEY_SM2:
158 case EVP_PKEY_DHX:
159 case EVP_PKEY_SCRYPT:
160 case EVP_PKEY_HKDF:
161 case EVP_PKEY_CMAC:
162 case EVP_PKEY_HMAC:
163 case EVP_PKEY_SIPHASH:
164 case EVP_PKEY_POLY1305:
165 return 1;
166 default:
167 return 0;
168 }
169 #else
170 return 0;
171 #endif
172 }
173
174 static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
175 EVP_PKEY *pkey, ENGINE *e,
176 const char *keytype, const char *propquery,
177 int id)
178
179 {
180 EVP_PKEY_CTX *ret;
181 const EVP_PKEY_METHOD *pmeth = NULL;
182 EVP_KEYMGMT *keymgmt = NULL;
183
184 /*
185 * When using providers, the context is bound to the algo implementation
186 * later.
187 */
188 if (pkey == NULL && e == NULL && id == -1)
189 goto common;
190
191 /*
192 * If the internal key is provided, we extract the keytype from its
193 * keymgmt and skip over the legacy code.
194 */
195 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
196 /* If we have an engine, something went wrong somewhere... */
197 if (!ossl_assert(e == NULL))
198 return NULL;
199 keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
200 goto common;
201 }
202 #ifndef FIPS_MODULE
203 /* TODO(3.0) Legacy code should be removed when all is provider based */
204 /* BEGIN legacy */
205 if (id == -1) {
206 if (pkey == NULL)
207 return NULL;
208 id = pkey->type;
209 }
210
211 /*
212 * Here, we extract what information we can for the purpose of
213 * supporting usage with implementations from providers, to make
214 * for a smooth transition from legacy stuff to provider based stuff.
215 *
216 * If an engine is given, this is entirely legacy, and we should not
217 * pretend anything else, so we only set the name when no engine is
218 * given. If both are already given, someone made a mistake, and
219 * since that can only happen internally, it's safe to make an
220 * assertion.
221 */
222 if (!ossl_assert(e == NULL || keytype == NULL))
223 return NULL;
224 if (e == NULL)
225 keytype = OBJ_nid2sn(id);
226
227 # ifndef OPENSSL_NO_ENGINE
228 if (e == NULL && pkey != NULL)
229 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
230 /* Try to find an ENGINE which implements this method */
231 if (e) {
232 if (!ENGINE_init(e)) {
233 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
234 return NULL;
235 }
236 } else {
237 e = ENGINE_get_pkey_meth_engine(id);
238 }
239
240 /*
241 * If an ENGINE handled this method look it up. Otherwise use internal
242 * tables.
243 */
244 if (e != NULL) {
245 pmeth = ENGINE_get_pkey_meth(e, id);
246 else
247 # endif
248 pmeth = EVP_PKEY_meth_find(id);
249
250 if (pmeth == NULL) {
251 # ifndef OPENSSL_NO_ENGINE
252 ENGINE_finish(e);
253 # endif
254 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
255 return NULL;
256 }
257 /* END legacy */
258 #endif /* FIPS_MODULE */
259 common:
260 /*
261 * If there's no engine and there's a name, we try fetching a provider
262 * implementation.
263 */
264 if (e == NULL && keytype != NULL) {
265 int legacy = is_legacy_alg(id, keytype);
266
267 if (legacy) {
268 /* This could fail so ignore errors */
269 ERR_set_mark();
270 }
271
272 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
273 if (legacy) {
274 ERR_pop_to_mark();
275 } else if (keymgmt == NULL) {
276 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_FETCH_FAILED);
277 return NULL;
278 }
279 }
280
281 ret = OPENSSL_zalloc(sizeof(*ret));
282 if (ret == NULL) {
283 EVP_KEYMGMT_free(keymgmt);
284 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
285 ENGINE_finish(e);
286 #endif
287 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
288 return NULL;
289 }
290 ret->libctx = libctx;
291 ret->propquery = propquery;
292 ret->keytype = keytype;
293 ret->keymgmt = keymgmt;
294 ret->engine = e;
295 ret->pmeth = pmeth;
296 ret->operation = EVP_PKEY_OP_UNDEFINED;
297 ret->pkey = pkey;
298 if (pkey != NULL)
299 EVP_PKEY_up_ref(pkey);
300
301 if (pmeth != NULL && pmeth->init != NULL) {
302 if (pmeth->init(ret) <= 0) {
303 ret->pmeth = NULL;
304 EVP_PKEY_CTX_free(ret);
305 return NULL;
306 }
307 }
308
309 return ret;
310 }
311
312 /*- All methods below can also be used in FIPS_MODULE */
313
314 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OPENSSL_CTX *libctx,
315 const char *name,
316 const char *propquery)
317 {
318 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
319 }
320
321 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX *libctx, EVP_PKEY *pkey,
322 const char *propquery)
323 {
324 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
325 }
326
327 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
328 {
329 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
330 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
331 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
332 EVP_SIGNATURE_free(ctx->op.sig.signature);
333 ctx->op.sig.sigprovctx = NULL;
334 ctx->op.sig.signature = NULL;
335 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
336 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
337 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
338 EVP_KEYEXCH_free(ctx->op.kex.exchange);
339 ctx->op.kex.exchprovctx = NULL;
340 ctx->op.kex.exchange = NULL;
341 }
342 /* TODO(3.0): add dependancies and uncomment this when available for fips mode */
343 #ifndef FIPS_MODULE
344 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
345 if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
346 ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
347 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
348 ctx->op.ciph.ciphprovctx = NULL;
349 ctx->op.ciph.cipher = NULL;
350 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
351 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
352 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
353 }
354 #endif
355 }
356
357 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
358 {
359 if (ctx == NULL)
360 return;
361 if (ctx->pmeth && ctx->pmeth->cleanup)
362 ctx->pmeth->cleanup(ctx);
363
364 evp_pkey_ctx_free_old_ops(ctx);
365 EVP_KEYMGMT_free(ctx->keymgmt);
366
367 EVP_PKEY_free(ctx->pkey);
368 EVP_PKEY_free(ctx->peerkey);
369 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
370 ENGINE_finish(ctx->engine);
371 #endif
372 OPENSSL_free(ctx);
373 }
374
375 #ifndef FIPS_MODULE
376
377 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
378 const EVP_PKEY_METHOD *meth)
379 {
380 if (ppkey_id)
381 *ppkey_id = meth->pkey_id;
382 if (pflags)
383 *pflags = meth->flags;
384 }
385
386 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
387 {
388 int pkey_id = dst->pkey_id;
389 int flags = dst->flags;
390
391 *dst = *src;
392
393 /* We only copy the function pointers so restore the other values */
394 dst->pkey_id = pkey_id;
395 dst->flags = flags;
396 }
397
398 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
399 {
400 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
401 OPENSSL_free(pmeth);
402 }
403
404 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
405 {
406 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
407 }
408
409 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
410 {
411 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
412 }
413
414 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
415 {
416 EVP_PKEY_CTX *rctx;
417
418 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
419 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
420 && pctx->op.kex.exchprovctx == NULL)
421 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
422 && pctx->op.sig.sigprovctx == NULL)))
423 return NULL;
424 # ifndef OPENSSL_NO_ENGINE
425 /* Make sure it's safe to copy a pkey context using an ENGINE */
426 if (pctx->engine && !ENGINE_init(pctx->engine)) {
427 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
428 return 0;
429 }
430 # endif
431 rctx = OPENSSL_zalloc(sizeof(*rctx));
432 if (rctx == NULL) {
433 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
434 return NULL;
435 }
436
437 if (pctx->pkey != NULL)
438 EVP_PKEY_up_ref(pctx->pkey);
439 rctx->pkey = pctx->pkey;
440 rctx->operation = pctx->operation;
441 rctx->libctx = pctx->libctx;
442 rctx->keytype = pctx->keytype;
443 rctx->propquery = pctx->propquery;
444
445 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
446 if (pctx->op.kex.exchange != NULL) {
447 rctx->op.kex.exchange = pctx->op.kex.exchange;
448 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
449 OPENSSL_free(rctx);
450 return NULL;
451 }
452 }
453 if (pctx->op.kex.exchprovctx != NULL) {
454 if (!ossl_assert(pctx->op.kex.exchange != NULL))
455 return NULL;
456 rctx->op.kex.exchprovctx
457 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
458 if (rctx->op.kex.exchprovctx == NULL) {
459 EVP_KEYEXCH_free(rctx->op.kex.exchange);
460 OPENSSL_free(rctx);
461 return NULL;
462 }
463 return rctx;
464 }
465 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
466 if (pctx->op.sig.signature != NULL) {
467 rctx->op.sig.signature = pctx->op.sig.signature;
468 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
469 OPENSSL_free(rctx);
470 return NULL;
471 }
472 }
473 if (pctx->op.sig.sigprovctx != NULL) {
474 if (!ossl_assert(pctx->op.sig.signature != NULL))
475 return NULL;
476 rctx->op.sig.sigprovctx
477 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
478 if (rctx->op.sig.sigprovctx == NULL) {
479 EVP_SIGNATURE_free(rctx->op.sig.signature);
480 OPENSSL_free(rctx);
481 return NULL;
482 }
483 return rctx;
484 }
485 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
486 if (pctx->op.ciph.cipher != NULL) {
487 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
488 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
489 OPENSSL_free(rctx);
490 return NULL;
491 }
492 }
493 if (pctx->op.ciph.ciphprovctx != NULL) {
494 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
495 return NULL;
496 rctx->op.ciph.ciphprovctx
497 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
498 if (rctx->op.ciph.ciphprovctx == NULL) {
499 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
500 OPENSSL_free(rctx);
501 return NULL;
502 }
503 return rctx;
504 }
505 }
506
507 rctx->pmeth = pctx->pmeth;
508 # ifndef OPENSSL_NO_ENGINE
509 rctx->engine = pctx->engine;
510 # endif
511
512 if (pctx->peerkey)
513 EVP_PKEY_up_ref(pctx->peerkey);
514 rctx->peerkey = pctx->peerkey;
515
516 if (pctx->pmeth->copy(rctx, pctx) > 0)
517 return rctx;
518
519 rctx->pmeth = NULL;
520 EVP_PKEY_CTX_free(rctx);
521 return NULL;
522
523 }
524
525 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
526 {
527 if (app_pkey_methods == NULL) {
528 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
529 if (app_pkey_methods == NULL){
530 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
531 return 0;
532 }
533 }
534 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
535 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
536 return 0;
537 }
538 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
539 return 1;
540 }
541
542 void evp_app_cleanup_int(void)
543 {
544 if (app_pkey_methods != NULL)
545 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
546 }
547
548 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
549 {
550 const EVP_PKEY_METHOD *ret;
551
552 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
553
554 return ret == NULL ? 0 : 1;
555 }
556
557 size_t EVP_PKEY_meth_get_count(void)
558 {
559 size_t rv = OSSL_NELEM(standard_methods);
560
561 if (app_pkey_methods)
562 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
563 return rv;
564 }
565
566 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
567 {
568 if (idx < OSSL_NELEM(standard_methods))
569 return (standard_methods[idx])();
570 if (app_pkey_methods == NULL)
571 return NULL;
572 idx -= OSSL_NELEM(standard_methods);
573 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
574 return NULL;
575 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
576 }
577 #endif
578
579 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
580 {
581 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
582 && ctx->op.kex.exchprovctx != NULL
583 && ctx->op.kex.exchange != NULL
584 && ctx->op.kex.exchange->set_ctx_params != NULL)
585 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
586 params);
587 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
588 && ctx->op.sig.sigprovctx != NULL
589 && ctx->op.sig.signature != NULL
590 && ctx->op.sig.signature->set_ctx_params != NULL)
591 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
592 params);
593 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
594 && ctx->op.ciph.ciphprovctx != NULL
595 && ctx->op.ciph.cipher != NULL
596 && ctx->op.ciph.cipher->set_ctx_params != NULL)
597 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
598 params);
599 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
600 && ctx->op.keymgmt.genctx != NULL
601 && ctx->keymgmt != NULL
602 && ctx->keymgmt->gen_set_params != NULL)
603 return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
604 params);
605 return 0;
606 }
607
608 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
609 {
610 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
611 && ctx->op.kex.exchprovctx != NULL
612 && ctx->op.kex.exchange != NULL
613 && ctx->op.kex.exchange->get_ctx_params != NULL)
614 return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
615 params);
616 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
617 && ctx->op.sig.sigprovctx != NULL
618 && ctx->op.sig.signature != NULL
619 && ctx->op.sig.signature->get_ctx_params != NULL)
620 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
621 params);
622 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
623 && ctx->op.ciph.ciphprovctx != NULL
624 && ctx->op.ciph.cipher != NULL
625 && ctx->op.ciph.cipher->get_ctx_params != NULL)
626 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
627 params);
628 return 0;
629 }
630
631 #ifndef FIPS_MODULE
632 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
633 {
634 void *provctx;
635
636 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
637 && ctx->op.kex.exchange != NULL
638 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
639 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
640 return ctx->op.kex.exchange->gettable_ctx_params(provctx);
641 }
642 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
643 && ctx->op.sig.signature != NULL
644 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
645 provctx = ossl_provider_ctx(
646 EVP_SIGNATURE_provider(ctx->op.sig.signature));
647 return ctx->op.sig.signature->gettable_ctx_params(provctx);
648 }
649 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
650 && ctx->op.ciph.cipher != NULL
651 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
652 provctx = ossl_provider_ctx(
653 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
654 return ctx->op.ciph.cipher->gettable_ctx_params(provctx);
655 }
656 return NULL;
657 }
658
659 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
660 {
661 void *provctx;
662
663 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
664 && ctx->op.kex.exchange != NULL
665 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
666 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
667 return ctx->op.kex.exchange->settable_ctx_params(provctx);
668 }
669 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
670 && ctx->op.sig.signature != NULL
671 && ctx->op.sig.signature->settable_ctx_params != NULL) {
672 provctx = ossl_provider_ctx(
673 EVP_SIGNATURE_provider(ctx->op.sig.signature));
674 return ctx->op.sig.signature->settable_ctx_params(provctx);
675 }
676 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
677 && ctx->op.ciph.cipher != NULL
678 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
679 provctx = ossl_provider_ctx(
680 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
681 return ctx->op.ciph.cipher->settable_ctx_params(provctx);
682 }
683 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
684 && ctx->keymgmt != NULL)
685 return evp_keymgmt_gen_settable_params(ctx->keymgmt);
686
687 return NULL;
688 }
689
690 /*
691 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
692 *
693 * Return 1 on success, 0 or negative for errors.
694 *
695 * In particular they return -2 if any of the params is not supported.
696 *
697 * They are not available in FIPS_MODULE as they depend on
698 * - EVP_PKEY_CTX_{get,set}_params()
699 * - EVP_PKEY_CTX_{gettable,settable}_params()
700 *
701 */
702 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
703 {
704 const OSSL_PARAM *p;
705
706 if (ctx == NULL || params == NULL)
707 return 0;
708
709 for (p = params; p->key != NULL; p++) {
710 /* Check the ctx actually understands this parameter */
711 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
712 p->key) == NULL )
713 return -2;
714 }
715
716 return EVP_PKEY_CTX_set_params(ctx, params);
717 }
718
719 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
720 {
721 const OSSL_PARAM *p;
722
723 if (ctx == NULL || params == NULL)
724 return 0;
725
726 for (p = params; p->key != NULL; p++ ) {
727 /* Check the ctx actually understands this parameter */
728 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
729 p->key) == NULL )
730 return -2;
731 }
732
733 return EVP_PKEY_CTX_get_params(ctx, params);
734 }
735
736 # ifndef OPENSSL_NO_DH
737 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
738 {
739 OSSL_PARAM dh_pad_params[2];
740 unsigned int upad = pad;
741
742 /* We use EVP_PKEY_CTX_ctrl return values */
743 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
744 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
745 return -2;
746 }
747
748 /* TODO(3.0): Remove this eventually when no more legacy */
749 if (ctx->op.kex.exchprovctx == NULL)
750 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
751 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
752
753 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
754 dh_pad_params[1] = OSSL_PARAM_construct_end();
755
756 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
757 }
758 # endif
759
760 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
761 {
762 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
763 /* 80 should be big enough */
764 char name[80] = "";
765 const EVP_MD *tmp;
766
767 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
768 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
769 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
770 return -2;
771 }
772
773 /* TODO(3.0): Remove this eventually when no more legacy */
774 if (ctx->op.sig.sigprovctx == NULL)
775 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
776 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
777
778 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
779 name,
780 sizeof(name));
781 *p = OSSL_PARAM_construct_end();
782
783 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
784 return 0;
785
786 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
787 if (tmp == NULL)
788 return 0;
789
790 *md = tmp;
791
792 return 1;
793 }
794
795 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
796 {
797 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
798 const char *name;
799
800 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
801 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
802 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
803 return -2;
804 }
805
806 /* TODO(3.0): Remove this eventually when no more legacy */
807 if (ctx->op.sig.sigprovctx == NULL)
808 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
809 EVP_PKEY_CTRL_MD, 0, (void *)(md));
810
811 if (md == NULL) {
812 name = "";
813 } else {
814 name = EVP_MD_name(md);
815 }
816
817 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
818 /*
819 * Cast away the const. This is read
820 * only so should be safe
821 */
822 (char *)name, 0);
823 *p = OSSL_PARAM_construct_end();
824
825 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
826 }
827
828 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
829 {
830 OSSL_PARAM tls1_prf_md_params[2], *p = tls1_prf_md_params;
831 const char *name;
832
833 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
834 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
835 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
836 return -2;
837 }
838
839 /* TODO(3.0): Remove this eventually when no more legacy */
840 if (ctx->op.kex.exchprovctx == NULL)
841 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
842 EVP_PKEY_CTRL_TLS_MD, 0, (void *)(md));
843
844 if (md == NULL) {
845 name = "";
846 } else {
847 name = EVP_MD_name(md);
848 }
849
850 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
851 /*
852 * Cast away the const. This is read
853 * only so should be safe
854 */
855 (char *)name, 0);
856 *p++ = OSSL_PARAM_construct_end();
857
858 return EVP_PKEY_CTX_set_params(ctx, tls1_prf_md_params);
859 }
860
861 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
862 const unsigned char *sec, int seclen)
863 {
864 OSSL_PARAM tls1_prf_secret_params[2], *p = tls1_prf_secret_params;
865
866 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
867 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
868 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
869 return -2;
870 }
871
872 /* TODO(3.0): Remove this eventually when no more legacy */
873 if (ctx->op.kex.exchprovctx == NULL)
874 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
875 EVP_PKEY_CTRL_TLS_SECRET, seclen,
876 (void *)(sec));
877
878
879 if (seclen < 0) {
880 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_SECRET_LENGTH);
881 return 0;
882 }
883
884 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
885 /*
886 * Cast away the const. This is read
887 * only so should be safe
888 */
889 (unsigned char *)sec,
890 (size_t)seclen);
891 *p++ = OSSL_PARAM_construct_end();
892
893 return EVP_PKEY_CTX_set_params(ctx, tls1_prf_secret_params);
894 }
895
896 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
897 const unsigned char *seed, int seedlen)
898 {
899 OSSL_PARAM tls1_prf_seed_params[2], *p = tls1_prf_seed_params;
900
901 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
902 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
903 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
904 return -2;
905 }
906
907 /* TODO(3.0): Remove this eventually when no more legacy */
908 if (ctx->op.kex.exchprovctx == NULL)
909 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
910 EVP_PKEY_CTRL_TLS_SEED, seedlen,
911 (void *)(seed));
912
913 if (seedlen < 0) {
914 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_SEED_LENGTH);
915 return 0;
916 }
917
918 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
919 /*
920 * Cast away the const. This is read
921 * only so should be safe
922 */
923 (unsigned char *)seed,
924 (size_t)seedlen);
925 *p++ = OSSL_PARAM_construct_end();
926
927 return EVP_PKEY_CTX_set_params(ctx, tls1_prf_seed_params);
928 }
929
930 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
931 int cmd, int p1, void *p2)
932 {
933 /*
934 * GOST CMS format is different for different cipher algorithms.
935 * Most of other algorithms don't have such a difference
936 * so this ctrl is just ignored.
937 */
938 if (cmd == EVP_PKEY_CTRL_CIPHER)
939 return -2;
940
941 # ifndef OPENSSL_NO_DH
942 if (keytype == EVP_PKEY_DH) {
943 switch (cmd) {
944 case EVP_PKEY_CTRL_DH_PAD:
945 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
946 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
947 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1);
948 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
949 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1);
950 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
951 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1);
952 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
953 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1);
954 case EVP_PKEY_CTRL_DH_RFC5114:
955 return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1);
956 }
957 }
958 # endif
959 # ifndef OPENSSL_NO_DSA
960 if (keytype == EVP_PKEY_DSA) {
961 switch (cmd) {
962 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
963 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1);
964 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
965 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1);
966 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
967 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2);
968 }
969 }
970 # endif
971 # ifndef OPENSSL_NO_EC
972 if (keytype == EVP_PKEY_EC) {
973 switch (cmd) {
974 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
975 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1);
976 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
977 if (p1 == -2) {
978 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
979 } else if (p1 < -1 || p1 > 1) {
980 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
981 return -2;
982 } else {
983 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
984 }
985 case EVP_PKEY_CTRL_EC_KDF_TYPE:
986 if (p1 == -2) {
987 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
988 } else {
989 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
990 }
991 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
992 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
993 case EVP_PKEY_CTRL_EC_KDF_MD:
994 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
995 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
996 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
997 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
998 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
999 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
1000 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
1001 case EVP_PKEY_CTRL_EC_KDF_UKM:
1002 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
1003 }
1004 }
1005 # endif
1006 if (keytype == EVP_PKEY_RSA) {
1007 switch (cmd) {
1008 case EVP_PKEY_CTRL_RSA_OAEP_MD:
1009 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1010 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
1011 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
1012 case EVP_PKEY_CTRL_RSA_MGF1_MD:
1013 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1014 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
1015 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
1016 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
1017 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
1018 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
1019 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
1020 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
1021 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
1022 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
1023 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
1024 }
1025 }
1026 /*
1027 * keytype == -1 is used when several key types share the same structure,
1028 * or for generic controls that are the same across multiple key types.
1029 */
1030 if (keytype == -1) {
1031 if (optype == EVP_PKEY_OP_DERIVE) {
1032 switch (cmd) {
1033 case EVP_PKEY_CTRL_TLS_MD:
1034 return EVP_PKEY_CTX_set_tls1_prf_md(ctx, p2);
1035 case EVP_PKEY_CTRL_TLS_SECRET:
1036 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx, p2, p1);
1037 case EVP_PKEY_CTRL_TLS_SEED:
1038 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx, p2, p1);
1039 }
1040 }
1041 switch (cmd) {
1042 case EVP_PKEY_CTRL_MD:
1043 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
1044 case EVP_PKEY_CTRL_GET_MD:
1045 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
1046 case EVP_PKEY_CTRL_RSA_PADDING:
1047 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
1048 case EVP_PKEY_CTRL_GET_RSA_PADDING:
1049 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
1050 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
1051 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
1052 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
1053 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
1054 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
1055 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2);
1056 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
1057 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
1058 # ifndef OPENSSL_NO_CMS
1059 case EVP_PKEY_CTRL_CMS_DECRYPT:
1060 case EVP_PKEY_CTRL_CMS_ENCRYPT:
1061 # endif
1062 /* TODO (3.0) Temporary hack, this should probe */
1063 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS"))
1064 return 1;
1065 ERR_raise(ERR_LIB_EVP,
1066 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1067 return -2;
1068 }
1069 }
1070 return 0;
1071 }
1072
1073 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1074 int cmd, int p1, void *p2)
1075 {
1076 int ret;
1077
1078 if (ctx == NULL) {
1079 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
1080 return -2;
1081 }
1082
1083 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
1084 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1085 && ctx->op.sig.sigprovctx != NULL)
1086 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1087 && ctx->op.ciph.ciphprovctx != NULL)
1088 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1089 && ctx->op.keymgmt.genctx != NULL))
1090 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1091
1092 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1093 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
1094 return -2;
1095 }
1096 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1097 return -1;
1098
1099 /* Skip the operation checks since this is called in a very early stage */
1100 if (ctx->pmeth->digest_custom != NULL)
1101 goto doit;
1102
1103 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1104 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
1105 return -1;
1106 }
1107
1108 if ((optype != -1) && !(ctx->operation & optype)) {
1109 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
1110 return -1;
1111 }
1112
1113 doit:
1114 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1115
1116 if (ret == -2)
1117 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
1118
1119 return ret;
1120 }
1121
1122 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1123 int cmd, uint64_t value)
1124 {
1125 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1126 }
1127
1128 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
1129 const char *value)
1130 {
1131
1132 /* Special cases that we intercept */
1133 # ifndef OPENSSL_NO_EC
1134 /*
1135 * We don't support encoding settings for providers, i.e. the only
1136 * possible encoding is "named_curve", so we simply fail when something
1137 * else is given, and otherwise just pretend all is fine.
1138 */
1139 if (strcmp(name, "ec_param_enc") == 0) {
1140 if (strcmp(value, "named_curve") == 0) {
1141 return 1;
1142 } else {
1143 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1144 return -2;
1145 }
1146 }
1147 # endif
1148
1149 if (strcmp(name, "md") == 0)
1150 name = OSSL_ALG_PARAM_DIGEST;
1151 else if (strcmp(name, "rsa_padding_mode") == 0)
1152 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
1153 else if (strcmp(name, "rsa_mgf1_md") == 0)
1154 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
1155 else if (strcmp(name, "rsa_oaep_md") == 0)
1156 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
1157 else if (strcmp(name, "rsa_oaep_label") == 0)
1158 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
1159 else if (strcmp(name, "rsa_pss_saltlen") == 0)
1160 name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
1161 else if (strcmp(name, "rsa_keygen_bits") == 0)
1162 name = OSSL_PKEY_PARAM_RSA_BITS;
1163 else if (strcmp(name, "rsa_keygen_pubexp") == 0)
1164 name = OSSL_PKEY_PARAM_RSA_E;
1165 else if (strcmp(name, "rsa_keygen_primes") == 0)
1166 name = OSSL_PKEY_PARAM_RSA_PRIMES;
1167 else if (strcmp(name, "rsa_pss_keygen_md") == 0)
1168 name = OSSL_PKEY_PARAM_RSA_DIGEST;
1169 else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0)
1170 name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
1171 else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0)
1172 name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
1173 # ifndef OPENSSL_NO_DSA
1174 else if (strcmp(name, "dsa_paramgen_bits") == 0)
1175 name = OSSL_PKEY_PARAM_FFC_PBITS;
1176 else if (strcmp(name, "dsa_paramgen_q_bits") == 0)
1177 name = OSSL_PKEY_PARAM_FFC_QBITS;
1178 else if (strcmp(name, "dsa_paramgen_md") == 0)
1179 name = OSSL_PKEY_PARAM_FFC_DIGEST;
1180 # endif
1181 # ifndef OPENSSL_NO_DH
1182 else if (strcmp(name, "dh_paramgen_generator") == 0)
1183 name = OSSL_PKEY_PARAM_DH_GENERATOR;
1184 else if (strcmp(name, "dh_paramgen_prime_len") == 0)
1185 name = OSSL_PKEY_PARAM_FFC_PBITS;
1186 else if (strcmp(name, "dh_paramgen_subprime_len") == 0)
1187 name = OSSL_PKEY_PARAM_FFC_QBITS;
1188 else if (strcmp(name, "dh_paramgen_type") == 0) {
1189 name = OSSL_PKEY_PARAM_FFC_TYPE;
1190 value = dh_gen_type_id2name(atoi(value));
1191 } else if (strcmp(name, "dh_param") == 0)
1192 name = OSSL_PKEY_PARAM_GROUP_NAME;
1193 else if (strcmp(name, "dh_rfc5114") == 0) {
1194 name = OSSL_PKEY_PARAM_GROUP_NAME;
1195 value = ffc_named_group_from_uid(atoi(value));
1196 } else if (strcmp(name, "dh_pad") == 0)
1197 name = OSSL_EXCHANGE_PARAM_PAD;
1198 # endif
1199 # ifndef OPENSSL_NO_EC
1200 else if (strcmp(name, "ec_paramgen_curve") == 0)
1201 name = OSSL_PKEY_PARAM_GROUP_NAME;
1202 else if (strcmp(name, "ecdh_cofactor_mode") == 0)
1203 name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
1204 else if (strcmp(name, "ecdh_kdf_md") == 0)
1205 name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
1206 # endif
1207
1208 {
1209 /*
1210 * TODO(3.0) reduce the code above to only translate known legacy
1211 * string to the corresponding core name (see core_names.h), but
1212 * otherwise leave it to this code block to do the actual work.
1213 */
1214 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
1215 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1216 int rv = 0;
1217 int exists = 0;
1218
1219 if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
1220 strlen(value), &exists)) {
1221 if (!exists) {
1222 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
1223 "name=%s,value=%s", name, value);
1224 return -2;
1225 }
1226 return 0;
1227 }
1228 if (EVP_PKEY_CTX_set_params(ctx, params))
1229 rv = 1;
1230 OPENSSL_free(params[0].data);
1231 return rv;
1232 }
1233 }
1234
1235 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1236 const char *name, const char *value)
1237 {
1238 if (ctx == NULL) {
1239 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1240 return -2;
1241 }
1242
1243 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
1244 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1245 && ctx->op.sig.sigprovctx != NULL)
1246 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1247 && ctx->op.ciph.ciphprovctx != NULL)
1248 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1249 && ctx->op.keymgmt.genctx != NULL))
1250 return legacy_ctrl_str_to_param(ctx, name, value);
1251
1252 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
1253 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
1254 return -2;
1255 }
1256 if (strcmp(name, "digest") == 0)
1257 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
1258 value);
1259 return ctx->pmeth->ctrl_str(ctx, name, value);
1260 }
1261
1262 /* Utility functions to send a string of hex string to a ctrl */
1263
1264 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1265 {
1266 size_t len;
1267
1268 len = strlen(str);
1269 if (len > INT_MAX)
1270 return -1;
1271 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1272 }
1273
1274 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1275 {
1276 unsigned char *bin;
1277 long binlen;
1278 int rv = -1;
1279
1280 bin = OPENSSL_hexstr2buf(hex, &binlen);
1281 if (bin == NULL)
1282 return 0;
1283 if (binlen <= INT_MAX)
1284 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1285 OPENSSL_free(bin);
1286 return rv;
1287 }
1288
1289 /* Pass a message digest to a ctrl */
1290 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1291 {
1292 const EVP_MD *m;
1293
1294 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1295 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
1296 return 0;
1297 }
1298 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1299 }
1300
1301 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1302 {
1303 return ctx->operation;
1304 }
1305
1306 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1307 {
1308 ctx->keygen_info = dat;
1309 ctx->keygen_info_count = datlen;
1310 }
1311
1312 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1313 {
1314 ctx->data = data;
1315 }
1316
1317 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1318 {
1319 return ctx->data;
1320 }
1321
1322 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1323 {
1324 return ctx->pkey;
1325 }
1326
1327 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1328 {
1329 return ctx->peerkey;
1330 }
1331
1332 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1333 {
1334 ctx->app_data = data;
1335 }
1336
1337 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1338 {
1339 return ctx->app_data;
1340 }
1341
1342 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1343 int (*init) (EVP_PKEY_CTX *ctx))
1344 {
1345 pmeth->init = init;
1346 }
1347
1348 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1349 int (*copy) (EVP_PKEY_CTX *dst,
1350 const EVP_PKEY_CTX *src))
1351 {
1352 pmeth->copy = copy;
1353 }
1354
1355 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1356 void (*cleanup) (EVP_PKEY_CTX *ctx))
1357 {
1358 pmeth->cleanup = cleanup;
1359 }
1360
1361 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1362 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1363 int (*paramgen) (EVP_PKEY_CTX *ctx,
1364 EVP_PKEY *pkey))
1365 {
1366 pmeth->paramgen_init = paramgen_init;
1367 pmeth->paramgen = paramgen;
1368 }
1369
1370 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1371 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1372 int (*keygen) (EVP_PKEY_CTX *ctx,
1373 EVP_PKEY *pkey))
1374 {
1375 pmeth->keygen_init = keygen_init;
1376 pmeth->keygen = keygen;
1377 }
1378
1379 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1380 int (*sign_init) (EVP_PKEY_CTX *ctx),
1381 int (*sign) (EVP_PKEY_CTX *ctx,
1382 unsigned char *sig, size_t *siglen,
1383 const unsigned char *tbs,
1384 size_t tbslen))
1385 {
1386 pmeth->sign_init = sign_init;
1387 pmeth->sign = sign;
1388 }
1389
1390 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1391 int (*verify_init) (EVP_PKEY_CTX *ctx),
1392 int (*verify) (EVP_PKEY_CTX *ctx,
1393 const unsigned char *sig,
1394 size_t siglen,
1395 const unsigned char *tbs,
1396 size_t tbslen))
1397 {
1398 pmeth->verify_init = verify_init;
1399 pmeth->verify = verify;
1400 }
1401
1402 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1403 int (*verify_recover_init) (EVP_PKEY_CTX
1404 *ctx),
1405 int (*verify_recover) (EVP_PKEY_CTX
1406 *ctx,
1407 unsigned char
1408 *sig,
1409 size_t *siglen,
1410 const unsigned
1411 char *tbs,
1412 size_t tbslen))
1413 {
1414 pmeth->verify_recover_init = verify_recover_init;
1415 pmeth->verify_recover = verify_recover;
1416 }
1417
1418 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1419 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1420 EVP_MD_CTX *mctx),
1421 int (*signctx) (EVP_PKEY_CTX *ctx,
1422 unsigned char *sig,
1423 size_t *siglen,
1424 EVP_MD_CTX *mctx))
1425 {
1426 pmeth->signctx_init = signctx_init;
1427 pmeth->signctx = signctx;
1428 }
1429
1430 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1431 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1432 EVP_MD_CTX *mctx),
1433 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1434 const unsigned char *sig,
1435 int siglen,
1436 EVP_MD_CTX *mctx))
1437 {
1438 pmeth->verifyctx_init = verifyctx_init;
1439 pmeth->verifyctx = verifyctx;
1440 }
1441
1442 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1443 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1444 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1445 unsigned char *out,
1446 size_t *outlen,
1447 const unsigned char *in,
1448 size_t inlen))
1449 {
1450 pmeth->encrypt_init = encrypt_init;
1451 pmeth->encrypt = encryptfn;
1452 }
1453
1454 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1455 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1456 int (*decrypt) (EVP_PKEY_CTX *ctx,
1457 unsigned char *out,
1458 size_t *outlen,
1459 const unsigned char *in,
1460 size_t inlen))
1461 {
1462 pmeth->decrypt_init = decrypt_init;
1463 pmeth->decrypt = decrypt;
1464 }
1465
1466 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1467 int (*derive_init) (EVP_PKEY_CTX *ctx),
1468 int (*derive) (EVP_PKEY_CTX *ctx,
1469 unsigned char *key,
1470 size_t *keylen))
1471 {
1472 pmeth->derive_init = derive_init;
1473 pmeth->derive = derive;
1474 }
1475
1476 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1477 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1478 void *p2),
1479 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1480 const char *type,
1481 const char *value))
1482 {
1483 pmeth->ctrl = ctrl;
1484 pmeth->ctrl_str = ctrl_str;
1485 }
1486
1487 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1488 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1489 const unsigned char *tbs, size_t tbslen))
1490 {
1491 pmeth->digestsign = digestsign;
1492 }
1493
1494 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1495 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1496 size_t siglen, const unsigned char *tbs,
1497 size_t tbslen))
1498 {
1499 pmeth->digestverify = digestverify;
1500 }
1501
1502 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1503 int (*check) (EVP_PKEY *pkey))
1504 {
1505 pmeth->check = check;
1506 }
1507
1508 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1509 int (*check) (EVP_PKEY *pkey))
1510 {
1511 pmeth->public_check = check;
1512 }
1513
1514 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1515 int (*check) (EVP_PKEY *pkey))
1516 {
1517 pmeth->param_check = check;
1518 }
1519
1520 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1521 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1522 EVP_MD_CTX *mctx))
1523 {
1524 pmeth->digest_custom = digest_custom;
1525 }
1526
1527 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1528 int (**pinit) (EVP_PKEY_CTX *ctx))
1529 {
1530 *pinit = pmeth->init;
1531 }
1532
1533 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1534 int (**pcopy) (EVP_PKEY_CTX *dst,
1535 const EVP_PKEY_CTX *src))
1536 {
1537 *pcopy = pmeth->copy;
1538 }
1539
1540 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1541 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1542 {
1543 *pcleanup = pmeth->cleanup;
1544 }
1545
1546 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1547 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1548 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1549 EVP_PKEY *pkey))
1550 {
1551 if (pparamgen_init)
1552 *pparamgen_init = pmeth->paramgen_init;
1553 if (pparamgen)
1554 *pparamgen = pmeth->paramgen;
1555 }
1556
1557 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1558 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1559 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1560 EVP_PKEY *pkey))
1561 {
1562 if (pkeygen_init)
1563 *pkeygen_init = pmeth->keygen_init;
1564 if (pkeygen)
1565 *pkeygen = pmeth->keygen;
1566 }
1567
1568 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1569 int (**psign_init) (EVP_PKEY_CTX *ctx),
1570 int (**psign) (EVP_PKEY_CTX *ctx,
1571 unsigned char *sig, size_t *siglen,
1572 const unsigned char *tbs,
1573 size_t tbslen))
1574 {
1575 if (psign_init)
1576 *psign_init = pmeth->sign_init;
1577 if (psign)
1578 *psign = pmeth->sign;
1579 }
1580
1581 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1582 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1583 int (**pverify) (EVP_PKEY_CTX *ctx,
1584 const unsigned char *sig,
1585 size_t siglen,
1586 const unsigned char *tbs,
1587 size_t tbslen))
1588 {
1589 if (pverify_init)
1590 *pverify_init = pmeth->verify_init;
1591 if (pverify)
1592 *pverify = pmeth->verify;
1593 }
1594
1595 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1596 int (**pverify_recover_init) (EVP_PKEY_CTX
1597 *ctx),
1598 int (**pverify_recover) (EVP_PKEY_CTX
1599 *ctx,
1600 unsigned char
1601 *sig,
1602 size_t *siglen,
1603 const unsigned
1604 char *tbs,
1605 size_t tbslen))
1606 {
1607 if (pverify_recover_init)
1608 *pverify_recover_init = pmeth->verify_recover_init;
1609 if (pverify_recover)
1610 *pverify_recover = pmeth->verify_recover;
1611 }
1612
1613 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1614 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1615 EVP_MD_CTX *mctx),
1616 int (**psignctx) (EVP_PKEY_CTX *ctx,
1617 unsigned char *sig,
1618 size_t *siglen,
1619 EVP_MD_CTX *mctx))
1620 {
1621 if (psignctx_init)
1622 *psignctx_init = pmeth->signctx_init;
1623 if (psignctx)
1624 *psignctx = pmeth->signctx;
1625 }
1626
1627 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1628 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1629 EVP_MD_CTX *mctx),
1630 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1631 const unsigned char *sig,
1632 int siglen,
1633 EVP_MD_CTX *mctx))
1634 {
1635 if (pverifyctx_init)
1636 *pverifyctx_init = pmeth->verifyctx_init;
1637 if (pverifyctx)
1638 *pverifyctx = pmeth->verifyctx;
1639 }
1640
1641 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1642 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1643 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1644 unsigned char *out,
1645 size_t *outlen,
1646 const unsigned char *in,
1647 size_t inlen))
1648 {
1649 if (pencrypt_init)
1650 *pencrypt_init = pmeth->encrypt_init;
1651 if (pencryptfn)
1652 *pencryptfn = pmeth->encrypt;
1653 }
1654
1655 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1656 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1657 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1658 unsigned char *out,
1659 size_t *outlen,
1660 const unsigned char *in,
1661 size_t inlen))
1662 {
1663 if (pdecrypt_init)
1664 *pdecrypt_init = pmeth->decrypt_init;
1665 if (pdecrypt)
1666 *pdecrypt = pmeth->decrypt;
1667 }
1668
1669 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1670 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1671 int (**pderive) (EVP_PKEY_CTX *ctx,
1672 unsigned char *key,
1673 size_t *keylen))
1674 {
1675 if (pderive_init)
1676 *pderive_init = pmeth->derive_init;
1677 if (pderive)
1678 *pderive = pmeth->derive;
1679 }
1680
1681 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1682 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1683 void *p2),
1684 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1685 const char *type,
1686 const char *value))
1687 {
1688 if (pctrl)
1689 *pctrl = pmeth->ctrl;
1690 if (pctrl_str)
1691 *pctrl_str = pmeth->ctrl_str;
1692 }
1693
1694 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1695 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1696 const unsigned char *tbs, size_t tbslen))
1697 {
1698 if (digestsign)
1699 *digestsign = pmeth->digestsign;
1700 }
1701
1702 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1703 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1704 size_t siglen, const unsigned char *tbs,
1705 size_t tbslen))
1706 {
1707 if (digestverify)
1708 *digestverify = pmeth->digestverify;
1709 }
1710
1711 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1712 int (**pcheck) (EVP_PKEY *pkey))
1713 {
1714 if (pcheck != NULL)
1715 *pcheck = pmeth->check;
1716 }
1717
1718 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1719 int (**pcheck) (EVP_PKEY *pkey))
1720 {
1721 if (pcheck != NULL)
1722 *pcheck = pmeth->public_check;
1723 }
1724
1725 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1726 int (**pcheck) (EVP_PKEY *pkey))
1727 {
1728 if (pcheck != NULL)
1729 *pcheck = pmeth->param_check;
1730 }
1731
1732 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1733 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1734 EVP_MD_CTX *mctx))
1735 {
1736 if (pdigest_custom != NULL)
1737 *pdigest_custom = pmeth->digest_custom;
1738 }
1739
1740 #endif /* FIPS_MODULE */