无法使用 mbedtls 示例客户端连接到 https 服务器
Cannot connect to https server using mbedtls example client
EDIT:我在电路板和我的计算机上使用 python SSL 服务器使用静态 IP 进行了测试,它按预期工作,让我相信DHCP是问题所在。如果有人对可能发生的事情有线索,我们将不胜感激。
我在 STM32F746-NUCLEO 板上使用 mbedTLS 库,我想将它用作 SSL 客户端和服务器。服务器运行良好,所以我尝试使用客户端示例代码(原样,在一个单独的项目中)。
下面的mbedtls_net_connect
调用returns -68 (MBEDTLS_ERR_NET_CONNECT_FAILED)。深入挖掘表明这是由于路由错误(LwIP tcp.c 中的第 900 行),因为 local_ip 设置为 0。该板在连接到的家庭路由器上处于 DHCP 模式互联网。目标服务器已启动并且 运行 并且 SERVER_NAME
是纯文本的 IP 地址。
mbedtls_entropy_context client_entropy;
static mbedtls_net_context server_fd;
mbedtls_x509_crt cacert;
static uint32_t flags;
static uint8_t vrfy_buf[512];
static const uint8_t* client_pers = "ssl_client";
mbedtls_ssl_config client_config;
mbedtls_ctr_drbg_context client_ctr_drbg;
mbedtls_ssl_context client_ssl;
static uint8_t client_buf[1024];
void SSL_Server(void const *argument) {
int ret, len;
UNUSED(argument);
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&client_ssl);
mbedtls_ssl_config_init(&client_config);
mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&client_ctr_drbg);
// Seeding the random number generator
mbedtls_entropy_init( &client_entropy );
len = strlen((char *) client_pers);
if((ret = mbedtls_ctr_drbg_seed(&client_ctr_drbg, mbedtls_entropy_func,
&client_entropy, (const unsigned char *) client_pers, len)) != 0)
{
goto exit;
}
// 1. Initialize certificates
ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
mbedtls_test_cas_pem_len );
if( ret < 0 )
{
goto exit;
}
if((ret = mbedtls_net_connect(&server_fd, SERVER_NAME, SERVER_PORT,
MBEDTLS_NET_PROTO_TCP)) != 0)
{
mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
goto exit;
}
}
这里的SSL_Server函数是在main()
中调用的FreeRTOS线程。我还可以确认发生错误时网络接口已分配了 IP 地址。
我希望连接调用 return 0 并连接到服务器以启动 SSL 握手。
您需要为 LWIP 设置默认 netif 路由才能路由远程地址。
只需在函数 mbedtls_net_init()
.
内的 dhcp_start()
之后添加 netif_set_default(&netif);
void mbedtls_net_init( mbedtls_net_context *ctx ) {
...
/* add the network interface */
netif_add(&netif, &addr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/* register the default network interface */
netif_set_up(&netif);
#ifdef USE_DHCP
netif.ip_addr.addr = 0;
dhcp_start(&netif);
#endif
netif_set_default(&netif); // <-- Here
osDelay(500);
start = HAL_GetTick();
while((netif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000))
{
}
if (netif.ip_addr.addr == 0) {
printf(" Failed to get ip address! Please check your network configuration.\n");
Error_Handler();
}
...
MbedTLS 的文档可能有点棘手,希望对您有所帮助。
EDIT:我在电路板和我的计算机上使用 python SSL 服务器使用静态 IP 进行了测试,它按预期工作,让我相信DHCP是问题所在。如果有人对可能发生的事情有线索,我们将不胜感激。
我在 STM32F746-NUCLEO 板上使用 mbedTLS 库,我想将它用作 SSL 客户端和服务器。服务器运行良好,所以我尝试使用客户端示例代码(原样,在一个单独的项目中)。
下面的mbedtls_net_connect
调用returns -68 (MBEDTLS_ERR_NET_CONNECT_FAILED)。深入挖掘表明这是由于路由错误(LwIP tcp.c 中的第 900 行),因为 local_ip 设置为 0。该板在连接到的家庭路由器上处于 DHCP 模式互联网。目标服务器已启动并且 运行 并且 SERVER_NAME
是纯文本的 IP 地址。
mbedtls_entropy_context client_entropy;
static mbedtls_net_context server_fd;
mbedtls_x509_crt cacert;
static uint32_t flags;
static uint8_t vrfy_buf[512];
static const uint8_t* client_pers = "ssl_client";
mbedtls_ssl_config client_config;
mbedtls_ctr_drbg_context client_ctr_drbg;
mbedtls_ssl_context client_ssl;
static uint8_t client_buf[1024];
void SSL_Server(void const *argument) {
int ret, len;
UNUSED(argument);
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&client_ssl);
mbedtls_ssl_config_init(&client_config);
mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&client_ctr_drbg);
// Seeding the random number generator
mbedtls_entropy_init( &client_entropy );
len = strlen((char *) client_pers);
if((ret = mbedtls_ctr_drbg_seed(&client_ctr_drbg, mbedtls_entropy_func,
&client_entropy, (const unsigned char *) client_pers, len)) != 0)
{
goto exit;
}
// 1. Initialize certificates
ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
mbedtls_test_cas_pem_len );
if( ret < 0 )
{
goto exit;
}
if((ret = mbedtls_net_connect(&server_fd, SERVER_NAME, SERVER_PORT,
MBEDTLS_NET_PROTO_TCP)) != 0)
{
mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
goto exit;
}
}
这里的SSL_Server函数是在main()
中调用的FreeRTOS线程。我还可以确认发生错误时网络接口已分配了 IP 地址。
我希望连接调用 return 0 并连接到服务器以启动 SSL 握手。
您需要为 LWIP 设置默认 netif 路由才能路由远程地址。
只需在函数 mbedtls_net_init()
.
dhcp_start()
之后添加 netif_set_default(&netif);
void mbedtls_net_init( mbedtls_net_context *ctx ) {
...
/* add the network interface */
netif_add(&netif, &addr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/* register the default network interface */
netif_set_up(&netif);
#ifdef USE_DHCP
netif.ip_addr.addr = 0;
dhcp_start(&netif);
#endif
netif_set_default(&netif); // <-- Here
osDelay(500);
start = HAL_GetTick();
while((netif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000))
{
}
if (netif.ip_addr.addr == 0) {
printf(" Failed to get ip address! Please check your network configuration.\n");
Error_Handler();
}
...
MbedTLS 的文档可能有点棘手,希望对您有所帮助。