1 7 8 package java.security; 9 10 import java.lang.reflect.*; 11 import java.util.*; 12 import java.util.concurrent.ConcurrentHashMap ; 13 import java.io.*; 14 import java.net.URL ; 15 import sun.security.util.Debug; 16 import sun.security.util.PropertyExpander; 17 18 import java.security.Provider.Service; 19 20 import sun.security.jca.*; 21 22 29 30 public final class Security { 31 32 33 private static final Debug sdebug = 34 Debug.getInstance("properties"); 35 36 37 private static Properties props; 38 39 private static class ProviderProperty { 41 String className; 42 Provider provider; 43 } 44 45 static { 46 AccessController.doPrivileged(new PrivilegedAction () { 51 public Object run() { 52 initialize(); 53 return null; 54 } 55 }); 56 } 57 58 private static void initialize() { 59 props = new Properties(); 60 boolean loadedProps = false; 61 boolean overrideAll = false; 62 63 File propFile = securityPropFile("java.security"); 66 if (propFile.exists()) { 67 try { 68 FileInputStream fis = new FileInputStream(propFile); 69 InputStream is = new BufferedInputStream(fis); 70 props.load(is); 71 is.close(); 72 loadedProps = true; 73 74 if (sdebug != null) { 75 sdebug.println("reading security properties file: " + 76 propFile); 77 } 78 } catch (IOException e) { 79 if (sdebug != null) { 80 sdebug.println("unable to load security properties from " + 81 propFile); 82 e.printStackTrace(); 83 } 84 } 85 } 86 87 if ("true".equalsIgnoreCase(props.getProperty 88 ("security.overridePropertiesFile"))) { 89 90 String extraPropFile = System.getProperty 91 ("java.security.properties"); 92 if (extraPropFile != null && extraPropFile.startsWith("=")) { 93 overrideAll = true; 94 extraPropFile = extraPropFile.substring(1); 95 } 96 97 if (overrideAll) { 98 props = new Properties(); 99 if (sdebug != null) { 100 sdebug.println 101 ("overriding other security properties files!"); 102 } 103 } 104 105 if (extraPropFile != null) { 108 try { 109 URL propURL; 110 111 extraPropFile = PropertyExpander.expand(extraPropFile); 112 propFile = new File(extraPropFile); 113 if (propFile.exists()) { 114 propURL = new URL 115 ("file:" + propFile.getCanonicalPath()); 116 } else { 117 propURL = new URL (extraPropFile); 118 } 119 BufferedInputStream bis = new BufferedInputStream 120 (propURL.openStream()); 121 props.load(bis); 122 bis.close(); 123 loadedProps = true; 124 125 if (sdebug != null) { 126 sdebug.println("reading security properties file: " + 127 propURL); 128 if (overrideAll) { 129 sdebug.println 130 ("overriding other security properties files!"); 131 } 132 } 133 } catch (Exception e) { 134 if (sdebug != null) { 135 sdebug.println 136 ("unable to load security properties from " + 137 extraPropFile); 138 e.printStackTrace(); 139 } 140 } 141 } 142 } 143 144 if (!loadedProps) { 145 initializeStatic(); 146 if (sdebug != null) { 147 sdebug.println("unable to load security properties " + 148 "-- using defaults"); 149 } 150 } 151 152 } 153 154 158 private static void initializeStatic() { 159 props.put("security.provider.1", "sun.security.provider.Sun"); 160 props.put("security.provider.2", "sun.security.rsa.SunRsaSign"); 161 props.put("security.provider.3", "com.sun.net.ssl.internal.ssl.Provider"); 162 props.put("security.provider.4", "com.sun.crypto.provider.SunJCE"); 163 props.put("security.provider.5", "sun.security.jgss.SunProvider"); 164 props.put("security.provider.6", "com.sun.security.sasl.Provider"); 165 } 166 167 170 private Security() { 171 } 172 173 private static File securityPropFile(String filename) { 174 String sep = File.separator; 177 return new File(System.getProperty("java.home") + sep + "lib" + sep + 178 "security" + sep + filename); 179 } 180 181 188 private static ProviderProperty getProviderProperty(String key) { 189 ProviderProperty entry = null; 190 191 List providers = Providers.getProviderList().providers(); 192 for (int i = 0; i < providers.size(); i++) { 193 194 String matchKey = null; 195 Provider prov = (Provider )providers.get(i); 196 String prop = prov.getProperty(key); 197 198 if (prop == null) { 199 for (Enumeration e = prov.keys(); 202 e.hasMoreElements() && prop == null; ) { 203 matchKey = (String )e.nextElement(); 204 if (key.equalsIgnoreCase(matchKey)) { 205 prop = prov.getProperty(matchKey); 206 break; 207 } 208 } 209 } 210 211 if (prop != null) { 212 ProviderProperty newEntry = new ProviderProperty(); 213 newEntry.className = prop; 214 newEntry.provider = prov; 215 return newEntry; 216 } 217 } 218 219 return entry; 220 } 221 222 225 private static String getProviderProperty(String key, Provider provider) { 226 String prop = provider.getProperty(key); 227 if (prop == null) { 228 for (Enumeration e = provider.keys(); 231 e.hasMoreElements() && prop == null; ) { 232 String matchKey = (String )e.nextElement(); 233 if (key.equalsIgnoreCase(matchKey)) { 234 prop = provider.getProperty(matchKey); 235 break; 236 } 237 } 238 } 239 return prop; 240 } 241 242 265 @Deprecated 266 public static String getAlgorithmProperty(String algName, 267 String propName) { 268 ProviderProperty entry = getProviderProperty("Alg." + propName 269 + "." + algName); 270 if (entry != null) { 271 return entry.className; 272 } else { 273 return null; 274 } 275 } 276 277 322 public static synchronized int insertProviderAt(Provider provider, 323 int position) { 324 String providerName = provider.getName(); 325 check("insertProvider." + providerName); 326 ProviderList list = Providers.getFullProviderList(); 327 ProviderList newList = ProviderList.insertAt(list, provider, position - 1); 328 if (list == newList) { 329 return -1; 330 } 331 Providers.setProviderList(newList); 332 return newList.getIndex(providerName) + 1; 333 } 334 335 366 public static int addProvider(Provider provider) { 367 373 return insertProviderAt(provider, 0); 374 } 375 376 408 public static synchronized void removeProvider(String name) { 409 check("removeProvider." + name); 410 ProviderList list = Providers.getFullProviderList(); 411 ProviderList newList = ProviderList.remove(list, name); 412 Providers.setProviderList(newList); 413 } 414 415 421 public static Provider [] getProviders() { 422 return Providers.getFullProviderList().toArray(); 423 } 424 425 437 public static Provider getProvider(String name) { 438 return Providers.getProviderList().getProvider(name); 439 } 440 441 499 public static Provider [] getProviders(String filter) { 500 String key = null; 501 String value = null; 502 int index = filter.indexOf(':'); 503 504 if (index == -1) { 505 key = filter; 506 value = ""; 507 } else { 508 key = filter.substring(0, index); 509 value = filter.substring(index + 1); 510 } 511 512 Hashtable hashtableFilter = new Hashtable(1); 513 hashtableFilter.put(key, value); 514 515 return (getProviders(hashtableFilter)); 516 } 517 518 566 public static Provider [] getProviders(Map <String ,String > filter) { 567 Provider [] allProviders = Security.getProviders(); 570 Set keySet = filter.keySet(); 571 LinkedHashSet candidates = new LinkedHashSet(5); 572 573 if ((keySet == null) || (allProviders == null)) { 576 return allProviders; 577 } 578 579 boolean firstSearch = true; 580 581 for (Iterator ite = keySet.iterator(); ite.hasNext(); ) { 584 String key = (String )ite.next(); 585 String value = (String )filter.get(key); 586 587 LinkedHashSet newCandidates = getAllQualifyingCandidates(key, value, 588 allProviders); 589 if (firstSearch) { 590 candidates = newCandidates; 591 firstSearch = false; 592 } 593 594 if ((newCandidates != null) && !newCandidates.isEmpty()) { 595 for (Iterator cansIte = candidates.iterator(); 599 cansIte.hasNext(); ) { 600 Provider prov = (Provider )cansIte.next(); 601 if (!newCandidates.contains(prov)) { 602 cansIte.remove(); 603 } 604 } 605 } else { 606 candidates = null; 607 break; 608 } 609 } 610 611 if ((candidates == null) || (candidates.isEmpty())) 612 return null; 613 614 Object [] candidatesArray = candidates.toArray(); 615 Provider [] result = new Provider [candidatesArray.length]; 616 617 for (int i = 0; i < result.length; i++) { 618 result[i] = (Provider )candidatesArray[i]; 619 } 620 621 return result; 622 } 623 624 private static final Map <String ,Class > spiMap = 626 new ConcurrentHashMap <String ,Class >(); 627 628 633 private static Class getSpiClass(String type) { 634 Class clazz = spiMap.get(type); 635 if (clazz != null) { 636 return clazz; 637 } 638 try { 639 clazz = Class.forName("java.security." + type + "Spi"); 640 spiMap.put(type, clazz); 641 return clazz; 642 } catch (ClassNotFoundException e) { 643 throw (Error )new AssertionError ("Spi class not found").initCause(e); 644 } 645 } 646 647 655 static Object [] getImpl(String algorithm, String type, String provider) 656 throws NoSuchAlgorithmException , NoSuchProviderException { 657 if (provider == null) { 658 return GetInstance.getInstance 659 (type, getSpiClass(type), algorithm).toArray(); 660 } else { 661 return GetInstance.getInstance 662 (type, getSpiClass(type), algorithm, provider).toArray(); 663 } 664 } 665 666 static Object [] getImpl(String algorithm, String type, String provider, 667 Object params) throws NoSuchAlgorithmException , 668 NoSuchProviderException , InvalidAlgorithmParameterException { 669 if (provider == null) { 670 return GetInstance.getInstance 671 (type, getSpiClass(type), algorithm, params).toArray(); 672 } else { 673 return GetInstance.getInstance 674 (type, getSpiClass(type), algorithm, params, provider).toArray(); 675 } 676 } 677 678 685 static Object [] getImpl(String algorithm, String type, Provider provider) 686 throws NoSuchAlgorithmException { 687 return GetInstance.getInstance 688 (type, getSpiClass(type), algorithm, provider).toArray(); 689 } 690 691 static Object [] getImpl(String algorithm, String type, Provider provider, 692 Object params) throws NoSuchAlgorithmException , 693 InvalidAlgorithmParameterException { 694 return GetInstance.getInstance 695 (type, getSpiClass(type), algorithm, params, provider).toArray(); 696 } 697 698 721 public static String getProperty(String key) { 722 SecurityManager sm = System.getSecurityManager(); 723 if (sm != null) { 724 sm.checkPermission(new SecurityPermission ("getProperty."+ 725 key)); 726 } 727 String name = props.getProperty(key); 728 if (name != null) 729 name = name.trim(); return name; 731 } 732 733 755 public static void setProperty(String key, String datum) { 756 check("setProperty."+key); 757 props.put(key, datum); 758 invalidateSMCache(key); 759 } 760 761 771 private static void invalidateSMCache(String key) { 772 773 final boolean pa = key.equals("package.access"); 774 final boolean pd = key.equals("package.definition"); 775 776 if (pa || pd) { 777 AccessController.doPrivileged(new PrivilegedAction () { 778 public Object run() { 779 try { 780 781 Class cl = Class.forName( 782 "java.lang.SecurityManager", false, null); 783 Field f = null; 784 boolean accessible = false; 785 786 if (pa) { 787 f = cl.getDeclaredField("packageAccessValid"); 788 accessible = f.isAccessible(); 789 f.setAccessible(true); 790 } else { 791 f = cl.getDeclaredField("packageDefinitionValid"); 792 accessible = f.isAccessible(); 793 f.setAccessible(true); 794 } 795 f.setBoolean(f, false); 796 f.setAccessible(accessible); 797 } 798 catch (Exception e1) { 799 808 } 809 return null; 810 } 811 }); 812 } 813 } 814 815 private static void check(String directive) { 816 SecurityManager security = System.getSecurityManager(); 817 if (security != null) { 818 security.checkSecurityAccess(directive); 819 } 820 } 821 822 826 private static LinkedHashSet getAllQualifyingCandidates(String filterKey, 827 String filterValue, 828 Provider [] allProviders) { 829 String [] filterComponents = getFilterComponents(filterKey, 830 filterValue); 831 832 String serviceName = filterComponents[0]; 836 String algName = filterComponents[1]; 837 String attrName = filterComponents[2]; 838 839 return getProvidersNotUsingCache(serviceName, algName, attrName, 840 filterValue, allProviders); 841 } 842 843 private static LinkedHashSet getProvidersNotUsingCache(String serviceName, 844 String algName, 845 String attrName, 846 String filterValue, 847 Provider [] allProviders) { 848 LinkedHashSet candidates = new LinkedHashSet(5); 849 for (int i = 0; i < allProviders.length; i++) { 850 if (isCriterionSatisfied(allProviders[i], serviceName, 851 algName, 852 attrName, filterValue)) { 853 candidates.add(allProviders[i]); 854 } 855 } 856 return candidates; 857 } 858 859 863 private static boolean isCriterionSatisfied(Provider prov, 864 String serviceName, 865 String algName, 866 String attrName, 867 String filterValue) { 868 String key = serviceName + '.' + algName; 869 870 if (attrName != null) { 871 key += ' ' + attrName; 872 } 873 String propValue = getProviderProperty(key, prov); 876 877 if (propValue == null) { 878 String standardName = getProviderProperty("Alg.Alias." + 881 serviceName + "." + 882 algName, 883 prov); 884 if (standardName != null) { 885 key = serviceName + "." + standardName; 886 887 if (attrName != null) { 888 key += ' ' + attrName; 889 } 890 891 propValue = getProviderProperty(key, prov); 892 } 893 894 if (propValue == null) { 895 return false; 898 } 899 } 900 901 905 if (attrName == null) { 906 return true; 907 } 908 909 if (isStandardAttr(attrName)) { 912 return isConstraintSatisfied(attrName, filterValue, propValue); 913 } else { 914 return filterValue.equalsIgnoreCase(propValue); 915 } 916 } 917 918 922 private static boolean isStandardAttr(String attribute) { 923 if (attribute.equalsIgnoreCase("KeySize")) 926 return true; 927 928 if (attribute.equalsIgnoreCase("ImplementedIn")) 929 return true; 930 931 return false; 932 } 933 934 938 private static boolean isConstraintSatisfied(String attribute, 939 String value, 940 String prop) { 941 if (attribute.equalsIgnoreCase("KeySize")) { 944 int requestedSize = Integer.parseInt(value); 945 int maxSize = Integer.parseInt(prop); 946 if (requestedSize <= maxSize) { 947 return true; 948 } else { 949 return false; 950 } 951 } 952 953 if (attribute.equalsIgnoreCase("ImplementedIn")) { 956 return value.equalsIgnoreCase(prop); 957 } 958 959 return false; 960 } 961 962 static String [] getFilterComponents(String filterKey, String filterValue) { 963 int algIndex = filterKey.indexOf('.'); 964 965 if (algIndex < 0) { 966 throw new InvalidParameterException ("Invalid filter"); 969 } 970 971 String serviceName = filterKey.substring(0, algIndex); 972 String algName = null; 973 String attrName = null; 974 975 if (filterValue.length() == 0) { 976 algName = filterKey.substring(algIndex + 1).trim(); 979 if (algName.length() == 0) { 980 throw new InvalidParameterException ("Invalid filter"); 982 } 983 } else { 984 int attrIndex = filterKey.indexOf(' '); 988 989 if (attrIndex == -1) { 990 throw new InvalidParameterException ("Invalid filter"); 992 } else { 993 attrName = filterKey.substring(attrIndex + 1).trim(); 994 if (attrName.length() == 0) { 995 throw new InvalidParameterException ("Invalid filter"); 997 } 998 } 999 1000 if ((attrIndex < algIndex) || 1002 (algIndex == attrIndex - 1)) { 1003 throw new InvalidParameterException ("Invalid filter"); 1004 } else { 1005 algName = filterKey.substring(algIndex + 1, attrIndex); 1006 } 1007 } 1008 1009 String [] result = new String [3]; 1010 result[0] = serviceName; 1011 result[1] = algName; 1012 result[2] = attrName; 1013 1014 return result; 1015 } 1016 1017 1038 public static Set<String > getAlgorithms(String serviceName) { 1039 1040 if ((serviceName == null) || (serviceName.length() == 0) || 1041 (serviceName.endsWith("."))) { 1042 return Collections.EMPTY_SET; 1043 } 1044 1045 HashSet result = new HashSet(); 1046 Provider [] providers = Security.getProviders(); 1047 1048 for (int i = 0; i < providers.length; i++) { 1049 for (Enumeration e = providers[i].keys(); e.hasMoreElements(); ) { 1051 String currentKey = ((String )e.nextElement()).toUpperCase(); 1052 if (currentKey.startsWith(serviceName.toUpperCase())) { 1053 if (currentKey.indexOf(" ") < 0) { 1060 result.add(currentKey.substring(serviceName.length() + 1)); 1061 } 1062 } 1063 } 1064 } 1065 return Collections.unmodifiableSet(result); 1066 } 1067} 1068 1069 | Popular Tags |