Skip to content

Commit a487d76

Browse files
committed
cloned fixed RFC5280 subtree handling to asn1 package
1 parent bf0f718 commit a487d76

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

core/src/main/java/org/bouncycastle/asn1/x509/PKIXNameConstraintValidator.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
import org.bouncycastle.asn1.ASN1OctetString;
1313
import org.bouncycastle.asn1.ASN1Sequence;
1414
import org.bouncycastle.asn1.DERIA5String;
15+
import org.bouncycastle.asn1.x500.RDN;
1516
import org.bouncycastle.asn1.x500.X500Name;
17+
import org.bouncycastle.asn1.x500.style.BCStyle;
18+
import org.bouncycastle.asn1.x500.style.IETFUtils;
19+
import org.bouncycastle.asn1.x500.style.RFC4519Style;
20+
import org.bouncycastle.asn1.x509.GeneralName;
21+
import org.bouncycastle.asn1.x509.GeneralSubtree;
1622
import org.bouncycastle.util.Arrays;
1723
import org.bouncycastle.util.Integers;
1824
import org.bouncycastle.util.Strings;
@@ -392,9 +398,33 @@ private static boolean withinDNSubtree(
392398

393399
for (int j = subtree.size() - 1; j >= 0; j--)
394400
{
395-
if (!subtree.getObjectAt(j).equals(dns.getObjectAt(j)))
396-
{
397-
return false;
401+
// both subtree and dns are a ASN.1 Name and the elements are a RDN
402+
RDN subtreeRdn = RDN.getInstance(subtree.getObjectAt(j));
403+
dnsiteration:
404+
for (int k=0; k<dns.size(); k++) {
405+
RDN dnsRdn = RDN.getInstance(dns.getObjectAt(k));
406+
// check if types and values of all naming attributes are matching, other types which are not restricted are allowed, see https://tools.ietf.org/html/rfc5280#section-7.1
407+
if (subtreeRdn.size() > 0 && subtreeRdn.size() == dnsRdn.size()) {
408+
// Two relative distinguished names
409+
// RDN1 and RDN2 match if they have the same number of naming attributes
410+
// and for each naming attribute in RDN1 there is a matching naming attribute in RDN2.
411+
// NOTE: this is checking the attributes in the same order, which might be not necessary, if this is a problem also IETFUtils.rDNAreEqual mus tbe changed.
412+
for (int l=0; l<subtreeRdn.size(); l++) {
413+
if (!subtreeRdn.getTypesAndValues()[l].getType().equals(dnsRdn.getTypesAndValues()[l].getType())) {
414+
continue dnsiteration;
415+
}
416+
}
417+
// use new RFC 5280 comparison, NOTE: this is now different from with RFC 3280, where only binary comparison is used
418+
// obey RFC 5280 7.1
419+
// special treatment of serialNumber for GSMA SGP.22 RSP specification
420+
if (subtreeRdn.size() == 1 && subtreeRdn.getFirst().getType().equals(RFC4519Style.serialNumber)) {
421+
if (!dnsRdn.getFirst().getValue().toString().startsWith(subtreeRdn.getFirst().getValue().toString())) {
422+
return false;
423+
}
424+
} else if (!IETFUtils.rDNAreEqual(subtreeRdn, dnsRdn)) {
425+
return false;
426+
}
427+
}
398428
}
399429
}
400430

0 commit comments

Comments
 (0)