Sub topic 12
Adding TLS and Channel Binding
Assume you know basic pg_hba.conf records, PostgreSQL roles, and CIDR notation. This chapter focuses on protecting remote password authentication with TLS and channel binding.
TLS protects the connection
Transport Layer Security (TLS) encrypts data between a PostgreSQL client and server. Without TLS, a network observer may read queries, results, and other connection data. TLS also lets the client verify that it is talking to the intended server.
PostgreSQL must have SSL enabled, with a server certificate and private key configured:
# postgresql.conf
ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'The private key must be readable by the PostgreSQL server but not by ordinary users. After changing postgresql.conf, reload or restart PostgreSQL as required by the setting.
A hostssl rule accepts only TLS connections. A hostnossl rule accepts only unencrypted TCP connections. A normal host rule can match either kind.
| Rule type | Connection allowed |
|---|---|
| `hostssl` | TLS only |
| `hostnossl` | Non-TLS only |
| `host` | TLS or non-TLS |
For example, this rule requires TLS for the application subnet:
hostssl appdb app_user 10.0.0.0/24 scram-sha-256A later hostnossl rule could explicitly reject unencrypted access:
hostnossl appdb app_user 10.0.0.0/24 rejectBecause PostgreSQL uses the first matching rule, place the specific rules before broad rules.
Verify the server certificate
Encryption alone does not prove the server's identity. A man-in-the-middle attacker could create another encrypted endpoint and forward traffic. The client must validate the server certificate.
With libpq, sslmode=verify-full checks that:
- The certificate is signed by a trusted certificate authority.
- The certificate name matches the
hostvalue. - The connection uses TLS.
The host should be a DNS name included in the certificate's subject alternative names. The client needs a trusted CA certificate, commonly supplied with sslrootcert.
psql "host=db.example.com dbname=appdb user=app_user sslmode=verify-full sslrootcert=/etc/ssl/certs/company-ca.crt"Avoid sslmode=require when server identity matters. It encrypts the connection but does not fully verify the server certificate.
Channel binding strengthens SCRAM
SCRAM-SHA-256 proves knowledge of the password without sending the password itself. Channel binding ties that SCRAM exchange to the particular TLS connection. If an attacker tries to relay the authentication exchange through a different TLS connection, the channel-binding data will not match.
Require channel binding in a client connection with channel_binding=require:
psql "host=db.example.com dbname=appdb user=app_user sslmode=verify-full channel_binding=require"This option requires a TLS connection and a SCRAM authentication method that supports channel binding. A role whose password is still stored as an MD5 verifier cannot complete SCRAM channel binding. Reset that role's password after setting password_encryption to scram-sha-256.
Choose the protection level
Use hostssl for remote connections that carry credentials or sensitive data. Use verify-full for production clients. Add channel_binding=require when all clients and drivers support it. Test older drivers before enforcing it globally, because unsupported clients will fail rather than silently downgrade.
Exercise
A client uses sslmode=verify-full but omits channel_binding=require. Is the connection encrypted, and is channel binding required?
Show answer
The connection is encrypted and the server certificate is verified, but channel binding is not required. Add channel_binding=require to enforce it.