Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
12.4k
{
59
12.4k
    BIO *ret;
60
12.4k
    FILE *file = openssl_fopen(filename, mode);
61
12.4k
    int fp_flags = BIO_CLOSE;
62
63
12.4k
    if (strchr(mode, 'b') == NULL)
64
12.3k
        fp_flags |= BIO_FP_TEXT;
65
66
12.4k
    if (file == NULL) {
67
9.79k
        ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
68
9.79k
            "calling fopen(%s, %s)",
69
9.79k
            filename, mode);
70
9.79k
        if (errno == ENOENT
71
9.64k
#ifdef ENXIO
72
9.79k
            || errno == ENXIO
73
9.79k
#endif
74
9.79k
        )
75
9.79k
            ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE);
76
9.59k
        else
77
9.79k
            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
78
9.79k
        return NULL;
79
9.79k
    }
80
2.67k
    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
2.67k
    BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
87
2.67k
    BIO_set_fp(ret, file, fp_flags);
88
2.67k
    return ret;
89
2.67k
}
90
91
BIO *BIO_new_fp(FILE *stream, int close_flag)
92
99.1k
{
93
99.1k
    BIO *ret;
94
95
99.1k
    if ((ret = BIO_new(BIO_s_file())) == NULL)
96
0
        return NULL;
97
98
    /* redundant flag, left for documentation purposes */
99
99.1k
    BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
100
99.1k
    BIO_set_fp(ret, stream, close_flag);
101
99.1k
    return ret;
102
99.1k
}
103
104
const BIO_METHOD *BIO_s_file(void)
105
102k
{
106
102k
    return &methods_filep;
107
102k
}
108
109
static int file_new(BIO *bi)
110
102k
{
111
102k
    bi->init = 0;
112
102k
    bi->num = 0;
113
102k
    bi->ptr = NULL;
114
102k
    bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */
115
102k
    return 1;
116
102k
}
117
118
static int file_free(BIO *a)
119
204k
{
120
204k
    if (a == NULL)
121
0
        return 0;
122
204k
    if (a->shutdown) {
123
105k
        if ((a->init) && (a->ptr != NULL)) {
124
3.15k
            if (a->flags & BIO_FLAGS_UPLINK_INTERNAL)
125
0
                UP_fclose(a->ptr);
126
3.15k
            else
127
3.15k
                fclose(a->ptr);
128
3.15k
            a->ptr = NULL;
129
3.15k
            a->flags = BIO_FLAGS_UPLINK_INTERNAL;
130
3.15k
        }
131
105k
        a->init = 0;
132
105k
    }
133
204k
    return 1;
134
204k
}
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 && (out != NULL)) {
141
0
        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
142
0
            ret = UP_fread(out, 1, (int)outl, b->ptr);
143
0
        else
144
0
            ret = fread(out, 1, (int)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 = UP_fwrite(in, (int)inl, 1, b->ptr);
165
0
        else
166
0
            ret = fwrite(in, (int)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
44.0k
{
181
44.0k
    long ret = 1;
182
44.0k
    FILE *fp = (FILE *)b->ptr;
183
44.0k
    FILE **fpp;
184
44.0k
    char p[4];
185
44.0k
    int st;
186
187
44.0k
    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
0
        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
197
0
            ret = (long)UP_feof(fp);
198
0
        else
199
0
            ret = (long)feof(fp);
200
0
        break;
201
0
    case BIO_C_FILE_TELL:
202
0
    case BIO_CTRL_INFO:
203
0
        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
204
0
            ret = UP_ftell(b->ptr);
205
0
        else {
206
#if defined(OPENSSL_SYS_WINDOWS)
207
            /*
208
             * On Windows, for non-seekable files (stdin), ftell() is undefined.
209
             */
210
            if (GetFileType((HANDLE)_get_osfhandle(_fileno(fp))) != FILE_TYPE_DISK)
211
                ret = -1;
212
            else
213
                ret = ftell(fp);
214
#else
215
0
            ret = ftell(fp);
216
0
#endif
217
0
        }
218
0
        break;
219
44.0k
    case BIO_C_SET_FILE_PTR:
220
44.0k
        file_free(b);
221
44.0k
        b->shutdown = (int)num & BIO_CLOSE;
222
44.0k
        b->ptr = ptr;
223
44.0k
        b->init = 1;
224
#if BIO_FLAGS_UPLINK_INTERNAL != 0
225
#if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
226
#define _IOB_ENTRIES 20
227
#endif
228
        /* Safety net to catch purely internal BIO_set_fp calls */
229
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || defined(__BORLANDC__)
230
        if (ptr == stdin || ptr == stdout || ptr == stderr)
231
            BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
232
#elif defined(_IOB_ENTRIES)
233
        if ((size_t)ptr >= (size_t)stdin && (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
234
            BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
235
#endif
236
#endif
237
#ifdef UP_fsetmod
238
        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
239
            UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
240
        else
241
#endif
242
44.0k
        {
243
#if defined(OPENSSL_SYS_WINDOWS)
244
            int fd = _fileno((FILE *)ptr);
245
            if (num & BIO_FP_TEXT)
246
                _setmode(fd, _O_TEXT);
247
            else
248
                _setmode(fd, _O_BINARY);
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
44.0k
        }
268
44.0k
        break;
269
0
    case BIO_C_SET_FILENAME:
270
0
        file_free(b);
271
0
        b->shutdown = (int)num & BIO_CLOSE;
272
0
        if (num & BIO_FP_APPEND) {
273
0
            if (num & BIO_FP_READ)
274
0
                OPENSSL_strlcpy(p, "a+", sizeof(p));
275
0
            else
276
0
                OPENSSL_strlcpy(p, "a", sizeof(p));
277
0
        } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
278
0
            OPENSSL_strlcpy(p, "r+", sizeof(p));
279
0
        else if (num & BIO_FP_WRITE)
280
0
            OPENSSL_strlcpy(p, "w", sizeof(p));
281
0
        else if (num & BIO_FP_READ)
282
0
            OPENSSL_strlcpy(p, "r", sizeof(p));
283
0
        else {
284
0
            ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE);
285
0
            ret = 0;
286
0
            break;
287
0
        }
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
0
        fp = openssl_fopen(ptr, p);
298
0
        if (fp == NULL) {
299
0
            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
300
0
                "calling fopen(%s, %s)",
301
0
                (const char *)ptr, p);
302
0
            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
303
0
            ret = 0;
304
0
            break;
305
0
        }
306
0
        b->ptr = fp;
307
0
        b->init = 1;
308
        /* we did fopen -> we disengage UPLINK */
309
0
        BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
310
0
        break;
311
0
    case BIO_C_GET_FILE_PTR:
312
        /* the ptr parameter is actually a FILE ** in this case. */
313
0
        if (ptr != NULL) {
314
0
            fpp = (FILE **)ptr;
315
0
            if (BIO_FLAGS_UPLINK_INTERNAL == 0
316
0
                || b->flags & BIO_FLAGS_UPLINK_INTERNAL) {
317
0
                *fpp = (FILE *)b->ptr;
318
0
            } else { /* avoid returning internal FILE * to the app */
319
0
                *fpp = NULL;
320
0
                ret = 0;
321
0
            }
322
0
        }
323
0
        break;
324
0
    case BIO_CTRL_GET_CLOSE:
325
0
        ret = (long)b->shutdown;
326
0
        break;
327
0
    case BIO_CTRL_SET_CLOSE:
328
0
        b->shutdown = (int)num;
329
0
        break;
330
0
    case BIO_CTRL_FLUSH:
331
0
        st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
332
0
            ? UP_fflush(b->ptr)
333
0
            : fflush((FILE *)b->ptr);
334
0
        if (st == EOF) {
335
0
            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
336
0
                "calling fflush()");
337
0
            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
338
0
            ret = 0;
339
0
        }
340
0
        break;
341
0
    case BIO_CTRL_DUP:
342
0
        ret = 1;
343
0
        break;
344
345
0
    case BIO_CTRL_WPENDING:
346
0
    case BIO_CTRL_PENDING:
347
0
    case BIO_CTRL_PUSH:
348
0
    case BIO_CTRL_POP:
349
0
    default:
350
0
        ret = 0;
351
0
        break;
352
44.0k
    }
353
44.0k
    return ret;
354
44.0k
}
355
356
static int file_gets(BIO *bp, char *buf, int size)
357
656k
{
358
656k
    int ret = 0;
359
360
656k
    buf[0] = '\0';
361
656k
    if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
362
0
        if (!UP_fgets(buf, size, bp->ptr))
363
0
            goto err;
364
656k
    } else {
365
656k
        if (!fgets(buf, size, (FILE *)bp->ptr))
366
2.92k
            goto err;
367
656k
    }
368
653k
    if (buf[0] != '\0')
369
653k
        ret = strlen(buf);
370
656k
err:
371
656k
    return ret;
372
653k
}
373
374
static int file_puts(BIO *bp, const char *str)
375
0
{
376
0
    int n, ret;
377
378
0
    n = strlen(str);
379
0
    ret = file_write(bp, str, n);
380
0
    return ret;
381
0
}
382
383
#else
384
385
static int file_write(BIO *b, const char *in, int inl)
386
{
387
    return -1;
388
}
389
static int file_read(BIO *b, char *out, int outl)
390
{
391
    return -1;
392
}
393
static int file_puts(BIO *bp, const char *str)
394
{
395
    return -1;
396
}
397
static int file_gets(BIO *bp, char *buf, int size)
398
{
399
    return 0;
400
}
401
static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
402
{
403
    return 0;
404
}
405
static int file_new(BIO *bi)
406
{
407
    return 0;
408
}
409
static int file_free(BIO *a)
410
{
411
    return 0;
412
}
413
414
static const BIO_METHOD methods_filep = {
415
    BIO_TYPE_FILE,
416
    "FILE pointer",
417
    bwrite_conv,
418
    file_write,
419
    bread_conv,
420
    file_read,
421
    file_puts,
422
    file_gets,
423
    file_ctrl,
424
    file_new,
425
    file_free,
426
    NULL, /* file_callback_ctrl */
427
};
428
429
const BIO_METHOD *BIO_s_file(void)
430
{
431
    return &methods_filep;
432
}
433
434
BIO *BIO_new_file(const char *filename, const char *mode)
435
{
436
    return NULL;
437
}
438
439
#endif /* OPENSSL_NO_STDIO */