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