实现X509TrustManager - 将部分验证传递给现有验证程序

时间:2013-09-25 12:45:46

标签: java security ssl x509certificate pki

我需要忽略PKIX路径构建异常

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExc
ption: unable to find valid certification path to requested target

我知道如何通过编写自己的实现X509TrustManager的类来实现此目的,我始终return true来自isServerTrusted

但是,我不想信任所有服务器和所有客户。

  • 我希望像目前一样为客户完成所有默认验证。
  • 对于服务器,我想忽略仅针对一个特定证书的服务器证书验证,但希望继续进行验证,如当前所做的那样(例如,使用cacerts商店)。

我怎样才能实现这样的目标 - 即在我更换之前将部分验证传递给X509TrustFactory对象。

即。这就是我想要做的事情

public boolean isServerTrusted(X509Certificate[] chain)
{
    if(chain[0].getIssuerDN().getName().equals("MyTrustedServer") && chain[0].getSubjectDN().getName().equals("MyTrustedServer"))
        return true;

    // else I want to do whatever verification is normally done
}

此外,我不想打扰现有的isClientTrusted验证。

我该怎么做?

2 个答案:

答案 0 :(得分:37)

你可以抓住现有的默认信任管理器并使用以下内容将其包装起来:

TrustManagerFactory tmf = TrustManagerFactory
        .getInstance(TrustManagerFactory.getDefaultAlgorithm());
// Using null here initialises the TMF with the default trust store.
tmf.init((KeyStore) null);

// Get hold of the default trust manager
X509TrustManager x509Tm = null;
for (TrustManager tm : tmf.getTrustManagers()) {
    if (tm instanceof X509TrustManager) {
        x509Tm = (X509TrustManager) tm;
        break;
    }
}

// Wrap it in your own class.
final X509TrustManager finalTm = x509Tm;
X509TrustManager customTm = new X509TrustManager() {
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return finalTm.getAcceptedIssuers();
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain,
            String authType) throws CertificateException {
        finalTm.checkServerTrusted(chain, authType);
    }

    @Override
    public void checkClientTrusted(X509Certificate[] chain,
            String authType) throws CertificateException {
        finalTm.checkClientTrusted(chain, authType);
    }
};

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { customTm }, null);

// You don't have to set this as the default context,
// it depends on the library you're using.
SSLContext.setDefault(sslContext);

然后,您可以围绕finalTm.checkServerTrusted(chain, authType);实现自己的逻辑。

但是,您应该确保对要忽略的特定证书进行例外处理。

您在下面所做的是让任何证书与这些颁发者DN和主题DN(这不难伪造):

if(chain[0].getIssuerDN().getName().equals("MyTrustedServer") && chain[0].getSubjectDN().getName().equals("MyTrustedServer"))
    return true;

您可以从已知引用中加载X509Certificate实例,并比较链中的实际值。

此外,checkClientTrustedcheckServerTrusted不是返回truefalse的方法,而是默认情况下会以静默方式成功的void方法。如果您希望的证书出现问题,请明确抛出CertificateException

答案 1 :(得分:1)

您可以从有问题的特定证书创建信任管理器,而不是实施X509TrustManager来信任任何证书。从.p12.jks密钥库或.crt文件加载证书(您可以在Chrome中通过单击挂锁并选择证书,将浏览器中的证书复制到文件中) 。该代码比实现自己的X509TrustManager短:

private static SSLSocketFactory createSSLSocketFactory(File crtFile) throws GeneralSecurityException, IOException {
    SSLContext sslContext = SSLContext.getInstance("SSL");

    // Create a new trust store, use getDefaultType for .jks files or "pkcs12" for .p12 files
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    // You can supply a FileInputStream to a .jks or .p12 file and the keystore password as an alternative to loading the crt file
    trustStore.load(null, null);

    // Read the certificate from disk
    X509Certificate result;
    try (InputStream input = new FileInputStream(crtFile)) {
        result = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(input);
    }
    // Add it to the trust store
    trustStore.setCertificateEntry(crtFile.getName(), result);

    // Convert the trust store to trust managers
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();

    sslContext.init(null, trustManagers, null);
    return sslContext.getSocketFactory();
}

您可以通过调用HttpsURLConnection.setSSLSocketFactory(createSSLSocketFactory(crtFile))来使用它(不过,您可能想一次初始化套接字工厂并重用它)。