1 |
/* wptFileStatDlg.cpp - (File Manager) Show file statistics |
2 |
* Copyright (C) 2002, 2003, 2005 Timo Schulz |
3 |
* |
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 |
|
28 |
#include "resource.h" |
29 |
#include "wptTypes.h" |
30 |
#include "wptNLS.h" |
31 |
#include "wptGPG.h" |
32 |
#include "wptCommonCtl.h" |
33 |
#include "wptContext.h" |
34 |
#include "wptDlgs.h" |
35 |
#include "wptErrors.h" |
36 |
#include "wptW32API.h" |
37 |
#include "openpgp.h" |
38 |
|
39 |
static const char* |
40 |
get_pubkey_algo (int algo) |
41 |
{ |
42 |
if (algo == 17) |
43 |
return "DSA"; |
44 |
else if (is_ELGAMAL (algo)) |
45 |
return "ELG"; |
46 |
else if (is_RSA (algo)) |
47 |
return "RSA"; |
48 |
return NULL; |
49 |
} |
50 |
|
51 |
|
52 |
const char* |
53 |
get_symkey_algo (int algo) |
54 |
{ |
55 |
switch (algo) { |
56 |
case 0: return "PLAINTEXT"; |
57 |
case 1: return "IDEA"; |
58 |
case 2: return "3DES"; |
59 |
case 3: return "CAST5"; |
60 |
case 4: return "BLOWFISH"; |
61 |
case 5: return "RESERVED"; |
62 |
case 6: return "RESERVED"; |
63 |
case 7: return "AES"; |
64 |
case 8: return "AES-192"; |
65 |
case 9: return "AES-256"; |
66 |
default: break; |
67 |
} |
68 |
return "UNKNOWN"; |
69 |
} |
70 |
|
71 |
|
72 |
static const char* |
73 |
get_digest_algo (int algo) |
74 |
{ |
75 |
switch (algo) { |
76 |
case 0: return "NONE"; |
77 |
case 1: return "MD5"; |
78 |
case 2: return "SHA1"; |
79 |
case 3: return "RMD160"; |
80 |
case 4: return "RESERVED"; |
81 |
case 5: return "MD2"; |
82 |
case 6: return "RESERVED"; |
83 |
case 7: return "RESERVED"; |
84 |
case 8: return "SHA256"; |
85 |
case 9: return "SHA384"; |
86 |
case 10:return "SHA512"; |
87 |
default: break; |
88 |
} |
89 |
return "UNKNOWN"; |
90 |
} |
91 |
|
92 |
|
93 |
static const char* |
94 |
get_compress_algo (int algo) |
95 |
{ |
96 |
switch (algo) { |
97 |
case 0: return "NONE"; |
98 |
case 1: return "ZIB"; |
99 |
case 2: return "ZLIB"; |
100 |
case 3: return "BZIP2"; |
101 |
default: break; |
102 |
} |
103 |
return "UNKNOWN"; |
104 |
} |
105 |
|
106 |
|
107 |
static const char* |
108 |
get_s2k_algo (int algo) |
109 |
{ |
110 |
switch (algo) { |
111 |
case 0: return "SIMPLE"; |
112 |
case 1: return "SALTED"; |
113 |
case 3: return "ITER+SALTED"; |
114 |
default:break; |
115 |
} |
116 |
return "INVALID"; |
117 |
} |
118 |
|
119 |
|
120 |
static const char* |
121 |
get_timestring (long timestamp) |
122 |
{ |
123 |
static char ts[64]; |
124 |
struct tm *ltm; |
125 |
|
126 |
ltm = localtime (×tamp); |
127 |
_snprintf(ts, sizeof ts -1, "%04d-%02d-%02d", |
128 |
ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday); |
129 |
return ts; |
130 |
} |
131 |
|
132 |
|
133 |
/* List all PGP packets from the file @file. |
134 |
Return value: number of packets. */ |
135 |
static int |
136 |
do_list_packets (HWND list, const char *file) |
137 |
{ |
138 |
PACKET *pkt; |
139 |
gpg_iobuf_t inp = NULL; |
140 |
armor_filter_context_t afx; |
141 |
size_t n=0; |
142 |
int rc = 0; |
143 |
char inf[1024]; |
144 |
|
145 |
if (!list || !file) { |
146 |
log_debug ("do_list_packets: !list || !file"); |
147 |
return -1; |
148 |
} |
149 |
|
150 |
inp = gpg_iobuf_open (file); |
151 |
if (!inp) { |
152 |
log_box (_("File Manager"), MB_ERR, "%s: %s", file, |
153 |
winpt_strerror (WPTERR_FILE_OPEN)); |
154 |
return -1; |
155 |
} |
156 |
gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */ |
157 |
if (gpg_use_armor_filter (inp)) { |
158 |
memset (&afx, 0, sizeof (afx)); |
159 |
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
160 |
} |
161 |
pkt = (PACKET *)calloc(1, sizeof *pkt); |
162 |
gpg_init_packet (pkt); |
163 |
while ((rc = gpg_parse_packet (inp, pkt)) != -1) { |
164 |
switch (pkt->pkttype) { |
165 |
case PKT_PUBKEY_ENC: |
166 |
{PKT_pubkey_enc *enc = pkt->pkt.pubkey_enc; n++; |
167 |
if (!enc) |
168 |
break; |
169 |
_snprintf( inf, sizeof(inf)-1, |
170 |
"public key encryted packet: version %d, algo %s, keyid 0x%08X", |
171 |
enc->version, get_pubkey_algo(enc->pubkey_algo), enc->keyid[1]); |
172 |
listbox_add_string (list, inf);} |
173 |
break; |
174 |
|
175 |
case PKT_SYMKEY_ENC: |
176 |
{PKT_symkey_enc *enc = pkt->pkt.symkey_enc; n++; |
177 |
if (!enc) |
178 |
break; |
179 |
_snprintf (inf, sizeof(inf)-1, |
180 |
"symmetric key encrypted packet: version %d, cipher %s, s2k %s, hash %s", |
181 |
enc->version, get_symkey_algo(enc->cipher_algo), |
182 |
get_s2k_algo (enc->s2k.mode), get_digest_algo(enc->s2k.hash_algo)); |
183 |
listbox_add_string (list, inf); } |
184 |
break; |
185 |
|
186 |
case PKT_ENCRYPTED: |
187 |
case PKT_ENCRYPTED_MDC: |
188 |
{PKT_encrypted *enc = pkt->pkt.encrypted; n++; |
189 |
if (!enc) |
190 |
break; |
191 |
_snprintf( inf, sizeof(inf)-1, |
192 |
"encrypted data packet: mdc method %d, length %d", |
193 |
enc->mdc_method, enc->len ); |
194 |
listbox_add_string( list, inf ); } |
195 |
break; |
196 |
|
197 |
case PKT_PUBLIC_KEY: |
198 |
case PKT_PUBLIC_SUBKEY: |
199 |
{PKT_public_key *pk = pkt->pkt.public_key; n++; |
200 |
if (!pk) |
201 |
break; |
202 |
_snprintf( inf, sizeof(inf)-1, |
203 |
"public key packet: version %d, algo %s, created %s", |
204 |
pk->version, get_pubkey_algo(pk->pubkey_algo), |
205 |
get_timestring(pk->timestamp)); |
206 |
listbox_add_string( list, inf ); } |
207 |
break; |
208 |
|
209 |
case PKT_SECRET_KEY: |
210 |
case PKT_SECRET_SUBKEY: |
211 |
{PKT_secret_key *sk = pkt->pkt.secret_key; n++; |
212 |
if (!sk) |
213 |
break; |
214 |
_snprintf( inf, sizeof(inf)-1, |
215 |
"secret key packet: version %d, algo %s, created %s mode %d", |
216 |
sk->version, get_pubkey_algo( sk->pubkey_algo ), |
217 |
get_timestring( sk->timestamp ), |
218 |
sk->protect.s2k.mode ); |
219 |
listbox_add_string( list, inf ); } |
220 |
break; |
221 |
|
222 |
|
223 |
case PKT_SIGNATURE: |
224 |
{PKT_signature *sig = pkt->pkt.signature; n++; |
225 |
if (!sig) |
226 |
break; |
227 |
_snprintf( inf, sizeof(inf)-1, |
228 |
"signature packet: version %d, algo %s, keyid 0x%08X, created %s", |
229 |
sig->version, get_pubkey_algo(sig->pubkey_algo), sig->keyid[1], |
230 |
get_timestring(sig->timestamp)); |
231 |
listbox_add_string( list, inf ); } |
232 |
break; |
233 |
|
234 |
|
235 |
case PKT_USER_ID: |
236 |
{PKT_user_id *id = pkt->pkt.user_id; n++; |
237 |
if (!id) |
238 |
break; |
239 |
_snprintf( inf, sizeof(inf)-1, "user id packet: %s", id->name ); |
240 |
listbox_add_string( list, inf ); } |
241 |
break; |
242 |
|
243 |
case PKT_COMPRESSED: |
244 |
{PKT_compressed *zip = pkt->pkt.compressed; n++; |
245 |
if (!zip) |
246 |
break; |
247 |
_snprintf( inf, sizeof(inf)-1, "compressed packet: algo %s, length %d", |
248 |
get_compress_algo(zip->algorithm), zip->len ); |
249 |
listbox_add_string( list, inf ); } |
250 |
break; |
251 |
|
252 |
default: |
253 |
break; |
254 |
} |
255 |
gpg_free_packet (pkt); |
256 |
gpg_init_packet (pkt); |
257 |
} |
258 |
gpg_iobuf_close (inp); |
259 |
safe_free (pkt); |
260 |
return n; |
261 |
} |
262 |
|
263 |
|
264 |
/* Dialog box procedure for showing file status. */ |
265 |
BOOL CALLBACK |
266 |
file_stat_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
267 |
{ |
268 |
const char *file; |
269 |
int n; |
270 |
|
271 |
switch (msg) { |
272 |
case WM_INITDIALOG: |
273 |
#ifndef LANG_DE |
274 |
SetWindowText (dlg, _("File Status")); |
275 |
#endif |
276 |
file = (const char*)lparam; |
277 |
if (file == NULL) |
278 |
dlg_fatal_error( dlg, "Could not get dialog state!" ); |
279 |
n = do_list_packets (GetDlgItem (dlg, IDC_FILE_STAT_LIST), file); |
280 |
if (n < 1) { |
281 |
msg_box (dlg, _("No valid OpenPGP data found."), _("File Status"), MB_ERR); |
282 |
EndDialog (dlg, FALSE); |
283 |
} |
284 |
SetForegroundWindow (dlg); |
285 |
break; |
286 |
|
287 |
case WM_SYSCOMMAND: |
288 |
if (LOWORD (wparam) == SC_CLOSE) |
289 |
EndDialog (dlg, TRUE); |
290 |
return TRUE; |
291 |
|
292 |
case WM_COMMAND: |
293 |
switch (LOWORD (wparam)) { |
294 |
case IDOK: |
295 |
EndDialog (dlg, TRUE); |
296 |
break; |
297 |
} |
298 |
break; |
299 |
} |
300 |
|
301 |
return FALSE; |
302 |
} |