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

Contents of /trunk/Src/wptKeyRevokeDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 442 - (show annotations)
Sat Apr 14 14:23:34 2012 UTC (12 years, 10 months ago) by twoaday
File size: 6678 byte(s)


1 /* wptKeyRevokeDlg.cpp - Key revocation dialog
2 * Copyright (C) 2001-2003, 2005-2006, 2009 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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <windows.h>
22
23 #include "resource.h"
24 #include "gpgme.h"
25 #include "wptErrors.h"
26 #include "wptGPG.h"
27 #include "wptW32API.h"
28 #include "wptTypes.h"
29 #include "wptCommonCtl.h"
30 #include "wptContext.h" /* for passphrase_s */
31 #include "wptDlgs.h"
32 #include "wptNLS.h"
33 #include "wptUTF8.h"
34
35 void secure_filename (char *file, size_t len);
36
37
38 /* Generate a file template for the cert based on the key
39 with the keyid @keyid. */
40 static void
41 mk_cert_fname (const char *keyid, char *fname, size_t flen)
42 {
43 winpt_key_s k;
44 const char *filename = _("Revocation-Certificate");
45
46 memset (&k, 0, sizeof (k));
47 if (winpt_get_pubkey (keyid, &k))
48 BUG (NULL);
49 _snprintf (fname, flen-1, "%s_%s.asc", k.ext->uids->name, filename);
50 secure_filename (fname, strlen (fname));
51 }
52
53
54 /* Release the cert data and store it in the file @fname. */
55 static void
56 release_cert_as_file (char *revcert, const char *fname)
57 {
58 gpgme_error_t err;
59 gpgme_data_t rev;
60
61 err = gpgme_data_new_from_mem (&rev, revcert, strlen (revcert), 1);
62 if (!err)
63 err = gpg_data_release_and_set_file (rev, fname);
64 if (err)
65 msg_box (NULL, gpgme_strerror (err), _("Key Revocation Cert"), MB_ERR);
66 safe_free (revcert);
67 }
68
69
70 static void
71 on_init_dialog (HWND dlg)
72 {
73 HWND list;
74
75 SetWindowText (dlg, _("Key Revocation Cert"));
76 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
77 SetDlgItemText (dlg, IDC_KEYREVOKE_REASONINF, _("Reason for revocation"));
78 SetDlgItemText (dlg, IDC_KEYREVOKE_OPTINF, _("Optional description text"));
79 SetDlgItemText (dlg, IDC_KEYREVOKE_PWDINF, _("&Passphrase:"));
80 SetDlgItemText (dlg, IDC_KEYREVOKE_OUTINF, _("Output file:"));
81 list = GetDlgItem (dlg, IDC_KEYREVOKE_REASON);
82 listbox_add_string (list, _("0. No reason specified"));
83 listbox_add_string (list, _("1. Key has been compromised"));
84 listbox_add_string (list, _("2. Key is superseded"));
85 listbox_add_string (list, _("3. Key is no longer used"));
86 SendMessage (list, LB_SETCURSEL, (WPARAM)0, 0);
87 SetForegroundWindow (dlg);
88 center_window (dlg, NULL);
89 }
90
91 /* Generate the data expected by the gpg command handler. */
92 static char*
93 generate_revoke_input (int code, const char *cmt, const char *pass)
94 {
95 const char *fmt;
96 char *p;
97 size_t n;
98
99 fmt = "Y\n" /* gen_revoke.okay */
100 "%d\n" /* ask_revocation_reason.code */
101 "%s\n" /* ask_revocation_reason.text */
102 "%s" /* text != NULL '\n' otherwise '' */
103 "Y\n" /* ask_revocation_reason.okay */
104 "%s\n"; /* passphrase.enter. */
105 n = strlen (fmt) + 32;
106 if (pass)
107 n += strlen (pass) + 1;
108 if (cmt)
109 n += strlen (cmt) + 1;
110 p = new char[n+1];
111 if (!p)
112 BUG (0);
113 sprintf (p, fmt, code, cmt? cmt : "", cmt? "\n" : "", pass? pass : "");
114 return p;
115 }
116
117
118 /* Dialog box procedure for key revocation. */
119 BOOL CALLBACK
120 key_revoke_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
121 {
122 static winpt_key_t k;
123 gpgme_error_t err;
124 HWND list;
125 int idx, use_desc;
126 char file[256];
127 char *pwd=NULL;
128 char *desc=NULL;
129 char *inp_data = NULL, *revcert=NULL;
130 const char *warning =
131 _("Please move this certificate to a medium where it can be "
132 "stored in a safe place (floppy, CDR, etc..).\n"
133 "If an attacker gets access to this certificate he can use it to "
134 "render your key unusable!");
135
136 switch( msg ) {
137 case WM_INITDIALOG:
138 if (!lparam)
139 BUG (0);
140 k = (winpt_key_t)lparam;
141 on_init_dialog (dlg);
142 return TRUE;
143
144 case WM_DESTROY:
145 balloon_msg_disable ();
146 break;
147
148 case WM_COMMAND:
149 switch (LOWORD (wparam)) {
150 case IDC_KEYREVOKE_CHOOSE:
151 balloon_msg_disable ();
152 const char *s;
153 mk_cert_fname (k->keyid, file, sizeof (file)-1);
154 s = get_filesave_dlg (dlg, _("Choose file to save the certificate"), NULL, file);
155 if (s && *s)
156 SetDlgItemText (dlg, IDC_KEYREVOKE_FILE, s);
157 return TRUE;
158
159 case IDOK:
160 list = GetDlgItem (dlg, IDC_KEYREVOKE_REASON);
161 idx = SendMessage (list, LB_GETCURSEL, 0, 0);
162 if (idx < 0 || idx > 3) {
163 show_balloon_msg (GetDlgItem (dlg, IDC_KEYREVOKE_REASON),
164 _("Please select a reason."), IDI_ERROR);
165 return TRUE;
166 }
167 if (!GetDlgItemText (dlg, IDC_KEYREVOKE_FILE, file,
168 DIM (file)-1)) {
169 show_balloon_msg (GetDlgItem (dlg, IDC_KEYREVOKE_FILE),
170 _("Please enter a file name."), IDI_ERROR);
171 return TRUE;
172 }
173 if (check_file_name (file, IS_PATH)) {
174 show_balloon_msg (GetDlgItem (dlg, IDC_KEYREVOKE_FILE),
175 _("The file name contains one or more illegal characters."),
176 IDI_ERROR);
177 return TRUE;
178 }
179
180 /* FIXME: check if use_desc is still required */
181 use_desc = 1;
182 if (!GetDlgItemText_utf8 (dlg, IDC_KEYREVOKE_TEXT, &desc))
183 use_desc = 0;
184 if (!GetDlgItemText_utf8 (dlg, IDC_KEYREVOKE_PWD, &pwd)) {
185 show_balloon_msg (GetDlgItem (dlg, IDC_KEYREVOKE_PWD),
186 _("Please enter the passphrase."), IDI_ERROR);
187 // FIXME: what if the key is not protected?
188 return TRUE;
189 }
190
191 inp_data = generate_revoke_input (idx, desc, pwd);
192 err = gpg_revoke_cert (k->internal, inp_data, k->keyid, &revcert);
193 sfree_if_alloc (inp_data);
194 sfree_if_alloc (desc);
195 sfree_if_alloc (pwd);
196 if (err) {
197 msg_box (dlg, gpgme_strerror (err),
198 _("Key Revocation Cert"), MB_ERR);
199 safe_free (revcert);
200 return TRUE;
201 }
202 else {
203 show_msg (dlg, 1000, _("Revocation certificate generated."));
204 msg_box (dlg, warning, _("Key Revocation Cert"), MB_INFO);
205 release_cert_as_file (revcert, file);
206 EndDialog (dlg, TRUE);
207 }
208 return TRUE;
209
210 case IDCANCEL:
211 EndDialog (dlg, FALSE);
212 return TRUE;
213 }
214 break;
215 }
216
217 return FALSE;
218 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26