|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +page_title: Configure transit secret engine to use HSM managed keys |
| 4 | +description: >- |
| 5 | + Understand how to configure vault to use keys present in PKCS #11 based HSM for crypto-operations. |
| 6 | +--- |
| 7 | + |
| 8 | +# Configure transit secrets engine to use PKCS 11 HSM using managed keys |
| 9 | + |
| 10 | +@include 'alerts/enterprise-only.mdx' |
| 11 | + |
| 12 | +Transit secrets engine can be pluged into Vault's centralised abstraction layer called [managed keys](/vault/docs/enterprise/managed-keys) to delegate |
| 13 | +crypto-operations operations to a trusted external KMS or HSM. |
| 14 | +This allows users to leverage key management systems external to Vault for handling, storing, and interacting |
| 15 | +with private key material. |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +To setup transit secrets engine backed by PKCS 11 based HSM, you need: |
| 20 | + |
| 21 | +- A Vault Enterprise server running. |
| 22 | +- A PKCS 11 based HSM. |
| 23 | + |
| 24 | +## Vault setup |
| 25 | + |
| 26 | +1. Configure managed key. |
| 27 | + |
| 28 | + A PKCS#11 HSM requires a shared library to configure Vault to communication with the HSM. |
| 29 | + To declare this library, edit the Vault server configuration to add a [`kms_library` stanza](/vault/docs/configuration/kms-library) |
| 30 | + |
| 31 | + <CodeBlockConfig filename="config-vault.hcl"> |
| 32 | + |
| 33 | + ```hcl |
| 34 | + kms_library "pkcs11" { |
| 35 | + name = "myhsm" |
| 36 | + library = "/usr/vault/lib/libCryptoki2_64.so" |
| 37 | + } |
| 38 | + ``` |
| 39 | + |
| 40 | + </CodeBlockConfig> |
| 41 | + |
| 42 | +1. Use PKCS 11 with IBM EP11 tokens. |
| 43 | + |
| 44 | + <Note> |
| 45 | + |
| 46 | + User needs to have Crypto Express adapters enabled in the LinuxOne and Linux on IBM Z instances as well as configure to use EP11 token. |
| 47 | + User also needs to install opencryptoki which can then talk to EP11 token. For more details, refer to the [IBM EP11 Token](https://www.ibm.com/docs/en/linux-on-systems?topic=support-exploiting-enterprise-pkcs-11-using-opencryptoki) page. |
| 48 | + |
| 49 | + </Note> |
| 50 | + |
| 51 | + ```hcl |
| 52 | + kms_library "pkcs11" { |
| 53 | + name = "myhsm" |
| 54 | + library = "/usr/local/lib/opencryptoki/libopencryptoki.so" |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | +1. Restart or reload Vault's configuration with `SIGHUP`. |
| 59 | + |
| 60 | + ```shell-session |
| 61 | + $ kill -HUP $(pidof vault) |
| 62 | + ``` |
| 63 | +1. Each managed key requires type-specific configuration. You must identify the location of a key and PIN to access the HSM. This is very similar to Vault's PKCS#11 auto-unseal mechanism. Configure the managed key. |
| 64 | + |
| 65 | + ```shell-session |
| 66 | + $ vault write sys/managed-keys/pkcs11/transit-rsa-key \ |
| 67 | + library=myhsm \ |
| 68 | + slot=4 \ |
| 69 | + pin=12345678 \ |
| 70 | + key_label="vault-rsa-key" \ |
| 71 | + allow_generate_key=true \ |
| 72 | + mechanism=0x0001 \ |
| 73 | + allow_store_key=true \ |
| 74 | + key_bits=2048 \ |
| 75 | + any_mount=false |
| 76 | + ``` |
| 77 | + |
| 78 | + The PKCS#11 specific parameters are `library`, referring to the previously |
| 79 | + configured `kms_library` stanza, `slot`, `pin`, `key_label`, and `mechanism`, |
| 80 | + which identifies the object in the HSM which will hold the key. Its |
| 81 | + mechanism is `CKM_RSA_PKCS` (RSA with PKCS#11 v1.5 signatures), and that will |
| 82 | + require a 2048 bit key. The `allow_generate_key` flag indicates that Vault is |
| 83 | + allowed to request that the HSM generate a key. The `allow_store_key` parameter |
| 84 | + indicates that a new key may be stored in the backend. Without this flag, you |
| 85 | + may configure the key yourself in the HSM and just point Vault at the result. |
| 86 | + The `any_mount` means any mount in the namespace may access the managed |
| 87 | + key. For this example, it is set to false so that you can demonstrate how to |
| 88 | + lock managed key access down to a specific mount. |
| 89 | + |
| 90 | + **Example output:** |
| 91 | + |
| 92 | + <CodeBlockConfig hideClipboard> |
| 93 | + |
| 94 | + ```plaintext |
| 95 | + Success! Data written to: sys/managed-keys/pkcs11/transit-rsa-key |
| 96 | + ``` |
| 97 | + |
| 98 | + </CodeBlockConfig> |
| 99 | + |
| 100 | +1. Read the key back to verify if named managed key was created successfully. |
| 101 | + |
| 102 | + ```shell-session |
| 103 | + $ vault read sys/managed-keys/pkcs11/transit-rsa-key |
| 104 | +
|
| 105 | + Key Value |
| 106 | + --- ----- |
| 107 | + UUID fe8f3c8c-8f11-571d-b4ce-411314fcaf98 |
| 108 | + allow_generate_key true |
| 109 | + allow_replace_key false |
| 110 | + allow_store_key true |
| 111 | + any_mount false |
| 112 | + key_bits 2048 |
| 113 | + key_label vault-rsa-key |
| 114 | + library myhsm |
| 115 | + mechanism 1 |
| 116 | + name transit-rsa-key |
| 117 | + pin redacted |
| 118 | + slot 4 |
| 119 | + type pkcs11 |
| 120 | + usages [3 4] |
| 121 | + ``` |
| 122 | + |
| 123 | +## Configure transit secrets engine |
| 124 | + |
| 125 | +1. Enable the `transit` secrets engine at the `transit` path. |
| 126 | + |
| 127 | + ```shell-session |
| 128 | + $ vault secrets enable transit |
| 129 | + Success! Enabled the transit secrets engine at: transit/ |
| 130 | + ``` |
| 131 | + |
| 132 | +1. Tune the secrets engine to use managed keys. |
| 133 | + |
| 134 | + ```shell-session |
| 135 | + $ vault secrets tune --allowed-managed-keys=transit-rsa-key transit |
| 136 | + Success! Tuned the secrets engine at: transit/ |
| 137 | + ``` |
| 138 | + |
| 139 | + You configured the managed key with `any_mount=false` in the previous step. |
| 140 | + This command grants access to the `transit-rsa-key` to the Transit secrets |
| 141 | + engine's mount. |
| 142 | + |
| 143 | +1. Create a transit key. |
| 144 | + |
| 145 | + ```shell-session |
| 146 | + $ vault write transit/keys/hsm-transit-key \ |
| 147 | + type=managed_key \ |
| 148 | + managed_key_name=transit-rsa-key \ |
| 149 | + allow_plaintext_backup=false \ |
| 150 | + exportable=false |
| 151 | + ``` |
| 152 | + |
| 153 | + **Example output:** |
| 154 | + |
| 155 | + <CodeBlockConfig hideClipboard> |
| 156 | + |
| 157 | + ```text |
| 158 | + Key Value |
| 159 | + --- ----- |
| 160 | + allow_plaintext_backup false |
| 161 | + auto_rotate_period 0s |
| 162 | + deletion_allowed false |
| 163 | + derived false |
| 164 | + exportable false |
| 165 | + imported_key false |
| 166 | + latest_version 1 |
| 167 | + min_available_version 0 |
| 168 | + min_decryption_version 1 |
| 169 | + min_encryption_version 0 |
| 170 | + name hsm-transit-key |
| 171 | + supports_decryption true |
| 172 | + supports_derivation false |
| 173 | + supports_encryption true |
| 174 | + supports_signing true |
| 175 | + type managed_key |
| 176 | + ``` |
| 177 | + |
| 178 | + </CodeBlockConfig> |
| 179 | + |
| 180 | + At this step while creating transit key, RSA 2048 public private key pair also gets created in the HSM. The key label is `vault-rsa-key`. |
| 181 | + |
| 182 | +## Perform crypto-operation |
| 183 | + |
| 184 | +1. Perform the sign data operation. |
| 185 | + |
| 186 | + ```shell-session |
| 187 | + $ vault write transit/sign/hsm-transit-key \ |
| 188 | + input=$(echo -n "data to sign" | base64) \ |
| 189 | + hash_algorithm=sha2-256 |
| 190 | + ``` |
| 191 | + |
| 192 | + **Example output:** |
| 193 | + |
| 194 | + <CodeBlockConfig hideClipboard> |
| 195 | + |
| 196 | + ```text |
| 197 | + Key Value |
| 198 | + --- ----- |
| 199 | + key_version 1 |
| 200 | + signature vault:v1:bqLeknlTuvrPt99ubS0CZyDs5iG8q9n+7kLG+2qHmg1xcHxUdKYCvtIUO5ZpCjJ6SUA/cTJgeFqdhK5rOcSfTzhU5B+HABEYeVb+H7yQ4+sLWrJQ5fNAQZCly+AiXM4q2lAvMW651oq0D4R86f0tOUHVjsaQyqhrrtaA1GQoIcNikx4llTXiGRmc3A1dZmpFCpG7ejQuUF8mGj1tFmY/dyrV0V9o58rmqGe/ddfg+bAfpewNcqWPoWtB+o9QuFhw3L8eqxVCbF8lGfZlqJmudep13So4bC03bWdlZBET8nYpX51Bi5yz0C9wth666IiqE61JnPBlmcJFEyd3FCJ4yQ== |
| 201 | + ``` |
| 202 | + |
| 203 | + </CodeBlockConfig> |
| 204 | + |
| 205 | +2. Verify signed data. |
| 206 | + |
| 207 | + ```shell-session |
| 208 | + $ vault write transit/verify/hsm-transit-key \ |
| 209 | + input=$(echo -n "data to sign" | base64) \ |
| 210 | + hash_algorithm=sha2-256 \ |
| 211 | + signature="vault:v1:..." |
| 212 | + ``` |
| 213 | + |
| 214 | + The signature is the output of the sign command. |
| 215 | + |
| 216 | + **Example output:** |
| 217 | + |
| 218 | + <CodeBlockConfig hideClipboard> |
| 219 | + |
| 220 | + ```shell-session |
| 221 | + Key Value |
| 222 | + --- ----- |
| 223 | + valid true |
| 224 | + ``` |
| 225 | + |
| 226 | + </CodeBlockConfig> |
| 227 | + |
| 228 | + |
| 229 | +## Reference |
| 230 | + |
| 231 | +- [Encrypt data in transit with Vault](/vault/tutorials/encryption-as-a-service/eaas-transit) |
| 232 | +- [Transit secrets engine](/vault/docs/secrets/transit) |
| 233 | + |
| 234 | +## API |
| 235 | + |
| 236 | +- [Transit secrets engine (API)](/vault/api-docs/secret/transit) |
| 237 | +- [`/sys/managed-keys`](/vault/api-docs/system/managed-keys) endpoint |
0 commit comments