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_EXTENSIONS_H 17 #define PARSE_EXTENSIONS_H 18 19 #include <stdint.h> 20 #include "tls.h" 21 #include "hs_msg.h" 22 #include "parse_common.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** 29 * @brief Parse Client Hello extension 30 * 31 * @attention The input parameter pointer can't be NULL 32 * If parsing fails, the invoker releases the allocated memory 33 * 34 * @param ctx [IN] TLS context 35 * @param buf [IN] Message buffer, starting from the extension type 36 * @param bufLen [IN] Message length 37 * @param msg [OUT] Parsed message 38 * 39 * @retval HITLS_SUCCESS 40 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 41 * @retval HITLS_MEMALLOC_FAIL Memory allocation failed 42 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG Extension duplicated 43 */ 44 int32_t ParseClientExtension(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, ClientHelloMsg *msg); 45 46 /** 47 * @brief Release the buffer in the Client Hello extension structure 48 * 49 * @param msg [IN] Message structure 50 */ 51 void CleanClientHelloExtension(ClientHelloMsg *msg); 52 53 /** 54 * @brief Parse server hello extension 55 * 56 * @attention The input parameter pointer can't be NULL 57 * If the parsing fails, the invoker releases the allocated memory 58 * 59 * @param ctx [IN] TLS context 60 * @param buf [IN] Message buffer, starting from the extension type 61 * @param bufLen [IN] Message length 62 * @param msg [OUT] Parsed message 63 * 64 * @retval HITLS_SUCCESS 65 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 66 * @retval HITLS_MEMALLOC_FAIL Memory allocation failed 67 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG Extension duplicated 68 * @retval HITLS_PARSE_UNSUPPORTED_EXTENSION Unsupported extension 69 */ 70 int32_t ParseServerExtension(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, ServerHelloMsg *msg); 71 /** 72 * @brief Parse extension type and length 73 * 74 * @param ctx [IN] TLS context 75 * @param buf [IN] Message buffer, starting from the extension type 76 * @param bufLen [IN] Message length 77 * @param extMsgType [OUT] Extension type 78 * @param extMsgLen [OUT] Extension length 79 * 80 * @retval HITLS_SUCCESS 81 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 82 * @retval HITLS_MEMALLOC_FAIL Memory allocation failed 83 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG Extension duplicated 84 */ 85 int32_t ParseExHeader(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, uint16_t *extMsgType, uint32_t *extMsgLen); 86 /** 87 * @brief Release the buffer in the Server Hello extension structure 88 * 89 * @param msg [IN] Message structure 90 */ 91 void CleanServerHelloExtension(ServerHelloMsg *msg); 92 /** 93 * @brief Parse empty extension 94 * 95 * @param ctx [IN] TLS context 96 * @param extMsgType [IN] Extension type 97 * @param extMsgLen [IN] Extension length 98 * @param haveExtension [OUT] Indicates whether there are extensions 99 * 100 * @retval HITLS_SUCCESS 101 * @retval HITLS_PARSE_INVALID_MSG_LEN The message length is incorrect 102 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG Extension duplicated 103 */ 104 int32_t ParseEmptyExtension(TLS_Ctx *ctx, uint16_t extMsgType, uint32_t extMsgLen, bool *haveExtension); 105 106 int32_t ParseExCookie(const uint8_t *buf, uint32_t bufLen, uint8_t **cookie, uint16_t *cookieLen); 107 108 int32_t ParseSecRenegoInfo(TLS_Ctx *ctx, const uint8_t *buf, uint32_t bufLen, uint8_t **secRenegoInfo, 109 uint8_t *secRenegoInfoSize); 110 111 int32_t ParseServerSelectedAlpnProtocol( 112 ParsePacket *pkt, bool *haveSelectedAlpn, uint8_t **alpnSelected, uint16_t *alpnSelectedSize); 113 114 /** 115 * @brief Error process in duplicated extension 116 * 117 * @param ctx [IN] TLS context 118 * @param logId [IN] binlogid 119 * @param format [IN] Message for log function 120 121 * @retval HITLS_PARSE_DUPLICATE_EXTENDED_MSG 122 */ 123 int32_t ParseDupExtProcess(TLS_Ctx *ctx, uint32_t logId, const void *format); 124 125 /** 126 * @brief Parse extension length error 127 * 128 * @param ctx [IN] TLS context 129 * @param logId [IN] binlogid 130 * @param format [IN] Message for log function 131 132 * @retval HITLS_PARSE_INVALID_MSG_LEN 133 */ 134 int32_t ParseErrorExtLengthProcess(TLS_Ctx *ctx, uint32_t logId, const void *format); 135 136 bool GetExtensionFlagValue(TLS_Ctx *ctx, uint32_t hsExTypeId); 137 138 int32_t CheckForDuplicateExtension(uint64_t extensionTypeMask, uint32_t extensionId, TLS_Ctx *ctx); 139 #ifdef __cplusplus 140 } 141 #endif /* end __cplusplus */ 142 143 #endif /* end PARSE_EXTENSIONS_H */ 144