org/objectweb/proactive/ext/security/gui/ProActiveCertificateGeneratorGUI.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.security.gui;
00032 
00033 import java.awt.BorderLayout;
00034 import java.awt.event.ActionListener;
00035 import java.awt.event.WindowAdapter;
00036 import java.awt.event.WindowEvent;
00037 import java.io.File;
00038 import java.io.FileInputStream;
00039 import java.io.FileNotFoundException;
00040 import java.io.FileOutputStream;
00041 import java.io.IOException;
00042 import java.security.InvalidKeyException;
00043 import java.security.KeyPair;
00044 import java.security.KeyStore;
00045 import java.security.KeyStoreException;
00046 import java.security.NoSuchAlgorithmException;
00047 import java.security.NoSuchProviderException;
00048 import java.security.PrivateKey;
00049 import java.security.SignatureException;
00050 import java.security.UnrecoverableKeyException;
00051 import java.security.cert.Certificate;
00052 import java.security.cert.CertificateException;
00053 import java.security.cert.CertificateFactory;
00054 import java.security.cert.X509Certificate;
00055 import java.util.Enumeration;
00056 import java.util.Vector;
00057 
00058 import javax.swing.BoxLayout;
00059 import javax.swing.ButtonGroup;
00060 import javax.swing.JButton;
00061 import javax.swing.JFileChooser;
00062 import javax.swing.JFrame;
00063 import javax.swing.JLabel;
00064 import javax.swing.JMenu;
00065 import javax.swing.JMenuBar;
00066 import javax.swing.JMenuItem;
00067 import javax.swing.JOptionPane;
00068 import javax.swing.JPanel;
00069 import javax.swing.JRadioButton;
00070 import javax.swing.JScrollPane;
00071 import javax.swing.JTable;
00072 import javax.swing.JTextField;
00073 import javax.swing.JTextPane;
00074 import javax.swing.table.DefaultTableModel;
00075 
00076 import org.objectweb.proactive.ext.security.CertTools;
00077 import org.objectweb.proactive.ext.security.KeyTools;
00078 import org.objectweb.proactive.ext.security.SecurityConstants;
00079 
00080 
00085 public class ProActiveCertificateGeneratorGUI extends JFrame
00086     implements ActionListener {
00087     private String[] jTableColumnName = { "Entity Id", "DN" };
00088     private int KEYSTORE_APPLICATION = 1;
00089     private int KEYSTORE_ENTITY = 2;
00090     private int currentKeyStoreType;
00091     private javax.swing.JPanel jContentPane = null;
00092     private JLabel jLabel = null;
00093     private JTextPane jTextPanePublicKey = null;
00094     private JLabel jLabel1 = null;
00095     private JTextPane jTextPanePrivateKey = null;
00096     private JButton jButton = null;
00097     private KeyPair currentKeyPair = null;
00098 
00099     //   private X509Certificate currentCertificate = null;
00100     private KeyStore currentKeyStore = null;
00101     private JScrollPane jScrollPane = null;
00102     private JLabel jLabel2 = null;
00103     private JTextField jTextFieldDistinguishedName = null;
00104     private JLabel jLabel3 = null;
00105     private JTextField jTextFieldValidity = null;
00106     JFileChooser fc = new JFileChooser();
00107     private JMenuBar jJMenuBar = null;
00108     private JMenu jMenu = null;
00109     private JMenuItem jMenuItemLoadCertificate = null;
00110     private JPanel jContentPane1 = null;
00111     private JFrame jFrameSubCertificate = null; //  @jve:decl-index=0:visual-constraint="693,550"
00112     private JLabel jLabel4 = null;
00113     private JLabel jLabel5 = null;
00114     private JTextField jTextFieldSubCertificate = null;
00115     private JPanel jPanel = null;
00116     private JButton jButton1 = null;
00117     private JButton jButtonSubCertificateOk = null;
00118     private KeyStore subKeyStore = null;
00119     private JButton jButton3 = null;
00120     private JMenu jMenu1 = null;
00121     private JMenuItem jMenuItem = null;
00122     private JTable jTable = null;
00123     private JScrollPane jScrollPane1 = null;
00124     private JLabel jLabel6 = null;
00125     private JTextField jTextFieldPath = null;
00126     private JMenuItem jMenuItemSubCert = null;
00127     private JLabel jLabel7 = null;
00128     private JMenuItem jMenuSaveCert = null;
00129     private String lastOpenedDir = null;
00130     private JScrollPane jScrollPane2 = null;
00131     private JMenuItem jMenuItemAppCert = null;
00132     private JRadioButton jRadioButtonApplicationCertType = null;
00133     private JRadioButton jRadioButton1EntityCertType = null;
00134     private JLabel jLabel8 = null;
00135     private JLabel jLabel9 = null;
00136     private JLabel jLabel10 = null;
00137     private ButtonGroup bgCertificateType = new ButtonGroup();
00138 
00142     public ProActiveCertificateGeneratorGUI() {
00143         super();
00144         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
00145         addWindowListener(new WindowAdapter() {
00146                 public void windowClosing(WindowEvent e) {
00147                     System.exit(0);
00148                 }
00149             });
00150         initialize();
00151     }
00152 
00158     private void initialize() {
00159         this.setJMenuBar(getJJMenuBar());
00160         this.setSize(685, 476);
00161         this.setContentPane(getJContentPane());
00162         this.setTitle("ProActive Certificate Generator");
00163     }
00164 
00170     private javax.swing.JPanel getJContentPane() {
00171         if (jContentPane == null) {
00172             jLabel10 = new JLabel();
00173             jLabel9 = new JLabel();
00174             jLabel8 = new JLabel();
00175             jLabel7 = new JLabel();
00176             jLabel6 = new JLabel();
00177             jLabel = new JLabel();
00178             jLabel3 = new JLabel();
00179             jLabel2 = new JLabel();
00180             jLabel1 = new JLabel();
00181             jContentPane = new javax.swing.JPanel();
00182             jContentPane.setLayout(null);
00183             jLabel.setBounds(16, 125, 96, 26);
00184             jLabel.setText("Public key :");
00185             jLabel.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
00186             jLabel1.setBounds(29, 225, 81, 30);
00187             jLabel1.setText("Private key :");
00188             jLabel1.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
00189             jLabel2.setBounds(10, 357, 134, 24);
00190             jLabel2.setText("Subjet DN :");
00191             jLabel2.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
00192             jLabel3.setBounds(11, 396, 132, 21);
00193             jLabel3.setText("Validity (in days):");
00194             jLabel3.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
00195             jLabel6.setBounds(20, 9, 93, 25);
00196             jLabel6.setText("File :");
00197             jLabel6.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
00198             jLabel7.setBounds(393, 78, 249, 29);
00199             jLabel7.setText("Certificate chain :");
00200             jLabel8.setBounds(154, 43, 77, 21);
00201             jLabel8.setText("application");
00202             jLabel9.setBounds(289, 44, 83, 23);
00203             jLabel9.setText("Entity");
00204             jLabel10.setBounds(16, 44, 106, 22);
00205             jLabel10.setText("Certificate Type");
00206             jContentPane.add(jLabel, null);
00207             jContentPane.add(jLabel1, null);
00208             jContentPane.add(getJButton(), null);
00209             jContentPane.add(getJScrollPane(), null);
00210             jContentPane.add(jLabel2, null);
00211             jContentPane.add(getJTextFieldDistinguishedName(), null);
00212             jContentPane.add(jLabel3, null);
00213             jContentPane.add(getJTextFieldValidity(), null);
00214             jContentPane.add(getJButton3(), null);
00215             jContentPane.add(getJScrollPane1(), null);
00216             jContentPane.add(jLabel6, null);
00217             jContentPane.add(getJTextFieldPath(), null);
00218             jContentPane.add(jLabel7, null);
00219             jContentPane.add(getJScrollPane2(), null);
00220             jContentPane.add(getJRadioButtonApplicationCertType(), null);
00221             jContentPane.add(getJRadioButton1EntityCertType(), null);
00222             jContentPane.add(jLabel8, null);
00223             jContentPane.add(jLabel9, null);
00224             jContentPane.add(jLabel10, null);
00225             bgCertificateType.add(getJRadioButtonApplicationCertType());
00226             bgCertificateType.add(getJRadioButton1EntityCertType());
00227         }
00228         return jContentPane;
00229     }
00230 
00236     private JTextPane getJTextPanePublicKey() {
00237         if (jTextPanePublicKey == null) {
00238             jTextPanePublicKey = new JTextPane();
00239             jTextPanePublicKey.setEditable(false);
00240         }
00241         return jTextPanePublicKey;
00242     }
00243 
00249     private JTextPane getJTextPanePrivateKey() {
00250         if (jTextPanePrivateKey == null) {
00251             jTextPanePrivateKey = new JTextPane();
00252             jTextPanePrivateKey.setEditable(false);
00253         }
00254         return jTextPanePrivateKey;
00255     }
00256 
00262     private JButton getJButton() {
00263         if (jButton == null) {
00264             jButton = new JButton("Generate Key Pair");
00265             jButton.setBounds(181, 319, 171, 24);
00266             jButton.addActionListener(new java.awt.event.ActionListener() {
00267                     public void actionPerformed(java.awt.event.ActionEvent e) {
00268                         currentKeyPair = CertTools.keyPair(512);
00269                         jTextPanePrivateKey.setText(currentKeyPair.getPrivate()
00270                                                                   .toString());
00271                         jTextPanePublicKey.setText(currentKeyPair.getPublic()
00272                                                                  .toString());
00273                     }
00274                 });
00275         }
00276         return jButton;
00277     }
00278 
00284     private JScrollPane getJScrollPane() {
00285         if (jScrollPane == null) {
00286             jScrollPane = new JScrollPane();
00287             jScrollPane.setBounds(125, 121, 208, 90);
00288             jScrollPane.setViewportView(getJTextPanePublicKey());
00289         }
00290         return jScrollPane;
00291     }
00292 
00298     private JTextField getJTextFieldDistinguishedName() {
00299         if (jTextFieldDistinguishedName == null) {
00300             jTextFieldDistinguishedName = new JTextField();
00301             jTextFieldDistinguishedName.setBounds(149, 360, 321, 24);
00302         }
00303         return jTextFieldDistinguishedName;
00304     }
00305 
00311     private JTextField getJTextFieldValidity() {
00312         if (jTextFieldValidity == null) {
00313             jTextFieldValidity = new JTextField();
00314             jTextFieldValidity.setBounds(248, 385, 67, 24);
00315             jTextFieldValidity.setText("360");
00316         }
00317         return jTextFieldValidity;
00318     }
00319 
00320     public void actionPerformed(java.awt.event.ActionEvent ev) {
00321         if (ev.getSource() == jMenuSaveCert) {
00322             if (currentKeyPair == null) {
00323                 JOptionPane.showMessageDialog(this,
00324                     "No RSA key !\n you have to generate a key pair prior to saving the certificate into a file",
00325                     "Certificate Warning", JOptionPane.WARNING_MESSAGE);
00326                 return;
00327             }
00328 
00329             try {
00330                 CertificateFactory cf = CertificateFactory.getInstance("X.509",
00331                         "BC");
00332 
00333                 String dnEntry = jTextFieldDistinguishedName.getText();
00334 
00335                 X509Certificate certificate = CertTools.genSelfCert(dnEntry,
00336                         Integer.parseInt(getJTextFieldValidity().getText()) * 3600 * 24,
00337                         null, currentKeyPair.getPrivate(),
00338                         currentKeyPair.getPublic(), true);
00339 
00340                 System.out.println("ddd" + dnEntry);
00341 
00342                 currentKeyStore = KeyTools.createP12(SecurityConstants.KEYSTORE_ENTITY_PATH,
00343                         currentKeyPair.getPrivate(), certificate,
00344                         (Certificate[]) null);
00345 
00346                 JFileChooser fc = new JFileChooser(lastOpenedDir);
00347 
00348                 //fc.addActionListener(this);
00349                 //Handle open button action.
00350                 int returnVal = fc.showOpenDialog(ProActiveCertificateGeneratorGUI.this);
00351 
00352                 if (returnVal == JFileChooser.APPROVE_OPTION) {
00353                     File file = fc.getSelectedFile();
00354                     lastOpenedDir = file.getCanonicalPath();
00355                     //This is where a real application would open the file.
00356                     currentKeyStore.store(new FileOutputStream(file),
00357                         "ha".toCharArray());
00358                     System.out.println("writing: " + file.getName() + ".");
00359                 } else {
00360                     System.out.println("Open command cancelled by user.");
00361                 }
00362             } catch (CertificateException e1) {
00363                 e1.printStackTrace();
00364             } catch (NoSuchProviderException e1) {
00365                 e1.printStackTrace();
00366             } catch (InvalidKeyException e) {
00367                 e.printStackTrace();
00368             } catch (NumberFormatException e) {
00369                 e.printStackTrace();
00370             } catch (NoSuchAlgorithmException e) {
00371                 e.printStackTrace();
00372             } catch (SignatureException e) {
00373                 e.printStackTrace();
00374             } catch (KeyStoreException e) {
00375                 e.printStackTrace();
00376             } catch (FileNotFoundException e) {
00377                 e.printStackTrace();
00378             } catch (IOException e) {
00379                 e.printStackTrace();
00380             } catch (Exception e) {
00381                 e.printStackTrace();
00382             }
00383         }
00384         // loading a certificate
00385         else if (ev.getSource() == jMenuItemLoadCertificate) {
00386             File f = null;
00387 
00388             JFileChooser fc = new JFileChooser(lastOpenedDir);
00389 
00390             //fc.addActionListener(this);
00391             //Handle open button action.
00392             int returnVal = fc.showOpenDialog(ProActiveCertificateGeneratorGUI.this);
00393 
00394             if (returnVal == JFileChooser.APPROVE_OPTION) {
00395                 File file = fc.getSelectedFile();
00396                 try {
00397                     lastOpenedDir = file.getCanonicalPath();
00398                 } catch (IOException e) {
00399                     e.printStackTrace();
00400                 }
00401                 loadCertificateFile(file);
00402             } else {
00403                 System.out.println("Open command cancelled by user.");
00404             }
00405         }
00406         // creating a sub certificate
00407         else if (ev.getSource() == jMenuItemSubCert) {
00408             if (currentKeyStore == null) {
00409                 JOptionPane.showMessageDialog(this,
00410                     "No current certificate !\n you have to load or create one before generating an sub certificate",
00411                     "Certificate Warning", JOptionPane.WARNING_MESSAGE);
00412             } else {
00413                 getJTextFieldSubCertificate().setText(jTextFieldDistinguishedName.getText());
00414                 getJFrameSubCertificate().setVisible(true);
00415             }
00416         } else if (ev.getSource() == jButtonSubCertificateOk) {
00417             try {
00418                 CertificateFactory cf = CertificateFactory.getInstance("X.509",
00419                         "BC");
00420 
00421                 KeyPair subKeyPair = CertTools.keyPair(512);
00422 
00423                 X509Certificate subCertificate = CertTools.genCert(jTextFieldSubCertificate.getText(),
00424                         Long.parseLong(getJTextFieldValidity().getText()) * 3600 * 24,
00425                         null, subKeyPair.getPrivate(), subKeyPair.getPublic(),
00426                         true,
00427                         ((X509Certificate) currentKeyStore.getCertificate(
00428                             SecurityConstants.KEYSTORE_ENTITY_PATH)).getSubjectDN()
00429                          .toString(), currentKeyPair.getPrivate(),
00430                         ((X509Certificate) currentKeyStore.getCertificate(
00431                             SecurityConstants.KEYSTORE_ENTITY_PATH)).getPublicKey());
00432 
00433                 System.out.println("->" + jTextFieldSubCertificate.getText());
00434 
00435                 subKeyStore = KeyStore.getInstance("PKCS12", "BC");
00436 
00437                 subKeyStore = KeyTools.createP12(SecurityConstants.KEYSTORE_ENTITY_PATH,
00438                         subKeyPair.getPrivate(), subCertificate,
00439                         currentKeyStore.getCertificateChain(
00440                             SecurityConstants.KEYSTORE_ENTITY_PATH));
00441 
00442                 JFileChooser fc = new JFileChooser(lastOpenedDir);
00443 
00444                 //fc.addActionListener(this);
00445                 //Handle open button action.
00446                 int returnVal = fc.showOpenDialog(ProActiveCertificateGeneratorGUI.this);
00447 
00448                 if (returnVal == JFileChooser.APPROVE_OPTION) {
00449                     File file = fc.getSelectedFile();
00450                     lastOpenedDir = file.getCanonicalPath();
00451                     //This is where a real application would open the file.
00452                     subKeyStore.store(new FileOutputStream(file),
00453                         "ha".toCharArray());
00454                     System.out.println("writing: " + file.getName() + ".");
00455                 } else {
00456                     System.out.println("Open command cancelled by user.");
00457                 }
00458             } catch (CertificateException e1) {
00459                 e1.printStackTrace();
00460             } catch (NoSuchProviderException e1) {
00461                 e1.printStackTrace();
00462             } catch (InvalidKeyException e) {
00463                 e.printStackTrace();
00464             } catch (NumberFormatException e) {
00465                 e.printStackTrace();
00466             } catch (NoSuchAlgorithmException e) {
00467                 e.printStackTrace();
00468             } catch (SignatureException e) {
00469                 e.printStackTrace();
00470             } catch (Exception e) {
00471                 e.printStackTrace();
00472             }
00473         }
00474         // creating an application certificate
00475         else if (ev.getSource() == jMenuItemAppCert) {
00476             if (currentKeyStore == null) {
00477                 JOptionPane.showMessageDialog(this,
00478                     "No current certificate !\n you have to load or create one before generating an application certificate",
00479                     "Certificate Warning", JOptionPane.WARNING_MESSAGE);
00480             } else {
00481                 JOptionPane.showMessageDialog(this,
00482                     "No current certificate !\n you have to load or create one before generating an application certificate",
00483                     "Certificate Warning", JOptionPane.WARNING_MESSAGE);
00484 
00485                 try {
00486                     String reponse;
00487                     String message = "Enter a name for the application certificate \nIt could be the name of the application";
00488                     reponse = JOptionPane.showInputDialog(this, message);
00489 
00490                     CertificateFactory cf = CertificateFactory.getInstance("X.509",
00491                             "BC");
00492 
00493                     KeyPair subKeyPair = CertTools.keyPair(512);
00494 
00495                     X509Certificate subCertificate = CertTools.genCert(reponse,
00496                             Long.parseLong(getJTextFieldValidity().getText()) * 360,
00497                             null, subKeyPair.getPrivate(),
00498                             subKeyPair.getPublic(), true,
00499                             ((X509Certificate) currentKeyStore.getCertificate(
00500                                 SecurityConstants.KEYSTORE_ENTITY_PATH)).getSubjectDN()
00501                              .toString(), currentKeyPair.getPrivate(),
00502                             ((X509Certificate) currentKeyStore.getCertificate(
00503                                 SecurityConstants.KEYSTORE_ENTITY_PATH)).getPublicKey());
00504 
00505                     System.out.println("->" + reponse);
00506 
00507                     subKeyStore = KeyStore.getInstance("PKCS12", "BC");
00508 
00509                     subKeyStore = KeyTools.createP12(SecurityConstants.KEYSTORE_APPLICATION_PATH,
00510                             subKeyPair.getPrivate(), subCertificate,
00511                             currentKeyStore.getCertificateChain(
00512                                 SecurityConstants.KEYSTORE_ENTITY_PATH));
00513 
00514                     JFileChooser fc = new JFileChooser(lastOpenedDir);
00515 
00516                     //fc.addActionListener(this);
00517                     //Handle open button action.
00518                     int returnVal = fc.showOpenDialog(ProActiveCertificateGeneratorGUI.this);
00519 
00520                     if (returnVal == JFileChooser.APPROVE_OPTION) {
00521                         File file = fc.getSelectedFile();
00522                         lastOpenedDir = file.getCanonicalPath();
00523                         //This is where a real application would open the file.
00524                         subKeyStore.store(new FileOutputStream(file),
00525                             "ha".toCharArray());
00526                         System.out.println("writing: " + file.getName() + ".");
00527                     } else {
00528                         System.out.println("Open command cancelled by user.");
00529                     }
00530                 } catch (CertificateException e1) {
00531                     e1.printStackTrace();
00532                 } catch (NoSuchProviderException e1) {
00533                     e1.printStackTrace();
00534                 } catch (InvalidKeyException e) {
00535                     e.printStackTrace();
00536                 } catch (NumberFormatException e) {
00537                     e.printStackTrace();
00538                 } catch (NoSuchAlgorithmException e) {
00539                     e.printStackTrace();
00540                 } catch (SignatureException e) {
00541                     e.printStackTrace();
00542                 } catch (Exception e) {
00543                     e.printStackTrace();
00544                 }
00545             }
00546         } else if (ev.getSource() == jButton3) {
00547             try {
00548                 KeyTools.getCertChain(currentKeyStore,
00549                     SecurityConstants.KEYSTORE_ENTITY_PATH);
00550             } catch (KeyStoreException e) {
00551                 e.printStackTrace();
00552             }
00553         }
00554     }
00555 
00561     private JMenuBar getJJMenuBar() {
00562         if (jJMenuBar == null) {
00563             jJMenuBar = new JMenuBar();
00564             jJMenuBar.add(getJMenu());
00565             jJMenuBar.add(getJMenu1());
00566         }
00567         return jJMenuBar;
00568     }
00569 
00575     private JMenu getJMenu() {
00576         if (jMenu == null) {
00577             jMenu = new JMenu();
00578             jMenu.setName("File");
00579             jMenu.setText("File");
00580             jMenu.add(getJMenuItem());
00581         }
00582         return jMenu;
00583     }
00584 
00590     private JMenuItem getJMenuItemLoadCertificate() {
00591         if (jMenuItemLoadCertificate == null) {
00592             jMenuItemLoadCertificate = new JMenuItem();
00593             jMenuItemLoadCertificate.setText("Load Certificate");
00594             jMenuItemLoadCertificate.addActionListener(this);
00595         }
00596         return jMenuItemLoadCertificate;
00597     }
00598 
00604     private JPanel getJContentPane1() {
00605         if (jContentPane1 == null) {
00606             jContentPane1 = new JPanel();
00607             jLabel4 = new JLabel("sub Certificate Generator");
00608             jLabel5 = new JLabel("Sub certificate DN");
00609             jContentPane1.setLayout(new BorderLayout());
00610             jLabel4.setText("JLabel");
00611             jLabel5.setText("JLabel");
00612             jContentPane1.add(jLabel4, java.awt.BorderLayout.NORTH);
00613             jContentPane1.add(jLabel5, java.awt.BorderLayout.WEST);
00614             jContentPane1.add(getJTextFieldSubCertificate(),
00615                 java.awt.BorderLayout.CENTER);
00616             jContentPane1.add(getJPanel(), java.awt.BorderLayout.SOUTH);
00617         }
00618         return jContentPane1;
00619     }
00620 
00626     private JFrame getJFrameSubCertificate() {
00627         if (jFrameSubCertificate == null) {
00628             jFrameSubCertificate = new JFrame();
00629             jFrameSubCertificate.setContentPane(getJContentPane1());
00630             jFrameSubCertificate.setSize(379, 150);
00631             jFrameSubCertificate.setTitle("jFrameSubCertificate");
00632         }
00633         return jFrameSubCertificate;
00634     }
00635 
00641     private JTextField getJTextFieldSubCertificate() {
00642         if (jTextFieldSubCertificate == null) {
00643             jTextFieldSubCertificate = new JTextField();
00644         }
00645         return jTextFieldSubCertificate;
00646     }
00647 
00653     private JPanel getJPanel() {
00654         if (jPanel == null) {
00655             jPanel = new JPanel();
00656             jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.X_AXIS));
00657             jPanel.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
00658             jPanel.add(getJButton1(), null);
00659             jPanel.add(getJButtonSubCertificateOk(), null);
00660         }
00661         return jPanel;
00662     }
00663 
00669     private JButton getJButton1() {
00670         if (jButton1 == null) {
00671             jButton1 = new JButton();
00672             jButton1.setText("Cancel");
00673         }
00674         return jButton1;
00675     }
00676 
00682     private JButton getJButtonSubCertificateOk() {
00683         if (jButtonSubCertificateOk == null) {
00684             jButtonSubCertificateOk = new JButton();
00685             jButtonSubCertificateOk.addActionListener(this);
00686             jButtonSubCertificateOk.setText("Ok");
00687         }
00688         return jButtonSubCertificateOk;
00689     }
00690 
00696     private JButton getJButton3() {
00697         if (jButton3 == null) {
00698             jButton3 = new JButton("");
00699             jButton3.addActionListener(this);
00700             jButton3.setBounds(514, 391, 141, 25);
00701             jButton3.setText("Display as text");
00702         }
00703         return jButton3;
00704     }
00705 
00711     private JMenu getJMenu1() {
00712         if (jMenu1 == null) {
00713             jMenu1 = new JMenu();
00714             jMenu1.setText("Certificate");
00715             jMenu1.add(getJMenuItemLoadCertificate());
00716             jMenu1.add(getJMenuItemSubCert());
00717             jMenu1.add(getJMenuItemAppCert());
00718             jMenu1.add(getJMenuSaveCert());
00719         }
00720         return jMenu1;
00721     }
00722 
00728     private JMenuItem getJMenuItem() {
00729         if (jMenuItem == null) {
00730             jMenuItem = new JMenuItem();
00731             jMenuItem.setText("Quit");
00732             jMenuItem.addActionListener(new java.awt.event.ActionListener() {
00733                     public void actionPerformed(java.awt.event.ActionEvent e) {
00734                         System.exit(0);
00735                     }
00736                 });
00737         }
00738         return jMenuItem;
00739     }
00740 
00746     private JTable getJTable() {
00747         if (jTable == null) {
00748             jTable = new JTable(new DefaultTableModel(jTableColumnName, 0));
00749         }
00750         return jTable;
00751     }
00752 
00758     private JScrollPane getJScrollPane1() {
00759         if (jScrollPane1 == null) {
00760             jScrollPane1 = new JScrollPane();
00761             jScrollPane1.setBounds(392, 112, 249, 183);
00762             jScrollPane1.setViewportView(getJTable());
00763         }
00764         return jScrollPane1;
00765     }
00766 
00772     private JTextField getJTextFieldPath() {
00773         if (jTextFieldPath == null) {
00774             jTextFieldPath = new JTextField();
00775             jTextFieldPath.setBounds(119, 8, 520, 25);
00776             jTextFieldPath.setEditable(false);
00777         }
00778         return jTextFieldPath;
00779     }
00780 
00786     private JMenuItem getJMenuItemSubCert() {
00787         if (jMenuItemSubCert == null) {
00788             jMenuItemSubCert = new JMenuItem();
00789             jMenuItemSubCert.setText("create sub certificate");
00790             jMenuItemSubCert.setToolTipText(
00791                 "create a certificate signed by current certificate");
00792             jMenuItemSubCert.addActionListener(this);
00793         }
00794         return jMenuItemSubCert;
00795     }
00796 
00802     private JMenuItem getJMenuSaveCert() {
00803         if (jMenuSaveCert == null) {
00804             jMenuSaveCert = new JMenuItem();
00805             jMenuSaveCert.setText("Save");
00806             jMenuSaveCert.setToolTipText(
00807                 "Save current certificate into a pkcs12 file");
00808             jMenuSaveCert.addActionListener(this);
00809         }
00810         return jMenuSaveCert;
00811     }
00812 
00813     protected void loadCertificateFile(File file) {
00814         try {
00815             currentKeyStore = KeyStore.getInstance("PKCS12", "BC");
00816             Certificate[] chain = null;
00817             X509Certificate certificate = null;
00818 
00819             jTextFieldPath.setText(file.getAbsoluteFile().toURI().toString());
00820 
00821             //This is where a real application would open the file.
00822             System.out.println("Opening: " + file.getName() + ".");
00823             currentKeyStore.load(new FileInputStream(file), "ha".toCharArray());
00824 
00825             Enumeration e = currentKeyStore.aliases();
00826 
00827             System.out.println("--------------");
00828             for (; e.hasMoreElements();) {
00829                 System.out.println(e.nextElement());
00830             }
00831             System.out.println("--------------");
00832 
00833             // display certificate type 
00834             if (currentKeyStore.getCertificate(
00835                         SecurityConstants.KEYSTORE_APPLICATION_PATH) != null) {
00836                 getJRadioButtonApplicationCertType().setSelected(true);
00837                 System.out.println("cert type " + KEYSTORE_APPLICATION);
00838                 currentKeyStoreType = KEYSTORE_APPLICATION;
00839                 chain = currentKeyStore.getCertificateChain(SecurityConstants.KEYSTORE_APPLICATION_PATH);
00840                 certificate = ((X509Certificate) currentKeyStore.getCertificate(SecurityConstants.KEYSTORE_APPLICATION_PATH));
00841                 currentKeyPair = new KeyPair(certificate.getPublicKey(),
00842                         (PrivateKey) currentKeyStore.getKey(
00843                             SecurityConstants.KEYSTORE_APPLICATION_PATH, null));
00844             } else {
00845                 getJRadioButton1EntityCertType().setSelected(true);
00846                 System.out.println("cert type " + KEYSTORE_ENTITY);
00847                 currentKeyStoreType = KEYSTORE_ENTITY;
00848                 chain = currentKeyStore.getCertificateChain(SecurityConstants.KEYSTORE_ENTITY_PATH);
00849                 certificate = ((X509Certificate) currentKeyStore.getCertificate(SecurityConstants.KEYSTORE_ENTITY_PATH));
00850 
00851                 currentKeyPair = new KeyPair(certificate.getPublicKey(),
00852                         (PrivateKey) currentKeyStore.getKey(
00853                             SecurityConstants.KEYSTORE_ENTITY_PATH, null));
00854             }
00855 
00856             System.out.println(currentKeyStore + " chain length : " +
00857                 chain.length + " ++" + certificate.getSubjectDN());
00858 
00859             jTextFieldDistinguishedName.setText(certificate.getSubjectDN()
00860                                                            .toString());
00861             jTextPanePrivateKey.setText(currentKeyPair.getPrivate().toString());
00862             jTextPanePublicKey.setText(currentKeyPair.getPublic().toString());
00863 
00864             DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
00865 
00866             // empty jtable 
00867             for (int i = 0; i < tableModel.getRowCount(); i++) {
00868                 tableModel.removeRow(i);
00869             }
00870             for (int i = 1; i < chain.length; i++) {
00871                 Vector<String> v = new Vector<String>();
00872                 v.addElement(new Integer(i).toString());
00873                 v.addElement(((X509Certificate) chain[i]).getSubjectDN()
00874                               .toString());
00875                 tableModel.addRow(v);
00876             }
00877         } catch (KeyStoreException e) {
00878             e.printStackTrace();
00879         } catch (NoSuchProviderException e) {
00880             e.printStackTrace();
00881         } catch (NoSuchAlgorithmException e) {
00882             e.printStackTrace();
00883         } catch (CertificateException e) {
00884             e.printStackTrace();
00885         } catch (FileNotFoundException e) {
00886             e.printStackTrace();
00887         } catch (IOException e) {
00888             e.printStackTrace();
00889         } catch (UnrecoverableKeyException e) {
00890             e.printStackTrace();
00891         }
00892     }
00893 
00899     private JScrollPane getJScrollPane2() {
00900         if (jScrollPane2 == null) {
00901             jScrollPane2 = new JScrollPane();
00902             jScrollPane2.setBounds(127, 219, 207, 94);
00903             jScrollPane2.setViewportView(getJTextPanePrivateKey());
00904         }
00905         return jScrollPane2;
00906     }
00907 
00913     private JMenuItem getJMenuItemAppCert() {
00914         if (jMenuItemAppCert == null) {
00915             jMenuItemAppCert = new JMenuItem();
00916             jMenuItemAppCert.setToolTipText("Create an application certificate");
00917             jMenuItemAppCert.setText("Create an app cert");
00918             jMenuItemAppCert.addActionListener(this);
00919         }
00920         return jMenuItemAppCert;
00921     }
00922 
00928     private JRadioButton getJRadioButtonApplicationCertType() {
00929         if (jRadioButtonApplicationCertType == null) {
00930             jRadioButtonApplicationCertType = new JRadioButton();
00931             jRadioButtonApplicationCertType.setBounds(128, 44, 21, 21);
00932             jRadioButtonApplicationCertType.setEnabled(true);
00933         }
00934         return jRadioButtonApplicationCertType;
00935     }
00936 
00942     private JRadioButton getJRadioButton1EntityCertType() {
00943         if (jRadioButton1EntityCertType == null) {
00944             jRadioButton1EntityCertType = new JRadioButton();
00945 
00946             jRadioButton1EntityCertType.setBounds(255, 44, 21, 21);
00947             jRadioButton1EntityCertType.setEnabled(true);
00948         }
00949         return jRadioButton1EntityCertType;
00950     }
00951 
00952     public static void main(String[] args) {
00953     }
00954 } //  @jve:decl-index=0:visual-constraint="81,117"

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1