そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

CentOS8プライベート認証局(WEBサーバHTTPS化)【④WEBサーバの署名要求】

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・CentOS8でプライベート認証局を作って、LAN内のWEBサーバにHTTPSアクセスができるようにしたい。
・ネットで検索すると、新旧のいろいろな情報が出てきて、同じように真似してやってみたが上手くいかないので、最新の手順を詳しく知りたい。

こういった疑問に答えます。

本記事の内容

  1. CentOS8とOpenSSLでプライベート認証局(ルートCA)を作る方法
  2. CentOS8とOpenSSLで中間認証局を作る方法
  3. CentOS8のWEBサーバの証明書を作る方法
  4. CentOS8・ApacheのWEBサーバに証明書を設定してHTTPSアクセスを許可する方法

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

前回の記事

前回の記事で、中間CAの作成までが済みました。

souiunogaii.hatenablog.com

①~⑦までをまとめて読みたい方は下記のまとめ記事から。

souiunogaii.hatenablog.com

CentOS8のWEBサーバ側にOpenSSLをインストール

WEBサーバ側の環境

cat /etc/redhat-release
uname -r
[root@sv-neptune ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@sv-neptune ~]# uname -r
4.18.0-193.14.2.el8_2.x86_64

CentOS8にOpenSSLをインストール

dnf install openssl

今回の私の環境では、既にOpenSSLがインストール済でした。

[root@sv-neptune ~]# dnf install openssl
メタデータの期限切れの最終確認: 0:30:19 時間前の 20200913105402秒 に実施しました。
パッケージ openssl-1:1.1.1c-15.el8.x86_64 はすでにインストールされています。
依存関係が解決しました。
行うべきことはありません。
完了しました!

CentOS8で「/etc/pki/tls/misc/CA」を使えるようにする

CentOS8には、「/etc/pki/tls/misc/CA」が無いので、別のCentOS7サーバからコピーして配置しておきます。
パーミッションで実行権限を付与するのも忘れずに。

WEBサーバ(CentOS8)の署名要求CSRファイルを作成

WEBサーバ用のconfファイルを作成

openssl.cnf をコピーしてopenssl-req.cnf を作成

cp openssl.cnf openssl-req.cnf
[root@sv-neptune tls]# cp openssl.cnf openssl-req.cnf

confの内容を編集

vi /etc/pki/tls/openssl-req.cnf
[ req ]
default_bits        = 2048
default_md      = sha256
default_keyfile     = privkey.pem
distinguished_name  = req_distinguished_name
attributes      = req_attributes
# x509_extensions  = v3_ca # The extensions to add to the self signed cert

# Passwords for private keys if not present they will be prompted for
# input_password = secret
# output_password = secret

# This sets a mask for permitted string types. There are several options.
# default: PrintableString, T61String, BMPString.
# pkix  : PrintableString, BMPString (PKIX recommendation before 2004)
# utf8only: only UTF8Strings (PKIX recommendation after 2004).
# nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings).
# MASK:XXXX a literal mask value.
# WARNING: ancient versions of Netscape crash on BMPStrings or UTF8Strings.
string_mask = utf8only

req_extensions = v3_req # The extensions to add to a certificate request

[ req ]の部分の以下を変更

  • # x509_extensions = v3_ca # The extensions to add to the self signed cert コメントアウト
  • req_extensions = v3_req # The extensions to add to a certificate request 有効化
[ v3_req ]

# Extensions to add to a certificate request

basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment

subjectAltName = @alt_names

[ alt_names ]
DNS.1 = *.sv-neptune.com
DNS.2 = sv-neptune.com
DNS.3 = *.sv-neptune.wp
DNS.4 = sv-neptune.wp
DNS.5 = *.sv-neptune.net
DNS.6 = sv-neptune.net
DNS.7 = *.sv-neptune.info
DNS.8 = sv-neptune.info
DNS.9 = *.sv-neptune.db
DNS.10 = sv-neptune.db
DNS.11 = *.sv-neptune.biz
DNS.12 = sv-neptune.biz
DNS.13 = *.sv-neptune.ac
DNS.14 = sv-neptune.ac

[ v3_req ] の部分の以下を変更

  • subjectAltName = @alt_names を追記
  • [ alt_names ] を追記
  • DNS.1 = *.sv-neptune.com を追記
  • DNS.2 = sv-neptune.com を追記
  • 以下、登録したいDNS名を複数個、追記


1台のWEBサーバで、バーチャルホストで複数のドメインを運用している場合は、
「subjectAltName(サブジェクト代替名)」としてDNS名を複数登録することができます。

カレントディレクトリを「/etc/pki/tls/certs/」に移動

