Web 伺服器(第二版)-網頁內容加密傳輸SSL

本人曾在 Web 伺服器(第一版)的第三個網頁中使用輸入帳密才能登入網頁,但其實帳密資料在傳輸的過程中是沒有加密的,故此篇文章將教您加密資料。

以下將以 Web 伺服器(第一版)的第三個網頁示範加密流程。
安裝金鑰產生套件 openssl # yum install -y openssl
安裝 Apache ssl 模組:mod_ssl # yum install -y mod_ssl
開啟防火牆 # iptables -A INPUT -i eth0 -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A INPUT -i eth0 -p tcp -m state --state INVALID,NEW -j DROP
# iptables -A INPUT -i eth0 -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT

產生憑證 # /etc/pki/tls/certs
# make server.key  // 產生私鑰(Private Key)
 umask 77 ; \
   /usr/bin/openssl genrsa -aes128 2048 > server.key
 Generating RSA private key, 2048 bit long modulus
 .........................................+++
 ................+++  e is 65537 (0x10001)
 Enter pass phrase:  // 輸入口令,至少 4 個字元
 Verifying - Enter pass phrase:
# ll server.key
 -rw-------. 1 root root 1766 Dec 17 15:13 server.key

移除私鑰口令 # openssl rsa -in server.key -out www139.key
 Enter pass phrase for server.key:
 writing RSA key
# ll www139.key
 -rw-r--r--. 1 root root 1675 Dec 17 15:30 www139.key

憑證請求檔(CSR、Certificate Signing Request) # make www139.csr
 umask 77 ; \
   /usr/bin/openssl req -utf8 -new -key www139.key -out www139.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) [XX]:TW
 State or Province Name (full name) []:TAIWAN
 Locality Name (eg, city) [Default City]:TAIPEI
 Organization Name (eg, company) [Default Company Ltd]:
 Organizational Unit Name (eg, section) []:
 Common Name (eg, your name or your server's hostname) []:
 Email Address []:XXX@XXX.com

 Please enter the following 'extra' attributes
 to be sent with your certificate request
 A challenge password []:
 An optional company name []:

# ll www139.csr
 -rw-------. 1 root root 1013 Dec 17 15:34 www139.csr

自簽電子證書(Self-Sign Certificate) # openssl x509 -req -in www139.csr -signkey www139.key -out www139.crt -days 365
 Signature ok
 subject=/C=TW/ST=TAIWAN/L=TAIPEI/O=NTU/OU=CC/emailAddress=CC@com.tw
 Getting Private key

在需要加密的網頁中,加入以下設定  <VirtualHost *:443>
   DocumentRoot /var/www/svhost
   ServerName secretapacheserver139.example.com
   SSLEngine on  // 以下 5 個參數是參考 /etc/httpd/conf.d/ssl.conf
   SSLProtocol all -SSLv2
   SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
   SSLCertificateFile /etc/pki/tls/certs/www139.crt
   SSLCertificateKeyFile /etc/pki/tls/private/www139.key

   <Directory /var/www/svhost>
    AuthName "Secret Hideout"
    AuthType basic
    AuthUserFile /var/www/svhost/users
    require valid-user
   </Directory>
 </VirtualHost>

沒有留言:

張貼留言