tls_app_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * File : tls_app_test.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-01-22 chenyong first version
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <rtthread.h>
  28. #include <tls_certificate.h>
  29. #include <tls_client.h>
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include <mbedtls/config.h>
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35. // https://www.rt-thread.org/download/rt-thread.txt
  36. #define MBEDTLS_WEB_SERVER "www.rt-thread.org"
  37. #define MBEDTLS_WEB_PORT "443"
  38. #define MBEDTLS_READ_BUFFER 1024
  39. static const char *REQUEST = "GET /download/rt-thread.txt HTTP/1.1\r\n"
  40. "Host: www.rt-thread.org\r\n"
  41. "User-Agent: rtthread/3.1 rtt\r\n"
  42. "\r\n";
  43. static void mbedtls_client_entry(void *parament)
  44. {
  45. int ret = 0;
  46. MbedTLSSession *tls_session = RT_NULL;
  47. char *pers = "hello_world";
  48. rt_kprintf("MbedTLS test sample!\r\n");
  49. #ifdef FINSH_USING_MSH
  50. rt_kprintf("Memory usage before the handshake connection is established:\r\n");
  51. msh_exec("free", rt_strlen("free"));
  52. #endif
  53. tls_session = (MbedTLSSession *) tls_malloc(sizeof(MbedTLSSession));
  54. if (tls_session == RT_NULL)
  55. {
  56. rt_kprintf("No memory for MbedTLS session object.\n");
  57. return;
  58. }
  59. tls_session->host = tls_strdup(MBEDTLS_WEB_SERVER);
  60. tls_session->port = tls_strdup(MBEDTLS_WEB_PORT);
  61. tls_session->buffer_len = MBEDTLS_READ_BUFFER;
  62. tls_session->buffer = tls_malloc(tls_session->buffer_len);
  63. if (tls_session->buffer == RT_NULL)
  64. {
  65. rt_kprintf("No memory for MbedTLS buffer\n");
  66. tls_free(tls_session);
  67. return;
  68. }
  69. rt_kprintf("Start handshake tick:%d\n", rt_tick_get());
  70. if ((ret = mbedtls_client_init(tls_session, (void *) pers, rt_strlen(pers))) != 0)
  71. {
  72. rt_kprintf("MbedTLSClientInit err return : -0x%x\n", -ret);
  73. goto __exit;
  74. }
  75. if ((ret = mbedtls_client_context(tls_session)) < 0)
  76. {
  77. rt_kprintf("MbedTLSCLlientContext err return : -0x%x\n", -ret);
  78. goto __exit;
  79. }
  80. if ((ret = mbedtls_client_connect(tls_session)) != 0)
  81. {
  82. rt_kprintf("MbedTLSCLlientConnect err return : -0x%x\n", -ret);
  83. goto __exit;
  84. }
  85. rt_kprintf("Finish handshake tick:%d\n", rt_tick_get());
  86. rt_kprintf("MbedTLS connect success...\n");
  87. #ifdef FINSH_USING_MSH
  88. rt_kprintf("Memory usage after the handshake connection is established:\r\n");
  89. msh_exec("free", rt_strlen("free"));
  90. #endif
  91. while ((ret = mbedtls_client_write(tls_session, (const unsigned char *) REQUEST, rt_strlen(REQUEST))) <= 0)
  92. {
  93. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
  94. {
  95. rt_kprintf("mbedtls_ssl_write returned -0x%x\n", -ret);
  96. goto __exit;
  97. }
  98. }
  99. rt_kprintf("Writing HTTP request success...\n");
  100. rt_kprintf("Getting HTTP response...\n");
  101. {
  102. int i = 0;
  103. rt_memset(tls_session->buffer, 0x00, MBEDTLS_READ_BUFFER);
  104. ret = mbedtls_client_read(tls_session, (unsigned char *) tls_session->buffer, MBEDTLS_READ_BUFFER);
  105. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE
  106. || ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
  107. goto __exit;
  108. if (ret < 0)
  109. {
  110. rt_kprintf("Mbedtls_ssl_read returned -0x%x\n", -ret);
  111. goto __exit;
  112. }
  113. if (ret == 0)
  114. {
  115. rt_kprintf("TCP server connection closed.\n");
  116. goto __exit;
  117. }
  118. for (i = 0; i < ret; i++)
  119. rt_kprintf("%c", tls_session->buffer[i]);
  120. rt_kprintf("\n");
  121. }
  122. __exit:
  123. mbedtls_client_close(tls_session);
  124. rt_kprintf("MbedTLS connection close success.\n");
  125. return;
  126. }
  127. int mbedtls_client_start(void)
  128. {
  129. rt_thread_t tid;
  130. tid = rt_thread_create("tls_c", mbedtls_client_entry, RT_NULL, 6 * 1024, RT_THREAD_PRIORITY_MAX / 3 - 1, 5);
  131. if (tid)
  132. {
  133. rt_thread_startup(tid);
  134. }
  135. return RT_EOK;
  136. }
  137. #ifdef RT_USING_FINSH
  138. #include <finsh.h>
  139. FINSH_FUNCTION_EXPORT_ALIAS(mbedtls_client_start, tls_test, mbedtls client test start);
  140. #ifdef FINSH_USING_MSH
  141. MSH_CMD_EXPORT_ALIAS(mbedtls_client_start, tls_test, mbedtls client test start);
  142. #endif
  143. #endif