espconn_mbedtls.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * ESPRSSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef ESPCONN_MBEDTLS_H_
  25. #define ESPCONN_MBEDTLS_H_
  26. #include "lwip/ip.h"
  27. #include "lwip/app/espconn.h"
  28. #include "user_interface.h"
  29. #if !defined(ESPCONN_MBEDTLS)
  30. #include "mbedtls/net_sockets.h"
  31. #include "mbedtls/debug.h"
  32. #include "mbedtls/ssl.h"
  33. #include "mbedtls/entropy.h"
  34. #include "mbedtls/ctr_drbg.h"
  35. typedef struct espconn *pmbedtls_espconn;
  36. typedef struct espconn mbedtls_espconn;
  37. typedef struct{
  38. // mbedtls_entropy_context entropy;
  39. mbedtls_x509_crt cacert;
  40. mbedtls_x509_crt clicert;
  41. mbedtls_pk_context pkey;
  42. }mbedtls_session, *pmbedtls_session;
  43. typedef struct{
  44. bool quiet;
  45. int record_len;
  46. pmbedtls_session psession;
  47. mbedtls_net_context fd;
  48. mbedtls_ctr_drbg_context ctr_drbg;
  49. mbedtls_ssl_context ssl;
  50. mbedtls_ssl_config conf;
  51. mbedtls_entropy_context entropy;
  52. bool SentFnFlag;
  53. sint32 verify_result;
  54. }mbedtls_msg, *pmbedtls_msg;
  55. typedef enum {
  56. ESPCONN_CERT_OWN,
  57. ESPCONN_CERT_AUTH,
  58. ESPCONN_PK,
  59. }mbedtls_auth_type;
  60. typedef enum {
  61. ESPCONN_IDLE = 0,
  62. ESPCONN_CLIENT,
  63. ESPCONN_MAX
  64. }espconn_level;
  65. typedef struct _file_head{
  66. char file_name[32];
  67. uint16_t file_length;
  68. }file_head;
  69. typedef struct _file_param{
  70. file_head file_head;
  71. int32 file_offerset;
  72. }file_param;
  73. typedef struct _ssl_sector{
  74. uint32 sector;
  75. bool flag;
  76. }ssl_sector;
  77. struct ssl_options {
  78. uint16 buffer_size;
  79. ssl_sector cert_ca_sector;
  80. ssl_sector cert_req_sector;
  81. int cert_verify_callback;
  82. int cert_auth_callback;
  83. };
  84. #define SSL_KEEP_INTVL 1
  85. #define SSL_KEEP_CNT 5
  86. #define SSL_KEEP_IDLE 90
  87. #define ssl_keepalive_enable(pcb) ((pcb)->so_options |= SOF_KEEPALIVE)
  88. #define ssl_keepalive_disable(pcb) ((pcb)->so_options &= ~SOF_KEEPALIVE)
  89. enum {
  90. SIG_ESPCONN_TLS_ERRER = 0x3B
  91. };
  92. #define ESPCONN_SECURE_MAX_SIZE 8192
  93. #define ESPCONN_SECURE_DEFAULT_HEAP 0x3800
  94. #define ESPCONN_HANDSHAKE_TIMEOUT 0x3C
  95. #define ESPCONN_INVALID_TYPE 0xFFFFFFFF
  96. #define MBEDTLS_SSL_PLAIN_ADD TCP_MSS
  97. #define FLASH_SECTOR_SIZE 4096
  98. extern struct ssl_options ssl_client_options;
  99. typedef struct{
  100. uint32 parame_sec;
  101. uint32 parame_type;
  102. uint32 parame_datalen;
  103. char* parame_data;
  104. }mbedtls_parame, *pmbedtls_parame;
  105. /*
  106. * Storage format identifiers
  107. * Recognized formats: PEM and DER
  108. */
  109. typedef enum{
  110. ESPCONN_FORMAT_INIT = 0,
  111. ESPCONN_FORMAT_DER = 1,
  112. ESPCONN_FORMAT_PEM = 2,
  113. ESPCONN_FORMAT_INVALID
  114. }espconn_format;
  115. #define ESPCONN_EVENT_RECV(pcb,p,err) \
  116. do { \
  117. if((pcb)!= NULL && (pcb)->recv_callback != NULL) { \
  118. (pcb)->state = ESPCONN_READ; \
  119. (pcb)->recv_callback((pcb),(p),(err));\
  120. (pcb)->state = ESPCONN_CONNECT; \
  121. } else { \
  122. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  123. } \
  124. } while (0)
  125. #define ESPCONN_EVENT_SEND(pcb) \
  126. do { \
  127. if((pcb)!= NULL && (pcb)->sent_callback != NULL) { \
  128. (pcb)->state = ESPCONN_CONNECT; \
  129. (pcb)->sent_callback(pcb);\
  130. } else { \
  131. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  132. } \
  133. } while (0)
  134. #define ESPCONN_EVENT_CONNECTED(pcb) \
  135. do { \
  136. if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->connect_callback != NULL) { \
  137. (pcb)->state = ESPCONN_CONNECT; \
  138. (pcb)->proto.tcp->connect_callback(pcb);\
  139. } else { \
  140. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  141. } \
  142. } while (0)
  143. #define ESPCONN_EVENT_CLOSED(pcb) \
  144. do { \
  145. if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->disconnect_callback != NULL) { \
  146. (pcb)->state = ESPCONN_CLOSE; \
  147. (pcb)->proto.tcp->disconnect_callback(pcb);\
  148. } else { \
  149. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  150. } \
  151. } while (0)
  152. #define ESPCONN_EVENT_ERROR(pcb,err) \
  153. do { \
  154. if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->reconnect_callback != NULL) { \
  155. (pcb)->state = ESPCONN_CLOSE; \
  156. (pcb)->proto.tcp->reconnect_callback(pcb,err);\
  157. } else { \
  158. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  159. } \
  160. } while (0)
  161. /******************************************************************************
  162. * FunctionName : espconn_ssl_client
  163. * Description : Initialize the client: set up a connect PCB and bind it to
  164. * the defined port
  165. * Parameters : espconn -- the espconn used to build client
  166. * Returns : none
  167. *******************************************************************************/
  168. extern sint8 espconn_ssl_client(struct espconn *espconn);
  169. /******************************************************************************
  170. * FunctionName : espconn_ssl_write
  171. * Description : sent data for client or server
  172. * Parameters : void *arg -- client or server to send
  173. * uint8* psent -- Data to send
  174. * uint16 length -- Length of data to send
  175. * Returns : none
  176. *******************************************************************************/
  177. extern void espconn_ssl_sent(void *arg, uint8 *psent, uint16 length);
  178. /******************************************************************************
  179. * FunctionName : espconn_ssl_disconnect
  180. * Description : A new incoming connection has been disconnected.
  181. * Parameters : espconn -- the espconn used to disconnect with host
  182. * Returns : none
  183. *******************************************************************************/
  184. extern void espconn_ssl_disconnect(espconn_msg *pdis);
  185. #endif
  186. #endif /* ESPCONN_MBEDTLS_H_ */