/src/openssl/crypto/evp/bio_md.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2021 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 <errno.h>  | 
12  |  | #include <openssl/buffer.h>  | 
13  |  | #include <openssl/evp.h>  | 
14  |  | #include "internal/bio.h"  | 
15  |  |  | 
16  |  | /*  | 
17  |  |  * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest  | 
18  |  |  */  | 
19  |  |  | 
20  |  | static int md_write(BIO *h, char const *buf, int num);  | 
21  |  | static int md_read(BIO *h, char *buf, int size);  | 
22  |  | static int md_gets(BIO *h, char *str, int size);  | 
23  |  | static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);  | 
24  |  | static int md_new(BIO *h);  | 
25  |  | static int md_free(BIO *data);  | 
26  |  | static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);  | 
27  |  |  | 
28  |  | static const BIO_METHOD methods_md = { | 
29  |  |     BIO_TYPE_MD,  | 
30  |  |     "message digest",  | 
31  |  |     bwrite_conv,  | 
32  |  |     md_write,  | 
33  |  |     bread_conv,  | 
34  |  |     md_read,  | 
35  |  |     NULL,                       /* md_puts, */  | 
36  |  |     md_gets,  | 
37  |  |     md_ctrl,  | 
38  |  |     md_new,  | 
39  |  |     md_free,  | 
40  |  |     md_callback_ctrl,  | 
41  |  | };  | 
42  |  |  | 
43  |  | const BIO_METHOD *BIO_f_md(void)  | 
44  | 0  | { | 
45  | 0  |     return &methods_md;  | 
46  | 0  | }  | 
47  |  |  | 
48  |  | static int md_new(BIO *bi)  | 
49  | 0  | { | 
50  | 0  |     EVP_MD_CTX *ctx;  | 
51  |  | 
  | 
52  | 0  |     ctx = EVP_MD_CTX_new();  | 
53  | 0  |     if (ctx == NULL)  | 
54  | 0  |         return 0;  | 
55  |  |  | 
56  | 0  |     BIO_set_init(bi, 1);  | 
57  | 0  |     BIO_set_data(bi, ctx);  | 
58  |  | 
  | 
59  | 0  |     return 1;  | 
60  | 0  | }  | 
61  |  |  | 
62  |  | static int md_free(BIO *a)  | 
63  | 0  | { | 
64  | 0  |     if (a == NULL)  | 
65  | 0  |         return 0;  | 
66  | 0  |     EVP_MD_CTX_free(BIO_get_data(a));  | 
67  | 0  |     BIO_set_data(a, NULL);  | 
68  | 0  |     BIO_set_init(a, 0);  | 
69  |  | 
  | 
70  | 0  |     return 1;  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | static int md_read(BIO *b, char *out, int outl)  | 
74  | 0  | { | 
75  | 0  |     int ret = 0;  | 
76  | 0  |     EVP_MD_CTX *ctx;  | 
77  | 0  |     BIO *next;  | 
78  |  | 
  | 
79  | 0  |     if (out == NULL)  | 
80  | 0  |         return 0;  | 
81  |  |  | 
82  | 0  |     ctx = BIO_get_data(b);  | 
83  | 0  |     next = BIO_next(b);  | 
84  |  | 
  | 
85  | 0  |     if ((ctx == NULL) || (next == NULL))  | 
86  | 0  |         return 0;  | 
87  |  |  | 
88  | 0  |     ret = BIO_read(next, out, outl);  | 
89  | 0  |     if (BIO_get_init(b)) { | 
90  | 0  |         if (ret > 0) { | 
91  | 0  |             if (EVP_DigestUpdate(ctx, (unsigned char *)out,  | 
92  | 0  |                                  (unsigned int)ret) <= 0)  | 
93  | 0  |                 return -1;  | 
94  | 0  |         }  | 
95  | 0  |     }  | 
96  | 0  |     BIO_clear_retry_flags(b);  | 
97  | 0  |     BIO_copy_next_retry(b);  | 
98  | 0  |     return ret;  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | static int md_write(BIO *b, const char *in, int inl)  | 
102  | 0  | { | 
103  | 0  |     int ret = 0;  | 
104  | 0  |     EVP_MD_CTX *ctx;  | 
105  | 0  |     BIO *next;  | 
106  |  | 
  | 
107  | 0  |     if ((in == NULL) || (inl <= 0))  | 
108  | 0  |         return 0;  | 
109  |  |  | 
110  | 0  |     ctx = BIO_get_data(b);  | 
111  | 0  |     next = BIO_next(b);  | 
112  | 0  |     if ((ctx != NULL) && (next != NULL))  | 
113  | 0  |         ret = BIO_write(next, in, inl);  | 
114  |  | 
  | 
115  | 0  |     if (BIO_get_init(b)) { | 
116  | 0  |         if (ret > 0) { | 
117  | 0  |             if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,  | 
118  | 0  |                                   (unsigned int)ret)) { | 
119  | 0  |                 BIO_clear_retry_flags(b);  | 
120  | 0  |                 return 0;  | 
121  | 0  |             }  | 
122  | 0  |         }  | 
123  | 0  |     }  | 
124  | 0  |     if (next != NULL) { | 
125  | 0  |         BIO_clear_retry_flags(b);  | 
126  | 0  |         BIO_copy_next_retry(b);  | 
127  | 0  |     }  | 
128  | 0  |     return ret;  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | static long md_ctrl(BIO *b, int cmd, long num, void *ptr)  | 
132  | 0  | { | 
133  | 0  |     EVP_MD_CTX *ctx, *dctx, **pctx;  | 
134  | 0  |     const EVP_MD **ppmd;  | 
135  | 0  |     EVP_MD *md;  | 
136  | 0  |     long ret = 1;  | 
137  | 0  |     BIO *dbio, *next;  | 
138  |  |  | 
139  |  | 
  | 
140  | 0  |     ctx = BIO_get_data(b);  | 
141  | 0  |     next = BIO_next(b);  | 
142  |  | 
  | 
143  | 0  |     switch (cmd) { | 
144  | 0  |     case BIO_CTRL_RESET:  | 
145  | 0  |         if (BIO_get_init(b))  | 
146  | 0  |             ret = EVP_DigestInit_ex(ctx, EVP_MD_CTX_get0_md(ctx), NULL);  | 
147  | 0  |         else  | 
148  | 0  |             ret = 0;  | 
149  | 0  |         if (ret > 0)  | 
150  | 0  |             ret = BIO_ctrl(next, cmd, num, ptr);  | 
151  | 0  |         break;  | 
152  | 0  |     case BIO_C_GET_MD:  | 
153  | 0  |         if (BIO_get_init(b)) { | 
154  | 0  |             ppmd = ptr;  | 
155  | 0  |             *ppmd = EVP_MD_CTX_get0_md(ctx);  | 
156  | 0  |         } else  | 
157  | 0  |             ret = 0;  | 
158  | 0  |         break;  | 
159  | 0  |     case BIO_C_GET_MD_CTX:  | 
160  | 0  |         pctx = ptr;  | 
161  | 0  |         *pctx = ctx;  | 
162  | 0  |         BIO_set_init(b, 1);  | 
163  | 0  |         break;  | 
164  | 0  |     case BIO_C_SET_MD_CTX:  | 
165  | 0  |         if (BIO_get_init(b))  | 
166  | 0  |             BIO_set_data(b, ptr);  | 
167  | 0  |         else  | 
168  | 0  |             ret = 0;  | 
169  | 0  |         break;  | 
170  | 0  |     case BIO_C_DO_STATE_MACHINE:  | 
171  | 0  |         BIO_clear_retry_flags(b);  | 
172  | 0  |         ret = BIO_ctrl(next, cmd, num, ptr);  | 
173  | 0  |         BIO_copy_next_retry(b);  | 
174  | 0  |         break;  | 
175  |  |  | 
176  | 0  |     case BIO_C_SET_MD:  | 
177  | 0  |         md = ptr;  | 
178  | 0  |         ret = EVP_DigestInit_ex(ctx, md, NULL);  | 
179  | 0  |         if (ret > 0)  | 
180  | 0  |             BIO_set_init(b, 1);  | 
181  | 0  |         break;  | 
182  | 0  |     case BIO_CTRL_DUP:  | 
183  | 0  |         dbio = ptr;  | 
184  | 0  |         dctx = BIO_get_data(dbio);  | 
185  | 0  |         if (!EVP_MD_CTX_copy_ex(dctx, ctx))  | 
186  | 0  |             return 0;  | 
187  | 0  |         BIO_set_init(b, 1);  | 
188  | 0  |         break;  | 
189  | 0  |     default:  | 
190  | 0  |         ret = BIO_ctrl(next, cmd, num, ptr);  | 
191  | 0  |         break;  | 
192  | 0  |     }  | 
193  | 0  |     return ret;  | 
194  | 0  | }  | 
195  |  |  | 
196  |  | static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)  | 
197  | 0  | { | 
198  | 0  |     BIO *next;  | 
199  |  | 
  | 
200  | 0  |     next = BIO_next(b);  | 
201  |  | 
  | 
202  | 0  |     if (next == NULL)  | 
203  | 0  |         return 0;  | 
204  |  |  | 
205  | 0  |     return BIO_callback_ctrl(next, cmd, fp);  | 
206  | 0  | }  | 
207  |  |  | 
208  |  | static int md_gets(BIO *bp, char *buf, int size)  | 
209  | 0  | { | 
210  | 0  |     EVP_MD_CTX *ctx;  | 
211  | 0  |     unsigned int ret;  | 
212  |  | 
  | 
213  | 0  |     ctx = BIO_get_data(bp);  | 
214  |  | 
  | 
215  | 0  |     if (size < EVP_MD_CTX_get_size(ctx))  | 
216  | 0  |         return 0;  | 
217  |  |  | 
218  | 0  |     if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)  | 
219  | 0  |         return -1;  | 
220  |  |  | 
221  | 0  |     return (int)ret;  | 
222  | 0  | }  |