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

PKIXNameConstraintValidator NPEs on OtherName

Open KyleBS opened this issue 7 years ago • 8 comments

The intersectOtherName method of PKIXNameConstraintValidator appears to fail to to account for null input:

https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java#L511-L518

The next intersection down, intersectEmail, however does:

https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java#L530-L557

In practice, this results in intersectPermittedSubtree throwing NPEs whenever a GeneralSubtree containing a OtherName GeneralName is supplied.

KyleBS avatar Sep 20 '19 16:09 KyleBS

Thanks for the report. I think this is now fixed.

ghost avatar Sep 30 '19 06:09 ghost

Great! Mind linking where this was fixed?

KyleBS avatar Sep 30 '19 10:09 KyleBS

It's now in the current beta: https://www.bouncycastle.org/betas github should update sometime today as well.

ghost avatar Sep 30 '19 18:09 ghost

Pulled the latest beta (164b05) to check it out, not seeing a behavior change:

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.x509.*;

public class Main {

    public static void main(final String[] args) {
        GeneralName name = new GeneralName(GeneralName.otherName, new OtherName(new ASN1ObjectIdentifier("1.1"), DERNull.INSTANCE));
        GeneralSubtree subtree = new GeneralSubtree(name);

        PKIXNameConstraintValidator validator = new PKIXNameConstraintValidator();
        validator.intersectPermittedSubtree(subtree);
    }
}

yields:

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.util.HashSet.<init>(HashSet.java:119)
        at org.bouncycastle.asn1.x509.PKIXNameConstraintValidator.intersectOtherName(PKIXNameConstraintValidator.java:513)
        at org.bouncycastle.asn1.x509.PKIXNameConstraintValidator.intersectPermittedSubtree(PKIXNameConstraintValidator.java:169)
        at org.bouncycastle.asn1.x509.PKIXNameConstraintValidator.intersectPermittedSubtree(PKIXNameConstraintValidator.java:135)
        at Main.main(Main.java:12)

Not seeing any commits that seem relevant in the github mirror or upstream git repo. Any chance this didn't get committed?

KyleBS avatar Oct 01 '19 11:10 KyleBS

Hmmmm... try 164b06 - not sure what happened with 164b05, but 06 passes the example

ghost avatar Oct 02 '19 11:10 ghost

Sure enough, 164b06 works for me as well. Thanks again!

KyleBS avatar Oct 02 '19 12:10 KyleBS

Hello, this issue seems to be resolved, but now there is an unexpected ClassCastException happening. Still related to PKIXNameConstraintsValidator on OtherName


import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.x509.*;
import org.bouncycastle.jce.provider.*;
import org.bouncycastle.jce.provider.PKIXNameConstraintValidator;

public class Main {
    public static void main(final String[] args) {
        GeneralName name = new GeneralName(GeneralName.otherName, new org.bouncycastle.asn1.x509.OtherName(new ASN1ObjectIdentifier("1.1"), DERNull.INSTANCE));
        GeneralSubtree subtree = new GeneralSubtree(name);

        PKIXNameConstraintValidator validator = new PKIXNameConstraintValidator();
        validator.intersectPermittedSubtree(subtree);
        validator.addExcludedSubtree(subtree);

        try {
            validator.checkExcluded(name);
        } catch (PKIXNameConstraintValidatorException e) {
            e.printStackTrace();
        }

        try{
            validator.checkPermitted(name);
        } catch (PKIXNameConstraintValidatorException e) {
            e.printStackTrace();
        }
    }
}

Results in:

org.bouncycastle.jce.provider.PKIXNameConstraintValidatorException: OtherName is from an excluded subtree.
	at org.bouncycastle.jce.provider.PKIXNameConstraintValidator.checkExcluded(Unknown Source)
	at Main.main(Main.java:18)
Caused by: org.bouncycastle.asn1.x509.NameConstraintValidatorException: OtherName is from an excluded subtree.
	at org.bouncycastle.asn1.x509.PKIXNameConstraintValidator.checkExcludedOtherName(Unknown Source)
	at org.bouncycastle.asn1.x509.PKIXNameConstraintValidator.checkExcluded(Unknown Source)
	... 2 more
Exception in thread "main" java.lang.ClassCastException: org.bouncycastle.asn1.x509.GeneralSubtree cannot be cast to org.bouncycastle.asn1.x509.OtherName
	at org.bouncycastle.asn1.x509.PKIXNameConstraintValidator.checkPermittedOtherName(Unknown Source)
	at org.bouncycastle.asn1.x509.PKIXNameConstraintValidator.checkPermitted(Unknown Source)
	at org.bouncycastle.jce.provider.PKIXNameConstraintValidator.checkPermitted(Unknown Source)
	at Main.main(Main.java:24)

It appears as though checkExcludedNameConstraints appropriately calls OtherName.getInstance, however checkPermittedNameConstraints inappropriately casts resulting in this issue:

https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java#L856-L892

bbernier avatar Oct 16 '19 21:10 bbernier

Cut https://github.com/bcgit/bc-java/issues/613 for the above comment.

bbernier avatar Oct 24 '19 14:10 bbernier