mbedtls_sockets.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <cfw.h>
  2. #include "sockets.h"
  3. #include "lwip/tcp.h"
  4. #include "lwip/api.h"
  5. #include "lwip/netdb.h"
  6. //#include "netif.h"
  7. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SSL
  8. #ifdef CONFIG_MBEDTLS_TCPIP_SUPPORT
  9. #include "mbedtls/ssl.h"
  10. #include "mbedtls/net_sockets.h"
  11. #include "mbedtls/entropy.h"
  12. #include "mbedtls/ctr_drbg.h"
  13. #include "mbedtls/timing.h"
  14. #include "mbedtls/debug.h"
  15. #define DATA_OUTPUT_RINGBUF_SIZE 1024
  16. #define TLSSOCK_INVALID_HANDLE 0 //-1
  17. #define TLSSOCK_HANDLE uint32_t
  18. #define MBEDSOCK_TASK_PRIORITY OSI_PRIORITY_NORMAL //(COS_MMI_TASKS_PRIORITY_BASE + 2)
  19. #define TLSSOCK_ERR_SOCK_CONNECT_FAIL -1
  20. #define TLSSOCK_ERR_SSL_CERTIFICATE_VERIFY_FAIL -2
  21. #define IP_ADDR uint32_t
  22. typedef osiThread_t *HANDLE;
  23. #define COS_CALLBACK_FUNC_T osiCallback_t
  24. #define FALSE 0
  25. #define TRUE 1
  26. #define COS_CALLBACK_FUNC_T osiCallback_t
  27. typedef enum
  28. {
  29. TLSSOCK_STATE_CREATED,
  30. TLSSOCK_STATE_CONNECTING,
  31. TLSSOCK_STATE_CONNECTED,
  32. TLSSOCK_STATE_REFUSED,
  33. } TLSSOCK_STATE_NUM;
  34. typedef enum
  35. {
  36. EV_CFW_TLSSOCK_CREATE_REQ = 0x1000,
  37. EV_CFW_TLSSOCK_CONNECT_REQ,
  38. EV_CFW_TLSSOCK_FREE_REQ,
  39. EV_CFW_TLSSOCK_INTERNAL_READ_REQ,
  40. EV_CFW_TLSSOCK_INTERNAL_CLOSE_IND
  41. } CFW_TLSSOCK_REQ;
  42. typedef enum
  43. {
  44. EV_CFW_TLSSOCK_CONNECT_EVENT_IND,
  45. EV_CFW_TLSSOCK_CLOSE_EVENT_IND,
  46. EV_CFW_TLSSOCK_DATA_SENT_EVENT_IND,
  47. EV_CFW_TLSSOCK_READ_EVENT_IND,
  48. } SOCK_TYPE;
  49. typedef enum
  50. {
  51. TLSSOCK_CFG_TYPE_VERSION,
  52. TLSSOCK_CFG_TYPE_AUTHMODE,
  53. TLSSOCK_CFG_TYPE_CIPHER_SUITE,
  54. TLSSOCK_CFG_TYPE_CA,
  55. TLSSOCK_CFG_TYPE_CLI_CERT,
  56. TLSSOCK_CFG_TYPE_CLI_KEY,
  57. TLSSOCK_CFG_TYPE_IGNORE_RTC_TIME,
  58. TLSSOCK_CFG_TYPE_HTTPS,
  59. TLSSOCK_CFG_TYPE_HTTPS_CTXI,
  60. TLSSOCK_CFG_TYPE_SMTPS_TYPE,
  61. TLSSOCK_CFG_TYPE_SMTPS_CTXI,
  62. } TLSSOCK_CFG_TYPE_E;
  63. #define TLSSOCK_AUTH_MODE_NONE 0
  64. #define TLSSOCK_AUTH_MODE_SERVER_OPTIONAL 1
  65. #define TLSSOCK_AUTH_MODE_SERVER_REQUIRED 2
  66. #define TLSSOCK_INVALID_CID 0xFF
  67. struct data_buf_t
  68. {
  69. uint32_t put;
  70. uint32_t get;
  71. uint8_t buf[DATA_OUTPUT_RINGBUF_SIZE];
  72. };
  73. typedef struct _TLSSOCK_CRT_T
  74. {
  75. char *crt;
  76. int32_t len;
  77. } TLSSOCK_CRT_T;
  78. typedef struct _TLSSOCK_CONTEXT
  79. {
  80. int32_t is_used;
  81. TLSSOCK_STATE_NUM state;
  82. char *ip;
  83. char *port;
  84. uint8_t nCid;
  85. uint8_t nSimid;
  86. uint16_t fport;
  87. uint32_t fip;
  88. uint32_t rx_data_len;
  89. int32_t report_flag;
  90. HANDLE notify_task;
  91. COS_CALLBACK_FUNC_T notify_callback;
  92. void *funcParam;
  93. mbedtls_ssl_context ssl;
  94. mbedtls_net_context server_fd;
  95. mbedtls_entropy_context entropy;
  96. mbedtls_ctr_drbg_context ctr_drbg;
  97. mbedtls_ssl_config conf;
  98. mbedtls_x509_crt cacert;
  99. int32_t sock_id;
  100. mbedtls_x509_crt clicert;
  101. mbedtls_pk_context pkey;
  102. struct data_buf_t rx_buf;
  103. } TLSSOCK_CONTEXT;
  104. void mbedtlsSocket_Init(void);
  105. TLSSOCK_HANDLE mbedtlsSocket_Create(HANDLE notify_task, COS_CALLBACK_FUNC_T func, void *funcParam);
  106. int32_t MbedtlsSocket_SetReportFlag(TLSSOCK_HANDLE handle, uint8_t value);
  107. int32_t mbedtlsSocket_Connect(TLSSOCK_HANDLE handle, IP_ADDR ip, uint16_t port);
  108. int32_t mbedtlsSocket_Send(TLSSOCK_HANDLE handle, uint8_t *buf, uint32_t len);
  109. int32_t MbedtlsSocket_Recv(TLSSOCK_HANDLE handle, uint8_t *buf, uint32_t len);
  110. void mbedtlsSocket_Free(TLSSOCK_HANDLE handle);
  111. int32_t mbedtlsSocket_Cfg(TLSSOCK_HANDLE handle, TLSSOCK_CFG_TYPE_E type, uint32_t param);
  112. int32_t mbedtlsSocket_SetSimCid(TLSSOCK_HANDLE handle, uint8_t nSimid, uint8_t nCid);
  113. #endif
  114. #endif