Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/SigningPolicy.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;
16
17
18 import org.globus.gsi.util.CertificateUtil;
19
20 import java.util.List;
21 import java.util.Vector;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import javax.security.auth.x500.X500Principal;
25 import org.globus.gsi.SigningPolicyParser;
26
27 /**
28 * Represents a signing policy associated with a particular CA. The signing policy defines a list of distinguished
29 * names which are allowed to sign certificates for a particular Certificate Authority subject distinguished name.
30 *
31 * @version ${version}
32 * @since 1.0
33 */
34 // COMMENT: BCB: new method signatures
35 public class SigningPolicy {
36
37 private X500Principal caSubject;
38 private List<Pattern> allowedDNs;
39
40 /**
41 * Create a signing policy for the supplied subject which allows the supplied list of DNs to sign certificates.
42 *
43 * @param caSubjectDN The DN for the subject to which this policy applies.
44 * @param allowedDNs The list of DNs which can sign certs for this subject.
45 */
46 public SigningPolicy(X500Principal caSubjectDN, String[] allowedDNs) {
47
48 if ((caSubjectDN == null) || (allowedDNs == null)) {
49 throw new IllegalArgumentException();
50 }
51
52 this.caSubject = caSubjectDN;
53 int numberOfDNs = allowedDNs.length;
54 this.allowedDNs = new Vector<Pattern>(numberOfDNs);
55 for (String anAllowedDNs : allowedDNs) {
56 this.allowedDNs.add(SigningPolicyParser.getPattern(anAllowedDNs));
57
58 }
59 }
60
61 /**
62 * Create a signing policy for the supplied subject which allows subjects whose DNs match one of the supplied
63 * patterns to sign certificates.
64 *
65 * @param caSubjectDN The DN for the subject to which this policy applies.
66 * @param allowedDNs A list of patterns to which to compare signing entity DNs.
67 */
68 // COMMENT: allowedDNs != null is new, and causes the test to fail
69 public SigningPolicy(X500Principal caSubjectDN, List<Pattern> allowedDNs) {
70
71 if ((caSubjectDN == null) || (allowedDNs == null)) {
72 throw new IllegalArgumentException();
73 }
74
75 this.caSubject = caSubjectDN;
76 this.allowedDNs = allowedDNs;
77 }
78
79 /**
80 * Get CA subject DN for which this signing policy is defined.
81 *
82 * @return returns the CA subject
83 */
84 public X500Principal getCASubjectDN() {
85 return this.caSubject;
86 }
87
88 /**
89 * Ascertains if the subjectDN is valid against this policy.
90 *
91 * @param subject Subject DN to be validated
92 * @return true if the DN is valid under this policy and false if it is not
93 */
94 public boolean isValidSubject(X500Principal subject) {
95
96 if (subject == null) {
97 throw new IllegalArgumentException();
98 }
99
100 String subjectDN = CertificateUtil.toGlobusID(subject);
101
102 if ((this.allowedDNs == null) || (this.allowedDNs.size() < 1)) {
103 return false;
104 }
105
106 int size = this.allowedDNs.size();
107 for (int i = 0; i < size; i++) {
108 Pattern pattern = allowedDNs.get(i);
109 Matcher matcher = pattern.matcher(subjectDN);
110 boolean valid = matcher.matches();
111 if (valid) {
112 return true;
113 }
114 }
115
116 return false;
117 }
118
119 /**
120 * Return the patterns which identify the valid signing entities. If this signing policy has been created with a
121 * set of DNs then the patterns will simply match the DNs.
122 *
123 * @return The patterns matching allowed signing entities.
124 */
125 public List<Pattern> getAllowedDNs() {
126 return this.allowedDNs;
127 }
128
129 /**
130 * Method to determine if a signing policy is available for a
131 * given DN.
132 *
133 * @return If the patterns vector is not null and has atleast one
134 * element, true is returned. Else the method returns false.
135 */
136 public boolean isPolicyAvailable() {
137
138 if ((this.allowedDNs == null) ||
139 (this.allowedDNs.size() < 1)) {
140 return false;
141 }
142 return true;
143 }
144
145 }