• 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 #ifndef PARSER_COMMON_H
17 #define PARSER_COMMON_H
18 
19 #include <stdint.h>
20 #include "tls.h"
21 #include "hs_msg.h"
22 #include "cert_method.h"
23 #include "cert_mgr_ctx.h"
24 #include "security.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 typedef struct {
31     TLS_Ctx *ctx;
32     const uint8_t *buf;
33     uint32_t bufLen;
34     uint32_t *bufOffset;
35 } ParsePacket;
36 
37 /**
38  * @brief   Parse the version of the message
39  *
40  * @param   pkt [IN] Context for parsing
41  * @param   version [OUT] Parsed version
42  *
43  * @retval  HITLS_SUCCESS
44  * @retval  HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect
45  */
46 int32_t ParseVersion(ParsePacket *pkt, uint16_t *version);
47 
48 /**
49  * @brief   Parse random number in message
50  *
51  * @param   pkt [IN] Context for parsing
52  * @param   random [OUT]  Parsed random number
53  * @param   randomSize [IN] Random number length
54  *
55  * @retval  HITLS_SUCCESS
56  * @retval  HITLS_MEMCPY_FAIL Memory Copy Failed
57  * @retval  HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect
58  */
59 int32_t ParseRandom(ParsePacket *pkt, uint8_t *random, uint32_t randomSize);
60 
61 /**
62  * @brief   Parse SessionId in message
63  *
64  * @param   pkt [IN] Context for parsing
65  * @param   id [OUT] Parsed session ID
66  * @param   idSize [OUT] Parsed session ID length
67  *
68  * @retval  HITLS_SUCCESS
69  * @retval  HITLS_MEMALLOC_FAIL Memory allocation failed
70  * @retval  HITLS_MEMCPY_FAIL Memory Copy Failed
71  * @retval  HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect
72  */
73 int32_t ParseSessionId(ParsePacket *pkt, uint8_t *idSize, uint8_t **id);
74 
75 /**
76  * @brief   Parse Cookie in message
77  *
78  * @param   pkt [IN] Context for parsing
79  * @param   cookie [OUT] Parsed cookie
80  * @param   cookieLen [OUT] Parsed cookie length
81  *
82  * @retval  HITLS_SUCCESS
83  * @retval  HITLS_MEMALLOC_FAIL Memory allocation failed
84  * @retval  HITLS_MEMCPY_FAIL Memory Copy Failed
85  * @retval  HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect
86  */
87 int32_t ParseCookie(ParsePacket *pkt, uint8_t *cookieLen, uint8_t **cookie);
88 
89 /**
90  * @brief   Parse TrustCA list in message
91  *
92  * @param   data [IN] TrustCAList message buffer
93  * @param   buf [IN]  TrustCAList message buffer length
94  *
95  * @retval  HITLS_TrustedCAList * Pointer to the CAList header
96  */
97 HITLS_TrustedCAList *ParseDNList(const uint8_t *data, uint32_t len);
98 
99 /**
100  * @brief   Free the buffer of TrustCAList
101  *
102  * @param   listHead [IN] Pointer to the CAList header
103  *
104  * @retval  void
105  */
106 void FreeDNList(HITLS_TrustedCAList *caList);
107 
108 /**
109  * @brief   Parse uint8_t data
110  *
111  * @param   pkt [IN] Context for parsing
112  * @param   object [OUT] Parsed data
113  *
114  * @retval  HITLS_SUCCESS success
115  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
116  */
117 int32_t ParseBytesToUint8(ParsePacket *pkt, uint8_t *object);
118 
119 /**
120  * @brief   Parse uint16_t data
121  *
122  * @param   pkt [IN] Context for parsing
123  * @param   object [OUT] Parsed data
124  *
125  * @retval  HITLS_SUCCESS success
126  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
127  */
128 int32_t ParseBytesToUint16(ParsePacket *pkt, uint16_t *object);
129 
130 /**
131  * @brief   Parse 3 bytes data
132  *
133  * @param   pkt [IN] Context for parsing
134  * @param   object [OUT] Parsed data
135  *
136  * @retval  HITLS_SUCCESS success
137  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
138  */
139 int32_t ParseBytesToUint24(ParsePacket *pkt, uint32_t *object);
140 
141 /**
142  * @brief   Parse uint32_t data
143  *
144  * @param   pkt [IN] Context for parsing
145  * @param   object [OUT] Parsed data
146  *
147  * @retval  HITLS_SUCCESS success
148  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
149  */
150 int32_t ParseBytesToUint32(ParsePacket *pkt, uint32_t *object);
151 
152 /**
153  * @brief   Parse one byte length field, then parse the following content
154  *
155  * @param   pkt [IN] Context for parsing
156  * @param   objectSize [OUT] Parsed one byte data length
157  * @param   object [OUT] Parsed data
158  *
159  * @retval  HITLS_SUCCESS success
160  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
161  */
162 int32_t ParseOneByteLengthField(ParsePacket *pkt, uint8_t *objectSize, uint8_t **object);
163 
164 /**
165  * @brief   Parse two byte length field, then parse the following content
166  *
167  * @param   pkt [IN] Context for parsing
168  * @param   objectSize [OUT] Parsed one byte data length
169  * @param   object [OUT] Parsed data
170  *
171  * @retval  HITLS_SUCCESS success
172  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
173  */
174 int32_t ParseTwoByteLengthField(ParsePacket *pkt, uint16_t *objectSize, uint8_t **object);
175 
176 /**
177  * @brief   Parse data by length
178  *
179  * @param   pkt [IN] Context for parsing
180  * @param   object [OUT] Parsed data, need memory allocation
181  * @param   length [IN] Length of data need be parsed
182  *
183  * @retval  HITLS_SUCCESS success
184  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
185  */
186 int32_t ParseBytesToArray(ParsePacket *pkt, uint8_t **object, uint32_t length);
187 
188 /**
189  * @brief   Parse data by length
190  *
191  * @param   pkt [IN] Context for parsing
192  * @param   object [OUT] Parsed data, do not need memory allocation
193  * @param   length [IN] Length of data need be parsed
194  *
195  * @retval  HITLS_SUCCESS success
196  * @retval  HITLS_PARSE_INVALID_MSG_LEN bufLen is not enough
197  */
198 int32_t ParseCopyBytesToArray(ParsePacket *pkt, uint8_t *object, uint32_t length);
199 
200 /**
201  * @brief   Error processing function in parse module
202  *
203  * @param   ctx [IN] TLS context
204  * @param   err [IN] Error code need to be pushed and returned
205  * @param   logId [IN] binlogid
206  * @param   format [IN] Message for log function
207  * @param   description [IN] Alert description
208 
209  * @retval  error code
210  */
211 int32_t ParseErrorProcess(TLS_Ctx *ctx, int32_t err, uint32_t logId, const void *format, ALERT_Description description);
212 
213 /**
214  * @brief   Check whether the peer certificate matches the peer signature algorithm.
215  *
216  * @param   ctx [IN] TLS context
217  * @param   peerCert [IN] peerCert
218  * @param   signScheme [IN] peer signScheme
219 
220  * @retval  error code
221  */
222 int32_t CheckPeerSignScheme(HITLS_Ctx *ctx, CERT_Pair *peerCert, uint16_t signScheme);
223 
224 #ifdef __cplusplus
225 }
226 #endif /* end __cplusplus */
227 
228 #endif /* end PARSER_COMMON_H */
229