1 /*
2 * Copyright (C) 2022 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 <securec.h>
17 #include "attest_utils_file.h"
18 #include "attest_adapter_oem.h"
19
20 // 是否存在标记
OEMIsFlagExist(OEM_FLAG_TYPE type)21 bool OEMIsFlagExist(OEM_FLAG_TYPE type)
22 {
23 bool result = false;
24 switch (type) {
25 case OEM_FLAG_RESET:
26 result = IsFileExist(AUTH_RESULT_PATH, RESET_FLAG_FILE_NAME);
27 break;
28 default:
29 break;
30 }
31 return result;
32 }
33
34 // 创建标记
OEMCreateFlag(OEM_FLAG_TYPE type)35 int32_t OEMCreateFlag(OEM_FLAG_TYPE type)
36 {
37 int32_t result = ATTEST_ERR;
38 switch (type) {
39 case OEM_FLAG_RESET:
40 result = CreateFile(AUTH_RESULT_PATH, RESET_FLAG_FILE_NAME);
41 break;
42 default:
43 break;
44 }
45 return result;
46 }
47
48 // 写入认证结果
OEMWriteAuthStatus(const char * data,uint32_t len)49 int32_t OEMWriteAuthStatus(const char* data, uint32_t len)
50 {
51 if (CreateFile(AUTH_RESULT_PATH, AUTH_STATUS_FILE_NAME) != 0) {
52 return ATTEST_ERR;
53 }
54 return WriteFile(AUTH_RESULT_PATH, AUTH_STATUS_FILE_NAME, data, len);
55 }
56
57 // 读取认证结果
OEMReadAuthStatus(char * buffer,uint32_t bufferLen)58 int32_t OEMReadAuthStatus(char* buffer, uint32_t bufferLen)
59 {
60 return ReadFile(AUTH_RESULT_PATH, AUTH_STATUS_FILE_NAME, buffer, bufferLen);
61 }
62
63 // 读取认证结果长度
OEMGetAuthStatusFileSize(uint32_t * len)64 int32_t OEMGetAuthStatusFileSize(uint32_t* len)
65 {
66 return GetFileSize(AUTH_RESULT_PATH, AUTH_STATUS_FILE_NAME, len);
67 }
68
69 // 读取凭据
OEMReadTicket(TicketInfo * ticketInfo)70 int32_t OEMReadTicket(TicketInfo* ticketInfo)
71 {
72 char ticket[TICKET_ENCRYPT_LEN + SALT_ENCRYPT_LEN] = {0};
73 if (ReadFile(AUTH_RESULT_PATH, TICKET_FILE_NAME, ticket, sizeof(ticket)) != 0) {
74 return ATTEST_ERR;
75 }
76 if (memcpy_s(ticketInfo->ticket, TICKET_ENCRYPT_LEN, ticket, TICKET_ENCRYPT_LEN) != 0 ||
77 memcpy_s(ticketInfo->salt, SALT_ENCRYPT_LEN, ticket + TICKET_ENCRYPT_LEN, SALT_ENCRYPT_LEN) != 0) {
78 return ATTEST_ERR;
79 }
80 return ATTEST_OK;
81 }
82
83 // 写入凭据
OEMWriteTicket(const TicketInfo * ticketInfo)84 int32_t OEMWriteTicket(const TicketInfo* ticketInfo)
85 {
86 char ticket[TICKET_ENCRYPT_LEN + SALT_ENCRYPT_LEN] = {0};
87 if (memcpy_s(ticket, TICKET_ENCRYPT_LEN, ticketInfo->ticket, TICKET_ENCRYPT_LEN) != 0 ||
88 memcpy_s(ticket + TICKET_ENCRYPT_LEN, SALT_ENCRYPT_LEN, ticketInfo->salt, SALT_ENCRYPT_LEN) != 0) {
89 return ATTEST_ERR;
90 }
91
92 if (CreateFile(AUTH_RESULT_PATH, TICKET_FILE_NAME) != 0) {
93 return ATTEST_ERR;
94 }
95 return WriteFile(AUTH_RESULT_PATH, TICKET_FILE_NAME, ticket, sizeof(ticket));
96 }
97
98 // 是否存在网络配置信息
OEMIsNetworkConfigExist(void)99 bool OEMIsNetworkConfigExist(void)
100 {
101 return false;
102 }
103
104 // 写入网络配置信息
OEMWriteNetworkConfig(const char * buffer,uint32_t bufferLen)105 int32_t OEMWriteNetworkConfig(const char* buffer, uint32_t bufferLen)
106 {
107 (void)buffer;
108 (void)bufferLen;
109 return 0;
110 }
111
112 // 读取网络配置信息
OEMReadNetworkConfig(char * buffer,uint32_t bufferLen)113 int32_t OEMReadNetworkConfig(char* buffer, uint32_t bufferLen)
114 {
115 (void)buffer;
116 (void)bufferLen;
117 return 0;
118 }
119
120 // 读取默认网络配置信息
OEMReadDefaultNetworkConfig(char * buffer,uint32_t bufferLen)121 int32_t OEMReadDefaultNetworkConfig(char* buffer, uint32_t bufferLen)
122 {
123 (void)buffer;
124 (void)bufferLen;
125 return 0;
126 }
127
128 // 写入认证结果
OEMWriteAuthResultCode(const char * data,uint32_t len)129 int32_t OEMWriteAuthResultCode(const char* data, uint32_t len)
130 {
131 if (CreateFile(AUTH_RESULT_PATH, AUTH_RESULT_CODE_FILE_NAME) != 0) {
132 return ATTEST_ERR;
133 }
134 return WriteFile(AUTH_RESULT_PATH, AUTH_RESULT_CODE_FILE_NAME, data, len);
135 }
136
137 // 读取认证结果
OEMReadAuthResultCode(char * buffer,uint32_t bufferLen)138 int32_t OEMReadAuthResultCode(char* buffer, uint32_t bufferLen)
139 {
140 return ReadFile(AUTH_RESULT_PATH, AUTH_RESULT_CODE_FILE_NAME, buffer, bufferLen);
141 }
142
143 // 写入满载情况
OEMWriteFullLoadStatus(const char * data,uint32_t len)144 int32_t OEMWriteFullLoadStatus(const char* data, uint32_t len)
145 {
146 if (CreateFile(AUTH_RESULT_PATH, FULL_LOAD_STATUS_FILE_NAME) != 0) {
147 return ATTEST_ERR;
148 }
149 return WriteFile(AUTH_RESULT_PATH, FULL_LOAD_STATUS_FILE_NAME, data, len);
150 }
151
152 // 读取满载情况
OEMReadFullLoadStatus(char * buffer,uint32_t bufferLen)153 int32_t OEMReadFullLoadStatus(char* buffer, uint32_t bufferLen)
154 {
155 return ReadFile(AUTH_RESULT_PATH, FULL_LOAD_STATUS_FILE_NAME, buffer, bufferLen);
156 }
157