Usage | ssl-certificate-rust | Spatie       

 SPATIE  

ssl-certificate-rust
====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Ssl-certificate-rust](https://spatie.be/docs/ssl-certificate-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/ssl-certificate-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/ssl-certificate-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/ssl-certificate-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/ssl-certificate-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/ssl-certificate-rust/v1/usage)
- [ Downloading certificates ](https://spatie.be/docs/ssl-certificate-rust/v1/downloading-certificates)
- [ Testing ](https://spatie.be/docs/ssl-certificate-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/ssl-certificate-rust/v1/changelog)

 Usage
=====

You can create an instance of `SslCertificate` with this named constructor:

```
let certificate = SslCertificate::create_for_host_name("spatie.be")?;
```

You can create an instance of `SslCertificate` passing the port with this named constructor:

```
let certificate = SslCertificate::create_for_host_name("spatie.be:443")?;
```

You can use this fluent style to specify a specific port to connect to.

```
SslCertificate::download()
    .using_port(custom_port)
    .for_host(host_name)?;
```

You can check the certificate on a different IP address using the same style.

```
SslCertificate::download()
    .from_ip_address(ip_address)?
    .for_host(host_name)?;
```

This also works with IPv6 addresses.

```
SslCertificate::download()
    .from_ip_address("2a00:1450:4001:80e::200e")?
    .for_host("google.com")?;
```

You can specify socket context options using the typed `SocketOptions` struct.

```
SslCertificate::download()
    .with_socket_context_options(SocketOptions {
        bind_address: Some("0.0.0.0:0".parse()?),
        ..SocketOptions::default()
    })
    .for_host(host_name)?;
```

By default the peer is verified against the CA bundle of your system. You can verify it against your own bundle of trusted root certificates instead.

```
SslCertificate::download()
    .with_ca_file("/path/to/cacert.pem")
    .for_host(host_name)?;
```

If the given `ip_address` is invalid, `Error::InvalidIpAddress` is returned.

If the given `host_name` is invalid, `Error::InvalidUrl` is returned.

If the given `host_name` is valid but there was a problem downloading the certificate, an `Error` describes the DNS, I/O or TLS failure. No certificate is represented by `Error::NoCertificate`, not a boolean.

### Getting the issuer name

```
certificate.issuer(); // returns "Let's Encrypt Authority X3"
```

### Getting the domain name

Returns the primary domain name for the certificate.

```
certificate.domain(); // returns "spatie.be"
```

### Getting the certificate's signing algorithm

Returns the algorithm used for signing the certificate.

```
certificate.signature_algorithm(); // returns "RSA-SHA256"
```

### Getting the certificate's organization

Returns the organization belonging to the certificate.

```
certificate.organization(); // returns "Spatie BVBA"
```

### Getting the subject's organization

Returns the organization of the certificate's subject.

```
certificate.subject_organization(); // returns "Spatie BVBA"
```

These getters return an empty string when the certificate has no corresponding common name or organization.

### Getting the additional domain names

A certificate can cover multiple (sub)domains. Here's how to get them.

```
certificate.additional_domains(); // returns ["spatie.be", "www.spatie.be"]
```

A domain name returned with this method can start with `*`, meaning it is valid for single-label subdomains of that domain. IP alternative names retain the `IP Address:` prefix. `domains()` combines the alternative names with the primary domain and removes duplicates.

### Getting the fingerprint

```
certificate.fingerprint(); // returns a fingerprint for the certificate
```

This is the lowercase SHA-1 digest of the DER certificate, without separators. Use SHA-256 for security-sensitive comparisons.

### Getting the SHA256 fingerprint

```
certificate.fingerprint_sha256(); // returns a SHA256 fingerprint for the certificate
```

### Getting the date when the certificate becomes valid

```
certificate.valid_from_date(); // returns an OffsetDateTime
```

### Getting the expiration date

```
certificate.expiration_date(); // returns an OffsetDateTime
```

Dates use UTC. `days_until_expiration_date()` returns signed whole days, truncating toward zero. `lifespan_in_days()` returns the whole days between the validity dates.

### Determining if the certificate is still valid

Returns true if the current date and time is between `valid_from_date` and `expiration_date`, including the boundaries.

```
certificate.is_valid(None); // returns a boolean
```

You also use this method to determine if a given domain is covered by the certificate. Of course it'll keep checking if the current date and time is between `valid_from_date` and `expiration_date`.

```
certificate.is_valid(Some("spatie.be")); // returns true
certificate.is_valid(Some("laravel.com")); // returns false
```

This is a metadata check, not a check of the certificate's chain of trust. Downloads verify trust separately. `applies_to_url()` checks domain coverage without checking dates. It accepts URLs, bare host names and IP addresses. It checks the primary domain as well as alternative names, just like the PHP package. rustls uses stricter WebPKI rules for the actual TLS handshake.

### Determining if the certificate is still valid until a given date

Returns true if the certificate is valid and if the `expiration_date` is after the given date.

```
certificate.is_valid_until(OffsetDateTime::now_utc() + Duration::days(7), None); // returns a boolean
```

For deterministic date checks, use `is_valid_at(now, url)`, `is_expired_at(now)` and `days_until_expiration_date_at(now)` instead of changing a global clock.

### Determining if the certificate is expired

```
certificate.is_expired(); // returns a boolean if expired
```

### Convert the certificate to an array

You can convert a certificate to a `CertificateProperties` struct using the `to_array` method.

```
let certificate_properties = certificate.to_array();
```

The properties can be used to create a new instance of the certificate.

```
SslCertificate::create_from_array(certificate_properties)?;
```

`CertificateProperties` stores DER bytes and the optional peer socket address. Both the properties and `SslCertificate` implement Serde serialization. Deserialization parses the DER again. These are Rust-native properties, not PHP's array or serialized object format.

### Other certificate properties

`serial_number()` returns the serial number as a decimal string. `public_key_algorithm()` and `public_key_size()` return the public key algorithm and size in bits.

`raw_certificate_fields()` exposes typed metadata, with distinguished-name attributes keyed by dotted OIDs and every extension's original DER value. `raw_certificate_fields_json()` and the certificate's `Display` implementation return that metadata as JSON. `hash()` is an MD5 identifier of this JSON, not a security digest. Its value differs from PHP's OpenSSL-specific metadata hash; use a fingerprint to compare the same certificate across languages.

`contains_domain()` checks literal domain membership, including subdomains of an entry. It is case-sensitive and treats `*` literally. Use `applies_to_url()` for case-insensitive wildcard and URL matching.

`is_self_signed()` compares the issuer and subject common names, just like the PHP package. It is a heuristic, not signature or trust verification. `uses_sha1_hash()` detects SHA-1 certificate signatures. `is_pre_certificate()` checks for the CT poison extension.

`remote_address()` returns the peer's `SocketAddr` when downloaded, or `None` when loaded locally. `as_der()` and `to_pem()` expose the original certificate. `SslCertificate::der2pem()` wraps DER bytes in a PEM envelope.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
