summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2014-05-03 16:22:28 +0000
committerjsing <>2014-05-03 16:22:28 +0000
commit65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8 (patch)
tree4993b7660557e78f309731f46f029f969f974e84
parent0225d59faf0ea827794d5d88eb3e1dddb205242c (diff)
downloadopenbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.tar.gz
openbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.tar.bz2
openbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.zip
KNF.
-rw-r--r--src/lib/libcrypto/evp/bio_b64.c618
-rw-r--r--src/lib/libcrypto/evp/bio_enc.c412
-rw-r--r--src/lib/libcrypto/evp/bio_md.c231
-rw-r--r--src/lib/libcrypto/evp/bio_ok.c581
-rw-r--r--src/lib/libssl/src/crypto/evp/bio_b64.c618
-rw-r--r--src/lib/libssl/src/crypto/evp/bio_enc.c412
-rw-r--r--src/lib/libssl/src/crypto/evp/bio_md.c231
-rw-r--r--src/lib/libssl/src/crypto/evp/bio_ok.c581
8 files changed, 1820 insertions, 1864 deletions
diff --git a/src/lib/libcrypto/evp/bio_b64.c b/src/lib/libcrypto/evp/bio_b64.c
index 02631ec05a..f963b606d2 100644
--- a/src/lib/libcrypto/evp/bio_b64.c
+++ b/src/lib/libcrypto/evp/bio_b64.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
@@ -69,15 +69,14 @@ static int b64_puts(BIO *h, const char *str);
69static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2); 69static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
70static int b64_new(BIO *h); 70static int b64_new(BIO *h);
71static int b64_free(BIO *data); 71static int b64_free(BIO *data);
72static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); 72static long b64_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
73#define B64_BLOCK_SIZE 1024 73#define B64_BLOCK_SIZE 1024
74#define B64_BLOCK_SIZE2 768 74#define B64_BLOCK_SIZE2 768
75#define B64_NONE 0 75#define B64_NONE 0
76#define B64_ENCODE 1 76#define B64_ENCODE 1
77#define B64_DECODE 2 77#define B64_DECODE 2
78 78
79typedef struct b64_struct 79typedef struct b64_struct {
80 {
81 /*BIO *bio; moved to the BIO structure */ 80 /*BIO *bio; moved to the BIO structure */
82 int buf_len; 81 int buf_len;
83 int buf_off; 82 int buf_off;
@@ -87,11 +86,11 @@ typedef struct b64_struct
87 int start; /* have we started decoding yet? */ 86 int start; /* have we started decoding yet? */
88 int cont; /* <= 0 when finished */ 87 int cont; /* <= 0 when finished */
89 EVP_ENCODE_CTX base64; 88 EVP_ENCODE_CTX base64;
90 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10]; 89 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
91 char tmp[B64_BLOCK_SIZE]; 90 char tmp[B64_BLOCK_SIZE];
92 } BIO_B64_CTX; 91} BIO_B64_CTX;
93 92
94static BIO_METHOD methods_b64= { 93static BIO_METHOD methods_b64 = {
95 .type = BIO_TYPE_BASE64, 94 .type = BIO_TYPE_BASE64,
96 .name = "base64 encoding", 95 .name = "base64 encoding",
97 .bwrite = b64_write, 96 .bwrite = b64_write,
@@ -103,465 +102,436 @@ static BIO_METHOD methods_b64= {
103 .callback_ctrl = b64_callback_ctrl 102 .callback_ctrl = b64_callback_ctrl
104}; 103};
105 104
106BIO_METHOD *BIO_f_base64(void) 105BIO_METHOD *
107 { 106BIO_f_base64(void)
108 return(&methods_b64); 107{
109 } 108 return (&methods_b64);
109}
110 110
111static int b64_new(BIO *bi) 111static int
112 { 112b64_new(BIO *bi)
113{
113 BIO_B64_CTX *ctx; 114 BIO_B64_CTX *ctx;
114 115
115 ctx=(BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX)); 116 ctx = (BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX));
116 if (ctx == NULL) return(0); 117 if (ctx == NULL)
117 118 return (0);
118 ctx->buf_len=0; 119
119 ctx->tmp_len=0; 120 ctx->buf_len = 0;
120 ctx->tmp_nl=0; 121 ctx->tmp_len = 0;
121 ctx->buf_off=0; 122 ctx->tmp_nl = 0;
122 ctx->cont=1; 123 ctx->buf_off = 0;
123 ctx->start=1; 124 ctx->cont = 1;
124 ctx->encode=0; 125 ctx->start = 1;
125 126 ctx->encode = 0;
126 bi->init=1; 127
127 bi->ptr=(char *)ctx; 128 bi->init = 1;
128 bi->flags=0; 129 bi->ptr = (char *)ctx;
130 bi->flags = 0;
129 bi->num = 0; 131 bi->num = 0;
130 return(1); 132 return (1);
131 } 133}
132 134
133static int b64_free(BIO *a) 135static int
134 { 136b64_free(BIO *a)
135 if (a == NULL) return(0); 137{
138 if (a == NULL)
139 return (0);
136 free(a->ptr); 140 free(a->ptr);
137 a->ptr=NULL; 141 a->ptr = NULL;
138 a->init=0; 142 a->init = 0;
139 a->flags=0; 143 a->flags = 0;
140 return(1); 144 return (1);
141 } 145}
142 146
143static int b64_read(BIO *b, char *out, int outl) 147static int
144 { 148b64_read(BIO *b, char *out, int outl)
145 int ret=0,i,ii,j,k,x,n,num,ret_code=0; 149{
150 int ret = 0, i, ii, j,k, x,n, num, ret_code = 0;
146 BIO_B64_CTX *ctx; 151 BIO_B64_CTX *ctx;
147 unsigned char *p,*q; 152 unsigned char *p, *q;
148 153
149 if (out == NULL) return(0); 154 if (out == NULL)
150 ctx=(BIO_B64_CTX *)b->ptr; 155 return (0);
156 ctx = (BIO_B64_CTX *)b->ptr;
151 157
152 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 158 if ((ctx == NULL) || (b->next_bio == NULL))
159 return (0);
153 160
154 BIO_clear_retry_flags(b); 161 BIO_clear_retry_flags(b);
155 162
156 if (ctx->encode != B64_DECODE) 163 if (ctx->encode != B64_DECODE) {
157 { 164 ctx->encode = B64_DECODE;
158 ctx->encode=B64_DECODE; 165 ctx->buf_len = 0;
159 ctx->buf_len=0; 166 ctx->buf_off = 0;
160 ctx->buf_off=0; 167 ctx->tmp_len = 0;
161 ctx->tmp_len=0;
162 EVP_DecodeInit(&(ctx->base64)); 168 EVP_DecodeInit(&(ctx->base64));
163 } 169 }
164 170
165 /* First check if there are bytes decoded/encoded */ 171 /* First check if there are bytes decoded/encoded */
166 if (ctx->buf_len > 0) 172 if (ctx->buf_len > 0) {
167 {
168 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 173 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
169 i=ctx->buf_len-ctx->buf_off; 174 i = ctx->buf_len - ctx->buf_off;
170 if (i > outl) i=outl; 175 if (i > outl)
171 OPENSSL_assert(ctx->buf_off+i < (int)sizeof(ctx->buf)); 176 i = outl;
172 memcpy(out,&(ctx->buf[ctx->buf_off]),i); 177 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
173 ret=i; 178 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
174 out+=i; 179 ret = i;
175 outl-=i; 180 out += i;
176 ctx->buf_off+=i; 181 outl -= i;
177 if (ctx->buf_len == ctx->buf_off) 182 ctx->buf_off += i;
178 { 183 if (ctx->buf_len == ctx->buf_off) {
179 ctx->buf_len=0; 184 ctx->buf_len = 0;
180 ctx->buf_off=0; 185 ctx->buf_off = 0;
181 }
182 } 186 }
187 }
183 188
184 /* At this point, we have room of outl bytes and an empty 189 /* At this point, we have room of outl bytes and an empty
185 * buffer, so we should read in some more. */ 190 * buffer, so we should read in some more. */
186 191
187 ret_code=0; 192 ret_code = 0;
188 while (outl > 0) 193 while (outl > 0) {
189 {
190 if (ctx->cont <= 0) 194 if (ctx->cont <= 0)
191 break; 195 break;
192 196
193 i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]), 197 i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]),
194 B64_BLOCK_SIZE-ctx->tmp_len); 198 B64_BLOCK_SIZE - ctx->tmp_len);
195 199
196 if (i <= 0) 200 if (i <= 0) {
197 { 201 ret_code = i;
198 ret_code=i;
199 202
200 /* Should we continue next time we are called? */ 203 /* Should we continue next time we are called? */
201 if (!BIO_should_retry(b->next_bio)) 204 if (!BIO_should_retry(b->next_bio)) {
202 { 205 ctx->cont = i;
203 ctx->cont=i;
204 /* If buffer empty break */ 206 /* If buffer empty break */
205 if(ctx->tmp_len == 0) 207 if (ctx->tmp_len == 0)
206 break; 208 break;
207 /* Fall through and process what we have */ 209 /* Fall through and process what we have */
208 else 210 else
209 i = 0; 211 i = 0;
210 } 212 }
211 /* else we retry and add more data to buffer */ 213 /* else we retry and add more data to buffer */
212 else 214 else
213 break; 215 break;
214 } 216 }
215 i+=ctx->tmp_len; 217 i += ctx->tmp_len;
216 ctx->tmp_len = i; 218 ctx->tmp_len = i;
217 219
218 /* We need to scan, a line at a time until we 220 /* We need to scan, a line at a time until we
219 * have a valid line if we are starting. */ 221 * have a valid line if we are starting. */
220 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) 222 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
221 {
222 /* ctx->start=1; */ 223 /* ctx->start=1; */
223 ctx->tmp_len=0; 224 ctx->tmp_len = 0;
224 } 225 } else if (ctx->start) {
225 else if (ctx->start) 226 q = p=(unsigned char *)ctx->tmp;
226 { 227 for (j = 0; j < i; j++) {
227 q=p=(unsigned char *)ctx->tmp; 228 if (*(q++) != '\n')
228 for (j=0; j<i; j++) 229 continue;
229 {
230 if (*(q++) != '\n') continue;
231 230
232 /* due to a previous very long line, 231 /* due to a previous very long line,
233 * we need to keep on scanning for a '\n' 232 * we need to keep on scanning for a '\n'
234 * before we even start looking for 233 * before we even start looking for
235 * base64 encoded stuff. */ 234 * base64 encoded stuff. */
236 if (ctx->tmp_nl) 235 if (ctx->tmp_nl) {
237 { 236 p = q;
238 p=q; 237 ctx->tmp_nl = 0;
239 ctx->tmp_nl=0;
240 continue; 238 continue;
241 } 239 }
242 240
243 k=EVP_DecodeUpdate(&(ctx->base64), 241 k = EVP_DecodeUpdate(&(ctx->base64),
244 (unsigned char *)ctx->buf, 242 (unsigned char *)ctx->buf,
245 &num,p,q-p); 243 &num, p, q - p);
246 if ((k <= 0) && (num == 0) && (ctx->start)) 244 if ((k <= 0) && (num == 0) && (ctx->start))
247 EVP_DecodeInit(&ctx->base64); 245 EVP_DecodeInit(&ctx->base64);
248 else 246 else {
249 {
250 if (p != (unsigned char *) 247 if (p != (unsigned char *)
251 &(ctx->tmp[0])) 248 &(ctx->tmp[0])) {
252 { 249 i -= (p - (unsigned char *)
253 i-=(p- (unsigned char *) 250 &(ctx->tmp[0]));
254 &(ctx->tmp[0])); 251 for (x = 0; x < i; x++)
255 for (x=0; x < i; x++) 252 ctx->tmp[x] = p[x];
256 ctx->tmp[x]=p[x]; 253 }
257 }
258 EVP_DecodeInit(&ctx->base64); 254 EVP_DecodeInit(&ctx->base64);
259 ctx->start=0; 255 ctx->start = 0;
260 break; 256 break;
261 }
262 p=q;
263 } 257 }
258 p = q;
259 }
264 260
265 /* we fell off the end without starting */ 261 /* we fell off the end without starting */
266 if ((j == i) && (num == 0)) 262 if ((j == i) && (num == 0)) {
267 {
268 /* Is this is one long chunk?, if so, keep on 263 /* Is this is one long chunk?, if so, keep on
269 * reading until a new line. */ 264 * reading until a new line. */
270 if (p == (unsigned char *)&(ctx->tmp[0])) 265 if (p == (unsigned char *)&(ctx->tmp[0])) {
271 {
272 /* Check buffer full */ 266 /* Check buffer full */
273 if (i == B64_BLOCK_SIZE) 267 if (i == B64_BLOCK_SIZE) {
274 { 268 ctx->tmp_nl = 1;
275 ctx->tmp_nl=1; 269 ctx->tmp_len = 0;
276 ctx->tmp_len=0;
277 }
278 } 270 }
271 }
279 else if (p != q) /* finished on a '\n' */ 272 else if (p != q) /* finished on a '\n' */
280 { 273 {
281 n=q-p; 274 n = q - p;
282 for (ii=0; ii<n; ii++) 275 for (ii = 0; ii < n; ii++)
283 ctx->tmp[ii]=p[ii]; 276 ctx->tmp[ii] = p[ii];
284 ctx->tmp_len=n; 277 ctx->tmp_len = n;
285 } 278 }
286 /* else finished on a '\n' */ 279 /* else finished on a '\n' */
287 continue; 280 continue;
288 } 281 } else {
289 else 282 ctx->tmp_len = 0;
290 {
291 ctx->tmp_len=0;
292 } 283 }
293 } 284 } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
294 else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0))
295 {
296 /* If buffer isn't full and we can retry then 285 /* If buffer isn't full and we can retry then
297 * restart to read in more data. 286 * restart to read in more data.
298 */ 287 */
299 continue; 288 continue;
300 } 289 }
301 290
302 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) 291 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
303 { 292 int z, jj;
304 int z,jj;
305 293
306#if 0 294#if 0
307 jj=(i>>2)<<2; 295 jj = (i >> 2) << 2;
308#else 296#else
309 jj = i & ~3; /* process per 4 */ 297 jj = i & ~3; /* process per 4 */
310#endif 298#endif
311 z=EVP_DecodeBlock((unsigned char *)ctx->buf, 299 z = EVP_DecodeBlock((unsigned char *)ctx->buf,
312 (unsigned char *)ctx->tmp,jj); 300 (unsigned char *)ctx->tmp, jj);
313 if (jj > 2) 301 if (jj > 2) {
314 { 302 if (ctx->tmp[jj-1] == '=') {
315 if (ctx->tmp[jj-1] == '=')
316 {
317 z--; 303 z--;
318 if (ctx->tmp[jj-2] == '=') 304 if (ctx->tmp[jj-2] == '=')
319 z--; 305 z--;
320 }
321 } 306 }
307 }
322 /* z is now number of output bytes and jj is the 308 /* z is now number of output bytes and jj is the
323 * number consumed */ 309 * number consumed */
324 if (jj != i) 310 if (jj != i) {
325 { 311 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
326 memmove(ctx->tmp, &ctx->tmp[jj], i-jj); 312 ctx->tmp_len = i - jj;
327 ctx->tmp_len=i-jj;
328 }
329 ctx->buf_len=0;
330 if (z > 0)
331 {
332 ctx->buf_len=z;
333 }
334 i=z;
335 } 313 }
336 else 314 ctx->buf_len = 0;
337 { 315 if (z > 0) {
338 i=EVP_DecodeUpdate(&(ctx->base64), 316 ctx->buf_len = z;
339 (unsigned char *)ctx->buf,&ctx->buf_len,
340 (unsigned char *)ctx->tmp,i);
341 ctx->tmp_len = 0;
342 } 317 }
343 ctx->buf_off=0; 318 i = z;
344 if (i < 0) 319 } else {
345 { 320 i = EVP_DecodeUpdate(&(ctx->base64),
346 ret_code=0; 321 (unsigned char *)ctx->buf, &ctx->buf_len,
347 ctx->buf_len=0; 322 (unsigned char *)ctx->tmp, i);
323 ctx->tmp_len = 0;
324 }
325 ctx->buf_off = 0;
326 if (i < 0) {
327 ret_code = 0;
328 ctx->buf_len = 0;
348 break; 329 break;
349 } 330 }
350 331
351 if (ctx->buf_len <= outl) 332 if (ctx->buf_len <= outl)
352 i=ctx->buf_len; 333 i = ctx->buf_len;
353 else 334 else
354 i=outl; 335 i = outl;
355 336
356 memcpy(out,ctx->buf,i); 337 memcpy(out, ctx->buf, i);
357 ret+=i; 338 ret += i;
358 ctx->buf_off=i; 339 ctx->buf_off = i;
359 if (ctx->buf_off == ctx->buf_len) 340 if (ctx->buf_off == ctx->buf_len) {
360 { 341 ctx->buf_len = 0;
361 ctx->buf_len=0; 342 ctx->buf_off = 0;
362 ctx->buf_off=0;
363 }
364 outl-=i;
365 out+=i;
366 } 343 }
344 outl -= i;
345 out += i;
346 }
367 /* BIO_clear_retry_flags(b); */ 347 /* BIO_clear_retry_flags(b); */
368 BIO_copy_next_retry(b); 348 BIO_copy_next_retry(b);
369 return((ret == 0)?ret_code:ret); 349 return ((ret == 0) ? ret_code : ret);
370 } 350}
371 351
372static int b64_write(BIO *b, const char *in, int inl) 352static int
373 { 353b64_write(BIO *b, const char *in, int inl)
374 int ret=0; 354{
355 int ret = 0;
375 int n; 356 int n;
376 int i; 357 int i;
377 BIO_B64_CTX *ctx; 358 BIO_B64_CTX *ctx;
378 359
379 ctx=(BIO_B64_CTX *)b->ptr; 360 ctx = (BIO_B64_CTX *)b->ptr;
380 BIO_clear_retry_flags(b); 361 BIO_clear_retry_flags(b);
381 362
382 if (ctx->encode != B64_ENCODE) 363 if (ctx->encode != B64_ENCODE) {
383 { 364 ctx->encode = B64_ENCODE;
384 ctx->encode=B64_ENCODE; 365 ctx->buf_len = 0;
385 ctx->buf_len=0; 366 ctx->buf_off = 0;
386 ctx->buf_off=0; 367 ctx->tmp_len = 0;
387 ctx->tmp_len=0;
388 EVP_EncodeInit(&(ctx->base64)); 368 EVP_EncodeInit(&(ctx->base64));
389 } 369 }
390 370
391 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf)); 371 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
392 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 372 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
393 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 373 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
394 n=ctx->buf_len-ctx->buf_off; 374 n = ctx->buf_len - ctx->buf_off;
395 while (n > 0) 375 while (n > 0) {
396 { 376 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
397 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 377 if (i <= 0) {
398 if (i <= 0)
399 {
400 BIO_copy_next_retry(b); 378 BIO_copy_next_retry(b);
401 return(i); 379 return (i);
402 } 380 }
403 OPENSSL_assert(i <= n); 381 OPENSSL_assert(i <= n);
404 ctx->buf_off+=i; 382 ctx->buf_off += i;
405 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf)); 383 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
406 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 384 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
407 n-=i; 385 n -= i;
408 } 386 }
409 /* at this point all pending data has been written */ 387 /* at this point all pending data has been written */
410 ctx->buf_off=0; 388 ctx->buf_off = 0;
411 ctx->buf_len=0; 389 ctx->buf_len = 0;
412 390
413 if ((in == NULL) || (inl <= 0)) return(0); 391 if ((in == NULL) || (inl <= 0))
392 return (0);
414 393
415 while (inl > 0) 394 while (inl > 0) {
416 { 395 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
417 n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
418 396
419 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) 397 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
420 { 398 if (ctx->tmp_len > 0) {
421 if (ctx->tmp_len > 0)
422 {
423 OPENSSL_assert(ctx->tmp_len <= 3); 399 OPENSSL_assert(ctx->tmp_len <= 3);
424 n=3-ctx->tmp_len; 400 n = 3 - ctx->tmp_len;
425 /* There's a theoretical possibility for this */ 401 /* There's a theoretical possibility for this */
426 if (n > inl) 402 if (n > inl)
427 n=inl; 403 n = inl;
428 memcpy(&(ctx->tmp[ctx->tmp_len]),in,n); 404 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
429 ctx->tmp_len+=n; 405 ctx->tmp_len += n;
430 ret += n; 406 ret += n;
431 if (ctx->tmp_len < 3) 407 if (ctx->tmp_len < 3)
432 break; 408 break;
433 ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(unsigned char *)ctx->tmp,ctx->tmp_len); 409 ctx->buf_len = EVP_EncodeBlock(
434 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 410 (unsigned char *)ctx->buf,
411 (unsigned char *)ctx->tmp, ctx->tmp_len);
412 OPENSSL_assert(ctx->buf_len <=
413 (int)sizeof(ctx->buf));
435 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 414 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
436 /* Since we're now done using the temporary 415 /* Since we're now done using the temporary
437 buffer, the length should be 0'd */ 416 buffer, the length should be 0'd */
438 ctx->tmp_len=0; 417 ctx->tmp_len = 0;
439 } 418 } else {
440 else 419 if (n < 3) {
441 { 420 memcpy(ctx->tmp, in, n);
442 if (n < 3) 421 ctx->tmp_len = n;
443 {
444 memcpy(ctx->tmp,in,n);
445 ctx->tmp_len=n;
446 ret += n; 422 ret += n;
447 break; 423 break;
448 } 424 }
449 n-=n%3; 425 n -= n % 3;
450 ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(const unsigned char *)in,n); 426 ctx->buf_len = EVP_EncodeBlock(
451 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 427 (unsigned char *)ctx->buf,
428 (const unsigned char *)in, n);
429 OPENSSL_assert(ctx->buf_len <=
430 (int)sizeof(ctx->buf));
452 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 431 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
453 ret += n; 432 ret += n;
454 }
455 } 433 }
456 else 434 } else {
457 {
458 EVP_EncodeUpdate(&(ctx->base64), 435 EVP_EncodeUpdate(&(ctx->base64),
459 (unsigned char *)ctx->buf,&ctx->buf_len, 436 (unsigned char *)ctx->buf, &ctx->buf_len,
460 (unsigned char *)in,n); 437 (unsigned char *)in, n);
461 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 438 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
462 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 439 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
463 ret += n; 440 ret += n;
464 } 441 }
465 inl-=n; 442 inl -= n;
466 in+=n; 443 in += n;
467 444
468 ctx->buf_off=0; 445 ctx->buf_off = 0;
469 n=ctx->buf_len; 446 n = ctx->buf_len;
470 while (n > 0) 447 while (n > 0) {
471 { 448 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
472 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 449 if (i <= 0) {
473 if (i <= 0)
474 {
475 BIO_copy_next_retry(b); 450 BIO_copy_next_retry(b);
476 return((ret == 0)?i:ret); 451 return ((ret == 0) ? i : ret);
477 } 452 }
478 OPENSSL_assert(i <= n); 453 OPENSSL_assert(i <= n);
479 n-=i; 454 n -= i;
480 ctx->buf_off+=i; 455 ctx->buf_off += i;
481 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf)); 456 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
482 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 457 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
483 }
484 ctx->buf_len=0;
485 ctx->buf_off=0;
486 } 458 }
487 return(ret); 459 ctx->buf_len = 0;
460 ctx->buf_off = 0;
488 } 461 }
462 return (ret);
463}
489 464
490static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) 465static long
491 { 466b64_ctrl(BIO *b, int cmd, long num, void *ptr)
467{
492 BIO_B64_CTX *ctx; 468 BIO_B64_CTX *ctx;
493 long ret=1; 469 long ret = 1;
494 int i; 470 int i;
495 471
496 ctx=(BIO_B64_CTX *)b->ptr; 472 ctx = (BIO_B64_CTX *)b->ptr;
497 473
498 switch (cmd) 474 switch (cmd) {
499 {
500 case BIO_CTRL_RESET: 475 case BIO_CTRL_RESET:
501 ctx->cont=1; 476 ctx->cont = 1;
502 ctx->start=1; 477 ctx->start = 1;
503 ctx->encode=B64_NONE; 478 ctx->encode = B64_NONE;
504 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 479 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
505 break; 480 break;
506 case BIO_CTRL_EOF: /* More to read */ 481 case BIO_CTRL_EOF: /* More to read */
507 if (ctx->cont <= 0) 482 if (ctx->cont <= 0)
508 ret=1; 483 ret = 1;
509 else 484 else
510 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 485 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
511 break; 486 break;
512 case BIO_CTRL_WPENDING: /* More to write in buffer */ 487 case BIO_CTRL_WPENDING: /* More to write in buffer */
513 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 488 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
514 ret=ctx->buf_len-ctx->buf_off; 489 ret = ctx->buf_len - ctx->buf_off;
515 if ((ret == 0) && (ctx->encode != B64_NONE) 490 if ((ret == 0) && (ctx->encode != B64_NONE) &&
516 && (ctx->base64.num != 0)) 491 (ctx->base64.num != 0))
517 ret=1; 492 ret = 1;
518 else if (ret <= 0) 493 else if (ret <= 0)
519 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 494 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
520 break; 495 break;
521 case BIO_CTRL_PENDING: /* More to read in buffer */ 496 case BIO_CTRL_PENDING: /* More to read in buffer */
522 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 497 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
523 ret=ctx->buf_len-ctx->buf_off; 498 ret = ctx->buf_len - ctx->buf_off;
524 if (ret <= 0) 499 if (ret <= 0)
525 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 500 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
526 break; 501 break;
527 case BIO_CTRL_FLUSH: 502 case BIO_CTRL_FLUSH:
528 /* do a final write */ 503 /* do a final write */
529again: 504again:
530 while (ctx->buf_len != ctx->buf_off) 505 while (ctx->buf_len != ctx->buf_off) {
531 { 506 i = b64_write(b, NULL, 0);
532 i=b64_write(b,NULL,0);
533 if (i < 0) 507 if (i < 0)
534 return i; 508 return i;
535 } 509 }
536 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) 510 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
537 { 511 if (ctx->tmp_len != 0) {
538 if (ctx->tmp_len != 0) 512 ctx->buf_len = EVP_EncodeBlock(
539 { 513 (unsigned char *)ctx->buf,
540 ctx->buf_len=EVP_EncodeBlock( 514 (unsigned char *)ctx->tmp,
541 (unsigned char *)ctx->buf, 515 ctx->tmp_len);
542 (unsigned char *)ctx->tmp, 516 ctx->buf_off = 0;
543 ctx->tmp_len); 517 ctx->tmp_len = 0;
544 ctx->buf_off=0;
545 ctx->tmp_len=0;
546 goto again; 518 goto again;
547 }
548 } 519 }
549 else if (ctx->encode != B64_NONE && ctx->base64.num != 0) 520 } else if (ctx->encode != B64_NONE && ctx->base64.num != 0) {
550 { 521 ctx->buf_off = 0;
551 ctx->buf_off=0;
552 EVP_EncodeFinal(&(ctx->base64), 522 EVP_EncodeFinal(&(ctx->base64),
553 (unsigned char *)ctx->buf, 523 (unsigned char *)ctx->buf,
554 &(ctx->buf_len)); 524 &(ctx->buf_len));
555 /* push out the bytes */ 525 /* push out the bytes */
556 goto again; 526 goto again;
557 } 527 }
558 /* Finally flush the underlying BIO */ 528 /* Finally flush the underlying BIO */
559 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 529 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
560 break; 530 break;
561 531
562 case BIO_C_DO_STATE_MACHINE: 532 case BIO_C_DO_STATE_MACHINE:
563 BIO_clear_retry_flags(b); 533 BIO_clear_retry_flags(b);
564 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 534 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
565 BIO_copy_next_retry(b); 535 BIO_copy_next_retry(b);
566 break; 536 break;
567 537
@@ -571,27 +541,29 @@ again:
571 case BIO_CTRL_GET: 541 case BIO_CTRL_GET:
572 case BIO_CTRL_SET: 542 case BIO_CTRL_SET:
573 default: 543 default:
574 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 544 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
575 break; 545 break;
576 }
577 return(ret);
578 } 546 }
547 return (ret);
548}
579 549
580static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 550static long
581 { 551b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
582 long ret=1; 552{
553 long ret = 1;
583 554
584 if (b->next_bio == NULL) return(0); 555 if (b->next_bio == NULL)
585 switch (cmd) 556 return (0);
586 { 557 switch (cmd) {
587 default: 558 default:
588 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 559 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
589 break; 560 break;
590 }
591 return(ret);
592 }
593
594static int b64_puts(BIO *b, const char *str)
595 {
596 return b64_write(b,str,strlen(str));
597 } 561 }
562 return (ret);
563}
564
565static int
566b64_puts(BIO *b, const char *str)
567{
568 return b64_write(b, str, strlen(str));
569}
diff --git a/src/lib/libcrypto/evp/bio_enc.c b/src/lib/libcrypto/evp/bio_enc.c
index 3362c25768..46cd2a6a60 100644
--- a/src/lib/libcrypto/evp/bio_enc.c
+++ b/src/lib/libcrypto/evp/bio_enc.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
@@ -73,8 +73,7 @@ static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
73#define ENC_BLOCK_SIZE (1024*4) 73#define ENC_BLOCK_SIZE (1024*4)
74#define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2) 74#define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2)
75 75
76typedef struct enc_struct 76typedef struct enc_struct {
77 {
78 int buf_len; 77 int buf_len;
79 int buf_off; 78 int buf_off;
80 int cont; /* <= 0 when finished */ 79 int cont; /* <= 0 when finished */
@@ -84,10 +83,10 @@ typedef struct enc_struct
84 /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate 83 /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
85 * can return up to a block more data than is presented to it 84 * can return up to a block more data than is presented to it
86 */ 85 */
87 char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2]; 86 char buf[ENC_BLOCK_SIZE + BUF_OFFSET + 2];
88 } BIO_ENC_CTX; 87} BIO_ENC_CTX;
89 88
90static BIO_METHOD methods_enc= { 89static BIO_METHOD methods_enc = {
91 .type = BIO_TYPE_CIPHER, 90 .type = BIO_TYPE_CIPHER,
92 .name = "cipher", 91 .name = "cipher",
93 .bwrite = enc_write, 92 .bwrite = enc_write,
@@ -98,292 +97,292 @@ static BIO_METHOD methods_enc= {
98 .callback_ctrl = enc_callback_ctrl 97 .callback_ctrl = enc_callback_ctrl
99}; 98};
100 99
101BIO_METHOD *BIO_f_cipher(void) 100BIO_METHOD *
102 { 101BIO_f_cipher(void)
103 return(&methods_enc); 102{
104 } 103 return (&methods_enc);
104}
105 105
106static int enc_new(BIO *bi) 106static int
107 { 107enc_new(BIO *bi)
108{
108 BIO_ENC_CTX *ctx; 109 BIO_ENC_CTX *ctx;
109 110
110 ctx=(BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX)); 111 ctx = (BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX));
111 if (ctx == NULL) return(0); 112 if (ctx == NULL)
113 return (0);
112 EVP_CIPHER_CTX_init(&ctx->cipher); 114 EVP_CIPHER_CTX_init(&ctx->cipher);
113 115
114 ctx->buf_len=0; 116 ctx->buf_len = 0;
115 ctx->buf_off=0; 117 ctx->buf_off = 0;
116 ctx->cont=1; 118 ctx->cont = 1;
117 ctx->finished=0; 119 ctx->finished = 0;
118 ctx->ok=1; 120 ctx->ok = 1;
119 121
120 bi->init=0; 122 bi->init = 0;
121 bi->ptr=(char *)ctx; 123 bi->ptr = (char *)ctx;
122 bi->flags=0; 124 bi->flags = 0;
123 return(1); 125 return (1);
124 } 126}
125 127
126static int enc_free(BIO *a) 128static int
127 { 129enc_free(BIO *a)
130{
128 BIO_ENC_CTX *b; 131 BIO_ENC_CTX *b;
129 132
130 if (a == NULL) return(0); 133 if (a == NULL)
131 b=(BIO_ENC_CTX *)a->ptr; 134 return (0);
135 b = (BIO_ENC_CTX *)a->ptr;
132 EVP_CIPHER_CTX_cleanup(&(b->cipher)); 136 EVP_CIPHER_CTX_cleanup(&(b->cipher));
133 OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX)); 137 OPENSSL_cleanse(a->ptr, sizeof(BIO_ENC_CTX));
134 free(a->ptr); 138 free(a->ptr);
135 a->ptr=NULL; 139 a->ptr = NULL;
136 a->init=0; 140 a->init = 0;
137 a->flags=0; 141 a->flags = 0;
138 return(1); 142 return (1);
139 } 143}
140 144
141static int enc_read(BIO *b, char *out, int outl) 145static int
142 { 146enc_read(BIO *b, char *out, int outl)
143 int ret=0,i; 147{
148 int ret = 0, i;
144 BIO_ENC_CTX *ctx; 149 BIO_ENC_CTX *ctx;
145 150
146 if (out == NULL) return(0); 151 if (out == NULL)
147 ctx=(BIO_ENC_CTX *)b->ptr; 152 return (0);
153 ctx = (BIO_ENC_CTX *)b->ptr;
148 154
149 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 155 if ((ctx == NULL) || (b->next_bio == NULL))
156 return (0);
150 157
151 /* First check if there are bytes decoded/encoded */ 158 /* First check if there are bytes decoded/encoded */
152 if (ctx->buf_len > 0) 159 if (ctx->buf_len > 0) {
153 { 160 i = ctx->buf_len - ctx->buf_off;
154 i=ctx->buf_len-ctx->buf_off; 161 if (i > outl)
155 if (i > outl) i=outl; 162 i = outl;
156 memcpy(out,&(ctx->buf[ctx->buf_off]),i); 163 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
157 ret=i; 164 ret = i;
158 out+=i; 165 out += i;
159 outl-=i; 166 outl -= i;
160 ctx->buf_off+=i; 167 ctx->buf_off += i;
161 if (ctx->buf_len == ctx->buf_off) 168 if (ctx->buf_len == ctx->buf_off) {
162 { 169 ctx->buf_len = 0;
163 ctx->buf_len=0; 170 ctx->buf_off = 0;
164 ctx->buf_off=0;
165 }
166 } 171 }
172 }
167 173
168 /* At this point, we have room of outl bytes and an empty 174 /* At this point, we have room of outl bytes and an empty
169 * buffer, so we should read in some more. */ 175 * buffer, so we should read in some more. */
170 176
171 while (outl > 0) 177 while (outl > 0) {
172 { 178 if (ctx->cont <= 0)
173 if (ctx->cont <= 0) break; 179 break;
174 180
175 /* read in at IV offset, read the EVP_Cipher 181 /* read in at IV offset, read the EVP_Cipher
176 * documentation about why */ 182 * documentation about why */
177 i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE); 183 i = BIO_read(b->next_bio, &(ctx->buf[BUF_OFFSET]), ENC_BLOCK_SIZE);
178 184
179 if (i <= 0) 185 if (i <= 0) {
180 {
181 /* Should be continue next time we are called? */ 186 /* Should be continue next time we are called? */
182 if (!BIO_should_retry(b->next_bio)) 187 if (!BIO_should_retry(b->next_bio)) {
183 { 188 ctx->cont = i;
184 ctx->cont=i; 189 i = EVP_CipherFinal_ex(&(ctx->cipher),
185 i=EVP_CipherFinal_ex(&(ctx->cipher), 190 (unsigned char *)ctx->buf,
186 (unsigned char *)ctx->buf, 191 &(ctx->buf_len));
187 &(ctx->buf_len)); 192 ctx->ok = i;
188 ctx->ok=i; 193 ctx->buf_off = 0;
189 ctx->buf_off=0; 194 } else {
190 } 195 ret = (ret == 0) ? i : ret;
191 else
192 {
193 ret=(ret == 0)?i:ret;
194 break; 196 break;
195 }
196 } 197 }
197 else 198 } else {
198 {
199 EVP_CipherUpdate(&(ctx->cipher), 199 EVP_CipherUpdate(&(ctx->cipher),
200 (unsigned char *)ctx->buf,&ctx->buf_len, 200 (unsigned char *)ctx->buf, &ctx->buf_len,
201 (unsigned char *)&(ctx->buf[BUF_OFFSET]),i); 201 (unsigned char *)&(ctx->buf[BUF_OFFSET]), i);
202 ctx->cont=1; 202 ctx->cont = 1;
203 /* Note: it is possible for EVP_CipherUpdate to 203 /* Note: it is possible for EVP_CipherUpdate to
204 * decrypt zero bytes because this is or looks like 204 * decrypt zero bytes because this is or looks like
205 * the final block: if this happens we should retry 205 * the final block: if this happens we should retry
206 * and either read more data or decrypt the final 206 * and either read more data or decrypt the final
207 * block 207 * block
208 */ 208 */
209 if(ctx->buf_len == 0) continue; 209 if (ctx->buf_len == 0)
210 } 210 continue;
211 }
211 212
212 if (ctx->buf_len <= outl) 213 if (ctx->buf_len <= outl)
213 i=ctx->buf_len; 214 i = ctx->buf_len;
214 else 215 else
215 i=outl; 216 i = outl;
216 if (i <= 0) break; 217 if (i <= 0)
217 memcpy(out,ctx->buf,i); 218 break;
218 ret+=i; 219 memcpy(out, ctx->buf, i);
219 ctx->buf_off=i; 220 ret += i;
220 outl-=i; 221 ctx->buf_off = i;
221 out+=i; 222 outl -= i;
222 } 223 out += i;
224 }
223 225
224 BIO_clear_retry_flags(b); 226 BIO_clear_retry_flags(b);
225 BIO_copy_next_retry(b); 227 BIO_copy_next_retry(b);
226 return((ret == 0)?ctx->cont:ret); 228 return ((ret == 0) ? ctx->cont : ret);
227 } 229}
228 230
229static int enc_write(BIO *b, const char *in, int inl) 231static int
230 { 232enc_write(BIO *b, const char *in, int inl)
231 int ret=0,n,i; 233{
234 int ret = 0, n, i;
232 BIO_ENC_CTX *ctx; 235 BIO_ENC_CTX *ctx;
233 236
234 ctx=(BIO_ENC_CTX *)b->ptr; 237 ctx = (BIO_ENC_CTX *)b->ptr;
235 ret=inl; 238 ret = inl;
236 239
237 BIO_clear_retry_flags(b); 240 BIO_clear_retry_flags(b);
238 n=ctx->buf_len-ctx->buf_off; 241 n = ctx->buf_len - ctx->buf_off;
239 while (n > 0) 242 while (n > 0) {
240 { 243 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
241 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 244 if (i <= 0) {
242 if (i <= 0)
243 {
244 BIO_copy_next_retry(b); 245 BIO_copy_next_retry(b);
245 return(i); 246 return (i);
246 }
247 ctx->buf_off+=i;
248 n-=i;
249 } 247 }
248 ctx->buf_off += i;
249 n -= i;
250 }
250 /* at this point all pending data has been written */ 251 /* at this point all pending data has been written */
251 252
252 if ((in == NULL) || (inl <= 0)) return(0); 253 if ((in == NULL) || (inl <= 0))
254 return (0);
253 255
254 ctx->buf_off=0; 256 ctx->buf_off = 0;
255 while (inl > 0) 257 while (inl > 0) {
256 { 258 n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
257 n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
258 EVP_CipherUpdate(&(ctx->cipher), 259 EVP_CipherUpdate(&(ctx->cipher),
259 (unsigned char *)ctx->buf,&ctx->buf_len, 260 (unsigned char *)ctx->buf, &ctx->buf_len,
260 (unsigned char *)in,n); 261 (unsigned char *)in, n);
261 inl-=n; 262 inl -= n;
262 in+=n; 263 in += n;
263 264
264 ctx->buf_off=0; 265 ctx->buf_off = 0;
265 n=ctx->buf_len; 266 n = ctx->buf_len;
266 while (n > 0) 267 while (n > 0) {
267 { 268 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
268 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 269 if (i <= 0) {
269 if (i <= 0)
270 {
271 BIO_copy_next_retry(b); 270 BIO_copy_next_retry(b);
272 return (ret == inl) ? i : ret - inl; 271 return (ret == inl) ? i : ret - inl;
273 }
274 n-=i;
275 ctx->buf_off+=i;
276 } 272 }
277 ctx->buf_len=0; 273 n -= i;
278 ctx->buf_off=0; 274 ctx->buf_off += i;
279 } 275 }
280 BIO_copy_next_retry(b); 276 ctx->buf_len = 0;
281 return(ret); 277 ctx->buf_off = 0;
282 } 278 }
279 BIO_copy_next_retry(b);
280 return (ret);
281}
283 282
284static long enc_ctrl(BIO *b, int cmd, long num, void *ptr) 283static long
285 { 284enc_ctrl(BIO *b, int cmd, long num, void *ptr)
285{
286 BIO *dbio; 286 BIO *dbio;
287 BIO_ENC_CTX *ctx,*dctx; 287 BIO_ENC_CTX *ctx, *dctx;
288 long ret=1; 288 long ret = 1;
289 int i; 289 int i;
290 EVP_CIPHER_CTX **c_ctx; 290 EVP_CIPHER_CTX **c_ctx;
291 291
292 ctx=(BIO_ENC_CTX *)b->ptr; 292 ctx = (BIO_ENC_CTX *)b->ptr;
293 293
294 switch (cmd) 294 switch (cmd) {
295 {
296 case BIO_CTRL_RESET: 295 case BIO_CTRL_RESET:
297 ctx->ok=1; 296 ctx->ok = 1;
298 ctx->finished=0; 297 ctx->finished = 0;
299 EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL, 298 EVP_CipherInit_ex(&(ctx->cipher), NULL, NULL, NULL, NULL,
300 ctx->cipher.encrypt); 299 ctx->cipher.encrypt);
301 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 300 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
302 break; 301 break;
303 case BIO_CTRL_EOF: /* More to read */ 302 case BIO_CTRL_EOF: /* More to read */
304 if (ctx->cont <= 0) 303 if (ctx->cont <= 0)
305 ret=1; 304 ret = 1;
306 else 305 else
307 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 306 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
308 break; 307 break;
309 case BIO_CTRL_WPENDING: 308 case BIO_CTRL_WPENDING:
310 ret=ctx->buf_len-ctx->buf_off; 309 ret = ctx->buf_len - ctx->buf_off;
311 if (ret <= 0) 310 if (ret <= 0)
312 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 311 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
313 break; 312 break;
314 case BIO_CTRL_PENDING: /* More to read in buffer */ 313 case BIO_CTRL_PENDING: /* More to read in buffer */
315 ret=ctx->buf_len-ctx->buf_off; 314 ret = ctx->buf_len - ctx->buf_off;
316 if (ret <= 0) 315 if (ret <= 0)
317 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 316 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
318 break; 317 break;
319 case BIO_CTRL_FLUSH: 318 case BIO_CTRL_FLUSH:
320 /* do a final write */ 319 /* do a final write */
321again: 320again:
322 while (ctx->buf_len != ctx->buf_off) 321 while (ctx->buf_len != ctx->buf_off) {
323 { 322 i = enc_write(b, NULL, 0);
324 i=enc_write(b,NULL,0);
325 if (i < 0) 323 if (i < 0)
326 return i; 324 return i;
327 } 325 }
328 326
329 if (!ctx->finished) 327 if (!ctx->finished) {
330 { 328 ctx->finished = 1;
331 ctx->finished=1; 329 ctx->buf_off = 0;
332 ctx->buf_off=0; 330 ret = EVP_CipherFinal_ex(&(ctx->cipher),
333 ret=EVP_CipherFinal_ex(&(ctx->cipher), 331 (unsigned char *)ctx->buf,
334 (unsigned char *)ctx->buf, 332 &(ctx->buf_len));
335 &(ctx->buf_len)); 333 ctx->ok = (int)ret;
336 ctx->ok=(int)ret; 334 if (ret <= 0)
337 if (ret <= 0) break; 335 break;
338 336
339 /* push out the bytes */ 337 /* push out the bytes */
340 goto again; 338 goto again;
341 } 339 }
342 340
343 /* Finally flush the underlying BIO */ 341 /* Finally flush the underlying BIO */
344 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 342 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
345 break; 343 break;
346 case BIO_C_GET_CIPHER_STATUS: 344 case BIO_C_GET_CIPHER_STATUS:
347 ret=(long)ctx->ok; 345 ret = (long)ctx->ok;
348 break; 346 break;
349 case BIO_C_DO_STATE_MACHINE: 347 case BIO_C_DO_STATE_MACHINE:
350 BIO_clear_retry_flags(b); 348 BIO_clear_retry_flags(b);
351 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 349 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
352 BIO_copy_next_retry(b); 350 BIO_copy_next_retry(b);
353 break; 351 break;
354 case BIO_C_GET_CIPHER_CTX: 352 case BIO_C_GET_CIPHER_CTX:
355 c_ctx=(EVP_CIPHER_CTX **)ptr; 353 c_ctx = (EVP_CIPHER_CTX **)ptr;
356 (*c_ctx)= &(ctx->cipher); 354 (*c_ctx) = &(ctx->cipher);
357 b->init=1; 355 b->init = 1;
358 break; 356 break;
359 case BIO_CTRL_DUP: 357 case BIO_CTRL_DUP:
360 dbio=(BIO *)ptr; 358 dbio = (BIO *)ptr;
361 dctx=(BIO_ENC_CTX *)dbio->ptr; 359 dctx = (BIO_ENC_CTX *)dbio->ptr;
362 EVP_CIPHER_CTX_init(&dctx->cipher); 360 EVP_CIPHER_CTX_init(&dctx->cipher);
363 ret = EVP_CIPHER_CTX_copy(&dctx->cipher,&ctx->cipher); 361 ret = EVP_CIPHER_CTX_copy(&dctx->cipher, &ctx->cipher);
364 if (ret) 362 if (ret)
365 dbio->init=1; 363 dbio->init = 1;
366 break; 364 break;
367 default: 365 default:
368 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 366 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
369 break; 367 break;
370 }
371 return(ret);
372 } 368 }
369 return (ret);
370}
373 371
374static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 372static long
375 { 373enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
376 long ret=1; 374{
375 long ret = 1;
377 376
378 if (b->next_bio == NULL) return(0); 377 if (b->next_bio == NULL)
379 switch (cmd) 378 return (0);
380 { 379 switch (cmd) {
381 default: 380 default:
382 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 381 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
383 break; 382 break;
384 }
385 return(ret);
386 } 383 }
384 return (ret);
385}
387 386
388/* 387/*
389void BIO_set_cipher_ctx(b,c) 388void BIO_set_cipher_ctx(b,c)
@@ -399,28 +398,29 @@ EVP_CIPHER_ctx *c;
399 b->init=1; 398 b->init=1;
400 ctx=(BIO_ENC_CTX *)b->ptr; 399 ctx=(BIO_ENC_CTX *)b->ptr;
401 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); 400 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
402 401
403 if (b->callback != NULL) 402 if (b->callback != NULL)
404 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); 403 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
405 } 404 }
406*/ 405*/
407 406
408void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, 407void
409 const unsigned char *i, int e) 408BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
410 { 409 const unsigned char *i, int e)
410{
411 BIO_ENC_CTX *ctx; 411 BIO_ENC_CTX *ctx;
412 412
413 if (b == NULL) return; 413 if (b == NULL)
414 return;
414 415
415 if ((b->callback != NULL) && 416 if ((b->callback != NULL) &&
416 (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0)) 417 (b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= 0))
417 return; 418 return;
418 419
419 b->init=1; 420 b->init = 1;
420 ctx=(BIO_ENC_CTX *)b->ptr; 421 ctx = (BIO_ENC_CTX *)b->ptr;
421 EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e); 422 EVP_CipherInit_ex(&(ctx->cipher), c,NULL, k, i, e);
422
423 if (b->callback != NULL)
424 b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
425 }
426 423
424 if (b->callback != NULL)
425 b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
426}
diff --git a/src/lib/libcrypto/evp/bio_md.c b/src/lib/libcrypto/evp/bio_md.c
index 85eead6c95..7ace6a4e89 100644
--- a/src/lib/libcrypto/evp/bio_md.c
+++ b/src/lib/libcrypto/evp/bio_md.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
@@ -72,7 +72,7 @@ static int md_gets(BIO *h, char *str, int size);
72static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2); 72static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
73static int md_new(BIO *h); 73static int md_new(BIO *h);
74static int md_free(BIO *data); 74static int md_free(BIO *data);
75static long md_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); 75static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
76 76
77static BIO_METHOD methods_md = { 77static BIO_METHOD methods_md = {
78 .type = BIO_TYPE_MD, 78 .type = BIO_TYPE_MD,
@@ -86,182 +86,186 @@ static BIO_METHOD methods_md = {
86 .callback_ctrl = md_callback_ctrl 86 .callback_ctrl = md_callback_ctrl
87}; 87};
88 88
89BIO_METHOD *BIO_f_md(void) 89BIO_METHOD *
90 { 90BIO_f_md(void)
91 return(&methods_md); 91{
92 } 92 return (&methods_md);
93}
93 94
94static int md_new(BIO *bi) 95static int
95 { 96md_new(BIO *bi)
97{
96 EVP_MD_CTX *ctx; 98 EVP_MD_CTX *ctx;
97 99
98 ctx=EVP_MD_CTX_create(); 100 ctx = EVP_MD_CTX_create();
99 if (ctx == NULL) return(0); 101 if (ctx == NULL)
102 return (0);
100 103
101 bi->init=0; 104 bi->init = 0;
102 bi->ptr=(char *)ctx; 105 bi->ptr = (char *)ctx;
103 bi->flags=0; 106 bi->flags = 0;
104 return(1); 107 return (1);
105 } 108}
106 109
107static int md_free(BIO *a) 110static int
108 { 111md_free(BIO *a)
109 if (a == NULL) return(0); 112{
113 if (a == NULL)
114 return (0);
110 EVP_MD_CTX_destroy(a->ptr); 115 EVP_MD_CTX_destroy(a->ptr);
111 a->ptr=NULL; 116 a->ptr = NULL;
112 a->init=0; 117 a->init = 0;
113 a->flags=0; 118 a->flags = 0;
114 return(1); 119 return (1);
115 } 120}
116 121
117static int md_read(BIO *b, char *out, int outl) 122static int
118 { 123md_read(BIO *b, char *out, int outl)
119 int ret=0; 124{
125 int ret = 0;
120 EVP_MD_CTX *ctx; 126 EVP_MD_CTX *ctx;
121 127
122 if (out == NULL) return(0); 128 if (out == NULL)
123 ctx=b->ptr; 129 return (0);
130 ctx = b->ptr;
124 131
125 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 132 if ((ctx == NULL) || (b->next_bio == NULL))
133 return (0);
126 134
127 ret=BIO_read(b->next_bio,out,outl); 135 ret = BIO_read(b->next_bio, out, outl);
128 if (b->init) 136 if (b->init) {
129 { 137 if (ret > 0) {
130 if (ret > 0) 138 if (EVP_DigestUpdate(ctx, (unsigned char *)out,
131 { 139 (unsigned int)ret) <= 0)
132 if (EVP_DigestUpdate(ctx,(unsigned char *)out, 140 return (-1);
133 (unsigned int)ret)<=0) return (-1);
134 }
135 } 141 }
142 }
136 BIO_clear_retry_flags(b); 143 BIO_clear_retry_flags(b);
137 BIO_copy_next_retry(b); 144 BIO_copy_next_retry(b);
138 return(ret); 145 return (ret);
139 } 146}
140 147
141static int md_write(BIO *b, const char *in, int inl) 148static int
142 { 149md_write(BIO *b, const char *in, int inl)
143 int ret=0; 150{
151 int ret = 0;
144 EVP_MD_CTX *ctx; 152 EVP_MD_CTX *ctx;
145 153
146 if ((in == NULL) || (inl <= 0)) return(0); 154 if ((in == NULL) || (inl <= 0))
147 ctx=b->ptr; 155 return (0);
156 ctx = b->ptr;
148 157
149 if ((ctx != NULL) && (b->next_bio != NULL)) 158 if ((ctx != NULL) && (b->next_bio != NULL))
150 ret=BIO_write(b->next_bio,in,inl); 159 ret = BIO_write(b->next_bio, in, inl);
151 if (b->init) 160 if (b->init) {
152 { 161 if (ret > 0) {
153 if (ret > 0) 162 if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
154 { 163 (unsigned int)ret)) {
155 if (!EVP_DigestUpdate(ctx,(const unsigned char *)in,
156 (unsigned int)ret))
157 {
158 BIO_clear_retry_flags(b); 164 BIO_clear_retry_flags(b);
159 return 0; 165 return 0;
160 }
161 } 166 }
162 } 167 }
163 if(b->next_bio != NULL) 168 }
164 { 169 if (b->next_bio != NULL) {
165 BIO_clear_retry_flags(b); 170 BIO_clear_retry_flags(b);
166 BIO_copy_next_retry(b); 171 BIO_copy_next_retry(b);
167 }
168 return(ret);
169 } 172 }
173 return (ret);
174}
170 175
171static long md_ctrl(BIO *b, int cmd, long num, void *ptr) 176static long
172 { 177md_ctrl(BIO *b, int cmd, long num, void *ptr)
173 EVP_MD_CTX *ctx,*dctx,**pctx; 178{
179 EVP_MD_CTX *ctx, *dctx, **pctx;
174 const EVP_MD **ppmd; 180 const EVP_MD **ppmd;
175 EVP_MD *md; 181 EVP_MD *md;
176 long ret=1; 182 long ret = 1;
177 BIO *dbio; 183 BIO *dbio;
178 184
179 ctx=b->ptr; 185 ctx = b->ptr;
180 186
181 switch (cmd) 187 switch (cmd) {
182 {
183 case BIO_CTRL_RESET: 188 case BIO_CTRL_RESET:
184 if (b->init) 189 if (b->init)
185 ret = EVP_DigestInit_ex(ctx,ctx->digest, NULL); 190 ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
186 else 191 else
187 ret=0; 192 ret = 0;
188 if (ret > 0) 193 if (ret > 0)
189 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 194 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
190 break; 195 break;
191 case BIO_C_GET_MD: 196 case BIO_C_GET_MD:
192 if (b->init) 197 if (b->init) {
193 { 198 ppmd = ptr;
194 ppmd=ptr; 199 *ppmd = ctx->digest;
195 *ppmd=ctx->digest; 200 } else
196 } 201 ret = 0;
197 else
198 ret=0;
199 break; 202 break;
200 case BIO_C_GET_MD_CTX: 203 case BIO_C_GET_MD_CTX:
201 pctx=ptr; 204 pctx = ptr;
202 *pctx=ctx; 205 *pctx = ctx;
203 b->init = 1; 206 b->init = 1;
204 break; 207 break;
205 case BIO_C_SET_MD_CTX: 208 case BIO_C_SET_MD_CTX:
206 if (b->init) 209 if (b->init)
207 b->ptr=ptr; 210 b->ptr = ptr;
208 else 211 else
209 ret=0; 212 ret = 0;
210 break; 213 break;
211 case BIO_C_DO_STATE_MACHINE: 214 case BIO_C_DO_STATE_MACHINE:
212 BIO_clear_retry_flags(b); 215 BIO_clear_retry_flags(b);
213 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 216 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
214 BIO_copy_next_retry(b); 217 BIO_copy_next_retry(b);
215 break; 218 break;
216 219
217 case BIO_C_SET_MD: 220 case BIO_C_SET_MD:
218 md=ptr; 221 md = ptr;
219 ret = EVP_DigestInit_ex(ctx,md, NULL); 222 ret = EVP_DigestInit_ex(ctx, md, NULL);
220 if (ret > 0) 223 if (ret > 0)
221 b->init=1; 224 b->init = 1;
222 break; 225 break;
223 case BIO_CTRL_DUP: 226 case BIO_CTRL_DUP:
224 dbio=ptr; 227 dbio = ptr;
225 dctx=dbio->ptr; 228 dctx = dbio->ptr;
226 if (!EVP_MD_CTX_copy_ex(dctx,ctx)) 229 if (!EVP_MD_CTX_copy_ex(dctx, ctx))
227 return 0; 230 return 0;
228 b->init=1; 231 b->init = 1;
229 break; 232 break;
230 default: 233 default:
231 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 234 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
232 break; 235 break;
233 }
234 return(ret);
235 } 236 }
237 return (ret);
238}
236 239
237static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 240static long
238 { 241md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
239 long ret=1; 242{
243 long ret = 1;
240 244
241 if (b->next_bio == NULL) return(0); 245 if (b->next_bio == NULL)
242 switch (cmd) 246 return (0);
243 { 247 switch (cmd) {
244 default: 248 default:
245 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 249 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
246 break; 250 break;
247 }
248 return(ret);
249 } 251 }
252 return (ret);
253}
250 254
251static int md_gets(BIO *bp, char *buf, int size) 255static int
252 { 256md_gets(BIO *bp, char *buf, int size)
257{
253 EVP_MD_CTX *ctx; 258 EVP_MD_CTX *ctx;
254 unsigned int ret; 259 unsigned int ret;
255 260
256 261 ctx = bp->ptr;
257 ctx=bp->ptr;
258 if (size < ctx->digest->md_size) 262 if (size < ctx->digest->md_size)
259 return(0); 263 return (0);
260 if (EVP_DigestFinal_ex(ctx,(unsigned char *)buf,&ret)<=0) 264 if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
261 return -1; 265 return -1;
262 266
263 return((int)ret); 267 return ((int)ret);
264 } 268}
265 269
266/* 270/*
267static int md_puts(bp,str) 271static int md_puts(bp,str)
@@ -271,4 +275,3 @@ char *str;
271 return(-1); 275 return(-1);
272 } 276 }
273*/ 277*/
274
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
146typedef struct ok_struct 146typedef 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
160static BIO_METHOD methods_ok = { 159static 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
171BIO_METHOD *BIO_f_reliable(void) 170BIO_METHOD *
172 { 171BIO_f_reliable(void)
173 return(&methods_ok); 172{
174 } 173 return (&methods_ok);
174}
175 175
176static int ok_new(BIO *bi) 176static int
177 { 177ok_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
200static int ok_free(BIO *a) 202static int
201 { 203ok_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
212static int ok_read(BIO *b, char *out, int outl) 216static int
213 { 217ok_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
297static int ok_write(BIO *b, const char *in, int inl) 297static int
298 { 298ok_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
362static long ok_ctrl(BIO *b, int cmd, long num, void *ptr) 362static long
363 { 363ok_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
450static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 446static long
451 { 447ok_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
464static void longswap(void *_ptr, size_t len) 461static void
462longswap(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
477static int sig_out(BIO* b) 475static int
478 { 476sig_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: 505berr:
506 BIO_clear_retry_flags(b); 506 BIO_clear_retry_flags(b);
507 return 0; 507 return 0;
508 } 508}
509 509
510static int sig_in(BIO* b) 510static int
511 { 511sig_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: 548berr:
550 BIO_clear_retry_flags(b); 549 BIO_clear_retry_flags(b);
551 return 0; 550 return 0;
552 } 551}
553 552
554static int block_out(BIO* b) 553static int
555 { 554block_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: 576berr:
577 BIO_clear_retry_flags(b); 577 BIO_clear_retry_flags(b);
578 return 0; 578 return 0;
579 } 579}
580 580
581static int block_in(BIO* b) 581static int
582 { 582block_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
622berr:
619 BIO_clear_retry_flags(b); 623 BIO_clear_retry_flags(b);
620 return 0; 624 return 0;
621 } 625}
622
diff --git a/src/lib/libssl/src/crypto/evp/bio_b64.c b/src/lib/libssl/src/crypto/evp/bio_b64.c
index 02631ec05a..f963b606d2 100644
--- a/src/lib/libssl/src/crypto/evp/bio_b64.c
+++ b/src/lib/libssl/src/crypto/evp/bio_b64.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
@@ -69,15 +69,14 @@ static int b64_puts(BIO *h, const char *str);
69static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2); 69static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
70static int b64_new(BIO *h); 70static int b64_new(BIO *h);
71static int b64_free(BIO *data); 71static int b64_free(BIO *data);
72static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); 72static long b64_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
73#define B64_BLOCK_SIZE 1024 73#define B64_BLOCK_SIZE 1024
74#define B64_BLOCK_SIZE2 768 74#define B64_BLOCK_SIZE2 768
75#define B64_NONE 0 75#define B64_NONE 0
76#define B64_ENCODE 1 76#define B64_ENCODE 1
77#define B64_DECODE 2 77#define B64_DECODE 2
78 78
79typedef struct b64_struct 79typedef struct b64_struct {
80 {
81 /*BIO *bio; moved to the BIO structure */ 80 /*BIO *bio; moved to the BIO structure */
82 int buf_len; 81 int buf_len;
83 int buf_off; 82 int buf_off;
@@ -87,11 +86,11 @@ typedef struct b64_struct
87 int start; /* have we started decoding yet? */ 86 int start; /* have we started decoding yet? */
88 int cont; /* <= 0 when finished */ 87 int cont; /* <= 0 when finished */
89 EVP_ENCODE_CTX base64; 88 EVP_ENCODE_CTX base64;
90 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE)+10]; 89 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
91 char tmp[B64_BLOCK_SIZE]; 90 char tmp[B64_BLOCK_SIZE];
92 } BIO_B64_CTX; 91} BIO_B64_CTX;
93 92
94static BIO_METHOD methods_b64= { 93static BIO_METHOD methods_b64 = {
95 .type = BIO_TYPE_BASE64, 94 .type = BIO_TYPE_BASE64,
96 .name = "base64 encoding", 95 .name = "base64 encoding",
97 .bwrite = b64_write, 96 .bwrite = b64_write,
@@ -103,465 +102,436 @@ static BIO_METHOD methods_b64= {
103 .callback_ctrl = b64_callback_ctrl 102 .callback_ctrl = b64_callback_ctrl
104}; 103};
105 104
106BIO_METHOD *BIO_f_base64(void) 105BIO_METHOD *
107 { 106BIO_f_base64(void)
108 return(&methods_b64); 107{
109 } 108 return (&methods_b64);
109}
110 110
111static int b64_new(BIO *bi) 111static int
112 { 112b64_new(BIO *bi)
113{
113 BIO_B64_CTX *ctx; 114 BIO_B64_CTX *ctx;
114 115
115 ctx=(BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX)); 116 ctx = (BIO_B64_CTX *)malloc(sizeof(BIO_B64_CTX));
116 if (ctx == NULL) return(0); 117 if (ctx == NULL)
117 118 return (0);
118 ctx->buf_len=0; 119
119 ctx->tmp_len=0; 120 ctx->buf_len = 0;
120 ctx->tmp_nl=0; 121 ctx->tmp_len = 0;
121 ctx->buf_off=0; 122 ctx->tmp_nl = 0;
122 ctx->cont=1; 123 ctx->buf_off = 0;
123 ctx->start=1; 124 ctx->cont = 1;
124 ctx->encode=0; 125 ctx->start = 1;
125 126 ctx->encode = 0;
126 bi->init=1; 127
127 bi->ptr=(char *)ctx; 128 bi->init = 1;
128 bi->flags=0; 129 bi->ptr = (char *)ctx;
130 bi->flags = 0;
129 bi->num = 0; 131 bi->num = 0;
130 return(1); 132 return (1);
131 } 133}
132 134
133static int b64_free(BIO *a) 135static int
134 { 136b64_free(BIO *a)
135 if (a == NULL) return(0); 137{
138 if (a == NULL)
139 return (0);
136 free(a->ptr); 140 free(a->ptr);
137 a->ptr=NULL; 141 a->ptr = NULL;
138 a->init=0; 142 a->init = 0;
139 a->flags=0; 143 a->flags = 0;
140 return(1); 144 return (1);
141 } 145}
142 146
143static int b64_read(BIO *b, char *out, int outl) 147static int
144 { 148b64_read(BIO *b, char *out, int outl)
145 int ret=0,i,ii,j,k,x,n,num,ret_code=0; 149{
150 int ret = 0, i, ii, j,k, x,n, num, ret_code = 0;
146 BIO_B64_CTX *ctx; 151 BIO_B64_CTX *ctx;
147 unsigned char *p,*q; 152 unsigned char *p, *q;
148 153
149 if (out == NULL) return(0); 154 if (out == NULL)
150 ctx=(BIO_B64_CTX *)b->ptr; 155 return (0);
156 ctx = (BIO_B64_CTX *)b->ptr;
151 157
152 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 158 if ((ctx == NULL) || (b->next_bio == NULL))
159 return (0);
153 160
154 BIO_clear_retry_flags(b); 161 BIO_clear_retry_flags(b);
155 162
156 if (ctx->encode != B64_DECODE) 163 if (ctx->encode != B64_DECODE) {
157 { 164 ctx->encode = B64_DECODE;
158 ctx->encode=B64_DECODE; 165 ctx->buf_len = 0;
159 ctx->buf_len=0; 166 ctx->buf_off = 0;
160 ctx->buf_off=0; 167 ctx->tmp_len = 0;
161 ctx->tmp_len=0;
162 EVP_DecodeInit(&(ctx->base64)); 168 EVP_DecodeInit(&(ctx->base64));
163 } 169 }
164 170
165 /* First check if there are bytes decoded/encoded */ 171 /* First check if there are bytes decoded/encoded */
166 if (ctx->buf_len > 0) 172 if (ctx->buf_len > 0) {
167 {
168 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 173 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
169 i=ctx->buf_len-ctx->buf_off; 174 i = ctx->buf_len - ctx->buf_off;
170 if (i > outl) i=outl; 175 if (i > outl)
171 OPENSSL_assert(ctx->buf_off+i < (int)sizeof(ctx->buf)); 176 i = outl;
172 memcpy(out,&(ctx->buf[ctx->buf_off]),i); 177 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
173 ret=i; 178 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
174 out+=i; 179 ret = i;
175 outl-=i; 180 out += i;
176 ctx->buf_off+=i; 181 outl -= i;
177 if (ctx->buf_len == ctx->buf_off) 182 ctx->buf_off += i;
178 { 183 if (ctx->buf_len == ctx->buf_off) {
179 ctx->buf_len=0; 184 ctx->buf_len = 0;
180 ctx->buf_off=0; 185 ctx->buf_off = 0;
181 }
182 } 186 }
187 }
183 188
184 /* At this point, we have room of outl bytes and an empty 189 /* At this point, we have room of outl bytes and an empty
185 * buffer, so we should read in some more. */ 190 * buffer, so we should read in some more. */
186 191
187 ret_code=0; 192 ret_code = 0;
188 while (outl > 0) 193 while (outl > 0) {
189 {
190 if (ctx->cont <= 0) 194 if (ctx->cont <= 0)
191 break; 195 break;
192 196
193 i=BIO_read(b->next_bio,&(ctx->tmp[ctx->tmp_len]), 197 i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]),
194 B64_BLOCK_SIZE-ctx->tmp_len); 198 B64_BLOCK_SIZE - ctx->tmp_len);
195 199
196 if (i <= 0) 200 if (i <= 0) {
197 { 201 ret_code = i;
198 ret_code=i;
199 202
200 /* Should we continue next time we are called? */ 203 /* Should we continue next time we are called? */
201 if (!BIO_should_retry(b->next_bio)) 204 if (!BIO_should_retry(b->next_bio)) {
202 { 205 ctx->cont = i;
203 ctx->cont=i;
204 /* If buffer empty break */ 206 /* If buffer empty break */
205 if(ctx->tmp_len == 0) 207 if (ctx->tmp_len == 0)
206 break; 208 break;
207 /* Fall through and process what we have */ 209 /* Fall through and process what we have */
208 else 210 else
209 i = 0; 211 i = 0;
210 } 212 }
211 /* else we retry and add more data to buffer */ 213 /* else we retry and add more data to buffer */
212 else 214 else
213 break; 215 break;
214 } 216 }
215 i+=ctx->tmp_len; 217 i += ctx->tmp_len;
216 ctx->tmp_len = i; 218 ctx->tmp_len = i;
217 219
218 /* We need to scan, a line at a time until we 220 /* We need to scan, a line at a time until we
219 * have a valid line if we are starting. */ 221 * have a valid line if we are starting. */
220 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) 222 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
221 {
222 /* ctx->start=1; */ 223 /* ctx->start=1; */
223 ctx->tmp_len=0; 224 ctx->tmp_len = 0;
224 } 225 } else if (ctx->start) {
225 else if (ctx->start) 226 q = p=(unsigned char *)ctx->tmp;
226 { 227 for (j = 0; j < i; j++) {
227 q=p=(unsigned char *)ctx->tmp; 228 if (*(q++) != '\n')
228 for (j=0; j<i; j++) 229 continue;
229 {
230 if (*(q++) != '\n') continue;
231 230
232 /* due to a previous very long line, 231 /* due to a previous very long line,
233 * we need to keep on scanning for a '\n' 232 * we need to keep on scanning for a '\n'
234 * before we even start looking for 233 * before we even start looking for
235 * base64 encoded stuff. */ 234 * base64 encoded stuff. */
236 if (ctx->tmp_nl) 235 if (ctx->tmp_nl) {
237 { 236 p = q;
238 p=q; 237 ctx->tmp_nl = 0;
239 ctx->tmp_nl=0;
240 continue; 238 continue;
241 } 239 }
242 240
243 k=EVP_DecodeUpdate(&(ctx->base64), 241 k = EVP_DecodeUpdate(&(ctx->base64),
244 (unsigned char *)ctx->buf, 242 (unsigned char *)ctx->buf,
245 &num,p,q-p); 243 &num, p, q - p);
246 if ((k <= 0) && (num == 0) && (ctx->start)) 244 if ((k <= 0) && (num == 0) && (ctx->start))
247 EVP_DecodeInit(&ctx->base64); 245 EVP_DecodeInit(&ctx->base64);
248 else 246 else {
249 {
250 if (p != (unsigned char *) 247 if (p != (unsigned char *)
251 &(ctx->tmp[0])) 248 &(ctx->tmp[0])) {
252 { 249 i -= (p - (unsigned char *)
253 i-=(p- (unsigned char *) 250 &(ctx->tmp[0]));
254 &(ctx->tmp[0])); 251 for (x = 0; x < i; x++)
255 for (x=0; x < i; x++) 252 ctx->tmp[x] = p[x];
256 ctx->tmp[x]=p[x]; 253 }
257 }
258 EVP_DecodeInit(&ctx->base64); 254 EVP_DecodeInit(&ctx->base64);
259 ctx->start=0; 255 ctx->start = 0;
260 break; 256 break;
261 }
262 p=q;
263 } 257 }
258 p = q;
259 }
264 260
265 /* we fell off the end without starting */ 261 /* we fell off the end without starting */
266 if ((j == i) && (num == 0)) 262 if ((j == i) && (num == 0)) {
267 {
268 /* Is this is one long chunk?, if so, keep on 263 /* Is this is one long chunk?, if so, keep on
269 * reading until a new line. */ 264 * reading until a new line. */
270 if (p == (unsigned char *)&(ctx->tmp[0])) 265 if (p == (unsigned char *)&(ctx->tmp[0])) {
271 {
272 /* Check buffer full */ 266 /* Check buffer full */
273 if (i == B64_BLOCK_SIZE) 267 if (i == B64_BLOCK_SIZE) {
274 { 268 ctx->tmp_nl = 1;
275 ctx->tmp_nl=1; 269 ctx->tmp_len = 0;
276 ctx->tmp_len=0;
277 }
278 } 270 }
271 }
279 else if (p != q) /* finished on a '\n' */ 272 else if (p != q) /* finished on a '\n' */
280 { 273 {
281 n=q-p; 274 n = q - p;
282 for (ii=0; ii<n; ii++) 275 for (ii = 0; ii < n; ii++)
283 ctx->tmp[ii]=p[ii]; 276 ctx->tmp[ii] = p[ii];
284 ctx->tmp_len=n; 277 ctx->tmp_len = n;
285 } 278 }
286 /* else finished on a '\n' */ 279 /* else finished on a '\n' */
287 continue; 280 continue;
288 } 281 } else {
289 else 282 ctx->tmp_len = 0;
290 {
291 ctx->tmp_len=0;
292 } 283 }
293 } 284 } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
294 else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0))
295 {
296 /* If buffer isn't full and we can retry then 285 /* If buffer isn't full and we can retry then
297 * restart to read in more data. 286 * restart to read in more data.
298 */ 287 */
299 continue; 288 continue;
300 } 289 }
301 290
302 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) 291 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
303 { 292 int z, jj;
304 int z,jj;
305 293
306#if 0 294#if 0
307 jj=(i>>2)<<2; 295 jj = (i >> 2) << 2;
308#else 296#else
309 jj = i & ~3; /* process per 4 */ 297 jj = i & ~3; /* process per 4 */
310#endif 298#endif
311 z=EVP_DecodeBlock((unsigned char *)ctx->buf, 299 z = EVP_DecodeBlock((unsigned char *)ctx->buf,
312 (unsigned char *)ctx->tmp,jj); 300 (unsigned char *)ctx->tmp, jj);
313 if (jj > 2) 301 if (jj > 2) {
314 { 302 if (ctx->tmp[jj-1] == '=') {
315 if (ctx->tmp[jj-1] == '=')
316 {
317 z--; 303 z--;
318 if (ctx->tmp[jj-2] == '=') 304 if (ctx->tmp[jj-2] == '=')
319 z--; 305 z--;
320 }
321 } 306 }
307 }
322 /* z is now number of output bytes and jj is the 308 /* z is now number of output bytes and jj is the
323 * number consumed */ 309 * number consumed */
324 if (jj != i) 310 if (jj != i) {
325 { 311 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
326 memmove(ctx->tmp, &ctx->tmp[jj], i-jj); 312 ctx->tmp_len = i - jj;
327 ctx->tmp_len=i-jj;
328 }
329 ctx->buf_len=0;
330 if (z > 0)
331 {
332 ctx->buf_len=z;
333 }
334 i=z;
335 } 313 }
336 else 314 ctx->buf_len = 0;
337 { 315 if (z > 0) {
338 i=EVP_DecodeUpdate(&(ctx->base64), 316 ctx->buf_len = z;
339 (unsigned char *)ctx->buf,&ctx->buf_len,
340 (unsigned char *)ctx->tmp,i);
341 ctx->tmp_len = 0;
342 } 317 }
343 ctx->buf_off=0; 318 i = z;
344 if (i < 0) 319 } else {
345 { 320 i = EVP_DecodeUpdate(&(ctx->base64),
346 ret_code=0; 321 (unsigned char *)ctx->buf, &ctx->buf_len,
347 ctx->buf_len=0; 322 (unsigned char *)ctx->tmp, i);
323 ctx->tmp_len = 0;
324 }
325 ctx->buf_off = 0;
326 if (i < 0) {
327 ret_code = 0;
328 ctx->buf_len = 0;
348 break; 329 break;
349 } 330 }
350 331
351 if (ctx->buf_len <= outl) 332 if (ctx->buf_len <= outl)
352 i=ctx->buf_len; 333 i = ctx->buf_len;
353 else 334 else
354 i=outl; 335 i = outl;
355 336
356 memcpy(out,ctx->buf,i); 337 memcpy(out, ctx->buf, i);
357 ret+=i; 338 ret += i;
358 ctx->buf_off=i; 339 ctx->buf_off = i;
359 if (ctx->buf_off == ctx->buf_len) 340 if (ctx->buf_off == ctx->buf_len) {
360 { 341 ctx->buf_len = 0;
361 ctx->buf_len=0; 342 ctx->buf_off = 0;
362 ctx->buf_off=0;
363 }
364 outl-=i;
365 out+=i;
366 } 343 }
344 outl -= i;
345 out += i;
346 }
367 /* BIO_clear_retry_flags(b); */ 347 /* BIO_clear_retry_flags(b); */
368 BIO_copy_next_retry(b); 348 BIO_copy_next_retry(b);
369 return((ret == 0)?ret_code:ret); 349 return ((ret == 0) ? ret_code : ret);
370 } 350}
371 351
372static int b64_write(BIO *b, const char *in, int inl) 352static int
373 { 353b64_write(BIO *b, const char *in, int inl)
374 int ret=0; 354{
355 int ret = 0;
375 int n; 356 int n;
376 int i; 357 int i;
377 BIO_B64_CTX *ctx; 358 BIO_B64_CTX *ctx;
378 359
379 ctx=(BIO_B64_CTX *)b->ptr; 360 ctx = (BIO_B64_CTX *)b->ptr;
380 BIO_clear_retry_flags(b); 361 BIO_clear_retry_flags(b);
381 362
382 if (ctx->encode != B64_ENCODE) 363 if (ctx->encode != B64_ENCODE) {
383 { 364 ctx->encode = B64_ENCODE;
384 ctx->encode=B64_ENCODE; 365 ctx->buf_len = 0;
385 ctx->buf_len=0; 366 ctx->buf_off = 0;
386 ctx->buf_off=0; 367 ctx->tmp_len = 0;
387 ctx->tmp_len=0;
388 EVP_EncodeInit(&(ctx->base64)); 368 EVP_EncodeInit(&(ctx->base64));
389 } 369 }
390 370
391 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf)); 371 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
392 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 372 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
393 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 373 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
394 n=ctx->buf_len-ctx->buf_off; 374 n = ctx->buf_len - ctx->buf_off;
395 while (n > 0) 375 while (n > 0) {
396 { 376 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
397 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 377 if (i <= 0) {
398 if (i <= 0)
399 {
400 BIO_copy_next_retry(b); 378 BIO_copy_next_retry(b);
401 return(i); 379 return (i);
402 } 380 }
403 OPENSSL_assert(i <= n); 381 OPENSSL_assert(i <= n);
404 ctx->buf_off+=i; 382 ctx->buf_off += i;
405 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf)); 383 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
406 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 384 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
407 n-=i; 385 n -= i;
408 } 386 }
409 /* at this point all pending data has been written */ 387 /* at this point all pending data has been written */
410 ctx->buf_off=0; 388 ctx->buf_off = 0;
411 ctx->buf_len=0; 389 ctx->buf_len = 0;
412 390
413 if ((in == NULL) || (inl <= 0)) return(0); 391 if ((in == NULL) || (inl <= 0))
392 return (0);
414 393
415 while (inl > 0) 394 while (inl > 0) {
416 { 395 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
417 n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
418 396
419 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) 397 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
420 { 398 if (ctx->tmp_len > 0) {
421 if (ctx->tmp_len > 0)
422 {
423 OPENSSL_assert(ctx->tmp_len <= 3); 399 OPENSSL_assert(ctx->tmp_len <= 3);
424 n=3-ctx->tmp_len; 400 n = 3 - ctx->tmp_len;
425 /* There's a theoretical possibility for this */ 401 /* There's a theoretical possibility for this */
426 if (n > inl) 402 if (n > inl)
427 n=inl; 403 n = inl;
428 memcpy(&(ctx->tmp[ctx->tmp_len]),in,n); 404 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
429 ctx->tmp_len+=n; 405 ctx->tmp_len += n;
430 ret += n; 406 ret += n;
431 if (ctx->tmp_len < 3) 407 if (ctx->tmp_len < 3)
432 break; 408 break;
433 ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(unsigned char *)ctx->tmp,ctx->tmp_len); 409 ctx->buf_len = EVP_EncodeBlock(
434 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 410 (unsigned char *)ctx->buf,
411 (unsigned char *)ctx->tmp, ctx->tmp_len);
412 OPENSSL_assert(ctx->buf_len <=
413 (int)sizeof(ctx->buf));
435 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 414 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
436 /* Since we're now done using the temporary 415 /* Since we're now done using the temporary
437 buffer, the length should be 0'd */ 416 buffer, the length should be 0'd */
438 ctx->tmp_len=0; 417 ctx->tmp_len = 0;
439 } 418 } else {
440 else 419 if (n < 3) {
441 { 420 memcpy(ctx->tmp, in, n);
442 if (n < 3) 421 ctx->tmp_len = n;
443 {
444 memcpy(ctx->tmp,in,n);
445 ctx->tmp_len=n;
446 ret += n; 422 ret += n;
447 break; 423 break;
448 } 424 }
449 n-=n%3; 425 n -= n % 3;
450 ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(const unsigned char *)in,n); 426 ctx->buf_len = EVP_EncodeBlock(
451 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 427 (unsigned char *)ctx->buf,
428 (const unsigned char *)in, n);
429 OPENSSL_assert(ctx->buf_len <=
430 (int)sizeof(ctx->buf));
452 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 431 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
453 ret += n; 432 ret += n;
454 }
455 } 433 }
456 else 434 } else {
457 {
458 EVP_EncodeUpdate(&(ctx->base64), 435 EVP_EncodeUpdate(&(ctx->base64),
459 (unsigned char *)ctx->buf,&ctx->buf_len, 436 (unsigned char *)ctx->buf, &ctx->buf_len,
460 (unsigned char *)in,n); 437 (unsigned char *)in, n);
461 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf)); 438 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
462 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 439 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
463 ret += n; 440 ret += n;
464 } 441 }
465 inl-=n; 442 inl -= n;
466 in+=n; 443 in += n;
467 444
468 ctx->buf_off=0; 445 ctx->buf_off = 0;
469 n=ctx->buf_len; 446 n = ctx->buf_len;
470 while (n > 0) 447 while (n > 0) {
471 { 448 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
472 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 449 if (i <= 0) {
473 if (i <= 0)
474 {
475 BIO_copy_next_retry(b); 450 BIO_copy_next_retry(b);
476 return((ret == 0)?i:ret); 451 return ((ret == 0) ? i : ret);
477 } 452 }
478 OPENSSL_assert(i <= n); 453 OPENSSL_assert(i <= n);
479 n-=i; 454 n -= i;
480 ctx->buf_off+=i; 455 ctx->buf_off += i;
481 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf)); 456 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
482 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 457 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
483 }
484 ctx->buf_len=0;
485 ctx->buf_off=0;
486 } 458 }
487 return(ret); 459 ctx->buf_len = 0;
460 ctx->buf_off = 0;
488 } 461 }
462 return (ret);
463}
489 464
490static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) 465static long
491 { 466b64_ctrl(BIO *b, int cmd, long num, void *ptr)
467{
492 BIO_B64_CTX *ctx; 468 BIO_B64_CTX *ctx;
493 long ret=1; 469 long ret = 1;
494 int i; 470 int i;
495 471
496 ctx=(BIO_B64_CTX *)b->ptr; 472 ctx = (BIO_B64_CTX *)b->ptr;
497 473
498 switch (cmd) 474 switch (cmd) {
499 {
500 case BIO_CTRL_RESET: 475 case BIO_CTRL_RESET:
501 ctx->cont=1; 476 ctx->cont = 1;
502 ctx->start=1; 477 ctx->start = 1;
503 ctx->encode=B64_NONE; 478 ctx->encode = B64_NONE;
504 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 479 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
505 break; 480 break;
506 case BIO_CTRL_EOF: /* More to read */ 481 case BIO_CTRL_EOF: /* More to read */
507 if (ctx->cont <= 0) 482 if (ctx->cont <= 0)
508 ret=1; 483 ret = 1;
509 else 484 else
510 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 485 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
511 break; 486 break;
512 case BIO_CTRL_WPENDING: /* More to write in buffer */ 487 case BIO_CTRL_WPENDING: /* More to write in buffer */
513 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 488 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
514 ret=ctx->buf_len-ctx->buf_off; 489 ret = ctx->buf_len - ctx->buf_off;
515 if ((ret == 0) && (ctx->encode != B64_NONE) 490 if ((ret == 0) && (ctx->encode != B64_NONE) &&
516 && (ctx->base64.num != 0)) 491 (ctx->base64.num != 0))
517 ret=1; 492 ret = 1;
518 else if (ret <= 0) 493 else if (ret <= 0)
519 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 494 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
520 break; 495 break;
521 case BIO_CTRL_PENDING: /* More to read in buffer */ 496 case BIO_CTRL_PENDING: /* More to read in buffer */
522 OPENSSL_assert(ctx->buf_len >= ctx->buf_off); 497 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
523 ret=ctx->buf_len-ctx->buf_off; 498 ret = ctx->buf_len - ctx->buf_off;
524 if (ret <= 0) 499 if (ret <= 0)
525 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 500 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
526 break; 501 break;
527 case BIO_CTRL_FLUSH: 502 case BIO_CTRL_FLUSH:
528 /* do a final write */ 503 /* do a final write */
529again: 504again:
530 while (ctx->buf_len != ctx->buf_off) 505 while (ctx->buf_len != ctx->buf_off) {
531 { 506 i = b64_write(b, NULL, 0);
532 i=b64_write(b,NULL,0);
533 if (i < 0) 507 if (i < 0)
534 return i; 508 return i;
535 } 509 }
536 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) 510 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
537 { 511 if (ctx->tmp_len != 0) {
538 if (ctx->tmp_len != 0) 512 ctx->buf_len = EVP_EncodeBlock(
539 { 513 (unsigned char *)ctx->buf,
540 ctx->buf_len=EVP_EncodeBlock( 514 (unsigned char *)ctx->tmp,
541 (unsigned char *)ctx->buf, 515 ctx->tmp_len);
542 (unsigned char *)ctx->tmp, 516 ctx->buf_off = 0;
543 ctx->tmp_len); 517 ctx->tmp_len = 0;
544 ctx->buf_off=0;
545 ctx->tmp_len=0;
546 goto again; 518 goto again;
547 }
548 } 519 }
549 else if (ctx->encode != B64_NONE && ctx->base64.num != 0) 520 } else if (ctx->encode != B64_NONE && ctx->base64.num != 0) {
550 { 521 ctx->buf_off = 0;
551 ctx->buf_off=0;
552 EVP_EncodeFinal(&(ctx->base64), 522 EVP_EncodeFinal(&(ctx->base64),
553 (unsigned char *)ctx->buf, 523 (unsigned char *)ctx->buf,
554 &(ctx->buf_len)); 524 &(ctx->buf_len));
555 /* push out the bytes */ 525 /* push out the bytes */
556 goto again; 526 goto again;
557 } 527 }
558 /* Finally flush the underlying BIO */ 528 /* Finally flush the underlying BIO */
559 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 529 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
560 break; 530 break;
561 531
562 case BIO_C_DO_STATE_MACHINE: 532 case BIO_C_DO_STATE_MACHINE:
563 BIO_clear_retry_flags(b); 533 BIO_clear_retry_flags(b);
564 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 534 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
565 BIO_copy_next_retry(b); 535 BIO_copy_next_retry(b);
566 break; 536 break;
567 537
@@ -571,27 +541,29 @@ again:
571 case BIO_CTRL_GET: 541 case BIO_CTRL_GET:
572 case BIO_CTRL_SET: 542 case BIO_CTRL_SET:
573 default: 543 default:
574 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 544 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
575 break; 545 break;
576 }
577 return(ret);
578 } 546 }
547 return (ret);
548}
579 549
580static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 550static long
581 { 551b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
582 long ret=1; 552{
553 long ret = 1;
583 554
584 if (b->next_bio == NULL) return(0); 555 if (b->next_bio == NULL)
585 switch (cmd) 556 return (0);
586 { 557 switch (cmd) {
587 default: 558 default:
588 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 559 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
589 break; 560 break;
590 }
591 return(ret);
592 }
593
594static int b64_puts(BIO *b, const char *str)
595 {
596 return b64_write(b,str,strlen(str));
597 } 561 }
562 return (ret);
563}
564
565static int
566b64_puts(BIO *b, const char *str)
567{
568 return b64_write(b, str, strlen(str));
569}
diff --git a/src/lib/libssl/src/crypto/evp/bio_enc.c b/src/lib/libssl/src/crypto/evp/bio_enc.c
index 3362c25768..46cd2a6a60 100644
--- a/src/lib/libssl/src/crypto/evp/bio_enc.c
+++ b/src/lib/libssl/src/crypto/evp/bio_enc.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
@@ -73,8 +73,7 @@ static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
73#define ENC_BLOCK_SIZE (1024*4) 73#define ENC_BLOCK_SIZE (1024*4)
74#define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2) 74#define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2)
75 75
76typedef struct enc_struct 76typedef struct enc_struct {
77 {
78 int buf_len; 77 int buf_len;
79 int buf_off; 78 int buf_off;
80 int cont; /* <= 0 when finished */ 79 int cont; /* <= 0 when finished */
@@ -84,10 +83,10 @@ typedef struct enc_struct
84 /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate 83 /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
85 * can return up to a block more data than is presented to it 84 * can return up to a block more data than is presented to it
86 */ 85 */
87 char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2]; 86 char buf[ENC_BLOCK_SIZE + BUF_OFFSET + 2];
88 } BIO_ENC_CTX; 87} BIO_ENC_CTX;
89 88
90static BIO_METHOD methods_enc= { 89static BIO_METHOD methods_enc = {
91 .type = BIO_TYPE_CIPHER, 90 .type = BIO_TYPE_CIPHER,
92 .name = "cipher", 91 .name = "cipher",
93 .bwrite = enc_write, 92 .bwrite = enc_write,
@@ -98,292 +97,292 @@ static BIO_METHOD methods_enc= {
98 .callback_ctrl = enc_callback_ctrl 97 .callback_ctrl = enc_callback_ctrl
99}; 98};
100 99
101BIO_METHOD *BIO_f_cipher(void) 100BIO_METHOD *
102 { 101BIO_f_cipher(void)
103 return(&methods_enc); 102{
104 } 103 return (&methods_enc);
104}
105 105
106static int enc_new(BIO *bi) 106static int
107 { 107enc_new(BIO *bi)
108{
108 BIO_ENC_CTX *ctx; 109 BIO_ENC_CTX *ctx;
109 110
110 ctx=(BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX)); 111 ctx = (BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX));
111 if (ctx == NULL) return(0); 112 if (ctx == NULL)
113 return (0);
112 EVP_CIPHER_CTX_init(&ctx->cipher); 114 EVP_CIPHER_CTX_init(&ctx->cipher);
113 115
114 ctx->buf_len=0; 116 ctx->buf_len = 0;
115 ctx->buf_off=0; 117 ctx->buf_off = 0;
116 ctx->cont=1; 118 ctx->cont = 1;
117 ctx->finished=0; 119 ctx->finished = 0;
118 ctx->ok=1; 120 ctx->ok = 1;
119 121
120 bi->init=0; 122 bi->init = 0;
121 bi->ptr=(char *)ctx; 123 bi->ptr = (char *)ctx;
122 bi->flags=0; 124 bi->flags = 0;
123 return(1); 125 return (1);
124 } 126}
125 127
126static int enc_free(BIO *a) 128static int
127 { 129enc_free(BIO *a)
130{
128 BIO_ENC_CTX *b; 131 BIO_ENC_CTX *b;
129 132
130 if (a == NULL) return(0); 133 if (a == NULL)
131 b=(BIO_ENC_CTX *)a->ptr; 134 return (0);
135 b = (BIO_ENC_CTX *)a->ptr;
132 EVP_CIPHER_CTX_cleanup(&(b->cipher)); 136 EVP_CIPHER_CTX_cleanup(&(b->cipher));
133 OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX)); 137 OPENSSL_cleanse(a->ptr, sizeof(BIO_ENC_CTX));
134 free(a->ptr); 138 free(a->ptr);
135 a->ptr=NULL; 139 a->ptr = NULL;
136 a->init=0; 140 a->init = 0;
137 a->flags=0; 141 a->flags = 0;
138 return(1); 142 return (1);
139 } 143}
140 144
141static int enc_read(BIO *b, char *out, int outl) 145static int
142 { 146enc_read(BIO *b, char *out, int outl)
143 int ret=0,i; 147{
148 int ret = 0, i;
144 BIO_ENC_CTX *ctx; 149 BIO_ENC_CTX *ctx;
145 150
146 if (out == NULL) return(0); 151 if (out == NULL)
147 ctx=(BIO_ENC_CTX *)b->ptr; 152 return (0);
153 ctx = (BIO_ENC_CTX *)b->ptr;
148 154
149 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 155 if ((ctx == NULL) || (b->next_bio == NULL))
156 return (0);
150 157
151 /* First check if there are bytes decoded/encoded */ 158 /* First check if there are bytes decoded/encoded */
152 if (ctx->buf_len > 0) 159 if (ctx->buf_len > 0) {
153 { 160 i = ctx->buf_len - ctx->buf_off;
154 i=ctx->buf_len-ctx->buf_off; 161 if (i > outl)
155 if (i > outl) i=outl; 162 i = outl;
156 memcpy(out,&(ctx->buf[ctx->buf_off]),i); 163 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
157 ret=i; 164 ret = i;
158 out+=i; 165 out += i;
159 outl-=i; 166 outl -= i;
160 ctx->buf_off+=i; 167 ctx->buf_off += i;
161 if (ctx->buf_len == ctx->buf_off) 168 if (ctx->buf_len == ctx->buf_off) {
162 { 169 ctx->buf_len = 0;
163 ctx->buf_len=0; 170 ctx->buf_off = 0;
164 ctx->buf_off=0;
165 }
166 } 171 }
172 }
167 173
168 /* At this point, we have room of outl bytes and an empty 174 /* At this point, we have room of outl bytes and an empty
169 * buffer, so we should read in some more. */ 175 * buffer, so we should read in some more. */
170 176
171 while (outl > 0) 177 while (outl > 0) {
172 { 178 if (ctx->cont <= 0)
173 if (ctx->cont <= 0) break; 179 break;
174 180
175 /* read in at IV offset, read the EVP_Cipher 181 /* read in at IV offset, read the EVP_Cipher
176 * documentation about why */ 182 * documentation about why */
177 i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE); 183 i = BIO_read(b->next_bio, &(ctx->buf[BUF_OFFSET]), ENC_BLOCK_SIZE);
178 184
179 if (i <= 0) 185 if (i <= 0) {
180 {
181 /* Should be continue next time we are called? */ 186 /* Should be continue next time we are called? */
182 if (!BIO_should_retry(b->next_bio)) 187 if (!BIO_should_retry(b->next_bio)) {
183 { 188 ctx->cont = i;
184 ctx->cont=i; 189 i = EVP_CipherFinal_ex(&(ctx->cipher),
185 i=EVP_CipherFinal_ex(&(ctx->cipher), 190 (unsigned char *)ctx->buf,
186 (unsigned char *)ctx->buf, 191 &(ctx->buf_len));
187 &(ctx->buf_len)); 192 ctx->ok = i;
188 ctx->ok=i; 193 ctx->buf_off = 0;
189 ctx->buf_off=0; 194 } else {
190 } 195 ret = (ret == 0) ? i : ret;
191 else
192 {
193 ret=(ret == 0)?i:ret;
194 break; 196 break;
195 }
196 } 197 }
197 else 198 } else {
198 {
199 EVP_CipherUpdate(&(ctx->cipher), 199 EVP_CipherUpdate(&(ctx->cipher),
200 (unsigned char *)ctx->buf,&ctx->buf_len, 200 (unsigned char *)ctx->buf, &ctx->buf_len,
201 (unsigned char *)&(ctx->buf[BUF_OFFSET]),i); 201 (unsigned char *)&(ctx->buf[BUF_OFFSET]), i);
202 ctx->cont=1; 202 ctx->cont = 1;
203 /* Note: it is possible for EVP_CipherUpdate to 203 /* Note: it is possible for EVP_CipherUpdate to
204 * decrypt zero bytes because this is or looks like 204 * decrypt zero bytes because this is or looks like
205 * the final block: if this happens we should retry 205 * the final block: if this happens we should retry
206 * and either read more data or decrypt the final 206 * and either read more data or decrypt the final
207 * block 207 * block
208 */ 208 */
209 if(ctx->buf_len == 0) continue; 209 if (ctx->buf_len == 0)
210 } 210 continue;
211 }
211 212
212 if (ctx->buf_len <= outl) 213 if (ctx->buf_len <= outl)
213 i=ctx->buf_len; 214 i = ctx->buf_len;
214 else 215 else
215 i=outl; 216 i = outl;
216 if (i <= 0) break; 217 if (i <= 0)
217 memcpy(out,ctx->buf,i); 218 break;
218 ret+=i; 219 memcpy(out, ctx->buf, i);
219 ctx->buf_off=i; 220 ret += i;
220 outl-=i; 221 ctx->buf_off = i;
221 out+=i; 222 outl -= i;
222 } 223 out += i;
224 }
223 225
224 BIO_clear_retry_flags(b); 226 BIO_clear_retry_flags(b);
225 BIO_copy_next_retry(b); 227 BIO_copy_next_retry(b);
226 return((ret == 0)?ctx->cont:ret); 228 return ((ret == 0) ? ctx->cont : ret);
227 } 229}
228 230
229static int enc_write(BIO *b, const char *in, int inl) 231static int
230 { 232enc_write(BIO *b, const char *in, int inl)
231 int ret=0,n,i; 233{
234 int ret = 0, n, i;
232 BIO_ENC_CTX *ctx; 235 BIO_ENC_CTX *ctx;
233 236
234 ctx=(BIO_ENC_CTX *)b->ptr; 237 ctx = (BIO_ENC_CTX *)b->ptr;
235 ret=inl; 238 ret = inl;
236 239
237 BIO_clear_retry_flags(b); 240 BIO_clear_retry_flags(b);
238 n=ctx->buf_len-ctx->buf_off; 241 n = ctx->buf_len - ctx->buf_off;
239 while (n > 0) 242 while (n > 0) {
240 { 243 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
241 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 244 if (i <= 0) {
242 if (i <= 0)
243 {
244 BIO_copy_next_retry(b); 245 BIO_copy_next_retry(b);
245 return(i); 246 return (i);
246 }
247 ctx->buf_off+=i;
248 n-=i;
249 } 247 }
248 ctx->buf_off += i;
249 n -= i;
250 }
250 /* at this point all pending data has been written */ 251 /* at this point all pending data has been written */
251 252
252 if ((in == NULL) || (inl <= 0)) return(0); 253 if ((in == NULL) || (inl <= 0))
254 return (0);
253 255
254 ctx->buf_off=0; 256 ctx->buf_off = 0;
255 while (inl > 0) 257 while (inl > 0) {
256 { 258 n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
257 n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
258 EVP_CipherUpdate(&(ctx->cipher), 259 EVP_CipherUpdate(&(ctx->cipher),
259 (unsigned char *)ctx->buf,&ctx->buf_len, 260 (unsigned char *)ctx->buf, &ctx->buf_len,
260 (unsigned char *)in,n); 261 (unsigned char *)in, n);
261 inl-=n; 262 inl -= n;
262 in+=n; 263 in += n;
263 264
264 ctx->buf_off=0; 265 ctx->buf_off = 0;
265 n=ctx->buf_len; 266 n = ctx->buf_len;
266 while (n > 0) 267 while (n > 0) {
267 { 268 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
268 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); 269 if (i <= 0) {
269 if (i <= 0)
270 {
271 BIO_copy_next_retry(b); 270 BIO_copy_next_retry(b);
272 return (ret == inl) ? i : ret - inl; 271 return (ret == inl) ? i : ret - inl;
273 }
274 n-=i;
275 ctx->buf_off+=i;
276 } 272 }
277 ctx->buf_len=0; 273 n -= i;
278 ctx->buf_off=0; 274 ctx->buf_off += i;
279 } 275 }
280 BIO_copy_next_retry(b); 276 ctx->buf_len = 0;
281 return(ret); 277 ctx->buf_off = 0;
282 } 278 }
279 BIO_copy_next_retry(b);
280 return (ret);
281}
283 282
284static long enc_ctrl(BIO *b, int cmd, long num, void *ptr) 283static long
285 { 284enc_ctrl(BIO *b, int cmd, long num, void *ptr)
285{
286 BIO *dbio; 286 BIO *dbio;
287 BIO_ENC_CTX *ctx,*dctx; 287 BIO_ENC_CTX *ctx, *dctx;
288 long ret=1; 288 long ret = 1;
289 int i; 289 int i;
290 EVP_CIPHER_CTX **c_ctx; 290 EVP_CIPHER_CTX **c_ctx;
291 291
292 ctx=(BIO_ENC_CTX *)b->ptr; 292 ctx = (BIO_ENC_CTX *)b->ptr;
293 293
294 switch (cmd) 294 switch (cmd) {
295 {
296 case BIO_CTRL_RESET: 295 case BIO_CTRL_RESET:
297 ctx->ok=1; 296 ctx->ok = 1;
298 ctx->finished=0; 297 ctx->finished = 0;
299 EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL, 298 EVP_CipherInit_ex(&(ctx->cipher), NULL, NULL, NULL, NULL,
300 ctx->cipher.encrypt); 299 ctx->cipher.encrypt);
301 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 300 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
302 break; 301 break;
303 case BIO_CTRL_EOF: /* More to read */ 302 case BIO_CTRL_EOF: /* More to read */
304 if (ctx->cont <= 0) 303 if (ctx->cont <= 0)
305 ret=1; 304 ret = 1;
306 else 305 else
307 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 306 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
308 break; 307 break;
309 case BIO_CTRL_WPENDING: 308 case BIO_CTRL_WPENDING:
310 ret=ctx->buf_len-ctx->buf_off; 309 ret = ctx->buf_len - ctx->buf_off;
311 if (ret <= 0) 310 if (ret <= 0)
312 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 311 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
313 break; 312 break;
314 case BIO_CTRL_PENDING: /* More to read in buffer */ 313 case BIO_CTRL_PENDING: /* More to read in buffer */
315 ret=ctx->buf_len-ctx->buf_off; 314 ret = ctx->buf_len - ctx->buf_off;
316 if (ret <= 0) 315 if (ret <= 0)
317 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 316 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
318 break; 317 break;
319 case BIO_CTRL_FLUSH: 318 case BIO_CTRL_FLUSH:
320 /* do a final write */ 319 /* do a final write */
321again: 320again:
322 while (ctx->buf_len != ctx->buf_off) 321 while (ctx->buf_len != ctx->buf_off) {
323 { 322 i = enc_write(b, NULL, 0);
324 i=enc_write(b,NULL,0);
325 if (i < 0) 323 if (i < 0)
326 return i; 324 return i;
327 } 325 }
328 326
329 if (!ctx->finished) 327 if (!ctx->finished) {
330 { 328 ctx->finished = 1;
331 ctx->finished=1; 329 ctx->buf_off = 0;
332 ctx->buf_off=0; 330 ret = EVP_CipherFinal_ex(&(ctx->cipher),
333 ret=EVP_CipherFinal_ex(&(ctx->cipher), 331 (unsigned char *)ctx->buf,
334 (unsigned char *)ctx->buf, 332 &(ctx->buf_len));
335 &(ctx->buf_len)); 333 ctx->ok = (int)ret;
336 ctx->ok=(int)ret; 334 if (ret <= 0)
337 if (ret <= 0) break; 335 break;
338 336
339 /* push out the bytes */ 337 /* push out the bytes */
340 goto again; 338 goto again;
341 } 339 }
342 340
343 /* Finally flush the underlying BIO */ 341 /* Finally flush the underlying BIO */
344 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 342 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
345 break; 343 break;
346 case BIO_C_GET_CIPHER_STATUS: 344 case BIO_C_GET_CIPHER_STATUS:
347 ret=(long)ctx->ok; 345 ret = (long)ctx->ok;
348 break; 346 break;
349 case BIO_C_DO_STATE_MACHINE: 347 case BIO_C_DO_STATE_MACHINE:
350 BIO_clear_retry_flags(b); 348 BIO_clear_retry_flags(b);
351 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 349 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
352 BIO_copy_next_retry(b); 350 BIO_copy_next_retry(b);
353 break; 351 break;
354 case BIO_C_GET_CIPHER_CTX: 352 case BIO_C_GET_CIPHER_CTX:
355 c_ctx=(EVP_CIPHER_CTX **)ptr; 353 c_ctx = (EVP_CIPHER_CTX **)ptr;
356 (*c_ctx)= &(ctx->cipher); 354 (*c_ctx) = &(ctx->cipher);
357 b->init=1; 355 b->init = 1;
358 break; 356 break;
359 case BIO_CTRL_DUP: 357 case BIO_CTRL_DUP:
360 dbio=(BIO *)ptr; 358 dbio = (BIO *)ptr;
361 dctx=(BIO_ENC_CTX *)dbio->ptr; 359 dctx = (BIO_ENC_CTX *)dbio->ptr;
362 EVP_CIPHER_CTX_init(&dctx->cipher); 360 EVP_CIPHER_CTX_init(&dctx->cipher);
363 ret = EVP_CIPHER_CTX_copy(&dctx->cipher,&ctx->cipher); 361 ret = EVP_CIPHER_CTX_copy(&dctx->cipher, &ctx->cipher);
364 if (ret) 362 if (ret)
365 dbio->init=1; 363 dbio->init = 1;
366 break; 364 break;
367 default: 365 default:
368 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 366 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
369 break; 367 break;
370 }
371 return(ret);
372 } 368 }
369 return (ret);
370}
373 371
374static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 372static long
375 { 373enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
376 long ret=1; 374{
375 long ret = 1;
377 376
378 if (b->next_bio == NULL) return(0); 377 if (b->next_bio == NULL)
379 switch (cmd) 378 return (0);
380 { 379 switch (cmd) {
381 default: 380 default:
382 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 381 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
383 break; 382 break;
384 }
385 return(ret);
386 } 383 }
384 return (ret);
385}
387 386
388/* 387/*
389void BIO_set_cipher_ctx(b,c) 388void BIO_set_cipher_ctx(b,c)
@@ -399,28 +398,29 @@ EVP_CIPHER_ctx *c;
399 b->init=1; 398 b->init=1;
400 ctx=(BIO_ENC_CTX *)b->ptr; 399 ctx=(BIO_ENC_CTX *)b->ptr;
401 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); 400 memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
402 401
403 if (b->callback != NULL) 402 if (b->callback != NULL)
404 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); 403 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
405 } 404 }
406*/ 405*/
407 406
408void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, 407void
409 const unsigned char *i, int e) 408BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
410 { 409 const unsigned char *i, int e)
410{
411 BIO_ENC_CTX *ctx; 411 BIO_ENC_CTX *ctx;
412 412
413 if (b == NULL) return; 413 if (b == NULL)
414 return;
414 415
415 if ((b->callback != NULL) && 416 if ((b->callback != NULL) &&
416 (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0)) 417 (b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= 0))
417 return; 418 return;
418 419
419 b->init=1; 420 b->init = 1;
420 ctx=(BIO_ENC_CTX *)b->ptr; 421 ctx = (BIO_ENC_CTX *)b->ptr;
421 EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e); 422 EVP_CipherInit_ex(&(ctx->cipher), c,NULL, k, i, e);
422
423 if (b->callback != NULL)
424 b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
425 }
426 423
424 if (b->callback != NULL)
425 b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
426}
diff --git a/src/lib/libssl/src/crypto/evp/bio_md.c b/src/lib/libssl/src/crypto/evp/bio_md.c
index 85eead6c95..7ace6a4e89 100644
--- a/src/lib/libssl/src/crypto/evp/bio_md.c
+++ b/src/lib/libssl/src/crypto/evp/bio_md.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
@@ -72,7 +72,7 @@ static int md_gets(BIO *h, char *str, int size);
72static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2); 72static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
73static int md_new(BIO *h); 73static int md_new(BIO *h);
74static int md_free(BIO *data); 74static int md_free(BIO *data);
75static long md_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); 75static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
76 76
77static BIO_METHOD methods_md = { 77static BIO_METHOD methods_md = {
78 .type = BIO_TYPE_MD, 78 .type = BIO_TYPE_MD,
@@ -86,182 +86,186 @@ static BIO_METHOD methods_md = {
86 .callback_ctrl = md_callback_ctrl 86 .callback_ctrl = md_callback_ctrl
87}; 87};
88 88
89BIO_METHOD *BIO_f_md(void) 89BIO_METHOD *
90 { 90BIO_f_md(void)
91 return(&methods_md); 91{
92 } 92 return (&methods_md);
93}
93 94
94static int md_new(BIO *bi) 95static int
95 { 96md_new(BIO *bi)
97{
96 EVP_MD_CTX *ctx; 98 EVP_MD_CTX *ctx;
97 99
98 ctx=EVP_MD_CTX_create(); 100 ctx = EVP_MD_CTX_create();
99 if (ctx == NULL) return(0); 101 if (ctx == NULL)
102 return (0);
100 103
101 bi->init=0; 104 bi->init = 0;
102 bi->ptr=(char *)ctx; 105 bi->ptr = (char *)ctx;
103 bi->flags=0; 106 bi->flags = 0;
104 return(1); 107 return (1);
105 } 108}
106 109
107static int md_free(BIO *a) 110static int
108 { 111md_free(BIO *a)
109 if (a == NULL) return(0); 112{
113 if (a == NULL)
114 return (0);
110 EVP_MD_CTX_destroy(a->ptr); 115 EVP_MD_CTX_destroy(a->ptr);
111 a->ptr=NULL; 116 a->ptr = NULL;
112 a->init=0; 117 a->init = 0;
113 a->flags=0; 118 a->flags = 0;
114 return(1); 119 return (1);
115 } 120}
116 121
117static int md_read(BIO *b, char *out, int outl) 122static int
118 { 123md_read(BIO *b, char *out, int outl)
119 int ret=0; 124{
125 int ret = 0;
120 EVP_MD_CTX *ctx; 126 EVP_MD_CTX *ctx;
121 127
122 if (out == NULL) return(0); 128 if (out == NULL)
123 ctx=b->ptr; 129 return (0);
130 ctx = b->ptr;
124 131
125 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 132 if ((ctx == NULL) || (b->next_bio == NULL))
133 return (0);
126 134
127 ret=BIO_read(b->next_bio,out,outl); 135 ret = BIO_read(b->next_bio, out, outl);
128 if (b->init) 136 if (b->init) {
129 { 137 if (ret > 0) {
130 if (ret > 0) 138 if (EVP_DigestUpdate(ctx, (unsigned char *)out,
131 { 139 (unsigned int)ret) <= 0)
132 if (EVP_DigestUpdate(ctx,(unsigned char *)out, 140 return (-1);
133 (unsigned int)ret)<=0) return (-1);
134 }
135 } 141 }
142 }
136 BIO_clear_retry_flags(b); 143 BIO_clear_retry_flags(b);
137 BIO_copy_next_retry(b); 144 BIO_copy_next_retry(b);
138 return(ret); 145 return (ret);
139 } 146}
140 147
141static int md_write(BIO *b, const char *in, int inl) 148static int
142 { 149md_write(BIO *b, const char *in, int inl)
143 int ret=0; 150{
151 int ret = 0;
144 EVP_MD_CTX *ctx; 152 EVP_MD_CTX *ctx;
145 153
146 if ((in == NULL) || (inl <= 0)) return(0); 154 if ((in == NULL) || (inl <= 0))
147 ctx=b->ptr; 155 return (0);
156 ctx = b->ptr;
148 157
149 if ((ctx != NULL) && (b->next_bio != NULL)) 158 if ((ctx != NULL) && (b->next_bio != NULL))
150 ret=BIO_write(b->next_bio,in,inl); 159 ret = BIO_write(b->next_bio, in, inl);
151 if (b->init) 160 if (b->init) {
152 { 161 if (ret > 0) {
153 if (ret > 0) 162 if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
154 { 163 (unsigned int)ret)) {
155 if (!EVP_DigestUpdate(ctx,(const unsigned char *)in,
156 (unsigned int)ret))
157 {
158 BIO_clear_retry_flags(b); 164 BIO_clear_retry_flags(b);
159 return 0; 165 return 0;
160 }
161 } 166 }
162 } 167 }
163 if(b->next_bio != NULL) 168 }
164 { 169 if (b->next_bio != NULL) {
165 BIO_clear_retry_flags(b); 170 BIO_clear_retry_flags(b);
166 BIO_copy_next_retry(b); 171 BIO_copy_next_retry(b);
167 }
168 return(ret);
169 } 172 }
173 return (ret);
174}
170 175
171static long md_ctrl(BIO *b, int cmd, long num, void *ptr) 176static long
172 { 177md_ctrl(BIO *b, int cmd, long num, void *ptr)
173 EVP_MD_CTX *ctx,*dctx,**pctx; 178{
179 EVP_MD_CTX *ctx, *dctx, **pctx;
174 const EVP_MD **ppmd; 180 const EVP_MD **ppmd;
175 EVP_MD *md; 181 EVP_MD *md;
176 long ret=1; 182 long ret = 1;
177 BIO *dbio; 183 BIO *dbio;
178 184
179 ctx=b->ptr; 185 ctx = b->ptr;
180 186
181 switch (cmd) 187 switch (cmd) {
182 {
183 case BIO_CTRL_RESET: 188 case BIO_CTRL_RESET:
184 if (b->init) 189 if (b->init)
185 ret = EVP_DigestInit_ex(ctx,ctx->digest, NULL); 190 ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
186 else 191 else
187 ret=0; 192 ret = 0;
188 if (ret > 0) 193 if (ret > 0)
189 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 194 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
190 break; 195 break;
191 case BIO_C_GET_MD: 196 case BIO_C_GET_MD:
192 if (b->init) 197 if (b->init) {
193 { 198 ppmd = ptr;
194 ppmd=ptr; 199 *ppmd = ctx->digest;
195 *ppmd=ctx->digest; 200 } else
196 } 201 ret = 0;
197 else
198 ret=0;
199 break; 202 break;
200 case BIO_C_GET_MD_CTX: 203 case BIO_C_GET_MD_CTX:
201 pctx=ptr; 204 pctx = ptr;
202 *pctx=ctx; 205 *pctx = ctx;
203 b->init = 1; 206 b->init = 1;
204 break; 207 break;
205 case BIO_C_SET_MD_CTX: 208 case BIO_C_SET_MD_CTX:
206 if (b->init) 209 if (b->init)
207 b->ptr=ptr; 210 b->ptr = ptr;
208 else 211 else
209 ret=0; 212 ret = 0;
210 break; 213 break;
211 case BIO_C_DO_STATE_MACHINE: 214 case BIO_C_DO_STATE_MACHINE:
212 BIO_clear_retry_flags(b); 215 BIO_clear_retry_flags(b);
213 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 216 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
214 BIO_copy_next_retry(b); 217 BIO_copy_next_retry(b);
215 break; 218 break;
216 219
217 case BIO_C_SET_MD: 220 case BIO_C_SET_MD:
218 md=ptr; 221 md = ptr;
219 ret = EVP_DigestInit_ex(ctx,md, NULL); 222 ret = EVP_DigestInit_ex(ctx, md, NULL);
220 if (ret > 0) 223 if (ret > 0)
221 b->init=1; 224 b->init = 1;
222 break; 225 break;
223 case BIO_CTRL_DUP: 226 case BIO_CTRL_DUP:
224 dbio=ptr; 227 dbio = ptr;
225 dctx=dbio->ptr; 228 dctx = dbio->ptr;
226 if (!EVP_MD_CTX_copy_ex(dctx,ctx)) 229 if (!EVP_MD_CTX_copy_ex(dctx, ctx))
227 return 0; 230 return 0;
228 b->init=1; 231 b->init = 1;
229 break; 232 break;
230 default: 233 default:
231 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 234 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
232 break; 235 break;
233 }
234 return(ret);
235 } 236 }
237 return (ret);
238}
236 239
237static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 240static long
238 { 241md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
239 long ret=1; 242{
243 long ret = 1;
240 244
241 if (b->next_bio == NULL) return(0); 245 if (b->next_bio == NULL)
242 switch (cmd) 246 return (0);
243 { 247 switch (cmd) {
244 default: 248 default:
245 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 249 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
246 break; 250 break;
247 }
248 return(ret);
249 } 251 }
252 return (ret);
253}
250 254
251static int md_gets(BIO *bp, char *buf, int size) 255static int
252 { 256md_gets(BIO *bp, char *buf, int size)
257{
253 EVP_MD_CTX *ctx; 258 EVP_MD_CTX *ctx;
254 unsigned int ret; 259 unsigned int ret;
255 260
256 261 ctx = bp->ptr;
257 ctx=bp->ptr;
258 if (size < ctx->digest->md_size) 262 if (size < ctx->digest->md_size)
259 return(0); 263 return (0);
260 if (EVP_DigestFinal_ex(ctx,(unsigned char *)buf,&ret)<=0) 264 if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
261 return -1; 265 return -1;
262 266
263 return((int)ret); 267 return ((int)ret);
264 } 268}
265 269
266/* 270/*
267static int md_puts(bp,str) 271static int md_puts(bp,str)
@@ -271,4 +275,3 @@ char *str;
271 return(-1); 275 return(-1);
272 } 276 }
273*/ 277*/
274
diff --git a/src/lib/libssl/src/crypto/evp/bio_ok.c b/src/lib/libssl/src/crypto/evp/bio_ok.c
index d0bcbc2bef..882aaee0fb 100644
--- a/src/lib/libssl/src/crypto/evp/bio_ok.c
+++ b/src/lib/libssl/src/crypto/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
146typedef struct ok_struct 146typedef 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
160static BIO_METHOD methods_ok = { 159static 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
171BIO_METHOD *BIO_f_reliable(void) 170BIO_METHOD *
172 { 171BIO_f_reliable(void)
173 return(&methods_ok); 172{
174 } 173 return (&methods_ok);
174}
175 175
176static int ok_new(BIO *bi) 176static int
177 { 177ok_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
200static int ok_free(BIO *a) 202static int
201 { 203ok_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
212static int ok_read(BIO *b, char *out, int outl) 216static int
213 { 217ok_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
297static int ok_write(BIO *b, const char *in, int inl) 297static int
298 { 298ok_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
362static long ok_ctrl(BIO *b, int cmd, long num, void *ptr) 362static long
363 { 363ok_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
450static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 446static long
451 { 447ok_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
464static void longswap(void *_ptr, size_t len) 461static void
462longswap(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
477static int sig_out(BIO* b) 475static int
478 { 476sig_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: 505berr:
506 BIO_clear_retry_flags(b); 506 BIO_clear_retry_flags(b);
507 return 0; 507 return 0;
508 } 508}
509 509
510static int sig_in(BIO* b) 510static int
511 { 511sig_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: 548berr:
550 BIO_clear_retry_flags(b); 549 BIO_clear_retry_flags(b);
551 return 0; 550 return 0;
552 } 551}
553 552
554static int block_out(BIO* b) 553static int
555 { 554block_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: 576berr:
577 BIO_clear_retry_flags(b); 577 BIO_clear_retry_flags(b);
578 return 0; 578 return 0;
579 } 579}
580 580
581static int block_in(BIO* b) 581static int
582 { 582block_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
622berr:
619 BIO_clear_retry_flags(b); 623 BIO_clear_retry_flags(b);
620 return 0; 624 return 0;
621 } 625}
622