You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

281 lines
7.2 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. #include <openssl/bio.h>
  57. #if !defined(OPENSSL_TRUSTY)
  58. #include <errno.h>
  59. #include <string.h>
  60. #if !defined(OPENSSL_WINDOWS)
  61. #include <unistd.h>
  62. #else
  63. #include <io.h>
  64. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  65. #include <windows.h>
  66. OPENSSL_MSVC_PRAGMA(warning(pop))
  67. #endif
  68. #include <openssl/buf.h>
  69. #include <openssl/err.h>
  70. #include <openssl/mem.h>
  71. #include "internal.h"
  72. #include "../internal.h"
  73. static int bio_fd_non_fatal_error(int err) {
  74. if (
  75. #ifdef EWOULDBLOCK
  76. err == EWOULDBLOCK ||
  77. #endif
  78. #ifdef WSAEWOULDBLOCK
  79. err == WSAEWOULDBLOCK ||
  80. #endif
  81. #ifdef ENOTCONN
  82. err == ENOTCONN ||
  83. #endif
  84. #ifdef EINTR
  85. err == EINTR ||
  86. #endif
  87. #ifdef EAGAIN
  88. err == EAGAIN ||
  89. #endif
  90. #ifdef EPROTO
  91. err == EPROTO ||
  92. #endif
  93. #ifdef EINPROGRESS
  94. err == EINPROGRESS ||
  95. #endif
  96. #ifdef EALREADY
  97. err == EALREADY ||
  98. #endif
  99. 0) {
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. #if defined(OPENSSL_WINDOWS)
  105. #define BORINGSSL_ERRNO (int)GetLastError()
  106. #define BORINGSSL_CLOSE _close
  107. #define BORINGSSL_LSEEK _lseek
  108. #define BORINGSSL_READ _read
  109. #define BORINGSSL_WRITE _write
  110. #else
  111. #define BORINGSSL_ERRNO errno
  112. #define BORINGSSL_CLOSE close
  113. #define BORINGSSL_LSEEK lseek
  114. #define BORINGSSL_READ read
  115. #define BORINGSSL_WRITE write
  116. #endif
  117. int bio_fd_should_retry(int i) {
  118. if (i == -1) {
  119. return bio_fd_non_fatal_error(BORINGSSL_ERRNO);
  120. }
  121. return 0;
  122. }
  123. BIO *BIO_new_fd(int fd, int close_flag) {
  124. BIO *ret = BIO_new(BIO_s_fd());
  125. if (ret == NULL) {
  126. return NULL;
  127. }
  128. BIO_set_fd(ret, fd, close_flag);
  129. return ret;
  130. }
  131. static int fd_new(BIO *bio) {
  132. // num is used to store the file descriptor.
  133. bio->num = -1;
  134. return 1;
  135. }
  136. static int fd_free(BIO *bio) {
  137. if (bio == NULL) {
  138. return 0;
  139. }
  140. if (bio->shutdown) {
  141. if (bio->init) {
  142. BORINGSSL_CLOSE(bio->num);
  143. }
  144. bio->init = 0;
  145. }
  146. return 1;
  147. }
  148. static int fd_read(BIO *b, char *out, int outl) {
  149. int ret = 0;
  150. ret = BORINGSSL_READ(b->num, out, outl);
  151. BIO_clear_retry_flags(b);
  152. if (ret <= 0) {
  153. if (bio_fd_should_retry(ret)) {
  154. BIO_set_retry_read(b);
  155. }
  156. }
  157. return ret;
  158. }
  159. static int fd_write(BIO *b, const char *in, int inl) {
  160. int ret = BORINGSSL_WRITE(b->num, in, inl);
  161. BIO_clear_retry_flags(b);
  162. if (ret <= 0) {
  163. if (bio_fd_should_retry(ret)) {
  164. BIO_set_retry_write(b);
  165. }
  166. }
  167. return ret;
  168. }
  169. static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
  170. long ret = 1;
  171. int *ip;
  172. switch (cmd) {
  173. case BIO_CTRL_RESET:
  174. num = 0;
  175. OPENSSL_FALLTHROUGH;
  176. case BIO_C_FILE_SEEK:
  177. ret = 0;
  178. if (b->init) {
  179. ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
  180. }
  181. break;
  182. case BIO_C_FILE_TELL:
  183. case BIO_CTRL_INFO:
  184. ret = 0;
  185. if (b->init) {
  186. ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
  187. }
  188. break;
  189. case BIO_C_SET_FD:
  190. fd_free(b);
  191. b->num = *((int *)ptr);
  192. b->shutdown = (int)num;
  193. b->init = 1;
  194. break;
  195. case BIO_C_GET_FD:
  196. if (b->init) {
  197. ip = (int *)ptr;
  198. if (ip != NULL) {
  199. *ip = b->num;
  200. }
  201. return b->num;
  202. } else {
  203. ret = -1;
  204. }
  205. break;
  206. case BIO_CTRL_GET_CLOSE:
  207. ret = b->shutdown;
  208. break;
  209. case BIO_CTRL_SET_CLOSE:
  210. b->shutdown = (int)num;
  211. break;
  212. case BIO_CTRL_PENDING:
  213. case BIO_CTRL_WPENDING:
  214. ret = 0;
  215. break;
  216. case BIO_CTRL_FLUSH:
  217. ret = 1;
  218. break;
  219. default:
  220. ret = 0;
  221. break;
  222. }
  223. return ret;
  224. }
  225. static int fd_gets(BIO *bp, char *buf, int size) {
  226. char *ptr = buf;
  227. char *end = buf + size - 1;
  228. if (size <= 0) {
  229. return 0;
  230. }
  231. while (ptr < end && fd_read(bp, ptr, 1) > 0 && ptr[0] != '\n') {
  232. ptr++;
  233. }
  234. ptr[0] = '\0';
  235. return ptr - buf;
  236. }
  237. static const BIO_METHOD methods_fdp = {
  238. BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */,
  239. fd_gets, fd_ctrl, fd_new, fd_free, NULL /* callback_ctrl */,
  240. };
  241. const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
  242. int BIO_set_fd(BIO *bio, int fd, int close_flag) {
  243. return BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
  244. }
  245. int BIO_get_fd(BIO *bio, int *out_fd) {
  246. return BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
  247. }
  248. #endif // OPENSSL_TRUSTY