• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(__linux) || defined(__sun) || defined(__hpux)
58 // Following definition aliases fopen to fopen64 on above mentioned
59 // platforms. This makes it possible to open and sequentially access
60 // files larger than 2GB from 32-bit application. It does not allow to
61 // traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
62 // 32-bit platform permits that, not with fseek/ftell. Not to mention
63 // that breaking 2GB limit for seeking would require surgery to *our*
64 // API. But sequential access suffices for practical cases when you
65 // can run into large files, such as fingerprinting, so we can let API
66 // alone. For reference, the list of 32-bit platforms which allow for
67 // sequential access of large files without extra "magic" comprise *BSD,
68 // Darwin, IRIX...
69 #ifndef _FILE_OFFSET_BITS
70 #define _FILE_OFFSET_BITS 64
71 #endif
72 #endif
73 
74 #include <openssl/bio.h>
75 
76 #include <errno.h>
77 #include <stdio.h>
78 #include <string.h>
79 
80 #include <openssl/err.h>
81 #include <openssl/mem.h>
82 
83 #include "../internal.h"
84 
85 
86 #define BIO_FP_READ 0x02
87 #define BIO_FP_WRITE 0x04
88 #define BIO_FP_APPEND 0x08
89 
90 #if !defined(OPENSSL_NO_FILESYSTEM)
91 #define fopen_if_available fopen
92 #else
fopen_if_available(const char * path,const char * mode)93 static FILE *fopen_if_available(const char *path, const char *mode) {
94   errno = ENOENT;
95   return NULL;
96 }
97 #endif
98 
BIO_new_file(const char * filename,const char * mode)99 BIO *BIO_new_file(const char *filename, const char *mode) {
100   BIO *ret;
101   FILE *file;
102 
103   file = fopen_if_available(filename, mode);
104   if (file == NULL) {
105     OPENSSL_PUT_SYSTEM_ERROR();
106 
107     ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
108     if (errno == ENOENT) {
109       OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE);
110     } else {
111       OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB);
112     }
113     return NULL;
114   }
115 
116   ret = BIO_new_fp(file, BIO_CLOSE);
117   if (ret == NULL) {
118     fclose(file);
119     return NULL;
120   }
121 
122   return ret;
123 }
124 
BIO_new_fp(FILE * stream,int close_flag)125 BIO *BIO_new_fp(FILE *stream, int close_flag) {
126   BIO *ret = BIO_new(BIO_s_file());
127 
128   if (ret == NULL) {
129     return NULL;
130   }
131 
132   BIO_set_fp(ret, stream, close_flag);
133   return ret;
134 }
135 
file_free(BIO * bio)136 static int file_free(BIO *bio) {
137   if (!bio->shutdown) {
138     return 1;
139   }
140 
141   if (bio->init && bio->ptr != NULL) {
142     fclose(bio->ptr);
143     bio->ptr = NULL;
144   }
145   bio->init = 0;
146 
147   return 1;
148 }
149 
file_read(BIO * b,char * out,int outl)150 static int file_read(BIO *b, char *out, int outl) {
151   if (!b->init) {
152     return 0;
153   }
154 
155   size_t ret = fread(out, 1, outl, (FILE *)b->ptr);
156   if (ret == 0 && ferror((FILE *)b->ptr)) {
157     OPENSSL_PUT_SYSTEM_ERROR();
158     OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
159     return -1;
160   }
161 
162   // fread reads at most |outl| bytes, so |ret| fits in an int.
163   return (int)ret;
164 }
165 
file_write(BIO * b,const char * in,int inl)166 static int file_write(BIO *b, const char *in, int inl) {
167   if (!b->init) {
168     return 0;
169   }
170 
171   int ret = (int)fwrite(in, inl, 1, (FILE *)b->ptr);
172   if (ret > 0) {
173     ret = inl;
174   }
175   return ret;
176 }
177 
file_ctrl(BIO * b,int cmd,long num,void * ptr)178 static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
179   long ret = 1;
180   FILE *fp = (FILE *)b->ptr;
181   FILE **fpp;
182 
183   switch (cmd) {
184     case BIO_CTRL_RESET:
185       num = 0;
186       OPENSSL_FALLTHROUGH;
187     case BIO_C_FILE_SEEK:
188       ret = (long)fseek(fp, num, 0);
189       break;
190     case BIO_CTRL_EOF:
191       ret = (long)feof(fp);
192       break;
193     case BIO_C_FILE_TELL:
194     case BIO_CTRL_INFO:
195       ret = ftell(fp);
196       break;
197     case BIO_C_SET_FILE_PTR:
198       file_free(b);
199       b->shutdown = (int)num & BIO_CLOSE;
200       b->ptr = ptr;
201       b->init = 1;
202       break;
203     case BIO_C_SET_FILENAME:
204       file_free(b);
205       b->shutdown = (int)num & BIO_CLOSE;
206       const char *mode;
207       if (num & BIO_FP_APPEND) {
208         if (num & BIO_FP_READ) {
209           mode = "a+";
210         } else {
211           mode = "a";
212         }
213       } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
214         mode = "r+";
215       } else if (num & BIO_FP_WRITE) {
216         mode = "w";
217       } else if (num & BIO_FP_READ) {
218         mode = "r";
219       } else {
220         OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE);
221         ret = 0;
222         break;
223       }
224       fp = fopen_if_available(ptr, mode);
225       if (fp == NULL) {
226         OPENSSL_PUT_SYSTEM_ERROR();
227         ERR_add_error_data(5, "fopen('", ptr, "','", mode, "')");
228         OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
229         ret = 0;
230         break;
231       }
232       b->ptr = fp;
233       b->init = 1;
234       break;
235     case BIO_C_GET_FILE_PTR:
236       // the ptr parameter is actually a FILE ** in this case.
237       if (ptr != NULL) {
238         fpp = (FILE **)ptr;
239         *fpp = (FILE *)b->ptr;
240       }
241       break;
242     case BIO_CTRL_GET_CLOSE:
243       ret = (long)b->shutdown;
244       break;
245     case BIO_CTRL_SET_CLOSE:
246       b->shutdown = (int)num;
247       break;
248     case BIO_CTRL_FLUSH:
249       ret = 0 == fflush((FILE *)b->ptr);
250       break;
251     case BIO_CTRL_WPENDING:
252     case BIO_CTRL_PENDING:
253     default:
254       ret = 0;
255       break;
256   }
257   return ret;
258 }
259 
file_gets(BIO * bp,char * buf,int size)260 static int file_gets(BIO *bp, char *buf, int size) {
261   if (size == 0) {
262     return 0;
263   }
264 
265   if (!fgets(buf, size, (FILE *)bp->ptr)) {
266     buf[0] = 0;
267     // TODO(davidben): This doesn't distinguish error and EOF. This should check
268     // |ferror| as in |file_read|.
269     return 0;
270   }
271 
272   return (int)strlen(buf);
273 }
274 
275 static const BIO_METHOD methods_filep = {
276     BIO_TYPE_FILE,   "FILE pointer",
277     file_write,      file_read,
278     NULL /* puts */, file_gets,
279     file_ctrl,       NULL /* create */,
280     file_free,       NULL /* callback_ctrl */,
281 };
282 
BIO_s_file(void)283 const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
284 
285 
BIO_get_fp(BIO * bio,FILE ** out_file)286 int BIO_get_fp(BIO *bio, FILE **out_file) {
287   return (int)BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char *)out_file);
288 }
289 
BIO_set_fp(BIO * bio,FILE * file,int close_flag)290 int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
291   return (int)BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *)file);
292 }
293 
BIO_read_filename(BIO * bio,const char * filename)294 int BIO_read_filename(BIO *bio, const char *filename) {
295   return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
296                        (char *)filename);
297 }
298 
BIO_write_filename(BIO * bio,const char * filename)299 int BIO_write_filename(BIO *bio, const char *filename) {
300   return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
301                        (char *)filename);
302 }
303 
BIO_append_filename(BIO * bio,const char * filename)304 int BIO_append_filename(BIO *bio, const char *filename) {
305   return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
306                        (char *)filename);
307 }
308 
BIO_rw_filename(BIO * bio,const char * filename)309 int BIO_rw_filename(BIO *bio, const char *filename) {
310   return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME,
311                        BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE,
312                        (char *)filename);
313 }
314 
BIO_tell(BIO * bio)315 long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); }
316 
BIO_seek(BIO * bio,long offset)317 long BIO_seek(BIO *bio, long offset) {
318   return BIO_ctrl(bio, BIO_C_FILE_SEEK, offset, NULL);
319 }
320