Coverage Report

Created: 2026-04-12 07:08

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