1# @ohos.security.certManager (证书管理模块) 2 3证书管理主要提供系统级的证书管理能力,实现证书全生命周期(安装,存储,使用,销毁)的管理和安全使用 。 4 5> **说明:** 6> 7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import certManager from '@ohos.security.certManager'; 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| issuerName | string | 是 | 是 | 表示证书的颁发者名称。 | 98| subjectName | string | 是 | 是 | 表示证书的使用者名称。 | 99 100## Credential 101 102表示凭据详细信息。 103 104**系统能力:** SystemCapability.Security.CertificateManager 105 106| 名称 | 类型 | 可读 | 可写 | 说明 | 107| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 108| type | string | 是 | 是 | 表示凭据的类型。 | 109| alias | string | 是 | 是 | 表示凭据的别名。 | 110| keyUri | string | 是 | 是 | 表示凭据的唯一标识符。 | 111| certNum | number | 是 | 是 | 表示凭据中包含的证书个数。 | 112| keyNum | number | 是 | 是 | 表示凭据中包含的密钥个数。 | 113| credentialData | Uint8Array | 是 | 是 | 表示凭据二进制数据。 | 114 115## CredentialAbstract 116 117表示凭据的简要信息。 118 119**系统能力:** SystemCapability.Security.CertificateManager 120 121| 名称 | 类型 | 可读 | 可写 | 说明 | 122| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 123| type | string | 是 | 是 | 表示凭据的类型。 | 124| alias | string | 是 | 是 | 表示凭据的别名。 | 125| keyUri | string | 是 | 是 | 表示凭据的唯一标识符。 | 126 127## CMResult 128 129表示接口的返回结果。 130 131**系统能力:** SystemCapability.Security.CertificateManager 132 133| 名称 | 类型 | 可读 | 可写 | 说明 | 134| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 135| certList? | Array<[CertAbstract](#certabstract)> | 是 | 是 | 表示证书简要信息的列表。 | 136| certInfo? | [CertInfo](#certinfo) | 是 | 是 | 表示证书详情。 | 137| credentialList? | Array<[CredentialAbstract](#credentialabstract)> | 是 | 是 | 表示凭据简要信息的列表。 | 138| credential? | [Credential](#credential) | 是 | 是 | 表示凭据详情。 | 139| appUidList? | Array<string> | 是 | 是 | 表示授权应用列表。 | 140| uri? | string | 是 | 是 | 表示证书或凭据的唯一标识符。 | 141| outData? | Uint8Array | 是 | 是 | 表示签名结果。 | 142 143## CMHandle 144 145表示签名、验签的初始化操作句柄。 146 147**系统能力:** SystemCapability.Security.CertificateManager 148 149| 名称 | 类型 | 可读 | 可写 | 说明 | 150| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 151| handle | Uint8Array | 是 | 是 | 表示签名、验签的初始化操作句柄。 | 152 153## CMErrorCode 154 155表示调用证书管理相关API的错误码。 156 157**系统能力:** SystemCapability.Security.CertificateManager 158 159| 名称 | 值 | 说明 | 160| ---------- | ------ | --------- | 161| CM_ERROR_NO_PERMISSION | 201 | 表示应用程序无权限调用接口。 | 162| CM_ERROR_INVALID_PARAMS | 401 | 表示输入参数无效。 | 163| CM_ERROR_GENERIC | 17500001 | 表示调用接口时发生内部错误。 | 164| CM_ERROR_NO_FOUND | 17500002 | 表示证书或凭据不存在。 | 165| CM_ERROR_INCORRECT_FORMAT | 17500003 | 表示输入证书或凭据的数据格式无效。 | 166 167## certManager.installPrivateCertificate 168 169installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string, callback: AsyncCallback\<CMResult>) : void 170 171表示安装私有凭据,使用Callback回调异步返回结果。 172 173**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 174 175**系统能力:** SystemCapability.Security.CertificateManager 176 177**参数**: 178 179| 参数名 | 类型 | 必填 | 说明 | 180| -------- | ------------------------------------------------- | ---- | -------------------------- | 181| keystore | Uint8Array | 是 | 表示带有密钥对和证书的密钥库文件。 | 182| keystorePwd | string | 是 | 表示密钥库文件的密码,长度限制32字节以内。 | 183| certAlias | string | 是 | 表示用户输入的凭据别名,当前仅支持传入数字、字母或下划线,长度建议32字节以内。 | 184| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。表示安装凭据的结果,返回值为[CMResult](#cmresult)中的uri。 | 185 186**错误码:** 187 188以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 189 190| 错误码ID | 错误信息 | 191| -------- | ------------- | 192| 17500001 | There is an generic error occurred when calling the API. | 193| 17500003 | The keystore is not valid format or keystorePwd is not correct. | 194 195**示例**: 196```ts 197import certManager from '@ohos.security.certManager'; 198 199/* 安装的凭据数据需要业务赋值,本例数据非凭据数据 */ 200let keystore: Uint8Array = new Uint8Array([ 201 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 202]); 203let keystorePwd: string = "123456"; 204try { 205 certManager.installPrivateCertificate(keystore, keystorePwd, "test", (err, cmResult) => { 206 if (err != null) { 207 console.error("[Callback]installPrivateCertificate err: " + err.code); 208 } else { 209 let uri: string = (cmResult.uri == undefined) ? '' : cmResult.uri; 210 console.log("[Callback]installPrivateCertificate success"); 211 } 212 }); 213} catch (error) { 214 console.error("[Callback]installPrivateCertificate failed"); 215} 216``` 217 218## certManager.installPrivateCertificate 219 220installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string) : Promise\<CMResult> 221 222表示安装私有证书,使用Promise方式异步返回结果。 223 224**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 225 226**系统能力:** SystemCapability.Security.CertificateManager 227 228**参数**: 229 230| 参数名 | 类型 | 必填 | 说明 | 231| -------- | ------------------------------------------------- | ---- | -------------------------- | 232| keystore | Uint8Array | 是 | 表示带有密钥对和证书的密钥库文件。 | 233| keystorePwd | string | 是 | 表示密钥库文件的密码,长度限制32字节以内。 | 234| certAlias | string | 是 | 表示用户输入的凭据别名,当前仅支持传入数字、字母或下划线,长度建议32字节以内。 | 235 236**返回值**: 237 238| 类型 | 说明 | 239| ------------------------------------------- | -------------------- | 240| Promise\<[CMResult](#cmresult)> | 回调函数。表示安装凭据的结果,返回值为[CMResult](#cmresult)中的uri。 | 241 242**错误码:** 243 244以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 245 246| 错误码ID | 错误信息 | 247| -------- | ------------- | 248| 17500001 | There is an generic error occurred when calling the API. | 249| 17500003 | The keystore is not valid format or keystorePwd is not correct. | 250 251**示例**: 252 253```ts 254import certManager from '@ohos.security.certManager'; 255import { BusinessError } from '@ohos.base'; 256 257/* 安装的凭据数据需要业务赋值,本例数据非凭据数据 */ 258let keystore: Uint8Array = new Uint8Array([ 259 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 260]); 261let keystorePwd: string = "123456"; 262try { 263 certManager.installPrivateCertificate(keystore, keystorePwd, 'test').then((cmResult) => { 264 let uri: string = (cmResult.uri == undefined) ? '' : cmResult.uri; 265 console.log("[Promise]installPrivateCertificate success"); 266 }).catch((err: BusinessError) => { 267 console.error('[Promise]installPrivateCertificate failed'); 268 }) 269} catch (err) { 270 console.error("[Promise]installPrivateCertificate failed"); 271} 272``` 273 274## certManager.getPrivateCertificate 275 276getPrivateCertificate(keyUri: string, callback: AsyncCallback\<CMResult>) : void 277 278表示获取私有凭据详情,使用Callback回调异步返回结果。 279 280**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 281 282**系统能力:** SystemCapability.Security.CertificateManager 283 284**参数**: 285 286| 参数名 | 类型 | 必填 | 说明 | 287| -------- | ------------------------------------------------- | ---- | -------------------------- | 288| keyUri | string | 是 | 表示待获取凭据的唯一标识符。 | 289| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。表示获取私有凭据详情的结果,返回值为[CMResult](#cmresult)中的credential。 | 290 291**错误码:** 292 293以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 294 295| 错误码ID | 错误信息 | 296| -------- | ------------- | 297| 17500001 | There is an generic error occurred when calling the API. | 298| 17500002 | The certificate do not exist. | 299 300**示例**: 301```ts 302import certManager from '@ohos.security.certManager'; 303 304let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 305try { 306 certManager.getPrivateCertificate(uri, (err, cmResult) => { 307 if (err != null) { 308 console.error("getPrivateCertificate error"); 309 } else { 310 if (cmResult.credential == undefined) { 311 console.log("[Callback]getPrivateCertificate result is undefined"); 312 } else { 313 let list = cmResult.credential; 314 console.log("[Callback]getPrivateCertificate success"); 315 } 316 } 317 }); 318} catch (error) { 319 console.error("[Callback]installPrivateCertificate failed"); 320} 321``` 322 323## certManager.getPrivateCertificate 324 325getPrivateCertificate(keyUri: string) : Promise\<CMResult> 326 327表示获取私有凭据详情,使用Promise方式异步返回结果。 328 329**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 330 331**系统能力:** SystemCapability.Security.CertificateManager 332 333**参数**: 334 335| 参数名 | 类型 | 必填 | 说明 | 336| -------- | ------------------------------------------------- | ---- | -------------------------- | 337| keyUri | string | 是 | 表示待获取凭据的唯一标识符。 | 338 339**返回值**: 340 341| 类型 | 说明 | 342| ------------------------------------------- | -------------------- | 343| Promise\<[CMResult](#cmresult)> | 回调函数。表示获取私有凭据详情的结果,返回值[CMResult](#cmresult)中的credential。 | 344 345**错误码:** 346 347以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 348 349| 错误码ID | 错误信息 | 350| -------- | ------------- | 351| 17500001 | There is an generic error occurred when calling the API. | 352| 17500002 | The certificate do not exist. | 353 354**示例**: 355```ts 356import certManager from '@ohos.security.certManager'; 357import { BusinessError } from '@ohos.base'; 358 359let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 360try { 361 certManager.getPrivateCertificate(uri).then((cmResult) => { 362 if (cmResult.credential == undefined) { 363 console.log("[Promise]getPrivateCertificate result is undefined"); 364 } else { 365 let list = cmResult.credential; 366 console.log("[Promise]getPrivateCertificate success"); 367 } 368 }).catch((err: BusinessError) => { 369 console.error('[Promise]getPrivateCertificate failed'); 370 }) 371} catch (err) { 372 console.error("[Promise]getPrivateCertificate failed"); 373} 374``` 375 376## certManager.uninstallPrivateCertificate 377 378uninstallPrivateCertificate(keyUri: string, callback: AsyncCallback\<void>) : void 379 380表示卸载指定的私有凭据,使用Callback回调异步返回结果。 381 382**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 383 384**系统能力:** SystemCapability.Security.CertificateManager 385 386**参数**: 387 388| 参数名 | 类型 | 必填 | 说明 | 389| -------- | ------------------------------------------------- | ---- | -------------------------- | 390| keyUri | string | 是 | 表示待卸载凭据的唯一标识符。 | 391| callback | AsyncCallback\<void> | 是 | 回调函数。使用AsyncCallback的第一个error参数判断是否执行成功,error为null表示成功,不为null表示失败。 | 392 393**错误码:** 394 395以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 396 397| 错误码ID | 错误信息 | 398| -------- | ------------- | 399| 17500001 | There is an generic error occurred when calling the API. | 400| 17500002 | The certificate do not exist. | 401 402**示例**: 403```ts 404import certManager from '@ohos.security.certManager'; 405 406let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 407try { 408 certManager.uninstallPrivateCertificate(uri, (err, result) => { 409 if (err != null) { 410 console.error("[Callback]uninstallPrivateCertificate error"); 411 } else { 412 console.log("[Callback]uninstallPrivateCertificate success"); 413 } 414 }); 415} catch (err) { 416 console.error("[Callback]uninstallPrivateCertificate failed"); 417} 418``` 419 420## certManager.uninstallPrivateCertificate 421 422uninstallPrivateCertificate(keyUri: string) : Promise\<void> 423 424表示卸载指定的私有凭据,使用Promise方式异步返回结果。 425 426**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 427 428**系统能力:** SystemCapability.Security.CertificateManager 429 430**参数**: 431 432| 参数名 | 类型 | 必填 | 说明 | 433| -------- | ------------------------------------------------- | ---- | -------------------------- | 434| keyUri | string | 是 | 表示待卸载凭据的唯一标识符。 | 435 436**返回值**: 437 438| 类型 | 说明 | 439| ------------------------------------------- | -------------------- | 440| Promise\<void> | Promise对象。 | 441 442**错误码:** 443 444以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 445 446| 错误码ID | 错误信息 | 447| -------- | ------------- | 448| 17500001 | There is an generic error occurred when calling the API. | 449| 17500002 | The certificate do not exist. | 450 451**示例**: 452```ts 453import certManager from '@ohos.security.certManager'; 454import { BusinessError } from '@ohos.base'; 455 456let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 457try { 458 certManager.uninstallPrivateCertificate(uri).then((cmResult) => { 459 console.log("[Promise]uninstallPrivateCertificate success"); 460 }).catch((err: BusinessError) => { 461 console.error('[Promise]uninstallPrivateCertificate failed, code =', err.code); 462 }) 463} catch (err) { 464 console.error("[Promise]uninstallPrivateCertificate failed"); 465} 466``` 467 468## certManager.init 469 470init(authUri: string, spec: CMSignatureSpec, callback: AsyncCallback\<CMHandle>) : void 471 472表示使用凭据进行签名、验签的初始化操作,使用Callback回调异步返回结果。 473 474**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 475 476**系统能力:** SystemCapability.Security.CertificateManager 477 478**参数**: 479 480| 参数名 | 类型 | 必填 | 说明 | 481| -------- | ------------------------------------------------- | ---- | -------------------------- | 482| authUri | string | 是 | 表示使用凭据的唯一标识符。 | 483| spec | [CMSignatureSpec](#cmsignaturespec) | 是 | 表示签名、验签的属性。 | 484| callback | AsyncCallback\<[CMHandle](#cmhandle)> | 是 | 回调函数。表示初始化操作返回的句柄。 | 485 486**错误码:** 487 488以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 489 490| 错误码ID | 错误信息 | 491| -------- | ------------- | 492| 17500001 | There is an generic error occurred when calling the API. | 493| 17500002 | The certificate do not exist. | 494 495**示例**: 496```ts 497import certManager from '@ohos.security.certManager'; 498 499let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 500const req: certManager.CMSignatureSpec = { 501 purpose: certManager.CmKeyPurpose.CM_KEY_PURPOSE_SIGN, 502 padding: certManager.CmKeyPadding.CM_PADDING_PSS, 503 digest: certManager.CmKeyDigest.CM_DIGEST_SHA256 504} 505try { 506 certManager.init(uri, req, (err, cmHandle) => { 507 if (err != null) { 508 console.error("[Callback]init err"); 509 } else { 510 console.log("[Callback]init success"); 511 } 512 }) 513} catch (err) { 514 console.error("[Callback]init failed"); 515} 516``` 517 518## certManager.init 519 520init(authUri: string, spec: CMSignatureSpec) : Promise\<CMHandle> 521 522表示使用凭据进行签名、验签的初始化操作,使用Promise方式异步返回结果。 523 524**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 525 526**系统能力:** SystemCapability.Security.CertificateManager 527 528**参数**: 529 530| 参数名 | 类型 | 必填 | 说明 | 531| -------- | ------------------------------------------------- | ---- | -------------------------- | 532| authUri | string | 是 | 表示使用凭据的唯一标识符。 | 533| spec | [CMSignatureSpec](#cmsignaturespec) | 是 | 表示签名、验签的属性。 | 534 535**返回值**: 536 537| 类型 | 说明 | 538| ------------------------------------------- | -------------------- | 539| Promise\<[CMHandle](#cmhandle)> | 表示初始化操作返回的句柄。 | 540 541**错误码:** 542 543以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 544 545| 错误码ID | 错误信息 | 546| -------- | ------------- | 547| 17500001 | There is an generic error occurred when calling the API. | 548| 17500002 | The certificate do not exist. | 549 550**示例**: 551```ts 552import certManager from '@ohos.security.certManager'; 553import { BusinessError } from '@ohos.base'; 554 555let uri: string = 'test'; /* 业务安装凭据,返回唯一标识符,此处省略 */ 556const req: certManager.CMSignatureSpec = { 557 purpose: certManager.CmKeyPurpose.CM_KEY_PURPOSE_VERIFY, 558 padding: certManager.CmKeyPadding.CM_PADDING_PSS, 559 digest: certManager.CmKeyDigest.CM_DIGEST_MD5 560} 561try { 562 certManager.init(uri, req).then((handle) => { 563 console.log('[Promise]init success'); 564 }).catch((error: BusinessError) => { 565 console.error('[Promise]init failed'); 566 }) 567} catch (err) { 568 console.error("[Promise]init failed"); 569} 570``` 571 572## certManager.update 573 574update(handle: Uint8Array, data: Uint8Array, callback: AsyncCallback\<void>): void 575 576表示签名、验签时对数据的更新操作,使用Callback回调异步返回结果。 577 578**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 579 580**系统能力:** SystemCapability.Security.CertificateManager 581 582**参数**: 583 584| 参数名 | 类型 | 必填 | 说明 | 585| -------- | ------------------------------------------------- | ---- | -------------------------- | 586| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 587| data | Uint8Array | 是 | 表示待签名、验签的数据。 | 588| callback | AsyncCallback\<void> | 是 | 回调函数。使用AsyncCallback的第一个error参数判断是否执行成功,error为null表示成功,不为null表示失败。 | 589 590**错误码:** 591 592以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 593 594| 错误码ID | 错误信息 | 595| -------- | ------------- | 596| 17500001 | There is an generic error occurred when calling the API. | 597 598**示例**: 599```ts 600import certManager from '@ohos.security.certManager'; 601 602/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 603let cmHandle: Uint8Array = new Uint8Array([ 604 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 605]); 606let srcData: Uint8Array = new Uint8Array([ 607 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 608]); 609try { 610 certManager.update(cmHandle, srcData, (err, result) => { 611 if (err != null) { 612 console.error("[Callback]certManager update error"); 613 } else { 614 console.log("[Callback]certManager update success"); 615 } 616 }); 617} catch (err) { 618 console.error("[Callback]update failed"); 619} 620``` 621 622## certManager.update 623 624update(handle: Uint8Array, data: Uint8Array): Promise\<void> 625 626表示签名、验签时对数据的更新操作,使用Promise方式异步返回结果。 627 628**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 629 630**系统能力:** SystemCapability.Security.CertificateManager 631 632**参数**: 633 634| 参数名 | 类型 | 必填 | 说明 | 635| -------- | ------------------------------------------------- | ---- | -------------------------- | 636| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 637| data | Uint8Array | 是 | 表示待签名、验签的数据。 | 638 639**返回值**: 640 641| 类型 | 说明 | 642| ------------------------------------------- | -------------------- | 643| Promise\<void> | Promise对象。 | 644 645**错误码:** 646 647以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 648 649| 错误码ID | 错误信息 | 650| -------- | ------------- | 651| 17500001 | There is an generic error occurred when calling the API. | 652 653**示例**: 654```ts 655import certManager from '@ohos.security.certManager'; 656import { BusinessError } from '@ohos.base'; 657 658/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 659let cmHandle: Uint8Array = new Uint8Array([ 660 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 661]); 662let srcData: Uint8Array = new Uint8Array([ 663 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 664]); 665try { 666 certManager.update(cmHandle, srcData).then((result) => { 667 console.log('[Promise]update success'); 668 }).catch((error: BusinessError) => { 669 console.error('[Promise]update failed'); 670 }) 671} catch (err) { 672 console.error("[Promise]certManager update error"); 673} 674``` 675 676## certManager.finish 677 678finish(handle: Uint8Array, callback: AsyncCallback\<CMResult>) : void 679 680表示完成签名的操作,Callback回调异步返回结果。 681 682**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 683 684**系统能力:** SystemCapability.Security.CertificateManager 685 686**参数**: 687 688| 参数名 | 类型 | 必填 | 说明 | 689| -------- | ------------------------------------------------- | ---- | -------------------------- | 690| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 691| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。表示签名的结果,返回值为[CMResult](#cmresult)中的outData。 | 692 693**错误码:** 694 695以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 696 697| 错误码ID | 错误信息 | 698| -------- | ------------- | 699| 17500001 | There is an generic error occurred when calling the API. | 700 701**示例**: 702```ts 703import certManager from '@ohos.security.certManager'; 704 705/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 706let cmHandle: Uint8Array = new Uint8Array([ 707 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 708]); 709try { 710 certManager.finish(cmHandle, (err, cmResult) => { 711 if (err != null) { 712 console.error("[Callback]certManager sign failed"); 713 } else { 714 if (cmResult.outData != undefined) { 715 let signRes: Uint8Array = cmResult.outData; 716 console.log("[Callback]certManager sign success"); 717 } else { 718 console.error("[Callback]certManager sign failed"); 719 } 720 } 721 }); 722} catch(error) { 723 console.error("[Callback]certManager finish error"); 724} 725``` 726 727## certManager.finish 728 729finish(handle: Uint8Array, signature: Uint8Array, callback: AsyncCallback\<CMResult>) : void 730 731表示完成验签的操作,使用Callback回调异步返回结果。 732 733**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 734 735**系统能力:** SystemCapability.Security.CertificateManager 736 737**参数**: 738 739| 参数名 | 类型 | 必填 | 说明 | 740| -------- | ------------------------------------------------- | ---- | -------------------------- | 741| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 742| signature | Uint8Array | 是 | 表示签名数据。 | 743| callback | AsyncCallback\<[CMResult](#cmresult)> | 是 | 回调函数。使用AsyncCallback的第一个error参数判断是否执行成功,error为null表示成功,不为null表示失败。 | 744 745**错误码:** 746 747以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 748 749| 错误码ID | 错误信息 | 750| -------- | ------------- | 751| 17500001 | There is an generic error occurred when calling the API. | 752 753**示例**: 754```ts 755import certManager from '@ohos.security.certManager'; 756 757/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 758let cmHandle: Uint8Array = new Uint8Array([ 759 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 760]); 761let signRes: Uint8Array = new Uint8Array([ 762 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 763]); 764try { 765 certManager.finish(cmHandle, signRes, (err, cmResult) => { 766 if (err != null) { 767 console.error("[Callback]certManager verify failed"); 768 } else { 769 console.log("[Callback]certManager verify success"); 770 } 771 }); 772} catch(error) { 773 console.error("[Callback]certManager finish error"); 774} 775``` 776 777## certManager.finish 778 779finish(handle: Uint8Array, signature?: Uint8Array) : Promise\<CMResult> 780 781表示完成签名、验签的操作,使用Promise方式异步返回结果。 782 783**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 784 785**系统能力:** SystemCapability.Security.CertificateManager 786 787**参数**: 788 789| 参数名 | 类型 | 必填 | 说明 | 790| -------- | ------------------------------------------------- | ---- | -------------------------- | 791| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 792| signature | Uint8Array | 否 | 表示签名数据。 | 793 794**返回值**: 795 796| 类型 | 说明 | 797| ------------------------------- | ------------------------------------------------------------ | 798| Promise\<[CMResult](#cmresult)> | 表示签名、验证操作的结果。执行签名操作时,表示签名的结果,返回值为[CMResult](#cmresult)中的outData;执行验签操作时,无数据返回,无异常则认为验签成功。 | 799 800**错误码:** 801 802以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 803 804| 错误码ID | 错误信息 | 805| -------- | ------------- | 806| 17500001 | There is an generic error occurred when calling the API. | 807 808**示例**: 809```ts 810import certManager from '@ohos.security.certManager'; 811import { BusinessError } from '@ohos.base'; 812 813/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 814let cmHandle: Uint8Array = new Uint8Array([ 815 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 816]); 817try { 818 /* 签名的finish操作 */ 819 certManager.finish(cmHandle).then((cmResult) => { 820 if (cmResult.outData != undefined) { 821 let signRes1: Uint8Array = cmResult.outData; 822 console.log("[Promise]finish sign success"); 823 } else { 824 console.error("[Promise]finish sign failed"); 825 } 826 }).catch((err: BusinessError) => { 827 console.error('[Promise]finish sign failed'); 828 }) 829 830 /* 签名的结果 */ 831 let signRes: Uint8Array = new Uint8Array([ 832 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 833 ]); 834 /* 验签的finish操作 */ 835 certManager.finish(cmHandle, signRes).then((cmResult) => { 836 console.log("[Promise]finish verify success"); 837 }).catch((err: BusinessError) => { 838 console.error('[Promise]finish verify failed'); 839 }) 840} catch(error) { 841 console.error("[Promise]certManager finish error"); 842} 843``` 844 845## certManager.abort 846 847abort(handle: Uint8Array, callback: AsyncCallback\<void>) : void 848 849表示中止签名、验签的操作,使用Callback回调异步返回结果。 850 851**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 852 853**系统能力:** SystemCapability.Security.CertificateManager 854 855**参数**: 856 857| 参数名 | 类型 | 必填 | 说明 | 858| -------- | ------------------------------------------------- | ---- | -------------------------- | 859| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 860| callback | AsyncCallback\<void> | 是 | 回调函数。使用AsyncCallback的第一个error参数判断是否执行成功,error为null表示成功,不为null表示失败。 | 861 862**错误码:** 863 864以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 865 866| 错误码ID | 错误信息 | 867| -------- | ------------- | 868| 17500001 | There is an generic error occurred when calling the API. | 869 870**示例**: 871```ts 872import certManager from '@ohos.security.certManager'; 873 874/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 875let cmHandle: Uint8Array = new Uint8Array([ 876 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 877]); 878try { 879 certManager.abort(cmHandle, (err, cmResult) => { 880 if (err != null) { 881 console.error("[Callback]certManager abort failed"); 882 } else { 883 console.log("[Callback]certManager abort success"); 884 } 885 }); 886} catch(error) { 887 console.error("[Callback]certManager abort error"); 888} 889``` 890 891## certManager.abort 892 893abort(handle: Uint8Array) : Promise\<void> 894 895表示中止签名、验签的操作,使用Promise方式异步返回结果。 896 897**需要权限:** ohos.permission.ACCESS_CERT_MANAGER 898 899**系统能力:** SystemCapability.Security.CertificateManager 900 901**参数**: 902 903| 参数名 | 类型 | 必填 | 说明 | 904| -------- | ------------------------------------------------- | ---- | -------------------------- | 905| handle | Uint8Array | 是 | 表示初始化操作返回的句柄。 | 906 907**返回值**: 908 909| 类型 | 说明 | 910| ------------------------------------------- | -------------------- | 911| Promise\<void> | Promise对象。 | 912 913**错误码:** 914 915以下错误码的详细介绍请参见[证书管理错误码](errorcode-certManager.md)。 916 917| 错误码ID | 错误信息 | 918| -------- | ------------- | 919| 17500001 | There is an generic error occurred when calling the API. | 920 921**示例**: 922```ts 923import certManager from '@ohos.security.certManager'; 924import { BusinessError } from '@ohos.base'; 925 926/* cmHandle为业务调用init接口的返回值,此处仅为示例 */ 927let cmHandle: Uint8Array = new Uint8Array([ 928 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 929]); 930try { 931 certManager.abort(cmHandle).then((result) => { 932 console.log('[Promise]abort success'); 933 }).catch((error: BusinessError) => { 934 console.error('[Promise]abort failed'); 935 }) 936} catch (err) { 937 console.error("[Promise]certManager abort error"); 938} 939```