1 |
twoaday |
225 |
/* wptFileStatDlg.cpp - List the status of an OpenPGP file |
2 |
|
|
* Copyright (C) 2002, 2003, 2005, 2006 Timo Schulz |
3 |
werner |
36 |
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
|
|
|
24 |
|
|
#include <windows.h> |
25 |
|
|
#include <time.h> |
26 |
|
|
#include <sys/types.h> |
27 |
twoaday |
310 |
#include <assert.h> |
28 |
werner |
36 |
|
29 |
werner |
47 |
#include "resource.h" |
30 |
werner |
36 |
#include "wptTypes.h" |
31 |
|
|
#include "wptGPG.h" |
32 |
|
|
#include "wptCommonCtl.h" |
33 |
|
|
#include "wptContext.h" |
34 |
|
|
#include "wptErrors.h" |
35 |
|
|
#include "wptW32API.h" |
36 |
twoaday |
225 |
#include "wptUTF8.h" |
37 |
werner |
36 |
#include "openpgp.h" |
38 |
|
|
|
39 |
twoaday |
271 |
/* Make sure that the list won't consume too much memory. */ |
40 |
|
|
#define MAX_PKTS 1024 |
41 |
|
|
|
42 |
twoaday |
256 |
/* Return the date in locale format. */ |
43 |
twoaday |
225 |
const char* |
44 |
|
|
get_fmt_date (long timest) |
45 |
|
|
{ |
46 |
twoaday |
310 |
static char buf[128]; |
47 |
twoaday |
225 |
|
48 |
twoaday |
271 |
get_locale_date (timest, buf, DIM (buf)-1); |
49 |
twoaday |
225 |
return buf; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
|
53 |
twoaday |
256 |
/* Return humand readable public key algo. */ |
54 |
werner |
36 |
static const char* |
55 |
|
|
get_pubkey_algo (int algo) |
56 |
|
|
{ |
57 |
|
|
if (algo == 17) |
58 |
|
|
return "DSA"; |
59 |
|
|
else if (is_ELGAMAL (algo)) |
60 |
|
|
return "ELG"; |
61 |
|
|
else if (is_RSA (algo)) |
62 |
|
|
return "RSA"; |
63 |
|
|
return NULL; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
|
67 |
twoaday |
256 |
/* Return human readable symmetric algo. */ |
68 |
werner |
36 |
const char* |
69 |
|
|
get_symkey_algo (int algo) |
70 |
|
|
{ |
71 |
|
|
switch (algo) { |
72 |
|
|
case 0: return "PLAINTEXT"; |
73 |
|
|
case 1: return "IDEA"; |
74 |
|
|
case 2: return "3DES"; |
75 |
|
|
case 3: return "CAST5"; |
76 |
|
|
case 4: return "BLOWFISH"; |
77 |
twoaday |
226 |
case 7: return "AES128"; |
78 |
|
|
case 8: return "AES192"; |
79 |
|
|
case 9: return "AES256"; |
80 |
twoaday |
178 |
default: break; |
81 |
werner |
36 |
} |
82 |
|
|
return "UNKNOWN"; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
twoaday |
256 |
/* Return human readable hash algo. */ |
87 |
werner |
36 |
static const char* |
88 |
|
|
get_digest_algo (int algo) |
89 |
|
|
{ |
90 |
|
|
switch (algo) { |
91 |
|
|
case 0: return "NONE"; |
92 |
|
|
case 1: return "MD5"; |
93 |
|
|
case 2: return "SHA1"; |
94 |
|
|
case 3: return "RMD160"; |
95 |
|
|
case 5: return "MD2"; |
96 |
|
|
case 8: return "SHA256"; |
97 |
|
|
case 9: return "SHA384"; |
98 |
|
|
case 10:return "SHA512"; |
99 |
twoaday |
178 |
default: break; |
100 |
werner |
36 |
} |
101 |
|
|
return "UNKNOWN"; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
|
105 |
twoaday |
256 |
/* Return human readable compress algo. */ |
106 |
werner |
36 |
static const char* |
107 |
|
|
get_compress_algo (int algo) |
108 |
|
|
{ |
109 |
|
|
switch (algo) { |
110 |
|
|
case 0: return "NONE"; |
111 |
|
|
case 1: return "ZIB"; |
112 |
|
|
case 2: return "ZLIB"; |
113 |
|
|
case 3: return "BZIP2"; |
114 |
twoaday |
178 |
default: break; |
115 |
werner |
36 |
} |
116 |
|
|
return "UNKNOWN"; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
twoaday |
256 |
/* Return human string to key algo. */ |
121 |
werner |
36 |
static const char* |
122 |
twoaday |
178 |
get_s2k_algo (int algo) |
123 |
|
|
{ |
124 |
|
|
switch (algo) { |
125 |
|
|
case 0: return "SIMPLE"; |
126 |
|
|
case 1: return "SALTED"; |
127 |
|
|
case 3: return "ITER+SALTED"; |
128 |
|
|
default:break; |
129 |
|
|
} |
130 |
|
|
return "INVALID"; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
|
134 |
twoaday |
225 |
/* List all OpenPGP packets from the file @file. |
135 |
werner |
36 |
Return value: number of packets. */ |
136 |
|
|
static int |
137 |
|
|
do_list_packets (HWND list, const char *file) |
138 |
|
|
{ |
139 |
|
|
PACKET *pkt; |
140 |
|
|
gpg_iobuf_t inp = NULL; |
141 |
|
|
armor_filter_context_t afx; |
142 |
|
|
size_t n=0; |
143 |
|
|
int rc = 0; |
144 |
twoaday |
225 |
char inf[1024], *p; |
145 |
werner |
36 |
|
146 |
|
|
if (!list || !file) { |
147 |
twoaday |
271 |
log_debug ("do_list_packets: invalid parameters\n"); |
148 |
werner |
36 |
return -1; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
inp = gpg_iobuf_open (file); |
152 |
|
|
if (!inp) { |
153 |
twoaday |
256 |
log_box (_("File Manager"), MB_ERR, "%s: %s", file, |
154 |
werner |
36 |
winpt_strerror (WPTERR_FILE_OPEN)); |
155 |
|
|
return -1; |
156 |
|
|
} |
157 |
twoaday |
256 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
158 |
werner |
36 |
if (gpg_use_armor_filter (inp)) { |
159 |
|
|
memset (&afx, 0, sizeof (afx)); |
160 |
|
|
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
161 |
|
|
} |
162 |
|
|
pkt = (PACKET *)calloc(1, sizeof *pkt); |
163 |
|
|
gpg_init_packet (pkt); |
164 |
|
|
while ((rc = gpg_parse_packet (inp, pkt)) != -1) { |
165 |
|
|
switch (pkt->pkttype) { |
166 |
|
|
case PKT_PUBKEY_ENC: |
167 |
|
|
{PKT_pubkey_enc *enc = pkt->pkt.pubkey_enc; n++; |
168 |
|
|
if (!enc) |
169 |
|
|
break; |
170 |
twoaday |
271 |
_snprintf (inf, DIM(inf)-1, |
171 |
werner |
36 |
"public key encryted packet: version %d, algo %s, keyid 0x%08X", |
172 |
|
|
enc->version, get_pubkey_algo(enc->pubkey_algo), enc->keyid[1]); |
173 |
|
|
listbox_add_string (list, inf);} |
174 |
|
|
break; |
175 |
|
|
|
176 |
|
|
case PKT_SYMKEY_ENC: |
177 |
|
|
{PKT_symkey_enc *enc = pkt->pkt.symkey_enc; n++; |
178 |
|
|
if (!enc) |
179 |
|
|
break; |
180 |
twoaday |
271 |
_snprintf (inf, DIM(inf)-1, |
181 |
twoaday |
178 |
"symmetric key encrypted packet: version %d, cipher %s, s2k %s, hash %s", |
182 |
twoaday |
225 |
enc->version, get_symkey_algo (enc->cipher_algo), |
183 |
|
|
get_s2k_algo (enc->s2k.mode), |
184 |
|
|
get_digest_algo (enc->s2k.hash_algo)); |
185 |
twoaday |
178 |
listbox_add_string (list, inf); } |
186 |
werner |
36 |
break; |
187 |
|
|
|
188 |
|
|
case PKT_ENCRYPTED: |
189 |
|
|
case PKT_ENCRYPTED_MDC: |
190 |
|
|
{PKT_encrypted *enc = pkt->pkt.encrypted; n++; |
191 |
|
|
if (!enc) |
192 |
|
|
break; |
193 |
twoaday |
271 |
_snprintf (inf, DIM(inf)-1, |
194 |
twoaday |
225 |
"encrypted data packet: mdc method %d, length %d", |
195 |
|
|
enc->mdc_method, enc->len); |
196 |
|
|
listbox_add_string (list, inf); } |
197 |
werner |
36 |
break; |
198 |
|
|
|
199 |
|
|
case PKT_PUBLIC_KEY: |
200 |
|
|
case PKT_PUBLIC_SUBKEY: |
201 |
|
|
{PKT_public_key *pk = pkt->pkt.public_key; n++; |
202 |
|
|
if (!pk) |
203 |
|
|
break; |
204 |
twoaday |
271 |
_snprintf( inf, DIM(inf)-1, |
205 |
werner |
36 |
"public key packet: version %d, algo %s, created %s", |
206 |
twoaday |
225 |
pk->version, get_pubkey_algo (pk->pubkey_algo), |
207 |
|
|
get_fmt_date (pk->timestamp)); |
208 |
|
|
listbox_add_string (list, inf); } |
209 |
werner |
36 |
break; |
210 |
|
|
|
211 |
|
|
case PKT_SECRET_KEY: |
212 |
|
|
case PKT_SECRET_SUBKEY: |
213 |
|
|
{PKT_secret_key *sk = pkt->pkt.secret_key; n++; |
214 |
|
|
if (!sk) |
215 |
|
|
break; |
216 |
twoaday |
271 |
_snprintf( inf, DIM(inf)-1, |
217 |
werner |
36 |
"secret key packet: version %d, algo %s, created %s mode %d", |
218 |
twoaday |
225 |
sk->version, get_pubkey_algo (sk->pubkey_algo), |
219 |
|
|
get_fmt_date (sk->timestamp), |
220 |
|
|
sk->protect.s2k.mode); |
221 |
|
|
listbox_add_string (list, inf); } |
222 |
werner |
36 |
break; |
223 |
twoaday |
225 |
|
224 |
werner |
36 |
case PKT_SIGNATURE: |
225 |
|
|
{PKT_signature *sig = pkt->pkt.signature; n++; |
226 |
|
|
if (!sig) |
227 |
|
|
break; |
228 |
twoaday |
271 |
_snprintf( inf, DIM(inf)-1, |
229 |
werner |
36 |
"signature packet: version %d, algo %s, keyid 0x%08X, created %s", |
230 |
|
|
sig->version, get_pubkey_algo(sig->pubkey_algo), sig->keyid[1], |
231 |
twoaday |
225 |
get_fmt_date (sig->timestamp)); |
232 |
|
|
listbox_add_string (list, inf); } |
233 |
werner |
36 |
break; |
234 |
|
|
|
235 |
|
|
|
236 |
|
|
case PKT_USER_ID: |
237 |
|
|
{PKT_user_id *id = pkt->pkt.user_id; n++; |
238 |
|
|
if (!id) |
239 |
|
|
break; |
240 |
twoaday |
225 |
p = utf8_to_native (id->name); |
241 |
twoaday |
271 |
_snprintf (inf, DIM (inf)-1, "user id packet: %s", p); |
242 |
twoaday |
225 |
free_if_alloc (p); |
243 |
|
|
listbox_add_string (list, inf); } |
244 |
werner |
36 |
break; |
245 |
|
|
|
246 |
|
|
case PKT_COMPRESSED: |
247 |
|
|
{PKT_compressed *zip = pkt->pkt.compressed; n++; |
248 |
|
|
if (!zip) |
249 |
|
|
break; |
250 |
twoaday |
271 |
_snprintf (inf, DIM(inf)-1, |
251 |
twoaday |
225 |
"compressed packet: algo %s, length %d", |
252 |
|
|
get_compress_algo(zip->algorithm), zip->len); |
253 |
|
|
listbox_add_string (list, inf); } |
254 |
werner |
36 |
break; |
255 |
twoaday |
178 |
|
256 |
twoaday |
65 |
default: |
257 |
|
|
break; |
258 |
werner |
36 |
} |
259 |
|
|
gpg_free_packet (pkt); |
260 |
|
|
gpg_init_packet (pkt); |
261 |
twoaday |
271 |
if (n > MAX_PKTS) |
262 |
|
|
break; |
263 |
werner |
36 |
} |
264 |
|
|
gpg_iobuf_close (inp); |
265 |
|
|
safe_free (pkt); |
266 |
|
|
return n; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
|
270 |
|
|
/* Dialog box procedure for showing file status. */ |
271 |
|
|
BOOL CALLBACK |
272 |
|
|
file_stat_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
273 |
|
|
{ |
274 |
|
|
const char *file; |
275 |
|
|
|
276 |
|
|
switch (msg) { |
277 |
twoaday |
256 |
case WM_INITDIALOG: |
278 |
werner |
36 |
file = (const char*)lparam; |
279 |
twoaday |
310 |
assert (file != NULL); |
280 |
twoaday |
271 |
if (do_list_packets (GetDlgItem (dlg, IDC_FILE_STAT_LIST), file) < 1) { |
281 |
|
|
msg_box (dlg, _("No valid OpenPGP data found."), |
282 |
|
|
_("File Status"), MB_ERR); |
283 |
werner |
36 |
EndDialog (dlg, FALSE); |
284 |
twoaday |
225 |
return TRUE; |
285 |
werner |
36 |
} |
286 |
twoaday |
256 |
SetWindowText (dlg, _("File Status")); |
287 |
werner |
36 |
SetForegroundWindow (dlg); |
288 |
|
|
break; |
289 |
|
|
|
290 |
|
|
case WM_COMMAND: |
291 |
|
|
switch (LOWORD (wparam)) { |
292 |
|
|
case IDOK: |
293 |
|
|
EndDialog (dlg, TRUE); |
294 |
|
|
break; |
295 |
twoaday |
213 |
|
296 |
|
|
case IDCANCEL: |
297 |
|
|
EndDialog (dlg, FALSE); |
298 |
|
|
break; |
299 |
werner |
36 |
} |
300 |
|
|
break; |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
return FALSE; |
304 |
|
|
} |