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

Unable to import an asymmetric wrapping key? #371

Open
mouse07410 opened this issue Nov 22, 2023 · 6 comments
Open

Unable to import an asymmetric wrapping key? #371

mouse07410 opened this issue Nov 22, 2023 · 6 comments

Comments

@mouse07410
Copy link
Contributor

Two YubiHSM2 devices with firmware 2.0.0 and 2.2.0. Latest yubihsm-shell master.

Goal:

  1. generate an RSA-2048 (for testing - the production will be RSA-3072) key-pair elsewhere (TPMv2, if it matters) and output its public part in PEM format (say, named k1.pem);
  2. import its public key (say, named tpmwrap) into HSM2;
  3. generate symmetric (data-encrypting) and asymmetric (some for key exchange, and some - for digital signature) keys, export them wrapped under tpmwrap key;
  4. import into TPM those exported/wrapped keys.

Step 1 succeeds (obviously) - generating the key and outputting k1.pem works.

Step 2 fails with Unable to read asymmetric key.

OpenSSL-3.1.4 believes the key is OK:

$ openssl rsa -pubin -text -in k1.pem
Public-Key: (2048 bit)
Modulus:
    00:aa:e7:f7:71:af:c9:e9:0a:1c:62:95:2b:b7:ee:
    16:6c:9d:3e:d9:da:71:83:58:3c:34:ec:34:8b:3b:
    25:f8:1b:3b:af:0d:20:5e:ef:cb:ea:b1:9c:c8:0d:
    97:d6:5a:ec:ca:0e:96:1b:c1:f9:ff:e9:f5:7f:c3:
    fc:ef:47:0a:a3:8d:a2:01:e1:b1:3b:08:96:26:9a:
    e4:c7:7a:0f:4f:ec:db:c8:d1:8b:d6:01:92:e0:cb:
    7c:1c:65:5f:4c:d4:65:be:4b:62:2f:b8:11:82:af:
    0e:b7:a6:27:5d:17:cb:a5:03:13:43:8f:8b:53:8d:
    ba:14:c6:7c:fe:f6:5e:75:50:29:26:09:ab:77:96:
    1f:6f:e0:b3:e9:90:09:d9:2d:29:5a:49:61:04:2e:
    cd:ac:b1:dc:d0:0f:91:1f:c3:e0:8d:1f:c2:3a:d6:
    64:3b:90:8b:41:e9:b1:bf:6e:f8:51:6e:82:85:f6:
    d5:78:f9:99:d9:71:80:d5:de:08:da:3f:08:5f:d5:
    ee:06:c9:da:70:11:ee:80:40:c4:6c:65:d5:52:9e:
    28:9e:90:77:3e:d7:f9:ce:df:75:89:d7:4a:63:c5:
    70:01:2b:9c:c8:87:cd:91:1e:cc:46:fe:51:ea:29:
    cc:a0:96:19:d9:69:26:76:38:1a:07:a5:3d:8f:83:
    f4:fb
Exponent: 65537 (0x10001)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAquf3ca/J6QocYpUrt+4W
bJ0+2dpxg1g8NOw0izsl+Bs7rw0gXu/L6rGcyA2X1lrsyg6WG8H5/+n1f8P870cK
o42iAeGxOwiWJprkx3oPT+zbyNGL1gGS4Mt8HGVfTNRlvktiL7gRgq8Ot6YnXRfL
pQMTQ4+LU426FMZ8/vZedVApJgmrd5Yfb+Cz6ZAJ2S0pWklhBC7NrLHc0A+RH8Pg
jR/COtZkO5CLQemxv274UW6ChfbVePmZ2XGA1d4I2j8IX9XuBsnacBHugEDEbGXV
Up4onpB3Ptf5zt91iddKY8VwASucyIfNkR7MRv5R6inMoJYZ2WkmdjgaB6U9j4P0
+wIDAQAB
-----END PUBLIC KEY-----
$ 

Here's my command, and what YubiHSM2 tells me:

yubihsm> put asymmetric 0 0xea14 asymtmpwrap 1,2,3,4,5,6,7,8 decrypt-oaep,import-wrapped,export-wrapped,exportable-under-wrap,wrap-data,unwrap-data decrypt-oaep,import-wrapped,export-wrapped,exportable-under-wrap k1.pem
Unable to read asymmetric key
yubihsm> 

