2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include "crypto/evp.h"
15 #include "internal/provider.h"
16 #include "internal/numbers.h" /* includes SIZE_MAX */
17 #include "evp_local.h"
21 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
23 ERR_raise(ERR_LIB_EVP, EVP_R_ONLY_ONESHOT_SUPPORTED);
28 * If we get the "NULL" md then the name comes back as "UNDEF". We want to use
31 static const char *canon_mdname(const char *mdname)
33 if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
39 static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
40 const EVP_MD *type, const char *mdname,
41 OSSL_LIB_CTX *libctx, const char *props,
42 ENGINE *e, EVP_PKEY *pkey, int ver,
43 const OSSL_PARAM params[])
45 EVP_PKEY_CTX *locpctx = NULL;
46 EVP_SIGNATURE *signature = NULL;
47 EVP_KEYMGMT *tmp_keymgmt = NULL;
48 const OSSL_PROVIDER *tmp_prov = NULL;
49 const char *supported_sig = NULL;
50 char locmdname[80] = ""; /* 80 chars should be enough */
52 int ret, iter, reinit = 1;
54 if (!evp_md_ctx_free_algctx(ctx))
57 if (ctx->pctx == NULL) {
60 ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
62 ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
64 if (ctx->pctx == NULL)
70 if (evp_pkey_ctx_is_legacy(locpctx))
73 /* do not reinitialize if pkey is set or operation is different */
76 || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
77 : EVP_PKEY_OP_SIGNCTX)
78 || (signature = locpctx->op.sig.signature) == NULL
79 || locpctx->op.sig.algctx == NULL))
83 props = locpctx->propquery;
85 if (locpctx->pkey == NULL) {
86 ERR_clear_last_mark();
87 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
92 evp_pkey_ctx_free_old_ops(locpctx);
94 if (mdname == NULL && type == NULL)
95 mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
100 * Try to derive the supported signature from |locpctx->keymgmt|.
102 if (!ossl_assert(locpctx->pkey->keymgmt == NULL
103 || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
104 ERR_clear_last_mark();
105 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
108 supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
110 if (supported_sig == NULL) {
111 ERR_clear_last_mark();
112 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
117 * We perform two iterations:
119 * 1. Do the normal signature fetch, using the fetching data given by
121 * 2. Do the provider specific signature fetch, from the same provider
124 * We then try to fetch the keymgmt from the same provider as the
125 * signature, and try to export |ctx->pkey| to that keymgmt (when
126 * this keymgmt happens to be the same as |ctx->keymgmt|, the export
127 * is a no-op, but we call it anyway to not complicate the code even
129 * If the export call succeeds (returns a non-NULL provider key pointer),
130 * we're done and can perform the operation itself. If not, we perform
131 * the second iteration, or jump to legacy.
133 for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
134 EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
137 * If we're on the second iteration, free the results from the first.
138 * They are NULL on the first iteration, so no need to check what
139 * iteration we're on.
141 EVP_SIGNATURE_free(signature);
142 EVP_KEYMGMT_free(tmp_keymgmt);
146 signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
148 if (signature != NULL)
149 tmp_prov = EVP_SIGNATURE_get0_provider(signature);
152 tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
154 evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
155 supported_sig, locpctx->propquery);
156 if (signature == NULL)
160 if (signature == NULL)
164 * Ensure that the key is provided, either natively, or as a cached
165 * export. We start by fetching the keymgmt with the same name as
166 * |locpctx->pkey|, but from the provider of the signature method, using
167 * the same property query as when fetching the signature method.
168 * With the keymgmt we found (if we did), we try to export |locpctx->pkey|
169 * to it (evp_pkey_export_to_provider() is smart enough to only actually
171 * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt)
173 tmp_keymgmt_tofree = tmp_keymgmt =
174 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
175 EVP_KEYMGMT_get0_name(locpctx->keymgmt),
177 if (tmp_keymgmt != NULL)
178 provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
179 &tmp_keymgmt, locpctx->propquery);
180 if (tmp_keymgmt == NULL)
181 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
184 if (provkey == NULL) {
185 EVP_SIGNATURE_free(signature);
186 ERR_clear_last_mark();
187 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
193 /* No more legacy from here down to legacy: */
195 locpctx->op.sig.signature = signature;
196 locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
197 : EVP_PKEY_OP_SIGNCTX;
198 locpctx->op.sig.algctx
199 = signature->newctx(ossl_provider_ctx(signature->prov), props);
200 if (locpctx->op.sig.algctx == NULL) {
201 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
210 ctx->reqdigest = type;
212 mdname = canon_mdname(EVP_MD_get0_name(type));
214 if (mdname == NULL && !reinit) {
215 if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
217 sizeof(locmdname)) > 0) {
218 mdname = canon_mdname(locmdname);
222 if (mdname != NULL) {
224 * We're about to get a new digest so clear anything associated with
227 evp_md_ctx_clear_digest(ctx, 1, 0);
229 /* legacy code support for engines */
232 * This might be requested by a later call to EVP_MD_CTX_get0_md().
233 * In that case the "explicit fetch" rules apply for that
234 * function (as per man pages), i.e. the ref count is not updated
235 * so the EVP_MD should not be used beyond the lifetime of the
238 ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
239 if (ctx->fetched_digest != NULL) {
240 ctx->digest = ctx->reqdigest = ctx->fetched_digest;
242 /* legacy engine support : remove the mark when this is deleted */
243 ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname);
244 if (ctx->digest == NULL) {
245 (void)ERR_clear_last_mark();
246 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
250 (void)ERR_pop_to_mark();
255 if (signature->digest_verify_init == NULL) {
256 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
259 ret = signature->digest_verify_init(locpctx->op.sig.algctx,
260 mdname, provkey, params);
262 if (signature->digest_sign_init == NULL) {
263 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
266 ret = signature->digest_sign_init(locpctx->op.sig.algctx,
267 mdname, provkey, params);
271 * If the operation was not a success and no digest was found, an error
272 * needs to be raised.
274 if (ret > 0 || mdname != NULL)
276 if (type == NULL) /* This check is redundant but clarifies matters */
277 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
280 evp_pkey_ctx_free_old_ops(locpctx);
281 locpctx->operation = EVP_PKEY_OP_UNDEFINED;
282 EVP_KEYMGMT_free(tmp_keymgmt);
287 * If we don't have the full support we need with provided methods,
288 * let's go see if legacy does.
291 EVP_KEYMGMT_free(tmp_keymgmt);
294 if (type == NULL && mdname != NULL)
295 type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
297 if (ctx->pctx->pmeth == NULL) {
298 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
302 if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
306 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
307 type = EVP_get_digestbynid(def_nid);
311 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
317 if (ctx->pctx->pmeth->verifyctx_init) {
318 if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
320 ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
321 } else if (ctx->pctx->pmeth->digestverify != 0) {
322 ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
323 ctx->update = update;
324 } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
328 if (ctx->pctx->pmeth->signctx_init) {
329 if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
331 ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
332 } else if (ctx->pctx->pmeth->digestsign != 0) {
333 ctx->pctx->operation = EVP_PKEY_OP_SIGN;
334 ctx->update = update;
335 } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
339 if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
343 if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
345 if (!EVP_DigestInit_ex(ctx, type, e))
348 * This indicates the current algorithm requires
349 * special treatment before hashing the tbs-message.
351 ctx->pctx->flag_call_digest_custom = 0;
352 if (ctx->pctx->pmeth->digest_custom != NULL)
353 ctx->pctx->flag_call_digest_custom = 1;
360 ret = evp_pkey_ctx_use_cached_data(locpctx);
363 EVP_KEYMGMT_free(tmp_keymgmt);
364 return ret > 0 ? 1 : 0;
367 int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
368 const char *mdname, OSSL_LIB_CTX *libctx,
369 const char *props, EVP_PKEY *pkey,
370 const OSSL_PARAM params[])
372 return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0,
376 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
377 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
379 return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0,
383 int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
384 const char *mdname, OSSL_LIB_CTX *libctx,
385 const char *props, EVP_PKEY *pkey,
386 const OSSL_PARAM params[])
388 return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1,
392 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
393 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
395 return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1,
398 #endif /* FIPS_MDOE */
400 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
402 EVP_PKEY_CTX *pctx = ctx->pctx;
405 || pctx->operation != EVP_PKEY_OP_SIGNCTX
406 || pctx->op.sig.algctx == NULL
407 || pctx->op.sig.signature == NULL)
410 if (pctx->op.sig.signature->digest_sign_update == NULL) {
411 ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
415 return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx,
420 /* do_sigver_init() checked that |digest_custom| is non-NULL */
421 if (pctx->flag_call_digest_custom
422 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
424 pctx->flag_call_digest_custom = 0;
427 return EVP_DigestUpdate(ctx, data, dsize);
430 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
432 EVP_PKEY_CTX *pctx = ctx->pctx;
435 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
436 || pctx->op.sig.algctx == NULL
437 || pctx->op.sig.signature == NULL)
440 if (pctx->op.sig.signature->digest_verify_update == NULL) {
441 ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
445 return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx,
450 /* do_sigver_init() checked that |digest_custom| is non-NULL */
451 if (pctx->flag_call_digest_custom
452 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
454 pctx->flag_call_digest_custom = 0;
457 return EVP_DigestUpdate(ctx, data, dsize);
461 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
465 EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
468 || pctx->operation != EVP_PKEY_OP_SIGNCTX
469 || pctx->op.sig.algctx == NULL
470 || pctx->op.sig.signature == NULL)
473 if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
474 return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx,
476 sigret == NULL ? 0 : *siglen);
477 dctx = EVP_PKEY_CTX_dup(pctx);
481 r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx,
484 EVP_PKEY_CTX_free(dctx);
488 if (pctx == NULL || pctx->pmeth == NULL) {
489 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
493 /* do_sigver_init() checked that |digest_custom| is non-NULL */
494 if (pctx->flag_call_digest_custom
495 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
497 pctx->flag_call_digest_custom = 0;
499 if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
501 return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
502 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
503 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
505 dctx = EVP_PKEY_CTX_dup(pctx);
508 r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
509 EVP_PKEY_CTX_free(dctx);
513 if (pctx->pmeth->signctx != NULL)
517 if (sigret != NULL) {
518 unsigned char md[EVP_MAX_MD_SIZE];
519 unsigned int mdlen = 0;
521 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
523 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
525 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
527 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
531 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
532 EVP_MD_CTX_free(tmp_ctx);
536 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
537 sigret, siglen, tmp_ctx);
539 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
540 EVP_MD_CTX_free(tmp_ctx);
544 if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
548 if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
551 int s = EVP_MD_get_size(ctx->digest);
553 if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
560 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
561 const unsigned char *tbs, size_t tbslen)
563 EVP_PKEY_CTX *pctx = ctx->pctx;
566 && pctx->operation == EVP_PKEY_OP_SIGNCTX
567 && pctx->op.sig.algctx != NULL
568 && pctx->op.sig.signature != NULL) {
569 if (pctx->op.sig.signature->digest_sign != NULL)
570 return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx,
572 sigret == NULL ? 0 : *siglen,
576 if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
577 return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
580 if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
582 return EVP_DigestSignFinal(ctx, sigret, siglen);
585 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
588 unsigned char md[EVP_MAX_MD_SIZE];
590 unsigned int mdlen = 0;
592 EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
595 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
596 || pctx->op.sig.algctx == NULL
597 || pctx->op.sig.signature == NULL)
600 if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
601 return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx,
603 dctx = EVP_PKEY_CTX_dup(pctx);
607 r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx,
609 EVP_PKEY_CTX_free(dctx);
613 if (pctx == NULL || pctx->pmeth == NULL) {
614 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
618 /* do_sigver_init() checked that |digest_custom| is non-NULL */
619 if (pctx->flag_call_digest_custom
620 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
622 pctx->flag_call_digest_custom = 0;
624 if (pctx->pmeth->verifyctx != NULL)
628 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
630 r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
632 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
634 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
637 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
638 EVP_MD_CTX_free(tmp_ctx);
642 r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
643 sig, siglen, tmp_ctx);
645 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
646 EVP_MD_CTX_free(tmp_ctx);
650 return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
653 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
654 size_t siglen, const unsigned char *tbs, size_t tbslen)
656 EVP_PKEY_CTX *pctx = ctx->pctx;
659 && pctx->operation == EVP_PKEY_OP_VERIFYCTX
660 && pctx->op.sig.algctx != NULL
661 && pctx->op.sig.signature != NULL) {
662 if (pctx->op.sig.signature->digest_verify != NULL)
663 return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx,
668 if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
669 return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
672 if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
674 return EVP_DigestVerifyFinal(ctx, sigret, siglen);
676 #endif /* FIPS_MODULE */