Tomcat-SSL環境を構築する手順メモ

開発環境でTomcatのみでSSL環境を構築する際の手順を忘れそうなのでメモしておく。

まず公開鍵と非公開鍵の鍵ペアを生成し、キーストア(.keystore)へ格納します。(別名はtomcatにしています)

$ keytool -genkey -alias tomcat -keyalg RSA
Enter keystore password:  changeit
What is your first and last name?
  [Unknown]:  aknot.jp
What is the name of your organizational unit?
  [Unknown]:  
What is the name of your organization?
  [Unknown]:  kouichi
What is the name of your City or Locality?
  [Unknown]:  Shinjuku-ku
What is the name of your State or Province?
  [Unknown]:  Tokyo
What is the two-letter country code for this unit?
  [Unknown]:  JP
Is CN=aknot.jp, OU=Unknown, O=kouichi, L=Shinjuku-ku, ST=Tokyo, C=JP correct?
  [no]:  yes

Enter key password for <tomcat>
        (RETURN if same as keystore password):  

パスワードを訊かれるので、デフォルトのchangeitを指定しています。

完了後、初めてキーストアを作ったので、.keystoreが作成されたことを確認します。

$ ls -alF /Users/kouichi/.keystore 
-rw-r--r--  1 kouichi  staff  1357  2 27 12:38 /Users/kouichi/.keystore

キーストア内にある証明書の一覧を署名付きで表示して、格納されていることを確認します。

$ keytool -list -v
Enter keystore password:  changeit

Keystore type: jks
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: tomcat
Creation date: Feb 27, 2009
Entry type: keyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=aknot.jp, OU=Unknown, O=kouichi, L=Shinjuku-ku, ST=Tokyo, C=JP
Issuer: CN=aknot.jp, OU=Unknown, O=kouichi, L=Shinjuku-ku, ST=Tokyo, C=JP
Serial number: 49a76011
Valid from: Fri Feb 27 12:37:53 JST 2009 until: Thu May 28 12:37:53 JST 2009
Certificate fingerprints:
         MD5:  EF:A1:56:7B:59:4A:95:48:8E:04:DC:8C:18:33:DD:F4
         SHA1: C9:1F:76:6E:51:DC:60:C1:38:F0:1C:56:61:87:08:77:61:94:7A:D8


*******************************************
*******************************************

次に、Tomcatの設定ファイル(server.xml)を変更します。

<-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
<!--
<Connector 
           port="8443" minProcessors="5" maxProcessors="75"
           enableLookups="true" disableUploadTimeout="true"
           acceptCount="100" debug="0" scheme="https" secure="true";
           clientAuth="false" sslProtocol="TLS"/>
-->

となっている行を、以下のように変更します。(アンコメントアウトします)

<-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
<Connector 
           port="8443" minProcessors="5" maxProcessors="75"
           enableLookups="true" disableUploadTimeout="true"
           acceptCount="100" debug="0" scheme="https" secure="true";
           clientAuth="false" sslProtocol="TLS"/>

最後に Tomcatを再起動します。

信頼された機関にも署名されたものでないので、ブラウザからのアクセスした際には警告が表示されます。これでHTTPS通信ができる環境が構築されたことになります。