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.SecureRandom;
24  import java.util.List;
25  
26  import javax.net.ssl.SSLContext;
27  import javax.net.ssl.TrustManager;
28  
29  import org.apache.directory.api.ldap.model.exception.LdapException;
30  import org.apache.directory.ldap.client.api.NoVerificationTrustManager;
31  import org.apache.directory.server.i18n.I18n;
32  import org.apache.directory.server.ldap.LdapServer;
33  import org.apache.directory.server.protocol.shared.transport.TcpTransport;
34  import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
35  import org.apache.mina.core.filterchain.IoFilterChainBuilder;
36  import org.apache.mina.filter.ssl.SslFilter;
37  
38  
39  /**
40   * Loads the certificate file for LDAPS support and creates the appropriate
41   * MINA filter chain.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   *
45   */
46  public final class LdapsInitializer
47  {
48      private LdapsInitializer()
49      {
50      }
51  
52  
53      /**
54       * Initialize the LDAPS server.
55       *
56       * @param ldapServer The LDAP server instance
57       * @param transport The TCP transport that contains the SSL configuration
58       * @return A IoFilter chain
59       * @throws LdapException If we had a pb
60       */
61      public static IoFilterChainBuilder init( LdapServer ldapServer, TcpTransport transport ) throws LdapException
62      {
63          SSLContext sslCtx;
64  
65          try
66          {
67              // Initialize the SSLContext to work with our key managers.
68              sslCtx = SSLContext.getInstance( "TLS" );
69              sslCtx.init( ldapServer.getKeyManagerFactory().getKeyManagers(), new TrustManager[]
70                  { new NoVerificationTrustManager() }, new SecureRandom() );
71          }
72          catch ( Exception e )
73          {
74              throw new LdapException( I18n.err( I18n.ERR_683 ), e );
75          }
76  
77          DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();
78          SslFilter sslFilter = new SslFilter( sslCtx );
79  
80          // The ciphers
81          List<String> cipherSuites = transport.getCipherSuite();
82  
83          if ( ( cipherSuites != null ) && !cipherSuites.isEmpty() )
84          {
85              sslFilter.setEnabledCipherSuites( cipherSuites.toArray( new String[cipherSuites.size()] ) );
86          }
87  
88          // The protocols
89          List<String> enabledProtocols = transport.getEnabledProtocols();
90  
91          if ( ( enabledProtocols != null ) && !enabledProtocols.isEmpty() )
92          {
93              sslFilter.setEnabledProtocols( enabledProtocols.toArray( new String[enabledProtocols.size()] ) );
94          }
95          else
96          {
97              // Be sure we disable SSLV3
98              sslFilter.setEnabledProtocols( new String[]
99                  { "TLSv1", "TLSv1.1", "TLSv1.2" } );
100         }
101 
102         // The remaining SSL parameters
103         sslFilter.setNeedClientAuth( transport.isNeedClientAuth() );
104         sslFilter.setWantClientAuth( transport.isWantClientAuth() );
105 
106         chain.addLast( "sslFilter", sslFilter );
107 
108         return chain;
109     }
110 }