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 #include "securec.h"
16 #include "hitls_build.h"
17 #include "tls_binlog_id.h"
18 #include "bsl_log_internal.h"
19 #include "bsl_log.h"
20 #include "bsl_err_internal.h"
21 #include "bsl_bytes.h"
22 #include "hitls.h"
23 #include "hitls_error.h"
24 #include "hitls_config.h"
25 #include "tls.h"
26 #include "hs.h"
27 #include "hs_common.h"
28 #include "parse_msg.h"
29 #include "parse_common.h"
30 #ifdef HITLS_TLS_FEATURE_INDICATOR
31 #include "indicator.h"
32 #endif /* HITLS_TLS_FEATURE_INDICATOR */
33 typedef int32_t (*CheckHsMsgTypeFunc)(TLS_Ctx *ctx, const HS_MsgType msgType);
34
35 typedef struct {
36 HS_MsgType msgType;
37 CheckHsMsgTypeFunc checkCb;
38 } HsMsgTypeCheck;
39
40 #ifdef HITLS_TLS_PROTO_DTLS12
CheckHelloVerifyRequestType(TLS_Ctx * ctx,const HS_MsgType msgType)41 static int32_t CheckHelloVerifyRequestType(TLS_Ctx *ctx, const HS_MsgType msgType)
42 {
43 if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask) && msgType == SERVER_HELLO) {
44 (void)HS_ChangeState(ctx, TRY_RECV_SERVER_HELLO);
45 return HITLS_SUCCESS;
46 }
47 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17022, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
48 "Check hvr Type fail", 0, 0, 0, 0);
49 return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
50 }
51 #endif
52
CheckServerHelloType(TLS_Ctx * ctx,const HS_MsgType msgType)53 static int32_t CheckServerHelloType(TLS_Ctx *ctx, const HS_MsgType msgType)
54 {
55 /* In DTLS, When client try to receive ServerHello message, it doesn't know if server enables
56 * isSupportDtlsCookieExchange. If client receives HelloVerifyRequest message, also valid */
57 if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask) && msgType == HELLO_VERIFY_REQUEST) {
58 (void)HS_ChangeState(ctx, TRY_RECV_HELLO_VERIFY_REQUEST);
59 return HITLS_SUCCESS;
60 }
61 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17331, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
62 "CheckServerHelloType fail", 0, 0, 0, 0);
63 return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
64 }
65
CheckServerKeyExchangeType(TLS_Ctx * ctx,const HS_MsgType msgType)66 static int32_t CheckServerKeyExchangeType(TLS_Ctx *ctx, const HS_MsgType msgType)
67 {
68 /* When the PSK and RSA_PSK are used, whether the ServerKeyExchange message is received depends on whether the
69 * server sends a PSK identity hint */
70 if (ctx->hsCtx->kxCtx->keyExchAlgo == HITLS_KEY_EXCH_PSK ||
71 ctx->hsCtx->kxCtx->keyExchAlgo == HITLS_KEY_EXCH_RSA_PSK) {
72 if (msgType == CERTIFICATE_REQUEST) {
73 (void)HS_ChangeState(ctx, TRY_RECV_CERTIFICATE_REQUEST);
74 return HITLS_SUCCESS;
75 } else if (msgType == SERVER_HELLO_DONE) {
76 (void)HS_ChangeState(ctx, TRY_RECV_SERVER_HELLO_DONE);
77 return HITLS_SUCCESS;
78 }
79 }
80 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17025, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
81 "CheckServerKeyExchangeType fail", 0, 0, 0, 0);
82 return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
83 }
84
CheckCertificateRequestType(TLS_Ctx * ctx,const HS_MsgType msgType)85 static int32_t CheckCertificateRequestType(TLS_Ctx *ctx, const HS_MsgType msgType)
86 {
87 uint32_t version = HS_GetVersion(ctx);
88 if (version == HITLS_VERSION_TLS13) {
89 if (msgType == CERTIFICATE) {
90 (void)HS_ChangeState(ctx, TRY_RECV_CERTIFICATE);
91 return HITLS_SUCCESS;
92 }
93 } else {
94 if (msgType == SERVER_HELLO_DONE) {
95 (void)HS_ChangeState(ctx, TRY_RECV_SERVER_HELLO_DONE);
96 return HITLS_SUCCESS;
97 }
98 }
99 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17026, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
100 "Check cert reqType fail", 0, 0, 0, 0);
101 return HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE;
102 }
103
104 static const HsMsgTypeCheck g_checkHsMsgTypeList[] = {
105 [TRY_RECV_CLIENT_HELLO] = {.msgType = CLIENT_HELLO,
106 .checkCb = NULL},
107 [TRY_RECV_SERVER_HELLO] = {.msgType = SERVER_HELLO, .checkCb = CheckServerHelloType},
108 #ifdef HITLS_TLS_PROTO_DTLS12
109 [TRY_RECV_HELLO_VERIFY_REQUEST] = {.msgType = HELLO_VERIFY_REQUEST, .checkCb = CheckHelloVerifyRequestType},
110 #endif
111 [TRY_RECV_ENCRYPTED_EXTENSIONS] = {.msgType = ENCRYPTED_EXTENSIONS, .checkCb = NULL},
112 [TRY_RECV_CERTIFICATE] = {.msgType = CERTIFICATE, .checkCb = NULL},
113 [TRY_RECV_SERVER_KEY_EXCHANGE] = {.msgType = SERVER_KEY_EXCHANGE, .checkCb = CheckServerKeyExchangeType},
114 [TRY_RECV_CERTIFICATE_REQUEST] = {.msgType = CERTIFICATE_REQUEST, .checkCb = CheckCertificateRequestType},
115 [TRY_RECV_SERVER_HELLO_DONE] = {.msgType = SERVER_HELLO_DONE, .checkCb = NULL},
116 [TRY_RECV_CLIENT_KEY_EXCHANGE] = {.msgType = CLIENT_KEY_EXCHANGE, .checkCb = NULL},
117 [TRY_RECV_CERTIFICATE_VERIFY] = {.msgType = CERTIFICATE_VERIFY, .checkCb = NULL},
118 [TRY_RECV_NEW_SESSION_TICKET] = {.msgType = NEW_SESSION_TICKET, .checkCb = NULL},
119 [TRY_RECV_FINISH] = {.msgType = FINISHED, .checkCb = NULL},
120 [TRY_RECV_KEY_UPDATE] = {.msgType = KEY_UPDATE, .checkCb = NULL},
121 [TRY_RECV_HELLO_REQUEST] = {.msgType = HELLO_REQUEST, .checkCb = NULL},
122 };
123
CheckHsMsgType(TLS_Ctx * ctx,HS_MsgType msgType)124 int32_t CheckHsMsgType(TLS_Ctx *ctx, HS_MsgType msgType)
125 {
126 if (ctx->state != CM_STATE_HANDSHAKING && ctx->state != CM_STATE_RENEGOTIATION) {
127 return HITLS_SUCCESS;
128 }
129
130 if ((msgType == HELLO_REQUEST) && (ctx->isClient)) {
131 /* The HelloRequest message may appear at any time during the handshake.
132 The client should ignore this message */
133 return HITLS_SUCCESS;
134 }
135
136 HS_Ctx *hsCtx = ctx->hsCtx;
137 const char *expectedMsg = NULL;
138 if (msgType != g_checkHsMsgTypeList[hsCtx->state].msgType) {
139 if (g_checkHsMsgTypeList[hsCtx->state].checkCb == NULL ||
140 g_checkHsMsgTypeList[hsCtx->state].checkCb(ctx, msgType) != HITLS_SUCCESS) {
141 expectedMsg = HS_GetMsgTypeStr(g_checkHsMsgTypeList[hsCtx->state].msgType);
142 }
143 }
144
145 if (msgType == FINISHED && HS_GetVersion(ctx) != HITLS_VERSION_TLS13 &&
146 !(ctx->state == CM_STATE_HANDSHAKING && ctx->preState == CM_STATE_TRANSPORTING)) {
147 bool isCcsRecv = ctx->method.isRecvCCS(ctx);
148 if (isCcsRecv != true) {
149 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15349, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
150 "recv finish but haven't recv ccs", 0, 0, 0, 0);
151 expectedMsg = HS_GetMsgTypeStr(FINISHED);
152 }
153 }
154
155 if (expectedMsg != NULL) {
156 BSL_LOG_BINLOG_VARLEN(BINLOG_ID16148, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
157 "Handshake state expect %s", expectedMsg);
158 BSL_LOG_BINLOG_VARLEN(BINLOG_ID16149, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
159 ", but got %s.", HS_GetMsgTypeStr(msgType));
160 return ParseErrorProcess(ctx, HITLS_MSG_HANDLE_UNEXPECTED_MESSAGE, 0,
161 NULL, ALERT_UNEXPECTED_MESSAGE);
162 }
163 return HITLS_SUCCESS;
164 }
165
CheckHsMsgLen(TLS_Ctx * ctx,HS_MsgInfo * hsMsgInfo)166 static int32_t CheckHsMsgLen(TLS_Ctx *ctx, HS_MsgInfo *hsMsgInfo)
167 {
168 int32_t ret = HITLS_SUCCESS;
169 uint32_t hsMsgOfSpecificTypeMaxSize = HS_MaxMessageSize(ctx, hsMsgInfo->type);
170 if (hsMsgInfo->length > hsMsgOfSpecificTypeMaxSize) {
171 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16161, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
172 "(D)TLS HS msg type: %d, parsed length: %u, max length: %u.", (int)hsMsgInfo->type, hsMsgInfo->length,
173 hsMsgOfSpecificTypeMaxSize, 0);
174 return ParseErrorProcess(ctx, HITLS_PARSE_EXCESSIVE_MESSAGE_SIZE, 0,
175 NULL, ALERT_ILLEGAL_PARAMETER);
176 }
177 uint32_t headerLen = IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask) ?
178 DTLS_HS_MSG_HEADER_SIZE : HS_MSG_HEADER_SIZE;
179 ret = HS_GrowMsgBuf(ctx, headerLen + hsMsgInfo->length, true);
180 if (ret != HITLS_SUCCESS) {
181 return ret;
182 }
183 hsMsgInfo->rawMsg = ctx->hsCtx->msgBuf;
184 hsMsgInfo->headerAndBodyLen = headerLen + hsMsgInfo->length;
185 return ret;
186 }
187
188 #ifdef HITLS_TLS_PROTO_DTLS12
DtlsParseHsMsgHeader(TLS_Ctx * ctx,const uint8_t * data,uint32_t len,HS_MsgInfo * hsMsgInfo)189 static int32_t DtlsParseHsMsgHeader(TLS_Ctx *ctx, const uint8_t *data, uint32_t len, HS_MsgInfo *hsMsgInfo)
190 {
191 const char *logStr = BINGLOG_STR("parse DTLS handshake msg header failed.");
192 if (len < DTLS_HS_MSG_HEADER_SIZE) {
193 return ParseErrorProcess(ctx, HITLS_PARSE_INVALID_MSG_LEN, BINLOG_ID15599,
194 logStr, ALERT_DECODE_ERROR);
195 }
196
197 hsMsgInfo->type = data[0]; /* The 0 byte is the handshake message type */
198 if (hsMsgInfo->type >= HS_MSG_TYPE_END) {
199 BSL_ERR_PUSH_ERROR(HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG);
200 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16123, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
201 "DTLS invalid message type: %d.", hsMsgInfo->type, 0, 0, 0);
202 ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_UNEXPECTED_MESSAGE);
203 return HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG;
204 }
205 hsMsgInfo->length = BSL_ByteToUint24(&data[DTLS_HS_MSGLEN_ADDR]);
206 hsMsgInfo->sequence = BSL_ByteToUint16(&data[DTLS_HS_MSGSEQ_ADDR]);
207 hsMsgInfo->fragmentOffset = BSL_ByteToUint24(&data[DTLS_HS_FRAGMENT_OFFSET_ADDR]);
208 hsMsgInfo->fragmentLength = BSL_ByteToUint24(&data[DTLS_HS_FRAGMENT_LEN_ADDR]);
209
210 if (((hsMsgInfo->fragmentLength + hsMsgInfo->fragmentOffset) > hsMsgInfo->length) ||
211 ((hsMsgInfo->length != 0) && (hsMsgInfo->fragmentLength == 0))) {
212 return ParseErrorProcess(ctx, HITLS_PARSE_INVALID_MSG_LEN, BINLOG_ID15600,
213 logStr, ALERT_DECODE_ERROR);
214 }
215
216 return CheckHsMsgLen(ctx, hsMsgInfo);
217 }
218
219 #endif /* HITLS_TLS_PROTO_DTLS12 */
220 #ifdef HITLS_TLS_PROTO_TLS
TlsParseHsMsgHeader(TLS_Ctx * ctx,const uint8_t * data,uint32_t len,HS_MsgInfo * hsMsgInfo)221 static int32_t TlsParseHsMsgHeader(TLS_Ctx *ctx, const uint8_t *data, uint32_t len, HS_MsgInfo *hsMsgInfo)
222 {
223 const char *logStr = BINGLOG_STR("parse TLS handshake msg header failed.");
224 if (len < HS_MSG_HEADER_SIZE) {
225 return ParseErrorProcess(ctx, HITLS_PARSE_INVALID_MSG_LEN, BINLOG_ID15601,
226 logStr, ALERT_DECODE_ERROR);
227 }
228
229 hsMsgInfo->type = data[0];
230
231 if (hsMsgInfo->type >= HS_MSG_TYPE_END) {
232 return ParseErrorProcess(ctx, HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG, BINLOG_ID16160,
233 logStr, ALERT_UNEXPECTED_MESSAGE);
234 }
235
236 int32_t ret = CheckHsMsgType(ctx, hsMsgInfo->type);
237 if (ret != HITLS_SUCCESS) {
238 return ret;
239 }
240
241 hsMsgInfo->length = BSL_ByteToUint24(data + sizeof(uint8_t)); /* Parse handshake body length */
242 hsMsgInfo->sequence = 0; /* TLS does not have this field */
243 hsMsgInfo->fragmentOffset = 0; /* TLS does not have this field */
244 hsMsgInfo->fragmentLength = 0; /* TLS does not have this field */
245
246 return CheckHsMsgLen(ctx, hsMsgInfo);
247 }
248 #endif /* HITLS_TLS_PROTO_TLS */
249 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
ParseHandShakeMsg(TLS_Ctx * ctx,const uint8_t * data,uint32_t len,HS_Msg * hsMsg)250 static int32_t ParseHandShakeMsg(TLS_Ctx *ctx, const uint8_t *data, uint32_t len, HS_Msg *hsMsg)
251 {
252 switch (hsMsg->type) {
253 case CLIENT_HELLO:
254 return ParseClientHello(ctx, data, len, hsMsg);
255 case SERVER_HELLO:
256 return ParseServerHello(ctx, data, len, hsMsg);
257 case HELLO_VERIFY_REQUEST:
258 return ParseHelloVerifyRequest(ctx, data, len, hsMsg);
259 case CERTIFICATE:
260 return ParseCertificate(ctx, data, len, hsMsg);
261 case SERVER_KEY_EXCHANGE:
262 return ParseServerKeyExchange(ctx, data, len, hsMsg);
263 case CERTIFICATE_REQUEST:
264 return ParseCertificateRequest(ctx, data, len, hsMsg);
265 case CLIENT_KEY_EXCHANGE:
266 return ParseClientKeyExchange(ctx, data, len, hsMsg);
267 case CERTIFICATE_VERIFY:
268 return ParseCertificateVerify(ctx, data, len, hsMsg);
269 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
270 case NEW_SESSION_TICKET:
271 return ParseNewSessionTicket(ctx, data, len, hsMsg);
272 #endif /* HITLS_TLS_FEATURE_SESSION_TICKET */
273 case FINISHED:
274 return ParseFinished(ctx, data, len, hsMsg);
275 case HELLO_REQUEST:
276 case SERVER_HELLO_DONE:
277 if (len != 0u) {
278 BSL_LOG_BINLOG_VARLEN(BINLOG_ID15603, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
279 "msg %s", HS_GetMsgTypeStr(hsMsg->type));
280 return ParseErrorProcess(ctx, HITLS_PARSE_INVALID_MSG_LEN, BINLOG_ID15611,
281 BINGLOG_STR("length is not zero"), ALERT_ILLEGAL_PARAMETER);
282 }
283 return HITLS_SUCCESS;
284 default:
285 break;
286 }
287
288 BSL_ERR_PUSH_ERROR(HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG);
289 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15604, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
290 "dtls parse handshake msg error, unsupport type[%d].", hsMsg->type, 0, 0, 0);
291 ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_UNEXPECTED_MESSAGE);
292 return HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG;
293 }
294 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
295 #ifdef HITLS_TLS_PROTO_TLS13
Tls13ParseHandShakeMsg(TLS_Ctx * ctx,const uint8_t * hsBodyData,uint32_t hsBodyLen,HS_Msg * hsMsg)296 int32_t Tls13ParseHandShakeMsg(TLS_Ctx *ctx, const uint8_t *hsBodyData, uint32_t hsBodyLen, HS_Msg *hsMsg)
297 {
298 switch (hsMsg->type) {
299 #ifdef HITLS_TLS_HOST_SERVER
300 case CLIENT_HELLO:
301 return ParseClientHello(ctx, hsBodyData, hsBodyLen, hsMsg);
302 #endif /* HITLS_TLS_HOST_SERVER */
303 #ifdef HITLS_TLS_HOST_CLIENT
304 case SERVER_HELLO:
305 return ParseServerHello(ctx, hsBodyData, hsBodyLen, hsMsg);
306 case ENCRYPTED_EXTENSIONS:
307 return ParseEncryptedExtensions(ctx, hsBodyData, hsBodyLen, hsMsg);
308 case CERTIFICATE_REQUEST:
309 return Tls13ParseCertificateRequest(ctx, hsBodyData, hsBodyLen, hsMsg);
310 case NEW_SESSION_TICKET:
311 return ParseNewSessionTicket(ctx, hsBodyData, hsBodyLen, hsMsg);
312 #endif /* HITLS_TLS_HOST_CLIENT */
313 case CERTIFICATE:
314 return Tls13ParseCertificate(ctx, hsBodyData, hsBodyLen, hsMsg);
315 case CERTIFICATE_VERIFY:
316 return ParseCertificateVerify(ctx, hsBodyData, hsBodyLen, hsMsg);
317 case FINISHED:
318 return ParseFinished(ctx, hsBodyData, hsBodyLen, hsMsg);
319 #ifdef HITLS_TLS_FEATURE_KEY_UPDATE
320 case KEY_UPDATE:
321 return ParseKeyUpdate(ctx, hsBodyData, hsBodyLen, hsMsg);
322 #endif /* HITLS_TLS_FEATURE_KEY_UPDATE */
323 case HELLO_REQUEST:
324 if (hsBodyLen != 0u) {
325 return ParseErrorProcess(ctx, HITLS_PARSE_INVALID_MSG_LEN, BINLOG_ID15611,
326 BINGLOG_STR("hello request length is not zero"), ALERT_DECODE_ERROR);
327 }
328 return HITLS_SUCCESS;
329 default:
330 break;
331 }
332
333 BSL_ERR_PUSH_ERROR(HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG);
334 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15605, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
335 "recv unsupport handshake msg type[%d].", hsMsg->type, 0, 0, 0);
336 return HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG;
337 }
338 #endif /* HITLS_TLS_PROTO_TLS13 */
HS_ParseMsgHeader(TLS_Ctx * ctx,const uint8_t * data,uint32_t len,HS_MsgInfo * hsMsgInfo)339 int32_t HS_ParseMsgHeader(TLS_Ctx *ctx, const uint8_t *data, uint32_t len, HS_MsgInfo *hsMsgInfo)
340 {
341 if ((ctx == NULL) || (ctx->method.sendAlert == NULL) || (data == NULL) || (hsMsgInfo == NULL)) {
342 return ParseErrorProcess(ctx, HITLS_INTERNAL_EXCEPTION, BINLOG_ID15606,
343 BINGLOG_STR("null input parameter"), ALERT_UNKNOWN);
344 }
345
346 uint32_t version = HS_GetVersion(ctx);
347
348 switch (version) {
349 #ifdef HITLS_TLS_PROTO_TLS
350 case HITLS_VERSION_TLS12:
351 case HITLS_VERSION_TLS13:
352 #ifdef HITLS_TLS_PROTO_TLCP11
353 case HITLS_VERSION_TLCP_DTLCP11:
354 #if defined(HITLS_TLS_PROTO_DTLCP11)
355 if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask)) {
356 return DtlsParseHsMsgHeader(ctx, data, len, hsMsgInfo);
357 }
358 #endif
359 #endif
360 return TlsParseHsMsgHeader(ctx, data, len, hsMsgInfo);
361 #endif /* HITLS_TLS_PROTO_TLS */
362 #ifdef HITLS_TLS_PROTO_DTLS12
363 case HITLS_VERSION_DTLS12:
364 return DtlsParseHsMsgHeader(ctx, data, len, hsMsgInfo);
365 #endif
366 default:
367 break;
368 }
369
370 BSL_ERR_PUSH_ERROR(HITLS_PARSE_UNSUPPORT_VERSION);
371 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15607, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
372 "unsupport msg header version[0x%x].", version, 0, 0, 0);
373 return HITLS_PARSE_UNSUPPORT_VERSION;
374 }
375
HS_ParseMsg(TLS_Ctx * ctx,const HS_MsgInfo * hsMsgInfo,HS_Msg * hsMsg)376 int32_t HS_ParseMsg(TLS_Ctx *ctx, const HS_MsgInfo *hsMsgInfo, HS_Msg *hsMsg)
377 {
378 if ((ctx == NULL) || (ctx->method.sendAlert == NULL) || (hsMsgInfo == NULL) || (hsMsgInfo->rawMsg == NULL) ||
379 (hsMsg == NULL)) {
380 BSL_ERR_PUSH_ERROR(HITLS_INTERNAL_EXCEPTION);
381 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15608, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
382 "the input parameter pointer is null.", 0, 0, 0, 0);
383 return HITLS_INTERNAL_EXCEPTION;
384 }
385 hsMsg->type = hsMsgInfo->type;
386 hsMsg->length = hsMsgInfo->length;
387 hsMsg->sequence = hsMsgInfo->sequence;
388 hsMsg->fragmentOffset = hsMsgInfo->fragmentOffset;
389 hsMsg->fragmentLength = hsMsgInfo->fragmentLength;
390
391 uint32_t version = HS_GetVersion(ctx);
392
393 switch (version) {
394 #ifdef HITLS_TLS_PROTO_TLS_BASIC
395 case HITLS_VERSION_TLS12:
396 #ifdef HITLS_TLS_PROTO_TLCP11
397 case HITLS_VERSION_TLCP_DTLCP11:
398 #if defined(HITLS_TLS_PROTO_DTLCP11)
399 if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask)) {
400 return ParseHandShakeMsg(ctx, &hsMsgInfo->rawMsg[DTLS_HS_MSG_HEADER_SIZE], hsMsgInfo->length, hsMsg);
401 }
402 #endif
403 #endif
404 return ParseHandShakeMsg(ctx, &hsMsgInfo->rawMsg[HS_MSG_HEADER_SIZE], hsMsgInfo->length, hsMsg);
405 #endif /* HITLS_TLS_PROTO_TLS_BASIC */
406 #ifdef HITLS_TLS_PROTO_TLS13
407 case HITLS_VERSION_TLS13:
408 return Tls13ParseHandShakeMsg(ctx, &hsMsgInfo->rawMsg[HS_MSG_HEADER_SIZE], hsMsgInfo->length, hsMsg);
409 #endif /* HITLS_TLS_PROTO_TLS13 */
410 #ifdef HITLS_TLS_PROTO_DTLS12
411 case HITLS_VERSION_DTLS12:
412 return ParseHandShakeMsg(ctx, &hsMsgInfo->rawMsg[DTLS_HS_MSG_HEADER_SIZE], hsMsgInfo->length, hsMsg);
413 #endif
414 default:
415 break;
416 }
417 BSL_ERR_PUSH_ERROR(HITLS_PARSE_UNSUPPORT_VERSION);
418 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15609, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
419 "unsupport handshake msg version[0x%x].", version, 0, 0, 0);
420 return HITLS_PARSE_UNSUPPORT_VERSION;
421 }
422
HS_CleanMsg(HS_Msg * hsMsg)423 void HS_CleanMsg(HS_Msg *hsMsg)
424 {
425 if (hsMsg == NULL) {
426 return;
427 }
428
429 switch (hsMsg->type) {
430 #ifdef HITLS_TLS_HOST_SERVER
431 case CLIENT_HELLO:
432 return CleanClientHello(&hsMsg->body.clientHello);
433 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
434 case CLIENT_KEY_EXCHANGE:
435 return CleanClientKeyExchange(&hsMsg->body.clientKeyExchange);
436 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
437 #endif /* HITLS_TLS_HOST_SERVER */
438 #ifdef HITLS_TLS_HOST_CLIENT
439 case SERVER_HELLO:
440 return CleanServerHello(&hsMsg->body.serverHello);
441 case HELLO_VERIFY_REQUEST:
442 return CleanHelloVerifyRequest(&hsMsg->body.helloVerifyReq);
443 case CERTIFICATE_REQUEST:
444 return CleanCertificateRequest(&hsMsg->body.certificateReq);
445 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
446 case SERVER_KEY_EXCHANGE:
447 return CleanServerKeyExchange(&hsMsg->body.serverKeyExchange);
448 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
449 #ifdef HITLS_TLS_PROTO_TLS13
450 case ENCRYPTED_EXTENSIONS:
451 return CleanEncryptedExtensions(&hsMsg->body.encryptedExtensions);
452 #endif /* HITLS_TLS_PROTO_TLS13 */
453 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
454 case NEW_SESSION_TICKET:
455 return CleanNewSessionTicket(&hsMsg->body.newSessionTicket);
456 #endif /* HITLS_TLS_FEATURE_SESSION_TICKET */
457 #endif /* HITLS_TLS_HOST_CLIENT */
458 case CERTIFICATE:
459 return CleanCertificate(&hsMsg->body.certificate);
460 case CERTIFICATE_VERIFY:
461 return CleanCertificateVerify(&hsMsg->body.certificateVerify);
462 case FINISHED:
463 return CleanFinished(&hsMsg->body.finished);
464 case KEY_UPDATE:
465 case HELLO_REQUEST:
466 case SERVER_HELLO_DONE:
467 return;
468 default:
469 break;
470 }
471
472 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15610, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
473 "clean unsupport handshake msg type[%d].", hsMsg->type, 0, 0, 0);
474 return;
475 }
476