Skip to content

Commit

Permalink
Provide private key format to DViewPrivateKey dialog to avoid always …
Browse files Browse the repository at this point in the history
…displaying "PKCS#8" (issue #436)
  • Loading branch information
tulsidas authored Jan 4, 2024
1 parent b686821 commit 5359db0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
15 changes: 15 additions & 0 deletions kse/src/main/java/org/kse/crypto/privatekey/PrivateKeyFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.kse.crypto.privatekey;

public enum PrivateKeyFormat {
PKCS1("PKCS#1"), PKCS8("PKCS#8"), MSPVK("MS PVK");

private String value;

private PrivateKeyFormat(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.text.MessageFormat;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
Expand All @@ -54,6 +55,7 @@
import org.kse.crypto.privatekey.MsPvkUtil;
import org.kse.crypto.privatekey.OpenSslPvkUtil;
import org.kse.crypto.privatekey.Pkcs8Util;
import org.kse.crypto.privatekey.PrivateKeyFormat;
import org.kse.crypto.publickey.OpenSslPubUtil;
import org.kse.crypto.x509.X509CertUtil;
import org.kse.gui.KseFrame;
Expand Down Expand Up @@ -262,6 +264,7 @@ private void downloadCert(URL url) throws IOException, CryptoException {
private void showPrivateKey(byte[] data, CryptoFileType fileType) throws IOException, CryptoException {
PrivateKey privKey = null;
Password password = null;
PrivateKeyFormat format = null;

switch (fileType) {
case ENC_PKCS8_PVK:
Expand All @@ -270,36 +273,42 @@ private void showPrivateKey(byte[] data, CryptoFileType fileType) throws IOExcep
return;
}
privKey = Pkcs8Util.loadEncrypted(data, password);
format = PrivateKeyFormat.PKCS8;
break;
case UNENC_PKCS8_PVK:
privKey = Pkcs8Util.load(data);
format = PrivateKeyFormat.PKCS8;
break;
case ENC_OPENSSL_PVK:
password = getPassword();
if (password == null || password.isNulled()) {
return;
}
privKey = OpenSslPvkUtil.loadEncrypted(data, password);
format = PrivateKeyFormat.PKCS1;
break;
case UNENC_OPENSSL_PVK:
privKey = OpenSslPvkUtil.load(data);
format = PrivateKeyFormat.PKCS1;
break;
case ENC_MS_PVK:
password = getPassword();
if (password == null || password.isNulled()) {
return;
}
privKey = MsPvkUtil.loadEncrypted(data, password);
format = PrivateKeyFormat.MSPVK;
break;
case UNENC_MS_PVK:
privKey = MsPvkUtil.load(data);
format = PrivateKeyFormat.MSPVK;
break;
default:
break;
}

DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, res.getString(
"ExamineClipboardAction.PrivateKeyDetails.Title"), "", privKey, preferences);
"ExamineClipboardAction.PrivateKeyDetails.Title"), "", privKey, preferences, Optional.ofNullable(format));
dViewPrivateKey.setLocationRelativeTo(frame);
dViewPrivateKey.setVisible(true);
}
Expand Down
11 changes: 10 additions & 1 deletion kse/src/main/java/org/kse/gui/actions/ExamineFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import java.util.Base64;
import java.util.Optional;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
Expand All @@ -47,6 +48,7 @@
import org.kse.crypto.privatekey.MsPvkUtil;
import org.kse.crypto.privatekey.OpenSslPvkUtil;
import org.kse.crypto.privatekey.Pkcs8Util;
import org.kse.crypto.privatekey.PrivateKeyFormat;
import org.kse.crypto.publickey.OpenSslPubUtil;
import org.kse.crypto.signing.JarParser;
import org.kse.crypto.x509.X509CertUtil;
Expand Down Expand Up @@ -254,6 +256,7 @@ private void openPrivateKey(File file, CryptoFileType fileType) throws IOExcepti
byte[] data = decodeIfBase64(FileUtils.readFileToByteArray(file));
PrivateKey privKey = null;
Password password = null;
PrivateKeyFormat format = null;

switch (fileType) {
case ENC_PKCS8_PVK:
Expand All @@ -262,37 +265,43 @@ private void openPrivateKey(File file, CryptoFileType fileType) throws IOExcepti
return;
}
privKey = Pkcs8Util.loadEncrypted(data, password);
format = PrivateKeyFormat.PKCS8;
break;
case UNENC_PKCS8_PVK:
privKey = Pkcs8Util.load(data);
format = PrivateKeyFormat.PKCS8;
break;
case ENC_OPENSSL_PVK:
password = getPassword(file);
if (password == null || password.isNulled()) {
return;
}
privKey = OpenSslPvkUtil.loadEncrypted(data, password);
format = PrivateKeyFormat.PKCS1;
break;
case UNENC_OPENSSL_PVK:
privKey = OpenSslPvkUtil.load(data);
format = PrivateKeyFormat.PKCS1;
break;
case ENC_MS_PVK:
password = getPassword(file);
if (password == null || password.isNulled()) {
return;
}
privKey = MsPvkUtil.loadEncrypted(data, password);
format = PrivateKeyFormat.MSPVK;
break;
case UNENC_MS_PVK:
privKey = MsPvkUtil.load(data);
format = PrivateKeyFormat.MSPVK;
break;
default:
break;
}

DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, MessageFormat.format(
res.getString("ExamineFileAction.PrivateKeyDetailsFile.Title"), file.getName()), FileNameUtil.removeExtension(file.getName()), privKey,
preferences);
preferences, Optional.ofNullable(format));
dViewPrivateKey.setLocationRelativeTo(frame);
dViewPrivateKey.setVisible(true);
}
Expand Down
3 changes: 2 additions & 1 deletion kse/src/main/java/org/kse/gui/actions/KeyDetailsAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.security.PrivateKey;
import java.security.PublicKey;
import java.text.MessageFormat;
import java.util.Optional;

import javax.crypto.SecretKey;
import javax.swing.ImageIcon;
Expand Down Expand Up @@ -104,7 +105,7 @@ public void showKeySelectedEntry() {

DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, MessageFormat.format(
res.getString("KeyDetailsAction.PrivateKeyDetailsEntry.Title"), alias), alias, privateKey,
preferences);
preferences, Optional.empty());
dViewPrivateKey.setLocationRelativeTo(frame);
dViewPrivateKey.setVisible(true);
} else if (key instanceof PublicKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.security.KeyStore;
import java.security.PrivateKey;
import java.text.MessageFormat;
import java.util.Optional;

import javax.swing.ImageIcon;

Expand Down Expand Up @@ -77,7 +78,7 @@ protected void doAction() {

DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, MessageFormat.format(
res.getString("KeyPairPrivateKeyDetailsAction.PrivKeyDetailsEntry.Title"), alias), alias, privKey,
preferences);
preferences, Optional.empty());
dViewPrivateKey.setLocationRelativeTo(frame);
dViewPrivateKey.setVisible(true);
} catch (Exception ex) {
Expand Down
12 changes: 9 additions & 3 deletions kse/src/main/java/org/kse/gui/dialogs/DViewPrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.ResourceBundle;

import javax.swing.JButton;
Expand All @@ -52,6 +53,7 @@
import org.kse.crypto.KeyInfo;
import org.kse.crypto.keypair.KeyPairType;
import org.kse.crypto.keypair.KeyPairUtil;
import org.kse.crypto.privatekey.PrivateKeyFormat;
import org.kse.gui.CursorUtil;
import org.kse.gui.JEscDialog;
import org.kse.gui.LnfUtil;
Expand Down Expand Up @@ -95,6 +97,8 @@ public class DViewPrivateKey extends JEscDialog {

private KsePreferences preferences;

private Optional<PrivateKeyFormat> format;

/**
* Creates a new DViewPrivateKey dialog.
*
Expand All @@ -103,12 +107,13 @@ public class DViewPrivateKey extends JEscDialog {
* @param privateKey Private key to display
* @throws CryptoException A problem was encountered getting the private key's details
*/
public DViewPrivateKey(JFrame parent, String title, String alias, PrivateKey privateKey, KsePreferences preferences)
public DViewPrivateKey(JFrame parent, String title, String alias, PrivateKey privateKey, KsePreferences preferences, Optional<PrivateKeyFormat> format)
throws CryptoException {
super(parent, title, Dialog.ModalityType.DOCUMENT_MODAL);
this.alias = alias;
this.privateKey = privateKey;
this.preferences = preferences;
this.format = format;
initComponents();
}

Expand All @@ -123,6 +128,7 @@ public DViewPrivateKey(JFrame parent, String title, String alias, PrivateKey pri
public DViewPrivateKey(JDialog parent, String title, PrivateKey privateKey) throws CryptoException {
super(parent, title, ModalityType.DOCUMENT_MODAL);
this.privateKey = privateKey;
this.format = Optional.empty();
initComponents();
jbExport.setVisible(false);
}
Expand Down Expand Up @@ -292,7 +298,7 @@ private void populateDialog() throws CryptoException {
jtfKeySize.setText(MessageFormat.format(res.getString("DViewPrivateKey.jtfKeySize.text"), "?"));
}

jtfFormat.setText(privateKey.getFormat());
jtfFormat.setText(format.map(PrivateKeyFormat::getValue).orElse(privateKey.getFormat()));

jtaEncoded.setText(new BigInteger(1, privateKey.getEncoded()).toString(16).toUpperCase());
jtaEncoded.setCaretPosition(0);
Expand Down Expand Up @@ -343,7 +349,7 @@ public static void main(String[] args) throws Exception {
KeyPair keyPair = keyGen.genKeyPair();

PrivateKey privKey = keyPair.getPrivate();
DViewPrivateKey dialog = new DViewPrivateKey(new javax.swing.JFrame(), "Title", "private", privKey, null);
DViewPrivateKey dialog = new DViewPrivateKey(new javax.swing.JFrame(), "Title", "private", privKey, null, Optional.empty());
DialogViewer.run(dialog);
}
}

0 comments on commit 5359db0

Please sign in to comment.