openssl (ubuntu)

もっとも一般的にはWEB BROEWSERと結合して使われる。
X.509 --- OpenSSLはこれを使う。
openssl,libssl0.9.8(apt-cache-search libssl | grep SSLで現れたもののうち最初のlibssl)とca-certificates,libssl-devをインストール。
basic commands

ssl certificates(証明書)

自己署名のものと世界的認証局により署名されたものがあり,一度署名されたものはopenssl toolkitで使える。

SSL certificates for server use

x.509準拠のSSL certificate(証明書)を作ったら
  1. 自らサインするか(Certificate Authority:CAを作って)
  2. Globally recognized authority signしてもらうか、できる
サインが終わったらこれは
  1. openssl toolkitで使用できるかあるいは
  2. 暗号化SSL接続(HTTPサーバーなど)を可能とするライブラリーが使用できる。
以下両者の説明

generating and Signing Self-Signed Certificate

完全にフリー、欠点は自己署名の証明書を使うSSLーSECUREDサーバーにアクセスするとき警告がでること。デフォルトではクライアントアプリケー ション(firefoxなど)は世界的認証局により署名された証明書に対する警告のみ抑えるが、サーバーのルート証明書をクライアントアプリケーションに イ ンポートした時にもsquelch(押しつぶす)できる(後出)。
openssl toolkitをインストール済みなら X.509 SSL 証明書を作るのは簡単である。自己署名証明書のためにはまずCA(証明書認証局=認証局)を以下のステップにしたがって確立する。

creating the certificate Authority

まず初期作業環境
cd &'& mkdir -p myCA/signedcerts && mkdir myCA/private && cd myCA
次に~/myCAに以下のコマンドでinitial certificate databaseを作る
echo '01' > serial && touch index.txt
~/myCA/caconfig.cnf CA証明書作成用ファイルを編集


# My sample caconfig.cnf file.
#
# Default configuration to use when one is not provided on the command line.
#
[ ca ]
default_ca      = local_ca
#
#
# Default location of directories and files needed to generate certificates.
#
[ local_ca ]
dir             = /home/********/myCA
certificate     = $dir/cacert.pem
database        = $dir/index.txt
new_certs_dir   = $dir/signedcerts
private_key     = $dir/private/cakey.pem
serial          = $dir/serial
#       
#
# Default expiration and encryption policies for certificates.
#
default_crl_days        = 365
default_days            = 1825
default_md              = md5
#       
policy          = local_ca_policy
x509_extensions = local_ca_extensions
#       
#
# Default policy to use when generating server certificates.  The following
# fields must be defined in the server certificate.
#
[ local_ca_policy ]
commonName              = supplied
stateOrProvinceName     = supplied
countryName             = supplied
emailAddress            = supplied
organizationName        = supplied
organizationalUnitName  = supplied
#       
#
# x509 extensions to use when generating server certificates.
#
[ local_ca_extensions ]
subjectAltName          = DNS:$$$$$$$$.myvnc.com
basicConstraints        = CA:false
nsCertType              = server
#       
#
# The default root certificate generation policy.
#
[ req ]
default_bits    = 2048
default_keyfile = /home/********/myCA/private/cakey.pem
default_md      = md5
#       
prompt                  = no
distinguished_name      = root_ca_distinguished_name
x509_extensions         = root_ca_extensions
#
#
# Root Certificate Authority distinguished name.  Change these fields to match
# your local environment!
#
[ root_ca_distinguished_name ]
commonName              = ********
stateOrProvinceName     = Fukuoka
countryName             = JA
emailAddress            = ********67@hotmail.com
organizationName        = ********Clinic
organizationalUnitName  = physician
#       
[ root_ca_extensions ]
basicConstraints        = CA:true

