package org.eparapher.rcp.views; import java.security.KeyStoreException; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.Iterator; import org.apache.log4j.Logger; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.action.Separator; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.window.Window; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IViewPart; import org.eclipse.ui.PlatformUI; import org.eparapher.core.crypto.EPKeystoreManager; import org.eparapher.core.crypto.keystore.KeystoreEntry; import org.eparapher.core.interfaces.IUserKeystore; import org.eparapher.rcp.EPReferences; import org.eparapher.rcp.dialog.CertificateRequestViewerDialog; import org.eparapher.rcp.dialog.CertificateViewerDialog; import org.eparapher.rcp.tools.GUIIcons; import org.eparapher.rcp.tools.eParapherTools; import org.eparapher.rcp.wizards.ExportKeysCertificatesWizard; import org.eparapher.rcp.wizards.ImportKeysCertificatesWizard; import org.eparapher.rcp.wizards.NewCertificateWizard; import org.eparapher.rcp.wizards.PinOrPassphraseWizard; import org.eparapher.rcp.tools.RCPGUI; /** * This view shows user's certificates obtained from the * Keystore defined in the configuration. *
* The view uses a label provider to define how model * objects should be presented in the view. Each * view can present the same model objects using * different labels and icons, if needed. Alternatively, * a single label provider can be shared between views * in order to ensure that objects of the same type are * presented in the same way everywhere. *
*/
public class UserCertificateStoreView extends AbstractCertificateView {
public static final String ID = "org.eparapher.rcp.views.UserCertificateStoreView";
private static Logger log = Logger.getLogger(UserCertificateStoreView.class);
private Action selectAliasAction;
private Action newCertificateAction;
private Action newSecretKeyAction;
private Action changePasswordAction;
/**
* The constructor.
*/
public UserCertificateStoreView() {
}
/**
* This is a callback that will allow us
* to create the viewer and initialize it.
*/
public void createPartControl(Composite parent) {
super.createPartControl(parent);
EPReferences.getInstance().setUsercertview(this);
}
protected void fillLocalPullDown(IMenuManager manager) {
manager.add(newCertificateAction);
manager.add(newSecretKeyAction);
manager.add(new Separator());
super.fillLocalPullDown(manager);
}
protected void fillContextMenu(IMenuManager manager) {
manager.add(selectAliasAction);
manager.add(changePasswordAction);
manager.add(new Separator());
super.fillContextMenu(manager);
}
protected void fillLocalToolBar(IToolBarManager manager) {
manager.add(newCertificateAction);
manager.add(newSecretKeyAction);
manager.add(new Separator());
manager.add(refreshAction);
manager.add(changePasswordAction);
manager.add(new Separator());
manager.add(importAction);
manager.add(exportAction);
}
protected void makeActions() {
super.makeActions();
importAction = new ImportAction();
exportAction = new ExportAction();
newCertificateAction = new NewX509Action();
newSecretKeyAction = new NewSymKeyAction();
selectAliasAction = new SelectAliasAction();
changePasswordAction = new ChangePasswordAction(this);
}
@Override
protected Image getCertificateImage(Object obj) {
KeystoreEntry certchain = (KeystoreEntry) obj;
IUserKeystore userKeystore = EPKeystoreManager.getInstance().getUserkeystore();
if (certchain.getKeystoreAlias().equals(userKeystore.getDefaultAlias()))
return GUIIcons.CERTIFICATE_SEL_ICON_IMAGE;
else if ( certchain.getCertificateChain()==null || certchain.getCertificateChain().length==0 )
return GUIIcons.PRIVATE_KEY_ICON_IMAGE;
else if (certchain.isTrustedCertificate())
return GUIIcons.CERTIFICATE_ICON_IMAGE;
else return GUIIcons.PK_AND_CERT_ICON_IMAGE;
}
@Override
protected Object[] getKeystoreEntries() {
KeystoreEntry[] certlist = new KeystoreEntry[0];
IUserKeystore userKeystore = EPKeystoreManager.getInstance().getUserkeystore();
if ( userKeystore == null ) {
eParapherTools.errorMessage("Failed to initialize your Personal Keystore.\r\nPlease change it in configuration.");
} else if ( userKeystore.loadKeyStore() ) {
certlist = userKeystore.getAllEntries();
}
return certlist;
}
class ImportAction extends Action {
public ImportAction() {
setText("Import...");
setToolTipText("Import a keypair and/or a certificate");
setImageDescriptor(GUIIcons.IMPORT_ICON);
}
public void run() {
//RCPGUI.infoMessage("Import a keypair and/or certificate","Not implemented yet.");
ImportKeysCertificatesWizard importWizard = new ImportKeysCertificatesWizard();
WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), importWizard);
if ( dialog.open() != Window.OK ) {
log.info("Keys and certificate export wizard cancelled");
return;
}
}
}
class ExportAction extends Action {
public ExportAction() {
setText("Export...");
setToolTipText("Export user keypair & certificate");
setImageDescriptor(GUIIcons.EXPORT_ICON);
}
public void run() {
ISelection selection = viewer.getSelection();
if (selection.isEmpty())
RCPGUI.infoMessage("Export a certificate","Please select a certificate in the list.");
else {
int size = ((IStructuredSelection)selection).size();
String[] selected_aliases = new String[size];
int i=0;
IStructuredSelection struct_sel = (IStructuredSelection)selection;
for (Iterator