--- trunk/Src/wptKeyCache.cpp 2006/01/20 09:19:15 164 +++ 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-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. @@ -36,6 +36,11 @@ #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. */ @@ -49,14 +54,15 @@ typedef struct attr_list_s *attr_list_t; +/* Free attribute list @ctx. */ void free_attr_list (attr_list_t ctx) { attr_list_t n; while (ctx) { n = ctx->next; - free (ctx->fpr); - free (ctx->d); + safe_free (ctx->fpr); + safe_free (ctx->d); ctx = n; } } @@ -81,6 +87,8 @@ 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) { @@ -102,7 +110,6 @@ pos++; p = strtok (NULL, " "); } - /*printf ("id=%s octets=%d flags=%d\n", c->fpr, c->octets, c->flags);*/ if (!*ctx) *ctx = c; else { @@ -111,6 +118,8 @@ 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; @@ -121,36 +130,22 @@ } -/* Always use the $users temp folder. */ -static FILE* -w32_tmpfile (char *tmp, DWORD tmplen) -{ - char id[16]; - - if (!GetTempPath (tmplen-17, tmp)) - return NULL; - if (tmp[strlen (tmp)-1] != '\\') - strcat (tmp, "\\"); - _snprintf (id, sizeof (id)-1, "%lu", GetTickCount ()); - strcat (tmp, id); - return fopen (tmp, "w+b"); -} - - static int parse_attr_data (const char *keyid, attr_list_t *list) { gpgme_error_t err; - FILE *tmp; - char *status, tmpnam[MAX_PATH+1]; + FILE *tmp; BYTE *data; - DWORD ndata; + char *status, tmpnam[MAX_PATH+1]; + DWORD ndata = 0; err = gpg_get_photoid_data (keyid, &status, &data, &ndata); if (err) return err; - if (ndata > 0 && (tmp = w32_tmpfile (tmpnam, MAX_PATH)) != NULL) { + 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); @@ -187,6 +182,8 @@ 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) { @@ -230,6 +227,8 @@ 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; } @@ -255,8 +254,83 @@ } +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) { @@ -281,13 +355,14 @@ gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ 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->pkt.signature->numrevkeys == 0) @@ -301,7 +376,9 @@ goto next; c->gloflags.has_desig_rev = 1; } - if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) { + if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 && c != NULL) { + if (c->sym_prefs) /* only use the prefs from the primary uid. */ + goto next; sym_prefs = gpg_parse_sig_subpkt (pkt->pkt.signature->hashed, SIGSUBPKT_PREF_SYM, &nsym); if (!sym_prefs) @@ -316,7 +393,7 @@ 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); } } @@ -358,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; @@ -379,10 +456,15 @@ c2 = c->next; gpgme_key_release (c->key); c->key = NULL; + 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 (c); + free_native_uids (&c->uids); + safe_free (c); } safe_free (ctx); } @@ -418,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) @@ -531,8 +613,8 @@ for (c = ctx->item; c; c = c->next) { if (c->flags != 0) { *r_status = c->flags; - c->flags = 0; *r_obj = c; + c->flags = 0; return 0; } } @@ -554,7 +636,7 @@ err = gpgme_new (&gctx); if (err) return err; - gpgme_set_keylist_mode (gctx, GPGME_KEYLIST_MODE_SIGS); + 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) @@ -589,7 +671,15 @@ } 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; } @@ -612,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; } @@ -646,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); @@ -659,6 +751,7 @@ 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); @@ -696,7 +789,7 @@ pos++; p = (unsigned char*)calloc (1, pos+1); if (!p) - abort (); + BUG (0); memcpy (p, prefs, pos); return p; } @@ -717,7 +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); - c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); + if (c_sec->sym_prefs) + c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); c->pubpart = c_sec; c->pubpart->key = key; } @@ -768,8 +862,12 @@ } 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; @@ -783,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; +}