package org.eparapher.rcp.dialog;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import org.apache.log4j.Logger;
import org.bouncycastle.asn1.pkcs.CertificationRequestInfo;
import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eparapher.rcp.tools.GUIIcons;
import org.eparapher.core.crypto.cert.CertificateInfo;
import org.eparapher.core.crypto.cert.X509Util;
import org.eparapher.core.tools.FileUtil;
/** This is a Dialog that parse and display X.509 certificate fields, extentions and properties
*
Based On :
*
* - http://www.eclipse.org/articles/Article-Forms/article.html
* - http://www.eclipse.org/articles/article.php?file=Article-Forms33/index.html
*
*
* @author Arnault MICHEL
*
*/
public class CertificateRequestViewerDialog extends Dialog {
private static Logger log = Logger.getLogger(CertificateRequestViewerDialog.class);
private String csrfile;
private String base64CSR;
private PKCS10CertificationRequest pkcs10csr;
private String title;
private FormToolkit toolkit;
private Form form;
private Text CSRFieldValue;
//private FormText CSRFieldValue;
public CertificateRequestViewerDialog(String fileName) {
super(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
this.setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | getDefaultOrientation());
if (!new File(fileName).exists())
log.error("CSR File does not exists");
this.csrfile = fileName;
ByteArrayInputStream bais;
try {
this.base64CSR = new String(FileUtil.readFile(fileName));
pkcs10csr = X509Util.getCSRFromPEM(fileName);
} catch (IOException e) {
log.error(e);
} catch (CertificateException e) {
log.error(e);
}
}
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
container.setLayout(new FillLayout());
//title
toolkit = new FormToolkit(container.getDisplay());
form = toolkit.createForm(container);
form.getBody().setLayout(new GridLayout());
form.setText("Certificate Signing Request");
toolkit.decorateFormHeading(form);
form.setImage(GUIIcons.DLG_ICON_VALID_CERT);
//CSR Info
showCSRInfo();
//CSR
showBase64CSR();
return container;
}
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
protected Point getInitialSize() {
return new Point(600, 600);
}
private void showCSRInfo() {
Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR| Section.TWISTIE|Section.EXPANDED);
section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL ));
//section.addExpansionListener(new ExpAdaptor());
section.setText("Certificate request content");
Composite sectionClient = toolkit.createComposite(section);
sectionClient.setLayout(new TableWrapLayout());
CertificationRequestInfo csrinfo = pkcs10csr.getCertificationRequestInfo();
String text = null;
try {
text = "";
} catch (InvalidKeyException e) {
log.error("Error occured while parsing CSR : "+csrfile,e);
} catch (NoSuchAlgorithmException e) {
log.error("Error occured while parsing CSR : "+csrfile,e);
} catch (NoSuchProviderException e) {
log.error("Error occured while parsing CSR : "+csrfile,e);
} catch (SignatureException e) {
log.error("Error occured when verifying signature of CSR : "+csrfile,e);
}
FormText formText = toolkit.createFormText(sectionClient, true);
formText.setText(text, true, false);
formText.setLayoutData(new TableWrapData(TableWrapData.FILL));
section.setClient(sectionClient);
}
private void showBase64CSR() {
Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR| Section.TWISTIE|Section.EXPANDED);
section.setText("PKCS10, base64 encoded");
section.setDescription("copy/paste the complete CSR from here");
//section.addExpansionListener(new ExpAdaptor());
section.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite sectionClient = toolkit.createComposite(section);
sectionClient.setLayout(new GridLayout(1,true));
sectionClient.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
CSRFieldValue = toolkit.createText(sectionClient, "", SWT.BORDER|SWT.READ_ONLY|SWT.MULTI);
GridData gd = new GridData(GridData.FILL_BOTH);
//gd.heightHint = 50;
CSRFieldValue.setLayoutData(gd);
CSRFieldValue.setText(base64CSR);
section.setClient(sectionClient);
}
/**
* Disposes the toolkit
*/
public void dispose() {
toolkit.dispose();
}
/**
* Setting title
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);
if (title != null) {
shell.setText(title);
}
shell.setImage(GUIIcons.CERTIFICATE_ICON_IMAGE);
}
}