package org.eparapher.rcp.views; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.security.cert.CRLException; import java.security.cert.CertificateEncodingException; import java.util.ArrayList; import org.apache.log4j.Logger; import org.bouncycastle.asn1.ASN1InputStream; import org.bouncycastle.asn1.DERApplicationSpecific; import org.bouncycastle.asn1.DEREnumerated; import org.bouncycastle.asn1.DERObject; import org.bouncycastle.asn1.DERUnknownTag; import org.bouncycastle.asn1.pkcs.ContentInfo; import org.bouncycastle.asn1.util.ASN1Dump; import org.bouncycastle.jce.PKCS10CertificationRequest; import org.bouncycastle.jce.provider.X509CRLObject; import org.bouncycastle.jce.provider.X509CertificateObject; import org.bouncycastle.openssl.PEMReader; import org.bouncycastle.x509.X509AttributeCertificate; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerSorter; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.ISelectionService; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; import org.eparapher.core.tools.FileUtil; import org.eparapher.rcp.views.documents.SecuredDocumentsView; /** * TODO : evolve with a tree viewer. *
* *
*/ public class ASN1Viewer extends ViewPart { public static final String ID = "org.eparapher.rcp.views.ASN1Viewer"; private static Logger log = Logger.getLogger(ASN1Viewer.class); private Text textdump; //private TreeViewer viewer; //private DrillDownAdapter drillDownAdapter; //private Action action1; //private Action action2; //private Action doubleClickAction; private ISelectionListener mylistener = new ISelectionListener() { public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) { //log.debug("selection changed on " + sourcepart + ". new selection is : "+selection); if (sourcepart instanceof SecuredDocumentsView && selection instanceof IStructuredSelection) { IStructuredSelection is = (IStructuredSelection) selection; textdump.setText("Please select only one item in the document view"); if (is.size()==1) { SecuredDocumentsView.TreeObject to = (SecuredDocumentsView.TreeObject) is.getFirstElement(); if (to instanceof SecuredDocumentsView.TreeParent ) textdump.setText("directory selected."); else { String path = to.getFilePath(); if (FileUtil.isFileTooLargeForJVMFreeRAM(path)) { textdump.setText("file " + path + " is too big for the JVM Free RAM"); } else { ASN1InputStream asn1is = null; DERObject der = null; //First : try with PEMReader try { PEMReader pr = new PEMReader(new FileReader(path)); Object obj = pr.readObject(); if (obj instanceof X509CertificateObject) asn1is = new ASN1InputStream(((X509CertificateObject)obj).getEncoded()); if (obj instanceof X509CRLObject) asn1is = new ASN1InputStream(((X509CRLObject)obj).getEncoded()); if (obj instanceof PKCS10CertificationRequest) asn1is = new ASN1InputStream(((PKCS10CertificationRequest)obj).getEncoded()); if (obj instanceof ContentInfo) asn1is = new ASN1InputStream(((ContentInfo)obj).getEncoded()); if (obj instanceof X509AttributeCertificate) asn1is = new ASN1InputStream(((X509AttributeCertificate)obj).getEncoded()); //if (obj instanceof ECNamedCurveParameterSpec) // asn1is = new ASN1InputStream(((ECNamedCurveParameterSpec)obj).getEncoded()); if (asn1is!=null) der = asn1is.readObject(); } catch (FileNotFoundException e) { log.debug(path + " is not found"); } catch (IOException e) { log.debug(path + " is not an ASN1 structure"); } catch (CertificateEncodingException e) { log.debug(path + " is not a valid X509 structure"); } catch (CRLException e) { log.debug(path + " is not a valid X509 CRL structure"); } //Then try to read it directly in ASN1 if (asn1is==null) { try { asn1is = new ASN1InputStream(FileUtil.readFile(path)); der = asn1is.readObject(); } catch (IOException e) { log.debug(path + " is not a valid ASN1 structure"); } } if ( der!=null && !( der instanceof DERUnknownTag) && !( der instanceof DEREnumerated) && !( der instanceof DERApplicationSpecific)) textdump.setText(ASN1Dump.dumpAsString(der,true)); else textdump.setText(path + " is not a valid ASN1 structure"); } } } } } }; /* * The content provider class is responsible for * providing objects to the view. It can wrap * existing objects in adapters or simply return * objects as-is. These objects may be sensitive * to the current input of the view, or ignore * it and always show the same content * (like Task List, for example). */ class TreeObject implements IAdaptable { private String name; private TreeParent parent; public TreeObject(String name) { this.name = name; } public String getName() { return name; } public void setParent(TreeParent parent) { this.parent = parent; } public TreeParent getParent() { return parent; } public String toString() { return getName(); } public Object getAdapter(Class key) { return null; } } class TreeParent extends TreeObject { private ArrayList children; public TreeParent(String name) { super(name); children = new ArrayList(); } public void addChild(TreeObject child) { children.add(child); child.setParent(this); } public void removeChild(TreeObject child) { children.remove(child); child.setParent(null); } public TreeObject [] getChildren() { return (TreeObject [])children.toArray(new TreeObject[children.size()]); } public boolean hasChildren() { return children.size()>0; } } class ViewContentProvider implements IStructuredContentProvider, ITreeContentProvider { private TreeParent invisibleRoot; public void inputChanged(Viewer v, Object oldInput, Object newInput) { } public void dispose() { } public Object[] getElements(Object parent) { if (parent.equals(getViewSite())) { if (invisibleRoot==null) initialize(); return getChildren(invisibleRoot); } return getChildren(parent); } public Object getParent(Object child) { if (child instanceof TreeObject) { return ((TreeObject)child).getParent(); } return null; } public Object [] getChildren(Object parent) { if (parent instanceof TreeParent) { return ((TreeParent)parent).getChildren(); } return new Object[0]; } public boolean hasChildren(Object parent) { if (parent instanceof TreeParent) return ((TreeParent)parent).hasChildren(); return false; } /* * We will set up a dummy model to initialize tree heararchy. * In a real code, you will connect to a real model and * expose its hierarchy. */ private void initialize() { TreeObject to1 = new TreeObject("Leaf 1"); TreeObject to2 = new TreeObject("Leaf 2"); TreeObject to3 = new TreeObject("Leaf 3"); TreeParent p1 = new TreeParent("Parent 1"); p1.addChild(to1); p1.addChild(to2); p1.addChild(to3); TreeObject to4 = new TreeObject("Leaf 4"); TreeParent p2 = new TreeParent("Parent 2"); p2.addChild(to4); TreeParent root = new TreeParent("Root"); root.addChild(p1); root.addChild(p2); invisibleRoot = new TreeParent(""); invisibleRoot.addChild(root); } } class ViewLabelProvider extends LabelProvider { public String getText(Object obj) { return obj.toString(); } public Image getImage(Object obj) { String imageKey = ISharedImages.IMG_OBJ_ELEMENT; if (obj instanceof TreeParent) imageKey = ISharedImages.IMG_OBJ_FOLDER; return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey); } } class NameSorter extends ViewerSorter { } /** * The constructor. */ public ASN1Viewer() { } /** * This is a callback that will allow us * to create the viewer and initialize it. */ public void createPartControl(Composite parent) { textdump = new Text(parent,SWT.BORDER | SWT.V_SCROLL); textdump.setText("ASN1 Dump Here"); getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(SecuredDocumentsView.ID,mylistener); /* viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); drillDownAdapter = new DrillDownAdapter(viewer); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setSorter(new NameSorter()); viewer.setInput(getViewSite()); // Create the help context id for the viewer's control PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "eParapher.viewer"); makeActions(); hookContextMenu(); hookDoubleClickAction(); contributeToActionBars();*/ } /* private void hookContextMenu() { MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.setRemoveAllWhenShown(true); menuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { ASN1Viewer.this.fillContextMenu(manager); } }); Menu menu = menuMgr.createContextMenu(viewer.getControl()); viewer.getControl().setMenu(menu); getSite().registerContextMenu(menuMgr, viewer); } private void contributeToActionBars() { IActionBars bars = getViewSite().getActionBars(); fillLocalPullDown(bars.getMenuManager()); fillLocalToolBar(bars.getToolBarManager()); } private void fillLocalPullDown(IMenuManager manager) { manager.add(action1); manager.add(new Separator()); manager.add(action2); } private void fillContextMenu(IMenuManager manager) { manager.add(action1); manager.add(action2); manager.add(new Separator()); drillDownAdapter.addNavigationActions(manager); // Other plug-ins can contribute there actions here manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); } private void fillLocalToolBar(IToolBarManager manager) { manager.add(action1); manager.add(action2); manager.add(new Separator()); drillDownAdapter.addNavigationActions(manager); } private void makeActions() { action1 = new Action() { public void run() { showMessage("Action 1 executed"); } }; action1.setText("Action 1"); action1.setToolTipText("Action 1 tooltip"); action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); action2 = new Action() { public void run() { showMessage("Action 2 executed"); } }; action2.setText("Action 2"); action2.setToolTipText("Action 2 tooltip"); action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); doubleClickAction = new Action() { public void run() { ISelection selection = viewer.getSelection(); Object obj = ((IStructuredSelection)selection).getFirstElement(); showMessage("Double-click detected on "+obj.toString()); } }; } private void hookDoubleClickAction() { viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { doubleClickAction.run(); } }); } private void showMessage(String message) { MessageDialog.openInformation( viewer.getControl().getShell(), "ASN1 Parser", message); } */ /** * Passing the focus request to the viewer's control. */ public void setFocus() { //viewer.getControl().setFocus(); } public void dispose() { //Remove the Selection listner ISelectionService s = getSite().getWorkbenchWindow().getSelectionService(); s.removeSelectionListener(mylistener); super.dispose(); } }