2 tierプライベート認証局を作る

(2015.7.20)

(2018.2) 最近の状況にアップデート. ページが長くなったので分割。

Root CA, Issuing CAを分離したオレオレ認証局 (CA) を作ります。そのうえで、クライアント証明書、サーバ証明書を作ってみます。

このページでは, ルートCA, 中間CAを作るところまで解説します。サーバ証明書は次回。

PKI: CA階層, 2/3 tierと基礎 を併用してください。CA.plスクリプトの解説はこちらです。

参考:

ルートCAの自己署名証明書を作る

(2018.2) 更新.

まず、ルートCAの自己署名証明書から。

環境: Fedora 26 Linux

設定ファイルを作る. /etc/pki/tls/openssl.cnf をコピーし, root-ca.cnf とでも名付ける.

[ v3_ca ]
basicConstraints = critical,CA:true
keyUsage         = critical, cRLSign, keyCertSign, digitalSignature

root になって、適当な場所でCA.pl スクリプトを走らせます。

# OPENSSL_CONFIG="-config root-ca.cnf" /usr/bin/CA.pl -newca
CA certificate filename (or enter to create)
改行 (証明書+秘密鍵)のPEMファイルを指定した場合は, ファイルをコピーして終わり.
Making CA certificate ...
====
openssl req -config root-ca.cnf -new -keyout /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/careq.pem
Generating a 2048 bit RSA private key
........................+++
.......+++
writing new private key to '/etc/pki/CA/private/cakey.pem'   秘密鍵の生成
Enter PEM pass phrase:秘密鍵を守るパスワード
Verifying - 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) [XX]:JP
State or Province Name (full name) []:Fukuoka
Locality Name (eg, city) [Default City]:Munakata
Organization Name (eg, company) [Default Company Ltd]:Netsphere Laboratories
Organizational Unit Name (eg, section) []:改行 OUは空で構わない
Common Name (eg, your name or your server's hostname) []:Netsphere Laboratories Root CA 2 CNが証明書ストアなどで表示される
Email Address []:hisashi.horikawa@gmail.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:改行
An optional company name []:改行
==> 0
====
====
openssl ca -config root-ca.cnf -create_serial -out /etc/pki/CA/cacert.pem -days 1095 -batch -keyfile /etc/pki/CA/private/cakey.pem -selfsign -extensions v3_ca -infiles /etc/pki/CA/careq.pem
Using configuration from root-ca.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:先ほどのパスワード
Check that the request matches the signature
Signature ok   秘密鍵で署名し, 証明書を作成
Certificate Details:
        Serial Number:
            e7:61:82:a2:d0:e2:04:df
        Validity
            Not Before: Feb 26 15:00:06 2018 GMT
            Not After : Feb 25 15:00:06 2021 GMT
        Subject:
            countryName               = JP
            stateOrProvinceName       = Fukuoka
            organizationName          = Netsphere Laboratories
            commonName                = Netsphere Laboratories Root CA 2
            emailAddress              = hisashi.horikawa@gmail.com
        X509v3 extensions:
            X509v3 Subject Key Identifier: 
                7F:FC:E6:94:2E:FC:12:24:61:36:63:94:C8:75:C5:1D:74:DF:B9:5C
            X509v3 Authority Key Identifier: 
                keyid:7F:FC:E6:94:2E:FC:12:24:61:36:63:94:C8:75:C5:1D:74:DF:B9:5C

            X509v3 Basic Constraints: critical
                CA:TRUE   認証局になっていることを確認.
            X509v3 Key Usage: critical
                Digital Signature, Certificate Sign, CRL Sign 設定ファイルから
Certificate is to be certified until Feb 25 15:00:06 2021 GMT (1095 days)

Write out database with 1 new entries
Data Base Updated
==> 0
====
CA certificate is in /etc/pki/CA/cacert.pem  証明書ファイル

実行前のディレクトリ

# find /etc/pki/CA/
/etc/pki/CA/
/etc/pki/CA/private
/etc/pki/CA/certs
/etc/pki/CA/crl
/etc/pki/CA/newcerts

実行後は、次のようにファイルが生成されています。

# find /etc/pki/CA
/etc/pki/CA
/etc/pki/CA/private
/etc/pki/CA/private/cakey.pem   Root CAの秘密鍵
/etc/pki/CA/careq.pem
/etc/pki/CA/certs
/etc/pki/CA/cacert.pem     Root CAの証明書
/etc/pki/CA/index.txt.attr
/etc/pki/CA/serial
/etc/pki/CA/crl
/etc/pki/CA/index.txt.old
/etc/pki/CA/index.txt
/etc/pki/CA/newcerts
/etc/pki/CA/newcerts/A38D896E37349F44.pem

証明書の内容を表示するには、次のようにします。一般ユーザでも表示できます。

$ openssl x509 -in /etc/pki/CA/cacert.pem -text

証明書が次のようになっていることが分かります;

  • Issuer (署名した人) と Subject が同じになっている
  • X509v3 Basic Constraints が「CA:TRUE」

中間CAを作る (TLS用)

中間CAのための鍵ペア (公開鍵と秘密鍵) を作り、公開鍵は先ほどのルート認証局に署名してもらい、証明書を作る。

中間CAからさらにCAを作れないように, CA:TRUE, pathlen = 0 にします。

この文書の 4.2.1.9. Basic Constraints にあります。

中間CAの秘密鍵

まず秘密鍵を作ります。

intermediate-ca$ openssl genrsa 2048 > my_intermediate_ca.private.pem
Generating RSA private key, 2048 bit long modulus
....................................+++
......................+++
e is 65537 (0x10001)

このファイルは「-----BEGIN RSA PRIVATE KEY-----」で始まるテキストファイルです。(PEM形式)

Web上には非常に混乱した解説が多くありますが、PEM形式は単に「-----BEGIN ラベル-----」で始まり「-----END ラベル-----」で終わるテキストファイルのファイル形式です。その中身が何であるかは, ラベルで決まります。秘密鍵に限定されるわけではありません。

秘密鍵の内容を表示するには, 次のようにします。

$ openssl rsa -text < my_intermediate_ca.private.pem
Private-Key: (2048 bit)
modulus:
    00: 以下略
publicExponent: 略
privateExponent:
    60: 以下略
素数が続く

秘密鍵から公開鍵を作るのは、こうします;

intermediate-ca$ openssl rsa -pubout < my_intermediate_ca.private.pem

署名要求 (CSR) を作る

秘密鍵から公開鍵を作り、Root CAに対して, その公開鍵に署名してもらうための署名要求 (CSR) を作る.

ここで入力するのは、中間CAの情報。

~/my-ca$ openssl req -new -key my_intermediate_ca.private.pem > my_intermediate_ca.csr.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) [XX]:JP
State or Province Name (full name) []:FUKUOKA
Locality Name (eg, city) [Default City]:Munakata
Organization Name (eg, company) [Default Company Ltd]:Netsphere Laboratories
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:Netsphere Laboratories Intermediate CA
Email Address []:hisashi.horikawa@gmail.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

