bc-java icon indicating copy to clipboard operation
bc-java copied to clipboard

NullPointerException in org.bouncycastle.jce.provider.BouncyCastleProvider

Open martijnbrinkers opened this issue 1 year ago • 2 comments

One of my test cases, now throws a NullPointerException when upgrading BC from 1.71 to 1.78.1

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
	at java.base/java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
	at java.base/java.util.Properties.put(Properties.java:1346)
	at java.base/java.security.Provider.putPropertyStrings(Provider.java:1440)
	at java.base/java.security.Provider.putService(Provider.java:1396)
	at org.bouncycastle.jce.provider.BouncyCastleProvider.access$401(BouncyCastleProvider.java:72)
	at org.bouncycastle.jce.provider.BouncyCastleProvider$2.run(BouncyCastleProvider.java:284)
	at org.bouncycastle.jce.provider.BouncyCastleProvider$2.run(BouncyCastleProvider.java:272)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
	at org.bouncycastle.jce.provider.BouncyCastleProvider.getService(BouncyCastleProvider.java:271)
	at java.base/sun.security.jca.GetInstance.getService(GetInstance.java:85)
	at java.base/sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
	at java.base/java.security.Security.getImpl(Security.java:658)
	at java.base/java.security.AlgorithmParameters.getInstance(AlgorithmParameters.java:213)
	at org.bouncycastle.jcajce.util.NamedJcaJceHelper.createAlgorithmParameters(NamedJcaJceHelper.java:71)
	at org.bouncycastle.cms.jcajce.EnvelopedDataHelper.createAlgorithmParameters(EnvelopedDataHelper.java:532)
	at org.bouncycastle.cms.jcajce.JceAlgorithmIdentifierConverter.getAlgorithmParameters(JceAlgorithmIdentifierConverter.java:49)
	at Test.main(Test.java:22)

The test uses a certificate created by Microsoft CA with some non-standard subject key identifier (SKI). This was working with BC 1.71 but results in a NPE on BC 1.78.1

Test case (with the asn1 object extracted from a bigger tests system which failed)

import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Base64;

import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.jcajce.JceAlgorithmIdentifierConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class Test
{
    public static void main(String[] args)
    throws CMSException
    {
        Security.addProvider(new BouncyCastleProvider());

        byte[] asn1 = Base64.getDecoder().decode("MA0GCSqGSIb3DQEBAQUA");

        AlgorithmIdentifier algorithmIdentifier = AlgorithmIdentifier.getInstance(asn1);

        AlgorithmParameters p = new JceAlgorithmIdentifierConverter().setProvider("BC").
                getAlgorithmParameters(algorithmIdentifier);
    }
}

martijnbrinkers avatar Sep 06 '24 13:09 martijnbrinkers

So I wasn't able to reproduce this at first, then I tried it in Java 21... okay, it seems Oracle have changed things again, previously a null would be returned as the service couldn't be created, now a service handle is returned but it has got a null class name on it. I guess this would be useful for someone, just not us... a big thank you for this one, we weren't even looking for something like this, now have a test in place which should keep triggering the failure and hopefully pick up any further changes they might make here.

The fix for this is now up in https://downloads.bouncycastle.org/betas if you would just confirm we're both seeing the same thing I and I really have fixed it, it would be appreciated.

dghgit avatar Sep 08 '24 01:09 dghgit

You are right. It fails on Java 21 but not on 17.

I can confirm that it no longer fails with the new beta release.

martijnbrinkers avatar Sep 08 '24 10:09 martijnbrinkers