use of org.nhindirect.config.ui.DNSController.CertContainer in project nhin-d by DirectProject.
the class MainController method toCertContainer.
public CertContainer toCertContainer(byte[] data) throws Exception {
CertContainer certContainer = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
// lets try this a as a PKCS12 data stream first
try {
KeyStore localKeyStore = KeyStore.getInstance("PKCS12", Certificate.getJCEProviderName());
localKeyStore.load(bais, "".toCharArray());
Enumeration<String> aliases = localKeyStore.aliases();
// we are really expecting only one alias
if (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
X509Certificate cert = (X509Certificate) localKeyStore.getCertificate(alias);
// check if there is private key
Key key = localKeyStore.getKey(alias, "".toCharArray());
if (key != null && key instanceof PrivateKey) {
certContainer = new CertContainer(cert, key);
}
}
} catch (Exception e) {
// must not be a PKCS12 stream, go on to next step
}
if (certContainer == null) {
//try X509 certificate factory next
bais.reset();
bais = new ByteArrayInputStream(data);
X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(bais);
certContainer = new CertContainer(cert, null);
}
bais.close();
} catch (Exception e) {
throw new ConfigurationServiceException("Data cannot be converted to a valid X.509 Certificate", e);
}
return certContainer;
}
Aggregations