]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_lib.c
Change EVP_PKEY_CTX_new_provided() to take a library context too.
[thirdparty/openssl.git] / crypto / evp / pmeth_lib.c
1
2 /*
3 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/core_names.h>
17 #include <openssl/dh.h>
18 #include "internal/cryptlib.h"
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "internal/numbers.h"
22 #include "internal/provider.h"
23 #include "evp_local.h"
24
25 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
26 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
27
28 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
29
30 /* This array needs to be in order of NIDs */
31 static pmeth_fn standard_methods[] = {
32 #ifndef OPENSSL_NO_RSA
33 rsa_pkey_method,
34 #endif
35 #ifndef OPENSSL_NO_DH
36 dh_pkey_method,
37 #endif
38 #ifndef OPENSSL_NO_DSA
39 dsa_pkey_method,
40 #endif
41 #ifndef OPENSSL_NO_EC
42 ec_pkey_method,
43 #endif
44 hmac_pkey_method,
45 #ifndef OPENSSL_NO_CMAC
46 cmac_pkey_method,
47 #endif
48 #ifndef OPENSSL_NO_RSA
49 rsa_pss_pkey_method,
50 #endif
51 #ifndef OPENSSL_NO_DH
52 dhx_pkey_method,
53 #endif
54 #ifndef OPENSSL_NO_SCRYPT
55 scrypt_pkey_method,
56 #endif
57 tls1_prf_pkey_method,
58 #ifndef OPENSSL_NO_EC
59 ecx25519_pkey_method,
60 ecx448_pkey_method,
61 #endif
62 hkdf_pkey_method,
63 #ifndef OPENSSL_NO_POLY1305
64 poly1305_pkey_method,
65 #endif
66 #ifndef OPENSSL_NO_SIPHASH
67 siphash_pkey_method,
68 #endif
69 #ifndef OPENSSL_NO_EC
70 ed25519_pkey_method,
71 ed448_pkey_method,
72 #endif
73 #ifndef OPENSSL_NO_SM2
74 sm2_pkey_method,
75 #endif
76 };
77
78 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
79
80 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
81 {
82 return ((*a)->pkey_id - ((**b)())->pkey_id);
83 }
84
85 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
86
87 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
88 const EVP_PKEY_METHOD *const *b)
89 {
90 return ((*a)->pkey_id - (*b)->pkey_id);
91 }
92
93 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
94 {
95 pmeth_fn *ret;
96 EVP_PKEY_METHOD tmp;
97 const EVP_PKEY_METHOD *t = &tmp;
98
99 tmp.pkey_id = type;
100 if (app_pkey_methods) {
101 int idx;
102 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
103 if (idx >= 0)
104 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
105 }
106 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
107 sizeof(standard_methods) /
108 sizeof(pmeth_fn));
109 if (ret == NULL || *ret == NULL)
110 return NULL;
111 return (**ret)();
112 }
113
114 static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
115 EVP_PKEY *pkey, ENGINE *e,
116 const char *name, const char *propquery,
117 int id)
118 {
119 EVP_PKEY_CTX *ret;
120 const EVP_PKEY_METHOD *pmeth = NULL;
121
122 /*
123 * When using providers, the context is bound to the algo implementation
124 * later.
125 */
126 if (pkey == NULL && e == NULL && id == -1)
127 goto common;
128
129 /* TODO(3.0) Legacy code should be removed when all is provider based */
130 /* BEGIN legacy */
131 if (id == -1) {
132 if (pkey == NULL)
133 return 0;
134 id = pkey->type;
135 }
136
137 /*
138 * Here, we extract what information we can for the purpose of
139 * supporting usage with implementations from providers, to make
140 * for a smooth transition from legacy stuff to provider based stuff.
141 *
142 * If an engine is given, this is entirely legacy, and we should not
143 * pretend anything else, so we only set the name when no engine is
144 * given. If both are already given, someone made a mistake, and
145 * since that can only happen internally, it's safe to make an
146 * assertion.
147 */
148 if (!ossl_assert(e == NULL || name == NULL))
149 return NULL;
150 if (e == NULL)
151 name = OBJ_nid2sn(id);
152 propquery = NULL;
153 /*
154 * We were called using legacy data, or an EVP_PKEY, but an EVP_PKEY
155 * isn't tied to a specific library context, so we fall back to the
156 * default library context.
157 * TODO(v3.0): an EVP_PKEY that doesn't originate from a leagacy key
158 * structure only has the pkeys[] cache, where the first element is
159 * considered the "origin". Investigate if that could be a suitable
160 * way to find a library context.
161 */
162 libctx = NULL;
163
164 #ifndef OPENSSL_NO_ENGINE
165 if (e == NULL && pkey != NULL)
166 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
167 /* Try to find an ENGINE which implements this method */
168 if (e) {
169 if (!ENGINE_init(e)) {
170 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
171 return NULL;
172 }
173 } else {
174 e = ENGINE_get_pkey_meth_engine(id);
175 }
176
177 /*
178 * If an ENGINE handled this method look it up. Otherwise use internal
179 * tables.
180 */
181 if (e)
182 pmeth = ENGINE_get_pkey_meth(e, id);
183 else
184 #endif
185 pmeth = EVP_PKEY_meth_find(id);
186
187 if (pmeth == NULL) {
188 #ifndef OPENSSL_NO_ENGINE
189 ENGINE_finish(e);
190 #endif
191 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
192 return NULL;
193 }
194 /* END legacy */
195
196 common:
197 ret = OPENSSL_zalloc(sizeof(*ret));
198 if (ret == NULL) {
199 #ifndef OPENSSL_NO_ENGINE
200 ENGINE_finish(e);
201 #endif
202 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
203 return NULL;
204 }
205 ret->libctx = libctx;
206 ret->algorithm = name;
207 ret->propquery = propquery;
208 ret->engine = e;
209 ret->pmeth = pmeth;
210 ret->operation = EVP_PKEY_OP_UNDEFINED;
211 ret->pkey = pkey;
212 if (pkey != NULL)
213 EVP_PKEY_up_ref(pkey);
214
215 if (pmeth != NULL && pmeth->init != NULL) {
216 if (pmeth->init(ret) <= 0) {
217 ret->pmeth = NULL;
218 EVP_PKEY_CTX_free(ret);
219 return NULL;
220 }
221 }
222
223 return ret;
224 }
225
226 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
227 {
228 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
229 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
230 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
231 EVP_KEYEXCH_free(ctx->op.kex.exchange);
232 ctx->op.kex.exchprovctx = NULL;
233 ctx->op.kex.exchange = NULL;
234 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
235 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
236 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
237 EVP_SIGNATURE_free(ctx->op.sig.signature);
238 ctx->op.sig.sigprovctx = NULL;
239 ctx->op.sig.signature = NULL;
240 }
241 }
242
243 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
244 {
245 EVP_PKEY_METHOD *pmeth;
246
247 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
248 if (pmeth == NULL) {
249 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
250 return NULL;
251 }
252
253 pmeth->pkey_id = id;
254 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
255 return pmeth;
256 }
257
258 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
259 const EVP_PKEY_METHOD *meth)
260 {
261 if (ppkey_id)
262 *ppkey_id = meth->pkey_id;
263 if (pflags)
264 *pflags = meth->flags;
265 }
266
267 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
268 {
269
270 dst->init = src->init;
271 dst->copy = src->copy;
272 dst->cleanup = src->cleanup;
273
274 dst->paramgen_init = src->paramgen_init;
275 dst->paramgen = src->paramgen;
276
277 dst->keygen_init = src->keygen_init;
278 dst->keygen = src->keygen;
279
280 dst->sign_init = src->sign_init;
281 dst->sign = src->sign;
282
283 dst->verify_init = src->verify_init;
284 dst->verify = src->verify;
285
286 dst->verify_recover_init = src->verify_recover_init;
287 dst->verify_recover = src->verify_recover;
288
289 dst->signctx_init = src->signctx_init;
290 dst->signctx = src->signctx;
291
292 dst->verifyctx_init = src->verifyctx_init;
293 dst->verifyctx = src->verifyctx;
294
295 dst->encrypt_init = src->encrypt_init;
296 dst->encrypt = src->encrypt;
297
298 dst->decrypt_init = src->decrypt_init;
299 dst->decrypt = src->decrypt;
300
301 dst->derive_init = src->derive_init;
302 dst->derive = src->derive;
303
304 dst->ctrl = src->ctrl;
305 dst->ctrl_str = src->ctrl_str;
306
307 dst->check = src->check;
308 }
309
310 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
311 {
312 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
313 OPENSSL_free(pmeth);
314 }
315
316 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
317 {
318 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
319 }
320
321 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
322 {
323 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
324 }
325
326 EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(OPENSSL_CTX *libctx,
327 const char *name,
328 const char *propquery)
329 {
330 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
331 }
332
333 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
334 {
335 EVP_PKEY_CTX *rctx;
336
337 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
338 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
339 && pctx->op.kex.exchprovctx == NULL)
340 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
341 && pctx->op.sig.sigprovctx == NULL)))
342 return NULL;
343 #ifndef OPENSSL_NO_ENGINE
344 /* Make sure it's safe to copy a pkey context using an ENGINE */
345 if (pctx->engine && !ENGINE_init(pctx->engine)) {
346 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
347 return 0;
348 }
349 #endif
350 rctx = OPENSSL_zalloc(sizeof(*rctx));
351 if (rctx == NULL) {
352 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
353 return NULL;
354 }
355
356 if (pctx->pkey != NULL)
357 EVP_PKEY_up_ref(pctx->pkey);
358 rctx->pkey = pctx->pkey;
359 rctx->operation = pctx->operation;
360 rctx->libctx = pctx->libctx;
361 rctx->algorithm = pctx->algorithm;
362 rctx->propquery = pctx->propquery;
363
364 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
365 if (pctx->op.kex.exchange != NULL) {
366 rctx->op.kex.exchange = pctx->op.kex.exchange;
367 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
368 OPENSSL_free(rctx);
369 return NULL;
370 }
371 }
372 if (pctx->op.kex.exchprovctx != NULL) {
373 if (!ossl_assert(pctx->op.kex.exchange != NULL))
374 return NULL;
375 rctx->op.kex.exchprovctx
376 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
377 if (rctx->op.kex.exchprovctx == NULL) {
378 EVP_KEYEXCH_free(rctx->op.kex.exchange);
379 OPENSSL_free(rctx);
380 return NULL;
381 }
382 return rctx;
383 }
384 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
385 if (pctx->op.sig.signature != NULL) {
386 rctx->op.sig.signature = pctx->op.sig.signature;
387 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
388 OPENSSL_free(rctx);
389 return NULL;
390 }
391 }
392 if (pctx->op.sig.sigprovctx != NULL) {
393 if (!ossl_assert(pctx->op.sig.signature != NULL))
394 return NULL;
395 rctx->op.sig.sigprovctx
396 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
397 if (rctx->op.sig.sigprovctx == NULL) {
398 EVP_SIGNATURE_free(rctx->op.sig.signature);
399 OPENSSL_free(rctx);
400 return NULL;
401 }
402 return rctx;
403 }
404 }
405
406 rctx->pmeth = pctx->pmeth;
407 #ifndef OPENSSL_NO_ENGINE
408 rctx->engine = pctx->engine;
409 #endif
410
411 if (pctx->peerkey)
412 EVP_PKEY_up_ref(pctx->peerkey);
413 rctx->peerkey = pctx->peerkey;
414
415 if (pctx->pmeth->copy(rctx, pctx) > 0)
416 return rctx;
417
418 rctx->pmeth = NULL;
419 EVP_PKEY_CTX_free(rctx);
420 return NULL;
421
422 }
423
424 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
425 {
426 if (app_pkey_methods == NULL) {
427 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
428 if (app_pkey_methods == NULL){
429 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
430 return 0;
431 }
432 }
433 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
434 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
435 return 0;
436 }
437 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
438 return 1;
439 }
440
441 void evp_app_cleanup_int(void)
442 {
443 if (app_pkey_methods != NULL)
444 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
445 }
446
447 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
448 {
449 const EVP_PKEY_METHOD *ret;
450
451 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
452
453 return ret == NULL ? 0 : 1;
454 }
455
456 size_t EVP_PKEY_meth_get_count(void)
457 {
458 size_t rv = OSSL_NELEM(standard_methods);
459
460 if (app_pkey_methods)
461 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
462 return rv;
463 }
464
465 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
466 {
467 if (idx < OSSL_NELEM(standard_methods))
468 return (standard_methods[idx])();
469 if (app_pkey_methods == NULL)
470 return NULL;
471 idx -= OSSL_NELEM(standard_methods);
472 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
473 return NULL;
474 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
475 }
476
477 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
478 {
479 if (ctx == NULL)
480 return;
481 if (ctx->pmeth && ctx->pmeth->cleanup)
482 ctx->pmeth->cleanup(ctx);
483
484 evp_pkey_ctx_free_old_ops(ctx);
485
486 EVP_PKEY_free(ctx->pkey);
487 EVP_PKEY_free(ctx->peerkey);
488 #ifndef OPENSSL_NO_ENGINE
489 ENGINE_finish(ctx->engine);
490 #endif
491 OPENSSL_free(ctx);
492 }
493
494 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
495 {
496 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
497 && ctx->op.sig.sigprovctx != NULL
498 && ctx->op.sig.signature != NULL
499 && ctx->op.sig.signature->get_ctx_params != NULL)
500 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
501 params);
502 return 0;
503 }
504
505 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
506 {
507 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
508 && ctx->op.sig.signature != NULL
509 && ctx->op.sig.signature->gettable_ctx_params != NULL)
510 return ctx->op.sig.signature->gettable_ctx_params();
511
512 return NULL;
513 }
514
515 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
516 {
517 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
518 && ctx->op.kex.exchprovctx != NULL
519 && ctx->op.kex.exchange != NULL
520 && ctx->op.kex.exchange->set_ctx_params != NULL)
521 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
522 params);
523 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
524 && ctx->op.sig.sigprovctx != NULL
525 && ctx->op.sig.signature != NULL
526 && ctx->op.sig.signature->set_ctx_params != NULL)
527 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
528 params);
529 return 0;
530 }
531
532 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
533 {
534 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
535 && ctx->op.kex.exchange != NULL
536 && ctx->op.kex.exchange->settable_ctx_params != NULL)
537 return ctx->op.kex.exchange->settable_ctx_params();
538 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
539 && ctx->op.sig.signature != NULL
540 && ctx->op.sig.signature->settable_ctx_params != NULL)
541 return ctx->op.sig.signature->settable_ctx_params();
542
543 return NULL;
544 }
545
546 #ifndef OPENSSL_NO_DH
547 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
548 {
549 OSSL_PARAM dh_pad_params[2];
550 unsigned int upad = pad;
551
552 /* We use EVP_PKEY_CTX_ctrl return values */
553 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
554 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
555 return -2;
556 }
557
558 /* TODO(3.0): Remove this eventually when no more legacy */
559 if (ctx->op.kex.exchprovctx == NULL)
560 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
561 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
562
563 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
564 dh_pad_params[1] = OSSL_PARAM_construct_end();
565
566 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
567 }
568 #endif
569
570 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
571 {
572 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
573 /* 80 should be big enough */
574 char name[80] = "";
575 const EVP_MD *tmp;
576
577 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
578 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
579 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
580 return -2;
581 }
582
583 /* TODO(3.0): Remove this eventually when no more legacy */
584 if (ctx->op.sig.sigprovctx == NULL)
585 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
586 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
587
588 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
589 name,
590 sizeof(name));
591 *p++ = OSSL_PARAM_construct_end();
592
593 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
594 return 0;
595
596 tmp = EVP_get_digestbyname(name);
597 if (tmp == NULL)
598 return 0;
599
600 *md = tmp;
601
602 return 1;
603 }
604
605 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
606 {
607 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
608 size_t mdsize;
609 const char *name;
610
611 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
612 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
613 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
614 return -2;
615 }
616
617 /* TODO(3.0): Remove this eventually when no more legacy */
618 if (ctx->op.sig.sigprovctx == NULL)
619 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
620 EVP_PKEY_CTRL_MD, 0, (void *)(md));
621
622 if (md == NULL) {
623 name = "";
624 mdsize = 0;
625 } else {
626 mdsize = EVP_MD_size(md);
627 name = EVP_MD_name(md);
628 }
629
630 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
631 /*
632 * Cast away the const. This is read
633 * only so should be safe
634 */
635 (char *)name,
636 strlen(name) + 1);
637 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
638 &mdsize);
639 *p++ = OSSL_PARAM_construct_end();
640
641 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
642 }
643
644 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
645 int cmd, int p1, void *p2)
646 {
647 switch (cmd) {
648 #ifndef OPENSSL_NO_DH
649 case EVP_PKEY_CTRL_DH_PAD:
650 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
651 #endif
652 case EVP_PKEY_CTRL_MD:
653 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
654 case EVP_PKEY_CTRL_GET_MD:
655 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
656 }
657 return 0;
658 }
659
660 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
661 int cmd, int p1, void *p2)
662 {
663 int ret;
664
665 if (ctx == NULL) {
666 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
667 return -2;
668 }
669
670 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
671 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
672 && ctx->op.sig.sigprovctx != NULL))
673 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
674
675 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
676 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
677 return -2;
678 }
679 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
680 return -1;
681
682 /* Skip the operation checks since this is called in a very early stage */
683 if (ctx->pmeth->digest_custom != NULL)
684 goto doit;
685
686 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
687 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
688 return -1;
689 }
690
691 if ((optype != -1) && !(ctx->operation & optype)) {
692 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
693 return -1;
694 }
695
696 doit:
697 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
698
699 if (ret == -2)
700 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
701
702 return ret;
703 }
704
705 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
706 int cmd, uint64_t value)
707 {
708 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
709 }
710
711 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
712 const char *value)
713 {
714 #ifndef OPENSSL_NO_DH
715 if (strcmp(name, "dh_pad") == 0) {
716 int pad;
717
718 pad = atoi(value);
719 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
720 }
721 #endif
722 if (strcmp(name, "digest") == 0) {
723 int ret;
724 EVP_MD *md;
725
726 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
727 return 0;
728 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
729 value, NULL);
730 if (md == NULL)
731 return 0;
732 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
733 EVP_MD_meth_free(md);
734 return ret;
735 }
736
737 return 0;
738 }
739
740 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
741 const char *name, const char *value)
742 {
743 if (ctx == NULL) {
744 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
745 return -2;
746 }
747
748 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
749 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
750 && ctx->op.sig.sigprovctx != NULL))
751 return legacy_ctrl_str_to_param(ctx, name, value);
752
753 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
754 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
755 return -2;
756 }
757 if (strcmp(name, "digest") == 0)
758 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
759 value);
760 return ctx->pmeth->ctrl_str(ctx, name, value);
761 }
762
763 /* Utility functions to send a string of hex string to a ctrl */
764
765 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
766 {
767 size_t len;
768
769 len = strlen(str);
770 if (len > INT_MAX)
771 return -1;
772 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
773 }
774
775 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
776 {
777 unsigned char *bin;
778 long binlen;
779 int rv = -1;
780
781 bin = OPENSSL_hexstr2buf(hex, &binlen);
782 if (bin == NULL)
783 return 0;
784 if (binlen <= INT_MAX)
785 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
786 OPENSSL_free(bin);
787 return rv;
788 }
789
790 /* Pass a message digest to a ctrl */
791 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
792 {
793 const EVP_MD *m;
794
795 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
796 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
797 return 0;
798 }
799 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
800 }
801
802 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
803 {
804 return ctx->operation;
805 }
806
807 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
808 {
809 ctx->keygen_info = dat;
810 ctx->keygen_info_count = datlen;
811 }
812
813 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
814 {
815 ctx->data = data;
816 }
817
818 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
819 {
820 return ctx->data;
821 }
822
823 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
824 {
825 return ctx->pkey;
826 }
827
828 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
829 {
830 return ctx->peerkey;
831 }
832
833 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
834 {
835 ctx->app_data = data;
836 }
837
838 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
839 {
840 return ctx->app_data;
841 }
842
843 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
844 int (*init) (EVP_PKEY_CTX *ctx))
845 {
846 pmeth->init = init;
847 }
848
849 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
850 int (*copy) (EVP_PKEY_CTX *dst,
851 const EVP_PKEY_CTX *src))
852 {
853 pmeth->copy = copy;
854 }
855
856 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
857 void (*cleanup) (EVP_PKEY_CTX *ctx))
858 {
859 pmeth->cleanup = cleanup;
860 }
861
862 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
863 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
864 int (*paramgen) (EVP_PKEY_CTX *ctx,
865 EVP_PKEY *pkey))
866 {
867 pmeth->paramgen_init = paramgen_init;
868 pmeth->paramgen = paramgen;
869 }
870
871 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
872 int (*keygen_init) (EVP_PKEY_CTX *ctx),
873 int (*keygen) (EVP_PKEY_CTX *ctx,
874 EVP_PKEY *pkey))
875 {
876 pmeth->keygen_init = keygen_init;
877 pmeth->keygen = keygen;
878 }
879
880 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
881 int (*sign_init) (EVP_PKEY_CTX *ctx),
882 int (*sign) (EVP_PKEY_CTX *ctx,
883 unsigned char *sig, size_t *siglen,
884 const unsigned char *tbs,
885 size_t tbslen))
886 {
887 pmeth->sign_init = sign_init;
888 pmeth->sign = sign;
889 }
890
891 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
892 int (*verify_init) (EVP_PKEY_CTX *ctx),
893 int (*verify) (EVP_PKEY_CTX *ctx,
894 const unsigned char *sig,
895 size_t siglen,
896 const unsigned char *tbs,
897 size_t tbslen))
898 {
899 pmeth->verify_init = verify_init;
900 pmeth->verify = verify;
901 }
902
903 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
904 int (*verify_recover_init) (EVP_PKEY_CTX
905 *ctx),
906 int (*verify_recover) (EVP_PKEY_CTX
907 *ctx,
908 unsigned char
909 *sig,
910 size_t *siglen,
911 const unsigned
912 char *tbs,
913 size_t tbslen))
914 {
915 pmeth->verify_recover_init = verify_recover_init;
916 pmeth->verify_recover = verify_recover;
917 }
918
919 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
920 int (*signctx_init) (EVP_PKEY_CTX *ctx,
921 EVP_MD_CTX *mctx),
922 int (*signctx) (EVP_PKEY_CTX *ctx,
923 unsigned char *sig,
924 size_t *siglen,
925 EVP_MD_CTX *mctx))
926 {
927 pmeth->signctx_init = signctx_init;
928 pmeth->signctx = signctx;
929 }
930
931 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
932 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
933 EVP_MD_CTX *mctx),
934 int (*verifyctx) (EVP_PKEY_CTX *ctx,
935 const unsigned char *sig,
936 int siglen,
937 EVP_MD_CTX *mctx))
938 {
939 pmeth->verifyctx_init = verifyctx_init;
940 pmeth->verifyctx = verifyctx;
941 }
942
943 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
944 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
945 int (*encryptfn) (EVP_PKEY_CTX *ctx,
946 unsigned char *out,
947 size_t *outlen,
948 const unsigned char *in,
949 size_t inlen))
950 {
951 pmeth->encrypt_init = encrypt_init;
952 pmeth->encrypt = encryptfn;
953 }
954
955 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
956 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
957 int (*decrypt) (EVP_PKEY_CTX *ctx,
958 unsigned char *out,
959 size_t *outlen,
960 const unsigned char *in,
961 size_t inlen))
962 {
963 pmeth->decrypt_init = decrypt_init;
964 pmeth->decrypt = decrypt;
965 }
966
967 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
968 int (*derive_init) (EVP_PKEY_CTX *ctx),
969 int (*derive) (EVP_PKEY_CTX *ctx,
970 unsigned char *key,
971 size_t *keylen))
972 {
973 pmeth->derive_init = derive_init;
974 pmeth->derive = derive;
975 }
976
977 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
978 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
979 void *p2),
980 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
981 const char *type,
982 const char *value))
983 {
984 pmeth->ctrl = ctrl;
985 pmeth->ctrl_str = ctrl_str;
986 }
987
988 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
989 int (*check) (EVP_PKEY *pkey))
990 {
991 pmeth->check = check;
992 }
993
994 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
995 int (*check) (EVP_PKEY *pkey))
996 {
997 pmeth->public_check = check;
998 }
999
1000 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1001 int (*check) (EVP_PKEY *pkey))
1002 {
1003 pmeth->param_check = check;
1004 }
1005
1006 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1007 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1008 EVP_MD_CTX *mctx))
1009 {
1010 pmeth->digest_custom = digest_custom;
1011 }
1012
1013 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1014 int (**pinit) (EVP_PKEY_CTX *ctx))
1015 {
1016 *pinit = pmeth->init;
1017 }
1018
1019 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1020 int (**pcopy) (EVP_PKEY_CTX *dst,
1021 const EVP_PKEY_CTX *src))
1022 {
1023 *pcopy = pmeth->copy;
1024 }
1025
1026 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1027 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1028 {
1029 *pcleanup = pmeth->cleanup;
1030 }
1031
1032 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1033 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1034 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1035 EVP_PKEY *pkey))
1036 {
1037 if (pparamgen_init)
1038 *pparamgen_init = pmeth->paramgen_init;
1039 if (pparamgen)
1040 *pparamgen = pmeth->paramgen;
1041 }
1042
1043 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1044 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1045 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1046 EVP_PKEY *pkey))
1047 {
1048 if (pkeygen_init)
1049 *pkeygen_init = pmeth->keygen_init;
1050 if (pkeygen)
1051 *pkeygen = pmeth->keygen;
1052 }
1053
1054 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1055 int (**psign_init) (EVP_PKEY_CTX *ctx),
1056 int (**psign) (EVP_PKEY_CTX *ctx,
1057 unsigned char *sig, size_t *siglen,
1058 const unsigned char *tbs,
1059 size_t tbslen))
1060 {
1061 if (psign_init)
1062 *psign_init = pmeth->sign_init;
1063 if (psign)
1064 *psign = pmeth->sign;
1065 }
1066
1067 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1068 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1069 int (**pverify) (EVP_PKEY_CTX *ctx,
1070 const unsigned char *sig,
1071 size_t siglen,
1072 const unsigned char *tbs,
1073 size_t tbslen))
1074 {
1075 if (pverify_init)
1076 *pverify_init = pmeth->verify_init;
1077 if (pverify)
1078 *pverify = pmeth->verify;
1079 }
1080
1081 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1082 int (**pverify_recover_init) (EVP_PKEY_CTX
1083 *ctx),
1084 int (**pverify_recover) (EVP_PKEY_CTX
1085 *ctx,
1086 unsigned char
1087 *sig,
1088 size_t *siglen,
1089 const unsigned
1090 char *tbs,
1091 size_t tbslen))
1092 {
1093 if (pverify_recover_init)
1094 *pverify_recover_init = pmeth->verify_recover_init;
1095 if (pverify_recover)
1096 *pverify_recover = pmeth->verify_recover;
1097 }
1098
1099 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1100 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1101 EVP_MD_CTX *mctx),
1102 int (**psignctx) (EVP_PKEY_CTX *ctx,
1103 unsigned char *sig,
1104 size_t *siglen,
1105 EVP_MD_CTX *mctx))
1106 {
1107 if (psignctx_init)
1108 *psignctx_init = pmeth->signctx_init;
1109 if (psignctx)
1110 *psignctx = pmeth->signctx;
1111 }
1112
1113 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1114 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1115 EVP_MD_CTX *mctx),
1116 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1117 const unsigned char *sig,
1118 int siglen,
1119 EVP_MD_CTX *mctx))
1120 {
1121 if (pverifyctx_init)
1122 *pverifyctx_init = pmeth->verifyctx_init;
1123 if (pverifyctx)
1124 *pverifyctx = pmeth->verifyctx;
1125 }
1126
1127 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1128 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1129 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1130 unsigned char *out,
1131 size_t *outlen,
1132 const unsigned char *in,
1133 size_t inlen))
1134 {
1135 if (pencrypt_init)
1136 *pencrypt_init = pmeth->encrypt_init;
1137 if (pencryptfn)
1138 *pencryptfn = pmeth->encrypt;
1139 }
1140
1141 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1142 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1143 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1144 unsigned char *out,
1145 size_t *outlen,
1146 const unsigned char *in,
1147 size_t inlen))
1148 {
1149 if (pdecrypt_init)
1150 *pdecrypt_init = pmeth->decrypt_init;
1151 if (pdecrypt)
1152 *pdecrypt = pmeth->decrypt;
1153 }
1154
1155 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1156 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1157 int (**pderive) (EVP_PKEY_CTX *ctx,
1158 unsigned char *key,
1159 size_t *keylen))
1160 {
1161 if (pderive_init)
1162 *pderive_init = pmeth->derive_init;
1163 if (pderive)
1164 *pderive = pmeth->derive;
1165 }
1166
1167 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1168 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1169 void *p2),
1170 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1171 const char *type,
1172 const char *value))
1173 {
1174 if (pctrl)
1175 *pctrl = pmeth->ctrl;
1176 if (pctrl_str)
1177 *pctrl_str = pmeth->ctrl_str;
1178 }
1179
1180 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1181 int (**pcheck) (EVP_PKEY *pkey))
1182 {
1183 if (pcheck != NULL)
1184 *pcheck = pmeth->check;
1185 }
1186
1187 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1188 int (**pcheck) (EVP_PKEY *pkey))
1189 {
1190 if (pcheck != NULL)
1191 *pcheck = pmeth->public_check;
1192 }
1193
1194 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1195 int (**pcheck) (EVP_PKEY *pkey))
1196 {
1197 if (pcheck != NULL)
1198 *pcheck = pmeth->param_check;
1199 }
1200
1201 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1202 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1203 EVP_MD_CTX *mctx))
1204 {
1205 if (pdigest_custom != NULL)
1206 *pdigest_custom = pmeth->digest_custom;
1207 }