4f79c32bbbcbcdbb8592f5efde978b5e66831754
[openssl.git] / crypto / bio / bss_file.c
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             BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
78         else
79             BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
80         return (NULL);
81     }
82     if ((ret = BIO_new(BIO_s_file())) == NULL) {
83         fclose(file);
84         return (NULL);
85     }
86
87     BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
88                                              * UPLINK */
89     BIO_set_fp(ret, file, fp_flags);
90     return (ret);
91 }
92
93 BIO *BIO_new_fp(FILE *stream, int close_flag)
94 {
95     BIO *ret;
96
97     if ((ret = BIO_new(BIO_s_file())) == NULL)
98         return (NULL);
99
100     /* redundant flag, left for documentation purposes */
101     BIO_set_flags(ret, BIO_FLAGS_UPLINK);
102     BIO_set_fp(ret, stream, close_flag);
103     return (ret);
104 }
105
106 const BIO_METHOD *BIO_s_file(void)
107 {
108     return (&methods_filep);
109 }
110
111 static int file_new(BIO *bi)
112 {
113     bi->init = 0;
114     bi->num = 0;
115     bi->ptr = NULL;
116     bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
117     return (1);
118 }
119
120 static int file_free(BIO *a)
121 {
122     if (a == NULL)
123         return (0);
124     if (a->shutdown) {
125         if ((a->init) && (a->ptr != NULL)) {
126             if (a->flags & BIO_FLAGS_UPLINK)
127                 UP_fclose(a->ptr);
128             else
129                 fclose(a->ptr);
130             a->ptr = NULL;
131             a->flags = BIO_FLAGS_UPLINK;
132         }
133         a->init = 0;
134     }
135     return (1);
136 }
137
138 static int file_read(BIO *b, char *out, int outl)
139 {
140     int ret = 0;
141
142     if (b->init && (out != NULL)) {
143         if (b->flags & BIO_FLAGS_UPLINK)
144             ret = UP_fread(out, 1, (int)outl, b->ptr);
145         else
146             ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
147         if (ret == 0
148             && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
149                                                ferror((FILE *)b->ptr)) {
150             SYSerr(SYS_F_FREAD, get_last_sys_error());
151             BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
152             ret = -1;
153         }
154     }
155     return (ret);
156 }
157
158 static int file_write(BIO *b, const char *in, int inl)
159 {
160     int ret = 0;
161
162     if (b->init && (in != NULL)) {
163         if (b->flags & BIO_FLAGS_UPLINK)
164             ret = UP_fwrite(in, (int)inl, 1, b->ptr);
165         else
166             ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
167         if (ret)
168             ret = inl;
169         /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
170         /*
171          * according to Tim Hudson <tjh@cryptsoft.com>, the commented out
172          * version above can cause 'inl' write calls under some stupid stdio
173          * implementations (VMS)
174          */
175     }
176     return (ret);
177 }
178
179 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
180 {
181     long ret = 1;
182     FILE *fp = (FILE *)b->ptr;
183     FILE **fpp;
184     char p[4];
185
186     switch (cmd) {
187     case BIO_C_FILE_SEEK:
188     case BIO_CTRL_RESET:
189         if (b->flags & BIO_FLAGS_UPLINK)
190             ret = (long)UP_fseek(b->ptr, num, 0);
191         else
192             ret = (long)fseek(fp, num, 0);
193         break;
194     case BIO_CTRL_EOF:
195         if (b->flags & BIO_FLAGS_UPLINK)
196             ret = (long)UP_feof(fp);
197         else
198             ret = (long)feof(fp);
199         break;
200     case BIO_C_FILE_TELL:
201     case BIO_CTRL_INFO:
202         if (b->flags & BIO_FLAGS_UPLINK)
203             ret = UP_ftell(b->ptr);
204         else
205             ret = ftell(fp);
206         break;
207     case BIO_C_SET_FILE_PTR:
208         file_free(b);
209         b->shutdown = (int)num & BIO_CLOSE;
210         b->ptr = ptr;
211         b->init = 1;
212 #  if BIO_FLAGS_UPLINK!=0
213 #   if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
214 #    define _IOB_ENTRIES 20
215 #   endif
216         /* Safety net to catch purely internal BIO_set_fp calls */
217 #   if defined(_MSC_VER) && _MSC_VER>=1900
218         if (ptr == stdin || ptr == stdout || ptr == stderr)
219             BIO_clear_flags(b, BIO_FLAGS_UPLINK);
220 #   elif defined(_IOB_ENTRIES)
221         if ((size_t)ptr >= (size_t)stdin &&
222             (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
223             BIO_clear_flags(b, BIO_FLAGS_UPLINK);
224 #   endif
225 #  endif
226 #  ifdef UP_fsetmod
227         if (b->flags & BIO_FLAGS_UPLINK)
228             UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
229         else
230 #  endif
231         {
232 #  if defined(OPENSSL_SYS_WINDOWS)
233             int fd = _fileno((FILE *)ptr);
234             if (num & BIO_FP_TEXT)
235                 _setmode(fd, _O_TEXT);
236             else
237                 _setmode(fd, _O_BINARY);
238 #  elif defined(OPENSSL_SYS_MSDOS)
239             int fd = fileno((FILE *)ptr);
240             /* Set correct text/binary mode */
241             if (num & BIO_FP_TEXT)
242                 _setmode(fd, _O_TEXT);
243             /* Dangerous to set stdin/stdout to raw (unless redirected) */
244             else {
245                 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
246                     if (isatty(fd) <= 0)
247                         _setmode(fd, _O_BINARY);
248                 } else
249                     _setmode(fd, _O_BINARY);
250             }
251 #  elif defined(OPENSSL_SYS_WIN32_CYGWIN)
252             int fd = fileno((FILE *)ptr);
253             if (num & BIO_FP_TEXT)
254                 setmode(fd, O_TEXT);
255             else
256                 setmode(fd, O_BINARY);
257 #  endif
258         }
259         break;
260     case BIO_C_SET_FILENAME:
261         file_free(b);
262         b->shutdown = (int)num & BIO_CLOSE;
263         if (num & BIO_FP_APPEND) {
264             if (num & BIO_FP_READ)
265                 OPENSSL_strlcpy(p, "a+", sizeof p);
266             else
267                 OPENSSL_strlcpy(p, "a", sizeof p);
268         } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
269             OPENSSL_strlcpy(p, "r+", sizeof p);
270         else if (num & BIO_FP_WRITE)
271             OPENSSL_strlcpy(p, "w", sizeof p);
272         else if (num & BIO_FP_READ)
273             OPENSSL_strlcpy(p, "r", sizeof p);
274         else {
275             BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
276             ret = 0;
277             break;
278         }
279 #  if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32_CYGWIN)
280         if (!(num & BIO_FP_TEXT))
281             strcat(p, "b");
282         else
283             strcat(p, "t");
284 #  endif
285         fp = openssl_fopen(ptr, p);
286         if (fp == NULL) {
287             SYSerr(SYS_F_FOPEN, get_last_sys_error());
288             ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
289             BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
290             ret = 0;
291             break;
292         }
293         b->ptr = fp;
294         b->init = 1;
295         BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
296                                                * UPLINK */
297         break;
298     case BIO_C_GET_FILE_PTR:
299         /* the ptr parameter is actually a FILE ** in this case. */
300         if (ptr != NULL) {
301             fpp = (FILE **)ptr;
302             *fpp = (FILE *)b->ptr;
303         }
304         break;
305     case BIO_CTRL_GET_CLOSE:
306         ret = (long)b->shutdown;
307         break;
308     case BIO_CTRL_SET_CLOSE:
309         b->shutdown = (int)num;
310         break;
311     case BIO_CTRL_FLUSH:
312         if (b->flags & BIO_FLAGS_UPLINK)
313             UP_fflush(b->ptr);
314         else
315             fflush((FILE *)b->ptr);
316         break;
317     case BIO_CTRL_DUP:
318         ret = 1;
319         break;
320
321     case BIO_CTRL_WPENDING:
322     case BIO_CTRL_PENDING:
323     case BIO_CTRL_PUSH:
324     case BIO_CTRL_POP:
325     default:
326         ret = 0;
327         break;
328     }
329     return (ret);
330 }
331
332 static int file_gets(BIO *bp, char *buf, int size)
333 {
334     int ret = 0;
335
336     buf[0] = '\0';
337     if (bp->flags & BIO_FLAGS_UPLINK) {
338         if (!UP_fgets(buf, size, bp->ptr))
339             goto err;
340     } else {
341         if (!fgets(buf, size, (FILE *)bp->ptr))
342             goto err;
343     }
344     if (buf[0] != '\0')
345         ret = strlen(buf);
346  err:
347     return (ret);
348 }
349
350 static int file_puts(BIO *bp, const char *str)
351 {
352     int n, ret;
353
354     n = strlen(str);
355     ret = file_write(bp, str, n);
356     return (ret);
357 }
358
359 #else
360
361 static int file_write(BIO *b, const char *in, int inl)
362 {
363     return -1;
364 }
365 static int file_read(BIO *b, char *out, int outl)
366 {
367     return -1;
368 }
369 static int file_puts(BIO *bp, const char *str)
370 {
371     return -1;
372 }
373 static int file_gets(BIO *bp, char *buf, int size)
374 {
375     return 0;
376 }
377 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
378 {
379     return 0;
380 }
381 static int file_new(BIO *bi)
382 {
383     return 0;
384 }
385 static int file_free(BIO *a)
386 {
387     return 0;
388 }
389
390 static const BIO_METHOD methods_filep = {
391     BIO_TYPE_FILE,
392     "FILE pointer",
393     file_write,
394     file_read,
395     file_puts,
396     file_gets,
397     file_ctrl,
398     file_new,
399     file_free,
400     NULL,
401 };
402
403 const BIO_METHOD *BIO_s_file(void)
404 {
405     return (&methods_filep);
406 }
407
408 BIO *BIO_new_file(const char *filename, const char *mode)
409 {
410     return NULL;
411 }
412
413 # endif                         /* OPENSSL_NO_STDIO */
414
415 #endif                          /* HEADER_BSS_FILE_C */