HttpsURLConnection.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* HttpsURLConnection.java -- an HTTPS connection.
  2. Copyright (C) 2004, 2006 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 javax.net.ssl;
  32. import java.net.HttpURLConnection;
  33. import java.net.URL;
  34. import java.security.Principal;
  35. import java.security.cert.Certificate;
  36. import java.security.cert.X509Certificate;
  37. /**
  38. * A URL connection that connects via the <i>Secure Socket Layer</i>
  39. * (<b>SSL</b>) for HTTPS connections.
  40. *
  41. * <p>This class may be used in the same way as {@link
  42. * HttpURLConnection}, and it will transparently negotiate the SSL
  43. * connection.
  44. *
  45. * @author Casey Marshall (rsdio@metastatic.org)
  46. */
  47. public abstract class HttpsURLConnection extends HttpURLConnection
  48. {
  49. // Fields.
  50. // ------------------------------------------------------------------
  51. /**
  52. * The default verifier.
  53. * This is lazily initialized as required.
  54. * @see #getDefaultHostnameVerifier
  55. */
  56. private static HostnameVerifier defaultVerifier;
  57. /**
  58. * The default factory.
  59. * This is lazily initialized as required.
  60. * @see #getDefaultSSLSocketFactory
  61. */
  62. private static SSLSocketFactory defaultFactory;
  63. /**
  64. * The hostname verifier used for this connection.
  65. */
  66. protected HostnameVerifier hostnameVerifier;
  67. /**
  68. * This connection's socket factory.
  69. */
  70. private SSLSocketFactory factory;
  71. // Constructor.
  72. // ------------------------------------------------------------------
  73. /**
  74. * Creates a new HTTPS URL connection.
  75. *
  76. * @param url The URL of the connection being established.
  77. * @specnote This was marked as throwing IOException in 1.4,
  78. * but this was removed in 1.5.
  79. */
  80. protected HttpsURLConnection(URL url)
  81. {
  82. super(url);
  83. }
  84. // Class methods.
  85. // ------------------------------------------------------------------
  86. /**
  87. * Returns the default hostname verifier used in all new
  88. * connections.
  89. * If the default verifier has not been set, a new default one will be
  90. * provided by this method.
  91. *
  92. * @return The default hostname verifier.
  93. */
  94. public static synchronized HostnameVerifier getDefaultHostnameVerifier()
  95. {
  96. if (defaultVerifier == null)
  97. {
  98. defaultVerifier = new TrivialHostnameVerifier();
  99. }
  100. return defaultVerifier;
  101. }
  102. /**
  103. * Sets the default hostname verifier to be used in all new
  104. * connections.
  105. *
  106. * @param newDefault The new default hostname verifier.
  107. * @throws IllegalArgumentException If <i>newDefault</i> is null.
  108. * @throws SecurityException If there is a security manager
  109. * currently installed and the caller does not have the {@link
  110. * SSLPermission} "setHostnameVerifier".
  111. */
  112. public static void setDefaultHostnameVerifier(HostnameVerifier newDefault)
  113. {
  114. if (newDefault == null)
  115. throw new IllegalArgumentException("default verifier cannot be null");
  116. SecurityManager sm = System.getSecurityManager();
  117. if (sm != null)
  118. sm.checkPermission(new SSLPermission("setHostnameVerifier"));
  119. synchronized (HttpsURLConnection.class)
  120. {
  121. defaultVerifier = newDefault;
  122. }
  123. }
  124. /**
  125. * Returns the default SSL socket factory used in all new
  126. * connections.
  127. * If the default SSL socket factory has not been set, a new default one
  128. * will be provided by this method.
  129. *
  130. * @return The default SSL socket factory.
  131. */
  132. public static synchronized SSLSocketFactory getDefaultSSLSocketFactory()
  133. {
  134. if (defaultFactory == null)
  135. {
  136. try
  137. {
  138. defaultFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
  139. }
  140. catch (Throwable t)
  141. {
  142. t.printStackTrace();
  143. }
  144. }
  145. return defaultFactory;
  146. }
  147. /**
  148. * Sets the default SSL socket factory to be used in all new
  149. * connections.
  150. *
  151. * @param newDefault The new socket factory.
  152. * @throws IllegalArgumentException If <i>newDefault</i> is null.
  153. * @throws SecurityException If there is a security manager
  154. * installed and a call to {@link
  155. * SecurityManager#checkSetFactory()} fails.
  156. */
  157. public static void setDefaultSSLSocketFactory(SSLSocketFactory newDefault)
  158. {
  159. if (newDefault == null)
  160. throw new IllegalArgumentException("default factory cannot be null");
  161. SecurityManager sm = System.getSecurityManager();
  162. if (sm != null)
  163. sm.checkSetFactory();
  164. synchronized (HttpsURLConnection.class)
  165. {
  166. defaultFactory = newDefault;
  167. }
  168. }
  169. // Instance methods.
  170. // ------------------------------------------------------------------
  171. /**
  172. * Returns the current hostname verifier for this instance.
  173. *
  174. * @return The hostname verifier.
  175. */
  176. public HostnameVerifier getHostnameVerifier()
  177. {
  178. if (hostnameVerifier == null)
  179. {
  180. hostnameVerifier = getDefaultHostnameVerifier();
  181. }
  182. return hostnameVerifier;
  183. }
  184. /**
  185. * Sets the hostname verifier for this instance.
  186. *
  187. * @param hostnameVerifier The new verifier.
  188. * @throws IllegalArgumentException If <i>hostnameVerifier</i> is
  189. * null.
  190. */
  191. public void setHostnameVerifier(HostnameVerifier hostnameVerifier)
  192. {
  193. if (hostnameVerifier == null)
  194. throw new IllegalArgumentException("verifier cannot be null");
  195. this.hostnameVerifier = hostnameVerifier;
  196. }
  197. /**
  198. * Returns the current SSL socket factory for this instance.
  199. *
  200. * @return The current SSL socket factory.
  201. */
  202. public SSLSocketFactory getSSLSocketFactory()
  203. {
  204. if (factory == null)
  205. {
  206. factory = getDefaultSSLSocketFactory();
  207. }
  208. return factory;
  209. }
  210. /**
  211. * Sets the SSL socket factory for this instance.
  212. *
  213. * @param factory The new factory.
  214. * @throws IllegalArgumentException If <i>factory</i> is null.
  215. */
  216. public void setSSLSocketFactory(SSLSocketFactory factory)
  217. {
  218. if (factory == null)
  219. throw new IllegalArgumentException("factory cannot be null");
  220. this.factory = factory;
  221. }
  222. /**
  223. * Returns the local principal for this connection.
  224. *
  225. * <p>The default implementation will return the {@link
  226. * javax.security.x500.X500Principal} for the end entity certificate
  227. * in the local certificate chain if those certificates are of type
  228. * {@link java.security.cert.X509Certificate}. Otherwise, this
  229. * method returns <code>null</code>.
  230. *
  231. * @return The local principal.
  232. * @since 1.5
  233. */
  234. public Principal getLocalPrincipal ()
  235. {
  236. Certificate[] c = getLocalCertificates ();
  237. if (c != null && c.length > 0 && (c[0] instanceof X509Certificate))
  238. return ((X509Certificate) c[0]).getSubjectX500Principal ();
  239. return null;
  240. }
  241. /**
  242. * Returns the remote peer's principal for this connection.
  243. *
  244. * <p>The default implementation will return the {@link
  245. * javax.security.x500.X500Principal} for the end entity certificate
  246. * in the remote peer's certificate chain if those certificates are
  247. * of type {@link java.security.cert.X509Certificate}. Otherwise,
  248. * this method returns <code>null</code>.
  249. *
  250. * @return The remote principal.
  251. * @throws SSLPeerUnverifiedException If the remote peer has not
  252. * been verified.
  253. * @since 1.5
  254. */
  255. public Principal getPeerPrincipal () throws SSLPeerUnverifiedException
  256. {
  257. Certificate[] c = getServerCertificates ();
  258. if (c != null && c.length > 0 && (c[0] instanceof X509Certificate))
  259. return ((X509Certificate) c[0]).getSubjectX500Principal ();
  260. return null;
  261. }
  262. // Abstract methods.
  263. // -------------------------------------------------------------------
  264. /**
  265. * Returns the cipher name negotiated for this connection.
  266. *
  267. * @return The cipher name.
  268. * @throws IllegalStateException If the connection has not yet been
  269. * established.
  270. */
  271. public abstract String getCipherSuite();
  272. /**
  273. * Returns the certificates used on the local side in this
  274. * connection.
  275. *
  276. * @return The local certificates.
  277. * @throws IllegalStateException If the connection has not yet been
  278. * established.
  279. */
  280. public abstract Certificate[] getLocalCertificates();
  281. /**
  282. * Returns the certificates sent by the other party.
  283. *
  284. * @return The peer's certificates.
  285. * @throws IllegalStateException If the connection has not yet been
  286. * established.
  287. * @throws SSLPeerUnverifiedException If the peer could not be
  288. * verified.
  289. */
  290. public abstract Certificate[] getServerCertificates() throws SSLPeerUnverifiedException;
  291. }