Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/bio/bss_file.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2017 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
#ifndef HEADER_BSS_FILE_C
11
# define HEADER_BSS_FILE_C
12
13
# if defined(__linux) || defined(__sun) || defined(__hpux)
14
/*
15
 * Following definition aliases fopen to fopen64 on above mentioned
16
 * platforms. This makes it possible to open and sequentially access files
17
 * larger than 2GB from 32-bit application. It does not allow to traverse
18
 * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
19
 * platform permits that, not with fseek/ftell. Not to mention that breaking
20
 * 2GB limit for seeking would require surgery to *our* API. But sequential
21
 * access suffices for practical cases when you can run into large files,
22
 * such as fingerprinting, so we can let API alone. For reference, the list
23
 * of 32-bit platforms which allow for sequential access of large files
24
 * without extra "magic" comprise *BSD, Darwin, IRIX...
25
 */
26
#  ifndef _FILE_OFFSET_BITS
27
#   define _FILE_OFFSET_BITS 64
28
#  endif
29
# endif
30
31
# include <stdio.h>
32
# include <errno.h>
33
# include "bio_lcl.h"
34
# include <openssl/err.h>
35
36
# if !defined(OPENSSL_NO_STDIO)
37
38
static int file_write(BIO *h, const char *buf, int num);
39
static int file_read(BIO *h, char *buf, int size);
40
static int file_puts(BIO *h, const char *str);
41
static int file_gets(BIO *h, char *str, int size);
42
static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
43
static int file_new(BIO *h);
44
static int file_free(BIO *data);
45
static const BIO_METHOD methods_filep = {
46
    BIO_TYPE_FILE,
47
    "FILE pointer",
48
    /* TODO: Convert to new style write function */
49
    bwrite_conv,
50
    file_write,
51
    /* TODO: Convert to new style read function */
52
    bread_conv,
53
    file_read,
54
    file_puts,
55
    file_gets,
56
    file_ctrl,
57
    file_new,
58
    file_free,
59
    NULL,                      /* file_callback_ctrl */
60
};
61
62
BIO *BIO_new_file(const char *filename, const char *mode)
63
0
{
64
0
    BIO  *ret;
65
0
    FILE *file = openssl_fopen(filename, mode);
66
0
    int fp_flags = BIO_CLOSE;
67
0
68
0
    if (strchr(mode, 'b') == NULL)
69
0
        fp_flags |= BIO_FP_TEXT;
70
0
71
0
    if (file == NULL) {
72
0
        SYSerr(SYS_F_FOPEN, get_last_sys_error());
73
0
        ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
74
0
        if (errno == ENOENT
75
0
# ifdef ENXIO
76
0
            || errno == ENXIO
77
0
# endif
78
0
            )
79
0
            BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
80
0
        else
81
0
            BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
82
0
        return NULL;
83
0
    }
84
0
    if ((ret = BIO_new(BIO_s_file())) == NULL) {
85
0
        fclose(file);
86
0
        return NULL;
87
0
    }
88
0
89
0
    BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
90
0
                                             * UPLINK */
91
0
    BIO_set_fp(ret, file, fp_flags);
92
0
    return ret;
93
0
}
94
95
BIO *BIO_new_fp(FILE *stream, int close_flag)
96
0
{
97
0
    BIO *ret;
98
0
99
0
    if ((ret = BIO_new(BIO_s_file())) == NULL)
100
0
        return NULL;
101
0
102
0
    /* redundant flag, left for documentation purposes */
103
0
    BIO_set_flags(ret, BIO_FLAGS_UPLINK);
104
0
    BIO_set_fp(ret, stream, close_flag);
105
0
    return ret;
106
0
}
107
108
const BIO_METHOD *BIO_s_file(void)
109
0
{
110
0
    return &methods_filep;
111
0
}
112
113
static int file_new(BIO *bi)
114
0
{
115
0
    bi->init = 0;
116
0
    bi->num = 0;
117
0
    bi->ptr = NULL;
118
0
    bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
119
0
    return 1;
120
0
}
121
122
static int file_free(BIO *a)
123
0
{
124
0
    if (a == NULL)
125
0
        return 0;
126
0
    if (a->shutdown) {
127
0
        if ((a->init) && (a->ptr != NULL)) {
128
0
            if (a->flags & BIO_FLAGS_UPLINK)
129
0
                UP_fclose(a->ptr);
130
0
            else
131
0
                fclose(a->ptr);
132
0
            a->ptr = NULL;
133
0
            a->flags = BIO_FLAGS_UPLINK;
134
0
        }
135
0
        a->init = 0;
136
0
    }
137
0
    return 1;
138
0
}
139
140
static int file_read(BIO *b, char *out, int outl)
141
0
{
142
0
    int ret = 0;
143
0
144
0
    if (b->init && (out != NULL)) {
145
0
        if (b->flags & BIO_FLAGS_UPLINK)
146
0
            ret = UP_fread(out, 1, (int)outl, b->ptr);
147
0
        else
148
0
            ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
149
0
        if (ret == 0
150
0
            && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
151
0
                                               ferror((FILE *)b->ptr)) {
152
0
            SYSerr(SYS_F_FREAD, get_last_sys_error());
153
0
            BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
154
0
            ret = -1;
155
0
        }
156
0
    }
157
0
    return ret;
158
0
}
159
160
static int file_write(BIO *b, const char *in, int inl)
161
0
{
162
0
    int ret = 0;
163
0
164
0
    if (b->init && (in != NULL)) {
165
0
        if (b->flags & BIO_FLAGS_UPLINK)
166
0
            ret = UP_fwrite(in, (int)inl, 1, b->ptr);
167
0
        else
168
0
            ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
169
0
        if (ret)
170
0
            ret = inl;
171
0
        /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
172
0
        /*
173
0
         * according to Tim Hudson <tjh@openssl.org>, the commented out
174
0
         * version above can cause 'inl' write calls under some stupid stdio
175
0
         * implementations (VMS)
176
0
         */
177
0
    }
178
0
    return ret;
179
0
}
180
181
static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
182
0
{
183
0
    long ret = 1;
184
0
    FILE *fp = (FILE *)b->ptr;
185
0
    FILE **fpp;
186
0
    char p[4];
187
0
    int st;
188
0
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)
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
0
        if (b->flags & BIO_FLAGS_UPLINK)
