Skip to content

Commit

Permalink
Update test for rnp_locate_key() with some edge cases and fix possibl…
Browse files Browse the repository at this point in the history
…e issue with hex decoding.
  • Loading branch information
ni4 authored and ronaldtse committed Mar 18, 2024
1 parent 7165af6 commit 54e6178
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/crypto/mem_ossl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ hex_decode(const char *hex, uint8_t *buf, size_t buf_len)
hex++;
continue;
}
if (hexlen < 2) {
/* We assume that spaces/tabs divide hex string between even groups of hex chars */
if (hex + 2 > end) {
RNP_LOG("Invalid hex string length.");
return 0;
}
Expand Down
28 changes: 28 additions & 0 deletions src/tests/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,24 @@ TEST_F(rnp_tests, test_ffi_locate_key)
// load our keyrings
assert_true(load_keys_gpg(ffi, "data/keyrings/1/pubring.gpg"));

// edge cases
{
rnp_key_handle_t key = NULL;
assert_rnp_failure(rnp_locate_key(NULL, "keyid", "7BC6709B15C23A4A", &key));
assert_rnp_failure(rnp_locate_key(ffi, NULL, "7BC6709B15C23A4A", &key));
assert_rnp_failure(rnp_locate_key(ffi, "keyid", NULL, &key));
assert_rnp_failure(rnp_locate_key(ffi, "keyid", "7BC6709B15C23A4A", NULL));
assert_rnp_failure(rnp_locate_key(ffi, "wrong", "7BC6709B15C23A4A", &key));
assert_rnp_failure(rnp_locate_key(ffi, "keyid", "C6709B15C23A4A", &key));
assert_rnp_failure(
rnp_locate_key(ffi, "fingerprint", "5A3CBF583AA80A2CCC53AA7BC6709B15C23A4A", &key));
assert_rnp_failure(
rnp_locate_key(ffi, "grip", "D6A0800A3FACDE0C0EB60B16B3669ED380FDFA", &key));
assert_rnp_failure(rnp_locate_key(ffi, "keyid", "0x7BC6 709B\r15C2 3A4A\n", &key));
assert_rnp_success(rnp_locate_key(ffi, "keyid", "0x7BC6 709B\t15C2 3A4A\t", &key));
assert_non_null(key);
rnp_key_handle_destroy(key);
}
// keyid
{
static const char *ids[] = {"7BC6709B15C23A4A",
Expand Down Expand Up @@ -6018,3 +6036,13 @@ TEST_F(rnp_tests, test_result_to_string)
}
}
}

TEST_F(rnp_tests, test_ffi_wrong_hex_length)
{
rnp_ffi_t ffi = NULL;
assert_rnp_success(rnp_ffi_create(&ffi, "GPG", "GPG"));
rnp_key_handle_t key = NULL;
assert_rnp_failure(rnp_locate_key(ffi, "keyid", "BC6709B15C23A4A", &key));
assert_rnp_failure(rnp_locate_key(ffi, "keyid", "C6709B15C23A4A", &key));
rnp_ffi_destroy(ffi);
}

0 comments on commit 54e6178

Please sign in to comment.