[精讚] [會員登入]
8445

[Centos7] SSL自簽憑證+APACHE+Selinux

SSL的自簽憑證

分享此文連結 //n.sfs.tw/11830

分享連結 [Centos7] SSL自簽憑證+APACHE+Selinux@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 11:51:00 最後編修
2017-09-26 14:06:16 By 張○○
 

自動目錄

SSL的自簽憑證,窮人SSL,雖然網頁會跳SSL不合法的警告,此文主要參考[1]提供的指令。

SSL的憑證信任鍊 trust china 是這樣: 根憑證機構(Root CA) =授權=> 中繼憑證機構(Intermedia CA) =授權=> 終端憑證機構(Endpoint CA)

  * CA就是授權機構 certificate authority

 

一、自簽Root CA

建立私鑰

# openssl genrsa  -out RootCA.key 2048

產生根憑證申請檔

# openssl req -new -key RootCA.key -out RootCA.req

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) [GB]:TW
State or Province Name (full name) [Berkshire]:Taiwan
Locality Name (eg, city) [Newbury]:Taichung
Organization Name (eg, company) [My Company Ltd]:Somewhere Co.Ltd.
Organizational Unit Name (eg, section) []:Sleep Dep.
Common Name (eg, your name or your server's hostname) []:hostname.example.com
Email Address []:<按ENTER>

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

利用申請檔產生憑證檔

# openssl x509 -req -days 3650 -sha256 -extensions v3_ca -signkey RootCA.key -in RootCA.req -out RootCA.crt

產生憑證檔RootCA.crt

接下來要拿根憑證來簽發終端憑證,因為沒有中繼憑證,所以直接來作伺服器憑證,也就是終端憑證。

 

二、製作終端憑證(伺服器憑證)

建立伺服器私鑰

# openssl genrsa -out ServerCert.key 2048

同上,產生憑證申請檔

# openssl req -new -key ServerCert.key -out ServerCert.req
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) [GB]:TW
State or Province Name (full name) [Berkshire]:Taiwan
Locality Name (eg, city) [Newbury]:Taichung
Organization Name (eg, company) [My Company Ltd]:Eat Co.Ltd.
Organizational Unit Name (eg, section) []:Pizza Dep.
Common Name (eg, your name or your server's hostname) []:hostname.example.com
Email Address []:<按ENTER>
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:<按ENTER>
An optional company name []:<按ENTER>

Common Name 中可以使用 wildcard寫法,例如 *.example.com

產生流水號檔

# echo 1000 > RootCA.srl

產生憑證檔一樣簽十年

# openssl x509 -req -days 3650 -sha256 -extensions v3_req -CA RootCA.crt -CAkey RootCA.key -CAserial RootCA.srl -CAcreateserial -in ServerCert.req -out ServerCert.crt

 

三、SELINUX 及搬移

# chcon -u system_u -t cert_t ServerCert.key ServerCert.crt RootCA.crt

複製到指定的位置

# cp ServerCert.key /etc/pki/tls/private/

# cp *.crt /etc/pki/tls/certs

 

 

四、APACHE上設定

修改 /etc/httpd/conf.d/ssl.conf

# 註解這兩項
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

# 修改
Listen 443
ServerName
hostname.example.com:443
SSLEngine on

# 加入這三項
SSLCertificateFile /etc/pki/tls/certs/ServerCert.crt
SSLCertificateKeyFile /etc/pki/tls/private/ServerCert.key
SSLCACertificateFile /etc/pki/tls/certs/RootCA.crt

其中ServerName要和上面設定的一樣

重新啟動 apache

# apachectl restart

 

錯誤排除

啟動失敗log檔在 /etc/httpd/logs/ssl_error_log 或是 /var/log/message 看你的設定。

[warn] RSA server certificate CommonName (CN) `somehost.example.com' does NOT match server name!?

  可能解法:你的 ServerCert.key 檔裡面設定的網域名稱和你的 ssl.conf中設定的名稱不一樣

[error] Unable to configure RSA server private key

  可能解法:你的 ServerCert.key 檔不正確,請檢查或重新建立

 

相關連結

[HTTPS] Ubuntu +SSL +正式的CA 設定

[SSL] 免費的SSL憑證(三個月)

[Centos7] HTTPS/SSL憑證的SELINUX設置

參考資料

[1] https://www.bear2little.net/wordpress/?p=401

[2] http://wiki.weithenn.org/cgi-bin/wiki.pl?SSL_Certificate-%E8%87%AA%E8%A1%8C%E7%94%A2%E7%94%9F_SSL_%E6%86%91%E8%AD%89 有原理說明

[3] https://en.wikipedia.org/wiki/Root_certificate

END

你可能感興趣的文章

[Centos6] 安裝wordpress Centos 6下安裝wordpress,非常簡單

[Rocky9] codeignitor4+ nginx+ php8.1-fpm + docker compose 這次的目標是把服務裝在docker,裡面放入 nginx 和 php8.1-fpm的 docker container, 並採用codeignitor4。

[Centos7] 安裝sphinx+php7 在centos7上安裝sphinx,在PHP7上運作正確

[Linux] 刪除last/lastb登出入記錄 linux shell的登出入記錄

Linux shell 的date表示法 linux下SHELL中的date表示法

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

UTF-8 BOM (Byte Order Mark) 的問題 在 Michael Kaplan 那看到 Every character has a story #4: U+feff

安裝SPHINX支援中文 新版本的 sphinx 和舊版不同,網路上很多範例和教學是不能用的。此文是安裝和設定方法分享

[札記] 2016.7~12月札記 札記,只是札記

[CodeIgniter3] 解決無法上傳特定檔案(.sb2)的問題 上傳時出現The filetype you are attempting to upload is not allowed,要怎麼解決?

[AS3] 我做的唯一一個Flash As3遊戲UFO INVADSION [AS3] 我做的唯一一個Flash As3遊戲,是第一個也是最後一個,後來就沒再寫as3,不過as3還滿好玩的。