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 PARSE_MSG_H 17 #define PARSE_MSG_H 18 19 #include <stdint.h> 20 #include "tls.h" 21 #include "hs_msg.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 /** 28 * @brief Parse client Hello message 29 * 30 * @param ctx [IN] TLS context 31 * @param data [IN] Message buffer 32 * @param len [IN] Message buffer length 33 * @param hsMsg [OUT] Parsed message structure 34 * 35 * @retval HITLS_SUCCESS 36 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 37 * @retval HITLS_MEMCPY_FAIL Memory copy failed 38 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 39 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG Extension duplicated 40 */ 41 int32_t ParseClientHello(TLS_Ctx *ctx, const uint8_t *data, uint32_t len, HS_Msg *hsMsg); 42 43 /** 44 * @brief Parse Server Hello message 45 * 46 * @param ctx [IN] TLS context 47 * @param buf [IN] Message buffer 48 * @param bufLen [IN] Maximum message length 49 * @param hsMsg [OUT] Message structure 50 * 51 * @retval HITLS_SUCCESS 52 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 53 * @retval HITLS_MEMCPY_FAIL Memory copy failed 54 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 55 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG Extension duplicated 56 */ 57 int32_t ParseServerHello(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 58 59 /** 60 * @brief Parse Hello Verify Request message 61 * 62 * @param ctx [IN] TLS context 63 * @param buf [IN] Message buffer 64 * @param bufLen [IN] Maximum message length 65 * @param hsMsg [OUT] Message structure 66 * 67 * @retval HITLS_SUCCESS 68 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 69 * @retval HITLS_MEMCPY_FAIL Memory copy failed 70 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 71 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG Extension duplicated 72 */ 73 int32_t ParseHelloVerifyRequest(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 74 /** 75 * @brief Parse TLS 1.3 EncryptedExtensions message 76 * 77 * @param ctx [IN] TLS context 78 * @param buf [IN] Message buffer 79 * @param bufLen [IN] Maximum message length 80 * @param hsMsg [OUT] Message structure 81 * 82 * @return HITLS_SUCCESS 83 * HITLS_INVALID_PARAMETERS The input parameter is a null pointer 84 * HITLS_ALERT_FATAL Message error 85 * HITLS_MEMALLOC_FAIL Memory allocated failed 86 */ 87 int32_t ParseEncryptedExtensions(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 88 89 /** 90 * @brief Parse certificate message 91 * 92 * @param ctx [IN] TLS context 93 * @param buf [IN] Message buffer 94 * @param bufLen [IN] Maximum message length 95 * @param hsMsg [OUT] Message structure 96 * 97 * @retval HITLS_SUCCESS 98 * @retval HITLSPARSE_CERT_ERR Failed to parse the certificate 99 * @retval HITLSPARSE_INVALID_MSG_LEN The message length is incorrect 100 */ 101 int32_t ParseCertificate(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 102 /** 103 * @brief Parse TLS 1.3 certificate message 104 * 105 * @param ctx [IN] TLS context 106 * @param buf [IN] Message buffer 107 * @param bufLen [IN] Maximum message length 108 * @param hsMsg [OUT] Message structure 109 * 110 * @retval HITLS_SUCCESS 111 * @retval HITLSPARSE_CERT_ERR Failed to parse the certificate 112 * @retval HITLSPARSE_INVALID_MSG_LEN The message length is incorrect 113 */ 114 int32_t Tls13ParseCertificate(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 115 /** 116 * @brief Parse Server Key Exchange message 117 * 118 * @param ctx [IN] TLS context 119 * @param data [IN] Message buffer 120 * @param len [IN] Message buffer length 121 * @param hsMsg [OUT] Parsed message structure 122 * 123 * @retval HITLS_SUCCESS 124 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 125 * @retval HITLS_PARSE_UNSUPPORT_KX_CURVE_TYPE Unsupported ECC curve type 126 * @retval HITLS_PARSE_ECDH_PUBKEY_ERR Failed to parse the ECDH public key 127 * @retval HITLS_PARSE_ECDH_SIGN_ERR Failed to parse the ECDH signature 128 * @retval HITLS_PARSE_UNSUPPORT_KX_ALG Unsupported key exchange algorithm 129 */ 130 int32_t ParseServerKeyExchange(TLS_Ctx *ctx, const uint8_t *data, uint32_t len, HS_Msg *hsMsg); 131 132 /** 133 * @brief Parse certificate request message, which is applicable to TLS1.2/DTLS/TLS1.3 protocols 134 * 135 * @param ctx [IN] TLS context 136 * @param buf [IN] Message buffer 137 * @param bufLen [IN] Maximum message length 138 * @param hsMsg [OUT] Message structure 139 * 140 * @retval HITLS_SUCCESS 141 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 142 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 143 */ 144 int32_t ParseCertificateRequest(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 145 /** 146 * @brief Parse TLS1.3 certificate request message 147 * 148 * @param ctx [IN] TLS context 149 * @param buf [IN] Message buffer 150 * @param bufLen [IN] Maximum message length 151 * @param hsMsg [OUT] Message structure 152 * 153 * @retval HITLS_SUCCESS 154 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 155 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 156 */ 157 int32_t Tls13ParseCertificateRequest(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 158 /** 159 * @brief Parse Client Key Exchange message 160 * 161 * @param ctx [IN] TLS context 162 * @param data [IN] Message buffer 163 * @param len [IN] Message buffer length 164 * @param hsMsg [OUT] Parsed Message structure 165 * 166 * @retval HITLS_SUCCESS 167 * @retval HITLS_MEMCPY_FAIL Memory copy failed 168 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 169 */ 170 int32_t ParseClientKeyExchange(TLS_Ctx *ctx, const uint8_t *data, uint32_t len, HS_Msg *hsMsg); 171 172 /** 173 * @brief Parse Certificate Verify message 174 * 175 * @param ctx [IN] TLS context 176 * @param buf [IN] Message buffer 177 * @param bufLen [IN] Maximum message length 178 * @param hsMsg [OUT] Message structure 179 * 180 * @retval HITLS_SUCCESS 181 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 182 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 183 */ 184 int32_t ParseCertificateVerify(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 185 186 /** 187 * @brief Parse Finished message 188 * 189 * @param ctx [IN] TLS context 190 * @param hsMsg [OUT] Message structure 191 * @param buf [IN] Message buffer 192 * @param bufLen [IN] Maximum message length 193 * 194 * @retval HITLS_SUCCESS 195 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 196 * @retval HITLS_MEMCPY_FAIL Memory copy failed 197 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 198 */ 199 int32_t ParseFinished(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 200 /** 201 * @brief Parse KeyUpdate message 202 * 203 * @param ctx [IN] TLS context 204 * @param hsMsg [OUT] Message structure 205 * @param buf [IN] Message buffer 206 * @param bufLen [IN] Maximum message length 207 * 208 * @retval HITLS_SUCCESS 209 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 210 * @retval HITLS_MEMCPY_FAIL Memory copy failed 211 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 212 */ 213 int32_t ParseKeyUpdate(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 214 215 /** 216 * @brief Parse new sessionticket message 217 * 218 * @param ctx [IN] TLS context 219 * @param hsMsg [OUT] Message structure 220 * @param buf [IN] Message buffer 221 * @param bufLen [IN] Maximum message length 222 * 223 * @retval HITLS_SUCCESS 224 * @retval HITLS_MEMALLOC_FAIL Memory allocated failed 225 * @retval HITLS_MEMCPY_FAIL Memory copy failed 226 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 227 */ 228 int32_t ParseNewSessionTicket(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, HS_Msg *hsMsg); 229 230 /** 231 * @brief Free the memory allocated in the Client Hello message structure 232 * 233 * @param msg [IN] Message structure 234 */ 235 void CleanClientHello(ClientHelloMsg *msg); 236 237 /** 238 * @brief Free the memory allocated in the Server Hello message structure 239 * 240 * @param msg [IN] Message structure 241 */ 242 void CleanServerHello(ServerHelloMsg *msg); 243 244 /** 245 * @brief Free the memory allocated in the Hello Verify Request message structure 246 * 247 * @param msg [IN] Message structure 248 */ 249 void CleanHelloVerifyRequest(HelloVerifyRequestMsg *msg); 250 /** 251 * @brief Free the memory allocated in the EncryptedExtensions message structure 252 * 253 * @param msg [IN] Message structure 254 */ 255 void CleanEncryptedExtensions(EncryptedExtensions *msg); 256 /** 257 * @brief Free the memory allocated in the certificate message structure 258 * 259 * @param msg [IN] Message structure 260 */ 261 void CleanCertificate(CertificateMsg *msg); 262 263 /** 264 * @brief Free the memory allocated in the ServerKeyExchangeMsg message structure 265 * 266 * @param msg [IN] Message structure 267 */ 268 void CleanServerKeyExchange(ServerKeyExchangeMsg *msg); 269 270 /** 271 * @brief Free the memory allocated in the Certificate Request message structure 272 * 273 * @param msg [IN] Message structure 274 */ 275 void CleanCertificateRequest(CertificateRequestMsg *msg); 276 277 /** 278 * @brief Free the memory allocated in the Client KeyExchange message structure 279 * 280 * @param msg [IN] Message structure 281 */ 282 void CleanClientKeyExchange(ClientKeyExchangeMsg *msg); 283 284 /** 285 * @brief Free the memory allocated in the Certificate Verify message structure 286 * 287 * @param msg [IN] Message structure 288 */ 289 void CleanCertificateVerify(CertificateVerifyMsg *msg); 290 291 /** 292 * @brief Free the memory allocated in the NewSessionTicket message structure 293 * 294 * @param msg [IN] Message structure 295 */ 296 void CleanNewSessionTicket(NewSessionTicketMsg *msg); 297 298 /** 299 * @brief Free the memory allocated in the Finished message structure 300 * 301 * @param msg [IN] Message structure 302 */ 303 void CleanFinished(FinishedMsg *msg); 304 305 #ifdef __cplusplus 306 } 307 #endif /* end __cplusplus */ 308 309 #endif /* end PARSE_MSG_H */