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