package org.eparapher.rcp.wizards; 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.Composite; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Text; import org.eparapher.core.EParapherManager; import org.eparapher.core.crypto.EPKeystoreManager; import org.eparapher.core.crypto.keystore.FileKeystore; import org.eparapher.core.crypto.keystore.HardwareKeyStore; import org.eparapher.core.crypto.tools.PasswordPolicyManager; import org.eparapher.core.interfaces.EParapherSettings; import org.eparapher.core.interfaces.IUserKeystore; import org.eparapher.rcp.tools.GUIIcons; public class PinOrPassphraseWizardPage extends WizardPage implements Listener { protected Text privateKeySecretText; protected Text privateKeySecretConfirmationText; private boolean isPrivateKey; private boolean isSecretcreation; private String ksSecretName; private String ksType; private String alias = ""; private int numCol = 6; Label[] pwdStrengthIcon; Label ppupperletter; Label pplowerletter; Label ppnumber; Label ppnumchar; Label ppspechar; protected PinOrPassphraseWizardPage( boolean misNew, boolean misPK, String malias) { super("Keystore Protection"); alias=malias; isPrivateKey = misPK; isSecretcreation = misNew; if (EPKeystoreManager.isPKCS11Used()) { ksType = "Token"; ksSecretName = "PIN Code"; } else { ksType = "file keystore"; ksSecretName = "passphrase"; } if (isSecretcreation) { setDescription("Please enter twice your new " + ksSecretName); } else { IUserKeystore userks = EPKeystoreManager.getInstance().getUserkeystore(); if (userks instanceof FileKeystore) setDescription("Please enter your " + ksSecretName + " to open :\r\n\t " + ((FileKeystore)userks).getKeystoreFileName() ); else if (userks instanceof HardwareKeyStore) setDescription("Please enter your " + ksType + " " + ksSecretName ); } if (isPrivateKey) { if (isSecretcreation) { setTitle("New private key secret for " + alias ); setImageDescriptor(GUIIcons.WIZARD_NEW_KEYPAIR); setPageComplete(false); } else { setTitle("Accessing your private key " + alias); setImageDescriptor(GUIIcons.WIZARD_PKEY_IMG); setDescription("Please enter your " + ksSecretName + " to access your " + alias + " private key."); } } else { if (isSecretcreation) { setTitle("New keystore secret"); setImageDescriptor(GUIIcons.WIZARD_NEW_KEYST); setPageComplete(false); } else { setTitle("Keystore secret"); if (EPKeystoreManager.isPKCS11Used()) setImageDescriptor(GUIIcons.WIZARD_SCARD_IMG); else setImageDescriptor(GUIIcons.WIZARD_KEYST_IMG); } } /* if (isPrivateKey) { if (isPassphrasecreation) { setTitle("New private key secret for " + alias); setDescription("Please enter twice your new" + ksSecretName); setImageDescriptor(GUIIcons.WIZARD_NEW_KEYPAIR); setPageComplete(false); } else { setTitle("Private key usage (Alias"+alias+")"); setDescription("Please enter your private key" + ksSecretName); setImageDescriptor(GUIIcons.WIZARD_PKEY_IMG); } } else { if (isPassphrasecreation) { setTitle("New keystore secret"); setDescription("Please enter twice your new" + ksSecretName); setImageDescriptor(GUIIcons.WIZARD_NEW_KEYST); setPageComplete(false); } else { setTitle("Keystore secret"); setDescription("Please enter your keystore " + ksSecretName); if (EPKeystoreManager.isPKCS11Used()) setImageDescriptor(GUIIcons.WIZARD_SCARD_IMG); else setImageDescriptor(GUIIcons.WIZARD_KEYST_IMG); } }*/ } public void createControl(Composite parent) { //parent.setLayout(new FillLayout()); Composite container = new Composite(parent, SWT.NULL); GridLayout gl = new GridLayout(numCol,false); gl.marginBottom=0; gl.marginTop=0; container.setLayout(gl); container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL)); Label label = new Label(container, SWT.NONE); label.setText(ksSecretName + " : "); label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); privateKeySecretText = new Text(container, SWT.BORDER | SWT.PASSWORD); GridData gd = new GridData(GridData.FILL_HORIZONTAL); gd.horizontalSpan = numCol - 1; privateKeySecretText.setLayoutData(gd); if (isSecretcreation) { Label label2 = new Label(container, SWT.NONE); label2.setText("Confirm "+ksSecretName+" : "); label2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); privateKeySecretConfirmationText = new Text(container, SWT.BORDER | SWT.PASSWORD); gd = new GridData(GridData.FILL_HORIZONTAL); gd.horizontalSpan = numCol - 1; privateKeySecretConfirmationText.setLayoutData(gd); //Pwd policy Label labelps = new Label(container, SWT.NONE); labelps.setText("Password strength : "); labelps.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); pwdStrengthIcon = new Label[5]; for (int i = 0; i < pwdStrengthIcon.length; i++) { pwdStrengthIcon[i] = new Label( container, SWT.NONE ); pwdStrengthIcon[i].setImage( GUIIcons.LOCK32KO_ICON_IMAGE ); pwdStrengthIcon[i].setLayoutData( new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); } EParapherSettings settings = EParapherManager.getInstance().getSettings(); String secret = (settings.getKeyStoreType().equals(EPKeystoreManager.PKCS11_CONFIGNAME)) ? "PIN" : "Passphrase"; String conditionnel = (settings.isSecPolCheck()) ? " must" : " might"; Group ng = new Group(container, SWT.NONE); ng.setText("Your new " + secret + conditionnel + " have more than : "); ng.setLayout(new GridLayout(2, false)); gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL); gd.horizontalSpan = numCol; ng.setLayoutData(gd); if (settings.isSecPolCheckCharLength()) ppnumchar = setPPLiveChecker(ng, settings.getSecPolMinCharLength() + " character(s)"); if (settings.isSecPolCheckUpperChar()) ppupperletter = setPPLiveChecker(ng, settings.getSecPolMinUpperChar() + " uppercase character(s)"); if (settings.isSecPolCheckLowerChar()) pplowerletter = setPPLiveChecker(ng, settings.getSecPolMinLowerChar() + " lowercase character(s)"); if (settings.isSecPolCheckNumberChar()) ppnumber = setPPLiveChecker(ng, settings.getSecPolMinNumberChar() + " single number character(s)"); if (settings.isSecPolCheckSpecialChar()) ppspechar = setPPLiveChecker(ng, settings.getSecPolMinSpecialChar() + " special character(s)"); if (settings.isSecPolCheck()) privateKeySecretConfirmationText.setEnabled(false); //Check privateKeySecretConfirmationText.addListener(SWT.KeyUp, this); privateKeySecretText.addListener(SWT.KeyUp, this); setErrorMessage("Please define a new passphrase"); } setControl(container); } private Label setPPLiveChecker(Composite container, String title) { Label lbl = new Label(container, SWT.NONE); lbl.setImage(GUIIcons.KO_ICON_IMAGE); lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); Label textlabel = new Label(container, SWT.NONE); textlabel.setText(title); GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); //gd.horizontalSpan = numCol-1; textlabel.setLayoutData(gd); return lbl; } public void setAlias(String malias) { this.alias = malias; } public String getSecret() { return privateKeySecretText.getText(); } public void setEmptySecretField() { if (privateKeySecretConfirmationText != null) privateKeySecretConfirmationText.setText(""); privateKeySecretText.setText(""); privateKeySecretText.setFocus(); } public String getSecretConfirmation() { return privateKeySecretConfirmationText.getText(); } public void setEmptySecretConfirmationField() { privateKeySecretConfirmationText.setText(""); } public void handleEvent(Event event) { if(getSecret().equalsIgnoreCase("")) { setErrorMessage("Please enter twice your new passphrase or cancel."); } else { if (!getSecret().equals(getSecretConfirmation())) { setErrorMessage("Input values are differents."); setPageComplete(false); } else { setErrorMessage(null); setMessage("Your passphrase has been successfully confirmed."); setPageComplete(true); } } if (event.widget == privateKeySecretText) { updatePasswordPolicyFields(); } } /** * Manage Password Policy */ private void updatePasswordPolicyFields() { PasswordPolicyManager ppmgr = PasswordPolicyManager.getIstance(); EParapherSettings settings = EParapherManager.getInstance().getSettings(); //update password policy icons if (settings.isSecPolCheckCharLength()) { if ( ppmgr.isMinCharsCheck(getSecret()) ) ppnumchar.setImage(GUIIcons.OK_ICON_IMAGE); else ppnumchar.setImage(GUIIcons.KO_ICON_IMAGE); } if (settings.isSecPolCheckUpperChar()) { if ( ppmgr.isMinUpperCheck(getSecret()) ) ppupperletter.setImage(GUIIcons.OK_ICON_IMAGE); else ppupperletter.setImage(GUIIcons.KO_ICON_IMAGE); } if (settings.isSecPolCheckLowerChar()) { if ( ppmgr.isMinLowerCheck(getSecret()) ) pplowerletter.setImage(GUIIcons.OK_ICON_IMAGE); else pplowerletter.setImage(GUIIcons.KO_ICON_IMAGE); } if (settings.isSecPolCheckNumberChar()) { if ( ppmgr.isMinNumberCheck(getSecret()) ) ppnumber.setImage(GUIIcons.OK_ICON_IMAGE); else ppnumber.setImage(GUIIcons.KO_ICON_IMAGE); } if (settings.isSecPolCheckSpecialChar()) { if ( ppmgr.isMinSpecialCheck(getSecret()) ) ppspechar.setImage(GUIIcons.OK_ICON_IMAGE); else ppspechar.setImage(GUIIcons.KO_ICON_IMAGE); } //update Password Strength int strength = ppmgr.getPasswordStrength(getSecret()); for (int i = 0; i < pwdStrengthIcon.length; i++) { if (strength>i) pwdStrengthIcon[i].setImage( GUIIcons.LOCK32_ICON_IMAGE ); else pwdStrengthIcon[i].setImage( GUIIcons.LOCK32KO_ICON_IMAGE ); } //Check if pwd/PIN policy is validated if (settings.isSecPolCheck()) if ( ppmgr.isSecPolicyValidated(getSecret()) ) privateKeySecretConfirmationText.setEnabled(true); else privateKeySecretConfirmationText.setEnabled(false); } }