use of org.kse.gui.error.Problem in project keystore-explorer by kaikramer.
the class AuthorityCertificatesAction method loadCaCertificatesKeyStore.
private KeyStore loadCaCertificatesKeyStore() {
File caCertificatesFile = applicationSettings.getCaCertificatesFile();
KeyStore caCertificatesKeyStore = null;
try {
// first try to open cacerts with default password
try {
Password password = new Password(AuthorityCertificates.CACERTS_DEFAULT_PWD.toCharArray());
caCertificatesKeyStore = KeyStoreUtil.load(caCertificatesFile, password);
if (caCertificatesFile != null) {
return caCertificatesKeyStore;
}
} catch (KeyStoreLoadException ex) {
// not default password, continue with password dialog
}
DGetPassword dGetPassword = new DGetPassword(frame, res.getString("AuthorityCertificatesAction.CaCertificatesKeyStorePassword.Title"));
dGetPassword.setLocationRelativeTo(frame);
dGetPassword.setVisible(true);
Password password = dGetPassword.getPassword();
if (password == null) {
return null;
}
try {
caCertificatesKeyStore = KeyStoreUtil.load(caCertificatesFile, password);
} catch (KeyStoreLoadException ex) {
String problemStr = MessageFormat.format(res.getString("AuthorityCertificatesAction.NoOpenCaCertificatesKeyStore.Problem"), ex.getKeyStoreType(), caCertificatesFile.getName());
String[] causes = new String[] { res.getString("AuthorityCertificatesAction.PasswordIncorrectKeyStore.Cause"), res.getString("AuthorityCertificatesAction.CorruptedKeyStore.Cause") };
Problem problem = new Problem(problemStr, causes, ex);
DProblem dProblem = new DProblem(frame, res.getString("AuthorityCertificatesAction.ProblemOpeningCaCertificatesKeyStore.Title"), problem);
dProblem.setLocationRelativeTo(frame);
dProblem.setVisible(true);
return null;
}
if (caCertificatesKeyStore == null) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("AuthorityCertificatesAction.FileNotRecognisedType.message"), caCertificatesFile.getName()), res.getString("AuthorityCertificatesAction.OpenCaCertificatesKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
return null;
}
return caCertificatesKeyStore;
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("AuthorityCertificatesAction.NoReadFile.message"), caCertificatesFile), res.getString("AuthorityCertificatesAction.OpenCaCertificatesKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
return null;
} catch (Exception ex) {
DError.displayError(frame, ex);
return null;
}
}
use of org.kse.gui.error.Problem in project keystore-explorer by kaikramer.
the class ImportCaReplyFromClipboardAction method openCaReply.
private X509Certificate[] openCaReply() {
X509Certificate[] certs = null;
try {
// get clip board contents, but only string types, not files
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable t = clipboard.getContents(null);
if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String data;
data = (String) t.getTransferData(DataFlavor.stringFlavor);
ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes());
// try to extract certs from clip board data
certs = X509CertUtil.loadCertificates(bais);
if (certs.length == 0) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("ImportCaReplyFromClipboardAction.NoCertsFound.message"), "Clipboard"), res.getString("ImportCaReplyFromClipboardAction.OpenCaReply.Title"), JOptionPane.WARNING_MESSAGE);
}
}
return certs;
} catch (Exception ex) {
String problemStr = MessageFormat.format(res.getString("ImportCaReplyFromClipboardAction.NoOpenCaReply.Problem"), "Clipboard");
String[] causes = new String[] { res.getString("ImportCaReplyFromClipboardAction.NotCaReply.Cause"), res.getString("ImportCaReplyFromClipboardAction.CorruptedCaReply.Cause") };
Problem problem = new Problem(problemStr, causes, ex);
DProblem dProblem = new DProblem(frame, res.getString("ImportCaReplyFromClipboardAction.ProblemOpeningCaReply.Title"), problem);
dProblem.setLocationRelativeTo(frame);
dProblem.setVisible(true);
return null;
}
}
use of org.kse.gui.error.Problem in project keystore-explorer by kaikramer.
the class ExamineClipboardAction method showCert.
private void showCert(InputStream is) throws CryptoException {
X509Certificate[] certs = null;
try {
certs = X509CertUtil.loadCertificates(is);
if (certs.length == 0) {
JOptionPane.showMessageDialog(frame, res.getString("ExamineClipboardAction.NoCertsFound.message"), res.getString("ExamineClipboardAction.OpenCertificate.Title"), JOptionPane.WARNING_MESSAGE);
}
} catch (Exception ex) {
String problemStr = res.getString("ExamineClipboardAction.NoOpenCert.Problem");
String[] causes = new String[] { res.getString("ExamineClipboardAction.NotCert.Cause"), res.getString("ExamineClipboardAction.CorruptedCert.Cause") };
Problem problem = new Problem(problemStr, causes, ex);
DProblem dProblem = new DProblem(frame, res.getString("ExamineClipboardAction.ProblemOpeningCert.Title"), problem);
dProblem.setLocationRelativeTo(frame);
dProblem.setVisible(true);
}
if (certs != null && certs.length > 0) {
DViewCertificate dViewCertificate = new DViewCertificate(frame, res.getString("ExamineClipboardAction.CertDetails.Title"), certs, kseFrame, DViewCertificate.IMPORT);
dViewCertificate.setLocationRelativeTo(frame);
dViewCertificate.setVisible(true);
}
}
use of org.kse.gui.error.Problem 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);
}
use of org.kse.gui.error.Problem in project keystore-explorer by kaikramer.
the class SetKeyPairPasswordAction method doAction.
/**
* Do action.
*/
@Override
protected void doAction() {
String alias = null;
try {
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
KeyStoreState currentState = history.getCurrentState();
KeyStoreState newState = currentState.createBasisForNextState(this);
KeyStore keyStore = newState.getKeyStore();
alias = kseFrame.getSelectedEntryAlias();
Password oldPassword = newState.getEntryPassword(alias);
DChangePassword dChangePassword = new DChangePassword(frame, DOCUMENT_MODAL, res.getString("SetKeyPairPasswordAction.SetKeyPairPassword.Title"), oldPassword, applicationSettings.getPasswordQualityConfig());
dChangePassword.setLocationRelativeTo(frame);
dChangePassword.setVisible(true);
if (oldPassword == null) {
oldPassword = dChangePassword.getOldPassword();
}
Password newPassword = dChangePassword.getNewPassword();
if ((oldPassword == null) || (newPassword == null)) {
return;
}
// Change the password by recreating the entry
Certificate[] certs = keyStore.getCertificateChain(alias);
certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
Key privateKey = keyStore.getKey(alias, oldPassword.toCharArray());
keyStore.deleteEntry(alias);
newState.removeEntryPassword(alias);
keyStore.setKeyEntry(alias, privateKey, newPassword.toCharArray(), certs);
if (currentState.getEntryPassword(alias) == null) {
currentState.setEntryPassword(alias, oldPassword);
}
newState.setEntryPassword(alias, newPassword);
currentState.append(newState);
kseFrame.updateControls(true);
JOptionPane.showMessageDialog(frame, res.getString("SetKeyPairPasswordAction.SetKeyPairPasswordSuccessful.message"), res.getString("SetKeyPairPasswordAction.SetKeyPairPassword.Title"), JOptionPane.INFORMATION_MESSAGE);
} catch (GeneralSecurityException ex) {
String problemStr = MessageFormat.format(res.getString("SetKeyPairPasswordAction.NoSetPasswordKeyPairEntry.Problem"), alias);
String[] causes = new String[] { res.getString("SetKeyPairPasswordAction.PasswordIncorrectKeyPairEntry.Cause"), res.getString("SetKeyPairPasswordAction.NotSupportedAlgorithmKeyPairEntry.Cause") };
Problem problem = new Problem(problemStr, causes, ex);
DProblem dProblem = new DProblem(frame, res.getString("SetKeyPairPasswordAction.ProblemSettingPasswordKeyPairEntry.Title"), problem);
dProblem.setLocationRelativeTo(frame);
dProblem.setVisible(true);
} catch (Exception ex) {
DError.displayError(frame, ex);
}
}
Aggregations