postgresql - psql:致命:连接需要有效的客户端证书

标签 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

客户端证书:

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

将必要的文件(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'

然后我得到:

psql: FATAL:  connection requires a valid client certificate

我执行的客户端证书签名过程有误吗?

我试过:

openssl verify -CAfile root.crt -purpose sslclient client.crt

我得到:

client.crt: OK

使用 Wireshark,这是我获取的客户端 (192.168.0.103) 和服务器 (192.168.0.100) 之间通信的捕获:

enter image description here

为什么会这样?

服务器似乎没有向客户端发送 CertificateRequest 消息。如下所示:

enter image description here

但这很奇怪,因为在 pg_hba.conf 中,我有:

hostssl all             postgres        192.168.0.103/32        cert

最佳答案

在这种情况下,我倾向于拔出 Wireshark 并监听 SSL 协商以确保客户端证书确实由客户端提供。

我建议也使用 openssl 来验证客户端->root 签名链接。

openssl verify -CAfile root.crt -purpose sslclient client.crt

编辑:即使选择了 cert 身份验证,也有必要指定 clientcert=1。是的,这很奇怪。

关于postgresql - psql:致命:连接需要有效的客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18497299/

相关文章:

postgresql - Postgres : difference between two timestamps (hours:minutes:seconds)

ssl - 我可以只购买一个证书并使用它来签署/保护许多资源吗?

java - ftp4j:client.login 返回无法识别的 SSL 消息,明文连接?

linux - 使用 OpenSSL 创建证书颁发机构

postgresql - '绑定(bind)消息提供 1 个参数,但准备好的语句 ""在 Node 中需要 5 个'

postgresql - 带有 INSTEAD OF 触发器的 Postgres INSERT ON CONFLICT 行为

postgresql - Postgres COPY TO 不适用于预期的目标目录

linux - 在 nginx 中处理默认服务器和 ip 转发的最佳实践

php - 需要帮助理解 php 签名验证

linux - 使用 bash shell 脚本加密文件