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_base.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 #define MAX_UINT64_LEN 21
27
GetDataFromBuf(uint8_t ** src,uint32_t * srcLen,uint8_t * dest,uint32_t destLen)28 ResultCode GetDataFromBuf(uint8_t **src, uint32_t *srcLen, uint8_t *dest, uint32_t destLen)
29 {
30 if (src == NULL || *src == NULL || srcLen == NULL || dest == NULL || destLen > *srcLen) {
31 LOG_ERROR("bad parameter.");
32 return RESULT_BAD_PARAM;
33 }
34
35 if (memcpy_s(dest, destLen, *src, destLen) != EOK) {
36 LOG_ERROR("copy fail.");
37 return RESULT_BAD_COPY;
38 }
39
40 *src = *src + destLen;
41 *srcLen = *srcLen - destLen;
42 return RESULT_SUCCESS;
43 }
44
GetBufFromData(uint8_t * src,uint32_t srcLen,uint8_t ** dest,uint32_t * destLen)45 ResultCode GetBufFromData(uint8_t *src, uint32_t srcLen, uint8_t **dest, uint32_t *destLen)
46 {
47 if (src == NULL || dest == NULL || *dest == NULL || destLen == NULL || srcLen > *destLen) {
48 LOG_ERROR("bad parameter.");
49 return RESULT_BAD_PARAM;
50 }
51 if (memcpy_s(*dest, *destLen, src, srcLen) != EOK) {
52 LOG_ERROR("copy fail.");
53 return RESULT_BAD_COPY;
54 }
55
56 *dest = *dest + srcLen;
57 *destLen = *destLen - srcLen;
58 return RESULT_SUCCESS;
59 }
60
GenerateFileName(uint64_t templateId,const char * prefix,const char * suffix,char * fileName,uint32_t fileNameLen)61 ResultCode GenerateFileName(uint64_t templateId, const char *prefix, const char *suffix,
62 char *fileName, uint32_t fileNameLen)
63 {
64 if (prefix == NULL || suffix == NULL || fileName == NULL) {
65 LOG_ERROR("bad parameter.");
66 return RESULT_BAD_PARAM;
67 }
68 if (memset_s(fileName, fileNameLen, 0, fileNameLen) != EOK) {
69 LOG_ERROR("reset fileName fail.");
70 return RESULT_PIN_FAIL;
71 }
72 char *buf = fileName;
73 uint32_t bufLen = fileNameLen;
74 if (GetBufFromData((uint8_t *)prefix, strlen(prefix), (uint8_t **)(&buf), &bufLen) != RESULT_SUCCESS) {
75 LOG_ERROR("copy prefix fail.");
76 return RESULT_BAD_COPY;
77 }
78 char templateIdStr[MAX_UINT64_LEN] = {'\0'};
79 if (snprintf_s(templateIdStr, MAX_UINT64_LEN, MAX_UINT64_LEN - 1, "%" PRIu64, templateId) < 0) {
80 LOG_ERROR("templateIdStr error.");
81 return RESULT_UNKNOWN;
82 }
83 if (GetBufFromData((uint8_t *)templateIdStr, strlen(templateIdStr),
84 (uint8_t **)(&buf), &bufLen) != RESULT_SUCCESS) {
85 LOG_ERROR("copy templateIdStr fail.");
86 return RESULT_BAD_COPY;
87 }
88 if (GetBufFromData((uint8_t *)suffix, strlen(suffix), (uint8_t **)&buf, &bufLen) != RESULT_SUCCESS) {
89 LOG_ERROR("copy suffix fail.");
90 return RESULT_BAD_COPY;
91 }
92 if (bufLen == 0) {
93 LOG_ERROR("no space for endl.");
94 return RESULT_BAD_COPY;
95 }
96
97 LOG_INFO("GenerateFileName succ.");
98 return RESULT_SUCCESS;
99 }
100
101 /* This is for example only, Should be implemented in trusted environment. */
ReadPinFile(uint8_t * data,uint32_t dataLen,uint64_t templateId,const char * suffix)102 ResultCode ReadPinFile(uint8_t *data, uint32_t dataLen, uint64_t templateId, const char *suffix)
103 {
104 if (data == NULL || suffix == NULL) {
105 LOG_ERROR("bad parameter.");
106 return RESULT_BAD_PARAM;
107 }
108 FileOperator *fileOp = GetFileOperator(DEFAULT_FILE_OPERATOR);
109 if (!IsFileOperatorValid(fileOp)) {
110 LOG_ERROR("fileOp invalid.");
111 return RESULT_GENERAL_ERROR;
112 }
113
114 char fileName[MAX_FILE_NAME_LEN] = {'\0'};
115 ResultCode ret = GenerateFileName(templateId, DEFAULT_FILE_HEAD, suffix, fileName, MAX_FILE_NAME_LEN);
116 if (ret != RESULT_SUCCESS) {
117 LOG_ERROR("ReadPinFile Generate Pin FileName fail.");
118 return RESULT_GENERAL_ERROR;
119 }
120 ret = (ResultCode)fileOp->readFile(fileName, data, dataLen);
121 if (ret != RESULT_SUCCESS) {
122 LOG_ERROR("read pin file fail.");
123 return ret;
124 }
125
126 return RESULT_SUCCESS;
127 }
128
129 /* This is for example only, Should be implemented in trusted environment. */
WritePinFile(const uint8_t * data,uint32_t dataLen,uint64_t templateId,const char * suffix)130 ResultCode WritePinFile(const uint8_t *data, uint32_t dataLen, uint64_t templateId, const char *suffix)
131 {
132 if (data == NULL || suffix == NULL) {
133 LOG_ERROR("bad parameter.");
134 return RESULT_BAD_PARAM;
135 }
136 FileOperator *fileOp = GetFileOperator(DEFAULT_FILE_OPERATOR);
137 if (!IsFileOperatorValid(fileOp)) {
138 LOG_ERROR("fileOp invalid.");
139 return RESULT_GENERAL_ERROR;
140 }
141
142 char fileName[MAX_FILE_NAME_LEN] = {'\0'};
143 ResultCode ret = GenerateFileName(templateId, DEFAULT_FILE_HEAD, suffix, fileName, MAX_FILE_NAME_LEN);
144 if (ret != RESULT_SUCCESS) {
145 LOG_ERROR("WritePinFile Generate Pin FileName fail.");
146 return RESULT_GENERAL_ERROR;
147 }
148 ret = (ResultCode)fileOp->writeFile(fileName, data, dataLen);
149 if (ret != RESULT_SUCCESS) {
150 LOG_ERROR("WritePinFile fail.");
151 return ret;
152 }
153 LOG_INFO("WritePinFile succ.");
154
155 return RESULT_SUCCESS;
156 }
157