1 |
/* wptVerifyList.cpp - Listview for verifying signatures |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
3 |
* Copyright (C) 2005 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 "wptTypes.h" |
30 |
#include "wptGPG.h" |
31 |
#include "wptCommonCtl.h" |
32 |
#include "wptKeylist.h" |
33 |
#include "wptNLS.h" |
34 |
#include "wptErrors.h" |
35 |
#include "wptUTF8.h" |
36 |
#include "wptW32API.h" |
37 |
|
38 |
|
39 |
/* Extract the file name part out of the given path in @path. |
40 |
Return value: file part or NULL on error. */ |
41 |
static char* |
42 |
extract_filename (const char *path) |
43 |
{ |
44 |
char * fname, *p; |
45 |
int n, len = 0; |
46 |
|
47 |
p = strrchr (path, '\\'); |
48 |
if (!p) |
49 |
return m_strdup (path); |
50 |
n = p - path; |
51 |
len = strlen (path) - n; |
52 |
fname = new char[len+1]; |
53 |
if (!fname) |
54 |
BUG (NULL); |
55 |
memcpy (fname, path+n+1, len); |
56 |
fname[len] = '\0'; |
57 |
return fname; |
58 |
} |
59 |
|
60 |
|
61 |
/* String representaton of the time in @timestamp. |
62 |
Format YEAR-MON-DAY HOUR:MIN:SEC. |
63 |
Return value: time as formatted string. */ |
64 |
const char* |
65 |
strtimestamp (long timestamp) |
66 |
{ |
67 |
static char timebuf[64]; |
68 |
struct tm *warp; |
69 |
const char *dat; |
70 |
|
71 |
dat = get_locale_date (timestamp, timebuf, sizeof (timebuf)-1); |
72 |
warp = localtime (×tamp); |
73 |
if (!dat) |
74 |
_snprintf (timebuf, sizeof (timebuf) - 1, |
75 |
"%04d-%02d-%02d %02d:%02d:%02d", |
76 |
warp->tm_year+1900, warp->tm_mon+1, warp->tm_mday, |
77 |
warp->tm_hour, warp->tm_min, warp->tm_sec); |
78 |
else |
79 |
_snprintf (timebuf, sizeof (timebuf)-1, |
80 |
"%s %02d:%02d:%02d", dat, |
81 |
warp->tm_hour, warp->tm_min, warp->tm_sec); |
82 |
|
83 |
return timebuf; |
84 |
} |
85 |
|
86 |
|
87 |
/* Build a verify signature list control. With the parent window |
88 |
from @ctrl and the mod given in @fm_mode. @lv contains the |
89 |
new control on success. |
90 |
Return value: 0 on success. */ |
91 |
int |
92 |
verlist_build (listview_ctrl_t *lv, HWND ctrl, int fm_mode) |
93 |
{ |
94 |
int j, rc = 0; |
95 |
struct listview_ctrl_s * c; |
96 |
struct listview_column_s fm_verlist[] = { |
97 |
{0, 100, (char *)_("Name") }, |
98 |
{1, 120, (char *)_("Status") }, |
99 |
{2, 115, (char *)_("Signed") }, |
100 |
{3, 58, (char *)_("Trust") }, |
101 |
{4, 160, (char *)_("User ID") }, |
102 |
{5, 0, NULL} |
103 |
|
104 |
}; |
105 |
struct listview_column_s verlist[] = { |
106 |
{0, 140, (char *)_("Status") }, |
107 |
{1, 120, (char *)_("Signed") }, |
108 |
{2, 58, (char *)_("Trust") }, |
109 |
{3, 80, (char *)_("Key ID" )}, |
110 |
{4, 160, (char *)_("User ID") }, |
111 |
{5, 0, NULL} |
112 |
}; |
113 |
|
114 |
rc = listview_new (&c); |
115 |
if (rc) |
116 |
return rc; |
117 |
|
118 |
c->ctrl = ctrl; |
119 |
if( fm_mode ) { |
120 |
for( j = 0; fm_verlist[j].fieldname; j++ ) |
121 |
listview_add_column( c, &fm_verlist[j] ); |
122 |
} |
123 |
else { |
124 |
for( j = 0; verlist[j].fieldname; j++ ) |
125 |
listview_add_column( c, &verlist[ j ] ); |
126 |
} |
127 |
listview_set_ext_style( c ); |
128 |
*lv = c; |
129 |
return 0; |
130 |
} |
131 |
|
132 |
|
133 |
/* Delete the given verify control in @lv. */ |
134 |
void |
135 |
verlist_delete (listview_ctrl_t lv) |
136 |
{ |
137 |
if (lv) { |
138 |
listview_release (lv); |
139 |
} |
140 |
} |
141 |
|
142 |
|
143 |
/* Add the given signature in @sig to the verify control @lv. |
144 |
Return value: 0 on success. */ |
145 |
int |
146 |
verlist_add_sig (listview_ctrl_t lv, gpgme_signature_t sig) |
147 |
{ |
148 |
gpgme_key_t key = NULL; |
149 |
const char * attr; |
150 |
u32 key_attr; |
151 |
char keyid[32+1]; |
152 |
char *uid = NULL; |
153 |
|
154 |
if (listview_add_item (lv, " ")) |
155 |
return WPTERR_GENERAL; |
156 |
|
157 |
get_pubkey (sig->fpr, &key); |
158 |
|
159 |
if (sig->summary == 0 && gpg_err_code (sig->status) == GPG_ERR_NO_ERROR) |
160 |
attr = get_gpg_sigstat (GPGME_SIGSUM_GREEN); |
161 |
else |
162 |
attr = get_gpg_sigstat (sig->summary); |
163 |
if (attr) |
164 |
listview_add_sub_item (lv, 0, 0, (char *)attr); |
165 |
|
166 |
attr = strtimestamp (sig->timestamp); |
167 |
listview_add_sub_item (lv, 0, 1, (char *)attr); |
168 |
|
169 |
attr = _("Unknown"); |
170 |
if (key) { |
171 |
key_attr = key->uids->validity; |
172 |
attr = get_key_trust2 (NULL, key_attr, 0, 0); |
173 |
} |
174 |
listview_add_sub_item (lv, 0, 2, (char *)attr); |
175 |
|
176 |
attr = sig->fpr; |
177 |
if (!attr || strlen (attr) < 8) |
178 |
listview_add_sub_item (lv, 0, 3, "????????"); |
179 |
else { |
180 |
if (strlen (attr) == 40) |
181 |
attr += 32; |
182 |
else if (strlen (attr) == 32) |
183 |
attr += 24; |
184 |
else |
185 |
attr += 8; |
186 |
_snprintf (keyid, sizeof keyid -1, "0x%s", attr); |
187 |
listview_add_sub_item (lv, 0, 3, keyid); |
188 |
} |
189 |
|
190 |
if (!key) { |
191 |
attr = _("Invalid User ID"); |
192 |
listview_add_sub_item( lv, 0, 4, (char *)attr ); |
193 |
} |
194 |
else { |
195 |
attr = key->uids->name; |
196 |
uid = utf8_to_native (attr); |
197 |
if (uid) { |
198 |
listview_add_sub_item( lv, 0, 4, (char *)uid ); |
199 |
free (uid); |
200 |
} |
201 |
} |
202 |
|
203 |
return 0; |
204 |
} |
205 |
|
206 |
|
207 |
/* Add the given file signature in @log to the verify control @lv. |
208 |
Return value: 0 on success. */ |
209 |
int |
210 |
verlist_add_sig_log (listview_ctrl_t lv, file_sig_ctx_t log) |
211 |
{ |
212 |
gpgme_signature_t sig = log->sig; |
213 |
gpgme_key_t key = NULL; |
214 |
const char *attr; |
215 |
char t[64], *name; |
216 |
|
217 |
if (listview_add_item (lv, "")) { |
218 |
log_debug ("verlist_add_sig_log: listview_add_item() failed.\n"); |
219 |
return WPTERR_GENERAL; |
220 |
} |
221 |
|
222 |
get_pubkey (sig->fpr, &key); |
223 |
|
224 |
name = extract_filename (log->file); |
225 |
if (name) |
226 |
listview_add_sub_item (lv, 0, 0, name); |
227 |
else |
228 |
listview_add_sub_item (lv, 0, 0, log->file); |
229 |
free_if_alloc (name); |
230 |
|
231 |
if (sig->summary == 0 && gpg_err_code (sig->status) == GPG_ERR_NO_ERROR) |
232 |
attr = get_gpg_sigstat (GPGME_SIGSUM_GREEN); |
233 |
else |
234 |
attr = get_gpg_sigstat (sig->summary); |
235 |
if (attr) |
236 |
listview_add_sub_item (lv, 0, 1, attr); |
237 |
|
238 |
if (sig->timestamp > 0) { |
239 |
attr = strtimestamp (sig->timestamp); |
240 |
listview_add_sub_item (lv, 0, 2, attr); |
241 |
} |
242 |
else |
243 |
listview_add_sub_item (lv, 0, 2, "No time"); |
244 |
|
245 |
attr = _("Unknown"); |
246 |
if (key) |
247 |
attr = get_key_trust2 (NULL, key->uids->validity, 0, 0); |
248 |
listview_add_sub_item (lv, 0, 3, attr); |
249 |
|
250 |
attr = sig->fpr; |
251 |
if (!log->use_uid && strlen (attr) == 40) { |
252 |
_snprintf (t, sizeof (t)-1, "0x%s", attr + 32); |
253 |
listview_add_sub_item (lv, 0, 4, t); |
254 |
} |
255 |
else if (!log->use_uid && strlen( attr ) == 32) { |
256 |
_snprintf (t, sizeof (t)-1, "0x%s", attr + 24); |
257 |
listview_add_sub_item (lv, 0, 4, t); |
258 |
} |
259 |
else if (log->user_id) { |
260 |
char *p = utf8_to_native (log->user_id); |
261 |
if (p) { |
262 |
listview_add_sub_item (lv, 0, 4, p); |
263 |
free (p); |
264 |
} |
265 |
} |
266 |
return 0; |
267 |
} |