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