| 1 | /* Identity.java --- Identity Class
|
|---|
| 2 | Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 | import java.io.Serializable;
|
|---|
| 40 | import java.util.Vector;
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | The Identity class is used to repsent people and companies that
|
|---|
| 44 | can be authenticated using public key encryption. The identities
|
|---|
| 45 | can also be abstract objects such as smart cards.
|
|---|
| 46 |
|
|---|
| 47 | Identity object store a name and public key for each identity.
|
|---|
| 48 | The names cannot be changed and the identities can be scoped.
|
|---|
| 49 | Each identity (name and public key) within a scope is unique
|
|---|
| 50 | to that scope.
|
|---|
| 51 |
|
|---|
| 52 | Each identity has a set of ceritificates which all specify the
|
|---|
| 53 | same public key but not necessarily the same name.
|
|---|
| 54 |
|
|---|
| 55 | The Identity class can be subclassed to allow additional
|
|---|
| 56 | information to be attached to it.
|
|---|
| 57 |
|
|---|
| 58 | @since JDK 1.1
|
|---|
| 59 |
|
|---|
| 60 | @deprecated Use java.security.KeyStore, the java.security.cert
|
|---|
| 61 | package, and java.security.Principal.
|
|---|
| 62 |
|
|---|
| 63 | @author Mark Benvenuto
|
|---|
| 64 | */
|
|---|
| 65 | public abstract class Identity implements Principal, Serializable
|
|---|
| 66 | {
|
|---|
| 67 | private String name;
|
|---|
| 68 | private IdentityScope scope;
|
|---|
| 69 | private PublicKey publicKey;
|
|---|
| 70 | private String info;
|
|---|
| 71 | private Vector certificates;
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | Creates a new instance of Identity from Serialized Data
|
|---|
| 75 | */
|
|---|
| 76 | protected Identity()
|
|---|
| 77 | {
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | Creates a new instance of Identity with the specified name
|
|---|
| 82 | and IdentityScope.
|
|---|
| 83 |
|
|---|
| 84 | @param name the name to use
|
|---|
| 85 | @param scope the scope to use
|
|---|
| 86 |
|
|---|
| 87 | @throws KeyManagementException if the identity is already
|
|---|
| 88 | present
|
|---|
| 89 | */
|
|---|
| 90 | public Identity(String name, IdentityScope scope)
|
|---|
| 91 | throws KeyManagementException
|
|---|
| 92 | {
|
|---|
| 93 | this.name = name;
|
|---|
| 94 | this.scope = scope;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | Creates a new instance of Identity with the specified name
|
|---|
| 99 | and no scope.
|
|---|
| 100 |
|
|---|
| 101 | @param name the name to use
|
|---|
| 102 | */
|
|---|
| 103 | public Identity(String name)
|
|---|
| 104 | {
|
|---|
| 105 | this.name = name;
|
|---|
| 106 | this.scope = null;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | Gets the name for this Identity.
|
|---|
| 111 |
|
|---|
| 112 | @return the name
|
|---|
| 113 | */
|
|---|
| 114 | public final String getName()
|
|---|
| 115 | {
|
|---|
| 116 | return name;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | Gets the scope for this Identity.
|
|---|
| 121 |
|
|---|
| 122 | @return the scope
|
|---|
| 123 | */
|
|---|
| 124 | public final IdentityScope getScope()
|
|---|
| 125 | {
|
|---|
| 126 | return scope;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | Gets the public key for this identity.
|
|---|
| 131 |
|
|---|
| 132 | @return the public key
|
|---|
| 133 | */
|
|---|
| 134 | public PublicKey getPublicKey()
|
|---|
| 135 | {
|
|---|
| 136 | return publicKey;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | Sets the public key for this identity.
|
|---|
| 141 | The old key and all certificates are removed.
|
|---|
| 142 |
|
|---|
| 143 | This class checks the security manager with the call
|
|---|
| 144 | checkSecurityAccess with "setIdentityPublicKey".
|
|---|
| 145 |
|
|---|
| 146 | @param key the public key to use
|
|---|
| 147 |
|
|---|
| 148 | @throws KeyManagementException if this public key is used by
|
|---|
| 149 | another identity in the current scope.
|
|---|
| 150 | @throws SecurityException - if the security manager denies
|
|---|
| 151 | access to "setIdentityPublicKey"
|
|---|
| 152 | */
|
|---|
| 153 | public void setPublicKey(PublicKey key) throws KeyManagementException
|
|---|
| 154 | {
|
|---|
| 155 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 156 | if (sm != null)
|
|---|
| 157 | sm.checkSecurityAccess("setIdentityPublicKey");
|
|---|
| 158 |
|
|---|
| 159 | this.publicKey = key;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | Sets the general information string.
|
|---|
| 164 |
|
|---|
| 165 | This class checks the security manager with the call
|
|---|
| 166 | checkSecurityAccess with "setIdentityInfo".
|
|---|
| 167 |
|
|---|
| 168 | @param info the general information string.
|
|---|
| 169 |
|
|---|
| 170 | @throws SecurityException - if the security manager denies
|
|---|
| 171 | access to "setIdentityInfo"
|
|---|
| 172 | */
|
|---|
| 173 | public void setInfo(String info)
|
|---|
| 174 | {
|
|---|
| 175 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 176 | if (sm != null)
|
|---|
| 177 | sm.checkSecurityAccess("setIdentityInfo");
|
|---|
| 178 |
|
|---|
| 179 | this.info = info;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | Gets the general information string.
|
|---|
| 184 |
|
|---|
| 185 | @return the string
|
|---|
| 186 | */
|
|---|
| 187 | public String getInfo()
|
|---|
| 188 | {
|
|---|
| 189 | return info;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /**
|
|---|
| 193 | Adds a certificate to the list of ceritificates for this
|
|---|
| 194 | identity. The public key in this certificate must match the
|
|---|
| 195 | existing public key if it exists.
|
|---|
| 196 |
|
|---|
| 197 | This class checks the security manager with the call
|
|---|
| 198 | checkSecurityAccess with "addIdentityCertificate".
|
|---|
| 199 |
|
|---|
| 200 | @param certificate the certificate to add
|
|---|
| 201 |
|
|---|
| 202 | @throws KeyManagementException if the certificate is invalid
|
|---|
| 203 | or the public key conflicts
|
|---|
| 204 | @throws SecurityException - if the security manager denies
|
|---|
| 205 | access to "addIdentityCertificate"
|
|---|
| 206 | */
|
|---|
| 207 | public void addCertificate(java.security.Certificate certificate)
|
|---|
| 208 | throws KeyManagementException
|
|---|
| 209 | {
|
|---|
| 210 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 211 | if (sm != null)
|
|---|
| 212 | sm.checkSecurityAccess("addIdentityCertificate");
|
|---|
| 213 |
|
|---|
| 214 | //Check public key of this certificate against the first one
|
|---|
| 215 | //in the vector
|
|---|
| 216 | if (certificates.size() > 0)
|
|---|
| 217 | {
|
|---|
| 218 | if (((Certificate) certificates.firstElement()).getPublicKey() !=
|
|---|
| 219 | publicKey)
|
|---|
| 220 | throw new KeyManagementException("Public key does not match");
|
|---|
| 221 | }
|
|---|
| 222 | certificates.addElement(certificate);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /**
|
|---|
| 226 | Removes a certificate from the list of ceritificates for this
|
|---|
| 227 | identity.
|
|---|
| 228 |
|
|---|
| 229 | This class checks the security manager with the call
|
|---|
| 230 | checkSecurityAccess with "removeIdentityCertificate".
|
|---|
| 231 |
|
|---|
| 232 | @param certificate the certificate to add
|
|---|
| 233 |
|
|---|
| 234 | @throws KeyManagementException if the certificate is invalid
|
|---|
| 235 | @throws SecurityException - if the security manager denies
|
|---|
| 236 | access to "removeIdentityCertificate"
|
|---|
| 237 | */
|
|---|
| 238 | public void removeCertificate(Certificate certificate)
|
|---|
| 239 | throws KeyManagementException
|
|---|
| 240 | {
|
|---|
| 241 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 242 | if (sm != null)
|
|---|
| 243 | sm.checkSecurityAccess("removeIdentityCertificate");
|
|---|
| 244 |
|
|---|
| 245 | if (certificates.contains(certificate) == false)
|
|---|
| 246 | throw new KeyManagementException("Certificate not found");
|
|---|
| 247 |
|
|---|
| 248 | certificates.removeElement(certificate);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /**
|
|---|
| 252 | Returns an array of certificates for this identity.
|
|---|
| 253 |
|
|---|
| 254 | @returns array of certificates
|
|---|
| 255 | */
|
|---|
| 256 | public Certificate[] certificates()
|
|---|
| 257 | {
|
|---|
| 258 | Certificate certs[] = new Certificate[certificates.size()];
|
|---|
| 259 | int max = certificates.size();
|
|---|
| 260 | for (int i = 0; i < max; i++)
|
|---|
| 261 | certs[i] = (Certificate) certificates.elementAt(i);
|
|---|
| 262 | return certs;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | /**
|
|---|
| 266 | Checks for equality between this Identity and the specified
|
|---|
| 267 | object. If first checks if they are the same object, then
|
|---|
| 268 | if the name and scope matches and returns true if successful.
|
|---|
| 269 | If these tests fail, identityEquals is called.
|
|---|
| 270 |
|
|---|
| 271 | @return true if they are equal, false otherwise
|
|---|
| 272 | */
|
|---|
| 273 | public final boolean equals(Object identity)
|
|---|
| 274 | {
|
|---|
| 275 | if (identity instanceof Identity)
|
|---|
| 276 | {
|
|---|
| 277 | if (identity == this)
|
|---|
| 278 | return true;
|
|---|
| 279 |
|
|---|
| 280 | if ((((Identity) identity).getName() == this.name) &&
|
|---|
| 281 | (((Identity) identity).getScope() == this.scope))
|
|---|
| 282 | return true;
|
|---|
| 283 |
|
|---|
| 284 | return identityEquals((Identity) identity);
|
|---|
| 285 | }
|
|---|
| 286 | return false;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /**
|
|---|
| 290 | Checks for equality between this Identity and the specified
|
|---|
| 291 | object. A subclass should override this method. The default
|
|---|
| 292 | behavior is to return true if the public key and names match.
|
|---|
| 293 |
|
|---|
| 294 | @return true if they are equal, false otherwise
|
|---|
| 295 | */
|
|---|
| 296 | protected boolean identityEquals(Identity identity)
|
|---|
| 297 | {
|
|---|
| 298 | return ((identity.getName() == this.name) &&
|
|---|
| 299 | (identity.getPublicKey() == this.publicKey));
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | /**
|
|---|
| 303 | Returns a string representing this Identity.
|
|---|
| 304 |
|
|---|
| 305 | This class checks the security manager with the call
|
|---|
| 306 | checkSecurityAccess with "printIdentity".
|
|---|
| 307 |
|
|---|
| 308 | @returns a string representing this Identity.
|
|---|
| 309 |
|
|---|
| 310 | @throws SecurityException - if the security manager denies
|
|---|
| 311 | access to "printIdentity"
|
|---|
| 312 | */
|
|---|
| 313 | public String toString()
|
|---|
| 314 | {
|
|---|
| 315 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 316 | if (sm != null)
|
|---|
| 317 | sm.checkSecurityAccess("printIdentity");
|
|---|
| 318 |
|
|---|
| 319 | /* TODO: Insert proper format here */
|
|---|
| 320 | return (name + ":@" + scope + " Public Key: " + publicKey);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /**
|
|---|
| 324 | Returns a detailed string representing this Identity.
|
|---|
| 325 |
|
|---|
| 326 | This class checks the security manager with the call
|
|---|
| 327 | checkSecurityAccess with "printIdentity".
|
|---|
| 328 |
|
|---|
| 329 | @param detailed indicates whether or not to provide detailed
|
|---|
| 330 | information
|
|---|
| 331 |
|
|---|
| 332 | @returns a string representing this Identity.
|
|---|
| 333 |
|
|---|
| 334 | @throws SecurityException - if the security manager denies
|
|---|
| 335 | access to "printIdentity"
|
|---|
| 336 | */
|
|---|
| 337 | public String toString(boolean detailed)
|
|---|
| 338 | {
|
|---|
| 339 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 340 | if (sm != null)
|
|---|
| 341 | sm.checkSecurityAccess("printIdentity");
|
|---|
| 342 |
|
|---|
| 343 | if (detailed)
|
|---|
| 344 | {
|
|---|
| 345 | /* TODO: Insert proper detailed format here */
|
|---|
| 346 | return (name + ":@" + scope + " Public Key: " + publicKey);
|
|---|
| 347 | }
|
|---|
| 348 | else
|
|---|
| 349 | {
|
|---|
| 350 | /* TODO: Insert proper format here */
|
|---|
| 351 | return (name + ":@" + scope + " Public Key: " + publicKey);
|
|---|
| 352 | }
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | /**
|
|---|
| 356 | Gets the hashcode for this Identity.
|
|---|
| 357 |
|
|---|
| 358 | @returns the hashcode
|
|---|
| 359 | */
|
|---|
| 360 | public int hashCode()
|
|---|
| 361 | {
|
|---|
| 362 | int ret = name.hashCode();
|
|---|
| 363 | if (publicKey != null)
|
|---|
| 364 | ret |= publicKey.hashCode();
|
|---|
| 365 | if (scope != null)
|
|---|
| 366 | ret |= scope.hashCode();
|
|---|
| 367 | if (info != null)
|
|---|
| 368 | ret |= info.hashCode();
|
|---|
| 369 | if (certificates != null)
|
|---|
| 370 | ret |= certificates.hashCode();
|
|---|
| 371 |
|
|---|
| 372 | return ret;
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|