2
0

bss_sock.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright 1995-2022 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/bio_tfo.h"
  13. #include "internal/cryptlib.h"
  14. #include "internal/ktls.h"
  15. #ifndef OPENSSL_NO_SOCK
  16. # include <openssl/bio.h>
  17. # ifdef WATT32
  18. /* Watt-32 uses same names */
  19. # undef sock_write
  20. # undef sock_read
  21. # undef sock_puts
  22. # define sock_write SockWrite
  23. # define sock_read SockRead
  24. # define sock_puts SockPuts
  25. # endif
  26. struct bss_sock_st {
  27. BIO_ADDR tfo_peer;
  28. int tfo_first;
  29. #ifndef OPENSSL_NO_KTLS
  30. unsigned char ktls_record_type;
  31. #endif
  32. };
  33. static int sock_write(BIO *h, const char *buf, int num);
  34. static int sock_read(BIO *h, char *buf, int size);
  35. static int sock_puts(BIO *h, const char *str);
  36. static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  37. static int sock_new(BIO *h);
  38. static int sock_free(BIO *data);
  39. int BIO_sock_should_retry(int s);
  40. static const BIO_METHOD methods_sockp = {
  41. BIO_TYPE_SOCKET,
  42. "socket",
  43. bwrite_conv,
  44. sock_write,
  45. bread_conv,
  46. sock_read,
  47. sock_puts,
  48. NULL, /* sock_gets, */
  49. sock_ctrl,
  50. sock_new,
  51. sock_free,
  52. NULL, /* sock_callback_ctrl */
  53. };
  54. const BIO_METHOD *BIO_s_socket(void)
  55. {
  56. return &methods_sockp;
  57. }
  58. BIO *BIO_new_socket(int fd, int close_flag)
  59. {
  60. BIO *ret;
  61. ret = BIO_new(BIO_s_socket());
  62. if (ret == NULL)
  63. return NULL;
  64. BIO_set_fd(ret, fd, close_flag);
  65. # ifndef OPENSSL_NO_KTLS
  66. {
  67. /*
  68. * The new socket is created successfully regardless of ktls_enable.
  69. * ktls_enable doesn't change any functionality of the socket, except
  70. * changing the setsockopt to enable the processing of ktls_start.
  71. * Thus, it is not a problem to call it for non-TLS sockets.
  72. */
  73. ktls_enable(fd);
  74. }
  75. # endif
  76. return ret;
  77. }
  78. static int sock_new(BIO *bi)
  79. {
  80. bi->init = 0;
  81. bi->num = 0;
  82. bi->flags = 0;
  83. bi->ptr = OPENSSL_zalloc(sizeof(struct bss_sock_st));
  84. if (bi->ptr == NULL)
  85. return 0;
  86. return 1;
  87. }
  88. static int sock_free(BIO *a)
  89. {
  90. if (a == NULL)
  91. return 0;
  92. if (a->shutdown) {
  93. if (a->init) {
  94. BIO_closesocket(a->num);
  95. }
  96. a->init = 0;
  97. a->flags = 0;
  98. }
  99. OPENSSL_free(a->ptr);
  100. a->ptr = NULL;
  101. return 1;
  102. }
  103. static int sock_read(BIO *b, char *out, int outl)
  104. {
  105. int ret = 0;
  106. if (out != NULL) {
  107. clear_socket_error();
  108. # ifndef OPENSSL_NO_KTLS
  109. if (BIO_get_ktls_recv(b))
  110. ret = ktls_read_record(b->num, out, outl);
  111. else
  112. # endif
  113. ret = readsocket(b->num, out, outl);
  114. BIO_clear_retry_flags(b);
  115. if (ret <= 0) {
  116. if (BIO_sock_should_retry(ret))
  117. BIO_set_retry_read(b);
  118. else if (ret == 0)
  119. b->flags |= BIO_FLAGS_IN_EOF;
  120. }
  121. }
  122. return ret;
  123. }
  124. static int sock_write(BIO *b, const char *in, int inl)
  125. {
  126. int ret = 0;
  127. # if !defined(OPENSSL_NO_KTLS) || defined(OSSL_TFO_SENDTO)
  128. struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
  129. # endif
  130. clear_socket_error();
  131. # ifndef OPENSSL_NO_KTLS
  132. if (BIO_should_ktls_ctrl_msg_flag(b)) {
  133. unsigned char record_type = data->ktls_record_type;
  134. ret = ktls_send_ctrl_message(b->num, record_type, in, inl);
  135. if (ret >= 0) {
  136. ret = inl;
  137. BIO_clear_ktls_ctrl_msg_flag(b);
  138. }
  139. } else
  140. # endif
  141. # if defined(OSSL_TFO_SENDTO)
  142. if (data->tfo_first) {
  143. struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
  144. socklen_t peerlen = BIO_ADDR_sockaddr_size(&data->tfo_peer);
  145. ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
  146. BIO_ADDR_sockaddr(&data->tfo_peer), peerlen);
  147. data->tfo_first = 0;
  148. } else
  149. # endif
  150. ret = writesocket(b->num, in, inl);
  151. BIO_clear_retry_flags(b);
  152. if (ret <= 0) {
  153. if (BIO_sock_should_retry(ret))
  154. BIO_set_retry_write(b);
  155. }
  156. return ret;
  157. }
  158. static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
  159. {
  160. long ret = 1;
  161. int *ip;
  162. struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
  163. # ifndef OPENSSL_NO_KTLS
  164. ktls_crypto_info_t *crypto_info;
  165. # endif
  166. switch (cmd) {
  167. case BIO_C_SET_FD:
  168. /* minimal sock_free() */
  169. if (b->shutdown) {
  170. if (b->init)
  171. BIO_closesocket(b->num);
  172. b->flags = 0;
  173. }
  174. b->num = *((int *)ptr);
  175. b->shutdown = (int)num;
  176. b->init = 1;
  177. data->tfo_first = 0;
  178. memset(&data->tfo_peer, 0, sizeof(data->tfo_peer));
  179. break;
  180. case BIO_C_GET_FD:
  181. if (b->init) {
  182. ip = (int *)ptr;
  183. if (ip != NULL)
  184. *ip = b->num;
  185. ret = b->num;
  186. } else
  187. ret = -1;
  188. break;
  189. case BIO_CTRL_GET_CLOSE:
  190. ret = b->shutdown;
  191. break;
  192. case BIO_CTRL_SET_CLOSE:
  193. b->shutdown = (int)num;
  194. break;
  195. case BIO_CTRL_DUP:
  196. case BIO_CTRL_FLUSH:
  197. ret = 1;
  198. break;
  199. case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
  200. case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
  201. {
  202. BIO_POLL_DESCRIPTOR *pd = ptr;
  203. if (!b->init) {
  204. ret = 0;
  205. break;
  206. }
  207. pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
  208. pd->value.fd = b->num;
  209. }
  210. break;
  211. # ifndef OPENSSL_NO_KTLS
  212. case BIO_CTRL_SET_KTLS:
  213. crypto_info = (ktls_crypto_info_t *)ptr;
  214. ret = ktls_start(b->num, crypto_info, num);
  215. if (ret)
  216. BIO_set_ktls_flag(b, num);
  217. break;
  218. case BIO_CTRL_GET_KTLS_SEND:
  219. return BIO_should_ktls_flag(b, 1) != 0;
  220. case BIO_CTRL_GET_KTLS_RECV:
  221. return BIO_should_ktls_flag(b, 0) != 0;
  222. case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
  223. BIO_set_ktls_ctrl_msg_flag(b);
  224. data->ktls_record_type = (unsigned char)num;
  225. ret = 0;
  226. break;
  227. case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
  228. BIO_clear_ktls_ctrl_msg_flag(b);
  229. ret = 0;
  230. break;
  231. case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
  232. ret = ktls_enable_tx_zerocopy_sendfile(b->num);
  233. if (ret)
  234. BIO_set_ktls_zerocopy_sendfile_flag(b);
  235. break;
  236. # endif
  237. case BIO_CTRL_EOF:
  238. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
  239. break;
  240. case BIO_C_GET_CONNECT:
  241. if (ptr != NULL && num == 2) {
  242. const char **pptr = (const char **)ptr;
  243. *pptr = (const char *)&data->tfo_peer;
  244. } else {
  245. ret = 0;
  246. }
  247. break;
  248. case BIO_C_SET_CONNECT:
  249. if (ptr != NULL && num == 2) {
  250. ret = BIO_ADDR_make(&data->tfo_peer,
  251. BIO_ADDR_sockaddr((const BIO_ADDR *)ptr));
  252. if (ret)
  253. data->tfo_first = 1;
  254. } else {
  255. ret = 0;
  256. }
  257. break;
  258. default:
  259. ret = 0;
  260. break;
  261. }
  262. return ret;
  263. }
  264. static int sock_puts(BIO *bp, const char *str)
  265. {
  266. int n, ret;
  267. n = strlen(str);
  268. ret = sock_write(bp, str, n);
  269. return ret;
  270. }
  271. int BIO_sock_should_retry(int i)
  272. {
  273. int err;
  274. if ((i == 0) || (i == -1)) {
  275. err = get_last_socket_error();
  276. return BIO_sock_non_fatal_error(err);
  277. }
  278. return 0;
  279. }
  280. int BIO_sock_non_fatal_error(int err)
  281. {
  282. switch (err) {
  283. # if defined(OPENSSL_SYS_WINDOWS)
  284. # if defined(WSAEWOULDBLOCK)
  285. case WSAEWOULDBLOCK:
  286. # endif
  287. # endif
  288. # ifdef EWOULDBLOCK
  289. # ifdef WSAEWOULDBLOCK
  290. # if WSAEWOULDBLOCK != EWOULDBLOCK
  291. case EWOULDBLOCK:
  292. # endif
  293. # else
  294. case EWOULDBLOCK:
  295. # endif
  296. # endif
  297. # if defined(ENOTCONN)
  298. case ENOTCONN:
  299. # endif
  300. # ifdef EINTR
  301. case EINTR:
  302. # endif
  303. # ifdef EAGAIN
  304. # if EWOULDBLOCK != EAGAIN
  305. case EAGAIN:
  306. # endif
  307. # endif
  308. # ifdef EPROTO
  309. case EPROTO:
  310. # endif
  311. # ifdef EINPROGRESS
  312. case EINPROGRESS:
  313. # endif
  314. # ifdef EALREADY
  315. case EALREADY:
  316. # endif
  317. return 1;
  318. default:
  319. break;
  320. }
  321. return 0;
  322. }
  323. #endif /* #ifndef OPENSSL_NO_SOCK */