Search in sources :

Example 1 with DGetNewPassword

use of org.kse.gui.password.DGetNewPassword 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 DGetNewPassword

use of org.kse.gui.password.DGetNewPassword 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 DGetNewPassword

use of org.kse.gui.password.DGetNewPassword 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 DGetNewPassword

use of org.kse.gui.password.DGetNewPassword 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 DGetNewPassword

use of org.kse.gui.password.DGetNewPassword in project keystore-explorer by kaikramer.

the class ImportKeyPairAction method importKeyPairOpenSsl.

private void importKeyPairOpenSsl() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DImportKeyPairOpenSsl dImportKeyPairOpenSsl = new DImportKeyPairOpenSsl(frame);
        dImportKeyPairOpenSsl.setLocationRelativeTo(frame);
        dImportKeyPairOpenSsl.setVisible(true);
        PrivateKey privateKey = dImportKeyPairOpenSsl.getPrivateKey();
        Certificate[] certs = dImportKeyPairOpenSsl.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) DImportKeyPairOpenSsl(org.kse.gui.dialogs.importexport.DImportKeyPairOpenSsl) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) 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)

Aggregations

Password (org.kse.crypto.Password)7 DGetNewPassword (org.kse.gui.password.DGetNewPassword)7 KeyStore (java.security.KeyStore)6 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)6 DGetAlias (org.kse.gui.dialogs.DGetAlias)6 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)6 KeyStoreState (org.kse.utilities.history.KeyStoreState)6 X509Certificate (java.security.cert.X509Certificate)5 PrivateKey (java.security.PrivateKey)4 Certificate (java.security.cert.Certificate)3 KeyPair (java.security.KeyPair)1 Provider (java.security.Provider)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 DGeneratingKeyPair (org.kse.gui.dialogs.DGeneratingKeyPair)1 DImportKeyPairOpenSsl (org.kse.gui.dialogs.importexport.DImportKeyPairOpenSsl)1