ssl – 独自の証明書チェーンを作成するには?

certificate ssl

テスト用に自分のOCSPレスポンダを設定したいのですが、そのためには、そこから生成された証明書をいくつか持っているルート証明書が必要です

opensslを使って自己署名証明書を作成することができたので、これをルート証明書として使いたいと思います。次のステップは派生証明書を作成することになりますが、その方法についてのドキュメントが見当たりません。誰かこの情報がどこにあるか知っていますか?

  • 編集:振り返ってみると、私の質問はまだ完全には答えられていないので、問題を明確にするために、私の証明書チェーンをこのように表現します。Root > A > B > C >

今のところ、下を経由してルート証明書とA証明書を作ることができているのですが、もっと長いチェーンを作る方法がわかりません

# Root certificate is created like this:
openssl req -new -newkey rsa:1024 -nodes -out ca.csr -keyout ca.key
openssl x509 -trustout -signkey ca.key -days 365 -req -in ca.csr -out ca.pem

# Certificate A is created like this:
openssl genrsa -out client.key 1024
openssl req -new -key client.key -out client.csr
openssl ca -in client.csr -out client.cer
  • このコマンドはルート証明書に暗黙のうちに依存しており、OpenSSL の設定ファイルの中に必要な情報がありますが、証明書 B は設定ファイルに登録されていない A にしか依存していません

証明書B以降の作成には、どのようなコマンドを使用すればよいのでしょうか?

  • 編集:この記事に答えがありました。証明書B(chain A -> B)はこの2つのコマンドで作成することができ、このアプローチはうまくいっているようです

    # Create a certificate request
    openssl req -new -keyout B.key -out B.request -days 365
    
    # Create and sign the certificate
    openssl ca -policy policy_anything -keyfile A.key -cert A.pem -out B.pem -infiles B.request
    

    また、openssl.cnfのファイルを変更しました

    [ usr_cert ]
    basicConstraints=CA:TRUE # prev value was FALSE
    

  68  StackedCrooked  2010-03-31


ベストアンサー

OpenSSLを直接使うことができます

  1. 認証局の秘密鍵を作成します(これが最も重要な鍵です)

    openssl req -new -newkey rsa:1024 -nodes -out ca.csr -keyout ca.key
    
  2. CAの自己署名証明書を作成します

    openssl x509 -trustout -signkey ca.key -days 365 -req -in ca.csr -out ca.pem
    
  3. クライアント証明書を発行するには、まず鍵を生成してから要求(または外部システムから提供されたものを使用)し、認証局の秘密鍵を使用して証明書に署名します

    openssl genrsa -out client.key 1024
    openssl req -new -key client.key -out client.csr
    openssl ca -in client.csr -out client.cer
    

(私はこれらのコマンドをopenssl.confファイルと一緒に使用しているので、いくつかのオプションを追加する必要があるかもしれません。最初に自分の.confファイルを設定する必要があるかもしれません)

30  twk  2010-03-31


CAを作成したら、それを使って証明書に署名します

  • キーを作成します。
    openssl genrsa -out key_A.key 1024
  • CSRを作成します。
    openssl req -new -key key_A.key -out csr_A.csr # You are about to be asked to enter information etc....
  • 署名してください。
    openssl x509 -req -days 365 -in csr_A.csr -CA CA_certificate_you_created.crt \ -CAkey CA_key_you_created.key -set_serial 01 -out crt_A.crt

    などと、AをBに、CA_certificate_you_created.crtcrt_A.crtに、CA_key_you_created.keykey_A.keyに置き換えます

以下を変更すると、発行した証明書を他の証明書の署名に使用できるようになります

basicConstraints=CA:TRUE  # prev value was FALSE

19  Mr_and_Mrs_D  2012-04-29


OpenSSL には Perl スクリプト CA.pl が付属しており、自己署名されたルート CA の証明書と、それに対応する秘密鍵の作成を支援します。また、他のキーペアや証明書署名要求 (CSR) の生成や、それらの CSR の処理 (つまり、それらの証明書の発行) などにも役立ちます

多くの製品では、CA 証明書に特定の属性が含まれていることを要求しており、CA 証明書としてマークしていないと、他の証明書の有効な署名者/発行者として受け入れられないことに注意してください。作成した自己署名証明書にその属性が含まれていない場合、他のソフトウェアがそれを有効なルート CA 証明書のように扱うようにするのに苦労することがあるかもしれません

私の記憶が正しければ、構文は次のようになります

CA.pl -newca    # Create a new root CA

