wolfio.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* io.h
  2. *
  3. * Copyright (C) 2006-2017 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfio.h
  23. */
  24. #ifndef WOLFSSL_IO_H
  25. #define WOLFSSL_IO_H
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* OCSP and CRL_IO require HTTP client */
  30. #if defined(HAVE_OCSP) || defined(HAVE_CRL_IO)
  31. #ifndef HAVE_HTTP_CLIENT
  32. #define HAVE_HTTP_CLIENT
  33. #endif
  34. #endif
  35. #if !defined(WOLFSSL_USER_IO)
  36. /* Micrium uses NetSock I/O callbacks in wolfio.c */
  37. #if !defined(USE_WOLFSSL_IO) && !defined(MICRIUM)
  38. #define USE_WOLFSSL_IO
  39. #endif
  40. #endif
  41. #if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT)
  42. #ifdef HAVE_LIBZ
  43. #include "zlib.h"
  44. #endif
  45. #ifndef USE_WINDOWS_API
  46. #ifdef WOLFSSL_LWIP
  47. /* lwIP needs to be configured to use sockets API in this mode */
  48. /* LWIP_SOCKET 1 in lwip/opt.h or in build */
  49. #include "lwip/sockets.h"
  50. #include <errno.h>
  51. #ifndef LWIP_PROVIDE_ERRNO
  52. #define LWIP_PROVIDE_ERRNO 1
  53. #endif
  54. #elif defined(FREESCALE_MQX)
  55. #include <posix.h>
  56. #include <rtcs.h>
  57. #elif defined(FREESCALE_KSDK_MQX)
  58. #include <rtcs.h>
  59. #elif (defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET))
  60. #include "cmsis_os.h"
  61. #include "rl_net.h"
  62. #include "errno.h"
  63. #elif defined(WOLFSSL_CMSIS_RTOS)
  64. #include "cmsis_os.h"
  65. #elif defined(WOLFSSL_TIRTOS)
  66. #include <sys/socket.h>
  67. #elif defined(FREERTOS_TCP)
  68. #include "FreeRTOS_Sockets.h"
  69. #elif defined(WOLFSSL_IAR_ARM)
  70. /* nothing */
  71. #elif defined(WOLFSSL_VXWORKS)
  72. #include <sockLib.h>
  73. #include <errno.h>
  74. #elif defined(WOLFSSL_ATMEL)
  75. #include "socket/include/socket.h"
  76. #elif defined(INTIME_RTOS)
  77. #undef MIN
  78. #undef MAX
  79. #include <rt.h>
  80. #include <sys/types.h>
  81. #include <sys/socket.h>
  82. #include <netdb.h>
  83. #include <netinet/in.h>
  84. #include <io.h>
  85. /* <sys/socket.h> defines these, to avoid conflict, do undef */
  86. #undef SOCKADDR
  87. #undef SOCKADDR_IN
  88. #elif defined(WOLFSSL_PRCONNECT_PRO)
  89. #include <prconnect_pro/prconnect_pro.h>
  90. #include <sys/types.h>
  91. #include <errno.h>
  92. #include <unistd.h>
  93. #include <fcntl.h>
  94. #include <netdb.h>
  95. #include <sys/ioctl.h>
  96. #elif defined(WOLFSSL_SGX)
  97. #include <errno.h>
  98. #elif !defined(WOLFSSL_NO_SOCK)
  99. #include <sys/types.h>
  100. #include <errno.h>
  101. #ifndef EBSNET
  102. #include <unistd.h>
  103. #endif
  104. #include <fcntl.h>
  105. #if defined(HAVE_RTP_SYS)
  106. #include <socket.h>
  107. #elif defined(EBSNET)
  108. #include "rtipapi.h" /* errno */
  109. #include "socket.h"
  110. #elif !defined(DEVKITPRO) && !defined(WOLFSSL_PICOTCP)
  111. #include <sys/socket.h>
  112. #include <arpa/inet.h>
  113. #include <netinet/in.h>
  114. #include <netdb.h>
  115. #ifdef __PPU
  116. #include <netex/errno.h>
  117. #else
  118. #include <sys/ioctl.h>
  119. #endif
  120. #endif
  121. #endif
  122. #endif /* USE_WINDOWS_API */
  123. #ifdef __sun
  124. #include <sys/filio.h>
  125. #endif
  126. #ifdef USE_WINDOWS_API
  127. /* no epipe yet */
  128. #ifndef WSAEPIPE
  129. #define WSAEPIPE -12345
  130. #endif
  131. #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
  132. #define SOCKET_EAGAIN WSAETIMEDOUT
  133. #define SOCKET_ECONNRESET WSAECONNRESET
  134. #define SOCKET_EINTR WSAEINTR
  135. #define SOCKET_EPIPE WSAEPIPE
  136. #define SOCKET_ECONNREFUSED WSAENOTCONN
  137. #define SOCKET_ECONNABORTED WSAECONNABORTED
  138. #define close(s) closesocket(s)
  139. #elif defined(__PPU)
  140. #define SOCKET_EWOULDBLOCK SYS_NET_EWOULDBLOCK
  141. #define SOCKET_EAGAIN SYS_NET_EAGAIN
  142. #define SOCKET_ECONNRESET SYS_NET_ECONNRESET
  143. #define SOCKET_EINTR SYS_NET_EINTR
  144. #define SOCKET_EPIPE SYS_NET_EPIPE
  145. #define SOCKET_ECONNREFUSED SYS_NET_ECONNREFUSED
  146. #define SOCKET_ECONNABORTED SYS_NET_ECONNABORTED
  147. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  148. #if MQX_USE_IO_OLD
  149. /* RTCS old I/O doesn't have an EWOULDBLOCK */
  150. #define SOCKET_EWOULDBLOCK EAGAIN
  151. #define SOCKET_EAGAIN EAGAIN
  152. #define SOCKET_ECONNRESET RTCSERR_TCP_CONN_RESET
  153. #define SOCKET_EINTR EINTR
  154. #define SOCKET_EPIPE EPIPE
  155. #define SOCKET_ECONNREFUSED RTCSERR_TCP_CONN_REFUSED
  156. #define SOCKET_ECONNABORTED RTCSERR_TCP_CONN_ABORTED
  157. #else
  158. #define SOCKET_EWOULDBLOCK NIO_EWOULDBLOCK
  159. #define SOCKET_EAGAIN NIO_EAGAIN
  160. #define SOCKET_ECONNRESET NIO_ECONNRESET
  161. #define SOCKET_EINTR NIO_EINTR
  162. #define SOCKET_EPIPE NIO_EPIPE
  163. #define SOCKET_ECONNREFUSED NIO_ECONNREFUSED
  164. #define SOCKET_ECONNABORTED NIO_ECONNABORTED
  165. #endif
  166. #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET)
  167. #define SOCKET_EWOULDBLOCK BSD_ERROR_WOULDBLOCK
  168. #define SOCKET_EAGAIN BSD_ERROR_LOCKED
  169. #define SOCKET_ECONNRESET BSD_ERROR_CLOSED
  170. #define SOCKET_EINTR BSD_ERROR
  171. #define SOCKET_EPIPE BSD_ERROR
  172. #define SOCKET_ECONNREFUSED BSD_ERROR
  173. #define SOCKET_ECONNABORTED BSD_ERROR
  174. #elif defined(WOLFSSL_PICOTCP)
  175. #define SOCKET_EWOULDBLOCK PICO_ERR_EAGAIN
  176. #define SOCKET_EAGAIN PICO_ERR_EAGAIN
  177. #define SOCKET_ECONNRESET PICO_ERR_ECONNRESET
  178. #define SOCKET_EINTR PICO_ERR_EINTR
  179. #define SOCKET_EPIPE PICO_ERR_EIO
  180. #define SOCKET_ECONNREFUSED PICO_ERR_ECONNREFUSED
  181. #define SOCKET_ECONNABORTED PICO_ERR_ESHUTDOWN
  182. #elif defined(FREERTOS_TCP)
  183. #define SOCKET_EWOULDBLOCK FREERTOS_EWOULDBLOCK
  184. #define SOCKET_EAGAIN FREERTOS_EWOULDBLOCK
  185. #define SOCKET_ECONNRESET FREERTOS_SOCKET_ERROR
  186. #define SOCKET_EINTR FREERTOS_SOCKET_ERROR
  187. #define SOCKET_EPIPE FREERTOS_SOCKET_ERROR
  188. #define SOCKET_ECONNREFUSED FREERTOS_SOCKET_ERROR
  189. #define SOCKET_ECONNABORTED FREERTOS_SOCKET_ERROR
  190. #else
  191. #define SOCKET_EWOULDBLOCK EWOULDBLOCK
  192. #define SOCKET_EAGAIN EAGAIN
  193. #define SOCKET_ECONNRESET ECONNRESET
  194. #define SOCKET_EINTR EINTR
  195. #define SOCKET_EPIPE EPIPE
  196. #define SOCKET_ECONNREFUSED ECONNREFUSED
  197. #define SOCKET_ECONNABORTED ECONNABORTED
  198. #endif /* USE_WINDOWS_API */
  199. #ifdef DEVKITPRO
  200. /* from network.h */
  201. int net_send(int, const void*, int, unsigned int);
  202. int net_recv(int, void*, int, unsigned int);
  203. #define SEND_FUNCTION net_send
  204. #define RECV_FUNCTION net_recv
  205. #elif defined(WOLFSSL_LWIP)
  206. #define SEND_FUNCTION lwip_send
  207. #define RECV_FUNCTION lwip_recv
  208. #elif defined(WOLFSSL_PICOTCP)
  209. #define SEND_FUNCTION pico_send
  210. #define RECV_FUNCTION pico_recv
  211. #elif defined(FREERTOS_TCP)
  212. #define RECV_FUNCTION(a,b,c,d) FreeRTOS_recv((Socket_t)(a),(void*)(b), (size_t)(c), (BaseType_t)(d))
  213. #define SEND_FUNCTION(a,b,c,d) FreeRTOS_send((Socket_t)(a),(void*)(b), (size_t)(c), (BaseType_t)(d))
  214. #elif defined(WOLFSSL_VXWORKS)
  215. #define SEND_FUNCTION send
  216. #define RECV_FUNCTION recv
  217. #else
  218. #define SEND_FUNCTION send
  219. #define RECV_FUNCTION recv
  220. #if !defined(HAVE_SOCKADDR) && !defined(WOLFSSL_NO_SOCK)
  221. #define HAVE_SOCKADDR
  222. #endif
  223. #endif
  224. #ifdef USE_WINDOWS_API
  225. typedef unsigned int SOCKET_T;
  226. #else
  227. typedef int SOCKET_T;
  228. #endif
  229. #ifndef WOLFSSL_NO_SOCK
  230. #ifndef XSOCKLENT
  231. #ifdef USE_WINDOWS_API
  232. #define XSOCKLENT int
  233. #else
  234. #define XSOCKLENT socklen_t
  235. #endif
  236. #endif
  237. /* Socket Addr Support */
  238. #ifdef HAVE_SOCKADDR
  239. typedef struct sockaddr SOCKADDR;
  240. typedef struct sockaddr_storage SOCKADDR_S;
  241. typedef struct sockaddr_in SOCKADDR_IN;
  242. #ifdef WOLFSSL_IPV6
  243. typedef struct sockaddr_in6 SOCKADDR_IN6;
  244. #endif
  245. typedef struct hostent HOSTENT;
  246. #endif /* HAVE_SOCKADDR */
  247. #ifdef HAVE_GETADDRINFO
  248. typedef struct addrinfo ADDRINFO;
  249. #endif
  250. #endif /* WOLFSSL_NO_SOCK */
  251. /* IO API's */
  252. #ifdef HAVE_IO_TIMEOUT
  253. WOLFSSL_API int wolfIO_SetBlockingMode(SOCKET_T sockfd, int non_blocking);
  254. WOLFSSL_API void wolfIO_SetTimeout(int to_sec);;
  255. WOLFSSL_API int wolfIO_Select(SOCKET_T sockfd, int to_sec);
  256. #endif
  257. WOLFSSL_API int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip,
  258. unsigned short port, int to_sec);
  259. WOLFSSL_API int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags);
  260. WOLFSSL_API int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags);
  261. #endif /* USE_WOLFSSL_IO || HAVE_HTTP_CLIENT */
  262. WOLFSSL_API int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx);
  263. WOLFSSL_API int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx);
  264. #if defined(USE_WOLFSSL_IO)
  265. /* default IO callbacks */
  266. WOLFSSL_API int EmbedReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx);
  267. WOLFSSL_API int EmbedSend(WOLFSSL* ssl, char* buf, int sz, void* ctx);
  268. #ifdef WOLFSSL_DTLS
  269. WOLFSSL_API int EmbedReceiveFrom(WOLFSSL* ssl, char* buf, int sz, void*);
  270. WOLFSSL_API int EmbedSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
  271. WOLFSSL_API int EmbedGenerateCookie(WOLFSSL* ssl, unsigned char* buf,
  272. int sz, void*);
  273. #ifdef WOLFSSL_MULTICAST
  274. WOLFSSL_API int EmbedReceiveFromMcast(WOLFSSL* ssl,
  275. char* buf, int sz, void*);
  276. #endif /* WOLFSSL_MULTICAST */
  277. #ifdef WOLFSSL_SESSION_EXPORT
  278. WOLFSSL_API int EmbedGetPeer(WOLFSSL* ssl, char* ip, int* ipSz,
  279. unsigned short* port, int* fam);
  280. WOLFSSL_API int EmbedSetPeer(WOLFSSL* ssl, char* ip, int ipSz,
  281. unsigned short port, int fam);
  282. #endif /* WOLFSSL_SESSION_EXPORT */
  283. #endif /* WOLFSSL_DTLS */
  284. #endif /* USE_WOLFSSL_IO */
  285. #ifdef HAVE_OCSP
  286. WOLFSSL_API int wolfIO_HttpBuildRequestOcsp(const char* domainName,
  287. const char* path, int ocspReqSz, unsigned char* buf, int bufSize);
  288. WOLFSSL_API int wolfIO_HttpProcessResponseOcsp(int sfd,
  289. unsigned char** respBuf, unsigned char* httpBuf, int httpBufSz,
  290. void* heap);
  291. WOLFSSL_API int EmbedOcspLookup(void*, const char*, int, unsigned char*,
  292. int, unsigned char**);
  293. WOLFSSL_API void EmbedOcspRespFree(void*, unsigned char*);
  294. #endif
  295. #ifdef HAVE_CRL_IO
  296. WOLFSSL_API int wolfIO_HttpBuildRequestCrl(const char* url, int urlSz,
  297. const char* domainName, unsigned char* buf, int bufSize);
  298. WOLFSSL_API int wolfIO_HttpProcessResponseCrl(WOLFSSL_CRL* crl, int sfd,
  299. unsigned char* httpBuf, int httpBufSz);
  300. WOLFSSL_API int EmbedCrlLookup(WOLFSSL_CRL* crl, const char* url,
  301. int urlSz);
  302. #endif
  303. #if defined(HAVE_HTTP_CLIENT)
  304. WOLFSSL_API int wolfIO_DecodeUrl(const char* url, int urlSz, char* outName,
  305. char* outPath, unsigned short* outPort);
  306. WOLFSSL_API int wolfIO_HttpBuildRequest(const char* reqType,
  307. const char* domainName, const char* path, int pathLen, int reqSz,
  308. const char* contentType, unsigned char* buf, int bufSize);
  309. WOLFSSL_API int wolfIO_HttpProcessResponse(int sfd, const char** appStrList,
  310. unsigned char** respBuf, unsigned char* httpBuf, int httpBufSz,
  311. int dynType, void* heap);
  312. #endif /* HAVE_HTTP_CLIENT */
  313. /* I/O callbacks */
  314. typedef int (*CallbackIORecv)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
  315. typedef int (*CallbackIOSend)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
  316. WOLFSSL_API void wolfSSL_SetIORecv(WOLFSSL_CTX*, CallbackIORecv);
  317. WOLFSSL_API void wolfSSL_SetIOSend(WOLFSSL_CTX*, CallbackIOSend);
  318. WOLFSSL_API void wolfSSL_SetIOReadCtx( WOLFSSL* ssl, void *ctx);
  319. WOLFSSL_API void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *ctx);
  320. WOLFSSL_API void* wolfSSL_GetIOReadCtx( WOLFSSL* ssl);
  321. WOLFSSL_API void* wolfSSL_GetIOWriteCtx(WOLFSSL* ssl);
  322. WOLFSSL_API void wolfSSL_SetIOReadFlags( WOLFSSL* ssl, int flags);
  323. WOLFSSL_API void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
  324. #ifdef HAVE_NETX
  325. WOLFSSL_LOCAL int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx);
  326. WOLFSSL_LOCAL int NetX_Send(WOLFSSL *ssl, char *buf, int sz, void *ctx);
  327. WOLFSSL_API void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket,
  328. ULONG waitoption);
  329. #endif /* HAVE_NETX */
  330. #ifdef MICRIUM
  331. WOLFSSL_LOCAL int MicriumSend(WOLFSSL* ssl, char* buf, int sz, void* ctx);
  332. WOLFSSL_LOCAL int MicriumReceive(WOLFSSL* ssl, char* buf, int sz,
  333. void* ctx);
  334. WOLFSSL_LOCAL int MicriumReceiveFrom(WOLFSSL* ssl, char* buf, int sz,
  335. void* ctx);
  336. WOLFSSL_LOCAL int MicriumSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
  337. #endif /* MICRIUM */
  338. #ifdef WOLFSSL_DTLS
  339. typedef int (*CallbackGenCookie)(WOLFSSL* ssl, unsigned char* buf, int sz,
  340. void* ctx);
  341. WOLFSSL_API void wolfSSL_CTX_SetGenCookie(WOLFSSL_CTX*, CallbackGenCookie);
  342. WOLFSSL_API void wolfSSL_SetCookieCtx(WOLFSSL* ssl, void *ctx);
  343. WOLFSSL_API void* wolfSSL_GetCookieCtx(WOLFSSL* ssl);
  344. #ifdef WOLFSSL_SESSION_EXPORT
  345. typedef int (*CallbackGetPeer)(WOLFSSL* ssl, char* ip, int* ipSz,
  346. unsigned short* port, int* fam);
  347. typedef int (*CallbackSetPeer)(WOLFSSL* ssl, char* ip, int ipSz,
  348. unsigned short port, int fam);
  349. WOLFSSL_API void wolfSSL_CTX_SetIOGetPeer(WOLFSSL_CTX*, CallbackGetPeer);
  350. WOLFSSL_API void wolfSSL_CTX_SetIOSetPeer(WOLFSSL_CTX*, CallbackSetPeer);
  351. #endif /* WOLFSSL_SESSION_EXPORT */
  352. #endif
  353. #ifndef XINET_NTOP
  354. #define XINET_NTOP(a,b,c,d) inet_ntop((a),(b),(c),(d))
  355. #ifdef USE_WINDOWS_API /* Windows-friendly definition */
  356. #undef XINET_NTOP
  357. #define XINET_NTOP(a,b,c,d) InetNtop((a),(b),(c),(d))
  358. #endif
  359. #endif
  360. #ifndef XINET_PTON
  361. #define XINET_PTON(a,b,c) inet_pton((a),(b),(c))
  362. #ifdef USE_WINDOWS_API /* Windows-friendly definition */
  363. #undef XINET_PTON
  364. #define XINET_PTON(a,b,c) InetPton((a),(b),(c))
  365. #endif
  366. #endif
  367. #ifndef XHTONS
  368. #define XHTONS(a) htons((a))
  369. #endif
  370. #ifndef XNTOHS
  371. #define XNTOHS(a) ntohs((a))
  372. #endif
  373. #ifndef WOLFSSL_IP4
  374. #define WOLFSSL_IP4 AF_INET
  375. #endif
  376. #ifndef WOLFSSL_IP6
  377. #define WOLFSSL_IP6 AF_INET6
  378. #endif
  379. #ifdef __cplusplus
  380. } /* extern "C" */
  381. #endif
  382. #endif /* WOLFSSL_IO_H */