製作憑證與建立安全連線

設定 Apache2 + SSL
在 Apache2 上面建立 SSL 連線,首先您要有伺服器憑證。
假如您尚未有伺服器憑證,請買一個、要一個、請別人做一個,或者參考其它文章自行製作。
‧自行製作憑證請注意:
憑證名稱 (Common Name) 請與伺服器名稱相同 (Server Name)。
製作好的憑證在使用上可能會出現「憑證無效」、「憑證無法驗證」等的訊息。
因為這些憑證的簽發者不是客戶端所已知且被信任的,所以您需要將簽發此憑證的根憑證發散出去給需要用到這些憑證的使用者。
在 Internet Explorer 上,下載根憑證後開啟可以將憑證匯入系統之內,
日後此根憑證所簽發的憑證都會被信任。
注意:發散的是憑證檔 (.crt, ex. RootCA.crt),不是 Private Key (.key, ex. RootCA.key) 檔。
From:http://giayiu.twbbs.org/FreeBSD/

‧使用 Ports 安裝 Apache2:
# 使用 CVSup 更新 Ports。
cd /usr/ports
make update
# 安裝 apache2
cd /usr/ports/www/apache2
make install clean
‧設定憑證資料:
將憑證檔與 Private Key 檔放至 /usr/local/etc/apache2/ssl.crt
Private Key 檔請更名為 server.key
憑證檔請更名為 server.crt
‧憑證設定注意:
您可以自行決定檔名及目錄,但請注意:
請修改 /usr/local/etc/apache2/ssl.conf 檔
「SSLCertificateFile」、「SSLCertificateKeyFile」的位置。
請修改 /usr/local/etc/rc.d/apache2.sh
將 start) 部份的 $PREFIX/etc/apache2/ssl.crt/server.crt 改成您的憑證位置,
這樣在開機啟動時,才會啟動 SSL。
/usr/local/etc/apache2/ssl.conf 預設的檔案位置:
Server Certificate:
SSLCertificateFile /usr/local/etc/apache2/ssl.crt/server.crt
Server Private Key:
SSLCertificateKeyFile /usr/local/etc/apache2/ssl.key/server.key
自行建立根憑證並簽發伺服器憑證
當你想為您的伺服器建立 SSL 連線時 (ex. web, smtp, pop3, imap ...),您首先需要製作一些憑證給伺服器以及客戶端使用。
假如您不需要花錢去申請公正憑證中心所簽發的憑證 (ex. VeriSign ...),您就可以使用如下的方式自行當做憑證中心簽發憑證給自行使用。
發行憑證流程:
1.使用者建立 Private Key,使用所建立的 Private Key 產生憑證申請書送至憑證中心。
2.憑證中心收到憑證申請書後,使用憑證中心的憑證對使用者的憑證申請書做簽名,完成簽發憑證。
建立憑證都需要三個步驟:
1. 建立 Private Key。
2. 使用 Private Key 產生憑證申請書。
3. 為憑證申請書籤名做簽發憑證。
所以您需要:
‧產生根憑證 (Root CA)。(簽發伺服器憑證用。)
要先建立根憑證的 Private Key。
產生根憑證的申請書。
使用根憑證本身簽髮根憑證。(因為您是憑證鍊的最高等級)
‧產生伺服器憑證。
建立伺服器憑證的 Private Key。
產生伺服器憑證的申請書。
使用根憑證簽發伺服器憑證。
‧在製作之前,有幾點要注意:
請自行更改所有的輸入、輸出檔名及路徑,放在 /usr/local/CA 是個不錯的選擇 。
所有的 Private Key 請妥善保存,不可外洩,否則其它人可以冒名簽發憑證,或者解碼被加密的資料。
根憑證的 Private Key 在製作時請一定要設定密碼。(不用做伺服器憑證)
根憑證的憑證名稱 (Common Name) 請填上發行人或單位名稱 (ex. ISU CNA Root CA)。
(不用做伺服器憑證)
伺服器憑證的憑證名稱 (Common Name) 請填上主機的全名 (ex. mail.cna.isu.edu.tw)。
憑證申請書填寫方法請見作法之後說明。
日後需要重新簽發或者製做其它的伺服器憑證,只需要重覆做產生伺服器憑證的動作就可以了。 (除非根憑證過期才需要重做根憑證。)
製作好的憑證在使用上可能會出現「憑證無效」、「憑證無法驗證」等的訊息。
因為這些憑證的簽發者不是客戶端所已知且被信任的,所以您需要將 RootCA.crt 發散出去給需要用到這些憑證的使用者。
在 Internet Explorer 上,下載根憑證後開啟可以將憑證匯入系統之內,日後此根憑證所簽發的憑證都會被信任。
注意:發散的是憑證檔 (.crt, ex. RootCA.crt),不是 Private Key (.key, ex. RootCA.key) 檔。
‧根憑證 (Root CA) 作法:
‧建立根憑證的 Private Key
# 在此步驟請務必要設定密碼!
openssl genrsa -des3 -out RootCA.key 2048
# 讓其它人無法讀寫 Private Key 檔。
chmod og-rwx RootCA.key
‧產生根憑證的申請書
# 請參考後面所提供的範例,Common Name 請不要填 Hostname。
# (不用做伺服器憑證)
# 在這要輸入產生根憑證 Private Key 時所設定的密碼。
openssl req -new -key RootCA.key -out RootCA.req
‧使用根憑證本身簽髮根憑證
# 在這要輸入產生根憑證 Private Key 時所設定的密碼。
# -days 設定根憑證的有效期限。(3650 ~= 十年)
openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey RootCA.key -in RootCA.req -out RootCA.crt
# 刪除申請書。(憑證已經簽發完成)
rm -f RootCA.req
‧伺服器憑證作法:
‧建立伺服器憑證的 Private Key
# 伺服器憑證不設定密碼。(否則開機啟動時會停住要求使用者輸入密碼。)
openssl genrsa -out HostCA.key 2048
# 讓其它人無法讀寫 Private Key 檔。
chmod og-rwx HostCA.key
‧產生伺服器憑證的申請書
# 請參考後面所提供的範例,Common Name 請填上 Hostname。
openssl req -new -key HostCA.key -out HostCA.req
‧使用根憑證簽發伺服器憑證
# 在這要輸入產生根憑證 Private Key 時所設定的密碼。
# -days 設定根憑證的有效期限。(365 ~= 一年)
# CAserial 是憑證序號檔。
# CAcreateserial 是前面所指定的序號檔不在時自動建立之。
openssl x509 -req -days 365 -sha1 -extensions v3_req -CA RootCA.crt -CAkey RootCA.key -CAserial RootCA.srl -CAcreateserial -in HostCA.req -out HostCA.crt
# 刪除申請書。(憑證已經簽發完成)
rm -f HostCA.req
‧憑證申請書填寫方法
/usr/local/CA# openssl req -new -key RootCA.key -out RootCA.req

