カテゴリー
WordPress

本家のお世話-#68。(WordPress SSL ログイン-#1)

The same article in English
投稿アップデート情報  追記(6/19)

 前に書いたが, WordPress のログインを自宅 LAN からのみ可にしていた。しばらく,これでやっていたが,やはり,不便。なので,この際,使っている WiFi 端末からログインできるようにしようと思うのだが, WordPress のパスワードを平文で流すのは日頃のポリシーに反する。で,サーバ (WindowsXP SP3 (x86)) 上で停止していた SSL 対応を復活してやろうと思い立った。もともと, Apache 2.4 win32 binaries は,Build with apr-1.4.6 apr-util-1.4.1 apr-iconv-1.2.1 openssl-1.0.1e zlib-1.2.7 pcre-8.32 libxml2-2.9.0 lua-5.1.5 expat-2.1.0 だし, WordPress のログインに私が使うだけなので,自前認証局を作成して Apache の mod_ssl を使えばいいかと思った。

 まずは,自前認証局のために, cakey.pem と cacert.pem を作る。普通, Windows 上では CA.pl を使うのだが, Apache 2.4 win32 binaries には CA.pl が入っていなかった。というわけで, openssl.exe をコマンドラインで使うことにする。どうやったらいいかは, the document page (魚拓です)にあった。

  1. サーバのどこかに自前認証局用のディレクトリ myCA を作成。 myCA の中には, private と newcerts の2つのディレクトリと,空の index.txt を作成しておく。実のところ, myCA はリムーバル・メディア上に作った。これだと,後で my_ca.crt と clientcert.p12 をクライアント用のパソコンに移動するのも簡単だし,自前認証局保管のためには,パソコンから切り離しておけば安全でもある。
  2. Apache conf ディレクトリに openssl.cnf がある。 WindowsXP だと, openssl.cnf は拡張子なしの openssl になっていて,しかもファイルタイプが “短縮ダイヤル” になっている。昔これで悩んだんだよねぇ。これを開けてカスタマイズする。 WindowsXP 上だと,セパレータを “/” から “” に変える作業をやっとかないと,下記のエラーが出る。 M$ の最近の cmd.exe だと “/” もちゃんと認識してくれるようだが, WinXP 上の cmd.exe ではダメ。さらに, はエスケープしておいてやらないといけないので, “” ということになる。

    Error opening CA private key ./myCA/private/cakey.pem
    3464:error:02001003:system library:fopen:No such process:.cryptobiobss_file.c:398:fopen(‘./myCA/private/cakey.pem’,’rb’)
    3464:error:20074002:BIO routines:FILE_CTRL:system lib:.cryptobiobss_file.c:400:
    unable to load CA private key

    openssl.cnf のカスタマイズ:

         RANDFILE = $ENV::HOME/.rnd —> RANDFILE = $ENV::HOME.rnd
         #oid_file = $ENV::HOME/.oid —> #oid_file = $ENV::HOME.oid

         dir = ./demoCA —> dir = (drive xx):myCA   (絶対パスじゃないとダメみたい)
         certs = $dir/certs —> certs = $dircerts
         crl_dir = $dir/crl —> crl_dir = $dircrl
         database = $dir/index.txt —> database = $dirindex.txt

         new_certs_dir = $dir/newcerts —> new_certs_dir = $dirnewcerts

         certificate = $dir/cacert.pem —> certificate = $dircacert.pem
         serial = $dir/serial —> serial = $dirserial
         crlnumber = $dir/crlnumber —> crlnumber = $dircrlnumber

         crl = $dir/crl.pem —> crl = $dircrl.pem
         private_key = $dir/private/cakey.pem —> private_key = $dirprivatecakey.pem
         RANDFILE = $dir/private/.rand —> RANDFILE = $dirprivate.rand

    もし, TSA (Time Stamping Authority) を使うなら,同じように [ tsa_config1 ] のとこをカスタマイズ。

    RSA private key のデフォルトとして, 2048 ビットを使うので,
         default_bits = 1024 —> default_bits = 2048

    ところで, (drive x):(Apache bin dir) へのパスを通していない。このためにしか使わないので,サーバ上で余計なパスを追加したくないからだが,この辺は私の流儀なので,各自の流儀でやってください。

  3. cmd.exe を起動する。
         >cd (drive xx):myCA
         >copy index.txt+   (UNIX touch コマンドの代わりに使う)
         >echo 01 > serial   (01 が書き込まれた serial ファイルがいるので)
         >(drive x):(Apache bin dir)openssl req -new -keyout privatecakey.pem -out careq.pem -config (drive x):(Apache conf dir)openssl.cnf

         Enter PEM pass phrase: xxxxxxxxxxxxxxxx   (cakey.pem 用の pass phrase)
         Verifying – Enter PEM pass phrase: xxxxxxxxxxxxxxxx   (cakey.pem 用の pass phrase)

         Country Name:JP   (今回作るすべてのキーで同一)
         State or Province Name:FUKUOKA   (今回作るすべてのキーで同一)
         Locality Name:FUKUOKA   (今回作るすべてのキーで同一)
         Organization Name:o6asan’s Web Site
         Organizational Unit Name:myCA
         Common Name:o6asan.com   (ここは省略不可)
         Email Address:私のメールアドレス

         A challenge password: 省略
         An optional company name: 省略

  4. CA extensions を使って, certificate request の自己署名:
         >(drive x):(Apache bin dir)openssl ca -config (drive x):(Apache conf dir)openssl.cnf -selfsign -in careq.pem -extensions v3_ca -out cacert.pem

         Enter pass phrase for (drive xx):myCAprivatecakey.pem: xxxxxxxxxxxxxxxx   (cakey.pem 用の pass phrase)

         Sign the certificate? [y/n]: y
         1 out of 1 certificate requests certified, commit? [y/n] y

         >copy cacert.pem (drive x):(Apache conf dir)   (サーバ用の cacert.pem)
         >copy cacert.pem my_ca.crt   (クライアント用の my_ca.crt)

 server.key と server.crt を作る。

  1.      >(drive x):(Apache bin dir)openssl req -new -keyout server.key -out server.csr -config (drive x):(Apache conf dir)openssl.cnf

         Enter PEM pass phrase: xxxxxxxxxxxxxxxx   (server.key 用の pass phrase)
         Verifying – Enter PEM pass phrase: xxxxxxxxxxxxxxxx   (server.key 用の pass phrase)

         Country Name:JP   (今回作るすべてのキーで同一)
         State or Province Name:FUKUOKA   (今回作るすべてのキーで同一)
         Locality Name:FUKUOKA   (今回作るすべてのキーで同一)
         Organization Name:o6asan’s Web Site
         Organizational Unit Name:WordPress
         Common Name:o6asan.com   (ここは省略不可)
         Email Address:私のメールアドレス

         A challenge password: 省略
         An optional company name: 省略

  2. certificate request の署名:
         >(drive x):(Apache bin dir)openssl ca -in server.csr -out server.crt -config (drive x):(Apache conf dir)openssl.cnf

         Enter pass phrase for (drive xx):myCAprivatecakey.pem: xxxxxxxxxxxxxxxx   (cakey.pem 用の pass phrase)

         Sign the certificate? [y/n]: y
         1 out of 1 certificate requests certified, commit? [y/n] y

         >copy server.crt (drive x):(Apache conf dir)

  3. Win32 は SSLPassPhraseDialog builtin をサポートしていないので, server.key の pass phrase を復号しておかないと, Apache の起動時にエラーが出る。 getting rid of an error at Apache startup time (PDF 版)を参考にして, server.key を作り直す。

         >copy server.key cp_server.key
         >(drive x):(Apache bin dir)openssl rsa <cp_server.key> (drive x):(Apache conf dir)server.key

         Enter pass phrase: xxxxxxxxxxxxxxxx   (server.key 用の pass phrase)

 最後に clientcert.p12 を作る。これは,クライアント認証用である。

  1.      >(drive x):(Apache bin dir)openssl req -new -keyout client.key -out client.csr -config (drive x):(Apache conf dir)openssl.cnf

         Enter PEM pass phrase: xxxxxxxxxxxxxxxx   (client.key 用の pass phrase)
         Verifying – Enter PEM pass phrase: xxxxxxxxxxxxxxxx   (client.key 用の pass phrase)

         Country Name:JP   (今回作るすべてのキーで同一)
         State or Province Name:FUKUOKA   (今回作るすべてのキーで同一)
         Locality Name:FUKUOKA   (今回作るすべてのキーで同一)
         Organization Name:o6asan’s Web Site
         Organizational Unit Name:Administration
         Common Name:o6asan   (ここは省略不可)
         Email Address:私のメールアドレス

         A challenge password: 省略
         An optional company name: 省略

  2.      >(drive x):(Apache bin dir)openssl ca -in client.csr -out client.crt -config (drive x):(Apache conf dir)openssl.cnf

         Enter pass phrase for (drive xx):myCAprivatecakey.pem: xxxxxxxxxxxxxxxx   (cakey.pem 用の pass phrase)

         Sign the certificate? [y/n]: y
         1 out of 1 certificate requests certified, commit? [y/n] y

  3. クライアント用に pkcs12 フォーマットのファイルを作る。
         >(drive x):(Apache bin dir)openssl pkcs12 -export -in client.crt -inkey client.key -out clientcert.p12

         Enter pass phrase for client.key: xxxxxxxxxxxxxxxx   (client.key 用の pass phrase)
         Enter Export Password: xxxxxxxxxxxxxxxx   (クライアント PC 用のパスワード)
         Verifying – Enter Export Password: xxxxxxxxxxxxxxxx   (クライアント PC 用のパスワード)

    cmd.exe を閉じる。

 これで, Apache conf ディレクトリに cacert.pem, server.crt, server.key ができ,リムーバルメディア上の myCA 内には, my_ca.crt と clientcert.p12 がある。この5つがあれば,クライアント認証も使える。

 カレントの httpd.conf の以下の行をアンコメントする。

  • LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
  • LoadModule ssl_module modules/mod_ssl.so
  • Include conf/extra/httpd-ssl.conf

 Apache extra conf ディレクトリにある httpd-ssl.conf をカスタマイズする。
     Listen 443 —> Listen xxxxx   (443のままでも構わない)

     <VirtualHost _default_:443> —> <VirtualHost _default_:xxxxx>
     DocumentRoot “c:/Apache24/htdocs” —> DocumentRoot “自鯖のドキュメントルート”
     ServerName www.example.com:443 —> ServerName o6asan.com:xxxxx
     ServerAdmin admin@example.com —> ServerAdmin 私のメールアドレス
     ErrorLog “c:/Apache24/logs/error.log”
     —> ErrorLog “|bin/rotatelogs.exe -l -f logs/error.%Y.%m.%d 86400″ (自鯖のフォーマットで)
     TransferLog “c:/Apache24/logs/access.log”
     —> TransferLog “|bin/rotatelogs.exe -l -f logs/access.%Y.%m.%d 86400″ (自鯖のフォーマットで)

     #SSLCACertificateFile “c:/Apache24/conf/ssl.crt/ca-bundle.crt”
     —> SSLCACertificateFile “C:/Apache24/conf/cacert.pem”

     #SSLVerifyClient require —> SSLVerifyClient require
     #SSLVerifyDepth 10 —> SSLVerifyDepth 10 SSLVerifyDepth 1

     <Directory “c:/Apache24/cgi-bin”> —> <Directory “自鯖の cgi-bin ディレクトリ”>

 Apache を再起動。
 my_ca.crt と clientcert.p12 を WiFi 端末 のブラウザにインポート。

 あとは, WordPress のセッティングだが,こっちの方がやったことないので,大変そうだ。

追記(6/19):
 この後, SSL で WordPress にログインしようとしたら,即座に「複数サイト機能はポート番号を含まない URL のみで動作します」というメッセージが戻ってきた。仕方がないので, xxxxx にしていたポートをデフォルトに戻した。(^^;)