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 #include <stdint.h>
18 #include "securec.h"
19 #include "bsl_sal.h"
20 #include "bsl_log.h"
21 #include "bsl_bytes.h"
22 #include "bsl_log_internal.h"
23 #include "bsl_err_internal.h"
24 #include "tls.h"
25 #include "hs_ctx.h"
26 #include "hs_msg.h"
27 #include "hs_common.h"
28 #include "hs_extensions.h"
29 #include "hitls_error.h"
30 #include "tls_binlog_id.h"
31 #include "cert_mgr_ctx.h"
32
33 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
34 // The client processes the certificate request
ClientRecvCertRequestProcess(TLS_Ctx * ctx,HS_Msg * msg)35 int32_t ClientRecvCertRequestProcess(TLS_Ctx *ctx, HS_Msg *msg)
36 {
37 /**
38 * If the server certificate is not received, a failure message is returned after the cert request is received
39 * RFC 5246 7.4.4: Note: It is a fatal handshake_failure alert for
40 * an anonymous server to request client authentication.
41 */
42 if (ctx->hsCtx->peerCert == NULL) {
43 BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_NO_PEER_CERTIFIACATE);
44 ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_HANDSHAKE_FAILURE);
45 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15869, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
46 "got cert request but not get peer certificate.", 0, 0, 0, 0);
47 return HITLS_MSG_HANDLE_NO_PEER_CERTIFIACATE;
48 }
49 /* If ECC and ECHDE of TLCP are used, this parameter must be set because the
50 * TLCP server must send the req cert message to the client to send the certificate, which may be
51 * used for identity authentication, The latter may be used for key derivation, depending on the cipher suite and
52 * server configuration (isSupportClientVerify). */
53 ctx->hsCtx->isNeedClientCert = true;
54
55 CertificateRequestMsg *certReq = &msg->body.certificateReq;
56 CERT_ExpectInfo expectCertInfo = {0};
57 expectCertInfo.certType = CERT_TYPE_UNKNOWN;
58 expectCertInfo.signSchemeList = certReq->signatureAlgorithms;
59 expectCertInfo.signSchemeNum = certReq->signatureAlgorithmsSize;
60 expectCertInfo.caList = certReq->caList;
61 (void)SAL_CERT_SelectCertByInfo(ctx, &expectCertInfo);
62
63 return HS_ChangeState(ctx, TRY_RECV_SERVER_HELLO_DONE);
64 }
65 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
66
67 #ifdef HITLS_TLS_FEATURE_PHA
Tls13ClientStoreCertReqCtx(TLS_Ctx * ctx,const CertificateRequestMsg * certReq)68 static int32_t Tls13ClientStoreCertReqCtx(TLS_Ctx *ctx, const CertificateRequestMsg *certReq)
69 {
70 /** If authentication is not performed after handshake, the cert req ctx length should be 0 */
71 if ((ctx->phaState != PHA_REQUESTED && certReq->certificateReqCtxSize != 0) ||
72 (ctx->phaState == PHA_REQUESTED && certReq->certificateReqCtxSize == 0)) {
73 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15870, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
74 "certificateReqCtxSize is invalid.", 0, 0, 0, 0);
75 ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_ILLEGAL_PARAMETER);
76 return HITLS_MSG_HANDLE_INVALID_CERT_REQ_CTX;
77 }
78 if (certReq->certificateReqCtxSize != 0) {
79 BSL_SAL_FREE(ctx->certificateReqCtx);
80 ctx->certificateReqCtx = BSL_SAL_Calloc(certReq->certificateReqCtxSize, sizeof(uint8_t));
81 if (ctx->certificateReqCtx == NULL) {
82 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17039, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
83 "Calloc fail", 0, 0, 0, 0);
84 return HITLS_MEMALLOC_FAIL;
85 }
86 ctx->certificateReqCtxSize = certReq->certificateReqCtxSize;
87 int32_t ret = memcpy_s(ctx->certificateReqCtx, certReq->certificateReqCtxSize,
88 certReq->certificateReqCtx, certReq->certificateReqCtxSize);
89 if (ret != EOK) {
90 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16171, BSL_LOG_LEVEL_WARN, BSL_LOG_BINLOG_TYPE_RUN,
91 "client calloc cert req ctx failed.", 0, 0, 0, 0);
92 BSL_ERR_PUSH_ERROR(HITLS_MEMCPY_FAIL);
93 return HITLS_MEMCPY_FAIL;
94 }
95 }
96 return HITLS_SUCCESS;
97 }
98 #endif /* HITLS_TLS_FEATURE_PHA */
99
100 #ifdef HITLS_TLS_PROTO_TLS13
Tls13ClientRecvCertRequestProcess(TLS_Ctx * ctx,const HS_Msg * msg)101 int32_t Tls13ClientRecvCertRequestProcess(TLS_Ctx *ctx, const HS_Msg *msg)
102 {
103 const CertificateRequestMsg *certReq = &msg->body.certificateReq;
104 int32_t ret = HITLS_SUCCESS;
105
106 ret = HS_CheckReceivedExtension(
107 ctx, CERTIFICATE_REQUEST, certReq->extensionTypeMask, HS_EX_TYPE_TLS1_3_ALLOWED_OF_CERTIFICATE_REQUEST);
108 if (ret != HITLS_SUCCESS) {
109 return ret;
110 }
111
112 #ifdef HITLS_TLS_FEATURE_PHA
113 ret = Tls13ClientStoreCertReqCtx(ctx, certReq);
114 if (ret != HITLS_SUCCESS) {
115 return ret;
116 }
117 #else
118 if (certReq->certificateReqCtxSize != 0) {
119 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15729, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
120 "certificateReqCtxSize is invalid.", 0, 0, 0, 0);
121 ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_ILLEGAL_PARAMETER);
122 return HITLS_MSG_HANDLE_INVALID_CERT_REQ_CTX;
123 }
124 #endif /* HITLS_TLS_FEATURE_PHA */
125 ctx->hsCtx->isNeedClientCert = true;
126 if (certReq->signatureAlgorithms == NULL) {
127 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17040, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
128 "miss signatureAlgorithms extension", 0, 0, 0, 0);
129 ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_MISSING_EXTENSION);
130 return HITLS_MSG_HANDLE_MISSING_EXTENSION;
131 }
132
133 CERT_ExpectInfo expectCertInfo = {0};
134 expectCertInfo.certType = CERT_TYPE_UNKNOWN;
135 expectCertInfo.signSchemeList = certReq->signatureAlgorithms;
136 expectCertInfo.signSchemeNum = certReq->signatureAlgorithmsSize;
137
138 expectCertInfo.caList = certReq->caList;
139 /* If no certificate is selected, the client sends an empty certificate message */
140 (void)SAL_CERT_SelectCertByInfo(ctx, &expectCertInfo);
141 #ifdef HITLS_TLS_FEATURE_PHA
142 if (ctx->phaState == PHA_REQUESTED) {
143 return HS_ChangeState(ctx, TRY_SEND_CERTIFICATE);
144 }
145 #endif /* HITLS_TLS_FEATURE_PHA */
146 return HS_ChangeState(ctx, TRY_RECV_CERTIFICATE);
147 }
148 #endif /* HITLS_TLS_PROTO_TLS13 */
149 #endif /* HITLS_TLS_HOST_CLIENT */