• 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 "hitls_build.h"
17 #if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
18 #include "tls_binlog_id.h"
19 #include "bsl_log_internal.h"
20 #include "bsl_log.h"
21 #include "bsl_err_internal.h"
22 #include "bsl_uio.h"
23 #include "bsl_errno.h"
24 #include "sal_time.h"
25 #include "hitls.h"
26 #include "hitls_error.h"
27 #include "tls_config.h"
28 #include "hs_ctx.h"
29 #include "hs_dtls_timer.h"
30 
31 #define DTLS_HS_2MSL_TIMEOUT_VALUE    240000000 /* 2 times the MSL(Maximum Segment Lifetime) time. Unit: us */
32 #define DTLS_HS_DEFAULT_TIMEOUT_VALUE 1000000
33 #define DTLS_HS_MAX_TIMEOUT_VALUE     60000000
34 #define DTLS_HS_MAX_TIMEOUT_NUM       12        /* Maximum Timeout Times */
35 
SetDtlsTimerDeadLine(TLS_Ctx * ctx,uint32_t timeoutValue)36 static int32_t SetDtlsTimerDeadLine(TLS_Ctx *ctx, uint32_t timeoutValue)
37 {
38     HS_Ctx *hsCtx = ctx->hsCtx;
39     BSL_TIME curTime = {0};
40     int32_t ret = (int32_t)BSL_SAL_SysTimeGet(&curTime);
41     if (ret != BSL_SUCCESS) {
42         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_SYS_TIME_FAIL);
43         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15774, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
44             "BSL_SAL_SysTimeGet fail when start dtls timer.", 0, 0, 0, 0);
45         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
46         return HITLS_MSG_HANDLE_SYS_TIME_FAIL;
47     }
48 
49     ret = (int32_t)BSL_DateTimeAddUs(&hsCtx->deadline, &curTime, timeoutValue);
50     if (ret != BSL_SUCCESS) {
51         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_SYS_TIME_FAIL);
52         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15775, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
53             "BSL_DateTimeAddUs fail when start dtls timer.", 0, 0, 0, 0);
54         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
55         return HITLS_MSG_HANDLE_SYS_TIME_FAIL;
56     }
57     return HITLS_SUCCESS;
58 }
59 
HS_Start2MslTimer(TLS_Ctx * ctx)60 int32_t HS_Start2MslTimer(TLS_Ctx *ctx)
61 {
62     HS_Ctx *hsCtx = ctx->hsCtx;
63     if (!BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP)) {
64         return HITLS_SUCCESS;
65     }
66 
67     uint32_t timeoutValue = DTLS_HS_2MSL_TIMEOUT_VALUE;
68     if (ctx->config.tlsConfig.dtlsPostHsTimeoutVal != 0) {
69         timeoutValue = ctx->config.tlsConfig.dtlsPostHsTimeoutVal;
70     }
71 
72     int32_t ret = SetDtlsTimerDeadLine(ctx, timeoutValue);
73     if (ret != BSL_SUCCESS) {
74         return ret;
75     }
76 
77     hsCtx->timeoutValue = timeoutValue;
78     hsCtx->timeoutNum = 0;
79     return HITLS_SUCCESS;
80 }
81 
HS_StartTimer(TLS_Ctx * ctx)82 int32_t HS_StartTimer(TLS_Ctx *ctx)
83 {
84     HS_Ctx *hsCtx = ctx->hsCtx;
85     if (!BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP)) {
86         return HITLS_SUCCESS;
87     }
88 
89     uint32_t timeoutValue = DTLS_HS_DEFAULT_TIMEOUT_VALUE;
90     if (ctx->config.tlsConfig.dtlsTimerCb != NULL) {
91         timeoutValue = ctx->config.tlsConfig.dtlsTimerCb(ctx, 0);
92     }
93 
94     int32_t ret = SetDtlsTimerDeadLine(ctx, timeoutValue);
95     if (ret != BSL_SUCCESS) {
96         return ret;
97     }
98 
99     hsCtx->timeoutValue = timeoutValue;
100     hsCtx->timeoutNum = 0;
101     return HITLS_SUCCESS;
102 }
103 
HS_IsTimeout(TLS_Ctx * ctx,bool * isTimeout)104 int32_t HS_IsTimeout(TLS_Ctx *ctx, bool *isTimeout)
105 {
106     HS_Ctx *hsCtx = ctx->hsCtx;
107     if (!BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP)) {
108         *isTimeout = false;
109         return HITLS_SUCCESS;
110     }
111 
112     BSL_TIME curTime = {0};
113     int32_t ret = (int32_t)BSL_SAL_SysTimeGet(&curTime);
114     if (ret != BSL_SUCCESS) {
115         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_SYS_TIME_FAIL);
116         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15776, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
117             "BSL_SAL_SysTimeGet fail when judgment dtls timeout.", 0, 0, 0, 0);
118         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
119         return HITLS_MSG_HANDLE_SYS_TIME_FAIL;
120     }
121 
122     *isTimeout = false;
123     /* When the server sends the hello verify request, the timer does not need to be started. In this case, the function
124      * returns a failure. Therefore, the failure is not considered as timeout */
125     ret = (int32_t)BSL_SAL_DateTimeCompareByUs(&curTime, &hsCtx->deadline);
126     if (ret == BSL_TIME_DATE_AFTER) {
127         *isTimeout = true;
128     }
129 
130     return HITLS_SUCCESS;
131 }
132 
HS_TimeoutProcess(TLS_Ctx * ctx)133 int32_t HS_TimeoutProcess(TLS_Ctx *ctx)
134 {
135     HS_Ctx *hsCtx = ctx->hsCtx;
136     if (!BSL_UIO_GetUioChainTransportType(ctx->uio, BSL_UIO_UDP)) {
137         return HITLS_SUCCESS;
138     }
139 
140     BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15777, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN,
141         "dtls timeout, timeoutNum = %u, timeoutValue = %u(us).",
142         hsCtx->timeoutNum, hsCtx->timeoutValue, 0, 0);
143 
144     uint32_t timeoutValue = hsCtx->timeoutValue;
145     if (ctx->config.tlsConfig.dtlsTimerCb != NULL) {
146         timeoutValue = ctx->config.tlsConfig.dtlsTimerCb(ctx, timeoutValue);
147     } else {
148         timeoutValue *= 2; /* 2 indicates that the timeout period for each retransmission is doubled. */
149         if (timeoutValue > DTLS_HS_MAX_TIMEOUT_VALUE) {
150             /* The maximum timeout duration of the timer is 60s */
151             timeoutValue = DTLS_HS_MAX_TIMEOUT_VALUE;
152         }
153     }
154 
155     int32_t ret = SetDtlsTimerDeadLine(ctx, timeoutValue);
156     if (ret != BSL_SUCCESS) {
157         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16827, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
158             "SetDtlsTimerDeadLine fail", 0, 0, 0, 0);
159         return ret;
160     }
161 
162     hsCtx->timeoutValue = timeoutValue;
163     hsCtx->timeoutNum++;
164     if (hsCtx->timeoutNum > DTLS_HS_MAX_TIMEOUT_NUM) {
165         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_DTLS_CONNECT_TIMEOUT);
166         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15778, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
167             "dtls connect timeout.", 0, 0, 0, 0);
168         /* There is no need to send an alert to peer after multiple connection failures */
169         return HITLS_MSG_HANDLE_DTLS_CONNECT_TIMEOUT;
170     }
171     return HITLS_SUCCESS;
172 }
173 #endif /* HITLS_TLS_PROTO_DTLS12 && HITLS_BSL_UIO_UDP */