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

Contents of /trunk/Src/wptKeyRevokeDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (show annotations)
Mon May 1 12:22:18 2006 UTC (18 years, 10 months ago) by twoaday
File size: 6592 byte(s)
See ChangeLog.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26