ContextNamePrincipal | Line # 65 | 38 | 17 | 91.4% |
0.9142857
|
No Tests | |||
1 | /* | |
2 | * Copyright (c) 2006, University of Kent | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * 1. Neither the name of the University of Kent nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |
20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
22 | * PURPOSE ARE DISCLAIMED. | |
23 | * | |
24 | * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
31 | * POSSIBILITY OF SUCH DAMAGE. | |
32 | * | |
33 | * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE | |
34 | * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS | |
35 | * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS | |
36 | * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH | |
37 | * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH | |
38 | * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY | |
39 | * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE | |
40 | * SERIOUS FAULTS, IN THIS SOFTWARE. | |
41 | * | |
42 | * 5. This license is governed, except to the extent that local laws | |
43 | * necessarily apply, by the laws of England and Wales. | |
44 | */ | |
45 | ||
46 | package issrg.pba.rbac; | |
47 | ||
48 | import java.security.Principal; | |
49 | ||
50 | /** | |
51 | * This class represents a context name, which is corresponding to the context | |
52 | * name in a MSoD policy. It is based on RFC2253. | |
53 | * Like DN -- distinguished name, it contains a group of variable-value pairs, | |
54 | * and the value can be * or $: | |
55 | * * means any specific value for this variable, for example, given c=*, then | |
56 | * c=21 and c=34 belong to two different context instances; | |
57 | * $ means any value regardless of its specific value, for example, given | |
58 | * c=$, then c=21 and c=34 belong to the same context instance if | |
59 | * other variable/value pairs also match. | |
60 | * | |
61 | * @author W.Xu | |
62 | * @version 0.1 | |
63 | */ | |
64 | ||
65 | public class ContextNamePrincipal implements java.security.Principal { | |
66 | public static final ContextNamePrincipal WHOLE_WORLD_DN = new ContextNamePrincipal(); // the static initializer below will set the right values | |
67 | 4 | static{ |
68 | 4 | try{ |
69 | 4 | WHOLE_WORLD_DN.name=""; |
70 | 4 | WHOLE_WORLD_DN.parsedDN=new ContextNamePrincipal("").parsedDN; |
71 | }catch(issrg.utils.RFC2253ParsingException rpe){ | |
72 | // this shouldn't happen | |
73 | } | |
74 | } | |
75 | ||
76 | private String name, contextName; | |
77 | private String [][][] parsedDN; | |
78 | private String [][][] instantiatedDN; | |
79 | ||
80 | 4 | protected ContextNamePrincipal() {} |
81 | ||
82 | /** | |
83 | * This constructor builds the object out of the String representation of the DN. It | |
84 | * uses <code>issrg.utils.RFC2253NameParser</code> to check if the name can be successfully parsed. | |
85 | * If not, an <code>issrg.utils.RFC2253ParsingException</code> is thrown. | |
86 | * | |
87 | * @params contextDN is the DN of the Principal | |
88 | * | |
89 | * @throws RFC2253ParsingException | |
90 | * | |
91 | * @see issrg.utils.RFC2253NameParser | |
92 | * @see issrg.utils.RFC2253ParsingException | |
93 | */ | |
94 | 361 | public ContextNamePrincipal(String contextDN) throws issrg.utils.RFC2253ParsingException { |
95 | 361 | name = issrg.utils.RFC2253NameParser.toCanonicalDN( |
96 | parsedDN=issrg.utils.RFC2253NameParser.distinguishedName(contextDN) | |
97 | ); | |
98 | 361 | this.contextName = contextDN; |
99 | } | |
100 | ||
101 | ||
102 | 39 | public String getName(){ |
103 | 39 | return name; |
104 | } | |
105 | ||
106 | /** | |
107 | * Returns the DN as an array, specified by issrg.utils.RFC2253Parser | |
108 | * | |
109 | * @return an array of values representing the DN | |
110 | */ | |
111 | 643 | public String [][][] getParsedDN(){ |
112 | 643 | return parsedDN; |
113 | } | |
114 | ||
115 | /** | |
116 | * This method is to compare two ContextNamePrincipal objects. If two | |
117 | * ContextNamePrincipal equals, then it returns true; | |
118 | * otherwise, it returns false. | |
119 | * | |
120 | * @param con is the input ContextNamePrincipal. | |
121 | * | |
122 | * @return true if this ContextNamePrincipal equals the input con according | |
123 | * to context name hierarchy; | |
124 | * otherwise return false. | |
125 | */ | |
126 | 39 | public boolean equals(ContextNamePrincipal con) { |
127 | 39 | return (name.compareToIgnoreCase(con.getName())==0) ; |
128 | } | |
129 | ||
130 | ||
131 | /** | |
132 | * This method is for MSoD rule matching. If this ContextNamePrincipal | |
133 | * contains con according to context match rules, then | |
134 | * it returns true; otherwise, false. added for MSoD. | |
135 | * | |
136 | * @param con is the input ContextNamePrincipal. | |
137 | * | |
138 | * @return true if this ContextNamePrincipal contains the input con according | |
139 | * to context name hierarchy; | |
140 | * otherwise return false. | |
141 | */ | |
142 | 569 | public boolean contains(ContextNamePrincipal con) { |
143 | 0 | if (con == null) return false; |
144 | ||
145 | 569 | String [][][] instanceDN = con.getParsedDN(); |
146 | ||
147 | 569 | if (parsedDN.length > instanceDN.length ) { |
148 | 169 | return false; //doesn't contain it |
149 | } | |
150 | ||
151 | // here we have: instanceDN.length >= parsedDN.length | |
152 | 835 | for (int i=0; i<parsedDN.length ; i++){ |
153 | //j loop is useless, because only j=0 is actually used | |
154 | //only k =0 and 1 are used. | |
155 | 712 | if (parsedDN[i][0][0].compareToIgnoreCase( instanceDN[i][0][0] ) != 0 ) { |
156 | 104 | return false; |
157 | } | |
158 | ||
159 | 608 | if ( (parsedDN[i][0][1].compareTo( "*" ) != 0) && (parsedDN[i][0][1].compareTo( "$" ) != 0) ) { // wildcard |
160 | 506 | if (parsedDN[i][0][1].compareTo( instanceDN[i][0][1] ) != 0 ){ |
161 | 173 | return false; |
162 | } | |
163 | } | |
164 | } // i loop | |
165 | 123 | return true; |
166 | } | |
167 | ||
168 | /** | |
169 | * This method is for MSoD rule to instantiate a ContextNamePrincipal, | |
170 | * i.e. * is instantiated with a value. added for MSoD. | |
171 | * | |
172 | * @param inputCNP is the input ContextNamePrincipal. | |
173 | * | |
174 | * @return the instantiated contextNamePrincipal, ie * is instantiated with | |
175 | * real value. | |
176 | */ | |
177 | 235 | public ContextNamePrincipal instantiate(ContextNamePrincipal inputCNP) { |
178 | 235 | if ( !contains(inputCNP)){ |
179 | 161 | return null; |
180 | } | |
181 | ||
182 | 74 | String [][][] inputDN = inputCNP.getParsedDN(); |
183 | 74 | try{ |
184 | 74 | instantiatedDN = issrg.utils.RFC2253NameParser.distinguishedName(contextName); |
185 | } catch(issrg.utils.RFC2253ParsingException e){ //will never happen here; be handled in constructor | |
186 | } | |
187 | ||
188 | 319 | for (int i=0; i<instantiatedDN.length ; i++){ |
189 | //j loop is useless, because only j=0 is actually used | |
190 | //only k =0 and 1 are used. | |
191 | 245 | if (instantiatedDN[i][0][0].compareTo( inputDN[i][0][0] ) != 0 ) { |
192 | 0 | return null; |
193 | } | |
194 | ||
195 | 245 | if (instantiatedDN[i][0][1].compareTo( "*" ) == 0 ) { // wildcard |
196 | 56 | instantiatedDN[i][0][1] = new String(inputDN[i][0][1]); |
197 | 189 | } else if (instantiatedDN[i][0][1].compareTo( "$" ) == 0 ) { // wildcard |
198 | 6 | instantiatedDN[i][0][1] = new String("$"); |
199 | } else { | |
200 | 183 | if (instantiatedDN[i][0][1].compareTo( inputDN[i][0][1] ) != 0 ) |
201 | 0 | return null; |
202 | } | |
203 | } // i loop | |
204 | ||
205 | 74 | ContextNamePrincipal c = null; |
206 | 74 | try{ |
207 | 74 | c= new ContextNamePrincipal(issrg.utils.RFC2253NameParser.toCanonicalDN(instantiatedDN)); |
208 | } catch (Exception e){} | |
209 | 74 | return c; |
210 | } | |
211 | ||
212 | } |
|