新人社内SEのメモ書き

琵琶湖生まれ琵琶湖育ち。京都の産業用機器メーカの社内SE。自身の体験した事や勉強したことなど記事にしてます。自作PCとドライブが大好き。

[OpenSSL]自己署名証明書を生成する方法

目標

サーバ証明書(公開鍵)www.example.jp.csrとサーバ秘密鍵www.example.jp.keyを生成する

構築環境

  • Windows Server 2016
  • OpenSSL 1.0.2l
    あんまりOS関係ないので、Linuxでもほぼ一緒だと思う

手順

  1. コンソールで秘密鍵の生成
    秘密鍵は、公開鍵で暗号化したものをサーバがこの鍵で復号化するものです
# openssl genrsa > www.example.jp.key
Generating RSA private key, 2048 bit long modulus
..............................................................+++
..+++
e is 65537 (0x10001)
  1. コンソールでCSRファイルを生成
# openssl req -new -key www.example.jp.key > www.example.jp.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
State or Province Name (full name) [Some-State]:kyoto
Locality Name (eg, city) []:kyoto
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Company
Organizational Unit Name (eg, section) []:Department
Common Name (e.g. server FQDN or YOUR name) []:www.example.jp
Email Address []:example@example.jp

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

まずCSRファイルってなんやねんってことですが、認証局(CA)に対して、SSLサーバ証明書への証明を申請する内容。
CSRファイルには「公開鍵」とそれの所有者情報と申請者が対応する秘密鍵を持っているのを示す申請者の署名が書いている。
(引用元CSRファイルとは何ですか? | SSLサーバ証明書のクロストラスト)

  1. サーバ証明書を生成 実際にお金を払って証明書を発行する場合は、前段階のCSRファイルを認証局(CA)に渡して証明してもらいます。
    今回は自己署名証明書なんで自己署名します。
# openssl x509 -req -signkey www.example.jp.key < www.example.jp.csr > www.example.jp.crt
Signature ok
subject=/C=jp/ST=kyoto/L=kyoto/O=Company/OU=Department/CN=www.example.jp/emailAddress=example@example.jp
Getting Private key