如何与其他用户共享密钥,以仅允许在 CloudHSM 中执行某些加密操作?

6 分钟阅读
0

如何允许用户包装密钥,并允许其他用户仅解包该密钥?

简短描述

您可以与用户共享对在 AWS CloudHSM 集群的 HSM 中存储的密钥的访问权限。但是,此操作使您的用户可以拥有所有访问权限(如包装、解包、加密和解密)。

解决方法

导出密钥,然后将其重新导入,并操作两次。导入的密钥属性会随着包装或解包的权限而改变。然后,与不同的用户共享密钥以执行包装或解包操作。

使用 cloudhsm_mgmt_util 命令行工具列出集群中的所有用户

在开始使用 cloudhsm_mgmt_util 工具之前,请参阅 CloudHSM 管理实用程序 (CMU) 入门

运行 listUsers 命令,如下所示:

Number of users found:8

    User Id             User Type       User Name                          MofnPubKey    LoginFailureCnt         2FA
         1              CO              admin                                    NO               0               NO
         2              AU              app_user                                 NO               0               NO
         3              CO              officer1                                 NO               0               NO
         4              CO              crypto_user1                             NO               0               NO
         5              CO              crypto_user2                             NO               0               NO
         6              CU              crypto_user3                             NO               0               NO
         7              CU              crypto_user4                             NO               0               NO
         8              CU              crypto_user5                             NO               0               NO

由于 crypto_user1 和 crypto_user2 是加密员,因此他们不能拥有密钥。此示例使用加密用户的 crypto_user3、crypto_user4 和 crypto_user5。

借助 key_mgmt_util 命令行工具,使用 genSymKey 命令在 HSM 中创建 AES 对称密钥

使用 crypto_user3 登录到 HSM,然后使用 key_mgmt_util 工具创建对称密钥以进行加密和解密。使用 genSymKey 命令创建 AES 256 位密钥,使用标注 rootKey,如下所示:

Command:  genSymKey -t 31 -s 32 -l rootKey

        Cfm3GenerateSymmetricKey returned: 0x00 : HSM Return: SUCCESS

        Symmetric Key Created.  Key Handle: 786512

        Cluster Error Status
        Node id 3 and err state 0x00000000 : HSM Return: SUCCESS
        Node id 4 and err state 0x00000000 : HSM Return: SUCCESS

在本示例中,使用密钥句柄 786512。

使用 genSymKey 命令创建会话密钥,将密钥包装到磁盘,然后重新将其解包到 HSM

使用 -sess 参数创建会话密钥。请确保会话窗口保持打开状态、已建立连接,并且 crypto_user3 保持登录状态。稍后通过标签 sessionKey 使用 genSymKey 命令标识此密钥。

Command:  genSymKey -t 31 -s 32 -sess -l sessionKey

        Cfm3GenerateSymmetricKey returned: 0x00 : HSM Return: SUCCESS

        Symmetric Key Created.  Key Handle: 786511

        Cluster Error Status
        Node id 3 and err state 0x00000000 : HSM Return: SUCCESS
        Node id 4 and err state 0x00000000 : HSM Return: SUCCESS

在本示例中,会话密钥 786511 执行 wrapKeyunWrapKey 操作。

注意:会话关闭后将删除会话密钥。

使用 wrapKey 命令将密钥写入磁盘且不要泄露密钥

将对称密钥 786512 导出文件,放置在本地驱动器上。对称密钥 786512 使用会话密钥 786511 加密。此加密可确保您永远不会将对称密钥 786512 以纯文本形式存储在 HSM 以外的地方。将 rootKey 包装到本地驱动器后,使用 unWrapKey 命令将 rootKey 786512 导回到 HSM。使用不同的密钥句柄在 HSM 中创建 rootKey 的副本。然后,更改 rootKey 副本的属性以限制该密钥的使用方式。

在本示例中,sessionKey 786511 将 rootKey 786512 包装起来,并将其作为 root.key 文件保存到本地驱动器中:

