Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b0bbf338f0409749ecb1d8e373e0cee8e9700615 (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
266
267
268
269
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
/*******************************************************************************
 * Copyright (c) 2021 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
 *******************************************************************************/
package org.eclipse.osgi.internal.signedcontent;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.security.CodeSigner;
import java.security.Timestamp;
import java.security.cert.Certificate;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.zip.ZipFile;
import org.eclipse.osgi.internal.debug.Debug;
import org.eclipse.osgi.signedcontent.InvalidContentException;
import org.eclipse.osgi.signedcontent.SignedContent;
import org.eclipse.osgi.signedcontent.SignedContentEntry;
import org.eclipse.osgi.signedcontent.SignerInfo;
import org.eclipse.osgi.storage.bundlefile.BundleFile;
import org.eclipse.osgi.storage.bundlefile.DirBundleFile;
import org.eclipse.osgi.storage.bundlefile.ZipBundleFile;

public class SignedContentFromBundleFile implements SignedContent {
	static abstract class BaseSignerInfo implements SignerInfo {
		private volatile Certificate trustAnchor = null;
		@Override
		public Certificate getTrustAnchor() {
			return trustAnchor;
		}

		@Override
		public boolean isTrusted() {
			return trustAnchor != null;
		}

		@Deprecated
		@Override
		public String getMessageDigestAlgorithm() {
			return "unknown"; //$NON-NLS-1$
		}

		void setTrustAnchor(Certificate anchor) {
			this.trustAnchor = anchor;
		}
	}

	static class TimestampSignerInfo extends BaseSignerInfo {
		private final Timestamp timestamp;

		public TimestampSignerInfo(Timestamp timestamp) {
			this.timestamp = timestamp;
		}

		@Override
		public Certificate[] getCertificateChain() {
			return timestamp.getSignerCertPath().getCertificates().toArray(new Certificate[0]);
		}

		Date getTimestamp() {
			return timestamp.getTimestamp();
		}
	}

	static class CodeSignerInfo extends BaseSignerInfo {
		private final CodeSigner codeSigner;
		private final TimestampSignerInfo timestamp;

		public CodeSignerInfo(CodeSigner codeSigner) {
			this.codeSigner = codeSigner;
			Timestamp ts = codeSigner.getTimestamp();
			this.timestamp = ts == null ? null : new TimestampSignerInfo(ts);
		}

		@Override
		public Certificate[] getCertificateChain() {
			return codeSigner.getSignerCertPath().getCertificates().toArray(new Certificate[0]);
		}

		TimestampSignerInfo getTSASignerInfo() {
			return timestamp;
		}
	}

	static class CodeSignerEntry implements SignedContentEntry {
		private final String name;
		private final List<CodeSignerInfo> signerInfos;

		public CodeSignerEntry(List<CodeSignerInfo> signerInfos, String name) {
			this.name = name;
			this.signerInfos = signerInfos;
		}

		@Override
		public String getName() {
			return name;
		}

		@Override
		public SignerInfo[] getSignerInfos() {
			return signerInfos.toArray(new SignerInfo[0]);
		}

		@Override
		public boolean isSigned() {
			return !signerInfos.isEmpty();
		}

		@Override
		public void verify() throws IOException, InvalidContentException {
			// already verified
		}
	}

	static class CorruptEntry implements SignedContentEntry {
		final InvalidContentException verifyError;
		final String name;

		@Override
		public String getName() {
			return name;
		}

		@Override
		public SignerInfo[] getSignerInfos() {
			return new SignerInfo[0];
		}

		@Override
		public boolean isSigned() {
			return false;
		}

		@Override
		public void verify() throws IOException, InvalidContentException {
			throw verifyError;
		}

		public CorruptEntry(InvalidContentException verifyError, String name) {
			super();
			this.verifyError = verifyError;
			this.name = name;
		}

	}

	private final List<CodeSignerInfo> signerInfos = new ArrayList<>();
	private final Map<String, SignedContentEntry> signedEntries;

	public SignedContentFromBundleFile(BundleFile bundleFile) throws IOException {
		signedEntries = getSignedEntries(() -> {
			try {
				return getJarInputStream(bundleFile);
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, () -> bundleFile, signerInfos);
	}

	public SignedContentFromBundleFile(File bundleFile, Debug debug) throws IOException {
		DirBundleFile tmpDirBundleFile = null;
		if (bundleFile.isDirectory()) {
			try {
				tmpDirBundleFile = new DirBundleFile(bundleFile, false);
			} catch (IOException e) {
				// ignore and move on
			}
		}
		DirBundleFile dirBundleFile = tmpDirBundleFile;
		signedEntries = getSignedEntries(() -> {
			try {
				if (dirBundleFile != null) {
					return getJarInputStream(dirBundleFile);
				}
				return new FileInputStream(bundleFile);
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, () -> {
			try {
				if (dirBundleFile != null) {
					return dirBundleFile;
				}
				// Make sure we have a ZipFile first, this will throw an IOException if not
				// valid.
				// Use SecureAction because it gives better errors about the path on exceptions
				ZipFile temp = SignedBundleHook.secureAction.getZipFile(bundleFile, false);
				temp.close();
				return new ZipBundleFile(bundleFile, null, null, debug, false);
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, signerInfos);
	}

	private static Map<String, SignedContentEntry> getSignedEntries(Supplier<InputStream> input,
			Supplier<BundleFile> bundleFile, List<CodeSignerInfo> signerInfos) throws IOException {
		Map<CodeSigner, CodeSignerInfo> codeSigners = new HashMap<>();
		Map<String, SignedContentEntry> signedEntries = new LinkedHashMap<>();
		try (JarInputStream jarInput = new JarInputStream(input.get())) {

			for (JarEntry entry = jarInput.getNextJarEntry(); entry != null; entry = jarInput.getNextJarEntry()) {
				// drain the entry so we can get the code signer
				try {
					for (byte[] drain = new byte[4096]; jarInput.read(drain, 0, drain.length) != -1;) {
						// nothing
					}
					CodeSigner[] signers = entry.getCodeSigners();
					if (signers != null) {
						List<CodeSignerInfo> entryInfos = new ArrayList<>(signers.length);
						for (CodeSigner codeSigner : signers) {
							CodeSignerInfo info = codeSigners.computeIfAbsent(codeSigner, CodeSignerInfo::new);
							entryInfos.add(info);
						}
						CodeSignerEntry signedEntry = new CodeSignerEntry(entryInfos, entry.getName());
						signedEntries.put(entry.getName(), signedEntry);
					}
				} catch (SecurityException | IOException e) {
					// assume corruption
					signedEntries.put(entry.getName(),
							new CorruptEntry(new InvalidContentException(entry.getName(), e), entry.getName()));
				}
			}
		} catch (SecurityException e) {
			Enumeration<String> paths = bundleFile.get().getEntryPaths("", true); //$NON-NLS-1$
			while (paths.hasMoreElements()) {
				String path = paths.nextElement();
				if (!path.endsWith("/") && !signedEntries.containsKey(path)) { //$NON-NLS-1$
					signedEntries.put(path, new CorruptEntry(new InvalidContentException(path, e), path));
				}
			}
		} catch (UncheckedIOException e) {
			throw e.getCause();
		}
		signerInfos.addAll(codeSigners.values());
		return signedEntries;
	}

	private static InputStream getJarInputStream(BundleFile bundleFile) throws IOException {
		File f = bundleFile.getBaseFile();
		if (f == null || f.isDirectory()) {
			return new BundleToJarInputStream(bundleFile);
		}
		return new FileInputStream(f);
	}

	@Override
	public SignedContentEntry[] getSignedEntries() {
		return signedEntries.values().toArray(new SignedContentEntry[0]);
	}

	@Override
	public SignedContentEntry getSignedEntry(String name) {
		return signedEntries.get(name);
	}

	@Override
	public SignerInfo[] getSignerInfos() {
		return signerInfos.toArray(new SignerInfo[0]);
	}

	@Override
	public boolean isSigned() {
		return !signerInfos.isEmpty();
	}

	@Override
	public Date getSigningTime(SignerInfo signerInfo) {
		if (signerInfo instanceof CodeSignerInfo) {
			TimestampSignerInfo tsInfo = ((CodeSignerInfo) signerInfo).getTSASignerInfo();
			if (tsInfo != null) {
				return tsInfo.getTimestamp();
			}
		}
		return null;
	}

	@Override
	public SignerInfo getTSASignerInfo(SignerInfo signerInfo) {
		if (signerInfo instanceof CodeSignerInfo) {
			return ((CodeSignerInfo) signerInfo).getTSASignerInfo();
		}
		return null;
	}

	@Override
	public void checkValidity(SignerInfo signerInfo)
			throws CertificateExpiredException, CertificateNotYetValidException {
		// TODO Auto-generated method stub

	}

}

Back to the top