1# @ohos.security.certManager (证书管理模块)(系统接口) 2 3<!--Kit: Device Certificate Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @chaceli--> 6<!--Designer: @chande--> 7<!--Tester: @zhangzhi1995--> 8<!--Adviser: @zengyawen--> 9 10证书管理主要提供系统级的证书管理能力,实现证书全生命周期(安装,存储,使用,销毁)的管理和安全使用。 11 12> **说明:** 13> 14> - 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.security.certManager (证书管理模块)](js-apis-certManager.md)。 16 17## 导入模块 18 19```ts 20import { certificateManager } from '@kit.DeviceCertificateKit'; 21``` 22 23## CMErrorCode 24 25表示调用证书管理相关API的错误码。 26 27**系统能力:** SystemCapability.Security.CertificateManager 28 29| 名称 | 值 | 说明 | 30| ---------- | ------ | --------- | 31| CM_ERROR_NOT_SYSTEM_APP | 202 | 表示应用程序不是系统应用程序。 <br> **系统接口:** 此接口为系统接口。 | 32 33## certificateManager.getAllAppPrivateCertificates 34 35getAllAppPrivateCertificates(callback: AsyncCallback\<CMResult>): void 36 37表示获取所有私有凭据列表,使用Callback回调异步返回结果。 38 39**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 和 ohos.permission.ACCESS_CERT_MANAGER_INTERNAL 40 41**系统能力:** SystemCapability.Security.CertificateManager 42 43**系统接口:** 此接口为系统接口。 44 45**参数**: 46 47| 参数名 | 类型 | 必填 | 说明 | 48| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 49| callback | AsyncCallback\<[CMResult](js-apis-certManager.md#cmresult)> | 是 | 回调函数。当获取所有私有凭据列表成功时,err为null,data为[CMResult](js-apis-certManager.md#cmresult)对象中的credentialList属性;否则为错误对象。 | 50 51**错误码:** 52 53以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 54 55| 错误码ID | 错误信息 | 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**示例**: 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) { // 私有凭据个数为0时,返回cmResult为undefined。 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 90表示获取所有私有凭据列表,使用Promise方式异步返回结果。 91 92**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 和 ohos.permission.ACCESS_CERT_MANAGER_INTERNAL 93 94**系统能力:** SystemCapability.Security.CertificateManager 95 96**系统接口:** 此接口为系统接口。 97 98**返回值**: 99 100| 类型 | 说明 | 101| ----------------------------------------------------- | ------------------------------------------------------------ | 102| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise对象。表示获取所有私有凭据列表的结果,返回值为[CMResult](js-apis-certManager.md#cmresult)对象中的credentialList属性。 | 103 104**错误码:** 105 106以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 107 108| 错误码ID | 错误信息 | 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**示例**: 115```ts 116import { certificateManager } from '@kit.DeviceCertificateKit'; 117import { BusinessError } from '@kit.BasicServicesKit'; 118 119try { 120 certificateManager.getAllAppPrivateCertificates().then((cmResult) => { 121 if (cmResult === undefined) { // 私有凭据个数为0时,返回cmResult为undefined。 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 141表示获取所有系统凭据列表,使用Promise方式异步返回结果。 142 143**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 144 145**系统能力:** SystemCapability.Security.CertificateManager 146 147**系统接口:** 此接口为系统接口。 148 149**返回值**: 150 151| 类型 | 说明 | 152| ----------------------------------------------------- | ------------------------------------------------------------ | 153| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise对象。表示获取所有系统凭据列表的结果,返回值为[CMResult](js-apis-certManager.md#cmresult)对象中的credentialList属性。 | 154 155**错误码:** 156 157以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 158 159| 错误码ID | 错误信息 | 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**示例**: 166```ts 167import { certificateManager } from '@kit.DeviceCertificateKit'; 168import { BusinessError } from '@kit.BasicServicesKit'; 169 170try { 171 certificateManager.getAllSystemAppCertificates().then((cmResult) => { 172 if (cmResult === undefined) { // 系统凭据个数为0时,返回cmResult为undefined。 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