199
0
            ret = (long)UP_feof(fp);
200
0
        else
201
0
            ret = (long)feof(fp);
202
0
        break;
203
0
    case BIO_C_FILE_TELL:
204
0
    case BIO_CTRL_INFO:
205
0
        if (b->flags & BIO_FLAGS_UPLINK)
206
0
            ret = UP_ftell(b->ptr);
207
0
        else
208
0
            ret = ftell(fp);
209
0
        break;
210
0
    case BIO_C_SET_FILE_PTR:
211
0
        file_free(b);
212
0
        b->shutdown = (int)num & BIO_CLOSE;
213
0
        b->ptr = ptr;
214
0
        b->init = 1;
215
#  if BIO_FLAGS_UPLINK!=0
216
#   if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
217
#    define _IOB_ENTRIES 20
218
#   endif
219
        /* Safety net to catch purely internal BIO_set_fp calls */
220
#   if defined(_MSC_VER) && _MSC_VER>=1900
221
        if (ptr == stdin || ptr == stdout || ptr == stderr)
222
            BIO_clear_flags(b, BIO_FLAGS_UPLINK);
223
#   elif defined(_IOB_ENTRIES)
224
        if ((size_t)ptr >= (size_t)stdin &&
225
            (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
226
            BIO_clear_flags(b, BIO_FLAGS_UPLINK);
227
#   endif
228
#  endif
229
#  ifdef UP_fsetmod
230
        if (b->flags & BIO_FLAGS_UPLINK)
231
            UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
232
        else
233
#  endif
234
        {
235
#  if defined(OPENSSL_SYS_WINDOWS)
236
            int fd = _fileno((FILE *)ptr);
237
            if (num & BIO_FP_TEXT)
238
                _setmode(fd, _O_TEXT);
239
            else
240
                _setmode(fd, _O_BINARY);
241
#  elif defined(OPENSSL_SYS_MSDOS)
242
            int fd = fileno((FILE *)ptr);
243
            /* Set correct text/binary mode */
244
            if (num & BIO_FP_TEXT)
245
                _setmode(fd, _O_TEXT);
246
            /* Dangerous to set stdin/stdout to raw (unless redirected) */
247
            else {
248
                if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
249
                    if (isatty(fd) <= 0)
250
                        _setmode(fd, _O_BINARY);
251
                } else
252
                    _setmode(fd, _O_BINARY);
253
            }
254
#  elif defined(OPENSSL_SYS_WIN32_CYGWIN)
255
            int fd = fileno((FILE *)ptr);
256
            if (num & BIO_FP_TEXT)
257
                setmode(fd, O_TEXT);
258
            else
259
                setmode(fd, O_BINARY);
260
#  endif
261
        }
262
0
        break;
263
0
    case BIO_C_SET_FILENAME:
264
0
        file_free(b);
265
0
        b->shutdown = (int)num & BIO_CLOSE;
266
0
        if (num & BIO_FP_APPEND) {
267
0
            if (num & BIO_FP_READ)
268
0
                OPENSSL_strlcpy(p, "a+", sizeof(p));
269
0
            else
270
0
                OPENSSL_strlcpy(p, "a", sizeof(p));
271
0
        } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
272
0
            OPENSSL_strlcpy(p, "r+", sizeof(p));
273
0
        else if (num & BIO_FP_WRITE)
274
0
            OPENSSL_strlcpy(p, "w", sizeof(p));
275
0
        else if (num & BIO_FP_READ)
276
0
            OPENSSL_strlcpy(p, "r", sizeof(p));
277
0
        else {
278
0
            BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
279
0
            ret = 0;
280
0
            break;
281
0
        }
282
#  if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32_CYGWIN)
283
        if (!(num & BIO_FP_TEXT))
284
            OPENSSL_strlcat(p, "b", sizeof(p));
285
        else
286
            OPENSSL_strlcat(p, "t", sizeof(p));
287
#  endif
288
0
        fp = openssl_fopen(ptr, p);
289
0
        if (fp == NULL) {
290
0
            SYSerr(SYS_F_FOPEN, get_last_sys_error());
291
0
            ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
292
0
            BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
293
0
            ret = 0;
294
0
            break;
295
0
        }
296
0
        b->ptr = fp;
297
0
        b->init = 1;
298
0
        BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
299
0
                                               * UPLINK */
300
0
        break;
301
0
    case BIO_C_GET_FILE_PTR:
302
0
        /* the ptr parameter is actually a FILE ** in this case. */
303
0
        if (ptr != NULL) {
304
0
            fpp = (FILE **)ptr;
305
0
            *fpp = (FILE *)b->ptr;
306
0
        }
307
0
        break;
308
0
    case BIO_CTRL_GET_CLOSE:
309
0
        ret = (long)b->shutdown;
310
0
        break;
311
0
    case BIO_CTRL_SET_CLOSE:
312
0
        b->shutdown = (int)num;
313
0
        break;
314
0
    case BIO_CTRL_FLUSH:
315
0
        st = b->flags & BIO_FLAGS_UPLINK
316
0
                ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
317
0
        if (st == EOF) {
318
0
            SYSerr(SYS_F_FFLUSH, get_last_sys_error());
319
0
            ERR_add_error_data(1, "fflush()");
320
0
            BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
321
0
            ret = 0;
322
0
        }
323
0
        break;
324
0
    case BIO_CTRL_DUP:
325
0
        ret = 1;
326
0
        break;
327
0
328
0
    case BIO_CTRL_WPENDING:
329
0
    case BIO_CTRL_PENDING:
330
0
    case BIO_CTRL_PUSH:
331
0
    case BIO_CTRL_POP:
332
0
    default:
333
0
        ret = 0;
334
0
        break;
335
0
    }
