< prev index next >

src/share/classes/sun/security/provider/PolicyFile.java

Print this page
rev 1387 : 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
Reviewed-by: xuelei, mullan
Contributed-by: alexandre.boulgakov@oracle.com

   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.provider;
  27 
  28 import java.io.*;
  29 import java.lang.RuntimePermission;
  30 import java.lang.reflect.*;
  31 import java.lang.ref.*;
  32 import java.net.MalformedURLException;
  33 import java.net.URL;
  34 import java.net.URI;
  35 import java.util.*;
  36 import java.util.Enumeration;
  37 import java.util.Hashtable;
  38 import java.util.List;
  39 import java.util.StringTokenizer;
  40 import java.util.PropertyPermission;
  41 import java.util.ArrayList;
  42 import java.util.ListIterator;
  43 import java.util.WeakHashMap;
  44 import java.text.MessageFormat;
  45 import com.sun.security.auth.PrincipalComparator;
  46 import java.security.*;
  47 import java.security.cert.Certificate;
  48 import java.security.cert.X509Certificate;
  49 import javax.security.auth.PrivateCredentialPermission;
  50 import javax.security.auth.Subject;
  51 import javax.security.auth.x500.X500Principal;
  52 import java.io.FilePermission;
  53 import java.net.SocketPermission;
  54 import java.net.NetPermission;
  55 import java.util.PropertyPermission;
  56 import java.util.concurrent.atomic.AtomicReference;
  57 import java.awt.AWTPermission;
  58 /*
  59 import javax.security.auth.AuthPermission;
  60 import javax.security.auth.kerberos.ServicePermission;
  61 import javax.security.auth.kerberos.DelegationPermission;
  62 import java.io.SerializablePermission;
  63 import java.util.logging.LoggingPermission;
  64 import java.sql.SQLPermission;
  65 import java.lang.reflect.ReflectPermission;
  66 import javax.sound.sampled.AudioPermission;
  67 import javax.net.ssl.SSLPermission;
  68 */
  69 import sun.misc.JavaSecurityProtectionDomainAccess;
  70 import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
  71 import sun.misc.SharedSecrets;
  72 import sun.security.util.Password;
  73 import sun.security.util.PolicyUtil;
  74 import sun.security.util.PropertyExpander;
  75 import sun.security.util.Debug;
  76 import sun.security.util.ResourcesMgr;
  77 import sun.security.util.SecurityConstants;
  78 import sun.net.www.ParseUtil;
  79 
  80 /**
  81  * This class represents a default implementation for
  82  * <code>java.security.Policy</code>.
  83  *
  84  * Note:
  85  * For backward compatibility with JAAS 1.0 it loads
  86  * both java.auth.policy and java.policy. However it
  87  * is recommended that java.auth.policy be not used
  88  * and the java.policy contain all grant entries including
  89  * that contain principal-based entries.
  90  *
  91  *
  92  * <p> This object stores the policy for entire Java runtime,


 994                     Constructor<?> c = pc.getConstructor(PARAMS1);
 995                     return (Permission) c.newInstance(new Object[] { name});
 996                 } catch (NoSuchMethodException ne) {
 997                     Constructor<?> c = pc.getConstructor(PARAMS2);
 998                     return (Permission) c.newInstance(
 999                           new Object[] { name, actions });
1000                 }
1001             } else {
1002                 Constructor<?> c = pc.getConstructor(PARAMS2);
1003                 return (Permission) c.newInstance(
1004                       new Object[] { name, actions });
1005              }
1006         }
1007     }
1008 
1009     /**
1010      * Creates one of the well-known permissions directly instead of
1011      * via reflection. Keep list short to not penalize non-JDK-defined
1012      * permissions.
1013      */
1014     private static final Permission getKnownInstance(Class claz,
1015         String name, String actions) {
1016         // XXX shorten list to most popular ones?
1017         if (claz.equals(FilePermission.class)) {
1018             return new FilePermission(name, actions);
1019         } else if (claz.equals(SocketPermission.class)) {
1020             return new SocketPermission(name, actions);
1021         } else if (claz.equals(RuntimePermission.class)) {
1022             return new RuntimePermission(name, actions);
1023         } else if (claz.equals(PropertyPermission.class)) {
1024             return new PropertyPermission(name, actions);
1025         } else if (claz.equals(NetPermission.class)) {
1026             return new NetPermission(name, actions);
1027         } else if (claz.equals(AllPermission.class)) {
1028             return SecurityConstants.ALL_PERMISSION;
1029         } else if (claz.equals(AWTPermission.class)) {
1030             return new AWTPermission(name, actions);
1031 /*
1032         } else if (claz.equals(ReflectPermission.class)) {
1033             return new ReflectPermission(name, actions);
1034         } else if (claz.equals(SecurityPermission.class)) {


1337         }
1338 
1339         // check to see if the Principals imply
1340 
1341         List<PolicyParser.PrincipalEntry> entryPs = entry.getPrincipals();
1342         if (debug != null) {
1343             ArrayList<PolicyParser.PrincipalEntry> accPs =
1344                         new ArrayList<PolicyParser.PrincipalEntry>();
1345             if (principals != null) {
1346                 for (int i = 0; i < principals.length; i++) {
1347                     accPs.add(new PolicyParser.PrincipalEntry
1348                                         (principals[i].getClass().getName(),
1349                                         principals[i].getName()));
1350                 }
1351             }
1352             debug.println("evaluate principals:\n" +
1353                 "\tPolicy Principals: " + entryPs + "\n" +
1354                 "\tActive Principals: " + accPs);
1355         }
1356 
1357         if (entryPs == null || entryPs.size() == 0) {
1358 
1359             // policy entry has no principals -
1360             // add perms regardless of principals in current ACC
1361 
1362             addPerms(perms, principals, entry);
1363             if (debug != null) {
1364                 debug.println("evaluation (codesource/principals) passed");
1365             }
1366             return;
1367 
1368         } else if (principals == null || principals.length == 0) {
1369 
1370             // current thread has no principals but this policy entry
1371             // has principals - perms are not added
1372 
1373             if (debug != null) {
1374                 debug.println("evaluation (principals) failed");
1375             }
1376             return;
1377         }


1539     }
1540 
1541     /**
1542      * <p>
1543      *
1544      * @param sp the SelfPermission that needs to be expanded <p>
1545      *
1546      * @param entryPs list of principals for the Policy entry.
1547      *
1548      * @param pdp Principal array from the current ProtectionDomain.
1549      *
1550      * @param perms the PermissionCollection where the individual
1551      *                  Permissions will be added after expansion.
1552      */
1553 
1554     private void expandSelf(SelfPermission sp,
1555                             List<PolicyParser.PrincipalEntry> entryPs,
1556                             Principal[] pdp,
1557                             Permissions perms) {
1558 
1559         if (entryPs == null || entryPs.size() == 0) {
1560             // No principals in the grant to substitute
1561             if (debug != null) {
1562                 debug.println("Ignoring permission "
1563                                 + sp.getSelfType()
1564                                 + " with target name ("
1565                                 + sp.getSelfName() + ").  "
1566                                 + "No Principal(s) specified "
1567                                 + "in the grant clause.  "
1568                                 + "SELF-based target names are "
1569                                 + "only valid in the context "
1570                                 + "of a Principal-based grant entry."
1571                              );
1572             }
1573             return;
1574         }
1575         int startIndex = 0;
1576         int v;
1577         StringBuilder sb = new StringBuilder();
1578         while ((v = sp.getSelfName().indexOf(SELF, startIndex)) != -1) {
1579 


1866                 else
1867                     palBuf.append(")");
1868             }
1869             pals = palBuf.toString();
1870         }
1871         return "PD CodeSource: "
1872                 + pd.getCodeSource()
1873                 +"\n\t" + "PD ClassLoader: "
1874                 + pd.getClassLoader()
1875                 +"\n\t" + "PD Principals: "
1876                 + pals;
1877     }
1878 
1879     /**
1880      * return true if no replacement was performed,
1881      * or if replacement succeeded.
1882      */
1883     private boolean replacePrincipals(
1884         List<PolicyParser.PrincipalEntry> principals, KeyStore keystore) {
1885 
1886         if (principals == null || principals.size() == 0 || keystore == null)
1887             return true;
1888 
1889         ListIterator<PolicyParser.PrincipalEntry> i = principals.listIterator();
1890         while (i.hasNext()) {
1891             PolicyParser.PrincipalEntry pppe = i.next();
1892             if (pppe.principalClass.equals(PolicyParser.REPLACE_NAME)) {
1893 
1894                 // perform replacement
1895                 // (only X509 replacement is possible now)
1896                 String name;
1897                 if ((name = getDN(pppe.principalName, keystore)) == null) {
1898                     return false;
1899                 }
1900 
1901                 if (debug != null) {
1902                     debug.println("  Replacing \"" +
1903                         pppe.principalName +
1904                         "\" with " +
1905                         X500PRINCIPAL + "/\"" +
1906                         name +


2455          */
2456         @Override public String toString() {
2457             return "(SelfPermission " + type + " " + name + " " + actions + ")";
2458         }
2459     }
2460 
2461     /**
2462      * holds policy information that we need to synch on
2463      */
2464     private static class PolicyInfo {
2465         private static final boolean verbose = false;
2466 
2467         // Stores grant entries in the policy
2468         final List<PolicyEntry> policyEntries;
2469 
2470         // Stores grant entries gotten from identity database
2471         // Use separate lists to avoid sync on policyEntries
2472         final List<PolicyEntry> identityPolicyEntries;
2473 
2474         // Maps aliases to certs
2475         final Map aliasMapping;
2476 
2477         // Maps ProtectionDomain to PermissionCollection
2478         private final ProtectionDomainCache[] pdMapping;
2479         private java.util.Random random;
2480 
2481         PolicyInfo(int numCaches) {
2482             policyEntries = new ArrayList<PolicyEntry>();
2483             identityPolicyEntries =
2484                 Collections.synchronizedList(new ArrayList<PolicyEntry>(2));
2485             aliasMapping = Collections.synchronizedMap(new HashMap(11));

2486 
2487             pdMapping = new ProtectionDomainCache[numCaches];
2488             JavaSecurityProtectionDomainAccess jspda
2489                 = SharedSecrets.getJavaSecurityProtectionDomainAccess();
2490             for (int i = 0; i < numCaches; i++) {
2491                 pdMapping[i] = jspda.getProtectionDomainCache();
2492             }
2493             if (numCaches > 1) {
2494                 random = new java.util.Random();
2495             }
2496         }
2497         ProtectionDomainCache getPdMapping() {
2498             if (pdMapping.length == 1) {
2499                 return pdMapping[0];
2500             } else {
2501                 int i = java.lang.Math.abs(random.nextInt() % pdMapping.length);
2502                 return pdMapping[i];
2503             }
2504         }
2505     }

   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.provider;
  27 
  28 import java.io.*;

  29 import java.lang.reflect.*;

  30 import java.net.MalformedURLException;
  31 import java.net.URL;
  32 import java.net.URI;
  33 import java.util.*;
  34 import java.util.Enumeration;

  35 import java.util.List;
  36 import java.util.StringTokenizer;

  37 import java.util.ArrayList;
  38 import java.util.ListIterator;

  39 import java.text.MessageFormat;
  40 import com.sun.security.auth.PrincipalComparator;
  41 import java.security.*;
  42 import java.security.cert.Certificate;
  43 import java.security.cert.X509Certificate;

  44 import javax.security.auth.Subject;
  45 import javax.security.auth.x500.X500Principal;
  46 import java.io.FilePermission;
  47 import java.net.SocketPermission;
  48 import java.net.NetPermission;
  49 import java.util.PropertyPermission;
  50 import java.util.concurrent.atomic.AtomicReference;
  51 import java.awt.AWTPermission;
  52 /*
  53 import javax.security.auth.AuthPermission;
  54 import javax.security.auth.kerberos.ServicePermission;
  55 import javax.security.auth.kerberos.DelegationPermission;
  56 import java.io.SerializablePermission;
  57 import java.util.logging.LoggingPermission;
  58 import java.sql.SQLPermission;
  59 import java.lang.reflect.ReflectPermission;
  60 import javax.sound.sampled.AudioPermission;
  61 import javax.net.ssl.SSLPermission;
  62 */
  63 import sun.misc.JavaSecurityProtectionDomainAccess;
  64 import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
  65 import sun.misc.SharedSecrets;

  66 import sun.security.util.PolicyUtil;
  67 import sun.security.util.PropertyExpander;
  68 import sun.security.util.Debug;
  69 import sun.security.util.ResourcesMgr;
  70 import sun.security.util.SecurityConstants;
  71 import sun.net.www.ParseUtil;
  72 
  73 /**
  74  * This class represents a default implementation for
  75  * <code>java.security.Policy</code>.
  76  *
  77  * Note:
  78  * For backward compatibility with JAAS 1.0 it loads
  79  * both java.auth.policy and java.policy. However it
  80  * is recommended that java.auth.policy be not used
  81  * and the java.policy contain all grant entries including
  82  * that contain principal-based entries.
  83  *
  84  *
  85  * <p> This object stores the policy for entire Java runtime,


 987                     Constructor<?> c = pc.getConstructor(PARAMS1);
 988                     return (Permission) c.newInstance(new Object[] { name});
 989                 } catch (NoSuchMethodException ne) {
 990                     Constructor<?> c = pc.getConstructor(PARAMS2);
 991                     return (Permission) c.newInstance(
 992                           new Object[] { name, actions });
 993                 }
 994             } else {
 995                 Constructor<?> c = pc.getConstructor(PARAMS2);
 996                 return (Permission) c.newInstance(
 997                       new Object[] { name, actions });
 998              }
 999         }
1000     }
1001 
1002     /**
1003      * Creates one of the well-known permissions directly instead of
1004      * via reflection. Keep list short to not penalize non-JDK-defined
1005      * permissions.
1006      */
1007     private static final Permission getKnownInstance(Class<?> claz,
1008         String name, String actions) {
1009         // XXX shorten list to most popular ones?
1010         if (claz.equals(FilePermission.class)) {
1011             return new FilePermission(name, actions);
1012         } else if (claz.equals(SocketPermission.class)) {
1013             return new SocketPermission(name, actions);
1014         } else if (claz.equals(RuntimePermission.class)) {
1015             return new RuntimePermission(name, actions);
1016         } else if (claz.equals(PropertyPermission.class)) {
1017             return new PropertyPermission(name, actions);
1018         } else if (claz.equals(NetPermission.class)) {
1019             return new NetPermission(name, actions);
1020         } else if (claz.equals(AllPermission.class)) {
1021             return SecurityConstants.ALL_PERMISSION;
1022         } else if (claz.equals(AWTPermission.class)) {
1023             return new AWTPermission(name, actions);
1024 /*
1025         } else if (claz.equals(ReflectPermission.class)) {
1026             return new ReflectPermission(name, actions);
1027         } else if (claz.equals(SecurityPermission.class)) {


1330         }
1331 
1332         // check to see if the Principals imply
1333 
1334         List<PolicyParser.PrincipalEntry> entryPs = entry.getPrincipals();
1335         if (debug != null) {
1336             ArrayList<PolicyParser.PrincipalEntry> accPs =
1337                         new ArrayList<PolicyParser.PrincipalEntry>();
1338             if (principals != null) {
1339                 for (int i = 0; i < principals.length; i++) {
1340                     accPs.add(new PolicyParser.PrincipalEntry
1341                                         (principals[i].getClass().getName(),
1342                                         principals[i].getName()));
1343                 }
1344             }
1345             debug.println("evaluate principals:\n" +
1346                 "\tPolicy Principals: " + entryPs + "\n" +
1347                 "\tActive Principals: " + accPs);
1348         }
1349 
1350         if (entryPs == null || entryPs.isEmpty()) {
1351 
1352             // policy entry has no principals -
1353             // add perms regardless of principals in current ACC
1354 
1355             addPerms(perms, principals, entry);
1356             if (debug != null) {
1357                 debug.println("evaluation (codesource/principals) passed");
1358             }
1359             return;
1360 
1361         } else if (principals == null || principals.length == 0) {
1362 
1363             // current thread has no principals but this policy entry
1364             // has principals - perms are not added
1365 
1366             if (debug != null) {
1367                 debug.println("evaluation (principals) failed");
1368             }
1369             return;
1370         }


1532     }
1533 
1534     /**
1535      * <p>
1536      *
1537      * @param sp the SelfPermission that needs to be expanded <p>
1538      *
1539      * @param entryPs list of principals for the Policy entry.
1540      *
1541      * @param pdp Principal array from the current ProtectionDomain.
1542      *
1543      * @param perms the PermissionCollection where the individual
1544      *                  Permissions will be added after expansion.
1545      */
1546 
1547     private void expandSelf(SelfPermission sp,
1548                             List<PolicyParser.PrincipalEntry> entryPs,
1549                             Principal[] pdp,
1550                             Permissions perms) {
1551 
1552         if (entryPs == null || entryPs.isEmpty()) {
1553             // No principals in the grant to substitute
1554             if (debug != null) {
1555                 debug.println("Ignoring permission "
1556                                 + sp.getSelfType()
1557                                 + " with target name ("
1558                                 + sp.getSelfName() + ").  "
1559                                 + "No Principal(s) specified "
1560                                 + "in the grant clause.  "
1561                                 + "SELF-based target names are "
1562                                 + "only valid in the context "
1563                                 + "of a Principal-based grant entry."
1564                              );
1565             }
1566             return;
1567         }
1568         int startIndex = 0;
1569         int v;
1570         StringBuilder sb = new StringBuilder();
1571         while ((v = sp.getSelfName().indexOf(SELF, startIndex)) != -1) {
1572 


1859                 else
1860                     palBuf.append(")");
1861             }
1862             pals = palBuf.toString();
1863         }
1864         return "PD CodeSource: "
1865                 + pd.getCodeSource()
1866                 +"\n\t" + "PD ClassLoader: "
1867                 + pd.getClassLoader()
1868                 +"\n\t" + "PD Principals: "
1869                 + pals;
1870     }
1871 
1872     /**
1873      * return true if no replacement was performed,
1874      * or if replacement succeeded.
1875      */
1876     private boolean replacePrincipals(
1877         List<PolicyParser.PrincipalEntry> principals, KeyStore keystore) {
1878 
1879         if (principals == null || principals.isEmpty() || keystore == null)
1880             return true;
1881 
1882         ListIterator<PolicyParser.PrincipalEntry> i = principals.listIterator();
1883         while (i.hasNext()) {
1884             PolicyParser.PrincipalEntry pppe = i.next();
1885             if (pppe.principalClass.equals(PolicyParser.REPLACE_NAME)) {
1886 
1887                 // perform replacement
1888                 // (only X509 replacement is possible now)
1889                 String name;
1890                 if ((name = getDN(pppe.principalName, keystore)) == null) {
1891                     return false;
1892                 }
1893 
1894                 if (debug != null) {
1895                     debug.println("  Replacing \"" +
1896                         pppe.principalName +
1897                         "\" with " +
1898                         X500PRINCIPAL + "/\"" +
1899                         name +


2448          */
2449         @Override public String toString() {
2450             return "(SelfPermission " + type + " " + name + " " + actions + ")";
2451         }
2452     }
2453 
2454     /**
2455      * holds policy information that we need to synch on
2456      */
2457     private static class PolicyInfo {
2458         private static final boolean verbose = false;
2459 
2460         // Stores grant entries in the policy
2461         final List<PolicyEntry> policyEntries;
2462 
2463         // Stores grant entries gotten from identity database
2464         // Use separate lists to avoid sync on policyEntries
2465         final List<PolicyEntry> identityPolicyEntries;
2466 
2467         // Maps aliases to certs
2468         final Map<Object, Object> aliasMapping;
2469 
2470         // Maps ProtectionDomain to PermissionCollection
2471         private final ProtectionDomainCache[] pdMapping;
2472         private java.util.Random random;
2473 
2474         PolicyInfo(int numCaches) {
2475             policyEntries = new ArrayList<PolicyEntry>();
2476             identityPolicyEntries =
2477                 Collections.synchronizedList(new ArrayList<PolicyEntry>(2));
2478             aliasMapping = Collections.synchronizedMap(
2479                     new HashMap<Object, Object>(11));
2480 
2481             pdMapping = new ProtectionDomainCache[numCaches];
2482             JavaSecurityProtectionDomainAccess jspda
2483                 = SharedSecrets.getJavaSecurityProtectionDomainAccess();
2484             for (int i = 0; i < numCaches; i++) {
2485                 pdMapping[i] = jspda.getProtectionDomainCache();
2486             }
2487             if (numCaches > 1) {
2488                 random = new java.util.Random();
2489             }
2490         }
2491         ProtectionDomainCache getPdMapping() {
2492             if (pdMapping.length == 1) {
2493                 return pdMapping[0];
2494             } else {
2495                 int i = java.lang.Math.abs(random.nextInt() % pdMapping.length);
2496                 return pdMapping[i];
2497             }
2498         }
2499     }
< prev index next >