| 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 | |
| 39 | package org.ietf.ldap.factory; |
| 40 | |
| 41 | import java.io.IOException; |
| 42 | import java.io.Serializable; |
| 43 | import java.net.Socket; |
| 44 | import java.net.UnknownHostException; |
| 45 | |
| 46 | import javax.net.ssl.SSLSocket; |
| 47 | import javax.net.ssl.SSLSocketFactory; |
| 48 | |
| 49 | import org.ietf.ldap.LDAPConnection; |
| 50 | import org.ietf.ldap.LDAPException; |
| 51 | import org.ietf.ldap.LDAPSocketFactory; |
| 52 | |
| 53 | /** |
| 54 | * Creates an SSL socket connection to a server, using the JSSE package |
| 55 | * from Sun. This class implements the <CODE>LDAPSocketFactory</CODE> |
| 56 | * interface. |
| 57 | * <P> |
| 58 | * |
| 59 | * @version 1.0 |
| 60 | * @see LDAPSocketFactory |
| 61 | * @see LDAPConnection#LDAPConnection(org.ietf.ldap.LDAPSocketFactory) |
| 62 | */ |
| 63 | public class JSSESocketFactory |
| 64 | implements LDAPSocketFactory, Serializable { |
| 65 | |
| 66 | static final long serialVersionUID = 6834205777733266609L; |
| 67 | |
| 68 | protected SSLSocketFactory factory = null; |
| 69 | |
| 70 | // Optional explicit cipher suites to use |
| 71 | protected String[] suites = null; |
| 72 | |
| 73 | /** |
| 74 | * Default factory constructor |
| 75 | */ |
| 76 | public JSSESocketFactory() { |
| 77 | this.factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Factory constructor that uses the default JSSE SSLSocketFactory |
| 82 | * |
| 83 | * @param suites Cipher suites to attempt to use with the server; |
| 84 | * if <code>null</code>, use any cipher suites available in the |
| 85 | * JSSE package |
| 86 | */ |
| 87 | public JSSESocketFactory( String[] suites ) { |
| 88 | this.suites = suites; |
| 89 | this.factory = (SSLSocketFactory)SSLSocketFactory.getDefault(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Factory constructor |
| 94 | * @param sf the SSL socketfactory to use |
| 95 | */ |
| 96 | public JSSESocketFactory( SSLSocketFactory factory) { |
| 97 | this.factory = factory; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Factory constructor |
| 102 | * |
| 103 | * @param suites Cipher suites to attempt to use with the server; |
| 104 | * if <code>null</code>, use any cipher suites available in the |
| 105 | * JSSE package |
| 106 | * @param sf the SSL socketfactory to use |
| 107 | */ |
| 108 | public JSSESocketFactory( String[] suites, SSLSocketFactory factory ) { |
| 109 | this.suites = suites; |
| 110 | this.factory = factory; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates an SSL socket |
| 115 | * |
| 116 | * @param host Host name or IP address of SSL server |
| 117 | * @param port Port numbers of SSL server |
| 118 | * @return A socket for an encrypted session |
| 119 | * @exception LDAPException on error creating socket |
| 120 | */ |
| 121 | public Socket createSocket(String host, int port) |
| 122 | throws LDAPException { |
| 123 | |
| 124 | SSLSocket sock = null; |
| 125 | |
| 126 | try { |
| 127 | sock = (SSLSocket)factory.createSocket(host, port); |
| 128 | |
| 129 | if (suites != null) { |
| 130 | sock.setEnabledCipherSuites(suites); |
| 131 | } |
| 132 | |
| 133 | // Start handshake manually to immediately expose potential |
| 134 | // SSL errors as exceptions. Otherwise, handshake will take |
| 135 | // place first time the data are written to the socket. |
| 136 | sock.startHandshake(); |
| 137 | |
| 138 | } catch (UnknownHostException e) { |
| 139 | throw new LDAPException("SSL connection to " + host + |
| 140 | ":" + port + ", " + e.getMessage(), |
| 141 | LDAPException.CONNECT_ERROR); |
| 142 | } catch (IOException f) { |
| 143 | throw new LDAPException("SSL connection to " + host + |
| 144 | ":" + port + ", " + f.getMessage(), |
| 145 | LDAPException.CONNECT_ERROR); |
| 146 | } |
| 147 | |
| 148 | return sock; |
| 149 | } |
| 150 | } |
| 151 |