Search in sources :

Example 11 with Password

use of org.kse.crypto.Password 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 12 with Password

use of org.kse.crypto.Password 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)

Example 13 with Password

use of org.kse.crypto.Password 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 14 with Password

use of org.kse.crypto.Password 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 15 with Password

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

the class KeyDetailsAction method showKeySelectedEntry.

/**
 * Show the key details of the selected KeyStore entry.
 */
public void showKeySelectedEntry() {
    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();
        Key key = keyStore.getKey(alias, password.toCharArray());
        if (key instanceof SecretKey) {
            SecretKey secretKey = (SecretKey) key;
            DViewSecretKey dViewSecretKey = new DViewSecretKey(frame, MessageFormat.format(res.getString("KeyDetailsAction.SecretKeyDetailsEntry.Title"), alias), secretKey);
            dViewSecretKey.setLocationRelativeTo(frame);
            dViewSecretKey.setVisible(true);
        } else if (key instanceof PrivateKey) {
            PrivateKey privateKey = (PrivateKey) key;
            DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, MessageFormat.format(res.getString("KeyDetailsAction.PrivateKeyDetailsEntry.Title"), alias), privateKey, history.getExplicitProvider());
            dViewPrivateKey.setLocationRelativeTo(frame);
            dViewPrivateKey.setVisible(true);
        } else if (key instanceof PublicKey) {
            PublicKey publicKey = (PublicKey) key;
            DViewPublicKey dViewPublicKey = new DViewPublicKey(frame, MessageFormat.format(res.getString("KeyDetailsAction.PublicKeyDetailsEntry.Title"), alias), publicKey);
            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) DViewSecretKey(org.kse.gui.dialogs.DViewSecretKey) PrivateKey(java.security.PrivateKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) PublicKey(java.security.PublicKey) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) KeyStore(java.security.KeyStore) DViewSecretKey(org.kse.gui.dialogs.DViewSecretKey) SecretKey(javax.crypto.SecretKey) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) DViewSecretKey(org.kse.gui.dialogs.DViewSecretKey) PublicKey(java.security.PublicKey) DViewPublicKey(org.kse.gui.dialogs.DViewPublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) Password(org.kse.crypto.Password)

Aggregations

Password (org.kse.crypto.Password)60 KeyStore (java.security.KeyStore)35 KeyStoreState (org.kse.utilities.history.KeyStoreState)32 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)31 PrivateKey (java.security.PrivateKey)24 File (java.io.File)23 FileNotFoundException (java.io.FileNotFoundException)16 Key (java.security.Key)15 X509Certificate (java.security.cert.X509Certificate)15 Certificate (java.security.cert.Certificate)13 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)12 CryptoException (org.kse.crypto.CryptoException)9 DProblem (org.kse.gui.error.DProblem)9 Problem (org.kse.gui.error.Problem)9 DGetAlias (org.kse.gui.dialogs.DGetAlias)8 DGetNewPassword (org.kse.gui.password.DGetNewPassword)8 JPasswordField (javax.swing.JPasswordField)6 DViewPrivateKey (org.kse.gui.dialogs.DViewPrivateKey)6 DGetPassword (org.kse.gui.password.DGetPassword)6 FileInputStream (java.io.FileInputStream)5