Sample code for 30+ languages & platforms
Rust

Load .crl, Convert to XML, Get Revoked Serial Numbers and Dates

See more Certificates Examples

Load a binary .crl file (Certificate Revocation List), converts to XML, and then gets the revoked certificate serial numbers and revocation dates.

Note: This example requires Chilkat v9.5.0.77 or greater.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Load a binary .crl file.
let bd_crl = chilkat::BinData::new();
if bd_crl.load_file("qa_data/crl/ca1.crl").is_err() {
    println!("Failed to load CRL file.");
    return;
}

let asn = chilkat::Asn::new();
if asn.load_bd(&bd_crl).is_err() {
    println!("{}", asn.last_error_text());
    return;
}

// Convert ASN.1 to XML and load into xml and re-emit for pretty printing..
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&asn.asn_to_xml().unwrap_or_default());
let _ = xml.save_xml("qa_output/crl.xml").is_ok();

// Use this online tool to generate parsing code from CRL XML: 
// Generate Parsing Code from XML

// Here's code to parse the XML.  This code was generated by the above tool,
// and then we modified it by removing unneeded code and changing some names
let mut i: i32 = 0;
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut k: i32 = 0;
let mut count_k: i32 = 0;

let mut revoked_cert_serial_hex = String::new();
let mut date_revoked = String::new();

let dt = chilkat::DateTime::new();

i = 0;
let count_i = xml.num_children_having_tag("sequence");
while i < count_i {
    xml.set_i(i);

    j = 0;
    count_j = xml.num_children_having_tag("sequence[i]|sequence");
    while j < count_j {
        xml.set_j(j);

        k = 0;
        count_k = xml.num_children_having_tag("sequence[i]|sequence[j]|sequence");
        while k < count_k {
            xml.set_k(k);

            // Get the revoked certificate's serial number in uppercase hex.
            revoked_cert_serial_hex = xml.get_child_content("sequence[i]|sequence[j]|sequence[k]|int").unwrap_or_default();
            println!("serial number: {}", revoked_cert_serial_hex);

            // Get the date/time revoked.  It will be a string formatted as "YYMMDDhhmmssZ", such as "181023093028Z"
            date_revoked = xml.get_child_content("sequence[i]|sequence[j]|sequence[k]|utctime").unwrap_or_default();

            // Starting in Chilkat v9.5.0.77, date/time strings formatted as YYMMDDhhmmssZ can be parsed as follows:
            let _ = dt.set_from_timestamp(&date_revoked);
            println!("date revoked: {}", dt.get_as_rfc822(false).unwrap_or_default());

            k = k + 1;
        }

        j = j + 1;
    }

    i = i + 1;
}