/* * EditKeyDialog.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.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.security.Key; import java.security.KeyStore; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.RowSorter; import javax.swing.SortOrder; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; /** * EditKey - File-> Edit-> Key "Dialog" * */ class EditKeyDialog { CryptoFile cryptoFile = new CryptoFile(); private final Gui mGui; final JTextField keyStoreNameField = new JTextField("", 20); final JTextField keyAliasNameField = new JTextField( 20 ); JDialog editKeyDialog; String newline = System.getProperty("line.separator"); /** * Constructor */ public EditKeyDialog (Gui gui) { ///////////////////////////////////////////////////////////// // Build the Dialog mGui = gui; editKeyDialog = new JDialog(mGui.mainFrame, "Edit Key(s)", true); JPanel contentPane = new JPanel(); editKeyDialog.getContentPane().add(contentPane); GroupLayout layout = new GroupLayout(contentPane); contentPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); FlowLayout flowLayoutLeft = new FlowLayout(); flowLayoutLeft.setAlignment(FlowLayout.LEFT); JPanel p1 = new JPanel(); JPanel p2 = new JPanel( new GridLayout(1,3) ); JPanel p3 = new JPanel(); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(p1) .addComponent(p2) .addComponent(p3) )); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(p1) .addComponent(p2) .addComponent(p3) ); ///////////////////////////////////////////////////////////// // p1 - Title editKeyDialog.setTitle("Professional Data Security (PDS) - Edit Key(s) within a KeyStore"); JLabel jlabel = null; jlabel = new JLabel("Select the KeyStore"); p1.add(jlabel); // p2 - Locate the KeyStore that contains the Key p2.setBorder(new TitledBorder("KeyStore Containing the Encryption Key(s)")); JPanel p2SubPanel = new JPanel(flowLayoutLeft); p2.add(p2SubPanel); JLabel p2Label = new JLabel("Path to KeyStore : "); p2SubPanel.add(p2Label); p2SubPanel.add(keyStoreNameField); keyStoreNameField.setEnabled(false); final JButton p2iconOpen = new JButton(Gui.createImageIcon("images/folder.gif", 28)); p2iconOpen.setFocusable(false); Gui.enterPressesWhenFocused(p2iconOpen); p2iconOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { JFrame frame = new JFrame(); // // Get KeyStore Filename (filename set in associateKeyStoreActionPerformed) // if ( associateKeyStoreActionPerformed(evt, cryptoFile) != 0 ) { editKeyDialog.setVisible(false); editKeyDialog.dispose(); return; } // // Get KeyStore passphrase (passphrase set in authDialog) // (2 = KeyStore, 3 = Key, 6 = KeyStore+Key) // new AuthDialog(frame, true, 2, cryptoFile); if ( cryptoFile.getKsPass() == null ) { editKeyDialog.setVisible(false); editKeyDialog.dispose(); return; } // // Get Key Alias (alias set in KeySelectorDialog) // new EditKeyDoWorkDialog (frame, true); if ( cryptoFile.getKeyAlias() == null ) { editKeyDialog.setVisible(false); editKeyDialog.dispose(); return; } editKeyDialog.setVisible(false); editKeyDialog.dispose(); } }); p2iconOpen.setToolTipText("Locate Encryption Key"); p2SubPanel.add(p2iconOpen); ///////////////////////////////////////////////////////////// // Finish up editKeyDialog.pack(); editKeyDialog.setLocationRelativeTo(null); editKeyDialog.setVisible(true); } /////////////////////////////////////////////////////////////////////// /** * Listeners for the EditKeyDialog class * */ /////////////////////////////////////////////////////////////////////// // // Associate the CryptoFile with a Key in a KeyStore // private int associateKeyStoreActionPerformed(ActionEvent evt, CryptoFile cryptoFile) { int returnVal; JFileChooser fileChooser; if ( mGui.stateObject.getDefaultKeyStoreDir() != null ) { fileChooser = new JFileChooser ( mGui.stateObject.getDefaultKeyStoreDir() ); } else { fileChooser = new JFileChooser ( mGui.runTimePath ); } fileChooser.setDialogTitle( "KeyStore Location" ); fileChooser.setApproveButtonText("Select"); FileNameExtensionFilter filterPDS = new FileNameExtensionFilter("Professional Data Security Files (PDS)", "PDS"); FileNameExtensionFilter filterJCEKS = new FileNameExtensionFilter("Java Cryptographic Extension (JCE) KeyStores (JCEKS)", "JCEKS"); FileNameExtensionFilter filterJKS = new FileNameExtensionFilter("Java KeyStores (JKS)", "JKS"); FileNameExtensionFilter filterKeyStores = new FileNameExtensionFilter("Java KeyStores (JCEKS, JKS)", "JCEKS", "JKS"); fileChooser.addChoosableFileFilter( filterJKS ); fileChooser.addChoosableFileFilter( filterJCEKS ); fileChooser.addChoosableFileFilter( filterPDS ); fileChooser.addChoosableFileFilter(filterKeyStores); returnVal = fileChooser.showOpenDialog(editKeyDialog); if ( returnVal == 0 ) { // Set the KeyStore name in the new CryptoFile object cryptoFile.setKsName( fileChooser.getSelectedFile().getPath() ); // System.out.println("KeyStore Name: "); // System.out.println(cryptoFile.getKsName()); // Update the Gui keyStoreNameField.setText( cryptoFile.getKsName() ); keyStoreNameField.setEnabled(true); keyStoreNameField.repaint(); // System.out.print("KeyStore: " + cryptoFile.getKsName() + newline); } else if ( returnVal == 1 ) { // System.out.print("Canceling..." + newline); } else { System.out.print("Error: " + returnVal + mGui.newline); } return returnVal; } /////////////////////////////////////////////////////////////////////// /** * Inner class instantiated by the ActionListener that is * responsible for associating a File with a KeyStore Key * */ /////////////////////////////////////////////////////////////////////// // // Popup a dialog showing the keys in a given KeyStore // class EditKeyDoWorkDialog extends JDialog { final JButton jButtonSetDefault = new JButton("Set as Default Key"); final JButton jButtonChangePass = new JButton("Change Passphrase"); final JButton jButtonDelete = new JButton("Delete"); final JPasswordField oldpw = new JPasswordField(15); final JPasswordField newpw1 = new JPasswordField(15); final JPasswordField newpw2 = new JPasswordField(15); public EditKeyDoWorkDialog(JFrame frame, boolean modal) { super(frame, modal); if (! initComponents() ) { return; } pack(); setLocationRelativeTo(frame); setTitle("Professional Data Security (PDS) - Edit Key(s) within a KeyStore"); setVisible(true); } boolean initComponents () { String ksElements[][] = null; // System.out.println("Selecting key from KeyStore..."); ///////////////////////////////////////////////////////////// // Build the Dialog final JPanel contentPane = new JPanel(); GroupLayout layout = new GroupLayout(contentPane); contentPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); add(contentPane); FlowLayout flowLayoutLeft = new FlowLayout(); flowLayoutLeft.setAlignment(FlowLayout.LEFT); JPanel p1 = new JPanel(); final JPanel p2 = new JPanel( flowLayoutLeft ); JPanel p3 = new JPanel(); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(p1) .addComponent(p2) .addComponent(p3) ) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(p1) .addComponent(p2) .addComponent(p3) ); ///////////////////////////////////////////////////////////// // p1 - Title JLabel jlabel = new JLabel("Keys in the selected KeyStore"); p1.add(jlabel); // p2 - KeyStore name final JPanel p2SubPanel = new JPanel(new FlowLayout()); p2.add(p2SubPanel); p2.setBorder(new TitledBorder("Select a Key to Modify or Delete")); // Need to get list of keys from KeyStore try { ksElements = new CryptoKeyStore().getKeyStore( cryptoFile.getKsName(), cryptoFile.getKsPass() ); } catch (Exception ex) { // System.out.println("Error getting KeyStore: " + ex); } if ( ksElements == null ) { // System.out.println("No keys in the store, or invalid passphrase provided."); StateObject.setFileChooserCancel(true); JOptionPane.showMessageDialog( mGui.mainFrame, "Unable to recover any Keys from this KeyStore." + newline + mGui.newline + "Try again with different credentials or" + mGui.newline + "select a different KeyStore.", "No Keys Recovered", JOptionPane.WARNING_MESSAGE); return false; } // System.out.println("Listing keys in Gui.java"); // if ( ksElements.length > 0 ) { // for (int count = 0, total = ksElements.length; count < total; ++count ) { // System.out.print("Created: " + ksElements[count][0] + "\t\t" ); // System.out.println("Alias: " + ksElements[count][1]); // } // } String col [] = {"Created", "Key Alias"}; final DefaultTableModel model = new DefaultTableModel(ksElements,col); final JTable table = new JTable( model ) { public boolean isCellEditable(int row, int column) { return false; } public Dimension getPreferredScrollableViewportSize() { Dimension size = super.getPreferredScrollableViewportSize(); return new Dimension(size.width, Math.min(getPreferredSize().height, 110)); } public void valueChanged(ListSelectionEvent e) { super.valueChanged( e ); jButtonSetDefault.setEnabled(true); jButtonChangePass.setEnabled(true); jButtonDelete.setEnabled(true); } }; table.setAutoResizeMode ( JTable.AUTO_RESIZE_ALL_COLUMNS ); table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); table.setRowSelectionAllowed(true); table.setAutoCreateRowSorter(true); table.setUpdateSelectionOnSort(true); TableRowSorter sorter = new TableRowSorter(table.getModel()); table.setRowSorter(sorter); Comparator dateComparator = new Comparator() { public int compare(String s1, String s2) { try { Date d1 = new Date(s1); Date d2 = new Date(s2); return d1.compareTo(d2); } catch (Exception e) { System.out.println("Error comparing dates in JTable"); return 0; } } }; sorter.setComparator(0, dateComparator); java.util.List sortKeys = new ArrayList(); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); sorter.setSortKeys(sortKeys); sorter.sort(); JScrollPane scrollPane = new JScrollPane(table); p2SubPanel.add(scrollPane); // p3 - Set as Default, Change Passphrase, Delete & Cancel Buttons jButtonSetDefault.setMnemonic(KeyEvent.VK_S); Gui.enterPressesWhenFocused(jButtonSetDefault); jButtonSetDefault.setEnabled(false); jButtonSetDefault.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // System.out.println("Getting selected key"); int selected = table.getSelectedRow(); String keyAlias = table.getValueAt(selected,1).toString(); // System.out.println("Key alias selected was: " + keyAlias); // System.out.println("Resetting Key Table...Default Key..."); table.clearSelection(); jButtonSetDefault.setEnabled(false); jButtonChangePass.setEnabled(false); jButtonDelete.setEnabled(false); // - update state object w/ ksName and keyAlias mGui.stateObject.setDefaultKeyStore( cryptoFile.getKsName() ); mGui.stateObject.setDefaultKeyAlias( keyAlias ); mGui.stateObject.setHaveDefaults( true ); // save state to "disk" JFrame frame = new JFrame(); // Save in-memory state to persistent storage. if ( mGui.stateObject.saveState() ) { JOptionPane.showMessageDialog( frame, "Key set to the Default Key." , "Success", JOptionPane.INFORMATION_MESSAGE, Gui.createImageIcon("images/PDS_Logo-32.png")); } else { System.out.println("Error saving current state."); JOptionPane.showMessageDialog( frame, "Unable to update persistent storage.", "Error", JOptionPane.ERROR_MESSAGE); } } }); p3.add(jButtonSetDefault); jButtonChangePass.setMnemonic(KeyEvent.VK_P); Gui.enterPressesWhenFocused(jButtonChangePass); jButtonChangePass.setEnabled(false); jButtonChangePass.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // System.out.println("Getting selected key"); int selected = table.getSelectedRow(); String keyAlias = table.getValueAt(selected,1).toString(); // System.out.println("Key alias selected was: " + keyAlias); // Set the Key Alias in the new CryptoFile object cryptoFile.setKeyAlias(keyAlias); // System.out.println("Key Alias: " + cryptoFile.getKeyAlias() ); // Issue "Risky Business" warning JFrame frame = new JFrame(); int n = JOptionPane.showOptionDialog( frame, "IMPORTANT NOTICE:" + mGui.newline + mGui.newline + "Changing a Key passphrase is a VERY risky operation." + mGui.newline + "Before proceeding you SHOULD make a backup copy of the current KeyStore." + mGui.newline + mGui.newline + "OTHERWISE, YOU RUN THE RISK OF LOSING ALL YOUR ENCRYPTED DATA." + mGui.newline + mGui.newline + "If you have not made a backup of the current KeyStore, please " + mGui.newline + "select NO and make a backup copy." + mGui.newline + mGui.newline + "Are you ready to continue?" + mGui.newline + mGui.newline + mGui.newline, "Please backup your current KeyStore !", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "No"); if ( n != 0 ) { // System.out.println("Canceling..."); // System.out.println("Resetting Key Table...Change Key Passphrase..."); table.clearSelection(); jButtonSetDefault.setEnabled(false); jButtonChangePass.setEnabled(false); jButtonDelete.setEnabled(false); return; } new ChangeKeyPassDialog(); // System.out.println("Resetting Key Table...Change Key Passphrase..."); table.clearSelection(); jButtonSetDefault.setEnabled(false); jButtonChangePass.setEnabled(false); jButtonDelete.setEnabled(false); } }); p3.add(jButtonChangePass); jButtonDelete.setMnemonic(KeyEvent.VK_D); Gui.enterPressesWhenFocused(jButtonDelete); jButtonDelete.setEnabled(false); jButtonDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // System.out.println("Getting selected key"); int selected = table.getSelectedRow(); String keyAlias = table.getValueAt(selected,1).toString(); // System.out.println("Key alias selected was: " + keyAlias); // // Set the Key Alias in the new CryptoFile object // cryptoFile.setKeyAlias(keyAlias); // System.out.println("Key Alias: " + cryptoFile.getKeyAlias() ); // Issue "Risky Business" warning JFrame frame = new JFrame(); int n = 0; // System.out.println(""); // System.out.println("Default Key Defined: " + stateObject.getHaveDefaults()); if (mGui.stateObject.getHaveDefaults()) { // System.out.println("Default Key Alias: " + stateObject.getDefaultKeyAlias()); // System.out.println("Default KeyStore : " + stateObject.getDefaultKeyStore()); // System.out.println("Current KeyStore : " + cryptoFile.getKsName() ); if (( mGui.stateObject.getDefaultKeyAlias().equals(keyAlias) ) && ( mGui.stateObject.getDefaultKeyStore().equals(cryptoFile.getKsName()))) { // System.out.println("Deleting the Default Key"); n = JOptionPane.showOptionDialog( frame, "IMPORTANT NOTICE:" + mGui.newline + mGui.newline + "You are about to delete the *DEFAULT* Encryption Key." + mGui.newline + mGui.newline + "Key Alias: " + keyAlias + mGui.newline + mGui.newline + "Any data that is encrypted by this key will be LOST." + mGui.newline + mGui.newline + "Continue?", "Delete a Key ?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "No"); } else { // System.out.println("Deleting a Key - not the Default"); n = JOptionPane.showOptionDialog( frame, "IMPORTANT NOTICE:" + mGui.newline + mGui.newline + "You are about to delete an Encryption Key." + mGui.newline + mGui.newline + "Key Alias: " + keyAlias + mGui.newline + mGui.newline + "Any data that is encrypted by this key will be LOST." + mGui.newline + mGui.newline + "Continue?", "Delete a Key ?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "No"); } } else { // System.out.println("Deleting a Key - not the Default"); n = JOptionPane.showOptionDialog( frame, "IMPORTANT NOTICE:" + mGui.newline + mGui.newline + "You are about to delete an Encryption Key." + mGui.newline + mGui.newline + "Key Alias: " + keyAlias + mGui.newline + mGui.newline + "Any data that is encrypted by this key will be LOST." + mGui.newline + mGui.newline + "Continue?", "Delete a Key ?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "No"); } // System.out.println("n: " + n); if ( n == 1 ) { // System.out.println("Canceling: " + n); // System.out.println("Resetting Key Table...Delete Encryption Key..."); table.clearSelection(); jButtonSetDefault.setEnabled(false); jButtonChangePass.setEnabled(false); jButtonDelete.setEnabled(false); return; } // Delete the Key // System.out.println("Deleting an Encryption Key."); try { // Load the KeyStore // System.out.println("Loading the KeyStore: " + cryptoFile.getKsName() ); KeyStore ks = CryptoKeyStore.loadKeyStore( cryptoFile.getKsName(), cryptoFile.getKsPass() ); // System.out.print("**** KeyStore Passphrase: "); // for ( int i = 0; i < cryptoFile.getKsPass().length; i++ ) { // System.out.print( cryptoFile.getKsPass()[i] ); // } // System.out.println(); // Delete the key via the alias // System.out.println("Deleting key: " + keyAlias ); ks.deleteEntry( keyAlias ); // Save the KeyStore // System.out.println("Saving the KeyStore."); if ( CryptoKeyStore.saveKeyStore( ks, cryptoFile.getKsPass(), cryptoFile.getKsName() ) ) { if (mGui.stateObject.getHaveDefaults()) { if (( mGui.stateObject.getDefaultKeyAlias().equals(keyAlias) ) && ( mGui.stateObject.getDefaultKeyStore().equals(cryptoFile.getKsName()))) { // System.out.println("Clearing Default Key Params"); mGui.stateObject.setDefaultKeyAlias( null ); mGui.stateObject.setDefaultKeyStore( null ); mGui.stateObject.setHaveDefaults( false ); } } JOptionPane.showMessageDialog( frame, "Key successfully deleted." , "Success", JOptionPane.INFORMATION_MESSAGE, Gui.createImageIcon("images/PDS_Logo-32.png")); } // Update the model // System.out.println("Updating the model."); // System.out.println("Lines in Model before: " + model.getRowCount()); // Check to see if it is in the model first int i = model.getRowCount(); int row = -1; while ( i > 0 ) { --i; // System.out.println ( (String) model.getValueAt(i, 0) ); if ( keyAlias.equals( model.getValueAt(i, 1) ) ) { // System.out.println("Match"); row = i; } } if ( row >= 0 ) { // System.out.println("Removing row: " + i); model.removeRow(row); } else { System.out.println("Key Alias not found in model: " + keyAlias); } // Update the table // System.out.println("Resetting Key Table...Delete Encryption Key..."); table.setModel(model); table.revalidate(); table.clearSelection(); table.setFillsViewportHeight(true); table.getPreferredScrollableViewportSize(); table.repaint(); jButtonSetDefault.setEnabled(false); jButtonChangePass.setEnabled(false); jButtonDelete.setEnabled(false); p2SubPanel.revalidate(); p2SubPanel.repaint(); p2.revalidate(); p2.repaint(); contentPane.revalidate(); contentPane.repaint(); // no frame to resize/repaint. variable passed as arg to constructor. // change code at some point? probably not. doesn't look too bad. } catch (Exception ex) { // notify if failure System.out.println("Error : " + ex); JOptionPane.showMessageDialog( frame, "Error Deleting Key: " + ex, "Error Deleting Key" , JOptionPane.ERROR_MESSAGE); } } }); p3.add(jButtonDelete); JButton jButtonClose = new JButton("Close"); Gui.enterPressesWhenFocused(jButtonClose); jButtonClose.setMnemonic(KeyEvent.VK_C); jButtonClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println("Close"); setVisible(false); dispose(); } }); // This button should be the default - but it won't take it // jButtonClose.setDefaultCapable(true); // getRootPane().setDefaultButton(jButtonClose); // jButtonClose.requestFocusInWindow(); // jButtonClose.requestFocus(); // contentPane.repaint(); // if ( jButtonClose.isDefaultButton() ) { // System.out.println("Is default"); // } else { // System.out.println("Is NOT default"); // } // if ( jButtonClose.isDefaultCapable() ) { // System.out.println("Is capable"); // } else { // System.out.println("Is NOT capable"); // } p3.add(jButtonClose); return true; } // //////////////////////////////////////////////////////////// // Inner-Inner Class // Will change the passphrase for a key class ChangeKeyPassDialog { public ChangeKeyPassDialog() { ///////////////////////////////////////////////////////////// // Build the Dialog final JDialog changeKeyPassDialog = new JDialog(mGui.mainFrame, "Modify a Key Passphrase", true); JPanel contentPane = new JPanel(); GroupLayout layout = new GroupLayout(contentPane); contentPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); changeKeyPassDialog.getContentPane().add(contentPane); FlowLayout flowLayoutLeft = new FlowLayout(); flowLayoutLeft.setAlignment(FlowLayout.LEFT); JPanel p1 = new JPanel(); JPanel p2 = new JPanel( flowLayoutLeft ); JPanel p3 = new JPanel( new GridLayout(0,1) ); JPanel p4 = new JPanel(); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) ) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) ); ///////////////////////////////////////////////////////////// // p1 - Title JLabel jlabel = new JLabel("Modify a Key Passphrase"); p1.add(jlabel); // p2 - Old passphrase JPanel p2SubPanel = new JPanel( new GridLayout(1,2) ); p2.add(p2SubPanel); p2.setBorder(new TitledBorder("Current Passphrase")); p2SubPanel.add(new JLabel("Passphrase: "), BorderLayout.WEST); p2SubPanel.add(oldpw, BorderLayout.WEST); // p3 - New passphrase JPanel p3SubPanel2a = new JPanel(flowLayoutLeft); p3.add(p3SubPanel2a); JPanel p3SubPanel2b = new JPanel(new GridLayout(2,2)); p3SubPanel2a.add(p3SubPanel2b); p3.setBorder(new TitledBorder("New Passphrase")); p3SubPanel2b.add(new JLabel("Passphrase: "), BorderLayout.WEST); p3SubPanel2b.add(newpw1, BorderLayout.WEST); p3SubPanel2b.add(new JLabel("Passphrase (again): "), BorderLayout.WEST); newpw2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( newpw2.getPassword().length > 0 ) { new ChangeKeyPassActionPerformed(); changeKeyPassDialog.setVisible(false); changeKeyPassDialog.dispose(); } } } ); p3SubPanel2b.add(newpw2, BorderLayout.WEST); // p4 - Modify & Cancel Buttons JButton jButtonModify = new JButton("Modify"); jButtonModify.setMnemonic(KeyEvent.VK_M); Gui.enterPressesWhenFocused(jButtonModify); jButtonModify.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { new ChangeKeyPassActionPerformed(); changeKeyPassDialog.setVisible(false); changeKeyPassDialog.dispose(); } }); p4.add(jButtonModify); JButton jButtonCancel = new JButton("Cancel"); jButtonCancel.setMnemonic(KeyEvent.VK_C); Gui.enterPressesWhenFocused(jButtonCancel); jButtonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jButtonSetDefault.setEnabled(false); jButtonChangePass.setEnabled(false); jButtonDelete.setEnabled(false); oldpw.setText(""); newpw1.setText(""); newpw2.setText(""); // System.out.println("Canceling..."); changeKeyPassDialog.setVisible(false); changeKeyPassDialog.dispose(); } }); p4.add(jButtonCancel); changeKeyPassDialog.pack(); changeKeyPassDialog.setLocationRelativeTo(null); changeKeyPassDialog.setVisible(true); } class ChangeKeyPassActionPerformed { public ChangeKeyPassActionPerformed() { char[] oldPass = oldpw.getPassword(); char[] newPass1 = newpw1.getPassword(); char[] newPass2 = newpw2.getPassword(); // System.out.print("Old Passphrase : "); // for ( int i = 0; i < oldPass.length; i++ ) { // System.out.print( oldPass[i] ); // } // System.out.println(); // System.out.print("First Passphrase : "); // for ( int i = 0; i < newPass1.length; i++ ) { // System.out.print( newPass1[i] ); // } // System.out.println(); // System.out.print("Next Passphrase : "); // for ( int i = 0; i < newPass2.length; i++ ) { // System.out.print( newPass2[i] ); // } // System.out.println(); JFrame frame = new JFrame(); // Was the first key pass provided and correct if ( ! (newPass1.length >= 8) ) { // notify if failure JOptionPane.showMessageDialog( frame, "Key passphrase must be at least 8 characters, " + mGui.newline + "containing both upper case and lower case characters " + mGui.newline + "as well as some other character.", "Passphrase empty", JOptionPane.WARNING_MESSAGE); return; } // Check for correct syntax boolean upper = false; // set to true if test passes boolean lower = false; // set to true if test passes boolean other = false; // set to true if test passes // Check for upper case first for ( int i = 0; i < newPass1.length; i++ ) { for ( int n = 65; n <= 90; n++ ) { if ( ( (int) newPass1[i] ) == n ) { upper = true; n = 91; i = newPass1.length; } } } // System.out.println("Contained upper: " + upper); // Check for lower case next for ( int i = 0; i < newPass1.length; i++ ) { for ( int n = 97; n <= 122; n++ ) { if ( ( (int) newPass1[i] ) == n ) { lower = true; n = 123; i = newPass1.length; } } } // System.out.println("Contained lower: " + lower); // Check for existence of another char last for ( int i = 0; i < newPass1.length; i++ ) { boolean otherUpper = false; boolean otherLower = false; // Is it an upper case char ? for ( int n = 65; n <= 90; n++ ) { if ( ( (int) newPass1[i] ) == n ) { otherUpper = true; n = 91; } } // If not upper, then is it lower case char? if ( ! otherUpper ) { for ( int j = 97; j <= 122; j++ ) { if ( ( (int) newPass1[i] ) == j ) { otherLower = true; j = 123; } } } if ( ( ! otherUpper ) && ( ! otherLower ) ) { other = true; i = newPass1.length; } } // System.out.println("Contained other: " + other); if( ! ( upper && lower && other ) ) { // notify if failure JOptionPane.showMessageDialog( frame, "KeyStore passphrase must be at least 8 characters, " + mGui.newline + "containing both upper case and lower case characters " + mGui.newline + "as well as some other character.", "Passphrase empty", JOptionPane.WARNING_MESSAGE); return; } // Did the passwords match if ( ! java.util.Arrays.equals(newPass1,newPass2) ) { // notify if failure JOptionPane.showMessageDialog( frame, "Passphrases did not match.", "Passphrase mismatch", JOptionPane.WARNING_MESSAGE); return; } // System.out.println("Modifying Key Passphrase..."); try { // Assign provided Key pass to "cryptoFile" cryptoFile.setKeyPass(oldPass); // Load the KeyStore KeyStore ks = CryptoKeyStore.loadKeyStore( cryptoFile.getKsName(), cryptoFile.getKsPass() ); // Load the key Key skey = new CryptoKey().getKey ( cryptoFile ); // Save the key with a new pass ks.setKeyEntry( cryptoFile.getKeyAlias(), skey, newPass1, null); // Save it with a new passphrase CryptoKeyStore.saveKeyStore( ks, cryptoFile.getKsPass(), cryptoFile.getKsName() ); JOptionPane.showMessageDialog( frame, "Key passphrase successfully changed." , "Success", JOptionPane.INFORMATION_MESSAGE, Gui.createImageIcon("images/PDS_Logo-32.png")); } catch (Exception ex) { // notify if failure System.out.println("Error : " + ex); JOptionPane.showMessageDialog( frame, "Unable to change the passphrase.", "Error", JOptionPane.ERROR_MESSAGE); } finally { // Scrub the passphrases // System.out.println("Scrubbing passphrases in Gui.editKeyStorePass()..."); for ( int i = 0; i < oldPass.length; i++ ) { oldPass[i] = '0'; } for ( int i = 0; i < newPass1.length; i++ ) { newPass1[i] = '0'; } for ( int i = 0; i < newPass2.length; i++ ) { newPass2[i] = '0'; } oldpw.setText(""); newpw1.setText(""); newpw2.setText(""); jButtonSetDefault.setEnabled(false); jButtonChangePass.setEnabled(false); jButtonDelete.setEnabled(false); } } } } } }