View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   */
19  package no.feide.moria.directory.backend.tools;
20  
21  import java.io.IOException;
22  import java.util.Hashtable;
23  
24  import javax.naming.Context;
25  import javax.naming.NamingException;
26  import javax.naming.ldap.InitialLdapContext;
27  import javax.naming.ldap.StartTlsRequest;
28  import javax.naming.ldap.StartTlsResponse;
29  
30  /***
31   * A simple tool to test TLS (StartTLS) behaviour against LDAP servers.
32   * @author Cato Olsen
33   */
34  public final class StartTlsTester {
35  
36      /*** Default private constructor. */
37      private StartTlsTester() {
38  
39          // Does nothing.
40  
41      };
42  
43  
44      /***
45       * Main method. Reads the index file, writes the index object, and finally
46       * verifies that the generated and written contents match.
47       * @param args
48       *            <ol>
49       *            <li>truststore filename</li>
50       *            <li>truststore password</li>
51       *            <li>LDAP URL</li>
52       *            </ol>
53       * @throws NamingException
54       *             If there is an LDAP problem.
55       * @throws IOException
56       *             If unable to read from or write to truststore file.
57       */
58      public static void main(final String[] args) throws NamingException,
59                                                  IOException {
60  
61          // Show usage?
62          if (args.length < 3) {
63              System.out.println("Usage:");
64              System.out.println("Parameter 1 - truststore filename");
65              System.out.println("Parameter 2 - truststore password");
66              System.out.println("Parameter 3 - LDAP URL");
67              System.exit(0);
68          }
69  
70          // Uncomment (or run with -D) to enable SSL debugging.
71          // System.setProperty("javax.net.debug", "ssl");
72  
73          // Status.
74          final String truststoreFilename = args[0];
75          System.out.println("Using truststore " + truststoreFilename);
76          final String truststorePassword = args[1];
77          final String url = args[2];
78          System.out.println("Connecting to " + url);
79  
80          // Setting global truststore properties.
81          System.setProperty("javax.net.ssl.trustStore", truststoreFilename);
82          System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
83  
84          // Prepare environment.
85          Hashtable env = new Hashtable();
86          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
87          env.put("java.naming.ldap.derefAliases", "never"); // Due to OpenSSL
88                                                              // problems.
89          env.put(Context.PROVIDER_URL, url);
90  
91          // Opening.
92          InitialLdapContext ldap = new InitialLdapContext(env, null);
93  
94          // Doing StartTLS.
95          System.out.println("Doing StartTLS");
96          StartTlsResponse tls = (StartTlsResponse) ldap.extendedOperation(new StartTlsRequest());
97  
98          // Opening TLS connection.
99          System.out.println("Opening SSL connection");
100         tls.negotiate();
101 
102         // Closing.
103         System.out.println("Closing");
104         tls.close();
105         ldap.close();
106 
107         // All done.
108         System.out.println("Done");
109 
110     }
111 }