Skip to content

Commit

Permalink
update rpm keyring API calls 1/2 (AddKeyFileToKeyring())
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverkurth committed Mar 10, 2023
1 parent 5237d70 commit a6e2786
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 81 deletions.
93 changes: 19 additions & 74 deletions client/gpgcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,97 +103,42 @@ AddKeyFileToKeyring(
)
{
uint32_t dwError = 0;
uint8_t* pPkt = NULL;
size_t nPktLen = 0;
char* pszKeyData = NULL;
int nKeyDataSize;
int nKeys = 0;
int nOffset = 0;

int subkeysCount, i;
rpmPubkey *subkeys = NULL;
rpmPubkey key = NULL;

if(IsNullOrEmptyString(pszFile) || !pKeyring)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = ReadGPGKeyFile(pszFile, &pszKeyData, &nKeyDataSize);
BAIL_ON_TDNF_ERROR(dwError);

while (nOffset < nKeyDataSize)
{
pgpArmor nArmor = pgpParsePkts(pszKeyData + nOffset, &pPkt, &nPktLen);
if(nArmor == PGPARMOR_PUBKEY)
{
dwError = AddKeyPktToKeyring(pKeyring, pPkt, nPktLen);
BAIL_ON_TDNF_ERROR(dwError);
nKeys++;
}
nOffset += nPktLen;
}
if (nKeys == 0) {
key = rpmPubkeyRead(pszFile);
if (key == NULL) {
pr_err("reading %s failed: %s (%d)", pszFile, strerror(errno), errno);
dwError = ERROR_TDNF_INVALID_PUBKEY_FILE;
BAIL_ON_TDNF_ERROR(dwError);
}

cleanup:
TDNF_SAFE_FREE_MEMORY(pszKeyData);
return dwError;
error:
goto cleanup;
}

uint32_t
AddKeyPktToKeyring(
rpmKeyring pKeyring,
uint8_t* pPkt,
size_t nPktLen
)
{
uint32_t dwError = 0;
pgpDig pDig = NULL;
rpmPubkey pPubkey = NULL;

if(!pKeyring || !pPkt || nPktLen == 0)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

pPubkey = rpmPubkeyNew (pPkt, nPktLen);
if(!pPubkey)
{
dwError = ERROR_TDNF_CREATE_PUBKEY_FAILED;
BAIL_ON_TDNF_ERROR(dwError);
if (rpmKeyringAddKey(pKeyring, key) == 0) {
pr_info("added key %s to keyring");
}
subkeys = rpmGetSubkeys(key, &subkeysCount);
rpmPubkeyFree(key);
for (i = 0; i < subkeysCount; i++) {
rpmPubkey subkey = subkeys[i];

pDig = rpmPubkeyDig(pPubkey);
if(!pDig)
{
dwError = ERROR_TDNF_CREATE_PUBKEY_FAILED;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = rpmKeyringLookup(pKeyring, pDig);
if(dwError == RPMRC_OK)
{
dwError = 0;//key exists
}
else
{
dwError = rpmKeyringAddKey(pKeyring, pPubkey);
if(dwError == 1)
{
dwError = 0;//Already added. ignore
if (rpmKeyringAddKey(pKeyring, subkey) == 0) {
pr_info("added subkey %d of main key %s to keyring\n", i, pszFile);
}
BAIL_ON_TDNF_ERROR(dwError);
rpmPubkeyFree(subkey);
}

cleanup:
if (subkeys)
free(subkeys);
return dwError;
error:
if(pPubkey)
{
rpmPubkeyFree(pPubkey);
}
goto cleanup;
}

Expand Down
7 changes: 0 additions & 7 deletions client/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ AddKeyFileToKeyring(
rpmKeyring pKeyring
);

uint32_t
AddKeyPktToKeyring(
rpmKeyring pKeyring,
uint8_t* pPkt,
size_t nPktLen
);

uint32_t
VerifyRpmSig(
rpmKeyring pKeyring,
Expand Down

0 comments on commit a6e2786

Please sign in to comment.