Command:  wrapKey -k 786512 -w 786511 -out root.key

        Key Wrapped.

        Wrapped Key written to file "root.key length 312
        
        Cfm2WrapKey returned: 0x00 : HSM Return: SUCCESS

注意:您可以使用 ls 命令查看本地目录中的包装密钥。

通过新句柄,使用 unWrapKey 命令将密钥导回到 HSM

使用 unWrapKey 命令,将密钥 root.key 导回到 HSM。此示例创建新密钥句柄 786523 和 786524。

重要提示:请执行此步骤两次。

Command:  unWrapKey -f root.key -w 786511

        Cfm3UnWrapKey returned: 0x00 : HSM Return: SUCCESS

        Key Unwrapped.  Key Handle: 786523

        Cluster Error Status
        Node id 3 and err state 0x00000000 : HSM Return: SUCCESS
        Node id 4 and err state 0x00000000 : HSM Return: SUCCESS

Command:  unWrapKey -f root.key -w 786511

        Cfm3UnWrapKey returned: 0x00 : HSM Return: SUCCESS

        Key Unwrapped.  Key Handle: 786524

        Cluster Error Status
        Node id 3 and err state 0x00000000 : HSM Return: SUCCESS
        Node id 4 and err state 0x00000000 : HSM Return: SUCCESS

验证密钥句柄并分配属性

使用 cloudhsm_mgmt_util 命令检查 crypto_user3 拥有的所有密钥句柄。在本示例中,记下 rootKey 786512、导入的密钥句柄 786523 和 786524。为 crypto_user3 输入用户 ID 60,以表明您不想计算所有密钥的哈希值。这样做可以查看 HSM 中使用的密钥句柄。findAllKeys 命令显示的密钥句柄在加密方式上是相同的,具有与 rootKey 786512 相同的属性。

注意:不显示会话密钥句柄 786511,因为该句柄只在会话的生命周期内予以保留。

aws-cloudhsm>findAllKeys 6 0
Keys on server 0(172.31.3.131):
Number of keys found 3
number of keys matched from start index 0::3
786512,786523,786524
findAllKeys success on server 0(172.31.3.131)
Keys on server 1(172.31.4.209):
Number of keys found 3
number of keys matched from start index 0::3
786512,786523,786524
findAllKeys success on server 1(172.31.4.209)

本示例为包装密钥 786524 分配了一个密钥句柄,为解包密钥 786523 分配了另一个密钥句柄。

通过命令 setAttribute,使用 cloudhsm_mgmt_util 配置密钥的包装/解包属性

setAttribute 命令需要密钥句柄和属性 ID。属性 ID 是指与密钥属性相关的整数。使用 listAttibutes 命令列出这些属性。通过密钥 786523,使用 aws-cloudhsm>setAttribute 命令将 OBJ_ATTR_WRAP 属性值设置为 2620

aws-cloudhsm>setAttribute 786523 262 0
*************************CAUTION********************************
This is a CRITICAL operation, should be done on all nodes in the
cluster. Cav server does NOT synchronize these changes with the
nodes on which this operation is not executed or failed, please
ensure this operation is executed on all nodes in the cluster.
****************************************************************

Do you want to continue(y/n)?y
setAttribute success on server 0(172.31.3.131)
setAttribute success on server 1(172.31.4.209)

然后,通过密钥 786524,使用 setAttribute 命令将 OBJ_ATTR_UNWRAP 属性值设置为 2630OBJ_ATTR_WRAP 属性为 262

aws-cloudhsm>setAttribute 786524 263 0
*************************CAUTION********************************
This is a CRITICAL operation, should be done on all nodes in the
cluster. Cav server does NOT synchronize these changes with the
nodes on which this operation is not executed or failed, please
ensure this operation is executed on all nodes in the cluster.
****************************************************************

Do you want to continue(y/n)?y
setAttribute success on server 0(172.31.3.131)
setAttribute success on server 1(172.31.4.209)

