OS X Yosemite の cURL で TLS 1.2 を使って接続してみる

OS X Yosemite(10.10.5) の cURL で TLS 1.2 接続するのに、 OpenSSL と cURL を入れ替える必要があったので記しておきます。

$ curl --dump-header - https://www.example.com --tlsv1.2 --verbose
*   Trying nnn.nnn.nnn.nnn...
* Connected to www.example.com (nnn.nnn.nnn.nnn) port 443 (#0)
* SSL peer handshake failed, the server most likely requires a client certificate to connect
* Closing connection 0
curl: (35) SSL peer handshake failed, the server most likely requires a client certificate to connect

サーバー側は TLS 1.2 設定に問題はないのですが、接続に失敗してしまいました。

$ which openssl
/usr/bin/openssl
$ openssl version
OpenSSL 0.9.8zg 14 July 2015

元々入っている OpenSSL が古いですね。
0.9.8 系は TLS 1.2 非対応なので、1.0.2 系に新しくします。

$ brew update
$ brew upgrade openssl
$ brew link openssl --force

ターミナルを立ち上げ直して、

$ which openssl
/usr/local/bin/openssl
$ openssl version
OpenSSL 1.0.2e 3 Dec 2015

1.0.2 系になりました。

念のため TLS 1.2 の cipher あるか確認してみましょう。

$ openssl ciphers -v
ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH     Au=RSA  Enc=AESGCM(256) Mac=AEAD
ECDHE-ECDSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH     Au=ECDSA Enc=AESGCM(256) Mac=AEAD
:
:
:

ありますね。

気を取り直して cURL 実行。

$ curl --dump-header - https://www.example.com --tlsv1.2 --verbose
*   Trying nnn.nnn.nnn.nnn...
* Connected to www.example.com (nnn.nnn.nnn.nnn) port 443 (#0)
* SSL peer handshake failed, the server most likely requires a client certificate to connect
* Closing connection 0
curl: (35) SSL peer handshake failed, the server most likely requires a client certificate to connect

相変わらず失敗。

OpenSSL を TLS クライアントとして実行してみました。

$ openssl s_client -connect www.example.com:443 -tls1_2
CONNECTED(00000003)
:
:
:

TLS 1.2 でつながりました。
ということは cURL がわるい?

$ which curl
/usr/bin/curl
$ curl --version
curl 7.43.0 (x86_64-apple-darwin14.0) libcurl/7.43.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz UnixSockets

バージョン情報の見方は curl.1 man ページ version

OpenSSL が使われていない…。 OpenSSL にリンクした cURL に差し替えてみます。

http://stackoverflow.com/questions/26461966/osx-10-10-curl-post-to-https-url-gives-sslread-error

$ brew install --with-openssl curl
:
:
🍺  /usr/local/Cellar/curl/7.47.0: 359 files, 2.5M, built in 5 minutes 1 second
$ brew link curl --force

インストールに5分以上かかりちょっとドキドキしますが、気長に待ちましょう。

ターミナルを立ち上げ直して、

$ which curl
/usr/local/bin/curl
$ curl --version
curl 7.47.0 (x86_64-apple-darwin14.5.0) libcurl/7.47.0 OpenSSL/1.0.2e zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

OpenSSL を利用するようになった模様。 そもそも SecureTransport とは Secure Transport ですね。

試しに www.google.com に繋いでみました。

$ curl --dump-header - https://www.google.com --tlsv1.2 --verbose

動きました。