1 |
/* wptSigList.cpp - Listview for key signatures |
2 |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* Copyright (C) 2005, 2006 g10 Code GmbH |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU General Public License |
9 |
* as published by the Free Software Foundation; either version 2 |
10 |
* of the License, or (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
24 |
#endif |
25 |
|
26 |
#include <windows.h> |
27 |
#include <time.h> |
28 |
|
29 |
#include "resource.h" |
30 |
#include "wptGPG.h" |
31 |
#include "wptCommonCtl.h" |
32 |
#include "wptKeylist.h" |
33 |
#include "wptNLS.h" |
34 |
#include "wptErrors.h" |
35 |
#include "wptTypes.h" |
36 |
#include "wptW32API.h" |
37 |
#include "wptVersion.h" |
38 |
#include "wptContext.h" |
39 |
|
40 |
|
41 |
/* Is the given signature an user-id signature? */ |
42 |
#define IS_UID_CERT(_class) ((_class) >= 0x10 && (_class) <= 0x13) |
43 |
|
44 |
|
45 |
/* Create a signature list control in @lv with the parent window |
46 |
given in @ctrl. |
47 |
Return value: 0 on success. */ |
48 |
static void |
49 |
siglist_build (listview_ctrl_t *lv, HWND ctrl) |
50 |
{ |
51 |
struct listview_ctrl_s *c; |
52 |
struct listview_column_s implist[] = { |
53 |
{0, 240, (char *)_("User ID")}, |
54 |
{1, 50, (char *)_("Valid")}, |
55 |
{2, 44, (char *)_("Class")}, |
56 |
{3, 68, (char *)_("Creation")}, |
57 |
{4, 80, (char *)_("Key ID")}, |
58 |
{5, 68, (char *)_("Expiration")}, |
59 |
{6, 56, (char *)_("Algorithm")}, |
60 |
{0, 0, NULL} |
61 |
}; |
62 |
int j; |
63 |
|
64 |
listview_new (&c, ctrl); |
65 |
for (j=0; implist[j].fieldname != NULL; j++) |
66 |
listview_add_column (c, &implist[j]); |
67 |
listview_set_ext_style (c); |
68 |
listview_del_all_items (c); |
69 |
*lv = c; |
70 |
} |
71 |
|
72 |
|
73 |
/* Delete the signature control in @lv. */ |
74 |
void |
75 |
siglist_delete (listview_ctrl_t lv) |
76 |
{ |
77 |
if (lv) { |
78 |
listview_release (lv); |
79 |
} |
80 |
} |
81 |
|
82 |
|
83 |
/* Indent the string in @old with the level @ilvl. |
84 |
Return value: indented string. */ |
85 |
static char* |
86 |
indent_string (const char *old, int ilvl) |
87 |
{ |
88 |
char *p; |
89 |
int i; |
90 |
|
91 |
p = new char[strlen (old) + 1 + ilvl]; |
92 |
if (!p) |
93 |
BUG (NULL); |
94 |
for (i = 0; i < ilvl; i++) |
95 |
p[i] = ' '; |
96 |
strcpy (p+i, old); |
97 |
return p; |
98 |
} |
99 |
|
100 |
|
101 |
/* Add an item at the given pos @pos with the opaque param @ks. */ |
102 |
int |
103 |
add_keysig_item (listview_ctrl_t ctx, int pos, gpgme_key_sig_t ks) |
104 |
{ |
105 |
LV_ITEM lvi; |
106 |
|
107 |
memset (&lvi, 0, sizeof lvi); |
108 |
lvi.iItem = pos; |
109 |
lvi.lParam = (LPARAM)ks; |
110 |
lvi.mask = LVIF_PARAM; |
111 |
if (ListView_InsertItem (ctx->ctrl, &lvi) != pos) |
112 |
return -1; |
113 |
return 0; |
114 |
} |
115 |
|
116 |
|
117 |
/* Add an userid signature @ks from the userid @uid to the sig list |
118 |
control @lv. |
119 |
Return value: 0 on success. */ |
120 |
static int |
121 |
siglist_add_key (listview_ctrl_t lv, struct native_uid_s *uid, |
122 |
gpgme_key_sig_t ks, int pos) |
123 |
{ |
124 |
struct winpt_key_s key; |
125 |
const char *attr; |
126 |
const char *s; |
127 |
char t[128]; |
128 |
int no_uid = 0; |
129 |
int rc = 0; |
130 |
|
131 |
if (ks && !IS_UID_CERT (ks->sig_class)) |
132 |
return 0; |
133 |
|
134 |
rc = add_keysig_item (lv, pos, ks); |
135 |
if (rc) |
136 |
return rc; |
137 |
|
138 |
attr = NULL; |
139 |
if (!uid) { |
140 |
memset (&key, 0, sizeof (key)); |
141 |
winpt_get_pubkey (ks->keyid, &key); |
142 |
if (key.ext != NULL) |
143 |
attr = key.ext->uids->uid; |
144 |
} |
145 |
else |
146 |
attr = uid->uid; |
147 |
if (attr && strlen (attr)) { |
148 |
char *p = uid? m_strdup (attr) : indent_string (attr, 2); |
149 |
listview_add_sub_item (lv, pos, SL_COL_UID, p); |
150 |
free_if_alloc (p); |
151 |
} |
152 |
else { |
153 |
listview_add_sub_item (lv, pos, SL_COL_UID, _(" user ID not found")); |
154 |
no_uid = 1; |
155 |
} |
156 |
|
157 |
/* Return immediately for root items. */ |
158 |
if (uid) |
159 |
return 0; |
160 |
|
161 |
switch (gpgme_err_code (ks->status)) { |
162 |
case GPG_ERR_NO_ERROR: s = "YES"; break; |
163 |
case GPG_ERR_NO_PUBKEY: s = "NOKEY"; break; |
164 |
case GPG_ERR_BAD_SIGNATURE:s = "NO"; break; |
165 |
default: s = "ERROR"; break; |
166 |
} |
167 |
if (no_uid) |
168 |
s = "NOKEY"; |
169 |
listview_add_sub_item (lv, pos, SL_COL_VALID, s); |
170 |
|
171 |
switch (ks->sig_class) { |
172 |
case 0x10: strcpy (t, " "); break; |
173 |
case 0x11: strcpy (t, "1"); break; |
174 |
case 0x12: strcpy (t, "2"); break; |
175 |
case 0x13: strcpy (t, "3"); break; |
176 |
} |
177 |
|
178 |
strcat (t, " "); |
179 |
if (!ks->exportable) |
180 |
strcat (t, "L"); |
181 |
/* XXX: if gpgme supports non-revocable status, add 'R' */ |
182 |
listview_add_sub_item (lv, pos, SL_COL_CLASS, t); |
183 |
|
184 |
s = get_key_created (ks->timestamp); |
185 |
listview_add_sub_item (lv, pos, SL_COL_CREATE, s); |
186 |
|
187 |
if (ks->keyid && strlen (ks->keyid) == 16) { |
188 |
_snprintf (t, DIM (t) -1, "0x%s", ks->keyid + 8); |
189 |
listview_add_sub_item (lv, pos, SL_COL_KEYID, t); |
190 |
} |
191 |
|
192 |
if (ks->expires > 0) { |
193 |
s = get_key_created (ks->expires); |
194 |
listview_add_sub_item (lv, pos, SL_COL_EXPIRE, s); |
195 |
} |
196 |
|
197 |
attr = get_key_pubalgo (ks->pubkey_algo); |
198 |
if (attr) |
199 |
listview_add_sub_item (lv, pos, 6, (char *)attr); |
200 |
|
201 |
return 0; |
202 |
} |
203 |
|
204 |
|
205 |
/* Load a signature list with the signatures of the key with |
206 |
the keyID @keyid. The parent window is in @ctrl. |
207 |
Return value is the handle to the listview control. */ |
208 |
listview_ctrl_t |
209 |
siglist_load (HWND ctrl, const char *keyid) |
210 |
{ |
211 |
gpgme_key_sig_t s; |
212 |
struct native_uid_s *u; |
213 |
winpt_key_s key; |
214 |
listview_ctrl_t lv; |
215 |
int pos = 0; |
216 |
int rc = 0; |
217 |
|
218 |
siglist_build (&lv, ctrl); |
219 |
memset (&key, 0, sizeof (key)); |
220 |
if (winpt_get_pubkey (keyid, &key)) |
221 |
BUG (NULL); |
222 |
|
223 |
for (u=key.ext->uids; u; u = u->next) { |
224 |
siglist_add_key (lv, u, NULL, pos++); |
225 |
for (s = u->signatures; s; s = s->next) { |
226 |
if (!IS_UID_CERT (s->sig_class)) |
227 |
continue; |
228 |
rc = siglist_add_key (lv, NULL, s, pos++); |
229 |
if (rc) |
230 |
break; |
231 |
} |
232 |
} |
233 |
if (rc) { |
234 |
siglist_delete (lv); |
235 |
lv = NULL; |
236 |
} |
237 |
return lv; |
238 |
} |
239 |
|
240 |
|
241 |
/* Integer comparsion of @a and @b. |
242 |
Return values: same as in strcmp. */ |
243 |
static inline int |
244 |
int_cmp (int a, int b) |
245 |
{ |
246 |
if (a == b) return 0; |
247 |
else if (a > b) return 1; |
248 |
else return -1; |
249 |
return 0; |
250 |
} |
251 |
|
252 |
|
253 |
/* Sorting callback for the signature list. */ |
254 |
static int CALLBACK |
255 |
siglist_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby) |
256 |
{ |
257 |
gpgme_key_sig_t a, b; |
258 |
int cmpresult=0; |
259 |
|
260 |
a = (gpgme_key_sig_t)first; |
261 |
b = (gpgme_key_sig_t)second; |
262 |
if (!a || !b) |
263 |
return -1; /* this indicates that one item is a root item. */ |
264 |
|
265 |
switch (sortby) { |
266 |
case KEY_SORT_KEYID: |
267 |
cmpresult = strcmp (a->keyid, b->keyid); |
268 |
break; |
269 |
|
270 |
case KEY_SORT_CREATED: |
271 |
cmpresult = int_cmp (a->timestamp, b->timestamp); |
272 |
break; |
273 |
|
274 |
case KEY_SORT_ALGO: |
275 |
cmpresult = int_cmp (a->pubkey_algo, b->pubkey_algo); |
276 |
break; |
277 |
|
278 |
case KEY_SORT_VALIDITY: |
279 |
cmpresult = int_cmp (a->status, b->status); |
280 |
break; |
281 |
|
282 |
case SIG_SORT_EXPIRE: |
283 |
cmpresult = int_cmp (a->expires, a->expires); |
284 |
break; |
285 |
|
286 |
case SIG_SORT_CLASS: |
287 |
cmpresult = int_cmp (a->sig_class, b->sig_class); |
288 |
break; |
289 |
} |
290 |
return cmpresult; |
291 |
} |
292 |
|
293 |
|
294 |
/* Sort the signature list @sigl by @sortby. */ |
295 |
void |
296 |
siglist_sort (listview_ctrl_t sigl, int sortby) |
297 |
{ |
298 |
listview_sort_items (sigl, sortby, siglist_cmp_cb); |
299 |
} |