https簡介
http協議在網絡中是以明文傳輸的,自然安全性不高。https使用了ssl加密機制,彌補了這點不足。
http:80端口
https:443端口
案例:
自建CA,實現一個https安全的會話機制。
這裏爲了方便期間,CA和web服務器是用一臺服務器共同實現
web生成並提交請求
[root@web CA]# (umask 077;openssl genrsa 1024 > httpd.key) [root@web CA]# openssl req -new -key httpd.key -out httpd.csr
自建ca
[root@web CA]# (umask 077;openssl genrsa 2048 > private/cakey.pem) [root@web CA]# openssl req -new -x509 -key private/cakey.pem -days 365 -out cacert.pem
web服務器端發給CA簽署(可以使用scp命令)
[root@web ssl]# openssl ca -in httpd.csr -out httpd.crt
簽署成功後有CA服務器發送個web,此時web服務器端就得到了3個相關私鑰和證書
[root@web ssl]# pwd /etc/httpd/conf/ssl [root@web ssl]# ls httpd.crt httpd.csr httpd.key
接下來開始部署我們web支持ssl功能了,想要支持ssl,apache必須加載mod-ssl模塊
查看我們的apache是否已經配置支持了ssl
[root@web ~]# grep mod_ssl /etc/httpd/conf/httpd.conf # (e.g. :80) if mod_ssl is being used, due to the nature of the [root@web ~]# grep mod_ssl /etc/httpd/conf.d/*.conf
既然grep不到,說明沒有支持,此時我們需要安裝一個mod_ssl
[root@web ~]# yum -y install mod_ssl [root@web ~]# rpm -ql mod_ssl /etc/httpd/conf.d/ssl.conf /usr/lib64/httpd/modules/mod_ssl.so /var/cache/mod_ssl /var/cache/mod_ssl/scache.dir /var/cache/mod_ssl/scache.pag /var/cache/mod_ssl/scache.sem
查看一下新安裝的配置文件吧,這裏加載了ssl的模塊支持,和新的端口號443等。
[root@web ~]# vim /etc/httpd/conf.d/ssl.conf LoadModule ssl_module modules/mod_ssl.so Listen 443 <VirtualHost _default_:443>
我們需要修改一下這個配置文件,讓其知道我們的web服務器的證書在哪裏,私鑰在哪裏,以方便把我們的證書發給客戶端,並且用私鑰解密客戶端用我們的證書中的公鑰加密的對稱密碼。
<VirtualHost _default_:443> # General setup for the virtual host, inherited from global configuration DocumentRoot "/var/www/html" ServerName www.magelinux.com:443--------要與證書申請時對應 ErrorLog logs/ssl_error_log-------------日誌文件 TransferLog logs/ssl_access_log LogLevel warn---------------------------日誌級別 SSLEngine on----------------------------是否開啓ssl功能 # SSL Protocol support: # List the enable protocol levels with which clients will be able to # connect. Disable SSLv2 access by default: SSLProtocol all -SSLv2 # SSL Cipher Suite: # List the ciphers that the client is permitted to negotiate. # See the mod_ssl documentation for a complete list. SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW # Server Certificate: SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt------證書位置 # Server Private Key: SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key---私鑰位置
通過命令ss -tunl 可以看出此時443端口已經開始監聽了,此時通過瀏覽器訪問會提示證書不守信用,有風險,這是因爲我們的證書是我們自建的CA簽署的,客戶端沒有認可
將CA證書導入到我們的客戶端電腦,並安裝。
當然我們也可以用openssl s_client -connect ip:443來診斷(man s_client)