In the request that your application server receives from the application, the key attestation certificate chain (certificate array) uses the X.509 format. The first certificate is the key attestation certificate, and the last one is the root CA certificate, with the intermediate CA certificates in the middle.
The certificate chain returned by the online key attestation API contains three certificates: one key attestation certificate, one intermediate CA certificate, and one root CA certificate.
The certificate chain returned by the offline key attestation API contains four certificates: one key attestation certificate, two intermediate CA certificates, and one root CA certificate.
The application server verifies the key attestation certificate chain as follows:
Use the trusted root CA certificate from the official website to verify the validity of the certificate chain. (Download address of the root CA certificate)
Do not use the subordinate CA certificate to verify the key attestation certificate chain on the application server. The subordinate CA certificate may change due to expiration or certificate revocation.
Parse the key attestation certificate to obtain the application public key, challenge, application ID, and key management component ID.
The application public key is obtained from the subjectPublicKeyInfo field of the key attestation certificate. Other fields are obtained from the Extensions field. The Extensions field OID is 1.3.6.1.4.1.2011.2.376.1.3 (key attestation extension field).
Check whether the challenge is the same as that in the application server cache in step 3 of the process of creating a key to establish a trusted credential.
Check whether the application ID is the same as expected. Check whether the request is from the expected HarmonyOS application based on the application ID.
Check whether the key source is the same as expected.
Key Attestation Certificate Format


