【STM32H5开发板】网络安全SSL测试及联网测试
[复制链接]
本帖最后由 bigbat 于 2023-5-24 11:54 编辑
ST公司只提供了ThreadX的NETX Duo的支持,而且ST提供了样例程序,这个程序还是支持SSL的版本,而且是TLS 1.2的版本,目前大多数的网站都是使用这个版本。1.3的版本只是在流程上做了修改,去除了1.2版的降级过程,使得1.3更加的安全,但是牺牲了兼容性,不过这个在嵌入式中也用不到。
开始想的比较简单,将Nx_MQTT_Client的例程迁移到H563ZI上运用就可以了,但是这个过程确非常的困难,主要的困难是样例的芯片是STM32H573I和目标的芯片不同,这就需要重新的设置,这个过程不是把设置弄成一样的就可以地,这期间走了很多弯路。下面就给大家填一下我遇到的坑。
1、Cube生成的代码不是很全,需要自己写很多的代码才可以,主要是三个线程和一个主线程。app_azure_rtos.c文件不要去动,注意:Cube的代码基本上严重的地方基本上没有锁死程序的代码。样例中的代码有多出错误处理是锁死地(就是样例中有Error_Handler();的地方),这点需要注意。
2、主要的任务在app_netxduo.c中进行,主要的线程有三个
TX_THREAD AppMQTTClientThread; //MQTT的工作线程
TX_THREAD AppSNTPThread;//SNTP的工作线程
TX_THREAD AppLinkThread;//网卡连接监控线程
这三个线程,其中AppSNTPThread线程完成后,启动AppMQTTClientThread线程,所以这里有一个大坑,就是AppSNTPThread工作异常,AppMQTTClientThread不会启动。
/** @brief SNTP Client thread entry.
* @param thread_input: ULONG user argument used by the thread entry
* @retval none
*/
static VOID App_SNTP_Thread_Entry(ULONG thread_input)
{
UINT ret;
ULONG fraction;
ULONG events = 0;
UINT server_status;
NXD_ADDRESS sntp_server_ip;
sntp_server_ip.nxd_ip_version = 4;
/* Look up SNTP Server address. */
ret = nx_dns_host_by_name_get(&DnsClient, (UCHAR *)SNTP_SERVER_NAME, &sntp_server_ip.nxd_ip_address.v4, DEFAULT_TIMEOUT);
/* Check for error. */
if (ret != NX_SUCCESS)
{
Error_Handler();
}
/* Create the SNTP Client */
ret = nx_sntp_client_create(&SntpClient, &NetXDuoEthIpInstance, 0, &NxAppPool, NULL, NULL, NULL);
/* Check for error. */
if (ret != NX_SUCCESS)
{
Error_Handler();
}
/* Setup time update callback function. */
nx_sntp_client_set_time_update_notify(&SntpClient, time_update_callback);
/* Use the IPv4 service to set up the Client and set the IPv4 SNTP server. */
ret = nx_sntp_client_initialize_unicast(&SntpClient, sntp_server_ip.nxd_ip_address.v4);
if (ret != NX_SUCCESS)
{
Error_Handler();
}
/* Run whichever service the client is configured for. */
ret = nx_sntp_client_run_unicast(&SntpClient);
if (ret != NX_SUCCESS)
{
Error_Handler();
}
/* Wait for a server update event. */
tx_event_flags_get(&SntpFlags, SNTP_UPDATE_EVENT, TX_OR_CLEAR, &events, PERIODIC_CHECK_INTERVAL);
if (events == SNTP_UPDATE_EVENT)
{
/* Check for valid SNTP server status. */
ret = nx_sntp_client_receiving_updates(&SntpClient, &server_status);
if ((ret != NX_SUCCESS) || (server_status == NX_FALSE))
{
/* We do not have a valid update. */
Error_Handler();
}
/* We have a valid update. Get the SNTP Client time. */
ret = nx_sntp_client_get_local_time_extended(&SntpClient, ¤t_time, &fraction, NX_NULL, 0);
/* take off 70 years difference */
current_time -= EPOCH_TIME_DIFF;
}
else
{
//一旦服务异常就锁死程序,所以需要除去错误处理
//Error_Handler();
}
/* start the MQTT client thread */
//如果出错就不会启动MQTT线程
tx_thread_resume(&AppMQTTClientThread);
}
这个关系很重要,经过很长时间的调试才找到这个坑。
3、SSL证书的生成,这个在readme.html文档中有说明。
Notes
To make an encrypted connection with MQTT server, user should follow these steps to add an x509 certificate to the mqtt_client and use it to ensure server’s authentication :
download certificate authority CA (in this application “mosquitto.org.der” downloaded from test.mosquitto
convert certificate downloaded by executing the following cmd from the file downloaded path :
xxd.exe -i mosquitto.org.der > mosquitto.cert.h
add the converted file under the application : NetXDuo/Nx_MQTT_Client/NetXDuo/App
configure MOSQUITTO_CERT_FILE with your certificate name.
xxd.exe工具是一个linux工具,这个工具有windows的版本。证书可以从mosquitto.org下载,按照说明操作可以得到证书。
生成的文件mosquitto.cert.h拷贝到app_netxduo.c所在的目录。
4、将例程中的app_netxduo.c和app_netxduo.h文件主要的函数复制到目标文件中。我没有原样的照抄,不过好像是一模一样的。主要的改动是注释掉错误处理。
if (events == SNTP_UPDATE_EVENT)
{
/* Check for valid SNTP server status. */
ret = nx_sntp_client_receiving_updates(&SntpClient, &server_status);
if ((ret != NX_SUCCESS) || (server_status == NX_FALSE))
{
/* We do not have a valid update. */
Error_Handler();
}
/* We have a valid update. Get the SNTP Client time. */
ret = nx_sntp_client_get_local_time_extended(&SntpClient, ¤t_time, &fraction, NX_NULL, 0);
/* take off 70 years difference */
current_time -= EPOCH_TIME_DIFF;
}
else
{
//Error_Handler();
}
5、编译程序运行。可以看到已经连接到了测试MQTT服务器了。
|