Enter pass phrase for RootCA.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.
-----
# 填入TW 代表 Taiwan
Country Name (2 letter code) [AU]:TW
# TW 的全名 Taiwan
State or Province Name (full name) [Some-State]:Taiwan
# 城市名
Locality Name (eg, city) []:Kaohsiung Country
# 組織名稱 (公司名稱)
Organization Name (eg, company) [Internet Widgits Pty Ltd]:I-Shou University
# 單位名稱 (部門名稱)
Organizational Unit Name (eg, section) []:Campus Network Association
# 憑證名稱 (請注意先前所提到有關根憑證與伺服器憑證的注意事項)
Common Name (eg, YOUR name) []:CNA Root CA
# 填入聯絡信箱
Email Address []:cna@cna.isu.edu.tw
# 以下請都按 [Enter] 跳過,不需要用到。
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

設定 Sendmail + SASL2 + TLS (SSL)
‧SSL
要建立 SSL 連線,必需在編譯時加上 TLS Support 的選項。
TLS 是在原本的連接埠 (usually, 25) 上面使用 STARTTLS 來建立 SSL 連線。
‧SASL2
要使用 SASL2,要先安裝 cyrus-sasl2 與 cyrus-sasl2-saslauthd,再編譯 Sendmail。
SASL 讓使用者經過伺服器認證後,能使用本伺服器寄發信件。
‧Outlook Express 客戶端設定:
開啟 Outlook Express,點選[工具]→[帳戶]→雙點您的 Email 帳戶
SASL)→伺服器→勾選 "我的伺服器需要驗證"。
TLS)→進階→勾選在 外寄郵件-SMTP 下方的 "這個伺服器需要安全連線-SSL"。
‧使用 Ports 安裝 Sendmail + SASL2 + TLS:
# 使用 CVSup 更新 Ports。
cd /usr/ports/mail/sendmail
# 安裝 Sendmail Support SASL2+TLS
make -DSENDMAIL_WITH_TLS -DSENDMAIL_WITH_SASL2 install
‧使用 Port 安裝 Sendmail + SASL2 + TLS 請注意:
使用 ports 安裝並不會動到系統內建的 Sendmail。
在 /etc/rc.conf 設定 sendmail_enable="NONE" 以關閉系統內建的 Sendmail。
用 /usr/local/etc/rc.d/sendmail.sh start 啟動。(開機時會自動執行此檔)
想讓系統內建的 Sendmail Support SASL2+TLS 請參考非 Port 安裝部份。
請跳過此行之後的安裝步驟,跳至接在之後的 sendmail.cf 設定。
--------------< 非 Ports 安裝 Sendmail + SASL2 + TLS >---------------------
‧使用 Ports 安裝 cyrus-sasl2 與 cyrus-sasl2-saslauthd:
# 使用 CVSup 更新 Ports。
cd /usr/ports
make update
# 安裝 cyrus-sasl2
cd /usr/ports/security/cyrus-sasl2
make install clean
# 安裝 cyrus-sasl2-saslauthd
cd /usr/ports/security/cyrus-sasl2-saslauthd
make install clean
‧編譯 Sendmail
請在 /etc/make.conf 中加入一些 Sendmail 編譯選項:
SENDMAIL_CFLAGS=-I/usr/local/include -DSASL=2 -DSTARTTLS
SENDMAIL_LDFLAGS=-L/usr/local/lib
SENDMAIL_LDADD=-lsasl2 -lssl -lcrypto
您可以另外安裝 Sendmail 來替換系統內建的,或者在 /usr/src/usr.sbin/sendmail 重編譯系統內建的 Sendmail。
或者 make update 後順道 make world 一下。
重新編譯系統內建的 Sendmail 請用:
cd /usr/src/usr.sbin/sendmail ; make clea ; make all install
-------------------------< Sendmail.cf 檔設定 >----------------------------
‧設定 sendmail.cf
請在 /etc/mail 或 /usr/local/etc/mail 設定 {hostname}.mc
請加入以下設定:
dnl The following lines are used to enable the STARTTLS function
define(`confCACERT_PATH', `/usr/local/CA')
define(`confCACERT', `/usr/local/CA/RootCA.crt')
define(`confSERVER_CERT', `/etc/mail/CA/HostCA.crt')
define(`confSERVER_KEY', `/etc/mail/CA/HostCA.key')
define(`confCLIENT_CERT', `/etc/mail/CA/HostCA.crt')
define(`confCLIENT_KEY', `/etc/mail/CA/HostCA.key')
define(`confDONT_BLAME_SENDMAIL', `GroupReadableKeyFile')
dnl The following lines are used to enable CYRUS-SASL function
TRUST_AUTH_MECH(`LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `LOGIN PLAIN')dnl
製作並安裝 sendmail.cf
make install
‧憑證設定:
將根憑證 RootCA.crt 放在 /usr/local/CA/
將伺服器憑證: HostCA.crt 放在 /etc/mail/CA/
將伺服器憑證 Private Key: HostCA.key 放在 /etc/mail/CA
設定 Private Key 權限:
chgrp smmsp /etc/mail/CA/HostCA.key
chmod o-rwx /etc/mail/CA/HostCA.key
‧憑證設定注意:
您可以自行決定檔名及目錄,但請連同更改 {hostname}.mc 的 CA 位置。
記得重做 sendmail.cf (cd /etc/mail ; make install)。
設定 imap-uw (POP3,IMAP4) + SSL
‧Outlook Express 客戶端設定:
開啟 Outlook Express,點選[工具]→[帳戶]→雙點您的 Email 帳戶
POP3)→進階→勾選在 內收郵件-POP3 下方的 "這個伺服器需要安全連線-SSL"。(連接埠→995)
IMAP4)→進階→勾選在 內收郵件-IMAP 下方的 "這個伺服器需要安全連線-SSL"。(連接埠→993)
‧使用 Ports 安裝 imap-uw (POP3, IMAP4):
# 使用 CVSup 更新 Ports。
cd /usr/ports
make update
# 安裝 imap-uw Support SSL
cd /usr/ports/mail/imap-uw
make -DWITH_SSL_AND_PLAINTEXT install clean
‧憑證設定:
將伺服器憑證與 Private Key 放在同一個檔案。
cat HostCA.key > /usr/local/certs/imapd.pem
cat HostCA.crt >> /usr/local/certs/imapd.pem
設定憑證權限、ipop3d 憑證檔:
chmod og-rwx /usr/local/certs/imapd.pem
# pop3 與 imap4 使用同一個憑證。
ln -s /usr/local/certs/imapd.pem /usr/local/certs/ipop3d.pem
‧imap-uw 設定:
確認 /etc/services
imaps 993/tcp # imap4 protocol over TLS/SSL
imaps 993/udp
pop3s 995/tcp spop3 # pop3 protocol over TLS/SSL
pop3s 995/udp spop3
修改 /etc/inetd.conf
pop3 stream tcp nowait root /usr/local/libexec/ipop3d ipop3d
pop3s stream tcp nowait root /usr/local/libexec/ipop3d ipop3d
imap4 stream tcp nowait root /usr/local/libexec/imapd imapd
imaps stream tcp nowait root /usr/local/libexec/imapd imapd
重新啟動 inetd
kill -HUP `cat /var/run/inetd.pid`
‧憑證設定注意:
不可以更改憑證位置與檔名。(ipop3d/imapd 固定讀取路徑與檔名)

Please follow and like us:

One comment on “製作憑證與建立安全連線

  1. 如果要讓所有 subdomain 也都共用一個憑證的話(就是*.denniswave.twbbs.org都用同一個),在"Common Name (eg, YOUR name) []:"填入:"*.denniswave.twbbs.org"就可以囉。

2 Pings/Trackbacks 於 "製作憑證與建立安全連線"

  1. 為 MySQL 建立 openSSL 安全憑證...
    之前有寫過怎樣製作憑證來達成安全連線,不過只有寫到 apache 和 mail 相關的程式。最近安裝 MySQL 5.1 server 之後,想說有把支援 openssl 編進去,不過也不知道有沒有作用。所以看了一下:
    # mys...

denniswave 發表迴響 取消回覆

你的電子郵件位址並不會被公開。 必要欄位標記為 *