bss_sock.c 6.0 KB

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