• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 /* BEGIN_HEADER */
17 
18 #include <stdio.h>
19 #include "stub_replace.h"
20 #include "hitls.h"
21 #include "hitls_config.h"
22 #include "hitls_error.h"
23 #include "bsl_uio.h"
24 #include "bsl_sal.h"
25 #include "tls.h"
26 #include "hlt.h"
27 #include "hlt_type.h"
28 #include "hs_ctx.h"
29 #include "pack.h"
30 #include "send_process.h"
31 #include "frame_link.h"
32 #include "frame_tls.h"
33 #include "frame_io.h"
34 #include "simulate_io.h"
35 #include "parser_frame_msg.h"
36 #include "cert.h"
37 #include "process.h"
38 #include "securec.h"
39 #include "session_type.h"
40 #include "rec_wrapper.h"
41 #include "common_func.h"
42 #include "conn_init.h"
43 #include "hs_extensions.h"
44 #include "hitls_crypt_init.h"
45 #include "alert.h"
46 #include "record.h"
47 #include "hs_kx.h"
48 #include "bsl_log.h"
49 #include "cert_callback.h"
50 /* END_HEADER */
51 
52 #define PORT 23456
53 #define READ_BUF_SIZE (18 * 1024)
54 #define ALERT_BODY_LEN 2u
55 
56 typedef struct {
57     uint16_t version;
58     BSL_UIO_TransportType uioType;
59     HITLS_Config *s_config;
60     HITLS_Config *c_config;
61     FRAME_LinkObj *client;
62     FRAME_LinkObj *server;
63     HITLS_Session *clientSession;       // Set the session to the client for session recovery.
64     HITLS_TicketKeyCb serverKeyCb;
65 } ResumeTestInfo;
66 
67 typedef struct{
68     char *ClientCipherSuite;
69     char *ServerCipherSuite;
70     char *ClientGroup;
71     char *ServerGroup;
72     uint8_t ClientKeyExchangeMode;
73     uint8_t ServerKeyExchangeMode;
74     uint8_t psk[PSK_MAX_LEN];
75     bool SetNothing;
76     bool SuccessOrFail;
77 } SetInfo;
78 
SetConfig(HLT_Ctx_Config * clientconfig,HLT_Ctx_Config * serverconfig,SetInfo setInfo)79 void SetConfig(HLT_Ctx_Config *clientconfig, HLT_Ctx_Config *serverconfig, SetInfo setInfo)
80 {
81     if ( !setInfo.SetNothing ) {
82 
83         // Configure the server configuration.
84         if (setInfo.ServerCipherSuite != NULL) {
85             HLT_SetCipherSuites(serverconfig, setInfo.ServerCipherSuite);
86         }
87         if (setInfo.ServerGroup != NULL) {
88             HLT_SetGroups(serverconfig, setInfo.ServerGroup);
89         }
90         memcpy_s(serverconfig->psk, PSK_MAX_LEN, setInfo.psk, sizeof(setInfo.psk));
91 
92         if ( (setInfo.ClientKeyExchangeMode & (TLS13_KE_MODE_PSK_WITH_DHE | TLS13_KE_MODE_PSK_ONLY)) != 0) {
93             clientconfig->keyExchMode = setInfo.ClientKeyExchangeMode;
94         }
95 
96         // Configure the client configuration.
97         if (setInfo.ClientCipherSuite != NULL) {
98             HLT_SetCipherSuites(clientconfig, setInfo.ClientCipherSuite);
99         }
100         if (setInfo.ClientGroup != NULL) {
101             HLT_SetGroups(clientconfig, setInfo.ClientGroup);
102         }
103         memcpy_s(clientconfig->psk, PSK_MAX_LEN, setInfo.psk, sizeof(setInfo.psk));
104         if ( (setInfo.ServerKeyExchangeMode & (TLS13_KE_MODE_PSK_WITH_DHE | TLS13_KE_MODE_PSK_ONLY)) != 0) {
105             serverconfig->keyExchMode = setInfo.ServerKeyExchangeMode;
106         }
107     }
108 }
DoHandshake(ResumeTestInfo * testInfo)109 static int32_t DoHandshake(ResumeTestInfo *testInfo)
110 {
111     /* Construct a connection. */
112     testInfo->client = FRAME_CreateLink(testInfo->c_config, testInfo->uioType);
113     if (testInfo->client == NULL) {
114         return HITLS_INTERNAL_EXCEPTION;
115     }
116 
117     if (testInfo->clientSession != NULL) {
118         int32_t ret = HITLS_SetSession(testInfo->client->ssl, testInfo->clientSession);
119         if (ret != HITLS_SUCCESS) {
120             return ret;
121         }
122     }
123 
124     testInfo->server = FRAME_CreateLink(testInfo->s_config, testInfo->uioType);
125     if (testInfo->server == NULL) {
126         return HITLS_INTERNAL_EXCEPTION;
127     }
128 
129     return FRAME_CreateConnection(testInfo->client, testInfo->server, true, HS_STATE_BUTT);
130 }
131 
ClientCreatConnectWithPara(HLT_FrameHandle * handle,SetInfo setInfo)132 void ClientCreatConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
133 {
134     HLT_Tls_Res *serverRes = NULL;
135     HLT_Tls_Res *clientRes = NULL;
136     HLT_Process *localProcess = NULL;
137     HLT_Process *remoteProcess = NULL;
138     HLT_Ctx_Config *serverConfig = NULL;
139     HLT_Ctx_Config *clientConfig = NULL;
140 
141     // Create a process.
142     localProcess = HLT_InitLocalProcess(HITLS);
143     ASSERT_TRUE(localProcess != NULL);
144     remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, false);
145     ASSERT_TRUE(remoteProcess != NULL);
146 
147     // The local client and remote server listen on the TLS connection.
148     serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
149     ASSERT_TRUE(serverConfig != NULL);
150     clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
151     ASSERT_TRUE(clientConfig != NULL);
152     // Configure the config file.
153     SetConfig(clientConfig, serverConfig, setInfo);
154 
155     // Listening connection.
156     serverRes = HLT_ProcessTlsAccept(remoteProcess, TLS1_3, serverConfig, NULL);
157     ASSERT_TRUE(serverRes != NULL);
158 
159     clientRes = HLT_ProcessTlsInit(localProcess, TLS1_3, clientConfig, NULL);
160     ASSERT_TRUE(clientRes != NULL);
161 
162     // Configure the interface for constructing abnormal messages.
163     handle->ctx = clientRes->ssl;
164     ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
165 
166     // Establish a connection.
167     if ( setInfo.SuccessOrFail ) {
168        ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) == 0);
169     }else {
170         ASSERT_TRUE(HLT_TlsConnect(clientRes->ssl) != 0);
171     }
172 
173 EXIT:
174     HLT_CleanFrameHandle();
175     HLT_FreeAllProcess();
176     return;
177 }
178 
ServerCreatConnectWithPara(HLT_FrameHandle * handle,SetInfo setInfo)179 void ServerCreatConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
180 {
181     HLT_Tls_Res *serverRes = NULL;
182     HLT_Tls_Res *clientRes = NULL;
183     HLT_Process *localProcess = NULL;
184     HLT_Process *remoteProcess = NULL;
185     HLT_Ctx_Config *serverConfig = NULL;
186     HLT_Ctx_Config *clientConfig = NULL;
187 
188     // Create a process.
189     localProcess = HLT_InitLocalProcess(HITLS);
190     ASSERT_TRUE(localProcess != NULL);
191     remoteProcess = HLT_LinkRemoteProcess(HITLS, TCP, PORT, false);
192     ASSERT_TRUE(remoteProcess != NULL);
193 
194     // The local client and remote server listen on the TLS connection.
195     serverConfig = HLT_NewCtxConfig(NULL, "SERVER");
196     ASSERT_TRUE(serverConfig != NULL);
197     clientConfig = HLT_NewCtxConfig(NULL, "CLIENT");
198     ASSERT_TRUE(clientConfig != NULL);
199     // Configure the config file.
200     SetConfig(clientConfig, serverConfig, setInfo);
201 
202     serverRes = HLT_ProcessTlsAccept(localProcess, TLS1_3, serverConfig, NULL);
203     ASSERT_TRUE(serverRes != NULL);
204 
205     // Insert abnormal message callback.
206     handle->ctx = serverRes->ssl;
207     ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
208 
209     // Client listening connection.
210     clientRes = HLT_ProcessTlsConnect(remoteProcess, TLS1_3, clientConfig, NULL);
211 
212     if ( setInfo.SuccessOrFail ) {
213        ASSERT_TRUE(clientRes != NULL);
214     }else {
215         ASSERT_TRUE(clientRes == NULL);
216     }
217 
218 EXIT:
219     HLT_CleanFrameHandle();
220     HLT_FreeAllProcess();
221     return;
222 }
223 
ResumeConnectWithPara(HLT_FrameHandle * handle,SetInfo setInfo)224 void ResumeConnectWithPara(HLT_FrameHandle *handle, SetInfo setInfo)
225 {
226     Process *localProcess = NULL;
227     Process *remoteProcess = NULL;
228     HLT_FD sockFd = {0};
229     int32_t serverConfigId = 0;
230 
231     HITLS_Session *session = NULL;
232     const char *writeBuf = "Hello world";
233     uint8_t readBuf[READ_BUF_SIZE] = {0};
234     uint32_t readLen;
235     int32_t cnt = 1;
236 
237     localProcess = HLT_InitLocalProcess(HITLS);
238     ASSERT_TRUE(localProcess != NULL);
239     remoteProcess = HLT_CreateRemoteProcess(HITLS);
240     ASSERT_TRUE(remoteProcess != NULL);
241 
242     // Apply for the config context.
243     void *clientConfig = HLT_TlsNewCtx(TLS1_3);
244     ASSERT_TRUE(clientConfig != NULL);
245 
246     // Configure the session restoration function.
247     HLT_Ctx_Config *clientCtxConfig = HLT_NewCtxConfig(NULL, "CLIENT");
248 
249     HLT_Ctx_Config *serverCtxConfig = HLT_NewCtxConfig(NULL, "SERVER");
250 #ifdef HITLS_TLS_FEATURE_PROVIDER
251     serverConfigId = HLT_RpcProviderTlsNewCtx(remoteProcess, TLS1_3, false, NULL, NULL, NULL, 0, NULL);
252 #else
253     serverConfigId = HLT_RpcTlsNewCtx(remoteProcess, TLS1_3, false);
254 #endif
255     ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
256     ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
257 
258     do {
259         if (cnt == 2) {
260             SetConfig(clientCtxConfig, serverCtxConfig, setInfo);
261             ASSERT_TRUE(HLT_TlsSetCtx(clientConfig, clientCtxConfig) == 0);
262             ASSERT_TRUE(HLT_RpcTlsSetCtx(remoteProcess, serverConfigId, serverCtxConfig) == 0);
263         }
264         DataChannelParam channelParam;
265         channelParam.port = PORT;
266         channelParam.type = TCP;
267         channelParam.isBlock = true;
268         sockFd = HLT_CreateDataChannel(localProcess, remoteProcess, channelParam);
269         ASSERT_TRUE((sockFd.srcFd > 0) && (sockFd.peerFd > 0));
270         remoteProcess->connFd = sockFd.peerFd;
271         localProcess->connFd = sockFd.srcFd;
272         remoteProcess->connType = TCP;
273         localProcess->connType = TCP;
274 
275         // The server applies for the context.
276         int32_t serverSslId = HLT_RpcTlsNewSsl(remoteProcess, serverConfigId);
277 
278         HLT_Ssl_Config *serverSslConfig;
279         serverSslConfig = HLT_NewSslConfig(NULL);
280         ASSERT_TRUE(serverSslConfig != NULL);
281         serverSslConfig->sockFd = remoteProcess->connFd;
282         serverSslConfig->connType = TCP;
283         // Set the FD.
284         ASSERT_TRUE(HLT_RpcTlsSetSsl(remoteProcess, serverSslId, serverSslConfig) == 0);
285         HLT_RpcTlsAccept(remoteProcess, serverSslId);
286 
287         // Client, applying for context
288         void *clientSsl = HLT_TlsNewSsl(clientConfig);
289         ASSERT_TRUE(clientSsl != NULL);
290 
291         HLT_Ssl_Config *clientSslConfig;
292         clientSslConfig = HLT_NewSslConfig(NULL);
293         ASSERT_TRUE(clientSslConfig != NULL);
294         clientSslConfig->sockFd = localProcess->connFd;
295         clientSslConfig->connType = TCP;
296 
297         // Set the FD.
298         HLT_TlsSetSsl(clientSsl, clientSslConfig);
299         if (session != NULL) {
300             handle->ctx = clientSsl;
301             ASSERT_TRUE(HLT_SetFrameHandle(handle) == 0);
302             ASSERT_TRUE(HITLS_SetSession(clientSsl, session) == HITLS_SUCCESS);
303 
304             if(!setInfo.SuccessOrFail){
305                 ASSERT_TRUE(HLT_TlsConnect(clientSsl) != 0);
306             }else {
307                 ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
308             }
309         }
310         else {
311             // Negotiation
312             ASSERT_TRUE(HLT_TlsConnect(clientSsl) == 0);
313             // Data read/write
314             ASSERT_TRUE(HLT_RpcTlsWrite(remoteProcess, serverSslId, (uint8_t *)writeBuf, strlen(writeBuf)) == 0);
315             ASSERT_TRUE(memset_s(readBuf, READ_BUF_SIZE, 0, READ_BUF_SIZE) == EOK);
316             ASSERT_TRUE(HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen) == 0);
317             ASSERT_TRUE(readLen == strlen(writeBuf));
318             ASSERT_TRUE(memcmp(writeBuf, readBuf, readLen) == 0);
319             // Disable the connection.
320             ASSERT_TRUE(HLT_RpcTlsClose(remoteProcess, serverSslId) == 0);
321             ASSERT_TRUE(HLT_TlsClose(clientSsl) == 0);
322             HLT_TlsRead(clientSsl, readBuf, READ_BUF_SIZE, &readLen);
323             HLT_RpcTlsRead(remoteProcess, serverSslId, readBuf, READ_BUF_SIZE, &readLen);
324             HLT_RpcCloseFd(remoteProcess, sockFd.peerFd, remoteProcess->connType);
325             HLT_CloseFd(sockFd.srcFd, localProcess->connType);
326 
327             session = HITLS_GetDupSession(clientSsl);
328             ASSERT_TRUE(session != NULL);
329             ASSERT_TRUE(HITLS_SESS_HasTicket(session) == true);
330             ASSERT_TRUE(HITLS_SESS_IsResumable(session) == true);
331         }cnt++;
332     } while (cnt < 3); // Perform the connection twice.
333 
334 EXIT:
335     HITLS_SESS_Free(session);
336     HLT_CleanFrameHandle();
337     HLT_FreeAllProcess();
338     return;
339 }
340 
Test_ServerAddKeyExchangeMode(HITLS_Ctx * ctx,uint8_t * data,uint32_t * len,uint32_t bufSize,void * user)341 static void Test_ServerAddKeyExchangeMode(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
342     uint32_t bufSize, void *user)
343 {
344     // Add the KeyExchangeMode extension to server hello.
345     (void)ctx;
346     (void)bufSize;
347     (void)user;
348     FRAME_Type frameType = { 0 };
349     frameType.versionType = HITLS_VERSION_TLS13;
350     FRAME_Msg frameMsg = { 0 };
351     frameMsg.recType.data = REC_TYPE_HANDSHAKE;
352     frameMsg.length.data = *len;
353     frameMsg.recVersion.data = HITLS_VERSION_TLS13;
354     uint32_t parseLen = 0;
355     FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
356     ASSERT_EQ(parseLen, *len);
357     ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
358 
359     uint8_t pskMode[] = {0, 0x2d, 0, 2, 1, 1};
360     frameMsg.body.hsMsg.length.state = ASSIGNED_FIELD;
361     frameMsg.body.hsMsg.length.data += sizeof(pskMode);
362     frameMsg.body.hsMsg.body.serverHello.extensionLen.state = ASSIGNED_FIELD;
363     frameMsg.body.hsMsg.body.serverHello.extensionLen.data = frameMsg.body.hsMsg.body.serverHello.extensionLen.data + sizeof(pskMode);
364     FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
365 
366     ASSERT_EQ(memcpy_s(&data[*len], bufSize - *len, &pskMode, sizeof(pskMode)), EOK);
367     *len += sizeof(pskMode);
368 EXIT:
369     FRAME_CleanMsg(&frameType, &frameMsg);
370     return;
371 }
372 
Test_Server_KeyShare_Miss(HITLS_Ctx * ctx,uint8_t * data,uint32_t * len,uint32_t bufSize,void * user)373 static void Test_Server_KeyShare_Miss(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
374     uint32_t bufSize, void *user)
375 {
376     // The keyshare extension of server hello is lost.
377     (void)ctx;
378     (void)bufSize;
379     (void)user;
380     FRAME_Type frameType = { 0 };
381     frameType.versionType = HITLS_VERSION_TLS13;
382     FRAME_Msg frameMsg = { 0 };
383     frameMsg.recType.data = REC_TYPE_HANDSHAKE;
384     frameMsg.length.data = *len;
385     frameMsg.recVersion.data = HITLS_VERSION_TLS13;
386     uint32_t parseLen = 0;
387     FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
388     ASSERT_EQ(parseLen, *len);
389     ASSERT_EQ(frameMsg.body.hsMsg.type.data, SERVER_HELLO);
390 
391     frameMsg.body.hsMsg.body.serverHello.keyShare.exState = MISSING_FIELD;
392 
393     FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
394 EXIT:
395     FRAME_CleanMsg(&frameType, &frameMsg);
396     return;
397 }
398 
Test_Client_MasterSecret_Miss(HITLS_Ctx * ctx,uint8_t * data,uint32_t * len,uint32_t bufSize,void * user)399 static void Test_Client_MasterSecret_Miss(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
400     uint32_t bufSize, void *user)
401 {
402     // The MasterSecret extension of client hello is lost.
403     (void)ctx;
404     (void)bufSize;
405     (void)user;
406     FRAME_Type frameType = { 0 };
407     frameType.versionType = HITLS_VERSION_TLS13;
408     FRAME_Msg frameMsg = { 0 };
409     frameMsg.recType.data = REC_TYPE_HANDSHAKE;
410     frameMsg.length.data = *len;
411     frameMsg.recVersion.data = HITLS_VERSION_TLS13;
412     uint32_t parseLen = 0;
413     FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
414     ASSERT_EQ(parseLen, *len);
415     ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
416 
417     frameMsg.body.hsMsg.body.clientHello.extendedMasterSecret.exState = MISSING_FIELD;
418 
419     FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
420 EXIT:
421     FRAME_CleanMsg(&frameType, &frameMsg);
422     return;
423 }
424 
Test_Client_ObfuscatedTicketAge_NotZero(HITLS_Ctx * ctx,uint8_t * data,uint32_t * len,uint32_t bufSize,void * user)425 static void Test_Client_ObfuscatedTicketAge_NotZero(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
426     uint32_t bufSize, void *user)
427 {
428     // The value of ObfuscatedTicketAge is not 0 for the external preset PSK.
429     (void)ctx;
430     (void)bufSize;
431     (void)user;
432     FRAME_Type frameType = { 0 };
433     frameType.versionType = HITLS_VERSION_TLS13;
434     FRAME_Msg frameMsg = { 0 };
435     frameMsg.recType.data = REC_TYPE_HANDSHAKE;
436     frameMsg.length.data = *len;
437     frameMsg.recVersion.data = HITLS_VERSION_TLS13;
438     uint32_t parseLen = 0;
439     FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
440     ASSERT_EQ(parseLen, *len);
441     ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
442 
443     frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.state = ASSIGNED_FIELD;
444     frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.data = 2;
445 
446     FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
447 EXIT:
448     FRAME_CleanMsg(&frameType, &frameMsg);
449     return;
450 }
451 
Test_Client_ObfuscatedTicketAge_Zero(HITLS_Ctx * ctx,uint8_t * data,uint32_t * len,uint32_t bufSize,void * user)452 static void Test_Client_ObfuscatedTicketAge_Zero(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
453     uint32_t bufSize, void *user)
454 {
455     // The value of ObfuscatedTicketAge is 0 for the PSK generated by the session.
456     (void)ctx;
457     (void)bufSize;
458     (void)user;
459     FRAME_Type frameType = { 0 };
460     frameType.versionType = HITLS_VERSION_TLS13;
461     FRAME_Msg frameMsg = { 0 };
462     frameMsg.recType.data = REC_TYPE_HANDSHAKE;
463     frameMsg.length.data = *len;
464     frameMsg.recVersion.data = HITLS_VERSION_TLS13;
465     uint32_t parseLen = 0;
466     FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
467     ASSERT_EQ(parseLen, *len);
468     ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
469 
470     frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.state = ASSIGNED_FIELD;
471     frameMsg.body.hsMsg.body.clientHello.psks.identities.data->obfuscatedTicketAge.data = 0;
472 
473     FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
474 EXIT:
475     FRAME_CleanMsg(&frameType, &frameMsg);
476     return;
477 }
478 
Test_Client_Binder_Unnormal(HITLS_Ctx * ctx,uint8_t * data,uint32_t * len,uint32_t bufSize,void * user)479 static void Test_Client_Binder_Unnormal(HITLS_Ctx *ctx, uint8_t *data, uint32_t *len,
480     uint32_t bufSize, void *user)
481 {
482     // Change the binder value of the psk extension of the client hello message.
483     (void)ctx;
484     (void)bufSize;
485     (void)user;
486     FRAME_Type frameType = { 0 };
487     frameType.versionType = HITLS_VERSION_TLS13;
488     FRAME_Msg frameMsg = { 0 };
489     frameMsg.recType.data = REC_TYPE_HANDSHAKE;
490     frameMsg.length.data = *len;
491     frameMsg.recVersion.data = HITLS_VERSION_TLS13;
492     uint32_t parseLen = 0;
493     FRAME_ParseMsgBody(&frameType, data, *len, &frameMsg, &parseLen);
494     ASSERT_EQ(parseLen, *len);
495     ASSERT_EQ(frameMsg.body.hsMsg.type.data, CLIENT_HELLO);
496 
497     uint8_t binder[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,};
498     frameMsg.body.hsMsg.body.clientHello.psks.binders.data->binder.state = ASSIGNED_FIELD;
499     memcpy_s(frameMsg.body.hsMsg.body.clientHello.psks.binders.data->binder.data, PSK_MAX_LEN, binder, sizeof(binder));
500 
501     FRAME_PackRecordBody(&frameType, &frameMsg, data, bufSize, len);
502 EXIT:
503     FRAME_CleanMsg(&frameType, &frameMsg);
504     return;
505 }
506 
FrameCallBack_ServerHello_KeyShare_Add(void * msg,void * userData)507 static void FrameCallBack_ServerHello_KeyShare_Add(void *msg, void *userData)
508 {
509     // ServerHello exception: The sent ServerHello message carries the keyshare extension.
510     HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
511     FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
512     ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
513     FRAME_ServerHelloMsg *serverhello = &frameMsg->body.hsMsg.body.serverHello;
514 
515     serverhello->keyShare.exState = INITIAL_FIELD;
516     serverhello->keyShare.exLen.state = INITIAL_FIELD;
517     serverhello->keyShare.data.state = INITIAL_FIELD;
518     serverhello->keyShare.data.group.state = ASSIGNED_FIELD;
519     serverhello->keyShare.data.group.data = HITLS_EC_GROUP_SECP256R1;
520 
521     FRAME_ModifyMsgInteger(HS_EX_TYPE_KEY_SHARE, &serverhello->keyShare.exType);
522     uint8_t uu[] = {0x00, 0x15, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56, 0x56};
523     FRAME_ModifyMsgArray8(uu, sizeof(uu), &serverhello->keyShare.data.keyExchange, &serverhello->keyShare.data.keyExchangeLen);
524 
525 EXIT:
526     return;
527 }
528 
FrameCallBack_ClientHello_PskExchangeMode_Miss(void * msg,void * userData)529 static void FrameCallBack_ClientHello_PskExchangeMode_Miss(void *msg, void *userData)
530 {
531     // ClientHello exception: The sent ClientHello message causes the Psk_Exchange_Mode extension to be lost.
532     HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
533     FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
534     ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
535     FRAME_ClientHelloMsg *clienthello = &frameMsg->body.hsMsg.body.clientHello;
536 
537     clienthello->pskModes.exState = MISSING_FIELD;
538 EXIT:
539     return;
540 }
541 
FrameCallBack_ClientHello_KeyShare_Miss(void * msg,void * userData)542 static void FrameCallBack_ClientHello_KeyShare_Miss(void *msg, void *userData)
543 {
544     // ClientHello exception: The KeyShare extension of the ClientHello message is lost.
545     HLT_FrameHandle *handle = (HLT_FrameHandle *)userData;
546     FRAME_Msg *frameMsg = (FRAME_Msg *)msg;
547     ASSERT_EQ(frameMsg->body.hsMsg.type.data, handle->expectHsType);
548     FRAME_ClientHelloMsg *clienthello = &frameMsg->body.hsMsg.body.clientHello;
549 
550     clienthello->keyshares.exState = MISSING_FIELD;
551 EXIT:
552     return;
553 }
554 
555 /** @
556 * @test  UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC001
557 * @brief 2.1-Incorrect DHE Share-6
558 * @spec  If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
559 *        appropriate alert.
560 * @title  The server sends an hrr message to request the key_share. The negotiation succeeds.
561 * @precon  nan
562 * @brief
563 * 1. Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
564 *    (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
565 * 2. Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and check whether the value of
566 *    the group field is HITLS_EC_GROUP_SECP384R1. (Expected result 2)
567 * 3. Continue to establish the connection. Expected result 3 is obtained.
568 * @expect
569 * 1. The setting is successful.
570 * 2. The value of the group field is HITLS_EC_GROUP_SECP384R1.
571 * 3. The connection is set up successfully.
572 @ */
573 /* BEGIN_CASE */
574 
UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC001()575 void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC001()
576 {
577     FRAME_Init();
578     FRAME_LinkObj *client = NULL;
579     FRAME_LinkObj *server = NULL;
580     HITLS_Config *clientconfig = NULL;
581     HITLS_Config *serverconfig = NULL;
582     clientconfig = HITLS_CFG_NewTLS13Config();
583     ASSERT_TRUE(clientconfig != NULL);
584     serverconfig = HITLS_CFG_NewTLS13Config();
585     ASSERT_TRUE(serverconfig != NULL);
586 
587     // Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
588     // (HITLS_EC_GROUP_SECP384R1) on the server.
589     uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
590     uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
591     ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
592     ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
593 
594     client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
595     ASSERT_TRUE(client != NULL);
596     server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
597     ASSERT_TRUE(server != NULL);
598 
599     ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
600     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
601     ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
602 
603     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(server->io);
604     uint8_t *sndBuf = ioUserData->sndMsg.msg;
605     uint32_t sndLen = ioUserData->sndMsg.len;
606     ASSERT_TRUE(sndLen != 0);
607 
608     uint32_t parseLen = 0;
609     FRAME_Msg frameMsg = {0};
610     FRAME_Type frameType = {0};
611     frameType.versionType = HITLS_VERSION_TLS13;
612     frameType.recordType = REC_TYPE_HANDSHAKE;
613     frameType.handshakeType = SERVER_HELLO;
614     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
615     ASSERT_TRUE(FRAME_ParseMsg(&frameType, sndBuf, sndLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
616 
617     // Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and check whether the value of
618     // the group field is HITLS_EC_GROUP_SECP384R1.
619     FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
620     ASSERT_TRUE(Hello_Retry_RequestMsg->keyShare.data.group.data == HITLS_EC_GROUP_SECP384R1);
621 
622     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
623 
624     ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_SEND_CLIENT_HELLO), HITLS_SUCCESS);
625 
626     // Continue to establish the connection.
627     ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
628 EXIT:
629     FRAME_CleanMsg(&frameType, &frameMsg);
630     HITLS_CFG_FreeConfig(clientconfig);
631     HITLS_CFG_FreeConfig(serverconfig);
632     FRAME_FreeLink(client);
633     FRAME_FreeLink(server);
634 }
635 /* END_CASE */
636 
637 /** @
638 * @test  UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC002
639 * @brief 2.1-Incorrect DHE Share-6
640 * @spec  If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
641 *        appropriate alert.
642 * @title  The server receives two unsupported key_share messages.
643 * @precon  nan
644 * @brief
645 * 1. Set the group (HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, and HITLS_EC_GROUP_SECP521R1) on the client and
646 *    set the group (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
647 * 2. Establish a connection, stop the server in TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the group
648 *    field in the connection to HITLS_EC_GROUP_SECP521R1. Expected result 2 is obtained.
649 * 3. Continue to establish a connection and observe the connection establishment result. Expected result 3 is obtained.
650 * @expect
651 * 1. The setting is successful.
652 * 2. The modification is successful.
653 * 3. The server sends a request again, and the client returns HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP.
654 @ */
655 /* BEGIN_CASE */
UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC002()656 void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC002()
657 {
658     FRAME_Init();
659     FRAME_LinkObj *client = NULL;
660     FRAME_LinkObj *server = NULL;
661     HITLS_Config *clientconfig = NULL;
662     HITLS_Config *serverconfig = NULL;
663     clientconfig = HITLS_CFG_NewTLS13Config();
664     ASSERT_TRUE(clientconfig != NULL);
665     serverconfig = HITLS_CFG_NewTLS13Config();
666     ASSERT_TRUE(serverconfig != NULL);
667 
668     // Set the group (HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, and HITLS_EC_GROUP_SECP521R1) on the client
669     // and set the group (HITLS_EC_GROUP_SECP384R1) on the server.
670     uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1, HITLS_EC_GROUP_SECP521R1};
671     uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
672     ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
673     ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
674 
675     client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
676     ASSERT_TRUE(client != NULL);
677     server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
678     ASSERT_TRUE(server != NULL);
679 
680     ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
681     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
682     ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
683     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
684 
685     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
686     uint8_t *recBuf = ioUserData->recMsg.msg;
687     uint32_t recLen = ioUserData->recMsg.len;
688     ASSERT_TRUE(recLen != 0);
689 
690     uint32_t parseLen = 0;
691     FRAME_Msg frameMsg = {0};
692     FRAME_Type frameType = {0};
693     frameType.versionType = HITLS_VERSION_TLS13;
694     frameType.recordType = REC_TYPE_HANDSHAKE;
695     frameType.handshakeType = SERVER_HELLO;
696     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
697     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
698 
699     // Establish a connection, stop the server in TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the group
700     // field in the connection to HITLS_EC_GROUP_SECP521R1.
701     FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
702     Hello_Retry_RequestMsg->keyShare.data.group.state = ASSIGNED_FIELD;
703     Hello_Retry_RequestMsg->keyShare.data.group.data = HITLS_EC_GROUP_SECP521R1;
704 
705     uint32_t sendLen = MAX_RECORD_LENTH;
706     uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
707     ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
708 
709     ioUserData->recMsg.len = 0;
710     ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
711     FRAME_CleanMsg(&frameType, &frameMsg);
712     memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
713 
714     ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_REC_NORMAL_IO_BUSY);
715     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
716     ASSERT_EQ(HITLS_Accept(server->ssl) , HITLS_REC_NORMAL_RECV_BUF_EMPTY);
717 
718     ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_REC_NORMAL_RECV_BUF_EMPTY);
719     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
720 
721     // Continue to establish a connection and observe the connection establishment result.
722     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
723     ASSERT_EQ(HITLS_Accept(server->ssl) , HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
724 EXIT:
725     FRAME_CleanMsg(&frameType, &frameMsg);
726     HITLS_CFG_FreeConfig(clientconfig);
727     HITLS_CFG_FreeConfig(serverconfig);
728     FRAME_FreeLink(client);
729     FRAME_FreeLink(server);
730 }
731 /* END_CASE */
732 
733 /** @
734 * @test  UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC003
735 * @brief 2.1-Incorrect DHE Share-6
736 * @spec  If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
737 *        appropriate alert.
738 * @title  The client receives the key_share with the same elliptic curve for the second time.
739 * @precon  nan
740 * @brief
741 * 1. Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
742 *     (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
743 * 2. Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
744 *    group field to HITLS_EC_GROUP_SECP256R1. Expected result 2 is obtained.
745 * 3. Continue connection establishment and observe the connection establishment result. Expected result 3 is obtained.
746 * @expect
747 * 1. The setting is successful.
748 * 2. The modification is successful.
749 * 3. The connection fails to be established. After receiving the request message, the client returns
750 *     HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP.
751 @ */
752 /* BEGIN_CASE */
UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC003()753 void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC003()
754 {
755     FRAME_Init();
756     FRAME_LinkObj *client = NULL;
757     FRAME_LinkObj *server = NULL;
758     HITLS_Config *clientconfig = NULL;
759     HITLS_Config *serverconfig = NULL;
760     clientconfig = HITLS_CFG_NewTLS13Config();
761     ASSERT_TRUE(clientconfig != NULL);
762     serverconfig = HITLS_CFG_NewTLS13Config();
763     ASSERT_TRUE(serverconfig != NULL);
764 
765     // Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
766     // (HITLS_EC_GROUP_SECP384R1) on the server.
767     uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
768     uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
769     ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
770     ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
771 
772     client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
773     ASSERT_TRUE(client != NULL);
774     server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
775     ASSERT_TRUE(server != NULL);
776 
777     ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
778     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
779     ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
780     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
781 
782     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
783     uint8_t *recBuf = ioUserData->recMsg.msg;
784     uint32_t recLen = ioUserData->recMsg.len;
785     ASSERT_TRUE(recLen != 0);
786 
787     uint32_t parseLen = 0;
788     FRAME_Msg frameMsg = {0};
789     FRAME_Type frameType = {0};
790     frameType.versionType = HITLS_VERSION_TLS13;
791     frameType.recordType = REC_TYPE_HANDSHAKE;
792     frameType.handshakeType = SERVER_HELLO;
793     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
794     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
795 
796     // Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
797     // group field to HITLS_EC_GROUP_SECP256R1.
798     FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
799     Hello_Retry_RequestMsg->keyShare.data.group.state = ASSIGNED_FIELD;
800     Hello_Retry_RequestMsg->keyShare.data.group.data = HITLS_EC_GROUP_SECP256R1;
801 
802     uint32_t sendLen = MAX_RECORD_LENTH;
803     uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
804     ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
805 
806     ioUserData->recMsg.len = 0;
807     ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
808     FRAME_CleanMsg(&frameType, &frameMsg);
809     memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
810 
811     ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
812 EXIT:
813     FRAME_CleanMsg(&frameType, &frameMsg);
814     HITLS_CFG_FreeConfig(clientconfig);
815     HITLS_CFG_FreeConfig(serverconfig);
816     FRAME_FreeLink(client);
817     FRAME_FreeLink(server);
818 }
819 /* END_CASE */
820 
821 /** @
822 * @test  UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC004
823 * @brief 2.1-Incorrect DHE Share-6
824 * @spec  If no common cryptographic parameters can be negotiated, the server MUST abort the handshake with an
825 *        appropriate alert.
826 * @title  The client receives an unsupported elliptic curve key_share request.
827 * @precon  nan
828 * @brief
829 *   1. Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
830 *       (HITLS_EC_GROUP_SECP384R1) on the server. Expected result 1 is obtained.
831 *   2. Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
832 *       group field to HITLS_EC_GROUP_SECP521R1. Expected result 2 is obtained.
833 *   3. Continue connection establishment and observe the connection establishment result. Expected result 3 is obtained.
834 * @expect
835 *   1. The setting is successful.
836 *   2. The modification is successful.
837 *   3. The connection fails to be established. After receiving the request message, the client returns
838 *       HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP.
839 @ */
840 /* BEGIN_CASE */
UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC004()841 void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC004()
842 {
843     FRAME_Init();
844     FRAME_LinkObj *client = NULL;
845     FRAME_LinkObj *server = NULL;
846     HITLS_Config *clientconfig = NULL;
847     HITLS_Config *serverconfig = NULL;
848     clientconfig = HITLS_CFG_NewTLS13Config();
849     ASSERT_TRUE(clientconfig != NULL);
850     serverconfig = HITLS_CFG_NewTLS13Config();
851     ASSERT_TRUE(serverconfig != NULL);
852 
853     // Set the group (HITLS_EC_GROUP_SECP256R1 and HITLS_EC_GROUP_SECP384R1) on the client and set the group
854     // (HITLS_EC_GROUP_SECP384R1) on the server.
855     uint16_t clientgroups[] = {HITLS_EC_GROUP_SECP256R1, HITLS_EC_GROUP_SECP384R1};
856     uint16_t servergroups[] = {HITLS_EC_GROUP_SECP384R1};
857     ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups) / sizeof(uint16_t)), HITLS_SUCCESS);
858     ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups) / sizeof(uint16_t)), HITLS_SUCCESS);
859 
860     client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
861     ASSERT_TRUE(client != NULL);
862     server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
863     ASSERT_TRUE(server != NULL);
864 
865     ASSERT_TRUE(HITLS_Connect(client->ssl) == HITLS_REC_NORMAL_RECV_BUF_EMPTY);
866     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
867     ASSERT_TRUE(HITLS_Accept(server->ssl) == HITLS_REC_NORMAL_IO_BUSY);
868     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
869 
870     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
871     uint8_t *recBuf = ioUserData->recMsg.msg;
872     uint32_t recLen = ioUserData->recMsg.len;
873     ASSERT_TRUE(recLen != 0);
874 
875     uint32_t parseLen = 0;
876     FRAME_Msg frameMsg = {0};
877     FRAME_Type frameType = {0};
878     frameType.versionType = HITLS_VERSION_TLS13;
879     frameType.recordType = REC_TYPE_HANDSHAKE;
880     frameType.handshakeType = SERVER_HELLO;
881     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
882     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
883 
884     // Establish a connection, stop the server in the TRY_SEND_HELLO_RETRY_REQUEST state, and change the value of the
885     // group field to HITLS_EC_GROUP_SECP521R1.
886     FRAME_ServerHelloMsg *Hello_Retry_RequestMsg = &frameMsg.body.hsMsg.body.serverHello;
887     Hello_Retry_RequestMsg->keyShare.data.group.state = ASSIGNED_FIELD;
888     Hello_Retry_RequestMsg->keyShare.data.group.data = HITLS_EC_GROUP_SECP521R1;
889 
890     uint32_t sendLen = MAX_RECORD_LENTH;
891     uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
892     ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
893 
894     ioUserData->recMsg.len = 0;
895     ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
896     FRAME_CleanMsg(&frameType, &frameMsg);
897     memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
898 
899     // Continue connection establishment and observe the connection establishment result.
900     ASSERT_EQ(HITLS_Connect(client->ssl) , HITLS_MSG_HANDLE_ILLEGAL_SELECTED_GROUP);
901 EXIT:
902     FRAME_CleanMsg(&frameType, &frameMsg);
903     HITLS_CFG_FreeConfig(clientconfig);
904     HITLS_CFG_FreeConfig(serverconfig);
905     FRAME_FreeLink(client);
906     FRAME_FreeLink(server);
907 }
908 /* END_CASE */
909 
910 /* @
911 * @test  UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC005
912 * @brief 2.1.  Incorrect DHE Share
913 * @spec  If the client has not provided a sufficient "key_share" extension (e.g., it includes only DHE or ECDHE groups
914     unacceptable to or unsupported by the server), the server corrects the mismatch with a HelloRetryRequest and the
915     client needs to restart the handshake with an appropriate "key_share" extension, as shown in Figure 2. If no common
916     cryptographic parameters can be negotiated, the server MUST abort the handshake with an appropriate alert.
917 * @title  Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
918     server. Observe the link setup result and check whether the server sends Hello_Retry_Requset.
919 * @precon  nan
920 * @brief
921 1. Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
922     server.
923 * @expect
924 1. Send clienthello with X25519 keyshare. The link is established successfully.
925 2. The server does not send Hello_Retry_Requset.
926 * @prior  Level 2
927 * @auto  TRUE
928 @ */
929 /* BEGIN_CASE */
UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC005()930 void UT_TLS_TLS13_CONSISTENCY_RFC8446_REQUEST_CLIENT_HELLO_FUNC_TC005()
931 {
932     FRAME_Init();
933     FRAME_LinkObj *client = NULL;
934     FRAME_LinkObj *server = NULL;
935     HITLS_Config *clientconfig = NULL;
936     HITLS_Config *serverconfig = NULL;
937     clientconfig = HITLS_CFG_NewTLS13Config();
938     ASSERT_TRUE(clientconfig != NULL);
939     serverconfig = HITLS_CFG_NewTLS13Config();
940     ASSERT_TRUE(serverconfig != NULL);
941 
942     uint16_t clientgroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
943     uint16_t servergroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
944     ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
945     ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
946 
947     client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
948     ASSERT_TRUE(client != NULL);
949     server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
950     ASSERT_TRUE(server != NULL);
951 
952     ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
953     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
954     uint8_t *recBuf = ioUserData->recMsg.msg;
955     uint32_t recLen = ioUserData->recMsg.len;
956     ASSERT_TRUE(recLen != 0);
957 
958     uint32_t parseLen = 0;
959     FRAME_Msg frameMsg = {0};
960     FRAME_Type frameType = {0};
961     frameType.versionType = HITLS_VERSION_TLS13;
962     frameType.recordType = REC_TYPE_HANDSHAKE;
963     frameType.handshakeType = SERVER_HELLO;
964     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
965     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
966 
967     FRAME_ServerHelloMsg *serverHello = &frameMsg.body.hsMsg.body.serverHello;
968     ASSERT_TRUE(serverHello->keyShare.data.group.data == HITLS_EC_GROUP_CURVE25519);
969     ASSERT_EQ(server->ssl->hsCtx->haveHrr, false);
970 
971     // Continue to establish the link.
972     ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
973 EXIT:
974     FRAME_CleanMsg(&frameType, &frameMsg);
975     HITLS_CFG_FreeConfig(clientconfig);
976     HITLS_CFG_FreeConfig(serverconfig);
977     FRAME_FreeLink(client);
978     FRAME_FreeLink(server);
979 }
980 /* END_CASE */
981 
982 /** @
983 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC001
984 * @brief 4.2.9-Pre-Shared Key Exchange Modes-77
985 * @spec  In order to use PSKs, clients MUST also send a "psk_key_exchange_modes" extension.
986 * @title  Preset psk Client Lost Pre-Shared Key Exchange Modes Extension
987 * @precon  nan
988 * @brief
989 * 1. Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
990 *    lost, and observe the server behavior.
991 * @expect
992 * 1. Connect establishment is interrupted.
993 @ */
994 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC001()995 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC001()
996 {
997     // Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
998     // lost, and observe the server behavior.
999     SetInfo setInfo = {0};
1000     memcpy_s(setInfo.psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
1001     setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
1002     setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
1003     setInfo.SuccessOrFail = 0;
1004     HLT_FrameHandle handle = {0};
1005     handle.pointType = POINT_SEND;
1006     handle.userData = (void *)&handle;
1007     handle.expectReType = REC_TYPE_HANDSHAKE;
1008     handle.expectHsType = CLIENT_HELLO;
1009     handle.frameCallBack = FrameCallBack_ClientHello_PskExchangeMode_Miss;
1010     ClientCreatConnectWithPara(&handle, setInfo);
1011 }
1012 /* END_CASE */
1013 
1014 /** @
1015 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC002
1016 * @brief 4.2.9-Pre-Shared Key Exchange Modes-77
1017 * @spec  In order to use PSKs, clients MUST also send a "psk_key_exchange_modes" extension.
1018 * @title  psk session recovery client lost Pre-Shared Key Exchange Modes extension
1019 * @precon  nan
1020 * @brief
1021 * 1. Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
1022 lost, and observe the server behavior.
1023 * @expect
1024 * 1. Connect establishment is interrupted.
1025 @ */
1026 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC002()1027 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_MISS_FUNC_TC002()
1028 {
1029     // Preset the psk, modify the client hello message sent by the client to make the psk_key_exchange_modes extension
1030     // lost, and observe the server behavior.
1031     SetInfo setInfo = {0};
1032     setInfo.SetNothing = 1;
1033     setInfo.SuccessOrFail = 0;
1034     HLT_FrameHandle handle = {0};
1035     handle.pointType = POINT_SEND;
1036     handle.userData = (void *)&handle;
1037     handle.expectReType = REC_TYPE_HANDSHAKE;
1038     handle.expectHsType = CLIENT_HELLO;
1039     handle.frameCallBack = FrameCallBack_ClientHello_PskExchangeMode_Miss;
1040     ResumeConnectWithPara(&handle, setInfo);
1041 }
1042 /* END_CASE */
1043 
1044 /** @
1045 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_ADD_FUNC_TC001
1046 * @brief 4.2.9-Pre-Shared Key Exchange Modes-80
1047 * @spec  The server MUST NOT send a "psk_key_exchange_modes" extension.
1048 * @title  The session restoration server carries psk_key_exchange_modes.
1049 * @precon  nan
1050 * @brief
1051 * 1. Establish a connection, save the session, and restore the session.
1052 * 2. Modify the server hello message sent by the server to carry the psk_key_exchange_mode extension and observe the
1053 *    client behavior.
1054 * @expect
1055 * 1. The connection is successfully established and the session is restored.
1056 * 2. The client sends an alert message and disconnects the connection.
1057 @ */
1058 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_ADD_FUNC_TC001()1059 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSK_EXCHANGE_MODES_ADD_FUNC_TC001()
1060 {
1061     FRAME_Init();
1062 
1063     ResumeTestInfo testInfo = {0};
1064     testInfo.version = HITLS_VERSION_TLS13;
1065     testInfo.uioType = BSL_UIO_TCP;
1066 
1067     // Establish a connection, save the session, and restore the session.
1068     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1069     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1070     ASSERT_TRUE(testInfo.s_config != NULL);
1071     ASSERT_TRUE(testInfo.c_config != NULL);
1072 
1073     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1074 
1075     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
1076     ASSERT_TRUE(testInfo.clientSession != NULL);
1077 
1078     FRAME_FreeLink(testInfo.client);
1079     testInfo.client = NULL;
1080     FRAME_FreeLink(testInfo.server);
1081     testInfo.server = NULL;
1082 
1083     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1084     ASSERT_TRUE(testInfo.client != NULL);
1085     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1086     ASSERT_TRUE(testInfo.server != NULL);
1087     ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
1088 
1089     // Modify the server hello message sent by the server to carry the psk_key_exchange_mode extension and observe the
1090     // client behavior.
1091     RecWrapper wrapper = {TRY_SEND_SERVER_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_ServerAddKeyExchangeMode};
1092     RegisterWrapper(wrapper);
1093 
1094     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
1095         HITLS_PARSE_UNSUPPORTED_EXTENSION);
1096 
1097 EXIT:
1098     ClearWrapper();
1099     HITLS_CFG_FreeConfig(testInfo.c_config);
1100     HITLS_CFG_FreeConfig(testInfo.s_config);
1101     FRAME_FreeLink(testInfo.client);
1102     FRAME_FreeLink(testInfo.server);
1103     HITLS_SESS_Free(testInfo.clientSession);
1104 }
1105 /* END_CASE */
1106 
1107 /** @
1108 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_ADD_FUNC_TC001
1109 * @brief 4.2.9-Pre-Shared Key Exchange Modes-80
1110 * @spec  psk_ke:  PSK-only key establishment.  In this mode, the server MUST NOT supply a "key_share" value.
1111 * @title  Preset the PSK. In psk_ke mode, the server carries the key_share extension.
1112 * @precon  nan
1113 * @brief
1114 * 1. Preset PSK
1115 * 2. Set psk_key_exchange_mode to psk_ke on the client and server,
1116 * 3. Modify the server hello message sent by the server to carry the key_share extension and observe the client
1117 *    behavior.
1118 * @expect
1119 * 1. The setting is successful.
1120 * 2. The setting is successful.
1121 * 3. The client sends an alert message and disconnects the connection.
1122 @ */
1123 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_ADD_FUNC_TC001()1124 void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_ADD_FUNC_TC001()
1125 {
1126     // Preset PSK
1127     SetInfo setInfo = {0};
1128     memcpy_s(setInfo.psk, PSK_MAX_LEN, "12121212121212", sizeof("12121212121212"));
1129     setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
1130     setInfo.ClientCipherSuite = "HITLS_AES_128_GCM_SHA256";
1131     // Set psk_key_exchange_mode to psk_ke on the client and server
1132     setInfo.ClientKeyExchangeMode = TLS13_KE_MODE_PSK_ONLY;
1133     setInfo.ServerKeyExchangeMode = TLS13_KE_MODE_PSK_ONLY;
1134     setInfo.SuccessOrFail = 0;
1135     // Modify the server hello message sent by the server to carry the key_share extension and observe the client
1136     // behavior.
1137     HLT_FrameHandle handle = {0};
1138     handle.pointType = POINT_SEND;
1139     handle.userData = (void *)&handle;
1140     handle.expectReType = REC_TYPE_HANDSHAKE;
1141     handle.expectHsType = SERVER_HELLO;
1142     handle.frameCallBack = FrameCallBack_ServerHello_KeyShare_Add;
1143     ServerCreatConnectWithPara(&handle, setInfo);
1144 }
1145 /* END_CASE */
1146 
1147 /** @
1148 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC001
1149 * @brief 4.2.9-Pre-Shared Key Exchange Modes-77
1150 * @spec  psk_dhe_ke:  PSK with (EC)DHE key establishment.  In this mode, the
1151 *       client and server MUST supply "key_share" values as described in Section 4.2.8.
1152 * @title  Session Recovery Client Lost Key_share Extension
1153 * @precon  nan
1154 * @brief
1155 * 1. When the session is recovered, modify the client hello message sent by the client so that the Key_Share extension
1156 *   is lost. Observe the server behavior.
1157 * @expect
1158 * 1. Connect establishment is interrupted.
1159 @ */
1160 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC001()1161 void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC001()
1162 {
1163     // When the session is recovered, modify the client hello message sent by the client so that the Key_Share extension
1164     // is lost. Observe the server behavior.
1165     SetInfo setInfo = {0};
1166     setInfo.SetNothing = 1;
1167     setInfo.SuccessOrFail = 0;
1168     HLT_FrameHandle handle = {0};
1169     handle.pointType = POINT_SEND;
1170     handle.userData = (void *)&handle;
1171     handle.expectReType = REC_TYPE_HANDSHAKE;
1172     handle.expectHsType = CLIENT_HELLO;
1173     handle.frameCallBack = FrameCallBack_ClientHello_KeyShare_Miss;
1174     ResumeConnectWithPara(&handle, setInfo);
1175 }
1176 /* END_CASE */
1177 
1178 /** @
1179 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC002
1180 * @brief 4.2.9-Pre-Shared Key Exchange Modes-80
1181 * @spec  psk_dhe_ke:  PSK with (EC)DHE key establishment.  In this mode, the
1182 *       client and server MUST supply "key_share" values as described in Section 4.2.8.
1183 * @title  Server: psk_key_exchange_mode: The key_share extension is lost under psk_dhe_ke.
1184 * @precon  nan
1185 * @brief
1186 * 1. Preset PSK
1187 * 2. Set psk_key_exchange_mode to psk_dhe_ke on the client server, modify the server hello message sent by the server to
1188 *     lose the key_share extension, and observe the client behavior.
1189 * @expect
1190 * 1. The setting is successful.
1191 * 2. The client sends an alert message to disconnect the connection.
1192 @ */
1193 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC002()1194 void UT_TLS_TLS13_RFC8446_CONSISTENCY_KEY_SHARE_MISS_FUNC_TC002()
1195 {
1196     FRAME_Init();
1197 
1198     ResumeTestInfo testInfo = {0};
1199     testInfo.version = HITLS_VERSION_TLS13;
1200     testInfo.uioType = BSL_UIO_TCP;
1201     RecWrapper wrapper = {
1202         TRY_SEND_SERVER_HELLO,
1203         REC_TYPE_HANDSHAKE,
1204         false,
1205         NULL,
1206         Test_Server_KeyShare_Miss
1207     };
1208     RegisterWrapper(wrapper);
1209 
1210     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1211     ASSERT_TRUE(testInfo.s_config != NULL);
1212     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1213     ASSERT_TRUE(testInfo.c_config != NULL);
1214 
1215     // Preset PSK
1216     char psk[] = "aaaaaaaaaaaaaaaa";
1217     ASSERT_TRUE(ExampleSetPsk(psk) == HITLS_SUCCESS);
1218     ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(testInfo.c_config, ExampleClientCb) == HITLS_SUCCESS);
1219     ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(testInfo.s_config, ExampleServerCb) == HITLS_SUCCESS);
1220 
1221     // Set psk_key_exchange_mode to psk_dhe_ke on the client server, modify the server hello message sent by the server
1222     // to lose the key_share extension, and observe the client behavior.
1223     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1224     ASSERT_TRUE(testInfo.client != NULL);
1225     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1226     ASSERT_TRUE(testInfo.server != NULL);
1227 
1228     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT),
1229         HITLS_MSG_HANDLE_HANDSHAKE_FAILURE);
1230 EXIT:
1231     ClearWrapper();
1232     HITLS_CFG_FreeConfig(testInfo.c_config);
1233     HITLS_CFG_FreeConfig(testInfo.s_config);
1234     FRAME_FreeLink(testInfo.client);
1235     FRAME_FreeLink(testInfo.server);
1236 }
1237 /* END_CASE */
1238 
1239 /** @
1240 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC001
1241 * @brief 4.2.11-Pre-Shared Key Extension-93
1242 * @spec  For identities established externally, an obfuscated_ticket_age of 0 SHOULD be used, and servers MUST ignore
1243 *        the value
1244 * @title  The obfuscated_ticket_age of the preset PSK is not 0.
1245 * @precon  nan
1246 * @brief
1247 * 1. Preset PSK
1248 * 2. Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
1249 *    ensure that the field is not 0.
1250 * 3. Establish a connection.
1251 * @expect
1252 * 1. The setting is successful.
1253 * 2. The modification is successful.
1254 * 3. The connection is successfully established and certificate authentication is performed.
1255 @ */
1256 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC001()1257 void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC001()
1258 {
1259     FRAME_Init();
1260 
1261     ResumeTestInfo testInfo = {0};
1262     testInfo.version = HITLS_VERSION_TLS13;
1263     testInfo.uioType = BSL_UIO_TCP;
1264     RecWrapper wrapper = {
1265         TRY_SEND_CLIENT_HELLO,
1266         REC_TYPE_HANDSHAKE,
1267         false,
1268         NULL,
1269         Test_Client_ObfuscatedTicketAge_NotZero
1270     };
1271     RegisterWrapper(wrapper);
1272 
1273     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1274     ASSERT_TRUE(testInfo.s_config != NULL);
1275     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1276     ASSERT_TRUE(testInfo.c_config != NULL);
1277 
1278     // Preset PSK
1279     char psk[] = "aaaaaaaaaaaaaaaa";
1280     ASSERT_TRUE(ExampleSetPsk(psk) == HITLS_SUCCESS);
1281     ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(testInfo.c_config, ExampleClientCb) == HITLS_SUCCESS);
1282     ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(testInfo.s_config, ExampleServerCb) == HITLS_SUCCESS);
1283 
1284     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1285     ASSERT_TRUE(testInfo.client != NULL);
1286     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1287     ASSERT_TRUE(testInfo.server != NULL);
1288 
1289     // Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
1290     // ensure that the field is not 0.
1291     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
1292 
1293     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
1294     uint8_t *recvBuf = ioUserData->recMsg.msg;
1295     uint32_t recvLen = ioUserData->recMsg.len;
1296     ASSERT_TRUE(recvLen != 0);
1297 
1298     uint32_t parseLen = 0;
1299     FRAME_Msg frameMsg = {0};
1300     FRAME_Type frameType = {0};
1301     frameType.versionType = HITLS_VERSION_TLS13;
1302     frameType.recordType = REC_TYPE_HANDSHAKE;
1303     frameType.handshakeType = SERVER_HELLO;
1304     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
1305     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
1306 
1307     FRAME_ServerHelloMsg *serverhelloMsg = &frameMsg.body.hsMsg.body.serverHello;
1308     ASSERT_TRUE(serverhelloMsg->pskSelectedIdentity.exLen.data == 0);
1309 
1310     // Establish a connection.
1311     ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) == HITLS_SUCCESS);
1312     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
1313 
1314 EXIT:
1315     ClearWrapper();
1316     FRAME_CleanMsg(&frameType, &frameMsg);
1317     HITLS_CFG_FreeConfig(testInfo.c_config);
1318     HITLS_CFG_FreeConfig(testInfo.s_config);
1319     FRAME_FreeLink(testInfo.client);
1320     FRAME_FreeLink(testInfo.server);
1321 }
1322 /* END_CASE */
1323 
1324 extern int32_t CompareBinder(TLS_Ctx *ctx, const PreSharedKey *pskNode, uint8_t *psk, uint32_t pskLen,
1325     uint32_t truncateHelloLen);
1326 
CompareBinder_Success(TLS_Ctx * ctx,const PreSharedKey * pskNode,uint8_t * psk,uint32_t pskLen,uint32_t truncateHelloLen)1327 static int32_t CompareBinder_Success(TLS_Ctx *ctx, const PreSharedKey *pskNode, uint8_t *psk, uint32_t pskLen,
1328     uint32_t truncateHelloLen)
1329 {
1330     (void)ctx;
1331     (void)pskNode;
1332     (void)psk;
1333     (void)pskLen;
1334     (void)truncateHelloLen;
1335     return 0;
1336 }
1337 
1338 /** @
1339 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC002
1340 * @brief 4.2.11-Pre-Shared Key Extension-93
1341 * @spec  For identities established externally, an obfuscated_ticket_age of 0 SHOULD be used, and servers MUST ignore
1342 *        the value
1343 * @title  The obfuscated_ticket_age of the PSK generated by the session is 0.
1344 * @precon  nan
1345 * @brief
1346 * 1. Preset PSK
1347 * 2. Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
1348 *    ensure that the value is not 0.
1349 * 3. Establish a connection.
1350 * @expect
1351 * 1. The setting is successful.
1352 * 2. The modification is successful.
1353 * 3. Certificate authentication
1354 @ */
1355 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC002()1356 void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC002()
1357 {
1358     FRAME_Init();
1359 
1360     ResumeTestInfo testInfo = {0};
1361     testInfo.version = HITLS_VERSION_TLS13;
1362     testInfo.uioType = BSL_UIO_TCP;
1363 
1364     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1365     ASSERT_TRUE(testInfo.s_config != NULL);
1366     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1367     ASSERT_TRUE(testInfo.c_config != NULL);
1368 
1369     // Preset PSK
1370     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1371 
1372     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
1373     ASSERT_TRUE(testInfo.clientSession != NULL);
1374 
1375     FRAME_FreeLink(testInfo.client);
1376     testInfo.client = NULL;
1377     FRAME_FreeLink(testInfo.server);
1378     testInfo.server = NULL;
1379 
1380     // Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
1381     // ensure that the value is not 0.
1382     RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_ObfuscatedTicketAge_Zero};
1383     RegisterWrapper(wrapper);
1384 
1385     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1386     ASSERT_TRUE(testInfo.client != NULL);
1387     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1388     ASSERT_TRUE(testInfo.server != NULL);
1389     ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
1390 
1391     STUB_Init();
1392     FuncStubInfo stubInfo = {0};
1393     STUB_Replace(&stubInfo, CompareBinder, CompareBinder_Success);
1394 
1395     // Establish a connection.
1396     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT), HITLS_SUCCESS);
1397     uint8_t isReused = 0;
1398     ASSERT_EQ(HITLS_IsSessionReused(testInfo.client->ssl, &isReused), HITLS_SUCCESS);
1399     ASSERT_EQ(isReused, 1);
1400 EXIT:
1401     ClearWrapper();
1402     STUB_Reset(&stubInfo);
1403     HITLS_CFG_FreeConfig(testInfo.c_config);
1404     HITLS_CFG_FreeConfig(testInfo.s_config);
1405     FRAME_FreeLink(testInfo.client);
1406     FRAME_FreeLink(testInfo.server);
1407     HITLS_SESS_Free(testInfo.clientSession);
1408 }
1409 /* END_CASE */
1410 
1411 /** @
1412 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC003
1413 * @brief 4.2.11-Pre-Shared Key Extension-93
1414 * @spec  For identities established externally, an obfuscated_ticket_age of 0 SHOULD be used, and servers MUST ignore
1415 *        the value
1416 * @title  The obfuscated_ticket_age of the PSK generated by the session is different from the original value.
1417 * @precon  nan
1418 * @brief
1419 * 1. Preset PSK
1420 * 2. Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
1421 *    ensure that the field is not 0.
1422 * 3. Establish a connection.
1423 * @expect
1424 * 1. The setting is successful.
1425 * 2. The modification is successful.
1426 * 3. Certificate authentication
1427 @ */
1428 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC003()1429 void UT_TLS_TLS13_RFC8446_CONSISTENCY_OBFUSCATED_TICKET_AGE_FUNC_TC003()
1430 {
1431     FRAME_Init();
1432 
1433     ResumeTestInfo testInfo = {0};
1434     testInfo.version = HITLS_VERSION_TLS13;
1435     testInfo.uioType = BSL_UIO_TCP;
1436 
1437     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1438     ASSERT_TRUE(testInfo.s_config != NULL);
1439     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1440     ASSERT_TRUE(testInfo.c_config != NULL);
1441 
1442     // Preset PSK
1443     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1444 
1445     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
1446     ASSERT_TRUE(testInfo.clientSession != NULL);
1447 
1448     FRAME_FreeLink(testInfo.client);
1449     testInfo.client = NULL;
1450     FRAME_FreeLink(testInfo.server);
1451     testInfo.server = NULL;
1452 
1453     // Modify the obfuscated_ticket_age field in the psk extension in the client hello message sent by the client to
1454     // ensure that the field is not 0.
1455     RecWrapper wrapper = {
1456         TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_ObfuscatedTicketAge_NotZero};
1457     RegisterWrapper(wrapper);
1458 
1459     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1460     ASSERT_TRUE(testInfo.client != NULL);
1461     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1462     ASSERT_TRUE(testInfo.server != NULL);
1463     ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
1464 
1465     STUB_Init();
1466     FuncStubInfo stubInfo = {0};
1467     STUB_Replace(&stubInfo, CompareBinder, CompareBinder_Success);
1468 
1469     // Establish a connection.
1470     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
1471 EXIT:
1472     ClearWrapper();
1473     STUB_Reset(&stubInfo);
1474     HITLS_CFG_FreeConfig(testInfo.c_config);
1475     HITLS_CFG_FreeConfig(testInfo.s_config);
1476     FRAME_FreeLink(testInfo.client);
1477     FRAME_FreeLink(testInfo.server);
1478     HITLS_SESS_Free(testInfo.clientSession);
1479 }
1480 /* END_CASE */
1481 
1482 #define SESSION_SZ 10
1483 static HITLS_Session *g_userSession[SESSION_SZ];
1484 static int g_sessionSz = 0;
1485 
ClearSessoins()1486 void ClearSessoins()
1487 {
1488     for (int i = 0; i < g_sessionSz; i++) {
1489         HITLS_SESS_Free(g_userSession[i]);
1490     }
1491     g_sessionSz = 0;
1492 }
1493 
GetSession(int index)1494 HITLS_Session *GetSession(int index)
1495 {
1496     if (index < g_sessionSz) {
1497         return HITLS_SESS_Dup(g_userSession[index]);
1498     }
1499     return NULL;
1500 }
1501 
AddSession(HITLS_Session * session)1502 void AddSession(HITLS_Session *session)
1503 {
1504     if (g_sessionSz < SESSION_SZ - 1) {
1505         g_userSession[g_sessionSz] = session;
1506         g_sessionSz++;
1507     }
1508 }
1509 
Test_NewSessionCb(HITLS_Ctx * ctx,HITLS_Session * session)1510 int32_t Test_NewSessionCb(HITLS_Ctx *ctx, HITLS_Session *session)
1511 {
1512     (void)ctx;
1513     if (ctx->isClient && HITLS_SESS_IsResumable(session)) {
1514         AddSession(session);
1515         return 1;
1516     }
1517     return 0;
1518 }
1519 
Test_PskUseSessionCb_WithSHA256(HITLS_Ctx * ctx,uint32_t hashAlgo,const uint8_t ** id,uint32_t * idLen,HITLS_Session ** session)1520 static int32_t Test_PskUseSessionCb_WithSHA256(HITLS_Ctx *ctx, uint32_t hashAlgo, const uint8_t **id,
1521     uint32_t *idLen, HITLS_Session **session)
1522 {
1523     (void)ctx;
1524     (void)hashAlgo;
1525     (void)id;
1526     (void)idLen;
1527     static uint8_t identity[] = "123456";
1528 
1529     if (g_sessionSz > 0) {
1530         *id = identity;
1531         *idLen = sizeof(identity);
1532         *session = GetSession(g_sessionSz - 1);
1533         (*session)->cipherSuite = HITLS_AES_128_GCM_SHA256;
1534         return HITLS_PSK_USE_SESSION_CB_SUCCESS;
1535     }
1536     return HITLS_PSK_USE_SESSION_CB_FAIL;
1537 }
Test_PskUseSessionCb_WithSHA384(HITLS_Ctx * ctx,uint32_t hashAlgo,const uint8_t ** id,uint32_t * idLen,HITLS_Session ** session)1538 static int32_t Test_PskUseSessionCb_WithSHA384(HITLS_Ctx *ctx, uint32_t hashAlgo, const uint8_t **id,
1539     uint32_t *idLen, HITLS_Session **session)
1540 {
1541     (void)ctx;
1542     (void)hashAlgo;
1543     (void)id;
1544     (void)idLen;
1545     static uint8_t identity[] = "123456";
1546 
1547     if (g_sessionSz > 0) {
1548         *id = identity;
1549         *idLen = sizeof(identity);
1550         *session = GetSession(g_sessionSz - 1);
1551         (*session)->cipherSuite = HITLS_AES_256_GCM_SHA384;
1552         return HITLS_PSK_USE_SESSION_CB_SUCCESS;
1553     }
1554     return HITLS_PSK_USE_SESSION_CB_FAIL;
1555 }
Test_PskUseSessionCb_Default(HITLS_Ctx * ctx,uint32_t hashAlgo,const uint8_t ** id,uint32_t * idLen,HITLS_Session ** session)1556 static int32_t Test_PskUseSessionCb_Default(HITLS_Ctx *ctx, uint32_t hashAlgo, const uint8_t **id,
1557     uint32_t *idLen, HITLS_Session **session)
1558 {
1559     (void)ctx;
1560     (void)hashAlgo;
1561     (void)id;
1562     (void)idLen;
1563     static uint8_t identity[] = "123456";
1564 
1565     if (g_sessionSz > 0) {
1566         *id = identity;
1567         *idLen = sizeof(identity);
1568         *session = GetSession(g_sessionSz - 1);
1569 
1570         HITLS_Session *newSession = HITLS_SESS_New();
1571         (*session)->cipherSuite = newSession->cipherSuite;
1572         HITLS_SESS_Free(newSession);
1573 
1574         return HITLS_PSK_USE_SESSION_CB_SUCCESS;
1575     }
1576     return HITLS_PSK_USE_SESSION_CB_FAIL;
1577 }
1578 
Test_PskFindSessionCb_WithSHA256(HITLS_Ctx * ctx,const uint8_t * identity,uint32_t identityLen,HITLS_Session ** session)1579 static int32_t Test_PskFindSessionCb_WithSHA256(HITLS_Ctx *ctx, const uint8_t *identity, uint32_t identityLen,
1580     HITLS_Session **session)
1581 {
1582     (void)ctx;
1583     (void)identity;
1584     (void)identityLen;
1585 
1586     if (g_sessionSz > 0) {
1587         *session = GetSession(g_sessionSz - 1);
1588         (*session)->cipherSuite = HITLS_AES_128_GCM_SHA256;
1589         return HITLS_PSK_FIND_SESSION_CB_SUCCESS;
1590     }
1591     return HITLS_PSK_FIND_SESSION_CB_FAIL;
1592 }
1593 
Test_PskFindSessionCb_WithSHA384(HITLS_Ctx * ctx,const uint8_t * identity,uint32_t identityLen,HITLS_Session ** session)1594 static int32_t Test_PskFindSessionCb_WithSHA384(HITLS_Ctx *ctx, const uint8_t *identity, uint32_t identityLen,
1595     HITLS_Session **session)
1596 {
1597     (void)ctx;
1598     (void)identity;
1599     (void)identityLen;
1600 
1601     if (g_sessionSz > 0) {
1602         *session = GetSession(g_sessionSz - 1);
1603         (*session)->cipherSuite = HITLS_AES_256_GCM_SHA384;
1604         return HITLS_PSK_FIND_SESSION_CB_SUCCESS;
1605     }
1606     return HITLS_PSK_FIND_SESSION_CB_FAIL;
1607 }
1608 
Test_PskFindSessionCb_Default(HITLS_Ctx * ctx,const uint8_t * identity,uint32_t identityLen,HITLS_Session ** session)1609 static int32_t Test_PskFindSessionCb_Default(HITLS_Ctx *ctx, const uint8_t *identity, uint32_t identityLen,
1610     HITLS_Session **session)
1611 {
1612     (void)ctx;
1613     (void)identity;
1614     (void)identityLen;
1615 
1616     if (g_sessionSz > 0) {
1617         *session = GetSession(g_sessionSz - 1);
1618 
1619         HITLS_Session *newSession = HITLS_SESS_New();
1620         (*session)->cipherSuite = newSession->cipherSuite;
1621         HITLS_SESS_Free(newSession);
1622 
1623         return HITLS_PSK_FIND_SESSION_CB_SUCCESS;
1624     }
1625     return HITLS_PSK_FIND_SESSION_CB_FAIL;
1626 }
1627 
1628 /** @
1629 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC001
1630 * @brief 4.2.11-Pre-Shared Key Extension-94
1631 * @spec  For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
1632 *        SHA-256 if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
1633 *        cipher suite.
1634 * @title  The hash settings on the client and server are inconsistent.
1635 * @precon  nan
1636 * @brief
1637 * 1. Preset the PSK. The client invokes HITLS_PskUseSessionCb to set the hash to sha256, and the service invokes
1638 *    HITLS_PskFindSessionCb to set the hash to sha384.
1639 * 2. Connect establishment
1640 * @expect
1641 * 1. The setting is successful.
1642 * 2. Connect establishment fails.
1643 @ */
1644 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC001()1645 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC001()
1646 {
1647     FRAME_Init();
1648 
1649     ResumeTestInfo testInfo = {0};
1650     testInfo.version = HITLS_VERSION_TLS13;
1651     testInfo.uioType = BSL_UIO_TCP;
1652     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1653     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1654     HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
1655 
1656     // Preset the PSK.
1657     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1658 
1659     FRAME_FreeLink(testInfo.client);
1660     testInfo.client = NULL;
1661     FRAME_FreeLink(testInfo.server);
1662     testInfo.server = NULL;
1663 
1664     // The client invokes HITLS_PskUseSessionCb to set the hash to sha256, and the service invokes
1665     // HITLS_PskFindSessionCb to set the hash to sha384.
1666     HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
1667     HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA384);
1668 
1669     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1670     ASSERT_TRUE(testInfo.client != NULL);
1671     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1672     ASSERT_TRUE(testInfo.server != NULL);
1673 
1674     // Connect establishment
1675     ASSERT_TRUE(
1676         FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT) == HITLS_MSG_HANDLE_PSK_INVALID);
1677 EXIT:
1678     ClearSessoins();
1679     HITLS_CFG_FreeConfig(testInfo.c_config);
1680     HITLS_CFG_FreeConfig(testInfo.s_config);
1681     FRAME_FreeLink(testInfo.client);
1682     FRAME_FreeLink(testInfo.server);
1683     HITLS_SESS_Free(testInfo.clientSession);
1684 }
1685 /* END_CASE */
1686 
1687 /** @   UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC007
1688 * @test  UT_TLS13_RFC8446_PSKHASH_TC001_1
1689 * @brief 4.2.11-Pre-Shared Key Extension-94
1690 * @spec  For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
1691 *        SHA-256. if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
1692 *        cipher suite.
1693 * @title  The hash settings on the client and server are inconsistent.
1694 * @precon  nan
1695 * @brief
1696 * 1. Preset the PSK. The client invokes HITLS_PskUseSessionCb to set the hash to sha384, and the service invokes
1697 *    HITLS_PskFindSessionCb to set the hash to sha256.
1698 * 2. Connect establishment
1699 * @expect
1700 * 1. The setting is successful.
1701 * 2. Connect establishment fails.
1702 @ */
1703 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC007()1704 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC007()
1705 {
1706     FRAME_Init();
1707 
1708     ResumeTestInfo testInfo = {0};
1709     testInfo.version = HITLS_VERSION_TLS13;
1710     testInfo.uioType = BSL_UIO_TCP;
1711     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1712     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1713     HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
1714 
1715     // Preset the PSK.
1716     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1717 
1718     FRAME_FreeLink(testInfo.client);
1719     testInfo.client = NULL;
1720     FRAME_FreeLink(testInfo.server);
1721     testInfo.server = NULL;
1722 
1723     // The client invokes HITLS_PskUseSessionCb to set the hash to sha384, and the service invokes
1724     // HITLS_PskFindSessionCb to set the hash to sha256.
1725     HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA384);
1726     HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
1727 
1728     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1729     ASSERT_TRUE(testInfo.client != NULL);
1730     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1731     ASSERT_TRUE(testInfo.server != NULL);
1732 
1733     // Connect establishment
1734     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) , HITLS_SUCCESS);
1735     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, HS_STATE_BUTT) , HITLS_SUCCESS);
1736 EXIT:
1737     ClearSessoins();
1738     HITLS_CFG_FreeConfig(testInfo.c_config);
1739     HITLS_CFG_FreeConfig(testInfo.s_config);
1740     FRAME_FreeLink(testInfo.client);
1741     FRAME_FreeLink(testInfo.server);
1742     HITLS_SESS_Free(testInfo.clientSession);
1743 }
1744 /* END_CASE */
1745 
1746 /** @
1747 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC002
1748 * @brief 4.2.11-Pre-Shared Key Extension-94
1749 * @spec  For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
1750 *        SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
1751 *        cipher suite.
1752 * @title  The hash set does not match the hash of the negotiated cipher suite.
1753 * @precon  nan
1754 * @brief
1755 * 1. Preset the PSK. The client and service monotonically use the HITLS_PskUseSessionCb to set the hash to sha256 and
1756 *    the negotiation cipher suite to sha384.
1757 * 2. Connect establishment
1758 * @expect
1759 * 1. The setting is successful.
1760 * 2. If the PSK authentication fails, perform certificate authentication.
1761 @ */
1762 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC002()1763 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC002()
1764 {
1765     FRAME_Init();
1766 
1767     ResumeTestInfo testInfo = {0};
1768     testInfo.version = HITLS_VERSION_TLS13;
1769     testInfo.uioType = BSL_UIO_TCP;
1770     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1771     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1772     HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
1773 
1774     // Preset the PSK.
1775     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1776 
1777     FRAME_FreeLink(testInfo.client);
1778     testInfo.client = NULL;
1779     FRAME_FreeLink(testInfo.server);
1780     testInfo.server = NULL;
1781 
1782     // The client and service monotonically use the HITLS_PskUseSessionCb to set the hash to sha256 and the negotiation
1783     // cipher suite to sha384.
1784     uint16_t cipher_suite[] = {HITLS_AES_256_GCM_SHA384};
1785     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
1786     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
1787     HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
1788     HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
1789 
1790     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1791     ASSERT_TRUE(testInfo.client != NULL);
1792     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1793     ASSERT_TRUE(testInfo.server != NULL);
1794 
1795     // Connect establishment
1796     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE), HITLS_SUCCESS);
1797     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_SUCCESS);
1798 
1799 EXIT:
1800     ClearSessoins();
1801     HITLS_CFG_FreeConfig(testInfo.c_config);
1802     HITLS_CFG_FreeConfig(testInfo.s_config);
1803     FRAME_FreeLink(testInfo.client);
1804     FRAME_FreeLink(testInfo.server);
1805     HITLS_SESS_Free(testInfo.clientSession);
1806 }
1807 /* END_CASE */
1808 
1809 /** @
1810 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC003
1811 * @brief 4.2.11-Pre-Shared Key Extension-94
1812 * @spec  For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
1813 *        SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
1814 *        cipher suite.
1815 * @title  The hash set by the hash matches the hash of the negotiated cipher suite.
1816 * @precon  nan
1817 * @brief
1818 * 1. Preset the PSK. Use the HITLS_PskUseSessionCb command to set the hash to sha256 and the negotiation cipher suite to
1819 *    sha256.
1820 * 2. Connect establishment
1821 * @expect
1822 * 1. The setting is successful.
1823 * 2. The connection is set up successfully.
1824 @ */
1825 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC003()1826 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC003()
1827 {
1828     FRAME_Init();
1829 
1830     ResumeTestInfo testInfo = {0};
1831     testInfo.version = HITLS_VERSION_TLS13;
1832     testInfo.uioType = BSL_UIO_TCP;
1833     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1834     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1835     HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
1836 
1837     // Preset the PSK.
1838     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1839 
1840     FRAME_FreeLink(testInfo.client);
1841     testInfo.client = NULL;
1842     FRAME_FreeLink(testInfo.server);
1843     testInfo.server = NULL;
1844 
1845     // Use the HITLS_PskUseSessionCb command to set the hash to sha256 and the negotiation cipher suite to sha256.
1846     uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
1847     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
1848     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
1849     HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
1850     HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
1851 
1852     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1853     ASSERT_TRUE(testInfo.client != NULL);
1854     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1855     ASSERT_TRUE(testInfo.server != NULL);
1856 
1857     // Connect establishment
1858     ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
1859 
1860     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
1861     uint8_t *recBuf = ioUserData->recMsg.msg;
1862     uint32_t recLen = ioUserData->recMsg.len;
1863     ASSERT_TRUE(recLen != 0);
1864 
1865     uint32_t parseLen = 0;
1866     FRAME_Msg frameMsg = {0};
1867     FRAME_Type frameType = {0};
1868     frameType.versionType = HITLS_VERSION_TLS13;
1869     frameType.recordType = REC_TYPE_HANDSHAKE;
1870     frameType.handshakeType = SERVER_HELLO;
1871     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
1872     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
1873 
1874     FRAME_ServerHelloMsg *ServerHello = &frameMsg.body.hsMsg.body.serverHello;
1875     ASSERT_TRUE(ServerHello->pskSelectedIdentity.data.data == 0);
1876 
1877     ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
1878 
1879 EXIT:
1880     ClearSessoins();
1881     FRAME_CleanMsg(&frameType, &frameMsg);
1882     HITLS_CFG_FreeConfig(testInfo.c_config);
1883     HITLS_CFG_FreeConfig(testInfo.s_config);
1884     FRAME_FreeLink(testInfo.client);
1885     FRAME_FreeLink(testInfo.server);
1886     HITLS_SESS_Free(testInfo.clientSession);
1887 }
1888 /* END_CASE */
1889 
1890 /** @
1891 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC004
1892 * @brief 4.2.11-Pre-Shared Key Extension-94
1893 * @spec  For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
1894 *        SHA-256 if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
1895 *        cipher suite.
1896 * @title  The default client hash is sha256.
1897 * @precon  nan
1898 * @brief
1899 * 1. Preset the PSK. The client does not set the hash algorithm. The server sets the hash algorithm to 256 and the
1900 *     negotiation cipher suite to sha256.
1901 * 2. Establish a connection and check the hash algorithm on the client.
1902 * @expect
1903 * 1. The setting is successful.
1904 * 2. The connection is set up successfully, and the hash algorithm on the client is 256.
1905 @ */
1906 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC004()1907 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC004()
1908 {
1909     FRAME_Init();
1910 
1911     ResumeTestInfo testInfo = {0};
1912     testInfo.version = HITLS_VERSION_TLS13;
1913     testInfo.uioType = BSL_UIO_TCP;
1914     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1915     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1916     HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
1917 
1918     uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
1919 
1920     // Preset the PSK.
1921     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1922 
1923     FRAME_FreeLink(testInfo.client);
1924     testInfo.client = NULL;
1925     FRAME_FreeLink(testInfo.server);
1926     testInfo.server = NULL;
1927 
1928     // The client does not set the hash algorithm. The server sets the hash algorithm to 256 and the negotiation cipher
1929     // suite to sha256.
1930     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
1931     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
1932     HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_Default);
1933     HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_WithSHA256);
1934 
1935     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1936     ASSERT_TRUE(testInfo.client != NULL);
1937     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
1938     ASSERT_TRUE(testInfo.server != NULL);
1939 
1940     // Establish a connection and check the hash algorithm on the client.
1941     ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) != HITLS_SUCCESS);
1942     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
1943 EXIT:
1944     HITLS_CFG_FreeConfig(testInfo.c_config);
1945     HITLS_CFG_FreeConfig(testInfo.s_config);
1946     FRAME_FreeLink(testInfo.client);
1947     FRAME_FreeLink(testInfo.server);
1948     HITLS_SESS_Free(testInfo.clientSession);
1949 }
1950 /* END_CASE */
1951 
1952 /** @
1953 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC005
1954 * @brief 4.2.11-Pre-Shared Key Extension-94
1955 * @spec  For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
1956 *        SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
1957 *        cipher suite.
1958 * @title  The default hash on the server is sha256.
1959 * @precon  nan
1960 * @brief
1961 * 1. Preset the PSK. The server does not set the hash algorithm. The client sets the hash algorithm to 256 and the
1962 *    negotiation cipher suite to sha256.
1963 * 2. Establish a connection and check the hash algorithm on the server.
1964 * @expect
1965 * 1. The setting is successful.
1966 * 2. The connection is set up successfully, and the hash algorithm on the server is 256.
1967 @ */
1968 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC005()1969 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC005()
1970 {
1971     FRAME_Init();
1972 
1973     ResumeTestInfo testInfo = {0};
1974     testInfo.version = HITLS_VERSION_TLS13;
1975     testInfo.uioType = BSL_UIO_TCP;
1976     testInfo.c_config = HITLS_CFG_NewTLS13Config();
1977     testInfo.s_config = HITLS_CFG_NewTLS13Config();
1978     HITLS_CFG_SetNewSessionCb(testInfo.c_config, Test_NewSessionCb);
1979 
1980     uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
1981 
1982     // Preset the PSK.
1983     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
1984 
1985     FRAME_FreeLink(testInfo.client);
1986     testInfo.client = NULL;
1987     FRAME_FreeLink(testInfo.server);
1988     testInfo.server = NULL;
1989 
1990     // The server does not set the hash algorithm. The client sets the hash algorithm to 256 and the negotiation cipher
1991     // suite to sha256.
1992     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
1993     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite) / sizeof(uint16_t));
1994     HITLS_CFG_SetPskUseSessionCallback(testInfo.c_config, Test_PskUseSessionCb_WithSHA256);
1995     HITLS_CFG_SetPskFindSessionCallback(testInfo.s_config, Test_PskFindSessionCb_Default);
1996 
1997     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
1998     ASSERT_TRUE(testInfo.client != NULL);
1999     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2000     ASSERT_TRUE(testInfo.server != NULL);
2001 
2002     // Establish a connection and check the hash algorithm on the server.
2003     ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) != HITLS_SUCCESS);
2004     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
2005 EXIT:
2006     HITLS_CFG_FreeConfig(testInfo.c_config);
2007     HITLS_CFG_FreeConfig(testInfo.s_config);
2008     FRAME_FreeLink(testInfo.client);
2009     FRAME_FreeLink(testInfo.server);
2010     HITLS_SESS_Free(testInfo.clientSession);
2011 }
2012 /* END_CASE */
2013 
2014 /** @
2015 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC006
2016 * @brief 4.2.11-Pre-Shared Key Extension-94
2017 * @spec  For externally established PSKs, the Hash algorithm MUST be set when the PSK is established or default to
2018 *        SHA-256.if no such algorithm is defined. The server MUST ensure that it selects a compatible PSK (if any) and
2019 *         cipher suite.
2020 * @title  The hash setting is inconsistent with the negotiated cipher suite.
2021 * @precon  nan
2022 * @brief
2023 * 1. The client invokes the HITLS_PskClientCb interface to set the preset PSK. The server invokes the HITLS_PskClientCb
2024 *     interface to set the preset PSK.
2025 * 2. The algorithm suite negotiation result is 384,
2026 * 3. Establish a connection.
2027 * @expect
2028 * 1. The setting is successful.
2029 * 2. The setting is successful.
2030 * 3. The connection is successfully established and certificate authentication is performed.
2031 @ */
2032 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC006()2033 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKHASH_FUNC_TC006()
2034 {
2035     FRAME_Init();
2036 
2037     ResumeTestInfo testInfo = {0};
2038     testInfo.version = HITLS_VERSION_TLS13;
2039     testInfo.uioType = BSL_UIO_TCP;
2040     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2041     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2042 
2043     // The client invokes the HITLS_PskClientCb interface to set the preset PSK. The server invokes the HITLS_PskClientCb interface to set the preset PSK.
2044     char psk[] = "aaaaaaaaaaaaaaaa";
2045     ASSERT_TRUE(ExampleSetPsk(psk) == HITLS_SUCCESS);
2046     ASSERT_TRUE(HITLS_CFG_SetPskClientCallback(testInfo.c_config, ExampleClientCb) == HITLS_SUCCESS);
2047     ASSERT_TRUE(HITLS_CFG_SetPskServerCallback(testInfo.s_config, ExampleServerCb) == HITLS_SUCCESS);
2048 
2049     // The algorithm suite negotiation result is 384
2050     uint16_t cipher_suite[] = { HITLS_AES_256_GCM_SHA384 };
2051     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2052     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2053 
2054     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2055     ASSERT_TRUE(testInfo.client != NULL);
2056     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2057     ASSERT_TRUE(testInfo.server != NULL);
2058 
2059     // Establish a connection.
2060     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) , HITLS_SUCCESS);
2061     ASSERT_TRUE(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
2062 
2063 EXIT:
2064     HITLS_CFG_FreeConfig(testInfo.c_config);
2065     HITLS_CFG_FreeConfig(testInfo.s_config);
2066     FRAME_FreeLink(testInfo.client);
2067     FRAME_FreeLink(testInfo.server);
2068     HITLS_SESS_Free(testInfo.clientSession);
2069 }
2070 /* END_CASE */
2071 
2072 /** @
2073 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC001
2074 * @brief 4.2.11-Pre-Shared Key Extension-97
2075 * @spec  the server MUST validate the corresponding binder value (see Section 4.2.11.2 below).
2076 *         If this value is not present or does not validate, the server MUST abort the handshake.
2077 * @title  Modify the binder of client hello to make the server verification fail.
2078 * @precon  nan
2079 * @brief
2080 * 1. The connection is established and the session is restored.
2081 * 2. Change the value of binder in the psk extension of the client hello message sent by the client, and observe the
2082 *    behavior on the server.
2083 * @expect
2084 * 1. The setting is successful.
2085 * 2. The server terminates the handshake.
2086 @ */
2087 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC001()2088 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC001()
2089 {
2090     FRAME_Init();
2091 
2092     ResumeTestInfo testInfo = {0};
2093     testInfo.version = HITLS_VERSION_TLS13;
2094     testInfo.uioType = BSL_UIO_TCP;
2095 
2096     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2097     ASSERT_TRUE(testInfo.s_config != NULL);
2098     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2099     ASSERT_TRUE(testInfo.c_config != NULL);
2100 
2101     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
2102 
2103     // The connection is established and the session is restored.
2104     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
2105     ASSERT_TRUE(testInfo.clientSession != NULL);
2106 
2107     FRAME_FreeLink(testInfo.client);
2108     testInfo.client = NULL;
2109     FRAME_FreeLink(testInfo.server);
2110     testInfo.server = NULL;
2111 
2112     // Change the value of binder in the psk extension of the client hello message sent by the client, and observe the
2113     // behavior on the server.
2114     RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_Binder_Unnormal};
2115     RegisterWrapper(wrapper);
2116 
2117     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2118     ASSERT_TRUE(testInfo.client != NULL);
2119     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2120     ASSERT_TRUE(testInfo.server != NULL);
2121     ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
2122 
2123     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_PSK_INVALID);
2124 EXIT:
2125     ClearWrapper();
2126     HITLS_CFG_FreeConfig(testInfo.c_config);
2127     HITLS_CFG_FreeConfig(testInfo.s_config);
2128     FRAME_FreeLink(testInfo.client);
2129     FRAME_FreeLink(testInfo.server);
2130     HITLS_SESS_Free(testInfo.clientSession);
2131 }
2132 /* END_CASE */
2133 
2134 /** @
2135 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC003
2136 * @brief 4.2.11-Pre-Shared Key Extension-97
2137 * @spec  the server MUST validate the corresponding binder value (see Section 4.2.11.2 below).
2138 *         If this value is not present or does not validate, the server MUST abort the handshake.
2139 * @title  Modify the client hello message so that the server fails to verify the binder.
2140 * @precon  nan
2141 * @brief
2142 * 1. The connection is established and the session is restored.
2143 * 2. Discard the master_secret extension of the client hello message sent by the client and observe the behavior of the
2144 *    server.
2145 * @expect
2146 * 1. The setting is successful.
2147 * 2. The server terminates the handshake.
2148 @ */
2149 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC003()2150 void UT_TLS_TLS13_RFC8446_CONSISTENCY_PSKBINDER_FUNC_TC003()
2151 {
2152     FRAME_Init();
2153 
2154     ResumeTestInfo testInfo = {0};
2155     testInfo.version = HITLS_VERSION_TLS13;
2156     testInfo.uioType = BSL_UIO_TCP;
2157 
2158     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2159     ASSERT_TRUE(testInfo.s_config != NULL);
2160     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2161     ASSERT_TRUE(testInfo.c_config != NULL);
2162 
2163     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
2164 
2165     // The connection is established and the session is restored.
2166     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
2167     ASSERT_TRUE(testInfo.clientSession != NULL);
2168 
2169     FRAME_FreeLink(testInfo.client);
2170     testInfo.client = NULL;
2171     FRAME_FreeLink(testInfo.server);
2172     testInfo.server = NULL;
2173 
2174     // Discard the master_secret extension of the client hello message sent by the client and observe the behavior of
2175     // the server.
2176     RecWrapper wrapper = {TRY_SEND_CLIENT_HELLO, REC_TYPE_HANDSHAKE, false, NULL, Test_Client_MasterSecret_Miss};
2177     RegisterWrapper(wrapper);
2178 
2179     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2180     ASSERT_TRUE(testInfo.client != NULL);
2181     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2182     ASSERT_TRUE(testInfo.server != NULL);
2183     ASSERT_EQ(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession), HITLS_SUCCESS);
2184 
2185     ASSERT_EQ(
2186         FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT), HITLS_MSG_HANDLE_PSK_INVALID);
2187 EXIT:
2188     ClearWrapper();
2189     HITLS_CFG_FreeConfig(testInfo.c_config);
2190     HITLS_CFG_FreeConfig(testInfo.s_config);
2191     FRAME_FreeLink(testInfo.client);
2192     FRAME_FreeLink(testInfo.server);
2193     HITLS_SESS_Free(testInfo.clientSession);
2194 }
2195 /* END_CASE */
2196 
2197 /** @
2198 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC001
2199 * @brief 4.2.11-Pre-Shared Key Extension-97
2200 * @spec  Implementations MUST NOT send any records with a version less than 0x0300.
2201 *         Implementations SHOULD NOT accept any records with a version less than 0x0300
2202 * @title  The server receives a client hello message whose recode version is 0x0300/0x0200.
2203 * @precon  nan
2204 * @brief
2205 * 1. Change the recode version of the client hello message sent by the client to 0x0300/0x0200.
2206 * 2. Observe the server behavior.
2207 * @expect
2208 * 1. The setting is successful.
2209 * 2. Connect establishment success/failure
2210 @ */
2211 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC001(int value,int expect)2212 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC001(int value, int expect)
2213 {
2214     FRAME_Init();
2215 
2216     ResumeTestInfo testInfo = {0};
2217     testInfo.version = HITLS_VERSION_TLS13;
2218     testInfo.uioType = BSL_UIO_TCP;
2219 
2220     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2221     ASSERT_TRUE(testInfo.s_config != NULL);
2222     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2223     ASSERT_TRUE(testInfo.c_config != NULL);
2224 
2225     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2226     ASSERT_TRUE(testInfo.client != NULL);
2227     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2228     ASSERT_TRUE(testInfo.server != NULL);
2229 
2230     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_RECV_CLIENT_HELLO), HITLS_SUCCESS);
2231 
2232     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.server->io);
2233     uint8_t *recvBuf = ioUserData->recMsg.msg;
2234     uint32_t recvLen = ioUserData->recMsg.len;
2235     ASSERT_TRUE(recvLen != 0);
2236 
2237     /* Change the recode version of the client hello message sent by the client to 0x0300/0x0200. */
2238     uint32_t parseLen = 0;
2239     FRAME_Msg frameMsg = {0};
2240     FRAME_Type frameType = {0};
2241     frameType.versionType = HITLS_VERSION_TLS13;
2242     frameType.recordType = REC_TYPE_HANDSHAKE;
2243     frameType.handshakeType = CLIENT_HELLO;
2244     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
2245     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
2246 
2247     frameMsg.recVersion.data = value;
2248 
2249     uint32_t sendLen = MAX_RECORD_LENTH;
2250     uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
2251     ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
2252 
2253     ioUserData->recMsg.len = 0;
2254     ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.server->io, sendBuf, sendLen) == HITLS_SUCCESS);
2255     FRAME_CleanMsg(&frameType, &frameMsg);
2256     memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
2257 
2258     /* Observe the server behavior. */
2259     ASSERT_TRUE(testInfo.server->ssl != NULL);
2260     ASSERT_EQ(HITLS_Accept(testInfo.server->ssl), expect);
2261 EXIT:
2262     HITLS_CFG_FreeConfig(testInfo.c_config);
2263     HITLS_CFG_FreeConfig(testInfo.s_config);
2264     FRAME_FreeLink(testInfo.client);
2265     FRAME_FreeLink(testInfo.server);
2266 }
2267 /* END_CASE */
2268 
2269 /** @
2270 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC002
2271 * @brief 4.2.11-Pre-Shared Key Extension-97
2272 * @spec  Implementations MUST NOT send any records with a version less than 0x0300.
2273 *         Implementations SHOULD NOT accept any records with a version less than 0x0300
2274 * @title  The client receives the server hello message whose recode version is 0x0300/0x0200.
2275 * @precon  nan
2276 * @brief
2277 * 1. Change the recode version of the client hello message sent by the server to 0x0300/0x0200.
2278 * 2. Observe client behavior.
2279 * @expect
2280 * 1. The setting is successful.
2281 * 2. Connect establishment success/failure
2282 @ */
2283 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC002(int value,int expect)2284 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECODE_VERSION_FUNC_TC002(int value, int expect)
2285 {
2286     FRAME_Init();
2287 
2288     ResumeTestInfo testInfo = {0};
2289     testInfo.version = HITLS_VERSION_TLS13;
2290     testInfo.uioType = BSL_UIO_TCP;
2291 
2292     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2293     ASSERT_TRUE(testInfo.s_config != NULL);
2294     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2295     ASSERT_TRUE(testInfo.c_config != NULL);
2296 
2297     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2298     ASSERT_TRUE(testInfo.client != NULL);
2299     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2300     ASSERT_TRUE(testInfo.server != NULL);
2301 
2302     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
2303 
2304     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
2305     uint8_t *recvBuf = ioUserData->recMsg.msg;
2306     uint32_t recvLen = ioUserData->recMsg.len;
2307     ASSERT_TRUE(recvLen != 0);
2308 
2309     /* Change the recode version of the client hello message sent by the server to 0x0300/0x0200. */
2310     uint32_t parseLen = 0;
2311     FRAME_Msg frameMsg = {0};
2312     FRAME_Type frameType = {0};
2313     frameType.versionType = HITLS_VERSION_TLS13;
2314     frameType.recordType = REC_TYPE_HANDSHAKE;
2315     frameType.handshakeType = SERVER_HELLO;
2316     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
2317     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
2318 
2319     frameMsg.recVersion.data = value;
2320 
2321     uint32_t sendLen = MAX_RECORD_LENTH;
2322     uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
2323     ASSERT_TRUE(FRAME_PackMsg(&frameType, &frameMsg, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
2324 
2325     ioUserData->recMsg.len = 0;
2326     ASSERT_TRUE(FRAME_TransportRecMsg(testInfo.client->io, sendBuf, sendLen) == HITLS_SUCCESS);
2327     FRAME_CleanMsg(&frameType, &frameMsg);
2328     memset_s(&frameMsg, sizeof(frameMsg), 0, sizeof(frameMsg));
2329 
2330     /* Observe client behavior. */
2331     ASSERT_TRUE(testInfo.server->ssl != NULL);
2332     ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), expect);
2333 EXIT:
2334     HITLS_CFG_FreeConfig(testInfo.c_config);
2335     HITLS_CFG_FreeConfig(testInfo.s_config);
2336     FRAME_FreeLink(testInfo.client);
2337     FRAME_FreeLink(testInfo.server);
2338 }
2339 /* END_CASE */
2340 
2341 /** @
2342 * @test UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC001
2343 * @brief 4.2.11-Pre-Shared Key Extension-94
2344 * @spec
2345 * @title  Two PSKs. Select the first one when the conditions are met.
2346 * @precon  nan
2347 * @brief
2348 * 1. The PSK is generated during connection establishment, and set to the client.
2349 * 2. Preset the PSK and establish a connection.
2350 * @expect
2351 * 1. The setting is successful.
2352 * 2. The connection is successfully set up and the first PSK is selected.
2353 @ */
2354 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC001()2355 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC001()
2356 {
2357     FRAME_Init();
2358 
2359     ResumeTestInfo testInfo = {0};
2360     testInfo.version = HITLS_VERSION_TLS13;
2361     testInfo.uioType = BSL_UIO_TCP;
2362     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2363     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2364 
2365     uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
2366     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2367     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2368 
2369     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
2370     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
2371     ASSERT_TRUE(testInfo.clientSession != NULL);
2372 
2373     FRAME_FreeLink(testInfo.client);
2374     testInfo.client = NULL;
2375     FRAME_FreeLink(testInfo.server);
2376     testInfo.server = NULL;
2377 
2378     // The PSK is generated during connection establishment, and set to the client.
2379     HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
2380     HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
2381 
2382     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2383     ASSERT_TRUE(testInfo.client != NULL);
2384     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2385     ASSERT_TRUE(testInfo.server != NULL);
2386     ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
2387 
2388     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
2389 
2390     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
2391     uint8_t *recvBuf = ioUserData->recMsg.msg;
2392     uint32_t recvLen = ioUserData->recMsg.len;
2393     ASSERT_TRUE(recvLen != 0);
2394 
2395     uint32_t parseLen = 0;
2396     FRAME_Msg frameMsg = {0};
2397     FRAME_Type frameType = {0};
2398     frameType.versionType = HITLS_VERSION_TLS13;
2399     frameType.recordType = REC_TYPE_HANDSHAKE;
2400     frameType.handshakeType = SERVER_HELLO;
2401     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
2402     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
2403 
2404     /* Preset the PSK and establish a connection. */
2405     FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
2406     ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data != 0);
2407     ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 0);
2408 
2409     ASSERT_TRUE(testInfo.client->ssl != NULL);
2410     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
2411 EXIT:
2412     FRAME_CleanMsg(&frameType, &frameMsg);
2413     HITLS_SESS_Free(testInfo.clientSession);
2414     HITLS_CFG_FreeConfig(testInfo.c_config);
2415     HITLS_CFG_FreeConfig(testInfo.s_config);
2416     FRAME_FreeLink(testInfo.client);
2417     FRAME_FreeLink(testInfo.server);
2418 }
2419 /* END_CASE */
2420 
2421 /** @
2422 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC002
2423 * @brief 4.2.11-Pre-Shared Key Extension-94
2424 * @spec
2425 * @title  Two psks. If the former one does not meet the requirements, select the second one.
2426 * @precon  nan
2427 * @brief
2428 * 1. The PSK is generated during connection establishment. Configure the default 384 algorithm suite on the client.
2429 * 2. Set the 256 cipher suite, preset the PSK, and establish a connection.
2430 * @expect
2431 * 1. The setting is successful.
2432 * 2. The connection is successfully established and the second cipher suite is selected.
2433 @ */
2434 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC002()2435 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC002()
2436 {
2437     FRAME_Init();
2438 
2439     ResumeTestInfo testInfo = {0};
2440     testInfo.version = HITLS_VERSION_TLS13;
2441     testInfo.uioType = BSL_UIO_TCP;
2442     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2443     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2444 
2445     // The PSK is generated during connection establishment. Configure the default 384 algorithm suite on the client.
2446     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
2447     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
2448     ASSERT_TRUE(testInfo.clientSession != NULL);
2449 
2450     FRAME_FreeLink(testInfo.client);
2451     testInfo.client = NULL;
2452     FRAME_FreeLink(testInfo.server);
2453     testInfo.server = NULL;
2454 
2455     /* Set the 256 cipher suite, preset the PSK, and establish a connection. */
2456     uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256, HITLS_AES_256_GCM_SHA384 };
2457     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2458     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite[0])/sizeof(uint16_t));
2459 
2460     HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
2461     HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
2462 
2463     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2464     ASSERT_TRUE(testInfo.client != NULL);
2465     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2466     ASSERT_TRUE(testInfo.server != NULL);
2467     ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
2468 
2469     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
2470 
2471     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
2472     uint8_t *recvBuf = ioUserData->recMsg.msg;
2473     uint32_t recvLen = ioUserData->recMsg.len;
2474     ASSERT_TRUE(recvLen != 0);
2475 
2476     uint32_t parseLen = 0;
2477     FRAME_Msg frameMsg = {0};
2478     FRAME_Type frameType = {0};
2479     frameType.versionType = HITLS_VERSION_TLS13;
2480     frameType.recordType = REC_TYPE_HANDSHAKE;
2481     frameType.handshakeType = SERVER_HELLO;
2482     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
2483     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
2484 
2485     FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
2486     ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data != 0);
2487     ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 1);
2488 
2489     ASSERT_TRUE(testInfo.client->ssl != NULL);
2490     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
2491 EXIT:
2492     FRAME_CleanMsg(&frameType, &frameMsg);
2493     HITLS_SESS_Free(testInfo.clientSession);
2494     HITLS_CFG_FreeConfig(testInfo.c_config);
2495     HITLS_CFG_FreeConfig(testInfo.s_config);
2496     FRAME_FreeLink(testInfo.client);
2497     FRAME_FreeLink(testInfo.server);
2498 }
2499 /* END_CASE */
2500 
2501 /** @
2502 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC003
2503 * @brief 4.2.11-Pre-Shared Key Extension-94
2504 * @spec
2505 * @title Neither of the two PSKs meets the requirements. Select certificate authentication.
2506 * @precon nan
2507 * @brief
2508 * 1. Set the 256 algorithm suite and set up a connection to generate a PSK, and set to the client.
2509 * 2. Set the 384 algorithm suite, preset the PSK, and establish a connection.
2510 * @expect
2511 * 1. The setting is successful.
2512 * 2. The connection is successfully established and the PSK authentication is performed.
2513 @ */
2514 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC003()2515 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC003()
2516 {
2517     FRAME_Init();
2518 
2519     ResumeTestInfo testInfo = {0};
2520     testInfo.version = HITLS_VERSION_TLS13;
2521     testInfo.uioType = BSL_UIO_TCP;
2522     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2523     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2524 
2525     // Set the 256 algorithm suite and set up a connection to generate a PSK, and set to the client.
2526     uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256 };
2527     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2528     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2529 
2530     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
2531     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
2532     ASSERT_TRUE(testInfo.clientSession != NULL);
2533 
2534     FRAME_FreeLink(testInfo.client);
2535     testInfo.client = NULL;
2536     FRAME_FreeLink(testInfo.server);
2537     testInfo.server = NULL;
2538 
2539     // Set the 384 algorithm suite, preset the PSK, and establish a connection.
2540     cipher_suite[0] = HITLS_AES_256_GCM_SHA384;
2541     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2542     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2543 
2544     HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
2545     HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
2546 
2547     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2548     ASSERT_TRUE(testInfo.client != NULL);
2549     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2550     ASSERT_TRUE(testInfo.server != NULL);
2551     ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
2552 
2553     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
2554 
2555     /* Obtain the message buffer. */
2556     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
2557     uint8_t *recvBuf = ioUserData->recMsg.msg;
2558     uint32_t recvLen = ioUserData->recMsg.len;
2559     ASSERT_TRUE(recvLen != 0);
2560 
2561     /* Parse the structure to the msg structure. */
2562     uint32_t parseLen = 0;
2563     FRAME_Msg frameMsg = {0};
2564     FRAME_Type frameType = {0};
2565     frameType.versionType = HITLS_VERSION_TLS13;
2566     frameType.recordType = REC_TYPE_HANDSHAKE;
2567     frameType.handshakeType = SERVER_HELLO;
2568     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
2569     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
2570 
2571     FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
2572     ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data == 0);
2573 
2574     ASSERT_TRUE(testInfo.client->ssl != NULL);
2575     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, false, TRY_SEND_CERTIFICATE) , HITLS_SUCCESS);
2576     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
2577 EXIT:
2578     FRAME_CleanMsg(&frameType, &frameMsg);
2579     HITLS_SESS_Free(testInfo.clientSession);
2580     HITLS_CFG_FreeConfig(testInfo.c_config);
2581     HITLS_CFG_FreeConfig(testInfo.s_config);
2582     FRAME_FreeLink(testInfo.client);
2583     FRAME_FreeLink(testInfo.server);
2584 }
2585 /* END_CASE */
2586 
2587 /** @
2588 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC004
2589 * @brief 4.2.11-Pre-Shared Key Extension-94
2590 * @spec
2591 * @title Trigger the hrr message and select the PSK.
2592 * @precon nan
2593 * @brief
2594 * 1. The PSK is generated during connection establishment, and set on the client.
2595 * 2. Set a group so that the server triggers the hrr message, preset the PSK, and establish a connection.
2596 * @expect
2597 * 1. The setting is successful.
2598 * 2. The connection is successfully set up. The server sends the hrr message and selects the PSK.
2599 @ */
2600 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC004()2601 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC004()
2602 {
2603     FRAME_Init();
2604 
2605     ResumeTestInfo testInfo = {0};
2606     testInfo.version = HITLS_VERSION_TLS13;
2607     testInfo.uioType = BSL_UIO_TCP;
2608     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2609     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2610 
2611     // The PSK is generated during connection establishment, and set on the client.
2612     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
2613     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
2614     ASSERT_TRUE(testInfo.clientSession != NULL);
2615 
2616     FRAME_FreeLink(testInfo.client);
2617     testInfo.client = NULL;
2618     FRAME_FreeLink(testInfo.server);
2619     testInfo.server = NULL;
2620 
2621     // Set a group so that the server triggers the hrr message, preset the PSK, and establish a connection.
2622     uint16_t clientGroups[] = {HITLS_EC_GROUP_CURVE25519, HITLS_EC_GROUP_SECP256R1};
2623     HITLS_CFG_SetGroups(testInfo.c_config, clientGroups, sizeof(clientGroups) / sizeof(uint16_t));
2624     uint16_t serverGroups[] = {HITLS_EC_GROUP_SECP256R1};
2625     HITLS_CFG_SetGroups(testInfo.s_config, serverGroups, sizeof(serverGroups) / sizeof(uint16_t));
2626 
2627     HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
2628     HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
2629 
2630     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2631     ASSERT_TRUE(testInfo.client != NULL);
2632     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2633     ASSERT_TRUE(testInfo.server != NULL);
2634     ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
2635 
2636     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
2637 
2638     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
2639     uint8_t *recvBuf = ioUserData->recMsg.msg;
2640     uint32_t recvLen = ioUserData->recMsg.len;
2641     ASSERT_TRUE(recvLen != 0);
2642 
2643     uint32_t parseLen = 0;
2644     FRAME_Msg frameMsg = {0};
2645     FRAME_Type frameType = {0};
2646     frameType.versionType = HITLS_VERSION_TLS13;
2647     frameType.recordType = REC_TYPE_HANDSHAKE;
2648     frameType.handshakeType = SERVER_HELLO;
2649     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
2650     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
2651 
2652     FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
2653     ASSERT_TRUE(testInfo.server->ssl->hsCtx->haveHrr == true);
2654     serverMsg = &frameMsg.body.hsMsg.body.serverHello;
2655     ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 0);
2656     ASSERT_TRUE(testInfo.client->ssl != NULL);
2657     ASSERT_EQ(HITLS_Connect(testInfo.client->ssl), HITLS_REC_NORMAL_IO_BUSY);
2658 
2659     FrameUioUserData *ioUserData2 = BSL_UIO_GetUserData(testInfo.client->io);
2660     uint32_t sendLen = ioUserData2->sndMsg.len;
2661     ASSERT_TRUE(sendLen != 0);
2662 
2663     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
2664 EXIT:
2665     FRAME_CleanMsg(&frameType, &frameMsg);
2666     HITLS_SESS_Free(testInfo.clientSession);
2667     HITLS_CFG_FreeConfig(testInfo.c_config);
2668     HITLS_CFG_FreeConfig(testInfo.s_config);
2669     FRAME_FreeLink(testInfo.client);
2670     FRAME_FreeLink(testInfo.server);
2671 }
2672 /* END_CASE */
2673 
2674 /* @
2675 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC005
2676 * @brief 4.2.11-Pre-Shared Key Extension-94
2677 * @spec
2678 * @title Compatibility between session restoration and user-configured PSK
2679 * @precon nan
2680 * @brief
2681 1. create a tls1.3 connection
2682 2. Configure the user-defined PSK through the callback function.
2683 3. Ensure that the PSK length configured by the user is greater than the PSK length for session restoration.
2684 4. Establish a link again.
2685 * @expect
2686 1. The connection is successful.
2687 2. Configuration succeeded.
2688 3. Configuration succeeded.
2689 4. The connection is successful.
2690 * @prior  Level 2
2691 * @auto  TRUE
2692 @ */
2693 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC005()2694 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RESUMEPSK_AND_SETPSK_FUNC_TC005()
2695 {
2696     FRAME_Init();
2697 
2698     ResumeTestInfo testInfo = {0};
2699     testInfo.version = HITLS_VERSION_TLS13;
2700     testInfo.uioType = BSL_UIO_TCP;
2701     testInfo.c_config = HITLS_CFG_NewTLS13Config();
2702     testInfo.s_config = HITLS_CFG_NewTLS13Config();
2703 
2704     // The PSK is generated during link establishment. Configure the default 384 algorithm suite on the client.
2705     ASSERT_EQ(DoHandshake(&testInfo), HITLS_SUCCESS);
2706     testInfo.clientSession = HITLS_GetDupSession(testInfo.client->ssl);
2707     ASSERT_TRUE(testInfo.clientSession != NULL);
2708 
2709     FRAME_FreeLink(testInfo.client);
2710     testInfo.client = NULL;
2711     FRAME_FreeLink(testInfo.server);
2712     testInfo.server = NULL;
2713 
2714     /* Set the 256 cipher suite, preset the PSK, and establish a link. */
2715     uint16_t cipher_suite[] = { HITLS_AES_128_GCM_SHA256, HITLS_AES_256_GCM_SHA384};
2716     HITLS_CFG_SetCipherSuites(testInfo.c_config, cipher_suite, sizeof(cipher_suite)/sizeof(uint16_t));
2717     HITLS_CFG_SetCipherSuites(testInfo.s_config, cipher_suite, sizeof(cipher_suite[0])/sizeof(uint16_t));
2718 
2719     ExampleSetPsk("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
2720     HITLS_CFG_SetPskClientCallback(testInfo.c_config, (HITLS_PskClientCb)ExampleClientCb);
2721     HITLS_CFG_SetPskServerCallback(testInfo.s_config, (HITLS_PskServerCb)ExampleServerCb);
2722 
2723     testInfo.client = FRAME_CreateLink(testInfo.c_config, testInfo.uioType);
2724     ASSERT_TRUE(testInfo.client != NULL);
2725     testInfo.server = FRAME_CreateLink(testInfo.s_config, testInfo.uioType);
2726     ASSERT_TRUE(testInfo.server != NULL);
2727     ASSERT_TRUE(HITLS_SetSession(testInfo.client->ssl, testInfo.clientSession) == HITLS_SUCCESS);
2728 
2729     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, TRY_RECV_SERVER_HELLO) , HITLS_SUCCESS);
2730 
2731     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(testInfo.client->io);
2732     uint8_t *recvBuf = ioUserData->recMsg.msg;
2733     uint32_t recvLen = ioUserData->recMsg.len;
2734     ASSERT_TRUE(recvLen != 0);
2735 
2736     uint32_t parseLen = 0;
2737     FRAME_Msg frameMsg = {0};
2738     FRAME_Type frameType = {0};
2739     frameType.versionType = HITLS_VERSION_TLS13;
2740     frameType.recordType = REC_TYPE_HANDSHAKE;
2741     frameType.handshakeType = SERVER_HELLO;
2742     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
2743     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
2744 
2745     FRAME_ServerHelloMsg *serverMsg = &frameMsg.body.hsMsg.body.serverHello;
2746     ASSERT_TRUE(serverMsg->pskSelectedIdentity.exLen.data != 0);
2747     ASSERT_TRUE(serverMsg->pskSelectedIdentity.data.data == 1);
2748 
2749     ASSERT_TRUE(testInfo.client->ssl != NULL);
2750     ASSERT_EQ(FRAME_CreateConnection(testInfo.client, testInfo.server, true, HS_STATE_BUTT) , HITLS_SUCCESS);
2751 EXIT:
2752     FRAME_CleanMsg(&frameType, &frameMsg);
2753     HITLS_SESS_Free(testInfo.clientSession);
2754     HITLS_CFG_FreeConfig(testInfo.c_config);
2755     HITLS_CFG_FreeConfig(testInfo.s_config);
2756     FRAME_FreeLink(testInfo.client);
2757     FRAME_FreeLink(testInfo.server);
2758 }
2759 /* END_CASE */
2760 
2761 /* During the TLS1.3 HRR handshaking, application messages can not be received*/
2762 /* BEGIN_CASE */
UT_TLS13_RFC8446_HRR_APP_RECV_TC001()2763 void UT_TLS13_RFC8446_HRR_APP_RECV_TC001()
2764 {
2765     FRAME_Init();
2766     HITLS_Config *tlsConfig = HITLS_CFG_NewTLSConfig();
2767     tlsConfig->isSupportClientVerify = true;
2768     HITLS_CFG_SetKeyExchMode(tlsConfig, TLS13_KE_MODE_PSK_WITH_DHE);
2769     ASSERT_TRUE(tlsConfig != NULL);
2770     FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2771     FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2772     ASSERT_TRUE(client != NULL);
2773     ASSERT_TRUE(server != NULL);
2774     HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
2775     HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
2776     const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
2777     uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
2778     HITLS_CFG_SetGroups(&(serverTlsCtx->config.tlsConfig), groups, groupsSize);
2779     /* 1. Initialize the client and server to tls1.3, construct the scenario where the supportedversion values carried
2780         by serverhello and hrr are different, */
2781     ASSERT_TRUE(FRAME_CreateConnection(client, server, false, TRY_RECV_CLIENT_HELLO) == HITLS_SUCCESS);
2782     ASSERT_TRUE(clientTlsCtx->state == CM_STATE_HANDSHAKING);
2783     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
2784     CONN_Deinit(serverTlsCtx);
2785     ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
2786     ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
2787 
2788     ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_SEND_CHANGE_CIPHER_SPEC);
2789     ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
2790     ASSERT_TRUE(serverTlsCtx->hsCtx->state == TRY_RECV_CLIENT_HELLO);
2791     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
2792 
2793     ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_IO_BUSY);
2794     ASSERT_TRUE(clientTlsCtx->hsCtx->state == TRY_SEND_CLIENT_HELLO);
2795     ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
2796     ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
2797 
2798     uint32_t sendLenapp = 7;
2799     uint8_t sendBufapp[7] = {0x17, 0x03, 0x03, 0x00, 0x02, 0x05, 0x05};
2800     uint32_t writeLen;
2801     BSL_UIO_Write(clientTlsCtx->uio, sendBufapp, sendLenapp, &writeLen);
2802     ASSERT_EQ(FRAME_TrasferMsgBetweenLink(client, server), HITLS_SUCCESS);
2803 
2804     ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
2805     ASSERT_EQ(HITLS_Accept(serverTlsCtx), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
2806 
2807     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
2808     ALERT_Info info = { 0 };
2809     ALERT_GetInfo(server->ssl, &info);
2810     ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
2811     ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
2812     ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
2813 EXIT:
2814     HITLS_CFG_FreeConfig(tlsConfig);
2815     FRAME_FreeLink(client);
2816     FRAME_FreeLink(server);
2817 }
2818 /* END_CASE */
2819 
2820 /** @
2821 * @test     UT_TLS1_3_RFC8446_Legacy_Version_TC001
2822 * @spec     For TLS 1.3, the legacy_record_version set to 0x0403 to client will get alert
2823 * @title    For TLS 1.3, the legacy_record_version set to 0x0403 to client will get alert
2824 * @precon   nan
2825 * @brief    5.1.  Record Layer line 190
2826 *           legacy_record_version: MUST be set to 0x0303 for all records generated by a TLS 1.3
2827             implementation other than an initial ClientHello (i.e., one not generated after a HelloRetryRequest),
2828             where it MAY also be 0x0301 for compatibility purposes. This field is deprecated and MUST be ignored
2829             for all purposes. Previous versions of TLS would use other values in this field under some circumstances.
2830 @ */
2831 /* BEGIN_CASE */
UT_TLS1_3_RFC8446_Legacy_Version_TC001(int statehs)2832 void UT_TLS1_3_RFC8446_Legacy_Version_TC001(int statehs)
2833 {
2834     FRAME_Init();
2835     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
2836     ASSERT_TRUE(tlsConfig != NULL);
2837 
2838     tlsConfig->isSupportExtendMasterSecret = true;
2839     tlsConfig->isSupportClientVerify = true;
2840     tlsConfig->isSupportNoClientCert = true;
2841 
2842     FRAME_LinkObj *client = NULL;
2843     FRAME_LinkObj *server = NULL;
2844     client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2845     /* Configure the server to support only the non-default curve. The server sends the HRR message. */
2846     const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
2847     uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
2848     HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
2849     server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2850     ASSERT_TRUE(client != NULL);
2851     ASSERT_TRUE(server != NULL);
2852     HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
2853     HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
2854     ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
2855     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
2856 
2857     ASSERT_TRUE(FRAME_CreateConnection(client, server, true, statehs) == HITLS_SUCCESS);
2858     ASSERT_TRUE(clientTlsCtx->hsCtx->state == (HITLS_HandshakeState)statehs);
2859     FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
2860     ioClientData->recMsg.msg[1] = 0x04u;
2861     ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_INVALID_PROTOCOL_VERSION);
2862     ALERT_Info info = {0};
2863     ALERT_GetInfo(client->ssl, &info);
2864     ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
2865     ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
2866     if (statehs == TRY_RECV_SERVER_HELLO) {
2867         ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
2868     } else {
2869         ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
2870     }
2871 EXIT:
2872     HITLS_CFG_FreeConfig(tlsConfig);
2873     FRAME_FreeLink(client);
2874     FRAME_FreeLink(server);
2875 }
2876 /* END_CASE */
2877 
2878 /** @
2879 * @test     UT_TLS1_3_RFC8446_Legacy_Version_TC002
2880 * @spec     For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
2881 * @title    For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
2882 * @precon   nan
2883 * @brief    5.1.  Record Layer line 190
2884 *           legacy_record_version: MUST be set to 0x0303 for all records generated by a TLS 1.3
2885             implementation other than an initial ClientHello (i.e., one not generated after a HelloRetryRequest),
2886             where it MAY also be 0x0301 for compatibility purposes. This field is deprecated and MUST be ignored
2887             for all purposes. Previous versions of TLS would use other values in this field under some circumstances.
2888 @ */
2889 /* BEGIN_CASE */
UT_TLS1_3_RFC8446_Legacy_Version_TC002(int statehs)2890 void UT_TLS1_3_RFC8446_Legacy_Version_TC002(int statehs)
2891 {
2892     FRAME_Init();
2893     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
2894     ASSERT_TRUE(tlsConfig != NULL);
2895 
2896     tlsConfig->isSupportExtendMasterSecret = true;
2897     tlsConfig->isSupportClientVerify = true;
2898     tlsConfig->isSupportNoClientCert = true;
2899 
2900     FRAME_LinkObj *client = NULL;
2901     FRAME_LinkObj *server = NULL;
2902     client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2903     /* Configure the server to support only the non-default curve. The server sends the HRR message. */
2904     const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
2905     uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
2906     HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
2907     server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2908     ASSERT_TRUE(client != NULL);
2909     ASSERT_TRUE(server != NULL);
2910     HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
2911     HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
2912     ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
2913     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
2914 
2915     ASSERT_TRUE(FRAME_CreateConnection(client, server, false, statehs) == HITLS_SUCCESS);
2916     ASSERT_TRUE(serverTlsCtx->hsCtx->state == (HITLS_HandshakeState)statehs);
2917     FrameUioUserData *ioClientData = BSL_UIO_GetUserData(server->io);
2918     ioClientData->recMsg.msg[1] = 0x04u;
2919     ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_INVALID_PROTOCOL_VERSION);
2920     ALERT_Info info = {0};
2921     ALERT_GetInfo(server->ssl, &info);
2922     ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
2923     ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
2924     if (statehs == TRY_RECV_CLIENT_HELLO) {
2925         ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
2926     } else {
2927         ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
2928     }
2929 EXIT:
2930     HITLS_CFG_FreeConfig(tlsConfig);
2931     FRAME_FreeLink(client);
2932     FRAME_FreeLink(server);
2933 }
2934 /* END_CASE */
2935 
2936 /* @
2937 * @test UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_PROCESS_TC001
2938 * @spec -
2939 * @title During connection establishment, tls13 server receives a warning alert, the connection state change to alerted
2940 * @precon nan
2941 * @brief 1. Initialize the client and server. Expected result 1.
2942 * 2. Initiate a connection, keep the connection status in the receive_client_key_exchange state,
2943      and simulate the scenario where the server receives a warning alert message. (Expected result 2)
2944 * @expect 1. Complete initialization.
2945 * 2. the connection state of server change to alerted
2946 * @prior Level 2
2947 * @auto TRUE
2948 @ */
2949 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_PROCESS_TC001()2950 void UT_TLS_TLS13_RFC8446_CONSISTENCY_ALERT_PROCESS_TC001()
2951 {
2952     FRAME_Init();
2953     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
2954 
2955     tlsConfig->isSupportExtendMasterSecret = true;
2956     tlsConfig->isSupportClientVerify = true;
2957     tlsConfig->isSupportNoClientCert = false;
2958 
2959     FRAME_LinkObj *client = NULL;
2960     FRAME_LinkObj *server = NULL;
2961 
2962     client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2963     server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
2964     ASSERT_TRUE(client != NULL);
2965     ASSERT_TRUE(server != NULL);
2966     HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
2967     HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
2968     ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
2969 
2970     ASSERT_EQ(FRAME_CreateConnection(client, server, false, TRY_RECV_CERTIFICATE), HITLS_SUCCESS);
2971     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_HANDSHAKING);
2972     ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
2973     uint8_t alertMsg[2] = {ALERT_LEVEL_WARNING, ALERT_NO_RENEGOTIATION};
2974     ASSERT_EQ(REC_Write(clientTlsCtx, REC_TYPE_ALERT, alertMsg, sizeof(alertMsg)), HITLS_SUCCESS);
2975     // clear the certificate verify in the cache
2976     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
2977     ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
2978 
2979     ASSERT_EQ(REC_Write(clientTlsCtx, REC_TYPE_ALERT, alertMsg, sizeof(alertMsg)), HITLS_SUCCESS);
2980     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(client, server) == HITLS_SUCCESS);
2981     ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
2982     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_ALERTED);
2983 
2984 EXIT:
2985     HITLS_CFG_FreeConfig(tlsConfig);
2986     FRAME_FreeLink(client);
2987     FRAME_FreeLink(server);
2988 }
2989 /* END_CASE */
2990 
2991 /** @
2992 * @test  UT_TLS_TLS13_RFC8446_HELLO_REQUEST_TC001
2993 * @spec  -
2994 * @title  Send a hello request when the link status is CM_STATE_IDLE.
2995 * @precon  nan
2996 * @brief  1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
2997 *         2. Construct a HelloRequest message and send it to the client. The client invokes the HITLS_Connect interface
2998     to receive the message. Expected result 2 is obtained.
2999 * @expect 1. The initialization is successful.
3000 *         2. After receiving the HelloRequest message, the client ignores the message and stays in the
3001     TRY_RECV_SERVER_HELLO state after sending the ClientHello message.
3002 * @prior  Level 1
3003 * @auto  TRUE
3004 @ */
3005 
3006 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_HELLO_REQUEST_TC001(void)3007 void UT_TLS_TLS13_RFC8446_HELLO_REQUEST_TC001(void)
3008 {
3009     FRAME_Init();
3010     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
3011     ASSERT_TRUE(tlsConfig != NULL);
3012     FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3013     ASSERT_TRUE(client != NULL);
3014     FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3015     ASSERT_TRUE(server != NULL);
3016     ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
3017 
3018     // Construct a HelloRequest message and send it to the client.
3019     uint8_t buf[HS_MSG_HEADER_SIZE] = {0u};
3020     size_t len = HS_MSG_HEADER_SIZE;
3021     REC_Write(server->ssl, REC_TYPE_HANDSHAKE, buf, len);
3022     ASSERT_TRUE(FRAME_TrasferMsgBetweenLink(server, client) == HITLS_SUCCESS);
3023 
3024     ASSERT_TRUE(client->ssl != NULL);
3025     ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
3026     ASSERT_TRUE(client->ssl->hsCtx->state == TRY_RECV_SERVER_HELLO);
3027 EXIT:
3028     HITLS_CFG_FreeConfig(tlsConfig);
3029     FRAME_FreeLink(client);
3030     FRAME_FreeLink(server);
3031 }
3032 /* END_CASE */
3033 
SetFrameType(FRAME_Type * frametype,uint16_t versionType,REC_Type recordType,HS_MsgType handshakeType,HITLS_KeyExchAlgo keyExType)3034 void SetFrameType(FRAME_Type *frametype, uint16_t versionType, REC_Type recordType, HS_MsgType handshakeType,
3035     HITLS_KeyExchAlgo keyExType)
3036 {
3037     frametype->versionType = versionType;
3038     frametype->recordType = recordType;
3039     frametype->handshakeType = handshakeType;
3040     frametype->keyExType = keyExType;
3041     frametype->transportType = BSL_UIO_TCP;
3042 }
3043 
3044 /** @
3045 * @test  UT_TLS_TLS13_RFC8446_MODIFIED_SESSID_FROM_SH_TC002
3046 * @spec  -
3047 * @title  Send a empty session id to client.
3048 * @precon  nan
3049 * @brief  1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
3050 *         2. Construct a server hello message with empty session id and send it to the client.
3051              Expected result 2 is obtained.
3052 * @expect 1. The initialization is successful.
3053 *         2. After receiving the HelloRequest message, the client send a ILLEGAL_PARAMETER alert.
3054 * @prior  Level 1
3055 * @auto  TRUE
3056 @ */
3057 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_MODIFIED_SESSID_FROM_SH_TC002()3058 void UT_TLS_TLS13_RFC8446_MODIFIED_SESSID_FROM_SH_TC002()
3059 {
3060     FRAME_Init();
3061     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
3062     ASSERT_TRUE(tlsConfig != NULL);
3063     FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3064     FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3065     ASSERT_TRUE(client != NULL);
3066     ASSERT_TRUE(server != NULL);
3067 
3068     ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
3069 
3070     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
3071     uint8_t *recvBuf = ioUserData->recMsg.msg;
3072     uint32_t recvLen = ioUserData->recMsg.len;
3073     ASSERT_TRUE(recvLen != 0);
3074     FRAME_Msg parsedSH = {0};
3075     uint32_t parseLen = 0;
3076     FRAME_Type frameType = {0};
3077     SetFrameType(&frameType, HITLS_VERSION_TLS13, REC_TYPE_HANDSHAKE, SERVER_HELLO, HITLS_KEY_EXCH_ECDHE);
3078     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recvBuf, recvLen, &parsedSH, &parseLen) == HITLS_SUCCESS);
3079 
3080     FRAME_ServerHelloMsg *shMsg = &parsedSH.body.hsMsg.body.serverHello;
3081     shMsg->sessionId.size = 0;
3082     shMsg->sessionId.state = MISSING_FIELD;
3083     shMsg->sessionIdSize.data = 0;
3084     uint32_t sendLen = MAX_RECORD_LENTH;
3085     uint8_t sendBuf[MAX_RECORD_LENTH] = {0};
3086     ASSERT_TRUE(FRAME_PackMsg(&frameType, &parsedSH, sendBuf, sendLen, &sendLen) == HITLS_SUCCESS);
3087     ioUserData->recMsg.len = 0;
3088     ASSERT_TRUE(FRAME_TransportRecMsg(client->io, sendBuf, sendLen) == HITLS_SUCCESS);
3089     ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_MSG_HANDLE_ILLEGAL_SESSION_ID);
3090     ALERT_Info alert = { 0 };
3091     ALERT_GetInfo(client->ssl, &alert);
3092     ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
3093     ASSERT_EQ(alert.description, ALERT_ILLEGAL_PARAMETER);
3094 EXIT:
3095     FRAME_CleanMsg(&frameType, &parsedSH);
3096     HITLS_CFG_FreeConfig(tlsConfig);
3097     FRAME_FreeLink(client);
3098     FRAME_FreeLink(server);
3099 }
3100 /* END_CASE */
3101 
3102 /** @
3103 * @test  UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_MUTI_CCS_TC001
3104 * @spec  -
3105 * @title  IN TLS1.3, mutiple ccs can be received.
3106 * @precon  nan
3107 * @brief  1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
3108 *         2. Construct a ChangeCipherSpec message and send it to the client five times. Expected result 2 is obtained.
3109 * @expect 1. The initialization is successful.
3110 *         2. return HITLS_REC_NORMAL_RECV_BUF_EMPTY.
3111 * @prior  Level 1
3112 * @auto  TRUE
3113 @ */
3114 /* IN TLS1.3, mutiple ccs can be received*/
3115 /* BEGIN_CASE */
UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_MUTI_CCS_TC001()3116 void UT_TLS_TLS13_RFC8446_CONSISTENCY_RECV_MUTI_CCS_TC001()
3117 {
3118     FRAME_Init();
3119     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
3120     ASSERT_TRUE(tlsConfig != NULL);
3121     FRAME_LinkObj *client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3122     FRAME_LinkObj *server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3123     ASSERT_TRUE(client != NULL);
3124     ASSERT_TRUE(server != NULL);
3125     HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
3126     HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
3127     ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_CERTIFICATE_VERIFY) == HITLS_SUCCESS);
3128 
3129     ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
3130     uint32_t sendLenccs = 6;
3131     uint8_t sendBufccs[6] = {0x14, 0x03, 0x03, 0x00, 0x01, 0x01};
3132     uint32_t writeLen;
3133     for (int i = 0; i < 5; i++) {
3134         BSL_UIO_Write(serverTlsCtx->uio, sendBufccs, sendLenccs, &writeLen);
3135         ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
3136         ASSERT_EQ(HITLS_Connect(clientTlsCtx), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
3137     }
3138     ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
3139 EXIT:
3140     HITLS_CFG_FreeConfig(tlsConfig);
3141     FRAME_FreeLink(client);
3142     FRAME_FreeLink(server);
3143 }
3144 /* END_CASE */
3145 
SendCcs(HITLS_Ctx * ctx,uint8_t * data,uint8_t len)3146 static int32_t SendCcs(HITLS_Ctx *ctx, uint8_t *data, uint8_t len)
3147 {
3148     /** Write records. */
3149     int32_t ret = REC_Write(ctx, REC_TYPE_CHANGE_CIPHER_SPEC, data, len);
3150     if (ret != HITLS_SUCCESS) {
3151         return ret;
3152     }
3153     /* If isFlightTransmitEnable is enabled, the stored handshake information needs to be sent. */
3154     uint8_t isFlightTransmitEnable;
3155     (void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
3156     if (isFlightTransmitEnable == 1) {
3157         ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
3158         if (ret == BSL_UIO_IO_BUSY) {
3159             return HITLS_REC_NORMAL_IO_BUSY;
3160         }
3161         if (ret != BSL_SUCCESS) {
3162             return HITLS_REC_ERR_IO_EXCEPTION;
3163         }
3164     }
3165     return HITLS_SUCCESS;
3166 }
3167 
SendAlert(HITLS_Ctx * ctx,ALERT_Level level,ALERT_Description description)3168 static int32_t SendAlert(HITLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
3169 {
3170     uint8_t data[ALERT_BODY_LEN];
3171     /** Obtain the alert level. */
3172     data[0] = level;
3173     data[1] = description;
3174     /** Write records. */
3175     int32_t ret = REC_Write(ctx, REC_TYPE_ALERT, data, ALERT_BODY_LEN);
3176     if (ret != HITLS_SUCCESS) {
3177         return ret;
3178     }
3179     /* If isFlightTransmitEnable is enabled, the stored handshake information needs to be sent. */
3180     uint8_t isFlightTransmitEnable;
3181     (void)HITLS_GetFlightTransmitSwitch(ctx, &isFlightTransmitEnable);
3182     if (isFlightTransmitEnable == 1) {
3183         ret = BSL_UIO_Ctrl(ctx->uio, BSL_UIO_FLUSH, 0, NULL);
3184         if (ret == BSL_UIO_IO_BUSY) {
3185             return HITLS_REC_NORMAL_IO_BUSY;
3186         }
3187         if (ret != BSL_SUCCESS) {
3188             return HITLS_REC_ERR_IO_EXCEPTION;
3189         }
3190     }
3191     return HITLS_SUCCESS;
3192 }
3193 
3194 /** @
3195 * @test  UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_RECEIVES_ENCRYPTED_CCS_TC001
3196 * @spec  -
3197 * @title  The encrypted CCS is received when the plaintext CCS is received.
3198 * @precon  nan
3199 * @brief  1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
3200 *         2. Construct encrypted CCS and send it to the client. Expected result 2 is obtained.
3201 * @expect 1. The initialization is successful.
3202 *         2. After receiving the CCS message, the client send a UNEXPECTED_MESSAGE alert.
3203 * @prior  Level 1
3204 * @auto  TRUE
3205 @ */
3206 /* BEGIN_CASE */
UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_RECEIVES_ENCRYPTED_CCS_TC001(void)3207 void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_RECEIVES_ENCRYPTED_CCS_TC001(void)
3208 {
3209     FRAME_Init();
3210 
3211     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
3212     ASSERT_TRUE(tlsConfig != NULL);
3213 
3214     tlsConfig->isSupportExtendMasterSecret = true;
3215     tlsConfig->isSupportClientVerify = true;
3216     tlsConfig->isSupportNoClientCert = true;
3217     FRAME_LinkObj *client = NULL;
3218     FRAME_LinkObj *server = NULL;
3219     client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3220     server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3221     ASSERT_TRUE(client != NULL);
3222     ASSERT_TRUE(server != NULL);
3223     HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
3224     HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
3225     ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
3226     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
3227     ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO) == HITLS_SUCCESS);
3228     // Sends serverhello to the peer end.
3229     ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
3230     // Processing serverhello
3231     ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_BUF_EMPTY);
3232     serverTlsCtx->recCtx->outBuf->end = 0;
3233     uint32_t hashLen = SAL_CRYPT_DigestSize(serverTlsCtx->negotiatedInfo.cipherSuiteInfo.hashAlg);
3234     ASSERT_EQ(HS_SwitchTrafficKey(serverTlsCtx, serverTlsCtx->hsCtx->serverHsTrafficSecret, hashLen, true),
3235         HITLS_SUCCESS);
3236     uint8_t data = 1;
3237     // send crypto ccs
3238     ASSERT_EQ(SendCcs(server->ssl, &data, sizeof(data)), HITLS_SUCCESS);
3239     FrameUioUserData *ioServerData = BSL_UIO_GetUserData(server->io);
3240     ioServerData->sndMsg.msg[0] = REC_TYPE_APP;
3241     FrameUioUserData *ioClientData = BSL_UIO_GetUserData(client->io);
3242     ioClientData->recMsg.len = 0;
3243     ASSERT_EQ(FRAME_TrasferMsgBetweenLink(server, client), HITLS_SUCCESS);
3244     // process crypto ccs
3245     ASSERT_EQ(HITLS_Connect(client->ssl), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
3246     ALERT_Info info = {0};
3247     ALERT_GetInfo(client->ssl, &info);
3248     ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
3249     ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
3250     ASSERT_EQ(info.description, ALERT_UNEXPECTED_MESSAGE);
3251 EXIT:
3252     HITLS_CFG_FreeConfig(tlsConfig);
3253     FRAME_FreeLink(client);
3254     FRAME_FreeLink(server);
3255 }
3256 /* END_CASE */
3257 
3258 #define ALERT_UNKNOWN_DESCRIPTION 254
3259 
3260 /** @
3261 * @test  UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_UNKNOWN_DESCRIPTION_TC001
3262 * @spec  -
3263 * @title  RFC8446 6.2 All alerts defined below in this section, as well as all unknown alerts,
3264         are universally considered fatal as of TLS 1.3 (see Section 6).
3265 * @precon  nan
3266 * @brief  1. Use the configuration items to configure the client and server. Expected result 1 is obtained.
3267 *         2. Construct alert message with alert level warning and ALERT_UNKNOWN_DESCRIPTION, and send it to the server.
3268             Expected result 2 is obtained.
3269 * @expect 1. The initialization is successful.
3270 *         2. After receiving the alert message, the server send a FATAL alert.
3271 * @prior  Level 1
3272 * @auto  TRUE
3273 @ */
3274 /* BEGIN_CASE */
UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_UNKNOWN_DESCRIPTION_TC001(void)3275 void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_UNKNOWN_DESCRIPTION_TC001(void)
3276 {
3277     FRAME_Init();
3278     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
3279     ASSERT_TRUE(tlsConfig != NULL);
3280 
3281     tlsConfig->isSupportClientVerify = true;
3282     FRAME_LinkObj *client = NULL;
3283     FRAME_LinkObj *server = NULL;
3284 
3285     client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3286     server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3287     ASSERT_TRUE(client != NULL);
3288     ASSERT_TRUE(server != NULL);
3289 
3290     ASSERT_TRUE(FRAME_CreateConnection(client, server, true, TRY_SEND_FINISH) == HITLS_SUCCESS);
3291 
3292     ASSERT_TRUE(SendAlert(client->ssl, ALERT_LEVEL_WARNING, ALERT_UNKNOWN_DESCRIPTION) == HITLS_SUCCESS);
3293     ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_REC_NORMAL_RECV_UNEXPECT_MSG);
3294 
3295     HITLS_Ctx *Ctx = FRAME_GetTlsCtx(server);
3296     ALERT_Info alert = { 0 };
3297     ALERT_GetInfo(Ctx, &alert);
3298     ASSERT_EQ(alert.level, ALERT_LEVEL_FATAL);
3299     ASSERT_EQ(alert.description, ALERT_UNKNOWN_DESCRIPTION);
3300 EXIT:
3301     HITLS_CFG_FreeConfig(tlsConfig);
3302     FRAME_FreeLink(client);
3303     FRAME_FreeLink(server);
3304 }
3305 /* END_CASE */
3306 
3307 /* @
3308 * @test  SDV_TLS13_RFC8446_REQUEST_CLIENT_HELLO_TC008
3309 * @brief 2.1.  Incorrect DHE Share
3310 * @spec  If the client has not provided a sufficient "key_share" extension (e.g., it includes only DHE or ECDHE groups
3311     unacceptable to or unsupported by the server), the server corrects the mismatch with a HelloRetryRequest and the
3312     client needs to restart the handshake with an appropriate "key_share" extension, as shown in Figure 2. If no common
3313     cryptographic parameters can be negotiated, the server MUST abort the handshake with an appropriate alert.
3314 * @title  Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
3315     server. Observe the link setup result and check whether the server sends Hello_Retry_Requset.
3316 * @precon  nan
3317 * @brief
3318 1. Configure groups_list:"brainpoolP512r1:X25519" on the client and groups_list:"brainpoolP512r1:X25519" on the
3319     server.
3320 * @expect
3321 1. Send clienthello with X25519 keyshare. The link is established successfully.
3322 2. The server does not send Hello_Retry_Requset.
3323 * @prior  Level 2
3324 * @auto  TRUE
3325 @ */
3326 /* BEGIN_CASE */
UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_REQUEST_CLIENT_HELLO_TC001()3327 void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_REQUEST_CLIENT_HELLO_TC001()
3328 {
3329     FRAME_Init();
3330     FRAME_LinkObj *client = NULL;
3331     FRAME_LinkObj *server = NULL;
3332     HITLS_Config *clientconfig = NULL;
3333     HITLS_Config *serverconfig = NULL;
3334     clientconfig = HITLS_CFG_NewTLS13Config();
3335     ASSERT_TRUE(clientconfig != NULL);
3336     serverconfig = HITLS_CFG_NewTLS13Config();
3337     ASSERT_TRUE(serverconfig != NULL);
3338 
3339     uint16_t clientgroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
3340     uint16_t servergroups[] = {HITLS_EC_GROUP_BRAINPOOLP512R1, HITLS_EC_GROUP_CURVE25519};
3341     ASSERT_EQ(HITLS_CFG_SetGroups(serverconfig, servergroups, sizeof(servergroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
3342     ASSERT_EQ(HITLS_CFG_SetGroups(clientconfig, clientgroups, sizeof(clientgroups)/sizeof(uint16_t)) , HITLS_SUCCESS);
3343 
3344     client = FRAME_CreateLink(clientconfig, BSL_UIO_TCP);
3345     ASSERT_TRUE(client != NULL);
3346     server = FRAME_CreateLink(serverconfig, BSL_UIO_TCP);
3347     ASSERT_TRUE(server != NULL);
3348 
3349     ASSERT_EQ(FRAME_CreateConnection(client, server, true, TRY_RECV_SERVER_HELLO), HITLS_SUCCESS);
3350     FrameUioUserData *ioUserData = BSL_UIO_GetUserData(client->io);
3351     uint8_t *recBuf = ioUserData->recMsg.msg;
3352     uint32_t recLen = ioUserData->recMsg.len;
3353     ASSERT_TRUE(recLen != 0);
3354 
3355     uint32_t parseLen = 0;
3356     FRAME_Msg frameMsg = {0};
3357     FRAME_Type frameType = {0};
3358     frameType.versionType = HITLS_VERSION_TLS13;
3359     frameType.recordType = REC_TYPE_HANDSHAKE;
3360     frameType.handshakeType = SERVER_HELLO;
3361     frameType.keyExType = HITLS_KEY_EXCH_ECDHE;
3362     ASSERT_TRUE(FRAME_ParseMsg(&frameType, recBuf, recLen, &frameMsg, &parseLen) == HITLS_SUCCESS);
3363 
3364     FRAME_ServerHelloMsg *serverHello = &frameMsg.body.hsMsg.body.serverHello;
3365     ASSERT_TRUE(serverHello->keyShare.data.group.data == HITLS_EC_GROUP_CURVE25519);
3366     ASSERT_EQ(server->ssl->hsCtx->haveHrr, false);
3367 
3368     // Continue to establish the link.
3369     ASSERT_EQ(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT), HITLS_SUCCESS);
3370 EXIT:
3371     FRAME_CleanMsg(&frameType, &frameMsg);
3372     HITLS_CFG_FreeConfig(clientconfig);
3373     HITLS_CFG_FreeConfig(serverconfig);
3374     FRAME_FreeLink(client);
3375     FRAME_FreeLink(server);
3376 }
3377 /* END_CASE */
3378 
3379 
3380 /** @
3381 * @test     UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_Legacy_Version_TC001
3382 * @spec     For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
3383 * @title    For TLS 1.3, the legacy_record_version set to 0x0403 to server will get alert
3384 * @precon   nan
3385 * @brief    5.1.  Record Layer line 190
3386 *           legacy_record_version: MUST be set to 0x0303 for all records generated by a TLS 1.3
3387             implementation other than an initial ClientHello (i.e., one not generated after a HelloRetryRequest),
3388             where it MAY also be 0x0301 for compatibility purposes. This field is deprecated and MUST be ignored
3389             for all purposes. Previous versions of TLS would use other values in this field under some circumstances.
3390 @ */
3391 /* BEGIN_CASE */
UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_Legacy_Version_TC001(int statehs)3392 void UT_TLS_SDV_TLS1_3_RFC8446_CONSISTENCY_Legacy_Version_TC001(int statehs)
3393 {
3394     FRAME_Init();
3395     HITLS_Config *tlsConfig = HITLS_CFG_NewTLS13Config();
3396     ASSERT_TRUE(tlsConfig != NULL);
3397 
3398     tlsConfig->isSupportExtendMasterSecret = true;
3399     tlsConfig->isSupportClientVerify = true;
3400     tlsConfig->isSupportNoClientCert = true;
3401 
3402     FRAME_LinkObj *client = NULL;
3403     FRAME_LinkObj *server = NULL;
3404     client = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3405     /* Configure the server to support only the non-default curve. The server sends the HRR message. */
3406     const uint16_t groups[] = {HITLS_EC_GROUP_SECP521R1};
3407     uint32_t groupsSize = sizeof(groups) / sizeof(uint16_t);
3408     HITLS_CFG_SetGroups(tlsConfig, groups, groupsSize);
3409     server = FRAME_CreateLink(tlsConfig, BSL_UIO_TCP);
3410     ASSERT_TRUE(client != NULL);
3411     ASSERT_TRUE(server != NULL);
3412     HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
3413     HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
3414     ASSERT_TRUE(clientTlsCtx->state == CM_STATE_IDLE);
3415     ASSERT_TRUE(serverTlsCtx->state == CM_STATE_IDLE);
3416 
3417     ASSERT_TRUE(FRAME_CreateConnection(client, server, false, statehs) == HITLS_SUCCESS);
3418     ASSERT_TRUE(serverTlsCtx->hsCtx->state == (HITLS_HandshakeState)statehs);
3419     FrameUioUserData *ioClientData = BSL_UIO_GetUserData(server->io);
3420     ioClientData->recMsg.msg[1] = 0x04u;
3421     ASSERT_EQ(HITLS_Accept(server->ssl), HITLS_REC_INVALID_PROTOCOL_VERSION);
3422     ALERT_Info info = {0};
3423     ALERT_GetInfo(server->ssl, &info);
3424     ASSERT_EQ(info.flag, ALERT_FLAG_SEND);
3425     ASSERT_EQ(info.level, ALERT_LEVEL_FATAL);
3426     if (statehs == TRY_RECV_CLIENT_HELLO) {
3427         ASSERT_EQ(info.description, ALERT_PROTOCOL_VERSION);
3428     } else {
3429         ASSERT_EQ(info.description, ALERT_DECODE_ERROR);
3430     }
3431 EXIT:
3432     HITLS_CFG_FreeConfig(tlsConfig);
3433     FRAME_FreeLink(client);
3434     FRAME_FreeLink(server);
3435 }
3436 /* END_CASE */