wolfssl_client.ino 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* wolfssl_client.ino
  2. *
  3. * Copyright (C) 2006-2023 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. This was original tested with Intel Galileo acting as the Client, with a
  23. laptop acting as a server using the server example provided in examples/server.
  24. Legacy Ardunio v1.86 was used to compile and program the Galileo
  25. */
  26. #define USE_CERT_BUFFERS_2048
  27. #include <wolfssl.h>
  28. #include <wolfssl/ssl.h>
  29. #include <Ethernet.h>
  30. #include <wolfssl/certs_test.h>
  31. const char host[] = "192.168.1.148"; /* server to connect to */
  32. const int port = 11111; /* port on server to connect to */
  33. int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx);
  34. int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx);
  35. int reconnect = 10;
  36. EthernetClient client;
  37. WOLFSSL_CTX* ctx = NULL;
  38. WOLFSSL* ssl = NULL;
  39. void setup() {
  40. WOLFSSL_METHOD* method;
  41. /* Initialize Return Code */
  42. int rc;
  43. Serial.begin(9600);
  44. /* Delay need to ensure connection to server */
  45. delay(4000);
  46. method = wolfTLSv1_2_client_method();
  47. if (method == NULL) {
  48. Serial.println("unable to get method");
  49. return;
  50. }
  51. ctx = wolfSSL_CTX_new(method);
  52. if (ctx == NULL) {
  53. Serial.println("unable to get ctx");
  54. return;
  55. }
  56. /* initialize wolfSSL using callback functions */
  57. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  58. rc = wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048,\
  59. sizeof_ca_cert_der_2048,\
  60. WOLFSSL_FILETYPE_ASN1);
  61. Serial.print("\n\n Return code of load_verify is:");
  62. Serial.println(rc);
  63. Serial.println("");
  64. rc = wolfSSL_CTX_use_certificate_buffer(ctx, client_cert_der_2048,\
  65. sizeof_client_cert_der_2048,\
  66. WOLFSSL_FILETYPE_ASN1);
  67. Serial.print("\n\n Return code of use_certificate_buffer is:");
  68. Serial.println(rc);
  69. Serial.println("");
  70. rc = wolfSSL_CTX_use_PrivateKey_buffer(ctx, client_key_der_2048,\
  71. sizeof_client_key_der_2048,\
  72. WOLFSSL_FILETYPE_ASN1);
  73. Serial.print("\n\n Return code of use_PrivateKey_buffer is:");
  74. Serial.println(rc);
  75. Serial.println("");
  76. wolfSSL_SetIOSend(ctx, EthernetSend);
  77. wolfSSL_SetIORecv(ctx, EthernetReceive);
  78. return;
  79. }
  80. int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx) {
  81. int sent = 0;
  82. sent = client.write((byte*)msg, sz);
  83. return sent;
  84. }
  85. int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx) {
  86. int ret = 0;
  87. while (client.available() > 0 && ret < sz) {
  88. reply[ret++] = client.read();
  89. }
  90. return ret;
  91. }
  92. void loop() {
  93. int err = 0;
  94. int input = 0;
  95. int total_input = 0;
  96. char msg[32] = "hello wolfssl!";
  97. int msgSz = (int)strlen(msg);
  98. char errBuf[80];
  99. char reply[80];
  100. const char* cipherName;
  101. if (reconnect) {
  102. reconnect--;
  103. if (client.connect(host, port)) {
  104. Serial.print("Connected to ");
  105. Serial.println(host);
  106. ssl = wolfSSL_new(ctx);
  107. if (ssl == NULL) {
  108. Serial.println("Unable to allocate SSL object");
  109. return;
  110. }
  111. err = wolfSSL_connect(ssl);
  112. if (err != WOLFSSL_SUCCESS) {
  113. err = wolfSSL_get_error(ssl, 0);
  114. wolfSSL_ERR_error_string(err, errBuf);
  115. Serial.print("TLS Connect Error: ");
  116. Serial.println(errBuf);
  117. }
  118. Serial.print("SSL version is ");
  119. Serial.println(wolfSSL_get_version(ssl));
  120. cipherName = wolfSSL_get_cipher(ssl);
  121. Serial.print("SSL cipher suite is ");
  122. Serial.println(cipherName);
  123. if ((wolfSSL_write(ssl, msg, msgSz)) == msgSz) {
  124. Serial.print("Server response: ");
  125. /* wait for data */
  126. while (!client.available()) {}
  127. /* read data */
  128. while (wolfSSL_pending(ssl)) {
  129. input = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
  130. total_input += input;
  131. if (input < 0) {
  132. err = wolfSSL_get_error(ssl, 0);
  133. wolfSSL_ERR_error_string(err, errBuf);
  134. Serial.print("TLS Read Error: ");
  135. Serial.println(errBuf);
  136. break;
  137. }
  138. else if (input > 0) {
  139. reply[input] = '\0';
  140. Serial.print(reply);
  141. }
  142. else {
  143. Serial.println();
  144. }
  145. }
  146. }
  147. else {
  148. err = wolfSSL_get_error(ssl, 0);
  149. wolfSSL_ERR_error_string(err, errBuf);
  150. Serial.print("TLS Write Error: ");
  151. Serial.println(errBuf);
  152. }
  153. wolfSSL_shutdown(ssl);
  154. wolfSSL_free(ssl);
  155. client.stop();
  156. Serial.println("Connection complete.");
  157. reconnect = 0;
  158. }
  159. else {
  160. Serial.println("Trying to reconnect...");
  161. }
  162. }
  163. delay(1000);
  164. }