我试图连接到我的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)之间通信的捕获:
你知道怎么理解吗?
#编辑 2
好吧,我照你说的做了,服务器似乎没有向客户端发送CertificateRequest消息..如下所示:
但这很奇怪,因为在pg_hba中.嗯,我有:
hostssl all postgres 192.168.0.103/32 cert
你怎么认为?
#编辑3 (SOLVED!)
我换了pg_hba.配置文件包含:
hostssl all postgres 192.168.0.103/32 cert clientcert=1
改变了postgresql.要在"安全和身份验证"部分添加的配置:
ssl_ca_file = 'root.crt'
成功了!非常感谢!