wolf_server.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* wolf_server.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 <stdio.h>
  22. #include <string.h>
  23. #include "r_t4_itcpip.h"
  24. #include "wolfssl/wolfcrypt/settings.h"
  25. #include "wolfssl/ssl.h"
  26. #include "wolfssl/certs_test.h"
  27. #include "wolf_demo.h"
  28. static int my_IORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  29. {
  30. int ret;
  31. ID cepid;
  32. if(ctx != NULL)cepid = *(ID *)ctx;
  33. else return WOLFSSL_CBIO_ERR_GENERAL;
  34. ret = tcp_rcv_dat(cepid, buff, sz, TMO_FEVR);
  35. if(ret == sz)return ret;
  36. else return WOLFSSL_CBIO_ERR_GENERAL;
  37. }
  38. static int my_IOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  39. {
  40. int ret;
  41. ID cepid;
  42. if(ctx != NULL)cepid = *(ID *)ctx;
  43. else return WOLFSSL_CBIO_ERR_GENERAL;
  44. ret = tcp_snd_dat(cepid, buff, sz, TMO_FEVR);
  45. if(ret == sz)return ret;
  46. else return WOLFSSL_CBIO_ERR_GENERAL;
  47. }
  48. WOLFSSL_CTX *wolfSSL_TLS_server_init()
  49. {
  50. int ret;
  51. WOLFSSL_CTX* ctx;
  52. #ifndef NO_FILESYSTEM
  53. #ifdef USE_ECC_CERT
  54. char *cert = "./certs/server-ecc-cert.pem";
  55. char *key = "./certs/server-ecc-key.pem";
  56. #else
  57. char *cert = "./certs/server-cert.pem";
  58. char *key = "./certs/server-key.pem";
  59. #endif
  60. #else
  61. #ifdef USE_ECC_CERT
  62. char *cert = serv_ecc_der_256;
  63. int sizeof_cert = sizeof_serv_ecc_der_256;
  64. char *cert = serv_ecc_key_der_256;
  65. int sizeof_key = sizeof_serv_ecc_key_der_256;
  66. #else
  67. const unsigned char *cert = server_cert_der_2048;
  68. #define sizeof_cert sizeof_server_cert_der_2048
  69. const unsigned char *key = server_key_der_2048;
  70. #define sizeof_key sizeof_server_key_der_2048
  71. #endif
  72. #endif
  73. wolfSSL_Init();
  74. #ifdef DEBUG_WOLFSSL
  75. wolfSSL_Debugging_ON();
  76. #endif
  77. /* Create and initialize WOLFSSL_CTX */
  78. if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method_ex((void *)NULL))) == NULL) {
  79. printf("ERROR: failed to create WOLFSSL_CTX\n");
  80. return NULL;
  81. }
  82. #if !defined(NO_FILESYSTEM)
  83. ret = wolfSSL_CTX_use_certificate_file(ctx, cert, 0);
  84. #else
  85. ret = wolfSSL_CTX_use_certificate_buffer(ctx, cert, sizeof_cert, SSL_FILETYPE_ASN1);
  86. #endif
  87. if (ret != SSL_SUCCESS) {
  88. printf("Error %d loading server-cert!\n", ret);
  89. return NULL;
  90. }
  91. /* Load server key into WOLFSSL_CTX */
  92. #if !defined(NO_FILESYSTEM)
  93. ret = wolfSSL_CTX_use_PrivateKey_file(ctx, key, 0);
  94. #else
  95. ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, key, sizeof_key, SSL_FILETYPE_ASN1);
  96. #endif
  97. if (ret != SSL_SUCCESS) {
  98. printf("Error %d loading server-key!\n", ret);
  99. return NULL;
  100. }
  101. /* Register callbacks */
  102. wolfSSL_SetIORecv(ctx, my_IORecv);
  103. wolfSSL_SetIOSend(ctx, my_IOSend);
  104. return ctx;
  105. }
  106. void wolfSSL_TLS_server(void *v_ctx, func_args *args)
  107. {
  108. ID cepid = 1;
  109. ID repid = 1;
  110. ER ercd;
  111. WOLFSSL_CTX *ctx = (WOLFSSL_CTX *)v_ctx;
  112. (void) args;
  113. WOLFSSL *ssl;
  114. int len;
  115. #define BUFF_SIZE 256
  116. char buff[BUFF_SIZE];
  117. T_IPV4EP dst_addr = {0, 0};
  118. if((ercd = tcp_acp_cep(cepid, repid, &dst_addr, TMO_FEVR)) != E_OK) {
  119. printf("ERROR TCP Accept: %d\n", ercd);
  120. return;
  121. }
  122. if((ssl = wolfSSL_new(ctx)) == NULL) {
  123. printf("ERROR: failed wolfSSL_new\n");
  124. return;
  125. }
  126. wolfSSL_SetIOReadCtx(ssl, (void *)&cepid);
  127. wolfSSL_SetIOWriteCtx(ssl, (void *)&cepid);
  128. if (wolfSSL_accept(ssl) < 0) {
  129. printf("ERROR: SSL Accept(%d)\n", wolfSSL_get_error(ssl, 0));
  130. return;
  131. }
  132. if ((len = wolfSSL_read(ssl, buff, sizeof(buff) - 1)) < 0) {
  133. printf("ERROR: SSL Read(%d)\n", wolfSSL_get_error(ssl, 0));
  134. return;
  135. }
  136. buff[len] = '\0';
  137. printf("Received: %s\n", buff);
  138. if (wolfSSL_write(ssl, buff, len) != len) {
  139. printf("ERROR: SSL Write(%d)\n", wolfSSL_get_error(ssl, 0));
  140. return;
  141. }
  142. wolfSSL_free(ssl);
  143. tcp_sht_cep(cepid);
  144. }