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
16 #include <stdbool.h>
17 #include "hitls_build.h"
18 #include "hitls_error.h"
19 #include "bsl_err_internal.h"
20 #include "tls_binlog_id.h"
21 #include "hitls_type.h"
22 #include "rec.h"
23 #include "hs.h"
24 #include "app.h"
25 #include "alert.h"
26 #include "change_cipher_spec.h"
27 #include "conn_common.h"
28 #include "hs_ctx.h"
29 // an instance of unexpectedMsgProcessCb
ConnUnexpectedMsg(HITLS_Ctx * ctx,uint32_t msgType,const uint8_t * data,uint32_t dataLen,bool isPlain)30 int32_t ConnUnexpectedMsg(HITLS_Ctx *ctx, uint32_t msgType, const uint8_t *data, uint32_t dataLen, bool isPlain)
31 {
32 (void)isPlain;
33 if (ctx == NULL || data == NULL) {
34 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16509, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
35 BSL_ERR_PUSH_ERROR(HITLS_NULL_INPUT);
36 return HITLS_NULL_INPUT;
37 }
38 if (msgType != REC_TYPE_ALERT) {
39 ALERT_ClearWarnCount(ctx);
40 }
41 int32_t ret = HITLS_REC_NORMAL_RECV_UNEXPECT_MSG;
42 #ifdef HITLS_TLS_PROTO_TLS13
43 if (isPlain) { // tls13
44 if (msgType == REC_TYPE_CHANGE_CIPHER_SPEC) {
45 return ProcessPlainCCS(ctx, data, dataLen);
46 }
47 return ProcessPlainAlert(ctx, data, dataLen);
48 }
49 #endif
50 switch (msgType) {
51 case REC_TYPE_CHANGE_CIPHER_SPEC:
52 return ProcessDecryptedCCS(ctx, data, dataLen);
53 case REC_TYPE_ALERT:
54 return ProcessDecryptedAlert(ctx, data, dataLen);
55 default:
56 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16512, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
57 "unknown msgType", 0, 0, 0, 0);
58 ALERT_Send(ctx, ALERT_LEVEL_FATAL, ALERT_UNEXPECTED_MESSAGE);
59 break;
60 }
61 return ret;
62 }
63
CONN_Init(TLS_Ctx * ctx)64 int32_t CONN_Init(TLS_Ctx *ctx)
65 {
66 int32_t ret = REC_Init(ctx);
67 if (ret != HITLS_SUCCESS) {
68 return ret;
69 }
70
71 ret = ALERT_Init(ctx);
72 if (ret != HITLS_SUCCESS) {
73 return ret;
74 }
75
76 ret = CCS_Init(ctx);
77 if (ret != HITLS_SUCCESS) {
78 return ret;
79 }
80
81 ret = HS_Init(ctx);
82 if (ret != HITLS_SUCCESS) {
83 return ret;
84 }
85
86 ctx->method.isRecvCCS = CCS_IsRecv;
87 ctx->method.sendCCS = CCS_Send;
88 ctx->method.ctrlCCS = CCS_Ctrl;
89 ctx->method.sendAlert = ALERT_Send;
90 ctx->method.getAlertFlag = ALERT_GetFlag;
91 ctx->method.unexpectedMsgProcessCb = ConnUnexpectedMsg;
92 #ifdef HITLS_TLS_FEATURE_KEY_UPDATE
93 ctx->keyUpdateType = HITLS_KEY_UPDATE_REQ_END;
94 ctx->isKeyUpdateRequest = false;
95 #endif
96 // default value is X509_V_OK(0)
97 ctx->peerInfo.verifyResult = 0;
98 #ifdef HITLS_TLS_CONFIG_STATE
99 ctx->rwstate = HITLS_NOTHING;
100 #endif
101 return HITLS_SUCCESS;
102 }
103
CONN_Deinit(TLS_Ctx * ctx)104 void CONN_Deinit(TLS_Ctx *ctx)
105 {
106 REC_DeInit(ctx);
107 ALERT_Deinit(ctx);
108 CCS_DeInit(ctx);
109 HS_DeInit(ctx);
110 return;
111 }