• 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 "device_attest_oem_file.h"
18 #include "device_attest_oem_adapter.h"
19 
20 static const char g_tokenMagicNum[] = {1, 2, 3, 4};
21 #define TOKEN_MAGIC_NUM_SIZE (sizeof(g_tokenMagicNum)/sizeof(g_tokenMagicNum[0]))
22 
ReadTokenWithFlag(char * path,char * fileName,char * result,unsigned int len)23 static int ReadTokenWithFlag(char* path, char* fileName, char* result, unsigned int len)
24 {
25     const unsigned int buffLen = TOKEN_MAGIC_NUM_SIZE + TOKEN_WITH_FLAG_SIZE + 1;
26     char buf[buffLen];
27     (void)memset_s(buf, buffLen, 0, buffLen);
28     if (OEMReadFile(path, fileName, buf, buffLen) != 0) {
29         return DEVICE_ATTEST_OEM_ERR;
30     }
31     int isTokenValid = 1;
32     for (unsigned int i = 0; i < TOKEN_MAGIC_NUM_SIZE; i++) {
33         if (buf[i] != g_tokenMagicNum[i]) {
34             isTokenValid = 0;
35             break;
36         }
37     }
38     if (isTokenValid == 0) {
39         return DEVICE_ATTEST_OEM_ERR;
40     }
41     (void)memcpy_s(result, TOKEN_WITH_FLAG_SIZE, buf + TOKEN_MAGIC_NUM_SIZE, TOKEN_WITH_FLAG_SIZE);
42     return DEVICE_ATTEST_OEM_OK;
43 }
44 
WriteTokenWithFlag(char * path,char * fileName,const char * tokenWithFlag,unsigned int len)45 static int WriteTokenWithFlag(char* path, char* fileName, const char* tokenWithFlag, unsigned int len)
46 {
47     const unsigned int buffLen = TOKEN_MAGIC_NUM_SIZE + TOKEN_WITH_FLAG_SIZE + 1;
48     char buf[buffLen];
49     (void)memset_s(buf, buffLen, 0, buffLen);
50 
51     for (unsigned int i = 0; i < TOKEN_MAGIC_NUM_SIZE; i++) {
52         buf[i] = g_tokenMagicNum[i];
53     }
54     (void)memcpy_s(buf + TOKEN_MAGIC_NUM_SIZE, TOKEN_WITH_FLAG_SIZE, tokenWithFlag, TOKEN_WITH_FLAG_SIZE);
55 
56     if (OEMCreateFile(path, fileName) != 0) {
57         return DEVICE_ATTEST_OEM_ERR;
58     }
59     return OEMWriteFile(path, fileName, buf, buffLen);
60 }
61 
GetTokenFlag(const char tokenWithFlag[])62 static uint32_t GetTokenFlag(const char tokenWithFlag[])
63 {
64     unsigned result = 0;
65     for (unsigned int i = 0; i < TOKEN_FLAG_SIZE; i++) {
66         result |= ((uint8_t)tokenWithFlag[TOKEN_SIZE + i]) << ((TOKEN_FLAG_SIZE - 1 - i) * BITS_PER_BYTE);
67     }
68     return result;
69 }
70 
SetTokenFlag(unsigned char flag[],uint32_t value)71 static void SetTokenFlag(unsigned char flag[], uint32_t value)
72 {
73     for (unsigned int i = 0; i < TOKEN_FLAG_SIZE; i++) {
74         flag[i] = (value >> (BITS_PER_BYTE * (TOKEN_FLAG_SIZE - 1 - i))) & 0xFF;
75     }
76 }
77 
OEMReadToken(char * token,uint32_t len)78 int32_t OEMReadToken(char *token, uint32_t len)
79 {
80     if (token == NULL || len == 0) {
81         return DEVICE_ATTEST_OEM_ERR;
82     }
83     char tokenWithFlagA[TOKEN_WITH_FLAG_SIZE] = {0};
84     char tokenWithFlagB[TOKEN_WITH_FLAG_SIZE] = {0};
85     int32_t retA = ReadTokenWithFlag(TOKEN_ADDR, TOKEN_A_ADDR, tokenWithFlagA, TOKEN_WITH_FLAG_SIZE);
86     int32_t retB = ReadTokenWithFlag(TOKEN_ADDR, TOKEN_B_ADDR, tokenWithFlagB, TOKEN_WITH_FLAG_SIZE);
87     if ((retA != DEVICE_ATTEST_OEM_OK) && (retB != DEVICE_ATTEST_OEM_OK)) {
88         // Files tokenA and tokenB do not exist
89         return DEVICE_ATTEST_OEM_UNPRESET;
90     } else if ((retA == DEVICE_ATTEST_OEM_OK) && (retB != DEVICE_ATTEST_OEM_OK)) {
91         // File tokenA exist, and file tokenB does not exist
92         (void)memcpy_s(token, len, tokenWithFlagA, len);
93         return DEVICE_ATTEST_OEM_OK;
94     } else if ((retA != DEVICE_ATTEST_OEM_OK) && (retB == DEVICE_ATTEST_OEM_OK)) {
95         // File tokenB exist, and file tokenA does not exist
96         (void)memcpy_s(token, len, tokenWithFlagB, len);
97         return DEVICE_ATTEST_OEM_OK;
98     } else {
99         // Both files tokenA and tokenB exist, use the one with the highest number of flags
100         uint32_t flagA = GetTokenFlag(tokenWithFlagA);
101         uint32_t flagB = GetTokenFlag(tokenWithFlagB);
102         if (flagA > flagB) {
103             (void)memcpy_s(token, len, tokenWithFlagA, len);
104             return DEVICE_ATTEST_OEM_OK;
105         } else {
106             (void)memcpy_s(token, len, tokenWithFlagB, len);
107             return DEVICE_ATTEST_OEM_OK;
108         }
109     }
110 }
111 
OEMWriteToken(const char * token,uint32_t len)112 int32_t OEMWriteToken(const char *token, uint32_t len)
113 {
114     if (token == NULL || len == 0) {
115         return DEVICE_ATTEST_OEM_ERR;
116     }
117     char tokenWithFlagA[TOKEN_WITH_FLAG_SIZE] = {0};
118     char tokenWithFlagB[TOKEN_WITH_FLAG_SIZE] = {0};
119     int32_t retA = ReadTokenWithFlag(TOKEN_ADDR, TOKEN_A_ADDR, tokenWithFlagA, TOKEN_WITH_FLAG_SIZE);
120     int32_t retB = ReadTokenWithFlag(TOKEN_ADDR, TOKEN_B_ADDR, tokenWithFlagB, TOKEN_WITH_FLAG_SIZE);
121     if ((retA != DEVICE_ATTEST_OEM_OK) && (retB != DEVICE_ATTEST_OEM_OK)) {
122         unsigned char flag[TOKEN_FLAG_SIZE] = {0};
123         if ((memcpy_s(tokenWithFlagA, TOKEN_WITH_FLAG_SIZE, token, len) != 0) ||
124             (memcpy_s(tokenWithFlagA + len, TOKEN_WITH_FLAG_SIZE - len, flag, TOKEN_FLAG_SIZE) != 0)) {
125             return DEVICE_ATTEST_OEM_ERR;
126         }
127         if (WriteTokenWithFlag(TOKEN_ADDR, TOKEN_A_ADDR, tokenWithFlagA, TOKEN_WITH_FLAG_SIZE) != 0) {
128             return DEVICE_ATTEST_OEM_ERR;
129         }
130         return DEVICE_ATTEST_OEM_OK;
131     } else if ((retA == DEVICE_ATTEST_OEM_OK) && (retB != DEVICE_ATTEST_OEM_OK)) {
132         (void)memset_s(tokenWithFlagB, TOKEN_WITH_FLAG_SIZE, 0, TOKEN_WITH_FLAG_SIZE);
133         uint32_t flagA = GetTokenFlag(tokenWithFlagA);
134         unsigned char flag[TOKEN_FLAG_SIZE] = {0};
135         SetTokenFlag(flag, (uint32_t)(flagA + 1));
136         if ((memcpy_s(tokenWithFlagB, TOKEN_WITH_FLAG_SIZE, token, len) != 0) ||
137             (memcpy_s(tokenWithFlagB + len, TOKEN_WITH_FLAG_SIZE - len, flag, TOKEN_FLAG_SIZE) != 0)) {
138             return DEVICE_ATTEST_OEM_ERR;
139         }
140         if (WriteTokenWithFlag(TOKEN_ADDR, TOKEN_B_ADDR, tokenWithFlagB, TOKEN_WITH_FLAG_SIZE) != 0) {
141             return DEVICE_ATTEST_OEM_ERR;
142         }
143         return DEVICE_ATTEST_OEM_OK;
144     } else if ((retA != DEVICE_ATTEST_OEM_OK) && (retB == DEVICE_ATTEST_OEM_OK)) {
145         (void)memset_s(tokenWithFlagA, TOKEN_WITH_FLAG_SIZE, 0, TOKEN_WITH_FLAG_SIZE);
146         uint32_t flagB = GetTokenFlag(tokenWithFlagB);
147         unsigned char flag[TOKEN_FLAG_SIZE] = {0};
148         SetTokenFlag(flag, (uint32_t)(flagB + 1));
149         if ((memcpy_s(tokenWithFlagA, TOKEN_WITH_FLAG_SIZE, token, len) != 0) ||
150             (memcpy_s(tokenWithFlagA + len, TOKEN_WITH_FLAG_SIZE - len, flag, TOKEN_FLAG_SIZE) != 0)) {
151             return DEVICE_ATTEST_OEM_ERR;
152         }
153         if (WriteTokenWithFlag(TOKEN_ADDR, TOKEN_A_ADDR, tokenWithFlagA, TOKEN_WITH_FLAG_SIZE) != 0) {
154             return DEVICE_ATTEST_OEM_ERR;
155         }
156         return DEVICE_ATTEST_OEM_OK;
157     } else {
158         uint32_t flagA = GetTokenFlag(tokenWithFlagA);
159         uint32_t flagB = GetTokenFlag(tokenWithFlagB);
160         if (flagA > flagB) {
161             (void)memset_s(tokenWithFlagB, TOKEN_WITH_FLAG_SIZE, 0, TOKEN_WITH_FLAG_SIZE);
162             unsigned char flag[TOKEN_FLAG_SIZE] = {0};
163             SetTokenFlag(flag, (uint32_t)(flagA + 1));
164             if ((memcpy_s(tokenWithFlagB, TOKEN_WITH_FLAG_SIZE, token, len) != 0) ||
165                 (memcpy_s(tokenWithFlagB + len, TOKEN_WITH_FLAG_SIZE - len, flag, TOKEN_FLAG_SIZE) != 0)) {
166                 return DEVICE_ATTEST_OEM_ERR;
167             }
168             if (WriteTokenWithFlag(TOKEN_ADDR, TOKEN_B_ADDR, tokenWithFlagB, TOKEN_WITH_FLAG_SIZE) != 0) {
169                 return DEVICE_ATTEST_OEM_ERR;
170             }
171             return DEVICE_ATTEST_OEM_OK;
172         } else {
173             (void)memset_s(tokenWithFlagA, TOKEN_WITH_FLAG_SIZE, 0, TOKEN_WITH_FLAG_SIZE);
174             unsigned char flag[TOKEN_FLAG_SIZE] = {0};
175             SetTokenFlag(flag, (uint32_t)(flagB + 1));
176             if ((memcpy_s(tokenWithFlagA, TOKEN_WITH_FLAG_SIZE, token, len) != 0) ||
177                 (memcpy_s(tokenWithFlagA + len, TOKEN_WITH_FLAG_SIZE - len, flag, TOKEN_FLAG_SIZE) != 0)) {
178                 return DEVICE_ATTEST_OEM_ERR;
179             }
180             if (WriteTokenWithFlag(TOKEN_ADDR, TOKEN_A_ADDR, tokenWithFlagA, TOKEN_WITH_FLAG_SIZE) != 0) {
181                 return DEVICE_ATTEST_OEM_ERR;
182             }
183             return DEVICE_ATTEST_OEM_OK;
184         }
185     }
186 }
187 
OEMGetManufacturekey(char * manufacturekey,uint32_t len)188 int32_t OEMGetManufacturekey(char* manufacturekey, uint32_t len)
189 {
190     if ((manufacturekey == NULL) || (len == 0)) {
191         return DEVICE_ATTEST_OEM_ERR;
192     }
193     const char manufacturekeyBuf[] = {
194         0x13, 0x42, 0x3F, 0x3F, 0x53, 0x3F, 0x72, 0x30, 0x3F, 0x3F, 0x1C, 0x3F, 0x2F, 0x3F, 0x2E, 0x42,
195         0x3F, 0x08, 0x3F, 0x57, 0x3F, 0x10, 0x3F, 0x3F, 0x29, 0x17, 0x52, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
196         0x57, 0x16, 0x3F, 0x7D, 0x4A, 0x0F, 0x3F, 0x3F, 0x3F, 0x30, 0x0C, 0x3F, 0x3F, 0x4C, 0x3F, 0x47
197     };
198     uint32_t manufacturekeyBufLen = sizeof(manufacturekeyBuf);
199     if (len < manufacturekeyBufLen) {
200         return DEVICE_ATTEST_OEM_ERR;
201     }
202 
203     int32_t ret = memcpy_s(manufacturekey, len, manufacturekeyBuf, manufacturekeyBufLen);
204     return ret;
205 }
206 
OEMGetProductId(char * productId,uint32_t len)207 int32_t OEMGetProductId(char* productId, uint32_t len)
208 {
209     if ((productId == NULL) || (len == 0)) {
210         return DEVICE_ATTEST_OEM_ERR;
211     }
212     // Please fill in the actual value
213     const char productIdBuf[] = "";
214     uint32_t productIdLen = strlen(productIdBuf);
215     if (len < productIdLen) {
216         return DEVICE_ATTEST_OEM_ERR;
217     }
218 
219     int32_t ret = memcpy_s(productId, len, productIdBuf, productIdLen);
220     return ret;
221 }
222 
OEMGetProductKey(char * productKey,uint32_t len)223 int32_t OEMGetProductKey(char* productKey, uint32_t len)
224 {
225     if ((productKey == NULL) || (len == 0)) {
226         return DEVICE_ATTEST_OEM_ERR;
227     }
228     // Please fill in the actual value
229     const char productKeyBuf[] = "";
230     uint32_t productKeyLen = sizeof(productKeyBuf);
231     if (len < productKeyLen) {
232         return DEVICE_ATTEST_OEM_ERR;
233     }
234 
235     int32_t ret = memcpy_s(productKey, len, productKeyBuf, productKeyLen);
236     return ret;
237 }
238