Working with Certificate-Based Authentication

1. Understanding Public Key Infrastructure

ComponentRole
CA (Certificate Authority)Issues and signs certificates
RA (Registration Authority)Validates requestor identity
SubscriberHolder of the certificate
Relying PartyVerifies certificates
CRL / OCSPRevocation distribution
Trust anchorRoot CA in trust store

2. Using X.509 Digital Certificates

FieldPurpose
SubjectIdentity (CN, O, OU)
IssuerSigning CA
Serial NumberUnique per CA
ValidityNotBefore / NotAfter
Public KeyRSA / ECDSA / EdDSA
SANSubject Alternative Names (DNS, IP, URI)
EKUExtended Key Usage (clientAuth, serverAuth)
SignatureCA's signature over TBSCertificate

3. Implementing Client Certificate Authentication

Example: Nginx client cert

server {
  listen 443 ssl;
  ssl_certificate     server.crt;
  ssl_certificate_key server.key;
  ssl_client_certificate ca-bundle.crt;
  ssl_verify_client on;
  ssl_verify_depth 2;

  location /api {
    proxy_set_header X-Client-DN $ssl_client_s_dn;
    proxy_pass http://upstream;
  }
}

4. Implementing Mutual TLS (mTLS)

Client ─── ClientHello ──→ Server
Client ←── ServerHello + Cert + CertReq ── Server
Client ─── ClientCert + ClientKeyExchange + CertVerify ──→ Server
Both ←── Finished ──→
      
Use CasePattern
Service meshIstio, Linkerd auto-issue per-pod certs
B2B APIPartner-issued client certs
IoTDevice identity cert per unit
FAPI bankingmTLS-bound OAuth tokens

5. Generating Certificate Signing Requests

Example: OpenSSL CSR

openssl ecparam -name prime256v1 -genkey -out client.key
openssl req -new -key client.key -out client.csr \
  -subj "/CN=alice@acme.com/O=Acme/C=US" \
  -addext "subjectAltName=DNS:alice.acme.com,email:alice@acme.com" \
  -addext "extendedKeyUsage=clientAuth"

6. Validating Certificate Chains

CheckDetail
Chain buildCert → Intermediate(s) → Root in trust store
SignatureEach cert signed by parent's private key
Validity datesAll certs within NotBefore/NotAfter
Path lengthBasic Constraints respected
Name constraintsSAN matches expected name
EKUclientAuth present
RevocationCRL or OCSP not revoked

7. Checking Certificate Revocation

MethodDetail
CRLPeriodic blob of revoked serials — large, stale
OCSPPer-cert query to responder — privacy issue
OCSP StaplingServer attaches signed status — preferred
CRLite / CRLSetsBrowser-pushed compressed lists
Short-lived certsRotation < revocation window (no CRL needed)

8. Setting Certificate Expiration

Cert TypeLifetime (2026)
Public TLS≤398 days (CA/B Forum), trending to 90 days
Internal mTLS1–24 hours (SPIFFE)
Code signing1–3 years
Root CA10–25 years

9. Implementing Certificate Pinning

Example: OkHttp pinning

CertificatePinner pinner = new CertificatePinner.Builder()
  .add("api.example.com",
       "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
       "sha256/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=") // backup
  .build();
new OkHttpClient.Builder().certificatePinner(pinner).build();
Warning: Pinning without a backup pin can brick the app if the cert rotates. Always pin to public key (SPKI) and include a backup.

10. Using Smart Cards for Authentication

StandardUse
PIV (FIPS 201)US Federal CAC/PIV cards
PKCS#11Cross-platform crypto token API
PKCS#15On-card data structure
CCIDUSB smart-card reader protocol
AuthPIN unlocks private key → signs challenge