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