簡體   English   中英

psql:致命:連接需要有效的客戶端證書

[英]psql: FATAL: connection requires a valid client certificate

我正在嘗試連接到我的 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) 之間的通信獲取的捕獲:

在此處輸入圖像描述

為什么會這樣?

似乎服務器沒有向客戶端發送 CertificateRequest 消息。如下所示:

在此處輸入圖像描述

但這很奇怪,因為在 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 是的,這很奇怪。

只是在這里為其他人添加另一個答案。 使用 org.postgresql.driver 從 jdbc 連接時,我遇到了同樣的錯誤。 問題是我為 sslkey 參數傳遞的密鑰格式是 pem 格式。 這個鏈接解釋了它。

https://jdbc.postgresql.org/documentation/head/connect.html#:~:text=keyfile.-,sslkey,-%3D%20String

Note: The key file must be in PKCS-12 or in PKCS-8 DER format. A PEM key can be converted to DER format using the openssl command:

openssl pkcs8 -topk8 -inform PEM -in postgresql.key -outform DER -out postgresql.pk8 -v1 PBE-MD5-DES

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM