Coverage Report

Created: 2026-06-18 06:34

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