Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/LDAPSSLSocketWrapFactory.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap;
39
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.OutputStream;
43 import java.lang.reflect.Constructor;
44 import java.lang.reflect.Method;
45 import java.net.InetAddress;
46 import java.net.Socket;
47 import java.util.Hashtable;
48
49 /**
50 * Creates an SSL socket connection to an LDAP Server. This class is provided
51 * by the package in which the SSL socket does not extend Socket object.
52 * The class internally provides a wrapper to convert the SSL socket extending
53 * the Object class to the one extending the Socket class.
54 * This factory class implements the <CODE>LDAPSocketFactory</CODE> interface.
55 * <P>
56 *
57 * To use this class, pass the instance of this factory object to the
58 * <CODE>LDAPConnection</CODE> constructor.
59 *
60 * @version 1.0
61 * @see LDAPSocketFactory
62 * @see LDAPConnection#LDAPConnection(org.ietf.ldap.LDAPSocketFactory)
63 */
64 public class LDAPSSLSocketWrapFactory
65 implements LDAPSSLSocketFactoryExt, java.io.Serializable {
66
67 static final long serialVersionUID = -4171548771815037740L;
68
69 /**
70 * The constructor with the specified package for security
71 * @param className the name of a class which has an implementation
72 * of the SSL Socket extending Object class
73 */
74 public LDAPSSLSocketWrapFactory(String className) {
75 _packageName = new String(className);
76 }
77
78 /**
79 * The constructor with the specified package for security and the
80 * specified cipher suites.
81 * @param className the name of a class which has an implementation
82 * of the SSL Socket extending Object class
83 * @param cipherSuites the cipher suites
84 */
85 public LDAPSSLSocketWrapFactory(String className, Object cipherSuites) {
86 _packageName = new String(className);
87 _cipherSuites = cipherSuites;
88 }
89
90 /**
91 * Returns socket to the specified host name and port number.
92 * @param host the host to connect to
93 * @param port the port number
94 * @return the socket to the host name and port number as passed in.
95 * @exception LDAPException A socket to the specified host and port
96 * could not be created.
97 */
98 public Socket createSocket(String host, int port) throws LDAPException {
99
100 LDAPSSLSocket s = null;
101
102 try {
103 if (_cipherSuites == null)
104 s = new LDAPSSLSocket(host, port, _packageName);
105 else
106 s = new LDAPSSLSocket(host, port, _packageName,
107 _cipherSuites);
108 return s;
109 } catch (Exception e) {
110 System.err.println("Exception: "+e.toString());
111 throw new LDAPException("Failed to create SSL socket",
112 LDAPException.CONNECT_ERROR);
113 }
114 }
115
116 /**
117 * Returns <code>true</code> if client authentication is to be used.
118 * @return <code>true</code> if client authentication is enabled;
119 * <code>false</code>if client authentication is disabled.
120 */
121 public boolean isClientAuth() {
122 return _clientAuth;
123 }
124
125 /**
126 * <B>(Not implemented yet)</B> <BR>
127 * Enables client authentication for an application running in
128 * a java VM which provides transparent certificate database management.
129 * Calling this method has no effect after createSocket() has been
130 * called.
131 * @exception LDAPException Since this method is not yet implemented,
132 * calling this method throws an exception.
133 */
134 public void enableClientAuth() throws LDAPException {
135 throw new LDAPException("Client Authentication is not implemented yet.");
136 }
137
138 /**
139 * Returns the name of the class that implements SSL sockets for this factory.
140 *
141 * @return the name of the class that implements SSL sockets for this factory.
142 */
143 public String getSSLSocketImpl() {
144 return _packageName;
145 }
146
147 /**
148 * Returns the suite of ciphers used for SSL connections made through
149 * sockets created by this factory.
150 *
151 * @return the suite of ciphers used.
152 */
153 public Object getCipherSuites() {
154 return _cipherSuites;
155 }
156
157 /**
158 * Indicates if client authentication is on.
159 */
160 private boolean _clientAuth = false;
161
162 /**
163 * Name of class implementing SSLSocket.
164 */
165 private String _packageName = null;
166
167 /**
168 * The cipher suites
169 */
170 private Object _cipherSuites = null;
171 }
172
173 // LDAPSSLSocket class wraps the implementation of the SSL socket
174 class LDAPSSLSocket extends Socket {
175
176 public LDAPSSLSocket(String host, int port, String packageName)
177 throws LDAPException {
178 super();
179 _packageName = packageName;
180 try {
181 // instantiate the SSLSocketFactory implementation, and
182 // find the right constructor
183 Class<?> c = Class.forName(_packageName);
184 Constructor<?>[] m = c.getConstructors();
185
186 for (int i = 0; i < m.length; i++) {
187 /* Check if the signature is right: String, int */
188 Class<?>[] params = m[i].getParameterTypes();
189
190 if ((params.length == 2) &&
191 (params[0].getName().equals("java.lang.String")) &&
192 (params[1].getName().equals("int"))) {
193 Object[] args = new Object[2];
194 args[0] = host;
195 args[1] = port;
196 _socket = (m[i].newInstance(args));
197 return;
198 }
199 }
200 throw new LDAPException("No appropriate constructor in " +
201 _packageName, LDAPException.PARAM_ERROR);
202 } catch (ClassNotFoundException e) {
203 throw new LDAPException("Class " + _packageName + " not found",
204 LDAPException.OTHER);
205 } catch (Exception e) {
206 throw new LDAPException("Failed to create SSL socket",
207 LDAPException.CONNECT_ERROR);
208 }
209 }
210
211 public LDAPSSLSocket(String host, int port, String packageName,
212 Object cipherSuites) throws LDAPException {
213 super();
214 _packageName = packageName;
215 String cipherClassName = null;
216 if (cipherSuites != null)
217 cipherClassName = cipherSuites.getClass().getName();
218
219 try {
220 // instantiate the SSLSocketFactory implementation, and
221 // find the right constructor
222 Class<?> c = Class.forName(_packageName);
223 Constructor<?>[] m = c.getConstructors();
224
225 for (int i = 0; i < m.length; i++) {
226 /* Check if the signature is right: String, int */
227 Class<?>[] params = m[i].getParameterTypes();
228 if (cipherSuites == null)
229 throw new LDAPException("Cipher Suites is required");
230
231 if ((params.length == 3) &&
232 (params[0].getName().equals("java.lang.String")) &&
233 (params[1].getName().equals("int")) &&
234 (params[2].getName().equals(cipherClassName))) {
235 Object[] args = new Object[3];
236 args[0] = host;
237 args[1] = port;
238 args[2] = cipherSuites;
239 _socket = (m[i].newInstance(args));
240 return;
241 }
242 }
243 throw new LDAPException("No appropriate constructor in " +
244 _packageName, LDAPException.PARAM_ERROR);
245 } catch (ClassNotFoundException e) {
246 throw new LDAPException("Class " + _packageName + " not found",
247 LDAPException.OTHER);
248 } catch (Exception e) {
249 throw new LDAPException("Failed to create SSL socket",
250 LDAPException.CONNECT_ERROR);
251 }
252 }
253
254 public InputStream getInputStream() {
255 try {
256 Object obj = invokeMethod(_socket, "getInputStream", null);
257 return (InputStream)obj;
258 } catch (LDAPException e) {
259 printDebug(e.toString());
260 }
261
262 return null;
263 }
264
265 public OutputStream getOutputStream() {
266 try {
267 Object obj = invokeMethod(_socket, "getOutputStream", null);
268 return (OutputStream)obj;
269 } catch (LDAPException e) {
270 printDebug(e.toString());
271 }
272
273 return null;
274 }
275
276 public void close() throws IOException {
277 try {
278 invokeMethod(_socket, "close", null);
279 } catch (LDAPException e) {
280 printDebug(e.toString());
281 }
282 }
283
284 public void close(boolean wait) throws IOException {
285 try {
286 Object[] args = new Object[1];
287 args[0] = wait;
288 invokeMethod(_socket, "close", args);
289 } catch (LDAPException e) {
290 printDebug(e.toString());
291 }
292 }
293
294 public InetAddress getInetAddress() {
295 try {
296 Object obj = invokeMethod(_socket, "getInetAddress", null);
297 return (InetAddress)obj;
298 } catch (LDAPException e) {
299 printDebug(e.toString());
300 }
301
302 return null;
303 }
304
305 public int getLocalPort() {
306 try {
307 Object obj = invokeMethod(_socket, "getLocalPort", null);
308 return ((Integer)obj).intValue();
309 } catch (LDAPException e) {
310 printDebug(e.toString());
311 }
312
313 return -1;
314 }
315
316 public int getPort() {
317 try {
318 Object obj = invokeMethod(_socket, "getPort", null);
319 return ((Integer)obj).intValue();
320 } catch (LDAPException e) {
321 printDebug(e.toString());
322 }
323
324 return -1;
325 }
326
327 private Object invokeMethod(Object obj, String name, Object[] args) throws
328 LDAPException {
329 try {
330 Method m = getMethod(name);
331 if (m != null) {
332 return (m.invoke(obj, args));
333 }
334 } catch (Exception e) {
335 throw new LDAPException("Invoking "+name+": "+
336 e.toString(), LDAPException.PARAM_ERROR);
337 }
338
339 return null;
340 }
341
342 private Method getMethod(String name) throws
343 LDAPException {
344 try {
345 Method method = null;
346 if ((method = _methodLookup.get(name)) != null)
347 return method;
348
349 Class<?> c = Class.forName(_packageName);
350 Method[] m = c.getMethods();
351 for (int i = 0; i < m.length; i++ ) {
352 if (m[i].getName().equals(name)) {
353 _methodLookup.put(name, m[i]);
354 return m[i];
355 }
356 }
357 throw new LDAPException("Method " + name + " not found in " +
358 _packageName);
359 } catch (ClassNotFoundException e) {
360 throw new LDAPException("Class "+ _packageName + " not found");
361 }
362 }
363
364 private void printDebug(String msg) {
365 if (_debug) {
366 System.out.println(msg);
367 }
368 }
369
370 private final boolean _debug = true;
371 private Object _socket;
372 private Hashtable<String, Method> _methodLookup = new Hashtable<>();
373 private String _packageName = null;
374 }
375