commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwins...@apache.org
Subject svn commit: r436949 - in /jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp: FTPSClient.java FTPSSocketFactory.java FTPSTrustManager.java
Date Fri, 25 Aug 2006 21:05:56 GMT
Author: rwinston
Date: Fri Aug 25 14:05:56 2006
New Revision: 436949

URL: http://svn.apache.org/viewvc?rev=436949&view=rev
Log:
Add initial FTPS classes

Added:
    jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSClient.java
    jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSSocketFactory.java
    jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSTrustManager.java

Added: jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSClient.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSClient.java?rev=436949&view=auto
==============================================================================
--- jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSClient.java
(added)
+++ jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSClient.java
Fri Aug 25 14:05:56 2006
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2001-2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.net.ftp;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketException;
+import java.security.KeyStore;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.TrustManager;
+
+public class FTPSClient extends FTPClient
+{
+	private static final String PASSWORD = "password";
+	
+	private SSLContext context;
+	
+	public FTPSClient()
+	{
+		try
+		{
+			KeyStore keyStore = KeyStore.getInstance("JCEKS");
+			keyStore.load(null, PASSWORD.toCharArray());
+
+			KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+			keyManagerFactory.init(keyStore, PASSWORD.toCharArray());
+
+			this.context = SSLContext.getInstance("TLS");
+			this.context.init(keyManagerFactory.getKeyManagers(), new TrustManager[] { new FTPSTrustManager()
}, null);
+		}
+		catch (Exception e)
+		{
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * @see org.apache.commons.net.SocketClient#connect(java.net.InetAddress, int, java.net.InetAddress,
int)
+	 */
+	public void connect(InetAddress address, int port, InetAddress localAddress, int localPort)
throws SocketException, IOException
+	{
+		super.connect(address, port, localAddress, localPort);
+		
+		this.secure();
+	}
+
+	/**
+	 * @see org.apache.commons.net.SocketClient#connect(java.net.InetAddress, int)
+	 */
+	public void connect(InetAddress address, int port) throws SocketException, IOException
+	{
+		super.connect(address, port);
+		
+		this.secure();
+	}
+
+	/**
+	 * @see org.apache.commons.net.SocketClient#connect(java.lang.String, int, java.net.InetAddress,
int)
+	 */
+	public void connect(String address, int port, InetAddress localAddress, int localPort) throws
SocketException, IOException
+	{
+		super.connect(address, port, localAddress, localPort);
+		
+		this.secure();
+	}
+
+	/**
+	 * @see org.apache.commons.net.SocketClient#connect(java.lang.String, int)
+	 */
+	public void connect(String address, int port) throws SocketException, IOException
+	{
+		super.connect(address, port);
+		
+		this.secure();
+	}
+	
+	public void secure() throws IOException
+	{
+		this.sendCommand("AUTH", "TLS");
+		
+		SSLSocket socket = (SSLSocket) this.context.getSocketFactory().createSocket(this._socket_,
this.getRemoteAddress().getHostAddress(), this.getRemotePort(), true);
+		
+		socket.startHandshake();
+		
+		this._socket_ = socket;
+		this._controlInput_ = new BufferedReader(new InputStreamReader(socket.getInputStream(),
getControlEncoding()));
+		this._controlOutput_ = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),
getControlEncoding()));
+
+		this.setSocketFactory(new FTPSSocketFactory(this.context));
+		
+		this.sendCommand("PBSZ", "0");
+		this.sendCommand("PROT", "P");
+	}
+
+	/**
+	 * @see org.apache.commons.net.ftp.FTPClient#_openDataConnection_(int, java.lang.String)
+	 */
+	protected Socket _openDataConnection_(int command, String arg) throws IOException
+	{
+		Socket socket = super._openDataConnection_(command, arg);
+		
+		if (socket != null)
+		{
+			((SSLSocket) socket).startHandshake();
+		}
+		
+		return socket;
+	}
+}

Added: jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSSocketFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSSocketFactory.java?rev=436949&view=auto
==============================================================================
--- jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSSocketFactory.java
(added)
+++ jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSSocketFactory.java
Fri Aug 25 14:05:56 2006
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2001-2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.net.ftp;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLServerSocket;
+import javax.net.ssl.SSLSocket;
+
+import org.apache.commons.net.SocketFactory;
+
+public class FTPSSocketFactory implements SocketFactory
+{
+	private SSLContext context;
+	
+	public FTPSSocketFactory(SSLContext context)
+	{
+		this.context = context;
+	}
+	
+	public Socket createSocket(String address, int port) throws UnknownHostException, IOException
+	{
+		return this.init(this.context.getSocketFactory().createSocket(address, port));
+	}
+
+	public Socket createSocket(InetAddress address, int port) throws IOException
+	{
+		return this.init(this.context.getSocketFactory().createSocket(address, port));
+	}
+
+	public Socket createSocket(String address, int port, InetAddress localAddress, int localPort)
throws UnknownHostException, IOException
+	{
+		return this.init(this.context.getSocketFactory().createSocket(address, port, localAddress,
localPort));
+	}
+
+	public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int
localPort) throws IOException
+	{
+		return this.init(this.context.getSocketFactory().createSocket(address, port, localAddress,
localPort));
+	}
+	
+	public ServerSocket createServerSocket(int port) throws IOException
+	{
+		return this.init(this.context.getServerSocketFactory().createServerSocket(port));
+	}
+
+	public ServerSocket createServerSocket(int port, int backlog) throws IOException
+	{
+		return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
+	}
+
+	public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws
IOException
+	{
+		return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog,
ifAddress));
+	}
+	
+	public Socket init(Socket socket) throws IOException
+	{
+		((SSLSocket) socket).startHandshake();
+		
+		return socket;
+	}
+	
+	public ServerSocket init(ServerSocket socket) throws IOException
+	{
+		((SSLServerSocket) socket).setUseClientMode(true);
+		
+		return socket;
+	}
+}

Added: jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSTrustManager.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSTrustManager.java?rev=436949&view=auto
==============================================================================
--- jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSTrustManager.java
(added)
+++ jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/java/org/apache/commons/net/ftp/FTPSTrustManager.java
Fri Aug 25 14:05:56 2006
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2001-2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.net.ftp;
+
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.X509TrustManager;
+
+public class FTPSTrustManager implements X509TrustManager
+{
+	public void checkClientTrusted(X509Certificate[] certificates, String authType)
+	{
+		return;
+	}
+
+	public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException
+	{
+		for (int i = 0; i < certificates.length; ++i)
+		{
+			certificates[i].checkValidity();
+		}
+	}
+
+	public X509Certificate[] getAcceptedIssuers()
+	{
+		return null;
+	}
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message