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 
16 #include "pin_db_ops_v0.h"
17 
18 #include <inttypes.h>
19 #include "securec.h"
20 
21 #include "adaptor_file.h"
22 #include "adaptor_log.h"
23 #include "adaptor_memory.h"
24 #include "adaptor_time.h"
25 #include "file_operator.h"
26 
27 #include "pin_db_ops_base.h"
28 
GetMaxLockedAntiBruteInfo(AntiBruteInfoV0 * antiBruteInfoV0)29 void GetMaxLockedAntiBruteInfo(AntiBruteInfoV0 *antiBruteInfoV0)
30 {
31     if (antiBruteInfoV0 == NULL) {
32         return;
33     }
34     antiBruteInfoV0->authErrorCount = ATTI_BRUTE_SECOND_STAGE;
35     antiBruteInfoV0->startFreezeTime = GetRtcTime();
36 }
37 
GetPinIndexV0(uint8_t * data,uint32_t dataLen,PinDbV0 * pinDbV0)38 static ResultCode GetPinIndexV0(uint8_t *data, uint32_t dataLen, PinDbV0 *pinDbV0)
39 {
40     if (sizeof(PinInfoV0) * pinDbV0->pinIndexLen != dataLen) {
41         LOG_ERROR("bad data length.");
42         return RESULT_GENERAL_ERROR;
43     }
44     pinDbV0->pinIndex = (PinIndexV0 *)Malloc(sizeof(PinIndexV0) * pinDbV0->pinIndexLen);
45     if (pinDbV0->pinIndex == NULL) {
46         LOG_ERROR("pinIndex malloc fail.");
47         return RESULT_NO_MEMORY;
48     }
49     uint8_t *temp = data;
50     uint32_t tempLen = dataLen;
51     for (uint32_t i = 0; i < pinDbV0->pinIndexLen; i++) {
52         if (GetDataFromBuf(&temp, &tempLen, (uint8_t *)(&(pinDbV0->pinIndex[i].pinInfo)),
53             sizeof(pinDbV0->pinIndex[i].pinInfo)) != RESULT_SUCCESS) {
54             LOG_ERROR("read pinInfo fail.");
55             Free(pinDbV0->pinIndex);
56             pinDbV0->pinIndex = NULL;
57             return RESULT_BAD_READ;
58         }
59         if (ReadPinFile((uint8_t *)(&(pinDbV0->pinIndex[i].antiBruteInfo)),
60             sizeof(pinDbV0->pinIndex[i].antiBruteInfo),
61             pinDbV0->pinIndex[i].pinInfo.templateId, ANTI_BRUTE_SUFFIX) != RESULT_SUCCESS) {
62             LOG_ERROR("read AntiBruteInfo fail.");
63             GetMaxLockedAntiBruteInfo(&(pinDbV0->pinIndex[i].antiBruteInfo));
64             (void)WritePinFile((uint8_t *)(&(pinDbV0->pinIndex[i].antiBruteInfo)),
65                 sizeof(pinDbV0->pinIndex[i].antiBruteInfo),
66                 pinDbV0->pinIndex[i].pinInfo.templateId, ANTI_BRUTE_SUFFIX);
67         }
68     }
69     return RESULT_SUCCESS;
70 }
71 
UnpackPinDbV0(uint8_t * data,uint32_t dataLen,PinDbV0 * pinDbV0)72 static bool UnpackPinDbV0(uint8_t *data, uint32_t dataLen, PinDbV0 *pinDbV0)
73 {
74     uint8_t *temp = data;
75     uint32_t tempLen = dataLen;
76     if (GetDataFromBuf(&temp, &tempLen, (uint8_t *)(&(pinDbV0->version)),
77         sizeof(pinDbV0->version)) != RESULT_SUCCESS) {
78         LOG_ERROR("read version fail.");
79         return false;
80     }
81     if (pinDbV0->version != DB_VERSION_0) {
82         LOG_ERROR("read version %{public}u.", pinDbV0->version);
83         return false;
84     }
85     if (GetDataFromBuf(&temp, &tempLen, (uint8_t *)(&(pinDbV0->pinIndexLen)),
86         sizeof(pinDbV0->pinIndexLen)) != RESULT_SUCCESS) {
87         LOG_ERROR("read pinIndexLen fail.");
88         return false;
89     }
90     if (pinDbV0->pinIndexLen > MAX_CRYPTO_INFO_SIZE) {
91         pinDbV0->pinIndexLen = 0;
92         LOG_ERROR("pinIndexLen too large.");
93         return false;
94     }
95     if (pinDbV0->pinIndexLen == 0) {
96         pinDbV0->pinIndex = NULL;
97         return true;
98     }
99     if (GetPinIndexV0(temp, tempLen, pinDbV0) != RESULT_SUCCESS) {
100         pinDbV0->pinIndexLen = 0;
101         LOG_ERROR("GetPinIndexV0 fail.");
102         return false;
103     }
104     return true;
105 }
106 
GetPinDbV0(uint8_t * data,uint32_t dataLen)107 void *GetPinDbV0(uint8_t *data, uint32_t dataLen)
108 {
109     if (data == NULL || dataLen == 0) {
110         LOG_INFO("no data provided");
111         return NULL;
112     }
113     PinDbV0 *pinDbV0 = Malloc(sizeof(PinDbV0));
114     if (pinDbV0 == NULL) {
115         LOG_ERROR("get pinDbV0 fail");
116         return NULL;
117     }
118     (void)memset_s(pinDbV0, sizeof(PinDbV0), 0, sizeof(PinDbV0));
119     if (!UnpackPinDbV0(data, dataLen, pinDbV0)) {
120         LOG_ERROR("UnpackPinDbV0 fail");
121         FreePinDbV0((void **)(&pinDbV0));
122         return NULL;
123     }
124     return pinDbV0;
125 }
126 
FreePinDbV0(void ** pinDb)127 void FreePinDbV0(void **pinDb)
128 {
129     if (pinDb == NULL) {
130         return;
131     }
132     PinDbV0 *pinDbV0 = *pinDb;
133     if (pinDbV0 == NULL) {
134         return;
135     }
136     if (pinDbV0->pinIndex != NULL) {
137         Free(pinDbV0->pinIndex);
138     }
139     Free(*pinDb);
140     *pinDb = NULL;
141 }
142