blob: 25c1dbdb0c07240baa3eec0ed3c82a2b9c1d3faf [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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)
David Benjamin808f8322017-08-18 14:06:02 -040058// 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...
Adam Langley95c29f32014-06-20 12:00:00 -070069#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>
Adam Langley2b2d66d2015-01-30 17:08:37 -080078#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070079
80#include <openssl/buf.h>
81#include <openssl/err.h>
82#include <openssl/mem.h>
83
84
85#define BIO_FP_READ 0x02
86#define BIO_FP_WRITE 0x04
87#define BIO_FP_APPEND 0x08
88
Adam Langley95c29f32014-06-20 12:00:00 -070089BIO *BIO_new_file(const char *filename, const char *mode) {
90 BIO *ret;
91 FILE *file;
92
Adam Langley65dcfc72016-02-22 09:16:57 -080093 file = fopen(filename, mode);
Adam Langley95c29f32014-06-20 12:00:00 -070094 if (file == NULL) {
David Benjamin3fc138e2015-10-28 18:03:21 -040095 OPENSSL_PUT_SYSTEM_ERROR();
Adam Langley95c29f32014-06-20 12:00:00 -070096
97 ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
98 if (errno == ENOENT) {
David Benjamin3570d732015-06-29 00:28:17 -040099 OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE);
Adam Langley95c29f32014-06-20 12:00:00 -0700100 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400101 OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700102 }
103 return NULL;
104 }
105
106 ret = BIO_new(BIO_s_file());
107 if (ret == NULL) {
108 fclose(file);
109 return NULL;
110 }
111
112 BIO_set_fp(ret, file, BIO_CLOSE);
113 return ret;
114}
115
116BIO *BIO_new_fp(FILE *stream, int close_flag) {
117 BIO *ret = BIO_new(BIO_s_file());
118
119 if (ret == NULL) {
120 return NULL;
121 }
122
123 BIO_set_fp(ret, stream, close_flag);
124 return ret;
125}
126
127static int file_new(BIO *bio) { return 1; }
128
129static int file_free(BIO *bio) {
130 if (bio == NULL) {
131 return 0;
132 }
133
134 if (!bio->shutdown) {
135 return 1;
136 }
137
138 if (bio->init && bio->ptr != NULL) {
139 fclose(bio->ptr);
140 bio->ptr = NULL;
141 }
142 bio->init = 0;
143
144 return 1;
145}
146
147static int file_read(BIO *b, char *out, int outl) {
Adam Langley95c29f32014-06-20 12:00:00 -0700148 if (!b->init) {
149 return 0;
150 }
151
David Benjaminffadb392015-08-28 15:39:49 -0400152 size_t ret = fread(out, 1, outl, (FILE *)b->ptr);
Adam Langley95c29f32014-06-20 12:00:00 -0700153 if (ret == 0 && ferror((FILE *)b->ptr)) {
David Benjamin3fc138e2015-10-28 18:03:21 -0400154 OPENSSL_PUT_SYSTEM_ERROR();
David Benjamin3570d732015-06-29 00:28:17 -0400155 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
David Benjaminffadb392015-08-28 15:39:49 -0400156 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700157 }
158
David Benjamin808f8322017-08-18 14:06:02 -0400159 // fread reads at most |outl| bytes, so |ret| fits in an int.
David Benjaminffadb392015-08-28 15:39:49 -0400160 return (int)ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700161}
162
163static int file_write(BIO *b, const char *in, int inl) {
164 int ret = 0;
165
166 if (!b->init) {
167 return 0;
168 }
169
170 ret = fwrite(in, inl, 1, (FILE *)b->ptr);
171 if (ret > 0) {
172 ret = inl;
173 }
174 return ret;
175}
176
177static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
178 long ret = 1;
179 FILE *fp = (FILE *)b->ptr;
180 FILE **fpp;
181 char p[4];
182
183 switch (cmd) {
184 case BIO_CTRL_RESET:
185 num = 0;
186 case BIO_C_FILE_SEEK:
187 ret = (long)fseek(fp, num, 0);
188 break;
189 case BIO_CTRL_EOF:
190 ret = (long)feof(fp);
191 break;
192 case BIO_C_FILE_TELL:
193 case BIO_CTRL_INFO:
194 ret = ftell(fp);
195 break;
196 case BIO_C_SET_FILE_PTR:
197 file_free(b);
198 b->shutdown = (int)num & BIO_CLOSE;
199 b->ptr = ptr;
200 b->init = 1;
201 break;
202 case BIO_C_SET_FILENAME:
203 file_free(b);
204 b->shutdown = (int)num & BIO_CLOSE;
205 if (num & BIO_FP_APPEND) {
206 if (num & BIO_FP_READ) {
207 BUF_strlcpy(p, "a+", sizeof(p));
208 } else {
209 BUF_strlcpy(p, "a", sizeof(p));
210 }
211 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
212 BUF_strlcpy(p, "r+", sizeof(p));
213 } else if (num & BIO_FP_WRITE) {
214 BUF_strlcpy(p, "w", sizeof(p));
215 } else if (num & BIO_FP_READ) {
216 BUF_strlcpy(p, "r", sizeof(p));
217 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400218 OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE);
Adam Langley95c29f32014-06-20 12:00:00 -0700219 ret = 0;
220 break;
221 }
Adam Langley65dcfc72016-02-22 09:16:57 -0800222 fp = fopen(ptr, p);
Adam Langley95c29f32014-06-20 12:00:00 -0700223 if (fp == NULL) {
David Benjamin3fc138e2015-10-28 18:03:21 -0400224 OPENSSL_PUT_SYSTEM_ERROR();
Adam Langley95c29f32014-06-20 12:00:00 -0700225 ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
David Benjamin3570d732015-06-29 00:28:17 -0400226 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700227 ret = 0;
228 break;
229 }
230 b->ptr = fp;
231 b->init = 1;
232 break;
233 case BIO_C_GET_FILE_PTR:
David Benjamin808f8322017-08-18 14:06:02 -0400234 // the ptr parameter is actually a FILE ** in this case.
Adam Langley95c29f32014-06-20 12:00:00 -0700235 if (ptr != NULL) {
236 fpp = (FILE **)ptr;
237 *fpp = (FILE *)b->ptr;
238 }
239 break;
240 case BIO_CTRL_GET_CLOSE:
241 ret = (long)b->shutdown;
242 break;
243 case BIO_CTRL_SET_CLOSE:
244 b->shutdown = (int)num;
245 break;
246 case BIO_CTRL_FLUSH:
247 ret = 0 == fflush((FILE *)b->ptr);
248 break;
249 case BIO_CTRL_WPENDING:
250 case BIO_CTRL_PENDING:
251 default:
252 ret = 0;
253 break;
254 }
255 return ret;
256}
257
258static int file_gets(BIO *bp, char *buf, int size) {
259 int ret = 0;
260
261 if (size == 0) {
262 return 0;
263 }
264
265 if (!fgets(buf, size, (FILE *)bp->ptr)) {
266 buf[0] = 0;
267 goto err;
268 }
269 ret = strlen(buf);
270
271err:
272 return ret;
273}
274
Adam Langley95c29f32014-06-20 12:00:00 -0700275static const BIO_METHOD methods_filep = {
David Benjamin63421112017-01-26 21:50:04 -0500276 BIO_TYPE_FILE, "FILE pointer",
277 file_write, file_read,
278 NULL /* puts */, file_gets,
279 file_ctrl, file_new,
280 file_free, NULL /* callback_ctrl */,
281};
Adam Langley95c29f32014-06-20 12:00:00 -0700282
283const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
284
285
286int BIO_get_fp(BIO *bio, FILE **out_file) {
287 return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file);
288}
289
290int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
291 return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file);
292}
293
294int BIO_read_filename(BIO *bio, const char *filename) {
295 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
296 (char *)filename);
297}
298
299int BIO_write_filename(BIO *bio, const char *filename) {
300 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
301 (char *)filename);
302}
303
304int BIO_append_filename(BIO *bio, const char *filename) {
305 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
306 (char *)filename);
307}
308
309int BIO_rw_filename(BIO *bio, const char *filename) {
310 return BIO_ctrl(bio, BIO_C_SET_FILENAME,
311 BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename);
312}