Java > TLS (SSL)のデバッグ

更新日 2014-05-08
広告

JavaでTLS (SSL)を使い、設定ミスがあると、以下の例外が発生することが多いです。

  • SSLHandshakeException
  • SSLKeyException
  • SSLPeerUnverifiedException
  • SSLProtocolException

javaコマンドに'-Djavax.net.debug=all'オプションを指定すると、TLS (SSL)ネゴシエーション処理のデバッグ文を出力してくれます。以下のサイトが参考になります。

  • http://docs.oracle.com/javase/jp/7/technotes/guides/security/jsse/ReadDebug.html

例えば、TomcatのSSL処理をデバッグしたい場合は、tomcat/bin/setenv.shを用意し、以下のように記述しておけば、catalina.outにデバッグ文が出力されます。

JAVA_OPTS=-Djavax.net.debug=all
以下、TLSネゴシエーションが行われた場合の、ログの例と、説明を記します。
 Allow unsafe renegotiation: false
 Allow legacy hello messages: true
 Is initial handshake: true
 Is secure renegotiation: false
 http-bio-8443-Acceptor-0, setSoTimeout(60000) called
 Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
 Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA
 Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
 Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
 Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
 Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
 Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256
 Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
 Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA
 No available cipher suite for TLSv1.1
Tomcat (というかJSSE)がサポートしているcipher suite (ハッシュアルゴリズムや暗号アルゴリズム)の一覧です。
 [Raw read]: length = 5
 0000: 16 03 03 00 C2
   :
[Ras read] というのは、データを受信したということです。上記の場合、受信したデータサイズは'5'です。
 http-bio-8443-exec-1, READ: TLSv1.2 Handshake, length = 194
 *** ClientHello, TLSv1.2
 RandomCookie:  GMT: 1374112707 bytes = { 226, 112, 120, 134, 129, 3, 22, 56, 126, 21, 35, 159, 149, 91, 194, 135, 136, 72, 54, 122, 71, 152, 51, 80, 53, 57, 148, 251 }
 Session ID:  {}
 Cipher Suites: [TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256]
 Compression Methods:  { 0 }
 Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1}
 Extension ec_point_formats, formats: [uncompressed]
 Extension signature_algorithms, signature_algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA224withECDSA, SHA224withRSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA, MD5withRSA
 Extension server_name, server_name: [host_name: test.server.com]
 ***
HTTPクライアントが送信してきた'ClientHello'の内容です。 TLSバージョン1.2で、ネゴシエーションしようとしていることがわかります。 signature_algorithmsの行は、クライアントがサポートしているCipher Suitesの一覧です。
 %% Negotiating:  [Session-1, TLS_RSA_WITH_AES_128_CBC_SHA256]
 *** ServerHello, TLSv1.2
 RandomCookie:  GMT: 1374112707 bytes = { 145, 15, 58, 31, 190, 46, 95, 144, 18, 172, 73, 125, 92, 53, 183, 51, 167, 38, 231, 243, 98, 75, 250, 250, 194, 232, 137, 57 }
 Session ID:  {82, 231, 76, 195, 219, 84, 74, 215, 228, 126, 116, 34, 233, 127, 134, 42, 206, 166, 6, 177, 134, 188, 112, 207, 87, 28, 133, 100, 67, 78, 217, 226}
 Cipher Suite: TLS_RSA_WITH_AES_128_CBC_SHA256
 Compression Method: 0
 Extension renegotiation_info, renegotiated_connection: <empty>
 ***
 Cipher suite:  TLS_RSA_WITH_AES_128_CBC_SHA256
サーバ(Tomcat)が返す'ServerHello'の内容です。 ciphper suiteとして、'TLS_RSA_WITH_AES_128_CBC_SHA256'を使うことに決まったことがわかります。
 *** Certificate chain
 chain [0] = [
 [
   Version: V3
   Subject: CN=server, OU=Server Certificate, O=Test, C=JP
   Signature Algorithm: SHA256withRSA, OID = 1.3.830.114548.1.1.12
 
   Key:  Sun RSA public key, 2048 bits
   :
 ***
サーバがクライアントに返す「サーバの証明書情報」です。 サーバがクライアントに返す証明書情報が正しいかどうかは、ここをチェックするべきです。 chain [0] というのは、証明書の配列(trust chain)の一番目を意味しています。 中間CAやルートCAの証明書と、チェーンが構築されていれば、chain [1]やchain [2]も出力されるはずです。
 *** CertificateRequest
 Cert Types: RSA, DSS, ECDSA
 Supported Signature Algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA224withECDSA, SHA224withRSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA, MD5withRSA
 Cert Authorities:
 <CN=Client CA, OU=RSA CA, O=, C=US>
サーバ認証だけでなく、クライアント認証も行う場合は、上記のように 'CertificateRequest' の内容が出力されます。
 *** ServerHelloDone
 [write] MD5 and SHA1 hashes:  len = 1516
 0000: 02 00 00 4D 04 03 52 E7   4C C3 91 0E 39 3D BE 2E  ...M..R.L...9=..
   :
 http-bio-8443-exec-1, READ: TLSv1.2 Handshake, length = 1169
 *** Certificate chain
 chain [0] = [
 [
   Version: V3
   Subject: CN=test, OU=Test Certificate, O=Test, C=JP
   Signature Algorithm: SHA256withRSA, OID = 1.2.840.113542.1.2.32
 
   Key:  Sun RSA public key, 2048 bits
   :
クライアントが送付してきた証明書の情報です。 クライアントの証明書をチェックする場合は、ここを見るべきです。 ただし、サーバ認証が失敗した場合は、上述のクライアントの証明書情報が出力される前に、例外(Exception)が発生します。
 ***
 Found trusted certificate:
 [
 [
   Version: V3
   Subject: CN=Client CA, OU=CA, O=Test, C=US
   Signature Algorithm: SHA256withRSA, OID = 1.2.830.193341.2.1.11
 
   Key:  Sun RSA public key, 2048 bits
     :

クライアント認証が成功すれば、上記のように、"found trusted certificate" と、なります。

以降、暗号化で使う鍵の作成処理などが行われます。

 *** ClientKeyExchange, RSA PreMasterSecret, TLSv1.2
  :

 *** CertificateVerify
  :

 *** Finished
このあと、HTTP通信が行われます。
   :
 http-bio-8443-exec-2, setSoTimeout(59999) called
 http-bio-8443-exec-2, received EOFException: ignored
 http-bio-8443-exec-2, called closeInternal(false)
 http-bio-8443-exec-2, SEND TLSv1.2 ALERT:  warning, description = close_notify
 http-bio-8443-exec-2, called closeSocket(selfInitiated)
 http-bio-8443-exec-2, called close()
 http-bio-8443-exec-2, called closeInternal(true)
HTTP通信の終了後、上記のログが出力されます。
広告
お問い合わせは sweng.tips@gmail.com まで。
inserted by FC2 system