4
votes

I write FTPS server, and I have problems with ssl connection after AUTH TLS command. Simple example:

try
{
    int ServerPort = 21;
    ServerSocket FtpExServer = new ServerSocket(ServerPort);
    while(true)
    {
        Socket S = FtpExServer.accept();
        InputStreamReader ISR = new InputStreamReader(S.getInputStream());
        OutputStreamWriter OSW = new OutputStreamWriter(S.getOutputStream());
        BufferedReader ClientSocketReader = new BufferedReader(ISR);
        PrintWriter ClientSocketWriter = new PrintWriter(OSW, true);

        ClientSocketWriter.println("220 Welcome to FTP server.");
        print(ClientSocketReader.readLine());
        ClientSocketWriter.println("234 AUTH TLS successful");

        char[] passphrase = "pass".toCharArray();
        char[] cpassphrase = "cpass".toCharArray();
        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(new FileInputStream("keystore.jks"), passphrase);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(keystore, cpassphrase);
        SSLContext context = SSLContext.getInstance("TLS");
        KeyManager[] keyManagers = kmf.getKeyManagers();
        context.init(keyManagers, null, null);
        SSLServerSocketFactory ssf = context.getServerSocketFactory();

        SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(990);
                ss.setSoTimeout(2000);          
                SSLSocket s = (SSLSocket)ss.accept();

        ISR = new InputStreamReader(s.getInputStream());
        OSW = new OutputStreamWriter(s.getOutputStream());
        ClientSocketReader = new BufferedReader(ISR);
        ClientSocketWriter = new PrintWriter(OSW, true);

        ClientSocketWriter.println("234 AUTH TLS successful");
        print(ClientSocketReader.readLine());
        ClientSocketWriter.println("331 Password required for smie");
        print(ClientSocketReader.readLine());
        ClientSocketWriter.println("230 User smie logged in");
        print(ClientSocketReader.readLine());
        ClientSocketWriter.println("215 UNIX Type: L8");
        print(ClientSocketReader.readLine());
        ClientSocketWriter.println("550 Command not suported.");
    }
}
catch(Exception e)
{
    print(e);
}

Description: FTP client(for example MoveITFreely) connect to server on port 21. After send command "AUTH TLS", server send "234 AUTH TLS successful". Now client must to connect to server on port 990(?), but client dont connect and get timeout exception.

What i do wrong?

1

1 Answers

7
votes

There exist two methods to add SSL to FTP.

First method is called implicit SSL. It means that the server is listening on port 990 and when the client connects to it, first SSL/TLS negotiation is performed, and then the established connection is used as a command channel for communication (for data channel SSL handshake is also performed in a similar manner).

Second method is what you attempt to use. It's called explicit SSL. The client connects on port 21, sends AUTH TLS and starts SSL negotiation on existing connection. Data channel can be secured or not secured depending on how you want it (you specify this using PROT command).

You mixed the methods. I suggest that you read detailed explanation in Wikipedia before going further. Then read RFC for explicit TLS.

Update: Also you'd need SSLClientSocket, not SSLServerSocket.