CA.pl -newreq   # Create a new CSR

CA.pl -sign     # Sign a CSR, creating a cert

CA.pl -pkcs12   # Turn an issued cert, plus its matching private key and trust chain,
# into a .p12 file you can install on another machine

10  Spiff  2010-03-31


私が見つけた一番簡単な解決策はこちらです

cert=crt.pem
certPk=pk.pem
ca=ca.crt.pem
caPk=ca.pk.pem

host=example.com
certValidityDays=30

cd "$(mktemp -d)"

# Create CA
openssl req -newkey rsa:4096 -keyout "${caPk}" -x509 -new -nodes -out "${ca}" \
-subj "/OU=Unknown/O=Unknown/L=Unknown/ST=unknown/C=AU" -days "${certValidityDays}"

# Create Cert Signing Request
openssl req -new -newkey rsa:4096 -nodes -keyout "${certPk}" -out csr.pem \
-subj "/CN=${host}/OU=Unknown/O=Unknown/L=Unknown/ST=unknown/C=AU"

# Sign Cert
openssl x509 -req -in csr.pem -CA "${ca}" -CAkey "${caPk}" -CAcreateserial -out "${cert}" \
-days "${certValidityDays}"
  • 基本的には3つのコマンドのみ
  • コマンドをわかりやすくするために、いくつかの変数を導入しました
  • ウェブサーバに必要なものはcert, certPk, caです
  • caPkは、より多くの証明書に署名したい場合にのみ必要です
  • はインタラクションなしで実行されるので、バッチ処理で使用することができます
  • インタラクションが欲しいなら、-subjの部分を省けばいい

さらにいくつかのヒント

  • このように、標準化された debian 環境でこれらのステップを実行することができます
docker run --rm -it debian:buster-20200327-slim bash
apt update && apt install openssl #=1.1.1d-0+deb10u2
# Run steps above, then `exit` - the certs are stored in a subfolder in your current directory

1  schnatterer  2020-04-14


Summary

ルートCA、中間CA、リーフ証明書の作成に使用するコマンドのまとめ

openssl genrsa -out root.key 2048
openssl req -new -key root.key -out root.csr -config root_req.config
openssl ca -in root.csr -out root.pem -config root.config -selfsign -extfile ca.ext -days 1095

openssl genrsa -out intermediate.key 2048
openssl req -new -key intermediate.key -out intermediate.csr -config intermediate_req.config
openssl ca -in intermediate.csr -out intermediate.pem -config root.config -extfile ca.ext -days 730

openssl genrsa -out leaf.key 2048
openssl req -new -key leaf.key -out leaf.csr -config leaf_req.config
openssl ca -in leaf.csr -out leaf.pem -config intermediate.config -days 365

openssl verify -x509_strict -CAfile root.pem -untrusted intermediate.pem leaf.pem

これらのコマンドは、以下に説明するいくつかの設定に依存している。これらのコマンドは、x509 コマンドだけでできるような、チェーンでいくつかの証明書が必要な場合には、少しやりすぎである。これらのコマンドはまた、テキストデータベースで認証を追跡し、シリアル番号を自動インクリメントする。この回答を読む前か後に、openssl caのマニュアルページの警告とバグのセクションを読むことをお勧めします

Directory Structure

始める前に以下のようなディレクトリ構造が必要になります

ca.ext              # the extensions required for a CA certificate for signing certs
intermediate.config # configuration for the intermediate CA
root.config         # configuration for the root CA

leaf_req.config         # configuration for the leaf cert's csr
intermediate_req.config # configuration for the intermediate CA's csr
root_req.config         # configuration for the root CA's csr

intermediate_ca/    # state files specific to the intermediate CA
index           # a text database of issued certificates
serial          # an auto-incrementing serial number for issued certificates
root_ca/            # state files specific to the root CA
index           # a text database of issued certificates
serial          # an auto-incrementing serial number for issued certificates

これがより恒久的なCAになると、次のような変更が考えられます

  1. 各CAの設定ファイル、秘密鍵(後に生成)、証明書ファイル(後に生成)をCAのディレクトリに移動します。これには、設定ファイルの変更が必要になります
  2. 発行された証明書のためのCAのディレクトリにサブディレクトリを作成します。これには設定ファイルの変更が必要です
  3. 秘密鍵を暗号化します
  4. CA設定ファイルの証明書のデフォルトの発行日数を設定します

ディレクトリ構造ファイルの内容を起動します

