1 |
werner |
36 |
/* wptKeygenDlg.cpp - Key Generation dialog |
2 |
|
|
* Copyright (C) 2000-2005 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
|
|
|
24 |
|
|
#include <windows.h> |
25 |
|
|
|
26 |
werner |
47 |
#include "resource.h" |
27 |
werner |
36 |
#include "wptTypes.h" |
28 |
|
|
#include "wptNLS.h" |
29 |
|
|
#include "wptGPG.h" |
30 |
|
|
#include "wptCommonCtl.h" |
31 |
|
|
#include "wptContext.h" /* for passphrase_s */ |
32 |
|
|
#include "wptDlgs.h" |
33 |
|
|
#include "wptW32API.h" |
34 |
|
|
#include "wptVersion.h" |
35 |
|
|
#include "wptErrors.h" |
36 |
|
|
|
37 |
twoaday |
204 |
|
38 |
werner |
36 |
/* All valid key generation combination. */ |
39 |
|
|
enum gpg_keytype_t { |
40 |
|
|
GPG_KEYGEN_NONE = 0, |
41 |
|
|
GPG_KEYGEN_DSA_ELG = 1, |
42 |
|
|
GPG_KEYGEN_DSA_RSA = 2, |
43 |
|
|
GPG_KEYGEN_DSA_SIG = 3, |
44 |
|
|
GPG_KEYGEN_RSA_SIG = 4, |
45 |
|
|
GPG_KEYGEN_RSA = 5, |
46 |
|
|
GPG_KEYGEN_RSA_RSA = 6 /*PGP*/ |
47 |
|
|
}; |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
static const char key_params[] = |
51 |
|
|
"<GnupgKeyParms format=\"internal\">\n" |
52 |
|
|
"Key-Type: %s\n" |
53 |
|
|
"Key-Usage: sign\n" |
54 |
|
|
"Key-Length: %d\n" |
55 |
|
|
"Subkey-Type: %s\n" |
56 |
|
|
"Subkey-Length: %d\n" |
57 |
|
|
"Name-Real: %s\n" |
58 |
|
|
"Name-Email: %s\n" |
59 |
|
|
"Expire-Date: %s\n" |
60 |
|
|
"Passphrase: %s\n" |
61 |
|
|
"</GnupgKeyParms>\n"; |
62 |
|
|
|
63 |
|
|
static const char key_params_with_comment[] = |
64 |
|
|
"<GnupgKeyParms format=\"internal\">\n" |
65 |
|
|
"Key-Type: %s\n" |
66 |
|
|
"Key-Usage: sign\n" |
67 |
|
|
"Key-Length: %d\n" |
68 |
|
|
"Subkey-Type: %s\n" |
69 |
|
|
"Subkey-Length: %d\n" |
70 |
|
|
"Name-Real: %s\n" |
71 |
|
|
"Name-Comment: %s\n" |
72 |
|
|
"Name-Email: %s\n" |
73 |
|
|
"Expire-Date: %s\n" |
74 |
|
|
"Passphrase: %s\n" |
75 |
|
|
"</GnupgKeyParms>\n"; |
76 |
|
|
|
77 |
|
|
static const char key_params_one[] = |
78 |
|
|
"<GnupgKeyParms format=\"internal\">\n" |
79 |
|
|
"Key-Type: %s\n" |
80 |
|
|
"Key-Length: %d\n" |
81 |
|
|
"Key-Usage: %s\n" |
82 |
|
|
"Name-Real: %s\n" |
83 |
|
|
"Name-Email: %s\n" |
84 |
|
|
"Expire-Date: %s\n" |
85 |
|
|
"Passphrase: %s\n" |
86 |
|
|
"</GnupgKeyParms>\n"; |
87 |
|
|
|
88 |
|
|
static const char key_params_one_with_comment[] = |
89 |
|
|
"<GnupgKeyParms format=\"internal\">\n" |
90 |
|
|
"Key-Type: %s\n" |
91 |
|
|
"Key-Length: %d\n" |
92 |
|
|
"Key-Usage: %s\n" |
93 |
|
|
"Name-Real: %s\n" |
94 |
|
|
"Name-Comment: %s\n" |
95 |
twoaday |
204 |
"Name-Email: %s\n" |
96 |
werner |
36 |
"Expire-Date: %s\n" |
97 |
|
|
"Passphrase: %s\n" |
98 |
|
|
"</GnupgKeyParms>\n"; |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/* Generate the GPG specific genkey params with the given information. |
102 |
|
|
@keytype: See valid combinations. |
103 |
|
|
@bits: Length in bits. |
104 |
|
|
@user: user-ID name |
105 |
|
|
@comment: comment for the user-ID. |
106 |
|
|
@email: email address. |
107 |
|
|
@expdata: date of expiration or NULL. |
108 |
|
|
@passphrase: the actual passphrase. |
109 |
|
|
Return value: the gen. params. */ |
110 |
|
|
static char* |
111 |
|
|
gpg_genkey_params (int keytype, int bits, |
112 |
|
|
const char *user, const char *comment, const char *email, |
113 |
|
|
const char *expdate, const char *passphrase) |
114 |
|
|
{ |
115 |
|
|
char *p = NULL; |
116 |
|
|
int addsize = strlen ("sign encrypt"); |
117 |
|
|
int size = 0; |
118 |
|
|
|
119 |
|
|
if (keytype == GPG_KEYGEN_NONE) |
120 |
|
|
return NULL; |
121 |
|
|
|
122 |
twoaday |
130 |
size = strlen (user) + strlen (email) + strlen (passphrase) + addsize + 48; |
123 |
|
|
if (comment && *comment) |
124 |
|
|
size += strlen (key_params_with_comment) + strlen (comment); |
125 |
|
|
else |
126 |
|
|
size += strlen (key_params); |
127 |
|
|
if (expdate) |
128 |
|
|
size += strlen (expdate) + 1; |
129 |
twoaday |
201 |
p = new char[size+1]; |
130 |
twoaday |
130 |
if (!p) |
131 |
|
|
BUG (0); |
132 |
|
|
/* XXX: simply the whole switch-case code. */ |
133 |
|
|
if (comment && *comment) { |
134 |
werner |
36 |
switch( keytype ) { |
135 |
|
|
case GPG_KEYGEN_DSA_ELG: |
136 |
|
|
sprintf (p, key_params_with_comment, |
137 |
|
|
"DSA", 1024, "ELG-E", bits, user, comment, email, |
138 |
|
|
expdate ? expdate : "0", passphrase); |
139 |
|
|
break; |
140 |
|
|
|
141 |
|
|
case GPG_KEYGEN_DSA_RSA: |
142 |
|
|
sprintf( p, key_params_with_comment, |
143 |
|
|
"DSA", 1024, "RSA", bits, user, comment, email, |
144 |
|
|
expdate ? expdate : "0", passphrase ); |
145 |
|
|
break; |
146 |
|
|
|
147 |
|
|
case GPG_KEYGEN_DSA_SIG: |
148 |
|
|
sprintf( p, key_params_one_with_comment, |
149 |
|
|
"DSA", 1024, "sign", |
150 |
|
|
user, comment, email, |
151 |
|
|
expdate ? expdate : "0", passphrase ); |
152 |
|
|
break; |
153 |
|
|
|
154 |
|
|
case GPG_KEYGEN_RSA_SIG: |
155 |
|
|
sprintf( p, key_params_one_with_comment, |
156 |
|
|
"RSA", bits, "sign", |
157 |
|
|
user, comment, email, |
158 |
|
|
expdate ? expdate : "0", passphrase ); |
159 |
|
|
break; |
160 |
|
|
|
161 |
|
|
case GPG_KEYGEN_RSA: |
162 |
|
|
sprintf( p, key_params_one_with_comment, |
163 |
|
|
"RSA", bits, "sign encrypt", |
164 |
|
|
user, comment, email, |
165 |
|
|
expdate ? expdate : "0", passphrase ); |
166 |
|
|
break; |
167 |
|
|
|
168 |
|
|
case GPG_KEYGEN_RSA_RSA: |
169 |
|
|
sprintf( p, key_params_with_comment, |
170 |
|
|
"RSA", bits, "RSA", bits, user, comment, email, |
171 |
|
|
expdate? expdate : "0", passphrase ); |
172 |
|
|
break; |
173 |
|
|
|
174 |
|
|
default: |
175 |
twoaday |
201 |
free_if_alloc (p); |
176 |
werner |
36 |
break; |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
else { |
180 |
|
|
switch ( keytype ) { |
181 |
|
|
case GPG_KEYGEN_DSA_ELG: |
182 |
|
|
sprintf( p, key_params, |
183 |
|
|
"DSA", 1024, "ELG-E", bits, user, email, |
184 |
|
|
expdate ? expdate : "0", passphrase ); |
185 |
|
|
break; |
186 |
|
|
|
187 |
|
|
case GPG_KEYGEN_DSA_RSA: |
188 |
|
|
sprintf( p, key_params, |
189 |
|
|
"DSA", 1024, "RSA", bits, user, email, |
190 |
|
|
expdate ? expdate : "0", passphrase ); |
191 |
|
|
break; |
192 |
|
|
|
193 |
|
|
case GPG_KEYGEN_DSA_SIG: |
194 |
|
|
sprintf( p, key_params_one, |
195 |
|
|
"DSA", 1024, "sign", |
196 |
|
|
user, email, |
197 |
|
|
expdate ? expdate : "0", passphrase ); |
198 |
|
|
break; |
199 |
|
|
|
200 |
|
|
case GPG_KEYGEN_RSA_SIG: |
201 |
|
|
sprintf( p, key_params_one, |
202 |
|
|
"RSA", bits, "sign", |
203 |
|
|
user, email, |
204 |
|
|
expdate ? expdate : "0", passphrase ); |
205 |
|
|
break; |
206 |
|
|
|
207 |
|
|
case GPG_KEYGEN_RSA: |
208 |
|
|
sprintf( p, key_params_one, |
209 |
|
|
"RSA", bits, "sign encrypt", |
210 |
|
|
user, email, |
211 |
|
|
expdate ? expdate : "0", passphrase ); |
212 |
|
|
break; |
213 |
|
|
|
214 |
|
|
case GPG_KEYGEN_RSA_RSA: |
215 |
|
|
sprintf( p, key_params, |
216 |
|
|
"RSA", bits, "RSA", bits, user, email, |
217 |
|
|
expdate? expdate : "0", passphrase ); |
218 |
|
|
break; |
219 |
|
|
|
220 |
|
|
default: |
221 |
twoaday |
201 |
free_if_alloc (p); |
222 |
werner |
36 |
break; |
223 |
|
|
} |
224 |
|
|
} |
225 |
|
|
return p; |
226 |
twoaday |
32 |
} |
227 |
werner |
36 |
|
228 |
|
|
|
229 |
|
|
/* Generate a key with the given params @params. @prog_cb is a user defined |
230 |
|
|
progress callback which is called during the generation. |
231 |
|
|
@fpr will store the fingerprint of the generated key. |
232 |
|
|
Return value: 0 on success. */ |
233 |
|
|
gpgme_error_t |
234 |
|
|
gpg_genkey (const char *params, gpgme_progress_cb_t prog_cb, char **fpr) |
235 |
|
|
{ |
236 |
|
|
gpgme_error_t err = 0; |
237 |
|
|
gpgme_ctx_t ctx; |
238 |
|
|
gpgme_genkey_result_t res; |
239 |
|
|
|
240 |
|
|
err = gpgme_new(&ctx); |
241 |
|
|
if (err) |
242 |
|
|
return err; |
243 |
|
|
if (prog_cb) |
244 |
|
|
gpgme_set_progress_cb (ctx, prog_cb, NULL); |
245 |
|
|
err = gpgme_op_genkey (ctx, params, NULL, NULL); |
246 |
|
|
if (!err) { |
247 |
|
|
res = gpgme_op_genkey_result (ctx); |
248 |
twoaday |
201 |
*fpr = res->fpr? m_strdup (res->fpr) : NULL; |
249 |
werner |
36 |
} |
250 |
|
|
gpgme_release (ctx); |
251 |
|
|
return err; |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
/* Clear all dialog fields. */ |
257 |
|
|
static void |
258 |
|
|
clear_dlg_fields (HWND dlg) |
259 |
|
|
{ |
260 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_SUBKEYBITS, ""); |
261 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_NAME, ""); |
262 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_EMAIL, ""); |
263 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_COMMENT, ""); |
264 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_EXPDATE, ""); |
265 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_PASSPHRASE, ""); |
266 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_PWDCHECK, ""); |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
|
270 |
|
|
/* Ask the user if a keyring backup is wanted and if so, |
271 |
|
|
backup both keyrings to the selected folder. @dlg is |
272 |
|
|
the handle of the parent window.*/ |
273 |
|
|
static void |
274 |
|
|
backup_keyrings (HWND dlg) |
275 |
|
|
{ |
276 |
twoaday |
130 |
const char *name; |
277 |
|
|
char *path = NULL; |
278 |
|
|
char *keyring = NULL; |
279 |
werner |
36 |
int id; |
280 |
|
|
|
281 |
|
|
path = get_gnupg_path (); |
282 |
|
|
if (!path) |
283 |
|
|
BUG (dlg); |
284 |
|
|
id = msg_box (dlg, |
285 |
|
|
_("It is STRONGLY recommend that you backup your keyrings because they both " |
286 |
|
|
"contain VERY important data.\nRemember that your hard disk can crash or the " |
287 |
|
|
"files can be deleted by accident; so it is a good\nidea to store them on " |
288 |
|
|
"a different mass stoarge like a floppy or CDR!\n\n" |
289 |
|
|
"Backup your keyrings now?"), |
290 |
|
|
_("WARNING - Important hint" ), MB_YESNO); |
291 |
|
|
if (id == IDYES) { |
292 |
twoaday |
130 |
name = get_filesave_dlg (dlg, _("Destination for Public Keyring"), |
293 |
|
|
NULL, "pubring.gpg"); |
294 |
werner |
36 |
if( name ) { |
295 |
twoaday |
130 |
keyring = make_filename (path, "pubring", "gpg"); |
296 |
|
|
if( !CopyFile (keyring, name, FALSE)) |
297 |
|
|
log_box (_("Key Generation"), MB_ERR, |
298 |
|
|
_("Could not copy %s -> %s"), keyring, name); |
299 |
|
|
free_if_alloc (keyring); |
300 |
werner |
36 |
} |
301 |
twoaday |
130 |
name = get_filesave_dlg (dlg, _("Destination for Secret Keyring"), |
302 |
|
|
NULL, "secring.gpg"); |
303 |
werner |
36 |
if( name ) { |
304 |
twoaday |
130 |
keyring = make_filename (path, "secring", "gpg"); |
305 |
|
|
if (!CopyFile (keyring, name, FALSE)) |
306 |
|
|
log_box (_("Key Generation"), MB_ERR, |
307 |
|
|
_("Could not copy %s -> %s"), keyring, name); |
308 |
|
|
free_if_alloc (keyring); |
309 |
werner |
36 |
} |
310 |
|
|
} |
311 |
twoaday |
130 |
free_if_alloc (path); |
312 |
werner |
36 |
} |
313 |
|
|
|
314 |
|
|
|
315 |
|
|
/* Fill in all valid GPG algorithms. */ |
316 |
|
|
static void |
317 |
|
|
fill_keytype_box (HWND dlg) |
318 |
|
|
{ |
319 |
twoaday |
130 |
HWND cb = GetDlgItem (dlg, IDC_KEYGEN_KEYTYPE); |
320 |
werner |
36 |
|
321 |
twoaday |
130 |
#define addstr(cb, str) \ |
322 |
|
|
SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)(str)) |
323 |
|
|
addstr (cb, _("DSA and ELG (default)")); |
324 |
|
|
addstr (cb, _("DSA and RSA")); |
325 |
|
|
addstr (cb, _("DSA sign only")); |
326 |
|
|
addstr (cb, _("RSA sign only")); |
327 |
|
|
addstr (cb, _("RSA sign and encrypt")); |
328 |
|
|
addstr (cb, _("RSA and RSA (PGP)") ); |
329 |
|
|
SendMessage (cb, CB_SETCURSEL, 0, 0); |
330 |
werner |
36 |
#undef addstr |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
|
334 |
|
|
/* Check that the given date lies not in the past. |
335 |
|
|
Return value: 1 on success. */ |
336 |
|
|
int |
337 |
|
|
keygen_check_date (SYSTEMTIME *st) |
338 |
|
|
{ |
339 |
|
|
SYSTEMTIME t; |
340 |
|
|
|
341 |
|
|
GetSystemTime (&t); |
342 |
|
|
if (st->wYear > t.wYear || st->wMonth > t.wMonth) |
343 |
|
|
return 1; |
344 |
|
|
else if (st->wYear < t.wYear || st->wMonth < t.wMonth || st->wDay < t.wDay) |
345 |
twoaday |
130 |
return 0; |
346 |
werner |
36 |
return 1; |
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
|
350 |
|
|
/* Dialog box procedure for key generation. */ |
351 |
|
|
BOOL CALLBACK |
352 |
|
|
keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
353 |
|
|
{ |
354 |
|
|
static genkey_s *ctx; |
355 |
|
|
SYSTEMTIME st; |
356 |
|
|
gpgme_error_t err; |
357 |
twoaday |
204 |
char *utf8_name =NULL, *utf8_comment = NULL; |
358 |
|
|
char email[128]; |
359 |
twoaday |
130 |
char *pwd; |
360 |
werner |
36 |
char t[64], *expire = NULL, *fpr=NULL; |
361 |
twoaday |
130 |
int bits, use_comment, keytype = 0; |
362 |
|
|
int cancel = 0; |
363 |
|
|
char *p; |
364 |
werner |
36 |
|
365 |
|
|
switch ( msg ) { |
366 |
|
|
case WM_INITDIALOG: |
367 |
twoaday |
73 |
if (lparam != 0) |
368 |
werner |
36 |
ctx = (genkey_s *)lparam; |
369 |
twoaday |
130 |
SetWindowText (dlg, _("Key Generation")); |
370 |
|
|
SetDlgItemText(dlg, IDC_KEYGEN_INFO, |
371 |
werner |
36 |
_("NOTE: Key generation can be a lengthy process! Please wait until " |
372 |
twoaday |
130 |
"you get the message that key generation was finished.")); |
373 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_SUBKEYINF, _("Subkey size in &bits")); |
374 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_NAMEINF, _("&Real name")); |
375 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_COMMINF, _("&Comment (optional)")); |
376 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_EMAILINF, _("Email &address")); |
377 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_EXPINF, _("&Expire date")); |
378 |
|
|
SetDlgItemText (dlg, IDC_KEYGEN_KEYTYPEINF, _("Key &type")); |
379 |
werner |
36 |
SetDlgItemText (dlg, IDC_KEYGEN_EXPNEVER, _("&Never")); |
380 |
twoaday |
101 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
381 |
werner |
36 |
|
382 |
twoaday |
130 |
SetDlgItemInt (dlg, IDC_KEYGEN_SUBKEYBITS, DFAULT_KEYSIZE, FALSE); |
383 |
werner |
36 |
CheckDlgButton (dlg, IDC_KEYGEN_HIDEPWD, BST_CHECKED); |
384 |
|
|
CheckDlgButton (dlg, IDC_KEYGEN_EXPNEVER, BST_CHECKED); |
385 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), FALSE); |
386 |
|
|
fill_keytype_box (dlg); |
387 |
|
|
center_window (dlg, NULL); |
388 |
|
|
SetForegroundWindow (dlg); |
389 |
|
|
return TRUE; |
390 |
|
|
|
391 |
|
|
case WM_SYSCOMMAND: |
392 |
twoaday |
130 |
if (LOWORD (wparam) == SC_CLOSE) |
393 |
werner |
36 |
EndDialog (dlg, TRUE); |
394 |
|
|
return FALSE; |
395 |
|
|
|
396 |
|
|
case WM_COMMAND: |
397 |
|
|
if (HIWORD (wparam) == BN_CLICKED && |
398 |
|
|
LOWORD (wparam) == IDC_KEYGEN_EXPNEVER) { |
399 |
|
|
int never = IsDlgButtonChecked (dlg, IDC_KEYGEN_EXPNEVER); |
400 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), !never); |
401 |
|
|
} |
402 |
|
|
|
403 |
twoaday |
130 |
switch (LOWORD (wparam)) { |
404 |
werner |
36 |
case IDOK: |
405 |
|
|
bits = GetDlgItemInt (dlg, IDC_KEYGEN_SUBKEYBITS, NULL, FALSE); |
406 |
|
|
if (bits < 1024 || bits > 4096) { |
407 |
|
|
msg_box (dlg, _("Invalid value. Allowed values 1024-4096 bits."), |
408 |
|
|
_("Key Generation"), MB_ERR); |
409 |
|
|
return FALSE; |
410 |
|
|
} |
411 |
|
|
if (bits > DFAULT_KEYSIZE) { |
412 |
twoaday |
130 |
int id = msg_box (dlg, _("Do you really need such a large key?"), |
413 |
werner |
36 |
_("Key Generation"), MB_YESNO); |
414 |
|
|
if (id == IDNO) |
415 |
|
|
bits = DFAULT_KEYSIZE; |
416 |
|
|
} |
417 |
twoaday |
204 |
if (!GetDlgItemText_utf8 (dlg, IDC_KEYGEN_NAME, &utf8_name)) { |
418 |
|
|
msg_box (dlg, _("Please enter the name."), _("Key Generation"), MB_ERR); |
419 |
werner |
36 |
return FALSE; |
420 |
|
|
} |
421 |
twoaday |
204 |
if (strchr (utf8_name, '@')) { |
422 |
werner |
36 |
msg_box (dlg, _("Please do not enter the email address in the name field."), |
423 |
|
|
_("Key Generation"), MB_INFO); |
424 |
twoaday |
204 |
free_if_alloc (utf8_name); |
425 |
werner |
36 |
return FALSE; |
426 |
|
|
} |
427 |
twoaday |
130 |
if (!GetDlgItemText (dlg, IDC_KEYGEN_EMAIL, email, sizeof (email) -1) |
428 |
twoaday |
190 |
|| check_email_address (email)) { |
429 |
twoaday |
130 |
msg_box (dlg, _("Please enter a valid email address."), |
430 |
|
|
_("Key Generation"), MB_ERR); |
431 |
twoaday |
204 |
free_if_alloc (utf8_name); |
432 |
werner |
36 |
return FALSE; |
433 |
|
|
} |
434 |
twoaday |
204 |
use_comment = GetDlgItemText_utf8 (dlg, IDC_KEYGEN_COMMENT, |
435 |
|
|
&utf8_comment); |
436 |
|
|
if (use_comment > 0 && strchr (utf8_comment, '@')) { |
437 |
twoaday |
130 |
msg_box (dlg, _("Please do NOT enter the email address in the comment field."), |
438 |
|
|
_("Key Generation"), MB_INFO); |
439 |
twoaday |
204 |
free_if_alloc (utf8_name); |
440 |
|
|
free_if_alloc (utf8_comment); |
441 |
werner |
36 |
return FALSE; |
442 |
|
|
} |
443 |
|
|
keytype = SendDlgItemMessage (dlg, IDC_KEYGEN_KEYTYPE, CB_GETCURSEL, 0, 0) + 1; |
444 |
|
|
if (IsDlgButtonChecked (dlg, IDC_KEYGEN_EXPNEVER)) |
445 |
|
|
expire = NULL; |
446 |
|
|
else { |
447 |
|
|
DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYGEN_EXPDATE), &st); |
448 |
|
|
_snprintf (t, DIM (t)-1, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay); |
449 |
|
|
expire = t; |
450 |
|
|
} |
451 |
twoaday |
130 |
|
452 |
|
|
pwd = request_passphrase2 (_("Key Generation"), PASSDLG_STRICT, &cancel); |
453 |
|
|
if (cancel) { |
454 |
|
|
sfree_if_alloc (pwd); |
455 |
twoaday |
204 |
free_if_alloc (utf8_name); |
456 |
|
|
free_if_alloc (utf8_comment); |
457 |
twoaday |
130 |
return FALSE; |
458 |
|
|
} |
459 |
|
|
if (!pwd) { |
460 |
|
|
msg_box (dlg, _("Please enter the passphrase."), |
461 |
|
|
_("Key Generation"), MB_ERR); |
462 |
twoaday |
204 |
free_if_alloc (utf8_name); |
463 |
|
|
free_if_alloc (utf8_comment); |
464 |
werner |
36 |
return FALSE; |
465 |
|
|
} |
466 |
twoaday |
130 |
|
467 |
twoaday |
204 |
if (!use_comment && !strlen (utf8_comment)) |
468 |
twoaday |
130 |
p = gpg_genkey_params (keytype, bits, utf8_name, NULL, |
469 |
|
|
email, expire, pwd); |
470 |
twoaday |
204 |
else |
471 |
twoaday |
130 |
p = gpg_genkey_params (keytype, bits, utf8_name, utf8_comment, |
472 |
|
|
email, expire, pwd); |
473 |
twoaday |
204 |
free_if_alloc (utf8_name); |
474 |
|
|
free_if_alloc (utf8_comment); |
475 |
twoaday |
130 |
keygen_cb_dlg_create (); |
476 |
werner |
36 |
err = gpg_genkey (p, keygen_cb, &fpr); |
477 |
twoaday |
130 |
sfree_if_alloc (pwd); |
478 |
twoaday |
115 |
if (p) { |
479 |
werner |
36 |
wipememory (p, strlen (p)); /* burn the passphrase! */ |
480 |
twoaday |
201 |
free_if_alloc (p); |
481 |
werner |
36 |
} |
482 |
twoaday |
211 |
keygen_cb_dlg_destroy (1); |
483 |
twoaday |
115 |
if (err) { |
484 |
twoaday |
201 |
free_if_alloc (fpr); |
485 |
twoaday |
115 |
msg_box (dlg, gpgme_strerror (err), _("Key Generation"), MB_ERR); |
486 |
werner |
36 |
return FALSE; |
487 |
|
|
} |
488 |
twoaday |
115 |
status_box (dlg, _("Key Generation completed"), _("GnuPG Status")); |
489 |
werner |
36 |
|
490 |
|
|
keycache_update (0, fpr); |
491 |
|
|
keycache_update (1, fpr); |
492 |
twoaday |
201 |
free_if_alloc (fpr); |
493 |
werner |
36 |
|
494 |
|
|
clear_dlg_fields (dlg); |
495 |
|
|
backup_keyrings (dlg); |
496 |
twoaday |
150 |
if (ctx) |
497 |
|
|
ctx->cancel = 0; |
498 |
werner |
36 |
EndDialog (dlg, TRUE); |
499 |
|
|
return TRUE; |
500 |
|
|
|
501 |
|
|
case IDCANCEL: |
502 |
twoaday |
150 |
if (ctx) |
503 |
|
|
ctx->cancel = 1; |
504 |
|
|
EndDialog (dlg, FALSE); |
505 |
werner |
36 |
return FALSE; |
506 |
|
|
} |
507 |
|
|
break; |
508 |
|
|
} |
509 |
|
|
|
510 |
|
|
return FALSE; |
511 |
|
|
} |
512 |
|
|
|
513 |
|
|
|
514 |
|
|
BOOL CALLBACK |
515 |
|
|
keygen_wizard_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
516 |
|
|
{ |
517 |
|
|
static genkey_s *ctx; |
518 |
|
|
static int pubkey_algo = GPG_KEYGEN_DSA_ELG; |
519 |
|
|
gpgme_error_t err; |
520 |
twoaday |
204 |
char email[128]; |
521 |
|
|
char *utf8_name=NULL, * p, *fpr=NULL; |
522 |
werner |
36 |
char * pass = NULL; |
523 |
|
|
int cancel = 0; |
524 |
|
|
|
525 |
|
|
|
526 |
|
|
switch( msg ) { |
527 |
|
|
case WM_INITDIALOG: |
528 |
|
|
ctx = (genkey_s *)lparam; |
529 |
|
|
if (!ctx || (ctx && ctx->interactive == 0)) |
530 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_KEYWIZARD_EXPERT), FALSE); |
531 |
|
|
SetDlgItemText (dlg, IDC_KEYWIZARD_USERSA, _("&Prefer RSA keys")); |
532 |
|
|
SetDlgItemText (dlg, IDC_KEYWIZARD_NAMEINF, _("Real name:")); |
533 |
|
|
SetDlgItemText (dlg, IDC_KEYWIZARD_EMAILINF, _("Email address:")); |
534 |
|
|
SetDlgItemText (dlg, IDC_KEYWIZARD_TITLEINF, _("Name and E-Mail Assignment")); |
535 |
|
|
SetDlgItemText (dlg, IDC_KEYWIZARD_TEXT1INF, _("Every key pair must have a name associated with it. The name and\nemail address let your correspondents that your public key they are\nusing belongs to us.")); |
536 |
|
|
SetDlgItemText (dlg, IDC_KEYWIZARD_TEXT2INF, _("By accosiating an email address with your key pair, you will enable WinPT to assist your correspondents in selecting the correct public\nkey when communicating with you.")); |
537 |
|
|
SetWindowText (dlg, _("Key Generation Wizard")); |
538 |
twoaday |
105 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
539 |
|
|
SetDlgItemText (dlg, IDC_KEYWIZARD_EXPERT, _("E&xpert")); |
540 |
werner |
36 |
SetForegroundWindow (dlg); |
541 |
|
|
center_window (dlg, NULL); |
542 |
|
|
break; |
543 |
|
|
|
544 |
|
|
case WM_SYSCOMMAND: |
545 |
twoaday |
204 |
if (LOWORD (wparam) == SC_CLOSE) |
546 |
|
|
EndDialog (dlg, FALSE); |
547 |
werner |
36 |
|
548 |
|
|
case WM_COMMAND: |
549 |
twoaday |
204 |
switch (LOWORD( wparam)) { |
550 |
werner |
36 |
case IDC_KEYWIZARD_EXPERT: |
551 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYGEN, dlg, |
552 |
twoaday |
73 |
keygen_dlg_proc, 0); |
553 |
werner |
36 |
EndDialog (dlg, TRUE); |
554 |
|
|
break; |
555 |
|
|
|
556 |
|
|
case IDOK: |
557 |
twoaday |
204 |
if (!GetDlgItemText_utf8 (dlg, IDC_KEYWIZARD_NAME, &utf8_name)) { |
558 |
|
|
msg_box (dlg, _("Please enter the name."), |
559 |
|
|
_("Key Generation Wizard"), MB_ERR); |
560 |
werner |
36 |
return FALSE; |
561 |
|
|
} |
562 |
twoaday |
204 |
if (strchr (utf8_name, '@')) { |
563 |
werner |
36 |
msg_box (dlg, _("Please do not enter the email address in the name field."), |
564 |
|
|
_("Key Generation Wizard"), MB_WARN); |
565 |
twoaday |
204 |
free_if_alloc (utf8_name); |
566 |
werner |
36 |
return FALSE; |
567 |
|
|
} |
568 |
twoaday |
190 |
if (!GetDlgItemText(dlg, IDC_KEYWIZARD_EMAIL, email, sizeof email-1 ) |
569 |
|
|
|| check_email_address (email)) { |
570 |
|
|
msg_box (dlg, _("Please enter a valid email address."), |
571 |
|
|
_("Key Generation Wizard"), MB_ERR); |
572 |
twoaday |
204 |
free_if_alloc (utf8_name); |
573 |
werner |
36 |
return FALSE; |
574 |
|
|
} |
575 |
|
|
if (strchr (email, '<') || strchr (email, '>')) { |
576 |
|
|
msg_box (dlg, _("Please do not add '<' or '>' to the email address."), |
577 |
|
|
_("Key Generation Wizard"), MB_WARN); |
578 |
twoaday |
204 |
free_if_alloc (utf8_name); |
579 |
werner |
36 |
return FALSE; |
580 |
|
|
} |
581 |
|
|
pass = request_passphrase2 (_("Key Generation"), PASSDLG_STRICT, &cancel); |
582 |
twoaday |
204 |
if (cancel) { |
583 |
|
|
free_if_alloc (utf8_name); |
584 |
werner |
36 |
return FALSE; |
585 |
twoaday |
204 |
} |
586 |
werner |
36 |
if (IsDlgButtonChecked (dlg, IDC_KEYWIZARD_USERSA)) |
587 |
|
|
pubkey_algo = GPG_KEYGEN_DSA_RSA; |
588 |
|
|
p = gpg_genkey_params (pubkey_algo, DFAULT_KEYSIZE, utf8_name, |
589 |
|
|
NULL, email, NULL, pass); |
590 |
twoaday |
201 |
free_if_alloc (utf8_name); |
591 |
werner |
36 |
keygen_cb_dlg_create(); |
592 |
|
|
err = gpg_genkey (p, keygen_cb, &fpr); |
593 |
twoaday |
211 |
keygen_cb_dlg_destroy (1); |
594 |
twoaday |
201 |
if (p) { |
595 |
|
|
wipememory (p, strlen (p)); |
596 |
|
|
free_if_alloc (p); |
597 |
werner |
36 |
} |
598 |
|
|
sfree_if_alloc (pass); |
599 |
twoaday |
201 |
if (err) { |
600 |
|
|
msg_box (dlg, gpgme_strerror( err ), _("Key Generation Wizard"), MB_ERR); |
601 |
|
|
free_if_alloc (fpr); |
602 |
werner |
36 |
return FALSE; |
603 |
|
|
} |
604 |
twoaday |
204 |
status_box (dlg, _("Key Generation completed"), _("GnuPG Status")); |
605 |
werner |
36 |
keycache_update (0, fpr); |
606 |
|
|
keycache_update (1, fpr); |
607 |
twoaday |
201 |
free_if_alloc (fpr); |
608 |
twoaday |
150 |
|
609 |
werner |
36 |
backup_keyrings (dlg); |
610 |
twoaday |
150 |
if (ctx) |
611 |
|
|
ctx->cancel = 0; |
612 |
werner |
36 |
EndDialog (dlg, TRUE); |
613 |
|
|
break; |
614 |
|
|
|
615 |
|
|
case IDCANCEL: |
616 |
twoaday |
150 |
if (ctx) |
617 |
|
|
ctx->cancel = 1; |
618 |
|
|
EndDialog (dlg, FALSE); |
619 |
werner |
36 |
break; |
620 |
|
|
} |
621 |
|
|
break; |
622 |
|
|
} |
623 |
|
|
return FALSE; |
624 |
|
|
} |