Hi,
i'm trying to creating a pkcs12, and then parse it with the example
(pkcs12.java) provided with JSS .
Creation seems ok, but when parsing with pkcs12.java, I got an
exception when parsing the certbag content (parsing the key is ok):
InvalidBERException OCTET_STRING >> Incorrect tag: expected [UNIVERSAL
4], found [UNIVERSAL 16]
at org.mozilla.jss.pkcs123.CertBag.getInterpretedCert(CertBag.java:96)
The certificateshould be DER encoded, what I do. I guess I'm not the
only one to try to create p12... hope this will help others...
Here is the code which creates the p12 :
protected static void create(PrivateKeyInfo pki,
org.mozilla.jss.pkix.cert.Certificate cert ) {
//JSS initialisation...
// Add key
Password newPass = new Password("password".toCharArray());
AuthenticatedSafes newAuthSafes = new AuthenticatedSafes();
SEQUENCE safeContents = new SEQUENCE();
byte[] salt = new byte[
PBEAlgorithm.PBE_SHA1_DES3_CBC.getSaltLength()];
JSSSecureRandom rand = CryptoManager.getInstance().getSecureRNG();
rand.nextBytes(salt);
EncryptedPrivateKeyInfo epki = EncryptedPrivateKeyInfo.createPBE(
PBEAlgorithm.PBE_SHA1_DES3_CBC, newPass,
salt, 1, new PasswordConverter(), pki);
SafeBag safekey = new SafeBag( new
OBJECT_IDENTIFIER("1.2.840.113549.1.12.10.1.1"),
pki,new SET() );
safeContents.insertElementAt(safekey, 0);
// Add certificate
SafeBag safecert = SafeBag.createCertBag(ASN1Util.encode(cert),"Cert");
safeContents.insertElementAt(safecert,1);
newAuthSafes.addEncryptedSafeContents(
AuthenticatedSafes.DEFAULT_KEY_GEN_ALG, newPass,
null, AuthenticatedSafes.DEFAULT_ITERATIONS, safeContents);
// Create new PFX from the new authsafes
PFX newPfx = new PFX(newAuthSafes);
// Add a MAC to the new PFX
newPfx.computeMacData(newPass, null, PFX.DEFAULT_ITERATIONS);
// write the new PFX out to a file
FileOutputStream fos = new FileOutputStream("/test.p12");
newPfx.encode(fos);
fos.close();
newPass.clear();
} catch( Exception e ) {System.out.println("Exception : " +
e.getMessage());e.printStackTrace(); }
}