Search in sources :

Example 1 with DGetAlias

use of org.kse.gui.dialogs.DGetAlias 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)

Example 2 with DGetAlias

use of org.kse.gui.dialogs.DGetAlias 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 3 with DGetAlias

use of org.kse.gui.dialogs.DGetAlias 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 4 with DGetAlias

use of org.kse.gui.dialogs.DGetAlias in project keystore-explorer by kaikramer.

the class GenerateKeyPairAction method generateKeyPair.

/**
 * Generate a key pair (with certificate) in the currently opened KeyStore.
 *
 * @param issuerCert
 *                 Issuer certificate for signing the new certificate
 * @param issuerCertChain
 *                 Chain of issuer certificate
 * @param issuerPrivateKey
 *                 Issuer's private key for signing
 * @return Alias of new key pair
 */
public String generateKeyPair(X509Certificate issuerCert, X509Certificate[] issuerCertChain, PrivateKey issuerPrivateKey) {
    String alias = "";
    try {
        int keyPairSize = applicationSettings.getGenerateKeyPairSize();
        KeyPairType keyPairType = applicationSettings.getGenerateKeyPairType();
        KeyStore activeKeyStore = kseFrame.getActiveKeyStore();
        KeyStoreType activeKeyStoreType = KeyStoreType.resolveJce(activeKeyStore.getType());
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        Provider provider = history.getExplicitProvider();
        DGenerateKeyPair dGenerateKeyPair = new DGenerateKeyPair(frame, activeKeyStoreType, keyPairType, keyPairSize);
        dGenerateKeyPair.setLocationRelativeTo(frame);
        dGenerateKeyPair.setVisible(true);
        if (!dGenerateKeyPair.isSuccessful()) {
            return "";
        }
        keyPairType = dGenerateKeyPair.getKeyPairType();
        DGeneratingKeyPair dGeneratingKeyPair;
        if (keyPairType != KeyPairType.EC) {
            keyPairSize = dGenerateKeyPair.getKeyPairSize();
            dGeneratingKeyPair = new DGeneratingKeyPair(frame, keyPairType, keyPairSize, provider);
            applicationSettings.setGenerateKeyPairSize(keyPairSize);
            applicationSettings.setGenerateKeyPairType(keyPairType);
        } else {
            String curveName = dGenerateKeyPair.getCurveName();
            dGeneratingKeyPair = new DGeneratingKeyPair(frame, keyPairType, curveName, provider);
        }
        dGeneratingKeyPair.setLocationRelativeTo(frame);
        dGeneratingKeyPair.startKeyPairGeneration();
        dGeneratingKeyPair.setVisible(true);
        KeyPair keyPair = dGeneratingKeyPair.getKeyPair();
        if (keyPair == null) {
            return "";
        }
        DGenerateKeyPairCert dGenerateKeyPairCert = new DGenerateKeyPairCert(frame, res.getString("GenerateKeyPairAction.GenerateKeyPairCert.Title"), keyPair, keyPairType, issuerCert, issuerPrivateKey, provider);
        dGenerateKeyPairCert.setLocationRelativeTo(frame);
        dGenerateKeyPairCert.setVisible(true);
        X509Certificate certificate = dGenerateKeyPairCert.getCertificate();
        if (certificate == null) {
            return "";
        }
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("GenerateKeyPairAction.NewKeyPairEntryAlias.Title"), X509CertUtil.getCertificateAlias(certificate));
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        alias = dGetAlias.getAlias();
        if (alias == null) {
            return "";
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("GenerateKeyPairAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("GenerateKeyPairAction.NewKeyPairEntryAlias.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return "";
            }
        }
        Password password = new Password((char[]) null);
        KeyStoreType keyStoreType = KeyStoreType.resolveJce(activeKeyStore.getType());
        if (keyStoreType.hasEntryPasswords()) {
            DGetNewPassword dGetNewPassword = new DGetNewPassword(frame, res.getString("GenerateKeyPairAction.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);
        }
        // create new chain with certificates from issuer chain
        X509Certificate[] newCertChain = null;
        if (issuerCertChain != null) {
            newCertChain = new X509Certificate[issuerCertChain.length + 1];
            System.arraycopy(issuerCertChain, 0, newCertChain, 1, issuerCertChain.length);
            newCertChain[0] = certificate;
        } else {
            newCertChain = new X509Certificate[] { certificate };
        }
        keyStore.setKeyEntry(alias, keyPair.getPrivate(), password.toCharArray(), newCertChain);
        newState.setEntryPassword(alias, password);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("GenerateKeyPairAction.KeyPairGenerationSuccessful.message"), res.getString("GenerateKeyPairAction.GenerateKeyPair.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
    return alias;
}
Also used : KeyPair(java.security.KeyPair) DGenerateKeyPair(org.kse.gui.dialogs.DGenerateKeyPair) DGeneratingKeyPair(org.kse.gui.dialogs.DGeneratingKeyPair) KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) DGenerateKeyPairCert(org.kse.gui.dialogs.DGenerateKeyPairCert) X509Certificate(java.security.cert.X509Certificate) Provider(java.security.Provider) DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DGeneratingKeyPair(org.kse.gui.dialogs.DGeneratingKeyPair) KeyPairType(org.kse.crypto.keypair.KeyPairType) DGenerateKeyPair(org.kse.gui.dialogs.DGenerateKeyPair) DGetNewPassword(org.kse.gui.password.DGetNewPassword) DGetNewPassword(org.kse.gui.password.DGetNewPassword) Password(org.kse.crypto.Password)

Example 5 with DGetAlias

use of org.kse.gui.dialogs.DGetAlias in project keystore-explorer by kaikramer.

the class RenameKeyAction 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;
        }
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        Key key = keyStore.getKey(alias, password.toCharArray());
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("RenameKeyAction.NewEntryAlias.Title"), alias);
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String newAlias = dGetAlias.getAlias();
        if (newAlias == null) {
            return;
        }
        if (newAlias.equalsIgnoreCase(alias)) {
            JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("RenameKeyAction.RenameAliasIdentical.message"), alias), res.getString("RenameKeyAction.RenameEntry.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (keyStore.containsAlias(newAlias)) {
            String message = MessageFormat.format(res.getString("RenameKeyAction.OverWriteEntry.message"), newAlias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("RenameKeyAction.RenameEntry.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
            keyStore.deleteEntry(newAlias);
            newState.removeEntryPassword(newAlias);
        }
        keyStore.setKeyEntry(newAlias, key, password.toCharArray(), null);
        newState.setEntryPassword(newAlias, new Password(password));
        keyStore.deleteEntry(alias);
        newState.removeEntryPassword(alias);
        currentState.append(newState);
        kseFrame.updateControls(true);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) Key(java.security.Key) Password(org.kse.crypto.Password)

Aggregations

KeyStore (java.security.KeyStore)10 DGetAlias (org.kse.gui.dialogs.DGetAlias)10 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)10 KeyStoreState (org.kse.utilities.history.KeyStoreState)10 Password (org.kse.crypto.Password)8 X509Certificate (java.security.cert.X509Certificate)6 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)6 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 Certificate (java.security.cert.Certificate)5 PrivateKey (java.security.PrivateKey)4 Key (java.security.Key)2 KeyPair (java.security.KeyPair)1 Provider (java.security.Provider)1 ArrayList (java.util.ArrayList)1 SecretKey (javax.crypto.SecretKey)1 KeyPairType (org.kse.crypto.keypair.KeyPairType)1 SecretKeyType (org.kse.crypto.secretkey.SecretKeyType)1 DGenerateKeyPair (org.kse.gui.dialogs.DGenerateKeyPair)1 DGenerateKeyPairCert (org.kse.gui.dialogs.DGenerateKeyPairCert)1 DGenerateSecretKey (org.kse.gui.dialogs.DGenerateSecretKey)1