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 | #if !defined(OPENSSL_TRUSTY) |
77 | |
78 | #include <errno.h> |
79 | #include <stdio.h> |
80 | #include <string.h> |
81 | |
82 | #include <openssl/buf.h> |
83 | #include <openssl/err.h> |
84 | #include <openssl/mem.h> |
85 | |
86 | #include "../internal.h" |
87 | |
88 | |
89 | #define BIO_FP_READ 0x02 |
90 | #define BIO_FP_WRITE 0x04 |
91 | #define BIO_FP_APPEND 0x08 |
92 | |
93 | BIO *BIO_new_file(const char *filename, const char *mode) { |
94 | BIO *ret; |
95 | FILE *file; |
96 | |
97 | file = fopen(filename, mode); |
98 | if (file == NULL) { |
99 | OPENSSL_PUT_SYSTEM_ERROR(); |
100 | |
101 | ERR_add_error_data(5, "fopen('" , filename, "','" , mode, "')" ); |
102 | if (errno == ENOENT) { |
103 | OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE); |
104 | } else { |
105 | OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
106 | } |
107 | return NULL; |
108 | } |
109 | |
110 | ret = BIO_new_fp(file, BIO_CLOSE); |
111 | if (ret == NULL) { |
112 | fclose(file); |
113 | return NULL; |
114 | } |
115 | |
116 | return ret; |
117 | } |
118 | |
119 | BIO *BIO_new_fp(FILE *stream, int close_flag) { |
120 | BIO *ret = BIO_new(BIO_s_file()); |
121 | |
122 | if (ret == NULL) { |
123 | return NULL; |
124 | } |
125 | |
126 | BIO_set_fp(ret, stream, close_flag); |
127 | return ret; |
128 | } |
129 | |
130 | static int file_new(BIO *bio) { return 1; } |
131 | |
132 | static int file_free(BIO *bio) { |
133 | if (bio == NULL) { |
134 | return 0; |
135 | } |
136 | |
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 | |
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 | |
166 | static int file_write(BIO *b, const char *in, int inl) { |
167 | int ret = 0; |
168 | |
169 | if (!b->init) { |
170 | return 0; |
171 | } |
172 | |
173 | ret = fwrite(in, inl, 1, (FILE *)b->ptr); |
174 | if (ret > 0) { |
175 | ret = inl; |
176 | } |
177 | return ret; |
178 | } |
179 | |
180 | static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { |
181 | long ret = 1; |
182 | FILE *fp = (FILE *)b->ptr; |
183 | FILE **fpp; |
184 | char p[4]; |
185 | |
186 | switch (cmd) { |
187 | case BIO_CTRL_RESET: |
188 | num = 0; |
189 | OPENSSL_FALLTHROUGH; |
190 | case BIO_C_FILE_SEEK: |
191 | ret = (long)fseek(fp, num, 0); |
192 | break; |
193 | case BIO_CTRL_EOF: |
194 | ret = (long)feof(fp); |
195 | break; |
196 | case BIO_C_FILE_TELL: |
197 | case BIO_CTRL_INFO: |
198 | ret = ftell(fp); |
199 | break; |
200 | case BIO_C_SET_FILE_PTR: |
201 | file_free(b); |
202 | b->shutdown = (int)num & BIO_CLOSE; |
203 | b->ptr = ptr; |
204 | b->init = 1; |
205 | break; |
206 | case BIO_C_SET_FILENAME: |
207 | file_free(b); |
208 | b->shutdown = (int)num & BIO_CLOSE; |
209 | if (num & BIO_FP_APPEND) { |
210 | if (num & BIO_FP_READ) { |
211 | BUF_strlcpy(p, "a+" , sizeof(p)); |
212 | } else { |
213 | BUF_strlcpy(p, "a" , sizeof(p)); |
214 | } |
215 | } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) { |
216 | BUF_strlcpy(p, "r+" , sizeof(p)); |
217 | } else if (num & BIO_FP_WRITE) { |
218 | BUF_strlcpy(p, "w" , sizeof(p)); |
219 | } else if (num & BIO_FP_READ) { |
220 | BUF_strlcpy(p, "r" , sizeof(p)); |
221 | } else { |
222 | OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE); |
223 | ret = 0; |
224 | break; |
225 | } |
226 | fp = fopen(ptr, p); |
227 | if (fp == NULL) { |
228 | OPENSSL_PUT_SYSTEM_ERROR(); |
229 | ERR_add_error_data(5, "fopen('" , ptr, "','" , p, "')" ); |
230 | OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); |
231 | ret = 0; |
232 | break; |
233 | } |
234 | b->ptr = fp; |
235 | b->init = 1; |
236 | break; |
237 | case BIO_C_GET_FILE_PTR: |
238 | // the ptr parameter is actually a FILE ** in this case. |
239 | if (ptr != NULL) { |
240 | fpp = (FILE **)ptr; |
241 | *fpp = (FILE *)b->ptr; |
242 | } |
243 | break; |
244 | case BIO_CTRL_GET_CLOSE: |
245 | ret = (long)b->shutdown; |
246 | break; |
247 | case BIO_CTRL_SET_CLOSE: |
248 | b->shutdown = (int)num; |
249 | break; |
250 | case BIO_CTRL_FLUSH: |
251 | ret = 0 == fflush((FILE *)b->ptr); |
252 | break; |
253 | case BIO_CTRL_WPENDING: |
254 | case BIO_CTRL_PENDING: |
255 | default: |
256 | ret = 0; |
257 | break; |
258 | } |
259 | return ret; |
260 | } |
261 | |
262 | static int file_gets(BIO *bp, char *buf, int size) { |
263 | int ret = 0; |
264 | |
265 | if (size == 0) { |
266 | return 0; |
267 | } |
268 | |
269 | if (!fgets(buf, size, (FILE *)bp->ptr)) { |
270 | buf[0] = 0; |
271 | goto err; |
272 | } |
273 | ret = strlen(buf); |
274 | |
275 | err: |
276 | return ret; |
277 | } |
278 | |
279 | static const BIO_METHOD methods_filep = { |
280 | BIO_TYPE_FILE, "FILE pointer" , |
281 | file_write, file_read, |
282 | NULL /* puts */, file_gets, |
283 | file_ctrl, file_new, |
284 | file_free, NULL /* callback_ctrl */, |
285 | }; |
286 | |
287 | const BIO_METHOD *BIO_s_file(void) { return &methods_filep; } |
288 | |
289 | |
290 | int BIO_get_fp(BIO *bio, FILE **out_file) { |
291 | return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file); |
292 | } |
293 | |
294 | int BIO_set_fp(BIO *bio, FILE *file, int close_flag) { |
295 | return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file); |
296 | } |
297 | |
298 | int BIO_read_filename(BIO *bio, const char *filename) { |
299 | return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ, |
300 | (char *)filename); |
301 | } |
302 | |
303 | int BIO_write_filename(BIO *bio, const char *filename) { |
304 | return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE, |
305 | (char *)filename); |
306 | } |
307 | |
308 | int BIO_append_filename(BIO *bio, const char *filename) { |
309 | return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND, |
310 | (char *)filename); |
311 | } |
312 | |
313 | int BIO_rw_filename(BIO *bio, const char *filename) { |
314 | return BIO_ctrl(bio, BIO_C_SET_FILENAME, |
315 | BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename); |
316 | } |
317 | |
318 | #endif // OPENSSL_TRUSTY |
319 | |