java - 如何在 apache-commons net 中使用生成的证书配置客户端身份验证

标签 java ssl apache-commons-net ftps authentication

首先,我知道有一个类似的问题here但它没有回答我的疑问。 我有一个配置了 SSL 的 FTPS 服务器 (vsftpd)。

我已经生成了适当的 key :

openssl req -x509 -nodes -days 1825 -newkey rsa:2048 \
-keyout private/vsftpd.key \
-out certs/vsftpd.crt

并在服务器中配置相应的conf文件。

现在,在 Apache commons net 的 Java 客户端代码中,我可以通过 SSL 与服务器通信,如下所示:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

public class CommonsNetFTPSTest {
public static void main(String[] args) throws Exception {
    System.setProperty("javax.net.debug", "ssl");

    String server = "XXX.XXX.XXX.XXX";
    String username = "USER_TEST";
    String password = "ABCD1234";
    String remoteFile = "/Data/Input/PH240819";
    String localFile = "PH240819";
    String protocol = "SSL"; // TLS / null (SSL)
    int port = 990;
    int timeoutInMillis = 10000;
    boolean isImpicit = true;

    FTPSClient client = new FTPSClient(protocol, isImpicit);

    client.setDataTimeout(timeoutInMillis);
    client.addProtocolCommandListener(new PrintCommandListener(new  PrintWriter(System.out)));

    System.out.println("################ Connecting to Server ################################");

    try
    {
        int reply;
        System.out.println("################ Connect Call ################################");
        client.connect(server, port);

        client.login(username, password);

        System.out.println("################ Login Success ################################");

        //client.setFileType(FTP.BINARY_FILE_TYPE);
        client.setFileType(FTP.NON_PRINT_TEXT_FORMAT);
        client.execPBSZ(0);  // Set protection buffer size
        client.execPROT("P"); // Set data channel protection to private
        client.enterLocalPassiveMode();

        System.out.println("Connected to " + server + ".");
        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }

        client.listFiles();
        boolean retrieved = client.retrieveFile(remoteFile, new FileOutputStream(localFile));
    }
    catch (Exception e)
    {
        if (client.isConnected())
        {
            try
            {
                client.disconnect();
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        return;
    }
    finally
    {
        //client.disconnect();
        client.logout();
        System.out.println("# client disconnected");
    }
}
}

但我的客户以这种方式接受任何证书,我猜这可能是中间人攻击。所以我想告诉我的客户接受什么证书,并且只在证书有效时连接。

现在,我之前生成了两个文件:vsftpd.key 和 vsftpd.crt,并且我已经将 crt 文件导入到本地 keystore 中,如下所示:

keytool -import -alias alias -file vsftps.crt -keypass keypass -keystore mykeystore.jks -storepass Hello1

我的问题是,如何告诉我的客户使用来自 mykeystore 的证书?我还缺少什么?谢谢

最佳答案

好的,现在我有了。

我从一开始就做错了。首先,您需要将这两个文件(vsftpd.crt 和 vsftpd.key)转换为单个文件 PKCS12文件。

openssl pkcs12 -export -in vsftpd.crt -inkey vsftpd.key > vsftpd.p12

接下来,您需要将 PKCS12 文件导入到 keystore 中:

keytool -importkeystore -srckeystore vsftpd.p12 -destkeystore keystore.jks -srcstoretype pkcs12

详细说明 [此处]。 2

最后,您只需要使用生成的 keystore 实例化一个信任管理器,并将其交给 FTPSClient。像这样的东西:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.commons.net.io.Util;
import org.apache.commons.net.util.TrustManagerUtils;

public method() throws IOException, GeneralSecurityException{

    File storeFile = new File("path/to/keystore");

    KeyStore keyStore = loadStore("JKS", storeFile, "password");
    X509TrustManager defaultTrustManager = TrustManagerUtils.getDefaultTrustManager(keyStore);

    client = new FTPSClient(properties.getProtocol(), isImpicit);

    client.setTrustManager(defaultTrustManager);
    logOutput = new LogOutputStream(log, Level.INFO);
}

//Helper method from apache: http://commons.apache.org/proper/commons-net/apidocs/index.html?org/apache/commons/net/util/KeyManagerUtils.html
private KeyStore loadStore(String storeType, File storePath, String storePass)
        throws KeyStoreException,  IOException, GeneralSecurityException {
        KeyStore ks = KeyStore.getInstance(storeType);
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(storePath);
            ks.load(stream, storePass.toCharArray());
        } finally {
            Util.closeQuietly(stream);
        }
        return ks;
    }

关于java - 如何在 apache-commons net 中使用生成的证书配置客户端身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19173394/

相关文章:

java - 使用 FTPClient 传输 FTP 文件时出现错误

java - Mongodb查询: find if a map stored in mongo contains a specific value

ruby-on-rails - rails : How to start thin server in ssl

apache - Centos7 启用 SSL - 连接超时

java - FTPS 客户端中 listNames() 期间套接字超时

java - 如何将 FTP 服务器上的文件复制到 Java 中同一服务器上的目录?

java - 如何在Excel中导出网页

Java Swing : Cannot Edit JComponents in JWindow Extension

java - 什么是实现类型安全配置重新加载的好方法

ssl - apache httpclient pfx 文件