2 * Copyright 2006-2018 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 <openssl/x509.h>
15 #include "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
21 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
23 EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
27 static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
28 const EVP_MD *type, const char *mdname,
29 const char *props, ENGINE *e, EVP_PKEY *pkey,
30 EVP_SIGNATURE *signature, int ver)
32 EVP_PKEY_CTX *locpctx = NULL;
36 if (ctx->provctx != NULL) {
37 if (!ossl_assert(ctx->digest != NULL)) {
38 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
41 if (ctx->digest->freectx != NULL)
42 ctx->digest->freectx(ctx->provctx);
46 if (ctx->pctx == NULL) {
47 ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
48 if (ctx->pctx == NULL)
50 } else if (pkey != NULL) {
51 if (!EVP_PKEY_up_ref(pkey))
53 EVP_PKEY_free(ctx->pctx->pkey);
54 ctx->pctx->pkey = pkey;
57 evp_pkey_ctx_free_old_ops(locpctx);
58 if (locpctx->pkey == NULL)
61 if (e != NULL || locpctx->engine != NULL)
64 if (signature != NULL) {
65 if (!EVP_SIGNATURE_up_ref(signature))
69 * TODO(3.0): Check for legacy handling. Remove this once all all
70 * algorithms are moved to providers.
72 switch (locpctx->pkey->type) {
79 = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(locpctx->pkey->type), NULL);
81 if (signature == NULL) {
82 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
86 locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
87 : EVP_PKEY_OP_SIGNCTX;
89 locpctx->op.sig.signature = signature;
91 locpctx->op.sig.sigprovctx
92 = signature->newctx(ossl_provider_ctx(signature->prov));
93 if (locpctx->op.sig.sigprovctx == NULL) {
94 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
97 provkey = evp_keymgmt_export_to_provider(locpctx->pkey, signature->keymgmt);
98 if (provkey == NULL) {
99 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
103 if (mdname == NULL) {
104 mdname = EVP_MD_name(type);
105 ctx->reqdigest = type;
108 * This might be requested by a later call to EVP_MD_CTX_md(). In that
109 * case the "explicit fetch" rules apply for that function (as per
110 * man pages), i.e. the ref count is not updated so the EVP_MD should
111 * not be used beyound the lifetime of the EVP_MD_CTX.
114 = ctx->fetched_digest
116 ossl_provider_library_context(EVP_SIGNATURE_provider(signature)),
121 if (signature->digest_verify_init == NULL) {
122 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
125 ret = signature->digest_verify_init(locpctx->op.sig.sigprovctx, mdname,
128 if (signature->digest_sign_init == NULL) {
129 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
132 ret = signature->digest_sign_init(locpctx->op.sig.sigprovctx, mdname,
138 evp_pkey_ctx_free_old_ops(locpctx);
139 locpctx->operation = EVP_PKEY_OP_UNDEFINED;
143 if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
147 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
148 type = EVP_get_digestbynid(def_nid);
152 EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
158 if (ctx->pctx->pmeth->verifyctx_init) {
159 if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
161 ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
162 } else if (ctx->pctx->pmeth->digestverify != 0) {
163 ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
164 ctx->update = update;
165 } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
169 if (ctx->pctx->pmeth->signctx_init) {
170 if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
172 ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
173 } else if (ctx->pctx->pmeth->digestsign != 0) {
174 ctx->pctx->operation = EVP_PKEY_OP_SIGN;
175 ctx->update = update;
176 } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
180 if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
184 if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
186 if (!EVP_DigestInit_ex(ctx, type, e))
189 * This indicates the current algorithm requires
190 * special treatment before hashing the tbs-message.
192 if (ctx->pctx->pmeth->digest_custom != NULL)
193 return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx);
198 int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
199 const char *mdname, const char *props, EVP_PKEY *pkey,
200 EVP_SIGNATURE *signature)
202 return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, signature,
206 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
207 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
209 return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 0);
212 int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
213 const char *mdname, const char *props,
214 EVP_PKEY *pkey, EVP_SIGNATURE *signature)
216 return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, signature,
220 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
221 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
223 return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 1);
225 #endif /* FIPS_MDOE */
227 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
229 EVP_PKEY_CTX *pctx = ctx->pctx;
232 || pctx->operation != EVP_PKEY_OP_SIGNCTX
233 || pctx->op.sig.sigprovctx == NULL
234 || pctx->op.sig.signature == NULL)
237 return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.sigprovctx,
241 return EVP_DigestUpdate(ctx, data, dsize);
244 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
246 EVP_PKEY_CTX *pctx = ctx->pctx;
249 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
250 || pctx->op.sig.sigprovctx == NULL
251 || pctx->op.sig.signature == NULL)
254 return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.sigprovctx,
258 return EVP_DigestUpdate(ctx, data, dsize);
262 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
266 EVP_PKEY_CTX *pctx = ctx->pctx;
269 || pctx->operation != EVP_PKEY_OP_SIGNCTX
270 || pctx->op.sig.sigprovctx == NULL
271 || pctx->op.sig.signature == NULL)
274 return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.sigprovctx,
275 sigret, siglen, SIZE_MAX);
278 if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
280 return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
281 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
282 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
284 EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);
287 r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
288 EVP_PKEY_CTX_free(dctx);
292 if (pctx->pmeth->signctx)
297 unsigned char md[EVP_MAX_MD_SIZE];
298 unsigned int mdlen = 0;
299 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
301 r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);
303 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
305 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
308 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
309 EVP_MD_CTX_free(tmp_ctx);
313 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
314 sigret, siglen, tmp_ctx);
316 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
317 EVP_MD_CTX_free(tmp_ctx);
321 if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
325 if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
328 int s = EVP_MD_size(ctx->digest);
329 if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
336 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
337 const unsigned char *tbs, size_t tbslen)
339 if (ctx->pctx->pmeth->digestsign != NULL)
340 return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
341 if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
343 return EVP_DigestSignFinal(ctx, sigret, siglen);
346 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
349 unsigned char md[EVP_MAX_MD_SIZE];
351 unsigned int mdlen = 0;
353 EVP_PKEY_CTX *pctx = ctx->pctx;
356 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
357 || pctx->op.sig.sigprovctx == NULL
358 || pctx->op.sig.signature == NULL)
361 return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.sigprovctx,
365 if (ctx->pctx->pmeth->verifyctx)
369 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
371 r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);
373 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
375 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
378 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
379 EVP_MD_CTX_free(tmp_ctx);
383 r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
384 sig, siglen, tmp_ctx);
386 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
387 EVP_MD_CTX_free(tmp_ctx);
391 return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
394 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
395 size_t siglen, const unsigned char *tbs, size_t tbslen)
397 if (ctx->pctx->pmeth->digestverify != NULL)
398 return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
399 if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
401 return EVP_DigestVerifyFinal(ctx, sigret, siglen);
403 #endif /* FIPS_MODE */