io.r2.simplepemkeystore.spi.SimplePemKeyStoreSpi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-pem-keystore Show documentation
Show all versions of simple-pem-keystore Show documentation
A java keystore implementation to use PEM files directly.
package io.r2.simplepemkeystore.spi;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A simple (non-reloadable) PEM based key store
*/
public class SimplePemKeyStoreSpi extends BasePemKeyStore {
/**
* Loads from a stream of PEM files
* If no alias metadata present, it stores under alias 'server'
* Otherwise can parse multiple certificates separated by alias metadata
*
* @param stream input stream with multiple PEMs (including certificate chain and key)
* @param password not used, password protection is not supported
* @throws IOException on input error
* @throws NoSuchAlgorithmException - not thrown
* @throws CertificateException if loading is failed
*/
@Override
public void engineLoad(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
List certList = PemStreamParser.parseCertificateList(stream);
// check for alias conflict in input
Map newCerts = new HashMap<>();
for (PemCertKey certkey : certList) {
String alias = certkey.getAlias();
if (newCerts.putIfAbsent(alias, certkey) != null) {
throw new CertificateException("Multiple entries with the same alias: " + alias);
}
}
// no alias conflict: store everything (update existing also)
store.putAll(newCerts);
}
}