1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17// [Start create_parse_validate_certificate_extension_info_objects] 18 19import { cert } from '@kit.DeviceCertificateKit'; 20import { BusinessError } from '@kit.BasicServicesKit'; 21import { util } from '@kit.ArkTS'; 22 23// 证书扩展数据,以下只是一个示例。需要根据具体业务来赋值 24let extData = new Uint8Array([ 25 0x30, 0x40, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x1D, 26 0x13, 0x01, 0x01, 0xFF, 0x04, 0x05, 0x30, 0x03, 27 0x01, 0x01, 0xFF, 0x30, 0x0E, 0x06, 0x03, 0x55, 28 0x1D, 0x0F, 0x01, 0x01, 0xFF, 0x04, 0x04, 0x03, 29 0x02, 0x01, 0xC6, 0x30, 0x1D, 0x06, 0x03, 0x55, 30 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xE0, 0x8C, 31 0x9B, 0xDB, 0x25, 0x49, 0xB3, 0xF1, 0x7C, 0x86, 32 0xD6, 0xB2, 0x42, 0x87, 0x0B, 0xD0, 0x6B, 0xA0, 33 0xD9, 0xE4 34]); 35 36// 证书扩展示例 37function certExtensionSample(): void { 38 let textEncoder = new util.TextEncoder(); 39 let encodingBlob: cert.EncodingBlob = { 40 data: extData, 41 // 证书扩展格式,目前仅支持DER格式 42 encodingFormat: cert.EncodingFormat.FORMAT_DER 43 }; 44 45 // 创建一个证书扩展实例 46 cert.createCertExtension(encodingBlob, (err, certExtension) => { 47 if (err != null) { 48 // 证书扩展实例创建失败 49 console.error(`createCertExtension failed, errCode:${err.code}, errMsg:${err.message} `); 50 return; 51 } 52 // 证书扩展实例创建成功 53 console.log('createCertExtension success'); 54 55 try { 56 // 根据OID获取证书扩展信息 57 let oidData = '2.5.29.14'; 58 let oid: cert.DataBlob = { 59 data: textEncoder.encodeInto(oidData), 60 } 61 let entry = certExtension.getEntry(cert.ExtensionEntryType.EXTENSION_ENTRY_TYPE_ENTRY, oid); 62 63 // 检查证书是否为CA证书 64 let pathLen = certExtension.checkCA(); 65 console.log('test cert extension success'); 66 } catch (err) { 67 let e: BusinessError = err as BusinessError; 68 console.error(`operation failed, message:${e.message} ,code:${e.code} `); 69 } 70 }); 71} 72 73 74// [End create_parse_validate_certificate_extension_info_objects] 75 76@Entry 77@Component 78struct Index { 79 @State message: string = 'CreateParseVerifyCertextensionObject'; 80 81 build() { 82 Column({ space: 5 }) { 83 Text(this.message) 84 .fontSize(25) 85 .fontWeight(FontWeight.Bold) 86 Button('Call CreateParseVerifyCertextensionObject') 87 .width('70%') 88 .onClick(()=>{ 89 try { 90 certExtensionSample(); 91 this.message = 'Call Success'; 92 } catch { 93 this.message = 'Call Fail'; 94 } 95 }) 96 } 97 .height('100%') 98 .width('100%') 99 } 100}