CSRは「-----BEGIN CERTIFICATE REQUEST-----」で始まります。

内容の表示はこうします。CSRには, 公開鍵だけが含まれます。

$ openssl req -text < my_intermediate_ca.csr.pem
Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=JP, ST=FUKUOKA, L=Munakata, O=Netsphere Laboratories, CN=Netsphere Laboratories Intermediate CA/emailAddress=hisashi.horikawa@gmail.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00: 以下略

別解: 秘密鍵とCSRを同時に作る

先ほどは秘密鍵から署名要求を作ったが、何もないところから, 秘密鍵を作るのと同時にCSRを作るやり方;

~/my-ca$ /etc/pki/tls/misc/CA -newreq
Generating a 2048 bit RSA private key
.................................................................+++
......+++
writing new private key to 'newkey.pem'
Enter PEM pass phrase:           秘密鍵のパスフレーズを決める
Verifying - 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) [XX]:JP
State or Province Name (full name) []:FUKUOKA
Locality Name (eg, city) [Default City]:Munakata
Organization Name (eg, company) [Default Company Ltd]:Netsphere Laboratories
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:Netsphere Laboratories Intermediate CA 2
Email Address []:hisashi.horikawa@gmail.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Request is in newreq.pem, private key is in newkey.pem

この方法では、カレントディレクトリにパスワード保護された秘密鍵 newkey.pem と署名要求 newreq.pem ができる。

