Search in sources :

Example 11 with NotInitializedException

use of org.mozilla.jss.NotInitializedException in project jss by dogtagpki.

the class JSSKeyStoreSpi method engineGetCertificateChain.

@Override
public Certificate[] engineGetCertificateChain(String alias) {
    logger.debug("JSSKeyStoreSpi: engineGetCertificateChain(" + alias + ")");
    try {
        logger.debug("JSSKeyStoreSpi: searching for leaf cert");
        CryptoManager cm = CryptoManager.getInstance();
        X509Certificate leaf = cm.findCertByNickname(alias);
        logger.debug("JSSKeyStoreSpi: building cert chain");
        X509Certificate[] certs = cm.buildCertificateChain(leaf);
        Certificate[] chain = new Certificate[certs.length];
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        for (int i = 0; i < certs.length; i++) {
            X509Certificate cert = certs[i];
            logger.debug("JSSKeyStoreSpi: - " + cert.getSubjectDN());
            if (cert instanceof PK11Cert) {
                chain[i] = (PK11Cert) cert;
                continue;
            }
            byte[] bytes = cert.getEncoded();
            InputStream is = new ByteArrayInputStream(bytes);
            chain[i] = certFactory.generateCertificate(is);
        }
        return chain;
    } catch (ObjectNotFoundException e) {
        logger.debug("leaf cert not found: " + alias);
        return null;
    } catch (NotInitializedException e) {
        throw new RuntimeException(e);
    } catch (TokenException e) {
        throw new RuntimeException(e);
    } catch (CertificateException e) {
        throw new RuntimeException(e);
    }
}
Also used : NotInitializedException(org.mozilla.jss.NotInitializedException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CryptoManager(org.mozilla.jss.CryptoManager) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(org.mozilla.jss.crypto.X509Certificate) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectNotFoundException(org.mozilla.jss.crypto.ObjectNotFoundException) NoSuchTokenException(org.mozilla.jss.NoSuchTokenException) NoSuchItemOnTokenException(org.mozilla.jss.crypto.NoSuchItemOnTokenException) TokenException(org.mozilla.jss.crypto.TokenException) PK11Cert(org.mozilla.jss.pkcs11.PK11Cert) Certificate(java.security.cert.Certificate) X509Certificate(org.mozilla.jss.crypto.X509Certificate) TokenCertificate(org.mozilla.jss.crypto.TokenCertificate)

Example 12 with NotInitializedException

use of org.mozilla.jss.NotInitializedException in project jss by dogtagpki.

the class JSSKeyStoreSpi method engineGetCertificate.

@Override
public Certificate engineGetCertificate(String alias) {
    logger.debug("JSSKeyStoreSpi: engineGetCertificate(" + alias + ")");
    try {
        CryptoManager cm = CryptoManager.getInstance();
        X509Certificate cert = cm.findCertByNickname(alias);
        logger.debug("JSSKeyStoreSpi: cert found: " + alias);
        if (cert instanceof PK11Cert) {
            return (PK11Cert) cert;
        }
        byte[] bytes = cert.getEncoded();
        InputStream is = new ByteArrayInputStream(bytes);
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        return certFactory.generateCertificate(is);
    } catch (ObjectNotFoundException e) {
        logger.debug("JSSKeyStoreSpi: cert not found: " + alias);
        return null;
    } catch (NotInitializedException e) {
        throw new RuntimeException(e);
    } catch (TokenException e) {
        throw new RuntimeException(e);
    } catch (CertificateEncodingException e) {
        throw new RuntimeException(e);
    } catch (CertificateException e) {
        throw new RuntimeException(e);
    }
}
Also used : NotInitializedException(org.mozilla.jss.NotInitializedException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CryptoManager(org.mozilla.jss.CryptoManager) CertificateEncodingException(java.security.cert.CertificateEncodingException) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(org.mozilla.jss.crypto.X509Certificate) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectNotFoundException(org.mozilla.jss.crypto.ObjectNotFoundException) NoSuchTokenException(org.mozilla.jss.NoSuchTokenException) NoSuchItemOnTokenException(org.mozilla.jss.crypto.NoSuchItemOnTokenException) TokenException(org.mozilla.jss.crypto.TokenException) PK11Cert(org.mozilla.jss.pkcs11.PK11Cert)

Aggregations

NotInitializedException (org.mozilla.jss.NotInitializedException)12 CryptoManager (org.mozilla.jss.CryptoManager)11 TokenException (org.mozilla.jss.crypto.TokenException)9 NoSuchTokenException (org.mozilla.jss.NoSuchTokenException)7 NoSuchItemOnTokenException (org.mozilla.jss.crypto.NoSuchItemOnTokenException)6 ObjectNotFoundException (org.mozilla.jss.crypto.ObjectNotFoundException)6 X509Certificate (org.mozilla.jss.crypto.X509Certificate)6 CryptoToken (org.mozilla.jss.crypto.CryptoToken)5 CertificateException (java.security.cert.CertificateException)3 CryptoStore (org.mozilla.jss.crypto.CryptoStore)3 PrivateKey (org.mozilla.jss.crypto.PrivateKey)3 PK11Cert (org.mozilla.jss.pkcs11.PK11Cert)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 CertificateFactory (java.security.cert.CertificateFactory)2 ArrayList (java.util.ArrayList)2 SymmetricKey (org.mozilla.jss.crypto.SymmetricKey)2 TokenCertificate (org.mozilla.jss.crypto.TokenCertificate)2 Password (org.mozilla.jss.util.Password)2 CharConversionException (java.io.CharConversionException)1