tls_app_test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. extern int msh_exec(char *cmd, rt_size_t length);
  52. msh_exec("free", rt_strlen("free"));
  53. #endif
  54. tls_session = (MbedTLSSession *) tls_malloc(sizeof(MbedTLSSession));
  55. if (tls_session == RT_NULL)
  56. {
  57. rt_kprintf("No memory for MbedTLS session object.\n");
  58. return;
  59. }
  60. rt_memset(tls_session, 0x0, sizeof(MbedTLSSession));
  61. tls_session->host = tls_strdup(MBEDTLS_WEB_SERVER);
  62. tls_session->port = tls_strdup(MBEDTLS_WEB_PORT);
  63. tls_session->buffer_len = MBEDTLS_READ_BUFFER;
  64. tls_session->buffer = tls_malloc(tls_session->buffer_len);
  65. if (tls_session->buffer == RT_NULL)
  66. {
  67. rt_kprintf("No memory for MbedTLS buffer\n");
  68. if (tls_session->host)
  69. {
  70. tls_free(tls_session->host);
  71. }
  72. if (tls_session->port)
  73. {
  74. tls_free(tls_session->port);
  75. }
  76. tls_free(tls_session);
  77. return;
  78. }
  79. rt_kprintf("Start handshake tick:%d\n", rt_tick_get());
  80. if ((ret = mbedtls_client_init(tls_session, (void *) pers, rt_strlen(pers))) != 0)
  81. {
  82. rt_kprintf("MbedTLSClientInit err return : -0x%x\n", -ret);
  83. goto __exit;
  84. }
  85. if ((ret = mbedtls_client_context(tls_session)) < 0)
  86. {
  87. rt_kprintf("MbedTLSCLlientContext err return : -0x%x\n", -ret);
  88. goto __exit;
  89. }
  90. if ((ret = mbedtls_client_connect(tls_session)) != 0)
  91. {
  92. rt_kprintf("MbedTLSCLlientConnect err return : -0x%x\n", -ret);
  93. goto __exit;
  94. }
  95. rt_kprintf("Finish handshake tick:%d\n", rt_tick_get());
  96. rt_kprintf("MbedTLS connect success...\n");
  97. #ifdef FINSH_USING_MSH
  98. rt_kprintf("Memory usage after the handshake connection is established:\r\n");
  99. msh_exec("free", rt_strlen("free"));
  100. #endif
  101. while ((ret = mbedtls_client_write(tls_session, (const unsigned char *) REQUEST, rt_strlen(REQUEST))) <= 0)
  102. {
  103. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
  104. {
  105. rt_kprintf("mbedtls_ssl_write returned -0x%x\n", -ret);
  106. goto __exit;
  107. }
  108. }
  109. rt_kprintf("Writing HTTP request success...\n");
  110. rt_kprintf("Getting HTTP response...\n");
  111. {
  112. int i = 0;
  113. rt_memset(tls_session->buffer, 0x00, MBEDTLS_READ_BUFFER);
  114. ret = mbedtls_client_read(tls_session, (unsigned char *) tls_session->buffer, MBEDTLS_READ_BUFFER);
  115. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE
  116. || ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
  117. goto __exit;
  118. if (ret < 0)
  119. {
  120. rt_kprintf("Mbedtls_ssl_read returned -0x%x\n", -ret);
  121. goto __exit;
  122. }
  123. if (ret == 0)
  124. {
  125. rt_kprintf("TCP server connection closed.\n");
  126. goto __exit;
  127. }
  128. for (i = 0; i < ret; i++)
  129. rt_kprintf("%c", tls_session->buffer[i]);
  130. rt_kprintf("\n");
  131. }
  132. __exit:
  133. mbedtls_client_close(tls_session);
  134. rt_kprintf("MbedTLS connection close success.\n");
  135. return;
  136. }
  137. int mbedtls_client_start(void)
  138. {
  139. rt_thread_t tid;
  140. tid = rt_thread_create("tls_c", mbedtls_client_entry, RT_NULL, 6 * 1024, RT_THREAD_PRIORITY_MAX / 3 - 1, 5);
  141. if (tid)
  142. {
  143. rt_thread_startup(tid);
  144. }
  145. return RT_EOK;
  146. }
  147. #ifdef RT_USING_FINSH
  148. #include <finsh.h>
  149. FINSH_FUNCTION_EXPORT_ALIAS(mbedtls_client_start, tls_test, mbedtls client test start);
  150. #ifdef FINSH_USING_MSH
  151. MSH_CMD_EXPORT_ALIAS(mbedtls_client_start, tls_test, mbedtls client test start);
  152. #endif
  153. #endif