Implementing TLS and mTLS

1. Generating TLS Certificates

Example: Self-signed dev certs

openssl req -x509 -newkey rsa:2048 -nodes -days 365 \
  -subj "/CN=localhost" -keyout server.key -out server.crt
For ProductionUse
Internal PKIcert-manager + private CA
PublicLet's Encrypt via ACME
Service meshIstio/Linkerd auto-mTLS

2. Loading Server Certificates

Example: Load cert pair

cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
if err != nil { log.Fatal(err) }

3. Creating Server TLS Config

Example: Server with TLS

tlsCfg := &tls.Config{
    Certificates: []tls.Certificate{cert},
    MinVersion:   tls.VersionTLS13,
}
srv := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsCfg)))
FieldPurpose
MinVersionTLS 1.3 recommended (2026)
ClientCAsCA pool for verifying clients (mTLS)
ClientAuthmTLS mode

4. Creating Client TLS Config

Example: Client TLS

creds := credentials.NewTLS(&tls.Config{
    ServerName: "users.internal",
    MinVersion: tls.VersionTLS13,
})
conn, _ := grpc.NewClient(addr, grpc.WithTransportCredentials(creds))

5. Using System Certificate Pool

APIDetail
x509.SystemCertPool()OS root CAs
DefaultUsed when RootCAs nil

6. Adding Custom CA Certificates

Example: Append custom CA

pool, _ := x509.SystemCertPool()
caPEM, _ := os.ReadFile("ca.crt")
pool.AppendCertsFromPEM(caPEM)
tlsCfg := &tls.Config{RootCAs: pool}

7. Setting Server Name

FieldUse
ServerNameSNI + hostname verification
MismatchTLS handshake fails
InsecureSkipVerify=trueDev only — disables verification

8. Enabling Client Certificate Verification

ClientAuth ValueBehavior
NoClientCertDon't request
RequestClientCertRequest, don't require
RequireAnyClientCertAny cert
VerifyClientCertIfGivenVerify if provided
RequireAndVerifyClientCertStrict mTLS

9. Implementing Mutual TLS

Example: Strict mTLS server

caPEM, _ := os.ReadFile("ca.crt")
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(caPEM)
tlsCfg := &tls.Config{
    Certificates: []tls.Certificate{cert},
    ClientCAs:    pool,
    ClientAuth:   tls.RequireAndVerifyClientCert,
    MinVersion:   tls.VersionTLS13,
}
Client extract identityMethod
In interceptorpeer.FromContext(ctx)creds.TLSInfo
CN / SANRead from VerifiedChains[0][0]

10. Using Let's Encrypt Certificates

ToolUse
certbotIssue + renew
autocert (Go)Embedded ACME — only for HTTPS, not raw gRPC
cert-manager (K8s)ACME + secret rotation