/src/boringssl/crypto/bio/file.c
Line | Count | Source (jump to first uncovered line) |
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 <assert.h> |
77 | | #include <errno.h> |
78 | | #include <stdio.h> |
79 | | #include <string.h> |
80 | | |
81 | | #include <openssl/err.h> |
82 | | #include <openssl/mem.h> |
83 | | |
84 | | #include "../internal.h" |
85 | | |
86 | | #if defined(OPENSSL_WINDOWS) |
87 | | #include <fcntl.h> |
88 | | #include <io.h> |
89 | | #endif |
90 | | |
91 | 0 | #define BIO_FP_READ 0x02 |
92 | 0 | #define BIO_FP_WRITE 0x04 |
93 | 0 | #define BIO_FP_APPEND 0x08 |
94 | | |
95 | | #if !defined(OPENSSL_NO_FILESYSTEM) |
96 | 0 | #define fopen_if_available fopen |
97 | | #else |
98 | | static FILE *fopen_if_available(const char *path, const char *mode) { |
99 | | errno = ENOENT; |
100 | | return NULL; |
101 | | } |
102 | | #endif |
103 | | |
104 | | BIO *BIO_new_file(const char *filename, const char *mode) { |
105 | | BIO *ret; |
106 | | FILE *file; |
107 | | |
108 | | file = fopen_if_available(filename, mode); |
109 | | if (file == NULL) { |
110 | | OPENSSL_PUT_SYSTEM_ERROR(); |
111 | | |
112 | | ERR_add_error_data(5, "fopen('", filename, "','", mode, "')"); |
113 | | if (errno == ENOENT) { |
114 | | OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE); |
115 | | } else { |
116 | | OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
117 | | } |
118 | | return NULL; |
119 | | } |
120 | | |
121 | | ret = BIO_new_fp(file, BIO_CLOSE); |
122 | | if (ret == NULL) { |
123 | | fclose(file); |
124 | | return NULL; |
125 | | } |
126 | | |
127 | | return ret; |
128 | | } |
129 | | |
130 | 0 | BIO *BIO_new_fp(FILE *stream, int flags) { |
131 | 0 | BIO *ret = BIO_new(BIO_s_file()); |
132 | 0 | if (ret == NULL) { |
133 | 0 | return NULL; |
134 | 0 | } |
135 | | |
136 | 0 | BIO_set_fp(ret, stream, flags); |
137 | 0 | return ret; |
138 | 0 | } |
139 | | |
140 | 0 | static int file_free(BIO *bio) { |
141 | 0 | if (!bio->shutdown) { |
142 | 0 | return 1; |
143 | 0 | } |
144 | | |
145 | 0 | if (bio->init && bio->ptr != NULL) { |
146 | 0 | fclose(bio->ptr); |
147 | 0 | bio->ptr = NULL; |
148 | 0 | } |
149 | 0 | bio->init = 0; |
150 | |
|
151 | 0 | return 1; |
152 | 0 | } |
153 | | |
154 | 0 | static int file_read(BIO *b, char *out, int outl) { |
155 | 0 | if (!b->init) { |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | 0 | size_t ret = fread(out, 1, outl, (FILE *)b->ptr); |
160 | 0 | if (ret == 0 && ferror((FILE *)b->ptr)) { |
161 | 0 | OPENSSL_PUT_SYSTEM_ERROR(); |
162 | 0 | OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); |
163 | 0 | return -1; |
164 | 0 | } |
165 | | |
166 | | // fread reads at most |outl| bytes, so |ret| fits in an int. |
167 | 0 | return (int)ret; |
168 | 0 | } |
169 | | |
170 | 0 | static int file_write(BIO *b, const char *in, int inl) { |
171 | 0 | if (!b->init) { |
172 | 0 | return 0; |
173 | 0 | } |
174 | | |
175 | 0 | int ret = (int)fwrite(in, inl, 1, (FILE *)b->ptr); |
176 | 0 | if (ret > 0) { |
177 | 0 | ret = inl; |
178 | 0 | } |
179 | 0 | return ret; |
180 | 0 | } |
181 | | |
182 | 0 | static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { |
183 | 0 | long ret = 1; |
184 | 0 | FILE *fp = (FILE *)b->ptr; |
185 | 0 | FILE **fpp; |
186 | |
|
187 | 0 | switch (cmd) { |
188 | 0 | case BIO_CTRL_RESET: |
189 | 0 | num = 0; |
190 | 0 | OPENSSL_FALLTHROUGH; |
191 | 0 | case BIO_C_FILE_SEEK: |
192 | 0 | ret = (long)fseek(fp, num, 0); |
193 | 0 | break; |
194 | 0 | case BIO_CTRL_EOF: |
195 | 0 | ret = (long)feof(fp); |
196 | 0 | break; |
197 | 0 | case BIO_C_FILE_TELL: |
198 | 0 | case BIO_CTRL_INFO: |
199 | 0 | ret = ftell(fp); |
200 | 0 | break; |
201 | 0 | case BIO_C_SET_FILE_PTR: |
202 | 0 | file_free(b); |
203 | 0 | static_assert((BIO_CLOSE & BIO_FP_TEXT) == 0, |
204 | 0 | "BIO_CLOSE and BIO_FP_TEXT must not collide"); |
205 | | #if defined(OPENSSL_WINDOWS) |
206 | | // If |BIO_FP_TEXT| is not set, OpenSSL will switch the file to binary |
207 | | // mode. BoringSSL intentionally diverges here because it means code |
208 | | // tested under POSIX will inadvertently change the state of |FILE| |
209 | | // objects when wrapping them in a |BIO|. |
210 | | if (num & BIO_FP_TEXT) { |
211 | | _setmode(_fileno(ptr), _O_TEXT); |
212 | | } |
213 | | #endif |
214 | 0 | b->shutdown = (int)num & BIO_CLOSE; |
215 | 0 | b->ptr = ptr; |
216 | 0 | b->init = 1; |
217 | 0 | break; |
218 | 0 | case BIO_C_SET_FILENAME: |
219 | 0 | file_free(b); |
220 | 0 | b->shutdown = (int)num & BIO_CLOSE; |
221 | 0 | const char *mode; |
222 | 0 | if (num & BIO_FP_APPEND) { |
223 | 0 | if (num & BIO_FP_READ) { |
224 | 0 | mode = "ab+"; |
225 | 0 | } else { |
226 | 0 | mode = "ab"; |
227 | 0 | } |
228 | 0 | } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) { |
229 | 0 | mode = "rb+"; |
230 | 0 | } else if (num & BIO_FP_WRITE) { |
231 | 0 | mode = "wb"; |
232 | 0 | } else if (num & BIO_FP_READ) { |
233 | 0 | mode = "rb"; |
234 | 0 | } else { |
235 | 0 | OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE); |
236 | 0 | ret = 0; |
237 | 0 | break; |
238 | 0 | } |
239 | 0 | fp = fopen_if_available(ptr, mode); |
240 | 0 | if (fp == NULL) { |
241 | 0 | OPENSSL_PUT_SYSTEM_ERROR(); |
242 | 0 | ERR_add_error_data(5, "fopen('", ptr, "','", mode, "')"); |
243 | 0 | OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); |
244 | 0 | ret = 0; |
245 | 0 | break; |
246 | 0 | } |
247 | 0 | b->ptr = fp; |
248 | 0 | b->init = 1; |
249 | 0 | break; |
250 | 0 | case BIO_C_GET_FILE_PTR: |
251 | | // the ptr parameter is actually a FILE ** in this case. |
252 | 0 | if (ptr != NULL) { |
253 | 0 | fpp = (FILE **)ptr; |
254 | 0 | *fpp = (FILE *)b->ptr; |
255 | 0 | } |
256 | 0 | break; |
257 | 0 | case BIO_CTRL_GET_CLOSE: |
258 | 0 | ret = (long)b->shutdown; |
259 | 0 | break; |
260 | 0 | case BIO_CTRL_SET_CLOSE: |
261 | 0 | b->shutdown = (int)num; |
262 | 0 | break; |
263 | 0 | case BIO_CTRL_FLUSH: |
264 | 0 | ret = 0 == fflush((FILE *)b->ptr); |
265 | 0 | break; |
266 | 0 | case BIO_CTRL_WPENDING: |
267 | 0 | case BIO_CTRL_PENDING: |
268 | 0 | default: |
269 | 0 | ret = 0; |
270 | 0 | break; |
271 | 0 | } |
272 | 0 | return ret; |
273 | 0 | } |
274 | | |
275 | 0 | static int file_gets(BIO *bp, char *buf, int size) { |
276 | 0 | if (size == 0) { |
277 | 0 | return 0; |
278 | 0 | } |
279 | | |
280 | 0 | if (!fgets(buf, size, (FILE *)bp->ptr)) { |
281 | 0 | buf[0] = 0; |
282 | | // TODO(davidben): This doesn't distinguish error and EOF. This should check |
283 | | // |ferror| as in |file_read|. |
284 | 0 | return 0; |
285 | 0 | } |
286 | | |
287 | 0 | return (int)strlen(buf); |
288 | 0 | } |
289 | | |
290 | | static const BIO_METHOD methods_filep = { |
291 | | BIO_TYPE_FILE, "FILE pointer", |
292 | | file_write, file_read, |
293 | | NULL /* puts */, file_gets, |
294 | | file_ctrl, NULL /* create */, |
295 | | file_free, NULL /* callback_ctrl */, |
296 | | }; |
297 | | |
298 | 0 | const BIO_METHOD *BIO_s_file(void) { return &methods_filep; } |
299 | | |
300 | | |
301 | 0 | int BIO_get_fp(BIO *bio, FILE **out_file) { |
302 | 0 | return (int)BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char *)out_file); |
303 | 0 | } |
304 | | |
305 | 0 | int BIO_set_fp(BIO *bio, FILE *file, int flags) { |
306 | 0 | return (int)BIO_ctrl(bio, BIO_C_SET_FILE_PTR, flags, (char *)file); |
307 | 0 | } |
308 | | |
309 | 0 | int BIO_read_filename(BIO *bio, const char *filename) { |
310 | 0 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ, |
311 | 0 | (char *)filename); |
312 | 0 | } |
313 | | |
314 | 0 | int BIO_write_filename(BIO *bio, const char *filename) { |
315 | 0 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE, |
316 | 0 | (char *)filename); |
317 | 0 | } |
318 | | |
319 | 0 | int BIO_append_filename(BIO *bio, const char *filename) { |
320 | 0 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND, |
321 | 0 | (char *)filename); |
322 | 0 | } |
323 | | |
324 | 0 | int BIO_rw_filename(BIO *bio, const char *filename) { |
325 | 0 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, |
326 | 0 | BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, |
327 | 0 | (char *)filename); |
328 | 0 | } |
329 | | |
330 | 0 | long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); } |
331 | | |
332 | 0 | long BIO_seek(BIO *bio, long offset) { |
333 | 0 | return BIO_ctrl(bio, BIO_C_FILE_SEEK, offset, NULL); |
334 | 0 | } |