Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 822d4c688def1e0facb6d783193d14f9be67e08c (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*******************************************************************************
 * Copyright (c) 2007, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.security;

import java.util.ArrayList;
import java.util.Collection;
import junit.framework.TestSuite;
import org.osgi.framework.*;
import org.osgi.service.condpermadmin.*;

public class OSGiAPICertificateTest extends BaseSecurityTest {
	private static String dn1 = "CN=CA1 LeafA, O=CA1, L=Boston, ST=Massachusetts, C=US"; //$NON-NLS-1$
	private static String dn2 = "CN=CA1 Root, O=CA1, L=Boston, ST=Massachusetts, C=US"; //$NON-NLS-1$
	private static String dn3 = "CN=CA1 LeafA, O=CA1, L=Austin, ST=Texas, C=US"; //$NON-NLS-1$
	private static String dn4 = "CN=CA1 Root, O=CA1, L=Austin, ST=Texas, C=US"; //$NON-NLS-1$
	private static String dn5 = "CN=CA1 LeafA, O=CA1, L=*, ST=*, C=US"; //$NON-NLS-1$
	private static String dn6 = "CN=CA1 Root, O=CA1, L=*, ST=*, C=US"; //$NON-NLS-1$
	private static String dn7 = "*, L=*, ST=*, C=US"; //$NON-NLS-1$

	private static String dnChain01True = dn1 + ';' + dn2;
	private static String dnChain02True = "*;" + dn2; //$NON-NLS-1$
	private static String dnChain03True = dn1 + ";*"; //$NON-NLS-1$
	private static String dnChain04False = dn1 + ';' + dn4;
	private static String dnChain05False = dn3 + ';' + dn2;
	private static String dnChain06True = dn5 + ';' + dn6;
	private static String dnChain07True = dn7 + ';' + dn6;
	private static String dnChain08True = dn5 + ';' + dn7;

	private static String dnChain01TrueEscaped = escapeStar(dnChain01True);
	private static String dnChain02TrueEscaped = escapeStar(dnChain02True);
	private static String dnChain03TrueEscaped = escapeStar(dnChain03True);
	private static String dnChain04FalseEscaped = escapeStar(dnChain04False);
	private static String dnChain05FalseEscaped = escapeStar(dnChain05False);
	private static String dnChain06TrueEscaped = escapeStar(dnChain06True);
	private static String dnChain07TrueEscaped = escapeStar(dnChain07True);
	private static String dnChain08TrueEscaped = escapeStar(dnChain08True);

	private static ConditionInfo info01True = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {"-"}); //$NON-NLS-1$
	private static ConditionInfo info02False = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {"-", "!"}); //$NON-NLS-1$ //$NON-NLS-2$
	private static ConditionInfo info03True = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain01True});
	private static ConditionInfo info04True = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain02True});
	private static ConditionInfo info05True = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain03True});
	private static ConditionInfo info06False = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain04False});
	private static ConditionInfo info07False = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain05False});
	private static ConditionInfo info08True = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain06True});
	private static ConditionInfo info09True = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain07True});
	private static ConditionInfo info10True = new ConditionInfo(BundleSignerCondition.class.getName(), new String[] {dnChain08True});

	private Collection<Bundle> installedBundles = new ArrayList<Bundle>();

	public static TestSuite suite() {
		return new TestSuite(OSGiAPICertificateTest.class);
	}

	private static String escapeStar(String dnChain) {
		if (dnChain == null || dnChain.length() == 0)
			return dnChain;
		for (int star = dnChain.indexOf('*'); star >= 0; star = dnChain.indexOf('*', star + 2))
			dnChain = dnChain.substring(0, star) + '\\' + dnChain.substring(star);
		return dnChain;
	}

	public OSGiAPICertificateTest() {
		super();
	}

	public OSGiAPICertificateTest(String name, String jarname, String[] aliases) {
		super(name);
	}

	protected void setUp() throws Exception {
		registerEclipseTrustEngine();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		for (Bundle b : installedBundles) {
			try {
				b.uninstall();
			} catch (BundleException e) {
				// do nothing
			}
		}
	}

	protected Bundle installBundle(String bundlePath) {
		Bundle b = super.installBundle(bundlePath);
		installedBundles.add(b);
		return b;
	}

	public void testBundleSignerCondition01() {
		// test trusted cert with all signed match
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info01True);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition02() {
		// test trusted cert with all signed match + "!" not operation
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info02False);
			assertEquals("Unexpected condition value", Condition.FALSE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition03() {
		// test untrusted cert with all signed match
		try {
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info01True);
			assertEquals("Unexpected condition value", Condition.FALSE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition04() {
		// test untrusted cert with all signed match + "!" not operation
		try {
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info02False);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition05() {
		// test trusted cert with exact match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info03True);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition06() {
		// test trusted cert with prefix wildcard match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info04True);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition07() {
		// test trusted cert with postfix wildcard match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info05True);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition08() {
		// test trusted cert with wrong prefix dn
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info06False);
			assertEquals("Unexpected condition value", Condition.FALSE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition09() {
		// test trusted cert with wrong postfix dn
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info07False);
			assertEquals("Unexpected condition value", Condition.FALSE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition10() {
		// test trusted cert with RDN wildcard match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info08True);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition11() {
		// test trusted cert with RDN wildcard match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info09True);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testBundleSignerCondition12() {
		// test trusted cert with RDN wildcard match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			Condition condition = BundleSignerCondition.getCondition(testBundle, info10True);
			assertEquals("Unexpected condition value", Condition.TRUE, condition); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission01() {
		// test trusted cert with exact match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=-)", AdminPermission.CONTEXT); //$NON-NLS-1$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertTrue("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission02() {
		// test trusted cert with exact match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain01TrueEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertTrue("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission03() {
		// test trusted cert with exact match pattern + ! operation 
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(!(signer=-))", AdminPermission.CONTEXT); //$NON-NLS-1$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertFalse("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission04() {
		// test trusted cert with exact match pattern + ! operation
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(!(signer=" + dnChain01TrueEscaped + "))", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertFalse("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission05() {
		// test trusted cert with prefix wildcard match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain02TrueEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertTrue("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission06() {
		// test trusted cert with postfix wildcard match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain03TrueEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertTrue("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission07() {
		// test trusted cert with bad postfix dn match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain04FalseEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertFalse("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission08() {
		// test trusted cert with bad prefix dn match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain05FalseEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertFalse("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission09() {
		// test trusted cert with RDN match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain06TrueEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertTrue("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission10() {
		// test trusted cert with RDN match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain07TrueEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertTrue("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	public void testAdminPermission11() {
		// test trusted cert with RDN match pattern
		try {
			getTrustEngine().addTrustAnchor(getTestCertificate("ca1_leafa"), "ca1_leafa"); //$NON-NLS-1$ //$NON-NLS-2$
			Bundle testBundle = installBundle(getTestJarPath("signed")); //$NON-NLS-1$
			AdminPermission declaredPerm = new AdminPermission("(signer=" + dnChain08TrueEscaped + ")", AdminPermission.CONTEXT); //$NON-NLS-1$ //$NON-NLS-2$
			AdminPermission checkedPerm = new AdminPermission(testBundle, AdminPermission.CONTEXT);
			assertTrue("Security check failed", declaredPerm.implies(checkedPerm)); //$NON-NLS-1$
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

}

Back to the top