私はこのSSLと証明書に非常に慣れていません。サーバーが信頼するいくつかの証明書で構成される jks ファイルがあります。jks ファイルからすべての証明書を読み取り、それを getAcceptedIssuers() メソッドに返そうとしています。証明書のタイプは X509Certificate です。私が実装したメソッドは、jks ファイルを適切に読み取り、X509Certificate 証明書の配列リストを作成します。次に、配列リストを配列に変換しようとすると、この例外が発生します
[Ljava.lang.Object; cannot be cast to [Ljava.security.cert.X509Certificate;
at com.sample.ssl.GetCertificates.loadCertificatesFromCompanJks(GetCertificates.java:125)
at com.sample.ssl.GetCertificates$1.getAcceptedIssuers(GetCertificates.java:44)
at sun.security.ssl.AbstractTrustManagerWrapper.checkAlgorithmConstraints(Unknown Source)
at sun.security.ssl.AbstractTrustManagerWrapper.checkAdditionalTrust(Unknown Source)
at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.getSession(Unknown Source)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:91)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:397)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at com.sample.ssl.GetCertificates.postMessage(GetCertificates.java:82)
at com.sample.ssl.GetCertificates.main(GetCertificates.java:138)
私のコードは次のとおりです
public class GetCertificates {
static private TrustManager[] trustmgr = new TrustManager[]{new X509TrustManager() {
private X509Certificate[] certs = null;
public void checkClientTrusted(X509Certificate[] certs, String authType) {
System.out.println("checkClientTrusted");
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
System.out.println("checkServerTrusted");
}
public X509Certificate[] getAcceptedIssuers() {
System.out.println("getAcceptedIssuers");
certs = loadCertificatesFromCompanJks("C:/Users/vinod/Desktop/keystore.jks", "mypassword");
// return new
// X509Certificate[]{};
return certs;
}
}};
public void postMessage() {
try {
// here I prepare Url to execute and make a call
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static X509Certificate[] loadCertificatesFromCompanJks(String jksPath, String keyStorePassword) {
try {
X509Certificate X509Certificate[] = null;
Certificate[] certs = null;
ArrayList<X509Certificate> serverCerts = new ArrayList<X509Certificate>();
FileInputStream is = new FileInputStream(jksPath);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = keyStorePassword;
keystore.load(is, password.toCharArray());
Enumeration e = keystore.aliases();
for (; e.hasMoreElements(); ) {
String alias = (String) e.nextElement();
Certificate cert = keystore.getCertificate(alias);
X509Certificate cert1 = (X509Certificate) cert;
serverCerts.add(cert1);
}
is.close();
System.out.println("Number of server certificates : " + serverCerts.size());
X509Certificate = (java.security.cert.X509Certificate[]) serverCerts.toArray();
return X509Certificate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
new GetCertificates().postMessage();
}
}