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