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 #ifndef CUSTOM_EXTENSIONS_H 16 #define CUSTOM_EXTENSIONS_H 17 18 #include "hitls_build.h" 19 #include "hitls.h" 20 #include "hitls_custom_extensions.h" 21 22 // Define CustomExt_Method structure 23 typedef struct { 24 uint16_t extType; 25 uint32_t context; 26 HITLS_AddCustomExtCallback addCb; 27 HITLS_FreeCustomExtCallback freeCb; 28 void *addArg; 29 HITLS_ParseCustomExtCallback parseCb; 30 void *parseArg; 31 } CustomExt_Method; 32 33 // Define CustomExt_Methods structure 34 typedef struct CustomExt_Methods { 35 CustomExt_Method *meths; 36 uint32_t methsCount; 37 } CustomExt_Methods; 38 39 40 /** 41 * @brief Determines if packing custom extensions is needed for a given context. 42 * 43 * This function checks whether there are any custom extensions that need to be packed 44 * based on the provided context. It iterates through the list of custom extension methods 45 * and evaluates if any of them match the specified context. 46 * 47 * @param exts [IN] Pointer to the CustomExt_Methods structure containing extension methods 48 * @param context [IN] The context to check against the custom extensions 49 * @retval true if there are custom extensions that need to be packed for the given context 50 * @retval false otherwise 51 */ 52 bool IsPackNeedCustomExtensions(CustomExt_Methods *exts, uint32_t context); 53 54 55 /** 56 * @brief Determines if parsing custom extensions is needed for a given extension type and context. 57 * 58 * This function checks whether there are any custom extensions that need to be parsed 59 * based on the provided extension type and context. It iterates through the list of custom 60 * extension methods and evaluates if any of them match the specified extension type and context. 61 * 62 * @param exts [IN] Pointer to the CustomExt_Methods structure containing extension methods 63 * @param extType [IN] The extension type to check against the custom extensions 64 * @param context [IN] The context to check against the custom extensions 65 * @retval true if there are custom extensions that need to be parsed for the given extension type and context 66 * @retval false otherwise 67 */ 68 bool IsParseNeedCustomExtensions(CustomExt_Methods *exts, uint16_t extType, uint32_t context); 69 70 /** 71 * @brief Packs custom extensions into the provided buffer for a given context. 72 * 73 * This function iterates through the list of custom extension methods associated with the TLS context 74 * and packs the relevant custom extensions into the provided buffer. It checks each extension method 75 * to determine if it should be included based on the specified context. If an extension is applicable, 76 * it uses the associated add callback to pack the extension data into the buffer. 77 * 78 * @param ctx [IN] Pointer to the TLS context containing custom extension methods 79 * @param buf [OUT] Buffer where the packed custom extensions will be stored 80 * @param bufLen [IN] Length of the buffer 81 * @param len [OUT] Pointer to a variable where the total length of packed extensions will be stored 82 * @param context [IN] The context to check against the custom extensions 83 * @param cert [IN] Pointer to the HITLS_X509_Cert structure representing certificate information 84 * @param certIndex [IN] Certificate index indicating its position in the certificate chain 85 * @retval HITLS_SUCCESS if the custom extensions are successfully packed 86 * @retval An error code if packing fails, see hitls_error.h for details 87 */ 88 int32_t PackCustomExtensions(const struct TlsCtx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *len, uint32_t context, HITLS_X509_Cert *cert, uint32_t certIndex); 89 90 91 /** 92 * @brief Frees the custom extension methods in the HITLS configuration. 93 * 94 * This function frees the custom extension methods in the HITLS configuration. 95 * 96 * @param exts [IN] Pointer to the CustomExt_Methods structure containing extension methods 97 */ 98 void FreeCustomExtensions(CustomExt_Methods *exts); 99 100 /** 101 * @brief Duplicates the custom extension methods in the HITLS configuration. 102 * 103 * This function duplicates the custom extension methods in the HITLS configuration. 104 * 105 * @param exts [IN] Pointer to the CustomExt_Methods structure containing extension methods 106 * @retval Pointer to the duplicated CustomExt_Methods structure 107 */ 108 CustomExt_Methods *DupCustomExtensions(CustomExt_Methods *exts); 109 110 /** 111 * @brief Parses custom extensions from the provided buffer for a given extension type and context. 112 * 113 * This function iterates through the list of custom extension methods associated with the TLS context 114 * and parses the relevant custom extensions from the provided buffer. It checks each extension method 115 * to determine if it should be parsed based on the specified extension type and context. If an extension 116 * is applicable, it uses the associated parse callback to interpret the extension data. 117 * 118 * @param ctx [IN] Pointer to the TLS context containing custom extension methods 119 * @param buf [IN] Buffer containing the custom extensions to be parsed 120 * @param extType [IN] The extension type to check against the custom extensions 121 * @param extLen [IN] Length of the extension data in the buffer 122 * @param context [IN] The context to check against the custom extensions 123 * @param cert [IN] Pointer to the HITLS_X509_Cert structure representing certificate information 124 * @param certIndex [IN] Certificate index indicating its position in the certificate chain 125 * @retval HITLS_SUCCESS if the custom extensions are successfully parsed 126 * @retval An error code if parsing fails, see hitls_error.h for details 127 */ 128 int32_t ParseCustomExtensions(const struct TlsCtx *ctx, const uint8_t *buf, uint16_t extType, uint32_t extLen, 129 uint32_t context, HITLS_X509_Cert *cert, uint32_t certIndex); 130 131 #endif // CUSTOM_EXTENSIONS_H 132