~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/security/SSLSocketBuilder.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
package org.jboss.remoting.security;
 
23
 
 
24
import org.jboss.logging.Logger;
 
25
import org.jboss.remoting.serialization.ClassLoaderUtility;
 
26
import org.jboss.remoting.util.SecurityUtility;
 
27
import org.jboss.remoting.util.socket.RemotingKeyManager;
 
28
 
 
29
import javax.net.ServerSocketFactory;
 
30
import javax.net.SocketFactory;
 
31
import javax.net.ssl.KeyManager;
 
32
import javax.net.ssl.KeyManagerFactory;
 
33
import javax.net.ssl.SSLContext;
 
34
import javax.net.ssl.SSLServerSocketFactory;
 
35
import javax.net.ssl.SSLSocketFactory;
 
36
import javax.net.ssl.TrustManager;
 
37
import javax.net.ssl.TrustManagerFactory;
 
38
import javax.net.ssl.X509KeyManager;
 
39
import javax.net.ssl.X509TrustManager;
 
40
import java.io.File;
 
41
import java.io.IOException;
 
42
import java.io.InputStream;
 
43
import java.lang.reflect.Method;
 
44
import java.net.MalformedURLException;
 
45
import java.net.URL;
 
46
import java.security.AccessController;
 
47
import java.security.KeyStore;
 
48
import java.security.KeyStoreException;
 
49
import java.security.NoSuchAlgorithmException;
 
50
import java.security.NoSuchProviderException;
 
51
import java.security.PrivilegedAction;
 
52
import java.security.PrivilegedActionException;
 
53
import java.security.PrivilegedExceptionAction;
 
54
import java.security.Provider;
 
55
import java.security.SecureRandom;
 
56
import java.security.UnrecoverableKeyException;
 
57
import java.security.cert.CertificateException;
 
58
import java.security.cert.X509Certificate;
 
59
import java.util.Map;
 
60
 
 
61
/**
 
62
 * A class that contains code that remoting factories need to build customized server and client SSL sockets.
 
63
 *
 
64
 * @author <a href="mailto:mazz@jboss.com">John Mazzitelli</a>
 
65
 * @author <a href="mailto:telrod@jboss.com">Tom Elrod</a>
 
66
 *
 
67
 * @version $Revision: 5689 $
 
68
 */
 
69
public class SSLSocketBuilder implements SSLSocketBuilderMBean, Cloneable
 