~/myCA/caconfig.cnfとして保存。
つぎにcertificate authority root certificate and key(認証局ルート証明書と鍵)を作る。
export OPENSSL_CONF = ~/myCA/caconfig.cnf
これでopenssl toolに  OPENSSL_CONF = ~/myCA/caconfig.cnf を探すようにさせる。
そしてCA certificagte and key を
openssl req -x509 -newkey rsa:2048 -out cacert.pem -outform PEM -days 1825
で生じる。
generating a 2048 bit RSA privte key
----------------------
writing new private key to /home/********/myCA/private cakey.pem (default_keyfile
Enter PEM pass phrase: ******
Verifying  - Enter PEM pass phrase: 
-----
(pass phrase を忘れてはいけない。毎回使用する)

上記プロセスでPEMでフォーマットしたRSAの public/private key 暗号化を使った自己署名証明書を作り、場所は

Creating a Self-signed Server Certificate

こうしてcertificate authority(認証局)を構成できたら自己署名証明書にサインするのに使用できる。
以下のステップの前に証明書のprivate keyをパスフレーズで暗号化できる。

SSLを使えるなアプリケーションでそのスタート前にパスフレーズを入れなければ証明書は使用できない状態となるが、これはサーバーがコンピュータの再起 動時に未処理の状態でスタートしていなければ、パスフレーズに誰も到達できないという問題が生ずる。securityの trade-off (取引)で除外してある。

/etc/hosts に 自分のドメイン名を追記して

~/myCA/exampleserver.cnf

#
# exampleserver.cnf
#

[ req ]
prompt                  = no
distinguished_name      = server_distinguished_name

[ server_distinguished_name ]
commonName              = 自分のドメイン名
stateOrProvinceName     = Fukuoka
countryName             = JA
emailAddress            = 自分のメール宛先
organizationName        = ********Clinic
organizationalUnitName  = physician

特にcommonName値はhost name値とマッチせねば host/certificate mismatchがサーバーアクセス時に起きる。

server certificate & keyを以下で作る。

export OPENSSL_CONF = ~/myCA/exampleserver.cnf

openssl req -newkey rsa:1024 -keyout tempkey.pem -keyform PEM -out tempreq.pem -outform PEM
generating a 1024 bit RSA private key
--------------
--------------
writing new private key to tempkey.pem
Enter PEM passphrase: ******
verifying - Enter PEM passphrase :******
パスフレーズを忘れない
そしてtemporary private key を 非暗号化鍵に
openssl rsa < tempkey.pem > server_key.pem
パスフレーズ入力をプロンプトされ、以下の出力
Enter passphrase: ******
writing RSA key
keyを暗号化されたままにしたかったら以下を上記のステップの代わりにする。
mv tempkey.pem server_key.pem

server_keyをパスフレーズ暗号化したまま使うと、暗号化鍵を用いたサーバーアプリケーションが開始する度にパスフレーズ入力を要する。(誰かが KEYを入力せねばスタートしないということ)

ここでサーバー証明書にCA鍵でサインする。(以下)
export OPENSSL_CONF = ~/myCA/caconfig.cnf

openssl ca -in tempreq -out server_crt.pem
パスフレーズを要求されexampleserver.cnf  の情報を確認するようプロンプトされサインを確認させられる。出力は
Using configuration from /home/********/myCA/caconfig.cnf
Enter passphrase for /home/********/myCA/private cakey.pem/******
Check that the request match the signature
Signature OK
The Subject's Distinguished Name is as follows
commonName
stateOrProvinceName
countryName
emailAddress
organizationName organizationalUnitName
Certificate is to be certified until jan 4....(1825days)
sign the certificate?[y/n]:y
1 out of 1 certificate requests certified, commit?[y/n]:y
write out daatbase with 1 new entries
Database updated

temporary certificateとkey files を削除
rm -f tempkey.pem && rm -f tempreq.pem
Congraturation!

Optional Step for Certain Server Applications


Converting X.509 Certificate to PKCS#12 for Client Applications

クライアント使用のためサーバーのルートCA X.509からPKCS#12証明書を作るためには以下。
certificate&key(両者)を含むファイルを作る。
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
このcertificate/key combination をPKCS#12 certificateに以下のコマンドで
openssl pcks12 -export -out mycert.pfx -in mycert.pem -name "Certificate for whatever"
:Export password (blankでよい)
結果としてのmycert.pfx fileはweb browserやEmailにinportされ得る。
この証明書はルートCA証明書(自分のサーバーの)を表すので、CA証明書でサインされたsubsequentのサーバーレベル証明書はまた、この PCKS#12versionの証明書をインストールしたcliant applicationでも自動的にacceptされる。

Generating Certificate for Signing by Recognized CA


Using PCKS#12 Certificate in Client Applications

PCKS#12証明書をクライアントの視点から説明
証明書をFirefox Web Browser(や他のアプリケーション)中にインポートするプロセスを示す。
これらのアプリケーション中にルート証明書をインポートすることでどこからの証明書であろうとサーバーに信頼されたSSL暗号接続できる。(自己署名した 証明書にたいするやかましいメッセージ無しで)。

Importing a Certificate into Mozilla Firefox

PCKS#12 certificateをMozillaFirefox WEb Browser にインポートするのは簡単
  1. Firefox から Edit --> Preference(編集ー>設定)
  2. Advanced icon(詳細)
  3. View Certificate button(証明書を表示)
  4. Import button
  5. Certificate fileの場所までbrowseする(.pfx file typeが典型的)
  6. certificate fileをhighlightしopen button click
  7. Password promptされるかもしれない。 -->enter, confirm,click OK
  8. certificate Export password 入力がプロンプトされたらそれを入力するか泣ければOK
  9. message" Successfully restored your security certificate(s) and private key(s)"
  10. click OK
  11. これでサーバーのclient PCKS#12 certificate(mycert.pem) をfirefoxにインポートした。

Importing a Certificate into Evolution


Importing a Certificate into Mozilla Thunderbird


Importing a Certificate into the System-wide Certificate Authority Datebase

CA証明書を信頼された証明局のsystem-wideデータベースにインポートできる。このデータベースを使うアプリケーションは自動的にここにある証 明書のすべてを信頼する。
  1. 証明書をコピー(端末で)
  2. /etc/ca-certificates.cnf (ca-certificates configuration)を編集。  /usr/share/ca-certificates にコピーしたファイル名をfinal"#"のすぐ後のリストのトップに付加する。
    1. for example
      # This file lists certificates that you wish to use or to ignore to be installed in /etc/ssl/certs
      # update-ca-certificate(8) will update /etc/ssl/certs by reading this file.
      #
      # This is autogenerated by dpg-reconfigure ca-certificates.certificates should be installed under /usr/ca-certificate.and files with extension '.crt' is recognized as available certs.
      #
      # line begins with # is comment.line begins with ! is certificates filename to be deselected.
      #
      mycert.crt
      brasil.gov.br/brasil.gov.br.crt
      cacert.org/cacert.org.crt
      mozilla/ABAecom_=sub._Am._....
      [.....many additional certificates omitted]
  3. $ sudo update-ca-certificate
  4. systemCA証明書データベースに格入した。

Apache ssl