Skip to content

Commit

Permalink
Refactor rnp_key_store_refresh_subkey_grips to rnp::KeyStore->refresh…
Browse files Browse the repository at this point in the history
…_subkey_grips.
  • Loading branch information
ni4 committed Aug 29, 2023
1 parent 4255f77 commit a43e565
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/rekey/rnp_key_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class KeyStore {
pgp_key_t * add_subkey(pgp_key_t &srckey, pgp_key_t *oldkey);
pgp_sig_import_status_t import_subkey_signature(pgp_key_t & key,
const pgp_signature_t &sig);
bool refresh_subkey_grips(pgp_key_t &key);

public:
std::string path;
Expand Down
16 changes: 7 additions & 9 deletions src/librekey/rnp_key_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,15 @@ KeyStore::key_count() const
return keys.size();
}

namespace {
bool
rnp_key_store_refresh_subkey_grips(KeyStore *keyring, pgp_key_t *key)
KeyStore::refresh_subkey_grips(pgp_key_t &key)
{
if (key->is_subkey()) {
if (key.is_subkey()) {
RNP_LOG("wrong argument");
return false;
}

for (auto &skey : keyring->keys) {
for (auto &skey : keys) {
bool found = false;

/* if we have primary_grip then we also added to subkey_grips */
Expand All @@ -240,19 +239,19 @@ rnp_key_store_refresh_subkey_grips(KeyStore *keyring, pgp_key_t *key)
if (subsig.sig.type() != PGP_SIG_SUBKEY) {
continue;
}
if (subsig.sig.has_keyfp() && (key->fp() == subsig.sig.keyfp())) {
if (subsig.sig.has_keyfp() && (key.fp() == subsig.sig.keyfp())) {
found = true;
break;
}
if (subsig.sig.has_keyid() && (key->keyid() == subsig.sig.keyid())) {
if (subsig.sig.has_keyid() && (key.keyid() == subsig.sig.keyid())) {
found = true;
break;
}
}

if (found) {
try {
key->link_subkey_fp(skey);
key.link_subkey_fp(skey);
} catch (const std::exception &e) {
RNP_LOG("%s", e.what());
return false;
Expand All @@ -262,7 +261,6 @@ rnp_key_store_refresh_subkey_grips(KeyStore *keyring, pgp_key_t *key)

return true;
}
} // namespace

pgp_key_t *
KeyStore::add_subkey(pgp_key_t &srckey, pgp_key_t *oldkey)
Expand Down Expand Up @@ -350,7 +348,7 @@ KeyStore::add_key(pgp_key_t &srckey)
keybyfp[srckey.fp()] = std::prev(keys.end());
*added_key = pgp_key_t(srckey);
/* primary key may be added after subkeys, so let's handle this case correctly */
if (!rnp_key_store_refresh_subkey_grips(this, added_key)) {
if (!refresh_subkey_grips(*added_key)) {
RNP_LOG_KEY("failed to refresh subkey grips for %s", added_key);
}
} catch (const std::exception &e) {
Expand Down

0 comments on commit a43e565

Please sign in to comment.