2 * Copyright 2006-2021 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 (ctx->algctx != NULL) {
55 if (!ossl_assert(ctx->digest != NULL)) {
56 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
59 if (ctx->digest->freectx != NULL)
60 ctx->digest->freectx(ctx->algctx);
64 if (ctx->pctx == NULL) {
67 ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
69 ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
71 if (ctx->pctx == NULL)
77 if (evp_pkey_ctx_is_legacy(locpctx))
80 /* do not reinitialize if pkey is set or operation is different */
83 || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
84 : EVP_PKEY_OP_SIGNCTX)
85 || (signature = locpctx->op.sig.signature) == NULL
86 || locpctx->op.sig.algctx == NULL))
90 props = locpctx->propquery;
92 if (locpctx->pkey == NULL) {
93 ERR_clear_last_mark();
94 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
99 evp_pkey_ctx_free_old_ops(locpctx);
101 if (mdname == NULL && type == NULL)
102 mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
107 * Try to derive the supported signature from |locpctx->keymgmt|.
109 if (!ossl_assert(locpctx->pkey->keymgmt == NULL
110 || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
111 ERR_clear_last_mark();
112 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
115 supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
117 if (supported_sig == NULL) {
118 ERR_clear_last_mark();
119 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
124 * We perform two iterations:
126 * 1. Do the normal signature fetch, using the fetching data given by
128 * 2. Do the provider specific signature fetch, from the same provider
131 * We then try to fetch the keymgmt from the same provider as the
132 * signature, and try to export |ctx->pkey| to that keymgmt (when
133 * this keymgmt happens to be the same as |ctx->keymgmt|, the export
134 * is a no-op, but we call it anyway to not complicate the code even
136 * If the export call succeeds (returns a non-NULL provider key pointer),
137 * we're done and can perform the operation itself. If not, we perform
138 * the second iteration, or jump to legacy.
140 for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
141 EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
144 * If we're on the second iteration, free the results from the first.
145 * They are NULL on the first iteration, so no need to check what
146 * iteration we're on.
148 EVP_SIGNATURE_free(signature);
149 EVP_KEYMGMT_free(tmp_keymgmt);
153 signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
155 if (signature != NULL)
156 tmp_prov = EVP_SIGNATURE_get0_provider(signature);
159 tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
161 evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
162 supported_sig, locpctx->propquery);
163 if (signature == NULL)
167 if (signature == NULL)
171 * Ensure that the key is provided, either natively, or as a cached
172 * export. We start by fetching the keymgmt with the same name as
173 * |locpctx->pkey|, but from the provider of the signature method, using
174 * the same property query as when fetching the signature method.
175 * With the keymgmt we found (if we did), we try to export |locpctx->pkey|
176 * to it (evp_pkey_export_to_provider() is smart enough to only actually
178 * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt)
180 tmp_keymgmt_tofree = tmp_keymgmt =
181 evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
182 EVP_KEYMGMT_get0_name(locpctx->keymgmt),
184 if (tmp_keymgmt != NULL)
185 provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
186 &tmp_keymgmt, locpctx->propquery);
187 if (tmp_keymgmt == NULL)
188 EVP_KEYMGMT_free(tmp_keymgmt_tofree);
191 if (provkey == NULL) {
192 EVP_SIGNATURE_free(signature);
193 ERR_clear_last_mark();
194 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
200 /* No more legacy from here down to legacy: */
202 locpctx->op.sig.signature = signature;
203 locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
204 : EVP_PKEY_OP_SIGNCTX;
205 locpctx->op.sig.algctx
206 = signature->newctx(ossl_provider_ctx(signature->prov), props);
207 if (locpctx->op.sig.algctx == NULL) {
208 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
217 ctx->reqdigest = type;
219 mdname = canon_mdname(EVP_MD_get0_name(type));
221 if (mdname == NULL && !reinit) {
222 if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
224 sizeof(locmdname)) > 0) {
225 mdname = canon_mdname(locmdname);
229 if (mdname != NULL) {
231 * We're about to get a new digest so clear anything associated with
234 evp_md_ctx_clear_digest(ctx, 1);
236 /* legacy code support for engines */
239 * This might be requested by a later call to EVP_MD_CTX_get0_md().
240 * In that case the "explicit fetch" rules apply for that
241 * function (as per man pages), i.e. the ref count is not updated
242 * so the EVP_MD should not be used beyound the lifetime of the
245 ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
246 if (ctx->fetched_digest != NULL) {
247 ctx->digest = ctx->reqdigest = ctx->fetched_digest;
249 /* legacy engine support : remove the mark when this is deleted */
250 ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname);
251 if (ctx->digest == NULL) {
252 (void)ERR_clear_last_mark();
253 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
257 (void)ERR_pop_to_mark();
262 if (signature->digest_verify_init == NULL) {
263 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
266 ret = signature->digest_verify_init(locpctx->op.sig.algctx,
267 mdname, provkey, params);
269 if (signature->digest_sign_init == NULL) {
270 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
273 ret = signature->digest_sign_init(locpctx->op.sig.algctx,
274 mdname, provkey, params);
278 * If the operation was not a success and no digest was found, an error
279 * needs to be raised.
281 if (ret > 0 || mdname != NULL)
283 if (type == NULL) /* This check is redundant but clarifies matters */
284 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
287 evp_pkey_ctx_free_old_ops(locpctx);
288 locpctx->operation = EVP_PKEY_OP_UNDEFINED;
289 EVP_KEYMGMT_free(tmp_keymgmt);
294 * If we don't have the full support we need with provided methods,
295 * let's go see if legacy does.
298 EVP_KEYMGMT_free(tmp_keymgmt);
301 if (type == NULL && mdname != NULL)
302 type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
304 if (ctx->pctx->pmeth == NULL) {
305 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
309 if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
313 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
314 type = EVP_get_digestbynid(def_nid);
318 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
324 if (ctx->pctx->pmeth->verifyctx_init) {
325 if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
327 ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
328 } else if (ctx->pctx->pmeth->digestverify != 0) {
329 ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
330 ctx->update = update;
331 } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
335 if (ctx->pctx->pmeth->signctx_init) {
336 if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
338 ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
339 } else if (ctx->pctx->pmeth->digestsign != 0) {
340 ctx->pctx->operation = EVP_PKEY_OP_SIGN;
341 ctx->update = update;
342 } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
346 if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
350 if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
352 if (!EVP_DigestInit_ex(ctx, type, e))
355 * This indicates the current algorithm requires
356 * special treatment before hashing the tbs-message.
358 ctx->pctx->flag_call_digest_custom = 0;
359 if (ctx->pctx->pmeth->digest_custom != NULL)
360 ctx->pctx->flag_call_digest_custom = 1;
367 ret = evp_pkey_ctx_use_cached_data(locpctx);
370 EVP_KEYMGMT_free(tmp_keymgmt);
371 return ret > 0 ? 1 : 0;
374 int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
375 const char *mdname, OSSL_LIB_CTX *libctx,
376 const char *props, EVP_PKEY *pkey,
377 const OSSL_PARAM params[])
379 return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0,
383 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
384 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
386 return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0,
390 int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
391 const char *mdname, OSSL_LIB_CTX *libctx,
392 const char *props, EVP_PKEY *pkey,
393 const OSSL_PARAM params[])
395 return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1,
399 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
400 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
402 return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1,
405 #endif /* FIPS_MDOE */
407 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
409 EVP_PKEY_CTX *pctx = ctx->pctx;
412 || pctx->operation != EVP_PKEY_OP_SIGNCTX
413 || pctx->op.sig.algctx == NULL
414 || pctx->op.sig.signature == NULL)
417 if (pctx->op.sig.signature->digest_sign_update == NULL) {
418 ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
422 return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx,
427 /* do_sigver_init() checked that |digest_custom| is non-NULL */
428 if (pctx->flag_call_digest_custom
429 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
431 pctx->flag_call_digest_custom = 0;
434 return EVP_DigestUpdate(ctx, data, dsize);
437 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
439 EVP_PKEY_CTX *pctx = ctx->pctx;
442 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
443 || pctx->op.sig.algctx == NULL
444 || pctx->op.sig.signature == NULL)
447 if (pctx->op.sig.signature->digest_verify_update == NULL) {
448 ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
452 return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx,
457 /* do_sigver_init() checked that |digest_custom| is non-NULL */
458 if (pctx->flag_call_digest_custom
459 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
461 pctx->flag_call_digest_custom = 0;
464 return EVP_DigestUpdate(ctx, data, dsize);
468 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
472 EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
475 || pctx->operation != EVP_PKEY_OP_SIGNCTX
476 || pctx->op.sig.algctx == NULL
477 || pctx->op.sig.signature == NULL)
480 if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
481 return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx,
483 (siglen == NULL) ? 0 : *siglen);
484 dctx = EVP_PKEY_CTX_dup(pctx);
488 r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx,
490 (siglen == NULL) ? 0 : *siglen);
491 EVP_PKEY_CTX_free(dctx);
495 if (pctx == NULL || pctx->pmeth == NULL) {
496 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
500 /* do_sigver_init() checked that |digest_custom| is non-NULL */
501 if (pctx->flag_call_digest_custom
502 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
504 pctx->flag_call_digest_custom = 0;
506 if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
508 return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
509 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
510 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
512 dctx = EVP_PKEY_CTX_dup(pctx);
515 r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
516 EVP_PKEY_CTX_free(dctx);
520 if (pctx->pmeth->signctx != NULL)
524 if (sigret != NULL) {
525 unsigned char md[EVP_MAX_MD_SIZE];
526 unsigned int mdlen = 0;
528 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
530 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
532 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
534 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
538 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
539 EVP_MD_CTX_free(tmp_ctx);
543 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
544 sigret, siglen, tmp_ctx);
546 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
547 EVP_MD_CTX_free(tmp_ctx);
551 if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
555 if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
558 int s = EVP_MD_get_size(ctx->digest);
560 if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
567 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
568 const unsigned char *tbs, size_t tbslen)
570 EVP_PKEY_CTX *pctx = ctx->pctx;
573 && pctx->operation == EVP_PKEY_OP_SIGNCTX
574 && pctx->op.sig.algctx != NULL
575 && pctx->op.sig.signature != NULL) {
576 if (pctx->op.sig.signature->digest_sign != NULL)
577 return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx,
579 sigret == NULL ? 0 : *siglen,
583 if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
584 return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
587 if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
589 return EVP_DigestSignFinal(ctx, sigret, siglen);
592 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
595 unsigned char md[EVP_MAX_MD_SIZE];
597 unsigned int mdlen = 0;
599 EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
602 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
603 || pctx->op.sig.algctx == NULL
604 || pctx->op.sig.signature == NULL)
607 if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
608 return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx,
610 dctx = EVP_PKEY_CTX_dup(pctx);
614 r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx,
616 EVP_PKEY_CTX_free(dctx);
620 if (pctx == NULL || pctx->pmeth == NULL) {
621 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
625 /* do_sigver_init() checked that |digest_custom| is non-NULL */
626 if (pctx->flag_call_digest_custom
627 && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
629 pctx->flag_call_digest_custom = 0;
631 if (pctx->pmeth->verifyctx != NULL)
635 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
637 r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
639 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
641 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
644 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
645 EVP_MD_CTX_free(tmp_ctx);
649 r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
650 sig, siglen, tmp_ctx);
652 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
653 EVP_MD_CTX_free(tmp_ctx);
657 return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
660 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
661 size_t siglen, const unsigned char *tbs, size_t tbslen)
663 EVP_PKEY_CTX *pctx = ctx->pctx;
666 && pctx->operation == EVP_PKEY_OP_VERIFYCTX
667 && pctx->op.sig.algctx != NULL
668 && pctx->op.sig.signature != NULL) {
669 if (pctx->op.sig.signature->digest_verify != NULL)
670 return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx,
675 if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
676 return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
679 if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
681 return EVP_DigestVerifyFinal(ctx, sigret, siglen);
683 #endif /* FIPS_MODULE */