Search in sources :

Example 11 with KeyStoreState

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

the class CloseAction method closeKeyStore.

/**
 * Close the supplied KeyStore. Allow the user to save it if there are
 * unsaved changes.
 *
 * @param history
 *            KeyStore history
 * @return True if the KeyStore is closed, false otherwise
 */
public boolean closeKeyStore(KeyStoreHistory history) {
    KeyStoreState currentState = history.getCurrentState();
    if (needSave(currentState)) {
        kseFrame.focusOnKeyStore(currentState.getKeyStore());
        int wantSave = wantSave(history);
        if (wantSave == JOptionPane.YES_OPTION) {
            boolean saved = saveKeyStore(history);
            if (!saved) {
                return false;
            }
            // Current state may have changed with the addition of a
            // KeyStore password during
            // save
            currentState = history.getCurrentState();
        } else if ((wantSave == JOptionPane.CANCEL_OPTION) || (wantSave == JOptionPane.CLOSED_OPTION)) {
            return false;
        }
    }
    kseFrame.removeKeyStore(currentState.getKeyStore());
    kseFrame.updateControls(true);
    return true;
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState)

Example 12 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState 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 13 with KeyStoreState

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

the class TrustedCertificatePublicKeyDetailsAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStore keyStore = currentState.getKeyStore();
        String alias = kseFrame.getSelectedEntryAlias();
        PublicKey pubKey = keyStore.getCertificate(alias).getPublicKey();
        DViewPublicKey dViewPublicKey = new DViewPublicKey(frame, MessageFormat.format(res.getString("TrustedCertificatePublicKeyDetailsAction.PubKeyDetailsEntry.Title"), alias), pubKey);
        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) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) PublicKey(java.security.PublicKey) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) KeyStore(java.security.KeyStore)

Example 14 with KeyStoreState

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

