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 ALTER_H 17 #define ALTER_H 18 19 #include <stdbool.h> 20 #include <stdint.h> 21 #include "hitls_build.h" 22 #include "tls.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 typedef enum { 29 ALERT_FLAG_NO = 0, /* no alert message */ 30 ALERT_FLAG_RECV, /* received the alert message */ 31 ALERT_FLAG_SEND, /* the alert message needs to be sent */ 32 } ALERT_FLAG; 33 34 /** obtain the messages about receiving and sending by Alert */ 35 typedef struct { 36 uint8_t flag; /* send and receive flags, see ALERT_FLAG */ 37 uint8_t level; /* Alert level. For details, see ALERT_Level. */ 38 uint8_t description; /* Alert description. For details, see ALERT_Description. */ 39 uint8_t reverse; /* reserve, 4-byte aligned */ 40 } ALERT_Info; 41 42 /** 43 * @ingroup alert 44 * @brief Alert initialization function 45 * 46 * @param ctx [IN] tls Context 47 * 48 * @retval HITLS_SUCCESS succeeded. 49 * @retval HITLS_INTERNAL_EXCEPTION An unexpected internal error occurs. 50 * @retval HITLS_MEMALLOC_FAIL Failed to apply for memory. 51 */ 52 int32_t ALERT_Init(TLS_Ctx *ctx); 53 54 /** 55 * @ingroup alert 56 * @brief Alert deinitialization function 57 * 58 * @param ctx [IN] tls Context 59 * 60 */ 61 void ALERT_Deinit(TLS_Ctx *ctx); 62 63 /** 64 * @ingroup alert 65 * @brief Check whether there are received or sent alert messages to be processed. 66 * 67 * @attention ctx cannot be empty. 68 * @param ctx [IN] tls Context 69 * 70 * @retval true: The processing is required. 71 * @retval false: No processing is required. 72 */ 73 bool ALERT_GetFlag(const TLS_Ctx *ctx); 74 75 /** 76 * @ingroup alert 77 * @brief Obtain the alert information. 78 * 79 * @attention ctx and info cannot be empty. Ensure that the value is used when Alert_GetFlag is true. 80 * @param ctx [IN] tls Context 81 * @param info [IN] Alert information record 82 */ 83 void ALERT_GetInfo(const TLS_Ctx *ctx, ALERT_Info *info); 84 85 /** 86 * @brief Clear the alert information. 87 * 88 * @attention ctx cannot be empty. 89 * @param ctx [IN] tls Context 90 */ 91 void ALERT_CleanInfo(const TLS_Ctx *ctx); 92 93 /** 94 * @brief Send an alert message and cache it in the alert module. 95 * 96 * @attention ctx cannot be empty. 97 * @param ctx [IN] tls Context 98 * @param level [IN] Alert level 99 * @param description [IN] alert Description 100 * 101 */ 102 void ALERT_Send(const TLS_Ctx *ctx, ALERT_Level level, ALERT_Description description); 103 104 /** 105 * @brief Send the alert message cached by the alert module to the network layer. 106 * 107 * @attention ctx cannot be empty. Alert_Send must be invoked before flushing. 108 * @param ctx [IN] tls Context 109 * 110 * @retval HITLS_SUCCESS succeeded. 111 * @retval See REC_Write 112 */ 113 int32_t ALERT_Flush(TLS_Ctx *ctx); 114 115 /** 116 * @brief Process alert message after decryption 117 * 118 * @attention ctx cannot be empty. 119 * @param ctx [IN] tls Context 120 * @param data [IN] alert data 121 * @param dataLen [IN] alert data length 122 * @retval HITLS_REC_NORMAL_RECV_UNEXPECT_MSG 123 */ 124 int32_t ProcessDecryptedAlert(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen); 125 126 /** 127 * @brief Process plaintext alert message in TLS13 128 * 129 * @attention ctx cannot be empty. 130 * @param ctx [IN] tls Context 131 * @param data [IN] alert data 132 * @param dataLen [IN] alert data length 133 * @retval HITLS_REC_NORMAL_RECV_UNEXPECT_MSG 134 */ 135 int32_t ProcessPlainAlert(TLS_Ctx *ctx, const uint8_t *data, uint32_t dataLen); 136 137 /** 138 * @ingroup alert 139 * @brief Clear the number of consecutive received warnings 140 * 141 * @param ctx [IN] tls Context 142 */ 143 void ALERT_ClearWarnCount(TLS_Ctx *ctx); 144 145 /** 146 * @ingroup alert 147 * @brief Increase the number of alert and check whether it has exceeded the threshold or not 148 * 149 * @param ctx [IN] tls Context 150 * @param threshold [IN] alert number threshold 151 * @retval the number of alert has exceeded the threshold or not 152 */ 153 bool ALERT_HaveExceeded(TLS_Ctx *ctx, uint8_t threshold); 154 155 #ifdef HITLS_BSL_LOG 156 int32_t ReturnAlertProcess(TLS_Ctx *ctx, int32_t err, uint32_t logId, const void *logStr, 157 ALERT_Description description); 158 159 #define RETURN_ALERT_PROCESS(ctx, err, logId, logStr, description) \ 160 ReturnAlertProcess(ctx, err, logId, LOG_STR(logStr), description) 161 162 #else 163 164 #define RETURN_ALERT_PROCESS(ctx, err, logId, logStr, description) \ 165 (ctx)->method.sendAlert(ctx, ALERT_LEVEL_FATAL, description), (err) 166 #endif /* HITLS_BSL_LOG */ 167 168 #ifdef __cplusplus 169 } 170 #endif 171 172 #endif /* ALTER_H */