PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/classes/sun/security/provider/PolicyFile.java

#
Java | 2437 lines | 1346 code | 216 blank | 875 comment | 352 complexity | 4ed42564499ce72815c47020216da703 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  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. package sun.security.provider;
  26. import java.io.*;
  27. import java.lang.RuntimePermission;
  28. import java.lang.reflect.*;
  29. import java.lang.ref.*;
  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.Hashtable;
  36. import java.util.List;
  37. import java.util.StringTokenizer;
  38. import java.util.PropertyPermission;
  39. import java.util.ArrayList;
  40. import java.util.ListIterator;
  41. import java.util.WeakHashMap;
  42. import java.text.MessageFormat;
  43. import com.sun.security.auth.PrincipalComparator;
  44. import java.security.*;
  45. import java.security.cert.Certificate;
  46. import java.security.cert.X509Certificate;
  47. import javax.security.auth.PrivateCredentialPermission;
  48. import javax.security.auth.Subject;
  49. import javax.security.auth.x500.X500Principal;
  50. import java.io.FilePermission;
  51. import java.net.SocketPermission;
  52. import java.net.NetPermission;
  53. import java.util.PropertyPermission;
  54. import java.util.concurrent.atomic.AtomicReference;
  55. /*
  56. import javax.security.auth.AuthPermission;
  57. import javax.security.auth.kerberos.ServicePermission;
  58. import javax.security.auth.kerberos.DelegationPermission;
  59. import java.io.SerializablePermission;
  60. import java.util.logging.LoggingPermission;
  61. import java.sql.SQLPermission;
  62. import java.lang.reflect.ReflectPermission;
  63. import javax.sound.sampled.AudioPermission;
  64. import javax.net.ssl.SSLPermission;
  65. */
  66. import sun.misc.JavaSecurityProtectionDomainAccess;
  67. import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
  68. import sun.misc.SharedSecrets;
  69. import sun.security.util.Password;
  70. import sun.security.util.PolicyUtil;
  71. import sun.security.util.PropertyExpander;
  72. import sun.security.util.Debug;
  73. import sun.security.util.ResourcesMgr;
  74. import sun.security.util.SecurityConstants;
  75. import sun.net.www.ParseUtil;
  76. /**
  77. * This class represents a default implementation for
  78. * <code>java.security.Policy</code>.
  79. *
  80. * Note:
  81. * For backward compatibility with JAAS 1.0 it loads
  82. * both java.auth.policy and java.policy. However it
  83. * is recommended that java.auth.policy be not used
  84. * and the java.policy contain all grant entries including
  85. * that contain principal-based entries.
  86. *
  87. *
  88. * <p> This object stores the policy for entire Java runtime,
  89. * and is the amalgamation of multiple static policy
  90. * configurations that resides in files.
  91. * The algorithm for locating the policy file(s) and reading their
  92. * information into this <code>Policy</code> object is:
  93. *
  94. * <ol>
  95. * <li>
  96. * Loop through the <code>java.security.Security</code> properties,
  97. * <i>policy.url.1</i>, <i>policy.url.2</i>, ...,
  98. * <i>policy.url.X</i>" and
  99. * <i>auth.policy.url.1</i>, <i>auth.policy.url.2</i>, ...,
  100. * <i>auth.policy.url.X</i>". These properties are set
  101. * in the Java security properties file, which is located in the file named
  102. * &lt;JAVA_HOME&gt;/lib/security/java.security.
  103. * &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
  104. * and specifies the directory where the JRE is installed.
  105. * Each property value specifies a <code>URL</code> pointing to a
  106. * policy file to be loaded. Read in and load each policy.
  107. *
  108. * <i>auth.policy.url</i> is supported only for backward compatibility.
  109. *
  110. * <li>
  111. * The <code>java.lang.System</code> property <i>java.security.policy</i>
  112. * may also be set to a <code>URL</code> pointing to another policy file
  113. * (which is the case when a user uses the -D switch at runtime).
  114. * If this property is defined, and its use is allowed by the
  115. * security property file (the Security property,
  116. * <i>policy.allowSystemProperty</i> is set to <i>true</i>),
  117. * also load that policy.
  118. *
  119. * <li>
  120. * The <code>java.lang.System</code> property
  121. * <i>java.security.auth.policy</i> may also be set to a
  122. * <code>URL</code> pointing to another policy file
  123. * (which is the case when a user uses the -D switch at runtime).
  124. * If this property is defined, and its use is allowed by the
  125. * security property file (the Security property,
  126. * <i>policy.allowSystemProperty</i> is set to <i>true</i>),
  127. * also load that policy.
  128. *
  129. * <i>java.security.auth.policy</i> is supported only for backward
  130. * compatibility.
  131. *
  132. * If the <i>java.security.policy</i> or
  133. * <i>java.security.auth.policy</i> property is defined using
  134. * "==" (rather than "="), then ignore all other specified
  135. * policies and only load this policy.
  136. * </ol>
  137. *
  138. * Each policy file consists of one or more grant entries, each of
  139. * which consists of a number of permission entries.
  140. *
  141. * <pre>
  142. * grant signedBy "<b>alias</b>", codeBase "<b>URL</b>",
  143. * principal <b>principalClass</b> "<b>principalName</b>",
  144. * principal <b>principalClass</b> "<b>principalName</b>",
  145. * ... {
  146. *
  147. * permission <b>Type</b> "<b>name</b> "<b>action</b>",
  148. * signedBy "<b>alias</b>";
  149. * permission <b>Type</b> "<b>name</b> "<b>action</b>",
  150. * signedBy "<b>alias</b>";
  151. * ....
  152. * };
  153. * </pre>
  154. *
  155. * All non-bold items above must appear as is (although case
  156. * doesn't matter and some are optional, as noted below).
  157. * principal entries are optional and need not be present.
  158. * Italicized items represent variable values.
  159. *
  160. * <p> A grant entry must begin with the word <code>grant</code>.
  161. * The <code>signedBy</code>,<code>codeBase</code> and <code>principal</code>
  162. * name/value pairs are optional.
  163. * If they are not present, then any signer (including unsigned code)
  164. * will match, and any codeBase will match.
  165. * Note that the <i>principalClass</i>
  166. * may be set to the wildcard value, *, which allows it to match
  167. * any <code>Principal</code> class. In addition, the <i>principalName</i>
  168. * may also be set to the wildcard value, *, allowing it to match
  169. * any <code>Principal</code> name. When setting the <i>principalName</i>
  170. * to the *, do not surround the * with quotes.
  171. *
  172. * <p> A permission entry must begin with the word <code>permission</code>.
  173. * The word <code><i>Type</i></code> in the template above is
  174. * a specific permission type, such as <code>java.io.FilePermission</code>
  175. * or <code>java.lang.RuntimePermission</code>.
  176. *
  177. * <p> The "<i>action</i>" is required for
  178. * many permission types, such as <code>java.io.FilePermission</code>
  179. * (where it specifies what type of file access that is permitted).
  180. * It is not required for categories such as
  181. * <code>java.lang.RuntimePermission</code>
  182. * where it is not necessary - you either have the
  183. * permission specified by the <code>"<i>name</i>"</code>
  184. * value following the type name or you don't.
  185. *
  186. * <p> The <code>signedBy</code> name/value pair for a permission entry
  187. * is optional. If present, it indicates a signed permission. That is,
  188. * the permission class itself must be signed by the given alias in
  189. * order for it to be granted. For example,
  190. * suppose you have the following grant entry:
  191. *
  192. * <pre>
  193. * grant principal foo.com.Principal "Duke" {
  194. * permission Foo "foobar", signedBy "FooSoft";
  195. * }
  196. * </pre>
  197. *
  198. * <p> Then this permission of type <i>Foo</i> is granted if the
  199. * <code>Foo.class</code> permission has been signed by the
  200. * "FooSoft" alias, or if XXX <code>Foo.class</code> is a
  201. * system class (i.e., is found on the CLASSPATH).
  202. *
  203. *
  204. * <p> Items that appear in an entry must appear in the specified order
  205. * (<code>permission</code>, <i>Type</i>, "<i>name</i>", and
  206. * "<i>action</i>"). An entry is terminated with a semicolon.
  207. *
  208. * <p> Case is unimportant for the identifiers (<code>permission</code>,
  209. * <code>signedBy</code>, <code>codeBase</code>, etc.) but is
  210. * significant for the <i>Type</i>
  211. * or for any string that is passed in as a value. <p>
  212. *
  213. * <p> An example of two entries in a policy configuration file is
  214. * <pre>
  215. * // if the code is comes from "foo.com" and is running as "Duke",
  216. * // grant it read/write to all files in /tmp.
  217. *
  218. * grant codeBase "foo.com", principal foo.com.Principal "Duke" {
  219. * permission java.io.FilePermission "/tmp/*", "read,write";
  220. * };
  221. *
  222. * // grant any code running as "Duke" permission to read
  223. * // the "java.vendor" Property.
  224. *
  225. * grant principal foo.com.Principal "Duke" {
  226. * permission java.util.PropertyPermission "java.vendor";
  227. *
  228. *
  229. * </pre>
  230. * This Policy implementation supports special handling of any
  231. * permission that contains the string, "<b>${{self}}</b>", as part of
  232. * its target name. When such a permission is evaluated
  233. * (such as during a security check), <b>${{self}}</b> is replaced
  234. * with one or more Principal class/name pairs. The exact
  235. * replacement performed depends upon the contents of the
  236. * grant clause to which the permission belongs.
  237. *<p>
  238. *
  239. * If the grant clause does not contain any principal information,
  240. * the permission will be ignored (permissions containing
  241. * <b>${{self}}</b> in their target names are only valid in the context
  242. * of a principal-based grant clause). For example, BarPermission
  243. * will always be ignored in the following grant clause:
  244. *
  245. *<pre>
  246. * grant codebase "www.foo.com", signedby "duke" {
  247. * permission BarPermission "... ${{self}} ...";
  248. * };
  249. *</pre>
  250. *
  251. * If the grant clause contains principal information, <b>${{self}}</b>
  252. * will be replaced with that same principal information.
  253. * For example, <b>${{self}}</b> in BarPermission will be replaced by
  254. * <b>javax.security.auth.x500.X500Principal "cn=Duke"</b>
  255. * in the following grant clause:
  256. *
  257. * <pre>
  258. * grant principal javax.security.auth.x500.X500Principal "cn=Duke" {
  259. * permission BarPermission "... ${{self}} ...";
  260. * };
  261. * </pre>
  262. *
  263. * If there is a comma-separated list of principals in the grant
  264. * clause, then <b>${{self}}</b> will be replaced by the same
  265. * comma-separated list or principals.
  266. * In the case where both the principal class and name are
  267. * wildcarded in the grant clause, <b>${{self}}</b> is replaced
  268. * with all the principals associated with the <code>Subject</code>
  269. * in the current <code>AccessControlContext</code>.
  270. *
  271. *
  272. * <p> For PrivateCredentialPermissions, you can also use "<b>self</b>"
  273. * instead of "<b>${{self}}</b>". However the use of "<b>self</b>" is
  274. * deprecated in favour of "<b>${{self}}</b>".
  275. *
  276. * @see java.security.CodeSource
  277. * @see java.security.Permissions
  278. * @see java.security.ProtectionDomain
  279. */
  280. public class PolicyFile extends java.security.Policy {
  281. private static final Debug debug = Debug.getInstance("policy");
  282. private static final String NONE = "NONE";
  283. private static final String P11KEYSTORE = "PKCS11";
  284. private static final String SELF = "${{self}}";
  285. private static final String X500PRINCIPAL =
  286. "javax.security.auth.x500.X500Principal";
  287. private static final String POLICY = "java.security.policy";
  288. private static final String SECURITY_MANAGER = "java.security.manager";
  289. private static final String POLICY_URL = "policy.url.";
  290. private static final String AUTH_POLICY = "java.security.auth.policy";
  291. private static final String AUTH_POLICY_URL = "auth.policy.url.";
  292. private static final int DEFAULT_CACHE_SIZE = 1;
  293. // contains the policy grant entries, PD cache, and alias mapping
  294. private AtomicReference<PolicyInfo> policyInfo = new AtomicReference<>();
  295. private boolean constructed = false;
  296. private boolean expandProperties = true;
  297. private boolean ignoreIdentityScope = true;
  298. private boolean allowSystemProperties = true;
  299. private boolean notUtf8 = false;
  300. private URL url;
  301. // for use with the reflection API
  302. private static final Class[] PARAMS0 = { };
  303. private static final Class[] PARAMS1 = { String.class };
  304. private static final Class[] PARAMS2 = { String.class, String.class };
  305. /**
  306. * Initializes the Policy object and reads the default policy
  307. * configuration file(s) into the Policy object.
  308. */
  309. public PolicyFile() {
  310. init((URL)null);
  311. }
  312. /**
  313. * Initializes the Policy object and reads the default policy
  314. * from the specified URL only.
  315. */
  316. public PolicyFile(URL url) {
  317. this.url = url;
  318. init(url);
  319. }
  320. /**
  321. * Initializes the Policy object and reads the default policy
  322. * configuration file(s) into the Policy object.
  323. *
  324. * The algorithm for locating the policy file(s) and reading their
  325. * information into the Policy object is:
  326. * <pre>
  327. * loop through the Security Properties named "policy.url.1",
  328. * ""policy.url.2", "auth.policy.url.1", "auth.policy.url.2" etc, until
  329. * you don't find one. Each of these specify a policy file.
  330. *
  331. * if none of these could be loaded, use a builtin static policy
  332. * equivalent to the default lib/security/java.policy file.
  333. *
  334. * if the system property "java.policy" or "java.auth.policy" is defined
  335. * (which is the
  336. * case when the user uses the -D switch at runtime), and
  337. * its use is allowed by the security property file,
  338. * also load it.
  339. * </pre>
  340. *
  341. * Each policy file consists of one or more grant entries, each of
  342. * which consists of a number of permission entries.
  343. * <pre>
  344. * grant signedBy "<i>alias</i>", codeBase "<i>URL</i>" {
  345. * permission <i>Type</i> "<i>name</i>", "<i>action</i>",
  346. * signedBy "<i>alias</i>";
  347. * ....
  348. * permission <i>Type</i> "<i>name</i>", "<i>action</i>",
  349. * signedBy "<i>alias</i>";
  350. * };
  351. *
  352. * </pre>
  353. *
  354. * All non-italicized items above must appear as is (although case
  355. * doesn't matter and some are optional, as noted below).
  356. * Italicized items represent variable values.
  357. *
  358. * <p> A grant entry must begin with the word <code>grant</code>.
  359. * The <code>signedBy</code> and <code>codeBase</code> name/value
  360. * pairs are optional.
  361. * If they are not present, then any signer (including unsigned code)
  362. * will match, and any codeBase will match.
  363. *
  364. * <p> A permission entry must begin with the word <code>permission</code>.
  365. * The word <code><i>Type</i></code> in the template above would actually
  366. * be a specific permission type, such as
  367. * <code>java.io.FilePermission</code> or
  368. * <code>java.lang.RuntimePermission</code>.
  369. *
  370. * <p>The "<i>action</i>" is required for
  371. * many permission types, such as <code>java.io.FilePermission</code>
  372. * (where it specifies what type of file access is permitted).
  373. * It is not required for categories such as
  374. * <code>java.lang.RuntimePermission</code>
  375. * where it is not necessary - you either have the
  376. * permission specified by the <code>"<i>name</i>"</code>
  377. * value following the type name or you don't.
  378. *
  379. * <p>The <code>signedBy</code> name/value pair for a permission entry
  380. * is optional. If present, it indicates a signed permission. That is,
  381. * the permission class itself must be signed by the given alias in
  382. * order for it to be granted. For example,
  383. * suppose you have the following grant entry:
  384. *
  385. * <pre>
  386. * grant {
  387. * permission Foo "foobar", signedBy "FooSoft";
  388. * }
  389. * </pre>
  390. *
  391. * <p>Then this permission of type <i>Foo</i> is granted if the
  392. * <code>Foo.class</code> permission has been signed by the
  393. * "FooSoft" alias, or if <code>Foo.class</code> is a
  394. * system class (i.e., is found on the CLASSPATH).
  395. *
  396. * <p>Items that appear in an entry must appear in the specified order
  397. * (<code>permission</code>, <i>Type</i>, "<i>name</i>", and
  398. * "<i>action</i>"). An entry is terminated with a semicolon.
  399. *
  400. * <p>Case is unimportant for the identifiers (<code>permission</code>,
  401. * <code>signedBy</code>, <code>codeBase</code>, etc.) but is
  402. * significant for the <i>Type</i>
  403. * or for any string that is passed in as a value. <p>
  404. *
  405. * <p>An example of two entries in a policy configuration file is
  406. * <pre>
  407. * // if the code is signed by "Duke", grant it read/write to all
  408. * // files in /tmp.
  409. *
  410. * grant signedBy "Duke" {
  411. * permission java.io.FilePermission "/tmp/*", "read,write";
  412. * };
  413. * <p>
  414. * // grant everyone the following permission
  415. *
  416. * grant {
  417. * permission java.util.PropertyPermission "java.vendor";
  418. * };
  419. * </pre>
  420. */
  421. private void init(URL url) {
  422. // Properties are set once for each init(); ignore changes between
  423. // between diff invocations of initPolicyFile(policy, url, info).
  424. String numCacheStr =
  425. AccessController.doPrivileged(new PrivilegedAction<String>() {
  426. public String run() {
  427. expandProperties = "true".equalsIgnoreCase
  428. (Security.getProperty("policy.expandProperties"));
  429. ignoreIdentityScope = "true".equalsIgnoreCase
  430. (Security.getProperty("policy.ignoreIdentityScope"));
  431. allowSystemProperties = "true".equalsIgnoreCase
  432. (Security.getProperty("policy.allowSystemProperty"));
  433. notUtf8 = "false".equalsIgnoreCase
  434. (System.getProperty("sun.security.policy.utf8"));
  435. return System.getProperty("sun.security.policy.numcaches");
  436. }});
  437. int numCaches;
  438. if (numCacheStr != null) {
  439. try {
  440. numCaches = Integer.parseInt(numCacheStr);
  441. } catch (NumberFormatException e) {
  442. numCaches = DEFAULT_CACHE_SIZE;
  443. }
  444. } else {
  445. numCaches = DEFAULT_CACHE_SIZE;
  446. }
  447. // System.out.println("number caches=" + numCaches);
  448. PolicyInfo newInfo = new PolicyInfo(numCaches);
  449. initPolicyFile(newInfo, url);
  450. policyInfo.set(newInfo);
  451. }
  452. private void initPolicyFile(final PolicyInfo newInfo, final URL url) {
  453. if (url != null) {
  454. /**
  455. * If the caller specified a URL via Policy.getInstance,
  456. * we only read from that URL
  457. */
  458. if (debug != null) {
  459. debug.println("reading "+url);
  460. }
  461. AccessController.doPrivileged(new PrivilegedAction<Void>() {
  462. public Void run() {
  463. if (init(url, newInfo) == false) {
  464. // use static policy if all else fails
  465. initStaticPolicy(newInfo);
  466. }
  467. return null;
  468. }
  469. });
  470. } else {
  471. /**
  472. * Caller did not specify URL via Policy.getInstance.
  473. * Read from URLs listed in the java.security properties file.
  474. *
  475. * We call initPolicyFile with POLICY , POLICY_URL and then
  476. * call it with AUTH_POLICY and AUTH_POLICY_URL
  477. * So first we will process the JAVA standard policy
  478. * and then process the JAVA AUTH Policy.
  479. * This is for backward compatibility as well as to handle
  480. * cases where the user has a single unified policyfile
  481. * with both java policy entries and auth entries
  482. */
  483. boolean loaded_one = initPolicyFile(POLICY, POLICY_URL, newInfo);
  484. // To maintain strict backward compatibility
  485. // we load the static policy only if POLICY load failed
  486. if (!loaded_one) {
  487. // use static policy if all else fails
  488. initStaticPolicy(newInfo);
  489. }
  490. initPolicyFile(AUTH_POLICY, AUTH_POLICY_URL, newInfo);
  491. }
  492. }
  493. private boolean initPolicyFile(final String propname, final String urlname,
  494. final PolicyInfo newInfo) {
  495. Boolean loadedPolicy =
  496. AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
  497. public Boolean run() {
  498. boolean loaded_policy = false;
  499. if (allowSystemProperties) {
  500. String extra_policy = System.getProperty(propname);
  501. if (extra_policy != null) {
  502. boolean overrideAll = false;
  503. if (extra_policy.startsWith("=")) {
  504. overrideAll = true;
  505. extra_policy = extra_policy.substring(1);
  506. }
  507. try {
  508. extra_policy =
  509. PropertyExpander.expand(extra_policy);
  510. URL policyURL;
  511. File policyFile = new File(extra_policy);
  512. if (policyFile.exists()) {
  513. policyURL = ParseUtil.fileToEncodedURL
  514. (new File(policyFile.getCanonicalPath()));
  515. } else {
  516. policyURL = new URL(extra_policy);
  517. }
  518. if (debug != null)
  519. debug.println("reading "+policyURL);
  520. if (init(policyURL, newInfo))
  521. loaded_policy = true;
  522. } catch (Exception e) {
  523. // ignore.
  524. if (debug != null) {
  525. debug.println("caught exception: "+e);
  526. }
  527. }
  528. if (overrideAll) {
  529. if (debug != null) {
  530. debug.println("overriding other policies!");
  531. }
  532. return Boolean.valueOf(loaded_policy);
  533. }
  534. }
  535. }
  536. int n = 1;
  537. String policy_uri;
  538. while ((policy_uri = Security.getProperty(urlname+n)) != null) {
  539. try {
  540. URL policy_url = null;
  541. String expanded_uri = PropertyExpander.expand
  542. (policy_uri).replace(File.separatorChar, '/');
  543. if (policy_uri.startsWith("file:${java.home}/") ||
  544. policy_uri.startsWith("file:${user.home}/")) {
  545. // this special case accommodates
  546. // the situation java.home/user.home
  547. // expand to a single slash, resulting in
  548. // a file://foo URI
  549. policy_url = new File
  550. (expanded_uri.substring(5)).toURI().toURL();
  551. } else {
  552. policy_url = new URI(expanded_uri).toURL();
  553. }
  554. if (debug != null)
  555. debug.println("reading "+policy_url);
  556. if (init(policy_url, newInfo))
  557. loaded_policy = true;
  558. } catch (Exception e) {
  559. if (debug != null) {
  560. debug.println("error reading policy "+e);
  561. e.printStackTrace();
  562. }
  563. // ignore that policy
  564. }
  565. n++;
  566. }
  567. return Boolean.valueOf(loaded_policy);
  568. }
  569. });
  570. return loadedPolicy.booleanValue();
  571. }
  572. /**
  573. * Reads a policy configuration into the Policy object using a
  574. * Reader object.
  575. *
  576. * @param policyFile the policy Reader object.
  577. */
  578. private boolean init(URL policy, PolicyInfo newInfo) {
  579. boolean success = false;
  580. PolicyParser pp = new PolicyParser(expandProperties);
  581. InputStreamReader isr = null;
  582. try {
  583. // read in policy using UTF-8 by default
  584. //
  585. // check non-standard system property to see if
  586. // the default encoding should be used instead
  587. if (notUtf8) {
  588. isr = new InputStreamReader
  589. (PolicyUtil.getInputStream(policy));
  590. } else {
  591. isr = new InputStreamReader
  592. (PolicyUtil.getInputStream(policy), "UTF-8");
  593. }
  594. pp.read(isr);
  595. KeyStore keyStore = null;
  596. try {
  597. keyStore = PolicyUtil.getKeyStore
  598. (policy,
  599. pp.getKeyStoreUrl(),
  600. pp.getKeyStoreType(),
  601. pp.getKeyStoreProvider(),
  602. pp.getStorePassURL(),
  603. debug);
  604. } catch (Exception e) {
  605. // ignore, treat it like we have no keystore
  606. if (debug != null) {
  607. e.printStackTrace();
  608. }
  609. }
  610. Enumeration<PolicyParser.GrantEntry> enum_ = pp.grantElements();
  611. while (enum_.hasMoreElements()) {
  612. PolicyParser.GrantEntry ge = enum_.nextElement();
  613. addGrantEntry(ge, keyStore, newInfo);
  614. }
  615. } catch (PolicyParser.ParsingException pe) {
  616. MessageFormat form = new MessageFormat(ResourcesMgr.getString
  617. (POLICY + ".error.parsing.policy.message"));
  618. Object[] source = {policy, pe.getLocalizedMessage()};
  619. System.err.println(form.format(source));
  620. if (debug != null)
  621. pe.printStackTrace();
  622. } catch (Exception e) {
  623. if (debug != null) {
  624. debug.println("error parsing "+policy);
  625. debug.println(e.toString());
  626. e.printStackTrace();
  627. }
  628. } finally {
  629. if (isr != null) {
  630. try {
  631. isr.close();
  632. success = true;
  633. } catch (IOException e) {
  634. // ignore the exception
  635. }
  636. } else {
  637. success = true;
  638. }
  639. }
  640. return success;
  641. }
  642. private void initStaticPolicy(final PolicyInfo newInfo) {
  643. AccessController.doPrivileged(new PrivilegedAction<Void>() {
  644. public Void run() {
  645. PolicyEntry pe = new PolicyEntry(new CodeSource(null,
  646. (Certificate[]) null));
  647. pe.add(SecurityConstants.LOCAL_LISTEN_PERMISSION);
  648. pe.add(new PropertyPermission("java.version",
  649. SecurityConstants.PROPERTY_READ_ACTION));
  650. pe.add(new PropertyPermission("java.vendor",
  651. SecurityConstants.PROPERTY_READ_ACTION));
  652. pe.add(new PropertyPermission("java.vendor.url",
  653. SecurityConstants.PROPERTY_READ_ACTION));
  654. pe.add(new PropertyPermission("java.class.version",
  655. SecurityConstants.PROPERTY_READ_ACTION));
  656. pe.add(new PropertyPermission("os.name",
  657. SecurityConstants.PROPERTY_READ_ACTION));
  658. pe.add(new PropertyPermission("os.version",
  659. SecurityConstants.PROPERTY_READ_ACTION));
  660. pe.add(new PropertyPermission("os.arch",
  661. SecurityConstants.PROPERTY_READ_ACTION));
  662. pe.add(new PropertyPermission("file.separator",
  663. SecurityConstants.PROPERTY_READ_ACTION));
  664. pe.add(new PropertyPermission("path.separator",
  665. SecurityConstants.PROPERTY_READ_ACTION));
  666. pe.add(new PropertyPermission("line.separator",
  667. SecurityConstants.PROPERTY_READ_ACTION));
  668. pe.add(new PropertyPermission
  669. ("java.specification.version",
  670. SecurityConstants.PROPERTY_READ_ACTION));
  671. pe.add(new PropertyPermission
  672. ("java.specification.vendor",
  673. SecurityConstants.PROPERTY_READ_ACTION));
  674. pe.add(new PropertyPermission
  675. ("java.specification.name",
  676. SecurityConstants.PROPERTY_READ_ACTION));
  677. pe.add(new PropertyPermission
  678. ("java.vm.specification.version",
  679. SecurityConstants.PROPERTY_READ_ACTION));
  680. pe.add(new PropertyPermission
  681. ("java.vm.specification.vendor",
  682. SecurityConstants.PROPERTY_READ_ACTION));
  683. pe.add(new PropertyPermission
  684. ("java.vm.specification.name",
  685. SecurityConstants.PROPERTY_READ_ACTION));
  686. pe.add(new PropertyPermission("java.vm.version",
  687. SecurityConstants.PROPERTY_READ_ACTION));
  688. pe.add(new PropertyPermission("java.vm.vendor",
  689. SecurityConstants.PROPERTY_READ_ACTION));
  690. pe.add(new PropertyPermission("java.vm.name",
  691. SecurityConstants.PROPERTY_READ_ACTION));
  692. // No need to sync because noone has access to newInfo yet
  693. newInfo.policyEntries.add(pe);
  694. // Add AllPermissions for standard extensions
  695. String[] extCodebases = PolicyParser.parseExtDirs(
  696. PolicyParser.EXTDIRS_EXPANSION, 0);
  697. if (extCodebases != null && extCodebases.length > 0) {
  698. for (int i = 0; i < extCodebases.length; i++) {
  699. try {
  700. pe = new PolicyEntry(canonicalizeCodebase(
  701. new CodeSource(new URL(extCodebases[i]),
  702. (Certificate[]) null), false ));
  703. pe.add(SecurityConstants.ALL_PERMISSION);
  704. // No need to sync because noone has access to
  705. // newInfo yet
  706. newInfo.policyEntries.add(pe);
  707. } catch (Exception e) {
  708. // this is probably bad (though not dangerous).
  709. // What should we do?
  710. }
  711. }
  712. }
  713. return null;
  714. }
  715. });
  716. }
  717. /**
  718. * Given a GrantEntry, create a codeSource.
  719. *
  720. * @return null if signedBy alias is not recognized
  721. */
  722. private CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore,
  723. PolicyInfo newInfo) throws java.net.MalformedURLException
  724. {
  725. Certificate[] certs = null;
  726. if (ge.signedBy != null) {
  727. certs = getCertificates(keyStore, ge.signedBy, newInfo);
  728. if (certs == null) {
  729. // we don't have a key for this alias,
  730. // just return
  731. if (debug != null) {
  732. debug.println(" -- No certs for alias '" +
  733. ge.signedBy + "' - ignoring entry");
  734. }
  735. return null;
  736. }
  737. }
  738. URL location;
  739. if (ge.codeBase != null)
  740. location = new URL(ge.codeBase);
  741. else
  742. location = null;
  743. return (canonicalizeCodebase(new CodeSource(location, certs),false));
  744. }
  745. /**
  746. * Add one policy entry to the list.
  747. */
  748. private void addGrantEntry(PolicyParser.GrantEntry ge,
  749. KeyStore keyStore, PolicyInfo newInfo) {
  750. if (debug != null) {
  751. debug.println("Adding policy entry: ");
  752. debug.println(" signedBy " + ge.signedBy);
  753. debug.println(" codeBase " + ge.codeBase);
  754. if (ge.principals != null && ge.principals.size() > 0) {
  755. ListIterator<PolicyParser.PrincipalEntry> li =
  756. ge.principals.listIterator();
  757. while (li.hasNext()) {
  758. PolicyParser.PrincipalEntry pppe = li.next();
  759. debug.println(" " + pppe.toString());
  760. }
  761. }
  762. }
  763. try {
  764. CodeSource codesource = getCodeSource(ge, keyStore, newInfo);
  765. // skip if signedBy alias was unknown...
  766. if (codesource == null) return;
  767. // perform keystore alias principal replacement.
  768. // for example, if alias resolves to X509 certificate,
  769. // replace principal with: <X500Principal class> <SubjectDN>
  770. // -- skip if alias is unknown
  771. if (replacePrincipals(ge.principals, keyStore) == false)
  772. return;
  773. PolicyEntry entry = new PolicyEntry(codesource, ge.principals);
  774. Enumeration<PolicyParser.PermissionEntry> enum_ =
  775. ge.permissionElements();
  776. while (enum_.hasMoreElements()) {
  777. PolicyParser.PermissionEntry pe = enum_.nextElement();
  778. try {
  779. // perform ${{ ... }} expansions within permission name
  780. expandPermissionName(pe, keyStore);
  781. // XXX special case PrivateCredentialPermission-SELF
  782. Permission perm;
  783. if (pe.permission.equals
  784. ("javax.security.auth.PrivateCredentialPermission") &&
  785. pe.name.endsWith(" self")) {
  786. pe.name = pe.name.substring(0, pe.name.indexOf("self"))
  787. + SELF;
  788. }
  789. // check for self
  790. if (pe.name != null && pe.name.indexOf(SELF) != -1) {
  791. // Create a "SelfPermission" , it could be an
  792. // an unresolved permission which will be resolved
  793. // when implies is called
  794. // Add it to entry
  795. Certificate certs[];
  796. if (pe.signedBy != null) {
  797. certs = getCertificates(keyStore,
  798. pe.signedBy,
  799. newInfo);
  800. } else {
  801. certs = null;
  802. }
  803. perm = new SelfPermission(pe.permission,
  804. pe.name,
  805. pe.action,
  806. certs);
  807. } else {
  808. perm = getInstance(pe.permission,
  809. pe.name,
  810. pe.action);
  811. }
  812. entry.add(perm);
  813. if (debug != null) {
  814. debug.println(" "+perm);
  815. }
  816. } catch (ClassNotFoundException cnfe) {
  817. Certificate certs[];
  818. if (pe.signedBy != null) {
  819. certs = getCertificates(keyStore,
  820. pe.signedBy,
  821. newInfo);
  822. } else {
  823. certs = null;
  824. }
  825. // only add if we had no signer or we had a
  826. // a signer and found the keys for it.
  827. if (certs != null || pe.signedBy == null) {
  828. Permission perm = new UnresolvedPermission(
  829. pe.permission,
  830. pe.name,
  831. pe.action,
  832. certs);
  833. entry.add(perm);
  834. if (debug != null) {
  835. debug.println(" "+perm);
  836. }
  837. }
  838. } catch (java.lang.reflect.InvocationTargetException ite) {
  839. MessageFormat form = new MessageFormat
  840. (ResourcesMgr.getString
  841. (POLICY +
  842. ".error.adding.Permission.perm.message"));
  843. Object[] source = {pe.permission,
  844. ite.getTargetException().toString()};
  845. System.err.println(form.format(source));
  846. } catch (Exception e) {
  847. MessageFormat form = new MessageFormat
  848. (ResourcesMgr.getString
  849. (POLICY +
  850. ".error.adding.Permission.perm.message"));
  851. Object[] source = {pe.permission,
  852. e.toString()};
  853. System.err.println(form.format(source));
  854. }
  855. }
  856. // No need to sync because noone has access to newInfo yet
  857. newInfo.policyEntries.add(entry);
  858. } catch (Exception e) {
  859. MessageFormat form = new MessageFormat(ResourcesMgr.getString
  860. (POLICY
  861. + ".error.adding.Entry.message"));
  862. Object[] source = {e.toString()};
  863. System.err.println(form.format(source));
  864. }
  865. if (debug != null)
  866. debug.println();
  867. }
  868. /**
  869. * Returns a new Permission object of the given Type. The Permission is
  870. * created by getting the
  871. * Class object using the <code>Class.forName</code> method, and using
  872. * the reflection API to invoke the (String name, String actions)
  873. * constructor on the
  874. * object.
  875. *
  876. * @param type the type of Permission being created.
  877. * @param name the name of the Permission being created.
  878. * @param actions the actions of the Permission being created.
  879. *
  880. * @exception ClassNotFoundException if the particular Permission
  881. * class could not be found.
  882. *
  883. * @exception IllegalAccessException if the class or initializer is
  884. * not accessible.
  885. *
  886. * @exception InstantiationException if getInstance tries to
  887. * instantiate an abstract class or an interface, or if the
  888. * instantiation fails for some other reason.
  889. *
  890. * @exception NoSuchMethodException if the (String, String) constructor
  891. * is not found.
  892. *
  893. * @exception InvocationTargetException if the underlying Permission
  894. * constructor throws an exception.
  895. *
  896. */
  897. private static final Permission getInstance(String type,
  898. String name,
  899. String actions)
  900. throws ClassNotFoundException,
  901. InstantiationException,
  902. IllegalAccessException,
  903. NoSuchMethodException,
  904. InvocationTargetException
  905. {
  906. //XXX we might want to keep a hash of created factories...
  907. Class<?> pc = Class.forName(type);
  908. Permission answer = getKnownInstance(pc, name, actions);
  909. if (answer != null) {
  910. return answer;
  911. }
  912. if (name == null && actions == null) {
  913. try {
  914. Constructor<?> c = pc.getConstructor(PARAMS0);
  915. return (Permission) c.newInstance(new Object[] {});
  916. } catch (NoSuchMethodException ne) {
  917. try {
  918. Constructor<?> c = pc.getConstructor(PARAMS1);
  919. return (Permission) c.newInstance(
  920. new Object[] { name});
  921. } catch (NoSuchMethodException ne1 ) {
  922. Constructor<?> c = pc.getConstructor(PARAMS2);
  923. return (Permission) c.newInstance(
  924. new Object[] { name, actions });
  925. }
  926. }
  927. } else {
  928. if (name != null && actions == null) {
  929. try {
  930. Constructor<?> c = pc.getConstructor(PARAMS1);
  931. return (Permission) c.newInstance(new Object[] { name});
  932. } catch (NoSuchMethodException ne) {
  933. Constructor<?> c = pc.getConstructor(PARAMS2);
  934. return (Permission) c.newInstance(
  935. new Object[] { name, actions });
  936. }
  937. } else {
  938. Constructor<?> c = pc.getConstructor(PARAMS2);
  939. return (Permission) c.newInstance(
  940. new Object[] { name, actions });
  941. }
  942. }
  943. }
  944. /**
  945. * Creates one of the well-known permissions directly instead of
  946. * via reflection. Keep list short to not penalize non-JDK-defined
  947. * permissions.
  948. */
  949. private static final Permission getKnownInstance(Class claz,
  950. String name, String actions) {
  951. // XXX shorten list to most popular ones?
  952. if (claz.equals(FilePermission.class)) {
  953. return new FilePermission(name, actions);
  954. } else if (claz.equals(SocketPermission.class)) {
  955. return new SocketPermission(name, actions);
  956. } else if (claz.equals(RuntimePermission.class)) {
  957. return new RuntimePermission(name, actions);
  958. } else if (claz.equals(PropertyPermission.class)) {
  959. return new PropertyPermission(name, actions);
  960. } else if (claz.equals(NetPermission.class)) {
  961. return new NetPermission(name, actions);
  962. } else if (claz.equals(AllPermission.class)) {
  963. return SecurityConstants.ALL_PERMISSION;
  964. /*
  965. } else if (claz.equals(ReflectPermission.class)) {
  966. return new ReflectPermission(name, actions);
  967. } else if (claz.equals(SecurityPermission.class)) {
  968. return new SecurityPermission(name, actions);
  969. } else if (claz.equals(PrivateCredentialPermission.class)) {
  970. return new PrivateCredentialPermission(name, actions);
  971. } else if (claz.equals(AuthPermission.class)) {
  972. return new AuthPermission(name, actions);
  973. } else if (claz.equals(ServicePermission.class)) {
  974. return new ServicePermission(name, actions);
  975. } else if (claz.equals(DelegationPermission.class)) {
  976. return new DelegationPermission(name, actions);
  977. } else if (claz.equals(SerializablePermission.class)) {
  978. return new SerializablePermission(name, actions);
  979. } else if (claz.equals(AudioPermission.class)) {
  980. return new AudioPermission(name, actions);
  981. } else if (claz.equals(SSLPermission.class)) {
  982. return new SSLPermission(name, actions);
  983. } else if (claz.equals(LoggingPermission.class)) {
  984. return new LoggingPermission(name, actions);
  985. } else if (claz.equals(SQLPermission.class)) {
  986. return new SQLPermission(name, actions);
  987. */
  988. } else {
  989. return null;
  990. }
  991. }
  992. /**
  993. * Fetch all certs associated with this alias.
  994. */
  995. private Certificate[] getCertificates
  996. (KeyStore keyStore, String aliases, PolicyInfo newInfo) {
  997. List<Certificate> vcerts = null;
  998. StringTokenizer st = new StringTokenizer(aliases, ",");
  999. int n = 0;
  1000. while (st.hasMoreTokens()) {
  1001. String alias = st.nextToken().trim();
  1002. n++;
  1003. Certificate cert = null;
  1004. // See if this alias's cert has already been cached
  1005. synchronized (newInfo.aliasMapping) {
  1006. cert = (Certificate)newInfo.aliasMapping.get(alias);
  1007. if (cert == null && keyStore != null) {
  1008. try {
  1009. cert = keyStore.getCertificate(alias);
  1010. } catch (KeyStoreException kse) {
  1011. // never happens, because keystore has already been loaded
  1012. // when we call this
  1013. }
  1014. if (cert != null) {
  1015. newInfo.aliasMapping.put(alias, cert);
  1016. newInfo.aliasMapping.put(cert, alias);
  1017. }
  1018. }
  1019. }
  1020. if (cert != null) {
  1021. if (vcerts == null)
  1022. vcerts = new ArrayList<Certificate>();
  1023. vcerts.add(cert);
  1024. }
  1025. }
  1026. // make sure n == vcerts.size, since we are doing a logical *and*
  1027. if (vcerts != null && n == vcerts.size()) {
  1028. Certificate[] certs = new Certificate[vcerts.size()];
  1029. vcerts.toArray(certs);
  1030. return certs;
  1031. } else {
  1032. return null;
  1033. }
  1034. }
  1035. /**
  1036. * Refreshes the policy object by re-reading all the policy files.
  1037. */
  1038. @Override public void refresh() {
  1039. init(url);
  1040. }
  1041. /**
  1042. * Evaluates the the global policy for the permissions granted to
  1043. * the ProtectionDomain and tests whether the permission is
  1044. * granted.
  1045. *
  1046. * @param domain the ProtectionDomain to test
  1047. * @param permission the Permission object to be tested for implication.
  1048. *
  1049. * @return true if "permission" is a proper subset of a permission
  1050. * granted to this ProtectionDomain.
  1051. *
  1052. * @see java.security.ProtectionDomain
  1053. */
  1054. @Override
  1055. public boolean implies(ProtectionDomain pd, Permission p) {
  1056. PolicyInfo pi = policyInfo.get();
  1057. ProtectionDomainCache pdMap = pi.getPdMapping();
  1058. PermissionCollection pc = pdMap.get(pd);
  1059. if (pc != null) {
  1060. return pc.implies(p);
  1061. }
  1062. pc = getPermissions(pd);
  1063. if (pc == null) {
  1064. return false;
  1065. }
  1066. // cache mapping of protection domain to its PermissionCollection
  1067. pdMap.put(pd, pc);
  1068. return pc.implies(p);
  1069. }
  1070. /**
  1071. * Examines this <code>Policy</code> and returns the permissions granted
  1072. * to the specified <code>ProtectionDomain</code>. This includes
  1073. * the permissions currently associated with the domain as well
  1074. * as the policy permissions granted to the domain's
  1075. * CodeSource, ClassLoader, and Principals.
  1076. *
  1077. * <p> Note that this <code>Policy</code> implementation has
  1078. * special handling for PrivateCredentialPermissions.
  1079. * When this method encounters a <code>PrivateCredentialPermission</code>
  1080. * which specifies "self" as the <code>Principal</code> class and name,
  1081. * it does not add that <code>Permission</code> to the returned
  1082. * <code>PermissionCollection</code>. Instead, it builds
  1083. * a new <code>PrivateCredentialPermission</code>
  1084. * for each <code>Principal</code> associated with the provided
  1085. * <code>Subject</code>. Each new <code>PrivateCredentialPermission</code>
  1086. * contains the same Credential class as specified in the
  1087. * originally granted permission, as well as the Class and name
  1088. * for the respective <code>Principal</code>.
  1089. *
  1090. * <p>
  1091. *
  1092. * @param domain the Permissions granted to this
  1093. * <code>ProtectionDomain</code> are returned.
  1094. *
  1095. * @return the Permissions granted to the provided
  1096. * <code>ProtectionDomain</code>.
  1097. */
  1098. @Override
  1099. public PermissionCollection getPermissions(ProtectionDomain domain) {
  1100. Permissions perms = new Permissions();
  1101. if (domain == null)
  1102. return perms;
  1103. // first get policy perms
  1104. getPermissions(perms, domain);
  1105. // add static perms
  1106. // - adding static perms after policy perms is necessary
  1107. // to avoid a regression for 4301064
  1108. PermissionCollection pc = domain.getPermissions();
  1109. if (pc != null) {
  1110. synchronized (pc) {
  1111. Enumeration<Permission> e = pc.elements();
  1112. while (e.hasMoreElements()) {
  1113. perms.add(e.nextElement());
  1114. }
  1115. }
  1116. }
  1117. return perms;
  1118. }
  1119. /**
  1120. * Examines this Policy and creates a PermissionCollection object with
  1121. * the set of permissions for the specified CodeSource.
  1122. *
  1123. * @param CodeSource the codesource associated with the caller.
  1124. * This encapsulates the original location of the code (where the code
  1125. * came from) and the public key(s) of its signer.
  1126. *
  1127. * @return the set of permissions according to the policy.
  1128. */
  1129. @Overr

Large files files are truncated, but you can click here to view the full file