Debian Codegraph @

ssl-proxies/src/test/java/org/globus/gsi/util/TestSigningPolicyFileParser.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.util;
16
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19 import org.globus.gsi.SigningPolicyException;
20
21 import java.io.StringReader;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Vector;
25 import java.util.regex.Pattern;
26
27 import javax.security.auth.x500.X500Principal;
28
29 import org.globus.gsi.SigningPolicy;
30 import org.globus.gsi.SigningPolicyParser;
31 import org.globus.gsi.testutils.FileSetupUtil;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 public class TestSigningPolicyFileParser {
37
38 FileSetupUtil successFile;
39 FileSetupUtil singleAllowedDn;
40 FileSetupUtil[] tabTestFiles;
41
42 @Before
43 public void setup() throws Exception {
44
45 this.successFile =
46 new FileSetupUtil(
47 "certificateUtilTest/samplePolicy.signing_policy");
48
49 this.singleAllowedDn =
50 new FileSetupUtil("certificateUtilTest/5aba75cb.signing_policy");
51
52 this.tabTestFiles = new FileSetupUtil[3];
53 this.tabTestFiles[0] =
54 new FileSetupUtil("certificateUtilTest/afe55e66.signing_policy");
55 this.tabTestFiles[1] =
56 new FileSetupUtil("certificateUtilTest/cf4ba8c8.signing_policy");
57 this.tabTestFiles[2] =
58 new FileSetupUtil("certificateUtilTest/49f18420.signing_policy");
59 }
60
61 @Test
62 public void testPatternMatching() throws Exception {
63
64 // test getPattern method
65 // no wildcards or question marks
66 String patternStr = "CN=abcdefgh";
67 String patternR = (SigningPolicyParser.getPattern(patternStr))
68 .pattern();
69 assertTrue("CN=abcdefgh".equals(patternR));
70
71 // first character wildcard and question marks
72 String pattern1Str = "CN=*def?gh?";
73 Pattern pattern1 = SigningPolicyParser.getPattern(pattern1Str);
74 String pattern1R = pattern1.pattern();
75 assertTrue(("CN=" + SigningPolicyParser.WILDCARD_PATTERN + "def" +
76 SigningPolicyParser.SINGLE_PATTERN + "gh" +
77 SigningPolicyParser.SINGLE_PATTERN).equals(pattern1R));
78
79 // only wild cards
80 String pattern2Str = "/CN=abc*def*gh";
81 Pattern pattern2 = SigningPolicyParser.getPattern(pattern2Str);
82 String pattern2R = pattern2.pattern();
83 assertTrue(
84 ("/CN=abc" + SigningPolicyParser.WILDCARD_PATTERN + "def" +
85 SigningPolicyParser.WILDCARD_PATTERN + "gh").equals(
86 pattern2R));
87
88 // test isValidDN methods
89 // Add patern2, wildcards in middle
90 Vector<Pattern> allowed = new Vector();
91 allowed.add(pattern2);
92 X500Principal fooPrincipal = new X500Principal("CN=foo");
93 SigningPolicy policy = new SigningPolicy(fooPrincipal, allowed);
94
95 X500Principal subject21 = new X500Principal("CN=abc12DEF34defdef56gh");
96 assertTrue(policy.isValidSubject(subject21));
97
98 X500Principal subject22 =
99 new X500Principal("CN=123abc12def34defdef56gh");
100 assertFalse(policy.isValidSubject(subject22));
101
102 X500Principal subject23 =
103 new X500Principal("CN=abc12def34defdef56gh123");
104 assertFalse(policy.isValidSubject(subject23));
105
106 // wildcard as first and last character
107 String pattern3Str = "*abc*def*gh*";
108 Pattern pattern3 = SigningPolicyParser.getPattern(pattern3Str);
109 allowed.clear();
110 allowed.add(pattern3);
111 policy = new SigningPolicy(fooPrincipal, allowed);
112
113 X500Principal subject31 = new X500Principal("CN=ABC12def34defdef56gh");
114 assertTrue(policy.isValidSubject(subject31));
115
116 X500Principal subject32 =
117 new X500Principal("CN=123abc12def34defdef56gh555");
118 assertTrue(policy.isValidSubject(subject32));
119
120 // use of space and slashes, from old signing policy file
121 String pattern4Str = "/C=US/O=Globus/*";
122 Pattern pattern4 = SigningPolicyParser.getPattern(pattern4Str);
123 allowed.clear();
124 allowed.add(pattern4);
125
126 policy = new SigningPolicy(fooPrincipal, allowed);
127
128 X500Principal subject41 =
129 new X500Principal(
130 "CN=Globus Certification Authority, O=Globus, C=US");
131 assertTrue(policy.isValidSubject(subject41));
132
133 // wildcard as first character, question mark
134 String pattern5Str = "/*C=US/O=Globus/CN=foo-?/CN=*";
135 Pattern pattern5 = SigningPolicyParser.getPattern(pattern5Str);
136 allowed.clear();
137 allowed.add(pattern5);
138 policy = new SigningPolicy(fooPrincipal, allowed);
139
140 X500Principal subject51 =
141 new X500Principal(
142 "CN=Globus Certification Authority, O=Globus, O=US");
143 assertFalse(policy.isValidSubject(subject51));
144 X500Principal subject52 =
145 new X500Principal(
146 "CN=test space,CN=a12b,CN=foo-1,O=Globus,C=US,C=SOME");
147 assertTrue(policy.isValidSubject(subject52));
148 X500Principal subject53 =
149 new X500Principal("CN=,CN=foo-k,O=Globus,C=US");
150 assertTrue(policy.isValidSubject(subject53));
151 X500Principal subject54 =
152 new X500Principal("CN= , CN=foo-1, O=Globus, C=US");
153 assertTrue(policy.isValidSubject(subject54));
154
155 X500Principal subject55 =
156 new X500Principal("C=US,O=Globus,CN=foo-123,CN=");
157 assertFalse(policy.isValidSubject(subject55));
158
159 // multiple question mark with punctuation
160 String pattern6Str = "/C=US/O=global/CN=*/CN=user-??";
161 Pattern pattern6 = SigningPolicyParser.getPattern(pattern6Str);
162 allowed.clear();
163 allowed.add(pattern6);
164 policy = new SigningPolicy(fooPrincipal, allowed);
165
166 X500Principal subject61 =
167 new X500Principal("CN=user-12,CN=foo,O=Globus,C=US");
168 assertFalse(policy.isValidSubject(subject61));
169 X500Principal subject62 =
170 new X500Principal("CN=user-12,CN=foo,O=Global,C=US");
171 assertTrue(policy.isValidSubject(subject62));
172 X500Principal subject63 =
173 new X500Principal("CN=user-12,CN=bar 1,CN=foo ,O=global,C=US");
174 assertTrue(policy.isValidSubject(subject63));
175
176 // add multiple patterns and test validity if atleast one matches
177 String pattern7Str = "/C=US/O=Globus/CN=*/CN=user-??";
178 Pattern pattern7 = SigningPolicyParser.getPattern(pattern7Str);
179 allowed.add(pattern7);
180 policy = new SigningPolicy(fooPrincipal, allowed);
181
182 X500Principal subject71 =
183 new X500Principal("CN=user-12, CN=bar 1, CN=foo , O=Globus,C=US");
184 assertTrue(policy.isValidSubject(subject71));
185 assertTrue(policy.isValidSubject(subject63));
186 }
187
188 // JGLOBUS-103
189 @Test
190 public void testFileSuccess() throws Exception {
191
192 this.successFile.copyFileToTemp();
193
194 SigningPolicyParser parser = new SigningPolicyParser();
195 Map<X500Principal, SigningPolicy> map =
196 parser.parse(this.successFile.getAbsoluteFilename());
197
198 assertTrue(map != null);
199
200 SigningPolicy policy =
201 map.get(new X500Principal(
202 "CN=Globus Certification Authority,O=Globus,C=US"));
203
204 assertTrue(policy != null);
205 List<Pattern> allowedDN = policy.getAllowedDNs();
206 assertTrue(allowedDN != null);
207 assertTrue(allowedDN.size() == 2);
208
209 List<String> patterns = new Vector(2);
210 patterns.add((allowedDN.get(0)).pattern());
211 patterns.add((allowedDN.get(1)).pattern());
212
213 // given the getPattern method is already tested, assuming it
214 // works here.
215 Pattern p1 = SigningPolicyParser.getPattern("/C=us/O=Globus/*");
216 assertTrue(patterns.contains(p1.pattern()));
217 p1 = SigningPolicyParser.getPattern("/C=US/O=Globus/*");
218 assertTrue(patterns.contains(p1.pattern()));
219 p1 = SigningPolicyParser
220 .getPattern("/C=us/O=National Computational Science Alliance/*");
221 assertFalse(patterns.contains(p1.pattern()));
222
223 policy = map.get(new X500Principal(
224 "CN=Globus Certification Authority,O=National Computational Science Alliance,C=US"));
225 assertTrue(policy != null);
226 allowedDN = policy.getAllowedDNs();
227 assertTrue(allowedDN != null);
228 assertTrue(allowedDN.size() == 1);
229 patterns.clear();
230 patterns.add(((Pattern) allowedDN.get(0)).pattern());
231 p1 = SigningPolicyParser
232 .getPattern("/C=us/O=National Computational Science Alliance/*");
233 assertTrue(patterns.contains(p1.pattern()));
234
235 // test file with single allows DN without double quotes
236 this.singleAllowedDn.copyFileToTemp();
237 map.clear();
238 map = parser.parse(this.singleAllowedDn.getAbsoluteFilename());
239
240 policy = map.get(new X500Principal(
241 "OU=Certification Authority,O=National Computational Science Alliance,C=US"));
242
243 assertTrue(policy != null);
244 allowedDN.clear();
245 allowedDN = policy.getAllowedDNs();
246 assertTrue(allowedDN != null);
247 assertTrue(allowedDN.size() == 1);
248
249 patterns = new Vector(1);
250 patterns.add(((Pattern) allowedDN.get(0)).pattern());
251
252 p1 = SigningPolicyParser
253 .getPattern("/C=US/O=National Computational Science Alliance/*");
254 assertTrue(patterns.contains(p1.pattern()));
255 }
256
257 @Test
258 public void testFilesWithTab() throws Exception {
259
260 this.tabTestFiles[0].copyFileToTemp();
261 SigningPolicyParser parser = new SigningPolicyParser();
262
263 Map<X500Principal, SigningPolicy> map =
264 parser.parse(this.tabTestFiles[0].getAbsoluteFilename());
265
266 SigningPolicy policy =
267 map.get(new X500Principal("CN=CyGridCA,O=HPCL,O=CyGrid,C=CY"));
268 assertTrue(policy != null);
269 List<Pattern> allowedDN = policy.getAllowedDNs();
270 assertTrue(allowedDN != null);
271 assertTrue(allowedDN.size() == 1);
272
273 allowedDN.clear();
274 map.clear();
275 this.tabTestFiles[1].copyFileToTemp();
276 map = parser.parse(this.tabTestFiles[1].getAbsoluteFilename());
277 policy = map.get(new X500Principal("CN=CNRS,O=CNRS,C=FR"));
278 assertTrue(policy != null);
279 allowedDN = policy.getAllowedDNs();
280 assertTrue(allowedDN != null);
281 assertTrue(allowedDN.size() == 2);
282
283 Vector patterns = new Vector(2);
284 patterns.add(((Pattern) allowedDN.get(0)).pattern());
285 patterns.add(((Pattern) allowedDN.get(1)).pattern());
286
287 // given the getPattern method is already tested, assuming it
288 // works here.
289 Pattern p1 = SigningPolicyParser
290 .getPattern("/C=FR/O=CNRS/CN=CNRS-Projets");
291 assertTrue(patterns.contains(p1.pattern()));
292 p1 = SigningPolicyParser.getPattern("/C=FR/O=CNRS/CN=CNRS");
293 assertTrue(patterns.contains(p1.pattern()));
294
295
296 allowedDN.clear();
297 map.clear();
298 this.tabTestFiles[2].copyFileToTemp();
299 map = parser.parse(this.tabTestFiles[2].getAbsoluteFilename());
300
301 policy = map.get(
302 new X500Principal("CN=INFN Certification Authority,O=INFN,C=IT"));
303 assertTrue(policy != null);
304 allowedDN = policy.getAllowedDNs();
305 assertTrue(allowedDN != null);
306 assertTrue(allowedDN.size() == 2);
307
308 patterns.clear();
309 patterns.add(((Pattern) allowedDN.get(0)).pattern());
310 patterns.add(((Pattern) allowedDN.get(1)).pattern());
311
312 // given the getPattern method is already tested, assuming it
313 // works here.
314 p1 = SigningPolicyParser.getPattern("/C=it/O=INFN/*");
315 assertTrue(patterns.contains(p1.pattern()));
316 p1 = SigningPolicyParser.getPattern("/C=IT/O=INFN/*");
317 assertTrue(patterns.contains(p1.pattern()));
318 }
319
320 @Test(expected = SigningPolicyException.class)
321 public void testFileFailure() throws Exception {
322 SigningPolicyParser parser = new SigningPolicyParser();
323 parser.parse("Foo");
324 }
325
326 @Test
327 public void testParsingFailure() throws Exception {
328
329 SigningPolicyParser parser = new SigningPolicyParser();
330
331 // not x509
332 String error1 =
333 "access_id_CA notX509 '/C=US/O=Globus/CN=Globus " +
334 "Certification Authority'\n pos_rights globus CA:sign\n" +
335 " cond_subjects globus '\"/C=us/O=Globus/*\" \"/C=US/O=Globus/*\"'";
336
337
338 Map<X500Principal, SigningPolicy> map = null;
339 boolean worked = false;
340 try {
341 map = parser.parse(new StringReader(error1));
342 } catch (IllegalArgumentException e) {
343 worked = true;
344 }
345 assertTrue(worked);
346
347 // not globus
348 error1 =
349 "access_id_CA X509 '/C=US/O=Globus/CN=Globus " +
350 "Certification Authority'\n pos_rights notglobus " +
351 "CA:sign\n cond_subjects globus '\"/C=us/O=Globus/*\" " +
352 "\"/C=US/O=Globus/*\"'";
353 map = parser.parse(new StringReader(error1));
354
355
356 // order of rights matter, atleast one positive right implies
357 // allowed DN
358 error1 =
359 "access_id_CA X509 '/C=US/O=Globus/CN=Globus Certification " +
360 "Authority'\n pos_rights globus CA:sign\n cond_subjects" +
361 " globus '\"/C=us/O=Globus/*\" \"/C=US/O=Globus/*\"' \n " +
362 "neg_rights notglobus some:right";
363 map = parser.parse(new StringReader(error1));
364 SigningPolicy policy = map.get(new X500Principal(
365 "CN=Globus Certification Authority,O=Globus,C=US"));
366 assertTrue(policy != null);
367 List<Pattern> allowedDN = policy.getAllowedDNs();
368 assertTrue(allowedDN != null);
369 assertTrue(allowedDN.size() == 2);
370
371 // incorrect start
372 error1 =
373 "X509 '/C=US/O=Globus/CN=Globus Certification Authority'\n" +
374 " pos_rights notglobus CA:sign\n cond_subjects " +
375 "globus \'\"/C=us/O=Globus/*\" \"/C=US/O=Globus/*\"\'";
376 boolean exception = false;
377 try {
378 map = parser.parse(new StringReader(error1));
379 } catch (SigningPolicyException exp) {
380 exception = true;
381 }
382 assertTrue(exception);
383
384 // erroneous quote
385 error1 =
386 "access_id_CA X509 '/C=US/O=Globus/CN=Globus Certification " +
387 "Authority\n pos_rights notglobus CA:sign\n " +
388 "cond_subjects globus \'\"/C=us/O=Globus/*\" " +
389 "\"/C=US/O=Globus/*\"\'";
390 exception = false;
391 try {
392 map = parser.parse(new StringReader(error1));
393 } catch (SigningPolicyException exp) {
394 if ((exp.getMessage().indexOf("invalid")) != -1) {
395 exception = true;
396 }
397 }
398 assertTrue(exception);
399
400 // neg rights rather than restrictions
401 error1 =
402 "access_id_CA X509 '/C=US/O=Globus/CN=Globus " +
403 "Certification Authority'\n pos_rights globus " +
404 "CA:sign\n neg_rights notglobus some:right";
405 exception = false;
406 try {
407 map = parser.parse(new StringReader(error1));
408 } catch (SigningPolicyException exp) {
409 // if ((exp.getMessage().indexOf("File format is incorrect") != -1) &&
410 // (exp.getMessage().
411 // indexOf("neg_rights cannot be used here") != -1)) {
412 exception = true;
413 // }
414 }
415 assertTrue(exception);
416
417 // first pos_rights is all that matters
418 error1 =
419 "access_id_CA X509 '/C=US/O=Globus/CN=Globus Certification " +
420 "Authority'\n pos_rights globus CA:sign\n " +
421 "cond_subjects globus '\"/C=us/O=Globus/*\" " +
422 "\"/C=US/O=Globus/*\"' \n cond_subjects globus " +
423 "'\"/C=us/O=Globus/*\"'";
424 map = parser.parse(new StringReader(error1));
425 policy = map.get(new X500Principal(
426 "CN=Globus Certification Authority,O=Globus,C=US"));
427 assertTrue(policy != null);
428 allowedDN = policy.getAllowedDNs();
429 assertTrue(allowedDN != null);
430 assertTrue(allowedDN.size() == 2);
431 }
432
433 @After
434 public void cleanUp() throws Exception {
435
436 this.singleAllowedDn.deleteFile();
437 this.successFile.deleteFile();
438 this.tabTestFiles[0].deleteFile();
439 this.tabTestFiles[1].deleteFile();
440 this.tabTestFiles[2].deleteFile();
441
442 }
443 }