luat_libtcpip_mbedtls_ssl.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "string.h"
  4. #ifdef LUAT_USE_LIBTCPIP_MBEDTLS
  5. #include "mbedtls/net_sockets.h"
  6. #include "mbedtls/net.h"
  7. #include "mbedtls/ssl.h"
  8. #include "mbedtls/entropy.h"
  9. #include "mbedtls/ctr_drbg.h"
  10. #include "mbedtls/debug.h"
  11. #include <unistd.h>
  12. #include <arpa/inet.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16. #include "luat_libtcpip.h"
  17. #define LUAT_LOG_TAG "mbedtls"
  18. #include "luat_log.h"
  19. #define mbedtls_printf LLOGD
  20. typedef struct luat_libtcpip_mbedtls_ssl_ctx
  21. {
  22. mbedtls_net_context server_fd;
  23. mbedtls_entropy_context entropy;
  24. mbedtls_ctr_drbg_context ctr_drbg;
  25. mbedtls_ssl_context ssl;
  26. mbedtls_ssl_config conf;
  27. mbedtls_x509_crt cacert;
  28. }luat_libtcpip_mbedtls_ssl_ctx_t;
  29. static void* luat_libtcpip_socket_mbedtls_ssl(int domain, int type, int protocol) {
  30. int ret = 0;
  31. const char *pers = "ssl_client1";
  32. luat_libtcpip_mbedtls_ssl_ctx_t* ctx = luat_heap_malloc(sizeof(luat_libtcpip_mbedtls_ssl_ctx_t));
  33. if (ctx == NULL)
  34. return NULL;
  35. mbedtls_net_init( &ctx->server_fd );
  36. mbedtls_ssl_init( &ctx->ssl );
  37. mbedtls_ssl_config_init( &ctx->conf );
  38. mbedtls_x509_crt_init( &ctx->cacert );
  39. mbedtls_ctr_drbg_init( &ctx->ctr_drbg );
  40. mbedtls_entropy_init( &ctx->entropy );
  41. // 初始化种子
  42. if( ( ret = mbedtls_ctr_drbg_seed( &ctx->ctr_drbg, mbedtls_entropy_func, &ctx->entropy,
  43. (const unsigned char *) pers,
  44. strlen( pers ) ) ) != 0 )
  45. {
  46. printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  47. goto fail;
  48. }
  49. // 处理证书,如果有的话
  50. // ret = mbedtls_x509_crt_parse( &ctx->cacert, (const unsigned char *) iotda_crt_pem, sizeof(iotda_crt_pem) );
  51. // if (ret != 0) {
  52. // printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n", ret );
  53. // goto fail;
  54. // }
  55. return (void*)ctx;
  56. fail:
  57. luat_heap_free(ctx);
  58. return NULL;
  59. }
  60. static int luat_libtcpip_send_mbedtls_ssl(void* s, const void *data, size_t size, int flags) {
  61. luat_libtcpip_mbedtls_ssl_ctx_t* ctx = (luat_libtcpip_mbedtls_ssl_ctx_t*)s;
  62. return mbedtls_ssl_write(&ctx->ssl, data, size);
  63. }
  64. static int luat_libtcpip_recv_mbedtls_ssl(void* s, void *mem, size_t len, int flags) {
  65. luat_libtcpip_mbedtls_ssl_ctx_t* ctx = (luat_libtcpip_mbedtls_ssl_ctx_t*)s;
  66. // 设置好超时
  67. mbedtls_ssl_conf_read_timeout(&ctx->conf, 1);
  68. int ret = mbedtls_ssl_read(&ctx->ssl, mem, len);
  69. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  70. return 0;
  71. if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
  72. return -1;
  73. if (ret < 0) {
  74. // mbedtls_printf("mbedtls_ssl_read ret = %d", ret);
  75. return ret;
  76. }
  77. return ret;
  78. }
  79. static int luat_libtcpip_recv_timeout_mbedtls_ssl(void* s, void *mem, size_t len, int flags, int timeout) {
  80. luat_libtcpip_mbedtls_ssl_ctx_t* ctx = (luat_libtcpip_mbedtls_ssl_ctx_t*)s;
  81. // 设置好超时
  82. mbedtls_ssl_conf_read_timeout(&ctx->conf, timeout);
  83. int ret = mbedtls_ssl_read(&ctx->ssl, mem, len);
  84. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  85. return 0;
  86. if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
  87. return -1;
  88. if (ret < 0) {
  89. // mbedtls_printf("mbedtls_ssl_read ret = %d", ret);
  90. return ret;
  91. }
  92. return ret;
  93. }
  94. // static int luat_libtcpip_select_mbedtls_ssl(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  95. // struct timeval *timeout) {
  96. // return select(maxfdp1, readset, writeset, exceptset, timeout);
  97. // }
  98. static int luat_libtcpip_close_mbedtls_ssl(void* s) {
  99. luat_libtcpip_mbedtls_ssl_ctx_t* ctx = (luat_libtcpip_mbedtls_ssl_ctx_t*)s;
  100. if (ctx == NULL)
  101. return 0;
  102. mbedtls_net_free( &ctx->server_fd );
  103. mbedtls_x509_crt_free( &ctx->cacert );
  104. mbedtls_ssl_free( &ctx->ssl );
  105. mbedtls_ssl_config_free( &ctx->conf );
  106. mbedtls_ctr_drbg_free( &ctx->ctr_drbg );
  107. mbedtls_entropy_free( &ctx->entropy );
  108. luat_heap_free(ctx);
  109. return 0;
  110. }
  111. static int luat_libtcpip_connect_mbedtls_ssl(void* s, const char *hostname, uint16_t _port) {
  112. // 1. Start the connection
  113. int ret = 0;
  114. uint32_t flags;
  115. char port[8] = {0};
  116. sprintf(port, "%d", _port);
  117. luat_libtcpip_mbedtls_ssl_ctx_t* ctx = (luat_libtcpip_mbedtls_ssl_ctx_t*)s;
  118. ret = mbedtls_net_connect(&ctx->server_fd, hostname, port, MBEDTLS_NET_PROTO_TCP);
  119. if (ret != 0) {
  120. mbedtls_printf("mbedtls_net_connect %d\n", ret);
  121. return -1;
  122. }
  123. // Setting up the SSL/TLS structure...
  124. if( ( ret = mbedtls_ssl_config_defaults( &ctx->conf,
  125. MBEDTLS_SSL_IS_CLIENT,
  126. MBEDTLS_SSL_TRANSPORT_STREAM,
  127. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  128. {
  129. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  130. return -2;
  131. }
  132. // 暂时禁用证书验证
  133. // mbedtls_ssl_conf_authmode( &ctx->conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
  134. mbedtls_ssl_conf_authmode( &ctx->conf, MBEDTLS_SSL_VERIFY_NONE );
  135. //mbedtls_ssl_conf_ca_chain( &ctx->conf, &ctx->cacert, NULL );
  136. mbedtls_ssl_conf_rng( &ctx->conf, mbedtls_ctr_drbg_random, &ctx->ctr_drbg );
  137. if( ( ret = mbedtls_ssl_setup( &ctx->ssl, &ctx->conf ) ) != 0 )
  138. {
  139. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
  140. return -3;
  141. }
  142. if( ( ret = mbedtls_ssl_set_hostname( &ctx->ssl, hostname ) ) != 0 )
  143. {
  144. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  145. return -4;
  146. }
  147. mbedtls_ssl_set_bio( &ctx->ssl, &ctx->server_fd, mbedtls_net_send, NULL, mbedtls_net_recv_timeout );
  148. while( ( ret = mbedtls_ssl_handshake( &ctx->ssl ) ) != 0 )
  149. {
  150. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  151. {
  152. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
  153. // goto exit;
  154. return -5;
  155. }
  156. }
  157. if( ( flags = mbedtls_ssl_get_verify_result( &ctx->ssl ) ) != 0 ) {
  158. mbedtls_printf( " failed\n ! mbedtls_ssl_get_verify_result returned %d\n\n", flags );
  159. return -6;
  160. }
  161. return 0;
  162. // exit :
  163. // return -1;
  164. }
  165. static struct hostent* luat_libtcpip_gethostbyname_mbedtls_ssl(const char* name) {
  166. return gethostbyname(name);
  167. }
  168. static int luat_libtcpip_setsockopt_mbedtls_ssl(void* s, int level, int optname, const void *optval, uint32_t optlen) {
  169. // return setsockopt(s, level, optname, optval, optlen);
  170. return 0; // nop
  171. }
  172. luat_libtcpip_opts_t luat_libtcpip_mbedtls_ssl = {
  173. ._socket = luat_libtcpip_socket_mbedtls_ssl,
  174. ._close = luat_libtcpip_close_mbedtls_ssl,
  175. ._connect = luat_libtcpip_connect_mbedtls_ssl,
  176. ._gethostbyname = luat_libtcpip_gethostbyname_mbedtls_ssl,
  177. ._recv = luat_libtcpip_recv_mbedtls_ssl,
  178. ._recv_timeout = luat_libtcpip_recv_timeout_mbedtls_ssl,
  179. // ._select = luat_libtcpip_select_mbedtls_ssl,
  180. ._send = luat_libtcpip_send_mbedtls_ssl,
  181. ._setsockopt = luat_libtcpip_setsockopt_mbedtls_ssl
  182. };
  183. #endif