xref: /dragonfly/crypto/libressl/crypto/evp/bio_md.c (revision 6f5ec8b5)
1 /* $OpenBSD: bio_md.c,v 1.18 2022/01/14 08:40:57 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 #include <errno.h>
61 
62 #include <openssl/buffer.h>
63 #include <openssl/evp.h>
64 
65 #include "bio_local.h"
66 #include "evp_locl.h"
67 
68 /* BIO_put and BIO_get both add to the digest,
69  * BIO_gets returns the digest */
70 
71 static int md_write(BIO *h, char const *buf, int num);
72 static int md_read(BIO *h, char *buf, int size);
73 /*static int md_puts(BIO *h, const char *str); */
74 static int md_gets(BIO *h, char *str, int size);
75 static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
76 static int md_new(BIO *h);
77 static int md_free(BIO *data);
78 static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
79 
80 static const BIO_METHOD methods_md = {
81 	.type = BIO_TYPE_MD,
82 	.name = "message digest",
83 	.bwrite = md_write,
84 	.bread = md_read,
85 	.bgets = md_gets,
86 	.ctrl = md_ctrl,
87 	.create = md_new,
88 	.destroy = md_free,
89 	.callback_ctrl = md_callback_ctrl
90 };
91 
92 const BIO_METHOD *
93 BIO_f_md(void)
94 {
95 	return (&methods_md);
96 }
97 
98 static int
99 md_new(BIO *bi)
100 {
101 	EVP_MD_CTX *ctx;
102 
103 	ctx = EVP_MD_CTX_create();
104 	if (ctx == NULL)
105 		return (0);
106 
107 	bi->init = 0;
108 	bi->ptr = (char *)ctx;
109 	bi->flags = 0;
110 	return (1);
111 }
112 
113 static int
114 md_free(BIO *a)
115 {
116 	if (a == NULL)
117 		return (0);
118 	EVP_MD_CTX_destroy(a->ptr);
119 	a->ptr = NULL;
120 	a->init = 0;
121 	a->flags = 0;
122 	return (1);
123 }
124 
125 static int
126 md_read(BIO *b, char *out, int outl)
127 {
128 	int ret = 0;
129 	EVP_MD_CTX *ctx;
130 
131 	if (out == NULL)
132 		return (0);
133 	ctx = b->ptr;
134 
135 	if ((ctx == NULL) || (b->next_bio == NULL))
136 		return (0);
137 
138 	ret = BIO_read(b->next_bio, out, outl);
139 	if (b->init) {
140 		if (ret > 0) {
141 			if (EVP_DigestUpdate(ctx, (unsigned char *)out,
142 			    (unsigned int)ret) <= 0)
143 				return (-1);
144 		}
145 	}
146 	BIO_clear_retry_flags(b);
147 	BIO_copy_next_retry(b);
148 	return (ret);
149 }
150 
151 static int
152 md_write(BIO *b, const char *in, int inl)
153 {
154 	int ret = 0;
155 	EVP_MD_CTX *ctx;
156 
157 	if ((in == NULL) || (inl <= 0))
158 		return (0);
159 	ctx = b->ptr;
160 
161 	if ((ctx != NULL) && (b->next_bio != NULL))
162 		ret = BIO_write(b->next_bio, in, inl);
163 	if (b->init) {
164 		if (ret > 0) {
165 			if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
166 			    (unsigned int)ret)) {
167 				BIO_clear_retry_flags(b);
168 				return 0;
169 			}
170 		}
171 	}
172 	if (b->next_bio != NULL) {
173 		BIO_clear_retry_flags(b);
174 		BIO_copy_next_retry(b);
175 	}
176 	return (ret);
177 }
178 
179 static long
180 md_ctrl(BIO *b, int cmd, long num, void *ptr)
181 {
182 	EVP_MD_CTX *ctx, *dctx, **pctx;
183 	const EVP_MD **ppmd;
184 	EVP_MD *md;
185 	long ret = 1;
186 	BIO *dbio;
187 
188 	ctx = b->ptr;
189 
190 	switch (cmd) {
191 	case BIO_CTRL_RESET:
192 		if (b->init)
193 			ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
194 		else
195 			ret = 0;
196 		if (ret > 0)
197 			ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
198 		break;
199 	case BIO_C_GET_MD:
200 		if (b->init) {
201 			ppmd = ptr;
202 			*ppmd = ctx->digest;
203 		} else
204 			ret = 0;
205 		break;
206 	case BIO_C_GET_MD_CTX:
207 		pctx = ptr;
208 		*pctx = ctx;
209 		b->init = 1;
210 		break;
211 	case BIO_C_SET_MD_CTX:
212 		if (b->init)
213 			b->ptr = ptr;
214 		else
215 			ret = 0;
216 		break;
217 	case BIO_C_DO_STATE_MACHINE:
218 		BIO_clear_retry_flags(b);
219 		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
220 		BIO_copy_next_retry(b);
221 		break;
222 
223 	case BIO_C_SET_MD:
224 		md = ptr;
225 		ret = EVP_DigestInit_ex(ctx, md, NULL);
226 		if (ret > 0)
227 			b->init = 1;
228 		break;
229 	case BIO_CTRL_DUP:
230 		dbio = ptr;
231 		dctx = dbio->ptr;
232 		if (!EVP_MD_CTX_copy_ex(dctx, ctx))
233 			return 0;
234 		b->init = 1;
235 		break;
236 	default:
237 		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
238 		break;
239 	}
240 	return (ret);
241 }
242 
243 static long
244 md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
245 {
246 	long ret = 1;
247 
248 	if (b->next_bio == NULL)
249 		return (0);
250 	switch (cmd) {
251 	default:
252 		ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
253 		break;
254 	}
255 	return (ret);
256 }
257 
258 static int
259 md_gets(BIO *bp, char *buf, int size)
260 {
261 	EVP_MD_CTX *ctx;
262 	unsigned int ret;
263 
264 	ctx = bp->ptr;
265 	if (size < ctx->digest->md_size)
266 		return (0);
267 	if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
268 		return -1;
269 
270 	return ((int)ret);
271 }
272 
273 /*
274 static int md_puts(bp,str)
275 BIO *bp;
276 char *str;
277 	{
278 	return(-1);
279 	}
280 */
281