Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/bio/bss_file.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
    /* TODO: Convert to new style write function */
46
    bwrite_conv,
47
    file_write,
48
    /* TODO: Convert to new style read function */
49
    bread_conv,
50
    file_read,
51
    file_puts,
52
    file_gets,
53
    file_ctrl,
54
    file_new,
55
    file_free,
56
    NULL,                      /* file_callback_ctrl */
57
};
58
59
BIO *BIO_new_file(const char *filename, const char *mode)
60
0
{
61
0
    BIO  *ret;
62
0
    FILE *file = openssl_fopen(filename, mode);
63
0
    int fp_flags = BIO_CLOSE;
64
65
0
    if (strchr(mode, 'b') == NULL)
66
0
        fp_flags |= BIO_FP_TEXT;
67
68
0
    if (file == NULL) {
69
0
        SYSerr(SYS_F_FOPEN, get_last_sys_error());
70
0
        ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
71
0
        if (errno == ENOENT
72
0
#ifdef ENXIO
73
0
            || errno == ENXIO
74
0
#endif
75
0
            )
76
0
            BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
77
0
        else
78
0
            BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
79
0
        return NULL;
80
0
    }
81
0
    if ((ret = BIO_new(BIO_s_file())) == NULL) {
82
0
        fclose(file);
83
0
        return NULL;
84
0
    }
85
86
0
    BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
87
                                             * UPLINK */
88
0
    BIO_set_fp(ret, file, fp_flags);
89
0
    return ret;
90
0
}
91
92
BIO *BIO_new_fp(FILE *stream, int close_flag)
93
0
{
94
0
    BIO *ret;
95
96
0
    if ((ret = BIO_new(BIO_s_file())) == NULL)
97
0
        return NULL;
98
99
    /* redundant flag, left for documentation purposes */
100
0
    BIO_set_flags(ret, BIO_FLAGS_UPLINK);
101
0
    BIO_set_fp(ret, stream, close_flag);
102
0
    return ret;
103
0
}
104
105
const BIO_METHOD *BIO_s_file(void)
106
0
{
107
0
    return &methods_filep;
108
0
}
109
110
static int file_new(BIO *bi)
111
0
{
112
0
    bi->init = 0;
113
0
    bi->num = 0;
114
0
    bi->ptr = NULL;
115
0
    bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
116
0
    return 1;
117
0
}
118
119
static int file_free(BIO *a)
120
0
{
121
0
    if (a == NULL)
122
0
        return 0;
123
0
    if (a->shutdown) {
124
0
        if ((a->init) && (a->ptr != NULL)) {
125
0
            if (a->flags & BIO_FLAGS_UPLINK)
126
0
                UP_fclose(a->ptr);
127
0
            else
128
0
                fclose(a->ptr);
129
0
            a->ptr = NULL;
130
0
            a->flags = BIO_FLAGS_UPLINK;
131
0
        }
132
0
        a->init = 0;
133
0
    }
134
0
    return 1;
135
0
}
136
137
static int file_read(BIO *b, char *out, int outl)
138
0
{
139
0
    int ret = 0;
140
141
0
    if (b->init && (out != NULL)) {
142
0
        if (b->flags & BIO_FLAGS_UPLINK)
143
0
            ret = UP_fread(out, 1, (int)outl, b->ptr);
144
0
        else
145
0
            ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
146
0
        if (ret == 0
147
0
            && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
148
0
                                               ferror((FILE *)b->ptr)) {
149
0
            SYSerr(SYS_F_FREAD, get_last_sys_error());
150
0
            BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
151
0
            ret = -1;
152
0
        }
153
0
    }
154
0
    return ret;
155
0
}
156
157
static int file_write(BIO *b, const char *in, int inl)
158
0
{
159
0
    int ret = 0;
160
161
0
    if (b->init && (in != NULL)) {
162
0
        if (b->flags & BIO_FLAGS_UPLINK)
163
0
            ret = UP_fwrite(in, (int)inl, 1, b->ptr);
164
0
        else
165
0
            ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
166
0
        if (ret)
167
0
            ret = inl;
168
        /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
169
        /*
170
         * according to Tim Hudson <tjh@openssl.org>, the commented out
171
         * version above can cause 'inl' write calls under some stupid stdio
172
         * implementations (VMS)
173
         */
174
0
    }
175
0
    return 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)
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
0
        if (b->flags & BIO_FLAGS_UPLINK)
