9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javaでhttps接続確認する

Last updated at Posted at 2020-01-03

https接続確認をブラウザからではなく、Javaから行います。
環境はJava8を利用しています。他にはライブラリは利用していません。

ソース

package ssltest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.cert.Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;

public class Connect {

	public static void main(String[] args) {
		try {
			URL url = new URL("https://google.co.jp/");
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.connect();
			dispEnv();
			dispContents(url);
			dispCerts(conn);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void dispEnv() {
		try {
			System.out.print("オペレーティングシステム名(os.name):");
			System.out.println(System.getProperty("os.name"));
			System.out.print("Javaバージョン(java.version):");
			System.out.println(System.getProperty("java.version"));

			InetAddress ia = InetAddress.getLocalHost();
			String ip = ia.getHostAddress();
			String hostname = ia.getHostName();
			System.out.println("IPアドレス(getLocalHost):" + ip);
			System.out.println("ホスト名(hostName):" + hostname);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public static void dispCerts(HttpsURLConnection conn) {
		System.out.println("----サーバの証明書チェーンを表示----");
		try {
			Certificate[] certs = conn.getServerCertificates();
			for (Certificate cert : certs) {
				System.out.println(cert);
			}
		} catch (SSLPeerUnverifiedException e) {
			e.printStackTrace();
		}
	}

	public static void dispContents(URL url) {
		System.out.println("----コンテンツを表示----");
		try {
			InputStream strm = url.openStream();
			InputStreamReader in = new InputStreamReader(strm);
			BufferedReader inb = new BufferedReader(in);
			String line;
			while ((line = inb.readLine()) != null) {
				System.out.println(line);
			}
			inb.close();
			in.close();
			strm.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

実行結果

一部内容を伏せているのと省略をしています。

オペレーティングシステム名(os.name):Windows 10
Javaバージョン(java.version):1.8.0_74
IPアドレス(getLocalHost):192.168.XXX.XXX
ホスト名(hostName):DESKTOP-XXXXX
----コンテンツを表示----
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head>
・・・
</body></html>
----サーバの証明書チェーンを表示----
[
[
  Version: V3
  Subject: CN=*.google.co.jp, O=Google LLC, L=Mountain View, ST=California, C=US
  Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11
・・・

エラーの場合

記事を書くときに参考にしたサイト様ではエラーが発生してしまいました。

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
	at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
	at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
	at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
	at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
	at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
	at ssltest.Connect.main(Connect.java:22)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
	at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
	at sun.security.validator.Validator.validate(Validator.java:260)
	at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
	... 11 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
	at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
	at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
	... 17 more

正常時との差異

どうやら、基本制限に△が付いているとなるようです。
分かったことがあれば追記します。
image.png

実行jarファイル

気軽に実行できるようにjarファイルを用意しました。下記jarファイルをダウンロードして、動かしたいサーバに配置した後にjavaのパスを通した状態で引数にURLを指定して利用してください。
https://github.com/koni1212/httpsConnect/raw/master/jarfile/httpsConnect.jar

image.png

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?