你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

开箱测评-STM32H5内部网络安全SSL测试及联网测试

[复制链接]
guoyuli 发布时间:2023-9-11 10:43
意法半导体公司提供了 ThreadX 的 NETX Duo 支持,且提供了样例程序,该程序支持SSL版本,而且是 TLS 1.2 的版本,目前大多数网站都是使用这个版本。开始计划的测试方法考虑过于简单,以为将 Nx_MQTT_Client 例程迁移到 H563ZI 上运用就可以了,但这个过程非常困难,原因是样例芯片是 STM32H573I 而非 STM32H563ZI,需要重新的设置,设置过程走了很多弯路。 651.jpg 3 S8 R7 j2 }% y. x
1、Cube生成的代码不全,需要自己写很多的代码才可以,主要是三个线程和一个主线程。app_azure_rtos.c 文件不要去动,注意:Cube的代码基本上没有锁死程序的代码;样例中的代码有 Error_Handler(); 的代码,这点需要注意。  f$ ?' a: W- r8 Y: ?5 p, [) N
2、主要 任务在 app_netxduo.c 中进行,主要线程有三个
  1. TX_THREAD AppMQTTClientThread; //MQTT的工作线程
    + N- a  @) y- v  A5 _
  2. TX_THREAD AppSNTPThread;//SNTP的工作线程
    & K! x, y- @0 z
  3. TX_THREAD AppLinkThread;//网卡连接监控线程
复制代码

