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