70
{
 
71
   /**
 
72
    * Constant defining the config property used to define the SSL provider to use.
 
73
    */
 
74
   public static final String REMOTING_SSL_PROVIDER_NAME = "org.jboss.remoting.sslProviderName";
 
75
 
 
76
   /**
 
77
    * Constant defining the config property used to define the SSL socket protocol to use.
 
78
    */
 
79
   public static final String REMOTING_SSL_PROTOCOL = "org.jboss.remoting.sslProtocol";
 
80
 
 
81
   /**
 
82
    * If the protocol isn't specified, this will be the default.
 
83
    * Value is "TLS".
 
84
    */
 
85
   public static final String DEFAULT_SSL_PROTOCOL = "TLS";
 
86
 
 
87
   /**
 
88
    * Constant defining the config property used to define if the sockets will be in
 
89
    * client or server mode.
 
90
    */
 
91
   public static final String REMOTING_SOCKET_USE_CLIENT_MODE = "org.jboss.remoting.socket.useClientMode";
 
92
 
 
93
   /**
 
94
    * Constant defining the config property used to define if the server sockets will be in
 
95
    * client or server mode.
 
96
    */
 
97
   public static final String REMOTING_SERVER_SOCKET_USE_CLIENT_MODE = "org.jboss.remoting.serversocket.useClientMode";
 
98
 
 
99
   /**
 
100
    * Constant defining the config property used to define if sockets need or want
 
101
    * client authentication. This configuration option is only useful for sockets in the server mode.
 
102
    * The value of such a property is one of the CLIENT_AUTH_MODE_XXX constants.
 
103
    */
 
104
   public static final String REMOTING_CLIENT_AUTH_MODE = "org.jboss.remoting.clientAuthMode";
 
105
 
 
106
   /**
 
107
    * Client auth mode that indicates client authentication will not be peformed.
 
108
    */
 
109
   public static final String CLIENT_AUTH_MODE_NONE = "none";
 
110
 
 
111
   /**
 
112
    * Client auth mode that indicates that we want client authentication but it isn't required.
 
113
    */
 
114
   public static final String CLIENT_AUTH_MODE_WANT = "want";
 
115
 
 
116
   /**
 
117
    * Client auth mode that indicates that client authentication is required.
 
118
    */
 
119
   public static final String CLIENT_AUTH_MODE_NEED = "need";
 
120
 
 
121
   /**
 
122
    * Constant defining the config property used to define if a client should attempt to
 
123
    * authenticate a server certificate as one it trusts.  The value of such a property is
 
124
    * a boolean.
 
125
    */
 
126
   public static final String REMOTING_SERVER_AUTH_MODE = "org.jboss.remoting.serverAuthMode";
 
127
 
 
128
   /**
 
129
    * Constant defining the config property used to define where JBoss/Remoting will
 
130
    * look for the keystore file. This can be relative to the thread's
 
131
    * classloader or can be an absolute path on the file system or can be a URL.
 
132
    */
 
133
   public static final String REMOTING_KEY_STORE_FILE_PATH = "org.jboss.remoting.keyStore";
 
134
 
 
135
   /**
 
136
    * Constant defining the config property that defines the keystore's type.
 
137
    */
 
138
   public static final String REMOTING_KEY_STORE_TYPE = "org.jboss.remoting.keyStoreType";
 
139
 
 
140
   /**
 
141
    * Constant defining the config property that defines the key management algorithm
 
142
    * used by the keystore.
 
143
    */
 
144
   public static final String REMOTING_KEY_STORE_ALGORITHM = "org.jboss.remoting.keyStoreAlgorithm";
 
145
 
 
146
   /**
 
147
    * Constant defining the config property that defines the password of the keystore.
 
148
    */
 
149
   public static final String REMOTING_KEY_STORE_PASSWORD = "org.jboss.remoting.keyStorePassword";
 
150
 
 
151
   /**
 
152
    * Constant defining the config property that indicates the client's alias as found in the keystore.
 
153
    */
 
154
   public static final String REMOTING_KEY_ALIAS = "org.jboss.remoting.keyAlias";
 
155
 
 
156
   /**
 
157
    * Constant defining the config property that indicates the key password for the keys in the key store.
 
158
    */
 
159
   public static final String REMOTING_KEY_PASSWORD = "org.jboss.remoting.keyPassword";
 
160
 
 
161
   /**
 
162
    * Constant that defines the standard system property that the javax.net.ssl
 
163
    * classes look for when locating the keystore file.
 
164
    */
 
165
   public static final String STANDARD_KEY_STORE_FILE_PATH = "javax.net.ssl.keyStore";
 
166
 
 
167
   /**
 
168
    * Constant that defines the standard system property that the javax.net.ssl
 
169
    * classes look for when needing to know what type the keystore file is.
 
170
    */
 
171
   public static final String STANDARD_KEY_STORE_TYPE = "javax.net.ssl.keyStoreType";
 
172
 
 
173
   /**
 
174
    * Constant that defines the standard system property that the javax.net.ssl
 
175
    * classes look for when needing the keystore password.
 
176
    */
 
177
   public static final String STANDARD_KEY_STORE_PASSWORD = "javax.net.ssl.keyStorePassword";
 
178
 
 
179
   /**
 
180
    * Default key/trust store type if one not set as bean property, via config, or via system property.
 
181
    * Value is 'JKS'.
 
182
    */
 
183
   public static final String DEFAULT_KEY_STORE_TYPE = "JKS";
 
184
 
 
185
   /**
 
186
    * Default key/trust store algorithm if one net set as bean property or via config.
 
187
    * Value is 'SunX509'.
 
188
    */
 
189
   public static final String DEFAULT_KEY_STORE_ALGORITHM = "SunX509";
 
190
 
 
191
   /**
 
192
    * Constant defining the config property used to define where JBoss/Remoting
 
193
    * will look for the truststore file. This can be relative to the thread's
 
194
    * classloader or can be an absolute path on the file system.
 
195
    */
 
196
   public static final String REMOTING_TRUST_STORE_FILE_PATH = "org.jboss.remoting.trustStore";
 
197
 
 
198
   /**
 
199
    * Constant defining the config property that defines the truststore's type.
 
200
    */
 
201
   public static final String REMOTING_TRUST_STORE_TYPE = "org.jboss.remoting.trustStoreType";
 
202
 
 
203
   /**
 
204
    * Constant defining the config property that defines the key management
 
205
    * algorithm used by the truststore.
 
206
    */
 
207
   public static final String REMOTING_TRUST_STORE_ALGORITHM = "org.jboss.remoting.trustStoreAlgorithm";
 
208
 
 
209
   /**
 
210
    * Constant defining the config property that defines the password of the keystore.
 
211
    */
 
212
   public static final String REMOTING_TRUST_STORE_PASSWORD = "org.jboss.remoting.trustStorePassword";
 
213
 
 
214
   /**
 
215
    * Constant that defines the standard system property that the javax.net.ssl
 
216
    * classes look for when locating the truststore file.
 
217
    */
 
218
   public static final String STANDARD_TRUST_STORE_FILE_PATH = "javax.net.ssl.trustStore";
 
219
 
 
220
   /**
 
221
    * Constant that defines the standard system property that the javax.net.ssl
 
222
    * classes look for when needing to know what type the truststore file is.
 
223
    */
 
224
   public static final String STANDARD_TRUST_STORE_TYPE = "javax.net.ssl.trustStoreType";
 
225
 
 
226
   /**
 
227
    * Constant that defines the standard system property that the javax.net.ssl
 
228
    * classes look for when needing the truststore password.
 
229
    */
 
230
   public static final String STANDARD_TRUST_STORE_PASSWORD = "javax.net.ssl.trustStorePassword";
 
231
 
 
232
   /**
 
233
    * System property key to define the fully qualified class name of default socket factory to use
 
234
    * when not using custom config.
 
235
    */
 
236
   public static final String REMOTING_DEFAULT_SOCKET_FACTORY_CLASS = "org.jboss.remoting.defaultSocketFactory";
 
237
   
 
238
   public static final String NONE_STORE = "NONE";
 
239
 
 
240
   private SSLContext sslContextServerSocketFactory = null; // context that builds the server socket factories
 
241
   private SSLContext sslContextSocketFactory = null; // context that builds the socket factories
 
242
   private Provider provider = null;
 
243
   private String providerName = null;
 
244
   private String secureSocketProtocol = null;
 
245
 
 
246
   private KeyManager[] keyManagers = null;
 
247
   private TrustManager[] trustManagers = null;
 
248
   private SecureRandom secureRandom = null;
 
249
 
 
250
   private URL    keyStoreFilePath = null;
 
251
   private String keyStoreType = null;
 
252
   private String keyStoreAlgorithm = null;
 
253
   private String keyStorePassword = null;
 
254
   private String keyAlias = null;
 
255
   private String keyPassword = null;
 
256
 
 
257
   private URL    trustStoreFilePath = null;
 
258
   private String trustStoreType = null;
 
259
   private String trustStoreAlgorithm = null;
 
260
   private String trustStorePassword = null;
 
261
 
 
262
   private Map config = null;
 
263
   private Boolean socketUseClientMode = null;
 
264
   private Boolean serverSocketUseClientMode = null;
 
265
   private String clientAuthMode = null;
 
266
   private Boolean serverAuthMode = null;
 
267
 
 
268
   private boolean useSSLServerSocketFactory = true;
 
269
   private boolean useSSLSocketFactory = true;
 
270
 
 
271
   private static final Logger log = Logger.getLogger(SSLSocketBuilder.class);
 
272
   
 
273
   private static URL NONE_STORE_URL;
 
274
   
 
275
   static
 
276
   {
 
277
      try
 
278
      {
 
279
         NONE_STORE_URL = new URL("file:NONE");
 
280
      } catch (MalformedURLException e)
 
281
      {
 
282
         log.info("unexpected URL exception", e);
 
283
      }
 
284
   }
 
285
 
 
286
   /**
 
287
    * Constructor for {@link SSLSocketBuilder} that does not have
 
288
    * any configuration so it falls back to all defaults.
 
289
    */
 
290
   public SSLSocketBuilder()
 
291
   {
 
292
      this(null);
 
293
   }
 
294
 
 
295
   /**
 
296
    * Constructor for {@link SSLSocketBuilder} that allows the caller to
 
297
    * override the default settings for the various SSL configuration
 
298
    * properties.
 
299
    *
 
300
    * @param config configuration with properties defining things like where the
 
301
    *               keystore and truststore files are, their types, etc.
 
302
    */
 
303
   public SSLSocketBuilder(Map config)
 
304
   {
 
305
      this.config = config;
 
306
   }
 
307
 
 
308
   /**
 
309
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setUseSSLServerSocketFactory(boolean)
 
310
    */
 
311
   public void setUseSSLServerSocketFactory(boolean shouldUse)
 
312
   {
 
313
      this.useSSLServerSocketFactory = shouldUse;
 
314
   }
 
315
 
 
316
   /**
 
317
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getUseSSLServerSocketFactory()
 
318
    */
 
319
   public boolean getUseSSLServerSocketFactory()
 
320
   {
 
321
      return useSSLServerSocketFactory;
 
322
   }
 
323
 
 
324
   /**
 
325
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setUseSSLSocketFactory(boolean)
 
326
    */
 
327
   public void setUseSSLSocketFactory(boolean shouldUse)
 
328
   {
 
329
      this.useSSLSocketFactory = shouldUse;
 
330
   }
 
331
 
 
332
   /**
 
333
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getUseSSLSocketFactory()
 
334
    */
 
335
   public boolean getUseSSLSocketFactory()
 
336
   {
 
337
      return useSSLSocketFactory;
 
338
   }
 
339
 
 
340
   /**
 
341
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#createSSLServerSocketFactory()
 
342
    */
 
343
   public ServerSocketFactory createSSLServerSocketFactory() throws IOException
 
344
   {
 
345
      return createSSLServerSocketFactory( null );
 
346
   }
 
347
 
 
348
   /**
 
349
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#createSSLServerSocketFactory(org.jboss.remoting.security.CustomSSLServerSocketFactory)
 
350
    */
 
351
   public ServerSocketFactory createSSLServerSocketFactory(CustomSSLServerSocketFactory wrapper) throws IOException
 
352
   {
 
353
      ServerSocketFactory ssf = null;
 
354
 
 
355
      if( getUseSSLServerSocketFactory() )
 
356
      {
 
357
         ssf = SSLServerSocketFactory.getDefault();
 
358
      }
 
359
      else
 
360
      {
 
361
         if (wrapper == null)
 
362
         {
 
363
            wrapper = new CustomSSLServerSocketFactory(null, this);
 
364
         }
 
365
 
 
366
         ssf = createCustomServerSocketFactory(wrapper);
 
367
      }
 
368
 
 
369
      return ssf;
 
370
   }
 
371
 
 
372
   /**
 
373
    * This creates a fully custom SSL server socket factory using this object's configuration.
 
374
    *
 
375
    * @param wrapper the wrapper where the created factory will be stored
 
376
    *
 
377
    * @return the SSLServerSocketFactory
 
378
    *
 
379
    * @throws IOException
 
380
    */
 
381
   protected ServerSocketFactory createCustomServerSocketFactory(CustomSSLServerSocketFactory wrapper) throws IOException
 
382
   {
 
383
      if (sslContextServerSocketFactory == null)
 
384
      {
 
385
         createServerSocketFactorySSLContext();
 
386
         initializeServerSocketFactorySSLContext();
 
387
      }
 
388
 
 
389
      ServerSocketFactory ssf = sslContextServerSocketFactory.getServerSocketFactory();
 
390
 
 
391
      wrapper.setFactory((SSLServerSocketFactory) ssf);
 
392
 
 
393
      return wrapper;
 
394
   }
 
395
 
 
396
   /**
 
397
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#createSSLSocketFactory()
 
398
    */
 
399
   public SocketFactory createSSLSocketFactory() throws IOException
 
400
   {
 
401
      return createSSLSocketFactory(null);
 
402
   }
 
403
 
 
404
 
 
405
   /**
 
406
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#createSSLSocketFactory(org.jboss.remoting.security.CustomSSLSocketFactory)
 
407
    */
 
408
   public SocketFactory createSSLSocketFactory(CustomSSLSocketFactory wrapper) throws IOException
 
409
   {
 
410
      SocketFactory sf = null;
 
411
 
 
412
      if (getUseSSLSocketFactory())
 
413
      {
 
414
         String defaultFactoryName = getSystemProperty(REMOTING_DEFAULT_SOCKET_FACTORY_CLASS);
 
415
         
 
416
         if (defaultFactoryName != null)
 
417
         {
 
418
            try
 
419
            {
 
420
               final Class sfClass = ClassLoaderUtility.loadClass(defaultFactoryName, SSLSocketBuilder.class);
 
421
               Method m = getMethod(sfClass, "getDefault", null);
 
422
               
 
423
               if (m == null)
 
424
               {
 
425
                  throw new RuntimeException(
 
426
                        "Could not create the socket factory "
 
427
                        + defaultFactoryName
 
428
                        + " because the class "
 
429
                        + sfClass
 
430
                        + " doesn't provide the getDefault method.");
 
431
               }
 
432
               sf = (SocketFactory) m.invoke(null, null);
 
433
            }
 
434
            catch (Exception ex)
 
435
            {
 
436
               throw new RuntimeException(
 
437
                     "Could not create the socket factory "
 
438
                     + defaultFactoryName, ex);
 
439
            }
 
440
         }
 
441
         if (sf == null)
 
442
         {
 
443
            sf = SSLSocketFactory.getDefault();
 
444
         }
 
445
      }
 
446
      else
 
447
      {
 
448
         if (wrapper == null)
 
449
         {
 
450
            wrapper = new CustomSSLSocketFactory(null, this);
 
451
         }
 
452
 
 
453
         sf = createCustomSocketFactory(wrapper);
 
454
      }
 
455
 
 
456
      return sf;
 
457
   }
 
458
 
 
459
   /**
 
460
    * This creates a fully custom SSL socket factory using this object's configuration.
 
461
    *
 
462
    * @param wrapper the wrapper where the created factory will be stored
 
463
    *
 
464
    * @return the wrapper with the new factory stored in it
 
465
    *
 
466
    * @throws IOException
 
467
    */
 
468
   protected SocketFactory createCustomSocketFactory(CustomSSLSocketFactory wrapper) throws IOException
 
469
   {
 
470
      if (sslContextSocketFactory == null)
 
471
      {
 
472
         createSocketFactorySSLContext();
 
473
         initializeSocketFactorySSLContext();
 
474
      }
 
475
 
 
476
      SocketFactory sf = sslContextSocketFactory.getSocketFactory();
 
477
 
 
478
      wrapper.setFactory((SSLSocketFactory) sf);
 
479
 
 
480
      return wrapper;
 
481
   }
 
482
 
 
483
 
 
484
   /**
 
485
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getServerSocketFactorySSLContext()
 
486
    */
 
487
   public SSLContext getServerSocketFactorySSLContext()
 
488
   {
 
489
      return sslContextServerSocketFactory;
 
490
   }
 
491
 
 
492
   /**
 
493
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getSocketFactorySSLContext()
 
494
    */
 
495
   public SSLContext getSocketFactorySSLContext()
 
496
   {
 
497
      return sslContextSocketFactory;
 
498
   }
 
499
 
 
500
   /**
 
501
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getSecureSocketProtocol()
 
502
    */
 
503
   public String getSecureSocketProtocol()
 
504
   {
 
505
      if (secureSocketProtocol == null)
 
506
      {
 
507
         if(config != null)
 
508
         {
 
509
            secureSocketProtocol = (String) config.get(REMOTING_SSL_PROTOCOL);
 
510
         }
 
511
         if (secureSocketProtocol == null)
 
512
         {
 
513
            secureSocketProtocol = DEFAULT_SSL_PROTOCOL;
 
514
         }
 
515
      }
 
516
 
 
517
      return secureSocketProtocol;
 
518
   }
 
519
 
 
520
   /**
 
521
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setSecureSocketProtocol(String)
 
522
    */
 
523
   public void setSecureSocketProtocol(String protocol)
 
524
   {
 
525
      if(protocol != null && protocol.length() > 0)
 
526
      {
 
527
         this.secureSocketProtocol = protocol;
 
528
      }
 
529
      else
 
530
      {
 
531
         throw new IllegalArgumentException("Can not set remoting socket factory with null protocol");
 
532
      }
 
533
   }
 
534
 
 
535
   /**
 
536
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getProvider()
 
537
    */
 
538
   public Provider getProvider()
 
539
   {
 
540
      return provider;
 
541
   }
 
542
 
 
543
   /**
 
544
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setProvider(java.security.Provider)
 
545
    */
 
546
   public void setProvider(Provider provider)
 
547
   {
 
548
      this.provider = provider;
 
549
   }
 
550
 
 
551
   /**
 
552
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getProviderName()
 
553
    */
 
554
   public String getProviderName()
 
555
   {
 
556
      if (providerName == null)
 
557
      {
 
558
         if(config != null)
 
559
         {
 
560
            providerName = (String) config.get(REMOTING_SSL_PROVIDER_NAME);
 
561
         }
 
562
      }
 
563
      return providerName;
 
564
   }
 
565
 
 
566
   /**
 
567
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setProviderName(java.lang.String)
 
568
    */
 
569
   public void setProviderName(String providerName)
 
570
   {
 
571
      this.providerName = providerName;
 
572
   }
 
573
 
 
574
   /**
 
575
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getSecureRandom()
 
576
    */
 
577
   public SecureRandom getSecureRandom()
 
578
   {
 
579
      if(secureRandom != null)
 
580
      {
 
581
         return secureRandom;
 
582
      }
 
583
 
 
584
      secureRandom = new SecureRandom();
 
585
 
 
586
      return secureRandom;
 
587
   }
 
588
 
 
589
   /**
 
590
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setSecureRandom(java.security.SecureRandom)
 
591
    */
 
592
   public void setSecureRandom(SecureRandom secureRandom)
 
593
   {
 
594
      this.secureRandom = secureRandom;
 
595
   }
 
596
 
 
597
   /**
 
598
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getKeyStoreURL()
 
599
    */
 
600
   public String getKeyStoreURL()
 
601
   {
 
602
      URL keyStore = getKeyStore();
 
603
      if(keyStore != null)
 
604
      {
 
605
         return keyStore.toString();
 
606
      }
 
607
      else
 
608
      {
 
609
         return null;
 
610
      }
 
611
   }
 
612
 
 
613
   /**
 
614
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getKeyStore()
 
615
    */
 
616
   public URL getKeyStore()
 
617
   {
 
618
      if(keyStoreFilePath != null)
 
619
      {
 
620
         return keyStoreFilePath;
 
621
      }
 
622
 
 
623
      if(config != null)
 
624
      {
 
625
         String path = (String) config.get(REMOTING_KEY_STORE_FILE_PATH);
 
626
         if(path != null && path.length() > 0)
 
627
         {
 
628
            setKeyStoreURL( path );
 
629
         }
 
630
      }
 
631
 
 
632
      if(keyStoreFilePath == null)
 
633
      {
 
634
         String path = getSystemProperty(STANDARD_KEY_STORE_FILE_PATH);;
 
635
         if(path != null && path.length() > 0)
 
636
         {
 
637
            setKeyStoreURL( path );
 
638
         }
 
639
      }
 
640
 
 
641
      return keyStoreFilePath;
 
642
   }
 
643
 
 
644
   /**
 
645
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setKeyStoreURL(java.lang.String)
 
646
    */
 
647
   public void setKeyStoreURL(String keyStoreFilePath)
 
648
   {
 
649
      try
 
650
      {
 
651
         this.keyStoreFilePath = validateStoreURL(keyStoreFilePath);
 
652
      }
 
653
      catch (IOException e)
 
654
      {
 
655
         throw new RuntimeException( "Cannot validate the store URL: " + keyStoreFilePath , e );
 
656
      }
 
657
   }
 
658
 
 
659
   /**
 
660
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setKeyStore(java.net.URL)
 
661
    */
 
662
   public void setKeyStore(URL keyStore)
 
663
   {
 
664
      this.keyStoreFilePath = keyStore;
 
665
   }
 
666
 
 
667
   /**
 
668
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getKeyStoreType()
 
669
    */
 
670
   public String getKeyStoreType()
 
671
   {
 
672
      if(keyStoreType != null)
 
673
      {
 
674
         return keyStoreType;
 
675
      }
 
676
 
 
677
      if(config != null)
 
678
      {
 
679
         String type = (String)config.get(REMOTING_KEY_STORE_TYPE);
 
680
         if(type != null && type.length() > 0)
 
681
         {
 
682
            keyStoreType = type;
 
683
         }
 
684
      }
 
685
 
 
686
      if(keyStoreType == null)
 
687
      {
 
688
         keyStoreType = getSystemProperty(STANDARD_KEY_STORE_TYPE);
 
689
         if(keyStoreType == null)
 
690
         {
 
691
            keyStoreType = DEFAULT_KEY_STORE_TYPE;
 
692
         }
 
693
      }
 
694
 
 
695
      return keyStoreType;
 
696
   }
 
697
 
 
698
   /**
 
699
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setKeyStoreType(java.lang.String)
 
700
    */
 
701
   public void setKeyStoreType(String keyStoreType)
 
702
   {
 
703
      this.keyStoreType = keyStoreType;
 
704
   }
 
705
 
 
706
   /**
 
707
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getKeyStoreAlgorithm()
 
708
    */
 
709
   public String getKeyStoreAlgorithm()
 
710
   {
 
711
      if(keyStoreAlgorithm != null)
 
712
      {
 
713
         return keyStoreAlgorithm;
 
714
      }
 
715
 
 
716
      if(config != null)
 
717
      {
 
718
         String alg = (String)config.get(REMOTING_KEY_STORE_ALGORITHM);
 
719
         if(alg != null && alg.length() > 0)
 
720
         {
 
721
            keyStoreAlgorithm = alg;
 
722
         }
 
723
      }
 
724
 
 
725
      if(keyStoreAlgorithm == null)
 
726
      {
 
727
         keyStoreAlgorithm = DEFAULT_KEY_STORE_ALGORITHM;
 
728
      }
 
729
 
 
730
      return keyStoreAlgorithm;
 
731
   }
 
732
 
 
733
   /**
 
734
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setKeyStoreAlgorithm(java.lang.String)
 
735
    */
 
736
   public void setKeyStoreAlgorithm(String algorithm)
 
737
   {
 
738
      this.keyStoreAlgorithm = algorithm;
 
739
   }
 
740
 
 
741
   /**
 
742
    * Returns the password used to gain access to the keystore.
 
743
    *
 
744
    * @return keystore password
 
745
    */
 
746
   public String getKeyStorePassword()
 
747
   {
 
748
      if(keyStorePassword != null)
 
749
      {
 
750
         return keyStorePassword;
 
751
      }
 
752
 
 
753
      if(config != null)
 
754
      {
 
755
         String passwd = (String)config.get(REMOTING_KEY_STORE_PASSWORD);
 
756
         if(passwd != null && passwd.length() > 0)
 
757
         {
 
758
            keyStorePassword = passwd;
 
759
         }
 
760
      }
 
761
 
 
762
      if(keyStorePassword == null)
 
763
      {
 
764
         keyStorePassword = getSystemProperty(STANDARD_KEY_STORE_PASSWORD);
 
765
      }
 
766
 
 
767
      return keyStorePassword;
 
768
   }
 
769
 
 
770
   /**
 
771
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setKeyStorePassword(java.lang.String)
 
772
    */
 
773
   public void setKeyStorePassword(String keyStorePassword)
 
774
   {
 
775
      this.keyStorePassword = keyStorePassword;
 
776
   }
 
777
 
 
778
   /**
 
779
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getTrustStoreURL()
 
780
    */
 
781
   public String getTrustStoreURL()
 
782
   {
 
783
      URL trustStore = getTrustStore();
 
784
      if(trustStore != null)
 
785
      {
 
786
         return trustStore.toString();
 
787
      }
 
788
      else
 
789
      {
 
790
         return null;
 
791
      }
 
792
   }
 
793
 
 
794
   /**
 
795
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getTrustStore()
 
796
    */
 
797
   public URL getTrustStore()
 
798
   {
 
799
      if(trustStoreFilePath != null)
 
800
      {
 
801
         return trustStoreFilePath;
 
802
      }
 
803
 
 
804
      if(config != null)
 
805
      {
 
806
         String path = (String)config.get(REMOTING_TRUST_STORE_FILE_PATH);
 
807
         if(path != null && path.length() > 0)
 
808
         {
 
809
            setTrustStoreURL( path );
 
810
         }
 
811
      }
 
812
 
 
813
      if(trustStoreFilePath == null)
 
814
      {
 
815
         String path = getSystemProperty(STANDARD_TRUST_STORE_FILE_PATH);
 
816
         if(path != null && path.length() > 0)
 
817
         {
 
818
            setTrustStoreURL( path );
 
819
         }
 
820
      }
 
821
 
 
822
      return trustStoreFilePath;
 
823
   }
 
824
 
 
825
   /**
 
826
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setTrustStore(java.net.URL)
 
827
    */
 
828
   public void setTrustStore(URL trustStore)
 
829
   {
 
830
      this.trustStoreFilePath = trustStore;
 
831
   }
 
832
 
 
833
   /**
 
834
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setTrustStoreURL(java.lang.String)
 
835
    */
 
836
   public void setTrustStoreURL(String trustStoreFilePath)
 
837
   {
 
838
      try
 
839
      {
 
840
         this.trustStoreFilePath = validateStoreURL(trustStoreFilePath);
 
841
      }
 
842
      catch (IOException e)
 
843
      {
 
844
         throw new RuntimeException( "Cannot validate the store URL: " + trustStoreFilePath , e );
 
845
      }
 
846
   }
 
847
 
 
848
   /**
 
849
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getTrustStoreType()
 
850
    */
 
851
   public String getTrustStoreType()
 
852
   {
 
853
      if(trustStoreType != null)
 
854
      {
 
855
         return trustStoreType;
 
856
      }
 
857
 
 
858
      if(config != null)
 
859
      {
 
860
         String type = (String)config.get(REMOTING_TRUST_STORE_TYPE);
 
861
         if(type != null && type.length() > 0)
 
862
         {
 
863
            trustStoreType = type;
 
864
         }
 
865
      }
 
866
 
 
867
      if(trustStoreType == null)
 
868
      {
 
869
         trustStoreType = getSystemProperty(STANDARD_TRUST_STORE_TYPE);
 
870
         if(trustStoreType == null)
 
871
         {
 
872
            trustStoreType = getKeyStoreType();
 
873
         }
 
874
      }
 
875
 
 
876
      return trustStoreType;
 
877
   }
 
878
 
 
879
   /**
 
880
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setTrustStoreType(java.lang.String)
 
881
    */
 
882
   public void setTrustStoreType(String trustStoreType)
 
883
   {
 
884
      this.trustStoreType = trustStoreType;
 
885
   }
 
886
 
 
887
   /**
 
888
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getTrustStoreAlgorithm()
 
889
    */
 
890
   public String getTrustStoreAlgorithm()
 
891
   {
 
892
      if(trustStoreAlgorithm != null)
 
893
      {
 
894
         return trustStoreAlgorithm;
 
895
      }
 
896
 
 
897
      if(config != null)
 
898
      {
 
899
         String alg = (String)config.get(REMOTING_TRUST_STORE_ALGORITHM);
 
900
         if(alg != null && alg.length() > 0)
 
901
         {
 
902
            trustStoreAlgorithm = alg;
 
903
         }
 
904
      }
 
905
 
 
906
      if(trustStoreAlgorithm == null)
 
907
      {
 
908
         trustStoreAlgorithm = getKeyStoreAlgorithm();
 
909
      }
 
910
 
 
911
      return trustStoreAlgorithm;
 
912
   }
 
913
 
 
914
   /**
 
915
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setTrustStoreAlgorithm(java.lang.String)
 
916
    */
 
917
   public void setTrustStoreAlgorithm(String algorithm)
 
918
   {
 
919
      this.trustStoreAlgorithm = algorithm;
 
920
   }
 
921
 
 
922
   /**
 
923
    * Returns the password used to gain access to the truststore.
 
924
    *
 
925
    * @return truststore password
 
926
    */
 
927
   public String getTrustStorePassword()
 
928
   {
 
929
      if(trustStorePassword != null)
 
930
      {
 
931
         return trustStorePassword;
 
932
      }
 
933
 
 
934
      if(config != null)
 
935
      {
 
936
         String passwd = (String)config.get(REMOTING_TRUST_STORE_PASSWORD);
 
937
         if(passwd != null && passwd.length() > 0)
 
938
         {
 
939
            trustStorePassword = passwd;
 
940
         }
 
941
      }
 
942
 
 
943
      if(trustStorePassword == null)
 
944
      {
 
945
         trustStorePassword = getSystemProperty(STANDARD_TRUST_STORE_PASSWORD);
 
946
         if(trustStorePassword == null)
 
947
         {
 
948
            trustStorePassword = getKeyStorePassword();
 
949
         }
 
950
      }
 
951
 
 
952
      return trustStorePassword;
 
953
   }
 
954
 
 
955
   /**
 
956
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setTrustStorePassword(java.lang.String)
 
957
    */
 
958
   public void setTrustStorePassword(String trustStorePassword)
 
959
   {
 
960
      this.trustStorePassword = trustStorePassword;
 
961
   }
 
962
 
 
963
   /**
 
964
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getKeyAlias()
 
965
    */
 
966
   public String getKeyAlias()
 
967
   {
 
968
      if(keyAlias != null)
 
969
      {
 
970
         return keyAlias;
 
971
      }
 
972
      if(config != null)
 
973
      {
 
974
         keyAlias = (String)config.get(REMOTING_KEY_ALIAS);
 
975
      }
 
976
      return keyAlias;
 
977
   }
 
978
 
 
979
   /**
 
980
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setKeyAlias(java.lang.String)
 
981
    */
 
982
   public void setKeyAlias(String alias)
 
983
   {
 
984
      this.keyAlias = alias;
 
985
   }
 
986
 
 
987
   /**
 
988
    * Returns the password to use for the keys within the key store.
 
989
    * If this value is not set, this will return <code>null</code> but
 
990
    * when this value is needed by this class, the value for the key store
 
991
    * password will be used instead.
 
992
    *
 
993
    * @return key password
 
994
    */
 
995
   public String getKeyPassword()
 
996
   {
 
997
      if(keyPassword != null)
 
998
      {
 
999
         return keyPassword;
 
1000
      }
 
1001
 
 
1002
      if(config != null)
 
1003
      {
 
1004
         String passwd = (String)config.get(REMOTING_KEY_PASSWORD);
 
1005
         if(passwd != null && passwd.length() > 0)
 
1006
         {
 
1007
            keyPassword = passwd;
 
1008
         }
 
1009
      }
 
1010
 
 
1011
      return keyPassword;
 
1012
   }
 
1013
 
 
1014
   /**
 
1015
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setKeyPassword(java.lang.String)
 
1016
    */
 
1017
   public void setKeyPassword(String keyPassword)
 
1018
   {
 
1019
      this.keyPassword = keyPassword;
 
1020
   }
 
1021
 
 
1022
   /**
 
1023
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#isSocketUseClientMode()
 
1024
    */
 
1025
   public boolean isSocketUseClientMode( )
 
1026
   {
 
1027
      if (socketUseClientMode == null)
 
1028
      {
 
1029
         if (config != null && config.containsKey(REMOTING_SOCKET_USE_CLIENT_MODE))
 
1030
         {
 
1031
            socketUseClientMode = Boolean.valueOf((String) config.get(REMOTING_SOCKET_USE_CLIENT_MODE));
 
1032
         }
 
1033
         else
 
1034
         {
 
1035
            socketUseClientMode = Boolean.TRUE;
 
1036
         }
 
1037
      }
 
1038
 
 
1039
      return socketUseClientMode.booleanValue();
 
1040
   }
 
1041
 
 
1042
   /**
 
1043
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#isServerSocketUseClientMode()
 
1044
    */
 
1045
   public boolean isServerSocketUseClientMode( )
 
1046
   {
 
1047
      if (serverSocketUseClientMode == null)
 
1048
      {
 
1049
         if (config != null && config.containsKey(REMOTING_SERVER_SOCKET_USE_CLIENT_MODE))
 
1050
         {
 
1051
            serverSocketUseClientMode = Boolean.valueOf((String) config.get(REMOTING_SERVER_SOCKET_USE_CLIENT_MODE));
 
1052
         }
 
1053
         else
 
1054
         {
 
1055
            serverSocketUseClientMode = Boolean.FALSE;
 
1056
         }
 
1057
      }
 
1058
 
 
1059
      return serverSocketUseClientMode.booleanValue();
 
1060
   }
 
1061
 
 
1062
   /**
 
1063
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setSocketUseClientMode(boolean)
 
1064
    */
 
1065
   public void setSocketUseClientMode( boolean useClientMode )
 
1066
   {
 
1067
      this.socketUseClientMode = Boolean.valueOf(useClientMode);
 
1068
   }
 
1069
 
 
1070
   /**
 
1071
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setServerSocketUseClientMode(boolean)
 
1072
    */
 
1073
   public void setServerSocketUseClientMode( boolean useClientMode )
 
1074
   {
 
1075
      this.serverSocketUseClientMode = Boolean.valueOf(useClientMode);
 
1076
   }
 
1077
 
 
1078
   /**
 
1079
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#isClientAuthModeNone()
 
1080
    */
 
1081
   public boolean isClientAuthModeNone()
 
1082
   {
 
1083
      return CLIENT_AUTH_MODE_NONE.equals(getClientAuthMode());
 
1084
   }
 
1085
 
 
1086
   /**
 
1087
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#isClientAuthModeWant()
 
1088
    */
 
1089
   public boolean isClientAuthModeWant()
 
1090
   {
 
1091
      return CLIENT_AUTH_MODE_WANT.equals(getClientAuthMode());
 
1092
   }
 
1093
 
 
1094
   /**
 
1095
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#isClientAuthModeNeed()
 
1096
    */
 
1097
   public boolean isClientAuthModeNeed()
 
1098
   {
 
1099
      return CLIENT_AUTH_MODE_NEED.equals(getClientAuthMode());
 
1100
   }
 
1101
 
 
1102
   /**
 
1103
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#getClientAuthMode()
 
1104
    */
 
1105
   public String getClientAuthMode()
 
1106
   {
 
1107
      if (clientAuthMode == null)
 
1108
      {
 
1109
         if (config != null && config.containsKey(REMOTING_CLIENT_AUTH_MODE))
 
1110
         {
 
1111
            setClientAuthMode( (String) config.get(REMOTING_CLIENT_AUTH_MODE) );
 
1112
         }
 
1113
         else
 
1114
         {
 
1115
            clientAuthMode = CLIENT_AUTH_MODE_NONE;
 
1116
         }
 
1117
      }
 
1118
 
 
1119
      return clientAuthMode;
 
1120
   }
 
1121
 
 
1122
   /**
 
1123
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setClientAuthMode(java.lang.String)
 
1124
    */
 
1125
   public void setClientAuthMode(String mode)
 
1126
   {
 
1127
      if (  mode == null ||
 
1128
            (!mode.equalsIgnoreCase(CLIENT_AUTH_MODE_NONE)
 
1129
             && !mode.equalsIgnoreCase(CLIENT_AUTH_MODE_WANT)
 
1130
             && !mode.equalsIgnoreCase(CLIENT_AUTH_MODE_NEED)))
 
1131
      {
 
1132
         log.warn("Client authentication mode is invalid [" + mode + "]; falling back to NEED mode");
 
1133
         clientAuthMode = CLIENT_AUTH_MODE_NEED;
 
1134
      }
 
1135
      else
 
1136
      {
 
1137
         clientAuthMode = mode;
 
1138
      }
 
1139
 
 
1140
      return;
 
1141
   }
 
1142
 
 
1143
   /**
 
1144
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#isServerAuthMode()
 
1145
    */
 
1146
   public boolean isServerAuthMode()
 
1147
   {
 
1148
      if (serverAuthMode == null)
 
1149
      {
 
1150
         if (config != null && config.containsKey(REMOTING_SERVER_AUTH_MODE))
 
1151
         {
 
1152
            serverAuthMode = Boolean.valueOf( (String) config.get(REMOTING_SERVER_AUTH_MODE) );
 
1153
         }
 
1154
         else
 
1155
         {
 
1156
            serverAuthMode = Boolean.TRUE;
 
1157
         }
 
1158
      }
 
1159
 
 
1160
      return serverAuthMode.booleanValue();
 
1161
   }
 
1162
 
 
1163
   /**
 
1164
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#setServerAuthMode(boolean)
 
1165
    */
 
1166
   public void setServerAuthMode(boolean mode)
 
1167
   {
 
1168
      serverAuthMode = Boolean.valueOf(mode);
 
1169
   }
 
1170
 
 
1171
   /**
 
1172
    * Creates (but does not initialize) the SSL context used by this object
 
1173
    * to create server socket factories.
 
1174
    * The provider/protocol is used to determine what SSL context to use.
 
1175
    * Call {@link #initializeServerSocketFactorySSLContext()} if you want
 
1176
    * to create and initialize in one method call.
 
1177
    * If the server socket factory SSL context was already created, this will create
 
1178
    * a new one and remove the old one.
 
1179
    *
 
1180
    * @throws IOException
 
1181
    */
 
1182
   protected void createServerSocketFactorySSLContext()
 
1183
         throws IOException
 
1184
   {
 
1185
      try
 
1186
      {
 
1187
         if(getProvider() != null)
 
1188
         {
 
1189
            sslContextServerSocketFactory = SSLContext.getInstance(getSecureSocketProtocol(), getProvider());
 
1190
         }
 
1191
         else if(getProviderName() != null)
 
1192
         {
 
1193
            sslContextServerSocketFactory = SSLContext.getInstance(getSecureSocketProtocol(), getProviderName());
 
1194
         }
 
1195
         else
 
1196
         {
 
1197
            sslContextServerSocketFactory = SSLContext.getInstance(getSecureSocketProtocol());
 
1198
         }
 
1199
      }
 
1200
      catch(Exception e)
 
1201
      {
 
1202
         IOException ioe = new IOException("Error creating server socket factory SSL context: " + e.getMessage());
 
1203
         ioe.setStackTrace(e.getStackTrace());
 
1204
         throw ioe;
 
1205
      }
 
1206
 
 
1207
      return;
 
1208
   }
 
1209
 
 
1210
   /**
 
1211
    * Creates (but does not initialize) the SSL context used by this object
 
1212
    * to create socket factories.
 
1213
    * The provider/protocol is used to determine what SSL context to use.
 
1214
    * Call {@link #initializeSocketFactorySSLContext()} if you want
 
1215
    * to create and initialize in one method call.
 
1216
    * If the socket factory SSL context was already created, this will create
 
1217
    * a new one and remove the old one.
 
1218
    *
 
1219
    * @throws IOException
 
1220
    */
 
1221
   protected void createSocketFactorySSLContext()
 
1222
   throws IOException
 
1223
   {
 
1224
      try
 
1225
      {
 
1226
         if(getProvider() != null)
 
1227
         {
 
1228
            sslContextSocketFactory = SSLContext.getInstance(getSecureSocketProtocol(), getProvider());
 
1229
         }
 
1230
         else if(getProviderName() != null)
 
1231
         {
 
1232
            sslContextSocketFactory = SSLContext.getInstance(getSecureSocketProtocol(), getProviderName());
 
1233
         }
 
1234
         else
 
1235
         {
 
1236
            sslContextSocketFactory = SSLContext.getInstance(getSecureSocketProtocol());
 
1237
         }
 
1238
      }
 
1239
      catch(Exception e)
 
1240
      {
 
1241
         IOException ioe = new IOException("Error creating socket factory SSL context: " + e.getMessage());
 
1242
         ioe.setStackTrace(e.getStackTrace());
 
1243
         throw ioe;
 
1244
      }
 
1245
 
 
1246
      return;
 
1247
   }
 
1248
 
 
1249
   /**
 
1250
    * Initializes the SSL context used by this object that will create the server socket factories.
 
1251
    * If the SSL context is not yet created, this method will also create it.
 
1252
    * The provider/protocol is used to determine what SSL context to use.  Key and trust managers
 
1253
    * are loaded and a secure random object is created and the SSL context for the
 
1254
    * protocol/provider is initialized with them.
 
1255
    *
 
1256
    * @throws IOException
 
1257
    */
 
1258
   protected void initializeServerSocketFactorySSLContext()
 
1259
         throws IOException
 
1260
   {
 
1261
      try
 
1262
      {
 
1263
         if (sslContextServerSocketFactory == null)
 
1264
         {
 
1265
            createServerSocketFactorySSLContext();
 
1266
         }
 
1267
 
 
1268
         try
 
1269
         {
 
1270
            keyManagers = loadKeyManagers();
 
1271
         }
 
1272
         catch (NullStoreURLException e)
 
1273
         {
 
1274
            if (isServerSocketUseClientMode())
 
1275
            {
 
1276
               keyManagers = null;
 
1277
               log.debug("Could not find keytore url.  " + e.getMessage());
 
1278
            }
 
1279
            else
 
1280
            {
 
1281
               // because this ssl context will create server socket factories, will throw if can not find keystore
 
1282
               IOException ioe = new IOException("Can not find keystore url.");
 
1283
               ioe.initCause(e);
 
1284
               throw ioe;
 
1285
            }
 
1286
         }
 
1287
 
 
1288
         try
 
1289
         {
 
1290
            boolean isClientMode = isServerSocketUseClientMode();
 
1291
            trustManagers = loadTrustManagers(isClientMode);
 
1292
         }
 
1293
         catch (NullStoreURLException e)
 
1294
         {
 
1295
            trustManagers = null;
 
1296
            log.debug("Could not find truststore url.  " + e.getMessage());
 
1297
         }
 
1298
 
 
1299
         secureRandom = getSecureRandom();
 
1300
 
 
1301
         sslContextServerSocketFactory.init(keyManagers, trustManagers, secureRandom);
 
1302
      }
 
1303
      catch(Exception e)
 
1304
      {
 
1305
         IOException ioe = new IOException("Error initializing server socket factory SSL context: " + e.getMessage());
 
1306
         ioe.setStackTrace(e.getStackTrace());
 
1307
         throw ioe;
 
1308
      }
 
1309
 
 
1310
      return;
 
1311
   }
 
1312
 
 
1313
   /**
 
1314
    * Initializes the SSL context used by this object that will create the socket factories.
 
1315
    * If the SSL context is not yet created, this method will also create it.
 
1316
    * The provider/protocol is used to determine what SSL context to use.  Key and trust managers
 
1317
    * are loaded and a secure random object is created and the SSL context for the
 
1318
    * protocol/provider is initialized with them.
 
1319
    *
 
1320
    * @throws IOException
 
1321
    */
 
1322
   protected void initializeSocketFactorySSLContext()
 
1323
   throws IOException
 
1324
   {
 
1325
      try
 
1326
      {
 
1327
         if (sslContextSocketFactory == null)
 
1328
         {
 
1329
            createSocketFactorySSLContext();
 
1330
         }
 
1331
 
 
1332
         try
 
1333
         {
 
1334
            keyManagers = loadKeyManagers();
 
1335
         }
 
1336
         catch (NullStoreURLException e)
 
1337
         {
 
1338
            if (isSocketUseClientMode())
 
1339
            {
 
1340
               // this is allowable since would be the normal scenario
 
1341
               keyManagers = null;
 
1342
               log.debug("Could not find keystore url.  " + e.getMessage());
 
1343
            }
 
1344
            else
 
1345
            {
 
1346
               IOException ioe = new IOException("Can not find keystore url.");
 
1347
               ioe.initCause(e);
 
1348
               throw ioe;
 
1349
            }
 
1350
         }
 
1351
 
 
1352
         try
 
1353
         {
 
1354
            boolean isClientMode = isSocketUseClientMode();
 
1355
            trustManagers = loadTrustManagers(isClientMode);
 
1356
         }
 
1357
         catch (NullStoreURLException e)
 
1358
         {
 
1359
            // If the keyManagers is not null, could possibly be using in client mode
 
1360
            // so want to allow it.  Otherwise, need to throw exception as will not be able
 
1361
            // to use in client mode or not
 
1362
            if(keyManagers != null)
 
1363
            {
 
1364
               trustManagers = null;
 
1365
               log.debug("Could not find truststore url.  " + e.getMessage());
 
1366
            }
 
1367
            else
 
1368
            {
 
1369
               IOException ioe = new IOException("Can not find truststore url.");
 
1370
               ioe.initCause(e);
 
1371
               throw ioe;
 
1372
            }
 
1373
         }
 
1374
 
 
1375
         secureRandom = getSecureRandom();
 
1376
 
 
1377
         sslContextSocketFactory.init(keyManagers, trustManagers, secureRandom);
 
1378
      }
 
1379
      catch(Exception e)
 
1380
      {
 
1381
         IOException ioe = new IOException("Error initializing socket factory SSL context: " + e.getMessage());
 
1382
         ioe.setStackTrace(e.getStackTrace());
 
1383
         throw ioe;
 
1384
      }
 
1385
 
 
1386
      return;
 
1387
   }
 
1388
 
 
1389
   /**
 
1390
    * Loads the trust managers based on this object's truststore.
 
1391
    *
 
1392
    * @return array of trust managers that should be loaded in this object's SSL context
 
1393
    *
 
1394
    * @throws NoSuchProviderException
 
1395
    * @throws NoSuchAlgorithmException
 
1396
    * @throws IOException
 
1397
    * @throws CertificateException
 
1398
    * @throws KeyStoreException
 
1399
    * @throws NullStoreURLException
 
1400
    */
 
1401
   protected TrustManager[] loadTrustManagers(boolean isClientMode)
 
1402
         throws NoSuchProviderException, NoSuchAlgorithmException, IOException, CertificateException, KeyStoreException, NullStoreURLException
 
1403
   {
 
1404
      if(isClientMode && !isServerAuthMode())
 
1405
      {
 
1406
         // we are in client mode and do not want to perform server cert authentication
 
1407
         // return a trust manager that trusts all certs
 
1408
         trustManagers = new TrustManager[] {
 
1409
               new X509TrustManager() {
 
1410
                  public void checkClientTrusted( X509Certificate[] chain, String authType ) {}
 
1411
                  public void checkServerTrusted( X509Certificate[] chain, String authType ) {}
 
1412
                  public X509Certificate[] getAcceptedIssuers()  { return null; }
 
1413
               }};
 
1414
      }
 
1415
      else
 
1416
      {
 
1417
         String tsType = getTrustStoreType();
 
1418
         String tsPasswd = getTrustStorePassword();
 
1419
         URL tsPathURL = getTrustStore();
 
1420
 
 
1421
         String tsAlg = getTrustStoreAlgorithm();
 
1422
 
 
1423
         TrustManagerFactory trustMgrFactory;
 
1424
         KeyStore trustStore = loadKeyStore(tsType, tsPathURL, tsPasswd);
 
1425
 
 
1426
         if (getProvider() != null)
 
1427
         {
 
1428
            trustMgrFactory = TrustManagerFactory.getInstance(tsAlg, getProvider());
 
1429
         }
 
1430
         else if (getProviderName() != null)
 
1431
         {
 
1432
            trustMgrFactory = TrustManagerFactory.getInstance(tsAlg, getProviderName());
 
1433
         }
 
1434
         else
 
1435
         {
 
1436
            trustMgrFactory = TrustManagerFactory.getInstance(tsAlg);
 
1437
         }
 
1438
 
 
1439
         if (trustStore != null)
 
1440
         {
 
1441
            trustMgrFactory.init(trustStore);
 
1442
 
 
1443
            trustManagers = trustMgrFactory.getTrustManagers();
 
1444
         }
 
1445
      }
 
1446
 
 
1447
      return trustManagers;
 
1448
   }
 
1449
 
 
1450
   /**
 
1451
    * Loads the key managers based on this object's truststore.
 
1452
    *
 
1453
    * @return array of key managers that should be loaded in this object's SSL context
 
1454
    *
 
1455
    * @throws NoSuchProviderException
 
1456
    * @throws NoSuchAlgorithmException
 
1457
    * @throws IOException
 
1458
    * @throws CertificateException
 
1459
    * @throws KeyStoreException
 
1460
    * @throws UnrecoverableKeyException
 
1461
    * @throws NullStoreURLException
 
1462
    */
 
1463
   protected KeyManager[] loadKeyManagers()
 
1464
         throws NoSuchProviderException, NoSuchAlgorithmException, IOException, CertificateException,
 
1465
                KeyStoreException, UnrecoverableKeyException, NullStoreURLException
 
1466
   {
 
1467
      String ksPasswd = getKeyStorePassword();
 
1468
      String ksType = getKeyStoreType();
 
1469
      URL ksPathURL = getKeyStore();
 
1470
 
 
1471
      KeyStore keyStore = loadKeyStore(ksType, ksPathURL, ksPasswd);
 
1472
 
 
1473
      if(keyStore != null)
 
1474
      {
 
1475
         String alias = getKeyAlias();
 
1476
 
 
1477
         // check that keystore contains supplied alias (if there is one)
 
1478
         if(alias != null)
 
1479
         {
 
1480
            boolean containsAlias = keyStore.isKeyEntry(alias);
 
1481
            if(!containsAlias)
 
1482
            {
 
1483
               // can not continue as supplied alias does not exist as key entry
 
1484
               throw new IOException("Can not find key entry for key store (" + ksPathURL + ") with given alias (" + alias + ")");
 
1485
            }
 
1486
         }
 
1487
 
 
1488
         KeyManagerFactory keyMgrFactory = null;
 
1489
         String alg = getKeyStoreAlgorithm();
 
1490
 
 
1491
         if(getProvider() != null)
 
1492
         {
 
1493
            keyMgrFactory = KeyManagerFactory.getInstance(alg, getProvider());
 
1494
         }
 
1495
         else if(getProviderName() != null)
 
1496
         {
 
1497
            keyMgrFactory = KeyManagerFactory.getInstance(alg, getProviderName());
 
1498
         }
 
1499
         else
 
1500
         {
 
1501
            keyMgrFactory = KeyManagerFactory.getInstance(alg);
 
1502
         }
 
1503
 
 
1504
         // get they key password, if it isn't defined, use the key store password
 
1505
         String keyPasswd = getKeyPassword();
 
1506
         if (keyPasswd == null || keyPasswd.length() == 0)
 
1507
         {
 
1508
            keyPasswd = ksPasswd;
 
1509
         }
 
1510
 
 
1511
         char[] keyPasswdCharArray = keyPasswd == null ? null : keyPasswd.toCharArray();
 
1512
         keyMgrFactory.init(keyStore, keyPasswdCharArray);
 
1513
         keyManagers = keyMgrFactory.getKeyManagers();
 
1514
 
 
1515
         // if alias provided, use helper impl to hard wire alias name to be used
 
1516
         if(alias != null)
 
1517
         {
 
1518
            //TODO: -TME Need careful review of if this is really needed or not.
 
1519
            for(int x = 0; x < keyManagers.length; x++)
 
1520
            {
 
1521
               keyManagers[x] = new RemotingKeyManager((X509KeyManager)keyManagers[x], alias);
 
1522
            }
 
1523
         }
 
1524
 
 
1525
      }
 
1526
      return keyManagers;
 
1527
   }
 
1528
 
 
1529
   /**
 
1530
    * Loads a key store file and returns it.
 
1531
    *
 
1532
    * @param storeType the type of store file
 
1533
    * @param storePathURL the URL to the file - may be relative to the current thread's classloader
 
1534
    *                  or may be absolute path to a file on the file system.
 
1535
    * @param storePassword password to gain access to the store file
 
1536
    *
 
1537
    * @return the key store
 
1538
    *
 
1539
    * @throws KeyStoreException
 
1540
    * @throws NoSuchProviderException
 
1541
    * @throws IOException
 
1542
    * @throws NoSuchAlgorithmException
 
1543
    * @throws CertificateException
 
1544
    * @throws NullStoreURLException
 
1545
    */
 
1546
   protected KeyStore loadKeyStore(String storeType, URL storePathURL, String storePassword)
 
1547
         throws KeyStoreException, NoSuchProviderException, IOException, NoSuchAlgorithmException, CertificateException, NullStoreURLException
 
1548
   {
 
1549
      KeyStore keyStore = null;
 
1550
 
 
1551
      if(getProvider() != null)
 
1552
      {
 
1553
         keyStore = KeyStore.getInstance(storeType, getProvider());
 
1554
      }
 
1555
      else if(getProviderName() != null)
 
1556
      {
 
1557
         keyStore = KeyStore.getInstance(storeType, getProviderName());
 
1558
      }
 
1559
      else
 
1560
      {
 
1561
         keyStore = KeyStore.getInstance(storeType);
 
1562
      }
 
1563
 
 
1564
      if ( storePathURL == null )
 
1565
      {
 
1566
         throw new NullStoreURLException("Can not find store file for url because store url is null.");
 
1567
      }
 
1568
 
 
1569
      URL url = storePathURL == NONE_STORE_URL ? null : storePathURL;
 
1570
      
 
1571
      // now that keystore instance created, need to load data from file
 
1572
      InputStream keyStoreInputStream = null;
 
1573
 
 
1574
      try
 
1575
      {
 
1576
         if (url != null)
 
1577
         {
 
1578
            keyStoreInputStream = url.openStream();
 
1579
         }
 
1580
        
 
1581
         // is ok for password to be null, as will just be used to check integrity of store
 
1582
         char[] password = storePassword != null ? storePassword.toCharArray() : null;
 
1583
         keyStore.load(keyStoreInputStream, password);
 
1584
      }
 
1585
      finally
 
1586
      {
 
1587
         if(keyStoreInputStream != null)
 
1588
         {
 
1589
            try
 
1590
            {
 
1591
               keyStoreInputStream.close();
 
1592
            }
 
1593
            catch(IOException e)
 
1594
            {
 
1595
               // no op
 
1596
            }
 
1597
            keyStoreInputStream = null;
 
1598
         }
 
1599
      }
 
1600
 
 
1601
      return keyStore;
 
1602
   }
 
1603
 
 
1604
   /**
 
1605
    * Given a store file path, this will verify that the store actually exists.
 
1606
    * First, it checks to see if its a valid URL, then it checks to see if the
 
1607
    * file path is found in the file system and finally will be checked to see
 
1608
    * if it can be found as a resource within the current thread's classloader.
 
1609
    * An exception is thrown if the store cannot be found.
 
1610
    *
 
1611
    * @param storePath the path which can be a URL, path to a resource in classloader
 
1612
    *                  or a file path on the file system.
 
1613
    *
 
1614
    * @return the URL of the file that was found
 
1615
    *
 
1616
    * @throws IOException if the store could not be found
 
1617
    */
 
1618
   protected URL validateStoreURL(String storePath) throws IOException
 
1619
   {
 
1620
      if (NONE_STORE.equals(storePath))
 
1621
      {
 
1622
         return NONE_STORE_URL;
 
1623
      }
 
1624
      
 
1625
      URL url = null;
 
1626
 
 
1627
      // First see if this is a URL
 
1628
      try
 
1629
      {
 
1630
         url = new URL(storePath);
 
1631
      }
 
1632
      catch(MalformedURLException e)
 
1633
      {
 
1634
         // Not a URL or a protocol without a handler so...
 
1635
         // next try to locate this as file path
 
1636
         final File tst = new File(storePath);
 
1637
         Boolean exists = (Boolean) AccessController.doPrivileged( new PrivilegedAction()
 
1638
         {
 
1639
            public Object run()
 
1640
            {
 
1641
               return new Boolean(tst.exists());
 
1642
            }
 
1643
         });
 
1644
 
 
1645
         if(exists.booleanValue())
 
1646
         {
 
1647
            url = tst.toURL();
 
1648
         }
 
1649
         else
 
1650
         {
 
1651
            // not a file either, lastly try to locate this as a classpath resource
 
1652
            if(url == null)
 
1653
            {
 
1654
               ClassLoader loader = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
 
1655
               {
 
1656
                  public Object run()
 
1657
                  {
 
1658
                     return Thread.currentThread().getContextClassLoader();
 
1659
                  }
 
1660
               });
 
1661
               url = loader.getResource(storePath);
 
1662
            }
 
1663
         }
 
1664
      }
 
1665
 
 
1666
      // Fail if no valid key store was located
 
1667
      if(url == null)
 
1668
      {
 
1669
         String msg = "Failed to find url=" + storePath + " as a URL, file or resource";
 
1670
         throw new MalformedURLException(msg);
 
1671
      }
 
1672
 
 
1673
      return url;
 
1674
   }
 
