Downloading certificates | ssl-certificate-go | Spatie       

 SPATIE  

ssl-certificate-go
==================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Ssl-certificate-go](https://spatie.be/docs/ssl-certificate-go/v1)  Downloading certificates

 Version   v1      

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

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

 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. [ Using a hashed CA directory ](#content-using-a-hashed-ca-directory)
4. [ Downloading the full chain ](#content-downloading-the-full-chain)
5. [ Timeouts and SNI ](#content-timeouts-and-sni)
6. [ Socket context options ](#content-socket-context-options)
7. [ Errors ](#content-errors)

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

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

```
downloader := sslcertificate.NewDownloader()
downloader.VerifyPeer = false
downloader.VerifyPeerName = false
certificate, err := downloader.ForHost(ctx, "expired.badssl.com")
```

Only do this to inspect invalid certificates, never to authenticate a server. `VerifyPeer` controls chain, date and server-auth checks. `VerifyPeerName` controls host name checks independently. Disabling either weakens authentication. TLS handshake signatures are still checked by Go.

Always start with `NewDownloader()`. A zero-value downloader is not configured and returns a port or timeout error. Do not construct one with a struct literal and accidentally leave verification 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.

```
downloader := sslcertificate.NewDownloader()
downloader.CAFile = "/path/to/cacert.pem"
certificate, err := downloader.ForHost(ctx, "spatie.be")
```

This replaces the system roots. An unreadable bundle, a file without usable PEM certificates or an empty combined store fails instead of disabling verification. Go does not fetch missing intermediates or check OCSP/CRL revocation here.

Using a hashed CA directory
-----------------------------------------------------------------------------------------------------------------------------------------

Set `CAPath` to a directory prepared with `openssl rehash /path/to/certs` or `c_rehash`. PEM files named `<8 hex digits>.<number>`, such as `a3418fda.0`, are loaded eagerly. Symlinks are supported. Other entries are ignored.

```
downloader := sslcertificate.NewDownloader()
downloader.CAPath = "/path/to/certs"
certificate, err := downloader.ForHost(ctx, "spatie.be")
```

You can combine `CAFile` and `CAPath`. Both sources are trusted, replacing system roots. Set either field to `""` to clear it. An unreadable matching file or directory returns an error. If neither is configured, the system roots apply.

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

Set `FullChain = true` and call `GetCertificates(ctx, host)` for the server-sent chain, leaf first and without duplicates. Without this option only the leaf is returned. `ForHost` always returns the leaf. The result is not a chain assembled from the local trust store. Servers normally omit the root.

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

`Timeout` is a `time.Duration`, defaults to 30 seconds and must be positive. The context and timeout cover DNS, TCP connection and TLS handshake. A shorter context deadline wins. An injected dialer must honor cancellation.

SNI is enabled by default. `SNI = false` turns it off without disabling host name verification. `IPAddress` changes the connection target, while the original host controls SNI and verification. IPv6 addresses are supplied without brackets. An explicit URL port takes precedence over `Port`.

This package sends no HTTP request and follows no redirects. PHP's `setFollowLocation` therefore has no Go equivalent.

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

Use a `net.Dialer` for binding, keep-alive and socket controls instead of PHP's socket option array. You can replace the entire dial function.

```
dialer := &net.Dialer{
    LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0")},
}
downloader := sslcertificate.NewDownloader()
downloader.DialContext = dialer.DialContext
certificate, err := downloader.ForHost(ctx, "spatie.be")
```

`TLSConfig` accepts a `*tls.Config` for protocol versions, cipher settings, client certificates, root pools and custom verification hooks. It is cloned per call. The downloader manages `ServerName` and `InsecureSkipVerify` to implement independent name and chain checks. Setting those fields on `TLSConfig` does not override the downloader's verification switches. `CAFile` and `CAPath`, when set, override `TLSConfig.RootCAs`.

Go's `VerifyPeerCertificate` hook receives nil verified chains because standard verification is performed by the downloader's `VerifyConnection` hook. Prefer `VerifyConnection` for checks that must follow the selected trust policy.

Configure the downloader, its TLS config and dialer before use. They can be reused concurrently but must not be mutated during calls. Go's supported TLS algorithms and protocol versions apply, even when certificate verification is disabled.

Errors
--------------------------------------------------------------------------

Use `errors.Is` with `ErrInvalidURL`, `ErrInvalidIPAddress` or `ErrNoCertificate`. File, network and TLS errors are wrapped with `%w`, so `errors.Is` and `errors.As`retain access to `context.Canceled`, `context.DeadlineExceeded`, `*net.DNSError`and X.509 verification errors.

 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)
