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