PageRenderTime 69ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/classes/java/security/UnresolvedPermission.java

#
Java | 604 lines | 294 code | 47 blank | 263 comment | 92 complexity | 536baa3726547612e9f8e502a58a7a39 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  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 java.security;
  26. import java.io.IOException;
  27. import java.io.ByteArrayInputStream;
  28. import java.util.ArrayList;
  29. import java.util.Enumeration;
  30. import java.util.Hashtable;
  31. import java.util.Vector;
  32. import java.lang.reflect.*;
  33. import java.security.cert.*;
  34. /**
  35. * The UnresolvedPermission class is used to hold Permissions that
  36. * were "unresolved" when the Policy was initialized.
  37. * An unresolved permission is one whose actual Permission class
  38. * does not yet exist at the time the Policy is initialized (see below).
  39. *
  40. * <p>The policy for a Java runtime (specifying
  41. * which permissions are available for code from various principals)
  42. * is represented by a Policy object.
  43. * Whenever a Policy is initialized or refreshed, Permission objects of
  44. * appropriate classes are created for all permissions
  45. * allowed by the Policy.
  46. *
  47. * <p>Many permission class types
  48. * referenced by the policy configuration are ones that exist
  49. * locally (i.e., ones that can be found on CLASSPATH).
  50. * Objects for such permissions can be instantiated during
  51. * Policy initialization. For example, it is always possible
  52. * to instantiate a java.io.FilePermission, since the
  53. * FilePermission class is found on the CLASSPATH.
  54. *
  55. * <p>Other permission classes may not yet exist during Policy
  56. * initialization. For example, a referenced permission class may
  57. * be in a JAR file that will later be loaded.
  58. * For each such class, an UnresolvedPermission is instantiated.
  59. * Thus, an UnresolvedPermission is essentially a "placeholder"
  60. * containing information about the permission.
  61. *
  62. * <p>Later, when code calls AccessController.checkPermission
  63. * on a permission of a type that was previously unresolved,
  64. * but whose class has since been loaded, previously-unresolved
  65. * permissions of that type are "resolved". That is,
  66. * for each such UnresolvedPermission, a new object of
  67. * the appropriate class type is instantiated, based on the
  68. * information in the UnresolvedPermission.
  69. *
  70. * <p> To instantiate the new class, UnresolvedPermission assumes
  71. * the class provides a zero, one, and/or two-argument constructor.
  72. * The zero-argument constructor would be used to instantiate
  73. * a permission without a name and without actions.
  74. * A one-arg constructor is assumed to take a <code>String</code>
  75. * name as input, and a two-arg constructor is assumed to take a
  76. * <code>String</code> name and <code>String</code> actions
  77. * as input. UnresolvedPermission may invoke a
  78. * constructor with a <code>null</code> name and/or actions.
  79. * If an appropriate permission constructor is not available,
  80. * the UnresolvedPermission is ignored and the relevant permission
  81. * will not be granted to executing code.
  82. *
  83. * <p> The newly created permission object replaces the
  84. * UnresolvedPermission, which is removed.
  85. *
  86. * <p> Note that the <code>getName</code> method for an
  87. * <code>UnresolvedPermission</code> returns the
  88. * <code>type</code> (class name) for the underlying permission
  89. * that has not been resolved.
  90. *
  91. * @see java.security.Permission
  92. * @see java.security.Permissions
  93. * @see java.security.PermissionCollection
  94. * @see java.security.Policy
  95. *
  96. *
  97. * @author Roland Schemers
  98. */
  99. public final class UnresolvedPermission extends Permission
  100. implements java.io.Serializable
  101. {
  102. private static final long serialVersionUID = -4821973115467008846L;
  103. private static final sun.security.util.Debug debug =
  104. sun.security.util.Debug.getInstance
  105. ("policy,access", "UnresolvedPermission");
  106. /**
  107. * The class name of the Permission class that will be
  108. * created when this unresolved permission is resolved.
  109. *
  110. * @serial
  111. */
  112. private String type;
  113. /**
  114. * The permission name.
  115. *
  116. * @serial
  117. */
  118. private String name;
  119. /**
  120. * The actions of the permission.
  121. *
  122. * @serial
  123. */
  124. private String actions;
  125. private transient java.security.cert.Certificate certs[];
  126. /**
  127. * Creates a new UnresolvedPermission containing the permission
  128. * information needed later to actually create a Permission of the
  129. * specified class, when the permission is resolved.
  130. *
  131. * @param type the class name of the Permission class that will be
  132. * created when this unresolved permission is resolved.
  133. * @param name the name of the permission.
  134. * @param actions the actions of the permission.
  135. * @param certs the certificates the permission's class was signed with.
  136. * This is a list of certificate chains, where each chain is composed of a
  137. * signer certificate and optionally its supporting certificate chain.
  138. * Each chain is ordered bottom-to-top (i.e., with the signer certificate
  139. * first and the (root) certificate authority last). The signer
  140. * certificates are copied from the array. Subsequent changes to
  141. * the array will not affect this UnsolvedPermission.
  142. */
  143. public UnresolvedPermission(String type,
  144. String name,
  145. String actions,
  146. java.security.cert.Certificate certs[])
  147. {
  148. super(type);
  149. if (type == null)
  150. throw new NullPointerException("type can't be null");
  151. this.type = type;
  152. this.name = name;
  153. this.actions = actions;
  154. if (certs != null) {
  155. // Extract the signer certs from the list of certificates.
  156. for (int i=0; i<certs.length; i++) {
  157. if (!(certs[i] instanceof X509Certificate)) {
  158. // there is no concept of signer certs, so we store the
  159. // entire cert array
  160. this.certs = certs.clone();
  161. break;
  162. }
  163. }
  164. if (this.certs == null) {
  165. // Go through the list of certs and see if all the certs are
  166. // signer certs.
  167. int i = 0;
  168. int count = 0;
  169. while (i < certs.length) {
  170. count++;
  171. while (((i+1) < certs.length) &&
  172. ((X509Certificate)certs[i]).getIssuerDN().equals(
  173. ((X509Certificate)certs[i+1]).getSubjectDN())) {
  174. i++;
  175. }
  176. i++;
  177. }
  178. if (count == certs.length) {
  179. // All the certs are signer certs, so we store the entire
  180. // array
  181. this.certs = certs.clone();
  182. }
  183. if (this.certs == null) {
  184. // extract the signer certs
  185. ArrayList<java.security.cert.Certificate> signerCerts =
  186. new ArrayList<>();
  187. i = 0;
  188. while (i < certs.length) {
  189. signerCerts.add(certs[i]);
  190. while (((i+1) < certs.length) &&
  191. ((X509Certificate)certs[i]).getIssuerDN().equals(
  192. ((X509Certificate)certs[i+1]).getSubjectDN())) {
  193. i++;
  194. }
  195. i++;
  196. }
  197. this.certs =
  198. new java.security.cert.Certificate[signerCerts.size()];
  199. signerCerts.toArray(this.certs);
  200. }
  201. }
  202. }
  203. }
  204. private static final Class[] PARAMS0 = { };
  205. private static final Class[] PARAMS1 = { String.class };
  206. private static final Class[] PARAMS2 = { String.class, String.class };
  207. /**
  208. * try and resolve this permission using the class loader of the permission
  209. * that was passed in.
  210. */
  211. Permission resolve(Permission p, java.security.cert.Certificate certs[]) {
  212. if (this.certs != null) {
  213. // if p wasn't signed, we don't have a match
  214. if (certs == null) {
  215. return null;
  216. }
  217. // all certs in this.certs must be present in certs
  218. boolean match;
  219. for (int i = 0; i < this.certs.length; i++) {
  220. match = false;
  221. for (int j = 0; j < certs.length; j++) {
  222. if (this.certs[i].equals(certs[j])) {
  223. match = true;
  224. break;
  225. }
  226. }
  227. if (!match) return null;
  228. }
  229. }
  230. try {
  231. Class pc = p.getClass();
  232. if (name == null && actions == null) {
  233. try {
  234. Constructor c = pc.getConstructor(PARAMS0);
  235. return (Permission)c.newInstance(new Object[] {});
  236. } catch (NoSuchMethodException ne) {
  237. try {
  238. Constructor c = pc.getConstructor(PARAMS1);
  239. return (Permission) c.newInstance(
  240. new Object[] { name});
  241. } catch (NoSuchMethodException ne1) {
  242. Constructor c = pc.getConstructor(PARAMS2);
  243. return (Permission) c.newInstance(
  244. new Object[] { name, actions });
  245. }
  246. }
  247. } else {
  248. if (name != null && actions == null) {
  249. try {
  250. Constructor c = pc.getConstructor(PARAMS1);
  251. return (Permission) c.newInstance(
  252. new Object[] { name});
  253. } catch (NoSuchMethodException ne) {
  254. Constructor c = pc.getConstructor(PARAMS2);
  255. return (Permission) c.newInstance(
  256. new Object[] { name, actions });
  257. }
  258. } else {
  259. Constructor c = pc.getConstructor(PARAMS2);
  260. return (Permission) c.newInstance(
  261. new Object[] { name, actions });
  262. }
  263. }
  264. } catch (NoSuchMethodException nsme) {
  265. if (debug != null ) {
  266. debug.println("NoSuchMethodException:\n could not find " +
  267. "proper constructor for " + type);
  268. nsme.printStackTrace();
  269. }
  270. return null;
  271. } catch (Exception e) {
  272. if (debug != null ) {
  273. debug.println("unable to instantiate " + name);
  274. e.printStackTrace();
  275. }
  276. return null;
  277. }
  278. }
  279. /**
  280. * This method always returns false for unresolved permissions.
  281. * That is, an UnresolvedPermission is never considered to
  282. * imply another permission.
  283. *
  284. * @param p the permission to check against.
  285. *
  286. * @return false.
  287. */
  288. public boolean implies(Permission p) {
  289. return false;
  290. }
  291. /**
  292. * Checks two UnresolvedPermission objects for equality.
  293. * Checks that <i>obj</i> is an UnresolvedPermission, and has
  294. * the same type (class) name, permission name, actions, and
  295. * certificates as this object.
  296. *
  297. * <p> To determine certificate equality, this method only compares
  298. * actual signer certificates. Supporting certificate chains
  299. * are not taken into consideration by this method.
  300. *
  301. * @param obj the object we are testing for equality with this object.
  302. *
  303. * @return true if obj is an UnresolvedPermission, and has the same
  304. * type (class) name, permission name, actions, and
  305. * certificates as this object.
  306. */
  307. public boolean equals(Object obj) {
  308. if (obj == this)
  309. return true;
  310. if (! (obj instanceof UnresolvedPermission))
  311. return false;
  312. UnresolvedPermission that = (UnresolvedPermission) obj;
  313. // check type
  314. if (!this.type.equals(that.type)) {
  315. return false;
  316. }
  317. // check name
  318. if (this.name == null) {
  319. if (that.name != null) {
  320. return false;
  321. }
  322. } else if (!this.name.equals(that.name)) {
  323. return false;
  324. }
  325. // check actions
  326. if (this.actions == null) {
  327. if (that.actions != null) {
  328. return false;
  329. }
  330. } else {
  331. if (!this.actions.equals(that.actions)) {
  332. return false;
  333. }
  334. }
  335. // check certs
  336. if ((this.certs == null && that.certs != null) ||
  337. (this.certs != null && that.certs == null) ||
  338. (this.certs != null && that.certs != null &&
  339. this.certs.length != that.certs.length)) {
  340. return false;
  341. }
  342. int i,j;
  343. boolean match;
  344. for (i = 0; this.certs != null && i < this.certs.length; i++) {
  345. match = false;
  346. for (j = 0; j < that.certs.length; j++) {
  347. if (this.certs[i].equals(that.certs[j])) {
  348. match = true;
  349. break;
  350. }
  351. }
  352. if (!match) return false;
  353. }
  354. for (i = 0; that.certs != null && i < that.certs.length; i++) {
  355. match = false;
  356. for (j = 0; j < this.certs.length; j++) {
  357. if (that.certs[i].equals(this.certs[j])) {
  358. match = true;
  359. break;
  360. }
  361. }
  362. if (!match) return false;
  363. }
  364. return true;
  365. }
  366. /**
  367. * Returns the hash code value for this object.
  368. *
  369. * @return a hash code value for this object.
  370. */
  371. public int hashCode() {
  372. int hash = type.hashCode();
  373. if (name != null)
  374. hash ^= name.hashCode();
  375. if (actions != null)
  376. hash ^= actions.hashCode();
  377. return hash;
  378. }
  379. /**
  380. * Returns the canonical string representation of the actions,
  381. * which currently is the empty string "", since there are no actions for
  382. * an UnresolvedPermission. That is, the actions for the
  383. * permission that will be created when this UnresolvedPermission
  384. * is resolved may be non-null, but an UnresolvedPermission
  385. * itself is never considered to have any actions.
  386. *
  387. * @return the empty string "".
  388. */
  389. public String getActions()
  390. {
  391. return "";
  392. }
  393. /**
  394. * Get the type (class name) of the underlying permission that
  395. * has not been resolved.
  396. *
  397. * @return the type (class name) of the underlying permission that
  398. * has not been resolved
  399. *
  400. * @since 1.5
  401. */
  402. public String getUnresolvedType() {
  403. return type;
  404. }
  405. /**
  406. * Get the target name of the underlying permission that
  407. * has not been resolved.
  408. *
  409. * @return the target name of the underlying permission that
  410. * has not been resolved, or <code>null</code>,
  411. * if there is no targe name
  412. *
  413. * @since 1.5
  414. */
  415. public String getUnresolvedName() {
  416. return name;
  417. }
  418. /**
  419. * Get the actions for the underlying permission that
  420. * has not been resolved.
  421. *
  422. * @return the actions for the underlying permission that
  423. * has not been resolved, or <code>null</code>
  424. * if there are no actions
  425. *
  426. * @since 1.5
  427. */
  428. public String getUnresolvedActions() {
  429. return actions;
  430. }
  431. /**
  432. * Get the signer certificates (without any supporting chain)
  433. * for the underlying permission that has not been resolved.
  434. *
  435. * @return the signer certificates for the underlying permission that
  436. * has not been resolved, or null, if there are no signer certificates.
  437. * Returns a new array each time this method is called.
  438. *
  439. * @since 1.5
  440. */
  441. public java.security.cert.Certificate[] getUnresolvedCerts() {
  442. return (certs == null) ? null : certs.clone();
  443. }
  444. /**
  445. * Returns a string describing this UnresolvedPermission. The convention
  446. * is to specify the class name, the permission name, and the actions, in
  447. * the following format: '(unresolved "ClassName" "name" "actions")'.
  448. *
  449. * @return information about this UnresolvedPermission.
  450. */
  451. public String toString() {
  452. return "(unresolved " + type + " " + name + " " + actions + ")";
  453. }
  454. /**
  455. * Returns a new PermissionCollection object for storing
  456. * UnresolvedPermission objects.
  457. * <p>
  458. * @return a new PermissionCollection object suitable for
  459. * storing UnresolvedPermissions.
  460. */
  461. public PermissionCollection newPermissionCollection() {
  462. return new UnresolvedPermissionCollection();
  463. }
  464. /**
  465. * Writes this object out to a stream (i.e., serializes it).
  466. *
  467. * @serialData An initial <code>String</code> denoting the
  468. * <code>type</code> is followed by a <code>String</code> denoting the
  469. * <code>name</code> is followed by a <code>String</code> denoting the
  470. * <code>actions</code> is followed by an <code>int</code> indicating the
  471. * number of certificates to follow
  472. * (a value of "zero" denotes that there are no certificates associated
  473. * with this object).
  474. * Each certificate is written out starting with a <code>String</code>
  475. * denoting the certificate type, followed by an
  476. * <code>int</code> specifying the length of the certificate encoding,
  477. * followed by the certificate encoding itself which is written out as an
  478. * array of bytes.
  479. */
  480. private void writeObject(java.io.ObjectOutputStream oos)
  481. throws IOException
  482. {
  483. oos.defaultWriteObject();
  484. if (certs==null || certs.length==0) {
  485. oos.writeInt(0);
  486. } else {
  487. // write out the total number of certs
  488. oos.writeInt(certs.length);
  489. // write out each cert, including its type
  490. for (int i=0; i < certs.length; i++) {
  491. java.security.cert.Certificate cert = certs[i];
  492. try {
  493. oos.writeUTF(cert.getType());
  494. byte[] encoded = cert.getEncoded();
  495. oos.writeInt(encoded.length);
  496. oos.write(encoded);
  497. } catch (CertificateEncodingException cee) {
  498. throw new IOException(cee.getMessage());
  499. }
  500. }
  501. }
  502. }
  503. /**
  504. * Restores this object from a stream (i.e., deserializes it).
  505. */
  506. private void readObject(java.io.ObjectInputStream ois)
  507. throws IOException, ClassNotFoundException
  508. {
  509. CertificateFactory cf;
  510. Hashtable<String, CertificateFactory> cfs = null;
  511. ois.defaultReadObject();
  512. if (type == null)
  513. throw new NullPointerException("type can't be null");
  514. // process any new-style certs in the stream (if present)
  515. int size = ois.readInt();
  516. if (size > 0) {
  517. // we know of 3 different cert types: X.509, PGP, SDSI, which
  518. // could all be present in the stream at the same time
  519. cfs = new Hashtable<String, CertificateFactory>(3);
  520. this.certs = new java.security.cert.Certificate[size];
  521. }
  522. for (int i=0; i<size; i++) {
  523. // read the certificate type, and instantiate a certificate
  524. // factory of that type (reuse existing factory if possible)
  525. String certType = ois.readUTF();
  526. if (cfs.containsKey(certType)) {
  527. // reuse certificate factory
  528. cf = cfs.get(certType);
  529. } else {
  530. // create new certificate factory
  531. try {
  532. cf = CertificateFactory.getInstance(certType);
  533. } catch (CertificateException ce) {
  534. throw new ClassNotFoundException
  535. ("Certificate factory for "+certType+" not found");
  536. }
  537. // store the certificate factory so we can reuse it later
  538. cfs.put(certType, cf);
  539. }
  540. // parse the certificate
  541. byte[] encoded=null;
  542. try {
  543. encoded = new byte[ois.readInt()];
  544. } catch (OutOfMemoryError oome) {
  545. throw new IOException("Certificate too big");
  546. }
  547. ois.readFully(encoded);
  548. ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
  549. try {
  550. this.certs[i] = cf.generateCertificate(bais);
  551. } catch (CertificateException ce) {
  552. throw new IOException(ce.getMessage());
  553. }
  554. bais.close();
  555. }
  556. }
  557. }