/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package View;
import Crypto.Certificat;
import Crypto.RSA;
import java.io.File;
import java.security.KeyPair;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import sun.security.x509.X509CertImpl;
/**
*
* @author tiph
*/
public class OptionCertificatClePrivee extends javax.swing.JPanel implements OptionIterface {
/**
* Creates new form OptionCertificatClePrivee
*/
public OptionCertificatClePrivee() {
initComponents();
tb_nom.setText(Config.Config.getInstance().getNomProf());
tb_prenom.setText(Config.Config.getInstance().getPrenomProf());
tb_email.setText(Config.Config.getInstance().getEmailProf());
tb_certificatpath.setText(Config.Config.getInstance().getPathCertificat());
tb_privatekeypath.setText(Config.Config.getInstance().getPathPrivateKey());
l_error.setVisible(false);
}
/**
* This method is called from within the constructor to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
bt_import_certificat = new javax.swing.JButton();
jSeparator1 = new javax.swing.JSeparator();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
bt_import_cleprivee = new javax.swing.JButton();
tb_certificatpath = new javax.swing.JTextField();
tb_privatekeypath = new javax.swing.JTextField();
l_error = new javax.swing.JLabel();
tb_email = new javax.swing.JTextField();
tb_nom = new javax.swing.JTextField();
tb_prenom = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jSeparator2 = new javax.swing.JSeparator();
bt_generateboth = new javax.swing.JButton();
bt_import_certificat.setText("Importer");
bt_import_certificat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_import_certificatActionPerformed(evt);
}
});
jLabel1.setText("Certificat :");
jLabel2.setText("Clé privée :");
bt_import_cleprivee.setText("Importer");
bt_import_cleprivee.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_import_clepriveeActionPerformed(evt);
}
});
tb_certificatpath.setEditable(false);
tb_certificatpath.setText("jTextField1");
tb_privatekeypath.setEditable(false);
tb_privatekeypath.setText("jTextField1");
l_error.setForeground(new java.awt.Color(255, 0, 0));
l_error.setText("jLabel3");
tb_email.setText("jTextField1");
tb_nom.setText("jTextField1");
tb_prenom.setText("jTextField1");
jLabel3.setText("Prénom :");
jLabel4.setText("E-mail :");
jLabel5.setText("Nom :");
bt_generateboth.setText("Générer");
bt_generateboth.setToolTipText("Genere un couple certificat/clé privée");
bt_generateboth.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_generatebothActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(l_error, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(tb_nom, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tb_prenom, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(tb_email)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tb_certificatpath, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jSeparator1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_import_certificat))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_import_cleprivee))
.addComponent(tb_privatekeypath, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)))
.addGap(16, 16, 16)
.addComponent(bt_generateboth, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(tb_nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(tb_prenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(tb_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(bt_import_certificat))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tb_certificatpath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(bt_import_cleprivee))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tb_privatekeypath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(bt_generateboth, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(l_error, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(57, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void bt_import_certificatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_import_certificatActionPerformed
String path = doImport();
if (path != null) {
tb_certificatpath.setText(path);
}
}//GEN-LAST:event_bt_import_certificatActionPerformed
private void bt_import_clepriveeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_import_clepriveeActionPerformed
String path = doImport();
if (path != null) {
tb_privatekeypath.setText(path);
}
}//GEN-LAST:event_bt_import_clepriveeActionPerformed
private void bt_generatebothActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_generatebothActionPerformed
if (tb_nom.getText().isEmpty() || tb_prenom.getText().isEmpty()) {
l_error.setText("Veuillez remplir vos informations au dessus ! ");
l_error.setVisible(true);
} else {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Selection du dossier d'export de la clé privée et du certificat");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showDialog(this, "Selectionner") == JFileChooser.APPROVE_OPTION) {
tb_certificatpath.setText(fc.getSelectedFile().getAbsolutePath() + File.separator + "certificat.cert");
tb_privatekeypath.setText(fc.getSelectedFile().getAbsolutePath() + File.separator + "private.key");
KeyPair kp = RSA.generateKeyPair();
RSA.saveKeyToFile(fc.getSelectedFile().getAbsolutePath() + File.separator + "private.key", kp.getPrivate());
X509CertImpl c = Certificat.generateX509Certificate(kp.getPublic(),
tb_nom.getText() + " " + tb_prenom.getText(),
kp.getPrivate(),
tb_nom.getText() + " " + tb_prenom.getText());
Certificat.saveCertToFile(fc.getSelectedFile().getAbsolutePath() + File.separator + "certificat.cert", c);
l_error.setVisible(false);
}
}
}//GEN-LAST:event_bt_generatebothActionPerformed
private String doImport() {
JFileChooser fileChooser = new JFileChooser();
String path = null;
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
// On recupere le fichier et on l'ouvre dans un nouveau tab
path = fileChooser.getSelectedFile().getAbsolutePath();
}
return path;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bt_generateboth;
private javax.swing.JButton bt_import_certificat;
private javax.swing.JButton bt_import_cleprivee;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JLabel l_error;
private javax.swing.JTextField tb_certificatpath;
private javax.swing.JTextField tb_email;
private javax.swing.JTextField tb_nom;
private javax.swing.JTextField tb_prenom;
private javax.swing.JTextField tb_privatekeypath;
// End of variables declaration//GEN-END:variables
@Override
public String getOptionName() {
return "Gestion clés";
}
@Override
public Icon getOptionIcon() {
return null;
}
@Override
public void saveParams() {
Config.Config.getInstance().setNomProf(tb_nom.getText());
Config.Config.getInstance().setPrenomProf(tb_prenom.getText());
Config.Config.getInstance().setEmailProf(tb_email.getText());
Config.Config.getInstance().setPathCertificat(tb_certificatpath.getText());
Config.Config.getInstance().setPathPrivateKey(tb_privatekeypath.getText());
}
}