【问题标题】:How to retrieve the KeyManager, TrustManager, and SecureRandom object being used by the SSL Client?如何检索 SSL 客户端正在使用的 KeyManager、TrustManager 和 SecureRandom 对象?
【发布时间】:2016-01-08 15:30:57
【问题描述】:

我正在尝试创建一个使用 TSLv1.2 协议执行握手的 SSL 套接字工厂。

到目前为止我所拥有的[1/18 更新]

SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
ServerSocketFactory sf = SSLServerSocketFactory.getDefault();
KeyManager[] km = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()).getKeyManagers();
TrustManager[] tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).getTrustManagers();
SecureRandom random = new SecureRandom();
sslContext.init(km, tm, random);
requestContext.put(BindingProviderProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory());

我希望从SSLServerSocketFactory.getDefault() 中获取 KeyManager、TrustManager 和 SecureRandom 对象,但没有 getter。
我可以从另一个地方拉这个吗?还是更有效的方法?

我不想手动创建密钥和信任管理器以避免需要系统特定的配置。

完整方法供参考:

    public MyOutBoundClientWSImpl(URL wsdlUrl){
        super(wsdlUrl, serviceName);
        this.wsUrl=wsdlUrl;
        this.mService = this.getMySoapHttpPort();
        Map<String, Object> requestContext = ((BindingProvider)mService).getRequestContext();
        requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, REQUEST_TIMEOUT); // Timeout in millis
        requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT); // Timeout in millis
        try {
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            ServerSocketFactory sf = SSLServerSocketFactory.getDefault();
            KeyManager[] km = ??;
            TrustManager[] tm = ??;
            SecureRandom random = ??;
            sslContext.init(km, tm, random);
            requestContext.put(BindingProviderProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

【问题讨论】:

  • 手动创建它们不会使您的代码特定于系统。不清楚您的问题是什么。
  • 我想使用默认使用的任何东西。另外,我不知道密钥库密码,也不确定是否可以得到它。此外,如果我在两个不同的环境中使用密码或有不同的密钥库,我将需要不同的配置来设置它。这就是我所说的系统特定的意思。

标签: java ssl tls1.2 sslsocketfactory


【解决方案1】:

查看官方文档:

https://docs.oracle.com/javase/7/docs/api/javax/net/ssl/SSLContext.html

public final void init(KeyManager[] km,
        TrustManager[] tm,
        SecureRandom random)
                throws KeyManagementException

初始化这个上下文。前两个参数中的任何一个都可以是 null 在这种情况下,将搜索已安装的安全提供程序 为相应工厂的最高优先级实施。 同样,安全随机参数可能为空,在这种情况下 将使用默认实现。

事实证明这很容易做到,您只需将所有空值传递给 init 方法。

    public MyOutBoundClientWSImpl(URL wsdlUrl){
        super(wsdlUrl, serviceName);
        this.wsUrl=wsdlUrl;
        this.mService = this.getMySoapHttpPort();
        Map<String, Object> requestContext = ((BindingProvider)mService).getRequestContext();
        requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, REQUEST_TIMEOUT); // Timeout in millis
        requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT); // Timeout in millis
        try {
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            KeyManager[] km = null;
            TrustManager[] tm = null;
            SecureRandom random = null;                
            sslContext.init(km, tm, random);
            requestContext.put(BindingProviderProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多