使用 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 實例的 ID 及 EP11 端點位址。 後續步驟需要此資訊。
-
建立自訂 IAM 角色
Discover HPCS。 此角色提供非常有限的許可權來探索 PKCS #11 程式庫所需的 Hyper Protect Crypto Services 實例。 此角色無權使用、建立或管理金鑰或 EP11 金鑰儲存庫。- 從使用者介面中,移至 管理 > 存取權 (IAM),並選取 角色,然後按一下 建立。
- 輸入角色的名稱
Discover HPCS。 - 輸入角色的 ID。 此 ID 在 CRN 中使用,當您使用 API 來指派存取權時使用。 角色 ID 必須以大寫字母開頭,且只能使用英數字元; 例如
DiscoverHPCS。 - 選用項目: 輸入簡潔且有用的說明,以協助指派存取權的使用者知道角色指派提供給使用者的存取權層次。 當使用者指派服務的存取權時,此說明也會顯示在使用者介面中。
- 從服務清單中,選取 Hyper Protect Crypto Services
- 針對
hs-crypto.discovery.listservers動作選取 新增,然後按一下 建立。
-
遵循 設定 PKCS #11 API 使用者類型 中的指示,為一般使用者及匿名使用者設定服務 ID 及 API 金鑰。
請勿設定指示中提及的 SO 使用者類型。 此外,與指示相反,請勿將
Key operator自訂角色指派給匿名使用者服務 ID,而是改為指派Discover HPCS自訂角色。使用此設定,匿名使用者對 Hyper Protect Crypto Services 實例只有非常有限的許可權,無法使用、建立或管理金鑰或 EP11 金鑰儲存庫。
-
儲存一般使用者及匿名使用者的 API 金鑰值,以進行後續步驟。
-
遵循 指示 來建立專用 EP11 金鑰儲存庫,並記下金鑰儲存庫 ID 以進行後續步驟。
設定 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 實例的 ID - 將
<EP11_endpoint_URL>和<EP11_endpoint_port_number>取代為 Hyper Protect Crypto Services 實例之 EP11 端點位址的個別參數 - 將
<private_keystore_id>取代為您先前建立的專用金鑰儲存庫 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。