openssl: Broken providers no_cache support

Ok so this is a difficult to the bug one and apologies in advance that I couldn’t find the actual triggering issue even after half a day of GDB sessions.

Here’s what happens.

Configuration

the openssl.cnf file activates the default provider and the pkcs11-provider, no special options or evp parameters are provided.

Startup conditions

The pkcs11-provider does not have any special token configured, which means it will try to load a default token via p11-kit if explicitly invoked but will not really initialize anything. In this case whenever ossl_provider_query_operation() is called for the provider it returns a NULL pointer and *no_cache set to 1, to indicate this response should not be cache.

The problem

An application needs to use MD4 digesting (yeah I know), and does the following operations:

OSSL_PROVIDER_try_load(NULL, "legacy");
type = EVP_Md4();
ctx = EV_MD_CTX_new();
EVP_DigestInit_ex(tx, type, NULL);

Inside the init function EVP_MD_fetch (searching for “MD4” in this case) is attempted, trying to follow the method fetching calls I see that all three providers are found (default, pkcs11, legacy, and all three have their operation 1 (digesting) queried, but for reasons I cannot fully understand the code decides to not map any algorithm and a finally request to the algorithm store cahce fails to find anything and bails.

The working case

With the exact same code above but toggling a pkcs11-provider config option that lets it initialize early and therefore return pointers and *no_cache = 0 the EVP_MD_fetch resolves immediately from the alg store cache without even needing to construct a method (arguably the digests have been resolved by a previous call). The same behavior if the provider is disabled in config.

Conclusions so far

Somehow returning *no_cache = 1 (or perhaps NULL maps ?) confuses OpenSSL to the point that any attempt to resolve digest operations from any provider seem to fail, even if other providers do positively respond that they support the requested algorithm.

I tried to understand how all this caching code playes together but I could not find how/where in the code this could manifest. Hopefully someone in the core team understand the caching better.

In my opinion a misbheaving* provider should not cause other providers to fail to work so I think this is an OpenSSL bug regardless of whether the current pkcs11 provider behavior is correct or not.

Any hint on how to further debug this problem are also welcome.

  • I think it should be allowed to defer mapping, int he pkcs11 provider case this is becasuse until a token is inserted we do not know what mechanisms the token supports and therefore we attempt to delay responding anything to OpenSSL until (a) we have a token, and (b) we know which mechanisms it supports so we can definitely return what we can do.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 35 (35 by maintainers)

Commits related to this issue

Most upvoted comments

@mouse07410 I think I’ve merged it to 3.2+ 3.0/3.1 require a backport and have separate PRs

I strongly suspect that calling ossl_method_construct always with 0 is a wrong thing.

After discussion with @simo5

Using NSS token:

mkdir /tmp/mytoken
echo "12345678" > /tmp/mytoken.txt
certutil -N -d /tmp/mytoken -f /tmp/mytoken.txt

Config

[pkcs11_sect]
activate = 1
pkcs11-module-path = /usr/lib64/libsoftokn3.so
pkcs11-module-init-args = configDir=/tmp/mytoken

patch returning *no_cache = 1; for OSSL_OP_ENCODER for pkcs11 provider

causes a failure.

FWIW, I think this should be fixed in current versions, a provider that does not behave as intended should not affect other providers in any case (IMO), but for the long term, I think OpenSSL should provide new providers with a callback they can invoke to ask OpenSSL to forget everything it knows about the provider and re-probe all names, this would allows pkc11-provider to properly handle late initialization and token (re)insertion.

apparently openssl does also negative caching?