The class provided by this package makes it incredibly easy to query the properties on an SSL certificate. We have three options for fetching a certificate. Here's an example:
# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let path_to_certificate_file = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/spatieCertificate.pem");
# let certificate_data = std::fs::read_to_string(path_to_certificate_file)?;
use spatie_ssl_certificate::SslCertificate;
// fetch the certificate using a URL
let certificate = SslCertificate::create_for_host_name("spatie.be")?;
// or from a certificate file
let certificate = SslCertificate::create_from_file(path_to_certificate_file)?;
// or from a string
let certificate = SslCertificate::create_from_string(&certificate_data)?;
certificate.is_valid(None); // returns true if the certificate is currently valid
certificate.expiration_date(); // returns an OffsetDateTime
certificate.valid_from_date(); // returns an OffsetDateTime
certificate.days_until_expiration_date(); // returns the days between today and expiration_date
certificate.lifespan_in_days(); // returns the days between valid_from_date and expiration_date
certificate.issuer(); // returns "Let's Encrypt Authority X3"
certificate.organization(); // returns the organization name when available
certificate.subject_organization(); // returns the subject organization name when available
certificate.public_key_algorithm(); // returns the public key algorithm
certificate.public_key_size(); // returns the public key size
certificate.signature_algorithm(); // returns the signature algorithm
# Ok(())
# }This is the Rust version of spatie/ssl-certificate. It uses rustls for TLS and x509-parser for certificate metadata.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Requires Rust 1.89 or newer. Add the crate to your Cargo.toml:
[dependencies]
spatie-ssl-certificate = "1"You can create an instance of SslCertificate with this named constructor:
# use spatie_ssl_certificate::SslCertificate;
# fn main() -> Result<(), spatie_ssl_certificate::Error> {
let certificate = SslCertificate::create_for_host_name("spatie.be")?;
# Ok(())
# }See the usage guide for ports, IP addresses, certificate properties and validity checks, and downloading certificates for chain downloads and TLS options. The v1 documentation follows the PHP package's examples.
Downloads verify the certificate chain and host name against the system CA bundle by default. is_valid checks dates and optional domain coverage only; it does not establish trust for a certificate loaded from a file or string.
cargo test --locked --all-targets --all-features
cargo test --locked --doc --all-featuresSee all quality checks. The README and Markdown guides are tested by rustdoc, including setup on hidden # lines.
Tests use local TLS servers and certificate fixtures. They do not depend on public hosts being reachable. Network examples compile without contacting those hosts.
See CHANGELOG.md.
See CONTRIBUTING.md and the Rust API Guidelines.
Using an AI coding agent? Load Apollo's rust-best-practices skill when writing or reviewing code in this package.
Report vulnerabilities privately using the instructions in SECURITY.md.
The MIT License (MIT). See LICENSE.md.
