private KeySelectorResult x509DataSelect(X509Data xd, SignatureMethod sm)
throws KeyStoreException, KeySelectorException {
// convert signature algorithm to compatible public-key alg OID
String algOID = getPKAlgorithmOID(sm.getAlgorithm());
X509CertSelector subjectcs = new X509CertSelector();
try {
subjectcs.setSubjectPublicKeyAlgID(algOID);
} catch (IOException ioe) {
throw new KeySelectorException(ioe);
}
Collection certs = new ArrayList();
Iterator xi = xd.getContent().iterator();
while (xi.hasNext()) {
Object o = xi.next();
// check X509IssuerSerial
if (o instanceof X509IssuerSerial) {
X509IssuerSerial xis = (X509IssuerSerial) o;
try {
subjectcs.setSerialNumber(xis.getSerialNumber());
String issuer = new X500Principal(xis.getIssuerName()).getName();
// strip off newline
if (issuer.endsWith("\n")) {
issuer = new String
(issuer.toCharArray(), 0, issuer.length()-1);
}
subjectcs.setIssuer(issuer);
} catch (IOException ioe) {
throw new KeySelectorException(ioe);
}
// check X509SubjectName
} else if (o instanceof String) {
String sn = (String) o;
try {
String subject = new X500Principal(sn).getName();
// strip off newline
if (subject.endsWith("\n")) {
subject = new String
(subject.toCharArray(), 0, subject.length()-1);
}
subjectcs.setSubject(subject);
} catch (IOException ioe) {
throw new KeySelectorException(ioe);
}
// check X509SKI
} else if (o instanceof byte[]) {
byte[] ski = (byte[]) o;
// DER-encode ski - required by X509CertSelector
byte[] encodedSki = new byte[ski.length+2];
encodedSki[0] = 0x04; // OCTET STRING tag value
encodedSki[1] = (byte) ski.length; // length
System.arraycopy(ski, 0, encodedSki, 2, ski.length);
subjectcs.setSubjectKeyIdentifier(encodedSki);
} else if (o instanceof X509Certificate) {
certs.add((X509Certificate) o);
// check X509CRL
// not supported: should use CertPath API
} else {
// skip all other entries
continue;
}
}
KeySelectorResult ksr = keyStoreSelect(subjectcs);
if (ksr != null) {
return ksr;
}
if (!certs.isEmpty() && !trusted) {
// try to find public key in certs in X509Data
Iterator i = certs.iterator();
while (i.hasNext()) {
X509Certificate cert = (X509Certificate) i.next();
if (subjectcs.match(cert)) {
return new SimpleKeySelectorResult(cert.getPublicKey());
}
}
}
return null;