package org.eparapher.rcp.wizards; import java.io.File; import org.apache.log4j.Logger; import org.eclipse.jface.wizard.IWizardPage; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.PlatformUI; import org.eparapher.core.crypto.EPKeystoreManager; import org.eparapher.core.tools.JVMSettings; import org.eparapher.rcp.tools.GUIIcons; import org.eparapher.rcp.tools.eParapherTools; public class KeysAndFormatWizardPage extends WizardPage implements Listener { private static Logger log = Logger.getLogger(KeysAndFormatWizardPage.class); private Button exportOnlyCertificates; private Button exportPKAndCertificates; private Button keystoreFileSelection; private Button exportedPKPassword; private Combo keystoreFormat; private Text exportedKeystoreFile; private boolean canFlip; private String[] keystoreFileExt; protected KeysAndFormatWizardPage() { super("Keypair/Certificate export ..."); setTitle("User certificate export wizard"); setDescription("Please select your keypair and/or certificate export settings."); setImageDescriptor(GUIIcons.WIZARD_NEW_KEYCERT); } public void createControl(Composite parent) { Composite container = new Composite(parent, SWT.NONE); // create the desired layout for this wizard page GridLayout gl = new GridLayout(); gl.numColumns = 3; gl.makeColumnsEqualWidth = false; container.setLayout(gl); container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); GridData gd = new GridData(GridData.FILL_HORIZONTAL); gd.horizontalSpan = gl.numColumns; //Self Signed certificate or generate a CSR Label label = new Label (container, SWT.NONE); label.setText("Export :"); label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); exportOnlyCertificates = new Button(container, SWT.RADIO); exportOnlyCertificates.setText("Certificates (Base 64 PEM format)"); gd = new GridData(GridData.FILL_HORIZONTAL); gd.horizontalSpan = gl.numColumns - 1; exportOnlyCertificates.setLayoutData(gd); exportOnlyCertificates.setSelection(true); exportOnlyCertificates.addListener(SWT.Selection, this); Label emptylabel = new Label (container, SWT.NONE); exportPKAndCertificates = new Button(container, SWT.RADIO); exportPKAndCertificates.setText("Keys && certificates (Keystore)"); gd = new GridData(GridData.FILL_HORIZONTAL); gd.horizontalSpan = gl.numColumns - 1; exportPKAndCertificates.setLayoutData(gd); exportPKAndCertificates.setSelection(false); exportPKAndCertificates.addListener(SWT.Selection, this); eParapherTools.createGUILine(container, gl.numColumns); Label labelKeystoreFormat = new Label(container, SWT.NULL); labelKeystoreFormat.setText("Keystore Type : "); labelKeystoreFormat.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); keystoreFormat = new Combo(container, SWT.READ_ONLY |SWT.BORDER); keystoreFormat.setItems(EPKeystoreManager.KSFILEFORMAT); keystoreFormat.select(0); gd = new GridData(GridData.FILL_HORIZONTAL); gd.horizontalSpan = gl.numColumns - 1; keystoreFormat.setLayoutData(gd); keystoreFormat.addListener(SWT.Selection, this); keystoreFormat.setEnabled(false); Label labelKeystoreFile = new Label(container, SWT.NULL); labelKeystoreFile.setText("Keystore File : "); labelKeystoreFile.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); exportedKeystoreFile = new Text(container,SWT.BORDER); exportedKeystoreFile.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); exportedKeystoreFile.setText(JVMSettings.getUserHome()); keystoreFileSelection = new Button(container, SWT.PUSH); keystoreFileSelection.setText("Select"); keystoreFileSelection.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); keystoreFileSelection.addListener(SWT.Selection, this); Label labelPKPassword = new Label(container, SWT.NULL); labelPKPassword.setText("Reuse PK passwords : "); labelPKPassword.setToolTipText("Carefull : this low down the security level."); labelPKPassword.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); exportedPKPassword = new Button(container, SWT.CHECK ); exportedPKPassword.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); exportedPKPassword.setSelection(true); exportedPKPassword.setEnabled(false); setKeystoreFilter(); ((ExportKeysCertificatesWizard)getWizard()).newSecret.setPageComplete(true); setPageComplete(isPageComplete()); setControl(container); } /** * @see Listener#handleEvent(Event) */ public void handleEvent(Event event) { if ((event.widget == exportOnlyCertificates)) { if (exportOnlyCertificates.getSelection()) { keystoreFormat.setEnabled(false); exportedPKPassword.setEnabled(false); ((ExportKeysCertificatesWizard)getWizard()).newSecret.setPageComplete(true); log.debug("Certificate only export"); } } if ((event.widget == exportPKAndCertificates)) { if (exportPKAndCertificates.getSelection()) { keystoreFormat.setEnabled(true); exportedPKPassword.setEnabled(true); ((ExportKeysCertificatesWizard)getWizard()).newSecret.setPageComplete(false); log.debug(" Certificate and private key export"); } } if ((event.widget == keystoreFileSelection)) { selectExportFileOrDirectory(); log.debug("Choose an exported File : Launch"); } if ((event.widget == keystoreFormat)) setKeystoreFilter(); setPageComplete(isPageComplete()); } private void setKeystoreFilter() { //log.debug("Keystore type change : " + keystoreFormat.getText()); if ( keystoreFormat.getText().equals("JKS") ) keystoreFileExt = new String[] { "*.jks", "*." }; if ( keystoreFormat.getText().equals("JCEKS") ) keystoreFileExt = new String[] { "*.jceks", "*.jks", "*." }; if ( keystoreFormat.getText().equals("BKS") ) keystoreFileExt = new String[] { "*.bks", "*." }; if ( keystoreFormat.getText().equals("PKCS12") ) keystoreFileExt = new String[] { "*.p12", "*.pkcs12", "*." }; } public boolean isPageComplete() { this.canFlip=false; File exportfileordir = new File(exportedKeystoreFile.getText()); if (exportOnlyCertificates.getSelection()) if (!exportfileordir.isDirectory()) return false; if (exportPKAndCertificates.getSelection()) { if (exportfileordir.isDirectory()) return false; else this.canFlip=true; } //log.debug("page complete"); return true; } /* * Returns the next page. * Saves the values from this page in the model associated * with the wizard. Initializes the widgets on the next page. */ public IWizardPage getNextPage() { if (exportPKAndCertificates.getSelection()) { PinOrPassphraseWizardPage newPPhrase = ((ExportKeysCertificatesWizard)getWizard()).newSecret; return newPPhrase; } return null; } public boolean canFlipToNextPage() { return this.canFlip; } private void selectExportFileOrDirectory() { String value=""; if (exportOnlyCertificates.getSelection()) { DirectoryDialog fd = new DirectoryDialog(getShell(), SWT.OPEN); fd.setText("Directory to export X509 Certificate"); fd.setFilterPath(JVMSettings.getUserHome()); value = fd.open(); } if (exportPKAndCertificates.getSelection()) { FileDialog fd = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE); fd.setText("New Keystore"); fd.setFilterPath(JVMSettings.getUserHome()); fd.setFilterExtensions(keystoreFileExt); value = fd.open(); } exportedKeystoreFile.setText(value); } public String getKeystoreType() { return EPKeystoreManager.KSFILEFORMAT[keystoreFormat.getSelectionIndex()]; } public String getKeystoreFile() { return exportedKeystoreFile.getText(); } public boolean isPrivateKeyExported() { if (exportOnlyCertificates.getSelection()) return false; if (exportPKAndCertificates.getSelection()) return true; return false; } public boolean isPrivateKeyPassword() { return exportedPKPassword.getSelection(); } }