View Javadoc

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