nginx SSL双向认证部署。
以下架构拓扑图
双向认证原理:
-
- 证书是否过期
- 发型服务器证书的CA是否可靠
- 返回的公钥是否能正确解开返回证书中的数字签名
- 服务器证书上的域名是否和服务器的实际域名相匹配
验证通过后,将继续进行通信,否则,终止通信
4、服务端要求客户端发送客户端的证书,客户端会将自己的证书发送至服务端
5、验证客户端的证书,通过验证后,会获得客户端的公钥
6、客户端向服务端发送自己所能支持的对称加密方案,供服务器端进行选择
7、服务器端在客户端提供的加密方案中选择加密程度最高的加密方式
8、将加密方案通过使用之前获取到的公钥进行加密,返回给客户端
9、客户端收到服务端返回的加密方案密文后,使用自己的私钥进行解密,获取具体加密方式,而后,产生该加密方式的随机码,用作加密过程中的密钥,使用之前从服务端证书中获取到的公钥进行加密后,发送给服务端
10、服务端收到客户端发送的消息后,使用自己的私钥进行解密,获取对称加密的密钥,在接下来的会话中,服务器和客户端将会使用该密码进行对称加密,保证通信过程中信息的安全。
请确保server已经安装了Nginx 在server端操作
1. 生成一个 CA 私钥: ca.key
mkdir /root/ssl #方便存放生成的ssl cd /root/ssl 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]:hz
Locality Name (eg, city) []:hz
Organization Name (eg, company) [Internet Widgits Pty Ltd]:dgx
Organizational Unit Name (eg, section) []:dgx
Common Name (e.g. server FQDN or YOUR name) []:root
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]:hz
Locality Name (eg, city) []:hz
Organization Name (eg, company) [Internet Widgits Pty Ltd]:dgx
Organizational Unit Name (eg, section) []:dgx
Common Name (e.g. server FQDN or YOUR name) []:192.168.1.1
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]:hz
Locality Name (eg, city) []:hz
Organization Name (eg, company) [Internet Widgits Pty Ltd]:dgx
Organizational Unit Name (eg, section) []:dgx
Common Name (e.g. server FQDN or YOUR name) []:test
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:/root/nginx# ls ca.crt ca.key ca.srl client.crt client.csr client.key server.crt server.csr server.key
如何配置nginx
docker run -d --name nginx nginx #临时启动nginx 用于拷贝配置文件 docker cp nginx:/etc/nginx /root/nginx #配置文件拷贝到本地 docker rm -f nginx #删除刚才启动的nginx docker run -dit --name nginx --restart alway -p 80:80 -p 443:443 -v /root/nginx:/etc/nginx nginx #正式启动nginx 端口自行修改
#服务端
修改nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server_tokens off; #隐藏nginx版本号 server { listen 443 ssl; server_name localhost; ssl_certificate /etc/nginx/ssl/server.crt; #配置证书位置 请保证以下三项在容器里有ssl文件 能访问到此自签证书 ssl_certificate_key /etc/nginx/ssl/server.key; #配置秘钥位置 ssl_client_certificate /etc/nginx/ssl/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; } } }
配置完重启nginx
docker restart nginx
验证
curl https://192.168.1.1:443 -k
<html>
<head><title>400 No required SSL certificate was sent</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>No required SSL certificate was sent</center>
<hr><center>nginx</center>
</body>
</html>
#以上证明双向认证成功 ,需要公私钥才可访问
curl --cert ./client.crt --key ./client.key https://192.168.1.1:443 -k
#指定私钥 可绝对路径 相对路径需要加“./” 不然会报错400 No required SSL certificate was sent
客户端搭建NG 跟上面步骤一样
客户端nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } stream { server { listen 80; proxy_pass 192.168.1.1:443; proxy_ssl on; proxy_ssl_certificate /etc/nginx/ssl/client.crt; #指定密钥去访问服务端 proxy_ssl_certificate_key /etc/nginx/ssl/client.key; } }