IBM Cloud Docs
将 Hyper Protect Crypto Services PKCS #11 用于 Oracle 透明数据库加密

将 Hyper Protect Crypto Services PKCS #11 用于 Oracle 透明数据库加密

透明数据加密 (TDE) 是一种完善的技术,用于加密数据库中的敏感数据。 TDE 受各种流行的数据库系统 (包括云和本地) 支持,例如 Oracle® 数据库。 通过 TDE,数据库系统会对数据库存储介质 (例如表空间和文件) 上的数据以及备份介质上的数据进行加密。 数据库系统在被授权用户和应用程序使用时自动且透明地对数据进行加密和解密。 数据库用户不需要了解 TDE,数据库应用也不需要专门针对 TDE 进行适配。

通常,TDE 使用两层密钥层次结构,由 TDE 主加密密钥和 TDE 数据加密密钥组成。 TDE 数据加密密钥用于加密和解密数据,而 TDE 主加密密钥用于加密和解密 TDE 数据加密密钥。

因此,您在规划 TDE 时的一个重要问题是: 您在何处保留 TDE 主加密密钥,以及如何保护它?

目标

本教程说明如何通过将 TDE 主加密密钥存储在 IBM Cloud® Hyper Protect Crypto Services中来保持对这些密钥的完全和独占控制。 为此,您需要使用 Hyper Protect Crypto Services的 PKCS #11 集成功能。

通过本教程,您将实现下图中描述的设置。

使用标准 PKCS 的透明数据库加密 #11 API
图 1。 使用标准 PKCS #11 API
进行透明数据库加密

在此设置中,Oracle Database 将调用操作来管理 Hyper Protect Crypto Services PKCS #11 库上的 TDE 主加密密钥。 Hyper Protect Crypto Services PKCS #11 库与 Hyper Protect Crypto Services 实例进行交互,这为存储和管理 TDE 主加密密钥提供了最佳的类技术。

准备工作

要完成本教程,您需要满足以下先决条件:

任务流程

要完成此解决方案,请完成以下步骤:

  1. 初始化 Hyper Protect Crypto Services 实例
  2. 在 Oracle Database 环境中设置 Hyper Protect Crypto Services PKCS #11 库
  3. 设置 Oracle Database TDE 并加密数据

让我们从 Hyper Protect Crypto Services 实例初始化过程开始。

初始化 Hyper Protect Crypto Services 实例

  1. 对于本教程,您需要首先 初始化 Hyper Protect Crypto Services 实例

    记下 Hyper Protect Crypto Services 实例的标识和 EP11 端点地址。 后续步骤需要此信息。

  2. 生成用于访问 Hyper Protect Crypto Services 实例的 API 密钥。 运行以下命令为 IBM Cloud 帐户创建 API 密钥:

    ibmcloud iam api-key-create apikeyhpcs -d "API key for Hyper Protect Crypto Services PKCS11"
    
  3. 保存 API 密钥的值以用于后续步骤。

在 Oracle Database 环境中设置 Hyper Protect Crypto Services PKCS #11 库

1. 设置 Oracle Database

您需要使用 Oracle Advanced Security 安装 Oracle Database Enterprise Edition。 本教程使用单个实例 Oracle Database 19.3 Enterprise Edition Docker 容器。 有关 Oracle Database 容器的更多信息以及有关构建相应容器的指示信息,请参阅 Docker 上的 Oracle Database

  1. 启动 Oracle Database 容器:

    docker run --name oradb -p 1521:1521 -p 5500:5500 -e ORACLE_PWD=password oracle/database:19.3.0-ee
    

    等待实例和数据库创建完成。

  2. 从主机系统上的命令行运行以下命令:

    docker exec -it --user root --workdir / oradb 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 端点地址的相应参数。
  • <your_api_key> 替换为您创建的 API 密钥的值。
