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