2
0

sctp-client-dtls.c 3.6 KB

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