// Convert the certificate data form a string to a Uint8Array.
...
...
@@ -133,7 +145,7 @@ function certSample() {
});
// Time represented in a string.
letdate="150527000001Z";
letdate="20220830000001Z";
// Verify the certificate validity period.
try{
...
...
@@ -145,6 +157,105 @@ function certSample() {
}
```
## Operating Certificate Extensions
> **NOTE**
>
> The following scenario applies to JS development using API version 10 and OpenHarmony SDK 4.0.9 or later.
**When to Use**
Typical operations involve the following:
1. Parse the certificate extension data to generate a certificate extension object.
2. Obtain certificate extension information, for example, obtaining the object identifiers (OIDs) of certificate extensions and obtaining specific data based on an OID.
3. Check whether a certificate is a CA certificate.
**Available APIs**
For details about the APIs, see [Certificate](../reference/apis/js-apis-cert.md).
The table below describes the APIs used in this guide.
| cryptoCert | createCertExtension(inStream : EncodingBlob, callback : AsyncCallback) : void | Creates a **certExtension** instance. This API uses an asynchronous callback to return the result.|
| cryptoCert | createCertExtension(inStream : EncodingBlob) : Promise | Creates a **certExtension** instance. This API uses a promise to return the result. |
| CertExtension | getEncoded() : EncodingBlob | Obtains the serialized data of the certificate extension. |
| CertExtension | getOidList(valueType : ExtensionOidType) : DataArray | Obtains the OIDs of certificate extensions. |
| CertExtension | getEntry(valueType: ExtensionEntryType, oid : DataBlob) : DataBlob | Obtains the certificate extension object information. |
| CertExtension | checkCA() : number | Checks whether the certificate is a CA certificate. |
**How to Develop**
Example: Parse the X.509 certificate extension data to generate a **CerExtension** instance and call the related APIs.
```javascript
importcryptoCertfrom'@ohos.security.cert';
// Certificate extension data, which is only an example. Set it based on service requirements.
letcertData=newUint8Array([
0x30,0x40,0x30,0x0F,0x06,0x03,0x55,0x1D,
0x13,0x01,0x01,0xFF,0x04,0x05,0x30,0x03,
0x01,0x01,0xFF,0x30,0x0E,0x06,0x03,0x55,
0x1D,0x0F,0x01,0x01,0xFF,0x04,0x04,0x03,
0x02,0x01,0xC6,0x30,0x1D,0x06,0x03,0x55,
0x1D,0x0E,0x04,0x16,0x04,0x14,0xE0,0x8C,
0x9B,0xDB,0x25,0x49,0xB3,0xF1,0x7C,0x86,
0xD6,0xB2,0x42,0x87,0x0B,0xD0,0x6B,0xA0,
0xD9,0xE4
]);
// Convert the string into a Uint8Array.
functionstringToUint8Array(str){
vararr=[];
for(vari=0,j=str.length;i<j;i++){
arr.push(str.charCodeAt(i));
}
returnnewUint8Array(arr);
}
// Certificate extension example.
functioncertExtensionSample(){
letencodingBlob={
data:certData,
// Certificate extension format. Currently, only the DER format is supported.