Skip to content

Commit

Permalink
Refactor rnp_op_verify_st.signatures to vector instead of pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Sep 15, 2023
1 parent c813be1 commit 08a6330
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
7 changes: 3 additions & 4 deletions src/lib/ffi-priv-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,9 @@ struct rnp_op_verify_st {
rnp_output_t output{};
rnp_ctx_t rnpctx{};
/* these fields are filled after operation execution */
rnp_op_verify_signature_t signatures{};
size_t signature_count{};
std::string filename;
uint32_t file_mtime{};
std::vector<rnp_op_verify_signature_st> signatures_;
std::string filename;
uint32_t file_mtime{};
/* encryption information */
bool encrypted{};
bool mdc{};
Expand Down
56 changes: 24 additions & 32 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3021,35 +3021,28 @@ rnp_op_verify_on_signatures(const std::vector<pgp_signature_info_t> &sigs, void

try {
/* in case we have multiple signed layers */
delete[] op->signatures;
op->signatures = new rnp_op_verify_signature_st[sigs.size()];
} catch (const std::exception &e) {
FFI_LOG(op->ffi, "%s", e.what());
return;
}
op->signature_count = sigs.size();

size_t i = 0;
for (const auto &sinfo : sigs) {
rnp_op_verify_signature_t res = &op->signatures[i++];
/* sinfo.sig may be NULL */
if (sinfo.sig) {
try {
res->sig_pkt = *sinfo.sig;
} catch (const std::exception &e) {
FFI_LOG(op->ffi, "%s", e.what());
op->signatures_.resize(sigs.size());

size_t i = 0;
for (const auto &sinfo : sigs) {
auto &res = op->signatures_[i++];
/* sinfo.sig may be NULL */
if (sinfo.sig) {
res.sig_pkt = *sinfo.sig;
}
}

if (sinfo.unknown) {
res->verify_status = RNP_ERROR_SIGNATURE_UNKNOWN;
} else if (sinfo.valid) {
res->verify_status = sinfo.expired ? RNP_ERROR_SIGNATURE_EXPIRED : RNP_SUCCESS;
} else {
res->verify_status =
sinfo.no_signer ? RNP_ERROR_KEY_NOT_FOUND : RNP_ERROR_SIGNATURE_INVALID;
if (sinfo.unknown) {
res.verify_status = RNP_ERROR_SIGNATURE_UNKNOWN;
} else if (sinfo.valid) {
res.verify_status = sinfo.expired ? RNP_ERROR_SIGNATURE_EXPIRED : RNP_SUCCESS;
} else {
res.verify_status =
sinfo.no_signer ? RNP_ERROR_KEY_NOT_FOUND : RNP_ERROR_SIGNATURE_INVALID;
}
res.ffi = op->ffi;
}
res->ffi = op->ffi;
} catch (const std::exception &e) {
FFI_LOG(op->ffi, "%s", e.what());
}
}

Expand Down Expand Up @@ -3301,8 +3294,8 @@ try {
}
/* Allow to require all signatures be valid */
if (op->require_all_sigs && !ret) {
for (size_t i = 0; i < op->signature_count; i++) {
if (op->signatures[i].verify_status) {
for (auto &sig : op->signatures_) {
if (sig.verify_status) {
ret = RNP_ERROR_SIGNATURE_INVALID;
break;
}
Expand All @@ -3323,7 +3316,7 @@ try {
return RNP_ERROR_NULL_POINTER;
}

*count = op->signature_count;
*count = op->signatures_.size();
return RNP_SUCCESS;
}
FFI_GUARD
Expand All @@ -3334,11 +3327,11 @@ try {
if (!op || !sig) {
return RNP_ERROR_NULL_POINTER;
}
if (idx >= op->signature_count) {
if (idx >= op->signatures_.size()) {
FFI_LOG(op->ffi, "Invalid signature index: %zu", idx);
return RNP_ERROR_BAD_PARAMETERS;
}
*sig = &op->signatures[idx];
*sig = &op->signatures_[idx];
return RNP_SUCCESS;
}
FFI_GUARD
Expand Down Expand Up @@ -3568,7 +3561,6 @@ FFI_GUARD

rnp_op_verify_st::~rnp_op_verify_st()
{
delete[] signatures;
delete used_recipient;
delete used_symenc;
}
Expand Down

0 comments on commit 08a6330

Please sign in to comment.