PostgerSQL 安全管理 自签名CA证书

1 背景知识

自签名CA证书,不仅仅可以加密通讯连接,还可以认证服务器和客户端身份。

2 生成CA 私钥和自签名证书

2.1 生成CA证书

mkdir -p ~/.ssl && cd ~/.ssl && rm -rf ./*
openssl req -new -x509 -days 365 -nodes \
-config /etc/pki/tls/openssl.cnf  \
-out root.crt -keyout root.key -subj "/CN=FooServerCA"

3 生成服务器私钥、证书请求文件、服务器证书

3.1 生成服务器私钥、证书请求文件

openssl req -new -nodes -text \
-config /etc/pki/tls/openssl.cnf \
-out server.csr \
-keyout server.key \
-subj "/CN=192.168.10.170"

Warning

3.2 签署服务器证书

openssl x509 -req -in server.csr -text -days 365 \
-CA root.crt \
-CAkey root.key \
-CAcreateserial \
-out server.crt

4 为KES客户端生成私钥、证书请求文件、客户端证书

4.1 生成客户端证书请求文件和私钥

openssl req -new -nodes -text \
-config /etc/pki/tls/openssl.cnf \
-out postgresql.csr \
-keyout postgresql.key \
-subj "/CN=postgres"

4.2 使用根证书和根证书私钥来签署客户端证书

openssl x509 -req -in postgresql.csr -text -days 365 \
-CA root.crt \
-CAkey root.key \
-CAcreateserial \
-out postgresql.crt

4.3 自签名CA证书文件汇总和说明

1、前三步会生成下面的文件。
2、server.crtserver.key应该存储在服务器上,
3、root.crt应该存储在客户端上,以便客户端可以验证服务器的证书是否可信。
4、root.key应该离线存储以用于创建将来的证书。

-rw-rw-r--. 1 postgres postgres 2715 Sep  5 13:28 postgresql.crt
-rw-rw-r--. 1 postgres postgres 3321 Sep  5 13:28 postgresql.csr
-rw-------. 1 postgres postgres 1704 Sep  5 13:28 postgresql.key
-rw-rw-r--. 1 postgres postgres 1119 Sep  5 13:27 root.crt
-rw-------. 1 postgres postgres 1704 Sep  5 13:27 root.key
-rw-rw-r--. 1 postgres postgres   41 Sep  5 13:28 root.srl
-rw-rw-r--. 1 postgres postgres 2735 Sep  5 13:28 server.crt
-rw-rw-r--. 1 postgres postgres 3335 Sep  5 13:27 server.csr
-rw-------. 1 postgres postgres 1704 Sep  5 13:27 server.key



5 将自签名CA证书配置到数据库

cp root.crt server.crt server.key $PGDATA
vi $PGDATA/postgresql.conf
ssl = on
ssl_ca_file = 'root.crt'    
ssl_cert_file = 'server.crt'
#ssl_crl_file = ''
ssl_key_file = 'server.key'
ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' 

6 重启服务器

pg_ctl restart 

7 使用加密方式连接服务器

psql -h 127.0.0.1 -U postgres -d testdb 
//屏幕输出:登录成功
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

8 使用 verify-ca 方式验证服务器

8.1 配置客户端证书和私钥

 mkdir -p ~/.postgresql && cd ~/.ssl 
 cp root.crt postgresql.crt postgresql.key ~/.postgresql

8.2 配置环境变量

export PGSSLMODE="verify-ca"  

8.3 登录数据库

psql -h 192.168.10.170 -U postgres -d testdb

9 使用verify-full 方式验证服务器

9.1 配置环境变量

export PGSSLMODE="verify-full"  

9.2 登录数据库

[postgres@node1 .ssl]$ export PGSSLMODE="verify-full"
[postgres@node1 .ssl]$ psql -h node1 -U user01 -d testdb
psql: error: server certificate for "192.168.10.170" does not match host name "node1"
[postgres@node1 .ssl]$ psql -h 127.0.0.1 -U user01 -d testdb
psql: error: server certificate for "192.168.10.170" does not match host name "127.0.0.1"
[postgres@node1 .ssl]$ psql -h 127.0.0.1 -U postgresql -d testdb
psql: error: server certificate for "192.168.10.170" does not match host name "127.0.0.1"
[postgres@node1 .ssl]$ psql -h 127.0.0.1 -U postgres -d testdb
psql: error: server certificate for "192.168.10.170" does not match host name "127.0.0.1"

Note

1、设置为verify-ca仅校验数据库证书真伪,
2、设置为verify-full校验数据库证书真伪及通用名CN匹配数据库连接的hostname。
2、clientcert认证选项设置为verify-ca仅校验客户端证书真伪,设置为verify-full校验客户端证书真伪及通用名CN匹配数据库用户或用户映射。
3.使用clientcert认证选项时,连接类型可以设置为hostssl,但host类型也同时支持hostssl及hostnossl。
4.使用cert认证方法只能设置连接类型为hostssl。
5.客户端证书clientcert=verify-full认证方式,openGauss与PostgreSQL有差异,

参考文档

openGauss与PostgreSQL对比测试SSL之自签名CA证书双向认证测试 - 知乎 (zhihu.com)