📄 certviewer.java
字号:
package com.ca.commons.security.cert;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;
import java.io.*;
import java.security.cert.*;
import com.ca.commons.security.cert.extensions.*;
import com.ca.commons.security.asn1.*;
import com.ca.commons.security.util.CertUtil;
import com.ca.commons.cbutil.*;
import java.util.StringTokenizer;
/**
* <p>A certificate viewer which is similar to the Microsoft's,
* written in Java, therefore it can be run in all operating systems
* with a Java virtual machine installed.
* General tab, Details tab, Certification Path tab, OK button.</p>
*
* <p>Note that the static 'setProperties()' method must be called
* before use if graphics are to be displayed.</p>
*
* @author vbui, cbetts
*/
// XXX this class has gathered a fair bit of lint - there are a bunch of generic security methods
// XXX that should be moved into some sort of security util class... - CB
// TODO: logging, and bubble exception properly from CertUtil
public class CertViewer extends CBDialog
{
/**
* Utility method for passing around a cert and accompanying
* filename
*/
public static class CertAndFileName
{
public X509Certificate cert;
public String fileName;
}
/**
* The actual cert currently being displayed.
*/
private X509Certificate cert = null;
/**
* The file name the cert was loaded from (if any).
*/
private String fileName = null;
// visual elements
private JTabbedPane tabs = new JTabbedPane();
private CertGeneralViewPanel generalView = null;
private CertDetailsViewPanel detailsView = null;
private CertPathViewPanel pathView = null;
private CBButton okButton, saveButton, loadButton;
/**
* When the mode is set to this the cert can only be viewed
*/
public static final int VIEW_ONLY = 0;
/**
* When the mode is set to this the cert can be viewed and saved to file
*/
public static final int VIEW_SAVE = 1;
/**
* When the mode is set to this the cert can be loaded from file and viewed
*/
public static final int VIEW_LOAD = 2;
/**
* When the mode is set to this the cert can be viewed, saved and loaded
*/
public static final int VIEW_SAVE_LOAD = 3;
/**
* Synonym for VIEW_SAVE_LOAD
*/
public static final int VIEW_LOAD_SAVE = 3;
protected int mode = VIEW_SAVE;
//public static String imagePath = null;
public static ImageIcon certLargeIcon = null;
public static ImageIcon certIcon = null;
public static ImageIcon attributeIcon = null;
public static ImageIcon extensionIcon = null;
public static ImageIcon criticalExtensionIcon = null;
public static ImageIcon thumbprintIcon = null;
public static Image frameIcon = null;
/**
* used to store image directory and default browsing directory properties,
* under "dir.images" and "cert.homeDir".
*/
public static Properties properties = null;
public static String helpLink = null; //TE: help link, might not be initialised, if it is a help button should be added.
/**
* sets the graphic icons for the various buttons and cert display screens,
* using the image directory set via the setProperties() method.
*/
protected static void setupGraphics()
{
try
{
certLargeIcon = getImageIcon("certificate_large.gif");
certIcon = getImageIcon("certificate.gif");
attributeIcon = getImageIcon("attribute.gif");
extensionIcon = getImageIcon("extension.gif");
criticalExtensionIcon = getImageIcon("criticalExtension.gif");
thumbprintIcon = getImageIcon("thumbprint.gif");
frameIcon = getImageIcon("pki_icon.gif").getImage();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
/**
* Local copy of standard JXplorer function to allow stand-alone use.
*/
public static ImageIcon getImageIcon(String name)
{
if (properties == null) return null;
ImageIcon newIcon = new ImageIcon(properties.getProperty("dir.images") + name);
return newIcon;
}
/**
* Sets up the help link for the help button.
*/
public static void setupHelpLink(String link)
{
helpLink = link;
}
/**
* Used to set the global properties used by CertViewer to load images,
* and to determine the default browseing directory (which can be
* maintained between sessions for added usability).
* @param props a properties object containing a "dir.images" property
* (setting the image directory path) and a "cert.homeDir" property
* describing the directory the browser should start viewing in.
*/
public static void setProperties(Properties props) { properties = props; }
/**
* A utility method - this allows just the image directory to be
* set. It is usually preferable to use setProperties() instead,
* which allows the cert.homeDir property to be maintained between
* sessions.
*/
public static void setImageDirectory(String imagePath)
{
if (properties == null)
properties = new Properties();
properties.put("dir.images", imagePath);
}
public static CertAndFileName loadCertificate(Frame owner)
{
CertViewer viewer = new CertViewer(owner, null, VIEW_SAVE_LOAD);
// by this stage, the viewer has already prompted the user to
// select a certificate - if it is null, it means the user
// hit 'cancel' on the file chooser, so bail out...
if (viewer.getCertificate() == null)
return null;
// show the certificate viewer, and allow the user to decide
// whether to use the current cert, or select another.
viewer.setVisible(true);
CertAndFileName returnInfo = new CertAndFileName();
returnInfo.cert = viewer.getCertificate();
returnInfo.fileName = viewer.getFileName();
return returnInfo;
}
public static CertAndFileName editCertificate(Frame owner, byte[] certData)
{
if (certData == null) return loadCertificate(owner);
X509Certificate cert = CertUtil.loadX509Certificate (certData);
CertViewer viewer = new CertViewer(owner, cert, VIEW_SAVE_LOAD);
// by this stage, the viewer has already prompted the user to
// select a certificate - if it is null, it means the user
// hit 'cancel' on the file chooser, so bail out...
if (viewer.getCertificate() == null)
return null;
// show the certificate viewer, and allow the user to decide
// whether to use the current cert, or select another.
viewer.setVisible(true);
CertAndFileName returnInfo = new CertAndFileName();
returnInfo.cert = viewer.getCertificate();
returnInfo.fileName = viewer.getFileName();
return returnInfo;
}
/**
* Frameless Constructor. This should only be used if no
* parent frame can be found, and will cause odd behaviour
* in some circumstances.
* @deprecated
* @param cert the X509 certificate to view
*/
public CertViewer(X509Certificate cert)
{
super(null, CBIntText.get("Certificate"), null);
init(cert, VIEW_SAVE);
}
/**
* Constructor.
* @param owner the parent Frame (used for centering and look and feel propogation)
* @param cert the X509 certificate to view
*/
public CertViewer(Frame owner, X509Certificate cert)
{
super(owner, CBIntText.get("Certificate"), helpLink); //TE: if there is no help link supplied helpLink will be null.
init(cert, VIEW_SAVE);
}
/**
* Constructor.
* @param owner the parent Frame (used for centering and look and feel propogation)
* @param cert the X509 certificate to view
* @param mode one of VIEW, VIEW_SAVE, VIEW_LOAD or VIEW_SAVE_LOAD, describing
* what options should be available to the user.
*/
public CertViewer(Frame owner, X509Certificate cert, int mode)
{
super(owner, CBIntText.get("Certificate"), helpLink); //TE: if there is no help link supplied helpLink will be null.
init(cert, mode);
}
public void init(X509Certificate cert, int mode)
{
if (certLargeIcon == null)
setupGraphics();
this.mode = mode;
displayCert(cert); // setup tab panes.
saveButton = new CBButton(CBIntText.get("Copy to File"), CBIntText.get("Copy to File."));
loadButton = new CBButton(CBIntText.get("Read from File"), CBIntText.get("Read from File."));
// Since this inherits from CBDialog, it already has 'ok' and
// 'cancel'buttons; depending on what mode we're in, we will
// add others, and possibly remove the cancel.
if (mode == VIEW_ONLY || mode == VIEW_SAVE)
{ // if we can't load, there's no need for cancel
buttonPanel.remove(Cancel); // remove default CBDialog cancel button
}
if (mode == VIEW_SAVE || mode == VIEW_SAVE_LOAD)
{
buttonPanel.add(saveButton, 0);
}
if (mode == VIEW_LOAD || mode == VIEW_SAVE_LOAD)
{
buttonPanel.add(loadButton, 0);
//OK.setText(CBIntText.get("Submit"));
}
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
setSize(440, 477);
CBUtility.center(this, owner);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -