Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6d98b34c16400aa56d7942cbe38f5842c3fbdcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*******************************************************************************
 * Copyright (c) 2007, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 474273
 *******************************************************************************/

package org.eclipse.ui.internal.about;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.StringTokenizer;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobFunction;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.osgi.signedcontent.SignedContent;
import org.eclipse.osgi.signedcontent.SignedContentFactory;
import org.eclipse.osgi.signedcontent.SignerInfo;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.statushandlers.StatusManager;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * @since 3.3
 *
 */
public class BundleSigningInfo {

	private Composite composite;
	private Text date;
	private StyledText certificate;
	private AboutBundleData data;

	public void setData(AboutBundleData data) {
		this.data = data;
		startJobs();
	}

	public Control createContents(Composite parent) {

		composite = new Composite(parent, SWT.BORDER);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		composite.setLayout(layout);

		// date
		{
			Label label = new Label(composite, SWT.NONE);
			label.setText(WorkbenchMessages.BundleSigningTray_Signing_Date);
			GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
			date = new Text(composite, SWT.READ_ONLY);
			GC gc = new GC(date);
			gc.setFont(JFaceResources.getDialogFont());
			Point size = gc.stringExtent(DateFormat.getDateTimeInstance().format(new Date()));
			data.widthHint = size.x;
			gc.dispose();
			date.setText(WorkbenchMessages.BundleSigningTray_Working);
			date.setLayoutData(data);
		}
		// signer
		{
			Label label = new Label(composite, SWT.NONE);
			label.setText(WorkbenchMessages.BundleSigningTray_Signing_Certificate);
			GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false);
			data.horizontalSpan = 2;
			data = new GridData(SWT.FILL, SWT.FILL, true, true);
			data.horizontalSpan = 2;
			certificate = new StyledText(composite, SWT.READ_ONLY | SWT.MULTI | SWT.WRAP);
			certificate.setText(WorkbenchMessages.BundleSigningTray_Working);
			certificate.setLayoutData(data);
		}
		Dialog.applyDialogFont(composite);

		startJobs(); // start the jobs that will prime the content
		return composite;
	}

	/**
	 *
	 */
	private void startJobs() {
		if (!isOpen())
			return;
		certificate.setText(WorkbenchMessages.BundleSigningTray_Working);
		date.setText(WorkbenchMessages.BundleSigningTray_Working);
		final BundleContext bundleContext = WorkbenchPlugin.getDefault().getBundleContext();
		final ServiceReference<SignedContentFactory> factoryRef = bundleContext
				.getServiceReference(SignedContentFactory.class);
		if (factoryRef == null) {
			StatusManager.getManager().handle(new Status(IStatus.WARNING, WorkbenchPlugin.PI_WORKBENCH,
					WorkbenchMessages.BundleSigningTray_Cant_Find_Service), StatusManager.LOG);
			return;
		}

		final SignedContentFactory contentFactory = bundleContext.getService(factoryRef);
		if (contentFactory == null) {
			StatusManager.getManager().handle(new Status(IStatus.WARNING, WorkbenchPlugin.PI_WORKBENCH,
					WorkbenchMessages.BundleSigningTray_Cant_Find_Service), StatusManager.LOG);
			return;
		}

		final AboutBundleData myData = data;
		final Job signerJob = Job.create(
				NLS.bind(WorkbenchMessages.BundleSigningTray_Determine_Signer_For, myData.getId()),
				(IJobFunction) monitor -> {
					try {
						if (myData != data)
							return Status.OK_STATUS;
						SignedContent signedContent = contentFactory.getSignedContent(myData.getBundle());
						if (myData != data)
							return Status.OK_STATUS;
						SignerInfo[] signers = signedContent.getSignerInfos();
						final String signerText, dateText;
						if (!isOpen() && BundleSigningInfo.this.data == myData)
							return Status.OK_STATUS;

						if (signers.length == 0) {
							signerText = WorkbenchMessages.BundleSigningTray_Unsigned;
							dateText = WorkbenchMessages.BundleSigningTray_Unsigned;
						} else {
							Properties[] certs = parseCerts(signers[0].getCertificateChain());
							if (certs.length == 0)
								signerText = WorkbenchMessages.BundleSigningTray_Unknown;
							else {
								StringBuilder buffer = new StringBuilder();
								for (Iterator<Entry<Object, Object>> i = certs[0].entrySet().iterator(); i.hasNext();) {
									Entry<Object, Object> entry = i.next();
									buffer.append(entry.getKey());
									buffer.append('=');
									buffer.append(entry.getValue());
									if (i.hasNext())
										buffer.append('\n');
								}
								signerText = buffer.toString();
							}

							Date signDate = signedContent.getSigningTime(signers[0]);
							if (signDate != null)
								dateText = DateFormat.getDateTimeInstance().format(signDate);
							else
								dateText = WorkbenchMessages.BundleSigningTray_Unknown;
						}

						PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {
							// check to see if the tray is still visible
							// and if
							// we're still looking at the same item
							if (!isOpen() && BundleSigningInfo.this.data != myData)
								return;
							certificate.setText(signerText);
							date.setText(dateText);
						});

					} catch (IOException e1) {
						return new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, e1.getMessage(), e1);
					} catch (GeneralSecurityException e2) {
						return new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, e2.getMessage(), e2);
					}
					return Status.OK_STATUS;
				});
		signerJob.setSystem(true);
		signerJob.belongsTo(signerJob);
		signerJob.schedule();

		Job cleanup = Job.create(WorkbenchMessages.BundleSigningTray_Unget_Signing_Service, (IJobFunction) monitor -> {
			try {
				Job.getJobManager().join(signerJob, monitor);
			} catch (OperationCanceledException e1) {
			} catch (InterruptedException e2) {
			}
			bundleContext.ungetService(factoryRef);
			return Status.OK_STATUS;
		});
		cleanup.setSystem(true);
		cleanup.schedule();

	}

	/**
	 *
	 */
	private boolean isOpen() {
		return certificate != null && !certificate.isDisposed();
	}

	private Properties[] parseCerts(Certificate[] chain) {
		List<Properties> certs = new ArrayList<>(chain.length);
		for (int i = 0; i < chain.length; i++) {
			if (!(chain[i] instanceof X509Certificate))
				continue;
			Properties cert = parseCert(((X509Certificate) chain[i]).getSubjectDN().getName());
			if (cert != null)
				certs.add(cert);
		}
		return certs.toArray(new Properties[certs.size()]);

	}

	/**
	 * @param certString
	 * @return
	 */
	private Properties parseCert(String certString) {
		StringTokenizer toker = new StringTokenizer(certString, ","); //$NON-NLS-1$
		Properties cert = new Properties();
		while (toker.hasMoreTokens()) {
			String pair = toker.nextToken();
			int idx = pair.indexOf('=');
			if (idx > 0 && idx < pair.length() - 2) {
				String key = pair.substring(0, idx).trim();
				String value = pair.substring(idx + 1).trim();
				if (value.length() > 2) {
					if (value.charAt(0) == '\"')
						value = value.substring(1);

					if (value.charAt(value.length() - 1) == '\"')
						value = value.substring(0, value.length() - 1);
				}
				cert.setProperty(key, value);
			}
		}
		return cert;
	}

	public void dispose() {
		composite.dispose();
	}
}

Back to the top