import java.security.cert.X509Certificate;
public class ForcedX509TrustManager implements com.sun.net.ssl.X509TrustManager {
private X509Certificate chain;
private String authType;
/**
* 信頼されない証明書でも強制的に認証する(クライアント認証)
* @param chain 証明書チェーン配列
* @return true=認証OK
*/
public boolean isClientTrusted(X509Certificate chain){
return true;
}
/**
* 信頼されない証明書でも強制的に認証する
* @param chain 証明書チェーン配列
* @return true=認証OK
*/
public boolean isServerTrusted(X509Certificate chain){
return true;
}
/**
* 本当は証明書を返す
* @return 証明書
*/
public java.security.cert.X509Certificate getAcceptedIssuers() {
return null;
}
/**
* ピアから提出された一部のまたは完全な証明書チェーンを使用して、
* 信頼できるルートへの証明書パスを構築し、認証タイプに基づいて
* クライアント SSL 認証を検証できるかどうか信頼できるかどうかを返します。
* @param chain ピアの証明書チェーン
* @param authType クライアント証明書に基づいた認証タイプ
*/
public void checkClientTrusted(X509Certificate chain,String authType){
this.chain = chain;
this.authType = authType;
}
/**
* ピアから提出された一部のまたは完全な証明書チェーンを使用して、
* 信頼できるルートへの証明書パスを構築し、認証タイプに基づいて
* サーバ SSL 認証を検証できるかどうか信頼できるかどうかを返します。
* @param chain ピアの証明書チェーン
* @param authType 使用される鍵交換アルゴリズム
*/
public void checkServerTrusted(X509Certificate chain,String authType){
this.chain = chain;
this.authType = authType;
}
public String getAuthType(){
return this.authType;
}
public X509Certificate getChain(){
return this.chain;
}
}
========
import java.io.IOException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import com.sun.net.ssl.HttpsURLConnection;
public class ForcedURLConnect {
public ForcedURLConnect(){
}
/**
* HttpsURLConnection取得.
* 取得したHttpsURLConnectionに対して、setDoOutput(true)を実行することで、
* 以下のように要求を送る。(POSTの場合)
* ForcedURLConnect forceUrlCon = new ForcedURLConnect();
* HttpsURLConnection hcon = forceUrlCon.getHttpsURLConnection(urlstrings);
* hcon.setDoOutput(true);
* PrintWriter pout = new PrintWriter(hcon.getOutputStream());
* @param urlstr 参照したURL文字列="https://*"
* @return コネクション
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public HttpsURLConnection getHttpsURLConnection(String urlstr)
throws IOException, NoSuchAlgorithmException, KeyManagementException{
// SunJSSE Providerを指定
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//HTTPS プロトコルハンドラをロード
System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
URL url = new URL(urlstr);
HttpsURLConnection hCon = (HttpsURLConnection)url.openConnection();
javax.net.ssl.SSLSocketFactory sslsf=null;
com.sun.net.ssl.KeyManager km=null;
//証明書の信頼性を決定するためのインターフェース
com.sun.net.ssl.TrustManager[] tm = { new ForcedX509TrustManager() };
//ソケットプロトコルを実装するSSLContextを作成
com.sun.net.ssl.SSLContext sslContext = com.sun.net.ssl.SSLContext.getInstance("SSL");
//SSLContextを初期化
sslContext.init(km,tm,new java.security.SecureRandom());
//SSLContextのSocketFactoryを取得
sslsf = sslContext.getSocketFactory();
//URLConnectionにSocketFactoryをセット
hCon.setSSLSocketFactory(sslsf);
//ホスト名を無視させる
com.sun.net.ssl.HostnameVerifier hv = new com.sun.net.ssl.HostnameVerifier(){
public boolean verify(String hostname, String certHostname){
return true;
}
};
hCon.setHostnameVerifier(hv);
return hCon;
}
}