PageRenderTime 114ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/javax/rmi/ssl/SslRMIClientSocketFactory.java

https://gitlab.com/borneywpf/openjdk-7-src
Java | 212 lines | 67 code | 10 blank | 135 comment | 12 complexity | 013e67b9fb00b2a051e33d116e830e97 MD5 | raw file
  1. /*
  2. * Copyright (c) 2003, 2008, 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 javax.rmi.ssl;
  26. import java.io.IOException;
  27. import java.io.Serializable;
  28. import java.net.Socket;
  29. import java.rmi.server.RMIClientSocketFactory;
  30. import java.util.StringTokenizer;
  31. import javax.net.SocketFactory;
  32. import javax.net.ssl.SSLSocket;
  33. import javax.net.ssl.SSLSocketFactory;
  34. /**
  35. * <p>An <code>SslRMIClientSocketFactory</code> instance is used by the RMI
  36. * runtime in order to obtain client sockets for RMI calls via SSL.</p>
  37. *
  38. * <p>This class implements <code>RMIClientSocketFactory</code> over
  39. * the Secure Sockets Layer (SSL) or Transport Layer Security (TLS)
  40. * protocols.</p>
  41. *
  42. * <p>This class creates SSL sockets using the default
  43. * <code>SSLSocketFactory</code> (see {@link
  44. * SSLSocketFactory#getDefault}). All instances of this class are
  45. * functionally equivalent. In particular, they all share the same
  46. * truststore, and the same keystore when client authentication is
  47. * required by the server. This behavior can be modified in
  48. * subclasses by overriding the {@link #createSocket(String,int)}
  49. * method; in that case, {@link #equals(Object) equals} and {@link
  50. * #hashCode() hashCode} may also need to be overridden.</p>
  51. *
  52. * <p>If the system property
  53. * <code>javax.rmi.ssl.client.enabledCipherSuites</code> is specified,
  54. * the {@link #createSocket(String,int)} method will call {@link
  55. * SSLSocket#setEnabledCipherSuites(String[])} before returning the
  56. * socket. The value of this system property is a string that is a
  57. * comma-separated list of SSL/TLS cipher suites to enable.</p>
  58. *
  59. * <p>If the system property
  60. * <code>javax.rmi.ssl.client.enabledProtocols</code> is specified,
  61. * the {@link #createSocket(String,int)} method will call {@link
  62. * SSLSocket#setEnabledProtocols(String[])} before returning the
  63. * socket. The value of this system property is a string that is a
  64. * comma-separated list of SSL/TLS protocol versions to enable.</p>
  65. *
  66. * @see javax.net.ssl.SSLSocketFactory
  67. * @see javax.rmi.ssl.SslRMIServerSocketFactory
  68. * @since 1.5
  69. */
  70. public class SslRMIClientSocketFactory
  71. implements RMIClientSocketFactory, Serializable {
  72. /**
  73. * <p>Creates a new <code>SslRMIClientSocketFactory</code>.</p>
  74. */
  75. public SslRMIClientSocketFactory() {
  76. // We don't force the initialization of the default SSLSocketFactory
  77. // at construction time - because the RMI client socket factory is
  78. // created on the server side, where that initialization is a priori
  79. // meaningless, unless both server and client run in the same JVM.
  80. // We could possibly override readObject() to force this initialization,
  81. // but it might not be a good idea to actually mix this with possible
  82. // deserialization problems.
  83. // So contrarily to what we do for the server side, the initialization
  84. // of the SSLSocketFactory will be delayed until the first time
  85. // createSocket() is called - note that the default SSLSocketFactory
  86. // might already have been initialized anyway if someone in the JVM
  87. // already called SSLSocketFactory.getDefault().
  88. //
  89. }
  90. /**
  91. * <p>Creates an SSL socket.</p>
  92. *
  93. * <p>If the system property
  94. * <code>javax.rmi.ssl.client.enabledCipherSuites</code> is
  95. * specified, this method will call {@link
  96. * SSLSocket#setEnabledCipherSuites(String[])} before returning
  97. * the socket. The value of this system property is a string that
  98. * is a comma-separated list of SSL/TLS cipher suites to
  99. * enable.</p>
  100. *
  101. * <p>If the system property
  102. * <code>javax.rmi.ssl.client.enabledProtocols</code> is
  103. * specified, this method will call {@link
  104. * SSLSocket#setEnabledProtocols(String[])} before returning the
  105. * socket. The value of this system property is a string that is a
  106. * comma-separated list of SSL/TLS protocol versions to
  107. * enable.</p>
  108. */
  109. public Socket createSocket(String host, int port) throws IOException {
  110. // Retrieve the SSLSocketFactory
  111. //
  112. final SocketFactory sslSocketFactory = getDefaultClientSocketFactory();
  113. // Create the SSLSocket
  114. //
  115. final SSLSocket sslSocket = (SSLSocket)
  116. sslSocketFactory.createSocket(host, port);
  117. // Set the SSLSocket Enabled Cipher Suites
  118. //
  119. final String enabledCipherSuites =
  120. System.getProperty("javax.rmi.ssl.client.enabledCipherSuites");
  121. if (enabledCipherSuites != null) {
  122. StringTokenizer st = new StringTokenizer(enabledCipherSuites, ",");
  123. int tokens = st.countTokens();
  124. String enabledCipherSuitesList[] = new String[tokens];
  125. for (int i = 0 ; i < tokens; i++) {
  126. enabledCipherSuitesList[i] = st.nextToken();
  127. }
  128. try {
  129. sslSocket.setEnabledCipherSuites(enabledCipherSuitesList);
  130. } catch (IllegalArgumentException e) {
  131. throw (IOException)
  132. new IOException(e.getMessage()).initCause(e);
  133. }
  134. }
  135. // Set the SSLSocket Enabled Protocols
  136. //
  137. final String enabledProtocols =
  138. System.getProperty("javax.rmi.ssl.client.enabledProtocols");
  139. if (enabledProtocols != null) {
  140. StringTokenizer st = new StringTokenizer(enabledProtocols, ",");
  141. int tokens = st.countTokens();
  142. String enabledProtocolsList[] = new String[tokens];
  143. for (int i = 0 ; i < tokens; i++) {
  144. enabledProtocolsList[i] = st.nextToken();
  145. }
  146. try {
  147. sslSocket.setEnabledProtocols(enabledProtocolsList);
  148. } catch (IllegalArgumentException e) {
  149. throw (IOException)
  150. new IOException(e.getMessage()).initCause(e);
  151. }
  152. }
  153. // Return the preconfigured SSLSocket
  154. //
  155. return sslSocket;
  156. }
  157. /**
  158. * <p>Indicates whether some other object is "equal to" this one.</p>
  159. *
  160. * <p>Because all instances of this class are functionally equivalent
  161. * (they all use the default
  162. * <code>SSLSocketFactory</code>), this method simply returns
  163. * <code>this.getClass().equals(obj.getClass())</code>.</p>
  164. *
  165. * <p>A subclass should override this method (as well
  166. * as {@link #hashCode()}) if its instances are not all
  167. * functionally equivalent.</p>
  168. */
  169. public boolean equals(Object obj) {
  170. if (obj == null) return false;
  171. if (obj == this) return true;
  172. return this.getClass().equals(obj.getClass());
  173. }
  174. /**
  175. * <p>Returns a hash code value for this
  176. * <code>SslRMIClientSocketFactory</code>.</p>
  177. *
  178. * @return a hash code value for this
  179. * <code>SslRMIClientSocketFactory</code>.
  180. */
  181. public int hashCode() {
  182. return this.getClass().hashCode();
  183. }
  184. // We use a static field because:
  185. //
  186. // SSLSocketFactory.getDefault() always returns the same object
  187. // (at least on Sun's implementation), and we want to make sure
  188. // that the Javadoc & the implementation stay in sync.
  189. //
  190. // If someone needs to have different SslRMIClientSocketFactory factories
  191. // with different underlying SSLSocketFactory objects using different key
  192. // and trust stores, he can always do so by subclassing this class and
  193. // overriding createSocket(String host, int port).
  194. //
  195. private static SocketFactory defaultSocketFactory = null;
  196. private static synchronized SocketFactory getDefaultClientSocketFactory() {
  197. if (defaultSocketFactory == null)
  198. defaultSocketFactory = SSLSocketFactory.getDefault();
  199. return defaultSocketFactory;
  200. }
  201. private static final long serialVersionUID = -8310631444933958385L;
  202. }