跳到主要内容

Go x509: certificate signed by unknown authority — Causes, Fixes, and Debugging Commands

What “x509: certificate signed by unknown authority” means in Go

Go’s crypto/x509 package builds a certificate chain from the server’s leaf certificate up to a root CA in the trust store. If it cannot find a trusted root, it returns x509: certificate signed by unknown authority. In Go 1.20+ you’ll often see it wrapped as tls: failed to verify certificate: x509: certificate signed by unknown authority.

Go does not ship a browser-style CA bundle and does not read browser trust stores. On Linux it reads the OS CA bundle (for example /etc/ssl/certs/ca-certificates.crt) and honors SSL_CERT_FILE / SSL_CERT_DIR. That is why the same code works on your laptop and fails in a minimal container.

The three causes in practice are: (1) the container image has no ca-certificates package, (2) the service uses a private CA that is not in the trust store, or (3) the server sends an incomplete chain missing an intermediate certificate.

First five minutes: reproduce and inspect the chain

Start with OpenSSL from the same network path as the failing Go program:

openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts </dev/null

Look for Verify return code: 0 (ok) and count the certificates. A healthy public chain normally prints at least two BEGIN CERTIFICATE blocks: the leaf and one intermediate. If you see only one, the server is not sending the intermediate.

openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts </dev/null 2>/dev/null | grep -c 'BEGIN CERTIFICATE'

To see what Go actually receives, use this temporary diagnostic program. Never ship InsecureSkipVerify in production:

package main

import (
    "crypto/tls"
    "fmt"
    "net/http"
)

func main() {
    tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
    client := &http.Client{Transport: tr}
    resp, err := client.Get("https://api.example.com")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    for i, cert := range resp.TLS.PeerCertificates {
        fmt.Println(i, "Subject:", cert.Subject)
        fmt.Println(i, "Issuer:", cert.Issuer)
        fmt.Println(i, "NotAfter:", cert.NotAfter)
    }
}

If the issuer of the leaf is not present in the printed list, the server chain is incomplete. If the chain is complete but the root is private, jump to the private CA section.

Cause 1: container images without ca-certificates

Minimal images such as alpine, scratch, and some distroless variants do not include a CA bundle. Go will fail every HTTPS request to a public CA until you add one.

Alpine:

FROM alpine:3.20
RUN apk add --no-cache ca-certificates && update-ca-certificates

Debian or Ubuntu:

RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*

For scratch or distroless, copy the bundle from the build stage:

FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app

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

Verify inside the running container:

ls -l /etc/ssl/certs/ca-certificates.crt
apk info ca-certificates   # Alpine
dpkg -l ca-certificates    # Debian/Ubuntu

If the file is missing, that is your root cause. Rebuild the image; do not mount a CA file from your laptop as a long-term fix.

Cause 2: private or internal CA not in the trust store

If the server certificate is issued by an internal CA, Go has no reason to trust it. You have two supported options: point Go at a PEM file with the private root, or load the root into a CertPool in code.

Option A — environment variable. This is the fastest fix for a container or a one-off process:

SSL_CERT_FILE=/etc/ssl/certs/internal-root.pem ./myapp

SSL_CERT_FILE replaces the default bundle file list on Linux. If you also need public roots, concatenate them into one bundle:

cat /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/internal-root.pem > /etc/ssl/certs/combined.pem
SSL_CERT_FILE=/etc/ssl/certs/combined.pem ./myapp

For a directory of hashed certificates, use SSL_CERT_DIR:

mkdir -p /etc/ssl/internal-certs
cp internal-root.pem /etc/ssl/internal-certs/
c_rehash /etc/ssl/internal-certs
SSL_CERT_DIR=/etc/ssl/internal-certs ./myapp

Option B — code. Load the private root explicitly. Check the boolean return value of AppendCertsFromPEM; ignoring it is a common source of silent failures:

pool := x509.NewCertPool()
pemBytes, err := os.ReadFile("/etc/ssl/certs/internal-root.pem")
if err != nil {
    log.Fatal(err)
}
if !pool.AppendCertsFromPEM(pemBytes) {
    log.Fatal("failed to parse CA certificate")
}
client := &http.Client{
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{RootCAs: pool},
    },
}

To keep the system roots and add the private root, start from x509.SystemCertPool() instead of x509.NewCertPool():

pool, err := x509.SystemCertPool()
if err != nil {
    log.Fatal(err)
}
pool.AppendCertsFromPEM(pemBytes)

Mount the PEM as a Kubernetes Secret or ConfigMap and set SSL_CERT_FILE in the Deployment. That keeps the CA out of the image and lets you rotate it without a rebuild.

Cause 3: server sends an incomplete certificate chain

Trusting the root is not enough if the server omits the intermediate. Go can only build a chain from the certificates the server sends plus the roots it trusts. The server should send the leaf plus all intermediates; it should not send the root.

Check the count again:

openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts </dev/null 2>/dev/null | grep -c 'BEGIN CERTIFICATE'

