1 |
/* wptKeyManager.cpp - Handy functions for the Key Manager dialog |
2 |
* Copyright (C) 2001-2006 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 <commctrl.h> |
28 |
#include <assert.h> |
29 |
#include <stdio.h> |
30 |
|
31 |
#include "gpgme.h" |
32 |
#include "resource.h" |
33 |
#include "wptTypes.h" |
34 |
#include "wptW32API.h" |
35 |
#include "wptVersion.h" |
36 |
#include "wptCommonCtl.h" |
37 |
#include "wptNLS.h" |
38 |
#include "wptErrors.h" |
39 |
#include "wptContext.h" |
40 |
#include "wptGPG.h" |
41 |
#include "wptKeylist.h" |
42 |
#include "wptFileManager.h" |
43 |
#include "wptDlgs.h" |
44 |
#include "wptKeyserver.h" |
45 |
#include "wptKeyManager.h" |
46 |
#include "wptKeylist.h" |
47 |
#include "wptHTTP.h" |
48 |
#include "wptKeyEdit.h" |
49 |
#include "wptImport.h" |
50 |
#include "wptCrypto.h" |
51 |
#include "wptUTF8.h" |
52 |
#include "wptGPGME.h" |
53 |
|
54 |
|
55 |
/* Remove all kind of invalid characters which would |
56 |
result in an illegal file name. */ |
57 |
void |
58 |
secure_filename (char *file, size_t len) |
59 |
{ |
60 |
char *p = file; |
61 |
size_t i; |
62 |
|
63 |
/* XXX: it is possible that the recipient has a different |
64 |
charset and thus maybe 8-bit chars should be stripped also. */ |
65 |
for (i=0; i < len; i++) { |
66 |
if (p[i] == ' ' || p[i] == ':' || p[i] == '?' || p[i] == '|' || |
67 |
p[i] == '\\' || p[i] == '/') |
68 |
p[i] = '_'; |
69 |
} |
70 |
} |
71 |
|
72 |
|
73 |
/* Return a user friendly key representation in @buf of |
74 |
the key given by @keyid. */ |
75 |
void |
76 |
key_get_clip_info (const char *keyid, char *buf, size_t buflen) |
77 |
{ |
78 |
winpt_key_s key; |
79 |
gpgme_key_t pk; |
80 |
|
81 |
memset (&key, 0, sizeof (key)); |
82 |
if (winpt_get_pubkey (keyid, &key)) |
83 |
BUG (NULL); |
84 |
pk = key.ctx; |
85 |
_snprintf (buf, buflen-1, |
86 |
"pub %04d%s/%s %s %s\r\n" |
87 |
" Primary key fingerprint: %s\r\n", |
88 |
pk->subkeys->length, |
89 |
get_key_pubalgo2 (pk->subkeys->pubkey_algo), |
90 |
pk->subkeys->keyid+8, |
91 |
get_key_created (pk->subkeys->timestamp), |
92 |
key.ext->uids->uid, |
93 |
get_key_fpr (pk)); |
94 |
} |
95 |
|
96 |
|
97 |
/* Return a general description of the key @key. */ |
98 |
char* |
99 |
km_key_get_info (winpt_key_t key, int is_sec) |
100 |
{ |
101 |
gpgme_key_t pk; |
102 |
const char *fmt = "%s %04d%s/0x%s %s\n \"%s\""; |
103 |
char *p; |
104 |
int n; |
105 |
|
106 |
pk = key->ctx; |
107 |
n = strlen (fmt) + 8 + 2 + 16 + 12 + strlen (pk->uids->uid) + 32; |
108 |
p = new char[n+1]; |
109 |
if (!p) |
110 |
BUG (NULL); |
111 |
_snprintf (p, n, fmt, is_sec? "sec" : "pub", |
112 |
pk->subkeys->length, |
113 |
get_key_pubalgo2 (pk->subkeys->pubkey_algo), |
114 |
pk->subkeys->keyid+8, get_key_created (pk->subkeys->timestamp), |
115 |
key->ext->uids->uid); |
116 |
return p; |
117 |
} |
118 |
|
119 |
|
120 |
/* Retrieve the opaque 32-bit param stored within the list view item |
121 |
at position @idx and return the gpgme key context. */ |
122 |
gpgme_key_t |
123 |
km_get_key_ptr (listview_ctrl_t lv, int idx, struct keycache_s **r_ctx) |
124 |
{ |
125 |
struct keycache_s *ctx; |
126 |
|
127 |
ctx = (struct keycache_s *)listview_get_item2 (lv, idx); |
128 |
if (!ctx) |
129 |
BUG (0); |
130 |
if (r_ctx) |
131 |
*r_ctx = ctx; |
132 |
return ctx->key; |
133 |
} |
134 |
|
135 |
|
136 |
/* Check if list view @lv contains a secret key at position @pos. |
137 |
If utrust is valid, set it to 1 if the key is valid -1 otherwise. |
138 |
Return value: 1 normal key, 2 smart card key. */ |
139 |
int |
140 |
km_check_for_seckey (listview_ctrl_t lv, int pos, int *utrust) |
141 |
{ |
142 |
gpgme_key_t key; |
143 |
winpt_key_s sk; |
144 |
int type = 0; |
145 |
|
146 |
key = km_get_key_ptr (lv, pos, NULL); |
147 |
if (utrust) |
148 |
*utrust = 0; |
149 |
memset (&sk, 0, sizeof (sk)); |
150 |
if (!winpt_get_seckey (key->subkeys->keyid+8, &sk)) |
151 |
type = 1; |
152 |
if (sk.ext && sk.ext->gloflags.divert_to_card) |
153 |
type = 2; |
154 |
if (utrust && (key->expired || key->revoked)) |
155 |
*utrust = -1; |
156 |
else if (utrust && key->owner_trust == GPGME_VALIDITY_ULTIMATE) |
157 |
*utrust = 1; |
158 |
return type; |
159 |
} |
160 |
|
161 |
|
162 |
/* Check if the key at position @pos is protected with a passwd. */ |
163 |
int |
164 |
km_check_if_protected (listview_ctrl_t lv, int pos) |
165 |
{ |
166 |
gpgme_key_t key; |
167 |
winpt_key_s k; |
168 |
|
169 |
key = km_get_key_ptr (lv, pos, NULL); |
170 |
if (!key) |
171 |
return 1; /* assume yes */ |
172 |
winpt_get_pubkey (key->subkeys->keyid, &k); |
173 |
return k.is_protected; |
174 |
} |
175 |
|
176 |
|
177 |
/* Check if the key has a good status. |
178 |
Return value: 0 on success. */ |
179 |
int |
180 |
km_check_key_status (listview_ctrl_t lv, int pos) |
181 |
{ |
182 |
int flags = km_get_key_status (lv, pos); |
183 |
|
184 |
if (flags & KM_FLAG_EXPIRED) { |
185 |
msg_box (lv->ctrl, _("This key has expired!\n" |
186 |
"Key check failed."), _("Key Manager"), MB_ERR); |
187 |
return -1; |
188 |
} |
189 |
else if (flags & KM_FLAG_REVOKED) { |
190 |
msg_box (lv->ctrl, _("This key has been revoked by its owner!\n" |
191 |
"Key check failed."), _("Key Manager"), MB_ERR); |
192 |
return -1; |
193 |
} |
194 |
|
195 |
return 0; |
196 |
} |
197 |
|
198 |
|
199 |
/* Return all key flags ORed. */ |
200 |
int |
201 |
km_get_key_status (listview_ctrl_t lv, int pos) |
202 |
{ |
203 |
gpgme_key_t key; |
204 |
int flags = 0; |
205 |
|
206 |
if (pos == -1) |
207 |
return 0; |
208 |
key = km_get_key_ptr (lv, pos, NULL); |
209 |
if (!key) |
210 |
return 0; |
211 |
|
212 |
if (key->expired) |
213 |
flags |= KM_FLAG_EXPIRED; |
214 |
if (key->revoked) |
215 |
flags |= KM_FLAG_REVOKED; |
216 |
if (key->disabled) |
217 |
flags |= KM_FLAG_DISABLED; |
218 |
return flags; |
219 |
} |
220 |
|
221 |
|
222 |
/* Interface to enable or disable a key (@enable = 1 then enable). |
223 |
The key is retrieved from a list control @lv at the pos @pos. */ |
224 |
int |
225 |
km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable) |
226 |
{ |
227 |
gpgme_error_t err; |
228 |
gpgme_key_t key; |
229 |
GpgKeyEdit *ke; |
230 |
|
231 |
key = km_get_key_ptr (lv, pos, NULL); |
232 |
ke = new GpgKeyEdit (key->subkeys->keyid); |
233 |
if (!ke) |
234 |
BUG (NULL); |
235 |
|
236 |
err = enable? ke->enable () : ke->disable (); |
237 |
if (!err) |
238 |
show_msg (dlg, 1500, _("Key status changed.")); |
239 |
else |
240 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
241 |
delete ke; |
242 |
return err? WPTERR_GENERAL : 0; |
243 |
} |
244 |
|
245 |
|
246 |
|
247 |
/* Create a string that contain all keyids from |
248 |
the key list @rset separated by a space. */ |
249 |
char* |
250 |
gpg_keylist_to_pattern (gpgme_key_t *rset, int n) |
251 |
{ |
252 |
char *p; |
253 |
int i; |
254 |
|
255 |
if (!n) |
256 |
return NULL; |
257 |
p = (char *)calloc (1, n*(16+1)+n+2); |
258 |
if (!p) |
259 |
BUG (NULL); |
260 |
for (i=0; i < n; i++) { |
261 |
strcat (p, rset[i]->subkeys->keyid); |
262 |
strcat (p, " "); |
263 |
} |
264 |
return p; |
265 |
} |
266 |
|
267 |
|
268 |
/* Export the selected keys in @lv to the clipboard. */ |
269 |
int |
270 |
km_clip_export (HWND dlg, listview_ctrl_t lv) |
271 |
{ |
272 |
gpgme_error_t err; |
273 |
gpgme_key_t *rset; |
274 |
GPGME *ctx; |
275 |
char *patt=NULL; |
276 |
char buf[256]; |
277 |
int n=0; |
278 |
int rc=0; |
279 |
|
280 |
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
281 |
if (!n) { |
282 |
msg_box (dlg, _("No key was selected for export."), |
283 |
_("Key Manager"), MB_ERR); |
284 |
return WPTERR_GENERAL; |
285 |
} |
286 |
|
287 |
patt = gpg_keylist_to_pattern (rset, n); |
288 |
|
289 |
ctx = new GPGME (); |
290 |
if (!ctx) |
291 |
BUG (0); |
292 |
ctx->setArmor (true); |
293 |
err = ctx->exportToClipboard (patt); |
294 |
if (err) { |
295 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
296 |
rc = WPTERR_GENERAL; |
297 |
goto leave; |
298 |
} |
299 |
if (n == 1) { |
300 |
key_get_clip_info (rset[0]->subkeys->keyid, buf, sizeof (buf)-1); |
301 |
set_clip_text2 (NULL, buf, strlen (buf), 0); |
302 |
} |
303 |
|
304 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
305 |
|
306 |
leave: |
307 |
safe_free (rset); |
308 |
safe_free (patt); |
309 |
delete ctx; |
310 |
return rc; |
311 |
} |
312 |
|
313 |
|
314 |
/* Export the selected secret key from @lv into @fname. |
315 |
It is only allowed to export a single secret key. */ |
316 |
int |
317 |
km_privkey_export (HWND dlg, listview_ctrl_t lv, const char *fname) |
318 |
{ |
319 |
gpgme_key_t *rset; |
320 |
gpgme_error_t err; |
321 |
int n = 0; |
322 |
|
323 |
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
324 |
if (!n) { |
325 |
msg_box (dlg, _("No key was selected for export."), |
326 |
_("Key Manager"), MB_ERR); |
327 |
return WPTERR_GENERAL; |
328 |
} |
329 |
if (n > 1) { |
330 |
msg_box (dlg, _("Only one secret key can be exported."), |
331 |
_("Key Manager"), MB_ERR); |
332 |
free (rset); |
333 |
return 0; /* we checked this before, so we just quit */ |
334 |
} |
335 |
|
336 |
err = gpg_export_seckey (rset[0]->subkeys->keyid, fname); |
337 |
if (err) |
338 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
339 |
else |
340 |
log_box (_("Key Manager"), MB_OK, |
341 |
_("Secret key successfully saved in '%s'."), fname); |
342 |
|
343 |
free (rset); |
344 |
return err? WPTERR_GENERAL : 0; |
345 |
} |
346 |
|
347 |
|
348 |
/* Export the selected recipients from @lv into the file @fname. */ |
349 |
int |
350 |
km_file_export (HWND dlg, listview_ctrl_t lv, const char *fname) |
351 |
{ |
352 |
GPGME *ctx; |
353 |
gpgme_key_t *rset; |
354 |
gpgme_error_t err; |
355 |
char *patt; |
356 |
int n; |
357 |
|
358 |
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
359 |
if (!n) { |
360 |
msg_box (dlg, _("No key was selected for export."), |
361 |
_("Key Manager"), MB_ERR); |
362 |
return WPTERR_GENERAL; |
363 |
} |
364 |
patt = gpg_keylist_to_pattern (rset, n); |
365 |
|
366 |
ctx = new GPGME (); |
367 |
ctx->setArmor (true); |
368 |
err = ctx->exportToFile (patt, fname); |
369 |
if (err) { |
370 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
371 |
goto leave; |
372 |
} |
373 |
else |
374 |
log_box (_("Key Manager"), MB_OK, |
375 |
_("Key(s) successfully saved in '%s'."), fname); |
376 |
|
377 |
leave: |
378 |
delete ctx; |
379 |
safe_free (patt); |
380 |
return (int)err; |
381 |
} |
382 |
|
383 |
|
384 |
/* Read a dash escaped key from the clipboard |
385 |
unescape it and write it back. */ |
386 |
static int |
387 |
extract_dash_escaped_key (void) |
388 |
{ |
389 |
gpgme_data_t inp, plain; |
390 |
gpgme_error_t err; |
391 |
|
392 |
err = gpg_data_new_from_clipboard (&inp, 0); |
393 |
if (err) { |
394 |
msg_box (NULL, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
395 |
return -1; |
396 |
} |
397 |
gpg_data_extract_plaintext (inp, &plain); |
398 |
gpg_data_release_and_set_clipboard (plain, 0); |
399 |
gpgme_data_release (inp); |
400 |
|
401 |
return 0; |
402 |
} |
403 |
|
404 |
|
405 |
/* Import the PGP key data from the clipboard. |
406 |
Return value: 0 on success. */ |
407 |
static gpgme_error_t |
408 |
gpg_op_clip_import (gpgme_ctx_t ctx) |
409 |
{ |
410 |
gpgme_error_t err = 0; |
411 |
gpgme_data_t keydata = NULL; |
412 |
|
413 |
err = gpg_data_new_from_clipboard (&keydata, 0); |
414 |
if (!err) |
415 |
err = gpgme_op_import (ctx, keydata); |
416 |
|
417 |
gpgme_data_release (keydata); |
418 |
return err; |
419 |
} |
420 |
|
421 |
/* Import keys from the clipboard. */ |
422 |
int |
423 |
km_clip_import (HWND dlg, int *r_newkeys, int *r_newsks) |
424 |
{ |
425 |
gpgme_error_t err; |
426 |
gpgme_ctx_t ctx = NULL; |
427 |
gpgme_import_result_t res; |
428 |
fm_state_s fm_stat; |
429 |
int pgptype; |
430 |
int id, has_data = 0; |
431 |
int new_keys = 0, new_sks = 0; |
432 |
|
433 |
if (!gpg_clip_istext_avail (&has_data) && !has_data) { |
434 |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_ISEMPTY), |
435 |
_("Key Manager"), MB_ERR); |
436 |
return WPTERR_CLIP_ISEMPTY; |
437 |
} |
438 |
err = gpg_clip_is_secured (&pgptype, &has_data); |
439 |
if (err) |
440 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
441 |
if (!has_data) { |
442 |
msg_box (dlg, _("No valid OpenPGP data found."), |
443 |
_("Key Manager"), MB_ERR); |
444 |
return WPTERR_NODATA; |
445 |
} |
446 |
if (!(pgptype & PGP_PUBKEY) && !(pgptype & PGP_SECKEY)) { |
447 |
msg_box (dlg, _("No valid OpenPGP keys found."), |
448 |
_("Key Manager"), MB_ERR); |
449 |
return WPTERR_NODATA; |
450 |
} |
451 |
if (pgptype & PGP_DASH_ESCAPED) { |
452 |
id = msg_box (dlg, _("The key you want to import is dash escaped.\n" |
453 |
"Do you want to extract the key?"), |
454 |
_("Key Manager"), MB_YESNO); |
455 |
if (id == IDYES) |
456 |
extract_dash_escaped_key (); |
457 |
else |
458 |
msg_box (dlg, _("Cannot import dash escaped OpenPGP keys."), |
459 |
_("Key Manager"), MB_INFO); |
460 |
} |
461 |
|
462 |
memset (&fm_stat, 0, sizeof (fm_stat)); |
463 |
fm_stat.opaque = m_strdup ("Clipboard"); |
464 |
fm_stat.import.is_clip = 1; |
465 |
has_data = dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
466 |
file_import_dlg_proc, (LPARAM)&fm_stat, |
467 |
_("Key Import"), IDS_WINPT_IMPORT); |
468 |
if (!has_data) |
469 |
goto leave; |
470 |
|
471 |
err = gpgme_new (&ctx); |
472 |
if (err) |
473 |
BUG (NULL); |
474 |
op_begin (); |
475 |
err = gpg_op_clip_import (ctx); |
476 |
op_end (); |
477 |
if (err) { |
478 |
msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
479 |
goto leave; |
480 |
} |
481 |
|
482 |
res = gpgme_op_import_result (ctx); |
483 |
print_import_status (res); |
484 |
new_keys = res->considered - res->unchanged; |
485 |
new_sks = res->secret_imported - res->secret_unchanged; |
486 |
if (res->no_user_id > 0) { |
487 |
msg_box (dlg, _("Key without a self signature was dectected!\n" |
488 |
"(This key is NOT usable for encryption, etc)\n"), |
489 |
_("Import"), MB_WARN); |
490 |
} |
491 |
|
492 |
leave: |
493 |
if (ctx) |
494 |
gpgme_release (ctx); |
495 |
free_if_alloc (fm_stat.opaque); |
496 |
if (r_newkeys) |
497 |
*r_newkeys = new_keys; |
498 |
if (r_newsks) |
499 |
*r_newsks = new_sks; |
500 |
if (!new_keys || !has_data) |
501 |
return WPTERR_NODATA; |
502 |
return (int)err; |
503 |
} |
504 |
|
505 |
|
506 |
/* Import a key from the http URL @url. */ |
507 |
int |
508 |
km_http_import (HWND dlg, const char *url) |
509 |
{ |
510 |
FILE *fp; |
511 |
wHTTP *hd; |
512 |
char tmpfile[500]; |
513 |
int rc = 0; |
514 |
|
515 |
if (strncmp (url, "http://", 7)) { |
516 |
log_box (_("Key Import HTTP"), MB_ERR, _("Invalid HTTP URL: %s"), url); |
517 |
return WPTERR_GENERAL; |
518 |
} |
519 |
|
520 |
get_temp_name (tmpfile, sizeof (tmpfile)-1, "winpt_http.tmp"); |
521 |
fp = fopen (tmpfile, "wb"); |
522 |
if (!fp) { |
523 |
log_box (_("Key Import HTTP"), MB_ERR, "%s: %s", tmpfile, |
524 |
winpt_strerror (WPTERR_FILE_CREAT)); |
525 |
return WPTERR_FILE_CREAT; |
526 |
} |
527 |
|
528 |
hd = new wHTTP (url); |
529 |
if (hd->getStatusCode () == HTTP_STAT_200) |
530 |
hd->readData (fp); |
531 |
else { |
532 |
log_box (_("Key Import HTTP"), MB_ERR, |
533 |
_("Could not fetch key from URL: %s"), url); |
534 |
rc = WPTERR_GENERAL; |
535 |
} |
536 |
|
537 |
delete hd; |
538 |
fclose (fp); |
539 |
if (!rc) |
540 |
km_file_import (dlg, tmpfile, NULL, NULL); |
541 |
DeleteFile (tmpfile); |
542 |
return rc; |
543 |
} |
544 |
|
545 |
|
546 |
/* Import a key from the given file @fname, if @fname is |
547 |
NULL use the common 'file open' dialog. |
548 |
On success an import statistics dialog is shown. */ |
549 |
int |
550 |
km_file_import (HWND dlg, const char *fname, int *r_newkeys, int *r_newsks) |
551 |
{ |
552 |
gpgme_data_t keydata = NULL; |
553 |
gpgme_ctx_t ctx; |
554 |
gpgme_error_t err; |
555 |
fm_state_s fm_stat; |
556 |
gpgme_import_result_t res; |
557 |
const char *name; |
558 |
int no_data = 0; |
559 |
int new_keys = 0, new_sks = 0; |
560 |
|
561 |
if (!fname) { |
562 |
name = get_fileopen_dlg (dlg, _("Choose Name of the Key File"), |
563 |
NULL, NULL); |
564 |
if (!name) |
565 |
return WPTERR_GENERAL; |
566 |
} |
567 |
else |
568 |
name = fname; |
569 |
|
570 |
memset (&fm_stat, 0, sizeof (fm_stat)); |
571 |
fm_stat.opaque = m_strdup (name); |
572 |
|
573 |
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
574 |
file_import_dlg_proc, (LPARAM)&fm_stat, |
575 |
_("File Import"), IDS_WINPT_IMPORT); |
576 |
if (fm_stat.cancel == 1) { |
577 |
free_if_alloc (fm_stat.opaque); |
578 |
return WPTERR_GENERAL; |
579 |
} |
580 |
|
581 |
err = gpgme_new (&ctx); |
582 |
if (err) |
583 |
BUG (NULL); |
584 |
err = gpgme_data_new_from_file (&keydata, name, 1); |
585 |
if (err) { |
586 |
msg_box (dlg, _("Could not read key-data from file."), |
587 |
_("Key Manager"), MB_ERR); |
588 |
goto leave; |
589 |
} |
590 |
|
591 |
op_begin (); |
592 |
err = gpgme_op_import (ctx, keydata); |
593 |
op_end (); |
594 |
if (err) { |
595 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
596 |
goto leave; |
597 |
} |
598 |
|
599 |
res = gpgme_op_import_result (ctx); |
600 |
if (res->unchanged == res->considered && |
601 |
res->secret_unchanged == res->secret_imported) |
602 |
no_data = 1; |
603 |
new_keys = res->considered - res->unchanged; |
604 |
new_sks = res->secret_imported - res->secret_unchanged; |
605 |
if (res->new_revocations == 0 && fm_stat.import.revcert == 1) |
606 |
res->new_revocations = 1; |
607 |
if (res->secret_imported == 0 && fm_stat.import.has_seckey == 1) |
608 |
res->secret_imported = 1; |
609 |
|
610 |
/* XXX: if we import a key pair but the secret key comes first, |
611 |
no_{valid}_user_id is 1 even so the public key, which comes |
612 |
later is valid and self-signed. */ |
613 |
print_import_status (res); |
614 |
if (res->no_user_id > 0) { |
615 |
msg_box (dlg, _("Key without a self signature was dectected!\n" |
616 |
"(This key is NOT usable for encryption, etc)\n"), |
617 |
_("Import"), MB_WARN); |
618 |
} |
619 |
|
620 |
leave: |
621 |
gpgme_data_release (keydata); |
622 |
gpgme_release (ctx); |
623 |
free_if_alloc (fm_stat.opaque); |
624 |
if (r_newkeys) |
625 |
*r_newkeys = new_keys; |
626 |
if (r_newsks) |
627 |
*r_newsks = new_sks; |
628 |
if (no_data) |
629 |
return WPTERR_NODATA; |
630 |
return (int)err; |
631 |
} |
632 |
|
633 |
|
634 |
/* Import all dropped files. */ |
635 |
int |
636 |
km_dropped_file_import (HWND dlg, HDROP hdrop, |
637 |
int *r_newkeys, int *r_newsks) |
638 |
{ |
639 |
char name[MAX_PATH+1]; |
640 |
UINT n = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0); |
641 |
UINT i; |
642 |
int newk=0, newsk=0, err=0; |
643 |
|
644 |
for (i=0; i < n; i++) { |
645 |
DragQueryFile (hdrop, i, name, MAX_PATH); |
646 |
err = km_file_import (dlg, name, &newk, &newsk); |
647 |
*r_newkeys = (*r_newkeys) + newk; |
648 |
*r_newsks = (*r_newsks) + newsk; |
649 |
} |
650 |
DragFinish (hdrop); |
651 |
return err; |
652 |
} |
653 |
|
654 |
|
655 |
/* Mark the keys in @rset as deleted in the keycache. */ |
656 |
static void |
657 |
delete_keys_from_cache (gpgme_key_t *rset, size_t n) |
658 |
{ |
659 |
gpg_keycache_t pub = keycache_get_ctx (1); |
660 |
gpg_keycache_t sec = keycache_get_ctx (0); |
661 |
gpgme_key_t sk; |
662 |
int i=0; |
663 |
|
664 |
while (n-- > 0) { |
665 |
if (!get_seckey (rset[i]->subkeys->keyid, &sk)) |
666 |
gpg_keycache_delete_key (sec, sk->subkeys->keyid); |
667 |
gpg_keycache_delete_key (pub, rset[i]->subkeys->keyid); |
668 |
i++; |
669 |
} |
670 |
} |
671 |
|
672 |
|
673 |
/* Check that the selected default secret key is still |
674 |
available. If not, delete the entry in gpg.conf. */ |
675 |
static void |
676 |
check_exist_default_key (void) |
677 |
{ |
678 |
gpgme_key_t sk; |
679 |
char *defkey; |
680 |
|
681 |
defkey = get_gnupg_default_key (); |
682 |
if (defkey && get_seckey (defkey, &sk)) |
683 |
set_gnupg_default_key (NULL); |
684 |
free_if_alloc (defkey); |
685 |
} |
686 |
|
687 |
|
688 |
/* Delete the list view items which match a key ptr |
689 |
inside the @rset array. */ |
690 |
static void |
691 |
lv_del_confirmed_keys (listview_ctrl_t lv, gpgme_key_t *rset) |
692 |
{ |
693 |
winpt_key_s key2; |
694 |
int j, i, n = listview_count_items (lv, 0); |
695 |
|
696 |
for (i=0; i < n; i++) { |
697 |
if (listview_get_item_state (lv, i)) { |
698 |
km_get_key (lv, i, &key2); |
699 |
for (j=0; rset[j] != NULL; j++) { |
700 |
if (rset[j] == key2.ctx) |
701 |
listview_del_item (lv, i); |
702 |
} |
703 |
} |
704 |
} |
705 |
} |
706 |
|
707 |
|
708 |
/* Delete all selected keys from the list view @lv. */ |
709 |
int |
710 |
km_delete_keys (listview_ctrl_t lv, HWND dlg) |
711 |
{ |
712 |
gpgme_error_t err; |
713 |
gpgme_ctx_t ctx; |
714 |
gpgme_key_t *rset; |
715 |
winpt_key_s key2; |
716 |
char *p; |
717 |
int with_seckey=0, seckey_type=0, confirm=0; |
718 |
int i, rc, n, k_pos=0; |
719 |
|
720 |
if (listview_get_curr_pos (lv) == -1) { |
721 |
msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR); |
722 |
return -1; |
723 |
} |
724 |
|
725 |
if (listview_count_items (lv, 1) > 8) { |
726 |
i = msg_box (NULL, _("Do you really want to confirm each key?"), |
727 |
_("Delete Confirmation"), MB_YESNOCANCEL|MB_ICONQUESTION); |
728 |
if (i == IDCANCEL) |
729 |
return 0; |
730 |
if (i != IDNO) |
731 |
confirm = 1; |
732 |
} |
733 |
else |
734 |
confirm = 1; |
735 |
|
736 |
/* n = total amount of keys, i is the selected amount. */ |
737 |
n = listview_count_items (lv, 0); |
738 |
i = listview_count_items (lv, 1); |
739 |
rset = (gpgme_key_t *)calloc (i+1, sizeof (gpgme_key_t)); |
740 |
if (!rset) |
741 |
BUG (NULL); |
742 |
for (i = 0; i < n; i++) { |
743 |
if (listview_get_item_state(lv, i)) { |
744 |
km_get_key (lv, i, &key2); |
745 |
|
746 |
seckey_type = km_check_for_seckey (lv, i, NULL); |
747 |
if (confirm && !seckey_type) { |
748 |
p = km_key_get_info (&key2, 0); |
749 |
rc = log_box (_("Key Manager"), MB_YESNO|MB_ICONWARNING, |
750 |
_("Do you really want to delete this key?\n\n" |
751 |
"%s"), p); |
752 |
if (rc == IDYES) |
753 |
rset[k_pos++] = key2.ctx; |
754 |
free_if_alloc (p); |
755 |
} |
756 |
else if (confirm) { |
757 |
p = km_key_get_info (&key2, 1); |
758 |
rc = log_box (_("Key Manager"), MB_YESNO|MB_ICONWARNING, |
759 |
_("Do you really want to delete this KEY PAIR?\n\n" |
760 |
"Please remember that you are not able to decrypt\n" |
761 |
"messages you stored with this key any longer.\n" |
762 |
"\n" |
763 |
"%s"), p); |
764 |
if (rc == IDYES) { |
765 |
if (seckey_type == 2) |
766 |
msg_box (dlg, _("The actual secret key is stored on a smartcard.\n" |
767 |
"Only the public key and the secret key \n" |
768 |
"placeholder will be deleted.\n"), |
769 |
_("Key Manager"), MB_OK); |
770 |
rset[k_pos++] = key2.ctx; |
771 |
} |
772 |
with_seckey = 1; |
773 |
free_if_alloc (p); |
774 |
} |
775 |
else { |
776 |
with_seckey = 1; |
777 |
rset[k_pos++] = key2.ctx; |
778 |
} |
779 |
} |
780 |
} |
781 |
|
782 |
if (k_pos == 0) { |
783 |
free (rset); |
784 |
return 0; |
785 |
} |
786 |
|
787 |
err = gpgme_new (&ctx); |
788 |
if (err) |
789 |
BUG (NULL); |
790 |
n = k_pos; |
791 |
op_begin (); |
792 |
for (i=0; i < k_pos; i++) { |
793 |
err = gpgme_op_delete (ctx, rset[i], with_seckey); |
794 |
if (err) |
795 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
796 |
else |
797 |
n--; |
798 |
} |
799 |
op_end (); |
800 |
if (n == 0) |
801 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
802 |
gpgme_release (ctx); |
803 |
lv_del_confirmed_keys (lv, rset); |
804 |
delete_keys_from_cache (rset, k_pos); |
805 |
safe_free (rset); |
806 |
if (with_seckey) |
807 |
check_exist_default_key (); |
808 |
return (int)err; |
809 |
} |
810 |
|
811 |
|
812 |
/* Send the select key in @lv to the keyserver @host:@port. */ |
813 |
int |
814 |
km_send_to_keyserver (listview_ctrl_t lv, HWND dlg, |
815 |
const char *host, WORD port) |
816 |
{ |
817 |
winpt_key_s key; |
818 |
int id; |
819 |
|
820 |
id = listview_get_curr_pos (lv); |
821 |
if (id == -1) { |
822 |
msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR ); |
823 |
return WPTERR_GENERAL; |
824 |
} |
825 |
|
826 |
km_get_key (lv, id, &key); |
827 |
id = log_box (_("Key Manager"), MB_YESNO, |
828 |
_("Do you really want to send '0x%s' to keyserver %s?"), |
829 |
key.keyid, host); |
830 |
if (id == IDYES) |
831 |
hkp_send_key (dlg, host, port, key.keyid); |
832 |
|
833 |
return 0; |
834 |
} |
835 |
|
836 |
|
837 |
/* Send the selected key in @lv via MAPI to a mail recipient. */ |
838 |
int |
839 |
km_send_to_mail_recipient (listview_ctrl_t lv, HWND dlg) |
840 |
{ |
841 |
gpgme_key_t key; |
842 |
gpgme_error_t rc; |
843 |
GPGME *ctx; |
844 |
struct keycache_s *c; |
845 |
char *fname; |
846 |
int pos; |
847 |
int n; |
848 |
|
849 |
if (listview_count_items (lv, 1) > 1) { |
850 |
msg_box (dlg, _("Please only select one key."), |
851 |
_("Key Manager"), MB_INFO|MB_OK); |
852 |
return WPTERR_GENERAL; |
853 |
} |
854 |
pos = listview_get_curr_pos (lv); |
855 |
if (pos == -1) { |
856 |
msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR); |
857 |
return WPTERR_GENERAL; |
858 |
} |
859 |
key = km_get_key_ptr (lv, pos, &c); |
860 |
|
861 |
n = strlen (c->uids->name)+1 + MAX_PATH + 5; |
862 |
fname = new char[n+1]; |
863 |
get_temp_name (fname, n-5, c->uids->name); |
864 |
secure_filename (fname, strlen (fname)); |
865 |
strcat (fname, ".asc"); |
866 |
ctx = new GPGME (); |
867 |
ctx->setArmor (true); |
868 |
rc = ctx->exportToFile (key->subkeys->keyid, fname); |
869 |
if (rc) |
870 |
msg_box (dlg, gpgme_strerror (rc), _("Key Manager"), MB_ERR); |
871 |
else |
872 |
mapi_send_pubkey (key->subkeys->keyid+8, fname); |
873 |
|
874 |
delete ctx; |
875 |
free_if_alloc (fname); |
876 |
return rc; |
877 |
} |
878 |
|
879 |
|
880 |
/* Refresh the selected key in the listview @lv at position @pos. |
881 |
Legal flags are 0 = single key. */ |
882 |
static void |
883 |
km_refresh_one_key (listview_ctrl_t lv, HWND dlg, int pos, int flags) |
884 |
{ |
885 |
winpt_key_s pk; |
886 |
gpgme_key_t key; |
887 |
const char *pref_kserv = default_keyserver; |
888 |
unsigned short pref_kserv_port = default_keyserver_port; |
889 |
char keyid[16+1]; |
890 |
int idx, err = 0; |
891 |
|
892 |
if (pos != 0) |
893 |
idx = pos; |
894 |
else |
895 |
idx = listview_get_curr_pos (lv); |
896 |
if (idx != -1) { |
897 |
key = km_get_key_ptr (lv, idx, NULL); |
898 |
|
899 |
/* In single refresh mode, try to use the users preferred keyserver. */ |
900 |
if (flags == 0 && |
901 |
!winpt_get_pubkey (key->subkeys->keyid+8, &pk) && |
902 |
!gpg_keycache_update_attr (pk.ext, KC_ATTR_PREFKSERV, 0) && |
903 |
pk.ext->pref_keyserver && |
904 |
!strstr (pk.ext->pref_keyserver, "ldap://")) { |
905 |
pref_kserv = pk.ext->pref_keyserver; |
906 |
pref_kserv_port = pk.ext->pref_keyserver_port; |
907 |
} |
908 |
_snprintf (keyid, sizeof (keyid)-1, "%s", key->subkeys->keyid+8); |
909 |
err = hkp_recv_key (dlg, pref_kserv, pref_kserv_port, |
910 |
keyid, 0, flags); |
911 |
/* if we receive just a single key (no refresh mode), update it. */ |
912 |
if (!flags && !err) |
913 |
keycache_update (0, keyid); |
914 |
} |
915 |
} |
916 |
|
917 |
|
918 |
/* Refresh the selected keys from the default keyserver. */ |
919 |
int |
920 |
km_refresh_from_keyserver (listview_ctrl_t lv, HWND dlg) |
921 |
{ |
922 |
int cnt, id, i; |
923 |
|
924 |
cnt = listview_count_items (lv, 0); |
925 |
if (listview_count_items (lv, 1) == cnt) { |
926 |
id = msg_box (dlg, _("Do you really want to refresh all keys in the keyring?"), |
927 |
_("Key Manager"), MB_YESNO); |
928 |
if (id == IDNO) |
929 |
return -1; |
930 |
if (kserver_check_inet_connection ()) { |
931 |
msg_box (dlg, _("Could not connect to keyserver, abort procedure."), |
932 |
_("Key Manager"), MB_ERR); |
933 |
return -1; |
934 |
} |
935 |
} |
936 |
if (listview_count_items (lv, 1) == 1) |
937 |
km_refresh_one_key (lv, dlg, listview_get_curr_pos (lv), 0); |
938 |
else { |
939 |
for (i=0; i < cnt; i++) { |
940 |
if (listview_get_item_state (lv, i)) |
941 |
km_refresh_one_key (lv, dlg, i, KM_KS_REFRESH); |
942 |
} |
943 |
} |
944 |
return 0; |
945 |
} |
946 |
|
947 |
|
948 |
void |
949 |
km_set_clip_info (const char *uid) |
950 |
{ |
951 |
char buf[256]; |
952 |
|
953 |
key_get_clip_info (uid, buf, sizeof (buf)-1); |
954 |
set_clip_text (NULL, buf, strlen (buf)); |
955 |
} |
956 |
|
957 |
|
958 |
|
959 |
/* Return TRUE if the key in the list @lv at pos @pos is an |
960 |
old version 3 key. */ |
961 |
int |
962 |
km_key_is_v3 (listview_ctrl_t lv, int pos) |
963 |
{ |
964 |
gpgme_key_t pk; |
965 |
|
966 |
pk = km_get_key_ptr (lv, pos, NULL); |
967 |
if (strlen (pk->subkeys->fpr) == 32 && |
968 |
pk->subkeys->pubkey_algo == GPGME_PK_RSA) |
969 |
return -1; |
970 |
return 0; |
971 |
} |
972 |
|
973 |
|
974 |
/* Set trust of selected key in @lv (at @pos) to ultimate. */ |
975 |
int |
976 |
km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos) |
977 |
{ |
978 |
gpgme_error_t err; |
979 |
gpgme_key_t key; |
980 |
GpgKeyEdit *ke; |
981 |
|
982 |
key = km_get_key_ptr (lv, pos, NULL); |
983 |
ke = new GpgKeyEdit (key->subkeys->keyid); |
984 |
if (!ke) |
985 |
BUG (0); |
986 |
|
987 |
err = ke->setTrust (GPGME_VALIDITY_ULTIMATE); |
988 |
if (err) |
989 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
990 |
else |
991 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
992 |
|
993 |
delete ke; |
994 |
return (int)err; |
995 |
} |
996 |
|
997 |
|
998 |
/* Provide a search dialog and try to find and select |
999 |
the key which matches the entered pattern. */ |
1000 |
void |
1001 |
km_find_key (HWND dlg, listview_ctrl_t lv) |
1002 |
{ |
1003 |
int oldpos = listview_get_curr_pos (lv); |
1004 |
int n; |
1005 |
char *name = get_input_dialog (dlg, _("Search"), _("Search for:")); |
1006 |
|
1007 |
if (!name) |
1008 |
return; |
1009 |
if (oldpos < 0) |
1010 |
oldpos = 0; |
1011 |
n = listview_find (lv, name, 1); |
1012 |
if (n != -1) { |
1013 |
listview_select_one (lv, n); |
1014 |
listview_scroll (lv, oldpos, n); |
1015 |
} |
1016 |
else { |
1017 |
const char *s = _("String pattern \"%s\" not found."); |
1018 |
char *p = new char[strlen (s) + strlen (name) + 2]; |
1019 |
if (!p) |
1020 |
BUG (0); |
1021 |
sprintf (p, s, name); |
1022 |
msg_box (dlg, p, _("Key Manager"), MB_INFO); |
1023 |
free_if_alloc (p); |
1024 |
} |
1025 |
free_if_alloc (name); |
1026 |
} |
1027 |
|
1028 |
|
1029 |
/* Return a user-friendly name for a key derrived from |
1030 |
name. If @is_secret is 1, a secret key name will be generated. */ |
1031 |
char* |
1032 |
km_gen_export_filename (const char *keyid, int is_secret) |
1033 |
{ |
1034 |
winpt_key_s key; |
1035 |
char *p; |
1036 |
|
1037 |
memset (&key, 0, sizeof (key)); |
1038 |
if (winpt_get_pubkey (keyid, &key)) |
1039 |
return m_strdup (keyid); |
1040 |
|
1041 |
p = new char[strlen (key.ext->uids->name) + 8 + 16]; |
1042 |
if (!p) |
1043 |
BUG (0); |
1044 |
sprintf (p, "%s%s.asc", key.ext->uids->name, is_secret? "_sec" : ""); |
1045 |
secure_filename (p, strlen (p)); |
1046 |
return p; |
1047 |
} |
1048 |
|
1049 |
|
1050 |
/* Retrieve the GPGME pointer from the selected list view |
1051 |
item and return the WinPT specific key context. */ |
1052 |
int |
1053 |
km_get_key (listview_ctrl_t lv, int pos, winpt_key_t k) |
1054 |
{ |
1055 |
struct keycache_s *c; |
1056 |
gpgme_key_t key; |
1057 |
|
1058 |
if (pos == -1) |
1059 |
return -1; |
1060 |
key = km_get_key_ptr (lv, pos, &c); |
1061 |
memset (k, 0, sizeof (*k)); |
1062 |
strncpy (k->tmp_keyid, key->subkeys->keyid+8, 8); |
1063 |
k->keyid = k->tmp_keyid; |
1064 |
k->is_protected = km_check_if_protected (lv, pos); |
1065 |
k->key_pair = km_check_for_seckey (lv, pos, NULL); |
1066 |
k->is_v3 = km_key_is_v3 (lv, pos); |
1067 |
k->flags = km_get_key_status (lv, pos); |
1068 |
k->ctx = key; |
1069 |
k->ext = c; |
1070 |
k->uid = c->uids->uid; |
1071 |
return 0; |
1072 |
} |
1073 |
|
1074 |
|
1075 |
/* Show details of the revocation of the key @k. */ |
1076 |
void |
1077 |
km_key_show_revoc_info (winpt_key_t k) |
1078 |
{ |
1079 |
const char *desc; |
1080 |
char *val=NULL, *p; |
1081 |
int len, reason; |
1082 |
const char *reasons[] = { |
1083 |
_("0. No reason specified"), |
1084 |
_("1. Key has been compromised"), |
1085 |
_("2. Key is superseded"), |
1086 |
_("3. Key is no longer used"), |
1087 |
NULL |
1088 |
}; |
1089 |
|
1090 |
/* spk:29:1:19:%00This key is kaputt */ |
1091 |
gpg_find_key_subpacket (k->keyid, 29, &val); |
1092 |
if (!val || strlen (val) < 3) { |
1093 |
safe_free (val); |
1094 |
return; |
1095 |
} |
1096 |
|
1097 |
len = atoi (val+9); |
1098 |
p = strchr (val+9, ':'); |
1099 |
assert (p); |
1100 |
p++; |
1101 |
reason = atoi (p+1); |
1102 |
if (strlen (p+1) > 0) |
1103 |
desc = p+3; |
1104 |
else |
1105 |
desc = "none"; |
1106 |
log_box (_("Key Manager"), MB_OK, |
1107 |
"Key revocation info for '%s':\n" |
1108 |
"Reason for revocation: %s\n" |
1109 |
"Description: %s\n", |
1110 |
k->ctx->uids->name, reasons[reason % 4]+3, |
1111 |
desc); |
1112 |
safe_free (val); |
1113 |
} |