bss_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright 1995-2017 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. BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
  71. else
  72. BIOerr(BIO_F_BIO_NEW_FILE, 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. BIOerr(BIO_F_FILE_READ, 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
  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. # elif defined(OPENSSL_SYS_MSDOS)
  221. int fd = fileno((FILE *)ptr);
  222. /* Set correct text/binary mode */
  223. if (num & BIO_FP_TEXT)
  224. _setmode(fd, _O_TEXT);
  225. /* Dangerous to set stdin/stdout to raw (unless redirected) */
  226. else {
  227. if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
  228. if (isatty(fd) <= 0)
  229. _setmode(fd, _O_BINARY);
  230. } else
  231. _setmode(fd, _O_BINARY);
  232. }
  233. # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
  234. int fd = fileno((FILE *)ptr);
  235. if (!(num & BIO_FP_TEXT))
  236. setmode(fd, O_BINARY);
  237. # endif
  238. }
  239. break;
  240. case BIO_C_SET_FILENAME:
  241. file_free(b);
  242. b->shutdown = (int)num & BIO_CLOSE;
  243. if (num & BIO_FP_APPEND) {
  244. if (num & BIO_FP_READ)
  245. OPENSSL_strlcpy(p, "a+", sizeof(p));
  246. else
  247. OPENSSL_strlcpy(p, "a", sizeof(p));
  248. } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
  249. OPENSSL_strlcpy(p, "r+", sizeof(p));
  250. else if (num & BIO_FP_WRITE)
  251. OPENSSL_strlcpy(p, "w", sizeof(p));
  252. else if (num & BIO_FP_READ)
  253. OPENSSL_strlcpy(p, "r", sizeof(p));
  254. else {
  255. BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
  256. ret = 0;
  257. break;
  258. }
  259. # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
  260. if (!(num & BIO_FP_TEXT))
  261. OPENSSL_strlcat(p, "b", sizeof(p));
  262. else
  263. OPENSSL_strlcat(p, "t", sizeof(p));
  264. # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
  265. if (!(num & BIO_FP_TEXT))
  266. OPENSSL_strlcat(p, "b", sizeof(p));
  267. # endif
  268. fp = openssl_fopen(ptr, p);
  269. if (fp == NULL) {
  270. ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
  271. "calling fopen(%s, %s)",
  272. ptr, p);
  273. BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
  274. ret = 0;
  275. break;
  276. }
  277. b->ptr = fp;
  278. b->init = 1;
  279. /* we did fopen -> we disengage UPLINK */
  280. BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
  281. break;
  282. case BIO_C_GET_FILE_PTR:
  283. /* the ptr parameter is actually a FILE ** in this case. */
  284. if (ptr != NULL) {
  285. fpp = (FILE **)ptr;
  286. *fpp = (FILE *)b->ptr;
  287. }
  288. break;
  289. case BIO_CTRL_GET_CLOSE:
  290. ret = (long)b->shutdown;
  291. break;
  292. case BIO_CTRL_SET_CLOSE:
  293. b->shutdown = (int)num;
  294. break;
  295. case BIO_CTRL_FLUSH:
  296. st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
  297. ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
  298. if (st == EOF) {
  299. ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
  300. "calling fflush()");
  301. BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
  302. ret = 0;
  303. }
  304. break;
  305. case BIO_CTRL_DUP:
  306. ret = 1;
  307. break;
  308. case BIO_CTRL_WPENDING:
  309. case BIO_CTRL_PENDING:
  310. case BIO_CTRL_PUSH:
  311. case BIO_CTRL_POP:
  312. default:
  313. ret = 0;
  314. break;
  315. }
  316. return ret;
  317. }
  318. static int file_gets(BIO *bp, char *buf, int size)
  319. {
  320. int ret = 0;
  321. buf[0] = '\0';
  322. if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
  323. if (!UP_fgets(buf, size, bp->ptr))
  324. goto err;
  325. } else {
  326. if (!fgets(buf, size, (FILE *)bp->ptr))
  327. goto err;
  328. }
  329. if (buf[0] != '\0')
  330. ret = strlen(buf);
  331. err:
  332. return ret;
  333. }
  334. static int file_puts(BIO *bp, const char *str)
  335. {
  336. int n, ret;
  337. n = strlen(str);
  338. ret = file_write(bp, str, n);
  339. return ret;
  340. }
  341. #else
  342. static int file_write(BIO *b, const char *in, int inl)
  343. {
  344. return -1;
  345. }
  346. static int file_read(BIO *b, char *out, int outl)
  347. {
  348. return -1;
  349. }
  350. static int file_puts(BIO *bp, const char *str)
  351. {
  352. return -1;
  353. }
  354. static int file_gets(BIO *bp, char *buf, int size)
  355. {
  356. return 0;
  357. }
  358. static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
  359. {
  360. return 0;
  361. }
  362. static int file_new(BIO *bi)
  363. {
  364. return 0;
  365. }
  366. static int file_free(BIO *a)
  367. {
  368. return 0;
  369. }
  370. static const BIO_METHOD methods_filep = {
  371. BIO_TYPE_FILE,
  372. "FILE pointer",
  373. /* TODO: Convert to new style write function */
  374. bwrite_conv,
  375. file_write,
  376. /* TODO: Convert to new style read function */
  377. bread_conv,
  378. file_read,
  379. file_puts,
  380. file_gets,
  381. file_ctrl,
  382. file_new,
  383. file_free,
  384. NULL, /* file_callback_ctrl */
  385. };
  386. const BIO_METHOD *BIO_s_file(void)
  387. {
  388. return &methods_filep;
  389. }
  390. BIO *BIO_new_file(const char *filename, const char *mode)
  391. {
  392. return NULL;
  393. }
  394. #endif /* OPENSSL_NO_STDIO */