• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 "cert_manager_api.h"
17 
18 #include "cm_advsecmode_check.h"
19 #include "cm_log.h"
20 #include "cm_mem.h"
21 #include "cm_ipc_client.h"
22 #include "cm_type.h"
23 
CmGetCertList(uint32_t store,struct CertList * certificateList)24 CM_API_EXPORT int32_t CmGetCertList(uint32_t store, struct CertList *certificateList)
25 {
26     CM_LOG_D("enter get certificate list");
27     if (certificateList == NULL) {
28         CM_LOG_E("invalid input arguments");
29         return CMR_ERROR_NULL_POINTER;
30     }
31 
32     if ((certificateList->certAbstract == NULL) || (store != CM_SYSTEM_TRUSTED_STORE)) {
33         CM_LOG_E("invalid input arguments store:%u", store);
34         return CMR_ERROR_INVALID_ARGUMENT;
35     }
36 
37     int32_t ret = CmClientGetCertList(store, certificateList);
38     CM_LOG_D("leave get certificate list, result = %d", ret);
39     return ret;
40 }
41 
CmGetCertInfo(const struct CmBlob * certUri,uint32_t store,struct CertInfo * certificateInfo)42 CM_API_EXPORT int32_t CmGetCertInfo(const struct CmBlob *certUri, uint32_t store,
43     struct CertInfo *certificateInfo)
44 {
45     CM_LOG_D("enter get certificate info");
46     if ((certUri == NULL) || (certificateInfo == NULL)) {
47         CM_LOG_E("invalid input arguments");
48         return CMR_ERROR_NULL_POINTER;
49     }
50 
51     if ((certificateInfo->certInfo.data == NULL) || (certificateInfo->certInfo.size == 0) ||
52         (store != CM_SYSTEM_TRUSTED_STORE)) {
53         CM_LOG_E("invalid input arguments store:%u", store);
54         return CMR_ERROR_INVALID_ARGUMENT;
55     }
56 
57     int32_t ret = CmClientGetCertInfo(certUri, store, certificateInfo);
58     CM_LOG_D("leave get certificate info, result = %d", ret);
59     return ret;
60 }
61 
CmSetCertStatus(const struct CmBlob * certUri,const uint32_t store,const bool status)62 CM_API_EXPORT int32_t CmSetCertStatus(const struct CmBlob *certUri, const uint32_t store,
63     const bool status)
64 {
65     CM_LOG_D("enter set certificate status");
66     if (certUri == NULL) {
67         CM_LOG_E("invalid input arguments");
68         return CMR_ERROR_NULL_POINTER;
69     }
70 
71     if (store != CM_SYSTEM_TRUSTED_STORE) {
72         CM_LOG_E("invalid input arguments store:%u", store);
73         return CMR_ERROR_INVALID_ARGUMENT;
74     }
75 
76     uint32_t uStatus = status ? 0 : 1; // 0 indicates the certificate enabled status
77 
78     int32_t ret = CmClientSetCertStatus(certUri, store, uStatus);
79     CM_LOG_D("leave set certificate status, result = %d", ret);
80     return ret;
81 }
82 
CmInstallAppCertEx(const struct CmAppCertParam * certParam,struct CmBlob * keyUri)83 CM_API_EXPORT int32_t CmInstallAppCertEx(const struct CmAppCertParam *certParam, struct CmBlob *keyUri)
84 {
85     CM_LOG_D("enter install app certificate extension");
86     /* The store must be private, and the userid must be invalid */
87     if (certParam == NULL || certParam->appCert == NULL || certParam->appCertPwd == NULL||
88         certParam->certAlias == NULL || keyUri == NULL || certParam->userId != INIT_INVALID_VALUE ||
89         keyUri->data == NULL || certParam->store != CM_PRI_CREDENTIAL_STORE || CM_LEVEL_CHECK(certParam->level)) {
90         CM_LOG_E("an error in the parameters of installing the application certificate ex.");
91         return CMR_ERROR_INVALID_ARGUMENT;
92     }
93 
94     int32_t ret = CmClientInstallAppCert(certParam, keyUri);
95     CM_LOG_D("leave install app certificate extension, result = %d", ret);
96     return ret;
97 }
98 
CmInstallAppCert(const struct CmBlob * appCert,const struct CmBlob * appCertPwd,const struct CmBlob * certAlias,const uint32_t store,struct CmBlob * keyUri)99 CM_API_EXPORT int32_t CmInstallAppCert(const struct CmBlob *appCert, const struct CmBlob *appCertPwd,
100     const struct CmBlob *certAlias, const uint32_t store, struct CmBlob *keyUri)
101 {
102     CM_LOG_D("enter install app certificate");
103     if (appCert == NULL || appCertPwd == NULL || certAlias == NULL ||
104         keyUri == NULL || keyUri->data == NULL || CM_STORE_CHECK(store)) {
105         CM_LOG_E("an error in the parameters of installing the application certificate.");
106         return CMR_ERROR_INVALID_ARGUMENT;
107     }
108 
109     /* The public credentials are at the EL2 level. */
110     enum CmAuthStorageLevel level = (store == CM_CREDENTIAL_STORE) ? CM_AUTH_STORAGE_LEVEL_EL2 :
111         CM_AUTH_STORAGE_LEVEL_EL1;
112 
113     struct CmAppCertParam certParam = { (struct CmBlob *)appCert, (struct CmBlob *)appCertPwd,
114         (struct CmBlob *)certAlias, store, INIT_INVALID_VALUE, level };
115 
116     int32_t ret = CmClientInstallAppCert(&certParam, keyUri);
117     CM_LOG_D("leave install app certificate, result = %d", ret);
118     return ret;
119 }
120 
CmUninstallAppCert(const struct CmBlob * keyUri,const uint32_t store)121 CM_API_EXPORT int32_t CmUninstallAppCert(const struct CmBlob *keyUri, const uint32_t store)
122 {
123     CM_LOG_D("enter uninstall app certificate");
124     if (keyUri == NULL || CM_STORE_CHECK(store)) {
125         return CMR_ERROR_INVALID_ARGUMENT;
126     }
127 
128     int32_t ret = CmClientUninstallAppCert(keyUri, store);
129     CM_LOG_D("leave uninstall app certificate, result = %d", ret);
130     return ret;
131 }
132 
CmUninstallAllAppCert(void)133 CM_API_EXPORT int32_t CmUninstallAllAppCert(void)
134 {
135     CM_LOG_D("enter uninstall all app certificate");
136 
137     int32_t ret = CmClientUninstallAllAppCert(CM_MSG_UNINSTALL_ALL_APP_CERTIFICATE);
138 
139     CM_LOG_D("leave uninstall all app certificate, result = %d", ret);
140     return ret;
141 }
142 
CmGetAppCertList(const uint32_t store,struct CredentialList * certificateList)143 CM_API_EXPORT int32_t CmGetAppCertList(const uint32_t store, struct CredentialList *certificateList)
144 {
145     CM_LOG_D("enter get app certificatelist");
146     if (certificateList == NULL || CM_STORE_CHECK(store)) {
147         return CMR_ERROR_INVALID_ARGUMENT;
148     }
149 
150     int32_t ret = CmClientGetAppCertList(store, certificateList);
151     CM_LOG_D("leave get app certificatelist, result = %d", ret);
152     return ret;
153 }
154 
CmCallingGetAppCertList(const uint32_t store,struct CredentialList * certificateList)155 CM_API_EXPORT int32_t CmCallingGetAppCertList(const uint32_t store, struct CredentialList *certificateList)
156 {
157     if (certificateList == NULL || CM_STORE_CHECK(store)) {
158         return CMR_ERROR_INVALID_ARGUMENT;
159     }
160 
161     int32_t ret = CmClientGetCallingAppCertList(store, certificateList);
162     return ret;
163 }
164 
CmGetAppCert(const struct CmBlob * keyUri,const uint32_t store,struct Credential * certificate)165 CM_API_EXPORT int32_t CmGetAppCert(const struct CmBlob *keyUri, const uint32_t store,
166     struct Credential *certificate)
167 {
168     CM_LOG_D("enter get app certificate");
169     if (keyUri == NULL || certificate == NULL || CM_STORE_CHECK(store)) {
170         return CMR_ERROR_INVALID_ARGUMENT;
171     }
172 
173     int32_t ret = CmClientGetAppCert(keyUri, store, certificate);
174     CM_LOG_D("leave get app certificate, result = %d", ret);
175     return ret;
176 }
177 
CmGrantAppCertificate(const struct CmBlob * keyUri,uint32_t appUid,struct CmBlob * authUri)178 CM_API_EXPORT int32_t CmGrantAppCertificate(const struct CmBlob *keyUri, uint32_t appUid, struct CmBlob *authUri)
179 {
180     CM_LOG_D("enter grant app certificate");
181     if ((keyUri == NULL) || (authUri == NULL)) {
182         CM_LOG_E("invalid input arguments");
183         return CMR_ERROR_INVALID_ARGUMENT;
184     }
185 
186     int32_t ret = CmClientGrantAppCertificate(keyUri, appUid, authUri);
187     CM_LOG_D("leave grant app certificate, result = %d", ret);
188     return ret;
189 }
190 
CmGetAuthorizedAppList(const struct CmBlob * keyUri,struct CmAppUidList * appUidList)191 CM_API_EXPORT int32_t CmGetAuthorizedAppList(const struct CmBlob *keyUri, struct CmAppUidList *appUidList)
192 {
193     CM_LOG_D("enter get authorized app list");
194     if ((keyUri == NULL) || (appUidList == NULL)) {
195         CM_LOG_E("invalid input arguments");
196         return CMR_ERROR_INVALID_ARGUMENT;
197     }
198 
199     int32_t ret = CmClientGetAuthorizedAppList(keyUri, appUidList);
200     CM_LOG_D("leave get authorized app list, result = %d", ret);
201     return ret;
202 }
203 
CmIsAuthorizedApp(const struct CmBlob * authUri)204 CM_API_EXPORT int32_t CmIsAuthorizedApp(const struct CmBlob *authUri)
205 {
206     CM_LOG_D("enter check is app authed");
207     if (authUri == NULL) {
208         CM_LOG_E("invalid input arguments");
209         return CMR_ERROR_INVALID_ARGUMENT;
210     }
211 
212     int32_t ret = CmClientIsAuthorizedApp(authUri);
213     CM_LOG_D("leave check is app authed, result = %d", ret);
214     return ret;
215 }
216 
CmRemoveGrantedApp(const struct CmBlob * keyUri,uint32_t appUid)217 CM_API_EXPORT int32_t CmRemoveGrantedApp(const struct CmBlob *keyUri, uint32_t appUid)
218 {
219     CM_LOG_D("enter remove granted app");
220     if (keyUri == NULL) {
221         CM_LOG_E("invalid input arguments");
222         return CMR_ERROR_INVALID_ARGUMENT;
223     }
224 
225     int32_t ret = CmClientRemoveGrantedApp(keyUri, appUid);
226     CM_LOG_D("leave remove granted app, result = %d", ret);
227     return ret;
228 }
229 
CmInit(const struct CmBlob * authUri,const struct CmSignatureSpec * spec,struct CmBlob * handle)230 CM_API_EXPORT int32_t CmInit(const struct CmBlob *authUri, const struct CmSignatureSpec *spec, struct CmBlob *handle)
231 {
232     CM_LOG_D("enter cert manager init");
233     if ((authUri == NULL) || (spec == NULL) || (handle == NULL)) {
234         CM_LOG_E("invalid input arguments");
235         return CMR_ERROR_INVALID_ARGUMENT;
236     }
237 
238     int32_t ret = CmClientInit(authUri, spec, handle);
239     CM_LOG_D("leave cert manager init, result = %d", ret);
240     return ret;
241 }
242 
CmUpdate(const struct CmBlob * handle,const struct CmBlob * inData)243 CM_API_EXPORT int32_t CmUpdate(const struct CmBlob *handle, const struct CmBlob *inData)
244 {
245     CM_LOG_D("enter cert manager update");
246     if ((handle == NULL) || (inData == NULL)) {
247         CM_LOG_E("invalid input arguments");
248         return CMR_ERROR_INVALID_ARGUMENT;
249     }
250 
251     int32_t ret = CmClientUpdate(handle, inData);
252     CM_LOG_D("leave cert manager update, result = %d", ret);
253     return ret;
254 }
255 
CmFinish(const struct CmBlob * handle,const struct CmBlob * inData,struct CmBlob * outData)256 CM_API_EXPORT int32_t CmFinish(const struct CmBlob *handle, const struct CmBlob *inData, struct CmBlob *outData)
257 {
258     CM_LOG_D("enter cert manager finish");
259     if ((handle == NULL) || (inData == NULL) || (outData == NULL)) {
260         CM_LOG_E("invalid input arguments");
261         return CMR_ERROR_INVALID_ARGUMENT;
262     }
263 
264     int32_t ret = CmClientFinish(handle, inData, outData);
265     CM_LOG_D("leave cert manager finish, result = %d", ret);
266     return ret;
267 }
268 
CmAbort(const struct CmBlob * handle)269 CM_API_EXPORT int32_t CmAbort(const struct CmBlob *handle)
270 {
271     CM_LOG_D("enter cert manager abort");
272     if (handle == NULL) {
273         CM_LOG_E("invalid input arguments");
274         return CMR_ERROR_INVALID_ARGUMENT;
275     }
276 
277     int32_t ret = CmClientAbort(handle);
278     CM_LOG_D("leave cert manager abort, result = %d", ret);
279     return ret;
280 }
281 
CmGetUserCertList(uint32_t store,struct CertList * certificateList)282 CM_API_EXPORT int32_t CmGetUserCertList(uint32_t store, struct CertList *certificateList)
283 {
284     CM_LOG_D("enter get cert list");
285     if (certificateList == NULL) {
286         return CMR_ERROR_NULL_POINTER;
287     }
288 
289     const struct UserCAProperty property = { INIT_INVALID_VALUE, CM_ALL_USER };
290     int32_t ret = CmClientGetUserCertList(&property, store, certificateList);
291     CM_LOG_D("leave get cert list, result = %d", ret);
292     return ret;
293 }
294 
CmGetUserCertInfo(const struct CmBlob * certUri,uint32_t store,struct CertInfo * certificateInfo)295 CM_API_EXPORT int32_t CmGetUserCertInfo(const struct CmBlob *certUri, uint32_t store, struct CertInfo *certificateInfo)
296 {
297     CM_LOG_D("enter get cert info");
298     if ((certUri == NULL) || (certificateInfo == NULL)) {
299         return CMR_ERROR_NULL_POINTER;
300     }
301 
302     int32_t ret = CmClientGetUserCertInfo(certUri, store, certificateInfo);
303     CM_LOG_D("leave get cert info, result = %d", ret);
304     return ret;
305 }
306 
CmSetUserCertStatus(const struct CmBlob * certUri,uint32_t store,const bool status)307 CM_API_EXPORT int32_t CmSetUserCertStatus(const struct CmBlob *certUri, uint32_t store, const bool status)
308 {
309     CM_LOG_D("enter set cert status");
310     if (certUri == NULL) {
311         return CMR_ERROR_NULL_POINTER;
312     }
313 
314     uint32_t uStatus = status ? 0 : 1; // 0 indicates the certificate enabled status
315 
316     int32_t ret = CmClientSetUserCertStatus(certUri, store, uStatus);
317     CM_LOG_D("leave set cert status, result = %d", ret);
318     return ret;
319 }
320 
CmInstallUserTrustedCert(const struct CmBlob * userCert,const struct CmBlob * certAlias,struct CmBlob * certUri)321 CM_API_EXPORT int32_t CmInstallUserTrustedCert(const struct CmBlob *userCert, const struct CmBlob *certAlias,
322     struct CmBlob *certUri)
323 {
324     CM_LOG_D("enter install user trusted cert");
325     if ((userCert == NULL) || (certAlias == NULL) || (certUri == NULL)) {
326         return CMR_ERROR_INVALID_ARGUMENT;
327     }
328 
329     uint32_t userId = INIT_INVALID_VALUE;
330     bool status = true;
331     int32_t ret = CmInstallUserCACert(userCert, certAlias, userId, status, certUri);
332     CM_LOG_D("leave install user trusted cert, result = %d", ret);
333     return ret;
334 }
335 
CmUninstallUserTrustedCert(const struct CmBlob * certUri)336 CM_API_EXPORT int32_t CmUninstallUserTrustedCert(const struct CmBlob *certUri)
337 {
338     CM_LOG_D("enter uninstall user trusted cert");
339     if (certUri == NULL) {
340         return CMR_ERROR_INVALID_ARGUMENT;
341     }
342 
343     int32_t ret = CmClientUninstallUserTrustedCert(certUri);
344     CM_LOG_D("leave uninstall user trusted cert, result = %d", ret);
345     return ret;
346 }
347 
CmUninstallAllUserTrustedCert(void)348 CM_API_EXPORT int32_t CmUninstallAllUserTrustedCert(void)
349 {
350     CM_LOG_D("enter uninstall all user trusted cert");
351 
352     int32_t ret = CmClientUninstallAllUserTrustedCert();
353     CM_LOG_D("leave uninstall all user trusted cert, result = %d", ret);
354     return ret;
355 }
356 
CmInstallSystemAppCert(const struct CmAppCertParam * certParam,struct CmBlob * keyUri)357 CM_API_EXPORT int32_t CmInstallSystemAppCert(const struct CmAppCertParam *certParam, struct CmBlob *keyUri)
358 {
359     CM_LOG_D("enter install system app certificate");
360     if ((certParam == NULL) || (certParam->appCert == NULL) || (certParam->appCertPwd == NULL) ||
361         (certParam->certAlias == NULL) || (keyUri == NULL) || (keyUri->data == NULL) ||
362         (certParam->store != CM_SYS_CREDENTIAL_STORE) || (certParam->userId == 0) ||
363         (certParam->userId == INIT_INVALID_VALUE)) {
364         return CMR_ERROR_INVALID_ARGUMENT;
365     }
366 
367     int32_t ret = CmClientInstallSystemAppCert(certParam, keyUri);
368     CM_LOG_D("leave install system app certificate, result = %d", ret);
369     return ret;
370 }
371 
CmInstallUserCACert(const struct CmBlob * userCert,const struct CmBlob * certAlias,const uint32_t userId,const bool status,struct CmBlob * certUri)372 CM_API_EXPORT int32_t CmInstallUserCACert(const struct CmBlob *userCert,
373     const struct CmBlob *certAlias, const uint32_t userId, const bool status, struct CmBlob *certUri)
374 {
375     CM_LOG_D("enter install user ca cert");
376     if ((userCert == NULL) || (certAlias == NULL) || (certUri == NULL)) {
377         return CMR_ERROR_INVALID_ARGUMENT;
378     }
379 
380     bool isAdvSecMode = false;
381     int32_t ret = CheckAdvSecMode(&isAdvSecMode);
382     if (ret != CM_SUCCESS) {
383         return ret;
384     }
385     if (isAdvSecMode) {
386         CM_LOG_E("InstallUserTrustedCert: the device enters advanced security mode");
387         return CMR_ERROR_DEVICE_ENTER_ADVSECMODE;
388     }
389 
390     uint32_t uStatus = status ? 0 : 1; // 0 indicates the certificate enabled status
391     ret = CmClientInstallUserTrustedCert(userCert, certAlias, userId, uStatus, certUri);
392     CM_LOG_D("leave install user ca cert, result = %d", ret);
393     return ret;
394 }
395 
CmGetUserCACertList(const struct UserCAProperty * property,struct CertList * certificateList)396 CM_API_EXPORT int32_t CmGetUserCACertList(const struct UserCAProperty *property, struct CertList *certificateList)
397 {
398     CM_LOG_D("enter get user ca cert list");
399     if (certificateList == NULL || property == NULL) {
400         return CMR_ERROR_NULL_POINTER;
401     }
402 
403     const uint32_t store = CM_USER_TRUSTED_STORE;
404     int32_t ret = CmClientGetUserCertList(property, store, certificateList);
405     CM_LOG_D("leave get user ca cert list, result = %d", ret);
406     return ret;
407 }
408 
CmGetCertStorePath(const enum CmCertType type,const uint32_t userId,char * path,uint32_t pathLen)409 CM_API_EXPORT int32_t CmGetCertStorePath(const enum CmCertType type, const uint32_t userId,
410     char *path, uint32_t pathLen)
411 {
412     if (path == NULL) {
413         return CMR_ERROR_NULL_POINTER;
414     }
415 
416     if (type == CM_CA_CERT_SYSTEM) {
417         if (strcpy_s(path, pathLen, CA_STORE_PATH_SYSTEM) != EOK) {
418             CM_LOG_E("get system ca path: out path len[%u] too small.", pathLen);
419             return CMR_ERROR_BUFFER_TOO_SMALL;
420         }
421         return CM_SUCCESS;
422     }
423 
424     if (type == CM_CA_CERT_USER) {
425         if (sprintf_s(path, pathLen, "%s%u", CA_STORE_PATH_USER_SERVICE_BASE, userId) < 0) {
426             CM_LOG_E("get user ca path: out path len[%u] too small.", pathLen);
427             return CMR_ERROR_BUFFER_TOO_SMALL;
428         }
429         return CM_SUCCESS;
430     }
431 
432     return CMR_ERROR_INVALID_ARGUMENT;
433 }
434 
435