--- trunk/Src/wptKeyCache.cpp 2005/10/28 07:15:26 41 +++ trunk/Src/wptKeyCache.cpp 2006/05/03 14:34:08 209 @@ -1,14 +1,14 @@ /* wptKeyCache.cpp- Caching for the pub- and the secring - * Copyright (C) 2001-2005 Timo Schulz + * Copyright (C) 2001-2006 Timo Schulz * - * This file is part of MyGPGME. + * This file is part of WinPT. * - * MyGPGME is free software; you can redistribute it and/or modify + * WinPT is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * MyGPGME is distributed in the hope that it will be useful, + * WinPT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. @@ -17,34 +17,149 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ + +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include #include #include #include #include +#include -#include "w32gpgme.h" #include "wptKeyCache.h" #include "openpgp.h" #include "wptNLS.h" #include "wptErrors.h" #include "wptW32API.h" +#include "wptGPG.h" +#include "wptTypes.h" +#include "wptCommonCtl.h" +#include "wptContext.h" +#include "wptKeyEdit.h" +#include "wptUTF8.h" + + +/* Attribute list which holds the image data. */ +struct attr_list_s { + struct attr_list_s *next; + char *fpr; /* fingerprint of the key */ + unsigned char *d; /* actual JPEG data. */ + unsigned long octets; /* length of the data. */ + unsigned int flags; /* status of the attribute. */ +}; +typedef struct attr_list_s *attr_list_t; -/* convert a binary buffer into its hex representation. */ -static void -buffer_to_string (char *dst, size_t dlen, const byte *buf, size_t nbytes) +/* Free attribute list @ctx. */ +void +free_attr_list (attr_list_t ctx) { - char dig[3]; - size_t i; + attr_list_t n; + while (ctx) { + n = ctx->next; + safe_free (ctx->fpr); + safe_free (ctx->d); + ctx = n; + } +} + +/* Parse the attribute list in @fp and store it into @ctx. + Return value: number of parsed items. */ +int +parse_attr_list (FILE *fp, const BYTE *data, DWORD datlen, attr_list_t *ctx) +{ + attr_list_t c, t; + char buf[512], *p, *buffer; + int pos, n=0; + + *ctx = NULL; + while (fgets (buf, 511, fp)) { + if (strstr (buf, "\r\n")) + buf[strlen (buf)-2]=0; + if (strstr (buf, "\n")) + buf[strlen (buf)-1]=0; + if (strlen (buf) < 2 || !strstr (buf, "ATTRIBUTE")) + continue; + buffer = buf+9+10; + pos = 0; + c = (attr_list_t)calloc (1, sizeof *c); + if (!c) + BUG (0); + p = strtok (buffer, " "); + while (p != NULL) { + switch (pos) { + case 0: + c->fpr = strdup (p); + break; + + case 1: + c->octets = strtoul (p, NULL, 10); + break; + + case 7: + c->flags = strtoul (p, NULL, 10); + break; + + default: + break; + } + pos++; + p = strtok (NULL, " "); + } + if (!*ctx) + *ctx = c; + else { + for (t = *ctx; t->next; t=t->next) + ; + t->next = c; + } + c->d = (unsigned char*)malloc (c->octets); + if (!c->d) + BUG (0); + memcpy (c->d, data, c->octets); + data += c->octets; + datlen -= c->octets; + n++; + } + /*assert (datlen == 0); */ + return n; +} + - memset (dst, 0, dlen); - for (i = 0; i < nbytes && dlen > 0; i++) { - sprintf (dig, "%02X", buf[i]); - strcat (dst, dig); - dlen -= 2; +static int +parse_attr_data (const char *keyid, attr_list_t *list) +{ + gpgme_error_t err; + FILE *tmp; + BYTE *data; + char *status, tmpnam[MAX_PATH+1]; + DWORD ndata = 0; + + err = gpg_get_photoid_data (keyid, &status, &data, &ndata); + if (err) + return err; + + get_temp_name (tmpnam, MAX_PATH, NULL); + tmp = fopen (tmpnam, "w+b"); + if (ndata > 0 && tmp != NULL) { + fwrite (status, 1, strlen (status), tmp); + fflush (tmp); + rewind (tmp); + + ndata = parse_attr_list (tmp, data, ndata, list); + fclose (tmp); + DeleteFile (tmpnam); } + else + *list = NULL; + + safe_free (status); + safe_free (data); + return ndata; } @@ -53,7 +168,7 @@ static void parse_secring (gpg_keycache_t cache, const char *kid, const char *secring) { - PACKET *pkt = (PACKET*)calloc (1, sizeof *pkt); + PACKET *pkt; PKT_secret_key *sk; gpg_iobuf_t inp; gpgme_error_t err; @@ -62,11 +177,13 @@ char keyid[16+1]; inp = gpg_iobuf_open (secring); - if (!inp) { - safe_free (pkt); + if (!inp) return; - } + gpg_iobuf_ioctl (inp, 3, 1, NULL); + pkt = (PACKET*)calloc (1, sizeof *pkt); + if (!pkt) + BUG (0); gpg_init_packet (pkt); while (gpg_parse_packet (inp, pkt) != -1) { if (pkt->pkttype == PKT_SECRET_KEY) { @@ -95,46 +212,163 @@ } +/* Update the photo image of a single key with the fingerprint + @fpr. The @dat struct contains the new item data. */ +static gpgme_error_t +keycache_update_photo (gpg_keycache_t ctx, const char *fpr, attr_list_t dat) +{ + struct keycache_s *fnd = NULL; + gpgme_key_t key; + + gpg_keycache_find_key2 (ctx, fpr, 0, &key, &fnd); + if (!fnd) + return gpg_error (GPG_ERR_NOT_FOUND); + safe_free (fnd->attrib.d); + fnd->attrib.flags = dat->flags; + fnd->attrib.len = dat->octets; + fnd->attrib.d = (unsigned char*)malloc (dat->octets); + if (!fnd->attrib.d) + BUG (0); + memcpy (fnd->attrib.d, dat->d, dat->octets); + return 0; +} + + +/* Update all photo images in the cache. */ +static gpgme_error_t +keycache_update_photos (gpg_keycache_t ctx) +{ + attr_list_t list=NULL, n; + DWORD ndata; + + ndata = parse_attr_data (NULL, &list); + if (ndata < 1) { + free_attr_list (list); + return 0; + } + + for (n=list; n; n=n->next) + keycache_update_photo (ctx, n->fpr, n); + free_attr_list (list); + return 0; +} + + +static void +keycache_decode_uid (struct keycache_s *ctx) +{ + gpgme_user_id_t u; + struct native_uid_s *n, *t; + + for (u = ctx->key->uids; u; u = u->next) { + n = (struct native_uid_s*)calloc (1, sizeof *n); + if (!n) + BUG (0); + if (is_8bit_string (u->uid)) { + n->malloced = 1; + n->uid = utf8_to_native (u->uid); + if (u->name != NULL) + n->name = utf8_to_native (u->name); + if (u->email != NULL) + n->email = strdup (u->email); + if (u->comment != NULL) + n->comment = utf8_to_native (u->comment); + } + else { + n->malloced = 0; + n->uid = u->uid; + n->name = u->name; + n->comment = u->comment; + n->email = u->email; + } + n->signatures = u->signatures; + n->validity = u->validity; + n->revoked = u->revoked; + if (!ctx->uids) + ctx->uids = n; + else { + for (t = ctx->uids; t->next; t=t->next) + ; + t->next = n; + } + } +} + + +/* Store utf8 decoded user IDs in the code to avoid in-place decoding. */ +static void +keycache_decode_uids (gpg_keycache_t ctx) +{ + struct keycache_s *c; + + for (c = ctx->item; c; c = c->next) + keycache_decode_uid (c); +} + + +static void +free_native_uids (struct native_uid_s **r_n) +{ + struct native_uid_s *t; + struct native_uid_s *n = *r_n; + + while (n != NULL) { + t = n->next; + if (n->malloced) { + safe_free (n->uid); + safe_free (n->name); + safe_free (n->comment); + safe_free (n->email); + safe_free (n->uid); + } + safe_free (n); + n = t; + } + *r_n = NULL; +} + + + /* Merge the information from the keyrings into the key cache structure. */ -gpgme_error_t +static gpgme_error_t keycache_prepare2 (gpg_keycache_t ctx, const char *kid, const char *pubring, const char *secring) { - gpgme_error_t err; + gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); gpgme_key_t key = NULL; gpg_iobuf_t inp; - PACKET *pkt = (PACKET*)calloc (1, sizeof * pkt); + PACKET *pkt; struct keycache_s *c; const byte *sym_prefs; - char keyid[16+1], *id = NULL; + char keyid[16+1]; int key_seen = 0; - size_t nbytes = 0, nsym =0; + size_t nsym =0; if (secring) { parse_secring (ctx, kid, secring); - if (!pubring) { - safe_free(pkt); + if (!pubring) return 0; - } } inp = gpg_iobuf_open (pubring); - if (!inp) { - safe_free( pkt ); + if (!inp) return gpg_error (GPG_ERR_KEYRING_OPEN); - } - gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */ + gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ - gpg_init_packet( pkt ); + pkt = (PACKET*)calloc (1, sizeof * pkt); + if (!pkt) + BUG (0); + gpg_init_packet (pkt); while (gpg_parse_packet (inp, pkt) != -1) { if (pkt->pkttype == PKT_PUBLIC_KEY) { strcpy (keyid, ""); key_seen = 1; } - - if (pkt->pkttype == PKT_SIGNATURE && pkt->pkt.signature->sig_class == 0x1F) { + if (pkt->pkttype == PKT_SIGNATURE && + pkt->pkt.signature->sig_class == 0x1F) { if (pkt->pkt.signature->numrevkeys == 0) goto next; - _snprintf (keyid, sizeof keyid -1, "%08X", pkt->pkt.signature->keyid[1]); + _snprintf (keyid, sizeof (keyid) -1, "%08X", + pkt->pkt.signature->keyid[1]); if (kid && strcmp (kid, keyid) != 0) goto next; err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c); @@ -142,12 +376,15 @@ goto next; c->gloflags.has_desig_rev = 1; } - if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) { - sym_prefs=gpg_parse_sig_subpkt (pkt->pkt.signature->hashed, - SIGSUBPKT_PREF_SYM, &nsym); - if (sym_prefs == NULL) + if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 && c != NULL) { + if (c->sym_prefs) /* only use the prefs from the primary uid. */ goto next; - _snprintf (keyid, sizeof keyid - 1, "%08X", pkt->pkt.signature->keyid[1]); + sym_prefs = gpg_parse_sig_subpkt (pkt->pkt.signature->hashed, + SIGSUBPKT_PREF_SYM, &nsym); + if (!sym_prefs) + goto next; + _snprintf (keyid, sizeof (keyid) - 1, "%08X", + pkt->pkt.signature->keyid[1]); if (kid && strcmp (kid, keyid) != 0) goto next; err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c); @@ -156,40 +393,15 @@ else if (nsym > 0) { c->sym_prefs = (unsigned char*)calloc (1, nsym+1); if (!c->sym_prefs) - return gpg_error (GPG_ERR_ENOMEM); + BUG (0); memcpy (c->sym_prefs, sym_prefs, nsym); } } - if (pkt->pkttype == PKT_USER_ID) { - if (id) - free (id); - id = strdup (pkt->pkt.user_id->name); - if (!id) { - err = gpg_error (GPG_ERR_ENOMEM); - goto fail; - } - } - if ((pkt->pkttype == PKT_USER_ID || pkt->pkttype == PKT_ATTRIBUTE) - && pkt->pkt.user_id->attrib_data && key) { - PKT_user_id *id = pkt->pkt.user_id; - c->attrib.used = 1; - c->attrib.len = id->attrib_len; - c->attrib.d = (unsigned char*)calloc (1, id->attrib_len + 1); - if (!c->attrib.d) { - err = gpg_error (GPG_ERR_ENOMEM); - goto fail; - } - memcpy (c->attrib.d, id->attrib_data, id->attrib_len); - key = NULL; - c = NULL; - } next: gpg_free_packet (pkt); gpg_init_packet(pkt); } -fail: - safe_free (id); safe_free (pkt); gpg_iobuf_close (inp); return err; @@ -223,7 +435,7 @@ return gpg_error (GPG_ERR_INV_ARG); ctx = (gpg_keycache_t)calloc (1, sizeof *ctx); if (!ctx) - return gpg_error (GPG_ERR_ENOMEM); + BUG (0); ctx->secret = 0; ctx->pos = 0; *r_ctx = ctx; @@ -244,18 +456,17 @@ c2 = c->next; gpgme_key_release (c->key); c->key = NULL; - if (c->sym_prefs) - free (c->sym_prefs); - c->sym_prefs = NULL; - if (c->attrib.d) - free (c->attrib.d); - c->attrib.d = NULL; - if (c->card_type) - free (c->card_type); - free (c); + if (c->rev != NULL) + gpg_desig_rev_release (c->rev); + c->rev = NULL; + safe_free (c->pref_keyserver); + safe_free (c->sym_prefs); + safe_free (c->attrib.d); + safe_free (c->card_type); + free_native_uids (&c->uids); + safe_free (c); } - if (ctx) - free (ctx); + safe_free (ctx); } @@ -289,7 +500,7 @@ c = (struct keycache_s*)calloc (1, sizeof *c); if (!c) - return gpg_error (GPG_ERR_ENOMEM); + BUG (0); c->gloflags.is_protected = 1; /*default: assume protection. */ c->key = key; if (!ctx->item) @@ -360,7 +571,7 @@ } *r_key = NULL; return gpg_error (GPG_ERR_INTERNAL); -} /* keycache_find_key */ +} gpgme_error_t @@ -371,6 +582,47 @@ } +/* Reload a photo image of a single key with the keyid @keyid. + Return value: 0 on success. */ +static gpgme_error_t +keycache_reload_photo (gpg_keycache_t ctx, const char *keyid) +{ + attr_list_t list; + + if (parse_attr_data (keyid, &list) < 1) { + free_attr_list (list); + return 0; + } + keycache_update_photo (ctx, list->fpr, list); + free_attr_list (list); + return 0; +} + + +/* Return the next key which was updated. Before it is + returned the update flag is cleared. + @r_status is 1 for a new key and 2 for an updated key. + Return value: 0 on success. */ +gpgme_error_t +gpg_keycache_next_updated_key (gpg_keycache_t ctx, + struct keycache_s **r_obj, + int *r_status) +{ + struct keycache_s *c; + + for (c = ctx->item; c; c = c->next) { + if (c->flags != 0) { + *r_status = c->flags; + *r_obj = c; + c->flags = 0; + return 0; + } + } + return gpg_error (GPG_ERR_NOT_FOUND); +} + + + gpgme_error_t gpg_keycache_update_key (gpg_keycache_t ctx, int is_sec, void *opaque, const char *keyid) @@ -384,6 +636,7 @@ err = gpgme_new (&gctx); if (err) return err; + gpgme_set_keylist_mode (gctx, GPGME_KEYLIST_MODE_SIGS/*|GPGME_KEYLIST_MODE_SIG_NOTATIONS*/); err = gpgme_get_key (gctx, keyid, &key, is_sec); gpgme_release (gctx); if (err) @@ -393,12 +646,14 @@ log_debug ("keycache update: keyid=%s %p\r\n", keyid, pub); gpgme_key_release (fndkey); c->key = key; - c->flags = 0; + c->flags = KC_FLAG_UPD; if (is_sec && pub != NULL && !gpg_keycache_find_key (pub, keyid, 0, &fndkey)) { log_debug ("keycache update: set public part %p\r\n", fndkey); c->pubpart->key = fndkey; } + /* XXX: this is also called for keys without a photo-id. */ + keycache_reload_photo (ctx, keyid); } else { log_debug ("keycache add: sync public part\r\n"); @@ -414,7 +669,17 @@ c->gloflags.divert_to_card = c_new->gloflags.divert_to_card; } } + if (c) + c->flags = KC_FLAG_ADD; + + } + + /* refresh utf8 user ID list. */ + if (c != NULL) { + free_native_uids (&c->uids); + keycache_decode_uid (c); } + return 0; } @@ -437,17 +702,18 @@ c = ctx->item; if (c->next == NULL) { gpgme_key_release (itm->key); - if (itm) - free (itm); + safe_free (itm); ctx->item = NULL; } else { - while (c && c->next != itm) - c = c->next; + for (; c != NULL; c = c->next) { + if (c->next == itm) + break; + } + assert (c != NULL); /* XXX: sometimes access violation. */ c->next = c->next->next; gpgme_key_release (itm->key); - if (itm) - free (itm); + safe_free (itm); } return 0; } @@ -471,7 +737,8 @@ if (err) return err; - gpgme_set_keylist_mode (c, GPGME_KEYLIST_MODE_SIGS); + /* XXX: GPGME_KEYLIST_MODE_SIG_NOTATIONS causes an internal error! */ + gpgme_set_keylist_mode (c, GPGME_KEYLIST_MODE_SIGS/*|GPGME_KEYLIST_MODE_SIG_NOTATIONS*/); err = gpgme_op_keylist_start (c, pattern, secret); while(!err) { err = gpgme_op_keylist_next (c, &key); @@ -483,6 +750,8 @@ } if (gpgme_err_code (err) == GPG_ERR_EOF) err = gpg_error (GPG_ERR_NO_ERROR); + keycache_update_photos (ctx); + keycache_decode_uids (ctx); /* XXX: make sure the progress dialog is closed. */ gpgme_op_keylist_end (c); gpgme_release (c); @@ -510,6 +779,22 @@ } +static unsigned char* +copy_uid_prefs (const unsigned char *prefs) +{ + unsigned char *p; + size_t pos=0; + + while (prefs[pos] != 0) + pos++; + p = (unsigned char*)calloc (1, pos+1); + if (!p) + BUG (0); + memcpy (p, prefs, pos); + return p; +} + + gpgme_error_t gpg_keycache_sync (gpg_keycache_t pub, gpg_keycache_t sec) { @@ -525,6 +810,8 @@ c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card; if (!c->gloflags.divert_to_card) c->gloflags.divert_to_card = key_divert_to_card (key); + if (c_sec->sym_prefs) + c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); c->pubpart = c_sec; c->pubpart->key = key; } @@ -573,9 +860,14 @@ *r_key = NULL; return gpg_error (GPG_ERR_EOF); } - + if (ctx->tmp->flags != 0) + ctx->tmp->flags = 0; /* reset the 'updated' status. */ + /* it might be possible there is no public key. */ + if (flags && ctx->tmp->pubpart == NULL) + flags = 0; *r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key; - *c = ctx->tmp = ctx->tmp->next; + *c = ctx->tmp; + ctx->tmp = ctx->tmp->next; ctx->pos++; return 0; @@ -589,8 +881,155 @@ gpg_keycache_next_key (gpg_keycache_t ctx, int flags, gpgme_key_t *r_key) { struct keycache_s *c=NULL; - gpgme_error_t err = 0; + gpgme_error_t err; err = keycache_next_key (ctx, flags, &c, r_key); return err; } + +gpgme_error_t +gpg_keycache_next_key2 (gpg_keycache_t ctx, int flags, + struct keycache_s **c, gpgme_key_t *r_key) +{ + return keycache_next_key (ctx, flags, c, r_key); +} + + +/* Search for a key with the pattern @pattern and mark + this key as the default signing key if found. + Return value: 0 on success. */ +gpgme_error_t +gpg_keycache_set_default_key (gpg_keycache_t ctx, + const char *pattern) +{ + gpgme_error_t err; + gpgme_key_t key; + struct keycache_s *itm; + + err = gpg_keycache_find_key2 (ctx, pattern, 0, &key, &itm); + if (err) + return err; + + if (itm) + itm->default_key = 1; + return 0; +} + +/* Return the default key from the cache. If no was + marked before, NULL is returned in @r_key. + Return value: 0 on success. */ +gpgme_error_t +gpg_keycache_get_default_key (gpg_keycache_t ctx, + gpgme_key_t *r_key) +{ + struct keycache_s *itm; + + *r_key = NULL; + for (itm = ctx->item; itm; itm = itm->next) { + if (itm->default_key) { + *r_key = itm->key; + break; + } + } + if (!*r_key) + return gpgme_error (GPG_ERR_NOT_FOUND); + return 0; +} + + +static gpgme_error_t +decode_subpacket (const char *subpkt_data, int *type, + char **out, WORD *outlen) +{ + char tmp[128], *val; + char *enc = NULL; + size_t pos = 0, i=0; + + /* example: spk:24:1:21:http%3A//subkeys.pgp.de */ + *outlen = 0; + *out = NULL; + + if (strncmp (subpkt_data, "spk:", 4)) + return gpg_error (GPG_ERR_NO_DATA); + + strncpy (tmp, subpkt_data, 62); + val = strtok (tmp, ":"); + while (val != NULL) { + switch (pos++) { + case 0: + break; + + case 1: + if (type) + *type = atoi (val); + break; + + case 2: + break; + + case 3: + *outlen = atoi (val); + break; + + case 4: + enc = strdup (val); + break; + } + val = strtok (NULL, ":"); + } + if (!enc) + return gpg_error (GPG_ERR_NO_DATA);; + *out = (char*)calloc (1, strlen (enc)+1); + if (!*out) + BUG (0); + for (pos = 0; pos < strlen (enc); pos++) { + if (enc[pos] == '%' && enc[pos+1] == '%') + (*out)[i++] = '%'; + else if (enc[pos] == '%') { + char temp[3]; + temp[0] = enc[++pos]; + temp[1] = enc[++pos]; + temp[2] = 0; + (*out)[i++] = (char)strtoul (temp, NULL, 16); + } + else + (*out)[i++] = enc[pos]; + } + (*out)[i] = 0; + safe_free (enc); + return 0; +} + + +/* If the attribute given in @attr is not set in the + key cache object, try to update it. */ +gpgme_error_t +gpg_keycache_update_attr (struct keycache_s *item, + int attr, int force) +{ + gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); + char *val = NULL; + WORD n = 0; + + switch (attr) { + case KC_ATTR_PREFSYM: + if (!force && item->sym_prefs) + break; + safe_free (item->sym_prefs); + err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); + if (!err && val != NULL) + err = decode_subpacket (val, NULL, (char**)&item->sym_prefs, &n); + break; + + case KC_ATTR_PREFKSERV: + if (!force && item->pref_keyserver) + break; + safe_free (item->pref_keyserver); + err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); + if (!err && val != NULL) + err = decode_subpacket (val, NULL, &item->pref_keyserver, &n); + break; + } + safe_free (val); + return err; +}