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