1# Certificate Extension Development 2 3 4This topic walks you through on how to create a certificate extension (**CertExtension**) object, obtain the certificate extension information based on an object identifier (OID), and check whether the certificate is a CA certificate. 5 6 7## How to Develop 8 91. Import the [cert](../../reference/apis-device-certificate-kit/js-apis-cert.md) module. 10 ```ts 11 import { cert } from '@kit.DeviceCertificateKit'; 12 ``` 13 142. Use [cert.createCertExtension](../../reference/apis-device-certificate-kit/js-apis-cert.md#certcreatecertextension10) to create a **CertExtension** object. 15 163. Use [CertExtension.getEntry](../../reference/apis-device-certificate-kit/js-apis-cert.md#getentry10) to obtain the certificate extension of the specified OID. 17 184. Use [CertExtension.checkCA](../../reference/apis-device-certificate-kit/js-apis-cert.md#checkca10) to check whether the certificate is a CA certificate. 19 20```ts 21import { cert } from '@kit.DeviceCertificateKit'; 22import { BusinessError } from '@kit.BasicServicesKit'; 23import { util } from '@kit.ArkTS'; 24 25// Certificate extension data. The following is only an example. 26let extData = new Uint8Array([ 27 0x30, 0x40, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x1D, 28 0x13, 0x01, 0x01, 0xFF, 0x04, 0x05, 0x30, 0x03, 29 0x01, 0x01, 0xFF, 0x30, 0x0E, 0x06, 0x03, 0x55, 30 0x1D, 0x0F, 0x01, 0x01, 0xFF, 0x04, 0x04, 0x03, 31 0x02, 0x01, 0xC6, 0x30, 0x1D, 0x06, 0x03, 0x55, 32 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xE0, 0x8C, 33 0x9B, 0xDB, 0x25, 0x49, 0xB3, 0xF1, 0x7C, 0x86, 34 0xD6, 0xB2, 0x42, 0x87, 0x0B, 0xD0, 0x6B, 0xA0, 35 0xD9, 0xE4 36]); 37 38// Certificate extension example. 39function certExtensionSample(): void { 40 let textEncoder = new util.TextEncoder(); 41 let encodingBlob: cert.EncodingBlob = { 42 data: extData, 43 // Certificate extension format. Currently, only the DER format is supported. 44 encodingFormat: cert.EncodingFormat.FORMAT_DER 45 }; 46 47 // Create a CertExtension object. 48 cert.createCertExtension(encodingBlob, (err, certExtension) => { 49 if (err != null) { 50 // The CertExtension object fails to be created. 51 console.error(`createCertExtension failed, errCode:${err.code}, errMsg:${err.message} `); 52 return; 53 } 54 // The CertExtension object is created. 55 console.log('createCertExtension success'); 56 57 try { 58 // Obtain the certificate extension information based on an OID. 59 let oidData = '2.5.29.14'; 60 let oid: cert.DataBlob = { 61 data: textEncoder.encodeInto(oidData), 62 } 63 let entry = certExtension.getEntry(cert.ExtensionEntryType.EXTENSION_ENTRY_TYPE_ENTRY, oid); 64 65 // Check whether the certificate is a CA certificate. 66 let pathLen = certExtension.checkCA(); 67 console.log('test cert extension success'); 68 } catch (err) { 69 let e: BusinessError = err as BusinessError; 70 console.error(`operation failed, message:${e.message} ,code:${e.code} `); 71 } 72 }); 73} 74``` 75