SSLおれおれ証明書とクライアント認証

おれおれ証明書

自宅サーバで個人用途でわざわざSSL証明書を買えないけど、外部からWebメールやPOP3sとかSMTPsを使いたい。そんなあなたには、これ!おれおれ証明書。そのまま自分で自分を証明する、俺だよ俺証明書です。クライアント認証ってのは、SSLでサーバを証明するのではなくクライアントを証明し認証する使い方。この組み合わせで安全な仕組みができる

どこのサイトを見てもCA作ったりしてめんどくさいと思うあなたには、これ最速!!OpenSSLで簡単に証明書を作成する方法たった2行のコマンドでおれおれ証明書ができちゃいます

CA認証局

電子証明書の設置場所

どこでもいいので、場所を決めます。

cd /etc/ssl
mkdir CA

OpenSSLのデフォルト値を変更しておく

同じ様な質問を何回も聞かれるのでデフォルト値を変更しておく。/etc/ssl/openssl.cnf

dir           = /etc/ssl/CA
countryName_default           = JP
stateOrProvinceName_default   = Tokyo
0.organizationName_default    = Kung Noi HomeServer
organizationalUnitName_default        = Kung Noi HomeServer
commonName_default              = nai.homelinux.net
emailAddress_default            = pom@nai.homelinux.net

各種ディレクトリ

必要なファイル、ディレクトリを作る

cat /dev/null > index.txt
echo '01' > serial
echo '01' > crlnumber
mkdir private
chmod 700 private
mkdir newcerts
mkdir crl
mkdir certs

秘密鍵

まず、秘密鍵を作ります

openssl genrsa -out private/cakey.pem 1024

証明書

秘密鍵を使って、証明書を作成。-daysオプションで有効期限を10年と設定する(※有効期限について)。

#openssl req -new -x509 -days 3560 -key private/cakey.pem -out cacert.pem
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) [JP]:
State or Province Name (full name) [Tokyo]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Kung Noi HomeServer]:
Organizational Unit Name (eg, section) [Kung Noi HomeServer]:
Common Name (eg, YOUR name) [nai.homelinux.net]:
Email Address [pom@nai.homelinux.net]:

サーバ証明書

サーバ証明書作成の流れはまず署名リクエスト(CSR)を作成し、それをおれおれCAで署名してできあがる。

場所

どこでもいいけど、CAに合わせて/etc/ssl/Serverにしとく

#mkdir /etc/ssl/Server

秘密鍵

CAどうようにまず、秘密鍵から作成

#openssl genrsa -out private.pem 1024

署名リクエスト作成

認証局に署名してもらう為に、まずリクエストを作成する必要がある

#openssl req -new -key private.pem -out request.pem
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) [JP]:
State or Province Name (full name) [Tokyo]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Kung Noi HomeServer]:
Organizational Unit Name (eg, section) [Kung Noi HomeServer]:
Common Name (eg, YOUR name) [nai.homelinux.net]:
Email Address [pom@nai.homelinux.net]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 何も入力せずエンター
An optional company name []: 何も入力せずエンター

Netscapeユーザへの心

Netscapeユーザを気にしないならここは無視してOK。でも以降のコマンドで「-config」のとこは省いて。

OpenSSLのコンフィグをコピー

#cp /etc/ssl/openssl.cnf ./.
# vi ./openssl.cnf

ここを編集

[ usr_cert ]
nsCertType                      = server

おれおれCAで署名

#openssl ca -config openssl.cnf -policy policy_anything -out ./cert-ca.pem -infiles request.pem 
Using configuration from openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: xxx xx xx:xx:xx xxxx GMT
            Not After : xxx xx xx:xx:xx xxxx GMT
        Subject:
            countryName               = JP
            stateOrProvinceName       = Tokyo
            organizationName          = Kung Noi HomeServer
            organizationalUnitName    = Kung Noi HomeServer
            commonName                = nai.homelinux.net
            emailAddress              = pom@nai.homelinux.net
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Cert Type:
                SSL Server
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
            X509v3 Authority Key Identifier:
                xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

Certificate is to be certified until xxx xx xx:xx:xx xxxx GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

メールサーバ用

メールでは証明書を確認しないんで、証明を抜き取りサーバ証明書だけとる。courier等で使用する為、サーバ証明書だけ抜き取る

#openssl x509 -in cert-ca.pem -out cert.pem

MUAはcourierを使うんだが、証明書と秘密鍵を混ぜた物を使うみたい。

#cat private.pem cert.pem > mail.pem

認証確認

