我写了FTPS服务器,我在AUTH TLS命令后遇到ssl连接问题。 简单的例子:
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);
}
描述:FTP客户端(例如MoveITFreely)连接到端口21上的服务器。发送命令“AUTH TLS”后,服务器发送“234 AUTH TLS成功”。现在客户端必须连接到端口990(?)上的服务器,但客户端不连接并获得超时异常。
我做错了什么?
答案 0 :(得分:7)
有两种方法可以将SSL添加到FTP。
第一种方法称为隐式SSL。这意味着服务器正在侦听端口990,当客户端连接到它时,首先执行SSL / TLS协商,然后将建立的连接用作通信的命令通道(对于数据通道,SSL握手也在类似的方式)。
第二种方法是您尝试使用的方法。它被称为显式SSL。客户端在端口21上连接,发送AUTH TLS并在现有连接上启动SSL协商。根据您的需要,可以保护数据通道或不保护数据通道(使用PROT命令指定)。
你混合了这些方法。我建议您在继续之前阅读detailed explanation in Wikipedia。然后阅读RFC for explicit TLS。
更新:你还需要SSLClientSocket,而不是SSLServerSocket。