iamcredentialtemplate: &defaultiamcredential
          enabled: true
          endpoint: "https://iam.cloud.ibm.com"
          # Keep the 'apikey' empty. It will be overridden by the Anonymous user API key configured later.
          apikey:
          # 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>"
      # The EP11 endpoint port number
      port: "<EP11_endpoint_port_number>"
      tls:
        # Grep11 requires TLS connection.
        enabled: true
        # Grep11 requires server only authentication, so 'mutual' needs to be set as 'false'.
        mutual: false
        # 'cacert' is a full-path certificate file.
        # In Linux with the 'ca-ca-certificates' package installed, this is normally not needed.
        cacert:
        # Grep11 requires the server-only authentication, so 'certfile' and 'keyfile' need to be empty.
        certfile:
        keyfile:
    storage:
      filestore:
        enabled: false
        storagepath:
        # 'remotestore' needs to be enabled if you want to generate keys with the attribute CKA_TOKEN.
      remotestore:
        enabled: true
    users:
      0: # The index of the Security Officer (SO) user MUST be 0.
        # The name for the Security Officer (SO) user. For example: "Administrator".
        # NEVER put the API key under the SO user for security reasons.
        name: "Administrator"
        iamauth:
          <<: *defaultiamcredential
      1: # The index of the normal user MUST be 1.
        # The name for the normal user. For example: "Normal user".
        # NEVER put the API key under the normal user for security reasons.
        name: "Normal user"
         # The Space ID is a 128-bit UUID and can be chosen freely.
         # The UUID can be generated by third-party tools, such as 'https://www.uuidgenerator.net/'.
         # For example: "f00db2f1-4421-4032-a505-465bedfa845b".
         # 'tokenspaceID' under the normal user is to identify the private keystore.
        tokenspaceID: "f00db2f1-4421-4032-a505-465bedfa845b"
        iamauth:
          <<: *defaultiamcredential
      2: # The index of the anonymous user MUST be 2.
        # The name for the anonymous user. For example: "Anonymous".
        name: "Anonymous"
        # The Space ID is a 128-bit UUID and can be chosen freely.
        # The UUID can be generated by third-party tools, such as 'https://www.uuidgenerator.net/'.
        # For example: "ca22be26-b798-4fdf-8c83-3e3a492dc215".
        # 'tokenspaceID' under the anonymous user is to identify the public keystore.
        tokenspaceID: "ca22be26-b798-4fdf-8c83-3e3a492dc215"
        iamauth:
          <<: *defaultiamcredential
          # This API key for the Anonymous user must be provided.
          # It will overide the 'apikey' in the previous defaultcredentials.iamauth.apikey field
          apikey: "<your_api_key>"
logging:
  # Set the logging level.
  # The supported levels, in an increasing order of verboseness, are:
  # 'panic', 'fatal', 'error', 'warning'/'warn', 'info', 'debug', 'trace'.
  # The Default value is 'debug'.
  loglevel: debug
  # The full path of your logging file.
  # For example: /tmp/grep11client.log
  logpath: /tmp/grep11client.log

3. 安装 Hyper Protect Crypto Services PKCS #11 库

  1. 下载最新的 PKCS #11 库

  2. 将创建的配置文件 grep11client.yaml 和 PKCS #11 库 pkcs11-grep11-<platform>.so.<version> 复制到 Oracle Database 容器中的主文件夹中。

  3. root 身份运行以下命令,以在 Oracle Database 设置中安装 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 /opt/oracle/extapi/64/hsm/ibm
    cp pkcs11-grep11.so.1.1.3 /opt/oracle/extapi/64/hsm/ibm/pkcs11-grep11.so
    chown -R oracle:oinstall /opt/oracle/extapi
    
    touch /tmp/grep11client.log
    chmod a+rw /tmp/grep11client.log
    chown oracle:oinstall /tmp/grep11client.log
    

目录 /opt/oracle/extapi/64/hsm 和子目录只能包含一个库文件。 除去该目录和子目录中存在的任何其他库文件。