现在,您拥有可以包装和解包的 rootKey 786512、只能用于包装的密钥 786524 和只能用于解包的密钥 786523。

在本示例中,crypto_user3 拥有密钥 786512、786523、786524,并且这些密钥不共享。

测试包装密钥,以确认所有属性是否设置正确

使用 key_mgmt_util 命令行工具运行 wrapKey 命令。

使用分配给解包的密钥句柄 786523 将 rootKey 786512 包装到本地驱动器。

然后,使用分配给包装的密钥句柄 786524 将 rootKey 786512 包装到本地驱动器。

Command:  wrapKey -k 786512 -w 786523 -out wrapped.key

        Cfm2WrapKey returned: 0xb3

        HSM Error: This operation violates the current configured/FIPS policies

Command:  wrapKey -k 786512 -w 786524 -out wrapped.key

        Key Wrapped.

        Wrapped Key written to file "wrapped.key.test length 309

        Cfm2WrapKey returned: 0x00 : HSM Return: SUCCESS

在本示例中,针对包装密钥 786523 的命令出现故障,如 HSM 错误中所述。HSM 错误产生的原因是包装 OBJ_ATTR_WRAP 的属性设置为 0

运行 getAttribute 命令,以验证密钥属性值是否设置为 false

getAttribute 命令使用 key_mgmt_utilcloudhsm_mgmt_util 工具。尽管这两个命令行工具的语法不同,但它们都显示属性的结果。此示例使用 cloudhsm_mgmt_util,因为它无需属性即可保存到文件中。

aws-cloudhsm>getAttribute 786523 262
Attribute Value on server 0(172.31.3.131):
OBJ_ATTR_WRAP
0x00000000

Attribute Value on server 1(172.31.4.209):
OBJ_ATTR_WRAP
0x00000000

与其他用户共享密钥

使用 cloudhsm_mgm_util 命令将密钥共享到 crypto-user4(用户 ID 7)和 crypto-user5(用户 ID 8)。将解包密钥 786523 共享到 crypto_user4,将包装密钥 786524 共享到 crypto_user5。此操作允许用户 crypto_user4 和 crypto_user5 使用密钥进行加密,但不能删除、导出、共享、取消共享或更改密钥属性。这可确保用户 crypto_user4 和 crypto_user5 在分配密钥属性时只能对 crypto_user3 指定的操作使用密钥。

aws-cloudhsm>shareKey 786523 7 1
*************************CAUTION********************************
This is a CRITICAL operation, should be done on all nodes in the
cluster. Cav server does NOT synchronize these changes with the
nodes on which this operation is not executed or failed, please
ensure this operation is executed on all nodes in the cluster.
****************************************************************

Do you want to continue(y/n)?y
shareKey success on server 0(172.31.3.131)
shareKey success on server 1(172.31.4.209)


aws-cloudhsm>shareKey 786524 8 1
*************************CAUTION********************************
This is a CRITICAL operation, should be done on all nodes in the
cluster. Cav server does NOT synchronize these changes with the
nodes on which this operation is not executed or failed, please
ensure this operation is executed on all nodes in the cluster.
****************************************************************

Do you want to continue(y/n)?y
shareKey success on server 0(172.31.3.131)
shareKey success on server 1(172.31.4.209)

使用磁盘和 HSM 之间的 wrapKey 命令测试包装与解包的功能

测试 crypto_user4(用户 ID 7)是否可以解包与 crypto_user3 包装在一起的密钥。

密钥 crypto_user3 和 crypto_user_4 具有相同的加密方式,分别用于执行包装和解包操作。

Command:  unWrapKey -f wrapped.key -w 786523

        Cfm3UnWrapKey returned: 0x00 : HSM Return: SUCCESS

        Key Unwrapped.  Key Handle: 1048669

        Cluster Error Status
        Node id 3 and err state 0x00000000 : HSM Return: SUCCESS
        Node id 4 and err state 0x00000000 : HSM Return: SUCCESS

相关信息

激活 CloudHSM 集群

AWS 官方
AWS 官方已更新 3 年前