續(xù)接下面兩篇文章:
《openGauss與PostgreSQL對比測試SSL之自簽名私有證書測試》自簽名私有證書測試
《openGauss與PostgreSQL對比測試SSL之自簽名CA證書單向認證測試》自簽名CA證書單向認證測試。
本文測試自簽名CA證書的雙向認證: 客戶端驗證服務(wù)器證書的有效性,同時服務(wù)器端也要驗證客戶端證書的有效性,只有認證成功,連接才能建立。
服務(wù)端證書的客戶端認證模式
1.客戶端SSLMODE設(shè)置為verify-ca僅校驗數(shù)據(jù)庫證書真?zhèn)巍?br /> 2.客戶端SSLMODE設(shè)置為verify-full校驗數(shù)據(jù)庫證書真?zhèn)渭巴ㄓ妹鸆N匹配數(shù)據(jù)庫連接的hostname。
客戶端證書的服務(wù)器認證模式
1.數(shù)據(jù)庫認證文件pg_hba.conf配置認證選項clientcert=verify-ca僅驗證客戶端證書真?zhèn)危J證方法可選。
2.數(shù)據(jù)庫認證文件pg_hba.conf配置認證選項clientcert=verify-full驗證客戶端證書真?zhèn)渭癈N匹配數(shù)據(jù)庫連接用戶名或映射匹配,認證方法可選。
3.數(shù)據(jù)庫認證文件pg_hba.conf配置認證方法cert,免密驗證客戶端證書真?zhèn)渭癈N匹配數(shù)據(jù)庫連接用戶名或映射匹配
自簽名CA證書雙向認證測試
1.創(chuàng)建CA證書
用于給數(shù)據(jù)庫服務(wù)器證書簽名的CA證書:ca_server.crt
$ openssl req -new -x509 -days 365 -nodes \
-config openssl.cnf \
-out ca_server.crt -keyout ca_server.key -subj "/CN=FooServerCA"
用于給客戶端證書簽名的CA證書:ca_client.crt
$ openssl req -new -x509 -days 365 -nodes \
-config openssl.cnf \
-out ca_client.crt -keyout ca_client.key -subj "/CN=FooClientCA"
2.生成數(shù)據(jù)庫服務(wù)器證書請求文件并簽名
$ openssl req -new -nodes -text \
-config openssl.cnf \
-out server.csr \
-keyout server.key \
-subj "/CN=192.168.137.5"
將證書請求文件(包含用戶信息)和證書簽名分開操作,證書請求文件可重用,因為后面可能需要重新生成證書。
使用ca_server.crt對證書請求文件簽名
$ openssl x509 -req -in server.csr -text -days 5 \
-CA ca_server.crt \
-CAkey ca_server.key \
-CAcreateserial \
-out server.crt
這里設(shè)置有效期為5天,可以觀察在服務(wù)器證書有效期小于7天的時候,連接登錄后會在日志中產(chǎn)生告警提醒。
3.生成客戶端證書請求文件并簽名
$ openssl req -new -nodes -text \
-config openssl.cnf \
-out client.csr \
-keyout client.key \
-subj "/CN=dbuser1"
將證書請求文件(包含用戶信息)和證書簽名分開操作,證書請求文件可重用,因為后面可能需要重新生成證書。
使用ca_client.crt對證書請求文件簽名
$ openssl x509 -req -in client.csr -text -days 30 \
-CA ca_client.crt \
-CAkey ca_client.key \
-CAcreateserial \
-out client.crt
4.傳輸數(shù)據(jù)庫服務(wù)器證書及未加密的私鑰文件至數(shù)據(jù)庫服務(wù)器
修改文件權(quán)限以符合安全設(shè)置
$ chmod 0600 ca_client.crt server.crt server.key
傳輸文件到數(shù)據(jù)庫服務(wù)器PGDATA目錄
$ cp ca_client.crt server.crt server.key $PGDATA
注意:如果PostgreSQL使用-g, --allow-group-access
開啟了組訪問權(quán)限,則需要拷貝文件到PGDATA目錄之外以符合安全設(shè)置。
5.數(shù)據(jù)庫SSL參數(shù)配置
postgreql.conf文件配置參數(shù)
ssl=on
ssl_cert_file= 'server.crt'
ssl_key_file= 'server.key'
ssl_ca_file='ca_client.crt'
然后重啟數(shù)據(jù)庫服務(wù)。
6.配置數(shù)據(jù)庫客戶端
客戶端使用linux下psql,證書文件的默認路徑為$HOME/.postgresql/
chmod 0600 client.key client.crt
cp client.crt client.key ~/.postgresql/
cat ca_server.crt > ~/.postgresql/root.crt
chmod 0600 ~/.postgresql/root.crt
測試一
pg_hba.conf文件配置hostssl條目。
hostssl all all 0.0.0.0/0 md5
測試驗證數(shù)據(jù)庫服務(wù)器證書
openGauss數(shù)據(jù)庫
gsql "sslmode=verify-ca" -p6432 -h 192.168.137.5 -Upostgres
Password for user postgres:
gsql ((GaussDB Kernel V500R001C20 build ) compiled at 2021-03-09 18:30:51 commit 0 last mr )
SSL connection (cipher: DHE-RSA-AES128-GCM-SHA256, bits: 128)
Type "help" for help.
postgres=>
PostgreSQL數(shù)據(jù)庫
psql "sslmode=verify-ca" -h192.168.137.11
Password for user postgres:
psql (12.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=# \q
使用sslmode=verify-ca僅驗證服務(wù)器證書真?zhèn)危项A(yù)期。
測試二
測試數(shù)據(jù)庫服務(wù)器證書設(shè)置的通用名CN是否匹配客戶端連接的hostname
openGauss數(shù)據(jù)庫
gsql "sslmode=verify-full" -p6432 -h nodex -Upostgres
gsql: server common name "192.168.137.5" does not match host name "nodex"
PostgreSQL數(shù)據(jù)庫
psql "sslmode=verify-full" -hnode11
psql: error: server certificate for "192.168.137.11" does not match host name "node11"
分別使用ip地址及主機名測試,與通用名CN匹配的ip地址可成功登錄,使用主機名連接報錯,報錯提示如上,符合預(yù)期。
測試三
測試驗證客戶端證書
pg_hba.conf文件配置hostssl條目。
hostssl all all 0.0.0.0/0 md5 clientcert=verify-ca
此時數(shù)據(jù)庫連接使用ip地址或者hostname均可連接
openGauss數(shù)據(jù)庫
gsql "sslcert=/home/omm/.postgresql/client.crt sslkey=/home/omm/.postgresql/client.key"
-h192.168.137.5 -p6432 -Upostgres
Password for user postgres:
Warning: The client certificate will expire in 29 days.
gsql ((GaussDB Kernel V500R001C20 build ) compiled at 2021-03-09 18:30:51 commit 0 last mr )
SSL connection (cipher: DHE-RSA-AES128-GCM-SHA256, bits: 128)
Type "help" for help.
postgres=>
使用hostname也可連接
gsql "sslcert=/home/omm/.postgresql/client.crt sslkey=/home/omm/.postgresql/client.key"
-hnodex -p6432 -Upostgres
如果使用不正確的客戶端證書,比如手工修改client.crt內(nèi)容,測試會失敗
PostgreSQL數(shù)據(jù)庫
psql "sslcert=/home/postgres/.postgresql/client.crt sslkey=/home/postgres/.postgresql/client.key" -h192.168.137.11
Password for user postgres:
psql (12.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=# \q
使用hostname也可連接
psql "sslcert=/home/postgres/.postgresql/client.crt sslkey=/home/postgres/.postgresql/client.key" -hnode11
Password for user postgres:
psql (12.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=# \q
如果使用不正確的客戶端證書,比如手工修改client.crt內(nèi)容,測試會失敗
psql "sslcert=/home/postgres/.postgresql/client.crt sslkey=/home/postgres/.postgresql/client.key" -h192.168.137.11
psql: error: SSL error: tlsv1 alert unknown ca
FATAL: no pg_hba.conf entry for host "192.168.137.11", user "postgres", database "postgres", SSL off
分別使用ip地址及主機名測試clientcert=verify-ca選項,測試結(jié)果符合預(yù)期。
測試四
測試驗證客戶端證書及CN匹配用戶或用戶映射
pg_hba.conf文件配置hostssl條目。
hostssl all all 0.0.0.0/0 md5 clientcert=verify-full
此時數(shù)據(jù)庫連接用戶必須配置CN中配置的名稱dbuser1
openGauss數(shù)據(jù)庫
gsql "dbname=postgres sslcert=/home/omm/.postgresql/client.crt sslkey=/home/omm/.postgresql/client.key" -h192.168.137.5 -p6432 -Udbuser1
上面使用dbuser1可以登錄成功,如果使用其他用戶也能登錄成功。
PostgreSQL數(shù)據(jù)庫
psql "dbname=postgres sslcert=/home/postgres/.postgresql/client.crt sslkey=/home/postgres/.postgresql/client.key" -h192.168.137.11 -p6000 -Udbuser1
上面使用dbuser1可以登錄成功,如果使用其他用戶比如postgres則會出現(xiàn)下面的錯誤提示。
psql: error: FATAL: password authentication failed for user "postgres"
FATAL: no pg_hba.conf entry for host "192.168.137.11", user "postgres", database "postgres", SSL off
測試五
測試cert免密認證:驗證客戶端證書及CN匹配用戶或用戶映射
pg_hba.conf文件配置hostssl條目。
hostssl all all 0.0.0.0/0 cert
此時數(shù)據(jù)庫連接用戶必須配置CN中配置的名稱dbuser1,同時不需要輸入密碼。
openGauss數(shù)據(jù)庫
gsql "dbname=postgres sslcert=/home/omm/.postgresql/client.crt sslkey=/home/omm/.postgresql/client.key" -h192.168.137.5 -p6432 -Udbuser1
Warning: The client certificate will expire in 29 days.
gsql ((GaussDB Kernel V500R001C20 build ) compiled at 2021-03-09 18:30:51 commit 0 last mr )
SSL connection (cipher: DHE-RSA-AES128-GCM-SHA256, bits: 128)
Type "help" for help.
postgres=>
上面使用dbuser1用戶可直接登錄,不需要輸入密碼,如果使用其他用戶比如postgres則會出現(xiàn)下面的錯誤提示。
Warning: The client certificate will expire in 29 days.
gsql: FATAL: certificate authentication failed for user "postgres"
FATAL: no pg_hba.conf entry for host "192.168.137.5", user "postgres", database "postgres", SSL off
PostgreSQL數(shù)據(jù)庫
psql "dbname=postgres sslcert=/home/postgres/.postgresql/client.crt sslkey=/home/postgres/.postgresql/client.key"
-h192.168.137.11 -Udbuser1
psql (12.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=> \q
上面使用dbuser1用戶可直接登錄,不需要輸入密碼,如果使用其他用戶比如postgres則會出現(xiàn)下面的錯誤提示。
psql: error: FATAL: certificate authentication failed for user "postgres"
FATAL: no pg_hba.conf entry for host "192.168.137.11", user "postgres", database "postgres", SSL off
總結(jié)
1.sslmode連接參數(shù)設(shè)置為verify-ca僅校驗數(shù)據(jù)庫證書真?zhèn)危O(shè)置為verify-full校驗數(shù)據(jù)庫證書真?zhèn)渭巴ㄓ妹鸆N匹配數(shù)據(jù)庫連接的hostname。
2.clientcert認證選項設(shè)置為verify-ca僅校驗客戶端證書真?zhèn)危O(shè)置為verify-full校驗客戶端證書真?zhèn)渭巴ㄓ妹鸆N匹配數(shù)據(jù)庫用戶或用戶映射。
3.使用clientcert認證選項時,連接類型可以設(shè)置為hostssl,但host類型也同時支持hostssl及hostnossl。
4.使用cert認證方法只能設(shè)置連接類型為hostssl。
5.客戶端證書clientcert=verify-full認證方式,openGauss與PostgreSQL有差異,參見測試四。




