Nginx配置HTTPS客户端认证
最近折腾自己个人网站,有一个私人模块,只能自己访问,使用登录授权方式虽然也能达到目的,但每次都要登录,且密码也不一定安全。想起学HTTPS的时,有一个客户端证书的概念,应该可以满足更高的安全性要求,本文记录折腾过程。
原理
TLS握手过程,第二步Server Hello
中可以要求客户端提供证书,接着客户端需要将服务器颁发的证书发送给服务器。服务端验证客户端证书,有效则使用证书公钥加密信息发送给客户端,客户端通过私钥解密所需信息,计算出Pre-master
,用服务端证书公钥加密,发送给服务器。
如果客户端证书无效,或者过期等,服务端在收到后会断开连接。
准备证书
主要准备三份证书:CA证书、服务器证书、客户端证书。如果只是单个用户需要,可以不需要CA证书,具体如何做,留给大家思考。
新建临时工作区
cd /root
mkdir -p tmp/ca/conf
cd tmp
CA证书
CA证书作为根证书,可以签发众多客户端证书,在TSL握手过程中用于验证客户端证书是否有效。
pwd: /root/tmp/ca
第一步,生成CA证书密钥
openssl genrsa 2048 > ca.key
第二步,生成CA证书
openssl req -new -x509 -key ca.key -out ca.crt -days 3650
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 wi