Added pgp_ensure_pubkey

master
Edouard Tisserant 8 years ago
parent 0e3e78b365
commit a389cbfa7e

@ -1337,6 +1337,37 @@ pgp_add_to_pubring(pgp_keyring_t *keyring, const pgp_pubkey_t *pubkey, pgp_conte
}
}
pgp_key_t *pgp_ensure_pubkey(
pgp_keyring_t *keyring,
pgp_pubkey_t *pubkey,
uint8_t *pubkeyid)
{
pgp_key_t *key;
unsigned c;
/* try to find key keyring */
for (c = 0; keyring && c < keyring->keyc; c += 1) {
if (memcmp(keyring->keys[c].sigid,
pubkeyid, PGP_KEY_ID_SIZE) == 0) {
return &keyring->keys[c];
}
}
/* if key doesn't already exist in keyring, create it */
EXPAND_ARRAY(keyring, key);
key = &keyring->keys[keyring->keyc++];
(void) memset(key, 0x0, sizeof(*key));
/* fill in what we already know */
key->type = PGP_PTAG_CT_PUBLIC_KEY;
pgp_pubkey_dup(&key->key.pubkey, pubkey);
pgp_pubkey_dup(&key->sigkey, pubkey);
(void) memcpy(&key->sigid, pubkeyid, sizeof(PGP_KEY_ID_SIZE));
pgp_fingerprint(&key->sigfingerprint, pubkey, keyring->hashtype);
return key;
}
/* add a key to a secret keyring */
int
pgp_add_to_secring(pgp_keyring_t *keyring, const pgp_seckey_t *seckey)

@ -164,6 +164,10 @@ char *pgp_export_key(pgp_io_t *, const pgp_key_t *, uint8_t *);
int pgp_keyring_add(pgp_keyring_t *, const pgp_key_t *);
int pgp_add_to_pubring(pgp_keyring_t *, const pgp_pubkey_t *, pgp_content_enum tag);
pgp_key_t *pgp_ensure_pubkey(
pgp_keyring_t *,
pgp_pubkey_t *,
uint8_t *);
int pgp_add_to_secring(pgp_keyring_t *, const pgp_seckey_t *);
int pgp_append_keyring(pgp_keyring_t *, pgp_keyring_t *);

@ -972,6 +972,7 @@ typedef struct pgp_subsig_t {
/* describes a user's key */
struct pgp_key_t {
/* TODO make uid a struct with siginfo and dynarrays of pkts */
DYNARRAY(uint8_t *, uid); /* array of user ids */
DYNARRAY(pgp_subpacket_t, packet); /* array of raw subpackets */
DYNARRAY(pgp_subsig_t, subsig); /* array of signature subkeys */

@ -245,7 +245,6 @@ pgp_validate_key_cb(const pgp_packet_t *pkt, pgp_cbdata_t *cbinfo)
pgp_pubkey_t *sigkey;
pgp_error_t **errors;
pgp_io_t *io;
unsigned from;
unsigned valid = 0;
io = cbinfo->io;
@ -336,10 +335,11 @@ pgp_validate_key_cb(const pgp_packet_t *pkt, pgp_cbdata_t *cbinfo)
}
case PGP_PTAG_CT_SIGNATURE: /* V3 sigs */
case PGP_PTAG_CT_SIGNATURE_FOOTER: /* V4 sigs */
from = 0;
sigkey = NULL;
if(vdata->keyring){
unsigned from;
const pgp_key_t *signer;
from = 0;
signer = pgp_getkeybyid(io, vdata->keyring,
content->sig.info.signer_id,
&from, &sigkey);
@ -753,17 +753,31 @@ static void printtime(time_t t)
tm->tm_mon + 1,
tm->tm_mday);
}
typedef struct key_filter_cb_t{
pgp_keyring_t *destring;
pgp_key_t *key;
} key_filter_cb_t;
static pgp_cb_ret_t key_filter_cb (
validate_key_cb_t *vdata,
const pgp_subpacket_t *sigpkt)
{
key_filter_cb_t *filter = vdata->on_valid_args;
if(vdata->not_commited){
printf("New key ************************************** \n");
/*hexdump(stdout, "signer ID", vdata->valid_sig_info.signer_id,
sizeof(vdata->valid_sig_info.signer_id));*/
hexdump(stdout, "pubkey ID", vdata->pubkeyid,
sizeof(vdata->pubkeyid));
if((filter->key = pgp_ensure_pubkey(filter->destring,
&vdata->pubkey,
vdata->pubkeyid))==NULL){
return PGP_FINISHED;
}
}
switch(vdata->last_seen){
case ATTRIBUTE:
printf("ATTRIBUTE\n");
@ -786,7 +800,13 @@ static pgp_cb_ret_t key_filter_cb (
hexdump(stdout, "subkey ID", subkeyid,
sizeof(subkeyid));
/* XXX TODO add/update subkey
* with expiration and flags from sig info */
* with expiration and flags from sig info
if (memcmp(key->encid, "\0\0\0\0\0\0\0\0", 8) == 0) {
pgp_pubkey_dup(&key->enckey, pubkey);
(void) memcpy(&key->encid, pubkeyid, sizeof(PGP_KEY_ID_SIZE));
(void) memcpy(&key->encfingerprint, &key->sigfingerprint,
sizeof(key->encfingerprint));
* */
}
break;
case PRIMARYKEY:
@ -810,17 +830,21 @@ pgp_filter_keys_from_mem(pgp_io_t *io,
pgp_memory_t *mem)
{
pgp_validation_t vresult;
pgp_stream_t *stream;
const unsigned noaccum = 0;
validate_key_cb_t vdata;
const int printerrors = 1;
unsigned res;
pgp_stream_t *stream;
const unsigned noaccum = 0;
validate_key_cb_t vdata;
key_filter_cb_t filter;
const int printerrors = 1;
unsigned res;
(void) memset(&vresult, 0x0, sizeof(vresult));
(void) memset(&vdata, 0x0, sizeof(vdata));
vdata.result = &vresult;
vdata.getpassphrase = NULL;
(void) memset(&filter, 0x0, sizeof(filter));
filter.destring = destring;
stream = pgp_new(sizeof(*stream));
pgp_parse_options(stream, PGP_PTAG_SS_ALL, PGP_PARSE_PARSED);
pgp_setup_memory_read(io, &stream, mem, &vdata, pgp_validate_key_cb,

Loading…
Cancel
Save