blog/content/post/2010/12/23/2010-12-23-00001439.md

172 lines
8.3 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: Gauche で HTTPS 接続を行う
author: kazu634
date: 2010-12-23
url: /2010/12/23/_1643/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5399;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
---
<div class="section">
<h4>
背景
</h4>
<p>
Gauche 0.9.1 (2010.12.11リリース)から HTTPS 接続が行えるようになりました。今回、 HTTPS 接続を行う必要性に迫られたので、 Gauche で HTTPS 接続を試みてみました。
</p>
<p>
なお、unix限定とありますが、Mac (Snow Leopard) でも動作したことを付け加えておきます。
</p>
<h4>
Gauche の HTTPS 接続の前提条件
</h4>
<p>
リリース時の説明には次のように記載されています:
</p>
<blockquote title="404 Not Found" cite="http://practical-scheme.net/gauche/index-j.html">
<p>
rfc.httpモジュールがhttpsコネクションをサポートします (今のところunix限定)。 コネクション自体は外部プログラム(stunnel) に依存します。
</p>
<p>
<cite><a href="http://practical-scheme.net/gauche/index-j.html" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://practical-scheme.net/gauche/index-j.html', 'Gauche &#8211; A Scheme Implementation');" target="_blank">Gauche &#8211; A Scheme Implementation</a></cite>
</p>
</blockquote>
<p>
Gauche の Info にはこんな説明が:
</p>
<pre>
Currently Gauche relies on an external program `stunnel' (either
version 3 or 4) to handle secure connections. So when you request a
secure connection, Gauche looks for `stunnel' program in `PATH'. On
some systems a newer version of it is called `stunnel4', and Gauche
looks for it, too. If you want to add certificates, for example, you
have to adjust `stunnel' settings.
A plan is to provide the means of secure connections as long as
`stunnel', including native SSL support, and the higher-level library
automatically choose whatever is available and best.
Use the following procedure to check if you can use secure
connections:
-- Function: http-secure-connection-available?
Returns `#t' if running Gauche can use secure connection, `#f'
otherwise.
</pre>
<p>
手っ取り早く言えば、
</p>
<ol>
<li>
stunnel を使って HTTPS 接続を実現しているから、 PATH が通っているところに stunnel を設置する
</li>
<li>
HTTPS 接続を行えるかどうかは、(http-secure-connection-available?) の戻り値で判定できる
</li>
</ol>
<h4>
stunnelとは
</h4>
<p>
不勉強で、stunnelを知らなかったので、調べてみました:
</p>
<blockquote title="Stunnel:title - Wikipedia" cite="http://ja.wikipedia.org/wiki/Stunnel">
<p>
Stunnel は、フリーなマルチプラットフォーム対応のプログラムであり、汎用TLS/SSLトンネリングサービスを提供する。
</p>
<p>
Stunnel は、TLSやSSLにネイティブで対応していないクライアントサーバシステムに安全な暗号化されたコネクションを提供するのに利用できる。各種オペレーティングシステム上で動作し、ほとんどのUnix系OSとWindowsでも動作する。TLSまたはSSLプロトコルの実装にはOpenSSLやSSLeayなどのライブラリが別途必要である。
</p>
<p>
Stunnel はSSLコネクションにX.509公開鍵証明書を使った公開鍵暗号を使っている。クライアントもオプションとして証明書で認証できる。
</p>
<p>
<cite><a href="http://ja.wikipedia.org/wiki/Stunnel" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://ja.wikipedia.org/wiki/Stunnel', 'Stunnel &#8211; Wikipedia');" target="_blank">Stunnel &#8211; Wikipedia</a></cite>
</p>
</blockquote>
<p>
HTTPS 接続を行うためのコネクションを提供するプログラムのようです。知らなかった。
</p>
<h4>
stunnel のインストール
</h4>
<p>
Macでパッケージ管理に Homebrew を用いているので、Homebrew で stunnel を導入してみました。 MacPorts を使っている人は MacPorts に読み替えてください。
</p>
<pre class="syntax-highlight">
kazu634@kazu634% brew search stunnel ~/working/tmp_lisp/foursquare <span class="synStatement">[</span><span class="synConstant">448</span><span class="synStatement">]</span>
stunnel
kazu634@kazu634% brew <span class="synStatement">install</span> stunnel ~/working/tmp_lisp/foursquare <span class="synStatement">[</span><span class="synConstant">449</span><span class="synStatement">]</span>
<span class="synStatement">==&#62;</span> Downloading http://www.stunnel.org/download/stunnel/src/stunnel<span class="synConstant">-4</span>.<span class="synConstant">33</span>.tar.gz
<span class="synComment">######################################################################## 100.0%</span>
<span class="synStatement">==&#62;</span> Downloading patches
<span class="synStatement">==&#62;</span> Patching
patching file tools/stunnel.cnf
<span class="synStatement">==&#62;</span> ./configure <span class="synSpecial">--disable-dependency-tracking</span> <span class="synSpecial">--disable-libwrap</span> <span class="synSpecial">--prefix=/usr/local/Cellar/stunnel/4.33</span> <span class="synSpecial">--sysconfdir=/usr/local/etc</span>
<span class="synStatement">==&#62;</span> make <span class="synStatement">install</span>
<span class="synStatement">==&#62;</span> Caveats
A bogus SSL server certificate has been installed to:
/usr/<span class="synStatement">local</span>/etc/stunnel/stunnel.pem
This certificate will be used by default unless a config file says
otherwise<span class="synStatement">!</span>
In your stunnel configuration, specify a SSL certificate with
the <span class="synStatement">&#34;</span><span class="synConstant">cert =</span><span class="synStatement">&#34;</span> option <span class="synStatement">for</span> each service.
<span class="synStatement">==&#62;</span> Summary
/usr/<span class="synStatement">local</span>/Cellar/stunnel/<span class="synConstant">4</span>.<span class="synConstant">33</span>: <span class="synConstant">33</span> files, 492K, built <span class="synStatement">in</span> <span class="synConstant">16</span> seconds
kazu634@kazu634% which stunnel ~/working/tmp_lisp/foursquare <span class="synStatement">[</span><span class="synConstant">450</span><span class="synStatement">]</span>
/usr/<span class="synStatement">local</span>/bin/stunnel
</pre>
<h4>
使ってみる
</h4>
<p>
この時点で使えるか、試してみました:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>http-secure-connection-available?<span class="synSpecial">)</span>
#<span class="synStatement">t</span>
</pre>
<p>
なんか使える雰囲気です。そこで、 Foursquare の API にアクセスしてみることにしました。基本的には、 http-get に secureキーワードを指定し、 #t にします。こんな感じ:
</p>
<pre class="syntax-highlight">
gosh&#62; <span class="synSpecial">(</span>receive <span class="synSpecial">(</span>status header body<span class="synSpecial">)</span>
<span class="synSpecial">(</span>http-get <span class="synConstant">&#34;api.foursquare.com&#34;</span>
<span class="synSpecial">(</span>string-append
<span class="synConstant">&#34;/v2/users/self/checkins?oauth_token=&#34;</span>
<span class="synConstant">&#34;私の oauth_token &#34;</span><span class="synSpecial">)</span>
:secure #t<span class="synSpecial">)</span>
<span class="synComment">;;; ~~~~~~~~~~~</span>
<span class="synSpecial">(</span><span class="synStatement">print</span> status<span class="synSpecial">))</span>
<span class="synConstant">200</span>
#&#60;undef&#62;
</pre>
<p>
ステータスコード 200 はアクセス成功という意味なので、アクセスできています!やった!
</p>
</div>