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