OpenSSL - ぷちWiki

最終更新日時:

OpenSSL とは



OpenSSL は SSL (Secure Socket Layer) を使用して通信を暗号化するためのライブラリ&コマンド群です。
OpenSSH や Apache 等の Web サーバで https 通信を実現する際に必要となります。

インストール



OpenSSL-0.9.7c の例です。

まず OpenSSL 公式サイトよりアーカイブをダウンロードし、展開します。

 % gzip -dc openssl-0.9.7c.tar.gz | tar xvf -
 % cd openssl-0.9.7c

展開先のディレクトリで以下のように実行すれば完了です。
config 時に shared オプションを指定すると共有ライブラリを作成します。

 % ./config shared
 % make
 % make test
 % su
 # make install

CSR の作成方法



Verisign 等の証明書を作成する際には CSR が必要となります。
CSR を作成するにはまず、秘密鍵を作成する必要があります。
下記の例では、疑似乱数を使用して秘密鍵を作成しています。

 % openssl md5 * /usr/local/ssl/bin/* > rand.dat
 % openssl genrsa -rand rand.dat -des3 1024 > server.key

上記の秘密鍵はパスフレーズを含んでいますが、何かと不便ですのでパスフレーズ無しの秘密鍵に変更します。

 % openssl rsa -in server.key -out server2.key

次に秘密鍵から CSR を作成します。
コマンド実行後、適切に情報を入力します。

 % openssl req -new -key server2.key -out server.csr

その他確認用コマンド



秘密鍵の内容を確認

 # openssl rsa -in xxx.key -text

CSRの内容を確認

 # openssl req -in xxx.csr -text

証明書の内容を確認

 # openssl x509 -in xxx.cer -text


自前の証明書作成方法



証明書は通常 Verisign のような信用できる機関にて発行してもらうのですが、これを自前で作成してしまう方法です。
もちろん信用できない機関が発行している証明書ですから、https の証明書として使用した場合、たいていのブラウザは警告を発します。
が、信用できないだけで https 通信は可能ですので、内部的なサービスや暫定的な処置として重宝します。

まず、自前の認証局を作成します。
適当なディレクトリに移動し、以下を実行します。

 % /usr/share/ssl/misc/CA -newca 

すると、以下のようにいくつか聞いてくるので、適当に設定します。

 CA certificate filename (or enter to create)
 
 Making CA certificate ...
 Using configuration from /usr/share/ssl/openssl.cnf
 Generating a 1024 bit RSA private key
 ............................++++++
 .......++++++
 writing new private key to './demoCA/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]:JA
 State or Province Name (full name) [Some-State]:Kanagawa
 Locality Name (eg, city) []:Yokohama-shi
 Organization Name (eg, company) [Internet Widgits Pty Ltd]:koumei2
 Organizational Unit Name (eg, section) []:
 Common Name (eg, your name or your server's hostname) []:lightly.plala.jp
 Email Address []:

これで、カレントディレクトリに demoCA というディレクトリが作成され、そこに認証局が作成されています。
次に、証明書を作成します。
CSR を作成し、以下を実行します。

 % openssl x509 -in server.csr -req -signkey server.key -out server.crt

OpenSSL の一覧