Search in sources :

Example 16 with KeyStoreType

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

Example 17 with KeyStoreType

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

the class ImportKeyPairAction method importKeyPairPvk.

private void importKeyPairPvk() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DImportKeyPairPvk dImportKeyPairPvk = new DImportKeyPairPvk(frame);
        dImportKeyPairPvk.setLocationRelativeTo(frame);
        dImportKeyPairPvk.setVisible(true);
        PrivateKey privateKey = dImportKeyPairPvk.getPrivateKey();
        Certificate[] certs = dImportKeyPairPvk.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) DImportKeyPairPvk(org.kse.gui.dialogs.importexport.DImportKeyPairPvk) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) DGetAlias(org.kse.gui.dialogs.DGetAlias) 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)

Example 18 with KeyStoreType

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

the class NewAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    try {
        DNewKeyStoreType dNewKeyStoreType = new DNewKeyStoreType(frame);
        dNewKeyStoreType.setLocationRelativeTo(frame);
        dNewKeyStoreType.setVisible(true);
        KeyStoreType keyStoreType = dNewKeyStoreType.getKeyStoreType();
        if (keyStoreType == null) {
            return;
        }
        KeyStore newKeyStore = KeyStoreUtil.create(keyStoreType);
        untitledCount++;
        String untitled = MessageFormat.format(res.getString("NewAction.Untitled"), untitledCount);
        kseFrame.addKeyStore(newKeyStore, untitled, null, null);
    } 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) KeyStore(java.security.KeyStore)

Example 19 with KeyStoreType

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

the class CryptoFileUtil method detectFileType.

/**
 * Detect the cryptographic file type of the supplied input stream.
 *
 * @param is
 *            Input stream to detect type for
 * @return Type or null if file not of a recognised type
 * @throws IOException
 *             If an I/O problem occurred
 */
public static CryptoFileType detectFileType(InputStream is) throws IOException {
    byte[] contents = ReadUtil.readFully(is);
    EncryptionType pkcs8EncType = Pkcs8Util.getEncryptionType(new ByteArrayInputStream(contents));
    if (pkcs8EncType != null) {
        if (pkcs8EncType == ENCRYPTED) {
            return ENC_PKCS8_PVK;
        } else if (pkcs8EncType == UNENCRYPTED) {
            return UNENC_PKCS8_PVK;
        }
    }
    EncryptionType msPvkEncType = MsPvkUtil.getEncryptionType(new ByteArrayInputStream(contents));
    if (msPvkEncType != null) {
        if (msPvkEncType == ENCRYPTED) {
            return ENC_MS_PVK;
        } else if (msPvkEncType == UNENCRYPTED) {
            return UNENC_MS_PVK;
        }
    }
    EncryptionType openSslPvkEncType = OpenSslPvkUtil.getEncryptionType(new ByteArrayInputStream(contents));
    if (openSslPvkEncType != null) {
        if (openSslPvkEncType == ENCRYPTED) {
            return ENC_OPENSSL_PVK;
        } else if (openSslPvkEncType == UNENCRYPTED) {
            return UNENC_OPENSSL_PVK;
        }
    }
    try {
        OpenSslPubUtil.load(new ByteArrayInputStream(contents));
        return OPENSSL_PUB;
    } catch (Exception ex) {
    // Ignore - not an OpenSSL public key file
    } catch (OutOfMemoryError ex) {
    // Ignore - not an OpenSSL public key file, some files cause the
    // heap space to fill up with the load call
    }
    try {
        if (X509CertUtil.loadCertificates(new ByteArrayInputStream(contents)).length > 0) {
            return CERT;
        }
    } catch (Exception ex) {
    // Ignore - not a certificate file
    }
    try {
        X509CertUtil.loadCRL(new ByteArrayInputStream(contents));
        return CRL;
    } catch (Exception ex) {
    // Ignore - not a CRL file
    }
    CsrType csrType = detectCsrType(contents);
    if (csrType != null) {
        return csrType.getCryptoFileType();
    }
    KeyStoreType keyStoreType = detectKeyStoreType(new ByteArrayInputStream(contents));
    if (keyStoreType != null) {
        return keyStoreType.getCryptoFileType();
    }
    // Not a recognised type
    return UNKNOWN;
}
Also used : EncryptionType(org.kse.crypto.privatekey.EncryptionType) CsrType(org.kse.crypto.csr.CsrType) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SpkacException(org.kse.crypto.csr.spkac.SpkacException)

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