196
0
            ret = (long)UP_feof(fp);
197
0
        else
198
0
            ret = (long)feof(fp);
199
0
        break;
200
0
    case BIO_C_FILE_TELL:
201
0
    case BIO_CTRL_INFO:
202
0
        if (b->flags & BIO_FLAGS_UPLINK)
203
0
            ret = UP_ftell(b->ptr);
204
0
        else
205
0
            ret = ftell(fp);
206
0
        break;
207
0
    case BIO_C_SET_FILE_PTR:
208
0
        file_free(b);
209
0
        b->shutdown = (int)num & BIO_CLOSE;
210
0
        b->ptr = ptr;
211
0
        b->init = 1;
212
# if BIO_FLAGS_UPLINK!=0
213
#  if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
214
#   define _IOB_ENTRIES 20
215
#  endif
216
        /* Safety net to catch purely internal BIO_set_fp calls */
217
#  if defined(_MSC_VER) && _MSC_VER>=1900
218
        if (ptr == stdin || ptr == stdout || ptr == stderr)
219
            BIO_clear_flags(b, BIO_FLAGS_UPLINK);
220
#  elif defined(_IOB_ENTRIES)
221
        if ((size_t)ptr >= (size_t)stdin &&
222
            (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
223
            BIO_clear_flags(b, BIO_FLAGS_UPLINK);
224
#  endif
225
# endif
226
# ifdef UP_fsetmod
227
        if (b->flags & BIO_FLAGS_UPLINK)
228
            UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
229
        else
230
# endif
231
0
        {
232
# if defined(OPENSSL_SYS_WINDOWS)
233
            int fd = _fileno((FILE *)ptr);
234
            if (num & BIO_FP_TEXT)
235
                _setmode(fd, _O_TEXT);
236
            else
237
                _setmode(fd, _O_BINARY);
238
# elif defined(OPENSSL_SYS_MSDOS)
239
            int fd = fileno((FILE *)ptr);
240
            /* Set correct text/binary mode */
241
            if (num & BIO_FP_TEXT)
242
                _setmode(fd, _O_TEXT);
243
            /* Dangerous to set stdin/stdout to raw (unless redirected) */
244
            else {
245
                if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
246
                    if (isatty(fd) <= 0)
247
                        _setmode(fd, _O_BINARY);
248
                } else
249
                    _setmode(fd, _O_BINARY);
250
            }
251
# elif defined(OPENSSL_SYS_WIN32_CYGWIN)
252
            int fd = fileno((FILE *)ptr);
253
            if (!(num & BIO_FP_TEXT))
254
                setmode(fd, O_BINARY);
255
# endif
256
0
        }
257
0
        break;
258
0
    case BIO_C_SET_FILENAME:
259
0
        file_free(b);
260
0
        b->shutdown = (int)num & BIO_CLOSE;
261
0
        if (num & BIO_FP_APPEND) {
262
0
            if (num & BIO_FP_READ)
263
0
                OPENSSL_strlcpy(p, "a+", sizeof(p));
264
0
            else
265
0
                OPENSSL_strlcpy(p, "a", sizeof(p));
266
0
        } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
267
0
            OPENSSL_strlcpy(p, "r+", sizeof(p));
268
0
        else if (num & BIO_FP_WRITE)
269
0
            OPENSSL_strlcpy(p, "w", sizeof(p));
270
0
        else if (num & BIO_FP_READ)
271
0
            OPENSSL_strlcpy(p, "r", sizeof(p));
272
0
        else {
273
0
            BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
274
0
            ret = 0;
275
0
            break;
276
0
        }
277
# if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
278
        if (!(num & BIO_FP_TEXT))
279
            OPENSSL_strlcat(p, "b", sizeof(p));
280
        else
281
            OPENSSL_strlcat(p, "t", sizeof(p));
282
# elif defined(OPENSSL_SYS_WIN32_CYGWIN)
283
        if (!(num & BIO_FP_TEXT))
284
            OPENSSL_strlcat(p, "b", sizeof(p));
285
# endif
286
0
        fp = openssl_fopen(ptr, p);
287
0
        if (fp == NULL) {
288
0
            SYSerr(SYS_F_FOPEN, get_last_sys_error());
289
0
            ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
290
0
            BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
291
0
            ret = 0;
292
0
            break;
293
0
        }
294
0
        b->ptr = fp;
295
0
        b->init = 1;
296
0
        BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
297
                                               * UPLINK */
298
0
        break;
299
0
    case BIO_C_GET_FILE_PTR:
300
        /* the ptr parameter is actually a FILE ** in this case. */
301
0
        if (ptr != NULL) {
302
0
            fpp = (FILE **)ptr;
303
0
            *fpp = (FILE *)b->ptr;
304
0
        }
305
0
        break;
306
0
    case BIO_CTRL_GET_CLOSE:
307
0
        ret = (long)b->shutdown;
308
0
        break;
309
0
    case BIO_CTRL_SET_CLOSE:
310
0
        b->shutdown = (int)num;
311
0
        break;
312
0
    case BIO_CTRL_FLUSH:
313
0
        st = b->flags & BIO_FLAGS_UPLINK
314
0
                ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
315
0
        if (st == EOF) {
316
0
            SYSerr(SYS_F_FFLUSH, get_last_sys_error());
317
0
            ERR_add_error_data(1, "fflush()");
318
0
            BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
319
0
            ret = 0;
320
0
        }
321
0
        break;
322
0
    case BIO_CTRL_DUP:
323
0
        ret = 1;
324
0
        break;
325
326
0
    case BIO_CTRL_WPENDING:
327
0
    case BIO_CTRL_PENDING:
328
0
    case BIO_CTRL_PUSH:
329
0
    case BIO_CTRL_POP:
330
0
    default:
331
0
        ret = 0;
332
0
        break;
333
0
    }
