Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bio/bss_file.c
Line
Count
Source
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 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
11.4k
{
59
11.4k
    BIO *ret;
60
11.4k
    FILE *file = openssl_fopen(filename, mode);
61
11.4k
    int fp_flags = BIO_CLOSE;
62
63
11.4k
    if (strchr(mode, 'b') == NULL)
64
11.2k
        fp_flags |= BIO_FP_TEXT;
65
66
11.4k
    if (file == NULL) {
67
9.00k
        ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
68
9.00k
            "calling fopen(%s, %s)",
69
9.00k
            filename, mode);
70
9.00k
        if (errno == ENOENT
71
8.81k
#ifdef ENXIO
72
9.00k
            || errno == ENXIO
73
9.00k
#endif
74
9.00k
        )
75
9.00k
            ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE);
76
8.77k
        else
77
9.00k
            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
78
9.00k
        return NULL;
79
9.00k
    }
80
2.44k
    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.44k
    BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
87
2.44k
    BIO_set_fp(ret, file, fp_flags);
88
2.44k
    return ret;
89
2.44k
}
90
91
BIO *BIO_new_fp(FILE *stream, int close_flag)
92
124k
{
93
124k
    BIO *ret;
94
95
124k
    if ((ret = BIO_new(BIO_s_file())) == NULL)
96
0
        return NULL;
97
98
    /* redundant flag, left for documentation purposes */
99
124k
    BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
100
124k
    BIO_set_fp(ret, stream, close_flag);
101
124k
    return ret;
102
124k
}
103
104
const BIO_METHOD *BIO_s_file(void)
105
126k
{
106
126k
    return &methods_filep;
107
126k
}
108
109
static int file_new(BIO *bi)
110
126k
{
111
126k
    bi->init = 0;
112
126k
    bi->num = 0;
113
126k
    bi->ptr = NULL;
114
126k
    bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */
115
126k
    return 1;
116
126k
}
117
118
static int file_free(BIO *a)
119
253k
{
120
253k
    if (a == NULL)
121
0
        return 0;
122
253k
    if (a->shutdown) {
123
129k
        if ((a->init) && (a->ptr != NULL)) {
124
2.44k
            if (a->flags & BIO_FLAGS_UPLINK_INTERNAL)
125
0
                UP_fclose(a->ptr);
126
2.44k
            else
127
2.44k
                fclose(a->ptr);
128
2.44k
            a->ptr = NULL;
129
2.44k
            a->flags = BIO_FLAGS_UPLINK_INTERNAL;
130
2.44k
        }
131
129k
        a->init = 0;
132
129k
    }
133
253k
    return 1;
134
253k
}
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
102k
{
181
102k
    long ret = 1;
182
102k
    FILE *fp = (FILE *)b->ptr;
183
102k
    FILE **fpp;
184
102k
    char p[4];
185
102k
    int st;
186
187
102k
    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
102k
    case BIO_C_SET_FILE_PTR:
220
102k
        file_free(b);
221
102k
        b->shutdown = (int)num & BIO_CLOSE;
222
102k
        b->ptr = ptr;
223
102k
        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
102k
        {
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
            /*
250
             * Reports show that ftell() isn't trustable in text mode.
251
             * This has been confirmed as a bug in the Universal C RTL, see
252
             * https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html
253
             * The suggested work-around from Microsoft engineering is to
254
             * turn off buffering until the bug is resolved.
255
             */
256
            if ((num & BIO_FP_TEXT) != 0)
257
                setvbuf((FILE *)ptr, NULL, _IONBF, 0);
258
#elif defined(OPENSSL_SYS_MSDOS)
259
            int fd = fileno((FILE *)ptr);
260
            /* Set correct text/binary mode */
261
            if (num & BIO_FP_TEXT)
262
                _setmode(fd, _O_TEXT);
263
            /* Dangerous to set stdin/stdout to raw (unless redirected) */
264
            else {
265
                if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
266
                    if (isatty(fd) <= 0)
267
                        _setmode(fd, _O_BINARY);
268
                } else
269
                    _setmode(fd, _O_BINARY);
270
            }
271
#elif defined(OPENSSL_SYS_WIN32_CYGWIN)
272
            int fd = fileno((FILE *)ptr);
273
            if (!(num & BIO_FP_TEXT))
274
                setmode(fd, O_BINARY);
275
#endif
276
102k
        }
277
102k
        break;
278
0
    case BIO_C_SET_FILENAME:
279
0
        file_free(b);
280
0
        b->shutdown = (int)num & BIO_CLOSE;
281
0
        if (num & BIO_FP_APPEND) {
282
0
            if (num & BIO_FP_READ)
283
0
                OPENSSL_strlcpy(p, "a+", sizeof(p));
284
0
            else
285
0
                OPENSSL_strlcpy(p, "a", sizeof(p));
286
0
        } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
