Ensure we look at EVP_MD_CTX_FLAG_KEEP_PKEY_CTX in non-legacy code
[openssl.git] / crypto / evp / digest.c
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <openssl/objects.h>
12 #include <openssl/evp.h>
13 #include <openssl/engine.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include "internal/cryptlib.h"
17 #include "crypto/evp.h"
18 #include "internal/provider.h"
19 #include "evp_local.h"
20
21 /* This call frees resources associated with the context */
22 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
23 {
24     if (ctx == NULL)
25         return 1;
26
27 #ifndef FIPS_MODE
28     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
29     /*
30      * pctx should be freed by the user of EVP_MD_CTX
31      * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
32      */
33     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
34         EVP_PKEY_CTX_free(ctx->pctx);
35 #endif
36
37     if (ctx->digest == NULL || ctx->digest->prov == NULL)
38         goto legacy;
39
40     if (ctx->provctx != NULL) {
41         if (ctx->digest->freectx != NULL)
42             ctx->digest->freectx(ctx->provctx);
43         ctx->provctx = NULL;
44         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
45     }
46
47     if (ctx->pctx != NULL)
48         goto legacy;
49
50     return 1;
51
52     /* TODO(3.0): Remove legacy code below */
53  legacy:
54
55     /*
56      * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
57      * sometimes only copies of the context are ever finalised.
58      */
59     if (ctx->digest && ctx->digest->cleanup
60         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
61         ctx->digest->cleanup(ctx);
62     if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
63         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
64         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
65     }
66
67 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
68     ENGINE_finish(ctx->engine);
69 #endif
70     OPENSSL_cleanse(ctx, sizeof(*ctx));
71
72     return 1;
73 }
74
75 EVP_MD_CTX *EVP_MD_CTX_new(void)
76 {
77     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
78 }
79
80 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
81 {
82     if (ctx == NULL)
83         return;
84
85     EVP_MD_CTX_reset(ctx);
86
87     EVP_MD_free(ctx->fetched_digest);
88     ctx->fetched_digest = NULL;
89     ctx->digest = NULL;
90     ctx->reqdigest = NULL;
91
92     OPENSSL_free(ctx);
93     return;
94 }
95
96 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
97 {
98     EVP_MD_CTX_reset(ctx);
99     return EVP_DigestInit_ex(ctx, type, NULL);
100 }
101
102 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
103 {
104 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
105     ENGINE *tmpimpl = NULL;
106 #endif
107
108     EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
109
110     if (type != NULL)
111         ctx->reqdigest = type;
112
113     /* TODO(3.0): Legacy work around code below. Remove this */
114 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
115     /*
116      * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
117      * this context may already have an ENGINE! Try to avoid releasing the
118      * previous handle, re-querying for an ENGINE, and having a
119      * reinitialisation, when it may all be unnecessary.
120      */
121     if (ctx->engine && ctx->digest &&
122         (type == NULL || (type->type == ctx->digest->type)))
123         goto skip_to_init;
124
125     if (type != NULL) {
126         /*
127          * Ensure an ENGINE left lying around from last time is cleared (the
128          * previous check attempted to avoid this if the same ENGINE and
129          * EVP_MD could be used).
130          */
131         ENGINE_finish(ctx->engine);
132         ctx->engine = NULL;
133     }
134
135     if (type != NULL && impl == NULL)
136         tmpimpl = ENGINE_get_digest_engine(type->type);
137 #endif
138
139     /*
140      * If there are engines involved or if we're being used as part of
141      * EVP_DigestSignInit then we should use legacy handling for now.
142      */
143     if (ctx->engine != NULL
144             || impl != NULL
145 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
146             || tmpimpl != NULL
147 #endif
148             || ctx->pctx != NULL
149             || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
150         if (ctx->digest == ctx->fetched_digest)
151             ctx->digest = NULL;
152         EVP_MD_free(ctx->fetched_digest);
153         ctx->fetched_digest = NULL;
154         goto legacy;
155     }
156
157     if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
158         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
159         ctx->md_data = NULL;
160     }
161
162     /* TODO(3.0): Start of non-legacy code below */
163
164     if (type->prov == NULL) {
165 #ifdef FIPS_MODE
166         /* We only do explict fetches inside the FIPS module */
167         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
168         return 0;
169 #else
170         EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
171
172         if (provmd == NULL) {
173             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
174             return 0;
175         }
176         type = provmd;
177         EVP_MD_free(ctx->fetched_digest);
178         ctx->fetched_digest = provmd;
179 #endif
180     }
181
182     if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
183         if (ctx->digest->freectx != NULL)
184             ctx->digest->freectx(ctx->provctx);
185         ctx->provctx = NULL;
186     }
187     ctx->digest = type;
188     if (ctx->provctx == NULL) {
189         ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
190         if (ctx->provctx == NULL) {
191             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
192             return 0;
193         }
194     }
195
196     if (ctx->digest->dinit == NULL) {
197         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
198         return 0;
199     }
200
201     return ctx->digest->dinit(ctx->provctx);
202
203     /* TODO(3.0): Remove legacy code below */
204  legacy:
205
206 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
207     if (type) {
208         if (impl != NULL) {
209             if (!ENGINE_init(impl)) {
210                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
211                 return 0;
212             }
213         } else {
214             /* Ask if an ENGINE is reserved for this job */
215             impl = tmpimpl;
216         }
217         if (impl != NULL) {
218             /* There's an ENGINE for this job ... (apparently) */
219             const EVP_MD *d = ENGINE_get_digest(impl, type->type);
220
221             if (d == NULL) {
222                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
223                 ENGINE_finish(impl);
224                 return 0;
225             }
226             /* We'll use the ENGINE's private digest definition */
227             type = d;
228             /*
229              * Store the ENGINE functional reference so we know 'type' came
230              * from an ENGINE and we need to release it when done.
231              */
232             ctx->engine = impl;
233         } else
234             ctx->engine = NULL;
235     } else {
236         if (!ctx->digest) {
237             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
238             return 0;
239         }
240         type = ctx->digest;
241     }
242 #endif
243     if (ctx->digest != type) {
244         if (ctx->digest && ctx->digest->ctx_size) {
245             OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
246             ctx->md_data = NULL;
247         }
248         ctx->digest = type;
249         if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
250             ctx->update = type->update;
251             ctx->md_data = OPENSSL_zalloc(type->ctx_size);
252             if (ctx->md_data == NULL) {
253                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
254                 return 0;
255             }
256         }
257     }
258 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
259  skip_to_init:
260 #endif
261 #ifndef FIPS_MODE
262     /*
263      * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
264      * or when using providers.
265      */
266     if (ctx->pctx != NULL
267             && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
268                  || ctx->pctx->op.sig.signature == NULL)) {
269         int r;
270         r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
271                               EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
272         if (r <= 0 && (r != -2))
273             return 0;
274     }
275 #endif
276     if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
277         return 1;
278     return ctx->digest->init(ctx);
279 }
280
281 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
282 {
283     if (count == 0)
284         return 1;
285
286     if (ctx->digest == NULL || ctx->digest->prov == NULL)
287         goto legacy;
288
289     if (ctx->digest->dupdate == NULL) {
290         EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
291         return 0;
292     }
293     return ctx->digest->dupdate(ctx->provctx, data, count);
294
295     /* TODO(3.0): Remove legacy code below */
296  legacy:
297     return ctx->update(ctx, data, count);
298 }
299
300 /* The caller can assume that this removes any secret data from the context */
301 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
302 {
303     int ret;
304     ret = EVP_DigestFinal_ex(ctx, md, size);
305     EVP_MD_CTX_reset(ctx);
306     return ret;
307 }
308
309 /* The caller can assume that this removes any secret data from the context */
310 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
311 {
312     int ret;
313     size_t size = 0;
314     size_t mdsize = EVP_MD_size(ctx->digest);
315
316     if (ctx->digest == NULL || ctx->digest->prov == NULL)
317         goto legacy;
318
319     if (ctx->digest->dfinal == NULL) {
320         EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
321         return 0;
322     }
323
324     ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
325
326     if (isize != NULL) {
327         if (size <= UINT_MAX) {
328             *isize = (int)size;
329         } else {
330             EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
331             ret = 0;
332         }
333     }
334
335     EVP_MD_CTX_reset(ctx);
336     return ret;
337
338     /* TODO(3.0): Remove legacy code below */
339  legacy:
340     OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
341     ret = ctx->digest->final(ctx, md);
342     if (isize != NULL)
343         *isize = mdsize;
344     if (ctx->digest->cleanup) {
345         ctx->digest->cleanup(ctx);
346         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
347     }
348     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
349     return ret;
350 }
351
352 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
353 {
354     int ret = 0;
355     OSSL_PARAM params[2];
356     size_t i = 0;
357
358     if (ctx->digest == NULL || ctx->digest->prov == NULL)
359         goto legacy;
360
361     if (ctx->digest->dfinal == NULL) {
362         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
363         return 0;
364     }
365
366     params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
367     params[i++] = OSSL_PARAM_construct_end();
368
369     if (EVP_MD_CTX_set_params(ctx, params) > 0)
370         ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
371     EVP_MD_CTX_reset(ctx);
372     return ret;
373
374 legacy:
375     if (ctx->digest->flags & EVP_MD_FLAG_XOF
376         && size <= INT_MAX
377         && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
378         ret = ctx->digest->final(ctx, md);
379         if (ctx->digest->cleanup != NULL) {
380             ctx->digest->cleanup(ctx);
381             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
382         }
383         OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
384     } else {
385         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
386     }
387
388     return ret;
389 }
390
391 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
392 {
393     EVP_MD_CTX_reset(out);
394     return EVP_MD_CTX_copy_ex(out, in);
395 }
396
397 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
398 {
399     unsigned char *tmp_buf;
400
401     if (in == NULL || in->digest == NULL) {
402         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
403         return 0;
404     }
405
406     if (in->digest->prov == NULL)
407         goto legacy;
408
409     if (in->digest->dupctx == NULL) {
410         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
411         return 0;
412     }
413
414     EVP_MD_CTX_reset(out);
415     if (out->fetched_digest != NULL)
416         EVP_MD_free(out->fetched_digest);
417     *out = *in;
418     /* NULL out pointers in case of error */
419     out->pctx = NULL;
420     out->provctx = NULL;
421
422     if (in->fetched_digest != NULL)
423         EVP_MD_up_ref(in->fetched_digest);
424
425     out->provctx = in->digest->dupctx(in->provctx);
426     if (out->provctx == NULL) {
427         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
428         return 0;
429     }
430
431     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
432     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
433 #ifndef FIPS_MODE
434     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
435     if (in->pctx != NULL) {
436         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
437         if (out->pctx == NULL) {
438             EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
439             EVP_MD_CTX_reset(out);
440             return 0;
441         }
442     }
443 #endif
444
445     return 1;
446
447     /* TODO(3.0): Remove legacy code below */
448  legacy:
449 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
450     /* Make sure it's safe to copy a digest context using an ENGINE */
451     if (in->engine && !ENGINE_init(in->engine)) {
452         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
453         return 0;
454     }
455 #endif
456
457     if (out->digest == in->digest) {
458         tmp_buf = out->md_data;
459         EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
460     } else
461         tmp_buf = NULL;
462     EVP_MD_CTX_reset(out);
463     memcpy(out, in, sizeof(*out));
464
465     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
466     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
467
468     /* Null these variables, since they are getting fixed up
469      * properly below.  Anything else may cause a memleak and/or
470      * double free if any of the memory allocations below fail
471      */
472     out->md_data = NULL;
473     out->pctx = NULL;
474
475     if (in->md_data && out->digest->ctx_size) {
476         if (tmp_buf)
477             out->md_data = tmp_buf;
478         else {
479             out->md_data = OPENSSL_malloc(out->digest->ctx_size);
480             if (out->md_data == NULL) {
481                 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
482                 return 0;
483             }
484         }
485         memcpy(out->md_data, in->md_data, out->digest->ctx_size);
486     }
487
488     out->update = in->update;
489
490 #ifndef FIPS_MODE
491     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
492     if (in->pctx) {
493         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
494         if (!out->pctx) {
495             EVP_MD_CTX_reset(out);
496             return 0;
497         }
498     }
499 #endif
500
501     if (out->digest->copy)
502         return out->digest->copy(out, in);
503
504     return 1;
505 }
506
507 int EVP_Digest(const void *data, size_t count,
508                unsigned char *md, unsigned int *size, const EVP_MD *type,
509                ENGINE *impl)
510 {
511     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
512     int ret;
513
514     if (ctx == NULL)
515         return 0;
516     EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
517     ret = EVP_DigestInit_ex(ctx, type, impl)
518         && EVP_DigestUpdate(ctx, data, count)
519         && EVP_DigestFinal_ex(ctx, md, size);
520     EVP_MD_CTX_free(ctx);
521
522     return ret;
523 }
524
525 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
526 {
527     if (digest != NULL && digest->get_params != NULL)
528         return digest->get_params(params);
529     return 0;
530 }
531
532 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
533 {
534     if (digest != NULL && digest->gettable_params != NULL)
535         return digest->gettable_params();
536     return NULL;
537 }
538
539 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
540 {
541     if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
542         return ctx->digest->set_ctx_params(ctx->provctx, params);
543     return 0;
544 }
545
546 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
547 {
548     if (md != NULL && md->settable_ctx_params != NULL)
549         return md->settable_ctx_params();
550     return NULL;
551 }
552
553 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
554 {
555     if (ctx != NULL
556             && ctx->digest != NULL
557             && ctx->digest->settable_ctx_params != NULL)
558         return ctx->digest->settable_ctx_params();
559
560     return NULL;
561 }
562
563 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
564 {
565     if (ctx->digest != NULL && ctx->digest->get_params != NULL)
566         return ctx->digest->get_ctx_params(ctx->provctx, params);
567     return 0;
568 }
569
570 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
571 {
572     if (md != NULL && md->gettable_ctx_params != NULL)
573         return md->gettable_ctx_params();
574     return NULL;
575 }
576
577 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
578 {
579     if (ctx != NULL
580             && ctx->digest != NULL
581             && ctx->digest->gettable_ctx_params != NULL)
582         return ctx->digest->gettable_ctx_params();
583
584     return NULL;
585 }
586
587 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
588 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
589 {
590     int ret = EVP_CTRL_RET_UNSUPPORTED;
591     int set_params = 1;
592     size_t sz;
593     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
594
595     if (ctx == NULL || ctx->digest == NULL) {
596         ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
597         return 0;
598     }
599
600     if (ctx->digest->prov == NULL)
601         goto legacy;
602
603     switch (cmd) {
604     case EVP_MD_CTRL_XOF_LEN:
605         sz = (size_t)p1;
606         params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
607         break;
608     case EVP_MD_CTRL_MICALG:
609         set_params = 0;
610         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
611                                                      p2, p1 ? p1 : 9999);
612         break;
613     default:
614         return EVP_CTRL_RET_UNSUPPORTED;
615     }
616
617     if (set_params)
618         ret = evp_do_md_ctx_setparams(ctx->digest, ctx->provctx, params);
619     else
620         ret = evp_do_md_ctx_getparams(ctx->digest, ctx->provctx, params);
621     return ret;
622
623
624 /* TODO(3.0): Remove legacy code below */
625  legacy:
626     if (ctx->digest->md_ctrl == NULL) {
627         ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
628         return 0;
629     }
630
631     ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
632     if (ret <= 0)
633         return 0;
634     return ret;
635 }
636
637 EVP_MD *evp_md_new(void)
638 {
639     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
640
641     if (md != NULL) {
642         md->lock = CRYPTO_THREAD_lock_new();
643         if (md->lock == NULL) {
644             OPENSSL_free(md);
645             return NULL;
646         }
647         md->refcnt = 1;
648     }
649     return md;
650 }
651
652 /*
653  * FIPS module note: since internal fetches will be entirely
654  * provider based, we know that none of its code depends on legacy
655  * NIDs or any functionality that use them.
656  */
657 #ifndef FIPS_MODE
658 /* TODO(3.x) get rid of the need for legacy NIDs */
659 static void set_legacy_nid(const char *name, void *vlegacy_nid)
660 {
661     int nid;
662     int *legacy_nid = vlegacy_nid;
663
664     if (*legacy_nid == -1)       /* We found a clash already */
665         return;
666     if ((nid = OBJ_sn2nid(name)) == NID_undef
667         && (nid = OBJ_ln2nid(name)) == NID_undef)
668         return;
669     if (*legacy_nid != NID_undef && *legacy_nid != nid) {
670         *legacy_nid = -1;
671         return;
672     }
673     *legacy_nid = nid;
674 }
675 #endif
676
677 static void *evp_md_from_dispatch(int name_id,
678                                   const OSSL_DISPATCH *fns,
679                                   OSSL_PROVIDER *prov, void *unused)
680 {
681     EVP_MD *md = NULL;
682     int fncnt = 0;
683
684     /* EVP_MD_fetch() will set the legacy NID if available */
685     if ((md = evp_md_new()) == NULL) {
686         EVPerr(0, ERR_R_MALLOC_FAILURE);
687         return NULL;
688     }
689
690 #ifndef FIPS_MODE
691     /* TODO(3.x) get rid of the need for legacy NIDs */
692     md->type = NID_undef;
693     evp_doall_names(prov, name_id, set_legacy_nid, &md->type);
694     if (md->type == -1) {
695         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
696         EVP_MD_free(md);
697         return NULL;
698     }
699 #endif
700
701     md->name_id = name_id;
702
703     for (; fns->function_id != 0; fns++) {
704         switch (fns->function_id) {
705         case OSSL_FUNC_DIGEST_NEWCTX:
706             if (md->newctx == NULL) {
707                 md->newctx = OSSL_get_OP_digest_newctx(fns);
708                 fncnt++;
709             }
710             break;
711         case OSSL_FUNC_DIGEST_INIT:
712             if (md->dinit == NULL) {
713                 md->dinit = OSSL_get_OP_digest_init(fns);
714                 fncnt++;
715             }
716             break;
717         case OSSL_FUNC_DIGEST_UPDATE:
718             if (md->dupdate == NULL) {
719                 md->dupdate = OSSL_get_OP_digest_update(fns);
720                 fncnt++;
721             }
722             break;
723         case OSSL_FUNC_DIGEST_FINAL:
724             if (md->dfinal == NULL) {
725                 md->dfinal = OSSL_get_OP_digest_final(fns);
726                 fncnt++;
727             }
728             break;
729         case OSSL_FUNC_DIGEST_DIGEST:
730             if (md->digest == NULL)
731                 md->digest = OSSL_get_OP_digest_digest(fns);
732             /* We don't increment fnct for this as it is stand alone */
733             break;
734         case OSSL_FUNC_DIGEST_FREECTX:
735             if (md->freectx == NULL) {
736                 md->freectx = OSSL_get_OP_digest_freectx(fns);
737                 fncnt++;
738             }
739             break;
740         case OSSL_FUNC_DIGEST_DUPCTX:
741             if (md->dupctx == NULL)
742                 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
743             break;
744         case OSSL_FUNC_DIGEST_GET_PARAMS:
745             if (md->get_params == NULL)
746                 md->get_params = OSSL_get_OP_digest_get_params(fns);
747             break;
748         case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
749             if (md->set_ctx_params == NULL)
750                 md->set_ctx_params = OSSL_get_OP_digest_set_ctx_params(fns);
751             break;
752         case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
753             if (md->get_ctx_params == NULL)
754                 md->get_ctx_params = OSSL_get_OP_digest_get_ctx_params(fns);
755             break;
756         case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
757             if (md->gettable_params == NULL)
758                 md->gettable_params = OSSL_get_OP_digest_gettable_params(fns);
759             break;
760         case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
761             if (md->settable_ctx_params == NULL)
762                 md->settable_ctx_params =
763                     OSSL_get_OP_digest_settable_ctx_params(fns);
764             break;
765         case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
766             if (md->gettable_ctx_params == NULL)
767                 md->gettable_ctx_params =
768                     OSSL_get_OP_digest_gettable_ctx_params(fns);
769             break;
770         }
771     }
772     if ((fncnt != 0 && fncnt != 5)
773         || (fncnt == 0 && md->digest == NULL)) {
774         /*
775          * In order to be a consistent set of functions we either need the
776          * whole set of init/update/final etc functions or none of them.
777          * The "digest" function can standalone. We at least need one way to
778          * generate digests.
779          */
780         EVP_MD_free(md);
781         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
782         return NULL;
783     }
784     md->prov = prov;
785     if (prov != NULL)
786         ossl_provider_up_ref(prov);
787
788     return md;
789 }
790
791 static int evp_md_up_ref(void *md)
792 {
793     return EVP_MD_up_ref(md);
794 }
795
796 static void evp_md_free(void *md)
797 {
798     EVP_MD_free(md);
799 }
800
801 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
802                      const char *properties)
803 {
804     EVP_MD *md =
805         evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
806                           evp_md_from_dispatch, NULL, evp_md_up_ref,
807                           evp_md_free);
808
809     return md;
810 }
811
812 int EVP_MD_up_ref(EVP_MD *md)
813 {
814     int ref = 0;
815
816     CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
817     return 1;
818 }
819
820 void EVP_MD_free(EVP_MD *md)
821 {
822     int i;
823
824     if (md == NULL)
825         return;
826
827     CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
828     if (i > 0)
829         return;
830     ossl_provider_free(md->prov);
831     CRYPTO_THREAD_lock_free(md->lock);
832     OPENSSL_free(md);
833 }
834
835 void EVP_MD_do_all_ex(OPENSSL_CTX *libctx,
836                           void (*fn)(EVP_MD *mac, void *arg),
837                           void *arg)
838 {
839     evp_generic_do_all(libctx, OSSL_OP_DIGEST,
840                        (void (*)(void *, void *))fn, arg,
841                        evp_md_from_dispatch, NULL, evp_md_free);
842 }