SSL/TLS
If you are not familiar with SSL/TLS and feel that all kinds of certificates are very confusing, you can refer to this article to generate certificate
example:demo/ssl
1. Enumerated type
1.1. SSL_Version_t
SSL/TLS version
typedef enum{
SSL_VERSION_SSLv3 = 0,
SSL_VERSION_TLSv1 ,
SSL_VERSION_TLSv1_1 ,
SSL_VERSION_TLSv1_2 ,
SSL_VERSION_MAX
}SSL_Version_t;
1.2. SSL_Error_t
error message
typedef enum{
SSL_ERROR_NONE = 0x00 ,
SSL_ERROR_PARAM = -0x01 ,
SSL_ERROR_PARSE = -0x02 ,
SSL_ERROR_MALLOC_FAIL= -3 ,
SSL_ERROR_CONNECTION = -4 ,
SSL_ERROR_TIMEOUT = -5 ,
SSL_ERROR_FAIL = -6 ,
SSL_ERROR_INTERNAL ,
SSL_ERROR_MAX
} SSL_Error_t;
1.3. SSL_Verify_Mode_t
Validation mode
typedef enum{
SSL_VERIFY_MODE_NONE = 0,
SSL_VERIFY_MODE_OPTIONAL = 1,
SSL_VERIFY_MODE_REQUIRED = 2,
SSL_VERIFY_MODE_MAX
}SSL_Verify_Mode_t;
2. Struct
2.1. SSL_Config_t
SSL/TLS configuration
typedef struct{
const char* caCert;
const char* caCrl;
const char* clientCert;
const char* clientKey;
const char* clientKeyPasswd;
const char* hostName;
SSL_Version_t minVersion;
SSL_Version_t maxVersion;
SSL_Verify_Mode_t verifyMode;
const char* entropyCustom; //自定义混淆字符
//////////////////////////////////
void* obj;//do not edit it
//////////////////////////////////
} SSL_Config_t;
3. Function
3.1. SSL_Init
SSL_Error_t SSL_Init(SSL_Config_t* sslConfig);
Function
Init SSL/TLS
Parameters
- sslConfig:Initialization configuration parameters
Return
- Return error information
3.2. SSL_Connect
SSL_Error_t SSL_Connect(SSL_Config_t* sslConfig, const char* server, const char* port);
Function
Connect the server with the SSL/TLS protocol
Parameters
- sslConfig:SSL/TLS configuration information
- server: SSL/TLS server that needs to be connected
- port: server port
Return
- Return error information
3.3. SSL_Write
int SSL_Write(SSL_Config_t* sslConfig, uint8_t* data, int length, int timeoutMs);
Function
SSL/TLS Sending data (blocking)
Parameters
- sslConfig:SSL/TLS configuration
- data: data sent to the server
- the length of the data sent to the server by length:
- timeoutMs: sends timeout time (reserved, temporarily unavailable)
Return
- Data length or error code that is successfully sent
3.4. SSL_Read
int SSL_Read(SSL_Config_t* sslConfig, uint8_t* data, int length, int timeoutMs);
Function
Read the data
Parameters
- sslConfig:SSL/TLS configuration
- data: received data
- the length of the data received by length:
- timeoutMs: receives timeout time
Return
- The data length or error code that is successfully received
3.5. SSL_Close
SSL_Error_t SSL_Close(SSL_Config_t* sslConfig);
Function
Close connection
Parameters
- sslConfig:SSL/TLS configuration
Return
- error message
3.6. SSL_Destroy
SSL_Error_t SSL_Destroy(SSL_Config_t* sslConfig);
Function
Release the space occupied by SSL/TLS
Parameters
- sslConfig:SSL/TLS configuration
Return
- error code of release