SSL対応 Apache(mod_ssl編)
Secure Sockets Layer (SSL)
ブラウザーと Web サーバー(httpd)との通信は、Hyper Text Transfer Protcol (HTTP) と呼ばれるプロトコルでデータをやり取りします。しかし、生データが流れるためインターネットショッピング等をする際に、個人情報が漏れるなどセキュリティー上不具合が生じます。そこで、データを暗号化する Secure Sockets Layer (SSL)という仕組みが考えられました。
Apache を SSL対応にするために必要なモジュール
http://www.apache.org/dist/httpd/apache_1.3.26.tar.gz
http://www.modssl.org/source/mod_ssl-2.8.10-1.3.26.tar.gz
http://www.openssl.org/source/openssl-0.9.6d.tar.gz
上記のモジュールをダウンロードして SSL対応の Apache を作る場合、ソースそのものが改ざんされていないかチェックしておくと良いでしょう。PGP Signatures が用意されているので、 PGPを利用して改ざんされていないかモジュールを調べておくことをお勧めします。
MM Shared Memory library を利用したい場合、http://www.engelschall.com/sw/mm/ から必要なモジュールをダウンロードしてライブラリーを作成(./configure --disable-shared;make
)し、moad_ssl
のコンフィグレーション時に --with-mm=mm_path
オプションを利用してください。
モジュールの作成
まず、はじめに OpenSSL をインストールする必要があります。 デフォルトコンフィグレーションでは、/usr/local/ssl
ディレクトリーに各モジュールがインストールされます。
# gzip -dc openssl-0.9.6d.tar.gz | tar xf - # cd openssl-0.9.6d # ./config # make # make test # make install # cd ..
Apache のソース(apache_1.3.26.tar.gz
)と mod_ssl のソース(mod_ssl-2.8.10-1.3.26
)を展開し、apache
や openssl の場所を指定してmod_ssl をコンフィグレーションします。mod_sslのconfigure
を実行すると、Apache側のconfigure
も自動的に実行してくれます。
# gzip -dc apache_1.3.26.tar.gz | tar xf - # gzip -dc mod_ssl-2.8.10-1.3.26 | tar xf - # cd mod_ssl-2.8.10-1.3.26 # ./configure --with-apache=../apache_1.3.26 --with-ssl=/usr/local/ssl \ > --enable-shared=ssl --prefix=/usr/local/apache # cd ../apache_1.3.26 # make
動作確認のための証明書や認証キーの作成とインストール
とりあえず動作テストのための、証明書や認証キーを作成します。 このとき認証機関(Certification Authority)は、架空(?)の Snake Oil になります。この証明書を利用して実際に運用しないでください。
# make certificate SSL Certificate Generation Utility (mkcert.sh) Copyright (c) 1998-2000 Ralf S. Engelschall, All Rights Reserved. Generating test certificate signed by Snake Oil CA [TEST] WARNING: Do not use this for real-life/production systems ______________________________________________________________________ STEP 0: Decide the signature algorithm used for certificate The generated X.509 CA certificate can contain either RSA or DSA based ingredients. Select the one you want to use. Signature Algorithm ((R)SA or (D)SA) [R]:<CR> ______________________________________________________________________ STEP 1: Generating RSA private key (1024 bit) [server.key] 95191 semi-random bytes loaded Generating RSA private key, 1024 bit long modulus ..++++++ ...++++++ e is 65537 (0x10001) ______________________________________________________________________ STEP 2: Generating X.509 certificate signing request [server.csr] Using configuration from .mkcert.cfg You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- 1. Country Name (2 letter code) [XY]:JP 2. State or Province Name (full name) [Snake Desert]:Tokyo 3. Locality Name (eg, city) [Snake Town]:Tokyo 4. Organization Name (eg, company) [Snake Oil, Ltd]:Example Co. 5. Organizational Unit Name (eg, section) [Webserver Team]:Baz Section 6. Common Name (eg, FQDN) [www.snakeoil.dom]:www.example.com 7. Email Address (eg, name@FQDN) [www@snakeoil.dom]:webmaster@example.com 8. Certificate Validity (days) [365]:<CR> ______________________________________________________________________ STEP 3: Generating X.509 certificate signed by Snake Oil CA [server.crt] Certificate Version (1 or 3) [3]:<CR> Signature ok subject=/C=JP/ST=Tokyo/L=Tokyo/O=Example Co./OU=Baz Section/CN=www.example.com/ Email=webmaster@example.com Getting CA Private Key Verify: matching certificate & key modulus read RSA key Verify: matching certificate signature ../conf/ssl.crt/server.crt: /C=XY/ST=Snake Desert/L=Snake Town/O=Snake Oil, Ltd/ OU=Certificate Authority/CN=Snake Oil CA/Email=ca@snakeoil.dom error 10 at 1 depth lookup:certificate has expired OK ______________________________________________________________________ STEP 4: Enrypting RSA private key with a pass phrase for security [server.key] The contents of the server.key file (the generated private key) has to be kept secret. So we strongly recommend you to encrypt the server.key file with a Triple-DES cipher and a Pass Phrase. Encrypt the private key now? [Y/n]:<CR> read RSA key writing RSA key Enter PEM pass phrase:<パスフレーズを入力> Verifying password - Enter PEM pass phrase:<確認のため同じパスフレーズを入力> Fine, you're using an encrypted RSA private key. ______________________________________________________________________ RESULT: Server Certification Files o conf/ssl.key/server.key The PEM-encoded RSA private key file which you configure with the 'SSLCertificateKeyFile' directive (automatically done when you install via APACI). KEEP THIS FILE PRIVATE! o conf/ssl.crt/server.crt The PEM-encoded X.509 certificate file which you configure with the 'SSLCertificateFile' directive (automatically done when you install via APACI). o conf/ssl.csr/server.csr The PEM-encoded X.509 certificate signing request file which you can send to an official Certificate Authority (CA) in order to request a real server certificate (signed by this CA instead of our demonstration-only Snake Oil CA) which later can replace the conf/ssl.crt/server.crt file. WARNING: Do not use this for real-life/production systems # # make install
起動
Apache を起動させる際の apachectl コマンドに、新しく startssl コマンドラインオプションが追加されます。 今までどおり、start を指定すると SSL 非対応の apache のみ起動します。
# /usr/local/apache/bin/apachectl startssl Apache/1.3.26 mod_ssl/2.8.10 (Pass Phrase Dialog) Some of your private key files are encrypted for security reasons. In order to read them you have to provide us with the pass phrases. Server www.example.com:443 (RSA) Enter pass phrase:<パスフレーズを入力> Ok: Pass Phrase Dialog successful. /usr/local/apache/bin/apachectl startssl: httpd started #
Apache を SSL 対応で起動するたびに、パスフレーズを聞いてくるので OS 起動時に自動的に起動することができません。そこで、パスフレーズをはずしてしまいます。
# cd /usr/local/apache/conf/ssl.key # cp -p server.key server.key.org # /usr/local/ssl/bin/openssl rsa -in server.key.org -out server.key read RSA key Enter PEM pass phrase:<パスフレーズを入力> writing RSA key #
動作確認
実際にブラウザーからサーバーにアクセスし、SSL が機能しているか確認します。ここで、注意することがあります。通 常、URL は http://www.example.com/ と指定しますが、SSL の場合、https://www.example.com/ というふうに httpsとなります。これまでの設定では、通信中のデータを暗号化する程度に利用するなら問題ないが、クレジット番号を利用するような場合、ベリサインのようなそれなりに信用のある機関に証明書を発行してもらう必要があります。
証明書や認証キーの作成
商用利用するためには、本当に信頼のおける環境なのかを相手に証明するために、ベリサインのような公の認証機関が発行した証明書を利用します。証明書等を入手したら、Apache の設定ファイル(/usr/local/apache/conf/httpd.conf)を編集し登録します。
SSLCertificateFile SSLCertificateKeyFile SSLCACertificateFile SSLCARevocationFile
ベリサインの証明書を登録する方法はここから。説明では ssleayコマンドを利用していますが openssl コマンドを利用する。
しかし、経路暗号化のためだけに利用するならそれほどシリアスになる必要はありません。そこで、自分で証明した証明書を作って登録してしまいます。
【コンパイル環境がある場合】
make certificate する際に TYPE=custom オプションを使用することで簡単に作成できます。
# make certificate TYPE=custom
以降、質問に対して必要な情報を入力していきます。
【コンパイル環境がない場合】
openssl コマンドを利用して公開鍵証明書作成、CSR(Certificate Signing Request:証明書要求)の作成、CSR に署名し apache に登録することで利用できるようになります。
証明書等はどこに作っても良いが、管理しやすいように /usr/local/apache/conf に作成します。openssl コマンドを起動する上で必要となる設定ファイル(openssl.cnf)を作成します。以下は、最低限修正していくと便利な部分のみ記述しておきます。次回利用するのに以下の変更以外に default とついた部分を修正しておくと良いでしょう。
# cd /usr/local/apache/conf # mkdir ca # cd ca # cp /usr/local/ssl/openssl.cnf . # edit openssl.cnf
RANDFILE = private/.rnd dir = .
openssl.cnf に従って必要なファイルとディレクトリーを作成します。その後、opensslコマンドを利用して証明書を作成します。
# echo 01 > serial # touch index.txt # mkdir certs crl newcerts private # /usr/local/ssl/bin/openssl md5 * > private/.rnd # /usr/local/ssl/bin/openssl req -x509 -new -config ./openssl.cnf \ > -keyout private/cakey.pem -out cacert.pem Using configuration from ./openssl.cnf Generating a 1024 bit RSA private key ........++++++ ................................++++++ writing new private key to 'private/cakey.pem' Enter PEM pass phrase:<パスフレーズを入力> Verifying password - Enter PEM pass phrase:<確認のため同じパスフレーズを入力> ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:JP State or Province Name (full name) [Some-State]:Tokyo Locality Name (eg, city) []:Tokyo Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Co. Organizational Unit Name (eg, section) []:Baz Section Common Name (eg, YOUR name) []:www.example.com Email Address []:webmaster@example.com # /usr/local/ssl/bin/openssl req -new -config ./openssl.cnf \ > -keyout private/site-cert.key -out site-cert.pem Using configuration from ./openssl.cnf
Generating a 1024 bit RSA private key .....++++++ .........++++++ writing new private key to 'private/site-cert.key' Enter PEM pass phrase:<パスフレーズを入力> Verifying password - Enter PEM pass phrase:<確認のため同じパスフレーズを入力> ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:JP State or Province Name (full name) [Some-State]:Tokyo Locality Name (eg, city) []:Tokyo Organization Name (eg, company) [Internet Widgits Pty Ltd]]:Example Co. Organizational Unit Name (eg, section) []:Baz Section Common Name (eg, YOUR name) []:www.example.com Email Address []:webmaster@example.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:<CR> An optional company name []:<CR> # /usr/local/ssl/bin/openssl ca -config ./openssl.cnf \ > -in site-cert.pem -out site-cert.crt Using configuration from ./openssl.cnf Enter PEM pass phrase:<パスフレーズを入力> Check that the request matches the signature Signature ok The Subjects Distinguished Name is as follows countryName :PRINTABLE:'JP' stateOrProvinceName :PRINTABLE:'Tokyo' localityName :PRINTABLE:'Tokyo' organizationName :PRINTABLE:'Example Co.' organizationalUnitName:PRINTABLE:'Baz Section' commonName :PRINTABLE:'www.example.com' emailAddress :IA5STRING:'webmaster@example.com' Certificate is to be certified until Jun 17 03:02:44 2003 GMT (365 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated #
【作成したキーを Apache に登録】
作成した証明書や認証キーにあわせて Apache の設定ファイル(/usr/local/apache/conf/httpd.conf)を編集します。
SSLCertificateFile /usr/local/apache/conf/ca/site-cert.crt SSLCertificateKeyFile /usr/local/apache/conf/ca/private/site-cert.key
として証明書等を登録することができます。