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_HOST_SERVER
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include "securec.h"
20 #include "bsl_sal.h"
21 #include "tls_binlog_id.h"
22 #include "bsl_log_internal.h"
23 #include "bsl_log.h"
24 #include "bsl_err_internal.h"
25 #include "bsl_bytes.h"
26 #include "hitls_error.h"
27 #include "tls.h"
28 #include "hs_common.h"
29 #include "hs_ctx.h"
30 #include "hs_extensions.h"
31 #include "pack_common.h"
32 #include "pack_extensions.h"
33 #include "cert_mgr_ctx.h"
34
35 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
36 typedef struct {
37 uint8_t certType;
38 bool isSupported;
39 } PackCertTypesInfo;
PackCertificateTypes(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)40 static int32_t PackCertificateTypes(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
41 {
42 uint32_t offset = 0u;
43 const TLS_Config *config = &(ctx->config.tlsConfig);
44
45 if ((config->cipherSuites == NULL) || (config->cipherSuitesSize == 0)) {
46 BSL_ERR_PUSH_ERROR(HITLS_INTERNAL_EXCEPTION);
47 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15682, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
48 "pack certificate types error, invalid input parameter.", 0, 0, 0, 0);
49 return HITLS_INTERNAL_EXCEPTION;
50 }
51
52 PackCertTypesInfo certTypeLists[] = {
53 {CERT_TYPE_RSA_SIGN, false},
54 {CERT_TYPE_ECDSA_SIGN, false},
55 {CERT_TYPE_DSS_SIGN, false}
56 };
57
58 uint8_t certTypeListsSize = (uint8_t)(sizeof(certTypeLists) / sizeof(certTypeLists[0]));
59 uint8_t supportedCertTypesSize = 0;
60 uint32_t baseSignAlgorithmsSize = config->signAlgorithmsSize;
61 const uint16_t *baseSignAlgorithms = config->signAlgorithms;
62 for (uint32_t i = 0; i < baseSignAlgorithmsSize; i++) {
63 HITLS_CERT_KeyType keyType = SAL_CERT_SignScheme2CertKeyType(ctx, baseSignAlgorithms[i]);
64 CERT_Type certType = CertKeyType2CertType(keyType);
65 for (uint32_t j = 0; j < certTypeListsSize; j++) {
66 if ((certTypeLists[j].certType == certType) && (certTypeLists[j].isSupported == false)) {
67 certTypeLists[j].isSupported = true;
68 supportedCertTypesSize++;
69 break;
70 }
71 }
72 }
73
74 if (bufLen < (sizeof(uint8_t) + supportedCertTypesSize)) {
75 return PackBufLenError(BINLOG_ID17119, BINGLOG_STR("certificate type"));
76 }
77
78 buf[offset] = supportedCertTypesSize;
79 offset += sizeof(uint8_t);
80 for (uint32_t i = 0; i < certTypeListsSize; i++) {
81 if (certTypeLists[i].isSupported == true) {
82 buf[offset] = certTypeLists[i].certType;
83 offset += sizeof(uint8_t);
84 }
85 }
86
87 *usedLen = offset;
88 return HITLS_SUCCESS;
89 }
90 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
91
92 #if defined(HITLS_TLS_PROTO_TLS12) || defined(HITLS_TLS_PROTO_DTLS12)
PackSignAlgorithms(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)93 static int32_t PackSignAlgorithms(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
94 {
95 uint32_t offset = 0u;
96 const TLS_Config *config = &(ctx->config.tlsConfig);
97
98 if ((config->signAlgorithms == NULL) || (config->signAlgorithmsSize == 0)) {
99 BSL_ERR_PUSH_ERROR(HITLS_INTERNAL_EXCEPTION);
100 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15684, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
101 "pack signature algorithms error, invalid input parameter.", 0, 0, 0, 0);
102 return HITLS_INTERNAL_EXCEPTION;
103 }
104
105 uint16_t signAlgorithmsSize = (uint16_t)config->signAlgorithmsSize * sizeof(uint16_t);
106 if (bufLen < (sizeof(uint16_t) + signAlgorithmsSize)) {
107 return PackBufLenError(BINLOG_ID15683, BINGLOG_STR("sign algorithms"));
108 }
109
110 BSL_Uint16ToByte(signAlgorithmsSize, &buf[offset]);
111 offset += sizeof(uint16_t);
112 for (uint32_t index = 0; index < config->signAlgorithmsSize; index++) {
113 BSL_Uint16ToByte(config->signAlgorithms[index], &buf[offset]);
114 offset += sizeof(uint16_t);
115 }
116
117 *usedLen = offset;
118 return HITLS_SUCCESS;
119 }
120 #endif /* HITLS_TLS_PROTO_TLS12 || HITLS_TLS_PROTO_DTLS12 */
121
122 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
PackCertificateRequest(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)123 int32_t PackCertificateRequest(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
124 {
125 uint32_t offset = 0u;
126 uint32_t len = 0u;
127
128 int32_t ret = PackCertificateTypes(ctx, buf, bufLen, &len);
129 if (ret != HITLS_SUCCESS) {
130 return ret;
131 }
132 offset += len;
133
134 #if defined(HITLS_TLS_PROTO_TLS12) || defined(HITLS_TLS_PROTO_DTLS12)
135 /* TLCP does not have the signature algorithm field */
136 if (ctx->negotiatedInfo.version != HITLS_VERSION_TLCP_DTLCP11) {
137 len = 0u;
138 ret = PackSignAlgorithms(ctx, &buf[offset], bufLen - offset, &len);
139 if (ret != HITLS_SUCCESS) {
140 return ret;
141 }
142 offset += len;
143 }
144 #endif
145 /* The distinguishable name of the certificate authorization list. The currently supported certificate authorization
146 * list is empty */
147 BSL_Uint16ToByte(0, &buf[offset]);
148 offset += sizeof(uint16_t);
149
150 *usedLen = offset;
151 return HITLS_SUCCESS;
152 }
153 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
154 #ifdef HITLS_TLS_PROTO_TLS13
PackSignAlgorithmsExtension(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)155 static int32_t PackSignAlgorithmsExtension(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
156 {
157 uint32_t offset = 0u;
158 const TLS_Config *config = &(ctx->config.tlsConfig);
159
160 if ((config->signAlgorithms == NULL) || (config->signAlgorithmsSize == 0)) {
161 BSL_ERR_PUSH_ERROR(HITLS_INTERNAL_EXCEPTION);
162 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15686, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
163 "pack signature algorithms error, invalid input parameter.", 0, 0, 0, 0);
164 return HITLS_INTERNAL_EXCEPTION;
165 }
166
167 uint32_t signAlgorithmsSize = 0;
168 uint16_t *signAlgorithms = CheckSupportSignAlgorithms(ctx, config->signAlgorithms,
169 config->signAlgorithmsSize, &signAlgorithmsSize);
170 if (signAlgorithms == NULL || signAlgorithmsSize == 0) {
171 BSL_SAL_FREE(signAlgorithms);
172 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17310, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
173 "no available signAlgo", 0, 0, 0, 0);
174 ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
175 return HITLS_CERT_ERR_NO_SIGN_SCHEME_MATCH;
176 }
177
178 uint16_t exMsgHeaderLen = sizeof(uint16_t);
179 uint16_t exMsgDataLen = sizeof(uint16_t) * (uint16_t)signAlgorithmsSize;
180
181 int32_t ret = PackExtensionHeader(HS_EX_TYPE_SIGNATURE_ALGORITHMS, exMsgHeaderLen + exMsgDataLen, buf, bufLen);
182 if (ret != HITLS_SUCCESS) {
183 BSL_SAL_FREE(signAlgorithms);
184 return ret;
185 }
186 offset += HS_EX_HEADER_LEN;
187
188 if (bufLen < sizeof(uint16_t) + offset) {
189 BSL_SAL_FREE(signAlgorithms);
190 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16920, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "buflen err", 0, 0, 0, 0);
191 return HITLS_PACK_NOT_ENOUGH_BUF_LENGTH;
192 }
193 BSL_Uint16ToByte(exMsgDataLen, &buf[offset]);
194 offset += sizeof(uint16_t);
195
196 if (bufLen < exMsgDataLen + offset) {
197 BSL_SAL_FREE(signAlgorithms);
198 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16921, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "buflen err", 0, 0, 0, 0);
199 return HITLS_PACK_NOT_ENOUGH_BUF_LENGTH;
200 }
201 for (uint32_t index = 0; index < signAlgorithmsSize; index++) {
202 BSL_Uint16ToByte(signAlgorithms[index], &buf[offset]);
203 offset += sizeof(uint16_t);
204 }
205 BSL_SAL_FREE(signAlgorithms);
206
207 *usedLen = offset;
208 return HITLS_SUCCESS;
209 }
210
PackCertReqExtensions(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)211 static int32_t PackCertReqExtensions(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
212 {
213 int32_t ret = HITLS_SUCCESS;
214 uint32_t listSize;
215 uint32_t exLen = 0u;
216 uint32_t offset = 0u;
217
218 const PackExtInfo extMsgList[] = {
219 {.exMsgType = HS_EX_TYPE_SIGNATURE_ALGORITHMS,
220 .needPack = true,
221 .packFunc = PackSignAlgorithmsExtension},
222 {.exMsgType = HS_EX_TYPE_SIGNATURE_ALGORITHMS_CERT,
223 .needPack = false,
224 .packFunc = NULL},
225 };
226
227 listSize = sizeof(extMsgList) / sizeof(extMsgList[0]);
228 #ifdef HITLS_TLS_FEATURE_CUSTOM_EXTENSION
229 if (IsPackNeedCustomExtensions(CUSTOM_EXT_FROM_CTX(ctx), HITLS_EX_TYPE_TLS1_3_CERTIFICATE_REQUEST)) {
230 ret = PackCustomExtensions(ctx, &buf[offset], bufLen - offset, &exLen, HITLS_EX_TYPE_TLS1_3_CERTIFICATE_REQUEST, NULL, 0);
231 if (ret != HITLS_SUCCESS) {
232 return ret;
233 }
234 offset += exLen;
235 }
236 #endif /* HITLS_TLS_FEATURE_CUSTOM_EXTENSION */
237
238 for (uint32_t index = 0; index < listSize; index++) {
239 if (extMsgList[index].packFunc == NULL) {
240 exLen = 0u;
241 ret = PackEmptyExtension(extMsgList[index].exMsgType, extMsgList[index].needPack,
242 &buf[offset], bufLen - offset, &exLen);
243 if (ret != HITLS_SUCCESS) {
244 return ret;
245 }
246 offset += exLen;
247 }
248 if (extMsgList[index].packFunc != NULL && extMsgList[index].needPack) {
249 exLen = 0u;
250 ret = extMsgList[index].packFunc(ctx, &buf[offset], bufLen - offset, &exLen);
251 if (ret != HITLS_SUCCESS) {
252 return ret;
253 }
254 offset += exLen;
255 }
256 }
257
258 *usedLen = offset;
259 return HITLS_SUCCESS;
260 }
261
Tls13PackCertReqExtensions(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * len)262 int32_t Tls13PackCertReqExtensions(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *len)
263 {
264 int32_t ret = HITLS_SUCCESS;
265 uint32_t headerLen;
266 uint32_t exLen = 0u;
267
268 headerLen = sizeof(uint16_t);
269 if (bufLen < headerLen) {
270 return PackBufLenError(BINLOG_ID15687, BINGLOG_STR("certReq extension"));
271 }
272
273 /* Pack the extended content of the Tls1.3 Certificate Request */
274 ret = PackCertReqExtensions(ctx, &buf[headerLen], bufLen - headerLen, &exLen);
275 if (ret != HITLS_SUCCESS) {
276 return ret;
277 }
278
279 if (exLen > 0u) {
280 BSL_Uint16ToByte((uint16_t)exLen, buf);
281 *len = exLen + headerLen;
282 } else {
283 BSL_Uint16ToByte((uint16_t) 0, buf);
284 *len = 0u + headerLen;
285 }
286
287 return HITLS_SUCCESS;
288 }
289
Tls13PackCertificateRequest(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)290 int32_t Tls13PackCertificateRequest(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
291 {
292 int32_t ret = HITLS_SUCCESS;
293 uint32_t offset = 0u;
294 uint32_t exMsgLen = 0u;
295
296 if (bufLen < sizeof(uint8_t) + ctx->certificateReqCtxSize) {
297 return PackBufLenError(BINLOG_ID15688, BINGLOG_STR("tls1.3 certReq"));
298 }
299 /* Pack certificate_request_context */
300 buf[offset] = (uint8_t)ctx->certificateReqCtxSize;
301 offset++;
302
303 if (ctx->certificateReqCtxSize > 0) {
304 (void)memcpy_s(&buf[offset], bufLen - offset, ctx->certificateReqCtx, ctx->certificateReqCtxSize);
305 offset += ctx->certificateReqCtxSize;
306 }
307
308 ret = Tls13PackCertReqExtensions(ctx, &buf[offset], bufLen - offset, &exMsgLen);
309 if (ret != HITLS_SUCCESS) {
310 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15690, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
311 "pack tls1.3 certificate request msg extension content fail.", 0, 0, 0, 0);
312 return ret;
313 }
314 offset += exMsgLen;
315 *usedLen = offset;
316
317 return HITLS_SUCCESS;
318 }
319 #endif /* HITLS_TLS_PROTO_TLS13 */
320
321 #endif /* HITLS_TLS_HOST_SERVER */