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| certManager | 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| certManager | installPrivateCertificate(keystore: Uint8Array, keystorePwd: string, certAlias: string) : Promise\<CMResult> | Installs a private credential. This API uses a promise to return the result. | 34| certManager | getPrivateCertificate(keyUri: string, callback: AsyncCallback\<CMResult>) : void | Obtains a private credential. This API uses an asynchronous callback to return the result. | 35| certManager | getPrivateCertificate(keyUri: string) : Promise\<CMResult> | Obtains a private credential. This API uses a promise to return the result. | 36| certManager | uninstallPrivateCertificate(keyUri: string, callback: AsyncCallback\<void>) : void | Uninstalls a private credential. This API uses an asynchronous callback to return the result. | 37| certManager | uninstallPrivateCertificate(keyUri: string) : Promise\<void> | Uninstalls a private credential. This API uses a promise to return the result.| 38| certManager | 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| certManager | init(authUri: string, spec: CMSignatureSpec) : Promise\<CMHandle> | Initializes the signing or signature verification operation. This API uses a promise to return the result. | 40| certManager | 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| certManager | 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| certManager | finish(handle: Uint8Array, callback: AsyncCallback\<CMResult>) : void | Finishes the signing operation. This API uses an asynchronous callback to return the result. | 43| certManager | finish(handle: Uint8Array, signature: Uint8Array, callback: AsyncCallback\<CMResult>) : void | Finishes the signing operation. This API uses an asynchronous callback to return the result. | 44| certManager | finish(handle: Uint8Array, signature?: Uint8Array) : Promise\<CMResult> | Finishes the signing or signature verification operation. This API uses a promise to return the result.| 45| certManager | 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| certManager | 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 certManager from '@ohos.security.certManager'; 56 import { BusinessError } from '@ohos.base'; 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 certManagerSample() { 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 certManager.installPrivateCertificate(keystore, keystorePwd, "testPriCredential"); 73 appKeyUri = (res.uri != undefined) ? res.uri : ''; 74 } catch (err) { 75 let e: BusinessError = err as BusinessError; 76 console.error("installPrivateCertificates error, errcode:" + e.code); 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: certManager.CMSignatureSpec = { 87 purpose: certManager.CmKeyPurpose.CM_KEY_PURPOSE_SIGN, 88 padding: certManager.CmKeyPadding.CM_PADDING_PSS, 89 digest: certManager.CmKeyDigest.CM_DIGEST_SHA256 90 }; 91 92 /* Generate a signature. */ 93 const signHandle: certManager.CMHandle = await certManager.init(appKeyUri, signSpec); 94 await certManager.update(signHandle.handle, srcData); 95 const signResult: certManager.CMResult = await certManager.finish(signHandle.handle); 96 97 /* Construct the parameters for signature verification. */ 98 const verifySpec: certManager.CMSignatureSpec = { 99 purpose: certManager.CmKeyPurpose.CM_KEY_PURPOSE_VERIFY, 100 padding: certManager.CmKeyPadding.CM_PADDING_PSS, 101 digest: certManager.CmKeyDigest.CM_DIGEST_SHA256 102 }; 103 104 /** Verify the signature. */ 105 const verifyHandle: certManager.CMHandle = await certManager.init(appKeyUri, verifySpec); 106 await certManager.update(verifyHandle.handle, srcData); 107 const verifyResult = await certManager.finish(verifyHandle.handle, signResult.outData); 108 console.log("sign and verify success"); 109 } catch (err) { 110 let e: BusinessError = err as BusinessError; 111 console.error("sign or verify failed, errcode:" + e.code); 112 } 113 114 try { 115 /* Uninstall a private credential. */ 116 await certManager.uninstallPrivateCertificate(appKeyUri); 117 } catch (err) { 118 let e: BusinessError = err as BusinessError; 119 console.error("uninstallPrivateCertificate failed, errcode:" + e.code); 120 } 121 } 122 ``` 123