• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Certificate Management Dialog Box Development
2
3<!--Kit: Device Certificate Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @chaceli-->
6<!--Designer: @chande-->
7<!--Tester: @zhangzhi1995-->
8<!--Adviser: @zengyawen-->
9
10> **NOTE**
11>
12> This guide applies to the SDK of API version 13 or later.
13
14You can use the **certificateManagerDialog** APIs to open the certificate management dialog box and perform certificate management, such as installing, storing, using, and destroying a certificate.
15
16## Available APIs
17
18For details about the APIs, see [Certificate Management Dialog Box](../../reference/apis-device-certificate-kit/js-apis-certManagerDialog.md).
19
20The following table describes the commonly used APIs.
21
22| Instance         | API                                                      | Description                                        |
23| --------------- | ------------------------------------------------------------ | -------------------------------------------- |
24| certificateManagerDialog        | openCertificateManagerDialog(context: common.Context, pageType: CertificateDialogPageType): Promise\<void> | Opens the certificate management dialog box and displays the page of the specified type. This API uses a promise to return the result.|
25| certificateManagerDialog | openInstallCertificateDialog(context: common.Context, certType: CertificateType, certScope: CertificateScope, cert: Uint8Array): Promise\<string><sup>14+</sup> | Opens the certificate installation dialog box to install a certificate. This API uses a promise to return the unique identifier of the installed certificate.<br>This API is available only to 2-in-1 devices.|
26| certificateManagerDialog | openUninstallCertificateDialog(context: common.Context, certType: CertificateType, certUri: string): Promise\<void><sup>18+</sup> | Opens the certificate uninstallation dialog box to uninstall a certificate. This API uses a promise to return the result.<br>This API is available only to 2-in-1 devices.|
27| certificateManagerDialog | openCertificateDetailDialog(context: common.Context, cert: Uint8Array, property: CertificateDialogProperty): Promise\<void><sup>18+</sup> | Opens the certificate details dialog box to display the certificate details. This API uses a promise to return the result.<br>This API is available only to 2-in-1 devices.|
28
29## How to Develop
30
311. Request and declare permissions.
32
33   Required permission: **ohos.permission.ACCESS_CERT_MANAGER**
34
35   For details about how to request permissions, see [Workflow for Requesting Permissions](../AccessToken/determine-application-mode.md).
36
37   For details about how to declare permissions, see [Declaring Permissions](../AccessToken/declare-permissions.md).
38
392. Import the required module.
40
41   ```ts
42   import { certificateManagerDialog } from '@kit.DeviceCertificateKit';
43   import { BusinessError } from '@kit.BasicServicesKit';
44   import { common } from '@kit.AbilityKit';
45   import { UIContext } from '@kit.ArkUI';
46   ```
47
483. Open the certificate management dialog box.
49
50   ```ts
51   /* context is application context information, which is obtained by the caller. The context here is only an example. */
52   let context: common.Context = new UIContext().getHostContext() as common.Context;
53   async function certificateManagerDialogSample() {
54     /* pageType specifies the type of the page to display. In this example, pageType is PAGE_MAIN, which indicates the main page of the Certificate Manager application. */
55     let pageType: certificateManagerDialog.CertificateDialogPageType = certificateManagerDialog.CertificateDialogPageType.PAGE_MAIN;
56     try {
57       certificateManagerDialog.openCertificateManagerDialog(context, pageType).then(() => {
58         console.info('Succeeded in opening certificate manager dialog.');
59       }).catch((err: BusinessError) => {
60         console.error(`Failed to open certificate manager dialog. Code: ${err.code}, message: ${err.message}`);
61       })
62     } catch (error) {
63       console.error(`Failed to open certificate manager dialog. Code: ${error.code}, message: ${error.message}`);
64     }
65   }
66   ```
674. Call the APIs for installing a certificate, uninstalling a certificate, and viewing certificate details, respectively. These APIs are available only to 2-in-1 devices.
68
69   ```ts
70   /* context is application context information, which is obtained by the caller. The context here is only an example. */
71   let context: common.Context = new UIContext().getHostContext() as common.Context;
72   async function userCADialogSample() {
73     let certUri: string = '';
74     let certType = certificateManagerDialog.CertificateType.CA_CERT;
75     /* The user CA certificate data must be assigned based on the service. */
76     let cert = new Uint8Array([
77       0x30, 0x82, 0x01, 0x2E, 0x30, 0x81, 0xD5, 0x02, 0x14, 0x28, 0x75, 0x71, 0x22, 0xDF, 0xDC, 0xCB,
78     ]);
79
80     try {
81       /* Install the certificate. */
82       let certScope = certificateManagerDialog.CertificateScope.CURRENT_USER; /* Install the certificate under the current user. */
83       certificateManagerDialog.openInstallCertificateDialog(context, certType, certScope, cert).then((result) => {
84         console.info('Succeeded in opening install ca dialog.');
85         certUri = result;
86       }).catch((err: BusinessError) => {
87         console.error(`Failed to open install ca dialog. Code: ${err.code}, message: ${err.message}`);
88       })
89     } catch (error) {
90       console.error(`Failed to open install ca dialog. Code: ${error.code}, message: ${error.message}`);
91     }
92
93     try {
94       /* Uninstall the certificate. */
95       certificateManagerDialog.openUninstallCertificateDialog(context, certType, certUri).then(() => {
96         console.info('Succeeded in opening uninstall ca dialog.');
97       }).catch((err: BusinessError) => {
98         console.error(`Failed to open uninstall ca dialog. Code: ${err.code}, message: ${err.message}`);
99       })
100     } catch (error) {
101       console.error(`Failed to open uninstall ca dialog. Code: ${error.code}, message: ${error.message}`);
102     }
103
104     try {
105       let property: certificateManagerDialog.CertificateDialogProperty = {
106         showInstallButton: false    /* Do not display the button for installing the certificate. */
107       };
108       /* Display certificate details. */
109       certificateManagerDialog.openCertificateDetailDialog(context, cert, property).then(() => {
110         console.info('Succeeded in opening show ca detail dialog.');
111       }).catch((err: BusinessError) => {
112         console.error(`Failed to open show ca detail dialog. Code: ${err.code}, message: ${err.message}`);
113       })
114     } catch (error) {
115       console.error(`Failed to open show ca detail dialog. Code: ${error.code}, message: ${error.message}`);
116     }
117   }
118   ```
119