使用 Hyper Protect Crypto Services PKCS #11 for IBM Db2 本机加密
当密钥数据库文件和数据库备份映像存储在外部存储介质上时,IBM Db2® 本机加密可保护它们免受不当访问。 数据库系统在被授权用户和应用程序使用时自动对数据进行加密和解密。 通常,数据库用户不需要了解本机加密,数据库客户机应用程序也不需要专门进行调整。
Db2 本机加密使用两层密钥层次结构: 使用数据加密密钥 (DEK) 对数据进行加密。 DEK 使用主密钥进行加密,并以加密形式与数据库或备份映像一起存储。 Db2 针对每个加密数据库和每个加密备份生成唯一的 DEK。
主密钥用于加密 DEK。 每个加密数据库一次与一个主密钥关联。
规划 Db2 本机加密时的一个重要问题是在何处保留主密钥以及如何保护主密钥。
目标
本教程说明如何通过将主密钥存储在 IBM Cloud® Hyper Protect Crypto Services中来保持对主密钥的完全和独占控制。 为此,您需要使用 Hyper Protect Crypto Services的 PKCS #11 集成功能。
通过本教程,您将实现下图中描述的设置。
在此设置中,Db2 将调用操作以管理 Hyper Protect Crypto Services PKCS #11 库上的主密钥。 Hyper Protect Crypto Services PKCS #11 库与 Hyper Protect Crypto Services 实例进行交互,这为存储和管理主密钥提供了最佳的类技术。
准备工作
要完成本教程,您需要满足以下先决条件:
任务流程
要完成此解决方案,请完成以下步骤:
初始化 Hyper Protect Crypto Services 实例
-
对于本教程,您需要首先 初始化 Hyper Protect Crypto Services 实例。
记下 Hyper Protect Crypto Services 实例的标识和 EP11 端点地址。 后续步骤需要此信息。
-
创建定制 IAM 角色
Discover HPCS
。 此角色提供非常有限的许可权来发现 PKCS #11 库所需的 Hyper Protect Crypto Services 实例。 此角色无权使用,创建或管理密钥或 EP11 密钥库。- 在 UI 中,转至 管理 > 访问权 (IAM),然后选择 角色,然后单击 创建。
- 输入角色的名称
Discover HPCS
。 - 输入角色的标识。 此标识在 CRN 中使用,在您使用 API 分配访问权时使用此标识。 角色标识必须以大写字母开头,并且仅使用字母数字字符; 例如,
DiscoverHPCS
。 - 可选: 输入帮助分配访问权的用户知道角色分配为用户提供的访问权级别的简明且有用的描述。 当用户分配对服务的访问权时,此描述也会显示在 UI 中。
- 从服务列表中,选择 Hyper Protect Crypto Services
- 针对
hs-crypto.discovery.listservers
操作选择 添加,然后单击 创建。
-
遵循 设置 PKCS #11 API 用户类型 中的指示信息,为普通用户和匿名用户设置服务标识和 API 密钥。
请勿设置指示信息中提到的 SO 用户类型。 另外,与指示信息相反,请勿向匿名用户服务标识分配
Key operator
定制角色,而是改为分配Discover HPCS
定制角色。通过此设置,匿名用户仅对 Hyper Protect Crypto Services 实例具有非常有限的许可权,并且无法使用,创建或管理密钥或 EP11 密钥库。
-
保存正常用户和匿名用户的 API 密钥值以执行后续步骤。
-
遵循 指示信息 以创建专用 EP11 密钥库,并记下后续步骤的密钥库标识。
设置 Hyper Protect Crypto Services PKCS #11 库
1. 运行 Db2 Community Edition 容器
-
使用以下命令来运行 Db2 Community Edition 容器:
docker run -itd --name mydb --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=password -e DBNAME=testdb ibmcom/db2
-
从主机系统上的命令行运行以下命令:
docker exec -it --user root --workdir / mydb bash
此 shell 将用于作为后续步骤的 root
来运行命令。
2. 创建 Hyper Protect Crypto Services PKCS #11 配置文件
现在,为 Hyper Protect Crypto Services PKCS #11 功能部件创建配置文件。 配置文件名为 grep11client.yaml
。
调整以下文件模板并将其命名为 grep11client.yaml
:
- 将
<instance_ID>
替换为 Hyper Protect Crypto Services 实例的标识 - 将
<EP11_endpoint_URL>
和<EP11_endpoint_port_number>
替换为 Hyper Protect Crypto Services 实例的 EP11 端点地址的相应参数 - 将
<private_keystore_id>
替换为先前创建的专用密钥库的标识 - 将
<anonymous_user_api_key>
替换为匿名用户的相应 API 密钥
iamcredentialtemplate: &defaultiamcredential
enabled: true
endpoint: "https://iam.cloud.ibm.com"
# The Universally Unique IDentifier (UUID) of your Hyper Protect Crypto Services instance.
instance: "<instance_ID>"
tokens:
0:
grep11connection:
# The EP11 endpoint address starting from 'ep11'. For example: "<instance_ID>.ep11.us-south.hs-crypto.appdomain.cloud"
address: "<EP11_endpoint_URL>"
port: "<EP11_endpoint_port_number>" # The EP11 endpoint port number
tls:
enabled: true # EP11 requires TLS connection.
mutual: false
storage:
remotestore:
enabled: true
users:
# The Security Officer (SO) user
0: # The index of the Security Officer (SO) user MUST be 0.
# The name for the Security Officer (SO) user. For example: "Administrator":
name: "Administrator"
iamauth: *defaultiamcredential
# The normal user
1: # The index of the normal user MUST be 1.
# The name for the normal user. For example: "Normal user":
name: "Normal user"
# The 128-bit UUID of the private keystore which you created previously
tokenspaceID: "<private_keystore_id>"
iamauth: *defaultiamcredential
# The anonymous user
2: # The index of the anonymous user MUST be 2.
# The name for the anonymous user. For example: "Anonymous":
name: "Anonymous"
# The public keystore will not be used with this setup.
# Specify an arbitrary 128-bit UUID below, e.g.:
tokenspaceID: "12345678-1234-1234-1234-1234567890AB"
iamauth:
<<: *defaultiamcredential
# Provide the API key for the Anonymous user.
apikey: "<anonymous_user_api_key>"
logging:
# Set the logging level.
# The supported levels, in an increasing order of verboseness: 'panic', 'fatal', 'error', 'warning'/'warn', 'info', 'debug', 'trace'. The Default value is 'warning'.
loglevel: "info"
logpath: "/tmp/grep11client.log" # The full path of your logging file.
3. 安装 Hyper Protect Crypto Services PKCS #11 库
-
将先前创建的配置文件
grep11client.yaml
和 PKCS #11 库pkcs11-grep11-<platform>.so.<version>
复制到 Db2 容器中。 -
以
root
身份运行以下命令,以在 Db2 安装中安装 Hyper Protect Crypto Services PKCS #11 库。mkdir /etc/ep11client chmod a+rx /etc/ep11client/ cp grep11client.yaml /etc/ep11client/grep11client.yaml chmod a+r /etc/ep11client/grep11client.yaml mkdir -p /pkcs11 cp pkcs11-grep11-<platform>.so.<version> /pkcs11/pkcs11-grep11.so chmod -R a+rwx /pkcs11 touch /tmp/grep11client.log chmod a+rw /tmp/grep11client.log
设置 Db2 本机加密
现在,我们来设置 Db2 本机加密。 为此,请确保您具有所有数据库管理员特权。
-
使用以下内容创建文件
/pkcs11/keystore.conf
:VERSION=1 PRODUCT_NAME=Other ALLOW_KEY_INSERT_WITHOUT_KEYSTORE_BACKUP=true LIBRARY=/pkcs11/pkcs11-grep11.so SLOT_ID=0 NEW_OBJECT_TYPE=PRIVATE KEYSTORE_STASH=/pkcs11/pkcs11_pw.sth
-
以
root
身份运行以下命令以更新文件/pkcs11/keystore.conf
的所有权和许可权:chown -R db2inst1:db2iadm1 /pkcs11/keystore.conf chmod ug+rw /pkcs11/keystore.conf
-
To create a password stash file, run the following commands and replace <normal_user_api_key> by the API key of the normal user that you created.
su - db2inst1 db2credman -stash -password "<normal_user_api_key>" -to /pkcs11/pkcs11_pw.sth
-
要更新 Db2 配置,请以用户
db2inst1
身份运行以下命令:db2 update dbm cfg using keystore_location /pkcs11/keystore.conf keystore_type pkcs11
-
要设置环境变量
DB2_DEK_MAC_TYPE
,请以用户db2inst1
身份运行以下命令,然后重新启动 DB2:db2 terminate db2stop export DB2_DEK_MAC_TYPE=HMAC db2start
在启动 Db2之前,需要指定环境变量
DB2_DEK_MAC_TYPE=HMAC
。 如果在 Windows 上使用 Db2,那么需要使用以下命令来设置 Db2 概要文件变量:db2set -g DB2_DEK_MAC_TYPE=HMAC
-
要创建加密数据库,请运行以下命令:
db2 create db cryptdb1 encrypt
此命令打印以下信息:
DB20000I The CREATE DATABASE command completed successfully.
-
要测试加密数据库,请运行以下命令:
db2 connect to cryptdb1 db2 "create table test (id int not null, data varchar(100))" db2 "insert into test values (1, 'This is a secret text')" db2 "select * from test"
此命令打印以下信息:
ID DATA ----------- ---------------------------------------------------------------------------------------------------- 1 This is a secret text 1 record(s) selected.
后续步骤
您的敏感数据现在安全地存储在加密存储器中。 主密钥以高度安全且防篡改的方式保存在 Hyper Protect Crypto Services 中。
在本教程中,您学习了如何使用 Hyper Protect Crypto Services设置 Db2 本机加密。
- 了解有关 PKCS #11 的更多信息。
- 了解有关 PKCS #11 API 的更多信息。
- 开始使用 PKCS #11 API。