1675
 
 
1676
   public Object clone()
 
1677
   {
 
1678
      try
 
1679
      {
 
1680
         return super.clone();
 
1681
      }
 
1682
      catch (CloneNotSupportedException e)
 
1683
      {
 
1684
         return null;
 
1685
      }
 
1686
   }
 
1687
 
 
1688
   //*******************************************************************
 
1689
   // START
 
1690
   // The following are just needed in order to make it a service mbean.
 
1691
   // They are just NOOPs (no implementation).
 
1692
   //*******************************************************************
 
1693
 
 
1694
   /**
 
1695
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#create()
 
1696
    */
 
1697
   public void create() throws Exception
 
1698
   {
 
1699
      return; // no-op
 
1700
   }
 
1701
 
 
1702
   /**
 
1703
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#start()
 
1704
    */
 
1705
   public void start() throws Exception
 
1706
   {
 
1707
      return; // no-op
 
1708
   }
 
1709
 
 
1710
   /**
 
1711
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#stop()
 
1712
    */
 
1713
   public void stop()
 
1714
   {
 
1715
      return; // no-op
 
1716
   }
 
1717
 
 
1718
   /**
 
1719
    * @see org.jboss.remoting.security.SSLSocketBuilderMBean#destroy()
 
1720
    */
 
