SSL單向認(rèn)證和雙向認(rèn)證:
SSL單向認(rèn)證:只有一端校驗對端的證書合法性,通常都是客戶端來校驗服務(wù)器的合法性。即在一般的單向認(rèn)證中,只要求服務(wù)器端部署了ssl證書就行,客戶端可以無證書,任何用戶都可以去訪問服務(wù)端,服務(wù)端只是提供了身份認(rèn)證。
client: 無證書
server: server.crt, server.key
SSL雙向認(rèn)證:客戶端和服務(wù)端相互校驗,服務(wù)器需要校驗每個客戶端,每個客戶端也需要校驗服務(wù)器,只有服務(wù)器和用戶雙方都有證書才能正常通信,因此只能是服務(wù)端允許的客戶才能訪問服務(wù)器。
client: root.crt, postgresql.crt, postgresql.key
server: root.crt, server.crt, server.key
下面分別從服務(wù)端和客戶端說明如何配置SSL單/雙向認(rèn)證**********************************************
一、服務(wù)端
下載pg安裝包:
wget https://ftp.postgresql.org/pub/source/v11.4/postgresql-11.4.tar.gz
安裝前準(zhǔn)備:
yum install net-tools -y
yum install sysstat -y
yum install iotop libXp redhat-lsb gcc gdb –y
yum install xorg-x11-xauth -y
yum install -y vim lrzsz tree wget gcc gcc-c++ readline-devel hwloc smartmontools
yum install -y readline readline-devel openssl openssl-devel zlib zlib-devel numactl
解壓 :
tar zxvf postgresql-11.4.tar.gz
編譯:
./configure --prefix=/usr/local/postgresql --with-openssl #加 --with-openssl編譯選項
安裝:
make && make install
創(chuàng)建目錄:
mkdir /usr/local/postgresql/data
mkdir /usr/local/postgresql/log
加入系統(tǒng)環(huán)境變量:
vim /etc/profile
export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data
export PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
使配置文件生效:
source /etc/profile
增加用戶 postgres 并賦權(quán):
adduser postgres
passwd postgres
chown -R postgres:root /usr/local/postgresql/
切換到用戶 postgres:
su postgres
cd /usr/local/postgresql/data/
創(chuàng)建證書:
雖然可以使用自簽名證書進(jìn)行測試,但在實際生產(chǎn)中應(yīng)該使用由證書頒發(fā)機(jī)構(gòu)(CA)(通常是企業(yè)范圍的根CA)簽署的證書。
要創(chuàng)建可以由客戶端驗證身份的服務(wù)器證書,首先創(chuàng)建證書簽名請求(certificate signing request (CSR))和公鑰/私鑰文件:
[root@xxx data]# openssl req -new -nodes -text -out root.csr -keyout root.key -subj "/CN=root.yourdomain.com" aisainfo
Generating a 2048 bit RSA private key
............................................................................................................................+++
....+++
writing new private key to 'root.key'
-----
[root@xxx data]# ll
-rw-r--r--. 1 root root 3315 May 6 07:30 root.csr
-rw-r--r--. 1 root root 1704 May 6 07:30 root.key
然后,使用密鑰對請求進(jìn)行簽名,以創(chuàng)建根證書頒發(fā)機(jī)構(gòu)(在Linux上使用默認(rèn)的OpenSSL配置文件位置):
[root@xxx data]# openssl x509 -req -in root.csr -text -days 3650 -extfile /etc/pki/tls/openssl.cnf -extensions v3_ca -signkey root.key -out root.crt
Signature ok
subject=/CN=postgres
Certificate:
Data:
Version: 1 (0x0)
Serial Number:
9c:dc:90:eb:19:e4:e4:6d
Signature Algorithm: NULL
Issuer: CN=postgres
Validity
Not Before: May 6 03:28:00 2023 GMT
Not After : May 3 03:28:00 2033 GMT
Subject: CN=postgres
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:c4:35:2b:73:a1:d2:b5:17:3b:10:9c:2e:b6:53:
a6:90:fb:22:a4:97:5b:73:58:19:cc:9a:1f:08:0d:
2f:07:2d:df:ee:4e:18:f4:c5:12:5c:95:3b:90:d2:
ca:28:31:4d:b2:fa:e9:5f:6d:57:c0:30:ec:bf:55:
ff:9b:18:c6:68:f3:7c:2a:bc:94:1d:52:0a:d2:00:
ae:f4:79:9c:53:b1:43:b0:12:65:1f:ef:d0:14:b2:
64:7c:32:ec:97:93:a0:e1:41:b1:54:53:85:99:d8:
ac:3f:a3:f0:d0:d4:95:0d:b2:15:86:aa:6b:df:5d:
c6:70:bf:ea:d4:c0:01:51:24:ef:7b:f0:6e:8a:42:
7a:e2:ed:7d:29:a4:c5:7f:5e:2d:dc:fd:bb:3c:35:
4b:af:13:3b:22:c7:47:a5:49:92:36:f7:f2:57:78:
ef:c8:e1:cb:47:ff:6d:31:cd:97:f5:2f:02:76:07:
05:27:9a:39:da:e0:92:a7:fc:51:cb:b9:59:ea:c9:
b2:65:23:29:7e:88:62:59:c6:5a:a3:3c:91:ab:ea:
4d:1b:32:04:e1:45:6e:98:5b:a5:42:51:47:51:1c:
b3:74:58:08:5d:51:d9:1d:62:3f:37:4e:4a:ed:1e:
72:ae:d0:58:17:bb:26:57:20:95:72:ef:04:02:00:
bd:27
Exponent: 65537 (0x10001)
Signature Algorithm: NULL
Getting Private key
[root@xxx data]# ll
-rw-r--r--. 1 root root 1090 May 6 07:31 root.crt
-rw-r--r--. 1 root root 3315 May 6 07:30 root.csr
-rw-r--r--. 1 root root 1704 May 6 07:30 root.key
最后,創(chuàng)建一個由新的根證書頒發(fā)機(jī)構(gòu)簽名的服務(wù)器證書:
[root@xxx data]# openssl req -new -nodes -text -out server.csr -keyout server.key -subj "/CN=dbhost.yourdomain.com" udmuser
Generating a 2048 bit RSA private key
...............................................+++
.....+++
writing new private key to 'server.key'
-----
[root@xxx data]# ll
-rw-r--r--. 1 root root 3315 May 6 07:37 server.csr
-rw-r--r--. 1 root root 1704 May 6 07:37 server.key
[root@xxx data]# chmod og-rwx server.key
[root@xxx data]# openssl x509 -req -in server.csr -text -days 365 -CA root.crt -CAkey root.key -CAcreateserial -out server.crt
Signature ok
subject=/CN=postgres
Certificate:
Data:
Version: 1 (0x0)
Serial Number:
bb:ca:fa:52:ef:54:e9:c7
Signature Algorithm: NULL
Issuer: CN=postgres
Validity
Not Before: May 6 07:39:04 2023 GMT
Not After : May 5 07:39:04 2024 GMT
Subject: CN=postgres
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:e5:41:69:05:65:23:dc:5e:ef:b0:af:29:01:b6:
00:91:4b:a6:20:09:72:9b:d4:eb:75:3a:1d:75:ae:
76:9d:7c:6f:f8:cc:2b:03:39:d1:37:23:4e:cf:99:
69:8e:93:45:61:b7:af:e5:a2:63:61:31:05:07:ca:
d6:5a:cf:f5:53:02:60:1c:15:ed:32:7f:18:27:c8:
d0:c1:b4:f7:2d:87:19:51:34:ed:ef:04:2f:65:18:
74:0d:b9:5c:c0:e2:ff:21:16:4a:cd:b3:13:a8:2c:
bb:4f:e5:20:83:26:16:0c:48:ce:c1:d9:36:c1:fb:
db:8e:dd:9e:1b:fc:11:d6:73:a6:74:b0:27:bb:0d:
d9:80:2f:b9:ee:b3:bf:a2:3e:8c:15:12:be:35:85:
2c:5c:5a:2c:c1:c6:a4:63:ae:cd:37:49:38:aa:55:
67:32:b3:11:7e:ce:e8:e2:46:1e:57:52:a6:2a:6b:
2e:90:f8:e0:25:aa:0c:fa:8a:74:d1:4f:39:c9:0e:
68:09:89:8c:54:e6:0a:68:27:76:82:e4:eb:f0:64:
9d:e1:34:f5:19:e5:cf:dc:81:03:cb:40:d9:a2:64:
64:a1:92:b3:c4:14:3a:c3:0d:7b:b8:86:6a:c3:a2:
f5:1a:af:20:9e:e3:da:fc:58:38:6f:4d:8c:b6:1f:
ad:cb
Exponent: 65537 (0x10001)
Signature Algorithm: NULL
Getting CA Private Key
[root@xxx data]# ll
-rw-r--r--. 1 root root 973 May 6 07:39 server.crt
-rw-r--r--. 1 root root 3315 May 6 07:37 server.csr
-rw-------. 1 root root 1679 May 6 07:38 server.key
執(zhí)行初始化數(shù)據(jù)庫命令:
/usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
注意:
1)不能在 root 用戶下初始數(shù)據(jù)庫,否則會報錯;
2)初始化之后配置pg_hba.conf和postgres.conf,接著啟動數(shù)據(jù)庫可直接按配置啟動數(shù)據(jù)庫;下面描述的方法是啟動數(shù)據(jù)庫后,再修改配置并使配置生效的方法,配置參數(shù)都是一樣的。
切到root下,為 pg_ctl 創(chuàng)建軟鏈接:
su
ln -s /usr/local/postgresql/bin/pg_ctl /usr/bin/pg_ctl
ln -s /usr/local/postgresql/bin/psql /usr/bin/psql
為用戶 postgres 賦權(quán):
chown -R postgres:postgres /usr/local/postgresql/data
chmod -R 0700 /usr/local/postgresql/data
切換到用戶 postgres啟動服務(wù):
su postgres
pg_ctl start -l /usr/local/postgresql/log/pg_server.log
到指定路徑查看日志
日志路徑: /usr/local/postgresql/log/pg_server.log ----> /usr/local/postgresql/data/pg_log
修改postgres.conf,配置SSL參數(shù):
vim postgres.conf
ssl = on #支持SSL,默認(rèn)off(關(guān)閉)。該參數(shù)只能在Server啟動時設(shè)置。SSL通信只能通過TCP/IP連接進(jìn)行。
ssl_cert_file = 'server.crt' #指定包含SSL服務(wù)器證書的文件的名稱。
ssl_key_file = 'server.key' #指定包含SSL服務(wù)器私鑰的文件的名稱。
注意:
單項認(rèn)證采用以上配置即可,雙向認(rèn)證使用以下配置【雙向認(rèn)證必須配置ssl_ca_file項】:
ssl = on #支持SSL,默認(rèn)off(關(guān)閉)。該參數(shù)只能在Server啟動時設(shè)置。SSL通信只能通過TCP/IP連接進(jìn)行。
ssl_ca_file = 'root.crt' #指定根證書,SSL單項認(rèn)證時也可以不配置、SSL雙向認(rèn)證必須配置
ssl_cert_file = 'server.crt' #指定包含SSL服務(wù)器證書的文件的名稱。
ssl_key_file = 'server.key' #指定包含SSL服務(wù)器私鑰的文件的名稱。
修改pg_hba.conf
如果強(qiáng)制SSL連接(僅允許SSL連接)、不允許普通連接,則修改pg_hba.conf,配置SSL連接認(rèn)證規(guī)則:
vim pg_hba.conf
單向認(rèn)證:
hostssl all all 0.0.0.0/0 md5
雙向認(rèn)證(以下兩種配置方式都可以):
hostssl all all 0.0.0.0/0 md5 clientcert=1
hostssl all all 0.0.0.0/0 cert
注意:
不同版本的postgresql配置雙向認(rèn)證的參數(shù)略有不同,我當(dāng)前安裝的v11.4版本就可以用clientcert=1或者cert,具體可以根據(jù)自己安裝的版本查看官方說明,v11.4版本的配置說明如下:
【參考資料】:PostgreSQL: Documentation: 11: 18.9. Secure TCP/IP Connections with SSL
18.9.3. Using Client Certificates
To require the client to supply a trusted certificate, place certificates of the root certificate authorities (CAs) you trust in a file in the data directory, set the parameter ssl_ca_file in postgresql.conf to the new file name, and add the authentication option clientcert=1 to the appropriate hostssl line(s) in pg_hba.conf. A certificate will then be requested from the client during SSL connection startup. (See Section 34.18 for a description of how to set up certificates on the client.) The server will verify that the client’s certificate is signed by one of the trusted certificate authorities.
The clientcert authentication option is available for all authentication methods, but only in pg_hba.conf lines specified as hostssl. When clientcert is not specified or is set to 0, the server will still verify any presented client certificates against its CA file, if one is configured — but it will not insist that a client certificate be presented.
If you are setting up client certificates, you may wish to use the cert authentication method, so that the certificates control user authentication as well as providing connection security. See Section 20.12 for details. (It is not necessary to specify clientcert=1 explicitly when using the cert authentication method.)
說明:
pg_hba.conf中的Client連接認(rèn)證規(guī)則配置的幾種類型:local、host、hostssl、hostnossl
local: 此記錄匹配通過 Unix 域套接字進(jìn)行的聯(lián)接企圖,沒有這種類型的記錄,就不允許 Unix 域套接字的聯(lián)接。
host: 此記錄匹配使用TCP/IP進(jìn)行的連接嘗試,他既匹配通過ssl方式的連接,也匹配通過非ssl方式的連接,會優(yōu)先使用ssl認(rèn)證。
hostssl: 此記錄匹配使用TCP/IP進(jìn)行的連接嘗試,但僅在使用SSL加密進(jìn)行連接時才匹配。hostssl表示強(qiáng)制使用ssl。
hostnossl:此記錄類型具有與hostssl相反的行為:它僅匹配不使用SSL的TCP/IP上的連接嘗試。hostnossl表示前置不使用ssl。
登陸PostgreSQL數(shù)據(jù)庫,打開ssl開關(guān):
psql -U postgres -d postgres
postgres=# alter system set ssl=on;
ALTER SYSTEM
調(diào)用pg_reload_conf()以確保配置文件被加載:
postgres=# select pg_reload_conf();
重新加載配置遇到的幾個問題:
1、未刪除server.key的密碼,報錯:“private key file ““server.key”” cannot be reloaded because it requires a passphrase”,"
解決方法:刪除私鑰中的密碼
openssl rsa -in server.key -out server.key
2、server.key未修改訪問權(quán)限,報錯:“private key file “server.key” has group or world access”
解決方法:修改文件權(quán)限
chmod 600 xxxfile
3、未正確配置pg_hba.conf,遠(yuǎn)程連接時報錯:
“xxxuser”,“xxxdb”,25257,“10.11.58.83:44764”,644a0fb6.62a9,1,“authentication”,2023-04-27 06:01:26 UTC,3/2216,0,FATAL,28000,“no pg_hba.conf entry for host ““10.11.58.83"”, user ““xxxuser””, database ““xxxdb””, SSL off”,”"
解決方法:檢查pg_hba.conf文件配置
重啟數(shù)據(jù)庫,ssl生效
[postgres@localhost~]$ psql -Upostgres postgres -h localhost
Password forusersa:
psql (11.1)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" forhelp.
查看ssl開關(guān):
postgres=# show ssl;
ssl
-----
on
(1 row)
檢查使用SSL/TLS的會話連接
postgres=# select pg_ssl.pid, pg_ssl.ssl, pg_ssl.version,pg_sa.backend_type, pg_sa.usename, pg_sa.client_addr from pg_stat_ssl pg_ssl join pg_stat_activity pg_sa on pg_ssl.pid = pg_sa.pid;
pid | ssl | version | backend_type | usename | client_addr
-------+-----+---------+------------------------------+----------+-------------
16629 | f | | autovacuum launcher | |
16748 | f | | logical replication launcher | postgres |
25923 | t | TLSv1.2 | client backend | postgres | ::1
16627 | f | | background writer | |
16626 | f | | checkpointer | |
16628 | f | | walwriter | |
(6 rows)
總結(jié):
服務(wù)端按需求配置postgres.conf、pg_hba.conf兩個配置文件,并放入相關(guān)證書文件(root.crt, server.crt, server.key)即可打開SSL單/雙向認(rèn)證連接;僅做單向認(rèn)證的話到這里就完成了,無需對客戶端進(jìn)行配置;下面描述的是客戶端雙向認(rèn)證配置流程。
二、客戶端:
1)生成客戶端SSL配置(在服務(wù)端進(jìn)行):
服務(wù)端為客戶端生成客戶后端證書,提供給客戶端連接時使用。
客戶端開啟SSL配置雙向認(rèn)證連接服務(wù)器,需要三個文件:
root.key(根證書)
postgresql.crt(客戶端證書)
postgresql.key(客戶端私鑰)
在服務(wù)器端操作、生成客戶端需要的文件,即客戶后端私鑰(postgresql.key)和客戶端證書(postgresql.crt):
創(chuàng)建一個由根證書頒發(fā)機(jī)構(gòu)簽名的客戶端證書:
[root@xxx data]# openssl req -new -nodes -text -out postgresql.csr -keyout postgresql.key -subj "/CN=dbhost.yourdomain.com" udmuser
Generating a 2048 bit RSA private key
...........+++
............................................................+++
writing new private key to 'postgresql.key'
-----
[root@xxx data]# ll
-rw-r--r--. 1 root root 3315 May 6 08:26 postgresql.csr
-rw-r--r--. 1 root root 1704 May 6 08:26 postgresql.key
[root@xxx data]# chmod og-rwx postgresql.key
[root@xxx data]# openssl x509 -req -in postgresql.csr -text -days 365 -CA root.crt -CAkey root.key -CAcreateserial -out postgresql.crt
Signature ok
subject=/CN=postgres
Certificate:
Data:
Version: 1 (0x0)
Serial Number:
c1:32:b7:ec:c8:a1:31:4f
Signature Algorithm: NULL
Issuer: CN=postgres
Validity
Not Before: May 6 08:28:15 2023 GMT
Not After : May 5 08:28:15 2024 GMT
Subject: CN=postgres
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:e0:8c:eb:29:3c:df:17:ca:8c:e4:ab:37:8f:f9:
02:ec:3e:c3:0f:0d:86:2c:fb:21:aa:81:39:31:fd:
45:4e:3e:a4:99:f8:f4:49:cc:69:d2:1a:31:87:f1:
e8:17:46:86:0b:d5:06:46:9a:85:1a:d5:7a:1a:d4:
27:b7:09:6d:28:0e:9b:ff:c4:96:90:5e:02:b2:b3:
be:49:d0:6b:f3:aa:25:5d:1f:22:b3:a1:e2:0d:29:
6b:e6:41:09:85:a6:66:ee:2f:6b:a7:25:8a:25:f3:
92:71:6b:fc:d9:34:68:eb:70:0c:f0:d8:b2:4f:82:
f3:02:fa:0f:02:c8:95:5e:32:09:d0:01:ae:39:8d:
13:c4:9a:e1:6b:ab:3b:01:96:c1:d8:95:0c:70:22:
2b:7d:a8:e9:0d:5f:18:5f:5c:6e:84:80:0a:51:77:
2e:87:3d:62:6e:0b:30:39:a2:76:19:ff:95:da:ad:
fb:61:44:e7:15:f3:c9:c6:fa:b2:26:db:b9:79:08:
ad:75:bf:26:15:54:62:63:ee:ed:0e:67:6d:05:df:
3c:03:4a:fc:d8:6c:f8:b3:b4:fb:ab:66:c0:f8:e6:
4d:3f:0b:77:9d:be:4f:a8:5c:c0:4f:1f:76:d4:cf:
4b:33:e8:40:09:96:8c:02:dc:9c:9e:d3:37:59:d4:
65:d3
Exponent: 65537 (0x10001)
Signature Algorithm: NULL
Getting CA Private Key
[root@xxx data]# ll
-rw-r--r--. 1 root root 973 May 6 08:28 postgresql.crt
-rw-r--r--. 1 root root 3315 May 6 08:26 postgresql.csr
-rw-------. 1 root root 1679 May 6 08:27 postgresql.key
2) 拷貝客戶端SSL配置文件到客戶端機(jī)器
root.key #根證書
postgresql.crt #客戶端證書
postgresql.key #客戶端私鑰
將 1)中生成的客戶端證書從服務(wù)端復(fù)制到客戶端以下路徑:
(a)~/.postgresql/目錄下(沒有找到的話在root下mkdir一個.postgresql文件夾再放進(jìn)去證書),這是默認(rèn)路徑
(b) 將證書放到自定義路徑下,并通過設(shè)置環(huán)境變量指定證書路徑:
export PGSSLROOTCERT=/home/pgcert/root.crt
export PGSSLCERT=/home/pgcert/postgresql.crt
export PGSSLKEY=/home/pgcert/postgresql.key
3)配置/etc/odbc.ini文件:
PostgreSQL 的幾種SSL連接模式:
disable: 只嘗試非SSL連接。
allow: 首先嘗試非SSL連接,若失敗再嘗試SSL連接。
prefer: 默認(rèn)模式,首先嘗試SSL連接,若失敗再嘗試非SSL連接。
require: 只嘗試SSL連接,若有根證書存在,等同于verify-ca。
verify-ca: 只嘗試SSL連接,并用根證書驗證服務(wù)器證書是不是根CA簽發(fā)的。
verify-full:只嘗試SSL連接,并用根證書驗證服務(wù)器證書是不是根CA簽發(fā)的,且主題必須匹配連接域名或IP地址
進(jìn)行SSL連接的話僅考慮配置后三種連接模式,其區(qū)別在于:
Require:只對數(shù)據(jù)鏈路加密,并不驗證數(shù)據(jù)庫的真實性。
Verify-CA:加密數(shù)據(jù)鏈路,同時驗證數(shù)據(jù)庫的真實性。
Verify-Full:加密數(shù)據(jù)鏈路,驗證數(shù)據(jù)庫的真實性,同時比對證書內(nèi)的CN或DNS與連接時配置的數(shù)據(jù)庫連接地址是否一致。
并且:
verify-ca和verify-full之間的區(qū)別取決于根CA的策略。如果使用了一個公共CA,verify-ca允許連接到那些可能已經(jīng)被其他人注冊到該CA的服務(wù)器。在這種情況下,總是應(yīng)該使用verify-full。如果使用了一個本地CA或者甚至是一個自簽名的證書,使用verify-ca常常就可以提供足夠的保護(hù)。
由于我的項目使用的是自簽名的證書,因此我配置Sslmode = require,無證書時可進(jìn)行單向認(rèn)證連接;放入相關(guān)證書(等同于verify-ca)即可滿足雙向認(rèn)證連接的需求。
增加下面的配置:
vim /etc/odbc.ini文章來源:http://www.zghlxwxcb.cn/news/detail-459995.html
Sslmode = require
三、測試:
遠(yuǎn)程連接數(shù)據(jù)源并進(jìn)行數(shù)據(jù)庫操作:
[root@xxx /root]#isql xxxxx -v
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select * from xxxTable;
操作數(shù)據(jù)庫和抓包:
1、遠(yuǎn)程連接
2、操作數(shù)據(jù)庫
3、斷開遠(yuǎn)程連接
單向認(rèn)證抓包:
雙向認(rèn)證抓包:文章來源地址http://www.zghlxwxcb.cn/news/detail-459995.html
到了這里,關(guān)于PostgreSQL安裝和開啟SSL加密連接【配置單/雙向認(rèn)證】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!