[SSL] 採用的是 [X.509] 由上而下金字塔式的憑證制度 (Root CA >> CA >> Certificate)
在 [X.509] 中每一個合格的憑證上都會有一個簽名。最下層的憑證上,會有一個認證中心 (CA) 的簽名表示此憑證為認證中心 (CA) 檢查過確認所有者資料無誤。中間的認證中心 (CA) 上,也會有管轄它的最高層認證中心 (Root CA) 的簽名,表示最高層認證中心授權給它,可以簽發別人的憑證。
程式自己會認得幾家可靠的認證中心 (CA),碰到 [SSL] 網站時,雖然不認得伺服器的憑證 (Certificate),但只要那個憑證上有自己認得的認證中心 (CA) 簽名保證過,那個憑證就沒有問題。
一般商業型網站申請憑證大致可分為下列的步驟 (CA for Apache):
一般商業型網站申請憑證大致可分為下列的步驟 (CA for IIS):
請依如下步驟來產生亂數檔案
#cd /etc/ssl //切換路徑 #openssl rand -out /etc/ssl/private/.rand 1024 //製作亂數檔 #vi /etc/ssl/openssl.cnf //修改 openssl.cnf 內容如下 dir = ./demoCA //預設值 dir = /etc/ssl //修改後
請依如下步驟製作最上層認證中心的 Private Key (rootca.key)
#cd /etc/ssl //切換路徑 #openssl genrsa -des3 -out rootca.key 2048 //產生最上層的 Private Key(rootca.key) Generating RSA private key, 2048 bit long modulus ......................................................................+++ ..........+++ e is 65537 (0x10001) Enter pass phrase for rootca.key: //輸入密碼 (Rootca 的 Private key 密碼) Verifying - Enter pass phrase for rootca.key: //再輸入一次密碼(Private key 的密碼)
請依如下步驟填寫相關資訊來產生憑證申請書 (rootca.req)
#openssl req -new -key rootca.key -out rootca.req //產生 rootca.req (憑證申請書) Enter pass phrase for rootca.key: //輸入密碼 (Rootca 的 Private key 密碼) 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 //國碼臺灣是 TW State or Province Name (full name) [Some-State]:Taiwan R.O.C //國名臺灣填 Taiwan Locality Name (eg, city) []:Taipei //地名 Organization Name (eg, company) [Internet Widgits Pty Ltd]:FreeBSD Personal Reserach //組織單位名稱 Organizational Unit Name (eg, section) []:FreeBSD Personal Reserach //部門名稱 Common Name (eg, YOUR name) []:Weithenn-Wang //憑證的名稱 Email Address []:weithenn@weithenn.org //申請單位的聯絡信箱 Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:按 Enter 直接跳過 //申請書的密碼 An optional company name []:按 Enter 直接跳過 //憑證代辦公司的名稱
因為此次實作自已是擔任 RootCA 的角色所以自己給自己簽名,而最高層認證中心 (RootCA) 最好永遠不要過期。因為 RootCA 一旦過期則所有原來它 (RootCA) 所簽發出去的憑證也都要重簽,且所有 SSL 程式也都要重新設定。因此我們將 RootCA 效期簽 7305 天 (20年)。若未設設定效期的話則預設為 30 天 (一個月)。
#openssl x509 -req -days 7305 -sha1 -extfile /etc/ssl/openssl.cnf -extensions v3_ca -signkey rootca.key -in rootca.req -out rootca.crt Signature ok subject=/C=TW/ST=Taiwan R.O.C/L=Taipei/O=FreeBSD Personal Reserach/OU=FreeBSD Personal Reserach /CN=Weithenn-Wang/emailAddress=weithenn@weithenn.org Getting Private key Enter pass phrase for rootca.key: //輸入密碼(Rootca 的 Private key 密碼)
建議伺服器的 Private Key 不要設定密碼,否則當 SSL 伺服器程式啟動的時候,Web Server 讀取憑證和 Private Key 時便會詢問一次密碼 (您必須親自在 Console 端輸入)。所以建議您若沒有 [KVM Over IP] 設定或您的機器在遠端的話請不要設定 Private Key 密碼。
請鍵入如下指令產生伺服器用的 Private Key (weithenn.org.key)
#openssl genrsa -out weithenn.org.key 2048 //產生伺服器用的 Private key (weithenn.org.key) Generating RSA private key, 2048 bit long modulus ......................................................+++ ......+++ e is 65537 (0x10001)
請依如下步驟填寫相關資訊來產生憑證申請書 (weithenn.org.req)
#openssl req -new -key weithenn.org.key -out weithenn.org.req //產生伺服器憑證申請書 (weithenn.org.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) [AU]:TW //國碼臺灣是 TW State or Province Name (full name) [Some-State]:Taiwan R.O.C //國名臺灣填 Taiwan Locality Name (eg, city) []:Taipei //地名 Organization Name (eg, company) [Internet Widgits Pty Ltd]:FreeBSD Personal Reserach //組織單位名稱 Organizational Unit Name (eg, section) []:FreeBSD Personal Reserach //部門名稱 Common Name (eg, YOUR name) []:www.weithenn.org //憑證的名稱 (通常為伺服器 FQDN) Email Address []:weithenn@weithenn.org //申請單位的聯絡信箱 Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: 按 Enter 直接跳過 //申請書的密碼 An optional company name []: 按 Enter 直接跳過 //憑證代辦公司的名稱
最高層認證中心 (RootCA) 簽發 Web 伺服器 (Apache伺服器) 3650 天 (十年) 的憑證 (RootCA >> Server)
#openssl x509 -req -days 3650 -sha1 -extfile /etc/ssl/openssl.cnf -extensions v3_req -CA rootca.crt -CAkey rootca.key \ -CAserial rootca.srl -CAcreateserial -in weithenn.org.req -out weithenn.org.crt //接續上一行 Signature ok subject=/C=TW/ST=Taiwan R.O.C/L=Taipei/O=FreeBSD Personal Research/OU=FreeBSD Personal Research /CN=www.weithenn.org/emailAddress=weithenn@weithenn.org Getting CA Private Key Enter pass phrase for rootca.key: //輸入密碼 (RootCA 的 Private Key 密碼)
伺服器憑證產生完成之後將相關檔案路徑設定至 Apache 設定檔 (httpd.conf) 內
#vi /usr/local/etc/apache/httpd.conf //修改 Apache 設定檔內容如下 SSLCertificateFile /etc/ssl/weithenn.org.crt //指定使用的憑證 SSLCertificateKeyFile /etc/ssl/weithenn.org.key //指定使用的 Private Key
完成後即可測試您的 Apache 是否具有 [HTTPs] 功能。
ex. http'''s'''://www.weithenn.org
由於此次實作為自行產生憑證也就是自已擔任最高層認證中心 (RootCA),因為它 (RootCA) 已經是最上層沒有再上層可以給它簽名了所以只好自己簽自己,憑證上的簽名是自己簽的。以下實作為自行產生 RootCA 之後簽憑證給自已的 Web Server (Apache),也因為 RootCA 是自行產生的所以一般的瀏灠器不可能認得,因此連結憑證畫面會出現一個驚嘆號而另外二個狀態則是 OK 的 (如左下圖),若您將 RootCA 匯入至瀏灠器後則憑證畫面應該三個狀態都要是打勾才正確。