CRL Development
This topic walks you through on how to create a certificate revocation list (CRL) object, obtain CRL information, check whether a certificate has been revoked, and print the revocation date if the certificate has been revoked.
How to Develop
-
Import the certFramework and cryptoFramework modules.
import { cert } from '@kit.DeviceCertificateKit'; import { cryptoFramework } from '@kit.CryptoArchitectureKit'; -
Use cert.createX509CRL to create an X.509 CRL object.
-
Obtain CRL information.
The following example shows how to obtain the CRL version and type. For more information, see X509CRL.
-
Create a PublicKey object.
For details, see convertKey.
-
Use X509CRL.verify to verify the signature.
-
Use cert.createX509Cert to create an X509Cert object based on the existing X.509 certificate data.
-
Use X509CRL.isRevoked to check whether the X.509 certificate has been revoked.
-
Use X509CRL.getRevokedCert to obtain the revoked certificate.
-
Use X509CRLEntry.getRevocationDate to obtain the date when the certificate was revoked.
import { cert } from '@kit.DeviceCertificateKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { util } from '@kit.ArkTS';
// CRL data example.
let crlData = '-----BEGIN X509 CRL-----\n' +
'MIHzMF4CAQMwDQYJKoZIhvcNAQEEBQAwFTETMBEGA1UEAxMKQ1JMIGlzc3VlchcN\n' +
'MTcwODA3MTExOTU1WhcNMzIxMjE0MDA1MzIwWjAVMBMCAgPoFw0zMjEyMTQwMDUz\n' +
'MjBaMA0GCSqGSIb3DQEBBAUAA4GBACEPHhlaCTWA42ykeaOyR0SGQIHIOUR3gcDH\n' +
'J1LaNwiL+gDxI9rMQmlhsUGJmPIPdRs9uYyI+f854lsWYisD2PUEpn3DbEvzwYeQ\n' +
'5SqQoPDoM+YfZZa23hoTLsu52toXobP74sf/9K501p/+8hm4ROMLBoRT86GQKY6g\n' +
'eavsH0Q3\n' +
'-----END X509 CRL-----\n'
let certData = '-----BEGIN CERTIFICATE-----\n' +
'MIIBLzCB1QIUO/QDVJwZLIpeJyPjyTvE43xvE5cwCgYIKoZIzj0EAwIwGjEYMBYG\n' +
'A1UEAwwPRXhhbXBsZSBSb290IENBMB4XDTIzMDkwNDExMjAxOVoXDTI2MDUzMDEx\n' +
'MjAxOVowGjEYMBYGA1UEAwwPRXhhbXBsZSBSb290IENBMFkwEwYHKoZIzj0CAQYI\n' +
'KoZIzj0DAQcDQgAEHjG74yMIueO7z3T+dyuEIrhxTg2fqgeNB3SGfsIXlsiUfLTa\n' +
'tUsU0i/sePnrKglj2H8Abbx9PK0tsW/VgqwDIDAKBggqhkjOPQQDAgNJADBGAiEA\n' +
'0ce/fvA4tckNZeB865aOApKXKlBjiRlaiuq5mEEqvNACIQDPD9WyC21MXqPBuRUf\n' +
'BetUokslUfjT6+s/X4ByaxycAA==\n' +
'-----END CERTIFICATE-----\n';
let pubKeyData = new Uint8Array([
0x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
0x05, 0x00, 0x03, 0x81, 0x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xDC, 0x4C, 0x2D,
0x57, 0x49, 0x3D, 0x42, 0x52, 0x1A, 0x09, 0xED, 0x3E, 0x90, 0x29, 0x51, 0xF7, 0x70, 0x15, 0xFE,
0x76, 0xB0, 0xDB, 0xDF, 0xA1, 0x2C, 0x6C, 0x67, 0x95, 0xDA, 0x63, 0x3D, 0x4F, 0x71, 0x48, 0x8C,
0x3E, 0xFA, 0x24, 0x79, 0xE9, 0xF2, 0xF2, 0x20, 0xCB, 0xF1, 0x59, 0x6B, 0xED, 0xC8, 0x72, 0x66,
0x6E, 0x31, 0xD4, 0xF3, 0xCE, 0x0B, 0x12, 0xC4, 0x17, 0x39, 0xB4, 0x52, 0x16, 0xD3, 0xE3, 0xC0,
0xF8, 0x48, 0xB3, 0xF6, 0x40, 0xD5, 0x47, 0x23, 0x30, 0x7F, 0xA7, 0xC5, 0x5A, 0x5A, 0xBB, 0x5C,
0x7B, 0xEF, 0x69, 0xE2, 0x74, 0x35, 0x24, 0x22, 0x25, 0x45, 0x7E, 0xFC, 0xE8, 0xC4, 0x52, 0x65,
0xA0, 0x4E, 0xBC, 0xFD, 0x3F, 0xD9, 0x85, 0x14, 0x8A, 0x5A, 0x93, 0x02, 0x24, 0x6C, 0x19, 0xBA,
0x81, 0xBE, 0x65, 0x2E, 0xCB, 0xBB, 0xE9, 0x91, 0x7B, 0x7C, 0x47, 0xC2, 0x61, 0x02, 0x03, 0x01,
0x00, 0x01
]);
// CRL example.
function crlSample(): void {
let textEncoder = new util.TextEncoder();
let encodingBlob: cert.EncodingBlob = {
// Convert the CRL data from a string to a Unit8Array.
data: textEncoder.encodeInto(crlData),
// CRL format. Only the PEM and DER formats are supported. In this example, the CRL is in PEM format.
encodingFormat: cert.EncodingFormat.FORMAT_PEM
};
// Create an X509CRL object.
cert.createX509CRL(encodingBlob, (err, x509Crl) => {
if (err != null) {
// The X509CRL object fails to be created.
console.error(`createX509Crl failed, errCode: ${err.code}, errMsg:${err.message} `);
return;
}
// The X509CRL object is successfully created.
console.log('createX509CRL success');
// Obtain the CRL version
let version = x509Crl.getVersion();
let revokedType = x509Crl.getType();
console.log(`X509 CRL version: ${version}, type :${revokedType}`);
// Pass in the public key binary data to convertKey() of @ohos.security.cryptoFramework to obtain a public key object.
try {
let keyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_3');
console.log('createAsyKeyGenerator success');
let pubEncodingBlob: cryptoFramework.DataBlob = {
data: pubKeyData,
};
keyGenerator.convertKey(pubEncodingBlob, null, (e, keyPair) => {
if (e == null) {
console.log('convert key success');
x509Crl.verify(keyPair.pubKey, (err, data) => {
if (err == null) {
// Signature verification is successful.
console.log('verify success');
} else {
// Signature verification fails.
console.error(`verify failed, errCode: ${err.code}, errMsg: ${err.message}`);
}
});
} else {
console.error(`convert key failed, message: ${e.message}, code: ${e.code} `);
}
})
} catch (error) {
let e: BusinessError = error as BusinessError;
console.error(`get pubKey failed, errCode: ${e.code}, errMsg: ${e.message}` );
}
// Use createX509Cert() of certFramework to create an X509Cert object.
let certBlob: cert.EncodingBlob = {
data: textEncoder.encodeInto(certData),
encodingFormat: cert.EncodingFormat.FORMAT_PEM
};
let revokedFlag = true;
let serial:bigint = BigInt('0');
cert.createX509Cert(certBlob, (err, cert) => {
serial = cert.getCertSerialNumber();
if (err == null) {
try {
// Check whether the certificate has been revoked.
revokedFlag = x509Crl.isRevoked(cert);
console.log(`revokedFlag is: ${revokedFlag}`);
} catch (error) {
let e: BusinessError = error as BusinessError;
console.error(`isRevoked failed, errCode: ${e.code}, errMsg:${e.message}`);
}
} else {
console.error(`create x509 cert failed, errCode: ${err.code}, errMsg: ${err.message}`);
}
})
if (!revokedFlag) {
console.log('the given cert is not revoked.');
return;
}
// Obtain the revoked certificate based on the serial number.
try {
let crlEntry = x509Crl.getRevokedCert(serial);
console.log('get getRevokedCert success');
let serialNumber = crlEntry.getSerialNumber();
console.log(`crlEntry serialNumber is: ${serialNumber}`);
// Obtain the revocation date of the certificate.
let date = crlEntry.getRevocationDate();
console.log(`revocation date is: ${date}`);
} catch (error) {
let e: BusinessError = error as BusinessError;
console.error(`getRevokedCert failed, errCode: ${e.code}, errMsg: ${e.message}`);
}
});
}