1 package org.eparapher.rcp.views;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.security.cert.CRLException;
7 import java.security.cert.CertificateEncodingException;
8 import java.util.ArrayList;
9
10 import org.apache.log4j.Logger;
11 import org.bouncycastle.asn1.ASN1InputStream;
12 import org.bouncycastle.asn1.DERApplicationSpecific;
13 import org.bouncycastle.asn1.DEREnumerated;
14 import org.bouncycastle.asn1.DERObject;
15 import org.bouncycastle.asn1.DERUnknownTag;
16 import org.bouncycastle.asn1.pkcs.ContentInfo;
17 import org.bouncycastle.asn1.util.ASN1Dump;
18 import org.bouncycastle.jce.PKCS10CertificationRequest;
19 import org.bouncycastle.jce.provider.X509CRLObject;
20 import org.bouncycastle.jce.provider.X509CertificateObject;
21 import org.bouncycastle.openssl.PEMReader;
22 import org.bouncycastle.x509.X509AttributeCertificate;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredContentProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.ITreeContentProvider;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.viewers.Viewer;
30 import org.eclipse.jface.viewers.ViewerSorter;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.ui.ISelectionListener;
36 import org.eclipse.ui.ISelectionService;
37 import org.eclipse.ui.ISharedImages;
38 import org.eclipse.ui.IWorkbenchPart;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.part.ViewPart;
41 import org.eparapher.core.tools.FileUtil;
42 import org.eparapher.rcp.views.documents.SecuredDocumentsView;
43
44
45
46
47
48
49
50
51
52 public class ASN1Viewer extends ViewPart {
53
54 public static final String ID = "org.eparapher.rcp.views.ASN1Viewer";
55 private static Logger log = Logger.getLogger(ASN1Viewer.class);
56
57 private Text textdump;
58
59
60
61
62
63 private ISelectionListener mylistener = new ISelectionListener() {
64 public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
65
66 if (sourcepart instanceof SecuredDocumentsView &&
67 selection instanceof IStructuredSelection) {
68 IStructuredSelection is = (IStructuredSelection) selection;
69 textdump.setText("Please select only one item in the document view");
70 if (is.size()==1) {
71 SecuredDocumentsView.TreeObject to = (SecuredDocumentsView.TreeObject) is.getFirstElement();
72 if (to instanceof SecuredDocumentsView.TreeParent )
73 textdump.setText("directory doesn't have an ASN1 structure.");
74 else {
75 String path = to.getFilePath();
76 ASN1InputStream asn1is = null;
77 DERObject der = null;
78
79 try {
80 PEMReader pr = new PEMReader(new FileReader(path));
81 Object obj = pr.readObject();
82 if (obj instanceof X509CertificateObject)
83 asn1is = new ASN1InputStream(((X509CertificateObject)obj).getEncoded());
84 if (obj instanceof X509CRLObject)
85 asn1is = new ASN1InputStream(((X509CRLObject)obj).getEncoded());
86 if (obj instanceof PKCS10CertificationRequest)
87 asn1is = new ASN1InputStream(((PKCS10CertificationRequest)obj).getEncoded());
88 if (obj instanceof ContentInfo)
89 asn1is = new ASN1InputStream(((ContentInfo)obj).getEncoded());
90 if (obj instanceof X509AttributeCertificate)
91 asn1is = new ASN1InputStream(((X509AttributeCertificate)obj).getEncoded());
92
93
94 if (asn1is!=null)
95 der = asn1is.readObject();
96 } catch (FileNotFoundException e) {
97 log.debug(path + " is not found");
98 } catch (IOException e) {
99 log.debug(path + " is not an ASN1 structure");
100 } catch (CertificateEncodingException e) {
101 log.debug(path + " is not a valid X509 structure");
102 } catch (CRLException e) {
103 log.debug(path + " is not a valid X509 CRL structure");
104 }
105
106 if (asn1is==null) {
107 try {
108 asn1is = new ASN1InputStream(FileUtil.readFile(path));
109 der = asn1is.readObject();
110 } catch (IOException e) {
111 log.debug(path + " is not a valid ASN1 structure");
112 }
113 }
114 if ( der!=null &&
115 !( der instanceof DERUnknownTag) &&
116 !( der instanceof DEREnumerated) &&
117 !( der instanceof DERApplicationSpecific))
118 textdump.setText(ASN1Dump.dumpAsString(der,true));
119 else
120 textdump.setText(path + " has not been detected as a valid ASN1 object/structure");
121 }
122 }
123 }
124 }
125 };
126
127
128
129
130
131
132
133
134
135
136
137 class TreeObject implements IAdaptable {
138 private String name;
139 private TreeParent parent;
140
141 public TreeObject(String name) {
142 this.name = name;
143 }
144 public String getName() {
145 return name;
146 }
147 public void setParent(TreeParent parent) {
148 this.parent = parent;
149 }
150 public TreeParent getParent() {
151 return parent;
152 }
153 public String toString() {
154 return getName();
155 }
156 public Object getAdapter(Class key) {
157 return null;
158 }
159 }
160
161 class TreeParent extends TreeObject {
162 private ArrayList children;
163 public TreeParent(String name) {
164 super(name);
165 children = new ArrayList();
166 }
167 public void addChild(TreeObject child) {
168 children.add(child);
169 child.setParent(this);
170 }
171 public void removeChild(TreeObject child) {
172 children.remove(child);
173 child.setParent(null);
174 }
175 public TreeObject [] getChildren() {
176 return (TreeObject [])children.toArray(new TreeObject[children.size()]);
177 }
178 public boolean hasChildren() {
179 return children.size()>0;
180 }
181 }
182
183 class ViewContentProvider implements IStructuredContentProvider,
184 ITreeContentProvider {
185 private TreeParent invisibleRoot;
186
187 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
188 }
189 public void dispose() {
190 }
191 public Object[] getElements(Object parent) {
192 if (parent.equals(getViewSite())) {
193 if (invisibleRoot==null) initialize();
194 return getChildren(invisibleRoot);
195 }
196 return getChildren(parent);
197 }
198 public Object getParent(Object child) {
199 if (child instanceof TreeObject) {
200 return ((TreeObject)child).getParent();
201 }
202 return null;
203 }
204 public Object [] getChildren(Object parent) {
205 if (parent instanceof TreeParent) {
206 return ((TreeParent)parent).getChildren();
207 }
208 return new Object[0];
209 }
210 public boolean hasChildren(Object parent) {
211 if (parent instanceof TreeParent)
212 return ((TreeParent)parent).hasChildren();
213 return false;
214 }
215
216
217
218
219
220 private void initialize() {
221 TreeObject to1 = new TreeObject("Leaf 1");
222 TreeObject to2 = new TreeObject("Leaf 2");
223 TreeObject to3 = new TreeObject("Leaf 3");
224 TreeParent p1 = new TreeParent("Parent 1");
225 p1.addChild(to1);
226 p1.addChild(to2);
227 p1.addChild(to3);
228
229 TreeObject to4 = new TreeObject("Leaf 4");
230 TreeParent p2 = new TreeParent("Parent 2");
231 p2.addChild(to4);
232
233 TreeParent root = new TreeParent("Root");
234 root.addChild(p1);
235 root.addChild(p2);
236
237 invisibleRoot = new TreeParent("");
238 invisibleRoot.addChild(root);
239 }
240 }
241 class ViewLabelProvider extends LabelProvider {
242
243 public String getText(Object obj) {
244 return obj.toString();
245 }
246 public Image getImage(Object obj) {
247 String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
248 if (obj instanceof TreeParent)
249 imageKey = ISharedImages.IMG_OBJ_FOLDER;
250 return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
251 }
252 }
253 class NameSorter extends ViewerSorter {
254 }
255
256
257
258
259 public ASN1Viewer() {
260 }
261
262
263
264
265
266 public void createPartControl(Composite parent) {
267 textdump = new Text(parent,SWT.BORDER | SWT.V_SCROLL);
268 textdump.setText("ASN1 Dump Here");
269 getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(SecuredDocumentsView.ID,mylistener);
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375 public void setFocus() {
376
377 }
378 public void dispose() {
379
380 ISelectionService s = getSite().getWorkbenchWindow().getSelectionService();
381 s.removeSelectionListener(mylistener);
382 super.dispose();
383 }
384
385 }