use of de.carne.certmgr.certs.PasswordRequiredException in project certmgr by hdecarne.
the class JKSCertReaderWriter method writeEncryptedBinary.
@Override
public void writeEncryptedBinary(IOResource<OutputStream> out, CertObjectStore certObjects, PasswordCallback newPassword) throws IOException {
char[] passwordChars = newPassword.queryPassword(out.resource());
if (passwordChars == null) {
throw new PasswordRequiredException(out.resource());
}
try {
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
keyStore.load(null, null);
List<X509Certificate> crtChain = new ArrayList<>(certObjects.size());
for (CertObjectStore.Entry certObject : certObjects) {
if (certObject.type() == CertObjectType.CRT) {
keyStore.setCertificateEntry(certObject.alias(), certObject.getCRT());
crtChain.add(0, certObject.getCRT());
}
}
for (CertObjectStore.Entry certObject : certObjects) {
if (certObject.type() == CertObjectType.KEY) {
keyStore.setKeyEntry(certObject.alias(), certObject.getKey().getPrivate(), passwordChars, crtChain.toArray(new X509Certificate[crtChain.size()]));
crtChain.clear();
}
}
keyStore.store(out.io(), passwordChars);
} catch (GeneralSecurityException e) {
throw new CertProviderException(e);
}
}
use of de.carne.certmgr.certs.PasswordRequiredException in project certmgr by hdecarne.
the class JKSCertReaderWriter method readKeyStore.
@Nullable
private static CertObjectStore readKeyStore(String keyStoreType, @Nullable InputStream inputStream, String resource, PasswordCallback password) throws IOException {
KeyStore keyStore = null;
try {
keyStore = loadKeyStore(keyStoreType, inputStream, resource, password);
} catch (GeneralSecurityException e) {
throw new CertProviderException(e);
} catch (PasswordRequiredException e) {
throw e;
} catch (IOException e) {
LOG.info(e, "No KeyStore objects recognized in: ''{0}''", resource);
}
CertObjectStore certObjects = null;
if (keyStore != null) {
try {
certObjects = new CertObjectStore();
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
Certificate aliasCertificate = keyStore.getCertificate(alias);
if (aliasCertificate != null) {
if (aliasCertificate instanceof X509Certificate) {
certObjects.addCRT((X509Certificate) aliasCertificate);
} else {
LOG.warning("Ignoring certificate of key store entry ''{0}'' due to unsupported type ''{1}''", alias, aliasCertificate.getClass().getName());
}
}
Key aliasKey = getAliasKey(keyStore, alias, password);
if (aliasKey != null) {
if (aliasKey instanceof PrivateKey) {
try {
certObjects.addKey(KeyHelper.rebuildKeyPair((PrivateKey) aliasKey));
} catch (IOException e) {
LOG.warning(e, "Unable to rebuild key pair for private key ''{0}'' of type ''{1}''", alias, aliasKey.getClass().getName());
}
} else {
LOG.warning("Ignoring key of key store entry ''{0}'' due to unsupported type ''{1}''", alias, aliasKey.getClass().getName());
}
}
Certificate[] aliasChain = keyStore.getCertificateChain(alias);
if (aliasChain != null) {
for (Certificate aliasChainEntry : aliasChain) {
if (aliasChainEntry instanceof X509Certificate) {
certObjects.addCRT((X509Certificate) aliasChainEntry);
} else {
LOG.warning("Ignoring chain certificate of key store entry ''{0}'' due to unsupported type ''{1}''", alias, aliasChainEntry.getClass().getName());
}
}
}
}
} catch (GeneralSecurityException e) {
throw new CertProviderException(e);
}
}
return certObjects;
}
Aggregations