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