Coverage Report

Created: 2026-08-23 06:38

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