bss_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
  166. else
  167. BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
  168. return (NULL);
  169. }
  170. if ((ret = BIO_new(BIO_s_file())) == NULL) {
  171. fclose(file);
  172. return (NULL);
  173. }
  174. BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
  175. * UPLINK */
  176. BIO_set_fp(ret, file, BIO_CLOSE);
  177. return (ret);
  178. }
  179. BIO *BIO_new_fp(FILE *stream, int close_flag)
  180. {
  181. BIO *ret;
  182. if ((ret = BIO_new(BIO_s_file())) == NULL)
  183. return (NULL);
  184. BIO_set_flags(ret, BIO_FLAGS_UPLINK); /* redundant, left for
  185. * documentation puposes */
  186. BIO_set_fp(ret, stream, close_flag);
  187. return (ret);
  188. }
  189. BIO_METHOD *BIO_s_file(void)
  190. {
  191. return (&methods_filep);
  192. }
  193. static int MS_CALLBACK file_new(BIO *bi)
  194. {
  195. bi->init = 0;
  196. bi->num = 0;
  197. bi->ptr = NULL;
  198. bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */
  199. return (1);
  200. }
  201. static int MS_CALLBACK file_free(BIO *a)
  202. {
  203. if (a == NULL)
  204. return (0);
  205. if (a->shutdown) {
  206. if ((a->init) && (a->ptr != NULL)) {
  207. if (a->flags & BIO_FLAGS_UPLINK)
  208. UP_fclose(a->ptr);
  209. else
  210. fclose(a->ptr);
  211. a->ptr = NULL;
  212. a->flags = BIO_FLAGS_UPLINK;
  213. }
  214. a->init = 0;
  215. }
  216. return (1);
  217. }
  218. static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
  219. {
  220. int ret = 0;
  221. if (b->init && (out != NULL)) {
  222. if (b->flags & BIO_FLAGS_UPLINK)
  223. ret = UP_fread(out, 1, (int)outl, b->ptr);
  224. else
  225. ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
  226. if (ret == 0
  227. && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) :
  228. ferror((FILE *)b->ptr)) {
  229. SYSerr(SYS_F_FREAD, get_last_sys_error());
  230. BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
  231. ret = -1;
  232. }
  233. }
  234. return (ret);
  235. }
  236. static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
  237. {
  238. int ret = 0;
  239. if (b->init && (in != NULL)) {
  240. if (b->flags & BIO_FLAGS_UPLINK)
  241. ret = UP_fwrite(in, (int)inl, 1, b->ptr);
  242. else
  243. ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
  244. if (ret)
  245. ret = inl;
  246. /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
  247. /*
  248. * according to Tim Hudson <tjh@cryptsoft.com>, the commented out
  249. * version above can cause 'inl' write calls under some stupid stdio
  250. * implementations (VMS)
  251. */
  252. }
  253. return (ret);
  254. }
  255. static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
  256. {
  257. long ret = 1;
  258. FILE *fp = (FILE *)b->ptr;
  259. FILE **fpp;
  260. char p[4];
  261. switch (cmd) {
  262. case BIO_C_FILE_SEEK:
  263. case BIO_CTRL_RESET:
  264. if (b->flags & BIO_FLAGS_UPLINK)
  265. ret = (long)UP_fseek(b->ptr, num, 0);
  266. else
  267. ret = (long)fseek(fp, num, 0);
  268. break;
  269. case BIO_CTRL_EOF:
  270. if (b->flags & BIO_FLAGS_UPLINK)
  271. ret = (long)UP_feof(fp);
  272. else
  273. ret = (long)feof(fp);
  274. break;
  275. case BIO_C_FILE_TELL:
  276. case BIO_CTRL_INFO:
  277. if (b->flags & BIO_FLAGS_UPLINK)
  278. ret = UP_ftell(b->ptr);
  279. else
  280. ret = ftell(fp);
  281. break;
  282. case BIO_C_SET_FILE_PTR:
  283. file_free(b);
  284. b->shutdown = (int)num & BIO_CLOSE;
  285. b->ptr = ptr;
  286. b->init = 1;
  287. # if BIO_FLAGS_UPLINK!=0
  288. # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
  289. # define _IOB_ENTRIES 20
  290. # endif
  291. # if defined(_IOB_ENTRIES)
  292. /* Safety net to catch purely internal BIO_set_fp calls */
  293. if ((size_t)ptr >= (size_t)stdin &&
  294. (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
  295. BIO_clear_flags(b, BIO_FLAGS_UPLINK);
  296. # endif
  297. # endif
  298. # ifdef UP_fsetmod
  299. if (b->flags & BIO_FLAGS_UPLINK)
  300. UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
  301. else
  302. # endif
  303. {
  304. # if defined(OPENSSL_SYS_WINDOWS)
  305. int fd = _fileno((FILE *)ptr);
  306. if (num & BIO_FP_TEXT)
  307. _setmode(fd, _O_TEXT);
  308. else
  309. _setmode(fd, _O_BINARY);
  310. # elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
  311. int fd = fileno((FILE *)ptr);
  312. /* Under CLib there are differences in file modes */
  313. if (num & BIO_FP_TEXT)
  314. setmode(fd, O_TEXT);
  315. else
  316. setmode(fd, O_BINARY);
  317. # elif defined(OPENSSL_SYS_MSDOS)
  318. int fd = fileno((FILE *)ptr);
  319. /* Set correct text/binary mode */
  320. if (num & BIO_FP_TEXT)
  321. _setmode(fd, _O_TEXT);
  322. /* Dangerous to set stdin/stdout to raw (unless redirected) */
  323. else {
  324. if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
  325. if (isatty(fd) <= 0)
  326. _setmode(fd, _O_BINARY);
  327. } else
  328. _setmode(fd, _O_BINARY);
  329. }
  330. # elif defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
  331. int fd = fileno((FILE *)ptr);
  332. if (num & BIO_FP_TEXT)
  333. setmode(fd, O_TEXT);
  334. else
  335. setmode(fd, O_BINARY);
  336. # endif
  337. }
  338. break;
  339. case BIO_C_SET_FILENAME:
  340. file_free(b);
  341. b->shutdown = (int)num & BIO_CLOSE;
  342. if (num & BIO_FP_APPEND) {
  343. if (num & BIO_FP_READ)
  344. BUF_strlcpy(p, "a+", sizeof p);
  345. else
  346. BUF_strlcpy(p, "a", sizeof p);
  347. } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
  348. BUF_strlcpy(p, "r+", sizeof p);
  349. else if (num & BIO_FP_WRITE)
  350. BUF_strlcpy(p, "w", sizeof p);
  351. else if (num & BIO_FP_READ)
  352. BUF_strlcpy(p, "r", sizeof p);
  353. else {
  354. BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
  355. ret = 0;
  356. break;
  357. }
  358. # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
  359. if (!(num & BIO_FP_TEXT))
  360. strcat(p, "b");
  361. else
  362. strcat(p, "t");
  363. # endif
  364. # if defined(OPENSSL_SYS_NETWARE)
  365. if (!(num & BIO_FP_TEXT))
  366. strcat(p, "b");
  367. else
  368. strcat(p, "t");
  369. # endif
  370. fp = file_fopen(ptr, p);
  371. if (fp == NULL) {
  372. SYSerr(SYS_F_FOPEN, get_last_sys_error());
  373. ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
  374. BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
  375. ret = 0;
  376. break;
  377. }
  378. b->ptr = fp;
  379. b->init = 1;
  380. BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage
  381. * UPLINK */
  382. break;
  383. case BIO_C_GET_FILE_PTR:
  384. /* the ptr parameter is actually a FILE ** in this case. */
  385. if (ptr != NULL) {
  386. fpp = (FILE **)ptr;
  387. *fpp = (FILE *)b->ptr;
  388. }
  389. break;
  390. case BIO_CTRL_GET_CLOSE:
  391. ret = (long)b->shutdown;
  392. break;
  393. case BIO_CTRL_SET_CLOSE:
  394. b->shutdown = (int)num;
  395. break;
  396. case BIO_CTRL_FLUSH:
  397. if (b->flags & BIO_FLAGS_UPLINK)
  398. UP_fflush(b->ptr);
  399. else
  400. fflush((FILE *)b->ptr);
  401. break;
  402. case BIO_CTRL_DUP:
  403. ret = 1;
  404. break;
  405. case BIO_CTRL_WPENDING:
  406. case BIO_CTRL_PENDING:
  407. case BIO_CTRL_PUSH:
  408. case BIO_CTRL_POP:
  409. default:
  410. ret = 0;
  411. break;
  412. }
  413. return (ret);
  414. }
  415. static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
  416. {
  417. int ret = 0;
  418. buf[0] = '\0';
  419. if (bp->flags & BIO_FLAGS_UPLINK) {
  420. if (!UP_fgets(buf, size, bp->ptr))
  421. goto err;
  422. } else {
  423. if (!fgets(buf, size, (FILE *)bp->ptr))
  424. goto err;
  425. }
  426. if (buf[0] != '\0')
  427. ret = strlen(buf);
  428. err:
  429. return (ret);
  430. }
  431. static int MS_CALLBACK file_puts(BIO *bp, const char *str)
  432. {
  433. int n, ret;
  434. n = strlen(str);
  435. ret = file_write(bp, str, n);
  436. return (ret);
  437. }
  438. # endif /* OPENSSL_NO_STDIO */
  439. #endif /* HEADER_BSS_FILE_C */