~ubuntu-branches/ubuntu/maverick/eucalyptus/maverick

« back to all changes in this revision

Viewing changes to clc/modules/hsqldb/src/main/java/org/hsqldb/HsqlSocketFactorySecure.java

  • Committer: Bazaar Package Importer
  • Author(s): Dave Walker (Daviey)
  • Date: 2010-07-21 17:27:10 UTC
  • mfrom: (1.1.38 upstream)
  • Revision ID: james.westby@ubuntu.com-20100721172710-7xv07dmdqgivc3t9
Tags: 2.0~bzr1211-0ubuntu1
* New major upstream version merge, 2.0 (r1211).
* debian/patches/:
  - 01-wsdl-stubs.patch, debian/wsdl.md5sums: wsdl stubs updated.
  - 02-Makefile.patch: Updated to reflect new code layout.
  - 07-local_support_euca_conf-in.patch: Updated to reflect new code layout.
  - 08-ubuntu-default-networking.patch: Refreshed.
  - 09-small-128-192MB.patch: Updated to point to new location.
  - 10-disable-iscsi.patch: Refreshed.
  - 11-state-cleanup-memleakfix.patch: Removed, fixed upstream.
  - 15-fix-default-ramdisk.patch: Updated to point to new location.
  - 16-kvm_libvirt_xml_default_use_kvm.patch: Updated to reflect changes.
  - 17-fix_walrus_OOM_errors.patch: Removed, fixed upstream.
  - 18-priv_security.patch: Updated to reflect upstream changes.
  - 20-brute-force-webui.patch: Updated to reflect upstream changes. 
  - 21-eucalyptus-1.7-with-gwt-1.6.4.patch: New patch, allows 
    eucalyptus-1.7 to be built against gwt 1.6.4. Based on patch courtesy 
    of Dmitrii Zagorodnov, upstream. (LP: #597330)
* debian/eucalyptus-java-common.links: 
  - Changed symlink for groovy, point to groovy.all.jar, making compatiable 
    with groovy versions >1.7. (LP: #595421)
  - Added ant.jar & jetty-rewrite-handler.jar as they are now required.
* debian/control
  - & debian/build-jars: Added libjavassist-java and libjetty-extra-java as 
    build dependencies.
  - Added libjetty-extra-java as a dependency of eucalyptus-java-common
* The binary resulting jar's have been renamed from eucalyptus-*-1.6.2.jar
  to eucalyptus-*-main.jar:    
  - debian/eucalyptus-cc.upstart
  - debian/eucalyptus-cloud.install
  - debian/eucalyptus-common.eucalyptus.upstart
  - debian/eucalyptus-java-common.install
  - debian/eucalyptus-network.upstart
  - debian/eucalyptus-sc.install
  - debian/eucalyptus-walrus.install
* debian/eucalyptus-java-common.install: New upstream jars that have been
  installed:
  - eucalyptus-db-hsqldb-ext-main.jar
  - eucalyptus-component-main.jar
* debian/control:
  - Updated Standards Version to 3.8.4 (no change)
  - Updated the upstream Homepage to: http://open.eucalyptus.com/
  - Changed Vcs-Bzr to reflect new location of Ubuntu hosted development branch.
  - Made the Build Dependency of groovy and the binary eucalyptus-java-common
    package depend on version >=1.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2001-2008, The HSQL Development Group
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * Redistributions of source code must retain the above copyright notice, this
 
8
 * list of conditions and the following disclaimer.
 
9
 *
 
10
 * Redistributions in binary form must reproduce the above copyright notice,
 
11
 * this list of conditions and the following disclaimer in the documentation
 
12
 * and/or other materials provided with the distribution.
 
13
 *
 
14
 * Neither the name of the HSQL Development Group nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from this
 
16
 * software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
21
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 
22
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
package org.hsqldb;
 
32
 
 
33
import java.io.File;
 
34
import java.io.FileInputStream;
 
35
import java.net.InetAddress;
 
36
import java.net.ServerSocket;
 
37
import java.net.Socket;
 
38
import java.security.InvalidAlgorithmParameterException;
 
39
import java.security.KeyStore;
 
40
import java.security.KeyStoreException;
 
41
import java.security.NoSuchAlgorithmException;
 
42
import java.security.Principal;
 
43
import java.security.PrivateKey;
 
44
import java.security.UnrecoverableKeyException;
 
45
import java.security.cert.CertificateException;
 
46
import java.security.cert.X509Certificate;
 
47
 
 
48
import javax.net.ssl.KeyManager;
 
49
import javax.net.ssl.KeyManagerFactorySpi;
 
50
import javax.net.ssl.ManagerFactoryParameters;
 
51
import javax.net.ssl.SSLContext;
 
52
import javax.net.ssl.SSLEngine;
 
53
import javax.net.ssl.SSLServerSocket;
 
54
import javax.net.ssl.SSLServerSocketFactory;
 
55
import javax.net.ssl.SSLSocket;
 
56
import javax.net.ssl.SSLSocketFactory;
 
57
import javax.net.ssl.TrustManager;
 
58
import javax.net.ssl.TrustManagerFactorySpi;
 
59
import javax.net.ssl.X509ExtendedKeyManager;
 
60
import org.apache.log4j.Logger;
 
61
import com.sun.net.ssl.internal.ssl.X509ExtendedTrustManager;
 
62
 
 
63
/**
 
64
 * We need to override the way that hsqldb gets its secure sockets to use our internally mananged credentials.
 
65
 */
 
66
public final class HsqlSocketFactorySecure extends HsqlSocketFactory {
 
67
  private static Logger LOG = Logger.getLogger( HsqlSocketFactorySecure.class );
 
68
  protected SSLSocketFactory       socketFactory;
 
69
  protected SSLServerSocketFactory serverSocketFactory;
 
70
  private static final String PROTOCOL       = "TLS";
 
71
  private static SSLContext   SERVER_CONTEXT = null;
 
72
  private static SSLContext   CLIENT_CONTEXT = null;
 
73
  private static String KEYS = System.getProperty("euca.var.dir") + File.separator + "keys";
 
74
  private static String KEYSTORE = KEYS + File.separator + "euca.p12";
 
75
  private static String EUCALYPTUS = "eucalyptus";
 
76
  private static PrivateKey pk = null; 
 
77
  private static X509Certificate cert = null;
 
78
  static {
 
79
    SSLContext serverContext = null;
 
80
    SSLContext clientContext = null;
 
81
    System.setProperty( "javax.net.ssl.trustStore", KEYSTORE );
 
82
    System.setProperty( "javax.net.ssl.keyStore", KEYSTORE );
 
83
    System.setProperty( "javax.net.ssl.trustStoreType", "PKCS12" );
 
84
    System.setProperty( "javax.net.ssl.keyStoreType", "PKCS12" );
 
85
    System.setProperty( "javax.net.ssl.trustStorePassword", EUCALYPTUS );
 
86
    System.setProperty( "javax.net.ssl.keyStorePassword", EUCALYPTUS );
 
87
    System.setProperty( "javax.net.debug", "none" );//set this to "ssl" for debugging.
 
88
    try {
 
89
      serverContext = SSLContext.getInstance( "TLS" );
 
90
      serverContext.init( HsqlSocketFactorySecure.SimpleKeyManager.getKeyManagers( ), HsqlSocketFactorySecure.SimpleTrustManager.getTrustManagers( ), null );
 
91
    } catch ( Exception e ) {
 
92
      LOG.debug( e, e );
 
93
      throw new Error( "Failed to initialize the server-side SSLContext", e );
 
94
    }
 
95
    
 
96
    try {
 
97
      clientContext = SSLContext.getInstance( "TLS" );
 
98
      clientContext.init( HsqlSocketFactorySecure.SimpleKeyManager.getKeyManagers( ), HsqlSocketFactorySecure.SimpleTrustManager.getTrustManagers( ), null );
 
99
    } catch ( Exception e ) {
 
100
      LOG.debug( e, e );
 
101
      throw new Error( "Failed to initialize the client-side SSLContext", e );
 
102
    }
 
103
    loadKeystore( );
 
104
    SERVER_CONTEXT = serverContext;
 
105
    CLIENT_CONTEXT = clientContext;
 
106
  }
 
107
 
 
108
  private static void loadKeystore( ) {
 
109
    try {
 
110
      KeyStore keyStore = KeyStore.getInstance( "pkcs12", "BC" );
 
111
      if ( ( new File( KEYSTORE ) ).exists( ) ) {
 
112
        final FileInputStream fin = new FileInputStream( KEYSTORE );
 
113
        keyStore.load( fin, EUCALYPTUS.toCharArray( ) );
 
114
        fin.close( );
 
115
        pk = ( PrivateKey ) keyStore.getKey( EUCALYPTUS, EUCALYPTUS.toCharArray( ) );
 
116
        cert = ( X509Certificate ) keyStore.getCertificate( EUCALYPTUS );
 
117
      } 
 
118
    } catch ( Throwable t ) {
 
119
      LOG.error( t, t );
 
120
    }
 
121
  }
 
122
  
 
123
  public static SSLContext getServerContext( ) {
 
124
    return SERVER_CONTEXT;
 
125
  }
 
126
 
 
127
  public static SSLEngine getServerEngine() {
 
128
    SSLEngine engine = HsqlSocketFactorySecure.getServerContext( ).createSSLEngine( );
 
129
    engine.setUseClientMode( false );
 
130
    return engine;
 
131
  }
 
132
  public static SSLContext getClientContext( ) {
 
133
    return CLIENT_CONTEXT;
 
134
  }
 
135
  
 
136
  public static class SimpleKeyManager extends KeyManagerFactorySpi {
 
137
    private static KeyManager singleton = new SimplePKCS12KeyManager( );
 
138
    
 
139
    public static KeyManager[] getKeyManagers( ) {
 
140
      return new KeyManager[] { singleton };
 
141
    }
 
142
    
 
143
    @Override
 
144
    protected KeyManager[] engineGetKeyManagers( ) {
 
145
      return new KeyManager[] { singleton };
 
146
    }
 
147
    
 
148
    @Override
 
149
    protected void engineInit( ManagerFactoryParameters spec ) throws InvalidAlgorithmParameterException {}
 
150
    
 
151
    @Override
 
152
    protected void engineInit( KeyStore ks, char[] password ) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {}
 
153
    
 
154
    public static class SimplePKCS12KeyManager extends X509ExtendedKeyManager {
 
155
      
 
156
      @Override
 
157
      public String chooseClientAlias( String[] arg0, Principal[] arg1, Socket arg2 ) {
 
158
        return EUCALYPTUS;
 
159
      }
 
160
      
 
161
      @Override
 
162
      public String chooseServerAlias( String arg0, Principal[] arg1, Socket arg2 ) {
 
163
        return EUCALYPTUS;
 
164
      }
 
165
      
 
166
      @Override
 
167
      public X509Certificate[] getCertificateChain( String arg0 ) {
 
168
        if ( EUCALYPTUS.equals( arg0 ) ) {
 
169
          return trustedCerts;
 
170
        } else {
 
171
          return null;
 
172
        }
 
173
      }
 
174
      
 
175
      @Override
 
176
      public String[] getClientAliases( String arg0, Principal[] arg1 ) {
 
177
        return new String[] { EUCALYPTUS };
 
178
      }
 
179
      
 
180
      @Override
 
181
      public PrivateKey getPrivateKey( String arg0 ) {
 
182
        if ( EUCALYPTUS.equals( arg0 ) ) {
 
183
          return trustedKey;
 
184
        } else {
 
185
          return null;
 
186
        }
 
187
      }
 
188
      
 
189
      @Override
 
190
      public String[] getServerAliases( String arg0, Principal[] arg1 ) {
 
191
        return new String[] { EUCALYPTUS };
 
192
      }
 
193
      
 
194
      @Override
 
195
      public String chooseEngineClientAlias( String[] keyType, Principal[] issuers, SSLEngine engine ) {
 
196
        return EUCALYPTUS;
 
197
      }
 
198
      
 
199
      @Override
 
200
      public String chooseEngineServerAlias( String keyType, Principal[] issuers, SSLEngine engine ) {
 
201
        return EUCALYPTUS;
 
202
      }
 
203
      
 
204
    }
 
205
    
 
206
  }
 
207
  
 
208
  private static PrivateKey        trustedKey   = null;
 
209
  private static X509Certificate   trusted      = getTrustedCertificate( );
 
210
  private static X509Certificate[] trustedCerts = new X509Certificate[] { trusted };
 
211
  
 
212
  private static X509Certificate getTrustedCertificate( ) {
 
213
    try {
 
214
      synchronized ( HsqlSocketFactorySecure.class ) {
 
215
        if ( trusted == null ) {
 
216
          trusted = cert;
 
217
          trustedKey = pk;
 
218
        }
 
219
        return trusted;
 
220
      }
 
221
    } catch ( Exception e ) {
 
222
      LOG.error( e, e );
 
223
      throw new RuntimeException( e );
 
224
    }
 
225
  }
 
226
  
 
227
  public static class SimpleTrustManager extends TrustManagerFactorySpi {
 
228
    private static Logger             LOG       = Logger.getLogger( HsqlSocketFactorySecure.SimpleTrustManager.class );
 
229
    
 
230
    private static final TrustManager singleton = new SimpleX509TrustManager( );
 
231
    
 
232
    public static TrustManager[] getTrustManagers( ) {
 
233
      return new TrustManager[] { singleton };
 
234
    }
 
235
    
 
236
    @Override
 
237
    protected TrustManager[] engineGetTrustManagers( ) {
 
238
      return getTrustManagers( );
 
239
    }
 
240
    
 
241
    @Override
 
242
    protected void engineInit( KeyStore keystore ) throws KeyStoreException {}
 
243
    
 
244
    @Override
 
245
    protected void engineInit( ManagerFactoryParameters managerFactoryParameters ) throws InvalidAlgorithmParameterException {}
 
246
    
 
247
    public static class SimpleX509TrustManager extends X509ExtendedTrustManager {
 
248
      
 
249
      @Override
 
250
      public void checkClientTrusted( X509Certificate[] arg0, String arg1 ) throws CertificateException {}
 
251
      
 
252
      @Override
 
253
      public void checkServerTrusted( X509Certificate[] chain, String authType ) throws CertificateException {}
 
254
      
 
255
      @Override
 
256
      public X509Certificate[] getAcceptedIssuers( ) {
 
257
        return trustedCerts;
 
258
      }
 
259
      
 
260
      @Override
 
261
      public void checkClientTrusted( X509Certificate[] arg0, String arg1, String arg2, String arg3 ) throws CertificateException {}
 
262
      
 
263
      @Override
 
264
      public void checkServerTrusted( X509Certificate[] arg0, String arg1, String arg2, String arg3 ) throws CertificateException {}
 
265
    }
 
266
    
 
267
  }
 
268
  
 
269
  protected HsqlSocketFactorySecure( ) throws Exception {}
 
270
  
 
271
  public void configureSocket( Socket socket ) {
 
272
    super.configureSocket( socket );
 
273
  }
 
274
  
 
275
  public ServerSocket createServerSocket( int port ) throws Exception {
 
276
    return ( SSLServerSocket ) getServerSocketFactoryImpl( ).createServerSocket( port );
 
277
  }
 
278
  
 
279
  public ServerSocket createServerSocket( int port, String address ) throws Exception {
 
280
    InetAddress addr = InetAddress.getByName( address );
 
281
    return ( SSLServerSocket ) getServerSocketFactoryImpl( ).createServerSocket( port, 128, addr );
 
282
  }
 
283
  
 
284
  public Socket createSocket( String host, int port ) throws Exception {
 
285
    SSLSocket socket = ( SSLSocket ) getSocketFactoryImpl( ).createSocket( host, port );
 
286
    socket.startHandshake( );
 
287
    return socket;
 
288
  }
 
289
  
 
290
  public boolean isSecure( ) {
 
291
    return true;
 
292
  }
 
293
  
 
294
  protected SSLServerSocketFactory getServerSocketFactoryImpl( ) throws Exception {
 
295
    synchronized ( HsqlSocketFactorySecure.class ) {
 
296
      if ( serverSocketFactory == null ) {
 
297
        serverSocketFactory =  HsqlSocketFactorySecure.getServerContext( ).getServerSocketFactory( );
 
298
      }
 
299
    }
 
300
    return ( SSLServerSocketFactory ) serverSocketFactory;
 
301
  }
 
302
  
 
303
  protected SSLSocketFactory getSocketFactoryImpl( ) throws Exception {
 
304
    synchronized ( HsqlSocketFactorySecure.class ) {
 
305
      if ( socketFactory == null ) {
 
306
        socketFactory =  HsqlSocketFactorySecure.getClientContext( ).getSocketFactory( );
 
307
      }
 
308
    }
 
309
    return ( SSLSocketFactory ) socketFactory;
 
310
  }
 
311
  
 
312
}