目次

SSL自己証明書

自己証明書を作成する

秘密鍵

発行される証明書と対になるファイル。

e.g. パスフレーズ無し、鍵長2,048bitの秘密鍵「server.key」を作成する

[root@adm01 ~]# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
........................+++
...+++
e is 65537 (0x10001)
[root@adm01 ~]# ll
-rw-r--r--. 1 root root  1675 11月  4 18:05 2014 server.key
[root@adm01 ~]# cat server.key
-----BEGIN RSA PRIVATE KEY-----
~省略~
-----END RSA PRIVATE KEY-----

CSR

証明書の申請時に提出するファイル。

e.g. 作成した秘密鍵「server.key」で、CSR「server.csr」を作成する

[root@adm01 ~]# openssl req -new -key server.key -out aserver.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) []:Osaka
Locality Name (eg, city) [Default City]:Osaka-shi,Cyuoh-ku
Organization Name (eg, company) [Default Company Ltd]:Clown Inc.
Organizational Unit Name (eg, section) []:Clown Section
Common Name (eg, your name or your server's hostname) []:clown.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@adm01 ~]# ll
-rw-r--r--. 1 root root  1029 11月  4 18:08 2014 server.csr
-rw-r--r--. 1 root root  1675 11月  4 18:05 2014 server.key
[root@adm01 ~]# cat server.csr                                                         
-----BEGIN CERTIFICATE REQUEST-----
~省略~
-----END CERTIFICATE REQUEST-----

証明書(公開鍵)

いわゆる、SSL証明書。

e.g. 有効期限10年で、秘密鍵「server.key」とCSR「server.csr」を使用して証明書「server.crt」を作成する

[root@adm01 ~]# openssl x509 -days 3650 -req -signkey server.key -in server.csr -out server.crt
Signature ok
subject=/C=JP/ST=Osaka/L=Osaka-shi,Cyuoh-ku/O=Clown Inc./OU=Clown Section/CN=clown.com
Getting Private key
[root@adm01 ~]# ll
-rw-r--r--. 1 root root  1257 11月  4 18:09 2014 server.crt
-rw-r--r--. 1 root root  1029 11月  4 18:08 2014 server.csr
-rw-r--r--. 1 root root  1675 11月  4 18:05 2014 server.key
[root@adm01 ~]# cat server.crt
-----BEGIN CERTIFICATE-----
~省略~
-----END CERTIFICATE-----

秘密鍵と証明書を一発で作成する

※CSRは作成されない。

openssl req -nodes -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -new -x509 -sha256 -days 365 -out /etc/nginx/ssl/nginx.crt 

Apacheで利用する

適当な場所にデジタル証明書、秘密鍵を設置

[root@adm01 ~]# mkdir /etc/httpd/conf/ssl.crt/
[root@adm01 ~]# mv server.crt /etc/httpd/conf/ssl.crt/
[root@adm01 ~]# mkdir /etc/httpd/conf/ssl.key/
[root@adm01 ~]# mv server.key /etc/httpd/conf/ssl.key/
[root@adm01 ~]# rm -rf server.csr
[root@adm01 ~]# chmod 700 /etc/httpd/conf/ssl.key
[root@adm01 ~]# chmod 400 /etc/httpd/conf/ssl.key/server.key

ssl.confを編集する

[root@adm01 ~]# vi /etc/httpd/conf.d/ssl.conf

#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

設定反映

[root@adm01 ~]# /etc/init.d/httpd configtest
Syntax OK
[root@adm01 ~]# /etc/init.d/httpd reload
httpd を再読み込み中: 

Nginxで利用する

適当な場所にデジタル証明書、秘密鍵を設置

[root@adm01 ~]# mkdir /etc/nginx/conf.d/ssl.crt/
[root@adm01 ~]# mv server.crt /etc/nginx/conf.d/ssl.crt/
[root@adm01 ~]# mkdir /etc/nginx/conf.d/ssl.key/
[root@adm01 ~]# mv server.key /etc/nginx/conf.d/ssl.key/
[root@adm01 ~]# rm -rf server.csr
[root@adm01 ~]# chmod 700 /etc/nginx/conf.d/ssl.key
[root@adm01 ~]# chmod 400 /etc/nginx/conf.d/ssl.key/server.key

nginx の SSL設定ファイルを設定

# mv /etc/nginx/conf.d/example_ssl.conf /etc/nginx/conf.d/ssl.conf
# vi /etc/nginx/conf.d/ssl.conf

5行目付近「sever {」以下のコメントをすべてはずす。
10~11行目付近、鍵のパスを修正

# HTTPS server
#
server {
    listen       443 ssl;
    server_name  localhost;

#    ssl_certificate      /etc/nginx/cert.pem;
#    ssl_certificate_key  /etc/nginx/cert.key;
    ssl_certificate      /etc/nginx/conf.d/ssl.crt/server.crt;
    ssl_certificate_key  /etc/nginx/conf.d/ssl.key/server.key;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

SSL 有効化(nginx 再起動)

# /etc/init.d/nginx restart