334
0
    return ret;
335
0
}
336
337
static int file_gets(BIO *bp, char *buf, int size)
338
0
{
339
0
    int ret = 0;
340
341
0
    buf[0] = '\0';
342
0
    if (bp->flags & BIO_FLAGS_UPLINK) {
343
0
        if (!UP_fgets(buf, size, bp->ptr))
344
0
            goto err;
345
0
    } else {
346
0
        if (!fgets(buf, size, (FILE *)bp->ptr))
347
0
            goto err;
348
0
    }
349
0
    if (buf[0] != '\0')
350
0
        ret = strlen(buf);
351
0
 err:
352
0
    return ret;
353
0
}
354
355
static int file_puts(BIO *bp, const char *str)
356
0
{
357
0
    int n, ret;
358
359
0
    n = strlen(str);
360
0
    ret = file_write(bp, str, n);
361
0
    return ret;
362
0
}
363
364
#else
365
366
static int file_write(BIO *b, const char *in, int inl)
367
{
368
    return -1;
369
}
370
static int file_read(BIO *b, char *out, int outl)
371
{
372
    return -1;
373
}
374
static int file_puts(BIO *bp, const char *str)
375
{
376
    return -1;
377
}
378
static int file_gets(BIO *bp, char *buf, int size)
379
{
380
    return 0;
381
}
382
static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
383
{
384
    return 0;
385
}
386
static int file_new(BIO *bi)
387
{
388
    return 0;
389
}
390
static int file_free(BIO *a)
391
{
392
    return 0;
393
}
394
395
static const BIO_METHOD methods_filep = {
396
    BIO_TYPE_FILE,
397
    "FILE pointer",
398
    /* TODO: Convert to new style write function */
399
    bwrite_conv,
400
    file_write,
401
    /* TODO: Convert to new style read function */
402
    bread_conv,
403
    file_read,
404
    file_puts,
405
    file_gets,
406
    file_ctrl,
407
    file_new,
408
    file_free,
409
    NULL,                      /* file_callback_ctrl */
410
};
411
412
const BIO_METHOD *BIO_s_file(void)
413
{
414
    return &methods_filep;
415
}
416
417
BIO *BIO_new_file(const char *filename, const char *mode)
418
{
419
    return NULL;
420
}
421
422
#endif                         /* OPENSSL_NO_STDIO */