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