• 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 "securec.h"
17 #include "bsl_bytes.h"
18 #include "bsl_sal.h"
19 #include "hitls_error.h"
20 #include "tls.h"
21 #include "conn_init.h"
22 #include "hs_ctx.h"
23 #include "parse.h"
24 #include "conn_init.h"
25 #include "frame_tls.h"
26 #include "frame_msg.h"
27 #include "parser_frame_msg.h"
28 
SendAlertStake(const TLS_Ctx * ctx,ALERT_Level level,ALERT_Description description)29 void SendAlertStake(const TLS_Ctx *ctx, ALERT_Level level, ALERT_Description description)
30 {
31     (void)ctx;
32     (void)level;
33     (void)description;
34     return;
35 }
36 
ParserRecordHeader(FRAME_Msg * frameMsg,const uint8_t * buffer,uint32_t len,uint32_t * parserLen)37 int32_t ParserRecordHeader(FRAME_Msg *frameMsg, const uint8_t *buffer, uint32_t len, uint32_t *parserLen)
38 {
39     (void)len;
40     uint32_t bufOffset = 0;
41 
42     frameMsg->type = buffer[bufOffset];
43     bufOffset += sizeof(uint8_t);
44 
45     frameMsg->version = BSL_ByteToUint16(&buffer[bufOffset]);
46     bufOffset += sizeof(uint16_t);
47 
48 #ifdef HITLS_TLS_PROTO_DTLS12
49     if (IS_TRANSTYPE_DATAGRAM(frameMsg->transportType)) {
50         frameMsg->epochSeq = BSL_ByteToUint64(&buffer[bufOffset]);
51         bufOffset += sizeof(uint64_t);
52     }
53 #endif
54 
55     frameMsg->bodyLen = BSL_ByteToUint16(&buffer[bufOffset]);
56     bufOffset += sizeof(uint16_t);
57     *parserLen = bufOffset;
58 
59     return HITLS_SUCCESS;
60 }
61 
ParserHandShakeMsg(const FRAME_LinkObj * linkObj,FRAME_Msg * frameMsg,const uint8_t * buffer,uint32_t len,uint32_t * parserLen)62 int32_t ParserHandShakeMsg(const FRAME_LinkObj *linkObj, FRAME_Msg *frameMsg,
63     const uint8_t *buffer, uint32_t len, uint32_t *parserLen)
64 {
65     int32_t ret;
66     HS_MsgInfo hsMsgInfo = {0};
67     HITLS_Ctx *sslCtx = FRAME_GetTlsCtx(linkObj);
68 
69     SendAlertCallback tmpAlertCallback = sslCtx->method.sendAlert;
70     sslCtx->method.sendAlert = SendAlertStake;
71     CONN_Init(sslCtx);
72     ret = HS_ParseMsgHeader(sslCtx, buffer, len, &hsMsgInfo);
73     if (ret != HITLS_SUCCESS) {
74         sslCtx->method.sendAlert = tmpAlertCallback;
75         return ret;
76     }
77     hsMsgInfo.rawMsg = buffer;
78     ret = HS_ParseMsg(sslCtx, &hsMsgInfo, &frameMsg->body.handshakeMsg);
79     if (ret != HITLS_SUCCESS) {
80         sslCtx->method.sendAlert = tmpAlertCallback;
81         return ret;
82     }
83 
84     sslCtx->method.sendAlert = tmpAlertCallback;
85     *parserLen += hsMsgInfo.length;
86     return HITLS_SUCCESS;
87 }
88 
ParserCCSMsg(FRAME_Msg * frameMsg,const uint8_t * buffer,uint32_t len,uint32_t * parserLen)89 int32_t ParserCCSMsg(FRAME_Msg *frameMsg, const uint8_t *buffer, uint32_t len, uint32_t *parserLen)
90 {
91     (void)len;
92     frameMsg->body.ccsMsg.type = buffer[0];
93     *parserLen += sizeof(uint8_t);
94     return HITLS_SUCCESS;
95 }
96 
ParserAlertMsg(FRAME_Msg * frameMsg,const uint8_t * buffer,uint32_t len,uint32_t * parserLen)97 int32_t ParserAlertMsg(FRAME_Msg *frameMsg, const uint8_t *buffer, uint32_t len, uint32_t *parserLen)
98 {
99     (void)len;
100     uint32_t bufOffset = 0;
101     frameMsg->body.alertMsg.level = buffer[bufOffset];
102     bufOffset += sizeof(uint8_t);
103     frameMsg->body.alertMsg.description = buffer[bufOffset];
104     bufOffset += sizeof(uint8_t);
105     *parserLen += bufOffset;
106     return HITLS_SUCCESS;
107 }
108 
ParserAppMsg(FRAME_Msg * frameMsg,const uint8_t * buffer,uint32_t len,uint32_t * parserLen)109 int32_t ParserAppMsg(FRAME_Msg *frameMsg, const uint8_t *buffer, uint32_t len, uint32_t *parserLen)
110 {
111     (void)len;
112     uint32_t bufOffset = 0;
113     uint32_t userDataLen = BSL_ByteToUint32(&buffer[bufOffset]);
114     frameMsg->body.appMsg.len = userDataLen;
115     bufOffset += sizeof(uint32_t);
116     BSL_SAL_FREE(frameMsg->body.appMsg.buffer);
117     frameMsg->body.appMsg.buffer = BSL_SAL_Dump(&buffer[bufOffset], userDataLen);
118     if (frameMsg->body.appMsg.buffer == NULL) {
119         return HITLS_MEMALLOC_FAIL;
120     }
121 
122     bufOffset += userDataLen;
123     *parserLen += bufOffset;
124 
125     return HITLS_SUCCESS;
126 }
127 
ParserRecordBody(const FRAME_LinkObj * linkObj,FRAME_Msg * frameMsg,const uint8_t * buffer,uint32_t len,uint32_t * parserLen)128 int32_t ParserRecordBody(const FRAME_LinkObj *linkObj, FRAME_Msg *frameMsg,
129     const uint8_t *buffer, uint32_t len, uint32_t *parserLen)
130 {
131     switch (frameMsg->type) {
132         case REC_TYPE_HANDSHAKE:
133             return ParserHandShakeMsg(linkObj, frameMsg, buffer, len, parserLen);
134         case REC_TYPE_CHANGE_CIPHER_SPEC:
135             return ParserCCSMsg(frameMsg, buffer, len, parserLen);
136         case REC_TYPE_ALERT:
137             return ParserAlertMsg(frameMsg, buffer, len, parserLen);
138         case REC_TYPE_APP:
139             return ParserAppMsg(frameMsg, buffer, len, parserLen);
140         default:
141             break;
142     }
143 
144     return HITLS_SUCCESS;
145 }
146 
ParserTotalRecord(const FRAME_LinkObj * linkObj,FRAME_Msg * frameMsg,const uint8_t * buffer,uint32_t len,uint32_t * parserLen)147 int32_t ParserTotalRecord(const FRAME_LinkObj *linkObj, FRAME_Msg *frameMsg,
148     const uint8_t *buffer, uint32_t len, uint32_t *parserLen)
149 {
150     int32_t ret = ParserRecordHeader(frameMsg, buffer, len, parserLen);
151     if (ret != HITLS_SUCCESS) {
152         return ret;
153     }
154 
155     return ParserRecordBody(linkObj, frameMsg, &buffer[*parserLen], len - *parserLen, parserLen);
156 }
157 
CleanRecordBody(FRAME_Msg * frameMsg)158 void CleanRecordBody(FRAME_Msg *frameMsg)
159 {
160     if (frameMsg->type == REC_TYPE_HANDSHAKE) {
161         HS_CleanMsg(&frameMsg->body.handshakeMsg);
162     } else if (frameMsg->type == REC_TYPE_APP) {
163         BSL_SAL_FREE(frameMsg->body.appMsg.buffer);
164     }
165     BSL_SAL_FREE(frameMsg->buffer);
166 }
167