作業環境
- CentOS 8
- OpenSSL 1.1.1g
- httpd-2.4.37-39
- mod_ssl-2.4.37-39
ネットワーク検証における HTTPS サーバの必要性
構築対象ネットワークにおいて HTTPS サーバが存在し、ネットワーク機器を介してその HTTPS サーバとクライアント間で SSL/TLS 通信をするような場合、ネットワーク検証段階で SSL/TLS 通信についても検証を行う必要が発生します。
SSL/TLS 通信を検証するためには、検証環境に検証用の HTTPS サーバが必要となります。
以下では検証で使用するための簡易な HTTPS サーバの構築方法について記載します。
なお、 SSL/TLS 通信のためにはサーバ証明書が必要となるため、サーバ証明書を作成するための認証局(CA)の構築方法についても記載します。
手順概要
以下の手順で作業します。
- ルート認証局(ルート CA)の構築
- サーバ証明書作成用の秘密鍵及び CA 証明書を作成
- HTTPS サーバの構築
- Apache 及び SSL モジュールのインストール
- HTTPS サーバの証明書の作成
- SSL モジュールの設定
なお、ルート CA と HTTPS サーバは別々の CentOS マシンとして構築します。
ルート認証局(ルート CA)の構築
OpenSSL のインストール
OpenSSL がインストールされていない場合はインストールします。
dnf -y install openssl
ディレクトリ作成と OpenSSL 設定ファイル作成
ここでは作業ディレクトリは /opt/pki
とし、各ディレクトリを作成します。
mkdir /opt/pki
mkdir /opt/pki/config
mkdir /opt/pki/RootCA
OpenSSL 設定ファイル /opt/pki/config/openssl_sign.cnf
を作成します。
vi /opt/pki/config/openssl_sign.cnf
設定ファイルの内容は以下の通りとします。
[ ca ]
default_ca = CA_default
[ CA_default ]
dir = ./
certs = $dir/certs
crl_dir = $dir/crl
database = $dir/index.txt
new_certs_dir = $dir/newcerts
serial = $dir/serial
crlnumber = $dir/crlnumber
crl = $dir/crl.pem
RANDFILE = $dir/.rand
name_opt = ca_default
cert_opt = ca_default
default_days = 365
default_crl_days= 30
default_bits = 2048
default_md = sha256
preserve = no
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints=CA:true
keyUsage = cRLSign,keyCertSign
[ v3_server ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
crlDistributionPoints = URI:http://192.168.0.254/crl/example.net.crl
[ v3_client ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth
crlDistributionPoints = URI:http://192.168.0.254/crl/example.net.crl
秘密鍵の生成
作業ディレクトリは /opt/pki/RootCA
です。
まず以下の通り /opt/pki/RootCA
フォルダ内で環境を整えます。
cd /opt/pki/RootCA
mkdir newcerts
echo "01" > serial
echo "00" > crlnumber
touch index.txt
次に以下コマンド構文でルート CA の秘密鍵を作成します。
openssl genrsa \
-out "秘密鍵ファイル名" \
-aes256 \
-passout pass:"鍵の暗号化パスワード" \
"鍵長"
ここでは以下の通りとします。
# openssl genrsa \
> -out RootCA_key.pem \
> -aes256 \
> -passout pass:rootCaKeyPass \
> 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
...............................+++++
..............................+++++
e is 65537 (0x010001)
RootCA_key.pem
という秘密鍵ファイルが作成されました。
ls -l
合計 12
-rw-------. 1 root root 1766 10月 16 11:08 RootCA_key.pem
-rw-r--r--. 1 root root 3 10月 16 11:01 crlnumber
-rw-r--r--. 1 root root 0 10月 16 11:01 index.txt
drwxr-xr-x. 2 root root 6 10月 16 11:01 newcerts
-rw-r--r--. 1 root root 3 10月 16 11:01 serial
証明書署名要求(CSR)の作成
上で作成した秘密鍵を使用して CSR を作成します。
作業ディレクトリは /opt/pki/RootCA
です。
以下コマンド構文で CSR を作成します。
openssl req -new -out "CSRファイル名" -key "秘密鍵ファイル名" -passin pass:"秘密鍵パスワード"
ここでは以下の通りとします。この時、証明書情報の入力を対話的に行います。
# openssl req -new -out RootCA_csr.pem -key RootCA_key.pem -passin pass:rootCaKeyPass
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]:City
Organization Name (eg, company) [Default Company Ltd]:Company
Organizational Unit Name (eg, section) []:Section
Common Name (eg, your name or your server's hostname) []:CentOS8-CA
Email Address []:email@test.ca
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
RootCA_csr.pem
という CSR ファイルが作成されました。
ls -l
合計 16
-rw-r--r--. 1 root root 1041 10月 16 11:31 RootCA_csr.pem
-rw-------. 1 root root 1766 10月 16 11:08 RootCA_key.pem
-rw-r--r--. 1 root root 3 10月 16 11:01 crlnumber
-rw-r--r--. 1 root root 0 10月 16 11:01 index.txt
drwxr-xr-x. 2 root root 6 10月 16 11:01 newcerts
-rw-r--r--. 1 root root 3 10月 16 11:01 serial
証明書の作成
ルート CA 証明書は、CSR に対して自身の秘密鍵で署名する自己署名の形を取ります。
証明書作成コマンドの構文は以下の通りです。
openssl ca -config "設定ファイルパス" -batch -extensions v3_ca \
-out "証明書ファイル名" \
-in "CSRファイル名" \
-selfsign \
-keyfile "秘密鍵ファイル名" \
-passin pass:"秘密鍵のパスワード"
※設定ファイルとは手順の最初の方で作成していたファイルのことです
ここでは以下の通りとします。
# openssl ca -config ../config/openssl_sign.cnf -batch -extensions v3_ca \
> -out RootCA_crt.pem \
> -in RootCA_csr.pem \
> -selfsign \
> -keyfile RootCA_key.pem \
> -passin pass:rootCaKeyPass
Using configuration from ../config/openssl_sign.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Oct 16 02:35:42 2021 GMT
Not After : Oct 16 02:35:42 2022 GMT
Subject:
countryName = JP
stateOrProvinceName = Tokyo
organizationName = Company
organizationalUnitName = Section
commonName = CentOS8-CA
emailAddress = email@test.ca
X509v3 extensions:
X509v3 Subject Key Identifier:
8E:FC:02:FC:A7:5B:8D:C6:B6:5D:92:9E:F8:8C:87:1B:2B:4B:9B:D2
X509v3 Authority Key Identifier:
keyid:8E:FC:02:FC:A7:5B:8D:C6:B6:5D:92:9E:F8:8C:87:1B:2B:4B:9B:D2
X509v3 Basic Constraints:
CA:TRUE
X509v3 Key Usage:
Certificate Sign, CRL Sign
Certificate is to be certified until Oct 16 02:35:42 2022 GMT (365 days)
Write out database with 1 new entries
Data Base Updated
RootCA_crt.pem
という証明書ファイルが作成されました。
ls -l
合計 36
-rw-r--r--. 1 root root 4521 10月 16 11:35 RootCA_crt.pem
-rw-r--r--. 1 root root 1041 10月 16 11:31 RootCA_csr.pem
-rw-------. 1 root root 1766 10月 16 11:08 RootCA_key.pem
-rw-r--r--. 1 root root 3 10月 16 11:01 crlnumber
-rw-r--r--. 1 root root 105 10月 16 11:35 index.txt
-rw-r--r--. 1 root root 21 10月 16 11:35 index.txt.attr
-rw-r--r--. 1 root root 0 10月 16 11:01 index.txt.old
drwxr-xr-x. 2 root root 20 10月 16 11:35 newcerts
-rw-r--r--. 1 root root 3 10月 16 11:35 serial
-rw-r--r--. 1 root root 3 10月 16 11:01 serial.old
以上でルート CA の構築は完了です。
ルート CA の秘密鍵及び ルート CA 証明書は HTTPS サーバ証明書の作成に使用します。またルート CA 証明書は SSL/TLS クライアントにインストールして使用します。
HTTPS サーバの構築
必要パッケージのインストール
Apache と SSL モジュールが必要なため以下コマンドでインストールします。また、OpenSSL も必要なためインストールします。
dnf -y install httpd mod_ssl openssl
HTTPS サーバ証明書の作成
作業ディレクトリは /opt/pki
とします。
mkdir /opt/pki
cd /opt/pki
秘密鍵の作成
CSR 作成用の(HTTPS サーバの)秘密鍵を作成します。
openssl genrsa > "秘密鍵ファイル名"
ここでは以下の通り作成します。
# openssl genrsa > server.key
Generating RSA private key, 2048 bit long modulus (2 primes)
.......+++++
............................................................................................+++++
e is 65537 (0x010001)
証明書署名要求(CSR)の作成
以下コマンドで CSR を作成します。
openssl req -new -key "秘密鍵ファイル名" > "CSRファイル名"
ここでは以下の通り作成します。このとき証明書情報を対話的に入力します。
# openssl req -new -key server.key > server.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) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:City
Organization Name (eg, company) [Default Company Ltd]:Company
Organizational Unit Name (eg, section) []:Section
Common Name (eg, your name or your server's hostname) []:CentOS8-HTTPS
Email Address []:hoge@piyo
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
サーバの秘密鍵と CSR が作成できました。
# ls -l
合計 8
-rw-r--r--. 1 root root 1041 10月 16 16:06 server.csr
-rw-r--r--. 1 root root 1679 10月 16 16:05 server.key
作成した CSR を CA に転送します。
証明書の作成(CA で実施)
証明書の作成は CA で行います。
上で作成した CSR に対してルート CA の秘密鍵で署名して証明書を作成します。
準備:OpenSSL 設定ファイルの編集
Chrome などのブラウザで HTTPS サーバにアクセスする場合、Subject Alternative Names(SAN)
をサーバ証明書に含めないと、アクセス時に警告画面が表示されます。
(エラー内容:NET::ERR_CERT_COMMON_NAME_INVALID
)
そこで、SAN をサーバ証明書に含めるために OpenSSL の設定ファイル openssl_sign.cnf
を編集します。
具体的には [ v3_server ]
セクション内のディレクティブとして以下を追加します。
subjectAltName = DNS:"HTTPSサーバホスト名", IP:"HTTPSサーバIPアドレス"
例では以下内容を追加しました。
subjectAltName = DNS:CentOS8-HTTPS, IP:192.168.75.134
サーバ証明書の作成
以下コマンド構文で作成します。
openssl ca -config "設定ファイルパス" -batch -extensions v3_server \
-out "出力証明書ファイル名" \
-in "CSRファイル名" \
-cert "CA証明書ファイル名" \
-keyfile "CA秘密鍵ファイル名" \
-passin pass:"CA秘密鍵パスワード"
-extensions
の値が v3_server
になっていることに注意してください。
ここでは以下の通り作成します。
# openssl ca -config ../config/openssl_sign.cnf -batch -extensions v3_server \
> -out server.crt \
> -in server.csr \
> -cert RootCA_crt.pem \
> -keyfile RootCA_key.pem \
> -passin pass:rootCaKeyPass
Using configuration from openssl_sign.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 4 (0x4)
Validity
Not Before: Oct 16 09:07:51 2021 GMT
Not After : Oct 16 09:07:51 2022 GMT
Subject:
countryName = JP
stateOrProvinceName = Tokyo
organizationName = Company
organizationalUnitName = Section
commonName = 192.168.75.134
emailAddress = hoge@piyo
X509v3 extensions:
X509v3 Subject Key Identifier:
83:05:7E:9E:34:96:9E:54:2F:84:E7:F2:29:DB:0D:C6:3A:EA:FB:F2
X509v3 Authority Key Identifier:
keyid:1A:F4:66:DC:B3:5B:B0:68:69:D1:81:67:C1:23:32:23:25:C9:C8:F0
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment
X509v3 Extended Key Usage:
TLS Web Server Authentication
X509v3 CRL Distribution Points:
Full Name:
URI:http://192.168.0.254/crl/example.net.crl
X509v3 Subject Alternative Name:
DNS:CentOS8-HTTPS, IP Address:192.168.75.134
Certificate is to be certified until Oct 16 09:07:51 2022 GMT (365 days)
Write out database with 1 new entries
Data Base Updated
server.crt
という証明書ファイルが作成されました。
# ls -l
合計 56
-rw-r--r--. 1 root root 4500 10月 16 16:13 RootCA_crt.pem
-rw-r--r--. 1 root root 1037 10月 16 16:12 RootCA_csr.pem
-rw-------. 1 root root 1766 10月 16 16:12 RootCA_key.pem
-rw-r--r--. 1 root root 3 10月 16 16:11 crlnumber
-rw-r--r--. 1 root root 205 10月 16 16:19 index.txt
-rw-r--r--. 1 root root 21 10月 16 16:19 index.txt.attr
-rw-r--r--. 1 root root 21 10月 16 16:13 index.txt.attr.old
-rw-r--r--. 1 root root 101 10月 16 16:13 index.txt.old
drwxr-xr-x. 2 root root 34 10月 16 16:19 newcerts
-rw-r--r--. 1 root root 3 10月 16 16:19 serial
-rw-r--r--. 1 root root 3 10月 16 16:13 serial.old
-rw-r--r--. 1 root root 4507 10月 16 16:19 server.crt
-rw-r--r--. 1 root root 1041 10月 16 16:19 server.csr
証明書内容の確認
以下コマンドで証明書の内容を確認できます。
openssl x509 -in "証明書ファイル名" -text -noout
# openssl x509 -in server.crt -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 4 (0x4)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = JP, ST = Tokyo, O = Company, OU = Section, CN = CentOS8-CA, emailAddress = hoge@piyo
Validity
Not Before: Oct 16 09:07:51 2021 GMT
Not After : Oct 16 09:07:51 2022 GMT
Subject: C = JP, ST = Tokyo, O = Company, OU = Section, CN = 192.168.75.134, emailAddress = hoge@piyo
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public-Key: (2048 bit)
Modulus:
...
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
83:05:7E:9E:34:96:9E:54:2F:84:E7:F2:29:DB:0D:C6:3A:EA:FB:F2
X509v3 Authority Key Identifier:
keyid:1A:F4:66:DC:B3:5B:B0:68:69:D1:81:67:C1:23:32:23:25:C9:C8:F0
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment
X509v3 Extended Key Usage:
TLS Web Server Authentication
X509v3 CRL Distribution Points:
Full Name:
URI:http://192.168.0.254/crl/example.net.crl
X509v3 Subject Alternative Name:
DNS:CentOS8-HTTPS, IP Address:192.168.75.134
Signature Algorithm: sha256WithRSAEncryption
...
作成されたサーバ証明書は HTTPS サーバに転送して使用します。
秘密鍵と証明書の格納
秘密鍵(CSR 作成時に使用したもの)と証明書の格納先ディレクトリを作成し、それぞれ格納します。
保存ディレクトリは任意ですがここでは以下の通りとします。
mkdir /etc/httpd/conf/ssl.crt
mkdir /etc/httpd/conf/ssl.key
cp server.crt /etc/httpd/conf/ssl.crt/
cp server.key /etc/httpd/conf/ssl.key/
SSL モジュールの設定
設定ファイルは /etc/httpd/conf.d/ssl.conf
です。
vi /etc/httpd/conf.d/ssl.conf
SSLCertificateFile
と SSLCertificateKeyFile
を探してください。
- SSLCertificateFile
- サーバ証明書のパスを指定
- SSLCertificateKeyFile
- HTTPS サーバの秘密鍵のパスを指定
デフォルトの設定はコメントアウトしておき、以下の通り設定を追加します。
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key
Apache の起動
httpd サービスを起動させます。
systemctl start httpd
#起動済みだった場合は restart
systemctl restart httpd
サービスの自動起動設定もしておきます。
systemctl enable httpd
HTTPS サーバへアクセス
構築した HTTPS サーバにネットワーク接続できるクライアントからブラウザで
https://IPアドレス
へアクセスします。
なお、クライアントへはあらかじめルート CA 証明書をインストールしておきます。
ブラウザの警告表示が無く、以下のような画面が表示されることを確認します。
参考資料






