OpenSSLをSSL/TLSクライアントとして使ってみる

$ openssl s_client -connect servername:443 で SSL 接続

HTTP の接続を確認したくて telnet を使うことがある。
たとえば、 telnet で http://www.google.com:80/ に GET するには以下のコマンドを実行する。

$ telnet www.google.com 80
Trying 173.194.117.145...
Connected to www.google.com.
Escape character is '^]'.
GET / HTTP/1.1
host: www.google.com

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.jp/?gfe_rd=cr&ei=nLj5U8KaGeTM8geftoHoAw
Content-Length: 261
Date: Sat, 09 Aug 2014 06:48:56 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic

...

HTTPS の接続でも telnet を使えると便利なのだけれども、残念ながら telnet は HTTPS を喋れない。
そのような用途のために OpenSSL には s_client コマンドが存在し SSL/TLS クライアントとして使える。

$ openssl s_client -connect www.google.com:443 -quiet
depth=2 C = US, O = GeoTrust Inc., CN = GeoTrust Global CA
verify error:num=20:unable to get local issuer certificate
verify return:0
GET / HTTP/1.1
Host: www.google.com

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: https://www.google.co.jp/?gfe_rd=cr&ei=zLj5U-S3GOfM8gfDiYG4Ag
Content-Length: 262
Date: Sat, 09 Aug 2014 06:48:20 GMT
Server: GFE/2.0
Alternate-Protocol: 443:quic

...

雑多なメモ

実行結果をダンプしてプログラムの処理を終える

$ openssl s_client をすると、標準入力を受け付けているので、実行後にすぐに処理を終えたい場合は EOF を食わせれば良い(< /dev/null)

$ openssl s_client -connect www.google.com:443 < /dev/null
CONNECTED(00000003)
depth=2 C = US, O = GeoTrust Inc., CN = GeoTrust Global CA
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
...
---
DONE

特定プロトコルで通信させる

-ssl2, -ssl3, -tls1, -tls1_1, -tls1_2 など通信したいプロトコルを指定する。

TLS1 で通信する場合

$ openssl s_client -connect www.google.com:443 -tls1
...
SSL-Session:
    Protocol  : TLSv1

となる。

特定プロトコルでは通信させない

-no_ssl2, -no_ssl3, -no_tls1 など通信したくないプロトコルを指定

$ openssl s_client -connect www.google.com:443 -no_tls1_2
...
SSL-Session:
    Protocol  : TLSv1.1

と TLSv1.2 で通信されず TLSv1.1 で通信されている。

通信可能なプロトコルをどんどん減らしていくと

$ openssl s_client -connect www.google.com:443 -no_tls1_2 -no_tls1_1 -no_tls1 -no_ssl3
CONNECTED(00000003)
139809935660704:error:14077102:SSL routines:SSL23_GET_SERVER_HELLO:unsupported protocol:s23_clnt.c:735:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 7 bytes and written 305 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
---

というようにサーバが対応するプロトコルがなくなり、SSLセッションを確立できなくなる。

サーバの証明書をすべて表示

-showcertsオプションをつける

$ openssl s_client -connect www.google.com:443 -showcerts
CONNECTED(00000003)
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com
   i:/C=US/O=Google Inc/CN=Google Internet Authority G2
-----BEGIN CERTIFICATE-----
MIIEdjCCA16gAwIBAgIIRi5CVrsRlNwwDQYJKoZIhvcNAQEFBQAwSTELMAkGA1UE
...
xxKMbav/sRFQp9KmLOVluKAqbEyOz8escGXBl5y41Q6r1dNscqU5bnEu
-----END CERTIFICATE-----
 1 s:/C=US/O=Google Inc/CN=Google Internet Authority G2
   i:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
-----BEGIN CERTIFICATE-----
MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
...
yuGnBXj8ytqU0CwIPX4WecigUCAkVDNx
-----END CERTIFICATE-----
 2 s:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
   i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
-----BEGIN CERTIFICATE-----
MIIDfTCCAuagAwIBAgIDErvmMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT
...
b8ravHNjkOR/ez4iyz0H7V84dJzjA1BOoa+Y7mHyhD8S
-----END CERTIFICATE-----
---
Server certificate
subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com
issuer=/C=US/O=Google Inc/CN=Google Internet Authority G2
...

証明書チェーンの概要を確認

quote オプションなしに実行する。Certificate chain がサーバの送信する証明書一覧

$ openssl s_client -connect www.google.com:443 < /dev/null
...
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com
   i:/C=US/O=Google Inc/CN=Google Internet Authority G2
 1 s:/C=US/O=Google Inc/CN=Google Internet Authority G2
   i:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
 2 s:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
   i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
---
...

0, 1, 2 と3枚送られている。
s は subject(証明対象) で i は issuer(証明書の発行者)。
0 の issuer = 1 の subject というように、チェーンになっていることがわかる。

クライアント認証の接続確認

クライアント証明書(-cert cert.cer)と秘密鍵(-key cert.ke)を指定

$ openssl s_client -showcerts -cert cert.cer -key cert.key -connect servername:443

通信内容も含めてログを強化

-debugオプションをつける

$ openssl s_client -connect www.google.com:443 -debug

SSLセッションや証明書情報を表示しない

-quietオプションをつける

$ openssl s_client -connect www.google.com:443 -quiet
depth=2 C = US, O = GeoTrust Inc., CN = GeoTrust Global CA
verify error:num=20:unable to get local issuer certificate
verify return:0

通信のサマリーだけを表示

-briefオプションをつける

$ ./openssl version
OpenSSL 1.0.2-beta2 22 Jul 2014
$ ./openssl s_client -connect www.google.com:443 -brief
depth=2 C = US, O = GeoTrust Inc., CN = GeoTrust Global CA
verify error:num=20:unable to get local issuer certificate
CONNECTION ESTABLISHED
Protocol version: TLSv1.2
Ciphersuite: ECDHE-RSA-AES128-GCM-SHA256
Peer certificate: C = US, ST = California, L = Mountain View, O = Google Inc, CN = www.google.com
Hash used: SHA512
Supported Elliptic Curve Point Formats: uncompressed:ansiX962_compressed_prime:ansiX962_compressed_char2
Server Temp Key: ECDH, P-256, 256 bits
...

ChangeLog をよむと 1.0.2 系で追加された機能のようなので、1.0.2-beta2 なりを自分でインストールすること。

References

2 thoughts on “OpenSSLをSSL/TLSクライアントとして使ってみる

Leave a comment