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