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