/* * CryptoKeyStore.java * * * ==================================================== Professional Data Security (PDS) http://crypto.brettlee.com ==================================================== Copyright (c) 2009-2011, Brett Lee All rights reserved. Portions Copyright (C) 1995-2008, Sun Microsystems, Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the ORGANIZATION nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ============================================================================= */ package com.brettlee.crypto; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.util.Enumeration; import javax.swing.JFrame; import javax.swing.JOptionPane; class CryptoKeyStore { String newline = System.getProperty("line.separator"); static KeyStore loadKeyStore( String ksFileName, char[] ksPass ) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { // System.out.println("Beginning CryptoKeyStore.loadKeyStore..."); // Get extension of KeyStore (JKS, JCEKS) int dotPos = ksFileName.lastIndexOf("."); String extension = ksFileName.substring(dotPos + 1); // System.out.println("KeyStore extension is: " + extension); // Creating empty KeyStore final KeyStore ks = KeyStore.getInstance( extension ); // System.out.println("KeyStore has been set to type: " + ks.getType()); // Read KeyStore from storage into new KeyStore // System.out.println("KeyStore is: " + ksFileName); // System.out.println("Loading KeyStore from storage ..."); File ksFileIn = new File( ksFileName ); if ( ksFileIn.exists() ) { // System.out.println("File exists..."); // don't buffer KeyStore; it's tiny anyway final FileInputStream input = new FileInputStream( ksFileIn ); try { ks.load( input, ksPass ); } finally { input.close(); } } else { // notify if failure System.out.println("Error: KeyStore was not found!"); JFrame frame = new JFrame(); JOptionPane.showMessageDialog( frame, "Application error:: KeyStore was not found.", "KeyStore not found", JOptionPane.WARNING_MESSAGE); return null; } return ks; } // should be boolean return static boolean saveKeyStore ( final KeyStore keyStore, final char[] ksPass, String ksFileName ) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { File ksFile = new File( ksFileName ); final FileOutputStream out = new FileOutputStream(ksFile); boolean returnVal = true; try { keyStore.store( out, ksPass ); } catch ( Exception e ) { System.out.println("Error writing KeyStore: " + e ); returnVal = false; } finally { // Scrub the passwords // System.out.println("Scrubbing passwords in CryptoKeyStore.saveKeyStore()..."); // for ( int i=0; i < ksPass.length; i++ ) { ksPass[i] = '0'; } out.close(); } return returnVal; } // should be boolean return boolean createKeyStore ( String ksType, // [0]: Type of KeyStore "Instance" (JKS, JCEKS) "ksType" char[] ksPass, // [1]: Passphrase of KeyStore "ksPass" String ksFileName // [2]: Filename of KeyStore "ksFile" ) throws Exception { // Begin // System.out.println(newline + "Beginning CryptoKeyStore.createKeyStore..."); // Generate KeyStore // System.out.println("Creating new KeyStore ..."); KeyStore ks = KeyStore.getInstance( ksType ); ks.load( (InputStream) null, ksPass); // Write KeyStore to disk // System.out.println("Writing KeyStore to disk..."); boolean returnVal = saveKeyStore( ks, ksPass, ksFileName ); // Scrub the passwords // System.out.println("Scrubbing passwords in CryptoKeyStore.createKeyStore()..."); // for ( int i=0; i < ksPass.length; i++ ) { ksPass[i] = '0'; } return returnVal; } String[][] getKeyStore ( String keyStoreName, char[] ksPass ) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { // System.out.println("Starting CryptoKeyStore().getKeyStore() with: " + keyStoreName); // System.out.println("KeyStore Name : " + keyStoreName); // System.out.println("KeyStore Pass : " + ksPass); // System.out.println("Loading KeyStore: " + keyStoreName); KeyStore ks = loadKeyStore ( keyStoreName, ksPass ); // System.out.println("KeyStore Type: " + ks.getType() ); int keyCount = ks.size(); // System.out.println("Keys in KeyStore: " + keyCount); // System.out.println("Getting Key Aliases and Creation Dates..."); Enumeration ksEnum = ks.aliases(); String[][] ksElements = new String[keyCount][2]; for (int count=0; ksEnum.hasMoreElements(); ++count ) { ksElements[count][1] = (String)ksEnum.nextElement(); ksElements[count][0] = (String)ks.getCreationDate(ksElements[count][1]).toString(); // System.out.print("Created: " + ksElements[count][0] + "\t\t"); // System.out.println("Alias: " + ksElements[count][1]); } // // Want to sort the elements by date. How 'bout an ArrayList ? // // System.out.println("Sorting..."); // Arrays.sort(ksElements); // System.out.println("Displaying again..."); // for (int count=0; count < ksElements.length; ++count ) { // ksElements[count][0] = (String)ks.getCreationDate(ksElements[count][1]).toString(); // System.out.print("Alias: " + ksElements[count][1] + "\t\t" ); // System.out.println("Created: " + ksElements[count][0]); // } return ksElements; } }