1# Certificate Management Development
2
3> **NOTE**
4>
5> The SDK of API version 11 or later must be used.
6
7## Scenarios
8
9**Typical scenarios**
10
11   - Install an application certificate and its private credential.
12
13   - Obtain the application certificate and private credential.
14
15   - Perform signing and signature verification using an application certificate and its private credential.
16
17   - Uninstall an application certificate and its private credential.
18
19Before you get started, you need to know the combinations of the algorithm and the signing/signature verification parameters supported by certificate management.
20
21   The credential installation, signing, and signature verification in certificate management depends on [HUKS](../UniversalKeystoreKit/huks-overview.md). The algorithms supported by certificate management are a subset of HUKS. Currently, only private credentials using the RSA or ECC algorithm can be installed and used. For details about the parameter combinations supported by signing and signature verification, see the description of RSA and ECC in [Signing and Signature Verification Overview and Algorithm Specifications](../UniversalKeystoreKit/huks-signing-signature-verification-overview.md).
22
23
24## Available APIs
25
26For details about the APIs, see [Certificate Management](../../reference/apis-device-certificate-kit/js-apis-certManager.md).
27
28The following table describes the APIs used in the typical scenarios mentioned above.
29
30| Instance         | API                                                      | Description                                        |
31| --------------- | ------------------------------------------------------------ | -------------------------------------------- |
32| certificateManager        | installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string, callback: AsyncCallback\<CMResult>) : void  | Installs a private credential. This API uses an asynchronous callback to return the result.                |
33| certificateManager        | installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string) : Promise\<CMResult> | Installs a private credential. This API uses a promise to return the result.                 |
34| certificateManager        | getPrivateCertificate(keyUri: string, callback: AsyncCallback\<CMResult>) : void    | Obtains a private credential. This API uses an asynchronous callback to return the result.         |
35| certificateManager        | getPrivateCertificate(keyUri: string) : Promise\<CMResult>                         | Obtains a private credential. This API uses a promise to return the result.          |
36| certificateManager        | uninstallPrivateCertificate(keyUri: string, callback: AsyncCallback\<void>) : void  | Uninstalls a private credential. This API uses an asynchronous callback to return the result.        |
37| certificateManager        | uninstallPrivateCertificate(keyUri: string) : Promise\<void> | Uninstalls a private credential. This API uses a promise to return the result. |
38| certificateManager | init(authUri: string, spec: CMSignatureSpec, callback: AsyncCallback\<CMHandle>) : void | Initializes the signing or signature verification operation. This API uses an asynchronous callback to return the result. |
39| certificateManager | init(authUri: string, spec: CMSignatureSpec) : Promise\<CMHandle>  | Initializes the signing or signature verification operation. This API uses a promise to return the result. |
40| certificateManager        | update(handle: Uint8Array, data: Uint8Array, callback: AsyncCallback\<void>) : void         | Updates the data to be signed or verified. This API uses an asynchronous callback to return the result.        |
41| certificateManager        | update(handle: Uint8Array, data: Uint8Array) : Promise\<void> | Updates the data to be signed or verified. This API uses a promise to return the result. |
42| certificateManager        | finish(handle: Uint8Array, callback: AsyncCallback\<CMResult>) : void         | Finishes the signing operation. This API uses an asynchronous callback to return the result.        |
43| certificateManager        | finish(handle: Uint8Array, signature: Uint8Array, callback: AsyncCallback\<CMResult>) : void     | Finishes the signing operation. This API uses an asynchronous callback to return the result.        |
44| certificateManager        | finish(handle: Uint8Array, signature?: Uint8Array) : Promise\<CMResult> | Finishes the signing or signature verification operation. This API uses a promise to return the result. |
45| certificateManager        | abort(handle: Uint8Array, callback: AsyncCallback\<void>) : void         | Aborts the signing or signature verification operation. This API uses an asynchronous callback to return the result.        |
46| certificateManager        | abort(handle: Uint8Array) : Promise\<void> | Aborts the signing or signature verification operation. This API uses a promise to return the result. |
47
48## How to Develop
49
501. Request permissions.<br> To call **certManager** APIs, declare the ohos.permission.ACCESS_CERT_MANAGER permission in the **requestPermissions** field in the **module.json5** file. For more information, see [module.json5](../../quick-start/module-configuration-file.md).
51
522. Import modules.
53
54   ```ts
55   import { certificateManager } from '@kit.DeviceCertificateKit';
56   import { BusinessError } from '@kit.BasicServicesKit';
57   ```
583. Install a private credential, obtain the private credential, use it to sign and verify data. Then, uninstall the private credential.
59
60   ```ts
61   async function certificateManagerSample() {
62     /* The data of the credential to be installed must be assigned based on the service. The data in this example is not the real credential data. */
63     let keystore: Uint8Array = new Uint8Array([
64       0x30, 0x82, 0x04, 0x6a, 0x02, 0x01,
65     ]);
66
67     /* Keystore password of the credential to be installed. */
68     let keystorePwd: string = '123456';
69     let appKeyUri: string = '';
70     try {
71       /* Install a private credential. */
72       const res = await certificateManager.installPrivateCertificate(keystore, keystorePwd, "testPriCredential");
73       appKeyUri = (res.uri != undefined) ? res.uri : '';
74     } catch (err) {
75       let e: BusinessError = err as BusinessError;
76       console.error(`Failed to install private certificate. Code: ${e.code}, message: ${e.message}`);
77     }
78
79     try {
80       /* srcData is the data to be signed and verified. */
81       let srcData: Uint8Array = new Uint8Array([
82         0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
83     ]);
84
85       /* Construct the parameters used for signing. */
86       const signSpec: certificateManager.CMSignatureSpec = {
87         purpose: certificateManager.CmKeyPurpose.CM_KEY_PURPOSE_SIGN,
88         padding: certificateManager.CmKeyPadding.CM_PADDING_PSS,
89         digest: certificateManager.CmKeyDigest.CM_DIGEST_SHA256
90       };
91
92       /* Generate a signature. */
93       const signHandle: certificateManager.CMHandle = await certificateManager.init(appKeyUri, signSpec);
94       await certificateManager.update(signHandle.handle, srcData);
95       const signResult: certificateManager.CMResult = await certificateManager.finish(signHandle.handle);
96
97       /* Construct the parameters for signature verification. */
98       const verifySpec: certificateManager.CMSignatureSpec = {
99         purpose: certificateManager.CmKeyPurpose.CM_KEY_PURPOSE_VERIFY,
100         padding: certificateManager.CmKeyPadding.CM_PADDING_PSS,
101         digest: certificateManager.CmKeyDigest.CM_DIGEST_SHA256
102       };
103
104       /** Verify the signature. */
105       const verifyHandle: certificateManager.CMHandle = await certificateManager.init(appKeyUri, verifySpec);
106       await certificateManager.update(verifyHandle.handle, srcData);
107       const verifyResult = await certificateManager.finish(verifyHandle.handle, signResult.outData);
108       console.info('Succeeded in signing and verifying.');
109     } catch (err) {
110       let e: BusinessError = err as BusinessError;
111       console.error(`Failed to sign or verify. Code: ${e.code}, message: ${e.message}`);
112     }
113
114     try {
115       /* Uninstall a private credential. */
116       await certificateManager.uninstallPrivateCertificate(appKeyUri);
117     } catch (err) {
118       let e: BusinessError = err as BusinessError;
119       console.error(`Failed to uninstall private certificate. Code: ${e.code}, message: ${e.message}`);
120     }
121   }
122   ```
123