/[winpt]/trunk/Src/wptKeyManager.cpp
ViewVC logotype

Annotation of /trunk/Src/wptKeyManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 129 - (hide annotations)
Fri Dec 30 13:56:10 2005 UTC (19 years, 2 months ago) by twoaday
File size: 23575 byte(s)
2005-12-27  Timo Schulz  <ts@g10code.com>
                                                                                
        * wptListView.cpp (listview_set_view): New.
        (listview_del_column): New.
        * wptW32API.cpp (get_locale_date): New.
        (get_menu_state): New.
        (force_foreground_window): New.
        * wptVerifyList.cpp (strtimestamp): Support for
        locale date formats.
        * wptGPGUtil.cpp (gpg_revoke_cert): Handle bad
        passphrases.
        * wptKeyEditCB.cpp (editkey_command_handler): Immediately
        return when a bad passphrase was submitted.
        * wptKeyRevokersDlg.cpp (keyrevokers_dlg_proc): Change
        column order.
        * wptKeylist.cpp (keylist_upd_col): New.
        * wptKeyManagerDlg.cpp (update_ui_items): Deactivate
        'Revocation' for public keys.
        (translate_menu_strings): s/Revoke/Revoke Cert.
        (modify_listview_columns): New.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26