use of com.mindbright.security.pkcs12.AuthenticatedSafe in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method engineLoad.
public void engineLoad(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
try {
ASN1DER ber = new ASN1DER();
PFX pfx = new PFX();
ber.decode(stream, pfx);
if (password == null) {
password = new char[0];
}
checkMac(pfx, password);
AuthenticatedSafe authSafe = new AuthenticatedSafe();
ASN1OctetString data = pfx.getDataContent();
ByteArrayInputStream ba = new ByteArrayInputStream(data.getRaw());
ber.decode(ba, authSafe);
for (int i = 0; i < authSafe.getCount(); i++) {
ContentInfo ci = authSafe.getContentInfo(i);
String cit = ci.contentType.getString();
if (cit.equals("1.2.840.113549.1.7.1")) {
data = (ASN1OctetString) ci.content.getValue();
processSafeContents(data.getRaw());
} else if (cit.equals("1.2.840.113549.1.7.6")) {
EncryptedData ed = (EncryptedData) ci.content.getValue();
String alg = ed.encryptedContentInfo.contentEncryptionAlgorithm.algorithmName();
byte[] enc = ed.encryptedContentInfo.encryptedContent.getRaw();
PKCS12PbeParams params = (PKCS12PbeParams) ed.encryptedContentInfo.contentEncryptionAlgorithm.parameters.getValue();
byte[] salt = params.salt.getRaw();
int iterations = params.iterations.getValue().intValue();
byte[] dec = new byte[enc.length];
doCipher(Cipher.DECRYPT_MODE, password, enc, enc.length, dec, salt, iterations, alg);
processSafeContents(dec);
} else {
throw new IOException("ContentInfo type not supported: " + cit);
}
}
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
Aggregations