assertEquals(name.toString(), issuerDN);
if (signed) {
// Verify the signature
byte[] protBytes = respObject.getProtectedBytes();
DERBitString bs = respObject.getProtection();
Signature sig;
try {
sig = Signature.getInstance(PKCSObjectIdentifiers.sha1WithRSAEncryption.getId(), "BC");
sig.initVerify(cacert);
sig.update(protBytes);
boolean ret = sig.verify(bs.getBytes());
assertTrue(ret);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
assertTrue(false);
} catch (NoSuchProviderException e) {
e.printStackTrace();
assertTrue(false);
} catch (InvalidKeyException e) {
e.printStackTrace();
assertTrue(false);
} catch (SignatureException e) {
e.printStackTrace();
assertTrue(false);
}
}
if (pbe) {
DEROctetString os = header.getSenderKID();
assertNotNull(os);
String keyId = new String(os.getOctets());
log.debug("Found a sender keyId: " + keyId);
// Verify the PasswordBased protection of the message
byte[] protectedBytes = respObject.getProtectedBytes();
DERBitString protection = respObject.getProtection();
AlgorithmIdentifier pAlg = header.getProtectionAlg();
log.debug("Protection type is: " + pAlg.getObjectId().getId());
PBMParameter pp = PBMParameter.getInstance(pAlg.getParameters());
int iterationCount = pp.getIterationCount().getPositiveValue().intValue();
log.debug("Iteration count is: " + iterationCount);
AlgorithmIdentifier owfAlg = pp.getOwf();
// Normal OWF alg is 1.3.14.3.2.26 - SHA1
log.debug("Owf type is: " + owfAlg.getObjectId().getId());
AlgorithmIdentifier macAlg = pp.getMac();
// Normal mac alg is 1.3.6.1.5.5.8.1.2 - HMAC/SHA1
log.debug("Mac type is: " + macAlg.getObjectId().getId());
byte[] salt = pp.getSalt().getOctets();
// log.info("Salt is: "+new String(salt));
byte[] raSecret = pbeSecret.getBytes();
byte[] basekey = new byte[raSecret.length + salt.length];
for (int i = 0; i < raSecret.length; i++) {
basekey[i] = raSecret[i];
}
for (int i = 0; i < salt.length; i++) {
basekey[raSecret.length + i] = salt[i];
}
// Construct the base key according to rfc4210, section 5.1.3.1
MessageDigest dig = MessageDigest.getInstance(owfAlg.getObjectId().getId(), "BC");
for (int i = 0; i < iterationCount; i++) {
basekey = dig.digest(basekey);
dig.reset();
}
// HMAC/SHA1 os normal 1.3.6.1.5.5.8.1.2 or 1.2.840.113549.2.7
String macOid = macAlg.getObjectId().getId();
Mac mac = Mac.getInstance(macOid, "BC");
SecretKey key = new SecretKeySpec(basekey, macOid);
mac.init(key);
mac.reset();
mac.update(protectedBytes, 0, protectedBytes.length);
byte[] out = mac.doFinal();
// My out should now be the same as the protection bits
byte[] pb = protection.getBytes();
boolean ret = Arrays.equals(out, pb);
assertTrue(ret);
}
// --SenderNonce