Add ossl_siv symbols
[openssl.git] / crypto / bio / bss_file.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #if defined(__linux) || defined(__sun) || defined(__hpux)
11 /*
12  * Following definition aliases fopen to fopen64 on above mentioned
13  * platforms. This makes it possible to open and sequentially access files
14  * larger than 2GB from 32-bit application. It does not allow to traverse
15  * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
16  * platform permits that, not with fseek/ftell. Not to mention that breaking
17  * 2GB limit for seeking would require surgery to *our* API. But sequential
18  * access suffices for practical cases when you can run into large files,
19  * such as fingerprinting, so we can let API alone. For reference, the list
20  * of 32-bit platforms which allow for sequential access of large files
21  * without extra "magic" comprise *BSD, Darwin, IRIX...
22  */
23 # ifndef _FILE_OFFSET_BITS
24 #  define _FILE_OFFSET_BITS 64
25 # endif
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include "bio_local.h"
31 #include <openssl/err.h>
32
33 #if !defined(OPENSSL_NO_STDIO)
34
35 static int file_write(BIO *h, const char *buf, int num);
36 static int file_read(BIO *h, char *buf, int size);
37 static int file_puts(BIO *h, const char *str);
38 static int file_gets(BIO *h, char *str, int size);
39 static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
40 static int file_new(BIO *h);
41 static int file_free(BIO *data);
42 static const BIO_METHOD methods_filep = {
43     BIO_TYPE_FILE,
44     "FILE pointer",
45     /* TODO: Convert to new style write function */
46     bwrite_conv,
47     file_write,
48     /* TODO: Convert to new style read function */
49     bread_conv,
50     file_read,
51     file_puts,
52     file_gets,
53     file_ctrl,
54     file_new,
55     file_free,
56     NULL,                      /* file_callback_ctrl */
57 };
58
59 BIO *BIO_new_file(const char *filename, const char *mode)
60 {
61     BIO  *ret;
62     FILE *file = openssl_fopen(filename, mode);
63     int fp_flags = BIO_CLOSE;
64
65     if (strchr(mode, 'b') == NULL)
66         fp_flags |= BIO_FP_TEXT;
67
68     if (file == NULL) {
69         ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
70                        "calling fopen(%s, %s)",
71                        filename, mode);
72         if (errno == ENOENT
73 #ifdef ENXIO
74             || errno == ENXIO
75 #endif
76             )
77             ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE);
78         else
79             ERR_raise(ERR_LIB_BIO, 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     /* we did fopen -> we disengage UPLINK */
88     BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
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_INTERNAL);
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_INTERNAL; /* 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_INTERNAL)
127                 UP_fclose(a->ptr);
128             else
129                 fclose(a->ptr);
130             a->ptr = NULL;
131             a->flags = BIO_FLAGS_UPLINK_INTERNAL;
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_INTERNAL)
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_INTERNAL
149                 ? UP_ferror((FILE *)b->ptr) : ferror((FILE *)b->ptr))) {
150             ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
151                            "calling fread()");
152             ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
153             ret = -1;
154         }
155     }
156     return ret;
157 }
158
159 static int file_write(BIO *b, const char *in, int inl)
160 {
161     int ret = 0;
162
163     if (b->init && (in != NULL)) {
164         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
165             ret = UP_fwrite(in, (int)inl, 1, b->ptr);
166         else
167             ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
168         if (ret)
169             ret = inl;
170         /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
171         /*
172          * according to Tim Hudson <tjh@openssl.org>, the commented out
173          * version above can cause 'inl' write calls under some stupid stdio
174          * implementations (VMS)
175          */
176     }
177     return ret;
178 }
179
180 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
181 {
182     long ret = 1;
183     FILE *fp = (FILE *)b->ptr;
184     FILE **fpp;
185     char p[4];
186     int st;
187
188     switch (cmd) {
189     case BIO_C_FILE_SEEK:
190     case BIO_CTRL_RESET:
191         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
192             ret = (long)UP_fseek(b->ptr, num, 0);
193         else
194             ret = (long)fseek(fp, num, 0);
195         break;
196     case BIO_CTRL_EOF:
197         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
198             ret = (long)UP_feof(fp);
199         else
200             ret = (long)feof(fp);
201         break;
202     case BIO_C_FILE_TELL:
203     case BIO_CTRL_INFO:
204         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
205             ret = UP_ftell(b->ptr);
206         else
207             ret = ftell(fp);
208         break;
209     case BIO_C_SET_FILE_PTR:
210         file_free(b);
211         b->shutdown = (int)num & BIO_CLOSE;
212         b->ptr = ptr;
213         b->init = 1;
214 # if BIO_FLAGS_UPLINK_INTERNAL!=0
215 #  if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
216 #   define _IOB_ENTRIES 20
217 #  endif
218         /* Safety net to catch purely internal BIO_set_fp calls */
219 #  if defined(_MSC_VER) && _MSC_VER>=1900
220         if (ptr == stdin || ptr == stdout || ptr == stderr)
221             BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
222 #  elif defined(_IOB_ENTRIES)
223         if ((size_t)ptr >= (size_t)stdin &&
224             (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
225             BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
226 #  endif
227 # endif
228 # ifdef UP_fsetmod
229         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
230             UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
231         else
232 # endif
233         {
234 # if defined(OPENSSL_SYS_WINDOWS)
235             int fd = _fileno((FILE *)ptr);
236             if (num & BIO_FP_TEXT)
237                 _setmode(fd, _O_TEXT);
238             else
239                 _setmode(fd, _O_BINARY);
240             /*
241              * Reports show that ftell() isn't trustable in text mode.
242              * This has been confirmed as a bug in the Universal C RTL, see
243              * https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html
244              * The suggested work-around from Microsoft engineering is to
245              * turn off buffering until the bug is resolved.
246              */
247             if ((num & BIO_FP_TEXT) != 0)
248                 setvbuf((FILE *)ptr, NULL, _IONBF, 0);
249 # elif defined(OPENSSL_SYS_MSDOS)
250             int fd = fileno((FILE *)ptr);
251             /* Set correct text/binary mode */
252             if (num & BIO_FP_TEXT)
253                 _setmode(fd, _O_TEXT);
254             /* Dangerous to set stdin/stdout to raw (unless redirected) */
255             else {
256                 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
257                     if (isatty(fd) <= 0)
258                         _setmode(fd, _O_BINARY);
259                 } else
260                     _setmode(fd, _O_BINARY);
261             }
262 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
263             int fd = fileno((FILE *)ptr);
264             if (!(num & BIO_FP_TEXT))
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             ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE);
285             ret = 0;
286             break;
287         }
288 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
289         if (!(num & BIO_FP_TEXT))
290             OPENSSL_strlcat(p, "b", sizeof(p));
291         else
292             OPENSSL_strlcat(p, "t", sizeof(p));
293 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
294         if (!(num & BIO_FP_TEXT))
295             OPENSSL_strlcat(p, "b", sizeof(p));
296 # endif
297         fp = openssl_fopen(ptr, p);
298         if (fp == NULL) {
299             ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
300                            "calling fopen(%s, %s)",
301                            ptr, p);
302             ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
303             ret = 0;
304             break;
305         }
306         b->ptr = fp;
307         b->init = 1;
308         /* we did fopen -> we disengage UPLINK */
309         BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
310         break;
311     case BIO_C_GET_FILE_PTR:
312         /* the ptr parameter is actually a FILE ** in this case. */
313         if (ptr != NULL) {
314             fpp = (FILE **)ptr;
315             *fpp = (FILE *)b->ptr;
316         }
317         break;
318     case BIO_CTRL_GET_CLOSE:
319         ret = (long)b->shutdown;
320         break;
321     case BIO_CTRL_SET_CLOSE:
322         b->shutdown = (int)num;
323         break;
324     case BIO_CTRL_FLUSH:
325         st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
326                 ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
327         if (st == EOF) {
328             ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
329                            "calling fflush()");
330             ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
331             ret = 0;
332         }
333         break;
334     case BIO_CTRL_DUP:
335         ret = 1;
336         break;
337
338     case BIO_CTRL_WPENDING:
339     case BIO_CTRL_PENDING:
340     case BIO_CTRL_PUSH:
341     case BIO_CTRL_POP:
342     default:
343         ret = 0;
344         break;
345     }
346     return ret;
347 }
348
349 static int file_gets(BIO *bp, char *buf, int size)
350 {
351     int ret = 0;
352
353     buf[0] = '\0';
354     if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
355         if (!UP_fgets(buf, size, bp->ptr))
356             goto err;
357     } else {
358         if (!fgets(buf, size, (FILE *)bp->ptr))
359             goto err;
360     }
361     if (buf[0] != '\0')
362         ret = strlen(buf);
363  err:
364     return ret;
365 }
366
367 static int file_puts(BIO *bp, const char *str)
368 {
369     int n, ret;
370
371     n = strlen(str);
372     ret = file_write(bp, str, n);
373     return ret;
374 }
375
376 #else
377
378 static int file_write(BIO *b, const char *in, int inl)
379 {
380     return -1;
381 }
382 static int file_read(BIO *b, char *out, int outl)
383 {
384     return -1;
385 }
386 static int file_puts(BIO *bp, const char *str)
387 {
388     return -1;
389 }
390 static int file_gets(BIO *bp, char *buf, int size)
391 {
392     return 0;
393 }
394 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
395 {
396     return 0;
397 }
398 static int file_new(BIO *bi)
399 {
400     return 0;
401 }
402 static int file_free(BIO *a)
403 {
404     return 0;
405 }
406
407 static const BIO_METHOD methods_filep = {
408     BIO_TYPE_FILE,
409     "FILE pointer",
410     /* TODO: Convert to new style write function */
411     bwrite_conv,
412     file_write,
413     /* TODO: Convert to new style read function */
414     bread_conv,
415     file_read,
416     file_puts,
417     file_gets,
418     file_ctrl,
419     file_new,
420     file_free,
421     NULL,                      /* file_callback_ctrl */
422 };
423
424 const BIO_METHOD *BIO_s_file(void)
425 {
426     return &methods_filep;
427 }
428
429 BIO *BIO_new_file(const char *filename, const char *mode)
430 {
431     return NULL;
432 }
433
434 #endif                         /* OPENSSL_NO_STDIO */