//
PKIMessage respObject = PKIMessage.getInstance(new ASN1InputStream(new ByteArrayInputStream(retMsg)).readObject());
assertNotNull(respObject);
// The signer, i.e. the CA, check it's the right CA
PKIHeader header = respObject.getHeader();
// Check that the message is signed with the correct digest alg
if (signed) {
AlgorithmIdentifier algId = header.getProtectionAlg();
assertNotNull("The AlgorithmIdentifier in the response signature could not be read.", algId);
assertEquals(PKCSObjectIdentifiers.sha1WithRSAEncryption.getId(), algId.getObjectId().getId());
}
if (pbe) {
AlgorithmIdentifier algId = header.getProtectionAlg();
assertNotNull("Protection algorithm was null.", algId);
assertEquals("Protection algorithm id: " + algId.getObjectId().getId(), CMPObjectIdentifiers.passwordBasedMac.getId(), algId.getObjectId().getId()); //1.2.840.113549.1.1.5 - SHA-1 with RSA Encryption
}
// Check that the signer is the expected CA
assertEquals(header.getSender().getTagNo(), 4);
X509Name name = X509Name.getInstance(header.getSender().getName());
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
// SenderNonce is something the server came up with, but it should be 16
// chars
byte[] nonce = header.getSenderNonce().getOctets();
assertEquals(nonce.length, 16);
// --Recipient Nonce
// recipient nonce should be the same as we sent away as sender nonce
nonce = header.getRecipNonce().getOctets();
assertEquals(new String(nonce), new String(senderNonce));
// --Transaction ID
// transid should be the same as the one we sent
nonce = header.getTransactionID().getOctets();
assertEquals(new String(nonce), new String(transId));
}