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