336
0
    return ret;
337
0
}
338
339
static int file_gets(BIO *bp, char *buf, int size)
340
0
{
341
0
    int ret = 0;
342
0
343
0
    buf[0] = '\0';
344
0
    if (bp->flags & BIO_FLAGS_UPLINK) {
345
0
        if (!UP_fgets(buf, size, bp->ptr))
346
0
            goto err;
347
0
    } else {
348
0
        if (!fgets(buf, size, (FILE *)bp->ptr))
349
0
            goto err;
350
0
    }
351
0
    if (buf[0] != '\0')
352
0
        ret = strlen(buf);
353
0
 err:
354
0
    return ret;
355
0
}
356
357
static int file_puts(BIO *bp, const char *str)
358
0
{
359
0
    int n, ret;
360
0
361
0
    n = strlen(str);
362
0
    ret = file_write(bp, str, n);
363
0
    return ret;
364
0
}
365
366
#else
367
368
static int file_write(BIO *b, const char *in, int inl)
369
{
370
    return -1;
371
}
372
static int file_read(BIO *b, char *out, int outl)
373
{
374
    return -1;
375
}
376
static int file_puts(BIO *bp, const char *str)
377
{
378
    return -1;
379
}
380
static int file_gets(BIO *bp, char *buf, int size)
381
{
382
    return 0;
383
}
384
static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
385
{
386
    return 0;
387
}
388
static int file_new(BIO *bi)
389
{
390
    return 0;
391
}
392
static int file_free(BIO *a)
393
{
394
    return 0;
395
}
396
397
static const BIO_METHOD methods_filep = {
398
    BIO_TYPE_FILE,
399
    "FILE pointer",
400
    /* TODO: Convert to new style write function */
401
    bwrite_conv,
402
    file_write,
403
    /* TODO: Convert to new style read function */
404
    bread_conv,
405
    file_read,
406
    file_puts,
407
    file_gets,
408
    file_ctrl,
409
    file_new,
410
    file_free,
411
    NULL,                      /* file_callback_ctrl */
412
};
413
414
const BIO_METHOD *BIO_s_file(void)
415
{
416
    return &methods_filep;
417
}
418
419
BIO *BIO_new_file(const char *filename, const char *mode)
420
{
421
    return NULL;
422
}
423
424
# endif                         /* OPENSSL_NO_STDIO */
425
426
#endif                          /* HEADER_BSS_FILE_C */