Root CAが、秘密鍵で署名

ルートCAが 署名要求 (CSR) を受け取ったとして, 秘密鍵で署名し、証明書を作る。

/etc/pki/tls/openssl.cnf をコピーして修正する。変更は[ v3_ca ]セクションだけで大丈夫。

重要 前述のとおり, pathlen:0 にするのが重要. もう一段CAを作るときは pathlen:1

[ v3_ca ]

basicConstraints = critical,CA:true, pathlen:0

# Key usage: this is typical for a CA certificate. However since it will
# prevent it being used as an test self-signed certificate it is best
# left out by default.
keyUsage = critical, cRLSign, digitalSignature, keyCertSign

extendedKeyUsage = critical, serverAuth, clientAuth, emailProtection

# Some might want this also
nsCertType = sslCA, emailCA

extendedKeyUsage で新しく作るCAの目的を制限する。上の例はTLS用. 中間CAがこの目的以外で証明書を作っても、検証に失敗する。

コード署名のためのCAの場合は, 次のようにする;

extendedKeyUsage = critical, codeSigning

Unix rootになって、CA.plスクリプトを走らせる。ここでは-signCAを使う。

CSRのファイル名は newreq.pem でなければならない. OPENSSL_CONFIG環境変数で -in オプションを与えても、ハードコードされているため, うまく動かない.

# OPENSSL_CONFIG="-config intermediate-tls-ca.cnf -days 1095" /usr/bin/CA.pl -signCA
====
openssl ca -config intermediate-tls-ca.cnf -days 1095 -policy policy_anything -out newcert.pem -extensions v3_ca -infiles newreq.pem
Using configuration from intermediate-tls-ca.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:Root CA秘密鍵のパスワード
Check that the request matches the signature
Signature ok
Certificate Details:   今から作る証明書の内容
        Serial Number:
            e7:61:82:a2:d0:e2:04:e0
        Validity
            Not Before: Feb 27 13:29:55 2018 GMT
            Not After : Feb 26 13:29:55 2021 GMT
        Subject:
            countryName               = JP
            stateOrProvinceName       = Fukuoka
            localityName              = Munakata
            organizationName          = Netsphere Laboratories
            commonName                = Netsphere Laboratories TLS CA
            emailAddress              = hisashi.horikawa@gmail.com
        X509v3 extensions:
            X509v3 Subject Key Identifier: 
                BE:CC:6A:D8:8D:D3:ED:4C:C1:52:77:5A:38:2F:41:9E:B8:8B:A1:BA
            X509v3 Authority Key Identifier: 
                keyid:7F:FC:E6:94:2E:FC:12:24:61:36:63:94:C8:75:C5:1D:74:DF:B9:5C

            X509v3 Basic Constraints: critical
                CA:TRUE, pathlen:0
            X509v3 Key Usage: critical
                Digital Signature, Certificate Sign, CRL Sign
            X509v3 Extended Key Usage: critical
                TLS Web Server Authentication, TLS Web Client Authentication, E-mail Protection
            Netscape Cert Type: 
                SSL CA, S/MIME CA
Certificate is to be certified until Feb 26 13:29:55 2021 GMT (1095 days)
Sign the certificate? [y/n]:y  これで, 署名される


1 out of 1 certificate requests certified, commit? [y/n]y  /etc/pki/CA/newcerts に追加される
Write out database with 1 new entries
Data Base Updated
==> 0
====
Signed CA certificate is in newcert.pem

カレントディレクトリに中間CAの証明書 newcert.pem が作成される。

内容を見るのは、こう;

~/my-ca$ openssl x509 -in newcert.pem -text

次回へ続く。