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