Re: Setting min/max TLS protocol in clientside libpq

Michael Paquier <[email protected]> Sat, 11 Jan 2020 11:49:32 +0900
Newsgroups gmane.comp.db.postgresql.devel.general
Message-ID <[email protected]>
On Fri, Jan 10, 2020 at 12:01:36AM +0100, Daniel Gustafsson wrote:
> I looked into this and it turns out that OpenSSL does nothing to prevent the
> caller from setting a nonsensical protocol range like min=tlsv1.3,max=tlsv1.1.
> Thus, it's quite easy to screw up the backend server config and get it to start
> properly, but with quite unrelated error messages as a result on connection.

FWIW, here is the error produced, and that's confusing:
$ psql -d "host=localhost sslmode=require"
psql: error: could not connect to server: SSL error: tlsv1 alert
internal error

> Since I think this needs to be dealt with for both backend and frontend (if
> this is accepted), I removed it from this patch to return to it in a separate
> thread.

HEAD and back branches only care about the backend, so I think that we
should address this part first as your patch would I guess reuse the
interface we finish by using for the backend.  Looking at OpenSSL, I
agree that there is no internal logic to perform sanity checks on the
min/max bounds.  Still I can see that OpenSSL 1.1.0 has added some
"get" routines for SSL_CTX_set_min/max_proto_version:
https://www.openssl.org/docs/man1.1.0/man3/SSL_CTX_set_min_proto_version.html

Hmmmmeuh.  It would be perfect to rely only on OpenSSL for that part
to bring some sanity, and compare the results fetched from the SSL
context so as we don't have to worry about special cases in with the
GUC reload if the parameter is not set, or the parameter value is not
supported.  Now, OpenSSL <= 1.0.2 cannot do that, and you can get the
values set only after doing the set, so adding the compatibility
argument it is much more tempting to use our
ssl_protocol_version_to_openssl() wrapper and complain iff:
- both the min and max are supported values.
- min/max are incompatible.
And the check needs to be done before attempting to set the min/max
protos so as you don't finish with an incorrect intermediate state.
Daniel, are you planning to start a new thread?

> One thing I noticed when looking at it is that we now have sha2_openssl.c and
> openssl_protocol.c in src/common.  For easier visual grouping of OpenSSL
> functionality, it makes sense to me to rename sha2_openssl.c to openssl_sha2.c,
> but that might just be pointless churn.

Databases like consistency, and so do I, so no issues from me to do a
rename of the sha2.c file.  That makes sense with the addition of the
new file.
--
Michael
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAl4ZN7wACgkQnvQgOdby
QH0gDg//feOjh7yZqmJxvWI2wccSwbKoFM/fESywbexPE7VSWnNII0oPsvyLpYPr
Ldp/zuEyBSn3y6xp75exCO1L76nu0rGsYVm8Kgz4k3BtptNEKzyPfwznCaU3uwyE
NtRUecif0QyZAhwwOgCEndJ2ZdOEgvOlRmqciObmr30YYtJlV8mw8NfHdE7rNRBY
nmVlZHf9ebYHGETYjWdOkH4+epHQ9RgECua5F8DCh/GQKj8N9a8ZRP3yI+Vt87U3
gxC1z/oQsmqUO9Gt4Cq9pSiePu876iMbQBtqfG59hn0vCdMV51FhaUo5GiLdVn+a
fJ4RZHrwY7m4pTn10ICa1DDzTqdAoC/8tzXfuuMPWpxHhy0UFODlE1TxDV9R08hh
nzQn7Nu9lcELM7vH38z/ZAAGtA4Vokx2ObeYI5PyU+l7Z0mfJ4HA8CTecAheVL9t
FahvtcWrJwjhidvdYp2iDCPwJ9dPlF8SRUO4OFg8AygdnU5UhlCJ0fcudiTyjEEv
nagQNFzSIuQssYd4I+H82K/Doy64KqkRFWAloF4NfEzJqCRS+mZ7ydMsqQi1QyHL
Tearzb+ScM0FRDAP/gQf7FrbRgUcbMgMaW8LY+m9MqXr0NdB/RNIZM2trwKY/Ssq
IVsHnGxk+ZuIkp5aVlvWomow/WulfTdKjy705gQ3Y3Gfba8Ome8=
=lpVt
-----END PGP SIGNATURE-----