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