| 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.Serializable; |
| 41 | import java.lang.reflect.Constructor; |
| 42 | import java.net.Socket; |
| 43 | |
| 44 | /** |
| 45 | * Creates an SSL socket connection to an LDAP Server. This class |
| 46 | * implements the <CODE>LDAPSSLSocketFactoryExt</CODE> interface. |
| 47 | * <P> |
| 48 | * |
| 49 | * To construct an object of this class, you need to specify the |
| 50 | * name of a class that implements the <CODE>javax.net.ssl.SSLSocket</CODE> |
| 51 | * interface. If you do not specify a class name, the class |
| 52 | * <CODE>netscape.net.SSLSocket</CODE> is used by default. This |
| 53 | * class is included with Netscape Communicator 4.05 and up. |
| 54 | * <P> |
| 55 | * |
| 56 | * If you are using a Java VM that provides certificate database |
| 57 | * management (such as Netscape Communicator), you can authenticate |
| 58 | * your client to a secure LDAP server by using certificates. |
| 59 | * <P> |
| 60 | * |
| 61 | * @version 1.0 |
| 62 | * @see LDAPSSLSocketFactoryExt |
| 63 | * @see LDAPConnection#LDAPConnection(org.ietf.ldap.LDAPSocketFactory) |
| 64 | */ |
| 65 | public class LDAPSSLSocketFactory |
| 66 | implements LDAPSSLSocketFactoryExt, Serializable { |
| 67 | |
| 68 | static final long serialVersionUID = -3331456736649381427L; |
| 69 | |
| 70 | /** |
| 71 | * Indicates if client authentication is on. |
| 72 | */ |
| 73 | private boolean _clientAuth = false; |
| 74 | /** |
| 75 | * Name of class implementing SSLSocket. |
| 76 | */ |
| 77 | private String _packageName = "netscape.net.SSLSocket"; |
| 78 | |
| 79 | /** |
| 80 | * The cipher suites |
| 81 | */ |
| 82 | private Object _cipherSuites = null; |
| 83 | |
| 84 | /** |
| 85 | * Constructs an <CODE>LDAPSSLSocketFactory</CODE> object using |
| 86 | * the default SSL socket implementation, |
| 87 | * <CODE>netscape.net.SSLSocket</CODE>. (This class is provided |
| 88 | * with Netscape Communicator 4.05 and higher.) |
| 89 | */ |
| 90 | public LDAPSSLSocketFactory() { |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Constructs an <CODE>LDAPSSLSocketFactory</CODE> object using |
| 95 | * the default SSL socket implementation, |
| 96 | * <CODE>netscape.net.SSLSocket</CODE>. (This class is provided |
| 97 | * with Netscape Communicator 4.05 and up.) |
| 98 | * @param clientAuth <CODE>true</CODE> if certificate-based client |
| 99 | * authentication is desired. By default, client authentication is |
| 100 | * not used. |
| 101 | */ |
| 102 | public LDAPSSLSocketFactory(boolean clientAuth) { |
| 103 | _clientAuth = clientAuth; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Constructs an <CODE>LDAPSSLSocketFactory</CODE> object using |
| 108 | * the specified class. The class must implement the interface |
| 109 | * <CODE>javax.net.ssl.SSLSocket</CODE>. |
| 110 | * @param className the name of a class implementing |
| 111 | * the <CODE>javax.net.ssl.SSLSocket</CODE> interface. |
| 112 | * Pass <code>null</code> for this parameter to use the |
| 113 | * default SSL socket implementation, |
| 114 | * <CODE>netscape.net.SSLSocket</CODE>, which is included with |
| 115 | * Netscape Communicator 4.05 and higher. |
| 116 | */ |
| 117 | public LDAPSSLSocketFactory(String className) { |
| 118 | _packageName = new String(className); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Constructs an <CODE>LDAPSSLSocketFactory</CODE> object using |
| 123 | * the specified class. The class must implement the interface |
| 124 | * <CODE>javax.net.ssl.SSLSocket</CODE>. |
| 125 | * @param className the name of a class implementing |
| 126 | * the <CODE>javax.net.ssl.SSLSocket</CODE> interface. |
| 127 | * Pass <code>null</code> for this parameter to use the |
| 128 | * default SSL socket implementation, |
| 129 | * <CODE>netscape.net.SSLSocket</CODE>, which is included with |
| 130 | * Netscape Communicator 4.05 and higher. |
| 131 | * @param clientAuth <CODE>true</CODE> if certificate-based client |
| 132 | * authentication is desired. By default, client authentication is |
| 133 | * not used. |
| 134 | */ |
| 135 | public LDAPSSLSocketFactory(String className, boolean clientAuth) { |
| 136 | _packageName = new String(className); |
| 137 | _clientAuth = clientAuth; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * The constructor with the specified package for security and the specified |
| 142 | * cipher suites. |
| 143 | * @param className the name of a class implementing the interface |
| 144 | * <CODE>javax.net.ssl.SSLSocket</CODE>. |
| 145 | * Pass <code>null</code> for this parameter to use the |
| 146 | * default SSL socket implementation, |
| 147 | * <CODE>netscape.net.SSLSocket</CODE>, which is included with |
| 148 | * Netscape Communicator 4.05 and higher. |
| 149 | * @param cipherSuites the cipher suites to use for SSL connections |
| 150 | */ |
| 151 | public LDAPSSLSocketFactory(String className, Object cipherSuites) { |
| 152 | _packageName = new String(className); |
| 153 | _cipherSuites = cipherSuites; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * The constructor with the specified package for security and the specified |
| 158 | * cipher suites. |
| 159 | * @param className the name of a class implementing the interface |
| 160 | * <CODE>javax.net.ssl.SSLSocket</CODE>. |
| 161 | * Pass <code>null</code> for this parameter to use the |
| 162 | * default SSL socket implementation, |
| 163 | * <CODE>netscape.net.SSLSocket</CODE>, which is included with |
| 164 | * Netscape Communicator 4.05 and higher. |
| 165 | * @param cipherSuites the cipher suites to use for SSL connections |
| 166 | * @param clientAuth <CODE>true</CODE> if certificate-based client |
| 167 | * authentication is desired. By default, client authentication is |
| 168 | * not used. |
| 169 | */ |
| 170 | public LDAPSSLSocketFactory(String className, Object cipherSuites, |
| 171 | boolean clientAuth) { |
| 172 | _packageName = new String(className); |
| 173 | _cipherSuites = cipherSuites; |
| 174 | _clientAuth = clientAuth; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Enables certificate-based client authentication for an |
| 179 | * application. The application must be running in a Java VM |
| 180 | * that provides transparent certificate database management |
| 181 | * (for example, Netscape Communicator's Java VM). |
| 182 | * Call this method before you call <CODE>createSocket</CODE>. |
| 183 | * @see org.ietf.ldap.LDAPSSLSocketFactory#isClientAuth |
| 184 | * @see org.ietf.ldap.LDAPSSLSocketFactory#createSocket |
| 185 | * Note: enableClientAuth() is deprecated. This method is replaced |
| 186 | * by any one of the following constructors: |
| 187 | * <p> |
| 188 | * <CODE>LDAPSSLSocketFactory(boolean)</CODE> |
| 189 | * <CODE>LDAPSSLSocketFactory(java.lang.String, boolean)</CODE> |
| 190 | * <CODE>LDAPSSLSocketFactory(java.lang.String, java.lang.Object, boolean)</CODE> |
| 191 | */ |
| 192 | public void enableClientAuth() { |
| 193 | _clientAuth = true; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /** |
| 198 | * Enables client authentication for an application that uses |
| 199 | * an external (file-based) certificate database. |
| 200 | * <B>This method is currently not implemented.</B> |
| 201 | * Call this method before you call <CODE>createSocket</CODE>. |
| 202 | * @param certdb the pathname for the certificate database |
| 203 | * @param keydb the pathname for the private key database |
| 204 | * @param keypwd the password for the private key database |
| 205 | * @param certnickname the alias for the certificate |
| 206 | * @param keynickname the alias for the key |
| 207 | * @see org.ietf.ldap.LDAPSSLSocketFactory#isClientAuth |
| 208 | * @see org.ietf.ldap.LDAPSSLSocketFactory#createSocket |
| 209 | * @exception LDAPException Since this method is not yet implemented, |
| 210 | * calling this method throws an exception. |
| 211 | * Note: <CODE>enableClientAuth(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)</CODE> is deprecated. |
| 212 | * This method is replaced by any one of the following constructors: |
| 213 | * <p> |
| 214 | * <CODE>LDAPSSLSocketFactory(boolean)</CODE> |
| 215 | * <CODE>LDAPSSLSocketFactory(java.lang.String, boolean)</CODE> |
| 216 | * <CODE>LDAPSSLSocketFactory(java.lang.String, java.lang.Object, boolean)</CODE> |
| 217 | */ |
| 218 | public void enableClientAuth(String certdb, String keydb, String keypwd, |
| 219 | String certnickname, String keynickname) throws LDAPException { |
| 220 | throw new LDAPException("Client auth not supported now"); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns <code>true</code> if client authentication is enabled. |
| 225 | * @see org.ietf.ldap.LDAPSSLSocketFactory |
| 226 | */ |
| 227 | public boolean isClientAuth() { |
| 228 | return _clientAuth; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Returns the name of the class that implements SSL sockets for this factory. |
| 233 | * |
| 234 | * @return the name of the class that implements SSL sockets for this factory. |
| 235 | */ |
| 236 | public String getSSLSocketImpl() { |
| 237 | return _packageName; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns the suite of ciphers used for SSL connections made through |
| 242 | * sockets created by this factory. |
| 243 | * |
| 244 | * @return the suite of ciphers used. |
| 245 | */ |
| 246 | public Object getCipherSuites() { |
| 247 | return _cipherSuites; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Returns a socket to the LDAP server with the specified |
| 252 | * host name and port number. |
| 253 | * @param host the host to connect to |
| 254 | * @param port the port number |
| 255 | * @return the socket to the host name and port number. |
| 256 | * @exception LDAPException A socket to the specified host and port |
| 257 | * could not be created. |
| 258 | * @see org.ietf.ldap.LDAPSSLSocketFactory |
| 259 | */ |
| 260 | public Socket createSocket(String host, int port) |
| 261 | throws LDAPException { |
| 262 | |
| 263 | Socket s = null; |
| 264 | |
| 265 | if (_clientAuth) { |
| 266 | try { |
| 267 | /* Check if running in Communicator; if so, enable client |
| 268 | auth */ |
| 269 | String[] types = { "java.lang.String" }; |
| 270 | java.lang.reflect.Method m = |
| 271 | DynamicInvoker.getMethod( |
| 272 | "netscape.security.PrivilegeManager", |
| 273 | "enablePrivilege", |
| 274 | types ); |
| 275 | if (m != null) { |
| 276 | Object[] args = new Object[1]; |
| 277 | args[0] = new String("ClientAuth"); |
| 278 | m.invoke( null, args); |
| 279 | } |
| 280 | } catch (Exception e) { |
| 281 | String msg = "LDAPSSLSocketFactory.createSocket: invoking " + |
| 282 | "enablePrivilege: " + e.toString(); |
| 283 | throw new LDAPException(msg, LDAPException.PARAM_ERROR); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | try { |
| 288 | String cipherClassName = null; |
| 289 | if (_cipherSuites != null) |
| 290 | cipherClassName = _cipherSuites.getClass().getName(); |
| 291 | |
| 292 | /* Instantiate the SSLSocketFactory implementation, and |
| 293 | find the right constructor */ |
| 294 | Class<?> c = Class.forName(_packageName); |
| 295 | Constructor<?>[] m = c.getConstructors(); |
| 296 | for (int i = 0; i < m.length; i++) { |
| 297 | /* Check if the signature is right: String, int */ |
| 298 | Class<?>[] params = m[i].getParameterTypes(); |
| 299 | if ( (_cipherSuites == null) && (params.length == 2) && |
| 300 | (params[0].getName().equals("java.lang.String")) && |
| 301 | (params[1].getName().equals("int")) ) { |
| 302 | Object[] args = new Object[2]; |
| 303 | args[0] = host; |
| 304 | args[1] = port; |
| 305 | s = (Socket)(m[i].newInstance(args)); |
| 306 | return s; |
| 307 | } else if ( (_cipherSuites != null) && (params.length == 3) && |
| 308 | (params[0].getName().equals("java.lang.String")) && |
| 309 | (params[1].getName().equals("int")) && |
| 310 | (params[2].getName().equals(cipherClassName)) ) { |
| 311 | Object[] args = new Object[3]; |
| 312 | args[0] = host; |
| 313 | args[1] = port; |
| 314 | args[2] = _cipherSuites; |
| 315 | s = (Socket)(m[i].newInstance(args)); |
| 316 | return s; |
| 317 | } |
| 318 | } |
| 319 | throw new LDAPException("No appropriate constructor in " + |
| 320 | _packageName, |
| 321 | LDAPException.PARAM_ERROR); |
| 322 | } catch (ClassNotFoundException e) { |
| 323 | throw new LDAPException("Class " + _packageName + " not found", |
| 324 | LDAPException.PARAM_ERROR); |
| 325 | } catch (Exception e) { |
| 326 | throw new LDAPException("Failed to create SSL socket", |
| 327 | LDAPException.CONNECT_ERROR); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 |