向 TWCA 申請 SSL 憑證 (Ubuntu, Apache)

因為某些特殊緣故必須要向 TWCA (臺灣網路認證) 申請 SSL 憑證,已經申請好幾次了,每次做都還要去查資料,這篇文章把向 TWCA 申請 SSL 憑證的流程記下來。

TWCA 官網列出申請 SSL 憑證總共有 6 個步驟,接下來小蛙會著重在第 3 步:上傳 CSR (WEB) 以及第 6 步:安裝與設定 1.電子憑證 2.認證標章 (但不會使用認證標章)

申請 TWCA SSL 憑證:上傳 CSR (WEB)

產生主機 KEY 及 CSR 憑證請求檔

首先進到 /etc/ssl/ 產生金鑰 (private key)

cd /etc/ssl/
openssl genrsa -out server.key 2048

使用這把金鑰產生 CSR 憑證請求檔

openssl req -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) [AU]: TW (國家縮寫)
State or Province Name (full name) [Some-State]:Taiwan (國家名稱全名)
Locality Name (eg, city) []:Taichung (城市名稱)
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Noter (公司名稱) 
Organizational Unit Name (eg, section) []:IT (部門名稱)
Common Name (e.g. server FQDN or YOUR name) []:noter.tw (網域名稱)
Email Address []:[email protected] (電子郵件)

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:Noter (選用的公司名稱)

上傳 CSR

經過上述步驟後,在 /etc/ssl/ 下會多出 server.keyserver.csr,回到 TWCA 官網 第 3 點的描述

請將CSR檔案中的內容複製到下方的空欄中,注意複製的範圍應包括「—–BEGIN NEW CERTIFICATE REQUEST—–」到「—–END NEW CERTIFICATE REQUEST—–」的宣告文字。

透過 cat 印出剛剛產生的 CSR

cat server.csr

-----BEGIN CERTIFICATE REQUEST-----
...
...
...
-----END CERTIFICATE REQUEST-----

複製「—–BEGIN NEW CERTIFICATE REQUEST—–」到「—–END NEW CERTIFICATE REQUEST—–」範圍的文字,貼到 TWCA 官網中點擊繼續

TWCA SSL 憑證:上傳 CSR 1

確認紅框內的資訊後,輸入密碼、選擇驗證網域的方式、及表單編號

TWCA SSL 憑證:上傳 CSR 2

點擊繼續後即上傳完成

TWCA SSL 憑證:上傳 CSR 3

驗證網域所有權

小蛙這邊使用的是「網站檔案驗證」(上一步選擇的),收到操作手冊與驗證檔

TWCA SSL 憑證:驗證網域

在網站根目錄建立對應的資料夾及 whois.txt 檔案,並將 email 附件中的 whois.txt 貼上,也可以直接把附件中的檔案解壓縮後,直接上傳到網站根目錄中。例如小蛙的網站根目錄是 /var/www/html/

mkdir -p /var/www/html/.well-known/pki-validation/
vim /var/www/html/.well-known/pki-validation/whois.txt

完成後確定從 email 給的連結可以看到剛剛的 whois.txt 檔案,即可與 TWCA 聯繫進行下一步。

安裝與設定 1.電子憑證 2.認證標章

等待 TWCA 審核完成後收到一封 email 及一個壓縮檔,解壓縮後包含我們前面產生的檔案,共有以下

  1. server.key 伺服器金鑰
  2. server.csr TWCA CSR 憑證申請檔
  3. root.cer 根憑證
  4. server.cer 伺服器憑證
  5. uca.cer 中繼憑證

這邊會用到的為 1, 4, 5 三個檔案,如果收到的檔案是 uca_1.ceruca_2.cer,可先透過 cat uca_2.cer uca_1.cer > uca.cer 將檔案合併成一個。

以小蛙自己的環境 Apache2.4 為例,首先複製一份預設的檔案出來改

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default-le-ssl.conf
vim /etc/apache2/sites-available/000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName 改成 TWCA 申請 SSL 的網址
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Include /etc/letsencrypt/options-ssl-apache.conf  # 這個是因為小蛙有使用 letsencrypt
        SSLCertificateFile /etc/ssl/server.cer            # 加上這個
        SSLCertificateKeyFile /etc/ssl/server.key         # 加上這個
        SSLCertificateChainFile /etc/ssl/uca.cer          # 加上這個
</VirtualHost>
</IfModule>

儲存後啟用該站點,檢查沒問題後重起 apache 就完成囉!

a2ensite 000-default-le-ssl.conf
apache2ctl configtest
Syntax OK
service apache2 graceful

增加安全性系列:

    發佈留言

    發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

    這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料