• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IsFileExist(AUTH_RESULT_PATH, NETWORK_CONFIG_FILE_NAME);
102 }
103 
104 // 写入网络配置信息
OEMWriteNetworkConfig(const char * buffer,uint32_t bufferLen)105 int32_t OEMWriteNetworkConfig(const char* buffer, uint32_t bufferLen)
106 {
107     if (CreateFile(AUTH_RESULT_PATH, NETWORK_CONFIG_FILE_NAME) != 0) {
108         return ATTEST_ERR;
109     }
110     return WriteFile(AUTH_RESULT_PATH, NETWORK_CONFIG_FILE_NAME, buffer, bufferLen);
111 }
112 
113 // 读取网络配置信息
OEMReadNetworkConfig(char * buffer,uint32_t bufferLen)114 int32_t OEMReadNetworkConfig(char* buffer, uint32_t bufferLen)
115 {
116     return ReadFile(AUTH_RESULT_PATH, NETWORK_CONFIG_FILE_NAME, buffer, bufferLen);
117 }
118 
119 // 读取默认网络配置信息
OEMReadDefaultNetworkConfig(char * buffer,uint32_t bufferLen)120 int32_t OEMReadDefaultNetworkConfig(char* buffer, uint32_t bufferLen)
121 {
122     return ReadFile(ETC_DEVICE_ATTEST_PATH, NETWORK_CONFIG_FILE_NAME, buffer, bufferLen);
123 }
124 
125 // 写入认证结果
OEMWriteAuthResultCode(const char * data,uint32_t len)126 int32_t OEMWriteAuthResultCode(const char* data, uint32_t len)
127 {
128     if (CreateFile(AUTH_RESULT_PATH, AUTH_RESULT_CODE_FILE_NAME) != 0) {
129         return ATTEST_ERR;
130     }
131     return WriteFile(AUTH_RESULT_PATH, AUTH_RESULT_CODE_FILE_NAME, data, len);
132 }
133 
134 // 读取认证结果
OEMReadAuthResultCode(char * buffer,uint32_t bufferLen)135 int32_t OEMReadAuthResultCode(char* buffer, uint32_t bufferLen)
136 {
137     return ReadFile(AUTH_RESULT_PATH, AUTH_RESULT_CODE_FILE_NAME, buffer, bufferLen);
138 }
139