Added 'get' key by fingerprint, 'delelet' key by id an by fingerprint

master
Edouard Tisserant 8 years ago
parent ec72218253
commit df0053cbfc

@ -111,14 +111,14 @@ pgp_keydata_new(void)
/**
\ingroup HighLevel_Keyring
\brief Frees keydata and its memory
\brief Frees key's allocated memory
\param keydata Key to be freed.
\note This frees the keydata itself, as well as any other memory alloc-ed by it.
\note This does not free the keydata itself, but any other memory alloc-ed by it.
*/
void
pgp_keydata_free(pgp_key_t *keydata)
pgp_key_free(pgp_key_t *keydata)
{
unsigned n;
@ -144,8 +144,21 @@ pgp_keydata_free(pgp_key_t *keydata)
pgp_seckey_free(&keydata->key.seckey);
}
/* XXX sigkey enckey ? */
}
/**
\ingroup HighLevel_Keyring
\brief Frees keydata and its memory
\param keydata Key to be freed.
\note This frees the keydata itself, as well as any other memory alloc-ed by it.
*/
void
pgp_keydata_free(pgp_key_t *keydata)
{
pgp_key_free(keydata);
free(keydata);
}
@ -884,6 +897,26 @@ pgp_keyring_free(pgp_keyring_t *keyring)
keyring->keyc = keyring->keyvsize = 0;
}
static unsigned
deletekey( pgp_keyring_t *keyring, pgp_key_t *key, unsigned from)
{
/* 'from' is index of key to delete */
/* free key internals */
pgp_key_free(key);
/* decrement key count, vsize stays the same so no realloc needed */
keyring->keyc--;
/* Move following keys to fill the gap */
for ( ; keyring && from < keyring->keyc; from += 1) {
memcpy(&keyring->keys[from], &keyring->keys[from+1],
sizeof(pgp_key_t));
}
return 1;
}
/**
\ingroup HighLevel_KeyringFind
@ -932,6 +965,78 @@ pgp_getkeybyid(pgp_io_t *io, const pgp_keyring_t *keyring,
return NULL;
}
unsigned
pgp_deletekeybyid(pgp_io_t *io, pgp_keyring_t *keyring,
const uint8_t *keyid)
{
unsigned from = 0;
pgp_key_t *key;
if ((key = (pgp_key_t *)pgp_getkeybyid(io, keyring, keyid,
&from, NULL)) == NULL) {
return 0;
}
/* 'from' is now index of key to delete */
deletekey(keyring, key, from);
return 1;
}
/**
\ingroup HighLevel_KeyringFind
\brief Finds key in keyring from its Key Fingerprint
\param keyring Keyring to be searched
\param fpr fingerprint of required key
\param fpr length of required key
\return Pointer to key, if found; NULL, if not found
\note This returns a pointer to the key inside the given keyring,
not a copy. Do not free it after use.
*/
const pgp_key_t *
pgp_getkeybyfpr(pgp_io_t *io, const pgp_keyring_t *keyring,
const uint8_t *fpr, size_t length,
unsigned *from, pgp_pubkey_t **pubkey)
{
pgp_fingerprint_t *kfp = &keyring->keys[*from].sigfingerprint;
for ( ; keyring && *from < keyring->keyc; *from += 1) {
if (kfp->length == length &&
memcmp(kfp, fpr, length) == 0) {
if (pubkey) {
*pubkey = &keyring->keys[*from].key.pubkey;
}
return &keyring->keys[*from];
}
}
return NULL;
}
unsigned
pgp_deletekeybyfpr(pgp_io_t *io, pgp_keyring_t *keyring,
const uint8_t *fpr, size_t length)
{
unsigned from = 0;
pgp_key_t *key;
if ((key = (pgp_key_t *)pgp_getkeybyfpr(io, keyring, fpr, length,
&from, NULL)) == NULL) {
return 0;
}
/* 'from' is now index of key to delete */
deletekey(keyring, key, from);
return 1;
}
/* convert a string keyid into a binary keyid */
static void
str2keyid(const char *userid, uint8_t *keyid, size_t len)

@ -77,6 +77,19 @@ const pgp_key_t *pgp_getkeybyid(pgp_io_t *,
const uint8_t *,
unsigned *,
pgp_pubkey_t **);
unsigned pgp_deletekeybyid(pgp_io_t *,
pgp_keyring_t *,
const uint8_t *);
const pgp_key_t *pgp_getkeybyfpr(pgp_io_t *,
const pgp_keyring_t *,
const uint8_t *fpr,
size_t length,
unsigned *,
pgp_pubkey_t **);
unsigned pgp_deletekeybyfpr(pgp_io_t *,
pgp_keyring_t *,
const uint8_t *fpr,
size_t length);
const pgp_key_t *pgp_getkeybyname(pgp_io_t *,
const pgp_keyring_t *,
const char *);
@ -84,6 +97,7 @@ const pgp_key_t *pgp_getnextkeybyname(pgp_io_t *,
const pgp_keyring_t *,
const char *,
unsigned *);
void pgp_key_free(pgp_key_t *);
void pgp_keydata_free(pgp_key_t *);
int pgp_keydata_dup(pgp_key_t *, pgp_key_t *, unsigned);
void pgp_keyring_free(pgp_keyring_t *);

Loading…
Cancel
Save