Use the right NID when putting a method in the store
[oweals/openssl.git] / crypto / evp / digest.c
1 /*
2  * Copyright 1995-2018 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 "internal/cryptlib.h"
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/engine.h>
15 #include "internal/evp_int.h"
16 #include "internal/provider.h"
17 #include "evp_locl.h"
18
19 /* This call frees resources associated with the context */
20 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
21 {
22     if (ctx == NULL)
23         return 1;
24
25     if (ctx->digest == NULL || ctx->digest->prov == NULL)
26         goto legacy;
27
28     if (ctx->provctx != NULL) {
29         if (ctx->digest->freectx != NULL)
30             ctx->digest->freectx(ctx->provctx);
31         ctx->provctx = NULL;
32         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
33     }
34
35     if (ctx->pctx != NULL)
36         goto legacy;
37
38     return 1;
39
40     /* TODO(3.0): Remove legacy code below */
41  legacy:
42
43     /*
44      * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
45      * sometimes only copies of the context are ever finalised.
46      */
47     if (ctx->digest && ctx->digest->cleanup
48         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
49         ctx->digest->cleanup(ctx);
50     if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
51         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
52         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
53     }
54     /*
55      * pctx should be freed by the user of EVP_MD_CTX
56      * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
57      */
58     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
59         EVP_PKEY_CTX_free(ctx->pctx);
60 #ifndef OPENSSL_NO_ENGINE
61     ENGINE_finish(ctx->engine);
62 #endif
63     OPENSSL_cleanse(ctx, sizeof(*ctx));
64
65     return 1;
66 }
67
68 EVP_MD_CTX *EVP_MD_CTX_new(void)
69 {
70     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
71 }
72
73 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
74 {
75     if (ctx == NULL)
76         return;
77
78     if (ctx->digest == NULL || ctx->digest->prov == NULL)
79         goto legacy;
80
81     EVP_MD_CTX_reset(ctx);
82
83     EVP_MD_meth_free(ctx->fetched_digest);
84     ctx->fetched_digest = NULL;
85     ctx->digest = NULL;
86     ctx->reqdigest = NULL;
87
88     OPENSSL_free(ctx);
89     return;
90
91     /* TODO(3.0): Remove legacy code below */
92  legacy:
93     EVP_MD_CTX_reset(ctx);
94     OPENSSL_free(ctx);
95 }
96
97 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
98 {
99     EVP_MD_CTX_reset(ctx);
100     return EVP_DigestInit_ex(ctx, type, NULL);
101 }
102
103 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
104 {
105     EVP_MD *provmd;
106     ENGINE *tmpimpl = NULL;
107
108     EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
109
110     if (type != NULL)
111         ctx->reqdigest = type;
112
113     /* TODO(3.0): Legacy work around code below. Remove this */
114 #ifndef OPENSSL_NO_ENGINE
115     /*
116      * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
117      * this context may already have an ENGINE! Try to avoid releasing the
118      * previous handle, re-querying for an ENGINE, and having a
119      * reinitialisation, when it may all be unnecessary.
120      */
121     if (ctx->engine && ctx->digest &&
122         (type == NULL || (type->type == ctx->digest->type)))
123         goto skip_to_init;
124
125     if (type != NULL && impl == NULL)
126         tmpimpl = ENGINE_get_digest_engine(type->type);
127 #endif
128
129     /*
130      * If there are engines involved or if we're being used as part of
131      * EVP_DigestSignInit then we should use legacy handling for now.
132      */
133     if (ctx->engine != NULL
134             || impl != NULL
135             || tmpimpl != NULL
136             || ctx->pctx != NULL
137             || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
138         if (ctx->digest == ctx->fetched_digest)
139             ctx->digest = NULL;
140         EVP_MD_meth_free(ctx->fetched_digest);
141         ctx->fetched_digest = NULL;
142         goto legacy;
143     }
144
145     if (type->prov == NULL) {
146         switch(type->type) {
147         case NID_sha256:
148             break;
149         default:
150             goto legacy;
151         }
152     }
153
154     if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
155         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
156         ctx->md_data = NULL;
157     }
158
159     /* TODO(3.0): Start of non-legacy code below */
160
161     if (type->prov == NULL) {
162         provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
163         if (provmd == NULL) {
164             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
165             return 0;
166         }
167         type = provmd;
168         EVP_MD_meth_free(ctx->fetched_digest);
169         ctx->fetched_digest = provmd;
170     }
171
172     ctx->digest = type;
173     if (ctx->provctx == NULL) {
174         ctx->provctx = ctx->digest->newctx();
175         if (ctx->provctx == NULL) {
176             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
177             return 0;
178         }
179     }
180
181     if (ctx->digest->dinit == NULL) {
182         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
183         return 0;
184     }
185
186     return ctx->digest->dinit(ctx->provctx);
187
188     /* TODO(3.0): Remove legacy code below */
189  legacy:
190
191 #ifndef OPENSSL_NO_ENGINE
192     if (type) {
193         /*
194          * Ensure an ENGINE left lying around from last time is cleared (the
195          * previous check attempted to avoid this if the same ENGINE and
196          * EVP_MD could be used).
197          */
198         ENGINE_finish(ctx->engine);
199         if (impl != NULL) {
200             if (!ENGINE_init(impl)) {
201                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
202                 return 0;
203             }
204         } else {
205             /* Ask if an ENGINE is reserved for this job */
206             impl = tmpimpl;
207         }
208         if (impl != NULL) {
209             /* There's an ENGINE for this job ... (apparently) */
210             const EVP_MD *d = ENGINE_get_digest(impl, type->type);
211
212             if (d == NULL) {
213                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
214                 ENGINE_finish(impl);
215                 return 0;
216             }
217             /* We'll use the ENGINE's private digest definition */
218             type = d;
219             /*
220              * Store the ENGINE functional reference so we know 'type' came
221              * from an ENGINE and we need to release it when done.
222              */
223             ctx->engine = impl;
224         } else
225             ctx->engine = NULL;
226     } else {
227         if (!ctx->digest) {
228             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
229             return 0;
230         }
231         type = ctx->digest;
232     }
233 #endif
234     if (ctx->digest != type) {
235         if (ctx->digest && ctx->digest->ctx_size) {
236             OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
237             ctx->md_data = NULL;
238         }
239         ctx->digest = type;
240         if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
241             ctx->update = type->update;
242             ctx->md_data = OPENSSL_zalloc(type->ctx_size);
243             if (ctx->md_data == NULL) {
244                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
245                 return 0;
246             }
247         }
248     }
249 #ifndef OPENSSL_NO_ENGINE
250  skip_to_init:
251 #endif
252     if (ctx->pctx) {
253         int r;
254         r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
255                               EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
256         if (r <= 0 && (r != -2))
257             return 0;
258     }
259     if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
260         return 1;
261     return ctx->digest->init(ctx);
262 }
263
264 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
265 {
266     if (count == 0)
267         return 1;
268
269     if (ctx->digest == NULL || ctx->digest->prov == NULL)
270         goto legacy;
271
272     if (ctx->digest->dupdate == NULL) {
273         EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
274         return 0;
275     }
276     return ctx->digest->dupdate(ctx->provctx, data, count);
277
278     /* TODO(3.0): Remove legacy code below */
279  legacy:
280     return ctx->update(ctx, data, count);
281 }
282
283 /* The caller can assume that this removes any secret data from the context */
284 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
285 {
286     int ret;
287     ret = EVP_DigestFinal_ex(ctx, md, size);
288     EVP_MD_CTX_reset(ctx);
289     return ret;
290 }
291
292 /* The caller can assume that this removes any secret data from the context */
293 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
294 {
295     int ret;
296     size_t size = 0;
297
298     if (ctx->digest == NULL || ctx->digest->prov == NULL)
299         goto legacy;
300
301     if (ctx->digest->dfinal == NULL) {
302         EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
303         return 0;
304     }
305
306     ret = ctx->digest->dfinal(ctx->provctx, md, &size);
307
308     if (isize != NULL) {
309         if (size <= UINT_MAX) {
310             *isize = (int)size;
311         } else {
312             EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
313             ret = 0;
314         }
315     }
316
317     EVP_MD_CTX_reset(ctx);
318
319     return ret;
320
321     /* TODO(3.0): Remove legacy code below */
322  legacy:
323     OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
324     ret = ctx->digest->final(ctx, md);
325     if (isize != NULL)
326         *isize = ctx->digest->md_size;
327     if (ctx->digest->cleanup) {
328         ctx->digest->cleanup(ctx);
329         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
330     }
331     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
332     return ret;
333 }
334
335 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
336 {
337     int ret = 0;
338
339     if (ctx->digest->flags & EVP_MD_FLAG_XOF
340         && size <= INT_MAX
341         && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
342         ret = ctx->digest->final(ctx, md);
343
344         if (ctx->digest->cleanup != NULL) {
345             ctx->digest->cleanup(ctx);
346             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
347         }
348         OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
349     } else {
350         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
351     }
352
353     return ret;
354 }
355
356 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
357 {
358     EVP_MD_CTX_reset(out);
359     return EVP_MD_CTX_copy_ex(out, in);
360 }
361
362 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
363 {
364     unsigned char *tmp_buf;
365
366     if (in == NULL || in->digest == NULL) {
367         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
368         return 0;
369     }
370
371     if (in->digest->prov == NULL)
372         goto legacy;
373
374     if (in->digest->dupctx == NULL) {
375         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
376         return 0;
377     }
378
379     EVP_MD_CTX_reset(out);
380     if (out->fetched_digest != NULL)
381         EVP_MD_meth_free(out->fetched_digest);
382     *out = *in;
383     /* NULL out pointers in case of error */
384     out->pctx = NULL;
385     out->provctx = NULL;
386
387     if (in->fetched_digest != NULL)
388         EVP_MD_upref(in->fetched_digest);
389
390     out->provctx = in->digest->dupctx(in->provctx);
391     if (out->provctx == NULL) {
392         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
393         return 0;
394     }
395
396     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
397     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
398     if (in->pctx != NULL) {
399         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
400         if (out->pctx == NULL) {
401             EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
402             EVP_MD_CTX_reset(out);
403             return 0;
404         }
405     }
406
407     return 1;
408
409     /* TODO(3.0): Remove legacy code below */
410  legacy:
411 #ifndef OPENSSL_NO_ENGINE
412     /* Make sure it's safe to copy a digest context using an ENGINE */
413     if (in->engine && !ENGINE_init(in->engine)) {
414         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
415         return 0;
416     }
417 #endif
418
419     if (out->digest == in->digest) {
420         tmp_buf = out->md_data;
421         EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
422     } else
423         tmp_buf = NULL;
424     EVP_MD_CTX_reset(out);
425     memcpy(out, in, sizeof(*out));
426
427     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
428     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
429
430     /* Null these variables, since they are getting fixed up
431      * properly below.  Anything else may cause a memleak and/or
432      * double free if any of the memory allocations below fail
433      */
434     out->md_data = NULL;
435     out->pctx = NULL;
436
437     if (in->md_data && out->digest->ctx_size) {
438         if (tmp_buf)
439             out->md_data = tmp_buf;
440         else {
441             out->md_data = OPENSSL_malloc(out->digest->ctx_size);
442             if (out->md_data == NULL) {
443                 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
444                 return 0;
445             }
446         }
447         memcpy(out->md_data, in->md_data, out->digest->ctx_size);
448     }
449
450     out->update = in->update;
451
452     if (in->pctx) {
453         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
454         if (!out->pctx) {
455             EVP_MD_CTX_reset(out);
456             return 0;
457         }
458     }
459
460     if (out->digest->copy)
461         return out->digest->copy(out, in);
462
463     return 1;
464 }
465
466 int EVP_Digest(const void *data, size_t count,
467                unsigned char *md, unsigned int *size, const EVP_MD *type,
468                ENGINE *impl)
469 {
470     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
471     int ret;
472
473     if (ctx == NULL)
474         return 0;
475     EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
476     ret = EVP_DigestInit_ex(ctx, type, impl)
477         && EVP_DigestUpdate(ctx, data, count)
478         && EVP_DigestFinal_ex(ctx, md, size);
479     EVP_MD_CTX_free(ctx);
480
481     return ret;
482 }
483
484 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
485 {
486     if (ctx->digest && ctx->digest->md_ctrl) {
487         int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
488         if (ret <= 0)
489             return 0;
490         return 1;
491     }
492     return 0;
493 }
494
495 static void *evp_md_from_dispatch(int mdtype, const OSSL_DISPATCH *fns,
496                                     OSSL_PROVIDER *prov)
497 {
498     EVP_MD *md = NULL;
499     int fncnt = 0;
500
501     if ((md = EVP_MD_meth_new(mdtype, NID_undef)) == NULL)
502         return NULL;
503
504     for (; fns->function_id != 0; fns++) {
505         switch (fns->function_id) {
506         case OSSL_FUNC_DIGEST_NEWCTX:
507             if (md->newctx != NULL)
508                 break;
509             md->newctx = OSSL_get_OP_digest_newctx(fns);
510             fncnt++;
511             break;
512         case OSSL_FUNC_DIGEST_INIT:
513             if (md->dinit != NULL)
514                 break;
515             md->dinit = OSSL_get_OP_digest_init(fns);
516             fncnt++;
517             break;
518         case OSSL_FUNC_DIGEST_UPDDATE:
519             if (md->dupdate != NULL)
520                 break;
521             md->dupdate = OSSL_get_OP_digest_update(fns);
522             fncnt++;
523             break;
524         case OSSL_FUNC_DIGEST_FINAL:
525             if (md->dfinal != NULL)
526                 break;
527             md->dfinal = OSSL_get_OP_digest_final(fns);
528             fncnt++;
529             break;
530         case OSSL_FUNC_DIGEST_DIGEST:
531             if (md->digest != NULL)
532                 break;
533             md->digest = OSSL_get_OP_digest_digest(fns);
534             /* We don't increment fnct for this as it is stand alone */
535             break;
536         case OSSL_FUNC_DIGEST_FREECTX:
537             if (md->freectx != NULL)
538                 break;
539             md->freectx = OSSL_get_OP_digest_freectx(fns);
540             fncnt++;
541             break;
542         case OSSL_FUNC_DIGEST_DUPCTX:
543             if (md->dupctx != NULL)
544                 break;
545             md->dupctx = OSSL_get_OP_digest_dupctx(fns);
546             break;
547         case OSSL_FUNC_DIGEST_SIZE:
548             if (md->size != NULL)
549                 break;
550             md->size = OSSL_get_OP_digest_size(fns);
551             break;
552         case OSSL_FUNC_DIGEST_BLOCK_SIZE:
553             if (md->dblock_size != NULL)
554                 break;
555             md->dblock_size = OSSL_get_OP_digest_block_size(fns);
556             break;
557         }
558     }
559     if ((fncnt != 0 && fncnt != 5)
560         || (fncnt == 0 && md->digest == NULL)
561         || md->size == NULL) {
562         /*
563          * In order to be a consistent set of functions we either need the
564          * whole set of init/update/final etc functions or none of them.
565          * The "digest" function can standalone. We at least need one way to
566          * generate digests.
567          */
568         EVP_MD_meth_free(md);
569         return NULL;
570     }
571     md->prov = prov;
572     if (prov != NULL)
573         ossl_provider_upref(prov);
574
575     return md;
576 }
577
578 static int evp_md_upref(void *md)
579 {
580     return EVP_MD_upref(md);
581 }
582
583 static void evp_md_free(void *md)
584 {
585     EVP_MD_meth_free(md);
586 }
587
588 static int evp_md_nid(void *vmd)
589 {
590     EVP_MD *md = vmd;
591
592     return md->type;
593 }
594
595 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
596                      const char *properties)
597 {
598     return evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
599                              evp_md_from_dispatch, evp_md_upref,
600                              evp_md_free, evp_md_nid);
601 }