Search in sources :

Example 1 with TrustedCertificateBufferEntry

use of org.kse.utilities.buffer.TrustedCertificateBufferEntry in project keystore-explorer by kaikramer.

the class CopyAction method bufferSelectedEntry.

private BufferEntry bufferSelectedEntry() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        String alias = kseFrame.getSelectedEntryAlias();
        if (alias == null) {
            return null;
        }
        BufferEntry bufferEntry = null;
        KeyStore keyStore = currentState.getKeyStore();
        if (KeyStoreUtil.isKeyEntry(alias, keyStore)) {
            Password password = getEntryPassword(alias, currentState);
            if (password == null) {
                return null;
            }
            Key key = keyStore.getKey(alias, password.toCharArray());
            if (key instanceof PrivateKey) {
                JOptionPane.showMessageDialog(frame, res.getString("CopyAction.NoCopyKeyEntryWithPrivateKey.message"), res.getString("CopyAction.Copy.Title"), JOptionPane.WARNING_MESSAGE);
                return null;
            }
            bufferEntry = new KeyBufferEntry(alias, false, key, password);
        } else if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
            Certificate certificate = keyStore.getCertificate(alias);
            bufferEntry = new TrustedCertificateBufferEntry(alias, false, certificate);
        } else if (KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
            Password password = getEntryPassword(alias, currentState);
            if (password == null) {
                return null;
            }
            PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
            Certificate[] certificateChain = keyStore.getCertificateChain(alias);
            bufferEntry = new KeyPairBufferEntry(alias, false, privateKey, password, certificateChain);
        }
        return bufferEntry;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return null;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) KeyBufferEntry(org.kse.utilities.buffer.KeyBufferEntry) KeyPairBufferEntry(org.kse.utilities.buffer.KeyPairBufferEntry) TrustedCertificateBufferEntry(org.kse.utilities.buffer.TrustedCertificateBufferEntry) BufferEntry(org.kse.utilities.buffer.BufferEntry) KeyPairBufferEntry(org.kse.utilities.buffer.KeyPairBufferEntry) KeyBufferEntry(org.kse.utilities.buffer.KeyBufferEntry) TrustedCertificateBufferEntry(org.kse.utilities.buffer.TrustedCertificateBufferEntry) Key(java.security.Key) PrivateKey(java.security.PrivateKey) Password(org.kse.crypto.Password) Certificate(java.security.cert.Certificate)

Example 2 with TrustedCertificateBufferEntry

use of org.kse.utilities.buffer.TrustedCertificateBufferEntry in project keystore-explorer by kaikramer.

the class CutAction method bufferSelectedEntry.

private BufferEntry bufferSelectedEntry() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        String alias = kseFrame.getSelectedEntryAlias();
        if (alias == null) {
            return null;
        }
        BufferEntry bufferEntry = null;
        KeyStore keyStore = currentState.getKeyStore();
        if (KeyStoreUtil.isKeyEntry(alias, keyStore)) {
            Password password = getEntryPassword(alias, currentState);
            if (password == null) {
                return null;
            }
            Key key = keyStore.getKey(alias, password.toCharArray());
            if (key instanceof PrivateKey) {
                JOptionPane.showMessageDialog(frame, res.getString("CutAction.NoCutKeyEntryWithPrivateKey.message"), res.getString("CutAction.Cut.Title"), JOptionPane.WARNING_MESSAGE);
                return null;
            }
            bufferEntry = new KeyBufferEntry(alias, true, key, password);
        } else if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
            Certificate certificate = keyStore.getCertificate(alias);
            bufferEntry = new TrustedCertificateBufferEntry(alias, true, certificate);
        } else if (KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
            Password password = getEntryPassword(alias, currentState);
            if (password == null) {
                return null;
            }
            PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
            Certificate[] certificateChain = keyStore.getCertificateChain(alias);
            bufferEntry = new KeyPairBufferEntry(alias, true, privateKey, password, certificateChain);
        }
        KeyStoreState newState = currentState.createBasisForNextState(this);
        keyStore = newState.getKeyStore();
        keyStore.deleteEntry(alias);
        newState.removeEntryPassword(alias);
        currentState.append(newState);
        return bufferEntry;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return null;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) KeyBufferEntry(org.kse.utilities.buffer.KeyBufferEntry) KeyPairBufferEntry(org.kse.utilities.buffer.KeyPairBufferEntry) TrustedCertificateBufferEntry(org.kse.utilities.buffer.TrustedCertificateBufferEntry) BufferEntry(org.kse.utilities.buffer.BufferEntry) KeyPairBufferEntry(org.kse.utilities.buffer.KeyPairBufferEntry) KeyBufferEntry(org.kse.utilities.buffer.KeyBufferEntry) TrustedCertificateBufferEntry(org.kse.utilities.buffer.TrustedCertificateBufferEntry) Key(java.security.Key) PrivateKey(java.security.PrivateKey) Password(org.kse.crypto.Password) Certificate(java.security.cert.Certificate)

