Skip to content

Commit

Permalink
Remove redundant fields from the pgp_subsig_t.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Oct 22, 2024
1 parent e7febb0 commit 83d4c18
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
15 changes: 4 additions & 11 deletions src/lib/pgp-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,6 @@ pgp_subsig_t::pgp_subsig_t(const pgp_signature_t &pkt)
{
sig = pkt;
sigid = sig.get_id();
if (sig.has_subpkt(PGP_SIG_SUBPKT_TRUST)) {
trustlevel = sig.trust_level();
trustamount = sig.trust_amount();
}
if (sig.has_subpkt(PGP_SIG_SUBPKT_KEY_FLAGS)) {
key_flags = sig.key_flags();
}
/* add signature rawpacket */
rawpkt = pgp_rawpacket_t(sig);
}
Expand Down Expand Up @@ -2610,11 +2603,11 @@ pgp_key_t::refresh_data(const rnp::SecurityContext &ctx)
}
/* key flags: check in direct-key sig first, then primary uid, and then latest */
if (dirsig && dirsig->sig.has_subpkt(PGP_SIG_SUBPKT_KEY_FLAGS)) {
flags_ = dirsig->key_flags;
flags_ = dirsig->sig.key_flags();

Check warning on line 2606 in src/lib/pgp-key.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/pgp-key.cpp#L2606

Added line #L2606 was not covered by tests
} else if (prisig && prisig->sig.has_subpkt(PGP_SIG_SUBPKT_KEY_FLAGS)) {
flags_ = prisig->key_flags;
flags_ = prisig->sig.key_flags();
} else if (latest && latest->sig.has_subpkt(PGP_SIG_SUBPKT_KEY_FLAGS)) {
flags_ = latest->key_flags;
flags_ = latest->sig.key_flags();
} else {
flags_ = pgp_pk_alg_capabilities(alg());
}
Expand Down Expand Up @@ -2678,7 +2671,7 @@ pgp_key_t::refresh_data(pgp_key_t *primary, const rnp::SecurityContext &ctx)
expiration_ = sig ? sig->sig.key_expiration() : 0;
/* subkey flags */
if (sig && sig->sig.has_subpkt(PGP_SIG_SUBPKT_KEY_FLAGS)) {
flags_ = sig->key_flags;
flags_ = sig->sig.key_flags();
} else {
flags_ = pgp_pk_alg_capabilities(alg());
}
Expand Down
13 changes: 5 additions & 8 deletions src/lib/pgp-key.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,11 @@ typedef struct pgp_rawpacket_t {

/** information about the signature */
typedef struct pgp_subsig_t {
uint32_t uid{}; /* index in userid array in key for certification sig */
pgp_signature_t sig{}; /* signature packet */
pgp_sig_id_t sigid{}; /* signature identifier */
pgp_rawpacket_t rawpkt{}; /* signature's rawpacket */
uint8_t trustlevel{}; /* level of trust */
uint8_t trustamount{}; /* amount of trust */
uint8_t key_flags{}; /* key flags for certification/direct key sig */
pgp_validity_t validity{}; /* signature validity information */
uint32_t uid{}; /* index in userid array in key for certification sig */
pgp_signature_t sig{}; /* signature packet */
pgp_sig_id_t sigid{}; /* signature identifier */
pgp_rawpacket_t rawpkt{}; /* signature's rawpacket */
pgp_validity_t validity{}; /* signature validity information */

pgp_subsig_t() = delete;
pgp_subsig_t(const pgp_signature_t &sig);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8234,16 +8234,16 @@ add_json_subsig(json_object *jso, bool is_sub, uint32_t flags, const pgp_subsig_
return RNP_ERROR_OUT_OF_MEMORY;
}
// trust level and amount
if (!json_add(jsotrust, "level", (int) subsig->trustlevel) ||
!json_add(jsotrust, "amount", (int) subsig->trustamount)) {
if (!json_add(jsotrust, "level", (int) subsig->sig.trust_level()) ||
!json_add(jsotrust, "amount", (int) subsig->sig.trust_amount())) {
return RNP_ERROR_OUT_OF_MEMORY;
}
// key flags (usage)
if (!add_json_key_usage(jso, subsig->key_flags)) {
if (!add_json_key_usage(jso, subsig->sig.key_flags())) {
return RNP_ERROR_OUT_OF_MEMORY;
}
// key flags (other)
if (!add_json_key_flags(jso, subsig->key_flags)) {
if (!add_json_key_flags(jso, subsig->sig.key_flags())) {
return RNP_ERROR_OUT_OF_MEMORY;
}
// preferences
Expand Down
18 changes: 9 additions & 9 deletions src/tests/key-add-userid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ TEST_F(rnp_tests, test_key_add_userid)
// added0
assert_string_equal(key->get_uid(uidc).str.c_str(), "added0");
assert_int_equal(uidc, key->get_sig(subsigc).uid);
assert_int_equal(0x2, key->get_sig(subsigc).key_flags);
assert_int_equal(0x2, key->get_sig(subsigc).sig.key_flags());
// added1
assert_string_equal(key->get_uid(uidc + 1).str.c_str(), "added1");
assert_int_equal(uidc + 1, key->get_sig(subsigc + 1).uid);
assert_int_equal(0xAB, key->get_sig(subsigc + 1).key_flags);
assert_int_equal(0xAB, key->get_sig(subsigc + 1).sig.key_flags());
// added2
assert_string_equal(key->get_uid(uidc + 2).str.c_str(), "added2");
assert_int_equal(uidc + 2, key->get_sig(subsigc + 2).uid);
assert_int_equal(0xCD, key->get_sig(subsigc + 2).key_flags);
assert_int_equal(0xCD, key->get_sig(subsigc + 2).sig.key_flags());

// save the raw packets for the key (to reload later)
assert_rnp_success(init_mem_dest(&dst, NULL, 0));
Expand Down Expand Up @@ -180,40 +180,40 @@ TEST_F(rnp_tests, test_key_add_userid)
// added0
assert_string_equal(key->get_uid(uidc).str.c_str(), "added0");
assert_int_equal(uidc, key->get_sig(subsigc).uid);
assert_int_equal(0x2, key->get_sig(subsigc).key_flags);
assert_int_equal(0x2, key->get_sig(subsigc).sig.key_flags());
assert_true(key->get_uid(uidc).valid);
// added1
assert_string_equal(key->get_uid(uidc + 1).str.c_str(), "added1");
assert_int_equal(uidc + 1, key->get_sig(subsigc + 1).uid);
assert_int_equal(0xAB, key->get_sig(subsigc + 1).key_flags);
assert_int_equal(0xAB, key->get_sig(subsigc + 1).sig.key_flags());
assert_true(key->get_uid(uidc + 1).valid);
// added2
assert_string_equal(key->get_uid(uidc + 2).str.c_str(), "added2");
assert_int_equal(uidc + 2, key->get_sig(subsigc + 2).uid);
assert_int_equal(0xCD, key->get_sig(subsigc + 2).key_flags);
assert_int_equal(0xCD, key->get_sig(subsigc + 2).sig.key_flags());
assert_true(key->get_uid(uidc + 2).valid);
// delete primary userid and see how flags/expiration changes
key->del_uid(uidc + 1);
key->revalidate(*ks);
assert_string_equal(key->get_uid(uidc + 1).str.c_str(), "added2");
assert_int_equal(uidc + 1, key->get_sig(subsigc + 1).uid);
assert_int_equal(0xCD, key->get_sig(subsigc + 1).key_flags);
assert_int_equal(0xCD, key->get_sig(subsigc + 1).sig.key_flags());
assert_int_equal(key->expiration(), 0);
assert_int_equal(key->flags(), 0xCD);
// delete first uid
key->del_uid(0);
key->revalidate(*ks);
assert_string_equal(key->get_uid(uidc).str.c_str(), "added2");
assert_int_equal(uidc, key->get_sig(subsigc).uid);
assert_int_equal(0xCD, key->get_sig(subsigc).key_flags);
assert_int_equal(0xCD, key->get_sig(subsigc).sig.key_flags());
assert_int_equal(key->expiration(), 0);
assert_int_equal(key->flags(), 0xCD);
// delete last uid, leaving added0 only
key->del_uid(uidc);
key->revalidate(*ks);
assert_string_equal(key->get_uid(uidc - 1).str.c_str(), "added0");
assert_int_equal(uidc - 1, key->get_sig(subsigc - 1).uid);
assert_int_equal(0x2, key->get_sig(subsigc - 1).key_flags);
assert_int_equal(0x2, key->get_sig(subsigc - 1).sig.key_flags());
assert_int_equal(key->expiration(), base_expiry);
assert_int_equal(key->flags(), 0x2);
// delete added0
Expand Down

0 comments on commit 83d4c18

Please sign in to comment.