在arm上用mbedtls开发ssl/tls客户端时,与服务器进行ssl握手出现了问题 现在是客户端能成功发送client hello到服务器,服务器回应的 server hello 以及后面的信息 客户端收不到。我打开了mbedtls的debug,日志信息如下:
已成功连接远程服务器
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8771: => handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:3818: client state: 0
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3060: => flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3072: <= flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:3818: client state: 1
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3060: => flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3072: <= flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0825: => write client hello
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0862: client hello, max version: [3:3]
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0872: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0872: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0872: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0925: client hello, session id len.: 0
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0926: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0972: client hello, add ciphersuite: c02c
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0985: client hello, got 1 ciphersuites (excluding SCSVs)
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0994: adding EMPTY_RENEGOTIATION_INFO_SCSV
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1043: client hello, compress len.: 1
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1044: client hello, compress alg.: 0
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0228: client hello, adding signature_algorithms extension
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0313: client hello, adding supported_elliptic_curves extension
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:0378: client hello, adding supported_point_formats extension
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1121: client hello, total extension length: 28
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3489: => write handshake message
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3648: => write record
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3725: output record: msgtype = 22, version = [3:3], msglen = 77
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3730: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3730: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3730: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3730: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3730: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3730: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3730: 韊D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3060: => flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3078: message length: 82, out_left: 82
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3084: message length: 82, out_left: 82
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3112: <= flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3781: <= write record
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3625: <= write handshake message
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1157: <= write client hello
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:3818: client state: 2
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3060: => flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3072: <= flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1550: => parse server hello
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:4616: => read record
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:2841: => fetch input
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3001: in_left: 0, nb_want: 5
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3025: in_left: 0, nb_want: 5
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3027: in_left: 0, nb_want: 5
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3037: f_recv returned 1146 bytes but only 5 were requested
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:5278: re requested
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:4649: => read record
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1557: => parse server hello
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8781: <= handshake
failed
! mbedtls_ssl_handshake returned -0x6c00
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8771: => handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:3818: client state: 2
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3060: => flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3072: <= flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1550: => parse server hello
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:4616: => read record
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:2841: => fetch input
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3001: in_left: 0, nb_want: 5
当前数据为空
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3025: in_left: 0, nb_want: 5
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8781: <= handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8771: => handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:3818: client state: 2
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3060: => flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3072: <= flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1550: => parse server hello
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:4616: => read record
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:2841: => fetch input
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3001: in_left: 0, nb_want: 5
当前数据为空
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3025: in_left: 0, nb_want: 5
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8781: <= handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8771: => handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:3818: client state: 2
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3060: => flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3072: <= flush output
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:1550: => parse server hello
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:4616: => read record
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:2841: => fetch input
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3001: in_left: 0, nb_want: 5
当前数据为空
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:3025: in_left: 0, nb_want: 5
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8781: <= handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_tls.c:8771: => handshake
D:\Keil5\Packs\ARM\mbedTLS\1.6.1\library\ssl_cli.c:3818: client state: 2
后面就是一直循环,我在想是不是我接收函数出了问题,我是用W5500来进行通讯的,在移植mbedtls前 W5500能正常发送和接收数据。
接收函数如下:
int mbedtls_ssl_recv(void *ctx, unsigned char *buf, size_t len)
{
// ctx = ctx;
uint16_t rx_size = Read_SOCK_Data_Buffer(0, buffer);
memcpy(buf, buffer, rx_size);
len = rx_size;
if(rx_size == 0) return MBEDTLS_ERR_SSL_WANT_READ;
return (int) rx_size;
}
Read_SOCK_Data_Buffer函数是把读取的内容发送到buffer中,返回大小。
进行握手的代码如下:
uint8_t Socket_Connect(SOCKET sn, HexRecvBuff hex_buffer)
{
//printf("当前为TCP客户端模式 正在发起连接......\r\n");
/*通过不同的状态 来进行相应的操作*/
switch(Read_W5500_SOCK_1Byte(sn, Sn_SR))
{
case SOCK_CLOSED: //处于Socket sn关闭状态
Write_W5500_SOCK_1Byte(sn, Sn_CR, CLOSE); //先关闭
Write_W5500_SOCK_1Byte(sn, Sn_MR, MR_TCP); //设置为TCP模式
Write_W5500_SOCK_1Byte(sn, Sn_CR, OPEN); //打开Sokcet 0
return FALSE;
case SOCK_INIT: //此时对应的socket已经打开 开始连接
Write_W5500_SOCK_1Byte(sn, Sn_CR, CONNECT); //开始连接
ssl_init(); //初始化ssl
return FALSE;
case SOCK_ESTABLISHED: //此时已连接 开始处理
printf("已成功连接远程服务器\r\n");
int ret = 0;
while(Read_W5500_SOCK_1Byte(sn, Sn_SR) == SOCK_ESTABLISHED) //当服务器有数据时
{
while((ret = mbedtls_ssl_handshake_await()) != 0)
{
if(ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
{
printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret);
}
}
if(W5500_Interrupt) //发生W5500中断
{
W5500_Interrupt_Process();
}
// if((S0_Data & S_RECEIVE) == S_RECEIVE) //S0接收到了数据
// {
// S0_Data = 0;
// Process_Socket_Data(0); //处理W5500接收到的数据
// }
if(usart_state == 1) //当从串口助手发送消息给W5500时
{
//W5500_HttpGetJson();
//W5500_HttpGet();
mbedtls_ssl_send_data(Send_buf, sizeof(Send_buf));
hex_buffer = recvFromHexQueue();
}
}
return TRUE;
default:
break;
}
return FALSE;
}