数据传输加密是指在网络中传输的数据需要加密传输,从而确保数据的机密性和完整性。
针对网络中传输的数据进行加密,焦点就在连接上。对于GBase 8s体系下,总共有如下连接场景需要进行数据传输加密:
GBase 8s配置传输加密功能,首先需要生成数字证书,后续将数字证书分别导入到服务端和客户端的密钥数据库(也称作密钥库)中。
生成证书的详细步骤如下:
openssl genrsa -des3 -out ca.key 2048
这一步生成3倍DES加密的根证私钥
Generating RSA private key, 2048 bit long modulus
........................................................+++
.........................+++
e is 65537 (0x10001)
Enter pass phrase for ca.key: # 输入密码GBase123
Verifying - Enter pass phrase for ca.key: # 输入密码GBase123
openssl req -new -x509 -days 3650 -key ca.key -out ca.pem
这一步生成自签名的CA根证。它的是符合x509规范的,格式是PEM的。
Enter pass phrase for ca.key: # 输入密码GBase123
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN # 国名
State or Province Name (full name) []:TianJin # 省名
Locality Name (eg, city) [Default City]:TianJin # 市名
Organization Name (eg, company) [Default Company Ltd]:GBase # 公司名
Organizational Unit Name (eg, section) []: # 组织名
Common Name (eg, your name or your server's hostname) []:GBase.cn #通用名
Email Address []:[email protected] # 邮件地址
openssl genrsa -des3 -out subca.key 2048
这一步生成中间文件subca.key。
Generating RSA private key, 2048 bit long modulus
................+++
........+++
e is 65537 (0x10001)
Enter pass phrase for subca.key: # 输入密码GBase123
Verifying - Enter pass phrase for subca.key: # 输入密码GBase123
接着
openssl req -new -key subca.key -out subca.csr
再生成subca.csr
Enter pass phrase for subca.key: # 输入密码GBase123
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN # 国名
State or Province Name (full name) []:TianJin # 省名
Locality Name (eg, city) [Default City]:TianJin # 市名
Organization Name (eg, company) [Default Company Ltd]:GBase # 公司名
Organizational Unit Name (eg, section) []: # 组织名
Common Name (eg, your name or your server's hostname) []:GBase.cn #通用名
Email Address []:[email protected] # 邮件地址
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:GBase123 # 输入密码GBase123
An optional company name []:GBase.cn # 公司名
openssl x509 -req -days 3650 -in subca.csr -CA ca.pem -CAkey ca.key -set_serial 01 -out subca.pem
生成二级ca证书
Signature ok
subject=/C=CN/ST=TianJin/L=TianJin/O=GBase/CN=GBase.cn/[email protected]
Getting CA Private Key
Enter pass phrase for ca.key: # 输入密码GBase123
openssl genrsa -des3 -out server.key 2048
这一步生成中间文件server.key
Generating RSA private key, 2048 bit long modulus
...............................................................................................................+++
................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key: # 输入密码GBase123
Verifying - Enter pass phrase for server.key: # 输入密码GBase123
openssl req -new -key server.key -out server.csr
再生成server.csr
Enter pass phrase for server.key: # 输入密码GBase123
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN # 国名
State or Province Name (full name) []:TianJin # 省名
Locality Name (eg, city) [Default City]:TianJin # 市名
Organization Name (eg, company) [Default Company Ltd]:GBase # 公司名
Organizational Unit Name (eg, section) []: # 组织名
Common Name (eg, your name or your server's hostname) []:GBase.cn #通用名
Email Address []:[email protected] # 邮件地址
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:GBase123 # 输入密码GBase123
An optional company name []:GBase.cn # 公司名
openssl x509 -req -days 3650 -in server.csr -CA subca.pem -CAkey subca.key -set_serial 01 -out server.pem
生成server.pem
Signature ok
subject=/C=CN/ST=TianJin/L=TianJin/O=GBase/CN=GBase.cn/[email protected]
Getting CA Private Key
Enter pass phrase for subca.key: # 输入密码GBase123
openssl pkcs12 -export -in ca.pem -inkey ca.key -out server.pfx -passin pass:GBase123 -passout pass:GBase123
这一步将ca.pem加入到server.pfx,需要指定密码
openssl pkcs12 -export -in server.pem -inkey server.key -out server.pfx -passin pass:GBase123 -passout pass:GBase123
将server.pem加入到server.pfx,需要指定密码
CAfile包含证书链上的全部CA文件.
cat ca.pem > total.pem
cat subca.pem >> total.pem
服务端需要配置: total.pem、服务端证书server.pem和私钥server.key。
客户端需要配置:total.pem。
对于服务端,需要将证书文件total.pem、server.pem、server.key保存到$GBASEDBTDIR中的相关目录,比如$GBASEDBTDIR/ssl目录下。
对于客户端,dbaccess、odbc需要将证书文件total.pem保存到$GBASEDBTDIR的相关目录,比如$GBASEDBTDIR/ssl目录下;JDBC需要将证书文件total.pem导入到java密钥库。
本文通过$GBASEDBTDIR来代替GBase 8s产品的安装路径。对于服务端,$GBASEDBTDIR指Server的安装路径;对于客户端,如果是Server中的dbaccess,$GBASEDBTDIR为Server的安装路径,其他情况为CSDK的安装路径。如果Server和CSDK安装到同一个路径下,则$GBASEDBTDIR同时为服务端和客户端的安装路径。
首先,Server需要部署的内容包括如下部分:
VPCLASS encrypt,num=1
NETTYPE socssl,1,150,NET
本章节的所有操作都需要使用操作系统的gbasedbt用户完成。
需要通过${GBASEDBTSQLHOSTS}.ext指定GBS配置文件。
假定Server安装在/opt/gbase中,在sqlhosts中指定socssl的实例名是gbase01_ssl。
如果使用简单传输加密(就是说不使用根证),则配置文件的示例如下:
[config]
GBS_TYPE=gbasessl
[gbase01_ssl]
server=gbase01_server
client=gbase01_client
[gbase01_server]
TLSCertificateFile=/opt/gbase/ssl/server.pem
TLSCertificateKeyFile=/opt/gbase/ssl/server.key
TLSCertificateKeyFilePasswd=hgmzaF71KFqW7ZNkVTPR1g==
// GBase123的密文,使用gbspwdenc 进行加密后的密文
[gbase01_client]
TLSCACertificateFile=/opt/gbase/ssl/total.pem
TLSVerifyCert=verify
由于Server端也会部署dbaccess,另外,Server端也会安装csdk。因此,需要在配置文件中写入gbase01_client。
根据配置文件的配置,需要将CAfile(total.pem),服务端证书server.pem和私钥server.key,保存到$GBASEDBTDIR/ssl中。
修改$GBASEDBTDIR/etc/sqlhosts配置文件, 将需要启用传输加密功能的实例的连接类型设置为onsocssl;如下述示例中,两个数据库服务名称gbase01、gbase01_ssl,服务名gbase01连接类型为onsoctcp,为普通的tcp通信方式,服务名gbase01_ssl的连接类型为onsocssl,启用了传输加密方式:
gbase01 onsoctcp 192.168.0.4 9088
gbase01_ssl onsocssl 192.168.0.4 9089
修改$GBASEDBTDIR/etc/$ONCONFIG配置文件,配置GBS参数。
DBSERVERNAME gbase01
DBSERVERALIASES gbase01_ssl
VPCLASS encrypt,num=1
NETTYPE socssl,1,50,NET
服务端证书配置好后,需要重启数据库服务来生效。
这里特指只安装了CSDK的客户端
CSDK需要部署的内容包括如下部分:
客户端也需要通过${GBASEDBTSQLHOSTS}.ext指定GBS配置文件。
假定CSDK客户端安装在/opt/gbase中。客户端需要连接的实例名是gbase01_ssl。如果使用简单传输加密(就是说不使用根证),配置文件示例,如下:
[config]
GBS_TYPE=gbasessl
[gbase01_ssl]
client=gbase01_client
[gbase01_client]
TLSCACertificateFile=/opt/gbase/ssl/total.pem
TLSCertificateKeyFilePasswd=hgmzaF71KFqW7ZNkVTPR1g==
// GBase123的密文,使用gbspwdenc 进行加密后的密文
dbaccess、odbc通过修改$GBASEDBTDIR/etc/sqlhosts配置文件来指定连接的启用传输加密的数据库服务实例,并将连接类型设置为onsocssl来启用客户端的传输加密功能,示例如下:
gbase01_ssl onsocssl 192.168.0.4 9089
示例中gbase01_ssl为启用传输加密的数据库服务实例。
配置完后通过dbaccess连接启用传输加密的数据库服务,如果可以正常连接并执行SQL语句,则dbaccess传输加密功能配置成功。
export GBASEDBTDIR=/opt/gbase # 安装目录
export GBASEDBTSERVER=gbase01_ssl # 服务名称
export GBASEDBTSQLHOSTS=${GBASEDBTDIR}/etc/sqlhosts # SQLHOSTS文件
示例中GBASEDBTDIR根据dbaccess为Server还是CSDK中的dbaccess来设置为CSDK或者Server的安装路径。
2) dbaccess连接
执行dbaccess,然后执行SQL,如果执行成功则说明dbaccess配置后可以正常运行。
可以通过unixODBC程序来测试ODBC在配置传输加密功能后能否正常使用
export GBASEDBTDIR=/opt/gbase # 安装目录
export GBASEDBTSERVER=gbase01_ssl # 服务名称
export GBASEDBTSQLHOSTS=${GBASEDBTDIR}/etc/sqlhosts # SQLHOSTS文件
export ODBCINI=/home/gbase/odbc.ini # ODBCINI位置
export LD_LIBRARY_PATH=${GBASEDBTDIR}/lib:$GBASEDBTDIR/lib/cli:${GBASEDBTDIR}/lib/esql:${LD_LIBRARY_PATH} # LD库文件目录
;---------------------------------------------------------------------------
; GBase ODBC Sample File
;
; File: odbc.ini
;
;---------------------------------------------------------------------------
[ODBC Data Sources]
gbase01_ssl=GBase ODBC DRIVER
;
; Define ODBC Database Driver's Below - Driver Configuration Section
;
[gbase01_ssl]
Driver=/opt/gbase/lib/cli/iclis09b.so
Description=GBase ODBC DRIVER
Database=testdb
LogonID=gbasedbt
pwd=GBase123
Servername=gbase01_ssl
CursorBehavior=0
CLIENT_LOCALE=zh_CN.utf8
DB_LOCALE=zh_CN.utf8
TRANSLATIONDLL=/opt/gbase/lib/esql/igo4a304.so
;
; UNICODE connection Section
;
[ODBC]
;uncomment the below line for UNICODE connection
;UNICODE=UCS-4
;
; Trace file Section
;
Trace=0
TraceFile=/tmp/odbctrace.out
InstallDir=/opt/gbase
TRACEDLL=idmrs09a.so
isql -v gbase01_ssl
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select user from dual;
+---------------------------------+
| |
+---------------------------------+
| gbasedbt |
+---------------------------------+
SQLRowCount returns -1
1 rows fetched
SQL>
JDBC的传输加密功能独立于其他客户端的配置,需要将证书导入到JAVA的证书文件,并且在使用JDBC的程序中进行必要的设置。
确保使用gbasedbt用户进行操作。
首先,将CA证书文件server.pem和server.pfx拷贝到java目录下,例如/home/gbase/java目录下。
然后,使用java的证书导入工具keytool将server.pem和server.pfx生成client.keystore
keytool -importcert -alias ca -file server.pem -keystore client.keystore
将server.pem加入到client.keystore中
Enter keystore password: # 输入GBase123
Re-enter new password: # 输入GBase123
Owner: [email protected], CN=GBase.cn, O=GBase, L=TianJin, ST=TianJin, C=CN
Issuer: [email protected], CN=GBase.cn, O=GBase, L=TianJin, ST=TianJin, C=CN
Serial number: 1
Valid from: Fri Jul 03 10:53:15 CST 2020 until: Sat Jul 03 10:53:15 CST 2021
Certificate fingerprints:
MD5: B8:5E:D8:3B:1E:49:65:89:6E:9B:57:43:31:D8:9D:8D
SHA1: AD:F8:0F:B3:33:4D:D7:9C:6C:E9:26:DB:55:55:DE:52:86:7C:D2:FD
SHA256: 98:6E:52:F5:52:7A:FA:FB:51:26:68:4B:01:7A:31:09:99:AA:8B:71:76:4B:50:AB:E8:44:E1:29:10:BB:5F:8A
Signature algorithm name: SHA256withRSA
Subject Public Key Algorithm: 2048-bit RSA key
Version: 1
Trust this certificate? [no]: yes
Certificate was added to keystore
将server.pfx导入到client.keystore中
keytool -importkeystore -srckeystore server.pfx -srcstoretype pkcs12 -destkeystore client.keystore
忽视警告:
Importing keystore server.pfx to client.keystore...
Enter destination keystore password: # 输入GBase123
Enter source keystore password: # 输入GBase123
Entry for alias 1 successfully imported.
Import command completed: 1 entries successfully imported, 0 entries failed or cancelled
Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore client.keystore -destkeystore client.keystore -deststoretype pkcs12".
在JAVA程序中需要通过下述语句来启用和设置SSL,红色部分需要根据实际情况来设置:
/* System properties for keystore */
System.setProperty("javax.net.ssl.trustStore", "/绝对路径/client.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "证书文件密码");
/* Instantiate Gbasedbt connection pooled data source */
IfxConnectionPoolDataSource cds = new IfxConnectionPoolDataSource();
cds.setIfxSSLCONNECTION("true"); //启用ssl加密
本节提供了一个验证JDBC传输加密功能的示例程序,连接数据库服务并获取数据库服务的版本信息,用户根据实际情况修改示例程序中的红色部分,然后按照步骤编译运行即可。
import java.sql.Connection;
import java.sql.SQLException;
import com.gbasedbt.jdbc.IfxDriver;
import com.gbasedbt.jdbcx.IfxConnectionPoolDataSource;
public class GBasedbtSSLConnection {
public static void main(String[] args) {
Connection conn = null;
try {
/* System properties for keystore */
System.setProperty("javax.net.ssl.trustStore", "/home/gbase/java/client.keystore"); // client.keystore的绝对路径
System.setProperty("javax.net.ssl.trustStorePassword", "GBase123"); // 证书文件密码
/* Instantiate GBasedbt connection pooled data source */
IfxConnectionPoolDataSource cds = new IfxConnectionPoolDataSource();
/* Set SSLConnection property to true and port pointing to SSL port on the server */
cds.setIfxIFXHOST("192.168.0.4"); //服务器IP
cds.setServerName("gbase01_ssl"); //启用传输加密的数据库服务实例名
cds.setUser("gbasedbt"); //用户名
cds.setPassword("GBase123"); //密码
cds.setDatabaseName("testdb"); //所连接的数据库
cds.setPortNumber(9089); //实例所用端口号
cds.setIfxDB_LOCALE("zh_CN.utf8"); //设置DB_LOCALE
cds.setIfxCLIENT_LOCALE("zh_CN.utf8"); //设置CLIENT_LOCALE
cds.setIfxSSLCONNECTION("true"); //启用ssl加密
conn = cds.getPooledConnection().getConnection();
if(conn != null) {
System.out.println(" Successfully connected to GBasedbt database using SSL Connection");
System.out.println(" Database version ...: " + conn.getMetaData().getDatabaseProductVersion());
System.out.println(" JDBC Driver Version .: " + IfxDriver.getJDBCVersion());
}
}catch (Exception e) {
System.err.println("Error Message : " +e.getMessage());
if(e instanceof SQLException)
System.err.println("Error Code : " + ((SQLException)e).getErrorCode());
e.printStackTrace();
}finally {
if(conn != null)
try {
conn.close();
} catch (SQLException e) {}
}
}
}
编译,需要将ifxjdbc.jar及ifxjdbcx.jar加入到CLASSPATH中:
javac -cp .:./ifxjdbc.jar:./ifxjdbcx.jar GBasedbtSSLConnection.java
运行
java -cp .:./ifxjdbc.jar:./ifxjdbcx.jar GBasedbtSSLConnection
结果如下,表示使用ssl连接成功
Successfully connected to GBasedbt database using SSL Connection
Database version ...: 12.10.FC4G1AEE
JDBC Driver Version .: 4.10.JC4G1N999