NameConstraints intersectPermittedSubtree is not working when name constraints extensions are set in multiple place in the CA hierarchy.
For example: if you have dNSName .permitted1.permitted2.com in permittedSubtree for both the RootCA and SubordinateCA then the intersectDNS will return null instead of .permitted1.permitted2.com
public void intersectPermittedSubtree(GeneralSubtree[] permitted)
{
Map subtreesMap = new HashMap();
// group in sets in a map ordered by tag no.
for (int i = 0; i != permitted.length; i++)
{
GeneralSubtree subtree = permitted[i];
Integer tagNo = Integers.valueOf(subtree.getBase().getTagNo());
if (subtreesMap.get(tagNo) == null)
{
subtreesMap.put(tagNo, new HashSet());
}
((Set)subtreesMap.get(tagNo)).add(subtree);
}
for (Iterator it = subtreesMap.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
// go through all subtree groups
int nameType = ((Integer)entry.getKey()).intValue();
switch (nameType)
{
case GeneralName.otherName:
permittedSubtreesOtherName = intersectOtherName(permittedSubtreesOtherName,
(Set)entry.getValue());
break;
case GeneralName.rfc822Name:
permittedSubtreesEmail = intersectEmail(permittedSubtreesEmail,
(Set)entry.getValue());
break;
case GeneralName.dNSName:
permittedSubtreesDNS = intersectDNS(permittedSubtreesDNS,
(Set)entry.getValue());
break;
case GeneralName.directoryName:
permittedSubtreesDN = intersectDN(permittedSubtreesDN,
(Set)entry.getValue());
break;
case GeneralName.uniformResourceIdentifier:
permittedSubtreesURI = intersectURI(permittedSubtreesURI,
(Set)entry.getValue());
break;
case GeneralName.iPAddress:
permittedSubtreesIP = intersectIP(permittedSubtreesIP,
(Set)entry.getValue());
break;
default:
throw new IllegalStateException("Unknown tag encountered: " + nameType);
}
}
}
private Set intersectDNS(Set permitted, Set dnss)
{
Set intersect = new HashSet();
for (Iterator it = dnss.iterator(); it.hasNext();)
{
String dns = extractNameAsString(((GeneralSubtree)it.next())
.getBase());
if (permitted == null)
{
if (dns != null)
{
intersect.add(dns);
}
}
else
{
Iterator _iter = permitted.iterator();
while (_iter.hasNext())
{
String _permitted = (String)_iter.next();
if (withinDomain(_permitted, dns))
{
intersect.add(_permitted);
}
else if (withinDomain(dns, _permitted))
{
intersect.add(dns);
}
}
}
}
return intersect;
}
private boolean withinDomain(String testDomain, String domain)
{
String tempDomain = domain;
if (tempDomain.startsWith("."))
{
tempDomain = tempDomain.substring(1);
}
String[] domainParts = Strings.split(tempDomain, '.');
String[] testDomainParts = Strings.split(testDomain, '.');
// must have at least one subdomain
if (testDomainParts.length <= domainParts.length)
{
return false;
}
int d = testDomainParts.length - domainParts.length;
for (int i = -1; i < domainParts.length; i++)
{
if (i == -1)
{
if (testDomainParts[i + d].equals(""))
{
return false;
}
}
else if (!domainParts[i].equalsIgnoreCase(testDomainParts[i + d]))
{
return false;
}
}
return true;
}
An illustration of this:
PKIXNameConstraintValidator validator = new PKIXNameConstraintValidator();
validator.intersectPermittedSubtree(new GeneralSubtree(new GeneralName(GeneralName.dNSName, ".permitted")));
validator.checkPermitted(new GeneralName(GeneralName.dNSName, "permitted.permitted"));
This permitted subtree check passes.
PKIXNameConstraintValidator validator = new PKIXNameConstraintValidator();
validator.intersectPermittedSubtree(new GeneralSubtree(new GeneralName(GeneralName.dNSName, ".permitted")));
validator.intersectPermittedSubtree(new GeneralSubtree(new GeneralName(GeneralName.dNSName, ".permitted")));
validator.checkPermitted(new GeneralName(GeneralName.dNSName, "permitted.permitted"));
This permitted subtree check fails with org.bouncycastle.asn1.x509.NameConstraintValidatorException: DNS is not from a permitted subtree.
The first intersectPermittedSubtree call is reliant on a null check to establish the subtree intersection at https://github.com/bcgit/bc-java/blob/main/core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java#L1434-L1440
The second is actually trying to perform the intersection: https://github.com/bcgit/bc-java/blob/main/core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java#L1448-L1455
What seems to be happening is that because the two subtrees are the same, neither are considered subdomains of the other within the withinDomain method and the intersection of the two names is evaluating to the empty set at which point all permitted subtree validation checks will fail.
This seems to happen when the split method is called inside withinDomain https://github.com/bcgit/bc-java/blob/main/core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java#L1066. The split method https://github.com/bcgit/bc-java/blob/main/core/src/main/java/org/bouncycastle/util/Strings.java#L327-L356 has a bug where delimiters at position 0 (for e.g. the leading dot in ".permitted") is treated as no more tokens so the condition tokenLocation > 0 returns false and it returns the entire string [".permitted"] instead of ["", "permitted"]. The if condition https://github.com/bcgit/bc-java/blob/main/core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java#L1069C9-L1069C58 holds true and returns a false.