Documentation did not help much.

Please help.

@aveenismail
Copy link
Member

aveenismail commented Nov 22, 2023

Hi @mouse07410

I see few issues here:

1- It looks like you are trying to import an asymmetric key and use it as a wrap key. This is not supported by the YubiHSM2. Wrapkeys are separate objects with symmetric properties. Asymmetric keys can only be used for signing, decryption and derivation of ECDH keys.

2- The command have 2 issues that I can immediately see:
a. There is a delegated capabilities parameters, which put asymmetric is not expecting
b. It says in the ticket that k1.pem contains the public key of the RSA key. put asymmetric expects the private key as an input, not the public key.

I hope this helps

@mouse07410
Copy link
Contributor Author

@aveenismail thank you! Yes it helps, but here are some questions:

Wrapkeys are separate objects with symmetric properties. Asymmetric keys can only be used for signing, decryption and derivation of ECDH keys.

How can I get "wrapkey" functionality - securely export an object, such as symmetric key or asymmetric key-pair, from the HSM, and (securely) import it into a different device (another HSM, or TPM, or such)?

It says in the ticket that k1.pem contains the public key of the RSA key. put asymmetric expects the private key as an input, not the public key.

Does it mean there's no way to import a public key into HSM, and one would have to perform all the operations with it in software (on the host)? I'm trying to find a way to accomplish "wrapping" with asymmetric, as described above.

@aveenismail
Copy link
Member

1-
A wrap key can either be generated in the YubiHSM or imported into it. If you plan to import the wrapped objects into another YubiHSM, the same wrap key needs to be in both YubiHSMs. For that reason, I would suggest importing a wrap key. See https://developers.yubico.com/YubiHSM2/Commands/Put_Wrap_Key.html for a command example. This wrap key should have the export-wrapped and import-wrapped capabilities and as delegated capabilities, whatever capabilities the objects it's wrapping/unwrapping have. Also, the objects to wrap need to have the capability exportable-under-wrap otherwise the operation won't succeed.

Use the Export Wrapped command to export objects under wrap (https://developers.yubico.com/YubiHSM2/Commands/Export_Wrapped.html). Then use the Import Wrapped command to import the wrapped object into the other YubiHSM (https://developers.yubico.com/YubiHSM2/Commands/Import_Wrapped.html)

2-
That is correct. Public keys are not imported into the YubiHSM but they can be extracted from it. Only private key operations are performed inside the YubiHSM. Public key operations need to be preformed using software outside the YubiHSM. Asymmetric wrap of objects inside the YubiHSM is not supported

@mouse07410
Copy link
Contributor Author

If you plan to import the wrapped objects into another YubiHSM, the same wrap key needs to be in both YubiHSMs

I want to exchange wrapped objects between YubiHSM2 and a non-Yubico device(s) such as TPM. My preference is to keep the "unencrypted" objects only inside those hardware modules - which makes exporting an unencrypted key (even if HSM would allow it, which I doubt) and encrypting it on the host in software - undesirable. I guess, for that I'll need the exact format of the wrapped structure...

Re. asummetric wrap - please pass it to the leadership as a feature request.

@aveenismail
Copy link
Member

You are correct in that private keys cannot be exported from the YubiHSM unless they are wrapped. While Yubico does not provide a softwrare to unwrap a wrapped objects outside a YubiHSM, the yubihsm-wrap tool (part of this repository) does provide a way to wrap objects outside of a YubiHSM so they can be unwraped inside one. Maybe that can be a helpful reference.

Asymmetric wrap feature request has been forwarded to the product team.

@mouse07410
Copy link
Contributor Author

mouse07410 commented Nov 28, 2023

yubihsm-wrap tool (part of this repository) does provide a way to wrap objects outside of a YubiHSM so they can be unwraped inside one

Thank you - but I also need the reverse: wrap within the YubiHSM, and unwrap inside a TMP or another hardware module (not YubiHSM).

Asymmetric wrap feature request has been forwarded to the product team.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants