通过更改 xades4j 行为的解决方法。
因为我没有找到任何文档证明 X509SubjectName 中的属性顺序需要与相关证书中的 DN 顺序相同。我认为稳健性需要以下补丁。
如果xades4j.verification.SignatureUtil.java决定依赖主题名。它不应该只是继续使用它,而是应该验证主题名是否与证书中的主题名相同。这不能通过比较完整的字符串来完成。相反,两个主题名称都需要解组并根据它们的关联进行比较。
首先,选择行为需要稍作改变:不只是使用主题名,而是首先从证书中检索 DN,然后比较它们的值。如果它们使用完整的字符串不匹配,它们应该可以匹配它们的实际内容。因此,我们需要获取两个 DN 的属性。当没有发现差异时,我们仍在处理相同的 DN。在这种情况下,框架不能依赖主题名从证书存储中检索证书。而是将证书提供给 KeyInfo 对象。(那会被发现的!)
if (x509Data.containsIssuerSerial()) {
issuerSerial = x509Data.itemIssuerSerial(0);
certSelector.setIssuer(new X500Principal(issuerSerial.getIssuerName()));
certSelector.setSerialNumber(issuerSerial.getSerialNumber());
} else if (x509Data.containsSubjectName()) {
String subjectName = x509Data.itemSubjectName(0).getSubjectName();
X500Principal msgPrincipal = new X500Principal(subjectName);
String name = msgPrincipal.getName();
X509Certificate crt = x509Data.itemCertificate(0).getX509Certificate();
X500Principal crtPrincipal = crt.getSubjectX500Principal();
X500Principal prinFromCrt = crt.getSubjectX500Principal();
if(prinFromCrt.getName().equals(msgPrincipal.getName())){
// Continue using the xades specified subjectname
certSelector.setSubject(msgPrincipal);
} else {
//so the subject names are not equal.
//However the ietf specifications indicate you cannot rely on the order of the attributed.
//Therefor we need to compare all attributes seperately to know for sure.
boolean hasSameKeyValues = compareUnmarshelledX500PrincipalAttr(crtPrincipal,msgPrincipal);
if (hasSameKeyValues){
if (x509Data.containsCertificate()) {
certSelector.setCertificate(x509Data.itemCertificate(0).getX509Certificate());
}
}
}
} else if (x509Data.containsCertificate()) {
certSelector.setCertificate(x509Data.itemCertificate(0).getX509Certificate());
if (x509Data.containsSubjectName()){
//if(!(isEqualX500Elements(new X500Principal(x509Data.itemSubjectName(0).getSubjectName()), x509Data.itemCertificate(0).getX509Certificate()))){
// throw new InvalidKeyInfoDataException("X509Subject name differs from Subject name in certificate.");
//}
}
} else
// No criteria to select the leaf certificate.
// Improvement: search the SigningCertiticate property and try to
// find the "bottom" certificate.
{
throw new InvalidKeyInfoDataException("No criteria to select the leaf certificate");
}
下面是一个方法,它接受 DN 并请求包含键及其值的 HashMap。无论它们是否相同,它都会返回一个布尔值。
private static boolean compareUnmarshelledX500PrincipalAttr(X500Principal DN1, X500Principal DN2) {
HashMap attrDNCrt = splitDNAttr(DN1.getName());
HashMap attrDNMsg = splitDNAttr(DN2.getName());
if(attrDNCrt.keySet().equals(attrDNMsg.keySet())){
Set ks = attrDNCrt.keySet();
Iterator iKS = ks.iterator();
while (iKS.hasNext()){
String key = (String) iKS.next();
if(!attrDNCrt.get(key).toString().equals(attrDNMsg.get(key).toString())){
//Value of attribute is different. So not identical
return false;
}
}
//Yes, despite possible differences in order the key value pairs are identical.");
return true;
} else {
//"KeySets differ so they are different"
return false;
}
}
splitDNAttr 将依赖于密钥命名不包含“,”这一事实。因此,我首先在“=”上拆分,然后在最后一个“,”上拆分。可能有正则表达式也可以解决问题。(不幸的是,RegEx 对我来说完全不可读。)这些方法对我有用,但我想知道是否需要删除可能的尾随空格“?
static private HashMap splitDNAttr(String inputStr){
String[] strings;
List looseElements;
looseElements = new ArrayList();
//First split on the = which normally isn't escaped.
strings = inputStr.split("=");
looseElements.add(strings[0]);
//Loop Through string members
int i=1;
while (i<strings.length){
String[] subStrings;
//Look for the last comma, everything after is a key! This is because we have splitted the string on '='
int splitPos = strings[i].lastIndexOf(",");
if(splitPos>=0){
//Add all found items to a list. Order must be maintained!
String A = strings[i].substring(0,splitPos);
looseElements.add(strings[i].substring(0,splitPos));
String B = strings[i].substring(splitPos+1);
looseElements.add(strings[i].substring(splitPos+1));
} else {
looseElements.add(strings[i]);
}
i++;
}
// Put key and values in a HashMap
HashMap dnAttr = new HashMap();
Iterator iLooseElements;
iLooseElements = looseElements.iterator();
String a;
String b;
while(iLooseElements.hasNext()){
a = (String) iLooseElements.next();
b = (String) iLooseElements.next();
dnAttr.put(a, b);
}
return dnAttr;
}