2010/02/05

erlang ssl_esock

HTTPS サーバーから証明書を取ってこようシリーズ
今回は Erlang です

Erlang の SSL な module はこちら
Erlang -- SSL Reference Manual
ざっと見ると getpeercert という関数があるのでこれっぽいです

左のコラムに ssl と ssl_new というのがありますが、これは
以前は SSL 関連でも全て OpenSSL を利用していたようで
connection を張ろうとしたりすると ssl_esock という subprocess が立ち
そいつに色々と仕事を任せていたようです
でも Erlang 的にはそこに C が入ってしまうので嬉しくありません

そこで cryptographic primitive な部分だけ OpenSSL を使って
connection の部分なんかを Erlang で実装したのが ssl_new のようです
cryptographic primitive は crypto って application を使います

で、application とか良く分かってないんですが
とりあえず start とかしといたらいいんでしょ、というのが次
-module(get_cert).
-compile(export_all).

get_cert(Host) ->
application:start(crypto),
application:start(ssl),
{ok, Sock} = ssl:connect(Host, 443, [{ssl_imp, new}]),
{ok, Der} = ssl:peercert(Sock),
{ok, Cert} = public_key:pkix_decode_cert(Der, otp),
ssl:close(Sock),
application:stop(ssl),
application:stop(crypto),
Cert.
ssl:connect の option に ssl_new を使うと伝え
ssl:peercert で証明書を DER で取り
public_key:pkix_decode_cert でそれを parse しています
otp ってあるのは parse の形式で、あと plain があります
それぞれ
-include_lib("public_key/include/public_key.hrl").
-include_lib("public_key/include/OTP-PUB-KEY.hrl").
的なことをしておけば読み易いです
かんたんなことしかしていませんね

で、動かしてみると
$ erl
Erlang R13B03 (erts-5.7.4) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4 (abort with ^G)
1> get_cert:get_cert("blogger.com").

=INFO REPORT==== 15-Feb-2010::01:34:14 ===
application: crypto
exited: stopped
type: temporary

=INFO REPORT==== 15-Feb-2010::01:34:14 ===
application: ssl
exited: stopped
type: temporary
{'OTPCertificate',
{'OTPTBSCertificate',v3,134060097170805708820381,
{'SignatureAlgorithm',{1,2,840,113549,1,1,5},'NULL'},
{rdnSequence,
[[{'AttributeTypeAndValue',{2,5,4,6},"US"}],
[{'AttributeTypeAndValue',
{2,5,4,10},
{printableString,"Google Inc"}}],
[{'AttributeTypeAndValue',
{2,5,4,3},
{printableString,"Google Internet Authority"}}]]},
{'Validity',
{utcTime,"091222015844Z"},
{utcTime,"101222020844Z"}},
{rdnSequence,
[[{'AttributeTypeAndValue',{2,5,4,6},"US"}],
[{'AttributeTypeAndValue',
{2,5,4,8},
{printableString,"California"}}],
[{'AttributeTypeAndValue',
{2,5,4,7},
{printableString,"Mountain View"}}],
[{'AttributeTypeAndValue',
{2,5,4,10},
{printableString,"Google Inc"}}],
[{'AttributeTypeAndValue',
{2,5,4,3},
{teletexString,"*.blogger.com"}}]]},
(あと長いんで略)
こんな感じで X.509 を parse してくれてます

ちなみに public_key の Reference は
Erlang -- public_key
にあって
例えば pem_to_der ってのを使えば PEM の PATH 与えて
DER で返してくれるなんてのも分かります