Coverage Report

Created: 2026-09-12 06:55

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