bss_file.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* crypto/bio/bss_file.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /*-
  59. * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout
  60. * with binary data (e.g. asn1parse -inform DER < xxx) under
  61. * Windows
  62. */
  63. #ifndef HEADER_BSS_FILE_C
  64. # define HEADER_BSS_FILE_C
  65. # if defined(__linux) || defined(__sun) || defined(__hpux)
  66. /*
  67. * Following definition aliases fopen to fopen64 on above mentioned
  68. * platforms. This makes it possible to open and sequentially access files
  69. * larger than 2GB from 32-bit application. It does not allow to traverse
  70. * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
  71. * platform permits that, not with fseek/ftell. Not to mention that breaking
  72. * 2GB limit for seeking would require surgery to *our* API. But sequential
  73. * access suffices for practical cases when you can run into large files,
  74. * such as fingerprinting, so we can let API alone. For reference, the list
  75. * of 32-bit platforms which allow for sequential access of large files
  76. * without extra "magic" comprise *BSD, Darwin, IRIX...
  77. */
  78. # ifndef _FILE_OFFSET_BITS
  79. # define _FILE_OFFSET_BITS 64
  80. # endif
  81. # endif
  82. # include <stdio.h>
  83. # include <errno.h>
  84. # include "cryptlib.h"
  85. # include "bio_lcl.h"
  86. # include <openssl/err.h>
  87. # if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
  88. # include <nwfileio.h>
  89. # endif
  90. # if !defined(OPENSSL_NO_STDIO)
  91. static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
  92. static int MS_CALLBACK file_read(BIO *h, char *buf, int size);
  93. static int MS_CALLBACK file_puts(BIO *h, const char *str);
  94. static int MS_CALLBACK file_gets(BIO *h, char *str, int size);
  95. static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  96. static int MS_CALLBACK file_new(BIO *h);
  97. static int MS_CALLBACK file_free(BIO *data);
  98. static BIO_METHOD methods_filep = {
  99. BIO_TYPE_FILE,
  100. "FILE pointer",
  101. file_write,
  102. file_read,
  103. file_puts,
  104. file_gets,
  105. file_ctrl,
  106. file_new,
  107. file_free,
  108. NULL,
  109. };
  110. static FILE *file_fopen(const char *filename, const char *mode)
  111. {
  112. FILE *file = NULL;
  113. # if defined(_WIN32) && defined(CP_UTF8)
  114. int sz, len_0 = (int)strlen(filename) + 1;
  115. DWORD flags;
  116. /*
  117. * Basically there are three cases to cover: a) filename is
  118. * pure ASCII string; b) actual UTF-8 encoded string and
  119. * c) locale-ized string, i.e. one containing 8-bit
  120. * characters that are meaningful in current system locale.
  121. * If filename is pure ASCII or real UTF-8 encoded string,
  122. * MultiByteToWideChar succeeds and _wfopen works. If
  123. * filename is locale-ized string, chances are that
  124. * MultiByteToWideChar fails reporting
  125. * ERROR_NO_UNICODE_TRANSLATION, in which case we fall
  126. * back to fopen...
  127. */
  128. if ((sz = MultiByteToWideChar(CP_UTF8, (flags = MB_ERR_INVALID_CHARS),
  129. filename, len_0, NULL, 0)) > 0 ||
  130. (GetLastError() == ERROR_INVALID_FLAGS &&
  131. (sz = MultiByteToWideChar(CP_UTF8, (flags = 0),
  132. filename, len_0, NULL, 0)) > 0)
  133. ) {
  134. WCHAR wmode[8];
  135. WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
  136. if (MultiByteToWideChar(CP_UTF8, flags,
  137. filename, len_0, wfilename, sz) &&
  138. MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1,
  139. wmode, sizeof(wmode) / sizeof(wmode[0])) &&
  140. (file = _wfopen(wfilename, wmode)) == NULL &&
  141. (errno == ENOENT || errno == EBADF)
  142. ) {
  143. /*
  144. * UTF-8 decode succeeded, but no file, filename
  145. * could still have been locale-ized...
  146. */
  147. file = fopen(filename, mode);
  148. }
  149. } else if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
  150. file = fopen(filename, mode);
  151. }
  152. # else
  153. file = fopen(filename, mode);
  154. # endif
  155. return (file);
  156. }
  157. BIO *BIO_new_file(const char *filename, const char *mode)
  158. {
  159. BIO *ret;
  160. FILE *file = file_fopen(filename, mode);
  161. if (file == NULL) {
  162. SYSerr(SYS_F_FOPEN, get_last_sys_error());
  163. ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
  164. if (errno == ENOENT
  165. # ifdef ENXIO
  166. || errno == ENXIO
  167. # endif
  168. )
  169. BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
  170. else
  171. BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
  172. return (NULL);
  173. }
  174. if ((ret = BIO_new(BIO_s_file())) == NULL) {
  175. fclose(file);
  176. return (NULL);
  177. }
  178. BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
  179. * UPLINK */
  180. BIO_set_fp(ret, file, BIO_CLOSE);
  181. return (ret);
  182. }
  183. BIO *BIO_new_fp(FILE *stream, int close_flag)
  184. {
  185. BIO *ret;
  186. if ((ret = BIO_new(BIO_s_file())) == NULL)
  187. return (NULL);
  188. BIO_set_flags(ret, BIO_FLAGS_UPLINK); /* redundant, left for
  189. * documentation puposes */
  190. BIO_set_fp(ret, stream, close_flag);
  191. return (ret);
  192. }
  193. BIO_METHOD *BIO_s_file(void)
  194. {
  195. return (&methods_filep);
  196. }
  197. static int MS_CALLBACK file_new(BIO *bi)
  198. {
  199. bi->init = 0;
  200. bi->num = 0;
  201. bi->ptr = NULL;
  202. bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
  203. return (1);
  204. }
  205. static int MS_CALLBACK file_free(BIO *a)
  206. {
  207. if (a == NULL)
  208. return (0);
  209. if (a->shutdown) {
  210. if ((a->init) && (a->ptr != NULL)) {
  211. if (a->flags & BIO_FLAGS_UPLINK)
  212. UP_fclose(a->ptr);
  213. else
  214. fclose(a->ptr);
  215. a->ptr = NULL;
  216. a->flags = BIO_FLAGS_UPLINK;
  217. }
  218. a->init = 0;
  219. }
  220. return (1);
  221. }
  222. static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
  223. {
  224. int ret = 0;
  225. if (b->init && (out != NULL)) {
  226. if (b->flags & BIO_FLAGS_UPLINK)
  227. ret = UP_fread(out, 1, (int)outl, b->ptr);
  228. else
  229. ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
  230. if (ret == 0
  231. && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
  232. ferror((FILE *)b->ptr)) {
  233. SYSerr(SYS_F_FREAD, get_last_sys_error());
  234. BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
  235. ret = -1;
  236. }
  237. }
  238. return (ret);
  239. }
  240. static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
  241. {
  242. int ret = 0;
  243. if (b->init && (in != NULL)) {
  244. if (b->flags & BIO_FLAGS_UPLINK)
  245. ret = UP_fwrite(in, (int)inl, 1, b->ptr);
  246. else
  247. ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
  248. if (ret)
  249. ret = inl;
  250. /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
  251. /*
  252. * according to Tim Hudson <tjh@cryptsoft.com>, the commented out
  253. * version above can cause 'inl' write calls under some stupid stdio
  254. * implementations (VMS)
  255. */
  256. }
  257. return (ret);
  258. }
  259. static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
  260. {
  261. long ret = 1;
  262. FILE *fp = (FILE *)b->ptr;
  263. FILE **fpp;
  264. char p[4];
  265. int st;
  266. switch (cmd) {
  267. case BIO_C_FILE_SEEK:
  268. case BIO_CTRL_RESET:
  269. if (b->flags & BIO_FLAGS_UPLINK)
  270. ret = (long)UP_fseek(b->ptr, num, 0);
  271. else
  272. ret = (long)fseek(fp, num, 0);
  273. break;
  274. case BIO_CTRL_EOF:
  275. if (b->flags & BIO_FLAGS_UPLINK)
  276. ret = (long)UP_feof(fp);
  277. else
  278. ret = (long)feof(fp);
  279. break;
  280. case BIO_C_FILE_TELL:
  281. case BIO_CTRL_INFO:
  282. if (b->flags & BIO_FLAGS_UPLINK)
  283. ret = UP_ftell(b->ptr);
  284. else
  285. ret = ftell(fp);
  286. break;
  287. case BIO_C_SET_FILE_PTR:
  288. file_free(b);
  289. b->shutdown = (int)num & BIO_CLOSE;
  290. b->ptr = ptr;
  291. b->init = 1;
  292. # if BIO_FLAGS_UPLINK!=0
  293. # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
  294. # define _IOB_ENTRIES 20
  295. # endif
  296. /* Safety net to catch purely internal BIO_set_fp calls */
  297. # if defined(_MSC_VER) && _MSC_VER>=1900
  298. if (ptr == stdin || ptr == stdout || ptr == stderr)
  299. BIO_clear_flags(b, BIO_FLAGS_UPLINK);
  300. # elif defined(_IOB_ENTRIES)
  301. if ((size_t)ptr >= (size_t)stdin &&
  302. (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
  303. BIO_clear_flags(b, BIO_FLAGS_UPLINK);
  304. # endif
  305. # endif
  306. # ifdef UP_fsetmod
  307. if (b->flags & BIO_FLAGS_UPLINK)
  308. UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
  309. else
  310. # endif
  311. {
  312. # if defined(OPENSSL_SYS_WINDOWS)
  313. int fd = _fileno((FILE *)ptr);
  314. if (num & BIO_FP_TEXT)
  315. _setmode(fd, _O_TEXT);
  316. else
  317. _setmode(fd, _O_BINARY);
  318. # elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
  319. int fd = fileno((FILE *)ptr);
  320. /* Under CLib there are differences in file modes */
  321. if (num & BIO_FP_TEXT)
  322. setmode(fd, O_TEXT);
  323. else
  324. setmode(fd, O_BINARY);
  325. # elif defined(OPENSSL_SYS_MSDOS)
  326. int fd = fileno((FILE *)ptr);
  327. /* Set correct text/binary mode */
  328. if (num & BIO_FP_TEXT)
  329. _setmode(fd, _O_TEXT);
  330. /* Dangerous to set stdin/stdout to raw (unless redirected) */
  331. else {
  332. if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
  333. if (isatty(fd) <= 0)
  334. _setmode(fd, _O_BINARY);
  335. } else
  336. _setmode(fd, _O_BINARY);
  337. }
  338. # elif defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
  339. int fd = fileno((FILE *)ptr);
  340. if (num & BIO_FP_TEXT)
  341. setmode(fd, O_TEXT);
  342. else
  343. setmode(fd, O_BINARY);
  344. # endif
  345. }
  346. break;
  347. case BIO_C_SET_FILENAME:
  348. file_free(b);
  349. b->shutdown = (int)num & BIO_CLOSE;
  350. if (num & BIO_FP_APPEND) {
  351. if (num & BIO_FP_READ)
  352. BUF_strlcpy(p, "a+", sizeof p);
  353. else
  354. BUF_strlcpy(p, "a", sizeof p);
  355. } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
  356. BUF_strlcpy(p, "r+", sizeof p);
  357. else if (num & BIO_FP_WRITE)
  358. BUF_strlcpy(p, "w", sizeof p);
  359. else if (num & BIO_FP_READ)
  360. BUF_strlcpy(p, "r", sizeof p);
  361. else {
  362. BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
  363. ret = 0;
  364. break;
  365. }
  366. # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
  367. if (!(num & BIO_FP_TEXT))
  368. strcat(p, "b");
  369. else
  370. strcat(p, "t");
  371. # endif
  372. # if defined(OPENSSL_SYS_NETWARE)
  373. if (!(num & BIO_FP_TEXT))
  374. strcat(p, "b");
  375. else
  376. strcat(p, "t");
  377. # endif
  378. fp = file_fopen(ptr, p);
  379. if (fp == NULL) {
  380. SYSerr(SYS_F_FOPEN, get_last_sys_error());
  381. ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
  382. BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
  383. ret = 0;
  384. break;
  385. }
  386. b->ptr = fp;
  387. b->init = 1;
  388. BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
  389. * UPLINK */
  390. break;
  391. case BIO_C_GET_FILE_PTR:
  392. /* the ptr parameter is actually a FILE ** in this case. */
  393. if (ptr != NULL) {
  394. fpp = (FILE **)ptr;
  395. *fpp = (FILE *)b->ptr;
  396. }
  397. break;
  398. case BIO_CTRL_GET_CLOSE:
  399. ret = (long)b->shutdown;
  400. break;
  401. case BIO_CTRL_SET_CLOSE:
  402. b->shutdown = (int)num;
  403. break;
  404. case BIO_CTRL_FLUSH:
  405. st = b->flags & BIO_FLAGS_UPLINK
  406. ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
  407. if (st == EOF) {
  408. SYSerr(SYS_F_FFLUSH, get_last_sys_error());
  409. ERR_add_error_data(1, "fflush()");
  410. BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
  411. ret = 0;
  412. }
  413. break;
  414. case BIO_CTRL_DUP:
  415. ret = 1;
  416. break;
  417. case BIO_CTRL_WPENDING:
  418. case BIO_CTRL_PENDING:
  419. case BIO_CTRL_PUSH:
  420. case BIO_CTRL_POP:
  421. default:
  422. ret = 0;
  423. break;
  424. }
  425. return (ret);
  426. }
  427. static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
  428. {
  429. int ret = 0;
  430. buf[0] = '\0';
  431. if (bp->flags & BIO_FLAGS_UPLINK) {
  432. if (!UP_fgets(buf, size, bp->ptr))
  433. goto err;
  434. } else {
  435. if (!fgets(buf, size, (FILE *)bp->ptr))
  436. goto err;
  437. }
  438. if (buf[0] != '\0')
  439. ret = strlen(buf);
  440. err:
  441. return (ret);
  442. }
  443. static int MS_CALLBACK file_puts(BIO *bp, const char *str)
  444. {
  445. int n, ret;
  446. n = strlen(str);
  447. ret = file_write(bp, str, n);
  448. return (ret);
  449. }
  450. # endif /* OPENSSL_NO_STDIO */
  451. #endif /* HEADER_BSS_FILE_C */