4。请检查库设置

  1. 使用以下命令安装命令行实用程序 OpenSC (pkcs11-tool):

    sudo yum install opensc
    
  2. root 身份运行以下命令以检查库设置:

    pkcs11-tool --module=/opt/oracle/extapi/64/hsm/ibm/pkcs11-grep11.so -I
    

    此命令打印有关制造商和库的信息,例如:

    Cryptoki version 2.40
    Manufacturer     IBM ...
    Library          GREP11 PKCS11 client ...
    

5. 初始化 Hyper Protect Crypto Services PKCS #11 库

  1. 从主机系统上的命令行运行以下命令:

    docker exec -it oradb bash
    

    此 shell 可用于以用户 oracle 身份运行命令以执行后续步骤。

  2. 要初始化令牌,请运行以下命令并将 <your_api_key> 替换为您创建的 API 密钥。

    pkcs11-tool  --module /opt/oracle/extapi/64/hsm/ibm/pkcs11-grep11.so --init-token --label dbtoken --so-pin=<your_api_key>
    

    此命令打印以下状态消息,例如:

    Using slot 0 with a present token (0x0)
    Token successfully initialized
    

设置 Oracle Database TDE 并加密数据

现在让我们来扮演数据库管理员的角色。

  1. 通过添加以下行来更新目录 "$ORACLE_HOME/network/admin" 中的文件 "sqlnet.ora":

    encryption_wallet_location=(source=(method=hsm))
    

    为此,您可以运行以下命令:

    echo "encryption_wallet_location=(source=(method=hsm))" >> $ORACLE_HOME/network/admin/sqlnet.ora
    

    确保文件 "sqlnet.ora" 不包含 encryption_wallet_location 的其他设置。

  2. 使用以下命令打开密钥库。 将 <your_api_key> 替换为您创建的 API 密钥:

    export ORACLE_SID=<your SID, e.g. ORCLCDB>
    sqlplus / as sysdba
    SQL> ADMINISTER KEY MANAGEMENT SET KEYSTORE OPEN IDENTIFIED BY "<your_api_key>" CONTAINER=ALL;
    
  3. 使用以下命令创建主密钥。 将 <your_api_key> 替换为您创建的 API 密钥:

    SQL> ADMINISTER KEY MANAGEMENT SET KEY IDENTIFIED BY "<your_api_key>" WITH BACKUP CONTAINER=ALL;
    
  4. 使用以下命令创建加密表空间:

    SQL> CREATE TABLESPACE encrypted_ts DATAFILE 'tbs1_data.dbf' SIZE 128K AUTOEXTEND ON NEXT 64K ENCRYPTION USING 'AES256' DEFAULT STORAGE(ENCRYPT);
    
  5. 要验证设置,可以在加密表空间中创建表并插入一些测试数据:

    SQL> CREATE TABLE tde_ts_test (id    NUMBER(10), data  VARCHAR2(50)) TABLESPACE encrypted_ts;
    SQL> INSERT INTO tde_ts_test VALUES (1, 'This is a secret!');
    SQL> COMMIT;
    SQL> SELECT * FROM TDE_TS_TEST;
    
  6. 您还可以创建具有加密列的表,并插入一些测试数据:

    SQL> CREATE USER C##test IDENTIFIED BY test;
    SQL> GRANT UNLIMITED TABLESPACE TO C##test;
    SQL> CREATE TABLE C##test.tde_test (id NUMBER(10), data VARCHAR2(50) ENCRYPT);
    SQL> INSERT INTO C##test.tde_test VALUES (1, 'This is also a secret!');
    SQL> COMMIT;
    SQL> SELECT * FROM C##test.tde_test;
    
    # Verify encrypted tablespace and encrypted column
    SQL> SELECT TABLESPACE_NAME, ENCRYPTED FROM DBA_TABLESPACES;
    SQL> SELECT * FROM dba_encrypted_columns ;
    

后续步骤

现在,敏感数据安全地存储在已加密的表空间和已加密的列中。 而 TDE 主加密密钥以高度安全且防篡改的方式保存在 Hyper Protect Crypto Services 中。

在本教程中,您学习了如何使用 Hyper Protect Crypto Services来设置 Oracle Database TDE。