bss_sock.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 1995-2020 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. #include <stdio.h>
  10. #include <errno.h>
  11. #include "bio_local.h"
  12. #include "internal/cryptlib.h"
  13. #include "internal/ktls.h"
  14. #ifndef OPENSSL_NO_SOCK
  15. # include <openssl/bio.h>
  16. # ifdef WATT32
  17. /* Watt-32 uses same names */
  18. # undef sock_write
  19. # undef sock_read
  20. # undef sock_puts
  21. # define sock_write SockWrite
  22. # define sock_read SockRead
  23. # define sock_puts SockPuts
  24. # endif
  25. static int sock_write(BIO *h, const char *buf, int num);
  26. static int sock_read(BIO *h, char *buf, int size);
  27. static int sock_puts(BIO *h, const char *str);
  28. static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  29. static int sock_new(BIO *h);
  30. static int sock_free(BIO *data);
  31. int BIO_sock_should_retry(int s);
  32. static const BIO_METHOD methods_sockp = {
  33. BIO_TYPE_SOCKET,
  34. "socket",
  35. /* TODO: Convert to new style write function */
  36. bwrite_conv,
  37. sock_write,
  38. /* TODO: Convert to new style read function */
  39. bread_conv,
  40. sock_read,
  41. sock_puts,
  42. NULL, /* sock_gets, */
  43. sock_ctrl,
  44. sock_new,
  45. sock_free,
  46. NULL, /* sock_callback_ctrl */
  47. };
  48. const BIO_METHOD *BIO_s_socket(void)
  49. {
  50. return &methods_sockp;
  51. }
  52. BIO *BIO_new_socket(int fd, int close_flag)
  53. {
  54. BIO *ret;
  55. ret = BIO_new(BIO_s_socket());
  56. if (ret == NULL)
  57. return NULL;
  58. BIO_set_fd(ret, fd, close_flag);
  59. # ifndef OPENSSL_NO_KTLS
  60. {
  61. /*
  62. * The new socket is created successfully regardless of ktls_enable.
  63. * ktls_enable doesn't change any functionality of the socket, except
  64. * changing the setsockopt to enable the processing of ktls_start.
  65. * Thus, it is not a problem to call it for non-TLS sockets.
  66. */
  67. ktls_enable(fd);
  68. }
  69. # endif
  70. return ret;
  71. }
  72. static int sock_new(BIO *bi)
  73. {
  74. bi->init = 0;
  75. bi->num = 0;
  76. bi->ptr = NULL;
  77. bi->flags = 0;
  78. return 1;
  79. }
  80. static int sock_free(BIO *a)
  81. {
  82. if (a == NULL)
  83. return 0;
  84. if (a->shutdown) {
  85. if (a->init) {
  86. BIO_closesocket(a->num);
  87. }
  88. a->init = 0;
  89. a->flags = 0;
  90. }
  91. return 1;
  92. }
  93. static int sock_read(BIO *b, char *out, int outl)
  94. {
  95. int ret = 0;
  96. if (out != NULL) {
  97. clear_socket_error();
  98. # ifndef OPENSSL_NO_KTLS
  99. if (BIO_get_ktls_recv(b))
  100. ret = ktls_read_record(b->num, out, outl);
  101. else
  102. # endif
  103. ret = readsocket(b->num, out, outl);
  104. BIO_clear_retry_flags(b);
  105. if (ret <= 0) {
  106. if (BIO_sock_should_retry(ret))
  107. BIO_set_retry_read(b);
  108. else if (ret == 0)
  109. b->flags |= BIO_FLAGS_IN_EOF;
  110. }
  111. }
  112. return ret;
  113. }
  114. static int sock_write(BIO *b, const char *in, int inl)
  115. {
  116. int ret = 0;
  117. clear_socket_error();
  118. # ifndef OPENSSL_NO_KTLS
  119. if (BIO_should_ktls_ctrl_msg_flag(b)) {
  120. unsigned char record_type = (intptr_t)b->ptr;
  121. ret = ktls_send_ctrl_message(b->num, record_type, in, inl);
  122. if (ret >= 0) {
  123. ret = inl;
  124. BIO_clear_ktls_ctrl_msg_flag(b);
  125. }
  126. } else
  127. # endif
  128. ret = writesocket(b->num, in, inl);
  129. BIO_clear_retry_flags(b);
  130. if (ret <= 0) {
  131. if (BIO_sock_should_retry(ret))
  132. BIO_set_retry_write(b);
  133. }
  134. return ret;
  135. }
  136. static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
  137. {
  138. long ret = 1;
  139. int *ip;
  140. # ifndef OPENSSL_NO_KTLS
  141. ktls_crypto_info_t *crypto_info;
  142. # endif
  143. switch (cmd) {
  144. case BIO_C_SET_FD:
  145. sock_free(b);
  146. b->num = *((int *)ptr);
  147. b->shutdown = (int)num;
  148. b->init = 1;
  149. break;
  150. case BIO_C_GET_FD:
  151. if (b->init) {
  152. ip = (int *)ptr;
  153. if (ip != NULL)
  154. *ip = b->num;
  155. ret = b->num;
  156. } else
  157. ret = -1;
  158. break;
  159. case BIO_CTRL_GET_CLOSE:
  160. ret = b->shutdown;
  161. break;
  162. case BIO_CTRL_SET_CLOSE:
  163. b->shutdown = (int)num;
  164. break;
  165. case BIO_CTRL_DUP:
  166. case BIO_CTRL_FLUSH:
  167. ret = 1;
  168. break;
  169. # ifndef OPENSSL_NO_KTLS
  170. case BIO_CTRL_SET_KTLS:
  171. crypto_info = (ktls_crypto_info_t *)ptr;
  172. ret = ktls_start(b->num, crypto_info, num);
  173. if (ret)
  174. BIO_set_ktls_flag(b, num);
  175. break;
  176. case BIO_CTRL_GET_KTLS_SEND:
  177. return BIO_should_ktls_flag(b, 1);
  178. case BIO_CTRL_GET_KTLS_RECV:
  179. return BIO_should_ktls_flag(b, 0);
  180. case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
  181. BIO_set_ktls_ctrl_msg_flag(b);
  182. b->ptr = (void *)num;
  183. ret = 0;
  184. break;
  185. case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
  186. BIO_clear_ktls_ctrl_msg_flag(b);
  187. ret = 0;
  188. break;
  189. # endif
  190. case BIO_CTRL_EOF:
  191. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
  192. break;
  193. default:
  194. ret = 0;
  195. break;
  196. }
  197. return ret;
  198. }
  199. static int sock_puts(BIO *bp, const char *str)
  200. {
  201. int n, ret;
  202. n = strlen(str);
  203. ret = sock_write(bp, str, n);
  204. return ret;
  205. }
  206. int BIO_sock_should_retry(int i)
  207. {
  208. int err;
  209. if ((i == 0) || (i == -1)) {
  210. err = get_last_socket_error();
  211. return BIO_sock_non_fatal_error(err);
  212. }
  213. return 0;
  214. }
  215. int BIO_sock_non_fatal_error(int err)
  216. {
  217. switch (err) {
  218. # if defined(OPENSSL_SYS_WINDOWS)
  219. # if defined(WSAEWOULDBLOCK)
  220. case WSAEWOULDBLOCK:
  221. # endif
  222. # endif
  223. # ifdef EWOULDBLOCK
  224. # ifdef WSAEWOULDBLOCK
  225. # if WSAEWOULDBLOCK != EWOULDBLOCK
  226. case EWOULDBLOCK:
  227. # endif
  228. # else
  229. case EWOULDBLOCK:
  230. # endif
  231. # endif
  232. # if defined(ENOTCONN)
  233. case ENOTCONN:
  234. # endif
  235. # ifdef EINTR
  236. case EINTR:
  237. # endif
  238. # ifdef EAGAIN
  239. # if EWOULDBLOCK != EAGAIN
  240. case EAGAIN:
  241. # endif
  242. # endif
  243. # ifdef EPROTO
  244. case EPROTO:
  245. # endif
  246. # ifdef EINPROGRESS
  247. case EINPROGRESS:
  248. # endif
  249. # ifdef EALREADY
  250. case EALREADY:
  251. # endif
  252. return 1;
  253. default:
  254. break;
  255. }
  256. return 0;
  257. }
  258. #endif /* #ifndef OPENSSL_NO_SOCK */