• 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 #include <string.h>
17 #include "securec.h"
18 #include "hitls_build.h"
19 #include "tls_binlog_id.h"
20 #include "bsl_log_internal.h"
21 #include "bsl_log.h"
22 #include "bsl_err_internal.h"
23 #include "bsl_sal.h"
24 #include "hitls_error.h"
25 #include "rec.h"
26 #include "tls.h"
27 #include "hs_ctx.h"
28 #include "hs_verify.h"
29 #include "hs_common.h"
30 #include "hs_verify.h"
31 #include "recv_process.h"
32 #include "hs_kx.h"
33 #ifdef HITLS_TLS_FEATURE_SESSION
34 #include "session_mgr.h"
35 #endif
36 #ifdef HITLS_TLS_FEATURE_SESSION
37 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
SetSessionTicketInfo(TLS_Ctx * ctx)38 static int32_t SetSessionTicketInfo(TLS_Ctx *ctx)
39 {
40     int32_t ret = 0;
41     HS_Ctx *hsCtx = ctx->hsCtx;
42 
43     BSL_SAL_FREE(hsCtx->sessionId);
44     hsCtx->sessionIdSize = 0;
45 
46     if (hsCtx->ticketSize == 0) {
47         return HITLS_SUCCESS;
48     }
49 
50     if (ctx->isClient) {
51         uint8_t sessionId[HITLS_SESSION_ID_MAX_SIZE];
52         ret = SESSMGR_GernerateSessionId(ctx, sessionId, HITLS_SESSION_ID_MAX_SIZE);
53         if (ret != HITLS_SUCCESS) {
54             return ret;
55         }
56 
57         HITLS_SESS_SetSessionId(ctx->session, sessionId, HITLS_SESSION_ID_MAX_SIZE);
58     }
59 
60     ret = SESS_SetTicket(ctx->session, hsCtx->ticket, hsCtx->ticketSize);
61     if (ret != HITLS_SUCCESS) {
62         BSL_LOG_BINLOG_FIXLEN(
63             BINLOG_ID15970, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Session set ticket fail.", 0, 0, 0, 0);
64     }
65     return ret;
66 }
67 #endif
68 
SetSessionTicketAndSessionID(TLS_Ctx * ctx,bool isTls13)69 int32_t SetSessionTicketAndSessionID(TLS_Ctx *ctx, bool isTls13)
70 {
71     int32_t ret = HITLS_SUCCESS;
72     (void)ctx;
73     (void)isTls13;
74 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
75     if (ctx->negotiatedInfo.isTicket && !isTls13) {
76         ret = SetSessionTicketInfo(ctx);
77         if (ret != HITLS_SUCCESS) {
78             return ret;
79         }
80     }
81 #endif
82 
83 #ifdef HITLS_TLS_FEATURE_SESSION_ID
84     /* The default session length is 0. If the session length is not 0, insert the session length */
85     if (ctx->hsCtx->sessionIdSize != 0 && !isTls13) {
86         /* The session generated during the finish operation of TLS 1.3 cannot be used for session resume. In this
87         * case, sessionId is blocked so that the HITLS_SESS_IsResumable return value is false */
88         ret = HITLS_SESS_SetSessionId(ctx->session, ctx->hsCtx->sessionId, ctx->hsCtx->sessionIdSize);
89         if (ret != HITLS_SUCCESS) {
90             return ret;
91         }
92     }
93     ret = HITLS_SESS_SetSessionIdCtx(
94         ctx->session, ctx->config.tlsConfig.sessionIdCtx, ctx->config.tlsConfig.sessionIdCtxSize);
95     if (ret != HITLS_SUCCESS) {
96         return ret;
97     }
98 #endif
99     return ret;
100 }
101 
SessionConfig(TLS_Ctx * ctx)102 static int32_t SessionConfig(TLS_Ctx *ctx)
103 {
104     int32_t ret = 0;
105     bool isTls13 = (ctx->negotiatedInfo.version == HITLS_VERSION_TLS13);
106     HS_Ctx *hsCtx = ctx->hsCtx;
107     ret = SetSessionTicketAndSessionID(ctx, isTls13);
108     if (ret != HITLS_SUCCESS) {
109         return ret;
110     }
111 
112 #ifdef HITLS_TLS_FEATURE_SNI
113     /* When the SNI negotiation is HITLS_ACCEPT_ERR_OK, save the client Hello server_name extension to the session
114      * structure */
115     if (ctx->negotiatedInfo.isSniStateOK && isTls13 == false) {
116         ret = SESS_SetHostName(ctx->session, hsCtx->serverNameSize, hsCtx->serverName);
117         if (ret != HITLS_SUCCESS) {
118             BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17076, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
119                 "SetHostName fail", 0, 0, 0, 0);
120             return ret;
121         }
122     }
123 #endif /* HITLS_TLS_FEATURE_SNI */
124     (void)HITLS_SESS_SetProtocolVersion(ctx->session, ctx->negotiatedInfo.version);
125     (void)HITLS_SESS_SetCipherSuite(ctx->session, ctx->negotiatedInfo.cipherSuiteInfo.cipherSuite);
126 
127     uint32_t masterKeySize = MASTER_SECRET_LEN;
128 #ifdef HITLS_TLS_PROTO_TLS13
129     if (isTls13) {
130         masterKeySize = SAL_CRYPT_DigestSize(ctx->negotiatedInfo.cipherSuiteInfo.hashAlg);
131         if (masterKeySize == 0) {
132             BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17077, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
133                 "DigestSize fail", 0, 0, 0, 0);
134             return HITLS_CRYPT_ERR_DIGEST;
135         }
136     }
137 #endif
138 
139     ret = HITLS_SESS_SetMasterKey(ctx->session, hsCtx->masterKey, masterKeySize);
140     if (ret != HITLS_SUCCESS) {
141         return ret;
142     }
143     ret = HITLS_SESS_SetHaveExtMasterSecret(ctx->session, (uint8_t)ctx->negotiatedInfo.isExtendedMasterSecret);
144     if (ret != HITLS_SUCCESS) {
145         return ret;
146     }
147 #if defined(HITLS_TLS_CONNECTION_INFO_NEGOTIATION) && defined(HITLS_TLS_FEATURE_SESSION)
148     if (ctx->config.tlsConfig.isKeepPeerCert) {
149         ret = SESS_SetPeerCert(ctx->session, hsCtx->peerCert, ctx->isClient);
150         if (ret != HITLS_SUCCESS) {
151             return ret;
152         }
153         hsCtx->peerCert = NULL;
154     }
155 #endif /* HITLS_TLS_CONNECTION_INFO_NEGOTIATION && HITLS_TLS_FEATURE_SESSION */
156 
157     return HITLS_SUCCESS;
158 }
159 
HsSetSessionInfo(TLS_Ctx * ctx)160 static int32_t HsSetSessionInfo(TLS_Ctx *ctx)
161 {
162     int32_t ret = 0;
163     TLS_SessionMgr *sessMgr = ctx->config.tlsConfig.sessMgr;
164 
165     SESSMGR_ClearTimeout(sessMgr);
166 
167     /* This parameter is not required for session multiplexing */
168     if (ctx->negotiatedInfo.isResume == true) {
169         return HITLS_SUCCESS;
170     }
171 
172     HITLS_SESS_Free(ctx->session);
173 
174     ctx->session = HITLS_SESS_New();
175     if (ctx->session == NULL) {
176         BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
177         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15893, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
178             "Session malloc fail.", 0, 0, 0, 0);
179         return HITLS_MEMALLOC_FAIL;
180     }
181     uint64_t timeout = SESSMGR_GetTimeout(sessMgr);
182 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
183     timeout = ctx->hsCtx->ticketLifetimeHint == 0 ? timeout : ctx->hsCtx->ticketLifetimeHint;
184 #endif
185     HITLS_SESS_SetTimeout(ctx->session, timeout);
186     ret = SessionConfig(ctx);
187     if (ret != HITLS_SUCCESS) {
188         return ret;
189     }
190 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
191     /* The session cache does not store TLS1.3 sessions */
192     if (ctx->negotiatedInfo.version != HITLS_VERSION_TLS13) {
193         SESSMGR_InsertSession(sessMgr, ctx->session, ctx->isClient);
194         if (ctx->globalConfig != NULL && ctx->globalConfig->newSessionCb != NULL) {
195             HITLS_SESS_UpRef(ctx->session); // It is convenient for users to take away and needs to be released by users
196             if (ctx->globalConfig->newSessionCb(ctx, ctx->session) == 0) {
197                 /* If the user does not reference the session, the number of reference times decreases by 1 */
198                 HITLS_SESS_Free(ctx->session);
199             }
200         }
201     }
202 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
203     return HITLS_SUCCESS;
204 }
205 #endif /* HITLS_TLS_FEATURE_SESSION */
206 
CheckFinishedVerifyData(const FinishedMsg * finishedMsg,const uint8_t * verifyData,uint32_t verifyDataSize)207 int32_t CheckFinishedVerifyData(const FinishedMsg *finishedMsg, const uint8_t *verifyData, uint32_t verifyDataSize)
208 {
209     if ((finishedMsg->verifyDataSize == 0u) || (verifyDataSize == 0u)) {
210         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_INCORRECT_DIGEST_LEN);
211         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15737, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
212             "Finished data len cannot be zero.", 0, 0, 0, 0);
213         return HITLS_MSG_HANDLE_INCORRECT_DIGEST_LEN;
214     }
215 
216     if (finishedMsg->verifyDataSize != verifyDataSize) {
217         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_INCORRECT_DIGEST_LEN);
218         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15738, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
219             "Finished data len unequal.", 0, 0, 0, 0);
220         return HITLS_MSG_HANDLE_INCORRECT_DIGEST_LEN;
221     }
222 
223     if (memcmp(finishedMsg->verifyData, verifyData, verifyDataSize) != 0) {
224         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL);
225         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15739, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
226             "Finished data unequal.", 0, 0, 0, 0);
227         return HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL;
228     }
229 
230     return HITLS_SUCCESS;
231 }
232 #ifdef HITLS_TLS_HOST_CLIENT
ClientRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)233 int32_t ClientRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
234 {
235     int32_t ret = 0;
236     HS_Ctx *hsCtx = (HS_Ctx *)ctx->hsCtx;
237     VerifyCtx *verifyCtx = hsCtx->verifyCtx;
238     const FinishedMsg *finished = &msg->body.finished;
239     uint8_t verifyData[MAX_DIGEST_SIZE] = {0};
240     uint32_t verifyDataSize = MAX_DIGEST_SIZE;
241 
242     ret = VERIFY_GetVerifyData(verifyCtx, verifyData, &verifyDataSize);
243     if (ret != HITLS_SUCCESS) {
244         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15740, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
245             "client get server finished verify data error.", 0, 0, 0, 0);
246         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
247         return ret;
248     }
249 
250     ret = CheckFinishedVerifyData(finished, verifyData, verifyDataSize);
251     if (ret != HITLS_SUCCESS) {
252         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15741, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
253             "client verify server finished data error.", 0, 0, 0, 0);
254         if (ret == HITLS_MSG_HANDLE_INCORRECT_DIGEST_LEN) {
255             ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_DECODE_ERROR);
256         } else {
257             ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_DECRYPT_ERROR);
258         }
259         return HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL;
260     }
261 #ifdef HITLS_TLS_FEATURE_SESSION
262     ret = HsSetSessionInfo(ctx);
263     if (ret != HITLS_SUCCESS) {
264         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15895, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
265             "set session information failed.", 0, 0, 0, 0);
266         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
267         return ret;
268     }
269 #endif /* HITLS_TLS_FEATURE_SESSION */
270     /* CCS messages are not allowed to be received later. */
271     ctx->method.ctrlCCS(ctx, CCS_CMD_RECV_EXIT_READY);
272     return HITLS_SUCCESS;
273 }
274 #ifdef HITLS_TLS_PROTO_TLS_BASIC
Tls12ClientRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)275 int32_t Tls12ClientRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
276 {
277     int32_t ret = ClientRecvFinishedProcess(ctx, msg);
278     if (ret != HITLS_SUCCESS) {
279         return ret;
280     }
281 
282     if (ctx->negotiatedInfo.isResume == true) {
283         ctx->method.ctrlCCS(ctx, CCS_CMD_RECV_EXIT_READY);
284         return HS_ChangeState(ctx, TRY_SEND_CHANGE_CIPHER_SPEC);
285     }
286 
287     return HS_ChangeState(ctx, TLS_CONNECTED);
288 }
289 #endif /* HITLS_TLS_PROTO_TLS_BASIC */
290 
291 #ifdef HITLS_TLS_PROTO_DTLS12
DtlsClientRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)292 int32_t DtlsClientRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
293 {
294 #ifdef HITLS_BSL_UIO_UDP
295     if (ctx->preState == CM_STATE_TRANSPORTING && ctx->state == CM_STATE_HANDSHAKING) {
296         REC_RetransmitListFlush(ctx);
297         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15888, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN,
298             "recv post hs finished, send retransmit msg.", 0, 0, 0, 0);
299         return HS_ChangeState(ctx, TLS_CONNECTED);
300     }
301 #endif /* HITLS_BSL_UIO_UDP */
302     int32_t ret = ClientRecvFinishedProcess(ctx, msg);
303     if (ret != HITLS_SUCCESS) {
304         return ret;
305     }
306 #ifdef HITLS_BSL_UIO_UDP
307     /* Clear the retransmission queue */
308     REC_RetransmitListClean(ctx->recCtx);
309 #endif /* HITLS_BSL_UIO_UDP */
310     if (ctx->negotiatedInfo.isResume == true) {
311         ctx->method.ctrlCCS(ctx, CCS_CMD_RECV_EXIT_READY);
312         return HS_ChangeState(ctx, TRY_SEND_CHANGE_CIPHER_SPEC);
313     }
314 
315     return HS_ChangeState(ctx, TLS_CONNECTED);
316 }
317 #endif
318 #ifdef HITLS_TLS_PROTO_TLS13
Tls13ClientRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)319 int32_t Tls13ClientRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
320 {
321     int32_t ret = ClientRecvFinishedProcess(ctx, msg);
322     if (ret != HITLS_SUCCESS) {
323         return ret;
324     }
325 
326     ret = HS_TLS13CalcServerFinishProcessSecret(ctx);
327     if (ret != HITLS_SUCCESS) {
328         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17078, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
329             "CalcServerFinishProcessSecret fail", 0, 0, 0, 0);
330         return ret;
331     }
332 
333     /* Activate serverAppTrafficSecret to decrypt the App data sent by the server */
334     uint32_t hashLen = SAL_CRYPT_DigestSize(ctx->negotiatedInfo.cipherSuiteInfo.hashAlg);
335     ret = HS_SwitchTrafficKey(ctx, ctx->serverAppTrafficSecret, hashLen, false);
336     if (ret != HITLS_SUCCESS) {
337         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17079, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
338             "SwitchTrafficKey fail", 0, 0, 0, 0);
339         return ret;
340     }
341 
342     if (ctx->hsCtx->isNeedClientCert) {
343         return HS_ChangeState(ctx, TRY_SEND_CERTIFICATE);
344     }
345 
346     return HS_ChangeState(ctx, TRY_SEND_FINISH);
347 }
348 #endif /* HITLS_TLS_PROTO_TLS13 */
349 #endif /* HITLS_TLS_HOST_CLIENT */
350 
351 #ifdef HITLS_TLS_HOST_SERVER
352 
ServerRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)353 int32_t ServerRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
354 {
355     int32_t ret = 0;
356     HS_Ctx *hsCtx = (HS_Ctx *)ctx->hsCtx;
357     VerifyCtx *verifyCtx = hsCtx->verifyCtx;
358     uint8_t verifyData[MAX_DIGEST_SIZE] = {0};
359     uint32_t verifyDataSize = MAX_DIGEST_SIZE;
360     const FinishedMsg *finished = &msg->body.finished;
361 
362     ret = VERIFY_GetVerifyData(verifyCtx, verifyData, &verifyDataSize);
363     if (ret != HITLS_SUCCESS) {
364         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15742, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
365             "server get client finished verify data error.", 0, 0, 0, 0);
366         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
367         return ret;
368     }
369 
370     ret = CheckFinishedVerifyData(finished, verifyData, verifyDataSize);
371     if (ret != HITLS_SUCCESS) {
372         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL);
373         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15743, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
374             "server verify client finished data error.", 0, 0, 0, 0);
375         if (ret == HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL) {
376             ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_DECRYPT_ERROR);
377         } else {
378             ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_DECODE_ERROR);
379         }
380         return HITLS_MSG_HANDLE_VERIFY_FINISHED_FAIL;
381     }
382 #ifdef HITLS_TLS_FEATURE_SESSION
383     ret = HsSetSessionInfo(ctx);
384     if (ret != HITLS_SUCCESS) {
385         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15897, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
386             "set session information failed.", 0, 0, 0, 0);
387         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
388         return ret;
389     }
390 #endif /* HITLS_TLS_FEATURE_SESSION */
391     return HITLS_SUCCESS;
392 }
393 #ifdef HITLS_TLS_PROTO_TLS_BASIC
Tls12ServerRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)394 int32_t Tls12ServerRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
395 {
396     int32_t ret = ServerRecvFinishedProcess(ctx, msg);
397     if (ret != HITLS_SUCCESS) {
398         return ret;
399     }
400 
401     if (ctx->negotiatedInfo.isResume == true) {
402         ctx->method.ctrlCCS(ctx, CCS_CMD_RECV_EXIT_READY);
403         return HS_ChangeState(ctx, TLS_CONNECTED);
404     }
405 
406     if (ctx->negotiatedInfo.isTicket == true) {
407         return HS_ChangeState(ctx, TRY_SEND_NEW_SESSION_TICKET);
408     }
409 
410     return HS_ChangeState(ctx, TRY_SEND_CHANGE_CIPHER_SPEC);
411 }
412 #endif /* HITLS_TLS_PROTO_TLS_BASIC */
413 #ifdef HITLS_TLS_PROTO_DTLS12
DtlsServerRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)414 int32_t DtlsServerRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
415 {
416 #ifdef HITLS_BSL_UIO_UDP
417     if (ctx->preState == CM_STATE_TRANSPORTING && ctx->state == CM_STATE_HANDSHAKING) {
418         REC_RetransmitListFlush(ctx);
419         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15885, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN,
420             "recv post hs finished, send retransmit msg.", 0, 0, 0, 0);
421         return HS_ChangeState(ctx, TLS_CONNECTED);
422     }
423 #endif /* HITLS_BSL_UIO_UDP */
424     int32_t ret = ServerRecvFinishedProcess(ctx, msg);
425     if (ret != HITLS_SUCCESS) {
426         return ret;
427     }
428 #ifdef HITLS_BSL_UIO_UDP
429     /* Clear the retransmission queue */
430     REC_RetransmitListClean(ctx->recCtx);
431 #endif /* HITLS_BSL_UIO_UDP */
432 #ifdef HITLS_TLS_FEATURE_SESSION
433     if (ctx->negotiatedInfo.isResume == true) {
434         ctx->method.ctrlCCS(ctx, CCS_CMD_RECV_EXIT_READY);
435         return HS_ChangeState(ctx, TLS_CONNECTED);
436     }
437 #endif /* HITLS_TLS_FEATURE_SESSION */
438 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
439     if (ctx->negotiatedInfo.isTicket == true) {
440         return HS_ChangeState(ctx, TRY_SEND_NEW_SESSION_TICKET);
441     }
442 #endif /* HITLS_TLS_FEATURE_SESSION_TICKET */
443     return HS_ChangeState(ctx, TRY_SEND_CHANGE_CIPHER_SPEC);
444 }
445 #endif
446 #ifdef HITLS_TLS_PROTO_TLS13
Tls13ServerRecvFinishedProcess(TLS_Ctx * ctx,const HS_Msg * msg)447 int32_t Tls13ServerRecvFinishedProcess(TLS_Ctx *ctx, const HS_Msg *msg)
448 {
449     /** CCS messages are not allowed to be received */
450     ctx->method.ctrlCCS(ctx, CCS_CMD_RECV_EXIT_READY);
451     ctx->plainAlertForbid = true;
452 
453     int32_t ret = ServerRecvFinishedProcess(ctx, msg);
454     if (ret != HITLS_SUCCESS) {
455         return ret;
456     }
457 #ifdef HITLS_TLS_FEATURE_PHA
458     if (ctx->phaState == PHA_REQUESTED) {
459         ctx->phaState = PHA_EXTENSION;
460     } else
461 #endif /* HITLS_TLS_FEATURE_PHA */
462     {
463         /* Switch Application Traffic Secret */
464         uint32_t hashLen = SAL_CRYPT_DigestSize(ctx->negotiatedInfo.cipherSuiteInfo.hashAlg);
465         ret = HS_SwitchTrafficKey(ctx, ctx->clientAppTrafficSecret, hashLen, false);
466         if (ret != HITLS_SUCCESS) {
467             BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17080, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
468                 "SwitchTrafficKey fail", 0, 0, 0, 0);
469             return ret;
470         }
471 
472         ret = HS_TLS13DeriveResumptionMasterSecret(ctx);
473         if (ret != HITLS_SUCCESS) {
474             return ret;
475         }
476 #ifdef HITLS_TLS_FEATURE_PHA
477         if (ctx->phaState == PHA_EXTENSION && ctx->config.tlsConfig.isSupportClientVerify &&
478             ctx->config.tlsConfig.isSupportPostHandshakeAuth) {
479             SAL_CRYPT_DigestFree(ctx->phaHash);
480             ctx->phaHash = SAL_CRYPT_DigestCopy(ctx->hsCtx->verifyCtx->hashCtx);
481             if (ctx->phaHash == NULL) {
482                 BSL_ERR_PUSH_ERROR(HITLS_CRYPT_ERR_DIGEST);
483                 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16176, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
484                     "pha hash copy error: digest copy fail.", 0, 0, 0, 0);
485                 return HITLS_CRYPT_ERR_DIGEST;
486             }
487         }
488 #endif /* HITLS_TLS_FEATURE_PHA */
489     }
490 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
491     /* When ticketNums is 0, no ticket is sent */
492     if (ctx->hsCtx->sentTickets < ctx->config.tlsConfig.ticketNums) {
493         return HS_ChangeState(ctx, TRY_SEND_NEW_SESSION_TICKET);
494     }
495 #endif
496     return HS_ChangeState(ctx, TLS_CONNECTED);
497 }
498 #endif /* HITLS_TLS_PROTO_TLS13 */
499 #endif /* HITLS_TLS_HOST_SERVER */