1 |
/* WinPT.cpp - Windows Privacy Tray (WinPT) |
/* WinPT.cpp - Windows Privacy Tray (WinPT) |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
40 |
#include "wptCardEdit.h" |
#include "wptCardEdit.h" |
41 |
#include "wptCrypto.h" |
#include "wptCrypto.h" |
42 |
|
|
43 |
#define MIN_GPG_VER "1.4.2" /* Minimal GPG version. */ |
#define MIN_GPG_VER "1.4.2" /* Minimal GPG version. */ |
44 |
#define MIN_GPGME_VER "1.2.0" /* Minimal GPGME version. */ |
#define MIN_GPGME_VER NEED_GPGME_VERSION /* Minimal GPGME version. */ |
45 |
|
|
46 |
|
|
47 |
HINSTANCE glob_hinst; /* global instance for the dialogs */ |
HINSTANCE glob_hinst; /* global instance for the dialogs */ |
114 |
} |
} |
115 |
|
|
116 |
|
|
117 |
|
/* Load the GPG environment. On the first start, some |
118 |
|
checks are performed to find out in what state GPG is. |
119 |
|
Return value: 0 everything OK. |
120 |
|
>0 fatal error. |
121 |
|
-1 public keyring is empty or does not exist. */ |
122 |
|
static int |
123 |
|
load_gpg_env (void) |
124 |
|
{ |
125 |
|
SECURITY_ATTRIBUTES sec_attr; |
126 |
|
char *p; |
127 |
|
char *pkr; |
128 |
|
|
129 |
|
p = get_reg_entry_gpg4win ("gpg.exe"); |
130 |
|
if (!p) |
131 |
|
return (1); |
132 |
|
if (file_exist_check (p)) { |
133 |
|
free_if_alloc (p); |
134 |
|
return (1); |
135 |
|
} |
136 |
|
free_if_alloc (p); |
137 |
|
p = multi_gnupg_path (0); |
138 |
|
if (p && dir_exist_check (p)) { |
139 |
|
memset (&sec_attr, 0, sizeof (sec_attr)); |
140 |
|
sec_attr.nLength = sizeof (sec_attr); |
141 |
|
if (!CreateDirectory (p, &sec_attr)) { |
142 |
|
msg_box (NULL, _("Could not create GPG home directory"), |
143 |
|
_("WinPT Error"), MB_ERR); |
144 |
|
free_if_alloc (p); |
145 |
|
return (2); |
146 |
|
} |
147 |
|
} |
148 |
|
pkr = make_filename (p, "pubring", "gpg"); |
149 |
|
free_if_alloc (p); |
150 |
|
if (!pkr) |
151 |
|
return -1; |
152 |
|
if (get_file_size (pkr) == 0) { |
153 |
|
free_if_alloc (pkr); |
154 |
|
return -1; |
155 |
|
} |
156 |
|
return 0; |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
/* check if the default key from the gpg.conf file is available in the |
/* check if the default key from the gpg.conf file is available in the |
161 |
keyring. if not, bail out because encryption won't work properly then. */ |
keyring. if not, bail out because encryption won't work properly then. */ |
162 |
static int |
static int |
164 |
{ |
{ |
165 |
gpgme_key_t key; |
gpgme_key_t key; |
166 |
gpgme_error_t err = GPG_ERR_NO_ERROR; |
gpgme_error_t err = GPG_ERR_NO_ERROR; |
167 |
char * defkey; |
char *defkey; |
168 |
|
|
169 |
defkey = get_gnupg_default_key (); |
defkey = get_gnupg_default_key (); |
170 |
if (defkey) |
if (defkey) |
212 |
|
|
213 |
/* Check that the underlying crypto engine fullfills the minimal |
/* Check that the underlying crypto engine fullfills the minimal |
214 |
requirements so all commands work properly. */ |
requirements so all commands work properly. */ |
215 |
static int |
static bool |
216 |
check_crypto_engine (void) |
check_crypto_engine (void) |
217 |
{ |
{ |
218 |
int ma=1, mi=4, pa=2; /* GPG 1.4.2 */ |
int ma=1, mi=4, pa=2; /* GPG 1.4.2 */ |
222 |
if (rc == -1) { |
if (rc == -1) { |
223 |
msg_box (NULL, _("Could not read GnuPG version."), |
msg_box (NULL, _("Could not read GnuPG version."), |
224 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
225 |
return rc; |
return false; |
226 |
} |
} |
227 |
else if (rc) { |
else if (rc) { |
228 |
log_box (_("WinPT Error"), MB_ERR, |
log_box (_("WinPT Error"), MB_ERR, |
229 |
_("Sorry, you need a newer GPG version.\n" |
_("Sorry, you need a newer GPG version.\n" |
230 |
"GPG version %d.%d.%d required GPG version "MIN_GPG_VER), |
"GPG version %d.%d.%d required GPG version "MIN_GPG_VER), |
231 |
ma, mi, pa); |
ma, mi, pa); |
232 |
return rc; |
return false; |
233 |
} |
} |
234 |
/* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */ |
/* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */ |
235 |
if (ma > 1 || pa >= 3) |
if (ma > 1 || pa >= 3) |
238 |
gpgver[0] = ma; |
gpgver[0] = ma; |
239 |
gpgver[1] = mi; |
gpgver[1] = mi; |
240 |
gpgver[2] = pa; |
gpgver[2] = pa; |
241 |
return rc; |
return true; |
242 |
} |
} |
243 |
|
|
244 |
|
|
263 |
} |
} |
264 |
|
|
265 |
|
|
266 |
|
/* Check if both keyrings are empty. This indicates that |
267 |
|
WinPT should offer to generate a key pair. */ |
268 |
|
static bool |
269 |
|
check_for_empty_keyrings (bool pub_only) |
270 |
|
{ |
271 |
|
char *p; |
272 |
|
int n = 0; |
273 |
|
|
274 |
|
p = get_gnupg_keyring (1, 0); |
275 |
|
if (file_exist_check (p) == 0 && get_file_size (p) == 0) |
276 |
|
n++; |
277 |
|
free_if_alloc (p); |
278 |
|
if (pub_only) |
279 |
|
return n == 1? true : false; |
280 |
|
p = get_gnupg_keyring (0, 0); |
281 |
|
if (file_exist_check (p) == 0 && get_file_size (p) == 0) |
282 |
|
n++; |
283 |
|
free_if_alloc (p); |
284 |
|
return n==2? true : false; |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
/* Enable the mobility mode. */ |
/* Enable the mobility mode. */ |
289 |
static void |
static void |
290 |
enable_mobile_mode (void) |
enable_mobile_mode (void) |
354 |
return 0; |
return 0; |
355 |
} |
} |
356 |
*/ |
*/ |
357 |
|
|
358 |
if (gpg_md_selftest ()) { |
if (gpg_md_selftest ()) { |
359 |
msg_box (NULL, _("Cryptographic selftest failed."), |
msg_box (NULL, _("Cryptographic selftest failed."), |
360 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
401 |
gnupg_load_config (); |
gnupg_load_config (); |
402 |
} |
} |
403 |
|
|
404 |
|
if (is_gpg4win_installed ()) |
405 |
|
load_gpg_env (); /* XXX: check return code. */ |
406 |
|
|
407 |
rc = gnupg_check_homedir (); |
rc = gnupg_check_homedir (); |
408 |
if (rc) { |
if (rc) { |
409 |
log_box (_("WinPT Error"), MB_ERR, |
log_box (_("WinPT Error"), MB_ERR, |
468 |
return 0; |
return 0; |
469 |
} |
} |
470 |
} |
} |
471 |
|
if (check_for_empty_keyrings (false)) |
472 |
|
first_start = 1; |
473 |
|
|
474 |
if (!first_start) { |
if (!first_start) { |
475 |
rc = gpg_check_permissions (1); |
rc = gpg_check_permissions (1); |
556 |
|
|
557 |
if (!first_start && !start_gpgprefs) { |
if (!first_start && !start_gpgprefs) { |
558 |
gnupg_backup_options (); |
gnupg_backup_options (); |
559 |
rc = check_crypto_engine (); |
if (!check_crypto_engine ()) { |
|
if (rc) { |
|
560 |
DestroyWindow (hwnd); |
DestroyWindow (hwnd); |
561 |
free_gnupg_table (); |
free_gnupg_table (); |
562 |
return 0; |
return 0; |
564 |
} |
} |
565 |
|
|
566 |
if (start_gpgprefs) { |
if (start_gpgprefs) { |
|
char *ring; |
|
567 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
568 |
gpgprefs_dlg_proc, 0); |
gpgprefs_dlg_proc, 0); |
569 |
ring = get_gnupg_keyring (0, !NO_STRICT); |
if (check_for_empty_keyrings (true)) |
570 |
if (gnupg_access_keyring (0) == -1 && get_file_size (ring) == 0) |
first_start = 1; /* The public keyring is empty! */ |
|
first_start = 1; /* The keyring is empty! */ |
|
|
free_if_alloc (ring); |
|
571 |
} |
} |
572 |
|
|
573 |
if (first_start) { |
if (first_start) { |
598 |
} |
} |
599 |
break; |
break; |
600 |
|
|
601 |
case -1: |
case -1: /* Cancel/Abort. */ |
602 |
DestroyWindow (hwnd); |
DestroyWindow (hwnd); |
603 |
free_gnupg_table (); |
free_gnupg_table (); |
604 |
return 0; |
return 0; |