wolfssl_server.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* wolfssl_server.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. #include <wolfssl.h>
  22. #include <wolfssl/ssl.h>
  23. #include <Ethernet.h>
  24. #define USE_CERT_BUFFERS_256
  25. #include <wolfssl/certs_test.h>
  26. #ifdef NO_WOLFSSL_SERVER
  27. #error Please undefine NO_WOLFSSL_SERVER for this example
  28. #endif
  29. const int port = 11111; /* port to listen on */
  30. int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx);
  31. int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx);
  32. EthernetServer server(port);
  33. EthernetClient client;
  34. WOLFSSL_CTX* ctx = NULL;
  35. WOLFSSL* ssl = NULL;
  36. void setup() {
  37. int err;
  38. WOLFSSL_METHOD* method;
  39. Serial.begin(9600);
  40. method = wolfTLSv1_2_server_method();
  41. if (method == NULL) {
  42. Serial.println("unable to get method");
  43. return;
  44. }
  45. ctx = wolfSSL_CTX_new(method);
  46. if (ctx == NULL) {
  47. Serial.println("unable to get ctx");
  48. return;
  49. }
  50. /* initialize wolfSSL using callback functions */
  51. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
  52. wolfSSL_SetIOSend(ctx, EthernetSend);
  53. wolfSSL_SetIORecv(ctx, EthernetReceive);
  54. /* setup the private key and certificate */
  55. err = wolfSSL_CTX_use_PrivateKey_buffer(ctx, ecc_key_der_256,
  56. sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
  57. if (err != WOLFSSL_SUCCESS) {
  58. Serial.println("error setting key");
  59. return;
  60. }
  61. err = wolfSSL_CTX_use_certificate_buffer(ctx, serv_ecc_der_256,
  62. sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
  63. if (err != WOLFSSL_SUCCESS) {
  64. Serial.println("error setting certificate");
  65. return;
  66. }
  67. /* Start the server */
  68. server.begin();
  69. return;
  70. }
  71. int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx) {
  72. int sent = 0;
  73. sent = client.write((byte*)msg, sz);
  74. return sent;
  75. }
  76. int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx) {
  77. int ret = 0;
  78. while (client.available() > 0 && ret < sz) {
  79. reply[ret++] = client.read();
  80. }
  81. return ret;
  82. }
  83. void loop() {
  84. int err = 0;
  85. int input = 0;
  86. char errBuf[80];
  87. char reply[80];
  88. int replySz = 0;
  89. const char* cipherName;
  90. /* Listen for incoming client requests. */
  91. client = server.available();
  92. if (!client) {
  93. return;
  94. }
  95. if (client.connected()) {
  96. Serial.println("Client connected");
  97. ssl = wolfSSL_new(ctx);
  98. if (ssl == NULL) {
  99. Serial.println("Unable to allocate SSL object");
  100. return;
  101. }
  102. err = wolfSSL_accept(ssl);
  103. if (err != WOLFSSL_SUCCESS) {
  104. err = wolfSSL_get_error(ssl, 0);
  105. wolfSSL_ERR_error_string(err, errBuf);
  106. Serial.print("TLS Accept Error: ");
  107. Serial.println(errBuf);
  108. }
  109. Serial.print("SSL version is ");
  110. Serial.println(wolfSSL_get_version(ssl));
  111. cipherName = wolfSSL_get_cipher(ssl);
  112. Serial.print("SSL cipher suite is ");
  113. Serial.println(cipherName);
  114. Serial.print("Server Read: ");
  115. /* wait for data */
  116. while (!client.available()) {}
  117. /* read data */
  118. while (wolfSSL_pending(ssl)) {
  119. input = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
  120. if (input < 0) {
  121. err = wolfSSL_get_error(ssl, 0);
  122. wolfSSL_ERR_error_string(err, errBuf);
  123. Serial.print("TLS Read Error: ");
  124. Serial.println(errBuf);
  125. break;
  126. } else if (input > 0) {
  127. replySz = input;
  128. reply[input] = '\0';
  129. Serial.print(reply);
  130. } else {
  131. Serial.println();
  132. }
  133. }
  134. /* echo data */
  135. if ((wolfSSL_write(ssl, reply, replySz)) != replySz) {
  136. err = wolfSSL_get_error(ssl, 0);
  137. wolfSSL_ERR_error_string(err, errBuf);
  138. Serial.print("TLS Write Error: ");
  139. Serial.println(errBuf);
  140. }
  141. wolfSSL_shutdown(ssl);
  142. wolfSSL_free(ssl);
  143. }
  144. client.stop();
  145. Serial.println("Connection complete");
  146. }