bss_file.c 12 KB

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