use of com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method engineGetKey.
public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
String localKeyId = name2id.get(alias);
if (localKeyId == null) {
return null;
}
EncryptedPrivateKeyInfo epki = privateKeys.get(localKeyId);
return extractPrivateKey(epki, password);
}
use of com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method processSafeContents.
private void processSafeContents(byte[] scBer) throws IOException {
ByteArrayInputStream ba = new ByteArrayInputStream(scBer);
SafeContents sc = new SafeContents();
ASN1DER ber = new ASN1DER();
ber.decode(ba, sc);
for (int j = 0; j < sc.getCount(); j++) {
SafeBag safeBag = sc.getSafeBag(j);
String friendlyName = getAttribute(safeBag, "1.2.840.113549.1.9.20");
String localKeyId = getAttribute(safeBag, "1.2.840.113549.1.9.21");
if (friendlyName != null) {
if (localKeyId != null) {
name2id.put(friendlyName, localKeyId);
}
if (!aliases.contains(friendlyName)) {
aliases.addElement(friendlyName);
}
} else if (localKeyId != null) {
name2id.put(localKeyId, localKeyId);
if (!aliases.contains(localKeyId)) {
aliases.addElement(localKeyId);
}
}
switch(safeBag.getBagType()) {
case SafeBag.TYPE_PKCS8_SHROUDED_KEYBAG:
EncryptedPrivateKeyInfo keyBag = (EncryptedPrivateKeyInfo) safeBag.bagValue.getValue();
privateKeys.put(localKeyId, keyBag);
break;
case SafeBag.TYPE_CERTBAG:
CertBag cb = (CertBag) safeBag.bagValue.getValue();
byte[] derCert = ((ASN1OctetString) cb.certValue.getValue()).getRaw();
if (localKeyId == null) {
/*
* Trusted certs don't have a localKeyId
*/
localKeyId = friendlyName;
} else {
certificates.put(localKeyId, derCert);
}
break;
default:
throw new IOException("SafeBag type not supported: " + safeBag.bagId.getString());
}
}
}
use of com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo in project SpringRemote by HaleyWang.
the class NetscapeKeyStore method engineGetKey.
public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
KeyEntry keyEntry = getKeyEntry(alias);
if (!passwordCheck(password)) {
throw new UnrecoverableKeyException("Invalid password");
}
if (keyEntry != null) {
try {
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo();
ASN1DER ber = new ASN1DER();
ByteArrayInputStream ba = new ByteArrayInputStream(keyEntry.encryptedKey);
ber.decode(ba, epki);
byte[] enc = epki.encryptedData.getRaw();
byte[] dec = new byte[enc.length];
do3DESCipher(Cipher.DECRYPT_MODE, password, enc, 0, enc.length, dec, globalSalt(), keyEntry.salt);
ba = new ByteArrayInputStream(dec);
return PKCS12KeyStore.extractPrivateKey(dec);
} catch (IOException e) {
throw new UnrecoverableKeyException(e.getMessage());
}
}
return null;
}
Aggregations