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