• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.security.certManager (Certificate Management) (System API)
2
3The **certManager** module provides system-level certificate management capabilities to ensure secure use and management of certificates throughout their lifecycle (installation, storage, use, and destruction).
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.security.certManager (Certificate Management)](js-apis-certManager.md).
9
10## Modules to Import
11
12```ts
13import { certificateManager } from '@kit.DeviceCertificateKit';
14```
15
16## CMErrorCode
17
18Enumerates the error codes used in the certificate management APIs.
19
20**System capability**: System SystemCapability.Security.CertificateManager
21
22| Name      | Value|  Description     |
23| ---------- | ------ | --------- |
24| CM_ERROR_NOT_SYSTEM_APP   | 202      | The caller is not a system application.<br> **System API**: This is a system API.|
25
26## certificateManager.getAllAppPrivateCertificates
27
28getAllAppPrivateCertificates(callback: AsyncCallback\<CMResult>): void
29
30Obtains all private credentials. This API uses an asynchronous callback to return the result.
31
32**Required permissions**: ohos.permission.ACCESS_CERT_MANAGER and ohos.permission.ACCESS_CERT_MANAGER_INTERNAL
33
34**System capability**: System SystemCapability.Security.CertificateManager
35
36**System API**: This is a system API.
37
38**Parameters**
39
40| Name  | Type                                                       | Mandatory| Description                                                        |
41| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ |
42| callback | AsyncCallback\<[CMResult](js-apis-certManager.md#cmresult)> | Yes  | Callback used to return the result. If the operation is successful, **err** is **null** and **data** is **credentialList** in the [CMResult](#cmresult) object. Otherwise, **err** is an error object.|
43
44**Error codes**
45
46For details about the following error codes, see [Certificate Management Error Codes](errorcode-certManager.md).
47
48| ID| Error Message                                                    |
49| -------- | ------------------------------------------------------------ |
50| 201 | Permission verification failed. The application does not have the permission required to call the API. |
51| 202 | Permission verification failed. A non-system application calls a system API. |
52| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
53| 17500001 | Internal error. |
54
55**Example**
56```ts
57import { certificateManager } from '@kit.DeviceCertificateKit';
58
59try {
60  certificateManager.getAllAppPrivateCertificates((err, cmResult) => {
61    if (err != null) {
62      console.error(`Failed to get all app private certificates. Code: ${err.code}, message: ${err.message}`);
63    } else {
64      if (cmResult == undefined) { // If the number of private credentials is 0, return undefined in cmResult.
65        console.info('the count of the app private certificates is 0');
66      } else if (cmResult.credentialList == undefined) {
67        console.info('The result of getting all app private certificates is undefined.');
68      } else {
69        let list = cmResult.credentialList;
70        console.info('Succeeded in getting all app private certificates.');
71      }
72    }
73  });
74} catch (error) {
75  console.error(`Failed to get all app private certificates. Code: ${error.code}, message: ${error.message}`);
76}
77```
78
79## certificateManager.getAllAppPrivateCertificates
80
81getAllAppPrivateCertificates(): Promise\<CMResult>
82
83Obtains all private credentials. This API uses a promise to return the result.
84
85**Required permissions**: ohos.permission.ACCESS_CERT_MANAGER and ohos.permission.ACCESS_CERT_MANAGER_INTERNAL
86
87**System capability**: System SystemCapability.Security.CertificateManager
88
89**System API**: This is a system API.
90
91**Return value**
92
93| Type                                                 | Description                                                        |
94| ----------------------------------------------------- | ------------------------------------------------------------ |
95| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise used to return all the private credentials obtained, that is, **credentialList** in the [CMResult](#cmresult) object.|
96
97**Error codes**
98
99For details about the following error codes, see [Certificate Management Error Codes](errorcode-certManager.md).
100
101| ID| Error Message     |
102| -------- | ------------- |
103| 201 | Permission verification failed. The application does not have the permission required to call the API. |
104| 202 | Permission verification failed. A non-system application calls a system API. |
105| 17500001 | Internal error. |
106
107**Example**
108```ts
109import { certificateManager } from '@kit.DeviceCertificateKit';
110import { BusinessError } from '@kit.BasicServicesKit';
111
112try {
113  certificateManager.getAllAppPrivateCertificates().then((cmResult) => {
114    if (cmResult == undefined) { // If the number of private credentials is 0, return undefined in cmResult.
115      console.info('the count of the app private certificates is 0');
116    } else if (cmResult.credentialList == undefined) {
117      console.info('The result of getting all app private certificates is undefined.');
118    } else {
119      let list = cmResult.credentialList;
120      console.info('Succeeded in getting all app private certificates.');
121    }
122  }).catch((err: BusinessError) => {
123    console.error(`Failed to get all app private certificates. Code: ${err.code}, message: ${err.message}`);
124  })
125} catch (error) {
126  console.error(`Failed to get all app private certificates. Code: ${error.code}, message: ${error.message}`);
127}
128```
129
130## certificateManager.getAllSystemAppCertificates<sup>12+</sup>
131
132getAllSystemAppCertificates(): Promise\<CMResult>
133
134Obtains all system credentials. This API uses a promise to return the result.
135
136**Required permissions**: ohos.permission.ACCESS_CERT_MANAGER
137
138**System capability**: System SystemCapability.Security.CertificateManager
139
140**System API**: This is a system API.
141
142**Return value**
143
144| Type                                                 | Description                                                        |
145| ----------------------------------------------------- | ------------------------------------------------------------ |
146| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise used to return all the system credentials obtained, that is, **credentialList** in the [CMResult](#cmresult) object.|
147
148**Error codes**
149
150For details about the following error codes, see [Certificate Management Error Codes](errorcode-certManager.md).
151
152| ID| Error Message     |
153| -------- | ------------- |
154| 201 | Permission verification failed. The application does not have the permission required to call the API. |
155| 202 | Permission verification failed. A non-system application calls a system API. |
156| 17500001 | Internal error. |
157
158**Example**
159```ts
160import { certificateManager } from '@kit.DeviceCertificateKit';
161import { BusinessError } from '@kit.BasicServicesKit';
162
163try {
164  certificateManager.getAllSystemAppCertificates().then((cmResult) => {
165    if (cmResult == undefined) { // If the number of system credentials is 0, return undefined in cmResult.
166      console.info('the count of the system certificates is 0');
167    } else if (cmResult.credentialList == undefined) {
168      console.info('The result of getting all system app certificates is undefined.');
169    } else {
170      let list = cmResult.credentialList;
171      console.info('Succeeded in getting all system app certificates.');
172    }
173  }).catch((err: BusinessError) => {
174    console.error(`Failed to get all system app certificates. Code: ${err.code}, message: ${err.message}`);
175  })
176} catch (error) {
177  console.error(`Failed to get all system app certificates. Code: ${error.code}, message: ${error.message}`);
178}
179```
180