View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.ldap.handlers.ssl;
21  
22  
23  import java.security.KeyStore;
24  import java.security.SecureRandom;
25  import java.security.Security;
26  
27  import javax.naming.NamingException;
28  import javax.net.ssl.KeyManagerFactory;
29  import javax.net.ssl.SSLContext;
30  import javax.net.ssl.TrustManager;
31  
32  import org.apache.mina.common.DefaultIoFilterChainBuilder;
33  import org.apache.mina.common.IoFilterChainBuilder;
34  import org.apache.mina.filter.SSLFilter;
35  
36  
37  /**
38   * Loads the certificate file for LDAPS support and creates the appropriate
39   * MINA filter chain.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev:687105 $, $Date:2008-08-19 19:40:48 +0200 (Tue, 19 Aug 2008) $
43   *
44   */
45  public class LdapsInitializer
46  {
47      public static IoFilterChainBuilder init( KeyStore ks ) throws NamingException
48      {
49          SSLContext sslCtx;
50          try
51          {
52              // Set up key manager factory to use our key store
53              String algorithm = Security.getProperty( "ssl.KeyManagerFactory.algorithm" );
54              if ( algorithm == null )
55              {
56                  algorithm = "SunX509";
57              }
58              KeyManagerFactory kmf = KeyManagerFactory.getInstance( algorithm );
59              kmf.init( ks, null );
60  
61              // Initialize the SSLContext to work with our key managers.
62              sslCtx = SSLContext.getInstance( "TLS" );
63              sslCtx.init( kmf.getKeyManagers(), new TrustManager[]
64                  { new ServerX509TrustManager() }, new SecureRandom() );
65          }
66          catch ( Exception e )
67          {
68              throw ( NamingException ) new NamingException( "Failed to create a SSL context." ).initCause( e );
69          }
70  
71          DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();
72          chain.addLast( "sslFilter", new SSLFilter( sslCtx ) );
73          return chain;
74      }
75  }