1 | /*
|
---|
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | /*-
|
---|
11 | * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout
|
---|
12 | * with binary data (e.g. asn1parse -inform DER < xxx) under
|
---|
13 | * Windows
|
---|
14 | */
|
---|
15 |
|
---|
16 | #ifndef HEADER_BSS_FILE_C
|
---|
17 | # define HEADER_BSS_FILE_C
|
---|
18 |
|
---|
19 | # if defined(__linux) || defined(__sun) || defined(__hpux)
|
---|
20 | /*
|
---|
21 | * Following definition aliases fopen to fopen64 on above mentioned
|
---|
22 | * platforms. This makes it possible to open and sequentially access files
|
---|
23 | * larger than 2GB from 32-bit application. It does not allow to traverse
|
---|
24 | * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
|
---|
25 | * platform permits that, not with fseek/ftell. Not to mention that breaking
|
---|
26 | * 2GB limit for seeking would require surgery to *our* API. But sequential
|
---|
27 | * access suffices for practical cases when you can run into large files,
|
---|
28 | * such as fingerprinting, so we can let API alone. For reference, the list
|
---|
29 | * of 32-bit platforms which allow for sequential access of large files
|
---|
30 | * without extra "magic" comprise *BSD, Darwin, IRIX...
|
---|
31 | */
|
---|
32 | # ifndef _FILE_OFFSET_BITS
|
---|
33 | # define _FILE_OFFSET_BITS 64
|
---|
34 | # endif
|
---|
35 | # endif
|
---|
36 |
|
---|
37 | # include <stdio.h>
|
---|
38 | # include <errno.h>
|
---|
39 | # include "bio_lcl.h"
|
---|
40 | # include <openssl/err.h>
|
---|
41 |
|
---|
42 | # if !defined(OPENSSL_NO_STDIO)
|
---|
43 |
|
---|
44 | static int file_write(BIO *h, const char *buf, int num);
|
---|
45 | static int file_read(BIO *h, char *buf, int size);
|
---|
46 | static int file_puts(BIO *h, const char *str);
|
---|
47 | static int file_gets(BIO *h, char *str, int size);
|
---|
48 | static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
---|
49 | static int file_new(BIO *h);
|
---|
50 | static int file_free(BIO *data);
|
---|
51 | static const BIO_METHOD methods_filep = {
|
---|
52 | BIO_TYPE_FILE,
|
---|
53 | "FILE pointer",
|
---|
54 | file_write,
|
---|
55 | file_read,
|
---|
56 | file_puts,
|
---|
57 | file_gets,
|
---|
58 | file_ctrl,
|
---|
59 | file_new,
|
---|
60 | file_free,
|
---|
61 | NULL,
|
---|
62 | };
|
---|
63 |
|
---|
64 | BIO *BIO_new_file(const char *filename, const char *mode)
|
---|
65 | {
|
---|
66 | BIO *ret;
|
---|
67 | FILE *file = openssl_fopen(filename, mode);
|
---|
68 | int fp_flags = BIO_CLOSE;
|
---|
69 |
|
---|
70 | if (strchr(mode, 'b') == NULL)
|
---|
71 | fp_flags |= BIO_FP_TEXT;
|
---|
72 |
|
---|
73 | if (file == NULL) {
|
---|
74 | SYSerr(SYS_F_FOPEN, get_last_sys_error());
|
---|
75 | ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
|
---|
76 | if (errno == ENOENT
|
---|
77 | # ifdef ENXIO
|
---|
78 | || errno == ENXIO
|
---|
79 | # endif
|
---|
80 | )
|
---|
81 | BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
|
---|
82 | else
|
---|
83 | BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
|
---|
84 | return (NULL);
|
---|
85 | }
|
---|
86 | if ((ret = BIO_new(BIO_s_file())) == NULL) {
|
---|
87 | fclose(file);
|
---|
88 | return (NULL);
|
---|
89 | }
|
---|
90 |
|
---|
91 | BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
|
---|
92 | * UPLINK */
|
---|
93 | BIO_set_fp(ret, file, fp_flags);
|
---|
94 | return (ret);
|
---|
95 | }
|
---|
96 |
|
---|
97 | BIO *BIO_new_fp(FILE *stream, int close_flag)
|
---|
98 | {
|
---|
99 | BIO *ret;
|
---|
100 |
|
---|
101 | if ((ret = BIO_new(BIO_s_file())) == NULL)
|
---|
102 | return (NULL);
|
---|
103 |
|
---|
104 | /* redundant flag, left for documentation purposes */
|
---|
105 | BIO_set_flags(ret, BIO_FLAGS_UPLINK);
|
---|
106 | BIO_set_fp(ret, stream, close_flag);
|
---|
107 | return (ret);
|
---|
108 | }
|
---|
109 |
|
---|
110 | const BIO_METHOD *BIO_s_file(void)
|
---|
111 | {
|
---|
112 | return (&methods_filep);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static int file_new(BIO *bi)
|
---|
116 | {
|
---|
117 | bi->init = 0;
|
---|
118 | bi->num = 0;
|
---|
119 | bi->ptr = NULL;
|
---|
120 | bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
|
---|
121 | return (1);
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int file_free(BIO *a)
|
---|
125 | {
|
---|
126 | if (a == NULL)
|
---|
127 | return (0);
|
---|
128 | if (a->shutdown) {
|
---|
129 | if ((a->init) && (a->ptr != NULL)) {
|
---|
130 | if (a->flags & BIO_FLAGS_UPLINK)
|
---|
131 | UP_fclose(a->ptr);
|
---|
132 | else
|
---|
133 | fclose(a->ptr);
|
---|
134 | a->ptr = NULL;
|
---|
135 | a->flags = BIO_FLAGS_UPLINK;
|
---|
136 | }
|
---|
137 | a->init = 0;
|
---|
138 | }
|
---|
139 | return (1);
|
---|
140 | }
|
---|
141 |
|
---|
142 | static int file_read(BIO *b, char *out, int outl)
|
---|
143 | {
|
---|
144 | int ret = 0;
|
---|
145 |
|
---|
146 | if (b->init && (out != NULL)) {
|
---|
147 | if (b->flags & BIO_FLAGS_UPLINK)
|
---|
148 | ret = UP_fread(out, 1, (int)outl, b->ptr);
|
---|
149 | else
|
---|
150 | ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
|
---|
151 | if (ret == 0
|
---|
152 | && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
|
---|
153 | ferror((FILE *)b->ptr)) {
|
---|
154 | SYSerr(SYS_F_FREAD, get_last_sys_error());
|
---|
155 | BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
|
---|
156 | ret = -1;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | return (ret);
|
---|
160 | }
|
---|
161 |
|
---|
162 | static int file_write(BIO *b, const char *in, int inl)
|
---|
163 | {
|
---|
164 | int ret = 0;
|
---|
165 |
|
---|
166 | if (b->init && (in != NULL)) {
|
---|
167 | if (b->flags & BIO_FLAGS_UPLINK)
|
---|
168 | ret = UP_fwrite(in, (int)inl, 1, b->ptr);
|
---|
169 | else
|
---|
170 | ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
|
---|
171 | if (ret)
|
---|
172 | ret = inl;
|
---|
173 | /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
|
---|
174 | /*
|
---|
175 | * according to Tim Hudson <tjh@cryptsoft.com>, the commented out
|
---|
176 | * version above can cause 'inl' write calls under some stupid stdio
|
---|
177 | * implementations (VMS)
|
---|
178 | */
|
---|
179 | }
|
---|
180 | return (ret);
|
---|
181 | }
|
---|
182 |
|
---|
183 | static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
|
---|
184 | {
|
---|
185 | long ret = 1;
|
---|
186 | FILE *fp = (FILE *)b->ptr;
|
---|
187 | FILE **fpp;
|
---|
188 | char p[4];
|
---|
189 |
|
---|
190 | switch (cmd) {
|
---|
191 | case BIO_C_FILE_SEEK:
|
---|
192 | case BIO_CTRL_RESET:
|
---|
193 | if (b->flags & BIO_FLAGS_UPLINK)
|
---|
194 | ret = (long)UP_fseek(b->ptr, num, 0);
|
---|
195 | else
|
---|
196 | ret = (long)fseek(fp, num, 0);
|
---|
197 | break;
|
---|
198 | case BIO_CTRL_EOF:
|
---|
199 | if (b->flags & BIO_FLAGS_UPLINK)
|
---|
200 | ret = (long)UP_feof(fp);
|
---|
201 | else
|
---|
202 | ret = (long)feof(fp);
|
---|
203 | break;
|
---|
204 | case BIO_C_FILE_TELL:
|
---|
205 | case BIO_CTRL_INFO:
|
---|
206 | if (b->flags & BIO_FLAGS_UPLINK)
|
---|
207 | ret = UP_ftell(b->ptr);
|
---|
208 | else
|
---|
209 | ret = ftell(fp);
|
---|
210 | break;
|
---|
211 | case BIO_C_SET_FILE_PTR:
|
---|
212 | file_free(b);
|
---|
213 | b->shutdown = (int)num & BIO_CLOSE;
|
---|
214 | b->ptr = ptr;
|
---|
215 | b->init = 1;
|
---|
216 | # if BIO_FLAGS_UPLINK!=0
|
---|
217 | # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
|
---|
218 | # define _IOB_ENTRIES 20
|
---|
219 | # endif
|
---|
220 | /* Safety net to catch purely internal BIO_set_fp calls */
|
---|
221 | # if defined(_MSC_VER) && _MSC_VER>=1900
|
---|
222 | if (ptr == stdin || ptr == stdout || ptr == stderr)
|
---|
223 | BIO_clear_flags(b, BIO_FLAGS_UPLINK);
|
---|
224 | # elif defined(_IOB_ENTRIES)
|
---|
225 | if ((size_t)ptr >= (size_t)stdin &&
|
---|
226 | (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
|
---|
227 | BIO_clear_flags(b, BIO_FLAGS_UPLINK);
|
---|
228 | # endif
|
---|
229 | # endif
|
---|
230 | # ifdef UP_fsetmod
|
---|
231 | if (b->flags & BIO_FLAGS_UPLINK)
|
---|
232 | UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
|
---|
233 | else
|
---|
234 | # endif
|
---|
235 | {
|
---|
236 | # if defined(OPENSSL_SYS_WINDOWS)
|
---|
237 | int fd = _fileno((FILE *)ptr);
|
---|
238 | if (num & BIO_FP_TEXT)
|
---|
239 | _setmode(fd, _O_TEXT);
|
---|
240 | else
|
---|
241 | _setmode(fd, _O_BINARY);
|
---|
242 | # elif defined(OPENSSL_SYS_MSDOS)
|
---|
243 | int fd = fileno((FILE *)ptr);
|
---|
244 | /* Set correct text/binary mode */
|
---|
245 | if (num & BIO_FP_TEXT)
|
---|
246 | _setmode(fd, _O_TEXT);
|
---|
247 | /* Dangerous to set stdin/stdout to raw (unless redirected) */
|
---|
248 | else {
|
---|
249 | if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
|
---|
250 | if (isatty(fd) <= 0)
|
---|
251 | _setmode(fd, _O_BINARY);
|
---|
252 | } else
|
---|
253 | _setmode(fd, _O_BINARY);
|
---|
254 | }
|
---|
255 | # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
|
---|
256 | int fd = fileno((FILE *)ptr);
|
---|
257 | if (num & BIO_FP_TEXT)
|
---|
258 | setmode(fd, O_TEXT);
|
---|
259 | else
|
---|
260 | setmode(fd, O_BINARY);
|
---|
261 | # endif
|
---|
262 | }
|
---|
263 | break;
|
---|
264 | case BIO_C_SET_FILENAME:
|
---|
265 | file_free(b);
|
---|
266 | b->shutdown = (int)num & BIO_CLOSE;
|
---|
267 | if (num & BIO_FP_APPEND) {
|
---|
268 | if (num & BIO_FP_READ)
|
---|
269 | OPENSSL_strlcpy(p, "a+", sizeof p);
|
---|
270 | else
|
---|
271 | OPENSSL_strlcpy(p, "a", sizeof p);
|
---|
272 | } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
|
---|
273 | OPENSSL_strlcpy(p, "r+", sizeof p);
|
---|
274 | else if (num & BIO_FP_WRITE)
|
---|
275 | OPENSSL_strlcpy(p, "w", sizeof p);
|
---|
276 | else if (num & BIO_FP_READ)
|
---|
277 | OPENSSL_strlcpy(p, "r", sizeof p);
|
---|
278 | else {
|
---|
279 | BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
|
---|
280 | ret = 0;
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32_CYGWIN)
|
---|
284 | if (!(num & BIO_FP_TEXT))
|
---|
285 | strcat(p, "b");
|
---|
286 | else
|
---|
287 | strcat(p, "t");
|
---|
288 | # endif
|
---|
289 | fp = openssl_fopen(ptr, p);
|
---|
290 | if (fp == NULL) {
|
---|
291 | SYSerr(SYS_F_FOPEN, get_last_sys_error());
|
---|
292 | ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
|
---|
293 | BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
|
---|
294 | ret = 0;
|
---|
295 | break;
|
---|
296 | }
|
---|
297 | b->ptr = fp;
|
---|
298 | b->init = 1;
|
---|
299 | BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
|
---|
300 | * UPLINK */
|
---|
301 | break;
|
---|
302 | case BIO_C_GET_FILE_PTR:
|
---|
303 | /* the ptr parameter is actually a FILE ** in this case. */
|
---|
304 | if (ptr != NULL) {
|
---|
305 | fpp = (FILE **)ptr;
|
---|
306 | *fpp = (FILE *)b->ptr;
|
---|
307 | }
|
---|
308 | break;
|
---|
309 | case BIO_CTRL_GET_CLOSE:
|
---|
310 | ret = (long)b->shutdown;
|
---|
311 | break;
|
---|
312 | case BIO_CTRL_SET_CLOSE:
|
---|
313 | b->shutdown = (int)num;
|
---|
314 | break;
|
---|
315 | case BIO_CTRL_FLUSH:
|
---|
316 | if (b->flags & BIO_FLAGS_UPLINK)
|
---|
317 | UP_fflush(b->ptr);
|
---|
318 | else
|
---|
319 | fflush((FILE *)b->ptr);
|
---|
320 | break;
|
---|
321 | case BIO_CTRL_DUP:
|
---|
322 | ret = 1;
|
---|
323 | break;
|
---|
324 |
|
---|
325 | case BIO_CTRL_WPENDING:
|
---|
326 | case BIO_CTRL_PENDING:
|
---|
327 | case BIO_CTRL_PUSH:
|
---|
328 | case BIO_CTRL_POP:
|
---|
329 | default:
|
---|
330 | ret = 0;
|
---|
331 | break;
|
---|
332 | }
|
---|
333 | return (ret);
|
---|
334 | }
|
---|
335 |
|
---|
336 | static int file_gets(BIO *bp, char *buf, int size)
|
---|
337 | {
|
---|
338 | int ret = 0;
|
---|
339 |
|
---|
340 | buf[0] = '\0';
|
---|
341 | if (bp->flags & BIO_FLAGS_UPLINK) {
|
---|
342 | if (!UP_fgets(buf, size, bp->ptr))
|
---|
343 | goto err;
|
---|
344 | } else {
|
---|
345 | if (!fgets(buf, size, (FILE *)bp->ptr))
|
---|
346 | goto err;
|
---|
347 | }
|
---|
348 | if (buf[0] != '\0')
|
---|
349 | ret = strlen(buf);
|
---|
350 | err:
|
---|
351 | return (ret);
|
---|
352 | }
|
---|
353 |
|
---|
354 | static int file_puts(BIO *bp, const char *str)
|
---|
355 | {
|
---|
356 | int n, ret;
|
---|
357 |
|
---|
358 | n = strlen(str);
|
---|
359 | ret = file_write(bp, str, n);
|
---|
360 | return (ret);
|
---|
361 | }
|
---|
362 |
|
---|
363 | #else
|
---|
364 |
|
---|
365 | static int file_write(BIO *b, const char *in, int inl)
|
---|
366 | {
|
---|
367 | return -1;
|
---|
368 | }
|
---|
369 | static int file_read(BIO *b, char *out, int outl)
|
---|
370 | {
|
---|
371 | return -1;
|
---|
372 | }
|
---|
373 | static int file_puts(BIO *bp, const char *str)
|
---|
374 | {
|
---|
375 | return -1;
|
---|
376 | }
|
---|
377 | static int file_gets(BIO *bp, char *buf, int size)
|
---|
378 | {
|
---|
379 | return 0;
|
---|
380 | }
|
---|
381 | static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
|
---|
382 | {
|
---|
383 | return 0;
|
---|
384 | }
|
---|
385 | static int file_new(BIO *bi)
|
---|
386 | {
|
---|
387 | return 0;
|
---|
388 | }
|
---|
389 | static int file_free(BIO *a)
|
---|
390 | {
|
---|
391 | return 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | static const BIO_METHOD methods_filep = {
|
---|
395 | BIO_TYPE_FILE,
|
---|
396 | "FILE pointer",
|
---|
397 | file_write,
|
---|
398 | file_read,
|
---|
399 | file_puts,
|
---|
400 | file_gets,
|
---|
401 | file_ctrl,
|
---|
402 | file_new,
|
---|
403 | file_free,
|
---|
404 | NULL,
|
---|
405 | };
|
---|
406 |
|
---|
407 | const BIO_METHOD *BIO_s_file(void)
|
---|
408 | {
|
---|
409 | return (&methods_filep);
|
---|
410 | }
|
---|
411 |
|
---|
412 | BIO *BIO_new_file(const char *filename, const char *mode)
|
---|
413 | {
|
---|
414 | return NULL;
|
---|
415 | }
|
---|
416 |
|
---|
417 | # endif /* OPENSSL_NO_STDIO */
|
---|
418 |
|
---|
419 | #endif /* HEADER_BSS_FILE_C */
|
---|