作成したサーバ証明書とCA証明書が正常に作成できたか確認してみる

#openssl verify -CAfile /etc/ssl/CA/cacert.pem cert-ca.pem
cert-ca.pem: OK
#openssl verify -CAfile /etc/ssl/CA/cacert.pem cert.pem
cert.pem: OK

ここで稼動確認する

この段階でHTTPSなどで確認してみる。ApacheのSSL設定を行い、HTTPSができることを確認しておこう。

組み込み用の証明書作成

おれおれ証明書の為、ブラウザが毎回警告を発するので、信じてもらえるようにCA証明書をDER形式で吐き出しブラウザに組み込む

#openssl x509 -inform pem -in cacert.pem -out cacert.der -outform der

ブラウザ組み込み手順

ブラウザの組み込み手順はIEとFirefoxの手順を作ったので参照してください

クライアント証明書

ユーザ名をpomとした場合の作成方法です。場所は/etc/ssl/Usersとします

秘密鍵作成

他同様にまず秘密鍵を作成

#openssl genrsa -out private.pem 1024

署名リクエスト作成

サーバと同様にCAに署名してもらう為のリクエストを作成する

#openssl req -new -key private.pem -out request.pem
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) [JP]:
State or Province Name (full name) [Tokyo]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Kung Noi HomeServer]:
Organizational Unit Name (eg, section) [Kung Noi HomeServer]:user 解りやすくするため、userとする
Common Name (eg, YOUR name) [nai.homelinux.net]:pom ユーザ名
Email Address [pom@nai.homelinux.net]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 何も入力せずエンター
An optional company name []: 何も入力せずエンター

Netscapeユーザへの心

上のサーバ証明書と同様。Netscape用。

OpenSSLのコンフィグをコピー

#cp /etc/ssl/openssl.cnf ./.

# vi ./openssl.cnf

ここを編集

[ usr_cert ]
nsCertType                      = client, email

証明書作成

認証局に署名してもらいクライアント証明書を作成

#openssl ca -config openssl.cnf -out cert.pem -infiles request.pem
Using configuration from openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 3 (0x3)
        Validity
            Not Before: xxx xx xx:xx:xx xxxx GMT
            Not After : xxx xx xx:xx:xx xxxx GMT
        Subject:
            countryName               = JP
            stateOrProvinceName       = Tokyo
            organizationName          = Kung Noi HomeServer
            organizationalUnitName    = user
            commonName                = pom
            emailAddress              = pom@nai.homelinux.net
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Cert Type:
                SSL Client, S/MIME
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
            X509v3 Authority Key Identifier:
                keyid:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

Certificate is to be certified until xxx xx xx:xx:xx xxxx GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

組み込み用ファイルの作成

秘密鍵と証明書をまとめて、pkcs#12形式とういうものにする

#openssl pkcs12 -export -in cert.pem -inkey private.pem -certfile /etc/ssl/CA/cacert.pem -name nai.homelinux.net -caname nai.homelinux.net -out pom.p12
Enter Export Password: 組み込み時のパスワードを設定する
Verifying - Enter Export Password: 確認

ブラウザ組み込み手順

ブラウザの組み込み手順はIEとFirefoxの手順を作ったので参照してください

有効期限について

CA証明書の有効期限は10年ほどの長い期間にし、サーバ証明書をすこし短めの1年とします。そうすることで、クライアントにインポートした証明書は10年間変更する必要が無く、サーバ証明書は1年毎に再作成しなくてはならいのですが、これはセキュリティ上必要なことです。

コマンドオプションと設定ファイルの日付について

  • openssl req -new x509 〜の場合

    デフォルトの30日が有効になる。指定するなら-daysを使用します。

  • openssl ca 〜の場合

    設定ファイルのdefault_daysが有効になる。

  • openssl ca -gencrl 〜の場合

    設定ファイルのdefault_crl_daysが有効になる。

サーバ証明書の有効期限切れちゃたー

有効期限がきれたら、更新してやろう。

古いのを無効にする

#openssl ca -config openssl.cnf -revoke cert-ca.pem
Using configuration from ./openssl.cnf
DEBUG[load_index]: unique_subject = "yes"
Revoking Certificate 01.
Data Base Updated

あとは一緒

これら後のやることは、上のサーバ証明書作成からと同じ手順

まとめ

これで、すべて終了。あとは、ApacheのSSL設定を参考にし、クライアント認証の設定を行い認証を確認しよう。後でファイル一覧を書きます。


debian Valid HTML 4.01 Strict [VALID RSS!]