espconn_mbedtls.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. int record_len;
  39. }mbedtls_record;
  40. #if defined(ESP8266_PLATFORM)
  41. typedef struct{
  42. uint8* finished_buf;
  43. int finished_len;
  44. }mbedtls_finished, *pmbedtls_finished;
  45. #endif
  46. typedef struct{
  47. // mbedtls_entropy_context entropy;
  48. mbedtls_x509_crt cacert;
  49. mbedtls_x509_crt clicert;
  50. mbedtls_pk_context pkey;
  51. }mbedtls_session, *pmbedtls_session;
  52. typedef struct{
  53. bool quiet;
  54. mbedtls_record record;
  55. #if defined(ESP8266_PLATFORM)
  56. pmbedtls_finished pfinished;
  57. #endif
  58. pmbedtls_session psession;
  59. mbedtls_net_context fd;
  60. mbedtls_net_context listen_fd;
  61. mbedtls_ctr_drbg_context ctr_drbg;
  62. mbedtls_ssl_context ssl;
  63. mbedtls_ssl_config conf;
  64. mbedtls_entropy_context entropy;
  65. bool SentFnFlag;
  66. sint32 verify_result;
  67. }mbedtls_msg, *pmbedtls_msg;
  68. typedef enum {
  69. ESPCONN_CERT_OWN,
  70. ESPCONN_CERT_AUTH,
  71. ESPCONN_PK,
  72. ESPCONN_PASSWORD
  73. }mbedtls_auth_type;
  74. typedef enum {
  75. ESPCONN_IDLE = 0,
  76. ESPCONN_CLIENT,
  77. ESPCONN_SERVER,
  78. ESPCONN_BOTH,
  79. ESPCONN_MAX
  80. }espconn_level;
  81. typedef struct _file_head{
  82. char file_name[32];
  83. uint16_t file_length;
  84. }file_head;
  85. typedef struct _file_param{
  86. file_head file_head;
  87. int32 file_offerset;
  88. }file_param;
  89. typedef struct _ssl_sector{
  90. uint32 sector;
  91. bool flag;
  92. }ssl_sector;
  93. struct ssl_packet{
  94. uint8* pbuffer;
  95. uint16 buffer_size;
  96. ssl_sector cert_ca_sector;
  97. ssl_sector cert_req_sector;
  98. };
  99. typedef struct _ssl_opt {
  100. struct ssl_packet server;
  101. struct ssl_packet client;
  102. uint8 type;
  103. }ssl_opt;
  104. typedef struct{
  105. mbedtls_auth_type auth_type;
  106. espconn_level auth_level;
  107. }mbedtls_auth_info;
  108. #define SSL_KEEP_INTVL 1
  109. #define SSL_KEEP_CNT 5
  110. #define SSL_KEEP_IDLE 90
  111. #define ssl_keepalive_enable(pcb) ((pcb)->so_options |= SOF_KEEPALIVE)
  112. #define ssl_keepalive_disable(pcb) ((pcb)->so_options &= ~SOF_KEEPALIVE)
  113. enum {
  114. SIG_ESPCONN_TLS_ERRER = 0x3B
  115. };
  116. #define ESPCONN_SECURE_MAX_SIZE 8192
  117. #define ESPCONN_SECURE_DEFAULT_HEAP 0x3800
  118. #define ESPCONN_SECURE_DEFAULT_SIZE SSL_BUFFER_SIZE
  119. #define ESPCONN_HANDSHAKE_TIMEOUT 0x3C
  120. #define ESPCONN_INVALID_TYPE 0xFFFFFFFF
  121. #define MBEDTLS_SSL_PLAIN_ADD TCP_MSS
  122. #define FLASH_SECTOR_SIZE 4096
  123. extern ssl_opt ssl_option;
  124. typedef struct{
  125. uint32 parame_sec;
  126. uint32 parame_type;
  127. uint32 parame_datalen;
  128. char* parame_data;
  129. }mbedtls_parame, *pmbedtls_parame;
  130. /*
  131. * Storage format identifiers
  132. * Recognized formats: PEM and DER
  133. */
  134. typedef enum{
  135. ESPCONN_FORMAT_INIT = 0,
  136. ESPCONN_FORMAT_DER = 1,
  137. ESPCONN_FORMAT_PEM = 2,
  138. ESPCONN_FORMAT_INVALID
  139. }espconn_format;
  140. #define ESPCONN_EVENT_RECV(pcb,p,err) \
  141. do { \
  142. if((pcb)!= NULL && (pcb)->recv_callback != NULL) { \
  143. (pcb)->state = ESPCONN_READ; \
  144. (pcb)->recv_callback((pcb),(p),(err));\
  145. (pcb)->state = ESPCONN_CONNECT; \
  146. } else { \
  147. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  148. } \
  149. } while (0)
  150. #define ESPCONN_EVENT_SEND(pcb) \
  151. do { \
  152. if((pcb)!= NULL && (pcb)->sent_callback != NULL) { \
  153. (pcb)->state = ESPCONN_CONNECT; \
  154. (pcb)->sent_callback(pcb);\
  155. } else { \
  156. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  157. } \
  158. } while (0)
  159. #define ESPCONN_EVENT_CONNECTED(pcb) \
  160. do { \
  161. if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->connect_callback != NULL) { \
  162. (pcb)->state = ESPCONN_CONNECT; \
  163. (pcb)->proto.tcp->connect_callback(pcb);\
  164. } else { \
  165. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  166. } \
  167. } while (0)
  168. #define ESPCONN_EVENT_CLOSED(pcb) \
  169. do { \
  170. if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->disconnect_callback != NULL) { \
  171. (pcb)->state = ESPCONN_CLOSE; \
  172. (pcb)->proto.tcp->disconnect_callback(pcb);\
  173. } else { \
  174. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  175. } \
  176. } while (0)
  177. #define ESPCONN_EVENT_ERROR(pcb,err) \
  178. do { \
  179. if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->reconnect_callback != NULL) { \
  180. (pcb)->state = ESPCONN_CLOSE; \
  181. (pcb)->proto.tcp->reconnect_callback(pcb,err);\
  182. } else { \
  183. ESP_LOG("%s %d\n", __FILE__, __LINE__); \
  184. } \
  185. } while (0)
  186. /******************************************************************************
  187. * FunctionName : mbedtls_load_default_obj
  188. * Description : Initialize the server: set up a listen PCB and bind it to
  189. * the defined port
  190. * Parameters : espconn -- the espconn used to build client
  191. * Returns : none
  192. *******************************************************************************/
  193. bool mbedtls_load_default_obj(uint32 flash_sector, int obj_type, const unsigned char *load_buf, uint16 length);
  194. /******************************************************************************
  195. * FunctionName : sslserver_start
  196. * Description : Initialize the server: set up a listen PCB and bind it to
  197. * the defined port
  198. * Parameters : espconn -- the espconn used to build client
  199. * Returns : none
  200. *******************************************************************************/
  201. extern sint8 espconn_ssl_server(struct espconn *espconn);
  202. /******************************************************************************
  203. * FunctionName : espconn_ssl_client
  204. * Description : Initialize the client: set up a connect PCB and bind it to
  205. * the defined port
  206. * Parameters : espconn -- the espconn used to build client
  207. * Returns : none
  208. *******************************************************************************/
  209. extern sint8 espconn_ssl_client(struct espconn *espconn);
  210. /******************************************************************************
  211. * FunctionName : espconn_ssl_write
  212. * Description : sent data for client or server
  213. * Parameters : void *arg -- client or server to send
  214. * uint8* psent -- Data to send
  215. * uint16 length -- Length of data to send
  216. * Returns : none
  217. *******************************************************************************/
  218. extern void espconn_ssl_sent(void *arg, uint8 *psent, uint16 length);
  219. /******************************************************************************
  220. * FunctionName : espconn_ssl_disconnect
  221. * Description : A new incoming connection has been disconnected.
  222. * Parameters : espconn -- the espconn used to disconnect with host
  223. * Returns : none
  224. *******************************************************************************/
  225. extern void espconn_ssl_disconnect(espconn_msg *pdis);
  226. /******************************************************************************
  227. * FunctionName : espconn_secure_get_size
  228. * Description : get buffer size for client or server
  229. * Parameters : level -- set for client or server
  230. * 1: client,2:server,3:client and server
  231. * Returns : buffer size for client or server
  232. *******************************************************************************/
  233. extern sint16 espconn_secure_get_size(uint8 level);
  234. #endif
  235. #endif /* ESPCONN_MBEDTLS_H_ */