1. /*
  2. * @(#)PolicyFile.java 1.34 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.security.auth;
  8. import java.io.*;
  9. import java.lang.RuntimePermission;
  10. import java.lang.reflect.*;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.util.*;
  14. import java.security.AccessController;
  15. import java.security.CodeSource;
  16. import java.security.Identity;
  17. import java.security.IdentityScope;
  18. import java.security.KeyStore;
  19. import java.security.KeyStoreException;
  20. import java.security.Permission;
  21. import java.security.Permissions;
  22. import java.security.PermissionCollection;
  23. import java.security.Principal;
  24. import java.security.UnresolvedPermission;
  25. import java.security.Security;
  26. import java.security.cert.Certificate;
  27. import java.security.cert.X509Certificate;
  28. import javax.security.auth.Subject;
  29. import javax.security.auth.PrivateCredentialPermission;
  30. import sun.security.util.PropertyExpander;
  31. /**
  32. * This class represents a default implementation for
  33. * <code>javax.security.auth.Policy</code>.
  34. *
  35. * <p> This object stores the policy for entire Java runtime,
  36. * and is the amalgamation of multiple static policy
  37. * configurations that resides in files.
  38. * The algorithm for locating the policy file(s) and reading their
  39. * information into this <code>Policy</code> object is:
  40. *
  41. * <ol>
  42. * <li>
  43. * Loop through the <code>java.security.Security</code> properties,
  44. * <i>auth.policy.url.1</i>, <i>auth.policy.url.2</i>, ...,
  45. * <i>auth.policy.url.X</i>". These properties are set
  46. * in the Java security properties file, which is located in the file named
  47. * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
  48. * refers to the directory where the JDK was installed.
  49. * Each property value specifies a <code>URL</code> pointing to a
  50. * policy file to be loaded. Read in and load each policy.
  51. *
  52. * <li>
  53. * The <code>java.lang.System</code> property <i>java.security.auth.policy</i>
  54. * may also be set to a <code>URL</code> pointing to another policy file
  55. * (which is the case when a user uses the -D switch at runtime).
  56. * If this property is defined, and its use is allowed by the
  57. * security property file (the Security property,
  58. * <i>policy.allowSystemProperty</i> is set to <i>true</i>),
  59. * also load that policy.
  60. *
  61. * <li>
  62. * If the <i>java.security.auth.policy</i> property is defined using
  63. * "==" (rather than "="), then ignore all other specified
  64. * policies and only load this policy.
  65. * </ol>
  66. *
  67. * Each policy file consists of one or more grant entries, each of
  68. * which consists of a number of permission entries.
  69. *
  70. * <pre>
  71. * grant signedBy "<b>alias</b>", codeBase "<b>URL</b>",
  72. * principal <b>principalClass</b> "<b>principalName</b>",
  73. * principal <b>principalClass</b> "<b>principalName</b>",
  74. * ... {
  75. *
  76. * permission <b>Type</b> "<b>name</b> "<b>action</b>",
  77. * signedBy "<b>alias</b>";
  78. * permission <b>Type</b> "<b>name</b> "<b>action</b>",
  79. * signedBy "<b>alias</b>";
  80. * ....
  81. * };
  82. * </pre>
  83. *
  84. * All non-bold items above must appear as is (although case
  85. * doesn't matter and some are optional, as noted below).
  86. * Italicized items represent variable values.
  87. *
  88. * <p> A grant entry must begin with the word <code>grant</code>.
  89. * The <code>signedBy</code> and <code>codeBase</code>
  90. * name/value pairs are optional.
  91. * If they are not present, then any signer (including unsigned code)
  92. * will match, and any codeBase will match. Note that the
  93. * <code>principal</code> name/value pair is not optional.
  94. * This <code>Policy</code> implementation only permits
  95. * Principal-based grant entries. Note that the <i>principalClass</i>
  96. * may be set to the wildcard value, *, which allows it to match
  97. * any <code>Principal</code> class. In addition, the <i>principalName</i>
  98. * may also be set to the wildcard value, *, allowing it to match
  99. * any <code>Principal</code> name. When setting the <i>principalName</i>
  100. * to the *, do not surround the * with quotes.
  101. *
  102. * <p> A permission entry must begin with the word <code>permission</code>.
  103. * The word <code><i>Type</i></code> in the template above is
  104. * a specific permission type, such as <code>java.io.FilePermission</code>
  105. * or <code>java.lang.RuntimePermission</code>.
  106. *
  107. * <p> The "<i>action</i>" is required for
  108. * many permission types, such as <code>java.io.FilePermission</code>
  109. * (where it specifies what type of file access that is permitted).
  110. * It is not required for categories such as
  111. * <code>java.lang.RuntimePermission</code>
  112. * where it is not necessary - you either have the
  113. * permission specified by the <code>"<i>name</i>"</code>
  114. * value following the type name or you don't.
  115. *
  116. * <p> The <code>signedBy</code> name/value pair for a permission entry
  117. * is optional. If present, it indicates a signed permission. That is,
  118. * the permission class itself must be signed by the given alias in
  119. * order for it to be granted. For example,
  120. * suppose you have the following grant entry:
  121. *
  122. * <pre>
  123. * grant principal foo.com.Principal "Duke" {
  124. * permission Foo "foobar", signedBy "FooSoft";
  125. * }
  126. * </pre>
  127. *
  128. * <p> Then this permission of type <i>Foo</i> is granted if the
  129. * <code>Foo.class</code> permission has been signed by the
  130. * "FooSoft" alias, or if <code>Foo.class</code> is a
  131. * system class (i.e., is found on the CLASSPATH).
  132. *
  133. * <p> Items that appear in an entry must appear in the specified order
  134. * (<code>permission</code>, <i>Type</i>, "<i>name</i>", and
  135. * "<i>action</i>"). An entry is terminated with a semicolon.
  136. *
  137. * <p> Case is unimportant for the identifiers (<code>permission</code>,
  138. * <code>signedBy</code>, <code>codeBase</code>, etc.) but is
  139. * significant for the <i>Type</i>
  140. * or for any string that is passed in as a value. <p>
  141. *
  142. * <p> An example of two entries in a policy configuration file is
  143. * <pre>
  144. * // if the code is comes from "foo.com" and is running as "Duke",
  145. * // grant it read/write to all files in /tmp.
  146. *
  147. * grant codeBase "foo.com", principal foo.com.Principal "Duke" {
  148. * permission java.io.FilePermission "/tmp/*", "read,write";
  149. * };
  150. *
  151. * // grant any code running as "Duke" permission to read
  152. * // the "java.vendor" Property.
  153. *
  154. * grant principal foo.com.Principal "Duke" {
  155. * permission java.util.PropertyPermission "java.vendor";
  156. * </pre>
  157. *
  158. * <p> This <code>Policy</code> implementation supports
  159. * special handling for PrivateCredentialPermissions.
  160. * If a grant entry is configured with a
  161. * <code>PrivateCredentialPermission</code>,
  162. * and the "Principal Class/Principal Name" for that
  163. * <code>PrivateCredentialPermission</code> is "self",
  164. * then the entry grants the specified <code>Subject</code> permission to
  165. * access its own private Credential. For example,
  166. * the following grants the <code>Subject</code> "Duke"
  167. * access to its own a.b.Credential.
  168. *
  169. * <pre>
  170. * grant principal foo.com.Principal "Duke" {
  171. * permission javax.security.auth.PrivateCredentialPermission
  172. * "a.b.Credential self",
  173. * "read";
  174. * };
  175. * </pre>
  176. *
  177. * The following grants the <code>Subject</code> "Duke"
  178. * access to all of its own private Credentials:
  179. *
  180. * <pre>
  181. * grant principal foo.com.Principal "Duke" {
  182. * permission javax.security.auth.PrivateCredentialPermission
  183. * "* self",
  184. * "read";
  185. * };
  186. * </pre>
  187. *
  188. * The following grants all Subjects authenticated as a
  189. * <code>SolarisPrincipal</code> (regardless of their respective names)
  190. * permission to access their own private Credentials:
  191. *
  192. * <pre>
  193. * grant principal com.sun.security.auth.SolarisPrincipal * {
  194. * permission javax.security.auth.PrivateCredentialPermission
  195. * "* self",
  196. * "read";
  197. * };
  198. * </pre>
  199. *
  200. * The following grants all Subjects permission to access their own
  201. * private Credentials:
  202. *
  203. * <pre>
  204. * grant principal * * {
  205. * permission javax.security.auth.PrivateCredentialPermission
  206. * "* self",
  207. * "read";
  208. * };
  209. * </pre>
  210. * @deprecated As of JDK 1.4, replaced by
  211. * {@link sun.security.provider.PolicyFile}.
  212. * This class is entirely deprecated.
  213. *
  214. * @version 1.22, 01/25/00
  215. * @see java.security.CodeSource
  216. * @see java.security.Permissions
  217. * @see java.security.ProtectionDomain
  218. */
  219. @Deprecated
  220. public class PolicyFile extends javax.security.auth.Policy {
  221. static final java.util.ResourceBundle rb =
  222. (java.util.ResourceBundle)java.security.AccessController.doPrivileged
  223. (new java.security.PrivilegedAction() {
  224. public Object run() {
  225. return (java.util.ResourceBundle.getBundle
  226. ("sun.security.util.AuthResources"));
  227. }
  228. });
  229. // needs to be package private
  230. private static final sun.security.util.Debug debug =
  231. sun.security.util.Debug.getInstance("policy", "\t[Auth Policy]");
  232. private static final String AUTH_POLICY = "java.security.auth.policy";
  233. private static final String SECURITY_MANAGER = "java.security.manager";
  234. private static final String AUTH_POLICY_URL = "auth.policy.url.";
  235. private Vector policyEntries;
  236. private Hashtable aliasMapping;
  237. private boolean initialized = false;
  238. private boolean expandProperties = true;
  239. private boolean ignoreIdentityScope = false;
  240. // for use with the reflection API
  241. private static final Class[] PARAMS = { String.class, String.class};
  242. /**
  243. * Initializes the Policy object and reads the default policy
  244. * configuration file(s) into the Policy object.
  245. */
  246. public PolicyFile() {
  247. // initialize Policy if either the AUTH_POLICY or
  248. // SECURITY_MANAGER properties are set
  249. String prop = System.getProperty(AUTH_POLICY);
  250. if (prop == null) {
  251. prop = System.getProperty(SECURITY_MANAGER);
  252. }
  253. if (prop != null)
  254. init();
  255. }
  256. private synchronized void init() {
  257. if (initialized)
  258. return;
  259. policyEntries = new Vector();
  260. aliasMapping = new Hashtable(11);
  261. initPolicyFile();
  262. initialized = true;
  263. }
  264. /**
  265. * Refreshes the policy object by re-reading all the policy files.
  266. *
  267. * <p>
  268. *
  269. * @exception SecurityException if the caller doesn't have permission
  270. * to refresh the <code>Policy</code>.
  271. */
  272. public synchronized void refresh()
  273. {
  274. java.lang.SecurityManager sm = System.getSecurityManager();
  275. if (sm != null) {
  276. sm.checkPermission(new javax.security.auth.AuthPermission
  277. ("refreshPolicy"));
  278. }
  279. // XXX
  280. //
  281. // 1) if code instantiates PolicyFile directly, then it will need
  282. // all the permissions required for the PolicyFile initialization
  283. // 2) if code calls Policy.getPolicy, then it simply needs
  284. // AuthPermission(getPolicy), and the javax.security.auth.Policy
  285. // implementation instantiates PolicyFile in a doPrivileged block
  286. // 3) if after instantiating a Policy (either via #1 or #2),
  287. // code calls refresh, it simply needs
  288. // AuthPermission(refreshPolicy). then PolicyFile wraps
  289. // the refresh in a doPrivileged block.
  290. initialized = false;
  291. java.security.AccessController.doPrivileged
  292. (new java.security.PrivilegedAction() {
  293. public Object run() {
  294. init();
  295. return null;
  296. }
  297. });
  298. }
  299. private KeyStore initKeyStore(URL policyUrl, String keyStoreName,
  300. String keyStoreType) {
  301. if (keyStoreName != null) {
  302. try {
  303. /*
  304. * location of keystore is specified as absolute URL in policy
  305. * file, or is relative to URL of policy file
  306. */
  307. URL keyStoreUrl = null;
  308. try {
  309. keyStoreUrl = new URL(keyStoreName);
  310. // absolute URL
  311. } catch (java.net.MalformedURLException e) {
  312. // relative URL
  313. keyStoreUrl = new URL(policyUrl, keyStoreName);
  314. }
  315. if (debug != null) {
  316. debug.println("reading keystore"+keyStoreUrl);
  317. }
  318. InputStream inStream =
  319. new BufferedInputStream(getInputStream(keyStoreUrl));
  320. KeyStore ks;
  321. if (keyStoreType != null)
  322. ks = KeyStore.getInstance(keyStoreType);
  323. else
  324. ks = KeyStore.getInstance(KeyStore.getDefaultType());
  325. ks.load(inStream, null);
  326. inStream.close();
  327. return ks;
  328. } catch (Exception e) {
  329. // ignore, treat it like we have no keystore
  330. if (debug != null) {
  331. e.printStackTrace();
  332. }
  333. return null;
  334. }
  335. }
  336. return null;
  337. }
  338. private void initPolicyFile() {
  339. String prop = Security.getProperty("policy.expandProperties");
  340. if (prop != null) expandProperties = prop.equalsIgnoreCase("true");
  341. String iscp = Security.getProperty("policy.ignoreIdentityScope");
  342. if (iscp != null) ignoreIdentityScope = iscp.equalsIgnoreCase("true");
  343. String allowSys = Security.getProperty("policy.allowSystemProperty");
  344. if ((allowSys!=null) && allowSys.equalsIgnoreCase("true")) {
  345. String extra_policy = System.getProperty(AUTH_POLICY);
  346. if (extra_policy != null) {
  347. boolean overrideAll = false;
  348. if (extra_policy.startsWith("=")) {
  349. overrideAll = true;
  350. extra_policy = extra_policy.substring(1);
  351. }
  352. try {
  353. extra_policy = PropertyExpander.expand(extra_policy);
  354. URL policyURL;;
  355. File policyFile = new File(extra_policy);
  356. if (policyFile.exists()) {
  357. policyURL =
  358. new URL("file:" + policyFile.getCanonicalPath());
  359. } else {
  360. policyURL = new URL(extra_policy);
  361. }
  362. if (debug != null)
  363. debug.println("reading "+policyURL);
  364. init(policyURL);
  365. } catch (Exception e) {
  366. // ignore.
  367. if (debug != null) {
  368. debug.println("caught exception: "+e);
  369. }
  370. }
  371. if (overrideAll) {
  372. if (debug != null) {
  373. debug.println("overriding other policies!");
  374. }
  375. return;
  376. }
  377. }
  378. }
  379. int n = 1;
  380. boolean loaded_one = false;
  381. String policy_url;
  382. while ((policy_url = Security.getProperty(AUTH_POLICY_URL+n)) != null) {
  383. try {
  384. policy_url = PropertyExpander.expand(policy_url).replace
  385. (File.separatorChar, '/');
  386. if (debug != null)
  387. debug.println("reading "+policy_url);
  388. init(new URL(policy_url));
  389. loaded_one = true;
  390. } catch (Exception e) {
  391. if (debug != null) {
  392. debug.println("error reading policy "+e);
  393. e.printStackTrace();
  394. }
  395. // ignore that policy
  396. }
  397. n++;
  398. }
  399. if (loaded_one == false) {
  400. // do not load a static policy
  401. }
  402. }
  403. /** the scope to check */
  404. private static IdentityScope scope = null;
  405. /**
  406. * Checks public key. If it is marked as trusted in
  407. * the identity database, add it to the policy
  408. * with the AllPermission.
  409. */
  410. private boolean checkForTrustedIdentity(final Certificate cert) {
  411. // XXX JAAS has no way to access the SUN package.
  412. // we'll add this back in when JAAS goes into core.
  413. return false;
  414. }
  415. /**
  416. * Reads a policy configuration into the Policy object using a
  417. * Reader object.
  418. *
  419. * @param policyFile the policy Reader object.
  420. */
  421. private void init(URL policy) {
  422. PolicyParser pp = new PolicyParser(expandProperties);
  423. try {
  424. InputStreamReader isr
  425. = new InputStreamReader(getInputStream(policy));
  426. pp.read(isr);
  427. isr.close();
  428. KeyStore keyStore = initKeyStore(policy, pp.getKeyStoreUrl(),
  429. pp.getKeyStoreType());
  430. Enumeration enum_ = pp.grantElements();
  431. while (enum_.hasMoreElements()) {
  432. PolicyParser.GrantEntry ge =
  433. (PolicyParser.GrantEntry) enum_.nextElement();
  434. addGrantEntry(ge, keyStore);
  435. }
  436. } catch (PolicyParser.ParsingException pe) {
  437. System.err.println(AUTH_POLICY +
  438. rb.getString(": error parsing ") + policy);
  439. System.err.println(AUTH_POLICY +
  440. rb.getString(": ") +
  441. pe.getMessage());
  442. if (debug != null)
  443. pe.printStackTrace();
  444. } catch (Exception e) {
  445. if (debug != null) {
  446. debug.println("error parsing "+policy);
  447. debug.println(e.toString());
  448. e.printStackTrace();
  449. }
  450. }
  451. }
  452. /*
  453. * Fast path reading from file urls in order to avoid calling
  454. * FileURLConnection.connect() which can be quite slow the first time
  455. * it is called. We really should clean up FileURLConnection so that
  456. * this is not a problem but in the meantime this fix helps reduce
  457. * start up time noticeably for the new launcher. -- DAC
  458. */
  459. private InputStream getInputStream(URL url) throws IOException {
  460. if ("file".equals(url.getProtocol())) {
  461. String path = url.getFile().replace('/', File.separatorChar);
  462. return new FileInputStream(path);
  463. } else {
  464. return url.openStream();
  465. }
  466. }
  467. /**
  468. * Given a PermissionEntry, create a codeSource.
  469. *
  470. * @return null if signedBy alias is not recognized
  471. */
  472. CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore)
  473. throws java.net.MalformedURLException
  474. {
  475. Certificate[] certs = null;
  476. if (ge.signedBy != null) {
  477. certs = getCertificates(keyStore, ge.signedBy);
  478. if (certs == null) {
  479. // we don't have a key for this alias,
  480. // just return
  481. if (debug != null) {
  482. debug.println(" no certs for alias " +
  483. ge.signedBy + ", ignoring.");
  484. }
  485. return null;
  486. }
  487. }
  488. URL location;
  489. if (ge.codeBase != null)
  490. location = new URL(ge.codeBase);
  491. else
  492. location = null;
  493. if (ge.principals == null || ge.principals.size() == 0) {
  494. return (canonicalizeCodebase
  495. (new CodeSource(location, certs),
  496. false));
  497. } else {
  498. return (canonicalizeCodebase
  499. (new SubjectCodeSource(null, ge.principals, location, certs),
  500. false));
  501. }
  502. }
  503. /**
  504. * Add one policy entry to the vector.
  505. */
  506. private void addGrantEntry(PolicyParser.GrantEntry ge,
  507. KeyStore keyStore) {
  508. if (debug != null) {
  509. debug.println("Adding policy entry: ");
  510. debug.println(" signedBy " + ge.signedBy);
  511. debug.println(" codeBase " + ge.codeBase);
  512. if (ge.principals != null && ge.principals.size() > 0) {
  513. ListIterator li = ge.principals.listIterator();
  514. while (li.hasNext()) {
  515. PolicyParser.PrincipalEntry pppe =
  516. (PolicyParser.PrincipalEntry)li.next();
  517. debug.println(" " + pppe.principalClass +
  518. " " + pppe.principalName);
  519. }
  520. }
  521. debug.println();
  522. }
  523. try {
  524. CodeSource codesource = getCodeSource(ge, keyStore);
  525. // skip if signedBy alias was unknown...
  526. if (codesource == null) return;
  527. PolicyEntry entry = new PolicyEntry(codesource);
  528. Enumeration enum_ = ge.permissionElements();
  529. while (enum_.hasMoreElements()) {
  530. PolicyParser.PermissionEntry pe =
  531. (PolicyParser.PermissionEntry) enum_.nextElement();
  532. try {
  533. // XXX special case PrivateCredentialPermission-SELF
  534. Permission perm;
  535. if (pe.permission.equals
  536. ("javax.security.auth.PrivateCredentialPermission") &&
  537. pe.name.endsWith(" self")) {
  538. perm = getInstance(pe.permission,
  539. pe.name + " \"self\"",
  540. pe.action);
  541. } else {
  542. perm = getInstance(pe.permission,
  543. pe.name,
  544. pe.action);
  545. }
  546. entry.add(perm);
  547. if (debug != null) {
  548. debug.println(" "+perm);
  549. }
  550. } catch (ClassNotFoundException cnfe) {
  551. Certificate certs[];
  552. if (pe.signedBy != null)
  553. certs = getCertificates(keyStore, pe.signedBy);
  554. else
  555. certs = null;
  556. // only add if we had no signer or we had a
  557. // a signer and found the keys for it.
  558. if (certs != null || pe.signedBy == null) {
  559. Permission perm = new UnresolvedPermission(
  560. pe.permission,
  561. pe.name,
  562. pe.action,
  563. certs);
  564. entry.add(perm);
  565. if (debug != null) {
  566. debug.println(" "+perm);
  567. }
  568. }
  569. } catch (java.lang.reflect.InvocationTargetException ite) {
  570. System.err.println
  571. (AUTH_POLICY +
  572. rb.getString(": error adding Permission ") +
  573. pe.permission +
  574. rb.getString(" ") +
  575. ite.getTargetException());
  576. } catch (Exception e) {
  577. System.err.println
  578. (AUTH_POLICY +
  579. rb.getString(": error adding Permission ") +
  580. pe.permission +
  581. rb.getString(" ") +
  582. e);
  583. }
  584. }
  585. policyEntries.addElement(entry);
  586. } catch (Exception e) {
  587. System.err.println
  588. (AUTH_POLICY +
  589. rb.getString(": error adding Entry ") +
  590. ge +
  591. rb.getString(" ") +
  592. e);
  593. }
  594. if (debug != null)
  595. debug.println();
  596. }
  597. /**
  598. * Returns a new Permission object of the given Type. The Permission is
  599. * created by getting the
  600. * Class object using the <code>Class.forName</code> method, and using
  601. * the reflection API to invoke the (String name, String actions)
  602. * constructor on the
  603. * object.
  604. *
  605. * @param type the type of Permission being created.
  606. * @param name the name of the Permission being created.
  607. * @param actions the actions of the Permission being created.
  608. *
  609. * @exception ClassNotFoundException if the particular Permission
  610. * class could not be found.
  611. *
  612. * @exception IllegalAccessException if the class or initializer is
  613. * not accessible.
  614. *
  615. * @exception InstantiationException if getInstance tries to
  616. * instantiate an abstract class or an interface, or if the
  617. * instantiation fails for some other reason.
  618. *
  619. * @exception NoSuchMethodException if the (String, String) constructor
  620. * is not found.
  621. *
  622. * @exception InvocationTargetException if the underlying Permission
  623. * constructor throws an exception.
  624. *
  625. */
  626. private static final Permission getInstance(String type,
  627. String name,
  628. String actions)
  629. throws ClassNotFoundException,
  630. InstantiationException,
  631. IllegalAccessException,
  632. NoSuchMethodException,
  633. InvocationTargetException
  634. {
  635. //XXX we might want to keep a hash of created factories...
  636. Class pc = Class.forName(type);
  637. Constructor c = pc.getConstructor(PARAMS);
  638. return (Permission) c.newInstance(new Object[] { name, actions });
  639. }
  640. /**
  641. * Fetch all certs associated with this alias.
  642. */
  643. Certificate[] getCertificates(
  644. KeyStore keyStore, String aliases) {
  645. Vector vcerts = null;
  646. StringTokenizer st = new StringTokenizer(aliases, ",");
  647. int n = 0;
  648. while (st.hasMoreTokens()) {
  649. String alias = st.nextToken().trim();
  650. n++;
  651. Certificate cert = null;
  652. //See if this alias's cert has already been cached
  653. cert = (Certificate) aliasMapping.get(alias);
  654. if (cert == null && keyStore != null) {
  655. try {
  656. cert = keyStore.getCertificate(alias);
  657. } catch (KeyStoreException kse) {
  658. // never happens, because keystore has already been loaded
  659. // when we call this
  660. }
  661. if (cert != null) {
  662. aliasMapping.put(alias, cert);
  663. aliasMapping.put(cert, alias);
  664. }
  665. }
  666. if (cert != null) {
  667. if (vcerts == null)
  668. vcerts = new Vector();
  669. vcerts.addElement(cert);
  670. }
  671. }
  672. // make sure n == vcerts.size, since we are doing a logical *and*
  673. if (vcerts != null && n == vcerts.size()) {
  674. Certificate[] certs = new Certificate[vcerts.size()];
  675. vcerts.copyInto(certs);
  676. return certs;
  677. } else {
  678. return null;
  679. }
  680. }
  681. /**
  682. * Enumerate all the entries in the global policy object.
  683. * This method is used by policy admin tools. The tools
  684. * should use the Enumeration methods on the returned object
  685. * to fetch the elements sequentially.
  686. */
  687. private final synchronized Enumeration elements(){
  688. return policyEntries.elements();
  689. }
  690. /**
  691. * Examines this <code>Policy</code> and returns the Permissions granted
  692. * to the specified <code>Subject</code> and <code>CodeSource</code>.
  693. *
  694. * <p> Permissions for a particular <i>grant</i> entry are returned
  695. * if the <code>CodeSource</code> constructed using the codebase and
  696. * signedby values specified in the entry <code>implies</code>
  697. * the <code>CodeSource</code> provided to this method, and if the
  698. * <code>Subject</code> provided to this method contains all of the
  699. * Principals specified in the entry.
  700. *
  701. * <p> The <code>Subject</code> provided to this method contains all
  702. * of the Principals specified in the entry if, for each
  703. * <code>Principal</code>, "P1", specified in the <i>grant</i> entry
  704. * one of the following two conditions is met:
  705. *
  706. * <p>
  707. * <ol>
  708. * <li> the <code>Subject</code> has a
  709. * <code>Principal</code>, "P2", where
  710. * <code>P2.getClass().getName()</code> equals the
  711. * P1's class name, and where
  712. * <code>P2.getName()</code> equals the P1's name.
  713. *
  714. * <li> P1 implements
  715. * <code>com.sun.security.auth.PrincipalComparator</code>,
  716. * and <code>P1.implies</code> the provided <code>Subject</code>.
  717. * </ol>
  718. *
  719. * <p> Note that this <code>Policy</code> implementation has
  720. * special handling for PrivateCredentialPermissions.
  721. * When this method encounters a <code>PrivateCredentialPermission</code>
  722. * which specifies "self" as the <code>Principal</code> class and name,
  723. * it does not add that <code>Permission</code> to the returned
  724. * <code>PermissionCollection</code>. Instead, it builds
  725. * a new <code>PrivateCredentialPermission</code>
  726. * for each <code>Principal</code> associated with the provided
  727. * <code>Subject</code>. Each new <code>PrivateCredentialPermission</code>
  728. * contains the same Credential class as specified in the
  729. * originally granted permission, as well as the Class and name
  730. * for the respective <code>Principal</code>.
  731. *
  732. * <p>
  733. *
  734. * @param subject the Permissions granted to this <code>Subject</code>
  735. * and the additionally provided <code>CodeSource</code>
  736. * are returned. <p>
  737. *
  738. * @param codesource the Permissions granted to this <code>CodeSource</code>
  739. * and the additionally provided <code>Subject</code>
  740. * are returned.
  741. *
  742. * @return the Permissions granted to the provided <code>Subject</code>
  743. * <code>CodeSource</code>.
  744. */
  745. public PermissionCollection getPermissions(final Subject subject,
  746. final CodeSource codesource) {
  747. // XXX when JAAS goes into the JDK core,
  748. // we can remove this method and simply
  749. // rely on the getPermissions variant that takes a codesource,
  750. // which no one can use at this point in time.
  751. // at that time, we can also make SubjectCodeSource a public
  752. // class.
  753. // XXX
  754. //
  755. // 1) if code instantiates PolicyFile directly, then it will need
  756. // all the permissions required for the PolicyFile initialization
  757. // 2) if code calls Policy.getPolicy, then it simply needs
  758. // AuthPermission(getPolicy), and the javax.security.auth.Policy
  759. // implementation instantiates PolicyFile in a doPrivileged block
  760. // 3) if after instantiating a Policy (either via #1 or #2),
  761. // code calls getPermissions, PolicyFile wraps the call
  762. // in a doPrivileged block.
  763. return (PermissionCollection)java.security.AccessController.doPrivileged
  764. (new java.security.PrivilegedAction() {
  765. public Object run() {
  766. SubjectCodeSource scs = new SubjectCodeSource
  767. (subject,
  768. null,
  769. codesource == null ? null : codesource.getLocation(),
  770. codesource == null ? null : codesource.getCertificates());
  771. if (initialized)
  772. return getPermissions(new Permissions(), scs);
  773. else
  774. return new PolicyPermissions(PolicyFile.this, scs);
  775. }
  776. });
  777. }
  778. /**
  779. * Examines the global policy for the specified CodeSource, and
  780. * creates a PermissionCollection object with
  781. * the set of permissions for that principal's protection domain.
  782. *
  783. * @param CodeSource the codesource associated with the caller.
  784. * This encapsulates the original location of the code (where the code
  785. * came from) and the public key(s) of its signer.
  786. *
  787. * @return the set of permissions according to the policy.
  788. */
  789. PermissionCollection getPermissions(CodeSource codesource) {
  790. if (initialized)
  791. return getPermissions(new Permissions(), codesource);
  792. else
  793. return new PolicyPermissions(this, codesource);
  794. }
  795. /**
  796. * Examines the global policy for the specified CodeSource, and
  797. * creates a PermissionCollection object with
  798. * the set of permissions for that principal's protection domain.
  799. *
  800. * @param permissions the permissions to populate
  801. * @param codesource the codesource associated with the caller.
  802. * This encapsulates the original location of the code (where the code
  803. * came from) and the public key(s) of its signer.
  804. *
  805. * @return the set of permissions according to the policy.
  806. */
  807. Permissions getPermissions(final Permissions perms,
  808. final CodeSource cs)
  809. {
  810. if (!initialized) {
  811. init();
  812. }
  813. final CodeSource codesource[] = {null};
  814. codesource[0] = canonicalizeCodebase(cs, true);
  815. if (debug != null) {
  816. debug.println("evaluate("+codesource[0]+")\n");
  817. }
  818. // needs to be in a begin/endPrivileged block because
  819. // codesource.implies calls URL.equals which does an
  820. // InetAddress lookup
  821. for (int i = 0; i < policyEntries.size(); i++) {
  822. PolicyEntry entry = (PolicyEntry)policyEntries.elementAt(i);
  823. if (debug != null) {
  824. debug.println("PolicyFile CodeSource implies: " +
  825. entry.codesource.toString() + "\n\n" +
  826. "\t" + codesource[0].toString() + "\n\n");
  827. }
  828. if (entry.codesource.implies(codesource[0])) {
  829. for (int j = 0; j < entry.permissions.size(); j++) {
  830. Permission p =
  831. (Permission) entry.permissions.elementAt(j);
  832. if (debug != null) {
  833. debug.println(" granting " + p);
  834. }
  835. if (!addSelfPermissions(p, entry.codesource,
  836. codesource[0], perms)) {
  837. // we could check for duplicates
  838. // before adding new permissions,
  839. // but the SubjectDomainCombiner
  840. // already checks for duplicates later
  841. perms.add(p);
  842. }
  843. }
  844. }
  845. }
  846. // now see if any of the keys are trusted ids.
  847. if (!ignoreIdentityScope) {
  848. Certificate certs[] = codesource[0].getCertificates();
  849. if (certs != null) {
  850. for (int k=0; k < certs.length; k++) {
  851. if ((aliasMapping.get(certs[k]) == null) &&
  852. checkForTrustedIdentity(certs[k])) {
  853. // checkForTrustedIdentity added it
  854. // to the policy for us. next time
  855. // around we'll find it. This time
  856. // around we need to add it.
  857. perms.add(new java.security.AllPermission());
  858. }
  859. }
  860. }
  861. }
  862. return perms;
  863. }
  864. /**
  865. * Returns true if 'Self' permissions were added to the provided
  866. * 'perms', and false otherwise.
  867. *
  868. * <p>
  869. *
  870. * @param p check to see if this Permission is a "SELF"
  871. * PrivateCredentialPermission. <p>
  872. *
  873. * @param entryCs the codesource for the Policy entry.
  874. *
  875. * @param accCs the codesource for from the current AccessControlContext.
  876. *
  877. * @param perms the PermissionCollection where the individual
  878. * PrivateCredentialPermissions will be added.
  879. */
  880. private boolean addSelfPermissions(final Permission p,
  881. CodeSource entryCs,
  882. CodeSource accCs,
  883. Permissions perms) {
  884. if (!(p instanceof PrivateCredentialPermission))
  885. return false;
  886. if (!(entryCs instanceof SubjectCodeSource))
  887. return false;
  888. PrivateCredentialPermission pcp = (PrivateCredentialPermission)p;
  889. SubjectCodeSource scs = (SubjectCodeSource)entryCs;
  890. // see if it is a SELF permission
  891. String[][] pPrincipals = pcp.getPrincipals();
  892. if (pPrincipals.length <= 0 ||
  893. !pPrincipals[0][0].equalsIgnoreCase("self") ||
  894. !pPrincipals[0][1].equalsIgnoreCase("self")) {
  895. // regular PrivateCredentialPermission
  896. return false;
  897. } else {
  898. // granted a SELF permission - create a
  899. // PrivateCredentialPermission for each
  900. // of the Policy entry's CodeSource Principals
  901. if (scs.getPrincipals() == null) {
  902. // XXX SubjectCodeSource has no Subject???
  903. return true;
  904. }
  905. ListIterator pli = scs.getPrincipals().listIterator();
  906. while (pli.hasNext()) {
  907. PolicyParser.PrincipalEntry principal =
  908. (PolicyParser.PrincipalEntry)pli.next();
  909. // XXX
  910. // if the Policy entry's Principal does not contain a
  911. // WILDCARD for the Principal name, then a
  912. // new PrivateCredentialPermission is created
  913. // for the Principal listed in the Policy entry.
  914. // if the Policy entry's Principal contains a WILDCARD
  915. // for the Principal name, then a new
  916. // PrivateCredentialPermission is created
  917. // for each Principal associated with the Subject
  918. // in the current ACC.
  919. String[][] principalInfo = getPrincipalInfo
  920. (principal, accCs);
  921. for (int i = 0; i < principalInfo.length; i++) {
  922. // here's the new PrivateCredentialPermission
  923. PrivateCredentialPermission newPcp =
  924. new PrivateCredentialPermission
  925. (pcp.getCredentialClass() +
  926. " " +
  927. principalInfo[i][0] +
  928. " " +
  929. "\"" + principalInfo[i][1] + "\"",
  930. "read");
  931. if (debug != null) {
  932. debug.println("adding SELF permission: " +
  933. newPcp.toString());
  934. }
  935. perms.add(newPcp);
  936. }
  937. }
  938. }
  939. return true;
  940. }
  941. /**
  942. * return the principal class/name pair in the 2D array.
  943. * array[x][y]: x corresponds to the array length.
  944. * if (y == 0), it's the principal class.
  945. * if (y == 1), it's the principal name.
  946. */
  947. private String[][] getPrincipalInfo
  948. (PolicyParser.PrincipalEntry principal,
  949. final CodeSource accCs) {
  950. // there are 3 possibilities:
  951. // 1) the entry's Principal class and name are not wildcarded
  952. // 2) the entry's Principal name is wildcarded only
  953. // 3) the entry's Principal class and name are wildcarded
  954. if (!principal.principalClass.equals
  955. (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
  956. !principal.principalName.equals
  957. (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
  958. // build a PrivateCredentialPermission for the principal
  959. // from the Policy entry
  960. String[][] info = new String[1][2];
  961. info[0][0] = principal.principalClass;
  962. info[0][1] = principal.principalName;
  963. return info;
  964. } else if (!principal.principalClass.equals
  965. (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
  966. principal.principalName.equals
  967. (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
  968. // build a PrivateCredentialPermission for all
  969. // the Subject's principals that are instances of principalClass
  970. // the accCs is guaranteed to be a SubjectCodeSource
  971. // because the earlier CodeSource.implies succeeded
  972. SubjectCodeSource scs = (SubjectCodeSource)accCs;
  973. Set principalSet = null;
  974. try {
  975. Class pClass = Class.forName(principal.principalClass, false,
  976. ClassLoader.getSystemClassLoader());
  977. principalSet = scs.getSubject().getPrincipals(pClass);
  978. } catch (Exception e) {
  979. if (debug != null) {
  980. debug.println("problem finding Principal Class " +
  981. "when expanding SELF permission: " +
  982. e.toString());
  983. }
  984. }
  985. if (principalSet == null) {
  986. // error
  987. return new String[0][0];
  988. }
  989. String[][] info = new String[principalSet.size()][2];
  990. java.util.Iterator pIterator = principalSet.iterator();
  991. int i = 0;
  992. while (pIterator.hasNext()) {
  993. Principal p = (Principal)pIterator.next();
  994. info[i][0] = p.getClass().getName();
  995. info[i][1] = p.getName();
  996. i++;
  997. }
  998. return info;
  999. } else {
  1000. // build a PrivateCredentialPermission for every
  1001. // one of the current Subject's principals
  1002. // the accCs is guaranteed to be a SubjectCodeSource
  1003. // because the earlier CodeSource.implies succeeded
  1004. SubjectCodeSource scs = (SubjectCodeSource)accCs;
  1005. Set principalSet = scs.getSubject().getPrincipals();
  1006. String[][] info = new String[principalSet.size()][2];
  1007. java.util.Iterator pIterator = principalSet.iterator();
  1008. int i = 0;
  1009. while (pIterator.hasNext()) {
  1010. Principal p = (Principal)pIterator.next();
  1011. info[i][0] = p.getClass().getName();
  1012. info[i][1] = p.getName();
  1013. i++;
  1014. }
  1015. return info;
  1016. }
  1017. }
  1018. /*
  1019. * Returns the signer certificates from the list of certificates associated
  1020. * with the given code source.
  1021. *
  1022. * The signer certificates are those certificates that were used to verify
  1023. * signed code originating from the codesource location.
  1024. *
  1025. * This method assumes that in the given code source, each signer
  1026. * certificate is followed by its supporting certificate chain
  1027. * (which may be empty), and that the signer certificate and its
  1028. * supporting certificate chain are ordered bottom-to-top (i.e., with the
  1029. * signer certificate first and the (root) certificate authority last).
  1030. */
  1031. Certificate[] getSignerCertificates(CodeSource cs) {
  1032. Certificate[] certs = null;
  1033. if ((certs = cs.getCertificates()) == null)
  1034. return null;
  1035. for (int i=0; i<certs.length; i++) {
  1036. if (!(certs[i] instanceof X509Certificate))
  1037. return cs.getCertificates();
  1038. }
  1039. // Do we have to do anything?
  1040. int i = 0;
  1041. int count = 0;
  1042. while (i < certs.length) {
  1043. count++;
  1044. while (((i+1) < certs.length)
  1045. && ((X509Certificate)certs[i]).getIssuerDN().equals(
  1046. ((X509Certificate)certs[i+1]).getSubjectDN())) {
  1047. i++;
  1048. }
  1049. i++;
  1050. }
  1051. if (count == certs.length)
  1052. // Done
  1053. return certs;
  1054. ArrayList userCertList = new ArrayList();
  1055. i = 0;
  1056. while (i < certs.length) {
  1057. userCertList.add(certs[i]);
  1058. while (((i+1) < certs.length)
  1059. && ((X509Certificate)certs[i]).getIssuerDN().equals(
  1060. ((X509Certificate)certs[i+1]).getSubjectDN())) {
  1061. i++;
  1062. }
  1063. i++;
  1064. }
  1065. Certificate[] userCerts = new Certificate[userCertList.size()];
  1066. userCertList.toArray(userCerts);
  1067. return userCerts;
  1068. }
  1069. private CodeSource canonicalizeCodebase(CodeSource cs,
  1070. boolean extractSignerCerts) {
  1071. CodeSource canonCs = cs;
  1072. if (cs.getLocation() != null &&
  1073. cs.getLocation().getProtocol().equalsIgnoreCase("file")) {
  1074. try {
  1075. String path = cs.getLocation().getFile().replace
  1076. ('/',
  1077. File.separatorChar);
  1078. URL csUrl = null;
  1079. if (path.endsWith("*")) {
  1080. // remove trailing '*' because it causes canonicalization
  1081. // to fail on win32
  1082. path = path.substring(0, path.length()-1);
  1083. boolean appendFileSep = false;
  1084. if (path.endsWith(File.separator))
  1085. appendFileSep = true;
  1086. if (path.equals("")) {
  1087. path = System.getProperty("user.dir");
  1088. }
  1089. File f = new File(path);
  1090. path = f.getCanonicalPath();
  1091. StringBuffer sb = new StringBuffer(path);
  1092. // reappend '*' to canonicalized filename (note that
  1093. // canonicalization may have removed trailing file
  1094. // separator, so we have to check for that, too)
  1095. if (!path.endsWith(File.separator) &&
  1096. (appendFileSep || f.isDirectory()))
  1097. sb.append(File.separatorChar);
  1098. sb.append('*');
  1099. path = sb.toString();
  1100. } else {
  1101. path = new File(path).getCanonicalPath();
  1102. }
  1103. csUrl = new File(path).toURL();
  1104. if (cs instanceof SubjectCodeSource) {
  1105. SubjectCodeSource scs = (SubjectCodeSource)cs;
  1106. if (extractSignerCerts) {
  1107. canonCs = new SubjectCodeSource
  1108. (scs.getSubject(),
  1109. scs.getPrincipals(),
  1110. csUrl,
  1111. getSignerCertificates(scs));
  1112. } else {
  1113. canonCs = new SubjectCodeSource
  1114. (scs.getSubject(),
  1115. scs.getPrincipals(),
  1116. csUrl,
  1117. scs.getCertificates());
  1118. }
  1119. } else {
  1120. if (extractSignerCerts) {
  1121. canonCs = new CodeSource(csUrl,
  1122. getSignerCertificates(cs));
  1123. } else {
  1124. canonCs = new CodeSource(csUrl,
  1125. cs.getCertificates());
  1126. }
  1127. }
  1128. } catch (IOException ioe) {
  1129. // leave codesource as it is, unless we have to extract its
  1130. // signer certificates
  1131. if (extractSignerCerts) {
  1132. if (!(cs instanceof SubjectCodeSource)) {
  1133. canonCs = new CodeSource(cs.getLocation(),
  1134. getSignerCertificates(cs));
  1135. } else {
  1136. SubjectCodeSource scs = (SubjectCodeSource)cs;
  1137. canonCs = new SubjectCodeSource(scs.getSubject(),
  1138. scs.getPrincipals(),
  1139. scs.getLocation(),
  1140. getSignerCertificates(scs));
  1141. }
  1142. }
  1143. }
  1144. } else {
  1145. if (extractSignerCerts) {
  1146. if (!(cs instanceof SubjectCodeSource)) {
  1147. canonCs = new CodeSource(cs.getLocation(),
  1148. getSignerCertificates(cs));
  1149. } else {
  1150. SubjectCodeSource scs = (SubjectCodeSource)cs;
  1151. canonCs = new SubjectCodeSource(scs.getSubject(),
  1152. scs.getPrincipals(),
  1153. scs.getLocation(),
  1154. getSignerCertificates(scs));
  1155. }
  1156. }
  1157. }
  1158. return canonCs;
  1159. }
  1160. /**
  1161. * Each entry in the policy configuration file is represented by a
  1162. * PolicyEntry object. <p>
  1163. *
  1164. * A PolicyEntry is a (CodeSource,Permission) pair. The
  1165. * CodeSource contains the (URL, PublicKey) that together identify
  1166. * where the Java bytecodes come from and who (if anyone) signed
  1167. * them. The URL could refer to localhost. The URL could also be
  1168. * null, meaning that this policy entry is given to all comers, as
  1169. * long as they match the signer field. The signer could be null,
  1170. * meaning the code is not signed. <p>
  1171. *
  1172. * The Permission contains the (Type, Name, Action) triplet. <p>
  1173. *
  1174. * For now, the Policy object retrieves the public key from the
  1175. * X.509 certificate on disk that corresponds to the signedBy
  1176. * alias specified in the Policy config file. For reasons of
  1177. * efficiency, the Policy object keeps a hashtable of certs already
  1178. * read in. This could be replaced by a secure internal key
  1179. * store.
  1180. *
  1181. * <p>
  1182. * For example, the entry
  1183. * <pre>
  1184. * permission java.io.File "/tmp", "read,write",
  1185. * signedBy "Duke";
  1186. * </pre>
  1187. * is represented internally
  1188. * <pre>
  1189. *
  1190. * FilePermission f = new FilePermission("/tmp", "read,write");
  1191. * PublicKey p = publickeys.get("Duke");
  1192. * URL u = InetAddress.getLocalHost();
  1193. * CodeBase c = new CodeBase( p, u );
  1194. * pe = new PolicyEntry(f, c);
  1195. * </pre>
  1196. *
  1197. * @author Marianne Mueller
  1198. * @author Roland Schemers
  1199. * @version 1.6, 03/04/97
  1200. * @see java.security.CodeSource
  1201. * @see java.security.Policy
  1202. * @see java.security.Permissions
  1203. * @see java.security.ProtectionDomain
  1204. */
  1205. private static class PolicyEntry {
  1206. CodeSource codesource;
  1207. Vector permissions;
  1208. /**
  1209. * Given a Permission and a CodeSource, create a policy entry.
  1210. *
  1211. * XXX Decide if/how to add validity fields and "purpose" fields to
  1212. * XXX policy entries
  1213. *
  1214. * @param cs the CodeSource, which encapsulates the URL and the public
  1215. * key
  1216. * attributes from the policy config file. Validity checks are
  1217. * performed on the public key before PolicyEntry is called.
  1218. *
  1219. */
  1220. PolicyEntry(CodeSource cs)
  1221. {
  1222. this.codesource = cs;
  1223. this.permissions = new Vector();
  1224. }
  1225. /**
  1226. * add a Permission object to this entry.
  1227. */
  1228. void add(Permission p) {
  1229. permissions.addElement(p);
  1230. }
  1231. /**
  1232. * Return the CodeSource for this policy entry
  1233. */
  1234. CodeSource getCodeSource() {
  1235. return this.codesource;
  1236. }
  1237. public String toString(){
  1238. StringBuffer sb = new StringBuffer();
  1239. sb.append(rb.getString("("));
  1240. sb.append(getCodeSource());
  1241. sb.append("\n");
  1242. for (int j = 0; j < permissions.size(); j++) {
  1243. Permission p = (Permission) permissions.elementAt(j);
  1244. sb.append(rb.getString(" "));
  1245. sb.append(rb.getString(" "));
  1246. sb.append(p);
  1247. sb.append(rb.getString("\n"));
  1248. }
  1249. sb.append(rb.getString(")"));
  1250. sb.append(rb.getString("\n"));
  1251. return sb.toString();
  1252. }
  1253. }
  1254. }
  1255. class PolicyPermissions extends PermissionCollection {
  1256. private static final long serialVersionUID = -1954188373270545523L;
  1257. private CodeSource codesource;
  1258. private Permissions perms;
  1259. private PolicyFile policy;
  1260. private boolean notInit; // have we pulled in the policy permissions yet?
  1261. private Vector additionalPerms;
  1262. PolicyPermissions(PolicyFile policy,
  1263. CodeSource codesource)
  1264. {
  1265. this.codesource = codesource;
  1266. this.policy = policy;
  1267. this.perms = null;
  1268. this.notInit = true;
  1269. this.additionalPerms = null;
  1270. }
  1271. public void add(Permission permission) {
  1272. if (isReadOnly())
  1273. throw new SecurityException
  1274. (PolicyFile.rb.getString
  1275. ("attempt to add a Permission to a readonly PermissionCollection"));
  1276. if (perms == null) {
  1277. if (additionalPerms == null)
  1278. additionalPerms = new Vector();
  1279. additionalPerms.add(permission);
  1280. } else {
  1281. perms.add(permission);
  1282. }
  1283. }
  1284. private synchronized void init() {
  1285. if (notInit) {
  1286. if (perms == null)
  1287. perms = new Permissions();
  1288. if (additionalPerms != null) {
  1289. Enumeration e = additionalPerms.elements();
  1290. while (e.hasMoreElements()) {
  1291. perms.add((Permission)e.nextElement());
  1292. }
  1293. additionalPerms = null;
  1294. }
  1295. policy.getPermissions(perms,codesource);
  1296. notInit=false;
  1297. }
  1298. }
  1299. public boolean implies(Permission permission) {
  1300. if (notInit)
  1301. init();
  1302. return perms.implies(permission);
  1303. }
  1304. public Enumeration elements() {
  1305. if (notInit)
  1306. init();
  1307. return perms.elements();
  1308. }
  1309. public String toString() {
  1310. if (notInit)
  1311. init();
  1312. return perms.toString();
  1313. }
  1314. }