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 #include "hitls_build.h"
16 #ifdef HITLS_TLS_FEATURE_SECURITY
17 #include <stdint.h>
18 #include "securec.h"
19 #include "bsl_err_internal.h"
20 #include "tls_binlog_id.h"
21 #include "hitls_error.h"
22 #include "hitls_security.h"
23 #include "tls.h"
24 #include "security.h"
25 #include "config_type.h"
26 /* Number of security bits corresponding to the security level */
27 static const int32_t g_minBits[] = {HITLS_SECURITY_LEVEL_ONE_SECBITS,
28 HITLS_SECURITY_LEVEL_TWO_SECBITS,
29 HITLS_SECURITY_LEVEL_THREE_SECBITS,
30 HITLS_SECURITY_LEVEL_FOUR_SECBITS,
31 HITLS_SECURITY_LEVEL_FIVE_SECBITS};
SECURITY_GetSecbits(int32_t level)32 int32_t SECURITY_GetSecbits(int32_t level)
33 {
34 if (level <= HITLS_SECURITY_LEVEL_MIN) {
35 return 0;
36 } else {
37 level = (level > HITLS_SECURITY_LEVEL_MAX) ? HITLS_SECURITY_LEVEL_MAX : level;
38 }
39 return g_minBits[level - 1];
40 }
41
CheckCipherSuite(void * other,int32_t level)42 static int32_t CheckCipherSuite(void *other, int32_t level)
43 {
44 if (other == NULL) {
45 return SECURITY_ERR;
46 }
47
48 CipherSuiteInfo *info = (CipherSuiteInfo *)other;
49 int32_t minBits = SECURITY_GetSecbits(level);
50 if (info->strengthBits < minBits) {
51 return SECURITY_ERR;
52 }
53 /* The anonymous cipher suite is insecure. */
54 if (info->minVersion != HITLS_VERSION_TLS13 && info->authAlg == HITLS_AUTH_NULL) {
55 return SECURITY_ERR;
56 }
57 /* The level is greater than or equal to 1, and the export cipher suite and the MD5 algorithm for calculating MAC
58 * addresses are prohibited. Currently, the export cipher suite and the MD5 algorithm for calculating MAC addresses
59 * are not supported. Therefore, the check is not required. */
60 /* The RC4 stream encryption algorithm is not supported because the RC4 stream encryption algorithm is not
61 * supported. */
62 /* Forbidding non-forward security cipher suites when Level is greater than or equal to 3. */
63 if ((level >= HITLS_SECURITY_LEVEL_THREE) &&
64 (info->kxAlg != HITLS_KEY_EXCH_DHE && info->kxAlg != HITLS_KEY_EXCH_ECDHE &&
65 info->kxAlg != HITLS_KEY_EXCH_DHE_PSK && info->kxAlg != HITLS_KEY_EXCH_ECDHE_PSK &&
66 info->minVersion != HITLS_VERSION_TLS13)) {
67 return SECURITY_ERR;
68 }
69 /* If the level is greater than or equal to 4, disable the SHA1 algorithm. */
70
71 if ((level >= HITLS_SECURITY_LEVEL_FOUR) && (info->macAlg == HITLS_MAC_1)) {
72 return SECURITY_ERR;
73 }
74
75 return SECURITY_SUCCESS;
76 }
77
CheckVersion(int32_t id,int32_t level)78 static int32_t CheckVersion(int32_t id, int32_t level)
79 {
80 /* Check the DTLS version. */
81 if (IS_DTLS_VERSION((uint32_t)id)) {
82 /* The level is greater than or equal to 1, and DTLS1.0 cannot be used. */
83 if ((level >= HITLS_SECURITY_LEVEL_ONE) && ((uint32_t)id > HITLS_VERSION_DTLS12)) {
84 return SECURITY_ERR;
85 }
86 return SECURITY_SUCCESS;
87 }
88 #ifdef HITLS_TLS_PROTO_TLCP11
89 /* If the level is greater than or equal to 1, SSL2.0, SSL3.0, TLS1.0, and TLS1.1 cannot be used. */
90 if ((level >= HITLS_SECURITY_LEVEL_ONE) && ((uint32_t)id < HITLS_VERSION_TLS12) &&
91 ((uint32_t)id != HITLS_VERSION_TLCP_DTLCP11)) {
92 return SECURITY_ERR;
93 }
94 /* Level is greater than or equal to 4 and TLCP1.1 is prohibited because the security strength of the signature
95 * algorithm CERT_SIG_SCHEME_SM2_SM3 is 128 bits. */
96 if ((level >= HITLS_SECURITY_LEVEL_FOUR) && ((uint32_t)id == HITLS_VERSION_TLCP_DTLCP11)) {
97 return SECURITY_ERR;
98 }
99 #else
100 /* If the level is greater than or equal to 1, SSL2.0, SSL3.0, TLS1.0, and TLS1.1 cannot be used. */
101 if ((level >= HITLS_SECURITY_LEVEL_ONE) && ((uint32_t)id < HITLS_VERSION_TLS12)) {
102 return SECURITY_ERR;
103 }
104 #endif
105 return SECURITY_SUCCESS;
106 }
107
CheckSessionTicket(int32_t level)108 static int32_t CheckSessionTicket(int32_t level)
109 {
110 /* If the level is greater than or equal to 3, the session ticket is prohibited. */
111 if (level >= HITLS_SECURITY_LEVEL_THREE) {
112 return SECURITY_ERR;
113 }
114 return SECURITY_SUCCESS;
115 }
116
117 /* Default callback function */
SECURITY_DefaultCb(const HITLS_Ctx * ctx,const HITLS_Config * config,int32_t option,int32_t bits,int32_t id,void * other,void * exData)118 int32_t SECURITY_DefaultCb(const HITLS_Ctx *ctx, const HITLS_Config *config, int32_t option, int32_t bits, int32_t id,
119 void *other, void *exData)
120 {
121 (void)exData;
122 int32_t ret;
123 int32_t level = HITLS_DEFAULT_SECURITY_LEVEL;
124 int32_t minBits;
125 const TLS_GroupInfo *groupInfo = NULL;
126 const TLS_SigSchemeInfo *schemeInfo = NULL;
127 if (ctx == NULL && config == NULL) {
128 return SECURITY_ERR;
129 } else if (config != NULL) {
130 (void)HITLS_CFG_GetSecurityLevel(config, &level);
131 } else if (ctx != NULL) {
132 (void)HITLS_GetSecurityLevel(ctx, &level);
133 }
134 /* No restrictions are imposed when Level is 0. */
135 if (level <= HITLS_SECURITY_LEVEL_MIN) {
136 return SECURITY_SUCCESS;
137 }
138
139 if (level > HITLS_SECURITY_LEVEL_MAX) {
140 level = HITLS_SECURITY_LEVEL_MAX;
141 }
142
143 /* Check the number of security bits. */
144 minBits = SECURITY_GetSecbits(level);
145 switch (option) {
146 case HITLS_SECURITY_SECOP_VERSION:
147 /* Check the version. */
148 ret = CheckVersion(id, level);
149 break;
150 case HITLS_SECURITY_SECOP_CIPHER_SUPPORTED:
151 case HITLS_SECURITY_SECOP_CIPHER_SHARED:
152 case HITLS_SECURITY_SECOP_CIPHER_CHECK:
153 /* Check the algorithm suite. */
154 ret = CheckCipherSuite(other, level);
155 break;
156 case HITLS_SECURITY_SECOP_SIGALG_SUPPORTED:
157 case HITLS_SECURITY_SECOP_SIGALG_SHARED:
158 case HITLS_SECURITY_SECOP_SIGALG_CHECK:
159 /* Check the signature algorithm. */
160 schemeInfo = ConfigGetSignatureSchemeInfo(config, id);
161 if (schemeInfo != NULL && schemeInfo->secBits >= g_minBits[level - 1]) {
162 ret = SECURITY_SUCCESS;
163 } else {
164 ret = SECURITY_ERR;
165 }
166 break;
167 case HITLS_SECURITY_SECOP_CURVE_SUPPORTED:
168 case HITLS_SECURITY_SECOP_CURVE_SHARED:
169 case HITLS_SECURITY_SECOP_CURVE_CHECK:
170 /* Check the group. */
171 groupInfo = ConfigGetGroupInfo(config, id);
172 if (groupInfo != NULL && groupInfo->secBits >= g_minBits[level - 1]) {
173 ret = SECURITY_SUCCESS;
174 } else {
175 ret = SECURITY_ERR;
176 }
177 break;
178 case HITLS_SECURITY_SECOP_TICKET:
179 /* Check the session ticket. */
180 ret = CheckSessionTicket(level);
181 break;
182 default:
183 if (bits < minBits) {
184 return SECURITY_ERR;
185 }
186 return SECURITY_SUCCESS;
187 }
188 return ret;
189 }
190
SECURITY_SetDefault(HITLS_Config * config)191 void SECURITY_SetDefault(HITLS_Config *config)
192 {
193 if (config == NULL) {
194 return;
195 }
196 /* Default security settings. Set the default security level and default security callback function. */
197 config->securityLevel = HITLS_DEFAULT_SECURITY_LEVEL;
198 config->securityCb = SECURITY_DefaultCb;
199 return;
200 }
201
SECURITY_CfgCheck(const HITLS_Config * config,int32_t option,int32_t bits,int32_t id,void * other)202 int32_t SECURITY_CfgCheck(const HITLS_Config *config, int32_t option, int32_t bits, int32_t id, void *other)
203 {
204 if (config == NULL) {
205 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16698, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "config null", 0, 0, 0, 0);
206 BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
207 return HITLS_NULL_INPUT;
208 }
209 if (config->securityCb == NULL) {
210 /* The security callback function is empty and does not need to be checked. */
211 return SECURITY_SUCCESS;
212 }
213 return config->securityCb(NULL, config, option, bits, id, other, config->securityExData);
214 }
SECURITY_SslCheck(const HITLS_Ctx * ctx,int32_t option,int32_t bits,int32_t id,void * other)215 int32_t SECURITY_SslCheck(const HITLS_Ctx *ctx, int32_t option, int32_t bits, int32_t id, void *other)
216 {
217 if (ctx == NULL) {
218 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16699, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ctx null", 0, 0, 0, 0);
219 BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
220 return HITLS_NULL_INPUT;
221 }
222
223 return SECURITY_CfgCheck(&(ctx->config.tlsConfig), option, bits, id, other);
224 }
225 #endif /* HITLS_TLS_FEATURE_SECURITY */