Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

351 righe
11 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #if defined(__linux) || defined(__sun) || defined(__hpux)
  57. /* Following definition aliases fopen to fopen64 on above mentioned
  58. * platforms. This makes it possible to open and sequentially access
  59. * files larger than 2GB from 32-bit application. It does not allow to
  60. * traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
  61. * 32-bit platform permits that, not with fseek/ftell. Not to mention
  62. * that breaking 2GB limit for seeking would require surgery to *our*
  63. * API. But sequential access suffices for practical cases when you
  64. * can run into large files, such as fingerprinting, so we can let API
  65. * alone. For reference, the list of 32-bit platforms which allow for
  66. * sequential access of large files without extra "magic" comprise *BSD,
  67. * Darwin, IRIX...
  68. */
  69. #ifndef _FILE_OFFSET_BITS
  70. #define _FILE_OFFSET_BITS 64
  71. #endif
  72. #endif
  73. #include <openssl/bio.h>
  74. #include <errno.h>
  75. #include <stdio.h>
  76. #include <string.h>
  77. #include <openssl/buf.h>
  78. #include <openssl/err.h>
  79. #include <openssl/mem.h>
  80. #define BIO_FP_READ 0x02
  81. #define BIO_FP_WRITE 0x04
  82. #define BIO_FP_APPEND 0x08
  83. static FILE *open_file(const char *filename, const char *mode) {
  84. #if defined(_WIN32) && defined(CP_UTF8)
  85. int sz, len_0 = (int)strlen(filename) + 1;
  86. DWORD flags;
  87. /* Basically there are three cases to cover: a) filename is pure ASCII
  88. * string; b) actual UTF-8 encoded string and c) locale-ized string, i.e. one
  89. * containing 8-bit characters that are meaningful in current system locale.
  90. * If filename is pure ASCII or real UTF-8 encoded string,
  91. * MultiByteToWideChar succeeds and _wfopen works. If filename is locale-ized
  92. * string, chances are that MultiByteToWideChar fails reporting
  93. * ERROR_NO_UNICODE_TRANSLATION, in which case we fall back to fopen... */
  94. if ((sz = MultiByteToWideChar(CP_UTF8, (flags = MB_ERR_INVALID_CHARS),
  95. filename, len_0, NULL, 0)) > 0 ||
  96. (GetLastError() == ERROR_INVALID_FLAGS &&
  97. (sz = MultiByteToWideChar(CP_UTF8, (flags = 0), filename, len_0, NULL,
  98. 0)) > 0)) {
  99. WCHAR wmode[8];
  100. WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
  101. if (MultiByteToWideChar(CP_UTF8, flags, filename, len_0, wfilename, sz) &&
  102. MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1, wmode,
  103. sizeof(wmode) / sizeof(wmode[0])) &&
  104. (file = _wfopen(wfilename, wmode)) == NULL &&
  105. (errno == ENOENT ||
  106. errno == EBADF)) /* UTF-8 decode succeeded, but no file, filename
  107. * could still have been locale-ized... */
  108. return fopen(filename, mode);
  109. } else if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
  110. return fopen(filename, mode);
  111. }
  112. #else
  113. return fopen(filename, mode);
  114. #endif
  115. }
  116. BIO *BIO_new_file(const char *filename, const char *mode) {
  117. BIO *ret;
  118. FILE *file;
  119. file = open_file(filename, mode);
  120. if (file == NULL) {
  121. OPENSSL_PUT_SYSTEM_ERROR(fopen);
  122. ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
  123. if (errno == ENOENT) {
  124. OPENSSL_PUT_ERROR(BIO, BIO_new_file, BIO_R_NO_SUCH_FILE);
  125. } else {
  126. OPENSSL_PUT_ERROR(BIO, BIO_new_file, BIO_R_SYS_LIB);
  127. }
  128. return NULL;
  129. }
  130. ret = BIO_new(BIO_s_file());
  131. if (ret == NULL) {
  132. fclose(file);
  133. return NULL;
  134. }
  135. BIO_set_fp(ret, file, BIO_CLOSE);
  136. return ret;
  137. }
  138. BIO *BIO_new_fp(FILE *stream, int close_flag) {
  139. BIO *ret = BIO_new(BIO_s_file());
  140. if (ret == NULL) {
  141. return NULL;
  142. }
  143. BIO_set_fp(ret, stream, close_flag);
  144. return ret;
  145. }
  146. static int file_new(BIO *bio) { return 1; }
  147. static int file_free(BIO *bio) {
  148. if (bio == NULL) {
  149. return 0;
  150. }
  151. if (!bio->shutdown) {
  152. return 1;
  153. }
  154. if (bio->init && bio->ptr != NULL) {
  155. fclose(bio->ptr);
  156. bio->ptr = NULL;
  157. }
  158. bio->init = 0;
  159. return 1;
  160. }
  161. static int file_read(BIO *b, char *out, int outl) {
  162. int ret = 0;
  163. if (!b->init) {
  164. return 0;
  165. }
  166. ret = fread(out, 1, outl, (FILE *)b->ptr);
  167. if (ret == 0 && ferror((FILE *)b->ptr)) {
  168. OPENSSL_PUT_SYSTEM_ERROR(fread);
  169. OPENSSL_PUT_ERROR(BIO, file_read, ERR_R_SYS_LIB);
  170. ret = -1;
  171. }
  172. return ret;
  173. }
  174. static int file_write(BIO *b, const char *in, int inl) {
  175. int ret = 0;
  176. if (!b->init) {
  177. return 0;
  178. }
  179. ret = fwrite(in, inl, 1, (FILE *)b->ptr);
  180. if (ret > 0) {
  181. ret = inl;
  182. }
  183. return ret;
  184. }
  185. static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
  186. long ret = 1;
  187. FILE *fp = (FILE *)b->ptr;
  188. FILE **fpp;
  189. char p[4];
  190. switch (cmd) {
  191. case BIO_CTRL_RESET:
  192. num = 0;
  193. case BIO_C_FILE_SEEK:
  194. ret = (long)fseek(fp, num, 0);
  195. break;
  196. case BIO_CTRL_EOF:
  197. ret = (long)feof(fp);
  198. break;
  199. case BIO_C_FILE_TELL:
  200. case BIO_CTRL_INFO:
  201. ret = ftell(fp);
  202. break;
  203. case BIO_C_SET_FILE_PTR:
  204. file_free(b);
  205. b->shutdown = (int)num & BIO_CLOSE;
  206. b->ptr = ptr;
  207. b->init = 1;
  208. break;
  209. case BIO_C_SET_FILENAME:
  210. file_free(b);
  211. b->shutdown = (int)num & BIO_CLOSE;
  212. if (num & BIO_FP_APPEND) {
  213. if (num & BIO_FP_READ) {
  214. BUF_strlcpy(p, "a+", sizeof(p));
  215. } else {
  216. BUF_strlcpy(p, "a", sizeof(p));
  217. }
  218. } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
  219. BUF_strlcpy(p, "r+", sizeof(p));
  220. } else if (num & BIO_FP_WRITE) {
  221. BUF_strlcpy(p, "w", sizeof(p));
  222. } else if (num & BIO_FP_READ) {
  223. BUF_strlcpy(p, "r", sizeof(p));
  224. } else {
  225. OPENSSL_PUT_ERROR(BIO, file_ctrl, BIO_R_BAD_FOPEN_MODE);
  226. ret = 0;
  227. break;
  228. }
  229. fp = open_file(ptr, p);
  230. if (fp == NULL) {
  231. OPENSSL_PUT_SYSTEM_ERROR(fopen);
  232. ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
  233. OPENSSL_PUT_ERROR(BIO, file_ctrl, ERR_R_SYS_LIB);
  234. ret = 0;
  235. break;
  236. }
  237. b->ptr = fp;
  238. b->init = 1;
  239. break;
  240. case BIO_C_GET_FILE_PTR:
  241. /* the ptr parameter is actually a FILE ** in this case. */
  242. if (ptr != NULL) {
  243. fpp = (FILE **)ptr;
  244. *fpp = (FILE *)b->ptr;
  245. }
  246. break;
  247. case BIO_CTRL_GET_CLOSE:
  248. ret = (long)b->shutdown;
  249. break;
  250. case BIO_CTRL_SET_CLOSE:
  251. b->shutdown = (int)num;
  252. break;
  253. case BIO_CTRL_FLUSH:
  254. ret = 0 == fflush((FILE *)b->ptr);
  255. break;
  256. case BIO_CTRL_WPENDING:
  257. case BIO_CTRL_PENDING:
  258. default:
  259. ret = 0;
  260. break;
  261. }
  262. return ret;
  263. }
  264. static int file_gets(BIO *bp, char *buf, int size) {
  265. int ret = 0;
  266. if (size == 0) {
  267. return 0;
  268. }
  269. if (!fgets(buf, size, (FILE *)bp->ptr)) {
  270. buf[0] = 0;
  271. goto err;
  272. }
  273. ret = strlen(buf);
  274. err:
  275. return ret;
  276. }
  277. static int file_puts(BIO *bp, const char *str) {
  278. return file_write(bp, str, strlen(str));
  279. }
  280. static const BIO_METHOD methods_filep = {
  281. BIO_TYPE_FILE, "FILE pointer", file_write, file_read, file_puts,
  282. file_gets, file_ctrl, file_new, file_free, NULL, };
  283. const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
  284. int BIO_get_fp(BIO *bio, FILE **out_file) {
  285. return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file);
  286. }
  287. int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
  288. return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file);
  289. }
  290. int BIO_read_filename(BIO *bio, const char *filename) {
  291. return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
  292. (char *)filename);
  293. }
  294. int BIO_write_filename(BIO *bio, const char *filename) {
  295. return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
  296. (char *)filename);
  297. }
  298. int BIO_append_filename(BIO *bio, const char *filename) {
  299. return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
  300. (char *)filename);
  301. }
  302. int BIO_rw_filename(BIO *bio, const char *filename) {
  303. return BIO_ctrl(bio, BIO_C_SET_FILENAME,
  304. BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename);
  305. }