/* * @(#)KeystoreLogin.java 1.3 2002/05/12 * * Copyright (c) 2001 Michel Gallant. * @author Michel Gallant */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.security.*; import java.security.cert.*; import java.util.*; /** * A utility applet for signed Plugin deployment to allow reading * of Java 2 keystore files, policy files and Java2 security files. * Requires secure login to password protected keystores. * Handles JKS Sun keystore type. * Handles PKCS12 keystore types for j2se v1.4+ or with earlier j2se + JSSE installed. */ public class KeystoreLogin extends java.applet.Applet implements ActionListener { boolean userValid = false; MediaTracker tracker ; Image ntlogo ; logocanvas appletlogo; MyLogin login; private TextArea keyinfo = new TextArea(); private Button b0, b1, b2, b3, b4, b5 ; private String uhome, jhome, fsep; private boolean defkeystoreexists = false; private String defkey; private boolean defpolicyexists = false ; private String defpol; private String defsecdir; private boolean defsecexists = false; private String lastDir = ""; public void init() { uhome = System.getProperty("user.home") ; jhome = System.getProperty("java.home") ; fsep = System.getProperty("file.separator") ; setBackground(new Color(192,192,192)) ; this.setLayout(new BorderLayout(5,5)) ; Panel p = new Panel(); b0 = new Button("Clear Text") ; b0.addActionListener(this) ; p.add(b0) ; b1 = new Button("Default Keystore") ; b1.addActionListener(this) ; b1.setBackground(Color.red) ; p.add(b1) ; b2 = new Button("Select Keystore") ; b2.addActionListener(this) ; b2.setBackground(Color.red) ; p.add(b2) ; b3 = new Button("Default Policyfile") ; b3.addActionListener(this) ; b3.setBackground(Color.green) ; p.add(b3) ; b4 = new Button("Select Policyfile") ; b4.addActionListener(this) ; b4.setBackground(Color.green) ; p.add(b4) ; b5 = new Button("Select Secfile") ; b5.addActionListener(this) ; b5.setBackground(Color.green) ; p.add(b5) ; add(p, "North"); add(keyinfo, "Center"); add(new Label(" "), "East") ; add(new Label(" "), "West") ; ntlogo = getImage(getCodeBase(), "nortelogo.gif") ; tracker = new MediaTracker(this) ; tracker.addImage(ntlogo,0) ; try { tracker.waitForID(0) ; } catch (InterruptedException e) { return ; } appletlogo = new logocanvas(ntlogo) ; getPathInfo() ; } private final void getPathInfo() { defkeystoreexists = false; defpolicyexists = false ; keyinfo.setText("------ Default Policy File and Keystore Status ------\n\n") ; if(jhome !=null) { keyinfo.append("java.home " + jhome + "\n") ; defsecdir = jhome + fsep + "lib" + fsep + "security"; if(( new File(defsecdir)).exists()) { defsecexists = true; keyinfo.append(defsecdir + " found.\n") ; } } else keyinfo.append("Problem getting \"java.home\" system property\n") ; if(uhome !=null) { keyinfo.append("user.home " + uhome + "\n\n") ; defpol = uhome + fsep + ".java.policy" ; if(( new File(defpol)).exists()) { defpolicyexists = true; keyinfo.append(defpol + " found.\n") ; } else keyinfo.append(defpol + " NOT found.\n") ; defkey = uhome + fsep + ".keystore" ; if(( new File(defkey)).exists()) { keyinfo.append(defkey + " found.\n") ; defkeystoreexists = true; } else keyinfo.append(defkey + " NOT found.\n") ; } else keyinfo.append("Problem getting \"user.home\" system property\n") ; } public void login(String keyfile) { if(login==null) login = new MyLogin (appletlogo, new Frame("")); else login.setVisible(true); login.requestFocus(); if (login.id) { String password = login.password.getText(); login.password.setText("") ; String storedump = keystoreDump(keyfile, password) ; synchronized(keyinfo){ // since append is NOT synchronized and this takes time keyinfo.setText( keyinfo.getText() + storedump + "\n") ; //bug in append so workaround } } login.setVisible(false) ; } private final String keystoreDump(String keystfile, String psd) { String tail ="---------- End Entries in Keystore: " + keystfile + "\n"; StringBuffer buff = null ; if(keystfile.equals(defkey)) buff = new StringBuffer("\n---------- Entries in Default Keystore: " + keystfile + "\n") ; else buff = new StringBuffer("\n---------- Entries in Selected Keystore: " + keystfile + "\n") ; KeyStore ksp = null; FileInputStream fis = null; try { ksp = KeyStore.getInstance("JKS"); fis = new FileInputStream(keystfile); ksp.load(fis, psd.toCharArray()) ; fis.close() ; } catch(KeyStoreException kse1) {;} // if keystore not recognized. catch(FileNotFoundException fnfe) {buff.append("File Not Found\n" + tail); return buff.toString() ;} catch(IOException ioe) { // this will happen if either keystore is not JKS (so try PKCS12), or bad password passed. try { ksp = KeyStore.getInstance("PKCS12"); fis = new FileInputStream(keystfile); ksp.load(fis, psd.toCharArray()) ; fis.close() ; } catch(FileNotFoundException fnfe) {buff.append("File Not Found\n" + tail); return buff.toString() ;} catch(IOException ioe1) { buff.append("Bad password, or unsupported keystore type.\n" + tail); return buff.toString() ;} catch(KeyStoreException kse2) { buff.append("Tried PKCS12 and JKS. KeyStore Type is neither JKS nor PKCS12 or no support for PKCS12.\n" + tail); return buff.toString() ;} catch(NoSuchAlgorithmException nsae) {buff.append("Algorithm Exception\n" + tail); return buff.toString() ;} catch(CertificateException nsse) { buff.append("Certificate Exception\n" + tail); return buff.toString() ;} } catch(NoSuchAlgorithmException nsae) {buff.append("Algorithm Exception\n" + tail); return buff.toString() ;} catch(CertificateException nsse) { buff.append("Certificate Exception\n" + tail); return buff.toString() ;} try { fis.close() ; buff.append("Keystore Provider: " + ksp.getProvider() + "\n"); buff.append("Keystore Contains: " + ksp.size() + " entries\n"); int entcount = 0 ; for (Enumeration e =ksp.aliases() ; e.hasMoreElements() ;) { String entry = (String) e.nextElement() ; buff.append("---------- Entry: " + ++entcount + " ----------\n"); buff.append(entry); if(ksp.isKeyEntry(entry)) buff.append(" [Key Entry]\n") ; else buff.append(" [Trusted Certificate Entry]\n") ; java.security.cert.Certificate [] certchain = ksp.getCertificateChain(entry) ; if (certchain !=null) { buff.append( certchain.length + " certs in this entry.\n") ; int certsinchain=certchain.length ; for(int i=0; i