bss_sock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. size_t crypto_info_len;
  142. # ifdef __FreeBSD__
  143. struct tls_enable *crypto_info;
  144. # else
  145. struct tls_crypto_info_all *crypto_info;
  146. # endif
  147. # endif
  148. switch (cmd) {
  149. case BIO_C_SET_FD:
  150. sock_free(b);
  151. b->num = *((int *)ptr);
  152. b->shutdown = (int)num;
  153. b->init = 1;
  154. break;
  155. case BIO_C_GET_FD:
  156. if (b->init) {
  157. ip = (int *)ptr;
  158. if (ip != NULL)
  159. *ip = b->num;
  160. ret = b->num;
  161. } else
  162. ret = -1;
  163. break;
  164. case BIO_CTRL_GET_CLOSE:
  165. ret = b->shutdown;
  166. break;
  167. case BIO_CTRL_SET_CLOSE:
  168. b->shutdown = (int)num;
  169. break;
  170. case BIO_CTRL_DUP:
  171. case BIO_CTRL_FLUSH:
  172. ret = 1;
  173. break;
  174. # ifndef OPENSSL_NO_KTLS
  175. case BIO_CTRL_SET_KTLS:
  176. # ifdef __FreeBSD__
  177. crypto_info = (struct tls_enable *)ptr;
  178. crypto_info_len = sizeof(*crypto_info);
  179. # else
  180. crypto_info = (struct tls_crypto_info_all *)ptr;
  181. crypto_info_len = crypto_info->tls_crypto_info_len;
  182. # endif
  183. ret = ktls_start(b->num, crypto_info, crypto_info_len, num);
  184. if (ret)
  185. BIO_set_ktls_flag(b, num);
  186. break;
  187. case BIO_CTRL_GET_KTLS_SEND:
  188. return BIO_should_ktls_flag(b, 1);
  189. case BIO_CTRL_GET_KTLS_RECV:
  190. return BIO_should_ktls_flag(b, 0);
  191. case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
  192. BIO_set_ktls_ctrl_msg_flag(b);
  193. b->ptr = (void *)num;
  194. ret = 0;
  195. break;
  196. case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
  197. BIO_clear_ktls_ctrl_msg_flag(b);
  198. ret = 0;
  199. break;
  200. # endif
  201. case BIO_CTRL_EOF:
  202. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
  203. break;
  204. default:
  205. ret = 0;
  206. break;
  207. }
  208. return ret;
  209. }
  210. static int sock_puts(BIO *bp, const char *str)
  211. {
  212. int n, ret;
  213. n = strlen(str);
  214. ret = sock_write(bp, str, n);
  215. return ret;
  216. }
  217. int BIO_sock_should_retry(int i)
  218. {
  219. int err;
  220. if ((i == 0) || (i == -1)) {
  221. err = get_last_socket_error();
  222. return BIO_sock_non_fatal_error(err);
  223. }
  224. return 0;
  225. }
  226. int BIO_sock_non_fatal_error(int err)
  227. {
  228. switch (err) {
  229. # if defined(OPENSSL_SYS_WINDOWS)
  230. # if defined(WSAEWOULDBLOCK)
  231. case WSAEWOULDBLOCK:
  232. # endif
  233. # endif
  234. # ifdef EWOULDBLOCK
  235. # ifdef WSAEWOULDBLOCK
  236. # if WSAEWOULDBLOCK != EWOULDBLOCK
  237. case EWOULDBLOCK:
  238. # endif
  239. # else
  240. case EWOULDBLOCK:
  241. # endif
  242. # endif
  243. # if defined(ENOTCONN)
  244. case ENOTCONN:
  245. # endif
  246. # ifdef EINTR
  247. case EINTR:
  248. # endif
  249. # ifdef EAGAIN
  250. # if EWOULDBLOCK != EAGAIN
  251. case EAGAIN:
  252. # endif
  253. # endif
  254. # ifdef EPROTO
  255. case EPROTO:
  256. # endif
  257. # ifdef EINPROGRESS
  258. case EINPROGRESS:
  259. # endif
  260. # ifdef EALREADY
  261. case EALREADY:
  262. # endif
  263. return 1;
  264. default:
  265. break;
  266. }
  267. return 0;
  268. }
  269. #endif /* #ifndef OPENSSL_NO_SOCK */