1/* $OpenBSD: bio_md.c,v 1.22 2024/04/09 13:52:41 beck 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_local.h"
67
68/* BIO_put and BIO_get both add to the digest,
69 * BIO_gets returns the digest */
70
71static int md_write(BIO *h, char const *buf, int num);
72static int md_read(BIO *h, char *buf, int size);
73/*static int md_puts(BIO *h, const char *str); */
74static int md_gets(BIO *h, char *str, int size);
75static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
76static int md_new(BIO *h);
77static int md_free(BIO *data);
78static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
79
80static 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
92const BIO_METHOD *
93BIO_f_md(void)
94{
95	return (&methods_md);
96}
97LCRYPTO_ALIAS(BIO_f_md);
98
99static int
100md_new(BIO *bi)
101{
102	EVP_MD_CTX *ctx;
103
104	ctx = EVP_MD_CTX_create();
105	if (ctx == NULL)
106		return (0);
107
108	bi->init = 0;
109	bi->ptr = (char *)ctx;
110	bi->flags = 0;
111	return (1);
112}
113
114static int
115md_free(BIO *a)
116{
117	if (a == NULL)
118		return (0);
119	EVP_MD_CTX_destroy(a->ptr);
120	a->ptr = NULL;
121	a->init = 0;
122	a->flags = 0;
123	return (1);
124}
125
126static int
127md_read(BIO *b, char *out, int outl)
128{
129	int ret = 0;
130	EVP_MD_CTX *ctx;
131
132	if (out == NULL)
133		return (0);
134	ctx = b->ptr;
135
136	if ((ctx == NULL) || (b->next_bio == NULL))
137		return (0);
138
139	ret = BIO_read(b->next_bio, out, outl);
140	if (b->init) {
141		if (ret > 0) {
142			if (EVP_DigestUpdate(ctx, (unsigned char *)out,
143			    (unsigned int)ret) <= 0)
144				return (-1);
145		}
146	}
147	BIO_clear_retry_flags(b);
148	BIO_copy_next_retry(b);
149	return (ret);
150}
151
152static int
153md_write(BIO *b, const char *in, int inl)
154{
155	int ret = 0;
156	EVP_MD_CTX *ctx;
157
158	if ((in == NULL) || (inl <= 0))
159		return (0);
160	ctx = b->ptr;
161
162	if ((ctx != NULL) && (b->next_bio != NULL))
163		ret = BIO_write(b->next_bio, in, inl);
164	if (b->init) {
165		if (ret > 0) {
166			if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
167			    (unsigned int)ret)) {
168				BIO_clear_retry_flags(b);
169				return 0;
170			}
171		}
172	}
173	if (b->next_bio != NULL) {
174		BIO_clear_retry_flags(b);
175		BIO_copy_next_retry(b);
176	}
177	return (ret);
178}
179
180static long
181md_ctrl(BIO *b, int cmd, long num, void *ptr)
182{
183	EVP_MD_CTX *ctx, *dctx, **pctx;
184	const EVP_MD **ppmd;
185	EVP_MD *md;
186	long ret = 1;
187	BIO *dbio;
188
189	ctx = b->ptr;
190
191	switch (cmd) {
192	case BIO_CTRL_RESET:
193		if (b->init)
194			ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
195		else
196			ret = 0;
197		if (ret > 0)
198			ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
199		break;
200	case BIO_C_GET_MD:
201		if (b->init) {
202			ppmd = ptr;
203			*ppmd = ctx->digest;
204		} else
205			ret = 0;
206		break;
207	case BIO_C_GET_MD_CTX:
208		pctx = ptr;
209		*pctx = ctx;
210		b->init = 1;
211		break;
212	case BIO_C_SET_MD_CTX:
213		if (b->init)
214			b->ptr = ptr;
215		else
216			ret = 0;
217		break;
218	case BIO_C_DO_STATE_MACHINE:
219		BIO_clear_retry_flags(b);
220		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
221		BIO_copy_next_retry(b);
222		break;
223
224	case BIO_C_SET_MD:
225		md = ptr;
226		ret = EVP_DigestInit_ex(ctx, md, NULL);
227		if (ret > 0)
228			b->init = 1;
229		break;
230	case BIO_CTRL_DUP:
231		dbio = ptr;
232		dctx = dbio->ptr;
233		if (!EVP_MD_CTX_copy_ex(dctx, ctx))
234			return 0;
235		b->init = 1;
236		break;
237	default:
238		ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
239		break;
240	}
241	return (ret);
242}
243
244static long
245md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
246{
247	long ret = 1;
248
249	if (b->next_bio == NULL)
250		return (0);
251	switch (cmd) {
252	default:
253		ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
254		break;
255	}
256	return (ret);
257}
258
259static int
260md_gets(BIO *bp, char *buf, int size)
261{
262	EVP_MD_CTX *ctx;
263	unsigned int ret;
264
265	ctx = bp->ptr;
266	if (size < ctx->digest->md_size)
267		return (0);
268	if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
269		return -1;
270
271	return ((int)ret);
272}
273
274/*
275static int md_puts(bp,str)
276BIO *bp;
277char *str;
278	{
279	return(-1);
280	}
281*/
282