• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18// [Start certificate_collection_and_certificate_revocation_list_collection_object_creation_and_acquisition]
19import { cert } from '@kit.DeviceCertificateKit';
20import { BusinessError } from '@kit.BasicServicesKit';
21import { util } from '@kit.ArkTS';
22
23async function createX509CRL(): Promise<cert.X509CRL> {
24  let crlData = '-----BEGIN X509 CRL-----\n' +
25    'MIHzMF4CAQMwDQYJKoZIhvcNAQEEBQAwFTETMBEGA1UEAxMKQ1JMIGlzc3VlchcN\n' +
26    'MTcwODA3MTExOTU1WhcNMzIxMjE0MDA1MzIwWjAVMBMCAgPoFw0zMjEyMTQwMDUz\n' +
27    'MjBaMA0GCSqGSIb3DQEBBAUAA4GBACEPHhlaCTWA42ykeaOyR0SGQIHIOUR3gcDH\n' +
28    'J1LaNwiL+gDxI9rMQmlhsUGJmPIPdRs9uYyI+f854lsWYisD2PUEpn3DbEvzwYeQ\n' +
29    '5SqQoPDoM+YfZZa23hoTLsu52toXobP74sf/9K501p/+8hm4ROMLBoRT86GQKY6g\n' +
30    'eavsH0Q3\n' +
31    '-----END X509 CRL-----\n';
32
33  // 证书吊销列表二进制数据,需业务自行赋值
34  let textEncoder = new util.TextEncoder();
35  let encodingBlob: cert.EncodingBlob = {
36    data: textEncoder.encodeInto(crlData),
37    // 根据encodingData的格式进行赋值,支持FORMAT_PEM和FORMAT_DER
38    encodingFormat: cert.EncodingFormat.FORMAT_PEM
39  };
40  let x509CRL: cert.X509CRL = {} as cert.X509CRL;
41  try {
42    x509CRL = await cert.createX509CRL(encodingBlob);
43  } catch (err) {
44    let e: BusinessError = err as BusinessError;
45    console.error(`createX509CRL failed, errCode: ${e.code}, errMsg: ${e.message}`);
46  }
47  return x509CRL;
48}
49
50async function createX509Cert(): Promise<cert.X509Cert> {
51  let certData = '-----BEGIN CERTIFICATE-----\n' +
52    'MIIBHTCBwwICA+gwCgYIKoZIzj0EAwIwGjEYMBYGA1UEAwwPRXhhbXBsZSBSb290\n' +
53    'IENBMB4XDTIzMDkwNTAyNDgyMloXDTI2MDUzMTAyNDgyMlowGjEYMBYGA1UEAwwP\n' +
54    'RXhhbXBsZSBSb290IENBMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEHjG74yMI\n' +
55    'ueO7z3T+dyuEIrhxTg2fqgeNB3SGfsIXlsiUfLTatUsU0i/sePnrKglj2H8Abbx9\n' +
56    'PK0tsW/VgqwDIDAKBggqhkjOPQQDAgNJADBGAiEApVZno/Z7WyDc/muRN1y57uaY\n' +
57    'Mjrgnvp/AMdE8qmFiDwCIQCrIYdHVO1awaPgcdALZY+uLQi6mEs/oMJLUcmaag3E\n' +
58    'Qw==\n' +
59    '-----END CERTIFICATE-----\n';
60
61  let textEncoder = new util.TextEncoder();
62  let encodingBlob: cert.EncodingBlob = {
63    data: textEncoder.encodeInto(certData),
64    // 根据encodingData的格式进行赋值,支持FORMAT_PEM和FORMAT_DER
65    encodingFormat: cert.EncodingFormat.FORMAT_PEM
66  };
67
68  let x509Cert: cert.X509Cert = {} as cert.X509Cert;
69  try {
70    x509Cert = await cert.createX509Cert(encodingBlob);
71  } catch (err) {
72    let e: BusinessError = err as BusinessError;
73    console.error(`createX509Cert failed, errCode: ${e.code}, errMsg: ${e.message}`);
74  }
75  return x509Cert;
76}
77
78async function sample() {
79  const x509Cert = await createX509Cert();
80  const x509CRL = await createX509CRL();
81  let collection: cert.CertCRLCollection = {} as cert.CertCRLCollection;
82  try {
83    collection = cert.createCertCRLCollection([x509Cert], [x509CRL]);
84    console.log('createCertCRLCollection success');
85  } catch (err) {
86    console.error('createCertCRLCollection failed');
87  }
88
89  const certParam: cert.X509CertMatchParameters = {
90    validDate: '231128000000Z'
91  }
92  try {
93    let certs: cert.X509Cert[] = await collection.selectCerts(certParam);
94  } catch (err) {
95    console.error('selectCerts failed');
96  }
97
98  const crlParam: cert.X509CRLMatchParameters = {
99    x509Cert: x509Cert
100  }
101  try {
102    let crls: cert.X509CRL[] = await collection.selectCRLs(crlParam);
103    console.error('selectCRLs success');
104  } catch (err) {
105    console.error('selectCRLs failed');
106  }
107}
108
109
110// [End certificate_collection_and_certificate_revocation_list_collection_object_creation_and_acquisition]
111@Entry
112@Component
113struct Index {
114  @State message: string = 'CreateGetCertCrlObject';
115
116  build() {
117    Column({ space: 5 }) {
118      Text(this.message)
119        .fontSize(25)
120        .fontWeight(FontWeight.Bold)
121      Button('Call CreateGetCertCrlObject')
122        .width('70%')
123        .onClick(() => {
124          try {
125            sample();
126            this.message = 'Call Success';
127          } catch {
128            this.message = 'Call Fail';
129          }
130        })
131    }
132    .height('100%')
133    .width('100%')
134  }
135}