RT4320/GH705: Fix PEM parsing bug.
[openssl.git] / crypto / bio / bf_lbuf.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57
58 #include <stdio.h>
59 #include <errno.h>
60 #include "internal/cryptlib.h"
61 #include <openssl/bio.h>
62 #include <openssl/evp.h>
63
64 static int linebuffer_write(BIO *h, const char *buf, int num);
65 static int linebuffer_read(BIO *h, char *buf, int size);
66 static int linebuffer_puts(BIO *h, const char *str);
67 static int linebuffer_gets(BIO *h, char *str, int size);
68 static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69 static int linebuffer_new(BIO *h);
70 static int linebuffer_free(BIO *data);
71 static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72
73 /* A 10k maximum should be enough for most purposes */
74 #define DEFAULT_LINEBUFFER_SIZE 1024*10
75
76 /* #define DEBUG */
77
78 static BIO_METHOD methods_linebuffer = {
79     BIO_TYPE_LINEBUFFER,
80     "linebuffer",
81     linebuffer_write,
82     linebuffer_read,
83     linebuffer_puts,
84     linebuffer_gets,
85     linebuffer_ctrl,
86     linebuffer_new,
87     linebuffer_free,
88     linebuffer_callback_ctrl,
89 };
90
91 BIO_METHOD *BIO_f_linebuffer(void)
92 {
93     return (&methods_linebuffer);
94 }
95
96 typedef struct bio_linebuffer_ctx_struct {
97     char *obuf;                 /* the output char array */
98     int obuf_size;              /* how big is the output buffer */
99     int obuf_len;               /* how many bytes are in it */
100 } BIO_LINEBUFFER_CTX;
101
102 static int linebuffer_new(BIO *bi)
103 {
104     BIO_LINEBUFFER_CTX *ctx;
105
106     ctx = OPENSSL_malloc(sizeof(*ctx));
107     if (ctx == NULL)
108         return (0);
109     ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
110     if (ctx->obuf == NULL) {
111         OPENSSL_free(ctx);
112         return (0);
113     }
114     ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
115     ctx->obuf_len = 0;
116
117     bi->init = 1;
118     bi->ptr = (char *)ctx;
119     bi->flags = 0;
120     return (1);
121 }
122
123 static int linebuffer_free(BIO *a)
124 {
125     BIO_LINEBUFFER_CTX *b;
126
127     if (a == NULL)
128         return (0);
129     b = (BIO_LINEBUFFER_CTX *)a->ptr;
130     OPENSSL_free(b->obuf);
131     OPENSSL_free(a->ptr);
132     a->ptr = NULL;
133     a->init = 0;
134     a->flags = 0;
135     return (1);
136 }
137
138 static int linebuffer_read(BIO *b, char *out, int outl)
139 {
140     int ret = 0;
141
142     if (out == NULL)
143         return (0);
144     if (b->next_bio == NULL)
145         return (0);
146     ret = BIO_read(b->next_bio, out, outl);
147     BIO_clear_retry_flags(b);
148     BIO_copy_next_retry(b);
149     return (ret);
150 }
151
152 static int linebuffer_write(BIO *b, const char *in, int inl)
153 {
154     int i, num = 0, foundnl;
155     BIO_LINEBUFFER_CTX *ctx;
156
157     if ((in == NULL) || (inl <= 0))
158         return (0);
159     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
160     if ((ctx == NULL) || (b->next_bio == NULL))
161         return (0);
162
163     BIO_clear_retry_flags(b);
164
165     do {
166         const char *p;
167
168         for (p = in; p < in + inl && *p != '\n'; p++) ;
169         if (*p == '\n') {
170             p++;
171             foundnl = 1;
172         } else
173             foundnl = 0;
174
175         /*
176          * If a NL was found and we already have text in the save buffer,
177          * concatenate them and write
178          */
179         while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
180                && ctx->obuf_len > 0) {
181             int orig_olen = ctx->obuf_len;
182
183             i = ctx->obuf_size - ctx->obuf_len;
184             if (p - in > 0) {
185                 if (i >= p - in) {
186                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
187                     ctx->obuf_len += p - in;
188                     inl -= p - in;
189                     num += p - in;
190                     in = p;
191                 } else {
192                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
193                     ctx->obuf_len += i;
194                     inl -= i;
195                     in += i;
196                     num += i;
197                 }
198             }
199             i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
200             if (i <= 0) {
201                 ctx->obuf_len = orig_olen;
202                 BIO_copy_next_retry(b);
203
204                 if (i < 0)
205                     return ((num > 0) ? num : i);
206                 if (i == 0)
207                     return (num);
208             }
209             if (i < ctx->obuf_len)
210                 memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
211             ctx->obuf_len -= i;
212         }
213
214         /*
215          * Now that the save buffer is emptied, let's write the input buffer
216          * if a NL was found and there is anything to write.
217          */
218         if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
219             i = BIO_write(b->next_bio, in, p - in);
220             if (i <= 0) {
221                 BIO_copy_next_retry(b);
222                 if (i < 0)
223                     return ((num > 0) ? num : i);
224                 if (i == 0)
225                     return (num);
226             }
227             num += i;
228             in += i;
229             inl -= i;
230         }
231     }
232     while (foundnl && inl > 0);
233     /*
234      * We've written as much as we can.  The rest of the input buffer, if
235      * any, is text that doesn't and with a NL and therefore needs to be
236      * saved for the next trip.
237      */
238     if (inl > 0) {
239         memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
240         ctx->obuf_len += inl;
241         num += inl;
242     }
243     return num;
244 }
245
246 static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
247 {
248     BIO *dbio;
249     BIO_LINEBUFFER_CTX *ctx;
250     long ret = 1;
251     char *p;
252     int r;
253     int obs;
254
255     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
256
257     switch (cmd) {
258     case BIO_CTRL_RESET:
259         ctx->obuf_len = 0;
260         if (b->next_bio == NULL)
261             return (0);
262         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
263         break;
264     case BIO_CTRL_INFO:
265         ret = (long)ctx->obuf_len;
266         break;
267     case BIO_CTRL_WPENDING:
268         ret = (long)ctx->obuf_len;
269         if (ret == 0) {
270             if (b->next_bio == NULL)
271                 return (0);
272             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
273         }
274         break;
275     case BIO_C_SET_BUFF_SIZE:
276         obs = (int)num;
277         p = ctx->obuf;
278         if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
279             p = OPENSSL_malloc((int)num);
280             if (p == NULL)
281                 goto malloc_error;
282         }
283         if (ctx->obuf != p) {
284             if (ctx->obuf_len > obs) {
285                 ctx->obuf_len = obs;
286             }
287             memcpy(p, ctx->obuf, ctx->obuf_len);
288             OPENSSL_free(ctx->obuf);
289             ctx->obuf = p;
290             ctx->obuf_size = obs;
291         }
292         break;
293     case BIO_C_DO_STATE_MACHINE:
294         if (b->next_bio == NULL)
295             return (0);
296         BIO_clear_retry_flags(b);
297         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
298         BIO_copy_next_retry(b);
299         break;
300
301     case BIO_CTRL_FLUSH:
302         if (b->next_bio == NULL)
303             return (0);
304         if (ctx->obuf_len <= 0) {
305             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
306             break;
307         }
308
309         for (;;) {
310             BIO_clear_retry_flags(b);
311             if (ctx->obuf_len > 0) {
312                 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
313                 BIO_copy_next_retry(b);
314                 if (r <= 0)
315                     return ((long)r);
316                 if (r < ctx->obuf_len)
317                     memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
318                 ctx->obuf_len -= r;
319             } else {
320                 ctx->obuf_len = 0;
321                 ret = 1;
322                 break;
323             }
324         }
325         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
326         break;
327     case BIO_CTRL_DUP:
328         dbio = (BIO *)ptr;
329         if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
330             ret = 0;
331         break;
332     default:
333         if (b->next_bio == NULL)
334             return (0);
335         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
336         break;
337     }
338     return (ret);
339  malloc_error:
340     BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
341     return (0);
342 }
343
344 static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
345 {
346     long ret = 1;
347
348     if (b->next_bio == NULL)
349         return (0);
350     switch (cmd) {
351     default:
352         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
353         break;
354     }
355     return (ret);
356 }
357
358 static int linebuffer_gets(BIO *b, char *buf, int size)
359 {
360     if (b->next_bio == NULL)
361         return (0);
362     return (BIO_gets(b->next_bio, buf, size));
363 }
364
365 static int linebuffer_puts(BIO *b, const char *str)
366 {
367     return (linebuffer_write(b, str, strlen(str)));
368 }