Search in sources :

Example 6 with KeyStoreHistory

use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.

the class ExportKeyPairPrivateKeyAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        String alias = kseFrame.getSelectedEntryAlias();
        Password password = getEntryPassword(alias, currentState);
        if (password == null) {
            return;
        }
        KeyStore keyStore = currentState.getKeyStore();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        DExportPrivateKeyType dExportPrivateKeyType = new DExportPrivateKeyType(frame);
        dExportPrivateKeyType.setLocationRelativeTo(frame);
        dExportPrivateKeyType.setVisible(true);
        if (!dExportPrivateKeyType.exportTypeSelected()) {
            return;
        }
        if (dExportPrivateKeyType.exportPkcs8()) {
            exportAsPkcs8(privateKey, alias);
        } else if (dExportPrivateKeyType.exportPvk()) {
            exportAsPvk(privateKey, alias);
        } else {
            exportAsOpenSsl(privateKey, alias);
        }
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PrivateKey(java.security.PrivateKey) DExportPrivateKeyType(org.kse.gui.dialogs.importexport.DExportPrivateKeyType) KeyStore(java.security.KeyStore) CryptoException(org.kse.crypto.CryptoException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Password(org.kse.crypto.Password)

Example 7 with KeyStoreHistory

use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.

the class ExportKeyPairPublicKeyAction method getCertificateChain.

private X509Certificate[] getCertificateChain(String alias) throws CryptoException {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStore keyStore = history.getCurrentState().getKeyStore();
        X509Certificate[] certChain = X509CertUtil.convertCertificates(keyStore.getCertificateChain(alias));
        return certChain;
    } catch (KeyStoreException ex) {
        String message = MessageFormat.format(res.getString("ExportKeyPairPublicKeyAction.NoAccessEntry.message"), alias);
        throw new CryptoException(message, ex);
    }
}
Also used : KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStoreException(java.security.KeyStoreException) CryptoException(org.kse.crypto.CryptoException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Example 8 with KeyStoreHistory

use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.

the class ExportTrustedCertificateAction method getCertificate.

private X509Certificate getCertificate(String alias) throws CryptoException {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStore keyStore = history.getCurrentState().getKeyStore();
        X509Certificate cert = X509CertUtil.convertCertificate(keyStore.getCertificate(alias));
        return cert;
    } catch (KeyStoreException ex) {
        String message = MessageFormat.format(res.getString("ExportTrustedCertificateAction.NoAccessEntry.message"), alias);
        throw new CryptoException(message, ex);
    }
}
Also used : KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStoreException(java.security.KeyStoreException) CryptoException(org.kse.crypto.CryptoException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Example 9 with KeyStoreHistory

use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.

the class ExportTrustedCertificatePublicKeyAction method getPublicKey.

private PublicKey getPublicKey(String alias) throws CryptoException {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStore keyStore = history.getCurrentState().getKeyStore();
        X509Certificate cert = X509CertUtil.convertCertificate(keyStore.getCertificate(alias));
        return cert.getPublicKey();
    } catch (KeyStoreException ex) {
        String message = MessageFormat.format(res.getString("ExportTrustedCertificatePublicKeyAction.NoAccessEntry.message"), alias);
        throw new CryptoException(message, ex);
    }
}
Also used : KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStoreException(java.security.KeyStoreException) CryptoException(org.kse.crypto.CryptoException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Example 10 with KeyStoreHistory

use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.

the class GenerateSecretKeyAction method generateSecret.

/**
 * Generate a secret key in the currently opened KeyStore.
 */
public void generateSecret() {
    try {
        int secretKeySize = applicationSettings.getGenerateSecretKeySize();
        SecretKeyType secretKeyType = applicationSettings.getGenerateSecretKeyType();
        DGenerateSecretKey dGenerateSecretKey = new DGenerateSecretKey(frame, secretKeyType, secretKeySize);
        dGenerateSecretKey.setLocationRelativeTo(frame);
        dGenerateSecretKey.setVisible(true);
        if (!dGenerateSecretKey.isSuccessful()) {
            return;
        }
        secretKeySize = dGenerateSecretKey.getSecretKeySize();
        secretKeyType = dGenerateSecretKey.getSecretKeyType();
        applicationSettings.setGenerateSecretKeySize(secretKeySize);
        applicationSettings.setGenerateSecretKeyType(secretKeyType);
        SecretKey secretKey = SecretKeyUtil.generateSecretKey(secretKeyType, secretKeySize);
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("GenerateSecretKeyAction.NewSecretKeyEntryAlias.Title"), null);
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String alias = dGetAlias.getAlias();
        if (alias == null) {
            return;
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("GenerateSecretKeyAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("GenerateSecretKeyAction.NewSecretKeyEntryAlias.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
        }
        Password password = new Password((char[]) null);
        KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
        if (type.hasEntryPasswords()) {
            DGetNewPassword dGetNewPassword = new DGetNewPassword(frame, res.getString("GenerateSecretKeyAction.NewSecretKeyEntryPassword.Title"), applicationSettings.getPasswordQualityConfig());
            dGetNewPassword.setLocationRelativeTo(frame);
            dGetNewPassword.setVisible(true);
            password = dGetNewPassword.getPassword();
            if (password == null) {
                return;
            }
        }
        if (keyStore.containsAlias(alias)) {
            keyStore.deleteEntry(alias);
            newState.removeEntryPassword(alias);
        }
        keyStore.setKeyEntry(alias, secretKey, password.toCharArray(), null);
        newState.setEntryPassword(alias, password);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("GenerateSecretKeyAction.SecretKeyGenerationSuccessful.message"), res.getString("GenerateSecretKeyAction.GenerateSecretKey.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) DGenerateSecretKey(org.kse.gui.dialogs.DGenerateSecretKey) KeyStore(java.security.KeyStore) DGetAlias(org.kse.gui.dialogs.DGetAlias) DGenerateSecretKey(org.kse.gui.dialogs.DGenerateSecretKey) SecretKey(javax.crypto.SecretKey) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) SecretKeyType(org.kse.crypto.secretkey.SecretKeyType) DGetNewPassword(org.kse.gui.password.DGetNewPassword) DGetNewPassword(org.kse.gui.password.DGetNewPassword) Password(org.kse.crypto.Password)

Aggregations

KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)60 KeyStore (java.security.KeyStore)45 KeyStoreState (org.kse.utilities.history.KeyStoreState)41 Password (org.kse.crypto.Password)31 X509Certificate (java.security.cert.X509Certificate)21 PrivateKey (java.security.PrivateKey)17 Certificate (java.security.cert.Certificate)13 Key (java.security.Key)12 CryptoException (org.kse.crypto.CryptoException)11 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)11 DGetAlias (org.kse.gui.dialogs.DGetAlias)10 KeyStoreException (java.security.KeyStoreException)9 File (java.io.File)7 GeneralSecurityException (java.security.GeneralSecurityException)6 DViewCertificate (org.kse.gui.dialogs.DViewCertificate)6 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 FileNotFoundException (java.io.FileNotFoundException)5 PublicKey (java.security.PublicKey)5 Provider (java.security.Provider)4 ArrayList (java.util.ArrayList)3