for (Iterator it = encCerts.iterator(); it.hasNext();) {
certs[i++]= new X509CertImpl((org.apache.harmony.security.x509.Certificate)it.next());
}
List sigInfos = signedData.getSignerInfos();
SignerInfo sigInfo;
if (!sigInfos.isEmpty()) {
sigInfo = (SignerInfo)sigInfos.get(0);
} else {
return null;
}
// Issuer
X500Principal issuer = sigInfo.getIssuer();
// Certificate serial number
BigInteger snum = sigInfo.getSerialNumber();
// Locate the certificate
int issuerSertIndex = 0;
for (i = 0; i < certs.length; i++) {
if (issuer.equals(certs[i].getIssuerDN()) &&
snum.equals(certs[i].getSerialNumber())) {
issuerSertIndex = i;
break;
}
}
if (i == certs.length) { // No issuer certificate found
return null;
}
if (certs[issuerSertIndex].hasUnsupportedCriticalExtension()) {
throw new SecurityException(Messages.getString("security.174")); //$NON-NLS-1$
}
// Get Signature instance
Signature sig = null;
String da = sigInfo.getdigestAlgorithm();
String dea = sigInfo.getDigestEncryptionAlgorithm();
String alg = null;
if (da != null && dea != null) {
alg = da + "with" + dea; //$NON-NLS-1$
try{
sig = Signature.getInstance(alg);
} catch (NoSuchAlgorithmException e) {}
}
if (sig == null) {
alg = da;
if (alg == null) {
return null;
}
try{
sig = Signature.getInstance(alg);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
sig.initVerify(certs[issuerSertIndex]);
// If the authenticatedAttributes field of SignerInfo contains more than zero attributes,
// compute the message digest on the ASN.1 DER encoding of the Attributes value.
// Otherwise, compute the message digest on the data.
List atr = sigInfo.getAuthenticatedAttributes();
byte[] sfBytes = InputStreamHelper.readFullyAndClose(signature);
if (atr == null) {
sig.update(sfBytes);
} else {
sig.update(sigInfo.getEncodedAuthenticatedAttributes());
// If the authenticatedAttributes field contains the message-digest attribute,
// verify that it equals the computed digest of the signature file
byte[] existingDigest = null;
for (Iterator it = atr.iterator(); it.hasNext();) {
AttributeTypeAndValue a = (AttributeTypeAndValue)it.next();
if (Arrays.equals(a.getType().getOid(), MESSAGE_DIGEST_OID) ){
//TODO value existingDigest = a.AttributeValue;
}
}
if (existingDigest != null) {
MessageDigest md = MessageDigest.getInstance(sigInfo.getDigestAlgorithm());
byte[] computedDigest = md.digest(sfBytes);
if (!Arrays.equals(existingDigest, computedDigest)) {
throw new SecurityException(Messages.getString("security.175")); //$NON-NLS-1$
}
}
}
if (!sig.verify(sigInfo.getEncryptedDigest())) {
throw new SecurityException(Messages.getString("security.176")); //$NON-NLS-1$
}
return createChain(certs[issuerSertIndex], certs);
}