ディレクトリ構造の各ファイルの内容は以下の通りです

ca.ext

[ default ]
basicConstraints = critical,CA:true     # recommended to be marked critical. required for a ca
keyUsage         = critical,keyCertSign # required to be marked critical. required for signing certs

intermediate.config

[ ca ]
default_ca      = CA_default

[ CA_default]
dir             = ./intermediate_ca   # helper variable pointing to ca specific files
database        = $dir/index          # database of certs generated by the ca
new_certs_dir   = ./                  # one dir up to make the demo easier
certificate     = ./intermediate.pem  # one dir up to make the demo easier
serial          = $dir/serial         # file with incrementing hex serial number for certs
private_key     = ./intermediate.key

policy          = policy_any
email_in_dn     = no                  # recommended
unique_subject  = no                  # recommended for easier certificate rollover
copy_extensions = none                # don't honor the extensions in the csr
default_md      = sha256

[ policy_any ]
countryName            = optional
stateOrProvinceName    = optional
organizationName       = optional
organizationalUnitName = optional
commonName             = supplied

root.config

[ ca ]
default_ca      = CA_default

[ CA_default]
dir             = ./root_ca      # helper variable pointing to ca specific files
database        = $dir/index     # database of certs generated by the ca
new_certs_dir   = ./             # one dir up to make the demo easier
certificate     = ./root.pem     # one dir up to make the demo easier
serial          = $dir/serial    # file with incrementing hex serial number for certs
private_key     = ./root.key

policy          = policy_any
email_in_dn     = no             # recommended
unique_subject  = no             # recommended for easier certificate rollover
copy_extensions = none           # don't honor the extensions in the csr
default_md      = sha256

[ policy_any ]
countryName            = optional
stateOrProvinceName    = optional
organizationName       = optional
organizationalUnitName = optional
commonName             = supplied

leaf_req.config

[ req ]
distinguished_name = req_distinguished_name
prompt             = no

[ req_distinguished_name ]
countryName = US
commonName  = Test Leaf

intermediate_req.config

[ req ]
distinguished_name = req_distinguished_name
prompt             = no

[ req_distinguished_name ]
countryName = US
commonName  = Test Intermediate CA

root_req.config

[ req ]
distinguished_name = req_distinguished_name
prompt             = no

[ req_distinguished_name ]
countryName = US
commonName  = Test Root CA

intermediate_ca/index (空のファイル)。発行された証明書のデータベース。自動的に更新される

[empty]

intermediate_ca/serial (単一の0は動作しません)。このファイルはオートインクリメントされます

00

root_ca/index (空のファイル)。発行された証明書のデータベース。自動的に更新されます

[empty]

root_ca/serial (単一の0は動作しません)。このファイルはオートインクリメントされます

00

Detailed commands

これで、この回答の冒頭からコマンドを実行できるようになりました

# create the private key for the root CA
openssl genrsa
-out root.key # output file
2048          # bitcount

# create the csr for the root CA
openssl req
-new
-key root.key           # private key associated with the csr
-out root.csr           # output file
-config root_req.config # contains config for generating the csr such as the distinguished name

# create the root CA cert
openssl ca
-in root.csr        # csr file
-out root.pem       # output certificate file
-config root.config # CA configuration file
-selfsign           # create a self-signed certificate
-extfile ca.ext     # extensions that must be present for CAs that sign certificates
-days 1095          # 3 years

# create the private key for the intermediate CA
openssl genrsa
-out intermediate.key # output file
2048                  # bitcount

# create the csr for the intermediate CA
openssl req
-new
-key intermediate.key           # private key associated with the csr
-out intermediate.csr           # output file
-config intermediate_req.config # contains config for generating the csr such as the distinguished name

# create the intermediate CA cert
openssl ca
-in intermediate.csr  # csr file
-out intermediate.pem # output certificate file
-config root.config   # CA configuration file (note: root is still issuing)
-extfile ca.ext       # extensions that must be present for CAs that sign certificates
-days 730             # 2 years

# create the private key for the leaf certificate
openssl genrsa
-out leaf.key # output file
2048          # bitcount

# create the csr for the leaf certificate
openssl req
-new
-key leaf.key           # private key associated with the csr
-out leaf.csr           # output file
-config leaf_req.config # contains config for generating the csr such as the distinguished name

# create the leaf certificate (note: no ca.ext. this certificate is not a CA)
openssl ca
-in leaf.csr                # csr file
-out leaf.pem               # output certificate file
-config intermediate.config # CA configuration file (note: intermediate is issuing)
-days 365                   # 1 year

