sctp-server-dtls.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* sctp-server-dtls.c
  2. *
  3. * Copyright (C) 2006-2021 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. /* wolfssl */
  22. #ifndef WOLFSSL_USER_SETTINGS
  23. #include <wolfssl/options.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #include <wolfssl/ssl.h>
  27. #if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS)
  28. /* sctp */
  29. #include <sys/socket.h>
  30. #include <sys/types.h>
  31. #include <netinet/in.h>
  32. /* std */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #define key "./certs/server-key.pem"
  38. #define cert "./certs/server-cert.pem"
  39. static int err_sys(const char* msg)
  40. {
  41. perror(msg);
  42. exit(EXIT_FAILURE);
  43. }
  44. #endif /* WOLFSSL_SCTP && WOLFSSL_DTLS */
  45. int main()
  46. {
  47. #if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS)
  48. int sd = socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
  49. if (sd < 0)
  50. err_sys("sctp socket error");
  51. struct sockaddr_in sa;
  52. memset(&sa, 0, sizeof(sa));
  53. sa.sin_family = AF_INET;
  54. sa.sin_addr.s_addr = htonl(INADDR_ANY);
  55. sa.sin_port = htons(12345);
  56. int ret = bind(sd, (struct sockaddr*)&sa, sizeof(sa));
  57. if (ret < 0)
  58. err_sys("sctp bind error");
  59. listen(sd, 3);
  60. int client_sd = accept(sd, NULL, NULL);
  61. if (client_sd < 0)
  62. err_sys("sctp accept error");
  63. const char* response = "well hello to you";
  64. char buffer[80];
  65. #ifdef WC_RNG_SEED_CB
  66. wc_SetSeed_Cb(wc_GenerateSeed);
  67. #endif
  68. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  69. if (ctx == NULL)
  70. err_sys("ctx new dtls server failed");
  71. ret = wolfSSL_CTX_dtls_set_sctp(ctx);
  72. if (ret != WOLFSSL_SUCCESS)
  73. err_sys("set sctp mode failed");
  74. ret = wolfSSL_CTX_use_PrivateKey_file(ctx, key, WOLFSSL_FILETYPE_PEM);
  75. if (ret != WOLFSSL_SUCCESS)
  76. err_sys("use private key error");
  77. ret = wolfSSL_CTX_use_certificate_file(ctx, cert, WOLFSSL_FILETYPE_PEM);
  78. if (ret != WOLFSSL_SUCCESS)
  79. err_sys("use cert error");
  80. WOLFSSL* ssl = wolfSSL_new(ctx);
  81. if (ssl == NULL)
  82. err_sys("ssl new dtls server failed");
  83. wolfSSL_set_fd(ssl, client_sd);
  84. ret = wolfSSL_accept(ssl);
  85. if (ret != WOLFSSL_SUCCESS)
  86. err_sys("ssl accept failed");
  87. printf("TLS version is %s\n", wolfSSL_get_version(ssl));
  88. printf("Cipher Suite is %s\n",
  89. wolfSSL_CIPHER_get_name(wolfSSL_get_current_cipher(ssl)));
  90. int got = wolfSSL_read(ssl, buffer, sizeof(buffer));
  91. if (got > 0) {
  92. buffer[got] = 0;
  93. printf("client said: %s\n", buffer);
  94. }
  95. wolfSSL_write(ssl, response, (int)strlen(response));
  96. unsigned char bigBuf[4096];
  97. wolfSSL_read(ssl, bigBuf, sizeof(bigBuf));
  98. wolfSSL_write(ssl, bigBuf, sizeof(bigBuf));
  99. wolfSSL_shutdown(ssl);
  100. wolfSSL_free(ssl);
  101. wolfSSL_CTX_free(ctx);
  102. close(sd);
  103. #endif /* WOLFSSL_SCTP && WOLFSSL_DTLS */
  104. return 0;
  105. }