『PostgreSQL で OpenSSL を使うための設定手順』 2004-10-11(訂1) 2001-05-10 桑村 潤 ここで説明している手順はマニュアルに書いてあります。ssl-tcp.html をご覧ください。 まず、OpenSSL をインストールしてある環境で、PostgreSQL を --with-openssl で構成してインストールしておくことを忘れないで ください。 1. 自己署名の証明書をつくります。 # 1. Create a quick self-signed certificate # the local host name as Common Name; # the challenge password can be left blank 以下のように質問に答えます。"Common Name" にマシンのホスト名、 "challenge password" には何も入れないでリターンします。 > openssl req -new -text -out cert.req -keyout privkey.pem <= Using configuration from /etc/ssl/openssl.cnf Generating a 1024 bit RSA private key .....++++++ ..++++++ writing new private key to 'privkey.pem' Enter PEM pass phrase: XXXXXXXXXXXXXXXXXXX <= Verifying password - Enter PEM pass phrase: XXXXXXXXXXXXXXXXXXX <= ----- 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]:Yokohama <= Locality Name (eg, city) []:Aoba <= Organization Name (eg, company) [Internet Widgits Pty Ltd]:JPUG <= Organizational Unit Name (eg, section) []:Plamo <= Common Name (eg, YOUR name) []:penpen <= ホスト名を Email Address []:juk@yokohama.email.ne.jp <= Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: <= なし An optional company name []: <= 2. パスフレーズを取り去ります。 # 2. Remove the passphrase # (as you must if you want automatic start-up of the postmaster) # enter the old passphrase > openssl rsa -in privkey.pem -out cert.pem read RSA key Enter PEM pass phrase: <= 1.で使ったパスフレーズを入力 writing RSA key 3. 証明書を自己署名の証明書に変換します。 # 3. Turn the certificate into a self-signed certificate > openssl req -x509 -in cert.req -text -key cert.pem -out cert.cert Using configuration from /etc/ssl/openssl.cnf ここまででつくられたファイルは: > ls -lt total 56 -rw-r--r-- 1 postgres pgsql 3447 4月 21日 22:33 cert.cert -rw-r--r-- 1 postgres pgsql 887 4月 21日 22:32 cert.pem -rw-r--r-- 1 postgres pgsql 963 4月 21日 22:32 privkey.pem -rw-r--r-- 1 postgres pgsql 2167 4月 21日 22:32 cert.req ..... 4. 鍵と証明書を postmaster が読みにゆく場所にコピーします。 # 4. copy the key and certificate to where the postmaster will look for > cp cert.pem $PGDATA/server.key > cp cert.cert $PGDATA/server.crt > chmod 400 $PGDATA/server.key $PGDATA/server.crt デフォルトだと $PGDATA で参照される場所: > ls -lt $PGDATA/ total 48 -r-------- 1 postgres pgsql 3447 4月 21日 22:34 server.crt -r-------- 1 postgres pgsql 887 4月 21日 22:34 server.key ..... 5. postgresql.conf に SSL を利用するための変更を加えます。 # 5. change postgresql.conf for using ssl このファイルは通常、 $PGDATA/postgresql.conf に存在します。 以下は postgresql.conf 変更の差分: (※ PostgreSQL-8.0 では、tcpip_socket = true のかわりに、 listen_addresses = '*' のように設定するのでご意ください。) > diff -u $PGDATA/postgresql.conf.bak $PGDATA/postgresql.conf --- /opt/pgsql/data/postgresql.conf.bak Sat Apr 21 22:35:21 2001 +++ /opt/pgsql/data/postgresql.conf Sat Apr 21 22:35:21 2001 @@ -28,6 +28,8 @@ # #tcpip_socket = false #ssl = false +tcpip_socket = true +ssl = true #max_connections = 32 # 1-1024 6. pg_hba.conf に hostssl タイプの接続を記述します。 # 6. add entry for hostssl type connection このファイルは通常、 $PGDATA/pg_hba.conf に存在します。 以下は pg_hba.conf 変更の差分: (※ pg_hba.conf の設定書式はPostgreSQLのバージョンによって 異なりますので、御注意ください。) > diff -u $PGDATA/pg_hba.conf.bak $PGDATA/pg_hba.conf --- /opt/pgsql/data/pg_hba.conf.bak Sat Apr 21 22:37:15 2001 +++ /opt/pgsql/data/pg_hba.conf Sat Apr 21 22:37:15 2001 @@ -186,3 +186,5 @@ # CAUTION: if you are on a multiple-user machine, the above default # configuration is probably too liberal for you --- change it to use # something other than "trust" authentication. + +hostssl all 192.168.2.0 255.255.255.0 trust