LoginSignup
53
69

More than 5 years have passed since last update.

httpsの設定とオレオレ証明書(自己署名証明書)の作成

Last updated at Posted at 2018-04-04

httpsの設定とOpenSSLでのオレオレ証明書(自己署名証明書)の作成手順。

環境:
CentOS7
Apatch2.4.6

必要なものをインストール

# yum install openssl
# yum install mod_ssl

OpenSSLとmod_sslをインストール。
既にインストールされている場合はスキップ

オレオレ証明書を作る

  1. 秘密鍵の作成
  2. 公開鍵 CSR(証明書署名要求)の作成 (追記参照)
  3. 証明書の作成 の順に準備する。

秘密鍵を作る

まず/etc/httpd/conf へ移動して秘密鍵を作る。

# cd /etc/httpd/conf
# openssl genrsa -aes128 1024 > server.key

Generating RSA private key, 1024 bit long modulus
..................++++++
..........................................++++++
e is 65537 (0x10001)
Enter pass phrase:(パスワード)
Verifying - Enter pass phrase:(パスワード再入力)

ここではserver.keyという名前で、128 ビットの AES 方式で暗号化した 1024 ビットの秘密鍵を作成している。
Enter pass phraseでパスワードを入力が求められるので、任意のパスワードを入力&再入力する。

公開鍵 CSR(証明書署名要求)を作る

次に秘密鍵を使って、公開鍵 公開鍵を含むCSR(証明書署名要求)を作成。

# openssl req -new -key server.key > server.csr
Enter pass phrase for server.key:(パスワード)
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]:Tokyo
Locality Name (eg, city) []:Shinjuku
Organization Name (eg, company) [Internet Widgits Pty Ltd]:(スキップ)
Organizational Unit Name (eg, section) []:(スキップ)
Common Name (eg, YOUR name) []:192.168.XXX.XXX
Email Address []:(スキップ)

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

最初にパスワードを聞かれたら、先ほど作った秘密鍵のパスワードを入力。
次に2桁の国識別コードは JP
都道府県、市区町村は任意の値
(スキップ)となっている箇所は空白のままとばしてOK。

Common Name にはブラウザで指定するアドレスを入力するので注意。
https://www.xxx.com/ でアクセスするのであれば xxx.com
https://192.168.XXX.XXX/ でアクセスするのであれば そのIP アドレスを指定する。

証明書を作る

上記でできた秘密鍵と公開鍵 CSR(証明書署名要求)を使って証明書を作成する。

# openssl x509 -in server.csr -days 36500 -req -signkey server.key > server.crt
Signature ok
subject=/C=JP/ST=Chiba/L=Funabashi/O=Default Company Ltd/CN=192.168.XXX.XXX
Getting Private key
Enter pass phrase for server.key:(上記で指定したパスワード)

server.crtというファイル名の証明書ができているはず。
上記の設定では、X.509 形式の証明書ファイルを、有効期間36500日(約100年)の指定にしている。

パスワードの解除(パスワード確認のスキップ)

Apatchの起動時に毎回パスワードを聞かれると面倒なので以下の設定をしておく。

# mv server.key server.key.bak
# openssl rsa -in server.key.bak > server.key
Enter pass phrase for server.key.back:(上記で指定したパスワード)
writing RSA key

証明書の設定

/etc/httpd/conf.d/ssl.confに証明書の設定を追加する。
(ssl.confはmod_sslをインストールした際に作成されているはず)

# vi /etc/httpd/conf.d/ssl.conf

  :
<VirtualHost _default_:443>
  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn
  SSLEngine on
  SSLProtocol all -SSLv2
  SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
  SSLCertificateFile /etc/httpd/conf/server.crt <--ここと
  SSLCertificateKeyFile /etc/httpd/conf/server.key <--ここを修正
  <Files ~ "\.(cgi|shtml|phtml|php3?)$">
    :
  </Files>
</VirtualHost>

作成した証明書のパスに修正すればOK。

Apatch リロード&再起動

ApatchをリロードすればOK。

# service httpd configtest
Syntax OK
# service httpd reload
Reloading httpd:

もし設定が反映されていなかったらApatchを再起動してみる。

firewallの設定

上記だけでhttpsにアクセスできるはずだけど、もしタイムアウトとかでhttpsに繋がらなかったらファイアウォールの設定を見てみる。

# firewall-cmd --list --zone=public --permanent
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client http mysql ssh https <--ここ!
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

servicesのところにhttpやhttpsがなかったら、追加する。

firewall-cmd --permanent --zone=public --add-service=http 
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

以上。

2018/04/05 追記
一部間違っている箇所をご指摘いただいたので、修正しました。
勉強不足ですみません。。教えてくださってありがとうございました。

53
69
3

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
53
69