cd /etc/pki/tls/certs/

WEBサーバの秘密鍵と署名要求CSRファイルを同時に作成

SSLEAY_CONFIG="-config /etc/pki/tls/openssl-req.cnf" /etc/pki/tls/misc/CA -newreq
[root@sv-neptune certs]# SSLEAY_CONFIG="-config /etc/pki/tls/openssl-req.cnf" /etc/pki/tls/misc/CA -newreq
Ignoring -days; not generating a certificate
Generating a RSA private key
..............+++++
....+++++
writing new private key to 'newkey.pem'
Enter PEM pass phrase:★★★秘密鍵のパスワード★★★
Verifying - Enter PEM pass phrase:★★★秘密鍵のパスワード★★★
-----
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) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:Shinagawa-ku
Organization Name (eg, company) [Default Company Ltd]:MEPTUNE Inc.
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:*.sv-neptune.com
Email Address []:admin@sv-neptune.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:★★★空のまま[Enter]キー★★★
An optional company name []:★★★空のまま[Enter]キー★★★
Request is in newreq.pem, private key is in newkey.pem

作成した署名要求CSRファイルの内容の表示

openssl req -text < newreq.pem
[root@sv-neptune certs]# openssl req -text < newreq.pem
Certificate Request:
    Data:
        Version: 1 (0x0)
        Subject: C = JP, ST = Tokyo, L = Shinagawa-ku, O = MEPTUNE Inc., CN = *.sv-neptune.com, emailAddress = admin@sv-neptune.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                RSA Public-Key: (2048 bit)
                Modulus:
                    00:c3:30:26:81:68:b3:e9:cd:c0:82:2b:4e:78:52:
                    d3:a9:27:c4:fe:69:9d:00:38:bb:c0:a4:72:d0:92:
------------------(中略)-------------------
                    15:43:1c:ab:ae:ae:b8:80:09:fc:80:4d:d8:ae:60:
                    1f:70:ca:59:2e:5e:73:b7:60:95:e2:ed:f3:a8:e7:
                    78:a7
                Exponent: 65537 (0x10001)
        Attributes:
        Requested Extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            X509v3 Key Usage:
                Digital Signature, Non Repudiation, Key Encipherment
            X509v3 Subject Alternative Name:
                DNS:*.sv-neptune.com, DNS:sv-neptune.com, DNS:*.sv-neptune.wp, DNS:sv-neptune.wp, DNS:*.sv-neptune.net, DNS:sv-neptune.net, DNS:*.sv-neptune.info, DNS:sv-neptune.info, DNS:*.sv-neptune.db, DNS:sv-neptune.db, DNS:*.sv-neptune.biz, DNS:sv-neptune.biz, DNS:*.sv-neptune.ac, DNS:sv-neptune.ac
    Signature Algorithm: sha256WithRSAEncryption
         be:50:f6:79:0e:05:34:b3:ca:83:8e:6d:e1:b3:8c:41:c9:74:
         19:84:0d:ae:6d:96:15:65:de:41:92:dd:41:78:f3:f5:9f:d4:
------------------(中略)-------------------
         fa:25:b7:3f:f3:98:71:e3:b7:b6:7c:73:3d:2d:7b:80:78:eb:
         b5:a3:19:44
-----BEGIN CERTIFICATE REQUEST-----
MIID+jCCAuICAQAwgYsxCzAJBgNVBAYTAkpQMQ4wDAYDVQQIDAVUb2t5bzEVMBMG
A1UEBwwMU2hpbmFnYXdhLWt1MRUwEwYDVQQKDAxNRVBUVU5FIEluYy4xGTAXBgNV
------------------(中略)-------------------
OamkgcfkJzI7JuBEBGDuZkyKZANEMvUdoReKDjnJ5OAriKIM4rfX4/oltz/zmHHj
t7Z8cz0te4B467WjGUQ=
-----END CERTIFICATE REQUEST-----

Requested Extensions: の部分に以下が入っていることを確認します。

  • X509v3 Subject Alternative Name:
  • DNS:*.sv-neptune.com, DNS:sv-neptune.com,・・・・・(以下省略)


WEBサーバの証明書に「Subject Alternative Name」のDNS名が入っていないと、
せっかく証明書を作っても、ブラウザ「Google Chrome」「Microsoft Edge」で表示するときに以下のエラーが出てしまいます。

  • このサーバーがxxxxxxxxであることを確認できませんでした。このサーバーのセキュリティ証明書でSAN(サブジェクトの別名)が指定されていません。設定が不適切であるか、悪意のあるユーザーによって接続が妨害されている可能性があります。

少し長くなったので、続きはまた次の記事に書きます。

souiunogaii.hatenablog.com