server_wolfssl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* server_wolfssl.c
  2. *
  3. * Copyright (C) 2006-2024 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. #include <Source/net_sock.h>
  22. #include <Source/net_app.h>
  23. #include <Source/net_util.h>
  24. #include <Source/net_ascii.h>
  25. #include <app_cfg.h>
  26. #include "wolfssl/ssl.h"
  27. #include "server_wolfssl.h"
  28. #define TLS_SERVER_PORT 11111
  29. #define TX_BUF_SIZE 64
  30. #define RX_BUF_SIZE 1024
  31. #define TCP_SERVER_CONN_Q_SIZE 1
  32. /* derived from wolfSSL/certs/server-ecc.der */
  33. static const CPU_INT08U server_ecc_der_256[] = { 0x30, 0x82, 0x03, 0x10,
  34. 0x30, 0x82, 0x02, 0xB5, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
  35. 0xEF, 0x46, 0xC7, 0xA4, 0x9B, 0xBB, 0x60, 0xD3, 0x30, 0x0A, 0x06, 0x08,
  36. 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30, 0x81, 0x8F, 0x31,
  37. 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  38. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A, 0x57,
  39. 0x61, 0x73, 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31, 0x10, 0x30,
  40. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65, 0x61, 0x74,
  41. 0x74, 0x6C, 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x0A,
  42. 0x0C, 0x07, 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31, 0x0C, 0x30,
  43. 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43, 0x43, 0x31,
  44. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  45. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  46. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  47. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  48. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E,
  49. 0x17, 0x0D, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31, 0x32, 0x30, 0x30, 0x37,
  50. 0x33, 0x38, 0x5A, 0x17, 0x0D, 0x31, 0x39, 0x30, 0x35, 0x30, 0x38, 0x32,
  51. 0x30, 0x30, 0x37, 0x33, 0x38, 0x5A, 0x30, 0x81, 0x8F, 0x31, 0x0B, 0x30,
  52. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  53. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A, 0x57, 0x61, 0x73,
  54. 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31, 0x10, 0x30, 0x0E, 0x06,
  55. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6C,
  56. 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x07,
  57. 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31, 0x0C, 0x30, 0x0A, 0x06,
  58. 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43, 0x43, 0x31, 0x18, 0x30,
  59. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E,
  60. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31,
  61. 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
  62. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C,
  63. 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13,
  64. 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A,
  65. 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xBB,
  66. 0x33, 0xAC, 0x4C, 0x27, 0x50, 0x4A, 0xC6, 0x4A, 0xA5, 0x04, 0xC3, 0x3C,
  67. 0xDE, 0x9F, 0x36, 0xDB, 0x72, 0x2D, 0xCE, 0x94, 0xEA, 0x2B, 0xFA, 0xCB,
  68. 0x20, 0x09, 0x39, 0x2C, 0x16, 0xE8, 0x61, 0x02, 0xE9, 0xAF, 0x4D, 0xD3,
  69. 0x02, 0x93, 0x9A, 0x31, 0x5B, 0x97, 0x92, 0x21, 0x7F, 0xF0, 0xCF, 0x18,
  70. 0xDA, 0x91, 0x11, 0x02, 0x34, 0x86, 0xE8, 0x20, 0x58, 0x33, 0x0B, 0x80,
  71. 0x34, 0x89, 0xD8, 0xA3, 0x81, 0xF7, 0x30, 0x81, 0xF4, 0x30, 0x1D, 0x06,
  72. 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x5D, 0x5D, 0x26, 0xEF,
  73. 0xAC, 0x7E, 0x36, 0xF9, 0x9B, 0x76, 0x15, 0x2B, 0x4A, 0x25, 0x02, 0x23,
  74. 0xEF, 0xB2, 0x89, 0x30, 0x30, 0x81, 0xC4, 0x06, 0x03, 0x55, 0x1D, 0x23,
  75. 0x04, 0x81, 0xBC, 0x30, 0x81, 0xB9, 0x80, 0x14, 0x5D, 0x5D, 0x26, 0xEF,
  76. 0xAC, 0x7E, 0x36, 0xF9, 0x9B, 0x76, 0x15, 0x2B, 0x4A, 0x25, 0x02, 0x23,
  77. 0xEF, 0xB2, 0x89, 0x30, 0xA1, 0x81, 0x95, 0xA4, 0x81, 0x92, 0x30, 0x81,
  78. 0x8F, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  79. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C,
  80. 0x0A, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31,
  81. 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65,
  82. 0x61, 0x74, 0x74, 0x6C, 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55,
  83. 0x04, 0x0A, 0x0C, 0x07, 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31,
  84. 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43,
  85. 0x43, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F,
  86. 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  87. 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48,
  88. 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F,
  89. 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  90. 0x82, 0x09, 0x00, 0xEF, 0x46, 0xC7, 0xA4, 0x9B, 0xBB, 0x60, 0xD3, 0x30,
  91. 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01,
  92. 0xFF, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03,
  93. 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xF1, 0xD0, 0xA6,
  94. 0x3E, 0x83, 0x33, 0x24, 0xD1, 0x7A, 0x05, 0x5F, 0x1E, 0x0E, 0xBD, 0x7D,
  95. 0x6B, 0x33, 0xE9, 0xF2, 0x86, 0xF3, 0xF3, 0x3D, 0xA9, 0xEF, 0x6A, 0x87,
  96. 0x31, 0xB3, 0xB7, 0x7E, 0x50, 0x02, 0x21, 0x00, 0xF0, 0x60, 0xDD, 0xCE,
  97. 0xA2, 0xDB, 0x56, 0xEC, 0xD9, 0xF4, 0xE4, 0xE3, 0x25, 0xD4, 0xB0, 0xC9,
  98. 0x25, 0x7D, 0xCA, 0x7A, 0x5D, 0xBA, 0xC4, 0xB2, 0xF6, 0x7D, 0x04, 0xC7,
  99. 0xBD, 0x62, 0xC9, 0x20 };
  100. /* derived from wolfSSL/certs/ecc-key.der */
  101. static const CPU_INT08U ecc_key_der_256[] = { 0x30, 0x77, 0x02, 0x01, 0x01,
  102. 0x04, 0x20, 0x45, 0xB6, 0x69, 0x02, 0x73, 0x9C, 0x6C, 0x85, 0xA1, 0x38,
  103. 0x5B, 0x72, 0xE8, 0xE8, 0xC7, 0xAC, 0xC4, 0x03, 0x8D, 0x53, 0x35, 0x04,
  104. 0xFA, 0x6C, 0x28, 0xDC, 0x34, 0x8D, 0xE1, 0xA8, 0x09, 0x8C, 0xA0, 0x0A,
  105. 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0xA1, 0x44,
  106. 0x03, 0x42, 0x00, 0x04, 0xBB, 0x33, 0xAC, 0x4C, 0x27, 0x50, 0x4A, 0xC6,
  107. 0x4A, 0xA5, 0x04, 0xC3, 0x3C, 0xDE, 0x9F, 0x36, 0xDB, 0x72, 0x2D, 0xCE,
  108. 0x94, 0xEA, 0x2B, 0xFA, 0xCB, 0x20, 0x09, 0x39, 0x2C, 0x16, 0xE8, 0x61,
  109. 0x02, 0xE9, 0xAF, 0x4D, 0xD3, 0x02, 0x93, 0x9A, 0x31, 0x5B, 0x97, 0x92,
  110. 0x21, 0x7F, 0xF0, 0xCF, 0x18, 0xDA, 0x91, 0x11, 0x02, 0x34, 0x86, 0xE8,
  111. 0x20, 0x58, 0x33, 0x0B, 0x80, 0x34, 0x89, 0xD8 };
  112. int wolfssl_server_test(void)
  113. {
  114. NET_ERR err;
  115. NET_SOCK_ID sock_listen;
  116. NET_SOCK_ID sock_req;
  117. NET_SOCK_ADDR_IPv4 server_addr;
  118. NET_SOCK_ADDR_LEN server_addr_len;
  119. NET_SOCK_ADDR_IPv4 client_sock_addr_ip;
  120. NET_SOCK_ADDR_LEN client_sock_addr_ip_size;
  121. CPU_CHAR rx_buf[RX_BUF_SIZE];
  122. CPU_CHAR tx_buf[TX_BUF_SIZE];
  123. CPU_BOOLEAN attempt_conn;
  124. OS_ERR os_err;
  125. WOLFSSL * ssl;
  126. WOLFSSL_CTX * ctx;
  127. int tx_buf_sz = 0, ret = 0, error = 0;
  128. #ifdef DEBUG_WOLFSSL
  129. wolfSSL_Debugging_ON();
  130. #endif
  131. /* wolfSSL INIT and CTX SETUP */
  132. wolfSSL_Init();
  133. /* SET UP NETWORK SOCKET */
  134. APP_TRACE_INFO(("Opening network socket...\r\n"));
  135. sock_listen = NetSock_Open(NET_SOCK_ADDR_FAMILY_IP_V4,
  136. NET_SOCK_TYPE_STREAM,
  137. NET_SOCK_PROTOCOL_TCP,
  138. &err);
  139. if (err != NET_SOCK_ERR_NONE) {
  140. APP_TRACE_INFO(("ERROR: NetSock_Open, err = %d\r\n", (int) err));
  141. return -1;
  142. }
  143. APP_TRACE_INFO(("Clearing memory for server_addr struct\r\n"));
  144. server_addr_len = sizeof(server_addr);
  145. Mem_Clr((void *) &server_addr, (CPU_SIZE_T) server_addr_len);
  146. APP_TRACE_INFO(("Setting up server_addr struct\r\n"));
  147. server_addr.AddrFamily = NET_SOCK_ADDR_FAMILY_IP_V4;
  148. server_addr.Addr = NET_UTIL_HOST_TO_NET_32(NET_SOCK_ADDR_IP_V4_WILDCARD);
  149. server_addr.Port = NET_UTIL_HOST_TO_NET_16(TLS_SERVER_PORT);
  150. NetSock_Bind((NET_SOCK_ID) sock_listen,
  151. (NET_SOCK_ADDR*) &server_addr,
  152. (NET_SOCK_ADDR_LEN) NET_SOCK_ADDR_SIZE,
  153. (NET_ERR*) &err);
  154. if (err != NET_SOCK_ERR_NONE) {
  155. APP_TRACE_INFO(("ERROR: NetSock_Bind, err = %d\r\n", (int) err));
  156. NetSock_Close(sock_listen, &err);
  157. return -1;
  158. }
  159. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  160. if (ctx == 0) {
  161. APP_TRACE_INFO(("ERROR: wolfSSL_CTX_new failed\r\n"));
  162. NetSock_Close(sock_listen, &err);
  163. return -1;
  164. }
  165. APP_TRACE_INFO(("wolfSSL_CTX_new done\r\n"));
  166. ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  167. server_ecc_der_256,
  168. sizeof(server_ecc_der_256),
  169. SSL_FILETYPE_ASN1);
  170. if (ret != SSL_SUCCESS) {
  171. APP_TRACE_INFO(
  172. ("ERROR: wolfSSL_CTX_use_certificate_buffer() failed\r\n"));
  173. NetSock_Close(sock_listen, &err);
  174. wolfSSL_CTX_free(ctx);
  175. return -1;
  176. }
  177. ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx,
  178. ecc_key_der_256,
  179. sizeof(ecc_key_der_256),
  180. SSL_FILETYPE_ASN1);
  181. if (ret != SSL_SUCCESS) {
  182. APP_TRACE_INFO(
  183. ("ERROR: wolfSSL_CTX_use_PrivateKey_buffer() failed\r\n"));
  184. NetSock_Close(sock_listen, &err);
  185. wolfSSL_CTX_free(ctx);
  186. return -1;
  187. }
  188. /* accept client socket connections */
  189. APP_TRACE_INFO(("Listening for client connection\r\n"));
  190. NetSock_Listen(sock_listen, TCP_SERVER_CONN_Q_SIZE, &err);
  191. if (err != NET_SOCK_ERR_NONE) {
  192. APP_TRACE_INFO(("ERROR: NetSock_Listen, err = %d\r\n", (int) err));
  193. NetSock_Close(sock_listen, &err);
  194. wolfSSL_CTX_free(ctx);
  195. return -1;
  196. }
  197. do {
  198. client_sock_addr_ip_size = sizeof(client_sock_addr_ip);
  199. sock_req = NetSock_Accept((NET_SOCK_ID) sock_listen,
  200. (NET_SOCK_ADDR*) &client_sock_addr_ip,
  201. (NET_SOCK_ADDR_LEN*) &client_sock_addr_ip_size,
  202. (NET_ERR*) &err);
  203. switch (err) {
  204. case NET_SOCK_ERR_NONE:
  205. attempt_conn = DEF_NO;
  206. break;
  207. case NET_ERR_INIT_INCOMPLETE:
  208. case NET_SOCK_ERR_NULL_PTR:
  209. case NET_SOCK_ERR_NONE_AVAIL:
  210. case NET_SOCK_ERR_CONN_ACCEPT_Q_NONE_AVAIL:
  211. attempt_conn = DEF_YES;
  212. break;
  213. case NET_SOCK_ERR_CONN_SIGNAL_TIMEOUT:
  214. APP_TRACE_INFO(
  215. ("NetSockAccept err = NET_SOCK_ERR_CONN_SIGNAL_TIMEOUT\r\n"));
  216. attempt_conn = DEF_YES;
  217. break;
  218. default:
  219. attempt_conn = DEF_NO;
  220. break;
  221. }
  222. } while (attempt_conn == DEF_YES);
  223. if (err != NET_SOCK_ERR_NONE) {
  224. APP_TRACE_INFO(("ERROR: NetSock_Accept, err = %d\r\n", (int) err));
  225. NetSock_Close(sock_listen, &err);
  226. return -1;
  227. }
  228. APP_TRACE_INFO(("Got client connection! Starting TLS negotiation\r\n"));
  229. /* set up wolfSSL session */
  230. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  231. APP_TRACE_INFO(("ERROR: wolfSSL_new() failed\r\n"));
  232. NetSock_Close(sock_req, &err);
  233. NetSock_Close(sock_listen, &err);
  234. wolfSSL_CTX_free(ctx);
  235. return -1;
  236. }
  237. APP_TRACE_INFO(("wolfSSL_new done\r\n"));
  238. ret = wolfSSL_set_fd(ssl, sock_req);
  239. if (ret != SSL_SUCCESS) {
  240. APP_TRACE_INFO(("ERROR: wolfSSL_set_fd() failed\r\n"));
  241. NetSock_Close(sock_req, &err);
  242. NetSock_Close(sock_listen, &err);
  243. wolfSSL_free(ssl);
  244. wolfSSL_CTX_free(ctx);
  245. return -1;
  246. }
  247. APP_TRACE_INFO(("wolfSSL_set_fd done\r\n"));
  248. do {
  249. error = 0; /* reset error */
  250. if (ret != SSL_SUCCESS) {
  251. error = wolfSSL_get_error(ssl, 0);
  252. APP_TRACE_INFO(
  253. ("ERROR: wolfSSL_accept() failed, err = %d\r\n", error));
  254. if (error != SSL_ERROR_WANT_READ) {
  255. NetSock_Close(sock_req, &err);
  256. NetSock_Close(sock_listen, &err);
  257. wolfSSL_free(ssl);
  258. wolfSSL_CTX_free(ctx);
  259. return -1;
  260. }
  261. OSTimeDlyHMSM(0u, 0u, 0u, 500u, OS_OPT_TIME_HMSM_STRICT, &os_err);
  262. }
  263. } while ((ret != SSL_SUCCESS) && (error == SSL_ERROR_WANT_READ));
  264. APP_TRACE_INFO(("wolfSSL_accept() ok...\r\n"));
  265. /* read client data */
  266. error = 0;
  267. Mem_Set(rx_buf, 0, RX_BUF_SIZE);
  268. ret = wolfSSL_read(ssl, rx_buf, RX_BUF_SIZE - 1);
  269. if (ret < 0) {
  270. error = wolfSSL_get_error(ssl, 0);
  271. if (error != SSL_ERROR_WANT_READ) {
  272. APP_TRACE_INFO(("wolfSSL_read failed, error = %d\r\n", error));
  273. NetSock_Close(sock_req, &err);
  274. NetSock_Close(sock_listen, &err);
  275. wolfSSL_free(ssl);
  276. wolfSSL_CTX_free(ctx);
  277. return -1;
  278. }
  279. }
  280. APP_TRACE_INFO(("AFTER wolfSSL_read() call, ret = %d\r\n", ret));
  281. if (ret > 0) {
  282. rx_buf[ret] = 0;
  283. APP_TRACE_INFO(("Client sent: %s\r\n", rx_buf));
  284. }
  285. /* write response to client */
  286. Mem_Set(tx_buf, 0, TX_BUF_SIZE);
  287. tx_buf_sz = 22;
  288. Str_Copy_N(tx_buf, "I hear ya fa shizzle!\n", tx_buf_sz);
  289. if (wolfSSL_write(ssl, tx_buf, tx_buf_sz) != tx_buf_sz) {
  290. error = wolfSSL_get_error(ssl, 0);
  291. APP_TRACE_INFO(("ERROR: wolfSSL_write() failed, err = %d\r\n", error));
  292. NetSock_Close(sock_req, &err);
  293. NetSock_Close(sock_listen, &err);
  294. wolfSSL_free(ssl);
  295. wolfSSL_CTX_free(ctx);
  296. return -1;
  297. }
  298. ret = wolfSSL_shutdown(ssl);
  299. if (ret == SSL_SHUTDOWN_NOT_DONE)
  300. wolfSSL_shutdown(ssl);
  301. wolfSSL_free(ssl);
  302. wolfSSL_CTX_free(ctx);
  303. wolfSSL_Cleanup();
  304. NetSock_Close(sock_req, &err);
  305. NetSock_Close(sock_listen, &err);
  306. return 0;
  307. }