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"
19 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
21 EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
25 static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
26 const EVP_MD *type, const char *mdname,
27 const char *props, ENGINE *e, EVP_PKEY *pkey,
28 EVP_SIGNATURE *signature, int ver)
30 EVP_PKEY_CTX *locpctx = NULL;
34 if (ctx->provctx != NULL) {
35 if (!ossl_assert(ctx->digest != NULL)) {
36 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
39 if (ctx->digest->freectx != NULL)
40 ctx->digest->freectx(ctx->provctx);
44 if (ctx->pctx == NULL) {
45 ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
46 if (ctx->pctx == NULL)
48 } else if (pkey != NULL) {
49 if (!EVP_PKEY_up_ref(pkey))
51 EVP_PKEY_free(ctx->pctx->pkey);
52 ctx->pctx->pkey = pkey;
55 evp_pkey_ctx_free_old_ops(locpctx);
56 if (locpctx->pkey == NULL)
59 if (e != NULL || locpctx->engine != NULL)
62 if (signature != NULL) {
63 if (!EVP_SIGNATURE_up_ref(signature))
67 * TODO(3.0): Check for legacy handling. Remove this once all all
68 * algorithms are moved to providers.
70 switch (locpctx->pkey->type) {
77 = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(locpctx->pkey->type), NULL);
79 if (signature == NULL) {
80 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
84 locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
85 : EVP_PKEY_OP_SIGNCTX;
87 locpctx->op.sig.signature = signature;
89 locpctx->op.sig.sigprovctx
90 = signature->newctx(ossl_provider_ctx(signature->prov));
91 if (locpctx->op.sig.sigprovctx == NULL) {
92 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
95 provkey = evp_keymgmt_export_to_provider(locpctx->pkey, signature->keymgmt);
96 if (provkey == NULL) {
97 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
101 if (mdname == NULL) {
102 mdname = EVP_MD_name(type);
103 ctx->reqdigest = type;
106 * This might be requested by a later call to EVP_MD_CTX_md(). In that
107 * case the "explicit fetch" rules apply for that function (as per
108 * man pages), i.e. the ref count is not updated so the EVP_MD should
109 * not be used beyound the lifetime of the EVP_MD_CTX.
112 = ctx->fetched_digest
114 ossl_provider_library_context(EVP_SIGNATURE_provider(signature)),
119 if (signature->digest_verify_init == NULL) {
120 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
123 ret = signature->digest_verify_init(locpctx->op.sig.sigprovctx, mdname,
126 if (signature->digest_sign_init == NULL) {
127 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
130 ret = signature->digest_sign_init(locpctx->op.sig.sigprovctx, mdname,
136 evp_pkey_ctx_free_old_ops(locpctx);
137 locpctx->operation = EVP_PKEY_OP_UNDEFINED;
141 if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
145 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
146 type = EVP_get_digestbynid(def_nid);
150 EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
156 if (ctx->pctx->pmeth->verifyctx_init) {
157 if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
159 ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
160 } else if (ctx->pctx->pmeth->digestverify != 0) {
161 ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
162 ctx->update = update;
163 } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
167 if (ctx->pctx->pmeth->signctx_init) {
168 if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
170 ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
171 } else if (ctx->pctx->pmeth->digestsign != 0) {
172 ctx->pctx->operation = EVP_PKEY_OP_SIGN;
173 ctx->update = update;
174 } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
178 if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
182 if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
184 if (!EVP_DigestInit_ex(ctx, type, e))
187 * This indicates the current algorithm requires
188 * special treatment before hashing the tbs-message.
190 if (ctx->pctx->pmeth->digest_custom != NULL)
191 return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx);
196 int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
197 const char *mdname, const char *props, EVP_PKEY *pkey,
198 EVP_SIGNATURE *signature)
200 return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, signature,
204 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
205 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
207 return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 0);
210 int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
211 const char *mdname, const char *props,
212 EVP_PKEY *pkey, EVP_SIGNATURE *signature)
214 return do_sigver_init(ctx, pctx, NULL, mdname, props, NULL, pkey, signature,
218 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
219 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
221 return do_sigver_init(ctx, pctx, type, NULL, NULL, e, pkey, NULL, 1);
224 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
226 EVP_PKEY_CTX *pctx = ctx->pctx;
229 || pctx->operation != EVP_PKEY_OP_SIGNCTX
230 || pctx->op.sig.sigprovctx == NULL
231 || pctx->op.sig.signature == NULL)
234 return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.sigprovctx,
238 return EVP_DigestUpdate(ctx, data, dsize);
241 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
243 EVP_PKEY_CTX *pctx = ctx->pctx;
246 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
247 || pctx->op.sig.sigprovctx == NULL
248 || pctx->op.sig.signature == NULL)
251 return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.sigprovctx,
255 return EVP_DigestUpdate(ctx, data, dsize);
259 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
263 EVP_PKEY_CTX *pctx = ctx->pctx;
266 || pctx->operation != EVP_PKEY_OP_SIGNCTX
267 || pctx->op.sig.sigprovctx == NULL
268 || pctx->op.sig.signature == NULL)
271 return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.sigprovctx,
272 sigret, siglen, SIZE_MAX);
275 if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
277 return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
278 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
279 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
281 EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);
284 r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
285 EVP_PKEY_CTX_free(dctx);
289 if (pctx->pmeth->signctx)
294 unsigned char md[EVP_MAX_MD_SIZE];
295 unsigned int mdlen = 0;
296 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
298 r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);
300 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
302 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
305 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
306 EVP_MD_CTX_free(tmp_ctx);
310 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
311 sigret, siglen, tmp_ctx);
313 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
314 EVP_MD_CTX_free(tmp_ctx);
318 if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
322 if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
325 int s = EVP_MD_size(ctx->digest);
326 if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
333 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
334 const unsigned char *tbs, size_t tbslen)
336 if (ctx->pctx->pmeth->digestsign != NULL)
337 return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
338 if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
340 return EVP_DigestSignFinal(ctx, sigret, siglen);
343 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
346 unsigned char md[EVP_MAX_MD_SIZE];
348 unsigned int mdlen = 0;
350 EVP_PKEY_CTX *pctx = ctx->pctx;
353 || pctx->operation != EVP_PKEY_OP_VERIFYCTX
354 || pctx->op.sig.sigprovctx == NULL
355 || pctx->op.sig.signature == NULL)
358 return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.sigprovctx,
362 if (ctx->pctx->pmeth->verifyctx)
366 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
368 r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);
370 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
372 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
375 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
376 EVP_MD_CTX_free(tmp_ctx);
380 r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
381 sig, siglen, tmp_ctx);
383 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
384 EVP_MD_CTX_free(tmp_ctx);
388 return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
391 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
392 size_t siglen, const unsigned char *tbs, size_t tbslen)
394 if (ctx->pctx->pmeth->digestverify != NULL)
395 return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
396 if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
398 return EVP_DigestVerifyFinal(ctx, sigret, siglen);