/* ShowX509Cert uses JavaPlugin1.2.2+ RSA signing to display details of client-side x509v1,v2,v3 certificates. Certificate must be in binary-der or base64 encoded format. Certificate details are displayed in Java console and textarea element of applet. by M. Gallant 07/22/2001 */ import java.awt.* ; import java.awt.event.*; import java.io.* ; import java.security.cert.*; import java.net.*; public class ShowX509Cert extends java.applet.Applet implements ActionListener { TextArea textArea = new TextArea (15, 75); private Button filebutton = new Button("Select Cert File") ; private Button urlbutton = new Button("Cert URL:") ; private TextField tf = new TextField("Enter a URL to a cert. here", 35) ; public void init() { setBackground( new Color(192,192,240) ) ; add(filebutton) ; filebutton.addActionListener(this); add(urlbutton) ; urlbutton.addActionListener(this); urlbutton.setBackground(Color.red) ; add(tf) ; add(textArea) ; } public void actionPerformed(ActionEvent e) { if ( e.getSource() == filebutton ) { Frame myframe = new Frame() ; FileDialog myfd = new FileDialog(myframe, "Select X509 Cert File ...", FileDialog.LOAD) ; myfd.toFront() ; myfd.show() ; String mydir = myfd.getDirectory(); String myname = myfd.getFile() ; File fname = new File(mydir+myname) ; textArea.setText("") ; if ( fname.exists() ) { try { InputStream inStream = new FileInputStream(fname); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream); inStream.close() ; textArea.setText("----- Certificate File: " + fname + " -----\n" + cert.toString()) ; } catch ( CertificateException cex ) { System.out.println(cex.toString()); textArea.setText(cex.toString()) ; } catch ( IOException ioe ) { System.out.println(ioe.toString()); textArea.setText(ioe.toString()) ; } } else { System.out.println("Couldn't find file: " + mydir+myname) ; textArea.setText("Couldn't find file: " + mydir+myname) ; } myframe.dispose() ; } if ( e.getSource() == urlbutton ) { // if a URL certificate was specified ... // BufferedInputStream buffin = null; // *** note that BufferedInputStream does NOT read properly with generateCertificate(BuffInpStr) String urlspec = tf.getText(); try { URL url = new URL(urlspec) ; InputStream buffin = url.openStream(); // buffin = new BufferedInputStream(url.openStream(), 512) ; textArea.setText("Retrieving network certificate ....... \n") ; CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate)cf.generateCertificate(buffin); buffin.close() ; textArea.setText("----- Certificate URL: " + urlspec + " -----\n" + cert.toString()) ; } // end try catch (MalformedURLException mue) { textArea.setText("Bad URL : " + urlspec) ; } catch (IOException ieo) { textArea.setText("Problem with " + urlspec) ; } catch (CertificateException ce) { textArea.setText("Problem reading " + urlspec + " URL as certificate. \n" + ce.toString() + "\n") ; } } } /** * Allow this applet to run as as application as well. * * @param args command line arguments ignored. */ static public void main(String args[]) { final ShowX509Cert applet = new ShowX509Cert(); final Frame frame = new Frame("Show X509 Certificates"); frame.setSize(600, 380); applet.init(); frame.add(applet); frame.validate(); frame.setVisible(true); applet.start(); frame.addWindowListener ( new java.awt.event.WindowAdapter() { /** * Handle request to shutdown. * * @param e event giving details of closing. */ public void windowClosing(java.awt.event.WindowEvent e) { applet.stop(); applet.destroy(); System.exit(0); } // end WindowClosing } // end anonymous class ); // end addWindowListener line } // end main } // end class