📄 certviewer.java
字号:
saveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) { saveCert(); }
});
loadButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) { loadCert(); }
});
// if we've started up with a null cert, fire up the load
// cert window immediately.
if (cert == null && (mode & VIEW_LOAD)>0)
loadCert();
}
/**
* This is a bit heavyhanded - when the cert changes,
* tear down everything and recreate all the gui objects
* in the tab pane. Better would be to charge off and
* update all the individual components, but right now
* I'm in a bit of a hurry :-) - CB
* @param displayCert the new cert to display.
*/
public void displayCert(X509Certificate displayCert)
{
cert = displayCert;
tabs.removeAll();
generalView = new CertGeneralViewPanel(displayCert);
detailsView = new CertDetailsViewPanel(displayCert);
pathView = new CertPathViewPanel(displayCert);
tabs.add("General", generalView);
tabs.add("Details", detailsView);
tabs.add("Certification Path", pathView);
display.removeAll();
makeHeavy();
display.add(tabs);
}
/**
* When the user hits 'cancel', the window is shut down,
* and the cert is set to null (for the benefit of anyone
* using this as a cert loader).
*/
public void doCancel()
{
cert = null;
super.doCancel();
}
/**
* Returns the certificate currently being viewed by the CertViewer.
* @return the current certificate (may be null).
*/
public X509Certificate getCertificate() { return cert; }
/**
* Returns the file name (if any) associated with the currently viewed
* cert.
* @return the full file name
*/
public String getFileName() { return fileName; }
/**
* This method prompts the user to select a file name to save the cert to.
*/
protected void saveCert()
{
JFileChooser chooser = new JFileChooser(properties.getProperty("cert.homeDir", System.getProperty("user.dir")));
chooser.addChoosableFileFilter(new CBFileFilter(new String[] {"der", "pem"}, CBIntText.get("Certificate File") + " (*.der) (*.pem)"));
int option = chooser.showSaveDialog(this);
File readFile = chooser.getSelectedFile();
if (option != JFileChooser.APPROVE_OPTION || readFile == null) // only do something if user chose 'ok'
{
return; // user picked 'cancel' or didn't select a file.
}
if (properties != null)
properties.setProperty("cert.homeDir", readFile.getParent());
fileName = readFile.toString();
try
{
byte[] derout = cert.getEncoded();
if (fileName.toLowerCase().endsWith(".pem")) // special handling for PEM files
{
//System.out.println("saving pem cert!!!");
derout = CBSecurity.convertToPEMCertificate(derout);
}
else if (fileName.toLowerCase().endsWith(".der") == false)
{
fileName = fileName + ".der";
readFile = new File(fileName);
}
if (saveFileCheck(readFile) == false) return; // something wrong with the file name
FileOutputStream fos = new FileOutputStream(readFile);
//System.out.println("writeing: " + derout.length + " bytes");
fos.write(derout);
fos.close();
}
catch (Exception ex)
{
CBUtility.error(CBIntText.get("Unable to save Certificate."), ex);
}
}
/**
* Checks a file before saving it.
*/
public boolean saveFileCheck(File checkMe)
{
if (checkMe.isDirectory())
{
CBUtility.error(checkMe.toString() + " is a directory.", null);
return false;
}
else if (checkMe.exists())
{
int saveAnswer = JOptionPane.showConfirmDialog(owner,
(checkMe.toString() + "\n " + CBIntText.get("This file already exists.\nDo you want to overwrite this file?")),
"Question", JOptionPane.OK_CANCEL_OPTION);
return (saveAnswer == JOptionPane.OK_OPTION);
}
return true;
}
protected void loadCert()
{
String browseDir = System.getProperty("user.dir");
if (properties != null)
{
if (properties.getProperty("cert.homeDir") != null)
browseDir = properties.getProperty("cert.homeDir");
}
JFileChooser chooser = new JFileChooser(browseDir);
chooser.addChoosableFileFilter(new CBFileFilter(new String[] {"der", "pem"}, CBIntText.get("Certificate File") + " (*.der), (*.pem)"));
int option = chooser.showOpenDialog(this);
File readFile = chooser.getSelectedFile();
if (option != JFileChooser.APPROVE_OPTION || readFile == null) // only do something if user chose 'ok'
{ // user picked 'cancel' or didn't select a file.
if (cert == null)
doCancel(); // quit out - we have nothing to display.
return;
}
try
{
if (properties != null)
properties.setProperty("cert.homeDir", readFile.getParent());
//Obsolete - use Sun code
//XXX - don't use Sun code - it doesn't handle plain text prefix in pem file correctly.
byte[] data = getDERCertDataFromFile(readFile);
//Obsolete - use Sun code
//XXX - don't use Sun code - it doesn't handle plain text prefix in pem file correctly.
X509Certificate newCert = CertUtil.loadX509Certificate(data);
/* Sun code
System.out.println("using new code");
X509Certificate newCert = CertUtil.loadX509Certificate(readFile);
*/
displayCert(newCert);
fileName = readFile.getName();
}
catch (Exception ex)
{
CBUtility.error(CBIntText.get("Unable to load Certificate."), ex);
}
}
/**
* Attempt to retrieve the most significant name in a distinguish name
* ????? (cn = somename)
*/
public static String getMostSignificantName(String dnstring)
{
String leftmostname = null;
StringTokenizer stok = new StringTokenizer(dnstring, ",");
if (stok.hasMoreTokens()) leftmostname = stok.nextToken();
return leftmostname;
}
/**
* This takes a file, and converts the certificate data within it into
* a byte array. While this is straightforward if it is DER encoded,
* some parsing is required if it is PEM to identify the certificate
* portion of the text, and convert it into raw DER bytes.
*
* @param file the file containing an X509 certificate in DER or PEM form
* @return the raw (DER) byte data.
*/
public static byte[] getDERCertDataFromFile(File file)
throws CertificateParsingException, FileNotFoundException, IOException
{
/* Read the file data into a byte array */
FileInputStream in = new FileInputStream(file);
byte [] buffer = new byte[(int) (file.length())];
in.read(buffer);
in.close();
/* check if this is pem base64 encoded data - if it is, translate it */
if (CBSecurity.isPEM(buffer))
{
//XXX <your code to handle unencrypted private keys here> XXX//
//System.out.println("\nparsing PEM data!!\n");
byte[] pemData = CBSecurity.convertFromPEMCertificate(buffer);
if (pemData == null)
throw new CertificateParsingException("Unable to parse PEM encoded cert - invalid PEM encoding.");
buffer = pemData;
}
return buffer;
}
/**
* Main method, the starting point of this program.
*/
public static void main(String[] args)
{
JFrame parent = new JFrame();
//parent.setIconImage(frameIcon);
X509Certificate cert = null;
try
{
byte[] data = getDERCertDataFromFile(new File(args[0]));
if ( (cert = CertUtil.loadX509Certificate(data)) == null )
{
System.out.println("Problem opening certfile \"" + args[0] + "\"");
System.exit(1);
}
String localDir = System.getProperty("user.dir") + File.separator;
Properties props = new Properties();
/*
* This sets the directory that the file browsers start in.
* This can be saved/read from file to allow the user to start
* loading/saving from the same place.
*/
props.setProperty("cert.homeDir", localDir + "certs" + File.separator);
/*
* This simply sets the directory where the GUI will try to load
* its button images from.
*/
props.setProperty("dir.images", localDir + "images" + File.separator);
CertViewer.setProperties(props);
CertViewer me = new CertViewer(parent, cert);
me.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
me.setVisible(true);
}
catch (Exception e)
{
System.err.println("ERROR OCCURRED");
e.printStackTrace();
System.exit(-1);
}
// look and feel
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -