1 package org.eparapher.rcp.dialog;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.IOException;
6 import java.security.InvalidKeyException;
7 import java.security.NoSuchAlgorithmException;
8 import java.security.NoSuchProviderException;
9 import java.security.SignatureException;
10 import java.security.cert.CertificateException;
11
12 import org.apache.log4j.Logger;
13 import org.bouncycastle.asn1.pkcs.CertificationRequestInfo;
14 import org.bouncycastle.jce.PKCS10CertificationRequest;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.layout.FillLayout;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.Text;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.forms.widgets.Form;
28 import org.eclipse.ui.forms.widgets.FormText;
29 import org.eclipse.ui.forms.widgets.FormToolkit;
30 import org.eclipse.ui.forms.widgets.Section;
31 import org.eclipse.ui.forms.widgets.TableWrapData;
32 import org.eclipse.ui.forms.widgets.TableWrapLayout;
33 import org.eparapher.rcp.tools.GUIIcons;
34 import org.eparapher.core.crypto.cert.CertificateInfo;
35 import org.eparapher.core.crypto.cert.X509Util;
36 import org.eparapher.core.tools.FileUtil;
37
38
39
40
41
42
43
44
45
46
47
48 public class CertificateRequestViewerDialog extends Dialog {
49
50 private static Logger log = Logger.getLogger(CertificateRequestViewerDialog.class);
51
52 private String csrfile;
53 private String base64CSR;
54 private PKCS10CertificationRequest pkcs10csr;
55
56 private String title;
57
58 private FormToolkit toolkit;
59 private Form form;
60
61 private Text CSRFieldValue;
62
63
64 public CertificateRequestViewerDialog(String fileName) {
65 super(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
66 this.setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | getDefaultOrientation());
67
68 if (!new File(fileName).exists())
69 log.error("CSR File does not exists");
70 this.csrfile = fileName;
71
72 ByteArrayInputStream bais;
73 try {
74 this.base64CSR = new String(FileUtil.readFile(fileName));
75 pkcs10csr = X509Util.getCSRFromPEM(fileName);
76 } catch (IOException e) {
77 log.error(e);
78 } catch (CertificateException e) {
79 log.error(e);
80 }
81 }
82
83 protected Control createDialogArea(Composite parent) {
84
85 Composite container = (Composite) super.createDialogArea(parent);
86 container.setLayout(new FillLayout());
87
88
89 toolkit = new FormToolkit(container.getDisplay());
90 form = toolkit.createForm(container);
91 form.getBody().setLayout(new GridLayout());
92 form.setText("Certificate Signing Request");
93
94 toolkit.decorateFormHeading(form);
95 form.setImage(GUIIcons.DLG_ICON_VALID_CERT);
96
97
98 showCSRInfo();
99
100
101 showBase64CSR();
102
103 return container;
104 }
105
106 protected void createButtonsForButtonBar(Composite parent) {
107 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
108 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
109 }
110
111 protected Point getInitialSize() {
112 return new Point(600, 600);
113 }
114
115 private void showCSRInfo() {
116 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR| Section.TWISTIE|Section.EXPANDED);
117 section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL ));
118
119 section.setText("Certificate request content");
120 Composite sectionClient = toolkit.createComposite(section);
121 sectionClient.setLayout(new TableWrapLayout());
122
123 CertificationRequestInfo csrinfo = pkcs10csr.getCertificationRequestInfo();
124 String text = null;
125 try {
126 text = "<form>";
127 text += "<li>Integrity : <b>"+(pkcs10csr.verify()?"verified":"KO")+"</b></li>";
128 text += "<li>Subject : <b>" + csrinfo.getSubject() + "</b></li>";
129 text += "<li>Keypair : <b>" + CertificateInfo.getPublicKeyInfo( pkcs10csr.getPublicKey() ) + "</b></li>";
130
131
132 text += "</form>";
133 } catch (InvalidKeyException e) {
134 log.error("Error occured while parsing CSR : "+csrfile,e);
135 } catch (NoSuchAlgorithmException e) {
136 log.error("Error occured while parsing CSR : "+csrfile,e);
137 } catch (NoSuchProviderException e) {
138 log.error("Error occured while parsing CSR : "+csrfile,e);
139 } catch (SignatureException e) {
140 log.error("Error occured when verifying signature of CSR : "+csrfile,e);
141 }
142
143 FormText formText = toolkit.createFormText(sectionClient, true);
144 formText.setText(text, true, false);
145 formText.setLayoutData(new TableWrapData(TableWrapData.FILL));
146
147 section.setClient(sectionClient);
148
149 }
150
151 private void showBase64CSR() {
152 Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR| Section.TWISTIE|Section.EXPANDED);
153 section.setText("PKCS10, base64 encoded");
154 section.setDescription("copy/paste the complete CSR from here");
155
156 section.setLayoutData(new GridData(GridData.FILL_BOTH));
157
158 Composite sectionClient = toolkit.createComposite(section);
159 sectionClient.setLayout(new GridLayout(1,true));
160 sectionClient.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
161
162 CSRFieldValue = toolkit.createText(sectionClient, "", SWT.BORDER|SWT.READ_ONLY|SWT.MULTI);
163 GridData gd = new GridData(GridData.FILL_BOTH);
164
165 CSRFieldValue.setLayoutData(gd);
166 CSRFieldValue.setText(base64CSR);
167
168 section.setClient(sectionClient);
169 }
170
171
172
173
174 public void dispose() {
175 toolkit.dispose();
176 }
177
178
179
180
181 protected void configureShell(Shell shell) {
182 super.configureShell(shell);
183 if (title != null) {
184 shell.setText(title);
185 }
186 shell.setImage(GUIIcons.CERTIFICATE_ICON_IMAGE);
187 }
188
189 }