--- trunk/Src/wptKeylist.cpp 2006/01/26 10:17:17 167 +++ trunk/Src/wptKeylist.cpp 2006/06/28 06:59:30 236 @@ -83,7 +83,7 @@ int j; for (j = 0; j < items; j++) { - if (!strcmp (keyid, ka[j].keyid )) + if (!strcmp (keyid, ka[j].keyid)) return 1; } return 0; @@ -155,7 +155,7 @@ /* Return the self signature of the key @keyid. If first is set, the first self sig will be returned. */ gpgme_key_sig_t -get_selfsig (gpgme_user_id_t uid, const char *keyid, int first) +get_selfsig (gpgme_key_sig_t sigs, const char *keyid, int first) { gpgme_key_sig_t s, self_sig=NULL; long timestamp=0; @@ -164,7 +164,7 @@ if (strlen (keyid) == 8) off = 8; - for (s = uid->signatures; s; s = s->next) { + for (s = sigs; s; s = s->next) { if (!strcmp (s->keyid+off, keyid) && s->timestamp > timestamp) { self_sig = s; timestamp = s->timestamp; @@ -176,7 +176,6 @@ } - const char* get_key_algo (gpgme_key_t key, int keyidx) { @@ -195,9 +194,18 @@ strcpy (alg, get_key_pubalgo (key->subkeys->pubkey_algo)); n = count_subkeys (key); if (n > 1) { - k = get_nth_key (key, n-1); + do { + k = get_nth_key (key, --n); + if (k->revoked || k->expired) + continue; + else + break; + } while (n > 0); subalg = get_key_pubalgo (k->pubkey_algo); - _snprintf (algo_id, DIM (algo_id)-1, "%s/%s", alg, subalg); + if (k == key->subkeys) + _snprintf (algo_id, DIM (algo_id)-1, "%s", subalg); + else + _snprintf (algo_id, DIM (algo_id)-1, "%s/%s", alg, subalg); return algo_id; } return get_key_pubalgo (key->subkeys->pubkey_algo); @@ -216,6 +224,7 @@ dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1); if (dat) return dat; + /* Fallback if locate date conversion failed. */ warp = localtime (×tamp); _snprintf (timebuf, sizeof timebuf - 1, "%04d-%02d-%02d", warp->tm_year + 1900, warp->tm_mon + 1, warp->tm_mday); @@ -303,7 +312,10 @@ case GPGME_PK_DSA: return "DSA"; case GPGME_PK_ELG: case GPGME_PK_ELG_E: return "ELG"; - case GPGME_PK_RSA: return "RSA"; + case 0: /* XXX: do we still need this?? */ + case GPGME_PK_RSA: + case GPGME_PK_RSA_S: + case GPGME_PK_RSA_E: return "RSA"; default: return "???"; } return "???"; @@ -341,6 +353,25 @@ } +/* Extract the key ID from the fingerprint. + A long ID will be converted into a short ID. */ +const char* +get_keyid_from_fpr (const char *fpr) +{ + if (!fpr) + return "????????"; + if (strlen (fpr) == 40) + fpr += 32; + else if (strlen (fpr) == 32) + fpr += 24; + else if (strlen (fpr) == 16) + fpr += 8; + else + return "????????"; + return fpr; +} + + const char* get_key_trust2 (gpgme_key_t key, int val, int uididx, int listmode) { @@ -355,8 +386,9 @@ case GPGME_VALIDITY_MARGINAL: return _("Marginal"); case GPGME_VALIDITY_FULL: - case GPGME_VALIDITY_ULTIMATE: return _("Full"); + case GPGME_VALIDITY_ULTIMATE: + return _("Ultimate"); } return ""; } @@ -469,13 +501,16 @@ static int CALLBACK keylist_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby) { + struct keycache_s *aa, *bb; gpgme_key_t a, b; int cmpresult = 0; - a = (gpgme_key_t)first; - b = (gpgme_key_t)second; - if (!a || !b) + aa = (struct keycache_s *)first; + bb = (struct keycache_s *)second; + if (!aa || !bb) BUG (NULL); + a = aa->key; + b = bb->key; switch (sortby & ~KEYLIST_SORT_DESC) { case KEY_SORT_USERID: @@ -562,33 +597,34 @@ HICON ico[2]; listview_ctrl_t lv; listview_column_t col; - int j, n = 0; - int rc = 0; - - rc = listview_new (&lv); - if (rc) - return rc; + int j, n = 0, ext_chk = 0; - lv->ctrl = ctrl; - if ((mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN)) { + listview_new (&lv, ctrl); + if (mode & KEYLIST_ENCRYPT_MIN) { col = klist_enc; - n = (DIM(klist_enc) -1); + n = (DIM (klist_enc) -1); + ext_chk = 1; } else if ((mode & KEYLIST_SIGN)) { col = klist_enc; - n = (DIM(klist_enc) - 1) - 1; + n = (DIM (klist_enc) - 1) - 1; + ext_chk = 1; } else { col = klist; - n = (DIM(klist) - 1); + n = (DIM (klist) - 1); } for (j = 0; j < n; j++) listview_add_column (lv, &col[j]); listview_set_ext_style (lv); + if (ext_chk) + listview_set_chkbox_style (lv); ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_PUBKEY); ico[1] = LoadIcon (glob_hinst, (LPCTSTR)IDI_KEYPAIR); - listview_set_image_list (lv, ico, 2); + ico[2] = LoadIcon (glob_hinst, (LPCTSTR)IDI_SORT_DOWNARROW); + ico[3] = LoadIcon (glob_hinst, (LPCTSTR)IDI_SORT_UPARROW); + listview_set_image_list (lv, 22, 14, ico, 4); listview_del_all_items (lv); *r_lv = lv; @@ -602,22 +638,23 @@ { gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); gpgme_key_t key, skey; - const char * keyid; + struct keycache_s *c; + const char *keyid; if (pubkc && seckc) { gpg_keycache_rewind (pubkc); - while (!gpg_keycache_next_key (pubkc, 0, &key)) { + while (!gpg_keycache_next_key2 (pubkc, 0, &c, &key)) { keyid = key->subkeys->keyid; if (keyid && !gpg_keycache_find_key (seckc, keyid, 0, &skey)) - keylist_add_key (lv, mode, key); + keylist_add_key (lv, mode, c, key); } } else if (pubkc) { gpg_keycache_rewind (pubkc); while (!err) { - err = gpg_keycache_next_key (pubkc, 0, &key); + err = gpg_keycache_next_key2 (pubkc, 0, &c, &key); if (!err) - keylist_add_key (lv, mode, key); + keylist_add_key (lv, mode, c, key); } } } @@ -637,7 +674,7 @@ return NULL; keylist_load_keycache (lv, mode, pubkc, seckc); keylist_sort (lv, sortby); - if ((mode & KEYLIST_ENCRYPT) || (mode & KEYLIST_ENCRYPT_MIN)) + if (mode & KEYLIST_ENCRYPT_MIN) keylist_add_groups (lv); return lv; } @@ -683,7 +720,8 @@ static int -do_addkey (listview_ctrl_t lv, gpgme_key_t key, int uididx, int keyidx, int list) +do_addkey (listview_ctrl_t lv, struct keycache_s *ctx, gpgme_key_t key, + int uididx, int keyidx, int list) { LV_ITEM lvi; gpgme_user_id_t u; @@ -700,21 +738,21 @@ key->uids->name, key->subkeys->keyid); return 0; } - - if (listview_add_item2 (lv, " ", (void *)key)) + + if (listview_add_item2 (lv, " ", (void *)ctx)) return WPTERR_GENERAL; - attr = key->uids->uid; + attr = ctx->uids->uid; memset (&lvi, 0, sizeof lvi); lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE; lvi.pszText = (char *)attr; lvi.iImage = find_secret_key (key)? 1 : 0; - lvi.lParam = (LPARAM )key; + lvi.lParam = (LPARAM )ctx; if (ListView_SetItem (lv->ctrl, &lvi) == FALSE) return WPTERR_GENERAL; if (uididx == -1) { /* request the primary user-id of the key. */ - attr = key->uids->uid; + attr = ctx->uids->uid; uididx = 0; } else { @@ -728,17 +766,12 @@ attr = _("Invalid User ID"); listview_add_sub_item (lv, 0, idx++, attr); } - else { - char *uid = utf8_to_wincp (attr, strlen (attr)); - if (uid) { - listview_add_sub_item (lv, 0, idx++, uid); - free (uid); - } - } + else + listview_add_sub_item (lv, 0, idx++, attr); k = get_nth_key (key, keyidx); if (k && k->keyid) { _snprintf (fmt, sizeof fmt -1, "0x%s", k->keyid + 8); - listview_add_sub_item( lv, 0, idx++, fmt ); + listview_add_sub_item (lv, 0, idx++, fmt); } if (list > 0) { key_attr = find_secret_key (key); @@ -793,7 +826,7 @@ int i; for (i=0; i < listview_count_items (lv, 0); i++) { - key = (gpgme_key_t)listview_get_item2 (lv, i); + key = km_get_key_ptr (lv, i, NULL); if (!key) continue; switch (col) { @@ -830,21 +863,19 @@ /* Update the listview item at position @pos with the data from the key @key. */ void -keylist_upd_key (listview_ctrl_t lv, int pos, gpgme_key_t key) +keylist_upd_key (listview_ctrl_t lv, int pos, + struct keycache_s *ctx, gpgme_key_t key) { const char *s; - char *uid, *p; + char *p; char tmp[32]; - listview_set_item2 (lv, pos, (void *)key); - /* the only mode we support is KYLIST_LIST in the Key Manager */ + listview_set_item2 (lv, pos, (void *)ctx); + /* the only mode we support is KEYLIST_LIST in the Key Manager */ - s = key->uids->uid; - if (s) { - uid = utf8_to_wincp2 (s); - listview_add_sub_item (lv, pos, KM_COL_UID, uid); - free (uid); - } + s = ctx->uids->uid; + if (s) + listview_add_sub_item (lv, pos, KM_COL_UID, s); s = key->subkeys->keyid; if (s) { @@ -881,7 +912,8 @@ int -keylist_add_key (listview_ctrl_t lv, int mode, gpgme_key_t key) +keylist_add_key (listview_ctrl_t lv, int mode, + struct keycache_s *ctx, gpgme_key_t key) { int uids, rc = 0, i; gpgme_subkey_t k; @@ -898,22 +930,22 @@ if (mode & KEYLIST_ALL) { uids = count_userids (key); - rc = do_addkey (lv, key, uids, i, 0); + rc = do_addkey (lv, ctx, key, uids, i, 0); if (rc) return rc; } else if (mode & KEYLIST_LIST) - return do_addkey (lv, key, -1, i, 1); + return do_addkey (lv, ctx, key, -1, i, 1); else if (mode & KEYLIST_ENCRYPT) { if (k->can_encrypt && key_is_useable (k)) { if (mode & KEYLIST_FLAG_FILE) { - rc = do_addkey (lv, key, -1, i, -1); + rc = do_addkey (lv, ctx, key, -1, i, -1); if (rc) return rc; } else { for (uids = 0; uids < count_userids (key); uids++) { - rc = do_addkey (lv, key, uids, i, -1); + rc = do_addkey (lv, ctx, key, uids, i, -1); if (rc) return rc; } @@ -923,7 +955,7 @@ else if (mode & KEYLIST_ENCRYPT_MIN) { if( k->can_encrypt && key_is_useable (k)) { - rc = do_addkey (lv, key, -1, i, -1); + rc = do_addkey (lv, ctx, key, -1, i, -1); return rc; } } @@ -931,7 +963,7 @@ if (k->can_sign && find_secret_key (key) && key_is_useable (k)) { - rc = do_addkey (lv, key, -1, i, -1); + rc = do_addkey (lv, ctx, key, -1, i, -1); if (rc) return rc; } @@ -972,6 +1004,7 @@ keylist_get_recipients (listview_ctrl_t lv, int *r_force_trust, int *r_count) { key_array_s *ka = NULL; + keycache_s *c; gpgme_key_t *keybuf, key; int count = 0, force_trust = 0; int n, j, ka_pos = 0, rc = 0; @@ -983,18 +1016,18 @@ if (!ka) BUG (NULL); - keybuf = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t)); + keybuf = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t)); if (!keybuf) BUG (NULL); for (j = 0; j < n; j++) { if (listview_get_item_state (lv, j) || n == 1) { - key = (gpgme_key_t)listview_get_item2 (lv, j); + key = km_get_key_ptr (lv, j, &c); if (!key) BUG (0); if (!key_check_validity (key) && !key_array_search (ka, ka_pos, key->subkeys->keyid)) { - char *warn = new char[512+strlen (key->uids->uid) + 1]; + char *warn = new char[512+strlen (c->uids->uid) + 1]; if (!warn) BUG (0); sprintf (warn, @@ -1002,7 +1035,7 @@ "named in the user ID. If you *really* know what you are\n" "doing, you may answer the next question with yes\n" "\n" - "Use \"%s\" anyway?"), key->uids->uid); + "Use \"%s\" anyway?"), c->uids->uid); if (reg_prefs.always_trust) rc = IDYES; else @@ -1050,6 +1083,7 @@ gpgme_key_t* keylist_enum_recipients (listview_ctrl_t lv, int listype, int *r_count) { + struct keycache_s *c; gpgme_key_t *rset; gpgme_key_t key; int i, n, id, k_pos=0; @@ -1057,21 +1091,19 @@ n = listview_count_items (lv, 0); if (!n) return 0; - rset = (gpgme_key_t*)calloc (n, sizeof (gpgme_key_t)); + rset = (gpgme_key_t*)calloc (n+1, sizeof (gpgme_key_t)); if (!rset) BUG (NULL); for (i = 0; i < n; i++) { if (!listview_get_item_state (lv, i)) continue; - key = (gpgme_key_t)listview_get_item2 (lv, i); - if (!key) - BUG (0); + key = km_get_key_ptr (lv, i, &c); switch (listype) { case KEYLIST_LIST: if (keylist_get_keyflags (key) & KEYFLAG_REVOKED) { id = printf_box (_("Recipients"), MB_INFO|MB_YESNO, _("KeyID %s.\nDo you really want to export a revoked key?"), - key->uids->uid); + c->uids->uid); if (id == IDNO) continue; } @@ -1132,7 +1164,7 @@ if (key->disabled || !key_is_useable (key->subkeys)) continue; - uid = utf8_to_wincp (id, strlen (id)); + uid = utf8_to_native (id); size = strlen (uid) + strlen (keyid) + 32; inf = new char[size+1]; if (!inf)