summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/bio_b64.c
diff options
context:
space:
mode:
authorjsing <>2014-05-03 16:22:28 +0000
committerjsing <>2014-05-03 16:22:28 +0000
commit65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8 (patch)
tree4993b7660557e78f309731f46f029f969f974e84 /src/lib/libcrypto/evp/bio_b64.c
parent0225d59faf0ea827794d5d88eb3e1dddb205242c (diff)
downloadopenbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.tar.gz
openbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.tar.bz2
openbsd-65940f4fdcb709b14e54a1c8f2d7e2e3d64fa2c8.zip
KNF.
Diffstat (limited to 'src/lib/libcrypto/evp/bio_b64.c')
-rw-r--r--src/lib/libcrypto/evp/bio_b64.c618
1 files changed, 295 insertions, 323 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}