]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/bio_md.c
Implement provider support for Asym Ciphers
[thirdparty/openssl.git] / crypto / evp / bio_md.c
CommitLineData
62867571
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <errno.h>
ec577822
BM
12#include <openssl/buffer.h>
13#include <openssl/evp.h>
a146ae55 14#include "internal/bio.h"
d02b48c6 15
0f113f3e
MC
16/*
17 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
18 */
d02b48c6 19
0e1c0612
UM
20static int md_write(BIO *h, char const *buf, int num);
21static int md_read(BIO *h, char *buf, int size);
0e1c0612
UM
22static int md_gets(BIO *h, char *str, int size);
23static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
d02b48c6
RE
24static int md_new(BIO *h);
25static int md_free(BIO *data);
fce78bd4 26static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
d3442bc7 27
04f6b0fd 28static const BIO_METHOD methods_md = {
27ab9195
DB
29 BIO_TYPE_MD,
30 "message digest",
3befffa3
MC
31 /* TODO: Convert to new style write function */
32 bwrite_conv,
0f113f3e 33 md_write,
d07aee2c
MC
34 /* TODO: Convert to new style read function */
35 bread_conv,
0f113f3e
MC
36 md_read,
37 NULL, /* md_puts, */
38 md_gets,
39 md_ctrl,
40 md_new,
41 md_free,
42 md_callback_ctrl,
43};
d02b48c6 44
04f6b0fd 45const BIO_METHOD *BIO_f_md(void)
0f113f3e 46{
26a7d938 47 return &methods_md;
0f113f3e 48}
d02b48c6 49
6b691a5c 50static int md_new(BIO *bi)
0f113f3e
MC
51{
52 EVP_MD_CTX *ctx;
d02b48c6 53
bfb0641f 54 ctx = EVP_MD_CTX_new();
0f113f3e 55 if (ctx == NULL)
26a7d938 56 return 0;
d02b48c6 57
a146ae55
MC
58 BIO_set_init(bi, 1);
59 BIO_set_data(bi, ctx);
60
61 return 1;
0f113f3e 62}
d02b48c6 63
6b691a5c 64static int md_free(BIO *a)
0f113f3e
MC
65{
66 if (a == NULL)
26a7d938 67 return 0;
a146ae55
MC
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;
0f113f3e
MC
73}
74
6b691a5c 75static int md_read(BIO *b, char *out, int outl)
0f113f3e
MC
76{
77 int ret = 0;
78 EVP_MD_CTX *ctx;
a146ae55 79 BIO *next;
d02b48c6 80
0f113f3e 81 if (out == NULL)
26a7d938 82 return 0;
d02b48c6 83
a146ae55
MC
84 ctx = BIO_get_data(b);
85 next = BIO_next(b);
86
87 if ((ctx == NULL) || (next == NULL))
26a7d938 88 return 0;
d02b48c6 89
a146ae55
MC
90 ret = BIO_read(next, out, outl);
91 if (BIO_get_init(b)) {
0f113f3e
MC
92 if (ret > 0) {
93 if (EVP_DigestUpdate(ctx, (unsigned char *)out,
94 (unsigned int)ret) <= 0)
26a7d938 95 return -1;
0f113f3e
MC
96 }
97 }
98 BIO_clear_retry_flags(b);
99 BIO_copy_next_retry(b);
26a7d938 100 return ret;
0f113f3e 101}
d02b48c6 102
0e1c0612 103static int md_write(BIO *b, const char *in, int inl)
0f113f3e
MC
104{
105 int ret = 0;
106 EVP_MD_CTX *ctx;
a146ae55 107 BIO *next;
d02b48c6 108
0f113f3e 109 if ((in == NULL) || (inl <= 0))
a146ae55 110 return 0;
d02b48c6 111
a146ae55
MC
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)) {
0f113f3e
MC
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 }
a146ae55 126 if (next != NULL) {
0f113f3e
MC
127 BIO_clear_retry_flags(b);
128 BIO_copy_next_retry(b);
129 }
a146ae55 130 return ret;
0f113f3e 131}
d02b48c6 132
0e1c0612 133static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
134{
135 EVP_MD_CTX *ctx, *dctx, **pctx;
136 const EVP_MD **ppmd;
137 EVP_MD *md;
138 long ret = 1;
a146ae55 139 BIO *dbio, *next;
d02b48c6 140
a146ae55
MC
141
142 ctx = BIO_get_data(b);
143 next = BIO_next(b);
d02b48c6 144
0f113f3e
MC
145 switch (cmd) {
146 case BIO_CTRL_RESET:
a146ae55 147 if (BIO_get_init(b))
bc3a1377 148 ret = EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL);
0f113f3e
MC
149 else
150 ret = 0;
151 if (ret > 0)
a146ae55 152 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
153 break;
154 case BIO_C_GET_MD:
a146ae55 155 if (BIO_get_init(b)) {
0f113f3e 156 ppmd = ptr;
bc3a1377 157 *ppmd = EVP_MD_CTX_md(ctx);
0f113f3e
MC
158 } else
159 ret = 0;
160 break;
161 case BIO_C_GET_MD_CTX:
162 pctx = ptr;
163 *pctx = ctx;
a146ae55 164 BIO_set_init(b, 1);
0f113f3e
MC
165 break;
166 case BIO_C_SET_MD_CTX:
a146ae55
MC
167 if (BIO_get_init(b))
168 BIO_set_data(b, ptr);
0f113f3e
MC
169 else
170 ret = 0;
171 break;
172 case BIO_C_DO_STATE_MACHINE:
173 BIO_clear_retry_flags(b);
a146ae55 174 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
175 BIO_copy_next_retry(b);
176 break;
d02b48c6 177
0f113f3e
MC
178 case BIO_C_SET_MD:
179 md = ptr;
180 ret = EVP_DigestInit_ex(ctx, md, NULL);
181 if (ret > 0)
a146ae55 182 BIO_set_init(b, 1);
0f113f3e
MC
183 break;
184 case BIO_CTRL_DUP:
185 dbio = ptr;
a146ae55 186 dctx = BIO_get_data(dbio);
0f113f3e
MC
187 if (!EVP_MD_CTX_copy_ex(dctx, ctx))
188 return 0;
a146ae55 189 BIO_set_init(b, 1);
0f113f3e
MC
190 break;
191 default:
a146ae55 192 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
193 break;
194 }
26a7d938 195 return ret;
0f113f3e 196}
d02b48c6 197
fce78bd4 198static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
199{
200 long ret = 1;
a146ae55
MC
201 BIO *next;
202
203 next = BIO_next(b);
204
205 if (next == NULL)
206 return 0;
d3442bc7 207
0f113f3e
MC
208 switch (cmd) {
209 default:
a146ae55 210 ret = BIO_callback_ctrl(next, cmd, fp);
0f113f3e
MC
211 break;
212 }
26a7d938 213 return ret;
0f113f3e 214}
d3442bc7 215
6b691a5c 216static int md_gets(BIO *bp, char *buf, int size)
0f113f3e
MC
217{
218 EVP_MD_CTX *ctx;
219 unsigned int ret;
d02b48c6 220
a146ae55
MC
221 ctx = BIO_get_data(bp);
222
bc3a1377 223 if (size < EVP_MD_CTX_size(ctx))
a146ae55
MC
224 return 0;
225
0f113f3e
MC
226 if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
227 return -1;
d02b48c6 228
26a7d938 229 return (int)ret;
0f113f3e 230}