Skip to content

Commit

Permalink
Added viewer for PKCS#12 data structure (issue #231)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikramer committed Jan 26, 2025
1 parent d5ef850 commit ab25fc1
Show file tree
Hide file tree
Showing 10 changed files with 878 additions and 4 deletions.
4 changes: 2 additions & 2 deletions kse/src/main/java/org/kse/gui/KseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@

import org.kse.KSE;
import org.kse.crypto.CryptoException;
import org.kse.gui.passwordmanager.Password;
import org.kse.crypto.keystore.KeyStoreType;
import org.kse.crypto.keystore.KeyStoreUtil;
import org.kse.gui.actions.AboutAction;
Expand Down Expand Up @@ -186,6 +185,7 @@
import org.kse.gui.dnd.DragTrustedCertificateEntry;
import org.kse.gui.dnd.KeyStoreEntryDragGestureListener;
import org.kse.gui.error.DError;
import org.kse.gui.passwordmanager.Password;
import org.kse.gui.preferences.PreferencesManager;
import org.kse.gui.preferences.data.KsePreferences;
import org.kse.gui.quickstart.JQuickStartPane;
Expand Down Expand Up @@ -2252,7 +2252,7 @@ private void initKeyStoreEntryPopupMenus() {
jmiMultiEntryExport.setToolTipText(null);
new StatusBarChangeHandler(jmiMultiEntryExport,
(String) exportSelectedCertificatesAction.getValue(Action.LONG_DESCRIPTION), this);

jpmMultiEntrySel = new JPopupMenu();
jpmMultiEntrySel.add(jmiMultiEntrySelCut);
jpmMultiEntrySel.add(jmiMultEntrySelCopy);
Expand Down
15 changes: 15 additions & 0 deletions kse/src/main/java/org/kse/gui/PlatformUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ public static JPanel createDialogButtonPanel(JButton jbPositive, JButton jbNegat
insets);
}

/**
* Create a dialog button panel with the order and alignment dependent on the platform.
*
* @param jbPositive Positive button
* @param jbNegative Negative button
* @param jbOther Other button
* @param insets Insets for panel (MiGLayout constraint)
* @return Dialog button panel
*/
public static JPanel createDialogButtonPanel(JButton jbPositive, JButton jbNegative, JButton jbOther,
String insets) {
return createDialogButtonPanel((jbPositive == null ? null : new JButton[] { jbPositive }), jbNegative,
(jbOther == null ? null : new JButton[] { jbOther }), insets);
}

/**
* Create a dialog button panel with the order and alignment dependent on
* the platform.
Expand Down
33 changes: 31 additions & 2 deletions kse/src/main/java/org/kse/gui/actions/ExamineFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.kse.gui.CurrentDirectory;
import org.kse.gui.FileChooserFactory;
import org.kse.gui.KseFrame;
import org.kse.gui.dialogs.DPkcs12Info;
import org.kse.gui.dialogs.DViewCertificate;
import org.kse.gui.dialogs.DViewCrl;
import org.kse.gui.dialogs.DViewCsr;
Expand All @@ -64,6 +65,7 @@
import org.kse.gui.error.Problem;
import org.kse.gui.password.DGetPassword;
import org.kse.gui.passwordmanager.Password;
import org.kse.gui.passwordmanager.PasswordManager;
import org.kse.utilities.io.FileNameUtil;

/**
Expand Down Expand Up @@ -113,9 +115,11 @@ public void openFile(File file) {
X509Certificate[] signerCertificates = jarParser.getSignerCertificates();
showCerts(signerCertificates, file.getName());
break;
case PKCS12_KS:
showPkcs12Info(file);
break;
case JCEKS_KS:
case JKS_KS:
case PKCS12_KS:
case BKS_KS:
case BKS_V1_KS:
case BCFKS_KS:
Expand Down Expand Up @@ -163,6 +167,31 @@ public void openFile(File file) {
}
}

private void showPkcs12Info(File file) throws IOException, CryptoException {
Password password;
PasswordManager passwordManager = PasswordManager.getInstance();
if (passwordManager.isKeyStorePasswordKnown(file)) {
unlockPasswordManager();
password = passwordManager.getKeyStorePassword(file).map(Password::new).orElse(null);
} else {
password = getPassword(file);
}

if (password == null || password.isNulled()) {
return;
}

byte[] p12Data = FileUtils.readFileToByteArray(file);

DPkcs12Info dPkcs12Info = new DPkcs12Info(frame, p12Data, password, file);
dPkcs12Info.setLocationRelativeTo(frame);
dPkcs12Info.setVisible(true);

if (!dPkcs12Info.isCancelled()) {
new OpenAction(kseFrame).openKeyStore(file, new String(password.toCharArray()));
}
}

private void showCerts(X509Certificate[] certs, String fileName) throws CryptoException {
if ((certs != null) && (certs.length > 0)) {
DViewCertificate dViewCertificate = new DViewCertificate(frame, MessageFormat.format(
Expand Down Expand Up @@ -254,8 +283,8 @@ private void openPrivateKey(File file, CryptoFileType fileType) throws IOExcepti

byte[] data = decodeIfBase64(FileUtils.readFileToByteArray(file));
PrivateKey privKey = null;
Password password = null;
PrivateKeyFormat format = null;
Password password;

switch (fileType) {
case ENC_PKCS8_PVK:
Expand Down
Loading

0 comments on commit ab25fc1

Please sign in to comment.