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_MANAGER 和 ohos.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.credentialList == undefined) { 65 console.info('The result of getting all app private certificates is undefined.'); 66 } else { 67 let list = cmResult.credentialList; 68 console.info('Succeeded in getting all app private certificates.'); 69 } 70 } 71 }); 72} catch (error) { 73 console.error(`Failed to get all app private certificates. Code: ${error.code}, message: ${error.message}`); 74} 75``` 76 77## certificateManager.getAllAppPrivateCertificates 78 79getAllAppPrivateCertificates(): Promise\<CMResult> 80 81表示获取所有私有凭据列表,使用Promise方式异步返回结果。 82 83**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 和 ohos.permission.ACCESS_CERT_MANAGER_INTERNAL 84 85**系统能力:** SystemCapability.Security.CertificateManager 86 87**系统接口:** 此接口为系统接口。 88 89**返回值**: 90 91| 类型 | 说明 | 92| ----------------------------------------------------- | ------------------------------------------------------------ | 93| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise对象。表示获取所有私有凭据列表的结果,返回值为[CMResult](#cmresult)对象中的credentialList属性。 | 94 95**错误码:** 96 97以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 98 99| 错误码ID | 错误信息 | 100| -------- | ------------- | 101| 201 | Permission verification failed. The application does not have the permission required to call the API. | 102| 202 | Permission verification failed. A non-system application calls a system API. | 103| 17500001 | Internal error. | 104 105**示例**: 106```ts 107import { certificateManager } from '@kit.DeviceCertificateKit'; 108import { BusinessError } from '@kit.BasicServicesKit'; 109 110try { 111 certificateManager.getAllAppPrivateCertificates().then((cmResult) => { 112 if (cmResult.credentialList == undefined) { 113 console.info('The result of getting all app private certificates is undefined.'); 114 } else { 115 let list = cmResult.credentialList; 116 console.info('Succeeded in getting all app private certificates.'); 117 } 118 }).catch((err: BusinessError) => { 119 console.error(`Failed to get all app private certificates. Code: ${err.code}, message: ${err.message}`); 120 }) 121} catch (error) { 122 console.error(`Failed to get all app private certificates. Code: ${error.code}, message: ${error.message}`); 123} 124``` 125 126## certificateManager.getAllSystemAppCertificates<sup>12+</sup> 127 128getAllSystemAppCertificates(): Promise\<CMResult> 129 130表示获取所有系统凭据列表,使用Promise方式异步返回结果。 131 132**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 133 134**系统能力:** SystemCapability.Security.CertificateManager 135 136**系统接口:** 此接口为系统接口。 137 138**返回值**: 139 140| 类型 | 说明 | 141| ----------------------------------------------------- | ------------------------------------------------------------ | 142| Promise\<[CMResult](js-apis-certManager.md#cmresult)> | Promise对象。表示获取所有系统凭据列表的结果,返回值为[CMResult](#cmresult)对象中的credentialList属性。 | 143 144**错误码:** 145 146以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 147 148| 错误码ID | 错误信息 | 149| -------- | ------------- | 150| 201 | Permission verification failed. The application does not have the permission required to call the API. | 151| 202 | Permission verification failed. A non-system application calls a system API. | 152| 17500001 | Internal error. | 153 154**示例**: 155```ts 156import { certificateManager } from '@kit.DeviceCertificateKit'; 157import { BusinessError } from '@kit.BasicServicesKit'; 158 159try { 160 certificateManager.getAllSystemAppCertificates().then((cmResult) => { 161 if (cmResult.credentialList == undefined) { 162 console.info('The result of getting all system app certificates is undefined.'); 163 } else { 164 let list = cmResult.credentialList; 165 console.info('Succeeded in getting all system app certificates.'); 166 } 167 }).catch((err: BusinessError) => { 168 console.error(`Failed to get all system app certificates. Code: ${err.code}, message: ${err.message}`); 169 }) 170} catch (error) { 171 console.error(`Failed to get all system app certificates. Code: ${error.code}, message: ${error.message}`); 172} 173``` 174