LoginSignup
1
0

自己署名証明書とサーバ証明書を作成する手順

Last updated at Posted at 2024-04-14

こんにちは。
株式会社クラスアクト インフラストラクチャ事業部の大塚です。

今回は自己署名証明書とサーバ証明書を作成していきたいと思います。
そして、この次の記事で作成したサーバ証明書を使ってapache2への通信をhttps化していきたいと思います。

環境イメージ

今回作成した環境配下となります。
OSはubuntu22.04となります。opensslを使って設定を入れ込んでいくのですが、基本的なディレクトリは/etc/sslとなります。
暗号化-ページ1.drawio (1).png

鍵作成やCSR作成、署名の流れは以下になると思います。
暗号化-ページ2.drawio (1).png

環境構築

CA秘密鍵と証明書の作成

まず、現状を確認します。/etc/ssl/配下で色々作業を行います。
※opensslはデフォルトでインストールされておりました。

root@ca-server:/etc/ssl# openssl version
OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
root@ca-server:~# cd /etc/ssl/
root@ca-server:/etc/ssl# ls
certs  openssl.cnf  private

次にopenssl.cnfファイルを確認します。
"dir = ./demoCA"との記載があるため、/etc/ssl/にdemoCAを作っていきます。
ここを編集していくことでオリジナルのディレクトリでも作業が出来ると思います。今回は確認までしておりません。

root@ca-server:/etc/ssl# cat openssl.cnf
~中略~
####################################################################
[ ca ]
default_ca      = CA_default            # The default ca section

####################################################################
[ CA_default ]

dir             = ./demoCA              # Where everything is kept
certs           = $dir/certs            # Where the issued certs are kept
crl_dir         = $dir/crl              # Where the issued crl are kept
database        = $dir/index.txt        # database index file.
#unique_subject = no                    # Set to 'no' to allow creation of
                                        # several certs with same subject.
new_certs_dir   = $dir/newcerts         # default place for new certs.

certificate     = $dir/cacert.pem       # The CA certificate
serial          = $dir/serial           # The current serial number
crlnumber       = $dir/crlnumber        # the current crl number
                                        # must be commented out to leave a V1 CRL
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/cakey.pem# The private key

x509_extensions = usr_cert              # The extensions to add to the cert
~中略~

demoCAディレクトリを作成し、そこでCA秘密鍵とCA証明書(自己署名証明書)を作成します。
まずは秘密鍵を作成します。
※2024ではなく2048が正しいと思います。失礼しました。

  • genrsa:RSA秘密鍵を作成
  • aes256:作成する秘密鍵をAES256ビットで暗号化
  • -out ca-key.pem:秘密鍵をca-key.pemという名前で書き出す
root@ca-server:/etc/ssl/demoCA# openssl genrsa -aes256 -out ca-key.pem 2024 
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:(password)

次にこの秘密鍵を使って自己署名証明書を作成します。CSRを作りながらそれに署名をしているイメージだと思います。

  • req:CSR(Certificate Signing Request)を生成
  • new:新しいCSRを作成する時に指定
  • x509:x509形式の自己署名証明書を作成
  • -key ca-key.pem: CSRに使用する秘密鍵を指定。自分で作成した鍵を指定しています。だから自己証明…
  • -out ca-cert.pem: 生成された自己署名証明書をca-cert.pemという名前のファイルに書き出す
  • -days 365: 証明書の有効期間を365日に設定

最後にlsコマンドを叩くことでCAの秘密鍵と証明書があることが確認できます。

root@ca-server:/etc/ssl/demoCA# openssl req -new -x509 -key ca-key.pem -out ca-cert.pem -days 365 
Enter pass phrase for ca-key.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) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Chuo
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Classact
Organizational Unit Name (eg, section) []:Infrastructure
Common Name (e.g. server FQDN or YOUR name) []:shota ohtsuka
Email Address []:shota@example.com
root@ca-server:/etc/ssl/demoCA# ls
ca-cert.pem  ca-key.pem

サーバ秘密鍵とCSRの作成

サーバの秘密鍵とCSRを作成していきます。
以下のコマンドで同時に作成をしていきます。最後にlsコマンドを実行し実際に作成されていることを確認します。

  • -newkey rsa:2048: 新しいRSA鍵ペア(公開鍵と秘密鍵)を生成します。2048ビットの鍵長を指定
  • -nodes: 秘密鍵をパスフレーズで暗号化しないように設定
  • -keyout server-key.pem: 生成された秘密鍵をserver-key.pemという名前のファイルに書き出す
  • -out server-req.pem: 生成されたCSRをserver-req.pemという名前のファイルに書き出す
root@ca-server:/etc/ssl/demoCA# openssl req -newkey rsa:2048 -nodes -keyout server-key.pem -out server-req.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) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Chuo
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Classact
Organizational Unit Name (eg, section) []:Infrastructure
Common Name (e.g. server FQDN or YOUR name) []:Shota Ohtsuka
Email Address []:shota@example.com

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

root@ca-server:/etc/ssl/demoCA# ls
ca-cert.pem  ca-key.pem  server-key.pem  server-req.pem

サーバのCSRに署名を行いサーバ証明書作成

以下のコマンドを実行し、CSRに対してCA秘密鍵/CA証明書で署名を行いサーバ証明書を作成します。

  • openssl x509: CSRを署名して証明書を生成するためのコマンド
  • -req -in server-req.pem: 署名するCSRファイルを指定
  • -out server-cert.pem: 生成された証明書をserver-cert.pemという名前のファイルに書き出す
  • -CA ca-cert.pem -CAkey ca-key.pem: CSRを署名するためのCA証明書と秘密鍵を指定
  • -CAcreateserial: シリアル番号ファイルが存在しない場合に、自動的に作成
    -days 365: 証明書の有効期間を365日に設定
root@ca-server:/etc/ssl/demoCA# openssl x509 -req -in server-req.pem -out server-cert.pem -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -days 365
Certificate request self-signature ok
subject=C = JP, ST = Tokyo, L = Chuo, O = Classact, OU = Infrastructure, CN = Shota Ohtsuka, emailAddress = shota@example.com
Enter pass phrase for ca-key.pem:
root@ca-server:/etc/ssl/demoCA# ls
ca-cert.pem  ca-key.pem  server-cert.pem  server-key.pem  server-req.pem

以上です!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0