Shammer's Philosophy

My private adversaria

独自CA環境構築と証明書作成手順(Debian / openssl コマンド編)

背景

CA.sh を使用して、CSR秘密鍵、証明書の出力先を指定するには CA.sh への追記が必要。追記するためには、各コマンドのオプションとかを調べる必要がある。でも、ここまでやるなら最初から自分でコマンド打ち込んでもいいのでは?ということで、openssl のコマンドを実行する手順を整理。

Directory 構成

以下のようにする。

OpenSSL_DIR
 MyRootCA
 MiddleCA
 Servers

MyRootCA の手順

openssl.cnf ファイルの編集内容は独自CA環境構築と証明書作成手順(Debian / CA.sh 編) - Shammerismと同じ。

  1. mkdir $OPENSSL_DIR
  2. cd $OPENSSL_DIR
  3. mkdir $RootCA_DIR(MyRootCA)
  4. cd $RootCA_DIR
  5. mkdir newcerts certs crl private
  6. touch index.txt
  7. echo 00 > serial
  8. cp -p $OpenSSL_ConfigFile(Debian:/etc/ssl/openssl.cnf) $OpenSSL_ConfigFile.default
  9. Edit $OpenSSL_ConfigFile
    • The default dir value of [ CA_default ] is "./demoCA", rename this to "./MyRootCA".
    • The default_md value of [ CA_default ] is "default_md", change this value to "sha256".
    • The default_bits of [ req ] is 1024, change this value to 2048.
    • The basicConstraints of [ v3_ca ] is "CA:true" only, comment out this line and delete # at the line of "basicConstraints = critical,CA:true".
  10. openssl genrsa -out ./private/cakey.pem 2048
  11. openssl req -new -x509 -key ./private/cakey.pem -outform PEM -out cacert.pem -days 3650

MiddleCA(中間認証局)の手順

  1. cd $OPENSSL_DIR
  2. mkdir $MiddleCA
  3. cd $MiddleCA
  4. mkdir private newcerts certs cry
  5. touch index.txt
  6. echo 00 > serial
  7. copy -p /etc/ssl/openssl.cnf .
  8. Edit ./openssl.cnf
    • Change the dir value of [ CA_default ] to $MiddleCA
    • Change stateOrProvinceName of [ policy_match ] to optional
    • Change organizationName of [ policy_match ] to optional
    • Copy [ v3_ca ] section as [ v3_middle_ca ] section
      • Enable basicConstraints of [ v3_middle_ca ] is "CA:true" only.
  9. openssl genrsa -out ./private/cakey.pem 2048
  10. openssl req -new -key ./private/cakey.pem -out middleca.csr
  11. cd $OPENSSL_DIR
  12. openssl ca -create_serial -batch -extensions v3_middle_ca -in $MiddleCA/middleca.csr -out $MiddleCA/cacert.pem

サーバー証明書作成

証明書は中間認証局で署名する。

  1. cd $OPENSSL_DIR
  2. mkdir -p Servers/$Server_DIR
  3. openssl genrsa -out $Server_DIR/privatekey.pem 2048
  4. openssl req -new -key $Server_DIR/privatekey.pem -out $Server_DIR/csr.pem
  5. openssl ca -config $MiddleCA/openssl.cnf -in $Server_DIR/csr.pem -out $SERVER_CERTIFICATE