Sub topic 12
Adding TLS and channel binding
This chapter assumes you can read a pg_hba.conf record, know how first-match ordering works, and understand that SCRAM-SHA-256 is the password method you want. It also assumes you can edit postgresql.conf and restart a server. Here you add TLS (Transport Layer Security) on top of SCRAM, use hostssl rules to require it, and use channel binding to tie the SCRAM login to the TLS session.
Why SCRAM alone is not enough
SCRAM never sends the password over the network. That protects the credential, but it does not protect anything else. Without TLS, the queries, the result rows and the session itself travel in plain text. Anyone on the network path can read them.
TLS provides two things SCRAM does not:
- Encryption: the traffic between client and server cannot be read in transit.
- Server identity: the client can check a certificate to confirm that it reached the real server.
Server identity matters for SCRAM too. A man-in-the-middle can sit between the client and the server and relay the SCRAM messages in both directions. Both sides think they are talking to each other, and the attacker sees everything. Channel binding closes this gap, as the later sections show.
Enabling TLS on the server
The server needs a certificate and a private key. Set these in postgresql.conf:
ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
ssl_min_protocol_version = 'TLSv1.2'Relative paths are resolved against the data directory. The key file must not be readable by group or others. PostgreSQL refuses to start if the permissions are too loose, with an error saying the private key file has group or world access. A key owned by the postgres user with mode 0600 works.
Changing ssl_cert_file or ssl_key_file takes effect on reload in current PostgreSQL releases. Turning ssl from off to on for the first time is safest with a restart.
You can check which connections actually use TLS from SQL:
SELECT pid, ssl, version, cipher
FROM pg_stat_ssl
WHERE ssl;The pg_stat_ssl view has one row per connection process. The ssl column is true when that connection is encrypted.
Requiring TLS with hostssl rules
Enabling TLS on the server only makes it available. A client that does not ask for TLS can still connect over plain TCP, unless pg_hba.conf forbids it.
The connection types control this:
| Type | Matches |
|---|---|
| `host` | TCP/IP connections, with or without TLS |
| `hostssl` | TCP/IP connections that use TLS |
| `hostnossl` | TCP/IP connections that do not use TLS |
To require TLS for remote clients, accept them only through hostssl and reject the plain-text path before anything else can match it:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
hostnossl all all 0.0.0.0/0 reject
hostnossl all all ::/0 rejectThe hostnossl ... reject lines are what make TLS mandatory. A plain-text client from 10.0.0.5 fails to match the hostssl rule, reaches the reject rule and is refused. Because the first match wins, the reject lines must come after the rules you want to allow and before any broader host rule that would accept plain-text connections.
If you used the single host all all 0.0.0.0/0 reject line from earlier chapters, the two hostnossl lines above replace it only for the plain-text case. Keep a final catch-all reject for TLS clients that match no allowed rule.
Remember that pg_hba.conf changes need a reload and that you should validate them first, as covered in the chapter on pg_hba_file_rules.
SELECT pg_reload_conf();
SELECT line_number, type, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;Any non-null error value means that line was not parsed and does not apply.
The client side: sslmode
The server's rules only describe what it will accept. The client decides how strict to be about the server. In libpq, the sslmode parameter controls this:
| sslmode | Encrypts | Verifies certificate chain | Verifies host name |
|---|---|---|---|
| `disable` | No | No | No |
| `prefer` (default) | If the server offers it | No | No |
| `require` | Yes | No | No |
| `verify-ca` | Yes | Yes | No |
| `verify-full` | Yes | Yes | Yes |
The default, prefer, is the weak point. It silently falls back to plain text, and an attacker who can interfere with the connection can force that fallback. Even require only guarantees encryption. It does not check who is on the other end, so a man-in-the-middle can present its own certificate and still be accepted.
Use verify-full for any connection that crosses a network you do not control. It checks that the certificate was signed by a trusted certificate authority (CA) and that the host name you typed matches the certificate. The client needs the CA certificate, which libpq reads from ~/.postgresql/root.crt by default or from the sslrootcert parameter.
How channel binding works
Channel binding adds a value from the TLS session to the SCRAM exchange. PostgreSQL uses the tls-server-end-point type. The value is a hash of the server's certificate.
The client computes the hash of the certificate it saw during the TLS handshake. It includes that hash in its SCRAM proof, so the server can recompute the proof with its own certificate hash. If the two hashes differ, the proof is wrong and authentication fails.
Consider a man-in-the-middle with a different certificate. The client's TLS session ends at the attacker, so the client binds to the attacker's certificate hash. The attacker then relays the SCRAM messages to the real server over a separate TLS session. The server expects a hash of its own certificate, and the values do not match. Authentication fails, even though the attacker relayed valid password proofs.
The method name in the protocol is SCRAM-SHA-256-PLUS when channel binding is in use. You do not name it in pg_hba.conf. The scram-sha-256 method supports both variants, and the client decides whether to use the -PLUS form.
Requiring channel binding on the client
Channel binding is enforced by the client, with the channel_binding connection parameter. It has three values:
disable: never use channel binding.prefer(default): use it if both sides support it, otherwise continue without.require: fail the connection unless channel binding is used.
The prefer default has the same weakness as sslmode=prefer. An attacker can make the server appear not to support channel binding, and the client continues without it. To get the protection, set require:
psql "host=db.example.com dbname=appdb user=app_user sslmode=verify-full channel_binding=require"With channel_binding=require, the connection fails unless all of these hold:
- The connection uses TLS.
- The server authenticates the client with SCRAM-SHA-256.
- The server supports the
-PLUSvariant, which needs an SSL library that provides it.
Because of point 2, this fails against roles that still hold MD5 hashes, and against trust, password or cert rules, since none of them perform a SCRAM exchange. That makes the setting a useful check. If it connects, you know SCRAM and TLS were both used.
Verifying what a session actually used
After connecting, confirm the result instead of assuming it. In psql, \conninfo reports whether the connection is encrypted. From SQL, query your own backend:
SELECT ssl, version, cipher
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();This shows TLS, but it does not show whether channel binding was used. Channel binding is not recorded in pg_stat_ssl. The practical evidence is that a connection with channel_binding=require succeeded.
Common problems
SSL is not enabled on the server: the client usedsslmode=requireor stricter, butssl = offon the server or the build has no TLS support. Setssl = onand restart.no pg_hba.conf entry for host ..., no encryption: the client connected without TLS and onlyhostsslrules matched its address. This is the requirement working as designed. Usesslmode=requireor stricter on the client.certificate verify failed: the client does not trust the CA that signed the server certificate. Supply the CA file withsslrootcert.server certificate for "x" does not match host name "y": withverify-full, the name you connect to must appear in the certificate's subject alternative names. Connect using that name, or reissue the certificate.channel binding required, but server authenticated client without channel binding: the matching rule used a method other than SCRAM, or the role has an MD5 password. Check the matching line inpg_hba_file_rulesand the role's stored hash.channel binding is required, but server did not offer an authentication method that supports channel binding: the server connection is not using TLS, or the client or server library does not supporttls-server-end-point.
Summary of a complete setup
- Enable
sslwith a certificate whose name matches what clients connect to. - Store all passwords as SCRAM verifiers.
- Use
hostsslrules withscram-sha-256for remote access and ahostnossl ... rejectrule for plain text. - Connect with
sslmode=verify-fullandchannel_binding=require. - Reload, then check
pg_hba_file_rulesfor errors.
Exercise
A client connects with sslmode=require channel_binding=require to a server that has a valid hostssl ... scram-sha-256 rule. The role's password is stored as an MD5 hash. Does the connection succeed, and what additional gap remains even if the role is fixed?
Show answer
It fails. The server cannot run a SCRAM exchange against an MD5 hash, so there is no SCRAM-SHA-256-PLUS to bind, and channel_binding=require makes the client refuse. After the role gets a SCRAM verifier, the connection works, but sslmode=require does not check the server's certificate. Channel binding then binds the session to whatever certificate the client was shown, so a man-in-the-middle presenting its own certificate to the client is not blocked by the TLS layer. Use sslmode=verify-full so the client verifies the certificate chain and host name.
Exercise
You want remote clients on 10.0.0.0/24 to reach appdb only over TLS. Write the two pg_hba.conf lines, in order, and explain why the order matters.
Show answer
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
hostnossl all all 0.0.0.0/0 reject
The first match wins. TLS connections from the subnet match the first line and are authenticated. Plain-text connections do not match hostssl, so they fall to the second line and are rejected. If a broader host rule appeared before them, it would accept plain-text connections first.