]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/digest.c
deprecate engines in libcrypto
[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/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_ex() or EVP_DigestVerifyInit_ex(), so we
299 * detect that and redirect to the correct EVP_Digest*Update() function
300 */
301 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
302 return EVP_DigestSignUpdate(ctx, data, count);
303 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
304 return EVP_DigestVerifyUpdate(ctx, data, count);
305 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
306 return 0;
307 }
308
309 if (ctx->digest == NULL
310 || ctx->digest->prov == NULL
311 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
312 goto legacy;
313
314 if (ctx->digest->dupdate == NULL) {
315 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
316 return 0;
317 }
318 return ctx->digest->dupdate(ctx->provctx, data, count);
319
320 /* TODO(3.0): Remove legacy code below */
321 legacy:
322 return ctx->update(ctx, data, count);
323 }
324
325 /* The caller can assume that this removes any secret data from the context */
326 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
327 {
328 int ret;
329 ret = EVP_DigestFinal_ex(ctx, md, size);
330 EVP_MD_CTX_reset(ctx);
331 return ret;
332 }
333
334 /* The caller can assume that this removes any secret data from the context */
335 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
336 {
337 int ret;
338 size_t size = 0;
339 size_t mdsize = EVP_MD_size(ctx->digest);
340
341 if (ctx->digest == NULL || ctx->digest->prov == NULL)
342 goto legacy;
343
344 if (ctx->digest->dfinal == NULL) {
345 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
346 return 0;
347 }
348
349 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
350
351 if (isize != NULL) {
352 if (size <= UINT_MAX) {
353 *isize = (int)size;
354 } else {
355 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
356 ret = 0;
357 }
358 }
359
360 return ret;
361
362 /* TODO(3.0): Remove legacy code below */
363 legacy:
364 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
365 ret = ctx->digest->final(ctx, md);
366 if (isize != NULL)
367 *isize = mdsize;
368 if (ctx->digest->cleanup) {
369 ctx->digest->cleanup(ctx);
370 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
371 }
372 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
373 return ret;
374 }
375
376 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
377 {
378 int ret = 0;
379 OSSL_PARAM params[2];
380 size_t i = 0;
381
382 if (ctx->digest == NULL || ctx->digest->prov == NULL)
383 goto legacy;
384
385 if (ctx->digest->dfinal == NULL) {
386 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
387 return 0;
388 }
389
390 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
391 params[i++] = OSSL_PARAM_construct_end();
392
393 if (EVP_MD_CTX_set_params(ctx, params) > 0)
394 ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
395 EVP_MD_CTX_reset(ctx);
396 return ret;
397
398 legacy:
399 if (ctx->digest->flags & EVP_MD_FLAG_XOF
400 && size <= INT_MAX
401 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
402 ret = ctx->digest->final(ctx, md);
403 if (ctx->digest->cleanup != NULL) {
404 ctx->digest->cleanup(ctx);
405 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
406 }
407 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
408 } else {
409 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
410 }
411
412 return ret;
413 }
414
415 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
416 {
417 EVP_MD_CTX_reset(out);
418 return EVP_MD_CTX_copy_ex(out, in);
419 }
420
421 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
422 {
423 unsigned char *tmp_buf;
424
425 if (in == NULL || in->digest == NULL) {
426 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
427 return 0;
428 }
429
430 if (in->digest->prov == NULL
431 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
432 goto legacy;
433
434 if (in->digest->dupctx == NULL) {
435 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
436 return 0;
437 }
438
439 EVP_MD_CTX_reset(out);
440 if (out->fetched_digest != NULL)
441 EVP_MD_free(out->fetched_digest);
442 *out = *in;
443 /* NULL out pointers in case of error */
444 out->pctx = NULL;
445 out->provctx = NULL;
446
447 if (in->fetched_digest != NULL)
448 EVP_MD_up_ref(in->fetched_digest);
449
450 out->provctx = in->digest->dupctx(in->provctx);
451 if (out->provctx == NULL) {
452 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
453 return 0;
454 }
455
456 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
457 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
458 #ifndef FIPS_MODULE
459 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
460 if (in->pctx != NULL) {
461 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
462 if (out->pctx == NULL) {
463 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
464 EVP_MD_CTX_reset(out);
465 return 0;
466 }
467 }
468 #endif
469
470 return 1;
471
472 /* TODO(3.0): Remove legacy code below */
473 legacy:
474 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
475 /* Make sure it's safe to copy a digest context using an ENGINE */
476 if (in->engine && !ENGINE_init(in->engine)) {
477 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
478 return 0;
479 }
480 #endif
481
482 if (out->digest == in->digest) {
483 tmp_buf = out->md_data;
484 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
485 } else
486 tmp_buf = NULL;
487 EVP_MD_CTX_reset(out);
488 memcpy(out, in, sizeof(*out));
489
490 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
491 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
492
493 /* Null these variables, since they are getting fixed up
494 * properly below. Anything else may cause a memleak and/or
495 * double free if any of the memory allocations below fail
496 */
497 out->md_data = NULL;
498 out->pctx = NULL;
499
500 if (in->md_data && out->digest->ctx_size) {
501 if (tmp_buf)
502 out->md_data = tmp_buf;
503 else {
504 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
505 if (out->md_data == NULL) {
506 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
507 return 0;
508 }
509 }
510 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
511 }
512
513 out->update = in->update;
514
515 #ifndef FIPS_MODULE
516 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
517 if (in->pctx) {
518 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
519 if (!out->pctx) {
520 EVP_MD_CTX_reset(out);
521 return 0;
522 }
523 }
524 #endif
525
526 if (out->digest->copy)
527 return out->digest->copy(out, in);
528
529 return 1;
530 }
531
532 int EVP_Digest(const void *data, size_t count,
533 unsigned char *md, unsigned int *size, const EVP_MD *type,
534 ENGINE *impl)
535 {
536 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
537 int ret;
538
539 if (ctx == NULL)
540 return 0;
541 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
542 ret = EVP_DigestInit_ex(ctx, type, impl)
543 && EVP_DigestUpdate(ctx, data, count)
544 && EVP_DigestFinal_ex(ctx, md, size);
545 EVP_MD_CTX_free(ctx);
546
547 return ret;
548 }
549
550 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
551 {
552 if (digest != NULL && digest->get_params != NULL)
553 return digest->get_params(params);
554 return 0;
555 }
556
557 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
558 {
559 if (digest != NULL && digest->gettable_params != NULL)
560 return digest->gettable_params();
561 return NULL;
562 }
563
564 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
565 {
566 EVP_PKEY_CTX *pctx = ctx->pctx;
567
568 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
569 return ctx->digest->set_ctx_params(ctx->provctx, params);
570
571 if (pctx != NULL
572 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
573 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
574 && pctx->op.sig.sigprovctx != NULL
575 && pctx->op.sig.signature->set_ctx_md_params != NULL)
576 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.sigprovctx,
577 params);
578 return 0;
579 }
580
581 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
582 {
583 if (md != NULL && md->settable_ctx_params != NULL)
584 return md->settable_ctx_params();
585 return NULL;
586 }
587
588 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
589 {
590 EVP_PKEY_CTX *pctx;
591
592 if (ctx != NULL
593 && ctx->digest != NULL
594 && ctx->digest->settable_ctx_params != NULL)
595 return ctx->digest->settable_ctx_params();
596
597 pctx = ctx->pctx;
598 if (pctx != NULL
599 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
600 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
601 && pctx->op.sig.sigprovctx != NULL
602 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
603 return pctx->op.sig.signature->settable_ctx_md_params(
604 pctx->op.sig.sigprovctx);
605
606 return NULL;
607 }
608
609 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
610 {
611 EVP_PKEY_CTX *pctx = ctx->pctx;
612
613 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
614 return ctx->digest->get_ctx_params(ctx->provctx, params);
615
616 if (pctx != NULL
617 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
618 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
619 && pctx->op.sig.sigprovctx != NULL
620 && pctx->op.sig.signature->get_ctx_md_params != NULL)
621 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.sigprovctx,
622 params);
623
624 return 0;
625 }
626
627 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
628 {
629 if (md != NULL && md->gettable_ctx_params != NULL)
630 return md->gettable_ctx_params();
631 return NULL;
632 }
633
634 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
635 {
636 EVP_PKEY_CTX *pctx;
637
638 if (ctx != NULL
639 && ctx->digest != NULL
640 && ctx->digest->gettable_ctx_params != NULL)
641 return ctx->digest->gettable_ctx_params();
642
643 pctx = ctx->pctx;
644 if (pctx != NULL
645 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
646 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
647 && pctx->op.sig.sigprovctx != NULL
648 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
649 return pctx->op.sig.signature->gettable_ctx_md_params(
650 pctx->op.sig.sigprovctx);
651
652 return NULL;
653 }
654
655 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
656 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
657 {
658 int ret = EVP_CTRL_RET_UNSUPPORTED;
659 int set_params = 1;
660 size_t sz;
661 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
662
663 if (ctx == NULL) {
664 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
665 return 0;
666 }
667
668 if (ctx->digest != NULL && ctx->digest->prov == NULL)
669 goto legacy;
670
671 switch (cmd) {
672 case EVP_MD_CTRL_XOF_LEN:
673 sz = (size_t)p1;
674 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
675 break;
676 case EVP_MD_CTRL_MICALG:
677 set_params = 0;
678 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
679 p2, p1 ? p1 : 9999);
680 break;
681 case EVP_CTRL_SSL3_MASTER_SECRET:
682 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
683 p2, p1);
684 break;
685 default:
686 goto conclude;
687 }
688
689 if (set_params)
690 ret = EVP_MD_CTX_set_params(ctx, params);
691 else
692 ret = EVP_MD_CTX_get_params(ctx, params);
693 goto conclude;
694
695
696 /* TODO(3.0): Remove legacy code below */
697 legacy:
698 if (ctx->digest->md_ctrl == NULL) {
699 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
700 return 0;
701 }
702
703 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
704 conclude:
705 if (ret <= 0)
706 return 0;
707 return ret;
708 }
709
710 EVP_MD *evp_md_new(void)
711 {
712 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
713
714 if (md != NULL) {
715 md->lock = CRYPTO_THREAD_lock_new();
716 if (md->lock == NULL) {
717 OPENSSL_free(md);
718 return NULL;
719 }
720 md->refcnt = 1;
721 }
722 return md;
723 }
724
725 /*
726 * FIPS module note: since internal fetches will be entirely
727 * provider based, we know that none of its code depends on legacy
728 * NIDs or any functionality that use them.
729 */
730 #ifndef FIPS_MODULE
731 /* TODO(3.x) get rid of the need for legacy NIDs */
732 static void set_legacy_nid(const char *name, void *vlegacy_nid)
733 {
734 int nid;
735 int *legacy_nid = vlegacy_nid;
736 /*
737 * We use lowest level function to get the associated method, because
738 * higher level functions such as EVP_get_digestbyname() have changed
739 * to look at providers too.
740 */
741 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
742
743 if (*legacy_nid == -1) /* We found a clash already */
744 return;
745
746 if (legacy_method == NULL)
747 return;
748 nid = EVP_MD_nid(legacy_method);
749 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
750 *legacy_nid = -1;
751 return;
752 }
753 *legacy_nid = nid;
754 }
755 #endif
756
757 static void *evp_md_from_dispatch(int name_id,
758 const OSSL_DISPATCH *fns,
759 OSSL_PROVIDER *prov)
760 {
761 EVP_MD *md = NULL;
762 int fncnt = 0;
763
764 /* EVP_MD_fetch() will set the legacy NID if available */
765 if ((md = evp_md_new()) == NULL) {
766 EVPerr(0, ERR_R_MALLOC_FAILURE);
767 return NULL;
768 }
769
770 #ifndef FIPS_MODULE
771 /* TODO(3.x) get rid of the need for legacy NIDs */
772 md->type = NID_undef;
773 evp_names_do_all(prov, name_id, set_legacy_nid, &md->type);
774 if (md->type == -1) {
775 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
776 EVP_MD_free(md);
777 return NULL;
778 }
779 #endif
780
781 md->name_id = name_id;
782
783 for (; fns->function_id != 0; fns++) {
784 switch (fns->function_id) {
785 case OSSL_FUNC_DIGEST_NEWCTX:
786 if (md->newctx == NULL) {
787 md->newctx = OSSL_FUNC_digest_newctx(fns);
788 fncnt++;
789 }
790 break;
791 case OSSL_FUNC_DIGEST_INIT:
792 if (md->dinit == NULL) {
793 md->dinit = OSSL_FUNC_digest_init(fns);
794 fncnt++;
795 }
796 break;
797 case OSSL_FUNC_DIGEST_UPDATE:
798 if (md->dupdate == NULL) {
799 md->dupdate = OSSL_FUNC_digest_update(fns);
800 fncnt++;
801 }
802 break;
803 case OSSL_FUNC_DIGEST_FINAL:
804 if (md->dfinal == NULL) {
805 md->dfinal = OSSL_FUNC_digest_final(fns);
806 fncnt++;
807 }
808 break;
809 case OSSL_FUNC_DIGEST_DIGEST:
810 if (md->digest == NULL)
811 md->digest = OSSL_FUNC_digest_digest(fns);
812 /* We don't increment fnct for this as it is stand alone */
813 break;
814 case OSSL_FUNC_DIGEST_FREECTX:
815 if (md->freectx == NULL) {
816 md->freectx = OSSL_FUNC_digest_freectx(fns);
817 fncnt++;
818 }
819 break;
820 case OSSL_FUNC_DIGEST_DUPCTX:
821 if (md->dupctx == NULL)
822 md->dupctx = OSSL_FUNC_digest_dupctx(fns);
823 break;
824 case OSSL_FUNC_DIGEST_GET_PARAMS:
825 if (md->get_params == NULL)
826 md->get_params = OSSL_FUNC_digest_get_params(fns);
827 break;
828 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
829 if (md->set_ctx_params == NULL)
830 md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
831 break;
832 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
833 if (md->get_ctx_params == NULL)
834 md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
835 break;
836 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
837 if (md->gettable_params == NULL)
838 md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
839 break;
840 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
841 if (md->settable_ctx_params == NULL)
842 md->settable_ctx_params =
843 OSSL_FUNC_digest_settable_ctx_params(fns);
844 break;
845 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
846 if (md->gettable_ctx_params == NULL)
847 md->gettable_ctx_params =
848 OSSL_FUNC_digest_gettable_ctx_params(fns);
849 break;
850 }
851 }
852 if ((fncnt != 0 && fncnt != 5)
853 || (fncnt == 0 && md->digest == NULL)) {
854 /*
855 * In order to be a consistent set of functions we either need the
856 * whole set of init/update/final etc functions or none of them.
857 * The "digest" function can standalone. We at least need one way to
858 * generate digests.
859 */
860 EVP_MD_free(md);
861 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
862 return NULL;
863 }
864 md->prov = prov;
865 if (prov != NULL)
866 ossl_provider_up_ref(prov);
867
868 return md;
869 }
870
871 static int evp_md_up_ref(void *md)
872 {
873 return EVP_MD_up_ref(md);
874 }
875
876 static void evp_md_free(void *md)
877 {
878 EVP_MD_free(md);
879 }
880
881 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
882 const char *properties)
883 {
884 EVP_MD *md =
885 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
886 evp_md_from_dispatch, evp_md_up_ref, evp_md_free);
887
888 return md;
889 }
890
891 int EVP_MD_up_ref(EVP_MD *md)
892 {
893 int ref = 0;
894
895 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
896 return 1;
897 }
898
899 void EVP_MD_free(EVP_MD *md)
900 {
901 int i;
902
903 if (md == NULL)
904 return;
905
906 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
907 if (i > 0)
908 return;
909 ossl_provider_free(md->prov);
910 CRYPTO_THREAD_lock_free(md->lock);
911 OPENSSL_free(md);
912 }
913
914 void EVP_MD_do_all_provided(OPENSSL_CTX *libctx,
915 void (*fn)(EVP_MD *mac, void *arg),
916 void *arg)
917 {
918 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
919 (void (*)(void *, void *))fn, arg,
920 evp_md_from_dispatch, evp_md_free);
921 }