1 /* Identity.java --- Identity Class
2 Copyright (C) 1999, 2003, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package java
.security
;
40 import java
.io
.Serializable
;
41 import java
.util
.Vector
;
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.
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>
52 * <p>Each identity has a set of ceritificates which all specify the same
53 * public key, but not necessarily the same name.</p>
55 * <p>The <code>Identity</code> class can be subclassed to allow additional
56 * information to be attached to it.</p>
58 * @author Mark Benvenuto
62 * @deprecated Replaced by <code>java.security.KeyStore</code>, the
63 * <code>java.security.cert</code> package, and
64 * <code>java.security.Principal</code>.
66 public abstract class Identity
implements Principal
, Serializable
68 private static final long serialVersionUID
= 3609922007826600659L;
71 private IdentityScope scope
;
72 private PublicKey publicKey
;
74 private Vector certificates
;
76 /** Constructor for serialization only. */
82 * Constructs a new instance of <code>Identity</code> with the specified
89 * @throws KeyManagementException
90 * if the identity is already present.
92 public Identity(String name
, IdentityScope scope
)
93 throws KeyManagementException
100 * Constructs a new instance of <code>Identity</code> with the specified
106 public Identity(String name
)
112 /** @return the name of this identity. */
113 public final String
getName()
118 /** @return the scope of this identity. */
119 public final IdentityScope
getScope()
125 * @return the public key of this identity.
126 * @see #setPublicKey(java.security.PublicKey)
128 public PublicKey
getPublicKey()
134 * Sets the public key for this identity. The old key and all certificates
138 * the public key to use.
139 * @throws KeyManagementException
140 * if this public key is used by another identity in the current
142 * @throws SecurityException
143 * if a {@link SecurityManager} is installed which disallows this
146 public void setPublicKey(PublicKey key
) throws KeyManagementException
148 SecurityManager sm
= System
.getSecurityManager();
150 sm
.checkSecurityAccess("setIdentityPublicKey");
152 this.publicKey
= key
;
156 * Sets the general information string.
159 * the general information string.
160 * @throws SecurityException
161 * if a {@link SecurityManager} is installed which disallows this
164 public void setInfo(String info
)
166 SecurityManager sm
= System
.getSecurityManager();
168 sm
.checkSecurityAccess("setIdentityInfo");
174 * @return the general information string of this identity.
175 * @see #setInfo(String)
177 public String
getInfo()
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
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
195 public void addCertificate(Certificate certificate
)
196 throws KeyManagementException
198 SecurityManager sm
= System
.getSecurityManager();
200 sm
.checkSecurityAccess("addIdentityCertificate");
202 // Check public key of this certificate against the first one in the vector
203 if (certificates
.size() > 0)
205 if (((Certificate
) certificates
.firstElement()).getPublicKey() != publicKey
)
206 throw new KeyManagementException("Public key does not match");
208 certificates
.addElement(certificate
);
212 * Removes a certificate from the list of ceritificates for this identity.
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
222 public void removeCertificate(Certificate certificate
)
223 throws KeyManagementException
225 SecurityManager sm
= System
.getSecurityManager();
227 sm
.checkSecurityAccess("removeIdentityCertificate");
229 if (certificates
.contains(certificate
) == false)
230 throw new KeyManagementException("Certificate not found");
232 certificates
.removeElement(certificate
);
235 /** @return an array of {@link Certificate}s for this identity. */
236 public Certificate
[] certificates()
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
);
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.
252 * @return <code>true</code> if they are equal, <code>false</code>
255 public final boolean equals(Object identity
)
257 if (identity
instanceof Identity
)
259 if (identity
== this)
262 if ((((Identity
) identity
).getName().equals(this.name
)) &&
263 (((Identity
) identity
).getScope().equals(this.scope
)))
266 return identityEquals((Identity
) identity
);
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.
276 * @return <code>true</code> if they are equal, <code>false</code>
279 protected boolean identityEquals(Identity identity
)
281 return ((identity
.getName().equals(this.name
)) &&
282 (identity
.getPublicKey().equals(this.publicKey
)));
286 * Returns a string representation of this Identity.
288 * @return a string representation of this Identity.
289 * @throws SecurityException
290 * if a {@link SecurityManager} is installed which disallows this
293 public String
toString()
295 SecurityManager sm
= System
.getSecurityManager();
297 sm
.checkSecurityAccess("printIdentity");
299 /* TODO: Insert proper format here */
300 return (name
+ ":@" + scope
+ " Public Key: " + publicKey
);
304 * Returns a detailed string representation of this Identity.
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
313 public String
toString(boolean detailed
)
315 SecurityManager sm
= System
.getSecurityManager();
317 sm
.checkSecurityAccess("printIdentity");
321 /* TODO: Insert proper detailed format here */
322 return (name
+ ":@" + scope
+ " Public Key: " + publicKey
);
326 /* TODO: Insert proper format here */
327 return (name
+ ":@" + scope
+ " Public Key: " + publicKey
);
331 /** @return a hashcode of this identity. */
332 public int hashCode()
334 int ret
= name
.hashCode();
335 if (publicKey
!= null)
336 ret
|= publicKey
.hashCode();
338 ret
|= scope
.hashCode();
340 ret
|= info
.hashCode();
341 if (certificates
!= null)
342 ret
|= certificates
.hashCode();