1 |
werner |
36 |
/* wptKeyManager.cpp - Handy functions for the Key Manager dialog |
2 |
|
|
* Copyright (C) 2001-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 |
twoaday |
121 |
|
22 |
werner |
36 |
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include <config.h> |
24 |
|
|
#endif |
25 |
|
|
|
26 |
|
|
#include <windows.h> |
27 |
|
|
#include <commctrl.h> |
28 |
|
|
#include <stdio.h> |
29 |
|
|
|
30 |
|
|
#include "gpgme.h" |
31 |
werner |
47 |
#include "resource.h" |
32 |
werner |
36 |
#include "wptTypes.h" |
33 |
|
|
#include "wptW32API.h" |
34 |
|
|
#include "wptVersion.h" |
35 |
|
|
#include "wptCommonCtl.h" |
36 |
|
|
#include "wptNLS.h" |
37 |
|
|
#include "wptErrors.h" |
38 |
|
|
#include "wptContext.h" |
39 |
|
|
#include "wptGPG.h" |
40 |
|
|
#include "wptKeylist.h" |
41 |
|
|
#include "wptFileManager.h" |
42 |
|
|
#include "wptDlgs.h" |
43 |
|
|
#include "wptKeyserver.h" |
44 |
|
|
#include "wptKeyManager.h" |
45 |
|
|
#include "wptKeylist.h" |
46 |
|
|
#include "wptHTTP.h" |
47 |
|
|
#include "wptKeyEdit.h" |
48 |
|
|
#include "wptImport.h" |
49 |
werner |
48 |
#include "wptCrypto.h" |
50 |
werner |
36 |
|
51 |
|
|
|
52 |
|
|
/* Return a user friendly key representation in @buf of |
53 |
|
|
the key given by @keyid. */ |
54 |
|
|
static void |
55 |
|
|
key_get_clip_info (const char *keyid, char *buf, size_t buflen) |
56 |
|
|
{ |
57 |
|
|
gpgme_key_t pk; |
58 |
|
|
|
59 |
|
|
if (get_pubkey (keyid, &pk)) |
60 |
|
|
BUG (NULL); |
61 |
|
|
_snprintf (buf, buflen-1, |
62 |
|
|
"pub %04d%s/%s %s %s\r\n" |
63 |
|
|
" Primary key fingerprint: %s\r\n", |
64 |
|
|
pk->subkeys->length, |
65 |
|
|
get_key_pubalgo (pk->subkeys->pubkey_algo), |
66 |
|
|
pk->subkeys->keyid+8, |
67 |
|
|
get_key_created (pk->subkeys->timestamp), |
68 |
|
|
pk->uids->uid, |
69 |
|
|
get_key_fpr (pk)); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
|
73 |
|
|
#if 0 |
74 |
|
|
/* Quoted the user-id given by @uid. If @uid is already |
75 |
|
|
quoted @uid is returned without any modifications. |
76 |
|
|
Return value: quoted @uid. */ |
77 |
|
|
char* |
78 |
|
|
km_quote_uid (const char *uid) |
79 |
|
|
{ |
80 |
|
|
char *q; |
81 |
|
|
|
82 |
|
|
if (*uid == '"' && uid[strlen (uid)-1] == '"') |
83 |
|
|
return m_strdup (uid); |
84 |
|
|
q = new char[strlen (uid) + 4]; |
85 |
|
|
if (!q) |
86 |
|
|
BUG (NULL); |
87 |
|
|
_snprintf (q, strlen (uid) + 3, "\"%s\"", uid); |
88 |
|
|
return q; |
89 |
|
|
} |
90 |
|
|
#endif |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/* Check if list view @lv contains a secret key at position @pos. |
94 |
|
|
If utrust is valid, set it to 1 if the key is valid -1 otherwise. |
95 |
|
|
Return value: 1 normal key, 2 smart card key. */ |
96 |
|
|
int |
97 |
|
|
km_check_for_seckey (listview_ctrl_t lv, int pos, int *utrust) |
98 |
|
|
{ |
99 |
|
|
char t[32], t2[64]; |
100 |
|
|
int type = 0; |
101 |
|
|
|
102 |
|
|
if (utrust) |
103 |
|
|
*utrust = 0; |
104 |
|
|
listview_get_item_text (lv, pos, 5, t, DIM (t)-1); |
105 |
|
|
listview_get_item_text (lv, pos, 2, t2, DIM (t2)-1); |
106 |
|
|
if (!strcmp (t2, "pub/sec")) |
107 |
|
|
type = 1; |
108 |
|
|
else if (!strcmp (t2, "pub/crd")) |
109 |
|
|
type = 2; |
110 |
|
|
if ((strstr (t, "Expired") || strstr (t, "Revoked")) && utrust) |
111 |
|
|
*utrust = -1; |
112 |
|
|
else if (stristr (t, "Ultimate") && utrust) |
113 |
|
|
*utrust = 1; |
114 |
|
|
return type; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
/* Check if the key at position @pos is protected with a passwd. */ |
119 |
|
|
int |
120 |
|
|
km_check_if_protected (listview_ctrl_t lv, int pos) |
121 |
|
|
{ |
122 |
|
|
gpgme_key_t key; |
123 |
|
|
winpt_key_s k; |
124 |
|
|
|
125 |
|
|
key = (gpgme_key_t)listview_get_item2 (lv, pos); |
126 |
|
|
if (!key) |
127 |
|
|
return 1; /* assume yes */ |
128 |
|
|
winpt_get_pubkey (key->subkeys->keyid, &k); |
129 |
|
|
return k.is_protected; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
|
133 |
twoaday |
121 |
/* Check if the key has a good status. |
134 |
|
|
Return value: 0 on success. */ |
135 |
werner |
36 |
int |
136 |
|
|
km_check_key_status (listview_ctrl_t lv, int pos) |
137 |
|
|
{ |
138 |
|
|
int flags = km_get_key_status (lv, pos); |
139 |
|
|
|
140 |
|
|
if (flags & KM_FLAG_EXPIRED) { |
141 |
|
|
msg_box (lv->ctrl, _("This key has expired!\n" |
142 |
|
|
"Key check failed."), _("Key Manager"), MB_ERR); |
143 |
|
|
return -1; |
144 |
|
|
} |
145 |
|
|
else if (flags & KM_FLAG_REVOKED) { |
146 |
|
|
msg_box (lv->ctrl, _("This key has been revoked by its owner!\n" |
147 |
|
|
"Key check failed."), _("Key Manager"), MB_ERR); |
148 |
|
|
return -1; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
return 0; |
152 |
twoaday |
121 |
} |
153 |
werner |
36 |
|
154 |
|
|
|
155 |
twoaday |
121 |
/* Return all key flags ORed. */ |
156 |
werner |
36 |
int |
157 |
|
|
km_get_key_status (listview_ctrl_t lv, int pos) |
158 |
|
|
{ |
159 |
|
|
gpgme_key_t key; |
160 |
|
|
int flags = 0; |
161 |
|
|
|
162 |
|
|
if (pos == -1) |
163 |
|
|
return 0; |
164 |
|
|
key = (gpgme_key_t)listview_get_item2 (lv, pos); |
165 |
|
|
if (key == NULL) |
166 |
|
|
return 0; |
167 |
|
|
|
168 |
|
|
if (key->expired) |
169 |
|
|
flags |= KM_FLAG_EXPIRED; |
170 |
|
|
if (key->revoked) |
171 |
|
|
flags |= KM_FLAG_REVOKED; |
172 |
|
|
if (key->disabled) |
173 |
|
|
flags |= KM_FLAG_DISABLED; |
174 |
|
|
return flags; |
175 |
twoaday |
121 |
} |
176 |
werner |
36 |
|
177 |
|
|
|
178 |
|
|
/* Interface to enable or disable a key (@enable = 1 then enable). |
179 |
|
|
The key is retrieved from a list control @lv at the pos @pos. */ |
180 |
|
|
int |
181 |
|
|
km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable) |
182 |
|
|
{ |
183 |
|
|
GpgKeyEdit *ke; |
184 |
|
|
gpgme_error_t err; |
185 |
|
|
char keyid[32]; |
186 |
|
|
|
187 |
|
|
listview_get_item_text (lv, pos, 1, keyid, DIM (keyid)-1); |
188 |
|
|
|
189 |
|
|
ke = new GpgKeyEdit (keyid); |
190 |
|
|
if (!ke) |
191 |
|
|
BUG (NULL); |
192 |
|
|
|
193 |
|
|
err = enable? ke->enable () : ke->disable (); |
194 |
twoaday |
121 |
if (!err) |
195 |
werner |
36 |
show_msg (dlg, 1500, _("Key status changed.")); |
196 |
|
|
else |
197 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
198 |
|
|
delete ke; |
199 |
|
|
return err? WPTERR_GENERAL : 0; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
/* Create a string that contain all keyids from |
205 |
|
|
the key list @rset separated by a space. */ |
206 |
|
|
char* |
207 |
|
|
gpg_keylist_to_pattern (gpgme_key_t *rset, int n) |
208 |
|
|
{ |
209 |
|
|
char *p; |
210 |
|
|
int i; |
211 |
|
|
|
212 |
|
|
if (!n) |
213 |
|
|
return NULL; |
214 |
|
|
p = (char *)calloc (1, n*(16+1)+n+2); |
215 |
|
|
if (!p) |
216 |
|
|
BUG (NULL); |
217 |
|
|
for (i=0; i < n; i++) { |
218 |
|
|
strcat (p, rset[i]->subkeys->keyid); |
219 |
|
|
strcat (p, " "); |
220 |
|
|
} |
221 |
|
|
return p; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
|
225 |
|
|
/* Export the keys given in @rset to the clipboard. |
226 |
|
|
Return value: 0 on success. */ |
227 |
|
|
static gpgme_error_t |
228 |
|
|
gpg_clip_export (gpgme_key_t *rset, int n) |
229 |
|
|
{ |
230 |
|
|
gpgme_error_t err = 0; |
231 |
|
|
gpgme_ctx_t ctx = NULL; |
232 |
|
|
gpgme_data_t keydata = NULL; |
233 |
|
|
char *patt=NULL; |
234 |
|
|
|
235 |
|
|
err = gpgme_new (&ctx); |
236 |
|
|
if (err) |
237 |
|
|
return err; |
238 |
|
|
gpgme_set_armor (ctx, 1); |
239 |
|
|
err = gpgme_data_new (&keydata); |
240 |
|
|
if (err) |
241 |
|
|
goto leave; |
242 |
|
|
|
243 |
|
|
patt = gpg_keylist_to_pattern (rset, n); |
244 |
|
|
if (!patt) { |
245 |
|
|
err = gpg_error (GPG_ERR_ENOMEM); |
246 |
|
|
goto leave; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
err = gpgme_op_export (ctx, patt, 0, keydata); |
250 |
|
|
if (err) |
251 |
|
|
goto leave; |
252 |
|
|
|
253 |
|
|
gpg_data_release_and_set_clipboard (keydata, 1); |
254 |
|
|
|
255 |
|
|
leave: |
256 |
|
|
if (patt) |
257 |
|
|
free (patt); |
258 |
|
|
gpgme_release (ctx); |
259 |
|
|
return err; |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
|
263 |
|
|
/* Export the selected keys in @lv to the clipboard. */ |
264 |
|
|
int |
265 |
|
|
km_clip_export (HWND dlg, listview_ctrl_t lv) |
266 |
|
|
{ |
267 |
|
|
gpgme_error_t err; |
268 |
|
|
gpgme_key_t *rset; |
269 |
|
|
char buf[256]; |
270 |
|
|
int n=0; |
271 |
|
|
int rc=0; |
272 |
|
|
|
273 |
|
|
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
274 |
|
|
if (!n) { |
275 |
twoaday |
121 |
msg_box (dlg, _("No key was selected for export."), |
276 |
|
|
_("Key Manager"), MB_ERR); |
277 |
|
|
rc = WPTERR_GENERAL; |
278 |
werner |
36 |
goto leave; |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
err = gpg_clip_export (rset, n); |
282 |
|
|
if (err) { |
283 |
|
|
msg_box( dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
284 |
|
|
rc = WPTERR_GENERAL; |
285 |
|
|
goto leave; |
286 |
|
|
} |
287 |
|
|
if (n == 1) { |
288 |
|
|
key_get_clip_info (rset[0]->subkeys->keyid, buf, sizeof (buf)-1); |
289 |
|
|
set_clip_text2 (NULL, buf, strlen (buf), 0); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
293 |
|
|
|
294 |
|
|
leave: |
295 |
|
|
free (rset); |
296 |
|
|
return rc; |
297 |
twoaday |
121 |
} |
298 |
werner |
36 |
|
299 |
|
|
|
300 |
|
|
/* Export the selected secret key from @lv into @fname. |
301 |
|
|
It is only allowed to export a single secret key. */ |
302 |
|
|
int |
303 |
|
|
km_privkey_export (HWND dlg, listview_ctrl_t lv, const char *fname) |
304 |
|
|
{ |
305 |
|
|
gpgme_key_t *rset; |
306 |
|
|
gpgme_error_t err; |
307 |
|
|
int n = 0; |
308 |
|
|
|
309 |
|
|
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
310 |
|
|
if (!n) { |
311 |
|
|
msg_box (dlg, _("No key was selected for export."), |
312 |
|
|
_("Key Manager"), MB_ERR); |
313 |
|
|
return WPTERR_GENERAL; |
314 |
|
|
} |
315 |
|
|
if (n > 1) { |
316 |
|
|
msg_box (dlg, _("Only one secret key can be exported."), |
317 |
|
|
_("Key Manager"), MB_ERR); |
318 |
|
|
free (rset); |
319 |
|
|
return 0; /* we checked this before, so we just quit */ |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
err = gpg_export_seckey (rset[0]->subkeys->keyid, fname); |
323 |
|
|
if (err) |
324 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
325 |
|
|
else |
326 |
|
|
log_box (_("Key Manager"), MB_OK, |
327 |
|
|
_("Secret key successfully saved in '%s'."), fname); |
328 |
|
|
|
329 |
|
|
free (rset); |
330 |
|
|
return err? WPTERR_GENERAL : 0; |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
|
334 |
|
|
int |
335 |
|
|
km_file_export (HWND dlg, listview_ctrl_t lv, const char * fname) |
336 |
|
|
{ |
337 |
|
|
gpgme_key_t *rset; |
338 |
|
|
gpgme_data_t keydata; |
339 |
|
|
gpgme_error_t err; |
340 |
|
|
gpgme_ctx_t ctx; |
341 |
|
|
char *patt; |
342 |
|
|
int n; |
343 |
|
|
|
344 |
|
|
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
345 |
|
|
if (!n) { |
346 |
|
|
msg_box (dlg, _("No key was selected for export."), |
347 |
|
|
_("Key Manager"), MB_ERR); |
348 |
|
|
return WPTERR_GENERAL; |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
err = gpgme_data_new (&keydata); |
352 |
|
|
if (err) |
353 |
|
|
BUG (NULL); |
354 |
|
|
err = gpgme_new (&ctx); |
355 |
|
|
if (err) |
356 |
|
|
BUG (NULL); |
357 |
|
|
gpgme_set_armor (ctx, 1); |
358 |
|
|
|
359 |
|
|
/*gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);*/ |
360 |
|
|
patt = gpg_keylist_to_pattern (rset, n); |
361 |
|
|
|
362 |
twoaday |
121 |
err = gpgme_op_export (ctx, patt, 0, keydata); |
363 |
werner |
36 |
if( err ) { |
364 |
twoaday |
121 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
365 |
werner |
36 |
goto leave; |
366 |
|
|
} |
367 |
twoaday |
121 |
|
368 |
|
|
log_box (_("Key Manager"), MB_OK, |
369 |
|
|
_("Key(s) successfully saved in '%s'."), fname); |
370 |
werner |
36 |
|
371 |
|
|
leave: |
372 |
twoaday |
121 |
err = gpg_data_release_and_set_file (keydata, fname); |
373 |
werner |
36 |
if (err) |
374 |
|
|
log_box (_("Key Manager"), MB_OK, |
375 |
|
|
_("Could not save data to '%s'."), fname); |
376 |
|
|
gpgme_release (ctx); |
377 |
|
|
free (patt); |
378 |
|
|
return (int)err; |
379 |
twoaday |
121 |
} |
380 |
werner |
36 |
|
381 |
|
|
|
382 |
twoaday |
121 |
/* Read a dash escaped key from the clipboard |
383 |
|
|
unescape it and write it back. */ |
384 |
werner |
36 |
static int |
385 |
|
|
extract_dash_escaped_key (void) |
386 |
|
|
{ |
387 |
|
|
gpgme_data_t inp, plain; |
388 |
|
|
gpgme_error_t err; |
389 |
|
|
|
390 |
|
|
err = gpg_data_new_from_clipboard (&inp, 0); |
391 |
|
|
if (err) { |
392 |
twoaday |
121 |
msg_box (NULL, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
393 |
werner |
36 |
return -1; |
394 |
|
|
} |
395 |
|
|
gpg_data_extract_plaintext (inp, &plain); |
396 |
|
|
gpg_data_release_and_set_clipboard (plain, 0); |
397 |
|
|
gpgme_data_release (inp); |
398 |
|
|
|
399 |
|
|
return 0; |
400 |
twoaday |
121 |
} |
401 |
werner |
36 |
|
402 |
|
|
|
403 |
|
|
/* Import keys from the clipboard. */ |
404 |
|
|
int |
405 |
|
|
km_clip_import (HWND dlg) |
406 |
|
|
{ |
407 |
|
|
gpgme_error_t err; |
408 |
|
|
int pgptype; |
409 |
|
|
int id; |
410 |
|
|
int has_data = 0; |
411 |
|
|
|
412 |
|
|
if (!gpg_clip_istext_avail (&has_data) && !has_data) { |
413 |
twoaday |
121 |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_ISEMPTY), |
414 |
|
|
_("Key Manager"), MB_ERR); |
415 |
werner |
36 |
return WPTERR_CLIP_ISEMPTY; |
416 |
|
|
} |
417 |
|
|
err = gpg_clip_is_secured (&pgptype, &has_data); |
418 |
|
|
if (err) |
419 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
420 |
|
|
if (!has_data) { |
421 |
twoaday |
121 |
msg_box (dlg, _("No valid OpenPGP data found."), |
422 |
|
|
_("Key Manager"), MB_ERR); |
423 |
werner |
36 |
return WPTERR_GENERAL; |
424 |
|
|
} |
425 |
|
|
if (!(pgptype & PGP_PUBKEY) && !(pgptype & PGP_SECKEY)) { |
426 |
twoaday |
121 |
msg_box (dlg, _("No valid OpenPGP keys found."), |
427 |
|
|
_("Key Manager"), MB_ERR); |
428 |
werner |
36 |
return WPTERR_GENERAL; |
429 |
|
|
} |
430 |
|
|
if (pgptype & PGP_DASH_ESCAPED) { |
431 |
|
|
id = msg_box (dlg, _("The key you want to import is dash escacped.\n" |
432 |
|
|
"Do you want to extract the key?"), |
433 |
|
|
_("Key Manager"), MB_YESNO); |
434 |
|
|
if (id == IDYES) |
435 |
|
|
extract_dash_escaped_key (); |
436 |
|
|
else |
437 |
|
|
msg_box (dlg, _("Cannot import dash escaped OpenPGP keys."), |
438 |
|
|
_("Key Manager"), MB_INFO); |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
442 |
twoaday |
77 |
clip_import_dlg_proc, 0, |
443 |
werner |
36 |
_("Key Import"), IDS_WINPT_IMPORT); |
444 |
|
|
|
445 |
|
|
return 0; |
446 |
|
|
} |
447 |
|
|
|
448 |
|
|
|
449 |
|
|
/* Import a key from the http URL @url. */ |
450 |
|
|
int |
451 |
|
|
km_http_import (HWND dlg, const char *url) |
452 |
|
|
{ |
453 |
|
|
http_hd_t hd; |
454 |
|
|
FILE *fp; |
455 |
|
|
char *p; |
456 |
|
|
char tmpfile[500]; |
457 |
|
|
int statcode; |
458 |
|
|
int rc = 0; |
459 |
|
|
|
460 |
|
|
if (strncmp (url, "http://", 7)) { |
461 |
|
|
log_box (_("Key Import HTTP"), MB_ERR, _("Invalid HTTP URL: %s"), url); |
462 |
|
|
return WPTERR_GENERAL; |
463 |
|
|
} |
464 |
|
|
|
465 |
|
|
GetTempPath (sizeof (tmpfile)-128, tmpfile); |
466 |
|
|
p = make_filename (tmpfile, "winpt_file_http", "tmp"); |
467 |
|
|
if (!p) |
468 |
|
|
BUG (0); |
469 |
|
|
fp = fopen (p, "wb"); |
470 |
|
|
if (!fp) { |
471 |
|
|
free_if_alloc (p); |
472 |
twoaday |
121 |
log_box (_("Key Import HTTP"), MB_ERR, "%s: %s", p, |
473 |
|
|
winpt_strerror (WPTERR_FILE_CREAT)); |
474 |
werner |
36 |
return WPTERR_FILE_CREAT; |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
/* parse URL */ |
478 |
|
|
rc = http_send_request2 (url, &hd); |
479 |
|
|
if (!rc) |
480 |
|
|
rc = http_parse_response (hd, &statcode); |
481 |
|
|
if (!rc) |
482 |
|
|
rc = http_parse_data (hd, fp); |
483 |
|
|
http_hd_free (hd); |
484 |
|
|
fclose (fp); |
485 |
|
|
if (rc) { |
486 |
|
|
msg_box (dlg, winpt_strerror (rc), _("Key Import HTTP"), MB_ERR); |
487 |
|
|
rc = WPTERR_GENERAL; |
488 |
|
|
} |
489 |
|
|
km_file_import (dlg, p); |
490 |
werner |
48 |
remove (p); |
491 |
werner |
36 |
free_if_alloc (p); |
492 |
|
|
return rc; |
493 |
|
|
} |
494 |
|
|
|
495 |
|
|
|
496 |
|
|
/* Import a key from the given file @fname. |
497 |
|
|
On success an import statistics dialog is shown. */ |
498 |
|
|
int |
499 |
|
|
km_file_import (HWND dlg, const char *fname) |
500 |
|
|
{ |
501 |
|
|
gpgme_data_t keydata = NULL; |
502 |
|
|
gpgme_ctx_t ctx; |
503 |
|
|
gpgme_error_t err; |
504 |
|
|
fm_state_s fm_stat; |
505 |
|
|
gpgme_import_result_t res; |
506 |
|
|
|
507 |
|
|
memset (&fm_stat, 0, sizeof (fm_stat)); |
508 |
|
|
fm_stat.opaque = m_strdup (fname); |
509 |
|
|
|
510 |
|
|
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
511 |
|
|
file_import_dlg_proc, (LPARAM)&fm_stat, |
512 |
|
|
_("File Import"), IDS_WINPT_IMPORT); |
513 |
|
|
if (fm_stat.cancel == 1) { |
514 |
|
|
free_if_alloc (fm_stat.opaque); |
515 |
|
|
return WPTERR_GENERAL; |
516 |
|
|
} |
517 |
|
|
|
518 |
|
|
err = gpgme_new (&ctx); |
519 |
|
|
if (err) |
520 |
|
|
BUG (NULL); |
521 |
|
|
err = gpgme_data_new_from_file (&keydata, fname, 1); |
522 |
|
|
if (err) { |
523 |
|
|
msg_box (dlg, _("Could not read key-data from file."), |
524 |
|
|
_("Key Manager"), MB_ERR); |
525 |
|
|
goto leave; |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
SetCursor (LoadCursor (NULL, IDC_WAIT)); |
529 |
|
|
err = gpgme_op_import (ctx, keydata); |
530 |
|
|
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
531 |
|
|
if (err) { |
532 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
533 |
|
|
goto leave; |
534 |
|
|
} |
535 |
|
|
|
536 |
|
|
res = gpgme_op_import_result (ctx); |
537 |
|
|
if (res->new_revocations == 0 && fm_stat.import.revcert == 1) |
538 |
|
|
res->new_revocations = 1; |
539 |
|
|
if (res->secret_imported == 0 && fm_stat.import.has_seckey == 1) |
540 |
|
|
res->secret_imported = 1; |
541 |
|
|
|
542 |
|
|
print_import_status (res); |
543 |
|
|
if (res->no_user_id > 0) { |
544 |
|
|
msg_box (dlg, _("Key without a self signature was dectected!\n" |
545 |
|
|
"(This key is NOT usable for encryption, etc)\n" |
546 |
|
|
"\n" |
547 |
|
|
"Cannot import these key(s)!"), _("Import"), MB_INFO); |
548 |
|
|
} |
549 |
|
|
|
550 |
|
|
leave: |
551 |
|
|
gpgme_data_release (keydata); |
552 |
|
|
gpgme_release (ctx); |
553 |
|
|
free_if_alloc (fm_stat.opaque); |
554 |
|
|
return (int)err; |
555 |
|
|
} |
556 |
|
|
|
557 |
|
|
|
558 |
|
|
/* Mark the keys in @rset as deleted in the keycache. */ |
559 |
|
|
static void |
560 |
|
|
delete_keys_from_cache (gpgme_key_t *rset, size_t n) |
561 |
|
|
{ |
562 |
|
|
gpg_keycache_t pub = keycache_get_ctx (1); |
563 |
|
|
int i=0; |
564 |
|
|
|
565 |
|
|
while (n-- > 0) |
566 |
|
|
gpg_keycache_delete_key (pub, rset[i++]->subkeys->keyid); |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
|
570 |
|
|
/* Delete all selected keys from the list view @lv. */ |
571 |
|
|
int |
572 |
|
|
km_delete_keys (listview_ctrl_t lv, HWND dlg) |
573 |
|
|
{ |
574 |
|
|
gpgme_error_t err; |
575 |
|
|
gpgme_ctx_t ctx; |
576 |
|
|
gpgme_key_t *rset, k; |
577 |
|
|
char keyid[32], uid[256], date[64], keylen[64]; |
578 |
|
|
int with_seckey=0, seckey_type=0, confirm=0; |
579 |
|
|
int i, rc, n, k_pos=0; |
580 |
|
|
|
581 |
|
|
if (listview_get_curr_pos (lv) == -1) { |
582 |
|
|
msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR); |
583 |
twoaday |
121 |
return -1; |
584 |
werner |
36 |
} |
585 |
|
|
|
586 |
|
|
if (listview_count_items (lv, 1) > 8) { |
587 |
|
|
i = msg_box (NULL, _("Do you really want to confirm each key?"), |
588 |
|
|
_("Delete Confirmation"), MB_YESNOCANCEL|MB_ICONQUESTION); |
589 |
|
|
if (i == IDCANCEL) |
590 |
|
|
return 0; |
591 |
|
|
if (i != IDNO) |
592 |
|
|
confirm = 1; |
593 |
|
|
} |
594 |
|
|
else |
595 |
|
|
confirm = 1; |
596 |
|
|
|
597 |
|
|
n = listview_count_items (lv, 0); |
598 |
|
|
rset = (gpgme_key_t *)calloc (n+1, sizeof (gpgme_key_t)); |
599 |
|
|
if (!rset) |
600 |
|
|
BUG (NULL); |
601 |
twoaday |
121 |
for (i = 0; i < n; i++) { |
602 |
|
|
if( listview_get_item_state(lv, i)) { |
603 |
|
|
listview_get_item_text (lv, i, 0, uid, sizeof uid - 1); |
604 |
|
|
listview_get_item_text (lv, i, 1, keyid, sizeof keyid - 1); |
605 |
|
|
listview_get_item_text (lv, i, 3, keylen, sizeof keylen - 1); |
606 |
|
|
listview_get_item_text (lv, i, 7, date, sizeof date - 1); |
607 |
werner |
36 |
seckey_type = km_check_for_seckey (lv, i, NULL); |
608 |
|
|
if (confirm && !seckey_type) { |
609 |
|
|
rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING, |
610 |
|
|
_("Do you really want to delete this key?\n\n" |
611 |
|
|
"pub %s %s %s\n" |
612 |
|
|
" \"%s\""), keylen, keyid, date, uid ); |
613 |
|
|
if (rc == IDYES) { |
614 |
|
|
get_pubkey (keyid, &k); |
615 |
|
|
rset[k_pos++] = k; |
616 |
|
|
} |
617 |
|
|
with_seckey = 0; |
618 |
|
|
} |
619 |
|
|
else if (confirm) { |
620 |
twoaday |
121 |
rc = log_box (_("Key Manager"), MB_YESNO|MB_ICONWARNING, |
621 |
werner |
36 |
_("Do you really want to delete this KEY PAIR?\n\n" |
622 |
|
|
"Please remember that you are not able to decrypt\n" |
623 |
|
|
"messages you stored with this key any longer.\n" |
624 |
|
|
"\n" |
625 |
|
|
"pub/sec %s %s %s\n" |
626 |
twoaday |
121 |
" \"%s\""), keylen, keyid, date, uid); |
627 |
werner |
36 |
if( rc == IDYES ) { |
628 |
|
|
if (seckey_type == 2) |
629 |
twoaday |
121 |
msg_box (dlg, _("The actual secret key is stored on a smartcard.\n" |
630 |
werner |
36 |
"Only the public key and the secret key \n" |
631 |
twoaday |
121 |
"placeholder will be deleted.\n"), |
632 |
|
|
_("Key Manager"), MB_OK); |
633 |
werner |
36 |
get_pubkey (keyid, &k); |
634 |
|
|
rset[k_pos++] = k; |
635 |
|
|
} |
636 |
|
|
with_seckey = 1; |
637 |
|
|
} |
638 |
|
|
else { |
639 |
|
|
with_seckey = 1; |
640 |
|
|
get_pubkey (keyid, &k); |
641 |
|
|
rset[k_pos++] = k; |
642 |
|
|
} |
643 |
|
|
} |
644 |
|
|
} |
645 |
|
|
|
646 |
|
|
if (k_pos == 0) { |
647 |
|
|
free (rset); |
648 |
|
|
return 0; |
649 |
|
|
} |
650 |
|
|
|
651 |
|
|
err = gpgme_new (&ctx); |
652 |
|
|
if (err) |
653 |
|
|
BUG (NULL); |
654 |
|
|
n=k_pos; |
655 |
|
|
for (i=0; i < k_pos; i++) { |
656 |
|
|
err = gpgme_op_delete (ctx, rset[i], with_seckey); |
657 |
|
|
if (err) |
658 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
659 |
|
|
else |
660 |
|
|
n--; |
661 |
|
|
} |
662 |
|
|
if (n == 0) |
663 |
twoaday |
121 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
664 |
werner |
36 |
gpgme_release (ctx); |
665 |
|
|
listview_del_items (lv); |
666 |
|
|
delete_keys_from_cache (rset, k_pos); |
667 |
|
|
free (rset); |
668 |
|
|
|
669 |
|
|
return (int)err; |
670 |
|
|
} |
671 |
|
|
|
672 |
|
|
|
673 |
twoaday |
121 |
/* Send the select key in @lv to the keyserver @host:@port. */ |
674 |
werner |
36 |
int |
675 |
twoaday |
121 |
km_send_to_keyserver (listview_ctrl_t lv, HWND dlg, const char *host, u16 port) |
676 |
werner |
36 |
{ |
677 |
|
|
char keyid[32]; |
678 |
|
|
const char *t; |
679 |
|
|
int id; |
680 |
|
|
|
681 |
twoaday |
121 |
id = listview_get_curr_pos (lv); |
682 |
|
|
if (id == -1) { |
683 |
werner |
36 |
msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR ); |
684 |
|
|
return WPTERR_GENERAL; |
685 |
|
|
} |
686 |
|
|
|
687 |
twoaday |
121 |
listview_get_item_text (lv, id, 1, keyid, sizeof keyid - 1); |
688 |
werner |
36 |
id = log_box (_("Key Manager"), MB_YESNO, |
689 |
|
|
_("Do you really want to send '%s' to keyserver %s?"), |
690 |
|
|
keyid, host); |
691 |
|
|
if (id == IDYES) { |
692 |
|
|
t = keyid; |
693 |
|
|
if (!strncmp (keyid, "0x", 2)) |
694 |
|
|
t += 2; |
695 |
|
|
hkp_send_key (dlg, host, port, t); |
696 |
|
|
} |
697 |
|
|
|
698 |
|
|
return 0; |
699 |
twoaday |
121 |
} |
700 |
werner |
36 |
|
701 |
|
|
|
702 |
twoaday |
77 |
/* Send the selected key in @lv via MAPI to a mail recipient. */ |
703 |
werner |
36 |
int |
704 |
twoaday |
77 |
km_send_to_mail_recipient (listview_ctrl_t lv, HWND dlg) |
705 |
werner |
36 |
{ |
706 |
|
|
gpgme_key_t key; |
707 |
|
|
gpgme_ctx_t ctx=NULL; |
708 |
twoaday |
77 |
gpgme_data_t out; |
709 |
werner |
36 |
gpgme_error_t rc; |
710 |
twoaday |
121 |
char keyid[32], tmp[128]; |
711 |
|
|
char *fname; |
712 |
werner |
36 |
int pos; |
713 |
|
|
|
714 |
twoaday |
121 |
if (listview_count_items (lv, 1) > 1) { |
715 |
twoaday |
77 |
msg_box (dlg, _("Please only select one key."), |
716 |
|
|
_("Key Manager"), MB_INFO|MB_OK); |
717 |
werner |
36 |
return WPTERR_GENERAL; |
718 |
|
|
} |
719 |
twoaday |
77 |
pos = listview_get_curr_pos (lv); |
720 |
|
|
if (pos == -1) { |
721 |
|
|
msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR); |
722 |
werner |
36 |
return WPTERR_GENERAL; |
723 |
|
|
} |
724 |
twoaday |
121 |
listview_get_item_text(lv, pos, 1, keyid, sizeof (keyid)-1); |
725 |
twoaday |
77 |
if (get_pubkey (keyid, &key)) |
726 |
|
|
BUG (NULL); |
727 |
werner |
36 |
|
728 |
twoaday |
121 |
GetTempPath (sizeof (tmp)-1, tmp); |
729 |
|
|
if (tmp[strlen (tmp)-1] == '\\') |
730 |
|
|
tmp[strlen (tmp)-1] = 0; |
731 |
|
|
fname = make_filename (tmp, key->uids->name, "asc"); |
732 |
|
|
for (pos=0; pos < (int)strlen (fname); pos++) { |
733 |
|
|
if (fname[pos] == ' ') |
734 |
|
|
fname[pos] = '_'; |
735 |
|
|
} |
736 |
|
|
|
737 |
twoaday |
77 |
rc = gpgme_new (&ctx); |
738 |
werner |
36 |
if (rc) |
739 |
twoaday |
77 |
BUG (NULL); |
740 |
|
|
rc = gpgme_data_new (&out); |
741 |
|
|
if (rc) |
742 |
|
|
BUG (NULL); |
743 |
|
|
|
744 |
|
|
gpgme_set_armor (ctx, 1); |
745 |
|
|
rc = gpgme_op_export (ctx, key->subkeys->keyid, 0, out); |
746 |
|
|
if (rc) |
747 |
werner |
36 |
msg_box (dlg, gpgme_strerror (rc), _("Key Manager"), MB_ERR); |
748 |
|
|
else |
749 |
twoaday |
121 |
mapi_send_pubkey (keyid, fname); |
750 |
werner |
36 |
|
751 |
twoaday |
121 |
gpg_data_release_and_set_file (out, fname); |
752 |
werner |
36 |
gpgme_release (ctx); |
753 |
twoaday |
121 |
free_if_alloc (fname); |
754 |
werner |
36 |
return rc; |
755 |
|
|
} |
756 |
|
|
|
757 |
|
|
|
758 |
|
|
static void |
759 |
|
|
km_refresh_one_key (listview_ctrl_t lv, HWND dlg, int pos) |
760 |
|
|
{ |
761 |
|
|
int idx; |
762 |
|
|
char keyid[32]; |
763 |
|
|
const char *t; |
764 |
|
|
|
765 |
|
|
if (pos != 0) |
766 |
|
|
idx = pos; |
767 |
|
|
else |
768 |
|
|
idx = listview_get_curr_pos (lv); |
769 |
twoaday |
121 |
if (idx != -1) { |
770 |
|
|
listview_get_item_text (lv, idx, 1, keyid, sizeof (keyid) - 1); |
771 |
werner |
36 |
t = keyid; |
772 |
|
|
if (!strncmp (keyid, "0x", 2)) |
773 |
|
|
t += 2; |
774 |
twoaday |
121 |
hkp_recv_key (dlg, default_keyserver, default_keyserver_port, |
775 |
|
|
t, 0, KM_KS_REFRESH); |
776 |
werner |
36 |
} |
777 |
|
|
} |
778 |
|
|
|
779 |
|
|
|
780 |
twoaday |
121 |
/* Refresh all keys from the default keyserver. */ |
781 |
werner |
36 |
void |
782 |
|
|
km_refresh_from_keyserver (listview_ctrl_t lv, HWND dlg) |
783 |
|
|
{ |
784 |
|
|
int idx, id, i; |
785 |
|
|
|
786 |
|
|
if (kserver_check_inet_connection ()) |
787 |
|
|
{ |
788 |
|
|
msg_box (dlg, _("Could not connect to keyserver, abort procedure."), |
789 |
|
|
_("Key Manager"), MB_ERR); |
790 |
|
|
return; |
791 |
|
|
} |
792 |
|
|
idx = listview_count_items (lv, 0); |
793 |
|
|
if (listview_count_items (lv, 1) == idx) { |
794 |
twoaday |
121 |
id = msg_box (dlg, _("Do you really want to refresh all keys in the keyring?"), |
795 |
|
|
_("Key Manager"), MB_YESNO); |
796 |
werner |
36 |
if (id == IDNO) |
797 |
|
|
return; |
798 |
|
|
for (i = 0; i < idx; i++) |
799 |
|
|
km_refresh_one_key (lv, dlg, i); |
800 |
|
|
} |
801 |
|
|
else if (idx == 1) |
802 |
|
|
km_refresh_one_key (lv, dlg, 0); |
803 |
|
|
else { |
804 |
|
|
for (i=0; i < listview_count_items (lv, 0); i++) { |
805 |
|
|
if (listview_get_item_state (lv, i)) |
806 |
|
|
km_refresh_one_key (lv, dlg, i); |
807 |
|
|
} |
808 |
|
|
} |
809 |
twoaday |
121 |
} |
810 |
werner |
36 |
|
811 |
|
|
|
812 |
|
|
void |
813 |
|
|
km_set_clip_info (const char *uid) |
814 |
|
|
{ |
815 |
|
|
char buf[256]; |
816 |
|
|
|
817 |
|
|
key_get_clip_info (uid, buf, 255); |
818 |
|
|
set_clip_text (NULL, buf, strlen (buf)); |
819 |
twoaday |
121 |
} |
820 |
werner |
36 |
|
821 |
|
|
|
822 |
|
|
|
823 |
|
|
/* Return TRUE if the key in the list @lv at pos @pos is an |
824 |
|
|
old version 3 key. */ |
825 |
|
|
int |
826 |
|
|
km_key_is_v3 (listview_ctrl_t lv, int pos) |
827 |
|
|
{ |
828 |
|
|
gpgme_key_t pk; |
829 |
|
|
char keyid[32]; |
830 |
|
|
|
831 |
|
|
listview_get_item_text (lv, pos, 1, keyid, sizeof keyid-1); |
832 |
|
|
if (get_pubkey (keyid, &pk)) |
833 |
|
|
BUG (NULL); |
834 |
|
|
if (strlen (pk->subkeys->fpr) == 32 && |
835 |
|
|
pk->subkeys->pubkey_algo == GPGME_PK_RSA) |
836 |
|
|
return -1; |
837 |
|
|
return 0; |
838 |
|
|
} |
839 |
|
|
|
840 |
|
|
|
841 |
|
|
/* Update the default key entry in the status bar for dialog @dlg. */ |
842 |
|
|
void |
843 |
|
|
km_update_default_key_str (HWND dlg) |
844 |
|
|
{ |
845 |
|
|
char *keyid, defkeyinf[512]; |
846 |
|
|
const char *fmt; |
847 |
|
|
|
848 |
|
|
/* XXX: also show the name? */ |
849 |
|
|
keyid = get_gnupg_default_key (); |
850 |
|
|
if (!keyid) |
851 |
twoaday |
66 |
return; |
852 |
werner |
36 |
if( (keyid[0] >= 'A' && keyid[0] <= 'Z') || (keyid[0] >= 'a' && keyid[0] <= 'z') |
853 |
|
|
|| (keyid[0] == '0' && keyid[1] == 'x') ) |
854 |
|
|
fmt = _("Default Key: %s"); |
855 |
|
|
else |
856 |
|
|
fmt = _("Default Key: 0x%s"); |
857 |
|
|
_snprintf (defkeyinf, sizeof defkeyinf - 1, fmt, keyid); |
858 |
|
|
SendMessage (dlg, SB_SETTEXT, 0, (LPARAM)defkeyinf); |
859 |
|
|
free_if_alloc (keyid); |
860 |
|
|
} |
861 |
|
|
|
862 |
|
|
|
863 |
|
|
/* Count all keys and show from @lv results in the status bar @sb. */ |
864 |
|
|
void |
865 |
twoaday |
121 |
km_update_status_bar (HWND sb, listview_ctrl_t lv) |
866 |
werner |
36 |
{ |
867 |
|
|
char txt_sec[128], txt_pub[128]; |
868 |
twoaday |
121 |
int nkeys = 0, nsec = 0; |
869 |
|
|
int i; |
870 |
werner |
36 |
|
871 |
|
|
nkeys = listview_count_items (lv, 0); |
872 |
|
|
for (i = 0; i < nkeys; i++) { |
873 |
|
|
if (km_check_for_seckey (lv, i, NULL)) |
874 |
|
|
nsec++; |
875 |
|
|
} |
876 |
|
|
_snprintf (txt_sec, sizeof (txt_sec)-1, _("%d secret keys"), nsec); |
877 |
|
|
_snprintf (txt_pub, sizeof (txt_pub)-1, _("%d keys"), nkeys); |
878 |
|
|
SendMessage (sb, SB_SETTEXT, 1, (LPARAM)txt_sec); |
879 |
|
|
SendMessage (sb, SB_SETTEXT, 2, (LPARAM)txt_pub); |
880 |
|
|
} |
881 |
|
|
|
882 |
|
|
|
883 |
|
|
/* Set trust of selected key in @lv (at @pos) to ultimate. */ |
884 |
|
|
int |
885 |
|
|
km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos) |
886 |
|
|
{ |
887 |
|
|
GpgKeyEdit *ke; |
888 |
|
|
gpgme_error_t err; |
889 |
|
|
char keyid[32]; |
890 |
|
|
|
891 |
|
|
listview_get_item_text (lv, pos, 1, keyid, 31); |
892 |
|
|
|
893 |
|
|
ke = new GpgKeyEdit (keyid); |
894 |
|
|
if (!ke) |
895 |
|
|
BUG (0); |
896 |
|
|
|
897 |
|
|
err = ke->setTrust (GPGME_VALIDITY_ULTIMATE); |
898 |
|
|
if (err) |
899 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
900 |
|
|
else |
901 |
|
|
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
902 |
|
|
|
903 |
|
|
delete ke; |
904 |
|
|
return (int)err; |
905 |
|
|
} |
906 |
|
|
|
907 |
|
|
|
908 |
|
|
void |
909 |
|
|
km_find_key (HWND dlg, listview_ctrl_t lv) |
910 |
|
|
{ |
911 |
|
|
int oldpos = listview_get_curr_pos (lv); |
912 |
|
|
int n; |
913 |
|
|
char *name = get_input_dialog (dlg, _("Search"), _("Search for:")); |
914 |
|
|
if (name == NULL) |
915 |
|
|
return; |
916 |
|
|
if (oldpos < 0) |
917 |
|
|
oldpos = 0; |
918 |
|
|
n = listview_find (lv, name); |
919 |
|
|
if (n != -1) { |
920 |
|
|
listview_select_one (lv, n); |
921 |
|
|
listview_scroll (lv, oldpos, n); |
922 |
|
|
} |
923 |
|
|
else { |
924 |
|
|
const char *s = _("String pattern \"%s\" not found."); |
925 |
|
|
char *p = new char[strlen (s) + strlen (name) + 2]; |
926 |
|
|
if (!p) |
927 |
|
|
BUG (0); |
928 |
|
|
sprintf (p, s, name); |
929 |
|
|
msg_box (dlg, p, _("Key Manager"), MB_INFO); |
930 |
|
|
free_if_alloc (p); |
931 |
|
|
} |
932 |
|
|
free_if_alloc (name); |
933 |
|
|
} |