• 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 "cm_test_common.h"
17 
18 #include "cert_manager_api.h"
19 
20 #include "cm_cert_data_ecc.h"
21 #include "cm_cert_data_ed25519.h"
22 #include "cm_cert_data_part1_rsa.h"
23 #include "cm_cert_data_part2_rsa.h"
24 #include "cm_mem.h"
25 #include "cm_test_log.h"
26 
27 #include "accesstoken_kit.h"
28 #include "nativetoken_kit.h"
29 #include "token_setproc.h"
30 #include <unistd.h>
31 #include <unordered_map>
32 
33 namespace CertmanagerTest {
34 constexpr uint32_t SLEEP_TIME = 3;
35 constexpr int32_t PERMISSION_MAX = 4;
36 constexpr int32_t PERMISSION_INDEX0 = 0;
37 constexpr int32_t PERMISSION_INDEX1 = 1;
38 constexpr int32_t PERMISSION_INDEX2 = 2;
39 constexpr int32_t PERMISSION_INDEX3 = 3;
40 
41 static const std::unordered_map<uint32_t, const uint8_t*> ALG_CODE_TO_CERT_DATA = {
42     { CERT_KEY_ALG_RSA, g_rsa2048P12CertInfo },
43     { CERT_KEY_ALG_ECC, g_eccP256P12CertInfo },
44     { CERT_KEY_ALG_RSA_512, g_rsa512P12CertInfo },
45     { CERT_KEY_ALG_RSA_1024, g_rsa1024P12CertInfo },
46     { CERT_KEY_ALG_RSA_3072, g_rsa3072P12CertInfo },
47     { CERT_KEY_ALG_RSA_4096, g_rsa4096P12CertInfo },
48     { CERT_KEY_ALG_ECC_P224, g_eccP224P12CertInfo },
49     { CERT_KEY_ALG_ECC_P384, g_eccP384P12CertInfo },
50     { CERT_KEY_ALG_ECC_P521, g_eccP521P12CertInfo },
51     { CERT_KEY_ALG_ED25519, g_ed25519P12CertInfo },
52     { CERT_KEY_ALG_SM2_1, g_sm2PfxCertInfo },
53     { CERT_KEY_ALG_SM2_2, g_sm2PfxCertInfo2 },
54 };
55 
56 static const std::unordered_map<uint32_t, uint32_t> ALG_CODE_TO_CERT_SIZE = {
57     { CERT_KEY_ALG_RSA, sizeof(g_rsa2048P12CertInfo) },
58     { CERT_KEY_ALG_ECC, sizeof(g_eccP256P12CertInfo) },
59     { CERT_KEY_ALG_RSA_512, sizeof(g_rsa512P12CertInfo) },
60     { CERT_KEY_ALG_RSA_1024, sizeof(g_rsa1024P12CertInfo) },
61     { CERT_KEY_ALG_RSA_3072, sizeof(g_rsa3072P12CertInfo) },
62     { CERT_KEY_ALG_RSA_4096, sizeof(g_rsa4096P12CertInfo) },
63     { CERT_KEY_ALG_ECC_P224, sizeof(g_eccP224P12CertInfo) },
64     { CERT_KEY_ALG_ECC_P384, sizeof(g_eccP384P12CertInfo) },
65     { CERT_KEY_ALG_ECC_P521, sizeof(g_eccP521P12CertInfo) },
66     { CERT_KEY_ALG_ED25519, sizeof(g_ed25519P12CertInfo) },
67     { CERT_KEY_ALG_SM2_1, sizeof(g_sm2PfxCertInfo) },
68     { CERT_KEY_ALG_SM2_2, sizeof(g_sm2PfxCertInfo2) },
69 };
70 
SetATPermission(void)71 void SetATPermission(void)
72 {
73     static bool firstRun = true;
74     const char **perms = new const char *[PERMISSION_MAX]; // 4 permissions
75     perms[PERMISSION_INDEX0] = "ohos.permission.ACCESS_CERT_MANAGER_INTERNAL"; // system_core
76     perms[PERMISSION_INDEX1] = "ohos.permission.ACCESS_CERT_MANAGER"; // normal
77     perms[PERMISSION_INDEX2] = "ohos.permission.ACCESS_USER_TRUSTED_CERT"; // system_core
78     perms[PERMISSION_INDEX3] = "ohos.permission.ACCESS_SYSTEM_APP_CERT"; // system_core
79     NativeTokenInfoParams infoInstance = {
80         .dcapsNum = 0,
81         .permsNum = PERMISSION_MAX,
82         .dcaps = nullptr,
83         .perms = perms,
84         .acls = nullptr,
85         .processName = "TestCertManager",
86         .aplStr = "system_core",
87     };
88 
89     auto tokenId = GetAccessTokenId(&infoInstance);
90     SetSelfTokenID(tokenId);
91     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
92     if (firstRun) {
93         system("pidof accesstoken_ser | xargs kill -9");
94         sleep(SLEEP_TIME);
95         firstRun = false;
96     }
97     delete[] perms;
98 }
99 
InitCertInfo(struct CertInfo * certInfo)100 int32_t InitCertInfo(struct CertInfo *certInfo)
101 {
102     if (certInfo == nullptr) {
103         return CMR_ERROR_MALLOC_FAIL;
104     }
105 
106     certInfo->certInfo.data =  static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE));
107     if (certInfo->certInfo.data  == NULL) {
108         return CMR_ERROR_MALLOC_FAIL;
109     }
110     certInfo->certInfo.size = MAX_LEN_CERTIFICATE;
111 
112     return CM_SUCCESS;
113 }
114 
InitCertList(struct CertList ** cList)115 int32_t InitCertList(struct CertList **cList)
116 {
117     *cList = static_cast<struct CertList *>(CmMalloc(sizeof(struct CertList)));
118     if (*cList == nullptr) {
119         return CMR_ERROR_MALLOC_FAIL;
120     }
121 
122     uint32_t buffSize = MAX_COUNT_CERTIFICATE_ALL * sizeof(struct CertAbstract);
123     (*cList)->certAbstract = static_cast<struct CertAbstract *>(CmMalloc(buffSize));
124     if ((*cList)->certAbstract == NULL) {
125         return CMR_ERROR_MALLOC_FAIL;
126     }
127     (void)memset_s((*cList)->certAbstract, buffSize, 0, buffSize);
128     (*cList)->certsCount = MAX_COUNT_CERTIFICATE_ALL;
129 
130     return CM_SUCCESS;
131 }
132 
InitUserCertInfo(struct CertInfo ** cInfo)133 int32_t InitUserCertInfo(struct CertInfo **cInfo)
134 {
135     *cInfo = static_cast<struct CertInfo *>(CmMalloc(sizeof(struct CertInfo)));
136     if (*cInfo == nullptr) {
137         return CMR_ERROR_MALLOC_FAIL;
138     }
139     (void)memset_s(*cInfo, sizeof(struct CertInfo), 0, sizeof(struct CertInfo));
140 
141     (*cInfo)->certInfo.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE));
142     if ((*cInfo)->certInfo.data == NULL) {
143         return CMR_ERROR_MALLOC_FAIL;
144     }
145     (*cInfo)->certInfo.size = MAX_LEN_CERTIFICATE;
146 
147     return CM_SUCCESS;
148 }
149 
FreeCertList(struct CertList * certList)150 void FreeCertList(struct CertList *certList)
151 {
152     if (certList == nullptr || certList->certAbstract == nullptr) {
153         return;
154     }
155 
156     CmFree(certList->certAbstract);
157     certList->certAbstract = nullptr;
158 
159     CmFree(certList);
160 }
161 
162 
FreeCMBlobData(struct CmBlob * blob)163 void FreeCMBlobData(struct CmBlob *blob)
164 {
165     if (blob == nullptr) {
166         return;
167     }
168 
169     if (blob->data != nullptr) {
170         CmFree(blob->data);
171         blob->data = nullptr;
172     }
173     blob->size = 0;
174 }
175 
FreeCertInfo(struct CertInfo * cInfo)176 void FreeCertInfo(struct CertInfo *cInfo)
177 {
178     if (cInfo == nullptr || (cInfo->certInfo).data == nullptr) {
179         return;
180     }
181 
182     FreeCMBlobData(&(cInfo->certInfo));
183     CmFree(cInfo);
184 }
185 
CompareCert(const struct CertAbstract * firstCert,const struct CertAbstract * secondCert)186 static bool CompareCert(const struct CertAbstract *firstCert, const struct CertAbstract *secondCert)
187 {
188     if (firstCert == nullptr || secondCert == nullptr) {
189         CM_TEST_LOG_E("cert invalid parameter");
190         return false;
191     }
192     return ((strcmp(firstCert->uri, secondCert->uri) == 0) &&
193         (strcmp(firstCert->certAlias, secondCert->certAlias) == 0) &&
194         (strcmp(firstCert->subjectName, secondCert->subjectName) == 0) &&
195         (firstCert->status == secondCert->status));
196 }
197 
CompareCredentialList(const struct CredentialAbstract * firstCert,const struct CredentialAbstract * secondCert)198 bool CompareCredentialList(const struct CredentialAbstract *firstCert, const struct CredentialAbstract *secondCert)
199 {
200     if (firstCert == nullptr || secondCert == nullptr) {
201         CM_TEST_LOG_E("cert invalid parameter");
202         return false;
203     }
204     return ((strcmp(firstCert->type, secondCert->type) == 0) &&
205         (strcmp(firstCert->alias, secondCert->alias) == 0) &&
206         (strcmp(firstCert->keyUri, secondCert->keyUri) == 0));
207 }
208 
DumpCertAbstractInfo(const struct CertAbstract * certAbstract)209 std::string  DumpCertAbstractInfo(const struct CertAbstract *certAbstract)
210 {
211     if (certAbstract == nullptr) {
212         return " ";
213     }
214     std::string str  = "";
215     str += ENDOF;
216     str +=  certAbstract->uri;
217     str += DELIMITER;
218     str += certAbstract->certAlias;
219     str += DELIMITER;
220     str += certAbstract->subjectName;
221     str += DELIMITER;
222     str += (certAbstract->status) ? "true" : "false";
223     str += ENDOF;
224     return str;
225 }
226 
DumpCertList(struct CertList * certList)227 std::string DumpCertList(struct CertList *certList)
228 {
229     if (certList == nullptr) {
230         return " ";
231     }
232 
233     std::string  str  = "";
234     if (certList->certsCount > 0 && certList->certAbstract != nullptr) {
235         for (uint32_t i = 0; i < certList->certsCount; i++) {
236             str +=  DumpCertAbstractInfo(&(certList->certAbstract[i]));
237         }
238     }
239     return str;
240 }
241 
CompareCertInfo(const struct CertInfo * firstCert,const struct CertInfo * secondCert)242 bool CompareCertInfo(const struct CertInfo *firstCert, const struct CertInfo *secondCert)
243 {
244     if (firstCert == nullptr || secondCert == nullptr) {
245         return false;
246     }
247     return ((strcmp(firstCert->uri, secondCert->uri) == 0) &&
248             (strcmp(firstCert->certAlias, secondCert->certAlias) == 0) &&
249             (firstCert->status == secondCert->status) &&
250             (strcmp(firstCert->issuerName, secondCert->issuerName) == 0) &&
251             (strcmp(firstCert->subjectName, secondCert->subjectName) == 0) &&
252             (strcmp(firstCert->serial, secondCert->serial) == 0) &&
253             (strcmp(firstCert->notBefore, secondCert->notBefore) == 0) &&
254             (strcmp(firstCert->notAfter, secondCert->notAfter) == 0) &&
255             (strcmp(firstCert->fingerprintSha256, secondCert->fingerprintSha256) == 0));
256 }
257 
CompareCertData(const struct CmBlob * firstData,const struct CmBlob * secondData)258 bool CompareCertData(const struct CmBlob *firstData, const struct CmBlob *secondData)
259 {
260     if (firstData == nullptr || secondData == nullptr) {
261         return false;
262     }
263     return ((firstData->size == secondData->size) &&
264             (memcmp(firstData->data, secondData->data, static_cast<int32_t>(firstData->size)) == 0));
265 }
266 
DumpCertInfo(const struct CertInfo * certInfo)267 std::string DumpCertInfo(const struct CertInfo *certInfo)
268 {
269     if (certInfo == nullptr) {
270         return " ";
271     }
272     std::string str = "";
273     str += ENDOF;
274     str += certInfo->uri;
275     str += DELIMITER;
276     str += certInfo->certAlias;
277     str += DELIMITER;
278     str += certInfo->subjectName;
279     str += DELIMITER;
280     str += (certInfo->status) ? "true" : "false";
281     str += ENDOF;
282     return str;
283 }
284 
CompareCredential(const struct Credential * firstCredential,const struct Credential * secondCredential)285 bool CompareCredential(const struct Credential *firstCredential, const struct Credential *secondCredential)
286 {
287     if (firstCredential == nullptr || secondCredential == nullptr) {
288         return false;
289     }
290     return ((strcmp(firstCredential->type, secondCredential->type) == 0) &&
291             (strcmp(firstCredential->alias, secondCredential->alias) == 0) &&
292             (strcmp(firstCredential->keyUri, secondCredential->keyUri) == 0) &&
293             (firstCredential->certNum == secondCredential->certNum) &&
294             (firstCredential->keyNum == secondCredential->keyNum) &&
295             (firstCredential->credData.size == secondCredential->credData.size));
296 }
297 
ConstructAppCertData(uint32_t alg,struct CmBlob * appCert)298 static int32_t ConstructAppCertData(uint32_t alg, struct CmBlob *appCert)
299 {
300     auto iterSize = ALG_CODE_TO_CERT_SIZE.find(alg);
301     if (iterSize == ALG_CODE_TO_CERT_SIZE.end()) {
302         return CMR_ERROR_INVALID_ARGUMENT;
303     }
304     appCert->size = iterSize->second;
305 
306     auto iterData = ALG_CODE_TO_CERT_DATA.find(alg);
307     if (iterData == ALG_CODE_TO_CERT_DATA.end()) {
308         return CMR_ERROR_INVALID_ARGUMENT;
309     }
310     appCert->data = const_cast<uint8_t *>(iterData->second);
311     return CM_SUCCESS;
312 }
313 
TestGenerateAppCert(const struct CmBlob * alias,uint32_t alg,uint32_t store)314 int32_t TestGenerateAppCert(const struct CmBlob *alias, uint32_t alg, uint32_t store)
315 {
316     struct CmBlob appCert = { 0, NULL };
317     int32_t ret = ConstructAppCertData(alg, &appCert);
318     if (ret != CM_SUCCESS) {
319         return ret;
320     }
321 
322     struct CmBlob appCertPwd = { sizeof(g_certPwd), const_cast<uint8_t *>(g_certPwd) };
323     if (alg == CERT_KEY_ALG_SM2_1 || alg == CERT_KEY_ALG_SM2_2) {
324         appCertPwd.size = sizeof(g_sm2certPwd);
325         appCertPwd.data = const_cast<uint8_t *>(g_sm2certPwd);
326     }
327     uint8_t uriData[MAX_LEN_URI] = {0};
328     struct CmBlob keyUri = { sizeof(uriData), uriData };
329 
330     if (store == CM_SYS_CREDENTIAL_STORE) {
331         struct CmAppCertParam appCertParam = { &appCert, &appCertPwd,
332             (struct CmBlob *)alias, CM_SYS_CREDENTIAL_STORE, TEST_USERID };
333         return CmInstallSystemAppCert(&appCertParam, &keyUri);
334     }
335     return CmInstallAppCert(&appCert, &appCertPwd, alias, store, &keyUri);
336 }
337 
FindCertAbstract(const struct CertAbstract * abstract,const struct CertList * cList)338 bool FindCertAbstract(const struct CertAbstract *abstract, const struct CertList *cList)
339 {
340     if (abstract == nullptr || cList == nullptr || cList->certsCount == 0) {
341         return false;
342     }
343     for (uint32_t i = 0; i < cList->certsCount; ++i) {
344         if (CompareCert(abstract, &(cList->certAbstract[i]))) {
345             return true;
346         }
347     }
348     return false;
349 }
350 }
351