Search in sources :

Example 6 with ObjectNotFoundException

use of org.mozilla.jss.crypto.ObjectNotFoundException in project jss by dogtagpki.

the class JSSKeyStoreSpi method engineDeleteEntry.

@Override
public void engineDeleteEntry(String alias) throws KeyStoreException {
    try {
        CryptoManager manager = CryptoManager.getInstance();
        try {
            logger.debug("JSSKeyStoreSpi: searching for cert");
            X509Certificate cert = manager.findCertByNickname(alias);
            CryptoToken token;
            if (cert instanceof TokenCertificate) {
                TokenCertificate tokenCert = (TokenCertificate) cert;
                token = tokenCert.getOwningToken();
            } else {
                token = manager.getInternalKeyStorageToken();
            }
            CryptoStore store = token.getCryptoStore();
            logger.debug("JSSKeyStoreSpi: deleting cert: " + alias);
            store.deleteCertOnly(cert);
            return;
        } catch (ObjectNotFoundException e) {
            logger.debug("JSSKeyStoreSpi: cert not found, searching for key");
        }
        String[] parts = parseAlias(alias);
        String tokenName = parts[0];
        String nickname = parts[1];
        CryptoToken token;
        if (tokenName == null) {
            token = manager.getInternalKeyStorageToken();
        } else {
            token = manager.getTokenByName(tokenName);
        }
        CryptoStore store = token.getCryptoStore();
        logger.debug("JSSKeyStoreSpi: searching for private key");
        for (PrivateKey privateKey : store.getPrivateKeys()) {
            // convert key ID into hexadecimal
            String keyID = Utils.HexEncode(privateKey.getUniqueID());
            logger.debug("JSSKeyStoreSpi: - " + keyID);
            if (!nickname.equals(keyID)) {
                continue;
            }
            try {
                logger.debug("JSSKeyStoreSpi: searching for public key: " + nickname);
                PublicKey publicKey = store.findPublicKey(privateKey);
                logger.debug("JSSKeyStoreSpi: deleting public key: " + nickname);
                store.deletePublicKey(publicKey);
            } catch (ObjectNotFoundException e) {
                logger.debug("JSSKeyStoreSpi: public key not found: " + nickname);
            }
            logger.debug("JSSKeyStoreSpi: deleting private key: " + nickname);
            store.deletePrivateKey(privateKey);
            return;
        }
        logger.debug("JSSKeyStoreSpi: entry not found: " + alias);
        throw new KeyStoreException("Entry not found: " + alias);
    } catch (NotInitializedException e) {
        throw new KeyStoreException(e);
    } catch (NoSuchTokenException e) {
        throw new KeyStoreException(e);
    } catch (TokenException e) {
        throw new KeyStoreException(e);
    } catch (NoSuchItemOnTokenException e) {
        throw new KeyStoreException(e);
    }
}
Also used : CryptoToken(org.mozilla.jss.crypto.CryptoToken) PrivateKey(org.mozilla.jss.crypto.PrivateKey) NotInitializedException(org.mozilla.jss.NotInitializedException) PublicKey(java.security.PublicKey) NoSuchItemOnTokenException(org.mozilla.jss.crypto.NoSuchItemOnTokenException) CryptoManager(org.mozilla.jss.CryptoManager) KeyStoreException(java.security.KeyStoreException) X509Certificate(org.mozilla.jss.crypto.X509Certificate) CryptoStore(org.mozilla.jss.crypto.CryptoStore) NoSuchTokenException(org.mozilla.jss.NoSuchTokenException) ObjectNotFoundException(org.mozilla.jss.crypto.ObjectNotFoundException) NoSuchTokenException(org.mozilla.jss.NoSuchTokenException) NoSuchItemOnTokenException(org.mozilla.jss.crypto.NoSuchItemOnTokenException) TokenException(org.mozilla.jss.crypto.TokenException) TokenCertificate(org.mozilla.jss.crypto.TokenCertificate)

Example 7 with ObjectNotFoundException

use of org.mozilla.jss.crypto.ObjectNotFoundException in project jss by dogtagpki.

the class JSSKeyStoreSpi method engineIsCertificateEntry.

/**
 * Returns true if there is a cert with this nickname but there is no
 * key associated with the cert.
 */
@Override
public boolean engineIsCertificateEntry(String alias) {
    logger.debug("JSSKeyStoreSpi: engineIsCertificateEntry(" + alias + ")");
    try {
        CryptoManager cm = CryptoManager.getInstance();
        cm.findCertByNickname(alias);
        logger.debug("JSSKeyStoreSpi: cert found: " + alias);
        return true;
    } catch (ObjectNotFoundException e) {
        logger.debug("JSSKeyStoreSpi: cert not found: " + alias);
        return false;
    } catch (NotInitializedException e) {
        throw new RuntimeException(e);
    } catch (TokenException e) {
        throw new RuntimeException(e);
    }
}
Also used : NotInitializedException(org.mozilla.jss.NotInitializedException) ObjectNotFoundException(org.mozilla.jss.crypto.ObjectNotFoundException) NoSuchTokenException(org.mozilla.jss.NoSuchTokenException) NoSuchItemOnTokenException(org.mozilla.jss.crypto.NoSuchItemOnTokenException) TokenException(org.mozilla.jss.crypto.TokenException) CryptoManager(org.mozilla.jss.CryptoManager)

Example 8 with ObjectNotFoundException

use of org.mozilla.jss.crypto.ObjectNotFoundException 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 9 with ObjectNotFoundException

use of org.mozilla.jss.crypto.ObjectNotFoundException 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

ObjectNotFoundException (org.mozilla.jss.crypto.ObjectNotFoundException)9 CryptoManager (org.mozilla.jss.CryptoManager)7 TokenException (org.mozilla.jss.crypto.TokenException)7 X509Certificate (org.mozilla.jss.crypto.X509Certificate)7 NotInitializedException (org.mozilla.jss.NotInitializedException)6 NoSuchItemOnTokenException (org.mozilla.jss.crypto.NoSuchItemOnTokenException)6 NoSuchTokenException (org.mozilla.jss.NoSuchTokenException)5 CryptoToken (org.mozilla.jss.crypto.CryptoToken)3 PrivateKey (org.mozilla.jss.crypto.PrivateKey)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 PublicKey (java.security.PublicKey)2 CertificateException (java.security.cert.CertificateException)2 CertificateFactory (java.security.cert.CertificateFactory)2 CryptoStore (org.mozilla.jss.crypto.CryptoStore)2 TokenCertificate (org.mozilla.jss.crypto.TokenCertificate)2 PK11Cert (org.mozilla.jss.pkcs11.PK11Cert)2 BigInteger (java.math.BigInteger)1 KeyStoreException (java.security.KeyStoreException)1 SecureRandom (java.security.SecureRandom)1