dcsimg
Sample Secure FTP Client (SSL)
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Henry_Perangelo
Posted On:   Thursday, July 19, 2001 06:12 AM

Does anyone have sample code for a secure ftp client. I'm looking for client code which uses SSL extensions to do secure FTP file transfers. Below is some code I have created but I am running into the following problem: See code (also included) Here is the problem ... C:Henryftptest>java SSLCadreFtp connect-1 connect-2 connect-3 connect-4 connect-5 connect-6 connect-7 connect-8 setMode... setFile... setServerDir... setClientDir... setTransferMode... setClientPort... setServerDirectory... createClientSideFile... RETR... javax.net.ssl.SSLException: No    More>>

Does anyone have sample code for a secure ftp client. I'm looking for client code which uses SSL extensions to do secure FTP file transfers. Below is some code I have created but I am running into the following problem:


See code (also included)




			
Here is the problem ...

C:Henryftptest>java SSLCadreFtp
connect-1
connect-2
connect-3
connect-4
connect-5
connect-6
connect-7
connect-8
setMode...
setFile...
setServerDir...
setClientDir...
setTransferMode...
setClientPort...
setServerDirectory...
createClientSideFile...
RETR...
javax.net.ssl.SSLException: No available certificate corresponds to the SSL cipher suites which are
enabled.
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.a([DashoPro-V1.2-120198])
at com.sun.net.ssl.internal.ssl.SSLServerSocketImpl.accept([DashoPro-V1.2-120198])
at SSLCadreFtp.getFile(SSLCadreFtp.java:245)
at SSLCadreFtp.main(SSLCadreFtp.java:394)

Status: ftp failure
--- ftp control responses ---
showControlReponses...
quitFtp...
bye...


------------------------------
Here is the client code !!!


			

/*
Basic client SSL FTP support
*/

import java.net.*;
import java.io.*;
import java.util.*;
import javax.net.*;
import javax.net.ssl.*;
import java.security.*;
import com.sun.net.ssl.*;

