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