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

PKIXNameConstraintValidator ClassCastExceptions on OtherName

Open bbernier opened this issue 7 years ago • 1 comments

There appears to be an erroneous ClassCastException happening when checking permitted subtrees on OtherName in PKIXNameConstraintValidator. 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

To reproduce:

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)

bbernier avatar Oct 24 '19 14:10 bbernier

Thanks for the report. This should now be fixed as well.

ghost avatar Mar 12 '20 04:03 ghost