import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class EasyX509TrustManager implements X509TrustManager {
X509TrustManager sunJSSEX509TrustManager;
EasyX509TrustManager(KeyStore ks) throws Exception {
// create a "default" JSSE X509TrustManager.
TrustManagerFactory tmf =
TrustManagerFactory.getInstance("SunX509", "SunJSSE");
tmf.init(ks);
TrustManager tms [] = tmf.getTrustManagers();
/*
* Iterate over the returned trustmanagers, look
* for an instance of X509TrustManager. If found,
* use that as our "default" trust manager.
*/
for (int i = 0; i < tms.length; i++) {
if (tms[i] instanceof X509TrustManager) {
sunJSSEX509TrustManager = (X509TrustManager) tms[i];
return;
}
}
/*
* Find some other way to initialize, or else we have to fail the
* constructor.
*/
throw new Exception("Couldn't initialize");
}
/**
* Constructor for EasyX509TrustManager.
*/
/**
* 校验客户端证书日期是否有效
*/
public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
}
/**
* 校验服务端证书日期是否有效
*/
public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
sunJSSEX509TrustManager.checkServerTrusted(certificates, authType);
for (X509Certificate certificate : certificates) {
certificate.checkValidity();
}
}
/**
* @see X509TrustManager#getAcceptedIssuers()
*/
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}