Skip to content

Commit

Permalink
Add a rpmKeyring iterator
Browse files Browse the repository at this point in the history
Filter subkeys by default but support returning them. This is not exposed
in the API for now. But can be used for returning a interator over subkeys.

Resolves: rpm-software-management#3337
  • Loading branch information
ffesti committed Oct 8, 2024
1 parent a45134e commit a460786
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
28 changes: 28 additions & 0 deletions include/rpm/rpmkeyring.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ rpmKeyring rpmKeyringFree(rpmKeyring keyring);
*/
int rpmKeyringAddKey(rpmKeyring keyring, rpmPubkey key);

/** \ingroup rpmkeyring
* Get iterator for all the primary keys in the keyring
* @param keyring keyring handle
* @return iterator or NULL
*/
rpmKeyringIterator rpmKeyringGetIterator(rpmKeyring keyring);

/** \ingroup rpmkeyring
* Reference a keyring iterator.
* @param iterator iterator handle
* @return new iterator reference
*/
rpmKeyringIterator rpmKeyringIteratorLink(rpmKeyringIterator iterator);

/** \ingroup rpmkeyring
* Get next key in keyring
* @param iterator iterator handle
* @return next public key or NULL if end is reached
*/
rpmPubkey rpmKeyringIteratorNext(rpmKeyringIterator iterator);

/** \ingroup rpmkeyring
* Free iterator
* @param iterator iterator handle
* @return NULL
*/
rpmKeyringIterator rpmKeyringIteratorFree(rpmKeyringIterator iterator);

/** \ingroup rpmkeyring
* Perform combined keyring lookup and signature verification
* @param keyring keyring handle
Expand Down
1 change: 1 addition & 0 deletions include/rpm/rpmtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef void * rpmCallbackData;

typedef struct rpmPubkey_s * rpmPubkey;
typedef struct rpmKeyring_s * rpmKeyring;
typedef struct rpmKeyringIterator_s * rpmKeyringIterator;

typedef uint32_t rpmsid;
typedef struct rpmstrPool_s * rpmstrPool;
Expand Down
73 changes: 73 additions & 0 deletions rpmio/rpmkeyring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ struct rpmKeyring_s {
std::shared_mutex mutex;
};

typedef enum rpmKeyringInteratorFilterMode_e {
RPMKEYRINGFILTERNONE = 0,
RPMKEYRINGFILTERSUBKEYS = 1,
} rpmKeyringIteratorFilterMode;

struct rpmKeyringIterator_s {
rpmKeyring keyring;
std::map<std::string,rpmPubkey>::const_iterator iterator;
rpmKeyringIteratorFilterMode filter;
int nrefs;
std::shared_mutex mutex;
};

static std::string key2str(const uint8_t *keyid)
{
return std::string(reinterpret_cast<const char *>(keyid), PGP_KEYID_LEN);
Expand All @@ -61,6 +74,66 @@ rpmKeyring rpmKeyringFree(rpmKeyring keyring)
return NULL;
}

rpmKeyringIterator rpmKeyringGetIterator(rpmKeyring keyring)
{
if (!keyring)
return NULL;

keyring = rpmKeyringLink(keyring);
rdlock lock(keyring->mutex);

rpmKeyringIterator iter = new rpmKeyringIterator_s {};
iter->iterator = keyring->keys.cbegin();
iter->keyring = keyring;
iter->nrefs = 1;
iter->filter = RPMKEYRINGFILTERSUBKEYS;
return iter;
}

rpmPubkey rpmKeyringIteratorNext(rpmKeyringIterator iterator)
{
rpmPubkey next = NULL;

if (!iterator)
return NULL;

wrlock ilock(iterator->mutex);
rdlock klock(iterator->keyring->mutex);

while (iterator->iterator != iterator->keyring->keys.end()) {
next = iterator->iterator->second;
iterator->iterator++;
rdlock lock(next->mutex);
if (iterator->filter == RPMKEYRINGFILTERNONE || !next->primarykey)
break;
}
return rpmPubkeyLink(next);
}

rpmKeyringIterator rpmKeyringIteratorLink(rpmKeyringIterator iterator)
{
if (iterator) {
wrlock lock(iterator->mutex);
iterator->nrefs++;
}

return iterator;
}

rpmKeyringIterator rpmKeyringIteratorFree(rpmKeyringIterator iterator)
{
if (!iterator)
return NULL;

wrlock lock(iterator->mutex);
if (--iterator->nrefs == 0) {
rpmKeyringFree(iterator->keyring);
delete iterator;
}
return NULL;
}


int rpmKeyringModify(rpmKeyring keyring, rpmPubkey key, rpmKeyringModifyMode mode)
{
int rc = 1; /* assume already seen key */
Expand Down

0 comments on commit a460786

Please sign in to comment.