Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Go: x509 certificate signed by unknown authority

Go cannot build a chain to a trusted root: no CA bundle in the image, a private CA, or a missing intermediate. Install roots or set RootCAs.

~5 min readGo
#Go#TLS#Docker#Security
A Go client crate refused at a cyan TLS gate, an amber root certificate card and a broken chain beside it

Introduction & Quick Summary

text
Get "https://api.internal.example/v1/health": tls: failed to verify certificate: x509: certificate signed by unknown authority

Go verified the server's certificate chain against its root pool and found no root that signs it. The connection is refused, not downgraded. Either the machine has no CA bundle (very common in containers), or the certificate really is signed by a private or self-signed authority that Go does not trust.

The cause is in one of three layers:

  • The runtime has no trusted roots. FROM scratch and some distroless images ship without /etc/ssl/certs/ca-certificates.crt.
  • The server uses a private CA. An internal service, a test environment, or a corporate TLS-inspecting proxy.
  • The chain is incomplete or wrong. The server sends a leaf certificate without its intermediate, so no path to a root can be built.

Diagnose from outside Go first, because it distinguishes the layers in one command:

bash
openssl s_client -connect api.internal.example:443 -servername api.internal.example \ -showcerts </dev/null 2>/dev/null | openssl x509 -noout -issuer -subject -dates
ObservationMeaningFix
unable to get local issuer certificateMissing intermediateFix the server chain
Chain shows a self-signed issuerPrivate CAAdd its root to the pool
Same binary works on the host, fails in DockerNo CA bundle in imageInstall or copy certificates
Works with curl, fails in GoGo's root pool differsCheck SSL_CERT_FILE, image contents
Fails only behind the office networkTLS inspectionTrust the proxy root

Core Mechanics, Signatures & Reproducible Setup

crypto/tls calls crypto/x509's verifier with a VerifyOptions carrying Roots. When Roots is nil, Go loads the system pool once per process: /etc/ssl/certs/ca-certificates.crt, /etc/pki/tls/certs/ca-bundle.crt, /etc/ssl/cert.pem and the hashed directory /etc/ssl/certs on Unix-like systems; the OS store on Windows and macOS. A missing file is not an error at startup — the pool is simply empty, and every public HTTPS request fails with this message.

Reproduce it with a throwaway self-signed certificate:

bash
openssl req -x509 -newkey rsa:2048 -nodes -days 1 \ -keyout key.pem -out cert.pem -subj "/CN=localhost" openssl s_server -accept 8443 -cert cert.pem -key key.pem -www &
go
resp, err := http.Get("https://localhost:8443") // Get "https://localhost:8443": tls: failed to verify certificate: // x509: certificate signed by unknown authority

The correct fix for a known private CA:

go
pool, err := x509.SystemCertPool() if err != nil { pool = x509.NewCertPool() } pem, err := os.ReadFile("/etc/certs/internal-root.pem") if err != nil { return nil, fmt.Errorf("read CA: %w", err) } if !pool.AppendCertsFromPEM(pem) { return nil, errors.New("no certificates found in CA file") } client := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}, }, Timeout: 10 * time.Second, }

AppendCertsFromPEM returning false is the quiet failure to watch for: the call succeeded, the pool is unchanged, and the error stays identical.

Key Comparison Table

FixScopeCorrect for
Install ca-certificates in imageWhole containerMissing bundle
SSL_CERT_FILE=/path/ca.pemProcess and childrenInjected CA without rebuilding
SSL_CERT_DIR=/etc/ssl/certsProcessHashed directory layouts
RootCAs on tls.ConfigThat client onlyApp-specific private CA
x509.SetFallbackRootsWhole Go programCustom fallback pool
InsecureSkipVerify: trueThat client, no verificationAlmost never
Stack layoutDiagnostic commandExpected result
Alpineapk info ca-certificatesPackage installed
Debian slimls -l /etc/ssl/certs/ca-certificates.crtFile present
Distroless / scratchdocker run --rm img ls /etc/ssl/certsMay be empty
Any imageopenssl s_client -connect ...Full chain with root

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Alpine and Debian images

dockerfile
FROM golang:1.25-alpine AS build WORKDIR /src COPY . . RUN CGO_ENABLED=0 go build -o /out/api ./cmd/api FROM alpine:3.21 RUN apk add --no-cache ca-certificates COPY --from=build /out/api /usr/local/bin/api ENTRYPOINT ["/usr/local/bin/api"]

CGO_ENABLED=0 makes the binary use Go's own resolver and certificate loading, which reads the bundle installed above. Without the apk add line, the image has the binary and no roots.

