Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / security / Identity.java
blobc9df0a58ffa83c77a194ab54f73b4fdccfcb52f5
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)
9 any later version.
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
19 02110-1301 USA.
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.
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;
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.
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
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>.
66 public abstract class Identity implements Principal, Serializable
68 private static final long serialVersionUID = 3609922007826600659L;
70 private String name;
71 private IdentityScope scope;
72 private PublicKey publicKey;
73 private String info;
74 private Vector certificates;
76 /** Constructor for serialization only. */
77 protected Identity()
81 /**
82 * Constructs a new instance of <code>Identity</code> with the specified
83 * name and scope.
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.
92 public Identity(String name, IdentityScope scope)
93 throws KeyManagementException
95 this.name = name;
96 this.scope = scope;
99 /**
100 * Constructs a new instance of <code>Identity</code> with the specified
101 * name and no scope.
103 * @param name
104 * the name to use.
106 public Identity(String name)
108 this.name = name;
109 this.scope = null;
112 /** @return the name of this identity. */
113 public final String getName()
115 return name;
118 /** @return the scope of this identity. */
119 public final IdentityScope getScope()
121 return scope;
125 * @return the public key of this identity.
126 * @see #setPublicKey(java.security.PublicKey)
128 public PublicKey getPublicKey()
130 return publicKey;
134 * Sets the public key for this identity. The old key and all certificates
135 * are removed.
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.
146 public void setPublicKey(PublicKey key) throws KeyManagementException
148 SecurityManager sm = System.getSecurityManager();
149 if (sm != null)
150 sm.checkSecurityAccess("setIdentityPublicKey");
152 this.publicKey = key;
156 * Sets the general information string.
158 * @param info
159 * the general information string.
160 * @throws SecurityException
161 * if a {@link SecurityManager} is installed which disallows this
162 * operation.
164 public void setInfo(String info)
166 SecurityManager sm = System.getSecurityManager();
167 if (sm != null)
168 sm.checkSecurityAccess("setIdentityInfo");
170 this.info = info;
174 * @return the general information string of this identity.
175 * @see #setInfo(String)
177 public String getInfo()
179 return info;
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.
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.
195 public void addCertificate(Certificate certificate)
196 throws KeyManagementException
198 SecurityManager sm = System.getSecurityManager();
199 if (sm != null)
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.
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.
222 public void removeCertificate(Certificate certificate)
223 throws KeyManagementException
225 SecurityManager sm = System.getSecurityManager();
226 if (sm != null)
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);
243 return certs;
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>
253 * otherwise.
255 public final boolean equals(Object identity)
257 if (identity instanceof Identity)
259 if (identity == this)
260 return true;
262 if ((((Identity) identity).getName().equals(this.name)) &&
263 (((Identity) identity).getScope().equals(this.scope)))
264 return true;
266 return identityEquals((Identity) identity);
268 return false;
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>
277 * otherwise.
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
291 * operation.
293 public String toString()
295 SecurityManager sm = System.getSecurityManager();
296 if (sm != null)
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.
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.
313 public String toString(boolean detailed)
315 SecurityManager sm = System.getSecurityManager();
316 if (sm != null)
317 sm.checkSecurityAccess("printIdentity");
319 if (detailed)
321 /* TODO: Insert proper detailed format here */
322 return (name + ":@" + scope + " Public Key: " + publicKey);
324 else
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();
337 if (scope != null)
338 ret |= scope.hashCode();
339 if (info != null)
340 ret |= info.hashCode();
341 if (certificates != null)
342 ret |= certificates.hashCode();
344 return ret;