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