1
0

tls_wolfssl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /* tls_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 <wolfssl/wolfcrypt/settings.h>
  22. #include <wolfssl/ssl.h>
  23. #include <wolfcrypt/test/test.h>
  24. #include <wolfcrypt/benchmark/benchmark.h>
  25. #include <wolfssl/wolfcrypt/logging.h> /* to use WOLFSSL_MSG */
  26. #include <tls_wolfssl.h>
  27. int setupTransport(clientConnectionHandleType* connectionHandle,
  28. char* connectionId) {
  29. int ret, error;
  30. void * sendBuffer;
  31. size_t bufferSizeInBytes;
  32. if ((ret = socketTransportInitialize("mailbox-transport.config",
  33. "transportConfigurationId",
  34. waitIndefinitely,&error)) != transportSuccess)
  35. printf("Initialize 0x%x, error=%d\n", ret, error);
  36. else if ((ret = socketTransportClientInitialize(waitIndefinitely,
  37. &error)) != transportSuccess)
  38. printf("ClientInitialize 0x%x, error=%d\n", ret, error);
  39. else if ((ret = socketTransportCreateConnection(connectionId,
  40. waitIndefinitely,
  41. COMPATIBILITY_ID_2,
  42. connectionHandle,
  43. &sendBuffer,
  44. &bufferSizeInBytes,
  45. &error)) != transportSuccess)
  46. printf("CreateConnection 0x%x, error=%d\n", ret, error);
  47. else if ((ret = socketTransportSetConnectionForThread(currentThreadHandle(),
  48. *connectionHandle,
  49. waitIndefinitely,
  50. &error)) != transportSuccess)
  51. printf("SetConnectionForThread 0x%x, error=%d\n", ret, error);
  52. return ret;
  53. }
  54. #if !defined(NO_WOLFSSL_CLIENT )
  55. /* 172.217.3.174 is the IP address of https://www.google.com */
  56. #define TCP_SERVER_IP_ADDR "172.217.3.174"
  57. #define TCP_SERVER_DOMAIN_NAME "www.google.com"
  58. #define TCP_SERVER_PORT 443
  59. #define TX_BUF_SIZE 64
  60. #define RX_BUF_SIZE 1024
  61. #define TX_MSG "GET /index.html HTTP/1.0\n\n"
  62. #define TX_MSG_SIZE sizeof(TX_MSG)
  63. static const unsigned char google_certs_ca[]="\n\
  64. ## Google Internet Authority G3 \n\
  65. -----BEGIN CERTIFICATE-----\n\
  66. MIIEXDCCA0SgAwIBAgINAeOpMBz8cgY4P5pTHTANBgkqhkiG9w0BAQsFADBMMSAw\n\
  67. HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEGA1UEChMKR2xvYmFs\n\
  68. U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xNzA2MTUwMDAwNDJaFw0yMTEy\n\
  69. MTUwMDAwNDJaMFQxCzAJBgNVBAYTAlVTMR4wHAYDVQQKExVHb29nbGUgVHJ1c3Qg\n\
  70. U2VydmljZXMxJTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzMw\n\
  71. ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDKUkvqHv/OJGuo2nIYaNVW\n\
  72. XQ5IWi01CXZaz6TIHLGp/lOJ+600/4hbn7vn6AAB3DVzdQOts7G5pH0rJnnOFUAK\n\
  73. 71G4nzKMfHCGUksW/mona+Y2emJQ2N+aicwJKetPKRSIgAuPOB6Aahh8Hb2XO3h9\n\
  74. RUk2T0HNouB2VzxoMXlkyW7XUR5mw6JkLHnA52XDVoRTWkNty5oCINLvGmnRsJ1z\n\
  75. ouAqYGVQMc/7sy+/EYhALrVJEA8KbtyX+r8snwU5C1hUrwaW6MWOARa8qBpNQcWT\n\
  76. kaIeoYvy/sGIJEmjR0vFEwHdp1cSaWIr6/4g72n7OqXwfinu7ZYW97EfoOSQJeAz\n\
  77. AgMBAAGjggEzMIIBLzAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0lBBYwFAYIKwYBBQUH\n\
  78. AwEGCCsGAQUFBwMCMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFHfCuFCa\n\
  79. Z3Z2sS3ChtCDoH6mfrpLMB8GA1UdIwQYMBaAFJviB1dnHB7AagbeWbSaLd/cGYYu\n\
  80. MDUGCCsGAQUFBwEBBCkwJzAlBggrBgEFBQcwAYYZaHR0cDovL29jc3AucGtpLmdv\n\
  81. b2cvZ3NyMjAyBgNVHR8EKzApMCegJaAjhiFodHRwOi8vY3JsLnBraS5nb29nL2dz\n\
  82. cjIvZ3NyMi5jcmwwPwYDVR0gBDgwNjA0BgZngQwBAgIwKjAoBggrBgEFBQcCARYc\n\
  83. aHR0cHM6Ly9wa2kuZ29vZy9yZXBvc2l0b3J5LzANBgkqhkiG9w0BAQsFAAOCAQEA\n\
  84. HLeJluRT7bvs26gyAZ8so81trUISd7O45skDUmAge1cnxhG1P2cNmSxbWsoiCt2e\n\
  85. ux9LSD+PAj2LIYRFHW31/6xoic1k4tbWXkDCjir37xTTNqRAMPUyFRWSdvt+nlPq\n\
  86. wnb8Oa2I/maSJukcxDjNSfpDh/Bd1lZNgdd/8cLdsE3+wypufJ9uXO1iQpnh9zbu\n\
  87. FIwsIONGl1p3A8CgxkqI/UAih3JaGOqcpcdaCIzkBaR9uYQ1X4k2Vg5APRLouzVy\n\
  88. 7a8IVk6wuy6pm+T7HT4LY8ibS5FEZlfAFLSW8NwsVz9SBK2Vqn1N0PIMn5xA6NZV\n\
  89. c7o835DLAFshEWfC7TIe3g==\n\
  90. -----END CERTIFICATE-----\n\
  91. ## Google Trust Services- GlobalSign Root CA-R2\n\
  92. -----BEGIN CERTIFICATE-----\n\
  93. MIIDujCCAqKgAwIBAgILBAAAAAABD4Ym5g0wDQYJKoZIhvcNAQEFBQAwTDEgMB4G\n\
  94. A1UECxMXR2xvYmFsU2lnbiBSb290IENBIC0gUjIxEzARBgNVBAoTCkdsb2JhbFNp\n\
  95. Z24xEzARBgNVBAMTCkdsb2JhbFNpZ24wHhcNMDYxMjE1MDgwMDAwWhcNMjExMjE1\n\
  96. MDgwMDAwWjBMMSAwHgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEG\n\
  97. A1UEChMKR2xvYmFsU2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjCCASIwDQYJKoZI\n\
  98. hvcNAQEBBQADggEPADCCAQoCggEBAKbPJA6+Lm8omUVCxKs+IVSbC9N/hHD6ErPL\n\
  99. v4dfxn+G07IwXNb9rfF73OX4YJYJkhD10FPe+3t+c4isUoh7SqbKSaZeqKeMWhG8\n\
  100. eoLrvozps6yWJQeXSpkqBy+0Hne/ig+1AnwblrjFuTosvNYSuetZfeLQBoZfXklq\n\
  101. tTleiDTsvHgMCJiEbKjNS7SgfQx5TfC4LcshytVsW33hoCmEofnTlEnLJGKRILzd\n\
  102. C9XZzPnqJworc5HGnRusyMvo4KD0L5CLTfuwNhv2GXqF4G3yYROIXJ/gkwpRl4pa\n\
  103. zq+r1feqCapgvdzZX99yqWATXgAByUr6P6TqBwMhAo6CygPCm48CAwEAAaOBnDCB\n\
  104. mTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUm+IH\n\
  105. V2ccHsBqBt5ZtJot39wZhi4wNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL2NybC5n\n\
  106. bG9iYWxzaWduLm5ldC9yb290LXIyLmNybDAfBgNVHSMEGDAWgBSb4gdXZxwewGoG\n\
  107. 3lm0mi3f3BmGLjANBgkqhkiG9w0BAQUFAAOCAQEAmYFThxxol4aR7OBKuEQLq4Gs\n\
  108. J0/WwbgcQ3izDJr86iw8bmEbTUsp9Z8FHSbBuOmDAGJFtqkIk7mpM0sYmsL4h4hO\n\
  109. 291xNBrBVNpGP+DTKqttVCL1OmLNIG+6KYnX3ZHu01yiPqFbQfXf5WRDLenVOavS\n\
  110. ot+3i9DAgBkcRcAtjOj4LaR0VknFBbVPFd5uRHg5h6h+u/N5GJG79G+dwfCMNYxd\n\
  111. AfvDbbnvRG15RjF+Cv6pgsH/76tuIMRQyV+dTZsXjAzlAcmgQWpzU/qlULRuJQ/7\n\
  112. TBj0/VLZjmmx6BEP3ojY+x1J96relc8geMJgEtslQIxq/H5COEBkEveegeGTLg==\n\
  113. -----END CERTIFICATE-----\n\
  114. ";
  115. void wolfssl_client_test(uintData_t statusPtr) {
  116. int sock;
  117. char rx_buf[RX_BUF_SIZE];
  118. char tx_buf[TX_BUF_SIZE];
  119. int ret = 0, error = 0;
  120. sockaddr_in server_addr;
  121. clientConnectionHandleType TCPclientHandle;
  122. WOLFSSL* ssl;
  123. WOLFSSL_CTX* ctx;
  124. /* set up the mailbox transport */
  125. if (setupTransport(&TCPclientHandle, (char*)"connectionId1") != transportSuccess){
  126. printf("TCP transport set up failed \n");
  127. return;
  128. }
  129. printf("Creating a network socket...\n");
  130. sock = socket(AF_INET, SOCK_STREAM, 0);
  131. if (sock == SOCKET_ERROR) {
  132. printf("ERROR: Failed to create socket, err = %d\n", errno);
  133. return;
  134. }
  135. printf("Clearing memory for server_addr struct\n");
  136. XMEMSET((char *) &server_addr, 0u, sizeof(server_addr));
  137. printf("Connecting to server IP address: %s, port: %d\n",
  138. TCP_SERVER_IP_ADDR, TCP_SERVER_PORT);
  139. server_addr.sin_family = AF_INET;
  140. server_addr.sin_addr.s_addr = inet_addr(TCP_SERVER_IP_ADDR);
  141. server_addr.sin_port = htons(TCP_SERVER_PORT);
  142. printf("Calling connect on socket\n");
  143. if (connect(sock, (sockaddr *) &server_addr, sizeof(server_addr)) < 0 ) {
  144. printf("ERROR: connect, err = %d\n", errno);
  145. closesocket(sock);
  146. return;
  147. }
  148. #ifdef DEBUG_WOLFSSL
  149. wolfSSL_Debugging_ON();
  150. #endif
  151. /* wolfSSL INIT and CTX SETUP */
  152. wolfSSL_Init();
  153. /* chooses the highest possible TLS version */
  154. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  155. /* SET UP NETWORK SOCKET */
  156. if (ctx == 0) {
  157. printf("ERROR: wolfSSL_CTX_new failed\n");
  158. closesocket(sock);
  159. return;
  160. }
  161. WOLFSSL_MSG("wolfSSL_CTX_new done");
  162. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  163. ret = wolfSSL_CTX_load_verify_buffer(ctx,
  164. google_certs_ca,
  165. sizeof(google_certs_ca),
  166. SSL_FILETYPE_PEM);
  167. if (ret != SSL_SUCCESS) {
  168. printf("ERROR: wolfSSL_CTX_load_verify_buffer() failed\n");
  169. closesocket(sock);
  170. wolfSSL_CTX_free(ctx);
  171. return;
  172. }
  173. ssl = wolfSSL_new(ctx);
  174. if (ssl == NULL) {
  175. printf("ERROR: wolfSSL_new() failed\n");
  176. closesocket(sock);
  177. wolfSSL_CTX_free(ctx);
  178. return;
  179. }
  180. WOLFSSL_MSG("wolfSSL_new done");
  181. ret = wolfSSL_set_fd(ssl, sock);
  182. if (ret != SSL_SUCCESS) {
  183. printf("ERROR: wolfSSL_set_fd() failed\n");
  184. closesocket(sock);
  185. wolfSSL_free(ssl);
  186. wolfSSL_CTX_free(ctx);
  187. return;
  188. }
  189. WOLFSSL_MSG("wolfSSL_set_fd done");
  190. do {
  191. error = 0; /* reset error */
  192. ret = wolfSSL_connect(ssl);
  193. if (ret != SSL_SUCCESS) {
  194. error = wolfSSL_get_error(ssl, 0);
  195. printf("ERROR: wolfSSL_connect() failed, err = %d\n", error);
  196. if (error != SSL_ERROR_WANT_READ) {
  197. closesocket(sock);
  198. wolfSSL_free(ssl);
  199. wolfSSL_CTX_free(ctx);
  200. return;
  201. }
  202. /* goToSleep() for 1 sec*/
  203. }
  204. } while ((ret != SSL_SUCCESS) && (error == SSL_ERROR_WANT_READ));
  205. printf("wolfSSL_connect() ok... sending GET\n");
  206. XSTRNCPY(tx_buf, TX_MSG, TX_MSG_SIZE);
  207. if (wolfSSL_write(ssl, tx_buf, TX_MSG_SIZE) != TX_MSG_SIZE) {
  208. error = wolfSSL_get_error(ssl, 0);
  209. printf("ERROR: wolfSSL_write() failed, err = %d\n", error);
  210. closesocket(sock);
  211. wolfSSL_free(ssl);
  212. wolfSSL_CTX_free(ctx);
  213. return;
  214. }
  215. do {
  216. error = 0; /* reset error */
  217. ret = wolfSSL_read(ssl, rx_buf, RX_BUF_SIZE - 1);
  218. if (ret < 0) {
  219. error = wolfSSL_get_error(ssl, 0);
  220. if (error != SSL_ERROR_WANT_READ) {
  221. printf("wolfSSL_read failed, error = %d\n", error);
  222. closesocket(sock);
  223. wolfSSL_free(ssl);
  224. wolfSSL_CTX_free(ctx);
  225. return;
  226. }
  227. /* goToSleep() for 1 second*/
  228. } else if (ret > 0) {
  229. rx_buf[ret] = 0;
  230. printf("%s\n", rx_buf);
  231. }
  232. } while (error == SSL_ERROR_WANT_READ);
  233. wolfSSL_shutdown(ssl);
  234. wolfSSL_free(ssl);
  235. wolfSSL_CTX_free(ctx);
  236. wolfSSL_Cleanup();
  237. closesocket(sock);
  238. return;
  239. }
  240. #endif /* NO_WOLFSSL_CLIENT */
  241. #if !defined(NO_WOLFSSL_SERVER)
  242. #define TLS_SERVER_PORT 11111
  243. #define TX_BUF_SIZE 64
  244. #define RX_BUF_SIZE 1024
  245. #define TCP_SERVER_CONN_Q_SIZE 1
  246. /* derived from wolfSSL/certs/server-ecc.der */
  247. static const unsigned char server_ecc_der_256[] = { 0x30, 0x82, 0x03, 0x10,
  248. 0x30, 0x82, 0x02, 0xB5, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
  249. 0xEF, 0x46, 0xC7, 0xA4, 0x9B, 0xBB, 0x60, 0xD3, 0x30, 0x0A, 0x06, 0x08,
  250. 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30, 0x81, 0x8F, 0x31,
  251. 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  252. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A, 0x57,
  253. 0x61, 0x73, 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31, 0x10, 0x30,
  254. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65, 0x61, 0x74,
  255. 0x74, 0x6C, 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x0A,
  256. 0x0C, 0x07, 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31, 0x0C, 0x30,
  257. 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43, 0x43, 0x31,
  258. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  259. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  260. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  261. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  262. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E,
  263. 0x17, 0x0D, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31, 0x32, 0x30, 0x30, 0x37,
  264. 0x33, 0x38, 0x5A, 0x17, 0x0D, 0x31, 0x39, 0x30, 0x35, 0x30, 0x38, 0x32,
  265. 0x30, 0x30, 0x37, 0x33, 0x38, 0x5A, 0x30, 0x81, 0x8F, 0x31, 0x0B, 0x30,
  266. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  267. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A, 0x57, 0x61, 0x73,
  268. 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31, 0x10, 0x30, 0x0E, 0x06,
  269. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6C,
  270. 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x07,
  271. 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31, 0x0C, 0x30, 0x0A, 0x06,
  272. 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43, 0x43, 0x31, 0x18, 0x30,
  273. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E,
  274. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31,
  275. 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
  276. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C,
  277. 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13,
  278. 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A,
  279. 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xBB,
  280. 0x33, 0xAC, 0x4C, 0x27, 0x50, 0x4A, 0xC6, 0x4A, 0xA5, 0x04, 0xC3, 0x3C,
  281. 0xDE, 0x9F, 0x36, 0xDB, 0x72, 0x2D, 0xCE, 0x94, 0xEA, 0x2B, 0xFA, 0xCB,
  282. 0x20, 0x09, 0x39, 0x2C, 0x16, 0xE8, 0x61, 0x02, 0xE9, 0xAF, 0x4D, 0xD3,
  283. 0x02, 0x93, 0x9A, 0x31, 0x5B, 0x97, 0x92, 0x21, 0x7F, 0xF0, 0xCF, 0x18,
  284. 0xDA, 0x91, 0x11, 0x02, 0x34, 0x86, 0xE8, 0x20, 0x58, 0x33, 0x0B, 0x80,
  285. 0x34, 0x89, 0xD8, 0xA3, 0x81, 0xF7, 0x30, 0x81, 0xF4, 0x30, 0x1D, 0x06,
  286. 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x5D, 0x5D, 0x26, 0xEF,
  287. 0xAC, 0x7E, 0x36, 0xF9, 0x9B, 0x76, 0x15, 0x2B, 0x4A, 0x25, 0x02, 0x23,
  288. 0xEF, 0xB2, 0x89, 0x30, 0x30, 0x81, 0xC4, 0x06, 0x03, 0x55, 0x1D, 0x23,
  289. 0x04, 0x81, 0xBC, 0x30, 0x81, 0xB9, 0x80, 0x14, 0x5D, 0x5D, 0x26, 0xEF,
  290. 0xAC, 0x7E, 0x36, 0xF9, 0x9B, 0x76, 0x15, 0x2B, 0x4A, 0x25, 0x02, 0x23,
  291. 0xEF, 0xB2, 0x89, 0x30, 0xA1, 0x81, 0x95, 0xA4, 0x81, 0x92, 0x30, 0x81,
  292. 0x8F, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  293. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C,
  294. 0x0A, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6E, 0x67, 0x74, 0x6F, 0x6E, 0x31,
  295. 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x53, 0x65,
  296. 0x61, 0x74, 0x74, 0x6C, 0x65, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55,
  297. 0x04, 0x0A, 0x0C, 0x07, 0x45, 0x6C, 0x69, 0x70, 0x74, 0x69, 0x63, 0x31,
  298. 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x45, 0x43,
  299. 0x43, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F,
  300. 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  301. 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48,
  302. 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F,
  303. 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  304. 0x82, 0x09, 0x00, 0xEF, 0x46, 0xC7, 0xA4, 0x9B, 0xBB, 0x60, 0xD3, 0x30,
  305. 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01,
  306. 0xFF, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03,
  307. 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xF1, 0xD0, 0xA6,
  308. 0x3E, 0x83, 0x33, 0x24, 0xD1, 0x7A, 0x05, 0x5F, 0x1E, 0x0E, 0xBD, 0x7D,
  309. 0x6B, 0x33, 0xE9, 0xF2, 0x86, 0xF3, 0xF3, 0x3D, 0xA9, 0xEF, 0x6A, 0x87,
  310. 0x31, 0xB3, 0xB7, 0x7E, 0x50, 0x02, 0x21, 0x00, 0xF0, 0x60, 0xDD, 0xCE,
  311. 0xA2, 0xDB, 0x56, 0xEC, 0xD9, 0xF4, 0xE4, 0xE3, 0x25, 0xD4, 0xB0, 0xC9,
  312. 0x25, 0x7D, 0xCA, 0x7A, 0x5D, 0xBA, 0xC4, 0xB2, 0xF6, 0x7D, 0x04, 0xC7,
  313. 0xBD, 0x62, 0xC9, 0x20 };
  314. /* derived from wolfSSL/certs/ecc-key.der */
  315. static const unsigned char ecc_key_der_256[] = { 0x30, 0x77, 0x02, 0x01, 0x01,
  316. 0x04, 0x20, 0x45, 0xB6, 0x69, 0x02, 0x73, 0x9C, 0x6C, 0x85, 0xA1, 0x38,
  317. 0x5B, 0x72, 0xE8, 0xE8, 0xC7, 0xAC, 0xC4, 0x03, 0x8D, 0x53, 0x35, 0x04,
  318. 0xFA, 0x6C, 0x28, 0xDC, 0x34, 0x8D, 0xE1, 0xA8, 0x09, 0x8C, 0xA0, 0x0A,
  319. 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0xA1, 0x44,
  320. 0x03, 0x42, 0x00, 0x04, 0xBB, 0x33, 0xAC, 0x4C, 0x27, 0x50, 0x4A, 0xC6,
  321. 0x4A, 0xA5, 0x04, 0xC3, 0x3C, 0xDE, 0x9F, 0x36, 0xDB, 0x72, 0x2D, 0xCE,
  322. 0x94, 0xEA, 0x2B, 0xFA, 0xCB, 0x20, 0x09, 0x39, 0x2C, 0x16, 0xE8, 0x61,
  323. 0x02, 0xE9, 0xAF, 0x4D, 0xD3, 0x02, 0x93, 0x9A, 0x31, 0x5B, 0x97, 0x92,
  324. 0x21, 0x7F, 0xF0, 0xCF, 0x18, 0xDA, 0x91, 0x11, 0x02, 0x34, 0x86, 0xE8,
  325. 0x20, 0x58, 0x33, 0x0B, 0x80, 0x34, 0x89, 0xD8 };
  326. void wolfssl_server_test(uintData_t statusPtr)
  327. {
  328. int sock_listen;
  329. int bindStatus;
  330. int sock_req;
  331. sockaddr_in socketAddr;
  332. sockaddr_in server_addr;
  333. int socketAddrLen=sizeof(sockaddr);
  334. char rx_buf[RX_BUF_SIZE];
  335. char tx_buf[TX_BUF_SIZE];
  336. clientConnectionHandleType TCPserverHandle;
  337. WOLFSSL * ssl;
  338. WOLFSSL_CTX * ctx;
  339. int tx_buf_sz = 0, ret = 0, error = 0;
  340. /* set up the mailbox transport */
  341. /* connectionId2 is defined in the mailbox-transport.config*/
  342. if (setupTransport(&TCPserverHandle, (char*)"connectionId2") != transportSuccess){
  343. printf("TCP transport set up failed \n");
  344. return;
  345. }
  346. /* SET UP NETWORK SOCKET */
  347. printf("Opening network socket...\n");
  348. sock_listen = socket(AF_INET, SOCK_STREAM, 0);
  349. if (sock_listen == SOCKET_ERROR) {
  350. printf("ERROR: socket, err = %d\n", errno);
  351. return;
  352. }
  353. printf("Clearing memory for server_addr struct\n");
  354. XMEMSET((char *) &server_addr, 0u, sizeof(server_addr));
  355. printf("Setting up server_addr struct\n");
  356. server_addr.sin_family = AF_INET;
  357. server_addr.sin_addr.s_addr = INADDR_ANY;
  358. server_addr.sin_port = htons(TLS_SERVER_PORT);
  359. bindStatus = bind(sock_listen, (sockaddr *) &server_addr, sizeof(server_addr));
  360. if (bindStatus == SOCKET_ERROR) {
  361. printf("ERROR: bind, err = %d\n", errno);
  362. closesocket(sock_listen);
  363. return;
  364. }
  365. /* wolfSSL INIT and CTX SETUP */
  366. wolfSSL_Init();
  367. /* chooses the highest possible TLS version */
  368. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  369. if (ctx == 0) {
  370. printf("ERROR: wolfSSL_CTX_new failed\n");
  371. closesocket(sock_listen);
  372. return;
  373. }
  374. WOLFSSL_MSG("wolfSSL_CTX_new done");
  375. ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  376. server_ecc_der_256,
  377. sizeof(server_ecc_der_256),
  378. SSL_FILETYPE_ASN1);
  379. if (ret != SSL_SUCCESS) {
  380. printf("ERROR: wolfSSL_CTX_use_certificate_buffer() failed, \
  381. err = %d\n", ret);
  382. closesocket(sock_listen);
  383. wolfSSL_CTX_free(ctx);
  384. return;
  385. }
  386. ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx,
  387. ecc_key_der_256,
  388. sizeof(ecc_key_der_256),
  389. SSL_FILETYPE_ASN1);
  390. if (ret != SSL_SUCCESS) {
  391. printf("ERROR: wolfSSL_CTX_use_PrivateKey_buffer() failed\n");
  392. closesocket(sock_listen);
  393. wolfSSL_CTX_free(ctx);
  394. return;
  395. }
  396. /* accept client socket connections */
  397. printf("Listening for client connection\n");
  398. printf("E.g, you can use ./examples/client/client.exe -h 192.168.219.100\n");
  399. printf(" \n");
  400. listen(sock_listen, TCP_SERVER_CONN_Q_SIZE);
  401. sock_req = accept(sock_listen,
  402. (sockaddr *) &socketAddr,
  403. &socketAddrLen);
  404. if (sock_req == -1) {
  405. printf("ERROR: accept, err = %d\n", errno);
  406. closesocket(sock_listen);
  407. return;
  408. }
  409. printf("Got client connection! Starting TLS negotiation\n");
  410. #ifdef DEBUG_WOLFSSL
  411. wolfSSL_Debugging_ON();
  412. #endif
  413. /* set up wolfSSL session */
  414. ssl = wolfSSL_new(ctx);
  415. if (ssl == NULL) {
  416. printf("ERROR: wolfSSL_new() failed\n");
  417. closesocket(sock_req);
  418. closesocket(sock_listen);
  419. wolfSSL_CTX_free(ctx);
  420. return;
  421. }
  422. WOLFSSL_MSG("wolfSSL_new done");
  423. ret = wolfSSL_set_fd(ssl, sock_req);
  424. if (ret != SSL_SUCCESS) {
  425. printf("ERROR: wolfSSL_set_fd() failed\n");
  426. closesocket(sock_req);
  427. closesocket(sock_listen);
  428. wolfSSL_free(ssl);
  429. wolfSSL_CTX_free(ctx);
  430. return;
  431. }
  432. WOLFSSL_MSG("wolfSSL_set_fd done");
  433. do {
  434. error = 0; /* reset error */
  435. if (ret != SSL_SUCCESS) {
  436. error = wolfSSL_get_error(ssl, 0);
  437. printf("ERROR: wolfSSL_accept() failed, err = %d\n", error);
  438. if (error != SSL_ERROR_WANT_READ) {
  439. closesocket(sock_req);
  440. closesocket(sock_listen);
  441. wolfSSL_free(ssl);
  442. wolfSSL_CTX_free(ctx);
  443. return;
  444. }
  445. /* goToSleep() for 500 milliseconds */
  446. }
  447. } while ((ret != SSL_SUCCESS) && (error == SSL_ERROR_WANT_READ));
  448. printf("wolfSSL_accept() ok...\n");
  449. /* read client data */
  450. error = 0;
  451. XMEMSET(rx_buf, 0u, RX_BUF_SIZE);
  452. ret = wolfSSL_read(ssl, rx_buf, RX_BUF_SIZE - 1);
  453. if (ret < 0) {
  454. error = wolfSSL_get_error(ssl, 0);
  455. if (error != SSL_ERROR_WANT_READ) {
  456. printf("wolfSSL_read failed, error = %d\n", error);
  457. closesocket(sock_req);
  458. closesocket(sock_listen);
  459. wolfSSL_free(ssl);
  460. wolfSSL_CTX_free(ctx);
  461. return;
  462. }
  463. }
  464. printf("AFTER wolfSSL_read() call, ret = %d\n", ret);
  465. if (ret > 0) {
  466. rx_buf[ret] = 0;
  467. printf("Client sent: %s\n", rx_buf);
  468. }
  469. /* write response to client */
  470. XMEMSET(tx_buf, 0u, TX_BUF_SIZE);
  471. tx_buf_sz = 22;
  472. XSTRNCPY(tx_buf, "I hear ya fa shizzle!\n", tx_buf_sz);
  473. if (wolfSSL_write(ssl, tx_buf, tx_buf_sz) != tx_buf_sz) {
  474. error = wolfSSL_get_error(ssl, 0);
  475. printf("ERROR: wolfSSL_write() failed, err = %d\n", error);
  476. closesocket(sock_req);
  477. closesocket(sock_listen);
  478. wolfSSL_free(ssl);
  479. wolfSSL_CTX_free(ctx);
  480. return;
  481. }
  482. ret = wolfSSL_shutdown(ssl);
  483. if (ret == SSL_SHUTDOWN_NOT_DONE)
  484. wolfSSL_shutdown(ssl);
  485. wolfSSL_free(ssl);
  486. wolfSSL_CTX_free(ctx);
  487. wolfSSL_Cleanup();
  488. closesocket(sock_req);
  489. closesocket(sock_listen);
  490. return;
  491. }
  492. #endif /* NO_WOLFSSL_SERVER */
  493. int wolfsslRunTests (void)
  494. {
  495. thread_handle_t TCPhandle;
  496. threadStatus ts;
  497. #if !defined(NO_CRYPT_TEST)
  498. wolfcrypt_test(NULL);
  499. #endif
  500. #if !defined(NO_CRYPT_BENCHMARK)
  501. benchmark_test(NULL);
  502. #endif
  503. #if !defined(NO_WOLFSSL_CLIENT)
  504. ts = createThread("TCPclient", "TCPThreadTemplate", wolfssl_client_test,
  505. 0, &TCPhandle );
  506. if (ts != threadSuccess) {
  507. printf("Unable to create TCP client thread, %i ", (size_t)ts);
  508. }
  509. #endif
  510. #if !defined(NO_WOLFSSL_SERVER)
  511. ts = createThread("TCPserver", "TCPThreadTemplate", wolfssl_server_test,
  512. 0, &TCPhandle );
  513. if (ts != threadSuccess) {
  514. printf("Unable to create TCP server thread, %i ", (size_t)ts);
  515. }
  516. #endif
  517. return 0;
  518. }