/** * Just re-encode the outer layer of the PKCS#12 file to definite length encoding. * * @param berPKCS12File - original PKCS#12 file * @return a byte array representing the DER encoding of the PFX structure * @throws IOException */ public static byte[] convertToDefiniteLength(byte[] berPKCS12File) throws IOException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); Pfx pfx = Pfx.getInstance(berPKCS12File); bOut.reset(); dOut.writeObject(pfx); return bOut.toByteArray(); }
/** * Build the Pfx structure, protecting it with a MAC calculated against the passed in password. * * @param macCalcBuilder a builder for a PKCS12 mac calculator. * @param password the password to use. * @return a Pfx object. * @throws PKCSException on a encoding or processing error. */ public PKCS12PfxPdu build(PKCS12MacCalculatorBuilder macCalcBuilder, char[] password) throws PKCSException { AuthenticatedSafe auth = AuthenticatedSafe.getInstance(new DLSequence(dataVector)); byte[] encAuth; try { encAuth = auth.getEncoded(); } catch (IOException e) { throw new PKCSException("unable to encode AuthenticatedSafe: " + e.getMessage(), e); } ContentInfo mainInfo = new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(encAuth)); MacData mData = null; if (macCalcBuilder != null) { MacDataGenerator mdGen = new MacDataGenerator(macCalcBuilder); mData = mdGen.build(password, encAuth); } // // output the Pfx // Pfx pfx = new Pfx(mainInfo, mData); return new PKCS12PfxPdu(pfx); }
public void performTest() throws Exception { testPKCS12Store(); testGOSTStore(); // converter tests KeyStore kS = KeyStore.getInstance("PKCS12", "BC"); byte[] data = PKCS12Util.convertToDefiniteLength(pkcs12); kS.load(new ByteArrayInputStream(data), passwd); // check MAC ASN1Encodable obj = new ASN1StreamParser(data).readObject(); if (!(obj instanceof DERSequenceParser)) { fail("Failed DER conversion test."); } data = PKCS12Util.convertToDefiniteLength(pkcs12, passwd, "BC"); kS.load(new ByteArrayInputStream(data), passwd); //check MAC obj = new ASN1StreamParser(data).readObject(); if (!(obj instanceof DERSequenceParser)) { fail("Failed deep DER conversion test - outer."); } Pfx pfx = Pfx.getInstance(obj); obj = new ASN1StreamParser(ASN1OctetString.getInstance(pfx.getAuthSafe().getContent()).getOctets()).readObject(); if (!(obj instanceof DERSequenceParser)) { fail("Failed deep DER conversion test - inner."); } }
public PKCS12PfxPdu(Pfx pfx) { this.pfx = pfx; }
/** * Return the underlying ASN.1 object. * * @return a Pfx object. */ public Pfx toASN1Structure() { return pfx; }