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

Fix armored stream detection and refactor pgp_source_t. #2155

Merged
merged 11 commits into from
Dec 6, 2023
Merged
2 changes: 1 addition & 1 deletion src/fuzzing/keyring_g10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)

init_mem_src(&memsrc, data, size, false);
ks.load_g10(memsrc);
src_close(&memsrc);
memsrc.close();

return 0;
}
24 changes: 13 additions & 11 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1624,23 +1624,23 @@ rnp_input_dearmor_if_needed(rnp_input_t input, bool noheaders = false)
bool require_armor = false;
/* check whether we already have armored stream */
if (input->src.type == PGP_STREAM_ARMORED) {
if (!src_eof(&input->src)) {
if (!input->src.eof()) {
/* be ready for the case of damaged armoring */
return src_error(&input->src) ? RNP_ERROR_READ : RNP_SUCCESS;
return input->src.error() ? RNP_ERROR_READ : RNP_SUCCESS;
}
/* eof - probably next we have another armored message */
src_close(&input->src);
input->src.close();
rnp_input_st *base = (rnp_input_st *) input->app_ctx;
*input = std::move(*base);
delete base;
/* we should not mix armored data with binary */
require_armor = true;
}
if (src_eof(&input->src)) {
if (input->src.eof()) {
return RNP_ERROR_EOF;
}
/* check whether input is armored only if base64 is not forced */
if (!noheaders && !is_armored_source(&input->src)) {
if (!noheaders && !input->src.is_armored()) {
return require_armor ? RNP_ERROR_BAD_FORMAT : RNP_SUCCESS;
}

Expand Down Expand Up @@ -1713,7 +1713,7 @@ try {
rnp::KeyStore tmp_store(PGP_KEY_STORE_GPG, "", ffi->context);

/* check whether input is base64 */
if (base64 && is_base64_source(input->src)) {
if (base64 && input->src.is_base64()) {
ret = rnp_input_dearmor_if_needed(input, true);
if (ret) {
return ret;
Expand Down Expand Up @@ -2013,7 +2013,7 @@ rnp_input_st::rnp_input_st() : reader(NULL), closer(NULL), app_ctx(NULL)
rnp_input_st &
rnp_input_st::operator=(rnp_input_st &&input)
{
src_close(&src);
src.close();
src = std::move(input.src);
memset(&input.src, 0, sizeof(input.src));
reader = input.reader;
Expand All @@ -2029,7 +2029,7 @@ rnp_input_st::operator=(rnp_input_st &&input)
rnp_input_st::~rnp_input_st()
{
bool armored = src.type == PGP_STREAM_ARMORED;
src_close(&src);
src.close();
if (armored) {
rnp_input_t armored = (rnp_input_t) app_ctx;
delete armored;
Expand Down Expand Up @@ -2152,8 +2152,8 @@ try {
return RNP_ERROR_OUT_OF_MEMORY;
}
src->param = obj;
src->read = input_reader_bounce;
src->close = input_closer_bounce;
src->raw_read = input_reader_bounce;
src->raw_close = input_closer_bounce;
src->type = PGP_STREAM_MEMORY;
*input = obj;
return RNP_SUCCESS;
Expand Down Expand Up @@ -8409,7 +8409,9 @@ try {
}

pgp_armored_msg_t msgtype = PGP_ARMORED_UNKNOWN;
if (is_armored_source(&input->src)) {
if (input->src.is_cleartext()) {
msgtype = PGP_ARMORED_CLEARTEXT;
} else if (input->src.is_armored()) {
msgtype = rnp_armored_get_type(&input->src);
} else {
msgtype = rnp_armor_guess_type(&input->src);
Expand Down
4 changes: 2 additions & 2 deletions src/librekey/rnp_key_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ KeyStore::load(const KeyProvider *key_provider)
if (!load_g10(src, key_provider)) {
RNP_LOG("Can't parse file: %s", apath.c_str()); // TODO: %S ?
}
src_close(&src);
src.close();
}
rnp_closedir(dir);
return true;
Expand All @@ -96,7 +96,7 @@ KeyStore::load(const KeyProvider *key_provider)
}

bool rc = load(src, key_provider);
src_close(&src);
src.close();
return rc;
}

Expand Down
Loading