| 1 7 8 package javax.security.auth; 9 10 import java.util.*; 11 import java.io.*; 12 import java.lang.reflect.*; 13 import java.text.MessageFormat ; 14 import java.security.AccessController ; 15 import java.security.AccessControlContext ; 16 import java.security.DomainCombiner ; 17 import java.security.Permission ; 18 import java.security.PermissionCollection ; 19 import java.security.Principal ; 20 import java.security.PrivilegedAction ; 21 import java.security.PrivilegedExceptionAction ; 22 import java.security.PrivilegedActionException ; 23 import java.security.ProtectionDomain ; 24 import sun.security.util.ResourcesMgr; 25 import sun.security.util.SecurityConstants; 26 27 84 public final class Subject implements java.io.Serializable { 85 86 private static final long serialVersionUID = -8308522755600156056L; 87 88 98 Set principals; 99 100 104 transient Set pubCredentials; 105 transient Set privCredentials; 106 107 112 private volatile boolean readOnly = false; 113 114 private static final int PRINCIPAL_SET = 1; 115 private static final int PUB_CREDENTIAL_SET = 2; 116 private static final int PRIV_CREDENTIAL_SET = 3; 117 118 135 public Subject() { 136 137 this.principals = Collections.synchronizedSet 138 (new SecureSet(this, PRINCIPAL_SET)); 139 this.pubCredentials = Collections.synchronizedSet 140 (new SecureSet(this, PUB_CREDENTIAL_SET)); 141 this.privCredentials = Collections.synchronizedSet 142 (new SecureSet(this, PRIV_CREDENTIAL_SET)); 143 } 144 145 180 public Subject(boolean readOnly, Set<? extends Principal > principals, 181 Set<?> pubCredentials, Set<?> privCredentials) 182 { 183 184 if (principals == null || 185 pubCredentials == null || 186 privCredentials == null) 187 throw new NullPointerException  188 (ResourcesMgr.getString("invalid null input(s)")); 189 190 this.principals = Collections.synchronizedSet(new SecureSet 191 (this, PRINCIPAL_SET, principals)); 192 this.pubCredentials = Collections.synchronizedSet(new SecureSet 193 (this, PUB_CREDENTIAL_SET, pubCredentials)); 194 this.privCredentials = Collections.synchronizedSet(new SecureSet 195 (this, PRIV_CREDENTIAL_SET, privCredentials)); 196 this.readOnly = readOnly; 197 } 198 199 219 public void setReadOnly() { 220 java.lang.SecurityManager sm = System.getSecurityManager(); 221 if (sm != null) { 222 sm.checkPermission(new AuthPermission ("setReadOnly")); 223 } 224 225 this.readOnly = true; 226 } 227 228 235 public boolean isReadOnly() { 236 return this.readOnly; 237 } 238 239 264 public static Subject getSubject(final AccessControlContext acc) { 265 266 java.lang.SecurityManager sm = System.getSecurityManager(); 267 if (sm != null) { 268 sm.checkPermission(new AuthPermission ("getSubject")); 269 } 270 271 if (acc == null) { 272 throw new NullPointerException (ResourcesMgr.getString 273 ("invalid null AccessControlContext provided")); 274 } 275 276 return (Subject )AccessController.doPrivileged 278 (new java.security.PrivilegedAction () { 279 public Object run() { 280 DomainCombiner dc = acc.getDomainCombiner(); 281 if (!(dc instanceof SubjectDomainCombiner )) 282 return null; 283 SubjectDomainCombiner sdc = (SubjectDomainCombiner )dc; 284 return sdc.getSubject(); 285 } 286 }); 287 } 288 289 321 public static Object doAs(final Subject subject, 322 final java.security.PrivilegedAction action) { 323 324 java.lang.SecurityManager sm = System.getSecurityManager(); 325 if (sm != null) { 326 sm.checkPermission(SecurityConstants.DO_AS_PERMISSION); 327 } 328 if (action == null) 329 throw new NullPointerException  330 (ResourcesMgr.getString("invalid null action provided")); 331 332 final AccessControlContext currentAcc = AccessController.getContext(); 335 336 return java.security.AccessController.doPrivileged 338 (action, 339 createContext(subject, currentAcc)); 340 } 341 342 379 public static Object doAs(final Subject subject, 380 final java.security.PrivilegedExceptionAction action) 381 throws java.security.PrivilegedActionException { 382 383 java.lang.SecurityManager sm = System.getSecurityManager(); 384 if (sm != null) { 385 sm.checkPermission(SecurityConstants.DO_AS_PERMISSION); 386 } 387 388 if (action == null) 389 throw new NullPointerException  390 (ResourcesMgr.getString("invalid null action provided")); 391 392 final AccessControlContext currentAcc = AccessController.getContext(); 394 395 return java.security.AccessController.doPrivileged 397 (action, 398 createContext(subject, currentAcc)); 399 } 400 401 433 public static Object doAsPrivileged(final Subject subject, 434 final java.security.PrivilegedAction action, 435 final java.security.AccessControlContext acc) { 436 437 java.lang.SecurityManager sm = System.getSecurityManager(); 438 if (sm != null) { 439 sm.checkPermission(SecurityConstants.DO_AS_PRIVILEGED_PERMISSION); 440 } 441 442 if (action == null) 443 throw new NullPointerException  444 (ResourcesMgr.getString("invalid null action provided")); 445 446 final AccessControlContext callerAcc = 449 (acc == null ? 450 new AccessControlContext (new ProtectionDomain [0]) : 451 acc); 452 453 return java.security.AccessController.doPrivileged 455 (action, 456 createContext(subject, callerAcc)); 457 } 458 459 496 public static Object doAsPrivileged(final Subject subject, 497 final java.security.PrivilegedExceptionAction action, 498 final java.security.AccessControlContext acc) 499 throws java.security.PrivilegedActionException { 500 501 java.lang.SecurityManager sm = System.getSecurityManager(); 502 if (sm != null) { 503 sm.checkPermission(SecurityConstants.DO_AS_PRIVILEGED_PERMISSION); 504 } 505 506 if (action == null) 507 throw new NullPointerException  508 (ResourcesMgr.getString("invalid null action provided")); 509 510 final AccessControlContext callerAcc = 512 (acc == null ? 513 new AccessControlContext (new ProtectionDomain [0]) : 514 acc); 515 516 return java.security.AccessController.doPrivileged 518 (action, 519 createContext(subject, callerAcc)); 520 } 521 522 private static AccessControlContext createContext(final Subject subject, 523 final AccessControlContext acc) { 524 525 526 return (AccessControlContext ) 527 java.security.AccessController.doPrivileged 528 (new java.security.PrivilegedAction () { 529 public Object run() { 530 if (subject == null) 531 return new AccessControlContext (acc, null); 532 else 533 return new AccessControlContext  534 (acc, 535 new SubjectDomainCombiner (subject)); 536 } 537 }); 538 } 539 540 555 public Set<Principal > getPrincipals() { 556 557 return principals; 560 } 561 562 584 public <T extends Principal > Set<T> getPrincipals(Class <T> c) { 585 586 if (c == null) 587 throw new NullPointerException  588 (ResourcesMgr.getString("invalid null Class provided")); 589 590 return new ClassSet(PRINCIPAL_SET, c); 593 } 594 595 609 public Set<Object > getPublicCredentials() { 610 611 return pubCredentials; 614 } 615 616 641 public Set<Object > getPrivateCredentials() { 642 643 651 return privCredentials; 654 } 655 656 678 public <T> Set<T> getPublicCredentials(Class <T> c) { 679 680 if (c == null) 681 throw new NullPointerException  682 (ResourcesMgr.getString("invalid null Class provided")); 683 684 return new ClassSet<T>(PUB_CREDENTIAL_SET, c); 687 } 688 689 715 public <T> Set<T> getPrivateCredentials(Class <T> c) { 716 717 725 if (c == null) 726 throw new NullPointerException  727 (ResourcesMgr.getString("invalid null Class provided")); 728 729 return new ClassSet<T>(PRIV_CREDENTIAL_SET, c); 732 } 733 734 755 public boolean equals(Object o) { 756 757 if (o == null) 758 return false; 759 760 if (this == o) 761 return true; 762 763 if (o instanceof Subject ) { 764 765 final Subject that = (Subject )o; 766 767 Set thatPrincipals; 769 synchronized(that.principals) { 770 thatPrincipals = new HashSet(that.principals); 772 } 773 if (!principals.equals(thatPrincipals)) { 774 return false; 775 } 776 777 Set thatPubCredentials; 778 synchronized(that.pubCredentials) { 779 thatPubCredentials = new HashSet(that.pubCredentials); 781 } 782 if (!pubCredentials.equals(thatPubCredentials)) { 783 return false; 784 } 785 786 Set thatPrivCredentials; 787 synchronized(that.privCredentials) { 788 thatPrivCredentials = new HashSet(that.privCredentials); 790 } 791 if (!privCredentials.equals(thatPrivCredentials)) { 792 return false; 793 } 794 return true; 795 } 796 return false; 797 } 798 799 806 public String toString() { 807 return toString(true); 808 } 809 810 815 String toString(boolean includePrivateCredentials) { 816 817 String s = new String (ResourcesMgr.getString("Subject:\n")); 818 String suffix = new String (); 819 820 synchronized(principals) { 821 Iterator pI = principals.iterator(); 822 while (pI.hasNext()) { 823 Principal p = (Principal )pI.next(); 824 suffix = suffix + ResourcesMgr.getString("\tPrincipal: ") + 825 p.toString() + ResourcesMgr.getString("\n"); 826 } 827 } 828 829 synchronized(pubCredentials) { 830 Iterator pI = pubCredentials.iterator(); 831 while (pI.hasNext()) { 832 Object o = pI.next(); 833 suffix = suffix + 834 ResourcesMgr.getString("\tPublic Credential: ") + 835 o.toString() + ResourcesMgr.getString("\n"); 836 } 837 } 838 839 if (includePrivateCredentials) { 840 synchronized(privCredentials) { 841 Iterator pI = privCredentials.iterator(); 842 while (pI.hasNext()) { 843 try { 844 Object o = pI.next(); 845 suffix += ResourcesMgr.getString 846 ("\tPrivate Credential: ") + 847 o.toString() + 848 ResourcesMgr.getString("\n"); 849 } catch (SecurityException se) { 850 suffix += ResourcesMgr.getString 851 ("\tPrivate Credential inaccessible\n"); 852 break; 853 } 854 } 855 } 856 } 857 return s + suffix; 858 } 859 860 870 public int hashCode() { 871 872 882 883 int hashCode = 0; 884 885 synchronized(principals) { 886 Iterator pIterator = principals.iterator(); 887 while (pIterator.hasNext()) { 888 Principal p = (Principal )pIterator.next(); 889 hashCode ^= p.hashCode(); 890 } 891 } 892 893 synchronized(pubCredentials) { 894 Iterator pubCIterator = pubCredentials.iterator(); 895 while (pubCIterator.hasNext()) { 896 hashCode ^= getCredHashCode(pubCIterator.next()); 897 } 898 } 899 return hashCode; 900 } 901 902 905 private int getCredHashCode(Object o) { 906 try { 907 return o.hashCode(); 908 } catch (IllegalStateException ise) { 909 return o.getClass().toString().hashCode(); 910 } 911 } 912 913 916 private void writeObject(java.io.ObjectOutputStream oos) 917 throws java.io.IOException { 918 synchronized(principals) { 919 oos.defaultWriteObject(); 920 } 921 } 922 923 926 private void readObject(java.io.ObjectInputStream s) 927 <
|