Example 3 with TrustedCertificateBufferEntry

use of org.kse.utilities.buffer.TrustedCertificateBufferEntry in project keystore-explorer by kaikramer.

the class PasteAction method pasteEntry.

private boolean pasteEntry(BufferEntry bufferEntry) {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        String alias = bufferEntry.getName();
        if (keyStore.containsAlias(alias)) {
            if (bufferEntry.isCut()) {
                int selected = JOptionPane.showConfirmDialog(frame, MessageFormat.format(res.getString("PasteAction.PasteExistsReplace.message"), alias), res.getString("PasteAction.Paste.Title"), JOptionPane.YES_NO_OPTION);
                if (selected != JOptionPane.YES_OPTION) {
                    return false;
                }
                keyStore.deleteEntry(alias);
                newState.removeEntryPassword(alias);
            } else {
                alias = getUniqueEntryName(alias, keyStore);
            }
        }
        if (bufferEntry instanceof KeyBufferEntry) {
            KeyStoreType keyStoreType = KeyStoreType.resolveJce(keyStore.getType());
            if (!keyStoreType.supportsKeyEntries()) {
                JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("PasteAction.NoPasteKeyEntry.message"), keyStoreType.friendly()), res.getString("PasteAction.Paste.Title"), JOptionPane.WARNING_MESSAGE);
                return false;
            }
            KeyBufferEntry keyBufferEntry = (KeyBufferEntry) bufferEntry;
            Key key = keyBufferEntry.getKey();
            Password password = keyBufferEntry.getPassword();
            keyStore.setKeyEntry(alias, key, password.toCharArray(), null);
            newState.setEntryPassword(alias, password);
        } else if (bufferEntry instanceof KeyPairBufferEntry) {
            KeyPairBufferEntry keyPairBufferEntry = (KeyPairBufferEntry) bufferEntry;
            PrivateKey privateKey = keyPairBufferEntry.getPrivateKey();
            Password password = keyPairBufferEntry.getPassword();
            Certificate[] certificateChain = keyPairBufferEntry.getCertificateChain();
            keyStore.setKeyEntry(alias, privateKey, password.toCharArray(), certificateChain);
            newState.setEntryPassword(alias, password);
        } else {
            TrustedCertificateBufferEntry certBufferEntry = (TrustedCertificateBufferEntry) bufferEntry;
            keyStore.setCertificateEntry(alias, certBufferEntry.getTrustedCertificate());
        }
        if (bufferEntry.isCut()) {
            Buffer.clear();
        }
        currentState.append(newState);
        kseFrame.updateControls(true);
        return true;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return false;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) KeyBufferEntry(org.kse.utilities.buffer.KeyBufferEntry) KeyPairBufferEntry(org.kse.utilities.buffer.KeyPairBufferEntry) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) TrustedCertificateBufferEntry(org.kse.utilities.buffer.TrustedCertificateBufferEntry) Key(java.security.Key) PrivateKey(java.security.PrivateKey) Password(org.kse.crypto.Password)

Aggregations

Key (java.security.Key)3 KeyStore (java.security.KeyStore)3 PrivateKey (java.security.PrivateKey)3 Password (org.kse.crypto.Password)3 KeyBufferEntry (org.kse.utilities.buffer.KeyBufferEntry)3 KeyPairBufferEntry (org.kse.utilities.buffer.KeyPairBufferEntry)3 TrustedCertificateBufferEntry (org.kse.utilities.buffer.TrustedCertificateBufferEntry)3 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)3 KeyStoreState (org.kse.utilities.history.KeyStoreState)3 Certificate (java.security.cert.Certificate)2 BufferEntry (org.kse.utilities.buffer.BufferEntry)2 KeyStoreException (java.security.KeyStoreException)1 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)1