1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
71
72
73
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
81 System.setProperty("javax.net.ssl.trustStore", truststoreFilename);
82 System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
83
84
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");
88
89 env.put(Context.PROVIDER_URL, url);
90
91
92 InitialLdapContext ldap = new InitialLdapContext(env, null);
93
94
95 System.out.println("Doing StartTLS");
96 StartTlsResponse tls = (StartTlsResponse) ldap.extendedOperation(new StartTlsRequest());
97
98
99 System.out.println("Opening SSL connection");
100 tls.negotiate();
101
102
103 System.out.println("Closing");
104 tls.close();
105 ldap.close();
106
107
108 System.out.println("Done");
109
110 }
111 }