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 16## 导入模块 17 18```ts 19import { certificateManager } from '@kit.DeviceCertificateKit'; 20``` 21 22## CmKeyPurpose 23 24表示密钥使用目的的枚举,用于签名、验签。 25 26**系统能力:** SystemCapability.Security.CertificateManager 27 28| 名称 | 值 | 说明 | 29| ---------- | ------ | --------- | 30| CM_KEY_PURPOSE_SIGN | 4 | 签名。 | 31| CM_KEY_PURPOSE_VERIFY | 8 | 验签。 | 32 33## CmKeyDigest 34 35表示签名、验签使用的摘要算法的枚举。 36 37**系统能力:** SystemCapability.Security.CertificateManager 38 39| 名称 | 值 | 说明 | 40| ---------- | ------ | --------- | 41| CM_DIGEST_NONE | 0 | 不需要摘要算法,选用此项时,需要业务传入已经计算过摘要的数据进行签名、验签。 | 42| CM_DIGEST_MD5 | 1 | MD5摘要算法。 | 43| CM_DIGEST_SHA1 | 2 | SHA1摘要算法。 | 44| CM_DIGEST_SHA224 | 3 | SHA224摘要算法。 | 45| CM_DIGEST_SHA256 | 4 | SHA256摘要算法。 | 46| CM_DIGEST_SHA384 | 5 | SHA384摘要算法。 | 47| CM_DIGEST_SHA512 | 6 | SHA512摘要算法。 | 48| CM_DIGEST_SM3<sup>18+</sup> | 7 | SM3摘要算法。 | 49 50## CmKeyPadding 51 52表示签名、验签使用的填充方式的枚举。 53 54**系统能力:** SystemCapability.Security.CertificateManager 55 56| 名称 | 值 | 说明 | 57| ---------- | ------ | --------- | 58| CM_PADDING_NONE | 0 | 无填充。 | 59| CM_PADDING_PSS | 1 | PSS方式填充。 | 60| CM_PADDING_PKCS1_V1_5 | 2 | PKCS1_V1_5方式填充。 | 61 62## CMSignatureSpec 63 64表示签名、验签操作使用的参数集合,包括密钥使用目的、填充方式和摘要算法。 65 66**系统能力:** SystemCapability.Security.CertificateManager 67 68| 名称 | 类型 | 只读 | 可选 | 说明 | 69| -------------- | --------------------------------- | ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | 70| purpose | [CmKeyPurpose](#cmkeypurpose) | 否 | 否 | 表示密钥使用目的的枚举。 | 71| padding | [CmKeyPadding](#cmkeypadding) | 否 | 是 | 表示填充方式的枚举。 | 72| digest | [CmKeyDigest](#cmkeydigest) | 否 | 是 | 表示摘要算法的枚举。 | 73 74 75## CertInfo 76 77表示证书详细信息。 78 79**系统能力:** SystemCapability.Security.CertificateManager 80 81| 名称 | 类型 | 只读 | 可选 | 说明 | 82| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 83| uri | string | 否 | 否 | 表示证书的唯一标识符,最大长度为256字节。 | 84| certAlias | string | 否 | 否 | 表示证书的别名,最大长度为128字节。 | 85| state | boolean | 否 | 否 | 表示证书的状态,true为启用状态、false为禁用状态。 | 86| issuerName | string | 否 | 否 | 表示证书的颁发者名称,最大长度为256字节。 | 87| subjectName | string | 否 | 否 | 表示证书的使用者名称,最大长度为1024字节。 | 88| serial | string | 否 | 否 | 表示证书的序列号,最大长度为64字节。 | 89| notBefore | string | 否 | 否 | 表示证书有效期起始日期,最大长度为32字节。 | 90| notAfter | string | 否 | 否 | 表示证书有效期截止日期,最大长度为32字节。 | 91| fingerprintSha256 | string | 否 | 否 | 表示证书的指纹值,最大长度为128字节。 | 92| cert | Uint8Array | 否 | 否 | 表示证书二进制数据,最大长度为8196字节。 | 93 94## CertAbstract 95 96表示证书简要信息。 97 98**系统能力:** SystemCapability.Security.CertificateManager 99 100| 名称 | 类型 | 只读 | 可选 | 说明 | 101| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 102| uri | string | 否 | 否 | 表示证书的唯一标识符,最大长度为256字节。 | 103| certAlias | string | 否 | 否 | 表示证书的别名,最大长度为128字节。 | 104| state | boolean | 否 | 否 | 表示证书的状态,true为启用状态、false为禁用状态。 | 105| subjectName | string | 否 | 否 | 表示证书的使用者名称,最大长度为1024字节。 | 106 107## Credential 108 109表示凭据详细信息。 110 111**系统能力:** SystemCapability.Security.CertificateManager 112 113| 名称 | 类型 | 只读 | 可选 | 说明 | 114| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 115| type | string | 否 | 否 | 表示凭据的类型,最大长度为8字节。 | 116| alias | string | 否 | 否 | 表示凭据的别名,最大长度为128字节。 | 117| keyUri | string | 否 | 否 | 表示凭据的唯一标识符,最大长度为256字节。 | 118| certNum | number | 否 | 否 | 表示凭据中包含的证书个数。 | 119| keyNum | number | 否 | 否 | 表示凭据中包含的密钥个数。 | 120| credentialData | Uint8Array | 否 | 否 | 表示凭据二进制数据,最大长度为20480字节。 | 121 122## CredentialAbstract 123 124表示凭据的简要信息。 125 126**系统能力:** SystemCapability.Security.CertificateManager 127 128| 名称 | 类型 | 只读 | 可选 | 说明 | 129| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 130| type | string | 否 | 否 | 表示凭据的类型,最大长度为8字节。 | 131| alias | string | 否 | 否 | 表示凭据的别名,最大长度为128字节。 | 132| keyUri | string | 否 | 否 | 表示凭据的唯一标识符,最大长度为256字节。 | 133 134## CMResult 135 136表示接口的返回结果。 137 138**系统能力:** SystemCapability.Security.CertificateManager 139 140| 名称 | 类型 | 只读 | 可选 | 说明 | 141| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 142| certList | Array<[CertAbstract](#certabstract)> | 否 | 是 | 表示证书简要信息的列表。 | 143| certInfo | [CertInfo](#certinfo) | 否 | 是 | 表示证书详情。 | 144| credentialList | Array<[CredentialAbstract](#credentialabstract)> | 否 | 是 | 表示凭据简要信息的列表。 | 145| credential | [Credential](#credential) | 否 | 是 | 表示凭据详情。 | 146| appUidList | Array\<string> | 否 | 是 | 表示授权应用列表。 | 147| uri | string | 否 | 是 | 表示证书或凭据的唯一标识符,最大长度为256字节。 | 148| outData | Uint8Array | 否 | 是 | 表示签名结果。 | 149 150## CMHandle 151 152表示签名、验签的初始化操作句柄。 153 154**系统能力:** SystemCapability.Security.CertificateManager 155 156| 名称 | 类型 | 只读 | 可选 | 说明 | 157| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 158| handle | Uint8Array | 否 | 否 | 签名、验签的初始化操作句柄,最大长度为8字节。 | 159 160## CMErrorCode 161 162表示调用证书管理相关API的错误码。 163 164**系统能力:** SystemCapability.Security.CertificateManager 165 166| 名称 | 值 | 说明 | 167| ---------- | ------ | --------- | 168| CM_ERROR_NO_PERMISSION | 201 | 表示应用程序无权限调用接口。 | 169| CM_ERROR_INVALID_PARAMS | 401 | 表示输入参数无效。 | 170| CM_ERROR_GENERIC | 17500001 | 表示调用接口时发生内部错误。 | 171| CM_ERROR_NO_FOUND | 17500002 | 表示证书或凭据不存在。 | 172| CM_ERROR_INCORRECT_FORMAT | 17500003 | 表示输入证书或凭据的数据格式无效。 | 173| CM_ERROR_MAX_CERT_COUNT_REACHED<sup>12+</sup> | 17500004 | 表示证书或凭据数量达到上限。 | 174| CM_ERROR_NO_AUTHORIZATION<sup>12+</sup> | 17500005 | 表示应用未经用户授权。 | 175| CM_ERROR_DEVICE_ENTER_ADVSECMODE<sup>18+</sup> | 17500007 | 表示设备进入坚盾守护模式。 | 176| CM_ERROR_STORE_PATH_NOT_SUPPORTED<sup>20+</sup> | 17500009 | 表示不支持指定的证书存储路径。 | 177 178## CertType<sup>18+</sup> 179 180表示证书类型。 181 182**系统能力:** SystemCapability.Security.CertificateManager 183 184| 名称 | 值 | 说明 | 185| ---------- | ------ | --------- | 186| CA_CERT_SYSTEM | 0 | 表示系统CA证书。 | 187| CA_CERT_USER | 1 | 表示用户CA证书。 | 188 189## CertScope<sup>18+</sup> 190 191表示证书的位置。 192 193**系统能力:** SystemCapability.Security.CertificateManager 194 195| 名称 | 值 | 说明 | 196| ---------- | ------ | --------- | 197| CURRENT_USER | 1 | 表示当前用户。 | 198| GLOBAL_USER | 2 | 表示设备公共,即所有用户都可以访问的位置。 | 199 200## CertAlgorithm<sup>20+</sup> 201 202表示证书的算法类型。 203 204**系统能力:** SystemCapability.Security.CertificateManager 205 206| 名称 | 值 | 说明 | 207|---------------| ------ |--------------------------| 208| INTERNATIONAL | 1 | 表示国际密码算法,如RSA、NIST ECC等。 | 209| SM | 2 | 表示商用密码算法,如SM2、SM4等。 | 210 211## CertStoreProperty<sup>18+</sup> 212 213表示获取证书存储位置的参数集合,包括证书的类型及证书的位置。 214 215**系统能力:** SystemCapability.Security.CertificateManager 216 217| 名称 | 类型 | 只读 | 可选 | 说明 | 218|-----------|-----------------------------------| ---- | ------------------------------------------------------------ |---------------------------------------------| 219| certType | [CertType](#certtype18) | 否 | 否 | 表示证书的类型。 | 220| certScope | [CertScope](#certscope18) | 否 | 是 | 表示证书的存储位置。当证书类型为CA_CERT_USER时,此项为必选项。 | 221| certAlg<sup>20+</sup> | [CertAlgorithm](#certalgorithm20) | 否 | 是 | 表示证书算法类型。仅当certType为CA_CERT_SYSTEM时有效,默认值为INTERNATIONAL。 | 222 223## AuthStorageLevel<sup>18+</sup> 224 225表示凭据的存储级别。 226 227**系统能力:** SystemCapability.Security.CertificateManager 228 229| 名称 | 值 | 说明 | 230| ------------ | ---- | ------------------------------------------ | 231| EL1 | 1 | EL1级别,表示设备启动后可以访问。 | 232| EL2 | 2 | EL2级别,表示设备首次解锁后可以访问。 | 233| EL4 | 4 | EL4级别,表示设备解锁时可以访问。 | 234 235## certificateManager.installPrivateCertificate 236 237installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string, callback: AsyncCallback\<CMResult>): void 238 239表示安装私有凭据,使用Callback回调异步返回结果。 240 241**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 242 243**系统能力:** SystemCapability.Security.CertificateManager 244 245**参数**: 246 247| 参数名 | 类型 | 必填 | 说明 | 248| -------- | ------------------------------------------------- | ---- | -------------------------- | 249| keystore | Uint8Array | 是 | 表示带有密钥对和证书的密钥库文件,最大长度为20480字节。 | 250| keystorePwd | string | 是 | 表示密钥库文件的密码,长度限制32字节以内。 | 251| certAlias | string | 是 | 表示用户输入的凭据别名,当前仅支持传入数字、字母或下划线,长度建议32字节以内。 | 252| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当安装私有凭据成功时,err为null,data为[CMResult](#cmresult)对象中的uri属性;否则为错误对象。 | 253 254**错误码:** 255 256以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 257 258| 错误码ID | 错误信息 | 259| -------- | ------------------------------------------------------------ | 260| 201 | Permission verification failed. The application does not have the permission required to call the API. | 261| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 262| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 263| 17500003 | The keystore is in an invalid format or the keystore password is incorrect. | 264| 17500004 | The number of certificates or credentials reaches the maximum allowed. | 265 266**示例**: 267```ts 268import { certificateManager } from '@kit.DeviceCertificateKit'; 269 270/* 安装的凭据数据需要业务赋值,本例数据非凭据数据 */ 271let keystore: Uint8Array = new Uint8Array([ 272 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 273]); 274let keystorePwd: string = "123456"; 275try { 276 certificateManager.installPrivateCertificate(keystore, keystorePwd, "test", (err, cmResult) => { 277 if (err != null) { 278 console.error(`Failed to install private certificate. Code: ${err.code}, message: ${err.message}`); 279 } else { 280 let uri: string = (cmResult?.uri == undefined) ? '' : cmResult.uri; 281 console.info('Succeeded in installing private certificate.'); 282 } 283 }); 284} catch (error) { 285 console.error(`Failed to install private certificate. Code: ${error.code}, message: ${error.message}`); 286} 287``` 288 289## certificateManager.installPrivateCertificate 290 291installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string): Promise\<CMResult> 292 293表示安装私有凭据,使用Promise方式异步返回结果。 294 295**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 296 297**系统能力:** SystemCapability.Security.CertificateManager 298 299**参数**: 300 301| 参数名 | 类型 | 必填 | 说明 | 302| -------- | ------------------------------------------------- | ---- | -------------------------- | 303| keystore | Uint8Array | 是 | 表示带有密钥对和证书的密钥库文件,最大长度为20480字节。 | 304| keystorePwd | string | 是 | 表示密钥库文件的密码,长度限制32字节以内。 | 305| certAlias | string | 是 | 表示用户输入的凭据别名,当前仅支持传入数字、字母或下划线,长度建议32字节以内。 | 306 307**返回值**: 308 309| 类型 | 说明 | 310| ------------------------------- | ------------------------------------------------------------ | 311| Promise\<[CMResult](#cmresult)> | Promise对象。表示安装私有凭据的结果,返回值为[CMResult](#cmresult)对象中的uri属性。 | 312 313**错误码:** 314 315以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 316 317| 错误码ID | 错误信息 | 318| -------- | ------------------------------------------------------------ | 319| 201 | Permission verification failed. The application does not have the permission required to call the API. | 320| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 321| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 322| 17500003 | The keystore is in an invalid format or the keystore password is incorrect. | 323| 17500004 | The number of certificates or credentials reaches the maximum allowed. | 324 325**示例**: 326 327```ts 328import { certificateManager } from '@kit.DeviceCertificateKit'; 329import { BusinessError } from '@kit.BasicServicesKit'; 330 331/* 安装的凭据数据需要业务赋值,本例数据非凭据数据 */ 332let keystore: Uint8Array = new Uint8Array([ 333 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 334]); 335let keystorePwd: string = "123456"; 336try { 337 certificateManager.installPrivateCertificate(keystore, keystorePwd, 'test').then((cmResult) => { 338 let uri: string = (cmResult?.uri == undefined) ? '' : cmResult.uri; 339 console.info('Succeeded in installing private certificate.'); 340 }).catch((err: BusinessError) => { 341 console.error(`Failed to install private certificate. Code: ${err.code}, message: ${err.message}`); 342 }) 343} catch (error) { 344 console.error(`Failed to install private certificate. Code: ${error.code}, message: ${error.message}`); 345} 346``` 347 348## certificateManager.installPrivateCertificate<sup>18+</sup> 349 350installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string, level: AuthStorageLevel): Promise\<CMResult> 351 352表示安装私有凭据并指定凭据的存储级别。使用Promise异步回调。 353 354**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 355 356**系统能力:** SystemCapability.Security.CertificateManager 357 358**参数:** 359 360| 参数名 | 类型 | 必填 | 说明 | 361| ----------- | ---------- | ---- | ------------------------------------------------------------ | 362| keystore | Uint8Array | 是 | 表示带有密钥对和证书的密钥库文件,最大长度为20480字节。 | 363| keystorePwd | string | 是 | 表示密钥库文件的密码。<br>长度限制:32字节以内。 | 364| certAlias | string | 是 | 表示用户输入的凭据别名,当前仅支持传入数字、字母或下划线。<br>长度建议:32字节以内。 | 365| level | [AuthStorageLevel](#authstoragelevel18) | 是 | 表示凭据的存储级别。 | 366 367**返回值:** 368 369| 类型 | 说明 | 370| ------------------------------- | ------------------------------------------------------------ | 371| Promise\<[CMResult](#cmresult)> | Promise对象。表示安装私有凭据的结果,返回值为[CMResult](#cmresult)对象中的uri属性。 | 372 373**错误码:** 374 375以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 376 377| 错误码ID | 错误信息 | 378| ---------------------- | ------------------------------------------------------------ | 379| 201 | Permission verification failed. The application does not have the permission required to call the API. | 380| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 381| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 382| 17500003 | The keystore is in an invalid format or the keystore password is incorrect. | 383| 17500004 | The number of certificates or credentials reaches the maximum allowed. | 384 385**示例:** 386 387```ts 388import { certificateManager } from '@kit.DeviceCertificateKit'; 389import { BusinessError } from '@kit.BasicServicesKit'; 390 391/* 安装的凭据数据需要业务赋值,本例数据非凭据数据。 */ 392let keystore: Uint8Array = new Uint8Array([ 393 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 394]); 395let keystorePwd: string = "123456"; 396try { 397 /* 安装凭据在首次解锁设备后可以使用。 */ 398 let level = certificateManager.AuthStorageLevel.EL2; 399 certificateManager.installPrivateCertificate(keystore, keystorePwd, 'test', level).then((cmResult) => { 400 let uri: string = (cmResult?.uri == undefined) ? '' : cmResult.uri; 401 console.info('Succeeded in installing private certificate.'); 402 }).catch((err: BusinessError) => { 403 console.error(`Failed to install private certificate. Code: ${err.code}, message: ${err.message}`); 404 }) 405} catch (error) { 406 console.error(`Failed to install private certificate. Code: ${error.code}, message: ${error.message}`); 407} 408``` 409 410## certificateManager.getPrivateCertificate 411 412getPrivateCertificate(keyUri: string, callback: AsyncCallback\<CMResult>): void 413 414表示获取私有凭据的详细信息,使用Callback回调异步返回结果。 415 416**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 417 418**系统能力:** SystemCapability.Security.CertificateManager 419 420**参数**: 421 422| 参数名 | 类型 | 必填 | 说明 | 423| -------- | ------------------------------------------------- | ---- | -------------------------- | 424| keyUri | string | 是 | 表示待获取凭据的唯一标识符,长度限制256字节以内。 | 425| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当获取私有凭据的详细信息成功时,err为null,data为[CMResult](#cmresult)对象中的credential属性;否则为错误对象。 | 426 427**错误码:** 428 429以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 430 431| 错误码ID | 错误信息 | 432| -------- | ------------- | 433| 201 | Permission verification failed. The application does not have the permission required to call the API. | 434| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 435| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 436| 17500002 | The certificate does not exist. | 437 438**示例**: 439```ts 440import { certificateManager } from '@kit.DeviceCertificateKit'; 441 442let uri: string = 'test'; /* 业务获取私有凭据详情,需要使用凭据的唯一标识符,此处省略 */ 443try { 444 certificateManager.getPrivateCertificate(uri, (err, cmResult) => { 445 if (err != null) { 446 console.error(`Failed to get private certificate. Code: ${err.code}, message: ${err.message}`); 447 } else { 448 if (cmResult?.credential == undefined) { 449 console.info('The result of getting private certificate is undefined.'); 450 } else { 451 let list = cmResult.credential; 452 console.info('Succeeded in getting private certificate.'); 453 } 454 } 455 }); 456} catch (error) { 457 console.error(`Failed to get private certificate. Code: ${error.code}, message: ${error.message}`); 458} 459``` 460 461## certificateManager.getPrivateCertificate 462 463getPrivateCertificate(keyUri: string): Promise\<CMResult> 464 465表示获取私有凭据详情,使用Promise方式异步返回结果。 466 467**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 468 469**系统能力:** SystemCapability.Security.CertificateManager 470 471**参数**: 472 473| 参数名 | 类型 | 必填 | 说明 | 474| -------- | ------------------------------------------------- | ---- | -------------------------- | 475| keyUri | string | 是 | 表示待获取凭据的唯一标识符,长度限制256字节以内。 | 476 477**返回值**: 478 479| 类型 | 说明 | 480| ------------------------------- | ------------------------------------------------------------ | 481| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取私有凭据详细信息的结果,返回值为[CMResult](#cmresult)对象中的credential属性。 | 482 483**错误码:** 484 485以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 486 487| 错误码ID | 错误信息 | 488| -------- | ------------- | 489| 201 | Permission verification failed. The application does not have the permission required to call the API. | 490| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 491| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 492| 17500002 | The certificate does not exist. | 493 494**示例**: 495```ts 496import { certificateManager } from '@kit.DeviceCertificateKit'; 497import { BusinessError } from '@kit.BasicServicesKit'; 498 499let uri: string = 'test'; /* 业务获取私有凭据详情,需要使用凭据的唯一标识符,此处省略 */ 500try { 501 certificateManager.getPrivateCertificate(uri).then((cmResult) => { 502 if (cmResult?.credential == undefined) { 503 console.info('The result of getting private certificate is undefined.'); 504 } else { 505 let list = cmResult.credential; 506 console.info('Succeeded in getting private certificate.'); 507 } 508 }).catch((err: BusinessError) => { 509 console.error(`Failed to get private certificate. Code: ${err.code}, message: ${err.message}`); 510 }) 511} catch (error) { 512 console.error(`Failed to get private certificate. Code: ${error.code}, message: ${error.message}`); 513} 514``` 515 516## certificateManager.uninstallPrivateCertificate 517 518uninstallPrivateCertificate(keyUri: string, callback: AsyncCallback\<void>): void 519 520表示卸载指定的私有凭据,使用Callback回调异步返回结果。 521 522**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 523 524**系统能力:** SystemCapability.Security.CertificateManager 525 526**参数**: 527 528| 参数名 | 类型 | 必填 | 说明 | 529| -------- | ------------------------------------------------- | ---- | -------------------------- | 530| keyUri | string | 是 | 表示待卸载凭据的唯一标识符,长度限制256字节以内。 | 531| callback | AsyncCallback\<void> | 是 | 回调函数。当卸载私有凭据成功时,err为null,否则为错误对象。 | 532 533**错误码:** 534 535以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 536 537| 错误码ID | 错误信息 | 538| -------- | ------------- | 539| 201 | Permission verification failed. The application does not have the permission required to call the API. | 540| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 541| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 542| 17500002 | The certificate does not exist. | 543 544**示例**: 545```ts 546import { certificateManager } from '@kit.DeviceCertificateKit'; 547 548let uri: string = 'test'; /* 业务删除私有凭据,需要使用凭据的唯一标识符,此处省略 */ 549try { 550 certificateManager.uninstallPrivateCertificate(uri, (err, result) => { 551 if (err != null) { 552 console.error(`Failed to uninstall private certificate. Code: ${err.code}, message: ${err.message}`); 553 } else { 554 console.info('Succeeded in uninstalling private certificate.'); 555 } 556 }); 557} catch (error) { 558 console.error(`Failed to uninstall private certificate. Code: ${error.code}, message: ${error.message}`); 559} 560``` 561 562## certificateManager.uninstallPrivateCertificate 563 564uninstallPrivateCertificate(keyUri: string): Promise\<void> 565 566表示卸载指定的私有凭据,使用Promise方式异步返回结果。 567 568**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 569 570**系统能力:** SystemCapability.Security.CertificateManager 571 572**参数**: 573 574| 参数名 | 类型 | 必填 | 说明 | 575| -------- | ------------------------------------------------- | ---- | -------------------------- | 576| keyUri | string | 是 | 表示待卸载凭据的唯一标识符,长度限制256字节以内。 | 577 578**返回值**: 579 580| 类型 | 说明 | 581| ------------------------------------------- | -------------------- | 582| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 583 584**错误码:** 585 586以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 587 588| 错误码ID | 错误信息 | 589| -------- | ------------- | 590| 201 | Permission verification failed. The application does not have the permission required to call the API. | 591| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 592| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 593| 17500002 | The certificate does not exist. | 594 595**示例**: 596```ts 597import { certificateManager } from '@kit.DeviceCertificateKit'; 598import { BusinessError } from '@kit.BasicServicesKit'; 599 600let uri: string = 'test'; /* 业务删除私有凭据,需要使用凭据的唯一标识符,此处省略 */ 601try { 602 certificateManager.uninstallPrivateCertificate(uri).then((cmResult) => { 603 console.info('Succeeded in uninstalling private certificate.'); 604 }).catch((err: BusinessError) => { 605 console.error(`Failed to uninstall private certificate. Code: ${err.code}, message: ${err.message}`); 606 }) 607} catch (error) { 608 console.error(`Failed to uninstall private certificate. Code: ${error.code}, message: ${error.message}`); 609} 610``` 611 612## certificateManager.installUserTrustedCertificateSync<sup>18+</sup> 613 614installUserTrustedCertificateSync(cert: Uint8Array, certScope: CertScope) : CMResult 615 616表示安装用户CA证书。 617 618**需要权限:** ohos.permission.ACCESS_ENTERPRISE_USER_TRUSTED_CERT<!--Del-->或ohos.permission.ACCESS_USER_TRUSTED_CERT<!--DelEnd--> 619 620**系统能力:** SystemCapability.Security.CertificateManager 621 622**参数**: 623 624| 参数名 | 类型 | 必填 | 说明 | 625|-----------|----------------------------|----|--------------| 626| cert | Uint8Array | 是 | 表示CA证书数据,最大长度为8196字节。 | 627| certScope | [CertScope](#certscope18) | 是 | 表示CA证书安装的位置。 | 628 629**返回值**: 630 631| 类型 | 说明 | 632|-----------------------|-----------------------------------| 633| [CMResult](#cmresult) | 表示CA证书的安装结果,返回值CMResult对象中的uri属性。 | 634 635**错误码:** 636 637以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 638 639| 错误码ID | 错误信息 | 640|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------| 641| 201 | Permission verification failed. The application does not have the permission required to call the API. | 642| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 643| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 644| 17500003 | Indicates that the certificate is in an invalid format. | 645| 17500004 | Indicates that the number of certificates reaches the maximum allowed. | 646| 17500007 | Indicates that the device enters advanced security mode. In this mode, the user CA certificate cannot be installed. | 647 648**示例**: 649 650```ts 651import {certificateManager} from '@kit.DeviceCertificateKit'; 652 653/* 安装的CA证书数据需要业务赋值,本例数据非CA证书数据 */ 654let certData: Uint8Array = new Uint8Array([ 655 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 656]); 657try { 658 let result: certificateManager.CMResult = certificateManager.installUserTrustedCertificateSync(certData, certificateManager.CertScope.CURRENT_USER); 659 let certUri = result.uri; 660 if (certUri === undefined) { 661 console.error("The result of install user trusted certificate is undefined."); 662 } else { 663 console.info("Successed to install user trusted certificate."); 664 } 665} catch (error) { 666 console.error(`Failed to install user trusted certificate. Code: ${error.code}, message: ${error.message}`); 667} 668``` 669 670## certificateManager.uninstallUserTrustedCertificateSync<sup>18+</sup> 671 672uninstallUserTrustedCertificateSync(certUri: string) : void 673 674表示删除用户CA证书。 675 676**需要权限:** ohos.permission.ACCESS_ENTERPRISE_USER_TRUSTED_CERT<!--Del-->或ohos.permission.ACCESS_USER_TRUSTED_CERT<!--DelEnd--> 677 678**系统能力:** SystemCapability.Security.CertificateManager 679 680**参数**: 681 682| 参数名 | 类型 | 必填 | 说明 | 683|-----------|----------------------------|----|--------------| 684| certUri | string | 是 | 表示待卸删除证书的唯一标识符,长度限制256字节以内。 | 685 686**错误码:** 687 688以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 689 690| 错误码ID | 错误信息 | 691|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------| 692| 201 | Permission verification failed. The application does not have the permission required to call the API. | 693| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 694| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 695| 17500002 | Indicates that the certificate does not exist. | 696 697**示例**: 698 699```ts 700import {certificateManager} from '@kit.DeviceCertificateKit'; 701 702let certUri: string = "test"; /* 业务删除证书,需要使用证书的标识符,此处省略 */ 703try { 704 certificateManager.uninstallUserTrustedCertificateSync(certUri); 705} catch (error) { 706 console.error(`Failed to uninstall user trusted certificate. Code: ${error.code}, message: ${error.message}`); 707} 708``` 709 710## certificateManager.init 711 712init(authUri: string, spec: CMSignatureSpec, callback: AsyncCallback\<CMHandle>): void 713 714表示使用凭据进行签名、验签的初始化操作,使用Callback回调异步返回结果。 715 716**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 717 718**系统能力:** SystemCapability.Security.CertificateManager 719 720**参数**: 721 722| 参数名 | 类型 | 必填 | 说明 | 723| -------- | ------------------------------------------------- | ---- | -------------------------- | 724| authUri | string | 是 | 表示使用凭据的唯一标识符,长度限制256字节以内。 | 725| spec | [CMSignatureSpec](#cmsignaturespec) | 是 | 表示签名、验签的属性。 | 726| callback | AsyncCallback\<[CMHandle](#cmhandle)> | 是 | 回调函数。当签名、验签的初始化操作成功时,err为null,data为获取到的CMHandle;否则为错误对象。 | 727 728**错误码:** 729 730以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 731 732| 错误码ID | 错误信息 | 733| -------- | ------------- | 734| 201 | Permission verification failed. The application does not have the permission required to call the API. | 735| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 736| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 737| 17500002 | The certificate does not exist. | 738| 17500005<sup>12+</sup> | The application is not authorized by the user. | 739 740**示例**: 741```ts 742import { certificateManager } from '@kit.DeviceCertificateKit'; 743 744let uri: string = 'test'; /* 业务使用凭据进行签名、验签的初始化操作,需要使用凭据的唯一标识符,此处省略 */ 745const req: certificateManager.CMSignatureSpec = { 746 purpose: certificateManager.CmKeyPurpose.CM_KEY_PURPOSE_SIGN, 747 padding: certificateManager.CmKeyPadding.CM_PADDING_PSS, 748 digest: certificateManager.CmKeyDigest.CM_DIGEST_SHA256 749} 750try { 751 certificateManager.init(uri, req, (err, cmHandle) => { 752 if (err != null) { 753 console.error(`Failed to init. Code: ${err.code}, message: ${err.message}`); 754 } else { 755 console.info('Succeeded in initiating.'); 756 } 757 }) 758} catch (error) { 759 console.error(`Failed to init. Code: ${error.code}, message: ${error.message}`); 760} 761``` 762 763## certificateManager.init 764 765init(authUri: string, spec: CMSignatureSpec): Promise\<CMHandle> 766 767表示使用凭据进行签名、验签的初始化操作,使用Promise方式异步返回结果。 768 769**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 770 771**系统能力:** SystemCapability.Security.CertificateManager 772 773**参数**: 774 775| 参数名 | 类型 | 必填 | 说明 | 776| -------- | ------------------------------------------------- | ---- | -------------------------- | 777| authUri | string | 是 | 表示使用凭据的唯一标识符,长度限制256字节以内。 | 778| spec | [CMSignatureSpec](#cmsignaturespec) | 是 | 表示签名、验签的属性。 | 779 780**返回值**: 781 782| 类型 | 说明 | 783| ------------------------------------------- | -------------------- | 784| Promise\<[CMHandle](#cmhandle)> | Promise对象。表示签名、验签的初始化操作结果,返回CMHandle对象。 | 785 786**错误码:** 787 788以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 789 790| 错误码ID | 错误信息 | 791| -------- | ------------- | 792| 201 | Permission verification failed. The application does not have the permission required to call the API. | 793| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 794| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 795| 17500002 | The certificate does not exist. | 796| 17500005<sup>12+</sup> | The application is not authorized by the user. | 797 798**示例**: 799```ts 800import { certificateManager } from '@kit.DeviceCertificateKit'; 801import { BusinessError } from '@kit.BasicServicesKit'; 802 803let uri: string = 'test'; /* 业务使用凭据进行签名、验签的初始化操作,需要使用凭据的唯一标识符,此处省略 */ 804const req: certificateManager.CMSignatureSpec = { 805 purpose: certificateManager.CmKeyPurpose.CM_KEY_PURPOSE_VERIFY, 806 padding: certificateManager.CmKeyPadding.CM_PADDING_PSS, 807 digest: certificateManager.CmKeyDigest.CM_DIGEST_MD5 808} 809try { 810 certificateManager.init(uri, req).then((handle) => { 811 console.info('Succeeded in initiating.'); 812 }).catch((err: BusinessError) => { 813 console.error(`Failed to init. Code: ${err.code}, message: ${err.message}`); 814 }) 815} catch (error) { 816 console.error(`Failed to init. Code: ${error.code}, message: ${error.message}`); 817} 818``` 819 820## certificateManager.update 821 822update(handle: Uint8Array, data: Uint8Array, callback: AsyncCallback\<void>): void 823 824表示签名、验签的数据更新操作,使用Callback回调异步返回结果。 825 826**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 827 828**系统能力:** SystemCapability.Security.CertificateManager 829 830**参数**: 831 832| 参数名 | 类型 | 必填 | 说明 | 833| -------- | ------------------------------------------------- | ---- | -------------------------- | 834| handle | Uint8Array | 是 | 表示初始化操作返回的句柄,最大长度为8字节。 | 835| data | Uint8Array | 是 | 表示待签名、验签的数据。 | 836| callback | AsyncCallback\<void> | 是 | 回调函数。当签名、验签的数据更新操作成功时,err为null,否则为错误对象。 | 837 838**错误码:** 839 840以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 841 842| 错误码ID | 错误信息 | 843| -------- | ------------------------------------------------------------ | 844| 201 | Permission verification failed. The application does not have the permission required to call the API. | 845| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 846| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 847 848**示例**: 849```ts 850import { certificateManager } from '@kit.DeviceCertificateKit'; 851 852/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 853let cmHandle: Uint8Array = new Uint8Array([ 854 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 855]); 856let srcData: Uint8Array = new Uint8Array([ 857 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 858]); 859try { 860 certificateManager.update(cmHandle, srcData, (err, result) => { 861 if (err != null) { 862 console.error(`Failed to update. Code: ${err.code}, message: ${err.message}`); 863 } else { 864 console.info('Succeeded in updating.'); 865 } 866 }); 867} catch (error) { 868 console.error(`Failed to update. Code: ${error.code}, message: ${error.message}`); 869} 870``` 871 872## certificateManager.update 873 874update(handle: Uint8Array, data: Uint8Array): Promise\<void> 875 876表示签名、验签的数据更新操作,使用Promise方式异步返回结果。 877 878**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 879 880**系统能力:** SystemCapability.Security.CertificateManager 881 882**参数**: 883 884| 参数名 | 类型 | 必填 | 说明 | 885| -------- | ------------------------------------------------- | ---- | -------------------------- | 886| handle | Uint8Array | 是 | 表示初始化操作返回的句柄,最大长度为8字节。 | 887| data | Uint8Array | 是 | 表示待签名、验签的数据。 | 888 889**返回值**: 890 891| 类型 | 说明 | 892| ------------------------------------------- | -------------------- | 893| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 894 895**错误码:** 896 897以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 898 899| 错误码ID | 错误信息 | 900| -------- | ------------------------------------------------------------ | 901| 201 | Permission verification failed. The application does not have the permission required to call the API. | 902| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 903| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 904 905**示例**: 906```ts 907import { certificateManager } from '@kit.DeviceCertificateKit'; 908import { BusinessError } from '@kit.BasicServicesKit'; 909 910/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 911let cmHandle: Uint8Array = new Uint8Array([ 912 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 913]); 914let srcData: Uint8Array = new Uint8Array([ 915 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 916]); 917try { 918 certificateManager.update(cmHandle, srcData).then((result) => { 919 console.info('Succeeded in updating.'); 920 }).catch((err: BusinessError) => { 921 console.error(`Failed to update. Code: ${err.code}, message: ${err.message}`); 922 }) 923} catch (error) { 924 console.error(`Failed to update. Code: ${error.code}, message: ${error.message}`); 925} 926``` 927 928## certificateManager.finish 929 930finish(handle: Uint8Array, callback: AsyncCallback\<CMResult>): void 931 932表示完成签名的操作,Callback回调异步返回结果。 933 934**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 935 936**系统能力:** SystemCapability.Security.CertificateManager 937 938**参数**: 939 940| 参数名 | 类型 | 必填 | 说明 | 941| -------- | ------------------------------------------------- | ---- | -------------------------- | 942| handle | Uint8Array | 是 | 表示初始化操作返回的句柄,最大长度为8字节。 | 943| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当签名成功时,err为null,data为[CMResult](#cmresult)对象中的outData属性,表示签名数据;否则为错误对象。 | 944 945**错误码:** 946 947以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 948 949| 错误码ID | 错误信息 | 950| -------- | ------------------------------------------------------------ | 951| 201 | Permission verification failed. The application does not have the permission required to call the API. | 952| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 953| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 954 955**示例**: 956```ts 957import { certificateManager } from '@kit.DeviceCertificateKit'; 958 959/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 960let cmHandle: Uint8Array = new Uint8Array([ 961 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 962]); 963try { 964 certificateManager.finish(cmHandle, (err, cmResult) => { 965 if (err != null) { 966 console.error(`Failed to finish. Code: ${err.code}, message: ${err.message}`); 967 } else { 968 if (cmResult?.outData != undefined) { 969 let signRes: Uint8Array = cmResult.outData; 970 console.info('Succeeded in finishing.'); 971 } else { 972 console.info('The result of finishing is undefined.'); 973 } 974 } 975 }); 976} catch(error) { 977 console.error(`Failed to finish. Code: ${error.code}, message: ${error.message}`); 978} 979``` 980 981## certificateManager.finish 982 983finish(handle: Uint8Array, signature: Uint8Array, callback: AsyncCallback\<CMResult>): void 984 985表示完成验签的操作,使用Callback回调异步返回结果。 986 987**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 988 989**系统能力:** SystemCapability.Security.CertificateManager 990 991**参数**: 992 993| 参数名 | 类型 | 必填 | 说明 | 994| -------- | ------------------------------------------------- | ---- | -------------------------- | 995| handle | Uint8Array | 是 | 表示初始化操作返回的句柄,最大长度为8字节。 | 996| signature | Uint8Array | 是 | 表示签名数据。 | 997| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。当验签成功时,err为null;否则为错误对象。 | 998 999**错误码:** 1000 1001以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1002 1003| 错误码ID | 错误信息 | 1004| -------- | ------------------------------------------------------------ | 1005| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1006| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1007| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1008 1009**示例**: 1010```ts 1011import { certificateManager } from '@kit.DeviceCertificateKit'; 1012 1013/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 1014let cmHandle: Uint8Array = new Uint8Array([ 1015 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 1016]); 1017let signRes: Uint8Array = new Uint8Array([ 1018 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 1019]); 1020try { 1021 certificateManager.finish(cmHandle, signRes, (err, cmResult) => { 1022 if (err != null) { 1023 console.error(`Failed to finish. Code: ${err.code}, message: ${err.message}`); 1024 } else { 1025 console.info('Succeeded in finishing.'); 1026 } 1027 }); 1028} catch(error) { 1029 console.error(`Failed to finish. Code: ${error.code}, message: ${error.message}`); 1030} 1031``` 1032 1033## certificateManager.finish 1034 1035finish(handle: Uint8Array, signature?: Uint8Array): Promise\<CMResult> 1036 1037表示完成签名、验签的操作,使用Promise方式异步返回结果。 1038 1039**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1040 1041**系统能力:** SystemCapability.Security.CertificateManager 1042 1043**参数**: 1044 1045| 参数名 | 类型 | 必填 | 说明 | 1046| -------- | ------------------------------------------------- | ---- | -------------------------- | 1047| handle | Uint8Array | 是 | 表示初始化操作返回的句柄,最大长度为8字节。 | 1048| signature | Uint8Array | 否 | 表示签名数据。 | 1049 1050**返回值**: 1051 1052| 类型 | 说明 | 1053| ------------------------------- | ------------------------------------------------------------ | 1054| Promise\<[CMResult](#cmresult)> | Promise对象。执行签名操作时,表示签名的结果,返回值为[CMResult](#cmresult)对象中的outData属性;执行验签操作时,无返回结果的Promise对象。 | 1055 1056**错误码:** 1057 1058以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1059 1060| 错误码ID | 错误信息 | 1061| -------- | ------------------------------------------------------------ | 1062| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1063| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1064| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1065 1066**示例**: 1067```ts 1068import { certificateManager } from '@kit.DeviceCertificateKit'; 1069import { BusinessError } from '@kit.BasicServicesKit'; 1070 1071/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 1072let cmHandle: Uint8Array = new Uint8Array([ 1073 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 1074]); 1075try { 1076 /* 签名的finish操作 */ 1077 certificateManager.finish(cmHandle).then((cmResult) => { 1078 if (cmResult?.outData != undefined) { 1079 let signRes1: Uint8Array = cmResult.outData; 1080 console.info('Succeeded in finishing signature.'); 1081 } else { 1082 console.info('The result of signature is undefined.'); 1083 } 1084 }).catch((err: BusinessError) => { 1085 console.error(`Failed to finish signature. Code: ${err.code}, message: ${err.message}`); 1086 }) 1087 1088 /* 签名的结果 */ 1089 let signRes: Uint8Array = new Uint8Array([ 1090 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 1091 ]); 1092 /* 验签的finish操作 */ 1093 certificateManager.finish(cmHandle, signRes).then((cmResult) => { 1094 console.info('Succeeded in finishing verification.'); 1095 }).catch((err: BusinessError) => { 1096 console.error(`Failed to finish verification. Code: ${err.code}, message: ${err.message}`); 1097 }) 1098} catch(error) { 1099 console.error(`Failed to finish. Code: ${error.code}, message: ${error.message}`); 1100} 1101``` 1102 1103## certificateManager.abort 1104 1105abort(handle: Uint8Array, callback: AsyncCallback\<void>): void 1106 1107表示中止签名、验签的操作,使用Callback回调异步返回结果。 1108 1109**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1110 1111**系统能力:** SystemCapability.Security.CertificateManager 1112 1113**参数**: 1114 1115| 参数名 | 类型 | 必填 | 说明 | 1116| -------- | ------------------------------------------------- | ---- | -------------------------- | 1117| handle | Uint8Array | 是 | 表示初始化操作返回的句柄,最大长度为8字节。 | 1118| callback | AsyncCallback\<void> | 是 | 回调函数。当中止签名、验签成功时,err为null,否则为错误对象。 | 1119 1120**错误码:** 1121 1122以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1123 1124| 错误码ID | 错误信息 | 1125| -------- | ------------------------------------------------------------ | 1126| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1127| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1128| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1129 1130**示例**: 1131```ts 1132import { certificateManager } from '@kit.DeviceCertificateKit'; 1133 1134/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 1135let cmHandle: Uint8Array = new Uint8Array([ 1136 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 1137]); 1138try { 1139 certificateManager.abort(cmHandle, (err, cmResult) => { 1140 if (err != null) { 1141 console.error(`Failed to abort. Code: ${err.code}, message: ${err.message}`); 1142 } else { 1143 console.info('Succeeded in aborting.'); 1144 } 1145 }); 1146} catch(error) { 1147 console.error(`Failed to abort. Code: ${error.code}, message: ${error.message}`); 1148} 1149``` 1150 1151## certificateManager.abort 1152 1153abort(handle: Uint8Array): Promise\<void> 1154 1155表示中止签名、验签的操作,使用Promise方式异步返回结果。 1156 1157**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1158 1159**系统能力:** SystemCapability.Security.CertificateManager 1160 1161**参数**: 1162 1163| 参数名 | 类型 | 必填 | 说明 | 1164| -------- | ------------------------------------------------- | ---- | -------------------------- | 1165| handle | Uint8Array | 是 | 表示初始化操作返回的句柄,最大长度为8字节。 | 1166 1167**返回值**: 1168 1169| 类型 | 说明 | 1170| ------------------------------------------- | -------------------- | 1171| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 1172 1173**错误码:** 1174 1175以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1176 1177| 错误码ID | 错误信息 | 1178| -------- | ------------------------------------------------------------ | 1179| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1180| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1181| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1182 1183**示例**: 1184```ts 1185import { certificateManager } from '@kit.DeviceCertificateKit'; 1186import { BusinessError } from '@kit.BasicServicesKit'; 1187 1188/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 1189let cmHandle: Uint8Array = new Uint8Array([ 1190 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 1191]); 1192try { 1193 certificateManager.abort(cmHandle).then((result) => { 1194 console.info('Succeeded in aborting.'); 1195 }).catch((err: BusinessError) => { 1196 console.error(`Failed to abort. Code: ${err.code}, message: ${err.message}`); 1197 }) 1198} catch (error) { 1199 console.error(`Failed to abort. Code: ${error.code}, message: ${error.message}`); 1200} 1201``` 1202 1203## certificateManager.getPublicCertificate<sup>12+</sup> 1204 1205getPublicCertificate(keyUri: string): Promise\<CMResult> 1206 1207表示获取用户公共凭据的详细信息,使用Promise方式异步返回结果。 1208 1209**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1210 1211**系统能力:** SystemCapability.Security.CertificateManager 1212 1213**参数**: 1214 1215| 参数名 | 类型 | 必填 | 说明 | 1216| -------- | ------------------------------------------------- | ---- | -------------------------- | 1217| keyUri | string | 是 | 表示用户公共凭据的唯一标识符,长度限制256字节以内。 | 1218 1219**返回值**: 1220 1221| 类型 | 说明 | 1222| ------------------------------- | ------------------------------------------------------------ | 1223| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取用户公共凭据详细信息的结果,返回值为[CMResult](#cmresult)对象中的credential属性。 | 1224 1225**错误码:** 1226 1227以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1228 1229| 错误码ID | 错误信息 | 1230| -------- | ------------- | 1231| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1232| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1233| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1234| 17500002 | The certificate does not exist. | 1235| 17500005 | The application is not authorized by the user. | 1236 1237**示例**: 1238```ts 1239import { certificateManager } from '@kit.DeviceCertificateKit'; 1240import { BusinessError } from '@kit.BasicServicesKit'; 1241 1242let uri: string = 'test'; /* 用户获取公共凭据详情,需要使用凭据的唯一标识符,此处省略 */ 1243try { 1244 certificateManager.getPublicCertificate(uri).then((cmResult) => { 1245 if (cmResult?.credential == undefined) { 1246 console.info('The result of getting public certificate is undefined.'); 1247 } else { 1248 let cred = cmResult.credential; 1249 console.info('Succeeded in getting Public certificate.'); 1250 } 1251 }).catch((err: BusinessError) => { 1252 console.error(`Failed to get Public certificate. Code: ${err.code}, message: ${err.message}`); 1253 }) 1254} catch (error) { 1255 console.error(`Failed to get Public certificate. Code: ${error.code}, message: ${error.message}`); 1256} 1257``` 1258 1259## certificateManager.isAuthorizedApp<sup>12+</sup> 1260 1261isAuthorizedApp(keyUri: string): Promise\<boolean> 1262 1263表示当前应用是否由指定的用户凭据授权,使用Promise方式异步返回结果。 1264 1265**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1266 1267**系统能力:** SystemCapability.Security.CertificateManager 1268 1269**参数**: 1270 1271| 参数名 | 类型 | 必填 | 说明 | 1272| -------- | ------------------------------------------------- | ---- | -------------------------- | 1273| keyUri | string | 是 | 表示用户授权给应用使用的凭据的唯一标识符,长度限制256字节以内。 | 1274 1275**返回值**: 1276 1277| 类型 | 说明 | 1278| ----------------- | ------------------------------------------------------------ | 1279| Promise\<boolean> | Promise对象。表示查询应用是否被授权的结果,true为已授权,false为未授权。 | 1280 1281**错误码:** 1282 1283以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1284 1285| 错误码ID | 错误信息 | 1286| -------- | ------------- | 1287| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1288| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1289| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1290 1291**示例**: 1292```ts 1293import { certificateManager } from '@kit.DeviceCertificateKit'; 1294import { BusinessError } from '@kit.BasicServicesKit'; 1295 1296let uri: string = 'test'; /* 用户授权给应用使用的凭据的唯一标识符,此处省略 */ 1297try { 1298 certificateManager.isAuthorizedApp(uri).then((res) => { 1299 if (res) { 1300 console.info('The application is authorized by the user.'); 1301 } else { 1302 console.info('The application is not authorized by the user.'); 1303 } 1304 }).catch((err: BusinessError) => { 1305 console.error(`Failed to check if the application is authorized. Code: ${err.code}, message: ${err.message}`); 1306 }) 1307} catch (error) { 1308 console.error(`Failed to check if the application is authorized. Code: ${error.code}, message: ${error.message}`); 1309} 1310``` 1311 1312## certificateManager.getAllUserTrustedCertificates<sup>12+</sup> 1313 1314getAllUserTrustedCertificates(): Promise\<CMResult> 1315 1316表示获取当前用户和设备公共位置的所有用户根CA证书列表,使用Promise方式异步返回结果。 1317 1318**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1319 1320**系统能力:** SystemCapability.Security.CertificateManager 1321 1322**返回值**: 1323 1324| 类型 | 说明 | 1325| ------------------------------- | ------------------------------------------------------------ | 1326| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取用户根CA证书列表的结果,返回值[CMResult](#cmresult)对象中的certList属性。 | 1327 1328**错误码:** 1329 1330以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1331 1332| 错误码ID | 错误信息 | 1333| -------- | ------------- | 1334| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1335| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1336 1337**示例**: 1338```ts 1339import { certificateManager } from '@kit.DeviceCertificateKit'; 1340import { BusinessError } from '@kit.BasicServicesKit'; 1341 1342try { 1343 certificateManager.getAllUserTrustedCertificates().then((cmResult) => { 1344 if (cmResult === undefined) { // 用户根CA证书个数为0时,返回cmResult为undefined。 1345 console.info('the count of the user trusted certificates is 0'); 1346 } else if (cmResult.certList == undefined) { 1347 console.info('The result of getting all user trusted certificates is undefined.'); 1348 } else { 1349 let list = cmResult.certList; 1350 console.info('Succeeded in getting all user trusted certificates.'); 1351 } 1352 }).catch((err: BusinessError) => { 1353 console.error(`Failed to get all user trusted certificates. Code: ${err.code}, message: ${err.message}`); 1354 }) 1355} catch (error) { 1356 console.error(`Failed to get all user trusted certificates. Code: ${error.code}, message: ${error.message}`); 1357} 1358``` 1359 1360## certificateManager.getAllUserTrustedCertificates<sup>18+</sup> 1361 1362getAllUserTrustedCertificates(scope: CertScope): Promise\<CMResult> 1363 1364表示根据证书的位置获取用户根CA证书列表,使用Promise方式异步返回结果。 1365 1366**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1367 1368**系统能力:** SystemCapability.Security.CertificateManager 1369 1370**参数**: 1371 1372| 参数名 | 类型 | 必填 | 说明 | 1373| ------ | ------------------------- | ---- | ---------------- | 1374| scope | [CertScope](#certscope18) | 是 | 表示证书的位置。 | 1375 1376**返回值**: 1377 1378| 类型 | 说明 | 1379| ------------------------------- | ------------------------------------------------------------ | 1380| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取用户根CA证书列表的结果,返回值[CMResult](#cmresult)对象中的certList属性。 | 1381 1382**错误码:** 1383 1384以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1385 1386| 错误码ID | 错误信息 | 1387| -------- | ------------------------------------------------------------ | 1388| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1389| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1390| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1391 1392**示例**: 1393 1394```ts 1395import { certificateManager } from '@kit.DeviceCertificateKit'; 1396import { BusinessError } from '@kit.BasicServicesKit'; 1397 1398try { 1399 /* 获取当前用户下的用户根CA证书列表; 如果需要获取设备公共位置的用户根CA列表,则传入GLOBAL_USER */ 1400 let scope: certificateManager.CertScope = certificateManager.CertScope.CURRENT_USER; 1401 certificateManager.getAllUserTrustedCertificates(scope).then((cmResult) => { 1402 if (cmResult === undefined) { // 用户根CA证书个数为0时,返回cmResult为undefined。 1403 console.info('the count of the user trusted certificates is 0'); 1404 } else if (cmResult.certList == undefined) { 1405 console.info('The result of getting current user trusted certificates is undefined.'); 1406 } else { 1407 let list = cmResult.certList; 1408 console.info('Succeeded in getting current user trusted certificates.'); 1409 } 1410 }).catch((err: BusinessError) => { 1411 console.error(`Failed to get current user trusted certificates. Code: ${err.code}, message: ${err.message}`); 1412 }) 1413} catch (error) { 1414 console.error(`Failed to get current user trusted certificates. Code: ${error.code}, message: ${error.message}`); 1415} 1416``` 1417 1418## certificateManager.getUserTrustedCertificate<sup>12+</sup> 1419 1420getUserTrustedCertificate(certUri: string): Promise\<CMResult> 1421 1422表示获取用户根CA证书的详细信息,使用Promise方式异步返回结果。 1423 1424**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1425 1426**系统能力:** SystemCapability.Security.CertificateManager 1427 1428**参数**: 1429 1430| 参数名 | 类型 | 必填 | 说明 | 1431| -------- | ------------------------------------------------- | ---- | -------------------------- | 1432| certUri | string | 是 | 表示用户根CA证书的唯一标识符,长度限制256字节以内。 | 1433 1434**返回值**: 1435 1436| 类型 | 说明 | 1437| ------------------------------- | ------------------------------------------------------------ | 1438| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取用户根CA证书详细信息的结果,返回值为[CMResult](#cmresult)对象中的certInfo属性。 | 1439 1440**错误码:** 1441 1442以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1443 1444| 错误码ID | 错误信息 | 1445| -------- | ------------- | 1446| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1447| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1448| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1449| 17500002 | The certificate does not exist. | 1450 1451**示例**: 1452```ts 1453import { certificateManager } from '@kit.DeviceCertificateKit'; 1454import { BusinessError } from '@kit.BasicServicesKit'; 1455 1456let certUri: string = 'testUserCert'; /* 用户获取用户根CA证书详情,需要使用CA证书的唯一标识符,此处省略 */ 1457try { 1458 certificateManager.getUserTrustedCertificate(certUri).then((cmResult) => { 1459 if (cmResult?.certInfo == undefined) { 1460 console.info('The result of getting user trusted certificate is undefined.'); 1461 } else { 1462 let cert = cmResult.certInfo; 1463 console.info('Succeeded in getting user trusted certificate.'); 1464 } 1465 }).catch((err: BusinessError) => { 1466 console.error(`Failed to get user trusted certificate. Code: ${err.code}, message: ${err.message}`); 1467 }) 1468} catch (error) { 1469 console.error(`Failed to get user trusted certificate. Code: ${error.code}, message: ${error.message}`); 1470} 1471``` 1472## certificateManager.getPrivateCertificates<sup>13+</sup> 1473 1474getPrivateCertificates(): Promise\<CMResult> 1475 1476表示获取应用安装的凭据列表,使用Promise方式异步返回结果。 1477 1478**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 1479 1480**系统能力:** SystemCapability.Security.CertificateManager 1481 1482**返回值**: 1483 1484| 类型 | 说明 | 1485| ------------------------------- | ------------------------------------------------------------ | 1486| Promise\<[CMResult](#cmresult)> | Promise对象。表示获取应用安装的凭据列表的结果,返回值[CMResult](#cmresult)对象中的credentialList属性。 | 1487 1488**错误码:** 1489 1490以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1491 1492| 错误码ID | 错误信息 | 1493| -------- | ------------- | 1494| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1495| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1496 1497**示例**: 1498```ts 1499import { certificateManager } from '@kit.DeviceCertificateKit'; 1500import { BusinessError } from '@kit.BasicServicesKit'; 1501 1502try { 1503 certificateManager.getPrivateCertificates().then((cmResult) => { 1504 if (cmResult === undefined) { // 应用安装的凭据个数为0时,返回cmResult为undefined。 1505 console.info('the count of the private certificates is 0'); 1506 } else if (cmResult.credentialList == undefined) { 1507 console.info('The result of getting all private certificates installed by the application is undefined.'); 1508 } else { 1509 let list = cmResult.credentialList; 1510 console.info('Succeeded in getting all private certificates installed by the application.'); 1511 } 1512 }).catch((err: BusinessError) => { 1513 console.error(`Failed to get all private certificates installed by the application. Code: ${err.code}, message: ${err.message}`); 1514 }) 1515} catch (error) { 1516 console.error(`Failed to get all private certificates installed by the application. Code: ${error.code}, message: ${error.message}`); 1517} 1518``` 1519## certificateManager.getCertificateStorePath<sup>18+</sup> 1520 1521getCertificateStorePath(property: CertStoreProperty): string; 1522 1523表示获取证书的存储路径。 1524 1525**系统能力:** SystemCapability.Security.CertificateManager 1526 1527**参数**: 1528 1529| 参数名 | 类型 | 必填 | 说明 | 1530| -------- | ----------------------------------------- | ---- | -------------------------------- | 1531| property | [CertStoreProperty](#certstoreproperty18) | 是 | 表示获取证书存储路径的参数集合。 | 1532 1533**返回值**: 1534 1535| 类型 | 说明 | 1536| ------ | -------------------- | 1537| string | 表示证书的存储路径。 | 1538 1539**错误码:** 1540 1541以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 1542 1543| 错误码ID | 错误信息 | 1544|----------| ------------- | 1545| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1546| 17500001 | Internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. File operation error. | 1547| 17500009 | The device does not support the specified certificate store path, such as the overseas device does not support the certificate which algorithm is SM. | 1548 1549**示例**: 1550```ts 1551import { certificateManager } from '@kit.DeviceCertificateKit'; 1552 1553try { 1554 /* 获取系统CA的存储位置 */ 1555 let property1: certificateManager.CertStoreProperty = { 1556 certType: certificateManager.CertType.CA_CERT_SYSTEM, 1557 } 1558 let systemCAPath = certificateManager.getCertificateStorePath(property1); 1559 console.info(`Success to get system ca path: ${systemCAPath}`); 1560 1561 /* 获取当前用户的用户CA存储位置 */ 1562 let property2: certificateManager.CertStoreProperty = { 1563 certType: certificateManager.CertType.CA_CERT_USER, 1564 certScope: certificateManager.CertScope.CURRENT_USER, 1565 } 1566 let userCACurrentPath = certificateManager.getCertificateStorePath(property2); 1567 console.info(`Success to get current user's user ca path: ${userCACurrentPath}`); 1568 1569 /* 获取设备公共的用户CA存储位置 */ 1570 let property3: certificateManager.CertStoreProperty = { 1571 certType: certificateManager.CertType.CA_CERT_USER, 1572 certScope: certificateManager.CertScope.GLOBAL_USER, 1573 } 1574 let globalCACurrentPath = certificateManager.getCertificateStorePath(property3); 1575 console.info(`Success to get global user's user ca path: ${globalCACurrentPath}`); 1576 1577 /* 获取SM算法系统CA的存储位置 */ 1578 let property4: certificateManager.CertStoreProperty = { 1579 certType: certificateManager.CertType.CA_CERT_SYSTEM, 1580 certAlg: certificateManager.CertAlgorithm.SM, 1581 } 1582 let smSystemCAPath = certificateManager.getCertificateStorePath(property4); 1583 console.info(`Success to get SM system ca path: ${smSystemCAPath}`); 1584} catch (error) { 1585 console.error(`Failed to get store path. Code: ${error.code}, message: ${error.message}`); 1586} 1587```