1# @ohos.security.certManager (证书管理模块) 2 3证书管理主要提供系统级的证书管理能力,实现证书全生命周期(安装,存储,使用,销毁)的管理和安全使用 。 4 5> **说明:** 6> 7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import { certificateManager } from '@kit.DeviceCertificateKit'; 13``` 14 15## CmKeyPurpose 16 17表示密钥使用目的的枚举,用于签名、验签。 18 19**系统能力:** SystemCapability.Security.CertificateManager 20 21| 名称 | 值 | 说明 | 22| ---------- | ------ | --------- | 23| CM_KEY_PURPOSE_SIGN | 4 | 签名 | 24| CM_KEY_PURPOSE_VERIFY | 8 | 验签 | 25 26## CmKeyDigest 27 28表示签名、验签使用的摘要算法的枚举。 29 30**系统能力:** SystemCapability.Security.CertificateManager 31 32| 名称 | 值 | 说明 | 33| ---------- | ------ | --------- | 34| CM_DIGEST_NONE | 0 | 不需要摘要算法,选用此项时,需要业务传入已经计算过摘要的数据进行签名、验签。 | 35| CM_DIGEST_MD5 | 1 | MD5摘要算法。 | 36| CM_DIGEST_SHA1 | 2 | SHA1摘要算法。 | 37| CM_DIGEST_SHA224 | 3 | SHA224摘要算法。 | 38| CM_DIGEST_SHA256 | 4 | SHA256摘要算法。 | 39| CM_DIGEST_SHA384 | 5 | SHA384摘要算法。 | 40| CM_DIGEST_SHA512 | 6 | SHA512摘要算法。 | 41 42## CmKeyPadding 43 44表示签名、验签使用的填充方式的枚举。 45 46**系统能力:** SystemCapability.Security.CertificateManager 47 48| 名称 | 值 | 说明 | 49| ---------- | ------ | --------- | 50| CM_PADDING_NONE | 0 | 无填充。 | 51| CM_PADDING_PSS | 1 | PSS方式填充。 | 52| CM_PADDING_PKCS1_V1_5 | 2 | PKCS1_V1_5方式填充。 | 53 54## CMSignatureSpec 55 56表示签名、验签操作使用的参数集合,包括密钥使用目的、填充方式和摘要算法。 57 58**系统能力:** SystemCapability.Security.CertificateManager 59 60| 名称 | 类型 | 只读 | 可选 | 说明 | 61| -------------- | --------------------------------- | ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | 62| purpose | [CmKeyPurpose](#cmkeypurpose) | 否 | 否 | 表示密钥使用目的的枚举。 | 63| padding | [CmKeyPadding](#cmkeypadding) | 否 | 是 | 表示填充方式的枚举。 | 64| digest | [CmKeyDigest](#cmkeydigest) | 否 | 是 | 表示摘要算法的枚举。 | 65 66 67## CertInfo 68 69表示证书详细信息。 70 71**系统能力:** SystemCapability.Security.CertificateManager 72 73| 名称 | 类型 | 只读 | 可选 | 说明 | 74| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 75| uri | string | 否 | 否 | 表示证书的唯一标识符。 | 76| certAlias | string | 否 | 否 | 表示证书的别名。 | 77| state | boolean | 否 | 否 | 表示证书的状态,true为启用状态、false为禁用状态。 | 78| issuerName | string | 否 | 否 | 表示证书的颁发者名称。 | 79| subjectName | string | 否 | 否 | 表示证书的使用者名称。 | 80| serial | string | 否 | 否 | 表示证书的序列号。 | 81| notBefore | string | 否 | 否 | 表示证书有效期起始日期。 | 82| notAfter | string | 否 | 否 | 表示证书有效期截止日期。 | 83| fingerprintSha256 | string | 否 | 否 | 表示证书的指纹值。 | 84| cert | Uint8Array | 否 | 否 | 表示证书二进制数据。 | 85 86## CertAbstract 87 88表示证书简要信息。 89 90**系统能力:** SystemCapability.Security.CertificateManager 91 92| 名称 | 类型 | 只读 | 可选 | 说明 | 93| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 94| uri | string | 否 | 否 | 表示证书的唯一标识符。 | 95| certAlias | string | 否 | 否 | 表示证书的别名。 | 96| state | boolean | 否 | 否 | 表示证书的状态,true为启用状态、false为禁用状态。 | 97| subjectName | string | 否 | 否 | 表示证书的使用者名称。 | 98 99## Credential 100 101表示凭据详细信息。 102 103**系统能力:** SystemCapability.Security.CertificateManager 104 105| 名称 | 类型 | 只读 | 可选 | 说明 | 106| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 107| type | string | 否 | 否 | 表示凭据的类型。 | 108| alias | string | 否 | 否 | 表示凭据的别名。 | 109| keyUri | string | 否 | 否 | 表示凭据的唯一标识符。 | 110| certNum | number | 否 | 否 | 表示凭据中包含的证书个数。 | 111| keyNum | number | 否 | 否 | 表示凭据中包含的密钥个数。 | 112| credentialData | Uint8Array | 否 | 否 | 表示凭据二进制数据。 | 113 114## CredentialAbstract 115 116表示凭据的简要信息。 117 118**系统能力:** SystemCapability.Security.CertificateManager 119 120| 名称 | 类型 | 只读 | 可选 | 说明 | 121| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 122| type | string | 否 | 否 | 表示凭据的类型。 | 123| alias | string | 否 | 否 | 表示凭据的别名。 | 124| keyUri | string | 否 | 否 | 表示凭据的唯一标识符。 | 125 126## CMResult 127 128表示接口的返回结果。 129 130**系统能力:** SystemCapability.Security.CertificateManager 131 132| 名称 | 类型 | 只读 | 可选 | 说明 | 133| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 134| certList | Array<[CertAbstract](#certabstract)> | 否 | 是 | 表示证书简要信息的列表。 | 135| certInfo | [CertInfo](#certinfo) | 否 | 是 | 表示证书详情。 | 136| credentialList | Array<[CredentialAbstract](#credentialabstract)> | 否 | 是 | 表示凭据简要信息的列表。 | 137| credential | [Credential](#credential) | 否 | 是 | 表示凭据详情。 | 138| appUidList | Array\<string> | 否 | 是 | 表示授权应用列表。 | 139| uri | string | 否 | 是 | 表示证书或凭据的唯一标识符。 | 140| outData | Uint8Array | 否 | 是 | 表示签名结果。 | 141 142## CMHandle 143 144表示签名、验签的初始化操作句柄。 145 146**系统能力:** SystemCapability.Security.CertificateManager 147 148| 名称 | 类型 | 只读 | 可选 | 说明 | 149| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 150| handle | Uint8Array | 否 | 否 | 签名、验签的初始化操作句柄。 | 151 152## CMErrorCode 153 154表示调用证书管理相关API的错误码。 155 156**系统能力:** SystemCapability.Security.CertificateManager 157 158| 名称 | 值 | 说明 | 159| ---------- | ------ | --------- | 160| CM_ERROR_NO_PERMISSION | 201 | 表示应用程序无权限调用接口。 | 161| CM_ERROR_INVALID_PARAMS | 401 | 表示输入参数无效。 | 162| CM_ERROR_GENERIC | 17500001 | 表示调用接口时发生内部错误。 | 163| CM_ERROR_NO_FOUND | 17500002 | 表示证书或凭据不存在。 | 164| CM_ERROR_INCORRECT_FORMAT | 17500003 | 表示输入证书或凭据的数据格式无效。 | 165| CM_ERROR_MAX_CERT_COUNT_REACHED<sup>12+</sup> | 17500004 | 表示证书或凭据数量达到上限。 | 166| CM_ERROR_NO_AUTHORIZATION<sup>12+</sup> | 17500005 | 表示应用未经用户授权。 | 167 168## certificateManager.installPrivateCertificate 169 170installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string, callback: AsyncCallback\<CMResult>): void 171 172表示安装私有凭据,使用Callback回调异步返回结果。 173 174**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 175 176**系统能力:** SystemCapability.Security.CertificateManager 177 178**参数**: 179 180| 参数名 | 类型 | 必填 | 说明 | 181| -------- | ------------------------------------------------- | ---- | -------------------------- | 182| keystore | Uint8Array | 是 | 表示带有密钥对和证书的密钥库文件。 | 183| keystorePwd | string | 是 | 表示密钥库文件的密码,长度限制32字节以内。 | 184| certAlias | string | 是 | 表示用户输入的凭据别名,当前仅支持传入数字、字母或下划线,长度建议32字节以内。 | 185| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当安装私有凭据成功时,err为null,data为[CMResult](#cmresult)对象中的uri属性;否则为错误对象。 | 186 187**错误码:** 188 189以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 190 191| 错误码ID | 错误信息 | 192| -------- | ------------------------------------------------------------ | 193| 201 | Permission verification failed. The application does not have the permission required to call the API. | 194| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 195| 17500001 | Internal error. | 196| 17500003 | The keystore is in an invalid format or the keystore password is incorrect. | 197| 17500004<sup>12+</sup> | The number of certificates or credentials reaches the maximum allowed. | 198 199**示例**: 200```ts 201import { certificateManager } from '@kit.DeviceCertificateKit'; 202 203/* 安装的凭据数据需要业务赋值,本例数据非凭据数据 */ 204let keystore: Uint8Array = new Uint8Array([ 205 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 206]); 207let keystorePwd: string = "123456"; 208try { 209 certificateManager.installPrivateCertificate(keystore, keystorePwd, "test", (err, cmResult) => { 210 if (err != null) { 211 console.error(`Failed to install private certificate. Code: ${err.code}, message: ${err.message}`); 212 } else { 213 let uri: string = (cmResult?.uri == undefined) ? '' : cmResult.uri; 214 console.info('Succeeded in installing private certificate.'); 215 } 216 }); 217} catch (error) { 218 console.error(`Failed to install private certificate. Code: ${error.code}, message: ${error.message}`); 219} 220``` 221 222## certificateManager.installPrivateCertificate 223 224installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string): Promise\<CMResult> 225 226表示安装私有凭据,使用Promise方式异步返回结果。 227 228**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 229 230**系统能力:** SystemCapability.Security.CertificateManager 231 232**参数**: 233 234| 参数名 | 类型 | 必填 | 说明 | 235| -------- | ------------------------------------------------- | ---- | -------------------------- | 236| keystore | Uint8Array | 是 | 表示带有密钥对和证书的密钥库文件。 | 237| keystorePwd | string | 是 | 表示密钥库文件的密码,长度限制32字节以内。 | 238| certAlias | string | 是 | 表示用户输入的凭据别名,当前仅支持传入数字、字母或下划线,长度建议32字节以内。 | 239 240**返回值**: 241 242| 类型 | 说明 | 243| ------------------------------- | ------------------------------------------------------------ | 244| Promise\<[CMResult](#cmresult)> | Promise对象。表示安装私有凭据的结果,返回值为[CMResult](#cmresult)对象中的uri属性。 | 245 246**错误码:** 247 248以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 249 250| 错误码ID | 错误信息 | 251| -------- | ------------------------------------------------------------ | 252| 201 | Permission verification failed. The application does not have the permission required to call the API. | 253| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 254| 17500001 | Internal error. | 255| 17500003 | The keystore is in an invalid format or the keystore password is incorrect. | 256| 17500004<sup>12+</sup> | The number of certificates or credentials reaches the maximum allowed. | 257 258**示例**: 259 260```ts 261import { certificateManager } from '@kit.DeviceCertificateKit'; 262import { BusinessError } from '@kit.BasicServicesKit'; 263 264/* 安装的凭据数据需要业务赋值,本例数据非凭据数据 */ 265let keystore: Uint8Array = new Uint8Array([ 266 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 267]); 268let keystorePwd: string = "123456"; 269try { 270 certificateManager.installPrivateCertificate(keystore, keystorePwd, 'test').then((cmResult) => { 271 let uri: string = (cmResult?.uri == undefined) ? '' : cmResult.uri; 272 console.info('Succeeded in installing private certificate.'); 273 }).catch((err: BusinessError) => { 274 console.error(`Failed to install private certificate. Code: ${err.code}, message: ${err.message}`); 275 }) 276} catch (error) { 277 console.error(`Failed to install private certificate. Code: ${error.code}, message: ${error.message}`); 278} 279``` 280 281## certificateManager.getPrivateCertificate 282 283getPrivateCertificate(keyUri: string, callback: AsyncCallback\<CMResult>): void 284 285表示获取私有凭据的详细信息,使用Callback回调异步返回结果。 286 287**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 288 289**系统能力:** SystemCapability.Security.CertificateManager 290 291**参数**: 292 293| 参数名 | 类型 | 必填 | 说明 | 294| -------- | ------------------------------------------------- | ---- | -------------------------- | 295| keyUri | string | 是 | 表示待获取凭据的唯一标识符。 | 296| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当获取私有凭据的详细信息成功时,err为null,data为[CMResult](#cmresult)对象中的credential属性;否则为错误对象。 | 297 298**错误码:** 299 300以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 301 302| 错误码ID | 错误信息 | 303| -------- | ------------- | 304| 201 | Permission verification failed. The application does not have the permission required to call the API. | 305| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 306| 17500001 | Internal error. | 307| 17500002 | The certificate does not exist. | 308 309**示例**: 310```ts 311import { certificateManager } from '@kit.DeviceCertificateKit'; 312 313let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 314try { 315 certificateManager.getPrivateCertificate(uri, (err, cmResult) => { 316 if (err != null) { 317 console.error(`Failed to get private certificate. Code: ${err.code}, message: ${err.message}`); 318 } else { 319 if (cmResult?.credential == undefined) { 320 console.info('The result of getting private certificate is undefined.'); 321 } else { 322 let list = cmResult.credential; 323 console.info('Succeeded in getting private certificate.'); 324 } 325 } 326 }); 327} catch (error) { 328 console.error(`Failed to get private certificate. Code: ${error.code}, message: ${error.message}`); 329} 330``` 331 332## certificateManager.getPrivateCertificate 333 334getPrivateCertificate(keyUri: string): Promise\<CMResult> 335 336表示获取私有凭据详情,使用Promise方式异步返回结果。 337 338**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 339 340**系统能力:** SystemCapability.Security.CertificateManager 341 342**参数**: 343 344| 参数名 | 类型 | 必填 | 说明 | 345| -------- | ------------------------------------------------- | ---- | -------------------------- | 346| keyUri | string | 是 | 表示待获取凭据的唯一标识符。 | 347 348**返回值**: 349 350| 类型 | 说明 | 351| ------------------------------- | ------------------------------------------------------------ | 352| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取私有凭据详细信息的结果,返回值为[CMResult](#cmresult)对象中的credential属性。 | 353 354**错误码:** 355 356以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 357 358| 错误码ID | 错误信息 | 359| -------- | ------------- | 360| 201 | Permission verification failed. The application does not have the permission required to call the API. | 361| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 362| 17500001 | Internal error. | 363| 17500002 | The certificate does not exist. | 364 365**示例**: 366```ts 367import { certificateManager } from '@kit.DeviceCertificateKit'; 368import { BusinessError } from '@kit.BasicServicesKit'; 369 370let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 371try { 372 certificateManager.getPrivateCertificate(uri).then((cmResult) => { 373 if (cmResult?.credential == undefined) { 374 console.info('The result of getting private certificate is undefined.'); 375 } else { 376 let list = cmResult.credential; 377 console.info('Succeeded in getting private certificate.'); 378 } 379 }).catch((err: BusinessError) => { 380 console.error(`Failed to get private certificate. Code: ${err.code}, message: ${err.message}`); 381 }) 382} catch (error) { 383 console.error(`Failed to get private certificate. Code: ${error.code}, message: ${error.message}`); 384} 385``` 386 387## certificateManager.uninstallPrivateCertificate 388 389uninstallPrivateCertificate(keyUri: string, callback: AsyncCallback\<void>): void 390 391表示卸载指定的私有凭据,使用Callback回调异步返回结果。 392 393**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 394 395**系统能力:** SystemCapability.Security.CertificateManager 396 397**参数**: 398 399| 参数名 | 类型 | 必填 | 说明 | 400| -------- | ------------------------------------------------- | ---- | -------------------------- | 401| keyUri | string | 是 | 表示待卸载凭据的唯一标识符。 | 402| callback | AsyncCallback\<void> | 是 | 回调函数。当卸载私有凭据成功时,err为null,否则为错误对象。 | 403 404**错误码:** 405 406以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 407 408| 错误码ID | 错误信息 | 409| -------- | ------------- | 410| 201 | Permission verification failed. The application does not have the permission required to call the API. | 411| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 412| 17500001 | Internal error. | 413| 17500002 | The certificate does not exist. | 414 415**示例**: 416```ts 417import { certificateManager } from '@kit.DeviceCertificateKit'; 418 419let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 420try { 421 certificateManager.uninstallPrivateCertificate(uri, (err, result) => { 422 if (err != null) { 423 console.error(`Failed to uninstall private certificate. Code: ${err.code}, message: ${err.message}`); 424 } else { 425 console.info('Succeeded in uninstalling private certificate.'); 426 } 427 }); 428} catch (error) { 429 console.error(`Failed to uninstall private certificate. Code: ${error.code}, message: ${error.message}`); 430} 431``` 432 433## certificateManager.uninstallPrivateCertificate 434 435uninstallPrivateCertificate(keyUri: string): Promise\<void> 436 437表示卸载指定的私有凭据,使用Promise方式异步返回结果。 438 439**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 440 441**系统能力:** SystemCapability.Security.CertificateManager 442 443**参数**: 444 445| 参数名 | 类型 | 必填 | 说明 | 446| -------- | ------------------------------------------------- | ---- | -------------------------- | 447| keyUri | string | 是 | 表示待卸载凭据的唯一标识符。 | 448 449**返回值**: 450 451| 类型 | 说明 | 452| ------------------------------------------- | -------------------- | 453| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 454 455**错误码:** 456 457以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 458 459| 错误码ID | 错误信息 | 460| -------- | ------------- | 461| 201 | Permission verification failed. The application does not have the permission required to call the API. | 462| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 463| 17500001 | Internal error. | 464| 17500002 | The certificate does not exist. | 465 466**示例**: 467```ts 468import { certificateManager } from '@kit.DeviceCertificateKit'; 469import { BusinessError } from '@kit.BasicServicesKit'; 470 471let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 472try { 473 certificateManager.uninstallPrivateCertificate(uri).then((cmResult) => { 474 console.info('Succeeded in uninstalling private certificate.'); 475 }).catch((err: BusinessError) => { 476 console.error(`Failed to uninstall private certificate. Code: ${err.code}, message: ${err.message}`); 477 }) 478} catch (error) { 479 console.error(`Failed to uninstall private certificate. Code: ${error.code}, message: ${error.message}`); 480} 481``` 482 483## certificateManager.init 484 485init(authUri: string, spec: CMSignatureSpec, callback: AsyncCallback\<CMHandle>): void 486 487表示使用凭据进行签名、验签的初始化操作,使用Callback回调异步返回结果。 488 489**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 490 491**系统能力:** SystemCapability.Security.CertificateManager 492 493**参数**: 494 495| 参数名 | 类型 | 必填 | 说明 | 496| -------- | ------------------------------------------------- | ---- | -------------------------- | 497| authUri | string | 是 | 表示使用凭据的唯一标识符。 | 498| spec | [CMSignatureSpec](#cmsignaturespec) | 是 | 表示签名、验签的属性。 | 499| callback | AsyncCallback\<[CMHandle](#cmhandle)> | 是 | 回调函数。当签名、验签的初始化操作成功时,err为null,data为获取到的CMHandle;否则为错误对象。 | 500 501**错误码:** 502 503以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 504 505| 错误码ID | 错误信息 | 506| -------- | ------------- | 507| 201 | Permission verification failed. The application does not have the permission required to call the API. | 508| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 509| 17500001 | Internal error. | 510| 17500002 | The certificate does not exist. | 511| 17500005<sup>12+</sup> | The application is not authorized by the user. | 512 513**示例**: 514```ts 515import { certificateManager } from '@kit.DeviceCertificateKit'; 516 517let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 518const req: certificateManager.CMSignatureSpec = { 519 purpose: certificateManager.CmKeyPurpose.CM_KEY_PURPOSE_SIGN, 520 padding: certificateManager.CmKeyPadding.CM_PADDING_PSS, 521 digest: certificateManager.CmKeyDigest.CM_DIGEST_SHA256 522} 523try { 524 certificateManager.init(uri, req, (err, cmHandle) => { 525 if (err != null) { 526 console.error(`Failed to init. Code: ${err.code}, message: ${err.message}`); 527 } else { 528 console.info('Succeeded in initiating.'); 529 } 530 }) 531} catch (error) { 532 console.error(`Failed to init. Code: ${error.code}, message: ${error.message}`); 533} 534``` 535 536## certificateManager.init 537 538init(authUri: string, spec: CMSignatureSpec): Promise\<CMHandle> 539 540表示使用凭据进行签名、验签的初始化操作,使用Promise方式异步返回结果。 541 542**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 543 544**系统能力:** SystemCapability.Security.CertificateManager 545 546**参数**: 547 548| 参数名 | 类型 | 必填 | 说明 | 549| -------- | ------------------------------------------------- | ---- | -------------------------- | 550| authUri | string | 是 | 表示使用凭据的唯一标识符。 | 551| spec | [CMSignatureSpec](#cmsignaturespec) | 是 | 表示签名、验签的属性。 | 552 553**返回值**: 554 555| 类型 | 说明 | 556| ------------------------------------------- | -------------------- | 557| Promise\<[CMHandle](#cmhandle)> | Promise对象。表示签名、验签的初始化操作结果,返回CMHandle对象。 | 558 559**错误码:** 560 561以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 562 563| 错误码ID | 错误信息 | 564| -------- | ------------- | 565| 201 | Permission verification failed. The application does not have the permission required to call the API. | 566| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 567| 17500001 | Internal error. | 568| 17500002 | The certificate does not exist. | 569| 17500005<sup>12+</sup> | The application is not authorized by the user. | 570 571**示例**: 572```ts 573import { certificateManager } from '@kit.DeviceCertificateKit'; 574import { BusinessError } from '@kit.BasicServicesKit'; 575 576let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 577const req: certificateManager.CMSignatureSpec = { 578 purpose: certificateManager.CmKeyPurpose.CM_KEY_PURPOSE_VERIFY, 579 padding: certificateManager.CmKeyPadding.CM_PADDING_PSS, 580 digest: certificateManager.CmKeyDigest.CM_DIGEST_MD5 581} 582try { 583 certificateManager.init(uri, req).then((handle) => { 584 console.info('Succeeded in initiating.'); 585 }).catch((err: BusinessError) => { 586 console.error(`Failed to init. Code: ${err.code}, message: ${err.message}`); 587 }) 588} catch (error) { 589 console.error(`Failed to init. Code: ${error.code}, message: ${error.message}`); 590} 591``` 592 593## certificateManager.update 594 595update(handle: Uint8Array, data: Uint8Array, callback: AsyncCallback\<void>): void 596 597表示签名、验签的数据更新操作,使用Callback回调异步返回结果。 598 599**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 600 601**系统能力:** SystemCapability.Security.CertificateManager 602 603**参数**: 604 605| 参数名 | 类型 | 必填 | 说明 | 606| -------- | ------------------------------------------------- | ---- | -------------------------- | 607| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 608| data | Uint8Array | 是 | 表示待签名、验签的数据。 | 609| callback | AsyncCallback\<void> | 是 | 回调函数。当签名、验签的数据更新操作成功时,err为null,否则为错误对象。 | 610 611**错误码:** 612 613以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 614 615| 错误码ID | 错误信息 | 616| -------- | ------------------------------------------------------------ | 617| 201 | Permission verification failed. The application does not have the permission required to call the API. | 618| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 619| 17500001 | Internal error. | 620 621**示例**: 622```ts 623import { certificateManager } from '@kit.DeviceCertificateKit'; 624 625/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 626let cmHandle: Uint8Array = new Uint8Array([ 627 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 628]); 629let srcData: Uint8Array = new Uint8Array([ 630 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 631]); 632try { 633 certificateManager.update(cmHandle, srcData, (err, result) => { 634 if (err != null) { 635 console.error(`Failed to update. Code: ${err.code}, message: ${err.message}`); 636 } else { 637 console.info('Succeeded in updating.'); 638 } 639 }); 640} catch (error) { 641 console.error(`Failed to update. Code: ${error.code}, message: ${error.message}`); 642} 643``` 644 645## certificateManager.update 646 647update(handle: Uint8Array, data: Uint8Array): Promise\<void> 648 649表示签名、验签的数据更新操作,使用Promise方式异步返回结果。 650 651**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 652 653**系统能力:** SystemCapability.Security.CertificateManager 654 655**参数**: 656 657| 参数名 | 类型 | 必填 | 说明 | 658| -------- | ------------------------------------------------- | ---- | -------------------------- | 659| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 660| data | Uint8Array | 是 | 表示待签名、验签的数据。 | 661 662**返回值**: 663 664| 类型 | 说明 | 665| ------------------------------------------- | -------------------- | 666| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 667 668**错误码:** 669 670以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 671 672| 错误码ID | 错误信息 | 673| -------- | ------------------------------------------------------------ | 674| 201 | Permission verification failed. The application does not have the permission required to call the API. | 675| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 676| 17500001 | Internal error. | 677 678**示例**: 679```ts 680import { certificateManager } from '@kit.DeviceCertificateKit'; 681import { BusinessError } from '@kit.BasicServicesKit'; 682 683/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 684let cmHandle: Uint8Array = new Uint8Array([ 685 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 686]); 687let srcData: Uint8Array = new Uint8Array([ 688 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 689]); 690try { 691 certificateManager.update(cmHandle, srcData).then((result) => { 692 console.info('Succeeded in updating.'); 693 }).catch((err: BusinessError) => { 694 console.error(`Failed to update. Code: ${err.code}, message: ${err.message}`); 695 }) 696} catch (error) { 697 console.error(`Failed to update. Code: ${error.code}, message: ${error.message}`); 698} 699``` 700 701## certificateManager.finish 702 703finish(handle: Uint8Array, callback: AsyncCallback\<CMResult>): void 704 705表示完成签名的操作,Callback回调异步返回结果。 706 707**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 708 709**系统能力:** SystemCapability.Security.CertificateManager 710 711**参数**: 712 713| 参数名 | 类型 | 必填 | 说明 | 714| -------- | ------------------------------------------------- | ---- | -------------------------- | 715| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 716| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当签名成功时,err为null,data为[CMResult](#cmresult)对象中的outData属性,表示签名数据;否则为错误对象。 | 717 718**错误码:** 719 720以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 721 722| 错误码ID | 错误信息 | 723| -------- | ------------------------------------------------------------ | 724| 201 | Permission verification failed. The application does not have the permission required to call the API. | 725| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 726| 17500001 | Internal error. | 727 728**示例**: 729```ts 730import { certificateManager } from '@kit.DeviceCertificateKit'; 731 732/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 733let cmHandle: Uint8Array = new Uint8Array([ 734 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 735]); 736try { 737 certificateManager.finish(cmHandle, (err, cmResult) => { 738 if (err != null) { 739 console.error(`Failed to finish. Code: ${err.code}, message: ${err.message}`); 740 } else { 741 if (cmResult?.outData != undefined) { 742 let signRes: Uint8Array = cmResult.outData; 743 console.info('Succeeded in finishing.'); 744 } else { 745 console.info('The result of finishing is undefined.'); 746 } 747 } 748 }); 749} catch(error) { 750 console.error(`Failed to finish. Code: ${error.code}, message: ${error.message}`); 751} 752``` 753 754## certificateManager.finish 755 756finish(handle: Uint8Array, signature: Uint8Array, callback: AsyncCallback\<CMResult>): void 757 758表示完成验签的操作,使用Callback回调异步返回结果。 759 760**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 761 762**系统能力:** SystemCapability.Security.CertificateManager 763 764**参数**: 765 766| 参数名 | 类型 | 必填 | 说明 | 767| -------- | ------------------------------------------------- | ---- | -------------------------- | 768| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 769| signature | Uint8Array | 是 | 表示签名数据。 | 770| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当验签成功时,err为null;否则为错误对象。 | 771 772**错误码:** 773 774以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 775 776| 错误码ID | 错误信息 | 777| -------- | ------------------------------------------------------------ | 778| 201 | Permission verification failed. The application does not have the permission required to call the API. | 779| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 780| 17500001 | Internal error. | 781 782**示例**: 783```ts 784import { certificateManager } from '@kit.DeviceCertificateKit'; 785 786/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 787let cmHandle: Uint8Array = new Uint8Array([ 788 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 789]); 790let signRes: Uint8Array = new Uint8Array([ 791 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 792]); 793try { 794 certificateManager.finish(cmHandle, signRes, (err, cmResult) => { 795 if (err != null) { 796 console.error(`Failed to finish. Code: ${err.code}, message: ${err.message}`); 797 } else { 798 console.info('Succeeded in finishing.'); 799 } 800 }); 801} catch(error) { 802 console.error(`Failed to finish. Code: ${error.code}, message: ${error.message}`); 803} 804``` 805 806## certificateManager.finish 807 808finish(handle: Uint8Array, signature?: Uint8Array): Promise\<CMResult> 809 810表示完成签名、验签的操作,使用Promise方式异步返回结果。 811 812**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 813 814**系统能力:** SystemCapability.Security.CertificateManager 815 816**参数**: 817 818| 参数名 | 类型 | 必填 | 说明 | 819| -------- | ------------------------------------------------- | ---- | -------------------------- | 820| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 821| signature | Uint8Array | 否 | 表示签名数据。 | 822 823**返回值**: 824 825| 类型 | 说明 | 826| ------------------------------- | ------------------------------------------------------------ | 827| Promise\<[CMResult](#cmresult)> | Promise对象。执行签名操作时,表示签名的结果,返回值为[CMResult](#cmresult)对象中的outData属性;执行验签操作时,无返回结果的Promise对象。 | 828 829**错误码:** 830 831以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 832 833| 错误码ID | 错误信息 | 834| -------- | ------------------------------------------------------------ | 835| 201 | Permission verification failed. The application does not have the permission required to call the API. | 836| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 837| 17500001 | Internal error. | 838 839**示例**: 840```ts 841import { certificateManager } from '@kit.DeviceCertificateKit'; 842import { BusinessError } from '@kit.BasicServicesKit'; 843 844/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 845let cmHandle: Uint8Array = new Uint8Array([ 846 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 847]); 848try { 849 /* 签名的finish操作 */ 850 certificateManager.finish(cmHandle).then((cmResult) => { 851 if (cmResult?.outData != undefined) { 852 let signRes1: Uint8Array = cmResult.outData; 853 console.info('Succeeded in finishing signature.'); 854 } else { 855 console.info('The result of signature is undefined.'); 856 } 857 }).catch((err: BusinessError) => { 858 console.error(`Failed to finish signature. Code: ${err.code}, message: ${err.message}`); 859 }) 860 861 /* 签名的结果 */ 862 let signRes: Uint8Array = new Uint8Array([ 863 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 864 ]); 865 /* 验签的finish操作 */ 866 certificateManager.finish(cmHandle, signRes).then((cmResult) => { 867 console.info('Succeeded in finishing verification.'); 868 }).catch((err: BusinessError) => { 869 console.error(`Failed to finish verification. Code: ${err.code}, message: ${err.message}`); 870 }) 871} catch(error) { 872 console.error(`Failed to finish. Code: ${error.code}, message: ${error.message}`); 873} 874``` 875 876## certificateManager.abort 877 878abort(handle: Uint8Array, callback: AsyncCallback\<void>): void 879 880表示中止签名、验签的操作,使用Callback回调异步返回结果。 881 882**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 883 884**系统能力:** SystemCapability.Security.CertificateManager 885 886**参数**: 887 888| 参数名 | 类型 | 必填 | 说明 | 889| -------- | ------------------------------------------------- | ---- | -------------------------- | 890| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 891| callback | AsyncCallback\<void> | 是 | 回调函数。当中止签名、验签成功时,err为null,否则为错误对象。 | 892 893**错误码:** 894 895以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 896 897| 错误码ID | 错误信息 | 898| -------- | ------------------------------------------------------------ | 899| 201 | Permission verification failed. The application does not have the permission required to call the API. | 900| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 901| 17500001 | Internal error. | 902 903**示例**: 904```ts 905import { certificateManager } from '@kit.DeviceCertificateKit'; 906 907/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 908let cmHandle: Uint8Array = new Uint8Array([ 909 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 910]); 911try { 912 certificateManager.abort(cmHandle, (err, cmResult) => { 913 if (err != null) { 914 console.error(`Failed to abort. Code: ${err.code}, message: ${err.message}`); 915 } else { 916 console.info('Succeeded in aborting.'); 917 } 918 }); 919} catch(error) { 920 console.error(`Failed to abort. Code: ${error.code}, message: ${error.message}`); 921} 922``` 923 924## certificateManager.abort 925 926abort(handle: Uint8Array): Promise\<void> 927 928表示中止签名、验签的操作,使用Promise方式异步返回结果。 929 930**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 931 932**系统能力:** SystemCapability.Security.CertificateManager 933 934**参数**: 935 936| 参数名 | 类型 | 必填 | 说明 | 937| -------- | ------------------------------------------------- | ---- | -------------------------- | 938| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 939 940**返回值**: 941 942| 类型 | 说明 | 943| ------------------------------------------- | -------------------- | 944| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 945 946**错误码:** 947 948以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 949 950| 错误码ID | 错误信息 | 951| -------- | ------------------------------------------------------------ | 952| 201 | Permission verification failed. The application does not have the permission required to call the API. | 953| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 954| 17500001 | Internal error. | 955 956**示例**: 957```ts 958import { certificateManager } from '@kit.DeviceCertificateKit'; 959import { BusinessError } from '@kit.BasicServicesKit'; 960 961/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 962let cmHandle: Uint8Array = new Uint8Array([ 963 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 964]); 965try { 966 certificateManager.abort(cmHandle).then((result) => { 967 console.info('Succeeded in aborting.'); 968 }).catch((err: BusinessError) => { 969 console.error(`Failed to abort. Code: ${err.code}, message: ${err.message}`); 970 }) 971} catch (error) { 972 console.error(`Failed to abort. Code: ${error.code}, message: ${error.message}`); 973} 974``` 975 976## certificateManager.getPublicCertificate<sup>12+</sup> 977 978getPublicCertificate(keyUri: string): Promise\<CMResult> 979 980表示获取用户公共凭据的详细信息,使用Promise方式异步返回结果。 981 982**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 983 984**系统能力:** SystemCapability.Security.CertificateManager 985 986**参数**: 987 988| 参数名 | 类型 | 必填 | 说明 | 989| -------- | ------------------------------------------------- | ---- | -------------------------- | 990| keyUri | string | 是 | 表示用户公共凭据的唯一标识符。 | 991 992**返回值**: 993 994| 类型 | 说明 | 995| ------------------------------- | ------------------------------------------------------------ | 996| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取用户公共凭据详细信息的结果,返回值为[CMResult](#cmresult)对象中的credential属性。 | 997 998**错误码:** 999 1000以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1001 1002| 错误码ID | 错误信息 | 1003| -------- | ------------- | 1004| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1005| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1006| 17500001 | Internal error. | 1007| 17500002 | The certificate does not exist. | 1008| 17500005 | The application is not authorized by the user. | 1009 1010**示例**: 1011```ts 1012import { certificateManager } from '@kit.DeviceCertificateKit'; 1013import { BusinessError } from '@kit.BasicServicesKit'; 1014 1015let uri: string = 'test'; /* 用户安装公共凭据返回的唯一标识符,此处省略 */ 1016try { 1017 certificateManager.getPublicCertificate(uri).then((cmResult) => { 1018 if (cmResult?.credential == undefined) { 1019 console.info('The result of getting public certificate is undefined.'); 1020 } else { 1021 let cred = cmResult.credential; 1022 console.info('Succeeded in getting Public certificate.'); 1023 } 1024 }).catch((err: BusinessError) => { 1025 console.error(`Failed to get Public certificate. Code: ${err.code}, message: ${err.message}`); 1026 }) 1027} catch (error) { 1028 console.error(`Failed to get Public certificate. Code: ${error.code}, message: ${error.message}`); 1029} 1030``` 1031 1032## certificateManager.isAuthorizedApp<sup>12+</sup> 1033 1034isAuthorizedApp(keyUri: string): Promise\<boolean> 1035 1036表示当前应用是否由指定的用户凭据授权,使用Promise方式异步返回结果。 1037 1038**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1039 1040**系统能力:** SystemCapability.Security.CertificateManager 1041 1042**参数**: 1043 1044| 参数名 | 类型 | 必填 | 说明 | 1045| -------- | ------------------------------------------------- | ---- | -------------------------- | 1046| keyUri | string | 是 | 表示用户授权给应用使用的凭据的唯一标识符。 | 1047 1048**返回值**: 1049 1050| 类型 | 说明 | 1051| ----------------- | ------------------------------------------------------------ | 1052| Promise\<boolean> | Promise对象。表示查询应用是否被授权的结果,返回boolean对象。 | 1053 1054**错误码:** 1055 1056以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1057 1058| 错误码ID | 错误信息 | 1059| -------- | ------------- | 1060| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1061| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1062| 17500001 | Internal error. | 1063 1064**示例**: 1065```ts 1066import { certificateManager } from '@kit.DeviceCertificateKit'; 1067import { BusinessError } from '@kit.BasicServicesKit'; 1068 1069let uri: string = 'test'; /* 用户授权给应用使用的凭据的唯一标识符,此处省略 */ 1070try { 1071 certificateManager.isAuthorizedApp(uri).then((res) => { 1072 if (res) { 1073 console.info('The application is authorized by the user.'); 1074 } else { 1075 console.info('The application is not authorized by the user.'); 1076 } 1077 }).catch((err: BusinessError) => { 1078 console.error(`Failed to get Public certificate. Code: ${err.code}, message: ${err.message}`); 1079 }) 1080} catch (error) { 1081 console.error(`Failed to get Public certificate. Code: ${error.code}, message: ${error.message}`); 1082} 1083``` 1084 1085## certificateManager.getAllUserTrustedCertificates<sup>12+</sup> 1086 1087getAllUserTrustedCertificates(): Promise\<CMResult> 1088 1089表示获取所有用户根CA证书列表,使用Promise方式异步返回结果。 1090 1091**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1092 1093**系统能力:** SystemCapability.Security.CertificateManager 1094 1095**返回值**: 1096 1097| 类型 | 说明 | 1098| ------------------------------- | ------------------------------------------------------------ | 1099| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取用户根CA证书列表的结果,返回值[CMResult](#cmresult)对象中的certList属性。 | 1100 1101**错误码:** 1102 1103以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1104 1105| 错误码ID | 错误信息 | 1106| -------- | ------------- | 1107| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1108| 17500001 | Internal error. | 1109 1110**示例**: 1111```ts 1112import { certificateManager } from '@kit.DeviceCertificateKit'; 1113import { BusinessError } from '@kit.BasicServicesKit'; 1114 1115try { 1116 certificateManager.getAllUserTrustedCertificates().then((cmResult) => { 1117 if (cmResult == undefined) { // 用户根CA证书个数为0时,返回cmResult为undefined 1118 console.info('the count of the user trusted certificates is 0'); 1119 } else if (cmResult.certList == undefined) { 1120 console.info('The result of getting all user trusted certificates is undefined.'); 1121 } else { 1122 let list = cmResult.certList; 1123 console.info('Succeeded in getting all user trusted certificates.'); 1124 } 1125 }).catch((err: BusinessError) => { 1126 console.error(`Failed to get all user trusted certificates. Code: ${err.code}, message: ${err.message}`); 1127 }) 1128} catch (error) { 1129 console.error(`Failed to get all user trusted certificates. Code: ${error.code}, message: ${error.message}`); 1130} 1131``` 1132 1133## certificateManager.getUserTrustedCertificate<sup>12+</sup> 1134 1135getUserTrustedCertificate(certUri: string): Promise\<CMResult> 1136 1137表示获取用户根CA证书的详细信息,使用Promise方式异步返回结果。 1138 1139**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1140 1141**系统能力:** SystemCapability.Security.CertificateManager 1142 1143**参数**: 1144 1145| 参数名 | 类型 | 必填 | 说明 | 1146| -------- | ------------------------------------------------- | ---- | -------------------------- | 1147| certUri | string | 是 | 表示用户用户根CA证书的唯一标识符。 | 1148 1149**返回值**: 1150 1151| 类型 | 说明 | 1152| ------------------------------- | ------------------------------------------------------------ | 1153| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取用户根CA证书详细信息的结果,返回值为[CMResult](#cmresult)对象中的certInfo属性。 | 1154 1155**错误码:** 1156 1157以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1158 1159| 错误码ID | 错误信息 | 1160| -------- | ------------- | 1161| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1162| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1163| 17500001 | Internal error. | 1164| 17500002 | The certificate does not exist. | 1165 1166**示例**: 1167```ts 1168import { certificateManager } from '@kit.DeviceCertificateKit'; 1169import { BusinessError } from '@kit.BasicServicesKit'; 1170 1171let certUri: string = 'testUserCert'; /* 用户安装用户根CA证书返回的唯一标识符,此处省略 */ 1172try { 1173 certificateManager.getUserTrustedCertificate(certUri).then((cmResult) => { 1174 if (cmResult?.certInfo == undefined) { 1175 console.info('The result of getting user trusted certificate is undefined.'); 1176 } else { 1177 let cert = cmResult.certInfo; 1178 console.info('Succeeded in getting user trusted certificate.'); 1179 } 1180 }).catch((err: BusinessError) => { 1181 console.error(`Failed to get user trusted certificate. Code: ${err.code}, message: ${err.message}`); 1182 }) 1183} catch (error) { 1184 console.error(`Failed to get user trusted certificate. Code: ${error.code}, message: ${error.message}`); 1185} 1186``` 1187## certificateManager.getPrivateCertificates<sup>13+</sup> 1188 1189getPrivateCertificates(): Promise\<CMResult> 1190 1191表示获取应用安装的凭据列表,使用Promise方式异步返回结果。 1192 1193**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1194 1195**系统能力:** SystemCapability.Security.CertificateManager 1196 1197**返回值**: 1198 1199| 类型 | 说明 | 1200| ------------------------------- | ------------------------------------------------------------ | 1201| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取应用安装的凭据列表的结果,返回值[CMResult](#cmresult)对象中的credentialList属性。 | 1202 1203**错误码:** 1204 1205以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1206 1207| 错误码ID | 错误信息 | 1208| -------- | ------------- | 1209| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1210| 17500001 | Internal error. | 1211 1212**示例**: 1213```ts 1214import { certificateManager } from '@kit.DeviceCertificateKit'; 1215import { BusinessError } from '@kit.BasicServicesKit'; 1216 1217try { 1218 certificateManager.getPrivateCertificates().then((cmResult) => { 1219 if (cmResult == undefined) { // 应用安装的凭据个数为0时,返回cmResult为undefined 1220 console.info('the count of the private certificates is 0'); 1221 } else if (cmResult.credentialList == undefined) { 1222 console.info('The result of getting all private certificates installed by the application is undefined.'); 1223 } else { 1224 let list = cmResult.credentialList; 1225 console.info('Succeeded in getting all private certificates installed by the application.'); 1226 } 1227 }).catch((err: BusinessError) => { 1228 console.error(`Failed to get all private certificates installed by the application. Code: ${err.code}, message: ${err.message}`); 1229 }) 1230} catch (error) { 1231 console.error(`Failed to get all private certificates installed by the application. Code: ${error.code}, message: ${error.message}`); 1232} 1233```