2013年10月1日 星期二

在 App Engine 中設定 "Trusted" HTTPS Intermediate Certificate

在 Google App Engine 中,如果要讓 App Engine 支援 HTTPS 連線,必須要到 Google Apps 的 Admin Panel 裏面設定你的 PEM encoded X.509 certificate 和 Unencrypted PEM encoded RSA private key

大概長得像上面這樣,上面那個通常是直接傳網域代管商交給你的 .crt 檔案,下面則是傳你自己做好的的 Private Key(不過還需要用 PEM 來 encode),總之,這個步驟本來是相當簡單的。

不過在 App Engine 中,因爲 Google 沒有讓你上傳 Intermediate Certificate 的地方,所以要用一個特殊的 Workaround 來解決。否則,在對 HTTPS Certification 要求比較嚴格的環境中,像是 Firefox 或是網域代管商的 Verifier,都會顯示你的網站是有 SSL 加密但是是「不可信任的(Untrusted)」。




首先,當你申請完成了 SSL Certification,這邊以 GoDaddy(網域代管)跟 example.com(網域名稱)爲例,你應該會得到兩個檔案:gd_bundle.crt 和 example.com.crt 這兩個 Certification。

gd_bundle.crt 就是 GoDaddy 核發的 Intermediate Certificate,example.com.crt 則是你的網站的 Certification。

Step 1. 產生 Keypair


Keypair 就是 public key 加上 private key,這兩組是用來確定你真的是網站所有人,而不是只是從 GoDaddy 網站上偷到你的 Certification。

產生的方式很簡單,首先你的系統要裝有 openssl(Mac OS X 內建,Linux 則需透過 package manager 安裝),然後執行下面指令

openssl req -out example.com-pub.csr -new -newkey rsa:2048 -nodes -keyout example.com-priv.key

他會開始問你問題,你就像回答問卷一樣如實回答完就好,然後你就會得到兩個檔案 example.com-pub.csr 就是你的 public key,反之 example.com-priv.key 就是你的 private key,這兩個檔案的名字可以隨便取,但是切記一定要收好而且「不要搞混誰是 public 誰是 private」,不然之後一切就又要重來。(不過重來也沒什麼大不了的....多創一個 key 很不方便而已)

Step 2. 把 Intermediate Certificate 加入你的網站憑證中


這個步驟是關鍵但其實很簡單,你只要把 Intermediate Certificate 加到網站 Certificate 的後面就好了,Intermediate Certificate 裏面有兩個 Certificate,網站的裏面只有一個,所以兩個合在一起只要這樣下指令就好

cat example.com.crt gd_bundle.crt > example.com-intermediate.pem

你就會得到一個有 intermediate 的 certification,example.com-intermediate.pem,這個檔案大概會長得像這樣:

-----BEGIN CERTIFICATE-----
blahblahblah…
(from example.com.crt)
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
blahblahblah…
(from gd_bundle.crt)
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
blahblahblah…
(from gd_bundle.crt)
-----END CERTIFICATE-----

這個檔案等等就是要用來上傳到 Google Admin Panel 的 PEM encoded X.509 certificate 中(上面那個)。

Step 3. 把你的 Private Key 用 PEM 來 encode


openssl rsa -in example.com-priv.key -out example.com-priv.key.pem

就會產生出 example.co-priv.key.pem 這個檔案,就是要用來上傳到 Unencrypted PEM encoded RSA private key 中的。

Final. 確定網站已經被 Trusted


可以使用 Firefox 或是 http://www.digicert.com/help/ 這類的網站來測試看看網站有沒有被 Trusted,經過筆者測試的結果是立刻就生效囉!

沒有留言:

張貼留言