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