| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public class LDAPSSLSocketWrapFactory |
| 65 |
implements LDAPSSLSocketFactoryExt, java.io.Serializable { |
| 66 |
|
| 67 |
static final long serialVersionUID = -4171548771815037740L; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public LDAPSSLSocketWrapFactory(String className) { |
| 75 |
_packageName = new String(className); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public LDAPSSLSocketWrapFactory(String className, Object cipherSuites) { |
| 86 |
_packageName = new String(className); |
| 87 |
_cipherSuites = cipherSuites; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public boolean isClientAuth() { |
| 122 |
return _clientAuth; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void enableClientAuth() throws LDAPException { |
| 135 |
throw new LDAPException("Client Authentication is not implemented yet."); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public String getSSLSocketImpl() { |
| 144 |
return _packageName; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public Object getCipherSuites() { |
| 154 |
return _cipherSuites; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private boolean _clientAuth = false; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
private String _packageName = null; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private Object _cipherSuites = null; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 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 |
|
| 182 |
|
| 183 |
Class<?> c = Class.forName(_packageName); |
| 184 |
Constructor<?>[] m = c.getConstructors(); |
| 185 |
|
| 186 |
for (int i = 0; i < m.length; i++) { |
| 187 |
|
| 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 |
|
| 221 |
|
| 222 |
Class<?> c = Class.forName(_packageName); |
| 223 |
Constructor<?>[] m = c.getConstructors(); |
| 224 |
|
| 225 |
for (int i = 0; i < m.length; i++) { |
| 226 |
|
| 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 |
|