LoginSignup
0
2

More than 3 years have passed since last update.

CentOS8.2でプライベート認証局のサーバ証明書をセットアップする手順

Last updated at Posted at 2020-12-04
../

VPSサーバー(CentOS8.2)でプライベート認証局のサーバ証明書を作成してみた。既にLet's Encryptで正規のサーバ証明書はセットアップできていたが、プライベート認証局のサーバ証明書を使う場合はどんな手順になるのか確認してみた。その手順をメモしておく。

プライベート認証局のサーバ証明書は、opensslを利用し、無料で作成でき、有効期限を10年に設定できる。ただし、プライベート認証局の場合、ブラウザからアクセスしたときに「保護されていない通信」の警告が出ることになる。開発やテスト目的での利用に限り、opensslは有効である。

openssl.cnfの編集

CentOS8では、opensslはインストール済みである。/etc/pki/tlsの配下に、プライベート認証局用のディレクトリを作成する。ドメイン名をディレクトリ名にすると識別しやすいだろう。以下の例では、/etc/pki/tls/xxx.com というディレクトリとする。

まず、ディレクトリを作成しておき、openssl.cnfを編集する。後でopensslコマンドを実行する際のいくつかの初期値を事前にセットしておく。

# 以下のバージョンで試している。
$ httpd -version
Server version: Apache/2.4.37 (centos)
Server built:   Sep 15 2020 15:41:16
$ openssl version
OpenSSL 1.1.1c FIPS  28 May 2019

$ cd /etc/pki/tls/
$ mkdir xxx.com             # プライベート認証局用のディレクトリ
$ cd xxx.com

$ cd /etc/pki/tls/
$ cp openssl.cnf openssl.cnf.original   # 念のためopenssl.cnfを退避
$ vi openssl.cnf
[ CA_default ]
dir = /etc/pki/tls/xxx.com      # プライベート認証局の所在

countryName_default = JP            # 日本
stateOrProvinceName_default = Kanagawa  # 都道府県
localityName_default = Kawasaki-city    # 市区町村
0.organizationName_default = xxx    # 会社名
organizationalUnitName_default = Network    # 部署名
nsCertType = server         # コメントアウトされているserverの行を復活させる

以下、秘密鍵の生成、パスフレーズの除去、署名要求(CSR)の生成、SSL証明書(公開鍵を含む)の作成という手順になる。

秘密鍵の生成


# 秘密鍵を生成する。
$ cd /etc/pki/tls/xxx.com
$ openssl genrsa -aes128 2048 > server.key
Generating RSA private key, 2048 bit long modulus (2 primes)
.........................+++++
...+++++
e is 65537 (0x010001)
Enter pass phrase:***
Verifying - Enter pass phrase:***

パスフレーズの除去


# 秘密鍵からパスフレーズを除去する。(同じファイル名で上書き)
$ openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:***
writing RSA key

署名要求(CSR)の生成


# 署名要求(CSR)を生成する。
$ openssl req -utf8 -new -key server.key -out 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) [JP]:
State or Province Name (full name) [Kanagawa]:
Locality Name (eg, city) [Kawasaki-city]:
Organization Name (eg, company) [xxx]:
Organizational Unit Name (eg, section) [Network]:
Common Name (eg, your name or your servers hostname) []:xxx.com
Email Address []:staff@xxx.com

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

SSL証明書(公開鍵を含む)の作成


# SSL証明書(公開鍵を含む)を作成する。(opensslでは、有効期限を10年にできる)
$ openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
Signature ok
subject=C = JP, ST = Kanagawa, L = Kawasaki-city, O = xxx, OU = Network, CN = xxx.com, emailAddress = staff@xxx.com
Getting Private key

$ chmod 600 server.key
$ ls -la /etc/pki/tls/xxx.com
-rw-r--r-- 1 root root 1448 12月  3 14:52 server.crt # SSL証明書ファイル(公開鍵を含む)
-rw-r--r-- 1 root root 1119 12月  3 14:52 server.csr # 署名要求ファイル
-rw------- 1 root root 1679 12月  3 14:51 server.key # 秘密鍵ファイル
$ openssl x509 -in server.crt -text

バーチャルホストの設定

証明書と秘密鍵はできたので、Apacheにバーチャルホストとして、:80ポートと:443ポートを定義する。

  • vhost-03-xxx.conf (xxx.com:80用)
  • vhost-03-xxx-ssl.conf (xxx.com:443用)

vhost-03-xxx.conf は、以下のようになる。


$ vi /etc/httpd/conf.d/vhost-03-xxx.conf
<VirtualHost xxx.com:80>
ServerName xxx.com
ServerAlias www.xxx.com
ServerAdmin webmaster@xxx.com
DocumentRoot "/var/www/html/xxx"

<Directory "/var/www/html/xxx">
    Options FollowSymLinks
    AllowOverride All
#   Order deny,allow
#   Allow from all
    Require all granted
</Directory>

ErrorLog logs/xxx-error_log
CustomLog logs/xxx-access_log combined
</VirtualHost>

また、vhost-03-xxx-ssl.conf は、以下の感じである。作成した証明書と秘密鍵の所在を記述する。


$ vi /etc/httpd/conf.d/vhost-03-xxx-ssl.conf
<VirtualHost xxx.com:443>
ServerName xxx.com
ServerAlias www.xxx.com
ServerAdmin webmaster@xxx.com
DocumentRoot "/var/www/html/xxx"

<Directory "/var/www/html/xxx">
    Options FollowSymLinks
    AllowOverride All
#   Order deny,allow
#   Allow from all
    Require all granted
</Directory>

ErrorLog logs/xxx-error_log
CustomLog logs/xxx-access_log combined

SSLCertificateFile /etc/pki/tls/xxx.com/server.crt
SSLCertificateKeyFile /etc/pki/tls/xxx.com/server.key
</VirtualHost>

起動と確認

httpdを再起動する。


# 以下はセットアップされていること
$ yum install mod_ssl
$ firewall-cmd --add-service=https --zone=public --permanent
$ firewall-cmd --reload

# ダミーのHTMLを配置する
$ cd /var/www/html
$ mkdir -p xxx
$ cp /var/www/html/index.html xxx
$ vi xxx/index.html                 # 適当に編集
$ chown -R apache.apache /var/www/html/xxx

# httpdのシンタックスチェックを行い、サービスを起動する
$ httpd -t
$ systemctl restart httpd

ブラウザから、https://xxx.com/ または https://www.xxx.com/ でアクセスしてみる。プライベート認証局のサーバ証明では「保護されていない通信」となり、鍵マークは表示してもらえない。たとえば、IEの場合、「このサイトは安全ではありません」と出る。「詳細情報」を展開し、「Web ページに移動 (非推奨)」をクリックすると、遷移はしてくれる。

以上

../
0
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
0
2