1.首先保证自己的程序已经实现了普通TCP连接通信
为了保证此篇文章能够让所有人都能应用,我就假设我的程序里面已经实现了TCP连接\
然后规定:
/*假设接收网络数据函数*/
int net_recv(char *data);
/*假设TCP发送数据函数*/
int net_send(char *data,int len);
2.按照上一节 把MBEDTLS包添加到工程
4.编译一下
5.选择C99模式
6.再编译一下
7.增加自己的随机数函数 和 时间戳返回函数
/*随机数函数*/
int mbedtls_hardware_poll( void *data,
unsigned char *output, size_t len, size_t *olen )
{
unsigned long randomValue = ((rand()*20) + 1000);//生成随机数
((void) data);
*olen = 0;
if( len < sizeof(unsigned long) ) return( 0 );
memcpy( output, &randomValue, sizeof(unsigned long) );
*olen = sizeof(unsigned long);
return 0;
}
/**
* @brief 时间函数(SSL底层会调用时间验证证书是否过期)
**/
//struct tm *lcTime;
//time_t startTime;
// lcTime = localtime (&startTime);
_ARMABI time_t time(time_t *t)
{
// time_t it;
if (t) {
return *t;
}
else
{
// startTime = 0;
// lcTime = localtime (&startTime);
// it = mktime(lcTime);
// return it ;
return 0;
}
}
12,连接上TCP以后,等待SSL握手成功
13,发送和接收数据
3.如果不能连接,可以打开DEBUG
#define MBEDTLS_DEBUG_C
4.配置DEBUG
/*设置 debug 输出函数*/
static void my_debug( void *ctx, int level,
const char *file, int line, const char *str )
{
printf("%s:%04d: %s\r\n", file, line, str );
}