1# @ohos.security.certManager (证书管理模块)(系统接口)
2
3证书管理主要提供系统级的证书管理能力,实现证书全生命周期(安装,存储,使用,销毁)的管理和安全使用 。
4
5> **说明:**
6>
7> - 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.security.certManager (证书管理模块)](js-apis-certManager.md)。
9
10## 导入模块
11
12```ts
13import { certificateManager } from '@kit.DeviceCertificateKit';
14```
15
16## CMErrorCode
17
18表示调用证书管理相关API的错误码。
19
20**系统能力:** SystemCapability.Security.CertificateManager
21
22| 名称       | 值 |  说明      |
23| ---------- | ------ | --------- |
24| CM_ERROR_NOT_SYSTEM_APP   | 202      | 表示应用程序不是系统应用程序 <br> **系统接口:** 此接口为系统接口。 |
25
26## certificateManager.getAllAppPrivateCertificates
27
28getAllAppPrivateCertificates(callback: AsyncCallback\<CMResult>): void
29
30表示获取所有私有凭据列表,使用Callback回调异步返回结果。
31
32**需要权限:** ohos.permission.ACCESS_CERT_MANAGERohos.permission.ACCESS_CERT_MANAGER_INTERNAL
33
34**系统能力:** SystemCapability.Security.CertificateManager
35
36**系统接口:** 此接口为系统接口。
37
38**参数**:
39
40| 参数名   | 类型                                                        | 必填 | 说明                                                         |
41| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ |
42| callback | AsyncCallback\<[CMResult](js-apis-certManager.md#cmresult)> | 是   | 回调函数。当获取所有私有凭据列表成功时,err为null,data为[CMResult](#cmresult)对象中的credentialList属性;否则为错误对象。 |
43
44**错误码:**
45
46以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。
47
48| 错误码ID | 错误信息                                                     |
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**示例**:
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) { // 私有凭据个数为0时,返回cmResult为undefined
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
83表示获取所有私有凭据列表,使用Promise方式异步返回结果。
84
85**需要权限:** ohos.permission.ACCESS_CERT_MANAGERohos.permission.ACCESS_CERT_MANAGER_INTERNAL
86
87**系统能力:** SystemCapability.Security.CertificateManager
88
89**系统接口:** 此接口为系统接口。
90
91**返回值**:
92
93| 类型                                                  | 说明                                                         |
94| ----------------------------------------------------- | ------------------------------------------------------------ |
95| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise对象。表示获取所有私有凭据列表的结果,返回值为[CMResult](#cmresult)对象中的credentialList属性。 |
96
97**错误码:**
98
99以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。
100
101| 错误码ID | 错误信息      |
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**示例**:
108```ts
109import { certificateManager } from '@kit.DeviceCertificateKit';
110import { BusinessError } from '@kit.BasicServicesKit';
111
112try {
113  certificateManager.getAllAppPrivateCertificates().then((cmResult) => {
114    if (cmResult == undefined) { // 私有凭据个数为0时,返回cmResult为undefined
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
134表示获取所有系统凭据列表,使用Promise方式异步返回结果。
135
136**需要权限:** ohos.permission.ACCESS_CERT_MANAGER
137
138**系统能力:** SystemCapability.Security.CertificateManager
139
140**系统接口:** 此接口为系统接口。
141
142**返回值**:
143
144| 类型                                                  | 说明                                                         |
145| ----------------------------------------------------- | ------------------------------------------------------------ |
146| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise对象。表示获取所有系统凭据列表的结果,返回值为[CMResult](#cmresult)对象中的credentialList属性。 |
147
148**错误码:**
149
150以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。
151
152| 错误码ID | 错误信息      |
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**示例**:
159```ts
160import { certificateManager } from '@kit.DeviceCertificateKit';
161import { BusinessError } from '@kit.BasicServicesKit';
162
163try {
164  certificateManager.getAllSystemAppCertificates().then((cmResult) => {
165    if (cmResult == undefined) { // 系统凭据个数为0时,返回cmResult为undefined
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