ssl_socket.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <cfw.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include "sockets.h"
  7. #include "lwip/tcp.h"
  8. #include "lwip/api.h"
  9. #include "lwip/netdb.h"
  10. //#include "netif.h"
  11. #include "mbedtls/ssl.h"
  12. #include "mbedtls/net_sockets.h"
  13. #include "mbedtls/entropy.h"
  14. #include "mbedtls/ctr_drbg.h"
  15. #include "mbedtls/timing.h"
  16. #include "mbedtls/debug.h"
  17. #include "mbedtls/platform.h"
  18. #define AT_TCPIP_HOSTLEN 255
  19. #define TLS_CLIENT_MAX_NUM 11
  20. #define TLS_CONTEXT_MAX_NUM 6
  21. typedef enum
  22. {
  23. EV_CFW_TLS_CREATE_REQ = 0x2000,
  24. EV_CFW_TLS_CONNECT_REQ,
  25. EV_CFW_TLS_FREE_REQ,
  26. EV_CFW_TLS_CLOSE_REQ,
  27. EV_CFW_TLS_INTERNAL_READ_REQ
  28. } CFW_TLSSOCK_REQ;
  29. typedef enum
  30. {
  31. TLS_CFG_TYPE_VERSION,
  32. TLS_CFG_TYPE_CIPHER_SUITE,
  33. TLS_CFG_TYPE_AUTHMODE,
  34. TLS_CFG_TYPE_CA,
  35. TLS_CFG_TYPE_CLI_CERT,
  36. TLS_CFG_TYPE_CLI_KEY,
  37. TLS_CFG_TYPE_IGNORE_TIME,
  38. TLS_CFG_TYPE_NEGO_TIMEOUT
  39. } TLS_CFG_TYPE_E;
  40. typedef enum
  41. {
  42. TLS_STATE_INITIAL,
  43. TLS_STATE_OPENING,
  44. TLS_STATE_CONNECTED,
  45. TLS_STATE_CLOSING,
  46. TLS_STATE_PEER_REFUSED,
  47. } TLS_STATE_NUM;
  48. typedef enum
  49. {
  50. EV_CFW_SSL_CONNECT_RSP_EVENT_IND,
  51. EV_CFW_SSL_CLOSE_RSP_EVENT_IND,
  52. EV_CFW_SSL_SENT_RSP_EVENT_IND,
  53. EV_CFW_SSL_DATA_EVENT_IND,
  54. } SOCK_TYPE;
  55. #define PLAIN_TEXT_RINGBUF_SIZE 1024
  56. typedef struct _PLAIN_RING_BUF
  57. {
  58. uint32_t put;
  59. uint32_t get;
  60. uint8_t buf[PLAIN_TEXT_RINGBUF_SIZE];
  61. } PLAIN_RING_BUF;
  62. typedef struct _CLIENT_CONTEXT
  63. {
  64. uint32_t ssid; //AT
  65. uint32_t isUsed;
  66. uint8_t isDtls;
  67. TLS_STATE_NUM state; //AT
  68. //uint8_t psk_id[128];
  69. //uint8_t psk[128];
  70. char uaIPAddress[AT_TCPIP_HOSTLEN]; //AT
  71. uint8_t nCid; //at
  72. uint8_t nSimid; //at
  73. uint16_t fport; //AT
  74. osiThread_t *notify_task; //AT AT_TASK
  75. osiCallback_t notify_callback; //AT SSL_RSP
  76. void *funcParam; //AT ENGINE
  77. uint32_t access_mode; //AT
  78. osiTimer_t *sendTimerID; //AT transparent mode
  79. int32_t uSocket; //SSL
  80. osiSemaphore_t *tlsbuf_sem; //SSL
  81. mbedtls_ssl_context ssl; //SSL
  82. mbedtls_net_context server_fd; //SSL
  83. mbedtls_entropy_context entropy; //SSL
  84. mbedtls_ctr_drbg_context ctr_drbg; //SSL
  85. mbedtls_ssl_config conf; //SSL
  86. mbedtls_ssl_session savedSession; //SSL
  87. PLAIN_RING_BUF databuf; //SSL
  88. uint32_t dataEventHandler; //SSL
  89. uint32_t authmode;
  90. mbedtls_x509_crt cacert;
  91. //int32_t sock_id;
  92. mbedtls_x509_crt clicert;
  93. mbedtls_pk_context pkey;
  94. mbedtls_timing_delay_context *timer;
  95. //struct data_buf_t rx_buf;
  96. int32_t sentSize;
  97. } CLIENT_CONTEXT;
  98. typedef struct _TLS_CONTEXT
  99. {
  100. uint32_t timeout;
  101. uint32_t ignore_time;
  102. uint32_t authmode;
  103. uint32_t ciphersuit;
  104. uint32_t min_ver;
  105. uint32_t max_ver;
  106. uint32_t ver;
  107. //struct data_buf_t rx_buf;
  108. } TLS_CONTEXT;
  109. void tls_Init(void);
  110. int32_t tls_SetSimCid(uint32_t client_id, uint8_t nSimid, uint8_t nCid);
  111. void tls_Close(uint32_t client_id);
  112. int32_t tls_Recv(uint32_t client_id, uint8_t *buf, uint32_t len);
  113. int32_t tls_Send(uint32_t client_id, uint8_t *buf, uint32_t len);
  114. int32_t tls_Connect(uint32_t client_id);
  115. int32_t tls_Cfg(uint32_t ssid, TLS_CFG_TYPE_E type, uint32_t param);
  116. int tls_Setup(uint32_t ssid, uint32_t client_id, osiCallback_t func, void *funcParam, uint8_t isDtls, char *psk_id, char *psk);