1 |
771 |
jeremybenn |
/* Identity.java --- Identity Class
|
2 |
|
|
Copyright (C) 1999, 2003, Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package java.security;
|
39 |
|
|
|
40 |
|
|
import java.io.Serializable;
|
41 |
|
|
import java.util.Vector;
|
42 |
|
|
|
43 |
|
|
/**
|
44 |
|
|
* The <code>Identity</code> class is used to represent people and companies
|
45 |
|
|
* that can be authenticated using public key encryption. The identities can
|
46 |
|
|
* also be abstract objects such as smart cards.
|
47 |
|
|
*
|
48 |
|
|
* <p><code>Identity</code> objects store a name and public key for each
|
49 |
|
|
* identity. The names cannot be changed and the identities can be scoped. Each
|
50 |
|
|
* identity (name and public key) within a scope are unique to that scope.</p>
|
51 |
|
|
*
|
52 |
|
|
* <p>Each identity has a set of ceritificates which all specify the same
|
53 |
|
|
* public key, but not necessarily the same name.</p>
|
54 |
|
|
*
|
55 |
|
|
* <p>The <code>Identity</code> class can be subclassed to allow additional
|
56 |
|
|
* information to be attached to it.</p>
|
57 |
|
|
*
|
58 |
|
|
* @author Mark Benvenuto
|
59 |
|
|
* @see IdentityScope
|
60 |
|
|
* @see Signer
|
61 |
|
|
* @see Principal
|
62 |
|
|
* @deprecated Replaced by <code>java.security.KeyStore</code>, the
|
63 |
|
|
* <code>java.security.cert</code> package, and
|
64 |
|
|
* <code>java.security.Principal</code>.
|
65 |
|
|
*/
|
66 |
|
|
public abstract class Identity implements Principal, Serializable
|
67 |
|
|
{
|
68 |
|
|
private static final long serialVersionUID = 3609922007826600659L;
|
69 |
|
|
|
70 |
|
|
private String name;
|
71 |
|
|
private IdentityScope scope;
|
72 |
|
|
private PublicKey publicKey;
|
73 |
|
|
private String info;
|
74 |
|
|
private Vector certificates;
|
75 |
|
|
|
76 |
|
|
/** Constructor for serialization only. */
|
77 |
|
|
protected Identity()
|
78 |
|
|
{
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Constructs a new instance of <code>Identity</code> with the specified
|
83 |
|
|
* name and scope.
|
84 |
|
|
*
|
85 |
|
|
* @param name
|
86 |
|
|
* the name to use.
|
87 |
|
|
* @param scope
|
88 |
|
|
* the scope to use.
|
89 |
|
|
* @throws KeyManagementException
|
90 |
|
|
* if the identity is already present.
|
91 |
|
|
*/
|
92 |
|
|
public Identity(String name, IdentityScope scope)
|
93 |
|
|
throws KeyManagementException
|
94 |
|
|
{
|
95 |
|
|
this.name = name;
|
96 |
|
|
this.scope = scope;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
/**
|
100 |
|
|
* Constructs a new instance of <code>Identity</code> with the specified
|
101 |
|
|
* name and no scope.
|
102 |
|
|
*
|
103 |
|
|
* @param name
|
104 |
|
|
* the name to use.
|
105 |
|
|
*/
|
106 |
|
|
public Identity(String name)
|
107 |
|
|
{
|
108 |
|
|
this.name = name;
|
109 |
|
|
this.scope = null;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/** @return the name of this identity. */
|
113 |
|
|
public final String getName()
|
114 |
|
|
{
|
115 |
|
|
return name;
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/** @return the scope of this identity. */
|
119 |
|
|
public final IdentityScope getScope()
|
120 |
|
|
{
|
121 |
|
|
return scope;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/**
|
125 |
|
|
* @return the public key of this identity.
|
126 |
|
|
* @see #setPublicKey(java.security.PublicKey)
|
127 |
|
|
*/
|
128 |
|
|
public PublicKey getPublicKey()
|
129 |
|
|
{
|
130 |
|
|
return publicKey;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
/**
|
134 |
|
|
* Sets the public key for this identity. The old key and all certificates
|
135 |
|
|
* are removed.
|
136 |
|
|
*
|
137 |
|
|
* @param key
|
138 |
|
|
* the public key to use.
|
139 |
|
|
* @throws KeyManagementException
|
140 |
|
|
* if this public key is used by another identity in the current
|
141 |
|
|
* scope.
|
142 |
|
|
* @throws SecurityException
|
143 |
|
|
* if a {@link SecurityManager} is installed which disallows this
|
144 |
|
|
* operation.
|
145 |
|
|
*/
|
146 |
|
|
public void setPublicKey(PublicKey key) throws KeyManagementException
|
147 |
|
|
{
|
148 |
|
|
SecurityManager sm = System.getSecurityManager();
|
149 |
|
|
if (sm != null)
|
150 |
|
|
sm.checkSecurityAccess("setIdentityPublicKey");
|
151 |
|
|
|
152 |
|
|
this.publicKey = key;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
/**
|
156 |
|
|
* Sets the general information string.
|
157 |
|
|
*
|
158 |
|
|
* @param info
|
159 |
|
|
* the general information string.
|
160 |
|
|
* @throws SecurityException
|
161 |
|
|
* if a {@link SecurityManager} is installed which disallows this
|
162 |
|
|
* operation.
|
163 |
|
|
*/
|
164 |
|
|
public void setInfo(String info)
|
165 |
|
|
{
|
166 |
|
|
SecurityManager sm = System.getSecurityManager();
|
167 |
|
|
if (sm != null)
|
168 |
|
|
sm.checkSecurityAccess("setIdentityInfo");
|
169 |
|
|
|
170 |
|
|
this.info = info;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
/**
|
174 |
|
|
* @return the general information string of this identity.
|
175 |
|
|
* @see #setInfo(String)
|
176 |
|
|
*/
|
177 |
|
|
public String getInfo()
|
178 |
|
|
{
|
179 |
|
|
return info;
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
/**
|
183 |
|
|
* Adds a certificate to the list of ceritificates for this identity. The
|
184 |
|
|
* public key in this certificate must match the existing public key if it
|
185 |
|
|
* exists.
|
186 |
|
|
*
|
187 |
|
|
* @param certificate
|
188 |
|
|
* the certificate to add.
|
189 |
|
|
* @throws KeyManagementException
|
190 |
|
|
* if the certificate is invalid, or the public key conflicts.
|
191 |
|
|
* @throws SecurityException
|
192 |
|
|
* if a {@link SecurityManager} is installed which disallows this
|
193 |
|
|
* operation.
|
194 |
|
|
*/
|
195 |
|
|
public void addCertificate(Certificate certificate)
|
196 |
|
|
throws KeyManagementException
|
197 |
|
|
{
|
198 |
|
|
SecurityManager sm = System.getSecurityManager();
|
199 |
|
|
if (sm != null)
|
200 |
|
|
sm.checkSecurityAccess("addIdentityCertificate");
|
201 |
|
|
|
202 |
|
|
// Check public key of this certificate against the first one in the vector
|
203 |
|
|
if (certificates.size() > 0)
|
204 |
|
|
{
|
205 |
|
|
if (((Certificate) certificates.firstElement()).getPublicKey() != publicKey)
|
206 |
|
|
throw new KeyManagementException("Public key does not match");
|
207 |
|
|
}
|
208 |
|
|
certificates.addElement(certificate);
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
/**
|
212 |
|
|
* Removes a certificate from the list of ceritificates for this identity.
|
213 |
|
|
*
|
214 |
|
|
* @param certificate
|
215 |
|
|
* the certificate to remove.
|
216 |
|
|
* @throws KeyManagementException
|
217 |
|
|
* if the certificate is invalid.
|
218 |
|
|
* @throws SecurityException
|
219 |
|
|
* if a {@link SecurityManager} is installed which disallows this
|
220 |
|
|
* operation.
|
221 |
|
|
*/
|
222 |
|
|
public void removeCertificate(Certificate certificate)
|
223 |
|
|
throws KeyManagementException
|
224 |
|
|
{
|
225 |
|
|
SecurityManager sm = System.getSecurityManager();
|
226 |
|
|
if (sm != null)
|
227 |
|
|
sm.checkSecurityAccess("removeIdentityCertificate");
|
228 |
|
|
|
229 |
|
|
if (certificates.contains(certificate) == false)
|
230 |
|
|
throw new KeyManagementException("Certificate not found");
|
231 |
|
|
|
232 |
|
|
certificates.removeElement(certificate);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/** @return an array of {@link Certificate}s for this identity. */
|
236 |
|
|
public Certificate[] certificates()
|
237 |
|
|
{
|
238 |
|
|
Certificate[] certs = new Certificate[certificates.size()];
|
239 |
|
|
int max = certificates.size();
|
240 |
|
|
for (int i = 0; i < max; i++)
|
241 |
|
|
certs[i] = (Certificate) certificates.elementAt(i);
|
242 |
|
|
|
243 |
|
|
return certs;
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
/**
|
247 |
|
|
* Checks for equality between this Identity and a specified object. It first
|
248 |
|
|
* checks if they are the same object, then if the name and scope match and
|
249 |
|
|
* returns <code>true</code> if successful. If these tests fail, the
|
250 |
|
|
* {@link #identityEquals(Identity)} method is called.
|
251 |
|
|
*
|
252 |
|
|
* @return <code>true</code> if they are equal, <code>false</code>
|
253 |
|
|
* otherwise.
|
254 |
|
|
*/
|
255 |
|
|
public final boolean equals(Object identity)
|
256 |
|
|
{
|
257 |
|
|
if (identity instanceof Identity)
|
258 |
|
|
{
|
259 |
|
|
if (identity == this)
|
260 |
|
|
return true;
|
261 |
|
|
|
262 |
|
|
if ((((Identity) identity).getName().equals(this.name)) &&
|
263 |
|
|
(((Identity) identity).getScope().equals(this.scope)))
|
264 |
|
|
return true;
|
265 |
|
|
|
266 |
|
|
return identityEquals((Identity) identity);
|
267 |
|
|
}
|
268 |
|
|
return false;
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
/**
|
272 |
|
|
* Checks for equality between this Identity and a specified object. A
|
273 |
|
|
* subclass should override this method. The default behavior is to return
|
274 |
|
|
* <code>true</code> if the public key and names match.
|
275 |
|
|
*
|
276 |
|
|
* @return <code>true</code> if they are equal, <code>false</code>
|
277 |
|
|
* otherwise.
|
278 |
|
|
*/
|
279 |
|
|
protected boolean identityEquals(Identity identity)
|
280 |
|
|
{
|
281 |
|
|
return ((identity.getName().equals(this.name)) &&
|
282 |
|
|
(identity.getPublicKey().equals(this.publicKey)));
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/**
|
286 |
|
|
* Returns a string representation of this Identity.
|
287 |
|
|
*
|
288 |
|
|
* @return a string representation of this Identity.
|
289 |
|
|
* @throws SecurityException
|
290 |
|
|
* if a {@link SecurityManager} is installed which disallows this
|
291 |
|
|
* operation.
|
292 |
|
|
*/
|
293 |
|
|
public String toString()
|
294 |
|
|
{
|
295 |
|
|
SecurityManager sm = System.getSecurityManager();
|
296 |
|
|
if (sm != null)
|
297 |
|
|
sm.checkSecurityAccess("printIdentity");
|
298 |
|
|
|
299 |
|
|
/* TODO: Insert proper format here */
|
300 |
|
|
return (name + ":@" + scope + " Public Key: " + publicKey);
|
301 |
|
|
}
|
302 |
|
|
|
303 |
|
|
/**
|
304 |
|
|
* Returns a detailed string representation of this Identity.
|
305 |
|
|
*
|
306 |
|
|
* @param detailed
|
307 |
|
|
* indicates whether or detailed information is desired.
|
308 |
|
|
* @return a string representation of this Identity.
|
309 |
|
|
* @throws SecurityException
|
310 |
|
|
* if a {@link SecurityManager} is installed which disallows this
|
311 |
|
|
* operation.
|
312 |
|
|
*/
|
313 |
|
|
public String toString(boolean detailed)
|
314 |
|
|
{
|
315 |
|
|
SecurityManager sm = System.getSecurityManager();
|
316 |
|
|
if (sm != null)
|
317 |
|
|
sm.checkSecurityAccess("printIdentity");
|
318 |
|
|
|
319 |
|
|
if (detailed)
|
320 |
|
|
{
|
321 |
|
|
/* TODO: Insert proper detailed format here */
|
322 |
|
|
return (name + ":@" + scope + " Public Key: " + publicKey);
|
323 |
|
|
}
|
324 |
|
|
else
|
325 |
|
|
{
|
326 |
|
|
/* TODO: Insert proper format here */
|
327 |
|
|
return (name + ":@" + scope + " Public Key: " + publicKey);
|
328 |
|
|
}
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
/** @return a hashcode of this identity. */
|
332 |
|
|
public int hashCode()
|
333 |
|
|
{
|
334 |
|
|
int ret = name.hashCode();
|
335 |
|
|
if (publicKey != null)
|
336 |
|
|
ret |= publicKey.hashCode();
|
337 |
|
|
if (scope != null)
|
338 |
|
|
ret |= scope.hashCode();
|
339 |
|
|
if (info != null)
|
340 |
|
|
ret |= info.hashCode();
|
341 |
|
|
if (certificates != null)
|
342 |
|
|
ret |= certificates.hashCode();
|
343 |
|
|
|
344 |
|
|
return ret;
|
345 |
|
|
}
|
346 |
|
|
}
|