diff --git a/src/pk/rsa/rsa_import.c b/src/pk/rsa/rsa_import.c index bc201ad97..607ab24a2 100644 --- a/src/pk/rsa/rsa_import.c +++ b/src/pk/rsa/rsa_import.c @@ -49,7 +49,7 @@ int rsa_import_pkcs1(const unsigned char *in, unsigned long inlen, rsa_key *key) key->type = PK_PUBLIC; } goto LBL_OUT; - } else if (err != CRYPT_INPUT_TOO_LONG) { + } else if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) { /* couldn't decode the version, so error out */ goto LBL_OUT; } @@ -70,8 +70,9 @@ int rsa_import_pkcs1(const unsigned char *in, unsigned long inlen, rsa_key *key) goto LBL_OUT; } key->type = PK_PRIVATE; - } else if (version == 1) { - /* we don't support multi-prime RSA */ + } else { + /* version 1 is multi-prime RSA, which we don't support, + * any other version is invalid */ err = CRYPT_PK_INVALID_TYPE; goto LBL_OUT; } diff --git a/tests/rsa_test.c b/tests/rsa_test.c index 45a3aca8e..e983ddd71 100644 --- a/tests/rsa_test.c +++ b/tests/rsa_test.c @@ -376,6 +376,19 @@ static int s_rsa_issue_301(int prng_idx) return CRYPT_OK; } +static int s_rsa_issue_803(void) +{ + rsa_key key; + /* SEQUENCE { INTEGER 2, INTEGER 0 }, an unknown RSAPrivateKey version */ + const unsigned char v2[] = { 0x30, 0x06, 0x02, 0x01, 0x02, 0x02, 0x01, 0x00 }; + /* SEQUENCE { INTEGER 0 }, a version without any key components */ + const unsigned char v0[] = { 0x30, 0x03, 0x02, 0x01, 0x00 }; + + SHOULD_FAIL(rsa_import(v2, sizeof(v2), &key)); + SHOULD_FAIL(rsa_import(v0, sizeof(v0), &key)); + return CRYPT_OK; +} + static int s_rsa_public_ubin_e(int prng_idx) { rsa_key key; @@ -605,6 +618,7 @@ int rsa_test(void) DO(s_rsa_macros_test()); DO(s_rsa_cryptx_issue_69()); DO(s_rsa_issue_301(prng_idx)); + DO(s_rsa_issue_803()); DO(s_rsa_public_ubin_e(prng_idx)); DO(s_rsa_oaep_small_ciphertext_test(prng_idx));