Search in sources :

Example 1 with CertContainer

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;
}
Also used : PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) KeyStore(java.security.KeyStore) CertContainer(org.nhindirect.config.ui.DNSController.CertContainer) X509Certificate(java.security.cert.X509Certificate) Key(java.security.Key) PrivateKey(java.security.PrivateKey) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Key (java.security.Key)1 KeyStore (java.security.KeyStore)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PrivateKey (java.security.PrivateKey)1 X509Certificate (java.security.cert.X509Certificate)1 CertificateEncodingException (javax.security.cert.CertificateEncodingException)1 ConfigurationServiceException (org.nhindirect.config.service.ConfigurationServiceException)1 CertContainer (org.nhindirect.config.ui.DNSController.CertContainer)1 TextParseException (org.xbill.DNS.TextParseException)1