/* * AuthDialog.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.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.border.TitledBorder; /** * AuthDialog - Get the appropriate authorization * (KeyStore pass, key pass, or both) * */ /////////////////////////////////////////////////////////////////////// class AuthDialog extends JDialog { /** * */ CryptoFile cryptoFile; final JPasswordField ksPassField = new JPasswordField(15); final JPasswordField keyPassField = new JPasswordField(15); final JRadioButton yesButton = new JRadioButton("Yes" , true); public AuthDialog( JFrame frame, boolean modal, int whatsRequired, CryptoFile cryptoFileAuth ) { super(frame, modal); cryptoFile = cryptoFileAuth; initComponents(whatsRequired); pack(); setLocationRelativeTo(frame); setTitle("Professional Data Security (PDS) - Authorization"); setVisible(true); } // // Popup a dialog prompting for the desired authentication // whatIsRequired (2 = KeyStore, 3 = Key, 6 = KeyStore+Key) // void initComponents ( final int whatsRequired ) { JRadioButton noButton = new JRadioButton("No" , false); ButtonGroup sharePassButton = new ButtonGroup(); ksPassField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { process( whatsRequired ); } } ); keyPassField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { process( whatsRequired ); } } ); ///////////////////////////////////////////////////////////// // Build the Dialog 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(); JPanel p2 = new JPanel( flowLayoutLeft ); JPanel p3 = new JPanel( flowLayoutLeft ); 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("Authentication Required"); p1.add(jlabel); if ( (whatsRequired % 2) == 0 ) { // p2 - KeyStore Passphrase JPanel p2SubPanel = new JPanel(new FlowLayout()); p2.add(p2SubPanel); p2.setBorder(new TitledBorder("KeyStore")); JLabel p2Label = new JLabel("Enter the KeyStore Passphrase: "); p2SubPanel.add(p2Label); ksPassField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( ksPassField.getPassword().length > 0 ) { process( whatsRequired); } } } ); p2SubPanel.add(ksPassField); } if ( (whatsRequired % 3) == 0 ) { // p3 - Key Passphrase p3.setBorder(new TitledBorder("Key")); JPanel p3SubPanel = new JPanel(new GridLayout(0,1)); p3.add(p3SubPanel); keyPassField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( keyPassField.getPassword().length > 0 ) { process( whatsRequired); } } } ); keyPassField.setEnabled(true); if ( (whatsRequired % 2) == 0 ) { JPanel p3SubPanela = new JPanel( flowLayoutLeft ); p3SubPanel.add(p3SubPanela); JLabel p3Label = new JLabel("KeyStore / Key share the same passphrase: "); p3SubPanela.add( p3Label, BorderLayout.WEST ); p3SubPanela.add( yesButton, BorderLayout.WEST); p3SubPanela.add( noButton, BorderLayout.WEST ); sharePassButton.add(yesButton); sharePassButton.add(noButton); noButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { keyPassField.setEnabled(true); } }); yesButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { keyPassField.setEnabled(false); } }); keyPassField.setEnabled(false); } JPanel p3SubPanelb = new JPanel( flowLayoutLeft ); p3SubPanel.add(p3SubPanelb); JLabel p3Label2 = new JLabel("Enter the Key Passphrase: "); p3SubPanelb.add(p3Label2, BorderLayout.WEST); keyPassField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( keyPassField.getPassword().length > 0 ) { process( whatsRequired); } } } ); p3SubPanelb.add(keyPassField, BorderLayout.WEST); } // p4 - Accept & Cancel Buttons JButton jButtonAccept = new JButton("Accept"); jButtonAccept.setMnemonic(KeyEvent.VK_A); Gui.enterPressesWhenFocused(jButtonAccept); jButtonAccept.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { process( whatsRequired); } }); p4.add(jButtonAccept); JButton jButtonCancel = new JButton("Cancel"); jButtonCancel.setMnemonic(KeyEvent.VK_C); Gui.enterPressesWhenFocused(jButtonCancel); jButtonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println("Canceling..."); // Clear KeyStore Pass char[] nullPass = null; cryptoFile.setKsPass(nullPass); // Set marker to indicate Cancel StateObject.setFileChooserCancel(true); setVisible(false); dispose(); } }); p4.add(jButtonCancel); } void process (int whatsRequired) { if ( (whatsRequired % 2) == 0 ) { // Get KeyStore Passphrase cryptoFile.setKsPass(ksPassField.getPassword()); // System.out.print("**** KeyStore Passphrase: "); // for ( int i = 0; i < cryptoFile.getKsPass().length; i++ ) { // System.out.print( cryptoFile.getKsPass()[i] ); // } // System.out.println(); } if ( (whatsRequired % 3) == 0 ) { // Get Key Passphrase cryptoFile.setKeyPass(keyPassField.getPassword()); if (( yesButton.isSelected() ) && (whatsRequired != 3)) { // Set Key Passphrase to KeySTORE Passphrase // System.out.println("*** Setting KeyPass to KsPass ***"); cryptoFile.setKeyPass(cryptoFile.getKsPass()); } // System.out.print("**** Key Passphrase: "); // for ( int i=0; i < cryptoFile.getKeyPass().length; i++ ) { // System.out.print( cryptoFile.getKeyPass()[i] ); // } // System.out.println(); } boolean pass = true; if ( (whatsRequired % 2) == 0 ) { if (ksPassField.getPassword().length == 0) { pass = false; JOptionPane.showMessageDialog( new JFrame(), "KeyStore passphrase cannot be empty.", "KeyStore Passphrase", JOptionPane.WARNING_MESSAGE); } } else if ( (whatsRequired % 3) == 0 ) { if (keyPassField.getPassword().length == 0) { pass = false; JOptionPane.showMessageDialog( new JFrame(), "Key passphrase cannot be empty.", "Key Passphrase", JOptionPane.WARNING_MESSAGE); } } if ( pass ) { setVisible(false); dispose(); } } }