1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "securec.h"
16 #include "attest_channel.h"
17 #include "attest_tls.h"
18 #include "attest_utils.h"
19 #include "attest_utils_log.h"
20 #include "attest_tdd_mock_config.h"
21 #include "attest_tdd_data_transfer.h"
22
23 int32_t g_netType = 0;
24
25 #define MAX_INVOKE_TIME 3
26 #define MAX_NO_EXTEND_TIME 2
27 #define INTERFACE_COUNT 4
28
29 // can't change network
30 #define ATTEST_RESET_CHAP_FIRST_MSG "210,0"
31 #define ATTEST_RESET_CHAP_SECOND_MSG "227,49"
32 #define ATTEST_RESET_CHAP_THIRD_MSG "69,166,93,255,123,34,99,104,97,108,108,101,110,103,101,34,58,34,51,57,\
33 97,57,100,48,52,100,52,49,54,49,55,49,54,50,56,57,51,99,51,51,49,50,99,101,98,48,51,48,97,99,97,99,56,100,56,\
34 98,100,48,99,99,57,102,99,101,98,99,97,98,53,52,48,50,97,52,51,56,57,49,51,52,49,100,34,44,34,99,117,114,114,\
35 101,110,116,84,105,109,101,34,58,49,54,56,48,55,56,49,54,55,49,50,50,54,44,34,101,114,114,99,111,100,101,34,58,\
36 48,44,34,115,101,114,118,101,114,73,110,102,111,34,58,123,34,97,99,116,105,118,101,83,105,116,101,34,58,34,119,\
37 105,115,101,100,101,118,105,99,101,45,108,105,116,101,45,100,114,99,110,46,111,112,101,110,104,97,114,109,111,\
38 110,121,46,99,110,34,44,34,115,116,97,110,100,98,121,83,105,116,101,34,58,34,119,105,115,101,100,101,118,105,99,\
39 101,45,108,105,116,101,45,100,114,99,110,46,111,112,101,110,104,97,114,109,111,110,121,46,99,110,34,125,125,0"
40
41 #define ATTEST_REST_ERROR_FIRST_MSG "210,0"
42 #define ATTEST_REST_ERROR_SECOND_MSG "5,49"
43 #define ATTEST_REST_ERROR_THIRD_MSG "69,128,194,255,123,34,101,114,114,99,111,100,101,34,58,49,53,48,48,51,125,0"
44
45 #define ATTEST_AUTH_FIRST_MSG "226,0"
46 #define ATTEST_AUTH_SECOND_MSG "1,185,54"
47 #define ATTEST_AUTH_THIRD_MSG "69,184,100,255,123,34,99,104,97,108,108,101,110,103,101,34,58,34,97,56,49,52,\
48 52,49,101,51,99,48,100,56,100,54,97,55,56,57,48,55,102,97,48,56,56,56,102,57,50,52,49,98,101,57,53,57,49,99,52,100,54,\
49 98,55,97,53,51,51,51,49,56,98,48,49,48,102,98,50,99,51,100,57,98,56,48,34,44,34,99,117,114,114,101,110,116,84,105,109,\
50 101,34,58,49,54,56,48,55,56,49,54,55,49,52,53,53,44,34,101,114,114,99,111,100,101,34,58,48,125,108"
51
52 #define ATTEST_ACTIVE_FIRST_MSG "210,0"
53 #define ATTEST_ACTIVE_SECOND_MSG "1,49"
54 #define ATTEST_ACTIVE_THIRD_MSG "69,254,246,255,123,34,101,114,114,99,111,100,101,34,58,48,125,0"
55
56 static const char *mockTlsData[MAX_INVOKE_TIME][INTERFACE_COUNT] = {
57 {ATTEST_RESET_CHAP_FIRST_MSG, ATTEST_REST_ERROR_FIRST_MSG, ATTEST_ACTIVE_FIRST_MSG, ATTEST_AUTH_FIRST_MSG},
58 {ATTEST_RESET_CHAP_SECOND_MSG, ATTEST_REST_ERROR_SECOND_MSG, ATTEST_ACTIVE_SECOND_MSG, ATTEST_AUTH_SECOND_MSG},
59 {ATTEST_RESET_CHAP_THIRD_MSG, ATTEST_REST_ERROR_THIRD_MSG, ATTEST_ACTIVE_THIRD_MSG, ATTEST_AUTH_THIRD_MSG}
60 };
61 bool isHasExtend[4] = {true, true, true, true};
62 int g_cout = 0;
63
TLSConnect(TLSSession * session)64 int32_t TLSConnect(TLSSession* session)
65 {
66 ATTEST_LOG_DEBUG("[TLSConnect mock] Begin.");
67 if (session == NULL) {
68 return ERR_NET_INVALID_ARG;
69 }
70 return ATTEST_OK;
71 }
72
TLSWrite(const TLSSession * session,const uint8_t * buf,size_t len)73 int32_t TLSWrite(const TLSSession* session, const uint8_t* buf, size_t len)
74 {
75 (void)session;
76 (void)buf;
77 (void)len;
78 return ATTEST_OK;
79 }
80
TLSRead(const TLSSession * session,uint8_t * buf,size_t len)81 int32_t TLSRead(const TLSSession* session, uint8_t* buf, size_t len)
82 {
83 (void)session;
84 (void)len;
85 if (g_cout > MAX_INVOKE_TIME || (g_netType < 0 || g_netType > INTERFACE_COUNT - 1)) {
86 return ATTEST_ERR;
87 }
88 int32_t ret = AttestSeriaToBinary(mockTlsData[g_cout][g_netType], &buf, len);
89 g_cout++;
90 int32_t maxInvokeTime = isHasExtend[g_netType] ? MAX_INVOKE_TIME : MAX_NO_EXTEND_TIME;
91 if (g_cout == maxInvokeTime) {
92 g_cout = 0;
93 }
94 return ret;
95 }
96
TLSClose(TLSSession * session)97 int32_t TLSClose(TLSSession* session)
98 {
99 (void)session;
100 return ATTEST_OK;
101 }
102