Case 2: scratch and distroless

dockerfile
FROM gcr.io/distroless/static-debian12:nonroot # includes ca-certificates COPY --from=build /out/api /api ENTRYPOINT ["/api"]

For a true scratch image, copy the bundle from the builder:

dockerfile
FROM scratch COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=build /out/api /api ENTRYPOINT ["/api"]

Verify inside the running container rather than trusting the Dockerfile:

docker run --rm --entrypoint sh myimage -c 'ls -l /etc/ssl/certs/ca-certificates.crt'

Case 3: A corporate proxy that re-signs traffic

The proxy returns certificates signed by its own root, which no public bundle contains. Export the root once and mount it, then point the process at it:

# kubernetes deployment env: - name: SSL_CERT_FILE value: /etc/corp/ca-bundle.pem volumeMounts: - name: corp-ca mountPath: /etc/corp readOnly: true volumes: - name: corp-ca configMap: name: corp-ca

SSL_CERT_FILE replaces the system bundle path for Go's loader; if you need both public roots and the proxy root, concatenate them into the mounted file. Setting it to a file with only the proxy root breaks connections to public sites, which is a common self-inflicted second incident.

Case 4: A server that omits its intermediate

bash
openssl s_client -connect api.example.com:443 -servername api.example.com \ -verify_return_error </dev/null 2>&1 | grep -E 'verify error|Verify return code' # Verify return code: 21 (unable to verify the first certificate)

The client cannot build a path to a trusted root even with a complete local bundle. Fix the server: send the leaf and intermediate in that order (fullchain.pem in nginx ssl_certificate), not just the leaf. Adding the intermediate to your Go client's pool also works and multiplies the problem across every client that talks to that host.

Case 5: Per-client pool without touching globals

go
func newClient(caPath string) (*http.Client, error) { rootCAs, err := x509.SystemCertPool() if err != nil { rootCAs = x509.NewCertPool() } pem, err := os.ReadFile(caPath) if err != nil { return nil, err } if ok := rootCAs.AppendCertsFromPEM(pem); !ok { return nil, fmt.Errorf("%s: no PEM certificates", caPath) } return &http.Client{ Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: rootCAs}}, Timeout: 15 * time.Second, }, nil }

One client for the internal service, the default client for everything else. Building a single global pool for the whole program couples unrelated dependencies to one certificate file.

Case 6: Pinning a certificate instead of trusting a CA

When you control both ends and want to reject everything else:

go
tlsConfig := &tls.Config{ InsecureSkipVerify: true, // verification done manually below VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error { want, _ := hex.DecodeString("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08") got := sha256.Sum256(rawCerts[0]) if !hmac.Equal(got[:], want) { return fmt.Errorf("certificate fingerprint mismatch: %x", got) } return nil }, }

Pinning survives a compromised CA but breaks on every certificate rotation, so it belongs on machine-to-machine links with a controlled rotation process — not on third-party APIs.

Common Compilation/Runtime Mistakes & Version Gotchas

  • InsecureSkipVerify: true as a fix. It disables hostname and chain verification, turning a TLS error into a silent man-in-the-middle vulnerability. If it appears in a code review, require the RootCAs alternative.
  • Trusting the leaf as a root. RootCAs must contain the issuing CA certificate, not the server certificate; adding the leaf produces x509: certificate signed by unknown authority still.
  • AppendCertsFromPEM return ignored. A PEM with only a private key, or with Windows line endings and a stray header, appends nothing and returns false.
  • SSL_CERT_FILE set to a directory. Go expects a file; a directory path silently yields an empty pool.
  • Time skew. An expired or not-yet-valid certificate reports as x509: certificate has expired or is not yet valid, but a badly skewed clock in a VM can look like an authority problem in aggregated logs.
  • Go 1.20+ macOS/Windows verifiers. On those platforms Go uses the system verifier, so a certificate trusted in Keychain works without a bundle; the same binary in Linux CI fails, which is why container tests matter.
  • MinVersion and legacy endpoints. Some internal services still offer TLS 1.0; raising MinVersion produces a handshake error, not a verification error, and is the correct trade.

Inspect a certificate bundle by decoding it with the base64 tool after stripping the PEM headers, and check what the endpoint actually presents with the curl builder using --insecure once to see the chain it advertises.

Primary references: crypto/x509 documents CertPool, SystemCertPool and the loading rules, and crypto/tls specifies RootCAs, VerifyPeerCertificate and MinVersion.

Official references

primary documentation

The Go documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Go guides

More in Go

All Go guides

The guides filed next to this one in Go, in library order.