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