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 #include "tls_binlog_id.h"
17 #include "bsl_log_internal.h"
18 #include "bsl_log.h"
19 #include "bsl_err_internal.h"
20 #include "hitls_error.h"
21 #include "tls.h"
22 #include "hs_ctx.h"
23 #include "hs_msg.h"
24 #include "hs_common.h"
25 #include "hs_kx.h"
26 #include "pack.h"
27 #include "send_process.h"
28 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
SendCertificateProcess(TLS_Ctx * ctx)29 int32_t SendCertificateProcess(TLS_Ctx *ctx)
30 {
31 int32_t ret = HITLS_SUCCESS;
32 HS_Ctx *hsCtx = (HS_Ctx *)ctx->hsCtx;
33 CERT_MgrCtx *mgrCtx = ctx->config.tlsConfig.certMgrCtx;
34 /* Determine whether the message needs to be packed */
35 if (hsCtx->msgLen == 0) {
36 /* Only the client can send a certificate message with an empty certificate */
37 if ((ctx->isClient == false) && (SAL_CERT_GetCurrentCert(mgrCtx) == NULL)) {
38 BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE);
39 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15760, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
40 "no certificate could be used in server.", 0, 0, 0, 0);
41 return HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE;
42 }
43
44 ret = HS_PackMsg(ctx, CERTIFICATE, hsCtx->msgBuf, hsCtx->bufferLen, &hsCtx->msgLen);
45 if (ret != HITLS_SUCCESS) {
46 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15761, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
47 "pack certificate msg fail.", 0, 0, 0, 0);
48 return ret;
49 }
50 }
51
52 ret = HS_SendMsg(ctx);
53 if (ret != HITLS_SUCCESS) {
54 return ret;
55 }
56
57 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15762, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
58 "send certificate msg success.", 0, 0, 0, 0);
59
60 if (ctx->isClient) {
61 return HS_ChangeState(ctx, TRY_SEND_CLIENT_KEY_EXCHANGE);
62 }
63 if (IsNeedServerKeyExchange(ctx) == true) {
64 return HS_ChangeState(ctx, TRY_SEND_SERVER_KEY_EXCHANGE);
65 }
66 /* The server sends CertificateRequest only when the isSupportClientVerify mode is enabled */
67 if (ctx->config.tlsConfig.isSupportClientVerify) {
68 /* isSupportClientOnceVerify specifies whether the CR is sent only in the initial handshake phase. */
69 /* The value of certReqSendTime indicates the number of sent CR messages. If the value of certReqSendTime in the
70 * renegotiation phase is 0 and isSupportClientOnceVerify is enabled, the CR messages will not be sent. */
71 if (ctx->negotiatedInfo.certReqSendTime < 1 || !(ctx->config.tlsConfig.isSupportClientOnceVerify)) {
72 return HS_ChangeState(ctx, TRY_SEND_CERTIFICATE_REQUEST);
73 }
74 }
75 return HS_ChangeState(ctx, TRY_SEND_SERVER_HELLO_DONE);
76 }
77 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
78 #ifdef HITLS_TLS_PROTO_TLS13
Tls13ClientSendCertificateProcess(TLS_Ctx * ctx)79 int32_t Tls13ClientSendCertificateProcess(TLS_Ctx *ctx)
80 {
81 int32_t ret = HITLS_SUCCESS;
82 HS_Ctx *hsCtx = (HS_Ctx *)ctx->hsCtx;
83
84 /* Determine whether the message needs to be packed */
85 if (hsCtx->msgLen == 0) {
86 /* In the middlebox scenario, if the client does not send the hrr message, a CCS message needs to be sent
87 * before the certificate */
88 if (!ctx->hsCtx->haveHrr
89 #ifdef HITLS_TLS_FEATURE_PHA
90 && ctx->phaState != PHA_REQUESTED
91 #endif /* HITLS_TLS_FEATURE_PHA */
92 ) {
93 ret = ctx->method.sendCCS(ctx);
94 if (ret != HITLS_SUCCESS) {
95 return ret;
96 }
97 }
98 #ifdef HITLS_TLS_FEATURE_PHA
99 if (ctx->phaState != PHA_REQUESTED)
100 #endif /* HITLS_TLS_FEATURE_PHA */
101 {
102 /* CCS messages cannot be encrypted. Therefore, you need to activate the
103 sending key of the client after sending CCS messages. */
104 uint32_t hashLen = SAL_CRYPT_DigestSize(ctx->negotiatedInfo.cipherSuiteInfo.hashAlg);
105 if (hashLen == 0) {
106 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17103, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
107 "DigestSize fail", 0, 0, 0, 0);
108 return HITLS_CRYPT_ERR_DIGEST;
109 }
110 ret = HS_SwitchTrafficKey(ctx, ctx->hsCtx->clientHsTrafficSecret, hashLen, true);
111 if (ret != HITLS_SUCCESS) {
112 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17104, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
113 "SwitchTrafficKey fail", 0, 0, 0, 0);
114 return ret;
115 }
116 }
117
118 ret = HS_PackMsg(ctx, CERTIFICATE, hsCtx->msgBuf, hsCtx->bufferLen, &hsCtx->msgLen);
119 if (ret != HITLS_SUCCESS) {
120 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15763, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
121 "pack tls1.3 client certificate msg fail.", 0, 0, 0, 0);
122 return ret;
123 }
124 }
125
126 ret = HS_SendMsg(ctx);
127 if (ret != HITLS_SUCCESS) {
128 return ret;
129 }
130
131 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15764, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
132 "send tls1.3 client certificate msg success.", 0, 0, 0, 0);
133
134 /* If the certificate is empty, the certificate verify message does not need to be sent. */
135 if (SAL_CERT_GetCurrentCert(ctx->config.tlsConfig.certMgrCtx) == NULL) {
136 return HS_ChangeState(ctx, TRY_SEND_FINISH);
137 }
138 return HS_ChangeState(ctx, TRY_SEND_CERTIFICATE_VERIFY);
139 }
140
Tls13ServerSendCertificateProcess(TLS_Ctx * ctx)141 int32_t Tls13ServerSendCertificateProcess(TLS_Ctx *ctx)
142 {
143 int32_t ret = HITLS_SUCCESS;
144 HS_Ctx *hsCtx = (HS_Ctx *)ctx->hsCtx;
145
146 /* Determine whether the message needs to be packed */
147 if (hsCtx->msgLen == 0) {
148 /* The server cannot send an empty certificate message */
149 if (SAL_CERT_GetCurrentCert(ctx->config.tlsConfig.certMgrCtx) == NULL) {
150 BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE);
151 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15765, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
152 "no certificate could be used in server.", 0, 0, 0, 0);
153 return HITLS_MSG_HANDLE_ERR_NO_SERVER_CERTIFICATE;
154 }
155
156 ret = HS_PackMsg(ctx, CERTIFICATE, hsCtx->msgBuf, hsCtx->bufferLen, &hsCtx->msgLen);
157 if (ret != HITLS_SUCCESS) {
158 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15766, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
159 "pack server tls1.3 certificate msg fail.", 0, 0, 0, 0);
160 return ret;
161 }
162 }
163
164 ret = HS_SendMsg(ctx);
165 if (ret != HITLS_SUCCESS) {
166 return ret;
167 }
168
169 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15767, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN,
170 "send tls1.3 server certificate msg success.", 0, 0, 0, 0);
171
172 return HS_ChangeState(ctx, TRY_SEND_CERTIFICATE_VERIFY);
173 }
174 #endif /* HITLS_TLS_PROTO_TLS13 */