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 #include <openssl/bio.h>
58 
59 #include <limits.h>
60 #include <string.h>
61 
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 
66 
BIO_new_mem_buf(void * buf,int len)67 BIO *BIO_new_mem_buf(void *buf, int len) {
68   BIO *ret;
69   BUF_MEM *b;
70   const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
71 
72   if (!buf && len != 0) {
73     OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER);
74     return NULL;
75   }
76 
77   ret = BIO_new(BIO_s_mem());
78   if (ret == NULL) {
79     return NULL;
80   }
81 
82   b = (BUF_MEM *)ret->ptr;
83   b->data = buf;
84   b->length = size;
85   b->max = size;
86 
87   ret->flags |= BIO_FLAGS_MEM_RDONLY;
88 
89   /* |num| is used to store the value that this BIO will return when it runs
90    * out of data. If it's negative then the retry flags will also be set. Since
91    * this is static data, retrying wont help */
92   ret->num = 0;
93 
94   return ret;
95 }
96 
mem_new(BIO * bio)97 static int mem_new(BIO *bio) {
98   BUF_MEM *b;
99 
100   b = BUF_MEM_new();
101   if (b == NULL) {
102     return 0;
103   }
104 
105   /* |shutdown| is used to store the close flag: whether the BIO has ownership
106    * of the BUF_MEM. */
107   bio->shutdown = 1;
108   bio->init = 1;
109   bio->num = -1;
110   bio->ptr = (char *)b;
111 
112   return 1;
113 }
114 
mem_free(BIO * bio)115 static int mem_free(BIO *bio) {
116   BUF_MEM *b;
117 
118   if (bio == NULL) {
119     return 0;
120   }
121 
122   if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
123     return 1;
124   }
125 
126   b = (BUF_MEM *)bio->ptr;
127   if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
128     b->data = NULL;
129   }
130   BUF_MEM_free(b);
131   bio->ptr = NULL;
132   return 1;
133 }
134 
mem_read(BIO * bio,char * out,int outl)135 static int mem_read(BIO *bio, char *out, int outl) {
136   int ret;
137   BUF_MEM *b = (BUF_MEM*) bio->ptr;
138 
139   BIO_clear_retry_flags(bio);
140   ret = outl;
141   if (b->length < INT_MAX && ret > (int)b->length) {
142     ret = b->length;
143   }
144 
145   if (ret > 0) {
146     memcpy(out, b->data, ret);
147     b->length -= ret;
148     if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
149       b->data += ret;
150     } else {
151       memmove(b->data, &b->data[ret], b->length);
152     }
153   } else if (b->length == 0) {
154     ret = bio->num;
155     if (ret != 0) {
156       BIO_set_retry_read(bio);
157     }
158   }
159   return ret;
160 }
161 
mem_write(BIO * bio,const char * in,int inl)162 static int mem_write(BIO *bio, const char *in, int inl) {
163   int ret = -1;
164   int blen;
165   BUF_MEM *b;
166 
167   b = (BUF_MEM *)bio->ptr;
168 
169   if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
170     OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
171     goto err;
172   }
173 
174   BIO_clear_retry_flags(bio);
175   blen = b->length;
176   if (INT_MAX - blen < inl) {
177     goto err;
178   }
179   if (BUF_MEM_grow_clean(b, blen + inl) != ((size_t) blen) + inl) {
180     goto err;
181   }
182   memcpy(&b->data[blen], in, inl);
183   ret = inl;
184 
185 err:
186   return ret;
187 }
188 
mem_puts(BIO * bp,const char * str)189 static int mem_puts(BIO *bp, const char *str) {
190   return mem_write(bp, str, strlen(str));
191 }
192 
mem_gets(BIO * bio,char * buf,int size)193 static int mem_gets(BIO *bio, char *buf, int size) {
194   int i, j;
195   char *p;
196   BUF_MEM *b = (BUF_MEM *)bio->ptr;
197 
198   BIO_clear_retry_flags(bio);
199   j = b->length;
200   if (size - 1 < j) {
201     j = size - 1;
202   }
203   if (j <= 0) {
204     if (size > 0) {
205       *buf = 0;
206     }
207     return 0;
208   }
209 
210   p = b->data;
211   for (i = 0; i < j; i++) {
212     if (p[i] == '\n') {
213       i++;
214       break;
215     }
216   }
217 
218   /* i is now the max num of bytes to copy, either j or up to and including the
219    * first newline */
220 
221   i = mem_read(bio, buf, i);
222   if (i > 0) {
223     buf[i] = '\0';
224   }
225   return i;
226 }
227 
mem_ctrl(BIO * bio,int cmd,long num,void * ptr)228 static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
229   long ret = 1;
230   char **pptr;
231 
232   BUF_MEM *b = (BUF_MEM *)bio->ptr;
233 
234   switch (cmd) {
235     case BIO_CTRL_RESET:
236       if (b->data != NULL) {
237         /* For read only case reset to the start again */
238         if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
239           b->data -= b->max - b->length;
240           b->length = b->max;
241         } else {
242           memset(b->data, 0, b->max);
243           b->length = 0;
244         }
245       }
246       break;
247     case BIO_CTRL_EOF:
248       ret = (long)(b->length == 0);
249       break;
250     case BIO_C_SET_BUF_MEM_EOF_RETURN:
251       bio->num = (int)num;
252       break;
253     case BIO_CTRL_INFO:
254       ret = (long)b->length;
255       if (ptr != NULL) {
256         pptr = (char **)ptr;
257         *pptr = (char *)&b->data[0];
258       }
259       break;
260     case BIO_C_SET_BUF_MEM:
261       mem_free(bio);
262       bio->shutdown = (int)num;
263       bio->ptr = ptr;
264       break;
265     case BIO_C_GET_BUF_MEM_PTR:
266       if (ptr != NULL) {
267         pptr = (char **)ptr;
268         *pptr = (char *)b;
269       }
270       break;
271     case BIO_CTRL_GET_CLOSE:
272       ret = (long)bio->shutdown;
273       break;
274     case BIO_CTRL_SET_CLOSE:
275       bio->shutdown = (int)num;
276       break;
277 
278     case BIO_CTRL_WPENDING:
279       ret = 0L;
280       break;
281     case BIO_CTRL_PENDING:
282       ret = (long)b->length;
283       break;
284     case BIO_CTRL_FLUSH:
285       ret = 1;
286       break;
287     default:
288       ret = 0;
289       break;
290   }
291   return ret;
292 }
293 
294 static const BIO_METHOD mem_method = {
295     BIO_TYPE_MEM, "memory buffer", mem_write, mem_read, mem_puts,
296     mem_gets,     mem_ctrl,        mem_new,   mem_free, NULL, };
297 
BIO_s_mem(void)298 const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
299 
BIO_mem_contents(const BIO * bio,const uint8_t ** out_contents,size_t * out_len)300 int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
301                      size_t *out_len) {
302   const BUF_MEM *b;
303   if (bio->method != &mem_method) {
304     return 0;
305   }
306 
307   b = (BUF_MEM *)bio->ptr;
308   *out_contents = (uint8_t *)b->data;
309   *out_len = b->length;
310   return 1;
311 }
312 
BIO_get_mem_data(BIO * bio,char ** contents)313 long BIO_get_mem_data(BIO *bio, char **contents) {
314   return BIO_ctrl(bio, BIO_CTRL_INFO, 0, (char *) contents);
315 }
316 
BIO_get_mem_ptr(BIO * bio,BUF_MEM ** out)317 int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) {
318   return BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, (char *) out);
319 }
320 
BIO_set_mem_buf(BIO * bio,BUF_MEM * b,int take_ownership)321 int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) {
322   return BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, (char *) b);
323 }
324 
BIO_set_mem_eof_return(BIO * bio,int eof_value)325 int BIO_set_mem_eof_return(BIO *bio, int eof_value) {
326   return BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
327 }
328