try
{
ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(pkcs12));
ASN1Sequence obj = (ASN1Sequence)aIn.readObject();
Pfx bag = new Pfx(obj);
ContentInfo info = bag.getAuthSafe();
MacData mData = bag.getMacData();
DigestInfo dInfo = mData.getMac();
AlgorithmIdentifier algId = dInfo.getAlgorithmId();
byte[] salt = mData.getSalt();
int itCount = mData.getIterationCount().intValue();
aIn = new ASN1InputStream(new ByteArrayInputStream(((ASN1OctetString)info.getContent()).getOctets()));
AuthenticatedSafe authSafe = new AuthenticatedSafe((ASN1Sequence)aIn.readObject());
ContentInfo[] c = authSafe.getContentInfo();
//
// private key section
//
if (!c[0].getContentType().equals(PKCSObjectIdentifiers.data))
{
return new SimpleTestResult(false, getName() + ": failed comparison data test");
}
aIn = new ASN1InputStream(new ByteArrayInputStream(((ASN1OctetString)c[0].getContent()).getOctets()));
ASN1Sequence seq = (ASN1Sequence)aIn.readObject();
SafeBag b = new SafeBag((ASN1Sequence)seq.getObjectAt(0));
if (!b.getBagId().equals(PKCSObjectIdentifiers.pkcs8ShroudedKeyBag))
{
return new SimpleTestResult(false, getName() + ": failed comparison shroudedKeyBag test");
}
EncryptedPrivateKeyInfo encInfo = EncryptedPrivateKeyInfo.getInstance((ASN1Sequence)b.getBagValue());
encInfo = new EncryptedPrivateKeyInfo(encInfo.getEncryptionAlgorithm(), encInfo.getEncryptedData());
b = new SafeBag(PKCSObjectIdentifiers.pkcs8ShroudedKeyBag, encInfo.toASN1Object(), b.getBagAttributes());
ByteArrayOutputStream abOut = new ByteArrayOutputStream();
ASN1OutputStream berOut = new ASN1OutputStream(abOut);
berOut.writeObject(new DERSequence(b));
c[0] = new ContentInfo(PKCSObjectIdentifiers.data, new BERConstructedOctetString(abOut.toByteArray()));
//
// certificates
//
if (!c[1].getContentType().equals(PKCSObjectIdentifiers.encryptedData))
{
return new SimpleTestResult(false, getName() + ": failed comparison encryptedData test");
}
EncryptedData eData = EncryptedData.getInstance(c[1].getContent());
c[1] = new ContentInfo(PKCSObjectIdentifiers.encryptedData, eData);
//
// create an octet stream represent the BER encoding of authSafe
//
authSafe = new AuthenticatedSafe(c);
abOut = new ByteArrayOutputStream();
berOut = new ASN1OutputStream(abOut);
berOut.writeObject(authSafe);
info = new ContentInfo(PKCSObjectIdentifiers.data, new BERConstructedOctetString(abOut.toByteArray()));
mData = new MacData(new DigestInfo(algId, dInfo.getDigest()), salt, itCount);
bag = new Pfx(info, mData);