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

YH_MSG_BUF_SIZE too small to import a wrapped RSA:4096 key #61

Closed
robn opened this issue Nov 7, 2019 · 5 comments
Closed

YH_MSG_BUF_SIZE too small to import a wrapped RSA:4096 key #61

robn opened this issue Nov 7, 2019 · 5 comments

Comments

@robn
Copy link
Contributor

robn commented Nov 7, 2019

I've created a RSA:4096 key and exported it as follows:

# create decrypt key
yubihsm-shell \
  --authkey=1 --password=password \
  --action=generate-asymmetric-key \
    --object-id=0x10 \
    --label=decrypt_key \
    --capabilities=decrypt-oaep,sign-pkcs,exportable-under-wrap \
    --algorithm=rsa4096

# create wrap key
echo 00000000000000000000000000000000 | yubihsm-shell \
  --authkey=1 --password=password \
  --action=put-wrap-key \
    --object-id=0x30 \
    --label=export_wrapkey \
    --capabilities=export-wrapped,import-wrapped \
    --delegated=decrypt-oaep,sign-pkcs,exportable-under-wrap

# export decrypt key under wrap
yubihsm-shell \
  --authkey=1 --password=password \
  --action=get-wrapped \
  --wrap-id=0x30 \
  --object-id=0x10 \
  --object-type=asymmetric-key \
  --out=/tmpfs/decrypt_key

# delete the decrypt key (making room for import)
yubihsm-shell \
  --authkey=1 --password=password \
  --action=delete-object \
  --object-id=0x10 \
  --object-type=asymmetric-key
# ls -l /tmpfs/decrypt_key
-rw-r--r-- 1 root root 5016 Nov  7 16:11 /tmpfs/decrypt_key

With #60 in place to allow this file to be loaded, I attempt to import:

# yubihsm-shell \
  --authkey=1 --password=password \
  --action=put-wrapped \
  --wrap-id=0x30 \
  --in=file:/tmpfs/decrypt_key
Using default connector URL: http://127.0.0.1:12345
Session keepalive set up to run every 15 seconds
Created session 0
Failed to store wrapped object: Invalid argument to a function
Unable to store wrapped object

Tracing the code, we see we land in this error condition in yh_util_import_wrapped:

  if (in_len > YH_MSG_BUF_SIZE - 2) {
    DBG_ERR("Too much data, must be < %d", YH_MSG_BUF_SIZE - 2);
    return YHR_INVALID_PARAMETERS;
  }

Here in_len is 3761 bytes, but YH_MSG_BUF_SIZE is 2048.

Reading further, it looks like YH_MSG_BUF_SIZE is deeply wired into USB message sizes, so naively increasing it is probably not going to work. Is that right?

Any ideas for how to import this key?

@a-dma
Copy link
Member

a-dma commented Nov 8, 2019

Hi, judging by the size of your file (5016 bytes), I'm guessing that what happened is that you've run the command a few times and ended up with two keys in the same file. yubihsm-shell appends the result to the output file and it's not clever enough to realize that it's two different blobs. If you manually delete half of your file (or just remove it all and export again), it should work.

As for the size of the buffer, as you've guessed it's not just an arbitrary limit in the code, the device itself can't read messages longer than 2kB.

@robn
Copy link
Contributor Author

robn commented Nov 8, 2019

Wow, that was it.

Not gonna lie, that's incredibly frustrating: I have spent hours on this today reading and tracing yubihsm-shell and even working through the USB packets to understand a series of apparent bugs that can all be put down to this. Is there a reason why it appends instead of either overwriting or failing (like most other Unix tools, I'd wager)? If I worked up a patch to make it overwrite or fail (or prompt, if I can work that out), would you be interested?

Either way, thanks.

@a-dma
Copy link
Member

a-dma commented Nov 8, 2019

Sorry to hear that you've wasted time on this.
Yes, I agree that appending is somewhat surprising, but for other formats (like PEM) it can make sense.

Truncating would be even more surprising in that regards, as you may have valuable data in that file. I think that leaves failing as the only sane alternative.

(I'll close the issue since it's resolved, but feel free to comment more).

@a-dma a-dma closed this as completed Nov 8, 2019
@robn
Copy link
Contributor Author

robn commented Nov 9, 2019

It could perhaps be less surprising if it only appends on output formats that can handle it, like PEM. For everything else maybe just abort with an error. It makes it clear what's happening.

@a-dma
Copy link
Member

a-dma commented Nov 11, 2019

I have created #63 to track this problem.

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