36 releases

0.16.0 Feb 29, 2024
0.15.1 Aug 3, 2023
0.15.0 Mar 15, 2023
0.14.0 Jun 30, 2022
0.1.0 Jan 20, 2018

#8 in Cryptography

Download history 321832/week @ 2024-07-30 334479/week @ 2024-08-06 330072/week @ 2024-08-13 330240/week @ 2024-08-20 316177/week @ 2024-08-27 348887/week @ 2024-09-03 319880/week @ 2024-09-10 306268/week @ 2024-09-17 330827/week @ 2024-09-24 359894/week @ 2024-10-01 345234/week @ 2024-10-08 383276/week @ 2024-10-15 383313/week @ 2024-10-22 359911/week @ 2024-10-29 366336/week @ 2024-11-05 323803/week @ 2024-11-12

1,508,343 downloads per month
Used in 1,189 crates (159 directly)

MIT/Apache

255KB
4.5K SLoC

License: MIT Apache License 2.0 docs.rs crates.io Download numbers Github CI Minimum rustc version

X.509 Parser

A X.509 v3 (RFC5280) parser, implemented with the nom parser combinator framework.

It is written in pure Rust, fast, and makes extensive use of zero-copy. A lot of care is taken to ensure security and safety of this crate, including design (recursion limit, defensive programming), tests, and fuzzing. It also aims to be panic-free.

The code is available on Github and is part of the Rusticata project.

Certificates are usually encoded in two main formats: PEM (usually the most common format) or DER. A PEM-encoded certificate is a container, storing a DER object. See the pem module for more documentation.

To decode a DER-encoded certificate, the main parsing method is X509Certificate::from_der ( part of the FromDer trait ), which builds a X509Certificate object.

An alternative method is to use X509CertificateParser, which allows specifying parsing options (for example, not automatically parsing option contents).

The returned objects for parsers follow the definitions of the RFC. This means that accessing fields is done by accessing struct members recursively. Some helper functions are provided, for example X509Certificate::issuer() returns the same as accessing <object>.tbs_certificate.issuer.

For PEM-encoded certificates, use the pem module.

Examples

Parsing a certificate in DER format:

use x509_parser::prelude::*;

static IGCA_DER: &[u8] = include_bytes!("../assets/IGC_A.der");

let res = X509Certificate::from_der(IGCA_DER);
match res {
    Ok((rem, cert)) => {
        assert!(rem.is_empty());
        //
        assert_eq!(cert.version(), X509Version::V3);
    },
    _ => panic!("x509 parsing failed: {:?}", res),
}

To parse a CRL and print information about revoked certificates:

#
#
let res = CertificateRevocationList::from_der(DER);
match res {
    Ok((_rem, crl)) => {
        for revoked in crl.iter_revoked_certificates() {
            println!("Revoked certificate serial: {}", revoked.raw_serial_as_string());
            println!("  Reason: {}", revoked.reason_code().unwrap_or_default().1);
        }
    },
    _ => panic!("CRL parsing failed: {:?}", res),
}

See also examples/print-cert.rs.

Features

/// Cryptographic signature verification: returns true if certificate was signed by issuer
#[cfg(feature = "verify")]
pub fn check_signature(cert: &X509Certificate<'_>, issuer: &X509Certificate<'_>) -> bool {
    let issuer_public_key = issuer.public_key();
    cert
        .verify_signature(Some(issuer_public_key))
        .is_ok()
}
  • The validate features add methods to run more validation functions on the certificate structure and values using the Validate trait. It does not validate any cryptographic parameter (see verify above).

Rust version requirements

x509-parser requires Rustc version 1.63 or greater, based on der-parser dependencies and for proc-macro attributes support.

Note that due to breaking changes in the time crate, a specific version of this crate must be specified for compiler versions <= 1.63: cargo update -p time --precise 0.3.20

Changes

See CHANGELOG.md

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3–12MB
~156K SLoC