33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
34 |
#include "wptErrors.h" |
#include "wptErrors.h" |
35 |
#include "wptW32API.h" |
#include "wptW32API.h" |
36 |
|
#include "wptCrypto.h" |
37 |
|
|
38 |
#define GPG_CONF "gpg.conf" |
#define GPG_CONF "gpg.conf" |
39 |
|
|
71 |
return m_strdup (buf); |
return m_strdup (buf); |
72 |
} |
} |
73 |
|
|
74 |
/* |
/* Return the full path of the GnuPG application. First the registry is scanned |
75 |
* Return the full path of the GnuPG application. First the registry is scanned |
for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
|
* for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
|
76 |
*/ |
*/ |
77 |
char* |
char* |
78 |
get_gnupg_path (void) |
get_gnupg_path (void) |
88 |
else |
else |
89 |
return multi_gnupg_path (); |
return multi_gnupg_path (); |
90 |
return m_strdup ("c:\\gnupg"); |
return m_strdup ("c:\\gnupg"); |
91 |
} /* get_gnupg_path */ |
} |
92 |
|
|
93 |
|
|
94 |
char* |
char* |
161 |
* appended string 'gpg.exe' is used. |
* appended string 'gpg.exe' is used. |
162 |
*/ |
*/ |
163 |
char* |
char* |
164 |
get_gnupg_prog( void ) |
get_gnupg_prog (void) |
165 |
{ |
{ |
166 |
char *p, *path, *pgm = NULL; |
char *p, *path, *pgm = NULL; |
167 |
size_t nlen = 0; |
size_t nlen = 0; |
183 |
|
|
184 |
|
|
185 |
static char * |
static char * |
186 |
default_key_from_cache (int * ret_no_useable) |
default_key_from_cache (int *ret_no_useable) |
187 |
{ |
{ |
188 |
const char * s; |
const char * s; |
189 |
char * keyid = NULL; |
char * keyid = NULL; |
190 |
gpgme_key_t key; |
gpgme_key_t key; |
191 |
gpgme_keycache_t sec = keycache_get_ctx (0); |
gpg_keycache_t sec = keycache_get_ctx (0); |
192 |
|
|
193 |
if (!sec) |
if (!sec) |
194 |
BUG (0); |
BUG (0); |
195 |
gpgme_keycache_rewind (sec); |
gpg_keycache_rewind (sec); |
196 |
while (!gpgme_keycache_next_key (sec, 1, &key)) |
while (!gpg_keycache_next_key (sec, 1, &key)) { |
197 |
{ |
if (key_is_useable (key)) { |
198 |
if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_USABLE, NULL, 0)) |
s = key->subkeys->keyid; |
|
{ |
|
|
s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0); |
|
199 |
if (s) |
if (s) |
200 |
keyid = m_strdup (s+8); |
keyid = m_strdup (s+8); |
201 |
break; |
break; |
202 |
} |
} |
203 |
} |
} |
204 |
if (!keyid) |
if (!keyid) { |
|
{ |
|
205 |
*ret_no_useable = 1; |
*ret_no_useable = 1; |
206 |
msg_box( NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR); |
msg_box( NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR); |
207 |
} |
} |
246 |
return keyid; |
return keyid; |
247 |
} /* get_gnupg_default_key */ |
} /* get_gnupg_default_key */ |
248 |
|
|
249 |
/* |
|
250 |
* Check if the gpg application (exe file) is available. |
/* Check if the gpg application (exe file) is available. */ |
|
*/ |
|
251 |
int |
int |
252 |
check_gnupg_prog( void ) |
check_gnupg_prog (void) |
253 |
{ |
{ |
254 |
char *pgm = NULL; |
char *pgm = NULL; |
255 |
int rc = 0; |
int rc = 0; |
256 |
|
|
257 |
pgm = get_gnupg_prog( ); |
pgm = get_gnupg_prog (); |
258 |
if( pgm == NULL ) |
if (!pgm) |
259 |
rc = WPTERR_GPG_EXEFILE; |
rc = WPTERR_GPG_EXEFILE; |
260 |
if( file_exist_check( pgm ) ) |
if (file_exist_check (pgm)) |
261 |
rc = WPTERR_GPG_EXEFILE; |
rc = WPTERR_GPG_EXEFILE; |
262 |
free_if_alloc( pgm ); |
free_if_alloc (pgm); |
263 |
return rc; |
return rc; |
264 |
} /* check_gpg_prog */ |
} |
265 |
|
|
266 |
|
|
267 |
static int |
static int |
268 |
parse_version_nr( const char * buf, int *major, int *minor, int *patch ) |
parse_version_nr (const char * buf, int *major, int *minor, int *patch) |
269 |
{ |
{ |
270 |
char tmp[8]; |
char tmp[8]; |
271 |
int i; |
int i; |
272 |
|
|
|
if( strncmp( buf, "gpg ", 4 ) ) |
|
|
return -1; |
|
|
buf += 4; |
|
|
if( strncmp( buf, "(GnuPG) ", 8 ) ) |
|
|
return -1; |
|
|
buf += 8; |
|
273 |
i=0; |
i=0; |
274 |
while( buf && *buf != '.' && i < 8 ) |
while( buf && *buf != '.' && i < 8 ) |
275 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
289 |
} |
} |
290 |
|
|
291 |
|
|
292 |
|
/* Check if the gnupg engine fullfills the minimum requirement |
293 |
|
version given in @r_major.@r_minor.@r_patch. On success these |
294 |
|
variables contain the GPG version which is installed. */ |
295 |
int |
int |
296 |
check_gnupg_engine (int * r_major, int * r_minor, int * r_patch) |
check_gnupg_engine (int *r_major, int *r_minor, int *r_patch) |
297 |
{ |
{ |
298 |
gpgme_error_t err; |
gpgme_ctx_t ctx; |
299 |
|
gpgme_engine_info_t inf; |
300 |
char * eng = NULL; |
char * eng = NULL; |
301 |
int major=0, minor=0, patch=0; |
int major=0, minor=0, patch=0; |
302 |
int rc; |
int rc; |
303 |
|
|
304 |
err = gpgme_op_version( &eng ); |
gpgme_new (&ctx); |
305 |
if( err ) |
inf = gpgme_ctx_get_engine_info (ctx); |
306 |
|
if (!inf) { |
307 |
|
gpgme_release (ctx); |
308 |
|
return -1; |
309 |
|
} |
310 |
|
/* We need to exec GPG again to find out if IDEA is available. */ |
311 |
|
if (gpg_get_version (&eng)) |
312 |
return -1; |
return -1; |
313 |
if( strstr( eng, "IDEA" ) ) |
if (strstr (eng, "IDEA")) |
314 |
idea_available = 1; |
idea_available = 1; |
315 |
rc = parse_version_nr( eng, &major, &minor, &patch ); |
free (eng); |
316 |
free( eng ); eng = NULL; |
rc = parse_version_nr( inf->version, &major, &minor, &patch ); |
317 |
if( rc ) |
if( rc ) { |
318 |
|
gpgme_release (ctx); |
319 |
return rc; |
return rc; |
320 |
if( major < *r_major |
} |
321 |
|| minor < *r_minor) |
if (major < *r_major || minor < *r_minor) |
322 |
rc = 1; |
rc = 1; |
323 |
else { |
else { |
324 |
if (patch < *r_patch ) |
if (patch < *r_patch) |
325 |
rc = 1; |
rc = 1; |
326 |
rc = 0; |
rc = 0; |
327 |
} |
} |
329 |
*r_minor = minor; |
*r_minor = minor; |
330 |
*r_patch = patch; |
*r_patch = patch; |
331 |
return rc; |
return rc; |
332 |
} /* check_gnupg_engine */ |
} |
333 |
|
|
334 |
|
|
335 |
int |
int |
651 |
} /* keyring_check_last_access */ |
} /* keyring_check_last_access */ |
652 |
|
|
653 |
|
|
654 |
const char * |
const char* |
655 |
gnupg_check_file_ext (const char * fname) |
gnupg_check_file_ext (const char *fname, int *r_type) |
656 |
{ |
{ |
657 |
char file_ext[5]; |
char file_ext[5]; |
658 |
|
|
659 |
if (!strchr( fname, '.' )) |
if (r_type) *r_type = PGP_NONE; |
660 |
|
if (!strchr (fname, '.' )) |
661 |
return "UNKNOWN"; |
return "UNKNOWN"; |
662 |
|
|
663 |
strncpy (file_ext, fname + strlen (fname) - 4, 4); |
strncpy (file_ext, fname + strlen (fname) - 4, 4); |
664 |
file_ext[4] = '\0'; |
file_ext[4] = '\0'; |
665 |
if (!stricmp (file_ext, ".asc")) |
if (!stricmp (file_ext, ".asc")) |
666 |
return "ARMORED"; |
return "ARMORED"; |
667 |
else if (!stricmp (file_ext, ".sig")) |
else if (!stricmp (file_ext, ".sig")) { |
668 |
|
if (r_type) |
669 |
|
*r_type = PGP_SIG; |
670 |
return "SIGNED"; |
return "SIGNED"; |
671 |
else if (!stricmp (file_ext, ".gpg") |
} |
672 |
|| !stricmp (file_ext, ".pgp")) |
else if (!stricmp (file_ext, ".gpg") || !stricmp (file_ext, ".pgp")) { |
673 |
|
if (r_type) |
674 |
|
*r_type = PGP_MESSAGE; |
675 |
return "ENCRYPTED"; |
return "ENCRYPTED"; |
676 |
else |
} |
|
return "UNKNOWN"; |
|
|
|
|
677 |
return "UNKNOWN"; |
return "UNKNOWN"; |
678 |
} /* gnupg_check_file_ext */ |
} |
679 |
|
|
680 |
|
|
681 |
char * |
char * |
904 |
|
|
905 |
|
|
906 |
void |
void |
907 |
gnupg_backup_options (int keep) |
gnupg_backup_options (void) |
908 |
{ |
{ |
909 |
char *optfile = NULL; |
char *cfgfile = NULL; |
910 |
char bak[1024]; |
char bak[512]; |
911 |
|
|
912 |
optfile = get_gnupg_cfgfile (); |
cfgfile = get_gnupg_cfgfile (); |
913 |
if (optfile == NULL) |
if (cfgfile == NULL) |
914 |
return; |
return; |
915 |
if (keep) |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
916 |
_snprintf (bak, DIM (bak)-1, "%s.old", optfile); |
CopyFile (cfgfile, bak, FALSE); |
917 |
else |
free_if_alloc (cfgfile); |
|
_snprintf (bak, DIM (bak)-1, "%s.O", optfile); |
|
|
CopyFile (optfile, bak, keep); |
|
|
free_if_alloc (optfile); |
|
918 |
} /* gnupg_backup_options */ |
} /* gnupg_backup_options */ |
919 |
|
|
920 |
|
|