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