nettype_tls.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2020-01-11 19:45:35
  5. * @LastEditTime: 2020-09-20 14:29:06
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "nettype_tls.h"
  9. #include "platform_net_socket.h"
  10. #include "platform_memory.h"
  11. #include "platform_timer.h"
  12. #include "random.h"
  13. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  14. #include "mbedtls/platform.h"
  15. #include "mbedtls/ssl.h"
  16. #include "mbedtls/entropy.h"
  17. #include "mbedtls/net_sockets.h"
  18. #include "mbedtls/ctr_drbg.h"
  19. #include "mbedtls/error.h"
  20. #include "mbedtls/debug.h"
  21. #include "mbedtls/x509_crt.h"
  22. #include "mbedtls/pk.h"
  23. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  24. static int server_certificate_verify(void *hostname, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
  25. {
  26. if (0 != *flags)
  27. MQTT_LOG_E("%s:%d %s()... server_certificate_verify failed returned 0x%04x\n", __FILE__, __LINE__, __FUNCTION__, *flags);
  28. return *flags;
  29. }
  30. #endif
  31. static int nettype_tls_entropy_source(void *data, uint8_t *output, size_t len, size_t *out_len)
  32. {
  33. uint32_t seed;
  34. (void) data;
  35. seed = random_number();
  36. if (len > sizeof(seed)) {
  37. len = sizeof(seed);
  38. }
  39. memcpy(output, &seed, len);
  40. *out_len = len;
  41. return 0;
  42. }
  43. static int nettype_tls_init(network_t* n, nettype_tls_params_t* nettype_tls_params)
  44. {
  45. int rc = MQTT_SUCCESS_ERROR;
  46. mbedtls_platform_set_calloc_free(platform_memory_calloc, platform_memory_free);
  47. mbedtls_net_init(&(nettype_tls_params->socket_fd));
  48. mbedtls_ssl_init(&(nettype_tls_params->ssl));
  49. mbedtls_ssl_config_init(&(nettype_tls_params->ssl_conf));
  50. mbedtls_ctr_drbg_init(&(nettype_tls_params->ctr_drbg));
  51. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  52. mbedtls_x509_crt_init(&(nettype_tls_params->ca_cert));
  53. mbedtls_x509_crt_init(&(nettype_tls_params->client_cert));
  54. mbedtls_pk_init(&(nettype_tls_params->private_key));
  55. #endif
  56. mbedtls_entropy_init(&(nettype_tls_params->entropy));
  57. mbedtls_entropy_add_source(&(nettype_tls_params->entropy), nettype_tls_entropy_source, NULL, MBEDTLS_ENTROPY_MAX_GATHER, MBEDTLS_ENTROPY_SOURCE_STRONG);
  58. if ((rc = mbedtls_ctr_drbg_seed(&(nettype_tls_params->ctr_drbg), mbedtls_entropy_func,
  59. &(nettype_tls_params->entropy), NULL, 0)) != 0) {
  60. MQTT_LOG_E("mbedtls_ctr_drbg_seed failed returned 0x%04x", (rc < 0 )? -rc : rc);
  61. RETURN_ERROR(rc);
  62. }
  63. if ((rc = mbedtls_ssl_config_defaults(&(nettype_tls_params->ssl_conf), MBEDTLS_SSL_IS_CLIENT,
  64. MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  65. MQTT_LOG_E("mbedtls_ssl_config_defaults failed returned 0x%04x", (rc < 0 )? -rc : rc);
  66. RETURN_ERROR(rc);
  67. }
  68. mbedtls_ssl_conf_rng(&(nettype_tls_params->ssl_conf), mbedtls_ctr_drbg_random, &(nettype_tls_params->ctr_drbg));
  69. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  70. if (NULL != n->ca_crt) {
  71. n->ca_crt_len = strlen(n->ca_crt);
  72. if (0 != (rc = (mbedtls_x509_crt_parse(&(nettype_tls_params->ca_cert), (unsigned char *)n->ca_crt,
  73. (n->ca_crt_len + 1))))) {
  74. MQTT_LOG_E("%s:%d %s()... parse ca crt failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
  75. RETURN_ERROR(rc);
  76. }
  77. }
  78. mbedtls_ssl_conf_ca_chain(&(nettype_tls_params->ssl_conf), &(nettype_tls_params->ca_cert), NULL);
  79. if ((rc = mbedtls_ssl_conf_own_cert(&(nettype_tls_params->ssl_conf),
  80. &(nettype_tls_params->client_cert), &(nettype_tls_params->private_key))) != 0) {
  81. MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_conf_own_cert failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
  82. RETURN_ERROR(rc);
  83. }
  84. mbedtls_ssl_conf_verify(&(nettype_tls_params->ssl_conf), server_certificate_verify, (void *)n->host);
  85. mbedtls_ssl_conf_authmode(&(nettype_tls_params->ssl_conf), MBEDTLS_SSL_VERIFY_REQUIRED);
  86. #endif
  87. mbedtls_ssl_conf_read_timeout(&(nettype_tls_params->ssl_conf), n->timeout_ms);
  88. if ((rc = mbedtls_ssl_setup(&(nettype_tls_params->ssl), &(nettype_tls_params->ssl_conf))) != 0) {
  89. MQTT_LOG_E("mbedtls_ssl_setup failed returned 0x%04x", (rc < 0 )? -rc : rc);
  90. RETURN_ERROR(rc);
  91. }
  92. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  93. if ((rc = mbedtls_ssl_set_hostname(&(nettype_tls_params->ssl), n->host)) != 0) {
  94. MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_set_hostname failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
  95. RETURN_ERROR(rc);
  96. }
  97. #endif
  98. mbedtls_ssl_set_bio(&(nettype_tls_params->ssl), &(nettype_tls_params->socket_fd), mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);
  99. RETURN_ERROR(MQTT_SUCCESS_ERROR);
  100. }
  101. int nettype_tls_connect(network_t* n)
  102. {
  103. int rc;
  104. if (NULL == n)
  105. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  106. nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) platform_memory_alloc(sizeof(nettype_tls_params_t));
  107. if (NULL == nettype_tls_params)
  108. RETURN_ERROR(MQTT_MEM_NOT_ENOUGH_ERROR);
  109. rc = nettype_tls_init(n, nettype_tls_params);
  110. if (MQTT_SUCCESS_ERROR != rc)
  111. goto exit;
  112. if (0 != (rc = mbedtls_net_connect(&(nettype_tls_params->socket_fd), n->host, n->port, MBEDTLS_NET_PROTO_TCP)))
  113. goto exit;
  114. while ((rc = mbedtls_ssl_handshake(&(nettype_tls_params->ssl))) != 0) {
  115. if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE) {
  116. MQTT_LOG_E("%s:%d %s()...mbedtls handshake failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
  117. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  118. if (rc == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
  119. MQTT_LOG_E("%s:%d %s()...unable to verify the server's certificate", __FILE__, __LINE__, __FUNCTION__);
  120. }
  121. #endif
  122. goto exit;
  123. }
  124. }
  125. if ((rc = mbedtls_ssl_get_verify_result(&(nettype_tls_params->ssl))) != 0) {
  126. MQTT_LOG_E("%s:%d %s()...mbedtls_ssl_get_verify_result returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
  127. goto exit;
  128. }
  129. n->nettype_tls_params = nettype_tls_params;
  130. RETURN_ERROR(MQTT_SUCCESS_ERROR)
  131. exit:
  132. platform_memory_free(nettype_tls_params);
  133. RETURN_ERROR(rc);
  134. }
  135. void nettype_tls_disconnect(network_t* n)
  136. {
  137. int rc = 0;
  138. if (NULL == n)
  139. return;
  140. nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;
  141. do {
  142. rc = mbedtls_ssl_close_notify(&(nettype_tls_params->ssl));
  143. } while (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE);
  144. mbedtls_net_free(&(nettype_tls_params->socket_fd));
  145. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  146. mbedtls_x509_crt_free(&(nettype_tls_params->client_cert));
  147. mbedtls_x509_crt_free(&(nettype_tls_params->ca_cert));
  148. mbedtls_pk_free(&(nettype_tls_params->private_key));
  149. #endif
  150. mbedtls_ssl_free(&(nettype_tls_params->ssl));
  151. mbedtls_ssl_config_free(&(nettype_tls_params->ssl_conf));
  152. mbedtls_ctr_drbg_free(&(nettype_tls_params->ctr_drbg));
  153. mbedtls_entropy_free(&(nettype_tls_params->entropy));
  154. platform_memory_free(nettype_tls_params);
  155. }
  156. int nettype_tls_write(network_t *n, unsigned char *buf, int len, int timeout)
  157. {
  158. int rc = 0;
  159. int write_len = 0;
  160. platform_timer_t timer;
  161. if (NULL == n)
  162. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  163. nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;
  164. platform_timer_cutdown(&timer, timeout);
  165. do {
  166. rc = mbedtls_ssl_write(&(nettype_tls_params->ssl), (unsigned char *)(buf + write_len), len - write_len);
  167. if (rc > 0) {
  168. write_len += rc;
  169. } else if ((rc == 0) || ((rc != MBEDTLS_ERR_SSL_WANT_WRITE) && (rc != MBEDTLS_ERR_SSL_WANT_READ) && (rc != MBEDTLS_ERR_SSL_TIMEOUT))) {
  170. MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_write failed: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
  171. break;
  172. }
  173. } while((!platform_timer_is_expired(&timer)) && (write_len < len));
  174. return write_len;
  175. }
  176. int nettype_tls_read(network_t *n, unsigned char *buf, int len, int timeout)
  177. {
  178. int rc = 0;
  179. int read_len = 0;
  180. platform_timer_t timer;
  181. if (NULL == n)
  182. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  183. nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;
  184. platform_timer_cutdown(&timer, timeout);
  185. do {
  186. rc = mbedtls_ssl_read(&(nettype_tls_params->ssl), (unsigned char *)(buf + read_len), len - read_len);
  187. if (rc > 0) {
  188. read_len += rc;
  189. } else if ((rc == 0) || ((rc != MBEDTLS_ERR_SSL_WANT_WRITE) && (rc != MBEDTLS_ERR_SSL_WANT_READ) && (rc != MBEDTLS_ERR_SSL_TIMEOUT))) {
  190. // MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_read failed: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
  191. break;
  192. }
  193. } while((!platform_timer_is_expired(&timer)) && (read_len < len));
  194. return read_len;
  195. }
  196. #endif /* MQTT_NETWORK_TYPE_NO_TLS */