diff options
author | jsing <> | 2014-05-03 16:22:28 +0000 |
---|---|---|
committer | jsing <> | 2014-05-03 16:22:28 +0000 |
commit | 65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8 (patch) | |
tree | 4993b7660557e78f309731f46f029f969f974e84 /src/lib/libcrypto/evp/bio_ok.c | |
parent | 0225d59faf0ea827794d5d88eb3e1dddb205242c (diff) | |
download | openbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.tar.gz openbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.tar.bz2 openbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.zip |
KNF.
Diffstat (limited to 'src/lib/libcrypto/evp/bio_ok.c')
-rw-r--r-- | src/lib/libcrypto/evp/bio_ok.c | 581 |
1 files changed, 292 insertions, 289 deletions
diff --git a/src/lib/libcrypto/evp/bio_ok.c b/src/lib/libcrypto/evp/bio_ok.c index d0bcbc2bef..882aaee0fb 100644 --- a/src/lib/libcrypto/evp/bio_ok.c +++ b/src/lib/libcrypto/evp/bio_ok.c | |||
@@ -5,21 +5,21 @@ | |||
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 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 | 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 | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 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. | 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 | 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. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 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: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 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 | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
52 | * | 52 | * |
53 | * The licence and distribution terms for any publically available version or | 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 | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -62,54 +62,54 @@ | |||
62 | Why BIO_f_reliable? | 62 | Why BIO_f_reliable? |
63 | 63 | ||
64 | I wrote function which took BIO* as argument, read data from it | 64 | I wrote function which took BIO* as argument, read data from it |
65 | and processed it. Then I wanted to store the input file in | 65 | and processed it. Then I wanted to store the input file in |
66 | encrypted form. OK I pushed BIO_f_cipher to the BIO stack | 66 | encrypted form. OK I pushed BIO_f_cipher to the BIO stack |
67 | and everything was OK. BUT if user types wrong password | 67 | and everything was OK. BUT if user types wrong password |
68 | BIO_f_cipher outputs only garbage and my function crashes. Yes | 68 | BIO_f_cipher outputs only garbage and my function crashes. Yes |
69 | I can and I should fix my function, but BIO_f_cipher is | 69 | I can and I should fix my function, but BIO_f_cipher is |
70 | easy way to add encryption support to many existing applications | 70 | easy way to add encryption support to many existing applications |
71 | and it's hard to debug and fix them all. | 71 | and it's hard to debug and fix them all. |
72 | 72 | ||
73 | So I wanted another BIO which would catch the incorrect passwords and | 73 | So I wanted another BIO which would catch the incorrect passwords and |
74 | file damages which cause garbage on BIO_f_cipher's output. | 74 | file damages which cause garbage on BIO_f_cipher's output. |
75 | 75 | ||
76 | The easy way is to push the BIO_f_md and save the checksum at | 76 | The easy way is to push the BIO_f_md and save the checksum at |
77 | the end of the file. However there are several problems with this | 77 | the end of the file. However there are several problems with this |
78 | approach: | 78 | approach: |
79 | 79 | ||
80 | 1) you must somehow separate checksum from actual data. | 80 | 1) you must somehow separate checksum from actual data. |
81 | 2) you need lot's of memory when reading the file, because you | 81 | 2) you need lot's of memory when reading the file, because you |
82 | must read to the end of the file and verify the checksum before | 82 | must read to the end of the file and verify the checksum before |
83 | letting the application to read the data. | 83 | letting the application to read the data. |
84 | 84 | ||
85 | BIO_f_reliable tries to solve both problems, so that you can | 85 | BIO_f_reliable tries to solve both problems, so that you can |
86 | read and write arbitrary long streams using only fixed amount | 86 | read and write arbitrary long streams using only fixed amount |
87 | of memory. | 87 | of memory. |
88 | 88 | ||
89 | BIO_f_reliable splits data stream into blocks. Each block is prefixed | 89 | BIO_f_reliable splits data stream into blocks. Each block is prefixed |
90 | with it's length and suffixed with it's digest. So you need only | 90 | with it's length and suffixed with it's digest. So you need only |
91 | several Kbytes of memory to buffer single block before verifying | 91 | several Kbytes of memory to buffer single block before verifying |
92 | it's digest. | 92 | it's digest. |
93 | 93 | ||
94 | BIO_f_reliable goes further and adds several important capabilities: | 94 | BIO_f_reliable goes further and adds several important capabilities: |
95 | 95 | ||
96 | 1) the digest of the block is computed over the whole stream | 96 | 1) the digest of the block is computed over the whole stream |
97 | -- so nobody can rearrange the blocks or remove or replace them. | 97 | -- so nobody can rearrange the blocks or remove or replace them. |
98 | 98 | ||
99 | 2) to detect invalid passwords right at the start BIO_f_reliable | 99 | 2) to detect invalid passwords right at the start BIO_f_reliable |
100 | adds special prefix to the stream. In order to avoid known plain-text | 100 | adds special prefix to the stream. In order to avoid known plain-text |
101 | attacks this prefix is generated as follows: | 101 | attacks this prefix is generated as follows: |
102 | 102 | ||
103 | *) digest is initialized with random seed instead of | 103 | *) digest is initialized with random seed instead of |
104 | standardized one. | 104 | standardized one. |
105 | *) same seed is written to output | 105 | *) same seed is written to output |
106 | *) well-known text is then hashed and the output | 106 | *) well-known text is then hashed and the output |
107 | of the digest is also written to output. | 107 | of the digest is also written to output. |
108 | 108 | ||
109 | reader can now read the seed from stream, hash the same string | 109 | reader can now read the seed from stream, hash the same string |
110 | and then compare the digest output. | 110 | and then compare the digest output. |
111 | 111 | ||
112 | Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I | 112 | Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I |
113 | initially wrote and tested this code on x86 machine and wrote the | 113 | initially wrote and tested this code on x86 machine and wrote the |
114 | digests out in machine-dependent order :( There are people using | 114 | digests out in machine-dependent order :( There are people using |
115 | this code and I cannot change this easily without making existing | 115 | this code and I cannot change this easily without making existing |
@@ -143,8 +143,7 @@ static int block_in(BIO* b); | |||
143 | #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE) | 143 | #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE) |
144 | #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back." | 144 | #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back." |
145 | 145 | ||
146 | typedef struct ok_struct | 146 | typedef struct ok_struct { |
147 | { | ||
148 | size_t buf_len; | 147 | size_t buf_len; |
149 | size_t buf_off; | 148 | size_t buf_off; |
150 | size_t buf_len_save; | 149 | size_t buf_len_save; |
@@ -152,10 +151,10 @@ typedef struct ok_struct | |||
152 | int cont; /* <= 0 when finished */ | 151 | int cont; /* <= 0 when finished */ |
153 | int finished; | 152 | int finished; |
154 | EVP_MD_CTX md; | 153 | EVP_MD_CTX md; |
155 | int blockout; /* output block is ready */ | 154 | int blockout; /* output block is ready */ |
156 | int sigio; /* must process signature */ | 155 | int sigio; /* must process signature */ |
157 | unsigned char buf[IOBS]; | 156 | unsigned char buf[IOBS]; |
158 | } BIO_OK_CTX; | 157 | } BIO_OK_CTX; |
159 | 158 | ||
160 | static BIO_METHOD methods_ok = { | 159 | static BIO_METHOD methods_ok = { |
161 | .type = BIO_TYPE_CIPHER, | 160 | .type = BIO_TYPE_CIPHER, |
@@ -168,321 +167,322 @@ static BIO_METHOD methods_ok = { | |||
168 | .callback_ctrl = ok_callback_ctrl | 167 | .callback_ctrl = ok_callback_ctrl |
169 | }; | 168 | }; |
170 | 169 | ||
171 | BIO_METHOD *BIO_f_reliable(void) | 170 | BIO_METHOD * |
172 | { | 171 | BIO_f_reliable(void) |
173 | return(&methods_ok); | 172 | { |
174 | } | 173 | return (&methods_ok); |
174 | } | ||
175 | 175 | ||
176 | static int ok_new(BIO *bi) | 176 | static int |
177 | { | 177 | ok_new(BIO *bi) |
178 | { | ||
178 | BIO_OK_CTX *ctx; | 179 | BIO_OK_CTX *ctx; |
179 | 180 | ||
180 | ctx=(BIO_OK_CTX *)malloc(sizeof(BIO_OK_CTX)); | 181 | ctx = (BIO_OK_CTX *)malloc(sizeof(BIO_OK_CTX)); |
181 | if (ctx == NULL) return(0); | 182 | if (ctx == NULL) |
183 | return (0); | ||
182 | 184 | ||
183 | ctx->buf_len=0; | 185 | ctx->buf_len = 0; |
184 | ctx->buf_off=0; | 186 | ctx->buf_off = 0; |
185 | ctx->buf_len_save=0; | 187 | ctx->buf_len_save = 0; |
186 | ctx->buf_off_save=0; | 188 | ctx->buf_off_save = 0; |
187 | ctx->cont=1; | 189 | ctx->cont = 1; |
188 | ctx->finished=0; | 190 | ctx->finished = 0; |
189 | ctx->blockout= 0; | 191 | ctx->blockout = 0; |
190 | ctx->sigio=1; | 192 | ctx->sigio = 1; |
191 | 193 | ||
192 | EVP_MD_CTX_init(&ctx->md); | 194 | EVP_MD_CTX_init(&ctx->md); |
193 | 195 | ||
194 | bi->init=0; | 196 | bi->init = 0; |
195 | bi->ptr=(char *)ctx; | 197 | bi->ptr = (char *)ctx; |
196 | bi->flags=0; | 198 | bi->flags = 0; |
197 | return(1); | 199 | return (1); |
198 | } | 200 | } |
199 | 201 | ||
200 | static int ok_free(BIO *a) | 202 | static int |
201 | { | 203 | ok_free(BIO *a) |
202 | if (a == NULL) return(0); | 204 | { |
205 | if (a == NULL) | ||
206 | return (0); | ||
203 | EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md); | 207 | EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md); |
204 | OPENSSL_cleanse(a->ptr,sizeof(BIO_OK_CTX)); | 208 | OPENSSL_cleanse(a->ptr, sizeof(BIO_OK_CTX)); |
205 | free(a->ptr); | 209 | free(a->ptr); |
206 | a->ptr=NULL; | 210 | a->ptr = NULL; |
207 | a->init=0; | 211 | a->init = 0; |
208 | a->flags=0; | 212 | a->flags = 0; |
209 | return(1); | 213 | return (1); |
210 | } | 214 | } |
211 | 215 | ||
212 | static int ok_read(BIO *b, char *out, int outl) | 216 | static int |
213 | { | 217 | ok_read(BIO *b, char *out, int outl) |
214 | int ret=0,i,n; | 218 | { |
219 | int ret = 0, i, n; | ||
215 | BIO_OK_CTX *ctx; | 220 | BIO_OK_CTX *ctx; |
216 | 221 | ||
217 | if (out == NULL) return(0); | 222 | if (out == NULL) |
218 | ctx=(BIO_OK_CTX *)b->ptr; | 223 | return (0); |
224 | ctx = (BIO_OK_CTX *)b->ptr; | ||
219 | 225 | ||
220 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0); | 226 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) |
227 | return (0); | ||
221 | 228 | ||
222 | while(outl > 0) | 229 | while (outl > 0) { |
223 | { | ||
224 | 230 | ||
225 | /* copy clean bytes to output buffer */ | 231 | /* copy clean bytes to output buffer */ |
226 | if (ctx->blockout) | 232 | if (ctx->blockout) { |
227 | { | 233 | i = ctx->buf_len - ctx->buf_off; |
228 | i=ctx->buf_len-ctx->buf_off; | 234 | if (i > outl) |
229 | if (i > outl) i=outl; | 235 | i = outl; |
230 | memcpy(out,&(ctx->buf[ctx->buf_off]),i); | 236 | memcpy(out, &(ctx->buf[ctx->buf_off]), i); |
231 | ret+=i; | 237 | ret += i; |
232 | out+=i; | 238 | out += i; |
233 | outl-=i; | 239 | outl -= i; |
234 | ctx->buf_off+=i; | 240 | ctx->buf_off += i; |
235 | 241 | ||
236 | /* all clean bytes are out */ | 242 | /* all clean bytes are out */ |
237 | if (ctx->buf_len == ctx->buf_off) | 243 | if (ctx->buf_len == ctx->buf_off) { |
238 | { | 244 | ctx->buf_off = 0; |
239 | ctx->buf_off=0; | ||
240 | 245 | ||
241 | /* copy start of the next block into proper place */ | 246 | /* copy start of the next block into proper place */ |
242 | if(ctx->buf_len_save- ctx->buf_off_save > 0) | 247 | if (ctx->buf_len_save - ctx->buf_off_save > 0) { |
243 | { | 248 | ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save; |
244 | ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save; | ||
245 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]), | 249 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]), |
246 | ctx->buf_len); | 250 | ctx->buf_len); |
247 | } | 251 | } else { |
248 | else | 252 | ctx->buf_len = 0; |
249 | { | ||
250 | ctx->buf_len=0; | ||
251 | } | ||
252 | ctx->blockout= 0; | ||
253 | } | 253 | } |
254 | ctx->blockout = 0; | ||
254 | } | 255 | } |
255 | 256 | } | |
257 | |||
256 | /* output buffer full -- cancel */ | 258 | /* output buffer full -- cancel */ |
257 | if (outl == 0) break; | 259 | if (outl == 0) |
260 | break; | ||
258 | 261 | ||
259 | /* no clean bytes in buffer -- fill it */ | 262 | /* no clean bytes in buffer -- fill it */ |
260 | n=IOBS- ctx->buf_len; | 263 | n = IOBS - ctx->buf_len; |
261 | i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n); | 264 | i = BIO_read(b->next_bio, &(ctx->buf[ctx->buf_len]), n); |
262 | 265 | ||
263 | if (i <= 0) break; /* nothing new */ | 266 | if (i <= 0) break; /* nothing new */ |
264 | 267 | ||
265 | ctx->buf_len+= i; | 268 | ctx->buf_len += i; |
266 | 269 | ||
267 | /* no signature yet -- check if we got one */ | 270 | /* no signature yet -- check if we got one */ |
268 | if (ctx->sigio == 1) | 271 | if (ctx->sigio == 1) { |
269 | { | 272 | if (!sig_in(b)) { |
270 | if (!sig_in(b)) | ||
271 | { | ||
272 | BIO_clear_retry_flags(b); | 273 | BIO_clear_retry_flags(b); |
273 | return 0; | 274 | return 0; |
274 | } | ||
275 | } | 275 | } |
276 | } | ||
276 | 277 | ||
277 | /* signature ok -- check if we got block */ | 278 | /* signature ok -- check if we got block */ |
278 | if (ctx->sigio == 0) | 279 | if (ctx->sigio == 0) { |
279 | { | 280 | if (!block_in(b)) { |
280 | if (!block_in(b)) | ||
281 | { | ||
282 | BIO_clear_retry_flags(b); | 281 | BIO_clear_retry_flags(b); |
283 | return 0; | 282 | return 0; |
284 | } | ||
285 | } | 283 | } |
284 | } | ||
286 | 285 | ||
287 | /* invalid block -- cancel */ | 286 | /* invalid block -- cancel */ |
288 | if (ctx->cont <= 0) break; | 287 | if (ctx->cont <= 0) |
288 | break; | ||
289 | 289 | ||
290 | } | 290 | } |
291 | 291 | ||
292 | BIO_clear_retry_flags(b); | 292 | BIO_clear_retry_flags(b); |
293 | BIO_copy_next_retry(b); | 293 | BIO_copy_next_retry(b); |
294 | return(ret); | 294 | return (ret); |
295 | } | 295 | } |
296 | 296 | ||
297 | static int ok_write(BIO *b, const char *in, int inl) | 297 | static int |
298 | { | 298 | ok_write(BIO *b, const char *in, int inl) |
299 | int ret=0,n,i; | 299 | { |
300 | int ret = 0, n, i; | ||
300 | BIO_OK_CTX *ctx; | 301 | BIO_OK_CTX *ctx; |
301 | 302 | ||
302 | if (inl <= 0) return inl; | 303 | if (inl <= 0) |
304 | return inl; | ||
303 | 305 | ||
304 | ctx=(BIO_OK_CTX *)b->ptr; | 306 | ctx = (BIO_OK_CTX *)b->ptr; |
305 | ret=inl; | 307 | ret = inl; |
306 | 308 | ||
307 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0); | 309 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) |
310 | return (0); | ||
308 | 311 | ||
309 | if(ctx->sigio && !sig_out(b)) | 312 | if (ctx->sigio && !sig_out(b)) |
310 | return 0; | 313 | return 0; |
311 | 314 | ||
312 | do{ | 315 | do { |
313 | BIO_clear_retry_flags(b); | 316 | BIO_clear_retry_flags(b); |
314 | n=ctx->buf_len-ctx->buf_off; | 317 | n = ctx->buf_len - ctx->buf_off; |
315 | while (ctx->blockout && n > 0) | 318 | while (ctx->blockout && n > 0) { |
316 | { | 319 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); |
317 | i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); | 320 | if (i <= 0) { |
318 | if (i <= 0) | ||
319 | { | ||
320 | BIO_copy_next_retry(b); | 321 | BIO_copy_next_retry(b); |
321 | if(!BIO_should_retry(b)) | 322 | if (!BIO_should_retry(b)) |
322 | ctx->cont= 0; | 323 | ctx->cont = 0; |
323 | return(i); | 324 | return (i); |
324 | } | ||
325 | ctx->buf_off+=i; | ||
326 | n-=i; | ||
327 | } | 325 | } |
326 | ctx->buf_off += i; | ||
327 | n -= i; | ||
328 | } | ||
328 | 329 | ||
329 | /* at this point all pending data has been written */ | 330 | /* at this point all pending data has been written */ |
330 | ctx->blockout= 0; | 331 | ctx->blockout = 0; |
331 | if (ctx->buf_len == ctx->buf_off) | 332 | if (ctx->buf_len == ctx->buf_off) { |
332 | { | 333 | ctx->buf_len = OK_BLOCK_BLOCK; |
333 | ctx->buf_len=OK_BLOCK_BLOCK; | 334 | ctx->buf_off = 0; |
334 | ctx->buf_off=0; | 335 | } |
335 | } | ||
336 | |||
337 | if ((in == NULL) || (inl <= 0)) return(0); | ||
338 | 336 | ||
339 | n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ? | 337 | if ((in == NULL) || (inl <= 0)) |
340 | (int)(OK_BLOCK_SIZE+OK_BLOCK_BLOCK-ctx->buf_len) : inl; | 338 | return (0); |
341 | 339 | ||
342 | memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n); | 340 | n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ? |
343 | ctx->buf_len+= n; | 341 | (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl; |
344 | inl-=n; | ||
345 | in+=n; | ||
346 | 342 | ||
347 | if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) | 343 | memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])), |
348 | { | 344 | (unsigned char *)in, n); |
349 | if (!block_out(b)) | 345 | ctx->buf_len += n; |
350 | { | 346 | inl -= n; |
347 | in += n; | ||
348 | |||
349 | if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) { | ||
350 | if (!block_out(b)) { | ||
351 | BIO_clear_retry_flags(b); | 351 | BIO_clear_retry_flags(b); |
352 | return 0; | 352 | return 0; |
353 | } | ||
354 | } | 353 | } |
355 | }while(inl > 0); | 354 | } |
355 | } while (inl > 0); | ||
356 | 356 | ||
357 | BIO_clear_retry_flags(b); | 357 | BIO_clear_retry_flags(b); |
358 | BIO_copy_next_retry(b); | 358 | BIO_copy_next_retry(b); |
359 | return(ret); | 359 | return (ret); |
360 | } | 360 | } |
361 | 361 | ||
362 | static long ok_ctrl(BIO *b, int cmd, long num, void *ptr) | 362 | static long |
363 | { | 363 | ok_ctrl(BIO *b, int cmd, long num, void *ptr) |
364 | { | ||
364 | BIO_OK_CTX *ctx; | 365 | BIO_OK_CTX *ctx; |
365 | EVP_MD *md; | 366 | EVP_MD *md; |
366 | const EVP_MD **ppmd; | 367 | const EVP_MD **ppmd; |
367 | long ret=1; | 368 | long ret = 1; |
368 | int i; | 369 | int i; |
369 | 370 | ||
370 | ctx=b->ptr; | 371 | ctx = b->ptr; |
371 | 372 | ||
372 | switch (cmd) | 373 | switch (cmd) { |
373 | { | ||
374 | case BIO_CTRL_RESET: | 374 | case BIO_CTRL_RESET: |
375 | ctx->buf_len=0; | 375 | ctx->buf_len = 0; |
376 | ctx->buf_off=0; | 376 | ctx->buf_off = 0; |
377 | ctx->buf_len_save=0; | 377 | ctx->buf_len_save = 0; |
378 | ctx->buf_off_save=0; | 378 | ctx->buf_off_save = 0; |
379 | ctx->cont=1; | 379 | ctx->cont = 1; |
380 | ctx->finished=0; | 380 | ctx->finished = 0; |
381 | ctx->blockout= 0; | 381 | ctx->blockout = 0; |
382 | ctx->sigio=1; | 382 | ctx->sigio = 1; |
383 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 383 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
384 | break; | 384 | break; |
385 | case BIO_CTRL_EOF: /* More to read */ | 385 | case BIO_CTRL_EOF: /* More to read */ |
386 | if (ctx->cont <= 0) | 386 | if (ctx->cont <= 0) |
387 | ret=1; | 387 | ret = 1; |
388 | else | 388 | else |
389 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 389 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
390 | break; | 390 | break; |
391 | case BIO_CTRL_PENDING: /* More to read in buffer */ | 391 | case BIO_CTRL_PENDING: /* More to read in buffer */ |
392 | case BIO_CTRL_WPENDING: /* More to read in buffer */ | 392 | case BIO_CTRL_WPENDING: /* More to read in buffer */ |
393 | ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0; | 393 | ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0; |
394 | if (ret <= 0) | 394 | if (ret <= 0) |
395 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 395 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
396 | break; | 396 | break; |
397 | case BIO_CTRL_FLUSH: | 397 | case BIO_CTRL_FLUSH: |
398 | /* do a final write */ | 398 | /* do a final write */ |
399 | if(ctx->blockout == 0) | 399 | if (ctx->blockout == 0) |
400 | if (!block_out(b)) | 400 | if (!block_out(b)) |
401 | return 0; | 401 | return 0; |
402 | 402 | ||
403 | while (ctx->blockout) | 403 | while (ctx->blockout) { |
404 | { | 404 | i = ok_write(b, NULL, 0); |
405 | i=ok_write(b,NULL,0); | 405 | if (i < 0) { |
406 | if (i < 0) | 406 | ret = i; |
407 | { | ||
408 | ret=i; | ||
409 | break; | 407 | break; |
410 | } | ||
411 | } | 408 | } |
409 | } | ||
410 | |||
411 | ctx->finished = 1; | ||
412 | ctx->buf_off = ctx->buf_len = 0; | ||
413 | ctx->cont = (int)ret; | ||
412 | 414 | ||
413 | ctx->finished=1; | ||
414 | ctx->buf_off=ctx->buf_len=0; | ||
415 | ctx->cont=(int)ret; | ||
416 | |||
417 | /* Finally flush the underlying BIO */ | 415 | /* Finally flush the underlying BIO */ |
418 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 416 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
419 | break; | 417 | break; |
420 | case BIO_C_DO_STATE_MACHINE: | 418 | case BIO_C_DO_STATE_MACHINE: |
421 | BIO_clear_retry_flags(b); | 419 | BIO_clear_retry_flags(b); |
422 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 420 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
423 | BIO_copy_next_retry(b); | 421 | BIO_copy_next_retry(b); |
424 | break; | 422 | break; |
425 | case BIO_CTRL_INFO: | 423 | case BIO_CTRL_INFO: |
426 | ret=(long)ctx->cont; | 424 | ret = (long)ctx->cont; |
427 | break; | 425 | break; |
428 | case BIO_C_SET_MD: | 426 | case BIO_C_SET_MD: |
429 | md=ptr; | 427 | md = ptr; |
430 | if (!EVP_DigestInit_ex(&ctx->md, md, NULL)) | 428 | if (!EVP_DigestInit_ex(&ctx->md, md, NULL)) |
431 | return 0; | 429 | return 0; |
432 | b->init=1; | 430 | b->init = 1; |
433 | break; | 431 | break; |
434 | case BIO_C_GET_MD: | 432 | case BIO_C_GET_MD: |
435 | if (b->init) | 433 | if (b->init) { |
436 | { | 434 | ppmd = ptr; |
437 | ppmd=ptr; | 435 | *ppmd = ctx->md.digest; |
438 | *ppmd=ctx->md.digest; | 436 | } else |
439 | } | 437 | ret = 0; |
440 | else | ||
441 | ret=0; | ||
442 | break; | 438 | break; |
443 | default: | 439 | default: |
444 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 440 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
445 | break; | 441 | break; |
446 | } | ||
447 | return(ret); | ||
448 | } | 442 | } |
443 | return (ret); | ||
444 | } | ||
449 | 445 | ||
450 | static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | 446 | static long |
451 | { | 447 | ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) |
452 | long ret=1; | 448 | { |
449 | long ret = 1; | ||
453 | 450 | ||
454 | if (b->next_bio == NULL) return(0); | 451 | if (b->next_bio == NULL) |
455 | switch (cmd) | 452 | return (0); |
456 | { | 453 | switch (cmd) { |
457 | default: | 454 | default: |
458 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | 455 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); |
459 | break; | 456 | break; |
460 | } | ||
461 | return(ret); | ||
462 | } | 457 | } |
458 | return (ret); | ||
459 | } | ||
463 | 460 | ||
464 | static void longswap(void *_ptr, size_t len) | 461 | static void |
462 | longswap(void *_ptr, size_t len) | ||
465 | { | 463 | { |
466 | if (_BYTE_ORDER == _LITTLE_ENDIAN) { | 464 | if (_BYTE_ORDER == _LITTLE_ENDIAN) { |
467 | size_t i; | 465 | size_t i; |
468 | unsigned char *p=_ptr,c; | 466 | unsigned char *p = _ptr, c; |
469 | 467 | ||
470 | for(i= 0;i < len;i+= 4) { | 468 | for (i = 0; i < len; i += 4) { |
471 | c=p[0],p[0]=p[3],p[3]=c; | 469 | c = p[0], p[0] = p[3], p[3] = c; |
472 | c=p[1],p[1]=p[2],p[2]=c; | 470 | c = p[1], p[1] = p[2], p[2] = c; |
473 | } | 471 | } |
474 | } | 472 | } |
475 | } | 473 | } |
476 | 474 | ||
477 | static int sig_out(BIO* b) | 475 | static int |
478 | { | 476 | sig_out(BIO* b) |
477 | { | ||
479 | BIO_OK_CTX *ctx; | 478 | BIO_OK_CTX *ctx; |
480 | EVP_MD_CTX *md; | 479 | EVP_MD_CTX *md; |
481 | 480 | ||
482 | ctx=b->ptr; | 481 | ctx = b->ptr; |
483 | md=&ctx->md; | 482 | md = &ctx->md; |
484 | 483 | ||
485 | if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return 1; | 484 | if (ctx->buf_len + 2* md->digest->md_size > OK_BLOCK_SIZE) |
485 | return 1; | ||
486 | 486 | ||
487 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) | 487 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) |
488 | goto berr; | 488 | goto berr; |
@@ -492,131 +492,134 @@ static int sig_out(BIO* b) | |||
492 | RAND_pseudo_bytes(md->md_data, md->digest->md_size); | 492 | RAND_pseudo_bytes(md->md_data, md->digest->md_size); |
493 | memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size); | 493 | memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size); |
494 | longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size); | 494 | longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size); |
495 | ctx->buf_len+= md->digest->md_size; | 495 | ctx->buf_len += md->digest->md_size; |
496 | 496 | ||
497 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) | 497 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) |
498 | goto berr; | 498 | goto berr; |
499 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) | 499 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) |
500 | goto berr; | 500 | goto berr; |
501 | ctx->buf_len+= md->digest->md_size; | 501 | ctx->buf_len += md->digest->md_size; |
502 | ctx->blockout= 1; | 502 | ctx->blockout = 1; |
503 | ctx->sigio= 0; | 503 | ctx->sigio = 0; |
504 | return 1; | 504 | return 1; |
505 | berr: | 505 | berr: |
506 | BIO_clear_retry_flags(b); | 506 | BIO_clear_retry_flags(b); |
507 | return 0; | 507 | return 0; |
508 | } | 508 | } |
509 | 509 | ||
510 | static int sig_in(BIO* b) | 510 | static int |
511 | { | 511 | sig_in(BIO* b) |
512 | { | ||
512 | BIO_OK_CTX *ctx; | 513 | BIO_OK_CTX *ctx; |
513 | EVP_MD_CTX *md; | 514 | EVP_MD_CTX *md; |
514 | unsigned char tmp[EVP_MAX_MD_SIZE]; | 515 | unsigned char tmp[EVP_MAX_MD_SIZE]; |
515 | int ret= 0; | 516 | int ret = 0; |
516 | 517 | ||
517 | ctx=b->ptr; | 518 | ctx = b->ptr; |
518 | md=&ctx->md; | 519 | md = &ctx->md; |
519 | 520 | ||
520 | if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return 1; | 521 | if ((int)(ctx->buf_len - ctx->buf_off) < 2*md->digest->md_size) |
522 | return 1; | ||
521 | 523 | ||
522 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) | 524 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) |
523 | goto berr; | 525 | goto berr; |
524 | memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size); | 526 | memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size); |
525 | longswap(md->md_data, md->digest->md_size); | 527 | longswap(md->md_data, md->digest->md_size); |
526 | ctx->buf_off+= md->digest->md_size; | 528 | ctx->buf_off += md->digest->md_size; |
527 | 529 | ||
528 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) | 530 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) |
529 | goto berr; | 531 | goto berr; |
530 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) | 532 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) |
531 | goto berr; | 533 | goto berr; |
532 | ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0; | 534 | ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0; |
533 | ctx->buf_off+= md->digest->md_size; | 535 | ctx->buf_off += md->digest->md_size; |
534 | if(ret == 1) | 536 | if (ret == 1) { |
535 | { | 537 | ctx->sigio = 0; |
536 | ctx->sigio= 0; | 538 | if (ctx->buf_len != ctx->buf_off) { |
537 | if(ctx->buf_len != ctx->buf_off) | 539 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), |
538 | { | 540 | ctx->buf_len - ctx->buf_off); |
539 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off); | ||
540 | } | ||
541 | ctx->buf_len-= ctx->buf_off; | ||
542 | ctx->buf_off= 0; | ||
543 | } | ||
544 | else | ||
545 | { | ||
546 | ctx->cont= 0; | ||
547 | } | 541 | } |
542 | ctx->buf_len -= ctx->buf_off; | ||
543 | ctx->buf_off = 0; | ||
544 | } else { | ||
545 | ctx->cont = 0; | ||
546 | } | ||
548 | return 1; | 547 | return 1; |
549 | berr: | 548 | berr: |
550 | BIO_clear_retry_flags(b); | 549 | BIO_clear_retry_flags(b); |
551 | return 0; | 550 | return 0; |
552 | } | 551 | } |
553 | 552 | ||
554 | static int block_out(BIO* b) | 553 | static int |
555 | { | 554 | block_out(BIO* b) |
555 | { | ||
556 | BIO_OK_CTX *ctx; | 556 | BIO_OK_CTX *ctx; |
557 | EVP_MD_CTX *md; | 557 | EVP_MD_CTX *md; |
558 | unsigned long tl; | 558 | unsigned long tl; |
559 | 559 | ||
560 | ctx=b->ptr; | 560 | ctx = b->ptr; |
561 | md=&ctx->md; | 561 | md = &ctx->md; |
562 | 562 | ||
563 | tl= ctx->buf_len- OK_BLOCK_BLOCK; | 563 | tl = ctx->buf_len - OK_BLOCK_BLOCK; |
564 | ctx->buf[0]=(unsigned char)(tl>>24); | 564 | ctx->buf[0] = (unsigned char)(tl >> 24); |
565 | ctx->buf[1]=(unsigned char)(tl>>16); | 565 | ctx->buf[1] = (unsigned char)(tl >> 16); |
566 | ctx->buf[2]=(unsigned char)(tl>>8); | 566 | ctx->buf[2] = (unsigned char)(tl >> 8); |
567 | ctx->buf[3]=(unsigned char)(tl); | 567 | ctx->buf[3] = (unsigned char)(tl); |
568 | if (!EVP_DigestUpdate(md, | 568 | if (!EVP_DigestUpdate(md, |
569 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) | 569 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) |
570 | goto berr; | 570 | goto berr; |
571 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) | 571 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) |
572 | goto berr; | 572 | goto berr; |
573 | ctx->buf_len+= md->digest->md_size; | 573 | ctx->buf_len += md->digest->md_size; |
574 | ctx->blockout= 1; | 574 | ctx->blockout = 1; |
575 | return 1; | 575 | return 1; |
576 | berr: | 576 | berr: |
577 | BIO_clear_retry_flags(b); | 577 | BIO_clear_retry_flags(b); |
578 | return 0; | 578 | return 0; |
579 | } | 579 | } |
580 | 580 | ||
581 | static int block_in(BIO* b) | 581 | static int |
582 | { | 582 | block_in(BIO* b) |
583 | { | ||
583 | BIO_OK_CTX *ctx; | 584 | BIO_OK_CTX *ctx; |
584 | EVP_MD_CTX *md; | 585 | EVP_MD_CTX *md; |
585 | unsigned long tl= 0; | 586 | unsigned long tl = 0; |
586 | unsigned char tmp[EVP_MAX_MD_SIZE]; | 587 | unsigned char tmp[EVP_MAX_MD_SIZE]; |
587 | 588 | ||
588 | ctx=b->ptr; | 589 | ctx = b->ptr; |
589 | md=&ctx->md; | 590 | md = &ctx->md; |
590 | 591 | ||
591 | assert(sizeof(tl)>=OK_BLOCK_BLOCK); /* always true */ | 592 | assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */ |
592 | tl =ctx->buf[0]; tl<<=8; | 593 | tl = ctx->buf[0]; |
593 | tl|=ctx->buf[1]; tl<<=8; | 594 | tl <<= 8; |
594 | tl|=ctx->buf[2]; tl<<=8; | 595 | tl |= ctx->buf[1]; |
595 | tl|=ctx->buf[3]; | 596 | tl <<= 8; |
597 | tl |= ctx->buf[2]; | ||
598 | tl <<= 8; | ||
599 | tl |= ctx->buf[3]; | ||
600 | |||
601 | if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md->digest->md_size) | ||
602 | return 1; | ||
596 | 603 | ||
597 | if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return 1; | ||
598 | |||
599 | if (!EVP_DigestUpdate(md, | 604 | if (!EVP_DigestUpdate(md, |
600 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) | 605 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) |
601 | goto berr; | 606 | goto berr; |
602 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) | 607 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) |
603 | goto berr; | 608 | goto berr; |
604 | if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0) | 609 | if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, |
605 | { | 610 | md->digest->md_size) == 0) { |
606 | /* there might be parts from next block lurking around ! */ | 611 | /* there might be parts from next block lurking around ! */ |
607 | ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size; | 612 | ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md->digest->md_size; |
608 | ctx->buf_len_save= ctx->buf_len; | 613 | ctx->buf_len_save = ctx->buf_len; |
609 | ctx->buf_off= OK_BLOCK_BLOCK; | 614 | ctx->buf_off = OK_BLOCK_BLOCK; |
610 | ctx->buf_len= tl+ OK_BLOCK_BLOCK; | 615 | ctx->buf_len = tl + OK_BLOCK_BLOCK; |
611 | ctx->blockout= 1; | 616 | ctx->blockout = 1; |
612 | } | 617 | } else { |
613 | else | 618 | ctx->cont = 0; |
614 | { | 619 | } |
615 | ctx->cont= 0; | ||
616 | } | ||
617 | return 1; | 620 | return 1; |
618 | berr: | 621 | |
622 | berr: | ||
619 | BIO_clear_retry_flags(b); | 623 | BIO_clear_retry_flags(b); |
620 | return 0; | 624 | return 0; |
621 | } | 625 | } |
622 | |||