the class SignCsrAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    FileOutputStream fos = null;
    File caReplyFile = null;
    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 privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        Certificate[] certs = keyStore.getCertificateChain(alias);
        KeyPairType keyPairType = KeyPairUtil.getKeyPairType(privateKey);
        File csrFile = chooseCsrFile();
        if (csrFile == null) {
            return;
        }
        PKCS10CertificationRequest pkcs10Csr = null;
        Spkac spkacCsr = null;
        try {
            CryptoFileType fileType = CryptoFileUtil.detectFileType(new FileInputStream(csrFile));
            if (fileType == CryptoFileType.PKCS10_CSR) {
                pkcs10Csr = Pkcs10Util.loadCsr(new FileInputStream(csrFile));
                if (!Pkcs10Util.verifyCsr(pkcs10Csr)) {
                    JOptionPane.showMessageDialog(frame, res.getString("SignCsrAction.NoVerifyPkcs10Csr.message"), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
                    return;
                }
            } else if (fileType == CryptoFileType.SPKAC_CSR) {
                spkacCsr = new Spkac(new FileInputStream(csrFile));
                if (!spkacCsr.verify()) {
                    JOptionPane.showMessageDialog(frame, res.getString("SignCsrAction.NoVerifySpkacCsr.message"), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
                    return;
                }
            } else {
                JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SignCsrAction.FileNotRecognisedType.message"), csrFile), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
                return;
            }
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SignCsrAction.NotFile.message"), csrFile), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        } catch (Exception ex) {
            String problemStr = MessageFormat.format(res.getString("SignCsrAction.NoOpenCsr.Problem"), csrFile.getName());
            String[] causes = new String[] { res.getString("SignCsrAction.NotCsr.Cause"), res.getString("SignCsrAction.CorruptedCsr.Cause") };
            Problem problem = new Problem(problemStr, causes, ex);
            DProblem dProblem = new DProblem(frame, res.getString("SignCsrAction.ProblemOpeningCsr.Title"), problem);
            dProblem.setLocationRelativeTo(frame);
            dProblem.setVisible(true);
            return;
        }
        X509Certificate[] signingChain = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        X509Certificate signingCert = signingChain[0];
        PublicKey publicKey = null;
        X500Name subject = null;
        DSignCsr dSignCsr = null;
        Provider provider = history.getExplicitProvider();
        if (pkcs10Csr != null) {
            publicKey = new JcaPKCS10CertificationRequest(pkcs10Csr).getPublicKey();
            subject = pkcs10Csr.getSubject();
            dSignCsr = new DSignCsr(frame, pkcs10Csr, csrFile, privateKey, keyPairType, signingCert, provider);
        } else {
            publicKey = spkacCsr.getPublicKey();
            subject = spkacCsr.getSubject().getName();
            dSignCsr = new DSignCsr(frame, spkacCsr, csrFile, privateKey, keyPairType, signingCert, provider);
        }
        dSignCsr.setLocationRelativeTo(frame);
        dSignCsr.setVisible(true);
        X509CertificateVersion version = dSignCsr.getVersion();
        SignatureType signatureType = dSignCsr.getSignatureType();
        Date validityStart = dSignCsr.getValidityStart();
        Date validityEnd = dSignCsr.getValidityEnd();
        BigInteger serialNumber = dSignCsr.getSerialNumber();
        caReplyFile = dSignCsr.getCaReplyFile();
        X509ExtensionSet extensions = dSignCsr.getExtensions();
        if (version == null) {
            return;
        }
        X500Name issuer = X500NameUtils.x500PrincipalToX500Name(signingCert.getSubjectX500Principal());
        // CA Reply is a cert with subject from CSR and issuer from signing cert's subject
        X509CertificateGenerator generator = new X509CertificateGenerator(version);
        X509Certificate caReplyCert = generator.generate(subject, issuer, validityStart, validityEnd, publicKey, privateKey, signatureType, serialNumber, extensions, provider);
        X509Certificate[] caReplyChain = new X509Certificate[signingChain.length + 1];
        caReplyChain[0] = caReplyCert;
        // Add all of the signing chain to the reply
        System.arraycopy(signingChain, 0, caReplyChain, 1, signingChain.length);
        byte[] caCertEncoded = X509CertUtil.getCertsEncodedPkcs7(caReplyChain);
        fos = new FileOutputStream(caReplyFile);
        fos.write(caCertEncoded);
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SignJarAction.NoWriteFile.message"), caReplyFile), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
        return;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return;
    } finally {
        IOUtils.closeQuietly(fos);
    }
    JOptionPane.showMessageDialog(frame, res.getString("SignCsrAction.SignCsrSuccessful.message"), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.INFORMATION_MESSAGE);
}
Also used : KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) FileNotFoundException(java.io.FileNotFoundException) X500Name(org.bouncycastle.asn1.x500.X500Name) X509CertificateGenerator(org.kse.crypto.x509.X509CertificateGenerator) X509CertificateVersion(org.kse.crypto.x509.X509CertificateVersion) KeyPairType(org.kse.crypto.keypair.KeyPairType) Password(org.kse.crypto.Password) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) JcaPKCS10CertificationRequest(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest) DSignCsr(org.kse.gui.dialogs.sign.DSignCsr) KeyStoreState(org.kse.utilities.history.KeyStoreState) JcaPKCS10CertificationRequest(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest) PublicKey(java.security.PublicKey) SignatureType(org.kse.crypto.signing.SignatureType) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) DProblem(org.kse.gui.error.DProblem) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) Provider(java.security.Provider) X509ExtensionSet(org.kse.crypto.x509.X509ExtensionSet) Spkac(org.kse.crypto.csr.spkac.Spkac) FileOutputStream(java.io.FileOutputStream) CryptoFileType(org.kse.crypto.filetype.CryptoFileType) BigInteger(java.math.BigInteger) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 15 with KeyStoreState

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

the class SignNewKeyPairAction method doAction.

@Override
protected void doAction() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        // get alias of selected (signing) key entry
        String alias = kseFrame.getSelectedEntryAlias();
        Password password = getEntryPassword(alias, currentState);
        if (password == null) {
            return;
        }
        KeyStore keyStore = currentState.getKeyStore();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        Certificate[] certs = keyStore.getCertificateChain(alias);
        X509Certificate[] signingCertChain = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        X509Certificate signingCert = signingCertChain[0];
        GenerateKeyPairAction generateKeyPairAction = new GenerateKeyPairAction(kseFrame);
        generateKeyPairAction.generateKeyPair(signingCert, signingCertChain, privateKey);
    } 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) Password(org.kse.crypto.Password) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

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