• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PROTO_TLS13
17 #ifdef HITLS_TLS_HOST_CLIENT
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 "hitls_error.h"
25 #include "tls.h"
26 #include "record.h"
27 #include "hs_ctx.h"
28 #include "hs_common.h"
29 #include "hs_extensions.h"
30 #include "hs_msg.h"
31 #include "hs_verify.h"
32 #include "alpn.h"
33 
34 
35 typedef int32_t (*CheckEncryptedExtFunc)(TLS_Ctx *ctx, const EncryptedExtensions *eEMsg);
36 #ifdef HITLS_TLS_FEATURE_SNI
Tls13ClientCheckServerName(TLS_Ctx * ctx,const EncryptedExtensions * eEMsg)37 static int32_t Tls13ClientCheckServerName(TLS_Ctx *ctx, const EncryptedExtensions *eEMsg)
38 {
39     if ((ctx->hsCtx->extFlag.haveServerName == false) && (eEMsg->haveServerName == true)) {
40         BSL_ERR_PUSH_ERROR(HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE);
41         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16200, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
42             "client did not send server_name but get extended server_name .", 0, 0, 0, 0);
43         ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_UNSUPPORTED_EXTENSION);
44         return HITLS_MSG_HANDLE_UNSUPPORT_EXTENSION_TYPE;
45     }
46 
47     /* Receive empty server_name extension */
48     if ((ctx->hsCtx->extFlag.haveServerName == true) && (eEMsg->haveServerName == true)) {
49         /* Not in session resumption and the client has previously sent the server_name extension */
50         if (ctx->session == NULL && ctx->config.tlsConfig.serverName != NULL &&
51             ctx->config.tlsConfig.serverNameSize > 0) {
52             /* Indicates server negotiated the server_name extension in client successfully */
53             ctx->negotiatedInfo.isSniStateOK = true;
54             ctx->hsCtx->serverNameSize = ctx->config.tlsConfig.serverNameSize;
55 
56             BSL_SAL_FREE(ctx->hsCtx->serverName);
57             ctx->hsCtx->serverName =
58                 (uint8_t *)BSL_SAL_Dump(ctx->config.tlsConfig.serverName, ctx->hsCtx->serverNameSize * sizeof(uint8_t));
59             if (ctx->hsCtx->serverName == NULL) {
60                 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17075, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
61                     "Dump fail", 0, 0, 0, 0);
62                 return HITLS_MEMCPY_FAIL;
63             }
64         }
65     }
66 
67     return HITLS_SUCCESS;
68 }
69 #endif /* HITLS_TLS_FEATURE_SNI */
70 
71 #ifdef HITLS_TLS_FEATURE_ALPN
Tls13ClientCheckNegotiatedAlpn(TLS_Ctx * ctx,const EncryptedExtensions * eEMsg)72 static int32_t Tls13ClientCheckNegotiatedAlpn(TLS_Ctx *ctx, const EncryptedExtensions *eEMsg)
73 {
74     return ClientCheckNegotiatedAlpn(
75         ctx, eEMsg->haveSelectedAlpn, eEMsg->alpnSelected, eEMsg->alpnSelectedSize);
76 }
77 #endif
78 
ClientCheckEncryptedExtensionsFlag(TLS_Ctx * ctx,const EncryptedExtensions * eEMsg)79 static int32_t ClientCheckEncryptedExtensionsFlag(TLS_Ctx *ctx, const EncryptedExtensions *eEMsg)
80 {
81     static const CheckEncryptedExtFunc EXT_INFO_LIST[] = {
82 #ifdef HITLS_TLS_FEATURE_SNI
83         Tls13ClientCheckServerName,
84 #endif /* HITLS_TLS_FEATURE_SNI */
85 #ifdef HITLS_TLS_FEATURE_ALPN
86         Tls13ClientCheckNegotiatedAlpn,
87 #endif
88         NULL,
89     };
90 
91     int32_t ret;
92     ret = HS_CheckReceivedExtension(ctx, ENCRYPTED_EXTENSIONS, eEMsg->extensionTypeMask,
93         HS_EX_TYPE_TLS1_3_ALLOWED_OF_ENCRYPTED_EXTENSIONS);
94     if (ret != HITLS_SUCCESS) {
95         return ret;
96     }
97     for (uint32_t i = 0; i < sizeof(EXT_INFO_LIST) / sizeof(EXT_INFO_LIST[0]); i++) {
98         if (EXT_INFO_LIST[i] == NULL) {
99             continue;
100         }
101         ret = EXT_INFO_LIST[i](ctx, eEMsg);
102         if (ret != HITLS_SUCCESS) {
103             return ret;
104         }
105     }
106 
107     return HITLS_SUCCESS;
108 }
109 
Tls13ClientRecvEncryptedExtensionsProcess(TLS_Ctx * ctx,const HS_Msg * msg)110 int32_t Tls13ClientRecvEncryptedExtensionsProcess(TLS_Ctx *ctx, const HS_Msg *msg)
111 {
112     int32_t ret;
113 
114     const EncryptedExtensions *eEMsg = &msg->body.encryptedExtensions;
115     // Process the extension.
116     ret = ClientCheckEncryptedExtensionsFlag(ctx, eEMsg);
117     if (ret != HITLS_SUCCESS) {
118         return ret;
119     }
120 
121     /* In psk_only mode, the 'server verify data' needs to be calculated
122      * for verifying the 'finished' message from the server. */
123     PskInfo13 *pskInfo = &ctx->hsCtx->kxCtx->pskInfo13;
124     if ((pskInfo->psk != NULL)) {
125         ret = VERIFY_Tls13CalcVerifyData(ctx, false);
126         if (ret != HITLS_SUCCESS) {
127             BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15856, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
128                 "client calculate server finished data error.", 0, 0, 0, 0);
129             ctx->method.sendAlert(ctx, ALERT_LEVEL_FATAL, ALERT_INTERNAL_ERROR);
130             return ret;
131         }
132 
133         return HS_ChangeState(ctx, TRY_RECV_FINISH);
134     }
135 
136     return HS_ChangeState(ctx, TRY_RECV_CERTIFICATE_REQUEST);
137 }
138 #endif /* HITLS_TLS_HOST_CLIENT */
139 #endif /* HITLS_TLS_PROTO_TLS13 */