xref: /freebsd/crypto/openssl/crypto/bio/bss_file.c (revision 046c625e9382e17da953767b881aaa782fa73af8)
1 /*
2  * Copyright 1995-2025 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 one 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     bwrite_conv,
46     file_write,
47     bread_conv,
48     file_read,
49     file_puts,
50     file_gets,
51     file_ctrl,
52     file_new,
53     file_free,
54     NULL,                      /* file_callback_ctrl */
55 };
56 
BIO_new_file(const char * filename,const char * mode)57 BIO *BIO_new_file(const char *filename, const char *mode)
58 {
59     BIO  *ret;
60     FILE *file = openssl_fopen(filename, mode);
61     int fp_flags = BIO_CLOSE;
62 
63     if (strchr(mode, 'b') == NULL)
64         fp_flags |= BIO_FP_TEXT;
65 
66     if (file == NULL) {
67         ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
68                        "calling fopen(%s, %s)",
69                        filename, mode);
70         if (errno == ENOENT
71 #ifdef ENXIO
72             || errno == ENXIO
73 #endif
74             )
75             ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE);
76         else
77             ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
78         return NULL;
79     }
80     if ((ret = BIO_new(BIO_s_file())) == NULL) {
81         fclose(file);
82         return NULL;
83     }
84 
85     /* we did fopen -> we disengage UPLINK */
86     BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
87     BIO_set_fp(ret, file, fp_flags);
88     return ret;
89 }
90 
BIO_new_fp(FILE * stream,int close_flag)91 BIO *BIO_new_fp(FILE *stream, int close_flag)
92 {
93     BIO *ret;
94 
95     if ((ret = BIO_new(BIO_s_file())) == NULL)
96         return NULL;
97 
98     /* redundant flag, left for documentation purposes */
99     BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
100     BIO_set_fp(ret, stream, close_flag);
101     return ret;
102 }
103 
BIO_s_file(void)104 const BIO_METHOD *BIO_s_file(void)
105 {
106     return &methods_filep;
107 }
108 
file_new(BIO * bi)109 static int file_new(BIO *bi)
110 {
111     bi->init = 0;
112     bi->num = 0;
113     bi->ptr = NULL;
114     bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */
115     return 1;
116 }
117 
file_free(BIO * a)118 static int file_free(BIO *a)
119 {
120     if (a == NULL)
121         return 0;
122     if (a->shutdown) {
123         if ((a->init) && (a->ptr != NULL)) {
124             if (a->flags & BIO_FLAGS_UPLINK_INTERNAL)
125                 UP_fclose(a->ptr);
126             else
127                 fclose(a->ptr);
128             a->ptr = NULL;
129             a->flags = BIO_FLAGS_UPLINK_INTERNAL;
130         }
131         a->init = 0;
132     }
133     return 1;
134 }
135 
file_read(BIO * b,char * out,int outl)136 static int file_read(BIO *b, char *out, int outl)
137 {
138     int ret = 0;
139 
140     if (b->init && (out != NULL)) {
141         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
142             ret = UP_fread(out, 1, (int)outl, b->ptr);
143         else
144             ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
145         if (ret == 0
146             && (b->flags & BIO_FLAGS_UPLINK_INTERNAL
147                 ? UP_ferror((FILE *)b->ptr) : ferror((FILE *)b->ptr))) {
148             ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
149                            "calling fread()");
150             ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
151             ret = -1;
152         }
153     }
154     return ret;
155 }
156 
file_write(BIO * b,const char * in,int inl)157 static int file_write(BIO *b, const char *in, int inl)
158 {
159     int ret = 0;
160 
161     if (b->init && (in != NULL)) {
162         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
163             ret = UP_fwrite(in, (int)inl, 1, b->ptr);
164         else
165             ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
166         if (ret)
167             ret = inl;
168         /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
169         /*
170          * according to Tim Hudson <tjh@openssl.org>, the commented out
171          * version above can cause 'inl' write calls under some stupid stdio
172          * implementations (VMS)
173          */
174     }
175     return ret;
176 }
177 
file_ctrl(BIO * b,int cmd,long num,void * ptr)178 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
179 {
180     long ret = 1;
181     FILE *fp = (FILE *)b->ptr;
182     FILE **fpp;
183     char p[4];
184     int st;
185 
186     switch (cmd) {
187     case BIO_C_FILE_SEEK:
188     case BIO_CTRL_RESET:
189         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
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_INTERNAL)
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_INTERNAL)
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_INTERNAL!=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) || defined(__BORLANDC__)
218         if (ptr == stdin || ptr == stdout || ptr == stderr)
219             BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
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_INTERNAL);
224 #  endif
225 # endif
226 # ifdef UP_fsetmod
227         if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
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_BINARY);
255 # endif
256         }
257         break;
258     case BIO_C_SET_FILENAME:
259         file_free(b);
260         b->shutdown = (int)num & BIO_CLOSE;
261         if (num & BIO_FP_APPEND) {
262             if (num & BIO_FP_READ)
263                 OPENSSL_strlcpy(p, "a+", sizeof(p));
264             else
265                 OPENSSL_strlcpy(p, "a", sizeof(p));
266         } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
267             OPENSSL_strlcpy(p, "r+", sizeof(p));
268         else if (num & BIO_FP_WRITE)
269             OPENSSL_strlcpy(p, "w", sizeof(p));
270         else if (num & BIO_FP_READ)
271             OPENSSL_strlcpy(p, "r", sizeof(p));
272         else {
273             ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE);
274             ret = 0;
275             break;
276         }
277 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
278         if (!(num & BIO_FP_TEXT))
279             OPENSSL_strlcat(p, "b", sizeof(p));
280         else
281             OPENSSL_strlcat(p, "t", sizeof(p));
282 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
283         if (!(num & BIO_FP_TEXT))
284             OPENSSL_strlcat(p, "b", sizeof(p));
285 # endif
286         fp = openssl_fopen(ptr, p);
287         if (fp == NULL) {
288             ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
289                            "calling fopen(%s, %s)",
290                            (const char *)ptr, p);
291             ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
292             ret = 0;
293             break;
294         }
295         b->ptr = fp;
296         b->init = 1;
297         /* we did fopen -> we disengage UPLINK */
298         BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
299         break;
300     case BIO_C_GET_FILE_PTR:
301         /* the ptr parameter is actually a FILE ** in this case. */
302         if (ptr != NULL) {
303             fpp = (FILE **)ptr;
304             *fpp = (FILE *)b->ptr;
305         }
306         break;
307     case BIO_CTRL_GET_CLOSE:
308         ret = (long)b->shutdown;
309         break;
310     case BIO_CTRL_SET_CLOSE:
311         b->shutdown = (int)num;
312         break;
313     case BIO_CTRL_FLUSH:
314         st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
315                 ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
316         if (st == EOF) {
317             ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
318                            "calling fflush()");
319             ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
320             ret = 0;
321         }
322         break;
323     case BIO_CTRL_DUP:
324         ret = 1;
325         break;
326 
327     case BIO_CTRL_WPENDING:
328     case BIO_CTRL_PENDING:
329     case BIO_CTRL_PUSH:
330     case BIO_CTRL_POP:
331     default:
332         ret = 0;
333         break;
334     }
335     return ret;
336 }
337 
file_gets(BIO * bp,char * buf,int size)338 static int file_gets(BIO *bp, char *buf, int size)
339 {
340     int ret = 0;
341 
342     buf[0] = '\0';
343     if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
344         if (!UP_fgets(buf, size, bp->ptr))
345             goto err;
346     } else {
347         if (!fgets(buf, size, (FILE *)bp->ptr))
348             goto err;
349     }
350     if (buf[0] != '\0')
351         ret = strlen(buf);
352  err:
353     return ret;
354 }
355 
file_puts(BIO * bp,const char * str)356 static int file_puts(BIO *bp, const char *str)
357 {
358     int n, ret;
359 
360     n = strlen(str);
361     ret = file_write(bp, str, n);
362     return ret;
363 }
364 
365 #else
366 
file_write(BIO * b,const char * in,int inl)367 static int file_write(BIO *b, const char *in, int inl)
368 {
369     return -1;
370 }
file_read(BIO * b,char * out,int outl)371 static int file_read(BIO *b, char *out, int outl)
372 {
373     return -1;
374 }
file_puts(BIO * bp,const char * str)375 static int file_puts(BIO *bp, const char *str)
376 {
377     return -1;
378 }
file_gets(BIO * bp,char * buf,int size)379 static int file_gets(BIO *bp, char *buf, int size)
380 {
381     return 0;
382 }
file_ctrl(BIO * b,int cmd,long num,void * ptr)383 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
384 {
385     return 0;
386 }
file_new(BIO * bi)387 static int file_new(BIO *bi)
388 {
389     return 0;
390 }
file_free(BIO * a)391 static int file_free(BIO *a)
392 {
393     return 0;
394 }
395 
396 static const BIO_METHOD methods_filep = {
397     BIO_TYPE_FILE,
398     "FILE pointer",
399     bwrite_conv,
400     file_write,
401     bread_conv,
402     file_read,
403     file_puts,
404     file_gets,
405     file_ctrl,
406     file_new,
407     file_free,
408     NULL,                      /* file_callback_ctrl */
409 };
410 
BIO_s_file(void)411 const BIO_METHOD *BIO_s_file(void)
412 {
413     return &methods_filep;
414 }
415 
BIO_new_file(const char * filename,const char * mode)416 BIO *BIO_new_file(const char *filename, const char *mode)
417 {
418     return NULL;
419 }
420 
421 #endif                         /* OPENSSL_NO_STDIO */
422