Skip to content

fix #803 - rsa_import_pkcs1: reject unknown versions - #814

Open
dlazobnaKocka wants to merge 1 commit into
libtom:developfrom
dlazobnaKocka:fix-rsa-import-unknown-version-803
Open

dlazobnaKocka wants to merge 1 commit into
libtom:developfrom
dlazobnaKocka:fix-rsa-import-unknown-version-803

Conversation

@dlazobnaKocka

Copy link
Copy Markdown

Fixes #803

rsa_import_pkcs1() could return CRYPT_OK without decoding any key components. There were two ways to get there:

  1. An RSAPrivateKey with a version other than 0 or 1. Neither branch matched, so the function fell through to err = CRYPT_OK. This is the case reported in RSA key import can succeed with an invalid/undefined version #803.
  2. A SEQUENCE that only contains the version INTEGER, for example 30 03 02 01 00. The first decode then returns CRYPT_OK instead of CRYPT_INPUT_TOO_LONG, and the "couldn't decode the version" check passes that CRYPT_OK straight back to the caller.

In both cases rsa_import() reported success with an empty modulus, and key->type was never set.

Changes:

  • Treat CRYPT_OK from the version-only decode like CRYPT_INPUT_TOO_LONG, so the input goes through the normal version check. A version-only SEQUENCE then fails in the full private key decode.
  • Turn the version == 1 branch into a plain else, so version 1 (multi-prime) and any unknown version both return CRYPT_PK_INVALID_TYPE.

The else branch suggested in #803 covers case 1 on its own. Case 2 needs the first change too.

Tests: added s_rsa_issue_803() to tests/rsa_test.c, which imports both inputs and expects an error. Without the fix, the test fails on the first import. With only the else branch, it fails on the second import. With the full fix, test-ltc passes with LTM, and helper.pl --check-all is clean.

Thanks to @headscott for the report and the suggested fix in #803.

rsa_import_pkcs1() returned CRYPT_OK without decoding any key
components in two cases:

- the RSAPrivateKey version was not 0 or 1
- the SEQUENCE contained only the version INTEGER

Reject both with an error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RSA key import can succeed with an invalid/undefined version

1 participant