検索中に、SSL 証明書署名要求に署名する方法がいくつか見つかりました。
モジュールの使用
x509
:openssl x509 -req -days 360 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
モジュールの使用
ca
:openssl ca -cert ca.crt -keyfile ca.key -in server.csr -out server.crt
注: この場合の正しいパラメータの使用方法がわかりません。使用する場合は正しい使用方法を教えてください。
認証局で証明書要求に署名するにはどのような方法を使用すればよいですか? ある方法は他の方法よりも優れていますか (たとえば、一方が非推奨になっているなど)?
ベストアンサー1
1. Using the x509 module
openssl x509 ...
...
2 Using the ca module
openssl ca ...
...
これらのコマンドの前置きが抜けています。
これは 2 段階のプロセスです。まず CA を設定し、次にエンド エンティティ証明書 (サーバーまたはユーザー) に署名します。2 つのコマンドはどちらも、2 つの手順を 1 つにまとめます。また、どちらも、CA とサーバー (エンド エンティティ) 証明書の両方に対して OpenSSL 構成ファイルがすでに設定されていることを前提としています。
まず、基本的な設定ファイルを作成します。
$ touch openssl-ca.cnf
次に、次のコードを追加します。
HOME = .
RANDFILE = $ENV::HOME/.rnd
####################################################################
[ ca ]
default_ca = CA_default # The default ca section
[ CA_default ]
default_days = 365 # How long to certify for
default_crl_days = 30 # How long before next CRL
default_md = sha256 # Use public key default MD
preserve = no # Keep passed DN ordering
x509_extensions = ca_extensions # The extensions to add to the cert
email_in_dn = no # Don't concat the email in the DN
copy_extensions = copy # Required to copy SANs from CSR to cert
####################################################################
[ req ]
default_bits = 4096
default_keyfile = cakey.pem
distinguished_name = ca_distinguished_name
x509_extensions = ca_extensions
string_mask = utf8only
####################################################################
[ ca_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = US
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = Maryland
localityName = Locality Name (eg, city)
localityName_default = Baltimore
organizationName = Organization Name (eg, company)
organizationName_default = Test CA, Limited
organizationalUnitName = Organizational Unit (eg, division)
organizationalUnitName_default = Server Research Department
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_default = Test CA
emailAddress = Email Address
emailAddress_default = [email protected]
####################################################################
[ ca_extensions ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always, issuer
basicConstraints = critical, CA:true
keyUsage = keyCertSign, cRLSign
上記のフィールドは、より複雑なものopenssl.cnf
( で確認できます/usr/lib/openssl.cnf
) から取得されていますが、CA 証明書と秘密キーを作成するために不可欠なものだと思います。
上記のフィールドを好みに合わせて調整します。デフォルトを使用すると、構成ファイルやコマンド オプションを試すときに同じ情報を入力する手間が省けます。
CRL 関連のものは省略しましたが、CA 操作には含まれているはずです。openssl.cnf
および関連crl_ext
セクションを参照してください。
次に、以下を実行します。 は、証明書を検査できるようにパスワードまたはパスフレーズを省略します。パスワードまたはパスフレーズを省略するのは、本当に悪い-nodes
考えです。
$ openssl req -x509 -config openssl-ca.cnf -days 365 -newkey rsa:4096 -sha256 -nodes -out cacert.pem -outform PEM
コマンドを実行すると、 はcacert.pem
CA 操作用の証明書になり、 はcakey.pem
秘密鍵になります。秘密鍵にはパスワードやパスフレーズがないことに注意してください。
次のコマンドで証明書をダンプできます。
$ openssl x509 -in cacert.pem -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 11485830970703032316 (0x9f65de69ceef2ffc)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=US, ST=MD, L=Baltimore, CN=Test CA/[email protected]
Validity
Not Before: Jan 24 14:24:11 2014 GMT
Not After : Feb 23 14:24:11 2014 GMT
Subject: C=US, ST=MD, L=Baltimore, CN=Test CA/[email protected]
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
Modulus:
00:b1:7f:29:be:78:02:b8:56:54:2d:2c:ec:ff:6d:
...
39:f9:1e:52:cb:8e:bf:8b:9e:a6:93:e1:22:09:8b:
59:05:9f
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
4A:9A:F3:10:9E:D7:CF:54:79:DE:46:75:7A:B0:D0:C1:0F:CF:C1:8A
X509v3 Authority Key Identifier:
keyid:4A:9A:F3:10:9E:D7:CF:54:79:DE:46:75:7A:B0:D0:C1:0F:CF:C1:8A
X509v3 Basic Constraints: critical
CA:TRUE
X509v3 Key Usage:
Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
4a:6f:1f:ac:fd:fb:1e:a4:6d:08:eb:f5:af:f6:1e:48:a5:c7:
...
cd:c6:ac:30:f9:15:83:41:c1:d1:20:fa:85:e7:4f:35:8f:b5:
38:ff:fd:55:68:2c:3e:37
そして、次の方法でその目的をテストします ( については心配しないでくださいAny Purpose: Yes
。「critical,CA:FALSE」ではなく「Any Purpose CA : Yes」を参照してください)。
$ openssl x509 -purpose -in cacert.pem -inform PEM
Certificate purposes:
SSL client : No
SSL client CA : Yes
SSL server : No
SSL server CA : Yes
Netscape SSL server : No
Netscape SSL server CA : Yes
S/MIME signing : No
S/MIME signing CA : Yes
S/MIME encryption : No
S/MIME encryption CA : Yes
CRL signing : Yes
CRL signing CA : Yes
Any Purpose : Yes
Any Purpose CA : Yes
OCSP helper : Yes
OCSP helper CA : Yes
Time Stamp signing : No
Time Stamp signing CA : Yes
-----BEGIN CERTIFICATE-----
MIIFpTCCA42gAwIBAgIJAJ9l3mnO7y/8MA0GCSqGSIb3DQEBCwUAMGExCzAJBgNV
...
aQUtFrV4hpmJUaQZ7ySr/RjCb4KYkQpTkOtKJOU1Ic3GrDD5FYNBwdEg+oXnTzWP
tTj//VVoLD43
-----END CERTIFICATE-----
パート 2 では、簡単に理解できる別の構成ファイルを作成します。まず、touch
(openssl-server.cnf
ユーザー証明書用にこのようなファイルを作成することもできます)。
$ touch openssl-server.cnf
次にそれを開いて、以下を追加します。
HOME = .
RANDFILE = $ENV::HOME/.rnd
####################################################################
[ req ]
default_bits = 2048
default_keyfile = serverkey.pem
distinguished_name = server_distinguished_name
req_extensions = server_req_extensions
string_mask = utf8only
####################################################################
[ server_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = US
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = MD
localityName = Locality Name (eg, city)
localityName_default = Baltimore
organizationName = Organization Name (eg, company)
organizationName_default = Test Server, Limited
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_default = Test Server
emailAddress = Email Address
emailAddress_default = [email protected]
####################################################################
[ server_req_extensions ]
subjectKeyIdentifier = hash
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
subjectAltName = @alternate_names
nsComment = "OpenSSL Generated Certificate"
####################################################################
[ alternate_names ]
DNS.1 = example.com
DNS.2 = www.example.com
DNS.3 = mail.example.com
DNS.4 = ftp.example.com
開発中でワークステーションをサーバーとして使用する必要がある場合は、Chrome で次の操作を行う必要があります。そうしないと、Chrome がCommon Nameが無効である ( ERR_CERT_COMMON_NAME_INVALID
)とエラーを発する場合があります。この場合、SAN 内の IP アドレスと CN の関係がわかりません。
# IPv4 localhost
IP.1 = 127.0.0.1
# IPv6 localhost
IP.2 = ::1
次に、サーバー証明書要求を作成します。必ず*を省略して ください。追加すると、要求ではなく証明書が作成されます。-x509
-x509
$ openssl req -config openssl-server.cnf -newkey rsa:2048 -sha256 -nodes -out servercert.csr -outform PEM
このコマンドを実行すると、 にリクエストがservercert.csr
、 に秘密鍵が保存されますserverkey.pem
。
そして再度検査することができます。
$ openssl req -text -noout -verify -in servercert.csr
Certificate:
verify OK
Certificate Request:
Version: 0 (0x0)
Subject: C=US, ST=MD, L=Baltimore, CN=Test Server/[email protected]
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:ce:3d:58:7f:a0:59:92:aa:7c:a0:82:dc:c9:6d:
...
f9:5e:0c:ba:84:eb:27:0d:d9:e7:22:5d:fe:e5:51:
86:e1
Exponent: 65537 (0x10001)
Attributes:
Requested Extensions:
X509v3 Subject Key Identifier:
1F:09:EF:79:9A:73:36:C1:80:52:60:2D:03:53:C7:B6:BD:63:3B:61
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Key Encipherment
X509v3 Subject Alternative Name:
DNS:example.com, DNS:www.example.com, DNS:mail.example.com, DNS:ftp.example.com
Netscape Comment:
OpenSSL Generated Certificate
Signature Algorithm: sha256WithRSAEncryption
6d:e8:d3:85:b3:88:d4:1a:80:9e:67:0d:37:46:db:4d:9a:81:
...
76:6a:22:0a:41:45:1f:e2:d6:e4:8f:a1:ca:de:e5:69:98:88:
a9:63:d0:a7
次に、CA で署名する必要があります。
CA によるサーバーの証明書への署名の準備がほぼ整いました。openssl-ca.cnf
コマンドを発行する前に、CA にはさらに 2 つのセクションが必要です。
まず、openssl-ca.cnf
次の 2 つのセクションを開いて追加します。
####################################################################
[ signing_policy ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
####################################################################
[ signing_req ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
[ CA_default ]
次に、のセクションに次のコードを追加しますopenssl-ca.cnf
。これらは複雑になる可能性があるため、以前は省略しました (当時は使用されていませんでした)。これから、これらがどのように使用されるかを説明しますので、意味がわかると思います。
base_dir = .
certificate = $base_dir/cacert.pem # The CA certifcate
private_key = $base_dir/cakey.pem # The CA private key
new_certs_dir = $base_dir # Location for new certs after signing
database = $base_dir/index.txt # Database index file
serial = $base_dir/serial.txt # The current serial number
unique_subject = no # Set to 'no' to allow creation of
# several certificates with same subject.
3番目に、タッチしindex.txt
てserial.txt
:
$ touch index.txt
$ echo '01' > serial.txt
次に、次の操作を実行します。
$ openssl ca -config openssl-ca.cnf -policy signing_policy -extensions signing_req -out servercert.pem -infiles servercert.csr
次のような画面が表示されます。
Using configuration from openssl-ca.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'US'
stateOrProvinceName :ASN.1 12:'MD'
localityName :ASN.1 12:'Baltimore'
commonName :ASN.1 12:'Test CA'
emailAddress :IA5STRING:'[email protected]'
Certificate is to be certified until Oct 20 16:12:39 2016 GMT (1000 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
コマンドを実行すると、 に新しく作成されたサーバー証明書が作成されますservercert.pem
。秘密鍵は以前に作成されており、 で使用できますserverkey.pem
。
最後に、次のようにして新しく作成された証明書を検査できます。
$ openssl x509 -in servercert.pem -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 9 (0x9)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=US, ST=MD, L=Baltimore, CN=Test CA/[email protected]
Validity
Not Before: Jan 24 19:07:36 2014 GMT
Not After : Oct 20 19:07:36 2016 GMT
Subject: C=US, ST=MD, L=Baltimore, CN=Test Server
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:ce:3d:58:7f:a0:59:92:aa:7c:a0:82:dc:c9:6d:
...
f9:5e:0c:ba:84:eb:27:0d:d9:e7:22:5d:fe:e5:51:
86:e1
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
1F:09:EF:79:9A:73:36:C1:80:52:60:2D:03:53:C7:B6:BD:63:3B:61
X509v3 Authority Key Identifier:
keyid:42:15:F2:CA:9C:B1:BB:F5:4C:2C:66:27:DA:6D:2E:5F:BA:0F:C5:9E
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Key Encipherment
X509v3 Subject Alternative Name:
DNS:example.com, DNS:www.example.com, DNS:mail.example.com, DNS:ftp.example.com
Netscape Comment:
OpenSSL Generated Certificate
Signature Algorithm: sha256WithRSAEncryption
b1:40:f6:34:f4:38:c8:57:d4:b6:08:f7:e2:71:12:6b:0e:4a:
...
45:71:06:a9:86:b6:0f:6d:8d:e1:c5:97:8d:fd:59:43:e9:3c:
56:a5:eb:c8:7e:9f:6b:7a
先ほど、 に以下を追加しましたCA_default
: copy_extensions = copy
。これは、リクエストを行ったユーザーによって提供された拡張子をコピーします。
を省略するとcopy_extensions = copy
、サーバー証明書に や のようなサブジェクト代替名 (SAN) が含まれなくなりますwww.example.com
。mail.example.com
を使用する場合でもcopy_extensions = copy
、リクエストをよく確認しないと、リクエストした人があなたをだまして、サーバー証明書やユーザー証明書ではなく、従属ルート証明書のようなものに署名させてしまう可能性があります。つまり、リクエストした人は、信頼できるルート証明書に連鎖する証明書を作成できるということです。openssl req -verify
署名する前に、リクエストを で必ず確認してください。
省略する unique_subject
か に設定すると、サブジェクトの識別名で作成できる証明書は1 つyes
だけになります。
unique_subject = yes # Set to 'no' to allow creation of
# several ctificates with same subject.
実験中に 2 番目の証明書を作成しようとすると、CA の秘密キーを使用してサーバーの証明書に署名するときに次の結果になります。
Sign the certificate? [y/n]:Y
failed to update database
TXT_DB error number 2
テストに最適ですunique_subject = no
。
自己署名 CA、下位 CA、エンド エンティティ証明書間で組織名の一貫性を保つには、CA 構成ファイルに次の内容を追加します。
[ policy_match ]
organizationName = match
組織名の変更を許可する場合は、以下を使用します。
[ policy_match ]
organizationName = supplied
X.509/PKIX 証明書の DNS 名の取り扱いに関しては、他にもルールがあります。ルールについては、次のドキュメントを参照してください。
- RFC 5280、インターネット X.509 公開鍵インフラストラクチャ証明書および証明書失効リスト (CRL) プロファイル
- RFC 6125、トランスポート層セキュリティ (TLS) のコンテキストで X.509 (PKIX) 証明書を使用したインターネット公開鍵インフラストラクチャ内のドメインベースのアプリケーション サービス ID の表現と検証
- RFC 6797、付録 A、HTTP 厳密なトランスポート セキュリティ (HSTS)
- RFC 7469、HTTP の公開鍵ピンニング拡張
- CA/ブラウザフォーラムのベースライン要件
- CA/ブラウザフォーラム拡張検証ガイドライン
RFC 6797 と RFC 7469 は、他の RFC や CA/B ドキュメントよりも制限が厳しいため、ここに記載されています。RFC 6797 と 7469 では、IP アドレスも許可されていません。