pla*_*pus 17 postgresql ssl openssl psql
我正在尝试连接到我的PostgreSQL服务器,但psql抱怨我没有有效的客户端证书.以下是我创建证书的方法:
自签名服务器证书:
openssl req -new -text -nodes -keyout server.key -out server.csr -subj '/C=US/ST=California/L=Fremont/O=Example/OU=CoreDev/CN=192.168.0.100' # CN is the server's IP address
openssl req -x509 -text -in server.csr -key server.key -out server.crt
cp server.crt root.crt
rm server.csr
chmod og-rwx server.key
Run Code Online (Sandbox Code Playgroud)
客户证书:
openssl req -new -nodes -keyout client.key -out client.csr -subj '/C=US/ST=California/L=Fremont/O=Example/OU=CoreDev/CN=postgres' # postgres is the database user name
openssl x509 -req -CAcreateserial -in client.csr -CA root.crt -CAkey server.key -out client.crt
rm client.csr
Run Code Online (Sandbox Code Playgroud)
将必要的文件(client.crt,client.key,root.crt)复制到客户端计算机上并更改权限(即chmod og-rwx client.key)后,我执行以下操作:
psql 'host=192.168.0.100 port=5432 dbname=postgres user=postgres sslmode=verify-full sslcert=client.crt sslkey=client.key sslrootcert=root.crt'
Run Code Online (Sandbox Code Playgroud)
然后我得到:
psql: FATAL: connection requires a valid client certificate
Run Code Online (Sandbox Code Playgroud)
我在做客户端证书签名过程错了吗?
谢谢,
我试过了:
openssl verify -CAfile root.crt -purpose sslclient client.crt
Run Code Online (Sandbox Code Playgroud)
我得到:
client.crt: OK
Run Code Online (Sandbox Code Playgroud)
使用Wireshark,这是我在客户端(192.168.0.103)和服务器(192.168.0.100)之间进行通信的捕获:
你知道怎么理解这个吗?
好的,我做了你说的,似乎服务器没有将CertificateRequest消息发送给客户端..如下所示:
但这很奇怪,因为在pg_hba.conf中,我有:
hostssl all postgres 192.168.0.103/32 cert
Run Code Online (Sandbox Code Playgroud)
你怎么看?
我更改了pg_hba.conf以包含:
hostssl all postgres 192.168.0.103/32 cert clientcert=1
Run Code Online (Sandbox Code Playgroud)
并更改了postgresql.conf以添加到"安全和身份验证"部分:
ssl_ca_file = 'root.crt'
Run Code Online (Sandbox Code Playgroud)
它工作了!非常感谢!
在这种情况下,我倾向于拔出Wireshark并窥探SSL协商,以确保客户端证书真正由客户端提供.
我建议使用openssl来验证客户端 - > root签名链接.
openssl verify -CAfile root.crt -purpose sslclient client.crt
Run Code Online (Sandbox Code Playgroud)
编辑:clientcert=1
即使cert
选择了身份验证,也必须指定.是的,这很奇怪.