openharmony 鸿蒙 verify-certchain-by-systemca

2026-08-25 浏览 (1)

Using the Prebuilt CA Certificate to Validate a Certificate Chain

Since API version 20, you can use the prebuilt CA certificate to validate a certificate chain.

To do so, you need to create a certificate chain object first.

How to Develop

  1. Import the cert module.

    import { cert } from '@kit.DeviceCertificateKit';
    
  2. Use cert.createX509CertChain to create an X.509 certificate chain (X509CertChain) object.

  3. Call x509CertChain.validate to set trustSystemCa to true and use the prebuilt CA certificate to validate the certificate chain.


import { cert } from '@kit.DeviceCertificateKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { util } from '@kit.ArkTS';
// ...
async function sample() {
  let textEncoder = new util.TextEncoder();
  // Binary data of the certificate chain, which needs to be assigned by the service.
  const encodingBlob: cert.EncodingBlob = {
    data: textEncoder.encodeInto(certChainData),
    // Assign a value based on the encodingData format. FORMAT_PEM, FORMAT_DER, and FORMAT_PKCS7 are supported.
    encodingFormat: cert.EncodingFormat.FORMAT_PEM
  };
  let x509CertChain: cert.X509CertChain = {} as cert.X509CertChain;
  try {
    x509CertChain = await cert.createX509CertChain(encodingBlob);
  } catch (err) {
    let e: BusinessError = err as BusinessError;
    console.error(`createX509CertChain failed, errCode: ${e.code}, errMsg: ${e.message}`);
  }

  // Certificate chain verification data, which needs to be assigned by the service.
  const param: cert.CertChainValidationParameters = {
    date: '20250623163000Z',
    trustAnchors: [{}],
    trustSystemCa: true,
  };
  try {
    const validationRes = await x509CertChain.validate(param);
    console.info('X509CertChain validate result: success.');
  } catch (err) {
    let e: BusinessError = err as BusinessError;
    console.error(`X509CertChain validate failed, errCode: ${e.code}, errMsg: ${e.message}`);
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 create-verify-cerchainvalidator-revocation-object

openharmony 鸿蒙 create-parse-verify-certextension-object

openharmony 鸿蒙 create-cms-enveloped-object

openharmony 鸿蒙 certManager-overview

openharmony 鸿蒙 certManagerDialog-guidelines

openharmony 鸿蒙 create-get-cert-crl-object

openharmony 鸿蒙 device-certificate-kit-intro

openharmony 鸿蒙 create-cms-decapsulation-object

openharmony 鸿蒙 create-verify-cerchainvalidator-object

openharmony 鸿蒙 Readme-EN

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/IADYDE48