Downloading certificates | 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)  Downloading certificates

 Version   v1      

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

  Downloading certificates    
- [ 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)

 Downloading certificates
========================

###  On this page 

1. [ Downloading invalid certificates ](#content-downloading-invalid-certificates)
2. [ Using your own CA bundle ](#content-using-your-own-ca-bundle)
3. [ Downloading the full chain ](#content-downloading-the-full-chain)
4. [ Timeouts and SNI ](#content-timeouts-and-sni)
5. [ Socket context options ](#content-socket-context-options)

Downloading invalid certificates
--------------------------------------------------------------------------------------------------------------------------------------------------------

If you want to download certificates even if they are invalid (for example, if they are expired), you can turn off verification explicitly:

```
let certificate = Downloader::download_certificate_from_url(
    "expired.badssl.com",
    Duration::from_secs(timeout_in_seconds),
    false,
)?;
```

This disables chain and peer name verification. Only do this to inspect an invalid certificate, never to authenticate a server.

The fluent downloader lets you disable chain verification with `with_verify_peer(false)` and peer name verification with `with_verify_peer_name(false)` independently. Disabling either weakens authentication. Handshake signatures are always checked by rustls, even when both checks are disabled.

Using your own CA bundle
--------------------------------------------------------------------------------------------------------------------------------

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)?;
```

This replaces the system roots. An unreadable, empty or malformed CA bundle fails instead of silently disabling verification. The downloader does not fetch missing intermediates or check revocation through OCSP or CRLs.

Downloading the full chain
--------------------------------------------------------------------------------------------------------------------------------------

Use `with_full_chain(true).get_certificates(host_name)` to get all certificates sent by the server. The leaf comes first and duplicate certificates are removed. Without that option, only the leaf is returned. `for_host()` always returns the leaf.

The result contains the chain the server sent, not the local trust store. Servers normally omit the root certificate.

Timeouts and SNI
--------------------------------------------------------------------------------------------------------

`set_timeout(Duration::from_secs(30))` sets the timeout for TCP connection attempts and the TLS handshake together. The default is 30 seconds. A zero timeout is rejected. DNS uses the operating system's synchronous resolver and is not covered by this timeout.

SNI is enabled by default. `using_sni(false)` turns it off without disabling hostname verification. `from_ip_address()` changes the connection target, but the original host name is still used for SNI and verification. An explicit port in the URL takes precedence over `using_port()`.

This package only performs a TLS handshake. It sends no HTTP request and never follows redirects, so there is no equivalent of PHP's ineffective `setFollowLocation` option.

Socket context options
--------------------------------------------------------------------------------------------------------------------------

`with_socket_context_options()` accepts `SocketOptions`, not a PHP context array. Set `bind_address` for a local bind address and port, `ipv6_only` for `IPV6_V6ONLY`, `tcp_nodelay` for TCP\_NODELAY or `reuse_address` for SO\_REUSEADDR. The bind address family must match the connection target.

rustls negotiates TLS 1.2 or 1.3 with modern cipher suites. A server that only supports obsolete TLS versions or algorithms cannot be inspected through this downloader, even with verification disabled. You can still parse its certificate from a PEM file or DER bytes.

 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)