If the count is 1, fix the server. For nginx, use fullchain.pem, not cert.pem:

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

For Apache:

SSLCertificateFile      /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

For a Go HTTPS server, load a PEM file that contains the leaf followed by the intermediate:

cert, err := tls.LoadX509KeyPair("fullchain.pem", "privkey.pem")
if err != nil {
    log.Fatal(err)
}
srv := &http.Server{
    Addr:      ":443",
    TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}},
}

After changing the server, re-run the OpenSSL command. You want Verify return code: 0 (ok) and at least two certificates.

Comparison table: which fix applies to your case

Symptom or context Root cause Fix Scope Effort
Works on laptop, fails in Alpine/scratch container No CA bundle in image apk add ca-certificates or copy ca-certificates.crt Image Low
Fails only against an internal hostname Private CA not trusted SSL_CERT_FILE or RootCAs pool App or Deployment Low
Fails for every client, browsers show a chain warning Server omits intermediate Serve fullchain.pem Server Low
Started after a certificate rotation Old root or intermediate in bundle Refresh bundle or pin new root Image or Secret Low
certificate is valid for X, not Y Hostname or SAN mismatch Reissue cert with correct SAN or fix URL Server or client config Medium
certificate has expired Expired leaf or intermediate Renew certificate; check clock Server Low
certificate relies on legacy Common Name field No SAN extension Reissue with SANs Server Medium
Works with curl but not Go in the same container curl honors CURL_CA_BUNDLE; Go does not Set SSL_CERT_FILE for Go App Low

Troubleshooting: real error messages and what to do

Get "https://api.example.com": tls: failed to verify certificate: x509: certificate signed by unknown authority
This is the standard chain failure. Run openssl s_client -showcerts, count certificates, and confirm the root is in the bundle used by the Go process.

x509: certificate signed by unknown authority (possibly because of "x509: ECDSA verification failure" while trying to verify candidate authority certificate "CN=Internal Root CA")
You have a CA with the same subject name but a different key, usually an old root left in the bundle. Remove the stale root and keep only the current one.

x509: certificate has expired or is not yet valid: current time 2024-06-01T12:00:00Z is after 2024-05-01T00:00:00Z
Renew the certificate. If the dates look wrong, check the container clock and the host’s NTP configuration.

x509: certificate is valid for *.example.com, not api.internal
The certificate does not cover the hostname you dialed. Connect to a name in the SAN list, or reissue the certificate.

x509: cannot validate certificate for 10.0.0.5 because it doesn't contain any IP SANs
You are using an IP address instead of a DNS name. Add an IP SAN to the certificate or switch the client to the DNS name.

x509: certificate relies on legacy Common Name field, use SANs instead
Modern Go rejects certificates that only have a Common Name. Reissue with a Subject Alternative Name extension.

x509: "api.example.com" certificate is not standards compliant
The certificate is malformed, often due to an empty or invalid SAN. Reissue it from your CA with a standard CSR.

Useful verification commands when you have the PEM files locally:

openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt -untrusted intermediate.pem leaf.pem
openssl x509 -in leaf.pem -noout -text | grep -A1 'Subject Alternative Name'

For a quick environment check inside the container:

env | grep -E 'SSL_CERT_FILE|SSL_CERT_DIR'
ls -l /etc/ssl/certs/ca-certificates.crt
./app 2>&1 | head -n 5

FAQ

Does Go use the system trust store or its own CA bundle?
On Linux, Go reads the OS CA bundle; it does not ship a browser-style root store. On Windows and macOS it uses the platform verifier when available. In containers, that means you must install or copy ca-certificates.

Why does curl work but my Go program fails?
curl honors CURL_CA_BUNDLE and may have a different default path. Go honors SSL_CERT_FILE and SSL_CERT_DIR on Linux. Set SSL_CERT_FILE for the Go process or load the root in code.

Does SSL_CERT_FILE add to the system roots or replace them?
On Linux it replaces the default bundle file list. If you need both public and private roots, concatenate them into one PEM and point SSL_CERT_FILE at that file.

Can I just set InsecureSkipVerify: true?
It disables verification and hostname checks, so it is only acceptable for temporary debugging. For production, trust the private CA or fix the server chain.

Where does Go look for CA certificates on Linux?
Common paths include /etc/ssl/certs/ca-certificates.crt, /etc/pki/tls/certs/ca-bundle.crt, and /etc/ssl/ca-bundle.pem. The exact list depends on the Go version and distribution. SSL_CERT_FILE overrides the file path; SSL_CERT_DIR overrides the directory.

How do I rotate a private CA without rebuilding the image?
Mount the PEM as a Secret or ConfigMap, set SSL_CERT_FILE to that path, and restart the pod. Keep the file name stable so the Deployment does not need changes.

Why does it work in Kubernetes on one node but not another?
The nodes may run different base images or have different CA bundles. Pin the CA bundle in your image or mount it explicitly; do not depend on the node’s trust store.