1 |
werner |
36 |
/* wptKeyRevokeDlg.cpp - Key revocation dialog |
2 |
twoaday |
328 |
* Copyright (C) 2001-2003, 2005-2006, 2009 Timo Schulz |
3 |
werner |
36 |
* 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 |
werner |
47 |
#include "resource.h" |
24 |
werner |
36 |
#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 |
twoaday |
247 |
void secure_filename (char *file, size_t len); |
36 |
werner |
36 |
|
37 |
twoaday |
247 |
|
38 |
werner |
36 |
/* 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 |
twoaday |
205 |
winpt_key_s k; |
44 |
twoaday |
328 |
const char *filename = _("Revocation-Certificate"); |
45 |
werner |
36 |
|
46 |
twoaday |
205 |
memset (&k, 0, sizeof (k)); |
47 |
|
|
if (winpt_get_pubkey (keyid, &k)) |
48 |
twoaday |
129 |
BUG (NULL); |
49 |
twoaday |
328 |
_snprintf (fname, flen-1, "%s_%s.asc", k.ext->uids->name, filename); |
50 |
twoaday |
247 |
secure_filename (fname, strlen (fname)); |
51 |
werner |
36 |
} |
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 |
twoaday |
247 |
err = gpg_data_release_and_set_file (rev, fname); |
64 |
|
|
if (err) |
65 |
twoaday |
129 |
msg_box (NULL, gpgme_strerror (err), _("Key Revocation Cert"), MB_ERR); |
66 |
twoaday |
200 |
safe_free (revcert); |
67 |
werner |
36 |
} |
68 |
|
|
|
69 |
|
|
|
70 |
twoaday |
200 |
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 |
twoaday |
328 |
SetDlgItemText (dlg, IDC_KEYREVOKE_PWDINF, _("&Passphrase:")); |
80 |
|
|
SetDlgItemText (dlg, IDC_KEYREVOKE_OUTINF, _("Output file:")); |
81 |
twoaday |
200 |
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 |
twoaday |
208 |
/* 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 |
twoaday |
200 |
|
99 |
twoaday |
208 |
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 |
werner |
36 |
/* 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 |
twoaday |
129 |
HWND list; |
125 |
twoaday |
65 |
int idx, use_desc; |
126 |
twoaday |
204 |
char file[256]; |
127 |
twoaday |
229 |
char *pwd=NULL; |
128 |
twoaday |
204 |
char *desc=NULL; |
129 |
werner |
36 |
char *inp_data = NULL, *revcert=NULL; |
130 |
twoaday |
129 |
const char *warning = |
131 |
twoaday |
248 |
_("Please move this certificate to a medium where it can be " |
132 |
twoaday |
129 |
"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 |
werner |
36 |
|
136 |
|
|
switch( msg ) { |
137 |
|
|
case WM_INITDIALOG: |
138 |
twoaday |
200 |
if (!lparam) |
139 |
twoaday |
225 |
BUG (0); |
140 |
twoaday |
208 |
k = (winpt_key_t)lparam; |
141 |
twoaday |
200 |
on_init_dialog (dlg); |
142 |
werner |
36 |
return TRUE; |
143 |
|
|
|
144 |
twoaday |
328 |
case WM_DESTROY: |
145 |
|
|
balloon_msg_disable (); |
146 |
|
|
break; |
147 |
|
|
|
148 |
werner |
36 |
case WM_COMMAND: |
149 |
twoaday |
129 |
switch (LOWORD (wparam)) { |
150 |
werner |
36 |
case IDC_KEYREVOKE_CHOOSE: |
151 |
twoaday |
328 |
balloon_msg_disable (); |
152 |
werner |
36 |
const char *s; |
153 |
twoaday |
256 |
mk_cert_fname (k->keyid, file, sizeof (file)-1); |
154 |
twoaday |
328 |
s = get_filesave_dlg (dlg, _("Choose file to save the certificate"), NULL, file); |
155 |
werner |
36 |
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 |
twoaday |
65 |
idx = SendMessage (list, LB_GETCURSEL, 0, 0); |
162 |
werner |
36 |
if (idx < 0 || idx > 3) { |
163 |
twoaday |
328 |
show_balloon_msg (GetDlgItem (dlg, IDC_KEYREVOKE_REASON), |
164 |
|
|
_("Please select a reason."), IDI_ERROR); |
165 |
werner |
36 |
return TRUE; |
166 |
|
|
} |
167 |
twoaday |
328 |
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 |
werner |
36 |
return TRUE; |
172 |
|
|
} |
173 |
twoaday |
247 |
if (check_file_name (file, IS_PATH)) { |
174 |
twoaday |
328 |
show_balloon_msg (GetDlgItem (dlg, IDC_KEYREVOKE_FILE), |
175 |
|
|
_("The file name contains one or more illegal characters."), |
176 |
|
|
IDI_ERROR); |
177 |
twoaday |
247 |
return TRUE; |
178 |
|
|
} |
179 |
twoaday |
442 |
|
180 |
|
|
/* FIXME: check if use_desc is still required */ |
181 |
werner |
36 |
use_desc = 1; |
182 |
twoaday |
204 |
if (!GetDlgItemText_utf8 (dlg, IDC_KEYREVOKE_TEXT, &desc)) |
183 |
werner |
36 |
use_desc = 0; |
184 |
twoaday |
229 |
if (!GetDlgItemText_utf8 (dlg, IDC_KEYREVOKE_PWD, &pwd)) { |
185 |
twoaday |
328 |
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 |
werner |
36 |
return TRUE; |
189 |
|
|
} |
190 |
|
|
|
191 |
twoaday |
204 |
inp_data = generate_revoke_input (idx, desc, pwd); |
192 |
twoaday |
208 |
err = gpg_revoke_cert (k->internal, inp_data, k->keyid, &revcert); |
193 |
twoaday |
225 |
sfree_if_alloc (inp_data); |
194 |
|
|
sfree_if_alloc (desc); |
195 |
twoaday |
229 |
sfree_if_alloc (pwd); |
196 |
werner |
36 |
if (err) { |
197 |
twoaday |
328 |
msg_box (dlg, gpgme_strerror (err), |
198 |
|
|
_("Key Revocation Cert"), MB_ERR); |
199 |
twoaday |
200 |
safe_free (revcert); |
200 |
twoaday |
225 |
return TRUE; |
201 |
werner |
36 |
} |
202 |
|
|
else { |
203 |
twoaday |
129 |
show_msg (dlg, 1000, _("Revocation certificate generated.")); |
204 |
|
|
msg_box (dlg, warning, _("Key Revocation Cert"), MB_INFO); |
205 |
werner |
36 |
release_cert_as_file (revcert, file); |
206 |
twoaday |
225 |
EndDialog (dlg, TRUE); |
207 |
werner |
36 |
} |
208 |
|
|
return TRUE; |
209 |
|
|
|
210 |
|
|
case IDCANCEL: |
211 |
|
|
EndDialog (dlg, FALSE); |
212 |
twoaday |
256 |
return TRUE; |
213 |
werner |
36 |
} |
214 |
|
|
break; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
return FALSE; |
218 |
|
|
} |