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

Annotation of /trunk/Src/wptKeyManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26