Search in sources :

Example 6 with KeyStoreType

use of org.kse.crypto.keystore.KeyStoreType 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 7 with KeyStoreType

use of org.kse.crypto.keystore.KeyStoreType 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 8 with KeyStoreType

use of org.kse.crypto.keystore.KeyStoreType in project keystore-explorer by kaikramer.

the class OpenCaCertificatesAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    File caCertificatesFile = applicationSettings.getCaCertificatesFile();
    if (caCertificatesFile.isFile()) {
        openKeyStore(caCertificatesFile, AuthorityCertificates.CACERTS_DEFAULT_PWD);
        return;
    }
    int selected = JOptionPane.showConfirmDialog(frame, res.getString("OpenCaCertificatesAction.NoCaCertificatesKeyStoreCreate.message"), res.getString("OpenCaCertificatesAction.OpenCaCertificatesKeyStore.Title"), JOptionPane.YES_NO_OPTION);
    if (selected != JOptionPane.YES_OPTION) {
        return;
    }
    try {
        DNewKeyStoreType dNewKeyStoreType = new DNewKeyStoreType(frame);
        dNewKeyStoreType.setLocationRelativeTo(frame);
        dNewKeyStoreType.setVisible(true);
        KeyStoreType keyStoreType = dNewKeyStoreType.getKeyStoreType();
        if (keyStoreType == null) {
            return;
        }
        Password password = getNewKeyStorePassword();
        if (password == null) {
            return;
        }
        KeyStore caCertificatesKeyStore = KeyStoreUtil.create(keyStoreType);
        KeyStoreUtil.save(caCertificatesKeyStore, caCertificatesFile, password);
        kseFrame.addKeyStore(caCertificatesKeyStore, caCertificatesFile, password);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : DNewKeyStoreType(org.kse.gui.dialogs.DNewKeyStoreType) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DNewKeyStoreType(org.kse.gui.dialogs.DNewKeyStoreType) File(java.io.File) KeyStore(java.security.KeyStore) Password(org.kse.crypto.Password)

Example 9 with KeyStoreType

use of org.kse.crypto.keystore.KeyStoreType in project keystore-explorer by kaikramer.

the class KseFrame method dragSelectedEntry.

/**
 * Get a the selected entry as a drag entry for DnD.
 *
 * @return Drag entry or null if entry could not be dragged
 */
public DragEntry dragSelectedEntry() {
    try {
        KeyStoreHistory history = getActiveKeyStoreHistory();
        if (history == null) {
            // No KeyStore to drag from
            return null;
        }
        KeyStoreState currentState = history.getCurrentState();
        KeyStore keyStore = currentState.getKeyStore();
        String alias = getSelectedEntryAlias();
        KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
        if (alias == null) {
            // No selected entry to drag
            return null;
        }
        if (KeyStoreUtil.isKeyEntry(alias, keyStore)) {
            JOptionPane.showMessageDialog(frame, res.getString("KseFrame.NoDragKeyEntry.message"), KSE.getApplicationName(), JOptionPane.WARNING_MESSAGE);
            return null;
        }
        if (KeyStoreUtil.isKeyPairEntry(alias, keyStore) && type.hasExportablePrivateKeys()) {
            // Otherwise entry must already be unlocked to get password
            Password password = currentState.getEntryPassword(alias);
            if (password == null && type.hasEntryPasswords()) {
                JOptionPane.showMessageDialog(frame, res.getString("KseFrame.NoDragLockedKeyPairEntry.message"), KSE.getApplicationName(), JOptionPane.WARNING_MESSAGE);
                return null;
            }
            PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
            Certificate[] certificateChain = keyStore.getCertificateChain(alias);
            return new DragKeyPairEntry(alias, privateKey, password, certificateChain);
        } else {
            Certificate trustedCertificate = keyStore.getCertificate(alias);
            return new DragTrustedCertificateEntry(alias, trustedCertificate);
        }
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return null;
    }
}
Also used : DragKeyPairEntry(org.kse.gui.dnd.DragKeyPairEntry) KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DragTrustedCertificateEntry(org.kse.gui.dnd.DragTrustedCertificateEntry) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.kse.crypto.CryptoException) Password(org.kse.crypto.Password) Certificate(java.security.cert.Certificate)

