Coverage Report

Created: 2026-04-01 06:58

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