已合并
Fix some external interfaces to allow setting null callbacks. #797
balabala-123创建于 2025年11月15日
Fix some external interfaces to allow setting null callbacks. #797
已合并
balabala-123创建于 2025年11月15日
15 个文件变更+213-132
@@ -105,6 +105,25 @@ int32_t HITLS_SetAlpnProtos(HITLS_Ctx *ctx, const uint8_t *protos, uint32_t prot
105 */105 */
106int32_t HITLS_GetSelectedAlpnProto(HITLS_Ctx *ctx, uint8_t **proto, uint32_t *protoLen);106int32_t HITLS_GetSelectedAlpnProto(HITLS_Ctx *ctx, uint8_t **proto, uint32_t *protoLen);
107 107 
108+/**
109+ * @ingroup hitls_alpn
110+ * @brief Obtaining the ALPN Negotiation Result
111+ * The server selects an appropriate ALPN based on the ALPN provided by the client and its own configured ALPN.
112+ * @param out [OUT] Outgoing selected protocol.
113+ * @param outLen [OUT] Length of the outgoing selected protocol.
114+ * @param servAlpnList [IN] Header address of the server ALPN list.
115+ * @param servAlpnListLen [IN] Length of the server ALPN list.
116+ * @param clientAlpnList [IN] Header address of the client ALPN list.
117+ * @param clientAlpnListLen [IN] Length of the client ALPN list.
118+ *
119+ * @retval HITLS_SUCCESS, succeeded. Note: Success does not necessarily mean negotiating ALPN; it requires making a
120+ * judgment on out.
121+ * @retval HITLS_NULL_INPUT, the input is NULL.
122+ * @retval HITLS_CONFIG_INVALID_LENGTH, ALPN length does not match the actual length.
123+ */
124+int32_t HITLS_SelectAlpnProtocol(uint8_t **out, uint8_t *outLen, const uint8_t *servAlpnList, uint32_t servAlpnListLen,
125+ const uint8_t *clientAlpnList, uint32_t clientAlpnListLen);
126+ 
108#ifdef __cplusplus127#ifdef __cplusplus
109}128}
110#endif129#endif
@@ -474,7 +474,7 @@ int32_t HITLS_X509_SetSerial(BSL_ASN1_Buffer *serial, const void *val, uint32_t
474#if defined(HITLS_PKI_X509_CRT) || defined(HITLS_PKI_X509_CRL)474#if defined(HITLS_PKI_X509_CRT) || defined(HITLS_PKI_X509_CRL)
475int32_t HITLS_X509_GetSerial(BSL_ASN1_Buffer *serial, void *val, uint32_t valLen)475int32_t HITLS_X509_GetSerial(BSL_ASN1_Buffer *serial, void *val, uint32_t valLen)
476{476{
477- if (valLen != sizeof(BSL_Buffer)) {477+ if (val == NULL || valLen != sizeof(BSL_Buffer)) {
478 BSL_ERR_PUSH_ERROR(HITLS_X509_ERR_INVALID_PARAM);478 BSL_ERR_PUSH_ERROR(HITLS_X509_ERR_INVALID_PARAM);
479 return HITLS_X509_ERR_INVALID_PARAM;479 return HITLS_X509_ERR_INVALID_PARAM;
480 }480 }
@@ -49,37 +49,6 @@ int32_t ServernameCbErrOK(HITLS_Ctx *ctx, int *alert, void *arg)
49// for alpn49// for alpn
50#define MAX_PROTOCOL_LEN 6553650#define MAX_PROTOCOL_LEN 65536
51 51 
52-/* Protocol matching function at the application layer */
53-static int32_t ExampleAlpnSelectProtocol(uint8_t **out, uint8_t *outLen, uint8_t *clientAlpnList,
54- uint8_t clientAlpnListLen, uint8_t *servAlpnList, uint8_t servAlpnListLen)
55-{
56- int32_t ret = HITLS_ALPN_ERR_ALERT_FATAL;
57- if (out == NULL || outLen == NULL || clientAlpnList == NULL || servAlpnList == NULL) {
58- return HITLS_NULL_INPUT;
59- }
60- 
61- uint8_t i = 0;
62- uint8_t j = 0;
63- for (i = 0; i < servAlpnListLen;) {
64- for (j = 0; j < clientAlpnListLen;) {
65- if (servAlpnList[i] == clientAlpnList[j] &&
66- (memcmp(&servAlpnList[i + 1], &clientAlpnList[j + 1], servAlpnList[i]) == 0)) {
67- *out = &servAlpnList[i + 1];
68- *outLen = servAlpnList[i];
69- ret = HITLS_ALPN_ERR_OK;
70- goto EXIT;
71- }
72- j = j + clientAlpnList[j];
73- ++j;
74- }
75- i = i + servAlpnList[i];
76- ++i;
77- }
78- 
79-EXIT:
80- return ret;
81-}
82- 
83/* UserData structure transferred by the server to the alpnCb callback. */52/* UserData structure transferred by the server to the alpnCb callback. */
84typedef struct TlsAlpnExtCtx_ {53typedef struct TlsAlpnExtCtx_ {
85 uint8_t *serverAlpnList;54 uint8_t *serverAlpnList;
@@ -96,10 +65,12 @@ int32_t ExampleAlpnCbForLlt(HITLS_Ctx *ctx, uint8_t **selectedProto, uint8_t *se
96 uint8_t *selected = NULL;65 uint8_t *selected = NULL;
97 uint8_t selectedLen = 0u;66 uint8_t selectedLen = 0u;
98 67 
99- ret = ExampleAlpnSelectProtocol(&selected, &selectedLen, clientAlpnList, clientAlpnListSize,68+ ret = HITLS_SelectAlpnProtocol(&selected, &selectedLen, alpnData->serverAlpnList, alpnData->serverAlpnListLen,
100- alpnData->serverAlpnList, alpnData->serverAlpnListLen);69+ clientAlpnList, clientAlpnListSize);
101- if (ret != HITLS_ALPN_ERR_OK) {70+ if (ret != HITLS_SUCCESS) {
102 return ret;71 return ret;
72+ } else if (selected == NULL) {
73+ return HITLS_ALPN_ERR_ALERT_FATAL;
103 }74 }
104 75 
105 *selectedProto = selected;76 *selectedProto = selected;
@@ -51,7 +51,7 @@ static int32_t ConfigAlpn(HITLS_Config *tlsConfig, char *AlpnList, bool isCient)
51 uint32_t AlpnListLen = 0;51 uint32_t AlpnListLen = 0;
52 if (AlpnList != NULL){52 if (AlpnList != NULL){
53 pAlpnList = AlpnList;53 pAlpnList = AlpnList;
54- AlpnListLen = strlen(pAlpnList); 54+ AlpnListLen = strlen(pAlpnList);
55 } else {55 } else {
56 pAlpnList = defaultAlpnList;56 pAlpnList = defaultAlpnList;
57 AlpnListLen = strlen(pAlpnList);57 AlpnListLen = strlen(pAlpnList);
@@ -79,9 +79,9 @@ EXIT:
79 * @test UT_TLS_ALPN_PARSE_PROTO_FUNC_TC00179 * @test UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001
80 * @title ALPN function test80 * @title ALPN function test
81 * @precon nan81 * @precon nan
82- * @brief server set alpn and alpn callback,client set alpn. The server supports the protocol configured on 82+ * @brief server set alpn and alpn callback,client set alpn. The server supports the protocol configured on
83 the client .Expect result 183 the client .Expect result 1
84- * @expect 1. server returns the protocol supported by the client 84+ * @expect 1. server returns the protocol supported by the client
85*/85*/
86/* BEGIN_CASE */86/* BEGIN_CASE */
87void UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001(int version)87void UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001(int version)
@@ -124,10 +124,18 @@ void UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001(int version)
124 ASSERT_EQ(ret, HITLS_SUCCESS);124 ASSERT_EQ(ret, HITLS_SUCCESS);
125 HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);125 HITLS_Ctx *clientTlsCtx = FRAME_GetTlsCtx(client);
126 HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);126 HITLS_Ctx *serverTlsCtx = FRAME_GetTlsCtx(server);
127- ASSERT_TRUE(memcmp(clientTlsCtx->negotiatedInfo.alpnSelected, "http/1.1", 8) == 0);127+ 
128- ASSERT_TRUE(clientTlsCtx->negotiatedInfo.alpnSelectedSize == 8);128+ uint8_t *clientAlpnProtosname = NULL;
129- ASSERT_TRUE(memcmp(serverTlsCtx->negotiatedInfo.alpnSelected, "http/1.1", 8) == 0);129+ uint32_t clientAlpnProtosnameLen = 0;
130- ASSERT_TRUE(serverTlsCtx->negotiatedInfo.alpnSelectedSize == 8);130+ HITLS_GetSelectedAlpnProto(clientTlsCtx, &clientAlpnProtosname, &clientAlpnProtosnameLen);
131+ ASSERT_TRUE(memcmp(clientAlpnProtosname, "http/1.1", 8) == 0);
132+ ASSERT_TRUE(clientAlpnProtosnameLen == 8);
133+ 
134+ uint8_t *serverAlpnProtosname = NULL;
135+ uint32_t serverAlpnProtosnameLen = 0;
136+ HITLS_GetSelectedAlpnProto(serverTlsCtx, &serverAlpnProtosname, &serverAlpnProtosnameLen);
137+ ASSERT_TRUE(memcmp(serverAlpnProtosname, "http/1.1", 8) == 0);
138+ ASSERT_TRUE(serverAlpnProtosnameLen == 8);
131 139 
132EXIT:140EXIT:
133 HITLS_CFG_FreeConfig(s_config);141 HITLS_CFG_FreeConfig(s_config);
@@ -135,4 +143,68 @@ EXIT:
135 FRAME_FreeLink(client);143 FRAME_FreeLink(client);
136 FRAME_FreeLink(server);144 FRAME_FreeLink(server);
137}145}
138-/* END_CASE */146+/* END_CASE */
147+ 
148+/**
149+ * @test UT_TLS_ALPN_SELECT_FUNC_TC001
150+ * @title HITLS_SelectAlpnProtocol function test
151+ * @precon nan
152+ * @brief server set alpn, client set alpn. The server supports the protocol configured on the client .Expect result 1.
153+ * @expect 1. cmp out with "http/1.1", outLen is 8.
154+*/
155+/* BEGIN_CASE */
156+void UT_TLS_ALPN_SELECT_FUNC_TC001()
157+{
158+ const char *clientAlpnList = "\x06spdy/3\x08http/1.1";
159+ const char *serverAlpnList = "\x08http/1.1\x06spdy/3";
160+ uint8_t *out = NULL;
161+ uint8_t outLen = 0;
162+ 
163+ int32_t ret = HITLS_SelectAlpnProtocol(&out, &outLen, (const uint8_t *)serverAlpnList, strlen(serverAlpnList),
164+ (const uint8_t *)clientAlpnList, strlen(clientAlpnList));
165+ ASSERT_EQ(ret, HITLS_SUCCESS);
166+ ASSERT_EQ(outLen, 8);
167+ ASSERT_TRUE(memcmp(out, "http/1.1", 8) == 0);
168+ 
169+EXIT:
170+ return;
171+}
172+/* END_CASE */
173+ 
174+/**
175+ * @test UT_TLS_ALPN_SELECT_FUNC_TC002
176+ * @title HITLS_SelectAlpnProtocol function test
177+ * @precon nan
178+ * @brief server set alpn, client set alpn. The server supports the protocol configured on the client .Expect result 1.
179+ * @expect 1. cmp out with "http/1.1", outLen is 8.
180+*/
181+/* BEGIN_CASE */
182+void UT_TLS_ALPN_SELECT_FUNC_TC002(
183+ int serverAlpnLen, Hex *serverAlpnList, int clientAlpnLen, Hex *clientAlpnList, int expectedRet, Hex *expectedOut)
184+{
185+ uint8_t *out = NULL;
186+ uint8_t outLen = 0;
187+ uint8_t *tempClientAlpn = clientAlpnList->x;
188+ uint32_t tempClientAlpnLen = clientAlpnLen;
189+ uint8_t *tempServerAlpn = serverAlpnList->x;
190+ uint32_t tempServerAlpnLen = serverAlpnLen;
191+ 
192+ if (serverAlpnLen == -1) {
193+ tempServerAlpn = NULL;
194+ tempServerAlpnLen = 0;
195+ }
196+ if (clientAlpnLen == -1) {
197+ tempClientAlpn = NULL;
198+ tempClientAlpnLen = 0;
199+ }
200+ 
201+ int32_t ret = HITLS_SelectAlpnProtocol(&out, &outLen, (const uint8_t *)tempServerAlpn, tempServerAlpnLen,
202+ (const uint8_t *)tempClientAlpn, tempClientAlpnLen);
203+ ASSERT_EQ(ret, expectedRet);
204+ ASSERT_EQ(outLen, expectedOut->len);
205+ ASSERT_TRUE(memcmp(out, expectedOut->x, expectedOut->len) == 0);
206+ 
207+EXIT:
208+ return;
209+}
210+/* END_CASE */
@@ -3,3 +3,30 @@ UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001:HITLS_VERSION_TLS12
3 3 
4UT_TLS_ALPN_PARSE_PROTO_FUNC_TC0014UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001
5UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001:HITLS_VERSION_TLS135UT_TLS_ALPN_PARSE_PROTO_FUNC_TC001:HITLS_VERSION_TLS13
6+ 
7+UT_TLS_ALPN_SELECT_FUNC_TC001
8+UT_TLS_ALPN_SELECT_FUNC_TC001:
9+ 
10+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-10-\x04spdy client-len-10-\x04http result-spdy
11+UT_TLS_ALPN_SELECT_FUNC_TC002:10:"04737064790468747470":10:"04737064790468747470":HITLS_SUCCESS:"73706479"
12+ 
13+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-5-\x04spdy client-len-5-\x04http result-null
14+UT_TLS_ALPN_SELECT_FUNC_TC002:5:"0473706479":5:"0468747470":HITLS_SUCCESS:""
15+ 
16+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-0-null client-len-5-\x04http result-null
17+UT_TLS_ALPN_SELECT_FUNC_TC002:0:"":5:"0468747470":HITLS_NULL_INPUT:""
18+ 
19+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-(-1)-null client-len-5-\x04http result-null
20+UT_TLS_ALPN_SELECT_FUNC_TC002:-1:"":5:"0468747470":HITLS_NULL_INPUT:""
21+ 
22+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-5-\x04spdy client-len-0-null result-null
23+UT_TLS_ALPN_SELECT_FUNC_TC002:5:"0468747470":0:"":HITLS_NULL_INPUT:""
24+ 
25+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-5-\x04spdy client-len-(-1)-null result-null
26+UT_TLS_ALPN_SELECT_FUNC_TC002:5:"0468747470":-1:"":HITLS_NULL_INPUT:""
27+ 
28+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-3-\x04spdy client-len-5-\x04http result-null
29+UT_TLS_ALPN_SELECT_FUNC_TC002:3:"0468747470":5:"0468747470":HITLS_CONFIG_INVALID_LENGTH:""
30+ 
31+UT_TLS_ALPN_SELECT_FUNC_TC002 server-len-5-\x05spdy client-len-5-\x05http result-null
32+UT_TLS_ALPN_SELECT_FUNC_TC002:5:"0568747470":5:"0568747470":HITLS_CONFIG_INVALID_LENGTH:""
@@ -1747,7 +1747,7 @@ void UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC001(int version)
1747 1747 
1748 ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);1748 ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
1749 1749 
1750- ret = HITLS_GetSharedGroup(server->ssl, 1, &groupId);1750+ ret = HITLS_GetSharedGroup(server->ssl, 0, &groupId);
1751 ASSERT_TRUE(ret == HITLS_SUCCESS);1751 ASSERT_TRUE(ret == HITLS_SUCCESS);
1752 ASSERT_TRUE(groupId == HITLS_EC_GROUP_SECP256R1);1752 ASSERT_TRUE(groupId == HITLS_EC_GROUP_SECP256R1);
1753 1753 
@@ -1871,7 +1871,7 @@ void UT_TLS_CM_HITLS_GetSharedGroup_FUNC_TC003(int version)
1871 1871 
1872 ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);1872 ASSERT_TRUE(FRAME_CreateConnection(client, server, true, HS_STATE_BUTT) == HITLS_SUCCESS);
1873 1873 
1874- ret = HITLS_GetSharedGroup(server->ssl, 2, &groupId);1874+ ret = HITLS_GetSharedGroup(server->ssl, 1, &groupId);
1875 ASSERT_TRUE(ret == HITLS_SUCCESS);1875 ASSERT_TRUE(ret == HITLS_SUCCESS);
1876 ASSERT_TRUE(groupId == HITLS_EC_GROUP_SECP384R1);1876 ASSERT_TRUE(groupId == HITLS_EC_GROUP_SECP384R1);
1877 1877 
@@ -452,7 +452,7 @@ void UT_TLS_CFG_SET_COOKIEGENERATECB_API_TC001(void)
452 452 
453 config = HITLS_CFG_NewDTLS12Config();453 config = HITLS_CFG_NewDTLS12Config();
454 454 
455- ASSERT_TRUE(HITLS_CFG_SetCookieGenCb(config, NULL) == HITLS_NULL_INPUT);455+ ASSERT_TRUE(HITLS_CFG_SetCookieGenCb(config, NULL) == HITLS_SUCCESS);
456 456 
457 ASSERT_TRUE(HITLS_CFG_SetCookieGenCb(config, UT_CookieGenerateCb) == HITLS_SUCCESS);457 ASSERT_TRUE(HITLS_CFG_SetCookieGenCb(config, UT_CookieGenerateCb) == HITLS_SUCCESS);
458 458 
@@ -483,7 +483,7 @@ void UT_TLS_CFG_SET_COOKIEVERIFYCB_API_TC001(void)
483 483 
484 config = HITLS_CFG_NewDTLS12Config();484 config = HITLS_CFG_NewDTLS12Config();
485 485 
486- ASSERT_TRUE(HITLS_CFG_SetCookieVerifyCb(config, NULL) == HITLS_NULL_INPUT);486+ ASSERT_TRUE(HITLS_CFG_SetCookieVerifyCb(config, NULL) == HITLS_SUCCESS);
487 487 
488 ASSERT_TRUE(HITLS_CFG_SetCookieVerifyCb(config, UT_CookieVerifyCb) == HITLS_SUCCESS);488 ASSERT_TRUE(HITLS_CFG_SetCookieVerifyCb(config, UT_CookieVerifyCb) == HITLS_SUCCESS);
489 489 
@@ -418,11 +418,11 @@ static uint16_t FindPreference(const HITLS_Ctx *ctx, int32_t nmatch, bool *haveF
418 for (uint32_t i = 0; i < preferGroupSize; i++) {418 for (uint32_t i = 0; i < preferGroupSize; i++) {
419 for (uint32_t j = 0; j < secondPreferGroupSize; j++) {419 for (uint32_t j = 0; j < secondPreferGroupSize; j++) {
420 if (preferGroups[i] == secondPreferGroups[j]) {420 if (preferGroups[i] == secondPreferGroups[j]) {
421- intersectionCnt++;
422 // Currently, the preferred nmatch is already matched421 // Currently, the preferred nmatch is already matched
423 bool isMatch = (intersectionCnt == nmatch);422 bool isMatch = (intersectionCnt == nmatch);
424 *haveFound = (isMatch ? true : (*haveFound));423 *haveFound = (isMatch ? true : (*haveFound));
425 ans = (isMatch ? preferGroups[i] : ans);424 ans = (isMatch ? preferGroups[i] : ans);
425+ intersectionCnt++;
426 // Jump out of the inner village and change426 // Jump out of the inner village and change
427 break;427 break;
428 }428 }
@@ -454,7 +454,7 @@ int32_t HITLS_GetSharedGroup(const HITLS_Ctx *ctx, int32_t nmatch, uint16_t *gro
454 *groupId = 0;454 *groupId = 0;
455 // Check the value range of nmatch and whether the interface is invoked by the server. The client cannot invoke the455 // Check the value range of nmatch and whether the interface is invoked by the server. The client cannot invoke the
456 // interface because the client cannot sense the peerInfo.456 // interface because the client cannot sense the peerInfo.
457- if (nmatch < GET_GROUPS_CNT || nmatch == 0 || ctx->isClient) {457+ if (nmatch < GET_GROUPS_CNT || ctx->isClient) {
458 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16464, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "invalid input", 0, 0, 0, 0);458 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16464, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "invalid input", 0, 0, 0, 0);
459 return HITLS_INVALID_INPUT;459 return HITLS_INVALID_INPUT;
460 }460 }
@@ -371,10 +371,6 @@ int32_t HITLS_GetSelectedAlpnProto(HITLS_Ctx *ctx, uint8_t **proto, uint32_t *pr
371 return HITLS_NULL_INPUT;371 return HITLS_NULL_INPUT;
372 }372 }
373 373 
374- if (ctx->negotiatedInfo.alpnSelected == NULL) {
375- return HITLS_NULL_INPUT;
376- }
377- 
378 *proto = ctx->negotiatedInfo.alpnSelected;374 *proto = ctx->negotiatedInfo.alpnSelected;
379 *protoLen = ctx->negotiatedInfo.alpnSelectedSize;375 *protoLen = ctx->negotiatedInfo.alpnSelectedSize;
380 376 
@@ -2110,25 +2110,6 @@ int32_t HITLS_CFG_GetDescription(const HITLS_Cipher *cipher, uint8_t *buf, int32
2110 return GetCipherSuiteDescription(cipher, buf, len);2110 return GetCipherSuiteDescription(cipher, buf, len);
2111}2111}
2112 2112 
2113-/**
2114- * @brief Determine whether to use the AEAD algorithm based on the cipher suite information.
2115- *
2116- * @param cipher [IN] Cipher suite information
2117- * @param isAead [OUT] Indicates whether to use the AEAD algorithm.
2118- * @return HITLS_SUCCESS Obtained successfully.
2119- * HITLS_NULL_INPUT The input parameter pointer is NULL.
2120- */
2121-int32_t HITLS_CIPHER_IsAead(const HITLS_Cipher *cipher, bool *isAead)
2122-{
2123- if (cipher == NULL || isAead == NULL) {
2124- BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
2125- return HITLS_NULL_INPUT;
2126- }
2127- 
2128- *isAead = (cipher->cipherType == HITLS_AEAD_CIPHER);
2129- return HITLS_SUCCESS;
2130-}
2131- 
2132const HITLS_Cipher *HITLS_CFG_GetCipherByID(uint16_t cipherSuite)2113const HITLS_Cipher *HITLS_CFG_GetCipherByID(uint16_t cipherSuite)
2133{2114{
2134 int32_t index = FindCipherSuiteIndexByCipherSuite(cipherSuite);2115 int32_t index = FindCipherSuiteIndexByCipherSuite(cipherSuite);
@@ -2150,4 +2131,25 @@ int32_t HITLS_CFG_GetCipherSuite(const HITLS_Cipher *cipher, uint16_t *cipherSui
2150 2131 
2151 return HITLS_SUCCESS;2132 return HITLS_SUCCESS;
2152}2133}
2153-#endif /* HITLS_TLS_CONFIG_CIPHER_SUITE */2134+#endif /* HITLS_TLS_CONFIG_CIPHER_SUITE */
2135+ 
2136+#ifdef HITLS_TLS_CONNECTION_INFO_NEGOTIATION
2137+/**
2138+ * @brief Determine whether to use the AEAD algorithm based on the cipher suite information.
2139+ *
2140+ * @param cipher [IN] Cipher suite information
2141+ * @param isAead [OUT] Indicates whether to use the AEAD algorithm.
2142+ * @return HITLS_SUCCESS Obtained successfully.
2143+ * HITLS_NULL_INPUT The input parameter pointer is NULL.
2144+ */
2145+int32_t HITLS_CIPHER_IsAead(const HITLS_Cipher *cipher, bool *isAead)
2146+{
2147+ if (cipher == NULL || isAead == NULL) {
2148+ BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
2149+ return HITLS_NULL_INPUT;
2150+ }
2151+ 
2152+ *isAead = (cipher->cipherType == HITLS_AEAD_CIPHER);
2153+ return HITLS_SUCCESS;
2154+}
2155+#endif /* HITLS_TLS_CONNECTION_INFO_NEGOTIATION */
@@ -1087,7 +1087,7 @@ int32_t HITLS_CFG_SetGroups(HITLS_Config *config, const uint16_t *groups, uint32
1087#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)1087#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
1088int32_t HITLS_CFG_SetCookieGenCb(HITLS_Config *config, HITLS_AppGenCookieCb callback)1088int32_t HITLS_CFG_SetCookieGenCb(HITLS_Config *config, HITLS_AppGenCookieCb callback)
1089{1089{
1090- if (config == NULL || callback == NULL) {1090+ if (config == NULL) {
1091 return HITLS_NULL_INPUT;1091 return HITLS_NULL_INPUT;
1092 }1092 }
1093 1093 
@@ -1097,7 +1097,7 @@ int32_t HITLS_CFG_SetCookieGenCb(HITLS_Config *config, HITLS_AppGenCookieCb call
1097 1097 
1098int32_t HITLS_CFG_SetCookieVerifyCb(HITLS_Config *config, HITLS_AppVerifyCookieCb callback)1098int32_t HITLS_CFG_SetCookieVerifyCb(HITLS_Config *config, HITLS_AppVerifyCookieCb callback)
1099{1099{
1100- if (config == NULL || callback == NULL) {1100+ if (config == NULL) {
1101 return HITLS_NULL_INPUT;1101 return HITLS_NULL_INPUT;
1102 }1102 }
1103 1103 
@@ -1107,7 +1107,7 @@ int32_t HITLS_CFG_SetCookieVerifyCb(HITLS_Config *config, HITLS_AppVerifyCookieC
1107 1107 
1108int32_t HITLS_CFG_SetDtlsTimerCb(HITLS_Config *config, HITLS_DtlsTimerCb callback)1108int32_t HITLS_CFG_SetDtlsTimerCb(HITLS_Config *config, HITLS_DtlsTimerCb callback)
1109{1109{
1110- if (config == NULL || callback == NULL) {1110+ if (config == NULL) {
1111 return HITLS_NULL_INPUT;1111 return HITLS_NULL_INPUT;
1112 }1112 }
1113 1113 
@@ -1528,7 +1528,7 @@ int32_t HITLS_CFG_SetPskIdentityHint(HITLS_Config *config, const uint8_t *hint,
1528// Configure clientCb, which is used to obtain the PSK through identity hints1528// Configure clientCb, which is used to obtain the PSK through identity hints
1529int32_t HITLS_CFG_SetPskClientCallback(HITLS_Config *config, HITLS_PskClientCb callback)1529int32_t HITLS_CFG_SetPskClientCallback(HITLS_Config *config, HITLS_PskClientCb callback)
1530{1530{
1531- if (config == NULL || callback == NULL) {1531+ if (config == NULL) {
1532 return HITLS_NULL_INPUT;1532 return HITLS_NULL_INPUT;
1533 }1533 }
1534 1534 
@@ -1539,7 +1539,7 @@ int32_t HITLS_CFG_SetPskClientCallback(HITLS_Config *config, HITLS_PskClientCb c
1539// Set serverCb to obtain the PSK through identity.1539// Set serverCb to obtain the PSK through identity.
1540int32_t HITLS_CFG_SetPskServerCallback(HITLS_Config *config, HITLS_PskServerCb callback)1540int32_t HITLS_CFG_SetPskServerCallback(HITLS_Config *config, HITLS_PskServerCb callback)
1541{1541{
1542- if (config == NULL || callback == NULL) {1542+ if (config == NULL) {
1543 return HITLS_NULL_INPUT;1543 return HITLS_NULL_INPUT;
1544 }1544 }
1545 1545 
@@ -1734,7 +1734,42 @@ int32_t HITLS_CFG_SetAlpnProtosSelectCb(HITLS_Config *config, HITLS_AlpnSelectCb
1734 1734 
1735 return HITLS_SUCCESS;1735 return HITLS_SUCCESS;
1736}1736}
1737-#endif1737+ 
1738+int32_t HITLS_SelectAlpnProtocol(uint8_t **out, uint8_t *outLen, const uint8_t *servAlpnList, uint32_t servAlpnListLen,
1739+ const uint8_t *clientAlpnList, uint32_t clientAlpnListLen)
1740+{
1741+ bool nullInput = out == NULL || outLen == NULL || clientAlpnList == NULL || servAlpnList == NULL ||
1742+ servAlpnListLen == 0 || clientAlpnListLen == 0;
1743+ if (nullInput == true) {
1744+ BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16690, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "intput null", 0, 0, 0, 0);
1745+ BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
1746+ return HITLS_NULL_INPUT;
1747+ }
1748+ 
1749+ /* Add the check on alpnList. The expected format is |protoLen1|proto1|protoLen2|proto2|...| */
1750+ if (AlpnListValidationCheck(servAlpnList, servAlpnListLen) != HITLS_SUCCESS ||
1751+ AlpnListValidationCheck(clientAlpnList, clientAlpnListLen) != HITLS_SUCCESS) {
1752+ return HITLS_CONFIG_INVALID_LENGTH;
1753+ }
1754+ 
1755+ for (uint32_t i = 0; i < servAlpnListLen;) {
1756+ for (uint32_t j = 0; j < clientAlpnListLen;) {
1757+ if (servAlpnList[i] == clientAlpnList[j] &&
1758+ (memcmp(&servAlpnList[i + 1], &clientAlpnList[j + 1], servAlpnList[i]) == 0)) {
1759+ *out = (uint8_t *)(uintptr_t)&servAlpnList[i + 1];
1760+ *outLen = servAlpnList[i];
1761+ return HITLS_SUCCESS;
1762+ }
1763+ j = j + clientAlpnList[j];
1764+ ++j;
1765+ }
1766+ i = i + servAlpnList[i];
1767+ ++i;
1768+ }
1769+ 
1770+ return HITLS_SUCCESS;
1771+}
1772+#endif /* HITLS_TLS_FEATURE_ALPN */
1738#if defined(HITLS_TLS_FEATURE_SESSION_ID)1773#if defined(HITLS_TLS_FEATURE_SESSION_ID)
1739int32_t HITLS_CFG_SetSessionIdCtx(HITLS_Config *config, const uint8_t *sessionIdCtx, uint32_t len)1774int32_t HITLS_CFG_SetSessionIdCtx(HITLS_Config *config, const uint8_t *sessionIdCtx, uint32_t len)
1740{1775{
@@ -360,7 +360,7 @@ static int32_t GetCipherInitCtx(HITLS_Lib_Ctx *libCtx, const char *attrName,
360 if (*ctx != NULL) {360 if (*ctx != NULL) {
361 return CRYPT_EAL_CipherReinit(*ctx, (uint8_t *)(uintptr_t)cipher->iv, cipher->ivLen);361 return CRYPT_EAL_CipherReinit(*ctx, (uint8_t *)(uintptr_t)cipher->iv, cipher->ivLen);
362 }362 }
363- 363+ 
364 *ctx = CRYPT_EAL_ProviderCipherNewCtx(libCtx, GetCipherAlgId(cipher->algo), attrName);364 *ctx = CRYPT_EAL_ProviderCipherNewCtx(libCtx, GetCipherAlgId(cipher->algo), attrName);
365 365 
366 int32_t ret = CRYPT_EAL_CipherInit(*ctx, cipher->key, cipher->keyLen, cipher->iv, cipher->ivLen, enc);366 int32_t ret = CRYPT_EAL_CipherInit(*ctx, cipher->key, cipher->keyLen, cipher->iv, cipher->ivLen, enc);
@@ -607,7 +607,7 @@ CRYPT_EAL_PkeyCtx *GeneratePkeyByParaId(HITLS_Lib_Ctx *libCtx, const char *attrN
607 pkey = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, algId, isKem ? CRYPT_EAL_PKEY_KEM_OPERATE : CRYPT_EAL_PKEY_EXCH_OPERATE, attrName);607 pkey = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, algId, isKem ? CRYPT_EAL_PKEY_KEM_OPERATE : CRYPT_EAL_PKEY_EXCH_OPERATE, attrName);
608 if (pkey == NULL) {608 if (pkey == NULL) {
609 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16658, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,609 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16658, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
610- "PkeyNewCtx fail", 0, 0, 0, 0);610+ "PkeyNewCtx fail, algId is %d", algId, 0, 0, 0);
611 return NULL;611 return NULL;
612 }612 }
613 613 
@@ -967,7 +967,6 @@ int32_t HITLS_CRYPT_HkdfExtract(HITLS_Lib_Ctx *libCtx,
967 }967 }
968 CRYPT_EAL_KdfCTX *kdfCtx = NULL;968 CRYPT_EAL_KdfCTX *kdfCtx = NULL;
969 kdfCtx = CRYPT_EAL_ProviderKdfNewCtx(libCtx, CRYPT_KDF_HKDF, attrName);969 kdfCtx = CRYPT_EAL_ProviderKdfNewCtx(libCtx, CRYPT_KDF_HKDF, attrName);
970-
971 if (kdfCtx == NULL) {970 if (kdfCtx == NULL) {
972 return HITLS_CRYPT_ERR_HKDF_EXTRACT;971 return HITLS_CRYPT_ERR_HKDF_EXTRACT;
973 }972 }
@@ -1161,8 +1160,8 @@ int32_t HITLS_CRYPT_KemEncapsulate(HITLS_Lib_Ctx *libCtx, const char *attrName,
1161 CRYPT_EAL_PkeyCtx *pkey = NULL;1160 CRYPT_EAL_PkeyCtx *pkey = NULL;
1162 pkey = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, groupInfo->algId, CRYPT_EAL_PKEY_KEM_OPERATE, attrName);1161 pkey = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, groupInfo->algId, CRYPT_EAL_PKEY_KEM_OPERATE, attrName);
1163 if (pkey == NULL) {1162 if (pkey == NULL) {
1164- BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16658, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,1163+ BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15656, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
1165- "PkeyNewCtx fail", 0, 0, 0, 0);1164+ "PkeyNewCtx fail id is %d", groupInfo->algId, 0, 0, 0);
1166 return HITLS_CRYPT_ERR_KEM_ENCAPSULATE;1165 return HITLS_CRYPT_ERR_KEM_ENCAPSULATE;
1167 }1166 }
1168 1167 
@@ -26,42 +26,13 @@
26 26 
27#define MAX_PROTOCOL_LEN 6553627#define MAX_PROTOCOL_LEN 65536
28 28 
29-int32_t ALPN_SelectProtocol(uint8_t **out, uint32_t *outLen, uint8_t *clientAlpnList, uint32_t clientAlpnListLen,
30- uint8_t *servAlpnList, uint32_t servAlpnListLen)
31-{
32- if (out == NULL || outLen == NULL || clientAlpnList == NULL || servAlpnList == NULL ||
33- servAlpnListLen == 0 || clientAlpnListLen == 0) {
34- BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16690, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "intput null", 0, 0, 0, 0);
35- BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
36- return HITLS_NULL_INPUT;
37- }
38- 
39- uint32_t i, j;
40- for (i = 0; i < servAlpnListLen;) {
41- for (j = 0; j < clientAlpnListLen;) {
42- if (servAlpnList[i] == clientAlpnList[j] &&
43- (memcmp(&servAlpnList[i + 1], &clientAlpnList[j + 1], servAlpnList[i]) == 0)) {
44- *out = &servAlpnList[i + 1];
45- *outLen = servAlpnList[i];
46- return HITLS_SUCCESS;
47- }
48- j = j + clientAlpnList[j];
49- ++j;
50- }
51- i = i + servAlpnList[i];
52- ++i;
53- }
54- 
55- return HITLS_SUCCESS;
56-}
57- 
58static int32_t SelectProtocol(TLS_Ctx *ctx, uint8_t *alpnSelected, uint16_t alpnSelectedSize)29static int32_t SelectProtocol(TLS_Ctx *ctx, uint8_t *alpnSelected, uint16_t alpnSelectedSize)
59{30{
60 uint8_t *protoMatch = NULL;31 uint8_t *protoMatch = NULL;
61- uint32_t protoMatchLen = 0;32+ uint8_t protoMatchLen = 0;
62 33 
63- int32_t ret = ALPN_SelectProtocol(&protoMatch, &protoMatchLen, alpnSelected,34+ int32_t ret = HITLS_SelectAlpnProtocol(&protoMatch, &protoMatchLen, ctx->config.tlsConfig.alpnList,
64- alpnSelectedSize, ctx->config.tlsConfig.alpnList, ctx->config.tlsConfig.alpnListSize);35+ ctx->config.tlsConfig.alpnListSize, alpnSelected, alpnSelectedSize);
65 if (ret != HITLS_SUCCESS) {36 if (ret != HITLS_SUCCESS) {
66 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15258, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,37 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15258, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
67 "client check proposed protocol fail due to invalid params.", 0, 0, 0, 0);38 "client check proposed protocol fail due to invalid params.", 0, 0, 0, 0);
@@ -74,8 +45,9 @@ static int32_t SelectProtocol(TLS_Ctx *ctx, uint8_t *alpnSelected, uint16_t alpn
74 "server proposed protocol is not supported by client", 0, 0, 0, 0);45 "server proposed protocol is not supported by client", 0, 0, 0, 0);
75 return HITLS_MSG_HANDLE_ALPN_PROTOCOL_NO_MATCH;46 return HITLS_MSG_HANDLE_ALPN_PROTOCOL_NO_MATCH;
76 }47 }
48+ uint32_t protoLen = protoMatchLen;
77 49 
78- uint8_t *alpnSelectedTmp = (uint8_t *)BSL_SAL_Calloc(1u, (protoMatchLen + 1));50+ uint8_t *alpnSelectedTmp = (uint8_t *)BSL_SAL_Calloc(1u, (protoLen + 1));
79 if (alpnSelectedTmp == NULL) {51 if (alpnSelectedTmp == NULL) {
80 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15260, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,52 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15260, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
81 "client malloc selected alpn mem failed.", 0, 0, 0, 0);53 "client malloc selected alpn mem failed.", 0, 0, 0, 0);
@@ -84,19 +56,10 @@ static int32_t SelectProtocol(TLS_Ctx *ctx, uint8_t *alpnSelected, uint16_t alpn
84 return HITLS_MEMALLOC_FAIL;56 return HITLS_MEMALLOC_FAIL;
85 }57 }
86 58 
87- if (memcpy_s(alpnSelectedTmp, protoMatchLen + 1, protoMatch,59+ (void)memcpy_s(alpnSelectedTmp, protoLen + 1, protoMatch, protoLen);
88- protoMatchLen) != EOK) {
89- BSL_SAL_FREE(alpnSelectedTmp);
90- BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15261, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
91- "client copy selected alpn failed.", 0, 0, 0, 0);
92- ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
93- BSL_ERR_PUSH_ERROR(HITLS_MEMCPY_FAIL);
94- return HITLS_MEMCPY_FAIL;
95- }
96- 
97 BSL_SAL_FREE(ctx->negotiatedInfo.alpnSelected);60 BSL_SAL_FREE(ctx->negotiatedInfo.alpnSelected);
98 ctx->negotiatedInfo.alpnSelected = alpnSelectedTmp;61 ctx->negotiatedInfo.alpnSelected = alpnSelectedTmp;
99- ctx->negotiatedInfo.alpnSelectedSize = protoMatchLen;62+ ctx->negotiatedInfo.alpnSelectedSize = protoLen;
100 63 
101 return HITLS_SUCCESS;64 return HITLS_SUCCESS;
102}65}
@@ -194,7 +194,7 @@ int32_t ParseServerSelectedAlpnProtocol(
194 194 
195 (void)memcpy_s(*alpnSelected, selectedAlpnLen + 1, &pkt->buf[offset], selectedAlpnLen + 1);195 (void)memcpy_s(*alpnSelected, selectedAlpnLen + 1, &pkt->buf[offset], selectedAlpnLen + 1);
196 196 
197- *alpnSelectedSize = selectedAlpnLen;197+ *alpnSelectedSize = selectedAlpnLen + 1;
198 *haveSelectedAlpn = true;198 *haveSelectedAlpn = true;
199 199 
200 return HITLS_SUCCESS;200 return HITLS_SUCCESS;
@@ -24,9 +24,6 @@
24extern "C" {24extern "C" {
25#endif25#endif
26 26 
27-int32_t ALPN_SelectProtocol(uint8_t **out, uint32_t *outLen, uint8_t *clientAlpnList, uint32_t clientAlpnListLen,
28- uint8_t *servAlpnList, uint32_t servAlpnListLen);
29- 
30int32_t ClientCheckNegotiatedAlpn(27int32_t ClientCheckNegotiatedAlpn(
31 TLS_Ctx *ctx, bool haveSelectedAlpn, uint8_t *alpnSelected, uint16_t alpnSelectedSize);28 TLS_Ctx *ctx, bool haveSelectedAlpn, uint8_t *alpnSelected, uint16_t alpnSelectedSize);
32 29