apacheでhttps通信をできるようにする

https通信を行うようにするには電子証明書が必要となる。電子証明書を作成するには証明書署名要求 (CSR: certificate signing request)を作成し、公開鍵証明書認証局(CA:Certificate Authority)にて署名した証明書を発行してもらわなければならない。今回は簡単なテスト用なので自己署名証明書(いわゆるオレオレ証明書)を利用する。そのためまずは署名するための認証局を作成する。実験環境はubuntu12.10。

CAの作成

今回はopensslを利用する.以下にてmake & install.

$ wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
$ tar xzvf openssl-1.0.1c.tar.gz
$ cd openssl-1.0.1c
$ ./config
$ make && make install

次に認証局を作成するための場所と設定ファイルのコピー。

# mkdir /etc/ssl
# cp openssl-1.0.1c/apps/openssl.cnf /etc/ssl # /usr/lib/ssl/openssl.cnfから/etc/ssl/openssl.cnfにシンボリックリンクが貼られていたのでここにコピーした。ここは環境によって適当に読み替える。
# cd /etc/ssl

実際に認証局を作成していく。openssl付属のCA.shなどのスクリプトを利用すると簡単につくってくれる。

# /usr/local/ssl/misc/CA.sh -newca
CA certificate filename (or enter to create)

Making CA certificate ...
Generating a 1024 bit RSA private key
..............................++++++
........................................................................++++++
writing new private key to './demoCA/private/./cakey.pem'
Enter PEM pass phrase: # 認証局の秘密鍵用のパスフレーズを入力
Verifying - 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) []:shinjyuku # 区などを入力
Organization Name (eg, company) [Internet Widgits Pty Ltd]:foo # 会社名などを入力
Organizational Unit Name (eg, section) []:developer group 1 # 部署名などを入力
Common Name (e.g. server FQDN or YOUR name) []:hoge.foo.com # FQDN
Email Address []:hoge@foo.com # 連絡先を入力

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:  #何も入力せずenter
An optional company name []: #何も入力せずenter
Using configuration from /usr/lib/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/./cakey.pem:  # 認証局の秘密鍵用のパスフレーズを再入力
Check that the request matches the signature
Signature ok
Certificate Details:
....

Write out database with 1 new entries
Data Base Updated

これで認証局の作成が完了した。

CSRの作成

次にhttps通信に利用する証明書を作成していく。証明書を作成するためにWebサーバー側で行うのはCSRの作成である。Webサーバーと認証局を同一のサーバー上で行っているのでいまどっちの作業を行っているかわかりにくい。イメージとして、Webサーバーと認証局を構築しているサーバーは別サーバーで行っていると読み替えた方がイメージは湧きやすいと思う。
CSRにはWebサーバーの秘密鍵と検証用のハッシュ値が含まれる。まずCSRに添付する秘密鍵を作成する。

# openssl genrsa -des3 -out server.key 1024
....
Enter pass phrase for server.key: # CSRに含めるサーバーの秘密鍵のパスフレーズを入力
Verifying - Enter pass phrase for server.key # 確認用に再入力

このままだとWebサーバーの起動毎にパスフレーズの入力を求められるのでプロテクトを解除する

# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key: # 先ほど入力したものを同じものを入力
writing RSA key

次にCSRを作成する

# openssl req -new -days 365 -key server.key -out server.csr
ここで入力を求められるがcountryName/stateOrProvinceName/organizationNameは認証局と同じものを設定しないと署名ができないので注意。

これでCSRが完成した。

証明書の作成

証明書を作成するには先ほど作成したWebサーバーのCSRにCAが署名すればよい。署名は以下のようにする

# openssl ca -in server.csr -keyfile demoCA/private/cakey.pem -out server.crt
ここで入力するパスフレーズは認証局のパスフレーズ値

これで証明書が作成できた。

apacheの設定

sslの設定で適切な場所に秘密鍵と証明書をおく

$ cat /etc/apache2/sites-enabled/httpd-ssl.conf
....
#   Server Certificate:
#   Point SSLCertificateFile at a PEM encoded certificate.  If
#   the certificate is encrypted, then you will be prompted for a
#   pass phrase.  Note that a kill -HUP will prompt again.  Keep
#   in mind that if you have both an RSA and a DSA certificate you
#   can configure both in parallel (to also allow the use of DSA
#   ciphers, etc.)
SSLCertificateFile "/etc/apache2/server.crt" <-- ここにwebサーバーの証明書をおく
#SSLCertificateFile "/etc/apache2/server-dsa.crt"

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile "/etc/apache2/server.key" <-- ここにwebサーバーの秘密鍵をおく
#SSLCertificateKeyFile "/etc/apache2/server-dsa.key"
....
# apache2ctl restart

以上でwebサーバーにhttpsアクセスできるようになるはず。

ブラウザインポート

アクセスするたびに警告がでるため、認証局をブラウザに信頼できる認証局と認識させる。

# openssl x509 -inform pem -in /etc/ssl/demoCA/cacert.pem -outform der -out ca.der

作成したca.derをブラウザインポートすると警告がでなくなる。