The Extensions field is in the ASN.1 DER encoding format. The data structure is defined as follows:
HuaweiAttestation ::= SEQUENCE {
version AttestationVersion DEFAULT v1,
claim1 AttestationClaim,
claim2 AttestationClaim,
claim3 AttestationClaim,
... ...
}
AttestationVersion ::= INTEGER { v1(0) }
AttestationClaim ::= SEQUENCE {
securityLevel SecurityLevel,
type AttestationType,
value AttestationValue
}
SecurityLevel ::= INTEGER
AttestationType ::= OBJECT IDENTIFIER
AttestationValue ::= ANY -- DEFINED BY AttestationType
ApplicationIDType ::= SEQUENCE {
type OBJECT IDENTIFIER,
value OCT_STR
} Values of AttestationClaim
| No. | Type (OID) | Value Data Type | securityLevel | Claim Description |
|---|---|---|---|---|
| 1 | 1.3.6.1.4.1.2011.2.376.2.1.4 | OCT_STR | Reserved. | Challenge passed by the application. |
| 2 | 1.3.6.1.4.1.2011.2.376.2.1.3 | ApplicationIDType | Reserved. | Application ID. |
| 3 | 1.3.6.1.4.1.2011.2.376.2.2.2.6 | OCT_STR | Reserved. | Key management component ID. The value is fixed at 0x28c4fb4944afec11b9090242ac120002 (component ID of Universal Keystore Kit). |
| 4 | 1.3.6.1.4.1.2011.2.376.2.2.4.8 | UTF8String | 2 | Product model of the device, which is supported since API version 20. The product model is used in device risk control scenarios to identify black/gray industry devices. Do not use this field to authenticate device identities. |
Values of securityLevel
| securityLevel | Description |
|---|---|
| 2 | Rich Execution Environment (REE), whose data comes from the HarmonyOS system layer. |
| 3 | Trusted Execution Environment (TEE), which protects data. |
Values of ApplicationIDType
| Type (OID) | Value Description |
|---|---|
| 1.3.6.1.4.1.2011.2.376.2.1.3.1 | HarmonyOS HAP application information, including the following fields: 1. appId: includes the bundle name of the HAP application and public key hash of the signing certificate. 2. bundleName: bundle name of the HAP application. 3. appIdentifier: unified app ID allocated to the HAP application in AppGallery Connect. This field is supported since API version 20. 4. appMode: status of the HAP application, which can be debug or release. This field is supported since API version 20. The value is a JSON string. An example is as follows: { "appId":"com.example.attesthcts_BDmjsOezxRmguzlYRVhQavW22Eswi5sX61wOAysWPOGS8TZ5tY1u1A9EcvarzyrfOEj5zT8BCGkfFkVjn0m5wzo=", "bundleName":"com.example.attesthcts", "appIdentifier":"5765880207853009781", "appMode":"release" } |
| 1.3.6.1.4.1.2011.2.376.2.1.3.2 | System ability ID. An example is as follows: {processName: "huawei_share"} |
Example
import org.bouncycastle.asn1.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.CertPath;
import java.security.cert.CertPathValidator;
import java.security.cert.CertificateFactory;
import java.security.cert.PKIXCertPathValidatorResult;
import java.security.cert.PKIXParameters;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import com.alibaba.fastjson2.JSON;
public class ParseAttestation {
static {
Security.addProvider(new BouncyCastleProvider());
}
// Key attestation certificate chain data obtained by a HarmonyOS application by using the anonAttestKeyItem API.
// The certificate chain returned by the online key attestation API contains three certificates, and the certificate chain returned by the offline key attestation API contains four certificates.
static String[] g_attestCertStr = new String[]{"-----BEGIN CERTIFICATE-----\nMIIEUDCCA/WgAwIBAgIOCfv5Xf9hjA2u32gjpG8wCgYIKoZIzj0EAwIwXTE5MDcGA1UEAwwwSHVhd2VpIENCRyBFQ0MgRGV2aWNlIEFub255bW91cyBBdHRlc3RhdGlvbiBDQSAxMRMwEQYDVQQKDApIdWF3ZWkgQ0JHMQswCQYDVQQGEwJDTjAeFw0yNTA1MTMwNjI3NDlaFw0yNTA1MjAwNjI3NDlaMCwxKjAoBgNVBAMMIURldmljZSBDZXJ0aWZpY2F0ZSBNYW5hZ2VtZW50IEtleTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALd0wgFgvDF5uPq2hh69LdRHnIX3+mzdAzf10L9Jk6bWPZqqTvz88ZX7e12Su1Myf5iyT3TMKjZ+Y2SnsWpHG/7Dpx990u7/CxeeRY0qziIqMTEbrLFaSHY++///9SxmEiM7a3z2Ged2FzDSvOTj1JVmm2hk+bUcceTVuHAmRwidFIQrL5h/lxaO3uPFqbTdiW6ocz06pEbi8mg6LAafik1pfsO30a3yIGiH1f4uZhCEFjHQxQdSFsRPh04Ehclx6lQ196tO0d3RHR8dxL7ghGNxs9rB1Sq/0TH2mK1vKAY/YgvBs5nypOnDY+0MXN7j5NucvJ32wssGI7CbmMxVeZMCAwEAAaOCAf0wggH5MB0GA1UdDgQWBBQA6HLpfdJvtiqPqQXenry8b6qjYzAMBgNVHRMBAf8EAjAAMAkGA1UdOAQCBQAwHwYDVR0jBBgwFoAU4yzL/3aHOxL7QyI/P/sCBoHfJ6cwggGcBgwrBgEEAY9bAoJ4AQMEggGKMIIBhgIBADCB/AIBAgYNKwYBBAGPWwKCeAIBAzCB5wYOKwYBBAGPWwKCeAIBAwEEgdR7ImFwcElk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3w5JMPOrDAKBggqhkjOPQQDAwNpADBmAjEA2zDQREvORPqcZyjwKDltu0T9zN8Cd3/hi4DQZvuRJdJOY57yIIO/LKxezzEcGiMMAjEAkX7r0U4Mcaw4uURMh+7tLMyvyxnlW8yJqBEOnZfqS8I8t0bQIY2r/5TQAPC0JhBm\n-----END CERTIFICATE-----"};
// Root CA certificate downloaded from the HarmonyOS official website.
static String g_rootCertStr = "-----BEGIN CERTIFICATE-----\n" +
"MIICCTCCAY6gAwIBAgIDVxAsMAoGCCqGSM49BAMDMEsxEzARBgNVBAoTCkh1YXdl\n" +
"aSBDQkcxNDAyBgNVBAMTK0h1YXdlaSBDQkcgRUNDIERldmljZSBBdHRlc3RhdGlv\n" +
"biBSb290IENBIDEwIBcNMjMxMTMwMDIwNjU1WhgPMjA3MzExMzAwMjA2NTVaMEsx\n" +
"EzARBgNVBAoTCkh1YXdlaSBDQkcxNDAyBgNVBAMTK0h1YXdlaSBDQkcgRUNDIERl\n" +
"dmljZSBBdHRlc3RhdGlvbiBSb290IENBIDEwdjAQBgcqhkjOPQIBBgUrgQQAIgNi\n" +
"AATDJzRdruaBeMoQBbdqCe51ezvkQn3OPYBoRmpL5KPktdFtD0b97FRp8jGLiUhP\n" +
"Kyo8M15fxW5Ams4s80E8I1BSXoovDnkKllFfUadD8URgwEfOk5qttYNKzJcULavO\n" +
"hbijQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW\n" +
"BBTaRGLD5yvof1E6XEuPQ3w5JMPOrDAKBggqhkjOPQQDAwNpADBmAjEA2zDQREvO\n" +
"RPqcZyjwKDltu0T9zN8Cd3/hi4DQZvuRJdJOY57yIIO/LKxezzEcGiMMAjEAkX7r\n" +
"0U4Mcaw4uURMh+7tLMyvyxnlW8yJqBEOnZfqS8I8t0bQIY2r/5TQAPC0JhBm\n" +
"-----END CERTIFICATE-----\n";
// Name of the file for storing the application public key generated by the HarmonyOS HAP application.
static String g_publicKeyFileName = "d:\\attestPublicKey.pem";
public static void main(String[] args) {
ParseAttestation parseAttestation = new ParseAttestation();
parseAttestation.parseAndValidateAttestCertChain(g_attestCertStr, g_rootCertStr, g_publicKeyFileName);
}
void parseAndValidateAttestCertChain(String[] attestCertStr, String rootCertStr, String publicKeyFileName) {
try {
// Parse the key attestation certificate chain.
List<X509Certificate> attestCerts = parseAttestationCerts(attestCertStr);
// Verify the key attestation certificate chain.
Date curDate = new Date();
validateAttestationCertChain(attestCerts, rootCertStr, curDate);
// Parse the key attestation certificate.
AttestationInfo attestInfo = extractAttestaionField(attestCerts.get(0));
// Check whether the key attestation information is correct.
if (!checkAttestInfo(attestInfo)) {
// TODO: Handle the exception.
}
// Save the application public key generated by the HarmonyOS HAP application.
saveAttestPublicKey(attestInfo.publicKey, publicKeyFileName);
} catch (Exception e) {
System.out.println(e);
}
}
List<X509Certificate> parseAttestationCerts(String[] certStr) throws Exception {
List<X509Certificate> certificateList = new ArrayList<>(certStr.length);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC");
for (int i = 0; i < certStr.length; i++) {
certificateList.add((X509Certificate) certificateFactory.generateCertificate(
new ByteArrayInputStream(certStr[i].getBytes())));
}
return certificateList;
}
void validateAttestationCertChain(List<X509Certificate> certs, String trustCAStr, Date date) throws Exception {
// Construct a certificate chain.
CertificateFactory factory = CertificateFactory.getInstance("X.509", "BC");
CertPath certPath = factory.generateCertPath(certs);
// Read the trust root certificate and construct a trustAnchor object.
X509Certificate trustCA = (X509Certificate) factory.generateCertificate(
new ByteArrayInputStream(trustCAStr.getBytes()));
TrustAnchor trustAnchor = new TrustAnchor(trustCA, null);
HashSet trustAnchorSet = new HashSet<TrustAnchor>();
trustAnchorSet.add(trustAnchor);
// Construct the validator and corresponding parameters.
PKIXParameters params = new PKIXParameters(trustAnchorSet);
params.setDate(date);
// The key attestation certificate has a short validity period and does not require certificate revocation verification.
params.setRevocationEnabled(false);
CertPathValidator validator = CertPathValidator.getInstance("PKIX", "BC");
try {
PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult) validator.validate(certPath, params);
System.out.println("Cert Chain validate success!");
} catch (Exception e) {
System.out.println("Cert Chain validate fail!" + e.getMessage());
}
}
int getInteger(ASN1Encodable value) {
if (value instanceof ASN1Integer) {
return ((ASN1Integer) value).getValue().intValue();
} else if (value instanceof ASN1Enumerated) {
return ((ASN1Enumerated) value).getValue().intValue();
} else {
throw new IllegalArgumentException(
"expected Integer value ; found " + value.getClass().getName() + " instead.");
}
}
byte[] getOctetString(ASN1Encodable value) {
if (value instanceof ASN1OctetString) {
return ((ASN1OctetString) value).getOctets();
} else {
throw new RuntimeException(
"expected OctetString value ; found " + value.getClass().getName() + " instead.");
}
}
void printBytes(byte[] byteArray) {
if (byteArray == null) {
System.out.println("null");
}
for (int i = 0; i < byteArray.length; i++) {
System.out.printf("%02X ", byteArray[i]);
}
System.out.println();
}
AttestationInfo extractAttestaionField(X509Certificate certificate) {
AttestationInfo attestInfo = new AttestationInfo();
// Obtain the application public key.
attestInfo.publicKey = certificate.getPublicKey();
// Obtain the Extensions field from the key attestation certificate.
byte[] attestationValue = certificate.getExtensionValue("1.3.6.1.4.1.2011.2.376.1.3");
if (attestationValue == null || attestationValue.length == 0) {
throw new IllegalArgumentException("Can't found the attestation extension!");
}
ASN1Sequence attestSequence = ASN1Sequence.getInstance(
ASN1OctetString.getInstance(attestationValue).getOctets());
// Obtain the attestation version.
attestInfo.version = getInteger(attestSequence.getObjectAt(0));
for (int i = 1; i < attestSequence.size(); i++) {
ASN1Sequence attestClaim = ASN1Sequence.getInstance(attestSequence.getObjectAt(i));
// Obtain the claim OID.
ASN1ObjectIdentifier attestClaimOid = (ASN1ObjectIdentifier) attestClaim.getObjectAt(1);
if ("1.3.6.1.4.1.2011.2.376.2.1.4".equalsIgnoreCase(attestClaimOid.getId())) {
// Read the challenge.
attestInfo.challenge = getOctetString(attestClaim.getObjectAt(2));
} else if ("1.3.6.1.4.1.2011.2.376.2.1.3".equalsIgnoreCase(attestClaimOid.getId())) {
// Read appInfo.
ASN1Sequence appInfoAsn1 = (ASN1Sequence) attestClaim.getObjectAt(2);
// Obtain the appInfo OID.
ASN1ObjectIdentifier appidOid = (ASN1ObjectIdentifier) appInfoAsn1.getObjectAt(0);
if (!"1.3.6.1.4.1.2011.2.376.2.1.3.1".equalsIgnoreCase(appidOid.getId())) {
continue;
}
// Read the HAP application information.
String appInfo = new String(getOctetString(appInfoAsn1.getObjectAt(1)));
System.out.println("appInfo is:\n" + appInfo);
attestInfo.appInfo = JSON.parseObject(appInfo, AppInfo.class);
} else if ("1.3.6.1.4.1.2011.2.376.2.2.2.6".equalsIgnoreCase(attestClaimOid.getId())) {
// Read the key management component ID. The value must be 0x28c4fb4944afec11b9090242ac120002.
attestInfo.keyManagerId = getOctetString(attestClaim.getObjectAt(2));
} else if ("1.3.6.1.4.1.2011.2.376.2.2.4.8".equalsIgnoreCase(attestClaimOid.getId())) {
// Read the product model.
attestInfo.model = attestClaim.getObjectAt(2).toString();
}
}
return attestInfo;
}
boolean checkAttestInfo(AttestationInfo attestInfo) {
// TODO: Verify the challenge.
System.out.println("challenge is:");
printBytes(attestInfo.challenge);
// TODO: Verify the information in appInfo.
System.out.println("appInfo.appId is:\n" + attestInfo.appInfo.appId);
System.out.println("appInfo.bundleName is:\n" + attestInfo.appInfo.bundleName);
System.out.println("appInfo.appIdentifier is:\n" + attestInfo.appInfo.appIdentifier);
System.out.println("appInfo.appMode is:\n" + attestInfo.appInfo.appMode);
// TODO: Verify keyManagerId. The value must be 0x28c4fb4944afec11b9090242ac120002.
System.out.println("key manager id is:");
printBytes(attestInfo.keyManagerId);
System.out.println("device model is:");
System.out.println(attestInfo.model);
return true;
}
void saveAttestPublicKey(PublicKey publicKey, String publicKeyFileName) throws Exception {
// TODO: Save the application public key obtained by attestInfo.publicKey.getEncoded() to the server.
FileOutputStream file = new FileOutputStream(publicKeyFileName);
file.write(publicKey.getEncoded());
file.close();
System.out.println("the app public key: \n" + publicKey);
}
class AttestationInfo {
public PublicKey publicKey;
public int version;
public byte[] challenge;
public AppInfo appInfo;
public byte[] keyManagerId;
public String model;
}
static class AppInfo {
public String appId;
public String bundleName;
public String appIdentifier;
public String appMode;
}
} After verifying the key attestation certificate chain, the application server saves the application public key in the certificate (see the sample code for verifying the key attestation certificate chain), so as to verify subsequent service requests. Before saving the key, ensure that it is unique.
Implementation suggestion: In scenarios such as password-free login and payment, generate a unique key pair for each login user in the application and associate the user with the public key on the application server.
Implementation note: Before verifying the service request signature, you need to find the application public key. You are advised to generate a unique ID (for example, calculate the hash) for the application public key and store the mapping between the key and its ID so the public key can be quickly retrieved using its ID.
The application server returns the application public key ID to the application, which then stores the application ID.