[精讚] [會員登入]
8865

[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

你可能感興趣的文章

[Centos7] selinux 修改網頁連線的埠號 想把網頁改到非標準的80埠,在selinux上要如何修改?

[MSSQL] 使用linux連接微軟的MS-SQL 職務上需要連接到mssql,這是我第一次試圖在linux連接mssql。

使用RRDTOOL來繪製流量圖 使用RRDTOOL來製作流量圖的簡略說明

[CENTOS7] 使用 logrotate 來整理wildfly日誌檔 解決 wildfly 的日誌檔不斷長大的問題

[CENTOS8] Failed to download metadata for repo 'appstream' 的問題 Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist的解決方法。

[Centos8] 利用nmcli 設定ipv4 位址 Centos7之後修改網路設定變得比較麻煩一點,此篇介紹使用nmcli指令的方法

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[HP DL380G7] 生效啟動第3,4片網卡/開啟或關閉內建的網卡 HP DL380G7 預設第3,4片網卡裝完系統後找不到,難道是壞了?要怎麼辦?

UTF-8的網頁但IE8一片空白 UTF8編碼的網頁在Firefox 正常顯示、但IE8 就是空白,IE8編碼設定是「自動偵測」可是自動偵測到的是 big5...

[PHP] 檢查檔案是否是圖檔 使用getimagesize函數檢查檔案是否是圖檔

詭異的創業思維 創業的思維中,有多少銀彈,有多少技術,有多少人脈,有多少時間等等,每個都要考慮進去,以熱忱建立的關係脆弱的像蘇打餅乾一樣..

APACHE的記錄檔格式 LogFormat 語法 在APACHE中有定義一些記錄的語法模版 在 /etc/httpd/conf/httpd.conf 中: LogForm