Don't send -1 as the length of the hmac key
authorMatt Caswell <matt@openssl.org>
Thu, 10 Sep 2020 13:46:41 +0000 (14:46 +0100)
committerDmitry Belyavskiy <beldmit@gmail.com>
Thu, 17 Sep 2020 08:12:08 +0000 (11:12 +0300)
commitb8e5622809d3b3f61c4a615e51f5a8fd492ee23f
treeb51a8346b16797d89060403ed56ef5a2c5cd2709
parent067a3057c3aab0cdd9a3cdb13c2e0000f69a4170
Don't send -1 as the length of the hmac key

The dgst app was using an undocumented behaviour in the
EVP_PKEY_new_raw_private_key() function when setting a key length for
a MAC. The old EVP_PKEY to MAC bridge, probably by accident, converts a
-1 length to a strlen() call, by virtue of the fact that it eventually
calls ASN1_STRING_set() which has this feature.

As noted above this is undocumented, and unexpected since the len
parameter to EVP_PKEY_new_raw_private_key() is an unsigned value (size_t).
In the old bridge it was later (silently) cast to an int, and therefore
the original -1 value was restored. This only works because sizeof(int) <=
sizeof(size_t). If we ever run on a platform where sizeof(int) >
sizeof(size_t) then it would have failed. The behaviour also doesn't hold
for EVP_PKEY_new_raw_private_key() in general - only when the old MAC
bridge was in use.

Rather than restore the original behaviour I think it is best to simply
fix the dgst app to not assume it exists. We should not bake in this
backwards and inconsistent behaviour.

Fixes #12837

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/12850)
apps/dgst.c