# verify the certificate chain
openssl verify
-x509_strict                # strict adherence to rules
-CAfile root.pem            # root certificate
-untrusted intermediate.pem # file with all intermediates
leaf.pem                    # leaf certificate to verify

Final thoughts

本番でCAを使おうと思っている人は、openssl caのmanページの警告とバグのセクションを読んでください(もしくはmanページ全体を読んでください)

1  Millie Smith  2020-10-03


Stack Overflowでthisの投稿を見つけたのですが、Node.JS用なのですが、thisのGitHubレポのスクリプトはopensslのコマンドを使ってルートCAとドメイン証明書を作成しています

Run using:

  • example.com:bash make-root-ca-and-certificates.sh 'example.com'
  • localhost:bash make-root-ca-and-certificates.sh 'localhost'

make-root-ca-and-certificates.sh

#!/bin/bash
FQDN=$1

# make directories to work from
mkdir -p certs/{server,client,ca,tmp}

# Create your very own Root Certificate Authority
openssl genrsa \
-out certs/ca/my-root-ca.key.pem \
2048

# Self-sign your Root Certificate Authority
# Since this is private, the details can be as bogus as you like
openssl req \
-x509 \
-new \
-nodes \
-key certs/ca/my-root-ca.key.pem \
-days 1024 \
-out certs/ca/my-root-ca.crt.pem \
-subj "/C=US/ST=Utah/L=Provo/O=ACME Signing Authority Inc/CN=example.com"

# Create a Device Certificate for each domain,
# such as example.com, *.example.com, awesome.example.com
# NOTE: You MUST match CN to the domain name or ip address you want to use
openssl genrsa \
-out certs/server/privkey.pem \
2048

# Create a request from your Device, which your Root CA will sign
openssl req -new \
-key certs/server/privkey.pem \
-out certs/tmp/csr.pem \
-subj "/C=US/ST=Utah/L=Provo/O=ACME Tech Inc/CN=${FQDN}"

# Sign the request from Device with your Root CA
# -CAserial certs/ca/my-root-ca.srl
openssl x509 \
-req -in certs/tmp/csr.pem \
-CA certs/ca/my-root-ca.crt.pem \
-CAkey certs/ca/my-root-ca.key.pem \
-CAcreateserial \
-out certs/server/cert.pem \
-days 500

# Create a public key, for funzies
# see https://gist.github.com/coolaj86/f6f36efce2821dfb046d
openssl rsa \
-in certs/server/privkey.pem \
-pubout -out certs/client/pubkey.pem

# Put things in their proper place
rsync -a certs/ca/my-root-ca.crt.pem certs/server/chain.pem
rsync -a certs/ca/my-root-ca.crt.pem certs/client/chain.pem
cat certs/server/cert.pem certs/server/chain.pem > certs/server/fullchain.pem

0  Ralph Bisschops  2018-11-06


ベストアンサーはここにあります – https://www.youtube.com/watch?v=KXi3-3dEb8k

中間証明書のCA基本制約をTrueにすることで、ニーズに合わせて修正しました

MyOpenssl.conf:

[ req ]
distinguished_name       = distinguished_name
extensions               = int_ca
req_extensions           = int_ca

[ int_ca ]
basicConstraints         = CA:TRUE

[ distinguished_name ]

#create Root CA
openssl genrsa -out RootCA.key 4096
openssl req -new -x509 -days 1826 -key RootCA.key -out RootCA.pem -subj "/C=US/O=xzy/OU=abc/CN=ROOT-CN"

#create Intermediate CA
openssl genrsa -out IntermediateCA.key 4096
openssl req -new -sha256 -key IntermediateCA.key -nodes -out IntermediateCA.csr -subj "/C=US/O=xyz/OU=abc/CN=INTERIM-CN"
openssl x509 -req -days 1000 -extfile MyOpenssl.conf -extensions int_ca -in IntermediateCA.csr -CA RootCA.pem -CAkey RootCA.key -CAcreateserial -out IntermediateCA.pem

#create EndUser certificates
openssl genrsa -out my_server.key 2048
openssl req -new -key my_server.key -out my_server.csr -subj "/C=US/O=xyz/OU=abc/CN=USER-CN"
openssl x509 -req -in my_server.csr -CA IntermediateCA.pem -CAkey IntermediateCA.key -set_serial 01 -out my_server.pem -days 500 -sha1

0  KungFuPanda  2020-06-01


タイトルとURLをコピーしました