View Javadoc

1   package org.eparapher.rcp.wizards;
2   
3   import org.eclipse.jface.wizard.WizardPage;
4   import org.eclipse.swt.SWT;
5   import org.eclipse.swt.layout.FillLayout;
6   import org.eclipse.swt.layout.GridData;
7   import org.eclipse.swt.layout.GridLayout;
8   import org.eclipse.swt.widgets.Composite;
9   import org.eclipse.swt.widgets.Event;
10  import org.eclipse.swt.widgets.Group;
11  import org.eclipse.swt.widgets.Label;
12  import org.eclipse.swt.widgets.Listener;
13  import org.eclipse.swt.widgets.Text;
14  import org.eparapher.core.EParapherManager;
15  import org.eparapher.core.crypto.EPKeystoreManager;
16  import org.eparapher.core.crypto.keystore.FileKeystore;
17  import org.eparapher.core.crypto.keystore.HardwareKeyStore;
18  import org.eparapher.core.crypto.keystore.IUserKeystore;
19  import org.eparapher.core.crypto.tools.PasswordPolicyManager;
20  import org.eparapher.core.interfaces.EParapherSettings;
21  import org.eparapher.rcp.tools.GUIIcons;
22  import org.eparapher.rcp.tools.RCPSettings;
23  
24  
25  public class PinOrPassphraseWizardPage extends WizardPage implements Listener {
26  
27  	private Text privateKeySecretText;
28  	private Text privateKeySecretConfirmationText;
29  	
30  	private boolean isPrivateKey;
31  	private boolean isSecretcreation;
32  	
33  	private String ksSecretName;
34  	private String ksType;
35  	
36  	private String alias = "";
37  	
38  	private int numCol = 6;
39  
40  	Label[] pwdStrengthIcon;
41  
42  	Label ppupperletter;
43  	Label pplowerletter;
44  	Label ppnumber;
45  	Label ppnumchar;
46  	Label ppspechar;
47  	
48  	protected PinOrPassphraseWizardPage( boolean misNew, boolean misPK, String malias) {
49  		super("Keystore Protection");
50  		alias=malias;
51  		isPrivateKey = misPK;
52  		isSecretcreation = misNew;
53  
54  		if (EPKeystoreManager.isPKCS11Used()) {
55  			ksType       = "Token";
56  			ksSecretName = "PIN Code";
57  		} else {
58  			ksType       = "file keystore";
59  			ksSecretName = "passphrase";
60  		}
61  		
62  		if (isSecretcreation) {
63  			setDescription("Please enter twice your new " + ksSecretName);
64  		} else {
65  			IUserKeystore userks = EPKeystoreManager.getInstance().getUserkeystore();
66  			if (userks instanceof FileKeystore)
67  				setDescription("Please enter your " + ksSecretName + " to open :\r\n\t " + ((FileKeystore)userks).getKeystoreFileName() );
68  			else if (userks instanceof HardwareKeyStore)
69  				setDescription("Please enter your " + ksType + " " + ksSecretName );
70  		}
71  		if (isPrivateKey) {
72  			if (isSecretcreation) {
73  				setTitle("New private key secret for " + alias );
74  				setImageDescriptor(GUIIcons.WIZARD_NEW_KEYPAIR);
75  				setPageComplete(false);
76  			} else {
77  				setTitle("Accessing your private key " + alias);
78  				setImageDescriptor(GUIIcons.WIZARD_PKEY_IMG);
79  				setDescription("Please enter your " + ksSecretName + " to access your " + alias + " private key.");
80  			}
81  		} else {
82  			if (isSecretcreation) {
83  				setTitle("New keystore secret");
84  				setImageDescriptor(GUIIcons.WIZARD_NEW_KEYST);
85  				setPageComplete(false);
86  			} else {
87  				setTitle("Keystore secret");
88  				if (EPKeystoreManager.isPKCS11Used())
89  					setImageDescriptor(GUIIcons.WIZARD_SCARD_IMG);
90  				else
91  					setImageDescriptor(GUIIcons.WIZARD_KEYST_IMG);
92  			}
93  		}
94  		/*
95  		if (isPrivateKey) {
96  			if (isPassphrasecreation) {
97  				setTitle("New private key secret for " + alias);
98  				setDescription("Please enter twice your new" + ksSecretName);
99  				setImageDescriptor(GUIIcons.WIZARD_NEW_KEYPAIR);
100 				setPageComplete(false);
101 			} else {
102 				setTitle("Private key usage (Alias"+alias+")");
103 				setDescription("Please enter your private key" + ksSecretName);
104 				setImageDescriptor(GUIIcons.WIZARD_PKEY_IMG);
105 			}
106 		} else {
107 			if (isPassphrasecreation) {
108 				setTitle("New keystore secret");
109 				setDescription("Please enter twice your new" + ksSecretName);
110 				setImageDescriptor(GUIIcons.WIZARD_NEW_KEYST);
111 				setPageComplete(false);
112 			} else {
113 				setTitle("Keystore secret");
114 				setDescription("Please enter your keystore " + ksSecretName);
115 				if (EPKeystoreManager.isPKCS11Used())
116 					setImageDescriptor(GUIIcons.WIZARD_SCARD_IMG);
117 				else
118 					setImageDescriptor(GUIIcons.WIZARD_KEYST_IMG);
119 			}
120 		}*/
121 	}
122 
123 	public void createControl(Composite parent) {
124 
125 	    //parent.setLayout(new FillLayout());
126 		Composite container = new Composite(parent, SWT.NULL);
127 		GridLayout gl = new GridLayout(numCol,false);
128 		gl.marginBottom=0;
129 		gl.marginTop=0;
130 		container.setLayout(gl);
131 		container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL));
132 
133 		Label label = new Label(container, SWT.NONE);
134 		label.setText(ksSecretName + " : ");
135 		label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
136 		
137 		privateKeySecretText = new Text(container, SWT.BORDER | SWT.PASSWORD);
138 		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
139 		gd.horizontalSpan = numCol - 1;
140 		privateKeySecretText.setLayoutData(gd);
141 		
142 		if (isSecretcreation) {
143 			Label label2 = new Label(container, SWT.NONE);
144 			label2.setText("Confirm "+ksSecretName+" : ");
145 			label2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
146 		
147 			privateKeySecretConfirmationText = new Text(container, SWT.BORDER | SWT.PASSWORD);
148 			gd = new GridData(GridData.FILL_HORIZONTAL);
149 			gd.horizontalSpan = numCol - 1;
150 			privateKeySecretConfirmationText.setLayoutData(gd);
151 			
152 			//Pwd policy
153 			Label labelps = new Label(container, SWT.NONE);
154 			labelps.setText("Password strength : ");
155 			labelps.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
156 			
157 			pwdStrengthIcon = new Label[5];
158 			for (int i = 0; i < pwdStrengthIcon.length; i++) {
159 				pwdStrengthIcon[i] = new Label( container, SWT.NONE );
160 				pwdStrengthIcon[i].setImage( GUIIcons.LOCK32KO_ICON_IMAGE );
161 				pwdStrengthIcon[i].setLayoutData( new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
162 			}
163 			
164 			EParapherSettings settings = EParapherManager.getInstance().getSettings();
165 			
166 			String secret = (settings.getPersonalStoreType().equals(EPKeystoreManager.PKCS11_CONFIGNAME)) ? "PIN" : "Passphrase";
167 			String conditionnel = (settings.isSecPolCheck()) ? " must" : " might";
168 			
169 			Group ng = new Group(container, SWT.NONE);
170 			ng.setText("Your new " + secret + conditionnel + " have more than : ");
171 			ng.setLayout(new GridLayout(2, false));
172 			gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL);
173 			gd.horizontalSpan = numCol;
174 			ng.setLayoutData(gd);
175 
176 			if (settings.isSecPolCheckCharLength())
177 				ppnumchar     = setPPLiveChecker(ng, settings.getSecPolMinCharLength() + " character(s)");
178 			if (settings.isSecPolCheckUpperChar())
179 				ppupperletter = setPPLiveChecker(ng, settings.getSecPolMinUpperChar() + " uppercase character(s)");
180 			if (settings.isSecPolCheckLowerChar())
181 				pplowerletter = setPPLiveChecker(ng, settings.getSecPolMinLowerChar() + " lowercase character(s)");
182 			if (settings.isSecPolCheckNumberChar())
183 				ppnumber      = setPPLiveChecker(ng, settings.getSecPolMinNumberChar() + " single number character(s)");
184 			if (settings.isSecPolCheckSpecialChar())
185 				ppspechar     = setPPLiveChecker(ng, settings.getSecPolMinSpecialChar() + " special character(s)");
186 			
187 			if (settings.isSecPolCheck())
188 				privateKeySecretConfirmationText.setEnabled(false);
189 			//Check
190 			privateKeySecretConfirmationText.addListener(SWT.KeyUp, this);
191 			privateKeySecretText.addListener(SWT.KeyUp, this);
192 			
193 			setErrorMessage("Please define a new passphrase");
194 			
195 		}
196 		setControl(container);
197 	}
198 	
199 	
200 	private Label setPPLiveChecker(Composite container, String title) {
201 		Label lbl = new Label(container, SWT.NONE);
202 		lbl.setImage(GUIIcons.KO_ICON_IMAGE);
203 		lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
204 		
205 		Label textlabel = new Label(container, SWT.NONE);
206 		textlabel.setText(title);
207 		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
208 		//gd.horizontalSpan = numCol-1;
209 		textlabel.setLayoutData(gd);
210 		
211 		return lbl;
212 	}
213 
214 	public void setAlias(String malias) {
215 		this.alias = malias;
216 	}
217 	
218 	public String getSecret() {
219 		return privateKeySecretText.getText();
220 	}
221 	public void setEmptySecretField() {
222 		if (privateKeySecretConfirmationText != null)
223 			privateKeySecretConfirmationText.setText("");
224 		privateKeySecretText.setText("");
225 		privateKeySecretText.setFocus();
226 	}
227 	
228 	public String getSecretConfirmation() {
229 		return privateKeySecretConfirmationText.getText();
230 	}
231 
232 	public void setEmptySecretConfirmationField() {
233 		privateKeySecretConfirmationText.setText("");
234 	}
235 
236 	public void handleEvent(Event event) {
237 		
238 		if(getSecret().equalsIgnoreCase("")) {
239 			setErrorMessage("Please enter twice your new passphrase or cancel.");
240 		} else	{
241 			if (!getSecret().equals(getSecretConfirmation())) {
242 				setErrorMessage("Input values are differents.");
243 				setPageComplete(false);
244 			} else {
245 				setErrorMessage(null);
246 				setMessage("Your passphrase has been successfully confirmed.");
247 				setPageComplete(true);
248 			}
249 		}
250 		if (event.widget == privateKeySecretText) {
251 			updatePasswordPolicyFields();
252 		}
253 		
254 	}
255 
256 	/**
257 	 * Manage Password Policy
258 	 */
259 	private void updatePasswordPolicyFields() {
260 		
261 		PasswordPolicyManager ppmgr = PasswordPolicyManager.getIstance();
262 		
263 		EParapherSettings settings = EParapherManager.getInstance().getSettings();
264 		
265 		//update password policy icons
266 		if (settings.isSecPolCheckCharLength()) {
267 			if ( ppmgr.isMinCharsCheck(getSecret()) )
268 				ppnumchar.setImage(GUIIcons.OK_ICON_IMAGE);
269 			else ppnumchar.setImage(GUIIcons.KO_ICON_IMAGE);
270 		}
271 		if (settings.isSecPolCheckUpperChar()) {
272 			if ( ppmgr.isMinUpperCheck(getSecret()) )
273 				ppupperletter.setImage(GUIIcons.OK_ICON_IMAGE);
274 			else ppupperletter.setImage(GUIIcons.KO_ICON_IMAGE);
275 		}
276 		if (settings.isSecPolCheckLowerChar()) {
277 			if ( ppmgr.isMinLowerCheck(getSecret()) )
278 				pplowerletter.setImage(GUIIcons.OK_ICON_IMAGE);
279 			else pplowerletter.setImage(GUIIcons.KO_ICON_IMAGE);
280 		}
281 		if (settings.isSecPolCheckNumberChar()) {
282 			if ( ppmgr.isMinNumberCheck(getSecret()) )
283 				 ppnumber.setImage(GUIIcons.OK_ICON_IMAGE);
284 			else ppnumber.setImage(GUIIcons.KO_ICON_IMAGE);
285 		}
286 		if (settings.isSecPolCheckSpecialChar()) {
287 			if ( ppmgr.isMinSpecialCheck(getSecret()) )
288 				ppspechar.setImage(GUIIcons.OK_ICON_IMAGE);
289 			else ppspechar.setImage(GUIIcons.KO_ICON_IMAGE);
290 		}
291 
292 		//update Password Strength
293 		int strength = ppmgr.getPasswordStrength(getSecret());
294 		for (int i = 0; i < pwdStrengthIcon.length; i++) {
295 			if (strength>i)
296 				pwdStrengthIcon[i].setImage( GUIIcons.LOCK32_ICON_IMAGE );
297 			else
298 				pwdStrengthIcon[i].setImage( GUIIcons.LOCK32KO_ICON_IMAGE );
299 		}
300 		
301 		//Check if pwd/PIN policy is validated
302 		if (settings.isSecPolCheck())
303 			if ( ppmgr.isSecPolicyValidated(getSecret()) ) 
304 				privateKeySecretConfirmationText.setEnabled(true);
305 			else
306 				privateKeySecretConfirmationText.setEnabled(false);
307 	}
308 
309 }