287
0
            OPENSSL_strlcpy(p, "r+", sizeof(p));
288
0
        else if (num & BIO_FP_WRITE)
289
0
            OPENSSL_strlcpy(p, "w", sizeof(p));
290
0
        else if (num & BIO_FP_READ)
291
0
            OPENSSL_strlcpy(p, "r", sizeof(p));
292
0
        else {
293
0
            ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE);
294
0
            ret = 0;
295
0
            break;
296
0
        }
297
#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
298
        if (!(num & BIO_FP_TEXT))
299
            OPENSSL_strlcat(p, "b", sizeof(p));
300
        else
301
            OPENSSL_strlcat(p, "t", sizeof(p));
302
#elif defined(OPENSSL_SYS_WIN32_CYGWIN)
303
        if (!(num & BIO_FP_TEXT))
304
            OPENSSL_strlcat(p, "b", sizeof(p));
305
#endif
306
0
        fp = openssl_fopen(ptr, p);
307
0
        if (fp == NULL) {
308
0
            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
309
0
                "calling fopen(%s, %s)",
310
0
                (const char *)ptr, p);
311
0
            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
312
0
            ret = 0;
313
0
            break;
314
0
        }
315
0
        b->ptr = fp;
316
0
        b->init = 1;
317
        /* we did fopen -> we disengage UPLINK */
318
0
        BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
319
0
        break;
320
0
    case BIO_C_GET_FILE_PTR:
321
        /* the ptr parameter is actually a FILE ** in this case. */
322
0
        if (ptr != NULL) {
323
0
            fpp = (FILE **)ptr;
324
0
            *fpp = (FILE *)b->ptr;
325
0
        }
326
0
        break;
327
0
    case BIO_CTRL_GET_CLOSE:
328
0
        ret = (long)b->shutdown;
329
0
        break;
330
0
    case BIO_CTRL_SET_CLOSE:
331
0
        b->shutdown = (int)num;
332
0
        break;
333
0
    case BIO_CTRL_FLUSH:
334
0
        st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
335
0
            ? UP_fflush(b->ptr)
336
0
            : fflush((FILE *)b->ptr);
337
0
        if (st == EOF) {
338
0
            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
339
0
                "calling fflush()");
340
0
            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
341
0
            ret = 0;
342
0
        }
343
0
        break;
344
0
    case BIO_CTRL_DUP:
345
0
        ret = 1;
346
0
        break;
347
348
0
    case BIO_CTRL_WPENDING:
349
0
    case BIO_CTRL_PENDING:
350
0
    case BIO_CTRL_PUSH:
351
0
    case BIO_CTRL_POP:
352
0
    default:
353
0
        ret = 0;
354
0
        break;
355
102k
    }
356
102k
    return ret;
357
102k
}
358
359
static int file_gets(BIO *bp, char *buf, int size)
360
512k
{
361
512k
    int ret = 0;
362
363
512k
    buf[0] = '\0';
364
512k
    if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
365
0
        if (!UP_fgets(buf, size, bp->ptr))
366
0
            goto err;
367
512k
    } else {
368
512k
        if (!fgets(buf, size, (FILE *)bp->ptr))
369
2.31k
            goto err;
370
512k
    }
371
510k
    if (buf[0] != '\0')
372
510k
        ret = strlen(buf);
373
512k
err:
374
512k
    return ret;
375
510k
}
376
377
static int file_puts(BIO *bp, const char *str)
378
0
{
379
0
    int n, ret;
380
381
0
    n = strlen(str);
382
0
    ret = file_write(bp, str, n);
383
0
    return ret;
384
0
}
385
386
#else
387
388
static int file_write(BIO *b, const char *in, int inl)
389
{
390
    return -1;
391
}
392
static int file_read(BIO *b, char *out, int outl)
393
{
394
    return -1;
395
}
396
static int file_puts(BIO *bp, const char *str)
397
{
398
    return -1;
399
}
400
static int file_gets(BIO *bp, char *buf, int size)
401
{
402
    return 0;
403
}
404
static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
405
{
406
    return 0;
407
}
408
static int file_new(BIO *bi)
409
{
410
    return 0;
411
}
412
static int file_free(BIO *a)
413
{
414
    return 0;
415
}
416
417
static const BIO_METHOD methods_filep = {
418
    BIO_TYPE_FILE,
419
    "FILE pointer",
420
    bwrite_conv,
421
    file_write,
422
    bread_conv,
423
    file_read,
424
    file_puts,
425
    file_gets,
426
    file_ctrl,
427
    file_new,
428
    file_free,
429
    NULL, /* file_callback_ctrl */
430
};
431
432
const BIO_METHOD *BIO_s_file(void)
433
{
434
    return &methods_filep;
435
}
436
437
BIO *BIO_new_file(const char *filename, const char *mode)
438
{
439
    return NULL;
440
}
441
442
#endif /* OPENSSL_NO_STDIO */