CodeSource.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* CodeSource.java -- Code location and certifcates
  2. Copyright (C) 1998, 2002, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.ByteArrayInputStream;
  34. import java.io.IOException;
  35. import java.io.ObjectInputStream;
  36. import java.io.ObjectOutputStream;
  37. import java.io.Serializable;
  38. import java.net.SocketPermission;
  39. import java.net.URL;
  40. // Note that this overrides Certificate in this package.
  41. import java.security.cert.Certificate;
  42. import java.security.cert.CertificateEncodingException;
  43. import java.security.cert.CertificateException;
  44. import java.security.cert.CertificateFactory;
  45. import java.util.Arrays;
  46. import java.util.HashSet;
  47. import java.util.Iterator;
  48. /**
  49. * This class represents a location from which code is loaded (as
  50. * represented by a URL), and the list of certificates that are used to
  51. * check the signatures of signed code loaded from this source.
  52. *
  53. * @author Aaron M. Renn (arenn@urbanophile.com)
  54. * @author Eric Blake (ebb9@email.byu.edu)
  55. * @since 1.1
  56. * @status updated to 1.4
  57. */
  58. public class CodeSource implements Serializable
  59. {
  60. /**
  61. * Compatible with JDK 1.1+.
  62. */
  63. private static final long serialVersionUID = 4977541819976013951L;
  64. /**
  65. * This is the URL that represents the code base from which code will
  66. * be loaded.
  67. *
  68. * @serial the code location
  69. */
  70. private final URL location;
  71. /** The set of certificates for this code base. */
  72. private transient HashSet certs;
  73. /**
  74. * This creates a new instance of <code>CodeSource</code> that loads code
  75. * from the specified URL location and which uses the specified certificates
  76. * for verifying signatures.
  77. *
  78. * @param location the location from which code will be loaded
  79. * @param certs the list of certificates
  80. */
  81. public CodeSource(URL location, Certificate[] certs)
  82. {
  83. this.location = location;
  84. if (certs != null)
  85. this.certs = new HashSet(Arrays.asList(certs));
  86. }
  87. /**
  88. * This method returns a hash value for this object.
  89. *
  90. * @return a hash value for this object
  91. */
  92. public int hashCode()
  93. {
  94. return (location == null ? 0 : location.hashCode())
  95. ^ (certs == null ? 0 : certs.hashCode());
  96. }
  97. /**
  98. * This method tests the specified <code>Object</code> for equality with
  99. * this object. This will be true if and only if the locations are equal
  100. * and the certificate sets are identical (ignoring order).
  101. *
  102. * @param obj the <code>Object</code> to test against
  103. * @return true if the specified object is equal to this one
  104. */
  105. public boolean equals(Object obj)
  106. {
  107. if (! (obj instanceof CodeSource))
  108. return false;
  109. CodeSource cs = (CodeSource) obj;
  110. return (certs == null ? cs.certs == null : certs.equals(cs.certs))
  111. && (location == null ? cs.location == null
  112. : location.equals(cs.location));
  113. }
  114. /**
  115. * This method returns the URL specifying the location from which code
  116. * will be loaded under this <code>CodeSource</code>.
  117. *
  118. * @return the code location for this <code>CodeSource</code>
  119. */
  120. public final URL getLocation()
  121. {
  122. return location;
  123. }
  124. /**
  125. * This method returns the list of digital certificates that can be used
  126. * to verify the signatures of code loaded under this
  127. * <code>CodeSource</code>.
  128. *
  129. * @return the certifcate list for this <code>CodeSource</code>
  130. */
  131. public final Certificate[] getCertificates()
  132. {
  133. if (certs == null)
  134. return null;
  135. Certificate[] c = new Certificate[certs.size()];
  136. certs.toArray(c);
  137. return c;
  138. }
  139. /**
  140. * This method tests to see if a specified <code>CodeSource</code> is
  141. * implied by this object. Effectively, to meet this test, the specified
  142. * object must have all the certifcates this object has (but may have more),
  143. * and must have a location that is a subset of this object's. In order
  144. * for this object to imply the specified object, the following must be
  145. * true:
  146. *
  147. * <ol>
  148. * <li><em>codesource</em> must not be <code>null</code>.</li>
  149. * <li>If <em>codesource</em> has a certificate list, all of it's
  150. * certificates must be present in the certificate list of this
  151. * code source.</li>
  152. * <li>If this object does not have a <code>null</code> location, then
  153. * the following addtional tests must be passed.
  154. *
  155. * <ol>
  156. * <li><em>codesource</em> must not have a <code>null</code>
  157. * location.</li>
  158. * <li><em>codesource</em>'s location must be equal to this object's
  159. * location, or
  160. * <ul>
  161. * <li><em>codesource</em>'s location protocol, port, and ref (aka,
  162. * anchor) must equal this objects</li>
  163. * <li><em>codesource</em>'s location host must imply this object's
  164. * location host, as determined by contructing
  165. * <code>SocketPermission</code> objects from each with no
  166. * action list and using that classes's <code>implies</code>
  167. * method</li>
  168. * <li>If this object's location file ends with a '/', then the
  169. * specified object's location file must start with this
  170. * object's location file. Otherwise, the specified object's
  171. * location file must start with this object's location file
  172. * with the '/' character appended to it.</li>
  173. * </ul></li>
  174. * </ol></li>
  175. * </ol>
  176. *
  177. * <p>For example, each of these locations imply the location
  178. * "http://java.sun.com/classes/foo.jar":</p>
  179. *
  180. * <pre>
  181. * http:
  182. * http://*.sun.com/classes/*
  183. * http://java.sun.com/classes/-
  184. * http://java.sun.com/classes/foo.jar
  185. * </pre>
  186. *
  187. * <p>Note that the code source with null location and null certificates implies
  188. * all other code sources.</p>
  189. *
  190. * @param cs the <code>CodeSource</code> to test against this object
  191. * @return true if this specified <code>CodeSource</code> is implied
  192. */
  193. public boolean implies(CodeSource cs)
  194. {
  195. if (cs == null)
  196. return false;
  197. // First check the certificate list.
  198. if (certs != null && (cs.certs == null || ! certs.containsAll(cs.certs)))
  199. return false;
  200. // Next check the location.
  201. if (location == null)
  202. return true;
  203. if (cs.location == null
  204. || ! location.getProtocol().equals(cs.location.getProtocol())
  205. || (location.getPort() != -1
  206. && location.getPort() != cs.location.getPort())
  207. || (location.getRef() != null
  208. && ! location.getRef().equals(cs.location.getRef())))
  209. return false;
  210. if (location.getHost() != null)
  211. {
  212. String their_host = cs.location.getHost();
  213. if (their_host == null)
  214. return false;
  215. SocketPermission our_sockperm =
  216. new SocketPermission(location.getHost(), "accept");
  217. SocketPermission their_sockperm =
  218. new SocketPermission(their_host, "accept");
  219. if (! our_sockperm.implies(their_sockperm))
  220. return false;
  221. }
  222. String our_file = location.getFile();
  223. if (our_file != null)
  224. {
  225. if (! our_file.endsWith("/"))
  226. our_file += "/";
  227. String their_file = cs.location.getFile();
  228. if (their_file == null
  229. || ! their_file.startsWith(our_file))
  230. return false;
  231. }
  232. return true;
  233. }
  234. /**
  235. * This method returns a <code>String</code> that represents this object.
  236. * The result is in the format <code>"(" + getLocation()</code> followed
  237. * by a space separated list of certificates (or "&lt;no certificates&gt;"),
  238. * followed by <code>")"</code>.
  239. *
  240. * @return a <code>String</code> for this object
  241. */
  242. public String toString()
  243. {
  244. CPStringBuilder sb = new CPStringBuilder("(").append(location);
  245. if (certs == null || certs.isEmpty())
  246. sb.append(" <no certificates>");
  247. else
  248. {
  249. Iterator iter = certs.iterator();
  250. for (int i = certs.size(); --i >= 0; )
  251. sb.append(' ').append(iter.next());
  252. }
  253. return sb.append(")").toString();
  254. }
  255. /**
  256. * Reads this object from a serialization stream.
  257. *
  258. * @param s the input stream
  259. * @throws IOException if reading fails
  260. * @throws ClassNotFoundException if deserialization fails
  261. * @serialData this reads the location, then expects an int indicating the
  262. * number of certificates. Each certificate is a String type
  263. * followed by an int encoding length, then a byte[] encoding
  264. */
  265. private void readObject(ObjectInputStream s)
  266. throws IOException, ClassNotFoundException
  267. {
  268. s.defaultReadObject();
  269. int count = s.readInt();
  270. certs = new HashSet();
  271. while (--count >= 0)
  272. {
  273. String type = (String) s.readObject();
  274. int bytes = s.readInt();
  275. byte[] encoded = new byte[bytes];
  276. for (int i = 0; i < bytes; i++)
  277. encoded[i] = s.readByte();
  278. ByteArrayInputStream stream = new ByteArrayInputStream(encoded);
  279. try
  280. {
  281. CertificateFactory factory = CertificateFactory.getInstance(type);
  282. certs.add(factory.generateCertificate(stream));
  283. }
  284. catch (CertificateException e)
  285. {
  286. // XXX Should we ignore this certificate?
  287. }
  288. }
  289. }
  290. /**
  291. * Writes this object to a serialization stream.
  292. *
  293. * @param s the output stream
  294. * @throws IOException if writing fails
  295. * @serialData this writes the location, then writes an int indicating the
  296. * number of certificates. Each certificate is a String type
  297. * followed by an int encoding length, then a byte[] encoding
  298. */
  299. private void writeObject(ObjectOutputStream s) throws IOException
  300. {
  301. s.defaultWriteObject();
  302. if (certs == null)
  303. s.writeInt(0);
  304. else
  305. {
  306. int count = certs.size();
  307. s.writeInt(count);
  308. Iterator iter = certs.iterator();
  309. while (--count >= 0)
  310. {
  311. Certificate c = (Certificate) iter.next();
  312. s.writeObject(c.getType());
  313. byte[] encoded;
  314. try
  315. {
  316. encoded = c.getEncoded();
  317. }
  318. catch (CertificateEncodingException e)
  319. {
  320. // XXX Should we ignore this certificate?
  321. encoded = null;
  322. }
  323. if (encoded == null)
  324. s.writeInt(0);
  325. else
  326. {
  327. s.writeInt(encoded.length);
  328. for (int i = 0; i < encoded.length; i++)
  329. s.writeByte(encoded[i]);
  330. }
  331. }
  332. }
  333. }
  334. } // class CodeSource