View Javadoc

1   package org.eparapher.rcp.wizards;
2   
3   import java.io.File;
4   
5   import org.apache.log4j.Logger;
6   import org.eclipse.jface.wizard.IWizardPage;
7   import org.eclipse.jface.wizard.WizardPage;
8   import org.eclipse.swt.SWT;
9   import org.eclipse.swt.layout.GridData;
10  import org.eclipse.swt.layout.GridLayout;
11  import org.eclipse.swt.widgets.Button;
12  import org.eclipse.swt.widgets.Combo;
13  import org.eclipse.swt.widgets.Composite;
14  import org.eclipse.swt.widgets.DirectoryDialog;
15  import org.eclipse.swt.widgets.Event;
16  import org.eclipse.swt.widgets.FileDialog;
17  import org.eclipse.swt.widgets.Label;
18  import org.eclipse.swt.widgets.Listener;
19  import org.eclipse.swt.widgets.Text;
20  import org.eclipse.ui.PlatformUI;
21  import org.eparapher.core.crypto.EPKeystoreManager;
22  import org.eparapher.core.tools.JVMSettings;
23  import org.eparapher.rcp.tools.GUIIcons;
24  import org.eparapher.rcp.tools.eParapherTools;
25  
26  
27  public class KeysAndFormatWizardPage extends WizardPage implements Listener {
28  	
29  	private static Logger log = Logger.getLogger(KeysAndFormatWizardPage.class);
30  	
31  	private Button exportOnlyCertificates;
32  	private Button exportPKAndCertificates;
33  	private Button keystoreFileSelection;
34  
35  	private Button exportedPKPassword;
36  	
37  	private Combo  keystoreFormat;
38  	
39  	private Text exportedKeystoreFile;
40  	
41  	private boolean canFlip;
42  	private String[]  keystoreFileExt;
43  	
44  	protected KeysAndFormatWizardPage() {
45  		super("Keypair/Certificate export ...");
46  		setTitle("User certificate export wizard");
47  		setDescription("Please select your keypair and/or certificate export settings.");
48  		setImageDescriptor(GUIIcons.WIZARD_NEW_KEYCERT);
49  	}
50  
51  	public void createControl(Composite parent) {
52  		Composite container = new Composite(parent, SWT.NONE);
53  
54  	    // create the desired layout for this wizard page
55  		GridLayout gl = new GridLayout();
56  		gl.numColumns = 3;
57  		gl.makeColumnsEqualWidth  = false;
58  		container.setLayout(gl);
59  		container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
60  
61  		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
62  		gd.horizontalSpan = gl.numColumns;
63  
64  		//Self Signed certificate or generate a CSR
65  		Label label = new Label (container, SWT.NONE);
66  		label.setText("Export :");
67  		label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
68  
69  		exportOnlyCertificates = new Button(container, SWT.RADIO);
70  		exportOnlyCertificates.setText("Certificates (Base 64 PEM format)");
71  		gd = new GridData(GridData.FILL_HORIZONTAL);
72  		gd.horizontalSpan = gl.numColumns - 1;
73  		exportOnlyCertificates.setLayoutData(gd);
74  		exportOnlyCertificates.setSelection(true);
75  		exportOnlyCertificates.addListener(SWT.Selection, this);
76  
77  		Label emptylabel = new Label (container, SWT.NONE);
78  		exportPKAndCertificates = new Button(container, SWT.RADIO);
79  		exportPKAndCertificates.setText("Keys && certificates (Keystore)");
80  		gd = new GridData(GridData.FILL_HORIZONTAL);
81  		gd.horizontalSpan = gl.numColumns - 1;
82  		exportPKAndCertificates.setLayoutData(gd);
83  		exportPKAndCertificates.setSelection(false);
84  		exportPKAndCertificates.addListener(SWT.Selection, this);
85  
86  	    eParapherTools.createGUILine(container, gl.numColumns);
87  
88  		Label labelKeystoreFormat = new Label(container, SWT.NULL);
89  		labelKeystoreFormat.setText("Keystore Type : ");
90  		labelKeystoreFormat.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
91  		keystoreFormat = new Combo(container, SWT.READ_ONLY |SWT.BORDER);
92  		keystoreFormat.setItems(EPKeystoreManager.KSFILEFORMAT);
93  		keystoreFormat.select(0);
94  		gd = new GridData(GridData.FILL_HORIZONTAL);
95  		gd.horizontalSpan = gl.numColumns - 1;
96  		keystoreFormat.setLayoutData(gd);
97  		keystoreFormat.addListener(SWT.Selection, this);
98  		keystoreFormat.setEnabled(false);
99  		
100 		Label labelKeystoreFile = new Label(container, SWT.NULL);
101 		labelKeystoreFile.setText("Keystore File : ");
102 		labelKeystoreFile.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
103 		exportedKeystoreFile = new Text(container,SWT.BORDER);
104 		exportedKeystoreFile.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
105 		exportedKeystoreFile.setText(JVMSettings.getUserHome());
106 		keystoreFileSelection = new Button(container, SWT.PUSH);
107 		keystoreFileSelection.setText("Select");
108 		keystoreFileSelection.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
109 		keystoreFileSelection.addListener(SWT.Selection, this);
110 		
111 		Label labelPKPassword = new Label(container, SWT.NULL);
112 		labelPKPassword.setText("Reuse PK passwords : ");
113 		labelPKPassword.setToolTipText("Carefull : this low down the security level.");
114 		labelPKPassword.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
115 		exportedPKPassword = new Button(container, SWT.CHECK );
116 		exportedPKPassword.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
117 		exportedPKPassword.setSelection(true);
118 		exportedPKPassword.setEnabled(false);
119 
120 		setKeystoreFilter();
121 		
122 		((ExportKeysCertificatesWizard)getWizard()).newSecret.setPageComplete(true);
123 		setPageComplete(isPageComplete());
124 		
125 		setControl(container);
126 	}
127 
128 	/**
129 	 * @see Listener#handleEvent(Event)
130 	 */
131 	public void handleEvent(Event event) {
132 	    
133 	    if ((event.widget == exportOnlyCertificates)) {
134 	    	if (exportOnlyCertificates.getSelection()) {
135 	    		keystoreFormat.setEnabled(false);
136 	    		exportedPKPassword.setEnabled(false);
137 	    		((ExportKeysCertificatesWizard)getWizard()).newSecret.setPageComplete(true);
138 	    		log.debug("Certificate only export");
139 	    	}
140 	    }
141 	    if ((event.widget == exportPKAndCertificates)) {
142 	    	if (exportPKAndCertificates.getSelection()) {
143 	    		keystoreFormat.setEnabled(true);
144 	    		exportedPKPassword.setEnabled(true);
145 	    		((ExportKeysCertificatesWizard)getWizard()).newSecret.setPageComplete(false);
146 	    		log.debug(" Certificate and private key export");
147 	    	}
148 	    }
149 	    if ((event.widget == keystoreFileSelection)) {
150 	    	selectExportFileOrDirectory();
151 	    	log.debug("Choose an exported File : Launch");
152 	    }
153 	    if ((event.widget == keystoreFormat))
154 	    	setKeystoreFilter();
155 
156 	    setPageComplete(isPageComplete());
157 	}
158     private void setKeystoreFilter() {
159     	//log.debug("Keystore type change : " + keystoreFormat.getText());
160     	if ( keystoreFormat.getText().equals("JKS") )
161     		keystoreFileExt = new String[] { "*.jks", "*." };
162     	if ( keystoreFormat.getText().equals("JCEKS") )
163     		keystoreFileExt = new String[] { "*.jceks", "*.jks", "*." };
164     	if ( keystoreFormat.getText().equals("BKS") )
165     		keystoreFileExt = new String[] { "*.bks", "*." };
166     	if ( keystoreFormat.getText().equals("PKCS12") )
167     		keystoreFileExt = new String[] { "*.p12", "*.pkcs12", "*." };
168 	}
169 
170 	public boolean isPageComplete() {
171 		this.canFlip=false;
172     	File exportfileordir = new File(exportedKeystoreFile.getText());
173     	if (exportOnlyCertificates.getSelection())
174     		if (!exportfileordir.isDirectory())
175     			return false;
176     	if (exportPKAndCertificates.getSelection()) {
177     		if (exportfileordir.isDirectory())
178     			return false;
179     		else
180     			this.canFlip=true;
181     	}
182     	//log.debug("page complete");
183         return true;
184     }
185 
186 	/*
187 	 * Returns the next page.
188 	 * Saves the values from this page in the model associated 
189 	 * with the wizard. Initializes the widgets on the next page.
190 	 */
191 	public IWizardPage getNextPage() {    		
192     	if (exportPKAndCertificates.getSelection()) {
193     		PinOrPassphraseWizardPage newPPhrase = ((ExportKeysCertificatesWizard)getWizard()).newSecret;
194 			return newPPhrase;
195 		}
196 		return null;
197 	}
198 
199 	public boolean canFlipToNextPage() {
200 		return this.canFlip;
201 	}
202 	
203 	private void selectExportFileOrDirectory() {
204 		String value="";
205 		if (exportOnlyCertificates.getSelection()) {
206 	        DirectoryDialog fd = new DirectoryDialog(getShell(), SWT.OPEN);
207 	        fd.setText("Directory to export X509 Certificate");
208 	        fd.setFilterPath(JVMSettings.getUserHome());
209 			value = fd.open();
210 		}
211 		if (exportPKAndCertificates.getSelection()) {
212 			FileDialog fd = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE);
213 			fd.setText("New Keystore");
214 	        fd.setFilterPath(JVMSettings.getUserHome());
215 	        fd.setFilterExtensions(keystoreFileExt);
216 			value = fd.open();
217 		}
218         exportedKeystoreFile.setText(value);
219 	}
220 	
221 	
222 	public String getKeystoreType() {
223 		return EPKeystoreManager.KSFILEFORMAT[keystoreFormat.getSelectionIndex()];
224 	}
225 	public String getKeystoreFile() {
226 		return exportedKeystoreFile.getText();
227 	}
228 	public boolean isPrivateKeyExported() {
229 		if (exportOnlyCertificates.getSelection())
230 			return false;
231 		if (exportPKAndCertificates.getSelection())
232 			return true;
233 		return false;
234 	}
235 	public boolean isPrivateKeyPassword() {
236 		return exportedPKPassword.getSelection();
237 	}
238 }