1 /*******************************************************************************
2  * Copyright (c) 2008,  Jay Rosenthal and others
3  *
4  *
5  * This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License 2.0
7  * which accompanies this distribution, and is available at
8  * https://www.eclipse.org/legal/epl-2.0/
9  *
10  * SPDX-License-Identifier: EPL-2.0
11  *
12  * Contributors:
13  *     Jay Rosenthal - initial API and implementation
14  *******************************************************************************/
15 
16 package org.eclipse.equinox.internal.provisional.security.ui;
17 
18 import java.security.cert.X509Certificate;
19 import java.text.DateFormat;
20 import org.eclipse.equinox.internal.security.ui.SecurityUIMsg;
21 import org.eclipse.equinox.internal.security.ui.wizard.CertificateViewer;
22 import org.eclipse.jface.dialogs.IDialogConstants;
23 import org.eclipse.jface.dialogs.TitleAreaDialog;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.layout.*;
28 import org.eclipse.swt.widgets.*;
29 
30 public class X509CertificateViewDialog extends TitleAreaDialog {
31 	private X509Certificate theCert;
32 	private static final DateFormat _df = DateFormat.getDateInstance(DateFormat.LONG);
33 	private X500PrincipalHelper nameHelper = new X500PrincipalHelper();
34 
35 	// We use the "bannerFont" for our bold font
36 	private static Font boldFont = JFaceResources.getBannerFont();
37 
X509CertificateViewDialog(Shell parentShell, X509Certificate cert)38 	public X509CertificateViewDialog(Shell parentShell, X509Certificate cert) {
39 		super(parentShell);
40 		this.theCert = cert;
41 	}
42 
43 	@Override
configureShell(Shell newShell)44 	protected void configureShell(Shell newShell) {
45 		super.configureShell(newShell);
46 		newShell.setText(SecurityUIMsg.CERTVIEW_SHELL_TITLE);
47 	}
48 
createContents(Composite parent)49 	protected Control createContents(Composite parent) {
50 		return super.createContents(parent);
51 	}
52 
createDialogArea(Composite parent)53 	protected Control createDialogArea(Composite parent) {
54 		nameHelper.setPrincipal(theCert.getSubjectX500Principal());
55 
56 		setTitle((nameHelper.getCN() != null ? nameHelper.getCN() : nameHelper.getOU()));
57 
58 		Composite composite = (Composite) super.createDialogArea(parent);
59 
60 		TabFolder tabFolder = new TabFolder(composite, SWT.BORDER);
61 		GridData bdata = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL);
62 		tabFolder.setLayoutData(bdata);
63 
64 		createBasicTab(tabFolder);
65 
66 		createAdvancedTab(tabFolder);
67 
68 		return composite;
69 	}
70 
createBasicTab(TabFolder tabFolder)71 	private void createBasicTab(TabFolder tabFolder) {
72 		String displayName = null;
73 		int labelIndent = 10;
74 		int dataIdent = 10;
75 
76 		TabItem basicTab = new TabItem(tabFolder, SWT.NULL);
77 		basicTab.setText(SecurityUIMsg.CERTVIEW_LABEL_BASIC);
78 		Composite basicTabComposite = new Composite(tabFolder, SWT.NONE);
79 
80 		GridLayout tabLayout = new GridLayout();
81 		tabLayout.numColumns = 2;
82 		basicTabComposite.setLayout(tabLayout);
83 
84 		Label issueToLabel = new Label(basicTabComposite, SWT.NONE);
85 		issueToLabel.setText(SecurityUIMsg.CERTPROP_X509_ISSUED_TO);
86 		issueToLabel.setFont(boldFont);
87 		configureLayout(issueToLabel, 2, 0, 0, 0);
88 
89 		// Display the RDNs of the Subject
90 		nameHelper.setPrincipal(theCert.getSubjectX500Principal());
91 
92 		Label CNLabel = new Label(basicTabComposite, SWT.NONE);
93 		CNLabel.setText(SecurityUIMsg.X500_LABEL_CN);
94 		configureLayout(CNLabel, 0, 0, labelIndent, 0);
95 
96 		Label subjectCN = new Label(basicTabComposite, SWT.NONE);
97 		displayName = nameHelper.getCN();
98 		subjectCN.setText((displayName != null ? displayName : SecurityUIMsg.CERTVAL_UNDEFINED));
99 		configureLayout(subjectCN, 0, 0, dataIdent, 0);
100 
101 		Label OLabel = new Label(basicTabComposite, SWT.NONE);
102 		OLabel.setText(SecurityUIMsg.X500_LABEL_O);
103 		configureLayout(OLabel, 0, 0, labelIndent, 0);
104 
105 		Label subjectO = new Label(basicTabComposite, SWT.NONE);
106 		displayName = nameHelper.getO();
107 		subjectO.setText((displayName != null ? displayName : SecurityUIMsg.CERTVAL_UNDEFINED));
108 		configureLayout(subjectO, 0, 0, dataIdent, 0);
109 
110 		Label OULabel = new Label(basicTabComposite, SWT.NONE);
111 		OULabel.setText(SecurityUIMsg.X500_LABEL_OU);
112 		configureLayout(OULabel, 0, 0, labelIndent, 0);
113 
114 		Label subjectOU = new Label(basicTabComposite, SWT.NONE);
115 		displayName = nameHelper.getOU();
116 		subjectOU.setText((displayName != null ? displayName : SecurityUIMsg.CERTVAL_UNDEFINED));
117 		configureLayout(subjectOU, 0, 0, dataIdent, 0);
118 
119 		Label issueByLabel = new Label(basicTabComposite, SWT.NONE);
120 		issueByLabel.setText(SecurityUIMsg.CERTPROP_X509_ISSUED_BY);
121 		configureLayout(issueByLabel, 2, 0, 0, 0);
122 		issueByLabel.setFont(boldFont);
123 
124 		// Display the RDNs of the Issuer
125 		nameHelper.setPrincipal(theCert.getIssuerX500Principal());
126 
127 		Label CNLabel2 = new Label(basicTabComposite, SWT.NONE);
128 		CNLabel2.setText(SecurityUIMsg.X500_LABEL_CN);
129 		configureLayout(CNLabel2, 0, 0, labelIndent, 0);
130 
131 		Label issuerCN = new Label(basicTabComposite, SWT.NONE);
132 		displayName = nameHelper.getCN();
133 		issuerCN.setText((displayName != null ? displayName : SecurityUIMsg.CERTVAL_UNDEFINED));
134 		configureLayout(issuerCN, 0, 0, dataIdent, 0);
135 
136 		Label OLabel2 = new Label(basicTabComposite, SWT.NONE);
137 		OLabel2.setText(SecurityUIMsg.X500_LABEL_O);
138 		configureLayout(OLabel2, 0, 0, labelIndent, 0);
139 
140 		Label issuerO = new Label(basicTabComposite, SWT.NONE);
141 		displayName = nameHelper.getO();
142 		issuerO.setText((displayName != null ? displayName : SecurityUIMsg.CERTVAL_UNDEFINED));
143 		configureLayout(issuerO, 0, 0, dataIdent, 0);
144 
145 		Label OULabel2 = new Label(basicTabComposite, SWT.NONE);
146 		OULabel2.setText(SecurityUIMsg.X500_LABEL_OU);
147 		configureLayout(OULabel2, 0, 0, labelIndent, 0);
148 
149 		Label issuerOU = new Label(basicTabComposite, SWT.NONE);
150 		displayName = nameHelper.getOU();
151 		issuerOU.setText((displayName != null ? displayName : SecurityUIMsg.CERTVAL_UNDEFINED));
152 		configureLayout(issuerOU, 0, 0, dataIdent, 0);
153 
154 		Label datesLabel = new Label(basicTabComposite, SWT.NONE);
155 		datesLabel.setText(SecurityUIMsg.CERTVIEW_LABEL_VALIDITY_DATES);
156 		configureLayout(datesLabel, 2, 0, 0, 0);
157 		datesLabel.setFont(boldFont);
158 
159 		Label validFrom = new Label(basicTabComposite, SWT.NONE);
160 		validFrom.setText(SecurityUIMsg.CERTPROP_X509_VALID_FROM);
161 		configureLayout(validFrom, 0, 0, labelIndent, 0);
162 
163 		Label fromDate = new Label(basicTabComposite, SWT.NONE);
164 		fromDate.setText(_df.format(theCert.getNotBefore()));
165 		configureLayout(fromDate, 0, 0, dataIdent, 0);
166 
167 		Label validTo = new Label(basicTabComposite, SWT.NONE);
168 		validTo.setText(SecurityUIMsg.CERTPROP_X509_VALID_TO);
169 		configureLayout(validTo, 0, 0, labelIndent, 0);
170 
171 		Label toDate = new Label(basicTabComposite, SWT.NONE);
172 		toDate.setText(_df.format(theCert.getNotAfter()));
173 		configureLayout(toDate, 0, 0, dataIdent, 0);
174 
175 		basicTab.setControl(basicTabComposite);
176 	}
177 
configureLayout(Control c, int horizontalSpan, int verticalSpan, int horizontalIndent, int vertIndent)178 	protected static void configureLayout(Control c, int horizontalSpan, int verticalSpan, int horizontalIndent, int vertIndent) {
179 		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_CENTER);
180 
181 		gd.horizontalSpan = horizontalSpan;
182 		gd.verticalSpan = verticalSpan;
183 		gd.horizontalIndent = horizontalIndent;
184 		gd.verticalIndent = vertIndent;
185 
186 		c.setLayoutData(gd);
187 
188 	}
189 
createAdvancedTab(final TabFolder tabFolder)190 	private void createAdvancedTab(final TabFolder tabFolder) {
191 		TabItem advancedTab = new TabItem(tabFolder, SWT.NULL);
192 		advancedTab.setText(SecurityUIMsg.CERTVIEW_LABEL_DETAILS);
193 		Composite advTabComposite = new Composite(tabFolder, SWT.NONE);
194 		advTabComposite.setLayout(new FillLayout(SWT.VERTICAL));
195 
196 		CertificateViewer certViewer = new CertificateViewer(advTabComposite);
197 		certViewer.setCertificate(theCert);
198 		advancedTab.setControl(advTabComposite);
199 	}
200 
setShellStyle(int newShellStyle)201 	protected void setShellStyle(int newShellStyle) {
202 
203 		super.setShellStyle(newShellStyle | SWT.RESIZE | SWT.DIALOG_TRIM);
204 	}
205 
createButtonsForButtonBar(Composite parent)206 	protected void createButtonsForButtonBar(Composite parent) {
207 		// The default has only a "Close" button, but it returns the CANCEL Id
208 		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CLOSE_LABEL, true);
209 
210 	}
211 
212 }
213