View Javadoc

1   /*
2    * $Id: SslConnector.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.ssl;
12  
13  import org.mule.api.lifecycle.CreateException;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.security.TlsDirectKeyStore;
16  import org.mule.api.security.TlsDirectTrustStore;
17  import org.mule.api.security.TlsIndirectKeyStore;
18  import org.mule.api.security.TlsProtocolHandler;
19  import org.mule.api.security.provider.SecurityProviderFactory;
20  import org.mule.api.security.tls.TlsConfiguration;
21  import org.mule.transport.tcp.TcpConnector;
22  import org.mule.transport.tcp.protocols.DirectProtocol;
23  
24  import java.io.IOException;
25  import java.net.ServerSocket;
26  import java.net.URI;
27  import java.security.Provider;
28  
29  import javax.net.ssl.KeyManagerFactory;
30  import javax.net.ssl.SSLServerSocket;
31  import javax.net.ssl.TrustManagerFactory;
32  
33  /**
34   * <code>SslConnector</code> provides a connector for SSL connections.
35   * Note that the *only* function of the code in this package is to configure and
36   * provide SSL enabled sockets.  All other logic is identical to TCP.
37   */
38  public class SslConnector extends TcpConnector
39          implements TlsDirectKeyStore, TlsIndirectKeyStore, TlsDirectTrustStore, TlsProtocolHandler
40  {
41  
42      public static final String SSL = "ssl";
43      public static final String PEER_CERTIFICATES = "PEER_CERTIFICATES";
44      public static final String LOCAL_CERTIFICATES = "LOCAL_CERTIFICATES";
45  
46      // null initial keystore - see below
47      private TlsConfiguration tls = new TlsConfiguration(null);
48  
49      public SslConnector()
50      {
51          setSocketFactory(new SslSocketFactory(tls));
52          setServerSocketFactory(new SslServerSocketFactory(tls));
53          setTcpProtocol(new DirectProtocol());
54          // setting this true causes problems as socket closes before handshake finishes
55          setValidateConnections(false);
56      }
57  
58      // @Override
59      protected void doInitialise() throws InitialisationException
60      {
61          super.doInitialise();
62          // the original logic here was slightly different to other uses of the TlsSupport code -
63          // it appeared to be equivalent to switching anon by whether or not a keyStore was defined
64          // (which seems to make sense), so that is used here.
65          try
66          {
67              tls.initialise(null == getKeyStore(), TlsConfiguration.JSSE_NAMESPACE);
68          }
69          catch (CreateException e)
70          {
71              throw new InitialisationException(e, this);
72          }
73      }
74  
75      // @Override
76      protected ServerSocket getServerSocket(URI uri) throws IOException
77      {
78          SSLServerSocket serverSocket = (SSLServerSocket) super.getServerSocket(uri);
79          serverSocket.setNeedClientAuth(isRequireClientAuthentication());
80          return serverSocket;
81      }
82  
83      // @Override
84      public String getProtocol()
85      {
86          return SSL;
87      }
88  
89      public String getClientKeyStore()
90      {
91          return tls.getClientKeyStore();
92      }
93  
94      public String getClientKeyStorePassword()
95      {
96          return tls.getClientKeyStorePassword();
97      }
98  
99      public String getClientKeyStoreType()
100     {
101         return this.tls.getClientKeyStoreType();
102     }
103 
104     public String getKeyManagerAlgorithm()
105     {
106         return tls.getKeyManagerAlgorithm();
107     }
108 
109     public KeyManagerFactory getKeyManagerFactory()
110     {
111         return tls.getKeyManagerFactory();
112     }
113 
114     public String getKeyPassword()
115     {
116         return tls.getKeyPassword();
117     }
118 
119     public String getKeyStore()
120     {
121         return tls.getKeyStore();
122     }
123 
124     public String getKeyStoreType()
125     {
126         return tls.getKeyStoreType();
127     }
128 
129     public String getProtocolHandler()
130     {
131         return tls.getProtocolHandler();
132     }
133 
134     public Provider getProvider()
135     {
136         return tls.getProvider();
137     }
138 
139     public SecurityProviderFactory getSecurityProviderFactory()
140     {
141         return tls.getSecurityProviderFactory();
142     }
143 
144     public String getSslType()
145     {
146         return tls.getSslType();
147     }
148 
149     public String getKeyStorePassword()
150     {
151         return tls.getKeyStorePassword();
152     }
153 
154     public String getTrustManagerAlgorithm()
155     {
156         return tls.getTrustManagerAlgorithm();
157     }
158 
159     public TrustManagerFactory getTrustManagerFactory()
160     {
161         return tls.getTrustManagerFactory();
162     }
163 
164     public String getTrustStore()
165     {
166         return tls.getTrustStore();
167     }
168 
169     public String getTrustStorePassword()
170     {
171         return tls.getTrustStorePassword();
172     }
173 
174     public String getTrustStoreType()
175     {
176         return tls.getTrustStoreType();
177     }
178 
179     public boolean isExplicitTrustStoreOnly()
180     {
181         return tls.isExplicitTrustStoreOnly();
182     }
183 
184     public boolean isRequireClientAuthentication()
185     {
186         return tls.isRequireClientAuthentication();
187     }
188 
189     public void setClientKeyStore(String clientKeyStore) throws IOException
190     {
191         tls.setClientKeyStore(clientKeyStore);
192     }
193 
194     public void setClientKeyStorePassword(String clientKeyStorePassword)
195     {
196         tls.setClientKeyStorePassword(clientKeyStorePassword);
197     }
198 
199     public void setClientKeyStoreType(String clientKeyStoreType)
200     {
201         this.tls.setClientKeyStoreType(clientKeyStoreType);
202     }
203 
204     public void setExplicitTrustStoreOnly(boolean explicitTrustStoreOnly)
205     {
206         tls.setExplicitTrustStoreOnly(explicitTrustStoreOnly);
207     }
208 
209     public void setKeyManagerAlgorithm(String keyManagerAlgorithm)
210     {
211         tls.setKeyManagerAlgorithm(keyManagerAlgorithm);
212     }
213 
214     public void setKeyPassword(String keyPassword)
215     {
216         tls.setKeyPassword(keyPassword);
217     }
218 
219     public void setKeyStore(String keyStore) throws IOException
220     {
221         tls.setKeyStore(keyStore);
222     }
223 
224     public void setKeyStoreType(String keystoreType)
225     {
226         tls.setKeyStoreType(keystoreType);
227     }
228 
229     public void setProtocolHandler(String protocolHandler)
230     {
231         tls.setProtocolHandler(protocolHandler);
232     }
233 
234     public void setProvider(Provider provider)
235     {
236         tls.setProvider(provider);
237     }
238 
239     public void setRequireClientAuthentication(boolean requireClientAuthentication)
240     {
241         tls.setRequireClientAuthentication(requireClientAuthentication);
242     }
243 
244     public void setSecurityProviderFactory(SecurityProviderFactory spFactory)
245     {
246         tls.setSecurityProviderFactory(spFactory);
247     }
248 
249     public void setSslType(String sslType)
250     {
251         tls.setSslType(sslType);
252     }
253 
254     public void setKeyStorePassword(String storePassword)
255     {
256         tls.setKeyStorePassword(storePassword);
257     }
258 
259     public void setTrustManagerAlgorithm(String trustManagerAlgorithm)
260     {
261         tls.setTrustManagerAlgorithm(trustManagerAlgorithm);
262     }
263 
264     public void setTrustManagerFactory(TrustManagerFactory trustManagerFactory)
265     {
266         tls.setTrustManagerFactory(trustManagerFactory);
267     }
268 
269     public void setTrustStore(String trustStore) throws IOException
270     {
271         tls.setTrustStore(trustStore);
272     }
273 
274     public void setTrustStorePassword(String trustStorePassword)
275     {
276         tls.setTrustStorePassword(trustStorePassword);
277     }
278 
279     public void setTrustStoreType(String trustStoreType)
280     {
281         tls.setTrustStoreType(trustStoreType);
282     }
283 
284 }