b704c991创建于 2025年10月27日历史提交

Creating a TrustAnchor Object Array from a .p12 File

This topic walks you through on how to create a TrustAnchor object array from a .p12 file during certificate chain verification.

How to Develop

  1. Import the cert module.

  2. Use cert.createTrustAnchorsWithKeyStore to create an X509TrustAnchor array object based on the .p12 file.

import { cert } from '@kit.DeviceCertificateKit';
import { BusinessError } from '@kit.BasicServicesKit';

function test() {
  // ...
  try {
    cert.createTrustAnchorsWithKeyStore(p12Data, '123456').then((data) => {
      console.info('createTrustAnchorsWithKeyStore result: success, the num of result is :' + data.length);
    }).catch((err: BusinessError) => {
      console.error(`createTrustAnchorsWithKeyStore failed, errCode: ${err.code}, message: ${err.message}`);
    })
  } catch (error) {
    console.error(`createTrustAnchorsWithKeyStore failed, errCode: ${error.code}, message: ${error.message}`);
  }
}