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