LoginSignup
1
2

More than 3 years have passed since last update.

自分のPCでSSL用鍵ファイル、CSRファイルの作成方法

Last updated at Posted at 2020-06-24

今回作る鍵、CSRファイル

今回作るファイルは以下の4つとなります。
※SSLは単年ごとに更新することが多いため、作成するファイル名には西暦を入れておくことをオススメします。

  • 元になる鍵(.key.org)ファイル作成
  • パスフレーズも.txtファイルで作成
  • PEMフレーズで暗号化解除した鍵(.key)ファイル作成
  • CSRファイル(.csr)の作成

環境

OS:MacOS
terminal:iTerm2

opensslのバージョン確認

まずは自分の環境にopensslがあるかを確認します。

openssl version

では、実際にコマンドを叩いてみます。

Terminal
$ openssl version
OpenSSL 1.1.1f  31 Mar 2020

上記のような結果が返ってきました。

※opensslがない場合はHomebrewからインストール、バージョンアップ、コマンドの有効化をして下さい
参考:CSR・秘密鍵の生成のために、macにHomebrewとopenSSLをインストールした

2048bit の秘密鍵を作成

わかりやすいように、自分のデスクトップに移動してからopensslコマンドで鍵ファイルを作成します。

Terminal
$ cd /Users/username/Desktop/
$ openssl genrsa -des3 -out sample.com_2020.key.org 2048

Generating RSA private key, 2048 bit long modulus (2 primes)
...+++++
..+++++
e is 65537 (0x010001)
Enter pass phrase for sample.com_2020.key: //任意のパスフレーズ
Verifying - Enter pass phrase for sample.com_2020.key: //任意のパスフレーズの確認

cdコマンドで自分のデスクトップへ移動し、opensslコマンドで、鍵長を2048bitで指定して作成しました。
作成の際にパスフレーズを設定するので、今回はpasswordとして作成しました。パスフレーズの確認も同じです。
デスクトップにsample.com_2020.key.orgというファイルが作成されていると思います。

この時設定したパスフレーズは.txtファイルにしてバックアップとして保存しておきます。
※バックアップが不要な方はスルーしてください。

パスフレーズの暗号化解除した鍵(.key)ファイルを作成

作成した.keyファイルのパスフレーズを外します。

Terminal
$ openssl rsa -in sample.com_2020.key.org -out sample.com_2020.key
Enter pass phrase for sample.com_2020.key:password
writing RSA key

簡単な確認方法としては.orgのファイルと.keyファイルを見比べてみてください。
先頭2.3行目の違いがわかるかと思います。

CSRファイルの作成

作成したsample.com_2020.keyを元にCSRファイルも作成していきます。

Terminal
$ openssl req -new -key sample.com_2020.key -out sample.com_2020.csr

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 //2文字国コード
State or Province Name (full name) [Some-State]:Osaka //所在地の都道府県名
Locality Name (eg, city) []:Osaka-city //所在地の市区町村名
Organization Name (eg, company) [Internet Widgits Pty Ltd]:sample inc. //組織、会社名
Organizational Unit Name (eg, section) []:sales //部署部門名
Common Name (e.g. server FQDN or YOUR name) []:sample.com //ドメイン
Email Address []: //入力不要

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

opensslコマンドを叩くと、CSRに組み込まれる情報の入力を求められます。
必要な情報を入力し完了するとデスクトップにsample.com_2020.csrが作成されます。

参考:[CSR生成] nginx + OpenSSL(新規・更新)

できあがったファイルリスト

  • sample.com_2020.key.org
  • sample.com_2020.key
  • sample.com_2020.csr
  • sample.com_2020_pass-phrase.txt

できあがった自作のCSRでSSL証明書も購入できますし、鍵ファイルもサーバーへインストールすることでSSL対応することもできます。
インストールについてはもう少し勉強してから書ければなと思います。

1
2
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
2