public class SSLCadreFtp{
// socket factories
SSLSocketFactory socketFactory = null;
SSLServerSocketFactory serverSocketFactory = null;

// FTP dual sockets
//java.net.Socket controlSocket = null;
//java.net.ServerSocket dataSocket = null;
SSLSocket controlSocket = null;
SSLServerSocket dataSocket = null;

// Used to communcate over control socket
java.io.BufferedReader controlIn = null;
java.io.PrintWriter controlOut = null;

// Used for receiving files from server
java.io.RandomAccessFile fin = null;
java.io.DataInputStream dis = null;
//java.net.Socket dataOut = null;
SSLSocket dataOut = null;

// Used for sending files to server
java.io.InputStream is = null;
java.io.DataOutputStream dos = null;
//java.net.Socket dataIn = null;
SSLSocket dataIn = null;

String hostName = null;
String userName = null;
String passWord = null;
String transferMode = null;
String transferFile = null;
String serverDirectory = null;
String clientDirectory = null;

public SSLCadreFtp( String hN,
String uN,
String pW){
hostName=hN;userName=uN;passWord=pW;
this.connect();
}

private void connect(){
try{
// Dynamic registration of SunJSSE provider
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.out.println("connect-1");

// Create a socket factories
socketFactory=(SSLSocketFactory) SSLSocketFactory.getDefault();
serverSocketFactory=(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
System.out.println("connect-2");

//ftp has a dual-socket protocol
//controlSocket=socketFactory.createSocket(hostName,21);
controlSocket=(SSLSocket)socketFactory.createSocket(hostName,990);
controlSocket.setSoTimeout(1000);
System.out.println("connect-3");

//dataSocket= new java.net.ServerSocket(0);
dataSocket=(SSLServerSocket)serverSocketFactory.createServerSocket(0);
dataSocket.setSoTimeout(1000);
System.out.println("connect-4");

// the control channel (messages to ftp server)
controlOut= (new java.io.PrintWriter(
controlSocket.getOutputStream(),true)
);
System.out.println("connect-5");

// the control channel (messages from ftp server)
controlIn= (new java.io.BufferedReader(
new java.io.InputStreamReader(
controlSocket.getInputStream())
)
);
System.out.println("connect-6");

// Login to the server
controlOut.println("USER " +userName);
controlOut.flush();
System.out.println("connect-7");

controlOut.println("PASS " +passWord);
controlOut.flush();
System.out.println("connect-8");

//controlOut.println("SSL AUTH");
//controlOut.flush();
//System.out.println("connect-9");

//controlOut.println("PROT P");
//controlOut.flush();
//System.out.println("connect-10");
}
catch(java.net.UnknownHostException e){
e.printStackTrace();
System.exit(0);
}
catch (java.io.IOException e){
e.printStackTrace();
System.exit(0);
}
catch (Exception e){
e.printStackTrace();
System.exit(0);
}
}

public void setMode(String tM){
transferMode=tM;
System.out.println("setMode...");
}

public void setFile(String f){
transferFile=f;
System.out.println("setFile...");
}

public void setServerDir(String sD){
serverDirectory=sD;
System.out.println("setServerDir...");
}

public void setClientDir(String cD){
clientDirectory=cD;
System.out.println("setClientDir...");
}

private void setTransferMode(){
// Set type of transfer ASCII or BINARY
if((transferMode.equals("B"))||(transferMode.equals("b"))){
//controlOut.println("TYPE L 32"); //BINARY {to unix}
//controlOut.println("TYPE L 8"); //BINARY {to pc}
controlOut.println("TYPE I"); //BINARY
}
else{
//controlOut.println("TYPE A N"); //ASCII
controlOut.println("TYPE A"); //ASCII
}
controlOut.flush();
System.out.println("setTransferMode...");
}

private void setClientPort(){
// Determine which port to send/get data to/from
// must put port info into the ftp RFC 959 format
// PORT h1,h2,h3,h4,p1,p2
int clientPort = dataSocket.getLocalPort();
int p1 = ((clientPort & 0xff00) >> 8);
int p2 = (clientPort & 0x00ff);
String port_command="PORT " +
controlSocket.getLocalAddress().getHostAddress().replace('.',',')+ //h1,h2,h3,h4
"," + p1 + "," + p2; //p1,p2
controlOut.println(port_command);
controlOut.flush();
System.out.println("setClientPort...");
}


private void setServerDirectory(){
// go to proper directory on server side
if (serverDirectory != null){
controlOut.println("CWD " + serverDirectory);
controlOut.flush();
}
System.out.println("setServerDirectory...");
}

private void createClientSideFile(){
// create file structure (on client-side) of the file you will retrieve
// we will write to this file on the client local disk
try{
if(clientDirectory != null)
fin=new java.io.RandomAccessFile((clientDirectory+transferFile),"rw");
else
fin=new java.io.RandomAccessFile((transferFile),"rw");
}
catch(java.io.FileNotFoundException e){
e.printStackTrace();
System.exit(0);
}
catch(java.io.IOException e){
e.printStackTrace();
System.exit(0);
}
catch (Exception e){
e.printStackTrace();
System.exit(0);
}
System.out.println("createClientSideFile...");
}

private void setClientSideFile(){
// create file structure (on client-side) of the file you will retrieve
// we will write to this file on the client local disk
try{
// do put logic
if(clientDirectory != null)
is = new java.io.FileInputStream(clientDirectory+transferFile);
else
is = new java.io.FileInputStream(transferFile);
}
catch(java.io.FileNotFoundException e){
e.printStackTrace();
System.exit(0);
}
catch(java.io.IOException e){
e.printStackTrace();
System.exit(0);
}
catch (Exception e){
e.printStackTrace();
System.exit(0);
}
System.out.println("setClientSideFile...");
}

public String getFile(){
try {
this.setTransferMode();
this.setClientPort();
this.setServerDirectory();
this.createClientSideFile();

// request (from server) to retrive a file
controlOut.println("RETR " +transferFile);
controlOut.flush();
System.out.println("RETR...");


// retrive the file from the data socket
dataIn=(SSLSocket)dataSocket.accept();
dis =new java.io.DataInputStream(dataIn.getInputStream());

// read info from dataSocket & write out to local disk
int offset;
byte[] data = new byte[1024];
while((offset=dis.read(data))!=-1){
fin.write(data, 0, offset);
}
System.out.println("RETR the file complete...");
fin.close();
dis.close();
dataIn.close();

//this.showControlReponses();
}
catch(java.net.UnknownHostException e){
e.printStackTrace();
return ("ftp failure");
}
catch (java.io.IOException e){
e.printStackTrace();
return ("ftp failure");
}
catch (Exception e){
e.printStackTrace();
return ("ftp failure");
}
return ("ftp success");
}

public String putFile(){
try {
this.setTransferMode();
this.setClientPort();
this.setServerDirectory();
this.setClientSideFile();

// request (from server) to retrive a file
controlOut.println("STOR " +transferFile);
controlOut.flush();
System.out.println("STOR...");

// store the file to the data socket
dataOut=(SSLSocket)dataSocket.accept();
dos = new java.io.DataOutputStream(dataOut.getOutputStream());

// write info from local disk & write out to dataSocket
int offset;
byte[] data = new byte[1024];
while((offset=is.read(data))!=-1){
dos.write(data, 0, offset);
}
System.out.println("STOR the file complete...");
is.close();
dos.close();
dataOut.close();

//this.showControlReponses();
}
catch(java.net.UnknownHostException e){
e.printStackTrace();
return ("ftp failure");
}
catch (java.io.IOException e){
e.printStackTrace();
return ("ftp failure");
}
catch (Exception e){
e.printStackTrace();
return ("ftp failure");
}
return ("ftp success");
}

public void bye(){
quitFtp();
System.out.println("bye...");
}

private void quitFtp(){
try {
// quit the ftp session
controlOut.println("QUIT");
controlOut.flush();

// Get a history of all processing
this.showControlReponses();

controlOut.close();
controlIn.close();
controlSocket.close();
dataSocket.close();
}
catch (java.io.IOException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
System.out.println("quitFtp...");
}

private void showControlReponses(){
try {
// Display history of the ftp servers responses
// to our commands
System.out.println("--- ftp control responses ---");
String inputLine_client;
while ((inputLine_client=controlIn.readLine())!=null)
System.out.println(inputLine_client);
}
catch (java.io.IOException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
System.out.println("showControlReponses...");
}

public static void main(String arg[]){
SSLCadreFtp cftp=null;

/*// Example FTP get
cftp= new SSLCadreFtp("aptest","test","test");
cftp.setMode("a");
cftp.setFile("DateUtil.java");
//cftp.setServerDir("/db/java/henry");
cftp.setClientDir("C:\Henry\ftptest\");
System.out.println("
Status: "+cftp.getFile());
cftp.bye();

// Example FTP put
cftp= new SSLCadreFtp("aptest","test","test");
cftp.setMode("a");
cftp.setFile("PutFtp.java");
//cftp.setServerDir("/db/java/henry");
cftp.setClientDir("C:\Henry\ftptest\");
System.out.println("
Status: "+cftp.putFile());
cftp.bye();
*/

// Example FTP get
cftp= new SSLCadreFtp("aptest","test","test");
cftp.setMode("b");
cftp.setFile("RegistryElement.java");
cftp.setServerDir("/db/java/henry");
cftp.setClientDir("C:\Henry\ftptest\");
System.out.println("
Status: "+cftp.getFile());
cftp.bye();

/*
Example FTP put
cftp = new SSLCadreFtp("aptest","test","test");
cftp.setMode("a");
cftp.setFile("PutFtp.java");
cftp.setServerDir("/db/java/henry");
cftp.setClientDir("C:\Henry\ftptest\");
System.out.println("
Status: "+cftp.putFile());
cftp.bye();
*/
}
}

--------------------




I have a similar client that does non-
secure ftp & it works fine. I think the
problem I'm having had to do with
security configuration issues. Feel free to play/use the code. If you find
the answer please post it!




The working non-secure (non-SSL)ftp code is included below. Hope you find use for it ...

			
/*
Basic client FTP support
*/

import java.net.*;
import java.io.*;

public class CadreFtp{
// FTP dual sockets
java.net.Socket controlSocket = null;
java.net.ServerSocket dataSocket = null;

// Used to communicate over control socket
java.io.BufferedReader controlIn = null;
java.io.PrintWriter controlOut = null;

// Used for receiving files from server
java.io.RandomAccessFile fin = null;
java.io.DataInputStream dis = null;
java.net.Socket dataOut = null;

// Used for sending files to server
java.io.InputStream is = null;
java.io.DataOutputStream dos = null;
java.net.Socket dataIn = null;

String hostName = null;
String userName = null;
String passWord = null;
String transferMode = null;
String transferFile = null;
String serverDirectory = null;
String clientDirectory = null;

public CadreFtp( String hN,
String uN,
String pW){
hostName=hN;userName=uN;passWord=pW;
this.connect();
}

private void connect(){
try{
//ftp has a dual-socket protocol
controlSocket = new java.net.Socket(hostName,21);
controlSocket.setSoTimeout(1000);
dataSocket = new java.net.ServerSocket(0);
dataSocket.setSoTimeout(1000);

// the control channel (messages to ftp server)
controlOut= (new java.io.PrintWriter(
controlSocket.getOutputStream(),true)
);

// the control channel (messages from ftp server)
controlIn= (new java.io.BufferedReader(
new java.io.InputStreamReader(
controlSocket.getInputStream())
)
);

// Login to the server
controlOut.println("USER " +userName);
controlOut.flush();

controlOut.println("PASS " +passWord);
controlOut.flush();
}
catch(java.net.UnknownHostException e){
e.printStackTrace();
System.exit(0);
}
catch (java.io.IOException e){
e.printStackTrace();
System.exit(0);
}
}

public void setMode(String tM){
transferMode=tM;
}

public void setFile(String f){
transferFile=f;
}

public void setServerDir(String sD){
serverDirectory=sD;
}

public void setClientDir(String cD){
clientDirectory=cD;
}

private void setTransferMode(){
// Set type of transfer ASCII or BINARY
if((transferMode.equals("B"))||(transferMode.equals("b"))){
//controlOut.println("TYPE L 32"); //BINARY {to unix}
//controlOut.println("TYPE L 8"); //BINARY {to pc}
controlOut.println("TYPE I"); //BINARY
}
else{
//controlOut.println("TYPE A N"); //ASCII
controlOut.println("TYPE A"); //ASCII
}
controlOut.flush();
}

private void setClientPort(){
// Determine which port to send/get data to/from
// must put port info into the ftp RFC 959 format
// PORT h1,h2,h3,h4,p1,p2
int clientPort = dataSocket.getLocalPort();
int p1 = ((clientPort & 0xff00) >> 8);
int p2 = (clientPort & 0x00ff);
String port_command="PORT " +
controlSocket.getLocalAddress().getHostAddress().replace('.',',')+ //h1,h2,h3,h4
"," + p1 + "," + p2; //p1,p2
controlOut.println(port_command);
controlOut.flush();
}


private void setServerDirectory(){
// go to proper directory on server side
if (serverDirectory != null){
controlOut.println("CWD " + serverDirectory);
controlOut.flush();
}
}

private void createClientSideFile(){
// create file structure (on client-side) of the file you will retrieve
// we will write to this file on the client local disk
try{
if(clientDirectory != null)
fin=new java.io.RandomAccessFile((clientDirectory+transferFile),"rw");
else
fin=new java.io.RandomAccessFile((transferFile),"rw");
}
catch(java.io.FileNotFoundException e){
e.printStackTrace();
System.exit(0);
}
catch(java.io.IOException e){
e.printStackTrace();
System.exit(0);
}
}

private void setClientSideFile(){
// create file structure (on client-side) of the file you will retrieve
// we will write to this file on the client local disk
try{
// do put logic
if(clientDirectory != null)
is = new java.io.FileInputStream(clientDirectory+transferFile);
else
is = new java.io.FileInputStream(transferFile);
}
catch(java.io.FileNotFoundException e){
e.printStackTrace();
System.exit(0);
}
catch(java.io.IOException e){
e.printStackTrace();
System.exit(0);
}
}

public String getFile(){
try {
this.setTransferMode();
this.setClientPort();
this.setServerDirectory();
this.createClientSideFile();

// request (from server) to retrive a file
controlOut.println("RETR " +transferFile);
controlOut.flush();

// retrive the file from the data socket
dataIn=dataSocket.accept();
dis =new java.io.DataInputStream(dataIn.getInputStream());

// read info from dataSocket & write out to local disk
int offset;
byte[] data = new byte[1024];
while((offset=dis.read(data))!=-1){
fin.write(data, 0, offset);
}
fin.close();
dis.close();
dataIn.close();

//this.showControlReponses();
}
catch(java.net.UnknownHostException e){
return ("ftp failure");
}
catch (java.io.IOException e){
return ("ftp failure");
}
return ("ftp success");
}

public String putFile(){
try {
this.setTransferMode();
this.setClientPort();
this.setServerDirectory();
this.setClientSideFile();

// request (from server) to retrive a file
controlOut.println("STOR " +transferFile);
controlOut.flush();

// store the file to the data socket
dataOut=dataSocket.accept();
dos = new java.io.DataOutputStream(dataOut.getOutputStream());

// write info from local disk & write out to dataSocket
int offset;
byte[] data = new byte[1024];
while((offset=is.read(data))!=-1){
dos.write(data, 0, offset);
}
is.close();
dos.close();
dataOut.close();

//this.showControlReponses();
}
catch(java.net.UnknownHostException e){
e.printStackTrace();
return ("ftp failure");
}
catch (java.io.IOException e){
e.printStackTrace();
return ("ftp failure");
}
catch (Exception e){
e.printStackTrace();
return ("ftp failure");
}
return ("ftp success");
}

public void bye(){
quitFtp();
}

private void quitFtp(){
try {
// quit the ftp session
controlOut.println("QUIT");
controlOut.flush();

// Get a history of all processing
this.showControlReponses();

controlOut.close();
controlIn.close();
controlSocket.close();
dataSocket.close();
}
catch (java.io.IOException e){
e.printStackTrace();
}
}

private void showControlReponses(){
try {
// Display history of the ftp servers responses
// to our commands
System.out.println("--- ftp control responses ---");
String inputLine_client;
while ((inputLine_client=controlIn.readLine())!=null)
System.out.println(inputLine_client);
}
catch (java.io.IOException e){
e.printStackTrace();
}
}

public static void main(String arg[]){
CadreFtp cftp=null;

// Example FTP get
cftp= new CadreFtp("aptest","test","test");
cftp.setMode("a");
cftp.setFile("DateUtil.java");
//cftp.setServerDir("/db/java/henry");
cftp.setClientDir("C:\Henry\ftptest\");
System.out.println("
Status: "+cftp.getFile());
cftp.bye();

// Example FTP put
cftp= new CadreFtp("aptest","test","test");
cftp.setMode("a");
cftp.setFile("PutFtp.java");
//cftp.setServerDir("/db/java/henry");
cftp.setClientDir("C:\Henry\ftptest\");
System.out.println("
Status: "+cftp.putFile());
cftp.bye();

//// Example FTP get
//cftp= new CadreFtp("aptest","test","test");
//cftp.setMode("a");
//cftp.setFile("RegistryElement.java");
//cftp.setServerDir("/db/java/henry");
//cftp.setClientDir("C:\Henry\ftptest\");
//System.out.println("
Status: "+cftp.getFile());
//cftp.bye();

//// Example FTP put
//cftp = new CadreFtp("aptest","test","test");
//cftp.setMode("a");
//cftp.setFile("PutFtp.java");
//cftp.setServerDir("/db/java/henry");
//cftp.setClientDir("C:\Henry\ftptest\");
//System.out.println("
Status: "+cftp.putFile());
//cftp.bye();
}
}

--------------------




Thanks,
-Henry-    <<Less
About | Sitemap | Contact