wolfssl_client.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* wolfssl_client.ino
  2. *
  3. * Copyright (C) 2006-2022 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.h>
  22. #include <wolfssl/ssl.h>
  23. #include <Ethernet.h>
  24. const char host[] = "192.168.1.148"; /* server to connect to */
  25. const int port = 11111; /* port on server to connect to */
  26. int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx);
  27. int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx);
  28. int reconnect = 10;
  29. EthernetClient client;
  30. WOLFSSL_CTX* ctx = NULL;
  31. WOLFSSL* ssl = NULL;
  32. void setup() {
  33. WOLFSSL_METHOD* method;
  34. Serial.begin(9600);
  35. method = wolfTLSv1_2_client_method();
  36. if (method == NULL) {
  37. Serial.println("unable to get method");
  38. return;
  39. }
  40. ctx = wolfSSL_CTX_new(method);
  41. if (ctx == NULL) {
  42. Serial.println("unable to get ctx");
  43. return;
  44. }
  45. /* initialize wolfSSL using callback functions */
  46. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
  47. wolfSSL_SetIOSend(ctx, EthernetSend);
  48. wolfSSL_SetIORecv(ctx, EthernetReceive);
  49. return;
  50. }
  51. int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx) {
  52. int sent = 0;
  53. sent = client.write((byte*)msg, sz);
  54. return sent;
  55. }
  56. int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx) {
  57. int ret = 0;
  58. while (client.available() > 0 && ret < sz) {
  59. reply[ret++] = client.read();
  60. }
  61. return ret;
  62. }
  63. void loop() {
  64. int err = 0;
  65. int input = 0;
  66. int total_input = 0;
  67. char msg[32] = "hello wolfssl!";
  68. int msgSz = (int)strlen(msg);
  69. char errBuf[80];
  70. char reply[80];
  71. const char* cipherName;
  72. if (reconnect) {
  73. reconnect--;
  74. if (client.connect(host, port)) {
  75. Serial.print("Connected to ");
  76. Serial.println(host);
  77. ssl = wolfSSL_new(ctx);
  78. if (ssl == NULL) {
  79. Serial.println("Unable to allocate SSL object");
  80. return;
  81. }
  82. err = wolfSSL_connect(ssl);
  83. if (err != WOLFSSL_SUCCESS) {
  84. err = wolfSSL_get_error(ssl, 0);
  85. wolfSSL_ERR_error_string(err, errBuf);
  86. Serial.print("TLS Connect Error: ");
  87. Serial.println(errBuf);
  88. }
  89. Serial.print("SSL version is ");
  90. Serial.println(wolfSSL_get_version(ssl));
  91. cipherName = wolfSSL_get_cipher(ssl);
  92. Serial.print("SSL cipher suite is ");
  93. Serial.println(cipherName);
  94. if ((wolfSSL_write(ssl, msg, msgSz)) == msgSz) {
  95. Serial.print("Server response: ");
  96. /* wait for data */
  97. while (!client.available()) {}
  98. /* read data */
  99. while (wolfSSL_pending(ssl)) {
  100. input = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
  101. total_input += input;
  102. if (input < 0) {
  103. err = wolfSSL_get_error(ssl, 0);
  104. wolfSSL_ERR_error_string(err, errBuf);
  105. Serial.print("TLS Read Error: ");
  106. Serial.println(errBuf);
  107. break;
  108. } else if (input > 0) {
  109. reply[input] = '\0';
  110. Serial.print(reply);
  111. } else {
  112. Serial.println();
  113. }
  114. }
  115. } else {
  116. err = wolfSSL_get_error(ssl, 0);
  117. wolfSSL_ERR_error_string(err, errBuf);
  118. Serial.print("TLS Write Error: ");
  119. Serial.println(errBuf);
  120. }
  121. wolfSSL_shutdown(ssl);
  122. wolfSSL_free(ssl);
  123. client.stop();
  124. Serial.println("Connection complete.");
  125. reconnect = 0;
  126. } else {
  127. Serial.println("Trying to reconnect...");
  128. }
  129. }
  130. delay(1000);
  131. }