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_CLIENT
17 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
18 #include <stdint.h>
19 #include "securec.h"
20 #include "tls_binlog_id.h"
21 #include "bsl_log_internal.h"
22 #include "bsl_log.h"
23 #include "bsl_err_internal.h"
24 #include "bsl_bytes.h"
25 #include "bsl_sal.h"
26 #include "hitls_error.h"
27 #include "tls.h"
28 #include "crypt.h"
29 #include "cert_method.h"
30 #include "hs_ctx.h"
31 #include "hs_common.h"
32 #include "pack_common.h"
33
34 #ifdef HITLS_TLS_SUITE_KX_ECDHE
PackClientKxMsgNamedCurve(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)35 static int32_t PackClientKxMsgNamedCurve(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
36 {
37 int32_t ret = HITLS_SUCCESS;
38 uint32_t pubKeyLen;
39 uint32_t offset = 0;
40 EcdhParam *ecdh = &(ctx->hsCtx->kxCtx->keyExchParam.ecdh);
41 HITLS_ECParameters *curveParams = &ecdh->curveParams;
42 KeyExchCtx *kxCtx = ctx->hsCtx->kxCtx;
43
44 pubKeyLen = SAL_CRYPT_GetCryptLength(ctx, HITLS_CRYPT_INFO_CMD_GET_PUBLIC_KEY_LEN, curveParams->param.namedcurve);
45 if (pubKeyLen == 0u) {
46 BSL_ERR_PUSH_ERROR(HITLS_PACK_INVALID_KX_PUBKEY_LENGTH);
47 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15673, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
48 "invalid key exchange pubKey length.", 0, 0, 0, 0);
49 return HITLS_PACK_INVALID_KX_PUBKEY_LENGTH;
50 }
51
52 if (bufLen < (sizeof(uint8_t) + pubKeyLen)) {
53 return PackBufLenError(BINLOG_ID15674, BINGLOG_STR("ClientKx"));
54 }
55 #ifdef HITLS_TLS_PROTO_TLCP11
56 if (ctx->negotiatedInfo.version ==
57 HITLS_VERSION_TLCP_DTLCP11) { /* Compatible with OpenSSL. Three bytes are added to the client key exchange. */
58 if (bufLen < (sizeof(uint8_t) + pubKeyLen + sizeof(uint8_t) + sizeof(uint16_t))) {
59 return PackBufLenError(BINLOG_ID16216, BINGLOG_STR("ClientKx"));
60 }
61 buf[offset] = HITLS_EC_CURVE_TYPE_NAMED_CURVE;
62 offset += sizeof(uint8_t);
63 BSL_Uint16ToByte(HITLS_EC_GROUP_SM2, &buf[offset]);
64 offset += sizeof(uint16_t);
65 }
66 #endif
67
68 uint32_t pubKeyLenOffset = offset;
69 offset += sizeof(uint8_t);
70 uint32_t pubKeyUsedLen = 0;
71 ret = SAL_CRYPT_EncodeEcdhPubKey(kxCtx->key, &buf[offset], pubKeyLen, &pubKeyUsedLen);
72 if (ret != HITLS_SUCCESS || pubKeyLen != pubKeyUsedLen) {
73 BSL_ERR_PUSH_ERROR(HITLS_CRYPT_ERR_ENCODE_ECDH_KEY);
74 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15675, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
75 "encode ecdh key fail.", 0, 0, 0, 0);
76 return HITLS_CRYPT_ERR_ENCODE_ECDH_KEY;
77 }
78 offset += pubKeyUsedLen;
79 buf[pubKeyLenOffset] = (uint8_t)pubKeyUsedLen;
80
81 *usedLen = offset;
82 return HITLS_SUCCESS;
83 }
84
PackClientKxMsgEcdhe(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)85 static int32_t PackClientKxMsgEcdhe(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
86 {
87 HITLS_ECCurveType type = ctx->hsCtx->kxCtx->keyExchParam.ecdh.curveParams.type;
88 switch (type) {
89 case HITLS_EC_CURVE_TYPE_NAMED_CURVE:
90 return PackClientKxMsgNamedCurve(ctx, buf, bufLen, usedLen);
91 default:
92 break;
93 }
94
95 BSL_ERR_PUSH_ERROR(HITLS_PACK_UNSUPPORT_KX_CURVE_TYPE);
96 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15676, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
97 "unsupport key exchange curve type.", 0, 0, 0, 0);
98 return HITLS_PACK_UNSUPPORT_KX_CURVE_TYPE;
99 }
100 #endif /* HITLS_TLS_SUITE_KX_ECDHE */
101 #ifdef HITLS_TLS_SUITE_KX_DHE
PackClientKxMsgDhe(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)102 static int32_t PackClientKxMsgDhe(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
103 {
104 int32_t ret = HITLS_SUCCESS;
105 DhParam *dh = &ctx->hsCtx->kxCtx->keyExchParam.dh;
106 KeyExchCtx *kxCtx = ctx->hsCtx->kxCtx;
107
108 uint32_t pubkeyLen = dh->plen;
109 if (pubkeyLen == 0u) {
110 BSL_ERR_PUSH_ERROR(HITLS_PACK_INVALID_KX_PUBKEY_LENGTH);
111 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15677, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
112 "invalid key exchange pubKey length.", 0, 0, 0, 0);
113 return HITLS_PACK_INVALID_KX_PUBKEY_LENGTH;
114 }
115
116 if (bufLen < (sizeof(uint16_t) + pubkeyLen)) {
117 return PackBufLenError(BINLOG_ID15678, BINGLOG_STR("ClientKx"));
118 }
119
120 uint32_t offset = sizeof(uint16_t);
121 /* fill pubkey */
122 ret = SAL_CRYPT_EncodeDhPubKey(kxCtx->key, &buf[offset], pubkeyLen, &pubkeyLen);
123 if (ret != HITLS_SUCCESS) {
124 BSL_ERR_PUSH_ERROR(HITLS_CRYPT_ERR_ENCODE_DH_KEY);
125 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15679, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
126 "encode dh pub key fail.", 0, 0, 0, 0);
127 return HITLS_CRYPT_ERR_ENCODE_DH_KEY;
128 }
129 offset += pubkeyLen;
130 /* fill pubkey length */
131 BSL_Uint16ToByte((uint16_t)pubkeyLen, buf);
132 *usedLen = offset;
133
134 return HITLS_SUCCESS;
135 }
136 #endif /* HITLS_TLS_SUITE_KX_DHE */
137 #ifdef HITLS_TLS_SUITE_KX_RSA
PackClientKxMsgRsa(TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)138 int32_t PackClientKxMsgRsa(TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
139 {
140 int32_t ret = HITLS_SUCCESS;
141 uint32_t encLen, offset = 0;
142 HS_Ctx *hsCtx = ctx->hsCtx;
143 KeyExchCtx *kxCtx = hsCtx->kxCtx;
144 uint8_t *preMasterSecret = kxCtx->keyExchParam.rsa.preMasterSecret;
145
146 if (bufLen < sizeof(uint16_t)) {
147 return PackBufLenError(BINLOG_ID15680, BINGLOG_STR("ClientKx"));
148 }
149
150 offset = sizeof(uint16_t);
151 encLen = bufLen - offset;
152
153 HITLS_Config *config = &ctx->config.tlsConfig;
154 CERT_MgrCtx *mgrCtx = config->certMgrCtx;
155 HITLS_CERT_X509 *cert = SAL_CERT_PairGetX509(hsCtx->peerCert);
156 HITLS_CERT_Key *pubkey = NULL;
157 ret = SAL_CERT_X509Ctrl(config, cert, CERT_CTRL_GET_PUB_KEY, NULL, (void *)&pubkey);
158 if (ret != HITLS_SUCCESS) {
159 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16929, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
160 "CERT_CTRL_GET_PUB_KEY fail", 0, 0, 0, 0);
161 return ret;
162 }
163 ret = SAL_CERT_KeyEncrypt(ctx, pubkey, preMasterSecret, MASTER_SECRET_LEN, &buf[offset], &encLen);
164 SAL_CERT_KeyFree(mgrCtx, pubkey);
165 if (ret != HITLS_SUCCESS) {
166 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16930, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
167 "GET_PUB_KEY fail", 0, 0, 0, 0);
168 return ret;
169 }
170 offset += encLen;
171 BSL_Uint16ToByte((uint16_t)encLen, buf);
172
173 *usedLen = offset;
174 return HITLS_SUCCESS;
175 }
176 #endif /* HITLS_TLS_SUITE_KX_RSA */
177 #ifdef HITLS_TLS_PROTO_TLCP11
PackClientKxMsgEcc(TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)178 static int32_t PackClientKxMsgEcc(TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
179 {
180 int32_t ret = HITLS_SUCCESS;
181 HS_Ctx *hsCtx = ctx->hsCtx;
182 KeyExchCtx *kxCtx = hsCtx->kxCtx;
183 uint8_t *preMasterSecret = kxCtx->keyExchParam.ecc.preMasterSecret;
184
185 if (bufLen < sizeof(uint16_t)) {
186 return PackBufLenError(BINLOG_ID16217, BINGLOG_STR("ClientKx"));
187 }
188
189 uint32_t offset = sizeof(uint16_t);
190 uint32_t encLen = bufLen - offset;
191
192 /* Encrypt the PreMasterSecret using the public key of the server certificate */
193 HITLS_Config *config = &ctx->config.tlsConfig;
194 CERT_MgrCtx *certMgrCtx = config->certMgrCtx;
195 HITLS_CERT_X509 *certEnc = SAL_CERT_GetTlcpEncCert(hsCtx->peerCert);
196 HITLS_CERT_Key *pubkey = NULL;
197 ret = SAL_CERT_X509Ctrl(config, certEnc, CERT_CTRL_GET_PUB_KEY, NULL, (void *)&pubkey);
198 if (ret != HITLS_SUCCESS) {
199 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16218, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
200 "get encrypt cert public key failed.", 0, 0, 0, 0);
201 return ret;
202 }
203 ret = SAL_CERT_KeyEncrypt(ctx, pubkey, preMasterSecret, MASTER_SECRET_LEN, &buf[offset], &encLen);
204 SAL_CERT_KeyFree(certMgrCtx, pubkey);
205 if (ret != HITLS_SUCCESS) {
206 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16932, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
207 "KeyEncrypt fail", 0, 0, 0, 0);
208 return ret;
209 }
210 offset += encLen;
211
212 /* fill the ciphertext length */
213 BSL_Uint16ToByte((uint16_t)encLen, buf);
214
215 *usedLen = offset;
216 return HITLS_SUCCESS;
217 }
218 #endif
219 #ifdef HITLS_TLS_FEATURE_PSK
PackClientKxMsgIdentity(const TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)220 static int32_t PackClientKxMsgIdentity(const TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
221 {
222 uint8_t *pskIdentity = ctx->hsCtx->kxCtx->pskInfo->identity;
223 uint32_t pskIdentitySize = ctx->hsCtx->kxCtx->pskInfo->identityLen;
224 uint32_t dataLen = sizeof(uint16_t) + pskIdentitySize;
225
226 if (bufLen < dataLen) {
227 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16933, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "buflen err", 0, 0, 0, 0);
228 BSL_ERR_PUSH_ERROR(HITLS_PACK_NOT_ENOUGH_BUF_LENGTH);
229 return HITLS_PACK_NOT_ENOUGH_BUF_LENGTH;
230 }
231
232 /* append identity */
233 uint32_t offset = 0u;
234 BSL_Uint16ToByte((uint16_t)pskIdentitySize, &buf[offset]);
235 offset += sizeof(uint16_t);
236
237 if (bufLen - offset < pskIdentitySize) {
238 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16934, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "buflen err", 0, 0, 0, 0);
239 BSL_ERR_PUSH_ERROR(HITLS_PACK_NOT_ENOUGH_BUF_LENGTH);
240 return HITLS_PACK_NOT_ENOUGH_BUF_LENGTH;
241 }
242 (void)memcpy_s(&buf[offset], bufLen - offset, pskIdentity, pskIdentitySize);
243 offset += pskIdentitySize;
244
245 *usedLen = offset;
246 return HITLS_SUCCESS;
247 }
248 #endif /* HITLS_TLS_FEATURE_PSK */
249 // Pack the ClientKeyExchange message.
250
PackClientKeyExchange(TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)251 int32_t PackClientKeyExchange(TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
252 {
253 int32_t ret = HITLS_SUCCESS;
254 uint32_t len = 0u;
255 uint32_t offset = 0u;
256 #ifdef HITLS_TLS_FEATURE_PSK
257 /* PSK negotiation pre act: append identity */
258 if (IsPskNegotiation(ctx)) {
259 if ((ret = PackClientKxMsgIdentity(ctx, buf, bufLen, &len)) != HITLS_SUCCESS) {
260 return ret;
261 }
262 offset = offset + len;
263 }
264 #endif /* HITLS_TLS_FEATURE_PSK */
265 len = 0u;
266 /* Pack the key exchange message */
267 switch (ctx->negotiatedInfo.cipherSuiteInfo.kxAlg) {
268 #ifdef HITLS_TLS_SUITE_KX_ECDHE
269 case HITLS_KEY_EXCH_ECDHE: /* TLCP is also included */
270 case HITLS_KEY_EXCH_ECDHE_PSK:
271 ret = PackClientKxMsgEcdhe(ctx, &buf[offset], bufLen - offset, &len);
272 break;
273 #endif /* HITLS_TLS_SUITE_KX_ECDHE */
274 #ifdef HITLS_TLS_SUITE_KX_DHE
275 case HITLS_KEY_EXCH_DHE:
276 case HITLS_KEY_EXCH_DHE_PSK:
277 ret = PackClientKxMsgDhe(ctx, &buf[offset], bufLen - offset, &len);
278 break;
279 #endif /* HITLS_TLS_SUITE_KX_DHE */
280 #ifdef HITLS_TLS_SUITE_KX_RSA
281 case HITLS_KEY_EXCH_RSA:
282 case HITLS_KEY_EXCH_RSA_PSK:
283 ret = PackClientKxMsgRsa(ctx, &buf[offset], bufLen - offset, &len);
284 break;
285 #endif /* HITLS_TLS_SUITE_KX_RSA */
286 #ifdef HITLS_TLS_PROTO_TLCP11
287 case HITLS_KEY_EXCH_ECC:
288 ret = PackClientKxMsgEcc(ctx, &buf[offset], bufLen - offset, &len);
289 break;
290 #endif
291 case HITLS_KEY_EXCH_PSK:
292 ret = HITLS_SUCCESS;
293 break;
294 default:
295 BSL_ERR_PUSH_ERROR(HITLS_PACK_UNSUPPORT_KX_ALG);
296 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15681, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
297 "unsupport key exchange algorithm when pack client key exchange.", 0, 0, 0, 0);
298 return HITLS_PACK_UNSUPPORT_KX_ALG;
299 }
300 offset = offset + len;
301 *usedLen = offset;
302 return ret;
303 }
304 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
305 #endif /* HITLS_TLS_HOST_CLIENT */