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