Re: OpenSSL 3.0.0 compatibility

From: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
To: Daniel Gustafsson <daniel(at)yesql(dot)se>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: OpenSSL 3.0.0 compatibility
Date: 2021-07-03 15:00:42
Message-ID: b1a62889-bb45-e5e0-d138-7a370a0a334f@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 12.03.21 08:51, Peter Eisentraut wrote:
>
> On 11.03.21 11:41, Daniel Gustafsson wrote:
>> .. and apply the padding changes as proposed in a patch upthread like
>> this (these
>> work for all OpenSSL versions I've tested, and I'm rather more puzzled
>> as to
>> why we got away with not having them in the past):
>
> Yes, before proceeding with this, we should probably understand why
> these changes are effective and why they haven't been required in the past.

I took another look at this with openssl-3.0.0-beta1. The issue with
the garbled padding output is still there. What I found is that
pgcrypto has been using the encryption and decryption API slightly
incorrectly. You are supposed to call EVP_DecryptUpdate() followed by
EVP_DecryptFinal_ex() (and similarly for encryption), but pgcrypto
doesn't do the second one. (To be fair, this API was added to OpenSSL
after pgcrypto first appeared.) The "final" functions take care of the
padding. We have been getting away with it like this because we do the
padding manually elsewhere. But apparently, something has changed in
OpenSSL 3.0.0 in that if padding is enabled in OpenSSL,
EVP_DecryptUpdate() doesn't flush the last normal block until the
"final" function is called.

Your proposed fix was to explicitly disable padding, and then this
problem goes away. You can still call the "final" functions, but they
won't do anything, except check that there is no more data left, but we
already check that elsewhere.

Another option is to throw out our own padding code and let OpenSSL
handle it. See attached demo patch. But that breaks the non-OpenSSL
code in internal.c, so we'd have to re-add the padding code there. So
this isn't quite as straightforward an option. (At least, with the
patch we can confirm that the OpenSSL padding works consistently with
our own implementation.)

So I think your proposed patch is sound and a good short-term and
low-risk solution.

Attachment Content-Type Size
0001-Use-EVP_EncryptFinal_ex-and-EVP_DecryptFinal_ex.patch text/plain 7.0 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2021-07-03 15:23:56 Re: Preventing abort() and exit() calls in libpq
Previous Message Tom Lane 2021-07-03 14:45:59 Re: Preventing abort() and exit() calls in libpq