1721
   public void destroy()
 
1722
   {
 
1723
      return; // no-op
 
1724
   }
 
1725
 
 
1726
   //*******************************************************************
 
1727
   // END
 
1728
   //*******************************************************************
 
1729
 
 
1730
   /**
 
1731
    * Used to indicate a store URL was not specified and thus the store is not available.
 
1732
    */
 
1733
   protected class NullStoreURLException extends Exception
 
1734
   {
 
1735
      private static final long serialVersionUID = 1L;
 
1736
 
 
1737
      /**
 
1738
       * @see Exception#Exception(String)
 
1739
       */
 
1740
      public NullStoreURLException(String message)
 
1741
      {
 
1742
         super(message);
 
1743
      }
 
1744
   }
 
1745
   
 
1746
   static private String getSystemProperty(final String name)
 
1747
   {
 
1748
      if (SecurityUtility.skipAccessControl())
 
1749
         return System.getProperty(name);
 
1750
      
 
1751
      String value = null;
 
1752
      try
 
1753
      {
 
1754
         value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
 
1755
         {
 
1756
            public Object run() throws Exception
 
1757
            {
 
1758
               return System.getProperty(name);
 
1759
            }
 
1760
         });
 
1761
      }
 
1762
      catch (PrivilegedActionException e)
 
1763
      {
 
1764
         throw (RuntimeException) e.getCause();
 
1765
      }
 
1766
      
 
1767
      return value;
 
1768
   }
 
1769
   
 
1770
   static private Method getMethod(final Class c, final String name, final Class[] parameterTypes)
 
1771
   throws NoSuchMethodException
 
1772
   {
 
1773
      if (SecurityUtility.skipAccessControl())
 
1774
      {
 
1775
         return c.getMethod(name, parameterTypes);
 
1776
      }
 
1777
 
 
1778
      try
 
1779
      {
 
1780
         return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
 
1781
         {
 
1782
            public Object run() throws NoSuchMethodException
 
1783
            {
 
1784
               return c.getMethod(name, parameterTypes);
 
1785
            }
 
1786
         });
 
1787
      }
 
1788
      catch (PrivilegedActionException e)
 
1789
      {
 
1790
         throw (NoSuchMethodException) e.getCause();
 
1791
      }
 
1792
   }
 
1793
}
 
 
b'\\ No newline at end of file'