Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export of HMAC key returns the wrong values #20804

Closed
rzimmerman opened this issue May 26, 2023 · 2 comments
Closed

Export of HMAC key returns the wrong values #20804

rzimmerman opened this issue May 26, 2023 · 2 comments
Assignees
Labels
bug Used to indicate a potential bug cryptosec secret/transit

Comments

@rzimmerman
Copy link

Describe the bug
The Transit Export endpoint returns erroneous values for HMAC keys. For Vault-generated keys, the Export endpoint returns random 32-byte values (regardless of the requested key size). For imported keys, the Export endpoint returns a blank (empty) key.

To Reproduce
Steps to reproduce the behavior:

  1. Run vault server -dev -dev-root-token-id root
  2. Run this gist
  3. AES keys return the expected value when imported + exported
  4. HMAC keys that are created are the wrong length (always 32 bytes)
  5. HMAC keys that are imported do not match the imported key

Expected behavior
Per the Gist above, I'd expect the exported key length to match the key_size parameter.
Also, I'd expect the exported HMAC key to match the imported key value. Output on my machine:

> Test create HMAC key of desired length:
Key length: 32, expected 64
> Test import HMAC key:
expected key: 3031323334353637383961626364656630313233343536373839616263646566
got:
> Test import AES key:
expected key: 3031323334353637383961626364656630313233343536373839616263646566
got:          3031323334353637383961626364656630313233343536373839616263646566

And directly querying the endpoints:

➜  vault read transit/export/hmac-key/hmac-test1
Key     Value
---     -----
keys    map[1:Z4DAJFsjCEVRgyjFFqjVcdrwvPhTuPLYiqqrUEFFK0U=]
name    hmac-test1
type    hmac
➜  vault read transit/export/hmac-key/hmac-test2
Key     Value
---     -----
keys    map[1:]
name    hmac-test2
type    hmac

Environment:

  • Vault Server Version (retrieve with vault status): 1.13.2
  • Vault CLI Version (retrieve with vault version): Vault v1.13.2 (b9b773f), built 2023-04-25T13:02:50Z (though this happens with github.com/hashicorp/vault-client-go v0.3.2)
  • Server Operating System/Architecture: MacOS (M1)

Vault server configuration file(s): N/A

Additional context

It looks like the Export endpoint is erroneously using the HMACKey "supplementary" key for hmac key types. This line should probably be returning key.Key rather than key.HMACKey:

case exportTypeHMACKey:
return strings.TrimSpace(base64.StdEncoding.EncodeToString(key.HMACKey)), nil

@rzimmerman rzimmerman changed the title Export of HMAC keys returns the wrong values Export of HMAC key returns the wrong values May 26, 2023
@cipherboy cipherboy added bug Used to indicate a potential bug secret/transit labels May 30, 2023
@cipherboy cipherboy self-assigned this May 30, 2023
@cipherboy
Copy link
Contributor

@rzimmerman Ahhh this makes more sense after playing with the code a little more. I think we shouldn't have kept with separate HMACKey fields on the export. Notably, if you look at the backup, the

There's I think two bugs:

  1. The shadowing of numBytes resulted in the decreased sizes you saw.
  2. The use of an incorrect-and-random HMACKey value (that wasn't used for signing) caused the export discrepancies you saw as well: imported keys didn't get HMAC variants.

I've addressed both of these in #20864, but still need docs and tests for this :-)

Thank you for the detailed reproducer, it was very helpful!

@cipherboy
Copy link
Contributor

This will go out in the next set of releases :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Used to indicate a potential bug cryptosec secret/transit
Projects
None yet
Development

No branches or pull requests

2 participants