Example 10 with KeyStoreType

use of org.kse.crypto.keystore.KeyStoreType in project keystore-explorer by kaikramer.

the class KseFrame method updateControls.

/**
 * Update the frame's controls dependent on the state of its open and active
 * KeyStores.
 *
 * @param keyStoreContentsChanged
 *            Have the active KeyStore's contents changed?
 */
public void updateControls(boolean keyStoreContentsChanged) {
    KeyStoreHistory history = getActiveKeyStoreHistory();
    if (history == null) {
        updateControlsNoKeyStoresOpen();
        return;
    }
    KeyStoreState currentState = history.getCurrentState();
    // Reload KeyStore in table if it has changed
    if (keyStoreContentsChanged) {
        try {
            String selectedAlias = getSelectedEntryAlias();
            ((KeyStoreTableModel) getActiveKeyStoreTable().getModel()).load(history);
            // Loading the model loses the selected entry - preserve it
            if (selectedAlias != null) {
                setSelectedEntryByAlias(selectedAlias);
            }
        } catch (GeneralSecurityException ex) {
            DError.displayError(frame, ex);
        } catch (CryptoException ex) {
            DError.displayError(frame, ex);
        }
    }
    // Can save if active KeyStore has not been saved
    if (!currentState.isSavedState()) {
        saveAction.setEnabled(true);
    } else {
        saveAction.setEnabled(false);
    }
    // Can save all if any KeyStore has been changed since saved
    boolean saveAll = false;
    for (int i = 0; i < histories.size(); i++) {
        KeyStoreHistory h = histories.get(i);
        if (!h.getCurrentState().isSavedState()) {
            // Yes - can Save All
            saveAll = true;
            break;
        }
    }
    saveAllAction.setEnabled(saveAll);
    // Can close
    closeAction.setEnabled(true);
    closeAllAction.setEnabled(true);
    // Can close others?
    if (jkstpKeyStores.getTabCount() > 1) {
        closeOthersAction.setEnabled(true);
    } else {
        closeOthersAction.setEnabled(false);
    }
    KeyStore keyStore = currentState.getKeyStore();
    KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
    // Can Save As
    if (type.isFileBased()) {
        saveAsAction.setEnabled(true);
    }
    // May be able to undo/redo
    updateUndoRedoControls(currentState);
    // May be able to cut/copy/paste
    if (type.isFileBased()) {
        updateCutCopyPasteControls();
    }
    // Can use tools on
    generateKeyPairAction.setEnabled(true);
    generateSecretKeyAction.setEnabled(type.supportsKeyEntries());
    importTrustedCertificateAction.setEnabled(true);
    importKeyPairAction.setEnabled(true);
    propertiesAction.setEnabled(true);
    if (type.isFileBased()) {
        setPasswordAction.setEnabled(true);
    }
    // Show default status bar display
    setDefaultStatusBarText();
    // Passwords, and therefore unlocking, are not relevant for PKCS #12 or KeyStores that are not file-based
    if (!type.hasEntryPasswords() || !type.isFileBased()) {
        unlockKeyPairAction.setEnabled(false);
        setKeyPairPasswordAction.setEnabled(false);
        unlockKeyAction.setEnabled(false);
        setKeyPasswordAction.setEnabled(false);
    } else {
        unlockKeyPairAction.setEnabled(true);
        setKeyPairPasswordAction.setEnabled(true);
        unlockKeyAction.setEnabled(true);
        setKeyPasswordAction.setEnabled(true);
    }
    // Special restrictions for MSCAPI and PKCS#11 type
    if (type == KeyStoreType.MS_CAPI_PERSONAL || type == KeyStoreType.PKCS11) {
        keyPairPrivateKeyDetailsAction.setEnabled(false);
        keyDetailsAction.setEnabled(false);
        renameKeyAction.setEnabled(false);
        renameKeyPairAction.setEnabled(false);
        renameTrustedCertificateAction.setEnabled(false);
        exportKeyPairAction.setEnabled(false);
        exportKeyPairPrivateKeyAction.setEnabled(false);
        jmKeyPairEditCertChain.setEnabled(false);
        appendToCertificateChainAction.setEnabled(false);
        removeFromCertificateChainAction.setEnabled(false);
        // "UnsupportedOperationException" ...
        jmKeyPairImportCaReply.setEnabled(false);
    } else {
        keyPairPrivateKeyDetailsAction.setEnabled(true);
        keyDetailsAction.setEnabled(true);
        renameKeyAction.setEnabled(true);
        renameKeyPairAction.setEnabled(true);
        renameTrustedCertificateAction.setEnabled(true);
        exportKeyPairAction.setEnabled(true);
        exportKeyPairPrivateKeyAction.setEnabled(true);
        jmKeyPairEditCertChain.setEnabled(true);
        appendToCertificateChainAction.setEnabled(true);
        removeFromCertificateChainAction.setEnabled(true);
        jmKeyPairImportCaReply.setEnabled(true);
    }
    // KeyStore type menu items
    if (type.isFileBased()) {
        jmChangeType.setEnabled(true);
        if (type == JKS) {
            jrbmiChangeTypeJks.setSelected(true);
            jrbmiKeyStoreChangeTypeJks.setSelected(true);
        } else if (type == JCEKS) {
            jrbmiChangeTypeJceks.setSelected(true);
            jrbmiKeyStoreChangeTypeJceks.setSelected(true);
        } else if (type == PKCS12) {
            jrbmiChangeTypePkcs12.setSelected(true);
            jrbmiKeyStoreChangeTypePkcs12.setSelected(true);
        } else if (type == BKS_V1) {
            jrbmiChangeTypeBksV1.setSelected(true);
            jrbmiKeyStoreChangeTypeBksV1.setSelected(true);
        } else if (type == BKS) {
            jrbmiChangeTypeBks.setSelected(true);
            jrbmiKeyStoreChangeTypeBks.setSelected(true);
        } else {
            jrbmiChangeTypeUber.setSelected(true);
            jrbmiKeyStoreChangeTypeUber.setSelected(true);
        }
    } else {
        jmKeyStoreChangeType.setEnabled(false);
    }
    // Show KeyStores tabbed pane
    frame.getContentPane().remove(jQuickStart);
    frame.getContentPane().add(jkstpKeyStores, BorderLayout.CENTER);
    updateKeyStoreTabsText();
    updateApplicationTitle();
    frame.repaint();
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.kse.crypto.CryptoException) KeyStore(java.security.KeyStore) Point(java.awt.Point)

Aggregations

KeyStoreType (org.kse.crypto.keystore.KeyStoreType)19 KeyStore (java.security.KeyStore)16 KeyStoreState (org.kse.utilities.history.KeyStoreState)13 Password (org.kse.crypto.Password)12 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)11 X509Certificate (java.security.cert.X509Certificate)7 PrivateKey (java.security.PrivateKey)6 DGetAlias (org.kse.gui.dialogs.DGetAlias)6 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 Certificate (java.security.cert.Certificate)4 Point (java.awt.Point)3 File (java.io.File)3 Key (java.security.Key)3 KeyStoreException (java.security.KeyStoreException)3 DNewKeyStoreType (org.kse.gui.dialogs.DNewKeyStoreType)3 FileNotFoundException (java.io.FileNotFoundException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 ArrayList (java.util.ArrayList)2 CryptoException (org.kse.crypto.CryptoException)2 DViewCertificate (org.kse.gui.dialogs.DViewCertificate)2