Search in sources :

Example 16 with KeyStoreState

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

the class ImportKeyPairAction method importKeyPairPkcs8.

private void importKeyPairPkcs8() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DImportKeyPairPkcs8 dImportKeyPairPkcs8 = new DImportKeyPairPkcs8(frame);
        dImportKeyPairPkcs8.setLocationRelativeTo(frame);
        dImportKeyPairPkcs8.setVisible(true);
        PrivateKey privateKey = dImportKeyPairPkcs8.getPrivateKey();
        Certificate[] certs = dImportKeyPairPkcs8.getCertificateChain();
        if ((privateKey == null) || (certs == null)) {
            return;
        }
        X509Certificate[] x509Certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.Title"), X509CertUtil.getCertificateAlias(x509Certs[0]));
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String alias = dGetAlias.getAlias();
        if (alias == null) {
            return;
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("ImportKeyPairAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.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("ImportKeyPairAction.NewKeyPairEntryPassword.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, privateKey, password.toCharArray(), x509Certs);
        newState.setEntryPassword(alias, password);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("ImportKeyPairAction.KeyPairImportSuccessful.message"), res.getString("ImportKeyPairAction.ImportKeyPair.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) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DImportKeyPairPkcs8(org.kse.gui.dialogs.importexport.DImportKeyPairPkcs8) DGetNewPassword(org.kse.gui.password.DGetNewPassword) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) DGetNewPassword(org.kse.gui.password.DGetNewPassword) Password(org.kse.crypto.Password)

Example 17 with KeyStoreState

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

the class ImportKeyPairAction method importKeyPairPkcs12.

private void importKeyPairPkcs12() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DImportKeyPairPkcs12 dImportKeyPairPkcs12 = new DImportKeyPairPkcs12(frame);
        dImportKeyPairPkcs12.setLocationRelativeTo(frame);
        dImportKeyPairPkcs12.setVisible(true);
        PrivateKey privKey = dImportKeyPairPkcs12.getPrivateKey();
        X509Certificate[] certs = dImportKeyPairPkcs12.getCertificateChain();
        if ((privKey == null) || (certs == null)) {
            return;
        }
        X509Certificate[] x509Certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.Title"), X509CertUtil.getCertificateAlias(x509Certs[0]));
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String alias = dGetAlias.getAlias();
        if (alias == null) {
            return;
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("ImportKeyPairAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.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("ImportKeyPairAction.NewKeyPairEntryPassword.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, privKey, password.toCharArray(), x509Certs);
        newState.setEntryPassword(alias, password);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("ImportKeyPairAction.KeyPairImportSuccessful.message"), res.getString("ImportKeyPairAction.ImportKeyPair.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) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DImportKeyPairPkcs12(org.kse.gui.dialogs.importexport.DImportKeyPairPkcs12) DGetNewPassword(org.kse.gui.password.DGetNewPassword) DGetNewPassword(org.kse.gui.password.DGetNewPassword) Password(org.kse.crypto.Password)

Example 18 with KeyStoreState

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

the class KeyDetailsAction method showKeySelectedEntry.

/**
 * Show the key details of the selected KeyStore entry.
 */
public void showKeySelectedEntry() {
    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();
        Key key = keyStore.getKey(alias, password.toCharArray());
        if (key instanceof SecretKey) {
            SecretKey secretKey = (SecretKey) key;
            DViewSecretKey dViewSecretKey = new DViewSecretKey(frame, MessageFormat.format(res.getString("KeyDetailsAction.SecretKeyDetailsEntry.Title"), alias), secretKey);
            dViewSecretKey.setLocationRelativeTo(frame);
            dViewSecretKey.setVisible(true);
        } else if (key instanceof PrivateKey) {
            PrivateKey privateKey = (PrivateKey) key;
            DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, MessageFormat.format(res.getString("KeyDetailsAction.PrivateKeyDetailsEntry.Title"), alias), privateKey, history.getExplicitProvider());
            dViewPrivateKey.setLocationRelativeTo(frame);
            dViewPrivateKey.setVisible(true);
        } else if (key instanceof PublicKey) {
            PublicKey publicKey = (PublicKey) key;
            DViewPublicKey dViewPublicKey = new DViewPublicKey(frame, MessageFormat.format(res.getString("KeyDetailsAction.PublicKeyDetailsEntry.Title"), alias), publicKey);
            dViewPublicKey.setLocationRelativeTo(frame);
            dViewPublicKey.setVisible(true);
        }
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) DViewSecretKey(org.kse.gui.dialogs.DViewSecretKey) PrivateKey(java.security.PrivateKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) PublicKey(java.security.PublicKey) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) KeyStore(java.security.KeyStore) DViewSecretKey(org.kse.gui.dialogs.DViewSecretKey) SecretKey(javax.crypto.SecretKey) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) DViewSecretKey(org.kse.gui.dialogs.DViewSecretKey) PublicKey(java.security.PublicKey) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) Password(org.kse.crypto.Password)

Example 19 with KeyStoreState

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

the class KeyPairPrivateKeyDetailsAction 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 privKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, MessageFormat.format(res.getString("KeyPairPrivateKeyDetailsAction.PrivKeyDetailsEntry.Title"), alias), privKey, history.getExplicitProvider());
        dViewPrivateKey.setLocationRelativeTo(frame);
        dViewPrivateKey.setVisible(true);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) KeyStore(java.security.KeyStore) Password(org.kse.crypto.Password)

Example 20 with KeyStoreState

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

the class SaveAction method saveKeyStore.

/**
 * Save the supplied KeyStore back to the file it was originally opened
 * from.
 *
 * @param history
 *            KeyStore history
 * @return True if the KeyStore is saved to disk, false otherwise
 */
protected boolean saveKeyStore(KeyStoreHistory history) {
    File saveFile = null;
    try {
        KeyStoreState currentState = history.getCurrentState();
        kseFrame.focusOnKeyStore(currentState.getKeyStore());
        saveFile = history.getFile();
        if (saveFile == null) {
            return saveKeyStoreAs(history);
        }
        Password password = currentState.getPassword();
        if (password == null) {
            SetPasswordAction setPasswordAction = new SetPasswordAction(kseFrame);
            if (setPasswordAction.setKeyStorePassword()) {
                currentState = history.getCurrentState();
                password = currentState.getPassword();
            } else {
                return false;
            }
        }
        KeyStoreUtil.save(currentState.getKeyStore(), saveFile, password);
        currentState.setPassword(password);
        currentState.setAsSavedState();
        kseFrame.updateControls(false);
        return true;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SaveAction.NoWriteFile.message"), saveFile), res.getString("SaveAction.SaveKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
        return false;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return false;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Password(org.kse.crypto.Password)

Aggregations

KeyStoreState (org.kse.utilities.history.KeyStoreState)48 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)41 KeyStore (java.security.KeyStore)39 Password (org.kse.crypto.Password)32 PrivateKey (java.security.PrivateKey)17 X509Certificate (java.security.cert.X509Certificate)15 Certificate (java.security.cert.Certificate)13 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)13 Key (java.security.Key)12 DGetAlias (org.kse.gui.dialogs.DGetAlias)10 File (java.io.File)9 FileNotFoundException (java.io.FileNotFoundException)7 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 PublicKey (java.security.PublicKey)5 CryptoException (org.kse.crypto.CryptoException)5 GeneralSecurityException (java.security.GeneralSecurityException)4 KeyStoreException (java.security.KeyStoreException)4 Provider (java.security.Provider)4 ArrayList (java.util.ArrayList)3 KeyPairType (org.kse.crypto.keypair.KeyPairType)3