View Javadoc

1   package org.eparapher.rcp.views;
2   
3   
4   import java.security.KeyStoreException;
5   import java.security.PrivateKey;
6   import java.security.cert.X509Certificate;
7   import java.util.Iterator;
8   
9   import org.apache.log4j.Logger;
10  import org.eclipse.jface.action.Action;
11  import org.eclipse.jface.action.IMenuManager;
12  import org.eclipse.jface.action.IToolBarManager;
13  import org.eclipse.jface.action.Separator;
14  import org.eclipse.jface.viewers.ISelection;
15  import org.eclipse.jface.viewers.IStructuredSelection;
16  import org.eclipse.jface.window.Window;
17  import org.eclipse.jface.wizard.WizardDialog;
18  import org.eclipse.swt.graphics.Image;
19  import org.eclipse.swt.widgets.Composite;
20  import org.eclipse.ui.IViewPart;
21  import org.eclipse.ui.PlatformUI;
22  import org.eparapher.core.crypto.EPKeystoreManager;
23  import org.eparapher.core.crypto.KeystoreEntry;
24  import org.eparapher.core.crypto.keystore.IUserKeystore;
25  import org.eparapher.rcp.EPReferences;
26  import org.eparapher.rcp.dialog.CertificateRequestViewerDialog;
27  import org.eparapher.rcp.dialog.CertificateViewerDialog;
28  import org.eparapher.rcp.tools.GUIIcons;
29  import org.eparapher.rcp.tools.eParapherTools;
30  import org.eparapher.rcp.wizards.ExportKeysCertificatesWizard;
31  import org.eparapher.rcp.wizards.ImportKeysCertificatesWizard;
32  import org.eparapher.rcp.wizards.NewCertificateWizard;
33  import org.eparapher.rcp.wizards.PinOrPassphraseWizard;
34  
35  import org.eparapher.rcp.tools.RCPGUI;
36  
37  /**
38   * This view shows user's certificates obtained from the
39   * Keystore defined in the configuration. 
40   * <p>
41   * The view uses a label provider to define how model
42   * objects should be presented in the view. Each
43   * view can present the same model objects using
44   * different labels and icons, if needed. Alternatively,
45   * a single label provider can be shared between views
46   * in order to ensure that objects of the same type are
47   * presented in the same way everywhere.
48   * <p>
49   */
50  
51  public class UserCertificateStoreView extends AbstractCertificateView {
52  	
53  	public static final String ID = "org.eparapher.rcp.views.UserCertificateStoreView";
54  	private static Logger log = Logger.getLogger(UserCertificateStoreView.class);
55  	
56  	private Action selectAliasAction;
57  	private Action newCertificateAction;
58  	private Action changePasswordAction;
59  	/**
60  	 * The constructor.
61  	 */
62  	public UserCertificateStoreView() {
63  		
64  	}
65  	
66  	/**
67  	 * This is a callback that will allow us
68  	 * to create the viewer and initialize it.
69  	 */
70  	public void createPartControl(Composite parent) {
71  		
72  		super.createPartControl(parent);
73  		
74  		EPReferences.getInstance().setUsercertview(this);
75  	}
76  	
77  	protected void fillLocalPullDown(IMenuManager manager) {
78  		manager.add(newCertificateAction);
79  		manager.add(new Separator());
80  		super.fillLocalPullDown(manager);
81  	}
82  
83  	protected void fillContextMenu(IMenuManager manager) {
84  		manager.add(selectAliasAction);
85  		manager.add(changePasswordAction);
86  		manager.add(new Separator());
87  		super.fillContextMenu(manager);
88  	}
89  	
90  	protected void fillLocalToolBar(IToolBarManager manager) {
91  		manager.add(newCertificateAction);
92  		manager.add(new Separator());
93  		manager.add(refreshAction);
94  		manager.add(changePasswordAction);
95  		manager.add(new Separator());
96  		manager.add(importAction);
97  		manager.add(exportAction);
98  	}
99  
100 	protected void makeActions() {
101 
102 		super.makeActions();
103 
104 		importAction = new ImportAction();
105 		exportAction = new ExportAction();
106 
107 		newCertificateAction = new NewX509Action();
108 
109 		selectAliasAction    = new SelectAliasAction();
110 
111 		changePasswordAction = new ChangePasswordAction(this);
112 	}
113 
114 	@Override
115 	protected Image getCertificateImage(Object obj) {
116 		KeystoreEntry certchain = (KeystoreEntry) obj;
117 		IUserKeystore userKeystore = EPKeystoreManager.getInstance().getUserkeystore();
118 		if (certchain.getKeystoreAlias().equals(userKeystore.getDefaultAlias()))
119 			return GUIIcons.CERTIFICATE_SEL_ICON_IMAGE;
120 		else if ( certchain.getCertificateChain()==null || certchain.getCertificateChain().length==0 )
121 			return GUIIcons.PRIVATE_KEY_ICON_IMAGE;
122 		else if (certchain.isTrustedCertificate())
123 			return GUIIcons.CERTIFICATE_ICON_IMAGE;
124 		else return GUIIcons.PK_AND_CERT_ICON_IMAGE;
125 	}
126 
127 	@Override
128 	protected Object[] getKeystoreEntries() {
129 		KeystoreEntry[] certlist = new KeystoreEntry[0];
130 		IUserKeystore userKeystore = EPKeystoreManager.getInstance().getUserkeystore();
131 		if ( userKeystore == null ) {
132 	        eParapherTools.errorMessage("Failed to initialize your Personal Keystore.\r\nPlease change it in configuration.");
133 		} else if ( userKeystore.loadKeyStore() ) {
134 			certlist = userKeystore.getKeystoreEntries();
135 		}
136 		return certlist;
137 	}
138 	
139 
140 	class ImportAction extends Action {
141 		public ImportAction() {
142 			setText("Import...");
143 			setToolTipText("Import a keypair and/or a certificate");
144 			setImageDescriptor(GUIIcons.IMPORT_ICON);
145 		}
146 		public void run() {
147 			//RCPGUI.infoMessage("Import a keypair and/or certificate","Not implemented yet.");
148 			ImportKeysCertificatesWizard importWizard = new ImportKeysCertificatesWizard();
149 			WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), importWizard);
150 			if ( dialog.open() != Window.OK ) {
151 				log.info("Keys and certificate export wizard cancelled");
152 				return;
153 			}
154 			
155 		}
156 	}
157 	
158 	class ExportAction extends Action {
159 		public ExportAction() {
160 			setText("Export...");
161 			setToolTipText("Export user keypair & certificate");
162 			setImageDescriptor(GUIIcons.EXPORT_ICON);
163 		}
164 		public void run() {
165 			ISelection selection = viewer.getSelection();
166 			if (selection.isEmpty())
167 				RCPGUI.infoMessage("Export a certificate","Please select a certificate in the list.");
168 			else {
169 				int size = ((IStructuredSelection)selection).size();
170 				String[] selected_aliases = new String[size];
171 				int i=0;
172 				IStructuredSelection struct_sel = (IStructuredSelection)selection;
173 				for (Iterator<KeystoreEntry> iterator = struct_sel.iterator(); iterator.hasNext();) {
174 					KeystoreEntry type = iterator.next();
175 					selected_aliases[i++] = type.getKeystoreAlias();
176 				}
177 				
178 				//Launch Wizard to get PDF Signature Parameters
179 				ExportKeysCertificatesWizard exportWizard = new ExportKeysCertificatesWizard(selected_aliases);
180 				WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), exportWizard);
181 				if ( dialog.open() != Window.OK ) {
182 					log.info("Keys and certificate export wizard cancelled");
183 					return;
184 				}
185 				
186 			}
187 		}
188 	}
189 	class ChangePasswordAction extends Action {
190 		IViewPart vp;
191 		public ChangePasswordAction(IViewPart mvp) {
192 			setText("Change &Passphrase");
193 			setToolTipText("Change the selected private key passphrase");
194 			setImageDescriptor(GUIIcons.LOCK_ICON);
195 			vp = mvp;
196 		}
197 		public void run() {
198 			ISelection selection = viewer.getSelection();
199 			if (selection.isEmpty())
200 				RCPGUI.infoMessage("Change private key passphrase","Please select one entry in the list.");
201 			else {
202 				String[] aliases_to_reset_pwd = getSelectedAlias();
203 				IUserKeystore  userks = EPKeystoreManager.getInstance().getUserkeystore();
204 				String initial_default_alias = userks.getDefaultAlias();
205 				for (String alias : aliases_to_reset_pwd) {
206 					
207 					userks.setDefaultAlias(alias);
208 					
209 					X509Certificate[] cc = userks.getX509CertificateChain();
210 					userks.loadPrivateKey();
211 					PrivateKey        pk = userks.getPrivateKey();
212 					
213 					PinOrPassphraseWizard passphraseWizard = new PinOrPassphraseWizard(true, true, alias );
214 					String pwd = passphraseWizard.askForSecret();
215 					if (pwd != null) {
216 						try {
217 							userks.getKeystore().setKeyEntry(alias, pk, pwd.toCharArray(), cc);
218 						} catch (KeyStoreException e) {
219 							log.error("Error while changing private key password", e);
220 						}
221 					}
222 				}
223 				userks.setDefaultAlias(initial_default_alias);
224 				//eParapherTools.infoMessage("Change private key passphrase","Not implemented yet");
225 			}
226 		}
227 	}
228 	class NewX509Action extends Action {
229 		public NewX509Action() {
230 			setText("New certificate");
231 			setToolTipText("Create a new X509 certificate");
232 			setImageDescriptor(GUIIcons.NEW_CERT_ICON);
233 		}
234 		public void run() {
235 			if (!EPKeystoreManager.getInstance().getUserkeystore().loadKeyStore()) {
236 				log.info("Cannot create a new keypair if the keystore is not loaded");
237 				return;
238 			}
239 			NewCertificateWizard newCertWizard = new NewCertificateWizard();
240 			WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), newCertWizard);
241 			if ( dialog.open() != Window.OK ) {
242 				log.info("New Certificate wizard cancelled");
243 			} else {
244 				viewer.refresh();
245 				if (newCertWizard.isCSR()) {
246 					CertificateRequestViewerDialog csrdialog = new CertificateRequestViewerDialog(newCertWizard.getCSRFile());
247 					csrdialog.open();
248 				}
249 				if (newCertWizard.isSelfSignedCert()) {
250 					CertificateViewerDialog csrdialog = new CertificateViewerDialog(newCertWizard.getGeneratedCertificate());
251 					csrdialog.open();
252 				}
253 			}
254 		}
255 	}
256 	
257 	class SelectAliasAction extends Action {
258 		public SelectAliasAction() {
259 			setText("Select as Default Certificate");
260 			setToolTipText("Select this certificate as a the defaultCertificate for signing");
261 			setImageDescriptor(GUIIcons.CERT_DEF_ICON);
262 		}
263 		public void run() {
264 			ISelection selection = viewer.getSelection();
265 			if (selection.isEmpty())
266 				RCPGUI.infoMessage("Default certificate","Please select first a certificate in the list.");
267 			if ( ((IStructuredSelection)selection).size() !=1 )
268 				RCPGUI.infoMessage("Default certificate","Please select only one certificate.");
269 			else {
270 				Object obj = ((IStructuredSelection)selection).getFirstElement();
271 				KeystoreEntry kse = (KeystoreEntry) obj;
272 				if (kse.getCertificateChain() != null && kse.getCertificateChain().length!=0 ) {
273 					String selectedAlias = kse.getKeystoreAlias();
274 					IUserKeystore userKeystore = EPKeystoreManager.getInstance().getUserkeystore();
275 					userKeystore.setDefaultAlias(selectedAlias);
276 					viewer.refresh();
277 				} else {
278 					RCPGUI.infoMessage("Default certificate","Cannot set this entry as the default one's for signature : there is no certificate.");
279 				}
280 			}
281 		}
282 	}
283 }