0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

サーバー証明書(オレオレ証明書)を作ってhttps通信をできるようにした。

Last updated at Posted at 2021-03-14

サーバ証明書とは

そのサーバの実在証明や、通信内容の暗号化に使用するものです。
Digicertやグローバルサインといった認証局によって発行されております。
しかし、上記の認証局を利用すると金がかかるので、今回は自分のサーバでオレオレ証明書という証明書を作成します。

オレオレ証明書とは

正規の認証局を通さず、自分自身のサーバーで発行したサーバー証明書です。
何の保証にもなっていませんが、今回は自宅内のサーバーとhttps通信したいだけなので問題ありません。

ざっくりとした作業手順

1.htpps通信を行いたいサーバーで秘密鍵を作成する。
2.作成した秘密鍵を元に、CSRを作成する。
3.CSRを元にしてサーバー証明書を作成する
4.webサーバの設定を修正し、https通信を可能とする

秘密鍵を作成する

以下のコマンドを入力して秘密鍵を作成します。

openssl genrsa -des3 -out 秘密鍵のパス 2048

コマンド入力後、秘密鍵のパスワードを要求されるので適当なパスワードを入力してください。

Enter pass phrase for 秘密鍵のパス:
Verifying - Enter pass phrase for 秘密鍵のパス:

これで秘密鍵の完成です。この秘密鍵を元にしてCSRを作成します。

CSRを作成する

CSRは、認証局にサーバー証明書を作成してもらうための申請書のようなものです。
以下のコマンドで作成することが可能です。

openssl req -new -key 秘密鍵のパス -out CSRのパス

コマンド実行後、以下のように色々と聞かれます。サーバー保有者や団体の情報を入力すれば完成です。

Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:tokyo
Locality Name (eg, city) [Default City]:hoge
Organization Name (eg, company) [Default Company Ltd]:company
Organizational Unit Name (eg, section) []:sales
Common Name (eg, your name or your server's hostname) []:www.test.com
Email Address []:

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

サーバー証明書の作成

本来ならば、CSRを認証局に提出してサーバー証明書を作成してもらうのですが、今回はオレオレ証明書なので自作します。

$ openssl x509 -in CSRのパス -days 365 -req -signkey 秘密鍵のパス -out 作成するサーバ証明書のパス
Signature ok
subject=/C=JP/ST=tokyo/L=hoge/O=company/OU=sales/CN=www.test.com
Getting Private key

webサーバーの設定

今回はwebサーバーとしてNginxを使用しております。

server {
  # ポート番号は80から443に変更
  listen 443;

  # SSLの設定を追記
  ssl on;
  ssl_certificate オレオレ証明書のパス;
  ssl_certificate_key 秘密鍵のパス;
  ~中略~
}

実践

これでhttpsでアクセスできるはずなので、早速実践してみましょう。
こんな画面になりますが、自分で作ったサーバー証明書なので警告が出るのは仕方ないです。
ブラウザ上で「thisisunsafe」と入力すれば、ページにアクセスすることができます。
dintPOIYUo4uzRP1615733137_1615733225.png

0
4
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
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?