1 7 8 package java.security; 9 10 import java.io.Serializable ; 11 import java.util.*; 12 13 42 @Deprecated 43 public abstract class Identity implements Principal , Serializable { 44 45 46 private static final long serialVersionUID = 3609922007826600659L; 47 48 53 private String name; 54 55 60 private PublicKey publicKey; 61 62 67 String info = "No further information available."; 68 69 74 IdentityScope scope; 75 76 81 Vector certificates; 82 83 86 protected Identity() { 87 this("restoring..."); 88 } 89 90 99 public Identity(String name, IdentityScope scope) throws 100 KeyManagementException { 101 this(name); 102 if (scope != null) { 103 scope.addIdentity(this); 104 } 105 this.scope = scope; 106 } 107 108 113 public Identity(String name) { 114 this.name = name; 115 } 116 117 122 public final String getName() { 123 return name; 124 } 125 126 131 public final IdentityScope getScope() { 132 return scope; 133 } 134 135 142 public PublicKey getPublicKey() { 143 return publicKey; 144 } 145 146 166 167 public void setPublicKey(PublicKey key) throws KeyManagementException { 168 169 check("setIdentityPublicKey"); 170 this.publicKey = key; 171 certificates = new Vector(); 172 } 173 174 190 public void setInfo(String info) { 191 check("setIdentityInfo"); 192 this.info = info; 193 } 194 195 202 public String getInfo() { 203 return info; 204 } 205 206 228 public void addCertificate(Certificate certificate) 229 throws KeyManagementException { 230 231 check("addIdentityCertificate"); 232 233 if (certificates == null) { 234 certificates = new Vector(); 235 } 236 if (publicKey != null) { 237 if (!keyEquals(publicKey, certificate.getPublicKey())) { 238 throw new KeyManagementException ( 239 "public key different from cert public key"); 240 } 241 } else { 242 publicKey = certificate.getPublicKey(); 243 } 244 certificates.addElement(certificate); 245 } 246 247 private boolean keyEquals(Key aKey, Key anotherKey) { 248 String aKeyFormat = aKey.getFormat(); 249 String anotherKeyFormat = anotherKey.getFormat(); 250 if ((aKeyFormat == null) ^ (anotherKeyFormat == null)) 251 return false; 252 if (aKeyFormat != null && anotherKeyFormat != null) 253 if (!aKeyFormat.equalsIgnoreCase(anotherKeyFormat)) 254 return false; 255 return java.util.Arrays.equals(aKey.getEncoded(), 256 anotherKey.getEncoded()); 257 } 258 259 260 278 public void removeCertificate(Certificate certificate) 279 throws KeyManagementException { 280 check("removeIdentityCertificate"); 281 if (certificates != null) { 282 certificates.removeElement(certificate); 283 } 284 } 285 286 291 public Certificate [] certificates() { 292 if (certificates == null) { 293 return new Certificate [0]; 294 } 295 int len = certificates.size(); 296 Certificate [] certs = new Certificate [len]; 297 certificates.copyInto(certs); 298 return certs; 299 } 300 301 316 public final boolean equals(Object identity) { 317 318 if (identity == this) { 319 return true; 320 } 321 322 if (identity instanceof Identity ) { 323 Identity i = (Identity )identity; 324 if (this.fullName().equals(i.fullName())) { 325 return true; 326 } else { 327 return identityEquals(i); 328 } 329 } 330 return false; 331 } 332 333 346 protected boolean identityEquals(Identity identity) { 347 if (!name.equalsIgnoreCase(identity.name)) 348 return false; 349 350 if ((publicKey == null) ^ (identity.publicKey == null)) 351 return false; 352 353 if (publicKey != null && identity.publicKey != null) 354 if (!publicKey.equals(identity.publicKey)) 355 return false; 356 357 return true; 358 359 } 360 361 364 String fullName() { 365 String parsable = name; 366 if (scope != null) { 367 parsable += "." + scope.getName(); 368 } 369 return parsable; 370 } 371 372 389 public String toString() { 390 check("printIdentity"); 391 String printable = name; 392 if (scope != null) { 393 printable += "[" + scope.getName() + "]"; 394 } 395 return printable; 396 } 397 398 420 public String toString(boolean detailed) { 421 String out = toString(); 422 if (detailed) { 423 out += "\n"; 424 out += printKeys(); 425 out += "\n" + printCertificates(); 426 if (info != null) { 427 out += "\n\t" + info; 428 } else { 429 out += "\n\tno additional information available."; 430 } 431 } 432 return out; 433 } 434 435 String printKeys() { 436 String key = ""; 437 if (publicKey != null) { 438 key = "\tpublic key initialized"; 439 } else { 440 key = "\tno public key"; 441 } 442 return key; 443 } 444 445 String printCertificates() { 446 String out = ""; 447 if (certificates == null) { 448 return "\tno certificates"; 449 } else { 450 out += "\tcertificates: \n"; 451 Enumeration e = certificates.elements(); 452 int i = 1; 453 while (e.hasMoreElements()) { 454 Certificate cert = (Certificate )e.nextElement(); 455 out += "\tcertificate " + i++ + 456 "\tfor : " + cert.getPrincipal() + "\n"; 457 out += "\t\t\tfrom : " + 458 cert.getGuarantor() + "\n"; 459 } 460 } 461 return out; 462 } 463 464 469 public int hashCode() { 470 return name.hashCode(); 471 } 472 473 private static void check(String directive) { 474 SecurityManager security = System.getSecurityManager(); 475 if (security != null) { 476 security.checkSecurityAccess(directive); 477 } 478 } 479 } 480 | Popular Tags |