bss_file.c 12 KB

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