2
0

sctp-client-dtls.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* sctp-client-dtls.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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. /* wolfssl */
  25. #ifndef WOLFSSL_USER_SETTINGS
  26. #include <wolfssl/options.h>
  27. #endif
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #include <wolfssl/ssl.h>
  30. #if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  31. /* sctp */
  32. #include <sys/socket.h>
  33. #include <sys/types.h>
  34. #include <arpa/inet.h>
  35. #include <netinet/in.h>
  36. /* std */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #define cacert "./certs/ca-cert.pem"
  42. static int err_sys(const char* msg)
  43. {
  44. perror(msg);
  45. exit(EXIT_FAILURE);
  46. }
  47. #endif /* WOLFSSL_SCTP && WOLFSSL_DTLS && !WOLFSSL_NO_TLS12 */
  48. int main(int argc, char **argv)
  49. {
  50. (void)argc;
  51. (void)argv;
  52. #if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53. int sd = socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
  54. if (sd < 0)
  55. err_sys("sctp socket error");
  56. struct sockaddr_in sa;
  57. memset(&sa, 0, sizeof(sa));
  58. sa.sin_family = AF_INET;
  59. sa.sin_addr.s_addr = inet_addr("127.0.0.1");
  60. sa.sin_port = htons(12345);
  61. int ret = connect(sd, (struct sockaddr*)&sa, sizeof(sa));
  62. if (ret < 0)
  63. err_sys("sctp connect error");
  64. const char* response = "hello there";
  65. char buffer[80];
  66. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  67. if (ctx == NULL)
  68. err_sys("ctx new dtls client failed");
  69. ret = wolfSSL_CTX_dtls_set_sctp(ctx);
  70. if (ret != WOLFSSL_SUCCESS)
  71. err_sys("set sctp mode failed");
  72. ret = wolfSSL_CTX_load_verify_locations(ctx, cacert, NULL);
  73. if (ret != WOLFSSL_SUCCESS)
  74. err_sys("ca cert error");
  75. WOLFSSL* ssl = wolfSSL_new(ctx);
  76. if (ssl == NULL)
  77. err_sys("ssl new dtls client failed");
  78. wolfSSL_set_fd(ssl, sd);
  79. ret = wolfSSL_connect(ssl);
  80. if (ret != WOLFSSL_SUCCESS)
  81. err_sys("ssl connect failed");
  82. printf("TLS version is %s\n", wolfSSL_get_version(ssl));
  83. printf("Cipher Suite is %s\n",
  84. wolfSSL_CIPHER_get_name(wolfSSL_get_current_cipher(ssl)));
  85. wolfSSL_write(ssl, response, (int)strlen(response));
  86. int got = wolfSSL_read(ssl, buffer, sizeof(buffer));
  87. if (got > 0) {
  88. buffer[got] = 0;
  89. printf("server said: %s\n", buffer);
  90. }
  91. unsigned char bigBuf[4096];
  92. unsigned int i;
  93. for (i = 0; i < (int)sizeof(bigBuf); i++)
  94. bigBuf[i] = (unsigned char)(i & 0xFF);
  95. wolfSSL_write(ssl, bigBuf, sizeof(bigBuf));
  96. memset(bigBuf, 0, sizeof(bigBuf));
  97. wolfSSL_read(ssl, bigBuf, sizeof(bigBuf));
  98. for (i = 0; i < sizeof(bigBuf); i++) {
  99. if (bigBuf[i] != (unsigned char)(i & 0xFF)) {
  100. fprintf(stderr, "big message check fail\n");
  101. break;
  102. }
  103. }
  104. wolfSSL_shutdown(ssl);
  105. wolfSSL_free(ssl);
  106. wolfSSL_CTX_free(ctx);
  107. close(sd);
  108. #endif /* WOLFSSL_SCTP && WOLFSSL_DTLS && !WOLFSSL_NO_TLS12 */
  109. return 0;
  110. }