SSL協議即用到了對稱加密也用到了非對稱加密(公鑰加密),在創建傳輸鏈路時,SSL首先對對稱加密的密鑰使用公鑰進行非對稱加密,鏈路創建好以後,SSL對傳輸內容使用對稱加密。
1.對稱加密
速度高,可加密內容較大,用來加密會話過程當中的消息。
2.公鑰加密
加密速度較慢,但能提供更好的身份認證技術,用來加密對稱加密的密鑰。html
Https在創建Socket鏈接以前,須要進行握手,具體過程以下: linux
一、客戶端向服務端發送SSL協議版本號、加密算法種類、隨機數等信息。
二、服務端給客戶端返回SSL協議版本號、加密算法種類、隨機數等信息,同時也返回服務器端的證書,即公鑰證書
三、客戶端使用服務端返回的信息驗證服務器的合法性,包括:nginx
證書是否過時git
髮型服務器證書的CA是否可靠web
返回的公鑰是否能正確解開返回證書中的數字簽名算法
服務器證書上的域名是否和服務器的實際域名相匹配json
驗證經過後,將繼續進行通訊,不然,終止通訊
四、客戶端向服務端發送本身所能支持的對稱加密方案,供服務器端進行選擇
五、服務器端在客戶端提供的加密方案中選擇加密程度最高的加密方式。
六、服務器將選擇好的加密方案經過明文方式返回給客戶端
七、客戶端接收到服務端返回的加密方式後,使用該加密方式生成產生隨機碼,用做通訊過程當中對稱加密的密鑰,使用服務端返回的公鑰進行加密,將加密後的隨機碼發送至服務器
八、服務器收到客戶端返回的加密信息後,使用本身的私鑰進行解密,獲取對稱加密密鑰。 在接下來的會話中,服務器和客戶端將會使用該密碼進行對稱加密,保證通訊過程當中信息的安全。ubuntu
雙向認證和單向認證原理基本差很少,只是除了客戶端須要認證服務端之外,增長了服務端對客戶端的認證,具體過程以下: vim
api
驗證經過後,將繼續進行通訊,不然,終止通訊
四、服務端要求客戶端發送客戶端的證書,客戶端會將本身的證書發送至服務端
五、驗證客戶端的證書,經過驗證後,會得到客戶端的公鑰
六、客戶端向服務端發送本身所能支持的對稱加密方案,供服務器端進行選擇
七、服務器端在客戶端提供的加密方案中選擇加密程度最高的加密方式
八、將加密方案經過使用以前獲取到的公鑰進行加密,返回給客戶端
九、客戶端收到服務端返回的加密方案密文後,使用本身的私鑰進行解密,獲取具體加密方式,然後,產生該加密方式的隨機碼,用做加密過程當中的密鑰,使用以前從服務端證書中獲取到的公鑰進行加密後,發送給服務端
十、服務端收到客戶端發送的消息後,使用本身的私鑰進行解密,獲取對稱加密的密鑰,在接下來的會話中,服務器和客戶端將會使用該密碼進行對稱加密,保證通訊過程當中信息的安全。
環境說明:
ip
|
操做系統
|
角色
|
---|---|---|
192.168.0.162 | Ubuntu 16.04.5 | server |
192.168.0.120 | Ubuntu 16.04.5 | client |
請確保server已經安裝了Nginx
1. 生成一個 CA 私鑰: ca.key
mkdir /etc/nginx/keys/ cd /etc/nginx/keys/ openssl genrsa -out ca.key 4096
Generating RSA private key, 4096 bit long modulus
................................................................++
....................++
e is 65537 (0x10001)
2. 生成一個 CA 的數字證書: ca.crt
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt
Generating RSA private key, 4096 bit long modulus
................................................................++
....................++
e is 65537 (0x10001)
root@ubuntu:/etc/nginx/keys# openssl req -new -x509 -days 3650 -key ca.key -out ca.crt
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]:CN
State or Province Name (full name) [Some-State]:Shanghai
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Team
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:Certificate Authority 2019
Email Address []:
======== 服務端保存 server.key,提供 server.csr,簽名生成 server.crt ========
1. 生成 server 端的私鑰: server.key
openssl genrsa -out server.key 4096
Generating RSA private key, 4096 bit long modulus
.........++
....++
e is 65537 (0x10001)
2. 生成 server 端數字證書請求: server.csr
openssl req -new -key server.key -out server.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) [AU]:CN
State or Province Name (full name) [Some-State]:Shanghai
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Sidien Test
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:192.168.0.162
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
注意:因爲使用ip地址訪問的,因此Common Name,輸入ip便可。
若是使用域名訪問,那麼這一步,必須是域名才行!
3. 用 CA 私鑰簽發 server 的數字證書: server.crt
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650
Signature ok
subject=/C=CN/ST=Shanghai/O=Sidien Test/CN=192.168.0.162
Getting CA Private Key
======== 終端保存 client.key,提供 client.csr,簽名生成 client.crt ========
1. 生成客戶端的私鑰與證書: client.key
openssl genrsa -out client.key 4096
Generating RSA private key, 4096 bit long modulus
....++
...................................................................................................................................................................................................++
e is 65537 (0x10001)
2. 生成 client 端數字證書請求: client.csr
openssl req -new -key client.key -out client.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) [AU]:CN
State or Province Name (full name) [Some-State]:Shanghai
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Test
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:GP1700
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3. 用 CA 私鑰簽發 client 的數字證書: client.crt
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 3650
Signature ok
subject=/C=CN/ST=Shanghai/O=Byzoro Test/CN=GP1700_v3.1.0
Getting CA Private Key
4. 查看文件
root@ubuntu:/etc/nginx/keys# ls ca.crt ca.key ca.srl client.crt client.csr client.key server.crt server.csr server.key
建立nginx配置文件
cd /etc/nginx/sites-enabled
vim https.conf
內容以下:
server { listen 443; server_name localhost; ssl on; ssl_certificate /etc/nginx/keys/server.crt;#配置證書位置 ssl_certificate_key /etc/nginx/keys/server.key;#配置祕鑰位置 ssl_client_certificate /etc/nginx/keys/ca.crt;#雙向認證 ssl_verify_client on; #雙向認證 ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照這個套件配置 ssl_prefer_server_ciphers on; root html; index index.html; location / { try_files $uri $uri/ =404; } }
注意:這裏使用的是ip地址訪問,若是使用域名訪問,請修改 server_name 爲域名地址
重載配置
nginx -s reload
至此,nginx的https就可使用了,默認443端口
使用https訪問頁面
展開,點擊繼續
效果以下:
提示須要證書才行,說明雙向認證是正常的!
curl -V
curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets
請確保curl版本不能低於 7.47版本,不然會出現:
400 No required SSL certificate was sent
cd /tmp/ scp -r 192.168.0.162:/etc/nginx/keys .
curl測試
curl --cacert ca.crt --cert client.crt --key client.key --tlsv1.2 https://192.168.0.162
輸出:
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
若是有輸出 Welcome to nginx! ,說明訪問正常!
若是要代理公司認證服務,好比:192.168.0.11:30014
修改 https.conf
upstream auth { server 192.168.0.11:30014; } server { listen 443; server_name localhost; ssl on; ssl_certificate /etc/nginx/keys/server.crt; ssl_certificate_key /etc/nginx/keys/server.key; ssl_client_certificate /etc/nginx/keys/ca.crt;#雙向認證 ssl_verify_client on; #雙向認證 ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照這個套件配置 ssl_prefer_server_ciphers on; root html; index index.html index.htm; location / { #try_files $uri $uri/ =404; proxy_pass http://auth; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream http_502 http_504 error timeout invalid_header; } }
使用curl,發送post請求
curl --cacert ca.crt --cert client.crt --key client.key --tlsv1.2 -H 'Content-type':'application/json' -d '{"userid":"123","duration":"3307"' https://192.168.0.162/Auth
輸出:
{"code":"200","data":"","error":""}
輸出上面那段信息,說明訪問正常。
本文參考連接: