]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/digest.c
Fix a use after free issue when a provider context is being used and isn't cached
[thirdparty/openssl.git] / crypto / evp / digest.c
CommitLineData
62867571 1/*
33388b44 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
5ba372b1 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
5ba372b1 8 */
d02b48c6 9
e4468e6d
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
d02b48c6 13#include <stdio.h>
ec577822
BM
14#include <openssl/objects.h>
15#include <openssl/evp.h>
ded346fa 16#include <openssl/ec.h>
3c27208f 17#include <openssl/engine.h>
d5e5e2ff
SL
18#include <openssl/params.h>
19#include <openssl/core_names.h>
20#include "internal/cryptlib.h"
25f2138b 21#include "crypto/evp.h"
3653d0c2 22#include "internal/provider.h"
706457b7 23#include "evp_local.h"
d02b48c6 24
aa64cf24 25
3101ab60
MC
26void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force)
27{
8c8cf0d9 28 if (ctx->provctx != NULL) {
8549b972 29 if (ctx->digest != NULL && ctx->digest->freectx != NULL)
8c8cf0d9
MC
30 ctx->digest->freectx(ctx->provctx);
31 ctx->provctx = NULL;
32 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
33 }
34
8c8cf0d9 35 /* TODO(3.0): Remove legacy code below */
8c8cf0d9 36
74cabf3f
RL
37 /*
38 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
39 * sometimes only copies of the context are ever finalised.
40 */
41 if (ctx->digest && ctx->digest->cleanup
42 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
43 ctx->digest->cleanup(ctx);
44 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
3101ab60 45 && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE) || force))
74cabf3f 46 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
3101ab60
MC
47 if (force)
48 ctx->digest = NULL;
319e518a 49
f844f9eb 50#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
7c96dbcd 51 ENGINE_finish(ctx->engine);
3101ab60 52 ctx->engine = NULL;
74cabf3f 53#endif
8549b972
P
54
55 /* Non legacy code, this has to be later than the ctx->digest cleaning */
56 EVP_MD_free(ctx->fetched_digest);
57 ctx->fetched_digest = NULL;
58 ctx->reqdigest = NULL;
3101ab60 59}
14bec2c4 60
3101ab60
MC
61/* This call frees resources associated with the context */
62int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
63{
64 if (ctx == NULL)
65 return 1;
66
67#ifndef FIPS_MODULE
68 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
69 /*
70 * pctx should be freed by the user of EVP_MD_CTX
71 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
72 */
73 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
74 EVP_PKEY_CTX_free(ctx->pctx);
75 ctx->pctx = NULL;
76 }
77#endif
14bec2c4 78
3101ab60 79 evp_md_ctx_clear_digest(ctx, 0);
3ce2fdab 80 OPENSSL_cleanse(ctx, sizeof(*ctx));
74cabf3f
RL
81
82 return 1;
0f113f3e 83}
dbad1690 84
ded346fa 85#ifndef FIPS_MODULE
d8652be0 86EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
b4250010 87 OSSL_LIB_CTX *libctx, const char *propq)
ded346fa
DDO
88{
89 EVP_MD_CTX *ctx;
90 EVP_PKEY_CTX *pctx = NULL;
91
92 if ((ctx = EVP_MD_CTX_new()) == NULL
93 || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
9311d0c4 94 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
ded346fa
DDO
95 goto err;
96 }
97
c2403f36 98 if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
ded346fa 99 goto err;
ded346fa
DDO
100
101 EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
102 return ctx;
103
104 err:
105 EVP_PKEY_CTX_free(pctx);
106 EVP_MD_CTX_free(ctx);
107 return NULL;
108}
109#endif
110
959ed531 111EVP_MD_CTX *EVP_MD_CTX_new(void)
0f113f3e 112{
74cabf3f
RL
113 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
114}
dbad1690 115
959ed531 116void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
74cabf3f 117{
8c8cf0d9
MC
118 if (ctx == NULL)
119 return;
120
8c8cf0d9
MC
121 EVP_MD_CTX_reset(ctx);
122
8c8cf0d9
MC
123 OPENSSL_free(ctx);
124 return;
0f113f3e 125}
dbad1690 126
2dc769a1 127int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
0f113f3e 128{
959ed531 129 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
130 return EVP_DigestInit_ex(ctx, type, NULL);
131}
0fea7ed4 132
11a57c7b 133int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
0f113f3e 134{
f844f9eb 135#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
8c8cf0d9 136 ENGINE *tmpimpl = NULL;
319e518a 137#endif
8c8cf0d9 138
b0002eb0
MC
139#if !defined(FIPS_MODULE)
140 if (ctx->pctx != NULL
141 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
142 && ctx->pctx->op.sig.sigprovctx != NULL) {
143 /*
144 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
145 * previously initialised with EVP_DigestSignInit() would retain
146 * information about the key, and re-initialise for another sign
147 * operation. So in that case we redirect to EVP_DigestSignInit()
148 */
149 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
150 return EVP_DigestSignInit(ctx, NULL, type, impl, NULL);
151 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
152 return EVP_DigestVerifyInit(ctx, NULL, type, impl, NULL);
9311d0c4 153 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
b0002eb0
MC
154 return 0;
155 }
156#endif
157
0f113f3e 158 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
8c8cf0d9 159
15de965f
MC
160 if (ctx->provctx != NULL) {
161 if (!ossl_assert(ctx->digest != NULL)) {
9311d0c4 162 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
15de965f
MC
163 return 0;
164 }
165 if (ctx->digest->freectx != NULL)
166 ctx->digest->freectx(ctx->provctx);
167 ctx->provctx = NULL;
168 }
169
5de9863b 170 if (type != NULL) {
b7c913c8 171 ctx->reqdigest = type;
5de9863b
TM
172 } else {
173 if (ctx->digest == NULL) {
174 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
175 return 0;
176 }
177 type = ctx->digest;
178 }
b7c913c8 179
8c8cf0d9 180 /* TODO(3.0): Legacy work around code below. Remove this */
f844f9eb 181#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e
MC
182 /*
183 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
184 * this context may already have an ENGINE! Try to avoid releasing the
185 * previous handle, re-querying for an ENGINE, and having a
0d4fb843 186 * reinitialisation, when it may all be unnecessary.
0f113f3e 187 */
a93e0e78
MRA
188 if (ctx->engine && ctx->digest &&
189 (type == NULL || (type->type == ctx->digest->type)))
0f113f3e 190 goto skip_to_init;
8c8cf0d9 191
98475995
RL
192 if (type != NULL) {
193 /*
194 * Ensure an ENGINE left lying around from last time is cleared (the
195 * previous check attempted to avoid this if the same ENGINE and
196 * EVP_MD could be used).
197 */
198 ENGINE_finish(ctx->engine);
199 ctx->engine = NULL;
200 }
201
8c8cf0d9
MC
202 if (type != NULL && impl == NULL)
203 tmpimpl = ENGINE_get_digest_engine(type->type);
204#endif
205
206 /*
19cfe784
MC
207 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
208 * should use legacy handling for now.
8c8cf0d9
MC
209 */
210 if (ctx->engine != NULL
211 || impl != NULL
f844f9eb 212#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
8c8cf0d9 213 || tmpimpl != NULL
319e518a 214#endif
8c8cf0d9
MC
215 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
216 if (ctx->digest == ctx->fetched_digest)
217 ctx->digest = NULL;
3fd70262 218 EVP_MD_free(ctx->fetched_digest);
8c8cf0d9
MC
219 ctx->fetched_digest = NULL;
220 goto legacy;
221 }
222
8c8cf0d9
MC
223 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
224 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
225 ctx->md_data = NULL;
226 }
227
228 /* TODO(3.0): Start of non-legacy code below */
229
230 if (type->prov == NULL) {
f844f9eb 231#ifdef FIPS_MODULE
79c44b4e 232 /* We only do explicit fetches inside the FIPS module */
9311d0c4 233 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
319e518a
MC
234 return 0;
235#else
236 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
237
ec0ce188 238 if (provmd == NULL)
8c8cf0d9 239 return 0;
8c8cf0d9 240 type = provmd;
3fd70262 241 EVP_MD_free(ctx->fetched_digest);
8c8cf0d9 242 ctx->fetched_digest = provmd;
319e518a 243#endif
8c8cf0d9
MC
244 }
245
d5e5e2ff
SL
246 if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
247 if (ctx->digest->freectx != NULL)
248 ctx->digest->freectx(ctx->provctx);
249 ctx->provctx = NULL;
250 }
8c8cf0d9
MC
251 ctx->digest = type;
252 if (ctx->provctx == NULL) {
a39eb840 253 ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
8c8cf0d9 254 if (ctx->provctx == NULL) {
9311d0c4 255 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
8c8cf0d9
MC
256 return 0;
257 }
258 }
259
260 if (ctx->digest->dinit == NULL) {
9311d0c4 261 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
8c8cf0d9
MC
262 return 0;
263 }
264
265 return ctx->digest->dinit(ctx->provctx);
266
267 /* TODO(3.0): Remove legacy code below */
268 legacy:
269
f844f9eb 270#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e 271 if (type) {
7c96dbcd 272 if (impl != NULL) {
0f113f3e 273 if (!ENGINE_init(impl)) {
9311d0c4 274 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
0f113f3e
MC
275 return 0;
276 }
7c96dbcd 277 } else {
0f113f3e 278 /* Ask if an ENGINE is reserved for this job */
8c8cf0d9 279 impl = tmpimpl;
7c96dbcd
RS
280 }
281 if (impl != NULL) {
0f113f3e
MC
282 /* There's an ENGINE for this job ... (apparently) */
283 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
7c96dbcd
RS
284
285 if (d == NULL) {
9311d0c4 286 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
0f113f3e
MC
287 ENGINE_finish(impl);
288 return 0;
289 }
290 /* We'll use the ENGINE's private digest definition */
291 type = d;
292 /*
293 * Store the ENGINE functional reference so we know 'type' came
294 * from an ENGINE and we need to release it when done.
295 */
296 ctx->engine = impl;
297 } else
298 ctx->engine = NULL;
0f113f3e 299 }
90e8a310 300#endif
0f113f3e 301 if (ctx->digest != type) {
ffe9150b 302 if (ctx->digest && ctx->digest->ctx_size) {
a93e0e78 303 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
ffe9150b
MC
304 ctx->md_data = NULL;
305 }
0f113f3e
MC
306 ctx->digest = type;
307 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
308 ctx->update = type->update;
84c15091 309 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
0f113f3e 310 if (ctx->md_data == NULL) {
9311d0c4 311 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
312 return 0;
313 }
314 }
315 }
f844f9eb 316#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e 317 skip_to_init:
0b13e9f0 318#endif
f844f9eb 319#ifndef FIPS_MODULE
dfcb5d29
MC
320 /*
321 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
322 * or when using providers.
323 */
864b89ce
MC
324 if (ctx->pctx != NULL
325 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
326 || ctx->pctx->op.sig.signature == NULL)) {
0f113f3e
MC
327 int r;
328 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
329 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
330 if (r <= 0 && (r != -2))
331 return 0;
332 }
319e518a 333#endif
0f113f3e
MC
334 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
335 return 1;
336 return ctx->digest->init(ctx);
337}
d02b48c6 338
f80921b6 339int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
0f113f3e 340{
a8274ea3
MC
341 if (count == 0)
342 return 1;
343
72df8f88
MC
344 if (ctx->pctx != NULL
345 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
346 && ctx->pctx->op.sig.sigprovctx != NULL) {
347 /*
348 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
349 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
350 * Some code calls EVP_DigestUpdate() directly even when initialised
d8652be0
MC
351 * with EVP_DigestSignInit_ex() or
352 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
0ab18e79 353 * the correct EVP_Digest*Update() function
72df8f88
MC
354 */
355 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
356 return EVP_DigestSignUpdate(ctx, data, count);
357 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
358 return EVP_DigestVerifyUpdate(ctx, data, count);
9311d0c4 359 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
72df8f88
MC
360 return 0;
361 }
362
557d6737
MC
363 if (ctx->digest == NULL
364 || ctx->digest->prov == NULL
365 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
8c8cf0d9
MC
366 goto legacy;
367
368 if (ctx->digest->dupdate == NULL) {
9311d0c4 369 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
8c8cf0d9
MC
370 return 0;
371 }
372 return ctx->digest->dupdate(ctx->provctx, data, count);
373
374 /* TODO(3.0): Remove legacy code below */
375 legacy:
0f113f3e
MC
376 return ctx->update(ctx, data, count);
377}
d02b48c6 378
dbad1690 379/* The caller can assume that this removes any secret data from the context */
2dc769a1 380int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
0f113f3e
MC
381{
382 int ret;
383 ret = EVP_DigestFinal_ex(ctx, md, size);
959ed531 384 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
385 return ret;
386}
20d2186c
DSH
387
388/* The caller can assume that this removes any secret data from the context */
8c8cf0d9 389int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
0f113f3e 390{
4bec3f6d 391 int ret, sz;
8c8cf0d9 392 size_t size = 0;
4bec3f6d
SL
393 size_t mdsize = 0;
394
395 if (ctx->digest == NULL)
396 return 0;
8c8cf0d9 397
4bec3f6d
SL
398 sz = EVP_MD_size(ctx->digest);
399 if (sz < 0)
400 return 0;
401 mdsize = sz;
402 if (ctx->digest->prov == NULL)
8c8cf0d9 403 goto legacy;
54a656ef 404
8c8cf0d9 405 if (ctx->digest->dfinal == NULL) {
9311d0c4 406 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
8c8cf0d9
MC
407 return 0;
408 }
409
0ad50b4d 410 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
8c8cf0d9
MC
411
412 if (isize != NULL) {
413 if (size <= UINT_MAX) {
414 *isize = (int)size;
415 } else {
9311d0c4 416 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
8c8cf0d9
MC
417 ret = 0;
418 }
419 }
420
8c8cf0d9
MC
421 return ret;
422
423 /* TODO(3.0): Remove legacy code below */
424 legacy:
0ad50b4d 425 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
0f113f3e 426 ret = ctx->digest->final(ctx, md);
8c8cf0d9 427 if (isize != NULL)
0ad50b4d 428 *isize = mdsize;
0f113f3e
MC
429 if (ctx->digest->cleanup) {
430 ctx->digest->cleanup(ctx);
431 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
432 }
3ce2fdab 433 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
0f113f3e
MC
434 return ret;
435}
351d8998 436
cd8d1456
AP
437int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
438{
439 int ret = 0;
d5e5e2ff
SL
440 OSSL_PARAM params[2];
441 size_t i = 0;
442
443 if (ctx->digest == NULL || ctx->digest->prov == NULL)
444 goto legacy;
cd8d1456 445
d5e5e2ff 446 if (ctx->digest->dfinal == NULL) {
9311d0c4 447 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
d5e5e2ff
SL
448 return 0;
449 }
450
4e7991b4 451 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
d5e5e2ff
SL
452 params[i++] = OSSL_PARAM_construct_end();
453
454 if (EVP_MD_CTX_set_params(ctx, params) > 0)
455 ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
39fde64a 456
d5e5e2ff
SL
457 return ret;
458
459legacy:
cd8d1456
AP
460 if (ctx->digest->flags & EVP_MD_FLAG_XOF
461 && size <= INT_MAX
462 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
463 ret = ctx->digest->final(ctx, md);
cd8d1456
AP
464 if (ctx->digest->cleanup != NULL) {
465 ctx->digest->cleanup(ctx);
466 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
467 }
468 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
469 } else {
9311d0c4 470 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
cd8d1456
AP
471 }
472
473 return ret;
474}
475
dbad1690 476int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e 477{
959ed531 478 EVP_MD_CTX_reset(out);
0f113f3e
MC
479 return EVP_MD_CTX_copy_ex(out, in);
480}
20d2186c
DSH
481
482int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e
MC
483{
484 unsigned char *tmp_buf;
8c8cf0d9
MC
485
486 if (in == NULL || in->digest == NULL) {
9311d0c4 487 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
0f113f3e
MC
488 return 0;
489 }
8c8cf0d9 490
557d6737
MC
491 if (in->digest->prov == NULL
492 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
8c8cf0d9
MC
493 goto legacy;
494
495 if (in->digest->dupctx == NULL) {
9311d0c4 496 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
8c8cf0d9
MC
497 return 0;
498 }
499
500 EVP_MD_CTX_reset(out);
501 if (out->fetched_digest != NULL)
3fd70262 502 EVP_MD_free(out->fetched_digest);
8c8cf0d9
MC
503 *out = *in;
504 /* NULL out pointers in case of error */
505 out->pctx = NULL;
506 out->provctx = NULL;
507
508 if (in->fetched_digest != NULL)
70c35fd1 509 EVP_MD_up_ref(in->fetched_digest);
8c8cf0d9 510
ada0670b
MC
511 if (in->provctx != NULL) {
512 out->provctx = in->digest->dupctx(in->provctx);
513 if (out->provctx == NULL) {
9311d0c4 514 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
ada0670b
MC
515 return 0;
516 }
8c8cf0d9
MC
517 }
518
519 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
520 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
f844f9eb 521#ifndef FIPS_MODULE
319e518a 522 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
8c8cf0d9
MC
523 if (in->pctx != NULL) {
524 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
525 if (out->pctx == NULL) {
9311d0c4 526 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
8c8cf0d9
MC
527 EVP_MD_CTX_reset(out);
528 return 0;
529 }
530 }
319e518a 531#endif
8c8cf0d9
MC
532
533 return 1;
534
535 /* TODO(3.0): Remove legacy code below */
536 legacy:
f844f9eb 537#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e
MC
538 /* Make sure it's safe to copy a digest context using an ENGINE */
539 if (in->engine && !ENGINE_init(in->engine)) {
9311d0c4 540 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
0f113f3e
MC
541 return 0;
542 }
0b13e9f0 543#endif
26188931 544
0f113f3e
MC
545 if (out->digest == in->digest) {
546 tmp_buf = out->md_data;
547 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
548 } else
549 tmp_buf = NULL;
959ed531 550 EVP_MD_CTX_reset(out);
b4faea50 551 memcpy(out, in, sizeof(*out));
0f113f3e 552
4803717f
PY
553 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
554 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
555
6aa0ba4b
RL
556 /* Null these variables, since they are getting fixed up
557 * properly below. Anything else may cause a memleak and/or
558 * double free if any of the memory allocations below fail
559 */
560 out->md_data = NULL;
561 out->pctx = NULL;
562
0f113f3e
MC
563 if (in->md_data && out->digest->ctx_size) {
564 if (tmp_buf)
565 out->md_data = tmp_buf;
566 else {
567 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
90945fa3 568 if (out->md_data == NULL) {
9311d0c4 569 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
570 return 0;
571 }
572 }
573 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
574 }
26188931 575
0f113f3e 576 out->update = in->update;
d4575825 577
f844f9eb 578#ifndef FIPS_MODULE
319e518a 579 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
0f113f3e
MC
580 if (in->pctx) {
581 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
582 if (!out->pctx) {
959ed531 583 EVP_MD_CTX_reset(out);
0f113f3e
MC
584 return 0;
585 }
586 }
319e518a 587#endif
18327cd0 588
0f113f3e
MC
589 if (out->digest->copy)
590 return out->digest->copy(out, in);
3a828611 591
0f113f3e
MC
592 return 1;
593}
88ce56f8 594
9e0aad9f 595int EVP_Digest(const void *data, size_t count,
0f113f3e
MC
596 unsigned char *md, unsigned int *size, const EVP_MD *type,
597 ENGINE *impl)
598{
bfb0641f 599 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e 600 int ret;
dbad1690 601
74cabf3f
RL
602 if (ctx == NULL)
603 return 0;
604 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
605 ret = EVP_DigestInit_ex(ctx, type, impl)
606 && EVP_DigestUpdate(ctx, data, count)
607 && EVP_DigestFinal_ex(ctx, md, size);
bfb0641f 608 EVP_MD_CTX_free(ctx);
dbad1690 609
0f113f3e
MC
610 return ret;
611}
dbad1690 612
ae3ff60e
RL
613int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
614{
615 if (digest != NULL && digest->get_params != NULL)
616 return digest->get_params(params);
617 return 0;
618}
619
620const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
621{
622 if (digest != NULL && digest->gettable_params != NULL)
18ec26ba
P
623 return digest->gettable_params(
624 ossl_provider_ctx(EVP_MD_provider(digest)));
ae3ff60e
RL
625 return NULL;
626}
627
d5e5e2ff
SL
628int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
629{
5f5c3b4f
MC
630 EVP_PKEY_CTX *pctx = ctx->pctx;
631
ada0670b 632 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
633 if (pctx != NULL
634 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
635 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
636 && pctx->op.sig.sigprovctx != NULL
637 && pctx->op.sig.signature->set_ctx_md_params != NULL)
638 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.sigprovctx,
639 params);
ada0670b
MC
640
641 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
642 return ctx->digest->set_ctx_params(ctx->provctx, params);
643
d5e5e2ff
SL
644 return 0;
645}
646
e6879a31 647const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
ae3ff60e 648{
e6879a31 649 if (md != NULL && md->settable_ctx_params != NULL)
18ec26ba 650 return md->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md)));
e6879a31
MC
651 return NULL;
652}
653
654const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
655{
5f5c3b4f
MC
656 EVP_PKEY_CTX *pctx;
657
18ec26ba
P
658 if (ctx == NULL)
659 return NULL;
660
ada0670b 661 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
662 pctx = ctx->pctx;
663 if (pctx != NULL
664 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
665 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
666 && pctx->op.sig.sigprovctx != NULL
667 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
668 return pctx->op.sig.signature->settable_ctx_md_params(
669 pctx->op.sig.sigprovctx);
670
ada0670b
MC
671 if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL)
672 return ctx->digest->settable_ctx_params(
673 ossl_provider_ctx(EVP_MD_provider(ctx->digest)));
674
ae3ff60e
RL
675 return NULL;
676}
677
4e7991b4 678int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
d5e5e2ff 679{
5f5c3b4f
MC
680 EVP_PKEY_CTX *pctx = ctx->pctx;
681
ada0670b 682 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
683 if (pctx != NULL
684 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
685 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
686 && pctx->op.sig.sigprovctx != NULL
687 && pctx->op.sig.signature->get_ctx_md_params != NULL)
688 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.sigprovctx,
689 params);
690
ada0670b
MC
691 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
692 return ctx->digest->get_ctx_params(ctx->provctx, params);
693
d5e5e2ff
SL
694 return 0;
695}
696
e6879a31 697const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
ae3ff60e 698{
e6879a31 699 if (md != NULL && md->gettable_ctx_params != NULL)
18ec26ba 700 return md->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md)));
e6879a31
MC
701 return NULL;
702}
703
704const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
705{
5f5c3b4f
MC
706 EVP_PKEY_CTX *pctx;
707
825ccf51
SL
708 if (ctx == NULL)
709 return NULL;
710
ada0670b 711 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
712 pctx = ctx->pctx;
713 if (pctx != NULL
714 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
715 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
716 && pctx->op.sig.sigprovctx != NULL
717 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
718 return pctx->op.sig.signature->gettable_ctx_md_params(
719 pctx->op.sig.sigprovctx);
720
ada0670b
MC
721 if (ctx->digest != NULL
722 && ctx->digest->gettable_ctx_params != NULL)
723 return ctx->digest->gettable_ctx_params(
724 ossl_provider_ctx(EVP_MD_provider(ctx->digest)));
725
ae3ff60e
RL
726 return NULL;
727}
728
83b4a243 729/* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
396d5fd0
DSH
730int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
731{
6a3b7c68
RL
732 int ret = EVP_CTRL_RET_UNSUPPORTED;
733 int set_params = 1;
734 size_t sz;
735 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
736
a2b62316
MC
737 if (ctx == NULL) {
738 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
6a3b7c68
RL
739 return 0;
740 }
741
a2b62316 742 if (ctx->digest != NULL && ctx->digest->prov == NULL)
6a3b7c68
RL
743 goto legacy;
744
745 switch (cmd) {
746 case EVP_MD_CTRL_XOF_LEN:
747 sz = (size_t)p1;
748 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
749 break;
750 case EVP_MD_CTRL_MICALG:
751 set_params = 0;
752 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
4e7991b4 753 p2, p1 ? p1 : 9999);
6a3b7c68 754 break;
c3885102
MC
755 case EVP_CTRL_SSL3_MASTER_SECRET:
756 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
757 p2, p1);
758 break;
6a3b7c68 759 default:
6a36f209 760 goto conclude;
396d5fd0 761 }
6a3b7c68
RL
762
763 if (set_params)
5f5c3b4f 764 ret = EVP_MD_CTX_set_params(ctx, params);
6a3b7c68 765 else
5f5c3b4f 766 ret = EVP_MD_CTX_get_params(ctx, params);
552be00d 767 goto conclude;
6a3b7c68
RL
768
769
770/* TODO(3.0): Remove legacy code below */
771 legacy:
772 if (ctx->digest->md_ctrl == NULL) {
773 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
774 return 0;
775 }
776
777 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
552be00d 778 conclude:
6a3b7c68
RL
779 if (ret <= 0)
780 return 0;
781 return ret;
396d5fd0 782}
3653d0c2 783
3fd70262
RL
784EVP_MD *evp_md_new(void)
785{
786 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
787
788 if (md != NULL) {
789 md->lock = CRYPTO_THREAD_lock_new();
790 if (md->lock == NULL) {
791 OPENSSL_free(md);
792 return NULL;
793 }
794 md->refcnt = 1;
795 }
796 return md;
797}
798
32040838
RL
799/*
800 * FIPS module note: since internal fetches will be entirely
801 * provider based, we know that none of its code depends on legacy
802 * NIDs or any functionality that use them.
803 */
f844f9eb 804#ifndef FIPS_MODULE
32040838
RL
805/* TODO(3.x) get rid of the need for legacy NIDs */
806static void set_legacy_nid(const char *name, void *vlegacy_nid)
807{
808 int nid;
809 int *legacy_nid = vlegacy_nid;
6a835fcf
RL
810 /*
811 * We use lowest level function to get the associated method, because
812 * higher level functions such as EVP_get_digestbyname() have changed
813 * to look at providers too.
814 */
815 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
32040838
RL
816
817 if (*legacy_nid == -1) /* We found a clash already */
818 return;
6a835fcf
RL
819
820 if (legacy_method == NULL)
32040838 821 return;
6a835fcf 822 nid = EVP_MD_nid(legacy_method);
32040838
RL
823 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
824 *legacy_nid = -1;
825 return;
826 }
827 *legacy_nid = nid;
828}
829#endif
830
38f79314
MC
831static int evp_md_cache_constants(EVP_MD *md)
832{
833 int ok;
834 size_t blksz = 0;
835 size_t mdsize = 0;
836 unsigned long flags = 0;
837 OSSL_PARAM params[4];
838
839 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
840 params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
841 params[2] = OSSL_PARAM_construct_ulong(OSSL_DIGEST_PARAM_FLAGS, &flags);
842 params[3] = OSSL_PARAM_construct_end();
843 ok = evp_do_md_getparams(md, params);
844 if (mdsize > INT_MAX || blksz > INT_MAX)
845 ok = 0;
846 if (ok) {
847 md->block_size = (int)blksz;
848 md->md_size = (int)mdsize;
849 md->flags = flags;
850 }
851 return ok;
852}
853
f7c16d48
RL
854static void *evp_md_from_dispatch(int name_id,
855 const OSSL_DISPATCH *fns,
0ddf74bf 856 OSSL_PROVIDER *prov)
3653d0c2
MC
857{
858 EVP_MD *md = NULL;
8c8cf0d9 859 int fncnt = 0;
3653d0c2 860
0211740f 861 /* EVP_MD_fetch() will set the legacy NID if available */
f7c16d48 862 if ((md = evp_md_new()) == NULL) {
9311d0c4 863 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
3653d0c2 864 return NULL;
6b9e3724 865 }
3653d0c2 866
f844f9eb 867#ifndef FIPS_MODULE
32040838
RL
868 /* TODO(3.x) get rid of the need for legacy NIDs */
869 md->type = NID_undef;
f651c727 870 evp_names_do_all(prov, name_id, set_legacy_nid, &md->type);
32040838
RL
871 if (md->type == -1) {
872 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
873 EVP_MD_free(md);
874 return NULL;
f7c16d48 875 }
ed71e917
MC
876#endif
877
32040838
RL
878 md->name_id = name_id;
879
3653d0c2 880 for (; fns->function_id != 0; fns++) {
3653d0c2
MC
881 switch (fns->function_id) {
882 case OSSL_FUNC_DIGEST_NEWCTX:
d5e5e2ff 883 if (md->newctx == NULL) {
363b1e5d 884 md->newctx = OSSL_FUNC_digest_newctx(fns);
d5e5e2ff
SL
885 fncnt++;
886 }
3653d0c2
MC
887 break;
888 case OSSL_FUNC_DIGEST_INIT:
d5e5e2ff 889 if (md->dinit == NULL) {
363b1e5d 890 md->dinit = OSSL_FUNC_digest_init(fns);
d5e5e2ff
SL
891 fncnt++;
892 }
3653d0c2 893 break;
df05f2ce 894 case OSSL_FUNC_DIGEST_UPDATE:
d5e5e2ff 895 if (md->dupdate == NULL) {
363b1e5d 896 md->dupdate = OSSL_FUNC_digest_update(fns);
d5e5e2ff
SL
897 fncnt++;
898 }
3653d0c2
MC
899 break;
900 case OSSL_FUNC_DIGEST_FINAL:
d5e5e2ff 901 if (md->dfinal == NULL) {
363b1e5d 902 md->dfinal = OSSL_FUNC_digest_final(fns);
d5e5e2ff
SL
903 fncnt++;
904 }
3653d0c2
MC
905 break;
906 case OSSL_FUNC_DIGEST_DIGEST:
d5e5e2ff 907 if (md->digest == NULL)
363b1e5d 908 md->digest = OSSL_FUNC_digest_digest(fns);
3653d0c2
MC
909 /* We don't increment fnct for this as it is stand alone */
910 break;
3653d0c2 911 case OSSL_FUNC_DIGEST_FREECTX:
d5e5e2ff 912 if (md->freectx == NULL) {
363b1e5d 913 md->freectx = OSSL_FUNC_digest_freectx(fns);
d5e5e2ff
SL
914 fncnt++;
915 }
3653d0c2 916 break;
8c8cf0d9 917 case OSSL_FUNC_DIGEST_DUPCTX:
d5e5e2ff 918 if (md->dupctx == NULL)
363b1e5d 919 md->dupctx = OSSL_FUNC_digest_dupctx(fns);
8c8cf0d9 920 break;
d5e5e2ff
SL
921 case OSSL_FUNC_DIGEST_GET_PARAMS:
922 if (md->get_params == NULL)
363b1e5d 923 md->get_params = OSSL_FUNC_digest_get_params(fns);
7556b9df 924 break;
92d9d0ae
RL
925 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
926 if (md->set_ctx_params == NULL)
363b1e5d 927 md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
6a3b7c68 928 break;
92d9d0ae
RL
929 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
930 if (md->get_ctx_params == NULL)
363b1e5d 931 md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
6a3b7c68 932 break;
ae3ff60e
RL
933 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
934 if (md->gettable_params == NULL)
363b1e5d 935 md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
ae3ff60e
RL
936 break;
937 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
938 if (md->settable_ctx_params == NULL)
939 md->settable_ctx_params =
363b1e5d 940 OSSL_FUNC_digest_settable_ctx_params(fns);
ae3ff60e
RL
941 break;
942 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
943 if (md->gettable_ctx_params == NULL)
944 md->gettable_ctx_params =
363b1e5d 945 OSSL_FUNC_digest_gettable_ctx_params(fns);
ae3ff60e 946 break;
3653d0c2 947 }
8c8cf0d9
MC
948 }
949 if ((fncnt != 0 && fncnt != 5)
6a3b7c68 950 || (fncnt == 0 && md->digest == NULL)) {
8c8cf0d9
MC
951 /*
952 * In order to be a consistent set of functions we either need the
953 * whole set of init/update/final etc functions or none of them.
954 * The "digest" function can standalone. We at least need one way to
955 * generate digests.
956 */
3fd70262 957 EVP_MD_free(md);
6a3b7c68 958 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
8c8cf0d9 959 return NULL;
3653d0c2
MC
960 }
961 md->prov = prov;
962 if (prov != NULL)
7c95390e 963 ossl_provider_up_ref(prov);
3653d0c2 964
38f79314
MC
965 if (!evp_md_cache_constants(md)) {
966 EVP_MD_free(md);
967 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
968 md = NULL;
969 }
970
3653d0c2
MC
971 return md;
972}
973
70c35fd1 974static int evp_md_up_ref(void *md)
3653d0c2 975{
70c35fd1 976 return EVP_MD_up_ref(md);
3653d0c2
MC
977}
978
979static void evp_md_free(void *md)
980{
3fd70262 981 EVP_MD_free(md);
3653d0c2
MC
982}
983
b4250010 984EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
3653d0c2
MC
985 const char *properties)
986{
0211740f
RL
987 EVP_MD *md =
988 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
0ddf74bf 989 evp_md_from_dispatch, evp_md_up_ref, evp_md_free);
0211740f 990
0211740f 991 return md;
3653d0c2 992}
c540f00f 993
3fd70262
RL
994int EVP_MD_up_ref(EVP_MD *md)
995{
996 int ref = 0;
997
998 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
999 return 1;
1000}
1001
1002void EVP_MD_free(EVP_MD *md)
1003{
1004 int i;
1005
1006 if (md == NULL)
1007 return;
1008
1009 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
1010 if (i > 0)
1011 return;
1012 ossl_provider_free(md->prov);
3fd70262
RL
1013 CRYPTO_THREAD_lock_free(md->lock);
1014 OPENSSL_free(md);
1015}
1016
b4250010 1017void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
251e610c
RL
1018 void (*fn)(EVP_MD *mac, void *arg),
1019 void *arg)
c540f00f
RL
1020{
1021 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1022 (void (*)(void *, void *))fn, arg,
0ddf74bf 1023 evp_md_from_dispatch, evp_md_free);
c540f00f 1024}