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 PACK_EXTENSIONS_H 17 #define PACK_EXTENSIONS_H 18 19 #include <stdint.h> 20 #include "tls.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 /** 27 * Hook function for packing extensions of client and server. 28 */ 29 typedef int32_t (*PACK_EXT_FUNC)(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *len); 30 31 /** 32 * PackExtInfo structure, used to transfer extension information of ClientHello messages 33 */ 34 typedef struct { 35 uint16_t exMsgType; /**< Extension type of message*/ 36 bool needPack; /**< Whether packing is needed */ 37 PACK_EXT_FUNC packFunc; /**< Hook for packing extensions*/ 38 } PackExtInfo; 39 40 typedef void (*GET_EXTSIZE_FUNC)(const TLS_Ctx *ctx, uint32_t *exSize); 41 42 typedef struct { 43 bool needCheck; 44 GET_EXTSIZE_FUNC getSizeFunc; 45 } GetExtFieldSize; 46 47 /** 48 * @brief Pack Client Hello extension 49 * 50 * @param ctx [IN] TLS context 51 * @param buf [OUT] Returned handshake message buffer 52 * @param bufLen [IN] Maximum buffer length of the handshake message 53 * @param len [OUT] Returned message length 54 * 55 * @retval HITLS_SUCCESS 56 * @retval HITLS_PACK_NOT_ENOUGH_BUF_LENGTH The message buffer length is insufficient 57 */ 58 int32_t PackClientExtension(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *len); 59 60 /** 61 * @brief Pack Server Hello extension 62 * 63 * @param ctx [IN] TLS context 64 * @param buf [OUT] Returned handshake message buffer 65 * @param bufLen [IN] Maximum buffer length of the handshake message 66 * @param len [OUT] Returned message length 67 * 68 * @retval HITLS_SUCCESS 69 * @retval HITLS_PACK_NOT_ENOUGH_BUF_LENGTH The message buffer length is insufficient 70 */ 71 int32_t PackServerExtension(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *len); 72 73 /** 74 * @brief Pack an empty extension 75 * 76 * @param ctx [IN] TLS context 77 * @param buf [OUT] Returned handshake message buffer 78 * @param bufLen [IN] Maximum buffer length of the handshake message 79 * @param len [OUT] Returned message length 80 * 81 * @retval HITLS_SUCCESS 82 * @retval HITLS_PACK_NOT_ENOUGH_BUF_LENGTH The message buffer length is insufficient 83 */ 84 int32_t PackEmptyExtension(uint16_t exMsgType, bool needPack, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen); 85 /** 86 * @brief Pack the header of an extension 87 * 88 * @param exMsgType [IN] Extension type 89 * @param exMsgLen [IN] Extension length 90 * @param buf [OUT] Returned handshake message buffer 91 * @param bufLen [IN] Maximum buffer length of the handshake message 92 * 93 * @retval HITLS_SUCCESS 94 * @retval For other error codes, see hitls_error.h 95 */ 96 int32_t PackExtensionHeader(uint16_t exMsgType, uint16_t exMsgLen, uint8_t *buf, uint32_t bufLen); 97 98 int32_t PackServerSelectAlpnProto(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen); 99 #ifdef __cplusplus 100 } 101 #endif /* end __cplusplus */ 102 103 #endif /* end PACK_EXTENSIONS_H */