3 f3 y4 @6 H0 n- k( k  Q这三个线程,其中 AppSNTPThread 线程完成后,启动 AppMQTTClientThread 线程,AppSNTPThread 工作异常,AppMQTTClientThread 不会启动。

8 |5 P6 d' S, |- z5 W. t9 i6 J
0 A& P7 F" j; [& q3 [' T

  1. 8 Y$ F* I8 y4 f/ x& u
  2. /** @brief   SNTP Client thread entry.  I$ s) n/ c3 ~- ]8 K5 Q3 R
  3. * @param thread_input: ULONG user argument used by the thread entry, C$ ^2 V" X( j- y3 k+ U
  4. * @retval none
    - f) J: o# r6 X. o# k- D
  5. */& u3 C$ h9 v) D9 ?! M7 I, U
  6. static VOID App_SNTP_Thread_Entry(ULONG thread_input)
    6 `" v( ?5 Y2 s" ]
  7. {$ z& \# K& K7 p+ e
  8.   UINT ret;
    # {& U( x/ Z6 @0 P
  9.   ULONG  fraction;+ M9 E  D3 [6 M; Q; E
  10.   ULONG  events = 0;
    $ H# N1 K9 p9 A' f
  11.   UINT   server_status;5 X3 r9 L7 r4 _6 B
  12.   NXD_ADDRESS sntp_server_ip;. o) `8 v: B3 s6 s& y
  13. 4 _0 r/ F$ }0 o
  14.   sntp_server_ip.nxd_ip_version = 4;
    3 m" b2 b1 j% q8 H' J

  15. 5 S8 e, @! n& c5 U2 O
  16.   /* Look up SNTP Server address. */
    0 j1 V8 @8 @: W  J* `; `
  17.   ret = nx_dns_host_by_name_get(&DnsClient, (UCHAR *)SNTP_SERVER_NAME, &sntp_server_ip.nxd_ip_address.v4, DEFAULT_TIMEOUT);2 R, J# Z  k4 W8 I. N

  18. - B) g5 r& i! F
  19.   /* Check for error. */. k! ~7 Q' ~# o% ?: x* W
  20.   if (ret != NX_SUCCESS)* P8 v; q! v# y
  21.   {
      Q5 w. f: {/ n
  22.     Error_Handler();
    * s/ ^' o" K" u4 k& K
  23.   }
    9 K1 [. F) r" W: |3 F8 p/ Q! w- h' }+ _

  24. ( l' d6 i$ u( l* c, `% v
  25.   /* Create the SNTP Client */
    8 ?$ `4 b6 y4 e, U$ [6 u; o
  26.   ret =  nx_sntp_client_create(&SntpClient, &NetXDuoEthIpInstance, 0, &NxAppPool, NULL, NULL, NULL);6 d. \* G; v( |/ h
  27. 8 ^# h- w$ B1 R) ]1 |/ X3 {  l
  28.   /* Check for error. */! t& m0 c# D( z6 C0 D, T
  29.   if (ret != NX_SUCCESS)
    # k; J* U1 j, z: U' P% v& S
  30.   {3 q4 i! x/ w/ Q# x7 P8 H+ p
  31.     Error_Handler();. B6 G+ k' `9 x0 N3 v1 M# x
  32.   }) o7 d/ A6 k1 m/ u4 D: b! ]

  33. . q& J2 V7 D1 N
  34.   /* Setup time update callback function. */6 ~) v  I. f9 C
  35.   nx_sntp_client_set_time_update_notify(&SntpClient, time_update_callback);
    , o; a3 F1 m; d5 v8 s- _4 L

  36. - i# e, J( u8 R# u; `) w9 a$ W8 F; L
  37.   /* Use the IPv4 service to set up the Client and set the IPv4 SNTP server. */1 H2 Y7 C4 `6 E8 k+ b7 e. o8 H: C' ]
  38.   ret = nx_sntp_client_initialize_unicast(&SntpClient, sntp_server_ip.nxd_ip_address.v4);) ]: F1 t8 k% x( R) P3 `

  39. 2 k+ R+ B  D0 M/ V
  40.   if (ret != NX_SUCCESS)0 i; Z* G. ]: L( R- Z( e$ u
  41.   {
    # z. y9 ^) I7 F# h$ W
  42.     Error_Handler();
    ) g0 W8 z/ I8 g  [/ D
  43.   }2 o9 U3 C" a! U( F; ^7 u

  44. + h! j6 s2 f9 K* Q$ M* h$ }
  45.   /* Run whichever service the client is configured for. */, f: n& ^# T+ h+ f! m* f% \) w
  46.   ret = nx_sntp_client_run_unicast(&SntpClient);, Z9 {* r6 `: [8 [7 Y2 J/ N

  47. & P, C+ ^/ `5 Q2 ?$ T  ?- W1 \
  48.   if (ret != NX_SUCCESS)
    % x8 k8 d# X2 y3 r1 W
  49.   {) h9 F% X) Z8 O9 ?' z$ ]
  50.     Error_Handler();
    + p0 C. U% x0 ^
  51.   }" B+ U& A& E; t3 C, a- A

  52. * Z9 E% M) {" d0 X
  53.   /* Wait for a server update event. */
    / z! c1 q5 X. S
  54.   tx_event_flags_get(&SntpFlags, SNTP_UPDATE_EVENT, TX_OR_CLEAR, &events, PERIODIC_CHECK_INTERVAL);
    ) b" o3 Y; k9 n2 c" U
  55. 4 M5 u8 x' u- m( |
  56.   if (events == SNTP_UPDATE_EVENT)
    1 i! d# M& _4 d: G5 O; t: K9 k
  57.   {7 u2 w, B- E! |( Y- }/ ~* T
  58.     /* Check for valid SNTP server status. */, S+ T6 l% h* m& H3 d7 s$ ^1 ?! |# u
  59.     ret = nx_sntp_client_receiving_updates(&SntpClient, &server_status);) _( a& n1 `. U  y0 N9 m' f

  60. 0 ?! ^+ W$ h9 Q+ W* t7 d
  61.     if ((ret != NX_SUCCESS) || (server_status == NX_FALSE))6 c3 ^) }' ~" C' R8 U# F
  62.     {
    & l$ |* U3 Y2 ^- q
  63.       /* We do not have a valid update. */
    ( k' e0 f, ~8 }6 Z* E. z
  64.       Error_Handler();( h) A) Z2 E, m* r7 y! o
  65.     }& v/ I) d# W& r
  66.     /* We have a valid update.  Get the SNTP Client time.  */
    8 \# Z; g; D& b% t3 {- Q4 B
  67.     ret = nx_sntp_client_get_local_time_extended(&SntpClient, ¤t_time, &fraction, NX_NULL, 0);
    ' [6 ^/ C8 G3 s6 C. U$ e# p" ?& V

  68. + h& i9 ~& F  \! p5 y
  69.     /* take off 70 years difference */
    2 x  K0 R: J6 V
  70.     current_time -= EPOCH_TIME_DIFF;- J* b/ E9 }1 C+ e7 w/ j, k

  71. 5 C% H- l7 K, R6 M( J) ~
  72.   }
    $ @- f$ T, L# n
  73.   else
    ' V2 K; U! N$ ?1 x
  74.   {
    4 @9 e5 [( a4 ~3 K& z3 I
  75.     //一旦服务异常就锁死程序,所以需要除去错误处理- M' U- Q' m, k3 |( D4 e
  76.     //Error_Handler();2 _2 b8 s7 o  G; j
  77.   }
    1 {' w& R6 x9 u+ |& i1 P

  78. , v/ s0 S2 b9 M  O) X! {8 `* |* W
  79.   /* start the MQTT client thread */
    3 v$ t6 W' O! y7 V' J2 @: f
  80.   //如果出错就不会启动MQTT线程
    4 D9 W" f$ r! i! j3 v; q' B5 a8 @
  81.   tx_thread_resume(&AppMQTTClientThread);
    # M# t: g- e. F# Z$ G

  82. 7 K1 R( [4 ~( F" d7 h
  83. }
复制代码
这个关系很重要,经过很长时间的调试才找到这个坑。3、SSL证书的生成,在 readme.html 文档中有说明。

  1. 2 S' n4 N, h  p5 G
  2. Notes& h+ U# Q! K% d8 S( Q( [
  3. 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 :7 ]- Y" I6 a. o5 s& w, b
  4. download certificate authority CA (in this application “mosquitto.org.der” downloaded from test.mosquitto# K! z  G( V* D0 _6 h% q  {
  5. 0 I0 f' v2 \" v+ T& q
  6. convert certificate downloaded by executing the following cmd from the file downloaded path :6 z2 J0 _* @* X

  7. 3 U- ^6 Y# G. R% ~& m: I" Q
  8.             xxd.exe -i mosquitto.org.der > mosquitto.cert.h
      j, H9 v$ T8 o. g6 W* m
  9. add the converted file under the application : NetXDuo/Nx_MQTT_Client/NetXDuo/App
    $ E0 P9 D$ J& g' g! L

  10. , p7 Z/ k, ?  Z
  11. configure MOSQUITTO_CERT_FILE with your certificate name.
复制代码
xxd.exe 工具是一个 linux 工具,有 windows 版本。证书可以从 mosquitto.org
下载,按照说明操作可以得到证书。& z" @8 d+ t# H8 n- c

5 X4 M2 c5 l/ A! E: y0 d: u6 a 652.png * v% p" \4 `. o2 w. I. ~

1 G9 J' O: f/ C$ m  h4 i. f生成的文件 mosquitto.cert.h 拷贝到 app_netxduo.c 所在的目录。; s2 o" o0 {8 m* C# V
7 K. F$ G( H( |0 [; K
4、将例程中的 app_netxduo.c 和 app_netxduo.h 文件的主要函数复制到目标文件中,需要做些改动,注释掉错误处理。
  1. if (events == SNTP_UPDATE_EVENT)
    0 |- z9 ^1 i  M0 T7 z7 V; ~% V
  2.   {
    ' G2 {( r: s, I: |7 W
  3.     /* Check for valid SNTP server status. */
    0 q7 h: O  J* Q  R( w
  4.     ret = nx_sntp_client_receiving_updates(&SntpClient, &server_status);
    1 P: [0 E7 G! e  u- I0 F5 R( N7 }
  5. ) C7 v; O. Z+ c0 t+ r
  6.     if ((ret != NX_SUCCESS) || (server_status == NX_FALSE))1 @8 l  o8 h% X* w1 v! P* R
  7.     {
    ; D1 d' f4 P8 Q1 _; K2 {8 g
  8.       /* We do not have a valid update. */6 e" B4 a2 z! F+ W7 P
  9.       Error_Handler();% C( O, O) ^  i0 @" p, S* c
  10.     }% g0 z; x1 f2 ?) B8 w" n
  11.     /* We have a valid update.  Get the SNTP Client time.  */
    1 J7 H5 e5 K; v6 N  g$ V( h
  12.     ret = nx_sntp_client_get_local_time_extended(&SntpClient, ¤t_time, &fraction, NX_NULL, 0);
    # q/ w* E9 Z( `

  13. 4 V& H. A' L0 X3 F7 P$ O- I
  14.     /* take off 70 years difference */' \+ t+ l# E2 K3 d9 {6 ]
  15.     current_time -= EPOCH_TIME_DIFF;8 I( Y& l6 r$ `6 v7 }7 _

  16. & V, B6 g  ?9 {9 v6 Y7 \/ ^9 x
  17.   }
    2 M" E: e7 N! F" r
  18.   else
    - G( R2 t1 L6 A' q( K
  19.   {9 i3 v% z6 o& T+ K
  20.     //Error_Handler();$ w! A7 Y, D! N+ h. q
  21.   }
复制代码
5、编译程序运行。可以看到已经连接到了测试 MQTT 服务器了。
) _$ Z$ m" k) W# `, e7 \# m
8 Q* O( K# L9 ?# M: _7 y 656.jpg
0 s/ t4 L+ j/ ]8 Y2 \  D7 r- W  N9 R" C5 E5 L$ z9 `$ J9 p1 T
来源:EEWORLD论坛网友 bigbat 版权归原作者所有4 G/ I9 w# E+ U, a, h3 Z

# S# |3 z7 c3 S
$ y' Z# t% L0 G& Z- ~; Y
7 q3 u8 a( z( T3 r0 B' o1 d1 }
收藏 评论0 发布时间:2023-9-11 10:43

举报

0个回答
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版